package js_of_ocaml-compiler

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file source_map_io.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 1 "compiler/lib/source_map_yojson.ml.in"
(* 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

(* copied from ocaml/utils/misc.ml *)
let get_build_path_prefix_map =
  let init = ref false in
  let map_cache = ref None in
  fun () ->
    if not !init then begin
      init := true;
      match Sys.getenv "BUILD_PATH_PREFIX_MAP" with
      | exception Not_found -> ()
      | encoded_map ->
        match Build_path_prefix_map.decode_map encoded_map with
          | Error err ->
              failwith @@ Printf.sprintf
                "Invalid value for the environment variable \
                 BUILD_PATH_PREFIX_MAP: %s" err
          | Ok map -> map_cache := Some map
    end;
    !map_cache

let json t =
  let rewrite_path path =
    if Filename.is_relative path then
      path
    else
      match 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);
     "mappings",      `String (string_of_mapping t.mappings);
     "sources",       `List (List.map (fun s -> `String (rewrite_path s)) t.sources);
     "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.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 json =
  match json with
  | `Assoc (("version", `Float version)
        :: rest) when int_of_float version = 3 ->
    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 = int_of_float version;
      file = def file "";
      sourceroot;
      names = def names [];
      sources_content = sources_content;
      sources = def sources [];
      mappings = mapping_of_string (def mappings "");
    }
  | _ -> invalid ()

let of_string s =
  of_json (Yojson.Basic.from_string s)

let to_string m =
  Yojson.Basic.to_string (json m)

let to_file m file =
  Yojson.Basic.to_file file (json m)

let enabled = true