Skip to content

Commit 1034fe7

Browse files
committed
Added instance to accessors, added unit tests
1 parent 826887b commit 1034fe7

File tree

5 files changed

+106
-11
lines changed

5 files changed

+106
-11
lines changed

src/Transpiler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export class TranspileError extends Error {
1717
}
1818
}
1919

20-
export enum Target {
20+
export enum LuaTarget {
2121
Lua53 = "5.3",
2222
LuaJIT = "JIT",
2323
}
2424

2525
export class LuaTranspiler {
26-
public static AvailableLuaTargets = [Target.LuaJIT, Target.Lua53];
26+
public static AvailableLuaTargets = [LuaTarget.LuaJIT, LuaTarget.Lua53];
2727

2828
// Transpile a source file
2929
public static transpileSourceFile(node: ts.SourceFile,
@@ -627,7 +627,7 @@ export class LuaTranspiler {
627627
let result = "";
628628

629629
// Transpile Bitops
630-
if (this.options.luaTarget === Target.LuaJIT) {
630+
if (this.options.luaTarget === LuaTarget.LuaJIT) {
631631
switch (node.operatorToken.kind) {
632632
case ts.SyntaxKind.AmpersandToken:
633633
result = `bit.band(${lhs},${rhs})`;
@@ -981,7 +981,7 @@ export class LuaTranspiler {
981981
fromCodePoint: "utf8.char",
982982
};
983983

984-
if (identifier.escapedText as string === "fromCodePoint" && this.options.luaTarget !== Target.Lua53) {
984+
if (identifier.escapedText as string === "fromCodePoint" && this.options.luaTarget !== LuaTarget.Lua53) {
985985
throw new TranspileError(
986986
`Unsupported string property ${identifier.escapedText} is only supported for lua 5.3.`,
987987
identifier
@@ -1079,13 +1079,13 @@ export class LuaTranspiler {
10791079
public transpileGetAccessor(node: ts.PropertyAccessExpression): string {
10801080
const name = node.name.escapedText;
10811081
const expression = this.transpileExpression(node.expression);
1082-
return `${expression}.get__${name}()`;
1082+
return `${expression}:get__${name}()`;
10831083
}
10841084

10851085
public transpileSetAccessor(node: ts.PropertyAccessExpression, value: string): string {
10861086
const name = node.name.escapedText;
10871087
const expression = this.transpileExpression(node.expression);
1088-
return `${expression}.set__${name}(${value})`;
1088+
return `${expression}:set__${name}(${value})`;
10891089
}
10901090

10911091
// Transpile a Math._ property
@@ -1407,7 +1407,7 @@ export class LuaTranspiler {
14071407
public transpileGetAccessorDeclaration(getAccessor: ts.GetAccessorDeclaration, className: string): string {
14081408
const name = (getAccessor.name as ts.Identifier).escapedText;
14091409

1410-
let result = this.indent + `function ${className}.get__${name}()\n`;
1410+
let result = this.indent + `function ${className}.get__${name}(self)\n`;
14111411

14121412
this.pushIndent();
14131413
result += this.transpileBlock(getAccessor.body);
@@ -1421,7 +1421,7 @@ export class LuaTranspiler {
14211421
public transpileSetAccessorDeclaration(setAccessor: ts.SetAccessorDeclaration, className: string): string {
14221422
const name = (setAccessor.name as ts.Identifier).escapedText;
14231423

1424-
const paramNames: string[] = [];
1424+
const paramNames: string[] = ["self"];
14251425
setAccessor.parameters.forEach((param) => {
14261426
paramNames.push((param.name as ts.Identifier).escapedText as string);
14271427
});

test/src/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import * as path from "path";
33

44
import { Expect } from "alsatian";
55

6-
import { LuaTranspiler, TranspileError } from "../../src/Transpiler";
6+
import { LuaTranspiler, TranspileError, LuaTarget } from "../../src/Transpiler";
77
import { CompilerOptions } from "../../src/CommandLineParser";
88

99
const LuaVM = require("lua.vm.js");
1010
const fs = require("fs");
1111

1212
const libSource = fs.readFileSync(path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts')).toString();
1313

14-
export function transpileString(str: string, options: CompilerOptions = { dontRequireLuaLib: true }): string {
14+
export function transpileString(str: string, options: CompilerOptions = { dontRequireLuaLib: true, luaTarget: LuaTarget.LuaJIT }): string {
1515
let compilerHost = {
1616
getSourceFile: (filename, languageVersion) => {
1717
if (filename === "file.ts") {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MyClass = MyClass or {}
2+
MyClass.__index = MyClass
3+
function MyClass.new(construct, ...)
4+
local instance = setmetatable({}, MyClass)
5+
if construct and MyClass.constructor then MyClass.constructor(instance, ...) end
6+
return instance
7+
end
8+
function MyClass.constructor(self)
9+
end
10+
function MyClass.get__field(self)
11+
return self._field+4
12+
end
13+
function MyClass.set__field(self,v)
14+
self._field=(v*2)
15+
end
16+
local instance = MyClass.new(true)
17+
18+
instance:set__field(4)
19+
local b = instance:get__field()
20+
21+
local c = (4+instance:get__field())*3
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class MyClass {
2+
private _field: number;
3+
public get field(): number {
4+
return this._field + 4;
5+
}
6+
public set field(v: number) {
7+
this._field = v*2;
8+
}
9+
}
10+
11+
var instance = new MyClass();
12+
instance.field = 4;
13+
const b = instance.field;
14+
const c = (4 + instance.field)*3;

test/unit/expressions.spec.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Expect, Test, TestCase } from "alsatian";
1+
import { Expect, Test, TestCase, FocusTest } from "alsatian";
2+
import { LuaTarget } from "../../src/Transpiler";
23

34
import * as ts from "typescript";
45
import * as util from "../src/util";
@@ -196,4 +197,63 @@ export class ExpressionTests {
196197
// Assert
197198
Expect(result).toBe(v1 + v2);
198199
}
200+
201+
@TestCase("inst.field", 8)
202+
@TestCase("inst.field + 3", 8 + 3)
203+
@TestCase("inst.field * 3", 8 * 3)
204+
@TestCase("inst.field / 3", 8 / 3)
205+
@TestCase("inst.field && 3", 8 && 3)
206+
@TestCase("inst.field || 3", 8 || 3)
207+
// @TestCase("inst.field & 3", 8 & 3)
208+
// @TestCase("inst.field | 3", 8 | 3)
209+
// @TestCase("inst.field << 3", 8 << 3)
210+
// @TestCase("inst.field >> 1", 8 >> 1)
211+
@TestCase(`"abc" + inst.field`, "abc8")
212+
public getAccessorBinary(expression: string, expected: any) {
213+
const source = `class MyClass {`
214+
+ ` public _field: number;`
215+
+ ` public get field(): number { return this._field + 4; }`
216+
+ ` public set field(v: number) { this._field = v; }`
217+
+ `}`
218+
+ `var inst = new MyClass();`
219+
+ `inst._field = 4;`
220+
+ `return ${expression};`;
221+
222+
// Transpile
223+
const lua = util.transpileString(source);
224+
225+
// Execute
226+
const result = util.executeLua(lua);
227+
228+
// Assert
229+
Expect(result).toBe(expected);
230+
}
231+
232+
@TestCase("= 4", 4 + 4)
233+
@TestCase("+= 3", 4 + 3 + 4)
234+
@TestCase("*= 3", 4 * 3 + 4)
235+
@TestCase("/= 3", 4 / 3 + 4)
236+
// @TestCase("&= 3", 4 & 3 + 4)
237+
// @TestCase("|= 3", 4 | 3 + 4)
238+
// @TestCase("<<= 3", 4 << 3 + 4)
239+
// @TestCase(">>= 3", 4 >> 3 + 4)
240+
public setAccessorBinary(expression: string, expected: any) {
241+
const source = `class MyClass {`
242+
+ ` public _field: number = 4;`
243+
+ ` public get field(): number { return this._field; }`
244+
+ ` public set field(v: number) { this._field = v + 4; }`
245+
+ `}`
246+
+ `var inst = new MyClass();`
247+
+ `inst.field ${expression};`
248+
+ `return inst._field;`;
249+
250+
// Transpile
251+
const lua = util.transpileString(source);
252+
253+
// Execute
254+
const result = util.executeLua(lua);
255+
256+
// Assert
257+
Expect(result).toBe(expected);
258+
}
199259
}

0 commit comments

Comments
 (0)