@markw65/monkeyc-optimizer 1.1.33 → 1.1.34

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 (80) hide show
  1. package/README.md +1 -932
  2. package/build/api.cjs +31 -31
  3. package/build/{chunk-7ANQTFGD.cjs → chunk-QBCVUVLD.cjs} +163 -46
  4. package/build/optimizer.cjs +17 -17
  5. package/build/sdk-util.cjs +14 -14
  6. package/build/src/api.d.ts +1 -1
  7. package/build/src/readprg.d.ts +0 -6
  8. package/build/src/src/api.d.ts +51 -0
  9. package/build/src/src/ast.d.ts +59 -0
  10. package/build/src/src/build.d.ts +7 -0
  11. package/build/src/src/cftinfo.d.ts +32 -0
  12. package/build/src/src/control-flow.d.ts +22 -0
  13. package/build/src/src/data-flow.d.ts +107 -0
  14. package/build/src/src/driver.d.ts +2 -0
  15. package/build/src/src/function-info.d.ts +14 -0
  16. package/build/src/src/inliner.d.ts +13 -0
  17. package/build/src/src/jungles.d.ts +66 -0
  18. package/build/src/src/launch.d.ts +5 -0
  19. package/build/src/src/logger.d.ts +6 -0
  20. package/build/src/src/manifest.d.ts +11 -0
  21. package/build/src/src/mc-rewrite.d.ts +13 -0
  22. package/build/src/src/negative-fixups.d.ts +1 -0
  23. package/build/src/src/optimizer-types.d.ts +249 -0
  24. package/build/src/src/optimizer.d.ts +100 -0
  25. package/build/src/src/pragma-checker.d.ts +3 -0
  26. package/build/src/src/pre.d.ts +2 -0
  27. package/build/src/src/projects.d.ts +26 -0
  28. package/build/src/src/readprg/array-init.d.ts +3 -0
  29. package/build/src/src/readprg/bytecode.d.ts +72 -0
  30. package/build/src/src/readprg/cflow.d.ts +17 -0
  31. package/build/src/src/readprg/data.d.ts +4 -0
  32. package/build/src/src/readprg/dce.d.ts +6 -0
  33. package/build/src/src/readprg/emit.d.ts +23 -0
  34. package/build/src/src/readprg/exceptions.d.ts +10 -0
  35. package/build/src/src/readprg/header.d.ts +15 -0
  36. package/build/src/src/readprg/interp.d.ts +21 -0
  37. package/build/src/src/readprg/linenum.d.ts +29 -0
  38. package/build/src/src/readprg/locals.d.ts +3 -0
  39. package/build/src/src/readprg/opcodes.d.ts +279 -0
  40. package/build/src/src/readprg/optimize.d.ts +3 -0
  41. package/build/src/src/readprg/pre.d.ts +2 -0
  42. package/build/src/src/readprg/sharing.d.ts +2 -0
  43. package/build/src/src/readprg/signer.d.ts +8 -0
  44. package/build/src/src/readprg/symbols.d.ts +19 -0
  45. package/build/src/src/readprg.d.ts +25 -0
  46. package/build/src/src/resources.d.ts +12 -0
  47. package/build/src/src/sdk-util.d.ts +28 -0
  48. package/build/src/src/type-flow/could-be.d.ts +5 -0
  49. package/build/src/src/type-flow/dead-store.d.ts +18 -0
  50. package/build/src/src/type-flow/interp-binary.d.ts +4 -0
  51. package/build/src/src/type-flow/interp-call.d.ts +16 -0
  52. package/build/src/src/type-flow/interp.d.ts +33 -0
  53. package/build/src/src/type-flow/intersection-type.d.ts +4 -0
  54. package/build/src/src/type-flow/minimize-locals.d.ts +4 -0
  55. package/build/src/src/type-flow/minimize-modules.d.ts +3 -0
  56. package/build/src/src/type-flow/optimize.d.ts +7 -0
  57. package/build/src/src/type-flow/sub-type.d.ts +2 -0
  58. package/build/src/src/type-flow/type-flow-util.d.ts +27 -0
  59. package/build/src/src/type-flow/types.d.ts +204 -0
  60. package/build/src/src/type-flow/union-type.d.ts +4 -0
  61. package/build/src/src/type-flow.d.ts +18 -0
  62. package/build/src/src/unused-exprs.d.ts +3 -0
  63. package/build/src/src/util.d.ts +33 -0
  64. package/build/src/src/variable-renamer.d.ts +4 -0
  65. package/build/src/src/visitor.d.ts +5 -0
  66. package/build/src/src/worker-pool.d.ts +4 -0
  67. package/build/src/src/worker-task.d.ts +88 -0
  68. package/build/src/src/worker-thread.d.ts +1 -0
  69. package/build/src/src/xml-util.d.ts +172 -0
  70. package/build/src/test/mocha/bytecode.spec.d.ts +1 -0
  71. package/build/src/test/mocha/coudbe.spec.d.ts +2 -0
  72. package/build/src/test/mocha/intersection.spec.d.ts +2 -0
  73. package/build/src/test/mocha/root.spec.d.ts +1 -0
  74. package/build/src/test/mocha/subtype.spec.d.ts +2 -0
  75. package/build/src/test/mocha/test-utils.d.ts +6 -0
  76. package/build/src/test/mocha/types.spec.d.ts +1 -0
  77. package/build/src/type-flow/interp.d.ts +3 -1
  78. package/build/src/type-flow/types.d.ts +1 -1
  79. package/build/worker-thread.cjs +3 -3
  80. package/package.json +1 -1
