Expose C++ object to JavaScript (or the other way around) with Embind

821 views
Skip to first unread message

Jendker

unread,
Sep 12, 2018, 10:46:34 AM9/12/18
to emscripten-discuss
Hello everyone,

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...

Thank you!

Brion Vibber

unread,
Sep 12, 2018, 12:16:43 PM9/12/18
to emscripten Mailing List
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

Jendker

unread,
Sep 13, 2018, 5:40:05 AM9/13/18
to emscripten-discuss
Thank you! That works like a charm.
What about the proper cleaning of the object? Will it be taken care of by some garbage collector in the browser?

Jendker

Brion Vibber

unread,
Sep 13, 2018, 8:55:07 AM9/13/18
to emscripten Mailing List
Since it's a global and nothing explicitly deletes it, it'll live as long as the Module does. When the page is closed, the whole page including Module and all its memory gets garbage collected.

If you're using modularized mode, then you should be able to GC a whole module instance by removing all references to it or into it.

-- 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 emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jendker

unread,
Sep 13, 2018, 10:34:47 AM9/13/18
to emscripten-discuss
Thank you Brion!

Keith Rosenberg

unread,
Oct 26, 2020, 11:15:28 PM10/26/20
to emscripten-discuss
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!

Brion Vibber

unread,
Oct 27, 2020, 11:05:12 AM10/27/20
to emscripten Mailing List
Sorry, I've never used webidl so can't help with it.

-- 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 emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/96d0252b-5894-4b92-a1c3-309781858792n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages