embedded-react 0.14.1 → 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/README.md +51 -486
- 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.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/FlatList.js +4 -4
- package/src/embedded-react/index.d.ts +2 -1
- 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 { aotError } from './diagnostics.mjs';
|
|
20
|
+
import { evalStatic, withUndefined } from './static-eval.mjs';
|
|
21
|
+
import { emitSourcedWide, lowerExpr } from './expressions.mjs';
|
|
22
|
+
import { isFn, declaredNames, collectState, componentReturnJSX, collectHelpers, collectCallbacks, collectMemos, collectEffects, usesState, } from './collect.mjs';
|
|
23
|
+
import { collectAnims, collectRefs } from './animations.mjs';
|
|
24
|
+
import { attrExpr } from './style-text.mjs';
|
|
25
|
+
import { inlineFragments } from './elements/props.mjs';
|
|
26
|
+
import { lowerEffect } from './handlers.mjs';
|
|
27
|
+
import { collectPanResponders, panSpreadTarget } from './pan-responder.mjs';
|
|
28
|
+
import { emitNode } from './nodes.mjs';
|
|
29
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
30
|
+
- Implementation
|
|
31
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
32
|
+
/**
|
|
33
|
+
* Reads a component's props as descriptors: `{static:true,value}` (folded),
|
|
34
|
+
* `{static:false,code, cType, struct}` (a runtime C expression — e.g., a list row's `item.field`), or
|
|
35
|
+
* `{fn:true,node}` (a callback).
|
|
36
|
+
*
|
|
37
|
+
* @param openingElement The component instance's opening tag.
|
|
38
|
+
* @param scope Compile-time constants in reach at the call site.
|
|
39
|
+
* @param env The caller's expression environment.
|
|
40
|
+
*
|
|
41
|
+
* @returns The props, by name.
|
|
42
|
+
*/
|
|
43
|
+
function extractProps(openingElement, scope, env) {
|
|
44
|
+
const props = {};
|
|
45
|
+
for (const attr of openingElement.attributes) {
|
|
46
|
+
// Static spread: {...obj} where obj folds to a compile-time object → merge its keys as props.
|
|
47
|
+
if (attr.type === 'JSXSpreadAttribute') {
|
|
48
|
+
// A PanResponder's handlers need a real scene node, which a component instance is not.
|
|
49
|
+
if (panSpreadTarget(attr.argument, env)) {
|
|
50
|
+
throw aotError('AOT: a PanResponder can only be spread onto a host element', 'spread `{...pan.panHandlers}` onto the <View> (or <Pressable>/<ScrollView>) that ' +
|
|
51
|
+
'should own the gesture, not onto a component instance — the AOT wires the responder ' +
|
|
52
|
+
'to a real scene node.');
|
|
53
|
+
}
|
|
54
|
+
let spreadObject;
|
|
55
|
+
try {
|
|
56
|
+
spreadObject = evalStatic(attr.argument, scope);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
throw new Error('AOT: only a compile-time-constant object can be spread to a component ({...obj})');
|
|
60
|
+
}
|
|
61
|
+
if (spreadObject == null || typeof spreadObject !== 'object') {
|
|
62
|
+
throw new Error('AOT: a component spread {...x} must resolve to an object');
|
|
63
|
+
}
|
|
64
|
+
for (const [propName, propValue] of Object.entries(spreadObject)) {
|
|
65
|
+
props[propName] = { static: true, value: propValue };
|
|
66
|
+
}
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
// `key` is React's, not the component's.
|
|
70
|
+
if (attr.type !== 'JSXAttribute' ||
|
|
71
|
+
attr.name.name === 'key') {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
// A callback prop (inline arrow, caller's useCallback or forwarded prop) is an ` fn `, resolved at its handler.
|
|
75
|
+
const node = attrExpr(attr);
|
|
76
|
+
if (isFn(node)) {
|
|
77
|
+
props[attr.name.name] = { fn: true, node };
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (node.type === 'Identifier' && env.callbacks?.has(node.name)) {
|
|
81
|
+
props[attr.name.name] = {
|
|
82
|
+
fn: true,
|
|
83
|
+
node: env.callbacks.get(node.name),
|
|
84
|
+
};
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (node.type === 'Identifier' && env.fnProps?.has(node.name)) {
|
|
88
|
+
props[attr.name.name] = {
|
|
89
|
+
fn: true,
|
|
90
|
+
...env.fnProps.get(node.name),
|
|
91
|
+
}; // forward original {node, env, state}
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
// Any other prop folds to a constant when it can, else becomes a runtime C expression.
|
|
95
|
+
try {
|
|
96
|
+
props[attr.name.name] = {
|
|
97
|
+
static: true,
|
|
98
|
+
value: evalStatic(node, scope),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
props[attr.name.name] = {
|
|
103
|
+
static: false,
|
|
104
|
+
...emitSourcedWide(node, env),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return props;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Maps a component's parameter to its prop descriptors (handles destructure rename and defaults).
|
|
112
|
+
*
|
|
113
|
+
* @param fn The component.
|
|
114
|
+
* @param props The props the instance was given.
|
|
115
|
+
*
|
|
116
|
+
* @returns The descriptor each name in the component's body is bound to.
|
|
117
|
+
*/
|
|
118
|
+
function bindParams(fn, props) {
|
|
119
|
+
const bindings = new Map();
|
|
120
|
+
const param = fn.params[0];
|
|
121
|
+
if (!param)
|
|
122
|
+
return bindings;
|
|
123
|
+
// `function C(props)`: the whole props object is one constant, so every prop must fold.
|
|
124
|
+
if (param.type === 'Identifier') {
|
|
125
|
+
const propsObject = {};
|
|
126
|
+
for (const [propName, descriptor] of Object.entries(props)) {
|
|
127
|
+
if (!descriptor.static) {
|
|
128
|
+
throw new Error('AOT: dynamic props require a destructured component parameter (e.g. `function C({ x })`)');
|
|
129
|
+
}
|
|
130
|
+
propsObject[propName] = descriptor.value;
|
|
131
|
+
}
|
|
132
|
+
bindings.set(param.name, { static: true, value: propsObject });
|
|
133
|
+
}
|
|
134
|
+
else if (param.type === 'ObjectPattern') {
|
|
135
|
+
// `function C({x, y: renamed, z = 1})`: bind each name to its prop, else to its folded default.
|
|
136
|
+
for (const property of param.properties) {
|
|
137
|
+
if (property.type === 'RestElement') {
|
|
138
|
+
throw new Error('AOT: rest props (...rest) in a component param not supported');
|
|
139
|
+
}
|
|
140
|
+
// Resolve a destructured prop's source name, local binding name, and optional default value descriptor.
|
|
141
|
+
const propName = property.key.name ??
|
|
142
|
+
property.key.value;
|
|
143
|
+
const bindName = property.value?.type === 'Identifier'
|
|
144
|
+
? property.value.name
|
|
145
|
+
: property.value?.type === 'AssignmentPattern'
|
|
146
|
+
? property.value.left.name
|
|
147
|
+
: propName;
|
|
148
|
+
let descriptor = props[propName];
|
|
149
|
+
if (!descriptor && property.value?.type === 'AssignmentPattern') {
|
|
150
|
+
descriptor = {
|
|
151
|
+
static: true,
|
|
152
|
+
value: evalStatic(property.value.right, {}),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
bindings.set(bindName, descriptor ?? { static: true, value: undefined });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
throw new Error('AOT: unsupported component parameter pattern');
|
|
160
|
+
}
|
|
161
|
+
return bindings;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* True if `expr` is how a component body refers to its children (destructured {children} or props.children).
|
|
165
|
+
*
|
|
166
|
+
* @param expr A child expression.
|
|
167
|
+
* @param env The expression environment.
|
|
168
|
+
*
|
|
169
|
+
* @returns Whether it names the children slot.
|
|
170
|
+
*/
|
|
171
|
+
function isChildrenRef(expr, env) {
|
|
172
|
+
const childrenRef = env.children?.ref;
|
|
173
|
+
if (!childrenRef)
|
|
174
|
+
return false;
|
|
175
|
+
if (childrenRef.kind === 'local') {
|
|
176
|
+
return expr.type === 'Identifier' && expr.name === childrenRef.name;
|
|
177
|
+
}
|
|
178
|
+
return (expr.type === 'MemberExpression' &&
|
|
179
|
+
!expr.computed &&
|
|
180
|
+
expr.object.type === 'Identifier' &&
|
|
181
|
+
expr.object.name === childrenRef.name &&
|
|
182
|
+
expr.property.name === 'children');
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Inlines a function component instance: bind props (static → scope, dynamic → locals), emit its JSX.
|
|
186
|
+
* Children passed at the call site are captured and emitted (in the CALLER's scope) where the body uses them.
|
|
187
|
+
*
|
|
188
|
+
* @param element The component instance.
|
|
189
|
+
* @param scope Compile-time constants in reach at the call site.
|
|
190
|
+
* @param out Everything emitted so far.
|
|
191
|
+
* @param env The caller's expression environment.
|
|
192
|
+
* @param state The caller's state table.
|
|
193
|
+
* @param opts How the instance is placed.
|
|
194
|
+
*
|
|
195
|
+
* @returns The instance, wrapping the element the component's JSX lowered to.
|
|
196
|
+
*/
|
|
197
|
+
export function emitComponent(element, scope, out, env, state, opts) {
|
|
198
|
+
// Look up the component and keep the call-site children that render something.
|
|
199
|
+
const componentName = element.openingElement.name.name;
|
|
200
|
+
const fn = out.components.get(componentName);
|
|
201
|
+
const childNodes = inlineFragments(element.children).filter(child => !(child.type === 'JSXText' && !child.value.trim()) &&
|
|
202
|
+
!(child.type === 'JSXExpressionContainer' &&
|
|
203
|
+
child.expression.type === 'JSXEmptyExpression'));
|
|
204
|
+
// How the body refers to children: destructured `{ children }` (a local) or whole `props` → props.children.
|
|
205
|
+
const param = fn.params[0];
|
|
206
|
+
let childrenRef = null;
|
|
207
|
+
if (param?.type === 'ObjectPattern') {
|
|
208
|
+
for (const property of param.properties) {
|
|
209
|
+
if ((property.key?.name ??
|
|
210
|
+
property.key?.value) === 'children') {
|
|
211
|
+
childrenRef = {
|
|
212
|
+
kind: 'local',
|
|
213
|
+
name: property.value?.name ?? 'children',
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else if (param?.type === 'Identifier') {
|
|
219
|
+
childrenRef = { kind: 'props', name: param.name };
|
|
220
|
+
}
|
|
221
|
+
// A child sees only module scope: start from module consts with no locals, so caller bindings cannot leak in.
|
|
222
|
+
const childScope = { ...(env.moduleConsts ?? scope) };
|
|
223
|
+
const childLocals = new Map();
|
|
224
|
+
// Callback props resolve to the caller's function, env and state; inherit the caller's own so they forward.
|
|
225
|
+
const fnProps = new Map(env.fnProps);
|
|
226
|
+
// Bind each prop: a callback to fnProps, a constant to the child's scope, a runtime value to its locals.
|
|
227
|
+
for (const [name, descriptor] of bindParams(fn, extractProps(element.openingElement, scope, env))) {
|
|
228
|
+
// The children binding comes from the slot, not from a value prop.
|
|
229
|
+
if (childrenRef?.kind === 'local' && name === childrenRef?.name)
|
|
230
|
+
continue;
|
|
231
|
+
if (descriptor.fn) {
|
|
232
|
+
fnProps.set(name, {
|
|
233
|
+
node: descriptor.node,
|
|
234
|
+
env: descriptor.env ?? env,
|
|
235
|
+
state: descriptor.state ?? state,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
else if (descriptor.static) {
|
|
239
|
+
childScope[name] = descriptor.value;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
childLocals.set(name, {
|
|
243
|
+
code: descriptor.code,
|
|
244
|
+
cType: descriptor.cType,
|
|
245
|
+
struct: descriptor.struct,
|
|
246
|
+
isBool: descriptor.isBool, // keep boolean-ness across the prop boundary (extractProps supplies it)
|
|
247
|
+
lit: descriptor.lit,
|
|
248
|
+
wide: descriptor.wide,
|
|
249
|
+
source: descriptor.source,
|
|
250
|
+
});
|
|
251
|
+
// A dynamic prop is a runtime binding; drop any same-named module const so no fold emits it instead.
|
|
252
|
+
delete childScope[name];
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
// Capture the call-site children with the caller's scope and env, for wherever the body renders them.
|
|
256
|
+
const children = {
|
|
257
|
+
nodes: childNodes,
|
|
258
|
+
scope,
|
|
259
|
+
env,
|
|
260
|
+
ref: childrenRef,
|
|
261
|
+
};
|
|
262
|
+
// Each inlined instance namespaces its own hooks under a unique `c<N>_` prefix so instances stay independent.
|
|
263
|
+
const prefix = `c${out.instN++}_`;
|
|
264
|
+
// Fold the child body's static consts as lowerApp does for App; they shadow same-named module consts.
|
|
265
|
+
if (fn.body.type === 'BlockStatement') {
|
|
266
|
+
// As in App: the body's own names, hook bindings included, shadow module ones before anything folds.
|
|
267
|
+
for (const name of declaredNames(fn.body.body)) {
|
|
268
|
+
delete childScope[name];
|
|
269
|
+
}
|
|
270
|
+
for (const stmt of fn.body.body) {
|
|
271
|
+
if (stmt.type !== 'VariableDeclaration' || stmt.kind !== 'const') {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
for (const decl of stmt.declarations) {
|
|
275
|
+
if (decl.id.type !== 'Identifier' || !decl.init)
|
|
276
|
+
continue;
|
|
277
|
+
if (childLocals.has(decl.id.name))
|
|
278
|
+
continue; // a dynamic prop of that name is already bound
|
|
279
|
+
try {
|
|
280
|
+
childScope[decl.id.name] = evalStatic(decl.init, withUndefined(childScope));
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
// A dynamic const hides any same-named module const; a memo re-binds it below, else it stays unresolved.
|
|
284
|
+
delete childScope[decl.id.name];
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// Collect the child's own hooks under its prefix; its state slots are runtime bindings, so they leave the scope.
|
|
290
|
+
const childAnims = collectAnims(fn.body, childScope, prefix);
|
|
291
|
+
const childRefs = collectRefs(fn.body, childScope, prefix, env.wide);
|
|
292
|
+
const childPans = collectPanResponders(fn.body, prefix);
|
|
293
|
+
const childCallbacks = collectCallbacks(fn.body);
|
|
294
|
+
const childMemos = collectMemos(fn.body);
|
|
295
|
+
let childState = state;
|
|
296
|
+
if (usesState(fn)) {
|
|
297
|
+
childState = collectState(fn.body, childScope, env.caps, prefix, env.wide);
|
|
298
|
+
out.childStateRecords.push(...childState.byName.values());
|
|
299
|
+
for (const name of childState.byName.keys()) {
|
|
300
|
+
delete childScope[name];
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// The child's own refs and animated values are runtime bindings too (see lowerApp).
|
|
304
|
+
for (const name of [...childAnims.keys(), ...childRefs.keys()]) {
|
|
305
|
+
delete childScope[name];
|
|
306
|
+
}
|
|
307
|
+
out.childRefs.push(...childRefs.values());
|
|
308
|
+
out.childAnimations.push(...childAnims.values());
|
|
309
|
+
// The child's expression environment: the caller's, with every name-keyed table replaced by the child's.
|
|
310
|
+
const childEnv = {
|
|
311
|
+
...env,
|
|
312
|
+
consts: childScope,
|
|
313
|
+
locals: childLocals,
|
|
314
|
+
children,
|
|
315
|
+
fnProps,
|
|
316
|
+
state: childState.byName,
|
|
317
|
+
animations: childAnims,
|
|
318
|
+
refs: childRefs,
|
|
319
|
+
pans: childPans,
|
|
320
|
+
callbacks: childCallbacks,
|
|
321
|
+
helpers: collectHelpers(fn.body, out.program),
|
|
322
|
+
cbPrefix: prefix,
|
|
323
|
+
};
|
|
324
|
+
// Resolve memos in order (fold, else a local C expr), then compile its effects, all in the child's env/state.
|
|
325
|
+
for (const [name, expr] of childMemos) {
|
|
326
|
+
try {
|
|
327
|
+
childScope[name] = evalStatic(expr, childScope);
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
const cExpr = emitSourcedWide(expr, childEnv);
|
|
331
|
+
childLocals.set(name, {
|
|
332
|
+
code: `(${cExpr.code})`,
|
|
333
|
+
cType: cExpr.cType,
|
|
334
|
+
isBool: cExpr.isBool,
|
|
335
|
+
lit: cExpr.lit,
|
|
336
|
+
wide: cExpr.wide,
|
|
337
|
+
source: cExpr.source,
|
|
338
|
+
});
|
|
339
|
+
delete childScope[name]; // a runtime binding beats a module const of its name in every fold
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
for (const effect of collectEffects(fn.body)) {
|
|
343
|
+
lowerEffect(effect, childEnv, childState, out);
|
|
344
|
+
}
|
|
345
|
+
// Emit the JSX the component returns, in the child's own scope.
|
|
346
|
+
const root = emitNode(componentReturnJSX(fn, childScope), childScope, out, childEnv, childState, opts);
|
|
347
|
+
return { kind: 'component', id: root.id, name: componentName, prefix, root };
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Emits an element / component child. opts.show toggles its display.
|
|
351
|
+
*
|
|
352
|
+
* @param node The child; anything but a JSX element is refused.
|
|
353
|
+
* @param scope Compile-time constants in reach.
|
|
354
|
+
* @param out Everything emitted so far.
|
|
355
|
+
* @param env The expression environment.
|
|
356
|
+
* @param state The component's state table.
|
|
357
|
+
* @param opts How the child is placed.
|
|
358
|
+
*
|
|
359
|
+
* @returns The child.
|
|
360
|
+
*/
|
|
361
|
+
function emitElementInto(node, scope, out, env, state, opts) {
|
|
362
|
+
if (node.type !== 'JSXElement') {
|
|
363
|
+
throw new Error(`AOT: expected a JSX element here, got ${node.type}`);
|
|
364
|
+
}
|
|
365
|
+
return emitNode(node, scope, out, env, state, opts);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Unrolls `arr.map((item, i) => <JSX/>)` over a COMPILE-TIME-CONSTANT array.
|
|
369
|
+
*
|
|
370
|
+
* @param call The `.map(…)` call.
|
|
371
|
+
* @param scope Compile-time constants in reach.
|
|
372
|
+
* @param out Everything emitted so far.
|
|
373
|
+
* @param env The expression environment.
|
|
374
|
+
* @param state The component's state table.
|
|
375
|
+
*
|
|
376
|
+
* @returns The rows.
|
|
377
|
+
*/
|
|
378
|
+
function emitMap(call, scope, out, env, state) {
|
|
379
|
+
// The array must fold at compile time and the callback must be inline.
|
|
380
|
+
let items;
|
|
381
|
+
try {
|
|
382
|
+
items = evalStatic(call.callee.object, scope);
|
|
383
|
+
}
|
|
384
|
+
catch {
|
|
385
|
+
throw new Error('AOT: .map target must be a compile-time-constant array (dynamic lists not yet supported)');
|
|
386
|
+
}
|
|
387
|
+
if (!Array.isArray(items)) {
|
|
388
|
+
throw new Error('AOT: .map target did not resolve to an array');
|
|
389
|
+
}
|
|
390
|
+
const mapFn = call.arguments[0];
|
|
391
|
+
if (!isFn(mapFn)) {
|
|
392
|
+
throw new Error('AOT: .map argument must be an inline function');
|
|
393
|
+
}
|
|
394
|
+
const itemName = mapFn.params[0]?.name;
|
|
395
|
+
const indexName = mapFn.params[1]?.name;
|
|
396
|
+
const rowJSX = componentReturnJSX(mapFn);
|
|
397
|
+
// Emit one row per element, with its item and index bound as compile-time constants.
|
|
398
|
+
return items.map((item, index) => {
|
|
399
|
+
const rowScope = { ...scope };
|
|
400
|
+
if (itemName) {
|
|
401
|
+
rowScope[itemName] = item;
|
|
402
|
+
}
|
|
403
|
+
if (indexName) {
|
|
404
|
+
rowScope[indexName] = index;
|
|
405
|
+
}
|
|
406
|
+
return emitElementInto(rowJSX, rowScope, out, rowEnv(env, [itemName, indexName], rowScope), state);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* The env for one `.map` row. Its callback params (item, index) shadow every outer binding of the same
|
|
411
|
+
* name — a JS arrow parameter always does — so those names leave every name-keyed map (state, locals,
|
|
412
|
+
* callbacks, helpers, …), and the row's own binding wins: in `consts`, or in `ownLocals` for a pooled
|
|
413
|
+
* row's struct item.
|
|
414
|
+
*
|
|
415
|
+
* @param env The expression environment around the `.map`.
|
|
416
|
+
* @param params The callback's parameter names (item, index), where present.
|
|
417
|
+
* @param consts The row's compile-time scope.
|
|
418
|
+
* @param ownLocals The row's runtime bindings, when its item is one (a pooled row).
|
|
419
|
+
*
|
|
420
|
+
* @returns The row's environment.
|
|
421
|
+
*/
|
|
422
|
+
function rowEnv(env, params, consts, ownLocals = null) {
|
|
423
|
+
// Copy a table without the row's names; one that binds none of them is shared as is.
|
|
424
|
+
const names = params.filter(Boolean);
|
|
425
|
+
const drop = (bindings) => {
|
|
426
|
+
if (!bindings || !names.some(name => bindings.has(name)))
|
|
427
|
+
return bindings;
|
|
428
|
+
const copy = new Map(bindings);
|
|
429
|
+
for (const name of names) {
|
|
430
|
+
copy.delete(name);
|
|
431
|
+
}
|
|
432
|
+
return copy;
|
|
433
|
+
};
|
|
434
|
+
// A row param named like the children reference (`children` or `props`) shadows it, leaving no children slot.
|
|
435
|
+
return {
|
|
436
|
+
...env,
|
|
437
|
+
consts,
|
|
438
|
+
state: drop(env.state),
|
|
439
|
+
refs: drop(env.refs),
|
|
440
|
+
animations: drop(env.animations),
|
|
441
|
+
locals: ownLocals ?? drop(env.locals),
|
|
442
|
+
callbacks: drop(env.callbacks),
|
|
443
|
+
fnProps: drop(env.fnProps),
|
|
444
|
+
pans: drop(env.pans),
|
|
445
|
+
helpers: drop(env.helpers),
|
|
446
|
+
svgImports: drop(env.svgImports),
|
|
447
|
+
children: names.includes(env.children?.ref?.name)
|
|
448
|
+
? null
|
|
449
|
+
: env.children,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* `{listState.map((item, i) => <Row/>)}` over a STATE array of variable length. Pre-allocates a fixed
|
|
454
|
+
* pool of `cap` rows (no runtime malloc); the row at index `row` binds `item` to `s_<name>[row]` (a struct
|
|
455
|
+
* local) and is shown only while `row < count` (display toggle). app_update then drives every row's content
|
|
456
|
+
* and show.
|
|
457
|
+
*
|
|
458
|
+
* @param call The `.map(…)` call.
|
|
459
|
+
* @param listState The list state it maps over.
|
|
460
|
+
* @param scope Compile-time constants in reach.
|
|
461
|
+
* @param out Everything emitted so far.
|
|
462
|
+
* @param env The expression environment.
|
|
463
|
+
* @param state The component's state table.
|
|
464
|
+
*
|
|
465
|
+
* @returns The pool.
|
|
466
|
+
*/
|
|
467
|
+
function emitDynamicMap(call, listState, scope, out, env, state) {
|
|
468
|
+
const mapFn = call.arguments[0];
|
|
469
|
+
if (!isFn(mapFn)) {
|
|
470
|
+
throw new Error('AOT: .map argument must be an inline function');
|
|
471
|
+
}
|
|
472
|
+
const itemName = mapFn.params[0]?.name;
|
|
473
|
+
const indexName = mapFn.params[1]?.name;
|
|
474
|
+
const rowJSX = componentReturnJSX(mapFn);
|
|
475
|
+
// Build every row of the pool; each row's display is tied to the list's current count.
|
|
476
|
+
const rows = [];
|
|
477
|
+
for (let rowIndex = 0; rowIndex < listState.cap; rowIndex++) {
|
|
478
|
+
// The item is a runtime struct slot, so drop any same-named const from the row scope, as emitComponent does.
|
|
479
|
+
const rowScope = { ...scope };
|
|
480
|
+
if (itemName) {
|
|
481
|
+
delete rowScope[itemName];
|
|
482
|
+
}
|
|
483
|
+
if (indexName) {
|
|
484
|
+
rowScope[indexName] = rowIndex; // the index is a compile-time literal per pooled row
|
|
485
|
+
}
|
|
486
|
+
const rowLocals = new Map(env.locals);
|
|
487
|
+
if (indexName) {
|
|
488
|
+
rowLocals.delete(indexName); // the index is a per-row literal in rowScope
|
|
489
|
+
}
|
|
490
|
+
// A row's item has no C type of its own: only its fields are read, through `struct`.
|
|
491
|
+
if (itemName) {
|
|
492
|
+
rowLocals.set(itemName, {
|
|
493
|
+
code: `${listState.arrayName}[${rowIndex}]`,
|
|
494
|
+
struct: listState.struct,
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
rows.push(emitElementInto(rowJSX, rowScope, out, rowEnv(env, [itemName, indexName], rowScope, rowLocals), state, { show: { kind: 'rowInUse', row: rowIndex, list: listState } }));
|
|
498
|
+
}
|
|
499
|
+
return { kind: 'pool', list: listState, rows };
|
|
500
|
+
}
|
|
501
|
+
/** The value types whose falsy value, 0, React renders as text. */
|
|
502
|
+
const NUMERIC_KINDS = new Set(['int', 'float', 'int64']);
|
|
503
|
+
/**
|
|
504
|
+
* Whether a folded child value renders nothing, as React treats it: false, null, undefined and the empty string.
|
|
505
|
+
*
|
|
506
|
+
* @param value The folded value.
|
|
507
|
+
*
|
|
508
|
+
* @returns Whether it renders nothing.
|
|
509
|
+
*/
|
|
510
|
+
const rendersNothing = (value) => value === false || value == null || value === '';
|
|
511
|
+
/**
|
|
512
|
+
* The error for a child expression that is not lowered; `what` describes it when it folded to a value.
|
|
513
|
+
*
|
|
514
|
+
* @param expr The child expression.
|
|
515
|
+
* @param what Its folded value, described, or null when it does not fold.
|
|
516
|
+
*
|
|
517
|
+
* @returns The error, for the caller to throw.
|
|
518
|
+
*/
|
|
519
|
+
function unsupportedChild(expr, what) {
|
|
520
|
+
const error = what == null
|
|
521
|
+
? aotError(`AOT: unsupported expression child "${expr.type}" in a container`, 'a child expression must be a JSX element, `cond && <El/>` / a ternary of elements, ' +
|
|
522
|
+
"or `list.map(item => <El/>)`. A bare variable holding JSX isn't inlined — write the " +
|
|
523
|
+
'element directly. (If this is a responsive Flow-A-only branch, compile at the target ' +
|
|
524
|
+
'board size via ER_AOT_SCREEN_W/H so the AOT folds the supported branch.)')
|
|
525
|
+
: aotError(`AOT: a non-element expression child (${what}) cannot render here`, 'only JSX elements render as children; wrap text in a <Text>{…}</Text>.');
|
|
526
|
+
if (expr.loc) {
|
|
527
|
+
error.aotLoc = expr.loc.start;
|
|
528
|
+
}
|
|
529
|
+
return error;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Lowers one branch of a conditional child: an element, a nested `&&` or ternary, or something that renders
|
|
533
|
+
* nothing. A condition that folds picks its branch now; a state-driven one builds every branch and shows each
|
|
534
|
+
* only while it is chosen, `within` the branch it sits in.
|
|
535
|
+
*
|
|
536
|
+
* @param expr The branch.
|
|
537
|
+
* @param scope Compile-time constants in reach.
|
|
538
|
+
* @param out Everything emitted so far.
|
|
539
|
+
* @param env The expression environment.
|
|
540
|
+
* @param state The component's state table.
|
|
541
|
+
* @param within When the branch it sits in shows, or null at the top.
|
|
542
|
+
*
|
|
543
|
+
* @returns The branch.
|
|
544
|
+
*/
|
|
545
|
+
function lowerBranch(expr, scope, out, env, state, within) {
|
|
546
|
+
if (expr.type === 'JSXElement') {
|
|
547
|
+
return emitElementInto(expr, scope, out, env, state, within ? { show: within } : {});
|
|
548
|
+
}
|
|
549
|
+
const isAnd = expr.type === 'LogicalExpression' && expr.operator === '&&';
|
|
550
|
+
if (isAnd || expr.type === 'ConditionalExpression') {
|
|
551
|
+
const testNode = isAnd ? expr.left : expr.test;
|
|
552
|
+
const whenTrue = isAnd
|
|
553
|
+
? expr.right
|
|
554
|
+
: expr.consequent;
|
|
555
|
+
const whenFalse = isAnd
|
|
556
|
+
? null
|
|
557
|
+
: expr.alternate;
|
|
558
|
+
// A test that folds picks its branch at build time; `&&` with a false test renders nothing.
|
|
559
|
+
let folded;
|
|
560
|
+
try {
|
|
561
|
+
folded = { value: evalStatic(testNode, scope) };
|
|
562
|
+
}
|
|
563
|
+
catch {
|
|
564
|
+
folded = null;
|
|
565
|
+
}
|
|
566
|
+
if (folded) {
|
|
567
|
+
// A false `&&` yields its left value, which must be one React renders as nothing (`0` is text).
|
|
568
|
+
if (isAnd && !folded.value && !rendersNothing(folded.value)) {
|
|
569
|
+
throw unsupportedChild(expr, JSON.stringify(folded.value) ?? String(folded.value));
|
|
570
|
+
}
|
|
571
|
+
const chosen = folded.value ? whenTrue : whenFalse;
|
|
572
|
+
return chosen
|
|
573
|
+
? lowerBranch(chosen, scope, out, env, state, within)
|
|
574
|
+
: null;
|
|
575
|
+
}
|
|
576
|
+
// A state-driven test builds every branch; each shows while the test picks it.
|
|
577
|
+
const test = lowerExpr(testNode, env);
|
|
578
|
+
if (isAnd && NUMERIC_KINDS.has(test.type.kind)) {
|
|
579
|
+
const error = aotError('AOT: a number on the left of `&&` renders 0 as text when it is 0', 'React draws the 0 itself, and text needs a <Text>. Test it as a condition: `count > 0 && <El/>`.');
|
|
580
|
+
if (testNode.loc) {
|
|
581
|
+
error.aotLoc = testNode.loc.start;
|
|
582
|
+
}
|
|
583
|
+
throw error;
|
|
584
|
+
}
|
|
585
|
+
const shown = (negate) => ({
|
|
586
|
+
kind: 'condition',
|
|
587
|
+
test,
|
|
588
|
+
negate,
|
|
589
|
+
within,
|
|
590
|
+
});
|
|
591
|
+
return {
|
|
592
|
+
kind: 'conditional',
|
|
593
|
+
test,
|
|
594
|
+
whenTrue: lowerBranch(whenTrue, scope, out, env, state, shown(false)),
|
|
595
|
+
whenFalse: whenFalse
|
|
596
|
+
? lowerBranch(whenFalse, scope, out, env, state, shown(true))
|
|
597
|
+
: null,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
// Anything else must be a constant that renders nothing.
|
|
601
|
+
let value;
|
|
602
|
+
try {
|
|
603
|
+
value = evalStatic(expr, withUndefined(scope));
|
|
604
|
+
}
|
|
605
|
+
catch {
|
|
606
|
+
throw unsupportedChild(expr, null);
|
|
607
|
+
}
|
|
608
|
+
if (!rendersNothing(value)) {
|
|
609
|
+
throw unsupportedChild(expr, JSON.stringify(value) ?? String(value));
|
|
610
|
+
}
|
|
611
|
+
return null;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Emits the children of a container node. Everything unrolls at compile time into a fixed set of nodes:
|
|
615
|
+
* fragments are inlined, a state-driven condition builds its elements and toggles their display, and a
|
|
616
|
+
* `.map` over list state builds a fixed pool of rows. Every child is either emitted or refused with an
|
|
617
|
+
* "AOT: …" error: one that would change the node count at runtime, text outside a <Text>, or anything else
|
|
618
|
+
* the engine has no node for.
|
|
619
|
+
*
|
|
620
|
+
* @param children The container's children.
|
|
621
|
+
* @param scope Compile-time constants in reach.
|
|
622
|
+
* @param out Everything emitted so far.
|
|
623
|
+
* @param env The expression environment.
|
|
624
|
+
* @param state The component's state table.
|
|
625
|
+
*
|
|
626
|
+
* @returns The container's children.
|
|
627
|
+
*/
|
|
628
|
+
export function emitChildren(children, scope, out, env, state) {
|
|
629
|
+
const lowered = [];
|
|
630
|
+
const element = (built) => lowered.push({ kind: 'element', element: built });
|
|
631
|
+
for (const child of inlineFragments(children)) {
|
|
632
|
+
// An element child is emitted as it is; an {expression} child is lowered by its shape below.
|
|
633
|
+
if (child.type === 'JSXElement') {
|
|
634
|
+
element(emitElementInto(child, scope, out, env, state));
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
// Text needs a <Text>; the whitespace between tags is not a child.
|
|
638
|
+
if (child.type === 'JSXText') {
|
|
639
|
+
if (child.value.trim()) {
|
|
640
|
+
const error = aotError(`AOT: text ${JSON.stringify(child.value.trim())} must be inside a <Text>`, 'only a <Text> draws text: <Text>…</Text>.');
|
|
641
|
+
if (child.loc) {
|
|
642
|
+
error.aotLoc = child.loc.start;
|
|
643
|
+
}
|
|
644
|
+
throw error;
|
|
645
|
+
}
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
if (child.type === 'JSXSpreadChild') {
|
|
649
|
+
throw unsupportedChild(child, null);
|
|
650
|
+
}
|
|
651
|
+
const expr = child.expression;
|
|
652
|
+
if (expr.type === 'JSXEmptyExpression')
|
|
653
|
+
continue;
|
|
654
|
+
if (isChildrenRef(expr, env)) {
|
|
655
|
+
// {children} / {props.children}: emit the captured call-site children, in the caller's scope/env.
|
|
656
|
+
lowered.push(...emitChildren(env.children.nodes, env.children.scope, out, env.children.env, state));
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
if (expr.type === 'CallExpression' &&
|
|
660
|
+
expr.callee.type === 'MemberExpression' &&
|
|
661
|
+
expr.callee.property.name === 'map') {
|
|
662
|
+
// `{items.map(item => <X/>)}`: a pool of rows over list state, else unrolled over a constant array.
|
|
663
|
+
const mapTarget = expr.callee.object;
|
|
664
|
+
const stateRecord = mapTarget.type === 'Identifier' ? env.state.get(mapTarget.name) : null;
|
|
665
|
+
if (stateRecord?.kind === 'list') {
|
|
666
|
+
lowered.push(emitDynamicMap(expr, stateRecord, scope, out, env, state));
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
emitMap(expr, scope, out, env, state).forEach(element);
|
|
670
|
+
}
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
// An element, `cond && <X/>`, a ternary of them (nested too), or a constant that renders nothing.
|
|
674
|
+
const branch = lowerBranch(expr, scope, out, env, state, null);
|
|
675
|
+
if (branch?.kind === 'conditional') {
|
|
676
|
+
lowered.push(branch);
|
|
677
|
+
}
|
|
678
|
+
else if (branch) {
|
|
679
|
+
element(branch);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
return lowered;
|
|
683
|
+
}
|