Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 44e8abd

Browse files
committed
[interpreter] adjust try_delegate semantics
Accounting for the discussion in #176 adjust try_delegate so that it can target any block.
1 parent ebe2da9 commit 44e8abd

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

interpreter/valid/valid.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let require b at s = if not b then error at s
1414

1515
(* Context *)
1616

17-
type label_kind = BodyLabel | BlockLabel | TryLabel | CatchLabel
17+
type label_kind = BlockLabel | CatchLabel
1818

1919
type context =
2020
{
@@ -409,7 +409,7 @@ let rec check_instr (c : context) (e : instr) (s : infer_stack_type) : op_type =
409409

410410
| TryCatch (bt, es, cts, ca) ->
411411
let FuncType (ts1, ts2) as ft = check_block_type c bt in
412-
let c_try = {c with labels = (TryLabel, ts2) :: c.labels} in
412+
let c_try = {c with labels = (BlockLabel, ts2) :: c.labels} in
413413
let c_catch = {c with labels = (CatchLabel, ts2) :: c.labels} in
414414
check_block c_try es ft e.at;
415415
List.iter (fun ct -> check_catch ct c_catch ft e.at) cts;
@@ -419,8 +419,8 @@ let rec check_instr (c : context) (e : instr) (s : infer_stack_type) : op_type =
419419
| TryDelegate (bt, es, x) ->
420420
let FuncType (ts1, ts2) as ft = check_block_type c bt in
421421
let (kind, _) = label c x in
422-
require (kind = TryLabel || kind = BodyLabel) e.at "invalid delegate label";
423-
check_block {c with labels = (TryLabel, ts2) :: c.labels} es ft e.at;
422+
require (kind = BlockLabel) e.at "invalid delegate label";
423+
check_block {c with labels = (BlockLabel, ts2) :: c.labels} es ft e.at;
424424
ts1 --> ts2
425425

426426
| Throw x ->
@@ -524,7 +524,7 @@ let check_type (t : type_) =
524524
let check_func (c : context) (f : func) =
525525
let {ftype; locals; body} = f.it in
526526
let FuncType (ts1, ts2) = type_ c ftype in
527-
let c' = {c with locals = ts1 @ locals; results = ts2; labels = [(BodyLabel, ts2)]} in
527+
let c' = {c with locals = ts1 @ locals; results = ts2; labels = [(BlockLabel, ts2)]} in
528528
check_block c' body (FuncType ([], ts2)) f.at
529529

530530
let check_tag (c : context) (t : tag) =

test/core/try_delegate.wast

+9-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
)
4646
)
4747

48+
(func (export "delegate-to-block") (result i32)
49+
(try (result i32)
50+
(do (block (try (do (throw $e0)) (delegate 0)))
51+
(i32.const 0))
52+
(catch_all (i32.const 1)))
53+
)
54+
4855
(func (export "delegate-to-caller")
4956
(try (do (try (do (throw $e0)) (delegate 1))) (catch_all))
5057
)
@@ -92,6 +99,8 @@
9299

93100
(assert_return (invoke "delegate-skip") (i32.const 3))
94101

102+
(assert_return (invoke "delegate-to-block") (i32.const 1))
103+
95104
(assert_exception (invoke "delegate-to-caller"))
96105

97106
(assert_malformed
@@ -118,8 +127,3 @@
118127
(module (func (try (do) (delegate 1))))
119128
"unknown label"
120129
)
121-
122-
(assert_invalid
123-
(module (func (block (try (do) (delegate 0)))))
124-
"invalid delegate label"
125-
)

0 commit comments

Comments
 (0)