embedded-react 0.18.0 → 0.20.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/NOTICE +1 -1
- package/README.md +8 -4
- package/aot/compile.mjs +178 -58
- package/aot/compiler/animations.mjs +290 -281
- package/aot/compiler/boot.mjs +70 -33
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +9 -9
- package/aot/compiler/collect.mjs +324 -205
- package/aot/compiler/control-flow.mjs +209 -197
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image.mjs +51 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +23 -23
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +372 -333
- package/aot/compiler/handlers.mjs +473 -387
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +4 -4
- package/aot/compiler/ir/reads.mjs +19 -19
- package/aot/compiler/ir/types.mjs +14 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/lower.mjs +77 -80
- package/aot/compiler/nodes.mjs +169 -152
- package/aot/compiler/options.mjs +35 -30
- package/aot/compiler/out.mjs +18 -22
- package/aot/compiler/package/animations.mjs +669 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +177 -0
- package/aot/compiler/package/bytecode.mjs +286 -0
- package/aot/compiler/package/checks.mjs +1022 -0
- package/aot/compiler/package/codegen.mjs +1329 -0
- package/aot/compiler/package/enums.mjs +138 -0
- package/aot/compiler/package/format.mjs +760 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +1606 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/tables.mjs +103 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +664 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +21 -21
- package/aot/compiler/static-eval.mjs +25 -23
- package/aot/compiler/style-text.mjs +161 -172
- package/aot/compiler/svg.mjs +477 -251
- package/aot/screenshot-smoke.mjs +55 -41
- package/aot/style-map.mjs +102 -90
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +14 -10
- package/assets/emit-c.mjs +83 -62
- package/assets/emit-container.mjs +124 -38
- package/assets/emit-pack.mjs +77 -67
- package/assets/font-config.mjs +9 -6
- package/assets/font-sizes.mjs +163 -123
- package/assets/glyph-coverage.mjs +74 -54
- package/assets/index.mjs +31 -9
- package/assets/rasterize.mjs +126 -99
- package/assets/svg-loader.mjs +8 -8
- package/build.mjs +16 -14
- package/cli.mjs +359 -172
- package/compile-bin.mjs +5 -5
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +46 -37
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +51 -38
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +31 -23
- package/package.json +11 -12
- package/persist-transform.mjs +34 -28
- package/qjsc-wasm.mjs +25 -14
- package/sim/embedded-react.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +94 -85
- package/sim-server.mjs +101 -82
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +4 -4
- package/src/embedded-react/LayoutAnimation.js +8 -5
- package/src/embedded-react/PanResponder.js +75 -71
- package/src/embedded-react/Pressable.js +2 -8
- package/src/embedded-react/SectionList.js +6 -6
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +5 -5
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
package/aot/compiler/nodes.mjs
CHANGED
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { checkNumber } from './check.mjs';
|
|
20
|
-
import { NODE_TYPES } from '../style-map.mjs';
|
|
21
|
-
import { aotError, checkEngineString, constantText, withLoc, } from './diagnostics.mjs';
|
|
20
|
+
import { NODE_TYPES, OPACITY_OPAQUE } from '../style-map.mjs';
|
|
21
|
+
import { aotError, checkEngineString, constantText, currentLoc, withLoc, } from './diagnostics.mjs';
|
|
22
22
|
import { buildValueOf, foldBoot } from './boot.mjs';
|
|
23
23
|
import { evalStaticOrThrow, evalStatic, foldScope } from './static-eval.mjs';
|
|
24
24
|
import { lowerExpr } from './expressions.mjs';
|
|
@@ -62,6 +62,10 @@ const ELLIPSIZE_MODES = {
|
|
|
62
62
|
tail: 'ER_TEXT_ELLIPSIZE_TAIL',
|
|
63
63
|
clip: 'ER_TEXT_ELLIPSIZE_CLIP',
|
|
64
64
|
};
|
|
65
|
+
/** The most lines a <Text> may keep: the engine counts them in a byte. */
|
|
66
|
+
const MAX_NUMBER_OF_LINES = 255;
|
|
67
|
+
/** The longest long press, in ms: the engine keeps it in 16 bits. */
|
|
68
|
+
const MAX_LONG_PRESS_MS = 65535;
|
|
65
69
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
66
70
|
- Implementation
|
|
67
71
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -86,25 +90,25 @@ function resolveTag(openingElement) {
|
|
|
86
90
|
* The node refs `ref={myRef}` stores the element's handle in (a node ref from useRef), marked used.
|
|
87
91
|
*
|
|
88
92
|
* @param openingElement The element's opening tag.
|
|
89
|
-
* @param
|
|
93
|
+
* @param environment The expression environment.
|
|
90
94
|
*
|
|
91
|
-
* @returns The refs'
|
|
95
|
+
* @returns The refs' slots.
|
|
92
96
|
*/
|
|
93
|
-
export function lowerRefBinds(openingElement,
|
|
97
|
+
export function lowerRefBinds(openingElement, environment) {
|
|
94
98
|
const refs = [];
|
|
95
|
-
for (const
|
|
96
|
-
if (
|
|
97
|
-
|
|
99
|
+
for (const attribute of openingElement.attributes) {
|
|
100
|
+
if (attribute.type !== 'JSXAttribute' ||
|
|
101
|
+
attribute.name.name !== 'ref') {
|
|
98
102
|
continue;
|
|
99
103
|
}
|
|
100
|
-
const
|
|
101
|
-
?
|
|
104
|
+
const refExpression = attribute.value?.type === 'JSXExpressionContainer'
|
|
105
|
+
? attribute.value.expression
|
|
102
106
|
: null;
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
const nodeRef =
|
|
107
|
+
if (refExpression?.type === 'Identifier' &&
|
|
108
|
+
environment.refs?.get(refExpression.name)?.kind === 'node') {
|
|
109
|
+
const nodeRef = environment.refs.get(refExpression.name);
|
|
106
110
|
nodeRef.used = true;
|
|
107
|
-
refs.push(nodeRef.
|
|
111
|
+
refs.push(nodeRef.slotName);
|
|
108
112
|
}
|
|
109
113
|
else {
|
|
110
114
|
throw new Error('AOT: ref={…} must reference a node ref declared with useRef()');
|
|
@@ -121,7 +125,9 @@ export function lowerRefBinds(openingElement, env) {
|
|
|
121
125
|
* @returns The attribute, if the element has it.
|
|
122
126
|
*/
|
|
123
127
|
function namedAttr(openingElement, name) {
|
|
124
|
-
return openingElement.attributes.find((
|
|
128
|
+
return openingElement.attributes.find((attribute) => attribute.type === 'JSXAttribute' &&
|
|
129
|
+
attribute.name &&
|
|
130
|
+
attribute.name.name === name);
|
|
125
131
|
}
|
|
126
132
|
/**
|
|
127
133
|
* True for `<Pressable disabled>` / `<TouchableOpacity disabled={SOME_CONST}>`. Must fold: `disabled`
|
|
@@ -135,10 +141,10 @@ function namedAttr(openingElement, name) {
|
|
|
135
141
|
* @returns Whether it is disabled.
|
|
136
142
|
*/
|
|
137
143
|
function pressableIsDisabled(element, tag, scope) {
|
|
138
|
-
const
|
|
139
|
-
if (!
|
|
144
|
+
const attribute = namedAttr(element.openingElement, 'disabled');
|
|
145
|
+
if (!attribute)
|
|
140
146
|
return false;
|
|
141
|
-
return !!evalStaticOrThrow(attrExpr(
|
|
147
|
+
return !!evalStaticOrThrow(attrExpr(attribute), scope, `AOT: <${tag} disabled> must be a compile-time constant`, 'a disabled one still renders — it lowers to a plain <Pressable>, children, layout ' +
|
|
142
148
|
'and style intact — but its press handlers are not emitted at all, so the ' +
|
|
143
149
|
'AOT has to know at build time. To gate on state, leave it enabled and return early ' +
|
|
144
150
|
'in the handler (`onPress={() => { if (!ready) return; … }}`).');
|
|
@@ -154,26 +160,26 @@ function pressableIsDisabled(element, tag, scope) {
|
|
|
154
160
|
*/
|
|
155
161
|
function lowerTextTruncation(openingElement, scope) {
|
|
156
162
|
const assigns = [];
|
|
157
|
-
const
|
|
158
|
-
if (
|
|
159
|
-
const lines = foldAttr(
|
|
163
|
+
const linesAttribute = namedAttr(openingElement, 'numberOfLines');
|
|
164
|
+
if (linesAttribute) {
|
|
165
|
+
const lines = foldAttr(linesAttribute, scope, 'AOT: <Text numberOfLines> must be a compile-time constant');
|
|
160
166
|
if (typeof lines !== 'number' ||
|
|
161
167
|
!Number.isInteger(lines) ||
|
|
162
168
|
lines < 0 ||
|
|
163
|
-
lines >
|
|
164
|
-
throw at(aotError(`AOT: <Text numberOfLines> must be a whole number from 0 to
|
|
169
|
+
lines > MAX_NUMBER_OF_LINES) {
|
|
170
|
+
throw at(aotError(`AOT: <Text numberOfLines> must be a whole number from 0 to ${MAX_NUMBER_OF_LINES} (got ${String(lines)})`, '0 does not truncate; any other value is the most lines the text wraps to.'), linesAttribute);
|
|
165
171
|
}
|
|
166
|
-
assigns.push({ field: 'number_of_lines',
|
|
172
|
+
assigns.push({ field: 'number_of_lines', value: lines });
|
|
167
173
|
}
|
|
168
|
-
const
|
|
169
|
-
if (
|
|
170
|
-
const mode = foldAttr(
|
|
174
|
+
const modeAttribute = namedAttr(openingElement, 'ellipsizeMode');
|
|
175
|
+
if (modeAttribute) {
|
|
176
|
+
const mode = foldAttr(modeAttribute, scope, 'AOT: <Text ellipsizeMode> must be a compile-time constant');
|
|
171
177
|
if (!Object.hasOwn(ELLIPSIZE_MODES, mode)) {
|
|
172
|
-
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'."),
|
|
178
|
+
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'."), modeAttribute);
|
|
173
179
|
}
|
|
174
180
|
assigns.push({
|
|
175
181
|
field: 'ellipsize_mode',
|
|
176
|
-
|
|
182
|
+
value: ELLIPSIZE_MODES[mode],
|
|
177
183
|
});
|
|
178
184
|
}
|
|
179
185
|
return assigns;
|
|
@@ -181,32 +187,32 @@ function lowerTextTruncation(openingElement, scope) {
|
|
|
181
187
|
/**
|
|
182
188
|
* An attribute's value, which must fold; the error is pinned to the attribute.
|
|
183
189
|
*
|
|
184
|
-
* @param
|
|
190
|
+
* @param attribute The attribute.
|
|
185
191
|
* @param scope Compile-time constants in reach.
|
|
186
192
|
* @param message The error when it does not fold.
|
|
187
193
|
*
|
|
188
194
|
* @returns The folded value.
|
|
189
195
|
*/
|
|
190
|
-
function foldAttr(
|
|
196
|
+
function foldAttr(attribute, scope, message) {
|
|
191
197
|
try {
|
|
192
|
-
return evalStaticOrThrow(attrExpr(
|
|
198
|
+
return evalStaticOrThrow(attrExpr(attribute), scope, message);
|
|
193
199
|
}
|
|
194
200
|
catch (error) {
|
|
195
|
-
throw at(error,
|
|
201
|
+
throw at(error, attribute);
|
|
196
202
|
}
|
|
197
203
|
}
|
|
198
204
|
/**
|
|
199
205
|
* A folded `activeOpacity` as a number in 0..1. Everything else is an error HERE, where the source line
|
|
200
|
-
* is still in reach: it would otherwise reach
|
|
201
|
-
*
|
|
202
|
-
* `activeOpacity` is `true`, so
|
|
206
|
+
* is still in reach: it would otherwise reach floatConstant and land in the package as a value that
|
|
207
|
+
* is not a number, or quietly means something the app never asked for — a bare
|
|
208
|
+
* `activeOpacity` is `true`, so 1, which is not dim at all; `{false}` is an invisible one.
|
|
203
209
|
*
|
|
204
210
|
* @param value The folded value.
|
|
205
|
-
* @param
|
|
211
|
+
* @param attribute The attribute, for the error's location.
|
|
206
212
|
*
|
|
207
213
|
* @returns The opacity.
|
|
208
214
|
*/
|
|
209
|
-
function opacityLiteralOrThrow(value,
|
|
215
|
+
function opacityLiteralOrThrow(value, attribute) {
|
|
210
216
|
if (typeof value !== 'number' ||
|
|
211
217
|
!Number.isFinite(value) ||
|
|
212
218
|
value < 0 ||
|
|
@@ -216,8 +222,8 @@ function opacityLiteralOrThrow(value, attr) {
|
|
|
216
222
|
? String(value)
|
|
217
223
|
: (JSON.stringify(value) ?? String(value));
|
|
218
224
|
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.');
|
|
219
|
-
if (
|
|
220
|
-
error.aotLoc =
|
|
225
|
+
if (attribute.loc) {
|
|
226
|
+
error.aotLoc = attribute.loc.start;
|
|
221
227
|
}
|
|
222
228
|
throw error;
|
|
223
229
|
}
|
|
@@ -233,43 +239,45 @@ function opacityLiteralOrThrow(value, attr) {
|
|
|
233
239
|
* @param element The <TouchableOpacity>.
|
|
234
240
|
* @param nodeId The node's variable.
|
|
235
241
|
* @param staticAssigns The style's constant writes.
|
|
236
|
-
* @param
|
|
242
|
+
* @param dynamicAssigns The style's state-driven writes.
|
|
237
243
|
* @param binds The style's animated-value binds.
|
|
238
|
-
* @param
|
|
244
|
+
* @param output Everything emitted so far.
|
|
239
245
|
* @param scope Compile-time constants in reach.
|
|
240
246
|
*
|
|
241
247
|
* @returns The opacity bind, and the fade to run on onPressIn and the one to run on onPressOut.
|
|
242
248
|
*/
|
|
243
|
-
function touchablePressFades(element, nodeId, staticAssigns,
|
|
249
|
+
function touchablePressFades(element, nodeId, staticAssigns, dynamicAssigns, binds, output, scope) {
|
|
244
250
|
// The press feedback owns the node's opacity, so the style may not animate it or drive it from state.
|
|
245
251
|
const hasAnimatedOpacity = binds.some(bind => bind.prop === 'ER_PROP_OPACITY');
|
|
246
252
|
if (hasAnimatedOpacity ||
|
|
247
|
-
|
|
253
|
+
dynamicAssigns.some(assign => assign.field === 'opacity')) {
|
|
248
254
|
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 " +
|
|
249
255
|
'it, it races it. To animate opacity yourself, use <Pressable>: the dim is just ' +
|
|
250
256
|
'onPressIn/onPressOut driving an Animated.Value (see TouchableOpacity.js for the ' +
|
|
251
257
|
'whole of it).');
|
|
252
258
|
}
|
|
253
|
-
// lowerStyle scaled static opacity to 0–255 (the resting value);
|
|
259
|
+
// lowerStyle scaled static opacity to 0–255 (the resting value); a NaN one is refused later.
|
|
254
260
|
const opacityAssign = staticAssigns.find(assign => assign.field === 'opacity');
|
|
255
|
-
const restingRaw = opacityAssign
|
|
261
|
+
const restingRaw = opacityAssign
|
|
262
|
+
? Number(opacityAssign.value) / OPACITY_OPAQUE
|
|
263
|
+
: 1;
|
|
256
264
|
const restingOpacity = Number.isFinite(restingRaw) ? restingRaw : 1;
|
|
257
265
|
// The dim target: a folded activeOpacity, or RN's default.
|
|
258
|
-
const
|
|
259
|
-
const activeOpacity =
|
|
260
|
-
? opacityLiteralOrThrow(evalStaticOrThrow(attrExpr(
|
|
266
|
+
const activeAttribute = namedAttr(element.openingElement, 'activeOpacity');
|
|
267
|
+
const activeOpacity = activeAttribute
|
|
268
|
+
? opacityLiteralOrThrow(evalStaticOrThrow(attrExpr(activeAttribute), scope, 'AOT: <TouchableOpacity activeOpacity> must be a compile-time constant', 'the dim target is baked into the handler, so it cannot come from state.'), activeAttribute)
|
|
261
269
|
: TOUCHABLE_ACTIVE_OPACITY;
|
|
262
270
|
// Bind a fresh animated value, starting at rest, to the node's opacity.
|
|
263
|
-
const
|
|
271
|
+
const slotName = `anim_press_${nodeId}`;
|
|
264
272
|
checkNumber(restingOpacity);
|
|
265
|
-
|
|
273
|
+
output.childAnimations.push({ slotName, initial: restingOpacity });
|
|
266
274
|
// Each fade is one timing animation of that value.
|
|
267
275
|
return {
|
|
268
|
-
bind: {
|
|
276
|
+
bind: { slotName, prop: 'ER_PROP_OPACITY' },
|
|
269
277
|
fades: {
|
|
270
|
-
onPressIn: { value:
|
|
278
|
+
onPressIn: { value: slotName, opacity: activeOpacity, durationMs: 0 },
|
|
271
279
|
onPressOut: {
|
|
272
|
-
value:
|
|
280
|
+
value: slotName,
|
|
273
281
|
opacity: restingOpacity,
|
|
274
282
|
durationMs: TOUCHABLE_RESTORE_MS,
|
|
275
283
|
},
|
|
@@ -297,7 +305,7 @@ function applyShow(node, show) {
|
|
|
297
305
|
value: {
|
|
298
306
|
kind: 'whileShown',
|
|
299
307
|
show,
|
|
300
|
-
value: { kind: 'constant',
|
|
308
|
+
value: { kind: 'constant', value: visible.value },
|
|
301
309
|
},
|
|
302
310
|
});
|
|
303
311
|
return;
|
|
@@ -309,53 +317,55 @@ function applyShow(node, show) {
|
|
|
309
317
|
* Lowers an event prop's function to a handler: a useCallback compiles once per instance and is shared, a
|
|
310
318
|
* callback prop compiles in its caller's environment, and an inline function compiles where it is.
|
|
311
319
|
*
|
|
312
|
-
* @param
|
|
313
|
-
* @param
|
|
314
|
-
* @param
|
|
320
|
+
* @param attribute The event prop.
|
|
321
|
+
* @param output Everything emitted so far.
|
|
322
|
+
* @param environment The expression environment.
|
|
315
323
|
* @param state The component's state table.
|
|
316
324
|
*
|
|
317
325
|
* @returns The handler's name.
|
|
318
326
|
*/
|
|
319
|
-
export function lowerEventHandler(
|
|
320
|
-
const
|
|
321
|
-
const layout =
|
|
322
|
-
if (
|
|
327
|
+
export function lowerEventHandler(attribute, output, environment, state) {
|
|
328
|
+
const handlerExpression = attrExpr(attribute);
|
|
329
|
+
const layout = attribute.name.name === 'onLayout';
|
|
330
|
+
if (handlerExpression.type === 'Identifier' &&
|
|
331
|
+
environment.callbacks?.has(handlerExpression.name)) {
|
|
323
332
|
// A useCallback compiles to one shared handler, namespaced by cbPrefix so each child instance gets its own.
|
|
324
333
|
// Its onLayout use gets a handler of its own, since only that event carries the layout rect. The key's
|
|
325
|
-
// `layout:` and the name's `
|
|
326
|
-
const callbackKey = `${
|
|
334
|
+
// `layout:` and the name's `layout_callback_` can't come from an identifier, so they never meet another callback.
|
|
335
|
+
const callbackKey = `${environment.callbackPrefix ?? ''}${handlerExpression.name}`;
|
|
327
336
|
const emittedKey = layout ? `layout:${callbackKey}` : callbackKey;
|
|
328
|
-
let handlerName =
|
|
337
|
+
let handlerName = output.emittedCallbacks.get(emittedKey);
|
|
329
338
|
if (!handlerName) {
|
|
330
|
-
handlerName = `${layout ? '
|
|
331
|
-
|
|
332
|
-
|
|
339
|
+
handlerName = `${layout ? 'layout_callback_' : 'callback_'}${callbackKey}`;
|
|
340
|
+
output.emittedCallbacks.set(emittedKey, handlerName);
|
|
341
|
+
output.handlers.push({
|
|
333
342
|
kind: 'callback',
|
|
334
343
|
name: handlerName,
|
|
335
|
-
callback: lowerHandler(
|
|
344
|
+
callback: lowerHandler(environment.callbacks.get(handlerExpression.name), { ...environment, layout }, state, output),
|
|
336
345
|
});
|
|
337
346
|
}
|
|
338
347
|
return handlerName;
|
|
339
348
|
}
|
|
340
|
-
const handlerName = `
|
|
341
|
-
if (
|
|
349
|
+
const handlerName = `handler_${output.handlers.length}`;
|
|
350
|
+
if (handlerExpression.type === 'Identifier' &&
|
|
351
|
+
environment.functionProps?.has(handlerExpression.name)) {
|
|
342
352
|
// Inline a callback prop's function, compiled in the caller's env/state so its setters and locals resolve.
|
|
343
|
-
const callbackProp =
|
|
344
|
-
|
|
353
|
+
const callbackProp = environment.functionProps.get(handlerExpression.name);
|
|
354
|
+
output.handlers.push({
|
|
345
355
|
kind: 'callback',
|
|
346
356
|
name: handlerName,
|
|
347
|
-
callback: lowerHandler(callbackProp.node, { ...callbackProp.
|
|
357
|
+
callback: lowerHandler(callbackProp.node, { ...callbackProp.environment, layout }, callbackProp.state, output),
|
|
348
358
|
});
|
|
349
359
|
}
|
|
350
|
-
else if (isFn(
|
|
351
|
-
|
|
360
|
+
else if (isFn(handlerExpression)) {
|
|
361
|
+
output.handlers.push({
|
|
352
362
|
kind: 'callback',
|
|
353
363
|
name: handlerName,
|
|
354
|
-
callback: lowerHandler(
|
|
364
|
+
callback: lowerHandler(handlerExpression, { ...environment, layout }, state, output),
|
|
355
365
|
});
|
|
356
366
|
}
|
|
357
367
|
else {
|
|
358
|
-
throw aotError(`AOT: ${
|
|
368
|
+
throw aotError(`AOT: ${attribute.name.name} must be an inline function, a useCallback, or a callback prop`, `pass an inline arrow (onPress={() => setX(…)}), a useCallback identifier, or a ` +
|
|
359
369
|
`function prop received by this component.`);
|
|
360
370
|
}
|
|
361
371
|
return handlerName;
|
|
@@ -368,14 +378,14 @@ export function lowerEventHandler(attr, out, env, state) {
|
|
|
368
378
|
*
|
|
369
379
|
* @param element The element.
|
|
370
380
|
* @param scope Compile-time constants in reach.
|
|
371
|
-
* @param
|
|
372
|
-
* @param
|
|
381
|
+
* @param output Everything emitted so far.
|
|
382
|
+
* @param environment The expression environment.
|
|
373
383
|
* @param state The component's state table.
|
|
374
|
-
* @param
|
|
384
|
+
* @param options How the element is placed.
|
|
375
385
|
*
|
|
376
386
|
* @returns What the element lowered to.
|
|
377
387
|
*/
|
|
378
|
-
function emitNodeImpl(element, scope,
|
|
388
|
+
function emitNodeImpl(element, scope, output, environment, state, options = {}) {
|
|
379
389
|
// Refuse any prop the element does not declare, whatever its value, then drop the no-ops and the props that
|
|
380
390
|
// fold to undefined before dispatch.
|
|
381
391
|
const tag = resolveTag(element.openingElement);
|
|
@@ -387,38 +397,38 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
387
397
|
}
|
|
388
398
|
element = withoutPlatformNoOps(spec, element);
|
|
389
399
|
}
|
|
390
|
-
element = omitUndefinedAttrs(element, scope,
|
|
400
|
+
element = omitUndefinedAttrs(element, scope, environment);
|
|
391
401
|
const typed = (node) => {
|
|
392
|
-
if (
|
|
393
|
-
applyShow(node,
|
|
402
|
+
if (options.show) {
|
|
403
|
+
applyShow(node, options.show);
|
|
394
404
|
}
|
|
395
405
|
return { kind: 'node', id: node.id, node };
|
|
396
406
|
};
|
|
397
407
|
if (tag === 'Svg') {
|
|
398
|
-
return typed(emitSvg(element, scope,
|
|
408
|
+
return typed(emitSvg(element, scope, output, environment, state, options));
|
|
399
409
|
}
|
|
400
410
|
if (tag === 'Switch') {
|
|
401
|
-
return typed(emitSwitch(element, scope,
|
|
411
|
+
return typed(emitSwitch(element, scope, output, environment, state));
|
|
402
412
|
}
|
|
403
413
|
if (tag === 'Dial')
|
|
404
|
-
return typed(emitDial(element, scope,
|
|
414
|
+
return typed(emitDial(element, scope, output, environment, state));
|
|
405
415
|
if (tag === 'TextInput') {
|
|
406
|
-
return typed(emitTextInput(element, scope,
|
|
416
|
+
return typed(emitTextInput(element, scope, output, environment, state));
|
|
407
417
|
}
|
|
408
418
|
if (tag === 'ActivityIndicator') {
|
|
409
|
-
return typed(emitActivityIndicator(element, scope,
|
|
419
|
+
return typed(emitActivityIndicator(element, scope, output, environment));
|
|
410
420
|
}
|
|
411
421
|
if (tag === 'Modal') {
|
|
412
|
-
return typed(emitModal(element, scope,
|
|
422
|
+
return typed(emitModal(element, scope, output, environment, state));
|
|
413
423
|
}
|
|
414
424
|
if (tag === 'FlatList') {
|
|
415
|
-
return emitFlatList(element, scope,
|
|
425
|
+
return emitFlatList(element, scope, output, environment, state, options);
|
|
416
426
|
}
|
|
417
427
|
// Anything else must be a host node or a function component defined in this file.
|
|
418
428
|
const nodeType = NODE_TYPES[tag];
|
|
419
429
|
if (!nodeType) {
|
|
420
|
-
if (
|
|
421
|
-
return emitComponent(element, scope,
|
|
430
|
+
if (output.components.has(tag)) {
|
|
431
|
+
return emitComponent(element, scope, output, environment, state, options);
|
|
422
432
|
}
|
|
423
433
|
// A package export with no lowering yet gets its own error; "unknown element" would read like a typo.
|
|
424
434
|
const replacement = FLOW_A_ONLY_COMPONENTS[tag];
|
|
@@ -440,7 +450,7 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
440
450
|
continue;
|
|
441
451
|
let panResponder;
|
|
442
452
|
try {
|
|
443
|
-
panResponder = panSpreadTarget(spread.argument,
|
|
453
|
+
panResponder = panSpreadTarget(spread.argument, environment);
|
|
444
454
|
}
|
|
445
455
|
catch (error) {
|
|
446
456
|
if (spread.loc && !error.aotLoc) {
|
|
@@ -462,62 +472,68 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
462
472
|
throw spreadError;
|
|
463
473
|
}
|
|
464
474
|
// Allocate the node and collect its style, text content, and image attributes.
|
|
465
|
-
const nodeId = `n${
|
|
466
|
-
const { staticAssigns,
|
|
475
|
+
const nodeId = `n${output.allocateNodeId()}`;
|
|
476
|
+
const { staticAssigns, dynamicAssigns, binds } = collectStyleAssigns(element.openingElement, scope, environment, spec);
|
|
467
477
|
if (tag === 'Text') {
|
|
468
478
|
staticAssigns.push(...lowerTextTruncation(element.openingElement, scope));
|
|
469
479
|
}
|
|
470
480
|
// A <Text> with a nested <Text> becomes inline SPANS; otherwise a single (possibly dynamic) string.
|
|
471
|
-
const spans = tag === 'Text'
|
|
481
|
+
const spans = tag === 'Text'
|
|
482
|
+
? collectTextSpans(element.children, scope, environment)
|
|
483
|
+
: null;
|
|
472
484
|
const text = tag === 'Text' && !spans
|
|
473
|
-
? lowerNodeText(element.children, scope,
|
|
485
|
+
? lowerNodeText(element.children, scope, environment)
|
|
474
486
|
: null;
|
|
475
487
|
if (text) {
|
|
476
488
|
checkEngineString(constantText(text), 'ER_TEXT_MAX', 'a <Text>');
|
|
477
489
|
}
|
|
478
490
|
// An <Image>'s resize/tint are static, so fold them into staticAssigns for both the static and dynamic paths.
|
|
479
|
-
const image = tag === 'Image' ? resolveImageAttrs(element,
|
|
491
|
+
const image = tag === 'Image' ? resolveImageAttrs(element, environment, output) : null;
|
|
480
492
|
checkEngineString(image?.imageName ?? null, 'ER_IMAGE_NAME_MAX', `image name "${image?.imageName}"`);
|
|
481
493
|
if (image?.resizeMode) {
|
|
482
|
-
staticAssigns.push({ field: 'resize_mode',
|
|
494
|
+
staticAssigns.push({ field: 'resize_mode', value: image.resizeMode });
|
|
483
495
|
}
|
|
484
496
|
if (image?.tintColor) {
|
|
485
|
-
staticAssigns.push({ field: 'tint_color',
|
|
497
|
+
staticAssigns.push({ field: 'tint_color', value: image.tintColor });
|
|
486
498
|
}
|
|
487
499
|
// `visible` is `display` as in Flow A (style `display` wins); not in collectStyleAssigns, which <Modal> shares.
|
|
488
|
-
const
|
|
489
|
-
|
|
500
|
+
const visibleAttribute = element.openingElement.attributes.find((attribute) => attribute.type === 'JSXAttribute' &&
|
|
501
|
+
attribute.name &&
|
|
502
|
+
attribute.name.name === 'visible');
|
|
503
|
+
if (visibleAttribute &&
|
|
490
504
|
!staticAssigns.some(assign => assign.field === 'display') &&
|
|
491
|
-
!
|
|
492
|
-
const
|
|
493
|
-
if (
|
|
494
|
-
staticAssigns.push({ field: 'display',
|
|
505
|
+
!dynamicAssigns.some(assign => assign.field === 'display')) {
|
|
506
|
+
const expression = visibleAttribute.value == null ? null : attrExpr(visibleAttribute); // bare `visible` === true
|
|
507
|
+
if (expression == null) {
|
|
508
|
+
staticAssigns.push({ field: 'display', value: 'ER_DISPLAY_FLEX' });
|
|
495
509
|
}
|
|
496
510
|
else {
|
|
497
511
|
try {
|
|
498
512
|
staticAssigns.push({
|
|
499
513
|
field: 'display',
|
|
500
|
-
|
|
514
|
+
value: evalStatic(expression, scope)
|
|
515
|
+
? 'ER_DISPLAY_FLEX'
|
|
516
|
+
: 'ER_DISPLAY_NONE',
|
|
501
517
|
});
|
|
502
518
|
}
|
|
503
519
|
catch {
|
|
504
|
-
|
|
520
|
+
dynamicAssigns.push({
|
|
505
521
|
field: 'display',
|
|
506
|
-
value: { kind: 'visible', value: lowerExpr(
|
|
522
|
+
value: { kind: 'visible', value: lowerExpr(expression, environment) },
|
|
507
523
|
});
|
|
508
524
|
}
|
|
509
525
|
}
|
|
510
526
|
}
|
|
511
527
|
// delayLongPress must fold since it is baked into props; 0 becomes 1 ms because 0 means "default" to the engine.
|
|
512
|
-
const
|
|
513
|
-
if (
|
|
528
|
+
const delayAttribute = namedAttr(element.openingElement, 'delayLongPress');
|
|
529
|
+
if (delayAttribute) {
|
|
514
530
|
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}.');
|
|
515
|
-
if (
|
|
531
|
+
if (delayAttribute.value == null) {
|
|
516
532
|
throw delayError;
|
|
517
533
|
}
|
|
518
534
|
let delayMs;
|
|
519
535
|
try {
|
|
520
|
-
delayMs = Number(evalStatic(attrExpr(
|
|
536
|
+
delayMs = Number(evalStatic(attrExpr(delayAttribute), scope));
|
|
521
537
|
}
|
|
522
538
|
catch {
|
|
523
539
|
throw delayError;
|
|
@@ -527,19 +543,19 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
527
543
|
}
|
|
528
544
|
staticAssigns.push({
|
|
529
545
|
field: 'long_press_ms',
|
|
530
|
-
|
|
546
|
+
value: Math.min(MAX_LONG_PRESS_MS, Math.max(1, Math.round(delayMs))),
|
|
531
547
|
});
|
|
532
548
|
}
|
|
533
|
-
// A state-driven conditional flips `display` in
|
|
534
|
-
if (
|
|
535
|
-
|
|
549
|
+
// A state-driven conditional flips `display` in the update; pushed last to beat the element's visible/display.
|
|
550
|
+
if (options.show) {
|
|
551
|
+
dynamicAssigns.push({
|
|
536
552
|
field: 'display',
|
|
537
|
-
value: { kind: 'shown', show:
|
|
553
|
+
value: { kind: 'shown', show: options.show },
|
|
538
554
|
});
|
|
539
555
|
}
|
|
540
556
|
// <TouchableOpacity>'s own opacity binding, on top of any the style asked for.
|
|
541
557
|
const press = tag === 'TouchableOpacity' && !isPressDisabled
|
|
542
|
-
? touchablePressFades(element, nodeId, staticAssigns,
|
|
558
|
+
? touchablePressFades(element, nodeId, staticAssigns, dynamicAssigns, binds, output, scope)
|
|
543
559
|
: null;
|
|
544
560
|
// A host node wires its binds (the press feedback's last), then its refs, events and PanResponders.
|
|
545
561
|
const wiring = binds.map((bind, index) => ({
|
|
@@ -550,18 +566,19 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
550
566
|
if (press) {
|
|
551
567
|
wiring.push({ kind: 'bind', bind: press.bind, index: binds.length });
|
|
552
568
|
}
|
|
553
|
-
for (const slot of lowerRefBinds(element.openingElement,
|
|
569
|
+
for (const slot of lowerRefBinds(element.openingElement, environment)) {
|
|
554
570
|
wiring.push({ kind: 'ref', slot });
|
|
555
571
|
}
|
|
556
572
|
const node = {
|
|
557
573
|
id: nodeId,
|
|
574
|
+
location: currentLoc(),
|
|
558
575
|
type: nodeType,
|
|
559
576
|
constantProps: staticAssigns,
|
|
560
|
-
stateProps:
|
|
577
|
+
stateProps: dynamicAssigns,
|
|
561
578
|
placeholder: null,
|
|
562
579
|
text,
|
|
563
|
-
image: image?.
|
|
564
|
-
? { kind: 'value', value: image.
|
|
580
|
+
image: image?.imageNameDynamic != null
|
|
581
|
+
? { kind: 'value', value: image.imageNameDynamic }
|
|
565
582
|
: image?.imageName != null
|
|
566
583
|
? { kind: 'static', name: image.imageName }
|
|
567
584
|
: null,
|
|
@@ -571,29 +588,29 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
571
588
|
};
|
|
572
589
|
// Compile each event prop into a handler; a disabled one drops its press events.
|
|
573
590
|
const fadesWired = new Set();
|
|
574
|
-
for (const
|
|
575
|
-
if (
|
|
591
|
+
for (const attribute of element.openingElement.attributes) {
|
|
592
|
+
if (attribute.type !== 'JSXAttribute')
|
|
576
593
|
continue;
|
|
577
|
-
const eventType = EVENT_TYPES[
|
|
594
|
+
const eventType = EVENT_TYPES[attribute.name.name];
|
|
578
595
|
if (!eventType)
|
|
579
596
|
continue;
|
|
580
597
|
if (isPressDisabled &&
|
|
581
|
-
PRESS_EVENTS.has(
|
|
598
|
+
PRESS_EVENTS.has(attribute.name.name)) {
|
|
582
599
|
continue;
|
|
583
600
|
}
|
|
584
|
-
let handlerName = lowerEventHandler(
|
|
601
|
+
let handlerName = lowerEventHandler(attribute, output, environment, state);
|
|
585
602
|
// Wrap the app's press-in/out with the fade; prepending would dim every other element sharing a useCallback.
|
|
586
|
-
const pressFade = press?.fades[
|
|
603
|
+
const pressFade = press?.fades[attribute.name.name];
|
|
587
604
|
if (pressFade) {
|
|
588
|
-
const wrapperName = `
|
|
589
|
-
|
|
605
|
+
const wrapperName = `handler_${output.handlers.length}`;
|
|
606
|
+
output.handlers.push({
|
|
590
607
|
kind: 'pressFade',
|
|
591
608
|
name: wrapperName,
|
|
592
609
|
fade: pressFade,
|
|
593
610
|
then: handlerName,
|
|
594
611
|
});
|
|
595
612
|
handlerName = wrapperName;
|
|
596
|
-
fadesWired.add(
|
|
613
|
+
fadesWired.add(attribute.name.name);
|
|
597
614
|
}
|
|
598
615
|
wiring.push({ kind: 'event', event: eventType, handler: handlerName });
|
|
599
616
|
}
|
|
@@ -602,8 +619,8 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
602
619
|
for (const eventName of ['onPressIn', 'onPressOut']) {
|
|
603
620
|
if (fadesWired.has(eventName))
|
|
604
621
|
continue;
|
|
605
|
-
const fadeHandlerName = `
|
|
606
|
-
|
|
622
|
+
const fadeHandlerName = `handler_${output.handlers.length}`;
|
|
623
|
+
output.handlers.push({
|
|
607
624
|
kind: 'pressFade',
|
|
608
625
|
name: fadeHandlerName,
|
|
609
626
|
fade: press.fades[eventName],
|
|
@@ -620,13 +637,13 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
620
637
|
for (const panResponder of panSpreads) {
|
|
621
638
|
wiring.push({
|
|
622
639
|
kind: 'responder',
|
|
623
|
-
pan: emitPanResponder(panResponder,
|
|
640
|
+
pan: emitPanResponder(panResponder, output, environment, state),
|
|
624
641
|
});
|
|
625
642
|
}
|
|
626
643
|
checkInterpolations(node);
|
|
627
644
|
// A <Text>'s children are its text, already consumed above.
|
|
628
645
|
if (tag !== 'Text') {
|
|
629
|
-
node.children = emitChildren(element.children, scope,
|
|
646
|
+
node.children = emitChildren(element.children, scope, output, environment, state);
|
|
630
647
|
}
|
|
631
648
|
return { kind: 'node', id: nodeId, node };
|
|
632
649
|
}
|
|
@@ -640,24 +657,24 @@ function emitNodeImpl(element, scope, out, env, state, opts = {}) {
|
|
|
640
657
|
*
|
|
641
658
|
* @param element The element.
|
|
642
659
|
* @param scope Compile-time constants in reach.
|
|
643
|
-
* @param
|
|
660
|
+
* @param environment The expression environment.
|
|
644
661
|
*
|
|
645
662
|
* @returns The element, or a copy of it without those attributes.
|
|
646
663
|
*/
|
|
647
|
-
function omitUndefinedAttrs(element, scope,
|
|
648
|
-
const
|
|
664
|
+
function omitUndefinedAttrs(element, scope, environment) {
|
|
665
|
+
const attributes = element.openingElement.attributes;
|
|
649
666
|
// Built lazily: the runtime-aware scope plus the global `undefined`, so `SHOW ? true : undefined` folds too.
|
|
650
667
|
let foldingScope = null;
|
|
651
|
-
const
|
|
652
|
-
if (
|
|
653
|
-
|
|
668
|
+
const keptAttributes = attributes.filter(attribute => {
|
|
669
|
+
if (attribute.type !== 'JSXAttribute' ||
|
|
670
|
+
attribute.value?.type !== 'JSXExpressionContainer') {
|
|
654
671
|
return true;
|
|
655
672
|
}
|
|
656
|
-
foldingScope ??= Object.assign(Object.create(foldScope(
|
|
673
|
+
foldingScope ??= Object.assign(Object.create(foldScope(environment, scope)), {
|
|
657
674
|
undefined,
|
|
658
675
|
});
|
|
659
676
|
try {
|
|
660
|
-
const value = evalStatic(
|
|
677
|
+
const value = evalStatic(attribute.value.expression, foldingScope, 'boot');
|
|
661
678
|
if (buildValueOf(value) !== undefined)
|
|
662
679
|
return true;
|
|
663
680
|
foldBoot(value); // undefined on this panel only: omitting it assumes the panel
|
|
@@ -667,26 +684,26 @@ function omitUndefinedAttrs(element, scope, env) {
|
|
|
667
684
|
return true; // not a compile-time value — its reader decides
|
|
668
685
|
}
|
|
669
686
|
});
|
|
670
|
-
return
|
|
687
|
+
return keptAttributes.length === attributes.length
|
|
671
688
|
? element
|
|
672
689
|
: {
|
|
673
690
|
...element,
|
|
674
|
-
openingElement: { ...element.openingElement, attributes:
|
|
691
|
+
openingElement: { ...element.openingElement, attributes: keptAttributes },
|
|
675
692
|
};
|
|
676
693
|
}
|
|
677
694
|
/**
|
|
678
|
-
* Refuses an interpolation table holding a number
|
|
695
|
+
* Refuses an interpolation table holding a number that isn't finite, at the element that binds it rather than when
|
|
679
696
|
* its build lines are written.
|
|
680
697
|
*
|
|
681
698
|
* @param node The node.
|
|
682
699
|
*/
|
|
683
700
|
export function checkInterpolations(node) {
|
|
684
701
|
for (const wire of node.wiring) {
|
|
685
|
-
if (wire.kind !== 'bind' || !wire.bind.
|
|
702
|
+
if (wire.kind !== 'bind' || !wire.bind.interpolation)
|
|
686
703
|
continue;
|
|
687
704
|
for (const point of [
|
|
688
|
-
...wire.bind.
|
|
689
|
-
...wire.bind.
|
|
705
|
+
...wire.bind.interpolation.input,
|
|
706
|
+
...wire.bind.interpolation.output,
|
|
690
707
|
]) {
|
|
691
708
|
checkNumber(point);
|
|
692
709
|
}
|