embedded-react 0.19.0 → 0.21.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 +5 -0
- package/README.md +8 -3
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +180 -90
- package/aot/compiler/animations.mjs +568 -317
- package/aot/compiler/boot.mjs +97 -35
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +70 -18
- package/aot/compiler/collect.mjs +787 -288
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +439 -268
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +52 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +61 -26
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +1685 -392
- package/aot/compiler/handlers.mjs +2029 -547
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +43 -4
- package/aot/compiler/ir/reads.mjs +223 -22
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +26 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +246 -111
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +195 -172
- package/aot/compiler/options.mjs +37 -31
- package/aot/compiler/out.mjs +32 -23
- package/aot/compiler/package/animations.mjs +779 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +175 -0
- package/aot/compiler/package/bytecode.mjs +315 -0
- package/aot/compiler/package/checks.mjs +1026 -0
- package/aot/compiler/package/codegen.mjs +2168 -0
- package/aot/compiler/package/enums.mjs +148 -0
- package/aot/compiler/package/format.mjs +771 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +2103 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +127 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +686 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +23 -21
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/static-eval.mjs +340 -30
- package/aot/compiler/style-text.mjs +178 -174
- package/aot/compiler/svg.mjs +480 -251
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +320 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +152 -92
- 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 +11 -7
- package/assets/emit-c.mjs +77 -58
- package/assets/emit-container.mjs +123 -37
- 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 +23 -13
- package/cli.mjs +402 -172
- package/compile-bin.mjs +3 -3
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +62 -64
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +50 -37
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +37 -21
- package/package.json +7 -8
- 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 +113 -94
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Dimensions.js +37 -0
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +3 -3
- 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 +7 -9
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +88 -8
- package/src/embedded-react/index.js +2 -0
- 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/nativeModule.js +462 -0
- 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
|
@@ -20,42 +20,47 @@ import { aotError } from './diagnostics.mjs';
|
|
|
20
20
|
import { foldBoot, isBoot } from './boot.mjs';
|
|
21
21
|
import { mergeBranchElements } from './branch-merge.mjs';
|
|
22
22
|
import { evalStatic, setBinding, withUndefined } from './static-eval.mjs';
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
23
|
+
import { holdSame, shapeOf } from './typing.mjs';
|
|
24
|
+
import { bindBuiltValues, foldRuntime, listItemOf, lowerExpr, lowerLocal, lowerSourcedWide, } from './expressions.mjs';
|
|
25
|
+
import { BOOL, INT, STRUCT } from './ir/types.mjs';
|
|
26
|
+
import { isFn, bindFoldedPattern, declaredNames, collectState, componentReturnJSX, collectHelpers, collectCallbacks, collectMemos, collectEffects, collectNativeEvents, usesState, bindConst, settleBootInitials, } from './collect.mjs';
|
|
25
27
|
import { collectAnims, collectRefs } from './animations.mjs';
|
|
26
28
|
import { attrExpr } from './style-text.mjs';
|
|
27
29
|
import { inlineFragments } from './elements/props.mjs';
|
|
28
|
-
import { lowerEffect } from './handlers.mjs';
|
|
30
|
+
import { lowerEffect, lowerNativeEvent } from './handlers.mjs';
|
|
31
|
+
import { bindDerivedList, derivedListOf, renderBindings, rowItemOf, } from './list-order.mjs';
|
|
29
32
|
import { collectPanResponders, panSpreadTarget } from './pan-responder.mjs';
|
|
30
33
|
import { emitNode } from './nodes.mjs';
|
|
34
|
+
import { bindContextUses, providedContextOf, providedEnvironment, } from './contexts.mjs';
|
|
35
|
+
import { narrowOn, narrowed, narrowedScope } from './tagged-records.mjs';
|
|
31
36
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
32
37
|
- Implementation
|
|
33
38
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
34
39
|
/**
|
|
35
40
|
* Reads a component's props as descriptors: `{static:true,value}` (folded),
|
|
36
|
-
* `{static:false,
|
|
41
|
+
* `{static:false, slotKind, struct}` (a runtime expression — e.g., a list row's `item.field`), or
|
|
37
42
|
* `{fn:true,node}` (a callback).
|
|
38
43
|
*
|
|
39
44
|
* @param openingElement The component instance's opening tag.
|
|
40
45
|
* @param scope Compile-time constants in reach at the call site.
|
|
41
|
-
* @param
|
|
46
|
+
* @param environment The caller's expression environment.
|
|
42
47
|
*
|
|
43
48
|
* @returns The props, by name.
|
|
44
49
|
*/
|
|
45
|
-
function extractProps(openingElement, scope,
|
|
50
|
+
function extractProps(openingElement, scope, environment) {
|
|
46
51
|
const props = {};
|
|
47
|
-
for (const
|
|
52
|
+
for (const attribute of openingElement.attributes) {
|
|
48
53
|
// Static spread: {...obj} where obj folds to a compile-time object → merge its keys as props.
|
|
49
|
-
if (
|
|
54
|
+
if (attribute.type === 'JSXSpreadAttribute') {
|
|
50
55
|
// A PanResponder's handlers need a real scene node, which a component instance is not.
|
|
51
|
-
if (panSpreadTarget(
|
|
56
|
+
if (panSpreadTarget(attribute.argument, environment)) {
|
|
52
57
|
throw aotError('AOT: a PanResponder can only be spread onto a host element', 'spread `{...pan.panHandlers}` onto the <View> (or <Pressable>/<ScrollView>) that ' +
|
|
53
58
|
'should own the gesture, not onto a component instance — the AOT wires the responder ' +
|
|
54
59
|
'to a real scene node.');
|
|
55
60
|
}
|
|
56
61
|
let spreadObject;
|
|
57
62
|
try {
|
|
58
|
-
spreadObject = evalStatic(
|
|
63
|
+
spreadObject = evalStatic(attribute.argument, scope);
|
|
59
64
|
}
|
|
60
65
|
catch {
|
|
61
66
|
throw new Error('AOT: only a compile-time-constant object can be spread to a component ({...obj})');
|
|
@@ -69,75 +74,96 @@ function extractProps(openingElement, scope, env) {
|
|
|
69
74
|
continue;
|
|
70
75
|
}
|
|
71
76
|
// `key` is React's, not the component's.
|
|
72
|
-
if (
|
|
73
|
-
|
|
77
|
+
if (attribute.type !== 'JSXAttribute' ||
|
|
78
|
+
attribute.name.name === 'key') {
|
|
74
79
|
continue;
|
|
75
80
|
}
|
|
76
|
-
|
|
77
|
-
const node = attrExpr(attr);
|
|
78
|
-
if (isFn(node)) {
|
|
79
|
-
props[attr.name.name] = { fn: true, node };
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
if (node.type === 'Identifier' && env.callbacks?.has(node.name)) {
|
|
83
|
-
props[attr.name.name] = {
|
|
84
|
-
fn: true,
|
|
85
|
-
node: env.callbacks.get(node.name),
|
|
86
|
-
};
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
if (node.type === 'Identifier' && env.fnProps?.has(node.name)) {
|
|
90
|
-
props[attr.name.name] = {
|
|
91
|
-
fn: true,
|
|
92
|
-
...env.fnProps.get(node.name),
|
|
93
|
-
}; // forward original {node, env, state}
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
// Any other prop folds to a constant when it can, else becomes a runtime C expression.
|
|
97
|
-
try {
|
|
98
|
-
const propName = attr.name.name;
|
|
99
|
-
props[propName] = {
|
|
100
|
-
static: true,
|
|
101
|
-
value: bindConst(propName, evalStatic(node, scope, 'boot')),
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
catch {
|
|
105
|
-
props[attr.name.name] = {
|
|
106
|
-
static: false,
|
|
107
|
-
...emitSourcedWide(node, env),
|
|
108
|
-
};
|
|
109
|
-
}
|
|
81
|
+
props[attribute.name.name] = propDescriptorOf(attribute.name.name, attrExpr(attribute), scope, environment);
|
|
110
82
|
}
|
|
111
83
|
return props;
|
|
112
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* A prop's descriptor (see extractProps): a callback, a list, a constant, or a runtime expression.
|
|
87
|
+
*
|
|
88
|
+
* @param propName The prop's name.
|
|
89
|
+
* @param node Its value.
|
|
90
|
+
* @param scope Compile-time constants in reach where the value is written.
|
|
91
|
+
* @param environment The expression environment there.
|
|
92
|
+
*
|
|
93
|
+
* @returns The descriptor.
|
|
94
|
+
*/
|
|
95
|
+
export function propDescriptorOf(propName, node, scope, environment) {
|
|
96
|
+
// A callback prop (inline arrow, caller's useCallback or forwarded prop) is an ` fn `, resolved at its handler.
|
|
97
|
+
if (isFn(node)) {
|
|
98
|
+
return { isCallback: true, node };
|
|
99
|
+
}
|
|
100
|
+
if (node.type === 'Identifier' && environment.callbacks?.has(node.name)) {
|
|
101
|
+
return { isCallback: true, node: environment.callbacks.get(node.name) };
|
|
102
|
+
}
|
|
103
|
+
// A forwarded callback prop keeps its original {node, env, state}.
|
|
104
|
+
if (node.type === 'Identifier' && environment.functionProps?.has(node.name)) {
|
|
105
|
+
return { isCallback: true, ...environment.functionProps.get(node.name) };
|
|
106
|
+
}
|
|
107
|
+
// A list state passes as it is, and so does a list filtered or sorted in render.
|
|
108
|
+
const listState = node.type === 'Identifier'
|
|
109
|
+
? environment.state.get(node.name)
|
|
110
|
+
: derivedListOf(node, environment);
|
|
111
|
+
if (listState?.kind === 'list') {
|
|
112
|
+
return { static: 'list', list: listState };
|
|
113
|
+
}
|
|
114
|
+
// Any other prop folds to a constant when it can, else becomes a runtime expression. A list's item by index
|
|
115
|
+
// passes as it is, and an array or object built from runtime values folds with those values kept as runtime
|
|
116
|
+
// reads.
|
|
117
|
+
try {
|
|
118
|
+
return {
|
|
119
|
+
static: true,
|
|
120
|
+
value: bindConst(propName, evalStatic(node, scope, 'boot')),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
const item = listItemOf(node, environment);
|
|
125
|
+
return item
|
|
126
|
+
? {
|
|
127
|
+
static: false,
|
|
128
|
+
...item,
|
|
129
|
+
source: { kind: 'local', local: item, location: node.loc?.start },
|
|
130
|
+
expression: { kind: 'local', local: item, type: STRUCT },
|
|
131
|
+
}
|
|
132
|
+
: node.type === 'ArrayExpression' || node.type === 'ObjectExpression'
|
|
133
|
+
? { static: true, value: foldRuntime(node, environment, scope) }
|
|
134
|
+
: Object.assign(lowerSourcedWide(node, environment), {
|
|
135
|
+
static: false,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
113
139
|
/**
|
|
114
140
|
* Maps a component's parameter to its prop descriptors (handles destructure rename and defaults).
|
|
115
141
|
*
|
|
116
|
-
* @param
|
|
142
|
+
* @param functionNode The component.
|
|
117
143
|
* @param props The props the instance was given.
|
|
118
144
|
* @param scope Module constants a default can refer to.
|
|
119
145
|
*
|
|
120
146
|
* @returns The descriptor each name in the component's body is bound to.
|
|
121
147
|
*/
|
|
122
|
-
function bindParams(
|
|
148
|
+
function bindParams(functionNode, props, scope) {
|
|
123
149
|
const bindings = new Map();
|
|
124
|
-
const
|
|
125
|
-
if (!
|
|
150
|
+
const parameter = functionNode.params[0];
|
|
151
|
+
if (!parameter)
|
|
126
152
|
return bindings;
|
|
127
153
|
// `function C(props)`: the whole props object is one constant, so every prop must fold.
|
|
128
|
-
if (
|
|
154
|
+
if (parameter.type === 'Identifier') {
|
|
129
155
|
const propsObject = {};
|
|
130
156
|
for (const [propName, descriptor] of Object.entries(props)) {
|
|
131
|
-
if (
|
|
157
|
+
if (descriptor.static !== true) {
|
|
132
158
|
throw new Error('AOT: dynamic props require a destructured component parameter (e.g. `function C({ x })`)');
|
|
133
159
|
}
|
|
134
160
|
propsObject[propName] = descriptor.value;
|
|
135
161
|
}
|
|
136
|
-
bindings.set(
|
|
162
|
+
bindings.set(parameter.name, { static: true, value: propsObject });
|
|
137
163
|
}
|
|
138
|
-
else if (
|
|
164
|
+
else if (parameter.type === 'ObjectPattern') {
|
|
139
165
|
// `function C({x, y: renamed, z = 1})`: bind each name to its prop, else to its folded default.
|
|
140
|
-
const properties =
|
|
166
|
+
const properties = parameter.properties.map(property => {
|
|
141
167
|
if (property.type === 'RestElement') {
|
|
142
168
|
throw new Error('AOT: rest props (...rest) in a component param not supported');
|
|
143
169
|
}
|
|
@@ -164,7 +190,7 @@ function bindParams(fn, props, scope) {
|
|
|
164
190
|
};
|
|
165
191
|
}
|
|
166
192
|
descriptor ??= { static: true, value: undefined };
|
|
167
|
-
if (descriptor.static)
|
|
193
|
+
if (descriptor.static === true)
|
|
168
194
|
setBinding(defaultScope, bindName, descriptor.value);
|
|
169
195
|
bindings.set(bindName, descriptor);
|
|
170
196
|
}
|
|
@@ -175,25 +201,66 @@ function bindParams(fn, props, scope) {
|
|
|
175
201
|
return bindings;
|
|
176
202
|
}
|
|
177
203
|
/**
|
|
178
|
-
*
|
|
204
|
+
* Binds a prop's descriptor to a name in a component's tables: a callback to its callback props, a list state to its
|
|
205
|
+
* lists, a constant to its scope, and a runtime value to its locals.
|
|
179
206
|
*
|
|
180
|
-
* @param
|
|
181
|
-
* @param
|
|
207
|
+
* @param name The name.
|
|
208
|
+
* @param descriptor The prop.
|
|
209
|
+
* @param tables The component's tables.
|
|
210
|
+
* @param environment Where a callback without one of its own is lowered.
|
|
211
|
+
* @param state The state table it is lowered with.
|
|
212
|
+
*/
|
|
213
|
+
export function bindProp(name, descriptor, tables, environment, state) {
|
|
214
|
+
if (descriptor.isCallback) {
|
|
215
|
+
tables.functionProps.set(name, {
|
|
216
|
+
node: descriptor.node,
|
|
217
|
+
environment: descriptor.environment ?? environment,
|
|
218
|
+
state: descriptor.state ?? state,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
else if (descriptor.static === 'list') {
|
|
222
|
+
tables.lists.set(name, descriptor.list);
|
|
223
|
+
delete tables.scope[name];
|
|
224
|
+
}
|
|
225
|
+
else if (descriptor.static) {
|
|
226
|
+
setBinding(tables.scope, name, descriptor.value);
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
// What it holds crosses the prop boundary, a boolean as a boolean (extractProps supplies it).
|
|
230
|
+
tables.locals.set(name, holdSame({
|
|
231
|
+
name: descriptor.name,
|
|
232
|
+
struct: descriptor.struct,
|
|
233
|
+
list: descriptor.list,
|
|
234
|
+
row: descriptor.row,
|
|
235
|
+
index: descriptor.index,
|
|
236
|
+
guarded: descriptor.guarded,
|
|
237
|
+
source: descriptor.source,
|
|
238
|
+
expression: descriptor.expression,
|
|
239
|
+
}, descriptor));
|
|
240
|
+
// A dynamic prop is a runtime binding; drop any same-named module const so no fold emits it instead.
|
|
241
|
+
delete tables.scope[name];
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* True if `expression` is how a component body refers to its children (destructured {children} or props.children).
|
|
246
|
+
*
|
|
247
|
+
* @param expression A child expression.
|
|
248
|
+
* @param environment The expression environment.
|
|
182
249
|
*
|
|
183
250
|
* @returns Whether it names the children slot.
|
|
184
251
|
*/
|
|
185
|
-
function isChildrenRef(
|
|
186
|
-
const childrenRef =
|
|
252
|
+
function isChildrenRef(expression, environment) {
|
|
253
|
+
const childrenRef = environment.children?.ref;
|
|
187
254
|
if (!childrenRef)
|
|
188
255
|
return false;
|
|
189
256
|
if (childrenRef.kind === 'local') {
|
|
190
|
-
return
|
|
257
|
+
return (expression.type === 'Identifier' && expression.name === childrenRef.name);
|
|
191
258
|
}
|
|
192
|
-
return (
|
|
193
|
-
!
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
259
|
+
return (expression.type === 'MemberExpression' &&
|
|
260
|
+
!expression.computed &&
|
|
261
|
+
expression.object.type === 'Identifier' &&
|
|
262
|
+
expression.object.name === childrenRef.name &&
|
|
263
|
+
expression.property.name === 'children');
|
|
197
264
|
}
|
|
198
265
|
/**
|
|
199
266
|
* Inlines a function component instance: bind props (static → scope, dynamic → locals), emit its JSX.
|
|
@@ -201,25 +268,25 @@ function isChildrenRef(expr, env) {
|
|
|
201
268
|
*
|
|
202
269
|
* @param element The component instance.
|
|
203
270
|
* @param scope Compile-time constants in reach at the call site.
|
|
204
|
-
* @param
|
|
205
|
-
* @param
|
|
271
|
+
* @param output Everything emitted so far.
|
|
272
|
+
* @param environment The caller's expression environment.
|
|
206
273
|
* @param state The caller's state table.
|
|
207
|
-
* @param
|
|
274
|
+
* @param options How the instance is placed.
|
|
208
275
|
*
|
|
209
276
|
* @returns The instance, wrapping the element the component's JSX lowered to.
|
|
210
277
|
*/
|
|
211
|
-
export function emitComponent(element, scope,
|
|
278
|
+
export function emitComponent(element, scope, output, environment, state, options) {
|
|
212
279
|
// Look up the component and keep the call-site children that render something.
|
|
213
280
|
const componentName = element.openingElement.name.name;
|
|
214
|
-
const
|
|
281
|
+
const functionNode = output.components.get(componentName);
|
|
215
282
|
const childNodes = inlineFragments(element.children).filter(child => !(child.type === 'JSXText' && !child.value.trim()) &&
|
|
216
283
|
!(child.type === 'JSXExpressionContainer' &&
|
|
217
284
|
child.expression.type === 'JSXEmptyExpression'));
|
|
218
285
|
// How the body refers to children: destructured `{ children }` (a local) or whole `props` → props.children.
|
|
219
|
-
const
|
|
286
|
+
const parameter = functionNode.params[0];
|
|
220
287
|
let childrenRef = null;
|
|
221
|
-
if (
|
|
222
|
-
for (const property of
|
|
288
|
+
if (parameter?.type === 'ObjectPattern') {
|
|
289
|
+
for (const property of parameter.properties) {
|
|
223
290
|
if ((property.key?.name ??
|
|
224
291
|
property.key?.value) === 'children') {
|
|
225
292
|
childrenRef = {
|
|
@@ -229,186 +296,244 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
229
296
|
}
|
|
230
297
|
}
|
|
231
298
|
}
|
|
232
|
-
else if (
|
|
233
|
-
childrenRef = { kind: 'props', name:
|
|
299
|
+
else if (parameter?.type === 'Identifier') {
|
|
300
|
+
childrenRef = { kind: 'props', name: parameter.name };
|
|
234
301
|
}
|
|
235
302
|
// A child sees only module scope: start from module consts with no locals, so caller bindings cannot leak in.
|
|
236
|
-
const moduleScope =
|
|
303
|
+
const moduleScope = environment.moduleConstants ?? scope;
|
|
237
304
|
const childScope = { ...moduleScope };
|
|
238
305
|
const childLocals = new Map();
|
|
239
306
|
// Callback props resolve to the caller's function, env and state; inherit the caller's own so they forward.
|
|
240
|
-
const
|
|
241
|
-
// Bind each prop: a callback to fnProps, a constant to the child's scope, a runtime value to its locals
|
|
242
|
-
|
|
307
|
+
const functionProps = new Map(environment.functionProps);
|
|
308
|
+
// Bind each prop: a callback to fnProps, a constant to the child's scope, a runtime value to its locals, and a
|
|
309
|
+
// list state to its state.
|
|
310
|
+
const listProps = new Map();
|
|
311
|
+
const tables = {
|
|
312
|
+
scope: childScope,
|
|
313
|
+
locals: childLocals,
|
|
314
|
+
functionProps,
|
|
315
|
+
lists: listProps,
|
|
316
|
+
};
|
|
317
|
+
for (const [name, descriptor] of bindParams(functionNode, extractProps(element.openingElement, scope, environment), moduleScope)) {
|
|
243
318
|
// The children binding comes from the slot, not from a value prop.
|
|
244
319
|
if (childrenRef?.kind === 'local' && name === childrenRef?.name)
|
|
245
320
|
continue;
|
|
246
|
-
|
|
247
|
-
fnProps.set(name, {
|
|
248
|
-
node: descriptor.node,
|
|
249
|
-
env: descriptor.env ?? env,
|
|
250
|
-
state: descriptor.state ?? state,
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
else if (descriptor.static) {
|
|
254
|
-
setBinding(childScope, name, descriptor.value);
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
childLocals.set(name, {
|
|
258
|
-
code: descriptor.code,
|
|
259
|
-
cType: descriptor.cType,
|
|
260
|
-
struct: descriptor.struct,
|
|
261
|
-
isBool: descriptor.isBool, // keep boolean-ness across the prop boundary (extractProps supplies it)
|
|
262
|
-
lit: descriptor.lit,
|
|
263
|
-
wide: descriptor.wide,
|
|
264
|
-
source: descriptor.source,
|
|
265
|
-
});
|
|
266
|
-
// A dynamic prop is a runtime binding; drop any same-named module const so no fold emits it instead.
|
|
267
|
-
delete childScope[name];
|
|
268
|
-
}
|
|
321
|
+
bindProp(name, descriptor, tables, environment, state);
|
|
269
322
|
}
|
|
270
323
|
// Capture the call-site children with the caller's scope and env, for wherever the body renders them.
|
|
271
324
|
const children = {
|
|
272
325
|
nodes: childNodes,
|
|
273
326
|
scope,
|
|
274
|
-
|
|
327
|
+
environment,
|
|
275
328
|
ref: childrenRef,
|
|
276
329
|
};
|
|
277
330
|
// Each inlined instance namespaces its own hooks under a unique `c<N>_` prefix so instances stay independent.
|
|
278
|
-
const prefix = `c${
|
|
331
|
+
const prefix = `c${output.instanceCount++}_`;
|
|
279
332
|
// Fold the child body's static consts as lowerApp does for App; they shadow same-named module consts.
|
|
280
|
-
|
|
333
|
+
let contextNames = new Set();
|
|
334
|
+
if (functionNode.body.type === 'BlockStatement') {
|
|
281
335
|
// As in App: the body's own names, hook bindings included, shadow module ones before anything folds.
|
|
282
|
-
for (const name of declaredNames(
|
|
336
|
+
for (const name of declaredNames(functionNode.body.body)) {
|
|
283
337
|
delete childScope[name];
|
|
284
338
|
}
|
|
285
|
-
|
|
286
|
-
|
|
339
|
+
// A value read from a context binds as a prop does, from the nearest provider above the instance.
|
|
340
|
+
contextNames = bindContextUses(functionNode.body, environment, tables);
|
|
341
|
+
for (const statement of functionNode.body.body) {
|
|
342
|
+
if (statement.type !== 'VariableDeclaration' ||
|
|
343
|
+
statement.kind !== 'const') {
|
|
287
344
|
continue;
|
|
288
345
|
}
|
|
289
|
-
for (const
|
|
290
|
-
if (
|
|
346
|
+
for (const declarator of statement.declarations) {
|
|
347
|
+
if (declarator.id.type === 'ObjectPattern' && declarator.init) {
|
|
348
|
+
try {
|
|
349
|
+
bindFoldedPattern(childScope, declarator.id, declarator.init);
|
|
350
|
+
}
|
|
351
|
+
catch {
|
|
352
|
+
// Not a fold, so bindBuiltValues binds it.
|
|
353
|
+
}
|
|
291
354
|
continue;
|
|
292
|
-
|
|
355
|
+
}
|
|
356
|
+
if (declarator.id.type !== 'Identifier' || !declarator.init)
|
|
357
|
+
continue;
|
|
358
|
+
if (childLocals.has(declarator.id.name))
|
|
293
359
|
continue; // a dynamic prop of that name is already bound
|
|
360
|
+
if (contextNames.has(declarator.id.name))
|
|
361
|
+
continue;
|
|
294
362
|
try {
|
|
295
|
-
setBinding(childScope,
|
|
363
|
+
setBinding(childScope, declarator.id.name, bindConst(declarator.id.name, evalStatic(declarator.init, withUndefined(childScope), 'boot')));
|
|
296
364
|
}
|
|
297
365
|
catch {
|
|
298
366
|
// A dynamic const hides any same-named module const; a memo re-binds it below, else it stays unresolved.
|
|
299
|
-
delete childScope[
|
|
367
|
+
delete childScope[declarator.id.name];
|
|
300
368
|
}
|
|
301
369
|
}
|
|
302
370
|
}
|
|
303
371
|
}
|
|
304
372
|
// Collect the child's own hooks under its prefix; its state slots are runtime bindings, so they leave the scope.
|
|
305
|
-
const
|
|
306
|
-
const childRefs = collectRefs(
|
|
307
|
-
const childPans = collectPanResponders(
|
|
308
|
-
const childCallbacks = collectCallbacks(
|
|
309
|
-
const childMemos = collectMemos(
|
|
373
|
+
const childAnimations = collectAnims(functionNode.body, childScope, prefix);
|
|
374
|
+
const childRefs = collectRefs(functionNode.body, childScope, prefix);
|
|
375
|
+
const childPans = collectPanResponders(functionNode.body, prefix);
|
|
376
|
+
const childCallbacks = collectCallbacks(functionNode.body);
|
|
377
|
+
const childMemos = collectMemos(functionNode.body);
|
|
310
378
|
let childState = state;
|
|
311
|
-
if (usesState(
|
|
312
|
-
childState = collectState(
|
|
313
|
-
|
|
379
|
+
if (usesState(functionNode)) {
|
|
380
|
+
childState = collectState(functionNode.body, childScope, environment.caps, prefix, output.program);
|
|
381
|
+
settleBootInitials(childState.byName.values());
|
|
382
|
+
output.childStateRecords.push(...childState.byName.values());
|
|
314
383
|
for (const name of childState.byName.keys()) {
|
|
315
384
|
delete childScope[name];
|
|
316
385
|
}
|
|
317
386
|
}
|
|
318
387
|
// The child's own refs and animated values are runtime bindings too (see lowerApp).
|
|
319
|
-
for (const name of [...
|
|
388
|
+
for (const name of [...childAnimations.keys(), ...childRefs.keys()]) {
|
|
320
389
|
delete childScope[name];
|
|
321
390
|
}
|
|
322
|
-
|
|
323
|
-
|
|
391
|
+
output.childRefs.push(...childRefs.values());
|
|
392
|
+
output.childAnimations.push(...childAnimations.values());
|
|
324
393
|
// The child's expression environment: the caller's, with every name-keyed table replaced by the child's.
|
|
325
|
-
const
|
|
326
|
-
...
|
|
327
|
-
|
|
394
|
+
const childEnvironment = {
|
|
395
|
+
...environment,
|
|
396
|
+
constants: childScope,
|
|
328
397
|
locals: childLocals,
|
|
329
398
|
children,
|
|
330
|
-
|
|
331
|
-
state
|
|
332
|
-
|
|
399
|
+
functionProps,
|
|
400
|
+
// A child without state of its own reads none of its caller's by name.
|
|
401
|
+
state: new Map([
|
|
402
|
+
...(childState === state ? [] : childState.byName),
|
|
403
|
+
...listProps,
|
|
404
|
+
]),
|
|
405
|
+
animations: childAnimations,
|
|
333
406
|
refs: childRefs,
|
|
334
407
|
pans: childPans,
|
|
335
408
|
callbacks: childCallbacks,
|
|
336
|
-
helpers: collectHelpers(
|
|
337
|
-
|
|
409
|
+
helpers: collectHelpers(functionNode.body, output.program),
|
|
410
|
+
callbackPrefix: prefix,
|
|
338
411
|
};
|
|
339
|
-
// Resolve memos in order (fold, else a local
|
|
340
|
-
for (const
|
|
412
|
+
// Resolve memos in order (fold, else a local expression), then compile its effects, all in the child's env/state.
|
|
413
|
+
for (const { name, expression, isMemo } of renderBindings(functionNode.body, childMemos)) {
|
|
414
|
+
if (!isMemo) {
|
|
415
|
+
bindDerivedList(name, expression, childEnvironment);
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
341
418
|
try {
|
|
342
|
-
setBinding(childScope, name, bindConst(name, evalStatic(
|
|
419
|
+
setBinding(childScope, name, bindConst(name, evalStatic(expression, childScope, 'boot')));
|
|
343
420
|
}
|
|
344
421
|
catch {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
422
|
+
if (bindDerivedList(name, expression, childEnvironment)) {
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
if (expression.type === 'ArrayExpression' ||
|
|
426
|
+
expression.type === 'ObjectExpression') {
|
|
427
|
+
setBinding(childScope, name, foldRuntime(expression, childEnvironment));
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
childLocals.set(name, lowerLocal(expression, childEnvironment));
|
|
354
431
|
delete childScope[name]; // a runtime binding beats a module const of its name in every fold
|
|
355
432
|
}
|
|
356
433
|
}
|
|
357
|
-
|
|
358
|
-
|
|
434
|
+
if (functionNode.body.type === 'BlockStatement') {
|
|
435
|
+
bindBuiltValues(functionNode.body.body, childEnvironment);
|
|
436
|
+
}
|
|
437
|
+
for (const effect of collectEffects(functionNode.body)) {
|
|
438
|
+
lowerEffect(effect, childEnvironment, childState, output);
|
|
439
|
+
}
|
|
440
|
+
for (const call of collectNativeEvents(functionNode.body)) {
|
|
441
|
+
lowerNativeEvent(call, childEnvironment, childState, output);
|
|
359
442
|
}
|
|
360
443
|
// Emit the JSX the component returns, in the child's own scope.
|
|
361
|
-
const root = emitNode(componentReturnJSX(
|
|
444
|
+
const root = emitNode(componentReturnJSX(functionNode, childScope), childScope, output, childEnvironment, childState, options);
|
|
362
445
|
return { kind: 'component', id: root.id, name: componentName, prefix, root };
|
|
363
446
|
}
|
|
364
447
|
/**
|
|
365
|
-
*
|
|
448
|
+
* A context's provider where one element is wanted, as a component's root or a branch: the one element it wraps,
|
|
449
|
+
* emitted reading its value. It may wrap `{children}` that are one element.
|
|
450
|
+
*
|
|
451
|
+
* @param element The provider.
|
|
452
|
+
* @param context The context it provides.
|
|
453
|
+
* @param scope Compile-time constants in reach.
|
|
454
|
+
* @param output Everything emitted so far.
|
|
455
|
+
* @param environment The expression environment.
|
|
456
|
+
* @param state The component's state table.
|
|
457
|
+
* @param options How the element is placed.
|
|
458
|
+
*
|
|
459
|
+
* @returns The element.
|
|
460
|
+
*/
|
|
461
|
+
export function emitProvider(element, context, scope, output, environment, state, options) {
|
|
462
|
+
const provided = providedEnvironment(element, context, scope, environment, state);
|
|
463
|
+
const rendered = (nodes) => inlineFragments(nodes).filter(child => !(child.type === 'JSXText' && !child.value.trim()) &&
|
|
464
|
+
!(child.type === 'JSXExpressionContainer' &&
|
|
465
|
+
child.expression.type === 'JSXEmptyExpression'));
|
|
466
|
+
const children = rendered(element.children);
|
|
467
|
+
if (children.length === 1 && children[0].type === 'JSXElement') {
|
|
468
|
+
return emitNode(children[0], scope, output, provided, state, options);
|
|
469
|
+
}
|
|
470
|
+
// `{children}`, when they are one element: emitted in the caller's scope, reading this provider.
|
|
471
|
+
if (children.length === 1 &&
|
|
472
|
+
children[0].type === 'JSXExpressionContainer' &&
|
|
473
|
+
isChildrenRef(children[0].expression, environment)) {
|
|
474
|
+
const slot = environment.children;
|
|
475
|
+
const nodes = rendered(slot.nodes);
|
|
476
|
+
if (nodes.length === 1 && nodes[0].type === 'JSXElement') {
|
|
477
|
+
return emitNode(nodes[0], slot.scope, output, { ...slot.environment, providers: provided.providers }, state, options);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
throw aotError(`AOT: <${context}.Provider> must wrap one element here`, "a provider adds no node of its own, so where one element is wanted (a component's root, a branch) it must " +
|
|
481
|
+
'wrap exactly one: put its children in a <View>.');
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Emits an element / component child. options.show toggles its display.
|
|
366
485
|
*
|
|
367
486
|
* @param node The child; anything but a JSX element is refused.
|
|
368
487
|
* @param scope Compile-time constants in reach.
|
|
369
|
-
* @param
|
|
370
|
-
* @param
|
|
488
|
+
* @param output Everything emitted so far.
|
|
489
|
+
* @param environment The expression environment.
|
|
371
490
|
* @param state The component's state table.
|
|
372
|
-
* @param
|
|
491
|
+
* @param options How the child is placed.
|
|
373
492
|
*
|
|
374
493
|
* @returns The child.
|
|
375
494
|
*/
|
|
376
|
-
function emitElementInto(node, scope,
|
|
495
|
+
function emitElementInto(node, scope, output, environment, state, options) {
|
|
377
496
|
if (node.type !== 'JSXElement') {
|
|
378
497
|
throw new Error(`AOT: expected a JSX element here, got ${node.type}`);
|
|
379
498
|
}
|
|
380
|
-
return emitNode(node, scope,
|
|
499
|
+
return emitNode(node, scope, output, environment, state, options);
|
|
381
500
|
}
|
|
382
501
|
/**
|
|
383
502
|
* Unrolls `arr.map((item, i) => <JSX/>)` over a COMPILE-TIME-CONSTANT array.
|
|
384
503
|
*
|
|
385
504
|
* @param call The `.map(…)` call.
|
|
386
505
|
* @param scope Compile-time constants in reach.
|
|
387
|
-
* @param
|
|
388
|
-
* @param
|
|
506
|
+
* @param output Everything emitted so far.
|
|
507
|
+
* @param environment The expression environment.
|
|
389
508
|
* @param state The component's state table.
|
|
390
509
|
*
|
|
391
510
|
* @returns The rows.
|
|
392
511
|
*/
|
|
393
|
-
function emitMap(call, scope,
|
|
394
|
-
// The array must fold at compile time and the callback must be inline.
|
|
512
|
+
function emitMap(call, scope, output, environment, state) {
|
|
513
|
+
// The array must fold at compile time, its values worked out at run time aside, and the callback must be inline.
|
|
514
|
+
const target = call.callee.object;
|
|
395
515
|
let items;
|
|
396
|
-
|
|
397
|
-
items =
|
|
516
|
+
if (target.type === 'ArrayExpression') {
|
|
517
|
+
items = foldRuntime(target, environment, scope);
|
|
398
518
|
}
|
|
399
|
-
|
|
400
|
-
|
|
519
|
+
else {
|
|
520
|
+
try {
|
|
521
|
+
items = evalStatic(target, scope);
|
|
522
|
+
}
|
|
523
|
+
catch {
|
|
524
|
+
throw new Error('AOT: .map target must be a list state, a constant array, or an array literal built in render');
|
|
525
|
+
}
|
|
401
526
|
}
|
|
402
527
|
if (!Array.isArray(items)) {
|
|
403
528
|
throw new Error('AOT: .map target did not resolve to an array');
|
|
404
529
|
}
|
|
405
|
-
const
|
|
406
|
-
if (!isFn(
|
|
530
|
+
const mapFunction = call.arguments[0];
|
|
531
|
+
if (!isFn(mapFunction)) {
|
|
407
532
|
throw new Error('AOT: .map argument must be an inline function');
|
|
408
533
|
}
|
|
409
|
-
const itemName =
|
|
410
|
-
const indexName =
|
|
411
|
-
const rowJSX = componentReturnJSX(
|
|
534
|
+
const itemName = mapFunction.params[0]?.name;
|
|
535
|
+
const indexName = mapFunction.params[1]?.name;
|
|
536
|
+
const rowJSX = componentReturnJSX(mapFunction);
|
|
412
537
|
// Emit one row per element, with its item and index bound as compile-time constants.
|
|
413
538
|
return items.map((item, index) => {
|
|
414
539
|
const rowScope = { ...scope };
|
|
@@ -418,25 +543,25 @@ function emitMap(call, scope, out, env, state) {
|
|
|
418
543
|
if (indexName) {
|
|
419
544
|
setBinding(rowScope, indexName, index);
|
|
420
545
|
}
|
|
421
|
-
return emitElementInto(rowJSX, rowScope,
|
|
546
|
+
return emitElementInto(rowJSX, rowScope, output, rowEnv(environment, [itemName, indexName], rowScope), state);
|
|
422
547
|
});
|
|
423
548
|
}
|
|
424
549
|
/**
|
|
425
550
|
* The env for one `.map` row. Its callback params (item, index) shadow every outer binding of the same
|
|
426
551
|
* name — a JS arrow parameter always does — so those names leave every name-keyed map (state, locals,
|
|
427
|
-
* callbacks, helpers, …), and the row's own binding wins: in `
|
|
552
|
+
* callbacks, helpers, …), and the row's own binding wins: in `constants`, or in `ownLocals` for a pooled
|
|
428
553
|
* row's struct item.
|
|
429
554
|
*
|
|
430
|
-
* @param
|
|
431
|
-
* @param
|
|
432
|
-
* @param
|
|
555
|
+
* @param environment The expression environment around the `.map`.
|
|
556
|
+
* @param parameters The callback's parameter names (item, index), where present.
|
|
557
|
+
* @param constants The row's compile-time scope.
|
|
433
558
|
* @param ownLocals The row's runtime bindings, when its item is one (a pooled row).
|
|
434
559
|
*
|
|
435
560
|
* @returns The row's environment.
|
|
436
561
|
*/
|
|
437
|
-
function rowEnv(
|
|
562
|
+
export function rowEnv(environment, parameters, constants, ownLocals = null) {
|
|
438
563
|
// Copy a table without the row's names; one that binds none of them is shared as is.
|
|
439
|
-
const names =
|
|
564
|
+
const names = parameters.filter(Boolean);
|
|
440
565
|
const drop = (bindings) => {
|
|
441
566
|
if (!bindings || !names.some(name => bindings.has(name)))
|
|
442
567
|
return bindings;
|
|
@@ -448,48 +573,48 @@ function rowEnv(env, params, consts, ownLocals = null) {
|
|
|
448
573
|
};
|
|
449
574
|
// A row param named like the children reference (`children` or `props`) shadows it, leaving no children slot.
|
|
450
575
|
return {
|
|
451
|
-
...
|
|
452
|
-
|
|
453
|
-
state: drop(
|
|
454
|
-
refs: drop(
|
|
455
|
-
animations: drop(
|
|
456
|
-
locals: ownLocals ?? drop(
|
|
457
|
-
callbacks: drop(
|
|
458
|
-
|
|
459
|
-
pans: drop(
|
|
460
|
-
helpers: drop(
|
|
461
|
-
svgImports: drop(
|
|
462
|
-
children: names.includes(
|
|
576
|
+
...environment,
|
|
577
|
+
constants,
|
|
578
|
+
state: drop(environment.state),
|
|
579
|
+
refs: drop(environment.refs),
|
|
580
|
+
animations: drop(environment.animations),
|
|
581
|
+
locals: ownLocals ?? drop(environment.locals),
|
|
582
|
+
callbacks: drop(environment.callbacks),
|
|
583
|
+
functionProps: drop(environment.functionProps),
|
|
584
|
+
pans: drop(environment.pans),
|
|
585
|
+
helpers: drop(environment.helpers),
|
|
586
|
+
svgImports: drop(environment.svgImports),
|
|
587
|
+
children: names.includes(environment.children?.ref?.name)
|
|
463
588
|
? null
|
|
464
|
-
:
|
|
589
|
+
: environment.children,
|
|
465
590
|
};
|
|
466
591
|
}
|
|
467
592
|
/**
|
|
468
593
|
* `{listState.map((item, i) => <Row/>)}` over a STATE array of variable length. Pre-allocates a fixed
|
|
469
|
-
* pool of `
|
|
470
|
-
* local) and is shown only while `row < count` (display toggle).
|
|
594
|
+
* pool of `capacity` rows (no runtime malloc); the row at index `row` binds `item` to `s_<name>[row]` (a struct
|
|
595
|
+
* local) and is shown only while `row < count` (display toggle). the update then drives every row's content
|
|
471
596
|
* and show.
|
|
472
597
|
*
|
|
473
598
|
* @param call The `.map(…)` call.
|
|
474
599
|
* @param listState The list state it maps over.
|
|
475
600
|
* @param scope Compile-time constants in reach.
|
|
476
|
-
* @param
|
|
477
|
-
* @param
|
|
601
|
+
* @param output Everything emitted so far.
|
|
602
|
+
* @param environment The expression environment.
|
|
478
603
|
* @param state The component's state table.
|
|
479
604
|
*
|
|
480
605
|
* @returns The pool.
|
|
481
606
|
*/
|
|
482
|
-
function emitDynamicMap(call, listState, scope,
|
|
483
|
-
const
|
|
484
|
-
if (!isFn(
|
|
607
|
+
function emitDynamicMap(call, listState, scope, output, environment, state) {
|
|
608
|
+
const mapFunction = call.arguments[0];
|
|
609
|
+
if (!isFn(mapFunction)) {
|
|
485
610
|
throw new Error('AOT: .map argument must be an inline function');
|
|
486
611
|
}
|
|
487
|
-
const itemName =
|
|
488
|
-
const indexName =
|
|
489
|
-
const rowJSX = componentReturnJSX(
|
|
612
|
+
const itemName = mapFunction.params[0]?.name;
|
|
613
|
+
const indexName = mapFunction.params[1]?.name;
|
|
614
|
+
const rowJSX = componentReturnJSX(mapFunction);
|
|
490
615
|
// Build every row of the pool; each row's display is tied to the list's current count.
|
|
491
616
|
const rows = [];
|
|
492
|
-
for (let rowIndex = 0; rowIndex < listState.
|
|
617
|
+
for (let rowIndex = 0; rowIndex < listState.capacity; rowIndex++) {
|
|
493
618
|
// The item is a runtime struct slot, so drop any same-named const from the row scope, as emitComponent does.
|
|
494
619
|
const rowScope = { ...scope };
|
|
495
620
|
if (itemName) {
|
|
@@ -498,24 +623,45 @@ function emitDynamicMap(call, listState, scope, out, env, state) {
|
|
|
498
623
|
if (indexName) {
|
|
499
624
|
setBinding(rowScope, indexName, rowIndex); // the index is a compile-time literal per pooled row
|
|
500
625
|
}
|
|
501
|
-
const rowLocals = new Map(
|
|
626
|
+
const rowLocals = new Map(environment.locals);
|
|
502
627
|
if (indexName) {
|
|
503
628
|
rowLocals.delete(indexName); // the index is a per-row literal in rowScope
|
|
504
629
|
}
|
|
505
|
-
// A row's item has no
|
|
630
|
+
// A row's item has no slot of its own: only its fields are read, through `struct`.
|
|
506
631
|
if (itemName) {
|
|
507
|
-
rowLocals.set(itemName, {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
632
|
+
rowLocals.set(itemName, rowItemOf(listState, { name: itemName, row: rowIndex }));
|
|
633
|
+
}
|
|
634
|
+
rows.push(emitElementInto(rowJSX, rowScope, output, {
|
|
635
|
+
...rowEnv(environment, [itemName, indexName], rowScope, rowLocals),
|
|
636
|
+
inListRow: true,
|
|
637
|
+
mounted: mountedWhile(environment, {
|
|
638
|
+
kind: 'compare',
|
|
639
|
+
operator: '<',
|
|
640
|
+
left: { kind: 'number', value: rowIndex, type: INT },
|
|
641
|
+
right: { kind: 'listLength', list: listState, type: INT },
|
|
642
|
+
type: BOOL,
|
|
643
|
+
}),
|
|
644
|
+
}, state, { show: { kind: 'rowInUse', row: rowIndex, list: listState } }));
|
|
514
645
|
}
|
|
515
646
|
return { kind: 'pool', list: listState, rows };
|
|
516
647
|
}
|
|
517
|
-
/**
|
|
518
|
-
|
|
648
|
+
/**
|
|
649
|
+
* Whether something under a test is mounted: the test, and whatever its surroundings are mounted under.
|
|
650
|
+
*
|
|
651
|
+
* @param environment The environment around the test.
|
|
652
|
+
* @param test What it is built under.
|
|
653
|
+
*
|
|
654
|
+
* @returns The condition.
|
|
655
|
+
*/
|
|
656
|
+
const mountedWhile = (environment, test) => environment.mounted
|
|
657
|
+
? {
|
|
658
|
+
kind: 'logical',
|
|
659
|
+
operator: '&&',
|
|
660
|
+
left: environment.mounted,
|
|
661
|
+
right: test,
|
|
662
|
+
type: BOOL,
|
|
663
|
+
}
|
|
664
|
+
: test;
|
|
519
665
|
/**
|
|
520
666
|
* Whether a folded child value renders nothing, as React treats it: false, null, undefined and the empty string.
|
|
521
667
|
*
|
|
@@ -527,50 +673,52 @@ const rendersNothing = (value) => value === false || value == null || value ===
|
|
|
527
673
|
/**
|
|
528
674
|
* The error for a child expression that is not lowered; `what` describes it when it folded to a value.
|
|
529
675
|
*
|
|
530
|
-
* @param
|
|
676
|
+
* @param expression The child expression.
|
|
531
677
|
* @param what Its folded value, described, or null when it does not fold.
|
|
532
678
|
*
|
|
533
679
|
* @returns The error, for the caller to throw.
|
|
534
680
|
*/
|
|
535
|
-
function unsupportedChild(
|
|
681
|
+
function unsupportedChild(expression, what) {
|
|
536
682
|
const error = what == null
|
|
537
|
-
? aotError(`AOT: unsupported expression child "${
|
|
683
|
+
? aotError(`AOT: unsupported expression child "${expression.type}" in a container`, 'a child expression must be a JSX element, `cond && <El/>` / a ternary of elements, ' +
|
|
538
684
|
"or `list.map(item => <El/>)`. A bare variable holding JSX isn't inlined — write the " +
|
|
539
685
|
'element directly. (If this is a responsive Flow-A-only branch, compile at the target ' +
|
|
540
686
|
'board size via ER_AOT_SCREEN_W/H so the AOT folds the supported branch.)')
|
|
541
687
|
: aotError(`AOT: a non-element expression child (${what}) cannot render here`, 'only JSX elements render as children; wrap text in a <Text>{…}</Text>.');
|
|
542
|
-
if (
|
|
543
|
-
error.aotLoc =
|
|
688
|
+
if (expression.loc) {
|
|
689
|
+
error.aotLoc = expression.loc.start;
|
|
544
690
|
}
|
|
545
691
|
return error;
|
|
546
692
|
}
|
|
547
693
|
/**
|
|
548
694
|
* Lowers one branch of a conditional child: an element, a nested `&&` or ternary, or something that renders
|
|
549
|
-
* nothing. A condition that folds picks its branch now
|
|
550
|
-
* only while it is chosen, `within` the branch it sits in.
|
|
695
|
+
* nothing. A condition that folds picks its branch now. A state-driven one builds the branch it picks; in a list
|
|
696
|
+
* row it builds every branch with the row and shows each only while it is chosen, `within` the branch it sits in.
|
|
551
697
|
*
|
|
552
|
-
* @param
|
|
698
|
+
* @param expression The branch.
|
|
553
699
|
* @param scope Compile-time constants in reach.
|
|
554
|
-
* @param
|
|
555
|
-
* @param
|
|
700
|
+
* @param output Everything emitted so far.
|
|
701
|
+
* @param environment The expression environment.
|
|
556
702
|
* @param state The component's state table.
|
|
557
703
|
* @param within When the branch it sits in shows, or null at the top.
|
|
558
704
|
*
|
|
559
705
|
* @returns The branch.
|
|
560
706
|
*/
|
|
561
|
-
function lowerBranch(
|
|
562
|
-
if (
|
|
563
|
-
return emitElementInto(
|
|
707
|
+
function lowerBranch(expression, scope, output, environment, state, within) {
|
|
708
|
+
if (expression.type === 'JSXElement') {
|
|
709
|
+
return emitElementInto(expression, scope, output, environment, state, within ? { show: within } : {});
|
|
564
710
|
}
|
|
565
|
-
const isAnd =
|
|
566
|
-
if (isAnd ||
|
|
567
|
-
const testNode = isAnd
|
|
711
|
+
const isAnd = expression.type === 'LogicalExpression' && expression.operator === '&&';
|
|
712
|
+
if (isAnd || expression.type === 'ConditionalExpression') {
|
|
713
|
+
const testNode = isAnd
|
|
714
|
+
? expression.left
|
|
715
|
+
: expression.test;
|
|
568
716
|
const whenTrue = isAnd
|
|
569
|
-
?
|
|
570
|
-
:
|
|
717
|
+
? expression.right
|
|
718
|
+
: expression.consequent;
|
|
571
719
|
const whenFalse = isAnd
|
|
572
720
|
? null
|
|
573
|
-
:
|
|
721
|
+
: expression.alternate;
|
|
574
722
|
// A test that folds picks its branch at build time; `&&` with a false test renders nothing.
|
|
575
723
|
let folded;
|
|
576
724
|
try {
|
|
@@ -585,78 +733,95 @@ function lowerBranch(expr, scope, out, env, state, within) {
|
|
|
585
733
|
? mergeBranchElements(testNode, whenTrue, whenFalse)
|
|
586
734
|
: null;
|
|
587
735
|
if (merged)
|
|
588
|
-
return lowerBranch(merged, scope,
|
|
736
|
+
return lowerBranch(merged, scope, output, environment, state, within);
|
|
589
737
|
folded = { value: foldBoot(folded.value) };
|
|
590
738
|
}
|
|
591
739
|
if (folded) {
|
|
592
740
|
// A false `&&` yields its left value, which must be one React renders as nothing (`0` is text).
|
|
593
741
|
if (isAnd && !folded.value && !rendersNothing(folded.value)) {
|
|
594
|
-
throw unsupportedChild(
|
|
742
|
+
throw unsupportedChild(expression, JSON.stringify(folded.value) ?? String(folded.value));
|
|
595
743
|
}
|
|
596
744
|
const chosen = folded.value ? whenTrue : whenFalse;
|
|
597
745
|
return chosen
|
|
598
|
-
? lowerBranch(chosen, scope,
|
|
746
|
+
? lowerBranch(chosen, scope, output, environment, state, within)
|
|
599
747
|
: null;
|
|
600
748
|
}
|
|
601
|
-
// A state-driven test builds every branch
|
|
602
|
-
const test = lowerExpr(testNode,
|
|
603
|
-
if (isAnd &&
|
|
749
|
+
// A state-driven test builds the branch it picks. A list row builds every branch, each shown while picked.
|
|
750
|
+
const test = lowerExpr(testNode, environment);
|
|
751
|
+
if (isAnd && shapeOf(test) === 'number') {
|
|
604
752
|
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/>`.');
|
|
605
753
|
if (testNode.loc) {
|
|
606
754
|
error.aotLoc = testNode.loc.start;
|
|
607
755
|
}
|
|
608
756
|
throw error;
|
|
609
757
|
}
|
|
610
|
-
const
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
within,
|
|
615
|
-
});
|
|
758
|
+
const toggle = !!environment.inListRow;
|
|
759
|
+
const shown = (negate) => toggle ? { kind: 'condition', test, negate, within } : null;
|
|
760
|
+
// A test on a tagged object's tag narrows the fields each branch can read.
|
|
761
|
+
const narrowing = narrowOn(testNode, environment);
|
|
616
762
|
return {
|
|
617
763
|
kind: 'conditional',
|
|
618
764
|
test,
|
|
619
|
-
|
|
765
|
+
toggle,
|
|
766
|
+
whenTrue: lowerBranch(whenTrue, narrowedScope(scope, narrowing.whenTrue), output, {
|
|
767
|
+
...narrowed(environment, narrowing.whenTrue),
|
|
768
|
+
mounted: mountedWhile(environment, test),
|
|
769
|
+
}, state, shown(false)),
|
|
620
770
|
whenFalse: whenFalse
|
|
621
|
-
? lowerBranch(whenFalse, scope,
|
|
771
|
+
? lowerBranch(whenFalse, narrowedScope(scope, narrowing.whenFalse), output, {
|
|
772
|
+
...narrowed(environment, narrowing.whenFalse),
|
|
773
|
+
mounted: mountedWhile(environment, {
|
|
774
|
+
kind: 'unary',
|
|
775
|
+
operator: '!',
|
|
776
|
+
operand: test,
|
|
777
|
+
type: BOOL,
|
|
778
|
+
}),
|
|
779
|
+
}, state, shown(true))
|
|
622
780
|
: null,
|
|
623
781
|
};
|
|
624
782
|
}
|
|
625
783
|
// Anything else must be a constant that renders nothing.
|
|
626
784
|
let value;
|
|
627
785
|
try {
|
|
628
|
-
value = evalStatic(
|
|
786
|
+
value = evalStatic(expression, withUndefined(scope));
|
|
629
787
|
}
|
|
630
788
|
catch {
|
|
631
|
-
throw unsupportedChild(
|
|
789
|
+
throw unsupportedChild(expression, null);
|
|
632
790
|
}
|
|
633
791
|
if (!rendersNothing(value)) {
|
|
634
|
-
throw unsupportedChild(
|
|
792
|
+
throw unsupportedChild(expression, JSON.stringify(value) ?? String(value));
|
|
635
793
|
}
|
|
636
794
|
return null;
|
|
637
795
|
}
|
|
638
796
|
/**
|
|
639
|
-
* Emits the children of a container node. Everything unrolls at compile time
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
* the engine has no node for.
|
|
797
|
+
* Emits the children of a container node. Everything unrolls at compile time: fragments are inlined, a
|
|
798
|
+
* state-driven condition lowers each of its branches, and a `.map` over list state builds a fixed pool of rows.
|
|
799
|
+
* Every child is either emitted or refused with an "AOT: …" error: text outside a <Text>, or anything else the
|
|
800
|
+
* engine has no node for.
|
|
644
801
|
*
|
|
645
802
|
* @param children The container's children.
|
|
646
803
|
* @param scope Compile-time constants in reach.
|
|
647
|
-
* @param
|
|
648
|
-
* @param
|
|
804
|
+
* @param output Everything emitted so far.
|
|
805
|
+
* @param environment The expression environment.
|
|
649
806
|
* @param state The component's state table.
|
|
650
807
|
*
|
|
651
808
|
* @returns The container's children.
|
|
652
809
|
*/
|
|
653
|
-
export function emitChildren(children, scope,
|
|
810
|
+
export function emitChildren(children, scope, output, environment, state) {
|
|
654
811
|
const lowered = [];
|
|
655
812
|
const element = (built) => lowered.push({ kind: 'element', element: built });
|
|
656
813
|
for (const child of inlineFragments(children)) {
|
|
814
|
+
// A context's provider is no node of its own: its children are emitted in its place, reading its value.
|
|
815
|
+
if (child.type === 'JSXElement') {
|
|
816
|
+
const context = providedContextOf(child, environment);
|
|
817
|
+
if (context) {
|
|
818
|
+
lowered.push(...emitChildren(child.children, scope, output, providedEnvironment(child, context, scope, environment, state), state));
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
657
822
|
// An element child is emitted as it is; an {expression} child is lowered by its shape below.
|
|
658
823
|
if (child.type === 'JSXElement') {
|
|
659
|
-
element(emitElementInto(child, scope,
|
|
824
|
+
element(emitElementInto(child, scope, output, environment, state));
|
|
660
825
|
continue;
|
|
661
826
|
}
|
|
662
827
|
// Text needs a <Text>; the whitespace between tags is not a child.
|
|
@@ -673,30 +838,36 @@ export function emitChildren(children, scope, out, env, state) {
|
|
|
673
838
|
if (child.type === 'JSXSpreadChild') {
|
|
674
839
|
throw unsupportedChild(child, null);
|
|
675
840
|
}
|
|
676
|
-
const
|
|
677
|
-
if (
|
|
841
|
+
const expression = child.expression;
|
|
842
|
+
if (expression.type === 'JSXEmptyExpression')
|
|
678
843
|
continue;
|
|
679
|
-
if (isChildrenRef(
|
|
680
|
-
// {children} / {props.children}: emit the captured call-site children, in the caller's scope/env.
|
|
681
|
-
|
|
844
|
+
if (isChildrenRef(expression, environment)) {
|
|
845
|
+
// {children} / {props.children}: emit the captured call-site children, in the caller's scope/env. They read
|
|
846
|
+
// a context from the providers where they are rendered, as in React.
|
|
847
|
+
lowered.push(...emitChildren(environment.children.nodes, environment.children.scope, output, {
|
|
848
|
+
...environment.children.environment,
|
|
849
|
+
providers: environment.providers,
|
|
850
|
+
}, state));
|
|
682
851
|
continue;
|
|
683
852
|
}
|
|
684
|
-
if (
|
|
685
|
-
|
|
686
|
-
|
|
853
|
+
if (expression.type === 'CallExpression' &&
|
|
854
|
+
expression.callee.type === 'MemberExpression' &&
|
|
855
|
+
expression.callee.property.name === 'map') {
|
|
687
856
|
// `{items.map(item => <X/>)}`: a pool of rows over list state, else unrolled over a constant array.
|
|
688
|
-
const mapTarget =
|
|
689
|
-
const stateRecord = mapTarget.type === 'Identifier'
|
|
857
|
+
const mapTarget = expression.callee.object;
|
|
858
|
+
const stateRecord = mapTarget.type === 'Identifier'
|
|
859
|
+
? environment.state.get(mapTarget.name)
|
|
860
|
+
: derivedListOf(mapTarget, environment);
|
|
690
861
|
if (stateRecord?.kind === 'list') {
|
|
691
|
-
lowered.push(emitDynamicMap(
|
|
862
|
+
lowered.push(emitDynamicMap(expression, stateRecord, scope, output, environment, state));
|
|
692
863
|
}
|
|
693
864
|
else {
|
|
694
|
-
emitMap(
|
|
865
|
+
emitMap(expression, scope, output, environment, state).forEach(element);
|
|
695
866
|
}
|
|
696
867
|
continue;
|
|
697
868
|
}
|
|
698
869
|
// An element, `cond && <X/>`, a ternary of them (nested too), or a constant that renders nothing.
|
|
699
|
-
const branch = lowerBranch(
|
|
870
|
+
const branch = lowerBranch(expression, scope, output, environment, state, null);
|
|
700
871
|
if (branch?.kind === 'conditional') {
|
|
701
872
|
lowered.push(branch);
|
|
702
873
|
}
|