Pārlūkot izejas kodu

Lua: Fix up instanceof method to better handle enum comparison, and to use fewer untyped operations

Justin Donaldson 9 gadi atpakaļ
vecāks
revīzija
1dc597c54b
1 mainītis faili ar 24 papildinājumiem un 18 dzēšanām
  1. 24 18
      std/lua/Boot.hx

+ 24 - 18
std/lua/Boot.hx

@@ -91,39 +91,45 @@ class Boot {
 		switch( cl ) {
 		switch( cl ) {
 			case Int:
 			case Int:
 				// TODO: matching js behavior here, but js behavior clamps.  Is that correct?
 				// TODO: matching js behavior here, but js behavior clamps.  Is that correct?
-				return (untyped __type__(o) == "number" &&  clamp(o) == o);
+				return (Lua.type(o) == "number" &&  clamp(o) == o);
 			case Float:
 			case Float:
-				return untyped __type__(o) == "number";
+				return Lua.type(o) == "number";
 			case Bool:
 			case Bool:
-				return untyped __type__(o) == "boolean";
+				return Lua.type(o) == "boolean";
 			case String:
 			case String:
-				return untyped __type__(o) == "string";
+				return Lua.type(o) == "string";
 			case Array:
 			case Array:
-				return untyped __type__(o) == "table"
+				return Lua.type(o) == "table"
+					&& untyped o.__enum__ == null
 					&& lua.Lua.getmetatable(o) != null
 					&& lua.Lua.getmetatable(o) != null
-					&& lua.Lua.getmetatable(o).__index == Array.prototype;
+					&& lua.Lua.getmetatable(o).__index == untyped Array.prototype;
 			case Table:
 			case Table:
-				return untyped __type__(o) == "table";
+				return Lua.type(o) == "table";
 			case Dynamic:
 			case Dynamic:
 				return true;
 				return true;
 			default: {
 			default: {
-				if (o == null) {
-					return false;
-				} else if ( untyped o.__enum__ != null ){
+				if ( o!= null &&  Lua.type(o)  == "table" && Lua.type(cl) == "table"){
+					// first check if o is instance of cl
+					if (inheritsFrom(o, cl)) return true;
+
+					// do not use isClass/isEnum here, perform raw checks
+					untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
+					untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
+					// last chance, is it an enum instance?
 					return o.__enum__ == cl;
 					return o.__enum__ == cl;
-				} else if (   untyped __type__(o)  == "table"
-					&& untyped __type__(cl) == "table"){
-					while (Lua.getmetatable(o) != null && Lua.getmetatable(o).__index != null){
-						if (Lua.getmetatable(o).__index == cl.prototype) return true;
-						o = Lua.getmetatable(o).__index;
-					}
+				} else {
 					return false;
 					return false;
 				}
 				}
-
-				return false;
 			}
 			}
 		}
 		}
 	}
 	}
+	static function inheritsFrom(o:Dynamic, cl:Class<Dynamic>) : Bool {
+		while (Lua.getmetatable(o) != null && Lua.getmetatable(o).__index != null){
+			if (Lua.getmetatable(o).__index == untyped cl.prototype) return true;
+			o = Lua.getmetatable(o).__index;
+		}
+		return false;
+	}
 
 
 	@:ifFeature("typed_cast")
 	@:ifFeature("typed_cast")
 	private static function __cast(o : Dynamic, t : Dynamic) {
 	private static function __cast(o : Dynamic, t : Dynamic) {