Debian Codegraph @

tests/cli/stack-switching/fun-state.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching
2
3 ;; Simple state example - functional with heterogeneous continuations
4 (module $state
5 (tag $get (result i32))
6 (tag $set (param i32))
7
8 (type $gf (func (param i32) (result i32)))
9 (type $sf (func (result i32)))
10
11 (type $gk (cont $gf))
12 (type $sk (cont $sf))
13
14 (func $getting (param $k (ref $gk)) (param $s i32) (result i32)
15 (block $on_get (result (ref $gk))
16 (block $on_set (result i32 (ref $sk))
17 (resume $gk (on $get $on_get) (on $set $on_set)
18 (local.get $s) (local.get $k)
19 )
20 (return)
21 ) ;; $on_set (result i32 (ref $sk))
22 (return_call $setting)
23 ) ;; $on_get (result (ref $gk))
24 (local.get $s)
25 (return_call $getting)
26 )
27
28 (func $setting (param $s i32) (param $k (ref $sk)) (result i32)
29 (block $on_get (result (ref $gk))
30 (block $on_set (result i32 (ref $sk))
31 (resume $sk (on $get $on_get) (on $set $on_set)
32 (local.get $k)
33 )
34 (return)
35 ) ;; $on_set (result i32 (ref $sk))
36 (return_call $setting)
37 ) ;; $on_get (result (ref $gk))
38 (local.get $s)
39 (return_call $getting)
40 )
41
42 (func $f (result i32)
43 (suspend $set (i32.const 7))
44 (i32.add
45 (suspend $get)
46 (i32.mul
47 (i32.const 2)
48 (suspend $set (i32.const 3))
49 (i32.add
50 (i32.const 3)
51 (suspend $get)
52 )
53 )
54 )
55 )
56
57 (elem declare func $f)
58 (func (export "run") (result i32)
59 (call $setting (i32.const 0) (cont.new $sk (ref.func $f)))
60 )
61 )
62
63 (assert_return (invoke "run") (i32.const 19))