Browse Source

some modifications:
modified getClass (check for __class__ first)
only use __isNativeObj on objects
added fixed reference to {}.toString
added exclusions to __nativeClassName
removed __resolveNativeClass from Type.resolveClass (doesn't work)

mockey 11 years ago
parent
commit
c85d75f9f5
2 changed files with 20 additions and 10 deletions
  1. 19 9
      std/js/Boot.hx
  2. 1 1
      std/js/_std/Type.hx

+ 19 - 9
std/js/Boot.hx

@@ -71,11 +71,13 @@ class Boot {
 		if (Std.is(o, Array))
 		if (Std.is(o, Array))
 			return Array;
 			return Array;
 		else {
 		else {
+			var cl = untyped __define_feature__("js.Boot.getClass", o.__class__);
+			if (cl != null)
+				return cl;
 			var name = __nativeClassName(o);
 			var name = __nativeClassName(o);
-			if (name == "Object" || name == "Function")
-				return untyped __define_feature__("js.Boot.getClass", o.__class__);
-			else
+			if (name != null)
 				return __resolveNativeClass(name);
 				return __resolveNativeClass(name);
+			return null;
 		}
 		}
 	}
 	}
 
 
@@ -186,12 +188,16 @@ class Boot {
 		default:
 		default:
 			if( o != null ) {
 			if( o != null ) {
 				// Check if o is an instance of a Haxe class or a native JS object
 				// Check if o is an instance of a Haxe class or a native JS object
-				if( (untyped __js__("typeof"))(cl) == "function" || __isNativeObj(cl) ) {
+				if( (untyped __js__("typeof"))(cl) == "function" ) {
 					if( untyped __js__("o instanceof cl") )
 					if( untyped __js__("o instanceof cl") )
 						return true;
 						return true;
 					if( __interfLoop(getClass(o),cl) )
 					if( __interfLoop(getClass(o),cl) )
 						return true;
 						return true;
 				}
 				}
+				else if ( (untyped __js__("typeof"))(cl) == "object" && __isNativeObj(cl) ) {
+					if( untyped __js__("o instanceof cl") )
+						return true;
+				}
 			} else {
 			} else {
 				return false;
 				return false;
 			}
 			}
@@ -207,17 +213,21 @@ class Boot {
 		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
 		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
 	}
 	}
 	
 	
+	static var __toStr = untyped __js__("{}.toString");
 	// get native JS [[Class]]
 	// get native JS [[Class]]
 	static function __nativeClassName(o:Dynamic):String {
 	static function __nativeClassName(o:Dynamic):String {
-		return untyped __js__("{}.toString").call(o).slice(8, -1);
+		var name = untyped __toStr.call(o).slice(8, -1);
+		trace(name);
+		// exclude general Object and Function
+		// also exclude Math and JSON, because instanceof cannot be called on them
+		if (name == "Object" || name == "Function" || name == "Math" || name == "JSON")
+			return null;
+		return name;
 	}
 	}
 	
 	
 	// check for usable native JS object
 	// check for usable native JS object
 	static function __isNativeObj(o:Dynamic):Bool {
 	static function __isNativeObj(o:Dynamic):Bool {
-		var name = __nativeClassName(o);
-		// exclude general Object and Function
-		// and also Math and JSON, because instanceof cannot be called on them
-		return name != "Object" && name != "Function" && name != "Math" && name != "JSON";
+		return __nativeClassName(o) != null;
 	}
 	}
 	
 	
 	// resolve native JS class (with window or global):
 	// resolve native JS class (with window or global):

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

@@ -66,7 +66,7 @@ enum ValueType {
 		var cl : Class<Dynamic> = $hxClasses[name];
 		var cl : Class<Dynamic> = $hxClasses[name];
 		// ensure that this is a class
 		// ensure that this is a class
 		if( cl == null || !js.Boot.isClass(cl) )
 		if( cl == null || !js.Boot.isClass(cl) )
-			return js.Boot.__resolveNativeClass(name);
+			return null;
 		return cl;
 		return cl;
 	}
 	}