Skip to content

Commit 9442bf6

Browse files
Chris Bajorinchrisbajorin
andauthored
handle non-indexable types for lefthand value of instanceof (#837)
* handle non-indexable types for lefthand value of instanceof * cleanup Co-authored-by: Chris Bajorin <chris@chrisbajorin.com>
1 parent 1a1e369 commit 9442bf6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/lualib/InstanceOf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function __TS__InstanceOf(this: void, obj: LuaClassInstance, classTbl: LuaClass)
88
return !!classTbl[Symbol.hasInstance](obj);
99
}
1010

11-
if (obj !== undefined) {
11+
if (typeof obj === "object") {
1212
let luaClass = obj.constructor;
1313
while (luaClass !== undefined) {
1414
if (luaClass === classTbl) {

test/unit/classes/instanceof.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ test("null instanceof Class", () => {
4949
`.expectToMatchJsResult();
5050
});
5151

52+
test("function instanceof Class", () => {
53+
util.testFunction`
54+
class myClass {}
55+
const noop = () => {};
56+
return (noop as any) instanceof myClass;
57+
`.expectToMatchJsResult();
58+
});
59+
60+
test("boolean instanceof Class", () => {
61+
util.testFunction`
62+
class myClass {}
63+
return (false as any) instanceof myClass;
64+
`.expectToMatchJsResult();
65+
});
66+
67+
test("number instanceof Class", () => {
68+
util.testFunction`
69+
class myClass {}
70+
return (5 as any) instanceof myClass;
71+
`.expectToMatchJsResult();
72+
});
73+
5274
test("instanceof export", () => {
5375
util.testModule`
5476
export class myClass {}

0 commit comments

Comments
 (0)