@markw65/monkeyc-optimizer 1.0.0 → 1.0.1

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 (2) hide show
  1. package/build/optimizer.cjs +94 -27
  2. package/package.json +1 -1
@@ -10741,12 +10741,69 @@ async function copyRecursiveAsNeeded(source, target, filter) {
10741
10741
  }
10742
10742
  }
10743
10743
 
10744
+ ;// CONCATENATED MODULE: ./src/negative-fixups.js
10745
+ /*
10746
+ * This is strange. It pretty much has to be a bug in the sdk,
10747
+ * but its the same in every sdk.
10748
+ *
10749
+ * ${sdk}/bin/api.mir describes the Toybox api down to every module,
10750
+ * class, function, enum and constant, together with the enum
10751
+ * and constant values.
10752
+ *
10753
+ * The problem is that every enum or constant with a negative
10754
+ * value, appears with the corresponding positive value in api.mir.
10755
+ * So eg Graphics.COLOR_TRANSPARENT should be -1, but instead, it has
10756
+ * the value 1.
10757
+ *
10758
+ * This is a (currently somewhat ad-hoc) list of the negative constants
10759
+ * so we can fix them after reading api.mir.
10760
+ */
10761
+ const negativeFixups = [
10762
+ "Toybox.Communications.BLE_CONNECTION_UNAVAILABLE",
10763
+ "Toybox.Communications.INVALID_HTTP_BODY_IN_REQUEST",
10764
+ "Toybox.Communications.REQUEST_CANCELLED",
10765
+ "Toybox.Communications.UNSUPPORTED_CONTENT_TYPE_IN_RESPONSE",
10766
+ "Toybox.Communications.UNABLE_TO_PROCESS_IMAGE",
10767
+ "Toybox.Communications.NETWORK_RESPONSE_OUT_OF_MEMORY",
10768
+ "Toybox.Communications.BLE_REQUEST_CANCELLED",
10769
+ "Toybox.Communications.INVALID_HTTP_METHOD_IN_REQUEST",
10770
+ "Toybox.Communications.BLE_NO_DATA",
10771
+ "Toybox.Communications.INVALID_HTTP_HEADER_FIELDS_IN_REQUEST",
10772
+ "Toybox.Communications.BLE_ERROR",
10773
+ "Toybox.Communications.NETWORK_RESPONSE_TOO_LARGE",
10774
+ "Toybox.Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE",
10775
+ "Toybox.Communications.BLE_REQUEST_TOO_LARGE",
10776
+ "Toybox.Communications.UNABLE_TO_PROCESS_MEDIA",
10777
+ "Toybox.Communications.REQUEST_CONNECTION_DROPPED",
10778
+ "Toybox.Communications.BLE_UNKNOWN_SEND_ERROR",
10779
+ "Toybox.Communications.BLE_QUEUE_FULL",
10780
+ "Toybox.Communications.STORAGE_FULL",
10781
+ "Toybox.Communications.BLE_SERVER_TIMEOUT",
10782
+ "Toybox.Communications.INVALID_HTTP_HEADER_FIELDS_IN_NETWORK_RESPONSE",
10783
+ "Toybox.Communications.SECURE_CONNECTION_REQUIRED",
10784
+ "Toybox.Communications.NETWORK_REQUEST_TIMED_OUT",
10785
+ "Toybox.Communications.BLE_HOST_TIMEOUT",
10786
+ "Toybox.Communications.UNABLE_TO_PROCESS_HLS",
10787
+ "Toybox.Graphics.COLOR_TRANSPARENT",
10788
+ "Toybox.AntPlus.INVALID_SPEED",
10789
+ "Toybox.AntPlus.INVALID_CADENCE",
10790
+ "Toybox.WatchUi.LAYOUT_VALIGN_START",
10791
+ "Toybox.WatchUi.LAYOUT_VALIGN_TOP",
10792
+ "Toybox.WatchUi.LAYOUT_VALIGN_BOTTOM",
10793
+ "Toybox.WatchUi.LAYOUT_VALIGN_CENTER",
10794
+ "Toybox.WatchUi.LAYOUT_HALIGN_RIGHT",
10795
+ "Toybox.WatchUi.LAYOUT_HALIGN_CENTER",
10796
+ "Toybox.WatchUi.LAYOUT_HALIGN_START",
10797
+ "Toybox.WatchUi.LAYOUT_HALIGN_LEFT",
10798
+ ];
10799
+
10744
10800
  ;// CONCATENATED MODULE: ./src/api.js
10745
10801
 
10746
10802
 
10747
10803
 
10748
10804
 
10749
10805
 
10806
+
10750
10807
  const LiteralIntegerRe = /^(0x[0-9a-f]+|\d+)(l)?$/;
