embedded-react 0.17.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 +27 -5
- package/aot/compiler/branch-merge.mjs +5 -9
- package/aot/compiler/c/animations.mjs +90 -16
- package/aot/compiler/c/nodes.mjs +20 -11
- package/aot/compiler/c/statements.mjs +59 -11
- package/aot/compiler/check.mjs +32 -3
- package/aot/compiler/collect.mjs +46 -2
- package/aot/compiler/control-flow.mjs +26 -14
- package/aot/compiler/diagnostics.mjs +33 -0
- package/aot/compiler/elements/dial.mjs +1 -3
- package/aot/compiler/elements/props.mjs +19 -9
- package/aot/compiler/elements/text-input.mjs +7 -2
- package/aot/compiler/expressions.mjs +29 -7
- package/aot/compiler/handlers.mjs +62 -24
- package/aot/compiler/keyboard.mjs +35 -21
- package/aot/compiler/lower.mjs +3 -3
- package/aot/compiler/nodes.mjs +16 -8
- package/aot/compiler/options.mjs +63 -12
- package/aot/compiler/static-eval.mjs +36 -4
- package/aot/compiler/style-text.mjs +35 -12
- 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,7 +16,7 @@
|
|
|
16
16
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
-
import { cScalarType } from '../c-syntax.mjs';
|
|
19
|
+
import { cScalarType, cstr } from '../c-syntax.mjs';
|
|
20
20
|
import { printAnimationStart } from './animations.mjs';
|
|
21
21
|
import { printUpdateVector } from './vectors.mjs';
|
|
22
22
|
import { printCondition, printExpr } from './expressions.mjs';
|
|
@@ -33,6 +33,20 @@ export const APP_UPDATE_CALL = ' app_update();';
|
|
|
33
33
|
const LIST_INDENT = INDENT;
|
|
34
34
|
/** The file-scope flags that record which state was written since app_update() last ran. */
|
|
35
35
|
export const CHANGED_FLAGS = 's_changed';
|
|
36
|
+
/** Reports a fixed limit the app ran past to stderr, once per place it can happen. */
|
|
37
|
+
export const APP_LIMIT_DEFINITION = `/* Reports a fixed limit the app ran past, once per place it can happen, so a timer appending to a full list
|
|
38
|
+
cannot flood the log. */
|
|
39
|
+
#define APP_LIMIT(msg) \\
|
|
40
|
+
do \\
|
|
41
|
+
{ \\
|
|
42
|
+
static bool reported_; \\
|
|
43
|
+
if (!reported_) \\
|
|
44
|
+
{ \\
|
|
45
|
+
reported_ = true; \\
|
|
46
|
+
fprintf(stderr, "AOT: %s\\n", msg); \\
|
|
47
|
+
} \\
|
|
48
|
+
} while (0)
|
|
49
|
+
`;
|
|
36
50
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
37
51
|
- Implementation
|
|
38
52
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -73,6 +87,38 @@ function printTimerAdd(timer) {
|
|
|
73
87
|
: printExpr(delay);
|
|
74
88
|
return `er_timer_add((int)(${delayMs}), ${timer.isRepeating ? 'true' : 'false'}, ${timer.fn})`;
|
|
75
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* A write into a fixed string buffer. One that can come out too long reports, once, when snprintf cuts it
|
|
92
|
+
* short; a constant was checked to fit at compile time.
|
|
93
|
+
*
|
|
94
|
+
* @param buffer The buffer, as a C lvalue.
|
|
95
|
+
* @param args snprintf's arguments after the buffer and its size.
|
|
96
|
+
* @param isConstant The value is a constant.
|
|
97
|
+
* @param report What APP_LIMIT reports when the value is cut short.
|
|
98
|
+
* @param indent The statement's indentation.
|
|
99
|
+
*
|
|
100
|
+
* @returns The C lines.
|
|
101
|
+
*/
|
|
102
|
+
export function printStringWrite(buffer, args, isConstant, report, indent) {
|
|
103
|
+
const write = `snprintf(${buffer}, sizeof(${buffer}), ${args})`;
|
|
104
|
+
if (isConstant) {
|
|
105
|
+
return [`${indent}${write};`];
|
|
106
|
+
}
|
|
107
|
+
return [
|
|
108
|
+
`${indent}if (${write} >= (int)sizeof(${buffer}))`,
|
|
109
|
+
`${indent}{`,
|
|
110
|
+
`${indent}${INDENT}APP_LIMIT(${cstr(report)});`,
|
|
111
|
+
`${indent}}`,
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The report for an app string buffer cut short.
|
|
116
|
+
*
|
|
117
|
+
* @param what What the buffer holds.
|
|
118
|
+
*
|
|
119
|
+
* @returns The report.
|
|
120
|
+
*/
|
|
121
|
+
const cutShort = (what) => `${what} cut short, raise ER_AOT_LIST_STR_CAP`;
|
|
76
122
|
/**
|
|
77
123
|
* A string state write. snprintf's source and destination may not overlap, so a value that reads the slot
|
|
78
124
|
* itself (`setLabel(label + '!')`) is built in a temporary first.
|
|
@@ -80,31 +126,32 @@ function printTimerAdd(timer) {
|
|
|
80
126
|
* @param stmt The write.
|
|
81
127
|
* @param indent The statement's indentation.
|
|
82
128
|
*
|
|
83
|
-
* @returns The C
|
|
129
|
+
* @returns The C lines.
|
|
84
130
|
*/
|
|
85
131
|
function printSetStateText(stmt, indent) {
|
|
86
132
|
const member = stmt.state.cMember;
|
|
87
133
|
const format = printText(stmt.text);
|
|
134
|
+
const report = cutShort(`state '${stmt.state.name}'`);
|
|
88
135
|
if (format.args.some(formatArg => readsMember(member).test(formatArg))) {
|
|
89
136
|
return [
|
|
90
137
|
`${indent}{`,
|
|
91
138
|
`${indent} char next[sizeof(${member})];`,
|
|
92
|
-
|
|
139
|
+
...printStringWrite('next', formatArgs(format), false, report, indent + INDENT),
|
|
93
140
|
`${indent} memcpy(${member}, next, strlen(next) + 1);`,
|
|
94
141
|
`${indent}}`,
|
|
95
|
-
]
|
|
142
|
+
];
|
|
96
143
|
}
|
|
97
|
-
return
|
|
144
|
+
return printStringWrite(member, formatArgs(format), !format.args.length, report, indent);
|
|
98
145
|
}
|
|
99
146
|
/**
|
|
100
|
-
* An appended list item: fill the next free slot's fields and count it,
|
|
147
|
+
* An appended list item: fill the next free slot's fields and count it, or report a full list.
|
|
101
148
|
*
|
|
102
149
|
* @param stmt The append.
|
|
103
150
|
*
|
|
104
151
|
* @returns The C lines.
|
|
105
152
|
*/
|
|
106
153
|
function printAppendItem(stmt) {
|
|
107
|
-
const { arrayName, countMember, cap } = stmt.list;
|
|
154
|
+
const { name, arrayName, countMember, cap } = stmt.list;
|
|
108
155
|
const lines = [
|
|
109
156
|
`${LIST_INDENT}if (${countMember} < ${cap})`,
|
|
110
157
|
`${LIST_INDENT}{`,
|
|
@@ -112,13 +159,14 @@ function printAppendItem(stmt) {
|
|
|
112
159
|
for (const itemValue of stmt.values) {
|
|
113
160
|
const slot = `${arrayName}[${countMember}].${itemValue.field.key}`;
|
|
114
161
|
if ('text' in itemValue) {
|
|
115
|
-
|
|
162
|
+
const format = printText(itemValue.text);
|
|
163
|
+
lines.push(...printStringWrite(slot, formatArgs(format), !format.args.length, cutShort(`field '${itemValue.field.key}' of list '${name}'`), LIST_INDENT + INDENT));
|
|
116
164
|
}
|
|
117
165
|
else {
|
|
118
166
|
lines.push(`${LIST_INDENT}${INDENT}${slot} = ${printExpr(itemValue.value)};`);
|
|
119
167
|
}
|
|
120
168
|
}
|
|
121
|
-
lines.push(`${LIST_INDENT}${INDENT}${countMember}++;`, `${LIST_INDENT}}`);
|
|
169
|
+
lines.push(`${LIST_INDENT}${INDENT}${countMember}++;`, `${LIST_INDENT}}`, `${LIST_INDENT}else`, `${LIST_INDENT}{`, `${LIST_INDENT}${INDENT}APP_LIMIT(${cstr(`list '${name}' full, raise ER_AOT_LIST_CAP`)});`, `${LIST_INDENT}}`);
|
|
122
170
|
return lines;
|
|
123
171
|
}
|
|
124
172
|
/**
|
|
@@ -140,7 +188,7 @@ function printStmt(stmt, indent) {
|
|
|
140
188
|
: [`${indent}char ${stmt.name}[${stmt.stringCap}];`];
|
|
141
189
|
return [
|
|
142
190
|
...declaration,
|
|
143
|
-
|
|
191
|
+
...printStringWrite(stmt.name, `"%s", ${printExpr(init)}`, init.kind === 'string', cutShort(`local '${stmt.name}'`), indent),
|
|
144
192
|
];
|
|
145
193
|
}
|
|
146
194
|
const declaredType = stmt.isHoisted
|
|
@@ -169,7 +217,7 @@ function printStmt(stmt, indent) {
|
|
|
169
217
|
];
|
|
170
218
|
case 'setStateText':
|
|
171
219
|
return [
|
|
172
|
-
printSetStateText(stmt, indent),
|
|
220
|
+
...printSetStateText(stmt, indent),
|
|
173
221
|
...printChangedFlag(stmt.state, indent),
|
|
174
222
|
];
|
|
175
223
|
case 'clearList':
|
package/aot/compiler/check.mjs
CHANGED
|
@@ -17,12 +17,14 @@
|
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { GRAD_MAX_STOPS } from '../../src/embedded-react/svg-ops.js';
|
|
20
|
-
import { aotError, currentLoc } from './diagnostics.mjs';
|
|
20
|
+
import { aotError, currentLoc, locateError } from './diagnostics.mjs';
|
|
21
21
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
22
22
|
- Constants
|
|
23
23
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
24
24
|
/** The most breakpoints an interpolation holds (the engine's ER_INTERPOLATE_MAX_POINTS). */
|
|
25
25
|
const MAX_INTERPOLATION_POINTS = 8;
|
|
26
|
+
/** The longest time an animation can wait or run for: INT_MAX, since the waits the app counts down take an int. */
|
|
27
|
+
const MAX_MILLISECONDS = 2 ** 31 - 1;
|
|
26
28
|
/** The most stops a <Dial indicatorGradient> holds (the engine's ER_GRADIENT_MAX_STOPS). */
|
|
27
29
|
export const DIAL_MAX_GRADIENT_STOPS = 4;
|
|
28
30
|
/** Where lowering records its checks while a compile runs, or null outside one, when a check runs at once. */
|
|
@@ -51,6 +53,29 @@ export function numberProblem(rawValue) {
|
|
|
51
53
|
}
|
|
52
54
|
return null;
|
|
53
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* What is wrong with an animation time, if anything: it is rounded to whole milliseconds, and C holds those
|
|
58
|
+
* from 0 to INT_MAX.
|
|
59
|
+
*
|
|
60
|
+
* @param field What the time is, for the error.
|
|
61
|
+
* @param rawValue The value; anything `Number()` converts.
|
|
62
|
+
*
|
|
63
|
+
* @returns The error to report, or null for a time C holds.
|
|
64
|
+
*/
|
|
65
|
+
export function millisecondsProblem(field, rawValue) {
|
|
66
|
+
const hint = `a time is a number of milliseconds from 0 to ${MAX_MILLISECONDS}.`;
|
|
67
|
+
const value = Math.round(Number(rawValue));
|
|
68
|
+
if (!Number.isFinite(value)) {
|
|
69
|
+
return aotError(`AOT: ${field} folded to ` +
|
|
70
|
+
`${rawValue === undefined ? 'undefined' : String(rawValue)}, which is not a time`, hint);
|
|
71
|
+
}
|
|
72
|
+
if (value < 0) {
|
|
73
|
+
return aotError(`AOT: ${field} is ${value} ms; a time cannot be negative`, hint);
|
|
74
|
+
}
|
|
75
|
+
return value > MAX_MILLISECONDS
|
|
76
|
+
? aotError(`AOT: ${field} is ${value} ms, past the int range`, hint)
|
|
77
|
+
: null;
|
|
78
|
+
}
|
|
54
79
|
/**
|
|
55
80
|
* What is wrong with a recorded subject, if anything.
|
|
56
81
|
*
|
|
@@ -62,6 +87,8 @@ export function checkProblem(subject) {
|
|
|
62
87
|
switch (subject.kind) {
|
|
63
88
|
case 'number':
|
|
64
89
|
return numberProblem(subject.value);
|
|
90
|
+
case 'milliseconds':
|
|
91
|
+
return millisecondsProblem(subject.field, subject.value);
|
|
65
92
|
case 'interpolationPoints':
|
|
66
93
|
return subject.count > MAX_INTERPOLATION_POINTS
|
|
67
94
|
? aotError(`AOT: .interpolate() supports up to ${MAX_INTERPOLATION_POINTS} breakpoints ` +
|
|
@@ -91,14 +118,16 @@ export function checkProblem(subject) {
|
|
|
91
118
|
* reported; otherwise, or inside a fallback attempt, it is checked at once.
|
|
92
119
|
*
|
|
93
120
|
* @param subject The subject.
|
|
121
|
+
* @param loc Where its error is reported, when that is more exact than the node being lowered.
|
|
94
122
|
*/
|
|
95
|
-
export function check(subject) {
|
|
123
|
+
export function check(subject, loc) {
|
|
96
124
|
if (recording && !attempts) {
|
|
97
|
-
recording.push({ ...subject, loc: currentLoc() });
|
|
125
|
+
recording.push({ ...subject, loc: loc ?? currentLoc() });
|
|
98
126
|
return;
|
|
99
127
|
}
|
|
100
128
|
const problem = checkProblem(subject);
|
|
101
129
|
if (problem) {
|
|
130
|
+
locateError(problem, loc);
|
|
102
131
|
throw problem;
|
|
103
132
|
}
|
|
104
133
|
}
|
package/aot/compiler/collect.mjs
CHANGED
|
@@ -23,7 +23,7 @@ import { resolve } from 'node:path';
|
|
|
23
23
|
import { aotError } from './diagnostics.mjs';
|
|
24
24
|
import { mergeBranchElements } from './branch-merge.mjs';
|
|
25
25
|
import { bootCheckpoint, foldBoot, isBoot, nameBootValues, screenBinding, } from './boot.mjs';
|
|
26
|
-
import { evalStaticOrThrow, evalStatic, withUndefined } from './static-eval.mjs';
|
|
26
|
+
import { evalStaticOrThrow, evalStatic, setBinding, withUndefined, } from './static-eval.mjs';
|
|
27
27
|
import { cConstant, cTypeOfValue } from './c-syntax.mjs';
|
|
28
28
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
29
29
|
- Constants
|
|
@@ -164,7 +164,7 @@ export function moduleScope(program, screen, seed = {}) {
|
|
|
164
164
|
continue;
|
|
165
165
|
}
|
|
166
166
|
try {
|
|
167
|
-
scope
|
|
167
|
+
setBinding(scope, decl.id.name, bindConst(decl.id.name, evalStatic(decl.init, withUndefined(scope), 'boot')));
|
|
168
168
|
}
|
|
169
169
|
catch {
|
|
170
170
|
/* not a static const — skip */
|
|
@@ -205,6 +205,34 @@ function inferItemStruct(items, name) {
|
|
|
205
205
|
});
|
|
206
206
|
return { fields };
|
|
207
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
|
+
}
|
|
208
236
|
/**
|
|
209
237
|
* Collects a component's useState declarations → state descriptors keyed by JS name (reads) and setter
|
|
210
238
|
* name (writes). `prefix` namespaces the C STORAGE so each inlined child instance gets its own slots: the
|
|
@@ -273,6 +301,12 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
273
301
|
}
|
|
274
302
|
throw error;
|
|
275
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);
|
|
276
310
|
record = {
|
|
277
311
|
name,
|
|
278
312
|
cField,
|
|
@@ -340,6 +374,9 @@ export function collectState(fnBody, scope, caps, prefix = '', wide = NO_WIDE) {
|
|
|
340
374
|
else if (cType === 'i64') {
|
|
341
375
|
cConstant(initial, cType); // BigInt refuses a value that is not a whole number
|
|
342
376
|
}
|
|
377
|
+
else if (cType === 'string') {
|
|
378
|
+
checkInitialFits(name, null, [[`state "${name}"`, String(initial)]], caps, initArg);
|
|
379
|
+
}
|
|
343
380
|
record = {
|
|
344
381
|
name,
|
|
345
382
|
cField,
|
|
@@ -760,6 +797,13 @@ export function collectEffects(fnBody) {
|
|
|
760
797
|
if (!isFn(call.arguments[0])) {
|
|
761
798
|
throw aotError('AOT: useEffect must take an inline function', 'write useEffect(() => { … }, []).');
|
|
762
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
|
+
}
|
|
763
807
|
effects.push({
|
|
764
808
|
fn: call.arguments[0],
|
|
765
809
|
deps: call.arguments[1],
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
import { aotError } from './diagnostics.mjs';
|
|
20
20
|
import { foldBoot, isBoot } from './boot.mjs';
|
|
21
21
|
import { mergeBranchElements } from './branch-merge.mjs';
|
|
22
|
-
import { evalStatic, withUndefined } from './static-eval.mjs';
|
|
22
|
+
import { evalStatic, setBinding, withUndefined } from './static-eval.mjs';
|
|
23
23
|
import { emitSourcedWide, lowerExpr } from './expressions.mjs';
|
|
24
24
|
import { isFn, declaredNames, collectState, componentReturnJSX, collectHelpers, collectCallbacks, collectMemos, collectEffects, usesState, bindConst, } from './collect.mjs';
|
|
25
25
|
import { collectAnims, collectRefs } from './animations.mjs';
|
|
@@ -115,10 +115,11 @@ function extractProps(openingElement, scope, env) {
|
|
|
115
115
|
*
|
|
116
116
|
* @param fn The component.
|
|
117
117
|
* @param props The props the instance was given.
|
|
118
|
+
* @param scope Module constants a default can refer to.
|
|
118
119
|
*
|
|
119
120
|
* @returns The descriptor each name in the component's body is bound to.
|
|
120
121
|
*/
|
|
121
|
-
function bindParams(fn, props) {
|
|
122
|
+
function bindParams(fn, props, scope) {
|
|
122
123
|
const bindings = new Map();
|
|
123
124
|
const param = fn.params[0];
|
|
124
125
|
if (!param)
|
|
@@ -136,11 +137,11 @@ function bindParams(fn, props) {
|
|
|
136
137
|
}
|
|
137
138
|
else if (param.type === 'ObjectPattern') {
|
|
138
139
|
// `function C({x, y: renamed, z = 1})`: bind each name to its prop, else to its folded default.
|
|
139
|
-
|
|
140
|
+
const properties = param.properties.map(property => {
|
|
140
141
|
if (property.type === 'RestElement') {
|
|
141
142
|
throw new Error('AOT: rest props (...rest) in a component param not supported');
|
|
142
143
|
}
|
|
143
|
-
// Resolve a destructured prop's source name
|
|
144
|
+
// Resolve a destructured prop's source name and local binding name.
|
|
144
145
|
const propName = property.key.name ??
|
|
145
146
|
property.key.value;
|
|
146
147
|
const bindName = property.value?.type === 'Identifier'
|
|
@@ -148,14 +149,24 @@ function bindParams(fn, props) {
|
|
|
148
149
|
: property.value?.type === 'AssignmentPattern'
|
|
149
150
|
? property.value.left.name
|
|
150
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) {
|
|
151
159
|
let descriptor = props[propName];
|
|
152
160
|
if (!descriptor && property.value?.type === 'AssignmentPattern') {
|
|
153
161
|
descriptor = {
|
|
154
162
|
static: true,
|
|
155
|
-
value: evalStatic(property.value.right,
|
|
163
|
+
value: bindConst(bindName, evalStatic(property.value.right, defaultScope, 'boot')),
|
|
156
164
|
};
|
|
157
165
|
}
|
|
158
|
-
|
|
166
|
+
descriptor ??= { static: true, value: undefined };
|
|
167
|
+
if (descriptor.static)
|
|
168
|
+
setBinding(defaultScope, bindName, descriptor.value);
|
|
169
|
+
bindings.set(bindName, descriptor);
|
|
159
170
|
}
|
|
160
171
|
}
|
|
161
172
|
else {
|
|
@@ -222,12 +233,13 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
222
233
|
childrenRef = { kind: 'props', name: param.name };
|
|
223
234
|
}
|
|
224
235
|
// A child sees only module scope: start from module consts with no locals, so caller bindings cannot leak in.
|
|
225
|
-
const
|
|
236
|
+
const moduleScope = env.moduleConsts ?? scope;
|
|
237
|
+
const childScope = { ...moduleScope };
|
|
226
238
|
const childLocals = new Map();
|
|
227
239
|
// Callback props resolve to the caller's function, env and state; inherit the caller's own so they forward.
|
|
228
240
|
const fnProps = new Map(env.fnProps);
|
|
229
241
|
// Bind each prop: a callback to fnProps, a constant to the child's scope, a runtime value to its locals.
|
|
230
|
-
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)) {
|
|
231
243
|
// The children binding comes from the slot, not from a value prop.
|
|
232
244
|
if (childrenRef?.kind === 'local' && name === childrenRef?.name)
|
|
233
245
|
continue;
|
|
@@ -239,7 +251,7 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
239
251
|
});
|
|
240
252
|
}
|
|
241
253
|
else if (descriptor.static) {
|
|
242
|
-
childScope
|
|
254
|
+
setBinding(childScope, name, descriptor.value);
|
|
243
255
|
}
|
|
244
256
|
else {
|
|
245
257
|
childLocals.set(name, {
|
|
@@ -280,7 +292,7 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
280
292
|
if (childLocals.has(decl.id.name))
|
|
281
293
|
continue; // a dynamic prop of that name is already bound
|
|
282
294
|
try {
|
|
283
|
-
childScope
|
|
295
|
+
setBinding(childScope, decl.id.name, bindConst(decl.id.name, evalStatic(decl.init, withUndefined(childScope), 'boot')));
|
|
284
296
|
}
|
|
285
297
|
catch {
|
|
286
298
|
// A dynamic const hides any same-named module const; a memo re-binds it below, else it stays unresolved.
|
|
@@ -327,7 +339,7 @@ export function emitComponent(element, scope, out, env, state, opts) {
|
|
|
327
339
|
// Resolve memos in order (fold, else a local C expr), then compile its effects, all in the child's env/state.
|
|
328
340
|
for (const [name, expr] of childMemos) {
|
|
329
341
|
try {
|
|
330
|
-
childScope
|
|
342
|
+
setBinding(childScope, name, bindConst(name, evalStatic(expr, childScope, 'boot')));
|
|
331
343
|
}
|
|
332
344
|
catch {
|
|
333
345
|
const cExpr = emitSourcedWide(expr, childEnv);
|
|
@@ -401,10 +413,10 @@ function emitMap(call, scope, out, env, state) {
|
|
|
401
413
|
return items.map((item, index) => {
|
|
402
414
|
const rowScope = { ...scope };
|
|
403
415
|
if (itemName) {
|
|
404
|
-
rowScope
|
|
416
|
+
setBinding(rowScope, itemName, item);
|
|
405
417
|
}
|
|
406
418
|
if (indexName) {
|
|
407
|
-
rowScope
|
|
419
|
+
setBinding(rowScope, indexName, index);
|
|
408
420
|
}
|
|
409
421
|
return emitElementInto(rowJSX, rowScope, out, rowEnv(env, [itemName, indexName], rowScope), state);
|
|
410
422
|
});
|
|
@@ -484,7 +496,7 @@ function emitDynamicMap(call, listState, scope, out, env, state) {
|
|
|
484
496
|
delete rowScope[itemName];
|
|
485
497
|
}
|
|
486
498
|
if (indexName) {
|
|
487
|
-
rowScope
|
|
499
|
+
setBinding(rowScope, indexName, rowIndex); // the index is a compile-time literal per pooled row
|
|
488
500
|
}
|
|
489
501
|
const rowLocals = new Map(env.locals);
|
|
490
502
|
if (indexName) {
|
|
@@ -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
|
*
|
|
@@ -306,9 +306,7 @@ export function emitDial(element, scope, out, env, state) {
|
|
|
306
306
|
stateProps: dynAssigns,
|
|
307
307
|
placeholder: null,
|
|
308
308
|
text: null,
|
|
309
|
-
image: knobImage != null
|
|
310
|
-
? { kind: 'static', name: knobImage, resetOnUpdate: true }
|
|
311
|
-
: null,
|
|
309
|
+
image: knobImage != null ? { kind: 'static', name: knobImage } : null,
|
|
312
310
|
spans: null,
|
|
313
311
|
children: [],
|
|
314
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: [],
|