package js_of_ocaml-compiler

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

Source file source_map.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2013 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! Stdlib

type map =
  | Gen of
      { gen_line : int
      ; gen_col : int
      }
  | Gen_Ori of
      { gen_line : int
      ; gen_col : int
      ; ori_source : int
      ; ori_line : int
      ; ori_col : int
      }
  | Gen_Ori_Name of
      { gen_line : int
      ; gen_col : int
      ; ori_source : int
      ; ori_line : int
      ; ori_col : int
      ; ori_name : int
      }

type mapping = map list

type t =
  { version : int
  ; file : string
  ; sourceroot : string option
  ; sources : string list
  ; sources_content : string option list option
  ; names : string list
  ; mappings : mapping
  }

let empty ~filename =
  { version = 3
  ; file = filename
  ; sourceroot = None
  ; sources = []
  ; sources_content = None
  ; names = []
  ; mappings = []
  }

let gen_line = function
  | Gen { gen_line; _ } | Gen_Ori { gen_line; _ } | Gen_Ori_Name { gen_line; _ } ->
      gen_line

let gen_col = function
  | Gen { gen_col; _ } | Gen_Ori { gen_col; _ } | Gen_Ori_Name { gen_col; _ } -> gen_col

let string_of_mapping mapping =
  let a = Array.of_list mapping in
  let len = Array.length a in
  Array.stable_sort
    ~cmp:(fun t1 t2 ->
      match compare (gen_line t1) (gen_line t2) with
      | 0 -> compare (gen_col t1) (gen_col t2)
      | n -> n)
    a;
  let buf = Buffer.create 1024 in
  (* The binary format encodes lines starting at zero, but
     [ori_line] and [gen_line] are 1 based. *)
  let gen_line_r = ref 1 in
  let gen_col_r = ref 0 in
  let ori_source_r = ref 0 in
  let ori_line_r = ref 1 in
  let ori_col_r = ref 0 in
  let ori_name_r = ref 0 in
  let rec loop prev i =
    if i < len
    then
      let c = a.(i) in
      if i + 1 < len && gen_line c = gen_line a.(i + 1) && gen_col c = gen_col a.(i + 1)
      then (* Only keep one source location per generated location *)
        loop prev (i + 1)
      else (
        if !gen_line_r <> gen_line c
        then (
          assert (!gen_line_r < gen_line c);
          for _i = !gen_line_r to gen_line c - 1 do
            Buffer.add_char buf ';'
          done;
          gen_col_r := 0;
          gen_line_r := gen_line c)
        else if i > 0
        then Buffer.add_char buf ',';
        let l =
          match c with
          | Gen { gen_line = _; gen_col } ->
              let res = [ gen_col - !gen_col_r ] in
              gen_col_r := gen_col;
              res
          | Gen_Ori { gen_line = _; gen_col; ori_source; ori_line; ori_col } ->
              let res =
                [ gen_col - !gen_col_r
                ; ori_source - !ori_source_r
                ; ori_line - !ori_line_r
                ; ori_col - !ori_col_r
                ]
              in
              gen_col_r := gen_col;
              ori_col_r := ori_col;
              ori_line_r := ori_line;
              ori_source_r := ori_source;
              res
          | Gen_Ori_Name
              { gen_line = _; gen_col; ori_source; ori_line; ori_col; ori_name } ->
              let res =
                [ gen_col - !gen_col_r
                ; ori_source - !ori_source_r
                ; ori_line - !ori_line_r
                ; ori_col - !ori_col_r
                ; ori_name - !ori_name_r
                ]
              in
              gen_col_r := gen_col;
              ori_col_r := ori_col;
              ori_line_r := ori_line;
              ori_source_r := ori_source;
              ori_name_r := ori_name;
              res
        in
        Vlq64.encode_l buf l;
        loop i (i + 1))
  in
  loop (-1) 0;
  Buffer.contents buf

