embedded-react 0.16.0 → 0.18.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/aot/compiler/animations.mjs +102 -34
- package/aot/compiler/assemble.mjs +169 -31
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +315 -0
- package/aot/compiler/c/animations.mjs +90 -16
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +63 -8
- package/aot/compiler/c/nodes.mjs +34 -13
- package/aot/compiler/c/statements.mjs +92 -15
- package/aot/compiler/check.mjs +32 -3
- package/aot/compiler/collect.mjs +95 -9
- package/aot/compiler/control-flow.mjs +43 -18
- package/aot/compiler/diagnostics.mjs +33 -0
- package/aot/compiler/elements/dial.mjs +8 -7
- package/aot/compiler/elements/props.mjs +19 -9
- package/aot/compiler/elements/text-input.mjs +7 -2
- package/aot/compiler/expressions.mjs +132 -10
- package/aot/compiler/handlers.mjs +62 -24
- package/aot/compiler/ir/boot.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +28 -4
- package/aot/compiler/ir/reads.mjs +351 -0
- package/aot/compiler/keyboard.mjs +35 -21
- package/aot/compiler/lower.mjs +40 -11
- package/aot/compiler/nodes.mjs +22 -9
- package/aot/compiler/options.mjs +63 -12
- package/aot/compiler/static-eval.mjs +314 -56
- package/aot/compiler/style-text.mjs +201 -19
- package/aot/compiler/svg.mjs +7 -3
- package/aot/style-map.mjs +9 -11
- package/assets/bake-svg.mjs +3 -2
- package/package.json +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/Animated.js +41 -0
- package/src/embedded-react/Button.js +3 -4
- package/src/embedded-react/Pressable.js +59 -0
- package/src/embedded-react/TouchableOpacity.js +6 -9
- package/src/embedded-react/index.d.ts +118 -30
- package/src/embedded-react/index.js +1 -1
- package/src/embedded-react/named-colors.js +37 -0
- package/src/embedded-react/svg-ops.js +13 -16
- package/src/host-config.js +50 -8
|
@@ -16,16 +16,28 @@
|
|
|
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
|
-
import { aotError } from './diagnostics.mjs';
|
|
23
|
+
import { aotError, checkEngineString } from './diagnostics.mjs';
|
|
23
24
|
import { at, checkAnimatedStyleKey, checkAttributes, checkStyleKey, inlineFragments, TEXT_SPAN, } from './elements/props.mjs';
|
|
24
25
|
import { evalStatic, foldScope, withUndefined } from './static-eval.mjs';
|
|
25
26
|
import { cstr } from './c-syntax.mjs';
|
|
26
27
|
import { jsxChildText, lowerExpr, lowerText } from './expressions.mjs';
|
|
27
28
|
import { printPropValue } from './c/nodes.mjs';
|
|
28
|
-
import { ANIM_STYLE_PROPS, ANIM_TRANSFORM_PROPS, parseInterp, } from './animations.mjs';
|
|
29
|
+
import { ANIM_COLOR_KEYS, 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
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -62,6 +74,22 @@ export function argbLiteral(value) {
|
|
|
62
74
|
.toUpperCase() +
|
|
63
75
|
'u');
|
|
64
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Refuses an animated binding whose values do not fit the key: a color key takes only an interpolation onto
|
|
79
|
+
* colors, and every other key only numbers.
|
|
80
|
+
*
|
|
81
|
+
* @param key The style or transform key.
|
|
82
|
+
* @param interp The binding's interpolation, or undefined for the value itself.
|
|
83
|
+
* @param node The node to pin an error to.
|
|
84
|
+
*/
|
|
85
|
+
function checkAnimatedColor(key, interp, node) {
|
|
86
|
+
if (ANIM_COLOR_KEYS.has(key) && !interp?.colors) {
|
|
87
|
+
throw at(aotError(`AOT: an animated "${key}" needs colors`, "bind value.interpolate({inputRange: [0, 1], outputRange: ['#000', '#fff']}) rather than a number."), node);
|
|
88
|
+
}
|
|
89
|
+
if (!ANIM_COLOR_KEYS.has(key) && interp?.colors) {
|
|
90
|
+
throw at(aotError(`AOT: an animated "${key}" takes numbers, not colors`, 'an outputRange of colors drives backgroundColor or color.'), node);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
65
93
|
/**
|
|
66
94
|
* Lowers a dynamic (state-referencing) color expression to an ARGB value: a color, or a ternary of them.
|
|
67
95
|
*
|
|
@@ -82,6 +110,13 @@ export function lowerColorValue(node, env) {
|
|
|
82
110
|
whenFalse: lowerColorValue(node.alternate, env),
|
|
83
111
|
};
|
|
84
112
|
}
|
|
113
|
+
// A const worked out at boot picks its color at boot, from its own expression.
|
|
114
|
+
const bootAlias = bootAliasOf(node, env);
|
|
115
|
+
if (bootAlias) {
|
|
116
|
+
const picked = tryLowering(() => lowerColorValue(bootAlias.node, bootAlias.env));
|
|
117
|
+
if (picked)
|
|
118
|
+
return picked;
|
|
119
|
+
}
|
|
85
120
|
// A statically resolvable color (a const string, or a theme token like `theme.card`) folds to a literal.
|
|
86
121
|
try {
|
|
87
122
|
const color = evalStatic(node, foldScope(env, env.consts ?? {}));
|
|
@@ -115,7 +150,9 @@ export const emitColorExpr = (node, env) => printPropValue(lowerColorValue(node,
|
|
|
115
150
|
*/
|
|
116
151
|
export function lowerEnumValue(node, table, env) {
|
|
117
152
|
if (node.type === 'StringLiteral') {
|
|
118
|
-
const enumConstant = table
|
|
153
|
+
const enumConstant = Object.hasOwn(table, node.value)
|
|
154
|
+
? table[node.value]
|
|
155
|
+
: undefined;
|
|
119
156
|
if (!enumConstant) {
|
|
120
157
|
throw aotError(`AOT: unsupported enum value "${node.value}"`, `one of: ${Object.keys(table).join(', ')}`);
|
|
121
158
|
}
|
|
@@ -129,10 +166,17 @@ export function lowerEnumValue(node, table, env) {
|
|
|
129
166
|
whenFalse: lowerEnumValue(node.alternate, table, env),
|
|
130
167
|
};
|
|
131
168
|
}
|
|
169
|
+
// A const worked out at boot picks its value at boot, from its own expression.
|
|
170
|
+
const bootAlias = bootAliasOf(node, env);
|
|
171
|
+
if (bootAlias) {
|
|
172
|
+
const picked = tryLowering(() => lowerEnumValue(bootAlias.node, table, bootAlias.env));
|
|
173
|
+
if (picked)
|
|
174
|
+
return picked;
|
|
175
|
+
}
|
|
132
176
|
// A statically resolvable enum (a const string) folds to its constant.
|
|
133
177
|
try {
|
|
134
178
|
const enumValue = evalStatic(node, foldScope(env, env.consts ?? {}));
|
|
135
|
-
if (typeof enumValue === 'string' && table
|
|
179
|
+
if (typeof enumValue === 'string' && Object.hasOwn(table, enumValue)) {
|
|
136
180
|
return { kind: 'constant', code: table[enumValue] };
|
|
137
181
|
}
|
|
138
182
|
}
|
|
@@ -242,6 +286,108 @@ function lowerDynamicStyleValue(key, valueNode, env) {
|
|
|
242
286
|
{ field: dynamicField.field, value: { kind: 'dimension', value: valueExpr } },
|
|
243
287
|
];
|
|
244
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* The expression and environment of a value worked out at boot that `node` reads as a whole (a const, or an
|
|
291
|
+
* entry of one), for a lowering that picks between literals.
|
|
292
|
+
*
|
|
293
|
+
* @param node The expression.
|
|
294
|
+
* @param env The expression environment.
|
|
295
|
+
*
|
|
296
|
+
* @returns The value's own expression and the environment it lowers in, or null.
|
|
297
|
+
*/
|
|
298
|
+
function bootAliasOf(node, env) {
|
|
299
|
+
let value;
|
|
300
|
+
try {
|
|
301
|
+
value = evalStatic(node, foldScope(env, env.consts ?? {}), 'boot');
|
|
302
|
+
}
|
|
303
|
+
catch {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
if (!isBoot(value) || !value.node || value.node === node)
|
|
307
|
+
return null;
|
|
308
|
+
const bootEnv = bootEnvOf(value);
|
|
309
|
+
return bootEnv && { node: value.node, env: bootEnv };
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* A style entry worked out at boot, as the runtime write of its field: a size or an opacity. Null for a value
|
|
313
|
+
* boot cannot work out, one that is no number, or a key that takes no runtime number, which then fold.
|
|
314
|
+
*
|
|
315
|
+
* @param key The style key.
|
|
316
|
+
* @param value The entry.
|
|
317
|
+
*
|
|
318
|
+
* @returns The write, or null.
|
|
319
|
+
*/
|
|
320
|
+
function bootStyleAssign(key, value) {
|
|
321
|
+
const dynamicField = Object.hasOwn(DYN_FIELDS, key)
|
|
322
|
+
? DYN_FIELDS[key]
|
|
323
|
+
: undefined;
|
|
324
|
+
const pixelField = Object.hasOwn(BOOT_PIXEL_FIELDS, key)
|
|
325
|
+
? BOOT_PIXEL_FIELDS[key]
|
|
326
|
+
: undefined;
|
|
327
|
+
// A color or an enum lowers its own expression (a ternary of literals) against the names it reads.
|
|
328
|
+
if (dynamicField?.kind === 'color' || dynamicField?.kind === 'enum') {
|
|
329
|
+
const env = bootEnvOf(value);
|
|
330
|
+
const valueNode = value.node;
|
|
331
|
+
if (!env || !valueNode)
|
|
332
|
+
return null;
|
|
333
|
+
const propValue = tryLowering(() => dynamicField.kind === 'enum'
|
|
334
|
+
? lowerEnumValue(valueNode, dynamicField.table, env)
|
|
335
|
+
: lowerColorValue(valueNode, env));
|
|
336
|
+
return propValue && { field: dynamicField.field, value: propValue };
|
|
337
|
+
}
|
|
338
|
+
const isNumeric = pixelField ||
|
|
339
|
+
dynamicField?.kind === 'opacity' ||
|
|
340
|
+
dynamicField?.kind === 'num';
|
|
341
|
+
if (!isNumeric)
|
|
342
|
+
return null;
|
|
343
|
+
value.name ??= key;
|
|
344
|
+
const slot = bootSlotOf(value);
|
|
345
|
+
if (!slot || (slot.type.kind !== 'int' && slot.type.kind !== 'float')) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
return {
|
|
349
|
+
field: pixelField ?? dynamicField.field,
|
|
350
|
+
value: {
|
|
351
|
+
kind: dynamicField?.kind === 'opacity' ? 'opacity' : 'dimension',
|
|
352
|
+
value: { kind: 'boot', slot, type: slot.type },
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Lowers a style value worked out at boot: as a state-driven one, or, for an inset or flexBasis, which state
|
|
358
|
+
* cannot drive, as the pixel size it is.
|
|
359
|
+
*
|
|
360
|
+
* @param key The style key.
|
|
361
|
+
* @param valueNode The value expression.
|
|
362
|
+
* @param env The expression environment.
|
|
363
|
+
*
|
|
364
|
+
* @returns The runtime writes.
|
|
365
|
+
*/
|
|
366
|
+
function lowerBootStyleValue(key, valueNode, env) {
|
|
367
|
+
if (!Object.hasOwn(BOOT_PIXEL_FIELDS, key)) {
|
|
368
|
+
return lowerDynamicStyleValue(key, valueNode, env);
|
|
369
|
+
}
|
|
370
|
+
const value = lowerExpr(valueNode, env);
|
|
371
|
+
if (value.type.kind !== 'int' && value.type.kind !== 'float') {
|
|
372
|
+
throw new Error(`AOT: style "${key}" needs a number`);
|
|
373
|
+
}
|
|
374
|
+
return [{ field: BOOT_PIXEL_FIELDS[key], value: { kind: 'dimension', value } }];
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Runs a lowering whose failure picks a fallback.
|
|
378
|
+
*
|
|
379
|
+
* @param attempt The lowering.
|
|
380
|
+
*
|
|
381
|
+
* @returns What it returns, or null when it throws.
|
|
382
|
+
*/
|
|
383
|
+
function tryLowering(attempt) {
|
|
384
|
+
try {
|
|
385
|
+
return attemptLowering(attempt);
|
|
386
|
+
}
|
|
387
|
+
catch {
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
245
391
|
/**
|
|
246
392
|
* Whether a style already writes `field`, counting its percentage twin: `width: '50%'` lowers to
|
|
247
393
|
* `width_pct`, and the author has still set the width. Used by the components that fall back to a
|
|
@@ -266,10 +412,11 @@ export const styleWrites = (staticAssigns, dynAssigns, field) => [field, `${fiel
|
|
|
266
412
|
* @param scope Compile-time constants in reach.
|
|
267
413
|
* @param env The expression environment.
|
|
268
414
|
* @param spec The element, for the style keys it takes.
|
|
415
|
+
* @param bootValues How a value worked out at boot is set: once at build, or taken at the build's panel.
|
|
269
416
|
*
|
|
270
417
|
* @returns The style's constant writes, state-driven writes, and animated-value binds.
|
|
271
418
|
*/
|
|
272
|
-
export function collectStyleAssigns(openingElement, scope, env, spec) {
|
|
419
|
+
export function collectStyleAssigns(openingElement, scope, env, spec, bootValues = 'boot') {
|
|
273
420
|
// ERProps field → its last write: a constant's C, or a state-driven value.
|
|
274
421
|
const fields = new Map();
|
|
275
422
|
const binds = []; // animated values bound to node properties (native driver)
|
|
@@ -324,6 +471,7 @@ export function collectStyleAssigns(openingElement, scope, env, spec) {
|
|
|
324
471
|
if (!Object.hasOwn(ANIM_TRANSFORM_PROPS, transformKey)) {
|
|
325
472
|
throw at(aotError(`AOT: transform "${transformKey}" cannot be animated in Flow B`, `animated transforms: ${Object.keys(ANIM_TRANSFORM_PROPS).join(', ')}.`), transformProp);
|
|
326
473
|
}
|
|
474
|
+
checkAnimatedColor(transformKey, mapped?.interp, transformProp);
|
|
327
475
|
for (const animProp of ANIM_TRANSFORM_PROPS[transformKey]) {
|
|
328
476
|
transformBinds.push(mapped
|
|
329
477
|
? { cVar: mapped.cVar, prop: animProp, interp: mapped.interp }
|
|
@@ -372,6 +520,7 @@ export function collectStyleAssigns(openingElement, scope, env, spec) {
|
|
|
372
520
|
const animVar = animRef(prop.value);
|
|
373
521
|
if (animVar && ANIM_STYLE_PROPS[key]) {
|
|
374
522
|
checkAnimatedStyleKey(spec, key, prop);
|
|
523
|
+
checkAnimatedColor(key, undefined, prop);
|
|
375
524
|
for (const animProp of ANIM_STYLE_PROPS[key]) {
|
|
376
525
|
binds.push({ cVar: animVar, prop: animProp });
|
|
377
526
|
}
|
|
@@ -380,6 +529,7 @@ export function collectStyleAssigns(openingElement, scope, env, spec) {
|
|
|
380
529
|
const animInterp = animInterpRef(prop.value);
|
|
381
530
|
if (animInterp && ANIM_STYLE_PROPS[key]) {
|
|
382
531
|
checkAnimatedStyleKey(spec, key, prop);
|
|
532
|
+
checkAnimatedColor(key, animInterp.interp, prop);
|
|
383
533
|
for (const animProp of ANIM_STYLE_PROPS[key]) {
|
|
384
534
|
binds.push({
|
|
385
535
|
cVar: animInterp.cVar,
|
|
@@ -400,11 +550,27 @@ export function collectStyleAssigns(openingElement, scope, env, spec) {
|
|
|
400
550
|
}
|
|
401
551
|
// Only a failed fold falls back to the dynamic path, so an unknown key is not misreported as state-driven.
|
|
402
552
|
let staticValue;
|
|
553
|
+
let readsBoot = false;
|
|
403
554
|
try {
|
|
404
|
-
staticValue = {
|
|
555
|
+
staticValue = {
|
|
556
|
+
v: evalStatic(prop.value, withUndefined(scope), bootValues === 'fold' ? 'fold' : 'strict'),
|
|
557
|
+
};
|
|
405
558
|
}
|
|
406
|
-
catch {
|
|
559
|
+
catch (error) {
|
|
407
560
|
staticValue = null; // references state — lower it as a dynamic value
|
|
561
|
+
readsBoot = error instanceof BootReadError;
|
|
562
|
+
}
|
|
563
|
+
// A value worked out at boot is set once at build; a key that takes no runtime value folds it instead.
|
|
564
|
+
if (readsBoot) {
|
|
565
|
+
const bootAssigns = tryLowering(() => lowerBootStyleValue(key, prop.value, env));
|
|
566
|
+
if (bootAssigns) {
|
|
567
|
+
checkStyleKey(spec, key, prop);
|
|
568
|
+
for (const assign of bootAssigns) {
|
|
569
|
+
setField(assign.field, { value: assign.value });
|
|
570
|
+
}
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
staticValue = { v: evalStatic(prop.value, withUndefined(scope)) };
|
|
408
574
|
}
|
|
409
575
|
if (staticValue) {
|
|
410
576
|
if (staticValue.v != null) {
|
|
@@ -424,11 +590,24 @@ export function collectStyleAssigns(openingElement, scope, env, spec) {
|
|
|
424
590
|
return;
|
|
425
591
|
}
|
|
426
592
|
// A static style reference; null, undefined or false (`cond && s`) means no style, as in RN.
|
|
427
|
-
|
|
593
|
+
let resolved = evalStatic(expr, withUndefined(scope), 'boot');
|
|
594
|
+
if (isBoot(resolved)) {
|
|
595
|
+
resolved = foldBoot(resolved);
|
|
596
|
+
}
|
|
428
597
|
if (resolved === null || resolved === undefined || resolved === false) {
|
|
429
598
|
return;
|
|
430
599
|
}
|
|
431
|
-
for (const [styleKey,
|
|
600
|
+
for (const [styleKey, entry] of Object.entries(resolved)) {
|
|
601
|
+
// An entry worked out at boot is set once at build, if its key takes a runtime value.
|
|
602
|
+
const bootAssign = bootValues === 'boot' && isBoot(entry)
|
|
603
|
+
? bootStyleAssign(styleKey, entry)
|
|
604
|
+
: null;
|
|
605
|
+
if (bootAssign) {
|
|
606
|
+
checkStyleKey(spec, styleKey, expr);
|
|
607
|
+
setField(bootAssign.field, { value: bootAssign.value });
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
const styleValue = foldBoot(entry);
|
|
432
611
|
if (styleValue === undefined || styleValue === null)
|
|
433
612
|
continue;
|
|
434
613
|
checkStyleKey(spec, styleKey, expr);
|
|
@@ -472,10 +651,7 @@ export function lowerNodeText(children, scope, env) {
|
|
|
472
651
|
const parts = [];
|
|
473
652
|
for (const child of inlineFragments(children)) {
|
|
474
653
|
if (child.type === 'JSXText') {
|
|
475
|
-
|
|
476
|
-
? child.value.replace(/\s+/g, ' ').trim()
|
|
477
|
-
: child.value;
|
|
478
|
-
parts.push({ kind: 'literal', text });
|
|
654
|
+
parts.push({ kind: 'literal', text: cleanJsxText(child.value) });
|
|
479
655
|
}
|
|
480
656
|
else if (child.type === 'JSXExpressionContainer') {
|
|
481
657
|
if (child.expression.type === 'JSXEmptyExpression')
|
|
@@ -508,7 +684,7 @@ const unsupportedTextChild = (child) => at(aotError(`AOT: a ${child.type} cannot
|
|
|
508
684
|
*
|
|
509
685
|
* @returns The text as React would render it.
|
|
510
686
|
*/
|
|
511
|
-
function cleanJsxText(value) {
|
|
687
|
+
export function cleanJsxText(value) {
|
|
512
688
|
// Find the last line with content: it is the only one not followed by a joining space.
|
|
513
689
|
const lines = value.split(/\r\n|\n|\r/);
|
|
514
690
|
let lastContentLine = 0;
|
|
@@ -590,13 +766,18 @@ export function collectTextSpans(children, scope, env) {
|
|
|
590
766
|
text_decoration: '0xFF',
|
|
591
767
|
letter_spacing: 'ER_LAYOUT_AUTO',
|
|
592
768
|
});
|
|
769
|
+
// A span's text is a C literal in a fixed engine buffer.
|
|
770
|
+
const spanText = (text) => {
|
|
771
|
+
checkEngineString(text, 'ER_SPAN_TEXT_MAX', 'a <Text> span');
|
|
772
|
+
return cstr(text);
|
|
773
|
+
};
|
|
593
774
|
// One span per child: text and static {…} values inherit the parent's style; a nested <Text> carries its own.
|
|
594
775
|
const spans = [];
|
|
595
776
|
for (const child of children) {
|
|
596
777
|
if (child.type === 'JSXText') {
|
|
597
778
|
const text = cleanJsxText(child.value);
|
|
598
779
|
if (text) {
|
|
599
|
-
spans.push(inheritSpan(
|
|
780
|
+
spans.push(inheritSpan(spanText(text)));
|
|
600
781
|
}
|
|
601
782
|
}
|
|
602
783
|
else if (child.type === 'JSXExpressionContainer') {
|
|
@@ -611,13 +792,14 @@ export function collectTextSpans(children, scope, env) {
|
|
|
611
792
|
}
|
|
612
793
|
// React draws nothing for null, undefined, or a boolean child.
|
|
613
794
|
if (value !== undefined && value !== null && typeof value !== 'boolean') {
|
|
614
|
-
spans.push(inheritSpan(
|
|
795
|
+
spans.push(inheritSpan(spanText(String(value))));
|
|
615
796
|
}
|
|
616
797
|
}
|
|
617
798
|
else if (child.type === 'JSXElement' &&
|
|
618
799
|
child.openingElement.name.name === 'Text') {
|
|
619
800
|
checkAttributes(TEXT_SPAN, child.openingElement);
|
|
620
|
-
|
|
801
|
+
// A span is a static table, so a value worked out at boot is taken at the build's panel instead.
|
|
802
|
+
const { staticAssigns, dynAssigns } = collectStyleAssigns(child.openingElement, scope, env, TEXT_SPAN, 'fold');
|
|
621
803
|
if (dynAssigns.length) {
|
|
622
804
|
throw aotError('AOT: a state-driven style on a nested <Text> span is not supported', 'give the span <Text> a static style.');
|
|
623
805
|
}
|
|
@@ -625,7 +807,7 @@ export function collectTextSpans(children, scope, env) {
|
|
|
625
807
|
const spanStyle = (fieldName, fallback) => staticAssigns.find(assign => assign.field === fieldName)?.expr ??
|
|
626
808
|
fallback;
|
|
627
809
|
spans.push({
|
|
628
|
-
text:
|
|
810
|
+
text: spanText(staticTextContent(child.children, scope)),
|
|
629
811
|
color: spanStyle('color', '0u'),
|
|
630
812
|
font_size: spanStyle('font_size', '0'),
|
|
631
813
|
font_weight: spanStyle('font_weight', '0xFF'),
|
package/aot/compiler/svg.mjs
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { check, checkNumber } from './check.mjs';
|
|
20
20
|
import { lowerStyle, NODE_TYPES } from '../style-map.mjs';
|
|
21
|
-
import { flattenSvg, parseColor, parsePath, KAPPA, PAINT_STRIDE, scaleVectorArtifact, } from '../../src/embedded-react/svg-ops.js';
|
|
21
|
+
import { flattenSvg, parseColor, parsePath, KAPPA, PAINT_PROPS, PAINT_STRIDE, scaleVectorArtifact, } from '../../src/embedded-react/svg-ops.js';
|
|
22
22
|
import { aotError } from './diagnostics.mjs';
|
|
23
23
|
import { evalStatic, withUndefined } from './static-eval.mjs';
|
|
24
24
|
import { lowerExpr } from './expressions.mjs';
|
|
@@ -1079,7 +1079,7 @@ function emitSvgSource(element, sourceAttr, scope, out, env) {
|
|
|
1079
1079
|
out.images.set(artifact.name, artifact.png);
|
|
1080
1080
|
}
|
|
1081
1081
|
const node = lowerSvgBox(`n${out.allocateNodeId()}`, NODE_TYPES.Image, svgBoxSize('width', width, true), svgBoxSize('height', height, true), element.openingElement, scope, env);
|
|
1082
|
-
node.image = { kind: 'static', name: artifact.name
|
|
1082
|
+
node.image = { kind: 'static', name: artifact.name };
|
|
1083
1083
|
return node;
|
|
1084
1084
|
}
|
|
1085
1085
|
// Scale the vector artifact to the box and bake its ops, paints and gradients into const tables.
|
|
@@ -1133,7 +1133,11 @@ function emitSvgDynamic(element, scope, out, env, state) {
|
|
|
1133
1133
|
continue;
|
|
1134
1134
|
locals.push(...shape.locals);
|
|
1135
1135
|
tape.push(tapeOp('ER_VOP_SHAPE'), tapeNumber(shapePaints.length), ...shape.entries);
|
|
1136
|
-
|
|
1136
|
+
// The root's paint cascades to each shape, as flattenSvg does.
|
|
1137
|
+
const paintAttrs = { ...attrs };
|
|
1138
|
+
for (const name of PAINT_PROPS)
|
|
1139
|
+
paintAttrs[name] ??= rootAttrs[name];
|
|
1140
|
+
shapePaints.push(lowerShapePaint(paintAttrs, env, scope));
|
|
1137
1141
|
}
|
|
1138
1142
|
// Allocate the node and this <Svg>'s table names.
|
|
1139
1143
|
const nodeId = `n${out.allocateNodeId()}`;
|
package/aot/style-map.mjs
CHANGED
|
@@ -13,18 +13,13 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
|
+
- Imports
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { NAMED_COLORS } from '../src/embedded-react/named-colors.js';
|
|
16
20
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
21
|
- Constants
|
|
18
22
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
-
/** A few CSS/RN named colors (extend as demos need them); everything else must be hex. */
|
|
20
|
-
const NAMED_COLORS = {
|
|
21
|
-
transparent: 0x00000000,
|
|
22
|
-
white: 0xffffffff,
|
|
23
|
-
black: 0xff000000,
|
|
24
|
-
red: 0xffff0000,
|
|
25
|
-
green: 0xff00ff00,
|
|
26
|
-
blue: 0xff0000ff,
|
|
27
|
-
};
|
|
28
23
|
// RN enum values → ER_* constants, shared by the static (KEYS) and state-driven (ENUM_FIELDS) lowerings.
|
|
29
24
|
const ALIGN = {
|
|
30
25
|
auto: 'ER_ALIGN_AUTO',
|
|
@@ -146,8 +141,9 @@ export function parseColorValue(color) {
|
|
|
146
141
|
}
|
|
147
142
|
// Accept a named color, else a hex string, expanding the 3-digit form to 6.
|
|
148
143
|
const normalized = color.trim().toLowerCase();
|
|
149
|
-
if (normalized
|
|
144
|
+
if (Object.hasOwn(NAMED_COLORS, normalized)) {
|
|
150
145
|
return NAMED_COLORS[normalized] >>> 0;
|
|
146
|
+
}
|
|
151
147
|
const hexMatch = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(normalized);
|
|
152
148
|
if (!hexMatch) {
|
|
153
149
|
throw new Error(`unsupported color "${color}" (use #rgb / #rrggbb / #rrggbbaa or a named color)`);
|
|
@@ -196,7 +192,9 @@ export function colorLiteral(color) {
|
|
|
196
192
|
* @returns A lookup that throws on a value the table does not have.
|
|
197
193
|
*/
|
|
198
194
|
const enumKey = (table, name) => (value) => {
|
|
199
|
-
const constant = table
|
|
195
|
+
const constant = Object.hasOwn(table, value)
|
|
196
|
+
? table[value]
|
|
197
|
+
: undefined;
|
|
200
198
|
if (!constant) {
|
|
201
199
|
throw new Error(`${name}: unsupported value "${value}" (one of ${Object.keys(table).join(', ')})`);
|
|
202
200
|
}
|
package/assets/bake-svg.mjs
CHANGED
|
@@ -303,11 +303,12 @@ function urlRef(v) {
|
|
|
303
303
|
return m ? m[1] : null;
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
-
/** A gradient coordinate: "50%" -> 0.5; a plain number passes through. */
|
|
306
|
+
/** A gradient coordinate: "50%" -> 0.5; a plain number passes through; NaN or infinite falls back to dflt. */
|
|
307
307
|
function gradCoord(v, dflt) {
|
|
308
308
|
if (v == null) return dflt;
|
|
309
309
|
const s = String(v).trim();
|
|
310
|
-
|
|
310
|
+
const n = s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s);
|
|
311
|
+
return Number.isFinite(n) ? n : dflt;
|
|
311
312
|
}
|
|
312
313
|
|
|
313
314
|
/** Local-space bounding box of a path-op array (no SHAPE header; MOVE/LINE/QUAD/CUBIC, CLOSE has no args). */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "embedded-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.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",
|
package/sim/embedded-react.wasm
CHANGED
|
Binary file
|
|
@@ -21,6 +21,31 @@ import {createElement, useRef, useEffect} from 'react';
|
|
|
21
21
|
import {NativeUI} from '../native-ui.js';
|
|
22
22
|
import {deepEqualProps} from '../props.js';
|
|
23
23
|
|
|
24
|
+
// The style keys an animated value can drive only through an interpolation onto colors.
|
|
25
|
+
const COLOR_PROPS = new Set(['backgroundColor', 'color']);
|
|
26
|
+
|
|
27
|
+
let _warnedColorBind = false;
|
|
28
|
+
let _warnedNumberBind = false;
|
|
29
|
+
|
|
30
|
+
/** Warns once that `prop` takes a color, which a plain float or a numeric outputRange cannot give it. */
|
|
31
|
+
function warnColorBind(prop) {
|
|
32
|
+
if (_warnedColorBind) return;
|
|
33
|
+
_warnedColorBind = true;
|
|
34
|
+
console.warn(
|
|
35
|
+
`embedded-react: an animated "${prop}" needs colors: bind value.interpolate({inputRange, ` +
|
|
36
|
+
`outputRange: ['#000', '#fff']}) rather than the value itself, which is ignored.`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Warns once that `prop` takes numbers, which an outputRange holding anything else cannot give it. */
|
|
41
|
+
function warnNumberBind(prop) {
|
|
42
|
+
if (_warnedNumberBind) return;
|
|
43
|
+
_warnedNumberBind = true;
|
|
44
|
+
console.warn(
|
|
45
|
+
`embedded-react: an animated "${prop}" takes an outputRange of numbers; this one is ignored.`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
24
49
|
/** A standalone animatable value bound to an engine-side float. */
|
|
25
50
|
export class AnimatedValue {
|
|
26
51
|
constructor(initial = 0) {
|
|
@@ -53,6 +78,10 @@ export class AnimatedValue {
|
|
|
53
78
|
__bind(node, prop) {
|
|
54
79
|
// The engine applies the current value on bind and ignores duplicate (node, prop) pairs, so
|
|
55
80
|
// this is safe to call on every render.
|
|
81
|
+
if (COLOR_PROPS.has(prop)) {
|
|
82
|
+
warnColorBind(prop);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
56
85
|
NativeUI.animValueBind(this._handle, node, prop);
|
|
57
86
|
}
|
|
58
87
|
|
|
@@ -74,6 +103,18 @@ export class AnimatedInterpolation {
|
|
|
74
103
|
}
|
|
75
104
|
|
|
76
105
|
__bind(node, prop) {
|
|
106
|
+
const out = this._config?.outputRange;
|
|
107
|
+
if (Array.isArray(out)) {
|
|
108
|
+
if (COLOR_PROPS.has(prop)) {
|
|
109
|
+
if (out.some(c => typeof c !== 'string')) {
|
|
110
|
+
warnColorBind(prop);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
} else if (out.some(n => typeof n !== 'number')) {
|
|
114
|
+
warnNumberBind(prop);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
77
118
|
NativeUI.animValueBindInterpolated(
|
|
78
119
|
this._parent._handle,
|
|
79
120
|
node,
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
// the same: drop to <Pressable> + <Text> and style them yourself. `color` and `disabled` are the two
|
|
30
30
|
// knobs, exactly as upstream.
|
|
31
31
|
import {createElement} from 'react';
|
|
32
|
-
import {
|
|
32
|
+
import {Text} from './components.js';
|
|
33
|
+
import {Pressable} from './Pressable.js';
|
|
33
34
|
import {createPropWarner, RN_PLATFORM_NO_OPS} from './warn-props.js';
|
|
34
35
|
|
|
35
36
|
// RN's Button styles are Platform.select({ios, android}) and Platform.OS is 'embedded', so neither
|
|
@@ -85,9 +86,7 @@ export function Button(props = {}) {
|
|
|
85
86
|
{
|
|
86
87
|
style: buttonStyles,
|
|
87
88
|
disabled,
|
|
88
|
-
|
|
89
|
-
// in name only (the engine has no such node flag), so a node left holding onPress still fires.
|
|
90
|
-
onPress: disabled ? undefined : onPress,
|
|
89
|
+
onPress,
|
|
91
90
|
},
|
|
92
91
|
createElement(
|
|
93
92
|
Text,
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 Cory Lamming
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// Pressable — RN's core press target, as a forwardRef component over the host <Pressable> element.
|
|
18
|
+
//
|
|
19
|
+
// Setting `disabled` withholds all press-related event handlers (`onPress`, `onLongPress`, `onPressIn`,
|
|
20
|
+
// `onPressOut`) so that NativeUI never registers callbacks on the native node. The C hit-testing
|
|
21
|
+
// logic skips nodes without press handlers, preventing disabled pressables from claiming the gesture responder.
|
|
22
|
+
import {createElement, forwardRef} from 'react';
|
|
23
|
+
import {Pressable as HostPressable} from './components.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A container that responds to touch interactions.
|
|
27
|
+
*
|
|
28
|
+
* When `disabled` is true, all press handlers are withheld so the node will not intercept touches
|
|
29
|
+
* or fire callbacks.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} [props] Pressable props.
|
|
32
|
+
* @param {boolean} [props.disabled] Whether touch interactions are disabled.
|
|
33
|
+
* @param {Function} [props.onPress] Called when a press is activated.
|
|
34
|
+
* @param {Function} [props.onLongPress] Called when a long press is activated.
|
|
35
|
+
* @param {Function} [props.onPressIn] Called immediately when a touch is pressed down.
|
|
36
|
+
* @param {Function} [props.onPressOut] Called when a touch is released or canceled.
|
|
37
|
+
* @param {*} [props.children] Children elements.
|
|
38
|
+
* @returns {*} A host <Pressable> element.
|
|
39
|
+
*/
|
|
40
|
+
export const Pressable = forwardRef(function Pressable(props = {}, ref) {
|
|
41
|
+
const {
|
|
42
|
+
disabled,
|
|
43
|
+
onPress,
|
|
44
|
+
onLongPress,
|
|
45
|
+
onPressIn,
|
|
46
|
+
onPressOut,
|
|
47
|
+
...rest
|
|
48
|
+
} = props;
|
|
49
|
+
|
|
50
|
+
return createElement(HostPressable, {
|
|
51
|
+
...rest,
|
|
52
|
+
ref,
|
|
53
|
+
disabled,
|
|
54
|
+
onPress: disabled ? undefined : onPress,
|
|
55
|
+
onLongPress: disabled ? undefined : onLongPress,
|
|
56
|
+
onPressIn: disabled ? undefined : onPressIn,
|
|
57
|
+
onPressOut: disabled ? undefined : onPressOut,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -37,13 +37,13 @@ import {
|
|
|
37
37
|
useMemo,
|
|
38
38
|
useRef,
|
|
39
39
|
} from 'react';
|
|
40
|
-
import {Pressable} from './
|
|
40
|
+
import {Pressable} from './Pressable.js';
|
|
41
41
|
import {timing, useAnimatedValue} from './Animated.js';
|
|
42
42
|
import {Easing} from './Easing.js';
|
|
43
43
|
import {flattenStyleObj} from '../props.js';
|
|
44
44
|
|
|
45
45
|
// RN's own numbers (Libraries/Components/Touchable/TouchableOpacity.js): the dim lands on touch-down
|
|
46
|
-
// with no ramp at all
|
|
46
|
+
// with no ramp at all and fades back over a quarter second once the finger lifts.
|
|
47
47
|
const DEFAULT_ACTIVE_OPACITY = 0.2;
|
|
48
48
|
const DIM_MS = 0;
|
|
49
49
|
const RESTORE_MS = 250;
|
|
@@ -140,13 +140,10 @@ export const TouchableOpacity = forwardRef(
|
|
|
140
140
|
ref,
|
|
141
141
|
style: composedStyle,
|
|
142
142
|
disabled,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
onLongPress: disabled ? undefined : onLongPress,
|
|
148
|
-
onPressIn: disabled ? undefined : handlePressIn,
|
|
149
|
-
onPressOut: disabled ? undefined : handlePressOut,
|
|
143
|
+
onPress,
|
|
144
|
+
onLongPress,
|
|
145
|
+
onPressIn: handlePressIn,
|
|
146
|
+
onPressOut: handlePressOut,
|
|
150
147
|
});
|
|
151
148
|
},
|
|
152
149
|
);
|