@@ -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
@@ -1413,6 +1413,18 @@ module Bigarray = struct
14131413 (Memory. wasm_struct_get ty (Memory. wasm_cast ty a) 3 )
14141414 (Arith. const (Int32. of_int n))
14151415
1416+ let little_endian () =
1417+ if Config.Flag. wasi ()
1418+ then Arith. (const 1l )
1419+ else
1420+ let * le =
1421+ register_import
1422+ ~import_module: " bindings"
1423+ ~name: " littleEndian"
1424+ (Global { mut = false ; typ = I32 })
1425+ in
1426+ return (W. GlobalGet le)
1427+
14161428 let get_at_offset ~(kind : Typing.Bigarray.kind ) a i =
14171429 let name, (typ : Wasm_ast.value_type ), size, box =
14181430 match kind with
@@ -1453,19 +1465,14 @@ module Bigarray = struct
14531465 return (W. F64PromoteF32 x) )
14541466 | Complex64 -> " dv_get_f64" , F64 , 4 , Fun. id
14551467 in
1456- let * little_endian =
1457- register_import
1458- ~import_module: " bindings"
1459- ~name: " littleEndian"
1460- (Global { mut = false ; typ = I32 })
1461- in
1468+ let * little_endian = little_endian () in
14621469 let * f =
14631470 register_import
1464- ~import_module: " bindings"
1471+ ~import_module: ( if Config.Flag. wasi () then " env " else " bindings" )
14651472 ~name
14661473 (Fun
14671474 { W. params =
1468- Ref { nullable = true ; typ = Extern }
1475+ Ref { nullable = not ( Config.Flag. wasi () ) ; typ = Extern }
14691476 :: I32
14701477 :: (if size = 0 then [] else [ I32 ])
14711478 ; result = [ typ ]
@@ -1487,14 +1494,12 @@ module Bigarray = struct
14871494 | Nativeint
14881495 | Float16 ->
14891496 box
1490- (return
1491- (W. Call
1492- (f, ta :: ofs :: (if size = 0 then [] else [ W. GlobalGet little_endian ]))))
1497+ (return (W. Call (f, ta :: ofs :: (if size = 0 then [] else [ little_endian ]))))
14931498 | Complex32 | Complex64 ->
14941499 let delta = Int32. shift_left 1l (size - 1 ) in
14951500 let * ofs' = Arith. (return ofs + const delta) in
1496- let * x = box (return (W. Call (f, [ ta; ofs; W. GlobalGet little_endian ]))) in
1497- let * y = box (return (W. Call (f, [ ta; ofs'; W. GlobalGet little_endian ]))) in
1501+ let * x = box (return (W. Call (f, [ ta; ofs; little_endian ]))) in
1502+ let * y = box (return (W. Call (f, [ ta; ofs'; little_endian ]))) in
14981503 let * ty = Type. float_array_type in
14991504 return (W. ArrayNewFixed (ty, [ x; y ]))
15001505
@@ -1539,19 +1544,14 @@ module Bigarray = struct
15391544 let * ty = Type. bigarray_type in
15401545 let * ta = Memory. wasm_struct_get ty (Memory. wasm_cast ty a) 2 in
15411546 let * ofs = Arith. (i lsl const (Int32. of_int size)) in
1542- let * little_endian =
1543- register_import
1544- ~import_module: " bindings"
1545- ~name: " littleEndian"
1546- (Global { mut = false ; typ = I32 })
1547- in
1547+ let * little_endian = little_endian () in
15481548 let * f =
15491549 register_import
1550- ~import_module: " bindings"
1550+ ~import_module: ( if Config.Flag. wasi () then " env " else " bindings" )
15511551 ~name
15521552 (Fun
15531553 { W. params =
1554- Ref { nullable = true ; typ = Extern }
1554+ Ref { nullable = not ( Config.Flag. wasi () ) ; typ = Extern }
15551555 :: I32
15561556 :: typ
15571557 :: (if size = 0 then [] else [ I32 ])
@@ -1572,18 +1572,15 @@ module Bigarray = struct
15721572 | Float16 ->
15731573 let * v = unbox v in
15741574 instr
1575- (W. CallInstr
1576- ( f
1577- , ta :: ofs :: v :: (if size = 0 then [] else [ W. GlobalGet little_endian ])
1578- ))
1575+ (W. CallInstr (f, ta :: ofs :: v :: (if size = 0 then [] else [ little_endian ])))
15791576 | Complex32 | Complex64 ->
15801577 let delta = Int32. shift_left 1l (size - 1 ) in
15811578 let * ofs' = Arith. (return ofs + const delta) in
15821579 let ty = Type. float_array_type in
15831580 let * x = unbox (Memory. wasm_array_get ~ty v (Arith. const 0l )) in
1584- let * () = instr (W. CallInstr (f, [ ta; ofs; x; W. GlobalGet little_endian ])) in
1581+ let * () = instr (W. CallInstr (f, [ ta; ofs; x; little_endian ])) in
15851582 let * y = unbox (Memory. wasm_array_get ~ty v (Arith. const 1l )) in
1586- instr (W. CallInstr (f, [ ta; ofs'; y; W. GlobalGet little_endian ]))
1583+ instr (W. CallInstr (f, [ ta; ofs'; y; little_endian ]))
15871584
15881585 let offset ~bound_error_index ~(layout : Typing.Bigarray.layout ) ta ~indices =
15891586 let l =
@@ -1956,21 +1953,34 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
19561953 x
19571954 (block_expr
19581955 { params = [] ; result = [ Type. value ] }
1959- (let * exn =
1960- block_expr
1961- { params = [] ; result = [ externref ] }
1962- (let * e =
1963- try_expr
1964- { params = [] ; result = [ externref ] }
1965- (body
1966- ~result_typ: [ externref ]
1967- ~fall_through: `Skip
1968- ~context: (`Skip :: `Skip :: `Catch :: context))
1969- [ ocaml_tag, 1 , Type. value; js_tag, 0 , externref ]
1970- in
1971- instr (W. Push e))
1972- in
1973- instr (W. CallInstr (f, [ exn ]))))
1956+ (if Config.Flag. wasi ()
1957+ then
1958+ let * e =
1959+ try_expr
1960+ { params = [] ; result = [ Type. value ] }
1961+ (body
1962+ ~result_typ: [ Type. value ]
1963+ ~fall_through: `Skip
1964+ ~context: (`Skip :: `Catch :: context))
1965+ [ ocaml_tag, 0 , Type. value ]
1966+ in
1967+ instr (W. Push e)
1968+ else
1969+ let * exn =
1970+ block_expr
1971+ { params = [] ; result = [ externref ] }
1972+ (let * e =
1973+ try_expr
1974+ { params = [] ; result = [ externref ] }
1975+ (body
1976+ ~result_typ: [ externref ]
1977+ ~fall_through: `Skip
1978+ ~context: (`Skip :: `Skip :: `Catch :: context))
1979+ [ ocaml_tag, 1 , Type. value; js_tag, 0 , externref ]
1980+ in
1981+ instr (W. Push e))
1982+ in
1983+ instr (W. CallInstr (f, [ exn ]))))
19741984 in
19751985 let * () = no_event in
19761986 exn_handler ~result_typ ~fall_through ~context )
0 commit comments