@markw65/monkeyc-optimizer 1.1.74 → 1.1.76

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/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  All notable changes to the "monkeyc-optimizer" package will be documented in this file.
4
4
 
5
+ ### 1.1.76
6
+
7
+ - Fix member expression lookup when base type is exact
8
+ - Fix path-resolution inside locals in jungle files
9
+
10
+ ### 1.1.75
11
+
12
+ - Fix issues using personality files from barrels
13
+ - Fix issues with enums values as the base of member expressions
14
+ - Handle `new` expressions as references to the `initialize` method
15
+ - Classes without an explicit `initialize` method get an implicit, empty `initialize`, rather than inheriting the parent's
16
+ - Fix `subtypeOf` for function types.
17
+ - Update tests to work with sdk-7.2.1 and later
18
+
5
19
  ### 1.1.74
6
20
 
7
21
  - Add refs from drawable-ids to the corresponding drawable (Fixes prettier-extension-monkeyc#20)
package/build/api.cjs CHANGED
@@ -18,55 +18,57 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var api_exports = {};
20
20
  __export(api_exports, {
21
- checkCompilerVersion: () => import_chunk_7OAMZRC7.checkCompilerVersion,
22
- collectNamespaces: () => import_chunk_7OAMZRC7.collectNamespaces,
23
- createDocumentationMap: () => import_chunk_7OAMZRC7.createDocumentationMap,
24
- diagnostic: () => import_chunk_7OAMZRC7.diagnostic,
25
- diagnosticHelper: () => import_chunk_7OAMZRC7.diagnosticHelper,
26
- findNamesInScope: () => import_chunk_7OAMZRC7.findNamesInScope,
27
- findUsingForNode: () => import_chunk_7OAMZRC7.findUsingForNode,
28
- formatAst: () => import_chunk_7OAMZRC7.formatAst,
29
- formatAstLongLines: () => import_chunk_7OAMZRC7.formatAstLongLines,
30
- formatScopedName: () => import_chunk_7OAMZRC7.formatScopedName,
31
- getApiFunctionInfo: () => import_chunk_7OAMZRC7.getApiFunctionInfo,
32
- getApiMapping: () => import_chunk_7OAMZRC7.getApiMapping,
33
- getSuperClasses: () => import_chunk_7OAMZRC7.getSuperClasses,
34
- handleImportUsing: () => import_chunk_7OAMZRC7.handleImportUsing,
21
+ checkCompilerVersion: () => import_chunk_3OBK4E4Q.checkCompilerVersion,
22
+ collectNamespaces: () => import_chunk_3OBK4E4Q.collectNamespaces,
23
+ createDocumentationMap: () => import_chunk_3OBK4E4Q.createDocumentationMap,
24
+ declKey: () => import_chunk_3OBK4E4Q.declKey,
25
+ diagnostic: () => import_chunk_3OBK4E4Q.diagnostic,
26
+ diagnosticHelper: () => import_chunk_3OBK4E4Q.diagnosticHelper,
27
+ findNamesInScope: () => import_chunk_3OBK4E4Q.findNamesInScope,
28
+ findUsingForNode: () => import_chunk_3OBK4E4Q.findUsingForNode,
29
+ formatAst: () => import_chunk_3OBK4E4Q.formatAst,
30
+ formatAstLongLines: () => import_chunk_3OBK4E4Q.formatAstLongLines,
31
+ formatScopedName: () => import_chunk_3OBK4E4Q.formatScopedName,
32
+ getApiFunctionInfo: () => import_chunk_3OBK4E4Q.getApiFunctionInfo,
33
+ getApiMapping: () => import_chunk_3OBK4E4Q.getApiMapping,
34
+ getSuperClasses: () => import_chunk_3OBK4E4Q.getSuperClasses,
35
+ handleImportUsing: () => import_chunk_3OBK4E4Q.handleImportUsing,
35
36
  hasProperty: () => import_chunk_JDC43A3I.hasProperty,
36
- isClassVariable: () => import_chunk_7OAMZRC7.isClassVariable,
37
- isLocal: () => import_chunk_7OAMZRC7.isLocal,
38
- isLookupCandidate: () => import_chunk_7OAMZRC7.isLookupCandidate,
39
- isStateNode: () => import_chunk_7OAMZRC7.isStateNode,
40
- lookupByFullName: () => import_chunk_7OAMZRC7.lookupByFullName,
41
- lookupNext: () => import_chunk_7OAMZRC7.lookupNext,
42
- lookupResultContains: () => import_chunk_7OAMZRC7.lookupResultContains,
43
- lookupWithType: () => import_chunk_7OAMZRC7.lookupWithType,
44
- makeToyboxLink: () => import_chunk_7OAMZRC7.makeToyboxLink,
45
- mapVarDeclsByType: () => import_chunk_7OAMZRC7.mapVarDeclsByType,
46
- markInvokeClassMethod: () => import_chunk_7OAMZRC7.markInvokeClassMethod,
47
- parseSdkVersion: () => import_chunk_7OAMZRC7.parseSdkVersion,
48
- popRootNode: () => import_chunk_7OAMZRC7.popRootNode,
49
- pushRootNode: () => import_chunk_7OAMZRC7.pushRootNode,
50
- resolveDiagnostics: () => import_chunk_7OAMZRC7.resolveDiagnostics,
51
- resolveDiagnosticsMap: () => import_chunk_7OAMZRC7.resolveDiagnosticsMap,
52
- sameLookupResult: () => import_chunk_7OAMZRC7.sameLookupResult,
37
+ isClassVariable: () => import_chunk_3OBK4E4Q.isClassVariable,
38
+ isLocal: () => import_chunk_3OBK4E4Q.isLocal,
39
+ isLookupCandidate: () => import_chunk_3OBK4E4Q.isLookupCandidate,
40
+ isStateNode: () => import_chunk_3OBK4E4Q.isStateNode,
41
+ lookupByFullName: () => import_chunk_3OBK4E4Q.lookupByFullName,
42
+ lookupNext: () => import_chunk_3OBK4E4Q.lookupNext,
43
+ lookupResultContains: () => import_chunk_3OBK4E4Q.lookupResultContains,
44
+ lookupWithType: () => import_chunk_3OBK4E4Q.lookupWithType,
45
+ makeToyboxLink: () => import_chunk_3OBK4E4Q.makeToyboxLink,
46
+ mapVarDeclsByType: () => import_chunk_3OBK4E4Q.mapVarDeclsByType,
47
+ markInvokeClassMethod: () => import_chunk_3OBK4E4Q.markInvokeClassMethod,
48
+ parseSdkVersion: () => import_chunk_3OBK4E4Q.parseSdkVersion,
49
+ popRootNode: () => import_chunk_3OBK4E4Q.popRootNode,
50
+ pushRootNode: () => import_chunk_3OBK4E4Q.pushRootNode,
51
+ resolveDiagnostics: () => import_chunk_3OBK4E4Q.resolveDiagnostics,
52
+ resolveDiagnosticsMap: () => import_chunk_3OBK4E4Q.resolveDiagnosticsMap,
53
+ sameLookupResult: () => import_chunk_3OBK4E4Q.sameLookupResult,
53
54
  traverseAst: () => import_chunk_JDC43A3I.traverseAst,
54
- variableDeclarationName: () => import_chunk_7OAMZRC7.variableDeclarationName,
55
- visitReferences: () => import_chunk_7OAMZRC7.visitReferences,
56
- visit_resources: () => import_chunk_7OAMZRC7.visit_resources,
57
- visitorNode: () => import_chunk_7OAMZRC7.visitorNode
55
+ variableDeclarationName: () => import_chunk_3OBK4E4Q.variableDeclarationName,
56
+ visitReferences: () => import_chunk_3OBK4E4Q.visitReferences,
57
+ visit_resources: () => import_chunk_3OBK4E4Q.visit_resources,
58
+ visitorNode: () => import_chunk_3OBK4E4Q.visitorNode
58
59
  });
59
60
  module.exports = __toCommonJS(api_exports);
60
- var import_chunk_7OAMZRC7 = require("./chunk-7OAMZRC7.cjs");
61
+ var import_chunk_3OBK4E4Q = require("./chunk-3OBK4E4Q.cjs");
61
62
  var import_chunk_NHZRKHZO = require("./chunk-NHZRKHZO.cjs");
62
63
  var import_chunk_JDC43A3I = require("./chunk-JDC43A3I.cjs");
63
64
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
64
- (0, import_chunk_7OAMZRC7.init_api)();
65
+ (0, import_chunk_3OBK4E4Q.init_api)();
65
66
  // Annotate the CommonJS export names for ESM import in node:
66
67
  0 && (module.exports = {
67
68
  checkCompilerVersion,
68
69
  collectNamespaces,
69
70
  createDocumentationMap,
71
+ declKey,
70
72
  diagnostic,
71
73
  diagnosticHelper,
72
74
  findNamesInScope,
@@ -26,8 +26,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var chunk_7OAMZRC7_exports = {};
30
- __export(chunk_7OAMZRC7_exports, {
29
+ var chunk_3OBK4E4Q_exports = {};
30
+ __export(chunk_3OBK4E4Q_exports, {
31
31
  EnumTagsConst: () => EnumTagsConst,
32
32
  LastTypeTag: () => LastTypeTag,
33
33
  ObjectLikeTagsConst: () => ObjectLikeTagsConst,
@@ -44,6 +44,7 @@ __export(chunk_7OAMZRC7_exports, {
44
44
  connectiq: () => connectiq,
45
45
  couldBe: () => couldBe,
46
46
  createDocumentationMap: () => createDocumentationMap,
47
+ declKey: () => declKey,
47
48
  defaultConfig: () => defaultConfig,
48
49
  diagnostic: () => diagnostic,
49
50
  diagnosticHelper: () => diagnosticHelper,
@@ -138,7 +139,7 @@ __export(chunk_7OAMZRC7_exports, {
138
139
  visitorNode: () => visitorNode,
139
140
  xml_util_exports: () => xml_util_exports
140
141
  });
141
- module.exports = __toCommonJS(chunk_7OAMZRC7_exports);
142
+ module.exports = __toCommonJS(chunk_3OBK4E4Q_exports);
142
143
  var import_chunk_NHZRKHZO = require("./chunk-NHZRKHZO.cjs");
143
144
  var import_chunk_JDC43A3I = require("./chunk-JDC43A3I.cjs");
144
145
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
@@ -8725,7 +8726,7 @@ function subtypeOfValue(pair) {
8725
8726
  case 8192: {
8726
8727
  const asd = pair.avalue;
8727
8728
  const bsd = pair.bvalue;
8728
- return (0, import_chunk_NHZRKHZO.some)(asd, (sna) => (0, import_chunk_NHZRKHZO.some)(bsd, (snb) => sna === snb));
8729
+ return (0, import_chunk_NHZRKHZO.every)(asd, (sna) => (0, import_chunk_NHZRKHZO.some)(bsd, (snb) => sna === snb));
8729
8730
  }
8730
8731
  case 16384: {
8731
8732
  const asd = pair.avalue;
@@ -9273,89 +9274,84 @@ function printBlockTrailer(block) {
9273
9274
  `Succs: ${(block.succs || []).map((block2) => block2.order).join(", ")} ExSucc: ${block.exsucc ? block.exsucc.order : ""}`
9274
9275
  );
9275
9276
  }
9276
- function filterDecls(decls, possible, name) {
9277
+ function filterDecls(state, decls, next) {
9278
+ const possible = (0, import_chunk_JDC43A3I.hasProperty)(state.allDeclarations, next.name) && state.allDeclarations[next.name];
9277
9279
  if (!possible)
9278
9280
  return [null, null];
9279
- const result = decls.reduce(
9280
- (cur, decl) => {
9281
- let declSups;
9282
- const found = possible.reduce((flag, poss) => {
9283
- if (decl === poss || poss.type === "ClassDeclaration" && getSuperClasses(poss)?.has(decl)) {
9284
- if (!cur[0]) {
9285
- cur = [/* @__PURE__ */ new Set(), /* @__PURE__ */ new Set()];
9286
- }
9287
- cur[0].add(poss);
9288
- cur[1].add(poss);
9289
- return true;
9290
- } else if (declSups !== void 0 ? declSups?.has(poss) : decl.type === "ClassDeclaration" && (declSups = getSuperClasses(decl))?.has(poss)) {
9291
- if (!cur[0]) {
9292
- cur = [/* @__PURE__ */ new Set([decl]), /* @__PURE__ */ new Set([poss])];
9293
- } else {
9294
- cur[0].add(decl);
9295
- if (Array.from(cur[1]).every((d) => {
9296
- if (decl === d)
9297
- return false;
9298
- if (!declSups.has(d))
9299
- return true;
9300
- if (d.type === "ClassDeclaration" && getSuperClasses(d)?.has(poss)) {
9301
- return false;
9302
- }
9303
- if (poss.type === "ClassDeclaration" && getSuperClasses(poss)?.has(d)) {
9304
- cur[1].delete(d);
9305
- }
9306
- return true;
9307
- })) {
9308
- cur[1].add(poss);
9309
- }
9281
+ const result = decls.reduce((cur, { sn: decl, exact }) => {
9282
+ let declSups;
9283
+ const getDeclSups = () => declSups !== void 0 ? declSups : decl.type === "ClassDeclaration" ? declSups = getSuperClasses(decl) : null;
9284
+ const found = possible.reduce((flag, poss) => {
9285
+ if (decl === poss || !exact && poss.type === "ClassDeclaration" && getSuperClasses(poss)?.has(decl)) {
9286
+ if (!cur) {
9287
+ cur = [/* @__PURE__ */ new Set(), /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set()];
9288
+ }
9289
+ cur[0].add(poss);
9290
+ cur[1].add(poss);
9291
+ if (cur[2].size && decl === poss && decl.type === "ClassDeclaration") {
9292
+ const posSupers = getSuperClasses(decl);
9293
+ if (posSupers) {
9294
+ cur[2].forEach((s) => posSupers.has(s) && cur[2].delete(s));
9310
9295
  }
9311
- return true;
9312
9296
  }
9313
- return flag;
9314
- }, false);
9315
- if (!found) {
9316
- let d = [decl];
9317
- do {
9318
- d.forEach((d2) => {
9319
- const stack = d2.stack;
9320
- possible.forEach((poss) => {
9321
- for (let i = stack.length; i--; ) {
9322
- const sn = stack[i].sn;
9323
- if (sn.decls === poss.decls) {
9324
- if (!cur[0]) {
9325
- cur = [/* @__PURE__ */ new Set(), /* @__PURE__ */ new Set()];
9326
- }
9327
- cur[0].add(decl);
9328
- cur[1].add(poss);
9329
- break;
9330
- }
9331
- if ((0, import_chunk_JDC43A3I.hasProperty)(sn.decls, name)) {
9332
- break;
9297
+ return true;
9298
+ } else if (poss.type === "ClassDeclaration" && getDeclSups()?.has(poss)) {
9299
+ if (!cur) {
9300
+ cur = [/* @__PURE__ */ new Set(), /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set()];
9301
+ }
9302
+ cur[0].add(decl);
9303
+ const supers = Array.from(cur[2]);
9304
+ if (!supers.some((s) => getSuperClasses(s)?.has(poss))) {
9305
+ const posSupers = getSuperClasses(poss);
9306
+ posSupers && supers.forEach((s) => posSupers.has(s) && cur[2].delete(s));
9307
+ cur[2].add(poss);
9308
+ }
9309
+ return true;
9310
+ }
9311
+ return flag;
9312
+ }, false);
9313
+ if (!found) {
9314
+ let d = [decl];
9315
+ do {
9316
+ d.forEach((d2) => {
9317
+ const stack = d2.stack;
9318
+ possible.forEach((poss) => {
9319
+ for (let i = stack.length; i--; ) {
9320
+ const sn = stack[i].sn;
9321
+ if (sn.decls === poss.decls) {
9322
+ if (!cur) {
9323
+ cur = [/* @__PURE__ */ new Set(), /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set()];
9333
9324
  }
9325
+ cur[0].add(decl);
9326
+ cur[1].add(poss);
9327
+ break;
9328
+ }
9329
+ if ((0, import_chunk_JDC43A3I.hasProperty)(sn.decls, next.name)) {
9330
+ break;
9334
9331
  }
9335
- });
9336
- });
9337
- d = d.flatMap((d2) => {
9338
- if (d2.type !== "ClassDeclaration" || !d2.superClass || d2.superClass === true) {
9339
- return [];
9340
9332
  }
9341
- return d2.superClass;
9342
9333
  });
9343
- } while (d.length);
9344
- }
9345
- return cur;
9346
- },
9347
- [null, null]
9348
- );
9349
- if (!result[0])
9334
+ });
9335
+ d = d.flatMap((d2) => {
9336
+ if (d2.type !== "ClassDeclaration" || !d2.superClass || d2.superClass === true) {
9337
+ return [];
9338
+ }
9339
+ return d2.superClass;
9340
+ });
9341
+ } while (d.length);
9342
+ }
9343
+ return cur;
9344
+ }, null);
9345
+ if (!result)
9350
9346
  return [null, null];
9347
+ result[2].forEach((c) => result[1].add(c));
9351
9348
  return [Array.from(result[0]), Array.from(result[1])];
9352
9349
  }
9353
9350
  function findObjectDeclsByProperty(state, object, next) {
9354
- const decls = getStateNodeDeclsFromType(state, object);
9351
+ const decls = getStateNodeDeclsWithExactFromType(state, object);
9355
9352
  if (!decls)
9356
9353
  return [null, null];
9357
- const possibleDecls = (0, import_chunk_JDC43A3I.hasProperty)(state.allDeclarations, next.name) && state.allDeclarations[next.name];
9358
- return filterDecls(decls, possibleDecls, next.name);
9354
+ return filterDecls(state, decls, next);
9359
9355
  }
9360
9356
  function refineObjectTypeByDecls(istate, object, trueDecls) {
9361
9357
  const refinedType = typeFromTypeStateNodes(istate.state, trueDecls);
@@ -10588,8 +10584,9 @@ function pushScopedNameType(istate, node, object) {
10588
10584
  if (istate.typeMap) {
10589
10585
  result = istate.typeMap.get(node);
10590
10586
  if (!result && object && node.type === "MemberExpression" && !node.computed) {
10591
- istate.typeMap.set(node.object, object.value);
10592
- const resolved = resolveDottedMember(istate, object.value, node);
10587
+ const objectType = deEnumerate(object.value);
10588
+ istate.typeMap.set(node.object, objectType);
10589
+ const resolved = resolveDottedMember(istate, objectType, node);
10593
10590
  if (resolved) {
10594
10591
  result = resolved.property;
10595
10592
  if (resolved.mayThrow) {
@@ -10944,7 +10941,7 @@ function evaluateNode(istate, node) {
10944
10941
  if (!isLookupCandidate(node)) {
10945
10942
  const property = popIstate(istate, node.property);
10946
10943
  const object = popIstate(istate, node.object);
10947
- const objectType = object.value;
10944
+ const objectType = deEnumerate(object.value);
10948
10945
  let byteArray = false;
10949
10946
  if (objectType.type & 32768 && objectType.value) {
10950
10947
  const odata = getObjectValue(objectType);
@@ -11115,29 +11112,26 @@ function evaluateNode(istate, node) {
11115
11112
  if (isExact(klass.value) && klass.value.type === 16384) {
11116
11113
  obj.value = { klass: klass.value };
11117
11114
  if (istate.checkTypes && klass.value.value) {
11118
- const results = lookupNext(
11119
- istate.state,
11120
- [
11121
- {
11122
- parent: null,
11123
- results: Array.isArray(klass.value.value) ? klass.value.value : [klass.value.value]
11124
- }
11125
- ],
11126
- "decls",
11127
- { type: "Identifier", name: "initialize" }
11115
+ const callees = (0, import_chunk_NHZRKHZO.map)(
11116
+ klass.value.value,
11117
+ (klass2) => klass2.decls?.initialize
11118
+ ).flat().filter(
11119
+ (result) => result?.type === "FunctionDeclaration"
11128
11120
  );
11129
- if (results) {
11130
- const callees = results.flatMap(
11131
- (lookupDef) => lookupDef.results.filter(
11132
- (result) => result.type === "FunctionDeclaration"
11133
- )
11134
- );
11121
+ if (callees.length) {
11135
11122
  checkCallArgs(
11136
11123
  istate,
11137
11124
  node,
11138
11125
  callees,
11139
11126
  args.map(({ value: value2 }) => value2)
11140
11127
  );
11128
+ } else if (args.length) {
11129
+ diagnostic(
11130
+ istate.state,
11131
+ node,
11132
+ `initialize method expected no args, but got ${args.length}`,
11133
+ istate.checkTypes
11134
+ );
11141
11135
  }
11142
11136
  }
11143
11137
  }
@@ -12209,6 +12203,9 @@ function setUnionComponent(v, tag, c) {
12209
12203
  }
12210
12204
  }
12211
12205
  function getStateNodeDeclsFromType(state, object) {
12206
+ return getStateNodeDeclsWithExactFromType(state, object).map(({ sn }) => sn);
12207
+ }
12208
+ function getStateNodeDeclsWithExactFromType(state, object) {
12212
12209
  const decls = [];
12213
12210
  if (object.value != null && object.type & (4096 | 16384 | 32768)) {
12214
12211
  forEachUnionComponent(
@@ -12220,20 +12217,15 @@ function getStateNodeDeclsFromType(state, object) {
12220
12217
  switch (type.type) {
12221
12218
  case 32768:
12222
12219
  if (type.value.klass.type === 16384 && type.value.klass.value) {
12223
- if (Array.isArray(type.value.klass.value)) {
12224
- decls.push(...type.value.klass.value);
12225
- } else {
12226
- decls.push(type.value.klass.value);
12227
- }
12220
+ (0, import_chunk_NHZRKHZO.forEach)(
12221
+ type.value.klass.value,
12222
+ (sn) => decls.push({ sn, exact: false })
12223
+ );
12228
12224
  }
12229
12225
  break;
12230
12226
  case 4096:
12231
12227
  case 16384:
12232
- if (Array.isArray(type.value)) {
12233
- decls.push(...type.value);
12234
- } else {
12235
- decls.push(type.value);
12236
- }
12228
+ (0, import_chunk_NHZRKHZO.forEach)(type.value, (sn) => decls.push({ sn, exact: true }));
12237
12229
  break;
12238
12230
  }
12239
12231
  }
@@ -12253,7 +12245,7 @@ function getStateNodeDeclsFromType(state, object) {
12253
12245
  }
12254
12246
  const name = `Toybox.Lang.${typeTagName(bit)}`;
12255
12247
  const sns = lookupByFullName(state, name);
12256
- sns.forEach((sn) => isStateNode(sn) && decls.push(sn));
12248
+ sns.forEach((sn) => isStateNode(sn) && decls.push({ sn, exact: true }));
12257
12249
  bits = next;
12258
12250
  } while (bits);
12259
12251
  }
@@ -13572,11 +13564,23 @@ function propagateTypes(state, root, graph, optimizeEquivalencies, copyPropStore
13572
13564
  function getStateType(blockState, decl) {
13573
13565
  return getStateEntry(blockState, decl).curType;
13574
13566
  }
13575
- function getStateEntry(blockState, decl) {
13567
+ function getStateEntry(blockState, decl, node) {
13576
13568
  if (Array.isArray(decl) || decl.type !== "MemberDecl" && decl.type !== "Unknown") {
13577
13569
  let tsVal = blockState.map.get(decl);
13578
13570
  if (!tsVal) {
13579
- tsVal = { curType: typeConstraint(decl, blockState) };
13571
+ const t = typeConstraint(decl, blockState);
13572
+ if (node?.type === "MemberExpression" && !node.computed) {
13573
+ const baseType = typeMap.get(node.object);
13574
+ if (baseType) {
13575
+ const r = evaluate(istate, node).value;
13576
+ if (subtypeOf(r, t)) {
13577
+ tsVal = { curType: r };
13578
+ }
13579
+ }
13580
+ }
13581
+ if (!tsVal) {
13582
+ tsVal = { curType: t };
13583
+ }
13580
13584
  blockState.map.set(decl, tsVal);
13581
13585
  }
13582
13586
  return tsVal;
@@ -13989,7 +13993,7 @@ function propagateTypes(state, root, graph, optimizeEquivalencies, copyPropStore
13989
13993
  break;
13990
13994
  }
13991
13995
  case "ref": {
13992
- const curEntry = getStateEntry(curState, event.decl);
13996
+ const curEntry = getStateEntry(curState, event.decl, event.node);
13993
13997
  typeMap.set(event.node, curEntry.curType);
13994
13998
  nodeEquivs.delete(event.node);
13995
13999
  if (curEntry.equivSet) {
@@ -16923,8 +16927,10 @@ function collectClassInfo(state) {
16923
16927
  }
16924
16928
  function getFileSources(fnMap) {
16925
16929
  return Promise.all(
16926
- Object.entries(fnMap).map(([name, value2]) => {
16927
- return value2.monkeyCSource || fs.readFile(name).then(
16930
+ Object.values(fnMap).map((value2) => {
16931
+ if (value2.monkeyCSource)
16932
+ return value2.monkeyCSource;
16933
+ return fs.readFile(value2.name).then(
16928
16934
  (data) => value2.monkeyCSource = data.toString().replace(/\r\n/g, "\n")
16929
16935
  );
16930
16936
  })
@@ -16934,12 +16940,12 @@ function getFileSources(fnMap) {
16934
16940
  }
16935
16941
  function getFileASTs(fnMap) {
16936
16942
  return getFileSources(fnMap).then(
16937
- () => Object.entries(fnMap).reduce((ok, [name, value2]) => {
16943
+ () => Object.values(fnMap).reduce((ok, value2) => {
16938
16944
  if (!value2.ast && !value2.parserError) {
16939
16945
  const options = {
16940
- filepath: name
16946
+ filepath: value2.name
16941
16947
  };
16942
- if (/\.mss$/i.test(name)) {
16948
+ if (/\.mss$/i.test(value2.name)) {
16943
16949
  options.mss = value2.barrel;
16944
16950
  }
16945
16951
  try {
@@ -17026,12 +17032,12 @@ async function analyze(fnMap, resourcesMap, manifestXML, config, allowParseError
17026
17032
  markApi = false;
17027
17033
  const state = preState;
17028
17034
  await getFileASTs(fnMap);
17029
- Object.entries(fnMap).forEach(([name, value2]) => {
17035
+ Object.values(fnMap).forEach((value2) => {
17030
17036
  const { ast, parserError } = value2;
17031
17037
  if (!ast) {
17032
17038
  if (allowParseErrors)
17033
17039
  return;
17034
- throw parserError || new Error(`Failed to parse ${name}`);
17040
+ throw parserError || new Error(`Failed to parse ${value2.name}`);
17035
17041
  }
17036
17042
  hasTests = false;
17037
17043
  collectNamespaces(ast, state);
@@ -17732,8 +17738,8 @@ async function optimizeMonkeyCHelper(fnMap, resourcesMap, manifestXML, config) {
17732
17738
  }
17733
17739
  const diagnostics = state.diagnostics && await resolveDiagnosticsMap(state.diagnostics);
17734
17740
  if (state.config?.checkBuildPragmas) {
17735
- Object.entries(fnMap).forEach(([name, f]) => {
17736
- pragmaChecker(state, f.ast, diagnostics?.[name]);
17741
+ Object.values(fnMap).forEach((f) => {
17742
+ pragmaChecker(state, f.ast, diagnostics?.[f.name]);
17737
17743
  });
17738
17744
  }
17739
17745
  return {
@@ -18637,6 +18643,9 @@ function visitorNode(node) {
18637
18643
  if (node.type === "BinaryExpression" && node.operator === "has" && node.right.type === "UnaryExpression" && node.right.operator === ":") {
18638
18644
  return node.right.argument;
18639
18645
  }
18646
+ if (node.type === "NewExpression") {
18647
+ return visitorNode(node.callee);
18648
+ }
18640
18649
  return node;
18641
18650
  }
18642
18651
  function visitReferences(state, ast, name, defn, callback, includeDefs = false, filter = null, typeMap = null, findSingleDefinition = false) {
@@ -18724,6 +18733,42 @@ function visitReferences(state, ast, name, defn, callback, includeDefs = false,
18724
18733
  return ["arguments"];
18725
18734
  }
18726
18735
  break;
18736
+ case "NewExpression": {
18737
+ const [name2, results] = lookup2(node.callee, true);
18738
+ if (!results)
18739
+ break;
18740
+ const initializers = /* @__PURE__ */ new Map();
18741
+ results.forEach((result) => {
18742
+ result.results.forEach((klass) => {
18743
+ if (klass.type !== "ClassDeclaration")
18744
+ return;
18745
+ const inits = klass.decls?.["initialize"];
18746
+ inits?.forEach((init) => {
18747
+ if (init.type === "FunctionDeclaration") {
18748
+ const existing = initializers.get(klass);
18749
+ if (existing) {
18750
+ existing.push(init);
18751
+ } else {
18752
+ initializers.set(klass, [init]);
18753
+ }
18754
+ }
18755
+ });
18756
+ });
18757
+ });
18758
+ if (initializers.size) {
18759
+ checkResults(
18760
+ [
18761
+ name2,
18762
+ Array.from(initializers).map(([parent, results2]) => ({
18763
+ parent,
18764
+ results: results2
18765
+ }))
18766
+ ],
18767
+ node
18768
+ );
18769
+ }
18770
+ break;
18771
+ }
18727
18772
  case "Identifier":
18728
18773
  if (!name || node.name === name) {
18729
18774
  return checkResults(lookup2(node), node);
@@ -28548,7 +28593,13 @@ function evaluate_locals(assignments) {
28548
28593
  while (true) {
28549
28594
  assignments = assignments.filter((a) => {
28550
28595
  if (a.names.length === 1 && a.values.every((v) => v.type === "Literal")) {
28551
- locals[a.names[0]] = a.values;
28596
+ locals[a.names[0]] = a.values.map((v) => {
28597
+ if ("source" in v) {
28598
+ v = { ...v };
28599
+ delete v.source;
28600
+ }
28601
+ return v;
28602
+ });
28552
28603
  return false;
28553
28604
  }
28554
28605
  return true;
@@ -30029,6 +30080,7 @@ async function fileInfoFromConfig(workspace, buildDir, output, buildConfig, extr
30029
30080
  ).concat(personalityFiles).map((file) => [
30030
30081
  file,
30031
30082
  {
30083
+ name: file,
30032
30084
  output: path4.join(
30033
30085
  output,
30034
30086
  relative_path_no_dotdot(path4.relative(workspace, file))
@@ -30102,7 +30154,9 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
30102
30154
  );
30103
30155
  barrelFnMaps.forEach(
30104
30156
  (barrelFnMap) => Object.entries(barrelFnMap).forEach(([key2, value2]) => {
30105
- if (!(0, import_chunk_JDC43A3I.hasProperty)(fnMap, key2)) {
30157
+ if (fnMap[key2]) {
30158
+ fnMap[key2 + "::" + value2.barrel] = value2;
30159
+ } else {
30106
30160
  fnMap[key2] = value2;
30107
30161
  }
30108
30162
  })
@@ -30130,7 +30184,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
30130
30184
  const opt_time = await (0, import_chunk_NHZRKHZO.first_modified)(
30131
30185
  Object.values(fnMap).map((v) => v.output)
30132
30186
  );
30133
- if (source_time < opt_time && 1723999677373 < opt_time) {
30187
+ if (source_time < opt_time && 1726690662875 < opt_time) {
30134
30188
  return {
30135
30189
  hasTests,
30136
30190
  diagnostics: prevDiagnostics,
@@ -30168,7 +30222,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
30168
30222
  hasTests: hasTests2,
30169
30223
  diagnostics,
30170
30224
  sdkVersion,
30171
- optimizerVersion: "1.1.74",
30225
+ optimizerVersion: "1.1.76",
30172
30226
  ...Object.fromEntries(
30173
30227
  configOptionsToCheck.map((option) => [option, config[option]])
30174
30228
  )
@@ -30233,9 +30287,13 @@ async function getProjectAnalysisHelper(targets, analysis, manifestXML, options)
30233
30287
  (results) => results.reduce((cur, result) => {
30234
30288
  if (!cur)
30235
30289
  return result;
30236
- Object.entries(result.fnMap).forEach(
30237
- ([key, value2]) => cur.fnMap[key] || (cur.fnMap[key] = value2)
30238
- );
30290
+ Object.entries(result.fnMap).forEach(([key, value2]) => {
30291
+ if (cur.fnMap[key]) {
30292
+ cur.fnMap[key + "::" + value2.barrel] = value2;
30293
+ } else {
30294
+ cur.fnMap[key] = value2;
30295
+ }
30296
+ });
30239
30297
  cur.paths.push(...result.paths);
30240
30298
  return cur;
30241
30299
  }, null)
@@ -30283,8 +30341,8 @@ async function getFnMapAnalysis(fnMap, resourcesMap, manifestXML, options) {
30283
30341
  const typeMap = await analyze_module_types(state);
30284
30342
  const diagnostics = state.diagnostics && await resolveDiagnosticsMap(state.diagnostics);
30285
30343
  if (state.config?.checkBuildPragmas) {
30286
- Object.entries(fnMap).forEach(
30287
- ([name, f]) => f.ast && pragmaChecker(state, f.ast, diagnostics?.[name])
30344
+ Object.values(fnMap).forEach(
30345
+ (f) => f.ast && pragmaChecker(state, f.ast, diagnostics?.[f.name])
30288
30346
  );
30289
30347
  }
30290
30348
  return { fnMap, state, typeMap };
@@ -30999,6 +31057,7 @@ var init_sdk_util = (0, import_chunk_ABYVSU2C.__esm)({
30999
31057
  connectiq,
31000
31058
  couldBe,
31001
31059
  createDocumentationMap,
31060
+ declKey,
31002
31061
  defaultConfig,
31003
31062
  diagnostic,
31004
31063
  diagnosticHelper,
@@ -18,30 +18,30 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var optimizer_exports = {};
20
20
  __export(optimizer_exports, {
21
- StateNodeAttributes: () => import_chunk_7OAMZRC7.StateNodeAttributes,
22
- buildConfigDescription: () => import_chunk_7OAMZRC7.buildConfigDescription,
23
- buildOptimizedProject: () => import_chunk_7OAMZRC7.buildOptimizedProject,
21
+ StateNodeAttributes: () => import_chunk_3OBK4E4Q.StateNodeAttributes,
22
+ buildConfigDescription: () => import_chunk_3OBK4E4Q.buildConfigDescription,
23
+ buildOptimizedProject: () => import_chunk_3OBK4E4Q.buildOptimizedProject,
24
24
  copyRecursiveAsNeeded: () => import_chunk_NHZRKHZO.copyRecursiveAsNeeded,
25
- defaultConfig: () => import_chunk_7OAMZRC7.defaultConfig,
26
- display: () => import_chunk_7OAMZRC7.display,
27
- generateOneConfig: () => import_chunk_7OAMZRC7.generateOneConfig,
28
- generateOptimizedProject: () => import_chunk_7OAMZRC7.generateOptimizedProject,
29
- getConfig: () => import_chunk_7OAMZRC7.getConfig,
30
- getFnMapAnalysis: () => import_chunk_7OAMZRC7.getFnMapAnalysis,
31
- getProjectAnalysis: () => import_chunk_7OAMZRC7.getProjectAnalysis,
32
- get_jungle: () => import_chunk_7OAMZRC7.get_jungle,
33
- isErrorWithLocation: () => import_chunk_7OAMZRC7.isErrorWithLocation,
34
- launchSimulator: () => import_chunk_7OAMZRC7.launchSimulator,
35
- manifestProducts: () => import_chunk_7OAMZRC7.manifestProducts,
36
- mctree: () => import_chunk_7OAMZRC7.mctree,
37
- simulateProgram: () => import_chunk_7OAMZRC7.simulateProgram
25
+ defaultConfig: () => import_chunk_3OBK4E4Q.defaultConfig,
26
+ display: () => import_chunk_3OBK4E4Q.display,
27
+ generateOneConfig: () => import_chunk_3OBK4E4Q.generateOneConfig,
28
+ generateOptimizedProject: () => import_chunk_3OBK4E4Q.generateOptimizedProject,
29
+ getConfig: () => import_chunk_3OBK4E4Q.getConfig,
30
+ getFnMapAnalysis: () => import_chunk_3OBK4E4Q.getFnMapAnalysis,
31
+ getProjectAnalysis: () => import_chunk_3OBK4E4Q.getProjectAnalysis,
32
+ get_jungle: () => import_chunk_3OBK4E4Q.get_jungle,
33
+ isErrorWithLocation: () => import_chunk_3OBK4E4Q.isErrorWithLocation,
34
+ launchSimulator: () => import_chunk_3OBK4E4Q.launchSimulator,
35
+ manifestProducts: () => import_chunk_3OBK4E4Q.manifestProducts,
36
+ mctree: () => import_chunk_3OBK4E4Q.mctree,
37
+ simulateProgram: () => import_chunk_3OBK4E4Q.simulateProgram
38
38
  });
39
39
  module.exports = __toCommonJS(optimizer_exports);
40
- var import_chunk_7OAMZRC7 = require("./chunk-7OAMZRC7.cjs");
40
+ var import_chunk_3OBK4E4Q = require("./chunk-3OBK4E4Q.cjs");
41
41
  var import_chunk_NHZRKHZO = require("./chunk-NHZRKHZO.cjs");
42
42
  var import_chunk_JDC43A3I = require("./chunk-JDC43A3I.cjs");
43
43
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
44
- (0, import_chunk_7OAMZRC7.init_optimizer)();
44
+ (0, import_chunk_3OBK4E4Q.init_optimizer)();
45
45
  // Annotate the CommonJS export names for ESM import in node:
46
46
  0 && (module.exports = {
47
47
  StateNodeAttributes,
@@ -18,25 +18,25 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var sdk_util_exports = {};
20
20
  __export(sdk_util_exports, {
21
- SectionKinds: () => import_chunk_7OAMZRC7.SectionKinds,
22
- appSupport: () => import_chunk_7OAMZRC7.appSupport,
23
- connectiq: () => import_chunk_7OAMZRC7.connectiq,
24
- getDeviceInfo: () => import_chunk_7OAMZRC7.getDeviceInfo,
25
- getFunctionDocumentation: () => import_chunk_7OAMZRC7.getFunctionDocumentation,
26
- getLanguages: () => import_chunk_7OAMZRC7.getLanguages,
27
- getSdkPath: () => import_chunk_7OAMZRC7.getSdkPath,
28
- isWin: () => import_chunk_7OAMZRC7.isWin,
29
- optimizeProgram: () => import_chunk_7OAMZRC7.optimizeProgram,
30
- readPrg: () => import_chunk_7OAMZRC7.readPrg,
31
- readPrgWithOffsets: () => import_chunk_7OAMZRC7.readPrgWithOffsets,
32
- xmlUtil: () => import_chunk_7OAMZRC7.xml_util_exports
21
+ SectionKinds: () => import_chunk_3OBK4E4Q.SectionKinds,
22
+ appSupport: () => import_chunk_3OBK4E4Q.appSupport,
23
+ connectiq: () => import_chunk_3OBK4E4Q.connectiq,
24
+ getDeviceInfo: () => import_chunk_3OBK4E4Q.getDeviceInfo,
25
+ getFunctionDocumentation: () => import_chunk_3OBK4E4Q.getFunctionDocumentation,
26
+ getLanguages: () => import_chunk_3OBK4E4Q.getLanguages,
27
+ getSdkPath: () => import_chunk_3OBK4E4Q.getSdkPath,
28
+ isWin: () => import_chunk_3OBK4E4Q.isWin,
29
+ optimizeProgram: () => import_chunk_3OBK4E4Q.optimizeProgram,
30
+ readPrg: () => import_chunk_3OBK4E4Q.readPrg,
31
+ readPrgWithOffsets: () => import_chunk_3OBK4E4Q.readPrgWithOffsets,
32
+ xmlUtil: () => import_chunk_3OBK4E4Q.xml_util_exports
33
33
  });
34
34
  module.exports = __toCommonJS(sdk_util_exports);
35
- var import_chunk_7OAMZRC7 = require("./chunk-7OAMZRC7.cjs");
35
+ var import_chunk_3OBK4E4Q = require("./chunk-3OBK4E4Q.cjs");
36
36
  var import_chunk_NHZRKHZO = require("./chunk-NHZRKHZO.cjs");
37
37
  var import_chunk_JDC43A3I = require("./chunk-JDC43A3I.cjs");
38
38
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
39
- (0, import_chunk_7OAMZRC7.init_sdk_util)();
39
+ (0, import_chunk_3OBK4E4Q.init_sdk_util)();
40
40
  // Annotate the CommonJS export names for ESM import in node:
41
41
  0 && (module.exports = {
42
42
  SectionKinds,
@@ -19,6 +19,7 @@ export declare function isStateNode(node: {
19
19
  export declare function variableDeclarationName(node: mctree.TypedIdentifier | mctree.InstanceofIdentifier): string;
20
20
  type DeclKind = "decls" | "type_decls";
21
21
  export declare function sameLookupResult(a: LookupDefinition[], b: LookupDefinition[]): boolean;
22
+ export declare function declKey(decl: StateNodeDecl): string | mctree.Identifier | mctree.Program | mctree.VariableDeclarator | mctree.EnumStringMember | mctree.BlockStatement | mctree.ForStatement | mctree.ClassDeclaration | mctree.EnumDeclaration | mctree.FunctionDeclaration | mctree.TypedefDeclaration | mctree.AsIdentifier | undefined;
22
23
  export declare function lookupResultContains(a: LookupDefinition[], b: LookupDefinition[]): boolean;
23
24
  export declare function isLookupCandidate(node: mctree.MemberExpression): false | mctree.Identifier;
24
25
  export declare function lookupNext(state: ProgramStateLive, results: LookupDefinition[], decls: DeclKind, property: mctree.Identifier): LookupDefinition[] | null;
@@ -197,6 +197,7 @@ export type ExcludeAnnotationsMap = {
197
197
  };
198
198
  export type FilesToOptimizeMap = {
199
199
  [key: string]: {
200
+ name: string;
200
201
  output: string;
201
202
  barrel: string;
202
203
  excludeAnnotations: ExcludeAnnotationsMap;
@@ -211,4 +211,8 @@ export declare function forEachUnionComponent(v: ExactOrUnion, bits: TypeTag, fn
211
211
  export declare function getUnionComponent<T extends ExactTypeTags>(v: ExactOrUnion, tag: T): ExactData<T> | null;
212
212
  export declare function setUnionComponent<T extends ExactTypeTags>(v: ExactOrUnion, tag: T, c: ExactData<T>): void;
213
213
  export declare function getStateNodeDeclsFromType(state: ProgramStateAnalysis, object: ExactOrUnion): StateNode[];
214
+ export declare function getStateNodeDeclsWithExactFromType(state: ProgramStateAnalysis, object: ExactOrUnion): {
215
+ sn: StateNode;
216
+ exact: boolean;
217
+ }[];
214
218
  export {};
@@ -21,17 +21,17 @@ __export(worker_thread_exports, {
21
21
  default: () => worker_thread_default
22
22
  });
23
23
  module.exports = __toCommonJS(worker_thread_exports);
24
- var import_chunk_7OAMZRC7 = require("./chunk-7OAMZRC7.cjs");
24
+ var import_chunk_3OBK4E4Q = require("./chunk-3OBK4E4Q.cjs");
25
25
  var import_chunk_NHZRKHZO = require("./chunk-NHZRKHZO.cjs");
26
26
  var import_chunk_JDC43A3I = require("./chunk-JDC43A3I.cjs");
27
27
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
28
28
  var import_node_worker_threads = require("node:worker_threads");
29
29
  var require_worker_thread = (0, import_chunk_ABYVSU2C.__commonJS)({
30
30
  "src/worker-thread.ts"() {
31
- (0, import_chunk_7OAMZRC7.init_worker_task)();
31
+ (0, import_chunk_3OBK4E4Q.init_worker_task)();
32
32
  if (import_node_worker_threads.parentPort) {
33
33
  import_node_worker_threads.parentPort.on("message", async (task) => {
34
- return import_node_worker_threads.parentPort.postMessage(await (0, import_chunk_7OAMZRC7.performTask)(task));
34
+ return import_node_worker_threads.parentPort.postMessage(await (0, import_chunk_3OBK4E4Q.performTask)(task));
35
35
  });
36
36
  }
37
37
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markw65/monkeyc-optimizer",
3
3
  "type": "commonjs",
4
- "version": "1.1.74",
4
+ "version": "1.1.76",
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",