typescript-to-lua 1.1.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/dist/LuaAST.d.ts +1 -0
- package/dist/LuaAST.js +10 -2
- package/dist/LuaLib.d.ts +4 -3
- package/dist/LuaLib.js +6 -5
- package/dist/LuaPrinter.d.ts +11 -4
- package/dist/LuaPrinter.js +58 -15
- package/dist/lualib/ArrayFlat.lua +1 -1
- package/dist/lualib/ArrayFlatMap.lua +1 -1
- package/dist/lualib/ArrayIsArray.lua +1 -1
- package/dist/lualib/ArrayReduce.lua +7 -1
- package/dist/lualib/ArrayReduceRight.lua +7 -1
- package/dist/lualib/ArraySetLength.lua +1 -1
- package/dist/lualib/ArraySplice.lua +5 -5
- package/dist/lualib/Await.lua +21 -13
- package/dist/lualib/CloneDescriptor.lua +7 -7
- package/dist/lualib/Decorate.lua +1 -1
- package/dist/lualib/DelegatedYield.lua +1 -3
- package/dist/lualib/Delete.lua +6 -6
- package/dist/lualib/Error.lua +47 -44
- package/dist/lualib/Generator.lua +1 -5
- package/dist/lualib/InstanceOf.lua +1 -1
- package/dist/lualib/InstanceOfObject.lua +1 -1
- package/dist/lualib/Map.lua +130 -134
- package/dist/lualib/MathSign.lua +8 -0
- package/dist/lualib/Number.lua +1 -1
- package/dist/lualib/NumberIsFinite.lua +1 -1
- package/dist/lualib/NumberToString.lua +3 -5
- package/dist/lualib/ObjectDefineProperty.lua +40 -8
- package/dist/lualib/ParseFloat.lua +9 -5
- package/dist/lualib/ParseInt.lua +15 -3
- package/dist/lualib/Promise.lua +28 -24
- package/dist/lualib/Set.lua +119 -123
- package/dist/lualib/SetDescriptor.lua +7 -1
- package/dist/lualib/SourceMapTraceBack.lua +1 -1
- package/dist/lualib/SparseArrayNew.lua +5 -0
- package/dist/lualib/SparseArrayPush.lua +9 -0
- package/dist/lualib/SparseArraySpread.lua +4 -0
- package/dist/lualib/StringAccess.lua +1 -1
- package/dist/lualib/StringCharCodeAt.lua +1 -1
- package/dist/lualib/StringEndsWith.lua +2 -2
- package/dist/lualib/StringPadEnd.lua +2 -2
- package/dist/lualib/StringPadStart.lua +2 -2
- package/dist/lualib/StringReplace.lua +7 -1
- package/dist/lualib/StringSlice.lua +2 -2
- package/dist/lualib/StringSplit.lua +3 -3
- package/dist/lualib/StringStartsWith.lua +1 -1
- package/dist/lualib/StringSubstr.lua +1 -1
- package/dist/lualib/StringSubstring.lua +2 -2
- package/dist/lualib/Symbol.lua +3 -5
- package/dist/lualib/WeakMap.lua +40 -42
- package/dist/lualib/WeakSet.lua +36 -38
- package/dist/lualib/lualib_bundle.lua +804 -735
- package/dist/transformation/builtins/array.js +1 -2
- package/dist/transformation/builtins/function.js +6 -4
- package/dist/transformation/builtins/index.d.ts +1 -1
- package/dist/transformation/builtins/index.js +41 -6
- package/dist/transformation/builtins/math.js +7 -1
- package/dist/transformation/builtins/object.d.ts +2 -1
- package/dist/transformation/builtins/object.js +1 -2
- package/dist/transformation/builtins/string.js +1 -2
- package/dist/transformation/context/context.d.ts +12 -0
- package/dist/transformation/context/context.js +102 -3
- package/dist/transformation/utils/diagnostics.d.ts +9 -6
- package/dist/transformation/utils/diagnostics.js +4 -3
- package/dist/transformation/utils/language-extensions.d.ts +2 -0
- package/dist/transformation/utils/language-extensions.js +7 -1
- package/dist/transformation/utils/lua-ast.d.ts +0 -1
- package/dist/transformation/utils/lua-ast.js +14 -23
- package/dist/transformation/utils/preceding-statements.d.ts +3 -0
- package/dist/transformation/utils/preceding-statements.js +11 -0
- package/dist/transformation/utils/safe-names.d.ts +4 -2
- package/dist/transformation/utils/safe-names.js +11 -5
- package/dist/transformation/utils/scope.d.ts +3 -1
- package/dist/transformation/utils/scope.js +9 -1
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +16 -1
- package/dist/transformation/utils/typescript/types.d.ts +3 -1
- package/dist/transformation/utils/typescript/types.js +40 -13
- package/dist/transformation/visitors/access.d.ts +3 -0
- package/dist/transformation/visitors/access.js +63 -36
- package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
- package/dist/transformation/visitors/binary-expression/assignments.js +66 -57
- package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
- package/dist/transformation/visitors/binary-expression/compound.js +66 -68
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +54 -25
- package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
- package/dist/transformation/visitors/binary-expression/index.js +65 -23
- package/dist/transformation/visitors/call.d.ts +3 -2
- package/dist/transformation/visitors/call.js +95 -141
- package/dist/transformation/visitors/class/index.js +5 -8
- package/dist/transformation/visitors/class/members/constructor.js +3 -1
- package/dist/transformation/visitors/class/new.js +4 -6
- package/dist/transformation/visitors/conditional.js +30 -37
- package/dist/transformation/visitors/delete.js +4 -0
- package/dist/transformation/visitors/errors.js +11 -9
- package/dist/transformation/visitors/expression-list.d.ts +7 -0
- package/dist/transformation/visitors/expression-list.js +120 -0
- package/dist/transformation/visitors/expression-statement.d.ts +3 -1
- package/dist/transformation/visitors/expression-statement.js +21 -19
- package/dist/transformation/visitors/function.d.ts +2 -0
- package/dist/transformation/visitors/function.js +63 -13
- package/dist/transformation/visitors/identifier.js +5 -0
- package/dist/transformation/visitors/language-extensions/index.d.ts +4 -0
- package/dist/transformation/visitors/language-extensions/index.js +17 -0
- package/dist/transformation/visitors/language-extensions/operators.d.ts +1 -1
- package/dist/transformation/visitors/language-extensions/operators.js +7 -2
- package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +5 -0
- package/dist/transformation/visitors/language-extensions/pairsIterable.js +53 -0
- package/dist/transformation/visitors/language-extensions/table.d.ts +1 -8
- package/dist/transformation/visitors/language-extensions/table.js +55 -53
- package/dist/transformation/visitors/literal.js +69 -16
- package/dist/transformation/visitors/loops/do-while.js +34 -8
- package/dist/transformation/visitors/loops/for-of.js +4 -0
- package/dist/transformation/visitors/loops/for.js +25 -5
- package/dist/transformation/visitors/loops/utils.d.ts +1 -0
- package/dist/transformation/visitors/loops/utils.js +19 -3
- package/dist/transformation/visitors/modules/export.js +1 -1
- package/dist/transformation/visitors/namespace.js +1 -1
- package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
- package/dist/transformation/visitors/optional-chaining.js +172 -0
- package/dist/transformation/visitors/sourceFile.js +4 -1
- package/dist/transformation/visitors/spread.js +14 -7
- package/dist/transformation/visitors/switch.js +29 -9
- package/dist/transformation/visitors/template.js +5 -2
- package/dist/transformation/visitors/typeof.js +3 -1
- package/dist/transformation/visitors/variable-declaration.js +37 -8
- package/dist/transformation/visitors/void.d.ts +1 -4
- package/dist/transformation/visitors/void.js +7 -12
- package/dist/transpilation/transformers.js +4 -1
- package/dist/transpilation/utils.js +2 -1
- package/language-extensions/index.d.ts +11 -1
- package/package.json +5 -5
- package/dist/lualib/OptionalChainAccess.lua +0 -6
- package/dist/lualib/OptionalFunctionCall.lua +0 -6
- package/dist/lualib/OptionalMethodCall.lua +0 -11
- package/dist/transformation/utils/transform.d.ts +0 -8
- package/dist/transformation/utils/transform.js +0 -14
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformOrderedExpressions = exports.transformExpressionList = exports.moveToPrecedingTemp = exports.shouldMoveToTemp = void 0;
|
|
4
|
+
const assert = require("assert");
|
|
5
|
+
const ts = require("typescript");
|
|
6
|
+
const lua = require("../../LuaAST");
|
|
7
|
+
const context_1 = require("../context");
|
|
8
|
+
const lualib_1 = require("../utils/lualib");
|
|
9
|
+
const preceding_statements_1 = require("../utils/preceding-statements");
|
|
10
|
+
const typescript_1 = require("../utils/typescript");
|
|
11
|
+
const optional_chaining_1 = require("./optional-chaining");
|
|
12
|
+
function shouldMoveToTemp(context, expression, tsOriginal) {
|
|
13
|
+
return (!lua.isLiteral(expression) &&
|
|
14
|
+
!(lua.isIdentifier(expression) && expression.symbolId === context_1.tempSymbolId) && // Treat generated temps as consts
|
|
15
|
+
!(tsOriginal && ((0, typescript_1.isConstIdentifier)(context, tsOriginal) || (0, optional_chaining_1.isOptionalContinuation)(tsOriginal))));
|
|
16
|
+
}
|
|
17
|
+
exports.shouldMoveToTemp = shouldMoveToTemp;
|
|
18
|
+
// Cache an expression in a preceding statement and return the temp identifier
|
|
19
|
+
function moveToPrecedingTemp(context, expression, tsOriginal) {
|
|
20
|
+
if (!shouldMoveToTemp(context, expression, tsOriginal)) {
|
|
21
|
+
return expression;
|
|
22
|
+
}
|
|
23
|
+
const tempIdentifier = context.createTempNameForLuaExpression(expression);
|
|
24
|
+
const tempDeclaration = lua.createVariableDeclarationStatement(tempIdentifier, expression, tsOriginal);
|
|
25
|
+
context.addPrecedingStatements(tempDeclaration);
|
|
26
|
+
return lua.cloneIdentifier(tempIdentifier, tsOriginal);
|
|
27
|
+
}
|
|
28
|
+
exports.moveToPrecedingTemp = moveToPrecedingTemp;
|
|
29
|
+
function transformExpressions(context, expressions) {
|
|
30
|
+
const precedingStatements = [];
|
|
31
|
+
const transformedExpressions = [];
|
|
32
|
+
let lastPrecedingStatementsIndex = -1;
|
|
33
|
+
for (let i = 0; i < expressions.length; ++i) {
|
|
34
|
+
const [expressionPrecedingStatements, expression] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expressions[i]));
|
|
35
|
+
transformedExpressions.push(expression);
|
|
36
|
+
if (expressionPrecedingStatements.length > 0) {
|
|
37
|
+
lastPrecedingStatementsIndex = i;
|
|
38
|
+
}
|
|
39
|
+
precedingStatements.push(expressionPrecedingStatements);
|
|
40
|
+
}
|
|
41
|
+
return { transformedExpressions, precedingStatements, lastPrecedingStatementsIndex };
|
|
42
|
+
}
|
|
43
|
+
function transformExpressionsUsingTemps(context, expressions, transformedExpressions, precedingStatements, lastPrecedingStatementsIndex) {
|
|
44
|
+
for (let i = 0; i < transformedExpressions.length; ++i) {
|
|
45
|
+
context.addPrecedingStatements(precedingStatements[i]);
|
|
46
|
+
if (i < lastPrecedingStatementsIndex) {
|
|
47
|
+
transformedExpressions[i] = moveToPrecedingTemp(context, transformedExpressions[i], expressions[i]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return transformedExpressions;
|
|
51
|
+
}
|
|
52
|
+
function pushToSparseArray(context, arrayIdentifier, expressions) {
|
|
53
|
+
if (!arrayIdentifier) {
|
|
54
|
+
arrayIdentifier = lua.createIdentifier(context.createTempName("array"));
|
|
55
|
+
const libCall = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.SparseArrayNew, undefined, ...expressions);
|
|
56
|
+
const declaration = lua.createVariableDeclarationStatement(arrayIdentifier, libCall);
|
|
57
|
+
context.addPrecedingStatements(declaration);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const libCall = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.SparseArrayPush, undefined, arrayIdentifier, ...expressions);
|
|
61
|
+
context.addPrecedingStatements(lua.createExpressionStatement(libCall));
|
|
62
|
+
}
|
|
63
|
+
return arrayIdentifier;
|
|
64
|
+
}
|
|
65
|
+
function transformExpressionsUsingSparseArray(context, expressions, transformedExpressions, precedingStatements) {
|
|
66
|
+
let arrayIdentifier;
|
|
67
|
+
let expressionBatch = [];
|
|
68
|
+
for (let i = 0; i < expressions.length; ++i) {
|
|
69
|
+
// Expressions with preceding statements should always be at the start of a batch
|
|
70
|
+
if (precedingStatements[i].length > 0 && expressionBatch.length > 0) {
|
|
71
|
+
arrayIdentifier = pushToSparseArray(context, arrayIdentifier, expressionBatch);
|
|
72
|
+
expressionBatch = [];
|
|
73
|
+
}
|
|
74
|
+
context.addPrecedingStatements(precedingStatements[i]);
|
|
75
|
+
expressionBatch.push(transformedExpressions[i]);
|
|
76
|
+
// Spread expressions should always be at the end of a batch
|
|
77
|
+
if (ts.isSpreadElement(expressions[i])) {
|
|
78
|
+
arrayIdentifier = pushToSparseArray(context, arrayIdentifier, expressionBatch);
|
|
79
|
+
expressionBatch = [];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (expressionBatch.length > 0) {
|
|
83
|
+
arrayIdentifier = pushToSparseArray(context, arrayIdentifier, expressionBatch);
|
|
84
|
+
}
|
|
85
|
+
assert(arrayIdentifier);
|
|
86
|
+
return [(0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.SparseArraySpread, undefined, arrayIdentifier)];
|
|
87
|
+
}
|
|
88
|
+
function countNeededTemps(context, expressions, transformedExpressions, lastPrecedingStatementsIndex) {
|
|
89
|
+
if (lastPrecedingStatementsIndex < 0) {
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
return transformedExpressions
|
|
93
|
+
.slice(0, lastPrecedingStatementsIndex)
|
|
94
|
+
.filter((e, i) => shouldMoveToTemp(context, e, expressions[i])).length;
|
|
95
|
+
}
|
|
96
|
+
// Transforms a list of expressions while flattening spreads and maintaining execution order
|
|
97
|
+
function transformExpressionList(context, expressions) {
|
|
98
|
+
const { transformedExpressions, precedingStatements, lastPrecedingStatementsIndex } = transformExpressions(context, expressions);
|
|
99
|
+
// If more than this number of temps are required to preserve execution order, we'll fall back to using the
|
|
100
|
+
// sparse array lib functions instead to prevent excessive locals.
|
|
101
|
+
const maxTemps = 2;
|
|
102
|
+
// Use sparse array lib if there are spreads before the last expression
|
|
103
|
+
// or if too many temps are needed to preserve order
|
|
104
|
+
const lastSpread = expressions.findIndex(e => ts.isSpreadElement(e));
|
|
105
|
+
if ((lastSpread >= 0 && lastSpread < expressions.length - 1) ||
|
|
106
|
+
countNeededTemps(context, expressions, transformedExpressions, lastPrecedingStatementsIndex) > maxTemps) {
|
|
107
|
+
return transformExpressionsUsingSparseArray(context, expressions, transformedExpressions, precedingStatements);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
return transformExpressionsUsingTemps(context, expressions, transformedExpressions, precedingStatements, lastPrecedingStatementsIndex);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.transformExpressionList = transformExpressionList;
|
|
114
|
+
// Transforms a series of expressions while maintaining execution order
|
|
115
|
+
function transformOrderedExpressions(context, expressions) {
|
|
116
|
+
const { transformedExpressions, precedingStatements, lastPrecedingStatementsIndex } = transformExpressions(context, expressions);
|
|
117
|
+
return transformExpressionsUsingTemps(context, expressions, transformedExpressions, precedingStatements, lastPrecedingStatementsIndex);
|
|
118
|
+
}
|
|
119
|
+
exports.transformOrderedExpressions = transformOrderedExpressions;
|
|
120
|
+
//# sourceMappingURL=expression-list.js.map
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
|
-
import
|
|
2
|
+
import * as lua from "../../LuaAST";
|
|
3
|
+
import { FunctionVisitor, TransformationContext } from "../context";
|
|
3
4
|
export declare const transformExpressionStatement: FunctionVisitor<ts.ExpressionStatement>;
|
|
5
|
+
export declare function transformExpressionToStatement(context: TransformationContext, expression: ts.Expression): lua.Statement | undefined;
|
|
@@ -1,23 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformExpressionStatement = void 0;
|
|
4
|
-
const ts = require("typescript");
|
|
3
|
+
exports.transformExpressionToStatement = exports.transformExpressionStatement = void 0;
|
|
5
4
|
const lua = require("../../LuaAST");
|
|
5
|
+
const context_1 = require("../context");
|
|
6
6
|
const binary_expression_1 = require("./binary-expression");
|
|
7
|
-
const table_1 = require("./language-extensions/table");
|
|
8
7
|
const unary_expression_1 = require("./unary-expression");
|
|
9
|
-
const void_1 = require("./void");
|
|
10
8
|
const transformExpressionStatement = (node, context) => {
|
|
11
|
-
const expression = node.expression;
|
|
12
|
-
if (ts.isCallExpression(expression) && (0, table_1.isTableDeleteCall)(context, expression)) {
|
|
13
|
-
return (0, table_1.transformTableDeleteExpression)(context, expression);
|
|
14
|
-
}
|
|
15
|
-
if (ts.isCallExpression(expression) && (0, table_1.isTableSetCall)(context, expression)) {
|
|
16
|
-
return (0, table_1.transformTableSetExpression)(context, expression);
|
|
17
|
-
}
|
|
18
|
-
if (ts.isVoidExpression(expression)) {
|
|
19
|
-
return (0, void_1.transformVoidExpressionStatement)(expression, context);
|
|
20
|
-
}
|
|
21
9
|
const unaryExpressionResult = (0, unary_expression_1.transformUnaryExpressionStatement)(context, node);
|
|
22
10
|
if (unaryExpressionResult) {
|
|
23
11
|
return unaryExpressionResult;
|
|
@@ -26,11 +14,25 @@ const transformExpressionStatement = (node, context) => {
|
|
|
26
14
|
if (binaryExpressionResult) {
|
|
27
15
|
return binaryExpressionResult;
|
|
28
16
|
}
|
|
29
|
-
|
|
30
|
-
return lua.isCallExpression(result) || lua.isMethodCallExpression(result)
|
|
31
|
-
? lua.createExpressionStatement(result)
|
|
32
|
-
: // Assign expression statements to dummy to make sure they're legal Lua
|
|
33
|
-
lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), result);
|
|
17
|
+
return transformExpressionToStatement(context, node.expression);
|
|
34
18
|
};
|
|
35
19
|
exports.transformExpressionStatement = transformExpressionStatement;
|
|
20
|
+
function transformExpressionToStatement(context, expression) {
|
|
21
|
+
const result = context.transformExpression(expression);
|
|
22
|
+
const isTempVariable = lua.isIdentifier(result) && result.symbolId === context_1.tempSymbolId;
|
|
23
|
+
if (isTempVariable) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
// "synthetic": no side effects and no original source
|
|
27
|
+
const isSyntheticExpression = (lua.isIdentifier(result) || lua.isLiteral(result)) && result.line === undefined;
|
|
28
|
+
if (isSyntheticExpression) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
if (lua.isCallExpression(result) || lua.isMethodCallExpression(result)) {
|
|
32
|
+
return lua.createExpressionStatement(result);
|
|
33
|
+
}
|
|
34
|
+
// Assign expression statements to dummy to make sure they're legal Lua
|
|
35
|
+
return lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), result);
|
|
36
|
+
}
|
|
37
|
+
exports.transformExpressionToStatement = transformExpressionToStatement;
|
|
36
38
|
//# sourceMappingURL=expression-statement.js.map
|
|
@@ -2,6 +2,8 @@ import * as ts from "typescript";
|
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { FunctionVisitor, TransformationContext } from "../context";
|
|
4
4
|
import { Scope } from "../utils/scope";
|
|
5
|
+
export declare function createCallableTable(functionExpression: lua.Expression): lua.Expression;
|
|
6
|
+
export declare function isFunctionTypeWithProperties(functionType: ts.Type): boolean;
|
|
5
7
|
export declare function transformFunctionBodyContent(context: TransformationContext, body: ts.ConciseBody): lua.Statement[];
|
|
6
8
|
export declare function transformFunctionBodyHeader(context: TransformationContext, bodyScope: Scope, parameters: ts.NodeArray<ts.ParameterDeclaration>, spreadIdentifier?: lua.Identifier): lua.Statement[];
|
|
7
9
|
export declare function transformFunctionBody(context: TransformationContext, parameters: ts.NodeArray<ts.ParameterDeclaration>, body: ts.ConciseBody, spreadIdentifier?: lua.Identifier, node?: ts.FunctionLikeDeclaration): [lua.Statement[], Scope];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformYieldExpression = exports.transformFunctionDeclaration = exports.transformFunctionLikeDeclaration = exports.transformFunctionToExpression = exports.transformParameters = exports.transformFunctionBody = exports.transformFunctionBodyHeader = exports.transformFunctionBodyContent = void 0;
|
|
3
|
+
exports.transformYieldExpression = exports.transformFunctionDeclaration = exports.transformFunctionLikeDeclaration = exports.transformFunctionToExpression = exports.transformParameters = exports.transformFunctionBody = exports.transformFunctionBodyHeader = exports.transformFunctionBodyContent = exports.isFunctionTypeWithProperties = exports.createCallableTable = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
@@ -8,9 +8,12 @@ const annotations_1 = require("../utils/annotations");
|
|
|
8
8
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
9
9
|
const export_1 = require("../utils/export");
|
|
10
10
|
const function_context_1 = require("../utils/function-context");
|
|
11
|
+
const language_extensions_1 = require("../utils/language-extensions");
|
|
11
12
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
12
13
|
const lualib_1 = require("../utils/lualib");
|
|
14
|
+
const preceding_statements_1 = require("../utils/preceding-statements");
|
|
13
15
|
const scope_1 = require("../utils/scope");
|
|
16
|
+
const typescript_1 = require("../utils/typescript");
|
|
14
17
|
const async_await_1 = require("./async-await");
|
|
15
18
|
const identifier_1 = require("./identifier");
|
|
16
19
|
const return_1 = require("./return");
|
|
@@ -32,10 +35,43 @@ function isRestParameterReferenced(identifier, scope) {
|
|
|
32
35
|
const references = scope.referencedSymbols.get(identifier.symbolId);
|
|
33
36
|
return references !== undefined && references.length > 0;
|
|
34
37
|
}
|
|
38
|
+
function createCallableTable(functionExpression) {
|
|
39
|
+
var _a;
|
|
40
|
+
// __call metamethod receives the table as the first argument, so we need to add a dummy parameter
|
|
41
|
+
if (lua.isFunctionExpression(functionExpression)) {
|
|
42
|
+
(_a = functionExpression.params) === null || _a === void 0 ? void 0 : _a.unshift(lua.createAnonymousIdentifier());
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
// functionExpression may have been replaced (lib functions, etc...),
|
|
46
|
+
// so we create a forwarding function to eat the extra argument
|
|
47
|
+
functionExpression = lua.createFunctionExpression(lua.createBlock([
|
|
48
|
+
lua.createReturnStatement([lua.createCallExpression(functionExpression, [lua.createDotsLiteral()])]),
|
|
49
|
+
]), [lua.createAnonymousIdentifier()], lua.createDotsLiteral(), lua.FunctionExpressionFlags.Inline);
|
|
50
|
+
}
|
|
51
|
+
return lua.createCallExpression(lua.createIdentifier("setmetatable"), [
|
|
52
|
+
lua.createTableExpression(),
|
|
53
|
+
lua.createTableExpression([
|
|
54
|
+
lua.createTableFieldExpression(functionExpression, lua.createStringLiteral("__call")),
|
|
55
|
+
]),
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
exports.createCallableTable = createCallableTable;
|
|
59
|
+
function isFunctionTypeWithProperties(functionType) {
|
|
60
|
+
if (functionType.isUnion()) {
|
|
61
|
+
return functionType.types.some(isFunctionTypeWithProperties);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return ((0, typescript_1.isFunctionType)(functionType) &&
|
|
65
|
+
functionType.getProperties().length > 0 &&
|
|
66
|
+
(0, language_extensions_1.getExtensionKinds)(functionType).length === 0 // ignore TSTL extension functions like $range
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.isFunctionTypeWithProperties = isFunctionTypeWithProperties;
|
|
35
71
|
function transformFunctionBodyContent(context, body) {
|
|
36
72
|
if (!ts.isBlock(body)) {
|
|
37
|
-
const returnStatement = (0, return_1.transformExpressionBodyToReturnStatement)(context, body);
|
|
38
|
-
return [returnStatement];
|
|
73
|
+
const [precedingStatements, returnStatement] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => (0, return_1.transformExpressionBodyToReturnStatement)(context, body));
|
|
74
|
+
return [...precedingStatements, returnStatement];
|
|
39
75
|
}
|
|
40
76
|
const bodyStatements = (0, scope_1.performHoisting)(context, context.transformStatements(body.statements));
|
|
41
77
|
return bodyStatements;
|
|
@@ -54,7 +90,9 @@ function transformFunctionBodyHeader(context, bodyScope, parameters, spreadIdent
|
|
|
54
90
|
headerStatements.push(transformParameterDefaultValueDeclaration(context, identifier, declaration.initializer));
|
|
55
91
|
}
|
|
56
92
|
// Binding pattern
|
|
57
|
-
|
|
93
|
+
const name = declaration.name;
|
|
94
|
+
const [precedingStatements, bindings] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => (0, variable_declaration_1.transformBindingPattern)(context, name, identifier));
|
|
95
|
+
bindingPatternDeclarations.push(...precedingStatements, ...bindings);
|
|
58
96
|
}
|
|
59
97
|
else if (declaration.initializer !== undefined) {
|
|
60
98
|
// Default parameter
|
|
@@ -157,24 +195,32 @@ function transformFunctionLikeDeclaration(node, context) {
|
|
|
157
195
|
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.TupleReturn));
|
|
158
196
|
}
|
|
159
197
|
const [functionExpression, functionScope] = transformFunctionToExpression(context, node);
|
|
198
|
+
const isNamedFunctionExpression = ts.isFunctionExpression(node) && node.name;
|
|
160
199
|
// Handle named function expressions which reference themselves
|
|
161
|
-
if (
|
|
200
|
+
if (isNamedFunctionExpression && functionScope.referencedSymbols) {
|
|
162
201
|
const symbol = context.checker.getSymbolAtLocation(node.name);
|
|
163
202
|
if (symbol) {
|
|
164
203
|
// TODO: Not using symbol ids because of https://github.com/microsoft/TypeScript/issues/37131
|
|
165
204
|
const isReferenced = [...functionScope.referencedSymbols].some(([, nodes]) => nodes.some(n => { var _a; return ((_a = context.checker.getSymbolAtLocation(n)) === null || _a === void 0 ? void 0 : _a.valueDeclaration) === symbol.valueDeclaration; }));
|
|
166
|
-
// Only
|
|
205
|
+
// Only handle if the name is actually referenced inside the function
|
|
167
206
|
if (isReferenced) {
|
|
168
207
|
const nameIdentifier = (0, identifier_1.transformIdentifier)(context, node.name);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
208
|
+
if (isFunctionTypeWithProperties(context.checker.getTypeAtLocation(node))) {
|
|
209
|
+
context.addPrecedingStatements([
|
|
210
|
+
lua.createVariableDeclarationStatement(nameIdentifier),
|
|
211
|
+
lua.createAssignmentStatement(nameIdentifier, createCallableTable(functionExpression)),
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
context.addPrecedingStatements(lua.createVariableDeclarationStatement(nameIdentifier, functionExpression));
|
|
216
|
+
}
|
|
217
|
+
return lua.cloneIdentifier(nameIdentifier);
|
|
174
218
|
}
|
|
175
219
|
}
|
|
176
220
|
}
|
|
177
|
-
return
|
|
221
|
+
return isNamedFunctionExpression && isFunctionTypeWithProperties(context.checker.getTypeAtLocation(node))
|
|
222
|
+
? createCallableTable(functionExpression)
|
|
223
|
+
: functionExpression;
|
|
178
224
|
}
|
|
179
225
|
exports.transformFunctionLikeDeclaration = transformFunctionLikeDeclaration;
|
|
180
226
|
const transformFunctionDeclaration = (node, context) => {
|
|
@@ -201,7 +247,11 @@ const transformFunctionDeclaration = (node, context) => {
|
|
|
201
247
|
const functionInfo = { referencedSymbols: (_a = functionScope.referencedSymbols) !== null && _a !== void 0 ? _a : new Map() };
|
|
202
248
|
scope.functionDefinitions.set(name.symbolId, functionInfo);
|
|
203
249
|
}
|
|
204
|
-
|
|
250
|
+
// Wrap functions with properties into a callable table
|
|
251
|
+
const wrappedFunction = node.name && isFunctionTypeWithProperties(context.checker.getTypeAtLocation(node.name))
|
|
252
|
+
? createCallableTable(functionExpression)
|
|
253
|
+
: functionExpression;
|
|
254
|
+
return (0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, name, wrappedFunction, node);
|
|
205
255
|
};
|
|
206
256
|
exports.transformFunctionDeclaration = transformFunctionDeclaration;
|
|
207
257
|
const transformYieldExpression = (expression, context) => {
|
|
@@ -5,6 +5,7 @@ const ts = require("typescript");
|
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const builtins_1 = require("../builtins");
|
|
7
7
|
const promise_1 = require("../builtins/promise");
|
|
8
|
+
const context_1 = require("../context");
|
|
8
9
|
const annotations_1 = require("../utils/annotations");
|
|
9
10
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
10
11
|
const export_1 = require("../utils/export");
|
|
@@ -16,7 +17,11 @@ const operators_1 = require("./language-extensions/operators");
|
|
|
16
17
|
const range_1 = require("./language-extensions/range");
|
|
17
18
|
const table_1 = require("./language-extensions/table");
|
|
18
19
|
const vararg_1 = require("./language-extensions/vararg");
|
|
20
|
+
const optional_chaining_1 = require("./optional-chaining");
|
|
19
21
|
function transformIdentifier(context, identifier) {
|
|
22
|
+
if ((0, optional_chaining_1.isOptionalContinuation)(identifier)) {
|
|
23
|
+
return lua.createIdentifier(identifier.text, undefined, context_1.tempSymbolId);
|
|
24
|
+
}
|
|
20
25
|
if ((0, multi_1.isMultiFunctionNode)(context, identifier)) {
|
|
21
26
|
context.diagnostics.push((0, diagnostics_1.invalidMultiFunctionUse)(identifier));
|
|
22
27
|
return lua.createAnonymousIdentifier(identifier);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { TransformationContext } from "../../context";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
import * as lua from "../../../LuaAST";
|
|
4
|
+
export declare function transformLanguageExtensionCallExpression(context: TransformationContext, node: ts.CallExpression, isOptionalCall: boolean): lua.Expression | undefined;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformLanguageExtensionCallExpression = void 0;
|
|
4
|
+
const operators_1 = require("./operators");
|
|
5
|
+
const table_1 = require("./table");
|
|
6
|
+
function transformLanguageExtensionCallExpression(context, node, isOptionalCall) {
|
|
7
|
+
const operatorMapping = (0, operators_1.transformOperatorMappingExpression)(context, node, isOptionalCall);
|
|
8
|
+
if (operatorMapping) {
|
|
9
|
+
return operatorMapping;
|
|
10
|
+
}
|
|
11
|
+
const tableCall = (0, table_1.transformTableExtensionCall)(context, node, isOptionalCall);
|
|
12
|
+
if (tableCall) {
|
|
13
|
+
return tableCall;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.transformLanguageExtensionCallExpression = transformLanguageExtensionCallExpression;
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -2,4 +2,4 @@ import * as ts from "typescript";
|
|
|
2
2
|
import * as lua from "../../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../../context";
|
|
4
4
|
export declare function isOperatorMapping(context: TransformationContext, node: ts.CallExpression | ts.Identifier): boolean;
|
|
5
|
-
export declare function transformOperatorMappingExpression(context: TransformationContext, node: ts.CallExpression): lua.Expression;
|
|
5
|
+
export declare function transformOperatorMappingExpression(context: TransformationContext, node: ts.CallExpression, isOptionalCall: boolean): lua.Expression | undefined;
|
|
@@ -77,9 +77,14 @@ function isOperatorMapping(context, node) {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
exports.isOperatorMapping = isOperatorMapping;
|
|
80
|
-
function transformOperatorMappingExpression(context, node) {
|
|
80
|
+
function transformOperatorMappingExpression(context, node, isOptionalCall) {
|
|
81
81
|
const extensionKind = getOperatorMapExtensionKindForCall(context, node);
|
|
82
|
-
|
|
82
|
+
if (!extensionKind)
|
|
83
|
+
return undefined;
|
|
84
|
+
if (isOptionalCall) {
|
|
85
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedBuiltinOptionalCall)(node));
|
|
86
|
+
return lua.createNilLiteral();
|
|
87
|
+
}
|
|
83
88
|
const isBefore53 = context.luaTarget === CompilerOptions_1.LuaTarget.Lua51 ||
|
|
84
89
|
context.luaTarget === CompilerOptions_1.LuaTarget.Lua52 ||
|
|
85
90
|
context.luaTarget === CompilerOptions_1.LuaTarget.LuaJIT ||
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import * as lua from "../../../LuaAST";
|
|
3
|
+
import { TransformationContext } from "../../context";
|
|
4
|
+
export declare function isPairsIterableExpression(context: TransformationContext, expression: ts.Expression): boolean;
|
|
5
|
+
export declare function transformForOfPairsIterableStatement(context: TransformationContext, statement: ts.ForOfStatement, block: lua.Block): lua.Statement;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformForOfPairsIterableStatement = exports.isPairsIterableExpression = void 0;
|
|
4
|
+
const ts = require("typescript");
|
|
5
|
+
const lua = require("../../../LuaAST");
|
|
6
|
+
const utils_1 = require("../../../utils");
|
|
7
|
+
const diagnostics_1 = require("../../utils/diagnostics");
|
|
8
|
+
const extensions = require("../../utils/language-extensions");
|
|
9
|
+
const utils_2 = require("../loops/utils");
|
|
10
|
+
const variable_declaration_1 = require("../variable-declaration");
|
|
11
|
+
function isPairsIterableType(type) {
|
|
12
|
+
return extensions.isExtensionType(type, extensions.ExtensionKind.PairsIterableType);
|
|
13
|
+
}
|
|
14
|
+
function isPairsIterableExpression(context, expression) {
|
|
15
|
+
const type = context.checker.getTypeAtLocation(expression);
|
|
16
|
+
return isPairsIterableType(type);
|
|
17
|
+
}
|
|
18
|
+
exports.isPairsIterableExpression = isPairsIterableExpression;
|
|
19
|
+
function transformForOfPairsIterableStatement(context, statement, block) {
|
|
20
|
+
const pairsCall = lua.createCallExpression(lua.createIdentifier("pairs"), [
|
|
21
|
+
context.transformExpression(statement.expression),
|
|
22
|
+
]);
|
|
23
|
+
let identifiers = [];
|
|
24
|
+
if (ts.isVariableDeclarationList(statement.initializer)) {
|
|
25
|
+
// Variables declared in for loop
|
|
26
|
+
// for key, value in iterable do
|
|
27
|
+
const binding = (0, utils_2.getVariableDeclarationBinding)(context, statement.initializer);
|
|
28
|
+
if (ts.isArrayBindingPattern(binding)) {
|
|
29
|
+
identifiers = binding.elements.map(e => (0, variable_declaration_1.transformArrayBindingElement)(context, e));
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
context.diagnostics.push((0, diagnostics_1.invalidPairsIterableWithoutDestructuring)(binding));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else if (ts.isArrayLiteralExpression(statement.initializer)) {
|
|
36
|
+
// Variables NOT declared in for loop - catch iterator values in temps and assign
|
|
37
|
+
// for ____key, ____value in iterable do
|
|
38
|
+
// key, value = ____key, ____value
|
|
39
|
+
identifiers = statement.initializer.elements.map(e => context.createTempNameForNode(e));
|
|
40
|
+
if (identifiers.length > 0) {
|
|
41
|
+
block.statements.unshift(lua.createAssignmentStatement(statement.initializer.elements.map(e => (0, utils_1.cast)(context.transformExpression(e), lua.isAssignmentLeftHandSideExpression)), identifiers));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
context.diagnostics.push((0, diagnostics_1.invalidPairsIterableWithoutDestructuring)(statement.initializer));
|
|
46
|
+
}
|
|
47
|
+
if (identifiers.length === 0) {
|
|
48
|
+
identifiers.push(lua.createAnonymousIdentifier());
|
|
49
|
+
}
|
|
50
|
+
return lua.createForInStatement(block, identifiers, [pairsCall], statement);
|
|
51
|
+
}
|
|
52
|
+
exports.transformForOfPairsIterableStatement = transformForOfPairsIterableStatement;
|
|
53
|
+
//# sourceMappingURL=pairsIterable.js.map
|
|
@@ -2,12 +2,5 @@ import * as ts from "typescript";
|
|
|
2
2
|
import * as lua from "../../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../../context";
|
|
4
4
|
export declare function isTableExtensionIdentifier(context: TransformationContext, node: ts.Identifier): boolean;
|
|
5
|
-
export declare function isTableDeleteCall(context: TransformationContext, node: ts.CallExpression): boolean;
|
|
6
|
-
export declare function isTableGetCall(context: TransformationContext, node: ts.CallExpression): boolean;
|
|
7
|
-
export declare function isTableHasCall(context: TransformationContext, node: ts.CallExpression): boolean;
|
|
8
|
-
export declare function isTableSetCall(context: TransformationContext, node: ts.CallExpression): boolean;
|
|
9
5
|
export declare function isTableNewCall(context: TransformationContext, node: ts.NewExpression): boolean;
|
|
10
|
-
export declare function
|
|
11
|
-
export declare function transformTableGetExpression(context: TransformationContext, node: ts.CallExpression): lua.Expression;
|
|
12
|
-
export declare function transformTableHasExpression(context: TransformationContext, node: ts.CallExpression): lua.Expression;
|
|
13
|
-
export declare function transformTableSetExpression(context: TransformationContext, node: ts.CallExpression): lua.Statement;
|
|
6
|
+
export declare function transformTableExtensionCall(context: TransformationContext, node: ts.CallExpression, isOptionalCall: boolean): lua.Expression | undefined;
|
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.transformTableExtensionCall = exports.isTableNewCall = exports.isTableExtensionIdentifier = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../../LuaAST");
|
|
6
6
|
const extensions = require("../../utils/language-extensions");
|
|
7
7
|
const typescript_1 = require("../../utils/typescript");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
8
|
+
const expression_list_1 = require("../expression-list");
|
|
9
|
+
const diagnostics_1 = require("../../utils/diagnostics");
|
|
10
|
+
const tableCallExtensions = [
|
|
10
11
|
extensions.ExtensionKind.TableDeleteType,
|
|
11
12
|
extensions.ExtensionKind.TableDeleteMethodType,
|
|
13
|
+
extensions.ExtensionKind.TableGetType,
|
|
14
|
+
extensions.ExtensionKind.TableGetMethodType,
|
|
15
|
+
extensions.ExtensionKind.TableHasType,
|
|
16
|
+
extensions.ExtensionKind.TableHasMethodType,
|
|
17
|
+
extensions.ExtensionKind.TableSetType,
|
|
18
|
+
extensions.ExtensionKind.TableSetMethodType,
|
|
12
19
|
];
|
|
13
|
-
const
|
|
14
|
-
const tableHasExtensions = [extensions.ExtensionKind.TableHasType, extensions.ExtensionKind.TableHasMethodType];
|
|
15
|
-
const tableSetExtensions = [extensions.ExtensionKind.TableSetType, extensions.ExtensionKind.TableSetMethodType];
|
|
16
|
-
const tableExtensions = [
|
|
17
|
-
extensions.ExtensionKind.TableNewType,
|
|
18
|
-
...tableDeleteExtensions,
|
|
19
|
-
...tableGetExtensions,
|
|
20
|
-
...tableHasExtensions,
|
|
21
|
-
...tableSetExtensions,
|
|
22
|
-
];
|
|
20
|
+
const tableExtensions = [extensions.ExtensionKind.TableNewType, ...tableCallExtensions];
|
|
23
21
|
function getTableExtensionKindForCall(context, node, validExtensions) {
|
|
24
22
|
const type = (0, typescript_1.getFunctionTypeForCall)(context, node);
|
|
25
23
|
return type && validExtensions.find(extensionKind => extensions.isExtensionType(type, extensionKind));
|
|
@@ -29,79 +27,83 @@ function isTableExtensionIdentifier(context, node) {
|
|
|
29
27
|
return tableExtensions.some(extensionKind => extensions.isExtensionType(type, extensionKind));
|
|
30
28
|
}
|
|
31
29
|
exports.isTableExtensionIdentifier = isTableExtensionIdentifier;
|
|
32
|
-
function isTableDeleteCall(context, node) {
|
|
33
|
-
return getTableExtensionKindForCall(context, node, tableDeleteExtensions) !== undefined;
|
|
34
|
-
}
|
|
35
|
-
exports.isTableDeleteCall = isTableDeleteCall;
|
|
36
|
-
function isTableGetCall(context, node) {
|
|
37
|
-
return getTableExtensionKindForCall(context, node, tableGetExtensions) !== undefined;
|
|
38
|
-
}
|
|
39
|
-
exports.isTableGetCall = isTableGetCall;
|
|
40
|
-
function isTableHasCall(context, node) {
|
|
41
|
-
return getTableExtensionKindForCall(context, node, tableHasExtensions) !== undefined;
|
|
42
|
-
}
|
|
43
|
-
exports.isTableHasCall = isTableHasCall;
|
|
44
|
-
function isTableSetCall(context, node) {
|
|
45
|
-
return getTableExtensionKindForCall(context, node, tableSetExtensions) !== undefined;
|
|
46
|
-
}
|
|
47
|
-
exports.isTableSetCall = isTableSetCall;
|
|
48
30
|
function isTableNewCall(context, node) {
|
|
49
31
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
50
32
|
return extensions.isExtensionType(type, extensions.ExtensionKind.TableNewType);
|
|
51
33
|
}
|
|
52
34
|
exports.isTableNewCall = isTableNewCall;
|
|
53
|
-
function
|
|
54
|
-
const
|
|
55
|
-
|
|
35
|
+
function transformTableExtensionCall(context, node, isOptionalCall) {
|
|
36
|
+
const extensionType = getTableExtensionKindForCall(context, node, tableCallExtensions);
|
|
37
|
+
if (!extensionType)
|
|
38
|
+
return;
|
|
39
|
+
if (isOptionalCall) {
|
|
40
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedBuiltinOptionalCall)(node));
|
|
41
|
+
return lua.createNilLiteral();
|
|
42
|
+
}
|
|
43
|
+
if (extensionType === extensions.ExtensionKind.TableDeleteType ||
|
|
44
|
+
extensionType === extensions.ExtensionKind.TableDeleteMethodType) {
|
|
45
|
+
return transformTableDeleteExpression(context, node, extensionType);
|
|
46
|
+
}
|
|
47
|
+
if (extensionType === extensions.ExtensionKind.TableGetType ||
|
|
48
|
+
extensionType === extensions.ExtensionKind.TableGetMethodType) {
|
|
49
|
+
return transformTableGetExpression(context, node, extensionType);
|
|
50
|
+
}
|
|
51
|
+
if (extensionType === extensions.ExtensionKind.TableHasType ||
|
|
52
|
+
extensionType === extensions.ExtensionKind.TableHasMethodType) {
|
|
53
|
+
return transformTableHasExpression(context, node, extensionType);
|
|
54
|
+
}
|
|
55
|
+
if (extensionType === extensions.ExtensionKind.TableSetType ||
|
|
56
|
+
extensionType === extensions.ExtensionKind.TableSetMethodType) {
|
|
57
|
+
return transformTableSetExpression(context, node, extensionType);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.transformTableExtensionCall = transformTableExtensionCall;
|
|
61
|
+
function transformTableDeleteExpression(context, node, extensionKind) {
|
|
56
62
|
const args = node.arguments.slice();
|
|
57
|
-
if (
|
|
63
|
+
if (extensionKind === extensions.ExtensionKind.TableDeleteMethodType &&
|
|
58
64
|
(ts.isPropertyAccessExpression(node.expression) || ts.isElementAccessExpression(node.expression))) {
|
|
59
65
|
// In case of method (no table argument), push method owner to front of args list
|
|
60
66
|
args.unshift(node.expression.expression);
|
|
61
67
|
}
|
|
62
68
|
// arg0[arg1] = nil
|
|
63
|
-
|
|
69
|
+
const [table, accessExpression] = (0, expression_list_1.transformExpressionList)(context, args);
|
|
70
|
+
context.addPrecedingStatements(lua.createAssignmentStatement(lua.createTableIndexExpression(table, accessExpression), lua.createNilLiteral(), node));
|
|
71
|
+
return lua.createBooleanLiteral(true);
|
|
64
72
|
}
|
|
65
|
-
|
|
66
|
-
function transformTableGetExpression(context, node) {
|
|
67
|
-
const extensionKind = getTableExtensionKindForCall(context, node, tableGetExtensions);
|
|
68
|
-
(0, utils_1.assert)(extensionKind);
|
|
73
|
+
function transformTableGetExpression(context, node, extensionKind) {
|
|
69
74
|
const args = node.arguments.slice();
|
|
70
|
-
if (
|
|
75
|
+
if (extensionKind === extensions.ExtensionKind.TableGetMethodType &&
|
|
71
76
|
(ts.isPropertyAccessExpression(node.expression) || ts.isElementAccessExpression(node.expression))) {
|
|
72
77
|
// In case of method (no table argument), push method owner to front of args list
|
|
73
78
|
args.unshift(node.expression.expression);
|
|
74
79
|
}
|
|
80
|
+
const [table, accessExpression] = (0, expression_list_1.transformExpressionList)(context, args);
|
|
75
81
|
// arg0[arg1]
|
|
76
|
-
return lua.createTableIndexExpression(
|
|
82
|
+
return lua.createTableIndexExpression(table, accessExpression, node);
|
|
77
83
|
}
|
|
78
|
-
|
|
79
|
-
function transformTableHasExpression(context, node) {
|
|
80
|
-
const extensionKind = getTableExtensionKindForCall(context, node, tableHasExtensions);
|
|
81
|
-
(0, utils_1.assert)(extensionKind);
|
|
84
|
+
function transformTableHasExpression(context, node, extensionKind) {
|
|
82
85
|
const args = node.arguments.slice();
|
|
83
|
-
if (
|
|
86
|
+
if (extensionKind === extensions.ExtensionKind.TableHasMethodType &&
|
|
84
87
|
(ts.isPropertyAccessExpression(node.expression) || ts.isElementAccessExpression(node.expression))) {
|
|
85
88
|
// In case of method (no table argument), push method owner to front of args list
|
|
86
89
|
args.unshift(node.expression.expression);
|
|
87
90
|
}
|
|
88
91
|
// arg0[arg1]
|
|
89
|
-
const
|
|
92
|
+
const [table, accessExpression] = (0, expression_list_1.transformExpressionList)(context, args);
|
|
93
|
+
const tableIndexExpression = lua.createTableIndexExpression(table, accessExpression);
|
|
90
94
|
// arg0[arg1] ~= nil
|
|
91
95
|
return lua.createBinaryExpression(tableIndexExpression, lua.createNilLiteral(), lua.SyntaxKind.InequalityOperator, node);
|
|
92
96
|
}
|
|
93
|
-
|
|
94
|
-
function transformTableSetExpression(context, node) {
|
|
95
|
-
const extensionKind = getTableExtensionKindForCall(context, node, tableSetExtensions);
|
|
96
|
-
(0, utils_1.assert)(extensionKind);
|
|
97
|
+
function transformTableSetExpression(context, node, extensionKind) {
|
|
97
98
|
const args = node.arguments.slice();
|
|
98
|
-
if (
|
|
99
|
+
if (extensionKind === extensions.ExtensionKind.TableSetMethodType &&
|
|
99
100
|
(ts.isPropertyAccessExpression(node.expression) || ts.isElementAccessExpression(node.expression))) {
|
|
100
101
|
// In case of method (no table argument), push method owner to front of args list
|
|
101
102
|
args.unshift(node.expression.expression);
|
|
102
103
|
}
|
|
103
104
|
// arg0[arg1] = arg2
|
|
104
|
-
|
|
105
|
+
const [table, accessExpression, value] = (0, expression_list_1.transformExpressionList)(context, args);
|
|
106
|
+
context.addPrecedingStatements(lua.createAssignmentStatement(lua.createTableIndexExpression(table, accessExpression), value, node));
|
|
107
|
+
return lua.createNilLiteral();
|
|
105
108
|
}
|
|
106
|
-
exports.transformTableSetExpression = transformTableSetExpression;
|
|
107
109
|
//# sourceMappingURL=table.js.map
|