title: WebAssembly calling JavaScript methods from wasm i.e. within C++ code
body: <p>I was playing with WebAssembly and so far and I was able to manage emscripten compile my test C++ project to wasm file
  em++ provides me 2 files i.e.</p>
  
  <p>mainTest.js  mainTest.wasm</p>
  
  <p>When I load mainTest.js in my html page then I get a JavaScript object called 
  "<strong>Module</strong>".</p>
  
  <p>I did found how to call C++/wasm methods from javascript i.e. something like:</p>
  
  <pre><code>var myTestInteger = Module._callMyTestMethod();
  </code></pre>
  
  <p>and read strings from the 
  <strong>Module.wasmMemory.buffer</strong>
  , but I do NOT understand how to call JavaScript from C++ code.</p>
  
  <p>i.e. I would like to be able to do something like that:</p>
  
  <pre><code>#ifdef __cplusplus
  extern "C" {
  #endif
  extern void testExternJSMethod();
  
  int main() 
  {
      cout &lt;&lt; " Hello From my Test1 !" &lt;&lt; endl;
  
      testExternJSMethod();
      return 0;
  }
  int EMSCRIPTEN_KEEPALIVE callMyTestMethod(){
      return 26;
  }
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>and the my js method testExternMethod that I am loading in another js file called utils.js</p>
  
  <pre><code>function testExternMethod() {
    console.log("Hello from testExternMethod!" + )
  }
  </code></pre>
  
  <p>Here I would like to call the JavaScript testExternJSMethod from C++.</p>
  
  <p>When I run the page in Firefox get "-1" in the debugger console.</p>
  
  <p>So what I am missing in this case? Unfortunately The Mozilla documentation is giving only examples in those S-expressions instead of C++.</p>
  
  <p>What am I missing in example? In C++ I have defined the method with the extern keyword i.e.</p>
  
  <pre><code>extern void testExternJSMethod();
  </code></pre>
  
  <p>but I get the feeling that that is not all I have to do.</p>
  
  <p>I believe that I should somehow link that JavaScript method to the Module somehow but I do not know how. 
  <strong>Module.asm</strong> gives me the <strong>exports</strong>. Which method call should give me the <strong>imports</strong>? since I believe that this <code>_testExternJSMethod()</code> should be in some imports method I can not figure out how to get to it.</p>