Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created April 29, 2025 20:01
Show Gist options
  • Save alexcrichton/e5d18e6f5aa25eed71a1d3decb137d04 to your computer and use it in GitHub Desktop.
Save alexcrichton/e5d18e6f5aa25eed71a1d3decb137d04 to your computer and use it in GitHub Desktop.
(component
(type $f (future))
(core func $new (canon future.new $f))
(core module $libc (memory (export "mem") 1))
(core instance $libc (instantiate $libc))
(core func $write (canon future.write $f async (memory $libc "mem")))
(core func $cancel (canon future.cancel-write $f))
(core func $close-read (canon future.close-readable $f))
(core module $m
(import "" "new" (func $new (result i64)))
(import "" "write" (func $write (param i32 i32) (result i32)))
(import "" "cancel" (func $cancel (param i32) (result i32)))
(import "" "close-read" (func $close-read (param i32)))
(func (export "f") (result i32)
(local $read i32)
(local $write i32)
(local $new i64)
(local.set $new (call $new))
(local.set $read (i32.wrap_i64 (local.get $new)))
(local.set $write (i32.wrap_i64 (i64.shr_u (local.get $new) (i64.const 32))))
;; start a write
local.get $write
i32.const 0
call $write
i32.const -1
i32.ne
if unreachable end
;; close the read end
local.get $read
call $close-read
;; cancel the write, returning the result
local.get $write
call $cancel
)
)
(core instance $i (instantiate $m
(with "" (instance
(export "new" (func $new))
(export "write" (func $write))
(export "cancel" (func $cancel))
(export "close-read" (func $close-read))
))
))
(func (export "f") (result u32) (canon lift (core func $i "f")))
)
(assert_return (invoke "f") (u32.const 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment