Skip to content

Commit

Permalink
doc: update WASI example to use import.meta.url
Browse files Browse the repository at this point in the history
PR-URL: #39925
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
guybedford authored and targos committed Sep 6, 2021
1 parent e9e181f commit f662599
Showing 1 changed file with 15 additions and 4 deletions.
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

0 comments on commit f662599

Please sign in to comment.