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/options.mjs
CHANGED
|
@@ -13,9 +13,41 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
|
+
- Constants
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
/**
|
|
20
|
+
* The largest value each size or cap takes; the smallest is 1. Screen dimensions end up in int16_t props, a
|
|
21
|
+
* list's row count and a string buffer's size in a C `int`, and a nested <Text>'s segment count in the
|
|
22
|
+
* engine's uint8_t `span_count`.
|
|
23
|
+
*/
|
|
24
|
+
const MAX_SCREEN = 32767;
|
|
25
|
+
const MAX_LIST_CAP = 32767;
|
|
26
|
+
const MAX_LIST_STR_CAP = 32767;
|
|
27
|
+
const MAX_TEXT_SPANS = 255;
|
|
16
28
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
29
|
- Implementation
|
|
18
30
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
31
|
+
/**
|
|
32
|
+
* Checks that a size or cap is a whole number from 1 to `max`, since it is written into the generated C as a
|
|
33
|
+
* `#define` or an array size.
|
|
34
|
+
*
|
|
35
|
+
* @param value The value, as the option or the parsed variable gave it.
|
|
36
|
+
* @param name The option or variable, as the error names it.
|
|
37
|
+
* @param max The largest value allowed.
|
|
38
|
+
* @param shown The value as the error shows it. Defaults to `value`.
|
|
39
|
+
*
|
|
40
|
+
* @returns The value, when it is in range.
|
|
41
|
+
*/
|
|
42
|
+
function wholeInRange(value, name, max, shown = String(value)) {
|
|
43
|
+
if (typeof value !== 'number' ||
|
|
44
|
+
!Number.isInteger(value) ||
|
|
45
|
+
value < 1 ||
|
|
46
|
+
value > max) {
|
|
47
|
+
throw new Error(`AOT: ${name} must be a whole number from 1 to ${max}, got ${shown}`);
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
19
51
|
/**
|
|
20
52
|
* compileSource's options with the target screen and the fixed-buffer caps filled in. Each one the caller
|
|
21
53
|
* leaves out falls back to its environment variable, then to a default. The environment is read on every
|
|
@@ -29,27 +61,46 @@
|
|
|
29
61
|
* maxTextSpans ER_AOT_MAX_TEXT_SPANS 4 Inline segments in a nested <Text>. Must match the
|
|
30
62
|
* engine's ER_TEXT_MAX_SPANS.
|
|
31
63
|
*
|
|
64
|
+
* Each value must be a whole number from 1 up: to 32767 for the screen and the list caps, to 255 for
|
|
65
|
+
* maxTextSpans. One that isn't, from the options or a variable, is an `AOT:` error that names it.
|
|
66
|
+
*
|
|
32
67
|
* @param opts The options the caller passed.
|
|
33
68
|
*
|
|
34
69
|
* @returns The same options with every value filled in.
|
|
35
70
|
*/
|
|
36
71
|
export function resolveOptions(opts) {
|
|
37
|
-
//
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
|
|
72
|
+
// An unset or blank variable falls back to the default; any other value must parse to one in range.
|
|
73
|
+
const isSet = (name) => (process.env[name] ?? '').trim() !== '';
|
|
74
|
+
const fromEnv = (name, fallback, max) => {
|
|
75
|
+
if (!isSet(name))
|
|
76
|
+
return fallback;
|
|
77
|
+
const raw = process.env[name];
|
|
78
|
+
return wholeInRange(Number(raw), name, max, JSON.stringify(raw));
|
|
79
|
+
};
|
|
80
|
+
const fromOption = (value, name, envName, fallback, max) => value != null
|
|
81
|
+
? wholeInRange(value, name, max)
|
|
82
|
+
: fromEnv(envName, fallback, max);
|
|
41
83
|
return {
|
|
42
84
|
...opts,
|
|
43
|
-
screen: opts.screen
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
85
|
+
screen: opts.screen
|
|
86
|
+
? {
|
|
87
|
+
width: wholeInRange(opts.screen.width, 'screen.width', MAX_SCREEN),
|
|
88
|
+
height: wholeInRange(opts.screen.height, 'screen.height', MAX_SCREEN),
|
|
89
|
+
}
|
|
90
|
+
: {
|
|
91
|
+
width: fromEnv('ER_AOT_SCREEN_W', 800, MAX_SCREEN),
|
|
92
|
+
height: fromEnv('ER_AOT_SCREEN_H', 480, MAX_SCREEN),
|
|
93
|
+
},
|
|
47
94
|
// Where the size came from, for the error footer: 'option', 'env' (both dimensions) or 'default'.
|
|
48
|
-
screenFrom: opts.screen
|
|
95
|
+
screenFrom: opts.screen
|
|
96
|
+
? 'option'
|
|
97
|
+
: isSet('ER_AOT_SCREEN_W') && isSet('ER_AOT_SCREEN_H')
|
|
98
|
+
? 'env'
|
|
99
|
+
: 'default',
|
|
49
100
|
caps: {
|
|
50
|
-
listCap: opts.listCap
|
|
51
|
-
listStrCap: opts.listStrCap
|
|
52
|
-
maxTextSpans: opts.maxTextSpans
|
|
101
|
+
listCap: fromOption(opts.listCap, 'listCap', 'ER_AOT_LIST_CAP', 16, MAX_LIST_CAP),
|
|
102
|
+
listStrCap: fromOption(opts.listStrCap, 'listStrCap', 'ER_AOT_LIST_STR_CAP', 48, MAX_LIST_STR_CAP),
|
|
103
|
+
maxTextSpans: fromOption(opts.maxTextSpans, 'maxTextSpans', 'ER_AOT_MAX_TEXT_SPANS', 4, MAX_TEXT_SPANS),
|
|
53
104
|
},
|
|
54
105
|
};
|
|
55
106
|
}
|
|
@@ -16,7 +16,21 @@
|
|
|
16
16
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
17
|
- Imports
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { BootReadError, BootValue, buildValueOf, containsBoot, foldBoot, isBoot, isContainer, } from './boot.mjs';
|
|
19
20
|
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
22
|
+
- Constants
|
|
23
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
24
|
+
/** The Math functions a fold works out, as JS does. */
|
|
25
|
+
const STATIC_MATH = {
|
|
26
|
+
min: Math.min,
|
|
27
|
+
max: Math.max,
|
|
28
|
+
floor: Math.floor,
|
|
29
|
+
ceil: Math.ceil,
|
|
30
|
+
round: Math.round,
|
|
31
|
+
trunc: Math.trunc,
|
|
32
|
+
abs: Math.abs,
|
|
33
|
+
};
|
|
20
34
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
21
35
|
- Implementation
|
|
22
36
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -49,14 +63,153 @@ export function evalStaticOrThrow(node, scope, message, hint) {
|
|
|
49
63
|
* Styles and state initial values fold through here. A caller that can also handle a runtime value catches
|
|
50
64
|
* the error thrown on anything dynamic (a state reference, say) and emits C instead.
|
|
51
65
|
*
|
|
66
|
+
* A value derived from the panel size (see BootValue) folds by `mode`: `fold` takes what it is on the build's
|
|
67
|
+
* panel and records that as an assumption the panel at boot must meet; `boot` keeps it as a boot value, which
|
|
68
|
+
* boot works out; `strict` throws a BootReadError, so the caller lowers it as a runtime value instead.
|
|
69
|
+
*
|
|
52
70
|
* @param node The expression.
|
|
53
71
|
* @param scope Compile-time constants in reach.
|
|
72
|
+
* @param mode How a value derived from the panel size folds.
|
|
54
73
|
*
|
|
55
74
|
* @returns The value: a number, string, boolean, null, or an object or array of those.
|
|
56
75
|
*
|
|
57
76
|
* @throws An error when any part of the expression is not a compile-time constant.
|
|
58
77
|
*/
|
|
59
|
-
export function evalStatic(node, scope) {
|
|
78
|
+
export function evalStatic(node, scope, mode = 'fold') {
|
|
79
|
+
const value = evaluate(node, scope, { scope, snapshot: null });
|
|
80
|
+
if (mode === 'boot')
|
|
81
|
+
return value;
|
|
82
|
+
if (mode === 'fold')
|
|
83
|
+
return foldBoot(value);
|
|
84
|
+
if (containsBoot(value)) {
|
|
85
|
+
throw new BootReadError();
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The names a boot value's expression reads, copied once per fold: the scope a const was folded in can lose
|
|
91
|
+
* or rebind names afterward.
|
|
92
|
+
*
|
|
93
|
+
* @param fold The fold.
|
|
94
|
+
*
|
|
95
|
+
* @returns The copy.
|
|
96
|
+
*/
|
|
97
|
+
function snapshotOf(fold) {
|
|
98
|
+
if (!fold.snapshot) {
|
|
99
|
+
const copy = {};
|
|
100
|
+
for (const name in fold.scope) {
|
|
101
|
+
setBinding(copy, name, fold.scope[name]);
|
|
102
|
+
}
|
|
103
|
+
fold.snapshot = copy;
|
|
104
|
+
}
|
|
105
|
+
return fold.snapshot;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* A boot value for an expression the fold could only take so far because it reads one.
|
|
109
|
+
*
|
|
110
|
+
* @param node The expression.
|
|
111
|
+
* @param value What it is on the build's panel.
|
|
112
|
+
* @param fold The fold.
|
|
113
|
+
* @param lowerable Boot can work it out from the expression.
|
|
114
|
+
* @param basis What a fold of it assumes when boot cannot work it out.
|
|
115
|
+
*
|
|
116
|
+
* @returns The boot value.
|
|
117
|
+
*/
|
|
118
|
+
const bootOf = (node, value, fold, lowerable = true, basis = []) => new BootValue(node, snapshotOf(fold), buildValueOf(value), lowerable, null, null, basis);
|
|
119
|
+
/**
|
|
120
|
+
* `test ? consequent : alternate` with a test that depends on the panel. Two objects with the same keys merge
|
|
121
|
+
* into one whose differing entries each pick at boot, so a branch that changes only values stays one value;
|
|
122
|
+
* anything else is one boot value.
|
|
123
|
+
*
|
|
124
|
+
* @param node The expression.
|
|
125
|
+
* @param test The test, as a boot value.
|
|
126
|
+
* @param fold The fold.
|
|
127
|
+
*
|
|
128
|
+
* @returns The value.
|
|
129
|
+
*/
|
|
130
|
+
function bootConditional(node, test, fold) {
|
|
131
|
+
const taken = buildValueOf(test) ? node.consequent : node.alternate;
|
|
132
|
+
const takenValue = evaluate(taken, fold.scope, fold);
|
|
133
|
+
let otherValue;
|
|
134
|
+
try {
|
|
135
|
+
otherValue = evaluate(taken === node.consequent ? node.alternate : node.consequent, fold.scope, fold);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// The branch the build's panel does not take does not fold, so boot cannot pick between them.
|
|
139
|
+
return bootOf(node, takenValue, fold, false, [test]);
|
|
140
|
+
}
|
|
141
|
+
const [consequent, alternate] = taken === node.consequent
|
|
142
|
+
? [takenValue, otherValue]
|
|
143
|
+
: [otherValue, takenValue];
|
|
144
|
+
return mergeBranches(node, node.consequent, node.alternate, [
|
|
145
|
+
consequent,
|
|
146
|
+
alternate,
|
|
147
|
+
takenValue,
|
|
148
|
+
]);
|
|
149
|
+
/**
|
|
150
|
+
* Merges two branch values at `at`, entry by entry where both are objects. An entry only one branch has is
|
|
151
|
+
* taken at the build's panel, which assumes the test.
|
|
152
|
+
*
|
|
153
|
+
* @param at The expression the merged value stands for.
|
|
154
|
+
* @param consequentNode The consequent branch's expression.
|
|
155
|
+
* @param alternateNode The alternate branch's expression.
|
|
156
|
+
* @param values The consequent's value, the alternate's, and the one the build's panel takes.
|
|
157
|
+
*
|
|
158
|
+
* @returns The merged value.
|
|
159
|
+
*/
|
|
160
|
+
function mergeBranches(at, consequentNode, alternateNode, [whenTrue, whenFalse, built]) {
|
|
161
|
+
if (whenTrue === whenFalse)
|
|
162
|
+
return whenTrue;
|
|
163
|
+
const mergeable = isContainer(whenTrue) &&
|
|
164
|
+
isContainer(whenFalse) &&
|
|
165
|
+
!Array.isArray(whenTrue) &&
|
|
166
|
+
!Array.isArray(whenFalse);
|
|
167
|
+
if (!mergeable) {
|
|
168
|
+
return bootOf(at, built, fold, !isContainer(buildValueOf(built)), [test]);
|
|
169
|
+
}
|
|
170
|
+
// Each entry is `test ? (consequent).key : (alternate).key`, which boot works out on its own.
|
|
171
|
+
const merged = {};
|
|
172
|
+
const keys = new Set([...Object.keys(whenTrue), ...Object.keys(whenFalse)]);
|
|
173
|
+
for (const key of keys) {
|
|
174
|
+
if (!Object.hasOwn(whenTrue, key) || !Object.hasOwn(whenFalse, key)) {
|
|
175
|
+
const builtEntry = built[key];
|
|
176
|
+
merged[key] = bootOf(at, builtEntry, fold, false, [test]);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const entryOf = (object) => ({
|
|
180
|
+
type: 'MemberExpression',
|
|
181
|
+
object,
|
|
182
|
+
property: { type: 'StringLiteral', value: key },
|
|
183
|
+
computed: true,
|
|
184
|
+
loc: object.loc,
|
|
185
|
+
});
|
|
186
|
+
const consequentEntry = entryOf(consequentNode);
|
|
187
|
+
const alternateEntry = entryOf(alternateNode);
|
|
188
|
+
merged[key] = mergeBranches({
|
|
189
|
+
type: 'ConditionalExpression',
|
|
190
|
+
test: node.test,
|
|
191
|
+
consequent: consequentEntry,
|
|
192
|
+
alternate: alternateEntry,
|
|
193
|
+
loc: at.loc,
|
|
194
|
+
}, consequentEntry, alternateEntry, [
|
|
195
|
+
whenTrue[key],
|
|
196
|
+
whenFalse[key],
|
|
197
|
+
built[key],
|
|
198
|
+
]);
|
|
199
|
+
}
|
|
200
|
+
return merged;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* evalStatic's fold, which keeps a value derived from the panel size as a boot value.
|
|
205
|
+
*
|
|
206
|
+
* @param node The expression.
|
|
207
|
+
* @param scope Compile-time constants in reach.
|
|
208
|
+
* @param fold The fold this expression is part of.
|
|
209
|
+
*
|
|
210
|
+
* @returns The value.
|
|
211
|
+
*/
|
|
212
|
+
function evaluate(node, scope, fold) {
|
|
60
213
|
switch (node.type) {
|
|
61
214
|
case 'NumericLiteral':
|
|
62
215
|
case 'StringLiteral':
|
|
@@ -65,69 +218,83 @@ export function evalStatic(node, scope) {
|
|
|
65
218
|
case 'NullLiteral':
|
|
66
219
|
return null;
|
|
67
220
|
case 'UnaryExpression': {
|
|
68
|
-
const operand =
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
221
|
+
const operand = evaluate(node.argument, scope, fold);
|
|
222
|
+
const apply = (value) => {
|
|
223
|
+
if (node.operator === '-')
|
|
224
|
+
return -value;
|
|
225
|
+
if (node.operator === '+')
|
|
226
|
+
return +value;
|
|
227
|
+
if (node.operator === '!')
|
|
228
|
+
return !value;
|
|
229
|
+
throw new Error('AOT: unsupported expression "UnaryExpression" in static context');
|
|
230
|
+
};
|
|
231
|
+
if (isBoot(operand)) {
|
|
232
|
+
const result = bootOf(node, apply(buildValueOf(operand)), fold);
|
|
233
|
+
// `!flag` holds exactly when `flag` does not, so a fold of it assumes the flag itself.
|
|
234
|
+
if (node.operator === '!' &&
|
|
235
|
+
typeof buildValueOf(operand) === 'boolean') {
|
|
236
|
+
result.equivalent = operand.equivalent ?? operand;
|
|
237
|
+
}
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
return apply(operand);
|
|
76
241
|
}
|
|
77
242
|
case 'BinaryExpression': {
|
|
78
|
-
const left =
|
|
79
|
-
const right =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return left - right;
|
|
85
|
-
case '*':
|
|
86
|
-
return left * right;
|
|
87
|
-
case '/':
|
|
88
|
-
return left / right;
|
|
89
|
-
case '%':
|
|
90
|
-
return left % right;
|
|
91
|
-
case '<':
|
|
92
|
-
return left < right;
|
|
93
|
-
case '>':
|
|
94
|
-
return left > right;
|
|
95
|
-
case '<=':
|
|
96
|
-
return left <= right;
|
|
97
|
-
case '>=':
|
|
98
|
-
return left >= right;
|
|
99
|
-
case '==':
|
|
100
|
-
case '===':
|
|
101
|
-
return left === right;
|
|
102
|
-
case '!=':
|
|
103
|
-
case '!==':
|
|
104
|
-
return left !== right;
|
|
105
|
-
}
|
|
106
|
-
break;
|
|
243
|
+
const left = evaluate(node.left, scope, fold);
|
|
244
|
+
const right = evaluate(node.right, scope, fold);
|
|
245
|
+
const result = binary(node.operator, buildValueOf(left), buildValueOf(right));
|
|
246
|
+
return isBoot(left) || isBoot(right)
|
|
247
|
+
? bootOf(node, result, fold)
|
|
248
|
+
: result;
|
|
107
249
|
}
|
|
108
250
|
case 'LogicalExpression': {
|
|
109
|
-
const left =
|
|
110
|
-
if (node.operator
|
|
111
|
-
|
|
251
|
+
const left = evaluate(node.left, scope, fold);
|
|
252
|
+
if (node.operator !== '&&' && node.operator !== '||')
|
|
253
|
+
break;
|
|
254
|
+
if (!isBoot(left)) {
|
|
255
|
+
const takesRight = node.operator === '&&' ? left : !left;
|
|
256
|
+
return takesRight ? evaluate(node.right, scope, fold) : left;
|
|
112
257
|
}
|
|
113
|
-
|
|
114
|
-
|
|
258
|
+
// A test that depends on the panel: boot picks a side, so both have to fold.
|
|
259
|
+
const takesRight = node.operator === '&&' ? buildValueOf(left) : !buildValueOf(left);
|
|
260
|
+
try {
|
|
261
|
+
const right = evaluate(node.right, scope, fold);
|
|
262
|
+
return bootOf(node, takesRight ? right : left.value, fold);
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
if (takesRight)
|
|
266
|
+
throw new Error('AOT: the right side does not fold');
|
|
267
|
+
return bootOf(node, left.value, fold, false, [left]);
|
|
115
268
|
}
|
|
116
|
-
break;
|
|
117
269
|
}
|
|
118
|
-
case 'ConditionalExpression':
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
270
|
+
case 'ConditionalExpression': {
|
|
271
|
+
const test = evaluate(node.test, scope, fold);
|
|
272
|
+
if (isBoot(test))
|
|
273
|
+
return bootConditional(node, test, fold);
|
|
274
|
+
return test
|
|
275
|
+
? evaluate(node.consequent, scope, fold)
|
|
276
|
+
: evaluate(node.alternate, scope, fold);
|
|
277
|
+
}
|
|
122
278
|
case 'Identifier':
|
|
123
|
-
if (node.name
|
|
279
|
+
if (hasBinding(scope, node.name))
|
|
124
280
|
return scope[node.name];
|
|
125
281
|
throw new Error(`AOT: cannot statically resolve identifier "${node.name}"`);
|
|
126
282
|
case 'MemberExpression': {
|
|
127
|
-
const object =
|
|
283
|
+
const object = evaluate(node.object, scope, fold);
|
|
128
284
|
const key = node.computed
|
|
129
|
-
?
|
|
285
|
+
? evaluate(node.property, scope, fold)
|
|
130
286
|
: node.property.name;
|
|
287
|
+
if (isBoot(key)) {
|
|
288
|
+
throw new Error('AOT: a member key that depends on the panel size');
|
|
289
|
+
}
|
|
290
|
+
// A member of a boot value is one too, which only a fold can take.
|
|
291
|
+
if (isBoot(object)) {
|
|
292
|
+
const objectValue = buildValueOf(object);
|
|
293
|
+
if (objectValue == null) {
|
|
294
|
+
throw new Error(`AOT: member access on null/undefined ("${key}")`);
|
|
295
|
+
}
|
|
296
|
+
return bootOf(node, objectValue[key], fold, false, [object]);
|
|
297
|
+
}
|
|
131
298
|
if (object == null) {
|
|
132
299
|
throw new Error(`AOT: member access on null/undefined ("${key}")`);
|
|
133
300
|
}
|
|
@@ -140,28 +307,89 @@ export function evalStatic(node, scope) {
|
|
|
140
307
|
throw new Error('AOT: object spreads/methods not supported in static objects');
|
|
141
308
|
}
|
|
142
309
|
const key = prop.computed
|
|
143
|
-
?
|
|
310
|
+
? evaluate(prop.key, scope, fold)
|
|
144
311
|
: (prop.key.name ??
|
|
145
312
|
prop.key.value);
|
|
146
|
-
|
|
313
|
+
if (isBoot(key)) {
|
|
314
|
+
throw new Error('AOT: an object key that depends on the panel size');
|
|
315
|
+
}
|
|
316
|
+
folded[key] = evaluate(prop.value, scope, fold);
|
|
147
317
|
}
|
|
148
318
|
return folded;
|
|
149
319
|
}
|
|
150
320
|
case 'ArrayExpression':
|
|
151
|
-
return node.elements.map(element => element ?
|
|
321
|
+
return node.elements.map(element => element ? evaluate(element, scope, fold) : null);
|
|
152
322
|
case 'CallExpression': {
|
|
153
323
|
// `StyleSheet.create({…})` folds to its argument; any other call is dynamic.
|
|
154
324
|
const callee = node.callee;
|
|
155
325
|
if (callee.type === 'MemberExpression' &&
|
|
156
326
|
callee.object.name === 'StyleSheet' &&
|
|
157
327
|
callee.property.name === 'create') {
|
|
158
|
-
return
|
|
328
|
+
return evaluate(node.arguments[0], scope, fold);
|
|
329
|
+
}
|
|
330
|
+
// Math.min / max / floor / ceil / round / trunc / abs fold as JS works them out; others are dynamic.
|
|
331
|
+
const mathFn = callee.type === 'MemberExpression' &&
|
|
332
|
+
!callee.computed &&
|
|
333
|
+
callee.object.type === 'Identifier' &&
|
|
334
|
+
callee.object.name === 'Math' &&
|
|
335
|
+
!hasBinding(scope, 'Math')
|
|
336
|
+
? callee.property.name
|
|
337
|
+
: null;
|
|
338
|
+
if (mathFn && Object.hasOwn(STATIC_MATH, mathFn)) {
|
|
339
|
+
const args = node.arguments.map(arg => {
|
|
340
|
+
if (arg.type === 'SpreadElement') {
|
|
341
|
+
throw new Error('AOT: cannot statically evaluate a spread');
|
|
342
|
+
}
|
|
343
|
+
return evaluate(arg, scope, fold);
|
|
344
|
+
});
|
|
345
|
+
const result = STATIC_MATH[mathFn](...args.map(buildValueOf));
|
|
346
|
+
return args.some(isBoot) ? bootOf(node, result, fold) : result;
|
|
159
347
|
}
|
|
160
348
|
throw new Error(`AOT: cannot statically evaluate call expression`);
|
|
161
349
|
}
|
|
162
350
|
}
|
|
163
351
|
throw new Error(`AOT: unsupported expression "${node.type}" in static context`);
|
|
164
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* A binary operator applied as JS applies it.
|
|
355
|
+
*
|
|
356
|
+
* @param operator The operator.
|
|
357
|
+
* @param left The left operand.
|
|
358
|
+
* @param right The right operand.
|
|
359
|
+
*
|
|
360
|
+
* @returns The result.
|
|
361
|
+
*/
|
|
362
|
+
function binary(operator, left, right) {
|
|
363
|
+
switch (operator) {
|
|
364
|
+
case '+':
|
|
365
|
+
return left + right;
|
|
366
|
+
case '-':
|
|
367
|
+
return left - right;
|
|
368
|
+
case '*':
|
|
369
|
+
return left * right;
|
|
370
|
+
case '/':
|
|
371
|
+
return left / right;
|
|
372
|
+
case '%':
|
|
373
|
+
return left % right;
|
|
374
|
+
case '<':
|
|
375
|
+
return left < right;
|
|
376
|
+
case '>':
|
|
377
|
+
return left > right;
|
|
378
|
+
case '<=':
|
|
379
|
+
return left <= right;
|
|
380
|
+
case '>=':
|
|
381
|
+
return left >= right;
|
|
382
|
+
case '==':
|
|
383
|
+
return left == right;
|
|
384
|
+
case '===':
|
|
385
|
+
return left === right;
|
|
386
|
+
case '!=':
|
|
387
|
+
return left != right;
|
|
388
|
+
case '!==':
|
|
389
|
+
return left !== right;
|
|
390
|
+
}
|
|
391
|
+
throw new Error('AOT: unsupported expression "BinaryExpression" in static context');
|
|
392
|
+
}
|
|
165
393
|
/**
|
|
166
394
|
* `scope` minus every name `env` binds at RUNTIME — locals, state, refs, animated values, and the event /
|
|
167
395
|
* gesture params of the handler being compiled. emitExpr resolves those before env.consts, so a fold
|
|
@@ -190,7 +418,7 @@ export function foldScope(env, scope) {
|
|
|
190
418
|
// Copy the scope only when a runtime name actually shadows one of its constants.
|
|
191
419
|
let hasShadowedName = false;
|
|
192
420
|
for (const name of runtimeNames) {
|
|
193
|
-
if (name
|
|
421
|
+
if (hasBinding(scope, name)) {
|
|
194
422
|
hasShadowedName = true;
|
|
195
423
|
break;
|
|
196
424
|
}
|
|
@@ -199,6 +427,36 @@ export function foldScope(env, scope) {
|
|
|
199
427
|
return scope;
|
|
200
428
|
return Object.fromEntries(Object.entries(scope).filter(([name]) => !runtimeNames.has(name)));
|
|
201
429
|
}
|
|
430
|
+
/**
|
|
431
|
+
* Whether `scope` binds `name`, through the scopes it reads through to but not Object.prototype.
|
|
432
|
+
*
|
|
433
|
+
* @param scope Compile-time constants in reach.
|
|
434
|
+
* @param name The identifier.
|
|
435
|
+
*
|
|
436
|
+
* @returns True when a scope in the chain has it as its own.
|
|
437
|
+
*/
|
|
438
|
+
export function hasBinding(scope, name) {
|
|
439
|
+
for (let layer = scope; layer && layer !== Object.prototype; layer = Object.getPrototypeOf(layer)) {
|
|
440
|
+
if (Object.hasOwn(layer, name))
|
|
441
|
+
return true;
|
|
442
|
+
}
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Binds `name` in `scope` as its own property, so `__proto__` is a name rather than Object.prototype's setter.
|
|
447
|
+
*
|
|
448
|
+
* @param scope The scope to bind in.
|
|
449
|
+
* @param name The identifier.
|
|
450
|
+
* @param value Its value.
|
|
451
|
+
*/
|
|
452
|
+
export function setBinding(scope, name, value) {
|
|
453
|
+
Object.defineProperty(scope, name, {
|
|
454
|
+
value,
|
|
455
|
+
writable: true,
|
|
456
|
+
enumerable: true,
|
|
457
|
+
configurable: true,
|
|
458
|
+
});
|
|
459
|
+
}
|
|
202
460
|
/**
|
|
203
461
|
* `scope` with the global `undefined` bound, so a fold can resolve the name.
|
|
204
462
|
*
|