Thank you Brion!

W dniu czwartek, 13 września 2018 14:55:07 UTC+2 użytkownik Brion Vibber 
napisał:
>
> 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
>
>
> On Thu, Sep 13, 2018, 2:40 AM Jendker <[email protected] <javascript:>> 
> wrote:
>
>> 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
>>
>> W dniu środa, 12 września 2018 18:16:43 UTC+2 użytkownik Brion Vibber 
>> napisał:
>>>
>>> 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
>> For more options, visit https://groups.google.com/d/optout.
>>
>

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

Reply via email to