Skip to content

Commit f104332

Browse files
committed
WASI runtime
1 parent 28fa15b commit f104332

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+4207
-141
lines changed

compiler/bin-wasm_of_ocaml/cmd_arg.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ let normalize_effects (effects : [ `Cps | `Jspi ] option) common : Config.effect
4444
| None ->
4545
(* For backward compatibility, consider that [--enable effects] alone means
4646
[--effects cps] *)
47-
if List.mem "effects" ~set:common.Jsoo_cmdline.Arg.optim.enable then `Cps else `Jspi
47+
if List.mem "effects" ~set:common.Jsoo_cmdline.Arg.optim.enable
48+
then `Cps
49+
else if List.mem "wasi" ~set:common.Jsoo_cmdline.Arg.optim.enable
50+
then `Disabled
51+
else `Jspi
4852
| Some ((`Cps | `Jspi) as e) -> e
4953

5054
type t =

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ let build_runtime ~runtime_file =
9292
| `Jspi -> "jspi"
9393
| `Cps -> "cps"
9494
| `Disabled | `Double_translation -> assert false) )
95+
; "wasi", Wat_preprocess.Bool (Config.Flag.wasi ())
9596
]
9697
in
9798
match
@@ -110,7 +111,9 @@ let build_runtime ~runtime_file =
110111
; file = module_name ^ ".wat"
111112
; source = Contents contents
112113
})
113-
Runtime_files.wat_files
114+
(if Config.Flag.wasi ()
115+
then ("libc", Runtime_files.wasi_libc) :: Runtime_files.wat_files
116+
else Runtime_files.wat_files)
114117
in
115118
Runtime.build
116119
~link_options:[ "-g" ]
@@ -163,7 +166,10 @@ let link_and_optimize
163166
@@ fun opt_temp_sourcemap' ->
164167
let primitives =
165168
Binaryen.dead_code_elimination
166-
~dependencies:Runtime_files.dependencies
169+
~dependencies:
170+
(if Config.Flag.wasi ()
171+
then Runtime_files.wasi_dependencies
172+
else Runtime_files.dependencies)
167173
~opt_input_sourcemap:opt_temp_sourcemap
168174
~opt_output_sourcemap:opt_temp_sourcemap'
169175
~input_file:temp_file
@@ -283,7 +289,13 @@ let build_js_runtime ~primitives ?runtime_arguments () =
283289
| _ -> assert false
284290
in
285291
let init_fun =
286-
match Parse_js.parse (Parse_js.Lexer.of_string Runtime_files.js_runtime) with
292+
match
293+
Parse_js.parse
294+
(Parse_js.Lexer.of_string
295+
(if Config.Flag.wasi ()
296+
then Runtime_files.js_wasi_launcher
297+
else Runtime_files.js_launcher))
298+
with
287299
| [ (Expression_statement f, _) ] -> f
288300
| _ -> assert false
289301
in
@@ -522,9 +534,12 @@ let run
522534
tmp_wasm_file
523535
in
524536
let wasm_name =
525-
Printf.sprintf
526-
"code-%s"
527-
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
537+
if Config.Flag.wasi ()
538+
then "code"
539+
else
540+
Printf.sprintf
541+
"code-%s"
542+
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
528543
in
529544
let tmp_wasm_file' = Filename.concat tmp_dir (wasm_name ^ ".wasm") in
530545
Sys.rename tmp_wasm_file tmp_wasm_file';

compiler/bin-wasm_of_ocaml/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
gen/gen.exe
2828
../../runtime/wasm/runtime.js
2929
../../runtime/wasm/deps.json
30+
../../runtime/wasm/runtime-wasi.js
31+
../../runtime/wasm/deps-wasi.json
32+
../../runtime/wasm/libc.wasm
3033
(glob_files ../../runtime/wasm/*.wat)
3134
(glob_files ../../runtime/wasm/runtime-*.wasm))
3235
(action

compiler/bin-wasm_of_ocaml/gen/gen.ml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ let read_file ic = really_input_string ic (in_channel_length ic)
55

66
let default_flags = []
77

8-
let interesting_runtimes = [ [ "effects", `S "jspi" ]; [ "effects", `S "cps" ] ]
8+
let interesting_runtimes =
9+
[ [ "effects", `S "jspi"; "wasi", `B false ]
10+
; [ "effects", `S "cps"; "wasi", `B false ]
11+
; [ "effects", `S "disabled"; "wasi", `B true ]
12+
; [ "effects", `S "cps"; "wasi", `B true ]
13+
]
14+
15+
let defaults = [ "effects", "disabled" ]
916

1017
let name_runtime standard l =
1118
let flags =
1219
List.filter_map
1320
(fun (k, v) ->
1421
match v with
15-
| `S s -> Some s
22+
| `S s -> if List.mem (k, s) defaults then None else Some s
1623
| `B b -> if b then Some k else None)
1724
l
1825
in
@@ -41,15 +48,24 @@ let () =
4148
let () = set_binary_mode_out stdout true in
4249
Format.printf "open Wasm_of_ocaml_compiler@.";
4350
Format.printf
44-
"let js_runtime = \"%s\"@."
51+
"let js_launcher = \"%s\"@."
4552
(String.escaped (read_file (open_in_bin Sys.argv.(1))));
4653
Format.printf
4754
"let dependencies = \"%s\"@."
4855
(String.escaped (read_file (open_in_bin Sys.argv.(2))));
56+
Format.printf
57+
"let js_wasi_launcher = \"%s\"@."
58+
(String.escaped (read_file (open_in_bin Sys.argv.(3))));
59+
Format.printf
60+
"let wasi_dependencies = \"%s\"@."
61+
(String.escaped (read_file (open_in_bin Sys.argv.(4))));
62+
Format.printf
63+
"let wasi_libc = \"%s\"@."
64+
(String.escaped (read_file (open_in_bin Sys.argv.(5))));
4965
let wat_files, runtimes =
5066
List.partition
5167
(fun f -> Filename.check_suffix f ".wat")
52-
(Array.to_list (Array.sub Sys.argv 3 (Array.length Sys.argv - 3)))
68+
(Array.to_list (Array.sub Sys.argv 6 (Array.length Sys.argv - 6)))
5369
in
5470
Format.printf
5571
"let wat_files = [%a]@."

