WASM API Reference

WebAssembly runtime utilities and polyfills

Implementation: WASM-only module. Requires --target wasm32-unknown-unknown compilation. Provides polyfill functions for WASM environments.

Import

U std/wasm

Overview

The wasm module provides WebAssembly-specific memory management, I/O, and WASI bindings. It is designed for programs compiled to the wasm32-unknown-unknown target and includes polyfills for standard library functions that are unavailable in the WASM environment.

Constants

ConstantValueDescription
WASM_TARGET1WASM environment flag
WASM_PAGE_SIZE65536WASM memory page size (64KB)
WASM_STDIN0Standard input fd (WASI)
WASM_STDOUT1Standard output fd (WASI)
WASM_STDERR2Standard error fd (WASI)
WASI_NS0wasi_snapshot_preview1 namespace
ENV_NS1env namespace (custom host functions)

Memory Management

wasm_memory_size

F wasm_memory_size() -> i64

Get current memory size in pages (each page = 64KB).

wasm_memory_grow

F wasm_memory_grow(pages: i64) -> i64

Grow memory by N pages. Returns previous size or -1 on failure.

I/O Functions

The module provides WASI-compatible I/O functions for writing to stdout/stderr within a WebAssembly environment.

Example

U std/wasm

F main() {
    # Check memory size
    pages := wasm_memory_size()
    total_bytes := pages * WASM_PAGE_SIZE

    # Grow memory
    old_pages := wasm_memory_grow(1)
}