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,1001 @@
|
|
|
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 { check, checkNumber } from './check.mjs';
|
|
20
|
+
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
import { evalStaticOrThrow, evalStatic, foldScope, evalStaticOr, } from './static-eval.mjs';
|
|
22
|
+
import { INT_MAX, lowerExpr } from './expressions.mjs';
|
|
23
|
+
import { NO_WIDE, isFn, wideInitial } from './collect.mjs';
|
|
24
|
+
import { lowerStmts } from './handlers.mjs';
|
|
25
|
+
import { isPanCreate } from './pan-responder.mjs';
|
|
26
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
27
|
+
- Constants
|
|
28
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
29
|
+
/** Style key → the ERAnimProp(s) an animated value binds to. */
|
|
30
|
+
export const ANIM_STYLE_PROPS = {
|
|
31
|
+
opacity: ['ER_PROP_OPACITY'],
|
|
32
|
+
backgroundColor: ['ER_PROP_BACKGROUND_COLOR'],
|
|
33
|
+
color: ['ER_PROP_COLOR'],
|
|
34
|
+
};
|
|
35
|
+
/** Transform key → the ERAnimProp(s) an animated value binds to. */
|
|
36
|
+
export const ANIM_TRANSFORM_PROPS = {
|
|
37
|
+
scale: ['ER_PROP_SCALE_X', 'ER_PROP_SCALE_Y'],
|
|
38
|
+
scaleX: ['ER_PROP_SCALE_X'],
|
|
39
|
+
scaleY: ['ER_PROP_SCALE_Y'],
|
|
40
|
+
translateX: ['ER_PROP_TRANSLATE_X'],
|
|
41
|
+
translateY: ['ER_PROP_TRANSLATE_Y'],
|
|
42
|
+
rotate: ['ER_PROP_ROTATE_Z'],
|
|
43
|
+
rotateZ: ['ER_PROP_ROTATE_Z'],
|
|
44
|
+
};
|
|
45
|
+
const ANIM_KINDS = new Set([
|
|
46
|
+
'timing',
|
|
47
|
+
'spring',
|
|
48
|
+
'decay',
|
|
49
|
+
'sequence',
|
|
50
|
+
'parallel',
|
|
51
|
+
'stagger',
|
|
52
|
+
'delay',
|
|
53
|
+
'loop',
|
|
54
|
+
]);
|
|
55
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
56
|
+
- Implementation
|
|
57
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
58
|
+
/**
|
|
59
|
+
* Collects `const x = useAnimatedValue(initial)` → Map (name → {cVar, initial}). `prefix` namespaces the
|
|
60
|
+
* C var (`s_av_<prefix><name>`) so each inlined child instance gets its own engine value handle.
|
|
61
|
+
*
|
|
62
|
+
* @param fnBody The component's body.
|
|
63
|
+
* @param scope Compile-time constants the initial values fold against.
|
|
64
|
+
* @param prefix The C name prefix: none for the App, `c<n>_` for an inlined instance.
|
|
65
|
+
*
|
|
66
|
+
* @returns The animated values, by name.
|
|
67
|
+
*/
|
|
68
|
+
export function collectAnims(fnBody, scope, prefix = '') {
|
|
69
|
+
// Collect animated value records by their JS binding name.
|
|
70
|
+
const anims = new Map();
|
|
71
|
+
// Only block-bodied functions can contain hook declarations to collect.
|
|
72
|
+
if (fnBody.type !== 'BlockStatement')
|
|
73
|
+
return anims;
|
|
74
|
+
// Scan top-level declarations in the function body for useAnimatedValue hooks to allocate animated slots.
|
|
75
|
+
for (const stmt of fnBody.body) {
|
|
76
|
+
// Only variable declarations can introduce bindings we need to inspect.
|
|
77
|
+
if (stmt.type !== 'VariableDeclaration')
|
|
78
|
+
continue;
|
|
79
|
+
// Collect useAnimatedValue declarations and register their engine-side animated slots.
|
|
80
|
+
for (const decl of stmt.declarations) {
|
|
81
|
+
const init = decl.init;
|
|
82
|
+
if (init?.type === 'CallExpression' &&
|
|
83
|
+
init.callee.name === 'useAnimatedValue' &&
|
|
84
|
+
decl.id.type === 'Identifier') {
|
|
85
|
+
const initVal = init.arguments[0]
|
|
86
|
+
? evalStaticOrThrow(init.arguments[0], scope, `AOT: the initial value of useAnimatedValue "${decl.id.name}" must be a compile-time constant`, 'give it a finite starting number: useAnimatedValue(0).')
|
|
87
|
+
: 0;
|
|
88
|
+
// An animated value is a float slot; null or a non-finite number would reach C as `NaNf`.
|
|
89
|
+
// Refuse here, at the declaration, with its location.
|
|
90
|
+
if (typeof initVal !== 'number' || !Number.isFinite(initVal)) {
|
|
91
|
+
const e = aotError(`AOT: the initial value of useAnimatedValue "${decl.id.name}" is ${initVal === undefined ? 'undefined' : String(initVal)}`, 'give it a finite starting number: useAnimatedValue(0).');
|
|
92
|
+
if (init.arguments[0]?.loc) {
|
|
93
|
+
e.aotLoc = init.arguments[0].loc.start;
|
|
94
|
+
}
|
|
95
|
+
throw e;
|
|
96
|
+
}
|
|
97
|
+
checkNumber(initVal);
|
|
98
|
+
anims.set(decl.id.name, {
|
|
99
|
+
cVar: `s_av_${prefix}${decl.id.name}`,
|
|
100
|
+
initial: initVal,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return anims;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Collects `const r = useRef(initial)` refs → Map (name → {cVar, cType, initial, kind}). Two kinds:
|
|
109
|
+
* - VALUE ref (numeric initial): a mutable C slot (escape-hatch state that does NOT re-render;
|
|
110
|
+
* `.current` reads/writes).
|
|
111
|
+
* - NODE ref (`useRef()` / `useRef(null)`): holds an `ERNode*`, captured by `ref={r}` on an element and
|
|
112
|
+
* used as the target of imperative calls like updateVector(r, …). kind === 'node'.
|
|
113
|
+
*
|
|
114
|
+
* @param fnBody The component's body.
|
|
115
|
+
* @param scope Compile-time constants the initial values fold against.
|
|
116
|
+
* @param prefix The C name prefix: none for the App, `c<n>_` for an inlined instance.
|
|
117
|
+
* @param wide Slots to declare 64-bit (see compileWidened).
|
|
118
|
+
*
|
|
119
|
+
* @returns The refs, by name.
|
|
120
|
+
*/
|
|
121
|
+
export function collectRefs(fnBody, scope, prefix = '', wide = NO_WIDE) {
|
|
122
|
+
// Collect ref records by their JS binding name.
|
|
123
|
+
const refs = new Map();
|
|
124
|
+
// Only block-bodied functions can contain ref declarations to collect.
|
|
125
|
+
if (fnBody.type !== 'BlockStatement')
|
|
126
|
+
return refs;
|
|
127
|
+
// Collect useRef declarations, splitting null/empty node refs from numeric value refs.
|
|
128
|
+
for (const stmt of fnBody.body) {
|
|
129
|
+
// Only variable declarations can introduce bindings we need to inspect.
|
|
130
|
+
if (stmt.type !== 'VariableDeclaration')
|
|
131
|
+
continue;
|
|
132
|
+
// Find useRef bindings and lower each supported initializer to either a node ref or value ref record.
|
|
133
|
+
for (const decl of stmt.declarations) {
|
|
134
|
+
const init = decl.init;
|
|
135
|
+
if (init?.type === 'CallExpression' &&
|
|
136
|
+
init.callee.name === 'useRef' &&
|
|
137
|
+
decl.id.type === 'Identifier') {
|
|
138
|
+
const arg = init.arguments[0];
|
|
139
|
+
// A PanResponder ref — collectPanResponders owns it
|
|
140
|
+
if (isPanCreate(arg))
|
|
141
|
+
continue;
|
|
142
|
+
// Empty or null useRef values are node refs, initialized to no bound ERNode.
|
|
143
|
+
const cVar = `s_ref_${prefix}${decl.id.name}`;
|
|
144
|
+
if (!arg || arg.type === 'NullLiteral') {
|
|
145
|
+
refs.set(decl.id.name, {
|
|
146
|
+
cVar,
|
|
147
|
+
cType: 'ERNode*',
|
|
148
|
+
kind: 'node',
|
|
149
|
+
used: false,
|
|
150
|
+
});
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
// Value refs must start from a finite compile-time number.
|
|
154
|
+
const value = evalStaticOrThrow(arg, scope, `AOT: useRef initial for "${decl.id.name}" must be a number (value ref) or null/empty (node ref)`, 'a value ref needs a compile-time number: useRef(0).');
|
|
155
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
156
|
+
const error = aotError(`AOT: useRef initial for "${decl.id.name}" must be a number (value ref) or null/empty (node ref)`, `got ${value === undefined ? 'undefined' : String(value)}.`);
|
|
157
|
+
if (arg.loc) {
|
|
158
|
+
error.aotLoc = arg.loc.start;
|
|
159
|
+
}
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
// A ref a timestamp or a number past the int range is written to hold 64 bits (see compileWidened).
|
|
163
|
+
const initialWidth = wideInitial(value);
|
|
164
|
+
const widened = Number.isInteger(value) && initialWidth !== 'float'
|
|
165
|
+
? (wide.get(cVar) ??
|
|
166
|
+
(initialWidth === 'number' ? 'number' : undefined))
|
|
167
|
+
: undefined;
|
|
168
|
+
const cType = !Number.isInteger(value) || initialWidth === 'float'
|
|
169
|
+
? 'float'
|
|
170
|
+
: widened
|
|
171
|
+
? 'i64'
|
|
172
|
+
: 'int';
|
|
173
|
+
if (cType === 'float') {
|
|
174
|
+
checkNumber(value);
|
|
175
|
+
}
|
|
176
|
+
refs.set(decl.id.name, {
|
|
177
|
+
cVar,
|
|
178
|
+
cType,
|
|
179
|
+
initial: value,
|
|
180
|
+
kind: 'value',
|
|
181
|
+
used: false,
|
|
182
|
+
...(widened ? { widened } : {}),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return refs;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The polynomial family of an `Easing.quad` / `Easing.cubic` node, for in/out/inOut composition.
|
|
191
|
+
*
|
|
192
|
+
* @param node The easing expression.
|
|
193
|
+
*
|
|
194
|
+
* @returns 'QUAD' or 'CUBIC', or null for anything else.
|
|
195
|
+
*/
|
|
196
|
+
function easingFamily(node) {
|
|
197
|
+
if (node?.type === 'MemberExpression' &&
|
|
198
|
+
node.object?.name === 'Easing') {
|
|
199
|
+
if (node.property.name === 'quad')
|
|
200
|
+
return 'QUAD';
|
|
201
|
+
if (node.property.name === 'cubic')
|
|
202
|
+
return 'CUBIC';
|
|
203
|
+
}
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Maps an `Easing.*` node → { ease: 'ER_EASE_*', bezier: [x1,y1,x2,y2] | null }. Handles the bare curves
|
|
208
|
+
* (linear/ease/quad/cubic/bounce/elastic), the in/out/inOut wrappers around quad/cubic, and
|
|
209
|
+
* Easing.bezier(x1,y1,x2,y2). No easing → ER_EASE_EASE_IN_OUT (RN's timing default); unknown → same.
|
|
210
|
+
*
|
|
211
|
+
* @param node The `easing:` expression, if there is one.
|
|
212
|
+
* @param env The expression environment for folding bezier control points.
|
|
213
|
+
*
|
|
214
|
+
* @returns The engine easing, and its control points when it is a bezier.
|
|
215
|
+
*/
|
|
216
|
+
function easingInfo(node, env) {
|
|
217
|
+
// Use React Native's timing default whenever no easing is supplied (or the expression is unsupported).
|
|
218
|
+
const FALLBACK = { ease: 'ER_EASE_EASE_IN_OUT', bezier: null };
|
|
219
|
+
if (!node)
|
|
220
|
+
return FALLBACK;
|
|
221
|
+
// Bare member: Easing.linear / Easing.ease / Easing.quad (== quad-in) / ...
|
|
222
|
+
if (node.type === 'MemberExpression' &&
|
|
223
|
+
node.object?.name === 'Easing') {
|
|
224
|
+
const map = {
|
|
225
|
+
linear: 'ER_EASE_LINEAR',
|
|
226
|
+
ease: 'ER_EASE_EASE',
|
|
227
|
+
quad: 'ER_EASE_QUAD_IN',
|
|
228
|
+
cubic: 'ER_EASE_CUBIC_IN',
|
|
229
|
+
bounce: 'ER_EASE_BOUNCE_OUT',
|
|
230
|
+
elastic: 'ER_EASE_ELASTIC_OUT',
|
|
231
|
+
};
|
|
232
|
+
return {
|
|
233
|
+
ease: map[node.property.name] || 'ER_EASE_EASE_IN_OUT',
|
|
234
|
+
bezier: null,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
// Call: Easing.bezier(...), Easing.elastic(n), Easing.in/out/inOut(inner)
|
|
238
|
+
if (node.type === 'CallExpression' &&
|
|
239
|
+
node.callee.type === 'MemberExpression' &&
|
|
240
|
+
node.callee.object?.name === 'Easing') {
|
|
241
|
+
const fn = node.callee.property.name;
|
|
242
|
+
if (fn === 'bezier') {
|
|
243
|
+
const cps = node.arguments
|
|
244
|
+
.slice(0, 4)
|
|
245
|
+
.map(a => Number(evalStaticOr(a, env, 0)));
|
|
246
|
+
return cps.length === 4
|
|
247
|
+
? { ease: 'ER_EASE_BEZIER', bezier: cps }
|
|
248
|
+
: FALLBACK;
|
|
249
|
+
}
|
|
250
|
+
if (fn === 'elastic') {
|
|
251
|
+
return { ease: 'ER_EASE_ELASTIC_OUT', bezier: null };
|
|
252
|
+
}
|
|
253
|
+
if (fn === 'in' || fn === 'out' || fn === 'inOut') {
|
|
254
|
+
const fam = easingFamily(node.arguments[0]);
|
|
255
|
+
const dir = fn === 'in' ? 'IN' : fn === 'out' ? 'OUT' : 'IN_OUT';
|
|
256
|
+
return fam ? { ease: `ER_EASE_${fam}_${dir}`, bezier: null } : FALLBACK;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return FALLBACK;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Parses a `.interpolate({ inputRange, outputRange, extrapolate })` config object → a static
|
|
263
|
+
* { input, output, exLeft, exRight } descriptor (ranges must be static, equal-length, 2..8 points).
|
|
264
|
+
*
|
|
265
|
+
* @param cfgNode The config argument.
|
|
266
|
+
* @param env The expression environment.
|
|
267
|
+
*
|
|
268
|
+
* @returns The folded interpolation.
|
|
269
|
+
*/
|
|
270
|
+
export function parseInterp(cfgNode, env) {
|
|
271
|
+
// Interpolation ranges must be known at compile time so the generated C can bake the breakpoints directly.
|
|
272
|
+
if (cfgNode?.type !== 'ObjectExpression') {
|
|
273
|
+
throw aotError('AOT: .interpolate() needs a config object literal { inputRange, outputRange }');
|
|
274
|
+
}
|
|
275
|
+
// Read a named property from the interpolation config, accepting both identifier and string-literal keys.
|
|
276
|
+
const get = (key) => cfgNode.properties.find(property => (property.key.name ??
|
|
277
|
+
property.key.value) === key)?.value;
|
|
278
|
+
// Fold an interpolation range into numeric breakpoints; ranges must be array literals for AOT emission.
|
|
279
|
+
const arr = (node, name) => {
|
|
280
|
+
if (node?.type !== 'ArrayExpression') {
|
|
281
|
+
throw aotError(`AOT: .interpolate() ${name} must be an array literal`);
|
|
282
|
+
}
|
|
283
|
+
return node.elements.map(e => Number(evalStatic(e, env.consts ?? {})));
|
|
284
|
+
};
|
|
285
|
+
// inputRange and outputRange define the piecewise mapping and must line up point-for-point.
|
|
286
|
+
const input = arr(get('inputRange'), 'inputRange');
|
|
287
|
+
const output = arr(get('outputRange'), 'outputRange');
|
|
288
|
+
if (input.length < 2 || input.length !== output.length) {
|
|
289
|
+
throw aotError('AOT: .interpolate() inputRange and outputRange must be the same length (>= 2)');
|
|
290
|
+
}
|
|
291
|
+
check({ kind: 'interpolationPoints', count: input.length });
|
|
292
|
+
// Convert RN extrapolation strings to engine enum names, defaulting to extend like React Native.
|
|
293
|
+
const ex = (node) => {
|
|
294
|
+
const extrapolateMode = node
|
|
295
|
+
? String(evalStaticOr(node, env, 'extend'))
|
|
296
|
+
: 'extend';
|
|
297
|
+
return extrapolateMode === 'clamp'
|
|
298
|
+
? 'ER_EXTRAPOLATE_CLAMP'
|
|
299
|
+
: extrapolateMode === 'identity'
|
|
300
|
+
? 'ER_EXTRAPOLATE_IDENTITY'
|
|
301
|
+
: 'ER_EXTRAPOLATE_EXTEND';
|
|
302
|
+
};
|
|
303
|
+
// RN's `extrapolate` applies to both sides; side-specific props override it.
|
|
304
|
+
const both = get('extrapolate');
|
|
305
|
+
return {
|
|
306
|
+
input,
|
|
307
|
+
output,
|
|
308
|
+
exLeft: ex(get('extrapolateLeft') ?? both),
|
|
309
|
+
exRight: ex(get('extrapolateRight') ?? both),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Builds a `get(key)` accessor over an `Animated.*(value, config)` call's config object literal.
|
|
314
|
+
*
|
|
315
|
+
* @param cfgObj The config argument, if there is one.
|
|
316
|
+
*
|
|
317
|
+
* @returns The accessor; it reads undefined for every key when there is no config.
|
|
318
|
+
*/
|
|
319
|
+
function animConfigGetter(cfgObj) {
|
|
320
|
+
return key => cfgObj?.properties?.find(property => (property.key.name ??
|
|
321
|
+
property.key.value) === key)?.value;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* A folded config number that will be printed as a float, checked now so a value with no C form is reported
|
|
325
|
+
* where the animation is lowered.
|
|
326
|
+
*
|
|
327
|
+
* @param rawValue The folded value.
|
|
328
|
+
*
|
|
329
|
+
* @returns The number.
|
|
330
|
+
*/
|
|
331
|
+
function configFloat(rawValue) {
|
|
332
|
+
checkNumber(rawValue);
|
|
333
|
+
return Number(rawValue);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Lowers one atomic animation (timing/spring/decay). `delayMs` is the absolute start delay (how composition
|
|
337
|
+
* offsets are realized) and `loop` repeats a timing on the engine's own loop.
|
|
338
|
+
*
|
|
339
|
+
* @param entry The animation.
|
|
340
|
+
* @param env The expression environment.
|
|
341
|
+
*
|
|
342
|
+
* @returns The animation.
|
|
343
|
+
*/
|
|
344
|
+
function lowerAnimation(entry, env) {
|
|
345
|
+
const { cVar, kind, get, delayMs, loop } = entry;
|
|
346
|
+
// Fold the curve's fields, using RN-like defaults when the app leaves one out.
|
|
347
|
+
let curve;
|
|
348
|
+
if (kind === 'spring') {
|
|
349
|
+
curve = {
|
|
350
|
+
kind: 'spring',
|
|
351
|
+
stiffness: configFloat(evalStaticOr(get('stiffness'), env, 200)),
|
|
352
|
+
damping: configFloat(evalStaticOr(get('damping'), env, 18)),
|
|
353
|
+
mass: configFloat(evalStaticOr(get('mass'), env, 1)),
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
else if (kind === 'decay') {
|
|
357
|
+
curve = {
|
|
358
|
+
kind: 'decay',
|
|
359
|
+
deceleration: configFloat(evalStaticOr(get('deceleration'), env, 0.998)),
|
|
360
|
+
velocity: configFloat(evalStaticOr(get('velocity'), env, 0)),
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
const durationMs = Math.round(Number(evalStaticOr(get('duration'), env, 250)));
|
|
365
|
+
const { ease, bezier } = easingInfo(get('easing'), env);
|
|
366
|
+
curve = {
|
|
367
|
+
kind: 'timing',
|
|
368
|
+
durationMs,
|
|
369
|
+
easing: ease,
|
|
370
|
+
bezier: bezier ? bezier.map(configFloat) : null,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
// Decay is velocity-driven and has no toValue target; every other type needs one.
|
|
374
|
+
const toNode = get('toValue');
|
|
375
|
+
if (!toNode && kind !== 'decay') {
|
|
376
|
+
throw aotError(`AOT: Animated.${kind}() config needs a toValue`);
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
handle: cVar,
|
|
380
|
+
curve,
|
|
381
|
+
to: toNode ? lowerExpr(toNode, env) : null,
|
|
382
|
+
delayMs,
|
|
383
|
+
isLooping: loop,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Flattens an Animated composition (timing/spring/decay/sequence/parallel/stagger/delay/loop) into a flat
|
|
388
|
+
* list of atomic entries, each with an ABSOLUTE start delay (ms) — composition is realized purely through
|
|
389
|
+
* per-entry delay_ms (no engine grouping needed for standalone values). Returns { entries, duration } where
|
|
390
|
+
* `duration` is this node's own run length in ms, used to offset later siblings in a sequence/stagger;
|
|
391
|
+
* null = unknown (spring/decay/loop), which is illegal to sequence anything after.
|
|
392
|
+
*
|
|
393
|
+
* @param node The composition.
|
|
394
|
+
* @param env The expression environment.
|
|
395
|
+
* @param baseDelay When the composition starts, in ms.
|
|
396
|
+
* @param loop The composition repeats forever (inside an Animated.loop).
|
|
397
|
+
*
|
|
398
|
+
* @returns Its atomic entries and its run length.
|
|
399
|
+
*/
|
|
400
|
+
function flattenAnim(node, env, baseDelay, loop) {
|
|
401
|
+
// Resolve a named animation handle, then require one of the supported Animated.* calls.
|
|
402
|
+
node = resolveAnim(node, env);
|
|
403
|
+
if (!isAnimatedCall(node)) {
|
|
404
|
+
throw aotError('AOT: an animation must be Animated.timing/spring/decay/sequence/parallel/stagger/delay/loop(...)');
|
|
405
|
+
}
|
|
406
|
+
// Atomic animations drive one useAnimatedValue directly; timing has a known length, spring/decay do not.
|
|
407
|
+
const kind = node.callee.property.name;
|
|
408
|
+
const args = node.arguments;
|
|
409
|
+
if (kind === 'timing' || kind === 'spring' || kind === 'decay') {
|
|
410
|
+
const valRef = args[0];
|
|
411
|
+
if (valRef?.type !== 'Identifier' || !env.animations?.has(valRef.name)) {
|
|
412
|
+
throw aotError(`AOT: Animated.${kind}() first argument must be a useAnimatedValue`);
|
|
413
|
+
}
|
|
414
|
+
const cVar = env.animations.get(valRef.name).cVar;
|
|
415
|
+
const get = animConfigGetter(args[1]);
|
|
416
|
+
const ownDelay = Math.round(Number(evalStaticOr(get('delay'), env, 0)));
|
|
417
|
+
const duration = kind === 'timing'
|
|
418
|
+
? ownDelay + Math.round(Number(evalStaticOr(get('duration'), env, 250)))
|
|
419
|
+
: null;
|
|
420
|
+
return {
|
|
421
|
+
entries: [{ cVar, kind, get, delayMs: baseDelay + ownDelay, loop }],
|
|
422
|
+
duration,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
// Animated.delay contributes time to a composition but starts no engine animation itself.
|
|
426
|
+
if (kind === 'delay') {
|
|
427
|
+
return {
|
|
428
|
+
entries: [],
|
|
429
|
+
duration: Math.round(Number(evalStaticOr(args[0], env, 0))),
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
// Composition nodes flatten into atomic animations with absolute delay_ms offsets.
|
|
433
|
+
if (kind === 'sequence' || kind === 'parallel' || kind === 'stagger') {
|
|
434
|
+
const list = kind === 'stagger' ? args[1] : args[0];
|
|
435
|
+
const staggerMs = kind === 'stagger'
|
|
436
|
+
? Math.round(Number(evalStaticOr(args[0], env, 0)))
|
|
437
|
+
: 0;
|
|
438
|
+
if (list?.type !== 'ArrayExpression') {
|
|
439
|
+
throw aotError(`AOT: Animated.${kind}(...) needs an array of animations`);
|
|
440
|
+
}
|
|
441
|
+
// Sequence advances its running offset; parallel/stagger start children from the same base with offsets.
|
|
442
|
+
const entries = [];
|
|
443
|
+
let off = baseDelay; // running offset (sequence)
|
|
444
|
+
let groupDur = 0; // max end-time relative to baseDelay (parallel/stagger)
|
|
445
|
+
let index = 0;
|
|
446
|
+
for (const child of list.elements) {
|
|
447
|
+
if (!child)
|
|
448
|
+
continue;
|
|
449
|
+
const start = kind === 'sequence' ? off : baseDelay + index * staggerMs;
|
|
450
|
+
const flatAnim = flattenAnim(child, env, start, loop);
|
|
451
|
+
entries.push(...flatAnim.entries);
|
|
452
|
+
if (kind === 'sequence') {
|
|
453
|
+
if (flatAnim.duration == null) {
|
|
454
|
+
throw aotError('AOT: an Animated.sequence entry needs a known duration — use Animated.timing / Animated.delay (a spring/decay/loop inside a sequence is not supported; it has no fixed length to offset the next entry by)');
|
|
455
|
+
}
|
|
456
|
+
off += flatAnim.duration;
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
const end = start - baseDelay + (flatAnim.duration ?? 0);
|
|
460
|
+
if (end > groupDur) {
|
|
461
|
+
groupDur = end;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
index++;
|
|
465
|
+
}
|
|
466
|
+
// The same value can't appear twice in a flat (delay_ms) composition: er_anim_value_animate cancels the
|
|
467
|
+
// running anim on a value, so concurrent/flat-sequenced same-value steps cancel each other. (A
|
|
468
|
+
// top-level Animated.sequence is handled separately via on_complete chaining, which does support this.)
|
|
469
|
+
const seen = new Set();
|
|
470
|
+
for (const entry of entries) {
|
|
471
|
+
if (seen.has(entry.cVar)) {
|
|
472
|
+
throw aotError('AOT: the same animated value is driven more than once in this composition — concurrent/flat same-value steps cancel each other. Use a top-level Animated.sequence(...) for multi-step animation of one value.');
|
|
473
|
+
}
|
|
474
|
+
seen.add(entry.cVar);
|
|
475
|
+
}
|
|
476
|
+
return {
|
|
477
|
+
entries,
|
|
478
|
+
duration: kind === 'sequence' ? off - baseDelay : groupDur,
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
// A nested loop can flatten only when it wraps a single atomic animation; complex loops need start-time chaining.
|
|
482
|
+
if (kind === 'loop') {
|
|
483
|
+
const flatAnim = flattenAnim(args[0], env, baseDelay, true);
|
|
484
|
+
if (flatAnim.entries.length !== 1) {
|
|
485
|
+
throw aotError('AOT: an Animated.loop inside another composition can only wrap a single Animated.timing/spring/decay', 'a loop around a sequence works when the loop is the animation you start: Animated.loop(Animated.sequence([...])).start().');
|
|
486
|
+
}
|
|
487
|
+
return { entries: flatAnim.entries, duration: null };
|
|
488
|
+
}
|
|
489
|
+
throw aotError(`AOT: Animated.${kind}(...) is not a supported animation`);
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* True for an `Animated.timing/spring/…/loop(…)` call: an animation, not a value.
|
|
493
|
+
*
|
|
494
|
+
* @param node Any node, or nothing.
|
|
495
|
+
*
|
|
496
|
+
* @returns Whether it is such a call.
|
|
497
|
+
*/
|
|
498
|
+
export const isAnimatedCall = (node) => node?.type === 'CallExpression' &&
|
|
499
|
+
node.callee.type === 'MemberExpression' &&
|
|
500
|
+
node.callee.object?.name === 'Animated' &&
|
|
501
|
+
ANIM_KINDS.has(node.callee.property?.name);
|
|
502
|
+
/**
|
|
503
|
+
* An animation handle (`anim` after `const anim = Animated.loop(…)`) resolved to the call it names.
|
|
504
|
+
*
|
|
505
|
+
* @param node An animation, or a handler local bound to one.
|
|
506
|
+
* @param env The expression environment.
|
|
507
|
+
*
|
|
508
|
+
* @returns The animation the node stands for.
|
|
509
|
+
*/
|
|
510
|
+
export const resolveAnim = (node, env) => node?.type === 'Identifier' && env.animLocals?.has(node.name)
|
|
511
|
+
? env.animLocals.get(node.name)
|
|
512
|
+
: node;
|
|
513
|
+
/**
|
|
514
|
+
* One Animated.timing/spring/decay call as a chain step, started `extraDelay` ms later than its own delay.
|
|
515
|
+
*
|
|
516
|
+
* @param node The call.
|
|
517
|
+
* @param env The expression environment.
|
|
518
|
+
* @param extraDelay Milliseconds added to its own delay.
|
|
519
|
+
*
|
|
520
|
+
* @returns The step.
|
|
521
|
+
*/
|
|
522
|
+
function animStep(node, env, extraDelay) {
|
|
523
|
+
const kind = node.callee.property.name;
|
|
524
|
+
// A chain step must drive one collected useAnimatedValue.
|
|
525
|
+
const valRef = node.arguments[0];
|
|
526
|
+
if (valRef?.type !== 'Identifier' || !env.animations?.has(valRef.name)) {
|
|
527
|
+
throw aotError(`AOT: Animated.${kind}() first argument must be a useAnimatedValue`);
|
|
528
|
+
}
|
|
529
|
+
// Fold this step's own delay and add any delay inherited from an enclosing sequence/delay.
|
|
530
|
+
const get = animConfigGetter(node.arguments[1]);
|
|
531
|
+
const ownDelay = Math.round(Number(evalStaticOr(get('delay'), env, 0)));
|
|
532
|
+
return {
|
|
533
|
+
cVar: env.animations.get(valRef.name).cVar,
|
|
534
|
+
kind,
|
|
535
|
+
get,
|
|
536
|
+
delayMs: extraDelay + ownDelay,
|
|
537
|
+
loop: false,
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* The C handle of every value an animation drives.
|
|
542
|
+
*
|
|
543
|
+
* @param node The animation.
|
|
544
|
+
* @param env The expression environment.
|
|
545
|
+
* @param accumulator Collects the handles.
|
|
546
|
+
*
|
|
547
|
+
* @returns `accumulator`.
|
|
548
|
+
*/
|
|
549
|
+
export function animValues(node, env, accumulator = new Set()) {
|
|
550
|
+
node = resolveAnim(node, env);
|
|
551
|
+
// Reject props that the FlatList-to-ScrollView rewrite cannot preserve.
|
|
552
|
+
if (!isAnimatedCall(node)) {
|
|
553
|
+
throw aotError('AOT: an animation must be Animated.timing/spring/decay/sequence/parallel/stagger/delay/loop(...)');
|
|
554
|
+
}
|
|
555
|
+
// Recurse into composed animations, skipping delay nodes because they drive no animated value.
|
|
556
|
+
const kind = node.callee.property.name;
|
|
557
|
+
const args = node.arguments;
|
|
558
|
+
if (kind === 'timing' || kind === 'spring' || kind === 'decay') {
|
|
559
|
+
accumulator.add(animStep(node, env, 0).cVar);
|
|
560
|
+
}
|
|
561
|
+
else if (kind === 'loop') {
|
|
562
|
+
animValues(args[0], env, accumulator);
|
|
563
|
+
}
|
|
564
|
+
else if (kind !== 'delay') {
|
|
565
|
+
const list = kind === 'stagger' ? args[1] : args[0];
|
|
566
|
+
for (const child of list?.elements ??
|
|
567
|
+
[]) {
|
|
568
|
+
if (child) {
|
|
569
|
+
animValues(child, env, accumulator);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return accumulator;
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* The steps of an `Animated.sequence([...])` for an on_complete chain. A delay() folds into the next step's
|
|
577
|
+
* delay_ms; `trailingDelay` is what follows the last step. A nested parallel/stagger/loop throws.
|
|
578
|
+
*
|
|
579
|
+
* @param seqNode The sequence call.
|
|
580
|
+
* @param env The expression environment.
|
|
581
|
+
*
|
|
582
|
+
* @returns The steps, and the delay after the last one.
|
|
583
|
+
*/
|
|
584
|
+
function sequenceSteps(seqNode, env) {
|
|
585
|
+
const list = seqNode.arguments[0];
|
|
586
|
+
// Sequence composition is lowered at build time, so its children must be an array literal.
|
|
587
|
+
if (list?.type !== 'ArrayExpression') {
|
|
588
|
+
throw aotError('AOT: Animated.sequence(...) needs an array of animations');
|
|
589
|
+
}
|
|
590
|
+
// Fold delay entries into the next real animation step so the sequence can be emitted as an on-complete chain.
|
|
591
|
+
const steps = [];
|
|
592
|
+
let pendingDelay = 0;
|
|
593
|
+
for (const element of list.elements) {
|
|
594
|
+
if (!element)
|
|
595
|
+
continue;
|
|
596
|
+
const child = resolveAnim(element, env);
|
|
597
|
+
// Reject non-animation entries in the sequence.
|
|
598
|
+
if (!isAnimatedCall(child)) {
|
|
599
|
+
throw aotError('AOT: Animated.sequence entries must be Animated.timing/spring/decay/delay(...)');
|
|
600
|
+
}
|
|
601
|
+
// Fold delay-only entries into the following animation step.
|
|
602
|
+
const kind = child.callee.property.name;
|
|
603
|
+
if (kind === 'delay') {
|
|
604
|
+
pendingDelay += Math.round(Number(evalStaticOr(child.arguments[0], env, 0)));
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
// Keep sequence chains flat: delay plus timing/spring/decay only.
|
|
608
|
+
if (kind !== 'timing' && kind !== 'spring' && kind !== 'decay') {
|
|
609
|
+
throw aotError('AOT: Animated.sequence entries must be Animated.timing/spring/decay/delay (a nested ' +
|
|
610
|
+
'parallel/stagger/loop inside a sequence is not yet supported — keep the sequence flat)');
|
|
611
|
+
}
|
|
612
|
+
steps.push(animStep(child, env, pendingDelay));
|
|
613
|
+
pendingDelay = 0;
|
|
614
|
+
}
|
|
615
|
+
return { steps, trailingDelay: pendingDelay };
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Lowers steps to an on_complete CHAIN: step 0 starts inline, and each step's completion callback starts the
|
|
619
|
+
* next. Unlike the flat delay_ms path, this is correct when steps share a value — er_anim_value_animate
|
|
620
|
+
* cancels the running anim on a value, so starting them together would cancel all but the last — and needs
|
|
621
|
+
* no fixed duration. A step that is interrupted (finished == false) ends the chain, as it does in JS. The
|
|
622
|
+
* chain's callbacks and loop slots are added to the output here.
|
|
623
|
+
*
|
|
624
|
+
* `loop` makes the last step start the first again: {iterations (negative = forever), resetTo (the value to
|
|
625
|
+
* put back where it started before each repeat, or null), trailingDelay (ms before each repeat)}.
|
|
626
|
+
*
|
|
627
|
+
* @param steps The chain's steps.
|
|
628
|
+
* @param env The expression environment.
|
|
629
|
+
* @param ctx The statement compiler's context.
|
|
630
|
+
* @param completion The completion callback to run when the chain ends, if any.
|
|
631
|
+
* @param loop How the chain repeats, or null for once.
|
|
632
|
+
* @param trailing For a chain that runs once: the composition's key and the wait after its last step.
|
|
633
|
+
*
|
|
634
|
+
* @returns The chain.
|
|
635
|
+
*/
|
|
636
|
+
function lowerChain(steps, env, ctx, completion, loop = null, trailing = null) {
|
|
637
|
+
// GLOBAL: callback names are file-scope, so must be unique across handlers
|
|
638
|
+
const id = ctx.out.seqN++;
|
|
639
|
+
// Lowered from the tail, the order the callbacks are emitted in.
|
|
640
|
+
const lowered = new Array(steps.length);
|
|
641
|
+
for (let index = steps.length - 1; index >= 0; index--) {
|
|
642
|
+
lowered[index] = lowerAnimation(steps[index], env);
|
|
643
|
+
}
|
|
644
|
+
// A repeat restarts step 0 after any trailing sequence delay.
|
|
645
|
+
const chain = {
|
|
646
|
+
kind: 'startChain',
|
|
647
|
+
id,
|
|
648
|
+
steps: lowered,
|
|
649
|
+
loop: loop
|
|
650
|
+
? {
|
|
651
|
+
iterations: loop.iterations,
|
|
652
|
+
resetHandle: loop.resetTo,
|
|
653
|
+
restart: lowerAnimation({ ...steps[0], delayMs: steps[0].delayMs + loop.trailingDelay }, env),
|
|
654
|
+
finalWait: loop.iterations >= 0 && completion && loop.trailingDelay > 0
|
|
655
|
+
? {
|
|
656
|
+
delayMs: loop.trailingDelay,
|
|
657
|
+
owner: waitOwner(ctx, loop.key, true),
|
|
658
|
+
}
|
|
659
|
+
: null,
|
|
660
|
+
}
|
|
661
|
+
: null,
|
|
662
|
+
completion,
|
|
663
|
+
completionWait: !loop && completion && trailing && trailing.delayMs > 0
|
|
664
|
+
? completionWait(ctx, trailing.key, trailing.delayMs, `er_seqcb_${id}_${steps.length}`)
|
|
665
|
+
: null,
|
|
666
|
+
};
|
|
667
|
+
ctx.out.animCbs.push({ kind: 'chain', chain });
|
|
668
|
+
ctx.out.effectDecls.push({ kind: 'chain', chain });
|
|
669
|
+
return chain;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* A start with nothing to run.
|
|
673
|
+
*
|
|
674
|
+
* @param ctx The statement compiler's context.
|
|
675
|
+
* @param completion The completion callback, if any; it never runs.
|
|
676
|
+
*
|
|
677
|
+
* @returns The start.
|
|
678
|
+
*/
|
|
679
|
+
const emptyStart = (ctx, completion) => ({
|
|
680
|
+
kind: 'startAnimations',
|
|
681
|
+
animations: [],
|
|
682
|
+
firstConfig: ctx.animIdx,
|
|
683
|
+
completion,
|
|
684
|
+
completionWait: null,
|
|
685
|
+
});
|
|
686
|
+
/** Numbers each composition node once, so a key names the place it is written. */
|
|
687
|
+
const compositionIds = new WeakMap();
|
|
688
|
+
let nextCompositionId = 0;
|
|
689
|
+
/**
|
|
690
|
+
* What identifies a composition as the owner of its completion waits: where it is written, within one component
|
|
691
|
+
* instance. Each run still waits on its own; the owner is what a stop names, so it ends the runs of the
|
|
692
|
+
* composition it stops and no other. A start and stop through the same local (`const pulse = …; pulse.start();
|
|
693
|
+
* … pulse.stop()`) reach the same node.
|
|
694
|
+
*
|
|
695
|
+
* @param node The composition, resolved.
|
|
696
|
+
* @param env The expression environment.
|
|
697
|
+
*
|
|
698
|
+
* @returns The key.
|
|
699
|
+
*/
|
|
700
|
+
function compositionKey(node, env) {
|
|
701
|
+
let id = node ? compositionIds.get(node) : -1;
|
|
702
|
+
if (id === undefined) {
|
|
703
|
+
id = nextCompositionId++;
|
|
704
|
+
compositionIds.set(node, id);
|
|
705
|
+
}
|
|
706
|
+
return `${env.cbPrefix ?? ''}|${id}`;
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* The owner number completion waits of a composition carry, so a stop can end them; numbered on first use.
|
|
710
|
+
*
|
|
711
|
+
* @param ctx The statement compiler's context.
|
|
712
|
+
* @param key The composition's key.
|
|
713
|
+
* @param isStart A start is asking, so the waits are real (a stop alone leaves nothing to end).
|
|
714
|
+
*
|
|
715
|
+
* @returns The owner.
|
|
716
|
+
*/
|
|
717
|
+
function waitOwner(ctx, key, isStart) {
|
|
718
|
+
let record = ctx.out.animWaits.get(key);
|
|
719
|
+
if (!record) {
|
|
720
|
+
record = { owner: ctx.out.animWaits.size, isStarted: false };
|
|
721
|
+
ctx.out.animWaits.set(key, record);
|
|
722
|
+
}
|
|
723
|
+
if (isStart) {
|
|
724
|
+
record.isStarted = true;
|
|
725
|
+
}
|
|
726
|
+
return record.owner;
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* True when a composition has an Animated.delay anywhere, the only thing a completion can wait on.
|
|
730
|
+
*
|
|
731
|
+
* @param node The composition.
|
|
732
|
+
* @param env The expression environment.
|
|
733
|
+
*
|
|
734
|
+
* @returns Whether it does.
|
|
735
|
+
*/
|
|
736
|
+
function hasDelay(node, env) {
|
|
737
|
+
node = resolveAnim(node, env);
|
|
738
|
+
if (!isAnimatedCall(node))
|
|
739
|
+
return false;
|
|
740
|
+
// Look through compositions; a timing, spring or decay waits on nothing beyond itself.
|
|
741
|
+
const kind = node.callee.property.name;
|
|
742
|
+
const args = node.arguments;
|
|
743
|
+
if (kind === 'delay')
|
|
744
|
+
return true;
|
|
745
|
+
if (kind === 'loop')
|
|
746
|
+
return hasDelay(args[0], env);
|
|
747
|
+
if (kind === 'sequence' || kind === 'parallel' || kind === 'stagger') {
|
|
748
|
+
const list = kind === 'stagger' ? args[1] : args[0];
|
|
749
|
+
return (list?.type === 'ArrayExpression' &&
|
|
750
|
+
list.elements.some(child => child && hasDelay(child, env)));
|
|
751
|
+
}
|
|
752
|
+
return false;
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* The composition whose completion waits `anim.stop()` ends, when it has a delay to wait on.
|
|
756
|
+
*
|
|
757
|
+
* @param node The composition being stopped.
|
|
758
|
+
* @param env The expression environment.
|
|
759
|
+
* @param ctx The statement compiler's context.
|
|
760
|
+
*
|
|
761
|
+
* @returns The owner, or null.
|
|
762
|
+
*/
|
|
763
|
+
export function stopWaitOwner(node, env, ctx) {
|
|
764
|
+
const resolved = resolveAnim(node, env);
|
|
765
|
+
return hasDelay(resolved, env)
|
|
766
|
+
? waitOwner(ctx, compositionKey(resolved, env), false)
|
|
767
|
+
: null;
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* The wait between a composition's last animation finishing and its completion running.
|
|
771
|
+
*
|
|
772
|
+
* @param ctx The statement compiler's context.
|
|
773
|
+
* @param key The composition's key.
|
|
774
|
+
* @param delayMs The wait.
|
|
775
|
+
* @param callback The name of the on_complete callback that starts it.
|
|
776
|
+
*
|
|
777
|
+
* @returns The wait.
|
|
778
|
+
*/
|
|
779
|
+
const completionWait = (ctx, key, delayMs, callback) => ({ delayMs, callback, owner: waitOwner(ctx, key, true) });
|
|
780
|
+
/**
|
|
781
|
+
* A start that drives no value. As in JS, its completion still runs as finished once the composition's delays
|
|
782
|
+
* have passed, counted down in a completion wait of its own.
|
|
783
|
+
*
|
|
784
|
+
* @param ctx The statement compiler's context.
|
|
785
|
+
* @param completion The completion callback, if any.
|
|
786
|
+
* @param delayMs How long the composition takes.
|
|
787
|
+
* @param key The composition's key.
|
|
788
|
+
*
|
|
789
|
+
* @returns The start.
|
|
790
|
+
*/
|
|
791
|
+
function startNothing(ctx, completion, delayMs, key) {
|
|
792
|
+
if (!completion) {
|
|
793
|
+
return emptyStart(ctx, completion);
|
|
794
|
+
}
|
|
795
|
+
const start = {
|
|
796
|
+
kind: 'completeAfter',
|
|
797
|
+
completion,
|
|
798
|
+
delayMs,
|
|
799
|
+
owner: null,
|
|
800
|
+
};
|
|
801
|
+
if (delayMs > 0) {
|
|
802
|
+
start.owner = waitOwner(ctx, key, true);
|
|
803
|
+
}
|
|
804
|
+
return start;
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Lowers `Animated.loop(animation, config?).start()`, or returns null for the flat path. An endless loop of
|
|
808
|
+
* one timing is the engine's own cfg.loop; anything else (a sequence, a spring or decay, a counted loop) is a
|
|
809
|
+
* chain whose last step starts the first again. As in JS, a looped single animation goes back to where it
|
|
810
|
+
* started before each repeat (resetBeforeIteration), and a looped sequence carries on from where it ended.
|
|
811
|
+
*
|
|
812
|
+
* @param loopNode The `Animated.loop(…)` call.
|
|
813
|
+
* @param env The expression environment.
|
|
814
|
+
* @param ctx The statement compiler's context.
|
|
815
|
+
* @param completion The completion callback to run when the loop ends, if any.
|
|
816
|
+
* @param key The loop's composition key.
|
|
817
|
+
*
|
|
818
|
+
* @returns The start, or null when the flat path (the engine's own cfg.loop) handles it.
|
|
819
|
+
*/
|
|
820
|
+
function lowerLoopStart(loopNode, env, ctx, completion, key) {
|
|
821
|
+
// Fold the loop count at build time; absent or noninteger iterations mean repeat forever.
|
|
822
|
+
const inner = resolveAnim(loopNode.arguments[0], env);
|
|
823
|
+
const kind = isAnimatedCall(inner) ? inner.callee.property.name : null;
|
|
824
|
+
const get = animConfigGetter(loopNode.arguments[1]);
|
|
825
|
+
let iterations = -1;
|
|
826
|
+
if (get('iterations')) {
|
|
827
|
+
let value;
|
|
828
|
+
try {
|
|
829
|
+
value = evalStatic(get('iterations'), foldScope(env, env.consts ?? {}));
|
|
830
|
+
}
|
|
831
|
+
catch {
|
|
832
|
+
throw aotError('AOT: Animated.loop iterations must be known at build time', 'use a number or a module-level constant.');
|
|
833
|
+
}
|
|
834
|
+
// JS loops forever on anything but a whole number.
|
|
835
|
+
iterations = Number.isInteger(value)
|
|
836
|
+
? Math.min(value, INT_MAX)
|
|
837
|
+
: -1;
|
|
838
|
+
}
|
|
839
|
+
// Use the engine's native loop only for endless reset timing loops; other loop shapes need callback chaining.
|
|
840
|
+
const reset = evalStaticOr(get('resetBeforeIteration'), env, true) !== false;
|
|
841
|
+
let steps;
|
|
842
|
+
let trailingDelay = 0;
|
|
843
|
+
let resetTo = null;
|
|
844
|
+
if (kind === 'sequence') {
|
|
845
|
+
({ steps, trailingDelay } = sequenceSteps(inner, env));
|
|
846
|
+
}
|
|
847
|
+
else if (kind === 'timing' || kind === 'spring' || kind === 'decay') {
|
|
848
|
+
if (kind === 'timing' && iterations < 0 && reset)
|
|
849
|
+
return null;
|
|
850
|
+
steps = [animStep(inner, env, 0)];
|
|
851
|
+
if (reset) {
|
|
852
|
+
resetTo = steps[0].cVar;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
else {
|
|
856
|
+
throw aotError('AOT: Animated.loop can repeat a timing, spring, decay or sequence (looping a parallel/stagger is not yet supported)');
|
|
857
|
+
}
|
|
858
|
+
// A zero-iteration loop starts nothing but still reports successful completion.
|
|
859
|
+
if (iterations === 0) {
|
|
860
|
+
return startNothing(ctx, completion, 0, key);
|
|
861
|
+
}
|
|
862
|
+
// A loop of delays alone completes after its passes; an endless one never does.
|
|
863
|
+
if (!steps.length) {
|
|
864
|
+
return iterations < 0
|
|
865
|
+
? emptyStart(ctx, completion)
|
|
866
|
+
: startNothing(ctx, completion, Math.min(iterations * trailingDelay, INT_MAX), key);
|
|
867
|
+
}
|
|
868
|
+
// A timing with no duration and no delay completes inside er_anim_value_animate, so a loop made only of
|
|
869
|
+
// those would restart itself forever without returning.
|
|
870
|
+
const instant = (animEntry) => animEntry.kind === 'timing' &&
|
|
871
|
+
animEntry.delayMs === 0 &&
|
|
872
|
+
Math.round(Number(evalStaticOr(animEntry.get('duration'), env, 250))) === 0;
|
|
873
|
+
if (trailingDelay === 0 && steps.every(instant)) {
|
|
874
|
+
throw aotError('AOT: every step of this Animated.loop finishes instantly, so it would never stop repeating', 'give a step a duration or a delay.');
|
|
875
|
+
}
|
|
876
|
+
return lowerChain(steps, env, ctx, completion, {
|
|
877
|
+
iterations,
|
|
878
|
+
resetTo,
|
|
879
|
+
trailingDelay,
|
|
880
|
+
key,
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Lowers a `.start(onComplete)` completion callback to a file-scope C fn (ERAnimCompleteFn) set as the
|
|
885
|
+
* animation's on_complete, and adds it to the output; its body runs setters/refs/etc. and re-applies state via
|
|
886
|
+
* app_update if needed.
|
|
887
|
+
*
|
|
888
|
+
* @param fnNode The callback.
|
|
889
|
+
* @param env The expression environment.
|
|
890
|
+
* @param state The component's state table.
|
|
891
|
+
* @param ctx The statement compiler's context.
|
|
892
|
+
*
|
|
893
|
+
* @returns The completion.
|
|
894
|
+
*/
|
|
895
|
+
function lowerCompletion(fnNode, env, state, ctx) {
|
|
896
|
+
// Generate a file-scope completion callback and expose start(({finished}) => ...) as a local C int.
|
|
897
|
+
const id = ctx.out.seqN++;
|
|
898
|
+
const name = `er_donecb_${id}`;
|
|
899
|
+
const locals = new Map(env.locals);
|
|
900
|
+
const param = fnNode.params[0];
|
|
901
|
+
if (param?.type === 'ObjectPattern') {
|
|
902
|
+
for (const property of param.properties) {
|
|
903
|
+
if ((property.key?.name ??
|
|
904
|
+
property.key?.value) === 'finished') {
|
|
905
|
+
locals.set(property.value?.name ?? 'finished', {
|
|
906
|
+
code: 'finished',
|
|
907
|
+
cType: 'int',
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
// Lower the callback body as statements; it re-renders if it changed React state.
|
|
913
|
+
const fnBody = fnNode.body;
|
|
914
|
+
const list = fnBody.type === 'BlockStatement'
|
|
915
|
+
? fnBody.body
|
|
916
|
+
: [{ type: 'ExpressionStatement', expression: fnBody }];
|
|
917
|
+
const cctx = {
|
|
918
|
+
stateChanged: false,
|
|
919
|
+
animIdx: 0,
|
|
920
|
+
out: ctx.out,
|
|
921
|
+
};
|
|
922
|
+
const body = lowerStmts(list, { ...env, locals }, state, cctx);
|
|
923
|
+
const completion = { name, body, updatesState: cctx.stateChanged };
|
|
924
|
+
ctx.out.animCbs.push({ kind: 'completion', completion });
|
|
925
|
+
return completion;
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Lowers `<animation>.start()` — a single Animated.timing/spring/decay or a composition
|
|
929
|
+
* (sequence/parallel/stagger/delay/loop). A sequence, and a loop the engine cannot repeat on its own, become
|
|
930
|
+
* an on_complete chain (lowerChain); everything else flattens to one animation per atomic entry, composition
|
|
931
|
+
* expressed through per-entry delay_ms. Native-driven; sets no React state.
|
|
932
|
+
*
|
|
933
|
+
* @param expr The `.start(…)` call.
|
|
934
|
+
* @param env The expression environment.
|
|
935
|
+
* @param state The component's state table.
|
|
936
|
+
* @param ctx The statement compiler's context.
|
|
937
|
+
*
|
|
938
|
+
* @returns The start.
|
|
939
|
+
*/
|
|
940
|
+
export function lowerAnimateStart(expr, env, state, ctx) {
|
|
941
|
+
// Resolve the animation being started and handle cases that need callback chaining before falling back to flat emission.
|
|
942
|
+
const completion = isFn(expr.arguments[0])
|
|
943
|
+
? lowerCompletion(expr.arguments[0], env, state, ctx)
|
|
944
|
+
: null;
|
|
945
|
+
const receiver = resolveAnim(expr.callee.object, env);
|
|
946
|
+
const kind = isAnimatedCall(receiver) ? receiver.callee.property.name : null;
|
|
947
|
+
const key = compositionKey(receiver, env);
|
|
948
|
+
if (kind === 'sequence') {
|
|
949
|
+
const { steps, trailingDelay } = sequenceSteps(receiver, env);
|
|
950
|
+
return steps.length
|
|
951
|
+
? lowerChain(steps, env, ctx, completion, null, {
|
|
952
|
+
key,
|
|
953
|
+
delayMs: trailingDelay,
|
|
954
|
+
})
|
|
955
|
+
: startNothing(ctx, completion, trailingDelay, key);
|
|
956
|
+
}
|
|
957
|
+
if (kind === 'loop') {
|
|
958
|
+
const start = lowerLoopStart(receiver, env, ctx, completion, key);
|
|
959
|
+
if (start)
|
|
960
|
+
return start;
|
|
961
|
+
}
|
|
962
|
+
// The flat path has no "after all entries finish" join point, so completion callbacks are limited to one entry.
|
|
963
|
+
const { entries, duration } = flattenAnim(receiver, env, 0, false);
|
|
964
|
+
if (!entries.length) {
|
|
965
|
+
return startNothing(ctx, completion, duration ?? 0, key);
|
|
966
|
+
}
|
|
967
|
+
if (completion && entries.length > 1) {
|
|
968
|
+
throw aotError('AOT: a .start(onComplete) callback on a parallel/stagger animation is not yet supported', 'attach the completion callback to a single animation or an Animated.sequence(...). For "after all parallel anims", restructure as a sequence.');
|
|
969
|
+
}
|
|
970
|
+
// A spring or decay has no fixed end, so a delay beside it that runs past its start cannot be waited out yet.
|
|
971
|
+
const [only] = entries;
|
|
972
|
+
if (completion &&
|
|
973
|
+
only &&
|
|
974
|
+
only.kind !== 'timing' &&
|
|
975
|
+
!only.loop &&
|
|
976
|
+
duration != null &&
|
|
977
|
+
duration > only.delayMs) {
|
|
978
|
+
throw aotError(`AOT: a .start(onComplete) callback on an Animated.${only.kind} beside a longer Animated.delay is not yet supported`, `a ${only.kind} has no fixed length, so the callback cannot wait for whichever ends last. Run them in an ` +
|
|
979
|
+
'Animated.sequence([...]) instead, or drop the delay.');
|
|
980
|
+
}
|
|
981
|
+
// Each entry starts on its own, numbered after the configs already in this body.
|
|
982
|
+
const start = emptyStart(ctx, completion);
|
|
983
|
+
for (const animEntry of entries) {
|
|
984
|
+
start.animations.push(lowerAnimation(animEntry, env));
|
|
985
|
+
ctx.animIdx++;
|
|
986
|
+
}
|
|
987
|
+
// A delay beside the one timing that outlasts it holds the completion back until the composition ends.
|
|
988
|
+
if (completion && only?.kind === 'timing' && !only.loop && duration != null) {
|
|
989
|
+
const end = only.delayMs +
|
|
990
|
+
Math.round(Number(evalStaticOr(only.get('duration'), env, 250)));
|
|
991
|
+
if (duration > end) {
|
|
992
|
+
start.completionWait = completionWait(ctx, key, duration - end, `er_waitcb_${ctx.out.seqN++}`);
|
|
993
|
+
ctx.out.animCbs.push({
|
|
994
|
+
kind: 'completionWait',
|
|
995
|
+
completion,
|
|
996
|
+
wait: start.completionWait,
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
return start;
|
|
1001
|
+
}
|