{ } qjs-modules

wasi

Source: lib/wasi.js (JS over fs and process) — exports: WASI, errno; qjsm, node, bun, deno

Node's node:wasi class (WASI preview1), in JS, for the wasm module or any WebAssembly global. No wasm engine's own WASI is used, so behavior is the same on wasm3 and WAMR. It touches only fs, process, Atomics and crypto (falling back to /dev/urandom), so the same file runs on node, bun and deno.

import { WASI } from 'wasi';
import * as wasm from 'wasm';

const wasi = new WASI({ version: 'preview1', args: ['prog', 'x'], env: { A: '1' }, preopens: { '/sandbox': '.' } });
const instance = new wasm.Instance(new wasm.Module(bytes), wasi.getImportObject());
const code = wasi.start(instance); // runs _start; returns the exit code

WASI

MemberMeaning
new WASI(options)version ('preview1' default, 'unstable'), args ([]), env ({}, not inherited), preopens ({ guestPath: hostPath }), returnOnExit (true), stdin/stdout/stderr (host fds, default 0/1/2)
getImportObject(){ wasi_snapshot_preview1: wasiImport } (wasi_unstable for 'unstable')
wasiImportthe preview1 functions; i64 parameters arrive as BigInt
start(instance)runs _start; returns the proc_exit code or 0; ERR_WASI_ALREADY_STARTED on a second call
initialize(instance)runs _initialize of a reactor module
finalizeBindings(instance, { memory }?)binds the guest memory without running anything

With returnOnExit: false, start() exits the process with the guest's code.

Behavior

AreaDetail
Pathsresolved inside the preopened directory; .. above it, an absolute path, or a symlink leading out gives NOTCAPABLE (76)
Filespath_open, fd_read/write/pread/pwrite/seek/tell/sync/allocate, fd_filestat_*, fd_readdir, rename, link, symlink, unlink, mkdir, rmdir
Clocksclock_time_get: realtime from Date.now() (millisecond resolution), others from hrtime()
random_getreads /dev/urandom
poll_oneoffclock subscriptions sleep; fd subscriptions report ready at once
Not implementedsock_* and proc_raise return NOSYS; fd_fdstat_set_rights returns NOSYS; rights are reported as all set and not enforced
ErrorsLinux errno from os/misc is mapped to the WASI errno

errno exports the WASI error numbers used (errno.NOENT === 44).

Export list

Generated by utilities/doc-api.js from the wasi dump of utilities/api-surface.sh: 55 members. Edit the text above this block, not the block.

WASI

class, constructible

  • methods: _bind(), _entryStat(), _exit(), _fd(), _makeImports(), _path(), _putStrings(), _rw(), _setTimes(), _stat(), _str(), _stringsSize(), _view(), _within(), _writeFilestat(), finalizeBindings(), getImportObject(), initialize(), start()

default

object

  • static methods: WASI()

errno

object

  • static values: ACCES = 2, AGAIN = 6, BADF = 8, BUSY = 10, E2BIG = 1, EXIST = 20, FAULT = 21, INTR = 27, INVAL = 28, IO = 29, ISDIR = 31, LOOP = 32, MFILE = 33, NAMETOOLONG = 37, NOENT = 44, NOMEM = 48, NOSPC = 51, NOSYS = 52, NOTCAPABLE = 76, NOTDIR = 54, NOTEMPTY = 55, NOTSUP = 58, NXIO = 60, OVERFLOW = 61, PERM = 63, PIPE = 64, RANGE = 68, ROFS = 69, SPIPE = 70, SUCCESS = 0, XDEV = 75