embedded-react 0.16.0 → 0.17.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.
@@ -16,7 +16,21 @@
16
16
  /*----------------------------------------------------------------------------------------------------------------------
17
17
  - Imports
18
18
  ---------------------------------------------------------------------------------------------------------------------*/
19
+ import { BootReadError, BootValue, buildValueOf, containsBoot, foldBoot, isBoot, isContainer, } from './boot.mjs';
19
20
  import { aotError } from './diagnostics.mjs';
21
+ /*----------------------------------------------------------------------------------------------------------------------
22
+ - Constants
23
+ ---------------------------------------------------------------------------------------------------------------------*/
24
+ /** The Math functions a fold works out, as JS does. */
25
+ const STATIC_MATH = {
26
+ min: Math.min,
27
+ max: Math.max,
28
+ floor: Math.floor,
29
+ ceil: Math.ceil,
30
+ round: Math.round,
31
+ trunc: Math.trunc,
32
+ abs: Math.abs,
33
+ };
20
34
  /*----------------------------------------------------------------------------------------------------------------------
21
35
  - Implementation
22
36
  ---------------------------------------------------------------------------------------------------------------------*/
@@ -49,14 +63,153 @@ export function evalStaticOrThrow(node, scope, message, hint) {
49
63
  * Styles and state initial values fold through here. A caller that can also handle a runtime value catches
50
64
  * the error thrown on anything dynamic (a state reference, say) and emits C instead.
51
65
  *
66
+ * A value derived from the panel size (see BootValue) folds by `mode`: `fold` takes what it is on the build's
67
+ * panel and records that as an assumption the panel at boot must meet; `boot` keeps it as a boot value, which
68
+ * boot works out; `strict` throws a BootReadError, so the caller lowers it as a runtime value instead.
69
+ *
52
70
  * @param node The expression.
53
71
  * @param scope Compile-time constants in reach.
72
+ * @param mode How a value derived from the panel size folds.
54
73
  *
55
74
  * @returns The value: a number, string, boolean, null, or an object or array of those.
56
75
  *
57
76
  * @throws An error when any part of the expression is not a compile-time constant.
58
77
  */
59
- export function evalStatic(node, scope) {
78
+ export function evalStatic(node, scope, mode = 'fold') {
79
+ const value = evaluate(node, scope, { scope, snapshot: null });
80
+ if (mode === 'boot')
81
+ return value;
82
+ if (mode === 'fold')
83
+ return foldBoot(value);
84
+ if (containsBoot(value)) {
85
+ throw new BootReadError();
86
+ }
87
+ return value;
88
+ }
89
+ /**
90
+ * The names a boot value's expression reads, copied once per fold: the scope a const was folded in can lose
91
+ * or rebind names afterward.
92
+ *
93
+ * @param fold The fold.
94
+ *
95
+ * @returns The copy.
96
+ */
97
+ function snapshotOf(fold) {
98
+ if (!fold.snapshot) {
99
+ const copy = {};
100
+ for (const name in fold.scope) {
101
+ copy[name] = fold.scope[name];
102
+ }
103
+ fold.snapshot = copy;
104
+ }
105
+ return fold.snapshot;
106
+ }
107
+ /**
108
+ * A boot value for an expression the fold could only take so far because it reads one.
109
+ *
110
+ * @param node The expression.
111
+ * @param value What it is on the build's panel.
112
+ * @param fold The fold.
113
+ * @param lowerable Boot can work it out from the expression.
114
+ * @param basis What a fold of it assumes when boot cannot work it out.
115
+ *
116
+ * @returns The boot value.
117
+ */
118
+ const bootOf = (node, value, fold, lowerable = true, basis = []) => new BootValue(node, snapshotOf(fold), buildValueOf(value), lowerable, null, null, basis);
119
+ /**
120
+ * `test ? consequent : alternate` with a test that depends on the panel. Two objects with the same keys merge
121
+ * into one whose differing entries each pick at boot, so a branch that changes only values stays one value;
122
+ * anything else is one boot value.
123
+ *
124
+ * @param node The expression.
125
+ * @param test The test, as a boot value.
126
+ * @param fold The fold.
127
+ *
128
+ * @returns The value.
129
+ */
130
+ function bootConditional(node, test, fold) {
131
+ const taken = buildValueOf(test) ? node.consequent : node.alternate;
132
+ const takenValue = evaluate(taken, fold.scope, fold);
133
+ let otherValue;
134
+ try {
135
+ otherValue = evaluate(taken === node.consequent ? node.alternate : node.consequent, fold.scope, fold);
136
+ }
137
+ catch {
138
+ // The branch the build's panel does not take does not fold, so boot cannot pick between them.
139
+ return bootOf(node, takenValue, fold, false, [test]);
140
+ }
141
+ const [consequent, alternate] = taken === node.consequent
142
+ ? [takenValue, otherValue]
143
+ : [otherValue, takenValue];
144
+ return mergeBranches(node, node.consequent, node.alternate, [
145
+ consequent,
146
+ alternate,
147
+ takenValue,
148
+ ]);
149
+ /**
150
+ * Merges two branch values at `at`, entry by entry where both are objects. An entry only one branch has is
151
+ * taken at the build's panel, which assumes the test.
152
+ *
153
+ * @param at The expression the merged value stands for.
154
+ * @param consequentNode The consequent branch's expression.
155
+ * @param alternateNode The alternate branch's expression.
156
+ * @param values The consequent's value, the alternate's, and the one the build's panel takes.
157
+ *
158
+ * @returns The merged value.
159
+ */
160
+ function mergeBranches(at, consequentNode, alternateNode, [whenTrue, whenFalse, built]) {
161
+ if (whenTrue === whenFalse)
162
+ return whenTrue;
163
+ const mergeable = isContainer(whenTrue) &&
164
+ isContainer(whenFalse) &&
165
+ !Array.isArray(whenTrue) &&
166
+ !Array.isArray(whenFalse);
167
+ if (!mergeable) {
168
+ return bootOf(at, built, fold, !isContainer(buildValueOf(built)), [test]);
169
+ }
170
+ // Each entry is `test ? (consequent).key : (alternate).key`, which boot works out on its own.
171
+ const merged = {};
172
+ const keys = new Set([...Object.keys(whenTrue), ...Object.keys(whenFalse)]);
173
+ for (const key of keys) {
174
+ if (!Object.hasOwn(whenTrue, key) || !Object.hasOwn(whenFalse, key)) {
175
+ const builtEntry = built[key];
176
+ merged[key] = bootOf(at, builtEntry, fold, false, [test]);
177
+ continue;
178
+ }
179
+ const entryOf = (object) => ({
180
+ type: 'MemberExpression',
181
+ object,
182
+ property: { type: 'StringLiteral', value: key },
183
+ computed: true,
184
+ loc: object.loc,
185
+ });
186
+ const consequentEntry = entryOf(consequentNode);
187
+ const alternateEntry = entryOf(alternateNode);
188
+ merged[key] = mergeBranches({
189
+ type: 'ConditionalExpression',
190
+ test: node.test,
191
+ consequent: consequentEntry,
192
+ alternate: alternateEntry,
193
+ loc: at.loc,
194
+ }, consequentEntry, alternateEntry, [
195
+ whenTrue[key],
196
+ whenFalse[key],
197
+ built[key],
198
+ ]);
199
+ }
200
+ return merged;
201
+ }
202
+ }
203
+ /**
204
+ * evalStatic's fold, which keeps a value derived from the panel size as a boot value.
205
+ *
206
+ * @param node The expression.
207
+ * @param scope Compile-time constants in reach.
208
+ * @param fold The fold this expression is part of.
209
+ *
210
+ * @returns The value.
211
+ */
212
+ function evaluate(node, scope, fold) {
60
213
  switch (node.type) {
61
214
  case 'NumericLiteral':
62
215
  case 'StringLiteral':
@@ -65,69 +218,83 @@ export function evalStatic(node, scope) {
65
218
  case 'NullLiteral':
66
219
  return null;
67
220
  case 'UnaryExpression': {
68
- const operand = evalStatic(node.argument, scope);
69
- if (node.operator === '-')
70
- return -operand;
71
- if (node.operator === '+')
72
- return +operand;
73
- if (node.operator === '!')
74
- return !operand;
75
- break;
221
+ const operand = evaluate(node.argument, scope, fold);
222
+ const apply = (value) => {
223
+ if (node.operator === '-')
224
+ return -value;
225
+ if (node.operator === '+')
226
+ return +value;
227
+ if (node.operator === '!')
228
+ return !value;
229
+ throw new Error('AOT: unsupported expression "UnaryExpression" in static context');
230
+ };
231
+ if (isBoot(operand)) {
232
+ const result = bootOf(node, apply(buildValueOf(operand)), fold);
233
+ // `!flag` holds exactly when `flag` does not, so a fold of it assumes the flag itself.
234
+ if (node.operator === '!' &&
235
+ typeof buildValueOf(operand) === 'boolean') {
236
+ result.equivalent = operand.equivalent ?? operand;
237
+ }
238
+ return result;
239
+ }
240
+ return apply(operand);
76
241
  }
77
242
  case 'BinaryExpression': {
78
- const left = evalStatic(node.left, scope);
79
- const right = evalStatic(node.right, scope);
80
- switch (node.operator) {
81
- case '+':
82
- return left + right;
83
- case '-':
84
- return left - right;
85
- case '*':
86
- return left * right;
87
- case '/':
88
- return left / right;
89
- case '%':
90
- return left % right;
91
- case '<':
92
- return left < right;
93
- case '>':
94
- return left > right;
95
- case '<=':
96
- return left <= right;
97
- case '>=':
98
- return left >= right;
99
- case '==':
100
- case '===':
101
- return left === right;
102
- case '!=':
103
- case '!==':
104
- return left !== right;
105
- }
106
- break;
243
+ const left = evaluate(node.left, scope, fold);
244
+ const right = evaluate(node.right, scope, fold);
245
+ const result = binary(node.operator, buildValueOf(left), buildValueOf(right));
246
+ return isBoot(left) || isBoot(right)
247
+ ? bootOf(node, result, fold)
248
+ : result;
107
249
  }
108
250
  case 'LogicalExpression': {
109
- const left = evalStatic(node.left, scope);
110
- if (node.operator === '&&') {
111
- return left ? evalStatic(node.right, scope) : left;
251
+ const left = evaluate(node.left, scope, fold);
252
+ if (node.operator !== '&&' && node.operator !== '||')
253
+ break;
254
+ if (!isBoot(left)) {
255
+ const takesRight = node.operator === '&&' ? left : !left;
256
+ return takesRight ? evaluate(node.right, scope, fold) : left;
112
257
  }
113
- if (node.operator === '||') {
114
- return left ? left : evalStatic(node.right, scope);
258
+ // A test that depends on the panel: boot picks a side, so both have to fold.
259
+ const takesRight = node.operator === '&&' ? buildValueOf(left) : !buildValueOf(left);
260
+ try {
261
+ const right = evaluate(node.right, scope, fold);
262
+ return bootOf(node, takesRight ? right : left.value, fold);
263
+ }
264
+ catch {
265
+ if (takesRight)
266
+ throw new Error('AOT: the right side does not fold');
267
+ return bootOf(node, left.value, fold, false, [left]);
115
268
  }
116
- break;
117
269
  }
118
- case 'ConditionalExpression':
119
- return evalStatic(node.test, scope)
120
- ? evalStatic(node.consequent, scope)
121
- : evalStatic(node.alternate, scope);
270
+ case 'ConditionalExpression': {
271
+ const test = evaluate(node.test, scope, fold);
272
+ if (isBoot(test))
273
+ return bootConditional(node, test, fold);
274
+ return test
275
+ ? evaluate(node.consequent, scope, fold)
276
+ : evaluate(node.alternate, scope, fold);
277
+ }
122
278
  case 'Identifier':
123
279
  if (node.name in scope)
124
280
  return scope[node.name];
125
281
  throw new Error(`AOT: cannot statically resolve identifier "${node.name}"`);
126
282
  case 'MemberExpression': {
127
- const object = evalStatic(node.object, scope);
283
+ const object = evaluate(node.object, scope, fold);
128
284
  const key = node.computed
129
- ? evalStatic(node.property, scope)
285
+ ? evaluate(node.property, scope, fold)
130
286
  : node.property.name;
287
+ if (isBoot(key)) {
288
+ throw new Error('AOT: a member key that depends on the panel size');
289
+ }
290
+ // A member of a boot value is one too, which only a fold can take.
291
+ if (isBoot(object)) {
292
+ const objectValue = buildValueOf(object);
293
+ if (objectValue == null) {
294
+ throw new Error(`AOT: member access on null/undefined ("${key}")`);
295
+ }
296
+ return bootOf(node, objectValue[key], fold, false, [object]);
297
+ }
131
298
  if (object == null) {
132
299
  throw new Error(`AOT: member access on null/undefined ("${key}")`);
133
300
  }
@@ -140,28 +307,87 @@ export function evalStatic(node, scope) {
140
307
  throw new Error('AOT: object spreads/methods not supported in static objects');
141
308
  }
142
309
  const key = prop.computed
143
- ? evalStatic(prop.key, scope)
310
+ ? evaluate(prop.key, scope, fold)
144
311
  : (prop.key.name ??
145
312
  prop.key.value);
146
- folded[key] = evalStatic(prop.value, scope);
313
+ if (isBoot(key)) {
314
+ throw new Error('AOT: an object key that depends on the panel size');
315
+ }
316
+ folded[key] = evaluate(prop.value, scope, fold);
147
317
  }
148
318
  return folded;
149
319
  }
150
320
  case 'ArrayExpression':
151
- return node.elements.map(element => element ? evalStatic(element, scope) : null);
321
+ return node.elements.map(element => element ? evaluate(element, scope, fold) : null);
152
322
  case 'CallExpression': {
153
323
  // `StyleSheet.create({…})` folds to its argument; any other call is dynamic.
154
324
  const callee = node.callee;
155
325
  if (callee.type === 'MemberExpression' &&
156
326
  callee.object.name === 'StyleSheet' &&
157
327
  callee.property.name === 'create') {
158
- return evalStatic(node.arguments[0], scope);
328
+ return evaluate(node.arguments[0], scope, fold);
329
+ }
330
+ // Math.min / max / floor / ceil / round / trunc / abs fold as JS works them out; others are dynamic.
331
+ const mathFn = callee.type === 'MemberExpression' &&
332
+ !callee.computed &&
333
+ callee.object.type === 'Identifier' &&
334
+ callee.object.name === 'Math' &&
335
+ !('Math' in scope)
336
+ ? callee.property.name
337
+ : null;
338
+ if (mathFn && Object.hasOwn(STATIC_MATH, mathFn)) {
339
+ const args = node.arguments.map(arg => {
340
+ if (arg.type === 'SpreadElement') {
341
+ throw new Error('AOT: cannot statically evaluate a spread');
342
+ }
343
+ return evaluate(arg, scope, fold);
344
+ });
345
+ const result = STATIC_MATH[mathFn](...args.map(buildValueOf));
346
+ return args.some(isBoot) ? bootOf(node, result, fold) : result;
159
347
  }
160
348
  throw new Error(`AOT: cannot statically evaluate call expression`);
161
349
  }
162
350
  }
163
351
  throw new Error(`AOT: unsupported expression "${node.type}" in static context`);
164
352
  }
353
+ /**
354
+ * A binary operator applied as JS applies it.
355
+ *
356
+ * @param operator The operator.
357
+ * @param left The left operand.
358
+ * @param right The right operand.
359
+ *
360
+ * @returns The result.
361
+ */
362
+ function binary(operator, left, right) {
363
+ switch (operator) {
364
+ case '+':
365
+ return left + right;
366
+ case '-':
367
+ return left - right;
368
+ case '*':
369
+ return left * right;
370
+ case '/':
371
+ return left / right;
372
+ case '%':
373
+ return left % right;
374
+ case '<':
375
+ return left < right;
376
+ case '>':
377
+ return left > right;
378
+ case '<=':
379
+ return left <= right;
380
+ case '>=':
381
+ return left >= right;
382
+ case '==':
383
+ case '===':
384
+ return left === right;
385
+ case '!=':
386
+ case '!==':
387
+ return left !== right;
388
+ }
389
+ throw new Error('AOT: unsupported expression "BinaryExpression" in static context');
390
+ }
165
391
  /**
166
392
  * `scope` minus every name `env` binds at RUNTIME — locals, state, refs, animated values, and the event /
167
393
  * gesture params of the handler being compiled. emitExpr resolves those before env.consts, so a fold
@@ -16,7 +16,8 @@
16
16
  /*----------------------------------------------------------------------------------------------------------------------
17
17
  - Imports
18
18
  ---------------------------------------------------------------------------------------------------------------------*/
19
- import { check } from './check.mjs';
19
+ import { attemptLowering, check } from './check.mjs';
20
+ import { BootReadError, bootEnvOf, bootSlotOf, foldBoot, isBoot, } from './boot.mjs';
20
21
  import { lowerStyle, isStyleKey, STYLE_KEYS, DYN_FIELDS, colorLiteral, } from '../style-map.mjs';
21
22
  import { parseColor } from '../../src/embedded-react/svg-ops.js';
22
23
  import { aotError } from './diagnostics.mjs';
@@ -26,6 +27,17 @@ import { cstr } from './c-syntax.mjs';
26
27
  import { jsxChildText, lowerExpr, lowerText } from './expressions.mjs';
27
28
  import { printPropValue } from './c/nodes.mjs';
28
29
  import { ANIM_STYLE_PROPS, ANIM_TRANSFORM_PROPS, parseInterp, } from './animations.mjs';
30
+ /*----------------------------------------------------------------------------------------------------------------------
31
+ - Constants
32
+ ---------------------------------------------------------------------------------------------------------------------*/
33
+ /** Pixel style keys state cannot drive but a value worked out at boot still sets, with their ERProps fields. */
34
+ const BOOT_PIXEL_FIELDS = {
35
+ left: 'left',
36
+ top: 'top',
37
+ right: 'right',
38
+ bottom: 'bottom',
39
+ flexBasis: 'flex_basis',
40
+ };
29
41
  /*----------------------------------------------------------------------------------------------------------------------
30
42
  - Implementation
31
43
  ---------------------------------------------------------------------------------------------------------------------*/
@@ -82,6 +94,13 @@ export function lowerColorValue(node, env) {
82
94
  whenFalse: lowerColorValue(node.alternate, env),
83
95
  };
84
96
  }
97
+ // A const worked out at boot picks its color at boot, from its own expression.
98
+ const bootAlias = bootAliasOf(node, env);
99
+ if (bootAlias) {
100
+ const picked = tryLowering(() => lowerColorValue(bootAlias.node, bootAlias.env));
101
+ if (picked)
102
+ return picked;
103
+ }
85
104
  // A statically resolvable color (a const string, or a theme token like `theme.card`) folds to a literal.
86
105
  try {
87
106
  const color = evalStatic(node, foldScope(env, env.consts ?? {}));
@@ -129,6 +148,13 @@ export function lowerEnumValue(node, table, env) {
129
148
  whenFalse: lowerEnumValue(node.alternate, table, env),
130
149
  };
131
150
  }
151
+ // A const worked out at boot picks its value at boot, from its own expression.
152
+ const bootAlias = bootAliasOf(node, env);
153
+ if (bootAlias) {
154
+ const picked = tryLowering(() => lowerEnumValue(bootAlias.node, table, bootAlias.env));
155
+ if (picked)
156
+ return picked;
157
+ }
132
158
  // A statically resolvable enum (a const string) folds to its constant.
133
159
  try {
134
160
  const enumValue = evalStatic(node, foldScope(env, env.consts ?? {}));
@@ -242,6 +268,108 @@ function lowerDynamicStyleValue(key, valueNode, env) {
242
268
  { field: dynamicField.field, value: { kind: 'dimension', value: valueExpr } },
243
269
  ];
244
270
  }
271
+ /**
272
+ * The expression and environment of a value worked out at boot that `node` reads as a whole (a const, or an
273
+ * entry of one), for a lowering that picks between literals.
274
+ *
275
+ * @param node The expression.
276
+ * @param env The expression environment.
277
+ *
278
+ * @returns The value's own expression and the environment it lowers in, or null.
279
+ */
280
+ function bootAliasOf(node, env) {
281
+ let value;
282
+ try {
283
+ value = evalStatic(node, foldScope(env, env.consts ?? {}), 'boot');
284
+ }
285
+ catch {
286
+ return null;
287
+ }
288
+ if (!isBoot(value) || !value.node || value.node === node)
289
+ return null;
290
+ const bootEnv = bootEnvOf(value);
291
+ return bootEnv && { node: value.node, env: bootEnv };
292
+ }
293
+ /**
294
+ * A style entry worked out at boot, as the runtime write of its field: a size or an opacity. Null for a value
295
+ * boot cannot work out, one that is no number, or a key that takes no runtime number, which then fold.
296
+ *
297
+ * @param key The style key.
298
+ * @param value The entry.
299
+ *
300
+ * @returns The write, or null.
301
+ */
302
+ function bootStyleAssign(key, value) {
303
+ const dynamicField = Object.hasOwn(DYN_FIELDS, key)
304
+ ? DYN_FIELDS[key]
305
+ : undefined;
306
+ const pixelField = Object.hasOwn(BOOT_PIXEL_FIELDS, key)
307
+ ? BOOT_PIXEL_FIELDS[key]
308
+ : undefined;
309
+ // A color or an enum lowers its own expression (a ternary of literals) against the names it reads.
310
+ if (dynamicField?.kind === 'color' || dynamicField?.kind === 'enum') {
311
+ const env = bootEnvOf(value);
312
+ const valueNode = value.node;
313
+ if (!env || !valueNode)
314
+ return null;
315
+ const propValue = tryLowering(() => dynamicField.kind === 'enum'
316
+ ? lowerEnumValue(valueNode, dynamicField.table, env)
317
+ : lowerColorValue(valueNode, env));
318
+ return propValue && { field: dynamicField.field, value: propValue };
319
+ }
320
+ const isNumeric = pixelField ||
321
+ dynamicField?.kind === 'opacity' ||
322
+ dynamicField?.kind === 'num';
323
+ if (!isNumeric)
324
+ return null;
325
+ value.name ??= key;
326
+ const slot = bootSlotOf(value);
327
+ if (!slot || (slot.type.kind !== 'int' && slot.type.kind !== 'float')) {
328
+ return null;
329
+ }
330
+ return {
331
+ field: pixelField ?? dynamicField.field,
332
+ value: {
333
+ kind: dynamicField?.kind === 'opacity' ? 'opacity' : 'dimension',
334
+ value: { kind: 'boot', slot, type: slot.type },
335
+ },
336
+ };
337
+ }
338
+ /**
339
+ * Lowers a style value worked out at boot: as a state-driven one, or, for an inset or flexBasis, which state
340
+ * cannot drive, as the pixel size it is.
341
+ *
342
+ * @param key The style key.
343
+ * @param valueNode The value expression.
344
+ * @param env The expression environment.
345
+ *
346
+ * @returns The runtime writes.
347
+ */
348
+ function lowerBootStyleValue(key, valueNode, env) {
349
+ if (!Object.hasOwn(BOOT_PIXEL_FIELDS, key)) {
350
+ return lowerDynamicStyleValue(key, valueNode, env);
351
+ }
352
+ const value = lowerExpr(valueNode, env);
353
+ if (value.type.kind !== 'int' && value.type.kind !== 'float') {
354
+ throw new Error(`AOT: style "${key}" needs a number`);
355
+ }
356
+ return [{ field: BOOT_PIXEL_FIELDS[key], value: { kind: 'dimension', value } }];
357
+ }
358
+ /**
359
+ * Runs a lowering whose failure picks a fallback.
360
+ *
361
+ * @param attempt The lowering.
362
+ *
363
+ * @returns What it returns, or null when it throws.
364
+ */
365
+ function tryLowering(attempt) {
366
+ try {
367
+ return attemptLowering(attempt);
368
+ }
369
+ catch {
370
+ return null;
371
+ }
372
+ }
245
373
  /**
246
374
  * Whether a style already writes `field`, counting its percentage twin: `width: '50%'` lowers to
247
375
  * `width_pct`, and the author has still set the width. Used by the components that fall back to a
@@ -266,10 +394,11 @@ export const styleWrites = (staticAssigns, dynAssigns, field) => [field, `${fiel
266
394
  * @param scope Compile-time constants in reach.
267
395
  * @param env The expression environment.
268
396
  * @param spec The element, for the style keys it takes.
397
+ * @param bootValues How a value worked out at boot is set: once at build, or taken at the build's panel.
269
398
  *
270
399
  * @returns The style's constant writes, state-driven writes, and animated-value binds.
271
400
  */
272
- export function collectStyleAssigns(openingElement, scope, env, spec) {
401
+ export function collectStyleAssigns(openingElement, scope, env, spec, bootValues = 'boot') {
273
402
  // ERProps field → its last write: a constant's C, or a state-driven value.
274
403
  const fields = new Map();
275
404
  const binds = []; // animated values bound to node properties (native driver)
@@ -400,11 +529,27 @@ export function collectStyleAssigns(openingElement, scope, env, spec) {
400
529
  }
401
530
  // Only a failed fold falls back to the dynamic path, so an unknown key is not misreported as state-driven.
402
531
  let staticValue;
532
+ let readsBoot = false;
403
533
  try {
404
- staticValue = { v: evalStatic(prop.value, withUndefined(scope)) };
534
+ staticValue = {
535
+ v: evalStatic(prop.value, withUndefined(scope), bootValues === 'fold' ? 'fold' : 'strict'),
536
+ };
405
537
  }
406
- catch {
538
+ catch (error) {
407
539
  staticValue = null; // references state — lower it as a dynamic value
540
+ readsBoot = error instanceof BootReadError;
541
+ }
542
+ // A value worked out at boot is set once at build; a key that takes no runtime value folds it instead.
543
+ if (readsBoot) {
544
+ const bootAssigns = tryLowering(() => lowerBootStyleValue(key, prop.value, env));
545
+ if (bootAssigns) {
546
+ checkStyleKey(spec, key, prop);
547
+ for (const assign of bootAssigns) {
548
+ setField(assign.field, { value: assign.value });
549
+ }
550
+ continue;
551
+ }
552
+ staticValue = { v: evalStatic(prop.value, withUndefined(scope)) };
408
553
  }
409
554
  if (staticValue) {
410
555
  if (staticValue.v != null) {
@@ -424,11 +569,24 @@ export function collectStyleAssigns(openingElement, scope, env, spec) {
424
569
  return;
425
570
  }
426
571
  // A static style reference; null, undefined or false (`cond && s`) means no style, as in RN.
427
- const resolved = evalStatic(expr, withUndefined(scope));
572
+ let resolved = evalStatic(expr, withUndefined(scope), 'boot');
573
+ if (isBoot(resolved)) {
574
+ resolved = foldBoot(resolved);
575
+ }
428
576
  if (resolved === null || resolved === undefined || resolved === false) {
429
577
  return;
430
578
  }
431
- for (const [styleKey, styleValue] of Object.entries(resolved)) {
579
+ for (const [styleKey, entry] of Object.entries(resolved)) {
580
+ // An entry worked out at boot is set once at build, if its key takes a runtime value.
581
+ const bootAssign = bootValues === 'boot' && isBoot(entry)
582
+ ? bootStyleAssign(styleKey, entry)
583
+ : null;
584
+ if (bootAssign) {
585
+ checkStyleKey(spec, styleKey, expr);
586
+ setField(bootAssign.field, { value: bootAssign.value });
587
+ continue;
588
+ }
589
+ const styleValue = foldBoot(entry);
432
590
  if (styleValue === undefined || styleValue === null)
433
591
  continue;
434
592
  checkStyleKey(spec, styleKey, expr);
@@ -617,7 +775,8 @@ export function collectTextSpans(children, scope, env) {
617
775
  else if (child.type === 'JSXElement' &&
618
776
  child.openingElement.name.name === 'Text') {
619
777
  checkAttributes(TEXT_SPAN, child.openingElement);
620
- const { staticAssigns, dynAssigns } = collectStyleAssigns(child.openingElement, scope, env, TEXT_SPAN);
778
+ // A span is a static table, so a value worked out at boot is taken at the build's panel instead.
779
+ const { staticAssigns, dynAssigns } = collectStyleAssigns(child.openingElement, scope, env, TEXT_SPAN, 'fold');
621
780
  if (dynAssigns.length) {
622
781
  throw aotError('AOT: a state-driven style on a nested <Text> span is not supported', 'give the span <Text> a static style.');
623
782
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "embedded-react",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "type": "module",
5
5
  "description": "React Native-style component package + reconciler that drives the embedded-react C engine through the QuickJS NativeUI bridge (Flow A).",
6
6
  "license": "Apache-2.0",