typescript-to-lua 1.6.3 → 1.7.0
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/dist/CompilerOptions.d.ts +1 -0
- package/dist/cli/parse.js +5 -0
- package/dist/lualib/ArraySlice.lua +10 -2
- package/dist/lualib/ArraySplice.lua +5 -1
- package/dist/lualib/Generator.lua +8 -1
- package/dist/lualib/ParseFloat.lua +5 -1
- package/dist/lualib/SparseArraySpread.lua +5 -1
- package/dist/lualib/StringCharCodeAt.lua +5 -1
- package/dist/lualib/lualib_bundle.lua +38 -7
- package/dist/lualib-build/plugin.js +1 -2
- package/dist/measure-performance.d.ts +15 -0
- package/dist/measure-performance.js +83 -0
- package/dist/transformation/builtins/array.js +2 -1
- package/dist/transformation/builtins/function.js +1 -1
- package/dist/transformation/builtins/global.d.ts +1 -1
- package/dist/transformation/builtins/global.js +13 -12
- package/dist/transformation/builtins/index.d.ts +2 -2
- package/dist/transformation/builtins/index.js +87 -89
- package/dist/transformation/builtins/object.d.ts +1 -1
- package/dist/transformation/builtins/object.js +4 -4
- package/dist/transformation/builtins/promise.js +4 -2
- package/dist/transformation/builtins/string.js +2 -0
- package/dist/transformation/context/context.d.ts +15 -0
- package/dist/transformation/context/context.js +66 -24
- package/dist/transformation/context/visitors.d.ts +1 -1
- package/dist/transformation/index.js +6 -5
- package/dist/transformation/utils/annotations.d.ts +1 -17
- package/dist/transformation/utils/annotations.js +48 -88
- package/dist/transformation/utils/assignment-validation.js +27 -23
- package/dist/transformation/utils/diagnostics.d.ts +1 -4
- package/dist/transformation/utils/diagnostics.js +2 -3
- package/dist/transformation/utils/function-context.d.ts +1 -1
- package/dist/transformation/utils/function-context.js +57 -51
- package/dist/transformation/utils/language-extensions.d.ts +57 -51
- package/dist/transformation/utils/language-extensions.js +99 -119
- package/dist/transformation/utils/lualib.d.ts +0 -1
- package/dist/transformation/utils/lualib.js +2 -8
- package/dist/transformation/utils/safe-names.d.ts +1 -1
- package/dist/transformation/utils/safe-names.js +3 -6
- package/dist/transformation/utils/scope.d.ts +0 -2
- package/dist/transformation/utils/scope.js +10 -27
- package/dist/transformation/utils/symbols.d.ts +1 -1
- package/dist/transformation/utils/symbols.js +7 -20
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +2 -1
- package/dist/transformation/utils/typescript/types.d.ts +2 -5
- package/dist/transformation/utils/typescript/types.js +27 -37
- package/dist/transformation/visitors/access.js +1 -4
- package/dist/transformation/visitors/block.js +4 -4
- package/dist/transformation/visitors/call.js +7 -18
- package/dist/transformation/visitors/class/index.js +8 -20
- package/dist/transformation/visitors/class/members/constructor.js +2 -2
- package/dist/transformation/visitors/class/new.js +2 -5
- package/dist/transformation/visitors/class/setup.d.ts +1 -1
- package/dist/transformation/visitors/class/setup.js +4 -4
- package/dist/transformation/visitors/class/utils.d.ts +1 -1
- package/dist/transformation/visitors/class/utils.js +2 -9
- package/dist/transformation/visitors/conditional.js +4 -4
- package/dist/transformation/visitors/function.d.ts +1 -1
- package/dist/transformation/visitors/function.js +8 -16
- package/dist/transformation/visitors/identifier.d.ts +1 -0
- package/dist/transformation/visitors/identifier.js +40 -44
- package/dist/transformation/visitors/language-extensions/call-extension.d.ts +10 -0
- package/dist/transformation/visitors/language-extensions/call-extension.js +23 -0
- package/dist/transformation/visitors/language-extensions/identifier.d.ts +5 -0
- package/dist/transformation/visitors/language-extensions/identifier.js +27 -0
- package/dist/transformation/visitors/language-extensions/iterable.d.ts +2 -3
- package/dist/transformation/visitors/language-extensions/iterable.js +21 -22
- package/dist/transformation/visitors/language-extensions/multi.d.ts +0 -1
- package/dist/transformation/visitors/language-extensions/multi.js +10 -24
- package/dist/transformation/visitors/language-extensions/operators.d.ts +2 -5
- package/dist/transformation/visitors/language-extensions/operators.js +89 -96
- package/dist/transformation/visitors/language-extensions/range.js +4 -2
- package/dist/transformation/visitors/language-extensions/table.d.ts +6 -3
- package/dist/transformation/visitors/language-extensions/table.js +46 -75
- package/dist/transformation/visitors/language-extensions/vararg.js +7 -3
- package/dist/transformation/visitors/literal.js +2 -23
- package/dist/transformation/visitors/loops/for-of.js +18 -18
- package/dist/transformation/visitors/loops/utils.js +5 -5
- package/dist/transformation/visitors/namespace.js +5 -14
- package/dist/transformation/visitors/return.js +2 -3
- package/dist/transformation/visitors/sourceFile.js +3 -4
- package/dist/transformation/visitors/spread.js +0 -5
- package/dist/transformation/visitors/switch.js +2 -2
- package/dist/transformation/visitors/variable-declaration.js +1 -1
- package/dist/transpilation/plugins.js +3 -0
- package/dist/transpilation/resolve.js +144 -161
- package/dist/transpilation/transpile.js +10 -0
- package/dist/transpilation/transpiler.d.ts +1 -0
- package/dist/transpilation/transpiler.js +18 -5
- package/dist/tstl.js +21 -0
- package/language-extensions/index.d.ts +156 -67
- package/package.json +2 -2
- package/dist/transformation/visitors/language-extensions/index.d.ts +0 -4
- package/dist/transformation/visitors/language-extensions/index.js +0 -17
- package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +0 -5
- package/dist/transformation/visitors/language-extensions/pairsIterable.js +0 -53
|
@@ -31,6 +31,7 @@ export interface TypeScriptToLuaOptions {
|
|
|
31
31
|
sourceMapTraceback?: boolean;
|
|
32
32
|
tstlVerbose?: boolean;
|
|
33
33
|
lua51AllowTryCatchInAsyncAwait?: boolean;
|
|
34
|
+
measurePerformance?: boolean;
|
|
34
35
|
}
|
|
35
36
|
export declare type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & TypeScriptToLuaOptions & {
|
|
36
37
|
[option: string]: any;
|
package/dist/cli/parse.js
CHANGED
|
@@ -79,6 +79,11 @@ exports.optionDeclarations = [
|
|
|
79
79
|
description: "Always allow try/catch in async/await functions for Lua 5.1.",
|
|
80
80
|
type: "boolean",
|
|
81
81
|
},
|
|
82
|
+
{
|
|
83
|
+
name: "measurePerformance",
|
|
84
|
+
description: "Measure performance of the tstl compiler.",
|
|
85
|
+
type: "boolean",
|
|
86
|
+
},
|
|
82
87
|
];
|
|
83
88
|
function updateParsedConfigFile(parsedConfigFile) {
|
|
84
89
|
let hasRootLevelOptions = false;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
local function __TS__ArraySlice(self, first, last)
|
|
2
2
|
local len = #self
|
|
3
|
-
|
|
3
|
+
local ____first_0 = first
|
|
4
|
+
if ____first_0 == nil then
|
|
5
|
+
____first_0 = 0
|
|
6
|
+
end
|
|
7
|
+
first = ____first_0
|
|
4
8
|
if first < 0 then
|
|
5
9
|
first = len + first
|
|
6
10
|
if first < 0 then
|
|
@@ -11,7 +15,11 @@ local function __TS__ArraySlice(self, first, last)
|
|
|
11
15
|
first = len
|
|
12
16
|
end
|
|
13
17
|
end
|
|
14
|
-
|
|
18
|
+
local ____last_1 = last
|
|
19
|
+
if ____last_1 == nil then
|
|
20
|
+
____last_1 = len
|
|
21
|
+
end
|
|
22
|
+
last = ____last_1
|
|
15
23
|
if last < 0 then
|
|
16
24
|
last = len + last
|
|
17
25
|
if last < 0 then
|
|
@@ -22,7 +22,11 @@ local function __TS__ArraySplice(self, ...)
|
|
|
22
22
|
elseif actualArgumentCount == 1 then
|
|
23
23
|
actualDeleteCount = len - start
|
|
24
24
|
else
|
|
25
|
-
|
|
25
|
+
local ____deleteCount_0 = deleteCount
|
|
26
|
+
if ____deleteCount_0 == nil then
|
|
27
|
+
____deleteCount_0 = 0
|
|
28
|
+
end
|
|
29
|
+
actualDeleteCount = ____deleteCount_0
|
|
26
30
|
if actualDeleteCount < 0 then
|
|
27
31
|
actualDeleteCount = 0
|
|
28
32
|
end
|
|
@@ -22,7 +22,14 @@ do
|
|
|
22
22
|
local args = {...}
|
|
23
23
|
local argsLength = select("#", ...)
|
|
24
24
|
return {
|
|
25
|
-
____coroutine = coroutine.create(function()
|
|
25
|
+
____coroutine = coroutine.create(function()
|
|
26
|
+
local ____fn_1 = fn
|
|
27
|
+
local ____unpack_0 = unpack
|
|
28
|
+
if ____unpack_0 == nil then
|
|
29
|
+
____unpack_0 = table.unpack
|
|
30
|
+
end
|
|
31
|
+
return ____fn_1(____unpack_0(args, 1, argsLength))
|
|
32
|
+
end),
|
|
26
33
|
[Symbol.iterator] = generatorIterator,
|
|
27
34
|
next = generatorNext
|
|
28
35
|
}
|
|
@@ -10,5 +10,9 @@ local function __TS__ParseFloat(numberString)
|
|
|
10
10
|
return ____temp_0
|
|
11
11
|
end
|
|
12
12
|
local number = tonumber(string.match(numberString, "^%s*(-?%d+%.?%d*)"))
|
|
13
|
-
|
|
13
|
+
local ____number_1 = number
|
|
14
|
+
if ____number_1 == nil then
|
|
15
|
+
____number_1 = 0 / 0
|
|
16
|
+
end
|
|
17
|
+
return ____number_1
|
|
14
18
|
end
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
local function __TS__SparseArraySpread(sparseArray)
|
|
2
|
-
local
|
|
2
|
+
local ____unpack_0 = unpack
|
|
3
|
+
if ____unpack_0 == nil then
|
|
4
|
+
____unpack_0 = table.unpack
|
|
5
|
+
end
|
|
6
|
+
local _unpack = ____unpack_0
|
|
3
7
|
return _unpack(sparseArray, 1, sparseArray.sparseLength)
|
|
4
8
|
end
|
|
@@ -5,5 +5,9 @@ local function __TS__StringCharCodeAt(self, index)
|
|
|
5
5
|
if index < 0 then
|
|
6
6
|
return 0 / 0
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
local ____string_byte_result_0 = string.byte(self, index + 1)
|
|
9
|
+
if ____string_byte_result_0 == nil then
|
|
10
|
+
____string_byte_result_0 = 0 / 0
|
|
11
|
+
end
|
|
12
|
+
return ____string_byte_result_0
|
|
9
13
|
end
|
|
@@ -345,7 +345,11 @@ end
|
|
|
345
345
|
|
|
346
346
|
local function __TS__ArraySlice(self, first, last)
|
|
347
347
|
local len = #self
|
|
348
|
-
|
|
348
|
+
local ____first_0 = first
|
|
349
|
+
if ____first_0 == nil then
|
|
350
|
+
____first_0 = 0
|
|
351
|
+
end
|
|
352
|
+
first = ____first_0
|
|
349
353
|
if first < 0 then
|
|
350
354
|
first = len + first
|
|
351
355
|
if first < 0 then
|
|
@@ -356,7 +360,11 @@ local function __TS__ArraySlice(self, first, last)
|
|
|
356
360
|
first = len
|
|
357
361
|
end
|
|
358
362
|
end
|
|
359
|
-
|
|
363
|
+
local ____last_1 = last
|
|
364
|
+
if ____last_1 == nil then
|
|
365
|
+
____last_1 = len
|
|
366
|
+
end
|
|
367
|
+
last = ____last_1
|
|
360
368
|
if last < 0 then
|
|
361
369
|
last = len + last
|
|
362
370
|
if last < 0 then
|
|
@@ -412,7 +420,11 @@ local function __TS__ArraySplice(self, ...)
|
|
|
412
420
|
elseif actualArgumentCount == 1 then
|
|
413
421
|
actualDeleteCount = len - start
|
|
414
422
|
else
|
|
415
|
-
|
|
423
|
+
local ____deleteCount_0 = deleteCount
|
|
424
|
+
if ____deleteCount_0 == nil then
|
|
425
|
+
____deleteCount_0 = 0
|
|
426
|
+
end
|
|
427
|
+
actualDeleteCount = ____deleteCount_0
|
|
416
428
|
if actualDeleteCount < 0 then
|
|
417
429
|
actualDeleteCount = 0
|
|
418
430
|
end
|
|
@@ -1175,7 +1187,14 @@ do
|
|
|
1175
1187
|
local args = {...}
|
|
1176
1188
|
local argsLength = select("#", ...)
|
|
1177
1189
|
return {
|
|
1178
|
-
____coroutine = coroutine.create(function()
|
|
1190
|
+
____coroutine = coroutine.create(function()
|
|
1191
|
+
local ____fn_1 = fn
|
|
1192
|
+
local ____unpack_0 = unpack
|
|
1193
|
+
if ____unpack_0 == nil then
|
|
1194
|
+
____unpack_0 = table.unpack
|
|
1195
|
+
end
|
|
1196
|
+
return ____fn_1(____unpack_0(args, 1, argsLength))
|
|
1197
|
+
end),
|
|
1179
1198
|
[Symbol.iterator] = generatorIterator,
|
|
1180
1199
|
next = generatorNext
|
|
1181
1200
|
}
|
|
@@ -1553,7 +1572,11 @@ local function __TS__ParseFloat(numberString)
|
|
|
1553
1572
|
return ____temp_0
|
|
1554
1573
|
end
|
|
1555
1574
|
local number = tonumber(string.match(numberString, "^%s*(-?%d+%.?%d*)"))
|
|
1556
|
-
|
|
1575
|
+
local ____number_1 = number
|
|
1576
|
+
if ____number_1 == nil then
|
|
1577
|
+
____number_1 = 0 / 0
|
|
1578
|
+
end
|
|
1579
|
+
return ____number_1
|
|
1557
1580
|
end
|
|
1558
1581
|
|
|
1559
1582
|
local function __TS__StringSubstr(self, from, length)
|
|
@@ -1947,7 +1970,11 @@ local function __TS__SparseArrayPush(sparseArray, ...)
|
|
|
1947
1970
|
end
|
|
1948
1971
|
|
|
1949
1972
|
local function __TS__SparseArraySpread(sparseArray)
|
|
1950
|
-
local
|
|
1973
|
+
local ____unpack_0 = unpack
|
|
1974
|
+
if ____unpack_0 == nil then
|
|
1975
|
+
____unpack_0 = table.unpack
|
|
1976
|
+
end
|
|
1977
|
+
local _unpack = ____unpack_0
|
|
1951
1978
|
return _unpack(sparseArray, 1, sparseArray.sparseLength)
|
|
1952
1979
|
end
|
|
1953
1980
|
|
|
@@ -2127,7 +2154,11 @@ local function __TS__StringCharCodeAt(self, index)
|
|
|
2127
2154
|
if index < 0 then
|
|
2128
2155
|
return 0 / 0
|
|
2129
2156
|
end
|
|
2130
|
-
|
|
2157
|
+
local ____string_byte_result_0 = string.byte(self, index + 1)
|
|
2158
|
+
if ____string_byte_result_0 == nil then
|
|
2159
|
+
____string_byte_result_0 = 0 / 0
|
|
2160
|
+
end
|
|
2161
|
+
return ____string_byte_result_0
|
|
2131
2162
|
end
|
|
2132
2163
|
|
|
2133
2164
|
local function __TS__StringEndsWith(self, searchString, endPosition)
|
|
@@ -4,7 +4,6 @@ exports.lualibDiagnostic = void 0;
|
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const tstl = require("..");
|
|
6
6
|
const path = require("path");
|
|
7
|
-
const lualib_1 = require("../transformation/utils/lualib");
|
|
8
7
|
const LuaLib_1 = require("../LuaLib");
|
|
9
8
|
const util_1 = require("./util");
|
|
10
9
|
const utils_1 = require("../utils");
|
|
@@ -51,7 +50,7 @@ class LuaLibPlugin {
|
|
|
51
50
|
}
|
|
52
51
|
// Transpile file as normal with tstl
|
|
53
52
|
const fileResult = context.superTransformNode(file)[0];
|
|
54
|
-
const usedFeatures = new Set(
|
|
53
|
+
const usedFeatures = new Set(context.usedLuaLibFeatures);
|
|
55
54
|
// Get all imports in file
|
|
56
55
|
const importNames = new Set();
|
|
57
56
|
const imports = file.statements.filter(ts.isImportDeclaration);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts a performance measurement section.
|
|
3
|
+
* @param name name of the measurement
|
|
4
|
+
*/
|
|
5
|
+
export declare function startSection(name: string): void;
|
|
6
|
+
/**
|
|
7
|
+
* Ends a performance measurement section.
|
|
8
|
+
* @param name name of the measurement
|
|
9
|
+
*/
|
|
10
|
+
export declare function endSection(name: string): void;
|
|
11
|
+
export declare function isMeasurementEnabled(): boolean;
|
|
12
|
+
export declare function enableMeasurement(): void;
|
|
13
|
+
export declare function disableMeasurement(): void;
|
|
14
|
+
export declare function forEachMeasure(callback: (measureName: string, duration: number) => void): void;
|
|
15
|
+
export declare function getTotalDuration(): number;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTotalDuration = exports.forEachMeasure = exports.disableMeasurement = exports.enableMeasurement = exports.isMeasurementEnabled = exports.endSection = exports.startSection = void 0;
|
|
4
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
5
|
+
// We use our own performance hooks implementation for easier use, but also call node's performance hooks, so it shows up in the profiler.
|
|
6
|
+
let enabled = false;
|
|
7
|
+
const marks = new Map();
|
|
8
|
+
const durations = new Map();
|
|
9
|
+
function timestamp() {
|
|
10
|
+
return perf_hooks_1.performance.now();
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Marks a performance event, with the given markName.
|
|
14
|
+
*/
|
|
15
|
+
function mark(markName) {
|
|
16
|
+
if (enabled) {
|
|
17
|
+
marks.set(markName, timestamp());
|
|
18
|
+
perf_hooks_1.performance.mark(markName);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Adds a performance measurement with the specified name.
|
|
23
|
+
*
|
|
24
|
+
* @param measureName The name of the performance measurement.
|
|
25
|
+
* @param startMarkName The name of the starting mark
|
|
26
|
+
* @param endMarkName The name of the ending mark
|
|
27
|
+
*/
|
|
28
|
+
function measure(measureName, startMarkName, endMarkName) {
|
|
29
|
+
var _a, _b, _c;
|
|
30
|
+
if (enabled) {
|
|
31
|
+
const end = (_a = marks.get(endMarkName)) !== null && _a !== void 0 ? _a : timestamp();
|
|
32
|
+
const start = (_b = marks.get(startMarkName)) !== null && _b !== void 0 ? _b : perf_hooks_1.performance.timeOrigin;
|
|
33
|
+
const previousDuration = (_c = durations.get(measureName)) !== null && _c !== void 0 ? _c : 0;
|
|
34
|
+
durations.set(measureName, previousDuration + (end - start));
|
|
35
|
+
perf_hooks_1.performance.measure(measureName, startMarkName, endMarkName);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Starts a performance measurement section.
|
|
40
|
+
* @param name name of the measurement
|
|
41
|
+
*/
|
|
42
|
+
function startSection(name) {
|
|
43
|
+
mark("start " + name);
|
|
44
|
+
}
|
|
45
|
+
exports.startSection = startSection;
|
|
46
|
+
/**
|
|
47
|
+
* Ends a performance measurement section.
|
|
48
|
+
* @param name name of the measurement
|
|
49
|
+
*/
|
|
50
|
+
function endSection(name) {
|
|
51
|
+
mark("end " + name);
|
|
52
|
+
measure(name, "start " + name, "end " + name);
|
|
53
|
+
}
|
|
54
|
+
exports.endSection = endSection;
|
|
55
|
+
function isMeasurementEnabled() {
|
|
56
|
+
return enabled;
|
|
57
|
+
}
|
|
58
|
+
exports.isMeasurementEnabled = isMeasurementEnabled;
|
|
59
|
+
function enableMeasurement() {
|
|
60
|
+
if (!enabled) {
|
|
61
|
+
enabled = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.enableMeasurement = enableMeasurement;
|
|
65
|
+
function disableMeasurement() {
|
|
66
|
+
if (enabled) {
|
|
67
|
+
enabled = false;
|
|
68
|
+
marks.clear();
|
|
69
|
+
durations.clear();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.disableMeasurement = disableMeasurement;
|
|
73
|
+
function forEachMeasure(callback) {
|
|
74
|
+
durations.forEach((duration, measureName) => callback(measureName, duration));
|
|
75
|
+
}
|
|
76
|
+
exports.forEachMeasure = forEachMeasure;
|
|
77
|
+
function getTotalDuration() {
|
|
78
|
+
let total = 0;
|
|
79
|
+
forEachMeasure((_, duration) => (total += duration));
|
|
80
|
+
return total;
|
|
81
|
+
}
|
|
82
|
+
exports.getTotalDuration = getTotalDuration;
|
|
83
|
+
//# sourceMappingURL=measure-performance.js.map
|
|
@@ -103,7 +103,8 @@ function transformArrayPrototypeCall(context, node, calledMethod) {
|
|
|
103
103
|
case "join":
|
|
104
104
|
const callerType = context.checker.getTypeAtLocation(calledMethod.expression);
|
|
105
105
|
const elementType = context.checker.getElementTypeOfArrayType(callerType);
|
|
106
|
-
if (elementType &&
|
|
106
|
+
if (elementType &&
|
|
107
|
+
(0, typescript_1.typeAlwaysHasSomeOfFlags)(context, elementType, ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike)) {
|
|
107
108
|
const defaultSeparatorLiteral = lua.createStringLiteral(",");
|
|
108
109
|
const param = params[0];
|
|
109
110
|
const parameters = [
|
|
@@ -5,9 +5,9 @@ const CompilerOptions_1 = require("../../CompilerOptions");
|
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
7
7
|
const function_context_1 = require("../utils/function-context");
|
|
8
|
-
const lua_ast_1 = require("../utils/lua-ast");
|
|
9
8
|
const lualib_1 = require("../utils/lualib");
|
|
10
9
|
const call_1 = require("../visitors/call");
|
|
10
|
+
const lua_ast_1 = require("../utils/lua-ast");
|
|
11
11
|
function transformFunctionPrototypeCall(context, node, calledMethod) {
|
|
12
12
|
const callerType = context.checker.getTypeAtLocation(calledMethod.expression);
|
|
13
13
|
if ((0, function_context_1.getFunctionContextType)(context, callerType) === function_context_1.ContextType.Void) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
|
-
export declare function
|
|
4
|
+
export declare function tryTransformBuiltinGlobalCall(context: TransformationContext, node: ts.CallExpression, expressionType: ts.Type): lua.Expression | undefined;
|
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.tryTransformBuiltinGlobalCall = void 0;
|
|
4
4
|
const lualib_1 = require("../utils/lualib");
|
|
5
5
|
const typescript_1 = require("../utils/typescript");
|
|
6
6
|
const call_1 = require("../visitors/call");
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
function tryTransformBuiltinGlobalCall(context, node, expressionType) {
|
|
8
|
+
function getParameters() {
|
|
9
|
+
const signature = context.checker.getResolvedSignature(node);
|
|
10
|
+
return (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
11
|
+
}
|
|
11
12
|
const name = expressionType.symbol.name;
|
|
12
13
|
switch (name) {
|
|
13
14
|
case "SymbolConstructor":
|
|
14
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Symbol, node, ...
|
|
15
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Symbol, node, ...getParameters());
|
|
15
16
|
case "NumberConstructor":
|
|
16
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Number, node, ...
|
|
17
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Number, node, ...getParameters());
|
|
17
18
|
case "isNaN":
|
|
18
19
|
case "isFinite":
|
|
19
20
|
const numberParameters = (0, typescript_1.isNumberType)(context, expressionType)
|
|
20
|
-
?
|
|
21
|
-
: [(0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Number, undefined, ...
|
|
21
|
+
? getParameters()
|
|
22
|
+
: [(0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Number, undefined, ...getParameters())];
|
|
22
23
|
return (0, lualib_1.transformLuaLibFunction)(context, name === "isNaN" ? lualib_1.LuaLibFeature.NumberIsNaN : lualib_1.LuaLibFeature.NumberIsFinite, node, ...numberParameters);
|
|
23
24
|
case "parseFloat":
|
|
24
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ParseFloat, node, ...
|
|
25
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ParseFloat, node, ...getParameters());
|
|
25
26
|
case "parseInt":
|
|
26
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ParseInt, node, ...
|
|
27
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ParseInt, node, ...getParameters());
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
|
-
exports.
|
|
30
|
+
exports.tryTransformBuiltinGlobalCall = tryTransformBuiltinGlobalCall;
|
|
30
31
|
//# sourceMappingURL=global.js.map
|
|
@@ -2,6 +2,6 @@ import * as ts from "typescript";
|
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
4
|
export declare function transformBuiltinPropertyAccessExpression(context: TransformationContext, node: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
5
|
-
export declare function transformBuiltinCallExpression(context: TransformationContext, node: ts.CallExpression
|
|
6
|
-
export declare function transformBuiltinIdentifierExpression(context: TransformationContext, node: ts.Identifier): lua.Expression | undefined;
|
|
5
|
+
export declare function transformBuiltinCallExpression(context: TransformationContext, node: ts.CallExpression): lua.Expression | undefined;
|
|
6
|
+
export declare function transformBuiltinIdentifierExpression(context: TransformationContext, node: ts.Identifier, symbol: ts.Symbol | undefined): lua.Expression | undefined;
|
|
7
7
|
export declare function checkForLuaLibType(context: TransformationContext, type: ts.Type): void;
|
|
@@ -22,10 +22,10 @@ const diagnostics_1 = require("../utils/diagnostics");
|
|
|
22
22
|
function transformBuiltinPropertyAccessExpression(context, node) {
|
|
23
23
|
const ownerType = context.checker.getTypeAtLocation(node.expression);
|
|
24
24
|
if (ts.isIdentifier(node.expression) && (0, typescript_1.isStandardLibraryType)(context, ownerType, undefined)) {
|
|
25
|
-
switch (
|
|
25
|
+
switch (ownerType.symbol.name) {
|
|
26
26
|
case "Math":
|
|
27
27
|
return (0, math_1.transformMathProperty)(context, node);
|
|
28
|
-
case "
|
|
28
|
+
case "SymbolConstructor":
|
|
29
29
|
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Symbol);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -40,102 +40,95 @@ function transformBuiltinPropertyAccessExpression(context, node) {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
exports.transformBuiltinPropertyAccessExpression = transformBuiltinPropertyAccessExpression;
|
|
43
|
-
function transformBuiltinCallExpression(context, node
|
|
44
|
-
const unsupportedOptionalCall = () => {
|
|
45
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedBuiltinOptionalCall)(node));
|
|
46
|
-
return lua.createNilLiteral();
|
|
47
|
-
};
|
|
43
|
+
function transformBuiltinCallExpression(context, node) {
|
|
48
44
|
const expressionType = context.checker.getTypeAtLocation(node.expression);
|
|
49
45
|
if (ts.isIdentifier(node.expression) && (0, typescript_1.isStandardLibraryType)(context, expressionType, undefined)) {
|
|
50
46
|
checkForLuaLibType(context, expressionType);
|
|
51
|
-
const result = (0, global_1.
|
|
52
|
-
if (result)
|
|
53
|
-
if (isOptionalCall)
|
|
54
|
-
return unsupportedOptionalCall();
|
|
47
|
+
const result = (0, global_1.tryTransformBuiltinGlobalCall)(context, node, expressionType);
|
|
48
|
+
if (result)
|
|
55
49
|
return result;
|
|
56
|
-
}
|
|
57
50
|
}
|
|
58
51
|
const calledMethod = ts.getOriginalNode((0, call_1.getCalledExpression)(node));
|
|
59
|
-
if (
|
|
60
|
-
|
|
52
|
+
if (ts.isPropertyAccessExpression(calledMethod)) {
|
|
53
|
+
const globalResult = tryTransformBuiltinGlobalMethodCall(context, node, calledMethod);
|
|
54
|
+
if (globalResult)
|
|
55
|
+
return globalResult;
|
|
56
|
+
const prototypeResult = tryTransformBuiltinPropertyCall(context, node, calledMethod);
|
|
57
|
+
if (prototypeResult)
|
|
58
|
+
return prototypeResult;
|
|
59
|
+
// object prototype call may work even without resolved signature/type (which the other builtin calls use)
|
|
60
|
+
// e.g. (foo as any).toString()
|
|
61
|
+
// prototype methods take precedence (e.g. number.toString(2))
|
|
62
|
+
const objectResult = (0, object_1.tryTransformObjectPrototypeCall)(context, node, calledMethod);
|
|
63
|
+
if (objectResult)
|
|
64
|
+
return objectResult;
|
|
61
65
|
}
|
|
62
|
-
|
|
63
|
-
|
|
66
|
+
}
|
|
67
|
+
exports.transformBuiltinCallExpression = transformBuiltinCallExpression;
|
|
68
|
+
function tryTransformBuiltinGlobalMethodCall(context, node, calledMethod) {
|
|
64
69
|
const ownerType = context.checker.getTypeAtLocation(calledMethod.expression);
|
|
65
|
-
if ((0, typescript_1.isStandardLibraryType)(context, ownerType, undefined))
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
case "PromiseConstructor":
|
|
97
|
-
if (isOptionalCall || isOptionalAccess)
|
|
98
|
-
return unsupportedOptionalCall();
|
|
99
|
-
return (0, promise_1.transformPromiseConstructorCall)(context, node, calledMethod);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const isStringFunction = (0, typescript_1.isStringType)(context, ownerType) ||
|
|
103
|
-
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isStringType));
|
|
104
|
-
if (isStringFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) {
|
|
105
|
-
if (isOptionalCall)
|
|
106
|
-
return unsupportedOptionalCall();
|
|
107
|
-
return (0, string_1.transformStringPrototypeCall)(context, node, calledMethod);
|
|
108
|
-
}
|
|
109
|
-
const isNumberFunction = (0, typescript_1.isNumberType)(context, ownerType) ||
|
|
110
|
-
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isNumberType));
|
|
111
|
-
if (isNumberFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) {
|
|
112
|
-
if (isOptionalCall)
|
|
113
|
-
return unsupportedOptionalCall();
|
|
114
|
-
return (0, number_1.transformNumberPrototypeCall)(context, node, calledMethod);
|
|
115
|
-
}
|
|
116
|
-
const isArrayFunction = (0, typescript_1.isArrayType)(context, ownerType) ||
|
|
117
|
-
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isArrayType));
|
|
118
|
-
if (isArrayFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) {
|
|
119
|
-
if (isOptionalCall)
|
|
120
|
-
return unsupportedOptionalCall();
|
|
121
|
-
return (0, array_1.transformArrayPrototypeCall)(context, node, calledMethod);
|
|
70
|
+
if (!(0, typescript_1.isStandardLibraryType)(context, ownerType, undefined))
|
|
71
|
+
return;
|
|
72
|
+
const ownerSymbol = ownerType.symbol;
|
|
73
|
+
if (!ownerSymbol || ownerSymbol.parent)
|
|
74
|
+
return;
|
|
75
|
+
let result;
|
|
76
|
+
switch (ownerSymbol.name) {
|
|
77
|
+
case "ArrayConstructor":
|
|
78
|
+
result = (0, array_1.transformArrayConstructorCall)(context, node, calledMethod);
|
|
79
|
+
break;
|
|
80
|
+
case "Console":
|
|
81
|
+
result = (0, console_1.transformConsoleCall)(context, node, calledMethod);
|
|
82
|
+
break;
|
|
83
|
+
case "Math":
|
|
84
|
+
result = (0, math_1.transformMathCall)(context, node, calledMethod);
|
|
85
|
+
break;
|
|
86
|
+
case "StringConstructor":
|
|
87
|
+
result = (0, string_1.transformStringConstructorCall)(context, node, calledMethod);
|
|
88
|
+
break;
|
|
89
|
+
case "ObjectConstructor":
|
|
90
|
+
result = (0, object_1.transformObjectConstructorCall)(context, node, calledMethod);
|
|
91
|
+
break;
|
|
92
|
+
case "SymbolConstructor":
|
|
93
|
+
result = (0, symbol_1.transformSymbolConstructorCall)(context, node, calledMethod);
|
|
94
|
+
break;
|
|
95
|
+
case "NumberConstructor":
|
|
96
|
+
result = (0, number_1.transformNumberConstructorCall)(context, node, calledMethod);
|
|
97
|
+
break;
|
|
98
|
+
case "PromiseConstructor":
|
|
99
|
+
result = (0, promise_1.transformPromiseConstructorCall)(context, node, calledMethod);
|
|
100
|
+
break;
|
|
122
101
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (isOptionalCall)
|
|
127
|
-
return unsupportedOptionalCall();
|
|
128
|
-
return (0, function_1.transformFunctionPrototypeCall)(context, node, calledMethod);
|
|
102
|
+
if (result && calledMethod.questionDotToken) {
|
|
103
|
+
// e.g. console?.log()
|
|
104
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedBuiltinOptionalCall)(calledMethod));
|
|
129
105
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
function tryTransformBuiltinPropertyCall(context, node, calledMethod) {
|
|
109
|
+
var _a;
|
|
110
|
+
const signatureDeclaration = (_a = context.checker.getResolvedSignature(node)) === null || _a === void 0 ? void 0 : _a.declaration;
|
|
111
|
+
if (!signatureDeclaration || !(0, typescript_1.isStandardLibraryDeclaration)(context, signatureDeclaration))
|
|
112
|
+
return;
|
|
113
|
+
const callSymbol = context.checker.getTypeAtLocation(signatureDeclaration).symbol;
|
|
114
|
+
const ownerSymbol = callSymbol.parent;
|
|
115
|
+
if (!ownerSymbol || ownerSymbol.parent)
|
|
116
|
+
return;
|
|
117
|
+
switch (ownerSymbol.name) {
|
|
118
|
+
case "String":
|
|
119
|
+
return (0, string_1.transformStringPrototypeCall)(context, node, calledMethod);
|
|
120
|
+
case "Number":
|
|
121
|
+
return (0, number_1.transformNumberPrototypeCall)(context, node, calledMethod);
|
|
122
|
+
case "Array":
|
|
123
|
+
case "ReadonlyArray":
|
|
124
|
+
return (0, array_1.transformArrayPrototypeCall)(context, node, calledMethod);
|
|
125
|
+
case "Function":
|
|
126
|
+
case "CallableFunction":
|
|
127
|
+
case "NewableFunction":
|
|
128
|
+
return (0, function_1.transformFunctionPrototypeCall)(context, node, calledMethod);
|
|
135
129
|
}
|
|
136
130
|
}
|
|
137
|
-
|
|
138
|
-
function transformBuiltinIdentifierExpression(context, node) {
|
|
131
|
+
function transformBuiltinIdentifierExpression(context, node, symbol) {
|
|
139
132
|
switch (node.text) {
|
|
140
133
|
case "NaN":
|
|
141
134
|
return (0, lua_ast_1.createNaN)(node);
|
|
@@ -144,7 +137,7 @@ function transformBuiltinIdentifierExpression(context, node) {
|
|
|
144
137
|
const huge = lua.createStringLiteral("huge");
|
|
145
138
|
return lua.createTableIndexExpression(math, huge, node);
|
|
146
139
|
case "globalThis":
|
|
147
|
-
return lua.createIdentifier("_G", node, (0, symbols_1.getIdentifierSymbolId)(context, node), "globalThis");
|
|
140
|
+
return lua.createIdentifier("_G", node, (0, symbols_1.getIdentifierSymbolId)(context, node, symbol), "globalThis");
|
|
148
141
|
}
|
|
149
142
|
}
|
|
150
143
|
exports.transformBuiltinIdentifierExpression = transformBuiltinIdentifierExpression;
|
|
@@ -163,9 +156,10 @@ const builtinErrorTypeNames = new Set([
|
|
|
163
156
|
"URIErrorConstructor",
|
|
164
157
|
]);
|
|
165
158
|
function checkForLuaLibType(context, type) {
|
|
166
|
-
|
|
159
|
+
const symbol = type.symbol;
|
|
160
|
+
if (!symbol || symbol.parent)
|
|
167
161
|
return;
|
|
168
|
-
const name =
|
|
162
|
+
const name = symbol.name;
|
|
169
163
|
switch (name) {
|
|
170
164
|
case "Map":
|
|
171
165
|
case "MapConstructor":
|
|
@@ -183,6 +177,10 @@ function checkForLuaLibType(context, type) {
|
|
|
183
177
|
case "WeakSetConstructor":
|
|
184
178
|
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.WeakSet);
|
|
185
179
|
return;
|
|
180
|
+
case "Promise":
|
|
181
|
+
case "PromiseConstructor":
|
|
182
|
+
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Promise);
|
|
183
|
+
return;
|
|
186
184
|
}
|
|
187
185
|
if (builtinErrorTypeNames.has(name)) {
|
|
188
186
|
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Error);
|
|
@@ -2,4 +2,4 @@ import * as lua from "../../LuaAST";
|
|
|
2
2
|
import * as ts from "typescript";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
4
|
export declare function transformObjectConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
5
|
-
export declare function
|
|
5
|
+
export declare function tryTransformObjectPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|