1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
(module
(memory 1 2)
(type $ii (func (param i32) (result i32)))
(import "env" "sleep" (func $sleep))
(import "env" "tunnel" (func $tunnel (param $x i32) (result i32)))
(export "memory" (memory 0))
(export "factorial-recursive" (func $factorial-recursive))
(global $temp (mut i32) (i32.const 0))
(table 10 funcref)
(elem (i32.const 5) $tablefunc)
(func "minimal" (result i32)
(call $sleep)
(i32.const 21)
)
(func "repeat" (result i32)
;; sleep twice, then return 42
(call $sleep)
(call $sleep)
(i32.const 42)
)
(func "local" (result i32)
(local $x i32)
(local.set $x (i32.load (i32.const 0))) ;; a zero that the optimizer won't see
(local.set $x
(i32.add (local.get $x) (i32.const 10)) ;; add 10
)
(call $sleep)
(local.get $x)
)
(func "local2" (result i32)
(local $x i32)
(local.set $x (i32.load (i32.const 0))) ;; a zero that the optimizer won't see
(local.set $x
(i32.add (local.get $x) (i32.const 10)) ;; add 10
)
(call $sleep)
(local.set $x
(i32.add (local.get $x) (i32.const 12)) ;; add 12 more
)
(local.get $x)
)
(func "params" (param $x i32) (param $y i32) (result i32)
(local.set $x
(i32.add (local.get $x) (i32.const 17)) ;; add 10
)
(local.set $y
(i32.add (local.get $y) (i32.const 1)) ;; add 12 more
)
(call $sleep)
(i32.add (local.get $x) (local.get $y))
)
(func $pre
(global.set $temp (i32.const 1))
)
(func $inner (param $x i32)
(if (i32.eqz (local.get $x)) (call $post))
(if (local.get $x) (call $sleep))
(if (i32.eqz (local.get $x)) (call $post))
)
(func $post
(global.set $temp
(i32.mul
(global.get $temp)
(i32.const 3)
)
)
)
(func "deeper" (param $x i32) (result i32)
(call $pre)
(call $inner (local.get $x))
(call $post)
(global.get $temp)
)
(func $factorial-recursive (param $x i32) (result i32)
(if
(i32.eq
(local.get $x)
(i32.const 1)
)
(return (i32.const 1))
)
(call $sleep)
(return
(i32.mul
(local.get $x)
(call $factorial-recursive
(i32.sub
(local.get $x)
(i32.const 1)
)
)
)
)
)
(func "factorial-loop" (param $x i32) (result i32)
(local $i i32)
(local $ret i32)
(local.set $ret (i32.const 1))
(local.set $i (i32.const 2))
(loop $l
(if
(i32.gt_u
(local.get $i)
(local.get $x)
)
(return (local.get $ret))
)
(local.set $ret
(i32.mul
(local.get $ret)
(local.get $i)
)
)
(call $sleep)
(local.set $i
(i32.add
(local.get $i)
(i32.const 1)
)
)
(br $l)
)
)
(func "end_tunnel" (param $x i32) (result i32)
(local.set $x
(i32.add (local.get $x) (i32.const 22))
)
(call $sleep)
(i32.add (local.get $x) (i32.const 5))
)
(func "do_tunnel" (param $x i32) (result i32)
(local.set $x
(i32.add (local.get $x) (i32.const 11))
)
(local.set $x
(call $tunnel (local.get $x)) ;; calls js which calls back into wasm for end_tunnel
)
(call $sleep)
(i32.add (local.get $x) (i32.const 33))
)
(func $tablefunc (param $y i32) (result i32)
(local.set $y
(i32.add (local.get $y) (i32.const 10))
)
(call $sleep)
(i32.add (local.get $y) (i32.const 30))
)
(func "call_indirect" (param $x i32) (param $y i32) (result i32)
(local.set $x
(i32.add (local.get $x) (i32.const 1))
)
(call $sleep)
(local.set $x
(i32.add (local.get $x) (i32.const 3))
)
(local.set $y
(call_indirect (type $ii) (local.get $y) (local.get $x)) ;; call function pointer x + 4, which will be 5
)
(local.set $y
(i32.add (local.get $y) (i32.const 90))
)
(call $sleep)
(i32.add (local.get $y) (i32.const 300)) ;; total is 10+30+90+300=430 + y's original value
)
(func "if_else" (param $x i32) (param $y i32) (result i32)
(if (i32.eq (local.get $x) (i32.const 1))
(local.set $y
(i32.add (local.get $y) (i32.const 10))
)
(local.set $y
(i32.add (local.get $y) (i32.const 20))
)
)
(if (i32.eq (local.get $x) (i32.const 1))
(local.set $y
(i32.add (local.get $y) (i32.const 40))
)
(call $sleep)
)
(if (i32.eq (local.get $x) (i32.const 1))
(call $sleep)
(local.set $y
(i32.add (local.get $y) (i32.const 90))
)
)
(if (i32.eq (local.get $x) (i32.const 1))
(call $sleep)
(call $sleep)
)
(local.set $y
(i32.add (local.get $y) (i32.const 160))
)
(call $sleep)
(local.set $y
(i32.add (local.get $y) (i32.const 250))
)
(local.get $y)
)
)
|