Add wasi:cli component support, linear memory and globals

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-21 01:03:32 -08:00
1 parent cb1cec8b13
commit 05488c9783
11 files changed
+518 -52

No files matched your search

+28 -13
View File
@@ -1,21 +1,36 @@
# wasm2elf
A WebAssembly → static x86-64 Linux ELF compiler, written in MoonBit. No C, no LLVM, no
runtime: the output is a single `PT_LOAD` segment with hand-encoded machine code.
runtime: the output is hand-encoded machine code in one or two `PT_LOAD` segments.
```
wasm-tools parse examples/shine_rise.wat -o shine_rise.wasm
moon build --target native
_build/native/debug/build/cmd/main/main.exe shine_rise.wasm shine_rise [entry-export]
./shine_rise # wasm trap: unreachable (exit 134)
just run sum # core module -> ELF, runs it, prints the exit code
just run-component hello # wasi:cli component -> ELF, prints "Hello from wasi:cli!"
just examples # everything in examples/
```
The entry export (default `_start`, then `main`, then the first function export) is called
from `_start`; its i32 result becomes the exit code. Traps print a message to stderr and
exit 134.
By hand: `wasm2elf <input.wasm> <output> [entry-export]`. The input can be a core module or
a component; for a component, the core module exporting `wasi:cli/run@…#run` is compiled.
Supported: i32 only; locals, params, calls, block/loop/if/else/br/br_if/return,
select/drop, all i32 arithmetic/comparison/shift/rotate ops, divide-by-zero and
INT_MIN/-1 traps. Not supported: i64/floats, memory, globals, tables, imports, br_table.
Input is assumed valid (`wasm-tools validate` it first). `./run_examples.sh` builds and
runs everything in `examples/`.
The entry export (default `_start`, then `main`, then `wasi:cli/run@…`, then the first
function export) is called from `_start`; its i32 result becomes the exit code. Traps
(`unreachable`, divide by zero, overflow, out-of-bounds memory) print a message to stderr
and exit 134.
## wasi:cli
The core module of a component imports lowered `wasi:cli` / `wasi:io` functions by name.
They are implemented as small native stubs (no host runtime): `wasi:cli/stdout.get-stdout`,
`stderr.get-stderr`, `stdin.get-stdin` (handles are fds 1/2/0),
`wasi:io/streams [method]output-stream.blocking-write-and-flush` (a `write(2)` syscall),
`wasi:cli/exit.exit`, and `[resource-drop]*` (no-op). Anything else is a compile error.
`examples/wit` holds minimal WIT for these interfaces so `wasm-tools component embed/new`
can build the example.
## Supported subset
i32 only; locals, params, calls, block/loop/if/else/br/br_if/return, select/drop, i32
arithmetic/comparison/shift/rotate, one fixed-size linear memory (i32 loads/stores with
bounds checks, `memory.size`; `memory.grow` always returns -1), i32 globals, active data
segments. Not supported: i64/floats, tables/`call_indirect`, `br_table`, start function,
non-function imports. Input is assumed valid (`wasm-tools validate` it first).