@markw65/monkeyc-optimizer 1.1.26 → 1.1.28
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/README.md +21 -1
- package/bin/cft-font-info.js +63 -0
- package/build/api.cjs +36 -34
- package/build/cftinfo.cjs +112 -0
- package/build/chunk-ABYVSU2C.cjs +74 -0
- package/build/chunk-MBTLUWXR.cjs +375 -0
- package/build/{chunk-YZ44IUV6.cjs → chunk-PVBIKZYZ.cjs} +105 -153
- package/build/{chunk-XO53WBWQ.cjs → chunk-ZI7CGRJC.cjs} +630 -902
- package/build/optimizer.cjs +21 -19
- package/build/sdk-util.cjs +17 -15
- package/build/src/cftinfo.d.ts +32 -0
- package/build/src/mc-rewrite.d.ts +4 -1
- package/build/src/optimizer.d.ts +1 -0
- package/build/src/projects.d.ts +2 -1
- package/build/src/type-flow/interp.d.ts +1 -0
- package/build/src/worker-task.d.ts +1 -0
- package/build/util.cjs +25 -24
- package/build/worker-thread.cjs +7 -5
- package/package.json +6 -2
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var chunk_MBTLUWXR_exports = {};
|
|
20
|
+
__export(chunk_MBTLUWXR_exports, {
|
|
21
|
+
adjustLoc: () => adjustLoc,
|
|
22
|
+
cloneDeep: () => cloneDeep,
|
|
23
|
+
getLiteralNode: () => getLiteralNode,
|
|
24
|
+
getNodeValue: () => getNodeValue,
|
|
25
|
+
hasProperty: () => hasProperty,
|
|
26
|
+
init_ast: () => init_ast,
|
|
27
|
+
isExpression: () => isExpression,
|
|
28
|
+
isStatement: () => isStatement,
|
|
29
|
+
locRange: () => locRange,
|
|
30
|
+
makeIdentifier: () => makeIdentifier,
|
|
31
|
+
makeMemberExpression: () => makeMemberExpression,
|
|
32
|
+
makeScopedName: () => makeScopedName,
|
|
33
|
+
mayThrow: () => mayThrow,
|
|
34
|
+
traverseAst: () => traverseAst,
|
|
35
|
+
withLoc: () => withLoc,
|
|
36
|
+
withLocDeep: () => withLocDeep,
|
|
37
|
+
wrap: () => wrap
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(chunk_MBTLUWXR_exports);
|
|
40
|
+
var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
|
|
41
|
+
var import_prettier_plugin_monkeyc = require("@markw65/prettier-plugin-monkeyc");
|
|
42
|
+
function isMCTreeNode(node) {
|
|
43
|
+
return node ? typeof node === "object" && "type" in node : false;
|
|
44
|
+
}
|
|
45
|
+
function traverseAst(node, pre, post, parent) {
|
|
46
|
+
const nodes = pre && pre(node);
|
|
47
|
+
if (nodes === false)
|
|
48
|
+
return;
|
|
49
|
+
if (!mctreeTypeInfo[node.type]) {
|
|
50
|
+
throw new Error("what?");
|
|
51
|
+
}
|
|
52
|
+
for (const key of nodes || mctreeTypeInfo[node.type].keys) {
|
|
53
|
+
const value = node[key];
|
|
54
|
+
if (!value)
|
|
55
|
+
continue;
|
|
56
|
+
if (Array.isArray(value)) {
|
|
57
|
+
const values = value;
|
|
58
|
+
const deletions = values.reduce(
|
|
59
|
+
(state, obj, i) => {
|
|
60
|
+
if (isMCTreeNode(obj)) {
|
|
61
|
+
const repl = traverseAst(obj, pre, post, node);
|
|
62
|
+
if (repl === false) {
|
|
63
|
+
if (!state)
|
|
64
|
+
state = {};
|
|
65
|
+
state[i] = true;
|
|
66
|
+
} else if (repl != null) {
|
|
67
|
+
if (!state)
|
|
68
|
+
state = {};
|
|
69
|
+
values[i] = repl;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return state;
|
|
73
|
+
},
|
|
74
|
+
null
|
|
75
|
+
);
|
|
76
|
+
if (deletions) {
|
|
77
|
+
values.splice(
|
|
78
|
+
0,
|
|
79
|
+
values.length,
|
|
80
|
+
...values.filter((obj, i) => deletions[i] !== true).flat(1)
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
} else if (isMCTreeNode(value)) {
|
|
84
|
+
let repl = traverseAst(value, pre, post, node);
|
|
85
|
+
if (repl === false) {
|
|
86
|
+
delete node[key];
|
|
87
|
+
} else if (repl != null) {
|
|
88
|
+
if (Array.isArray(repl)) {
|
|
89
|
+
if (isStatement(value) && repl.every((s) => isStatement(s))) {
|
|
90
|
+
repl = withLoc(
|
|
91
|
+
{
|
|
92
|
+
type: "BlockStatement",
|
|
93
|
+
body: repl
|
|
94
|
+
},
|
|
95
|
+
repl[0],
|
|
96
|
+
repl[repl.length - 1]
|
|
97
|
+
);
|
|
98
|
+
} else {
|
|
99
|
+
throw new Error("Array returned by traverseAst in Node context");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
node[key] = repl;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return post && post(node, parent);
|
|
107
|
+
}
|
|
108
|
+
function isStatement(node) {
|
|
109
|
+
return hasProperty(mctreeTypeInfo[node.type], "stmt");
|
|
110
|
+
}
|
|
111
|
+
function isExpression(node) {
|
|
112
|
+
return hasProperty(mctreeTypeInfo[node.type], "expr");
|
|
113
|
+
}
|
|
114
|
+
function mayThrow(node) {
|
|
115
|
+
switch (node.type) {
|
|
116
|
+
case "BinaryExpression":
|
|
117
|
+
case "CallExpression":
|
|
118
|
+
case "NewExpression":
|
|
119
|
+
case "ThrowStatement":
|
|
120
|
+
case "UnaryExpression":
|
|
121
|
+
case "UpdateExpression":
|
|
122
|
+
return true;
|
|
123
|
+
default:
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function hasProperty(obj, prop) {
|
|
128
|
+
return obj ? Object.prototype.hasOwnProperty.call(obj, prop) : false;
|
|
129
|
+
}
|
|
130
|
+
function withLoc(node, start, end) {
|
|
131
|
+
if (start && start.loc) {
|
|
132
|
+
node.start = start.start;
|
|
133
|
+
if (!node.end)
|
|
134
|
+
node.end = start.end;
|
|
135
|
+
node.loc = { ...node.loc || start.loc, start: start.loc.start };
|
|
136
|
+
if (end === start && start.origins) {
|
|
137
|
+
node.origins = start.origins;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (end === false) {
|
|
141
|
+
if (node.loc) {
|
|
142
|
+
node.loc.end = node.loc.start;
|
|
143
|
+
node.end = node.start;
|
|
144
|
+
}
|
|
145
|
+
} else if (end && end.loc) {
|
|
146
|
+
node.end = end.end;
|
|
147
|
+
node.loc = { ...node.loc || end.loc, end: end.loc.end };
|
|
148
|
+
}
|
|
149
|
+
return node;
|
|
150
|
+
}
|
|
151
|
+
function withLocDeep(node, start, end, inplace) {
|
|
152
|
+
node = withLoc(inplace ? node : { ...node }, start, end);
|
|
153
|
+
if (!inplace && node.origins) {
|
|
154
|
+
node.origins = [...node.origins];
|
|
155
|
+
}
|
|
156
|
+
for (const key of mctreeTypeInfo[node.type].keys) {
|
|
157
|
+
const value = node[key];
|
|
158
|
+
if (!value)
|
|
159
|
+
continue;
|
|
160
|
+
const fix = (v) => isMCTreeNode(v) ? withLocDeep(v, start, end, inplace) : v;
|
|
161
|
+
const repl = Array.isArray(value) ? value.map(fix) : fix(value);
|
|
162
|
+
inplace || (node[key] = repl);
|
|
163
|
+
}
|
|
164
|
+
return node;
|
|
165
|
+
}
|
|
166
|
+
function cloneDeep(node) {
|
|
167
|
+
return withLocDeep(node, null);
|
|
168
|
+
}
|
|
169
|
+
function getNodeValue(node) {
|
|
170
|
+
if (node.type === "BinaryExpression" && node.operator === "as" && node.right.type === "TypeSpecList" && node.right.ts.length === 1 && typeof node.right.ts[0] === "string") {
|
|
171
|
+
return getNodeValue(node.left);
|
|
172
|
+
}
|
|
173
|
+
if (node.type !== "Literal") {
|
|
174
|
+
return [null, null];
|
|
175
|
+
}
|
|
176
|
+
if (node.value === null) {
|
|
177
|
+
return [node, "Null"];
|
|
178
|
+
}
|
|
179
|
+
const type = typeof node.value;
|
|
180
|
+
if (type === "number") {
|
|
181
|
+
const match = import_prettier_plugin_monkeyc.LiteralIntegerRe.exec(node.raw);
|
|
182
|
+
if (match) {
|
|
183
|
+
return match[2] === "l" || match[2] === "L" ? [node, "Long"] : [node, "Number"];
|
|
184
|
+
}
|
|
185
|
+
return [node, node.raw.endsWith("d") ? "Double" : "Float"];
|
|
186
|
+
}
|
|
187
|
+
if (type === "bigint") {
|
|
188
|
+
return [node, "Long"];
|
|
189
|
+
}
|
|
190
|
+
if (type === "string") {
|
|
191
|
+
return node.raw.startsWith("'") ? [node, "Char"] : [node, "String"];
|
|
192
|
+
}
|
|
193
|
+
if (type === "boolean") {
|
|
194
|
+
return [node, "Boolean"];
|
|
195
|
+
}
|
|
196
|
+
throw new Error(`Literal has unknown type '${type}'`);
|
|
197
|
+
}
|
|
198
|
+
function wrap(node, loc) {
|
|
199
|
+
if (loc) {
|
|
200
|
+
node.loc = loc;
|
|
201
|
+
node.start = loc.start.offset;
|
|
202
|
+
node.end = loc.end.offset;
|
|
203
|
+
}
|
|
204
|
+
return node;
|
|
205
|
+
}
|
|
206
|
+
function locRange(start, end) {
|
|
207
|
+
return {
|
|
208
|
+
source: start.source || end.source,
|
|
209
|
+
start: start.start,
|
|
210
|
+
end: end.end
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function adjustLoc(loc, start = 1, end = -1) {
|
|
214
|
+
return {
|
|
215
|
+
source: loc.source,
|
|
216
|
+
start: {
|
|
217
|
+
offset: loc.start.offset + start,
|
|
218
|
+
line: loc.start.line,
|
|
219
|
+
column: loc.start.column + start
|
|
220
|
+
},
|
|
221
|
+
end: {
|
|
222
|
+
offset: loc.end.offset + end,
|
|
223
|
+
line: loc.end.line,
|
|
224
|
+
column: loc.end.column + end
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function makeIdentifier(name, loc) {
|
|
229
|
+
return wrap({ type: "Identifier", name }, loc);
|
|
230
|
+
}
|
|
231
|
+
function makeMemberExpression(object, property) {
|
|
232
|
+
return wrap(
|
|
233
|
+
{
|
|
234
|
+
type: "MemberExpression",
|
|
235
|
+
object,
|
|
236
|
+
property,
|
|
237
|
+
computed: false
|
|
238
|
+
},
|
|
239
|
+
object.loc && locRange(object.loc, property.loc)
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
function makeScopedName(dotted, l) {
|
|
243
|
+
const loc = l && adjustLoc(l, 0, l.start.offset - l.end.offset);
|
|
244
|
+
const result = dotted.split(/\s*\.\s*/).reduce(
|
|
245
|
+
({ cur, offset }, next) => {
|
|
246
|
+
const id = makeIdentifier(
|
|
247
|
+
next,
|
|
248
|
+
loc && adjustLoc(loc, offset, offset + next.length)
|
|
249
|
+
);
|
|
250
|
+
if (!cur) {
|
|
251
|
+
cur = id;
|
|
252
|
+
} else {
|
|
253
|
+
cur = makeMemberExpression(cur, id);
|
|
254
|
+
}
|
|
255
|
+
offset += next.length + 1;
|
|
256
|
+
return { cur, offset };
|
|
257
|
+
},
|
|
258
|
+
{ cur: null, offset: 0 }
|
|
259
|
+
).cur;
|
|
260
|
+
if (!result)
|
|
261
|
+
throw new Error("Failed to make a ScopedName");
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
function getLiteralNode(node) {
|
|
265
|
+
if (node == null)
|
|
266
|
+
return null;
|
|
267
|
+
if (node.type === "Literal")
|
|
268
|
+
return node;
|
|
269
|
+
if (node.type === "BinaryExpression" && node.operator === "as") {
|
|
270
|
+
return getLiteralNode(node.left) && node;
|
|
271
|
+
}
|
|
272
|
+
if (node.type === "UnaryExpression") {
|
|
273
|
+
if (node.argument.type !== "Literal")
|
|
274
|
+
return null;
|
|
275
|
+
switch (node.operator) {
|
|
276
|
+
case "-": {
|
|
277
|
+
const [arg, type] = getNodeValue(node.argument);
|
|
278
|
+
if (type === "Number" || type === "Long") {
|
|
279
|
+
return { ...arg, value: -arg.value, raw: `-${arg.raw}` };
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
var mctreeTypeInfo;
|
|
287
|
+
var init_ast = (0, import_chunk_ABYVSU2C.__esm)({
|
|
288
|
+
"src/ast.ts"() {
|
|
289
|
+
"use strict";
|
|
290
|
+
mctreeTypeInfo = {
|
|
291
|
+
ArrayExpression: { keys: ["elements"], expr: true },
|
|
292
|
+
AssignmentExpression: { keys: ["left", "right"], expr: true },
|
|
293
|
+
AttributeList: { keys: ["attributes"] },
|
|
294
|
+
Attributes: { keys: ["elements"] },
|
|
295
|
+
BinaryExpression: { keys: ["left", "right"], expr: true },
|
|
296
|
+
Block: { keys: [] },
|
|
297
|
+
BlockStatement: { keys: ["body", "innerComments"], stmt: true },
|
|
298
|
+
BreakStatement: { keys: [], stmt: true },
|
|
299
|
+
CallExpression: { keys: ["callee", "arguments"], expr: true },
|
|
300
|
+
CatchClause: { keys: ["param", "body"] },
|
|
301
|
+
CatchClauses: { keys: ["catches"] },
|
|
302
|
+
ClassBody: { keys: ["body"] },
|
|
303
|
+
ClassDeclaration: { keys: ["attrs", "id", "superClass", "body"], stmt: true },
|
|
304
|
+
ClassElement: { keys: ["item"] },
|
|
305
|
+
ConditionalExpression: {
|
|
306
|
+
keys: ["test", "consequent", "alternate"],
|
|
307
|
+
expr: true
|
|
308
|
+
},
|
|
309
|
+
ContinueStatement: { keys: [], stmt: true },
|
|
310
|
+
DoWhileStatement: { keys: ["body", "test"], stmt: true },
|
|
311
|
+
EnumDeclaration: { keys: ["attrs", "id", "body"], stmt: true },
|
|
312
|
+
EnumStringBody: { keys: ["members"] },
|
|
313
|
+
EnumStringMember: { keys: ["id", "init"] },
|
|
314
|
+
ExpressionStatement: { keys: ["expression"], stmt: true },
|
|
315
|
+
ForStatement: { keys: ["init", "test", "body", "update"], stmt: true },
|
|
316
|
+
FunctionDeclaration: {
|
|
317
|
+
keys: ["attrs", "id", "params", "returnType", "body"],
|
|
318
|
+
stmt: true
|
|
319
|
+
},
|
|
320
|
+
Identifier: { keys: [], expr: true },
|
|
321
|
+
IfStatement: { keys: ["test", "consequent", "alternate"], stmt: true },
|
|
322
|
+
ImportModule: { keys: ["id"] },
|
|
323
|
+
InstanceOfCase: { keys: ["id"] },
|
|
324
|
+
Line: { keys: [] },
|
|
325
|
+
Literal: { keys: [], expr: true },
|
|
326
|
+
LogicalExpression: { keys: ["left", "right"], expr: true },
|
|
327
|
+
MemberExpression: { keys: ["object", "property"], expr: true },
|
|
328
|
+
MethodDefinition: { keys: ["params", "returnType"] },
|
|
329
|
+
ModuleDeclaration: { keys: ["attrs", "id", "body"], stmt: true },
|
|
330
|
+
MultiLine: { keys: [] },
|
|
331
|
+
NewExpression: { keys: ["callee", "arguments"], expr: true },
|
|
332
|
+
ObjectExpression: { keys: ["properties"], expr: true },
|
|
333
|
+
ParenthesizedExpression: { keys: ["expression"], expr: true },
|
|
334
|
+
Program: { keys: ["body", "comments"] },
|
|
335
|
+
Property: { keys: ["key", "value"] },
|
|
336
|
+
ReturnStatement: { keys: ["argument"], stmt: true },
|
|
337
|
+
SequenceExpression: { keys: ["expressions"], expr: true },
|
|
338
|
+
SizedArrayExpression: { keys: ["size", "ts"], expr: true },
|
|
339
|
+
SwitchCase: { keys: ["test", "consequent"] },
|
|
340
|
+
SwitchStatement: { keys: ["discriminant", "cases"], stmt: true },
|
|
341
|
+
ThisExpression: { keys: [], expr: true },
|
|
342
|
+
ThrowStatement: { keys: ["argument"], stmt: true },
|
|
343
|
+
TryStatement: { keys: ["block", "handler", "finalizer"], stmt: true },
|
|
344
|
+
TypedefDeclaration: { keys: ["attrs", "id", "ts"], stmt: true },
|
|
345
|
+
TypeSpecList: { keys: ["ts"] },
|
|
346
|
+
TypeSpecPart: { keys: ["name", "body", "callspec", "generics"] },
|
|
347
|
+
UnaryExpression: { keys: ["argument"], expr: true },
|
|
348
|
+
UpdateExpression: { keys: ["argument"], expr: true },
|
|
349
|
+
Using: { keys: ["id", "as"] },
|
|
350
|
+
VariableDeclaration: { keys: ["attrs", "declarations"], stmt: true },
|
|
351
|
+
VariableDeclarator: { keys: ["id", "init"] },
|
|
352
|
+
WhileStatement: { keys: ["test", "body"], stmt: true }
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
357
|
+
0 && (module.exports = {
|
|
358
|
+
adjustLoc,
|
|
359
|
+
cloneDeep,
|
|
360
|
+
getLiteralNode,
|
|
361
|
+
getNodeValue,
|
|
362
|
+
hasProperty,
|
|
363
|
+
init_ast,
|
|
364
|
+
isExpression,
|
|
365
|
+
isStatement,
|
|
366
|
+
locRange,
|
|
367
|
+
makeIdentifier,
|
|
368
|
+
makeMemberExpression,
|
|
369
|
+
makeScopedName,
|
|
370
|
+
mayThrow,
|
|
371
|
+
traverseAst,
|
|
372
|
+
withLoc,
|
|
373
|
+
withLocDeep,
|
|
374
|
+
wrap
|
|
375
|
+
});
|