Skip to content

Commit 238e5eb

Browse files
authored
Merge pull request #67 from OlivierNicole/converge-jsoo-merge-03
Merge various recent compiler/lib/ updates from js_of_ocaml
2 parents 5e51faa + 929eed2 commit 238e5eb

Some content is hidden

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

92 files changed

+2399
-1060
lines changed

compiler/bin-js_of_ocaml/build_fs.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function jsoo_create_file_extern(name,content){
7878
~target:(JavaScript pfs_fmt)
7979
~standalone:true
8080
~wrap_with_fun:`Iife
81+
~link:`Needed
8182
(Parse_bytecode.Debug.create ~include_cmis:false false)
8283
code
8384
in

compiler/bin-js_of_ocaml/check_runtime.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ let f (runtime_files, bytecode, target_env) =
8888
needed
8989
in
9090
let needed = StringSet.of_list (List.map ~f:fst needed) in
91-
let from_runtime1 = Linker.get_provided () in
91+
let from_runtime1 = Linker.list_all () in
9292
let from_runtime2 = Primitive.get_external () in
9393
(* [from_runtime2] is a superset of [from_runtime1].
9494
Extra primitives are registered on the ocaml side (e.g. generate.ml) *)

compiler/bin-js_of_ocaml/cmd_arg.ml

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type t =
4646
; source_map : (string option * Source_map.t) option
4747
; runtime_files : string list
4848
; no_runtime : bool
49+
; include_partial_runtime : bool
4950
; runtime_only : bool
5051
; output_file : [ `Name of string | `Stdout ] * bool
5152
; input_file : string option
@@ -122,12 +123,11 @@ let options =
122123
let doc = "Do not include the standard runtime." in
123124
Arg.(value & flag & info [ "noruntime"; "no-runtime" ] ~doc)
124125
in
125-
let runtime_only =
126+
let include_partial_runtime =
126127
let doc =
127-
"[DEPRECATED: use js_of_ocaml build-runtime instead]. Generate a JavaScript file \
128-
containing/exporting the runtime only."
128+
"Include (partial) runtime when compiling cmo and cma files to JavaScript."
129129
in
130-
Arg.(value & flag & info [ "runtime-only" ] ~doc)
130+
Arg.(value & flag & info [ "include-partial-runtime" ] ~doc)
131131
in
132132
let no_sourcemap =
133133
let doc =
@@ -270,7 +270,7 @@ let options =
270270
no_cmis
271271
profile
272272
no_runtime
273-
runtime_only
273+
include_partial_runtime
274274
no_sourcemap
275275
sourcemap
276276
sourcemap_inline_in_js
@@ -283,16 +283,11 @@ let options =
283283
keep_unit_names =
284284
let chop_extension s = try Filename.chop_extension s with Invalid_argument _ -> s in
285285
let runtime_files = js_files in
286-
let runtime_files =
287-
if runtime_only && Filename.check_suffix input_file ".js"
288-
then runtime_files @ [ input_file ]
289-
else runtime_files
290-
in
291-
let fs_external = fs_external || (toplevel && no_cmis) || runtime_only in
286+
let fs_external = fs_external || (toplevel && no_cmis) in
292287
let input_file =
293-
match input_file, runtime_only with
294-
| "-", _ | _, true -> None
295-
| x, false -> Some x
288+
match input_file with
289+
| "-" -> None
290+
| x -> Some x
296291
in
297292
let output_file =
298293
match output_file with
@@ -341,7 +336,8 @@ let options =
341336
; include_dirs
342337
; runtime_files
343338
; no_runtime
344-
; runtime_only
339+
; include_partial_runtime
340+
; runtime_only = false
345341
; fs_files
346342
; fs_output
347343
; fs_external
@@ -355,7 +351,7 @@ let options =
355351
let t =
356352
Term.(
357353
const build_t
358-
$ Jsoo_cmdline.Arg.t
354+
$ Lazy.force Jsoo_cmdline.Arg.t
359355
$ set_param
360356
$ set_env
361357
$ dynlink
@@ -370,7 +366,7 @@ let options =
370366
$ no_cmis
371367
$ profile
372368
$ noruntime
373-
$ runtime_only
369+
$ include_partial_runtime
374370
$ no_sourcemap
375371
$ sourcemap
376372
$ sourcemap_inline_in_js
@@ -570,6 +566,7 @@ let options_runtime_only =
570566
; include_dirs
571567
; runtime_files
572568
; no_runtime
569+
; include_partial_runtime = false
573570
; runtime_only = true
574571
; fs_files
575572
; fs_output
@@ -584,7 +581,7 @@ let options_runtime_only =
584581
let t =
585582
Term.(
586583
const build_t
587-
$ Jsoo_cmdline.Arg.t
584+
$ Lazy.force Jsoo_cmdline.Arg.t
588585
$ toplevel
589586
$ no_cmis
590587
$ set_param

compiler/bin-js_of_ocaml/cmd_arg.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type t =
2626
; source_map : (string option * Source_map.t) option
2727
; runtime_files : string list
2828
; no_runtime : bool
29+
; include_partial_runtime : bool
2930
; runtime_only : bool
3031
; output_file : [ `Name of string | `Stdout ] * bool
3132
; input_file : string option

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let run
6868
{ Cmd_arg.common
6969
; profile
7070
; source_map
71-
; runtime_files
71+
; runtime_files = runtime_files_from_cmdline
7272
; no_runtime
7373
; input_file
7474
; output_file
@@ -87,6 +87,7 @@ let run
8787
; fs_external
8888
; export_file
8989
; keep_unit_names
90+
; include_partial_runtime
9091
} =
9192
let include_cmis = toplevel && not no_cmis in
9293
let custom_header = common.Jsoo_cmdline.Arg.custom_header in
@@ -120,11 +121,13 @@ let run
120121
Some (Hashtbl.fold (fun cmi () acc -> cmi :: acc) t [])
121122
in
122123
let runtime_files =
123-
if toplevel || dynlink
124+
if (not no_runtime) && (toplevel || dynlink)
124125
then
125126
let add_if_absent x l = if List.mem x ~set:l then l else x :: l in
126-
runtime_files |> add_if_absent "+toplevel.js" |> add_if_absent "+dynlink.js"
127-
else runtime_files
127+
runtime_files_from_cmdline
128+
|> add_if_absent "+toplevel.js"
129+
|> add_if_absent "+dynlink.js"
130+
else runtime_files_from_cmdline
128131
in
129132
let runtime_files, builtin =
130133
List.partition_map runtime_files ~f:(fun name ->
@@ -176,7 +179,7 @@ let run
176179
, noloc )
177180
])
178181
in
179-
let output (one : Parse_bytecode.one) ~standalone ~source_map ~linkall output_file =
182+
let output (one : Parse_bytecode.one) ~standalone ~source_map ~link output_file =
180183
check_debug one;
181184
let init_pseudo_fs = fs_external && standalone in
182185
let sm =
@@ -194,7 +197,7 @@ let run
194197
~target:(JavaScript fmt)
195198
~standalone
196199
?profile
197-
~linkall
200+
~link
198201
~wrap_with_fun
199202
?source_map
200203
one.debug
@@ -218,7 +221,7 @@ let run
218221
~target:(JavaScript fmt)
219222
~standalone
220223
?profile
221-
~linkall
224+
~link
222225
~wrap_with_fun
223226
?source_map
224227
one.debug
@@ -229,7 +232,14 @@ let run
229232
let instr = fs_instr2 in
230233
let code = Code.prepend Code.empty instr in
231234
let pfs_fmt = Pretty_print.to_out_channel chan in
232-
Driver.f' ~standalone ?profile ~wrap_with_fun pfs_fmt one.debug code));
235+
Driver.f'
236+
~standalone
237+
~link:`Needed
238+
?profile
239+
~wrap_with_fun
240+
pfs_fmt
241+
one.debug
242+
code));
233243
res
234244
in
235245
if times () then Format.eprintf "compilation: %a@." Timer.print t;
@@ -245,11 +255,32 @@ let run
245255
let uinfo = Unit_info.of_cmo cmo in
246256
Pretty_print.string fmt "\n";
247257
Pretty_print.string fmt (Unit_info.to_string uinfo);
248-
output code ~source_map ~standalone ~linkall:false output_file
258+
output code ~source_map ~standalone ~link:`No output_file
259+
in
260+
let output_partial_runtime ~standalone ~source_map ((_, fmt) as output_file) =
261+
assert (not standalone);
262+
let uinfo =
263+
Unit_info.of_primitives
264+
(Linker.list_all ~from:runtime_files_from_cmdline () |> StringSet.elements)
265+
in
266+
Pretty_print.string fmt "\n";
267+
Pretty_print.string fmt (Unit_info.to_string uinfo);
268+
let code =
269+
{ Parse_bytecode.code = Code.empty
270+
; cmis = StringSet.empty
271+
; debug = Parse_bytecode.Debug.create ~include_cmis:false false
272+
}
273+
in
274+
output
275+
code
276+
~source_map
277+
~standalone
278+
~link:(`All_from runtime_files_from_cmdline)
279+
output_file
249280
in
250281
(if runtime_only
251282
then (
252-
let prims = Primitive.get_external () |> StringSet.elements in
283+
let prims = Linker.list_all () |> StringSet.elements in
253284
assert (List.length prims > 0);
254285
let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`JavaScript in
255286
let uinfo = { uinfo with primitives = uinfo.primitives @ prims } in
@@ -268,7 +299,7 @@ let run
268299
(fun ~standalone ~source_map ((_, fmt) as output_file) ->
269300
Pretty_print.string fmt "\n";
270301
Pretty_print.string fmt (Unit_info.to_string uinfo);
271-
output code ~source_map ~standalone ~linkall:true output_file))
302+
output code ~source_map ~standalone ~link:`All output_file))
272303
else
273304
let kind, ic, close_ic, include_dirs =
274305
match input_file with
@@ -307,7 +338,7 @@ let run
307338
~build_info:(Build_info.create `Exe)
308339
~source_map
309340
(fst output_file)
310-
(output code ~linkall)
341+
(output code ~link:(if linkall then `All else `Needed))
311342
| `Cmo cmo ->
312343
let output_file =
313344
match output_file, keep_unit_names with
@@ -338,8 +369,35 @@ let run
338369
~build_info:(Build_info.create `Cmo)
339370
~source_map
340371
output_file
341-
(output_partial cmo code)
372+
(fun ~standalone ~source_map output ->
373+
let source_map =
374+
if not include_partial_runtime
375+
then source_map
376+
else output_partial_runtime ~standalone ~source_map output
377+
in
378+
output_partial cmo code ~standalone ~source_map output)
342379
| `Cma cma when keep_unit_names ->
380+
(if include_partial_runtime
381+
then
382+
let output_file =
383+
let gen dir = Filename.concat dir "runtime.js" in
384+
match output_file with
385+
| `Stdout, false -> gen "./"
386+
| `Name x, false -> gen (Filename.dirname x)
387+
| `Name x, true
388+
when String.length x > 0 && Char.equal x.[String.length x - 1] '/' ->
389+
gen x
390+
| `Stdout, true | `Name _, true ->
391+
failwith "use [-o dirname/] or remove [--keep-unit-names]"
392+
in
393+
output_gen
394+
~standalone:false
395+
~custom_header
396+
~build_info:(Build_info.create `Runtime)
397+
~source_map
398+
(`Name output_file)
399+
(fun ~standalone ~source_map output ->
400+
output_partial_runtime ~standalone ~source_map output));
343401
List.iter cma.lib_units ~f:(fun cmo ->
344402
let output_file =
345403
match output_file with
@@ -377,6 +435,11 @@ let run
377435
(output_partial cmo code))
378436
| `Cma cma ->
379437
let f ~standalone ~source_map output =
438+
let source_map =
439+
if not include_partial_runtime
440+
then source_map
441+
else output_partial_runtime ~standalone ~source_map output
442+
in
380443
List.fold_left cma.lib_units ~init:source_map ~f:(fun source_map cmo ->
381444
let t1 = Timer.make () in
382445
let code =

compiler/bin-js_of_ocaml/link.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ let options =
126126
let t =
127127
Term.(
128128
const build_t
129-
$ Jsoo_cmdline.Arg.t
129+
$ Lazy.force Jsoo_cmdline.Arg.t
130130
$ no_sourcemap
131131
$ sourcemap
132132
$ sourcemap_inline_in_js

compiler/bin-jsoo_minify/cmd_arg.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ let options =
4242
let build_t common files output_file use_stdin =
4343
`Ok { common; use_stdin; output_file; files }
4444
in
45-
let t = Term.(const build_t $ Jsoo_cmdline.Arg.t $ files $ output_file $ use_stdin) in
45+
let t =
46+
Term.(const build_t $ Lazy.force Jsoo_cmdline.Arg.t $ files $ output_file $ use_stdin)
47+
in
4648
Term.ret t
4749