compiler/lib-wasm/binaryen.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let common_options () =
3636
; "--enable-bulk-memory"
3737
; "--enable-nontrapping-float-to-int"
3838
; "--enable-strings"
39+
; "--enable-multimemory" (* To keep wasm-merge happy *)
3940
]
4041
in
4142
if Config.Flag.pretty () then "-g" :: l else l

compiler/lib-wasm/gc_target.ml

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,13 @@ module Value = struct
574574
return ())
575575
(val_int (if negate then Arith.eqz n else n))
576576

577-
let eq x y = eq_gen ~negate:false x y
577+
let eq x y =
578+
if Config.Flag.wasi () then val_int (ref_eq x y) else eq_gen ~negate:false x y
578579

579-
let neq x y = eq_gen ~negate:true x y
580+
let neq x y =
581+
if Config.Flag.wasi ()
582+
then val_int (Arith.eqz (ref_eq x y))
583+
else eq_gen ~negate:true x y
580584

581585
let ult = binop Arith.(ult)
582586

@@ -1299,7 +1303,12 @@ module Math = struct
12991303
{ W.params = List.init ~len:n ~f:(fun _ : W.value_type -> F64); result = [ F64 ] }
13001304

13011305
let unary name x =
1302-
let* f = register_import ~import_module:"Math" ~name (Fun (float_func_type 1)) in
1306+
let* f =
1307+
register_import
1308+
~import_module:(if Config.Flag.wasi () then "env" else "Math")
1309+
~name
1310+
(Fun (float_func_type 1))
1311+
in
13031312
let* x = x in
13041313
return (W.Call (f, [ x ]))
13051314

@@ -1342,7 +1351,12 @@ module Math = struct
13421351
let log10 f = unary "log10" f
13431352

