编译hello world到wasm并运行在node中

我试图在node.js中运行一个webassembly模块,我得到一个Wasm decoding failedResult 。 我认为我的问题是将节点Buffer转换为ArrayBuffer 。 这是我的适用代码:

 fs.readFileAsync( WASM_PATH ) .then( buf => buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) ) .then( arrayBuffer => Wasm.instantiateModule(arrayBuffer) ) .then( results => console.log(results.instance) ) .catch( err => console.error(err) ); 

我得到这个错误:

 (relevant details) Wasm decoding failedResult = expected version 0c 00 00 00, found 01 00 00 00 @+4 

当我使用emcc hello_world.c -s WASM=1 -o hello.html我可以加载模块并在浏览器中运行它。 所以,我很确定这是我的一个问题,或者可能是一些兼容性问题。 提前致谢。

您的节点版本使用V8以前版本的MVP,该版本需要版本0xC 。 您的工具链发出现在稳定的MVP版本0x1

节点大致跟随Chrome版本 , Chrome 57增加了对MVP WebAssembly的支持。 该页面说,V5版本的Chrome 57是5.7.492.65。

因此对Node的支持迫在眉睫。

或者,您可以使用旧的Emscripten工具链。 这将是相当古老的: 0xd是MVP之前的版本(和0xd实际上是相同的MVP, 0xd )。