zena:wasm
import {…} from 'zena:wasm';
Running other Wasm programs, for programs the zena command's host
runs: zena-cli:host/wasm as the compiler synthesizes it from the
host's WIT. The test runner runs each compiled test this way. The
host grants it together with process spawning; anywhere else the
imports are stubs that trap.
A program is a component or a core module, and the host tells them
apart from the file itself. A core module — a freestanding build,
hand-written .wat, another language's output — runs with no
imports, so arguments, environment, directories and the grant do not
reach it, and it has no output to capture. The rest of this module's
documentation says "component" for the program being run.
Examples ​
import { runModule } from 'zena:wasm';
let result = runModule('out/hello.wasm', {args: ['hello', 'world']});
console.log(`${result.outcome} ${result.exitCode}`);
Classes
RunResult
class RunResult
What a run produced.
new()
outcome: RunOutcome
exitCode: i32
The status the component ended with: the i32 its exported
function returned, the code it passed to exit, or 0 when the
function returns nothing. -1 when it trapped, timed out or failed
to start.
resultText: String
What the exported function returned, formatted the way zena run
prints it (7, 2.5); '' when it returned nothing or did not
return.
message: String
Why a run trapped, timed out or failed to start; otherwise ''.
stdout: String
What the component wrote, when its output was captured; otherwise ''.
stderr: String
callTime: Duration
How long the exported call took. Loading and instantiating the component are not included, so this is the number a benchmark wants.
succeeded(): boolean
True when the component returned or exited with status 0.
Run
resource class Run extends Resource
A run in progress: a resource over the host's handle, held as
Own<Run> and released when the owner leaves scope.
Enums
RunOutcome
enum RunOutcome
How a run ended.
Returned
The exported function returned. exitCode is the i32 it returned,
and resultText its first result however typed.
Exited
The component ended itself with exit(code); exitCode is the code.
Trapped
The component trapped. message has the trap and its backtrace.
TimedOut
The run was stopped at its time limit.
Failed
The component did not start: it could not be read, compiled, linked
or instantiated, or a path it was given is outside what this
program can reach. message says which.
Type aliases
DirMapping
type DirMapping = {from: String, to: String}
A directory of this program's, from, preopened for the run as to.
RunOptions
type RunOptions = {args?: Array<String>, env?: Map<String, String>, inheritEnv?: boolean, dirs?: Array<DirMapping>, invoke?: String, inheritStdio?: boolean, grant?: boolean, grantExtensions?: boolean, debug?: boolean, timeout?: Duration}
How to run a component. Every field is optional.
Functions
startModule
function startModule(path: String, options: RunOptions = {}): Own<Run>
Start a component and return at once.
runModule
function runModule(path: String, options: RunOptions = {}): RunResult
Run a component to its end.
precompileModule
function precompileModule(path: String, debug: boolean = false): String
Write a component's ahead-of-time compiled form beside it, so its first run skips compilation. Returns '' or why it could not.