embedded-react 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/aot/compile.mjs +178 -58
- package/aot/compiler/animations.mjs +290 -281
- package/aot/compiler/boot.mjs +70 -33
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +9 -9
- package/aot/compiler/collect.mjs +324 -205
- package/aot/compiler/control-flow.mjs +209 -197
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image.mjs +51 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +23 -23
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +372 -333
- package/aot/compiler/handlers.mjs +473 -387
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +4 -4
- package/aot/compiler/ir/reads.mjs +19 -19
- package/aot/compiler/ir/types.mjs +14 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/lower.mjs +77 -80
- package/aot/compiler/nodes.mjs +169 -152
- package/aot/compiler/options.mjs +35 -30
- package/aot/compiler/out.mjs +18 -22
- package/aot/compiler/package/animations.mjs +669 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +177 -0
- package/aot/compiler/package/bytecode.mjs +286 -0
- package/aot/compiler/package/checks.mjs +1022 -0
- package/aot/compiler/package/codegen.mjs +1329 -0
- package/aot/compiler/package/enums.mjs +138 -0
- package/aot/compiler/package/format.mjs +760 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +1606 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/tables.mjs +103 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +664 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +21 -21
- package/aot/compiler/static-eval.mjs +25 -23
- package/aot/compiler/style-text.mjs +161 -172
- package/aot/compiler/svg.mjs +477 -251
- package/aot/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +102 -90
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +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 +14 -12
- package/cli.mjs +359 -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 +46 -37
- 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 +28 -20
- 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 +101 -82
- package/src/embedded-react/Animated.js +24 -24
- 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 +6 -6
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +5 -5
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
package/aot/compiler/collect.mjs
CHANGED
|
@@ -19,15 +19,26 @@
|
|
|
19
19
|
import { checkNumber } from './check.mjs';
|
|
20
20
|
import { parse } from '@babel/parser';
|
|
21
21
|
import { readFileSync, existsSync } from 'node:fs';
|
|
22
|
-
import { resolve } from 'node:path';
|
|
23
|
-
import { aotError } from './diagnostics.mjs';
|
|
22
|
+
import { posix, resolve } from 'node:path';
|
|
23
|
+
import { aotError, locateError } from './diagnostics.mjs';
|
|
24
24
|
import { mergeBranchElements } from './branch-merge.mjs';
|
|
25
|
-
import { bootCheckpoint, foldBoot, isBoot, nameBootValues, screenBinding, } from './boot.mjs';
|
|
25
|
+
import { bootCheckpoint, bootSlotOf, buildValueOf, foldBoot, isBoot, nameBootValues, screenBinding, } from './boot.mjs';
|
|
26
26
|
import { evalStaticOrThrow, evalStatic, setBinding, withUndefined, } from './static-eval.mjs';
|
|
27
|
-
import { cConstant, cTypeOfValue } from './c-syntax.mjs';
|
|
28
27
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
29
28
|
- Constants
|
|
30
29
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
30
|
+
/**
|
|
31
|
+
* The kind of slot a constant is stored in: string, float (a fractional number), or int (anything else).
|
|
32
|
+
*
|
|
33
|
+
* @param value A compile-time value.
|
|
34
|
+
*
|
|
35
|
+
* @returns Its slot kind.
|
|
36
|
+
*/
|
|
37
|
+
const slotKindOfValue = (value) => typeof value === 'string'
|
|
38
|
+
? 'string'
|
|
39
|
+
: typeof value === 'number' && !Number.isInteger(value)
|
|
40
|
+
? 'float'
|
|
41
|
+
: 'int';
|
|
31
42
|
/** No state or ref slot widened to 64 bits — the collectors' default. */
|
|
32
43
|
export const NO_WIDE = new Map();
|
|
33
44
|
/**
|
|
@@ -68,8 +79,10 @@ export const isFn = (node) => (node &&
|
|
|
68
79
|
* @returns The App function.
|
|
69
80
|
*/
|
|
70
81
|
export function findComponent(program) {
|
|
71
|
-
for (const
|
|
72
|
-
const declaration =
|
|
82
|
+
for (const statement of program.body) {
|
|
83
|
+
const declaration = statement.type === 'ExportNamedDeclaration'
|
|
84
|
+
? statement.declaration
|
|
85
|
+
: statement;
|
|
73
86
|
if (!declaration)
|
|
74
87
|
continue;
|
|
75
88
|
if (declaration.type === 'FunctionDeclaration' &&
|
|
@@ -77,9 +90,10 @@ export function findComponent(program) {
|
|
|
77
90
|
return declaration;
|
|
78
91
|
}
|
|
79
92
|
if (declaration.type === 'VariableDeclaration') {
|
|
80
|
-
for (const
|
|
81
|
-
if (
|
|
82
|
-
|
|
93
|
+
for (const declarator of declaration.declarations) {
|
|
94
|
+
if (declarator.id?.name === 'App' &&
|
|
95
|
+
isFn(declarator.init)) {
|
|
96
|
+
return declarator.init;
|
|
83
97
|
}
|
|
84
98
|
}
|
|
85
99
|
}
|
|
@@ -117,10 +131,10 @@ export function declaredNames(body) {
|
|
|
117
131
|
}
|
|
118
132
|
};
|
|
119
133
|
// Collect identifiers from each top-level variable declaration, including destructured bindings.
|
|
120
|
-
for (const
|
|
121
|
-
if (
|
|
122
|
-
for (const
|
|
123
|
-
walk(
|
|
134
|
+
for (const statement of body) {
|
|
135
|
+
if (statement.type === 'VariableDeclaration') {
|
|
136
|
+
for (const declarator of statement.declarations) {
|
|
137
|
+
walk(declarator.id);
|
|
124
138
|
}
|
|
125
139
|
}
|
|
126
140
|
}
|
|
@@ -152,19 +166,21 @@ export function moduleScope(program, screen, seed = {}) {
|
|
|
152
166
|
// Seed the scope (image imports as asset names) before folding module consts, so a const can reference them.
|
|
153
167
|
const scope = { screen: screenBinding(screen), ...seed };
|
|
154
168
|
// Fold module variables in order so later ones see earlier ones; skip functions, destructuring and non-folds.
|
|
155
|
-
for (const
|
|
156
|
-
const declaration =
|
|
169
|
+
for (const statement of program.body) {
|
|
170
|
+
const declaration = statement.type === 'ExportNamedDeclaration'
|
|
171
|
+
? statement.declaration
|
|
172
|
+
: statement;
|
|
157
173
|
if (declaration?.type !== 'VariableDeclaration')
|
|
158
174
|
continue;
|
|
159
|
-
for (const
|
|
160
|
-
if (!
|
|
161
|
-
|
|
162
|
-
!
|
|
163
|
-
isFn(
|
|
175
|
+
for (const declarator of declaration.declarations) {
|
|
176
|
+
if (!declarator.id ||
|
|
177
|
+
declarator.id.type !== 'Identifier' ||
|
|
178
|
+
!declarator.init ||
|
|
179
|
+
isFn(declarator.init)) {
|
|
164
180
|
continue;
|
|
165
181
|
}
|
|
166
182
|
try {
|
|
167
|
-
setBinding(scope,
|
|
183
|
+
setBinding(scope, declarator.id.name, bindConst(declarator.id.name, evalStatic(declarator.init, withUndefined(scope), 'boot')));
|
|
168
184
|
}
|
|
169
185
|
catch {
|
|
170
186
|
/* not a static const — skip */
|
|
@@ -174,7 +190,7 @@ export function moduleScope(program, screen, seed = {}) {
|
|
|
174
190
|
return scope;
|
|
175
191
|
}
|
|
176
192
|
/**
|
|
177
|
-
* Infers a
|
|
193
|
+
* Infers a row's fields from a list-state's initial elements (objects of strings/numbers).
|
|
178
194
|
*
|
|
179
195
|
* @param items The folded initial value.
|
|
180
196
|
* @param name The state's name, for errors.
|
|
@@ -206,15 +222,15 @@ function inferItemStruct(items, name) {
|
|
|
206
222
|
return { fields };
|
|
207
223
|
}
|
|
208
224
|
/**
|
|
209
|
-
* Refuses an initial value too big for its fixed buffer:
|
|
225
|
+
* Refuses an initial value too big for its fixed buffer: it would be cut short at boot without a word.
|
|
210
226
|
*
|
|
211
227
|
* @param stateName The state's JS name.
|
|
212
228
|
* @param items The list's initial rows, or null for a scalar.
|
|
213
229
|
* @param strings The initial strings it starts with, as `[what, text]` pairs.
|
|
214
230
|
* @param caps The compile's caps.
|
|
215
|
-
* @param
|
|
231
|
+
* @param initialArgument The initial value, for the error's location.
|
|
216
232
|
*/
|
|
217
|
-
function checkInitialFits(stateName, items, strings, caps,
|
|
233
|
+
function checkInitialFits(stateName, items, strings, caps, initialArgument) {
|
|
218
234
|
let error = null;
|
|
219
235
|
if (items && items.length > caps.listCap) {
|
|
220
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.');
|
|
@@ -227,68 +243,121 @@ function checkInitialFits(stateName, items, strings, caps, initArg) {
|
|
|
227
243
|
`${caps.listStrCap - 1} bytes`, 'raise ER_AOT_LIST_STR_CAP (the listStrCap option), or shorten the string.');
|
|
228
244
|
}
|
|
229
245
|
if (error) {
|
|
230
|
-
if (
|
|
231
|
-
error.aotLoc =
|
|
246
|
+
if (initialArgument?.loc) {
|
|
247
|
+
error.aotLoc = initialArgument.loc.start;
|
|
232
248
|
}
|
|
233
249
|
throw error;
|
|
234
250
|
}
|
|
235
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* A state's initial value, when it is a number or a boolean derived from `screen`. A string stays folded: a closed
|
|
254
|
+
* set of strings is an interned enum, and there are only as many layouts as strings.
|
|
255
|
+
*
|
|
256
|
+
* @param initialArgument The initial value.
|
|
257
|
+
* @param scope Compile-time constants it folds against.
|
|
258
|
+
*
|
|
259
|
+
* @returns The boot value, or null.
|
|
260
|
+
*/
|
|
261
|
+
function bootNumberOf(initialArgument, scope) {
|
|
262
|
+
let value;
|
|
263
|
+
try {
|
|
264
|
+
value = evalStatic(initialArgument, scope, 'boot');
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
if (!isBoot(value) || !value.lowerable)
|
|
270
|
+
return null;
|
|
271
|
+
const built = buildValueOf(value);
|
|
272
|
+
return typeof built === 'number' || typeof built === 'boolean' ? value : null;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Settles each initial value derived from `screen`, once slots can be lowered: boot works it out, typed by its
|
|
276
|
+
* expression so that every panel gets the same slot (`screen.width / 2` is a fraction on every panel, even where it
|
|
277
|
+
* comes out whole). One boot can't work out is taken at the build's panel.
|
|
278
|
+
*
|
|
279
|
+
* @param states The states.
|
|
280
|
+
*/
|
|
281
|
+
export function settleBootInitials(states) {
|
|
282
|
+
for (const state of states) {
|
|
283
|
+
if (state.kind !== 'scalar' || !state.bootValue)
|
|
284
|
+
continue;
|
|
285
|
+
const slot = bootSlotOf(state.bootValue);
|
|
286
|
+
if (slot?.type.kind === 'float') {
|
|
287
|
+
state.slotKind = 'float';
|
|
288
|
+
state.isBoolean = false;
|
|
289
|
+
}
|
|
290
|
+
else if (slot?.type.kind === 'int' || slot?.type.kind === 'bool') {
|
|
291
|
+
state.slotKind = state.widened ? 'i64' : 'int';
|
|
292
|
+
state.isBoolean = slot.type.kind === 'bool';
|
|
293
|
+
}
|
|
294
|
+
if (slot) {
|
|
295
|
+
state.bootInitial = slot;
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
foldBoot(state.bootValue);
|
|
299
|
+
}
|
|
300
|
+
delete state.bootValue;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
236
303
|
/**
|
|
237
304
|
* Collects a component's useState declarations → state descriptors keyed by JS name (reads) and setter
|
|
238
|
-
* name (writes). `prefix` namespaces the
|
|
239
|
-
* lookup keys stay the bare JS names (`count`), but the
|
|
305
|
+
* name (writes). `prefix` namespaces the STORAGE so each inlined child instance gets its own slots: the
|
|
306
|
+
* lookup keys stay the bare JS names (`count`), but the field / array / count derive from `slotName`
|
|
240
307
|
* (`<prefix>count`). prefix='' (the App) keeps the storage names exactly the JS names.
|
|
241
308
|
* `caps` are the compiler's resolved caps (see resolveOptions); a list state takes its capacity from it.
|
|
242
309
|
*
|
|
243
|
-
* @param
|
|
310
|
+
* @param functionBody The component's body.
|
|
244
311
|
* @param scope Compile-time constants the initial values fold against.
|
|
245
312
|
* @param caps The compile's caps.
|
|
246
|
-
* @param prefix The
|
|
313
|
+
* @param prefix The storage prefix: none for the App, `c<n>_` for an inlined instance.
|
|
247
314
|
* @param wide Slots to declare 64-bit (see compileWidened).
|
|
248
315
|
*
|
|
249
316
|
* @returns The component's state table.
|
|
250
317
|
*/
|
|
251
|
-
export function collectState(
|
|
318
|
+
export function collectState(functionBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
252
319
|
// Index each state by its JS name and, when it has one, by its setter's name.
|
|
253
320
|
const byName = new Map();
|
|
254
321
|
const bySetter = new Map();
|
|
255
322
|
// Scan the body's top-level declarations for useState and useHostValue calls.
|
|
256
|
-
for (const
|
|
257
|
-
if (
|
|
323
|
+
for (const statement of functionBody.body) {
|
|
324
|
+
if (statement.type !== 'VariableDeclaration')
|
|
258
325
|
continue;
|
|
259
326
|
// Inspect each declaration in the statement so multiple hook bindings in one `const` are collected independently.
|
|
260
|
-
for (const
|
|
327
|
+
for (const declarator of statement.declarations) {
|
|
261
328
|
// Only hook-style call declarations can define collected state; plain variables and other initializers are skipped.
|
|
262
|
-
const
|
|
263
|
-
if (
|
|
329
|
+
const initializer = declarator.init;
|
|
330
|
+
if (initializer?.type !== 'CallExpression')
|
|
264
331
|
continue;
|
|
265
|
-
// useHostValue is a host-fed scalar:
|
|
266
|
-
const isHost =
|
|
267
|
-
if (
|
|
332
|
+
// useHostValue is a host-fed scalar: a state slot like useState, written only by the host through its input.
|
|
333
|
+
const isHost = initializer.callee.name === 'useHostValue';
|
|
334
|
+
if (initializer.callee.name !== 'useState' && !isHost) {
|
|
268
335
|
continue;
|
|
269
336
|
}
|
|
270
337
|
// Accept useHostValue(name) as a direct scalar binding, while useState must keep the usual [value, setter] tuple pattern.
|
|
271
|
-
if (isHost
|
|
338
|
+
if (isHost
|
|
339
|
+
? declarator.id.type !== 'Identifier'
|
|
340
|
+
: declarator.id.type !== 'ArrayPattern') {
|
|
272
341
|
continue;
|
|
273
342
|
}
|
|
274
343
|
// Derive the read name and optional setter from the binding shape: host values bind directly, while useState binds [value, setter].
|
|
275
344
|
const name = isHost
|
|
276
|
-
?
|
|
277
|
-
:
|
|
345
|
+
? declarator.id.name
|
|
346
|
+
: declarator.id.elements[0]
|
|
278
347
|
?.name;
|
|
279
348
|
const setter = isHost
|
|
280
349
|
? undefined
|
|
281
|
-
:
|
|
350
|
+
: declarator.id.elements[1]
|
|
282
351
|
?.name;
|
|
283
352
|
if (!name)
|
|
284
353
|
continue;
|
|
285
354
|
// Build either a list-state record from an array initializer or a scalar record from the folded initial value.
|
|
286
|
-
const
|
|
287
|
-
const
|
|
355
|
+
const slotName = prefix + name; // storage name (== name for the App; instance-unique for a child)
|
|
356
|
+
const initialArgument = initializer.arguments[0];
|
|
288
357
|
let record;
|
|
289
|
-
if (!isHost &&
|
|
290
|
-
// List state → a fixed-capacity
|
|
291
|
-
const items = evalStaticOrThrow(
|
|
358
|
+
if (!isHost && initialArgument?.type === 'ArrayExpression') {
|
|
359
|
+
// 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/numbers/strings — no ' +
|
|
292
361
|
'runtime values or function calls in the initial.');
|
|
293
362
|
// Infer the item struct, pinning any error to the initial value since this pass adds no location.
|
|
294
363
|
let struct;
|
|
@@ -296,8 +365,8 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
296
365
|
struct = inferItemStruct(items, name);
|
|
297
366
|
}
|
|
298
367
|
catch (error) {
|
|
299
|
-
if (
|
|
300
|
-
error.aotLoc =
|
|
368
|
+
if (initialArgument.loc && !error.aotLoc) {
|
|
369
|
+
error.aotLoc = initialArgument.loc.start; // collectState isn't withLoc-wrapped
|
|
301
370
|
}
|
|
302
371
|
throw error;
|
|
303
372
|
}
|
|
@@ -306,88 +375,91 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
306
375
|
.map((field) => [
|
|
307
376
|
`field "${field.key}" of list state "${name}"`,
|
|
308
377
|
String(item[field.key] ?? ''),
|
|
309
|
-
])), caps,
|
|
378
|
+
])), caps, initialArgument);
|
|
310
379
|
record = {
|
|
311
380
|
name,
|
|
312
|
-
|
|
381
|
+
slotName,
|
|
313
382
|
setter,
|
|
314
383
|
kind: 'list',
|
|
315
384
|
struct,
|
|
316
385
|
items,
|
|
317
|
-
|
|
318
|
-
cTypeName: `ErItem_${cField}`,
|
|
319
|
-
arrayName: `s_${cField}`,
|
|
320
|
-
countMember: `s_${cField}_count`,
|
|
386
|
+
capacity: caps.listCap,
|
|
321
387
|
};
|
|
322
388
|
}
|
|
323
389
|
else {
|
|
324
|
-
// Scalar state → one
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
:
|
|
329
|
-
|
|
330
|
-
|
|
390
|
+
// Scalar state → one slot. Fold the initial value; no argument starts it at 0. A number derived
|
|
391
|
+
// from `screen` is worked out at boot (see settleBootInitials), so it doesn't take a layout per value.
|
|
392
|
+
const bootValue = !isHost && initialArgument
|
|
393
|
+
? bootNumberOf(initialArgument, scope)
|
|
394
|
+
: null;
|
|
395
|
+
const initialValue = bootValue
|
|
396
|
+
? buildValueOf(bootValue)
|
|
397
|
+
: initialArgument
|
|
398
|
+
? 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
|
+
'bool, or arithmetic over consts) — not a runtime value or function call.')
|
|
400
|
+
: 0;
|
|
401
|
+
// A host value must be a finite number; check it directly, since slotKindOfValue maps non-numbers to int.
|
|
402
|
+
if (isHost && !Number.isFinite(initialValue)) {
|
|
331
403
|
throw aotError(`AOT: useHostValue("${name}") must be a number — its initial value evaluated to ` +
|
|
332
|
-
`${JSON.stringify(
|
|
404
|
+
`${JSON.stringify(initialValue)}`, 'useHostValue(0) / useHostValue(0.0) — the host feeds an int or float; booleans, ' +
|
|
333
405
|
'strings, and objects are not supported.');
|
|
334
406
|
}
|
|
335
|
-
// Refuse undefined, null and non-finite initials here; they would fold into
|
|
336
|
-
if (
|
|
337
|
-
|
|
338
|
-
(typeof
|
|
339
|
-
const error = aotError(`AOT: the initial value of state "${name}" is ${
|
|
340
|
-
if (
|
|
341
|
-
error.aotLoc =
|
|
407
|
+
// Refuse undefined, null and non-finite initials here; they would fold into nonsense with no location.
|
|
408
|
+
if (initialValue === undefined ||
|
|
409
|
+
initialValue === null ||
|
|
410
|
+
(typeof initialValue === 'number' && !Number.isFinite(initialValue))) {
|
|
411
|
+
const error = aotError(`AOT: the initial value of state "${name}" is ${initialValue === undefined ? 'undefined' : String(initialValue)}`, "give it a concrete starting value: useState(0), useState(''), useState(false).");
|
|
412
|
+
if (initialArgument?.loc) {
|
|
413
|
+
error.aotLoc = initialArgument.loc.start;
|
|
342
414
|
}
|
|
343
415
|
throw error;
|
|
344
416
|
}
|
|
345
|
-
// Pick the slot's
|
|
346
|
-
let
|
|
347
|
-
// Flag booleans:
|
|
348
|
-
const
|
|
417
|
+
// Pick the slot's kind from the initial value.
|
|
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';
|
|
349
421
|
// A literal written like 70.0 forces a float slot so the state can hold fractions (a smooth drag).
|
|
350
|
-
if (
|
|
351
|
-
|
|
352
|
-
typeof
|
|
353
|
-
/[.eE]/.test(
|
|
354
|
-
|
|
422
|
+
if (slotKind === 'int' &&
|
|
423
|
+
initialArgument?.type === 'NumericLiteral' &&
|
|
424
|
+
typeof initialArgument.extra?.raw === 'string' &&
|
|
425
|
+
/[.eE]/.test(initialArgument.extra.raw)) {
|
|
426
|
+
slotKind = 'float';
|
|
355
427
|
}
|
|
356
428
|
// A whole number past the int range starts it 64-bit (past 64 bits, a float), as does a setter that
|
|
357
429
|
// stores a timestamp or such a number in it (see compileWidened).
|
|
358
|
-
const initialWidth =
|
|
430
|
+
const initialWidth = slotKind === 'int' ? wideInitial(initialValue) : null;
|
|
359
431
|
if (initialWidth === 'float') {
|
|
360
|
-
|
|
432
|
+
slotKind = 'float';
|
|
361
433
|
}
|
|
362
|
-
const widened =
|
|
363
|
-
? (wide.get(
|
|
434
|
+
const widened = slotKind === 'int' && !isBoolean
|
|
435
|
+
? (wide.get(slotName) ??
|
|
364
436
|
(initialWidth === 'number' ? 'number' : undefined))
|
|
365
437
|
: undefined;
|
|
366
438
|
if (widened) {
|
|
367
|
-
|
|
439
|
+
slotKind = 'i64';
|
|
368
440
|
}
|
|
369
|
-
// String scalar → a fixed
|
|
370
|
-
const initial =
|
|
371
|
-
if (
|
|
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') {
|
|
372
444
|
checkNumber(initial);
|
|
373
445
|
}
|
|
374
|
-
else if (
|
|
375
|
-
|
|
446
|
+
else if (slotKind === 'i64') {
|
|
447
|
+
BigInt(initial); // refuses a value that is not a whole number
|
|
376
448
|
}
|
|
377
|
-
else if (
|
|
378
|
-
checkInitialFits(name, null, [[`state "${name}"`, String(initial)]], caps,
|
|
449
|
+
else if (slotKind === 'string') {
|
|
450
|
+
checkInitialFits(name, null, [[`state "${name}"`, String(initial)]], caps, initialArgument);
|
|
379
451
|
}
|
|
380
452
|
record = {
|
|
381
453
|
name,
|
|
382
|
-
|
|
454
|
+
slotName,
|
|
383
455
|
setter,
|
|
384
456
|
kind: 'scalar',
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
cMember: `s_state.${cField}`,
|
|
457
|
+
slotKind,
|
|
458
|
+
isBoolean,
|
|
388
459
|
initial,
|
|
389
|
-
host: isHost, // host-fed → also
|
|
460
|
+
host: isHost, // host-fed → also an input the host sets
|
|
390
461
|
...(widened ? { widened } : {}),
|
|
462
|
+
...(bootValue ? { bootValue } : {}),
|
|
391
463
|
};
|
|
392
464
|
}
|
|
393
465
|
// Register the state for reads by name and for writes through its setter.
|
|
@@ -404,21 +476,21 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
404
476
|
* responsive layout returns the branch for this build's screen. When the test depends on the panel and both
|
|
405
477
|
* outcomes return elements of one shape, they merge into one element whose values boot picks.
|
|
406
478
|
*
|
|
407
|
-
* @param
|
|
479
|
+
* @param functionBody The component's body.
|
|
408
480
|
* @param scope Compile-time constants the `if` tests fold against.
|
|
409
481
|
*
|
|
410
482
|
* @returns The returned element.
|
|
411
483
|
*/
|
|
412
|
-
export function findReturnJSX(
|
|
484
|
+
export function findReturnJSX(functionBody, scope = {}) {
|
|
413
485
|
const statementsOf = (branch) => branch.type === 'BlockStatement' ? branch.body : [branch];
|
|
414
486
|
// Fold top-level `if (staticCond) return …` at compile time — responsive layouts switch on `screen`.
|
|
415
487
|
const scan = (statements) => {
|
|
416
|
-
for (const [index,
|
|
488
|
+
for (const [index, statement] of statements.entries()) {
|
|
417
489
|
// A top-level `if` must fold; only the branch it takes is scanned.
|
|
418
|
-
if (
|
|
490
|
+
if (statement.type === 'IfStatement') {
|
|
419
491
|
let testValue;
|
|
420
492
|
try {
|
|
421
|
-
testValue = evalStatic(
|
|
493
|
+
testValue = evalStatic(statement.test, scope, 'boot');
|
|
422
494
|
}
|
|
423
495
|
catch {
|
|
424
496
|
throw new Error('AOT: a top-level `if` in the component must have a compile-time-constant test (e.g. ' +
|
|
@@ -426,13 +498,13 @@ export function findReturnJSX(fnBody, scope = {}) {
|
|
|
426
498
|
}
|
|
427
499
|
// A test that depends on the panel needs no variant when both outcomes have one shape.
|
|
428
500
|
if (isBoot(testValue)) {
|
|
429
|
-
const merged = mergeOutcomes(
|
|
501
|
+
const merged = mergeOutcomes(statement, statements.slice(index + 1));
|
|
430
502
|
if (merged)
|
|
431
503
|
return merged;
|
|
432
504
|
testValue = foldBoot(testValue);
|
|
433
505
|
}
|
|
434
506
|
// Follow only the statically selected branch and keep scanning it until a JSX return is found.
|
|
435
|
-
const branch = testValue ?
|
|
507
|
+
const branch = testValue ? statement.consequent : statement.alternate;
|
|
436
508
|
if (branch) {
|
|
437
509
|
const branchJSX = scan(statementsOf(branch));
|
|
438
510
|
if (branchJSX)
|
|
@@ -441,22 +513,51 @@ export function findReturnJSX(fnBody, scope = {}) {
|
|
|
441
513
|
continue;
|
|
442
514
|
}
|
|
443
515
|
// A `return` with a value ends the scan and must return a single JSX element.
|
|
444
|
-
if (
|
|
445
|
-
|
|
446
|
-
return stmt.argument;
|
|
447
|
-
throw new Error(`AOT: the component must return a single JSX element (got ${stmt.argument.type})`);
|
|
516
|
+
if (statement.type === 'ReturnStatement' && statement.argument) {
|
|
517
|
+
return returned(statement.argument);
|
|
448
518
|
}
|
|
449
519
|
}
|
|
450
520
|
return null;
|
|
451
521
|
};
|
|
522
|
+
// The element a `return` gives: a ternary on the panel is decided as a top-level `if` is.
|
|
523
|
+
const returned = (argument) => {
|
|
524
|
+
if (argument.type === 'JSXElement')
|
|
525
|
+
return argument;
|
|
526
|
+
if (argument.type !== 'ConditionalExpression') {
|
|
527
|
+
throw new Error(`AOT: the component must return a single JSX element (got ${argument.type})`);
|
|
528
|
+
}
|
|
529
|
+
let testValue;
|
|
530
|
+
try {
|
|
531
|
+
testValue = evalStatic(argument.test, scope, 'boot');
|
|
532
|
+
}
|
|
533
|
+
catch {
|
|
534
|
+
throw new Error('AOT: a ternary the component returns must have a compile-time-constant test (e.g. on the `screen` ' +
|
|
535
|
+
'global) — runtime layout branching is not supported');
|
|
536
|
+
}
|
|
537
|
+
if (isBoot(testValue)) {
|
|
538
|
+
const restore = bootCheckpoint();
|
|
539
|
+
try {
|
|
540
|
+
const merged = mergeBranchElements(argument.test, returned(argument.consequent), returned(argument.alternate));
|
|
541
|
+
if (merged)
|
|
542
|
+
return merged;
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
/* an outcome that does not compile here is left to the fold, which reports it if it is taken */
|
|
546
|
+
}
|
|
547
|
+
restore();
|
|
548
|
+
testValue = foldBoot(testValue);
|
|
549
|
+
}
|
|
550
|
+
return returned(testValue ? argument.consequent : argument.alternate);
|
|
551
|
+
};
|
|
452
552
|
// What each outcome of an `if` returns (a branch that does not return falls through), merged when it can be.
|
|
453
|
-
const mergeOutcomes = (
|
|
553
|
+
const mergeOutcomes = (statement, rest) => {
|
|
454
554
|
const restore = bootCheckpoint();
|
|
455
555
|
try {
|
|
456
|
-
const whenTrue = scan(statementsOf(
|
|
457
|
-
const whenFalse = (
|
|
556
|
+
const whenTrue = scan(statementsOf(statement.consequent)) ?? scan(rest);
|
|
557
|
+
const whenFalse = (statement.alternate && scan(statementsOf(statement.alternate))) ??
|
|
558
|
+
scan(rest);
|
|
458
559
|
const merged = whenTrue && whenFalse
|
|
459
|
-
? mergeBranchElements(
|
|
560
|
+
? mergeBranchElements(statement.test, whenTrue, whenFalse)
|
|
460
561
|
: null;
|
|
461
562
|
if (merged)
|
|
462
563
|
return merged;
|
|
@@ -468,7 +569,7 @@ export function findReturnJSX(fnBody, scope = {}) {
|
|
|
468
569
|
return null;
|
|
469
570
|
};
|
|
470
571
|
// Scan the body; a component that never returns is an error.
|
|
471
|
-
const returnJSX = scan(
|
|
572
|
+
const returnJSX = scan(functionBody.body);
|
|
472
573
|
if (!returnJSX) {
|
|
473
574
|
throw new Error('AOT: component has no return statement');
|
|
474
575
|
}
|
|
@@ -477,28 +578,28 @@ export function findReturnJSX(fnBody, scope = {}) {
|
|
|
477
578
|
/**
|
|
478
579
|
* Returns the JSX a function component returns (arrow expression body or a block's return).
|
|
479
580
|
*
|
|
480
|
-
* @param
|
|
581
|
+
* @param functionNode The component.
|
|
481
582
|
* @param scope Compile-time constants its top-level `if` tests fold against.
|
|
482
583
|
*
|
|
483
584
|
* @returns The returned element.
|
|
484
585
|
*/
|
|
485
|
-
export function componentReturnJSX(
|
|
486
|
-
if (
|
|
487
|
-
return
|
|
488
|
-
if (
|
|
489
|
-
return findReturnJSX(
|
|
586
|
+
export function componentReturnJSX(functionNode, scope = {}) {
|
|
587
|
+
if (functionNode.body.type === 'JSXElement')
|
|
588
|
+
return functionNode.body;
|
|
589
|
+
if (functionNode.body.type === 'BlockStatement')
|
|
590
|
+
return findReturnJSX(functionNode.body, scope);
|
|
490
591
|
throw new Error('AOT: component body must return a JSX element');
|
|
491
592
|
}
|
|
492
593
|
/**
|
|
493
594
|
* Whether a function returns JSX (a component) rather than a value (a helper).
|
|
494
595
|
*
|
|
495
|
-
* @param
|
|
596
|
+
* @param functionNode The function.
|
|
496
597
|
*
|
|
497
598
|
* @returns Whether its body is JSX or has a top-level `return <…/>`.
|
|
498
599
|
*/
|
|
499
|
-
const fnReturnsJSX = (
|
|
500
|
-
(
|
|
501
|
-
|
|
600
|
+
const fnReturnsJSX = (functionNode) => functionNode.body.type === 'JSXElement' ||
|
|
601
|
+
(functionNode.body.type === 'BlockStatement' &&
|
|
602
|
+
functionNode.body.body.some(statement => statement.type === 'ReturnStatement' &&
|
|
502
603
|
statement.argument?.type === 'JSXElement'));
|
|
503
604
|
/**
|
|
504
605
|
* Resolves a component definition expression to its function node, unwrapping memo(fn) / React.memo(fn).
|
|
@@ -529,8 +630,10 @@ function asComponentFn(node) {
|
|
|
529
630
|
*/
|
|
530
631
|
export function collectComponents(program) {
|
|
531
632
|
const components = new Map();
|
|
532
|
-
for (const
|
|
533
|
-
const declaration =
|
|
633
|
+
for (const statement of program.body) {
|
|
634
|
+
const declaration = statement.type === 'ExportNamedDeclaration'
|
|
635
|
+
? statement.declaration
|
|
636
|
+
: statement;
|
|
534
637
|
if (!declaration)
|
|
535
638
|
continue;
|
|
536
639
|
// `function Card() { return <…/>; }`
|
|
@@ -542,12 +645,13 @@ export function collectComponents(program) {
|
|
|
542
645
|
}
|
|
543
646
|
// `const Card = (…) => <…/>`, optionally wrapped in memo(…).
|
|
544
647
|
if (declaration.type === 'VariableDeclaration') {
|
|
545
|
-
for (const
|
|
546
|
-
if (
|
|
648
|
+
for (const declarator of declaration.declarations) {
|
|
649
|
+
if (declarator.id?.type !== 'Identifier' ||
|
|
650
|
+
declarator.id.name === 'App')
|
|
547
651
|
continue;
|
|
548
|
-
const
|
|
549
|
-
if (
|
|
550
|
-
components.set(
|
|
652
|
+
const functionNode = asComponentFn(declarator.init); // unwrap memo(...)
|
|
653
|
+
if (functionNode && fnReturnsJSX(functionNode)) {
|
|
654
|
+
components.set(declarator.id.name, functionNode);
|
|
551
655
|
}
|
|
552
656
|
}
|
|
553
657
|
}
|
|
@@ -568,35 +672,40 @@ export function collectComponents(program) {
|
|
|
568
672
|
export function collectHelpers(componentBody, program) {
|
|
569
673
|
// Record a binding as a helper when it holds a function that does not return JSX.
|
|
570
674
|
const helpers = new Map();
|
|
571
|
-
const addHelper = (name,
|
|
572
|
-
if (name &&
|
|
573
|
-
|
|
675
|
+
const addHelper = (name, functionNode) => {
|
|
676
|
+
if (name &&
|
|
677
|
+
name !== 'App' &&
|
|
678
|
+
isFn(functionNode) &&
|
|
679
|
+
!fnReturnsJSX(functionNode)) {
|
|
680
|
+
helpers.set(name, functionNode);
|
|
574
681
|
}
|
|
575
682
|
};
|
|
576
683
|
// Module-level function declarations and function-valued variables.
|
|
577
|
-
for (const
|
|
578
|
-
const declaration =
|
|
684
|
+
for (const statement of program.body) {
|
|
685
|
+
const declaration = statement.type === 'ExportNamedDeclaration'
|
|
686
|
+
? statement.declaration
|
|
687
|
+
: statement;
|
|
579
688
|
if (!declaration)
|
|
580
689
|
continue;
|
|
581
690
|
if (declaration.type === 'FunctionDeclaration' && declaration.id) {
|
|
582
691
|
addHelper(declaration.id.name, declaration);
|
|
583
692
|
}
|
|
584
693
|
if (declaration.type === 'VariableDeclaration') {
|
|
585
|
-
for (const
|
|
586
|
-
if (
|
|
587
|
-
addHelper(
|
|
694
|
+
for (const declarator of declaration.declarations) {
|
|
695
|
+
if (declarator.id?.type === 'Identifier') {
|
|
696
|
+
addHelper(declarator.id.name, declarator.init);
|
|
588
697
|
}
|
|
589
698
|
}
|
|
590
699
|
}
|
|
591
700
|
}
|
|
592
701
|
// The component's own top-level arrows and functions (a hook call is not a function, so it is skipped).
|
|
593
702
|
if (componentBody.type === 'BlockStatement') {
|
|
594
|
-
for (const
|
|
595
|
-
if (
|
|
703
|
+
for (const statement of componentBody.body) {
|
|
704
|
+
if (statement.type !== 'VariableDeclaration')
|
|
596
705
|
continue;
|
|
597
|
-
for (const
|
|
598
|
-
if (
|
|
599
|
-
addHelper(
|
|
706
|
+
for (const declarator of statement.declarations) {
|
|
707
|
+
if (declarator.id?.type === 'Identifier') {
|
|
708
|
+
addHelper(declarator.id.name, declarator.init);
|
|
600
709
|
}
|
|
601
710
|
}
|
|
602
711
|
}
|
|
@@ -607,7 +716,8 @@ export function collectHelpers(componentBody, program) {
|
|
|
607
716
|
* Collects image imports — `import wxSun from './assets/wx_sun.png'` → Map (local → { name, importPath }).
|
|
608
717
|
* `name` is the file's basename without extension (the asset key `<Image source>` resolves to and that the
|
|
609
718
|
* Flow A bundler also uses); `importPath` is the source-relative path the CLI bakes from. Mirrors the Flow A
|
|
610
|
-
* esbuild asset plugin so the same `import → basename` convention holds in both flows.
|
|
719
|
+
* esbuild asset plugin so the same `import → basename` convention holds in both flows. Two different files of
|
|
720
|
+
* one name would be one asset, so they are refused.
|
|
611
721
|
*
|
|
612
722
|
* @param program The parsed module.
|
|
613
723
|
*
|
|
@@ -616,16 +726,25 @@ export function collectHelpers(componentBody, program) {
|
|
|
616
726
|
export function collectImageImports(program) {
|
|
617
727
|
// Map each default import of an image file to the file's basename.
|
|
618
728
|
const byLocal = new Map();
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
729
|
+
const pathOf = new Map(); // name → the file it names
|
|
730
|
+
const normalized = (path) => posix.normalize(path.replace(/\\/g, '/'));
|
|
731
|
+
for (const statement of program.body) {
|
|
732
|
+
if (statement.type !== 'ImportDeclaration' ||
|
|
733
|
+
typeof statement.source.value !== 'string') {
|
|
622
734
|
continue;
|
|
623
735
|
}
|
|
624
|
-
const importPath =
|
|
736
|
+
const importPath = statement.source.value;
|
|
625
737
|
if (!IMAGE_EXT_RE.test(importPath))
|
|
626
738
|
continue;
|
|
627
739
|
const name = importPath.split(/[\\/]/).pop().replace(IMAGE_EXT_RE, '');
|
|
628
|
-
|
|
740
|
+
const first = pathOf.get(name);
|
|
741
|
+
if (first !== undefined && normalized(first) !== normalized(importPath)) {
|
|
742
|
+
const error = aotError(`AOT: two images are named "${name}" (${first} and ${importPath})`, 'an image is named by its file name, so rename one of the files.');
|
|
743
|
+
locateError(error, statement.loc?.start);
|
|
744
|
+
throw error;
|
|
745
|
+
}
|
|
746
|
+
pathOf.set(name, importPath);
|
|
747
|
+
for (const specifier of statement.specifiers) {
|
|
629
748
|
if (specifier.type === 'ImportDefaultSpecifier') {
|
|
630
749
|
byLocal.set(specifier.local.name, { name, importPath });
|
|
631
750
|
}
|
|
@@ -637,7 +756,7 @@ export function collectImageImports(program) {
|
|
|
637
756
|
* Collects `import x from './foo.svg'` → Map(localName → { name, importPath }). Unlike an image (whose
|
|
638
757
|
* bytes the CLI bakes AFTER compile, so compile only needs its asset name), an <Svg source> needs the
|
|
639
758
|
* GEOMETRY during compile — so the CLI bakes the .svg to a vector artifact up front (bakeSvgArtifacts) and
|
|
640
|
-
* passes it in as
|
|
759
|
+
* passes it in as options.svgArtifacts, keyed by `name`. emitSvgSource resolves the local → name → artifact.
|
|
641
760
|
*
|
|
642
761
|
* @param program The parsed module.
|
|
643
762
|
*
|
|
@@ -646,19 +765,19 @@ export function collectImageImports(program) {
|
|
|
646
765
|
export function collectSvgImports(program) {
|
|
647
766
|
// Map each default import of a .svg file to the file's basename.
|
|
648
767
|
const byLocal = new Map();
|
|
649
|
-
for (const
|
|
650
|
-
if (
|
|
651
|
-
typeof
|
|
768
|
+
for (const statement of program.body) {
|
|
769
|
+
if (statement.type !== 'ImportDeclaration' ||
|
|
770
|
+
typeof statement.source.value !== 'string') {
|
|
652
771
|
continue;
|
|
653
772
|
}
|
|
654
|
-
const importPath =
|
|
773
|
+
const importPath = statement.source.value;
|
|
655
774
|
if (!/\.svg$/i.test(importPath))
|
|
656
775
|
continue;
|
|
657
776
|
const name = importPath
|
|
658
777
|
.split(/[\\/]/)
|
|
659
778
|
.pop()
|
|
660
779
|
.replace(/\.svg$/i, '');
|
|
661
|
-
for (const specifier of
|
|
780
|
+
for (const specifier of statement.specifiers) {
|
|
662
781
|
if (specifier.type === 'ImportDefaultSpecifier') {
|
|
663
782
|
byLocal.set(specifier.local.name, { name, importPath });
|
|
664
783
|
}
|
|
@@ -668,17 +787,17 @@ export function collectSvgImports(program) {
|
|
|
668
787
|
}
|
|
669
788
|
/**
|
|
670
789
|
* Bakes a Flow B app's <Svg source> imports → { assetName: artifact } via the SAME baker Flow A's esbuild
|
|
671
|
-
* .svg loader uses (svgToVector). This is the I/O half (reads .svg files) kept OUT of the pure
|
|
672
|
-
* both CLI entries (aot/compile.mts and the consumer cli.mjs) call it and hand the result to
|
|
790
|
+
* .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.mjs) call it and hand the result to options.svgArtifacts.
|
|
673
792
|
*
|
|
674
|
-
* @param
|
|
675
|
-
* @param
|
|
793
|
+
* @param source App.jsx source text.
|
|
794
|
+
* @param baseDirectory Directory the import paths are resolved against (the app's dir).
|
|
676
795
|
*
|
|
677
796
|
* @returns Each import's artifact, by asset name: its op-tape, or a PNG for one the vector baker cannot draw.
|
|
678
797
|
*/
|
|
679
|
-
export async function bakeSvgArtifacts(
|
|
798
|
+
export async function bakeSvgArtifacts(source, baseDirectory) {
|
|
680
799
|
// Parse with the TS plugin too (harmless for plain JSX), so a .tsx app's <Svg source> imports are found.
|
|
681
|
-
const program = parse(
|
|
800
|
+
const program = parse(source, {
|
|
682
801
|
sourceType: 'module',
|
|
683
802
|
plugins: ['jsx', 'typescript'],
|
|
684
803
|
}).program;
|
|
@@ -690,7 +809,7 @@ export async function bakeSvgArtifacts(src, baseDir) {
|
|
|
690
809
|
// Read each imported file and bake it to a vector op-tape.
|
|
691
810
|
const artifacts = {};
|
|
692
811
|
for (const [, svgImport] of imports) {
|
|
693
|
-
const svgPath = resolve(
|
|
812
|
+
const svgPath = resolve(baseDirectory, svgImport.importPath);
|
|
694
813
|
if (!existsSync(svgPath)) {
|
|
695
814
|
throw new Error(`AOT: <Svg source> asset "${svgImport.name}" not found at ${svgPath}`);
|
|
696
815
|
}
|
|
@@ -699,7 +818,7 @@ export async function bakeSvgArtifacts(src, baseDir) {
|
|
|
699
818
|
const artifact = (await svgToVector(svgText));
|
|
700
819
|
if (artifact.dropped && artifact.dropped.length) {
|
|
701
820
|
console.warn(`embedded-react: ${svgImport.name}.svg uses unsupported SVG feature(s) [${artifact.dropped.join(', ')}] — ` +
|
|
702
|
-
`
|
|
821
|
+
`rasterising it as a fallback image (Flow B bakes the PNG into the package). Simplify the SVG ` +
|
|
703
822
|
`to keep it a live vector.`);
|
|
704
823
|
const { width, height, png } = await svgToRaster(svgText);
|
|
705
824
|
artifacts[svgImport.name] = {
|
|
@@ -718,27 +837,27 @@ export async function bakeSvgArtifacts(src, baseDir) {
|
|
|
718
837
|
}
|
|
719
838
|
/**
|
|
720
839
|
* Collects `const fn = useCallback((...) => {...}, deps)` → Map (name → arrow fn node). Deps are ignored:
|
|
721
|
-
* the AOT re-renders via its own dependency tracking, so useCallback only names a shared
|
|
840
|
+
* the AOT re-renders via its own dependency tracking, so useCallback only names a shared handler.
|
|
722
841
|
*
|
|
723
|
-
* @param
|
|
842
|
+
* @param functionBody The component's body.
|
|
724
843
|
*
|
|
725
844
|
* @returns The callbacks, by name.
|
|
726
845
|
*/
|
|
727
|
-
export function collectCallbacks(
|
|
846
|
+
export function collectCallbacks(functionBody) {
|
|
728
847
|
const callbacks = new Map();
|
|
729
|
-
if (
|
|
848
|
+
if (functionBody.type !== 'BlockStatement')
|
|
730
849
|
return callbacks;
|
|
731
850
|
// Collect top-level useCallback bindings whose first argument is an inline function, keyed by the variable name.
|
|
732
|
-
for (const
|
|
733
|
-
if (
|
|
851
|
+
for (const statement of functionBody.body) {
|
|
852
|
+
if (statement.type !== 'VariableDeclaration')
|
|
734
853
|
continue;
|
|
735
|
-
for (const
|
|
736
|
-
const
|
|
737
|
-
if (
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
isFn(
|
|
741
|
-
callbacks.set(
|
|
854
|
+
for (const declarator of statement.declarations) {
|
|
855
|
+
const initializer = declarator.init;
|
|
856
|
+
if (initializer?.type === 'CallExpression' &&
|
|
857
|
+
initializer.callee.name === 'useCallback' &&
|
|
858
|
+
declarator.id.type === 'Identifier' &&
|
|
859
|
+
isFn(initializer.arguments[0])) {
|
|
860
|
+
callbacks.set(declarator.id.name, initializer.arguments[0]);
|
|
742
861
|
}
|
|
743
862
|
}
|
|
744
863
|
}
|
|
@@ -747,29 +866,29 @@ export function collectCallbacks(fnBody) {
|
|
|
747
866
|
/**
|
|
748
867
|
* Collects `const m = useMemo(() => expr, deps)` → Map (name → the memo's expression node).
|
|
749
868
|
*
|
|
750
|
-
* @param
|
|
869
|
+
* @param functionBody The component's body.
|
|
751
870
|
*
|
|
752
871
|
* @returns Each memo's expression, by name.
|
|
753
872
|
*/
|
|
754
|
-
export function collectMemos(
|
|
873
|
+
export function collectMemos(functionBody) {
|
|
755
874
|
const memos = new Map();
|
|
756
|
-
if (
|
|
875
|
+
if (functionBody.type !== 'BlockStatement')
|
|
757
876
|
return memos;
|
|
758
877
|
// Collect expression-bodied useMemo bindings; block-bodied memos are rejected until statement-bodied folding is supported.
|
|
759
|
-
for (const
|
|
760
|
-
if (
|
|
878
|
+
for (const statement of functionBody.body) {
|
|
879
|
+
if (statement.type !== 'VariableDeclaration')
|
|
761
880
|
continue;
|
|
762
|
-
for (const
|
|
763
|
-
const
|
|
764
|
-
if (
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
isFn(
|
|
768
|
-
const body =
|
|
881
|
+
for (const declarator of statement.declarations) {
|
|
882
|
+
const initializer = declarator.init;
|
|
883
|
+
if (initializer?.type === 'CallExpression' &&
|
|
884
|
+
initializer.callee.name === 'useMemo' &&
|
|
885
|
+
declarator.id.type === 'Identifier' &&
|
|
886
|
+
isFn(initializer.arguments[0])) {
|
|
887
|
+
const body = initializer.arguments[0].body;
|
|
769
888
|
if (body.type === 'BlockStatement') {
|
|
770
|
-
throw new Error(`AOT: useMemo for "${
|
|
889
|
+
throw new Error(`AOT: useMemo for "${declarator.id.name}" must be a single expression (for now)`);
|
|
771
890
|
}
|
|
772
|
-
memos.set(
|
|
891
|
+
memos.set(declarator.id.name, body);
|
|
773
892
|
}
|
|
774
893
|
}
|
|
775
894
|
}
|
|
@@ -778,19 +897,19 @@ export function collectMemos(fnBody) {
|
|
|
778
897
|
/**
|
|
779
898
|
* Collects `useEffect(() => {…}, deps)` calls → { fn, deps, node }. Dep validity is checked at compile time.
|
|
780
899
|
*
|
|
781
|
-
* @param
|
|
900
|
+
* @param functionBody The component's body.
|
|
782
901
|
*
|
|
783
902
|
* @returns The effects, in source order.
|
|
784
903
|
*/
|
|
785
|
-
export function collectEffects(
|
|
904
|
+
export function collectEffects(functionBody) {
|
|
786
905
|
const effects = [];
|
|
787
|
-
if (
|
|
906
|
+
if (functionBody.type !== 'BlockStatement')
|
|
788
907
|
return effects;
|
|
789
908
|
// Collect top-level useEffect calls, requiring an inline function so the effect body can be lowered later with its deps.
|
|
790
|
-
for (const
|
|
791
|
-
if (
|
|
909
|
+
for (const statement of functionBody.body) {
|
|
910
|
+
if (statement.type !== 'ExpressionStatement')
|
|
792
911
|
continue;
|
|
793
|
-
const call =
|
|
912
|
+
const call = statement.expression;
|
|
794
913
|
if (call?.type === 'CallExpression' &&
|
|
795
914
|
call.callee.type === 'Identifier' &&
|
|
796
915
|
call.callee.name === 'useEffect') {
|
|
@@ -805,8 +924,8 @@ export function collectEffects(fnBody) {
|
|
|
805
924
|
throw error;
|
|
806
925
|
}
|
|
807
926
|
effects.push({
|
|
808
|
-
|
|
809
|
-
|
|
927
|
+
functionNode: call.arguments[0],
|
|
928
|
+
dependencies: call.arguments[1],
|
|
810
929
|
node: call,
|
|
811
930
|
});
|
|
812
931
|
}
|
|
@@ -816,14 +935,14 @@ export function collectEffects(fnBody) {
|
|
|
816
935
|
/**
|
|
817
936
|
* True if a function component declares any useState (per-instance child state — not yet supported).
|
|
818
937
|
*
|
|
819
|
-
* @param
|
|
938
|
+
* @param functionNode The component.
|
|
820
939
|
*
|
|
821
940
|
* @returns Whether its body calls useState.
|
|
822
941
|
*/
|
|
823
|
-
export function usesState(
|
|
824
|
-
if (
|
|
942
|
+
export function usesState(functionNode) {
|
|
943
|
+
if (functionNode.body.type !== 'BlockStatement')
|
|
825
944
|
return false;
|
|
826
|
-
return
|
|
827
|
-
statement.declarations.some(
|
|
828
|
-
|
|
945
|
+
return functionNode.body.body.some(statement => statement.type === 'VariableDeclaration' &&
|
|
946
|
+
statement.declarations.some(declarator => declarator.init?.type === 'CallExpression' &&
|
|
947
|
+
declarator.init.callee.name === 'useState'));
|
|
829
948
|
}
|