JavaScript 调用 WebAssembly 方法

WebAssembly 在线Editor

WebAssembly Studioicon-default.png?t=LA92https://webassembly.studio/ C源码:

WASM_EXPORT
int add( int a, int b){
  return a + b;
}

JS调用

 fetch('./math.wasm').then(response =>
            
            response.arrayBuffer()

        ).then( bytes => WebAssembly.instantiate(bytes) ).then( results => {

            instance = results.instance;

            const add = instance.exports.add;
            
            int result = add(1, 2);

            console.log(" 1 + 2 = " + result );

        }).catch(console.error);

猜你喜欢

转载自blog.csdn.net/qq_39162566/article/details/121425016