Skip to content

Commit 96de23a

Browse files
authored
Merge branch 'master' into label-parsing
2 parents 49eadfc + a9a4224 commit 96de23a

File tree

4 files changed

+41
-81
lines changed

4 files changed

+41
-81
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## Bug fixes
66
* Compiler (js parser): fix parsing of js labels (fix #1440)
7+
* Compiler: put custom header at the top of the output file (fix #1441)
78

89
# 5.1.1 (2023-03-15) - Lille
910
## Bug fixes

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,22 @@ let () = Sys.catch_break true
2828
let gen_unit_filename dir u =
2929
Filename.concat dir (Printf.sprintf "%s.js" u.Cmo_format.cu_name)
3030

31+
let header formatter ~custom_header =
32+
match custom_header with
33+
| None -> ()
34+
| Some c -> Pretty_print.string formatter (c ^ "\n")
35+
3136
let jsoo_header formatter build_info =
3237
Pretty_print.string formatter "// Generated by js_of_ocaml\n";
3338
Pretty_print.string formatter (Build_info.to_string build_info)
3439

35-
let output_gen ~build_info ~source_map output_file f =
40+
let output_gen ~standalone ~custom_header ~build_info ~source_map output_file f =
3641
let f chan k =
3742
let fmt = Pretty_print.to_out_channel chan in
3843
Driver.configure fmt;
44+
if standalone then header ~custom_header fmt;
3945
if Config.Flag.header () then jsoo_header fmt build_info;
40-
let sm = f ~source_map:(Option.map ~f:snd source_map) (k, fmt) in
46+
let sm = f ~standalone ~source_map:(Option.map ~f:snd source_map) (k, fmt) in
4147
match source_map, sm with
4248
| None, _ | _, None -> ()
4349
| Some (output_file, _), Some sm ->
@@ -169,7 +175,7 @@ let run
169175
, noloc )
170176
])
171177
in
172-
let output (one : Parse_bytecode.one) ~source_map ~linkall ~standalone output_file =
178+
let output (one : Parse_bytecode.one) ~standalone ~source_map ~linkall output_file =
173179
check_debug one;
174180
let init_pseudo_fs = fs_external && standalone in
175181
let sm =
@@ -189,7 +195,6 @@ let run
189195
~linkall
190196
~wrap_with_fun
191197
?source_map
192-
?custom_header
193198
fmt
194199
one.debug
195200
code
@@ -214,7 +219,6 @@ let run
214219
~linkall
215220
~wrap_with_fun
216221
?source_map
217-
?custom_header
218222
fmt
219223
one.debug
220224
code
@@ -224,28 +228,23 @@ let run
224228
let instr = fs_instr2 in
225229
let code = Code.prepend Code.empty instr in
226230
let pfs_fmt = Pretty_print.to_out_channel chan in
227-
Driver.f'
228-
~standalone
229-
?profile
230-
?custom_header
231-
~wrap_with_fun
232-
pfs_fmt
233-
one.debug
234-
code));
231+
Driver.f' ~standalone ?profile ~wrap_with_fun pfs_fmt one.debug code));
235232
res
236233
in
237234
if times () then Format.eprintf "compilation: %a@." Timer.print t;
238235
sm
239236
in
240237
let output_partial
241238
(cmo : Cmo_format.compilation_unit)
239+
~standalone
242240
~source_map
243241
code
244242
((_, fmt) as output_file) =
243+
assert (not standalone);
245244
let uinfo = Unit_info.of_cmo cmo in
246245
Pretty_print.string fmt "\n";
247246
Pretty_print.string fmt (Unit_info.to_string uinfo);
248-
output code ~source_map ~standalone:false ~linkall:false output_file
247+
output code ~source_map ~standalone ~linkall:false output_file
249248
in
250249
(if runtime_only
251250
then (
@@ -260,13 +259,15 @@ let run
260259
}
261260
in
262261
output_gen
262+
~standalone:true
263+
~custom_header
263264
~build_info:(Build_info.create `Runtime)
264265
~source_map
265266
(fst output_file)
266-
(fun ~source_map ((_, fmt) as output_file) ->
267+
(fun ~standalone ~source_map ((_, fmt) as output_file) ->
267268
Pretty_print.string fmt "\n";
268269
Pretty_print.string fmt (Unit_info.to_string uinfo);
269-
output code ~source_map ~standalone:true ~linkall:true output_file))
270+
output code ~source_map ~standalone ~linkall:true output_file))
270271
else
271272
let kind, ic, close_ic, include_dirs =
272273
match input_file with
@@ -299,10 +300,12 @@ let run
299300
in
300301
if times () then Format.eprintf " parsing: %a@." Timer.print t1;
301302
output_gen
303+
~standalone:true
304+
~custom_header
302305
~build_info:(Build_info.create `Exe)
303306
~source_map
304307
(fst output_file)
305-
(output code ~standalone:true ~linkall)
308+
(output code ~linkall)
306309
| `Cmo cmo ->
307310
let output_file =
308311
match output_file, keep_unit_names with
@@ -327,6 +330,8 @@ let run
327330
in
328331
if times () then Format.eprintf " parsing: %a@." Timer.print t1;
329332
output_gen
333+
~standalone:false
334+
~custom_header
330335
~build_info:(Build_info.create `Cmo)
331336
~source_map
332337
output_file
@@ -355,12 +360,14 @@ let run
355360
if times ()
356361
then Format.eprintf " parsing: %a (%s)@." Timer.print t1 cmo.cu_name;
357362
output_gen
363+
~standalone:false
364+
~custom_header
358365
~build_info:(Build_info.create `Cma)
359366
~source_map
360367
(`Name output_file)
361368
(output_partial cmo code))
362369
| `Cma cma ->
363-
let f ~source_map output =
370+
let f ~standalone ~source_map output =
364371
List.fold_left cma.lib_units ~init:source_map ~f:(fun source_map cmo ->
365372
let t1 = Timer.make () in
366373
let code =
@@ -373,9 +380,15 @@ let run
373380
in
374381
if times ()
375382
then Format.eprintf " parsing: %a (%s)@." Timer.print t1 cmo.cu_name;
376-
output_partial cmo ~source_map code output)
383+
output_partial cmo ~standalone ~source_map code output)
377384
in
378-
output_gen ~build_info:(Build_info.create `Cma) ~source_map (fst output_file) f);
385+
output_gen
386+
~standalone:false
387+
~custom_header
388+
~build_info:(Build_info.create `Cma)
389+
~source_map
390+
(fst output_file)
391+
f);
379392
close_ic ());
380393
Debug.stop_profiling ()
381394

compiler/lib/driver.ml

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,6 @@ let generate
187187
~warn_on_unhandled_effect
188188
d
189189

190-
let header formatter ~custom_header =
191-
match custom_header with
192-
| None -> ()
193-
| Some c -> Pretty_print.string formatter (c ^ "\n")
194-
195190
let debug_linker = Debug.find "linker"
196191

197192
let extra_js_files =
@@ -399,10 +394,9 @@ let coloring js =
399394
if times () then Format.eprintf " coloring: %a@." Timer.print t;
400395
js
401396

402-
let output formatter ~standalone ~custom_header ~source_map () js =
397+
let output formatter ~source_map () js =
403398
let t = Timer.make () in
404399
if times () then Format.eprintf "Start Writing file...@.";
405-
if standalone then header ~custom_header formatter;
406400
let sm = Js_output.program formatter ?source_map js in
407401
if times () then Format.eprintf " write: %a@." Timer.print t;
408402
sm
@@ -568,16 +562,7 @@ let configure formatter =
568562
Code.Var.set_pretty (pretty && not (Config.Flag.shortvar ()));
569563
Code.Var.set_stable (Config.Flag.stable_var ())
570564

571-
let full
572-
~standalone
573-
~wrap_with_fun
574-
~profile
575-
~linkall
576-
~source_map
577-
~custom_header
578-
formatter
579-
d
580-
p =
565+
let full ~standalone ~wrap_with_fun ~profile ~linkall ~source_map formatter d p =
581566
let exported_runtime = not standalone in
582567
let opt =
583568
specialize_js_once
@@ -595,34 +580,17 @@ let full
595580
+> pack ~wrap_with_fun ~standalone
596581
+> coloring
597582
+> check_js
598-
+> output formatter ~standalone ~custom_header ~source_map ()
583+
+> output formatter ~source_map ()
599584
in
600585
if times () then Format.eprintf "Start Optimizing...@.";
601586
let t = Timer.make () in
602587
let r = opt p in
603588
let () = if times () then Format.eprintf " optimizations : %a@." Timer.print t in
604589
emit r
605590

606-
let full_no_source_map
607-
~standalone
608-
~wrap_with_fun
609-
~profile
610-
~linkall
611-
~custom_header
612-
formatter
613-
d
614-
p =
591+
let full_no_source_map ~standalone ~wrap_with_fun ~profile ~linkall formatter d p =
615592
let (_ : Source_map.t option) =
616-
full
617-
~standalone
618-
~wrap_with_fun
619-
~profile
620-
~linkall
621-
~custom_header
622-
~source_map:None
623-
formatter
624-
d
625-
p
593+
full ~standalone ~wrap_with_fun ~profile ~linkall ~source_map:None formatter d p
626594
in
627595
()
628596

@@ -632,39 +600,20 @@ let f
632600
?(profile = O1)
633601
?(linkall = false)
634602
?source_map
635-
?custom_header
636603
formatter
637604
d
638605
p =
639-
full
640-
~standalone
641-
~wrap_with_fun
642-
~profile
643-
~linkall
644-
~source_map
645-
~custom_header
646-
formatter
647-
d
648-
p
606+
full ~standalone ~wrap_with_fun ~profile ~linkall ~source_map formatter d p
649607

650608
let f'
651609
?(standalone = true)
652610
?(wrap_with_fun = `Iife)
653611
?(profile = O1)
654612
?(linkall = false)
655-
?custom_header
656613
formatter
657614
d
658615
p =
659-
full_no_source_map
660-
~standalone
661-
~wrap_with_fun
662-
~profile
663-
~linkall
664-
~custom_header
665-
formatter
666-
d
667-
p
616+
full_no_source_map ~standalone ~wrap_with_fun ~profile ~linkall formatter d p
668617

669618
let from_string ~prims ~debug s formatter =
670619
let p, d = Parse_bytecode.from_string ~prims ~debug s in
@@ -673,7 +622,6 @@ let from_string ~prims ~debug s formatter =
673622
~wrap_with_fun:`Anonymous
674623
~profile:O1
675624
~linkall:false
676-
~custom_header:None
677625
formatter
678626
d
679627
p

compiler/lib/driver.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ val f :
2626
-> ?profile:profile
2727
-> ?linkall:bool
2828
-> ?source_map:Source_map.t
29-
-> ?custom_header:string
3029
-> Pretty_print.t
3130
-> Parse_bytecode.Debug.t
3231
-> Code.program
@@ -37,7 +36,6 @@ val f' :
3736
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
3837
-> ?profile:profile
3938
-> ?linkall:bool
40-
-> ?custom_header:string
4139
-> Pretty_print.t
4240
-> Parse_bytecode.Debug.t
4341
-> Code.program

0 commit comments

Comments
 (0)