embedded-react 0.20.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 (74) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +1 -0
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +16 -46
  5. package/aot/compiler/animations.mjs +299 -57
  6. package/aot/compiler/boot.mjs +27 -2
  7. package/aot/compiler/check.mjs +61 -9
  8. package/aot/compiler/collect.mjs +516 -136
  9. package/aot/compiler/contexts.mjs +391 -0
  10. package/aot/compiler/control-flow.mjs +276 -117
  11. package/aot/compiler/custom-hooks.mjs +686 -0
  12. package/aot/compiler/elements/button.mjs +182 -0
  13. package/aot/compiler/elements/image-background.mjs +76 -0
  14. package/aot/compiler/elements/image.mjs +2 -1
  15. package/aot/compiler/elements/props.mjs +38 -3
  16. package/aot/compiler/elements/rewrite.mjs +118 -0
  17. package/aot/compiler/elements/section-list.mjs +370 -0
  18. package/aot/compiler/expressions.mjs +1351 -97
  19. package/aot/compiler/handlers.mjs +1761 -365
  20. package/aot/compiler/helper-values.mjs +312 -0
  21. package/aot/compiler/ir/nodes.mjs +40 -1
  22. package/aot/compiler/ir/reads.mjs +206 -5
  23. package/aot/compiler/ir/text.mjs +5 -1
  24. package/aot/compiler/ir/types.mjs +12 -0
  25. package/aot/compiler/kept-states.mjs +177 -0
  26. package/aot/compiler/list-items.mjs +226 -0
  27. package/aot/compiler/list-order.mjs +586 -0
  28. package/aot/compiler/list-split.mjs +259 -0
  29. package/aot/compiler/loops.mjs +582 -0
  30. package/aot/compiler/lower.mjs +191 -53
  31. package/aot/compiler/native-files.mjs +439 -0
  32. package/aot/compiler/native-header.mjs +335 -0
  33. package/aot/compiler/native-modules.mjs +1173 -0
  34. package/aot/compiler/nodes.mjs +28 -22
  35. package/aot/compiler/options.mjs +3 -2
  36. package/aot/compiler/out.mjs +14 -1
  37. package/aot/compiler/package/animations.mjs +133 -23
  38. package/aot/compiler/package/assemble.mjs +2 -4
  39. package/aot/compiler/package/bytecode.mjs +37 -8
  40. package/aot/compiler/package/checks.mjs +4 -0
  41. package/aot/compiler/package/codegen.mjs +870 -31
  42. package/aot/compiler/package/enums.mjs +10 -0
  43. package/aot/compiler/package/format.mjs +12 -1
  44. package/aot/compiler/package/lower.mjs +658 -161
  45. package/aot/compiler/package/string-caps.mjs +515 -0
  46. package/aot/compiler/package/tables.mjs +24 -0
  47. package/aot/compiler/package/writer.mjs +22 -0
  48. package/aot/compiler/parse.mjs +3 -1
  49. package/aot/compiler/record-shapes.mjs +246 -0
  50. package/aot/compiler/records.mjs +654 -0
  51. package/aot/compiler/reducers.mjs +272 -0
  52. package/aot/compiler/static-eval.mjs +315 -7
  53. package/aot/compiler/style-text.mjs +18 -3
  54. package/aot/compiler/svg.mjs +7 -4
  55. package/aot/compiler/switch-cases.mjs +198 -0
  56. package/aot/compiler/tagged-records.mjs +450 -0
  57. package/aot/compiler/typing.mjs +307 -0
  58. package/aot/compiler/updaters.mjs +320 -0
  59. package/aot/compiler/window-dimensions.mjs +94 -0
  60. package/aot/compiler/worked-values.mjs +319 -0
  61. package/aot/demo-file.mjs +30 -0
  62. package/aot/style-map.mjs +50 -2
  63. package/build.mjs +9 -1
  64. package/cli.mjs +44 -1
  65. package/hotreload/pack-split.mjs +29 -40
  66. package/pack-container.mjs +9 -1
  67. package/package.json +1 -1
  68. package/sim/embedded-react.wasm +0 -0
  69. package/sim-server.mjs +22 -22
  70. package/src/embedded-react/Dimensions.js +37 -0
  71. package/src/embedded-react/SectionList.js +1 -3
  72. package/src/embedded-react/index.d.ts +83 -3
  73. package/src/embedded-react/index.js +2 -0
  74. package/src/embedded-react/nativeModule.js +462 -0
