Переглянути джерело

[js] replaced `untyped __js__` with `js.Syntax.code`

Aleksandr Kuzmenko 5 роки тому
батько
коміт
2e0f646e0f

+ 5 - 5
std/js/Boot.hx

@@ -129,7 +129,7 @@ class Boot {
 						// strange error on IE
 						return "???";
 					}
-					if (tostr != null && tostr != __js__("Object.toString") && js.Syntax.typeof(tostr) == "function") {
+					if (tostr != null && tostr != js.Syntax.code("Object.toString") && js.Syntax.typeof(tostr) == "function") {
 						var s2 = o.toString();
 						if (s2 != "[object Object]")
 							return s2;
@@ -138,15 +138,15 @@ class Boot {
 					s += "\t";
 					var hasp = (o.hasOwnProperty != null);
 					var k:String = null;
-					__js__("for( {0} in {1} ) {", k, o);
+					js.Syntax.code("for( {0} in {1} ) {", k, o);
 					if (hasp && !o.hasOwnProperty(k))
-						__js__("continue");
+						js.Syntax.code("continue");
 					if (k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__")
-						__js__("continue");
+						js.Syntax.code("continue");
 					if (str.length != 2)
 						str += ", \n";
 					str += s + k + " : " + __string_rec(o[k], s);
-					__js__("}");
+					js.Syntax.code("}");
 					s = s.substring(1);
 					str += "\n" + s + "}";
 					return str;

+ 3 - 3
std/js/Browser.hx

@@ -30,7 +30,7 @@ class Browser {
 	public static var window(get, never):js.html.Window;
 
 	extern inline static function get_window()
-		return untyped __js__("window");
+		return js.Syntax.code("window");
 
 	/** Shortcut to Window.document. */
 	public static var document(get, never):js.html.HTMLDocument;
@@ -110,10 +110,10 @@ class Browser {
 	 * Explorer.
 	 */
 	public static function createXMLHttpRequest():XMLHttpRequest {
-		if (untyped __js__("typeof XMLHttpRequest") != "undefined") {
+		if (js.Syntax.code("typeof XMLHttpRequest") != "undefined") {
 			return new XMLHttpRequest();
 		}
-		if (untyped __js__("typeof ActiveXObject") != "undefined") {
+		if (js.Syntax.code("typeof ActiveXObject") != "undefined") {
 			return js.Syntax.construct("ActiveXObject", "Microsoft.XMLHTTP");
 		}
 		throw "Unable to create XMLHttpRequest object.";

+ 2 - 2
std/js/_std/HxOverrides.hx

@@ -145,9 +145,9 @@ class HxOverrides {
 		untyped {
 			#if (js_es < 5)
 			__feature__('HxOverrides.indexOf',
-				if (Array.prototype.indexOf) __js__("HxOverrides").indexOf = function(a, o, i) return Array.prototype.indexOf.call(a, o, i));
+				if (Array.prototype.indexOf) js.Syntax.code("HxOverrides").indexOf = function(a, o, i) return Array.prototype.indexOf.call(a, o, i));
 			__feature__('HxOverrides.lastIndexOf',
-				if (Array.prototype.lastIndexOf) __js__("HxOverrides").lastIndexOf = function(a, o, i) return Array.prototype.lastIndexOf.call(a, o, i));
+				if (Array.prototype.lastIndexOf) js.Syntax.code("HxOverrides").lastIndexOf = function(a, o, i) return Array.prototype.lastIndexOf.call(a, o, i));
 			#end
 
 			__feature__('HxOverrides.now',

+ 3 - 3
std/js/_std/Reflect.hx

@@ -62,10 +62,10 @@
 		if (o != null)
 			untyped {
 				var hasOwnProperty = js.lib.Object.prototype.hasOwnProperty;
-				__js__("for( var f in o ) {");
+				js.Syntax.code("for( var f in o ) {");
 				if (f != "__id__" && f != "hx__closures__" && hasOwnProperty.call(o, f))
 					a.push(f);
-				__js__("}");
+				js.Syntax.code("}");
 			}
 		return a;
 	}
@@ -119,7 +119,7 @@
 	@:overload(function(f:Array<Dynamic>->Void):Dynamic {})
 	public static function makeVarArgs(f:Array<Dynamic>->Dynamic):Dynamic {
 		return function() {
-			var a = untyped Array.prototype.slice.call(__js__("arguments"));
+			var a = untyped Array.prototype.slice.call(js.Syntax.code("arguments"));
 			return f(a);
 		};
 	}

+ 9 - 9
std/js/_std/Std.hx

@@ -67,7 +67,7 @@ import js.Syntax;
 	}
 
 	public static inline function parseFloat(x:String):Float {
-		return untyped __js__("parseFloat")(x);
+		return js.Syntax.code("parseFloat({0})", x);
 	}
 
 	public static function random(x:Int):Int {
@@ -82,15 +82,15 @@ import js.Syntax;
 			__feature__("js.Boot.isClass", Array.__name__ = __feature__("Type.getClassName", "Array", true));
 			__feature__("Date.*", {
 				__feature__("js.Boot.getClass",
-					__js__('Date').prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["Date"] = __js__('Date'), __js__('Date')));
-				__feature__("js.Boot.isClass", __js__('Date').__name__ = "Date");
+					js.Syntax.code('Date').prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["Date"] = js.Syntax.code('Date'), js.Syntax.code('Date')));
+				__feature__("js.Boot.isClass", js.Syntax.code('Date').__name__ = "Date");
 			});
-			__feature__("Int.*", __js__('var Int = { };'));
-			__feature__("Dynamic.*", __js__('var Dynamic = { };'));
-			__feature__("Float.*", __js__('var Float = Number'));
-			__feature__("Bool.*", __js__('var Bool = Boolean'));
-			__feature__("Class.*", __js__('var Class = { };'));
-			__feature__("Enum.*", __js__('var Enum = { };'));
+			__feature__("Int.*", js.Syntax.code('var Int = { };'));
+			__feature__("Dynamic.*", js.Syntax.code('var Dynamic = { };'));
+			__feature__("Float.*", js.Syntax.code('var Float = Number'));
+			__feature__("Bool.*", js.Syntax.code('var Bool = Boolean'));
+			__feature__("Class.*", js.Syntax.code('var Class = { };'));
+			__feature__("Enum.*", js.Syntax.code('var Enum = { };'));
 			#if (js_es < 5)
 			__feature__("Array.map", if (Array.prototype.map == null) Array.prototype.map = function(f) {
 				var a = [];

+ 4 - 4
std/js/_std/Type.hx

@@ -129,13 +129,13 @@ enum ValueType {
 
 	public static function createEmptyInstance<T>(cl:Class<T>):T
 		untyped {
-			__js__("function empty() {}; empty.prototype = cl.prototype");
-			return __js__("new empty()");
+			js.Syntax.code("function empty() {}; empty.prototype = cl.prototype");
+			return js.Syntax.code("new empty()");
 		}
 	#else
 	public static function createInstance<T>(cl:Class<T>, args:Array<Dynamic>):T
 		untyped {
-			return untyped __js__("new ({0})", Function.prototype.bind.apply(cl, [null].concat(args)));
+			return js.Syntax.code("new ({0})", Function.prototype.bind.apply(cl, [null].concat(args)));
 		}
 
 	public static inline function createEmptyInstance<T>(cl:Class<T>):T {
@@ -201,7 +201,7 @@ enum ValueType {
 	#else
 	public static function getInstanceFields(c:Class<Dynamic>):Array<String> {
 		var a = [];
-		untyped __js__("for(var i in c.prototype) a.push(i)");
+		js.Syntax.code("for(var i in c.prototype) a.push(i)");
 		a.remove("__class__");
 		a.remove("__properties__");
 		return a;

+ 1 - 1
std/js/_std/haxe/ds/IntMap.hx

@@ -50,7 +50,7 @@ package haxe.ds;
 
 	public function keys():Iterator<Int> {
 		var a = [];
-		untyped __js__("for( var key in {0} ) {1}", h, if (h.hasOwnProperty(key)) a.push(key | 0));
+		js.Syntax.code("for( var key in {0} ) if({0}.hasOwnProperty(key)) {1}.push(key | 0)", h, a);
 		return a.iterator();
 	}
 

+ 4 - 4
std/js/_std/haxe/ds/ObjectMap.hx

@@ -70,18 +70,18 @@ class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
 		var id = getId(key);
 		if (untyped h.__keys__[id] == null)
 			return false;
-		untyped __js__("delete")(h[id]);
-		untyped __js__("delete")(h.__keys__[id]);
+		js.Syntax.delete(h, id);
+		js.Syntax.delete(h.__keys__, id);
 		return true;
 	}
 
 	public function keys():Iterator<K> {
 		var a = [];
 		untyped {
-			__js__("for( var key in this.h.__keys__ ) {");
+			js.Syntax.code("for( var key in this.h.__keys__ ) {");
 			if (h.hasOwnProperty(key))
 				a.push(h.__keys__[key]);
-			__js__("}");
+			js.Syntax.code("}");
 		}
 		return a.iterator();
 	}

+ 6 - 6
std/js/_std/haxe/ds/StringMap.hx

@@ -152,7 +152,7 @@ private class StringMapIterator<T> {
 	}
 
 	inline function isReserved(key:String):Bool {
-		return untyped __js__("__map_reserved")[key] != null;
+		return js.Syntax.code("__map_reserved[{0}]", key) != null;
 	}
 
 	public inline function set(key:String, value:T):Void {
@@ -212,17 +212,17 @@ private class StringMapIterator<T> {
 	function arrayKeys():Array<String> {
 		var out = [];
 		untyped {
-			__js__("for( var key in this.h ) {");
+			js.Syntax.code("for( var key in this.h ) {");
 			if (h.hasOwnProperty(key))
 				out.push(key);
-			__js__("}");
+			js.Syntax.code("}");
 		}
 		if (rh != null)
 			untyped {
-				__js__("for( var key in this.rh ) {");
+				js.Syntax.code("for( var key in this.rh ) {");
 				if (key.charCodeAt(0) == "$".code)
 					out.push(key.substr(1));
-				__js__("}");
+				js.Syntax.code("}");
 			}
 		return out;
 	}
@@ -264,7 +264,7 @@ private class StringMapIterator<T> {
 	}
 
 	static function __init__():Void {
-		untyped __js__("var __map_reserved = {};");
+		js.Syntax.code("var __map_reserved = {};");
 	}
 }
 #end

+ 2 - 2
tests/misc/projects/Issue5525/Main.hx

@@ -12,7 +12,7 @@ class Main {
 @:arrayAccess
 abstract A {
   inline public function new() {
-    this = untyped __js__("{}");
+    this = js.Syntax.code("{}");
   }
 }
 
@@ -21,6 +21,6 @@ typedef TB = B;
 @:arrayAccess
 abstract B(TB) {
   inline public function new() {
-    this = untyped __js__("{}");
+    this = js.Syntax.code("{}");
   }
 }

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

@@ -4,9 +4,8 @@ import TestJs.use;
 
 class Issue5745 {
 	@:js('
-		var fn = "filename";
-		var v = cat(fn);
-		runProgram.apply(undefined, ["rm",fn]);
+		var v = cat("filename");
+		runProgram.apply(undefined, ["rm","filename"]);
 		TestJs.use(v);
 	')
     static function test() {
@@ -19,9 +18,9 @@ class Issue5745 {
 
 class Shell {
     public inline static function runProgram(args:Array<String>):Int {
-        return untyped __js__('runProgram.apply(undefined, {0})', args);
+        return js.Syntax.code('runProgram.apply(undefined, {0})', args);
     }
     public inline static function cat(v:String):String {
-        return untyped __js__('cat({0})', v);
+        return js.Syntax.code('cat({0})', v);
     }
 }

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

@@ -5,7 +5,7 @@ private class MyType<@:const T> {
     public function new() { }
 
 	public inline function constGenericInlineWtf() {
-        untyped __js__('console.log({0})', T);
+        js.Syntax.code('console.log({0})', T);
     }
 }
 

+ 1 - 1
tests/unit/src/unit/issues/Issue3226.hx

@@ -5,7 +5,7 @@ class Issue3226 extends Test {
 	function testJs() {
 		var a = 1;
 		var v = 2;
-		untyped __js__("{0} = {0} + {1}", a, v);
+		a = js.Syntax.code("{0} + {1}", a, v);
 		eq(3, a);
 	}
 	#elseif (cpp && !cppia)

+ 1 - 2
tests/unit/src/unit/issues/Issue4085.hx

@@ -3,9 +3,8 @@ package unit.issues;
 class Issue4085 extends Test {
 	#if js
 	function test() {
-		function throwError() throw "hello, world";
 		var msg = null;
-		untyped __js__("try { throwError(); } catch (e) { msg = e.message; }");
+		js.Syntax.code("try { ({0})(); } catch (e) { ({1})(e); }", () -> throw "hello, world", e -> msg = e.message);
 		eq(msg, "hello, world");
 	}
 	#end

+ 11 - 11
tests/unit/src/unit/issues/Issue4644.hx

@@ -1,25 +1,25 @@
 package unit.issues;
 
 class Issue4644 extends Test {
+#if js
+
 	function test() {
-		#if js
-		var isHaxeError;
-		untyped __js__(
+		var isHaxeError = true;
+		js.Syntax.code(
 			"try {{
-				{0};
+				({0})();
 			}} catch (e) {{
-				{1} = (e instanceof {2});
+				({1})(e instanceof {2});
 			}}",
-			throw (new js.lib.Error() : Dynamic),
-			isHaxeError,
+			() -> throw (new js.lib.Error():Dynamic),
+			b -> isHaxeError = b,
 			#if js_unflatten
-			__js__("js._Boot.HaxeError")
+			js.Syntax.code("js._Boot.HaxeError")
 			#else
-			__js__("js__$Boot_HaxeError")
+			js.Syntax.code("js__$Boot_HaxeError")
 			#end
 		);
 		f(isHaxeError);
-		#end
-		noAssert();
 	}
+#end
 }

+ 1 - 1
tests/unit/src/unit/issues/Issue4862.hx

@@ -7,7 +7,7 @@ private extern enum abstract HttpStatus(Int) to Int {
     var NotFound;
 
     static function __init__():Void {
-        untyped __js__("var __issue4862__http_status = {Ok: 200, NotFound: 404};");
+        js.Syntax.code("var __issue4862__http_status = {Ok: 200, NotFound: 404};");
     }
 }
 #end

+ 1 - 1
tests/unit/src/unit/issues/Issue6448.hx

@@ -13,7 +13,7 @@ private extern class Lib {
 	static function returnTrue():Bool;
 
 	static function __init__():Void {
-		untyped __js__("function ___hx_returnTrue() { return true; }");
+		js.Syntax.code("function ___hx_returnTrue() { return true; }");
 	}
 }
 #end

+ 1 - 1
tests/unit/src/unit/issues/Issue6449.hx

@@ -8,7 +8,7 @@ class Issue6449 extends unit.Test {
 	}
 
 	static function doTest(isNaN:Float):Bool {
-		return untyped __js__("isNaN")(isNaN);
+		return js.Syntax.code("isNaN")(isNaN);
 	}
 	#end
 }