4850
let info =

compiler/bin-jsoo_minify/jsoo_minify.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ let f { Cmd_arg.common; output_file; use_stdin; files } =
8181
let true_ () = true in
8282
let open Config in
8383
let passes : ((unit -> bool) * (unit -> Js_traverse.mapper)) list =
84-
[ (Flag.shortvar, fun () -> new Js_traverse.rename_variable)
84+
[ ( Flag.shortvar
85+
, fun () -> (new Js_traverse.rename_variable ~esm:false :> Js_traverse.mapper) )
8586
; (true_, fun () -> new Js_traverse.simpl)
8687
; (true_, fun () -> new Js_traverse.clean)
8788
]

compiler/bin-wasm_of_ocaml/cmd_arg.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ let options =
145145
let t =
146146
Term.(
147147
const build_t
148-
$ Jsoo_cmdline.Arg.t
148+
$ Lazy.force Jsoo_cmdline.Arg.t
149149
$ set_param
150150
$ include_dirs
151151
$ profile
@@ -226,7 +226,7 @@ let options_runtime_only =
226226
let t =
227227
Term.(
228228
const build_t
229-
$ Jsoo_cmdline.Arg.t
229+
$ Lazy.force Jsoo_cmdline.Arg.t
230230
$ set_param
231231
$ include_dirs
232232
$ sourcemap

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ let generate_prelude ~out_file =
145145
@@ fun ch ->
146146
let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`Wasm in
147147
let live_vars, in_cps, p, debug =
148-
Driver.f ~target:Wasm (Parse_bytecode.Debug.create ~include_cmis:false false) code
148+
Driver.f
149+
~target:Wasm
150+
~link:`Needed
151+
(Parse_bytecode.Debug.create ~include_cmis:false false)
152+
code
149153
in
150154
let context = Wa_generate.start () in
151155
let _ =
@@ -182,7 +186,9 @@ let build_js_runtime ~primitives ?runtime_arguments () =
182186
in
183187
match
184188
List.split_last
185-
@@ Driver.link_and_pack [ Javascript.Return_statement (Some (EObj l)), N ]
189+
@@ Driver.link_and_pack
190+
~link:`Needed
191+
[ Javascript.Return_statement (Some (EObj l)), N ]
186192
with
187193
| Some x -> x
188194
| None -> assert false
@@ -281,7 +287,7 @@ let run
281287
let code = one.code in
282288
let standalone = Option.is_none unit_name in
283289
let live_vars, in_cps, p, debug =
284-
Driver.f ~target:Wasm ~standalone ?profile one.debug code
290+
Driver.f ~target:Wasm ~standalone ?profile ~link:`No one.debug code
285291
in
286292
let context = Wa_generate.start () in
287293
let toplevel_name, generated_js =

0 commit comments

Comments
 (0)