v2 / vlib / wasm / tests / debug_test.v
60 lines · 49 sloc · 1.54 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1module main
2
3import wasm
4
5fn test_debug() {
6 mut m := wasm.Module{}
7 m.enable_debug('hello VLANG!')
8
9 // FuncType with debuginfo/name
10 func_type := wasm.FuncType{[.i32_t, .i32_t], [.i32_t], 'fn_<int_int>_int'}
11
12 // []?string, named function parameters
13 param_names := [?string('a'), 'b']
14
15 mut a1 := m.new_debug_function('add', func_type, param_names)
16 {
17 loc := a1.new_local_named(.i32_t, 'intermediary')
18 a1.local_get(0)
19 a1.local_get(1)
20 a1.add(.i32_t)
21 a1.local_set(loc)
22 a1.local_get(loc)
23 }
24 m.commit(a1, false)
25 validate(m.compile()) or { panic(err) }
26}
27
28fn test_wasi_debug() {
29 mut m := wasm.Module{}
30 m.enable_debug('hello!')
31 m.new_global('__vsp', true, .i32_t, true, wasm.constexpr_value(10))
32
33 m.new_global_import('env', 'glble', .i32_t, false)
34
35 m.new_function_import('wasi_unstable', 'proc_exit', [.i32_t], [])
36 m.new_function_import('wasi_unstable', 'fd_write', [.i32_t, .i32_t, .i32_t, .i32_t], [
37 .i32_t,
38 ])
39 m.assign_memory('memory', true, 1, none)
40
41 m.new_data_segment('CIOVec.str', 0, [u8(8), 0, 0, 0]) // pointer to string
42 m.new_data_segment('CIOVec.len', 4, [u8(13), 0, 0, 0]) // length of string
43 m.new_data_segment(none, 8, 'Hello, WASI!\n'.bytes())
44
45 mut func := m.new_function('_start', [], [])
46 {
47 func.new_local_named(.i32_t, 'loc')
48 func.i32_const(1) // stdout
49 func.i32_const(0) // *iovs
50 func.i32_const(1) // 1 iov
51 func.i32_const(-1) // *retptrs
52 func.call_import('wasi_unstable', 'fd_write')
53 func.drop()
54 func.i32_const(0)
55 func.call_import('wasi_unstable', 'proc_exit')
56 }
57 m.commit(func, true)
58
59 print(m.compile().bytestr())
60}
61