Skip to content

Commit 3158d8c

Browse files
OlivierNicolehhugo
authored andcommitted
Compiler: Handle composite/Index sourcemaps
1 parent 95dafb1 commit 3158d8c

20 files changed

+436
-256
lines changed

compiler/bin-js_of_ocaml/build_fs.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function jsoo_create_file_extern(name,content){
7474
let code = Code.prepend Code.empty instr in
7575
Filename.gen_file output_file (fun chan ->
7676
let pfs_fmt = Pretty_print.to_out_channel chan in
77-
let (_ : Source_map.t option) =
77+
let (_ : Source_map.Standard.t option) =
7878
Driver.f
7979
~standalone:true
8080
~wrap_with_fun:`Iife

compiler/bin-js_of_ocaml/cmd_arg.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type t =
4343
{ common : Jsoo_cmdline.Arg.t
4444
; (* compile option *)
4545
profile : Driver.profile option
46-
; source_map : (string option * Source_map.t) option
46+
; source_map : (string option * Source_map.Standard.t) option
4747
; runtime_files : string list
4848
; no_runtime : bool
4949
; include_runtime : bool
@@ -308,7 +308,7 @@ let options =
308308
in
309309
Some
310310
( sm_output_file
311-
, { Source_map.version = 3
311+
, { Source_map.Standard.version = 3
312312
; file
313313
; sourceroot = sourcemap_root
314314
; sources = []
@@ -537,7 +537,7 @@ let options_runtime_only =
537537
in
538538
Some
539539
( sm_output_file
540-
, { Source_map.version = 3
540+
, { Source_map.Standard.version = 3
541541
; file
542542
; sourceroot = sourcemap_root
543543
; sources = []

compiler/bin-js_of_ocaml/cmd_arg.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type t =
2323
{ common : Jsoo_cmdline.Arg.t
2424
; (* compile option *)
2525
profile : Driver.profile option
26-
; source_map : (string option * Source_map.t) option
26+
; source_map : (string option * Source_map.Standard.t) option
2727
; runtime_files : string list
2828
; no_runtime : bool
2929
; include_runtime : bool

compiler/bin-js_of_ocaml/compile.ml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let output_gen ~standalone ~custom_header ~build_info ~source_map output_file f
4747
match source_map, sm with
4848
| None, _ | _, None -> ()
4949
| Some (output_file, _), Some sm ->
50+
let sm = `Standard sm in
5051
let urlData =
5152
match output_file with
5253
| None ->

compiler/bin-js_of_ocaml/link.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open Cmdliner
2323

2424
type t =
2525
{ common : Jsoo_cmdline.Arg.t
26-
; source_map : (string option * Source_map.t) option
26+
; source_map : (string option * Source_map.Standard.t) option
2727
; js_files : string list
2828
; output_file : string option
2929
; resolve_sourcemap_url : bool
@@ -102,7 +102,7 @@ let options =
102102
in
103103
Some
104104
( sm_output_file
105-
, { Source_map.version = 3
105+
, { Source_map.Standard.version = 3
106106
; file
107107
; sourceroot = sourcemap_root
108108
; sources = []

compiler/bin-jsoo_minify/jsoo_minify.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ let f { Cmd_arg.common; output_file; use_stdin; files } =
9292
if t () then (m ())#program p else p)
9393
in
9494
let p = Js_assign.program p in
95-
let (_ : Source_map.t option) = Js_output.program pp p in
95+
let (_ : Source_map.Standard.t option) = Js_output.program pp p in
9696
()
9797
in
9898
with_output (fun out_channel ->

compiler/lib/driver.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter d p =
713713
emit formatter optimized_code
714714

715715
let full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link d p =
716-
let (_ : Source_map.t option) =
716+
let (_ : Source_map.Standard.t option) =
717717
full ~standalone ~wrap_with_fun ~profile ~link ~source_map:None ~formatter d p
718718
in
719719
()

compiler/lib/driver.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ val f :
3535
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
3636
-> ?profile:profile
3737
-> link:[ `All | `All_from of string list | `Needed | `No ]
38-
-> ?source_map:Source_map.t
38+
-> ?source_map:Source_map.Standard.t
3939
-> formatter:Pretty_print.t
4040
-> Parse_bytecode.Debug.t
4141
-> Code.program
42-
-> Source_map.t option
42+
-> Source_map.Standard.t option
4343

4444
val f' :
4545
?standalone:bool

compiler/lib/js_assign.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ let program' (module Strategy : Strategy) p =
425425
"Some variables escaped (#%d). Use [--debug js_assign] for more info.@."
426426
(IdentSet.cardinal free)
427427
else
428-
let (_ : Source_map.t option) =
428+
let (_ : Source_map.Standard.t option) =
429429
Js_output.program
430430
~accept_unnamed_var:true
431431
(Pretty_print.to_out_channel stderr)

compiler/lib/js_output.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1905,8 +1905,8 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
19051905
let names = Hashtbl.create 17 in
19061906
let contents : Source_map.Source_content.t option list ref option =
19071907
match source_map with
1908-
| None | Some { Source_map.sources_content = None; _ } -> None
1909-
| Some { Source_map.sources_content = Some _; _ } -> Some (ref [])
1908+
| None | Some { Source_map.Standard.sources_content = None; _ } -> None
1909+
| Some { Source_map.Standard.sources_content = Some _; _ } -> Some (ref [])
19101910
in
19111911
let push_mapping, get_file_index, get_name_index, source_map_enabled =
19121912
let source_map_enabled =
@@ -1926,7 +1926,7 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
19261926
loop xs ys
19271927
in
19281928
loop sm.sources (Option.value ~default:[] sm.sources_content);
1929-
List.iter sm.Source_map.names ~f:(fun f ->
1929+
List.iter sm.Source_map.Standard.names ~f:(fun f ->
19301930
Hashtbl.add names f (Hashtbl.length names));
19311931
true
19321932
in
@@ -2014,7 +2014,7 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
20142014
{ gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
20152015
in
20162016
let mappings = Source_map.Mappings.encode mappings in
2017-
Some { sm with Source_map.sources; names; sources_content; mappings }
2017+
Some { sm with Source_map.Standard.sources; names; sources_content; mappings }
20182018
in
20192019
PP.check f;
20202020
(if stats ()

compiler/lib/js_output.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
val program :
2222
?accept_unnamed_var:bool
2323
-> Pretty_print.t
24-
-> ?source_map:Source_map.t
24+
-> ?source_map:Source_map.Standard.t
2525
-> Javascript.program
26-
-> Source_map.t option
26+
-> Source_map.Standard.t option

compiler/lib/link_js.ml

+16-10
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type action =
154154
| Drop
155155
| Unit
156156
| Build_info of Build_info.t
157-
| Source_map of Source_map.t
157+
| Source_map of Source_map.Standard.t
158158

159159
let prefix_kind line =
160160
match String.is_prefix ~prefix:sourceMappingURL line with
@@ -170,14 +170,19 @@ let prefix_kind line =
170170
| true -> `Json_base64 (String.length sourceMappingURL_base64)
171171
| false -> `Url (String.length sourceMappingURL))
172172

173+
let rule_out_index_map = function
174+
| `Standard sm -> sm
175+
| `Index _ -> failwith "unexpected index map at this stage"
176+
173177
let action ~resolve_sourcemap_url ~drop_source_map file line =
174178
match prefix_kind line, drop_source_map with
175179
| `Other, (true | false) -> Keep
176180
| `Unit, (true | false) -> Unit
177181
| `Build_info bi, _ -> Build_info bi
178182
| (`Json_base64 _ | `Url _), true -> Drop
179183
| `Json_base64 offset, false ->
180-
Source_map (Source_map.of_string (Base64.decode_exn ~off:offset line))
184+
Source_map
185+
(rule_out_index_map (Source_map.of_string (Base64.decode_exn ~off:offset line)))
181186
| `Url _, false when not resolve_sourcemap_url -> Drop
182187
| `Url offset, false ->
183188
let url = String.sub line ~pos:offset ~len:(String.length line - offset) in
@@ -186,7 +191,7 @@ let action ~resolve_sourcemap_url ~drop_source_map file line =
186191
let l = in_channel_length ic in
187192
let content = really_input_string ic l in
188193
close_in ic;
189-
Source_map (Source_map.of_string content)
194+
Source_map (rule_out_index_map (Source_map.of_string content))
190195

191196
module Units : sig
192197
val read : Line_reader.t -> Unit_info.t -> Unit_info.t
@@ -444,18 +449,19 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
444449
List.rev_map !sm ~f:(fun (sm, reloc) ->
445450
let tbl = Hashtbl.create 17 in
446451
List.iter reloc ~f:(fun (a, b) -> Hashtbl.add tbl a b);
447-
Source_map.filter_map sm ~f:(Hashtbl.find_opt tbl))
452+
Source_map.Standard.filter_map sm ~f:(Hashtbl.find_opt tbl))
448453
in
449-
(match Source_map.merge (init_sm :: sm) with
454+
(match Source_map.Standard.merge (init_sm :: sm) with
450455
| None -> ()
451456
| Some sm -> (
452457
(* preserve some info from [init_sm] *)
453458
let sm =
454-
{ sm with
455-
version = init_sm.version
456-
; file = init_sm.file
457-
; sourceroot = init_sm.sourceroot
458-
}
459+
`Standard
460+
{ sm with
461+
version = init_sm.version
462+
; file = init_sm.file
463+
; sourceroot = init_sm.sourceroot
464+
}
459465
in
460466
match file with
461467
| None ->

compiler/lib/link_js.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ val link :
2424
-> toplevel:bool
2525
-> files:string list
2626
-> resolve_sourcemap_url:bool
27-
-> source_map:(string option * Source_map.t) option
27+
-> source_map:(string option * Source_map.Standard.t) option
2828
-> unit

0 commit comments

Comments
 (0)