소스 검색

[js] add js.Lib.typeof and use that instead of untyped magic

Dan Korostelev 8 년 전
부모
커밋
ce4fd4c36d
4개의 변경된 파일45개의 추가작업 그리고 33개의 파일을 삭제
  1. 22 24
      std/js/Boot.hx
  2. 10 1
      std/js/Lib.hx
  3. 6 4
      std/js/_std/Reflect.hx
  4. 7 4
      std/js/_std/Type.hx

+ 22 - 24
std/js/Boot.hx

@@ -45,23 +45,21 @@ class Boot {
 	}
 
 	private static function __trace(v,i : haxe.PosInfos) {
-		untyped {
-			var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
-			#if jsfl
-			msg += __string_rec(v,"");
-			fl.trace(msg);
-			#else
-			msg += __string_rec(v, "");
-			if( i != null && i.customParams != null )
-				for( v in i.customParams )
-					msg += "," + __string_rec(v, "");
-			var d;
-			if( __js__("typeof")(document) != "undefined" && (d = document.getElementById("haxe:trace")) != null )
-				d.innerHTML += __unhtml(msg)+"<br/>";
-			else if( __js__("typeof console") != "undefined" && __js__("console").log != null )
-				__js__("console").log(msg);
-			#end
-		}
+		var msg = if (i != null) i.fileName + ":" + i.lineNumber + ": " else "";
+		#if jsfl
+		msg += __string_rec(v,"");
+		(untyped fl).trace(msg);
+		#else
+		msg += __string_rec(v, "");
+		if (i != null && i.customParams != null)
+			for (v in i.customParams)
+				msg += "," + __string_rec(v, "");
+		var d;
+		if( js.Lib.typeof(untyped document) != "undefined" && (d = (untyped document).getElementById("haxe:trace")) != null )
+			d.innerHTML += __unhtml(msg)+"<br/>";
+		else if( js.Lib.typeof(untyped console) != "undefined" && (untyped console).log != null )
+			(untyped console).log(msg);
+		#end
 	}
 
 	private static function __clear_trace() {
@@ -105,7 +103,7 @@ class Boot {
 			    return "null";
 			if( s.length >= 5 )
 				return "<...>"; // too much deep recursion
-			var t = __js__("typeof(o)");
+			var t = js.Lib.typeof(o);
 			if( t == "function" && (isClass(o) || isEnum(o)) )
 				t = "object";
 			switch( t ) {
@@ -191,13 +189,13 @@ class Boot {
 			return false;
 		switch( cl ) {
 		case Int:
-			return (untyped __js__("typeof"))(o) == "number" && untyped __js__("(o|0) === o");
+			return js.Lib.typeof(o) == "number" && untyped __js__("(o|0) === o");
 		case Float:
-			return (untyped __js__("typeof"))(o) == "number";
+			return js.Lib.typeof(o) == "number";
 		case Bool:
-			return (untyped __js__("typeof"))(o) == "boolean";
+			return js.Lib.typeof(o) == "boolean";
 		case String:
-			return (untyped __js__("typeof"))(o) == "string";
+			return js.Lib.typeof(o) == "string";
 		case Array:
 			return (untyped __js__("(o instanceof Array)")) && o.__enum__ == null;
 		case Dynamic:
@@ -205,13 +203,13 @@ class Boot {
 		default:
 			if( o != null ) {
 				// Check if o is an instance of a Haxe class or a native JS object
-				if( (untyped __js__("typeof"))(cl) == "function" ) {
+				if( js.Lib.typeof(cl) == "function" ) {
 					if( untyped __js__("o instanceof cl") )
 						return true;
 					if( __interfLoop(getClass(o),cl) )
 						return true;
 				}
-				else if ( (untyped __js__("typeof"))(cl) == "object" && __isNativeObj(cl) ) {
+				else if ( js.Lib.typeof(cl) == "object" && __isNativeObj(cl) ) {
 					if( untyped __js__("o instanceof cl") )
 						return true;
 				}

+ 10 - 1
std/js/Lib.hx

@@ -22,7 +22,7 @@
 package js;
 
 /**
-	Platform-specific JavaScript Library. Provides some platform-specific functions 
+	Platform-specific JavaScript Library. Provides some platform-specific functions
 	for the JavaScript target.
 **/
 class Lib {
@@ -87,6 +87,15 @@ class Lib {
 		return untyped __js__("this");
 	}
 
+	/**
+		Call JavaScript `typeof` operator on the `o` value
+		and return a string representing the JavaScript type of a value.
+
+		Read more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
+	**/
+	@:extern public static inline function typeof(o:Dynamic):String {
+		return untyped __typeof__(o);
+	}
 
 	/**
 		An alias of the JS "global" object.

+ 6 - 4
std/js/_std/Reflect.hx

@@ -59,8 +59,9 @@
 		return a;
 	}
 
-	public static function isFunction( f : Dynamic ) : Bool untyped {
-		return __js__("typeof(f)") == "function" && !(js.Boot.isClass(f) || js.Boot.isEnum(f));
+	@:access(js.Boot)
+	public static function isFunction( f : Dynamic ) : Bool {
+		return js.Lib.typeof(f) == "function" && !(js.Boot.isClass(f) || js.Boot.isEnum(f));
 	}
 
 	public static function compare<T>( a : T, b : T ) : Int {
@@ -75,10 +76,11 @@
 		return f1.scope == f2.scope && f1.method == f2.method && f1.method != null;
 	}
 
-	public static function isObject( v : Dynamic ) : Bool untyped {
+	@:access(js.Boot)
+	public static function isObject( v : Dynamic ) : Bool {
 		if( v == null )
 			return false;
-		var t = __js__("typeof(v)");
+		var t = js.Lib.typeof(v);
 		return (t == "string" || (t == "object" && v.__enum__ == null)) || (t == "function" && (js.Boot.isClass(v) || js.Boot.isEnum(v)) != null);
 	}
 

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

@@ -160,10 +160,13 @@ enum ValueType {
 		return ((cast e).__constructs__ : Array<String>).copy();
 	}
 
-	public static function typeof( v : Dynamic ) : ValueType untyped {
-		switch( __js__("typeof")(v) ) {
-		case "boolean": return TBool;
-		case "string": return TClass(String);
+	@:access(js.Boot)
+	public static function typeof( v : Dynamic ) : ValueType {
+		switch (js.Lib.typeof(v)) {
+		case "boolean":
+			return TBool;
+		case "string":
+			return TClass(String);
 		case "number":
 			// this should handle all cases : NaN, +/-Inf and Floats outside range
 			if( Math.ceil(v) == v%2147483648.0 )