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,55 @@
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
+ - Implementation
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ /**
20
+ * compileSource's options with the target screen and the fixed-buffer caps filled in. Each one the caller
21
+ * leaves out falls back to its environment variable, then to a default. The environment is read on every
22
+ * call, not at import, so one process can compile for several boards.
23
+ *
24
+ * screen ER_AOT_SCREEN_W / _H 800×480 Baked so a responsive app's `screen.width` branching
25
+ * folds to the layout for THIS build.
26
+ * listCap ER_AOT_LIST_CAP 16 Rows a list state holds. Each pooled row costs engine
27
+ * nodes, so lower it on a tight-RAM MCU.
28
+ * listStrCap ER_AOT_LIST_STR_CAP 48 Characters per string buffer (list field, string state).
29
+ * maxTextSpans ER_AOT_MAX_TEXT_SPANS 4 Inline segments in a nested <Text>. Must match the
30
+ * engine's ER_TEXT_MAX_SPANS.
31
+ *
32
+ * @param opts The options the caller passed.
33
+ *
34
+ * @returns The same options with every value filled in.
35
+ */
36
+ export function resolveOptions(opts) {
37
+ // A blank or unparsable variable falls back to the default for that value alone.
38
+ const fromEnv = (name, fallback) => Number(process.env[name]) || fallback;
39
+ const screenFromEnv = Number(process.env.ER_AOT_SCREEN_W) > 0 &&
40
+ Number(process.env.ER_AOT_SCREEN_H) > 0;
41
+ return {
42
+ ...opts,
43
+ screen: opts.screen ?? {
44
+ width: fromEnv('ER_AOT_SCREEN_W', 800),
45
+ height: fromEnv('ER_AOT_SCREEN_H', 480),
46
+ },
47
+ // Where the size came from, for the error footer: 'option', 'env' (both dimensions) or 'default'.
48
+ screenFrom: opts.screen ? 'option' : screenFromEnv ? 'env' : 'default',
49
+ caps: {
50
+ listCap: opts.listCap ?? fromEnv('ER_AOT_LIST_CAP', 16),
51
+ listStrCap: opts.listStrCap ?? fromEnv('ER_AOT_LIST_STR_CAP', 48),
52
+ maxTextSpans: opts.maxTextSpans ?? fromEnv('ER_AOT_MAX_TEXT_SPANS', 4),
53
+ },
54
+ };
55
+ }
@@ -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
+ - Classes
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ /**
20
+ * Everything the passes lower for one app besides its element tree: the functions, drawings, responders and
21
+ * effects, and the counters that name them. Each pass appends to it as the JSX tree is walked; writeApp then
22
+ * writes it out as app.gen.c.
23
+ *
24
+ * @class Out
25
+ */
26
+ export class Out {
27
+ /** Engine nodes created so far */
28
+ nodes = 0;
29
+ /** Event handlers, one function each. */
30
+ handlers = [];
31
+ /** A useCallback's C handler, by component prefix and name, so each is emitted once. */
32
+ cbEmitted = new Map();
33
+ /** File-scope vector op-tapes and paint tables, and the builders of state-driven drawings. */
34
+ vectors = [];
35
+ /** Numbers the next <Svg> (its tapes are `s_svg<id>_…`). */
36
+ svgN = 0;
37
+ /** Whether the generated C calls into <math.h>. */
38
+ needsMath = false;
39
+ timerFns = [];
40
+ usesTimers = false;
41
+ /** What er_app_build() runs last: mount-once effects, and the first run of each dep-driven one. */
42
+ mountEffects = [];
43
+ /** Animations' on_complete callbacks: completions, completion waits, and chains. */
44
+ animCbs = [];
45
+ /** The owner number of each composition that completion waits belong to, by composition; `isStarted` once a start waits. */
46
+ animWaits = new Map();
47
+ /** Numbers the next animation sequence. */
48
+ seqN = 0;
49
+ /** Image assets the app reaches: asset name → source-relative import path. */
50
+ images = new Map();
51
+ /** A dynamic image source was reached, so every imported image has to be baked. */
52
+ bakeAllImages = false;
53
+ /** Numbers the next inlined component instance (its storage prefix is `c<n>_`). */
54
+ instN = 0;
55
+ /** State, refs, and animated values that belong to inlined component instances. */
56
+ childStateRecords = [];
57
+ childRefs = [];
58
+ childAnimations = [];
59
+ /** Numbers the next effect. */
60
+ effN = 0;
61
+ effectFns = [];
62
+ /** File-scope variables the effects and animation loops need. */
63
+ effectDecls = [];
64
+ /** Dep-driven effects, which app_update() re-runs when one of their deps changed. */
65
+ depEffects = [];
66
+ /** Numbers the next PanResponder. */
67
+ panN = 0;
68
+ /** Every PanResponder a node spreads, lowered once each. */
69
+ panResponders = [];
70
+ /** The module's function components, by name, for inlining. */
71
+ components;
72
+ /** The parsed module, for the passes that scan it again. */
73
+ program;
74
+ /**
75
+ * @param components The module's function components, by name, for inlining.
76
+ * @param program The parsed module, for the passes that scan it again.
77
+ */
78
+ constructor(components, program) {
79
+ this.components = components;
80
+ this.program = program;
81
+ }
82
+ /** How many engine nodes have been created. */
83
+ get nodeCount() {
84
+ return this.nodes;
85
+ }
86
+ /**
87
+ * Reserves the next engine node.
88
+ *
89
+ * @returns Its number: the node is the C variable `n<number>`.
90
+ */
91
+ allocateNodeId() {
92
+ return this.nodes++;
93
+ }
94
+ }
@@ -0,0 +1,354 @@
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
+ import { lowerExpr } from './expressions.mjs';
21
+ import { isFn } from './collect.mjs';
22
+ import { lowerHandler } from './handlers.mjs';
23
+ import { FLOAT, INT } from './ir/types.mjs';
24
+ /*----------------------------------------------------------------------------------------------------------------------
25
+ - Constants
26
+ ---------------------------------------------------------------------------------------------------------------------*/
27
+ /** RN PanResponder config key → the engine responder QUERY it lowers to. */
28
+ const PAN_QUERIES = {
29
+ onStartShouldSetPanResponder: 'ER_QUERY_START_SHOULD_SET',
30
+ onStartShouldSetPanResponderCapture: 'ER_QUERY_START_SHOULD_SET_CAPTURE',
31
+ onMoveShouldSetPanResponder: 'ER_QUERY_MOVE_SHOULD_SET',
32
+ onMoveShouldSetPanResponderCapture: 'ER_QUERY_MOVE_SHOULD_SET_CAPTURE',
33
+ onPanResponderTerminationRequest: 'ER_QUERY_TERMINATION_REQUEST',
34
+ };
35
+ /** RN PanResponder config key → the engine responder EVENT it lowers to. */
36
+ const PAN_EVENTS = {
37
+ onPanResponderGrant: 'ER_EVENT_RESPONDER_GRANT',
38
+ onPanResponderMove: 'ER_EVENT_RESPONDER_MOVE',
39
+ onPanResponderRelease: 'ER_EVENT_RESPONDER_RELEASE',
40
+ onPanResponderTerminate: 'ER_EVENT_RESPONDER_TERMINATE',
41
+ onPanResponderReject: 'ER_EVENT_RESPONDER_REJECT',
42
+ };
43
+ /** The fields a gestureState carries. */
44
+ const GESTURE_FIELDS = new Set([
45
+ 'dx',
46
+ 'dy',
47
+ 'moveX',
48
+ 'moveY',
49
+ 'x0',
50
+ 'y0',
51
+ 'vx',
52
+ 'vy',
53
+ 'numberActiveTouches',
54
+ 'stateID',
55
+ ]);
56
+ /** Config keys the Flow A module acts on that have no Flow B lowering — rejected by name, not ignored. */
57
+ const PAN_FLOW_A_ONLY = {
58
+ onPanResponderStart: 'it reports EXTRA fingers joining a gesture already in flight; Flow B lowers one gesture per responder.',
59
+ onPanResponderEnd: 'it reports a finger lifting while others stay down; Flow B lowers one gesture per responder.',
60
+ };
61
+ /*----------------------------------------------------------------------------------------------------------------------
62
+ - Implementation
63
+ ---------------------------------------------------------------------------------------------------------------------*/
64
+ /**
65
+ * True for a `PanResponder.create({…})` call.
66
+ *
67
+ * @param node Any node, or nothing.
68
+ *
69
+ * @returns Whether it is such a call.
70
+ */
71
+ export const isPanCreate = (node) => node?.type === 'CallExpression' &&
72
+ node.callee.type === 'MemberExpression' &&
73
+ !node.callee.computed &&
74
+ node.callee.object.type === 'Identifier' &&
75
+ node.callee.object.name === 'PanResponder' &&
76
+ node.callee.property.name === 'create';
77
+ /**
78
+ * Lowers one `gestureState` field of a responder's callback.
79
+ *
80
+ * @param field The field's name.
81
+ * @param pan The responder whose callback reads it.
82
+ *
83
+ * @returns The field.
84
+ */
85
+ export function lowerGestureField(field, pan) {
86
+ if (GESTURE_FIELDS.has(field)) {
87
+ return {
88
+ kind: 'gesture',
89
+ field: field,
90
+ statePrefix: pan.cPrefix,
91
+ stateID: pan.stateID,
92
+ type: field === 'vx' || field === 'vy' ? FLOAT : INT,
93
+ };
94
+ }
95
+ throw aotError(`AOT: unknown gestureState field "${field}" in a PanResponder callback`, 'a gestureState carries dx / dy / moveX / moveY / x0 / y0 / vx / vy / numberActiveTouches / stateID.');
96
+ }
97
+ /**
98
+ * Validates one `PanResponder.create({…})` config and builds the descriptor the emitter works from.
99
+ *
100
+ * @param name The JS name the responder is bound to.
101
+ * @param createCall The `PanResponder.create(…)` call.
102
+ * @param prefix The C name prefix: none for the App, `c<n>_` for an inlined instance.
103
+ *
104
+ * @returns The responder.
105
+ */
106
+ function buildPanDescriptor(name, createCall, prefix) {
107
+ // The config must be an object literal, so every callback it holds is known at compile time.
108
+ const config = createCall.arguments[0];
109
+ if (!config || config.type !== 'ObjectExpression') {
110
+ throw aotError('AOT: PanResponder.create(...) needs an object literal of callbacks', 'write the config inline — `PanResponder.create({ onStartShouldSetPanResponder: () => ' +
111
+ 'true, … })` — so the AOT can see which callbacks exist at compile time.');
112
+ }
113
+ // File each callback under the engine query or event its key lowers to, rejecting keys Flow B cannot lower.
114
+ const queries = new Map();
115
+ const events = new Map();
116
+ for (const prop of config.properties) {
117
+ if (prop.type !== 'ObjectProperty' && prop.type !== 'ObjectMethod') {
118
+ throw aotError('AOT: a spread inside PanResponder.create({…}) is not supported', 'list the callbacks explicitly.');
119
+ }
120
+ if (prop.computed) {
121
+ throw aotError('AOT: a computed key in PanResponder.create({…}) is not supported', 'name each callback literally — the AOT decides at compile time which engine query or event a key becomes.');
122
+ }
123
+ // A shorthand method (`onPanResponderMove(e, g) { … }`) IS the function node; a property holds it.
124
+ const key = prop.key.name ?? prop.key.value;
125
+ const callback = prop.type === 'ObjectMethod' ? prop : prop.value;
126
+ if (PAN_FLOW_A_ONLY[key]) {
127
+ throw aotError(`AOT: PanResponder "${key}" is not supported in Flow B`, `${PAN_FLOW_A_ONLY[key]} Use onPanResponderGrant / Release / Terminate instead, or ` +
128
+ `build this screen for Flow A.`);
129
+ }
130
+ const query = PAN_QUERIES[key];
131
+ const event = PAN_EVENTS[key];
132
+ if (!query && !event) {
133
+ throw aotError(`AOT: unknown PanResponder config key "${key}"`, `supported: ${[...Object.keys(PAN_QUERIES), ...Object.keys(PAN_EVENTS)].join(', ')}.`);
134
+ }
135
+ if (prop.type !== 'ObjectMethod' && !isFn(callback)) {
136
+ throw aotError(`AOT: PanResponder "${key}" must be a function`, 'pass an inline arrow — `(e, g) => …` — so it can be compiled into the generated C.');
137
+ }
138
+ if (query) {
139
+ queries.set(query, callback);
140
+ }
141
+ else {
142
+ events.set(event, callback);
143
+ }
144
+ }
145
+ return {
146
+ name,
147
+ cPrefix: `s_pan_${prefix}${name}`,
148
+ fnPrefix: `er_pan_${prefix}${name}`,
149
+ stateID: 0, // assigned when the responder is first wired to a node
150
+ queries,
151
+ events,
152
+ lowered: null,
153
+ };
154
+ }
155
+ /**
156
+ * Collects `const pan = useRef(PanResponder.create({…})).current` → Map (name → descriptor). The
157
+ * `useRef(…)` form (read back as `pan.current.panHandlers`) is accepted too; a bare `create(…)` is not,
158
+ * because it would re-create the recognizer every render in Flow A and the two flows must agree.
159
+ *
160
+ * @param fnBody The component's body.
161
+ * @param prefix The C name prefix: none for the App, `c<n>_` for an inlined instance.
162
+ *
163
+ * @returns The responders, by name.
164
+ */
165
+ export function collectPanResponders(fnBody, prefix = '') {
166
+ // Only the component body's own top-level declarations can hold a responder.
167
+ const pans = new Map();
168
+ if (fnBody.type !== 'BlockStatement')
169
+ return pans;
170
+ for (const statement of fnBody.body) {
171
+ if (statement.type !== 'VariableDeclaration')
172
+ continue;
173
+ for (const decl of statement.declarations) {
174
+ const init = decl.init;
175
+ if (decl.id.type !== 'Identifier' || !init)
176
+ continue;
177
+ // Find the useRef call in either `useRef(…).current` or a bare `useRef(…)`.
178
+ const viaCurrent = init.type === 'MemberExpression' &&
179
+ !init.computed &&
180
+ init.property.name === 'current' &&
181
+ init.object.type === 'CallExpression' &&
182
+ init.object.callee.name === 'useRef'
183
+ ? init.object
184
+ : null;
185
+ const viaRef = init.type === 'CallExpression' &&
186
+ init.callee.name === 'useRef'
187
+ ? init
188
+ : null;
189
+ const useRefCall = viaCurrent || viaRef;
190
+ // Skip anything that is not a useRef around PanResponder.create(…); an unwrapped create is an error.
191
+ if (!useRefCall || !isPanCreate(useRefCall.arguments[0])) {
192
+ if (isPanCreate(init)) {
193
+ throw aotError('AOT: PanResponder.create(...) must be kept in a useRef', `write \`const ${decl.id.name} = useRef(PanResponder.create({…})).current;\` — the ` +
194
+ `recognizer owns the live gesture, so a fresh one per render would throw the drag ` +
195
+ `away (the same rule Flow A enforces).`);
196
+ }
197
+ continue;
198
+ }
199
+ // Build the responder's descriptor and record which way the app must spell its spread.
200
+ const pan = buildPanDescriptor(decl.id.name, useRefCall.arguments[0], prefix);
201
+ pan.viaRefCurrent = !viaCurrent; // how the app spells the spread: pan.current.panHandlers
202
+ pans.set(decl.id.name, pan);
203
+ }
204
+ }
205
+ return pans;
206
+ }
207
+ /**
208
+ * Lowers a should-set predicate to a single condition: the engine calls these synchronously inside
209
+ * hit-testing, before anyone owns the gesture, so they may only READ.
210
+ *
211
+ * @param fnNode The predicate.
212
+ * @param env The expression environment.
213
+ * @param pan The responder it belongs to.
214
+ *
215
+ * @returns The condition.
216
+ */
217
+ function lowerPanQuery(fnNode, env, pan) {
218
+ // The predicate reads the event and gestureState through its own parameter names.
219
+ const eventParam = fnNode.params[0]?.type === 'Identifier' ? fnNode.params[0].name : null;
220
+ const gestureParam = fnNode.params[1]?.type === 'Identifier' ? fnNode.params[1].name : null;
221
+ const queryEnv = { ...env, pan };
222
+ if (eventParam) {
223
+ queryEnv.event = eventParam;
224
+ }
225
+ if (gestureParam) {
226
+ queryEnv.gesture = gestureParam;
227
+ }
228
+ // Its body must be one expression: an expression body, or a block that only returns one.
229
+ const body = fnNode.body;
230
+ const expr = body.type === 'BlockStatement'
231
+ ? body.body.length === 1 && body.body[0].type === 'ReturnStatement'
232
+ ? body.body[0].argument
233
+ : null
234
+ : body;
235
+ if (!expr) {
236
+ throw aotError('AOT: a PanResponder should-set predicate must be a single boolean expression', 'write it as `() => true` or `(e, g) => Math.abs(g.dx) > 8`. The engine asks these ' +
237
+ 'mid-hit-test, so they may only read (state, event/gesture fields, constants) — never ' +
238
+ 'set state.');
239
+ }
240
+ return lowerExpr(expr, queryEnv);
241
+ }
242
+ /**
243
+ * Lowers a PanResponder onto the engine's responder system, not a transpiled copy of it: the engine already runs
244
+ * RN's state machine in C. Capture/bubble negotiation (er_responder_query_set) picks an owner, then GRANT / MOVE /
245
+ * RELEASE / TERMINATE fire on the owner alone, carrying cumulative travel and velocity. So the should-set
246
+ * predicates become QUERIES and the callbacks become responder EVENT handlers: no gesture math is duplicated into
247
+ * the generated C, and a granted pan owns the gesture, so a ScrollView ancestor cannot auto-scroll out from under
248
+ * it.
249
+ *
250
+ * The one thing with no engine counterpart is RN's anchor: `g.dx` is measured from the grant, while the engine's
251
+ * `data->dx` runs from touch-down, so a claim that needed 10 px of slop would otherwise open with a 10 px jump.
252
+ * That anchor, plus `x0`/`y0`, is a handful of file-scope ints per responder, kept by the grant, release and
253
+ * terminate handlers. Those three are ALWAYS lowered, even with no app callback; move and reject only with one.
254
+ *
255
+ * @param pan The responder; its gesture id must be assigned.
256
+ * @param env The expression environment.
257
+ * @param state The component's state table.
258
+ * @param out Everything emitted so far.
259
+ *
260
+ * @returns The responder.
261
+ */
262
+ export function lowerPanResponder(pan, env, state, out) {
263
+ const panEnv = { ...env, pan };
264
+ const event = (name, role) => ({
265
+ event: name,
266
+ role,
267
+ callback: pan.events.has(name)
268
+ ? lowerHandler(pan.events.get(name), panEnv, state, out, pan)
269
+ : null,
270
+ });
271
+ const events = [
272
+ event('ER_EVENT_RESPONDER_GRANT', 'grant'),
273
+ event('ER_EVENT_RESPONDER_RELEASE', 'release'),
274
+ event('ER_EVENT_RESPONDER_TERMINATE', 'terminate'),
275
+ ];
276
+ if (pan.events.has('ER_EVENT_RESPONDER_MOVE')) {
277
+ events.push(event('ER_EVENT_RESPONDER_MOVE', 'move'));
278
+ }
279
+ if (pan.events.has('ER_EVENT_RESPONDER_REJECT')) {
280
+ events.push(event('ER_EVENT_RESPONDER_REJECT', 'reject'));
281
+ }
282
+ return {
283
+ name: pan.name,
284
+ statePrefix: pan.cPrefix,
285
+ fnPrefix: pan.fnPrefix,
286
+ stateID: pan.stateID,
287
+ events,
288
+ queries: [...pan.queries].map(([query, predicate]) => ({
289
+ query,
290
+ test: lowerPanQuery(predicate, panEnv, pan),
291
+ })),
292
+ };
293
+ }
294
+ /**
295
+ * Lowers one PanResponder once, however many nodes spread it (see lowerPanResponder), and adds its event
296
+ * handlers to the output. Each node that spreads it wires them in its own build lines.
297
+ *
298
+ * @param pan The responder.
299
+ * @param out Everything emitted so far.
300
+ * @param env The expression environment.
301
+ * @param state The component's state table.
302
+ *
303
+ * @returns The lowered responder.
304
+ */
305
+ export function emitPanResponder(pan, out, env, state) {
306
+ if (!pan.lowered) {
307
+ pan.stateID = ++out.panN;
308
+ pan.lowered = lowerPanResponder(pan, env, state, out);
309
+ out.panResponders.push(pan.lowered);
310
+ for (const panEvent of pan.lowered.events) {
311
+ out.handlers.push({ kind: 'panEvent', pan: pan.lowered, event: panEvent });
312
+ }
313
+ }
314
+ return pan.lowered;
315
+ }
316
+ /**
317
+ * Resolves a JSX spread to the PanResponder it spreads, or null when it is some other spread. A name
318
+ * that IS a responder but is spelled the other way around (`.current` where the app already unwrapped it,
319
+ * or vice versa) is an error rather than a silent miss — Flow A would throw at runtime on the same code.
320
+ *
321
+ * @param argument The spread's argument.
322
+ * @param env The expression environment.
323
+ *
324
+ * @returns The responder, or null when the spread is not `<responder>.panHandlers`.
325
+ */
326
+ export function panSpreadTarget(argument, env) {
327
+ // Only `<name>.panHandlers` or `<name>.current.panHandlers` can spread a responder.
328
+ if (argument?.type !== 'MemberExpression' ||
329
+ argument.computed ||
330
+ argument.property.name !== 'panHandlers') {
331
+ return null;
332
+ }
333
+ const handlersOwner = argument.object;
334
+ const usesCurrent = handlersOwner.type === 'MemberExpression' &&
335
+ !handlersOwner.computed &&
336
+ handlersOwner.property.name === 'current' &&
337
+ handlersOwner.object.type === 'Identifier';
338
+ const rootName = usesCurrent
339
+ ? handlersOwner.object.name
340
+ : handlersOwner.name;
341
+ const pan = handlersOwner.type === 'Identifier' || usesCurrent
342
+ ? env.pans?.get(rootName)
343
+ : null;
344
+ if (!pan)
345
+ return null;
346
+ // The spread must unwrap `.current` exactly when the declaration did not.
347
+ if (pan.viaRefCurrent !== usesCurrent) {
348
+ throw aotError(`AOT: "${rootName}" is a PanResponder, but this spread does not match how it was declared`, pan.viaRefCurrent
349
+ ? `it was declared as \`useRef(PanResponder.create({…}))\`, so spread \`{...${rootName}.current.panHandlers}\`.`
350
+ : `it was declared as \`useRef(PanResponder.create({…})).current\`, so spread ` +
351
+ `\`{...${rootName}.panHandlers}\`.`);
352
+ }
353
+ return pan;
354
+ }