Skip to content

Commit ef2e705

Browse files
authored
Merge pull request #137 from WebAssembly/resume_throw_ref
Implementation and test suite for resume_throw_ref
2 parents 8fe7145 + 586ae27 commit ef2e705

File tree

12 files changed

+358
-4
lines changed

12 files changed

+358
-4
lines changed

interpreter/binary/decode.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,10 @@ let rec instr s =
646646
let tag = at var s in
647647
let xls = vec on_clause s in
648648
resume_throw x tag xls
649-
(* TODO: resume_throw_ref *)
649+
| 0xe5 ->
650+
let x = at var s in
651+
let xls = vec on_clause s in
652+
resume_throw_ref x xls
650653
| 0xe6 ->
651654
let x = at var s in
652655
let y = at var s in

interpreter/binary/encode.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ struct
303303
| Suspend x -> op 0xe2; var x
304304
| Resume (x, xls) -> op 0xe3; var x; resumetable xls
305305
| ResumeThrow (x, y, xls) -> op 0xe4; var x; var y; resumetable xls
306-
(* TOOD: resume_throw_ref *)
306+
| ResumeThrowRef (x, xls) -> op 0xe5; var x; resumetable xls
307307
| Switch (x, y) -> op 0xe6; var x; var y
308308

309309
| Throw x -> op 0x08; var x

interpreter/exec/eval.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,22 @@ let rec step (c : config) : config =
407407
cont := None;
408408
vs', [Prompt (hs, ctxt ([], [Throwing (tagt, args) @@ e.at])) @@ e.at]
409409

410+
| ResumeThrowRef (x, xls), Ref _ :: Ref (NullRef _) :: vs ->
411+
vs, [Trapping "null exception reference" @@ e.at]
412+
413+
| ResumeThrowRef (x, xls), Ref (NullRef _) :: vs ->
414+
vs, [Trapping "null continuation reference" @@ e.at]
415+
416+
| ResumeThrowRef (x, xls), Ref (ContRef {contents = None}) :: Ref _ :: vs ->
417+
vs, [Trapping "continuation already consumed" @@ e.at]
418+
419+
| ResumeThrowRef (x, xls),
420+
Ref (ContRef ({contents = Some (n, ctxt)} as cont)) ::
421+
v :: vs ->
422+
let hs = handle_table c xls in
423+
cont := None;
424+
vs, [Prompt (hs, ctxt ([v], [Plain ThrowRef @@ e.at])) @@ e.at]
425+
410426
| Switch (x, y), Ref (NullRef _) :: vs ->
411427
vs, [Trapping "null continuation reference" @@ e.at]
412428

interpreter/syntax/ast.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ and instr' =
177177
| Suspend of idx (* suspend continuation *)
178178
| Resume of idx * (idx * hdl) list (* resume continuation *)
179179
| ResumeThrow of idx * idx * (idx * hdl) list (* abort continuation *)
180+
| ResumeThrowRef of idx * (idx * hdl) list (* abort continuation *)
180181
| Switch of idx * idx (* direct switch continuation *)
181182
| Throw of idx (* throw exception *)
182183
| ThrowRef (* rethrow exception *)

interpreter/syntax/free.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ let rec instr (e : instr) =
181181
| ContNew x -> types (idx x)
182182
| ContBind (x, y) -> types (idx x) ++ types (idx y)
183183
| ResumeThrow (x, y, xys) -> types (idx x) ++ tags (idx y) ++ list (fun (x, y) -> tags (idx x) ++ hdl y) xys
184+
| ResumeThrowRef (x, xys) -> types (idx x) ++ list (fun (x, y) -> tags (idx x) ++ hdl y) xys
184185
| Resume (x, xys) -> types (idx x) ++ list (fun (x, y) -> tags (idx x) ++ hdl y) xys
185186
| Suspend x -> tags (idx x)
186187
| Switch (x, z) -> types (idx x) ++ tags (idx z)

