浏览代码

[js] setup proper keyword list based on selected ES version

Dan Korostelev 8 年之前
父节点
当前提交
cdb468bd09

+ 24 - 14
src/generators/genjs.ml

@@ -76,17 +76,28 @@ let dot_path = s_type_path
 
 let s_path ctx = if ctx.js_flatten then Path.flat_path else dot_path
 
-let kwds =
-	let h = Hashtbl.create 0 in
-	List.iter (fun s -> Hashtbl.add h s ()) [
-		"abstract"; "as"; "boolean"; "break"; "byte"; "case"; "catch"; "char"; "class"; "continue"; "const";
-		"debugger"; "default"; "delete"; "do"; "double"; "else"; "enum"; "export"; "extends"; "false"; "final";
-		"finally"; "float"; "for"; "function"; "goto"; "if"; "implements"; "import"; "in"; "instanceof"; "int";
-		"interface"; "is"; "let"; "long"; "namespace"; "native"; "new"; "null"; "package"; "private"; "protected";
-		"public"; "return"; "short"; "static"; "super"; "switch"; "synchronized"; "this"; "throw"; "throws";
-		"transient"; "true"; "try"; "typeof"; "use"; "var"; "void"; "volatile"; "while"; "with"; "yield"
-	];
-	h
+let kwds = Hashtbl.create 0
+
+let setup_kwds lst =
+	List.iter (fun s -> Hashtbl.add kwds s ()) lst
+
+let es3kwds = [
+	"abstract"; "boolean"; "break"; "byte"; "case"; "catch"; "char"; "class"; "const"; "continue";
+	"debugger"; "default"; "delete"; "do"; "double"; "else"; "enum"; "export"; "extends"; "false"; "final";
+	"finally"; "float"; "for"; "function"; "goto"; "if"; "implements"; "import"; "in"; "instanceof"; "int";
+	"interface"; "long"; "native"; "new"; "null"; "package"; "private"; "protected";
+	"public"; "return"; "short"; "static"; "super"; "switch"; "synchronized"; "this"; "throw"; "throws";
+	"transient"; "true"; "try"; "typeof"; "var"; "void"; "volatile"; "while"; "with"
+]
+
+let es5kwds = [
+	"arguments"; "break"; "case"; "catch"; "class"; "const"; "continue";
+	"debugger"; "default"; "delete"; "do"; "else"; "enum"; "eval"; "export"; "extends"; "false";
+	"finally"; "for"; "function"; "if"; "implements"; "import"; "in"; "instanceof";
+	"interface"; "let"; "new"; "null"; "package"; "private"; "protected";
+	"public"; "return"; "static"; "super"; "switch"; "this"; "throw";
+	"true"; "try"; "typeof"; "var"; "void"; "while"; "with"; "yield"
+]
 
 (* Identifiers Haxe reserves to make the JS output cleaner. These can still be used in untyped code (TLocal),
    but are escaped upon declaration. *)
@@ -1342,6 +1353,8 @@ let generate com =
 
 	let nodejs = Common.raw_defined com "nodejs" in
 
+	setup_kwds (if ctx.es_version >= 5 then es5kwds else es3kwds);
+
 	let exposed = List.concat (List.map (fun t ->
 		match t with
 			| TClassDecl c ->
@@ -1430,9 +1443,6 @@ let generate com =
 	);
 
 	if ctx.js_modern then begin
-		(* Additional ES5 strict mode keywords. *)
-		List.iter (fun s -> Hashtbl.replace kwds s ()) [ "arguments"; "eval" ];
-
 		(* Wrap output in a closure *)
 		print ctx "(function (%s) { \"use strict\"" (String.concat ", " (List.map fst closureArgs));
 		newline ctx;

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

@@ -44,7 +44,7 @@ class TestJs {
 		for (v in a) { }
 	}
 
-	@:js('var a = 1;var v2 = a;if(a + v2 > 0) {TestJs["use"](a);}')
+	@:js('var a = 1;var v2 = a;if(a + v2 > 0) {TestJs.use(a);}')
 	@:analyzer(no_const_propagation)
 	@:analyzer(no_copy_propagation)
 	@:analyzer(no_local_dce)
@@ -82,7 +82,7 @@ class TestJs {
 		}) {}
 	}
 
