v2 / vlib / wasm / tests / call_test.v
86 lines · 81 sloc · 1.5 KB · 3a06b55388559598c05e46ddb776d7443180b56b
Raw
1module main
2
3import wasm
4
5fn test_call() {
6 mut m := wasm.Module{}
7 mut a1 := m.new_function('const-i32', [], [.i32_t])
8 {
9 a1.i32_const(0x132)
10 }
11 m.commit(a1, false)
12 mut a2 := m.new_function('const-i64', [], [.i64_t])
13 {
14 a2.i64_const(0x164)
15 }
16 m.commit(a2, false)
17 mut a3 := m.new_function('const-f32', [], [.f32_t])
18 {
19 a3.f32_const(0.2)
20 }
21 m.commit(a3, false)
22 mut a4 := m.new_function('const-f64', [], [.f64_t])
23 {
24 a4.f64_const(0.4)
25 }
26 m.commit(a4, false)
27 mut a5 := m.new_function('const-i32-i64', [], [.i32_t, .i64_t])
28 {
29 a5.i32_const(0x132)
30 a5.i64_const(0x164)
31 }
32 m.commit(a5, false)
33
34 mut b1 := m.new_function('type-i32', [], [.i32_t])
35 {
36 b1.call('const-i32')
37 }
38 m.commit(b1, true)
39 mut b2 := m.new_function('type-i64', [], [.i64_t])
40 {
41 b2.call('const-i64')
42 }
43 m.commit(b2, true)
44 mut b3 := m.new_function('type-f32', [], [.f32_t])
45 {
46 b3.call('const-f32')
47 }
48 m.commit(b3, true)
49 mut b4 := m.new_function('type-f64', [], [.f64_t])
50 {
51 b4.call('const-f64')
52 }
53 m.commit(b4, true)
54 mut b5 := m.new_function('type-i32-i64', [], [.i32_t, .i64_t])
55 {
56 b5.call('const-i32-i64')
57 }
58 m.commit(b5, true)
59
60 mut fac := m.new_function('fac', [.i64_t], [.i64_t])
61 {
62 fac.local_get(0)
63 fac.eqz(.i64_t)
64 ifs := fac.c_if([], [.i64_t])
65 {
66 fac.i64_const(1)
67 }
68 fac.c_else(ifs)
69 {
70 {
71 fac.local_get(0)
72 }
73 {
74 fac.local_get(0)
75 fac.i64_const(1)
76 fac.sub(.i64_t)
77 fac.call('fac')
78 }
79 fac.mul(.i64_t)
80 }
81 fac.c_end(ifs)
82 }
83 m.commit(fac, true)
84
85 validate(m.compile()) or { panic(err) }
86}
87