Skip to content

Commit b5ae6af

Browse files
committed
Source map related code clean-up
1 parent 7e071a6 commit b5ae6af

File tree

4 files changed

+109
-129
lines changed

4 files changed

+109
-129
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 & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -849,63 +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.Standard.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 (Standard sm) (fun i j file ->
868-
l := source_name i j file :: !l);
869-
if not (List.is_empty !l)
870-
then z, Array.of_list (List.rev !l)
871-
else (
872-
Zip.close_in z;
873-
get_source_map_files files src_index))
874-
else (
875-
Zip.close_in z;
876-
get_source_map_files files src_index)
877-
else get_source_map_files files src_index
878-
879-
let add_source_map files z opt_source_map_file =
880-
Option.iter
881-
~f:(fun file ->
882-
Zip.add_file z ~name:"source_map.map" ~file;
883-
let sm = Source_map.of_file file in
884-
let files = Array.of_list files in
885-
let src_index = ref 0 in
886-
let st = ref None in
887-
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 =
888883
match !st with
889-
| Some (_, (z', _)) -> Zip.close_in z'
890-
| 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'
891890
in
892-
Wasm_source_map.iter_sources sm (fun i j file ->
893-
let z', files =
894-
match !st with
895-
| Some (i', st) when Poly.equal i i' -> st
896-
| _ ->
897-
let st' = get_source_map_files files src_index in
898-
finalize ();
899-
st := Some (i, st');
900-
st'
901-
in
902-
if Array.length files > 0 (* Source has source map *)
903-
then
904-
let name = files.(Option.value ~default:0 j) in
905-
if Zip.has_entry z' ~name
906-
then Zip.copy_file z' z ~src_name:name ~dst_name:(source_name i j file));
907-
finalize ())
908-
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 ()
909897

910898
let make_library ~output_file ~enable_source_maps ~files =
911899
let info =
@@ -936,33 +924,31 @@ let make_library ~output_file ~enable_source_maps ~files =
936924
@@ fun tmp_output_file ->
937925
let z = Zip.open_out tmp_output_file in
938926
add_info z ~build_info ~unit_data ();
939-
(*
940-
- Merge all code files into a single code file (gathering source maps)
941-
- Copy source files
942-
*)
943927
Fs.with_intermediate_file (Filename.temp_file "wasm" ".wasm")
944928
@@ fun tmp_wasm_file ->
945-
opt_with
946-
Fs.with_intermediate_file
947-
(if enable_source_maps then Some (Filename.temp_file "wasm" ".map") else None)
948-
@@ fun opt_output_sourcemap_file ->
949-
Wasm_link.f
950-
(List.map
951-
~f:(fun file ->
952-
let z' = Zip.open_in file in
953-
{ Wasm_link.module_name = "OCaml"
954-
; file
955-
; code = Some (Zip.read_entry z' ~name:"code.wasm")
956-
; opt_source_map =
957-
(if Zip.has_entry z' ~name:"source_map.map"
958-
then Some (`Data (Zip.read_entry z' ~name:"source_map.map"))
959-
else None)
960-
})
961-
files)
962-
~output_file:tmp_wasm_file
963-
~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
964950
Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
965-
add_source_map files z opt_output_sourcemap_file;
951+
if enable_source_maps then add_source_map files z output_sourcemap;
966952
Zip.close_out z
967953

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

compiler/lib-wasm/wasm_link.ml

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,12 +1856,11 @@ type input =
18561856
{ module_name : string
18571857
; file : string
18581858
; code : string option
1859-
; opt_source_map : [ `File of string | `Data of string ] option
1859+
; opt_source_map : Source_map.Standard.t option
18601860
}
18611861

1862-
let f files ~output_file ~opt_output_sourcemap_file =
1862+
let f files ~output_file =
18631863
let files =
1864-
let tmp_buf = Buffer.create 10000 in
18651864
Array.map
18661865
~f:(fun { module_name; file; code; opt_source_map } ->
18671866
let data =
@@ -1870,17 +1869,7 @@ let f files ~output_file ~opt_output_sourcemap_file =
18701869
| Some data -> data
18711870
in
18721871
let contents = Read.open_in file data in
1873-
{ module_name
1874-
; file
1875-
; contents
1876-
; source_map_contents =
1877-
Option.map
1878-
~f:(fun src ->
1879-
match src with
1880-
| `File file -> Source_map.Standard.of_file ~tmp_buf file
1881-
| `Data data -> Source_map.Standard.of_string ~tmp_buf data)
1882-
opt_source_map
1883-
})
1872+
{ module_name; file; contents; source_map_contents = opt_source_map })
18841873
(Array.of_list files)
18851874
in
18861875

@@ -2274,15 +2263,12 @@ let f files ~output_file ~opt_output_sourcemap_file =
22742263
pos_out out_ch + 1 + Buffer.length b
22752264
in
22762265
add_section out_ch ~id:10 code_pieces;
2277-
Option.iter
2278-
~f:(fun file ->
2279-
Source_map.to_file
2280-
(Wasm_source_map.concatenate
2281-
(List.map
2282-
~f:(fun (pos, sm) -> pos + code_section_offset, sm)
2283-
(List.rev !source_maps)))
2284-
file)
2285-
opt_output_sourcemap_file;
2266+
let source_map =
2267+
Wasm_source_map.concatenate
2268+
(List.map
2269+
~f:(fun (pos, sm) -> pos + code_section_offset, sm)
2270+
(List.rev !source_maps))
2271+
in
22862272

22872273
(* 11: data *)
22882274
ignore
@@ -2429,7 +2415,9 @@ let f files ~output_file ~opt_output_sourcemap_file =
24292415

24302416
add_section out_ch ~id:0 name_section_buffer;
24312417

2432-
close_out out_ch
2418+
close_out out_ch;
2419+
2420+
source_map
24332421

24342422
(*
24352423
LATER

compiler/lib-wasm/wasm_link.mli

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ type input =
22
{ module_name : string
33
; file : string
44
; code : string option
5-
; opt_source_map : [ `File of string | `Data of string ] option
5+
; opt_source_map : Source_map.Standard.t option
66
}
77

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

0 commit comments

Comments
 (0)