13441353
let binary name x y =
1345-
let* f = register_import ~import_module:"Math" ~name (Fun (float_func_type 2)) in
1354+
let* f =
1355+
register_import
1356+
~import_module:(if Config.Flag.wasi () then "env" else "Math")
1357+
~name
1358+
(Fun (float_func_type 2))
1359+
in
13461360
let* x = x in
13471361
let* y = y in
13481362
return (W.Call (f, [ x; y ]))
@@ -1681,21 +1695,34 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
16811695
x
16821696
(block_expr
16831697
{ params = []; result = [ Value.value ] }
1684-
(let* exn =
1685-
block_expr
1686-
{ params = []; result = [ externref ] }
1687-
(let* e =
1688-
try_expr
1689-
{ params = []; result = [ externref ] }
1690-
(body
1691-
~result_typ:[ externref ]
1692-
~fall_through:`Skip
1693-
~context:(`Skip :: `Skip :: `Catch :: context))
1694-
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1695-
in
1696-
instr (W.Push e))
1697-
in
1698-
instr (W.CallInstr (f, [ exn ]))))
1698+
(if Config.Flag.wasi ()
1699+
then
1700+
let* e =
1701+
try_expr
1702+
{ params = []; result = [ Value.value ] }
1703+
(body
1704+
~result_typ:[ Value.value ]
1705+
~fall_through:`Skip
1706+
~context:(`Skip :: `Catch :: context))
1707+
[ ocaml_tag, 0, Value.value ]
1708+
in
1709+
instr (W.Push e)
1710+
else
1711+
let* exn =
1712+
block_expr
1713+
{ params = []; result = [ externref ] }
1714+
(let* e =
1715+
try_expr
1716+
{ params = []; result = [ externref ] }
1717+
(body
1718+
~result_typ:[ externref ]
1719+
~fall_through:`Skip
1720+
~context:(`Skip :: `Skip :: `Catch :: context))
1721+
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1722+
in
1723+
instr (W.Push e))
1724+
in
1725+
instr (W.CallInstr (f, [ exn ]))))
16991726
in
17001727
let* () = no_event in
17011728
exn_handler ~result_typ ~fall_through ~context)

compiler/lib/config.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ module Flag = struct
101101
let auto_link = o ~name:"auto-link" ~default:true
102102

103103
let es6 = o ~name:"es6" ~default:false
104+
105+
let wasi = o ~name:"wasi" ~default:false
104106
end
105107

106108
module Param = struct

compiler/lib/config.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ module Flag : sig
7676

7777
val es6 : unit -> bool
7878

79+
val wasi : unit -> bool
80+
7981
val enable : string -> unit
8082

8183
val disable : string -> unit

compiler/lib/inline.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ let inline ~first_class_primitives live_vars closures name pc (outer, p) =
328328

329329
let times = Debug.find "times"
330330

331-
let f p live_vars =
331+
let f p (live_vars : Deadcode.variable_uses) =
332332
let first_class_primitives =
333333
match Config.target (), Config.effects () with
334334
| `JavaScript, `Disabled -> true

compiler/tests-jsoo/dune

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
(enabled_if
1212
(>= %{ocaml_version} 4.14))
1313
(inline_tests
14+
(deps
15+
(sandbox preserve_file_kind))
1416
(modes js wasm best))
1517
(preprocess
1618
(pps ppx_expect)))
@@ -22,6 +24,8 @@
2224
(enabled_if
2325
(>= %{ocaml_version} 5.1.1))
2426
(inline_tests
27+
(deps
28+
(sandbox preserve_file_kind))
2529
(modes js wasm best))
2630
(preprocess
2731
(pps ppx_expect)))
@@ -33,6 +37,22 @@
3337
(enabled_if
3438
(>= %{ocaml_version} 5.1.1))
3539
(inline_tests
40+
(deps
41+
(sandbox preserve_file_kind))
42+
(modes js wasm best))
43+
(preprocess
44+
(pps ppx_expect)))
45+
46+
(library
47+
(name jsoo_testsuite_perms)
48+
(modules test_unix_perms)
49+
(libraries unix)
50+
;; WASI has no notion of file permissions (it uses capabilities instead)
51+
(enabled_if
52+
(<> %{profile} wasi))
53+
(inline_tests
54+
(deps
55+
(sandbox preserve_file_kind))
3656
(modes js wasm best))
3757
(preprocess
3858
(pps ppx_expect)))
@@ -47,13 +67,16 @@
4767
test_float16
4868
test_marshal_compressed
4969
test_parsing
70+
test_unix_perms
5071
calc_parser
5172
calc_lexer))
5273
(libraries unix compiler-libs.common js_of_ocaml-compiler)
5374
(foreign_stubs
5475
(language c)
5576
(names bigarray_stubs jsoo_runtime_stubs))
5677
(inline_tests
78+
(deps
79+
(sandbox preserve_file_kind))
5780
(modes js wasm best))
5881
(preprocess
5982
(pps ppx_expect)))

0 commit comments

Comments
 (0)