Does node-chakracore support WASM (Web Assembly)?

346 Views Asked by At

Can I deploy the same WASM javascript modules to node-chakracore as I can to nodejs v8?

2

There are 2 best solutions below

0
On

ChakraCore has supported WebAssembly since v1.4, and node-chakracore has supported it via JavaScript since 8.x:

WASM is supported in Node-ChakraCore if you're using the WebAssembly methods from JavaScript. Using basic.wasm from here, the following code worked with Node-ChakraCore:

const fs = require('fs'); const buf = fs.readFileSync('basic.wasm')

async function test() {
    try {
        const module = await WebAssembly.compile(buf);
        const inst = new WebAssembly.Instance(module, {test: {foo: function(a){console.log(`foo called: ${a}`); return 2;}}});
        console.log(inst.exports.a(1));
    } catch (reason) { 
        console.log(`Failed: ${reason}`)
    } }

test();

https://github.com/sass/node-sass/pull/1777#discussion_r127280773

0
On

Alternatively, you can use node-wasm to load your wasm file and then in your node js app, do this:

import loadWasm from 'node-wasm';

async function run() {
  const {rust_function} = await loadWasm('/local/path/to/wasm');
  const result = rust_function();
  console.log(result);
}

run();

There's a complete example here in the same repo. Good luck!