Skip to content

Commit 6f53be3

Browse files
vouillonhhugo
authored andcommitted
Source maps: use ignoreList to hide runtime files
The generated code is also hidden by using a dummy file.
1 parent 9fcf5dc commit 6f53be3

File tree

8 files changed

+89
-9
lines changed

8 files changed

+89
-9
lines changed

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ let find_source file =
7979
match Builtins.find file with
8080
| Some f -> Some (Source_map.Source_content.create (Builtins.File.content f))
8181
| None ->
82-
if Sys.file_exists file && not (Sys.is_directory file)
82+
if String.equal file Js_output.blackbox_filename
83+
then Some (Source_map.Source_content.create "(* generated code *)")
84+
else if Sys.file_exists file && not (Sys.is_directory file)
8385
then
8486
let content = Fs.read_file file in
8587
Some (Source_map.Source_content.create content)
@@ -99,9 +101,18 @@ let sourcemap_section_of_info
99101
| None -> filename
100102
| Some _ -> Filename.concat "/builtin" filename)
101103
in
104+
let ignore_list =
105+
List.filter sources ~f:(fun filename -> String.is_prefix ~prefix:"/builtin/" filename)
106+
in
102107
let offset, mappings = Source_map.Mappings.encode_with_offset mappings in
103108
let map =
104-
{ (base : Source_map.Standard.t) with sources; sources_content; names; mappings }
109+
{ (base : Source_map.Standard.t) with
110+
sources
111+
; sources_content
112+
; names
113+
; mappings
114+
; ignore_list
115+
}
105116
in
106117
{ Source_map.Index.offset; map }
107118

compiler/lib/js_output.ml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ module Make (D : sig
133133

134134
val get_name_index : string -> int
135135

136+
val hidden_location : Source_map.map
137+
136138
val source_map_enabled : bool
137139

138140
val accept_unnamed_var : bool
@@ -167,7 +169,7 @@ struct
167169
then
168170
match loc with
169171
| N | U | Pi { Parse_info.src = None | Some ""; _ } ->
170-
push_mapping (PP.pos f) (Source_map.Gen { gen_line = -1; gen_col = -1 })
172+
push_mapping (PP.pos f) hidden_location
171173
| Pi { Parse_info.src = Some file; line; col; _ } ->
172174
push_mapping
173175
(PP.pos f)
@@ -209,10 +211,15 @@ struct
209211
(match loc with
210212
| None | Some { Parse_info.src = Some "" | None; _ } ->
211213
(* Use a dummy location. It is going to be ignored anyway *)
214+
let ori_source =
215+
match hidden_location with
216+
| Source_map.Gen_Ori { ori_source; _ } -> ori_source
217+
| _ -> 0
218+
in
212219
Source_map.Gen_Ori_Name
213220
{ gen_line = -1
214221
; gen_col = -1
215-
; ori_source = 0
222+
; ori_source
216223
; ori_line = 1
217224
; ori_col = 0
218225
; ori_name = get_name_index nm
@@ -2042,6 +2049,8 @@ let hashtbl_to_list htb =
20422049
|> List.sort ~cmp:(fun (_, a) (_, b) -> compare a b)
20432050
|> List.map ~f:fst
20442051
2052+
let blackbox_filename = "/builtin/blackbox.ml"
2053+
20452054
let program ?(accept_unnamed_var = false) ?(source_map = false) f p =
20462055
let temp_mappings = ref [] in
20472056
let files = Hashtbl.create 17 in
@@ -2061,13 +2070,24 @@ let program ?(accept_unnamed_var = false) ?(source_map = false) f p =
20612070
Hashtbl.add names name pos;
20622071
pos )
20632072
in
2073+
let hidden_location =
2074+
Source_map.Gen_Ori
2075+
{ gen_line = -1
2076+
; gen_col = -1
2077+
; ori_source = get_file_index blackbox_filename
2078+
; ori_line = 1
2079+
; ori_col = 0
2080+
}
2081+
in
20642082
let module O = Make (struct
20652083
let push_mapping = push_mapping
20662084
20672085
let get_name_index = get_name_index
20682086
20692087
let get_file_index = get_file_index
20702088
2089+
let hidden_location = hidden_location
2090+
20712091
let source_map_enabled = source_map
20722092
20732093
let accept_unnamed_var = accept_unnamed_var

compiler/lib/js_output.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ val program :
2424
-> Pretty_print.t
2525
-> Javascript.program
2626
-> Source_map.info
27+
28+
val blackbox_filename : string

compiler/lib/source_map.ml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ let string_of_stringlit (`Stringlit s) =
294294
| `String s -> s
295295
| _ -> invalid ()
296296

297+
let int_of_intlit (`Intlit s) =
298+
match Yojson.Safe.from_string s with
299+
| `Int s -> s
300+
| _ -> invalid ()
301+
297302
let stringlit name rest : [ `Stringlit of string ] option =
298303
try
299304
match List.assoc name rest with
@@ -325,6 +330,17 @@ let list_stringlit_opt name rest =
325330
| _ -> invalid ()
326331
with Not_found -> None
327332

333+
let list_intlit name rest =
334+
try
335+
match List.assoc name rest with
336+
| `List l ->
337+
Some
338+
(List.map l ~f:(function
339+
| `Intlit _ as s -> s
340+
| _ -> invalid ()))
341+
| _ -> invalid ()
342+
with Not_found -> None
343+
328344
module Standard = struct
329345
type t =
330346
{ version : int
@@ -334,6 +350,7 @@ module Standard = struct
334350
; sources_content : Source_content.t option list option
335351
; names : string list
336352
; mappings : Mappings.t
353+
; ignore_list : string list
337354
}
338355

339356
let empty ~inline_source_content =
@@ -344,6 +361,7 @@ module Standard = struct
344361
; sources_content = (if inline_source_content then Some [] else None)
345362
; names = []
346363
; mappings = Mappings.empty
364+
; ignore_list = []
347365
}
348366

349367
let maps ~sources_offset ~names_offset x =
@@ -472,6 +490,21 @@ module Standard = struct
472490
(List.map l ~f:(function
473491
| None -> `Null
474492
| Some x -> Source_content.to_json x))) )
493+
; ( "ignoreList"
494+
, match t.ignore_list with
495+
| [] -> None
496+
| _ ->
497+
Some
498+
(`List
499+
(let s = StringSet.of_list t.ignore_list in
500+
List.filter_map
501+
~f:(fun x -> x)
502+
(List.mapi
503+
~f:(fun i nm ->
504+
if StringSet.mem nm s
505+
then Some (`Intlit (string_of_int i))
506+
else None)
507+
t.sources))) )
475508
])
476509

