WebAssembly System Interface (WASI)
自 v12.16.0 版本开始新增
源代码: lib/wasi.js
The WASI API provides an implementation of the WebAssembly System Interface specification. WASI gives sandboxed WebAssembly applications access to the underlying operating system via a collection of POSIX-like functions.
MJS
CJS
To run the above example, create a new WebAssembly text format file named
demo.wat
:
TEXT
Use wabt to compile .wat
to .wasm
BASH
The --experimental-wasi-unstable-preview1
CLI argument is needed for this
example to run.
C WASI
自 v13.3.0, v12.16.0 版本开始新增
The WASI
class provides the WASI system call API and additional convenience
methods for working with WASI-based applications. Each WASI
instance
represents a distinct sandbox environment. For security purposes, each WASI
instance must have its command-line arguments, environment variables, and
sandbox directory structure configured explicitly.
M new WASI([options])
自 v13.3.0, v12.16.0 版本开始新增
options
Object
args
Array
An array of strings that the WebAssembly application will see as command-line arguments. The first argument is the virtual path to the WASI command itself. Default:[]
.env
Object
An object similar toprocess.env
that the WebAssembly application will see as its environment. Default:{}
.preopens
Object
This object represents the WebAssembly application's sandbox directory structure. The string keys ofpreopens
are treated as directories within the sandbox. The corresponding values inpreopens
are the real paths to those directories on the host machine.returnOnExit
boolean
By default, WASI applications terminate the Node.js process via the__wasi_proc_exit()
function. Setting this option totrue
causeswasi.start()
to return the exit code rather than terminate the process. Default:false
.stdin
integer
The file descriptor used as standard input in the WebAssembly application. Default:0
.stdout
integer
The file descriptor used as standard output in the WebAssembly application. Default:1
.stderr
integer
The file descriptor used as standard error in the WebAssembly application. Default:2
.
M wasi.start(instance)
自 v13.3.0, v12.16.0 版本开始新增
instance
WebAssembly.Instance
Attempt to begin execution of instance
as a WASI command by invoking its
_start()
export. If instance
does not contain a _start()
export, or if
instance
contains an _initialize()
export, then an exception is thrown.
start()
requires that instance
exports a WebAssembly.Memory
named
memory
. If instance
does not have a memory
export an exception is thrown.
If start()
is called more than once, an exception is thrown.