package/build/api.cjs CHANGED
@@ -18,44 +18,44 @@ 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_7ANQTFGD.checkCompilerVersion,
22
- collectNamespaces: () => import_chunk_7ANQTFGD.collectNamespaces,
23
- createDocumentationMap: () => import_chunk_7ANQTFGD.createDocumentationMap,
24
- diagnostic: () => import_chunk_7ANQTFGD.diagnostic,
25
- diagnosticHelper: () => import_chunk_7ANQTFGD.diagnosticHelper,
26
- findNamesInScope: () => import_chunk_7ANQTFGD.findNamesInScope,
27
- findUsingForNode: () => import_chunk_7ANQTFGD.findUsingForNode,
28
- formatAst: () => import_chunk_7ANQTFGD.formatAst,
29
- formatAstLongLines: () => import_chunk_7ANQTFGD.formatAstLongLines,
30
- getApiFunctionInfo: () => import_chunk_7ANQTFGD.getApiFunctionInfo,
31
- getApiMapping: () => import_chunk_7ANQTFGD.getApiMapping,
32
- getSuperClasses: () => import_chunk_7ANQTFGD.getSuperClasses,
21
+ checkCompilerVersion: () => import_chunk_QBCVUVLD.checkCompilerVersion,
22
+ collectNamespaces: () => import_chunk_QBCVUVLD.collectNamespaces,
23
+ createDocumentationMap: () => import_chunk_QBCVUVLD.createDocumentationMap,
24
+ diagnostic: () => import_chunk_QBCVUVLD.diagnostic,
25
+ diagnosticHelper: () => import_chunk_QBCVUVLD.diagnosticHelper,
26
+ findNamesInScope: () => import_chunk_QBCVUVLD.findNamesInScope,
27
+ findUsingForNode: () => import_chunk_QBCVUVLD.findUsingForNode,
28
+ formatAst: () => import_chunk_QBCVUVLD.formatAst,
29
+ formatAstLongLines: () => import_chunk_QBCVUVLD.formatAstLongLines,
30
+ getApiFunctionInfo: () => import_chunk_QBCVUVLD.getApiFunctionInfo,
31
+ getApiMapping: () => import_chunk_QBCVUVLD.getApiMapping,
32
+ getSuperClasses: () => import_chunk_QBCVUVLD.getSuperClasses,
33
33
  hasProperty: () => import_chunk_MBTLUWXR.hasProperty,
34
- isClassVariable: () => import_chunk_7ANQTFGD.isClassVariable,
35
- isLocal: () => import_chunk_7ANQTFGD.isLocal,
36
- isLookupCandidate: () => import_chunk_7ANQTFGD.isLookupCandidate,
37
- isStateNode: () => import_chunk_7ANQTFGD.isStateNode,
38
- lookupByFullName: () => import_chunk_7ANQTFGD.lookupByFullName,
39
- lookupNext: () => import_chunk_7ANQTFGD.lookupNext,
40
- lookupResultContains: () => import_chunk_7ANQTFGD.lookupResultContains,
41
- lookupWithType: () => import_chunk_7ANQTFGD.lookupWithType,
42
- makeToyboxLink: () => import_chunk_7ANQTFGD.makeToyboxLink,
43
- mapVarDeclsByType: () => import_chunk_7ANQTFGD.mapVarDeclsByType,
44
- markInvokeClassMethod: () => import_chunk_7ANQTFGD.markInvokeClassMethod,
45
- parseSdkVersion: () => import_chunk_7ANQTFGD.parseSdkVersion,
46
- sameLookupResult: () => import_chunk_7ANQTFGD.sameLookupResult,
34
+ isClassVariable: () => import_chunk_QBCVUVLD.isClassVariable,
35
+ isLocal: () => import_chunk_QBCVUVLD.isLocal,
36
+ isLookupCandidate: () => import_chunk_QBCVUVLD.isLookupCandidate,
37
+ isStateNode: () => import_chunk_QBCVUVLD.isStateNode,
38
+ lookupByFullName: () => import_chunk_QBCVUVLD.lookupByFullName,
39
+ lookupNext: () => import_chunk_QBCVUVLD.lookupNext,
40
+ lookupResultContains: () => import_chunk_QBCVUVLD.lookupResultContains,
41
+ lookupWithType: () => import_chunk_QBCVUVLD.lookupWithType,
42
+ makeToyboxLink: () => import_chunk_QBCVUVLD.makeToyboxLink,
43
+ mapVarDeclsByType: () => import_chunk_QBCVUVLD.mapVarDeclsByType,
44
+ markInvokeClassMethod: () => import_chunk_QBCVUVLD.markInvokeClassMethod,
45
+ parseSdkVersion: () => import_chunk_QBCVUVLD.parseSdkVersion,
46
+ sameLookupResult: () => import_chunk_QBCVUVLD.sameLookupResult,
47
47
  traverseAst: () => import_chunk_MBTLUWXR.traverseAst,
48
- variableDeclarationName: () => import_chunk_7ANQTFGD.variableDeclarationName,
49
- visitReferences: () => import_chunk_7ANQTFGD.visitReferences,
50
- visit_resources: () => import_chunk_7ANQTFGD.visit_resources,
51
- visitorNode: () => import_chunk_7ANQTFGD.visitorNode
48
+ variableDeclarationName: () => import_chunk_QBCVUVLD.variableDeclarationName,
49
+ visitReferences: () => import_chunk_QBCVUVLD.visitReferences,
50
+ visit_resources: () => import_chunk_QBCVUVLD.visit_resources,
51
+ visitorNode: () => import_chunk_QBCVUVLD.visitorNode
52
52
  });
53
53
  module.exports = __toCommonJS(api_exports);
54
- var import_chunk_7ANQTFGD = require("./chunk-7ANQTFGD.cjs");
54
+ var import_chunk_QBCVUVLD = require("./chunk-QBCVUVLD.cjs");
55
55
  var import_chunk_PVBIKZYZ = require("./chunk-PVBIKZYZ.cjs");