477510
let of_json (json : Yojson.Raw.t) =
@@ -505,13 +538,25 @@ module Standard = struct
505538
| None -> Mappings.empty
506539
| Some s -> Mappings.of_string_unsafe s
507540
in
541+
let ignore_list =
542+
let s =
543+
IntSet.of_list
544+
(List.map
545+
~f:int_of_intlit
546+
(Option.value ~default:[] (list_intlit "ignoreList" rest)))
547+
in
548+
List.filter_map
549+
~f:(fun x -> x)
550+
(List.mapi ~f:(fun i nm -> if IntSet.mem i s then Some nm else None) sources)
551+
in
508552
{ version = int_of_string version
509553
; file
510554
; sourceroot
511555
; names
512556
; sources_content
513557
; sources
514558
; mappings
559+
; ignore_list
515560
}
516561
| _ -> invalid ()
517562

@@ -520,7 +565,8 @@ module Standard = struct
520565
let to_file m file = Yojson.Raw.to_file file (json m)
521566

522567
let invariant
523-
{ version; file = _; sourceroot = _; names; sources_content; sources; mappings } =
568+
{ version; file = _; sourceroot = _; names; sources_content; sources; mappings; _ }
569+
=
524570
if not (version_is_valid version)
525571
then invalid_arg "Source_map.Standard.invariant: invalid version";
526572
match sources_content with

compiler/lib/source_map.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ module Standard : sig
9898
(** Left uninterpreted, since most useful operations can be performed efficiently
9999
directly on the encoded form, and a full decoding can be costly for big
100100
sourcemaps. *)
101+
; ignore_list : string list
101102
}
102103

103104
val filter_map : t -> f:(int -> int option) -> t

compiler/tests-compiler/build_path_prefix_map.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ let%expect_test _ =
4848
file: test.js
4949
sourceRoot: <none>
5050
sources:
51+
- /builtin/blackbox.ml
5152
- /dune-root/test.ml
5253
|}]

compiler/tests-compiler/sourcemap.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ let%expect_test _ =
8383
11: (globalThis));
8484
12:
8585
13: //# sourceMappingURL=test.map
86-
/dune-root/test.ml:1:0 -> 5:7
87-
null -> 5:17
86+
/builtin/blackbox.ml:1:0 -> 5:7
87+
/builtin/blackbox.ml:1:0 -> 5:17
8888
/dune-root/test.ml:1:4 -> 6:12
8989
/dune-root/test.ml:1:7 -> 6:15
9090
/dune-root/test.ml:1:11 -> 6:18
9191
/dune-root/test.ml:1:12 -> 6:28
9292
/dune-root/test.ml:1:12 -> 7:7
93-
null -> 7:14
93+
/builtin/blackbox.ml:1:0 -> 7:14
9494
|}]
9595

9696
let%expect_test _ =

compiler/tests-sourcemap/dump.reference

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
sourcemap for test.bc.js
2-
/my/sourceRoot#b.ml:1:0 -> 7: var <>runtime = globalThis.jsoo_runtime;
32
/my/sourceRoot#b.ml:1:4 -> 12: function <>f(x){return x - 1 | 0; }
43
/my/sourceRoot#b.ml:1:6 -> 14: function f(<>x){return x - 1 | 0; }
54
/my/sourceRoot#b.ml:1:10 -> 17: function f(x){<>return x - 1 | 0; }

0 commit comments

Comments
 (0)