Skip to content

Commit 77c0fab

Browse files
hhugoOlivierNicole
authored andcommitted
Misc: yojson is no longer optional
1 parent 01ba6db commit 77c0fab

File tree

14 files changed

+133
-233
lines changed

14 files changed

+133
-233
lines changed

compiler/bin-js_of_ocaml/cmd_arg.ml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,6 @@ let options =
319319
} )
320320
else None
321321
in
322-
let source_map =
323-
if Option.is_some source_map && not Source_map_io.enabled
324-
then (
325-
warn
326-
"Warning: '--source-map' flag ignored because js_of_ocaml was compiled without \
327-
sourcemap support (install yojson to enable support)\n\
328-
%!";
329-
None)
330-
else source_map
331-
in
332322
let params : (string * string) list = List.flatten set_param in
333323
let static_env : (string * string) list = List.flatten set_env in
334324
let include_dirs = normalize_include_dirs include_dirs in
@@ -559,16 +549,6 @@ let options_runtime_only =
559549
} )
560550
else None
561551
in
562-
let source_map =
563-
if Option.is_some source_map && not Source_map_io.enabled
564-
then (
565-
warn
566-
"Warning: '--source-map' flag ignored because js_of_ocaml was compiled without \
567-
sourcemap support (install yojson to enable support)\n\
568-
%!";
569-
None)
570-
else source_map
571-
in
572552
let params : (string * string) list = List.flatten set_param in
573553
let static_env : (string * string) list = List.flatten set_env in
574554
let include_dirs = normalize_include_dirs include_dirs in

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ let output_gen ~standalone ~custom_header ~build_info ~source_map output_file f
5050
let urlData =
5151
match output_file with
5252
| None ->
53-
let data = Source_map_io.to_string sm in
53+
let data = Source_map.to_string sm in
5454
"data:application/json;base64," ^ Base64.encode_exn data
5555
| Some output_file ->
56-
Source_map_io.to_file sm ~file:output_file;
56+
Source_map.to_file sm ~file:output_file;
5757
Filename.basename output_file
5858
in
5959
Pretty_print.newline fmt;

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f
3030
if Option.is_some sourcemap_root || not sourcemap_don't_inline_content
3131
then (
3232
let open Source_map in
33-
let source_map, mappings = Source_map_io.of_file_no_mappings sourcemap_file in
33+
let source_map, mappings = Source_map.of_file_no_mappings sourcemap_file in
3434
assert (List.is_empty (Option.value source_map.sources_content ~default:[]));
3535
(* Add source file contents to source map *)
3636
let sources_content =
@@ -50,7 +50,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f
5050
(if Option.is_some sourcemap_root then sourcemap_root else source_map.sourceroot)
5151
}
5252
in
53-
Source_map_io.to_file ?mappings source_map ~file:sourcemap_file)
53+
Source_map.to_file ?mappings source_map ~file:sourcemap_file)
5454

5555
let opt_with action x f =
5656
match x with

compiler/lib/dune

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
compiler-libs.bytecomp
88
menhirLib
99
sedlex
10-
(select
11-
source_map_io.ml
12-
from
13-
(yojson -> source_map_io.yojson.ml)
14-
(-> source_map_io.unsupported.ml)))
10+
yojson)
1511
(flags
1612
(:standard -w -7-37 -safe-string))
1713
(preprocess

compiler/lib/link_js.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ let action ~resolve_sourcemap_url ~drop_source_map file line =
177177
| `Build_info bi, _ -> Build_info bi
178178
| (`Json_base64 _ | `Url _), true -> Drop
179179
| `Json_base64 offset, false ->
180-
Source_map (Source_map_io.of_string (Base64.decode_exn ~off:offset line))
180+
Source_map (Source_map.of_string (Base64.decode_exn ~off:offset line))
181181
| `Url _, false when not resolve_sourcemap_url -> Drop
182182
| `Url offset, false ->
183183
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 =
186186
let l = in_channel_length ic in
187187
let content = really_input_string ic l in
188188
close_in ic;
189-
Source_map (Source_map_io.of_string content)
189+
Source_map (Source_map.of_string content)
190190

191191
module Units : sig
192192
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
465465
in
466466
match file with
467467
| None ->
468-
let data = Source_map_io.to_string sm in
468+
let data = Source_map.to_string sm in
469469
let s = sourceMappingURL_base64 ^ Base64.encode_exn data in
470470
Line_writer.write oc s
471471
| Some file ->
472-
Source_map_io.to_file sm ~file;
472+
Source_map.to_file sm ~file;
473473
let s = sourceMappingURL ^ Filename.basename file in
474474
Line_writer.write oc s));
475475
if times () then Format.eprintf " sourcemap: %a@." Timer.print t

