Skip to content

Commit 7a43909

Browse files
committed
WASI runtime
1 parent c4559c2 commit 7a43909

Some content is hidden

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

51 files changed

+4343
-318
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ let with_runtime_files ~runtime_wasm_files f =
8383

8484
let build_runtime ~runtime_file =
8585
(* Keep this variables in sync with gen/gen.ml *)
86-
let variables = [] in
86+
let variables = [ "wasi", Config.Flag.wasi () ] in
8787
match
8888
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
8989
assert (
@@ -100,7 +100,9 @@ let build_runtime ~runtime_file =
100100
; file = module_name ^ ".wat"
101101
; source = Contents contents
102102
})
103-
Runtime_files.wat_files
103+
(if Config.Flag.wasi ()
104+
then ("libc", Runtime_files.wasi_libc) :: Runtime_files.wat_files
105+
else Runtime_files.wat_files)
104106
in
105107
Runtime.build
106108
~link_options:[ "-g" ]
@@ -154,7 +156,10 @@ let link_and_optimize
154156
@@ fun opt_temp_sourcemap' ->
155157
let primitives =
156158
Binaryen.dead_code_elimination
157-
~dependencies:Runtime_files.dependencies
159+
~dependencies:
160+
(if Config.Flag.wasi ()
161+
then Runtime_files.wasi_dependencies
162+
else Runtime_files.dependencies)
158163
~opt_input_sourcemap:opt_temp_sourcemap
159164
~opt_output_sourcemap:opt_temp_sourcemap'
160165
~input_file:temp_file
@@ -275,7 +280,13 @@ let build_js_runtime ~primitives ?runtime_arguments () =
275280
in
276281
let prelude = Link.output_js always_required_js in
277282
let init_fun =
278-
match Parse_js.parse (Parse_js.Lexer.of_string Runtime_files.js_runtime) with
283+
match
284+
Parse_js.parse
285+
(Parse_js.Lexer.of_string
286+
(if Config.Flag.wasi ()
287+
then Runtime_files.js_wasi_launcher
288+
else Runtime_files.js_launcher))
289+
with
279290
| [ (Expression_statement f, _) ] -> f
280291
| _ -> assert false
281292
in
@@ -516,9 +527,12 @@ let run
516527
tmp_wasm_file
517528
in
518529
let wasm_name =
519-
Printf.sprintf
520-
"code-%s"
521-
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
530+
if Config.Flag.wasi ()
531+
then "code"
532+
else
533+
Printf.sprintf
534+
"code-%s"
535+
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
522536
in
523537
let tmp_wasm_file' = Filename.concat tmp_dir (wasm_name ^ ".wasm") in
524538
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let read_file ic = really_input_string ic (in_channel_length ic)
55

66
let default_flags = []
77

8-
let interesting_runtimes = [ [] ]
8+
let interesting_runtimes = [ [ "wasi", false ]; [ "wasi", true ] ]
99

1010
let name_runtime l =
1111
let flags = List.filter_map (fun (k, v) -> if v then Some k else None) l in
@@ -25,15 +25,24 @@ let print_flags f flags =
2525
let () =
2626
let () = set_binary_mode_out stdout true in
2727
Format.printf
28-
"let js_runtime = \"%s\"@."
28+
"let js_launcher = \"%s\"@."
2929
(String.escaped (read_file (open_in_bin Sys.argv.(1))));
3030
Format.printf
3131
"let dependencies = \"%s\"@."
3232
(String.escaped (read_file (open_in_bin Sys.argv.(2))));
33+
Format.printf
34+
"let js_wasi_launcher = \"%s\"@."
35+
(String.escaped (read_file (open_in_bin Sys.argv.(3))));
36+
Format.printf
37+
"let wasi_dependencies = \"%s\"@."
38+
(String.escaped (read_file (open_in_bin Sys.argv.(4))));
39+
Format.printf
40+
"let wasi_libc = \"%s\"@."
41+
(String.escaped (read_file (open_in_bin Sys.argv.(5))));
3342
let wat_files, runtimes =
3443
List.partition
3544
(fun f -> Filename.check_suffix f ".wat")
36-
(Array.to_list (Array.sub Sys.argv 3 (Array.length Sys.argv - 3)))
45+
(Array.to_list (Array.sub Sys.argv 6 (Array.length Sys.argv - 6)))
3746
in
3847
Format.printf
3948
"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
@@ -573,9 +573,13 @@ module Value = struct
573573
return ())
574574
(val_int (if negate then Arith.eqz n else n))
575575

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

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

