From ad01eddd2eff259487ea6b3add655b34ea4c6255 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Mon, 22 Jul 2024 14:50:03 +0200 Subject: [PATCH] Misc: yojson is no longer optional --- compiler/bin-js_of_ocaml/cmd_arg.ml | 20 ---- compiler/bin-js_of_ocaml/compile.ml | 4 +- compiler/bin-wasm_of_ocaml/compile.ml | 4 +- compiler/lib/dune | 6 +- compiler/lib/link_js.ml | 8 +- compiler/lib/source_map.ml | 106 +++++++++++++++++ compiler/lib/source_map.mli | 13 ++ compiler/lib/source_map_io.mli | 35 ------ compiler/lib/source_map_io.unsupported.ml | 28 ----- compiler/lib/source_map_io.yojson.ml | 132 --------------------- compiler/tests-compiler/util/util.ml | 2 +- compiler/tests-sourcemap/dump_sourcemap.ml | 2 +- manual/install.wiki | 2 +- tools/sourcemap/jsoo_sourcemap.ml | 4 +- 14 files changed, 133 insertions(+), 233 deletions(-) delete mode 100644 compiler/lib/source_map_io.mli delete mode 100644 compiler/lib/source_map_io.unsupported.ml delete mode 100644 compiler/lib/source_map_io.yojson.ml diff --git a/compiler/bin-js_of_ocaml/cmd_arg.ml b/compiler/bin-js_of_ocaml/cmd_arg.ml index e8083a221..629755117 100644 --- a/compiler/bin-js_of_ocaml/cmd_arg.ml +++ b/compiler/bin-js_of_ocaml/cmd_arg.ml @@ -319,16 +319,6 @@ let options = } ) else None in - let source_map = - if Option.is_some source_map && not Source_map_io.enabled - then ( - warn - "Warning: '--source-map' flag ignored because js_of_ocaml was compiled without \ - sourcemap support (install yojson to enable support)\n\ - %!"; - None) - else source_map - in let params : (string * string) list = List.flatten set_param in let static_env : (string * string) list = List.flatten set_env in let include_dirs = normalize_include_dirs include_dirs in @@ -559,16 +549,6 @@ let options_runtime_only = } ) else None in - let source_map = - if Option.is_some source_map && not Source_map_io.enabled - then ( - warn - "Warning: '--source-map' flag ignored because js_of_ocaml was compiled without \ - sourcemap support (install yojson to enable support)\n\ - %!"; - None) - else source_map - in let params : (string * string) list = List.flatten set_param in let static_env : (string * string) list = List.flatten set_env in let include_dirs = normalize_include_dirs include_dirs in diff --git a/compiler/bin-js_of_ocaml/compile.ml b/compiler/bin-js_of_ocaml/compile.ml index 6c16a5f2d..b456d69c3 100644 --- a/compiler/bin-js_of_ocaml/compile.ml +++ b/compiler/bin-js_of_ocaml/compile.ml @@ -50,10 +50,10 @@ let output_gen ~standalone ~custom_header ~build_info ~source_map output_file f let urlData = match output_file with | None -> - let data = Source_map_io.to_string sm in + let data = Source_map.to_string sm in "data:application/json;base64," ^ Base64.encode_exn data | Some output_file -> - Source_map_io.to_file sm ~file:output_file; + Source_map.to_file sm ~file:output_file; Filename.basename output_file in Pretty_print.newline fmt; diff --git a/compiler/bin-wasm_of_ocaml/compile.ml b/compiler/bin-wasm_of_ocaml/compile.ml index cfddd79c6..77668c3af 100644 --- a/compiler/bin-wasm_of_ocaml/compile.ml +++ b/compiler/bin-wasm_of_ocaml/compile.ml @@ -30,7 +30,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f if Option.is_some sourcemap_root || not sourcemap_don't_inline_content then ( let open Source_map in - let source_map, mappings = Source_map_io.of_file_no_mappings sourcemap_file in + let source_map, mappings = Source_map.of_file_no_mappings sourcemap_file in assert (List.is_empty (Option.value source_map.sources_content ~default:[])); (* Add source file contents to source map *) let sources_content = @@ -50,7 +50,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f (if Option.is_some sourcemap_root then sourcemap_root else source_map.sourceroot) } in - Source_map_io.to_file ?mappings source_map ~file:sourcemap_file) + Source_map.to_file ?mappings source_map ~file:sourcemap_file) let opt_with action x f = match x with diff --git a/compiler/lib/dune b/compiler/lib/dune index b03e41bdf..075e08643 100644 --- a/compiler/lib/dune +++ b/compiler/lib/dune @@ -7,11 +7,7 @@ compiler-libs.bytecomp menhirLib sedlex - (select - source_map_io.ml - from - (yojson -> source_map_io.yojson.ml) - (-> source_map_io.unsupported.ml))) + yojson) (flags (:standard -w -7-37 -safe-string)) (preprocess diff --git a/compiler/lib/link_js.ml b/compiler/lib/link_js.ml index 23932e75f..63e8639d5 100644 --- a/compiler/lib/link_js.ml +++ b/compiler/lib/link_js.ml @@ -177,7 +177,7 @@ 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 (Source_map.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 @@ -186,7 +186,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 (Source_map.of_string content) module Units : sig val read : Line_reader.t -> Unit_info.t -> Unit_info.t @@ -465,11 +465,11 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source in match file with | None -> - let data = Source_map_io.to_string sm in + let data = Source_map.to_string sm in let s = sourceMappingURL_base64 ^ Base64.encode_exn data in Line_writer.write oc s | Some file -> - Source_map_io.to_file sm ~file; + Source_map.to_file sm ~file; let s = sourceMappingURL ^ Filename.basename file in Line_writer.write oc s)); 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 3d88b0d1b..b8af36bf8 100644 --- a/compiler/lib/source_map.ml +++ b/compiler/lib/source_map.ml @@ -298,3 +298,109 @@ let merge = function ; names = List.rev acc_rev.names ; sources_content = Option.map ~f:List.rev acc_rev.sources_content } + +(* IO *) + +let json ?replace_mappings t = + let rewrite_path path = + if Filename.is_relative path + then path + else + match Build_path_prefix_map.get_build_path_prefix_map () with + | Some map -> Build_path_prefix_map.rewrite map path + | None -> path + in + `Assoc + [ "version", `Float (float_of_int t.version) + ; "file", `String (rewrite_path t.file) + ; ( "sourceRoot" + , `String + (match t.sourceroot with + | None -> "" + | Some s -> rewrite_path s) ) + ; "names", `List (List.map t.names ~f:(fun s -> `String s)) + ; "sources", `List (List.map t.sources ~f:(fun s -> `String (rewrite_path s))) + ; ( "mappings" + , `String (Option.value ~default:(string_of_mapping t.mappings) replace_mappings) ) + ; ( "sourcesContent" + , `List + (match t.sources_content with + | None -> [] + | Some l -> + List.map l ~f:(function + | None -> `Null + | Some s -> `String s)) ) + ] + +let invalid () = invalid_arg "Source_map.of_json" + +let string name rest = + try + match List.assoc name rest with + | `String s -> Some s + | `Null -> None + | _ -> invalid () + with Not_found -> None + +let list_string name rest = + try + match List.assoc name rest with + | `List l -> + Some + (List.map l ~f:(function + | `String s -> s + | _ -> invalid ())) + | _ -> invalid () + with Not_found -> None + +let list_string_opt name rest = + try + match List.assoc name rest with + | `List l -> + Some + (List.map l ~f:(function + | `String s -> Some s + | `Null -> None + | _ -> invalid ())) + | _ -> invalid () + with Not_found -> None + +let of_json ~parse_mappings json = + let parse ~version rest = + let def v d = + match v with + | None -> d + | Some v -> v + in + let file = string "file" rest in + let sourceroot = string "sourceRoot" rest in + let names = list_string "names" rest in + let sources = list_string "sources" rest in + let sources_content = list_string_opt "sourcesContent" rest in + let mappings = string "mappings" rest in + ( { version + ; file = def file "" + ; sourceroot + ; names = def names [] + ; sources_content + ; sources = def sources [] + ; mappings = mapping_of_string (def mappings "") + } + , if parse_mappings then None else mappings ) + in + match json with + | `Assoc (("version", `Float version) :: rest) when int_of_float version = 3 -> + parse ~version:3 rest + | `Assoc (("version", `Int 3) :: rest) -> parse ~version:3 rest + | _ -> invalid () + +let of_string s = of_json ~parse_mappings:true (Yojson.Basic.from_string s) |> fst + +let to_string m = Yojson.Basic.to_string (json m) + +let to_file ?mappings m ~file = + let replace_mappings = mappings in + Yojson.Basic.to_file file (json ?replace_mappings m) + +let of_file_no_mappings filename = + of_json ~parse_mappings:false (Yojson.Basic.from_file filename) diff --git a/compiler/lib/source_map.mli b/compiler/lib/source_map.mli index b394fe897..c928aae9f 100644 --- a/compiler/lib/source_map.mli +++ b/compiler/lib/source_map.mli @@ -59,3 +59,16 @@ val mapping_of_string : string -> mapping val string_of_mapping : mapping -> string val empty : filename:string -> t + +val to_string : t -> string + +val of_string : string -> t + +val of_file_no_mappings : string -> t * string option +(** Read source map from a file without parsing the mappings (which can be costly). The + [mappings] field is returned empty and the raw string is returned alongside the map. + *) + +val to_file : ?mappings:string -> t -> file:string -> unit +(** Write to a file. If a string is supplied as [mappings], use it instead of the + sourcemap's [mappings]. *) diff --git a/compiler/lib/source_map_io.mli b/compiler/lib/source_map_io.mli deleted file mode 100644 index c27288de0..000000000 --- a/compiler/lib/source_map_io.mli +++ /dev/null @@ -1,35 +0,0 @@ -(* Js_of_ocaml compiler - * http://www.ocsigen.org/js_of_ocaml/ - * Copyright (C) 2017 Hugo Heuzard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, with linking exception; - * either version 2.1 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) - -open Source_map - -val enabled : bool - -val to_string : t -> string - -val of_string : string -> t - -val of_file_no_mappings : string -> t * string option -(** Read source map from a file without parsing the mappings (which can be costly). The - [mappings] field is returned empty and the raw string is returned alongside the map. - *) - -val to_file : ?mappings:string -> t -> file:string -> unit -(** Write to a file. If a string is supplied as [mappings], use it instead of the - sourcemap's [mappings]. *) diff --git a/compiler/lib/source_map_io.unsupported.ml b/compiler/lib/source_map_io.unsupported.ml deleted file mode 100644 index dbdb35816..000000000 --- a/compiler/lib/source_map_io.unsupported.ml +++ /dev/null @@ -1,28 +0,0 @@ -(* Js_of_ocaml compiler - * http://www.ocsigen.org/js_of_ocaml/ - * Copyright (C) 2017 Hugo Heuzard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, with linking exception; - * either version 2.1 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) - -let fail () = failwith "Sourcemap support not available" - -let to_string _ = fail () - -let of_string _ = fail () - -let to_file _ _ = fail () - -let enabled = false diff --git a/compiler/lib/source_map_io.yojson.ml b/compiler/lib/source_map_io.yojson.ml deleted file mode 100644 index b9833d40a..000000000 --- a/compiler/lib/source_map_io.yojson.ml +++ /dev/null @@ -1,132 +0,0 @@ -(* Js_of_ocaml compiler - * http://www.ocsigen.org/js_of_ocaml/ - * Copyright (C) 2017 Hugo Heuzard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, with linking exception; - * either version 2.1 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) - -open Source_map - -let json ?replace_mappings t = - let rewrite_path path = - if Filename.is_relative path - then path - else - match Build_path_prefix_map.get_build_path_prefix_map () with - | Some map -> Build_path_prefix_map.rewrite map path - | None -> path - in - `Assoc - [ "version", `Float (float_of_int t.version) - ; "file", `String (rewrite_path t.file) - ; ( "sourceRoot" - , `String - (match t.sourceroot with - | None -> "" - | Some s -> rewrite_path s) ) - ; "names", `List (List.map (fun s -> `String s) t.names) - ; "sources", `List (List.map (fun s -> `String (rewrite_path s)) t.sources) - ; ( "mappings" - , `String (Option.value ~default:(string_of_mapping t.mappings) replace_mappings) ) - ; ( "sourcesContent" - , `List - (match t.sources_content with - | None -> [] - | Some l -> - List.map - (function - | None -> `Null - | Some s -> `String s) - l) ) - ] - -let invalid () = invalid_arg "Source_map_io.of_json" - -let string name rest = - try - match List.assoc name rest with - | `String s -> Some s - | `Null -> None - | _ -> invalid () - with Not_found -> None - -let list_string name rest = - try - match List.assoc name rest with - | `List l -> - Some - (List.map - (function - | `String s -> s - | _ -> invalid ()) - l) - | _ -> invalid () - with Not_found -> None - -let list_string_opt name rest = - try - match List.assoc name rest with - | `List l -> - Some - (List.map - (function - | `String s -> Some s - | `Null -> None - | _ -> invalid ()) - l) - | _ -> invalid () - with Not_found -> None - -let of_json ~parse_mappings json = - let parse ~version rest = - let def v d = - match v with - | None -> d - | Some v -> v - in - let file = string "file" rest in - let sourceroot = string "sourceRoot" rest in - let names = list_string "names" rest in - let sources = list_string "sources" rest in - let sources_content = list_string_opt "sourcesContent" rest in - let mappings = string "mappings" rest in - ( { version - ; file = def file "" - ; sourceroot - ; names = def names [] - ; sources_content - ; sources = def sources [] - ; mappings = (if parse_mappings then mapping_of_string (def mappings "") else []) - } - , if parse_mappings then None else mappings ) - in - match json with - | `Assoc (("version", `Float version) :: rest) when int_of_float version = 3 -> - parse ~version:3 rest - | `Assoc (("version", `Int 3) :: rest) -> parse ~version:3 rest - | _ -> invalid () - -let of_string s = of_json ~parse_mappings:true (Yojson.Basic.from_string s) |> fst - -let to_string m = Yojson.Basic.to_string (json m) - -let to_file ?mappings m ~file = - let replace_mappings = mappings in - Yojson.Basic.to_file file (json ?replace_mappings m) - -let of_file_no_mappings filename = - of_json ~parse_mappings:false (Yojson.Basic.from_file filename) - -let enabled = true diff --git a/compiler/tests-compiler/util/util.ml b/compiler/tests-compiler/util/util.ml index 8f6e4330f..278dd03fc 100644 --- a/compiler/tests-compiler/util/util.ml +++ b/compiler/tests-compiler/util/util.ml @@ -284,7 +284,7 @@ let extract_sourcemap file = | None -> String.concat ~sep:"\n" (input_lines line) | Some base64 -> Js_of_ocaml_compiler.Base64.decode_exn base64 in - Some (Js_of_ocaml_compiler.Source_map_io.of_string content) + Some (Js_of_ocaml_compiler.Source_map.of_string content) | _ -> None let compile_to_javascript diff --git a/compiler/tests-sourcemap/dump_sourcemap.ml b/compiler/tests-sourcemap/dump_sourcemap.ml index eec55e5a8..1d69de2fe 100644 --- a/compiler/tests-sourcemap/dump_sourcemap.ml +++ b/compiler/tests-sourcemap/dump_sourcemap.ml @@ -33,7 +33,7 @@ let extract_sourcemap lines = | None -> String.concat ~sep:"\n" (input_lines line) | Some base64 -> Base64.decode_exn base64 in - Some (Source_map_io.of_string content) + Some (Source_map.of_string content) | _ -> None let print_mapping lines (sm : Source_map.t) = diff --git a/manual/install.wiki b/manual/install.wiki index 5dce049e1..7a3449d7e 100644 --- a/manual/install.wiki +++ b/manual/install.wiki @@ -8,7 +8,7 @@ See opam files at the root of the repository for version constraints. Optional dependencies: * tyxml, see https://github.com/ocsigen/tyxml * reactiveData, see https://github.com/ocsigen/reactiveData - * yojson, see https://github.com/mjambon/yojson + * yojson, see https://github.com/ocaml-community/yojson == Install from opam {{{opam install js_of_ocaml js_of_ocaml-ppx js_of_ocaml-lwt}}} diff --git a/tools/sourcemap/jsoo_sourcemap.ml b/tools/sourcemap/jsoo_sourcemap.ml index 309358705..571a06384 100644 --- a/tools/sourcemap/jsoo_sourcemap.ml +++ b/tools/sourcemap/jsoo_sourcemap.ml @@ -44,5 +44,5 @@ let () = | Some base64 -> Js_of_ocaml_compiler.Base64.decode_exn base64) | _ -> failwith "unable to find sourcemap" in - let sm = Js_of_ocaml_compiler.Source_map_io.of_string content in - print_endline (Js_of_ocaml_compiler.Source_map_io.to_string sm) + let sm = Js_of_ocaml_compiler.Source_map.of_string content in + print_endline (Js_of_ocaml_compiler.Source_map.to_string sm)