813
813
(local.set $i (i32.add (i32.const 1 ) (local.get $i )))
814
814
(br $next )))
815
815
816
- (func $sumUp (export " sumUp" ) (param $n i32 ) (result i32 )
816
+ (func (export " sumUp" ) (param $n i32 ) (result i32 )
817
817
(local $i i32 )
818
818
(local $j i32 )
819
819
(local $k (ref $ct ))
831
831
)
832
832
(return (local.get $i )))
833
833
834
- (elem declare func $nats )
834
+ (func $nats-bad (type $ft )
835
+ (local $h (ref $ht ))
836
+ (local $i i32 )
837
+ (local.set $h (local.get 0 ))
838
+ (loop $next
839
+ (suspend_to $ht $yield
840
+ (local.get $i ) (local.get $h ))
841
+ (drop ) ;; drop the handle
842
+ (local.set $i (i32.add (i32.const 1 ) (local.get $i )))
843
+ (br $next )))
844
+
845
+ (func (export " sumUp-bad" ) (param $n i32 ) (result i32 )
846
+ (local $i i32 )
847
+ (local $j i32 )
848
+ (local $k (ref $ct ))
849
+ (local.set $k (cont.new $ct (ref.func $nats-bad )))
850
+ (loop $next
851
+ (block $on_yield (result i32 (ref $ct ))
852
+ (resume_with $ct (on $yield $on_yield ) (local.get $k ))
853
+ (return (local.get $i ))
854
+ ) ;; on_yield
855
+ (local.set $k )
856
+ (i32.add (local.get $i ))
857
+ (local.set $i )
858
+ (local.set $j (i32.add (i32.const 1 ) (local.get $j )))
859
+ (br_if $next (i32.le_u (local.get $j ) (local.get $n )))
860
+ )
861
+ (return (local.get $i )))
862
+
863
+ (elem declare func $nats $nats-bad )
864
+ )
865
+ (assert_return (invoke " sumUp" (i32.const 10 )) (i32.const 55 ))
866
+ (assert_suspension (invoke " sumUp-bad" (i32.const 10 )) " unhandled tag" )
867
+
868
+ (module
869
+ (type $ht (handler))
870
+ (type $ft (func (param (ref $ht ))))
871
+ (type $ct (cont $ft ))
872
+
873
+ (type $ft2 (func ))
874
+ (type $ct2 (cont $ft2 ))
875
+
876
+ (global $h1 (mut (ref null $ht )) (ref.null $ht ))
877
+ (global $h2 (mut (ref null $ht )) (ref.null $ht ))
878
+
879
+ (tag $yield )
880
+
881
+ (func $escape (type $ft )
882
+ (global.set $h2 (local.get 0 ))
883
+ (resume $ct2 (cont.new $ct2 (ref.func $do-suspend )))
884
+ (unreachable ))
885
+
886
+ (func $do-suspend
887
+ (suspend_to $ht $yield (global.get $h1 ))
888
+ (unreachable ))
889
+
890
+ (func $generate-name (type $ft )
891
+ (global.set $h1 (local.get 0 ))
892
+ (block $on_yield (result (ref $ct ))
893
+ (resume_with $ct (on $yield $on_yield ) (cont.new $ct (ref.func $escape )))
894
+ (unreachable )
895
+ ) ;; on_yield [named]
896
+ (unreachable ))
897
+
898
+ (func (export " use-escapee" )
899
+ (block $on_yield (result (ref $ct ))
900
+ (resume_with $ct (on $yield $on_yield ) (cont.new $ct (ref.func $generate-name )))
901
+ (unreachable )
902
+ ) ;; on_yield
903
+ (suspend_to $ht $yield (global.get $h2 ))
904
+ (unreachable ))
905
+
906
+ (elem declare func $escape $do-suspend $generate-name )
835
907
)
836
- (assert_return (invoke " sumUp " ( i32.const 10 )) ( i32.const 55 ) )
908
+ (assert_suspension (invoke " use-escapee " ) " unhandled tag " )
0 commit comments