Skip to content

Commit

Permalink
#10993 Fix: Reflect.hasField fails on Lua if object is function (#10994)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
sebthom and Simn authored Mar 4, 2023
1 parent 45f59a6 commit 36b9ce7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/macro/eval/evalStdLib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
10 changes: 6 additions & 4 deletions std/lua/_std/Reflect.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/src/unit/issues/Issue10993.hx
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 36b9ce7

Please sign in to comment.