X Tutup
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
3 changes: 3 additions & 0 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export class TSTLErrors {
public static InvalidPropertyCall = (node: ts.Node) =>
new TranspileError(`Tried to transpile a non-property call as property call.`, node)

public static InvalidElementCall = (node: ts.Node) =>
new TranspileError(`Tried to transpile a non-element call as an element call.`, node)

public static InvalidThrowExpression = (node: ts.Node) =>
new TranspileError(`Invalid throw expression, only strings can be thrown.`, node)

Expand Down
61 changes: 50 additions & 11 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1171,11 +1171,16 @@ export abstract class LuaTranspiler {
&& tsHelper.isInTupleReturnFunction(node, this.checker);
const isInDestructingAssignment = tsHelper.isInDestructingAssignment(node);
const returnValueIsUsed = node.parent && !ts.isExpressionStatement(node.parent);
const wrapResult = isTupleReturn && !isTupleReturnForward && !isInDestructingAssignment && returnValueIsUsed;

if (ts.isPropertyAccessExpression(node.expression)) {
const result = this.transpilePropertyCall(node);
return isTupleReturn && !isTupleReturnForward && !isInDestructingAssignment && returnValueIsUsed
? `({ ${result} })` : result;
return wrapResult ? `({ ${result} })` : result;
}

if (ts.isElementAccessExpression(node.expression)) {
const result = this.transpileElementCall(node);
return wrapResult ? `({ ${result} })` : result;
}

const signature = this.checker.getResolvedSignature(node);
Expand All @@ -1198,8 +1203,7 @@ export abstract class LuaTranspiler {
} else {
parameters = this.transpileArguments(node.arguments, signature);
}
return isTupleReturn && !isTupleReturnForward && !isInDestructingAssignment && returnValueIsUsed
? `({ ${callPath}(${parameters}) })` : `${callPath}(${parameters})`;
return wrapResult ? `({ ${callPath}(${parameters}) })` : `${callPath}(${parameters})`;
}

public transpilePropertyCall(node: ts.CallExpression): string {
Expand All @@ -1218,7 +1222,7 @@ export abstract class LuaTranspiler {
return this.transpileMathExpression(node.expression.name) + `(${parameters})`;
}

if (this.transpileExpression((node.expression as ts.PropertyAccessExpression).expression) === "String") {
if (this.transpileExpression(node.expression.expression) === "String") {
parameters = this.transpileArguments(node.arguments);
return this.transpileStringExpression(node.expression.name) + `(${parameters})`;
}
Expand All @@ -1241,12 +1245,6 @@ export abstract class LuaTranspiler {
return this.transpileArrayCallExpression(node);
}

// if ownerType inherits from an array, use array calls where appropriate
if (tsHelper.isArrayType(ownerType, this.checker)
&& tsHelper.isDefaultArrayCallMethodName(this.transpileIdentifier(node.expression.name))) {
return this.transpileArrayCallExpression(node);
}

if (tsHelper.isFunctionType(ownerType, this.checker)) {
return this.transpileFunctionCallExpression(node);
}
Expand Down Expand Up @@ -1279,6 +1277,47 @@ export abstract class LuaTranspiler {
}
}

public transpileElementCall(node: ts.CallExpression): string {
if (!ts.isElementAccessExpression(node.expression)) {
throw TSTLErrors.InvalidElementCall(node);
}

const signature = this.checker.getResolvedSignature(node);
let parameters = this.transpileArguments(node.arguments, signature);

const signatureDeclaration = signature.getDeclaration();
if (!signatureDeclaration
|| tsHelper.getDeclarationContextType(signatureDeclaration, this.checker) !== ContextType.Void) {
// Pass left-side as context

// Inject context parameter
if (node.arguments.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a little weird but I do understand it prevents some duplication. Maybe if you just improve the comment a little bit on the logic for this if statement, that would already help.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alrighty

parameters = "____TS_self, " + parameters;
} else {
parameters = "____TS_self";
}

const context = this.transpileExpression(node.expression.expression);
if (tsHelper.isExpressionWithEvaluationEffect(node.expression.expression)) {
// Cache left-side if it has effects
const argument = this.transpileExpression(node.expression.argumentExpression);
if (tsHelper.isExpressionStatement(node)) {
// Statement version
return `do local ____TS_self = ${context}; ____TS_self[${argument}](${parameters}); end`;
} else {
// Expression version
return `(function() local ____TS_self = ${context}; `
+ `return ____TS_self[${argument}](${parameters}); end)()`;
}
} else {
return `${this.transpileExpression(node.expression)}(${context}${parameters})`;
}
} else {
// No context
return `${this.transpileExpression(node.expression)}(${parameters})`;
}
}

public transpileStringCallExpression(node: ts.CallExpression): string {
const expression = node.expression as ts.PropertyAccessExpression;
const params = this.transpileArguments(node.arguments);
Expand Down
38 changes: 38 additions & 0 deletions test/unit/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,42 @@ export class FunctionTests {

Expect(result).toBe(s1 === s2);
}

@Test("Element access call")
public elementAccessCall(): void {
const code = `class C {
method(s: string) { return s; }
}
const c = new C();
return c['method']("foo");
`;
const result = util.transpileAndExecute(code);
Expect(result).toBe("foo");
}

@Test("Complex element access call")
public elementAccessCallComplex(): void {
const code = `class C {
method(s: string) { return s; }
}
function getC() { return new C(); }
return getC()['method']("foo");
`;
const result = util.transpileAndExecute(code);
Expect(result).toBe("foo");
}

@Test("Complex element access call statement")
public elementAccessCallComplexStatement(): void {
const code = `let foo: string;
class C {
method(s: string) { foo = s; }
}
function getC() { return new C(); }
getC()['method']("foo");
return foo;
`;
const result = util.transpileAndExecute(code);
Expect(result).toBe("foo");
}
}
X Tutup