diff options
author | Guy Bedford <guybedford@gmail.com> | 2021-08-28 20:16:49 +0300 |
---|---|---|
committer | Guy Bedford <guybedford@gmail.com> | 2021-08-31 18:07:35 +0300 |
commit | 162f07b7af24692de32d47b26e7cc5e52fc4e0c2 (patch) | |
tree | e9aa7a33f9572954a77d6d02811ff3ec15898699 | |
parent | f172c5ad5b5ef126134f45c2626151a675dc4bf3 (diff) |
doc: update WASI example to use import.meta.url
PR-URL: https://github.com/nodejs/node/pull/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>
-rw-r--r-- | doc/api/wasi.md | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/doc/api/wasi.md b/doc/api/wasi.md index 58c310edd82..e0cd77a5a42 100644 --- a/doc/api/wasi.md +++ b/doc/api/wasi.md @@ -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'; @@ -22,9 +22,14 @@ 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); @@ -32,9 +37,10 @@ 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, @@ -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); |