Skip to content

Commit 3cb7c4f

Browse files
andreiraduPerryvw
authored andcommitted
-added implementation for Object.hasOwnProperty (#235)
1 parent 1c9eca6 commit 3cb7c4f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Transpiler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,10 @@ export abstract class LuaTranspiler {
11621162
const name = this.transpileIdentifier(node.expression.name);
11631163
if (name === "toString") {
11641164
return `tostring(${this.transpileExpression(node.expression.expression)})`;
1165+
} else if (name === "hasOwnProperty") {
1166+
const expr = this.transpileExpression(node.expression.expression);
1167+
params = this.transpileArguments(node.arguments);
1168+
return `(rawget(${expr}, ${params} )~=nil)`;
11651169
} else {
11661170
callPath =
11671171
`${this.transpileExpression(node.expression.expression)}:${name}`;

test/unit/class.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,44 @@ export class ClassTests {
201201
// Assert
202202
Expect(result).toBe("instance of a");
203203
}
204+
@Test("HasOwnProperty true")
205+
public hasOwnProperty1(): void {
206+
// Transpile
207+
const lua = util.transpileString(
208+
`class a {
209+
public test(): void {
210+
}
211+
}
212+
let inst = new a();
213+
inst["prop"] = 17;
214+
return inst.hasOwnProperty("prop");`
215+
);
204216

217+
// Execute
218+
const result = util.executeLua(lua);
219+
220+
// Assert
221+
Expect(result).toBe(true);
222+
}
223+
@Test("HasOwnProperty false")
224+
public hasOwnProperty2(): void {
225+
// Transpile
226+
const lua = util.transpileString(
227+
`class a {
228+
public test(): void {
229+
}
230+
}
231+
let inst = new a();
232+
inst["prop"] = 17;
233+
return inst.hasOwnProperty("test");`
234+
);
235+
236+
// Execute
237+
const result = util.executeLua(lua);
238+
239+
// Assert
240+
Expect(result).toBe(false);
241+
}
205242
@Test("CastClassMethodCall")
206243
public extraParenthesisAssignment(): void {
207244
// Transpile

0 commit comments

Comments
 (0)