Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,10 @@ export function createIdentifier(
return expression;
}

export function cloneIdentifier(identifier: Identifier): Identifier {
return createIdentifier(identifier.text, undefined, identifier.symbolId);
}

export function createAnnonymousIdentifier(tsOriginal?: ts.Node, parent?: Node): Identifier {
const expression = createNode(SyntaxKind.Identifier, tsOriginal, parent) as Identifier;
expression.text = "____";
Expand Down
367 changes: 236 additions & 131 deletions src/LuaTransformer.ts

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions src/TSTLErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ export class TSTLErrors {
new TranspileError(`${name} expects ${expected} argument(s) but got ${got}.`, node);

public static InvalidExtensionMetaExtension = (node: ts.Node) =>
new TranspileError(`Cannot use both '!Extension' and '!MetaExtension' decorators on the same class.`, node);
new TranspileError(`Cannot use both '@extension' and '@metaExtension' decorators on the same class.`, node);

public static InvalidNewExpressionOnExtension = (node: ts.Node) =>
new TranspileError(`Cannot construct classes with decorator '!Extension' or '!MetaExtension'.`, node);
new TranspileError(`Cannot construct classes with decorator '@extension' or '@metaExtension'.`, node);

public static InvalidExtendsExtension = (node: ts.Node) =>
new TranspileError(`Cannot extend classes with decorator '@extension' or '@metaExtension'.`, node);

public static InvalidExportsExtension = (node: ts.Node) =>
new TranspileError(`Cannot export classes with decorator '@extension' or '@metaExtension'.`, node);

public static InvalidInstanceOfExtension = (node: ts.Node) =>
new TranspileError(`Cannot use instanceof on classes with decorator '@extension' or '@metaExtension'.`, node);

public static InvalidPropertyCall = (node: ts.Node) =>
new TranspileError(`Tried to transpile a non-property call as property call.`, node);
Expand Down
20 changes: 13 additions & 7 deletions src/lualib/InstanceOf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
interface LuaClass {
__index: LuaClass;
__base: LuaClass;
____super?: LuaClass;
}

function __TS__InstanceOf(obj: LuaClass, classTbl: LuaClass): boolean {
while (obj !== undefined) {
if (obj.__index === classTbl) {
return true;
interface LuaObject {
constructor: LuaClass;
}

function __TS__InstanceOf(obj: LuaObject, classTbl: LuaClass): boolean {
if (obj !== undefined) {
let luaClass = obj.constructor;
while (luaClass !== undefined) {
if (luaClass === classTbl) {
return true;
}
luaClass = luaClass.____super;
}
obj = obj.__base;
}
return false;
}
2 changes: 1 addition & 1 deletion test/translation/lua/classExtension1.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
MyClass.myFunction = function(self)
MyClass.prototype.myFunction = function(self)
end;
2 changes: 1 addition & 1 deletion test/translation/lua/classExtension2.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
TestClass.myFunction = function(self)
TestClass.prototype.myFunction = function(self)
end;
4 changes: 2 additions & 2 deletions test/translation/lua/classExtension3.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RenamedTestClass.myFunction = function(self)
RenamedTestClass.prototype.myFunction = function(self)
end;
RenamedMyClass.myFunction = function(self)
RenamedMyClass.prototype.myFunction = function(self)
end;
2 changes: 1 addition & 1 deletion test/translation/lua/classExtension4.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MyClass.test = "test";
MyClass.testP = "testP";
MyClass.myFunction = function(self)
MyClass.prototype.myFunction = function(self)
end;
13 changes: 7 additions & 6 deletions test/translation/lua/classPureAbstract.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
ClassB = ClassB or {};
ClassB.__index = ClassB;
ClassB.new = function(construct, ...)
local self = setmetatable({}, ClassB);
if construct and ClassB.constructor then
ClassB.constructor(self, ...);
end
ClassB.prototype = ClassB.prototype or {};
ClassB.prototype.__index = ClassB.prototype;
ClassB.prototype.constructor = ClassB;
ClassB.new = function(...)
local self = setmetatable({}, ClassB.prototype);
self:____constructor(...);
return self;
end;
ClassB.constructor = function(self)
ClassB.prototype.____constructor = function(self)
end;
19 changes: 10 additions & 9 deletions test/translation/lua/getSetAccessors.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
MyClass = MyClass or {};
MyClass.__index = MyClass;
MyClass.new = function(construct, ...)
local self = setmetatable({}, MyClass);
if construct and MyClass.constructor then
MyClass.constructor(self, ...);
end
MyClass.prototype = MyClass.prototype or {};
MyClass.prototype.__index = MyClass.prototype;
MyClass.prototype.constructor = MyClass;
MyClass.new = function(...)
local self = setmetatable({}, MyClass.prototype);
self:____constructor(...);
return self;
end;
MyClass.constructor = function(self)
MyClass.prototype.____constructor = function(self)
end;
MyClass.get__field = function(self)
MyClass.prototype.get__field = function(self)
return self._field + 4;
end;
MyClass.set__field = function(self, v)
MyClass.prototype.set__field = function(self, v)
self._field = v * 2;
end;
local instance = MyClass.new(true);
local instance = MyClass.new();
instance:set__field(4);
local b = instance:get__field();
local c = (4 + instance:get__field()) * 3;
15 changes: 8 additions & 7 deletions test/translation/lua/methodRestArguments.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
MyClass = MyClass or {};
MyClass.__index = MyClass;
MyClass.new = function(construct, ...)
local self = setmetatable({}, MyClass);
if construct and MyClass.constructor then
MyClass.constructor(self, ...);
end
MyClass.prototype = MyClass.prototype or {};
MyClass.prototype.__index = MyClass.prototype;
MyClass.prototype.constructor = MyClass;
MyClass.new = function(...)
local self = setmetatable({}, MyClass.prototype);
self:____constructor(...);
return self;
end;
MyClass.constructor = function(self)
MyClass.prototype.____constructor = function(self)
end;
MyClass.varargsFunction = function(self, a, ...)
MyClass.prototype.varargsFunction = function(self, a, ...)
local b = ({...});
end;
13 changes: 7 additions & 6 deletions test/translation/lua/modulesClassExport.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
local exports = exports or {};
exports.TestClass = exports.TestClass or {};
exports.TestClass.__index = exports.TestClass;
exports.TestClass.new = function(construct, ...)
local self = setmetatable({}, exports.TestClass);
if construct and exports.TestClass.constructor then
exports.TestClass.constructor(self, ...);
end
exports.TestClass.prototype = exports.TestClass.prototype or {};
exports.TestClass.prototype.__index = exports.TestClass.prototype;
exports.TestClass.prototype.constructor = TestClass;
exports.TestClass.new = function(...)
local self = setmetatable({}, exports.TestClass.prototype);
self:____constructor(...);
return self;
end;
exports.TestClass.constructor = function(self)
exports.TestClass.prototype.____constructor = function(self)
end;
return exports;
15 changes: 8 additions & 7 deletions test/translation/lua/modulesClassWithMemberExport.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
local exports = exports or {};
exports.TestClass = exports.TestClass or {};
exports.TestClass.__index = exports.TestClass;
exports.TestClass.new = function(construct, ...)
local self = setmetatable({}, exports.TestClass);
if construct and exports.TestClass.constructor then
exports.TestClass.constructor(self, ...);
end
exports.TestClass.prototype = exports.TestClass.prototype or {};
exports.TestClass.prototype.__index = exports.TestClass.prototype;
exports.TestClass.prototype.constructor = TestClass;
exports.TestClass.new = function(...)
local self = setmetatable({}, exports.TestClass.prototype);
self:____constructor(...);
return self;
end;
exports.TestClass.constructor = function(self)
exports.TestClass.prototype.____constructor = function(self)
end;
exports.TestClass.memberFunc = function(self)
exports.TestClass.prototype.memberFunc = function(self)
end;
return exports;
23 changes: 12 additions & 11 deletions test/translation/lua/namespaceMerge.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
MergedClass = MergedClass or {};
MergedClass.__index = MergedClass;
MergedClass.new = function(construct, ...)
local self = setmetatable({}, MergedClass);
self.propertyFunc = function(____)
end;
if construct and MergedClass.constructor then
MergedClass.constructor(self, ...);
end
MergedClass.prototype = MergedClass.prototype or {};
MergedClass.prototype.__index = MergedClass.prototype;
MergedClass.prototype.constructor = MergedClass;
MergedClass.new = function(...)
local self = setmetatable({}, MergedClass.prototype);
self:____constructor(...);
return self;
end;
MergedClass.constructor = function(self)
MergedClass.prototype.____constructor = function(self)
self.propertyFunc = function(____)
end;
end;
MergedClass.staticMethodA = function(self)
end;
MergedClass.staticMethodB = function(self)
self:staticMethodA();
end;
MergedClass.methodA = function(self)
MergedClass.prototype.methodA = function(self)
end;
MergedClass.methodB = function(self)
MergedClass.prototype.methodB = function(self)
self:methodA();
self:propertyFunc();
end;
Expand All @@ -27,7 +28,7 @@ do
MergedClass.namespaceFunc = function()
end;
end
local mergedClass = MergedClass.new(true);
local mergedClass = MergedClass.new();
mergedClass:methodB();
mergedClass:propertyFunc();
MergedClass:staticMethodB();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/assignments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class AssignmentTests {
+ `let [a,b] = jkl.abc();`;

const lua = util.transpileString(code);
Expect(lua).toBe("local jkl = def.new(true);\nlocal a, b = jkl:abc();");
Expect(lua).toBe("local jkl = def.new();\nlocal a, b = jkl:abc();");
}

@Test("TupleReturn functional")
Expand Down
Loading