From 29c4b077167501e2f9f67c49d66b1ac6ff7b093e Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 28 Aug 2021 10:16:49 -0700 Subject: [PATCH] doc: update WASI example to use import.meta.url MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/39925 Reviewed-By: Tobias Nießen Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- doc/api/wasi.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/doc/api/wasi.md b/doc/api/wasi.md index 58c310edd82585..e0cd77a5a42c78 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);