56
56
  var import_chunk_MBTLUWXR = require("./chunk-MBTLUWXR.cjs");
57
57
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
58
- (0, import_chunk_7ANQTFGD.init_api)();
58
+ (0, import_chunk_QBCVUVLD.init_api)();
59
59
  // Annotate the CommonJS export names for ESM import in node:
60
60
  0 && (module.exports = {
61
61
  checkCompilerVersion,
@@ -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_7ANQTFGD_exports = {};
30
- __export(chunk_7ANQTFGD_exports, {
29
+ var chunk_QBCVUVLD_exports = {};
30
+ __export(chunk_QBCVUVLD_exports, {
31
31
  EnumTagsConst: () => EnumTagsConst,
32
32
  LastTypeTag: () => LastTypeTag,
33
33
  ObjectLikeTagsConst: () => ObjectLikeTagsConst,
@@ -122,7 +122,7 @@ __export(chunk_7ANQTFGD_exports, {
122
122
  visitorNode: () => visitorNode,
123
123
  xml_util_exports: () => xml_util_exports
124
124
  });
125
- module.exports = __toCommonJS(chunk_7ANQTFGD_exports);
125
+ module.exports = __toCommonJS(chunk_QBCVUVLD_exports);
126
126
  var import_chunk_PVBIKZYZ = require("./chunk-PVBIKZYZ.cjs");
127
127
  var import_chunk_MBTLUWXR = require("./chunk-MBTLUWXR.cjs");
128
128
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
@@ -11724,19 +11724,70 @@ function getLhsConstraint(istate, node) {
11724
11724
  throw new Error("Checking types without a typeMap");
11725
11725
  }
11726
11726
  const object = istate.typeMap.get(node.object);
11727
- if (object && !node.computed) {
11728
- const [, trueDecls2] = findObjectDeclsByProperty(
11729
- istate.state,
11730
- object,
11731
- node
11732
- );
11733
- if (trueDecls2) {
11734
- lookupDefs = lookupNext(
11727
+ if (object) {
11728
+ if (node.computed) {
11729
+ if (object.value) {
11730
+ let result = null;
11731
+ if (object.type & 512) {
11732
+ const arr = getUnionComponent(
11733
+ object,
11734
+ 512
11735
+ /* Array */
11736
+ );
11737
+ if (arr) {
11738
+ result = arr;
11739
+ }
11740
+ }
11741
+ if (object.type & 1024) {
11742
+ const dict = getUnionComponent(
11743
+ object,
11744
+ 1024
11745
+ /* Dictionary */
11746
+ );
11747
+ if (dict) {
11748
+ if (result) {
11749
+ result = cloneType(result);
11750
+ unionInto(result, dict.value);
11751
+ } else {
11752
+ result = dict.value;
11753
+ }
11754
+ }
11755
+ }
11756
+ if (object.type & 32768) {
11757
+ const obj = getUnionComponent(
11758
+ object,
11759
+ 32768
11760
+ /* Object */
11761
+ );
11762
+ if (obj && isByteArrayData(obj)) {
11763
+ const t = {
11764
+ type: 8 | 128
11765
+ /* Char */
11766
+ };
11767
+ if (result) {
11768
+ unionInto(t, result);
11769
+ }
11770
+ result = t;
11771
+ }
11772
+ }
11773
+ if (result) {
11774
+ return result;
11775
+ }
11776
+ }
11777
+ } else {
11778
+ const [, trueDecls2] = findObjectDeclsByProperty(
11735
11779
  istate.state,
11736
- [{ parent: null, results: trueDecls2 }],
11737
- "decls",
11738
- node.property
11780
+ object,
11781
+ node
11739
11782
  );
11783
+ if (trueDecls2) {
11784
+ lookupDefs = lookupNext(
11785
+ istate.state,
11786
+ [{ parent: null, results: trueDecls2 }],
11787
+ "decls",
11788
+ node.property
11789
+ );
11790
+ }
11740
11791
  }
11741
11792
  }
11742
11793
  }
@@ -11842,7 +11893,10 @@ function evaluateNode(istate, node) {
11842
11893
  });
11843
11894
  return;
11844
11895
  }
11845
- if (istate.checkTypes && !couldBe(left.value, right.value)) {
11896
+ if (istate.checkTypes && !couldBe(left.value, right.value) && !subtypeOf({
11897
+ type: 1 | 32768
11898
+ /* Object */
11899
+ }, right.value)) {
11846
11900
  diagnostic(
11847
11901
  istate.state,
11848
11902
  node,
@@ -12077,26 +12131,39 @@ function evaluateNode(istate, node) {
12077
12131
  if (!isLookupCandidate(node)) {
12078
12132
  const property = popIstate(istate, node.property);
12079
12133
  const object = popIstate(istate, node.object);
12080
- if (hasValue(object.value) && object.value.type === 512 && property.value.type & (8 | 16)) {
12081
- push({
12082
- value: object.value.value,
12083
- embeddedEffects: object.embeddedEffects || property.embeddedEffects,
12084
- node
12085
- });
12086
- break;
12087
- }
12088
- if (hasValue(object.value) && object.value.type === 1024 && property.value.type & object.value.value.key.type) {
12089
- const value2 = {
12090
- type: 1
12091
- /* Null */
12092
- };
12093
- unionInto(value2, object.value.value.value);
12094
- push({
12095
- value: value2,
12096
- embeddedEffects: object.embeddedEffects || property.embeddedEffects,
12097
- node
12098
- });
12099
- break;
12134
+ if (hasValue(object.value)) {
12135
+ if (object.value.type === 512 && property.value.type & (8 | 16)) {
12136
+ push({
12137
+ value: object.value.value,
12138
+ embeddedEffects: object.embeddedEffects || property.embeddedEffects,
12139
+ node
12140
+ });
12141
+ break;
12142
+ }
12143
+ if (object.value.type === 1024 && property.value.type & object.value.value.key.type) {
12144
+ const value2 = {
12145
+ type: 1
12146
+ /* Null */
12147
+ };
12148
+ unionInto(value2, object.value.value.value);
12149
+ push({
12150
+ value: value2,
12151
+ embeddedEffects: object.embeddedEffects || property.embeddedEffects,
12152
+ node
12153
+ });
12154
+ break;
12155
+ }
12156
+ if (property.value.type & (8 | 16) && isByteArray(object.value)) {
12157
+ push({
12158
+ value: {
12159
+ type: 8
12160
+ /* Number */
12161
+ },
12162
+ embeddedEffects: object.embeddedEffects || property.embeddedEffects,
12163
+ node
12164
+ });
12165
+ break;
12166
+ }
12100
12167
  }
12101
12168
  push({
12102
12169
  value: {
@@ -12400,6 +12467,15 @@ function mustBeIdentical(a, b) {
12400
12467
  }
12401
12468
  return false;
12402
12469
  }
12470
+ function isByteArray(object) {
12471
+ return hasValue(object) && object.type === 32768 && isByteArrayData(object.value);
12472
+ }
12473
+ function isByteArrayData(objectData) {
12474
+ return objectData.klass.value && (0, import_chunk_PVBIKZYZ.every)(
12475
+ objectData.klass.value,
12476
+ (klass) => klass.fullName === "$.Toybox.Lang.ByteArray"
12477
+ );
12478
+ }
12403
12479
  var init_interp = (0, import_chunk_ABYVSU2C.__esm)({
12404
12480
  "src/type-flow/interp.ts"() {
12405
12481
  "use strict";
@@ -12407,6 +12483,7 @@ var init_interp = (0, import_chunk_ABYVSU2C.__esm)({
12407
12483
  (0, import_chunk_MBTLUWXR.init_ast)();
12408
12484
  init_data_flow();
12409
12485
  init_optimizer_types();
12486
+ (0, import_chunk_PVBIKZYZ.init_util)();
12410
12487
  init_could_be();
12411
12488
  init_interp_binary();
12412
12489
  init_interp_call();
@@ -14172,7 +14249,14 @@ function propagateTypes(state, func, graph, optimizeEquivalencies, copyPropStore
14172
14249
  next = findNextObjectType(istate, trueDecls, me);
14173
14250
  }
14174
14251
  } else {
14175
- if (cur.type & (4096 | 16384 | 32768)) {
14252
+ let byteArray = false;
14253
+ if (cur.type & 32768 && cur.value) {
14254
+ const odata = getObjectValue(cur);
14255
+ if (odata && isByteArrayData(odata)) {
14256
+ byteArray = true;
14257
+ }
14258
+ }
14259
+ if (cur.type & (4096 | 16384 | (byteArray ? 0 : 32768))) {
14176
14260
  next = {
14177
14261
  type: 524287
14178
14262
  /* Any */
@@ -14181,6 +14265,16 @@ function propagateTypes(state, func, graph, optimizeEquivalencies, copyPropStore
14181
14265
  updateAny = true;
14182
14266
  }
14183
14267
  } else {
14268
+ if (byteArray) {
14269
+ const t = {
14270
+ type: 8
14271
+ /* Number */
14272
+ };
14273
+ if (next) {
14274
+ unionInto(t, next);
14275
+ }
14276
+ next = t;
14277
+ }
14184
14278
  if (cur.type & 512) {
14185
14279
  const avalue = getUnionComponent(
14186
14280
  cur,
@@ -23752,7 +23846,7 @@ function simpleOpts(func, context) {
23752
23846
  return Array.from(func.blocks.values()).reduce((changes, block) => {
23753
23847
  for (let i = block.bytecodes.length; i--; ) {
23754
23848
  const cur = block.bytecodes[i];
23755
- if (cur.op === 0 || context.config.removeArgc && cur.op === 53) {
23849
+ if (cur.op === 0 || cur.op === 1 && cur.arg === 0 || context.config.removeArgc && cur.op === 53) {
23756
23850
  block.bytecodes.splice(i, 1);
23757
23851
  changes = true;
23758
23852
  if (logging3) {
@@ -23770,6 +23864,18 @@ function simpleOpts(func, context) {
23770
23864
  logging3 && (0, import_chunk_PVBIKZYZ.log)(`${func.name}: deleting no-op shift (${shift})`);
23771
23865
  continue;
23772
23866
  }
23867
+ if (shift === 1n && prev.op === 37) {
23868
+ const dup = prev;
23869
+ dup.op = 46;
23870
+ dup.arg = 0;
23871
+ dup.size = 2;
23872
+ const add = cur;
23873
+ add.op = 3;
23874
+ delete add.arg;
23875
+ add.size = 1;
23876
+ logging3 && (0, import_chunk_PVBIKZYZ.log)(`${func.name}: converting "ipush 1; shlv" to "dup 0; addv"`);
23877
+ continue;
23878
+ }
23773
23879
  if (shift < (prev.op === 49 ? 64n : 31n)) {
23774
23880
  const mul = 1n << shift;
23775
23881
  if (prev.op === 37) {
@@ -25368,11 +25474,15 @@ var init_symbols = (0, import_chunk_ABYVSU2C.__esm)({
25368
25474
  const { startPc, name, parent } = functionEntry.attr;
25369
25475
  if (!startPc || !name)
25370
25476
  return;
25477
+ const fullPc = Number(startPc.value.value);
25478
+ if (fullPc >>> 28 !== 1) {
25479
+ return;
25480
+ }
25481
+ const pc = fullPc & 16777215;
25371
25482
  const argc = functionEntry.children?.filter(
25372
25483
  (node) => node.type === "element" && node.name === "param"
25373
25484
  ).length || null;
25374
25485
  const fullName = (parent ? debugXml.processRefs(parent.value.value) + "." : "") + debugXml.processRefs(name.value.value);
25375
- const pc = Number(startPc.value.value) & 16777215;
25376
25486
  this.methods.set(pc, {
25377
25487
  name: fullName,
25378
25488
  id: null,
@@ -29907,7 +30017,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
29907
30017
  const opt_time = await (0, import_chunk_PVBIKZYZ.first_modified)(
29908
30018
  Object.values(fnMap).map((v) => v.output)
29909
30019
  );
29910
- if (source_time < opt_time && 1690936956222 < opt_time) {
30020
+ if (source_time < opt_time && 1692125151130 < opt_time) {
29911
30021
  return {
29912
30022
  hasTests,
29913
30023
  diagnostics: prevDiagnostics,
@@ -29945,7 +30055,7 @@ async function generateOneConfig(buildConfig, manifestXML, dependencyFiles, conf
29945
30055
  hasTests: hasTests2,
29946
30056
  diagnostics,
29947
30057
  sdkVersion,
29948
- optimizerVersion: "1.1.33",
30058
+ optimizerVersion: "1.1.34",
29949
30059
  ...Object.fromEntries(
29950
30060
  configOptionsToCheck.map((option) => [option, config[option]])
29951
30061
  )
@@ -30386,14 +30496,17 @@ async function optimizeProgram(filepath, devKey, output, config) {
30386
30496
  if (!output) {
30387
30497
  output = removeExt(filepath, ".prg") + ".opt.prg";
30388
30498
  }
30389
- const xmlBytes = await fs7.readFile(filepath + ".debug.xml").catch(() => "");
30390
- const debugXml = xml_util_exports.parseXml(xmlBytes.toString());
30391
- const key = devKey ? await getDevKey(devKey) : void 0;
30392
- const view = new DataView((await fs7.readFile(filepath)).buffer);
30393
- const { signature, buffer } = await optimizeProgramBuffer(
30499
+ const [debugXml, apiDebugXml, key, view] = await Promise.all([
30500
+ fs7.readFile(filepath + ".debug.xml").catch(() => "").then((xmlBytes) => xml_util_exports.parseXml(xmlBytes.toString())),
30501
+ getSdkPath().then((sdk) => fs7.readFile(`${sdk}bin/api.debug.xml`)).catch(() => "").then((xmlBytes) => xml_util_exports.parseXml(xmlBytes.toString())),
30502
+ devKey ? getDevKey(devKey) : void 0,
30503
+ fs7.readFile(filepath).then((prgData) => new DataView(prgData.buffer))
30504
+ ]);
30505
+ const { signature, buffer } = optimizeProgramBuffer(
30394
30506
  filepath,
30395
30507
  view,
30396
30508
  debugXml,
30509
+ apiDebugXml,
30397
30510
  key,
30398
30511
  config
30399
30512
  );
@@ -30414,7 +30527,7 @@ async function optimizeProgram(filepath, devKey, output, config) {
30414
30527
  await Promise.all(promises);
30415
30528
  return { signature, output };
30416
30529
  }
30417
- function optimizeProgramBuffer(filepath, view, debugXml, key, config) {
30530
+ function optimizeProgramBuffer(filepath, view, debugXml, apiDebugXml, key, config) {
30418
30531
  const { sections } = readPrgWithOffsets(view);
30419
30532
  (0, import_chunk_PVBIKZYZ.logger)("readprg", 5, sections);
30420
30533
  const symbolTable = new SymbolTable();
@@ -30425,6 +30538,9 @@ function optimizeProgramBuffer(filepath, view, debugXml, key, config) {
30425
30538
  ].view);
30426
30539
  }
30427
30540
  symbolTable.parseXml(debugXml);
30541
+ if (apiDebugXml) {
30542
+ symbolTable.parseXml(apiDebugXml);
30543
+ }
30428
30544
  const header = parseHeader(sections[
30429
30545
  -805253120
30430
30546
  /* HEADER */
@@ -30675,6 +30791,7 @@ function optimizePrgAndDebug(prgName, prgBuffer, prgOffset, prgLength, xmlName,
30675
30791
  prgName,
30676
30792
  new DataView(prgBuffer, prgOffset, prgLength),
30677
30793
  debugXml,
30794
+ null,
30678
30795
  key,
30679
30796
  config
30680
30797
  );
@@ -18,29 +18,29 @@ 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_7ANQTFGD.StateNodeAttributes,
22
- buildOptimizedProject: () => import_chunk_7ANQTFGD.buildOptimizedProject,
21
+ StateNodeAttributes: () => import_chunk_QBCVUVLD.StateNodeAttributes,
22
+ buildOptimizedProject: () => import_chunk_QBCVUVLD.buildOptimizedProject,
23
23
  copyRecursiveAsNeeded: () => import_chunk_PVBIKZYZ.copyRecursiveAsNeeded,
24
- defaultConfig: () => import_chunk_7ANQTFGD.defaultConfig,
25
- display: () => import_chunk_7ANQTFGD.display,
26
- generateApiMirTests: () => import_chunk_7ANQTFGD.generateApiMirTests,
27
- generateOneConfig: () => import_chunk_7ANQTFGD.generateOneConfig,
28
- generateOptimizedProject: () => import_chunk_7ANQTFGD.generateOptimizedProject,
29
- getConfig: () => import_chunk_7ANQTFGD.getConfig,
30
- getProjectAnalysis: () => import_chunk_7ANQTFGD.getProjectAnalysis,
31
- get_jungle: () => import_chunk_7ANQTFGD.get_jungle,
32
- isErrorWithLocation: () => import_chunk_7ANQTFGD.isErrorWithLocation,
33
- launchSimulator: () => import_chunk_7ANQTFGD.launchSimulator,
34
- manifestProducts: () => import_chunk_7ANQTFGD.manifestProducts,
35
- mctree: () => import_chunk_7ANQTFGD.mctree,
36
- simulateProgram: () => import_chunk_7ANQTFGD.simulateProgram
24
+ defaultConfig: () => import_chunk_QBCVUVLD.defaultConfig,
25
+ display: () => import_chunk_QBCVUVLD.display,
26
+ generateApiMirTests: () => import_chunk_QBCVUVLD.generateApiMirTests,
27
+ generateOneConfig: () => import_chunk_QBCVUVLD.generateOneConfig,
28
+ generateOptimizedProject: () => import_chunk_QBCVUVLD.generateOptimizedProject,
29
+ getConfig: () => import_chunk_QBCVUVLD.getConfig,
30
+ getProjectAnalysis: () => import_chunk_QBCVUVLD.getProjectAnalysis,
31
+ get_jungle: () => import_chunk_QBCVUVLD.get_jungle,
32
+ isErrorWithLocation: () => import_chunk_QBCVUVLD.isErrorWithLocation,
33
+ launchSimulator: () => import_chunk_QBCVUVLD.launchSimulator,
34
+ manifestProducts: () => import_chunk_QBCVUVLD.manifestProducts,
35
+ mctree: () => import_chunk_QBCVUVLD.mctree,
36
+ simulateProgram: () => import_chunk_QBCVUVLD.simulateProgram
37
37
  });
38
38
  module.exports = __toCommonJS(optimizer_exports);
39
- var import_chunk_7ANQTFGD = require("./chunk-7ANQTFGD.cjs");
39
+ var import_chunk_QBCVUVLD = require("./chunk-QBCVUVLD.cjs");
40
40
  var import_chunk_PVBIKZYZ = require("./chunk-PVBIKZYZ.cjs");
41
41
  var import_chunk_MBTLUWXR = require("./chunk-MBTLUWXR.cjs");
42
42
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
43
- (0, import_chunk_7ANQTFGD.init_optimizer)();
43
+ (0, import_chunk_QBCVUVLD.init_optimizer)();
44
44
  // Annotate the CommonJS export names for ESM import in node:
45
45
  0 && (module.exports = {
46
46
  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_7ANQTFGD.SectionKinds,
22
- appSupport: () => import_chunk_7ANQTFGD.appSupport,
23
- connectiq: () => import_chunk_7ANQTFGD.connectiq,
24
- getDeviceInfo: () => import_chunk_7ANQTFGD.getDeviceInfo,
25
- getFunctionDocumentation: () => import_chunk_7ANQTFGD.getFunctionDocumentation,
26
- getLanguages: () => import_chunk_7ANQTFGD.getLanguages,
27
- getSdkPath: () => import_chunk_7ANQTFGD.getSdkPath,
28
- isWin: () => import_chunk_7ANQTFGD.isWin,
29
- optimizeProgram: () => import_chunk_7ANQTFGD.optimizeProgram,
30
- readPrg: () => import_chunk_7ANQTFGD.readPrg,
31
- readPrgWithOffsets: () => import_chunk_7ANQTFGD.readPrgWithOffsets,
32
- xmlUtil: () => import_chunk_7ANQTFGD.xml_util_exports
21
+ SectionKinds: () => import_chunk_QBCVUVLD.SectionKinds,
22
+ appSupport: () => import_chunk_QBCVUVLD.appSupport,
23
+ connectiq: () => import_chunk_QBCVUVLD.connectiq,
24
+ getDeviceInfo: () => import_chunk_QBCVUVLD.getDeviceInfo,
25
+ getFunctionDocumentation: () => import_chunk_QBCVUVLD.getFunctionDocumentation,
26
+ getLanguages: () => import_chunk_QBCVUVLD.getLanguages,
27
+ getSdkPath: () => import_chunk_QBCVUVLD.getSdkPath,
28
+ isWin: () => import_chunk_QBCVUVLD.isWin,
29
+ optimizeProgram: () => import_chunk_QBCVUVLD.optimizeProgram,
30
+ readPrg: () => import_chunk_QBCVUVLD.readPrg,
31
+ readPrgWithOffsets: () => import_chunk_QBCVUVLD.readPrgWithOffsets,
32
+ xmlUtil: () => import_chunk_QBCVUVLD.xml_util_exports
33
33
  });
34
34
  module.exports = __toCommonJS(sdk_util_exports);
35
- var import_chunk_7ANQTFGD = require("./chunk-7ANQTFGD.cjs");
35
+ var import_chunk_QBCVUVLD = require("./chunk-QBCVUVLD.cjs");
36
36
  var import_chunk_PVBIKZYZ = require("./chunk-PVBIKZYZ.cjs");
37
37
  var import_chunk_MBTLUWXR = require("./chunk-MBTLUWXR.cjs");
38
38
  var import_chunk_ABYVSU2C = require("./chunk-ABYVSU2C.cjs");
39
- (0, import_chunk_7ANQTFGD.init_sdk_util)();
39
+ (0, import_chunk_QBCVUVLD.init_sdk_util)();
40
40
  // Annotate the CommonJS export names for ESM import in node:
41
41
  0 && (module.exports = {
42
42
  SectionKinds,
@@ -26,7 +26,7 @@ export declare function findNamesInScope(declStack: StateNode[][], pattern: stri
26
26
  readonly parent: StateNode;
27
27
  readonly depth: number;
28
28
  }])[];
29
- export declare function mapVarDeclsByType(state: ProgramStateAnalysis, decls: StateNodeDecl[], node: mctree.Node, typeMap: TypeMap | null | undefined): (mctree.Identifier | mctree.EnumStringMember | mctree.AsIdentifier | FunctionStateNode | ClassStateNode | ModuleStateNode | ProgramStateNode | VariableStateNode | import("./optimizer-types").BlockStateNode | TypedefStateNode | EnumStateNode)[];
29
+ export declare function mapVarDeclsByType(state: ProgramStateAnalysis, decls: StateNodeDecl[], node: mctree.Node, typeMap: TypeMap | null | undefined): (mctree.Identifier | mctree.EnumStringMember | mctree.AsIdentifier | FunctionStateNode | ModuleStateNode | ClassStateNode | TypedefStateNode | ProgramStateNode | EnumStateNode | import("./optimizer-types").BlockStateNode | VariableStateNode)[];
30
30
  export declare function formatAstLongLines(node: mctree.Node): string;
31
31
  export declare function createDocumentationMap(functionDocumentation: {
32
32
  name: string;
@@ -1,9 +1,7 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="node" />
3
2
  import * as crypto from "node:crypto";
4
3
  import { BuildConfig } from "./optimizer-types";
5
4
  import { SectionInfo } from "./readprg/bytecode";
6
- import { xmlUtil } from "./sdk-util";
7
5
  export declare function readPrg(path: string): Promise<{
8
6
  [k: string]: number;
9
7
  }>;
@@ -16,10 +14,6 @@ export declare function readPrgWithOffsets(view: DataView): {
16
14
  sections: Record<number, SectionInfo>;
17
15
  };
18
16
  export declare function optimizeProgram(filepath: string, devKey?: string | undefined, output?: string, config?: BuildConfig): Promise<unknown>;
19
- export declare function optimizeProgramBuffer(filepath: string, view: DataView, debugXml: xmlUtil.Document, key: crypto.KeyObject | undefined, config: BuildConfig | undefined): {
20
- signature: Buffer | undefined;
21
- buffer: Buffer;
22
- };
23
17
  export declare function optimizePrgAndDebug(prgName: string, prgBuffer: ArrayBuffer, prgOffset: number, prgLength: number, xmlName: string, xmlBuffer: ArrayBuffer, xmlOffset: number, xmlLength: number, key: crypto.KeyObject, config: BuildConfig | undefined): Promise<{
24
18
  sigBuffer: ArrayBufferLike | undefined;
25
19
  sigOffset: number | undefined;
@@ -0,0 +1,51 @@
1
+ import { mctree } from "@markw65/prettier-plugin-monkeyc";
2
+ import { hasProperty, traverseAst } from "./ast";
3
+ import { JungleResourceMap } from "./jungles";
4
+ import { ClassStateNode, Diagnostic, DiagnosticType, EnumStateNode, FunctionInfo, FunctionStateNode, LookupDefinition, LookupResult, ModuleStateNode, ProgramState, ProgramStateAnalysis, ProgramStateLive, ProgramStateNode, ProgramStateStack, StateNode, StateNodeDecl, TypedefStateNode, VariableStateNode } from "./optimizer-types";
5
+ import { visit_resources } from "./resources";
6
+ import { xmlUtil } from "./sdk-util";
7
+ import { TypeMap } from "./type-flow/interp";
8
+ export { visitorNode, visitReferences } from "./visitor";
9
+ export { traverseAst, hasProperty, visit_resources };
10
+ export declare function parseSdkVersion(version: string | undefined): number;
11
+ export declare function checkCompilerVersion(version: string, sdkVer: number): boolean | undefined;
12
+ export declare function getApiMapping(state?: ProgramState, resourcesMap?: Record<string, JungleResourceMap>, manifestXML?: xmlUtil.Document): Promise<ProgramStateNode>;
13
+ export declare function isStateNode(node: {
14
+ type: string;
15
+ }): node is StateNode;
16
+ export declare function variableDeclarationName(node: mctree.TypedIdentifier | mctree.InstanceofIdentifier): string;
17
+ type DeclKind = "decls" | "type_decls";
18
+ export declare function sameLookupResult(a: LookupDefinition[], b: LookupDefinition[]): boolean;
19
+ export declare function lookupResultContains(a: LookupDefinition[], b: LookupDefinition[]): boolean;
20
+ export declare function isLookupCandidate(node: mctree.MemberExpression): false | mctree.Identifier;
21
+ export declare function lookupNext(state: ProgramStateLive, results: LookupDefinition[], decls: DeclKind, property: mctree.Identifier): LookupDefinition[] | null;
22
+ export declare function lookupWithType(state: ProgramStateAnalysis, node: mctree.Node, typeMap: TypeMap | undefined | null, nonLocal?: boolean, stack?: ProgramStateStack | null): LookupResult;
23
+ export declare function collectNamespaces(ast: mctree.Program, stateIn?: ProgramState): ProgramStateNode;
24
+ export declare function formatAst(node: mctree.Node, monkeyCSource?: string | null, options?: Record<string, unknown> | null): string;
25
+ export declare function findNamesInScope(declStack: StateNode[][], pattern: string | RegExp): (readonly [StateNodeDecl, {
26
+ readonly parent: StateNode;
27
+ readonly depth: number;
28
+ }])[];
29
+ export declare function mapVarDeclsByType(state: ProgramStateAnalysis, decls: StateNodeDecl[], node: mctree.Node, typeMap: TypeMap | null | undefined): (mctree.Identifier | mctree.EnumStringMember | mctree.AsIdentifier | FunctionStateNode | ClassStateNode | ModuleStateNode | ProgramStateNode | VariableStateNode | import("./optimizer-types").BlockStateNode | TypedefStateNode | EnumStateNode)[];
30
+ export declare function formatAstLongLines(node: mctree.Node): string;
31
+ export declare function createDocumentationMap(functionDocumentation: {
32
+ name: string;
33
+ parent: string;
34
+ doc: string;
35
+ }[]): Promise<Map<string, string>>;
36
+ export declare function makeToyboxLink(result: StateNodeDecl): string | null;
37
+ export declare function lookupByFullName(state: ProgramStateAnalysis, fullName: string): StateNodeDecl[];
38
+ export declare function findUsingForNode(state: ProgramStateLive, stack: ProgramStateStack, i: number, node: mctree.Identifier): ModuleStateNode | {
39
+ name: string;
40
+ decls: {
41
+ parent: StateNode;
42
+ results: StateNodeDecl[];
43
+ };
44
+ }[] | null;
45
+ export declare function getApiFunctionInfo(state: ProgramState, func: FunctionStateNode): FunctionInfo | false;
46
+ export declare function markInvokeClassMethod(state: ProgramStateAnalysis, func: FunctionStateNode): void;
47
+ export declare function isLocal(v: VariableStateNode): boolean;
48
+ export declare function isClassVariable(v: VariableStateNode): boolean;
49
+ export declare function diagnostic(state: ProgramState, node: mctree.Node, message: string | null, type?: DiagnosticType, extra?: Diagnostic["extra"]): void;
50
+ export declare function diagnosticHelper(diagnostics: Record<string, Diagnostic[]>, node: mctree.Node, message: string | null, type: DiagnosticType | undefined, extra: Diagnostic["extra"] | undefined, uniqueLocs: boolean): void;
51
+ export declare function getSuperClasses(klass: ClassStateNode): Set<StateNode> | null;
@@ -0,0 +1,59 @@
1
+ import { mctree } from "@markw65/prettier-plugin-monkeyc";
2
+ import type { xmlUtil } from "./sdk-util";
3
+ export declare function traverseAst(node: mctree.Node, pre?: null | ((node: mctree.Node) => void | null | false | (keyof mctree.NodeAll)[]), post?: (node: mctree.Node, parent: mctree.Node | undefined) => void | null | false | mctree.Node | mctree.Node[], parent?: mctree.Node | undefined): false | void | null | mctree.Node | mctree.Node[];
4
+ export declare function isStatement(node: mctree.Node): node is mctree.Statement;
5
+ export declare function isExpression(node: mctree.Node): node is mctree.Expression;
6
+ export declare function mayThrow(node: mctree.Node): boolean;
7
+ export declare function hasProperty<T extends null extends T ? unknown : undefined extends T ? unknown : never>(obj: T, prop: string): obj is NonNullable<T>;
8
+ export declare function hasProperty<T>(obj: T, prop: string): boolean;
9
+ export declare function withLoc<T extends mctree.Node>(node: T, start: mctree.Node | null, end?: mctree.Node | undefined | false): T;
10
+ export declare function withLocDeep<T extends mctree.Node>(node: T, start: mctree.Node | null, end?: mctree.Node | undefined | false, inplace?: boolean): T;
11
+ export declare function cloneDeep<T extends mctree.Node>(node: T): T;
12
+ interface NumberLiteral extends mctree.Literal {
13
+ value: number;
14
+ }
15
+ interface LongLiteral extends mctree.Literal {
16
+ value: number | bigint;
17
+ }
18
+ interface StringLiteral extends mctree.Literal {
19
+ value: string;
20
+ }
21
+ interface CharLiteral extends mctree.Literal {
22
+ value: string;
23
+ }
24
+ interface BooleanLiteral extends mctree.Literal {
25
+ value: boolean;
26
+ }
27
+ interface NullLiteral extends mctree.Literal {
28
+ value: null;
29
+ }
30
+ type LiteralValues = [NumberLiteral, "Number" | "Float" | "Double"] | [LongLiteral, "Long"] | [StringLiteral, "String"] | [CharLiteral, "Char"] | [BooleanLiteral, "Boolean"] | [NullLiteral, "Null"];
31
+ export declare function getNodeValue(node: mctree.Literal): LiteralValues;
32
+ export declare function getNodeValue(node: mctree.Node): LiteralValues | [null, null];
33
+ export declare function wrap<T extends mctree.Node>(node: T, loc?: mctree.SourceLocation | null): T;
34
+ export declare function locRange(start: mctree.SourceLocation, end: mctree.SourceLocation): {
35
+ source: string | null | undefined;
36
+ start: mctree.Position;
37
+ end: mctree.Position;
38
+ };
39
+ export declare function adjustLoc(loc: xmlUtil.SourceLocation, start?: number, end?: number): {
40
+ readonly source: string | null | undefined;
41
+ readonly start: {
42
+ readonly offset: number;
43
+ readonly line: number;
44
+ readonly column: number;
45
+ };
46
+ readonly end: {
47
+ readonly offset: number;
48
+ readonly line: number;
49
+ readonly column: number;
50
+ };
51
+ };
52
+ export declare function makeIdentifier(name: string, loc?: mctree.SourceLocation | null | undefined): {
53
+ type: "Identifier";
54
+ name: string;
55
+ };
56
+ export declare function makeMemberExpression(object: mctree.ScopedName, property: mctree.Identifier): mctree.DottedName;
57
+ export declare function makeScopedName(dotted: string, l?: mctree.SourceLocation): mctree.ScopedName;
58
+ export declare function getLiteralNode(node: mctree.Node | null | undefined): null | mctree.Literal | mctree.AsExpression;
59
+ export {};