Skip to content

Commit 7afd3c6

Browse files
committed
Address Andreas' feedback
1 parent c78f405 commit 7afd3c6

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

interpreter/exec/eval.ml

+6-17
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ and admin_instr' =
7373
| Frame of int * frame * code
7474
| Handler of int * catch list * code
7575
| Handle of handle_table * code
76-
| Suspending of tag_inst * value stack * ref_ option * ctxt
76+
| Suspending of tag_inst * value stack * (int32 * ref_) option * ctxt
7777

7878
and ctxt = code -> code
7979
and handle_table = (tag_inst * idx) list * tag_inst list
@@ -104,15 +104,6 @@ let is_jumping e =
104104

105105
let compose (vs1, es1) (vs2, es2) = vs1 @ vs2, es1 @ es2
106106

107-
let switcher_contref_arity contarg =
108-
match contarg with
109-
| RefT (_, DefHT def) ->
110-
(match as_cont_str_type (expand_def_type def) with
111-
| ContT (DefHT def) ->
112-
let FuncT (ts, _) = as_func_str_type (expand_def_type def) in
113-
Lib.List32.length ts
114-
| _ -> assert false)
115-
| _ -> assert false
116107

117108
(* Configurations *)
118109

@@ -422,14 +413,12 @@ let rec step (c : config) : config =
422413

423414
| Switch (x, y), Ref (ContRef ({contents = Some (n, ctxt)} as cont)) :: vs ->
424415
let FuncT (ts, _) = func_type_of_cont_type c.frame.inst (cont_type c.frame.inst x) in
425-
let arity = switcher_contref_arity (Lib.List.last ts) in
416+
let FuncT (ts', _) = as_cont_func_ref_type (Lib.List.last ts) in
417+
let arity = Lib.List32.length ts' in
426418
let tagt = tag c.frame.inst y in
427419
let args, vs' = i32_split (Int32.sub n 1l) vs e.at in
428-
(* NOTE(dhil): Slight hack here. We update the arity of given
429-
contref to be the arity of the current continuation
430-
generated by the handler for switch. *)
431420
cont := Some (arity, ctxt);
432-
vs', [Suspending (tagt, args, Some (ContRef cont), fun code -> code) @@ e.at]
421+
vs', [Suspending (tagt, args, Some (arity, ContRef cont), fun code -> code) @@ e.at]
433422

434423
| ReturnCall x, vs ->
435424
(match (step {c with code = (vs, [Plain (Call x) @@ e.at])}).code with
@@ -1277,10 +1266,10 @@ let rec step (c : config) : config =
12771266
[Ref (ContRef (ref (Some (Lib.List32.length ts, ctxt'))))] @ vs1 @ vs,
12781267
[Plain (Br (List.assq tagt hs)) @@ e.at]
12791268

1280-
| Handle ((_, hs) as hso, (vs', {it = Suspending (tagt, vs1, Some (ContRef ({contents = Some (n, ctxt)} as cont)), ctxt'); at} :: es')), vs
1269+
| Handle ((_, hs) as hso, (vs', {it = Suspending (tagt, vs1, Some (ar, ContRef ({contents = Some (_, ctxt)} as cont)), ctxt'); at} :: es')), vs
12811270
when List.memq tagt hs ->
12821271
let ctxt'' code = compose (ctxt' code) (vs', es') in
1283-
let cont' = Ref (ContRef (ref (Some (n, ctxt'')))) in
1272+
let cont' = Ref (ContRef (ref (Some (ar, ctxt'')))) in
12841273
let args = cont' :: vs1 in
12851274
cont := None;
12861275
vs' @ vs, [Handle (hso, ctxt (args, [])) @@ e.at]

interpreter/syntax/types.ml

+14
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ let unpacked_storage_type = function
277277

278278
let unpacked_field_type (FieldT (_mut, t)) = unpacked_storage_type t
279279

280+
let as_def_heap_type (ht : heap_type) : def_type =
281+
match ht with
282+
| DefHT def -> def
283+
| _ -> assert false
284+
280285
let as_func_str_type (st : str_type) : func_type =
281286
match st with
282287
| DefFuncT ft -> ft
@@ -297,6 +302,15 @@ let as_array_str_type (st : str_type) : array_type =
297302
| DefArrayT at -> at
298303
| _ -> assert false
299304

305+
let as_cont_func_heap_type (ht : heap_type) : func_type =
306+
let ContT ht' = as_cont_str_type (expand_def_type (as_def_heap_type ht)) in
307+
as_func_str_type (expand_def_type (as_def_heap_type ht'))
308+
309+
let as_cont_func_ref_type (rt : val_type) : func_type =
310+
match rt with
311+
| RefT (_, ht) -> as_cont_func_heap_type ht
312+
| _ -> assert false
313+
300314
let extern_type_of_import_type (ImportT (et, _, _)) = et
301315
let extern_type_of_export_type (ExportT (et, _)) = et
302316

0 commit comments

Comments
 (0)