Selaa lähdekoodia

[js] refactor source map handling a bit more

Dan Korostelev 7 vuotta sitten
vanhempi
commit
43a643e050
1 muutettua tiedostoa jossa 30 lisäystä ja 25 poistoa
  1. 30 25
      src/generators/genjs.ml

+ 30 - 25
src/generators/genjs.ml

@@ -27,13 +27,17 @@ type sourcemap = {
 	sources_hash : (string, int) Hashtbl.t;
 	mappings : Rbuffer.t;
 
-	mutable source_last_line : int;
-	mutable source_last_col : int;
-	mutable source_last_file : int;
+	mutable source_last_pos : sourcemap_pos;
 	mutable print_comma : bool;
 	mutable output_last_col : int;
 	mutable output_current_col : int;
-	mutable current_expr : (int * int * int) option;
+	mutable current_expr : sourcemap_pos option;
+}
+
+and sourcemap_pos = {
+	file : int;
+	line : int;
+	col : int;
 }
 
 type ctx = {
@@ -138,7 +142,7 @@ let add_feature ctx = Common.add_feature ctx.com
 
 let unsupported p = abort "This expression cannot be compiled to Javascript" p
 
-let encode_mapping smap file line col =
+let encode_mapping smap pos =
 	if smap.print_comma then
 		Rbuffer.add_char smap.mappings ','
 	else
@@ -175,18 +179,16 @@ let encode_mapping smap file line col =
 	in
 
 	base64_vlq (smap.output_current_col - smap.output_last_col);
-	base64_vlq (file - smap.source_last_file);
-	base64_vlq (line - smap.source_last_line);
-	base64_vlq (col - smap.source_last_col);
+	base64_vlq (pos.file - smap.source_last_pos.file);
+	base64_vlq (pos.line - smap.source_last_pos.line);
+	base64_vlq (pos.col - smap.source_last_pos.col);
 
-	smap.source_last_file <- file;
-	smap.source_last_line <- line;
-	smap.source_last_col <- col;
+	smap.source_last_pos <- pos;
 	smap.output_last_col <- smap.output_current_col
 
-let add_mapping smap e =
-	if e.epos.pmin < 0 then () else
-	let pos = e.epos in
+let add_mapping smap pos =
+	if pos.pmin < 0 then () else
+
 	let file = try
 		Hashtbl.find smap.sources_hash pos.pfile
 	with Not_found ->
@@ -195,11 +197,16 @@ let add_mapping smap e =
 		DynArray.add smap.sources pos.pfile;
 		length
 	in
-	let line, col = Lexer.find_pos pos in
-	let line = line - 1 in
-	if smap.source_last_file != file || smap.source_last_line != line || smap.source_last_col != col then begin
-		smap.current_expr <- Some (file, line, col);
-		encode_mapping smap file line col
+
+	let pos =
+		let line, col = Lexer.find_pos pos in
+		let line = line - 1 in
+		{ file = file; line = line; col = col }
+	in
+
+	if smap.source_last_pos <> pos then begin
+		smap.current_expr <- Some pos;
+		encode_mapping smap pos
 	end
 
 let handle_newlines ctx str =
@@ -211,7 +218,7 @@ let handle_newlines ctx str =
 				smap.output_last_col <- 0;
 				smap.output_current_col <- 0;
 				smap.print_comma <- false;
-				Option.may (fun (file,line,col) -> encode_mapping smap file line col) smap.current_expr;
+				Option.may (encode_mapping smap) smap.current_expr;
 				loop next
 			end with Not_found ->
 				smap.output_current_col <- smap.output_current_col + (String.length str - from);
@@ -451,7 +458,7 @@ and add_objectdecl_parens e =
 	loop e
 
 and gen_expr ctx e =
-	Option.may (fun smap -> add_mapping smap e) ctx.smap;
+	Option.may (fun smap -> add_mapping smap e.epos) ctx.smap;
 	(match e.eexpr with
 	| TConst c -> gen_constant ctx e.epos c
 	| TLocal v -> spr ctx (ident v.v_name)
@@ -760,7 +767,7 @@ and gen_block_element ?(after=false) ctx e =
 		if after then newline ctx
 
 and gen_value ctx e =
-	Option.may (fun smap -> add_mapping smap e) ctx.smap;
+	Option.may (fun smap -> add_mapping smap e.epos) ctx.smap;
 	let assign e =
 		mk (TBinop (Ast.OpAssign,
 			mk (TLocal (match ctx.in_value with None -> assert false | Some v -> v)) t_dynamic e.epos,
@@ -1255,9 +1262,7 @@ let alloc_ctx com =
 	let smap =
 		if com.debug || Common.defined com Define.JsSourceMap then
 			Some {
-				source_last_line = 0;
-				source_last_col = 0;
-				source_last_file = 0;
+				source_last_pos = { file = 0; line = 0; col = 0};
 				print_comma = false;
 				output_last_col = 0;
 				output_current_col = 0;