embedded-react 0.19.0 → 0.21.0

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 (148) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +8 -3
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +180 -90
  5. package/aot/compiler/animations.mjs +568 -317
  6. package/aot/compiler/boot.mjs +97 -35
  7. package/aot/compiler/branch-merge.mjs +4 -3
  8. package/aot/compiler/check.mjs +70 -18
  9. package/aot/compiler/collect.mjs +787 -288
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +439 -268
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/diagnostics.mjs +22 -25
  14. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  15. package/aot/compiler/elements/button.mjs +182 -0
  16. package/aot/compiler/elements/dial.mjs +91 -77
  17. package/aot/compiler/elements/flat-list.mjs +24 -22
  18. package/aot/compiler/elements/image-background.mjs +76 -0
  19. package/aot/compiler/elements/image.mjs +52 -50
  20. package/aot/compiler/elements/modal.mjs +31 -30
  21. package/aot/compiler/elements/props.mjs +61 -26
  22. package/aot/compiler/elements/rewrite.mjs +118 -0
  23. package/aot/compiler/elements/section-list.mjs +370 -0
  24. package/aot/compiler/elements/switch.mjs +42 -34
  25. package/aot/compiler/elements/text-input.mjs +34 -32
  26. package/aot/compiler/expressions.mjs +1685 -392
  27. package/aot/compiler/handlers.mjs +2029 -547
  28. package/aot/compiler/helper-values.mjs +312 -0
  29. package/aot/compiler/ir/functions.mjs +11 -1
  30. package/aot/compiler/ir/nodes.mjs +43 -4
  31. package/aot/compiler/ir/reads.mjs +223 -22
  32. package/aot/compiler/ir/text.mjs +5 -1
  33. package/aot/compiler/ir/types.mjs +26 -14
  34. package/aot/compiler/ir/vectors.mjs +5 -1
  35. package/aot/compiler/kept-states.mjs +177 -0
  36. package/aot/compiler/keyboard.mjs +17 -7
  37. package/aot/compiler/list-items.mjs +226 -0
  38. package/aot/compiler/list-order.mjs +586 -0
  39. package/aot/compiler/list-split.mjs +259 -0
  40. package/aot/compiler/loops.mjs +582 -0
  41. package/aot/compiler/lower.mjs +246 -111
  42. package/aot/compiler/native-files.mjs +439 -0
  43. package/aot/compiler/native-header.mjs +335 -0
  44. package/aot/compiler/native-modules.mjs +1173 -0
  45. package/aot/compiler/nodes.mjs +195 -172
  46. package/aot/compiler/options.mjs +37 -31
  47. package/aot/compiler/out.mjs +32 -23
  48. package/aot/compiler/package/animations.mjs +779 -0
  49. package/aot/compiler/package/arena.mjs +252 -0
  50. package/aot/compiler/package/assemble.mjs +175 -0
  51. package/aot/compiler/package/bytecode.mjs +315 -0
  52. package/aot/compiler/package/checks.mjs +1026 -0
  53. package/aot/compiler/package/codegen.mjs +2168 -0
  54. package/aot/compiler/package/enums.mjs +148 -0
  55. package/aot/compiler/package/format.mjs +771 -0
  56. package/aot/compiler/package/layouts.mjs +136 -0
  57. package/aot/compiler/package/lower.mjs +2103 -0
  58. package/aot/compiler/package/props.mjs +118 -0
  59. package/aot/compiler/package/string-caps.mjs +515 -0
  60. package/aot/compiler/package/tables.mjs +127 -0
  61. package/aot/compiler/package/vectors.mjs +502 -0
  62. package/aot/compiler/package/writer.mjs +686 -0
  63. package/aot/compiler/pan-responder.mjs +69 -61
  64. package/aot/compiler/parse.mjs +23 -21
  65. package/aot/compiler/record-shapes.mjs +246 -0
  66. package/aot/compiler/records.mjs +654 -0
  67. package/aot/compiler/reducers.mjs +272 -0
  68. package/aot/compiler/static-eval.mjs +340 -30
  69. package/aot/compiler/style-text.mjs +178 -174
  70. package/aot/compiler/svg.mjs +480 -251
  71. package/aot/compiler/switch-cases.mjs +198 -0
  72. package/aot/compiler/tagged-records.mjs +450 -0
  73. package/aot/compiler/typing.mjs +307 -0
  74. package/aot/compiler/updaters.mjs +320 -0
  75. package/aot/compiler/window-dimensions.mjs +94 -0
  76. package/aot/compiler/worked-values.mjs +319 -0
  77. package/aot/demo-file.mjs +30 -0
  78. package/aot/screenshot-smoke.mjs +54 -40
  79. package/aot/style-map.mjs +152 -92
  80. package/assets/bake-font.mjs +123 -77
  81. package/assets/bake-image.mjs +39 -22
  82. package/assets/bake-svg.mjs +361 -262
  83. package/assets/build-builtin-font.mjs +11 -7
  84. package/assets/emit-c.mjs +77 -58
  85. package/assets/emit-container.mjs +123 -37
  86. package/assets/emit-pack.mjs +77 -67
  87. package/assets/font-config.mjs +9 -6
  88. package/assets/font-sizes.mjs +163 -123
  89. package/assets/glyph-coverage.mjs +74 -54
  90. package/assets/index.mjs +31 -9
  91. package/assets/rasterize.mjs +126 -99
  92. package/assets/svg-loader.mjs +8 -8
  93. package/build.mjs +23 -13
  94. package/cli.mjs +402 -172
  95. package/compile-bin.mjs +3 -3
  96. package/flash.mjs +537 -0
  97. package/hotreload/dev-device.mjs +90 -33
  98. package/hotreload/pack-app.mjs +55 -43
  99. package/hotreload/pack-split.mjs +62 -64
  100. package/hotreload/package-transfer.mjs +175 -0
  101. package/hotreload/protocol.mjs +50 -37
  102. package/hotreload/uploader.mjs +38 -27
  103. package/hotreload/watch-tree.mjs +19 -19
  104. package/pack-container.mjs +37 -21
  105. package/package.json +7 -8
  106. package/persist-transform.mjs +34 -28
  107. package/qjsc-wasm.mjs +25 -14
  108. package/sim/embedded-react.cjs +1 -1
  109. package/sim/embedded-react.js +1 -1
  110. package/sim/embedded-react.wasm +0 -0
  111. package/sim/index.html +94 -85
  112. package/sim-server.mjs +113 -94
  113. package/src/embedded-react/Animated.js +24 -24
  114. package/src/embedded-react/Dimensions.js +37 -0
  115. package/src/embedded-react/Easing.js +1 -1
  116. package/src/embedded-react/FlatList.js +3 -3
  117. package/src/embedded-react/LayoutAnimation.js +8 -5
  118. package/src/embedded-react/PanResponder.js +75 -71
  119. package/src/embedded-react/Pressable.js +2 -8
  120. package/src/embedded-react/SectionList.js +7 -9
  121. package/src/embedded-react/StyleSheet.js +4 -3
  122. package/src/embedded-react/TouchableOpacity.js +13 -8
  123. package/src/embedded-react/index.d.ts +88 -8
  124. package/src/embedded-react/index.js +2 -0
  125. package/src/embedded-react/layout-anim-config.js +10 -4
  126. package/src/embedded-react/list-child.js +5 -5
  127. package/src/embedded-react/named-colors.js +1 -1
  128. package/src/embedded-react/nativeModule.js +462 -0
  129. package/src/embedded-react/split-style.js +11 -11
  130. package/src/embedded-react/svg-ops.js +760 -593
  131. package/src/embedded-react/useHostValue.js +3 -3
  132. package/src/embedded-react/usePersistentState.js +6 -6
  133. package/src/embedded-react/warn-props.js +1 -1
  134. package/src/host-config.js +60 -53
  135. package/src/props.js +49 -41
  136. package/src/renderer.js +7 -4
  137. package/aot/compiler/assemble.mjs +0 -944
  138. package/aot/compiler/c/animations.mjs +0 -436
  139. package/aot/compiler/c/boot.mjs +0 -127
  140. package/aot/compiler/c/expressions.mjs +0 -270
  141. package/aot/compiler/c/functions.mjs +0 -268
  142. package/aot/compiler/c/keyboard.mjs +0 -96
  143. package/aot/compiler/c/nodes.mjs +0 -303
  144. package/aot/compiler/c/pan-responder.mjs +0 -136
  145. package/aot/compiler/c/statements.mjs +0 -319
  146. package/aot/compiler/c/text.mjs +0 -83
  147. package/aot/compiler/c/vectors.mjs +0 -381
  148. package/aot/compiler/c-syntax.mjs +0 -127
