Created
June 25, 2021 00:17
-
-
Save abd1rahmane/c37b6d5b18b98d3a02ea5ce8b58e91e4 to your computer and use it in GitHub Desktop.
Loading WASM in NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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