embedded-react 0.20.0 → 0.22.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 +3 -2
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +17 -47
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +62 -10
- package/aot/compiler/collect.mjs +517 -137
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +47 -8
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1378 -97
- package/aot/compiler/handlers.mjs +1807 -354
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- 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 +203 -53
- 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 +45 -31
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +18 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +906 -33
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +16 -3
- package/aot/compiler/package/lower.mjs +707 -176
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- 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/scroll-to.mjs +112 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +20 -5
- package/aot/compiler/svg.mjs +13 -7
- 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 +321 -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/style-map.mjs +94 -3
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +85 -94
- package/cli.mjs +490 -586
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +146 -213
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +94 -116
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/Dimensions.mjs +32 -0
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +238 -5
- package/src/embedded-react/index.js +26 -17
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -148
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
package/aot/compiler/collect.mjs
CHANGED
|
@@ -22,8 +22,14 @@ import { readFileSync, existsSync } from 'node:fs';
|
|
|
22
22
|
import { posix, resolve } from 'node:path';
|
|
23
23
|
import { aotError, locateError } from './diagnostics.mjs';
|
|
24
24
|
import { mergeBranchElements } from './branch-merge.mjs';
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
25
|
+
import { inferItemStruct } from './list-items.mjs';
|
|
26
|
+
import { flattenTree, isObjectTree } from './records.mjs';
|
|
27
|
+
import { recordShapesOf, returnedObjects, writtenObjects, } from './record-shapes.mjs';
|
|
28
|
+
import { stateLocal, tagText, taggedTreeOf } from './tagged-records.mjs';
|
|
29
|
+
import { patternNames } from './updaters.mjs';
|
|
30
|
+
import { splitCallOf } from './list-split.mjs';
|
|
31
|
+
import { bootCheckpoint, bootSlotOf, buildValueOf, foldBoot, isBoot, isContainer, nameBootValues, PANEL_KEY, screenBinding, } from './boot.mjs';
|
|
32
|
+
import { evalStaticOrThrow, evalStatic, setBinding, withUndefined, rethrowFoldLimit, HiddenField, RuntimeLeaf, } from './static-eval.mjs';
|
|
27
33
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
28
34
|
- Constants
|
|
29
35
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -39,8 +45,6 @@ const slotKindOfValue = (value) => typeof value === 'string'
|
|
|
39
45
|
: typeof value === 'number' && !Number.isInteger(value)
|
|
40
46
|
? 'float'
|
|
41
47
|
: 'int';
|
|
42
|
-
/** No state or ref slot widened to 64 bits — the collectors' default. */
|
|
43
|
-
export const NO_WIDE = new Map();
|
|
44
48
|
/**
|
|
45
49
|
* What a whole-number initial value needs beyond an int slot, typed as a constant of its size is: nothing in
|
|
46
50
|
* the int range, a 64-bit number past it, a float past 64 bits.
|
|
@@ -152,6 +156,53 @@ export function bindConst(name, value) {
|
|
|
152
156
|
nameBootValues(value, name);
|
|
153
157
|
return value;
|
|
154
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Folds a destructured const and binds each name it takes, as `const {width, height} = screen`.
|
|
161
|
+
*
|
|
162
|
+
* @param scope The scope to fold against and bind in.
|
|
163
|
+
* @param pattern The pattern.
|
|
164
|
+
* @param init What it destructures.
|
|
165
|
+
*
|
|
166
|
+
* @throws An error when the object doesn't fold, or the pattern is more than plain names for fields it holds.
|
|
167
|
+
*/
|
|
168
|
+
export function bindFoldedPattern(scope, pattern, init) {
|
|
169
|
+
const bound = [];
|
|
170
|
+
const collectFields = (objectPattern, value) => {
|
|
171
|
+
if (!isContainer(value) || Array.isArray(value)) {
|
|
172
|
+
throw new Error('AOT: only an object that folds is destructured here');
|
|
173
|
+
}
|
|
174
|
+
const fields = value;
|
|
175
|
+
for (const property of objectPattern.properties) {
|
|
176
|
+
if (property.type === 'RestElement' || property.computed) {
|
|
177
|
+
throw new Error('AOT: the pattern is more than plain names');
|
|
178
|
+
}
|
|
179
|
+
const key = property.key.type === 'Identifier'
|
|
180
|
+
? property.key.name
|
|
181
|
+
: property.key.type === 'StringLiteral'
|
|
182
|
+
? property.key.value
|
|
183
|
+
: null;
|
|
184
|
+
const field = key !== null && Object.hasOwn(fields, key) ? fields[key] : undefined;
|
|
185
|
+
if (field === undefined ||
|
|
186
|
+
field instanceof RuntimeLeaf ||
|
|
187
|
+
field instanceof HiddenField) {
|
|
188
|
+
throw new Error('AOT: the field does not fold');
|
|
189
|
+
}
|
|
190
|
+
if (property.value.type === 'Identifier') {
|
|
191
|
+
bound.push([property.value.name, field]);
|
|
192
|
+
}
|
|
193
|
+
else if (property.value.type === 'ObjectPattern') {
|
|
194
|
+
collectFields(property.value, field);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
throw new Error('AOT: the pattern is more than plain names');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
collectFields(pattern, evalStatic(init, withUndefined(scope), 'boot'));
|
|
202
|
+
for (const [name, value] of bound) {
|
|
203
|
+
setBinding(scope, name, bindConst(name, value));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
155
206
|
/**
|
|
156
207
|
* The module's compile-time scope: `screen`, the seed, and every module-level `const` that folds. A const
|
|
157
208
|
* derived from `screen` is a boot value (see BootValue).
|
|
@@ -164,8 +215,14 @@ export function bindConst(name, value) {
|
|
|
164
215
|
*/
|
|
165
216
|
export function moduleScope(program, screen, seed = {}) {
|
|
166
217
|
// Seed the scope (image imports as asset names) before folding module consts, so a const can reference them.
|
|
167
|
-
|
|
168
|
-
|
|
218
|
+
// The panel's size is kept twice: as `screen`, and as React Native reports it (see rewriteWindowDimensions).
|
|
219
|
+
const panel = screenBinding(screen);
|
|
220
|
+
const scope = {
|
|
221
|
+
screen: panel,
|
|
222
|
+
[PANEL_KEY]: { ...panel, scale: 1, fontScale: 1 },
|
|
223
|
+
...seed,
|
|
224
|
+
};
|
|
225
|
+
// Fold module variables in order so later ones see earlier ones; skip functions and non-folds.
|
|
169
226
|
for (const statement of program.body) {
|
|
170
227
|
const declaration = statement.type === 'ExportNamedDeclaration'
|
|
171
228
|
? statement.declaration
|
|
@@ -173,6 +230,15 @@ export function moduleScope(program, screen, seed = {}) {
|
|
|
173
230
|
if (declaration?.type !== 'VariableDeclaration')
|
|
174
231
|
continue;
|
|
175
232
|
for (const declarator of declaration.declarations) {
|
|
233
|
+
if (declarator.id?.type === 'ObjectPattern' && declarator.init) {
|
|
234
|
+
try {
|
|
235
|
+
bindFoldedPattern(scope, declarator.id, declarator.init);
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
rethrowFoldLimit(error);
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
176
242
|
if (!declarator.id ||
|
|
177
243
|
declarator.id.type !== 'Identifier' ||
|
|
178
244
|
!declarator.init ||
|
|
@@ -182,72 +248,32 @@ export function moduleScope(program, screen, seed = {}) {
|
|
|
182
248
|
try {
|
|
183
249
|
setBinding(scope, declarator.id.name, bindConst(declarator.id.name, evalStatic(declarator.init, withUndefined(scope), 'boot')));
|
|
184
250
|
}
|
|
185
|
-
catch {
|
|
186
|
-
|
|
251
|
+
catch (error) {
|
|
252
|
+
// Not a static const, so skipped, unless a string helper in it is past what a package can run.
|
|
253
|
+
rethrowFoldLimit(error);
|
|
187
254
|
}
|
|
188
255
|
}
|
|
189
256
|
}
|
|
190
257
|
return scope;
|
|
191
258
|
}
|
|
192
259
|
/**
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* @param items The folded initial value.
|
|
196
|
-
* @param name The state's name, for errors.
|
|
197
|
-
*
|
|
198
|
-
* @returns The struct: one field per key of the first element.
|
|
199
|
-
*/
|
|
200
|
-
function inferItemStruct(items, name) {
|
|
201
|
-
// Require a non-empty array so the first element can define the list state's fixed item shape.
|
|
202
|
-
const shapeHint = 'a list state is a fixed-shape struct array: each element must be an OBJECT with the same string/number fields, ' +
|
|
203
|
-
'e.g. useState([{ title: "A", n: 1 }, { title: "B", n: 2 }]). The first element defines the columns.';
|
|
204
|
-
if (!Array.isArray(items) || !items.length) {
|
|
205
|
-
throw aotError(`AOT: list state "${name}" needs ≥1 initial element to infer its item shape`, shapeHint);
|
|
206
|
-
}
|
|
207
|
-
// Require the first list element to be a plain object so its keys can define the item struct.
|
|
208
|
-
const first = items[0];
|
|
209
|
-
if (typeof first !== 'object' || first === null || Array.isArray(first)) {
|
|
210
|
-
throw aotError(`AOT: list state "${name}" elements must be objects`, shapeHint);
|
|
211
|
-
}
|
|
212
|
-
// Type each field from the first element's value: a string, an int or a float.
|
|
213
|
-
const fields = Object.keys(first).map((key) => {
|
|
214
|
-
const fieldValue = first[key];
|
|
215
|
-
if (typeof fieldValue === 'string')
|
|
216
|
-
return { key, kind: 'string' };
|
|
217
|
-
if (typeof fieldValue === 'number') {
|
|
218
|
-
return { key, kind: Number.isInteger(fieldValue) ? 'int' : 'float' };
|
|
219
|
-
}
|
|
220
|
-
throw aotError(`AOT: list state "${name}" field "${key}" must be a string or number`, shapeHint);
|
|
221
|
-
});
|
|
222
|
-
return { fields };
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Refuses an initial value too big for its fixed buffer: it would be cut short at boot without a word.
|
|
260
|
+
* Refuses an initial list with more rows than a list holds: they would be dropped at boot without a word. Each
|
|
261
|
+
* string's cap is sized from its values when the app is lowered.
|
|
226
262
|
*
|
|
227
263
|
* @param stateName The state's JS name.
|
|
228
|
-
* @param items The list's initial rows
|
|
229
|
-
* @param strings The initial strings it starts with, as `[what, text]` pairs.
|
|
264
|
+
* @param items The list's initial rows.
|
|
230
265
|
* @param caps The compile's caps.
|
|
231
266
|
* @param initialArgument The initial value, for the error's location.
|
|
232
267
|
*/
|
|
233
|
-
function
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
error = aotError(`AOT: list state "${stateName}" starts with ${items.length} rows, but a list holds ${caps.listCap}`, 'raise ER_AOT_LIST_CAP (the listCap option), or start with fewer rows.');
|
|
268
|
+
function checkInitialRows(stateName, items, caps, initialArgument) {
|
|
269
|
+
if (items.length <= caps.listCap) {
|
|
270
|
+
return;
|
|
237
271
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const [what, text] = tooLong;
|
|
242
|
-
error = aotError(`AOT: ${what} starts ${Buffer.byteLength(text, 'utf8')} bytes long, but a string holds ` +
|
|
243
|
-
`${caps.listStrCap - 1} bytes`, 'raise ER_AOT_LIST_STR_CAP (the listStrCap option), or shorten the string.');
|
|
244
|
-
}
|
|
245
|
-
if (error) {
|
|
246
|
-
if (initialArgument?.loc) {
|
|
247
|
-
error.aotLoc = initialArgument.loc.start;
|
|
248
|
-
}
|
|
249
|
-
throw error;
|
|
272
|
+
const error = aotError(`AOT: list state "${stateName}" starts with ${items.length} rows, but a list holds ${caps.listCap}`, 'raise ER_AOT_LIST_CAP (the listCap option), or start with fewer rows.');
|
|
273
|
+
if (initialArgument?.loc) {
|
|
274
|
+
error.aotLoc = initialArgument.loc.start;
|
|
250
275
|
}
|
|
276
|
+
throw error;
|
|
251
277
|
}
|
|
252
278
|
/**
|
|
253
279
|
* A state's initial value, when it is a number or a boolean derived from `screen`. A string stays folded: a closed
|
|
@@ -301,8 +327,288 @@ export function settleBootInitials(states) {
|
|
|
301
327
|
}
|
|
302
328
|
}
|
|
303
329
|
/**
|
|
304
|
-
*
|
|
305
|
-
*
|
|
330
|
+
* A scalar state's slot, its kind picked from its initial value.
|
|
331
|
+
*
|
|
332
|
+
* @param name The JS name.
|
|
333
|
+
* @param slotName The storage name.
|
|
334
|
+
* @param setter The setter's JS name.
|
|
335
|
+
* @param initialValue The folded initial value.
|
|
336
|
+
* @param initialNode What it was written as, if anything: `70.0` makes a float slot.
|
|
337
|
+
* @param isHost It is a useHostValue.
|
|
338
|
+
* @param bootValue An initial value derived from `screen`.
|
|
339
|
+
*
|
|
340
|
+
* @returns The state.
|
|
341
|
+
*/
|
|
342
|
+
function scalarStateOf(name, slotName, setter, initialValue, initialNode, isHost, bootValue) {
|
|
343
|
+
// Pick the slot's kind from the initial value.
|
|
344
|
+
let slotKind = slotKindOfValue(initialValue);
|
|
345
|
+
// Flag booleans: slotKindOfValue makes them int, but text lowering must render false differently from 0.
|
|
346
|
+
const isBoolean = typeof initialValue === 'boolean';
|
|
347
|
+
// A literal written like 70.0 forces a float slot so the state can hold fractions (a smooth drag).
|
|
348
|
+
if (slotKind === 'int' &&
|
|
349
|
+
initialNode?.type === 'NumericLiteral' &&
|
|
350
|
+
typeof initialNode.extra?.raw === 'string' &&
|
|
351
|
+
/[.eE]/.test(initialNode.extra.raw)) {
|
|
352
|
+
slotKind = 'float';
|
|
353
|
+
}
|
|
354
|
+
// A whole number past the int range starts it 64-bit (past 64 bits, a float). A setter that stores a timestamp
|
|
355
|
+
// or such a number in an int slot widens it later, once every store is lowered (see lowerApp).
|
|
356
|
+
const initialWidth = slotKind === 'int' ? wideInitial(initialValue) : null;
|
|
357
|
+
if (initialWidth === 'float') {
|
|
358
|
+
slotKind = 'float';
|
|
359
|
+
}
|
|
360
|
+
const widened = slotKind === 'int' && !isBoolean && initialWidth === 'number'
|
|
361
|
+
? 'number'
|
|
362
|
+
: undefined;
|
|
363
|
+
if (widened) {
|
|
364
|
+
slotKind = 'i64';
|
|
365
|
+
}
|
|
366
|
+
// String scalar → a fixed buffer; setters format into it (see scalarAssign).
|
|
367
|
+
const initial = slotKind === 'string' ? String(initialValue) : Number(initialValue);
|
|
368
|
+
if (slotKind === 'float') {
|
|
369
|
+
checkNumber(initial);
|
|
370
|
+
}
|
|
371
|
+
else if (slotKind === 'i64') {
|
|
372
|
+
BigInt(initial); // refuses a value that is not a whole number
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
name,
|
|
376
|
+
slotName,
|
|
377
|
+
setter,
|
|
378
|
+
kind: 'scalar',
|
|
379
|
+
slotKind,
|
|
380
|
+
isBoolean,
|
|
381
|
+
initial,
|
|
382
|
+
host: isHost, // host-fed → also an input the host sets
|
|
383
|
+
initialAt: initialNode?.loc?.start,
|
|
384
|
+
...(widened ? { widened } : {}),
|
|
385
|
+
...(bootValue ? { bootValue } : {}),
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* What an object literal holds at a key, when it is written as a plain field.
|
|
390
|
+
*
|
|
391
|
+
* @param node The object.
|
|
392
|
+
* @param key The key.
|
|
393
|
+
*
|
|
394
|
+
* @returns The field's value, or undefined.
|
|
395
|
+
*/
|
|
396
|
+
function fieldNodeOf(node, key) {
|
|
397
|
+
if (node?.type !== 'ObjectExpression')
|
|
398
|
+
return undefined;
|
|
399
|
+
for (const property of [...node.properties].reverse()) {
|
|
400
|
+
if (property.type !== 'ObjectProperty' || property.computed)
|
|
401
|
+
continue;
|
|
402
|
+
const name = property.key.type === 'Identifier'
|
|
403
|
+
? property.key.name
|
|
404
|
+
: property.key.type === 'StringLiteral'
|
|
405
|
+
? property.key.value
|
|
406
|
+
: null;
|
|
407
|
+
if (name === key)
|
|
408
|
+
return property.value;
|
|
409
|
+
}
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* An object state: one scalar state per field, nested objects flattened to dotted paths.
|
|
414
|
+
*
|
|
415
|
+
* @param name The JS name.
|
|
416
|
+
* @param prefix The storage prefix.
|
|
417
|
+
* @param setter The setter's JS name.
|
|
418
|
+
* @param value The folded initial value.
|
|
419
|
+
* @param initialNode What it was written as.
|
|
420
|
+
*
|
|
421
|
+
* @returns The record.
|
|
422
|
+
*/
|
|
423
|
+
function recordStateOf(name, prefix, setter, value, initialNode) {
|
|
424
|
+
const fields = new Map();
|
|
425
|
+
const fail = (message, hint, at) => {
|
|
426
|
+
const error = aotError(message, hint);
|
|
427
|
+
const location = (at ?? initialNode).loc;
|
|
428
|
+
if (location)
|
|
429
|
+
error.aotLoc = location.start;
|
|
430
|
+
return error;
|
|
431
|
+
};
|
|
432
|
+
const flatten = (object, path, node) => {
|
|
433
|
+
const entries = Object.entries(object);
|
|
434
|
+
if (!entries.length) {
|
|
435
|
+
throw fail(`AOT: state "${[name, ...path].join('.')}" is an empty object`, 'give it the fields it will hold, each with a starting value.', node);
|
|
436
|
+
}
|
|
437
|
+
const folded = {};
|
|
438
|
+
for (const [key, fieldValue] of entries) {
|
|
439
|
+
const fieldPath = [...path, key];
|
|
440
|
+
const fieldName = [name, ...fieldPath].join('.');
|
|
441
|
+
const fieldNode = fieldNodeOf(node, key);
|
|
442
|
+
// Fields are keyed by their dotted path, so a key can't hold a dot.
|
|
443
|
+
if (key === '' || key.includes('.')) {
|
|
444
|
+
throw fail(`AOT: state "${[name, ...path].join('.')}" has a field named "${key}"`, 'name each field with letters, digits or `_`.', fieldNode);
|
|
445
|
+
}
|
|
446
|
+
if (isObjectTree(fieldValue)) {
|
|
447
|
+
folded[key] = flatten(fieldValue, fieldPath, fieldNode);
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (Array.isArray(fieldValue)) {
|
|
451
|
+
throw fail(`AOT: state "${fieldName}" is an array inside an object state`, 'keep the list in a useState of its own.', fieldNode);
|
|
452
|
+
}
|
|
453
|
+
if (fieldValue == null ||
|
|
454
|
+
(typeof fieldValue === 'number' && !Number.isFinite(fieldValue)) ||
|
|
455
|
+
!['number', 'string', 'boolean'].includes(typeof fieldValue)) {
|
|
456
|
+
throw fail(`AOT: the initial value of state "${fieldName}" is ${typeof fieldValue === 'function' ? 'a function' : String(fieldValue)}`, "give each field a concrete starting value: 0, '' or false.", fieldNode);
|
|
457
|
+
}
|
|
458
|
+
const state = scalarStateOf(fieldName, prefix + fieldName, undefined, fieldValue, fieldNode, false, null);
|
|
459
|
+
state.initialAt ??= initialNode.loc?.start;
|
|
460
|
+
fields.set(fieldPath.join('.'), state);
|
|
461
|
+
folded[key] = new RuntimeLeaf(stateLocal(state));
|
|
462
|
+
}
|
|
463
|
+
return folded;
|
|
464
|
+
};
|
|
465
|
+
const folded = flatten(value, [], initialNode);
|
|
466
|
+
return { kind: 'record', name, setter, fields, value: folded };
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Makes an object state of several shapes a tagged record: a hidden slot holding the shape's index, and a slot for
|
|
470
|
+
* every field any shape has. A field the initial value lacks takes its kind from a constant written for it, and
|
|
471
|
+
* starts at zero.
|
|
472
|
+
*
|
|
473
|
+
* @param record The record of the initial value.
|
|
474
|
+
* @param shapes The shapes.
|
|
475
|
+
* @param written The objects the setter is given.
|
|
476
|
+
* @param prefix The storage prefix.
|
|
477
|
+
* @param initialNode What the initial value was written as.
|
|
478
|
+
*
|
|
479
|
+
* @returns The record.
|
|
480
|
+
*/
|
|
481
|
+
function tagRecord(record, shapes, written, prefix, initialNode) {
|
|
482
|
+
const { name, setter } = record;
|
|
483
|
+
const fail = (message, hint) => {
|
|
484
|
+
const error = aotError(message, hint);
|
|
485
|
+
if (initialNode.loc)
|
|
486
|
+
error.aotLoc = initialNode.loc.start;
|
|
487
|
+
return error;
|
|
488
|
+
};
|
|
489
|
+
const describe = (value) => value === null || value === undefined ? String(value) : `a ${typeof value}`;
|
|
490
|
+
const keys = new Set(shapes.shapes.flatMap(shape => [...shape]));
|
|
491
|
+
const fields = new Map();
|
|
492
|
+
for (const key of keys) {
|
|
493
|
+
if ([...keys].some(other => other.startsWith(`${key}.`))) {
|
|
494
|
+
throw fail(`AOT: "${name}.${key}" is an object in one shape and a value in another`, 'give the field the same kind in every shape.');
|
|
495
|
+
}
|
|
496
|
+
const constants = written.flatMap(object => {
|
|
497
|
+
const field = object.fields.get(key);
|
|
498
|
+
return field?.known ? [field] : [];
|
|
499
|
+
});
|
|
500
|
+
let field = record.fields.get(key);
|
|
501
|
+
if (!field) {
|
|
502
|
+
if (!constants.length) {
|
|
503
|
+
throw fail(`AOT: "${name}.${key}" is never written with a constant`, `its kind comes from a constant; write one for it once: ${setter}({…, ${key.split('.').at(-1)}: 0}).`);
|
|
504
|
+
}
|
|
505
|
+
const representative = constants.find(({ value }) => typeof value === 'number' && !Number.isInteger(value)) ??
|
|
506
|
+
constants.find(({ node }) => node?.type === 'NumericLiteral' &&
|
|
507
|
+
/[.eE]/.test(String(node.extra?.raw ?? ''))) ??
|
|
508
|
+
constants[0];
|
|
509
|
+
const { value } = representative;
|
|
510
|
+
if (!['number', 'string', 'boolean'].includes(typeof value) ||
|
|
511
|
+
(typeof value === 'number' && !Number.isFinite(value))) {
|
|
512
|
+
throw fail(`AOT: "${name}.${key}" is written with ${typeof value === 'number' ? value : describe(value)}`, "give each field a concrete value: 0, '' or false.");
|
|
513
|
+
}
|
|
514
|
+
field = scalarStateOf(`${name}.${key}`, `${prefix}${name}.${key}`, undefined, value, representative.node, false, null);
|
|
515
|
+
field.initial = field.slotKind === 'string' ? '' : 0;
|
|
516
|
+
field.initialAt = initialNode.loc?.start;
|
|
517
|
+
}
|
|
518
|
+
const kind = field.slotKind === 'string'
|
|
519
|
+
? 'string'
|
|
520
|
+
: field.isBoolean
|
|
521
|
+
? 'boolean'
|
|
522
|
+
: 'number';
|
|
523
|
+
for (const { value } of constants) {
|
|
524
|
+
if (typeof value !== kind) {
|
|
525
|
+
throw fail(`AOT: "${name}.${key}" holds a ${kind} in one place and ${describe(value)} in another`, `give the field one kind in every shape, as ${tagText(shapes.values[0])} gives it.`);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
fields.set(key, field);
|
|
529
|
+
}
|
|
530
|
+
const tagName = `${name}.${shapes.key}`;
|
|
531
|
+
const tagState = scalarStateOf(tagName, prefix + tagName, undefined, 0, undefined, false, null);
|
|
532
|
+
tagState.initialAt = initialNode.loc?.start;
|
|
533
|
+
const tagged = {
|
|
534
|
+
...record,
|
|
535
|
+
fields,
|
|
536
|
+
tag: {
|
|
537
|
+
key: shapes.key,
|
|
538
|
+
state: tagState,
|
|
539
|
+
values: shapes.values,
|
|
540
|
+
shapes: shapes.shapes,
|
|
541
|
+
},
|
|
542
|
+
};
|
|
543
|
+
tagged.value = taggedTreeOf(tagged, shapes.values.map((_, shape) => shape));
|
|
544
|
+
return tagged;
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* An error pinned to a node.
|
|
548
|
+
*
|
|
549
|
+
* @param error The error.
|
|
550
|
+
* @param node The node.
|
|
551
|
+
*
|
|
552
|
+
* @returns The error.
|
|
553
|
+
*/
|
|
554
|
+
function locatedAt(error, node) {
|
|
555
|
+
locateError(error, node?.loc?.start);
|
|
556
|
+
return error;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* The function a useReducer is given: written inline, or named and declared in the component or the module.
|
|
560
|
+
*
|
|
561
|
+
* @param node The useReducer's first argument.
|
|
562
|
+
* @param stateName The state's name, for errors.
|
|
563
|
+
* @param functionBody The component's body.
|
|
564
|
+
* @param program The module.
|
|
565
|
+
*
|
|
566
|
+
* @returns The reducer.
|
|
567
|
+
*/
|
|
568
|
+
function reducerOf(node, stateName, functionBody, program) {
|
|
569
|
+
// What a name is bound to at one level: a function, something else, or nothing.
|
|
570
|
+
const bindingIn = (statements, name) => {
|
|
571
|
+
for (const statement of statements) {
|
|
572
|
+
const declaration = statement.type === 'ExportNamedDeclaration'
|
|
573
|
+
? statement.declaration
|
|
574
|
+
: statement;
|
|
575
|
+
if (declaration?.type === 'FunctionDeclaration' &&
|
|
576
|
+
declaration.id?.name === name) {
|
|
577
|
+
return declaration;
|
|
578
|
+
}
|
|
579
|
+
if (declaration?.type !== 'VariableDeclaration')
|
|
580
|
+
continue;
|
|
581
|
+
for (const declarator of declaration.declarations) {
|
|
582
|
+
if (!patternNames(declarator.id).includes(name))
|
|
583
|
+
continue;
|
|
584
|
+
return declarator.id.type === 'Identifier' && isFn(declarator.init)
|
|
585
|
+
? declarator.init
|
|
586
|
+
: 'other';
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return null;
|
|
590
|
+
};
|
|
591
|
+
// The component's own binding of the name hides the module's.
|
|
592
|
+
const bound = isFn(node)
|
|
593
|
+
? node
|
|
594
|
+
: node?.type === 'Identifier'
|
|
595
|
+
? (bindingIn(functionBody.body, node.name) ??
|
|
596
|
+
bindingIn(program?.body ?? [], node.name))
|
|
597
|
+
: null;
|
|
598
|
+
const functionNode = bound === 'other' ? null : bound;
|
|
599
|
+
if (!functionNode) {
|
|
600
|
+
throw locatedAt(aotError(`AOT: the reducer for "${stateName}" must be a function in this file`, 'pass a function declared in this file, or write it inline: useReducer((state, action) => …, initial).'), node);
|
|
601
|
+
}
|
|
602
|
+
const name = node?.type === 'Identifier' ? node.name : 'reducer';
|
|
603
|
+
if (functionNode.params.length > 2 ||
|
|
604
|
+
functionNode.params.some(parameter => parameter.type !== 'Identifier')) {
|
|
605
|
+
throw locatedAt(aotError(`AOT: reducer "${name}" must take plain (state, action) params`, 'name the state and the action: function reducer(state, action) { … }.'), functionNode);
|
|
606
|
+
}
|
|
607
|
+
return { name, functionNode };
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Collects a component's useState and useReducer declarations → state descriptors keyed by JS name (reads) and setter
|
|
611
|
+
* name (writes): a useReducer's dispatch is its setter, and the state keeps its reducer. `prefix` namespaces the STORAGE so each inlined child instance gets its own slots: the
|
|
306
612
|
* lookup keys stay the bare JS names (`count`), but the field / array / count derive from `slotName`
|
|
307
613
|
* (`<prefix>count`). prefix='' (the App) keeps the storage names exactly the JS names.
|
|
308
614
|
* `caps` are the compiler's resolved caps (see resolveOptions); a list state takes its capacity from it.
|
|
@@ -311,11 +617,11 @@ export function settleBootInitials(states) {
|
|
|
311
617
|
* @param scope Compile-time constants the initial values fold against.
|
|
312
618
|
* @param caps The compile's caps.
|
|
313
619
|
* @param prefix The storage prefix: none for the App, `c<n>_` for an inlined instance.
|
|
314
|
-
* @param
|
|
620
|
+
* @param program The module, where a reducer may be declared.
|
|
315
621
|
*
|
|
316
622
|
* @returns The component's state table.
|
|
317
623
|
*/
|
|
318
|
-
export function collectState(functionBody, scope, caps, prefix = '',
|
|
624
|
+
export function collectState(functionBody, scope, caps, prefix = '', program) {
|
|
319
625
|
// Index each state by its JS name and, when it has one, by its setter's name.
|
|
320
626
|
const byName = new Map();
|
|
321
627
|
const bySetter = new Map();
|
|
@@ -330,8 +636,11 @@ export function collectState(functionBody, scope, caps, prefix = '', wide = NO_W
|
|
|
330
636
|
if (initializer?.type !== 'CallExpression')
|
|
331
637
|
continue;
|
|
332
638
|
// useHostValue is a host-fed scalar: a state slot like useState, written only by the host through its input.
|
|
333
|
-
|
|
334
|
-
|
|
639
|
+
// useReducer is a state slot whose setter is its dispatch.
|
|
640
|
+
const hook = initializer.callee.name;
|
|
641
|
+
const isHost = hook === 'useHostValue';
|
|
642
|
+
const isReducer = hook === 'useReducer';
|
|
643
|
+
if (hook !== 'useState' && !isHost && !isReducer) {
|
|
335
644
|
continue;
|
|
336
645
|
}
|
|
337
646
|
// Accept useHostValue(name) as a direct scalar binding, while useState must keep the usual [value, setter] tuple pattern.
|
|
@@ -351,18 +660,29 @@ export function collectState(functionBody, scope, caps, prefix = '', wide = NO_W
|
|
|
351
660
|
?.name;
|
|
352
661
|
if (!name)
|
|
353
662
|
continue;
|
|
354
|
-
// Build either a list-state record from an array initializer or a
|
|
663
|
+
// Build either a list-state record from an array initializer, or a split of a constant, or a scalar record from
|
|
664
|
+
// the folded initial value.
|
|
355
665
|
const slotName = prefix + name; // storage name (== name for the App; instance-unique for a child)
|
|
356
|
-
const initialArgument = initializer.arguments[0];
|
|
666
|
+
const initialArgument = initializer.arguments[isReducer ? 1 : 0];
|
|
667
|
+
let reducer;
|
|
668
|
+
if (isReducer) {
|
|
669
|
+
if (initializer.arguments.length !== 2) {
|
|
670
|
+
throw locatedAt(aotError(`AOT: useReducer for "${name}" must take a reducer and an initial value`, 'work the initial value out in the call instead of passing an init function: ' +
|
|
671
|
+
'useReducer(reducer, {count: 0}).'), initializer);
|
|
672
|
+
}
|
|
673
|
+
reducer = reducerOf(initializer.arguments[0], name, functionBody, program);
|
|
674
|
+
}
|
|
357
675
|
let record;
|
|
358
|
-
if (!isHost &&
|
|
676
|
+
if (!isHost &&
|
|
677
|
+
(initialArgument?.type === 'ArrayExpression' ||
|
|
678
|
+
splitCallOf(initialArgument))) {
|
|
359
679
|
// List state → a fixed-capacity pool of rows + a count.
|
|
360
|
-
const items = evalStaticOrThrow(initialArgument, scope, `AOT: the initial value of list state "${name}" must be a compile-time constant array`, 'useState([...]) initial must be a literal array of objects
|
|
361
|
-
'runtime values or function calls in the initial.');
|
|
680
|
+
const items = evalStaticOrThrow(initialArgument, scope, `AOT: the initial value of list state "${name}" must be a compile-time constant array`, 'useState([...]) initial must be a literal array of objects, numbers, strings or booleans, or a split ' +
|
|
681
|
+
'of a constant string — no runtime values or other function calls in the initial.');
|
|
362
682
|
// Infer the item struct, pinning any error to the initial value since this pass adds no location.
|
|
363
683
|
let struct;
|
|
364
684
|
try {
|
|
365
|
-
struct = inferItemStruct(items, name);
|
|
685
|
+
struct = inferItemStruct(items, `list state "${name}"`);
|
|
366
686
|
}
|
|
367
687
|
catch (error) {
|
|
368
688
|
if (initialArgument.loc && !error.aotLoc) {
|
|
@@ -370,12 +690,7 @@ export function collectState(functionBody, scope, caps, prefix = '', wide = NO_W
|
|
|
370
690
|
}
|
|
371
691
|
throw error;
|
|
372
692
|
}
|
|
373
|
-
|
|
374
|
-
.filter(field => field.kind === 'string')
|
|
375
|
-
.map((field) => [
|
|
376
|
-
`field "${field.key}" of list state "${name}"`,
|
|
377
|
-
String(item[field.key] ?? ''),
|
|
378
|
-
])), caps, initialArgument);
|
|
693
|
+
checkInitialRows(name, items, caps, initialArgument);
|
|
379
694
|
record = {
|
|
380
695
|
name,
|
|
381
696
|
slotName,
|
|
@@ -384,6 +699,7 @@ export function collectState(functionBody, scope, caps, prefix = '', wide = NO_W
|
|
|
384
699
|
struct,
|
|
385
700
|
items,
|
|
386
701
|
capacity: caps.listCap,
|
|
702
|
+
initialAt: initialArgument?.loc?.start,
|
|
387
703
|
};
|
|
388
704
|
}
|
|
389
705
|
else {
|
|
@@ -398,6 +714,34 @@ export function collectState(functionBody, scope, caps, prefix = '', wide = NO_W
|
|
|
398
714
|
? evalStaticOrThrow(initialArgument, scope, `AOT: the initial value of state "${name}" must be a compile-time constant`, 'useState(x) initial must be a literal or a constant expression (number, string, ' +
|
|
399
715
|
'bool, or arithmetic over consts) — not a runtime value or function call.')
|
|
400
716
|
: 0;
|
|
717
|
+
// Object state → a record, a scalar slot per field, which reads of its name fold through.
|
|
718
|
+
if (!isHost && isObjectTree(initialValue)) {
|
|
719
|
+
let recordState = recordStateOf(name, prefix, setter, initialValue, initialArgument);
|
|
720
|
+
// Objects written with other fields make it a tagged record of several shapes. A reducer writes the
|
|
721
|
+
// objects it returns.
|
|
722
|
+
const written = reducer
|
|
723
|
+
? returnedObjects(reducer.functionNode, scope)
|
|
724
|
+
: setter
|
|
725
|
+
? writtenObjects(functionBody, setter, scope)
|
|
726
|
+
: [];
|
|
727
|
+
const shapes = setter
|
|
728
|
+
? recordShapesOf(name, setter, flattenTree(initialValue), written, reducer ? object => `return ${object}` : undefined)
|
|
729
|
+
: null;
|
|
730
|
+
if (shapes) {
|
|
731
|
+
recordState = tagRecord(recordState, shapes, written, prefix, initialArgument);
|
|
732
|
+
byName.set(`${name}.${shapes.key}`, recordState.tag.state);
|
|
733
|
+
}
|
|
734
|
+
if (reducer)
|
|
735
|
+
recordState.reducer = reducer;
|
|
736
|
+
for (const [key, field] of recordState.fields) {
|
|
737
|
+
byName.set(`${name}.${key}`, field);
|
|
738
|
+
}
|
|
739
|
+
if (setter) {
|
|
740
|
+
bySetter.set(setter, recordState);
|
|
741
|
+
}
|
|
742
|
+
setBinding(scope, name, recordState.value);
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
401
745
|
// A host value must be a finite number; check it directly, since slotKindOfValue maps non-numbers to int.
|
|
402
746
|
if (isHost && !Number.isFinite(initialValue)) {
|
|
403
747
|
throw aotError(`AOT: useHostValue("${name}") must be a number — its initial value evaluated to ` +
|
|
@@ -414,55 +758,11 @@ export function collectState(functionBody, scope, caps, prefix = '', wide = NO_W
|
|
|
414
758
|
}
|
|
415
759
|
throw error;
|
|
416
760
|
}
|
|
417
|
-
|
|
418
|
-
let slotKind = slotKindOfValue(initialValue);
|
|
419
|
-
// Flag booleans: slotKindOfValue makes them int, but text lowering must render false differently from 0.
|
|
420
|
-
const isBoolean = typeof initialValue === 'boolean';
|
|
421
|
-
// A literal written like 70.0 forces a float slot so the state can hold fractions (a smooth drag).
|
|
422
|
-
if (slotKind === 'int' &&
|
|
423
|
-
initialArgument?.type === 'NumericLiteral' &&
|
|
424
|
-
typeof initialArgument.extra?.raw === 'string' &&
|
|
425
|
-
/[.eE]/.test(initialArgument.extra.raw)) {
|
|
426
|
-
slotKind = 'float';
|
|
427
|
-
}
|
|
428
|
-
// A whole number past the int range starts it 64-bit (past 64 bits, a float), as does a setter that
|
|
429
|
-
// stores a timestamp or such a number in it (see compileWidened).
|
|
430
|
-
const initialWidth = slotKind === 'int' ? wideInitial(initialValue) : null;
|
|
431
|
-
if (initialWidth === 'float') {
|
|
432
|
-
slotKind = 'float';
|
|
433
|
-
}
|
|
434
|
-
const widened = slotKind === 'int' && !isBoolean
|
|
435
|
-
? (wide.get(slotName) ??
|
|
436
|
-
(initialWidth === 'number' ? 'number' : undefined))
|
|
437
|
-
: undefined;
|
|
438
|
-
if (widened) {
|
|
439
|
-
slotKind = 'i64';
|
|
440
|
-
}
|
|
441
|
-
// String scalar → a fixed buffer; setters format into it (see scalarAssign).
|
|
442
|
-
const initial = slotKind === 'string' ? String(initialValue) : Number(initialValue);
|
|
443
|
-
if (slotKind === 'float') {
|
|
444
|
-
checkNumber(initial);
|
|
445
|
-
}
|
|
446
|
-
else if (slotKind === 'i64') {
|
|
447
|
-
BigInt(initial); // refuses a value that is not a whole number
|
|
448
|
-
}
|
|
449
|
-
else if (slotKind === 'string') {
|
|
450
|
-
checkInitialFits(name, null, [[`state "${name}"`, String(initial)]], caps, initialArgument);
|
|
451
|
-
}
|
|
452
|
-
record = {
|
|
453
|
-
name,
|
|
454
|
-
slotName,
|
|
455
|
-
setter,
|
|
456
|
-
kind: 'scalar',
|
|
457
|
-
slotKind,
|
|
458
|
-
isBoolean,
|
|
459
|
-
initial,
|
|
460
|
-
host: isHost, // host-fed → also an input the host sets
|
|
461
|
-
...(widened ? { widened } : {}),
|
|
462
|
-
...(bootValue ? { bootValue } : {}),
|
|
463
|
-
};
|
|
761
|
+
record = scalarStateOf(name, slotName, setter, initialValue, initialArgument, isHost, bootValue);
|
|
464
762
|
}
|
|
465
763
|
// Register the state for reads by name and for writes through its setter.
|
|
764
|
+
if (reducer)
|
|
765
|
+
record.reducer = reducer;
|
|
466
766
|
byName.set(name, record);
|
|
467
767
|
if (setter) {
|
|
468
768
|
bySetter.set(setter, record);
|
|
@@ -710,6 +1010,12 @@ export function collectHelpers(componentBody, program) {
|
|
|
710
1010
|
}
|
|
711
1011
|
}
|
|
712
1012
|
}
|
|
1013
|
+
// A block-bodied memo is a helper with no params.
|
|
1014
|
+
for (const [name, functionNode] of memoFunctions(componentBody)) {
|
|
1015
|
+
if (functionNode.body.type === 'BlockStatement') {
|
|
1016
|
+
helpers.set(memoHelperName(name), functionNode);
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
713
1019
|
return helpers;
|
|
714
1020
|
}
|
|
715
1021
|
/**
|
|
@@ -788,7 +1094,7 @@ export function collectSvgImports(program) {
|
|
|
788
1094
|
/**
|
|
789
1095
|
* Bakes a Flow B app's <Svg source> imports → { assetName: artifact } via the SAME baker Flow A's esbuild
|
|
790
1096
|
* .svg loader uses (svgToVector). This is the I/O half (reads .svg files) kept OUT of the pure compilePackage;
|
|
791
|
-
* both CLI entries (aot/compile.mts and the consumer cli.
|
|
1097
|
+
* both CLI entries (aot/compile.mts and the consumer cli.mts) call it and hand the result to options.svgArtifacts.
|
|
792
1098
|
*
|
|
793
1099
|
* @param source App.jsx source text.
|
|
794
1100
|
* @param baseDirectory Directory the import paths are resolved against (the app's dir).
|
|
@@ -864,17 +1170,24 @@ export function collectCallbacks(functionBody) {
|
|
|
864
1170
|
return callbacks;
|
|
865
1171
|
}
|
|
866
1172
|
/**
|
|
867
|
-
*
|
|
1173
|
+
* The name a block-bodied memo is kept under as a helper. No binding can have it.
|
|
1174
|
+
*
|
|
1175
|
+
* @param name The memo's name.
|
|
1176
|
+
*
|
|
1177
|
+
* @returns The helper's name.
|
|
1178
|
+
*/
|
|
1179
|
+
export const memoHelperName = (name) => `useMemo(${name})`;
|
|
1180
|
+
/**
|
|
1181
|
+
* Each `const m = useMemo(() => …, deps)` in a component's body, with its function.
|
|
868
1182
|
*
|
|
869
1183
|
* @param functionBody The component's body.
|
|
870
1184
|
*
|
|
871
|
-
* @returns Each memo's
|
|
1185
|
+
* @returns Each memo's name and function.
|
|
872
1186
|
*/
|
|
873
|
-
|
|
874
|
-
const memos = new Map();
|
|
1187
|
+
function memoFunctions(functionBody) {
|
|
875
1188
|
if (functionBody.type !== 'BlockStatement')
|
|
876
|
-
return
|
|
877
|
-
|
|
1189
|
+
return [];
|
|
1190
|
+
const found = [];
|
|
878
1191
|
for (const statement of functionBody.body) {
|
|
879
1192
|
if (statement.type !== 'VariableDeclaration')
|
|
880
1193
|
continue;
|
|
@@ -884,16 +1197,83 @@ export function collectMemos(functionBody) {
|
|
|
884
1197
|
initializer.callee.name === 'useMemo' &&
|
|
885
1198
|
declarator.id.type === 'Identifier' &&
|
|
886
1199
|
isFn(initializer.arguments[0])) {
|
|
887
|
-
|
|
888
|
-
if (body.type === 'BlockStatement') {
|
|
889
|
-
throw new Error(`AOT: useMemo for "${declarator.id.name}" must be a single expression (for now)`);
|
|
890
|
-
}
|
|
891
|
-
memos.set(declarator.id.name, body);
|
|
1200
|
+
found.push([declarator.id.name, initializer.arguments[0]]);
|
|
892
1201
|
}
|
|
893
1202
|
}
|
|
894
1203
|
}
|
|
1204
|
+
return found;
|
|
1205
|
+
}
|
|
1206
|
+
/**
|
|
1207
|
+
* Collects `const m = useMemo(() => expr, deps)` → Map (name → the memo's expression node). A block body is a call to
|
|
1208
|
+
* the helper collectHelpers keeps it as, so it is worked out as a helper's value is.
|
|
1209
|
+
*
|
|
1210
|
+
* @param functionBody The component's body.
|
|
1211
|
+
*
|
|
1212
|
+
* @returns Each memo's expression, by name.
|
|
1213
|
+
*/
|
|
1214
|
+
export function collectMemos(functionBody) {
|
|
1215
|
+
const memos = new Map();
|
|
1216
|
+
for (const [name, { body }] of memoFunctions(functionBody)) {
|
|
1217
|
+
memos.set(name, body.type === 'BlockStatement'
|
|
1218
|
+
? {
|
|
1219
|
+
type: 'CallExpression',
|
|
1220
|
+
callee: { type: 'Identifier', name: memoHelperName(name) },
|
|
1221
|
+
arguments: [],
|
|
1222
|
+
loc: body.loc,
|
|
1223
|
+
}
|
|
1224
|
+
: body);
|
|
1225
|
+
}
|
|
895
1226
|
return memos;
|
|
896
1227
|
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Collects the `useNativeEvent(Module, 'event', handler)` calls at the top of a component's body. One anywhere else is
|
|
1230
|
+
* refused, as a hook React calls conditionally would be.
|
|
1231
|
+
*
|
|
1232
|
+
* @param functionBody The component's body.
|
|
1233
|
+
*
|
|
1234
|
+
* @returns The calls, in source order.
|
|
1235
|
+
*/
|
|
1236
|
+
export function collectNativeEvents(functionBody) {
|
|
1237
|
+
const isSubscription = (node) => node?.type === 'CallExpression' &&
|
|
1238
|
+
node.callee.type === 'Identifier' &&
|
|
1239
|
+
node.callee.name === 'useNativeEvent';
|
|
1240
|
+
const calls = [];
|
|
1241
|
+
const topLevel = new Set();
|
|
1242
|
+
if (functionBody.type === 'BlockStatement') {
|
|
1243
|
+
for (const statement of functionBody.body) {
|
|
1244
|
+
if (statement.type === 'ExpressionStatement' &&
|
|
1245
|
+
isSubscription(statement.expression)) {
|
|
1246
|
+
calls.push(statement.expression);
|
|
1247
|
+
topLevel.add(statement.expression);
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
// Anywhere else: in a condition, a loop, a handler, or as a value.
|
|
1252
|
+
const refuseNested = (node) => {
|
|
1253
|
+
if (!node || typeof node !== 'object')
|
|
1254
|
+
return;
|
|
1255
|
+
if (Array.isArray(node)) {
|
|
1256
|
+
node.forEach(refuseNested);
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
const record = node;
|
|
1260
|
+
if (typeof record.type !== 'string')
|
|
1261
|
+
return;
|
|
1262
|
+
if (isSubscription(record) && !topLevel.has(record)) {
|
|
1263
|
+
const error = aotError('AOT: useNativeEvent() is called inside a condition, a loop, a function or an expression', 'call it as a statement at the top of the component, as React calls every hook.');
|
|
1264
|
+
if (record.loc)
|
|
1265
|
+
error.aotLoc = record.loc.start;
|
|
1266
|
+
throw error;
|
|
1267
|
+
}
|
|
1268
|
+
for (const key of Object.keys(record)) {
|
|
1269
|
+
if (key !== 'loc' && !key.endsWith('Comments')) {
|
|
1270
|
+
refuseNested(record[key]);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
};
|
|
1274
|
+
refuseNested(functionBody);
|
|
1275
|
+
return calls;
|
|
1276
|
+
}
|
|
897
1277
|
/**
|
|
898
1278
|
* Collects `useEffect(() => {…}, deps)` calls → { fn, deps, node }. Dep validity is checked at compile time.
|
|
899
1279
|
*
|
|
@@ -944,5 +1324,5 @@ export function usesState(functionNode) {
|
|
|
944
1324
|
return false;
|
|
945
1325
|
return functionNode.body.body.some(statement => statement.type === 'VariableDeclaration' &&
|
|
946
1326
|
statement.declarations.some(declarator => declarator.init?.type === 'CallExpression' &&
|
|
947
|
-
declarator.init.callee.name
|
|
1327
|
+
['useState', 'useReducer'].includes(declarator.init.callee.name)));
|
|
948
1328
|
}
|