Browse Source

[js] output file:line for traces (closes #6467)

Simon Krajewski 8 years ago
parent
commit
263b6e1727

+ 8 - 0
src/generators/genjs.ml

@@ -423,6 +423,14 @@ let rec gen_call ctx e el in_value =
 			spr ctx ")";
 		end else begin
 			spr ctx "console.log(";
+			begin match infos.eexpr with
+			| TObjectDecl (
+				("fileName" , { eexpr = (TConst (TString file)) }) ::
+				("lineNumber" , { eexpr = (TConst (TInt line)) }) :: _) ->
+					print ctx "\"%s:%i: \" + " file (Int32.to_int line);
+			| _ ->
+				()
+			end;
 			gen_value ctx e;
 			spr ctx ")";
 		end

+ 9 - 9
tests/optimization/src/TestJs.hx

@@ -82,11 +82,11 @@ class TestJs {
 		}) {}
 	}
 
-	@:js("var a = [1,2,3];var _g = 0;while(_g < a.length) {var v = a[_g];++_g;console.log(v + 2);}")
+	@:js('var a = [1,2,3];var _g = 0;while(_g < a.length) {var v = a[_g];++_g;TestJs["use"](v + 2);}')
 	static function testInlineFunctionWithAnonymousCallback() {
 		var a = [1,2,3];
 		inline function forEach(f) for (v in a) f(v);
-		forEach(function(x) trace(x + 2));
+		forEach(function(x) use(x + 2));
 	}
 
 	@:js('var a = "";var e;var _hx_tmp = a.toLowerCase();if(_hx_tmp == "e") {e = 0;} else {throw new Error();}')
@@ -99,12 +99,12 @@ class TestJs {
 		}
 	}
 
-	@:js('console.log("1" + "2" + "3" + "4");')
+	@:js('TestJs["use"]("1" + "2" + "3" + "4");')
 	static function testEnumValuePropagation1() {
 		var n = Node(Node(Leaf("1"), Node(Leaf("2"), Leaf("3"))), Leaf("4"));
 		switch (n) {
 			case Node(Node(Leaf(s1), Node(Leaf(s2), Leaf(s3))), Leaf(s4)):
-				trace(s1 + s2 + s3 + s4);
+				use(s1 + s2 + s3 + s4);
 			case _:
 		}
 	}
@@ -170,9 +170,9 @@ class TestJs {
 		try throw false catch (e:Dynamic) {}
 	}
 
-	@:js("try {throw new js__$Boot_HaxeError(false);} catch( e ) {if (e instanceof js__$Boot_HaxeError) e = e.val;console.log(e);}")
+	@:js('try {throw new js__$Boot_HaxeError(false);} catch( e ) {if (e instanceof js__$Boot_HaxeError) e = e.val;TestJs["use"](e);}')
 	static function testHaxeErrorUnwrappingWhenUsed() {
-		try throw false catch (e:Dynamic) trace(e);
+		try throw false catch (e:Dynamic) use(e);
 	}
 
 	@:js("try {throw new js__$Boot_HaxeError(false);} catch( e ) {if (e instanceof js__$Boot_HaxeError) e = e.val;if( js_Boot.__instanceof(e,Bool) ) {} else throw(e);}")
