Skip to content
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions doc/api/wasi.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ specification. WASI gives sandboxed WebAssembly applications access to the
underlying operating system via a collection of POSIX-like functions.

```mjs
import fs from 'fs';
import { readFile } from 'fs/promises';
import { WASI } from 'wasi';
import { argv, env } from 'process';

Expand All @@ -22,19 +22,25 @@ const wasi = new WASI({
'/sandbox': '/some/real/path/that/wasm/can/access'
}
});

// Some WASI binaries require:
// const importObject = { wasi_unstable: wasi.wasiImport };
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };

const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm'));
const wasm = await WebAssembly.compile(
await readFile(new URL('./demo.wasm', import.meta.url))
);
const instance = await WebAssembly.instantiate(wasm, importObject);

wasi.start(instance);
```

```cjs
'use strict';
const fs = require('fs');
const { readFile } = require('fs/promises');
const { WASI } = require('wasi');
const { argv, env } = require('process');
const { resolve } = require('path');

const wasi = new WASI({
args: argv,
Expand All @@ -43,10 +49,15 @@ const wasi = new WASI({
'/sandbox': '/some/real/path/that/wasm/can/access'
}
});

// Some WASI binaries require:
// const importObject = { wasi_unstable: wasi.wasiImport };
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };

(async () => {
const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm'));
const wasm = await WebAssembly.compile(
await readFile(resolve(__dirname, './demo.wasm'))
);
const instance = await WebAssembly.instantiate(wasm, importObject);

wasi.start(instance);
Expand Down