@@ -312,8 +312,15 @@ let value v =
312
312
| Num n -> [Const (n @@ v.at) @@ v.at]
313
313
| Vec s -> [VecConst (s @@ v.at) @@ v.at]
314
314
| Ref (NullRef ht ) -> [RefNull (Match. bot_of_heap_type [] ht) @@ v.at]
315
+ | Ref (HostRef n ) ->
316
+ [ Const (I32 n @@ v.at) @@ v.at;
317
+ Call (hostref_idx @@ v.at) @@ v.at;
318
+ ]
315
319
| Ref (Extern. ExternRef (HostRef n )) ->
316
- [Const (I32 n @@ v.at) @@ v.at; Call (hostref_idx @@ v.at) @@ v.at]
320
+ [ Const (I32 n @@ v.at) @@ v.at;
321
+ Call (hostref_idx @@ v.at) @@ v.at;
322
+ ExternConvert Externalize @@ v.at;
323
+ ]
317
324
| Ref _ -> assert false
318
325
319
326
let invoke ft vs at =
@@ -360,8 +367,14 @@ let rec type_of_result res =
360
367
) (List. hd ts) ts
361
368
362
369
let assert_return ress ts at =
370
+ let locals = ref [] in
363
371
let rec test (res , t ) =
364
- if not (Match. match_val_type [] t (type_of_result res)) then
372
+ if
373
+ not (
374
+ Match. match_val_type [] t (type_of_result res) ||
375
+ Match. match_val_type [] (type_of_result res) t
376
+ )
377
+ then
365
378
[ Br (0l @@ at) @@ at ]
366
379
else
367
380
match res.it with
@@ -437,7 +450,14 @@ let assert_return ress ts at =
437
450
| RefResult (RefPat {it = HostRef n ; _} ) ->
438
451
[ Const (Value. I32 n @@ at) @@ at;
439
452
Call (hostref_idx @@ at) @@ at;
440
- Call (eq_ref_idx @@ at) @@ at;
453
+ Call (eq_ref_idx @@ at) @@ at;
454
+ Test (Value. I32 I32Op. Eqz ) @@ at;
455
+ BrIf (0l @@ at) @@ at ]
456
+ | RefResult (RefPat {it = Extern. ExternRef (HostRef n ); _} ) ->
457
+ [ Const (Value. I32 n @@ at) @@ at;
458
+ Call (hostref_idx @@ at) @@ at;
459
+ ExternConvert Externalize @@ at;
460
+ Call (eq_ref_idx @@ at) @@ at;
441
461
Test (Value. I32 I32Op. Eqz ) @@ at;
442
462
BrIf (0l @@ at) @@ at ]
443
463
| RefResult (RefPat _ ) ->
@@ -453,17 +473,21 @@ let assert_return ress ts at =
453
473
Test (I32 I32Op. Eqz ) @@ at;
454
474
BrIf (0l @@ at) @@ at ]
455
475
| EitherResult ress ->
456
- [ Block (ValBlockType None ,
476
+ let idx = Lib.List32. length ! locals in
477
+ locals := ! locals @ [{ltype = t} @@ res.at];
478
+ [ LocalSet (idx @@ res.at) @@ res.at;
479
+ Block (ValBlockType None ,
457
480
List. map (fun resI ->
458
481
Block (ValBlockType None ,
482
+ [LocalGet (idx @@ resI.at) @@ resI.at] @
459
483
test (resI, t) @
460
484
[Br (1l @@ resI.at) @@ resI.at]
461
485
) @@ resI.at
462
486
) ress @
463
487
[Br (1l @@ at) @@ at]
464
488
) @@ at
465
489
]
466
- in [] , List. flatten (List. rev_map test (List. combine ress ts))
490
+ in ! locals , List. flatten (List. rev_map test (List. combine ress ts))
467
491
468
492
let i32 = NumT I32T
469
493
let anyref = RefT (Null , AnyHT )
@@ -502,12 +526,20 @@ let wrap item_name wrap_action wrap_assertion at =
502
526
in
503
527
let funcs = [{ftype = 0l @@ at; locals; body} @@ at] in
504
528
let m = {empty_module with types; funcs; imports; exports} @@ at in
529
+ (try
530
+ Valid. check_module m; (* sanity check *)
531
+ with Valid. Invalid _ as exn ->
532
+ prerr_endline (string_of_region at ^
533
+ " : internal error in JS converter, invalid wrapper module generated:" );
534
+ Sexpr. output stderr 80 (Arrange. module_ m);
535
+ raise exn
536
+ );
505
537
Encode. encode m
506
538
507
539
508
540
let is_js_num_type = function
509
- | I32T -> true
510
- | I64T | F32T | F64T -> false
541
+ | I32T | I64T -> true
542
+ | F32T | F64T -> false
511
543
512
544
let is_js_vec_type = function
513
545
| _ -> false
@@ -567,7 +599,7 @@ let of_num n =
567
599
let open Value in
568
600
match n with
569
601
| I32 i -> I32. to_string_s i
570
- | I64 i -> " int64( \" " ^ I64. to_string_s i ^ " \" ) "
602
+ | I64 i -> I64. to_string_s i ^ " n "
571
603
| F32 z -> of_float (F32. to_float z)
572
604
| F64 z -> of_float (F64. to_float z)
573
605
0 commit comments