embedded-react 0.20.0 → 0.22.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 (140) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +3 -2
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +17 -47
  5. package/aot/compiler/alerts.mjs +604 -0
  6. package/aot/compiler/animations.mjs +299 -57
  7. package/aot/compiler/boot.mjs +27 -2
  8. package/aot/compiler/check.mjs +62 -10
  9. package/aot/compiler/collect.mjs +517 -137
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +276 -117
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/elements/button.mjs +182 -0
  14. package/aot/compiler/elements/image-background.mjs +76 -0
  15. package/aot/compiler/elements/image.mjs +2 -1
  16. package/aot/compiler/elements/props.mjs +47 -8
  17. package/aot/compiler/elements/rewrite.mjs +118 -0
  18. package/aot/compiler/elements/section-list.mjs +370 -0
  19. package/aot/compiler/expressions.mjs +1378 -97
  20. package/aot/compiler/handlers.mjs +1807 -354
  21. package/aot/compiler/helper-values.mjs +312 -0
  22. package/aot/compiler/ir/nodes.mjs +40 -1
  23. package/aot/compiler/ir/reads.mjs +206 -5
  24. package/aot/compiler/ir/text.mjs +5 -1
  25. package/aot/compiler/ir/types.mjs +12 -0
  26. package/aot/compiler/kept-states.mjs +177 -0
  27. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  28. package/aot/compiler/keyboard.mjs +1 -1
  29. package/aot/compiler/list-items.mjs +226 -0
  30. package/aot/compiler/list-order.mjs +586 -0
  31. package/aot/compiler/list-split.mjs +259 -0
  32. package/aot/compiler/loops.mjs +582 -0
  33. package/aot/compiler/lower.mjs +203 -53
  34. package/aot/compiler/native-files.mjs +439 -0
  35. package/aot/compiler/native-header.mjs +335 -0
  36. package/aot/compiler/native-modules.mjs +1173 -0
  37. package/aot/compiler/nodes.mjs +45 -31
  38. package/aot/compiler/options.mjs +3 -2
  39. package/aot/compiler/out.mjs +18 -1
  40. package/aot/compiler/package/animations.mjs +133 -23
  41. package/aot/compiler/package/assemble.mjs +2 -4
  42. package/aot/compiler/package/bytecode.mjs +37 -8
  43. package/aot/compiler/package/checks.mjs +4 -0
  44. package/aot/compiler/package/codegen.mjs +906 -33
  45. package/aot/compiler/package/enums.mjs +10 -0
  46. package/aot/compiler/package/format.mjs +16 -3
  47. package/aot/compiler/package/lower.mjs +707 -176
  48. package/aot/compiler/package/string-caps.mjs +515 -0
  49. package/aot/compiler/package/tables.mjs +24 -0
  50. package/aot/compiler/package/writer.mjs +22 -0
  51. package/aot/compiler/parse.mjs +3 -1
  52. package/aot/compiler/record-shapes.mjs +246 -0
  53. package/aot/compiler/records.mjs +654 -0
  54. package/aot/compiler/reducers.mjs +272 -0
  55. package/aot/compiler/scroll-to.mjs +112 -0
  56. package/aot/compiler/static-eval.mjs +315 -7
  57. package/aot/compiler/style-text.mjs +20 -5
  58. package/aot/compiler/svg.mjs +13 -7
  59. package/aot/compiler/switch-cases.mjs +198 -0
  60. package/aot/compiler/tagged-records.mjs +450 -0
  61. package/aot/compiler/typing.mjs +307 -0
  62. package/aot/compiler/updaters.mjs +321 -0
  63. package/aot/compiler/window-dimensions.mjs +94 -0
  64. package/aot/compiler/worked-values.mjs +319 -0
  65. package/aot/demo-file.mjs +30 -0
  66. package/aot/style-map.mjs +94 -3
  67. package/assets/bake-font.mjs +187 -244
  68. package/assets/bake-image.mjs +47 -71
  69. package/assets/bake-svg.mjs +465 -533
  70. package/assets/build-builtin-font.mjs +14 -27
  71. package/assets/emit-c.mjs +124 -204
  72. package/assets/emit-container.mjs +129 -197
  73. package/assets/emit-pack.mjs +92 -136
  74. package/assets/font-config.mjs +19 -27
  75. package/assets/font-sizes.mjs +432 -507
  76. package/assets/glyph-coverage.mjs +170 -214
  77. package/assets/index.mjs +52 -97
  78. package/assets/rasterize.mjs +173 -194
  79. package/assets/svg-loader.mjs +42 -48
  80. package/build.mjs +85 -94
  81. package/cli.mjs +490 -586
  82. package/compile-bin.mjs +24 -36
  83. package/flash.mjs +297 -401
  84. package/hotreload/dev-device.mjs +229 -270
  85. package/hotreload/pack-app.mjs +118 -159
  86. package/hotreload/pack-split.mjs +146 -213
  87. package/hotreload/package-transfer.mjs +128 -131
  88. package/hotreload/protocol.mjs +118 -118
  89. package/hotreload/uploader.mjs +132 -159
  90. package/hotreload/watch-tree.mjs +56 -65
  91. package/pack-container.mjs +94 -116
  92. package/package.json +19 -16
  93. package/persist-transform.mjs +89 -105
  94. package/qjsc-wasm.mjs +47 -72
  95. package/sim/embedded-react.wasm +0 -0
  96. package/sim/index.html +1 -1
  97. package/sim-server.mjs +260 -354
  98. package/src/embedded-react/Alert.mjs +112 -0
  99. package/src/embedded-react/Animated.mjs +412 -0
  100. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  101. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  102. package/src/embedded-react/Dimensions.mjs +32 -0
  103. package/src/embedded-react/Easing.mjs +34 -0
  104. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  105. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  106. package/src/embedded-react/Keyboard.mjs +82 -0
  107. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  108. package/src/embedded-react/PanResponder.mjs +256 -0
  109. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  110. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  111. package/src/embedded-react/SectionList.mjs +103 -0
  112. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  113. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  114. package/src/embedded-react/background-image.mjs +321 -0
  115. package/src/embedded-react/{components.js → components.mjs} +1 -3
  116. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  117. package/src/embedded-react/index.d.ts +238 -5
  118. package/src/embedded-react/index.js +26 -17
  119. package/src/embedded-react/layout-anim-config.mjs +85 -0
  120. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  121. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  122. package/src/embedded-react/nativeModule.mjs +371 -0
  123. package/src/embedded-react/split-style.mjs +57 -0
  124. package/src/embedded-react/svg-ops.mjs +1075 -0
  125. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  126. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  127. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  128. package/src/host-config.mjs +512 -0
  129. package/src/{native-ui.js → native-ui.mjs} +1 -8
  130. package/src/props.mjs +341 -0
  131. package/src/{renderer.js → renderer.mjs} +30 -44
  132. package/src/embedded-react/Animated.js +0 -440
  133. package/src/embedded-react/Easing.js +0 -39
  134. package/src/embedded-react/PanResponder.js +0 -330
  135. package/src/embedded-react/SectionList.js +0 -148
  136. package/src/embedded-react/layout-anim-config.js +0 -101
  137. package/src/embedded-react/split-style.js +0 -58
  138. package/src/embedded-react/svg-ops.js +0 -1295
  139. package/src/host-config.js +0 -548
  140. package/src/props.js +0 -306
@@ -0,0 +1,94 @@
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
+ * @file compiler/window-dimensions.mts
18
+ * @brief Turns `useWindowDimensions()` and `Dimensions.get('window')` into reads of the panel's size.
19
+ */
20
+ import { traverse } from '@babel/core';
21
+ import { PANEL_KEY } from './boot.mjs';
22
+ import { aotError } from './diagnostics.mjs';
23
+ /*----------------------------------------------------------------------------------------------------------------------
24
+ - Constants
25
+ ---------------------------------------------------------------------------------------------------------------------*/
26
+ /** The package the two are imported from. */
27
+ const PACKAGE = 'embedded-react';
28
+ /** The sizes `Dimensions.get` names, which are the same on a panel. */
29
+ const SIZES = new Set(['window', 'screen']);
30
+ /*----------------------------------------------------------------------------------------------------------------------
31
+ - Implementation
32
+ ---------------------------------------------------------------------------------------------------------------------*/
33
+ /**
34
+ * The package export a name stands for where it is read, or null when it is anything else: a local, a prop or a
35
+ * state of that name hides the import.
36
+ *
37
+ * @param path Where the name is read.
38
+ * @param node The name.
39
+ *
40
+ * @returns The export's name, or null.
41
+ */
42
+ function packageExportOf(path, node) {
43
+ if (node.type !== 'Identifier')
44
+ return null;
45
+ const binding = path.scope.getBinding(node.name);
46
+ const specifier = binding?.path.node;
47
+ const declaration = binding?.path.parent;
48
+ if (specifier?.type !== 'ImportSpecifier' ||
49
+ declaration?.type !== 'ImportDeclaration' ||
50
+ declaration.source.value !== PACKAGE) {
51
+ return null;
52
+ }
53
+ const { imported } = specifier;
54
+ return imported.type === 'Identifier' ? imported.name : imported.value;
55
+ }
56
+ /**
57
+ * Rewrites each `useWindowDimensions()` and `Dimensions.get('window' | 'screen')` to a read of the panel's size,
58
+ * which folds as `screen` does. Only the package's own exports are rewritten, by whatever name they are imported.
59
+ *
60
+ * @param ast The parsed module, rewritten in place.
61
+ *
62
+ * @throws An AotError for any other use of `Dimensions`.
63
+ */
64
+ export function rewriteWindowDimensions(ast) {
65
+ const panel = (node) => ({ type: 'Identifier', name: PANEL_KEY, loc: node.loc });
66
+ traverse.cache.clear();
67
+ traverse(ast, {
68
+ CallExpression(path) {
69
+ const { callee, arguments: callArguments } = path.node;
70
+ if (packageExportOf(path, callee) === 'useWindowDimensions') {
71
+ path.replaceWith(panel(path.node));
72
+ }
73
+ else if (callee.type === 'MemberExpression' &&
74
+ packageExportOf(path, callee.object) === 'Dimensions') {
75
+ const [size] = callArguments;
76
+ const isGet = !callee.computed &&
77
+ callee.property.type === 'Identifier' &&
78
+ callee.property.name === 'get';
79
+ if (!isGet ||
80
+ callArguments.length !== 1 ||
81
+ size.type !== 'StringLiteral' ||
82
+ !SIZES.has(size.value)) {
83
+ const error = aotError("AOT: Dimensions has only get('window') and get('screen')", 'the panel size is fixed while the app runs, so there is nothing else to read or listen to.');
84
+ if (path.node.loc) {
85
+ error.aotLoc = path.node.loc.start;
86
+ }
87
+ throw error;
88
+ }
89
+ path.replaceWith(panel(path.node));
90
+ }
91
+ },
92
+ });
93
+ traverse.cache.clear();
94
+ }
@@ -0,0 +1,319 @@
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
+ * Values worked out with a loop: a block-bodied `useMemo`, or a helper used as a value, whose body has `let` locals
18
+ * or loops. Such a body can't be one expression, so it runs as statements, each `return` setting a local and leaving
19
+ * the body. In a handler, effect or timer it runs just before the statement that reads it. In render it has a state
20
+ * of its own, which the update works out again, before anything that reads it, when what it reads changes.
21
+ */
22
+ /*----------------------------------------------------------------------------------------------------------------------
23
+ - Imports
24
+ ---------------------------------------------------------------------------------------------------------------------*/
25
+ import { holdLater, setLater, shapeOf, shapeOfType, typedLater, } from './typing.mjs';
26
+ import { aotError } from './diagnostics.mjs';
27
+ import { assumeType, assumedTypeOf, buildsText, lowerSource, lowerText, typeSource, } from './expressions.mjs';
28
+ import { lowerStmts } from './handlers.mjs';
29
+ import { helperEnvironment } from './helper-values.mjs';
30
+ import { BOOL, FLOAT, INT, INT64_NUMBER, STRING, TIME } from './ir/types.mjs';
31
+ import { Out } from './out.mjs';
32
+ import { checkValueBody } from './updaters.mjs';
33
+ /*----------------------------------------------------------------------------------------------------------------------
34
+ - Variables
35
+ ---------------------------------------------------------------------------------------------------------------------*/
36
+ /** Numbers each value and each list worked out in render as it is made, so the update works out each in turn. */
37
+ let rebuilds = 0;
38
+ /** Numbers the locals a value is left in. */
39
+ let results = 0;
40
+ /*----------------------------------------------------------------------------------------------------------------------
41
+ - Implementation
42
+ ---------------------------------------------------------------------------------------------------------------------*/
43
+ /**
44
+ * The next number in the order the update works out what render makes: a value worked out with a loop, or a list
45
+ * filtered, sorted or split.
46
+ *
47
+ * @returns The number.
48
+ */
49
+ export const nextRebuild = () => rebuilds++;
50
+ /**
51
+ * Whether statements need to run as statements: they declare a `let` or loop, not counting a function inside them.
52
+ *
53
+ * @param statements The statements.
54
+ *
55
+ * @returns Whether they do.
56
+ */
57
+ function loopsIn(statements) {
58
+ return statements.some(statement => {
59
+ switch (statement?.type) {
60
+ case 'VariableDeclaration':
61
+ return statement.kind !== 'const';
62
+ case 'ForStatement':
63
+ case 'ForOfStatement':
64
+ case 'ForInStatement':
65
+ case 'WhileStatement':
66
+ case 'DoWhileStatement':
67
+ return true;
68
+ case 'BlockStatement':
69
+ return loopsIn(statement.body);
70
+ case 'IfStatement':
71
+ return loopsIn([statement.consequent, statement.alternate]);
72
+ case 'SwitchStatement':
73
+ return statement.cases.some(({ consequent }) => loopsIn(consequent));
74
+ case 'LabeledStatement':
75
+ return loopsIn([statement.body]);
76
+ default:
77
+ return false;
78
+ }
79
+ });
80
+ }
81
+ /**
82
+ * Whether a helper's body loops or declares a `let`, so it runs as statements rather than as one expression.
83
+ *
84
+ * @param helper The helper.
85
+ *
86
+ * @returns Whether it does.
87
+ */
88
+ export const runsAsStatements = (helper) => helper.body.type === 'BlockStatement' && loopsIn(helper.body.body);
89
+ /**
90
+ * What a value-giving body is, for errors: `the useMemo for "total"` or `helper "sum"`.
91
+ *
92
+ * @param name The helper's name.
93
+ *
94
+ * @returns The phrase, and the memo's name for a memo.
95
+ */
96
+ function describe(name) {
97
+ const memo = /^useMemo\((.*)\)$/.exec(name)?.[1] ?? null;
98
+ return {
99
+ what: memo === null ? `helper "${name}"` : `the useMemo for "${memo}"`,
100
+ memo,
101
+ };
102
+ }
103
+ /**
104
+ * The type a local holding every value a body returns has: a string, a boolean, or a number. A number is a float
105
+ * when any is, and 64 bits when any is, a time unless every 64-bit one is a plain number.
106
+ *
107
+ * @param types The returned values' types.
108
+ * @param what What returns them, for errors.
109
+ *
110
+ * @returns The type.
111
+ */
112
+ function joinedType(types, what) {
113
+ const kinds = new Set(types.map(type => type.kind));
114
+ joinedShape(types.map(shapeOfType), what);
115
+ if (kinds.has('string'))
116
+ return STRING;
117
+ if (kinds.has('bool'))
118
+ return BOOL;
119
+ if (kinds.has('int64')) {
120
+ if (kinds.has('float')) {
121
+ throw aotError(`AOT: ${what} loops, so it can't return a 64-bit whole number on one path and a fraction on another`, 'return whole numbers on every path, e.g. Math.round(x), or fractions on every path.');
122
+ }
123
+ return types.every(type => type.kind !== 'int64' || type.source === 'number')
124
+ ? INT64_NUMBER
125
+ : TIME;
126
+ }
127
+ return kinds.has('float') ? FLOAT : INT;
128
+ }
129
+ /**
130
+ * Whether a local holding every value a body returns holds a string, a boolean or a number: each must be one of
131
+ * those, and all the same.
132
+ *
133
+ * @param shapes The returned values' shapes.
134
+ * @param what What returns them, for errors.
135
+ *
136
+ * @returns The shape.
137
+ */
138
+ function joinedShape(shapes, what) {
139
+ const sorts = new Set(shapes);
140
+ if (sorts.has('struct') || sorts.has('node')) {
141
+ throw aotError(`AOT: ${what} loops, so it must return a number, a boolean or a string`, 'return the value itself, e.g. a field of an item rather than the item.');
142
+ }
143
+ if (sorts.size > 1) {
144
+ throw aotError(`AOT: ${what} loops, so each \`return\` must give the same kind of value`, `it returns a ${[...sorts].join(' on one path and a ')} on another. Return one kind on every path.`);
145
+ }
146
+ return shapes[0] ?? 'number';
147
+ }
148
+ /**
149
+ * How a local or a state holding a type is kept.
150
+ *
151
+ * @param type The type.
152
+ *
153
+ * @returns Its slot kind, and whether it is a boolean or a 64-bit plain number.
154
+ */
155
+ const heldAs = (type) => type.kind === 'string'
156
+ ? { slotKind: 'string' }
157
+ : type.kind === 'float'
158
+ ? { slotKind: 'float' }
159
+ : type.kind === 'int64'
160
+ ? {
161
+ slotKind: 'i64',
162
+ ...(type.source === 'number' ? { isLiteral: true } : {}),
163
+ }
164
+ : { slotKind: 'int', ...(type.kind === 'bool' ? { isBoolean: true } : {}) };
165
+ /**
166
+ * A value of a type that a local starts at before a `return` sets it.
167
+ *
168
+ * @param type The type.
169
+ *
170
+ * @returns The value.
171
+ */
172
+ const zeroOf = (type) => type.kind === 'string'
173
+ ? { kind: 'string', value: '', type }
174
+ : type.kind === 'bool'
175
+ ? { kind: 'boolean', value: false, type }
176
+ : { kind: 'number', value: 0, type };
177
+ /**
178
+ * Lowers a looping body to statements, each `return` leaving its value in a local of its own and the body.
179
+ *
180
+ * @param call The call.
181
+ * @param name The helper's name.
182
+ * @param helper The helper.
183
+ * @param environment The caller's environment.
184
+ * @param state The component's state table.
185
+ * @param output Everything emitted so far.
186
+ *
187
+ * @returns The statements and the local.
188
+ */
189
+ function lowerValueStatements(call, name, helper, environment, state, output) {
190
+ const { what, memo } = describe(name);
191
+ checkValueBody(helper.body, what, memo !== null);
192
+ const resultName = `value@${name}#${results++}`;
193
+ const returned = [];
194
+ const context = {
195
+ stateChanged: false,
196
+ output,
197
+ helperList: helper.body.body,
198
+ returnsValue: (argument, bodyEnvironment) => {
199
+ if (buildsText(argument, bodyEnvironment)) {
200
+ const text = lowerText(argument, bodyEnvironment);
201
+ const value = { kind: 'string', value: '', type: STRING };
202
+ returned.push({ value, text });
203
+ return [{ kind: 'assignLocal', name: resultName, value, text }];
204
+ }
205
+ const { source, expression: value } = lowerSource(argument, bodyEnvironment);
206
+ returned.push({ value, source });
207
+ return [{ kind: 'assignLocal', name: resultName, value }];
208
+ },
209
+ };
210
+ const body = lowerStmts(helper.body.body, helperEnvironment(name, helper, call.arguments, environment), state, context);
211
+ // Whether it is a string, a boolean or a number is known now; a number's width once each value is typed.
212
+ const shape = joinedShape(returned.map(({ value, text }) => (text ? 'string' : shapeOf(value))), what);
213
+ const type = () => joinedType(returned.map(({ value, text }) => (text ? STRING : value.type)), what);
214
+ const result = holdLater({ name: resultName }, shape, () => heldAs(type()));
215
+ assumeType(result, () => joinedType(returned.map(({ source }) => source ? typeSource(source, false).type : STRING), what));
216
+ return {
217
+ statements: [
218
+ {
219
+ kind: 'declareLocal',
220
+ name: resultName,
221
+ initializer: typedLater(shape, () => zeroOf(type())),
222
+ isHoisted: false,
223
+ },
224
+ ...(context.usedReturn ? [{ kind: 'block', body }] : body),
225
+ ],
226
+ result,
227
+ shape,
228
+ type,
229
+ };
230
+ }
231
+ /**
232
+ * A call to a helper that loops, used as a value. In a handler, effect or timer, the body runs before the statement
233
+ * that reads it, into a local. In render it is a state of its own, which the update works out again before anything
234
+ * that reads it.
235
+ *
236
+ * @param call The call.
237
+ * @param name The helper's name.
238
+ * @param helper The helper.
239
+ * @param environment The caller's environment.
240
+ *
241
+ * @returns The value.
242
+ */
243
+ export function workedValueOf(call, name, helper, environment) {
244
+ const location = call.loc?.start;
245
+ const { what, memo } = describe(name);
246
+ // In a handler, effect or timer: worked out just before the statement.
247
+ const sink = environment.before;
248
+ if (sink) {
249
+ const { statements, result } = lowerValueStatements(call, name, helper, environment, sink.state, sink.output);
250
+ sink.statements.push(...statements);
251
+ return { kind: 'local', local: result, location };
252
+ }
253
+ if (environment.outsideRender) {
254
+ throw aotError(`AOT: ${what} loops, so it can't be used as a value here`, 'call it from a statement of a handler, an effect or a timer, or from render.');
255
+ }
256
+ if (environment.inListRow) {
257
+ throw aotError(`AOT: ${what} loops, so it can't be used inside a list row`, 'a value worked out with a loop is worked out once, outside the rows. Work it out for the whole list in a ' +
258
+ 'useMemo, or keep it in each item.');
259
+ }
260
+ const values = environment.workedValues;
261
+ if (!values) {
262
+ throw new Error('AOT internal: no table for values worked out with a loop');
263
+ }
264
+ // In render: a state of its own, one per call and component instance. A call inside a helper being inlined may be
265
+ // given other arguments each time, so it gets one each time.
266
+ const prefix = environment.callbackPrefix ?? '';
267
+ const shared = !environment.inlining?.size;
268
+ const known = shared ? values.get(call)?.get(prefix) : undefined;
269
+ if (known)
270
+ return { kind: 'state', state: known, location };
271
+ const { statements, result, shape, type } = lowerValueStatements(call, name, helper, { ...environment, workedInRender: true }, { byName: new Map(environment.state), bySetter: new Map() }, new Out(new Map(), null));
272
+ const count = [...values.values()].reduce((sum, map) => sum + map.size, 0);
273
+ const label = memo ?? name;
274
+ const worked = {
275
+ kind: 'scalar',
276
+ name: label,
277
+ slotName: `${prefix}w${count}_${label}`,
278
+ setter: undefined,
279
+ slotKind: shape === 'string' ? 'string' : 'int',
280
+ isBoolean: shape === 'bool',
281
+ initial: shape === 'string' ? '' : 0,
282
+ host: false,
283
+ initialAt: location,
284
+ };
285
+ if (shape === 'number') {
286
+ setLater(worked, ['slotKind', 'widened'], () => {
287
+ const held = type();
288
+ const widened = held.kind !== 'int64'
289
+ ? undefined
290
+ : held.source === 'number'
291
+ ? 'number'
292
+ : 'time';
293
+ return { slotKind: heldAs(held).slotKind, widened };
294
+ });
295
+ assumeType(worked, () => assumedTypeOf(result));
296
+ }
297
+ const read = typedLater(shape, () => ({
298
+ kind: 'local',
299
+ local: result,
300
+ type: type(),
301
+ }));
302
+ worked.worked = {
303
+ rebuild: [
304
+ ...statements,
305
+ shape === 'string'
306
+ ? {
307
+ kind: 'setStateText',
308
+ state: worked,
309
+ text: { parts: [{ kind: 'value', value: read }] },
310
+ }
311
+ : { kind: 'setState', state: worked, value: read },
312
+ ],
313
+ order: nextRebuild(),
314
+ };
315
+ if (!values.has(call))
316
+ values.set(call, new Map());
317
+ values.get(call).set(shared ? prefix : `${prefix}#${count}`, worked);
318
+ return { kind: 'state', state: worked, location };
319
+ }
@@ -0,0 +1,30 @@
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
+ import { existsSync } from 'node:fs';
17
+ import { resolve } from 'node:path';
18
+ /**
19
+ * A demo's file, in TypeScript or JavaScript: `demoFile(directory, 'App')` is App.tsx when there is one, else
20
+ * App.jsx.
21
+ *
22
+ * @param directory The demo's folder.
23
+ * @param name The file's name, without its extension.
24
+ *
25
+ * @returns The path, which is the .jsx one when neither is there.
26
+ */
27
+ export function demoFile(directory, name) {
28
+ const typed = resolve(directory, `${name}.tsx`);
29
+ return existsSync(typed) ? typed : resolve(directory, `${name}.jsx`);
30
+ }
package/aot/style-map.mjs CHANGED
@@ -16,7 +16,8 @@
16
16
  /*----------------------------------------------------------------------------------------------------------------------
17
17
  - Imports
18
18
  ---------------------------------------------------------------------------------------------------------------------*/
