Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

6.5 KB
Raw Blame
;;! component_model_async = true
;;! component_model_async_stackful = true

;; A callback-lifted task ($A.f) blocks inside its core function (in
;; `waitable-set.wait`), so it holds $A's exclusive lock. Only sync- and
;; callback-lifted tasks need that lock, so a stackful export ($A.g) can still
;; start, and here it writes the future that $A.f is waiting on.
(component
  (component $A
    (core module $libc (memory (export "mem") 1))
    (core instance $libc (instantiate $libc))
    (type $FT (future))
    (core func $future.new (canon future.new $FT))
    (core func $future.read (canon future.read $FT async (memory (core memory $libc "mem"))))
    (core func $waitable-set.new (canon waitable-set.new))
    (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "mem"))))
    (core func $waitable.join (canon waitable.join))
    (core func $future.write (canon future.write $FT async (memory (core memory $libc "mem"))))
    (core func $future.drop-readable (canon future.drop-readable $FT))
    (core func $future.drop-writable (canon future.drop-writable $FT))
    (core func $task.return (canon task.return))
    (core module $m
      (import "libc" "mem" (memory 1))
      (import "" "future.new" (func $future.new (result i64)))
      (import "" "future.read" (func $future.read (param i32 i32) (result i32)))
      (import "" "future.write" (func $future.write (param i32 i32) (result i32)))
      (import "" "future.drop-readable" (func $future.drop-readable (param i32)))
      (import "" "future.drop-writable" (func $future.drop-writable (param i32)))
      (import "" "task.return" (func $task.return))
      (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
      (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
      (import "" "waitable.join" (func $waitable.join (param i32 i32)))
      (global $w (mut i32) (i32.const 0))
      (func (export "f") (result i32)
        (local $fut i64) (local $r i32) (local $ws i32)
        (local.set $fut (call $future.new))
        (local.set $r (i32.wrap_i64 (local.get $fut)))
        (global.set $w (i32.wrap_i64 (i64.shr_u (local.get $fut) (i64.const 32))))
        ;; async read: BLOCKED
        (if (i32.ne (call $future.read (local.get $r) (i32.const 0)) (i32.const -1))
          (then unreachable))
        ;; wait for it *inside the core function* (not by returning WAIT):
        ;; blocks until $g writes (FUTURE_READ = 4, COMPLETED = 0)
        (local.set $ws (call $waitable-set.new))
        (call $waitable.join (local.get $r) (local.get $ws))
        (if (i32.ne (call $waitable-set.wait (local.get $ws) (i32.const 0)) (i32.const 4))
          (then unreachable))
        (if (i32.ne (i32.load (i32.const 4)) (i32.const 0)) (then unreachable))
        (call $waitable.join (local.get $r) (i32.const 0))
        (call $future.drop-readable (local.get $r))
        (call $task.return)
        (i32.const 0 (; EXIT ;)))
      (func (export "cb") (param i32 i32 i32) (result i32) unreachable)
      (func (export "g")
        ;; the reader is waiting, so even the async write completes eagerly
        (if (i32.ne (call $future.write (global.get $w) (i32.const 0)) (i32.const 0))
          (then unreachable))
        (call $future.drop-writable (global.get $w))
        (call $task.return)))
    (core instance $i (instantiate $m
      (with "libc" (instance $libc))
      (with "" (instance
        (export "future.new" (func $future.new))
        (export "future.read" (func $future.read))
        (export "future.write" (func $future.write))
        (export "future.drop-readable" (func $future.drop-readable))
        (export "future.drop-writable" (func $future.drop-writable))
        (export "task.return" (func $task.return))
        (export "waitable-set.new" (func $waitable-set.new))
        (export "waitable-set.wait" (func $waitable-set.wait))
        (export "waitable.join" (func $waitable.join))))))
    (func (export "f") async (canon lift (core func $i "f") async (callback (core func $i "cb"))))
    (func (export "g") async (canon lift (core func $i "g") async)))

  (component $C
    (import "f" (func $f async))
    (import "g" (func $g async))
    (core module $libc (memory (export "mem") 1))
    (core instance $libc (instantiate $libc))
    (core func $f (canon lower (func $f) async (memory (core memory $libc "mem"))))
    (core func $g (canon lower (func $g) async (memory (core memory $libc "mem"))))
    (core func $waitable-set.new (canon waitable-set.new))
    (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "mem"))))
    (core func $waitable.join (canon waitable.join))
    (core func $subtask.drop (canon subtask.drop))
    (core func $task.return (canon task.return))
    (core module $m
      (import "" "f" (func $f (result i32)))
      (import "" "g" (func $g (result i32)))
      (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
      (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
      (import "" "waitable.join" (func $waitable.join (param i32 i32)))
      (import "" "subtask.drop" (func $subtask.drop (param i32)))
      (import "" "task.return" (func $task.return))
      (func (export "run")
        (local $ret i32) (local $sf i32) (local $ws i32)
        ;; $A.f blocks: subtask 1, STARTED (1)
        (local.set $ret (call $f))
        (if (i32.ne (local.get $ret) (i32.const 0x11)) (then unreachable))
        (local.set $sf (i32.shr_u (local.get $ret) (i32.const 4)))
        ;; $A.g starts right away and returns: RETURNED (2)
        (if (i32.ne (call $g) (i32.const 2)) (then unreachable))
        ;; now $A.f can finish
        (local.set $ws (call $waitable-set.new))
        (call $waitable.join (local.get $sf) (local.get $ws))
        (if (i32.ne (call $waitable-set.wait (local.get $ws) (i32.const 0)) (i32.const 1 (; SUBTASK ;)))
          (then unreachable))
        (call $subtask.drop (local.get $sf))
        (call $task.return)))
    (core instance $i (instantiate $m (with "" (instance
      (export "f" (func $f))
      (export "g" (func $g))
      (export "waitable-set.new" (func $waitable-set.new))
      (export "waitable-set.wait" (func $waitable-set.wait))
      (export "waitable.join" (func $waitable.join))
      (export "subtask.drop" (func $subtask.drop))
      (export "task.return" (func $task.return))))))
    (func (export "run") async (canon lift (core func $i "run") async)))

  (instance $a (instantiate $A))
  (instance $c (instantiate $C (with "f" (func $a "f")) (with "g" (func $a "g"))))
  (export "run" (func $c "run")))

(assert_return (invoke "run"))