diff --git a/compiler/lib/js_output.ml b/compiler/lib/js_output.ml index 8c2da5f4bb..4841af6952 100644 --- a/compiler/lib/js_output.ml +++ b/compiler/lib/js_output.ml @@ -1903,7 +1903,7 @@ let program ?(accept_unnamed_var = false) f ?source_map p = let temp_mappings = ref [] in let files = Hashtbl.create 17 in let names = Hashtbl.create 17 in - let contents : string option list ref option = + let contents : Source_map.Source_text.t list ref option = match source_map with | None | Some { Source_map.sources_contents = None; _ } -> None | Some { Source_map.sources_contents = Some _; _ } -> Some (ref []) @@ -1918,30 +1918,28 @@ let program ?(accept_unnamed_var = false) f ?source_map p = | [], _ -> () | x :: xs, [] -> Hashtbl.add files x (Hashtbl.length files); - Option.iter contents ~f:(fun r -> r := None :: !r); + Option.iter contents ~f:(fun r -> r := Source_map.Source_text.empty :: !r); loop xs [] | x :: xs, y :: ys -> Hashtbl.add files x (Hashtbl.length files); Option.iter contents ~f:(fun r -> r := y :: !r); loop xs ys in - let sources_contents = - Option.map ~f:(List.map ~f:Source_map.Source_text.decode) sm.sources_contents - in - loop sm.sources (Option.value ~default:[] sources_contents); + loop sm.sources (Option.value ~default:[] sm.sources_contents); List.iter sm.Source_map.names ~f:(fun f -> Hashtbl.add names f (Hashtbl.length names)); true in let find_source file = - match Builtins.find file with - | Some f -> Some (Builtins.File.content f) - | None -> - if Sys.file_exists file && not (Sys.is_directory file) - then - let content = Fs.read_file file in - Some content - else None + Source_map.Source_text.encode ( + match Builtins.find file with + | Some f -> Some (Builtins.File.content f) + | None -> + if Sys.file_exists file && not (Sys.is_directory file) + then + let content = Fs.read_file file in + Some content + else None) in ( (fun pos m -> temp_mappings := (pos, m) :: !temp_mappings) , (fun file -> @@ -1985,7 +1983,7 @@ let program ?(accept_unnamed_var = false) f ?source_map p = let sources_contents = let open Option.Syntax in let* r = contents in - Option.return (List.map ~f:Source_map.Source_text.encode (List.rev !r)) + Option.return (List.rev !r) in let sources = List.map sources ~f:(fun filename -> diff --git a/compiler/lib/source_map.ml b/compiler/lib/source_map.ml index 163231231c..ac62c5d0cf 100644 --- a/compiler/lib/source_map.ml +++ b/compiler/lib/source_map.ml @@ -1,3 +1,4 @@ + (* Js_of_ocaml compiler * http://www.ocsigen.org/js_of_ocaml/ * Copyright (C) 2013 Hugo Heuzard @@ -566,6 +567,8 @@ module Source_text = struct external of_json_string : string -> t = "%identity" external to_json_string : t -> string = "%identity" + + let empty = Uninterpreted "" let to_json = function diff --git a/compiler/lib/source_map.mli b/compiler/lib/source_map.mli index e220619713..c40a0e8dc5 100644 --- a/compiler/lib/source_map.mli +++ b/compiler/lib/source_map.mli @@ -79,6 +79,8 @@ end module Source_text : sig type t + val empty : t + val of_json_string : string -> t (** By default, sources contents are left uninterpreted as decoding this field can be costly if the amount of code is large, and is seldom required. It is guaranteed that