Skip to content

Commit ac75d97

Browse files
committed
Prepare sourcemap interface to input index maps (unsupported for now)
1 parent 8c1168b commit ac75d97

File tree

8 files changed

+31
-20
lines changed

8 files changed

+31
-20
lines changed

compiler/lib/link_js.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,19 @@ let prefix_kind line =
173173
| true -> `Json_base64 (String.length sourceMappingURL_base64)
174174
| false -> `Url (String.length sourceMappingURL))
175175

176+
let rule_out_index_map = function
177+
| `Standard sm -> sm
178+
| `Index _ -> failwith "unexpected index map at this stage"
179+
176180
let action ~resolve_sourcemap_url ~drop_source_map file line =
177181
match prefix_kind line, drop_source_map with
178182
| `Other, (true | false) -> Keep
179183
| `Unit, (true | false) -> Unit
180184
| `Build_info bi, _ -> Build_info bi
181185
| (`Json_base64 _ | `Url _), true -> Drop
182186
| `Json_base64 offset, false ->
183-
Source_map (Source_map_io.of_string (Base64.decode_exn ~off:offset line))
187+
Source_map (
188+
rule_out_index_map (Source_map_io.of_string (Base64.decode_exn ~off:offset line)))
184189
| `Url _, false when not resolve_sourcemap_url -> Drop
185190
| `Url offset, false ->
186191
let url = String.sub line ~pos:offset ~len:(String.length line - offset) in
@@ -189,7 +194,7 @@ let action ~resolve_sourcemap_url ~drop_source_map file line =
189194
let l = in_channel_length ic in
190195
let content = really_input_string ic l in
191196
close_in ic;
192-
Source_map (Source_map_io.of_string content)
197+
Source_map (rule_out_index_map (Source_map_io.of_string content))
193198

194199
module Units : sig
195200
val read : Line_reader.t -> Unit_info.t -> Unit_info.t
@@ -471,12 +476,12 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
471476
| _ -> false);
472477
{ version = init_sm.version
473478
; file = init_sm.file
474-
; Composite.sections =
479+
; Index.sections =
475480
(let _, sections =
476481
List.fold_right
477482
sourcemaps_and_line_counts
478483
~f:(fun (sm, generated_line_count) (cur_ofs, sections) ->
479-
let offset = Composite.{ gen_line = cur_ofs; gen_column = 0 } in
484+
let offset = Index.{ gen_line = cur_ofs; gen_column = 0 } in
480485
cur_ofs + generated_line_count, (offset, `Map sm) :: sections)
481486
~init:(0, [])
482487
in
@@ -493,11 +498,11 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
493498
in
494499
(match file with
495500
| None ->
496-
let data = Source_map_io.Composite.to_string merged_sourcemap in
501+
let data = Source_map_io.Index.to_string merged_sourcemap in
497502
let s = sourceMappingURL_base64 ^ Base64.encode_exn data in
498503
Line_writer.write oc s |> ignore
499504
| Some file ->
500-
Source_map_io.Composite.to_file merged_sourcemap file;
505+
Source_map_io.Index.to_file merged_sourcemap file;
501506
let s = sourceMappingURL ^ Filename.basename file in
502507
Line_writer.write oc s |> ignore);
503508
if times () then Format.eprintf " sourcemap: %a@." Timer.print t

compiler/lib/source_map.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ let concat ~file ~sourceroot s1 s2 =
675675
s2.mappings
676676
}
677677

678-
module Composite = struct
678+
module Index = struct
679679
type offset =
680680
{ gen_line : int
681681
; gen_column : int

compiler/lib/source_map.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ val concat : file:string -> sourceroot:string option -> t -> t -> t
118118
codebases, as it decodes the whole source text. This may be fixed in the
119119
future. *)
120120

121-
module Composite : sig
121+
module Index : sig
122122
type offset =
123123
{ gen_line : int
124124
; gen_column : int

compiler/lib/source_map_io.mli

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ val to_string : t -> string
2525

2626
val to_file : t -> string -> unit
2727

28-
val of_string : string -> t
28+
module Index : sig
29+
val to_string : Index.t -> string
2930

30-
module Composite : sig
31-
val to_string : Composite.t -> string
32-
33-
val to_file : Composite.t -> string -> unit
31+
val to_file : Index.t -> string -> unit
3432
end
33+
34+
val of_string : string -> [ `Standard of Source_map.t | `Index of Source_map.Index.t ]

compiler/lib/source_map_io.yojson.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ let of_json json =
9797
| `Intlit version when Int.equal (int_of_string version) 3 -> ()
9898
| `Floatlit _ | `Intlit _ -> invalid_arg "Source_map_io.of_json: version != 3"
9999
| _ -> invalid_arg "Source_map_io.of_json: version field is not a number");
100+
(match List.assoc "sections" rest with
101+
| _ ->
102+
invalid_arg "Source_map_io.of_json: this seems to be an index map. Reading index maps is currently not supported."
103+
| exception Not_found -> ());
100104
let file = string "file" rest in
101105
let sourceroot = string "sourceRoot" rest in
102106
let names = list_string "names" rest in
@@ -124,23 +128,23 @@ let of_json json =
124128
}
125129
| _ -> invalid ()
126130

127-
let of_string s = of_json (Yojson.Raw.from_string s)
131+
let of_string s = `Standard (of_json (Yojson.Raw.from_string s))
128132

129133
let to_string m = Yojson.Raw.to_string (json m)
130134

131135
let to_file m file = Yojson.Raw.to_file file (json m)
132136

133137
let enabled = true
134138

135-
module Composite = struct
139+
module Index = struct
136140
let json t =
137141
`Assoc
138-
[ "version", `Intlit (Int.to_string t.Composite.version)
142+
[ "version", `Intlit (Int.to_string t.Index.version)
139143
; "file", stringlit_of_string (rewrite_path t.file)
140144
; ( "sections"
141145
, `List
142146
(List.map
143-
(fun ({ Composite.gen_line; gen_column }, `Map sm) ->
147+
(fun ({ Index.gen_line; gen_column }, `Map sm) ->
144148
`Assoc
145149
[ ( "offset"
146150
, `Assoc

compiler/tests-compiler/build_path_prefix_map.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ let%expect_test _ =
2929
|> compile_cmo_to_javascript ~sourcemap:true ~pretty:false
3030
|> extract_sourcemap
3131
|> function
32-
| Some (sm : Js_of_ocaml_compiler.Source_map.t) ->
32+
| Some (`Standard (sm : Js_of_ocaml_compiler.Source_map.t)) ->
3333
Printf.printf "file: %s\n" sm.file;
3434
Printf.printf "sourceRoot: %s\n" (Option.value ~default:"<none>" sm.sourceroot);
3535
Printf.printf "sources:\n";
3636
List.iter sm.sources ~f:(fun source ->
3737
Printf.printf "- %s\n" (normalize_path source))
38+
| Some (`Index _) -> failwith "unexpected index map"
3839
| None -> failwith "no sourcemap generated!");
3940
[%expect
4041
{|

compiler/tests-compiler/sourcemap.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ let%expect_test _ =
5555
print_file (Filetype.path_of_js_file js_file);
5656
match extract_sourcemap js_file with
5757
| None -> Printf.printf "No sourcemap found\n"
58-
| Some sm -> print_mapping sm);
58+
| Some (`Standard sm) -> print_mapping sm
59+
| Some (`Index _) -> failwith "unexpected index map");
5960
[%expect
6061
{|
6162
$ cat "test.ml"

compiler/tests-compiler/util/util.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ val compile_bc_to_javascript :
5353
val jsoo_minify :
5454
?flags:string list -> pretty:bool -> Filetype.js_file -> Filetype.js_file
5555

56-
val extract_sourcemap : Filetype.js_file -> Js_of_ocaml_compiler.Source_map.t option
56+
val extract_sourcemap : Filetype.js_file -> [ `Standard of Js_of_ocaml_compiler.Source_map.t | `Index of Js_of_ocaml_compiler.Source_map.Index.t ] option
5757

5858
val run_javascript : Filetype.js_file -> string
5959

0 commit comments

Comments
 (0)