@markw65/monkeyc-optimizer 1.0.19 → 1.0.20

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 CHANGED
@@ -220,3 +220,18 @@ More fixes found via open source projects.
220
220
  - Code cleanup
221
221
  - Properly type the results of JSON.parse
222
222
  - Switch over to using ParenthesizedExpression for formatAst (depends on @markw65/prettier-plugin-monkeyc@1.0.22)
223
+
224
+ ### 1.0.20
225
+
226
+ - Bug fixes
227
+
228
+ - Fix a bug marking unknown callees
229
+ - Fix a bug in test.js that didn't notice when tests failed, and fix a failing test
230
+
231
+ - Optimizer enhancements
232
+
233
+ - Re-run the main optimization step, to properly account for functions that are unused after optimization
234
+ - Call the optimizer on 'unused' nodes before returning them
235
+
236
+ - Code cleanup
237
+ - Called function cleanup
package/build/api.cjs CHANGED
@@ -1086,6 +1086,13 @@ function evaluateFunction(func, args) {
1086
1086
  return false;
1087
1087
  }
1088
1088
  }
1089
+ function markFunctionCalled(state, func) {
1090
+ if (!hasProperty(state.calledFunctions, func.id.name)) {
1091
+ state.calledFunctions[func.id.name] = [func];
1092
+ return;
1093
+ }
1094
+ pushUnique(state.calledFunctions[func.id.name], func);
1095
+ }
1089
1096
  async function optimizeMonkeyC(fnMap) {
1090
1097
  const state = {
1091
1098
  ...(await analyze(fnMap)),
@@ -1295,10 +1302,7 @@ async function optimizeMonkeyC(fnMap) {
1295
1302
  used = checkInherited(parent, node.id.name);
1296
1303
  }
1297
1304
  if (used) {
1298
- if (!hasProperty(state.calledFunctions, node.id.name)) {
1299
- state.calledFunctions[node.id.name] = [];
1300
- }
1301
- state.calledFunctions[node.id.name].push(node);
1305
+ markFunctionCalled(state, node);
1302
1306
  }
1303
1307
  }
1304
1308
  }
@@ -1383,7 +1387,13 @@ async function optimizeMonkeyC(fnMap) {
1383
1387
  else {
1384
1388
  const ret = unused(node.expression, true);
1385
1389
  if (ret) {
1386
- return ret;
1390
+ return ret
1391
+ .map((s) => {
1392
+ const r2 = state.traverse(s);
1393
+ return r2 === false || r2 ? r2 : s;
1394
+ })
1395
+ .flat(1)
1396
+ .filter((s) => s !== false);
1387
1397
  }
1388
1398
  }
1389
1399
  break;
@@ -1393,6 +1403,11 @@ async function optimizeMonkeyC(fnMap) {
1393
1403
  Object.values(fnMap).forEach((f) => {
1394
1404
  collectNamespaces(f.ast, state);
1395
1405
  });
1406
+ state.calledFunctions = {};
1407
+ state.exposed = {};
1408
+ Object.values(fnMap).forEach((f) => {
1409
+ collectNamespaces(f.ast, state);
1410
+ });
1396
1411
  delete state.pre;
1397
1412
  delete state.post;
1398
1413
  const cleanup = (node) => {
@@ -1471,7 +1486,9 @@ async function optimizeMonkeyC(fnMap) {
1471
1486
  return state.diagnostics;
1472
1487
  }
1473
1488
  function optimizeCall(state, node, context) {
1474
- const [name, callees] = state.lookup(node.callee);
1489
+ const [name, results] = state.lookup(node.callee);
1490
+ const callees = results &&
1491
+ results.filter((c) => c.type === "FunctionDeclaration");
1475
1492
  if (!callees || !callees.length) {
1476
1493
  const n = name ||
1477
1494
  ("name" in node.callee && node.callee.name) ||
@@ -1506,10 +1523,7 @@ function optimizeCall(state, node, context) {
1506
1523
  }
1507
1524
  }
1508
1525
  }
1509
- if (!hasProperty(state.calledFunctions, name)) {
1510
- state.calledFunctions[name] = [];
1511
- }
1512
- callees.forEach((c) => isStateNode(c) && pushUnique(state.calledFunctions[name], c.node));
1526
+ callees.forEach((c) => markFunctionCalled(state, c.node));
1513
1527
  return null;
1514
1528
  }
1515
1529
 
@@ -10784,11 +10784,11 @@ function launchSimulator() {
10784
10784
  child.unref();
10785
10785
  });
10786
10786
  }
10787
- function simulateProgram(prg, device, test) {
10787
+ function simulateProgram(prg, device, test = false, logger) {
10788
10788
  const args = [prg, device];
10789
10789
  if (test)
10790
10790
  args.push("-t");
10791
- return (0,external_sdk_util_cjs_namespaceObject.getSdkPath)().then((sdk) => (0,external_util_cjs_namespaceObject.spawnByLine)(external_path_.resolve(sdk, "bin", "monkeydo"), args, (line) => console.log(line)).then(() => { }));
10791
+ return (0,external_sdk_util_cjs_namespaceObject.getSdkPath)().then((sdk) => (0,external_util_cjs_namespaceObject.spawnByLine)(external_path_.resolve(sdk, "bin", "monkeydo"), args, logger || ((line) => console.log(line))).then(() => { }));
10792
10792
  }
10793
10793
 
10794
10794
  ;// CONCATENATED MODULE: ./src/variable-renamer.ts
@@ -11804,6 +11804,13 @@ function evaluateFunction(func, args) {
11804
11804
  return false;
11805
11805
  }
11806
11806
  }
11807
+ function markFunctionCalled(state, func) {
11808
+ if (!(0,external_api_cjs_namespaceObject.hasProperty)(state.calledFunctions, func.id.name)) {
11809
+ state.calledFunctions[func.id.name] = [func];
11810
+ return;
11811
+ }
11812
+ (0,external_util_cjs_namespaceObject.pushUnique)(state.calledFunctions[func.id.name], func);
11813
+ }
11807
11814
  async function optimizeMonkeyC(fnMap) {
11808
11815
  const state = {
11809
11816
  ...(await analyze(fnMap)),
@@ -12013,10 +12020,7 @@ async function optimizeMonkeyC(fnMap) {
12013
12020
  used = checkInherited(parent, node.id.name);
12014
12021
  }
12015
12022
  if (used) {
12016
- if (!(0,external_api_cjs_namespaceObject.hasProperty)(state.calledFunctions, node.id.name)) {
12017
- state.calledFunctions[node.id.name] = [];
12018
- }
12019
- state.calledFunctions[node.id.name].push(node);
12023
+ markFunctionCalled(state, node);
12020
12024
  }
12021
12025
  }
12022
12026
  }
@@ -12101,7 +12105,13 @@ async function optimizeMonkeyC(fnMap) {
12101
12105
  else {
12102
12106
  const ret = unused(node.expression, true);
12103
12107
  if (ret) {
12104
- return ret;
12108
+ return ret
12109
+ .map((s) => {
12110
+ const r2 = state.traverse(s);
12111
+ return r2 === false || r2 ? r2 : s;
12112
+ })
12113
+ .flat(1)
12114
+ .filter((s) => s !== false);
12105
12115
  }
12106
12116
  }
12107
12117
  break;
@@ -12111,6 +12121,11 @@ async function optimizeMonkeyC(fnMap) {
12111
12121
  Object.values(fnMap).forEach((f) => {
12112
12122
  (0,external_api_cjs_namespaceObject.collectNamespaces)(f.ast, state);
12113
12123
  });
12124
+ state.calledFunctions = {};
12125
+ state.exposed = {};
12126
+ Object.values(fnMap).forEach((f) => {
12127
+ (0,external_api_cjs_namespaceObject.collectNamespaces)(f.ast, state);
12128
+ });
12114
12129
  delete state.pre;
12115
12130
  delete state.post;
12116
12131
  const cleanup = (node) => {
@@ -12189,7 +12204,9 @@ async function optimizeMonkeyC(fnMap) {
12189
12204
  return state.diagnostics;
12190
12205
  }
12191
12206
  function optimizeCall(state, node, context) {
12192
- const [name, callees] = state.lookup(node.callee);
12207
+ const [name, results] = state.lookup(node.callee);
12208
+ const callees = results &&
12209
+ results.filter((c) => c.type === "FunctionDeclaration");
12193
12210
  if (!callees || !callees.length) {
12194
12211
  const n = name ||
12195
12212
  ("name" in node.callee && node.callee.name) ||
@@ -12224,10 +12241,7 @@ function optimizeCall(state, node, context) {
12224
12241
  }
12225
12242
  }
12226
12243
  }
12227
- if (!(0,external_api_cjs_namespaceObject.hasProperty)(state.calledFunctions, name)) {
12228
- state.calledFunctions[name] = [];
12229
- }
12230
- callees.forEach((c) => (0,external_api_cjs_namespaceObject.isStateNode)(c) && (0,external_util_cjs_namespaceObject.pushUnique)(state.calledFunctions[name], c.node));
12244
+ callees.forEach((c) => markFunctionCalled(state, c.node));
12231
12245
  return null;
12232
12246
  }
12233
12247
 
@@ -12726,7 +12740,7 @@ async function generateOneConfig(buildConfig, dependencyFiles, config) {
12726
12740
  // the oldest optimized file, we don't need to regenerate
12727
12741
  const source_time = await (0,external_util_cjs_namespaceObject.last_modified)(Object.keys(fnMap).concat(dependencyFiles));
12728
12742
  const opt_time = await (0,external_util_cjs_namespaceObject.first_modified)(Object.values(fnMap).map((v) => v.output));
12729
- if (source_time < opt_time && 1654554780017 < opt_time) {
12743
+ if (source_time < opt_time && 1654708837029 < opt_time) {
12730
12744
  return { hasTests, diagnostics: prevDiagnostics };
12731
12745
  }
12732
12746
  }
@@ -1,2 +1,3 @@
1
+ import { LineHandler } from "./util";
1
2
  export declare function launchSimulator(): Promise<void>;
2
- export declare function simulateProgram(prg: string, device: string, test?: boolean): Promise<void>;
3
+ export declare function simulateProgram(prg: string, device: string, test?: boolean, logger?: LineHandler | LineHandler[]): Promise<void>;
@@ -121,7 +121,7 @@ declare global {
121
121
  [key: string]: true;
122
122
  };
123
123
  calledFunctions?: {
124
- [key: string]: unknown[];
124
+ [key: string]: mctree.FunctionDeclaration[];
125
125
  };
126
126
  localsStack?: {
127
127
  node?: mctree.Node;
@@ -2,12 +2,11 @@ import * as glob from "glob";
2
2
  export declare function globa(pattern: string, options?: glob.IOptions): Promise<Array<string>>;
3
3
  export declare function last_modified(inputs: string[]): Promise<number>;
4
4
  export declare function first_modified(inputs: string[]): Promise<number>;
5
- export declare function pushUnique<T>(arr: T[], value: T): void;
6
- declare type LineHandler = (line: string) => void;
5
+ export declare function pushUnique<T, U extends T>(arr: T[], value: U): void;
6
+ export declare type LineHandler = (line: string) => void;
7
7
  export declare function spawnByLine(command: string, args: string[], lineHandlers: LineHandler | LineHandler[], options?: {
8
8
  [key: string]: unknown;
9
9
  }): Promise<void>;
10
10
  export declare function readByLine(file: string, lineHandler: LineHandler): Promise<unknown>;
11
11
  export declare function promiseAll<T>(promiseFn: (i: number) => Promise<T>, parallelism: number): Promise<T[]>;
12
12
  export declare function copyRecursiveAsNeeded(source: string, target: string, filter?: (src: string, tgt: string) => boolean): Promise<void>;
13
- export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markw65/monkeyc-optimizer",
3
3
  "type": "module",
4
- "version": "1.0.19",
4
+ "version": "1.0.20",
5
5
  "description": "Source to source optimizer for Garmin Monkey C code",
6
6
  "main": "build/optimizer.cjs",
7
7
  "types": "build/src/optimizer.d.ts",