I wrote a "most minimal" C / JS interop demo a little while ago (only 
difference for C++ is that you need to wrap the exported functions in 
extern "C"):

https://github.com/floooh/emsc-interop-demo

The ccall helper function is only ever needed if you want marshalling from 
JS to C strings like here:

https://github.com/floooh/sokol/blob/c1bf1a9448e98cb4f626aa5fe837278ac8b12be0/sokol_args.h#L649-L657

Cheers!
On Saturday, 3 July 2021 at 03:53:07 UTC+2 [email protected] wrote:

> So I figured out that Module was not defined.
>
> And after a lot of failed examples, found this code that works...
> <script type="module">
>         import Module from '../cpp/sqrt2/example.js'
>         Module().then(function(mymod) {
>             console.log("Module Loaded")
>             const int_sqrt = mymod.cwrap('int_sqrt', 'number', ['number']);
>             console.log(int_sqrt(64));
>         }); 
> </script>
>
> when compiled with:
> emcc example.cpp -o example.js -s EXPORTED_FUNCTIONS="['_int_sqrt']" -s 
> EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s EXPORT_ES6=1 -s 
> MODULARIZE=1
>
> Now I just need to figure out how to split it up.
> I want the call to int_sqrt to be callable from a javascript function
>
> Thanks
>
>
> On Friday, July 2, 2021 at 3:21:28 PM UTC-6 Gary Stuart wrote:
>
>> Okay, so I took an example:
>>
>> #include <math.h>
>> extern "C" {
>>    int int_sqrt(int x) {
>>       return sqrt(x);
>>    }
>> }
>>
>> and put it in the file hello_function.cpp 
>>
>> I ran their command, in an emcmdprompt, and it produced errors:
>> emcc hello_function.cpp -o function.html -s 
>> EXPORTED_FUNCTIONS='["_int_sqrt"]' 
>> -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' 
>>
>> I found that swapping the double and single quotes solved that problem:
>> emcc hello_function.cpp -o function.html -s 
>> EXPORTED_FUNCTIONS="['_int_sqrt']" -s 
>> EXPORTED_RUNTIME_METHODS="['ccall','cwrap']"
>>
>> I put this line in the html file:
>>     <script type="text/javascript" src="/cpp/sqrt/function.js"></script>
>>
>> When I load the html file I get lots of errors:
>> Uncaught (in promise) TypeError: Cannot read property 'apply' of undefined
>>     at Module._emscripten_stack_init (function.js:1832)
>>     at stackCheckInit (function.js:2104)
>>     at run (function.js:2116)
>>     at runCaller (function.js:2095)
>>     at removeRunDependency (function.js:1451)
>>     at receiveInstance (factorial.js:1)
>>     at receiveInstantiationResult (factorial.js:1)
>>
>> Module._emscripten_stack_init @ function.js:1832
>> stackCheckInit @ function.js:2104
>> run @ function.js:2116
>> runCaller @ function.js:2095
>> removeRunDependency @ function.js:1451
>> receiveInstance @ factorial.js:1
>> receiveInstantiationResult @ factorial.js:1
>> function.js:1477 Uncaught (in promise) RuntimeError: abort(Assertion 
>> failed: undefined) at Error
>>     at jsStackTrace (function.js:1755)
>>     at stackTrace (function.js:1772)
>>     at abort (function.js:1471)
>>     at assert (function.js:678)
>>     at removeRunDependency (function.js:1438)
>>     at receiveInstance (function.js:1630)
>>     at receiveInstantiationResult (function.js:1647)
>>     at abort (function.js:1477)
>>     at assert (function.js:678)
>>     at removeRunDependency (function.js:1438)
>>     at receiveInstance (function.js:1630)
>>     at receiveInstantiationResult (function.js:1647)
>>
>>
>> As to         setup `Module['onRuntimeInitialized']` early on
>> I'm not sure of the proper syntax. I tried 
>>
>>     <script>
>>         Module['onRuntimeInitialized'] = onRuntimeInitializedFactorial;
>>     </script>
>>
>> and I get the error:
>> Uncaught ReferenceError: Module is not defined
>>
>> I feel like I'm missing something really basic that the rest of you just 
>> take for granted....
>>
>> I look forward to more clarification
>>
>> On Friday, July 2, 2021 at 2:53:15 PM UTC-6 [email protected] wrote:
>>
>>> Also, I believe you need to setup `Module['onRuntimeInitialized']` early 
>>> on, like at the top level of your script, preferably even before the module 
>>> JS file is imported.    IIUC, you need to make sure you register this 
>>> callback before the module is actually initialized, otherwise it might 
>>> never be called.   
>>>
>>> On Fri, Jul 2, 2021 at 1:05 PM Alon Zakai <[email protected]> wrote:
>>>
>>>> To use cwrap(), you must export it. See
>>>>
>>>>
>>>> https://emscripten.org/docs/api_reference/preamble.js.html?highlight=cwrap
>>>>
>>>> You can also run an example from the tutorial, which I verified now,
>>>>
>>>>
>>>> https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html?highlight=cwrap#interacting-with-an-api-written-in-c-c-from-nodejs
>>>>
>>>> It does have one typo which I'll fix now, the require should be of 
>>>> "api_example.js".
>>>>
>>>> On Fri, Jul 2, 2021 at 11:25 AM Gary Stuart <[email protected]> 
>>>> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I am trying to use emscripten on Windows 10.
>>>>> I have an existing Javascript client application that works.
>>>>> I want to connect in c++ code, and be able to call those c++ functions 
>>>>> when needed.
>>>>> I have spent 1.5 days and can't even make a simple example work.
>>>>>
>>>>> I am using emcc version: 2.0.25 (Online the latest version seems to be 
>>>>> 2.0.21???)
>>>>> I am using Chrome
>>>>>
>>>>> My example:
>>>>> function factorial()
>>>>> {
>>>>>     console.log("MT:F")
>>>>>     Module.onRuntimeInitialized = _ => {
>>>>>         console.log("A")
>>>>>
>>>>>         const  factorialCpp = Module.cwrap('factorial', 'number', 
>>>>> ['number']);
>>>>>         console.log(factorialCpp)
>>>>>         var result = factorialCpp(10);
>>>>>         console.log(result)
>>>>>     };   
>>>>>     console.log("MT:F-E")
>>>>> }
>>>>>
>>>>> When I call this function, I get only the "MT:F" and "MT:F-E", and no 
>>>>> errors in browser console.
>>>>>
>>>>> My compile line:
>>>>> emcc -O3 -s WASM=1 -s EXPORTED_RUNTIME_METHODS='["cwrap"]' -s 
>>>>> EXPORTED_FUNCTIONS="['_factorial']" factorial.cpp factorial-service.cpp 
>>>>> -o 
>>>>> factorial.js
>>>>>
>>>>> If optimization is -O0, it complains about the cwrap export
>>>>>
>>>>> I have also tried:
>>>>> function factorial2()
>>>>> {
>>>>>     Module['onRuntimeInitialized'] = onRuntimeInitializedFactorial;
>>>>>
>>>>>     const factorialCpp = Module.cwrap('factorial', 'number', ['number']);
>>>>>
>>>>>     function onRuntimeInitializedFactorial()
>>>>>     {
>>>>>         console.log("MT:F")
>>>>>             console.log("A")
>>>>>             var result = factorialCpp(10);
>>>>>             console.log(result)
>>>>>         console.log("MT:F-E")
>>>>>     }
>>>>> }
>>>>>
>>>>> and:
>>>>> function factorial3()
>>>>> {
>>>>>     var factorialCpp = Module.cwrap("factorial", "number", ["number"]);
>>>>>     var result = factorialCpp(10);
>>>>> }
>>>>>
>>>>> Both produce an error: Uncaught TypeError: Module.cwrap is not a 
>>>>> function
>>>>>
>>>>> I have found over 20 examples, that say just do this or that, but 
>>>>> nothing works.
>>>>> Maybe my environment has a config issue?
>>>>>
>>>>> I would appreciate any any guidance on this.
>>>>> Thanks,
>>>>> Gary
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> -- 
>>>>> 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/56840ae5-46b1-458d-b30f-27ee5433fd39n%40googlegroups.com
>>>>>  
>>>>> <https://groups.google.com/d/msgid/emscripten-discuss/56840ae5-46b1-458d-b30f-27ee5433fd39n%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/CAEX4NpQ21Yoyvy_xzrLkBTWmvEQoyVWc0fG3kAMmgx%3DmrhxE9g%40mail.gmail.com
>>>>  
>>>> <https://groups.google.com/d/msgid/emscripten-discuss/CAEX4NpQ21Yoyvy_xzrLkBTWmvEQoyVWc0fG3kAMmgx%3DmrhxE9g%40mail.gmail.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/26776ea0-9048-4d66-9147-e750e8d51de8n%40googlegroups.com.

Reply via email to