storm-lua-minify 0.2.0 → 0.9.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.
Files changed (116) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +157 -37
  3. package/dist/aggregateSpecialization.js +406 -0
  4. package/dist/ast2lua.js +223 -82
  5. package/dist/astWalk.js +162 -0
  6. package/dist/callGraph.js +372 -0
  7. package/dist/cli.js +53 -32
  8. package/dist/cliOptions.js +36 -0
  9. package/dist/cliProgress.js +87 -0
  10. package/dist/config.js +73 -0
  11. package/dist/constantFold.js +798 -0
  12. package/dist/controlFlow.js +266 -0
  13. package/dist/functionRewrites.js +580 -0
  14. package/dist/generatedAst.js +108 -0
  15. package/dist/generatedNode.js +23 -0
  16. package/dist/globalRename.js +35 -0
  17. package/dist/interproceduralAnalysis.js +842 -0
  18. package/dist/interproceduralConstants.js +120 -0
  19. package/dist/keywordLocator.js +68 -0
  20. package/dist/linker.js +13 -0
  21. package/dist/luaString.js +157 -0
  22. package/dist/minifier.js +1243 -35
  23. package/dist/optimizerAnalysis.js +43 -0
  24. package/dist/optimizerDiagnostics.js +65 -0
  25. package/dist/optimizerFacts.js +529 -0
  26. package/dist/optimizerPass.js +96 -0
  27. package/dist/optimizerTransaction.js +56 -0
  28. package/dist/optimizerValueDomain.js +180 -0
  29. package/dist/options.js +233 -0
  30. package/dist/progress.js +2 -0
  31. package/dist/removeUnused.js +145 -0
  32. package/dist/renamer.js +236 -53
  33. package/dist/resolver.js +37 -14
  34. package/dist/runtimeEnvironment.js +105 -0
  35. package/dist/sourceMetadata.js +314 -0
  36. package/dist/statementDataflow.js +259 -0
  37. package/dist/statementScheduler.js +595 -0
  38. package/dist/symbolLiveness.js +92 -0
  39. package/dist/tableEffects.js +356 -0
  40. package/dist/transform.js +40 -0
  41. package/dist/valueFlow.js +409 -0
  42. package/dist/wholeProgramExports.js +646 -0
  43. package/dist/wholeProgramFieldRenames.js +583 -0
  44. package/dist/wholeProgramFields.js +672 -0
  45. package/dist/wholeProgramObjects.js +783 -0
  46. package/package.json +50 -33
  47. package/.github/workflows/ci.yml +0 -39
  48. package/.github/workflows/publish.yml +0 -39
  49. package/.prettierignore +0 -3
  50. package/.prettierrc.json +0 -1
  51. package/dist/index.js +0 -27
  52. package/eslint.config.js +0 -29
  53. package/src/ast2lua.ts +0 -940
  54. package/src/cli.ts +0 -86
  55. package/src/linker.ts +0 -108
  56. package/src/minifier.ts +0 -284
  57. package/src/output.ts +0 -71
  58. package/src/renamer.ts +0 -134
  59. package/src/resolver.ts +0 -378
  60. package/test/circular-require.test.ts +0 -19
  61. package/test/fixtures/bare-require/main.lua +0 -2
  62. package/test/fixtures/bare-require/mod.lua +0 -1
  63. package/test/fixtures/bitwise-precedence/main.lua +0 -10
  64. package/test/fixtures/circular-require/a.lua +0 -2
  65. package/test/fixtures/circular-require/b.lua +0 -2
  66. package/test/fixtures/circular-require/main.lua +0 -2
  67. package/test/fixtures/dofile/greet.lua +0 -1
  68. package/test/fixtures/dofile/main.lua +0 -2
  69. package/test/fixtures/entry-scope-many-requires/dep_alpha.lua +0 -2
  70. package/test/fixtures/entry-scope-many-requires/dep_bravo.lua +0 -2
  71. package/test/fixtures/entry-scope-many-requires/dep_charlie.lua +0 -2
  72. package/test/fixtures/entry-scope-many-requires/dep_delta.lua +0 -2
  73. package/test/fixtures/entry-scope-many-requires/dep_echo.lua +0 -2
  74. package/test/fixtures/entry-scope-many-requires/dep_foxtrot.lua +0 -2
  75. package/test/fixtures/entry-scope-many-requires/dep_golf.lua +0 -2
  76. package/test/fixtures/entry-scope-many-requires/dep_hotel.lua +0 -2
  77. package/test/fixtures/entry-scope-many-requires/dep_india.lua +0 -2
  78. package/test/fixtures/entry-scope-many-requires/dep_juliet.lua +0 -2
  79. package/test/fixtures/entry-scope-many-requires/dep_kilo.lua +0 -2
  80. package/test/fixtures/entry-scope-many-requires/main.lua +0 -25
  81. package/test/fixtures/multi-require/common.lua +0 -1
  82. package/test/fixtures/multi-require/main.lua +0 -3
  83. package/test/fixtures/nested-module/main.lua +0 -2
  84. package/test/fixtures/nested-module/sub/deep.lua +0 -1
  85. package/test/fixtures/require-call/main.lua +0 -2
  86. package/test/fixtures/require-call/mod.lua +0 -5
  87. package/test/fixtures/require-in-expression/main.lua +0 -1
  88. package/test/fixtures/require-in-expression/mod.lua +0 -1
  89. package/test/fixtures/require-string-call/main.lua +0 -2
  90. package/test/fixtures/require-string-call/mod.lua +0 -5
  91. package/test/fixtures/single-file/main.lua +0 -15
  92. package/test/identifier-collision.test.ts +0 -28
  93. package/test/lib/collision.ts +0 -131
  94. package/test/lib/helpers.ts +0 -124
  95. package/test/no-rename.test.ts +0 -19
  96. package/test/output.test.ts +0 -95
  97. package/test/precedence.test.ts +0 -28
  98. package/test/renamer.test.ts +0 -130
  99. package/test/resolver.test.ts +0 -206
  100. package/test/roundtrip.test.ts +0 -26
  101. package/test/snapshot.test.ts +0 -27
  102. package/test/snapshots/bare-require.sl.lua +0 -1
  103. package/test/snapshots/bitwise-precedence.sl.lua +0 -2
  104. package/test/snapshots/dofile.sl.lua +0 -1
  105. package/test/snapshots/entry-scope-many-requires.m.lua +0 -14
  106. package/test/snapshots/multi-require.m.lua +0 -4
  107. package/test/snapshots/multi-require.sl.lua +0 -1
  108. package/test/snapshots/nested-module.m.lua +0 -4
  109. package/test/snapshots/require-call.m.lua +0 -5
  110. package/test/snapshots/require-call.sl.lua +0 -1
  111. package/test/snapshots/require-in-expression.sl.lua +0 -1
  112. package/test/snapshots/require-string-call.m.lua +0 -5
  113. package/test/snapshots/single-file.sl.lua +0 -6
  114. package/test/sourcemap.test.ts +0 -105
  115. package/tsconfig.eslint.json +0 -8
  116. package/tsconfig.json +0 -111
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.analyzeControlFlow = analyzeControlFlow;
4
+ exports.childStatementBodies = childStatementBodies;
5
+ const astWalk_1 = require("./astWalk");
6
+ /**
7
+ * Builds a conservative intraprocedural CFG. Nested function bodies are separate execution units;
8
+ * closure construction never creates an execution edge into the nested body.
9
+ */
10
+ function analyzeControlFlow(chunk, resolved, version = 0) {
11
+ const units = [];
12
+ const nodes = [];
13
+ const unknownEdges = [];
14
+ const pointByStatement = new WeakMap();
15
+ const nodeByStatement = new WeakMap();
16
+ const analyzedFunctions = new WeakSet();
17
+ let nextUnitId = 0;
18
+ let nextNodeId = 0;
19
+ const createNode = (unit, kind, statement, clauseIndex) => {
20
+ const created = {
21
+ id: nextNodeId++,
22
+ unit,
23
+ kind,
24
+ ...(statement ? { statement } : {}),
25
+ ...(clauseIndex === undefined ? {} : { clauseIndex }),
26
+ successors: [],
27
+ predecessors: [],
28
+ };
29
+ nodes.push(created);
30
+ if (statement && !nodeByStatement.has(statement)) {
31
+ nodeByStatement.set(statement, created);
32
+ }
33
+ return created;
34
+ };
35
+ const connect = (from, to, kind) => {
36
+ const edge = { from, to, kind };
37
+ from.successors.push(edge);
38
+ to.predecessors.push(edge);
39
+ if (kind === "unknown")
40
+ unknownEdges.push(edge);
41
+ };
42
+ const analyzeUnit = (body, kind, parent, owner) => {
43
+ const unit = {
44
+ id: nextUnitId++,
45
+ kind,
46
+ body,
47
+ ...(parent ? { parent } : {}),
48
+ ...(owner ? { owner } : {}),
49
+ };
50
+ units.push(unit);
51
+ const entry = createNode(unit, "entry");
52
+ const exit = createNode(unit, "exit");
53
+ const labels = new Map();
54
+ const pendingGotos = [];
55
+ const buildStatement = (statement, continuation, loopExit, loopBack) => {
56
+ switch (statement.type) {
57
+ case "ReturnStatement": {
58
+ const current = createNode(unit, "statement", statement);
59
+ connect(current, exit, "return");
60
+ return current;
61
+ }
62
+ case "BreakStatement": {
63
+ const current = createNode(unit, "statement", statement);
64
+ connect(current, loopExit ?? exit, loopExit ? "break" : "unknown");
65
+ return current;
66
+ }
67
+ case "GotoStatement": {
68
+ const current = createNode(unit, "statement", statement);
69
+ pendingGotos.push({
70
+ node: current,
71
+ target: resolved.symbolOf(statement.label),
72
+ });
73
+ return current;
74
+ }
75
+ case "LabelStatement": {
76
+ const current = createNode(unit, "statement", statement);
77
+ connect(current, continuation, "fallthrough");
78
+ const symbol = resolved.symbolOf(statement.label);
79
+ if (symbol)
80
+ labels.set(symbol, current);
81
+ return current;
82
+ }
83
+ case "IfStatement": {
84
+ let falseTarget = continuation;
85
+ for (let index = statement.clauses.length - 1; index >= 0; index--) {
86
+ const clause = statement.clauses[index];
87
+ const branchStart = buildBlock(clause.body, continuation, loopExit, loopBack);
88
+ if (clause.type === "ElseClause") {
89
+ falseTarget = branchStart;
90
+ continue;
91
+ }
92
+ const condition = createNode(unit, "condition", statement, index);
93
+ connect(condition, branchStart, "branch-true");
94
+ connect(condition, falseTarget, "branch-false");
95
+ falseTarget = condition;
96
+ }
97
+ nodeByStatement.set(statement, falseTarget);
98
+ return falseTarget;
99
+ }
100
+ case "DoStatement": {
101
+ const current = createNode(unit, "statement", statement);
102
+ const bodyStart = buildBlock(statement.body, continuation, loopExit, loopBack);
103
+ connect(current, bodyStart, "fallthrough");
104
+ return current;
105
+ }
106
+ case "WhileStatement": {
107
+ const header = createNode(unit, "condition", statement);
108
+ const bodyStart = buildBlock(statement.body, header, continuation, header);
109
+ connect(header, bodyStart, "loop-body");
110
+ connect(header, continuation, "loop-exit");
111
+ return header;
112
+ }
113
+ case "RepeatStatement": {
114
+ const loopEntry = createNode(unit, "statement", statement);
115
+ const condition = createNode(unit, "condition", statement, 0);
116
+ const bodyStart = buildBlock(statement.body, condition, continuation, condition);
117
+ connect(loopEntry, bodyStart, "loop-body");
118
+ connect(condition, continuation, "loop-exit");
119
+ connect(condition, bodyStart, "loop-back");
120
+ return loopEntry;
121
+ }
122
+ case "ForNumericStatement":
123
+ case "ForGenericStatement": {
124
+ const header = createNode(unit, "condition", statement);
125
+ const bodyStart = buildBlock(statement.body, header, continuation, header);
126
+ connect(header, bodyStart, "loop-body");
127
+ connect(header, continuation, "loop-exit");
128
+ return header;
129
+ }
130
+ case "LocalStatement":
131
+ case "AssignmentStatement":
132
+ case "CallStatement":
133
+ case "FunctionDeclaration": {
134
+ const current = createNode(unit, "statement", statement);
135
+ connect(current, continuation, continuation === exit
136
+ ? "exit"
137
+ : continuation === loopBack
138
+ ? "loop-back"
139
+ : "fallthrough");
140
+ return current;
141
+ }
142
+ }
143
+ };
144
+ const buildBlock = (statements, continuation, loopExit, loopBack) => {
145
+ let next = continuation;
146
+ for (let index = statements.length - 1; index >= 0; index--) {
147
+ next = buildStatement(statements[index], next, loopExit, loopBack);
148
+ }
149
+ return next;
150
+ };
151
+ const first = buildBlock(body, exit);
152
+ connect(entry, first, first === exit ? "exit" : "entry");
153
+ pendingGotos.forEach(({ node, target: targetSymbol }) => {
154
+ const target = targetSymbol ? labels.get(targetSymbol) : undefined;
155
+ const known = target !== undefined;
156
+ connect(node, known ? target : exit, known ? "goto" : "unknown");
157
+ });
158
+ const indexPoints = (statements) => {
159
+ statements.forEach((statement, index) => {
160
+ const node = nodeByStatement.get(statement);
161
+ if (node)
162
+ pointByStatement.set(statement, {
163
+ unit,
164
+ body: statements,
165
+ index,
166
+ statement,
167
+ node,
168
+ });
169
+ childBodiesOfStatement(statement).forEach(indexPoints);
170
+ });
171
+ };
172
+ indexPoints(body);
173
+ const nestedFunctions = [];
174
+ body.forEach((statement) => {
175
+ (0, astWalk_1.walkStatement)(statement, {
176
+ onFunction: (fn) => nestedFunctions.push(fn),
177
+ });
178
+ });
179
+ nestedFunctions.forEach((fn) => {
180
+ if (analyzedFunctions.has(fn))
181
+ return;
182
+ analyzedFunctions.add(fn);
183
+ analyzeUnit(fn.body, "function", unit, fn);
184
+ });
185
+ };
186
+ analyzeUnit(chunk.body, "chunk");
187
+ const dominators = computeDominators(units, nodes);
188
+ const nodeDominates = (first, last) => first.unit === last.unit && (dominators.get(last)?.has(first) ?? false);
189
+ return {
190
+ version,
191
+ complete: unknownEdges.length === 0,
192
+ units,
193
+ nodes,
194
+ unknownEdges,
195
+ pointOf: (statement) => pointByStatement.get(statement),
196
+ nodeOf: (statement) => nodeByStatement.get(statement),
197
+ dominates: (first, last) => {
198
+ const firstNode = nodeByStatement.get(first);
199
+ const lastNode = nodeByStatement.get(last);
200
+ return !!firstNode && !!lastNode && nodeDominates(firstNode, lastNode);
201
+ },
202
+ nodeDominates,
203
+ };
204
+ }
205
+ function computeDominators(units, nodes) {
206
+ const result = new Map();
207
+ units.forEach((unit) => {
208
+ const unitNodes = nodes.filter((node) => node.unit === unit);
209
+ const entry = unitNodes.find((node) => node.kind === "entry");
210
+ if (!entry)
211
+ return;
212
+ unitNodes.forEach((node) => result.set(node, new Set(node === entry ? [entry] : unitNodes)));
213
+ let changed = true;
214
+ while (changed) {
215
+ changed = false;
216
+ [...unitNodes].reverse().forEach((node) => {
217
+ if (node === entry)
218
+ return;
219
+ const predecessors = node.predecessors.map((edge) => edge.from);
220
+ let next = new Set();
221
+ if (predecessors.length > 0) {
222
+ next = new Set(result.get(predecessors[0]) ?? []);
223
+ predecessors.slice(1).forEach((predecessor) => {
224
+ const set = result.get(predecessor) ?? new Set();
225
+ [...next].forEach((candidate) => {
226
+ if (!set.has(candidate))
227
+ next.delete(candidate);
228
+ });
229
+ });
230
+ }
231
+ next.add(node);
232
+ const previous = result.get(node) ?? new Set();
233
+ if (previous.size !== next.size ||
234
+ [...previous].some((item) => !next.has(item))) {
235
+ result.set(node, next);
236
+ changed = true;
237
+ }
238
+ });
239
+ }
240
+ });
241
+ return result;
242
+ }
243
+ function childStatementBodies(body) {
244
+ return body.flatMap(childBodiesOfStatement);
245
+ }
246
+ function childBodiesOfStatement(statement) {
247
+ switch (statement.type) {
248
+ case "DoStatement":
249
+ case "WhileStatement":
250
+ case "RepeatStatement":
251
+ case "ForNumericStatement":
252
+ case "ForGenericStatement":
253
+ return [statement.body];
254
+ case "IfStatement":
255
+ return statement.clauses.map((clause) => clause.body);
256
+ case "FunctionDeclaration":
257
+ case "LocalStatement":
258
+ case "AssignmentStatement":
259
+ case "CallStatement":
260
+ case "ReturnStatement":
261
+ case "BreakStatement":
262
+ case "LabelStatement":
263
+ case "GotoStatement":
264
+ return [];
265
+ }
266
+ }