v2 / examples / wasm_codegen / hello_wasi.v
30 lines · 26 sloc · 853 bytes · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import wasm
2
3fn main() {
4 mut m := wasm.Module{}
5 m.enable_debug('vlang')
6 m.new_function_import('wasi_unstable', 'proc_exit', [.i32_t], [])
7 m.new_function_import('wasi_unstable', 'fd_write', [.i32_t, .i32_t, .i32_t, .i32_t], [
8 .i32_t,
9 ])
10 m.assign_memory('memory', true, 1, none)
11
12 m.new_data_segment('CIOVec.str', 0, [u8(8), 0, 0, 0]) // pointer to string
13 m.new_data_segment('CIOVec.len', 4, [u8(13), 0, 0, 0]) // length of string
14 m.new_data_segment(none, 8, 'Hello, WASI!\n'.bytes())
15
16 mut func := m.new_function('_start', [], [])
17 {
18 func.i32_const(1) // stdout
19 func.i32_const(0) // *iovs
20 func.i32_const(1) // 1 iov
21 func.i32_const(-1) // *retptrs
22 func.call_import('wasi_unstable', 'fd_write')
23 func.drop()
24 func.i32_const(0)
25 func.call_import('wasi_unstable', 'proc_exit')
26 }
27 m.commit(func, true)
28
29 print(m.compile().bytestr())
30}
31