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

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.

主站蜘蛛池模板: 肇庆市| 毕节市| 唐海县| 乌拉特前旗| 阿拉善右旗| 麻城市| 休宁县| 延长县| 宁德市| 西青区| 临夏县| 庆云县| 乌兰浩特市| 云南省| 西乌珠穆沁旗| 保德县| 紫金县| 桃园市| 武鸣县| 阿勒泰市| 巴东县| 龙南县| 马龙县| 连山| 闸北区| 舞钢市| 壤塘县| 铁岭市| 金华市| 乡城县| 琼中| 莫力| 平山县| 葵青区| 稻城县| 云和县| 离岛区| 兴宁市| 军事| 从江县| 安庆市|