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
package/aot/compiler/collect.mjs
CHANGED
|
@@ -21,7 +21,9 @@ import { parse } from '@babel/parser';
|
|
|
21
21
|
import { readFileSync, existsSync } from 'node:fs';
|
|
22
22
|
import { resolve } from 'node:path';
|
|
23
23
|
import { aotError } from './diagnostics.mjs';
|
|
24
|
-
import {
|
|
24
|
+
import { mergeBranchElements } from './branch-merge.mjs';
|
|
25
|
+
import { bootCheckpoint, foldBoot, isBoot, nameBootValues, screenBinding, } from './boot.mjs';
|
|
26
|
+
import { evalStaticOrThrow, evalStatic, setBinding, withUndefined, } from './static-eval.mjs';
|
|
25
27
|
import { cConstant, cTypeOfValue } from './c-syntax.mjs';
|
|
26
28
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
27
29
|
- Constants
|
|
@@ -125,7 +127,20 @@ export function declaredNames(body) {
|
|
|
125
127
|
return names;
|
|
126
128
|
}
|
|
127
129
|
/**
|
|
128
|
-
*
|
|
130
|
+
* A const's folded value, with the boot values in it named after the const.
|
|
131
|
+
*
|
|
132
|
+
* @param name The const's name.
|
|
133
|
+
* @param value Its folded value.
|
|
134
|
+
*
|
|
135
|
+
* @returns The value.
|
|
136
|
+
*/
|
|
137
|
+
export function bindConst(name, value) {
|
|
138
|
+
nameBootValues(value, name);
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The module's compile-time scope: `screen`, the seed, and every module-level `const` that folds. A const
|
|
143
|
+
* derived from `screen` is a boot value (see BootValue).
|
|
129
144
|
*
|
|
130
145
|
* @param program The parsed module.
|
|
131
146
|
* @param screen The panel size in this build folds its layout at.
|
|
@@ -135,7 +150,7 @@ export function declaredNames(body) {
|
|
|
135
150
|
*/
|
|
136
151
|
export function moduleScope(program, screen, seed = {}) {
|
|
137
152
|
// Seed the scope (image imports as asset names) before folding module consts, so a const can reference them.
|
|
138
|
-
const scope = { screen, ...seed };
|
|
153
|
+
const scope = { screen: screenBinding(screen), ...seed };
|
|
139
154
|
// Fold module variables in order so later ones see earlier ones; skip functions, destructuring and non-folds.
|
|
140
155
|
for (const stmt of program.body) {
|
|
141
156
|
const declaration = stmt.type === 'ExportNamedDeclaration' ? stmt.declaration : stmt;
|
|
@@ -149,7 +164,7 @@ export function moduleScope(program, screen, seed = {}) {
|
|
|
149
164
|
continue;
|
|
150
165
|
}
|
|
151
166
|
try {
|
|
152
|
-
scope
|
|
167
|
+
setBinding(scope, decl.id.name, bindConst(decl.id.name, evalStatic(decl.init, withUndefined(scope), 'boot')));
|
|
153
168
|
}
|
|
154
169
|
catch {
|
|
155
170
|
/* not a static const — skip */
|
|
@@ -190,6 +205,34 @@ function inferItemStruct(items, name) {
|
|
|
190
205
|
});
|
|
191
206
|
return { fields };
|
|
192
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Refuses an initial value too big for its fixed buffer: C would cut it short at boot without a word.
|
|
210
|
+
*
|
|
211
|
+
* @param stateName The state's JS name.
|
|
212
|
+
* @param items The list's initial rows, or null for a scalar.
|
|
213
|
+
* @param strings The initial strings it starts with, as `[what, text]` pairs.
|
|
214
|
+
* @param caps The compile's caps.
|
|
215
|
+
* @param initArg The initial value, for the error's location.
|
|
216
|
+
*/
|
|
217
|
+
function checkInitialFits(stateName, items, strings, caps, initArg) {
|
|
218
|
+
let error = null;
|
|
219
|
+
if (items && items.length > caps.listCap) {
|
|
220
|
+
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.');
|
|
221
|
+
}
|
|
222
|
+
// A string buffer keeps one byte for the terminator.
|
|
223
|
+
const tooLong = strings.find(([, text]) => Buffer.byteLength(text, 'utf8') >= caps.listStrCap);
|
|
224
|
+
if (!error && tooLong) {
|
|
225
|
+
const [what, text] = tooLong;
|
|
226
|
+
error = aotError(`AOT: ${what} starts ${Buffer.byteLength(text, 'utf8')} bytes long, but a string holds ` +
|
|
227
|
+
`${caps.listStrCap - 1} bytes`, 'raise ER_AOT_LIST_STR_CAP (the listStrCap option), or shorten the string.');
|
|
228
|
+
}
|
|
229
|
+
if (error) {
|
|
230
|
+
if (initArg?.loc) {
|
|
231
|
+
error.aotLoc = initArg.loc.start;
|
|
232
|
+
}
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
193
236
|
/**
|
|
194
237
|
* Collects a component's useState declarations → state descriptors keyed by JS name (reads) and setter
|
|
195
238
|
* name (writes). `prefix` namespaces the C STORAGE so each inlined child instance gets its own slots: the
|
|
@@ -258,6 +301,12 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
258
301
|
}
|
|
259
302
|
throw error;
|
|
260
303
|
}
|
|
304
|
+
checkInitialFits(name, items, items.flatMap(item => struct.fields
|
|
305
|
+
.filter(field => field.kind === 'string')
|
|
306
|
+
.map((field) => [
|
|
307
|
+
`field "${field.key}" of list state "${name}"`,
|
|
308
|
+
String(item[field.key] ?? ''),
|
|
309
|
+
])), caps, initArg);
|
|
261
310
|
record = {
|
|
262
311
|
name,
|
|
263
312
|
cField,
|
|
@@ -325,6 +374,9 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
325
374
|
else if (cType === 'i64') {
|
|
326
375
|
cConstant(initial, cType); // BigInt refuses a value that is not a whole number
|
|
327
376
|
}
|
|
377
|
+
else if (cType === 'string') {
|
|
378
|
+
checkInitialFits(name, null, [[`state "${name}"`, String(initial)]], caps, initArg);
|
|
379
|
+
}
|
|
328
380
|
record = {
|
|
329
381
|
name,
|
|
330
382
|
cField,
|
|
@@ -348,8 +400,9 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
348
400
|
return { byName, bySetter };
|
|
349
401
|
}
|
|
350
402
|
/**
|
|
351
|
-
* The JSX a component body returns. A top-level `if` whose test folds
|
|
352
|
-
* responsive layout returns the branch for this build's screen.
|
|
403
|
+
* The JSX a component body returns. A top-level `if` whose test folds is decided at compile time, so a
|
|
404
|
+
* responsive layout returns the branch for this build's screen. When the test depends on the panel and both
|
|
405
|
+
* outcomes return elements of one shape, they merge into one element whose values boot picks.
|
|
353
406
|
*
|
|
354
407
|
* @param fnBody The component's body.
|
|
355
408
|
* @param scope Compile-time constants the `if` tests fold against.
|
|
@@ -357,23 +410,31 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
357
410
|
* @returns The returned element.
|
|
358
411
|
*/
|
|
359
412
|
export function findReturnJSX(fnBody, scope = {}) {
|
|
413
|
+
const statementsOf = (branch) => branch.type === 'BlockStatement' ? branch.body : [branch];
|
|
360
414
|
// Fold top-level `if (staticCond) return …` at compile time — responsive layouts switch on `screen`.
|
|
361
415
|
const scan = (statements) => {
|
|
362
|
-
for (const stmt of statements) {
|
|
416
|
+
for (const [index, stmt] of statements.entries()) {
|
|
363
417
|
// A top-level `if` must fold; only the branch it takes is scanned.
|
|
364
418
|
if (stmt.type === 'IfStatement') {
|
|
365
419
|
let testValue;
|
|
366
420
|
try {
|
|
367
|
-
testValue = evalStatic(stmt.test, scope);
|
|
421
|
+
testValue = evalStatic(stmt.test, scope, 'boot');
|
|
368
422
|
}
|
|
369
423
|
catch {
|
|
370
424
|
throw new Error('AOT: a top-level `if` in the component must have a compile-time-constant test (e.g. ' +
|
|
371
425
|
'on the `screen` global) — runtime layout branching is not supported');
|
|
372
426
|
}
|
|
427
|
+
// A test that depends on the panel needs no variant when both outcomes have one shape.
|
|
428
|
+
if (isBoot(testValue)) {
|
|
429
|
+
const merged = mergeOutcomes(stmt, statements.slice(index + 1));
|
|
430
|
+
if (merged)
|
|
431
|
+
return merged;
|
|
432
|
+
testValue = foldBoot(testValue);
|
|
433
|
+
}
|
|
373
434
|
// Follow only the statically selected branch and keep scanning it until a JSX return is found.
|
|
374
435
|
const branch = testValue ? stmt.consequent : stmt.alternate;
|
|
375
436
|
if (branch) {
|
|
376
|
-
const branchJSX = scan(branch
|
|
437
|
+
const branchJSX = scan(statementsOf(branch));
|
|
377
438
|
if (branchJSX)
|
|
378
439
|
return branchJSX;
|
|
379
440
|
}
|
|
@@ -388,6 +449,24 @@ export function findReturnJSX(fnBody, scope = {}) {
|
|
|
388
449
|
}
|
|
389
450
|
return null;
|
|
390
451
|
};
|
|
452
|
+
// What each outcome of an `if` returns (a branch that does not return falls through), merged when it can be.
|
|
453
|
+
const mergeOutcomes = (stmt, rest) => {
|
|
454
|
+
const restore = bootCheckpoint();
|
|
455
|
+
try {
|
|
456
|
+
const whenTrue = scan(statementsOf(stmt.consequent)) ?? scan(rest);
|
|
457
|
+
const whenFalse = (stmt.alternate && scan(statementsOf(stmt.alternate))) ?? scan(rest);
|
|
458
|
+
const merged = whenTrue && whenFalse
|
|
459
|
+
? mergeBranchElements(stmt.test, whenTrue, whenFalse)
|
|
460
|
+
: null;
|
|
461
|
+
if (merged)
|
|
462
|
+
return merged;
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
/* an outcome that does not compile here is left to the fold, which reports it if it is taken */
|
|
466
|
+
}
|
|
467
|
+
restore();
|
|
468
|
+
return null;
|
|
469
|
+
};
|
|
391
470
|
// Scan the body; a component that never returns is an error.
|
|
392
471
|
const returnJSX = scan(fnBody.body);
|
|
393
472
|
if (!returnJSX) {
|
|
@@ -718,6 +797,13 @@ export function collectEffects(fnBody) {
|
|
|
718
797
|
if (!isFn(call.arguments[0])) {
|
|
719
798
|
throw aotError('AOT: useEffect must take an inline function', 'write useEffect(() => { … }, []).');
|
|
720
799
|
}
|
|
800
|
+
// React runs an effect with no deps after every render, which Flow B has no lowering for.
|
|
801
|
+
if (call.arguments.length < 2) {
|
|
802
|
+
const error = aotError('AOT: useEffect needs a dependency list', 'pass `[]` (run once) or `[a, b]` (re-run when a/b change).');
|
|
803
|
+
if (call.loc)
|
|
804
|
+
error.aotLoc = call.loc.start;
|
|
805
|
+
throw error;
|
|
806
|
+
}
|
|
721
807
|
effects.push({
|
|
722
808
|
fn: call.arguments[0],
|
|
723
809
|
deps: call.arguments[1],
|
|
@@ -17,9 +17,11 @@
|
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { aotError } from './diagnostics.mjs';
|
|
20
|
-
import {
|
|
20
|
+
import { foldBoot, isBoot } from './boot.mjs';
|
|
21
|
+
import { mergeBranchElements } from './branch-merge.mjs';
|
|
22
|
+
import { evalStatic, setBinding, withUndefined } from './static-eval.mjs';
|
|
21
23
|
import { emitSourcedWide, lowerExpr } from './expressions.mjs';
|
|
22
|
-
import { isFn, declaredNames, collectState, componentReturnJSX, collectHelpers, collectCallbacks, collectMemos, collectEffects, usesState, } from './collect.mjs';
|
|
24
|
+
import { isFn, declaredNames, collectState, componentReturnJSX, collectHelpers, collectCallbacks, collectMemos, collectEffects, usesState, bindConst, } from './collect.mjs';
|
|
23
25
|
import { collectAnims, collectRefs } from './animations.mjs';
|
|
24
26
|
import { attrExpr } from './style-text.mjs';
|
|
25
27
|
import { inlineFragments } from './elements/props.mjs';
|
|
@@ -93,9 +95,10 @@ function extractProps(openingElement, scope, env) {
|
|
|
93
95
|
}
|
|
94
96
|
// Any other prop folds to a constant when it can, else becomes a runtime C expression.
|
|
95
97
|
try {
|
|
96
|
-
|
|
98
|
+
const propName = attr.name.name;
|
|
99
|
+
props[propName] = {
|
|
97
100
|
static: true,
|
|
98
|
-
value: evalStatic(node, scope),
|
|
101
|
+
value: bindConst(propName, evalStatic(node, scope, 'boot')),
|
|
99
102
|
};
|
|
100
103
|
}
|
|
101
104
|
catch {
|
|
@@ -112,10 +115,11 @@ function extractProps(openingElement, scope, env) {
|
|
|
112
115
|
*
|
|
113
116
|
* @param fn The component.
|
|
114
117
|
* @param props The props the instance was given.
|
|
118
|
+
* @param scope Module constants a default can refer to.
|
|
115
119
|
*
|
|
116
120
|
* @returns The descriptor each name in the component's body is bound to.
|
|
117
121
|
*/
|
|
118
|
-
function bindParams(fn, props) {
|
|
122
|
+
function bindParams(fn, props, scope) {
|
|
119
123
|
const bindings = new Map();
|
|
120
124
|
const param = fn.params[0];
|
|
121
125
|
if (!param)
|
|
@@ -133,11 +137,11 @@ function bindParams(fn, props) {
|
|
|
133
137
|
}
|
|
134
138
|
else if (param.type === 'ObjectPattern') {
|
|
135
139
|
// `function C({x, y: renamed, z = 1})`: bind each name to its prop, else to its folded default.
|
|
136
|
-
|
|
140
|
+
const properties = param.properties.map(property => {
|
|
137
141
|
if (property.type === 'RestElement') {
|
|
138
142
|
throw new Error('AOT: rest props (...rest) in a component param not supported');
|
|
139
143
|
}
|
|
140
|
-
// Resolve a destructured prop's source name
|
|
144
|
+
// Resolve a destructured prop's source name and local binding name.
|
|
141
145
|
const propName = property.key.name ??
|
|
142
146
|
property.key.value;
|
|
143
147
|
const bindName = property.value?.type === 'Identifier'
|
|
@@ -145,14 +149,24 @@ function bindParams(fn, props) {
|
|
|
145
149
|
: property.value?.type === 'AssignmentPattern'
|
|
146
150
|
? property.value.left.name
|
|
147
151
|
: propName;
|
|
152
|
+
return { property, propName, bindName };
|
|
153
|
+
});
|
|
154
|
+
// As in JS, the pattern's names shadow module ones in a default, and each is in reach only once bound.
|
|
155
|
+
const defaultScope = { ...scope };
|
|
156
|
+
for (const { bindName } of properties)
|
|
157
|
+
delete defaultScope[bindName];
|
|
158
|
+
for (const { property, propName, bindName } of properties) {
|
|
148
159
|
let descriptor = props[propName];
|
|
149
160
|
if (!descriptor && property.value?.type === 'AssignmentPattern') {
|
|
150
161
|
descriptor = {
|
|
151
162
|
static: true,
|
|
152
|
-
value: evalStatic(property.value.right,
|
|
163
|
+
value: bindConst(bindName, evalStatic(property.value.right, defaultScope, 'boot')),
|
|
153
164
|
};
|
|
154
165
|
}
|
|
155
|
-
|
|
166
|
+
descriptor ??= { static: true, value: undefined };
|
|
167
|
+
if (descriptor.static)
|
|
168
|
+
setBinding(defaultScope, bindName, descriptor.value);
|
|
169
|
+
bindings.set(bindName, descriptor);
|
|
156
170
|
}
|
|
157
171
|
}
|
|
158
172
|
else {
|
|
@@ -219,12 +233,13 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
219
233
|
childrenRef = { kind: 'props', name: param.name };
|
|
220
234
|
}
|
|
221
235
|
// A child sees only module scope: start from module consts with no locals, so caller bindings cannot leak in.
|
|
222
|
-
const
|
|
236
|
+
const moduleScope = env.moduleConsts ?? scope;
|
|
237
|
+
const childScope = { ...moduleScope };
|
|
223
238
|
const childLocals = new Map();
|
|
224
239
|
// Callback props resolve to the caller's function, env and state; inherit the caller's own so they forward.
|
|
225
240
|
const fnProps = new Map(env.fnProps);
|
|
226
241
|
// Bind each prop: a callback to fnProps, a constant to the child's scope, a runtime value to its locals.
|
|
227
|
-
for (const [name, descriptor] of bindParams(fn, extractProps(element.openingElement, scope, env))) {
|
|
242
|
+
for (const [name, descriptor] of bindParams(fn, extractProps(element.openingElement, scope, env), moduleScope)) {
|
|
228
243
|
// The children binding comes from the slot, not from a value prop.
|
|
229
244
|
if (childrenRef?.kind === 'local' && name === childrenRef?.name)
|
|
230
245
|
continue;
|
|
@@ -236,7 +251,7 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
236
251
|
});
|
|
237
252
|
}
|
|
238
253
|
else if (descriptor.static) {
|
|
239
|
-
childScope
|
|
254
|
+
setBinding(childScope, name, descriptor.value);
|
|
240
255
|
}
|
|
241
256
|
else {
|
|
242
257
|
childLocals.set(name, {
|
|
@@ -277,7 +292,7 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
277
292
|
if (childLocals.has(decl.id.name))
|
|
278
293
|
continue; // a dynamic prop of that name is already bound
|
|
279
294
|
try {
|
|
280
|
-
childScope
|
|
295
|
+
setBinding(childScope, decl.id.name, bindConst(decl.id.name, evalStatic(decl.init, withUndefined(childScope), 'boot')));
|
|
281
296
|
}
|
|
282
297
|
catch {
|
|
283
298
|
// A dynamic const hides any same-named module const; a memo re-binds it below, else it stays unresolved.
|
|
@@ -324,7 +339,7 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
324
339
|
// Resolve memos in order (fold, else a local C expr), then compile its effects, all in the child's env/state.
|
|
325
340
|
for (const [name, expr] of childMemos) {
|
|
326
341
|
try {
|
|
327
|
-
childScope
|
|
342
|
+
setBinding(childScope, name, bindConst(name, evalStatic(expr, childScope, 'boot')));
|
|
328
343
|
}
|
|
329
344
|
catch {
|
|
330
345
|
const cExpr = emitSourcedWide(expr, childEnv);
|
|
@@ -398,10 +413,10 @@ function emitMap(call, scope, out, env, state) {
|
|
|
398
413
|
return items.map((item, index) => {
|
|
399
414
|
const rowScope = { ...scope };
|
|
400
415
|
if (itemName) {
|
|
401
|
-
rowScope
|
|
416
|
+
setBinding(rowScope, itemName, item);
|
|
402
417
|
}
|
|
403
418
|
if (indexName) {
|
|
404
|
-
rowScope
|
|
419
|
+
setBinding(rowScope, indexName, index);
|
|
405
420
|
}
|
|
406
421
|
return emitElementInto(rowJSX, rowScope, out, rowEnv(env, [itemName, indexName], rowScope), state);
|
|
407
422
|
});
|
|
@@ -481,7 +496,7 @@ function emitDynamicMap(call, listState, scope, out, env, state) {
|
|
|
481
496
|
delete rowScope[itemName];
|
|
482
497
|
}
|
|
483
498
|
if (indexName) {
|
|
484
|
-
rowScope
|
|
499
|
+
setBinding(rowScope, indexName, rowIndex); // the index is a compile-time literal per pooled row
|
|
485
500
|
}
|
|
486
501
|
const rowLocals = new Map(env.locals);
|
|
487
502
|
if (indexName) {
|
|
@@ -492,6 +507,7 @@ function emitDynamicMap(call, listState, scope, out, env, state) {
|
|
|
492
507
|
rowLocals.set(itemName, {
|
|
493
508
|
code: `${listState.arrayName}[${rowIndex}]`,
|
|
494
509
|
struct: listState.struct,
|
|
510
|
+
list: listState,
|
|
495
511
|
});
|
|
496
512
|
}
|
|
497
513
|
rows.push(emitElementInto(rowJSX, rowScope, out, rowEnv(env, [itemName, indexName], rowScope, rowLocals), state, { show: { kind: 'rowInUse', row: rowIndex, list: listState } }));
|
|
@@ -558,11 +574,20 @@ function lowerBranch(expr, scope, out, env, state, within) {
|
|
|
558
574
|
// A test that folds picks its branch at build time; `&&` with a false test renders nothing.
|
|
559
575
|
let folded;
|
|
560
576
|
try {
|
|
561
|
-
folded = { value: evalStatic(testNode, scope) };
|
|
577
|
+
folded = { value: evalStatic(testNode, scope, 'boot') };
|
|
562
578
|
}
|
|
563
579
|
catch {
|
|
564
580
|
folded = null;
|
|
565
581
|
}
|
|
582
|
+
// A test that depends on the panel needs no variant when both branches are elements of one shape.
|
|
583
|
+
if (folded && isBoot(folded.value)) {
|
|
584
|
+
const merged = whenTrue.type === 'JSXElement' && whenFalse?.type === 'JSXElement'
|
|
585
|
+
? mergeBranchElements(testNode, whenTrue, whenFalse)
|
|
586
|
+
: null;
|
|
587
|
+
if (merged)
|
|
588
|
+
return lowerBranch(merged, scope, out, env, state, within);
|
|
589
|
+
folded = { value: foldBoot(folded.value) };
|
|
590
|
+
}
|
|
566
591
|
if (folded) {
|
|
567
592
|
// A false `&&` yields its left value, which must be one React renders as nothing (`0` is text).
|
|
568
593
|
if (isAnd && !folded.value && !rendersNothing(folded.value)) {
|
|
@@ -38,6 +38,39 @@ export function currentLoc() {
|
|
|
38
38
|
}
|
|
39
39
|
return undefined;
|
|
40
40
|
}
|
|
41
|
+
/** The most bytes each of the engine's fixed string buffers holds, as er_scene.h defines them. */
|
|
42
|
+
export const ENGINE_STRING_MAX = {
|
|
43
|
+
ER_TEXT_MAX: 255,
|
|
44
|
+
ER_SPAN_TEXT_MAX: 63,
|
|
45
|
+
ER_PLACEHOLDER_MAX: 63,
|
|
46
|
+
ER_IMAGE_NAME_MAX: 63,
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* A text's string when every part of it is known at compile time.
|
|
50
|
+
*
|
|
51
|
+
* @param text The text.
|
|
52
|
+
*
|
|
53
|
+
* @returns The string, or null when a part is a run-time value.
|
|
54
|
+
*/
|
|
55
|
+
export const constantText = (text) => text.parts.every(part => part.kind === 'literal')
|
|
56
|
+
? text.parts.map(part => part.text).join('')
|
|
57
|
+
: null;
|
|
58
|
+
/**
|
|
59
|
+
* Refuses a constant string too long for the engine buffer it is copied into, which C would cut short.
|
|
60
|
+
*
|
|
61
|
+
* @param text The string, or null for one built at run time (checked when it is written).
|
|
62
|
+
* @param limit The er_scene.h define that sizes the buffer.
|
|
63
|
+
* @param what What the string is, for the error.
|
|
64
|
+
*/
|
|
65
|
+
export function checkEngineString(text, limit, what) {
|
|
66
|
+
if (text === null) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const bytes = Buffer.byteLength(text, 'utf8');
|
|
70
|
+
if (bytes > ENGINE_STRING_MAX[limit]) {
|
|
71
|
+
throw aotError(`AOT: ${what} is ${bytes} bytes, but the engine holds ${ENGINE_STRING_MAX[limit]} (${limit})`, `shorten it: ${limit} in er_scene.h sizes the engine's buffer.`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
41
74
|
/**
|
|
42
75
|
* Builds an AOT error carrying an optional `hint` (a "rewrite it like this" suggestion shown to the user).
|
|
43
76
|
*
|
|
@@ -109,7 +109,7 @@ export function emitDial(element, scope, out, env, state) {
|
|
|
109
109
|
const numeric = (field, node, isFloat, attr) => {
|
|
110
110
|
let number;
|
|
111
111
|
try {
|
|
112
|
-
number = evalStatic(node, scope);
|
|
112
|
+
number = evalStatic(node, scope, 'strict');
|
|
113
113
|
}
|
|
114
114
|
catch {
|
|
115
115
|
const value = lowerExpr(node, env);
|
|
@@ -135,7 +135,7 @@ export function emitDial(element, scope, out, env, state) {
|
|
|
135
135
|
const colour = (field, node, attr) => {
|
|
136
136
|
let color;
|
|
137
137
|
try {
|
|
138
|
-
color = evalStatic(node, scope);
|
|
138
|
+
color = evalStatic(node, scope, 'strict');
|
|
139
139
|
}
|
|
140
140
|
catch {
|
|
141
141
|
dynAssigns.push({ field, value: lowerColorValue(node, env) });
|
|
@@ -182,7 +182,7 @@ export function emitDial(element, scope, out, env, state) {
|
|
|
182
182
|
const { field, table } = DIAL_ENUM_PROPS[name];
|
|
183
183
|
let token = null;
|
|
184
184
|
try {
|
|
185
|
-
token = evalStatic(node, scope);
|
|
185
|
+
token = evalStatic(node, scope, 'strict');
|
|
186
186
|
}
|
|
187
187
|
catch {
|
|
188
188
|
/* state-driven — handled below */
|
|
@@ -203,7 +203,10 @@ export function emitDial(element, scope, out, env, state) {
|
|
|
203
203
|
else if (name === 'adjustable' || name === 'range') {
|
|
204
204
|
const field = name === 'range' ? 'arc_range' : 'arc_adjustable';
|
|
205
205
|
try {
|
|
206
|
-
staticAssigns.push({
|
|
206
|
+
staticAssigns.push({
|
|
207
|
+
field,
|
|
208
|
+
expr: evalStatic(node, scope, 'strict') ? '1' : '0',
|
|
209
|
+
});
|
|
207
210
|
}
|
|
208
211
|
catch {
|
|
209
212
|
dynAssigns.push({
|
|
@@ -303,9 +306,7 @@ export function emitDial(element, scope, out, env, state) {
|
|
|
303
306
|
stateProps: dynAssigns,
|
|
304
307
|
placeholder: null,
|
|
305
308
|
text: null,
|
|
306
|
-
image: knobImage != null
|
|
307
|
-
? { kind: 'static', name: knobImage, resetOnUpdate: true }
|
|
308
|
-
: null,
|
|
309
|
+
image: knobImage != null ? { kind: 'static', name: knobImage } : null,
|
|
309
310
|
spans: null,
|
|
310
311
|
children: [],
|
|
311
312
|
wiring: binds.map((bind, index) => ({ kind: 'bind', bind, index })),
|
|
@@ -87,6 +87,15 @@ const VIEW_STYLE = [
|
|
|
87
87
|
'borderColor',
|
|
88
88
|
'opacity',
|
|
89
89
|
];
|
|
90
|
+
/** What the engine draws an Image with, beyond its props: the corners its bitmap is clipped to, and its opacity. */
|
|
91
|
+
const IMAGE_STYLE = [
|
|
92
|
+
'borderRadius',
|
|
93
|
+
'borderTopLeftRadius',
|
|
94
|
+
'borderTopRightRadius',
|
|
95
|
+
'borderBottomRightRadius',
|
|
96
|
+
'borderBottomLeftRadius',
|
|
97
|
+
'opacity',
|
|
98
|
+
];
|
|
90
99
|
/** What the engine draws a Text with. */
|
|
91
100
|
const TEXT_STYLE = [
|
|
92
101
|
'color',
|
|
@@ -94,6 +103,7 @@ const TEXT_STYLE = [
|
|
|
94
103
|
'fontWeight',
|
|
95
104
|
'lineHeight',
|
|
96
105
|
'letterSpacing',
|
|
106
|
+
'opacity',
|
|
97
107
|
];
|
|
98
108
|
/** What the engine draws a Modal's own box with. */
|
|
99
109
|
const MODAL_STYLE = [
|
|
@@ -108,7 +118,9 @@ const TEXT_INPUT_STYLE = [...MODAL_STYLE, 'color', 'fontSize'];
|
|
|
108
118
|
/** The style keys the engine animates on a View, ScrollView, Pressable or Modal; `transform` animates anywhere. */
|
|
109
119
|
const VIEW_ANIMATED = ['opacity', 'backgroundColor', 'transform'];
|
|
110
120
|
/** The style keys the engine animates on a Text. */
|
|
111
|
-
const TEXT_ANIMATED = ['color', 'transform'];
|
|
121
|
+
const TEXT_ANIMATED = ['color', 'opacity', 'transform'];
|
|
122
|
+
/** The style keys the engine animates on an Image or a TextInput. */
|
|
123
|
+
const OPACITY_ANIMATED = ['opacity', 'transform'];
|
|
112
124
|
/** What a span of a multi-span <Text> carries; the rest of its look comes from the outer <Text>. */
|
|
113
125
|
const SPAN_STYLE = ['color', 'fontSize', 'fontWeight', 'letterSpacing'];
|
|
114
126
|
/** The paint a vector shape or group takes. */
|
|
@@ -135,8 +147,6 @@ const HOST_PROPS = [
|
|
|
135
147
|
const NO_TEXT_INPUT_FIELD = "the engine's TextInput has no such setting, so the device would ignore it. Remove it.";
|
|
136
148
|
/** Why an <Svg> refuses an event: its node is a drawing, not a touch target of its own. */
|
|
137
149
|
const SVG_EVENT = 'wrap it in a <Pressable> (or a <View>) that takes the event: <Pressable onPress={…}><Svg …/></Pressable>.';
|
|
138
|
-
/** Why an <Svg> refuses paint on itself: Flow A does not inherit it either. */
|
|
139
|
-
const SVG_ROOT_PAINT = 'paint goes on the shapes, or on a <G> around them; the <Svg> itself passes none down.';
|
|
140
150
|
/** The built-in elements, by tag. `Animated.X` resolves to X. */
|
|
141
151
|
const ELEMENTS = {
|
|
142
152
|
View: element('View', HOST_PROPS, [...LAYOUT_STYLE, ...VIEW_STYLE], 'nodes', {}, VIEW_ANIMATED),
|
|
@@ -147,7 +157,7 @@ const ELEMENTS = {
|
|
|
147
157
|
Pressable: element('Pressable', [...HOST_PROPS, 'disabled'], [...LAYOUT_STYLE, ...VIEW_STYLE], 'nodes', {}, VIEW_ANIMATED),
|
|
148
158
|
TouchableOpacity: element('TouchableOpacity', [...HOST_PROPS, 'disabled', 'activeOpacity'], [...LAYOUT_STYLE, ...VIEW_STYLE], 'nodes', {}, VIEW_ANIMATED),
|
|
149
159
|
Text: element('Text', [...HOST_PROPS, 'numberOfLines', 'ellipsizeMode'], [...LAYOUT_STYLE, ...TEXT_STYLE], 'text', {}, TEXT_ANIMATED),
|
|
150
|
-
Image: element('Image', [...HOST_PROPS, 'source', 'imageName', 'resizeMode', 'tintColor'], LAYOUT_STYLE, 'none'),
|
|
160
|
+
Image: element('Image', [...HOST_PROPS, 'source', 'imageName', 'resizeMode', 'tintColor'], [...LAYOUT_STYLE, ...IMAGE_STYLE], 'none', {}, OPACITY_ANIMATED),
|
|
151
161
|
Switch: element('Switch', [
|
|
152
162
|
'style',
|
|
153
163
|
'ref',
|
|
@@ -179,7 +189,7 @@ const ELEMENTS = {
|
|
|
179
189
|
'autoCapitalize',
|
|
180
190
|
'autoCorrect',
|
|
181
191
|
'returnKeyType',
|
|
182
|
-
].map(name => [name, NO_TEXT_INPUT_FIELD]))),
|
|
192
|
+
].map(name => [name, NO_TEXT_INPUT_FIELD])), OPACITY_ANIMATED),
|
|
183
193
|
ActivityIndicator: element('ActivityIndicator', ['style', 'ref', 'color', 'size', 'animating'], [...LAYOUT_STYLE, 'color'], 'none'),
|
|
184
194
|
Modal: element('Modal', [
|
|
185
195
|
'style',
|
|
@@ -229,10 +239,9 @@ const ELEMENTS = {
|
|
|
229
239
|
// The Flow A wrapper warns about these too, so a list that runs in the simulator also compiles.
|
|
230
240
|
platformNoOps: false,
|
|
231
241
|
},
|
|
232
|
-
Svg: element('Svg', ['style', 'ref', 'width', 'height', 'viewBox', 'source'], LAYOUT_STYLE, 'shapes', {
|
|
242
|
+
Svg: element('Svg', ['style', 'ref', 'width', 'height', 'viewBox', 'source', ...SVG_PAINT], LAYOUT_STYLE, 'shapes', {
|
|
233
243
|
visible: 'wrap it: <View visible={…}><Svg …/></View> — the View carries the hide and the whole subtree goes with it.',
|
|
234
244
|
...Object.fromEntries(Object.keys(EVENT_TYPES).map(name => [name, SVG_EVENT])),
|
|
235
|
-
...Object.fromEntries(SVG_PAINT.map(name => [name, SVG_ROOT_PAINT])),
|
|
236
245
|
}),
|
|
237
246
|
};
|
|
238
247
|
/** The shapes inside an <Svg>, by tag; they are drawing data, so they take no ref, style or events. */
|
|
@@ -442,8 +451,9 @@ export function checkAnimatedStyleKey(spec, key, node) {
|
|
|
442
451
|
checkStyleKey(spec, key, node);
|
|
443
452
|
if (spec.animatedStyleKeys.has(key))
|
|
444
453
|
return;
|
|
445
|
-
throw at(aotError(`AOT: an animated "${key}" has no effect on <${spec.tag}>`, 'the engine animates opacity
|
|
446
|
-
'a
|
|
454
|
+
throw at(aotError(`AOT: an animated "${key}" has no effect on <${spec.tag}>`, 'the engine animates opacity on a View, ScrollView, Pressable, Modal, Text, Image or TextInput, ' +
|
|
455
|
+
'backgroundColor on a View, ScrollView, Pressable or Modal, color on a Text, and transform anywhere. ' +
|
|
456
|
+
'Animate a wrapping <View>, or drive the value from state.'), node);
|
|
447
457
|
}
|
|
448
458
|
/**
|
|
449
459
|
* Refuses children on an element that holds none. Whitespace between tags and JSX comments are not children.
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { colorLiteral } from '../../style-map.mjs';
|
|
20
|
-
import { aotError } from '../diagnostics.mjs';
|
|
20
|
+
import { aotError, checkEngineString, constantText } from '../diagnostics.mjs';
|
|
21
21
|
import { evalStatic } from '../static-eval.mjs';
|
|
22
22
|
import { lowerExpr, lowerText } from '../expressions.mjs';
|
|
23
23
|
import { isFn } from '../collect.mjs';
|
|
@@ -83,6 +83,7 @@ export function emitTextInput(element, scope, out, env, state) {
|
|
|
83
83
|
}
|
|
84
84
|
else if (name === 'placeholder') {
|
|
85
85
|
placeholder = String(evalStatic(node, scope));
|
|
86
|
+
checkEngineString(placeholder, 'ER_PLACEHOLDER_MAX', 'a <TextInput> placeholder');
|
|
86
87
|
}
|
|
87
88
|
else if (name === 'placeholderTextColor') {
|
|
88
89
|
staticAssigns.push({
|
|
@@ -124,6 +125,10 @@ export function emitTextInput(element, scope, out, env, state) {
|
|
|
124
125
|
throw aotError(`AOT: <TextInput> prop "${name}" is not supported`);
|
|
125
126
|
}
|
|
126
127
|
}
|
|
128
|
+
const text = valueNode ? lowerText(valueNode, env, scope) : null;
|
|
129
|
+
if (text) {
|
|
130
|
+
checkEngineString(constantText(text), 'ER_TEXT_MAX', 'a <TextInput> value');
|
|
131
|
+
}
|
|
127
132
|
// value → the input's text buffer (er_node_set_props → er_text_input_set_text): static literal, or a
|
|
128
133
|
// state-driven value re-synced each app_update.
|
|
129
134
|
const node = {
|
|
@@ -132,7 +137,7 @@ export function emitTextInput(element, scope, out, env, state) {
|
|
|
132
137
|
constantProps: staticAssigns,
|
|
133
138
|
stateProps: dynAssigns,
|
|
134
139
|
placeholder,
|
|
135
|
-
text
|
|
140
|
+
text,
|
|
136
141
|
image: null,
|
|
137
142
|
spans: null,
|
|
138
143
|
children: [],
|