@@ -17,6 +17,7 @@
17
17
  - Imports
18
18
  ---------------------------------------------------------------------------------------------------------------------*/
19
19
  import { attemptLowering, check } from './check.mjs';
20
+ import { shapeOf } from './typing.mjs';
20
21
  import { BootReadError, bootEnvOf, bootSlotOf, foldBoot, isBoot, } from './boot.mjs';
21
22
  import { lowerStyle, isStyleKey, STYLE_KEYS, DYN_FIELDS, parseColorValue, } from '../style-map.mjs';
22
23
  import { parseColor } from '../../src/embedded-react/svg-ops.js';
@@ -247,7 +248,7 @@ function lowerDynamicStyleValue(key, valueNode, environment) {
247
248
  }
248
249
  // Opacity and every size are numbers.
249
250
  const valueExpression = lowerExpr(valueNode, environment);
250
- if (valueExpression.type.kind === 'string') {
251
+ if (shapeOf(valueExpression) === 'string') {
251
252
  const error = aotError(`AOT: style "${key}" needs a number, but the value is a string`, "JS would coerce the string; the AOT doesn't. Keep the state numeric — useState(10), not useState('10').");
252
253
  if (valueNode.loc) {
253
254
  error.aotLoc = valueNode.loc.start;
@@ -262,6 +263,15 @@ function lowerDynamicStyleValue(key, valueNode, environment) {
262
263
  },
263
264
  ];
264
265
  }
266
+ // A byte field takes a float with its fraction dropped, where a dimension would round it.
267
+ if (dynamicField.kind === 'byte') {
268
+ return [
269
+ {
270
+ field: dynamicField.field,
271
+ value: { kind: 'float', value: valueExpression },
272
+ },
273
+ ];
274
+ }
265
275
  return [
266
276
  {
267
277
  field: dynamicField.field,
@@ -320,6 +330,7 @@ export function bootStyleAssign(key, value) {
320
330
  }
321
331
  const isNumeric = pixelField ||
322
332
  dynamicField?.kind === 'opacity' ||
333
+ dynamicField?.kind === 'byte' ||
323
334
  dynamicField?.kind === 'num';
324
335
  if (!isNumeric)
325
336
  return null;
@@ -331,7 +342,11 @@ export function bootStyleAssign(key, value) {
331
342
  return {
332
343
  field: pixelField ?? dynamicField.field,
333
344
  value: {
334
- kind: dynamicField?.kind === 'opacity' ? 'opacity' : 'dimension',
345
+ kind: dynamicField?.kind === 'opacity'
346
+ ? 'opacity'
347
+ : dynamicField?.kind === 'byte'
348
+ ? 'float'
349
+ : 'dimension',
335
350
  value: { kind: 'boot', slot, type: slot.type },
336
351
  },
337
352
  };
@@ -351,7 +366,7 @@ function lowerBootStyleValue(key, valueNode, environment) {
351
366
  return lowerDynamicStyleValue(key, valueNode, environment);
352
367
  }
353
368
  const value = lowerExpr(valueNode, environment);
354
- if (value.type.kind !== 'int' && value.type.kind !== 'float') {
369
+ if (shapeOf(value) !== 'number') {
355
370
  throw new Error(`AOT: style "${key}" needs a number`);
356
371
  }
357
372
  return [{ field: BOOT_PIXEL_FIELDS[key], value: { kind: 'dimension', value } }];
@@ -16,6 +16,7 @@
16
16
  /*----------------------------------------------------------------------------------------------------------------------
17
17
  - Imports
18
18
  ---------------------------------------------------------------------------------------------------------------------*/
19
+ import { settledOf } from './typing.mjs';
19
20
  import { check, checkNumber } from './check.mjs';
20
21
  import { lowerStyle, NODE_TYPES } from '../style-map.mjs';
21
22
  import { flattenSvg, parseColor, parsePath, KAPPA, PAINT_PROPS, PAINT_STRIDE, scaleVectorArtifact, VOP_ARC, VOP_CUBIC, VOP_LINE, VOP_MOVE, VOP_QUAD, VOP_SHAPE, } from '../../src/embedded-react/svg-ops.js';
@@ -617,9 +618,11 @@ const tapeChain = (first, ...rest) => ({
617
618
  function tapeLiteral(value) {
618
619
  if (value?.kind === 'number')
619
620
  return value.value;
620
- return value?.kind === 'float' && value.value.kind === 'number'
621
- ? value.value.value
622
- : null;
621
+ if (value?.kind !== 'float')
622
+ return null;
623
+ // A value not typed yet is no literal: lowering types a literal at once.
624
+ const settled = settledOf(value.value);
625
+ return settled?.kind === 'number' ? settled.value : null;
623
626
  }
624
627
  /**
625
628
  * An arc's tape slots. The engine measures angles in radians clockwise from 3 o'clock, so each angle is
@@ -1006,7 +1009,7 @@ function lowerSvgBox(nodeId, type, width, height, openingElement, scope, environ
1006
1009
  const { staticAssigns, dynamicAssigns, binds } = collectStyleAssigns(openingElement, scope, environment, elementSpec('Svg'));
1007
1010
  // A value worked out at boot is set once, so only one state drives is refused.
1008
1011
  const readsOnlyBoot = (assign) => (assign.value.kind === 'dimension' || assign.value.kind === 'opacity') &&
1009
- assign.value.value.kind === 'boot';
1012
+ settledOf(assign.value.value)?.kind === 'boot';
1010
1013
  if (!dynamicAssigns.every(readsOnlyBoot)) {
1011
1014
  throw aotError('AOT: a state-driven style on an <Svg> is not supported', 'put the changing style on a wrapping <View>: <View style={…}><Svg …/></View>.');
1012
1015
  }
@@ -0,0 +1,198 @@
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
+ - Imports
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ import { aotError } from './diagnostics.mjs';
20
+ /*----------------------------------------------------------------------------------------------------------------------
21
+ - Implementation
22
+ ---------------------------------------------------------------------------------------------------------------------*/
23
+ /**
24
+ * A case's statements without the `break` that ends them, and whether they end there (a `break` or a `return`)
25
+ * rather than falling through to the next case.
26
+ *
27
+ * @param statements The case's statements.
28
+ *
29
+ * @returns The statements, and whether they end.
30
+ */
31
+ function endOfCase(statements) {
32
+ const last = statements.at(-1);
33
+ if (last?.type === 'BreakStatement' && !last.label) {
34
+ return { statements: statements.slice(0, -1), ends: true };
35
+ }
36
+ if (last?.type === 'ReturnStatement')
37
+ return { statements, ends: true };
38
+ // `case 1: { …; break; }`
39
+ if (last?.type === 'BlockStatement') {
40
+ const inner = endOfCase(last.body);
41
+ return {
42
+ statements: [
43
+ ...statements.slice(0, -1),
44
+ { ...last, body: inner.statements },
45
+ ],
46
+ ends: inner.ends,
47
+ };
48
+ }
49
+ return { statements, ends: false };
50
+ }
51
+ /**
52
+ * Refuses a `break` left in a case's statements, one that does not end the case.
53
+ *
54
+ * @param node The statements, or a node in them.
55
+ */
56
+ function refuseInnerBreak(node) {
57
+ if (!node || typeof node !== 'object')
58
+ return;
59
+ if (Array.isArray(node)) {
60
+ node.forEach(refuseInnerBreak);
61
+ return;
62
+ }
63
+ const statement = node;
64
+ if (statement.type === 'BreakStatement') {
65
+ const error = aotError('AOT: a `break` must be the last statement of its case', 'end the case with break, and write a break that depends on a test as if/else inside the case.');
66
+ if (statement.loc)
67
+ error.aotLoc = statement.loc.start;
68
+ throw error;
69
+ }
70
+ // A nested switch has breaks of its own, and a function's body is not this case's.
71
+ if (statement.type === 'SwitchStatement' ||
72
+ statement.type === 'ArrowFunctionExpression' ||
73
+ statement.type === 'FunctionExpression' ||
74
+ statement.type === 'FunctionDeclaration') {
75
+ return;
76
+ }
77
+ if (statement.type === 'IfStatement') {
78
+ refuseInnerBreak(statement.consequent);
79
+ refuseInnerBreak(statement.alternate);
80
+ }
81
+ else if (statement.type === 'BlockStatement') {
82
+ refuseInnerBreak(statement.body);
83
+ }
84
+ }
85
+ /**
86
+ * Whether an expression reads the same each time it is worked out: a name, a field of one, or a literal.
87
+ *
88
+ * @param node The expression.
89
+ *
90
+ * @returns Whether it does.
91
+ */
92
+ const readsSame = (node) => node.type === 'Identifier' ||
93
+ node.type === 'NumericLiteral' ||
94
+ node.type === 'StringLiteral' ||
95
+ node.type === 'BooleanLiteral' ||
96
+ (node.type === 'MemberExpression' &&
97
+ !node.computed &&
98
+ readsSame(node.object));
99
+ /**
100
+ * A `switch` as the `if … else if …` chain it runs as. Each case tests `discriminant === test`, and runs its own
101
+ * statements then the next cases' until one ends. Empty cases share the next one's statements, and the
102
+ * default is the last `else`. A discriminant that is not a plain read is worked out once, into a `const`.
103
+ *
104
+ * @param statement The switch.
105
+ *
106
+ * @returns The chain.
107
+ */
108
+ export function switchAsIf(statement) {
109
+ if (!readsSame(statement.discriminant)) {
110
+ const name = {
111
+ type: 'Identifier',
112
+ // `@` keeps it from naming anything the app declares.
113
+ name: `switch@${statement.start ?? 0}`,
114
+ };
115
+ return {
116
+ type: 'BlockStatement',
117
+ body: [
118
+ {
119
+ type: 'VariableDeclaration',
120
+ kind: 'const',
121
+ declarations: [
122
+ {
123
+ type: 'VariableDeclarator',
124
+ id: name,
125
+ init: statement.discriminant,
126
+ loc: statement.discriminant.loc,
127
+ },
128
+ ],
129
+ loc: statement.discriminant.loc,
130
+ },
131
+ switchAsIf({ ...statement, discriminant: name }),
132
+ ],
133
+ directives: [],
134
+ };
135
+ }
136
+ const { cases, discriminant } = statement;
137
+ // What each case runs: its own statements, then the next cases' until one ends.
138
+ const runs = cases.map((_, index) => {
139
+ const run = [];
140
+ for (let at = index; at < cases.length; at++) {
141
+ const { statements, ends } = endOfCase(cases[at].consequent);
142
+ run.push(...statements);
143
+ if (ends)
144
+ break;
145
+ }
146
+ refuseInnerBreak(run);
147
+ return run;
148
+ });
149
+ // Empty cases join the next case with a body.
150
+ const groups = [];
151
+ let pending = { tests: [], hasDefault: false, statements: [] };
152
+ cases.forEach((switchCase, index) => {
153
+ if (switchCase.test)
154
+ pending.tests.push(switchCase.test);
155
+ else
156
+ pending.hasDefault = true;
157
+ if (switchCase.consequent.length || index === cases.length - 1) {
158
+ pending.statements = runs[index];
159
+ groups.push(pending);
160
+ pending = { tests: [], hasDefault: false, statements: [] };
161
+ }
162
+ });
163
+ // The default's group runs for every value the other cases miss, its own tests included.
164
+ const fallback = groups.find(group => group.hasDefault);
165
+ const block = (statements) => ({
166
+ type: 'BlockStatement',
167
+ body: statements,
168
+ directives: [],
169
+ });
170
+ let chain = fallback ? block(fallback.statements) : null;
171
+ for (const group of groups.reverse()) {
172
+ if (group.hasDefault || (!fallback && !group.statements.length))
173
+ continue;
174
+ const test = group.tests
175
+ .map((caseTest) => ({
176
+ type: 'BinaryExpression',
177
+ operator: '===',
178
+ left: discriminant,
179
+ right: caseTest,
180
+ loc: caseTest.loc,
181
+ }))
182
+ .reduce((left, right) => ({
183
+ type: 'LogicalExpression',
184
+ operator: '||',
185
+ left,
186
+ right,
187
+ loc: left.loc,
188
+ }));
189
+ chain = {
190
+ type: 'IfStatement',
191
+ test,
192
+ consequent: block(group.statements),
193
+ alternate: chain,
194
+ loc: test.loc,
195
+ };
196
+ }
197
+ return chain ?? block([]);
198
+ }