(module $promise (type $func (;0;) (func)) (type $cont (;1;) (cont $func)) (type $i-func (;2;) (func (param i32))) (type $i-cont (;3;) (cont $i-func)) (type (;4;) (func (result i32))) (type (;5;) (func (param i32) (result i32))) (type (;6;) (func (param i32 (ref $i-cont)))) (type (;7;) (func (param i32 i32) (result (ref null $cont)))) (table $observers (;0;) 1000 (ref null $i-cont)) (memory (;0;) 1) (tag $too-many-promises (;0;) (type $func)) (tag $too-many-observers (;1;) (type $func)) (global $num-promises (;0;) (mut i32) i32.const 0) (global $max-promises (;1;) i32 i32.const 1000) (export "new" (func $new)) (export "fulfilled" (func $fulfilled)) (export "read" (func $read)) (export "await" (func $await)) (export "fulfill" (func $fulfill)) (func $new (;0;) (type 4) (result i32) (local $offset i32) (local $p i32) global.get $num-promises global.get $max-promises i32.eq if ;; label = @1 throw $too-many-promises end global.get $num-promises local.set $p local.get $p i32.const 4 i32.mul local.set $offset local.get $p ref.null $i-cont table.set $observers local.get $offset i32.const -1 i32.store local.get $p i32.const 1 i32.add global.set $num-promises local.get $p return ) (func $fulfilled (;1;) (type 5) (param $p i32) (result i32) (local $offset i32) local.get $p i32.const 4 i32.mul local.set $offset local.get $offset i32.load i32.const -1 i32.ne ) (func $read (;2;) (type 5) (param $p i32) (result i32) (local $offset i32) local.get $p i32.const 4 i32.mul local.set $offset local.get $offset i32.load ) (func $await (;3;) (type 6) (param $p i32) (param $k (ref $i-cont)) local.get $p table.get $observers ref.is_null if ;; label = @1 local.get $p local.get $k table.set $observers else throw $too-many-observers end ) (func $fulfill (;4;) (type 7) (param $p i32) (param $v i32) (result (ref null $cont)) (local $offset i32) (local $k (ref null $i-cont)) local.get $p i32.const 4 i32.mul local.set $offset local.get $offset local.get $v i32.store local.get $p table.get $observers local.set $k local.get $k ref.is_null if ;; label = @1 ref.null $cont return end local.get $v local.get $k cont.bind $i-cont $cont return ) )