Skip to content

Commit 76654e2

Browse files
rossbergbackes
authored andcommitted
[interpreter] Fix (and sanity-check) JS wrapper modules
1 parent 8ce0607 commit 76654e2

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

.github/workflows/ci-interpreter.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: CI for interpreter & tests
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [ main, wasm-3.0 ]
66
paths: [ .github/**, interpreter/**, test/** ]
77

88
pull_request:
9-
branches: [ main ]
9+
branches: [ main, wasm-3.0 ]
1010
paths: [ .github/**, interpreter/**, test/** ]
1111

1212
# Allows you to run this workflow manually from the Actions tab

interpreter/script/js.ml

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,15 @@ let value v =
309309
| Num n -> [Const (n @@ v.at) @@ v.at]
310310
| Vec s -> [VecConst (s @@ v.at) @@ v.at]
311311
| Ref (NullRef ht) -> [RefNull (Match.bot_of_heap_type [] ht) @@ v.at]
312+
| Ref (HostRef n) ->
313+
[ Const (I32 n @@ v.at) @@ v.at;
314+
Call (hostref_idx @@ v.at) @@ v.at;
315+
]
312316
| Ref (Extern.ExternRef (HostRef n)) ->
313-
[Const (I32 n @@ v.at) @@ v.at; Call (hostref_idx @@ v.at) @@ v.at]
317+
[ Const (I32 n @@ v.at) @@ v.at;
318+
Call (hostref_idx @@ v.at) @@ v.at;
319+
ExternConvert Externalize @@ v.at;
320+
]
314321
| Ref _ -> assert false
315322

316323
let invoke ft vs at =
@@ -357,6 +364,7 @@ let rec type_of_result res =
357364
) (List.hd ts) ts
358365

359366
let assert_return ress ts at =
367+
let locals = ref [] in
360368
let rec test (res, t) =
361369
if
362370
not (
@@ -436,10 +444,17 @@ let assert_return ress ts at =
436444
[ RefIsNull @@ at;
437445
Test (Value.I32 I32Op.Eqz) @@ at;
438446
BrIf (0l @@ at) @@ at ]
439-
| RefResult (RefPat {it = (HostRef n | Extern.ExternRef (HostRef n)); _}) ->
447+
| RefResult (RefPat {it = HostRef n; _}) ->
448+
[ Const (Value.I32 n @@ at) @@ at;
449+
Call (hostref_idx @@ at) @@ at;
450+
Call (eq_ref_idx @@ at) @@ at;
451+
Test (Value.I32 I32Op.Eqz) @@ at;
452+
BrIf (0l @@ at) @@ at ]
453+
| RefResult (RefPat {it = Extern.ExternRef (HostRef n); _}) ->
440454
[ Const (Value.I32 n @@ at) @@ at;
441455
Call (hostref_idx @@ at) @@ at;
442-
Call (eq_ref_idx @@ at) @@ at;
456+
ExternConvert Externalize @@ at;
457+
Call (eq_ref_idx @@ at) @@ at;
443458
Test (Value.I32 I32Op.Eqz) @@ at;
444459
BrIf (0l @@ at) @@ at ]
445460
| RefResult (RefPat _) ->
@@ -455,17 +470,21 @@ let assert_return ress ts at =
455470
Test (I32 I32Op.Eqz) @@ at;
456471
BrIf (0l @@ at) @@ at ]
457472
| EitherResult ress ->
458-
[ Block (ValBlockType None,
473+
let idx = Lib.List32.length !locals in
474+
locals := !locals @ [{ltype = t} @@ res.at];
475+
[ LocalSet (idx @@ res.at) @@ res.at;
476+
Block (ValBlockType None,
459477
List.map (fun resI ->
460478
Block (ValBlockType None,
479+
[LocalGet (idx @@ resI.at) @@ resI.at] @
461480
test (resI, t) @
462481
[Br (1l @@ resI.at) @@ resI.at]
463482
) @@ resI.at
464483
) ress @
465484
[Br (1l @@ at) @@ at]
466485
) @@ at
467486
]
468-
in [], List.flatten (List.rev_map test (List.combine ress ts))
487+
in !locals, List.flatten (List.rev_map test (List.combine ress ts))
469488

470489
let i32 = NumT I32T
471490
let anyref = RefT (Null, AnyHT)
@@ -504,12 +523,20 @@ let wrap item_name wrap_action wrap_assertion at =
504523
in
505524
let funcs = [{ftype = 0l @@ at; locals; body} @@ at] in
506525
let m = {empty_module with types; funcs; imports; exports} @@ at in
526+
(try
527+
Valid.check_module m; (* sanity check *)
528+
with Valid.Invalid _ as exn ->
529+
prerr_endline (string_of_region at ^
530+
": internal error in JS converter, invalid wrapper module generated:");
531+
Sexpr.output stderr 80 (Arrange.module_ m);
532+
raise exn
533+
);
507534
Encode.encode m
508535

509536

510537
let is_js_num_type = function
511-
| I32T -> true
512-
| I64T | F32T | F64T -> false
538+
| I32T | I64T -> true
539+
| F32T | F64T -> false
513540

514541
let is_js_vec_type = function
515542
| _ -> false
@@ -569,7 +596,7 @@ let of_num n =
569596
let open Value in
570597
match n with
571598
| I32 i -> I32.to_string_s i
572-
| I64 i -> "int64(\"" ^ I64.to_string_s i ^ "\")"
599+
| I64 i -> I64.to_string_s i ^ "n"
573600
| F32 z -> of_float (F32.to_float z)
574601
| F64 z -> of_float (F64.to_float z)
575602

0 commit comments

Comments
 (0)