Skip to content

Commit

Permalink
Misc: yojson is no longer optional
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo authored and OlivierNicole committed Aug 1, 2024
1 parent ab4f37e commit ad01edd
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 233 deletions.
20 changes: 0 additions & 20 deletions compiler/bin-js_of_ocaml/cmd_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compiler/bin-js_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions compiler/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions compiler/lib/link_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
106 changes: 106 additions & 0 deletions compiler/lib/source_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 13 additions & 0 deletions compiler/lib/source_map.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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]. *)
35 changes: 0 additions & 35 deletions compiler/lib/source_map_io.mli

This file was deleted.

28 changes: 0 additions & 28 deletions compiler/lib/source_map_io.unsupported.ml

This file was deleted.

Loading

0 comments on commit ad01edd

Please sign in to comment.