Coder Social home page Coder Social logo

Comments (4)

ftk avatar ftk commented on August 30, 2024 1

Currently testing this feature in sharedptr branch
fa6c664

from quickjspp.

cykoder avatar cykoder commented on August 30, 2024

I think the only way to achieve this is to replace myClassOpaque->exampleMethod(); with a quickjs call to invoke exampleMethod using myClassOpaque as an opaque object. C++ doesnt (and cant?) know that exampleMethod was overriden with JS code.

from quickjspp.

projectitis avatar projectitis commented on August 30, 2024

Thanks Sam. It's what I was expecting might be the case.

The object myClass is created in javascript. Do you know how I can get the JSValue for myClass from the C++ side? I'd like to make this friendly for the user, so avoiding messy solutions like the user passing it back from javascript to c++ via a method call.

On the C++ side I would like to do something like:

// C++ (psuedo code for brevity)
void myClass::exampleMethod() {
    JSValue jsObj = JS_getObjectForOpaque( this ); // <-- how to do this?
    if (jsObj && JS_HasProperty( jsObj , "exampleMethod" )) {
        JS_EvalThis( jsObj, "this.exampleMethod();" )
    }
}

from quickjspp.

cykoder avatar cykoder commented on August 30, 2024

I'm actually struggling with that same problem just for objects that have been instantiated in JS. EG:

class MyThing extends Thing {
  constructor() {
    super();
  }

  onUpdate() {
     // blah
  }
}

In C++, MyThing::doSomething wants to call the onUpdate method:

  qjs::Value thisVal = getContext()->newValue(this); // i think this is wrong, need to get existing value from opaque
  JSAtom propAtom = JS_NewAtom(ctx, "onUpdate");
  if (JS_HasProperty(ctx, thisVal.v , propAtom )) {
    JSValue funcVal = JS_GetProperty(ctx, thisVal.v, propAtom);
    const char* str = JS_ToCString(ctx, funcVal);
    if (str) {
      LOG_INFO << str;
    }
    JS_FreeCString(ctx, str);

    JS_Call(ctx, funcVal, thisVal.v, 0, NULL);
  } else {
    // doesnt seem to be set, this is what keeps happening
  }

the not set codepath is being taken unless the onUpdate method was defined in c++ too. likely a wrong thisVal - but also not sure how to get jsvalue from my c++ instance. @ftk any ideas?

Previously with dukglue I was able to do dukglue_pcall_method<void>(getContext(), this, "onUpdate", (double)delta); quite easily. Would be great if quickjshpp supported this. If not a pure quickjs way would be appreciated

from quickjspp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.