Skip to content

Commit

Permalink
Merge pull request #67 from OlivierNicole/converge-jsoo-merge-03
Browse files Browse the repository at this point in the history
Merge various recent compiler/lib/ updates from js_of_ocaml
  • Loading branch information
vouillon authored Sep 11, 2024
2 parents 5e51faa + 929eed2 commit 238e5eb
Show file tree
Hide file tree
Showing 92 changed files with 2,399 additions and 1,060 deletions.
1 change: 1 addition & 0 deletions compiler/bin-js_of_ocaml/build_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function jsoo_create_file_extern(name,content){
~target:(JavaScript pfs_fmt)
~standalone:true
~wrap_with_fun:`Iife
~link:`Needed
(Parse_bytecode.Debug.create ~include_cmis:false false)
code
in
Expand Down
2 changes: 1 addition & 1 deletion compiler/bin-js_of_ocaml/check_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ let f (runtime_files, bytecode, target_env) =
needed
in
let needed = StringSet.of_list (List.map ~f:fst needed) in
let from_runtime1 = Linker.get_provided () in
let from_runtime1 = Linker.list_all () in
let from_runtime2 = Primitive.get_external () in
(* [from_runtime2] is a superset of [from_runtime1].
Extra primitives are registered on the ocaml side (e.g. generate.ml) *)
Expand Down
33 changes: 15 additions & 18 deletions compiler/bin-js_of_ocaml/cmd_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type t =
; source_map : (string option * Source_map.t) option
; runtime_files : string list
; no_runtime : bool
; include_partial_runtime : bool
; runtime_only : bool
; output_file : [ `Name of string | `Stdout ] * bool
; input_file : string option
Expand Down Expand Up @@ -122,12 +123,11 @@ let options =
let doc = "Do not include the standard runtime." in
Arg.(value & flag & info [ "noruntime"; "no-runtime" ] ~doc)
in
let runtime_only =
let include_partial_runtime =
let doc =
"[DEPRECATED: use js_of_ocaml build-runtime instead]. Generate a JavaScript file \
containing/exporting the runtime only."
"Include (partial) runtime when compiling cmo and cma files to JavaScript."
in
Arg.(value & flag & info [ "runtime-only" ] ~doc)
Arg.(value & flag & info [ "include-partial-runtime" ] ~doc)
in
let no_sourcemap =
let doc =
Expand Down Expand Up @@ -270,7 +270,7 @@ let options =
no_cmis
profile
no_runtime
runtime_only
include_partial_runtime
no_sourcemap
sourcemap
sourcemap_inline_in_js
Expand All @@ -283,16 +283,11 @@ let options =
keep_unit_names =
let chop_extension s = try Filename.chop_extension s with Invalid_argument _ -> s in
let runtime_files = js_files in
let runtime_files =
if runtime_only && Filename.check_suffix input_file ".js"
then runtime_files @ [ input_file ]
else runtime_files
in
let fs_external = fs_external || (toplevel && no_cmis) || runtime_only in
let fs_external = fs_external || (toplevel && no_cmis) in
let input_file =
match input_file, runtime_only with
| "-", _ | _, true -> None
| x, false -> Some x
match input_file with
| "-" -> None
| x -> Some x
in
let output_file =
match output_file with
Expand Down Expand Up @@ -341,7 +336,8 @@ let options =
; include_dirs
; runtime_files
; no_runtime
; runtime_only
; include_partial_runtime
; runtime_only = false
; fs_files
; fs_output
; fs_external
Expand All @@ -355,7 +351,7 @@ let options =
let t =
Term.(
const build_t
$ Jsoo_cmdline.Arg.t
$ Lazy.force Jsoo_cmdline.Arg.t
$ set_param
$ set_env
$ dynlink
Expand All @@ -370,7 +366,7 @@ let options =
$ no_cmis
$ profile
$ noruntime
$ runtime_only
$ include_partial_runtime
$ no_sourcemap
$ sourcemap
$ sourcemap_inline_in_js
Expand Down Expand Up @@ -570,6 +566,7 @@ let options_runtime_only =
; include_dirs
; runtime_files
; no_runtime
; include_partial_runtime = false
; runtime_only = true
; fs_files
; fs_output
Expand All @@ -584,7 +581,7 @@ let options_runtime_only =
let t =
Term.(
const build_t
$ Jsoo_cmdline.Arg.t
$ Lazy.force Jsoo_cmdline.Arg.t
$ toplevel
$ no_cmis
$ set_param
Expand Down
1 change: 1 addition & 0 deletions compiler/bin-js_of_ocaml/cmd_arg.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type t =
; source_map : (string option * Source_map.t) option
; runtime_files : string list
; no_runtime : bool
; include_partial_runtime : bool
; runtime_only : bool
; output_file : [ `Name of string | `Stdout ] * bool
; input_file : string option
Expand Down
89 changes: 76 additions & 13 deletions compiler/bin-js_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ let run
{ Cmd_arg.common
; profile
; source_map
; runtime_files
; runtime_files = runtime_files_from_cmdline
; no_runtime
; input_file
; output_file
Expand All @@ -87,6 +87,7 @@ let run
; fs_external
; export_file
; keep_unit_names
; include_partial_runtime
} =
let include_cmis = toplevel && not no_cmis in
let custom_header = common.Jsoo_cmdline.Arg.custom_header in
Expand Down Expand Up @@ -120,11 +121,13 @@ let run
Some (Hashtbl.fold (fun cmi () acc -> cmi :: acc) t [])
in
let runtime_files =
if toplevel || dynlink
if (not no_runtime) && (toplevel || dynlink)
then
let add_if_absent x l = if List.mem x ~set:l then l else x :: l in
runtime_files |> add_if_absent "+toplevel.js" |> add_if_absent "+dynlink.js"
else runtime_files
runtime_files_from_cmdline
|> add_if_absent "+toplevel.js"
|> add_if_absent "+dynlink.js"
else runtime_files_from_cmdline
in
let runtime_files, builtin =
List.partition_map runtime_files ~f:(fun name ->
Expand Down Expand Up @@ -176,7 +179,7 @@ let run
, noloc )
])
in
let output (one : Parse_bytecode.one) ~standalone ~source_map ~linkall output_file =
let output (one : Parse_bytecode.one) ~standalone ~source_map ~link output_file =
check_debug one;
let init_pseudo_fs = fs_external && standalone in
let sm =
Expand All @@ -194,7 +197,7 @@ let run
~target:(JavaScript fmt)
~standalone
?profile
~linkall
~link
~wrap_with_fun
?source_map
one.debug
Expand All @@ -218,7 +221,7 @@ let run
~target:(JavaScript fmt)
~standalone
?profile
~linkall
~link
~wrap_with_fun
?source_map
one.debug
Expand All @@ -229,7 +232,14 @@ let run
let instr = fs_instr2 in
let code = Code.prepend Code.empty instr in
let pfs_fmt = Pretty_print.to_out_channel chan in
Driver.f' ~standalone ?profile ~wrap_with_fun pfs_fmt one.debug code));
Driver.f'
~standalone
~link:`Needed
?profile
~wrap_with_fun
pfs_fmt
one.debug
code));
res
in
if times () then Format.eprintf "compilation: %a@." Timer.print t;
Expand All @@ -245,11 +255,32 @@ let run
let uinfo = Unit_info.of_cmo cmo in
Pretty_print.string fmt "\n";
Pretty_print.string fmt (Unit_info.to_string uinfo);
output code ~source_map ~standalone ~linkall:false output_file
output code ~source_map ~standalone ~link:`No output_file
in
let output_partial_runtime ~standalone ~source_map ((_, fmt) as output_file) =
assert (not standalone);
let uinfo =
Unit_info.of_primitives
(Linker.list_all ~from:runtime_files_from_cmdline () |> StringSet.elements)
in
Pretty_print.string fmt "\n";
Pretty_print.string fmt (Unit_info.to_string uinfo);
let code =
{ Parse_bytecode.code = Code.empty
; cmis = StringSet.empty
; debug = Parse_bytecode.Debug.create ~include_cmis:false false
}
in
output
code
~source_map
~standalone
~link:(`All_from runtime_files_from_cmdline)
output_file
in
(if runtime_only
then (
let prims = Primitive.get_external () |> StringSet.elements in
let prims = Linker.list_all () |> StringSet.elements in
assert (List.length prims > 0);
let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`JavaScript in
let uinfo = { uinfo with primitives = uinfo.primitives @ prims } in
Expand All @@ -268,7 +299,7 @@ let run
(fun ~standalone ~source_map ((_, fmt) as output_file) ->
Pretty_print.string fmt "\n";
Pretty_print.string fmt (Unit_info.to_string uinfo);
output code ~source_map ~standalone ~linkall:true output_file))
output code ~source_map ~standalone ~link:`All output_file))
else
let kind, ic, close_ic, include_dirs =
match input_file with
Expand Down Expand Up @@ -307,7 +338,7 @@ let run
~build_info:(Build_info.create `Exe)
~source_map
(fst output_file)
(output code ~linkall)
(output code ~link:(if linkall then `All else `Needed))
| `Cmo cmo ->
let output_file =
match output_file, keep_unit_names with
Expand Down Expand Up @@ -338,8 +369,35 @@ let run
~build_info:(Build_info.create `Cmo)
~source_map
output_file
(output_partial cmo code)
(fun ~standalone ~source_map output ->
let source_map =
if not include_partial_runtime
then source_map
else output_partial_runtime ~standalone ~source_map output
in
output_partial cmo code ~standalone ~source_map output)
| `Cma cma when keep_unit_names ->
(if include_partial_runtime
then
let output_file =
let gen dir = Filename.concat dir "runtime.js" in
match output_file with
| `Stdout, false -> gen "./"
| `Name x, false -> gen (Filename.dirname x)
| `Name x, true
when String.length x > 0 && Char.equal x.[String.length x - 1] '/' ->
gen x
| `Stdout, true | `Name _, true ->
failwith "use [-o dirname/] or remove [--keep-unit-names]"
in
output_gen
~standalone:false
~custom_header
~build_info:(Build_info.create `Runtime)
~source_map
(`Name output_file)
(fun ~standalone ~source_map output ->
output_partial_runtime ~standalone ~source_map output));
List.iter cma.lib_units ~f:(fun cmo ->
let output_file =
match output_file with
Expand Down Expand Up @@ -377,6 +435,11 @@ let run
(output_partial cmo code))
| `Cma cma ->
let f ~standalone ~source_map output =
let source_map =
if not include_partial_runtime
then source_map
else output_partial_runtime ~standalone ~source_map output
in
List.fold_left cma.lib_units ~init:source_map ~f:(fun source_map cmo ->
let t1 = Timer.make () in
let code =
Expand Down
2 changes: 1 addition & 1 deletion compiler/bin-js_of_ocaml/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ let options =
let t =
Term.(
const build_t
$ Jsoo_cmdline.Arg.t
$ Lazy.force Jsoo_cmdline.Arg.t
$ no_sourcemap
$ sourcemap
$ sourcemap_inline_in_js
Expand Down
4 changes: 3 additions & 1 deletion compiler/bin-jsoo_minify/cmd_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ let options =
let build_t common files output_file use_stdin =
`Ok { common; use_stdin; output_file; files }
in
let t = Term.(const build_t $ Jsoo_cmdline.Arg.t $ files $ output_file $ use_stdin) in
let t =
Term.(const build_t $ Lazy.force Jsoo_cmdline.Arg.t $ files $ output_file $ use_stdin)
in
Term.ret t

