Let's explore the opposite idea. Imagine we want to give some text to a WebAssembly program.
We must:
Here's an example of what that initialization looks like:
// Turn "Ferris" into bytes
const utf8enc = new TextEncoder("utf-8");
let text = "Ferris";
let text_bytes = utf8enc.encode(text);
// Allocate enough space for the text
let len = text_bytes.length;
let start = module.instance.exports.wasm_malloc(len);
// Put the text in WebAssembly program's memory
let buffer = module.instance.exports.memory.buffer;
let memory = new Uint8Array(buffer);
memory.set(text_bytes, start);
// Run the program
module.instance.exports.main(start,len);