Skip to content

Instantly share code, notes, and snippets.

@abd1rahmane
Created June 25, 2021 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abd1rahmane/c37b6d5b18b98d3a02ea5ce8b58e91e4 to your computer and use it in GitHub Desktop.
Save abd1rahmane/c37b6d5b18b98d3a02ea5ce8b58e91e4 to your computer and use it in GitHub Desktop.
Loading WASM in NodeJS
const fs = require("fs");
async function loadWASM(pathToWasm) {
/**
* A helper function to load Wasm.
*/
try {
const buf = fs.readFileSync(pathToWasm);
const { instance } = await WebAssembly.instantiate(new Uint8Array(buf));
return instance;
} catch (error) {
console.error("Error in loading Wasm ", error);
}
}
(async () => {
const instance = await loadWASM("./factorial.wasm");
if (instance) {
console.log("instance :", instance.exports);
for (var i = 1; i <= 10; i++) {
console.log(
"The factorial of " + i + " = " + instance.exports._Z4facti(i)
);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment