Skip to content

Instantly share code, notes, and snippets.

@joshbetz
Last active May 2, 2023 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbetz/ae9539d1fb593b9922669065e05fa144 to your computer and use it in GitHub Desktop.
Save joshbetz/ae9539d1fb593b9922669065e05fa144 to your computer and use it in GitHub Desktop.
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));
{
"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"
}
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,
/*
We don't ned fileystem access, but if we did this is how it would work:
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment