@markw65/monkeyc-optimizer 1.1.14 → 1.1.15
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 +11 -0
- package/build/api.cjs +165 -146
- package/build/optimizer.cjs +154 -142
- package/build/sdk-util.cjs +7915 -7196
- package/build/src/optimizer-types.d.ts +1 -0
- package/build/src/readprg/bytecode.d.ts +7 -0
- package/build/src/readprg/dce.d.ts +1 -1
- package/build/src/readprg/opcodes.d.ts +3 -0
- package/build/src/readprg/sharing.d.ts +2 -0
- package/package.json +1 -1
package/build/optimizer.cjs
CHANGED
|
@@ -15649,33 +15649,6 @@ async function optimizeMonkeyC(fnMap, resourcesMap, manifestXML, config) {
|
|
|
15649
15649
|
return result;
|
|
15650
15650
|
};
|
|
15651
15651
|
const topLocals = () => state.localsStack[state.localsStack.length - 1];
|
|
15652
|
-
/*
|
|
15653
|
-
* Might this function be called from somewhere, including
|
|
15654
|
-
* callbacks from the api (eg getSettingsView, etc).
|
|
15655
|
-
*/
|
|
15656
|
-
const maybeCalled = (func) => {
|
|
15657
|
-
if (!func.body) {
|
|
15658
|
-
// this is an api.mir function. It can be called
|
|
15659
|
-
return true;
|
|
15660
|
-
}
|
|
15661
|
-
if ((0,external_api_cjs_namespaceObject.hasProperty)(state.exposed, func.id.name))
|
|
15662
|
-
return true;
|
|
15663
|
-
if (func.attrs &&
|
|
15664
|
-
func.attrs.attributes &&
|
|
15665
|
-
func.attrs.attributes.elements.some((attr) => {
|
|
15666
|
-
if (attr.type !== "UnaryExpression")
|
|
15667
|
-
return false;
|
|
15668
|
-
if (attr.argument.type !== "Identifier")
|
|
15669
|
-
return false;
|
|
15670
|
-
return attr.argument.name === "test";
|
|
15671
|
-
})) {
|
|
15672
|
-
return true;
|
|
15673
|
-
}
|
|
15674
|
-
if ((0,external_api_cjs_namespaceObject.hasProperty)(state.calledFunctions, func.id.name)) {
|
|
15675
|
-
return (state.calledFunctions[func.id.name].find((f) => f === func) !== null);
|
|
15676
|
-
}
|
|
15677
|
-
return false;
|
|
15678
|
-
};
|
|
15679
15652
|
/*
|
|
15680
15653
|
* Does elm (a class) have a maybeCalled function called name,
|
|
15681
15654
|
* anywhere in its superClass chain.
|
|
@@ -15685,7 +15658,7 @@ async function optimizeMonkeyC(fnMap, resourcesMap, manifestXML, config) {
|
|
|
15685
15658
|
elm.superClass.some((sc) => ((0,external_api_cjs_namespaceObject.hasProperty)(sc.decls, name) &&
|
|
15686
15659
|
sc.decls[name].some((f) => (0,external_api_cjs_namespaceObject.isStateNode)(f) &&
|
|
15687
15660
|
f.type === "FunctionDeclaration" &&
|
|
15688
|
-
maybeCalled(f.node))) ||
|
|
15661
|
+
maybeCalled(state, f.node))) ||
|
|
15689
15662
|
(sc.superClass && checkInherited(sc, name))));
|
|
15690
15663
|
const renamer = (idnode) => {
|
|
15691
15664
|
const ident = idnode.type === "Identifier" ? idnode : idnode.left;
|
|
@@ -15886,7 +15859,7 @@ async function optimizeMonkeyC(fnMap, resourcesMap, manifestXML, config) {
|
|
|
15886
15859
|
}
|
|
15887
15860
|
istate = is;
|
|
15888
15861
|
}
|
|
15889
|
-
if (parent.type === "ClassDeclaration" && !maybeCalled(node)) {
|
|
15862
|
+
if (parent.type === "ClassDeclaration" && !maybeCalled(state, node)) {
|
|
15890
15863
|
let used = false;
|
|
15891
15864
|
if (node.id.name === "initialize") {
|
|
15892
15865
|
used = true;
|
|
@@ -16088,15 +16061,31 @@ async function optimizeMonkeyC(fnMap, resourcesMap, manifestXML, config) {
|
|
|
16088
16061
|
Object.values(fnMap).forEach((f) => {
|
|
16089
16062
|
(0,external_api_cjs_namespaceObject.collectNamespaces)(f.ast, state);
|
|
16090
16063
|
});
|
|
16091
|
-
|
|
16092
|
-
|
|
16093
|
-
|
|
16094
|
-
|
|
16095
|
-
|
|
16096
|
-
|
|
16097
|
-
|
|
16098
|
-
|
|
16099
|
-
|
|
16064
|
+
const cleanupAll = () => Object.values(fnMap).reduce((changes, f) => {
|
|
16065
|
+
traverseAst(f.ast, undefined, (node) => {
|
|
16066
|
+
const ret = cleanup(state, node, f.ast);
|
|
16067
|
+
if (ret === false) {
|
|
16068
|
+
changes = true;
|
|
16069
|
+
state.removeNodeComments(node, f.ast);
|
|
16070
|
+
}
|
|
16071
|
+
else if (ret) {
|
|
16072
|
+
changes = true;
|
|
16073
|
+
}
|
|
16074
|
+
return ret;
|
|
16075
|
+
});
|
|
16076
|
+
return changes;
|
|
16077
|
+
}, false);
|
|
16078
|
+
do {
|
|
16079
|
+
state.usedByName = {};
|
|
16080
|
+
state.calledFunctions = {};
|
|
16081
|
+
state.exposed = state.nextExposed;
|
|
16082
|
+
state.nextExposed = {};
|
|
16083
|
+
Object.values(fnMap).forEach((f) => {
|
|
16084
|
+
(0,external_api_cjs_namespaceObject.collectNamespaces)(f.ast, state);
|
|
16085
|
+
});
|
|
16086
|
+
state.exposed = state.nextExposed;
|
|
16087
|
+
state.nextExposed = {};
|
|
16088
|
+
} while (cleanupAll() && state.config?.iterateOptimizer);
|
|
16100
16089
|
delete state.pre;
|
|
16101
16090
|
delete state.post;
|
|
16102
16091
|
if (state.config?.minimizeModules ?? true) {
|
|
@@ -16105,108 +16094,7 @@ async function optimizeMonkeyC(fnMap, resourcesMap, manifestXML, config) {
|
|
|
16105
16094
|
});
|
|
16106
16095
|
}
|
|
16107
16096
|
Object.values(state.allFunctions).forEach((fns) => fns.forEach((fn) => sizeBasedPRE(state, fn)));
|
|
16108
|
-
|
|
16109
|
-
switch (node.type) {
|
|
16110
|
-
case "ThisExpression":
|
|
16111
|
-
node.text = "self";
|
|
16112
|
-
break;
|
|
16113
|
-
case "EnumStringBody":
|
|
16114
|
-
if (node.members.every((m) => {
|
|
16115
|
-
const name = "name" in m ? m.name : m.id.name;
|
|
16116
|
-
return ((0,external_api_cjs_namespaceObject.hasProperty)(state.index, name) &&
|
|
16117
|
-
!(0,external_api_cjs_namespaceObject.hasProperty)(state.exposed, name) &&
|
|
16118
|
-
!(0,external_api_cjs_namespaceObject.hasProperty)(state.usedByName, name));
|
|
16119
|
-
})) {
|
|
16120
|
-
node.enumType = [
|
|
16121
|
-
...new Set(node.members.map((m) => {
|
|
16122
|
-
if (!("init" in m))
|
|
16123
|
-
return "Number";
|
|
16124
|
-
const [node, type] = getNodeValue(m.init);
|
|
16125
|
-
if (!node) {
|
|
16126
|
-
throw new Error("Failed to get type for eliminated enum");
|
|
16127
|
-
}
|
|
16128
|
-
return type;
|
|
16129
|
-
})),
|
|
16130
|
-
].join(" or ");
|
|
16131
|
-
node.members.splice(0);
|
|
16132
|
-
}
|
|
16133
|
-
break;
|
|
16134
|
-
case "EnumDeclaration":
|
|
16135
|
-
if (!node.body.members.length) {
|
|
16136
|
-
if (!node.id)
|
|
16137
|
-
return false;
|
|
16138
|
-
if (!node.body.enumType) {
|
|
16139
|
-
throw new Error("Missing enumType on optimized enum");
|
|
16140
|
-
}
|
|
16141
|
-
return {
|
|
16142
|
-
type: "TypedefDeclaration",
|
|
16143
|
-
id: node.id,
|
|
16144
|
-
ts: {
|
|
16145
|
-
type: "UnaryExpression",
|
|
16146
|
-
argument: {
|
|
16147
|
-
type: "TypeSpecList",
|
|
16148
|
-
ts: [
|
|
16149
|
-
node.body.enumType,
|
|
16150
|
-
],
|
|
16151
|
-
},
|
|
16152
|
-
prefix: true,
|
|
16153
|
-
operator: " as",
|
|
16154
|
-
},
|
|
16155
|
-
};
|
|
16156
|
-
}
|
|
16157
|
-
break;
|
|
16158
|
-
case "VariableDeclaration": {
|
|
16159
|
-
node.declarations = node.declarations.filter((d) => {
|
|
16160
|
-
const name = (0,external_api_cjs_namespaceObject.variableDeclarationName)(d.id);
|
|
16161
|
-
return (!(0,external_api_cjs_namespaceObject.hasProperty)(state.index, name) ||
|
|
16162
|
-
(0,external_api_cjs_namespaceObject.hasProperty)(state.exposed, name) ||
|
|
16163
|
-
(0,external_api_cjs_namespaceObject.hasProperty)(state.usedByName, name));
|
|
16164
|
-
});
|
|
16165
|
-
if (!node.declarations.length) {
|
|
16166
|
-
return false;
|
|
16167
|
-
}
|
|
16168
|
-
break;
|
|
16169
|
-
}
|
|
16170
|
-
case "ClassElement":
|
|
16171
|
-
if (!node.item) {
|
|
16172
|
-
return false;
|
|
16173
|
-
}
|
|
16174
|
-
break;
|
|
16175
|
-
case "FunctionDeclaration":
|
|
16176
|
-
if (!maybeCalled(node)) {
|
|
16177
|
-
if (node.attrs &&
|
|
16178
|
-
node.attrs.attributes &&
|
|
16179
|
-
node.attrs.attributes.elements.some((attr) => attr.type === "UnaryExpression" && attr.argument.name === "keep")) {
|
|
16180
|
-
break;
|
|
16181
|
-
}
|
|
16182
|
-
return false;
|
|
16183
|
-
}
|
|
16184
|
-
break;
|
|
16185
|
-
case "ClassDeclaration":
|
|
16186
|
-
case "ModuleDeclaration":
|
|
16187
|
-
// none of the attributes means anything on classes and
|
|
16188
|
-
// modules, and the new compiler complains about some
|
|
16189
|
-
// of them. Just drop them all.
|
|
16190
|
-
if (node.attrs && node.attrs.access) {
|
|
16191
|
-
if (node.attrs.attributes) {
|
|
16192
|
-
delete node.attrs.access;
|
|
16193
|
-
}
|
|
16194
|
-
else {
|
|
16195
|
-
delete node.attrs;
|
|
16196
|
-
}
|
|
16197
|
-
}
|
|
16198
|
-
}
|
|
16199
|
-
return null;
|
|
16200
|
-
};
|
|
16201
|
-
Object.entries(fnMap).forEach(([, f]) => {
|
|
16202
|
-
traverseAst(f.ast, undefined, (node) => {
|
|
16203
|
-
const ret = cleanup(node);
|
|
16204
|
-
if (ret === false) {
|
|
16205
|
-
state.removeNodeComments(node, f.ast);
|
|
16206
|
-
}
|
|
16207
|
-
return ret;
|
|
16208
|
-
});
|
|
16209
|
-
});
|
|
16097
|
+
cleanupAll();
|
|
16210
16098
|
config.checkCompilerLookupRules = checkLookupRules;
|
|
16211
16099
|
reportMissingSymbols(state, config);
|
|
16212
16100
|
if (state.inlineDiagnostics) {
|
|
@@ -16232,6 +16120,129 @@ async function optimizeMonkeyC(fnMap, resourcesMap, manifestXML, config) {
|
|
|
16232
16120
|
});
|
|
16233
16121
|
return state.diagnostics;
|
|
16234
16122
|
}
|
|
16123
|
+
/*
|
|
16124
|
+
* Might this function be called from somewhere, including
|
|
16125
|
+
* callbacks from the api (eg getSettingsView, etc).
|
|
16126
|
+
*/
|
|
16127
|
+
function maybeCalled(state, func) {
|
|
16128
|
+
if (!func.body) {
|
|
16129
|
+
// this is an api.mir function. It can be called
|
|
16130
|
+
return true;
|
|
16131
|
+
}
|
|
16132
|
+
if ((0,external_api_cjs_namespaceObject.hasProperty)(state.exposed, func.id.name))
|
|
16133
|
+
return true;
|
|
16134
|
+
if (func.attrs &&
|
|
16135
|
+
func.attrs.attributes &&
|
|
16136
|
+
func.attrs.attributes.elements.some((attr) => {
|
|
16137
|
+
if (attr.type !== "UnaryExpression")
|
|
16138
|
+
return false;
|
|
16139
|
+
if (attr.argument.type !== "Identifier")
|
|
16140
|
+
return false;
|
|
16141
|
+
return attr.argument.name === "test";
|
|
16142
|
+
})) {
|
|
16143
|
+
return true;
|
|
16144
|
+
}
|
|
16145
|
+
if ((0,external_api_cjs_namespaceObject.hasProperty)(state.calledFunctions, func.id.name)) {
|
|
16146
|
+
return state.calledFunctions[func.id.name].find((f) => f === func) !== null;
|
|
16147
|
+
}
|
|
16148
|
+
return false;
|
|
16149
|
+
}
|
|
16150
|
+
function cleanup(state, node, ast) {
|
|
16151
|
+
switch (node.type) {
|
|
16152
|
+
case "ThisExpression":
|
|
16153
|
+
node.text = "self";
|
|
16154
|
+
break;
|
|
16155
|
+
case "EnumStringBody":
|
|
16156
|
+
if (node.members.every((m) => {
|
|
16157
|
+
const name = "name" in m ? m.name : m.id.name;
|
|
16158
|
+
return ((0,external_api_cjs_namespaceObject.hasProperty)(state.index, name) &&
|
|
16159
|
+
!(0,external_api_cjs_namespaceObject.hasProperty)(state.exposed, name) &&
|
|
16160
|
+
!(0,external_api_cjs_namespaceObject.hasProperty)(state.usedByName, name));
|
|
16161
|
+
})) {
|
|
16162
|
+
node.enumType = [
|
|
16163
|
+
...new Set(node.members.map((m) => {
|
|
16164
|
+
if (!("init" in m))
|
|
16165
|
+
return "Number";
|
|
16166
|
+
const [node, type] = getNodeValue(m.init);
|
|
16167
|
+
if (!node) {
|
|
16168
|
+
throw new Error("Failed to get type for eliminated enum");
|
|
16169
|
+
}
|
|
16170
|
+
return type;
|
|
16171
|
+
})),
|
|
16172
|
+
].join(" or ");
|
|
16173
|
+
node.members.splice(0);
|
|
16174
|
+
}
|
|
16175
|
+
break;
|
|
16176
|
+
case "EnumDeclaration":
|
|
16177
|
+
if (!node.body.members.length) {
|
|
16178
|
+
if (!node.id)
|
|
16179
|
+
return false;
|
|
16180
|
+
if (!node.body.enumType) {
|
|
16181
|
+
throw new Error("Missing enumType on optimized enum");
|
|
16182
|
+
}
|
|
16183
|
+
state.removeNodeComments(node, ast);
|
|
16184
|
+
return withLocDeep({
|
|
16185
|
+
type: "TypedefDeclaration",
|
|
16186
|
+
id: node.id,
|
|
16187
|
+
ts: {
|
|
16188
|
+
type: "UnaryExpression",
|
|
16189
|
+
argument: {
|
|
16190
|
+
type: "TypeSpecList",
|
|
16191
|
+
ts: [
|
|
16192
|
+
node.body.enumType,
|
|
16193
|
+
],
|
|
16194
|
+
},
|
|
16195
|
+
prefix: true,
|
|
16196
|
+
operator: " as",
|
|
16197
|
+
},
|
|
16198
|
+
}, node, node);
|
|
16199
|
+
}
|
|
16200
|
+
break;
|
|
16201
|
+
case "VariableDeclarator": {
|
|
16202
|
+
const name = (0,external_api_cjs_namespaceObject.variableDeclarationName)(node.id);
|
|
16203
|
+
return !(0,external_api_cjs_namespaceObject.hasProperty)(state.index, name) ||
|
|
16204
|
+
(0,external_api_cjs_namespaceObject.hasProperty)(state.exposed, name) ||
|
|
16205
|
+
(0,external_api_cjs_namespaceObject.hasProperty)(state.usedByName, name)
|
|
16206
|
+
? null
|
|
16207
|
+
: false;
|
|
16208
|
+
}
|
|
16209
|
+
case "VariableDeclaration": {
|
|
16210
|
+
if (!node.declarations.length) {
|
|
16211
|
+
return false;
|
|
16212
|
+
}
|
|
16213
|
+
break;
|
|
16214
|
+
}
|
|
16215
|
+
case "ClassElement":
|
|
16216
|
+
if (!node.item) {
|
|
16217
|
+
return false;
|
|
16218
|
+
}
|
|
16219
|
+
break;
|
|
16220
|
+
case "FunctionDeclaration":
|
|
16221
|
+
if (!maybeCalled(state, node)) {
|
|
16222
|
+
if (node.attrs &&
|
|
16223
|
+
node.attrs.attributes &&
|
|
16224
|
+
node.attrs.attributes.elements.some((attr) => attr.type === "UnaryExpression" && attr.argument.name === "keep")) {
|
|
16225
|
+
break;
|
|
16226
|
+
}
|
|
16227
|
+
return false;
|
|
16228
|
+
}
|
|
16229
|
+
break;
|
|
16230
|
+
case "ClassDeclaration":
|
|
16231
|
+
case "ModuleDeclaration":
|
|
16232
|
+
// none of the attributes means anything on classes and
|
|
16233
|
+
// modules, and the new compiler complains about some
|
|
16234
|
+
// of them. Just drop them all.
|
|
16235
|
+
if (node.attrs && node.attrs.access) {
|
|
16236
|
+
if (node.attrs.attributes) {
|
|
16237
|
+
delete node.attrs.access;
|
|
16238
|
+
}
|
|
16239
|
+
else {
|
|
16240
|
+
delete node.attrs;
|
|
16241
|
+
}
|
|
16242
|
+
}
|
|
16243
|
+
}
|
|
16244
|
+
return null;
|
|
16245
|
+
}
|
|
16235
16246
|
function optimizeCall(istate, node, context) {
|
|
16236
16247
|
const state = istate.state;
|
|
16237
16248
|
const [name, results] = state.lookupNonlocal(node.callee);
|
|
@@ -16753,6 +16764,7 @@ const configOptionsToCheck = [
|
|
|
16753
16764
|
"extensionVersion",
|
|
16754
16765
|
"typeCheckLevel",
|
|
16755
16766
|
"covarianceWarnings",
|
|
16767
|
+
"iterateOptimizer",
|
|
16756
16768
|
];
|
|
16757
16769
|
/**
|
|
16758
16770
|
* @param {BuildConfig} config
|
|
@@ -16811,7 +16823,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
|
|
|
16811
16823
|
// the oldest optimized file, we don't need to regenerate
|
|
16812
16824
|
const source_time = await (0,external_util_cjs_namespaceObject.last_modified)(Object.keys(fnMap).concat(dependencyFiles));
|
|
16813
16825
|
const opt_time = await (0,external_util_cjs_namespaceObject.first_modified)(Object.values(fnMap).map((v) => v.output));
|
|
16814
|
-
if (source_time < opt_time &&
|
|
16826
|
+
if (source_time < opt_time && 1677202119524 < opt_time) {
|
|
16815
16827
|
return { hasTests, diagnostics: prevDiagnostics };
|
|
16816
16828
|
}
|
|
16817
16829
|
}
|
|
@@ -16838,7 +16850,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
|
|
|
16838
16850
|
return promises_namespaceObject.writeFile(external_path_.join(output, "build-info.json"), JSON.stringify({
|
|
16839
16851
|
hasTests,
|
|
16840
16852
|
diagnostics,
|
|
16841
|
-
optimizerVersion: "1.1.
|
|
16853
|
+
optimizerVersion: "1.1.15",
|
|
16842
16854
|
...Object.fromEntries(configOptionsToCheck.map((option) => [option, config[option]])),
|
|
16843
16855
|
}))
|
|
16844
16856
|
.then(() => ({ hasTests, diagnostics }));
|