【问题标题】:Does node-chakracore support WASM (Web Assembly)?node-chakracore 是否支持 WASM(Web 程序集)?
【发布时间】:2017-07-13 20:24:55
【问题描述】:

我可以将相同的 WASM javascript 模块部署到 node-chakracore,就像我可以部署到 nodejs v8 一样吗?

【问题讨论】:

    标签: node.js webassembly chakracore


    【解决方案1】:

    ChakraCore 从 v1.4 开始支持 WebAssembly,node-chakracore 从 8.x 开始通过 JavaScript 支持它:

    如果您使用 WebAssembly,Node-ChakraCore 支持 WASM 来自 JavaScript 的方法。从这里使用 basic.wasm,以下 使用 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

    【讨论】:

      【解决方案2】:

      或者,您可以使用 node-wasm 加载您的 wasm 文件,然后在您的节点 js 应用程序中执行以下操作:

      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();
      

      在同一个 repo 中有一个完整的示例 here。祝你好运!

      【讨论】:

        猜你喜欢
        • 2021-02-26
        • 1970-01-01
        • 2012-08-19
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        相关资源
        最近更新 更多