You are going about this the entirely wrong way. I simply do this in C++ to easily control my web content:
#include <../emsdk/emscripten/master/system/include/emscripten.h>
/* in your C++ code somewhere */
emscripten_run_script('alert(\"The operation is completed!\");');
emscripten_run_script will execute arbitrary Javascript code in the context of the document where the webassembly is loaded.
EDIT
If you wanted to perform the operation in reverse (calling a C++ method from the javascript context), you would want to expose the C++ method to Javascript like this:
#include <emscripten/bind.h>
int myFunction(int x) {
// Your C++ code here
return x * 2; // Example operation
}
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("myFunction", &myFunction);
}
Now in the Javascript context:
// Call the C++ function from JavaScript
const result = Module.myFunction(10);
// Decide what to do with the result
console.log(result); // Use the result in your JavaScript code
You can even pass Javascript functions as callbacks using emscripten::val and use them like any other javascript callback.
#include <emscripten.h>
void myFunctionWithCallback(int x, emscripten::val callback) {
int result = x * 2; // Example operation
// Convert the result to a val before passing to callback
callback(emscripten::val(result)); }
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("myFunctionWithCallback", &myFunctionWithCallback); }