@@ -476,7 +476,7 @@ class TestJs {
 	@:pure(false)
 	static function call(d1:Dynamic, d2:Dynamic) { return d1; }
 	@:pure(false)
-	static function use<T>(t:T) { return t; }
+	static public function use<T>(t:T) { return t; }
 
 	static var intField = 12;
 	static var stringField(default, never) = "foo";
@@ -495,13 +495,13 @@ class TestJs {
 	@:js('
 		var e = { };
 		e["a"] = 30;
-		console.log(e);
+		TestJs["use"](e);
 	')
 	@:analyzer(user_var_fusion)
 	static function testIssue4948() {
 		var e = new haxe.DynamicAccess();
 		e["a"] = 30;
-		trace(e);
+		use(e);
 	}
 
 

+ 30 - 28
tests/optimization/src/TestLocalDce.hx

@@ -1,3 +1,5 @@
+import TestJs.use;
+
 private class InlineCtor {
 	public var x:Int;
 	public var y:String;
@@ -15,25 +17,25 @@ private abstract MyEnum(String) to String {
 
 @:analyzer(no_user_var_fusion)
 class TestLocalDce {
-	@:js('console.log(3);')
+	@:js('TestJs["use"](3);')
 	static function testNoOpRemoval() {
 		1;
 		2;
 		{}
-		trace(3);
+		use(3);
 	}
 
 	@:js('
-		console.log(27);
+		TestJs["use"](27);
 	')
 	static function testConstMath() {
 		var a = 1 + 2;
 		var b = 9 * 3;
-		trace(b);
+		use(b);
 	}
 
 	@:js('
-		console.log("foo");
+		TestJs["use"]("foo");
 	')
 	static function testInlineCtor1() {
 		var c = new InlineCtor(12, "foo");
@@ -41,11 +43,11 @@ class TestLocalDce {
 		c.x = 13;
 		x = c.x;
 		var y = c.y;
-		trace(y);
+		use(y);
 	}
 
 	@:js('
-		console.log(12);
+		TestJs["use"](12);
 	')
 	static function testInlineCtor2() {
 		var a = 0;
@@ -54,11 +56,11 @@ class TestLocalDce {
 			a = 2;
 			new InlineCtor(12, "foo");
 		}
-		trace(a = c.x);
+		use(a = c.x);
 	}
 
 	@:js('
-		console.log(1);
+		TestJs["use"](1);
 	')
 	static function testInlineCtor3() {
 		var a = 0;
@@ -67,11 +69,11 @@ class TestLocalDce {
 			a = 1;
 			new InlineCtor(2, "b");
 		}
-		trace(b.x = a);
+		use(b.x = a);
 	}
 
 	@:js('
-		console.log(2);
+		TestJs["use"](2);
 	')
 	static function testStructureInline1() {
 		var x = {
@@ -80,7 +82,7 @@ class TestLocalDce {
 		}
 		var y = x.foo;
 		var z = x.bar;
-		trace(z);
+		use(z);
 	}
 
 	@:js('
@@ -93,38 +95,38 @@ class TestLocalDce {
 	}
 
 	@:js('
-		console.log(2);
+		TestJs["use"](2);
 	')
 	static function testArrayInline() {
 		var a = [1, 2];
 		var b = a.length;
-		trace(b);
+		use(b);
 	}
 
 	@:js('
 		var a = [1,2];
-		console.log(a[-1]);
+		TestJs["use"](a[-1]);
 	')
 	static function testArrayInlineCancelNegative() {
 		var a = [1, 2];
-		trace(a[-1]);
+		use(a[-1]);
 	}
 
 	@:js('
 		var a = [1,2];
-		console.log(a[2]);
+		TestJs["use"](a[2]);
 	')
 	static function testArrayInlineCancelExceeds() {
 		var a = [1, 2];
-		trace(a[2]);
+		use(a[2]);
 	}
 
 	@:js('
-		console.log("a");
+		TestJs["use"]("a");
 	')
 	static function testAbstractOverStringBinop() {
 		var s = "" + A;
-		trace(s);
+		use(s);
 	}
 
 	//@:js('
@@ -132,24 +134,24 @@ class TestLocalDce {
 		//s += 0;
 		//s += 6;
 		//s += 8;
-		//console.log(s);
+		//TestJs["use"](s);
 	//')
 	static function testLoopUnroll() {
 		var s = keep(1);
 		for (i in [0, 3, 4]) {
 			s += i * 2;
 		}
-		trace(s);
+		use(s);
 	}
 
-	//@:js('console.log(5.);')
+	//@:js('TestJs["use"](5.);')
 	static function testLoopUnrollDavid() {
 		var s = 0.0;
 		inline function foo(r)
 			return 2.0 + r;
 		for ( r in [0.0,1.0] )
 			s+=foo(r);
-		trace(s);
+		use(s);
 	}
 
 	//@:js('
@@ -162,7 +164,7 @@ class TestLocalDce {
 			//s += i * 2;
 			//continue;
 		//}
-		//console.log(s);
+		//TestJs["use"](s);
 	//')
 	static function testLoopUnrollContinue() {
 		var s = keep(1);
@@ -170,7 +172,7 @@ class TestLocalDce {
 			s += i * 2;
 			continue;
 		}
-		trace(s);
+		use(s);
 	}
 
 	//@:js('
@@ -183,7 +185,7 @@ class TestLocalDce {
 			//s += i * 2;
 			//break;
 		//}
-		//console.log(s);
+		//TestJs["use"](s);
 	//')
 	static function testLoopUnrollBreak() {
 		var s = keep(1);
@@ -191,7 +193,7 @@ class TestLocalDce {
 			s += i * 2;
 			break;
 		}
-		trace(s);
+		use(s);
 	}
 
 	@:pure(false)

+ 18 - 16
tests/optimization/src/issues/Issue4690.hx

@@ -1,14 +1,16 @@
 package issues;
 
+import TestJs.use;
+
 class Parent {
 	public var x:Int;
 	public var y:String;
 
 	public inline function new(x:Int, y:String) {
-		trace("Parent.new: Before assign");
+		use("Parent.new: Before assign");
 		this.x = x;
 		this.y = y;
-		trace("Parent.new: After assign");
+		use("Parent.new: After assign");
 	}
 }
 
@@ -17,11 +19,11 @@ class Child extends Parent {
 	public var z:Int;
 
 	public inline function new(x:Int, y:Int, z:Int) {
-		trace("Child.new: Before super");
+		use("Child.new: Before super");
 		super(x, Std.string(y));
-		trace("Child.new: After super");
+		use("Child.new: After super");
 		this.z = z;
-		trace("Child new: After assign");
+		use("Child new: After assign");
 	}
 }
 
@@ -30,23 +32,23 @@ class Issue4690 {
 		var c_z;
 		var c_y;
 		var c_x;
-		console.log("Child.new: Before super");
-		console.log("Parent.new: Before assign");
+		TestJs["use"]("Child.new: Before super");
+		TestJs["use"]("Parent.new: Before assign");
 		c_x = 1;
 		c_y = "" + 2;
-		console.log("Parent.new: After assign");
-		console.log("Child.new: After super");
+		TestJs["use"]("Parent.new: After assign");
+		TestJs["use"]("Child.new: After super");
 		c_z = 3;
-		console.log("Child new: After assign");
-		console.log(c_x);
-		console.log(c_y);
-		console.log(c_z);
+		TestJs["use"]("Child new: After assign");
+		TestJs["use"](c_x);
+		TestJs["use"](c_y);
+		TestJs["use"](c_z);
 	')
 	@:analyzer(no_const_propagation, no_fusion)
 	static function test() {
 		var c = new Child(1, 2, 3);
-		trace(c.x);
-		trace(c.y);
-		trace(c.z);
+		use(c.x);
+		use(c.y);
+		use(c.z);
 	}
 }

+ 4 - 2
tests/optimization/src/issues/Issue5745.hx

@@ -1,17 +1,19 @@
 package issues;
 
+import TestJs.use;
+
 class Issue5745 {
 	@:js('
 		var fn = "filename";
 		var v = cat(fn);
 		runProgram.apply(undefined, ["rm",fn]);
-		console.log(v);
+		TestJs["use"](v);
 	')
     static function test() {
         var fn = 'filename';
         var v = Shell.cat(fn);
         Shell.runProgram(['rm', fn]);
-        trace(v);
+        use(v);
     }
 }
 

+ 4 - 2
tests/optimization/src/issues/Issue5855.hx

@@ -1,5 +1,7 @@
 package issues;
 
+import TestJs.use;
+
 class C {
 	public var field:String;
 	public inline function new() {
@@ -15,11 +17,11 @@ class Issue5855 {
 		if(Math.random() > 0.5) {
 			c_field = "foo";
 		}
-		console.log(c_field);
+		TestJs["use"](c_field);
 	')
 	@:analyzer(ignore)
 	static function main() {
 		var c = new C();
-		trace(c.field);
+		use(c.field);
 	}
 }

+ 6 - 2
tests/optimization/src/issues/Issue6283.hx

@@ -1,5 +1,9 @@
 package issues;
 
+#if !macro
+import TestJs.use;
+#end
+
 @:callable
 abstract From<T>(T) from T to T {
 	inline function new (t:T) return new From(t);
@@ -9,11 +13,11 @@ abstract From<T>(T) from T to T {
 }
 
 class Issue6283 {
-	@:js('console.log(3);')
+	@:js('TestJs["use"](3);')
 	@:analyzer(no_local_dce)
 	static function f(a, b) {
 		#if !macro
-		trace(foo({}));
+		use(foo({}));
 		#end
 	}
 

+ 6 - 4
tests/optimization/src/issues/Issue6338.hx

@@ -1,5 +1,7 @@
 package issues;
 
+import TestJs.use;
+
 extern class Home {
 	static var ids: Ids;
 }
@@ -14,12 +16,12 @@ extern class Ids {
 
 class Issue6338 {
 	@:js('
-		console.log("hd");
-		console.log("ft");
+		TestJs["use"]("hd");
+		TestJs["use"]("ft");
 	')
 	@:analyzer(ignore)
 	static function test() {
-		trace(Home.ids.hd);
-		trace(Home.ids.ft);
+		use(Home.ids.hd);
+		use(Home.ids.ft);
 	}
 }

+ 4 - 2
tests/optimization/src/issues/Issue6409.hx

@@ -1,5 +1,7 @@
 package issues;
 
+import TestJs.use;
+
 enum E {
     Flags( v : String );
 }
@@ -8,12 +10,12 @@ class Issue6409 {
 	@:js('
 		var issues_E1 = null;
 		var f = issues_E.Flags("");
-		console.log(f);
+		TestJs["use"](f);
 	')
 	@:analyzer(ignore)
     static function test() {
         var issues_E = null;
         var f : E = Flags("");
-        trace(f);
+        use(f);
     }
 }