官术网_书友最值得收藏!

  • Learn WebAssembly
  • Mike Rourke
  • 178字
  • 2021-08-13 15:38:55

Wasm to JavaScript

The upper-right pane in the following screenshot contains some JavaScript code to compile the Wasm that was generated in the previous step. The wasmCode was generated when the build finished, so it should be available automatically. Rather than use the instantiate() method, WasmFiddle creates a compiled WebAssembly.Module instance and passes that into the constructor of a new WebAssembly.Instance. The wasmImports object is currently empty, although we could pass in a WebAssembly.Memory and WebAssembly.Table instance if desired:

JavaScript code calling the C function from the compiled Wasm module

The final line of JavaScript prints the result of addTwo() to the output in the lower-right pane when passed the number 2. The log() method is a custom function that ensures the output is printed to the lower-right pane (the number 4). Note how the JavaScript code interacts with wasmInstance. The addTwo() function is called from the instance's exports object. Although this was a contrived example, it demonstrates the steps C or C++ code goes through before it can be used by JavaScript as a Wasm module.

主站蜘蛛池模板: 平江县| 庐江县| 榆社县| 杭锦后旗| 通化县| 渑池县| 北海市| 磴口县| 西畴县| 杭锦旗| 香港| 吉水县| 湘乡市| 莱西市| 正宁县| 信宜市| 龙门县| 红桥区| 永顺县| 丰县| 三明市| 榕江县| 江安县| 望谟县| 綦江县| 南通市| 吐鲁番市| 黄浦区| 广元市| 云梦县| 萝北县| 五峰| 运城市| 莱阳市| 樟树市| 寿阳县| 延边| 泰和县| 布尔津县| 凉城县| 襄樊市|