Skip to content

Commit ab1faf2

Browse files
authored
Merge pull request #129 from ocaml-wasm/wasm-source-maps
Source maps: clean-up
2 parents 370ef42 + b5ae6af commit ab1faf2

File tree

9 files changed

+140
-160
lines changed

9 files changed

+140
-160
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,20 @@ let build_js_runtime ~primitives ?runtime_arguments () =
234234
in
235235
prelude ^ launcher
236236

237-
let add_source_map sourcemap_don't_inline_content z opt_source_map_file =
238-
Option.iter
239-
~f:(fun file ->
240-
Zip.add_file z ~name:"source_map.map" ~file;
237+
let add_source_map sourcemap_don't_inline_content z opt_source_map =
238+
let sm =
239+
match opt_source_map with
240+
| `File opt_file ->
241+
Option.map opt_file ~f:(fun file ->
242+
Zip.add_file z ~name:"source_map.map" ~file;
243+
Source_map.of_file file)
244+
| `Source_map sm ->
245+
Zip.add_entry z ~name:"source_map.map" ~contents:(Source_map.to_string sm);
246+
Some sm
247+
in
248+
Option.iter sm ~f:(fun sm ->
241249
if not sourcemap_don't_inline_content
242250
then
243-
let sm = Source_map.of_file file in
244251
Wasm_source_map.iter_sources sm (fun i j file ->
245252
if Sys.file_exists file && not (Sys.is_directory file)
246253
then
@@ -249,7 +256,6 @@ let add_source_map sourcemap_don't_inline_content z opt_source_map_file =
249256
z
250257
~name:(Link.source_name i j file)
251258
~contents:(Yojson.Basic.to_string (`String sm))))
252-
opt_source_map_file
253259

254260
let run
255261
{ Cmd_arg.common
@@ -488,7 +494,7 @@ let run
488494
let compile_cmo' z cmo =
489495
compile_cmo cmo (fun unit_data _ tmp_wasm_file opt_tmp_map_file ->
490496
Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
491-
add_source_map sourcemap_don't_inline_content z opt_tmp_map_file;
497+
add_source_map sourcemap_don't_inline_content z (`File opt_tmp_map_file);
492498
unit_data)
493499
in
494500
let unit_data = [ compile_cmo' z cmo ] in
@@ -499,6 +505,7 @@ let run
499505
@@ fun tmp_output_file ->
500506
let z = Zip.open_out tmp_output_file in
501507
let unit_data =
508+
let tmp_buf = Buffer.create 10000 in
502509
List.fold_right
503510
~f:(fun cmo cont l ->
504511
compile_cmo cmo
@@ -508,26 +515,26 @@ let run
508515
~init:(fun l ->
509516
Fs.with_intermediate_file (Filename.temp_file "wasm" ".wasm")
510517
@@ fun tmp_wasm_file ->
511-
opt_with
512-
Fs.with_intermediate_file
513-
(if enable_source_maps
514-
then Some (Filename.temp_file "wasm" ".map")
515-
else None)
516-
@@ fun opt_output_sourcemap_file ->
517518
let l = List.rev l in
518-
Wasm_link.f
519-
(List.map
520-
~f:(fun (_, _, file, opt_source_map) ->
521-
{ Wasm_link.module_name = "OCaml"
522-
; file
523-
; code = None
524-
; opt_source_map = Option.map ~f:(fun f -> `File f) opt_source_map
525-
})
526-
l)
527-
~output_file:tmp_wasm_file
528-
~opt_output_sourcemap_file;
519+
let source_map =
520+
Wasm_link.f
521+
(List.map
522+
~f:(fun (_, _, file, opt_source_map) ->
523+
{ Wasm_link.module_name = "OCaml"
524+
; file
525+
; code = None
526+
; opt_source_map =
527+
Option.map
528+
~f:(fun f -> Source_map.Standard.of_file ~tmp_buf f)
529+
opt_source_map
530+
})
531+
l)
532+
~output_file:tmp_wasm_file
533+
in
529534
Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
530-
add_source_map sourcemap_don't_inline_content z opt_output_sourcemap_file;
535+
if enable_source_maps
536+
then
537+
add_source_map sourcemap_don't_inline_content z (`Source_map source_map);
531538
List.map ~f:(fun (unit_data, _, _, _) -> unit_data) l)
532539
[]
533540
in

compiler/lib-wasm/link.ml

Lines changed: 63 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -849,62 +849,51 @@ let link ~output_file ~linkall ~enable_source_maps ~files =
849849
if times () then Format.eprintf " build JS runtime: %a@." Timer.print t1;
850850
if times () then Format.eprintf " emit: %a@." Timer.print t
851851

852-
let opt_with action x f =
853-
match x with
854-
| None -> f None
855-
| Some x -> action x (fun y -> f (Some y))
856-
857-
let rec get_source_map_files files src_index =
852+
let rec get_source_map_files ~tmp_buf files src_index =
858853
let z = Zip.open_in files.(!src_index) in
859854
incr src_index;
860-
if Zip.has_entry z ~name:"source_map.map"
861-
then
862-
let data = Zip.read_entry z ~name:"source_map.map" in
863-
let sm = Source_map.of_string data in
864-
if not (Wasm_source_map.is_empty sm)
865-
then (
866-
let l = ref [] in
867-
Wasm_source_map.iter_sources sm (fun i j file -> l := source_name i j file :: !l);
868-
if not (List.is_empty !l)
869-
then z, Array.of_list (List.rev !l)
870-
else (
871-
Zip.close_in z;
872-
get_source_map_files files src_index))
873-
else (
874-
Zip.close_in z;
875-
get_source_map_files files src_index)
876-
else get_source_map_files files src_index
877-
878-
let add_source_map files z opt_source_map_file =
879-
Option.iter
880-
~f:(fun file ->
881-
Zip.add_file z ~name:"source_map.map" ~file;
882-
let sm = Source_map.of_file file in
883-
let files = Array.of_list files in
884-
let src_index = ref 0 in
885-
let st = ref None in
886-
let finalize () =
855+
let l = ref [] in
856+
(if Zip.has_entry z ~name:"source_map.map"
857+
then
858+
let data = Zip.read_entry z ~name:"source_map.map" in
859+
let sm = Source_map.Standard.of_string ~tmp_buf data in
860+
if not (Wasm_source_map.is_empty sm)
861+
then
862+
Wasm_source_map.iter_sources (Standard sm) (fun i j file ->
863+
l := source_name i j file :: !l));
864+
if not (List.is_empty !l)
865+
then z, Array.of_list (List.rev !l)
866+
else (
867+
Zip.close_in z;
868+
get_source_map_files ~tmp_buf files src_index)
869+
870+
let add_source_map files z sm =
871+
let tmp_buf = Buffer.create 10000 in
872+
Zip.add_entry z ~name:"source_map.map" ~contents:(Source_map.to_string sm);
873+
let files = Array.of_list files in
874+
let src_index = ref 0 in
875+
let st = ref None in
876+
let finalize () =
877+
match !st with
878+
| Some (_, (z', _)) -> Zip.close_in z'
879+
| None -> ()
880+
in
881+
Wasm_source_map.iter_sources sm (fun i j file ->
882+
let z', files =
887883
match !st with
888-
| Some (_, (z', _)) -> Zip.close_in z'
889-
| None -> ()
884+
| Some (i', st) when Poly.equal i i' -> st
885+
| _ ->
886+
let st' = get_source_map_files ~tmp_buf files src_index in
887+
finalize ();
888+
st := Some (i, st');
889+
st'
890890
in
891-
Wasm_source_map.iter_sources sm (fun i j file ->
892-
let z', files =
893-
match !st with
894-
| Some (i', st) when Poly.equal i i' -> st
895-
| _ ->
896-
let st' = get_source_map_files files src_index in
897-
finalize ();
898-
st := Some (i, st');
899-
st'
900-
in
901-
if Array.length files > 0 (* Source has source map *)
902-
then
903-
let name = files.(Option.value ~default:0 j) in
904-
if Zip.has_entry z' ~name
905-
then Zip.copy_file z' z ~src_name:name ~dst_name:(source_name i j file));
906-
finalize ())
907-
opt_source_map_file
891+
if Array.length files > 0 (* Source has source map *)
892+
then
893+
let name = files.(Option.value ~default:0 j) in
894+
if Zip.has_entry z' ~name
895+
then Zip.copy_file z' z ~src_name:name ~dst_name:(source_name i j file));
896+
finalize ()
908897

909898
let make_library ~output_file ~enable_source_maps ~files =
910899
let info =
@@ -935,33 +924,31 @@ let make_library ~output_file ~enable_source_maps ~files =
935924
@@ fun tmp_output_file ->
936925
let z = Zip.open_out tmp_output_file in
937926
add_info z ~build_info ~unit_data ();
938-
(*
939-
- Merge all code files into a single code file (gathering source maps)
940-
- Copy source files
941-
*)
942927
Fs.with_intermediate_file (Filename.temp_file "wasm" ".wasm")
943928
@@ fun tmp_wasm_file ->
944-
opt_with
945-
Fs.with_intermediate_file
946-
(if enable_source_maps then Some (Filename.temp_file "wasm" ".map") else None)
947-
@@ fun opt_output_sourcemap_file ->
948-
Wasm_link.f
949-
(List.map
950-
~f:(fun file ->
951-
let z' = Zip.open_in file in
952-
{ Wasm_link.module_name = "OCaml"
953-
; file
954-
; code = Some (Zip.read_entry z' ~name:"code.wasm")
955-
; opt_source_map =
956-
(if Zip.has_entry z' ~name:"source_map.map"
957-
then Some (`Data (Zip.read_entry z' ~name:"source_map.map"))
958-
else None)
959-
})
960-
files)
961-
~output_file:tmp_wasm_file
962-
~opt_output_sourcemap_file;
929+
let output_sourcemap =
930+
Wasm_link.f
931+
(let tmp_buf = Buffer.create 10000 in
932+
List.map
933+
~f:(fun file ->
934+
let z' = Zip.open_in file in
935+
{ Wasm_link.module_name = "OCaml"
936+
; file
937+
; code = Some (Zip.read_entry z' ~name:"code.wasm")
938+
; opt_source_map =
939+
(if enable_source_maps && Zip.has_entry z' ~name:"source_map.map"
940+
then
941+
Some
942+
(Source_map.Standard.of_string
943+
~tmp_buf
944+
(Zip.read_entry z' ~name:"source_map.map"))
945+
else None)
946+
})
947+
files)
948+
~output_file:tmp_wasm_file
949+
in
963950
Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
964-
add_source_map files z opt_output_sourcemap_file;
951+
if enable_source_maps then add_source_map files z output_sourcemap;
965952
Zip.close_out z
966953

967954
let link ~output_file ~linkall ~mklib ~enable_source_maps ~files =

compiler/lib-wasm/wasm_link.ml

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ type t =
15151515
{ module_name : string
15161516
; file : string
15171517
; contents : Read.t
1518-
; source_map_contents : Source_map.t option
1518+
; source_map_contents : Source_map.Standard.t option
15191519
}
15201520

15211521
type import_status =
@@ -1874,12 +1874,11 @@ type input =
18741874
{ module_name : string
18751875
; file : string
18761876
; code : string option
1877-
; opt_source_map : [ `File of string | `Data of string ] option
1877+
; opt_source_map : Source_map.Standard.t option
18781878
}
18791879

1880-
let f files ~output_file ~opt_output_sourcemap_file =
1880+
let f files ~output_file =
18811881
let files =
1882-
let tmp_buf = Buffer.create 10000 in
18831882
Array.map
18841883
~f:(fun { module_name; file; code; opt_source_map } ->
18851884
let data =
@@ -1888,17 +1887,7 @@ let f files ~output_file ~opt_output_sourcemap_file =
18881887
| Some data -> data
18891888
in
18901889
let contents = Read.open_in file data in
1891-
{ module_name
1892-
; file
1893-
; contents
1894-
; source_map_contents =
1895-
Option.map
1896-
~f:(fun src ->
1897-
match src with
1898-
| `File file -> Source_map.of_file ~tmp_buf file
1899-
| `Data data -> Source_map.of_string ~tmp_buf data)
1900-
opt_source_map
1901-
})
1890+
{ module_name; file; contents; source_map_contents = opt_source_map })
19021891
(Array.of_list files)
19031892
in
19041893

@@ -2292,15 +2281,12 @@ let f files ~output_file ~opt_output_sourcemap_file =
22922281
pos_out out_ch + 1 + Buffer.length b
22932282
in
22942283
add_section out_ch ~id:10 code_pieces;
2295-
Option.iter
2296-
~f:(fun file ->
2297-
Source_map.to_file
2298-
(Wasm_source_map.concatenate
2299-
(List.map
2300-
~f:(fun (pos, sm) -> pos + code_section_offset, sm)
2301-
(List.rev !source_maps)))
2302-
file)
2303-
opt_output_sourcemap_file;
2284+
let source_map =
2285+
Wasm_source_map.concatenate
2286+
(List.map
2287+
~f:(fun (pos, sm) -> pos + code_section_offset, sm)
2288+
(List.rev !source_maps))
2289+
in
23042290

23052291
(* 11: data *)
23062292
ignore
@@ -2447,7 +2433,9 @@ let f files ~output_file ~opt_output_sourcemap_file =
24472433

24482434
add_section out_ch ~id:0 name_section_buffer;
24492435

2450-
close_out out_ch
2436+
close_out out_ch;
2437+
2438+
source_map
24512439

24522440
(*
24532441
LATER

compiler/lib-wasm/wasm_link.mli

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ type input =
2020
{ module_name : string
2121
; file : string
2222
; code : string option
23-
; opt_source_map : [ `File of string | `Data of string ] option
23+
; opt_source_map : Source_map.Standard.t option
2424
}
2525

26-
val f :
27-
input list -> output_file:string -> opt_output_sourcemap_file:string option -> unit
26+
val f : input list -> output_file:string -> Source_map.t

0 commit comments

Comments
 (0)