Pārlūkot izejas kodu

#10993 Fix: Reflect.hasField fails on Lua if object is function (#10994)

* Use lua.Lua.type instead of untyped code

* Fix Reflect.hasField fails on Lua if object is function

* [eval] return false for hasField on non-object

closes #10993

---------

Co-authored-by: Simon Krajewski <[email protected]>
Sebastian Thomschke 2 gadi atpakaļ
vecāks
revīzija
36b9ce7c4d

+ 1 - 1
src/macro/eval/evalStdLib.ml

@@ -1950,7 +1950,7 @@ module StdReflect = struct
 				end
 				end
 			| VInstance vi -> IntMap.mem name vi.iproto.pinstance_names || IntMap.mem name vi.iproto.pnames
 			| VInstance vi -> IntMap.mem name vi.iproto.pinstance_names || IntMap.mem name vi.iproto.pnames
 			| VPrototype proto -> IntMap.mem name proto.pnames
 			| VPrototype proto -> IntMap.mem name proto.pnames
-			| _ -> unexpected_value o "object"
+			| _ -> false (* issue #10993 *)
 		in
 		in
 		vbool b
 		vbool b
 	)
 	)

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

@@ -26,10 +26,12 @@ import lua.Boot;
 
 
 @:coreApi class Reflect {
 @:coreApi class Reflect {
 	public inline static function hasField(o:Dynamic, field:String):Bool {
 	public inline static function hasField(o:Dynamic, field:String):Bool {
-		if (Lua.type(o) == "string" && (untyped String.prototype[field] != null || field == "length")) {
-			return true;
+		return if (inline isFunction(o)) {
+			false;
+		} else if (Lua.type(o) == "string" && (untyped String.prototype[field] != null || field == "length")) {
+			true;
 		} else
 		} else
-			return untyped o.__fields__ != null ? o.__fields__[field] != null : o[field] != null;
+			untyped o.__fields__ != null ? o.__fields__[field] != null : o[field] != null;
 	}
 	}
 
 
 	public static function field(o:Dynamic, field:String):Dynamic
 	public static function field(o:Dynamic, field:String):Dynamic
@@ -116,7 +118,7 @@ import lua.Boot;
 		untyped {
 		untyped {
 			if (v == null)
 			if (v == null)
 				return false;
 				return false;
-			var t = __lua__("type(v)");
+			var t = lua.Lua.type(v);
 			return (t == "string" || (t == "table" && v.__enum__ == null))
 			return (t == "string" || (t == "table" && v.__enum__ == null))
 				|| (t == "function" && (lua.Boot.isClass(v) || lua.Boot.isEnum(v)) != null);
 				|| (t == "function" && (lua.Boot.isClass(v) || lua.Boot.isEnum(v)) != null);
 		}
 		}

+ 17 - 0
tests/unit/src/unit/issues/Issue10993.hx

@@ -0,0 +1,17 @@
+package unit.issues;
+
+enum Issue10993_TestEnum {
+	FOO;
+}
+
+class Issue10993 extends Test {
+	function testHasFieldWithEnum() {
+		final foo = Issue10993_TestEnum.FOO;
+		eq(false, Reflect.hasField(foo, "bar"));
+	}
+
+	function testHasFieldWithFunction() {
+		final foo = () -> null;
+		eq(false, Reflect.hasField(foo, "bar"));
+	}
+}