Skip to content

Instantly share code, notes, and snippets.

@hoangpq
Forked from kanaka/addTwo.wast
Created November 26, 2018 08:44
Show Gist options
  • Select an option

  • Save hoangpq/b754cbb18fd024ea9bc3cdf17ba9ef48 to your computer and use it in GitHub Desktop.

Select an option

Save hoangpq/b754cbb18fd024ea9bc3cdf17ba9ef48 to your computer and use it in GitHub Desktop.

Revisions

  1. @kanaka kanaka revised this gist Jun 8, 2017. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions readme.markdown
    Original file line number Diff line number Diff line change
    @@ -19,14 +19,9 @@ should get you going quickly.

    ### Prerequisites

    * Install node 8 nightly (has WebAssembly MVP support) and add it to your path:
    * Install [node 8](https://nodejs.org/en/download/current/)

    ```
    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
    * 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:
  2. @kanaka kanaka revised this gist Apr 28, 2017. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions readme.markdown
    Original 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
    ```
  3. @kanaka kanaka revised this gist Apr 28, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.markdown
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ should get you going quickly.

    ### Prerequisites

    * Install node 8 nightly and add it to your path:
    * 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
  4. @kanaka kanaka revised this gist Apr 28, 2017. 2 changed files with 23 additions and 52 deletions.
    73 changes: 22 additions & 51 deletions readme.markdown
    Original 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. This does not cover compiling from
    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 7.x
    ([instructions](https://nodejs.org/en/download/current/)). I've
    tested this with node 7.7.1
    * Install node 8 nightly and add it to your path:

    * 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/
    cd wabt
    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)
    ```
    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
    ```

    * Download and build a branch of the spec interpreter (binary-0xc)
    supporting the same version of WebAssembly as node 7 (version 12):
    * 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 https://github.com/WebAssembly/spec
    cd spec/interpreter
    git checkout binary-0xc
    make opt
    ```
    ```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):

    * **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
    ```
    ```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
    ./runwasm.js addTwo.wasm addTwo 2 3
    node ./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):
    The `runwasm.js` script is also written to be used as a node module:

    ```js
    node --expose-wasm
    node
    > w = require('./runwasm.js')
    > w.loadWebAssembly('addTwo.wasm').then(i => console.log(i.exports.addTwo(7,8)))
    15
    2 changes: 1 addition & 1 deletion runwasm.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/usr/bin/node --expose-wasm
    #!/usr/bin/env node

    // Copyright Joel Martin
    // License MIT
  5. @kanaka kanaka revised this gist Mar 6, 2017. No changes.
  6. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions runwasm.js
    Original 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
    // 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)
    })
    }
    }
  7. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions readme.markdown
    Original 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
    `runasm.js` script (this should print 5 to the console):
    `runwasm.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
    (note that `--expose-wasm` is require to enable WebAssembly support in
    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('./runasm.js')
    > w = require('./runwasm.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.
    You are free to use `runwasm.js` code under an MIT license.
  8. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.markdown
    Original 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
    https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format
    * Detailed Intro to WebAssembly: https://rsms.me/wasm-intro

    ### Prerequisites
  9. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions readme.markdown
    Original 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
  10. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion readme.markdown
    Original 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:
    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
  11. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 25 additions and 14 deletions.
    39 changes: 25 additions & 14 deletions readme.markdown
    Original 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
    ```
    ```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
    ```
    ```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
    ```
    ```bash
    spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasm
    ```
    Run an exported function in the wasm binary module using the
    `runasm.js` script:
    `runasm.js` script (this should print 5 to the console):
    ```bash
    ./runwasm.js addTwo.wasm addTwo 2 3
    ./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.
  12. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions readme.markdown
    Original 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
    ```
    ```bash
    git clone --recursive https://github.com/WebAssembly/wabt/
    cd wabt
    git checkout binary_0xc
    make gcc-release
    ```

    * **Option #2: spec interpreter**

  13. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion runwasm.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/usr/bin/node --expose-wasm

    // Copyright Joel Martin
    // License MPL-2.0
    // License MIT

    const fs = require('fs'),
    assert = require('assert')
  14. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions runwasm.js
    Original 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')

  15. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 21 additions and 20 deletions.
    41 changes: 21 additions & 20 deletions readme.markdown
    Original 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
    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
    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 (wabbit) and the interpreter included
    in the official WebAssembly spec.
    * 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: build wabt
    * **Option #1: wabt**

    * You'll need a standard gcc toolchain to 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):
    * 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: build spec interpreter
    * **Option #2: spec interpreter**

    * You'll need an Ocaml compiler version 4.02 or later (ocaml-nox on
    Ubuntu)
    * 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):
    * 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 and Run Wast Code

    Compile a wast to wasm (binary module):
    Compile a *wast* to *wasm* (binary module):

    * Using wast2wasm from wabt:
    * **Option #1: using wast2wasm from wabt**:

    ```bash
    wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm
    ```

    * Using spec interpreter:
    * **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:
    Run an exported function in the wasm binary module using the
    `runasm.js` script:

    ```bash
    ./runwasm.js addTwo.wasm addTwo 2 3
  16. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions readme.markdown
    Original 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
    make opt
    ```

    ### Compile and Run wast code

    Compile a wast to wasm using wabt:
    Compile a wast to wasm (binary module):

    * Use wast2wasm from wabt to build WebAssembly binary object (wasm):
    * Using wast2wasm from wabt:

    ```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):
    * 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
    ./runwasm.js addTwo.wasm addTwo 2 3
    ```
  17. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions readme.markdown
    Original 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
    ```
    ```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
    ```
    ```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
    ```
    ```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
    ```
    ```bash
    spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasm
    ```

    Run an exported function in the wasm binary module using the runasm.js
    script:
  18. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions readme.markdown
    Original 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
    ```
    ```bash
    ./runwasm.js addTwo.wasm addTwo 2 3
    ```
  19. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions readme.markdown
    Original 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 =
    ### 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 and Run wast code
    Compile a wast to wasm using wabt:
  20. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 17 additions and 16 deletions.
    33 changes: 17 additions & 16 deletions readme.markdown
    Original 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:
    = 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
    * Option #1: build wabt

    * You'll need a standard gcc toolchain to 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):
    * 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
    * Option #2: build spec interpreter

    * You'll need an Ocaml compiler version 4.02 or later (ocaml-nox on
    Ubuntu)
    * 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):
    * 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
    ```
    ```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:
  21. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 47 additions and 6 deletions.
    53 changes: 47 additions & 6 deletions readme.markdown
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,30 @@
    Simple wabt + node WebAssembly Example
    --------------------------------------
    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

    * Download and build branch of wabt (binary\_0xc) supporting same
    version of WebAssembly as node 7 (version 12):
    * 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
    ```

    Compile and run:
    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
    ```
    * Use the runasm.js script to call the exported "addTwo" function:
    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
  22. @kanaka kanaka revised this gist Mar 6, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions readme.markdown
    Original 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
    ```
  23. @kanaka kanaka created this gist Mar 6, 2017.
    6 changes: 6 additions & 0 deletions addTwo.wast
    Original 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)))
    26 changes: 26 additions & 0 deletions readme.markdown
    Original 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
    67 changes: 67 additions & 0 deletions runwasm.js
    Original 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)
    })
    }