-
-
Save hoangpq/b754cbb18fd024ea9bc3cdf17ba9ef48 to your computer and use it in GitHub Desktop.
Revisions
-
kanaka revised this gist
Jun 8, 2017 . 1 changed file with 2 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,14 +19,9 @@ should get you going quickly. ### Prerequisites * Install [node 8](https://nodejs.org/en/download/current/) * You need a *wast* to *wasm* compiler. The are several options. This example uses [wabt](https://github.com/WebAssembly/wabt/) (wabbit). You will need a standard gcc toolchain to build wabt. Build wabt and add it to your path: -
kanaka revised this gist
Apr 28, 2017 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -63,3 +63,13 @@ node ``` You are free to use `runwasm.js` code under an MIT license. You can also run wasm modules using [wac](https://github.com/kanaka/wac) (WebAssembly in C). wac is a WebAssembly interpreter in which the code runs in a native x86 host context rather than in a JavaScript/Web host context. ```bash wac addTwo.wasm addTwo 2 3 0x5:i32 ``` -
kanaka revised this gist
Apr 28, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ should get you going quickly. ### Prerequisites * Install node 8 nightly (has WebAssembly MVP support) and add it to your path: ``` curl https://nodejs.org/download/nightly/v8.0.0-nightly20170427892ce06dbd/node-v8.0.0-nightly20170427892ce06dbd-linux-x86.tar.gz | tar -xz -
kanaka revised this gist
Apr 28, 2017 . 2 changed files with 23 additions and 52 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ Run wast (WebAssembly) in node ------------------------------ This is a simple example of compiling and running the WebAssembly textual format (*wast*) in Node.js on Linux. This does not cover compiling from a higher level language to WebAssembly (for that you probably want [Emscripten](https://github.com/kripken/emscripten/)). But if all you want to do is play around directly with the *wast* textual format, this @@ -19,73 +19,44 @@ should get you going quickly. ### Prerequisites * Install node 8 nightly and add it to your path: ``` curl https://nodejs.org/download/nightly/v8.0.0-nightly20170427892ce06dbd/node-v8.0.0-nightly20170427892ce06dbd-linux-x86.tar.gz | tar -xz export PATH=`pwd`/node-v8.0.0-nightly20170427892ce06dbd-linux-x86/bin ``` * You need a *wast* to *wasm* compiler. The are several options, this example uses [wabt](https://github.com/WebAssembly/wabt/) (wabbit). You will need a standard gcc toolchain to build wabt. Build wabt and add it to your path: ```bash git clone --recursive https://github.com/WebAssembly/wabt/ cd wabt make gcc-release export PATH=`pwd`/out/gcc/Release ``` ### Compile and Run Wast Code Compile a *wast* to *wasm* (binary module): ```bash wast2wasm addTwo.wast -o addTwo.wasm ``` Run an exported function in the wasm binary module using the `runwasm.js` script (this should print 5 to the console): ```bash node ./runwasm.js addTwo.wasm addTwo 2 3 ``` The `runwasm.js` script is also written to be used as a node module: ```js node > w = require('./runwasm.js') > w.loadWebAssembly('addTwo.wasm').then(i => console.log(i.exports.addTwo(7,8))) 15 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/usr/bin/env node // Copyright Joel Martin // License MIT -
kanaka revised this gist
Mar 6, 2017 . No changes.There are no files selected for viewing
-
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,10 @@ function toUint8Array(buf) { return u } // Based on: // https://gist.github.com/kripken/59c67556dc03bb6d57052fedef1e61ab // and // http://thecodebarbarian.com/getting-started-with-webassembly-in-node.js.html // Loads a WebAssembly dynamic library, returns a promise. // imports is an optional imports object @@ -67,4 +70,4 @@ if (module.parent) { .catch(res => { console.log(res) }) } -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -74,22 +74,21 @@ Compile a *wast* to *wasm* (binary module): ``` Run an exported function in the wasm binary module using the `runwasm.js` script (this should print 5 to the console): ```bash ./runwasm.js addTwo.wasm addTwo 2 3 ``` The `runwasm.js` script is also written to be used as a node module (note that `--expose-wasm` is require to enable WebAssembly support in node 7.x): ```js node --expose-wasm > w = require('./runwasm.js') > w.loadWebAssembly('addTwo.wasm').then(i => console.log(i.exports.addTwo(7,8))) 15 ``` You are free to use `runwasm.js` code under an MIT license. -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ should get you going quickly. * 6-part illustrated intro to WebAssembly: https://hacks.mozilla.org/2017/02/a-cartoon-intro-to-webassembly/ * WebAssembly Text Format: https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format * Detailed Intro to WebAssembly: https://rsms.me/wasm-intro ### Prerequisites -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,14 @@ want to do is play around directly with the *wast* textual format, this should get you going quickly. ### Links * 6-part illustrated intro to WebAssembly: https://hacks.mozilla.org/2017/02/a-cartoon-intro-to-webassembly/ * WebAssembly Text Format: https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding\_the\_text\_format * Detailed Intro to WebAssembly: https://rsms.me/wasm-intro ### Prerequisites * Install node 7.x -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -72,7 +72,10 @@ Run an exported function in the wasm binary module using the ./runwasm.js addTwo.wasm addTwo 2 3 ``` The `runasm.js` script is also written to be used as a node module (note that `--expose-wasm` is require to enable WebAssembly support in node 7.x): ```js node --expose-wasm -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 25 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,32 +42,43 @@ should get you going quickly. * Download and build a branch of the spec interpreter (binary-0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone https://github.com/WebAssembly/spec cd spec/interpreter git checkout binary-0xc make opt ``` ### Compile and Run Wast Code Compile a *wast* to *wasm* (binary module): * **Option #1: using wast2wasm from wabt**: ```bash wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm ``` * **Option #2: using spec interpreter**: ```bash spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasm ``` Run an exported function in the wasm binary module using the `runasm.js` script (this should print 5 to the console): ```bash ./runwasm.js addTwo.wasm addTwo 2 3 ``` The `runasm.js` script is also written to be used as a node module: ```js node --expose-wasm > w = require('./runasm.js') > w.loadWebAssembly('addTwo.wasm').then(i => console.log(i.exports.addTwo(7,8))) 15 ``` You are free to use `runasm.js` code under an MIT license. -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,12 +27,12 @@ should get you going quickly. * Download and build a branch of wabt (binary\_0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone --recursive https://github.com/WebAssembly/wabt/ cd wabt git checkout binary_0xc make gcc-release ``` * **Option #2: spec interpreter** -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/usr/bin/node --expose-wasm // Copyright Joel Martin // License MIT const fs = require('fs'), assert = require('assert') -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ #!/usr/bin/node --expose-wasm // Copyright Joel Martin // License MPL-2.0 const fs = require('fs'), assert = require('assert') -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 21 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,10 @@ Run wast (WebAssembly) in node ------------------------------ This is a simple example of compiling and running the WebAssembly textual format (*wast*) in Node.js. This does not cover compiling from a higher level language to WebAssembly (for that you probably want [Emscripten](https://github.com/kripken/emscripten/)). But if all you want to do is play around directly with the *wast* textual format, this should get you going quickly. @@ -15,16 +15,17 @@ should get you going quickly. ([instructions](https://nodejs.org/en/download/current/)). I've tested this with node 7.7.1 * You need a *wast* to *wasm* compiler. There are two fairly straightfoward options: [wabt](https://github.com/WebAssembly/wabt/) (wabbit) and the official [WebAssembly interpreter](https://github.com/WebAssembly/spec/tree/master/interpreter). * **Option #1: wabt** * You'll need a standard gcc toolchain to build wabt * Download and build a branch of wabt (binary\_0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone --recursive https://github.com/WebAssembly/wabt/ @@ -33,13 +34,13 @@ git checkout binary_0xc make gcc-release ``` * **Option #2: spec interpreter** * You'll need an Ocaml compiler version 4.02 or later (ocaml-nox on Ubuntu) * Download and build a branch of the spec interpreter (binary-0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone https://github.com/WebAssembly/spec @@ -48,24 +49,24 @@ git checkout binary-0xc make opt ``` ### Compile and Run Wast Code Compile a *wast* to *wasm* (binary module): * **Option #1: using wast2wasm from wabt**: ```bash wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm ``` * **Option #2: using spec interpreter**: ```bash spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasm ``` Run an exported function in the wasm binary module using the `runasm.js` script: ```bash ./runwasm.js addTwo.wasm addTwo 2 3 -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 5 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,22 +45,20 @@ make gcc-release git clone https://github.com/WebAssembly/spec cd spec/interpreter git checkout binary-0xc make opt ``` ### Compile and Run wast code Compile a wast to wasm (binary module): * Using wast2wasm from wabt: ```bash wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm ``` * Using spec interpreter: ```bash spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasm @@ -70,5 +68,5 @@ Run an exported function in the wasm binary module using the runasm.js script: ```bash ./runwasm.js addTwo.wasm addTwo 2 3 ``` -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 18 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,12 +26,12 @@ should get you going quickly. * Download and build a branch of wabt (binary\_0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone --recursive https://github.com/WebAssembly/wabt/ cd wabt git checkout binary_0xc make gcc-release ``` * Option #2: build spec interpreter @@ -41,30 +41,30 @@ should get you going quickly. * Download and build a branch of the spec interpreter (binary-0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone https://github.com/WebAssembly/spec cd spec/interpreter git checkout binary-0xc make opt ``` ### Compile and Run wast code Compile a wast to wasm using wabt: * Use wast2wasm from wabt to build WebAssembly binary object (wasm): ```bash wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm ``` Compile a wast to wasm using the spec interpreter: * Use wast2wasm from wabt to build WebAssembly binary object (wasm): ```bash spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasm ``` Run an exported function in the wasm binary module using the runasm.js script: -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -69,6 +69,6 @@ Compile a wast to wasm using the spec interpreter: Run an exported function in the wasm binary module using the runasm.js script: ```bash ./runwasm.js addTwo.wasm addTwo 2 3 ``` -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ want to do is play around directly with the wast textual format, this should get you going quickly. ### Prerequisites * Install node 7.x ([instructions](https://nodejs.org/en/download/current/)). I've @@ -48,7 +48,7 @@ should get you going quickly. make opt ``` ### Compile and Run wast code Compile a wast to wasm using wabt: -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 17 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ want to do is play around directly with the wast textual format, this should get you going quickly. = Prerequisites = * Install node 7.x ([instructions](https://nodejs.org/en/download/current/)). I've @@ -19,12 +19,12 @@ Prerequisites: straightfoward options: wabt (wabbit) and the interpreter included in the official WebAssembly spec. * Option #1: build wabt * You'll need a standard gcc toolchain to build wabt * Download and build a branch of wabt (binary\_0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone --recursive https://github.com/WebAssembly/wabt/ @@ -33,21 +33,22 @@ Option #1: build wabt make gcc-release ``` * Option #2: build spec interpreter * You'll need an Ocaml compiler version 4.02 or later (ocaml-nox on Ubuntu) * Download and build a branch of the spec interpreter (binary-0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone https://github.com/WebAssembly/spec cd spec/interpreter git checkout binary-0xc make opt ``` = Compile and Run wast code = Compile a wast to wasm using wabt: -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 47 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,30 @@ Run wast (WebAssembly) in node ------------------------------ This is a simple example of compiling and running the WebAssembly textual format (wast) in Node.js. This does not cover compiling from a higher level language to WebAssembly (for that you probably want [Emscripten](https://github.com/kripken/emscripten/)). But if all you want to do is play around directly with the wast textual format, this should get you going quickly. Prerequisites: * Install node 7.x ([instructions](https://nodejs.org/en/download/current/)). I've tested this with node 7.7.1 * You need a wast to wasm compiler. There are two fairly straightfoward options: wabt (wabbit) and the interpreter included in the official WebAssembly spec. Option #1: build wabt * You'll need a standard gcc toolchain to build wabt * Download and build a branch of wabt (binary\_0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone --recursive https://github.com/WebAssembly/wabt/ @@ -17,15 +33,40 @@ Prerequisites: make gcc-release ``` Option #2: build spec interpreter * You'll need an Ocaml compiler version 4.02 or later (ocaml-nox on Ubuntu) * Download and build a branch of the spec interpreter (binary-0xc) supporting the same version of WebAssembly as node 7 (version 12): ```bash git clone https://github.com/WebAssembly/spec cd spec/interpreter git checkout binary-0xc make opt ``` Compile a wast to wasm using wabt: * Use wast2wasm from wabt to build WebAssembly binary object (wasm): ```bash wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm ``` Compile a wast to wasm using the spec interpreter: * Use wast2wasm from wabt to build WebAssembly binary object (wasm): ```bash spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasm ``` Run an exported function in the wasm binary module using the runasm.js script: ```bash ./runwasm.js addTwo.wasm addTwo 2 3 -
kanaka revised this gist
Mar 6, 2017 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,17 +10,23 @@ Prerequisites: * Download and build branch of wabt (binary\_0xc) supporting same version of WebAssembly as node 7 (version 12): ```bash git clone --recursive https://github.com/WebAssembly/wabt/ cd wabt git checkout binary_0xc make gcc-release ``` Compile and run: * Use wast2wasm from wabt to build WebAssembly binary object (wasm): ```bash wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm ``` * Use the runasm.js script to call the exported "addTwo" function: ```bash ./runwasm.js addTwo.wasm addTwo 2 3 ``` -
kanaka created this gist
Mar 6, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ (module (func $addTwo (param i32 i32) (result i32) (i32.add (get_local 0) (get_local 1))) (export "addTwo" (func $addTwo))) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ Simple wabt + node WebAssembly Example -------------------------------------- Prerequisites: * Install node 7.x ([instructions](https://nodejs.org/en/download/current/)). I've tested this with node 7.7.1 * Download and build branch of wabt (binary\_0xc) supporting same version of WebAssembly as node 7 (version 12): git clone --recursive https://github.com/WebAssembly/wabt/ cd wabt git checkout binary_0xc make gcc-release Compile and run: * Use wast2wasm from wabt to build WebAssembly binary object (wasm): wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm * Use the runasm.js script to call the exported "addTwo" function: ./runwasm.js addTwo.wasm addTwo 2 3 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ #!/usr/bin/node --expose-wasm const fs = require('fs'), assert = require('assert') assert('WebAssembly' in global, 'WebAssembly global object not detected') // Convert node Buffer to Uint8Array function toUint8Array(buf) { var u = new Uint8Array(buf.length) for (var i = 0; i < buf.length; ++i) { u[i] = buf[i] } return u } // Based on https://gist.github.com/kripken/59c67556dc03bb6d57052fedef1e61ab // Loads a WebAssembly dynamic library, returns a promise. // imports is an optional imports object function loadWebAssembly(filename, imports) { // Fetch the file and compile it const buffer = toUint8Array(fs.readFileSync(filename)) return WebAssembly.compile(buffer) .then(module => { // Create the imports for the module, including the // standard dynamic library imports imports = imports || {} imports.env = imports.env || {} imports.env.memoryBase = imports.env.memoryBase || 0 imports.env.tableBase = imports.env.tableBase || 0 if (!imports.env.memory) { imports.env.memory = new WebAssembly.Memory({ initial: 256 }) } if (!imports.env.table) { imports.env.table = new WebAssembly.Table({ initial: 0, element: 'anyfunc' }) } // Create the instance. return new WebAssembly.Instance(module, imports) }) } if (module.parent) { module.exports.loadWebAssembly = loadWebAssembly } else { assert(process.argv.length >= 4, 'Usage: ./runwasm.js prog.wasm func INT_ARG...') const wasm = process.argv[2], func = process.argv[3], // Convert args to either floats or ints args = process.argv.slice(4).map( x => x.match(/[.]/) ? parseFloat(x) : parseInt(x)) loadWebAssembly(wasm) .then(instance => { var exports = instance.exports assert(exports, 'no exports found') assert(func in exports, func + ' not found in wasm module exports') //console.log('calling exports.'+func+'('+args+')') console.log(exports[func](...args)) }) .catch(res => { console.log(res) }) }