compiler/lib/source_map.ml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,109 @@ let merge = function
298298
; names = List.rev acc_rev.names
299299
; sources_content = Option.map ~f:List.rev acc_rev.sources_content
300300
}
301+
302+
(* IO *)
303+
304+
let json ?replace_mappings t =
305+
let rewrite_path path =
306+
if Filename.is_relative path
307+
then path
308+
else
309+
match Build_path_prefix_map.get_build_path_prefix_map () with
310+
| Some map -> Build_path_prefix_map.rewrite map path
311+
| None -> path
312+
in
313+
`Assoc
314+
[ "version", `Float (float_of_int t.version)
315+
; "file", `String (rewrite_path t.file)
316+
; ( "sourceRoot"
317+
, `String
318+
(match t.sourceroot with
319+
| None -> ""
320+
| Some s -> rewrite_path s) )
321+
; "names", `List (List.map t.names ~f:(fun s -> `String s))
322+
; "sources", `List (List.map t.sources ~f:(fun s -> `String (rewrite_path s)))
323+
; ( "mappings"
324+
, `String (Option.value ~default:(string_of_mapping t.mappings) replace_mappings) )
325+
; ( "sourcesContent"
326+
, `List
327+
(match t.sources_content with
328+
| None -> []
329+
| Some l ->
330+
List.map l ~f:(function
331+
| None -> `Null
332+
| Some s -> `String s)) )
333+
]
334+
335+
let invalid () = invalid_arg "Source_map.of_json"
336+
337+
let string name rest =
338+
try
339+
match List.assoc name rest with
340+
| `String s -> Some s
341+
| `Null -> None
342+
| _ -> invalid ()
343+
with Not_found -> None
344+
345+
let list_string name rest =
346+
try
347+
match List.assoc name rest with
348+
| `List l ->
349+
Some
350+
(List.map l ~f:(function
351+
| `String s -> s
352+
| _ -> invalid ()))
353+
| _ -> invalid ()
354+
with Not_found -> None
355+
356+
let list_string_opt name rest =
357+
try
358+
match List.assoc name rest with
359+
| `List l ->
360+
Some
361+
(List.map l ~f:(function
362+
| `String s -> Some s
363+
| `Null -> None
364+
| _ -> invalid ()))
365+
| _ -> invalid ()
366+
with Not_found -> None
367+
368+
let of_json ~parse_mappings json =
369+
let parse ~version rest =
370+
let def v d =
371+
match v with
372+
| None -> d
373+
| Some v -> v
374+
in
375+
let file = string "file" rest in
376+
let sourceroot = string "sourceRoot" rest in
377+
let names = list_string "names" rest in
378+
let sources = list_string "sources" rest in
379+
let sources_content = list_string_opt "sourcesContent" rest in
380+
let mappings = string "mappings" rest in
381+
( { version
382+
; file = def file ""
383+
; sourceroot
384+
; names = def names []
385+
; sources_content
386+
; sources = def sources []
387+
; mappings = mapping_of_string (def mappings "")
388+
}
389+
, if parse_mappings then None else mappings )
390+
in
391+
match json with
392+
| `Assoc (("version", `Float version) :: rest) when int_of_float version = 3 ->
393+
parse ~version:3 rest
394+
| `Assoc (("version", `Int 3) :: rest) -> parse ~version:3 rest
395+
| _ -> invalid ()
396+
397+
let of_string s = of_json ~parse_mappings:true (Yojson.Basic.from_string s) |> fst
398+
399+
let to_string m = Yojson.Basic.to_string (json m)
400+
401+
let to_file ?mappings m ~file =
402+
let replace_mappings = mappings in
403+
Yojson.Basic.to_file file (json ?replace_mappings m)
404+
405+
let of_file_no_mappings filename =
406+
of_json ~parse_mappings:false (Yojson.Basic.from_file filename)

compiler/lib/source_map.mli

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,16 @@ val mapping_of_string : string -> mapping
5959
val string_of_mapping : mapping -> string
6060

6161
val empty : filename:string -> t
62+
63+
val to_string : t -> string
64+
65+
val of_string : string -> t
66+
67+
val of_file_no_mappings : string -> t * string option
68+
(** Read source map from a file without parsing the mappings (which can be costly). The
69+
[mappings] field is returned empty and the raw string is returned alongside the map.
70+
*)
71+
72+
val to_file : ?mappings:string -> t -> file:string -> unit
73+
(** Write to a file. If a string is supplied as [mappings], use it instead of the
74+
sourcemap's [mappings]. *)

compiler/lib/source_map_io.mli

Lines changed: 0 additions & 35 deletions
This file was deleted.

compiler/lib/source_map_io.unsupported.ml

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)