Search code examples
javascriptwebassembly

Pass pointer of an object built in javascript land to webassembly (C++) function


I have a function in C++ that takes a (const char*) argument. I want to call this function in JavaScript (through Web assembly) by passing to it the address of an object created in JavaScript. Is this possible?

The code is something like

// Create an object by loading from url.
const encoder = await loadModel("some_url");
const data = [1,2,3];

Module.onRuntimeInitialized = _ => {
  // Call the wasm function with a pointer to the encoder.
  Module.encode(&encoder, data);
}

How can I take the address of the encoder object to pass to the function?


Solution

  • It is common to serialize the JavaScript object to a primitive string to pass between programming languages.