Skip to content

Commit 3a3afc0

Browse files
Copilotenghitalo
andcommitted
Add documentation for new wasm features
Co-authored-by: enghitalo <63821277+enghitalo@users.noreply.github.com>
1 parent cbe4a8a commit 3a3afc0

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

vlib/wasm/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,94 @@ fn main() {
3737

3838
This module does not perform verification of the WebAssembly output.
3939
Use a tool like `wasm-validate` to validate, and `wasm-dis` to show a decompiled form.
40+
41+
## New Features (Phase 1 - 2026)
42+
43+
### Tables and Indirect Calls
44+
45+
Tables allow dynamic function dispatch through indirect calls:
46+
47+
```v
48+
mut m := wasm.Module{}
49+
50+
// Create a function to call indirectly
51+
mut target := m.new_function('target', [], [.i32_t])
52+
{
53+
target.i32_const(42)
54+
}
55+
m.commit(target, false)
56+
57+
// Create a table
58+
table_idx := m.new_table('func_table', false, 1, 10)
59+
60+
// Initialize table with the target function
61+
mut offset := wasm.ConstExpression{}
62+
offset.i32_const(0)
63+
m.new_element_segment(none, u32(table_idx), offset, ['target'])
64+
65+
// Call indirectly through the table
66+
mut caller := m.new_function('caller', [], [.i32_t])
67+
{
68+
caller.i32_const(0) // table index
69+
caller.call_indirect(0, u32(table_idx))
70+
}
71+
m.commit(caller, true)
72+
```
73+
74+
### WASI Support
75+
76+
Easily import WASI functions for system interactions:
77+
78+
```v
79+
mut m := wasm.Module{}
80+
81+
// Add WASI imports
82+
m.add_wasi_import('fd_write')
83+
m.add_wasi_import('proc_exit')
84+
85+
mut func := m.new_function('hello', [], [])
86+
{
87+
// Use fd_write to print to stdout
88+
func.i32_const(1) // fd (stdout)
89+
func.i32_const(0) // iovs pointer
90+
func.i32_const(1) // iovs_len
91+
func.i32_const(0) // nwritten pointer
92+
func.call_import('wasi_snapshot_preview1', 'fd_write')
93+
}
94+
m.commit(func, true)
95+
```
96+
97+
Supported WASI functions: `fd_write`, `proc_exit`, `args_get`, `args_sizes_get`, `environ_get`, `environ_sizes_get`, `clock_time_get`, `random_get`.
98+
99+
### Enhanced Control Flow
100+
101+
New branch instructions for more flexible control flow:
102+
103+
```v
104+
mut func := m.new_function('branch_demo', [.i32_t], [.i32_t])
105+
{
106+
// Direct branch
107+
func.br(0)
108+
109+
// Conditional branch
110+
func.local_get(0)
111+
func.br_if(0)
112+
113+
// Branch table (switch-case)
114+
func.local_get(0)
115+
func.br_table([u32(0), u32(1)], u32(2))
116+
}
117+
```
118+
119+
### i32 Comparison Shortcuts
120+
121+
Convenient shortcuts for common i32 comparisons:
122+
123+
```v
124+
func.local_get(0)
125+
func.local_get(1)
126+
func.i32_eq() // i32.eq
127+
func.i32_lt_s() // i32.lt_s (signed)
128+
func.i32_gt_u() // i32.gt_u (unsigned)
129+
// Also available: i32_ne, i32_le_s, i32_le_u, i32_ge_s, i32_ge_u
130+
```

0 commit comments

Comments
 (0)