3

I read this tutorial about wasm and after point 3. "Calling a custom function defined in C" I tried to figure out how to bind C++ functions and classes to JavaScript in that manner? Do I have to use Embind there for, but how?

I also found this thread and the second answer from @lacenen could be a workaround.

Community
  • 1
  • 1
Hölderlin
  • 424
  • 1
  • 3
  • 16

1 Answers1

0

Most of the time you just need to call a function in WASM. So the simple way:

#ifdef __cplusplus
extern "C" {
#endif

void EMSCRIPTEN_KEEPALIVE MyFunc()
{
    printf("MyFunc()\n");
}

#ifdef __cplusplus
}
#endif

Compiling with EmScripten you'll have a .js file with Module defined. So on the JavaScript side you can use ccall -- here's the call without params or return value:

Module.ccall('MyFunc', null, null, null);

See: https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-ccall-cwrap

nzeemin
  • 901
  • 8
  • 17