let info =
Expand Down
3 changes: 2 additions & 1 deletion compiler/bin-jsoo_minify/jsoo_minify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ let f { Cmd_arg.common; output_file; use_stdin; files } =
let true_ () = true in
let open Config in
let passes : ((unit -> bool) * (unit -> Js_traverse.mapper)) list =
[ (Flag.shortvar, fun () -> new Js_traverse.rename_variable)
[ ( Flag.shortvar
, fun () -> (new Js_traverse.rename_variable ~esm:false :> Js_traverse.mapper) )
; (true_, fun () -> new Js_traverse.simpl)
; (true_, fun () -> new Js_traverse.clean)
]
Expand Down
4 changes: 2 additions & 2 deletions compiler/bin-wasm_of_ocaml/cmd_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ let options =
let t =
Term.(
const build_t
$ Jsoo_cmdline.Arg.t
$ Lazy.force Jsoo_cmdline.Arg.t
$ set_param
$ include_dirs
$ profile
Expand Down Expand Up @@ -226,7 +226,7 @@ let options_runtime_only =
let t =
Term.(
const build_t
$ Jsoo_cmdline.Arg.t
$ Lazy.force Jsoo_cmdline.Arg.t
$ set_param
$ include_dirs
$ sourcemap
Expand Down
12 changes: 9 additions & 3 deletions compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ let generate_prelude ~out_file =
@@ fun ch ->
let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`Wasm in
let live_vars, in_cps, p, debug =
Driver.f ~target:Wasm (Parse_bytecode.Debug.create ~include_cmis:false false) code
Driver.f
~target:Wasm
~link:`Needed
(Parse_bytecode.Debug.create ~include_cmis:false false)
code
in
let context = Wa_generate.start () in
let _ =
Expand Down Expand Up @@ -182,7 +186,9 @@ let build_js_runtime ~primitives ?runtime_arguments () =
in
match
List.split_last
@@ Driver.link_and_pack [ Javascript.Return_statement (Some (EObj l)), N ]
@@ Driver.link_and_pack
~link:`Needed
[ Javascript.Return_statement (Some (EObj l)), N ]
with
| Some x -> x
| None -> assert false
Expand Down Expand Up @@ -281,7 +287,7 @@ let run
let code = one.code in
let standalone = Option.is_none unit_name in
let live_vars, in_cps, p, debug =
Driver.f ~target:Wasm ~standalone ?profile one.debug code
Driver.f ~target:Wasm ~standalone ?profile ~link:`No one.debug code
in
let context = Wa_generate.start () in
let toplevel_name, generated_js =
Expand Down
2 changes: 1 addition & 1 deletion compiler/bin-wasm_of_ocaml/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let options =
let t =
Term.(
const build_t
$ Jsoo_cmdline.Arg.t
$ Lazy.force Jsoo_cmdline.Arg.t
$ no_sourcemap
$ sourcemap
$ output_file
Expand Down
Loading

0 comments on commit 238e5eb

Please sign in to comment.