@@ -1331,8 +1331,8 @@ module Math = struct
13311331 let unary name x =
13321332 let * f =
13331333 register_import
1334- ~allow_tail_call: false
1335- ~import_module: " Math"
1334+ ~allow_tail_call: ( Config.Flag. wasi () )
1335+ ~import_module: ( if Config.Flag. wasi () then " env " else " Math" )
13361336 ~name
13371337 (Fun (float_func_type 1 ))
13381338 in
@@ -1380,8 +1380,8 @@ module Math = struct
13801380 let binary name x y =
13811381 let * f =
13821382 register_import
1383- ~allow_tail_call: false
1384- ~import_module: " Math"
1383+ ~allow_tail_call: ( Config.Flag. wasi () )
1384+ ~import_module: ( if Config.Flag. wasi () then " env " else " Math" )
13851385 ~name
13861386 (Fun (float_func_type 2 ))
13871387 in
@@ -1408,6 +1408,18 @@ module Bigarray = struct
14081408 (Memory. wasm_struct_get ty (Memory. wasm_cast ty a) 3 )
14091409 (Arith. const (Int32. of_int n))
14101410
1411+ let little_endian () =
1412+ if Config.Flag. wasi ()
1413+ then Arith. (const 1l )
1414+ else
1415+ let * le =
1416+ register_import
1417+ ~import_module: " bindings"
1418+ ~name: " littleEndian"
1419+ (Global { mut = false ; typ = I32 })
1420+ in
1421+ return (W. GlobalGet le)
1422+
14111423 let get_at_offset ~(kind : Typing.Bigarray.kind ) a i =
14121424 let name, (typ : Wasm_ast.value_type ), size, box =
14131425 match kind with
@@ -1448,19 +1460,14 @@ module Bigarray = struct
14481460 return (W. F64PromoteF32 x) )
14491461 | Complex64 -> " dv_get_f64" , F64 , 4 , Fun. id
14501462 in
1451- let * little_endian =
1452- register_import
1453- ~import_module: " bindings"
1454- ~name: " littleEndian"
1455- (Global { mut = false ; typ = I32 })
1456- in
1463+ let * little_endian = little_endian () in
14571464 let * f =
14581465 register_import
1459- ~import_module: " bindings"
1466+ ~import_module: ( if Config.Flag. wasi () then " env " else " bindings" )
14601467 ~name
14611468 (Fun
14621469 { W. params =
1463- Ref { nullable = true ; typ = Extern }
1470+ Ref { nullable = not ( Config.Flag. wasi () ) ; typ = Extern }
14641471 :: I32
14651472 :: (if size = 0 then [] else [ I32 ])
14661473 ; result = [ typ ]
@@ -1482,14 +1489,12 @@ module Bigarray = struct
14821489 | Nativeint
14831490 | Float16 ->
14841491 box
1485- (return
1486- (W. Call
1487- (f, ta :: ofs :: (if size = 0 then [] else [ W. GlobalGet little_endian ]))))
1492+ (return (W. Call (f, ta :: ofs :: (if size = 0 then [] else [ little_endian ]))))
14881493 | Complex32 | Complex64 ->
14891494 let delta = Int32. shift_left 1l (size - 1 ) in
14901495 let * ofs' = Arith. (return ofs + const delta) in
1491- let * x = box (return (W. Call (f, [ ta; ofs; W. GlobalGet little_endian ]))) in
1492- let * y = box (return (W. Call (f, [ ta; ofs'; W. GlobalGet little_endian ]))) in
1496+ let * x = box (return (W. Call (f, [ ta; ofs; little_endian ]))) in
1497+ let * y = box (return (W. Call (f, [ ta; ofs'; little_endian ]))) in
14931498 let * ty = Type. float_array_type in
14941499 return (W. ArrayNewFixed (ty, [ x; y ]))
14951500
@@ -1534,19 +1539,14 @@ module Bigarray = struct
15341539 let * ty = Type. bigarray_type in
15351540 let * ta = Memory. wasm_struct_get ty (Memory. wasm_cast ty a) 2 in
15361541 let * ofs = Arith. (i lsl const (Int32. of_int size)) in
1537- let * little_endian =
1538- register_import
1539- ~import_module: " bindings"
1540- ~name: " littleEndian"
1541- (Global { mut = false ; typ = I32 })
1542- in
1542+ let * little_endian = little_endian () in
15431543 let * f =
15441544 register_import
1545- ~import_module: " bindings"
1545+ ~import_module: ( if Config.Flag. wasi () then " env " else " bindings" )
15461546 ~name
15471547 (Fun
15481548 { W. params =
1549- Ref { nullable = true ; typ = Extern }
1549+ Ref { nullable = not ( Config.Flag. wasi () ) ; typ = Extern }
15501550 :: I32
15511551 :: typ
15521552 :: (if size = 0 then [] else [ I32 ])
@@ -1567,18 +1567,15 @@ module Bigarray = struct
15671567 | Float16 ->
15681568 let * v = unbox v in
15691569 instr
1570- (W. CallInstr
1571- ( f
1572- , ta :: ofs :: v :: (if size = 0 then [] else [ W. GlobalGet little_endian ])
1573- ))
1570+ (W. CallInstr (f, ta :: ofs :: v :: (if size = 0 then [] else [ little_endian ])))
15741571 | Complex32 | Complex64 ->
15751572 let delta = Int32. shift_left 1l (size - 1 ) in
15761573 let * ofs' = Arith. (return ofs + const delta) in
15771574 let ty = Type. float_array_type in
15781575 let * x = unbox (Memory. wasm_array_get ~ty v (Arith. const 0l )) in
1579- let * () = instr (W. CallInstr (f, [ ta; ofs; x; W. GlobalGet little_endian ])) in
1576+ let * () = instr (W. CallInstr (f, [ ta; ofs; x; little_endian ])) in
15801577 let * y = unbox (Memory. wasm_array_get ~ty v (Arith. const 1l )) in
1581- instr (W. CallInstr (f, [ ta; ofs'; y; W. GlobalGet little_endian ]))
1578+ instr (W. CallInstr (f, [ ta; ofs'; y; little_endian ]))
15821579
15831580 let offset ~bound_error_index ~(layout : Typing.Bigarray.layout ) ta ~indices =
15841581 let l =
@@ -1951,21 +1948,34 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
19511948 x
19521949 (block_expr
19531950 { params = [] ; result = [ Type. value ] }
1954- (let * exn =
1955- block_expr
1956- { params = [] ; result = [ externref ] }
1957- (let * e =
1958- try_expr
1959- { params = [] ; result = [ externref ] }
1960- (body
1961- ~result_typ: [ externref ]
1962- ~fall_through: `Skip
1963- ~context: (`Skip :: `Skip :: `Catch :: context))
1964- [ ocaml_tag, 1 , Type. value; js_tag, 0 , externref ]
1965- in
1966- instr (W. Push e))
1967- in
1968- instr (W. CallInstr (f, [ exn ]))))
1951+ (if Config.Flag. wasi ()
1952+ then
1953+ let * e =
1954+ try_expr
1955+ { params = [] ; result = [ Type. value ] }
1956+ (body
1957+ ~result_typ: [ Type. value ]
1958+ ~fall_through: `Skip
1959+ ~context: (`Skip :: `Catch :: context))
1960+ [ ocaml_tag, 0 , Type. value ]
1961+ in
1962+ instr (W. Push e)
1963+ else
1964+ let * exn =
1965+ block_expr
1966+ { params = [] ; result = [ externref ] }
1967+ (let * e =
1968+ try_expr
1969+ { params = [] ; result = [ externref ] }
1970+ (body
1971+ ~result_typ: [ externref ]
1972+ ~fall_through: `Skip
1973+ ~context: (`Skip :: `Skip :: `Catch :: context))
1974+ [ ocaml_tag, 1 , Type. value; js_tag, 0 , externref ]
1975+ in
1976+ instr (W. Push e))
1977+ in
1978+ instr (W. CallInstr (f, [ exn ]))))
19691979 in
19701980 let * () = no_event in
19711981 exn_handler ~result_typ ~fall_through ~context )
0 commit comments