WASI Preview 3¶
wasi-preview3 is the Kotlin-first facade for stable WASI 0.3 support. It
depends on component-model, keeps canonical handle types at the Wasm boundary,
and adds coroutine-friendly Kotlin APIs.
Main surfaces:
KotlinWasiPreview3builder and facade,WasiPreview3.await(future)for typedWitFuture<T>values,WitFuture<T>.asDeferred(...),Deferred<T>.toWitFuture(...),- readable and writable readiness helpers for
WitStream<T>, - bounded stream buffers and waitable limits,
- Kotlin clock and random configuration,
- capability-based
WasiFileSystemover preopened directories.
Example:
KotlinWasiPreview3.builder()
.withNetworkPolicy(
WasiNetworkPolicy(
httpEndpoints = setOf(
WasiHttpNetworkEndpoint(
WasiHttpNetworkProtocol.Https,
"api.example.com",
443,
),
),
),
)
.withResourceBudget(parallelism = 2, streamBufferCapacity = 64 * 1024)
.build()
.use { runtime ->
val future = runtime.completed("ready")
val value = runtime.await(future)
}
WasiPreview3 is AutoCloseable. Closing it cancels its child coroutine job,
drains owned WIT and socket resources, and closes internally created transports.
A CoroutineScope passed with withCoroutineScope remains caller-owned: the
host derives a child job from it and never cancels the supplied scope itself.
Networking is deny-by-default. HTTP grants match scheme, canonical host, and
port exactly. Raw TCP/UDP socket grants are independent and must be listed as
WasiNetworkEndpoint values.
KtorWasiHttpClient returns redirect responses without following them. A
custom WasiHttpClient is trusted host code and must likewise reject redirects
or re-authorize every redirect target before opening the next connection.
When running precompiled Preview 3 components through the Wasmtime
bridge, WasmtimePreview3ComponentConfig carries the same resource limits as
the raw Wasmtime backend: maxMemoryBytes, maxWasmStackBytes,
maxTableElements, maxInstances, maxTables, maxMemories, and maxFuel.
Count limits and maxFuel accept WasmtimeUnlimitedResourceLimit (-1) for
unlimited. maxFuel is Wasmtime guest execution fuel: it is consumed while guest
Wasm instructions run and traps the bridge call when exhausted. The Preview 3
bridge creates a fresh store for each component call or command run, so this is a
per-call budget. The precompiled component must be built with fuel enabled, for
example wasmtime compile -W fuel=1 ..., when maxFuel is used.
The separate executionTimeoutMillis value is a wall-clock bridge timeout. It is
useful as an outer policy but should not be treated as deterministic fuel or CPU
metering, and it does not account for host work running outside guest Wasm
instructions.
JVM and iOS provide Ktor-backed default socket runtimes. wasmJs provides Ktor-backed HTTP and suspend TCP connect/listen paths for Node-backed environments. Browser raw sockets, wasmJs UDP, and the default browser filesystem remain platform-unavailable and must be supplied by the application when needed.