Last active
May 2, 2023 19:16
-
-
Save joshbetz/ae9539d1fb593b9922669065e05fa144 to your computer and use it in GitHub Desktop.
Revisions
-
joshbetz revised this gist
May 2, 2023 . 1 changed file with 3 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 @@ -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 More info: https://nodejs.org/api/wasi.html -
joshbetz revised this gist
May 2, 2023 . 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 @@ -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( -
joshbetz created this gist
May 2, 2023 .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,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 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,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)); 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,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" } 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,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);