let mapping_of_string str =
  let total_len = String.length str in
  let gen_col = ref 0 in
  let ori_source = ref 0 in
  let ori_line = ref 1 in
  let ori_col = ref 0 in
  let ori_name = ref 0 in
  let rec readline line pos acc =
    if pos >= total_len
    then List.rev acc
    else
      let last = try String.index_from str pos ';' with Not_found -> total_len in
      gen_col := 0;
      let pos, acc = if pos = last then pos + 1, acc else read_tokens line pos last acc in
      readline (succ line) pos acc
  and read_tokens line start stop acc =
    let last = try min (String.index_from str start ',') stop with Not_found -> stop in
    let v = Vlq64.decode_l str ~pos:start ~len:(last - start) in
    match v with
    | [] -> last + 1, acc
    | v ->
        let v =
          match v with
          | [ g ] ->
              gen_col := !gen_col + g;
              Gen { gen_line = line; gen_col = !gen_col }
          | [ g; os; ol; oc ] ->
              gen_col := !gen_col + g;
              ori_source := !ori_source + os;
              ori_line := !ori_line + ol;
              ori_col := !ori_col + oc;
              Gen_Ori
                { gen_line = line
                ; gen_col = !gen_col
                ; ori_source = !ori_source
                ; ori_line = !ori_line
                ; ori_col = !ori_col
                }
          | [ g; os; ol; oc; on ] ->
              gen_col := !gen_col + g;
              ori_source := !ori_source + os;
              ori_line := !ori_line + ol;
              ori_col := !ori_col + oc;
              ori_name := !ori_name + on;
              Gen_Ori_Name
                { gen_line = line
                ; gen_col = !gen_col
                ; ori_source = !ori_source
                ; ori_line = !ori_line
                ; ori_col = !ori_col
                ; ori_name = !ori_name
                }
          | _ -> invalid_arg "Source_map.mapping_of_string"
        in
        let acc = v :: acc in
        if last = stop then last + 1, acc else read_tokens line (last + 1) stop acc
  in
  (* The binary format encodes lines starting at zero, but
     [ori_line] and [gen_line] are 1 based. *)
  readline 1 0 []

let maps ~sources_offset ~names_offset x =
  match x with
  | Gen _ -> x
  | Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col } ->
      let ori_source = ori_source + sources_offset in
      Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col }
  | Gen_Ori_Name { gen_line; gen_col; ori_source; ori_line; ori_col; ori_name } ->
      let ori_source = ori_source + sources_offset in
      let ori_name = ori_name + names_offset in
      Gen_Ori_Name { gen_line; gen_col; ori_source; ori_line; ori_col; ori_name }

let filter_map sm ~f =
  let a = Array.of_list sm.mappings in
  Array.stable_sort
    ~cmp:(fun t1 t2 ->
      match compare (gen_line t1) (gen_line t2) with
      | 0 -> compare (gen_col t1) (gen_col t2)
      | n -> n)
    a;
  let l = Array.to_list a |> List.group ~f:(fun a b -> gen_line a = gen_line b) in

  let rec loop acc mapping =
    match mapping with
    | [] -> List.rev acc
    | x :: xs ->
        let gen_line = gen_line (List.hd x) in
        let acc =
          match f gen_line with
          | None -> acc
          | Some gen_line ->
              List.rev_append_map
                x
                ~f:(function
                  | Gen { gen_line = _; gen_col } -> Gen { gen_line; gen_col }
                  | Gen_Ori { gen_line = _; gen_col; ori_source; ori_line; ori_col } ->
                      Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col }
                  | Gen_Ori_Name
                      { gen_line = _; gen_col; ori_source; ori_line; ori_col; ori_name }
                    ->
                      Gen_Ori_Name
                        { gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
                acc
        in
        loop acc xs
  in
  let mappings = loop [] l in
  { sm with mappings }

let merge = function
  | [] -> None
  | _ :: _ as l ->
      let rec loop acc_rev ~sources_offset ~names_offset l =
        match l with
        | [] -> acc_rev
        | sm :: rest ->
            let acc_rev =
              { acc_rev with
                sources = List.rev_append sm.sources acc_rev.sources
              ; names = List.rev_append sm.names acc_rev.names
              ; sources_content =
                  (match sm.sources_content, acc_rev.sources_content with
                  | Some x, Some acc_rev -> Some (List.rev_append x acc_rev)
                  | None, _ | _, None -> None)
              ; mappings =
                  List.rev_append_map
                    ~f:(maps ~sources_offset ~names_offset)
                    sm.mappings
                    acc_rev.mappings
              }
            in
            loop
              acc_rev
              ~sources_offset:(sources_offset + List.length sm.sources)
              ~names_offset:(names_offset + List.length sm.names)
              rest
      in
      let acc_rev =
        loop
          { (empty ~filename:"") with sources_content = Some [] }
          ~sources_offset:0
          ~names_offset:0
          l
      in
      Some
        { acc_rev with
          mappings = List.rev acc_rev.mappings
        ; sources = List.rev acc_rev.sources
        ; names = List.rev acc_rev.names
        ; sources_content = Option.map ~f:List.rev acc_rev.sources_content
        }
OCaml

Innovation. Community. Security.