Forráskód Böngészése

[js] hacky way to support multi-line exprs in source-maps

Dan Korostelev 9 éve
szülő
commit
80496a17f5
1 módosított fájl, 48 hozzáadás és 40 törlés
  1. 48 40
      src/generators/genjs.ml

+ 48 - 40
src/generators/genjs.ml

@@ -34,6 +34,7 @@ type sourcemap = {
 	mutable print_comma : bool;
 	mutable output_last_col : int;
 	mutable output_current_col : int;
+	mutable current_expr : texpr option;
 }
 
 type ctx = {
@@ -135,41 +136,10 @@ let static_field c s =
 let has_feature ctx = Common.has_feature ctx.com
 let add_feature ctx = Common.add_feature ctx.com
 
-let handle_newlines ctx str =
-	if ctx.com.debug then
-		let rec loop from =
-			try begin
-				let next = String.index_from str from '\n' + 1 in
-				Rbuffer.add_char ctx.smap.mappings ';';
-				ctx.smap.output_last_col <- 0;
-				ctx.smap.output_current_col <- 0;
-				ctx.smap.print_comma <- false;
-				loop next
-			end with Not_found ->
-				ctx.smap.output_current_col <- ctx.smap.output_current_col + (String.length str - from);
-		in
-		loop 0
-	else ()
-
-let flush ctx =
-	Rbuffer.output_buffer ctx.chan ctx.buf;
-	Rbuffer.clear ctx.buf
-
-let spr ctx s =
-	ctx.separator <- false;
-	handle_newlines ctx s;
-	Rbuffer.add_string ctx.buf s
-
-let print ctx =
-	ctx.separator <- false;
-	Printf.kprintf (fun s -> begin
-		handle_newlines ctx s;
-		Rbuffer.add_string ctx.buf s
-	end)
-
 let unsupported p = error "This expression cannot be compiled to Javascript" p
 
-let add_mapping ctx e =
+
+let add_mapping ctx force e =
 	if not ctx.com.debug || e.epos.pmin < 0 then () else
 	let pos = e.epos in
 	let smap = ctx.smap in
@@ -183,7 +153,8 @@ let add_mapping ctx e =
 	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
+	if force || smap.source_last_file != file || smap.source_last_line != line || smap.source_last_col != col then begin
+		smap.current_expr <- Some e;
 		if smap.print_comma then
 			Rbuffer.add_char smap.mappings ','
 		else
@@ -230,6 +201,39 @@ let add_mapping ctx e =
 		smap.output_last_col <- smap.output_current_col
 	end
 
+let handle_newlines ctx str =
+	if ctx.com.debug then
+		let rec loop from =
+			try begin
+				let next = String.index_from str from '\n' + 1 in
+				Rbuffer.add_char ctx.smap.mappings ';';
+				ctx.smap.output_last_col <- 0;
+				ctx.smap.output_current_col <- 0;
+				ctx.smap.print_comma <- false;
+				Option.may (fun e -> add_mapping ctx true e) ctx.smap.current_expr;
+				loop next
+			end with Not_found ->
+				ctx.smap.output_current_col <- ctx.smap.output_current_col + (String.length str - from);
+		in
+		loop 0
+	else ()
+
+let flush ctx =
+	Rbuffer.output_buffer ctx.chan ctx.buf;
+	Rbuffer.clear ctx.buf
+
+let spr ctx s =
+	ctx.separator <- false;
+	handle_newlines ctx s;
+	Rbuffer.add_string ctx.buf s
+
+let print ctx =
+	ctx.separator <- false;
+	Printf.kprintf (fun s -> begin
+		handle_newlines ctx s;
+		Rbuffer.add_string ctx.buf s
+	end)
+
 let write_mappings ctx =
 	let basefile = Filename.basename ctx.com.file in
 	print ctx "\n//# sourceMappingURL=%s.map" basefile;
@@ -467,8 +471,8 @@ let rec gen_call ctx e el in_value =
 		spr ctx ")"
 
 and gen_expr ctx e =
-	add_mapping ctx e;
-	match e.eexpr with
+	add_mapping ctx false e;
+	(match e.eexpr with
 	| TConst c -> gen_constant ctx e.epos c
 	| TLocal v -> spr ctx (ident v.v_name)
 	| TArray (e1,{ eexpr = TConst (TString s) }) when valid_js_ident s && (match e1.eexpr with TConst (TInt _|TFloat _) -> false | _ -> true) ->
@@ -805,7 +809,8 @@ and gen_expr ctx e =
 		gen_expr ctx e1;
 		spr ctx " , ";
 		spr ctx (ctx.type_accessor t);
-		spr ctx ")"
+		spr ctx ")");
+	ctx.smap.current_expr <- None;
 
 
 and gen_block_element ?(after=false) ctx e =
@@ -829,7 +834,7 @@ and gen_block_element ?(after=false) ctx e =
 		if after then newline ctx
 
 and gen_value ctx e =
-	add_mapping ctx e;
+	add_mapping ctx false e;
 	let assign e =
 		mk (TBinop (Ast.OpAssign,
 			mk (TLocal (match ctx.in_value with None -> assert false | Some v -> v)) t_dynamic e.epos,
@@ -858,7 +863,7 @@ and gen_value ctx e =
 			print ctx "(%s))" (this ctx)
 		)
 	in
-	match e.eexpr with
+	(match e.eexpr with
 	| TConst _
 	| TLocal _
 	| TArray _
@@ -940,7 +945,9 @@ and gen_value ctx e =
 		gen_expr ctx (mk (TTry (block (assign b),
 			List.map (fun (v,e) -> v, block (assign e)) catchs
 		)) e.etype e.epos);
-		v()
+		v());
+	ctx.smap.current_expr <- None
+
 
 let generate_package_create ctx (p,_) =
 	let rec loop acc = function
@@ -1250,6 +1257,7 @@ let alloc_ctx com =
 			sources = DynArray.create();
 			sources_hash = Hashtbl.create 0;
 			mappings = Rbuffer.create 16;
+			current_expr = None;
 		};
 		js_modern = not (Common.defined com Define.JsClassic);
 		js_flatten = not (Common.defined com Define.JsUnflatten);