Reusing inline JavaScript with EM_JS()

If you need a reusable function within your C/C++ file, you can wrap JavaScript code within an EM_JS() block and execute it like a normal C/C++ function. The definition for EM_JS() is described in the following code snippet:

EM_JS(return_type, function_name, arguments, code)

The return_type parameter represents the C type that corresponds with the JavaScript code's output (for example, int or float). If nothing is returned from the JavaScript code, specify void for the return_type. The next parameter, function_name, represents the name to use when calling the JavaScript code from other locations in the C/C++ file. The arguments parameter is used to define arguments that can be passed into the JavaScript code from the C calling function. The code parameter is the JavaScript code that's wrapped in curly braces. The following code snippet, taken from the Emscripten site, demonstrates the use of EM_JS() in a C file:

#include <emscripten.h>

EM_JS(void, take_args, (int x, float y), {
console.log(`I received ${x} and ${y}`);
});

int main() {
take_args(100, 35.5);
return 0;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset