embedded-react 0.15.0 → 0.16.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 (57) hide show
  1. package/aot/compile.mjs +173 -8272
  2. package/aot/compiler/animations.mjs +1001 -0
  3. package/aot/compiler/assemble.mjs +806 -0
  4. package/aot/compiler/c/animations.mjs +362 -0
  5. package/aot/compiler/c/expressions.mjs +215 -0
  6. package/aot/compiler/c/functions.mjs +268 -0
  7. package/aot/compiler/c/keyboard.mjs +96 -0
  8. package/aot/compiler/c/nodes.mjs +282 -0
  9. package/aot/compiler/c/pan-responder.mjs +136 -0
  10. package/aot/compiler/c/statements.mjs +242 -0
  11. package/aot/compiler/c/text.mjs +83 -0
  12. package/aot/compiler/c/vectors.mjs +381 -0
  13. package/aot/compiler/c-syntax.mjs +127 -0
  14. package/aot/compiler/check.mjs +165 -0
  15. package/aot/compiler/collect.mjs +743 -0
  16. package/aot/compiler/control-flow.mjs +683 -0
  17. package/aot/compiler/diagnostics.mjs +137 -0
  18. package/aot/compiler/elements/activity-indicator.mjs +160 -0
  19. package/aot/compiler/elements/dial.mjs +343 -0
  20. package/aot/compiler/elements/flat-list.mjs +139 -0
  21. package/aot/compiler/elements/image.mjs +151 -0
  22. package/aot/compiler/elements/modal.mjs +158 -0
  23. package/aot/compiler/elements/props.mjs +474 -0
  24. package/aot/compiler/elements/switch.mjs +183 -0
  25. package/aot/compiler/elements/text-input.mjs +171 -0
  26. package/aot/compiler/expressions.mjs +1479 -0
  27. package/aot/compiler/handlers.mjs +1072 -0
  28. package/aot/compiler/ir/animations.mjs +16 -0
  29. package/aot/compiler/ir/app.mjs +16 -0
  30. package/aot/compiler/ir/expressions.mjs +16 -0
  31. package/aot/compiler/ir/functions.mjs +16 -0
  32. package/aot/compiler/ir/keyboard.mjs +16 -0
  33. package/aot/compiler/ir/nodes.mjs +83 -0
  34. package/aot/compiler/ir/pan-responder.mjs +16 -0
  35. package/aot/compiler/ir/source.mjs +16 -0
  36. package/aot/compiler/ir/statements.mjs +16 -0
  37. package/aot/compiler/ir/text.mjs +16 -0
  38. package/aot/compiler/ir/types.mjs +185 -0
  39. package/aot/compiler/ir/vectors.mjs +16 -0
  40. package/aot/compiler/keyboard.mjs +152 -0
  41. package/aot/compiler/lower.mjs +282 -0
  42. package/aot/compiler/nodes.mjs +683 -0
  43. package/aot/compiler/options.mjs +55 -0
  44. package/aot/compiler/out.mjs +94 -0
  45. package/aot/compiler/pan-responder.mjs +354 -0
  46. package/aot/compiler/parse.mjs +300 -0
  47. package/aot/compiler/static-eval.mjs +228 -0
  48. package/aot/compiler/style-text.mjs +648 -0
  49. package/aot/compiler/svg.mjs +1167 -0
  50. package/aot/compiler/types.mjs +16 -0
  51. package/aot/screenshot-smoke.mjs +95 -55
  52. package/aot/style-map.mjs +366 -304
  53. package/cli.mjs +5 -8
  54. package/package.json +14 -6
  55. package/sim/embedded-react.wasm +0 -0
  56. package/src/embedded-react/FlatList.js +4 -4
  57. package/src/props.js +1 -1
