From 36b9ce7c4dac91839eacf987d432cee73152f03c Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Sat, 4 Mar 2023 16:36:15 +0100 Subject: [PATCH] #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 --- src/macro/eval/evalStdLib.ml | 2 +- std/lua/_std/Reflect.hx | 10 ++++++---- tests/unit/src/unit/issues/Issue10993.hx | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 tests/unit/src/unit/issues/Issue10993.hx diff --git a/src/macro/eval/evalStdLib.ml b/src/macro/eval/evalStdLib.ml index 313933591c3..fabeb298e56 100644 --- a/src/macro/eval/evalStdLib.ml +++ b/src/macro/eval/evalStdLib.ml @@ -1950,7 +1950,7 @@ module StdReflect = struct end | VInstance vi -> IntMap.mem name vi.iproto.pinstance_names || IntMap.mem name vi.iproto.pnames | VPrototype proto -> IntMap.mem name proto.pnames - | _ -> unexpected_value o "object" + | _ -> false (* issue #10993 *) in vbool b ) diff --git a/std/lua/_std/Reflect.hx b/std/lua/_std/Reflect.hx index 600dc01c1b1..ef4d5950e8e 100644 --- a/std/lua/_std/Reflect.hx +++ b/std/lua/_std/Reflect.hx @@ -26,10 +26,12 @@ import lua.Boot; @:coreApi class Reflect { 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 - 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 @@ -116,7 +118,7 @@ import lua.Boot; untyped { if (v == null) return false; - var t = __lua__("type(v)"); + var t = lua.Lua.type(v); return (t == "string" || (t == "table" && v.__enum__ == null)) || (t == "function" && (lua.Boot.isClass(v) || lua.Boot.isEnum(v)) != null); } diff --git a/tests/unit/src/unit/issues/Issue10993.hx b/tests/unit/src/unit/issues/Issue10993.hx new file mode 100644 index 00000000000..4be324ca523 --- /dev/null +++ b/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")); + } +}