diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 94070209e..4b6bf8736 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -1155,6 +1155,10 @@ export abstract class LuaTranspiler { const name = this.transpileIdentifier(node.expression.name); if (name === "toString") { return `tostring(${this.transpileExpression(node.expression.expression)})`; + } else if (name === "hasOwnProperty") { + const expr = this.transpileExpression(node.expression.expression); + params = this.transpileArguments(node.arguments); + return `(rawget(${expr}, ${params} )~=nil)`; } else { callPath = `${this.transpileExpression(node.expression.expression)}:${name}`; diff --git a/test/unit/class.spec.ts b/test/unit/class.spec.ts index 6f0060555..ca43f3290 100644 --- a/test/unit/class.spec.ts +++ b/test/unit/class.spec.ts @@ -201,7 +201,44 @@ export class ClassTests { // Assert Expect(result).toBe("instance of a"); } + @Test("HasOwnProperty true") + public hasOwnProperty1(): void { + // Transpile + const lua = util.transpileString( + `class a { + public test(): void { + } + } + let inst = new a(); + inst["prop"] = 17; + return inst.hasOwnProperty("prop");` + ); + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(true); + } + @Test("HasOwnProperty false") + public hasOwnProperty2(): void { + // Transpile + const lua = util.transpileString( + `class a { + public test(): void { + } + } + let inst = new a(); + inst["prop"] = 17; + return inst.hasOwnProperty("test");` + ); + + // Execute + const result = util.executeLua(lua); + + // Assert + Expect(result).toBe(false); + } @Test("CastClassMethodCall") public extraParenthesisAssignment(): void { // Transpile