interpreter/syntax/operators.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ let cont_bind x y = ContBind (x, y)
5454
let suspend x = Suspend x
5555
let resume x xys = Resume (x, xys)
5656
let resume_throw x y xys = ResumeThrow (x, y, xys)
57+
let resume_throw_ref x xys = ResumeThrowRef (x, xys)
5758
let switch x y = Switch (x, y)
5859
let throw x = Throw x
5960
let throw_ref = ThrowRef

interpreter/text/arrange.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ let rec instr e =
561561
"resume " ^ var x, resumetable xys
562562
| ResumeThrow (x, y, xys) ->
563563
"resume_throw " ^ var x ^ " " ^ var y, resumetable xys
564+
| ResumeThrowRef (x, xys) ->
565+
"resume_throw_ref " ^ var x, resumetable xys
564566
| Switch (x, z) ->
565567
"switch " ^ var x ^ " " ^ var z, []
566568
| Throw x -> "throw " ^ var x, []

interpreter/text/lexer.mll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ rule token = parse
230230
| "suspend" -> SUSPEND
231231
| "resume" -> RESUME
232232
| "resume_throw" -> RESUME_THROW
233+
| "resume_throw_ref" -> RESUME_THROW_REF
233234
| "switch" -> SWITCH
234235

235236

interpreter/text/parser.mly

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ let parse_annots (m : module_) : Custom.section list =
304304
%token MUT FIELD STRUCT ARRAY SUB FINAL REC
305305
%token UNREACHABLE NOP DROP SELECT
306306
%token BLOCK END IF THEN ELSE LOOP
307-
%token CONT_NEW CONT_BIND SUSPEND RESUME RESUME_THROW SWITCH
307+
%token CONT_NEW CONT_BIND SUSPEND RESUME RESUME_THROW RESUME_THROW_REF SWITCH
308308
%token BR BR_IF BR_TABLE BR_ON_NON_NULL
309309
%token<Ast.idx -> Ast.instr'> BR_ON_NULL
310310
%token<Ast.idx -> Types.ref_type -> Types.ref_type -> Ast.instr'> BR_ON_CAST
@@ -789,6 +789,11 @@ resume_instr_instr_list :
789789
let x = $2 c type_ in
790790
let tag = $3 c tag in
791791
let hs, es = $4 c in (resume_throw x tag hs @@ loc1) :: es }
792+
| RESUME_THROW_REF var resume_instr_handler_instr
793+
{ let loc1 = $loc($1) in
794+
fun c ->
795+
let x = $2 c type_ in
796+
let hs, es = $3 c in (resume_throw_ref x hs @@ loc1) :: es }
792797

793798
resume_instr_handler_instr :
794799
| LPAR ON var var RPAR resume_instr_handler_instr
@@ -907,6 +912,11 @@ expr1 : /* Sugar */
907912
let tag = $3 c tag in
908913
let hs, es = $4 c in
909914
es, resume_throw x tag hs }
915+
| RESUME_THROW_REF var resume_expr_handler
916+
{ fun c ->
917+
let x = $2 c type_ in
918+
let hs, es = $3 c in
919+
es, resume_throw_ref x hs }
910920
| BLOCK labeling_opt block
911921
{ fun c -> let c' = $2 c [] in let bt, es = $3 c' in [], block bt es }
912922
| LOOP labeling_opt block

interpreter/valid/valid.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ let rec check_instr (c : context) (e : instr) (s : infer_result_type) : infer_in
658658
check_resume_table c ts2 xys e.at;
659659
(ts0 @ [RefT (Null, VarHT (StatX x.it))]) --> ts2, []
660660

661+
| ResumeThrowRef (x, xys) ->
662+
let ct = cont_type c x in
663+
let FuncT (_ts1, ts2) = func_type_of_cont_type c ct x.at in
664+
check_resume_table c ts2 xys e.at;
665+
([RefT (Null, ExnHT); RefT (Null, VarHT (StatX x.it))]) --> ts2, []
666+
661667
| Switch (x, y) ->
662668
let ct1 = cont_type c x in
663669
let FuncT (ts11, ts12) = func_type_of_cont_type c ct1 x.at in

0 commit comments

Comments
 (0)