Skip to content

Commit ca573cb

Browse files
committed
Implemented typeof and instanceof
1 parent 63baa4f commit ca573cb

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

dist/lualib/typescript.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ function TS_push(list, ...)
126126
end
127127
end
128128

129+
function TS_instanceof(obj, class)
130+
while obj ~= nil do
131+
if obj.__index == class then
132+
return true
133+
end
134+
obj = obj.__base
135+
end
136+
return false
137+
end
138+
129139
-- Set data structure implementation
130140
Set = Set or {}
131141
Set.__index = Set

src/Transpiler.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ export abstract class LuaTranspiler {
597597
// Otherwise simply return the name
598598
return (node as ts.Identifier).text;
599599
case ts.SyntaxKind.StringLiteral:
600+
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
600601
const text = (node as ts.StringLiteral).text;
601602
return `"${text}"`;
602603
case ts.SyntaxKind.TemplateExpression:
@@ -639,6 +640,8 @@ export abstract class LuaTranspiler {
639640
case ts.SyntaxKind.AsExpression:
640641
// Also ignore as casts
641642
return this.transpileExpression((node as ts.AsExpression).expression);
643+
case ts.SyntaxKind.TypeOfExpression:
644+
return this.transpileTypeOfExpression(node as ts.TypeOfExpression);
642645
default:
643646
throw new TranspileError(
644647
"Unsupported expression kind: " + tsHelper.enumName(node.kind, ts.SyntaxKind),
@@ -755,6 +758,9 @@ export abstract class LuaTranspiler {
755758
case ts.SyntaxKind.InKeyword:
756759
result = `${rhs}[${lhs}]~=nil`;
757760
break;
761+
case ts.SyntaxKind.InstanceOfKeyword:
762+
result = `TS_instanceof(${lhs}, ${rhs})`;
763+
break;
758764
default:
759765
throw new TranspileError(
760766
"Unsupported binary operator kind: " + ts.tokenToString(node.operatorToken.kind),
@@ -1124,6 +1130,11 @@ export abstract class LuaTranspiler {
11241130
}
11251131
}
11261132

1133+
public transpileTypeOfExpression(node: ts.TypeOfExpression): string {
1134+
const expression = this.transpileExpression(node.expression);
1135+
return `type(${expression})`;
1136+
}
1137+
11271138
// Transpile a variable statement
11281139
public transpileVariableStatement(node: ts.VariableStatement): string {
11291140
let result = "";

test/unit/typechecking.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Expect, Test, TestCase, FocusTests } from "alsatian";
1+
import { Expect, Test, TestCase } from "alsatian";
22

33
import * as util from "../src/util";
44

5-
@FocusTests
65
export class OverloadTests {
76
@TestCase("0")
87
@TestCase("30")
@@ -89,7 +88,7 @@ export class OverloadTests {
8988
+ "let inst = new myClass(); return inst instanceof childClass;");
9089
const result = util.executeLua(lua);
9190

92-
Expect(result).toBeTruthy();
91+
Expect(result).toBe(false);
9392
}
9493

9594
@Test("null instanceof Object")

0 commit comments

Comments
 (0)