@@ -0,0 +1,282 @@
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 { checkNumber, lowerWithChecks, runChecks } from './check.mjs';
20
+ import { aotError } from './diagnostics.mjs';
21
+ import { evalStatic, withUndefined } from './static-eval.mjs';
22
+ import { findShadowedClockCalls, emitSourcedWide } from './expressions.mjs';
23
+ import { findComponent, declaredNames, moduleScope, collectState, findReturnJSX, collectComponents, collectHelpers, collectImageImports, collectSvgImports, collectCallbacks, collectMemos, collectEffects, } from './collect.mjs';
24
+ import { collectAnims, collectRefs } from './animations.mjs';
25
+ import { lowerEffect } from './handlers.mjs';
26
+ import { collectPanResponders } from './pan-responder.mjs';
27
+ import { emitNode } from './nodes.mjs';
28
+ import { lowerKeyboardConfig } from './keyboard.mjs';
29
+ import { normalizeUndefined, parseApp } from './parse.mjs';
30
+ import { Out } from './out.mjs';
31
+ import { isStateDriven, nodesOf, stateDrivenVectorOf } from './ir/nodes.mjs';
32
+ /*----------------------------------------------------------------------------------------------------------------------
33
+ - Implementation
34
+ ---------------------------------------------------------------------------------------------------------------------*/
35
+ /**
36
+ * Settles what only the whole app decides. With nothing for app_update() to re-apply there is no app_update(),
37
+ * so no body ends by calling it. A composition that no start waits on has no waits for a stop to end.
38
+ *
39
+ * @param out The lowered app's functions.
40
+ * @param hasUpdate The app has an app_update().
41
+ */
42
+ function settleFunctions(out, hasUpdate) {
43
+ const idleOwners = new Set([...out.animWaits.values()]
44
+ .filter(record => !record.isStarted)
45
+ .map(record => record.owner));
46
+ const settleStmts = (stmts) => {
47
+ for (const stmt of stmts) {
48
+ if (stmt.kind === 'stopAnimations') {
49
+ if (stmt.waitOwner !== null && idleOwners.has(stmt.waitOwner)) {
50
+ stmt.waitOwner = null;
51
+ }
52
+ }
53
+ else if (stmt.kind === 'if') {
54
+ settleStmts(stmt.consequent);
55
+ settleStmts(stmt.alternate ?? []);
56
+ }
57
+ }
58
+ };
59
+ const settle = (callback) => {
60
+ if (!callback)
61
+ return;
62
+ if (!hasUpdate) {
63
+ callback.updatesState = false;
64
+ }
65
+ settleStmts(callback.body);
66
+ };
67
+ for (const handler of out.handlers) {
68
+ if (handler.kind === 'callback') {
69
+ settle(handler.callback);
70
+ }
71
+ else if (handler.kind === 'panEvent') {
72
+ settle(handler.event.callback);
73
+ }
74
+ }
75
+ for (const timer of out.timerFns) {
76
+ settle(timer.callback);
77
+ }
78
+ for (const animationCallback of out.animCbs) {
79
+ if (animationCallback.kind === 'completion') {
80
+ settle(animationCallback.completion);
81
+ }
82
+ }
83
+ for (const effectFunction of out.effectFns) {
84
+ if (effectFunction.kind === 'mount') {
85
+ settleStmts(effectFunction.body);
86
+ }
87
+ else if (effectFunction.kind === 'cleanup') {
88
+ settleStmts(effectFunction.effect.cleanup);
89
+ }
90
+ else {
91
+ settleStmts(effectFunction.effect.body);
92
+ }
93
+ }
94
+ for (const mountEffect of out.mountEffects) {
95
+ if (mountEffect.kind === 'inline') {
96
+ settle(mountEffect.callback);
97
+ }
98
+ else if (mountEffect.kind === 'call' && !hasUpdate) {
99
+ mountEffect.updatesState = false;
100
+ }
101
+ }
102
+ }
103
+ /**
104
+ * Refuses state C cannot hold: a number in a list item that has no C form, and a host value that would take
105
+ * the name of the setter every app exports for the wall clock. A field an item leaves out starts at 0.
106
+ *
107
+ * @param stateRecords The app's state, then each inlined instance's.
108
+ */
109
+ function checkStateRecords(stateRecords) {
110
+ for (const record of stateRecords) {
111
+ if (record.kind !== 'list')
112
+ continue;
113
+ for (const item of record.items) {
114
+ for (const field of record.struct.fields) {
115
+ const value = item[field.key];
116
+ if (field.kind === 'string' || value == null)
117
+ continue;
118
+ const number = Number(value);
119
+ if (field.kind === 'float' || !Number.isFinite(number)) {
120
+ checkNumber(number);
121
+ }
122
+ }
123
+ }
124
+ }
125
+ if (stateRecords.some(record => record.kind === 'scalar' && record.host && record.name === 'wall_clock')) {
126
+ throw aotError('AOT: useHostValue("wall_clock") would collide with er_app_set_wall_clock()', 'rename the host value — every app exports er_app_set_wall_clock() for Date.now().');
127
+ }
128
+ }
129
+ /**
130
+ * Lowers a Flow B app's JSX (or TSX) source, then runs the check pass over what lowering recorded. Pure (no
131
+ * I/O); writeApp (assemble.mts) writes the result as C.
132
+ *
133
+ * @param src The App.jsx/App.tsx source text.
134
+ * @param opts compileSource's options, resolved (see resolveOptions).
135
+ * @param wide State/ref slots to declare 64-bit (see compileWidened).
136
+ * @param stores Collects every value stored into an int slot.
137
+ *
138
+ * @returns The lowered app.
139
+ */
140
+ export function lowerApp(src, opts, wide, stores) {
141
+ const { result, checks } = lowerWithChecks(() => lowerProgram(src, opts, wide, stores));
142
+ const app = { ...result, checks };
143
+ runChecks(app.checks);
144
+ return app;
145
+ }
146
+ /**
147
+ * Runs the compiler passes over the parsed app, from its module scope and hooks through the element tree to its
148
+ * effects, recording the values the check pass checks.
149
+ *
150
+ * @param src The App.jsx/App.tsx source text.
151
+ * @param opts compileSource's options, resolved (see resolveOptions).
152
+ * @param wide State/ref slots to declare 64-bit (see compileWidened).
153
+ * @param stores Collects every value stored into an int slot.
154
+ *
155
+ * @returns The lowered app, without its checks.
156
+ */
157
+ function lowerProgram(src, opts, wide, stores) {
158
+ // Parse the app and prepare the AST for the passes below.
159
+ const ast = parseApp(src, opts);
160
+ normalizeUndefined(ast);
161
+ const shadowedClock = findShadowedClockCalls(ast);
162
+ // Seed the module scope with image imports (each is its baked-name string) so consts that use them can fold.
163
+ const { screen } = opts;
164
+ const imageImports = collectImageImports(ast.program);
165
+ const svgImports = collectSvgImports(ast.program);
166
+ const imageSeed = Object.fromEntries([...imageImports].map(([local, imageImport]) => [local, imageImport.name]));
167
+ const scope = moduleScope(ast.program, screen, imageSeed);
168
+ // Snapshot module bindings before App's consts fold in: child components close over these, not App's shadows.
169
+ const moduleConsts = { ...scope };
170
+ const component = findComponent(ast.program);
171
+ // Hide redeclared module names, then fold the body's static consts so responsive `if`s resolve at compile time.
172
+ for (const name of declaredNames(component.body.body)) {
173
+ delete scope[name];
174
+ }
175
+ for (const statement of component.body.body) {
176
+ if (statement.type !== 'VariableDeclaration' ||
177
+ statement.kind !== 'const') {
178
+ continue;
179
+ }
180
+ for (const decl of statement.declarations) {
181
+ if (decl.id.type !== 'Identifier' || !decl.init)
182
+ continue;
183
+ try {
184
+ scope[decl.id.name] = evalStatic(decl.init, withUndefined(scope));
185
+ }
186
+ catch {
187
+ // A dynamic const must hide a same-named module const, or the JSX quietly renders the module value.
188
+ delete scope[decl.id.name];
189
+ }
190
+ }
191
+ }
192
+ // Collect the component's state; a state name hides any module const of the same name from the folds.
193
+ const state = collectState(component.body, scope, opts.caps, '', wide);
194
+ for (const name of state.byName.keys()) {
195
+ delete scope[name];
196
+ }
197
+ // Collect the component's other hooks, plus the file's helpers and child components.
198
+ const rootJSX = findReturnJSX(component.body, scope);
199
+ const anims = collectAnims(component.body, scope);
200
+ const refs = collectRefs(component.body, scope, '', wide);
201
+ for (const name of [...anims.keys(), ...refs.keys()]) {
202
+ delete scope[name];
203
+ }
204
+ const pans = collectPanResponders(component.body);
205
+ const callbacks = collectCallbacks(component.body);
206
+ const memos = collectMemos(component.body);
207
+ const helpers = collectHelpers(component.body, ast.program);
208
+ const components = collectComponents(ast.program);
209
+ const imageNames = new Map([...imageImports].map(([, imageImport]) => [
210
+ imageImport.name,
211
+ imageImport.importPath,
212
+ ])); // asset name → path
213
+ // The expression environment every emitter compiles against.
214
+ const env = {
215
+ state: state.byName,
216
+ locals: new Map(),
217
+ consts: scope,
218
+ moduleConsts, // what an inlined child component sees (see emitComponent)
219
+ animations: anims,
220
+ refs,
221
+ pans,
222
+ callbacks,
223
+ helpers,
224
+ imageNames,
225
+ svgImports,
226
+ svgArtifacts: opts.svgArtifacts || {},
227
+ caps: opts.caps,
228
+ wide,
229
+ stores,
230
+ shadowedClock,
231
+ };
232
+ // Before emit, fold each memo or inline its C expression at each use (no cache needed: dep tracking re-applies).
233
+ for (const [name, expr] of memos) {
234
+ try {
235
+ scope[name] = evalStatic(expr, scope);
236
+ }
237
+ catch {
238
+ const cExpr = emitSourcedWide(expr, env);
239
+ env.locals.set(name, {
240
+ code: `(${cExpr.code})`,
241
+ cType: cExpr.cType,
242
+ isBool: cExpr.isBool,
243
+ lit: cExpr.lit,
244
+ wide: cExpr.wide,
245
+ source: cExpr.source,
246
+ });
247
+ delete scope[name]; // a runtime binding beats a module const of its name in every fold
248
+ }
249
+ }
250
+ // Emit the scene graph from the root JSX; `out` collects every generated fragment.
251
+ const out = new Out(components, ast.program);
252
+ const keyboard = lowerKeyboardConfig(ast.program); // a module-level setKeyboardConfig({...})
253
+ const root = emitNode(rootJSX, scope, out, env, state);
254
+ // Emit registered only the images it reached; a dynamic source can't be enumerated, so bake every import.
255
+ if (out.bakeAllImages) {
256
+ for (const [, imageImport] of imageImports) {
257
+ out.images.set(imageImport.name, imageImport.importPath);
258
+ }
259
+ }
260
+ // Lower effects (mount-once or dep-driven) after emit, so out.timerFns/usesTimers include handler timers too.
261
+ for (const effect of collectEffects(component.body)) {
262
+ lowerEffect(effect, env, state, out);
263
+ }
264
+ // app_update() exists only when something has to be re-applied after a state change.
265
+ const hasUpdate = nodesOf(root).some(node => isStateDriven(node) || stateDrivenVectorOf(node) !== null) || out.depEffects.length > 0;
266
+ settleFunctions(out, hasUpdate);
267
+ const stateRecords = [...state.byName.values(), ...out.childStateRecords];
268
+ checkStateRecords(stateRecords);
269
+ return {
270
+ state: stateRecords,
271
+ refs: [...refs.values(), ...out.childRefs],
272
+ animations: [...anims.values(), ...out.childAnimations],
273
+ keyboard,
274
+ root,
275
+ hasUpdate,
276
+ images: [...out.images.entries()].map(([name, importPath]) => ({
277
+ name,
278
+ importPath,
279
+ })),
280
+ out,
281
+ };
282
+ }