@@ -24,8 +24,16 @@
24
24
  * @param found What the walk has found.
25
25
  */
26
26
  function readLocal(local, found) {
27
+ if (local.visit || (local.name && found.internal?.has(local.name)))
28
+ return;
27
29
  if (local.list) {
28
- found.states.add(local.list);
30
+ readList(local.list, found);
31
+ if (local.through) {
32
+ readList(local.through, found);
33
+ }
34
+ if (local.index) {
35
+ readExpr(local.index, found);
36
+ }
29
37
  }
30
38
  else if (local.source) {
31
39
  readSource(local.source, found);
@@ -34,6 +42,119 @@ function readLocal(local, found) {
34
42
  found.untracked = true;
35
43
  }
36
44
  }
45
+ /**
46
+ * Records what reading a list depends on. A constant array's rows never change; a list worked out in render is
47
+ * rebuilt from what its source, if it has one, and its tests read, which a state write flags.
48
+ *
49
+ * @param list The list.
50
+ * @param found What the walk has found.
51
+ */
52
+ function readList(list, found) {
53
+ if (list.derived) {
54
+ found.derived ??= new Set();
55
+ if (found.derived.has(list))
56
+ return;
57
+ found.derived.add(list);
58
+ if (list.derived.source) {
59
+ readList(list.derived.source, found);
60
+ }
61
+ list.derived.tests.forEach(test => readExpr(test, found));
62
+ return;
63
+ }
64
+ if (!list.constant) {
65
+ found.states.add(list);
66
+ }
67
+ }
68
+ /**
69
+ * Records what reading a state depends on: a value worked out with a loop is worked out again from what its
70
+ * statements read, which a state write flags.
71
+ *
72
+ * @param state The state.
73
+ * @param found What the walk has found.
74
+ */
75
+ function readState(state, found) {
76
+ if (state.kind === 'list' || !state.worked) {
77
+ found.states.add(state);
78
+ return;
79
+ }
80
+ found.worked ??= new Set();
81
+ if (found.worked.has(state))
82
+ return;
83
+ found.worked.add(state);
84
+ const { rebuild } = state.worked;
85
+ found.internal ??= new Set();
86
+ declaredLocals(rebuild, found.internal);
87
+ readStatements(rebuild, found);
88
+ }
89
+ /**
90
+ * Collects the names of the locals statements declare, at any depth, loops' visit numbers included.
91
+ *
92
+ * @param statements The statements.
93
+ * @param names Collects the names.
94
+ */
95
+ function declaredLocals(statements, names) {
96
+ for (const statement of statements) {
97
+ if (statement.kind === 'declareLocal') {
98
+ names.add(statement.name);
99
+ }
100
+ else if (statement.kind === 'if') {
101
+ declaredLocals(statement.consequent, names);
102
+ declaredLocals(statement.alternate ?? [], names);
103
+ }
104
+ else if (statement.kind === 'block') {
105
+ declaredLocals(statement.body, names);
106
+ }
107
+ else if (statement.kind === 'loop') {
108
+ if (statement.visit?.name)
109
+ names.add(statement.visit.name);
110
+ declaredLocals(statement.body, names);
111
+ declaredLocals(statement.step, names);
112
+ }
113
+ }
114
+ }
115
+ /**
116
+ * Records what the statements a value is worked out with read.
117
+ *
118
+ * @param statements The statements.
119
+ * @param found What the walk has found.
120
+ */
121
+ function readStatements(statements, found) {
122
+ for (const statement of statements) {
123
+ switch (statement.kind) {
124
+ case 'declareLocal':
125
+ readExpr(statement.initializer, found);
126
+ if (statement.text)
127
+ readText(statement.text, found);
128
+ break;
129
+ case 'assignLocal':
130
+ readExpr(statement.value, found);
131
+ if (statement.text)
132
+ readText(statement.text, found);
133
+ break;
134
+ case 'setState':
135
+ readExpr(statement.value, found);
136
+ break;
137
+ case 'setStateText':
138
+ case 'log':
139
+ readText(statement.text, found);
140
+ break;
141
+ case 'if':
142
+ readExpr(statement.test, found);
143
+ readStatements(statement.consequent, found);
144
+ readStatements(statement.alternate ?? [], found);
145
+ break;
146
+ case 'block':
147
+ readStatements(statement.body, found);
148
+ break;
149
+ case 'loop':
150
+ if (statement.test)
151
+ readExpr(statement.test, found);
152
+ readStatements(statement.body, found);
153
+ readStatements(statement.step, found);
154
+ break;
155
+ }
156
+ }
157
+ }
37
158
  /**
38
159
  * Records what an expression, before typing, reads.
39
160
  *
@@ -47,7 +168,7 @@ function readSource(source, found) {
47
168
  case 'boolean':
48
169
  return;
49
170
  case 'state':
50
- found.states.add(source.state);
171
+ readState(source.state, found);
51
172
  return;
52
173
  case 'local':
53
174
  readLocal(source.local, found);
@@ -56,7 +177,7 @@ function readSource(source, found) {
56
177
  found.untracked = true;
57
178
  return;
58
179
  case 'fixed':
59
- readExpr(source.expr, found);
180
+ readExpr(source.expression, found);
60
181
  return;
61
182
  case 'unary':
62
183
  readSource(source.operand, found);
@@ -72,37 +193,38 @@ function readSource(source, found) {
72
193
  readSource(source.alternate, found);
73
194
  return;
74
195
  case 'math':
75
- source.args.forEach(arg => readSource(arg, found));
196
+ source.callArguments.forEach(argument => readSource(argument, found));
76
197
  return;
77
198
  }
78
199
  }
79
200
  /**
80
201
  * Records what an expression reads.
81
202
  *
82
- * @param expr The expression.
203
+ * @param expression The expression.
83
204
  * @param found What the walk has found.
84
205
  */
85
- function readExpr(expr, found) {
86
- switch (expr.kind) {
206
+ function readExpr(expression, found) {
207
+ switch (expression.kind) {
87
208
  case 'number':
88
209
  case 'string':
89
210
  case 'boolean':
90
211
  case 'pi':
91
212
  return;
92
213
  case 'state':
93
- found.states.add(expr.state);
214
+ readState(expression.state, found);
94
215
  return;
95
216
  case 'boot':
96
217
  found.boot = true;
97
218
  return;
98
219
  case 'local':
99
- readLocal(expr.local, found);
220
+ readLocal(expression.local, found);
100
221
  return;
101
222
  case 'listLength':
102
- found.states.add(expr.list);
223
+ readList(expression.list, found);
103
224
  return;
104
225
  case 'itemField':
105
- readLocal(expr.item, found);
226
+ case 'itemPresent':
227
+ readLocal(expression.item, found);
106
228
  return;
107
229
  case 'ref':
108
230
  case 'clock':
@@ -116,11 +238,15 @@ function readExpr(expr, found) {
116
238
  case 'wholeRound':
117
239
  case 'trunc':
118
240
  case 'toInt':
119
- readExpr(expr.operand, found);
241
+ readExpr(expression.operand, found);
120
242
  return;
121
243
  case 'helper':
122
244
  case 'floatMath':
123
- expr.args.forEach(arg => readExpr(arg, found));
245
+ expression.callArguments.forEach(argument => readExpr(argument, found));
246
+ return;
247
+ case 'native':
248
+ found.untracked = true;
249
+ expression.call.callArguments.forEach(argument => readExpr(argument, found));
124
250
  return;
125
251
  case 'arithmetic':
126
252
  case 'floatDivide':
@@ -129,13 +255,21 @@ function readExpr(expr, found) {
129
255
  case 'stringEquals':
130
256
  case 'logical':
131
257
  case 'pick':
132
- readExpr(expr.left, found);
133
- readExpr(expr.right, found);
258
+ readExpr(expression.left, found);
259
+ readExpr(expression.right, found);
260
+ return;
261
+ case 'stringOrder':
262
+ readExpr(expression.left, found);
263
+ readExpr(expression.right, found);
264
+ return;
265
+ case 'stringIncludes':
266
+ readExpr(expression.haystack, found);
267
+ readExpr(expression.needle, found);
134
268
  return;
135
269
  case 'conditional':
136
- readExpr(expr.test, found);
137
- readExpr(expr.consequent, found);
138
- readExpr(expr.alternate, found);
270
+ readExpr(expression.test, found);
271
+ readExpr(expression.consequent, found);
272
+ readExpr(expression.alternate, found);
139
273
  return;
140
274
  }
141
275
  }
@@ -147,7 +281,7 @@ function readExpr(expr, found) {
147
281
  */
148
282
  function readShow(show, found) {
149
283
  if (show.kind === 'rowInUse') {
150
- found.states.add(show.list);
284
+ readList(show.list, found);
151
285
  return;
152
286
  }
153
287
  readExpr(show.test, found);
@@ -201,6 +335,12 @@ function readText(text, found) {
201
335
  for (const part of text.parts) {
202
336
  if (part.kind !== 'literal') {
203
337
  readExpr(part.value, found);
338
+ for (const edit of part.edits ?? []) {
339
+ if (edit.kind === 'slice') {
340
+ readExpr(edit.start, found);
341
+ readExpr(edit.end, found);
342
+ }
343
+ }
204
344
  }
205
345
  }
206
346
  }
@@ -253,7 +393,7 @@ function readPaintColor(color, found) {
253
393
  */
254
394
  const readsOf = (found) => found.untracked ? null : found.states;
255
395
  /**
256
- * The state a node's props, text and image read. app_update() sets them all again together, so this is
396
+ * The state a node's props, text and image read. the update sets them all again together, so this is
257
397
  * everything any of them reads.
258
398
  *
259
399
  * @param node The node.
@@ -262,7 +402,68 @@ const readsOf = (found) => found.untracked ? null : found.states;
262
402
  */
263
403
  export const nodeReads = (node) => readsOf(nodeFound(node));
264
404
  /**
265
- * Whether a node's runtime props read only values worked out at boot, so er_app_build() sets them once.
405
+ * The state an expression reads.
406
+ *
407
+ * @param expression The expression.
408
+ *
409
+ * @returns Its reads.
410
+ */
411
+ export function expressionReads(expression) {
412
+ const found = { states: new Set(), untracked: false };
413
+ readExpr(expression, found);
414
+ return readsOf(found);
415
+ }
416
+ /**
417
+ * What a filtered or sorted list's rebuild reads.
418
+ *
419
+ * @param list The list.
420
+ *
421
+ * @returns Its reads.
422
+ */
423
+ export function listReads(list) {
424
+ const found = { states: new Set(), untracked: false };
425
+ readList(list, found);
426
+ return readsOf(found);
427
+ }
428
+ /**
429
+ * The state a filtered or sorted list's rebuild reads, even beside something untracked.
430
+ *
431
+ * @param list The list.
432
+ *
433
+ * @returns The state.
434
+ */
435
+ export function listStates(list) {
436
+ const found = { states: new Set(), untracked: false };
437
+ readList(list, found);
438
+ return found.states;
439
+ }
440
+ /**
441
+ * What the update reads to work out a value a memo or a helper loops for in render.
442
+ *
443
+ * @param state The value's state.
444
+ *
445
+ * @returns Its reads.
446
+ */
447
+ export function workedReads(state) {
448
+ const found = { states: new Set(), untracked: false };
449
+ readState(state, found);
450
+ return readsOf(found);
451
+ }
452
+ /**
453
+ * The state the update reads to work out a value a memo or a helper loops for in render, even beside something
454
+ * untracked.
455
+ *
456
+ * @param state The value's state.
457
+ *
458
+ * @returns The state.
459
+ */
460
+ export function workedStates(state) {
461
+ const found = { states: new Set(), untracked: false };
462
+ readState(state, found);
463
+ return found.states;
464
+ }
465
+ /**
466
+ * Whether a node's runtime props read only values worked out at boot, so the build sets them once.
266
467
  *
267
468
  * @param node The node.
268
469
  *
@@ -270,7 +471,7 @@ export const nodeReads = (node) => readsOf(nodeFound(node));
270
471
  */
271
472
  export const isBootOnlyNode = (node) => isBootOnly(nodeFound(node));
272
473
  /**
273
- * Whether a drawing reads only values worked out at boot, so er_app_build() builds it once.
474
+ * Whether a drawing reads only values worked out at boot, so the build builds it once.
274
475
  *
275
476
  * @param vector The drawing.
276
477
  *
@@ -13,4 +13,8 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- export {};
16
+ /*----------------------------------------------------------------------------------------------------------------------
17
+ - Constants
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ /** slice's end when the call leaves it out: past any text, so the cut runs to the end. LOADI's largest immediate. */
20
+ export const TEXT_END = 0x7fff;
@@ -13,6 +13,18 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
+ /**
17
+ * What a slot was widened to hold in 64 bits, if it was.
18
+ *
19
+ * @param widths The widths.
20
+ * @param slot The slot's name.
21
+ *
22
+ * @returns What it holds, or undefined for a slot not widened to 64 bits.
23
+ */
24
+ export function wideSlotOf(widths, slot) {
25
+ const width = widths?.get(slot);
26
+ return width === 'float' ? undefined : width;
27
+ }
16
28
  /*----------------------------------------------------------------------------------------------------------------------
17
29
  - Constants
18
30
  ---------------------------------------------------------------------------------------------------------------------*/
@@ -45,7 +57,7 @@ export function slotOf(type) {
45
57
  case 'struct':
46
58
  return undefined;
47
59
  case 'node':
48
- return 'ERNode*'; // a handle, which no number or text slot holds
60
+ return 'node'; // a handle, which no number or text slot holds
49
61
  default:
50
62
  return type.kind;
51
63
  }
@@ -62,12 +74,12 @@ export const int64Of = (source) => source === 'time' ? TIME : source === 'number
62
74
  * The type of a value whose slot is known: a boolean when it holds one, a time when it is a 64-bit slot.
63
75
  *
64
76
  * @param slot The slot's kind.
65
- * @param isBool The value is a JS boolean.
77
+ * @param holdsBoolean The value is a JS boolean.
66
78
  *
67
79
  * @returns The type.
68
80
  */
69
- export function typeOfSlot(slot, isBool = false) {
70
- if (isBool) {
81
+ export function typeOfSlot(slot, holdsBoolean = false) {
82
+ if (holdsBoolean) {
71
83
  if (slot !== 'int') {
72
84
  throw new Error(`AOT internal: a boolean in a "${slot}" slot`);
73
85
  }
@@ -89,20 +101,20 @@ export function typeOfSlot(slot, isBool = false) {
89
101
  return NODE;
90
102
  }
91
103
  /**
92
- * The type of a value lowered to C: its slot, and what the C form records about it.
104
+ * The type of a value from its slot, and what the slot records about it.
93
105
  *
94
106
  * @param value The lowered value.
95
107
  *
96
108
  * @returns The type.
97
109
  */
98
- export function typeOfCExpr(value) {
99
- if (value.lit || value.wide) {
100
- if (value.cType !== 'i64' || (value.lit && value.wide)) {
110
+ export function typeOfSlotValue(value) {
111
+ if (value.isLiteral || value.wide) {
112
+ if (value.slotKind !== 'i64' || (value.isLiteral && value.wide)) {
101
113
  throw new Error('AOT internal: a 64-bit source on a value that is not one 64-bit number');
102
114
  }
103
- return value.lit ? INT64_NUMBER : INT64_MATH;
115
+ return value.isLiteral ? INT64_NUMBER : INT64_MATH;
104
116
  }
105
- return typeOfSlot(value.cType, value.isBool);
117
+ return typeOfSlot(value.slotKind, value.isBoolean);
106
118
  }
107
119
  /**
108
120
  * Whether a value is a time: a 64-bit number computed at runtime from a clock.
@@ -168,9 +180,9 @@ const wideSlotType = (widened) => widened === 'number' ? INT64_NUMBER : TIME;
168
180
  *
169
181
  * @returns Its type.
170
182
  */
171
- export const stateType = (state) => state.cType === 'i64'
183
+ export const stateType = (state) => state.slotKind === 'i64'
172
184
  ? wideSlotType(state.widened)
173
- : typeOfSlot(state.cType, state.isBool);
185
+ : typeOfSlot(state.slotKind, state.isBoolean);
174
186
  /**
175
187
  * The type of a ref's current value.
176
188
  *
@@ -180,6 +192,6 @@ export const stateType = (state) => state.cType === 'i64'
180
192
  */
181
193
  export const refType = (ref) => ref.kind === 'node'
182
194
  ? NODE
183
- : ref.cType === 'i64'
195
+ : ref.slotKind === 'i64'
184
196
  ? wideSlotType(ref.widened)
185
- : typeOfSlot(ref.cType);
197
+ : typeOfSlot(ref.slotKind);
@@ -13,4 +13,8 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- export {};
16
+ /*----------------------------------------------------------------------------------------------------------------------
17
+ - Constants
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ /** The most stops the engine's gradient holds (ER_VECTOR_GRADIENT_MAX_STOPS). */
20
+ export const MAX_GRADIENT_STOPS = 8;
@@ -0,0 +1,177 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /*
17
+ * A handler, effect or timer reads the state its render had: `setCount(count + 1)` leaves `count` as it was for the
18
+ * rest of the body. A setter writes its slot at once, so a body that sets a state and then reads it starts by
19
+ * keeping a copy, which its reads use. An updater's parameter and a reducer's state still read the slot.
20
+ */
21
+ /*----------------------------------------------------------------------------------------------------------------------
22
+ - Imports
23
+ ---------------------------------------------------------------------------------------------------------------------*/
24
+ import { defersTyping, whenTyped } from './check.mjs';
25
+ /*----------------------------------------------------------------------------------------------------------------------
26
+ - Constants
27
+ ---------------------------------------------------------------------------------------------------------------------*/
28
+ /** What a walk doesn't follow: an expression before typing repeats its reads, and the rest hold none. */
29
+ const SKIPPED_KEYS = new Set(['source', 'location', 'readsLatest']);
30
+ /*----------------------------------------------------------------------------------------------------------------------
31
+ - Implementation
32
+ ---------------------------------------------------------------------------------------------------------------------*/
33
+ /**
34
+ * Marks the statements an object state's updater or reducer lowered to, so their reads of its fields give what they
35
+ * hold now.
36
+ *
37
+ * @param statements The statements.
38
+ * @param states The slots of the state set.
39
+ *
40
+ * @returns The statements, marked, and those inside them.
41
+ */
42
+ export function readingLatest(statements, states) {
43
+ if (!states.size)
44
+ return statements;
45
+ // Each is marked in place: the typing pass has yet to fill some in, which a copy would miss.
46
+ return statements.map(statement => {
47
+ const marked = Object.assign(statement, { readsLatest: states });
48
+ switch (marked.kind) {
49
+ case 'if':
50
+ marked.consequent = readingLatest(marked.consequent, states);
51
+ marked.alternate =
52
+ marked.alternate && readingLatest(marked.alternate, states);
53
+ break;
54
+ case 'block':
55
+ case 'updateItems':
56
+ marked.body = readingLatest(marked.body, states);
57
+ break;
58
+ case 'loop':
59
+ marked.body = readingLatest(marked.body, states);
60
+ marked.step = readingLatest(marked.step, states);
61
+ break;
62
+ }
63
+ return marked;
64
+ });
65
+ }
66
+ /**
67
+ * The states a body reads after setting them.
68
+ *
69
+ * @param body The body's statements.
70
+ *
71
+ * @returns The states, in the order they are first read so.
72
+ */
73
+ function statesReadAfterSet(body) {
74
+ let written = new Set();
75
+ const found = new Set();
76
+ const path = new Set();
77
+ /** Every state the statements under a value set. */
78
+ const sets = (value, into) => {
79
+ if (!value || typeof value !== 'object' || path.has(value))
80
+ return;
81
+ if (Array.isArray(value)) {
82
+ value.forEach(element => sets(element, into));
83
+ return;
84
+ }
85
+ const node = value;
86
+ if ('slotName' in node)
87
+ return;
88
+ if (node.kind === 'setState' || node.kind === 'setStateText') {
89
+ into.add(node.state);
90
+ }
91
+ path.add(node);
92
+ for (const [key, child] of Object.entries(node)) {
93
+ if (!SKIPPED_KEYS.has(key))
94
+ sets(child, into);
95
+ }
96
+ path.delete(node);
97
+ };
98
+ const walk = (value, latest) => {
99
+ if (!value || typeof value !== 'object' || path.has(value))
100
+ return;
101
+ if (Array.isArray(value)) {
102
+ value.forEach(element => walk(element, latest));
103
+ return;
104
+ }
105
+ const node = value;
106
+ // A state or a list itself, which a statement names.
107
+ if ('slotName' in node)
108
+ return;
109
+ if (node.kind === 'state' && 'type' in node) {
110
+ const state = node.state;
111
+ if (written.has(state) && !node.latest && !latest?.has(state)) {
112
+ found.add(state);
113
+ }
114
+ return;
115
+ }
116
+ // A field given back as it was reads it, where the body set it before.
117
+ if (node.kind === 'keepField') {
118
+ const state = node.state;
119
+ const live = node.readsLatest ?? latest;
120
+ if (written.has(state) && !live?.has(state))
121
+ found.add(state);
122
+ return;
123
+ }
124
+ const reads = node.readsLatest ?? latest;
125
+ // Only one branch runs, so neither reads what the other set.
126
+ if (node.kind === 'if') {
127
+ walk(node.test, reads);
128
+ // A branch that returns sets nothing for what comes after.
129
+ const returns = (branch) => Array.isArray(branch) && branch.at(-1)?.kind === 'return';
130
+ const before = new Set(written);
131
+ walk(node.consequent, reads);
132
+ const consequent = returns(node.consequent) ? before : written;
133
+ written = new Set(before);
134
+ walk(node.alternate, reads);
135
+ if (returns(node.alternate))
136
+ written = new Set(before);
137
+ consequent.forEach(state => written.add(state));
138
+ return;
139
+ }
140
+ // A loop's later visits read what its earlier ones set.
141
+ if (node.kind === 'loop')
142
+ sets(node, written);
143
+ path.add(node);
144
+ for (const [key, child] of Object.entries(node)) {
145
+ if (!SKIPPED_KEYS.has(key))
146
+ walk(child, reads);
147
+ }
148
+ path.delete(node);
149
+ // A setter's own value reads the state before it is set.
150
+ if (node.kind === 'setState' || node.kind === 'setStateText') {
151
+ written.add(node.state);
152
+ }
153
+ };
154
+ walk(body, undefined);
155
+ return [...found];
156
+ }
157
+ /**
158
+ * Starts a body by keeping a copy of each state it sets and then reads, for those reads.
159
+ *
160
+ * @param body The body's statements.
161
+ * @param output Everything emitted so far; the states kept are added to it.
162
+ *
163
+ * @returns The body, after the copies when it needs any.
164
+ */
165
+ export function keepingStates(body, output) {
166
+ // What the body reads is known once it is typed, so the copies are found then: none leaves nothing to keep.
167
+ const states = [];
168
+ const kept = [{ kind: 'keepStates', states }, ...body];
169
+ whenTyped(() => {
170
+ states.push(...statesReadAfterSet(body));
171
+ states.forEach(state => output.keptStates.add(state));
172
+ if (!states.length && kept[0]?.kind === 'keepStates') {
173
+ kept.shift();
174
+ }
175
+ });
176
+ return defersTyping() || states.length ? kept : body;
177
+ }