Skip to content

Commit

Permalink
Source maps: avoid some code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Nov 5, 2024
1 parent eca029f commit 04db0cc
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 184 deletions.
2 changes: 1 addition & 1 deletion compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ let add_source_map sourcemap_don't_inline_content z opt_source_map_file =
Zip.add_file z ~name:"source_map.map" ~file;
if not sourcemap_don't_inline_content
then
let sm = Wa_source_map.load file in
let sm = Source_map.of_file file in
Wa_source_map.iter_sources sm (fun i j file ->
if Sys.file_exists file && not (Sys.is_directory file)
then
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib/source_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ module Mappings = struct

let empty = Uninterpreted ""

let is_empty (Uninterpreted s) = String.equal s ""

let of_string_unsafe : string -> t = fun s -> Uninterpreted s

let to_string : t -> string = fun (Uninterpreted s) -> s
Expand Down
3 changes: 3 additions & 0 deletions compiler/lib/source_map.mli
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ module Mappings : sig
val empty : t
(** The empty mapping. *)

val is_empty : t -> bool
(** Test whether the mapping is empty. *)

val of_string_unsafe : string -> t
(** [of_string_unsafe] does not perform any
validation of its argument, unlike {!val:decode}. It is guaranteed that
Expand Down
20 changes: 19 additions & 1 deletion compiler/lib/vlq64.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open! Stdlib
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

let code_rev =
let a = Array.make 255 (-1) in
let a = Array.make 256 (-1) in
for i = 0 to String.length alphabet - 1 do
a.(Char.code alphabet.[i]) <- i
done;
Expand Down Expand Up @@ -99,3 +99,21 @@ let decode_l s ~pos ~len =
aux i (d :: acc) len
in
aux pos [] len

type input =
{ string : string
; mutable pos : int
; len : int
}

let rec decode' src s pos offset i =
let digit = Array.unsafe_get code_rev (Char.code s.[pos]) in
if digit = -1 then invalid_arg "Vql64.decode'";
let i = i + ((digit land vlq_base_mask) lsl offset) in
if digit >= vlq_continuation_bit
then decode' src s (pos + 1) (offset + vlq_base_shift) i
else (
src.pos <- pos + 1;
i)

let decode src = fromVLQSigned (decode' src src.string src.pos 0 0)
10 changes: 10 additions & 0 deletions compiler/lib/vlq64.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@

val in_alphabet : char -> bool

type input =
{ string : string
; mutable pos : int
; len : int
}

val encode : Buffer.t -> int -> unit

val encode_l : Buffer.t -> int list -> unit

val decode : input -> int

val decode_l : string -> pos:int -> len:int -> int list
8 changes: 4 additions & 4 deletions compiler/lib/wasm/wa_link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ let source_name i j file =
let extract_source_map ~dir ~name z =
if Zip.has_entry z ~name:"source_map.map"
then (
let sm = Wa_source_map.parse (Zip.read_entry z ~name:"source_map.map") in
let sm = Source_map.of_string (Zip.read_entry z ~name:"source_map.map") in
let sm =
let rewrite_path path =
if Filename.is_relative path
Expand All @@ -590,7 +590,7 @@ let extract_source_map ~dir ~name z =
if Zip.has_entry z ~name then Some (Zip.read_entry z ~name) else None)
in
let map_name = name ^ ".wasm.map" in
Wa_source_map.write (Filename.concat dir map_name) sm;
Source_map.to_file sm (Filename.concat dir map_name);
Wasm_binary.append_source_map_section
~file:(Filename.concat dir (name ^ ".wasm"))
~url:map_name)
Expand Down Expand Up @@ -860,7 +860,7 @@ let rec get_source_map_files files src_index =
if Zip.has_entry z ~name:"source_map.map"
then
let data = Zip.read_entry z ~name:"source_map.map" in
let sm = Wa_source_map.parse data in
let sm = Source_map.of_string data in
if not (Wa_source_map.is_empty sm)
then (
let l = ref [] in
Expand All @@ -879,7 +879,7 @@ let add_source_map files z opt_source_map_file =
Option.iter
~f:(fun file ->
Zip.add_file z ~name:"source_map.map" ~file;
let sm = Wa_source_map.load file in
let sm = Source_map.of_file file in
let files = Array.of_list files in
let src_index = ref 0 in
let st = ref None in
Expand Down
Loading

0 comments on commit 04db0cc

Please sign in to comment.