From ac75d9770d6425d82e70dc4fc83753e0cc6a9c8c Mon Sep 17 00:00:00 2001 From: Olivier Nicole Date: Thu, 23 May 2024 17:03:23 +0200 Subject: [PATCH] Prepare sourcemap interface to input index maps (unsupported for now) --- compiler/lib/link_js.ml | 17 +++++++++++------ compiler/lib/source_map.ml | 2 +- compiler/lib/source_map.mli | 2 +- compiler/lib/source_map_io.mli | 10 +++++----- compiler/lib/source_map_io.yojson.ml | 12 ++++++++---- .../tests-compiler/build_path_prefix_map.ml | 3 ++- compiler/tests-compiler/sourcemap.ml | 3 ++- compiler/tests-compiler/util/util.mli | 2 +- 8 files changed, 31 insertions(+), 20 deletions(-) diff --git a/compiler/lib/link_js.ml b/compiler/lib/link_js.ml index 6c6b498a51..ae7d2b401f 100644 --- a/compiler/lib/link_js.ml +++ b/compiler/lib/link_js.ml @@ -173,6 +173,10 @@ let prefix_kind line = | true -> `Json_base64 (String.length sourceMappingURL_base64) | false -> `Url (String.length sourceMappingURL)) +let rule_out_index_map = function + | `Standard sm -> sm + | `Index _ -> failwith "unexpected index map at this stage" + let action ~resolve_sourcemap_url ~drop_source_map file line = match prefix_kind line, drop_source_map with | `Other, (true | false) -> Keep @@ -180,7 +184,8 @@ let action ~resolve_sourcemap_url ~drop_source_map file line = | `Build_info bi, _ -> Build_info bi | (`Json_base64 _ | `Url _), true -> Drop | `Json_base64 offset, false -> - Source_map (Source_map_io.of_string (Base64.decode_exn ~off:offset line)) + Source_map ( + rule_out_index_map (Source_map_io.of_string (Base64.decode_exn ~off:offset line))) | `Url _, false when not resolve_sourcemap_url -> Drop | `Url offset, false -> 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 = let l = in_channel_length ic in let content = really_input_string ic l in close_in ic; - Source_map (Source_map_io.of_string content) + Source_map (rule_out_index_map (Source_map_io.of_string content)) module Units : sig 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 | _ -> false); { version = init_sm.version ; file = init_sm.file - ; Composite.sections = + ; Index.sections = (let _, sections = List.fold_right sourcemaps_and_line_counts ~f:(fun (sm, generated_line_count) (cur_ofs, sections) -> - let offset = Composite.{ gen_line = cur_ofs; gen_column = 0 } in + let offset = Index.{ gen_line = cur_ofs; gen_column = 0 } in cur_ofs + generated_line_count, (offset, `Map sm) :: sections) ~init:(0, []) in @@ -493,11 +498,11 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source in (match file with | None -> - let data = Source_map_io.Composite.to_string merged_sourcemap in + let data = Source_map_io.Index.to_string merged_sourcemap in let s = sourceMappingURL_base64 ^ Base64.encode_exn data in Line_writer.write oc s |> ignore | Some file -> - Source_map_io.Composite.to_file merged_sourcemap file; + Source_map_io.Index.to_file merged_sourcemap file; let s = sourceMappingURL ^ Filename.basename file in Line_writer.write oc s |> ignore); if times () then Format.eprintf " sourcemap: %a@." Timer.print t diff --git a/compiler/lib/source_map.ml b/compiler/lib/source_map.ml index 6e1977e43c..3702ccbb52 100644 --- a/compiler/lib/source_map.ml +++ b/compiler/lib/source_map.ml @@ -675,7 +675,7 @@ let concat ~file ~sourceroot s1 s2 = s2.mappings } -module Composite = struct +module Index = struct type offset = { gen_line : int ; gen_column : int diff --git a/compiler/lib/source_map.mli b/compiler/lib/source_map.mli index cfebcbcfd1..e99e687e65 100644 --- a/compiler/lib/source_map.mli +++ b/compiler/lib/source_map.mli @@ -118,7 +118,7 @@ val concat : file:string -> sourceroot:string option -> t -> t -> t codebases, as it decodes the whole source text. This may be fixed in the future. *) -module Composite : sig +module Index : sig type offset = { gen_line : int ; gen_column : int diff --git a/compiler/lib/source_map_io.mli b/compiler/lib/source_map_io.mli index 5534f94a0e..41c41cba1b 100644 --- a/compiler/lib/source_map_io.mli +++ b/compiler/lib/source_map_io.mli @@ -25,10 +25,10 @@ val to_string : t -> string val to_file : t -> string -> unit -val of_string : string -> t +module Index : sig + val to_string : Index.t -> string -module Composite : sig - val to_string : Composite.t -> string - - val to_file : Composite.t -> string -> unit + val to_file : Index.t -> string -> unit end + +val of_string : string -> [ `Standard of Source_map.t | `Index of Source_map.Index.t ] diff --git a/compiler/lib/source_map_io.yojson.ml b/compiler/lib/source_map_io.yojson.ml index 7010ac2017..be2602e54f 100644 --- a/compiler/lib/source_map_io.yojson.ml +++ b/compiler/lib/source_map_io.yojson.ml @@ -97,6 +97,10 @@ let of_json json = | `Intlit version when Int.equal (int_of_string version) 3 -> () | `Floatlit _ | `Intlit _ -> invalid_arg "Source_map_io.of_json: version != 3" | _ -> invalid_arg "Source_map_io.of_json: version field is not a number"); + (match List.assoc "sections" rest with + | _ -> + invalid_arg "Source_map_io.of_json: this seems to be an index map. Reading index maps is currently not supported." + | exception Not_found -> ()); let file = string "file" rest in let sourceroot = string "sourceRoot" rest in let names = list_string "names" rest in @@ -124,7 +128,7 @@ let of_json json = } | _ -> invalid () -let of_string s = of_json (Yojson.Raw.from_string s) +let of_string s = `Standard (of_json (Yojson.Raw.from_string s)) let to_string m = Yojson.Raw.to_string (json m) @@ -132,15 +136,15 @@ let to_file m file = Yojson.Raw.to_file file (json m) let enabled = true -module Composite = struct +module Index = struct let json t = `Assoc - [ "version", `Intlit (Int.to_string t.Composite.version) + [ "version", `Intlit (Int.to_string t.Index.version) ; "file", stringlit_of_string (rewrite_path t.file) ; ( "sections" , `List (List.map - (fun ({ Composite.gen_line; gen_column }, `Map sm) -> + (fun ({ Index.gen_line; gen_column }, `Map sm) -> `Assoc [ ( "offset" , `Assoc diff --git a/compiler/tests-compiler/build_path_prefix_map.ml b/compiler/tests-compiler/build_path_prefix_map.ml index 6e2ce10fdc..00117ee13e 100644 --- a/compiler/tests-compiler/build_path_prefix_map.ml +++ b/compiler/tests-compiler/build_path_prefix_map.ml @@ -29,12 +29,13 @@ let%expect_test _ = |> compile_cmo_to_javascript ~sourcemap:true ~pretty:false |> extract_sourcemap |> function - | Some (sm : Js_of_ocaml_compiler.Source_map.t) -> + | Some (`Standard (sm : Js_of_ocaml_compiler.Source_map.t)) -> Printf.printf "file: %s\n" sm.file; Printf.printf "sourceRoot: %s\n" (Option.value ~default:"" sm.sourceroot); Printf.printf "sources:\n"; List.iter sm.sources ~f:(fun source -> Printf.printf "- %s\n" (normalize_path source)) + | Some (`Index _) -> failwith "unexpected index map" | None -> failwith "no sourcemap generated!"); [%expect {| diff --git a/compiler/tests-compiler/sourcemap.ml b/compiler/tests-compiler/sourcemap.ml index a073cef623..f0d9d6d04d 100644 --- a/compiler/tests-compiler/sourcemap.ml +++ b/compiler/tests-compiler/sourcemap.ml @@ -55,7 +55,8 @@ let%expect_test _ = print_file (Filetype.path_of_js_file js_file); match extract_sourcemap js_file with | None -> Printf.printf "No sourcemap found\n" - | Some sm -> print_mapping sm); + | Some (`Standard sm) -> print_mapping sm + | Some (`Index _) -> failwith "unexpected index map"); [%expect {| $ cat "test.ml" diff --git a/compiler/tests-compiler/util/util.mli b/compiler/tests-compiler/util/util.mli index 5788400928..38df2b62e8 100644 --- a/compiler/tests-compiler/util/util.mli +++ b/compiler/tests-compiler/util/util.mli @@ -53,7 +53,7 @@ val compile_bc_to_javascript : val jsoo_minify : ?flags:string list -> pretty:bool -> Filetype.js_file -> Filetype.js_file -val extract_sourcemap : Filetype.js_file -> Js_of_ocaml_compiler.Source_map.t option +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 val run_javascript : Filetype.js_file -> string