580584
let ult = binop Arith.(ult)
581585

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

12961300
let unary name x =
1297-
let* f = register_import ~import_module:"Math" ~name (Fun (float_func_type 1)) in
1301+
let* f =
1302+
register_import
1303+
~import_module:(if Config.Flag.wasi () then "env" else "Math")
1304+
~name
1305+
(Fun (float_func_type 1))
1306+
in
12981307
let* x = x in
12991308
return (W.Call (f, [ x ]))
13001309

@@ -1337,7 +1346,12 @@ module Math = struct
13371346
let log10 f = unary "log10" f
13381347

13391348
let binary name x y =
1340-
let* f = register_import ~import_module:"Math" ~name (Fun (float_func_type 2)) in
1349+
let* f =
1350+
register_import
1351+
~import_module:(if Config.Flag.wasi () then "env" else "Math")
1352+
~name
1353+
(Fun (float_func_type 2))
1354+
in
13411355
let* x = x in
13421356
let* y = y in
13431357
return (W.Call (f, [ x; y ]))
@@ -1676,21 +1690,34 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
16761690
x
16771691
(block_expr
16781692
{ params = []; result = [ Value.value ] }
1679-
(let* exn =
1680-
block_expr
1681-
{ params = []; result = [ externref ] }
1682-
(let* e =
1683-
try_expr
1684-
{ params = []; result = [ externref ] }
1685-
(body
1686-
~result_typ:[ externref ]
1687-
~fall_through:`Skip
1688-
~context:(`Skip :: `Skip :: `Catch :: context))
1689-
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1690-
in
1691-
instr (W.Push e))
1692-
in
1693-
instr (W.CallInstr (f, [ exn ]))))
1693+
(if Config.Flag.wasi ()
1694+
then
1695+
let* e =
1696+
try_expr
1697+
{ params = []; result = [ Value.value ] }
1698+
(body
1699+
~result_typ:[ Value.value ]
1700+
~fall_through:`Skip
1701+
~context:(`Skip :: `Catch :: context))
1702+
[ ocaml_tag, 0, Value.value ]
1703+
in
1704+
instr (W.Push e)
1705+
else
1706+
let* exn =
1707+
block_expr
1708+
{ params = []; result = [ externref ] }
1709+
(let* e =
1710+
try_expr
1711+
{ params = []; result = [ externref ] }
1712+
(body
1713+
~result_typ:[ externref ]
1714+
~fall_through:`Skip
1715+
~context:(`Skip :: `Skip :: `Catch :: context))
1716+
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1717+
in
1718+
instr (W.Push e))
1719+
in
1720+
instr (W.CallInstr (f, [ exn ]))))
16941721
in
16951722
let* () = no_event in
16961723
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))
5677
(inline_tests
78+
(deps
79+
(sandbox preserve_file_kind))
5780
(modes js wasm best))
5881
(preprocess
5982
(pps ppx_expect)))

compiler/tests-jsoo/lib-effects/assume_no_perform.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ let () =
125125

126126
(* The code below should be called in direct style despite the installed
127127
effect handler *)
128-
Js_of_ocaml.Effect_js.assume_no_perform (fun () ->
128+
Jsoo_runtime.Effect.assume_no_perform (fun () ->
129129
let m, sd = benchmark iter_fun 5 in
130130
let () = printf "Iter: mean = %f, sd = %f\n%!" m sd in
131131

0 commit comments

Comments
 (0)