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
24 changes: 12 additions & 12 deletions src/transformation/builtins/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty } from "../utils/diagnostics";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { PropertyCallExpression, transformArguments, transformCallAndArguments } from "../visitors/call";
import { transformArguments, transformCallAndArguments } from "../visitors/call";
import { isStringType, isNumberType, findFirstNonOuterParent } from "../utils/typescript";
import { moveToPrecedingTemp } from "../visitors/expression-list";
import { isUnpackCall, wrapInTable } from "../utils/lua-ast";

export function transformArrayConstructorCall(
context: TransformationContext,
node: PropertyCallExpression
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const expression = node.expression;
const signature = context.checker.getResolvedSignature(node);
const params = transformArguments(context, node.arguments, signature);

const expressionName = expression.name.text;
const expressionName = calledMethod.name.text;
switch (expressionName) {
case "from":
return transformLuaLibFunction(context, LuaLibFeature.ArrayFrom, node, ...params);
Expand All @@ -25,7 +25,7 @@ export function transformArrayConstructorCall(
case "of":
return wrapInTable(...params);
default:
context.diagnostics.push(unsupportedProperty(expression.name, "Array", expressionName));
context.diagnostics.push(unsupportedProperty(calledMethod.name, "Array", expressionName));
}
}

Expand All @@ -37,7 +37,7 @@ export function transformArrayConstructorCall(
*/
function transformSingleElementArrayPush(
context: TransformationContext,
node: PropertyCallExpression,
node: ts.CallExpression,
caller: lua.Expression,
param: lua.Expression
): lua.Expression {
Expand Down Expand Up @@ -68,13 +68,13 @@ function transformSingleElementArrayPush(

export function transformArrayPrototypeCall(
context: TransformationContext,
node: PropertyCallExpression
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const expression = node.expression;
const signature = context.checker.getResolvedSignature(node);
const [caller, params] = transformCallAndArguments(context, expression.expression, node.arguments, signature);
const [caller, params] = transformCallAndArguments(context, calledMethod.expression, node.arguments, signature);

const expressionName = expression.name.text;
const expressionName = calledMethod.name.text;
switch (expressionName) {
case "concat":
return transformLuaLibFunction(context, LuaLibFeature.ArrayConcat, node, caller, ...params);
Expand Down Expand Up @@ -143,7 +143,7 @@ export function transformArrayPrototypeCall(
case "splice":
return transformLuaLibFunction(context, LuaLibFeature.ArraySplice, node, caller, ...params);
case "join":
const callerType = context.checker.getTypeAtLocation(expression.expression);
const callerType = context.checker.getTypeAtLocation(calledMethod.expression);
const elementType = context.checker.getElementTypeOfArrayType(callerType);
if (elementType && (isStringType(context, elementType) || isNumberType(context, elementType))) {
const defaultSeparatorLiteral = lua.createStringLiteral(",");
Expand All @@ -170,7 +170,7 @@ export function transformArrayPrototypeCall(
case "flatMap":
return transformLuaLibFunction(context, LuaLibFeature.ArrayFlatMap, node, caller, ...params);
default:
context.diagnostics.push(unsupportedProperty(expression.name, "array", expressionName));
context.diagnostics.push(unsupportedProperty(calledMethod.name, "array", expressionName));
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/transformation/builtins/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty } from "../utils/diagnostics";
import { PropertyCallExpression, transformArguments } from "../visitors/call";
import { transformArguments } from "../visitors/call";

const isStringFormatTemplate = (node: ts.Expression) => ts.isStringLiteral(node) && node.text.includes("%");

export function transformConsoleCall(
context: TransformationContext,
expression: PropertyCallExpression
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const method = expression.expression;
const methodName = method.name.text;
const signature = context.checker.getResolvedSignature(expression);
const parameters = transformArguments(context, expression.arguments, signature);
const methodName = calledMethod.name.text;
const signature = context.checker.getResolvedSignature(node);
const parameters = transformArguments(context, node.arguments, signature);

switch (methodName) {
case "error":
case "info":
case "log":
case "warn":
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) {
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) {
// print(string.format([arguments]))
const stringFormatCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")),
Expand All @@ -31,7 +31,7 @@ export function transformConsoleCall(
// print([arguments])
return lua.createCallExpression(lua.createIdentifier("print"), parameters);
case "assert":
if (expression.arguments.length > 1 && isStringFormatTemplate(expression.arguments[1])) {
if (node.arguments.length > 1 && isStringFormatTemplate(node.arguments[1])) {
// assert([condition], string.format([arguments]))
const stringFormatCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")),
Expand All @@ -42,7 +42,7 @@ export function transformConsoleCall(
// assert()
return lua.createCallExpression(lua.createIdentifier("assert"), parameters);
case "trace":
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) {
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) {
// print(debug.traceback(string.format([arguments])))
const stringFormatCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")),
Expand All @@ -61,6 +61,6 @@ export function transformConsoleCall(
);
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
default:
context.diagnostics.push(unsupportedProperty(method.name, "console", methodName));
context.diagnostics.push(unsupportedProperty(calledMethod.name, "console", methodName));
}
}
14 changes: 7 additions & 7 deletions src/transformation/builtins/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import { unsupportedForTarget, unsupportedProperty, unsupportedSelfFunctionConve
import { ContextType, getFunctionContextType } from "../utils/function-context";
import { createUnpackCall } from "../utils/lua-ast";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { PropertyCallExpression, transformCallAndArguments } from "../visitors/call";
import { transformCallAndArguments } from "../visitors/call";

export function transformFunctionPrototypeCall(
context: TransformationContext,
node: PropertyCallExpression
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.CallExpression | undefined {
const expression = node.expression;
const callerType = context.checker.getTypeAtLocation(expression.expression);
const callerType = context.checker.getTypeAtLocation(calledMethod.expression);
if (getFunctionContextType(context, callerType) === ContextType.Void) {
context.diagnostics.push(unsupportedSelfFunctionConversion(node));
}

const signature = context.checker.getResolvedSignature(node);
const [caller, params] = transformCallAndArguments(context, expression.expression, node.arguments, signature);
const expressionName = expression.name.text;
const [caller, params] = transformCallAndArguments(context, calledMethod.expression, node.arguments, signature);
const expressionName = calledMethod.name.text;
switch (expressionName) {
case "apply":
const nonContextArgs = params.length > 1 ? [createUnpackCall(context, params[1], node.arguments[1])] : [];
Expand All @@ -30,7 +30,7 @@ export function transformFunctionPrototypeCall(
case "call":
return lua.createCallExpression(caller, params, node);
case "toString":
context.diagnostics.push(unsupportedProperty(expression.name, "function", expressionName));
context.diagnostics.push(unsupportedProperty(calledMethod.name, "function", expressionName));
}
}

Expand Down
46 changes: 22 additions & 24 deletions src/transformation/builtins/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { assume } from "../../utils";
import { TransformationContext } from "../context";
import { createNaN } from "../utils/lua-ast";
import { importLuaLibFeature, LuaLibFeature } from "../utils/lualib";
Expand All @@ -14,7 +13,7 @@ import {
isStandardLibraryType,
isStringType,
} from "../utils/typescript";
import { PropertyCallExpression } from "../visitors/call";
import { getCalledExpression } from "../visitors/call";
import { transformArrayConstructorCall, transformArrayProperty, transformArrayPrototypeCall } from "./array";
import { transformConsoleCall } from "./console";
import { transformFunctionPrototypeCall, transformFunctionProperty } from "./function";
Expand Down Expand Up @@ -74,79 +73,78 @@ export function transformBuiltinCallExpression(
}
}

const expression = ts.getOriginalNode(node.expression);
if (!ts.isPropertyAccessExpression(expression)) {
const calledMethod = ts.getOriginalNode(getCalledExpression(node));
if (!ts.isPropertyAccessExpression(calledMethod)) {
return;
}

const isOptionalAccess = expression.questionDotToken;
assume<PropertyCallExpression>(node);
const isOptionalAccess = calledMethod.questionDotToken;
// If the function being called is of type owner.func, get the type of owner
const ownerType = context.checker.getTypeAtLocation(expression.expression);
const ownerType = context.checker.getTypeAtLocation(calledMethod.expression);

if (isStandardLibraryType(context, ownerType, undefined)) {
const symbol = ownerType.getSymbol();
switch (symbol?.name) {
case "ArrayConstructor":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformArrayConstructorCall(context, node);
return transformArrayConstructorCall(context, node, calledMethod);
case "Console":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformConsoleCall(context, node);
return transformConsoleCall(context, node, calledMethod);
case "Math":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformMathCall(context, node);
return transformMathCall(context, node, calledMethod);
case "StringConstructor":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformStringConstructorCall(context, node);
return transformStringConstructorCall(context, node, calledMethod);
case "ObjectConstructor":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformObjectConstructorCall(context, node);
return transformObjectConstructorCall(context, node, calledMethod);
case "SymbolConstructor":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformSymbolConstructorCall(context, node);
return transformSymbolConstructorCall(context, node, calledMethod);
case "NumberConstructor":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformNumberConstructorCall(context, node);
return transformNumberConstructorCall(context, node, calledMethod);
case "PromiseConstructor":
if (isOptionalCall || isOptionalAccess) return unsupportedOptionalCall();
return transformPromiseConstructorCall(context, node);
return transformPromiseConstructorCall(context, node, calledMethod);
}
}

const isStringFunction =
isStringType(context, ownerType) ||
(expression.questionDotToken && isNullableType(context, ownerType, isStringType));
(calledMethod.questionDotToken && isNullableType(context, ownerType, isStringType));
if (isStringFunction && hasStandardLibrarySignature(context, node)) {
if (isOptionalCall) return unsupportedOptionalCall();
return transformStringPrototypeCall(context, node);
return transformStringPrototypeCall(context, node, calledMethod);
}

const isNumberFunction =
isNumberType(context, ownerType) ||
(expression.questionDotToken && isNullableType(context, ownerType, isNumberType));
(calledMethod.questionDotToken && isNullableType(context, ownerType, isNumberType));
if (isNumberFunction && hasStandardLibrarySignature(context, node)) {
if (isOptionalCall) return unsupportedOptionalCall();
return transformNumberPrototypeCall(context, node);
return transformNumberPrototypeCall(context, node, calledMethod);
}

const isArrayFunction =
isArrayType(context, ownerType) ||
(expression.questionDotToken && isNullableType(context, ownerType, isArrayType));
(calledMethod.questionDotToken && isNullableType(context, ownerType, isArrayType));
if (isArrayFunction && hasStandardLibrarySignature(context, node)) {
if (isOptionalCall) return unsupportedOptionalCall();
return transformArrayPrototypeCall(context, node);
return transformArrayPrototypeCall(context, node, calledMethod);
}

const isFunctionFunction =
isFunctionType(ownerType) ||
(expression.questionDotToken && isNullableType(context, ownerType, (_, t) => isFunctionType(t)));
(calledMethod.questionDotToken && isNullableType(context, ownerType, (_, t) => isFunctionType(t)));
if (isFunctionFunction && hasStandardLibrarySignature(context, node)) {
if (isOptionalCall) return unsupportedOptionalCall();
return transformFunctionPrototypeCall(context, node);
return transformFunctionPrototypeCall(context, node, calledMethod);
}

const objectResult = transformObjectPrototypeCall(context, node, expression);
const objectResult = transformObjectPrototypeCall(context, node, calledMethod);
if (objectResult) {
if (isOptionalCall) return unsupportedOptionalCall();
return objectResult;
Expand Down
10 changes: 5 additions & 5 deletions src/transformation/builtins/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty } from "../utils/diagnostics";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { PropertyCallExpression, transformArguments } from "../visitors/call";
import { transformArguments } from "../visitors/call";

export function transformMathProperty(
context: TransformationContext,
Expand Down Expand Up @@ -33,14 +33,14 @@ export function transformMathProperty(

export function transformMathCall(
context: TransformationContext,
node: PropertyCallExpression
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const expression = node.expression;
const signature = context.checker.getResolvedSignature(node);
const params = transformArguments(context, node.arguments, signature);
const math = lua.createIdentifier("math");

const expressionName = expression.name.text;
const expressionName = calledMethod.name.text;
switch (expressionName) {
// Lua 5.3: math.atan(y, x)
// Otherwise: math.atan2(y, x)
Expand Down Expand Up @@ -107,6 +107,6 @@ export function transformMathCall(
}

default:
context.diagnostics.push(unsupportedProperty(expression.name, "Math", expressionName));
context.diagnostics.push(unsupportedProperty(calledMethod.name, "Math", expressionName));
}
}
27 changes: 14 additions & 13 deletions src/transformation/builtins/number.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import ts = require("typescript");
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty } from "../utils/diagnostics";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { PropertyCallExpression, transformArguments } from "../visitors/call";
import { transformArguments } from "../visitors/call";

export function transformNumberPrototypeCall(
context: TransformationContext,
node: PropertyCallExpression
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const expression = node.expression;
const signature = context.checker.getResolvedSignature(node);
const params = transformArguments(context, node.arguments, signature);
const caller = context.transformExpression(expression.expression);
const caller = context.transformExpression(calledMethod.expression);

const expressionName = expression.name.text;
const expressionName = calledMethod.name.text;
switch (expressionName) {
case "toString":
return params.length === 0
? lua.createCallExpression(lua.createIdentifier("tostring"), [caller], node)
: transformLuaLibFunction(context, LuaLibFeature.NumberToString, node, caller, ...params);
default:
context.diagnostics.push(unsupportedProperty(expression.name, "number", expressionName));
context.diagnostics.push(unsupportedProperty(calledMethod.name, "number", expressionName));
}
}

export function transformNumberConstructorCall(
context: TransformationContext,
expression: PropertyCallExpression
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.CallExpression | undefined {
const method = expression.expression;
const parameters = transformArguments(context, expression.arguments);
const methodName = method.name.text;
const parameters = transformArguments(context, node.arguments);
const methodName = calledMethod.name.text;
switch (methodName) {
case "isNaN":
return transformLuaLibFunction(context, LuaLibFeature.NumberIsNaN, expression, ...parameters);
return transformLuaLibFunction(context, LuaLibFeature.NumberIsNaN, node, ...parameters);
case "isFinite":
return transformLuaLibFunction(context, LuaLibFeature.NumberIsFinite, expression, ...parameters);
return transformLuaLibFunction(context, LuaLibFeature.NumberIsFinite, node, ...parameters);
default:
context.diagnostics.push(unsupportedProperty(method.name, "Number", methodName));
context.diagnostics.push(unsupportedProperty(calledMethod.name, "Number", methodName));
}
}
Loading