-
Building with import { readFile } from "node:fs/promises";
const wasm = await readFile("./argon2.wasm");
const emscripten_notify_memory_growth = (memoryIndex) =>
console.log("emscripten_notify_memory_growth", memoryIndex);
const imports = { env: { emscripten_notify_memory_growth } };
const { instance } = await WebAssembly.instantiate(wasm, imports);
// Uncomment this to make emmalloc work
// Note that this allocation needs to be > 48
/*
const ptr1 = instance.exports.malloc(49);
instance.exports.free(ptr1);
*/
console.log(instance.exports.malloc(10));
console.log(instance.exports.malloc(10));
console.log(instance.exports.malloc(10)); Output
After uncommenting the marked section or removing
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
While investigating this problem trying to use import { readFile } from "node:fs/promises";
import { WASI } from "node:wasi";
const binary = await readFile("./src/argon2.wasm");
const wasi = new WASI({
version: "preview1"
});
const emscripten_notify_memory_growth = (memoryIndex) =>
console.log("emscripten_notify_memory_growth", memoryIndex);
const imports = { emscripten_notify_memory_growth };
const $imports = { env: imports, ...wasi.getImportObject() };
const { instance } = await WebAssembly.instantiate(binary, $imports);
wasi.initialize(instance);
console.log(instance.exports.malloc(10));
console.log(instance.exports.malloc(10));
console.log(instance.exports.malloc(10)); Output
Now I'm even more confused 😅 Edit It's actually |
Beta Was this translation helpful? Give feedback.
-
Figured it out. I have to call import { readFile } from "node:fs/promises";
const binary = await readFile("./src/argon2.wasm");
const emscripten_notify_memory_growth = (memoryIndex) =>
console.log("emscripten_notify_memory_growth", memoryIndex);
const imports = { emscripten_notify_memory_growth };
const $imports = { env: imports, wasi_snapshot_preview1: imports };
const { instance } = await WebAssembly.instantiate(binary, $imports);
instance.exports._initialize();
console.log(instance.exports.malloc(10));
console.log(instance.exports.malloc(10));
console.log(instance.exports.malloc(10));
|
Beta Was this translation helpful? Give feedback.
Figured it out. I have to call
instance.exports._initialize()
to make this work.