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.
- package/aot/compile.mjs +173 -8272
- package/aot/compiler/animations.mjs +1001 -0
- package/aot/compiler/assemble.mjs +806 -0
- package/aot/compiler/c/animations.mjs +362 -0
- package/aot/compiler/c/expressions.mjs +215 -0
- package/aot/compiler/c/functions.mjs +268 -0
- package/aot/compiler/c/keyboard.mjs +96 -0
- package/aot/compiler/c/nodes.mjs +282 -0
- package/aot/compiler/c/pan-responder.mjs +136 -0
- package/aot/compiler/c/statements.mjs +242 -0
- package/aot/compiler/c/text.mjs +83 -0
- package/aot/compiler/c/vectors.mjs +381 -0
- package/aot/compiler/c-syntax.mjs +127 -0
- package/aot/compiler/check.mjs +165 -0
- package/aot/compiler/collect.mjs +743 -0
- package/aot/compiler/control-flow.mjs +683 -0
- package/aot/compiler/diagnostics.mjs +137 -0
- package/aot/compiler/elements/activity-indicator.mjs +160 -0
- package/aot/compiler/elements/dial.mjs +343 -0
- package/aot/compiler/elements/flat-list.mjs +139 -0
- package/aot/compiler/elements/image.mjs +151 -0
- package/aot/compiler/elements/modal.mjs +158 -0
- package/aot/compiler/elements/props.mjs +474 -0
- package/aot/compiler/elements/switch.mjs +183 -0
- package/aot/compiler/elements/text-input.mjs +171 -0
- package/aot/compiler/expressions.mjs +1479 -0
- package/aot/compiler/handlers.mjs +1072 -0
- package/aot/compiler/ir/animations.mjs +16 -0
- package/aot/compiler/ir/app.mjs +16 -0
- package/aot/compiler/ir/expressions.mjs +16 -0
- package/aot/compiler/ir/functions.mjs +16 -0
- package/aot/compiler/ir/keyboard.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +83 -0
- package/aot/compiler/ir/pan-responder.mjs +16 -0
- package/aot/compiler/ir/source.mjs +16 -0
- package/aot/compiler/ir/statements.mjs +16 -0
- package/aot/compiler/ir/text.mjs +16 -0
- package/aot/compiler/ir/types.mjs +185 -0
- package/aot/compiler/ir/vectors.mjs +16 -0
- package/aot/compiler/keyboard.mjs +152 -0
- package/aot/compiler/lower.mjs +282 -0
- package/aot/compiler/nodes.mjs +683 -0
- package/aot/compiler/options.mjs +55 -0
- package/aot/compiler/out.mjs +94 -0
- package/aot/compiler/pan-responder.mjs +354 -0
- package/aot/compiler/parse.mjs +300 -0
- package/aot/compiler/static-eval.mjs +228 -0
- package/aot/compiler/style-text.mjs +648 -0
- package/aot/compiler/svg.mjs +1167 -0
- package/aot/compiler/types.mjs +16 -0
- package/aot/screenshot-smoke.mjs +95 -55
- package/aot/style-map.mjs +366 -304
- package/cli.mjs +5 -8
- package/package.json +14 -6
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/FlatList.js +4 -4
- package/src/props.js +1 -1
|
@@ -0,0 +1,683 @@
|
|
|
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 } from './check.mjs';
|
|
20
|
+
import { NODE_TYPES } from '../style-map.mjs';
|
|
21
|
+
import { aotError, withLoc } from './diagnostics.mjs';
|
|
22
|
+
import { evalStaticOrThrow, evalStatic, foldScope } from './static-eval.mjs';
|
|
23
|
+
import { lowerExpr } from './expressions.mjs';
|
|
24
|
+
import { isFn } from './collect.mjs';
|
|
25
|
+
import { attrExpr, collectStyleAssigns, lowerNodeText, collectTextSpans, } from './style-text.mjs';
|
|
26
|
+
import { at, checkAttributes, checkNoChildren, elementSpec, EVENT_TYPES, withoutPlatformNoOps, } from './elements/props.mjs';
|
|
27
|
+
import { lowerHandler } from './handlers.mjs';
|
|
28
|
+
import { emitPanResponder, panSpreadTarget } from './pan-responder.mjs';
|
|
29
|
+
import { emitComponent, emitChildren } from './control-flow.mjs';
|
|
30
|
+
import { emitSvg } from './svg.mjs';
|
|
31
|
+
import { emitDial } from './elements/dial.mjs';
|
|
32
|
+
import { emitSwitch } from './elements/switch.mjs';
|
|
33
|
+
import { emitTextInput } from './elements/text-input.mjs';
|
|
34
|
+
import { emitActivityIndicator } from './elements/activity-indicator.mjs';
|
|
35
|
+
import { emitModal } from './elements/modal.mjs';
|
|
36
|
+
import { emitFlatList } from './elements/flat-list.mjs';
|
|
37
|
+
import { resolveImageAttrs } from './elements/image.mjs';
|
|
38
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
39
|
+
- Constants
|
|
40
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
41
|
+
// JS-only wrappers with no lowering yet, mapped to the tree to write by hand; "unknown element" would mislead.
|
|
42
|
+
const FLOW_A_ONLY_COMPONENTS = {
|
|
43
|
+
Button: '<Pressable style={…} onPress={…}><Text style={…}>title</Text></Pressable>.',
|
|
44
|
+
ImageBackground: "<View style={…}><Image source={…} style={{position: 'absolute', top: 0, left: 0, " +
|
|
45
|
+
'right: 0, bottom: 0}} />…children…</View>.',
|
|
46
|
+
SectionList: '<ScrollView> with the header element and a data.map(…) per section (a section is not ' +
|
|
47
|
+
'one .map, which is why it has no lowering).',
|
|
48
|
+
};
|
|
49
|
+
// <TouchableOpacity> press feedback (see touchablePressFades).
|
|
50
|
+
const TOUCHABLE_ACTIVE_OPACITY = 0.2; // RN's default
|
|
51
|
+
const TOUCHABLE_RESTORE_MS = 250; // RN's fade-back; the dim itself lands with no ramp
|
|
52
|
+
/** The press events a disabled <Pressable> or <TouchableOpacity> drops — RN presses none of them and dims for none. */
|
|
53
|
+
const PRESS_EVENTS = new Set([
|
|
54
|
+
'onPress',
|
|
55
|
+
'onLongPress',
|
|
56
|
+
'onPressIn',
|
|
57
|
+
'onPressOut',
|
|
58
|
+
]);
|
|
59
|
+
/** The `ellipsizeMode`s the engine draws as asked. */
|
|
60
|
+
const ELLIPSIZE_MODES = {
|
|
61
|
+
tail: 'ER_TEXT_ELLIPSIZE_TAIL',
|
|
62
|
+
clip: 'ER_TEXT_ELLIPSIZE_CLIP',
|
|
63
|
+
};
|
|
64
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
65
|
+
- Implementation
|
|
66
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
67
|
+
/**
|
|
68
|
+
* Resolves a JSX tag to a name, mapping `Animated.View/Text/Image` to their host element.
|
|
69
|
+
*
|
|
70
|
+
* @param openingElement The element's opening tag.
|
|
71
|
+
*
|
|
72
|
+
* @returns The tag name.
|
|
73
|
+
*/
|
|
74
|
+
function resolveTag(openingElement) {
|
|
75
|
+
const nameNode = openingElement.name;
|
|
76
|
+
if (nameNode.type === 'JSXIdentifier')
|
|
77
|
+
return nameNode.name;
|
|
78
|
+
if (nameNode.type === 'JSXMemberExpression' &&
|
|
79
|
+
nameNode.object.name === 'Animated') {
|
|
80
|
+
return nameNode.property.name; // Animated.View → View
|
|
81
|
+
}
|
|
82
|
+
throw new Error('AOT: unsupported JSX tag expression');
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The node refs `ref={myRef}` stores the element's handle in (a node ref from useRef), marked used.
|
|
86
|
+
*
|
|
87
|
+
* @param openingElement The element's opening tag.
|
|
88
|
+
* @param env The expression environment.
|
|
89
|
+
*
|
|
90
|
+
* @returns The refs' C slots.
|
|
91
|
+
*/
|
|
92
|
+
export function lowerRefBinds(openingElement, env) {
|
|
93
|
+
const refs = [];
|
|
94
|
+
for (const attr of openingElement.attributes) {
|
|
95
|
+
if (attr.type !== 'JSXAttribute' ||
|
|
96
|
+
attr.name.name !== 'ref') {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const refExpr = attr.value?.type === 'JSXExpressionContainer'
|
|
100
|
+
? attr.value.expression
|
|
101
|
+
: null;
|
|
102
|
+
if (refExpr?.type === 'Identifier' &&
|
|
103
|
+
env.refs?.get(refExpr.name)?.kind === 'node') {
|
|
104
|
+
const nodeRef = env.refs.get(refExpr.name);
|
|
105
|
+
nodeRef.used = true;
|
|
106
|
+
refs.push(nodeRef.cVar);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
throw new Error('AOT: ref={…} must reference a node ref declared with useRef()');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return refs;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A JSX element's named attribute, or undefined.
|
|
116
|
+
*
|
|
117
|
+
* @param openingElement The element's opening tag.
|
|
118
|
+
* @param name The attribute's name.
|
|
119
|
+
*
|
|
120
|
+
* @returns The attribute, if the element has it.
|
|
121
|
+
*/
|
|
122
|
+
function namedAttr(openingElement, name) {
|
|
123
|
+
return openingElement.attributes.find((attr) => attr.type === 'JSXAttribute' && attr.name && attr.name.name === name);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* True for `<Pressable disabled>` / `<TouchableOpacity disabled={SOME_CONST}>`. Must fold: `disabled`
|
|
127
|
+
* decides what the build EMITS — the press handlers and any dim binding — so there is nothing left to
|
|
128
|
+
* decide at runtime.
|
|
129
|
+
*
|
|
130
|
+
* @param element The <Pressable> or <TouchableOpacity>.
|
|
131
|
+
* @param tag Its tag, for the error.
|
|
132
|
+
* @param scope Compile-time constants in reach.
|
|
133
|
+
*
|
|
134
|
+
* @returns Whether it is disabled.
|
|
135
|
+
*/
|
|
136
|
+
function pressableIsDisabled(element, tag, scope) {
|
|
137
|
+
const attr = namedAttr(element.openingElement, 'disabled');
|
|
138
|
+
if (!attr)
|
|
139
|
+
return false;
|
|
140
|
+
return !!evalStaticOrThrow(attrExpr(attr), scope, `AOT: <${tag} disabled> must be a compile-time constant`, 'a disabled one still renders — it lowers to a plain <Pressable>, children, layout ' +
|
|
141
|
+
'and style intact — but its press handlers are not emitted at all, so the ' +
|
|
142
|
+
'AOT has to know at build time. To gate on state, leave it enabled and return early ' +
|
|
143
|
+
'in the handler (`onPress={() => { if (!ready) return; … }}`).');
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* A <Text>'s truncation: `numberOfLines` must fold to a whole number of lines the engine's byte holds, and
|
|
147
|
+
* `ellipsizeMode` to a mode the engine draws.
|
|
148
|
+
*
|
|
149
|
+
* @param openingElement The <Text>'s opening tag.
|
|
150
|
+
* @param scope Compile-time constants in reach.
|
|
151
|
+
*
|
|
152
|
+
* @returns The ERProps writes.
|
|
153
|
+
*/
|
|
154
|
+
function lowerTextTruncation(openingElement, scope) {
|
|
155
|
+
const assigns = [];
|
|
156
|
+
const linesAttr = namedAttr(openingElement, 'numberOfLines');
|
|
157
|
+
if (linesAttr) {
|
|
158
|
+
const lines = foldAttr(linesAttr, scope, 'AOT: <Text numberOfLines> must be a compile-time constant');
|
|
159
|
+
if (typeof lines !== 'number' ||
|
|
160
|
+
!Number.isInteger(lines) ||
|
|
161
|
+
lines < 0 ||
|
|
162
|
+
lines > 255) {
|
|
163
|
+
throw at(aotError(`AOT: <Text numberOfLines> must be a whole number from 0 to 255 (got ${String(lines)})`, '0 does not truncate; any other value is the most lines the text wraps to.'), linesAttr);
|
|
164
|
+
}
|
|
165
|
+
assigns.push({ field: 'number_of_lines', expr: String(lines) });
|
|
166
|
+
}
|
|
167
|
+
const modeAttr = namedAttr(openingElement, 'ellipsizeMode');
|
|
168
|
+
if (modeAttr) {
|
|
169
|
+
const mode = foldAttr(modeAttr, scope, 'AOT: <Text ellipsizeMode> must be a compile-time constant');
|
|
170
|
+
if (!Object.hasOwn(ELLIPSIZE_MODES, mode)) {
|
|
171
|
+
throw at(aotError(`AOT: <Text ellipsizeMode> "${String(mode)}" is not drawn by the engine`, "use 'tail' or 'clip'; the engine draws 'head' and 'middle' as 'tail'."), modeAttr);
|
|
172
|
+
}
|
|
173
|
+
assigns.push({
|
|
174
|
+
field: 'ellipsize_mode',
|
|
175
|
+
expr: ELLIPSIZE_MODES[mode],
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return assigns;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* An attribute's value, which must fold; the error is pinned to the attribute.
|
|
182
|
+
*
|
|
183
|
+
* @param attr The attribute.
|
|
184
|
+
* @param scope Compile-time constants in reach.
|
|
185
|
+
* @param message The error when it does not fold.
|
|
186
|
+
*
|
|
187
|
+
* @returns The folded value.
|
|
188
|
+
*/
|
|
189
|
+
function foldAttr(attr, scope, message) {
|
|
190
|
+
try {
|
|
191
|
+
return evalStaticOrThrow(attrExpr(attr), scope, message);
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
throw at(error, attr);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* A folded `activeOpacity` as a number in 0..1. Everything else is an error HERE, where the source line
|
|
199
|
+
* is still in reach: it would otherwise reach floatLit and land in the generated C as a literal that
|
|
200
|
+
* either does not compile (`NaNf`) or quietly means something the app never asked for — a bare
|
|
201
|
+
* `activeOpacity` is `true`, so `1.0f`, which is not dim at all; `{false}` is an invisible one.
|
|
202
|
+
*
|
|
203
|
+
* @param value The folded value.
|
|
204
|
+
* @param attr The attribute, for the error's location.
|
|
205
|
+
*
|
|
206
|
+
* @returns The opacity.
|
|
207
|
+
*/
|
|
208
|
+
function opacityLiteralOrThrow(value, attr) {
|
|
209
|
+
if (typeof value !== 'number' ||
|
|
210
|
+
!Number.isFinite(value) ||
|
|
211
|
+
value < 0 ||
|
|
212
|
+
value > 1) {
|
|
213
|
+
// JSON.stringify turns NaN into the STRING "null", which reads as a different mistake entirely.
|
|
214
|
+
const shown = typeof value === 'number'
|
|
215
|
+
? String(value)
|
|
216
|
+
: (JSON.stringify(value) ?? String(value));
|
|
217
|
+
const error = aotError(`AOT: <TouchableOpacity activeOpacity> must be a number between 0 and 1 (got ${shown})`, 'it is the opacity the node dims to while held — 0 is invisible, 1 is no dim at all. RN defaults it to 0.2.');
|
|
218
|
+
if (attr.loc) {
|
|
219
|
+
error.aotLoc = attr.loc.start;
|
|
220
|
+
}
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
return value;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Lowers <TouchableOpacity>'s press feedback: a Pressable node plus one synthetic animated value bound to its
|
|
227
|
+
* opacity. Press-in snaps it to activeOpacity, press-out fades it back to whatever the style asks for. Flow A
|
|
228
|
+
* builds the same thing out of hooks (TouchableOpacity.js); here the feedback runs on the engine's native
|
|
229
|
+
* driver with no JS on the device at all. Returns the value's bind, and the two fades for the event loop to
|
|
230
|
+
* fold into its press handlers.
|
|
231
|
+
*
|
|
232
|
+
* @param element The <TouchableOpacity>.
|
|
233
|
+
* @param nodeId The node's variable.
|
|
234
|
+
* @param staticAssigns The style's constant writes.
|
|
235
|
+
* @param dynAssigns The style's state-driven writes.
|
|
236
|
+
* @param binds The style's animated-value binds.
|
|
237
|
+
* @param out Everything emitted so far.
|
|
238
|
+
* @param scope Compile-time constants in reach.
|
|
239
|
+
*
|
|
240
|
+
* @returns The opacity bind, and the fade to run on onPressIn and the one to run on onPressOut.
|
|
241
|
+
*/
|
|
242
|
+
function touchablePressFades(element, nodeId, staticAssigns, dynAssigns, binds, out, scope) {
|
|
243
|
+
// The press feedback owns the node's opacity, so the style may not animate it or drive it from state.
|
|
244
|
+
const hasAnimatedOpacity = binds.some(bind => bind.prop === 'ER_PROP_OPACITY');
|
|
245
|
+
if (hasAnimatedOpacity ||
|
|
246
|
+
dynAssigns.some(assign => assign.field === 'opacity')) {
|
|
247
|
+
throw aotError(`AOT: <TouchableOpacity> cannot take ${hasAnimatedOpacity ? 'an Animated' : 'a state-driven'} opacity`, "the press feedback owns this node's opacity — a second writer does not blend with " +
|
|
248
|
+
'it, it races it. To animate opacity yourself, use <Pressable>: the dim is just ' +
|
|
249
|
+
'onPressIn/onPressOut driving an Animated.Value (see TouchableOpacity.js for the ' +
|
|
250
|
+
'whole of it).');
|
|
251
|
+
}
|
|
252
|
+
// lowerStyle scaled static opacity to 0–255 (the resting value); leave a NaN one for the C compiler to reject.
|
|
253
|
+
const opacityAssign = staticAssigns.find(assign => assign.field === 'opacity');
|
|
254
|
+
const restingRaw = opacityAssign ? Number(opacityAssign.expr) / 255 : 1;
|
|
255
|
+
const restingOpacity = Number.isFinite(restingRaw) ? restingRaw : 1;
|
|
256
|
+
// The dim target: a folded activeOpacity, or RN's default.
|
|
257
|
+
const activeAttr = namedAttr(element.openingElement, 'activeOpacity');
|
|
258
|
+
const activeOpacity = activeAttr
|
|
259
|
+
? opacityLiteralOrThrow(evalStaticOrThrow(attrExpr(activeAttr), scope, 'AOT: <TouchableOpacity activeOpacity> must be a compile-time constant', 'the dim target is baked into the generated handler, so it cannot come from state.'), activeAttr)
|
|
260
|
+
: TOUCHABLE_ACTIVE_OPACITY;
|
|
261
|
+
// Bind a fresh animated value, starting at rest, to the node's opacity.
|
|
262
|
+
const cVar = `s_av_press_${nodeId}`;
|
|
263
|
+
checkNumber(restingOpacity);
|
|
264
|
+
out.childAnimations.push({ cVar, initial: restingOpacity });
|
|
265
|
+
// Each fade is one timing animation of that value.
|
|
266
|
+
return {
|
|
267
|
+
bind: { cVar, prop: 'ER_PROP_OPACITY' },
|
|
268
|
+
fades: {
|
|
269
|
+
onPressIn: { value: cVar, opacity: activeOpacity, durationMs: 0 },
|
|
270
|
+
onPressOut: {
|
|
271
|
+
value: cVar,
|
|
272
|
+
opacity: restingOpacity,
|
|
273
|
+
durationMs: TOUCHABLE_RESTORE_MS,
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Shows a typed element only while the state-driven conditional it was built under picks it, as the host-node
|
|
280
|
+
* path does through `display`. The engine derives a Modal's display from `modal_visible`, so a Modal's
|
|
281
|
+
* condition gates that flag instead.
|
|
282
|
+
*
|
|
283
|
+
* @param node The element's node.
|
|
284
|
+
* @param show When it shows.
|
|
285
|
+
*/
|
|
286
|
+
function applyShow(node, show) {
|
|
287
|
+
if (node.type !== 'ER_NODE_MODAL') {
|
|
288
|
+
node.stateProps.push({ field: 'display', value: { kind: 'shown', show } });
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const constantAt = node.constantProps.findIndex(assign => assign.field === 'modal_visible');
|
|
292
|
+
if (constantAt >= 0) {
|
|
293
|
+
const [visible] = node.constantProps.splice(constantAt, 1);
|
|
294
|
+
node.stateProps.push({
|
|
295
|
+
field: 'modal_visible',
|
|
296
|
+
value: {
|
|
297
|
+
kind: 'whileShown',
|
|
298
|
+
show,
|
|
299
|
+
value: { kind: 'constant', code: visible.expr },
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const visible = node.stateProps.find(assign => assign.field === 'modal_visible');
|
|
305
|
+
visible.value = { kind: 'whileShown', show, value: visible.value };
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Lowers an event prop's function to a handler: a useCallback compiles once per instance and is shared, a
|
|
309
|
+
* callback prop compiles in its caller's environment, and an inline function compiles where it is.
|
|
310
|
+
*
|
|
311
|
+
* @param attr The event prop.
|
|
312
|
+
* @param out Everything emitted so far.
|
|
313
|
+
* @param env The expression environment.
|
|
314
|
+
* @param state The component's state table.
|
|
315
|
+
*
|
|
316
|
+
* @returns The handler's name.
|
|
317
|
+
*/
|
|
318
|
+
export function lowerEventHandler(attr, out, env, state) {
|
|
319
|
+
const fn = attrExpr(attr);
|
|
320
|
+
if (fn.type === 'Identifier' && env.callbacks?.has(fn.name)) {
|
|
321
|
+
// A useCallback compiles to one shared handler, namespaced by cbPrefix so each child instance gets its own.
|
|
322
|
+
const callbackKey = `${env.cbPrefix ?? ''}${fn.name}`;
|
|
323
|
+
let handlerName = out.cbEmitted.get(callbackKey);
|
|
324
|
+
if (!handlerName) {
|
|
325
|
+
handlerName = `er_cb_${callbackKey}`;
|
|
326
|
+
out.cbEmitted.set(callbackKey, handlerName);
|
|
327
|
+
out.handlers.push({
|
|
328
|
+
kind: 'callback',
|
|
329
|
+
name: handlerName,
|
|
330
|
+
callback: lowerHandler(env.callbacks.get(fn.name), env, state, out),
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
return handlerName;
|
|
334
|
+
}
|
|
335
|
+
const handlerName = `er_handler_${out.handlers.length}`;
|
|
336
|
+
if (fn.type === 'Identifier' && env.fnProps?.has(fn.name)) {
|
|
337
|
+
// Inline a callback prop's function, compiled in the caller's env/state so its setters and locals resolve.
|
|
338
|
+
const callbackProp = env.fnProps.get(fn.name);
|
|
339
|
+
out.handlers.push({
|
|
340
|
+
kind: 'callback',
|
|
341
|
+
name: handlerName,
|
|
342
|
+
callback: lowerHandler(callbackProp.node, callbackProp.env, callbackProp.state, out),
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
else if (isFn(fn)) {
|
|
346
|
+
out.handlers.push({
|
|
347
|
+
kind: 'callback',
|
|
348
|
+
name: handlerName,
|
|
349
|
+
callback: lowerHandler(fn, env, state, out),
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
throw aotError(`AOT: ${attr.name.name} must be an inline function, a useCallback, or a callback prop`, `pass an inline arrow (onPress={() => setX(…)}), a useCallback identifier, or a ` +
|
|
354
|
+
`function prop received by this component.`);
|
|
355
|
+
}
|
|
356
|
+
return handlerName;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Lowers one JSX element to its engine node (see emitNode, the located entry point): dispatches the typed
|
|
360
|
+
* elements (Svg, Switch, Dial, TextInput, ActivityIndicator, Modal, FlatList) and function components to
|
|
361
|
+
* their emitters, and builds a generic host node (View / Text / Pressable / TouchableOpacity / Image /
|
|
362
|
+
* ScrollView) itself: style, text, events, refs, and children.
|
|
363
|
+
*
|
|
364
|
+
* @param element The element.
|
|
365
|
+
* @param scope Compile-time constants in reach.
|
|
366
|
+
* @param out Everything emitted so far.
|
|
367
|
+
* @param env The expression environment.
|
|
368
|
+
* @param state The component's state table.
|
|
369
|
+
* @param opts How the element is placed.
|
|
370
|
+
*
|
|
371
|
+
* @returns What the element lowered to.
|
|
372
|
+
*/
|
|
373
|
+
function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
374
|
+
// Refuse any prop the element does not declare, whatever its value, then drop the no-ops and the props that
|
|
375
|
+
// fold to undefined before dispatch.
|
|
376
|
+
const tag = resolveTag(element.openingElement);
|
|
377
|
+
const spec = elementSpec(tag);
|
|
378
|
+
if (spec) {
|
|
379
|
+
checkAttributes(spec, element.openingElement);
|
|
380
|
+
if (spec.children === 'none') {
|
|
381
|
+
checkNoChildren(spec, element.children);
|
|
382
|
+
}
|
|
383
|
+
element = withoutPlatformNoOps(spec, element);
|
|
384
|
+
}
|
|
385
|
+
element = omitUndefinedAttrs(element, scope, env);
|
|
386
|
+
const typed = (node) => {
|
|
387
|
+
if (opts.show) {
|
|
388
|
+
applyShow(node, opts.show);
|
|
389
|
+
}
|
|
390
|
+
return { kind: 'node', id: node.id, node };
|
|
391
|
+
};
|
|
392
|
+
if (tag === 'Svg') {
|
|
393
|
+
return typed(emitSvg(element, scope, out, env, state, opts));
|
|
394
|
+
}
|
|
395
|
+
if (tag === 'Switch') {
|
|
396
|
+
return typed(emitSwitch(element, scope, out, env, state));
|
|
397
|
+
}
|
|
398
|
+
if (tag === 'Dial')
|
|
399
|
+
return typed(emitDial(element, scope, out, env, state));
|
|
400
|
+
if (tag === 'TextInput') {
|
|
401
|
+
return typed(emitTextInput(element, scope, out, env, state));
|
|
402
|
+
}
|
|
403
|
+
if (tag === 'ActivityIndicator') {
|
|
404
|
+
return typed(emitActivityIndicator(element, scope, out, env));
|
|
405
|
+
}
|
|
406
|
+
if (tag === 'Modal') {
|
|
407
|
+
return typed(emitModal(element, scope, out, env, state));
|
|
408
|
+
}
|
|
409
|
+
if (tag === 'FlatList') {
|
|
410
|
+
return emitFlatList(element, scope, out, env, state, opts);
|
|
411
|
+
}
|
|
412
|
+
// Anything else must be a host node or a function component defined in this file.
|
|
413
|
+
const nodeType = NODE_TYPES[tag];
|
|
414
|
+
if (!nodeType) {
|
|
415
|
+
if (out.components.has(tag)) {
|
|
416
|
+
return emitComponent(element, scope, out, env, state, opts);
|
|
417
|
+
}
|
|
418
|
+
// A package export with no lowering yet gets its own error; "unknown element" would read like a typo.
|
|
419
|
+
const replacement = FLOW_A_ONLY_COMPONENTS[tag];
|
|
420
|
+
if (replacement) {
|
|
421
|
+
throw aotError(`AOT: <${tag}> is not supported in Flow B yet`, `it renders in Flow A (the simulator and the QuickJS runtime) but the AOT has no ` +
|
|
422
|
+
`lowering for it. Write it out by hand for the device build: ${replacement}`);
|
|
423
|
+
}
|
|
424
|
+
throw aotError(`AOT: unknown element <${tag}> (not a built-in or a component in this file)`, `<${tag}> must be a built-in (View / Text / Pressable / TouchableOpacity / Image / ` +
|
|
425
|
+
`ScrollView / Svg + shapes / Animated.*) or a function component defined in THIS ` +
|
|
426
|
+
`file. Check the import / spelling or define the component here.`);
|
|
427
|
+
}
|
|
428
|
+
// A disabled <Pressable> / <TouchableOpacity> has no press handlers or dim; the engine has no disabled flag.
|
|
429
|
+
const isPressDisabled = (tag === 'Pressable' || tag === 'TouchableOpacity') &&
|
|
430
|
+
pressableIsDisabled(element, tag, scope);
|
|
431
|
+
// Reject spreads, which the loops below would silently drop, except {...pan.panHandlers}.
|
|
432
|
+
const panSpreads = [];
|
|
433
|
+
for (const spread of element.openingElement.attributes) {
|
|
434
|
+
if (spread.type !== 'JSXSpreadAttribute')
|
|
435
|
+
continue;
|
|
436
|
+
let panResponder;
|
|
437
|
+
try {
|
|
438
|
+
panResponder = panSpreadTarget(spread.argument, env);
|
|
439
|
+
}
|
|
440
|
+
catch (error) {
|
|
441
|
+
if (spread.loc && !error.aotLoc) {
|
|
442
|
+
error.aotLoc = spread.loc.start;
|
|
443
|
+
}
|
|
444
|
+
throw error;
|
|
445
|
+
}
|
|
446
|
+
if (panResponder) {
|
|
447
|
+
panSpreads.push(panResponder);
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
const spreadError = aotError(`AOT: a spread {...} on <${tag}> is not supported`, `list each prop explicitly (e.g. style={…} onPress={…}). The only spread a host ` +
|
|
451
|
+
`element understands is a PanResponder's ({...pan.panHandlers}); otherwise spread ` +
|
|
452
|
+
`props are supported on a function component instance whose spread object folds to a ` +
|
|
453
|
+
`compile-time constant.`);
|
|
454
|
+
if (spread.loc) {
|
|
455
|
+
spreadError.aotLoc = spread.loc.start;
|
|
456
|
+
}
|
|
457
|
+
throw spreadError;
|
|
458
|
+
}
|
|
459
|
+
// Allocate the node and collect its style, text content, and image attributes.
|
|
460
|
+
const nodeId = `n${out.allocateNodeId()}`;
|
|
461
|
+
const { staticAssigns, dynAssigns, binds } = collectStyleAssigns(element.openingElement, scope, env, spec);
|
|
462
|
+
if (tag === 'Text') {
|
|
463
|
+
staticAssigns.push(...lowerTextTruncation(element.openingElement, scope));
|
|
464
|
+
}
|
|
465
|
+
// A <Text> with a nested <Text> becomes inline SPANS; otherwise a single (possibly dynamic) string.
|
|
466
|
+
const spans = tag === 'Text' ? collectTextSpans(element.children, scope, env) : null;
|
|
467
|
+
const text = tag === 'Text' && !spans
|
|
468
|
+
? lowerNodeText(element.children, scope, env)
|
|
469
|
+
: null;
|
|
470
|
+
// An <Image>'s resize/tint are static, so fold them into staticAssigns for both the static and dynamic paths.
|
|
471
|
+
const image = tag === 'Image' ? resolveImageAttrs(element, env, out) : null;
|
|
472
|
+
if (image?.resizeMode) {
|
|
473
|
+
staticAssigns.push({ field: 'resize_mode', expr: image.resizeMode });
|
|
474
|
+
}
|
|
475
|
+
if (image?.tintColor) {
|
|
476
|
+
staticAssigns.push({ field: 'tint_color', expr: image.tintColor });
|
|
477
|
+
}
|
|
478
|
+
// `visible` is `display` as in Flow A (style `display` wins); not in collectStyleAssigns, which <Modal> shares.
|
|
479
|
+
const visibleAttr = element.openingElement.attributes.find((attr) => attr.type === 'JSXAttribute' && attr.name && attr.name.name === 'visible');
|
|
480
|
+
if (visibleAttr &&
|
|
481
|
+
!staticAssigns.some(assign => assign.field === 'display') &&
|
|
482
|
+
!dynAssigns.some(assign => assign.field === 'display')) {
|
|
483
|
+
const expr = visibleAttr.value == null ? null : attrExpr(visibleAttr); // bare `visible` === true
|
|
484
|
+
if (expr == null) {
|
|
485
|
+
staticAssigns.push({ field: 'display', expr: 'ER_DISPLAY_FLEX' });
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
try {
|
|
489
|
+
staticAssigns.push({
|
|
490
|
+
field: 'display',
|
|
491
|
+
expr: evalStatic(expr, scope) ? 'ER_DISPLAY_FLEX' : 'ER_DISPLAY_NONE',
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
catch {
|
|
495
|
+
dynAssigns.push({
|
|
496
|
+
field: 'display',
|
|
497
|
+
value: { kind: 'visible', value: lowerExpr(expr, env) },
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
// delayLongPress must fold since it is baked into props; 0 becomes 1 ms because 0 means "default" to the engine.
|
|
503
|
+
const delayAttr = namedAttr(element.openingElement, 'delayLongPress');
|
|
504
|
+
if (delayAttr) {
|
|
505
|
+
const delayError = aotError(`AOT: <${tag} delayLongPress> must fold to a number`, 'pass a literal or a module-level constant in milliseconds, e.g. delayLongPress={800}.');
|
|
506
|
+
if (delayAttr.value == null) {
|
|
507
|
+
throw delayError;
|
|
508
|
+
}
|
|
509
|
+
let delayMs;
|
|
510
|
+
try {
|
|
511
|
+
delayMs = Number(evalStatic(attrExpr(delayAttr), scope));
|
|
512
|
+
}
|
|
513
|
+
catch {
|
|
514
|
+
throw delayError;
|
|
515
|
+
}
|
|
516
|
+
if (!Number.isFinite(delayMs)) {
|
|
517
|
+
throw delayError;
|
|
518
|
+
}
|
|
519
|
+
staticAssigns.push({
|
|
520
|
+
field: 'long_press_ms',
|
|
521
|
+
expr: String(Math.min(65535, Math.max(1, Math.round(delayMs)))),
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
// A state-driven conditional flips `display` in app_update; pushed last to beat the element's visible/display.
|
|
525
|
+
if (opts.show) {
|
|
526
|
+
dynAssigns.push({
|
|
527
|
+
field: 'display',
|
|
528
|
+
value: { kind: 'shown', show: opts.show },
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
// <TouchableOpacity>'s own opacity binding, on top of any the style asked for.
|
|
532
|
+
const press = tag === 'TouchableOpacity' && !isPressDisabled
|
|
533
|
+
? touchablePressFades(element, nodeId, staticAssigns, dynAssigns, binds, out, scope)
|
|
534
|
+
: null;
|
|
535
|
+
// A host node wires its binds (the press feedback's last), then its refs, events and PanResponders.
|
|
536
|
+
const wiring = binds.map((bind, index) => ({
|
|
537
|
+
kind: 'bind',
|
|
538
|
+
bind,
|
|
539
|
+
index,
|
|
540
|
+
}));
|
|
541
|
+
if (press) {
|
|
542
|
+
wiring.push({ kind: 'bind', bind: press.bind, index: binds.length });
|
|
543
|
+
}
|
|
544
|
+
for (const slot of lowerRefBinds(element.openingElement, env)) {
|
|
545
|
+
wiring.push({ kind: 'ref', slot });
|
|
546
|
+
}
|
|
547
|
+
const node = {
|
|
548
|
+
id: nodeId,
|
|
549
|
+
type: nodeType,
|
|
550
|
+
constantProps: staticAssigns,
|
|
551
|
+
stateProps: dynAssigns,
|
|
552
|
+
placeholder: null,
|
|
553
|
+
text,
|
|
554
|
+
image: image?.imageNameDyn != null
|
|
555
|
+
? { kind: 'value', value: image.imageNameDyn }
|
|
556
|
+
: image?.imageName != null
|
|
557
|
+
? { kind: 'static', name: image.imageName, resetOnUpdate: false }
|
|
558
|
+
: null,
|
|
559
|
+
spans,
|
|
560
|
+
wiring,
|
|
561
|
+
children: [],
|
|
562
|
+
};
|
|
563
|
+
// Compile each event prop into a handler; a disabled one drops its press events.
|
|
564
|
+
const fadesWired = new Set();
|
|
565
|
+
for (const attr of element.openingElement.attributes) {
|
|
566
|
+
if (attr.type !== 'JSXAttribute')
|
|
567
|
+
continue;
|
|
568
|
+
const eventType = EVENT_TYPES[attr.name.name];
|
|
569
|
+
if (!eventType)
|
|
570
|
+
continue;
|
|
571
|
+
if (isPressDisabled &&
|
|
572
|
+
PRESS_EVENTS.has(attr.name.name)) {
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
let handlerName = lowerEventHandler(attr, out, env, state);
|
|
576
|
+
// Wrap the app's press-in/out with the fade; prepending would dim every other element sharing a useCallback.
|
|
577
|
+
const pressFade = press?.fades[attr.name.name];
|
|
578
|
+
if (pressFade) {
|
|
579
|
+
const wrapperName = `er_handler_${out.handlers.length}`;
|
|
580
|
+
out.handlers.push({
|
|
581
|
+
kind: 'pressFade',
|
|
582
|
+
name: wrapperName,
|
|
583
|
+
fade: pressFade,
|
|
584
|
+
then: handlerName,
|
|
585
|
+
});
|
|
586
|
+
handlerName = wrapperName;
|
|
587
|
+
fadesWired.add(attr.name.name);
|
|
588
|
+
}
|
|
589
|
+
wiring.push({ kind: 'event', event: eventType, handler: handlerName });
|
|
590
|
+
}
|
|
591
|
+
// Whichever end of the press the app did not handle itself is the fade on its own.
|
|
592
|
+
if (press) {
|
|
593
|
+
for (const eventName of ['onPressIn', 'onPressOut']) {
|
|
594
|
+
if (fadesWired.has(eventName))
|
|
595
|
+
continue;
|
|
596
|
+
const fadeHandlerName = `er_handler_${out.handlers.length}`;
|
|
597
|
+
out.handlers.push({
|
|
598
|
+
kind: 'pressFade',
|
|
599
|
+
name: fadeHandlerName,
|
|
600
|
+
fade: press.fades[eventName],
|
|
601
|
+
then: null,
|
|
602
|
+
});
|
|
603
|
+
wiring.push({
|
|
604
|
+
kind: 'event',
|
|
605
|
+
event: EVENT_TYPES[eventName],
|
|
606
|
+
handler: fadeHandlerName,
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
// A PanResponder is lowered on the first node that spreads it.
|
|
611
|
+
for (const panResponder of panSpreads) {
|
|
612
|
+
wiring.push({
|
|
613
|
+
kind: 'responder',
|
|
614
|
+
pan: emitPanResponder(panResponder, out, env, state),
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
checkInterpolations(node);
|
|
618
|
+
// A <Text>'s children are its text, already consumed above.
|
|
619
|
+
if (tag !== 'Text') {
|
|
620
|
+
node.children = emitChildren(element.children, scope, out, env, state);
|
|
621
|
+
}
|
|
622
|
+
return { kind: 'node', id: nodeId, node };
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* `element` without the attributes whose value folds to `undefined` in this scope — typically a prop a
|
|
626
|
+
* child component was never given. Flow A omits an undefined prop, so every reader has to see it as absent;
|
|
627
|
+
* read as a value, `visible` would hide the node and `placeholder` would print the word "undefined". This
|
|
628
|
+
* is the one place that rule lives: every element reaches its reader through here. A copy is returned only
|
|
629
|
+
* when something is dropped, because the same JSX node is emitted once per scope (each .map row, each
|
|
630
|
+
* component instance) and one scope's answer must not leak into another's.
|
|
631
|
+
*
|
|
632
|
+
* @param element The element.
|
|
633
|
+
* @param scope Compile-time constants in reach.
|
|
634
|
+
* @param env The expression environment.
|
|
635
|
+
*
|
|
636
|
+
* @returns The element, or a copy of it without those attributes.
|
|
637
|
+
*/
|
|
638
|
+
function omitUndefinedAttrs(element, scope, env) {
|
|
639
|
+
const attrs = element.openingElement.attributes;
|
|
640
|
+
// Built lazily: the runtime-aware scope plus the global `undefined`, so `SHOW ? true : undefined` folds too.
|
|
641
|
+
let foldingScope = null;
|
|
642
|
+
const keptAttrs = attrs.filter(attr => {
|
|
643
|
+
if (attr.type !== 'JSXAttribute' ||
|
|
644
|
+
attr.value?.type !== 'JSXExpressionContainer') {
|
|
645
|
+
return true;
|
|
646
|
+
}
|
|
647
|
+
foldingScope ??= Object.assign(Object.create(foldScope(env, scope)), {
|
|
648
|
+
undefined,
|
|
649
|
+
});
|
|
650
|
+
try {
|
|
651
|
+
return evalStatic(attr.value.expression, foldingScope) !== undefined;
|
|
652
|
+
}
|
|
653
|
+
catch {
|
|
654
|
+
return true; // not a compile-time value — its reader decides
|
|
655
|
+
}
|
|
656
|
+
});
|
|
657
|
+
return keptAttrs.length === attrs.length
|
|
658
|
+
? element
|
|
659
|
+
: {
|
|
660
|
+
...element,
|
|
661
|
+
openingElement: { ...element.openingElement, attributes: keptAttrs },
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Refuses an interpolation table holding a number C cannot write, at the element that binds it rather than when
|
|
666
|
+
* its build lines are written.
|
|
667
|
+
*
|
|
668
|
+
* @param node The node.
|
|
669
|
+
*/
|
|
670
|
+
export function checkInterpolations(node) {
|
|
671
|
+
for (const wire of node.wiring) {
|
|
672
|
+
if (wire.kind !== 'bind' || !wire.bind.interp)
|
|
673
|
+
continue;
|
|
674
|
+
for (const point of [
|
|
675
|
+
...wire.bind.interp.input,
|
|
676
|
+
...wire.bind.interp.output,
|
|
677
|
+
]) {
|
|
678
|
+
checkNumber(point);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
/** Lowers one JSX element to its engine node; an AOT error it throws is pinned to the element. */
|
|
683
|
+
export const emitNode = withLoc(emitNodeImpl);
|