|
| 1 | +(component |
| 2 | + ;; This test has two components $C and $D, where $D imports and calls $C |
| 3 | + (component $C |
| 4 | + (core module $Memory (memory (export "mem") 1)) |
| 5 | + (core instance $memory (instantiate $Memory)) |
| 6 | + (core module $CM |
| 7 | + (import "" "mem" (memory 1)) |
| 8 | + (import "" "task.return" (func $return (param i32))) |
| 9 | + (import "" "waitable.join" (func $waitable.join (param i32 i32))) |
| 10 | + (import "" "waitable-set.new" (func $waitable-set.new (result i32))) |
| 11 | + (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32))) |
| 12 | + |
| 13 | + (global $ws (mut i32) (i32.const 0)) |
| 14 | + (func $start (global.set $ws (call $waitable-set.new))) |
| 15 | + (start $start) |
| 16 | + |
| 17 | + (func (export "blocker") (result i32) |
| 18 | + (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (global.get $ws) (i32.const 4))) |
| 19 | + ) |
| 20 | + (func (export "blocker_cb") (param $event_code i32) (param $index i32) (param $payload i32) (result i32) |
| 21 | + unreachable |
| 22 | + ;; TODO |
| 23 | + ;; (if (i32.ne (i32.const 4 (; FUTURE_READ ;)) (local.get $event_code)) |
| 24 | + ;; (then unreachable)) |
| 25 | + (if (i32.ne (i32.const 0 (; NONE ;)) (local.get $event_code)) |
| 26 | + (then unreachable)) |
| 27 | + (if (i32.ne (i32.const 0) (local.get $index)) |
| 28 | + (then unreachable)) |
| 29 | + (if (i32.ne (i32.const 0) (local.get $payload)) |
| 30 | + (then unreachable)) |
| 31 | + |
| 32 | + (call $return (i32.const 42)) |
| 33 | + (i32.const 0)) |
| 34 | + |
| 35 | + (func (export "unblocker") (result i32) |
| 36 | + unreachable |
| 37 | + ) |
| 38 | + (func (export "unblocker_cb") (param i32 i32 i32) (result i32) |
| 39 | + unreachable |
| 40 | + ) |
| 41 | + ) |
| 42 | + (canon task.return (result u32) (core func $task.return)) |
| 43 | + (canon waitable.join (core func $waitable.join)) |
| 44 | + (canon waitable-set.new (core func $waitable-set.new)) |
| 45 | + (canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait)) |
| 46 | + (core instance $cm (instantiate $CM (with "" (instance |
| 47 | + (export "mem" (memory $memory "mem")) |
| 48 | + (export "task.return" (func $task.return)) |
| 49 | + (export "waitable.join" (func $waitable.join)) |
| 50 | + (export "waitable-set.new" (func $waitable-set.new)) |
| 51 | + (export "waitable-set.wait" (func $waitable-set.wait)) |
| 52 | + )))) |
| 53 | + (func (export "blocker") (result u32) (canon lift |
| 54 | + (core func $cm "blocker") |
| 55 | + async (memory $memory "mem") (callback (func $cm "blocker_cb")) |
| 56 | + )) |
| 57 | + (func (export "unblocker") (result u32) (canon lift |
| 58 | + (core func $cm "unblocker") |
| 59 | + async (memory $memory "mem") (callback (func $cm "unblocker_cb")) |
| 60 | + )) |
| 61 | + ) |
| 62 | + |
| 63 | + (component $D |
| 64 | + (import "blocker" (func $blocker (result u32))) |
| 65 | + (import "unblocker" (func $unblocker (result u32))) |
| 66 | + |
| 67 | + (core module $Memory (memory (export "mem") 1)) |
| 68 | + (core instance $memory (instantiate $Memory)) |
| 69 | + (core module $DM |
| 70 | + (import "" "mem" (memory 1)) |
| 71 | + (import "" "waitable.join" (func $waitable.join (param i32 i32))) |
| 72 | + (import "" "waitable-set.new" (func $waitable-set.new (result i32))) |
| 73 | + (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32))) |
| 74 | + (import "" "blocker" (func $blocker (param i32 i32) (result i32))) |
| 75 | + (import "" "unblocker" (func $unblocker (param i32 i32) (result i32))) |
| 76 | + |
| 77 | + (global $ws (mut i32) (i32.const 0)) |
| 78 | + (func $start (global.set $ws (call $waitable-set.new))) |
| 79 | + (start $start) |
| 80 | + |
| 81 | + (func (export "run") (result i32) |
| 82 | + (local $ret i32) (local $subtaski i32) (local $event_code i32) (local $retp i32) |
| 83 | + |
| 84 | + ;; call 'blocker', which, true to its name, will block on an empty waitable set |
| 85 | + (local.set $ret (call $blocker (i32.const 0) (i32.const 0))) |
| 86 | + (if (i32.ne (i32.const 1 (; STARTING ;)) (i32.and (local.get $ret) (i32.const 0xf))) |
| 87 | + (then unreachable)) |
| 88 | + (local.set $subtaski (i32.shr_u (local.get $ret) (i32.const 4))) |
| 89 | + (if (i32.ne (i32.const 2) (local.get $subtaski)) |
| 90 | + (then unreachable)) |
| 91 | + |
| 92 | + ;; call 'unblocker' to unblock the 'blocker' subtask |
| 93 | + ;; TODO... |
| 94 | + |
| 95 | + |
| 96 | + ;; wait on the subtask |
| 97 | + (call $waitable.join (local.get $subtaski) (global.get $ws)) |
| 98 | + (local.set $retp (i32.const 0)) |
| 99 | + (local.set $event_code (call $waitable-set.wait (global.get $ws) (local.get $retp))) |
| 100 | + unreachable |
| 101 | + (if (i32.ne (i32.const 1 (; SUBTASK ;)) (local.get $event_code)) |
| 102 | + (then unreachable)) |
| 103 | + (if (i32.ne (local.get $subtaski) (i32.load (local.get $retp))) |
| 104 | + (then unreachable)) |
| 105 | + (if (i32.ne (i32.const 2 (; RETURNED ;)) (i32.load offset=4 (local.get $retp))) |
| 106 | + (then unreachable)) |
| 107 | + |
| 108 | + (i32.const 42) |
| 109 | + ) |
| 110 | + ) |
| 111 | + (canon waitable.join (core func $waitable.join)) |
| 112 | + (canon waitable-set.new (core func $waitable-set.new)) |
| 113 | + (canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait)) |
| 114 | + (canon lower (func $blocker) async (memory $memory "mem") (core func $blocker')) |
| 115 | + (core instance $dm (instantiate $DM (with "" (instance |
| 116 | + (export "mem" (memory $memory "mem")) |
| 117 | + (export "waitable.join" (func $waitable.join)) |
| 118 | + (export "waitable-set.new" (func $waitable-set.new)) |
| 119 | + (export "waitable-set.wait" (func $waitable-set.wait)) |
| 120 | + (export "blocker" (func $blocker')) |
| 121 | + )))) |
| 122 | + (func (export "run") (result u32) (canon lift (core func $dm "run"))) |
| 123 | + ) |
| 124 | + |
| 125 | + (instance $c (instantiate $C)) |
| 126 | + (instance $d (instantiate $D (with "blocker" (func $c "blocker")))) |
| 127 | + (func (export "run") (alias export $d "run")) |
| 128 | +) |
| 129 | +(assert_return (invoke "run") (u32.const 42)) |
0 commit comments