Skip to content

Commit 3a5a036

Browse files
committed
CR
1 parent 81aec6d commit 3a5a036

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let header formatter ~custom_header =
3434
| Some c -> Pretty_print.string formatter (c ^ "\n")
3535

3636
let jsoo_header formatter build_info =
37-
Pretty_print.string formatter "// Generated by js_of_ocaml\n";
37+
Pretty_print.string formatter (Printf.sprintf "%s\n" Global_constant.header);
3838
Pretty_print.string formatter (Build_info.to_string build_info)
3939

4040
type source_map_output =

compiler/lib/global_constant.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
open! Stdlib
2020

21+
let header = "// Generated by js_of_ocaml"
22+
2123
let global_object = "globalThis"
2224

2325
let global_object_ = Utf8_string.of_string_exn global_object

compiler/lib/link_js.ml

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -99,49 +99,30 @@ module Line_writer : sig
9999

100100
val of_channel : out_channel -> t
101101

102-
val write : ?source:Line_reader.t -> t -> string -> unit
102+
val write : t -> string -> unit
103103

104-
val write_lines : ?source:Line_reader.t -> t -> string -> unit
104+
val write_lines : t -> string -> unit
105105

106106
val lnum : t -> int
107107
end = struct
108108
type t =
109109
{ oc : out_channel
110110
; mutable lnum : int
111-
; mutable source : (string * int) option
112111
}
113112

114-
let of_channel oc = { oc; source = None; lnum = 0 }
113+
let of_channel oc = { oc; lnum = 0 }
115114

116-
let write ?source t s =
117-
let source =
118-
match source with
119-
| None -> None
120-
| Some ic -> Some (Line_reader.fname ic, Line_reader.lnum ic)
121-
in
122-
let emit fname lnum =
123-
output_string t.oc (Printf.sprintf "//# %d %S\n" lnum fname);
124-
1
125-
in
126-
let lnum_off =
127-
match t.source, source with
128-
| _, None -> 0
129-
| None, Some (fname, lnum) -> emit fname lnum
130-
| Some (fname1, lnum1), Some (fname2, lnum2) ->
131-
if String.equal fname1 fname2 && lnum1 + 1 = lnum2 then 0 else emit fname2 lnum2
132-
in
115+
let write t s =
133116
output_string t.oc s;
134117
output_string t.oc "\n";
135-
let lnum_off = lnum_off + 1 in
136-
t.source <- source;
137-
t.lnum <- t.lnum + lnum_off
118+
t.lnum <- t.lnum + 1
138119

139-
let write_lines ?source t lines =
120+
let write_lines t lines =
140121
let l = String.split_on_char ~sep:'\n' lines in
141122
let rec w = function
142123
| [ "" ] | [] -> ()
143124
| s :: xs ->
144-
write ?source t s;
125+
write t s;
145126
w xs
146127
in
147128
w l
@@ -172,7 +153,11 @@ let prefix_kind line =
172153

173154
let action ~resolve_sourcemap_url ~drop_source_map file line =
174155
match prefix_kind line, drop_source_map with
175-
| `Other, (true | false) -> Keep
156+
| `Other, (true | false) -> (
157+
match line with
158+
| "" -> Drop
159+
| s when String.equal s Global_constant.header -> Drop
160+
| _ -> Keep)
176161
| `Unit, (true | false) -> Unit
177162
| `Build_info bi, _ -> Build_info bi
178163
| (`Json_base64 _ | `Url _), true -> Drop
@@ -323,7 +308,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
323308
let reloc = ref [] in
324309
let copy ic oc =
325310
let line = Line_reader.next ic in
326-
Line_writer.write ~source:ic oc line
311+
Line_writer.write oc line
327312
in
328313
let rec read () =
329314
match Line_reader.peek ic with
@@ -342,6 +327,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
342327
if not !build_info_emitted
343328
then (
344329
let bi = Build_info.with_kind bi (if mklib then `Cma else `Unknown) in
330+
Line_writer.write oc Global_constant.header;
345331
Line_writer.write_lines oc (Build_info.to_string bi);
346332
build_info_emitted := true)
347333
| Drop -> skip ic
@@ -361,6 +347,12 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
361347
Line_writer.write_lines oc (Unit_info.to_string u));
362348
let size = ref 0 in
363349
let lsize = ref 0 in
350+
Line_writer.write
351+
oc
352+
(Printf.sprintf
353+
"//# %d %S"
354+
(Line_reader.lnum ic)
355+
(Line_reader.fname ic));
364356
while
365357
match Line_reader.peek ic with
366358
| None -> false
@@ -412,7 +404,6 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
412404
read ()
413405
in
414406
read ();
415-
Line_writer.write oc "";
416407
Line_reader.close ic;
417408
(match is_runtime with
418409
| None -> ()
@@ -434,7 +425,8 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
434425
(Parse_bytecode.Debug.create ~include_cmis:false false)
435426
code;
436427
let content = Buffer.contents b in
437-
Line_writer.write_lines oc content);
428+
Line_writer.write_lines oc content;
429+
Line_writer.write oc "");
438430
(match !sm_for_file with
439431
| None -> ()
440432
| Some x -> sm := (x, List.rev !reloc, line_offset) :: !sm);

0 commit comments

Comments
 (0)