19
- import { NAMED_COLORS } from '../src/embedded-react/named-colors.js';
19
+ import { BACKGROUND_MAX_STOPS, parseBackgroundImage, } from '../src/embedded-react/background-image.mjs';
20
+ import { NAMED_COLORS } from '../src/embedded-react/named-colors.mjs';
20
21
  import { numberProblem } from './compiler/check.mjs';
21
22
  /*----------------------------------------------------------------------------------------------------------------------
22
23
  - Constants
@@ -43,6 +44,11 @@ const FLEX_DIRECTION = {
43
44
  'row-reverse': 'ER_FLEX_ROW_REVERSE',
44
45
  'column-reverse': 'ER_FLEX_COLUMN_REVERSE',
45
46
  };
47
+ const FLEX_WRAP = {
48
+ nowrap: 'ER_WRAP_NOWRAP',
49
+ wrap: 'ER_WRAP_WRAP',
50
+ 'wrap-reverse': 'ER_WRAP_WRAP_REVERSE',
51
+ };
46
52
  const POSITION = {
47
53
  relative: 'ER_POSITION_RELATIVE',
48
54
  absolute: 'ER_POSITION_ABSOLUTE',
@@ -94,6 +100,7 @@ const NUMBER_FIELDS = {
94
100
  fontSize: 'font_size',
95
101
  lineHeight: 'line_height',
96
102
  letterSpacing: 'letter_spacing',
103
+ shadowOpacity: 'shadow_opacity',
97
104
  };
98
105
  /**
99
106
  * Enum style keys that can be state-driven: the value (a string literal or a ternary of them) lowers to the
@@ -101,6 +108,7 @@ const NUMBER_FIELDS = {
101
108
  */
