Skip to content

Instantly share code, notes, and snippets.

@joshbetz
Last active May 2, 2023 19:16
Show Gist options
  • Select an option

  • Save joshbetz/ae9539d1fb593b9922669065e05fa144 to your computer and use it in GitHub Desktop.

Select an option

Save joshbetz/ae9539d1fb593b9922669065e05fa144 to your computer and use it in GitHub Desktop.

Revisions

  1. joshbetz revised this gist May 2, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,6 @@
    2. ./javy compile fib.js -o fib.wasm
    3. node server.js

    Note: In order to support node modules, you need to use a bundler to include every module in 1 file and then compile to wasm. ref: https://github.com/Shopify/javy/issues/265
    Note: In order to support node modules, you need to use a bundler to include every module in 1 file and then compile to wasm. ref: https://github.com/Shopify/javy/issues/265

    More info: https://nodejs.org/api/wasi.html
  2. joshbetz revised this gist May 2, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -6,9 +6,12 @@ const wasi = new WASI({
    version: 'preview1',
    args: argv,
    env,
    /*
    We don't ned fileystem access, but if we did this is how it would work:
    preopens: {
    '/sandbox': '/tmp',
    },
    */
    });

    const wasm = await WebAssembly.compile(
  3. joshbetz created this gist May 2, 2023.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    1. Download javy from https://github.com/Shopify/javy/releases/latest
    2. ./javy compile fib.js -o fib.wasm
    3. node server.js

    Note: In order to support node modules, you need to use a bundler to include every module in 1 file and then compile to wasm. ref: https://github.com/Shopify/javy/issues/265
    14 changes: 14 additions & 0 deletions fib.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    function fib(n) {
    var a = 0, b = 1
    if (n > 0) {
    while (--n) {
    let t = a + b
    a = b
    b = t
    }
    return b
    }
    return a
    }

    console.log(fib(20));
    13 changes: 13 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    {
    "type": "module",
    "name": "wasm",
    "version": "1.0.0",
    "description": "",
    "main": "server.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
    },
    "author": "",
    "license": "ISC"
    }
    20 changes: 20 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import { readFile } from 'node:fs/promises';
    import { WASI } from 'wasi';
    import { argv, env } from 'node:process';

    const wasi = new WASI({
    version: 'preview1',
    args: argv,
    env,
    preopens: {
    '/sandbox': '/tmp',
    },
    });

    const wasm = await WebAssembly.compile(
    await readFile(new URL('./fib.wasm', import.meta.url)),
    );

    const instance = await WebAssembly.instantiate(wasm, wasi.getImportObject());

    wasi.start(instance);