Skip to content

Commit 1e54c94

Browse files
committed
Compiler: use sourcemap index instead of merge
1 parent a0a03bd commit 1e54c94

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
that follows the semantic of the backend (js or wasm)
1919
* Compiler: warn on joo_global_object
2020
* Compiler: revisit static env handling (#1708)
21+
* Compiler: Emit index map when linking multiple js files together (#1714)
2122
* Runtime: change Sys.os_type on windows (Cygwin -> Win32)
2223
* Runtime: backtraces are really expensive, they need to be be explicitly
2324
requested at compile time (--enable with-js-error) or at startup (OCAMLRUNPARAM=b=1)

compiler/lib/link_js.ml

+26-26
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,12 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
324324
let sm_for_file = ref None in
325325
let ic = Line_reader.open_ file in
326326
let skip ic = Line_reader.drop ic in
327+
let line_offset = Line_writer.lnum oc in
327328
let reloc = ref [] in
328329
let copy ic oc =
329330
let line = Line_reader.next ic in
330331
Line_writer.write ~source:ic oc line;
331-
reloc := (Line_reader.lnum ic, Line_writer.lnum oc) :: !reloc
332+
reloc := (Line_reader.lnum ic, Line_writer.lnum oc - line_offset) :: !reloc
332333
in
333334
let rec read () =
334335
match Line_reader.peek ic with
@@ -432,7 +433,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
432433
Line_writer.write_lines oc content);
433434
(match !sm_for_file with
434435
| None -> ()
435-
| Some x -> sm := (x, !reloc) :: !sm);
436+
| Some x -> sm := (x, !reloc, line_offset) :: !sm);
436437
match !build_info, build_info_for_file with
437438
| None, None -> ()
438439
| Some _, None -> ()
@@ -445,33 +446,32 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
445446
match source_map with
446447
| None -> ()
447448
| Some (file, init_sm) ->
448-
let sm =
449-
List.rev_map !sm ~f:(fun (sm, reloc) ->
449+
let sections =
450+
List.rev_map !sm ~f:(fun (sm, reloc, offset) ->
450451
let tbl = Hashtbl.create 17 in
451452
List.iter reloc ~f:(fun (a, b) -> Hashtbl.add tbl a b);
452-
Source_map.Standard.filter_map sm ~f:(Hashtbl.find_opt tbl))
453+
( { Source_map.Index.gen_line = offset; gen_column = 0 }
454+
, `Map (Source_map.Standard.filter_map sm ~f:(Hashtbl.find_opt tbl)) ))
453455
in
454-
(match Source_map.Standard.merge (init_sm :: sm) with
455-
| None -> ()
456-
| Some sm -> (
457-
(* preserve some info from [init_sm] *)
458-
let sm =
459-
`Standard
460-
{ sm with
461-
version = init_sm.version
462-
; file = init_sm.file
463-
; sourceroot = init_sm.sourceroot
464-
}
465-
in
466-
match file with
467-
| None ->
468-
let data = Source_map.to_string sm in
469-
let s = sourceMappingURL_base64 ^ Base64.encode_exn data in
470-
Line_writer.write oc s
471-
| Some file ->
472-
Source_map.to_file sm file;
473-
let s = sourceMappingURL ^ Filename.basename file in
474-
Line_writer.write oc s));
456+
let sm =
457+
{ Source_map.Index.version = init_sm.Source_map.Standard.version
458+
; file = init_sm.file
459+
; sections =
460+
(* preserve some info from [init_sm] *)
461+
List.map sections ~f:(fun (ofs, `Map sm) ->
462+
ofs, `Map { sm with sourceroot = init_sm.sourceroot })
463+
}
464+
in
465+
let sm = `Index sm in
466+
(match file with
467+
| None ->
468+
let data = Source_map.to_string sm in
469+
let s = sourceMappingURL_base64 ^ Base64.encode_exn data in
470+
Line_writer.write oc s
471+
| Some file ->
472+
Source_map.to_file sm file;
473+
let s = sourceMappingURL ^ Filename.basename file in
474+
Line_writer.write oc s);
475475
if times () then Format.eprintf " sourcemap: %a@." Timer.print t
476476

477477
let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source_map =

0 commit comments

Comments
 (0)