102
109
  const ENUM_FIELDS = {
103
110
  flexDirection: { field: 'flex_direction', table: FLEX_DIRECTION },
111
+ flexWrap: { field: 'flex_wrap', table: FLEX_WRAP },
104
112
  alignItems: { field: 'align_items', table: ALIGN },
105
113
  alignSelf: { field: 'align_self', table: ALIGN },
106
114
  justifyContent: { field: 'justify_content', table: JUSTIFY },
@@ -109,8 +117,8 @@ const ENUM_FIELDS = {
109
117
  };
110
118
  /**
111
119
  * Every style key whose value can be state-driven, with its ERProps field and how an update lowers it (see
112
- * DynamicStyleField). Keys absent here (such as the `flex` shorthand, `flexBasis`, the four insets and
113
- * `fontWeight`) can only be static.
120
+ * DynamicStyleField). Keys absent here (such as the `flex` shorthand, `flexBasis`, the four insets,
121
+ * `fontWeight` and `shadowOffset`) can only be static.
114
122
  */
115
123
  export const DYN_FIELDS = {
116
124
  ...Object.fromEntries(Object.entries(NUMBER_FIELDS).map(([styleKey, field]) => [
@@ -124,12 +132,17 @@ export const DYN_FIELDS = {
124
132
  backgroundColor: { field: 'background_color', kind: 'color' },
125
133
  color: { field: 'color', kind: 'color' },
126
134
  borderColor: { field: 'border_color', kind: 'color' },
135
+ shadowColor: { field: 'shadow_color', kind: 'color' },
136
+ shadowRadius: { field: 'shadow_radius', kind: 'byte' },
137
+ elevation: { field: 'elevation', kind: 'byte' },
127
138
  opacity: { field: 'opacity', kind: 'opacity' },
128
139
  };
129
140
  /** The engine's opacity for fully opaque, which a 0–1 opacity scales to. */
130
141
  export const OPACITY_OPAQUE = 255;
131
142
  /** The alpha of a color written without one. */
132
143
  const ALPHA_OPAQUE = 0xff;
144
+ /** The largest value a byte field holds. */
145
+ const BYTE_MAX = 255;
133
146
  /** The lightest numeric fontWeight drawn bold. */
134
147
  const BOLD_WEIGHT = 600;
135
148
  /*----------------------------------------------------------------------------------------------------------------------
@@ -226,6 +239,28 @@ const dimension = (value) => {
226
239
  // Adding 0 turns -0 into 0, whose bits a float field keeps.
227
240
  return Math.round(value) + 0;
228
241
  };
242
+ /**
243
+ * A whole number for a byte field.
244
+ *
245
+ * @param value The style value; must be a finite number.
246
+ *
247
+ * @returns It with its fraction dropped, clamped to 0–255, as Flow A reads one.
248
+ */
249
+ const byteValue = (value) => {
250
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
251
+ throw new Error(`expected a number, got ${JSON.stringify(value)}`);
252
+ }
253
+ // Adding 0 turns -0 into 0.
254
+ return Math.min(BYTE_MAX, Math.max(0, Math.trunc(value))) + 0;
255
+ };
256
+ /**
257
+ * One side of a `shadowOffset`.
258
+ *
259
+ * @param offset `width` or `height`; left out, it is 0.
260
+ *
261
+ * @returns The offset in pixels.
262
+ */
263
+ const shadowOffsetSide = (offset) => offset === undefined || offset === null ? 0 : floatConstant(offset);
229
264
  /**
230
265
  * A dimension that may be a percentage. `'50%'` → the engine's float `*_percent` field (% of the parent);
231
266
  * a number → the pixel field. Used for width / height / flexBasis and the four insets, all of which the
@@ -299,6 +334,9 @@ const KEYS = {
299
334
  value: enumKey(FLEX_DIRECTION, 'flexDirection')(direction),
300
335
  },
301
336
  ],
337
+ flexWrap: wrap => [
338
+ { field: 'flex_wrap', value: enumKey(FLEX_WRAP, 'flexWrap')(wrap) },
339
+ ],
302
340
  alignItems: alignment => [
303
341
  { field: 'align_items', value: enumKey(ALIGN, 'alignItems')(alignment) },
304
342
  ],
@@ -347,6 +385,39 @@ const KEYS = {
347
385
  backgroundColor: color => [
348
386
  { field: 'background_color', value: parseColorValue(color) },
349
387
  ],
388
+ // One gradient, as Flow A reads it. Every field is written, so a later backgroundImage replaces an earlier one
389
+ // whole.
390
+ backgroundImage: image => {
391
+ const result = parseBackgroundImage(image);
392
+ if (result.problem !== undefined) {
393
+ throw new Error(result.problem);
394
+ }
395
+ const { gradient } = result;
396
+ return [
397
+ {
398
+ field: 'gradient_type',
399
+ value: gradient.type === 'radial'
400
+ ? 'ER_GRADIENT_RADIAL'
401
+ : 'ER_GRADIENT_LINEAR',
402
+ },
403
+ { field: 'gradient_corner', value: gradient.corner },
404
+ { field: 'gradient_angle', value: gradient.angle },
405
+ { field: 'gradient_stop_count', value: gradient.stops.length },
406
+ ...Array.from({ length: BACKGROUND_MAX_STOPS }, (_, index) => {
407
+ const stop = gradient.stops[index];
408
+ return [
409
+ {
410
+ field: `gradient_stops[${index}].color`,
411
+ value: stop ? parseColorValue(stop.color) : 0,
412
+ },
413
+ {
414
+ field: `gradient_stops[${index}].position`,
415
+ value: stop ? stop.offset : 0,
416
+ },
417
+ ];
418
+ }).flat(),
419
+ ];
420
+ },
350
421
  borderRadius: radius => [{ field: 'border_radius', value: dimension(radius) }],
351
422
  // Per-corner radii; the engine reads 0 in one of these as "use borderRadius" (same as Flow A).
352
423
  borderTopLeftRadius: radius => [
@@ -372,6 +443,26 @@ const KEYS = {
372
443
  },
373
444
  ],
374
445
  zIndex: zIndex => [{ field: 'z_index', value: dimension(zIndex) }],
446
+ // Shadow, drawn only by a firmware built with ERUI_SHADOWS (same as Flow A).
447
+ shadowColor: color => [
448
+ { field: 'shadow_color', value: parseColorValue(color) },
449
+ ],
450
+ // Both sides are written, so a later shadowOffset replaces an earlier one whole, as in RN.
451
+ shadowOffset: offset => {
452
+ if (typeof offset !== 'object' || Array.isArray(offset)) {
453
+ throw new Error(`expected {width, height}, got ${JSON.stringify(offset)}`);
454
+ }
455
+ const { width, height } = offset;
456
+ return [
457
+ { field: 'shadow_offset_x', value: shadowOffsetSide(width) },
458
+ { field: 'shadow_offset_y', value: shadowOffsetSide(height) },
459
+ ];
460
+ },
461
+ shadowOpacity: opacity => [
462
+ { field: 'shadow_opacity', value: floatConstant(opacity) },
463
+ ],
464
+ shadowRadius: radius => [{ field: 'shadow_radius', value: byteValue(radius) }],
465
+ elevation: elevation => [{ field: 'elevation', value: byteValue(elevation) }],
375
466
  // Text
376
467
  color: color => [{ field: 'color', value: parseColorValue(color) }],
377
468
  fontSize: size => [{ field: 'font_size', value: dimension(size) }],