10751
10808
  /*
10752
10809
  * This is an unfortunate hack. I want to be able to extract things
@@ -10769,11 +10826,30 @@ async function getApiMapping(state) {
10769
10826
  .toString()
10770
10827
  .replace(/\r\n/g, "\n")
10771
10828
  .replace(/^\s*\[.*?\]\s*$/gm, "")
10772
- .replace(/(COLOR_TRANSPARENT|LAYOUT_[HV]ALIGN_\w+) = (\d+)/gm, "$1 = -$2")
10829
+ //.replace(/(COLOR_TRANSPARENT|LAYOUT_[HV]ALIGN_\w+) = (\d+)/gm, "$1 = -$2")
10773
10830
  .replace(/^(\s*type)\s/gm, "$1def ");
10774
10831
 
10775
10832
  try {
10776
- return collectNamespaces(parser.parse(api, {}), state);
10833
+ const result = collectNamespaces(parser.parse(api, {}), state);
10834
+ negativeFixups.forEach((fixup) => {
10835
+ const value = fixup.split(".").reduce((state, part) => {
10836
+ const decls = state.decls[part];
10837
+ if (!Array.isArray(decls) || decls.length != 1 || !decls[0]) {
10838
+ throw `Failed to find and fix negative constant ${fixup}`;
10839
+ }
10840
+ return decls[0];
10841
+ }, result);
10842
+ if (value.type != "Literal") {
10843
+ throw `Negative constant ${fixup} was not a Literal`;
10844
+ }
10845
+ if (value.value > 0) {
10846
+ value.value = -value.value;
10847
+ value.raw = "-" + value.raw;
10848
+ } else {
10849
+ console.log(`Negative fixup ${fixup} was already negative!`);
10850
+ }
10851
+ });
10852
+ return result;
10777
10853
  } catch (e) {
10778
10854
  console.error(e.toString());
10779
10855
  }
@@ -12162,6 +12238,9 @@ async function buildOptimizedProject(product, options) {
12162
12238
  } else {
12163
12239
  bin = external_path_.join(bin, "exported");
12164
12240
  name = `${program}.iq`;
12241
+ if (!hasProperty(config, "releaseBuild")) {
12242
+ config.releaseBuild = true;
12243
+ }
12165
12244
  }
12166
12245
  config.program = external_path_.join(bin, name);
12167
12246
  return build_project(product, config);
@@ -12293,32 +12372,19 @@ async function generateOneConfig(config) {
12293
12372
  async function generateApiMirTests(options) {
12294
12373
  const config = { ...defaultConfig, ...(options || {}) };
12295
12374
  const tests = [];
12296
- const state = {
12297
- post(node) {
12298
- if (node.type == "EnumDeclaration") {
12299
- const [parent] = state.stack.slice(-1);
12300
- node.body.members.forEach((e) => {
12301
- const name = e.name || e.id.name;
12302
- const values = parent.decls[name];
12303
- if (values == null || values.length != 1) {
12304
- throw "Can't find value for enum";
12305
- }
12306
- const value = values[0];
12307
- tests.push([`${parent.fullName}.${name}`, formatAst(value)]);
12308
- });
12309
- } else if (node.type == "VariableDeclaration" && node.kind == "const") {
12310
- const [parent] = state.stack.slice(-1);
12311
- node.declarations.forEach((decl) =>
12312
- tests.push([
12313
- `${parent.fullName}.${decl.id.name}`,
12314
- formatAst(decl.init),
12315
- ])
12316
- );
12375
+ const api = await getApiMapping();
12376
+ const findConstants = (node) => {
12377
+ Object.entries(node.decls).forEach(([key, decl]) => {
12378
+ if (decl.length > 1) throw `Bad decl length:${node.fullName}.${key}`;
12379
+ if (decl.length != 1) return;
12380
+ if (decl[0].type == "Literal") {
12381
+ tests.push([`${node.fullName}.${key}`, formatAst(decl[0])]);
12382
+ } else if (decl[0].decls) {
12383
+ findConstants(decl[0]);
12317
12384
  }
12318
- },
12385
+ });
12319
12386
  };
12320
- await getApiMapping(state);
12321
-
12387
+ findConstants(api);
12322
12388
  function hasTests(name) {
12323
12389
  const names = name.split(".");
12324
12390
  return names
@@ -12327,8 +12393,9 @@ async function generateApiMirTests(options) {
12327
12393
  .join(" && ");
12328
12394
  }
12329
12395
  const source = [
12396
+ "import Toybox.Lang;",
12330
12397
  "import Toybox.Test;",
12331
- "(:test)",
12398
+ "(:test,:typecheck(false))",
12332
12399
  "function apiTest(logger as Logger) as Boolean {",
12333
12400
  ...tests.map(
12334
12401
  (t) =>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markw65/monkeyc-optimizer",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "description": "Source to source optimizer for Garmin Monkey C code",
6
6
  "main": "build/optimizer.cjs",
7
7
  "scripts": {