Sorry, I've never used webidl so can't help with it. -- brion
On Mon, Oct 26, 2020, 8:15 PM Keith Rosenberg <[email protected]> wrote: > Hey Brion! Do you happen to have an example of the WebIDL way of doing > this same thing? I tried this: > > ``` > interface Application { > static Application Instance(); > }; > ``` > but glue doesn't love it: > ``` > ./glue/glue.cpp:27:10: error: no viable conversion from returned value of > type 'Application' to function return type 'Application *' > return self->Instance(); > ``` > for a class that looks like > ``` > class Application > { > public: > Application(const std::string& name = "App"); > static Application& Instance() { return *s_Instance; }; > private: > std::string m_Name; > static Application* s_Instance; > }; > ``` > Thanks! > On Wednesday, September 12, 2018 at 12:16:43 PM UTC-4 [email protected] > wrote: > >> On Wed, Sep 12, 2018 at 7:46 AM Jendker <[email protected]> wrote: >> >>> I am writing an application, where I would need to call the functions of >>> the object existing in C++ from JavaScript, but for now I did not find any >>> reasonable solution reading Embind documentation. >>> I know, that it would be possible to create object in JavaScript and >>> call each function from JavaScript, but I would like to avoid it, just keep >>> the bulk of code in C++. >>> >>> Is there any way to create object in C++ and access it from JavaScript, >>> or create object in JavaScript and access it from C++? (both would be fine) >>> I could just create object as global variable in C++ and call it every >>> time from the JavaScript binded function, which would be accessable, but >>> that would be far from clean design... >>> >> >> You can't directly access C++ global variables via embind, but you can >> export a function which will return the variable (this is known as the >> "singleton pattern") and export that to JavaScript. You need to also bind >> the class, or else embind won't know how to expose the instance methods etc. >> >> Something like this ought to work: >> >> class MyClass { >> ... >> public: >> void doSomething(); >> } >> >> static MyClass* singleton_val; >> >> MyClass* singleton() { >> if (singleton_val == NULL) { >> singleton_val = new MyClass; >> } >> return singleton_val; >> } >> >> EMSCRIPTEN_BINDINGS(my_module) { >> class_<MyClass>("MyClass") >> ... bindings for class ...; >> function("singleton", &singleton, allow_raw_pointers()); >> } >> >> (Or set the value from your main() and ensure it gets called before use.) >> >> >> Then from the JS side you'd call it like: >> >> Module.singleton().doSomething(); >> >> -- brion >> > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/emscripten-discuss/96d0252b-5894-4b92-a1c3-309781858792n%40googlegroups.com > <https://groups.google.com/d/msgid/emscripten-discuss/96d0252b-5894-4b92-a1c3-309781858792n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAFnWYTkn3-A_LRkJ45cn34Y8%2BQOabtBmEcvyC5hd4N%3DfYsN54g%40mail.gmail.com.
