Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: update WASI example to use import.meta.url #39925

Closed
wants to merge 6 commits into from
Closed
Changes from all 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 { join } = 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(join(__dirname, 'demo.wasm'))
);
const instance = await WebAssembly.instantiate(wasm, importObject);

wasi.start(instance);
Expand Down