-	@:js('var a = [1,2,3];var _g = 0;while(_g < a.length) {var v = a[_g];++_g;TestJs["use"](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);
@@ -99,7 +99,7 @@ class TestJs {
 		}
 	}
 
-	@:js('TestJs["use"]("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) {
@@ -123,7 +123,7 @@ class TestJs {
 
 	@:js('
 		var object = { "hello" : "world"};
-		TestJs["use"](object);
+		TestJs.use(object);
 	')
 	static function testQuotedStructureFields1() {
 		var object = {
@@ -134,7 +134,7 @@ class TestJs {
 
 	@:js('
 		var object = { "hello" : "world", world : "hello", "another" : "quote"};
-		TestJs["use"](object);
+		TestJs.use(object);
 	')
 	static function testQuotedStructureFields2() {
 		var object = {
@@ -147,7 +147,7 @@ class TestJs {
 
 	@:js('
 		var object = { "\'" : "world"};
-		TestJs["use"](object);
+		TestJs.use(object);
 	')
 	static function testQuotedStructureFields3() {
 		var object = {
@@ -170,7 +170,7 @@ 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;TestJs["use"](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) use(e);
 	}
@@ -181,7 +181,7 @@ class TestJs {
 	}
 
 
-	@:js('TestJs["use"](2);')
+	@:js('TestJs.use(2);')
 	static function testIssue3938() {
 		var a = 1;
 		if (a == 1) {
@@ -193,7 +193,7 @@ class TestJs {
 	}
 
 	@:js('
-		TestJs["use"](3);
+		TestJs.use(3);
 	')
 	static function testBinop() {
 		var a = 1;
@@ -202,17 +202,17 @@ class TestJs {
 	}
 
 	@:js('
-		TestJs["use"](false);
-		TestJs["use"](true);
-		TestJs["use"](true);
-		TestJs["use"](true);
-		TestJs["use"](false);
-		TestJs["use"](true);
-		TestJs["use"](true);
-		TestJs["use"](false);
-		TestJs["use"](false);
-		TestJs["use"](true);
-		TestJs["use"](false);
+		TestJs.use(false);
+		TestJs.use(true);
+		TestJs.use(true);
+		TestJs.use(true);
+		TestJs.use(false);
+		TestJs.use(true);
+		TestJs.use(true);
+		TestJs.use(false);
+		TestJs.use(false);
+		TestJs.use(true);
+		TestJs.use(false);
 	')
 	static function testEnumValueFlags() {
 		var flags = new haxe.EnumFlags();
@@ -240,7 +240,7 @@ class TestJs {
 	@:js('
 		var map = new haxe_ds_StringMap();
 		if(__map_reserved["some"] != null) {map.setReserved("some",2);} else {map.h["some"] = 2;}
-		TestJs["use"](2);
+		TestJs.use(2);
 	')
 	static function testIssue4731() {
 		var map = new Map();
@@ -296,8 +296,8 @@ class TestJs {
 		if(Math.random() < 0.5) {
 			b = "hello";
 		}
-		TestJs["use"](a);
-		TestJs["use"](b);
+		TestJs.use(a);
+		TestJs.use(b);
 	')
 	static function testIssue4739() {
 		var a = 0;
@@ -314,7 +314,7 @@ class TestJs {
 
 	@:js('
 		var a = TestJs.getInt();
-		TestJs["use"](a);
+		TestJs.use(a);
 	')
 	static function testCopyPropagation1() {
 		var a = getInt();
@@ -326,7 +326,7 @@ class TestJs {
 		var a = TestJs.getInt();
 		var b = a;
 		a = TestJs.getInt();
-		TestJs["use"](b);
+		TestJs.use(b);
 	')
 	static function testCopyPropagation2() {
 		var a = getInt();
@@ -337,7 +337,7 @@ class TestJs {
 
 	//@:js('
 		//var b = TestJs.getInt();
-		//TestJs["use"](b);
+		//TestJs.use(b);
 	//')
 	//static function testCopyPropagation3() {
 		//var a;
@@ -366,14 +366,14 @@ class TestJs {
 
 	@:js('
 		var d1 = TestJs.intField;
-		TestJs.call(TestJs["use"](null),d1);
+		TestJs.call(TestJs.use(null),d1);
 	')
 	static function testInlineRebuilding1() {
 		inlineCall(intField, use(null));
 	}
 
 	@:js('
-		TestJs.call(TestJs["use"](null),TestJs.stringField);
+		TestJs.call(TestJs.use(null),TestJs.stringField);
 	')
 	static function testInlineRebuilding2() {
 		inlineCall(stringField, use(null));
@@ -459,7 +459,7 @@ class TestJs {
 
 	@:js('
 		var i = 5;
-		while(--i >= 0) TestJs["use"](i);
+		while(--i >= 0) TestJs.use(i);
 	')
 	static function testPrefixRebuilding() {
 		var i:Int = 5;
@@ -484,7 +484,7 @@ class TestJs {
 	@:js('
 		var _g = Type["typeof"]("");
 		var v = _g[1] == 6 && _g[2] == String;
-		TestJs["use"](v);
+		TestJs.use(v);
 	')
 	static function testIssue4745() {
 		var o = "";
@@ -495,7 +495,7 @@ class TestJs {
 	@:js('
 		var e = { };
 		e["a"] = 30;
-		TestJs["use"](e);
+		TestJs.use(e);
 	')
 	@:analyzer(user_var_fusion)
 	static function testIssue4948() {

+ 14 - 14
tests/optimization/src/TestLocalDce.hx

@@ -17,7 +17,7 @@ private abstract MyEnum(String) to String {
 
 @:analyzer(no_user_var_fusion)
 class TestLocalDce {
-	@:js('TestJs["use"](3);')
+	@:js('TestJs.use(3);')
 	static function testNoOpRemoval() {
 		1;
 		2;
@@ -26,7 +26,7 @@ class TestLocalDce {
 	}
 
 	@:js('
-		TestJs["use"](27);
+		TestJs.use(27);
 	')
 	static function testConstMath() {
 		var a = 1 + 2;
@@ -35,7 +35,7 @@ class TestLocalDce {
 	}
 
 	@:js('
-		TestJs["use"]("foo");
+		TestJs.use("foo");
 	')
 	static function testInlineCtor1() {
 		var c = new InlineCtor(12, "foo");
@@ -47,7 +47,7 @@ class TestLocalDce {
 	}
 
 	@:js('
-		TestJs["use"](12);
+		TestJs.use(12);
 	')
 	static function testInlineCtor2() {
 		var a = 0;
@@ -60,7 +60,7 @@ class TestLocalDce {
 	}
 
 	@:js('
-		TestJs["use"](1);
+		TestJs.use(1);
 	')
 	static function testInlineCtor3() {
 		var a = 0;
@@ -73,7 +73,7 @@ class TestLocalDce {
 	}
 
 	@:js('
-		TestJs["use"](2);
+		TestJs.use(2);
 	')
 	static function testStructureInline1() {
 		var x = {
@@ -95,7 +95,7 @@ class TestLocalDce {
 	}
 
 	@:js('
-		TestJs["use"](2);
+		TestJs.use(2);
 	')
 	static function testArrayInline() {
 		var a = [1, 2];
@@ -105,7 +105,7 @@ class TestLocalDce {
 
 	@:js('
 		var a = [1,2];
-		TestJs["use"](a[-1]);
+		TestJs.use(a[-1]);
 	')
 	static function testArrayInlineCancelNegative() {
 		var a = [1, 2];
@@ -114,7 +114,7 @@ class TestLocalDce {
 
 	@:js('
 		var a = [1,2];
-		TestJs["use"](a[2]);
+		TestJs.use(a[2]);
 	')
 	static function testArrayInlineCancelExceeds() {
 		var a = [1, 2];
@@ -122,7 +122,7 @@ class TestLocalDce {
 	}
 
 	@:js('
-		TestJs["use"]("a");
+		TestJs.use("a");
 	')
 	static function testAbstractOverStringBinop() {
 		var s = "" + A;
@@ -134,7 +134,7 @@ class TestLocalDce {
 		//s += 0;
 		//s += 6;
 		//s += 8;
-		//TestJs["use"](s);
+		//TestJs.use(s);
 	//')
 	static function testLoopUnroll() {
 		var s = keep(1);
@@ -144,7 +144,7 @@ class TestLocalDce {
 		use(s);
 	}
 
-	//@:js('TestJs["use"](5.);')
+	//@:js('TestJs.use(5.);')
 	static function testLoopUnrollDavid() {
 		var s = 0.0;
 		inline function foo(r)
@@ -164,7 +164,7 @@ class TestLocalDce {
 			//s += i * 2;
 			//continue;
 		//}
-		//TestJs["use"](s);
+		//TestJs.use(s);
 	//')
 	static function testLoopUnrollContinue() {
 		var s = keep(1);
@@ -185,7 +185,7 @@ class TestLocalDce {
 			//s += i * 2;
 			//break;
 		//}
-		//TestJs["use"](s);
+		//TestJs.use(s);
 	//')
 	static function testLoopUnrollBreak() {
 		var s = keep(1);

+ 8 - 8
tests/optimization/src/issues/Issue4690.hx

@@ -32,17 +32,17 @@ class Issue4690 {
 		var c_z;
 		var c_y;
 		var c_x;
-		TestJs["use"]("Child.new: Before super");
-		TestJs["use"]("Parent.new: Before assign");
+		TestJs.use("Child.new: Before super");
+		TestJs.use("Parent.new: Before assign");
 		c_x = 1;
 		c_y = "" + 2;
-		TestJs["use"]("Parent.new: After assign");
-		TestJs["use"]("Child.new: After super");
+		TestJs.use("Parent.new: After assign");
+		TestJs.use("Child.new: After super");
 		c_z = 3;
-		TestJs["use"]("Child new: After assign");
-		TestJs["use"](c_x);
-		TestJs["use"](c_y);
-		TestJs["use"](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() {

+ 1 - 1
tests/optimization/src/issues/Issue5477.hx

@@ -2,7 +2,7 @@ package issues;
 
 class Issue5477 {
 	@:js('
-		issues_Issue5477["use"](issues_Issue5477.pureUse(12) > 0.5 ? 1 : issues_Issue5477.pureUse(12));
+		issues_Issue5477.use(issues_Issue5477.pureUse(12) > 0.5 ? 1 : issues_Issue5477.pureUse(12));
 	')
 	static function testIssue5477() {
 		var v = pureUse(12);

+ 1 - 1
tests/optimization/src/issues/Issue5509.hx

@@ -2,7 +2,7 @@ package issues;
 
 class Issue5509 {
 	@:js('
-		issues_Issue5509["use"]();
+		issues_Issue5509.use();
 	')
 	static function test() {
         var v = "some";

+ 1 - 1
tests/optimization/src/issues/Issue5745.hx

@@ -7,7 +7,7 @@ class Issue5745 {
 		var fn = "filename";
 		var v = cat(fn);
 		runProgram.apply(undefined, ["rm",fn]);
-		TestJs["use"](v);
+		TestJs.use(v);
 	')
     static function test() {
         var fn = 'filename';

+ 1 - 1
tests/optimization/src/issues/Issue5855.hx

@@ -17,7 +17,7 @@ class Issue5855 {
 		if(Math.random() > 0.5) {
 			c_field = "foo";
 		}
-		TestJs["use"](c_field);
+		TestJs.use(c_field);
 	')
 	@:analyzer(ignore)
 	static function main() {

+ 1 - 1
tests/optimization/src/issues/Issue6283.hx

@@ -13,7 +13,7 @@ abstract From<T>(T) from T to T {
 }
 
 class Issue6283 {
-	@:js('TestJs["use"](3);')
+	@:js('TestJs.use(3);')
 	@:analyzer(no_local_dce)
 	static function f(a, b) {
 		#if !macro

+ 2 - 2
tests/optimization/src/issues/Issue6338.hx

@@ -16,8 +16,8 @@ extern class Ids {
 
 class Issue6338 {
 	@:js('
-		TestJs["use"]("hd");
-		TestJs["use"]("ft");
+		TestJs.use("hd");
+		TestJs.use("ft");
 	')
 	@:analyzer(ignore)
 	static function test() {

+ 1 - 1
tests/optimization/src/issues/Issue6409.hx

@@ -10,7 +10,7 @@ class Issue6409 {
 	@:js('
 		var issues_E1 = null;
 		var f = issues_E.Flags("");
-		TestJs["use"](f);
+		TestJs.use(f);
 	')
 	@:analyzer(ignore)
     static function test() {