embedded-react 0.15.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aot/compile.mjs +173 -8272
- package/aot/compiler/animations.mjs +1001 -0
- package/aot/compiler/assemble.mjs +922 -0
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +319 -0
- package/aot/compiler/c/animations.mjs +362 -0
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +270 -0
- package/aot/compiler/c/functions.mjs +268 -0
- package/aot/compiler/c/keyboard.mjs +96 -0
- package/aot/compiler/c/nodes.mjs +294 -0
- package/aot/compiler/c/pan-responder.mjs +136 -0
- package/aot/compiler/c/statements.mjs +271 -0
- package/aot/compiler/c/text.mjs +83 -0
- package/aot/compiler/c/vectors.mjs +381 -0
- package/aot/compiler/c-syntax.mjs +127 -0
- package/aot/compiler/check.mjs +165 -0
- package/aot/compiler/collect.mjs +785 -0
- package/aot/compiler/control-flow.mjs +696 -0
- package/aot/compiler/diagnostics.mjs +137 -0
- package/aot/compiler/elements/activity-indicator.mjs +160 -0
- package/aot/compiler/elements/dial.mjs +346 -0
- package/aot/compiler/elements/flat-list.mjs +139 -0
- package/aot/compiler/elements/image.mjs +151 -0
- package/aot/compiler/elements/modal.mjs +158 -0
- package/aot/compiler/elements/props.mjs +474 -0
- package/aot/compiler/elements/switch.mjs +183 -0
- package/aot/compiler/elements/text-input.mjs +171 -0
- package/aot/compiler/expressions.mjs +1579 -0
- package/aot/compiler/handlers.mjs +1072 -0
- package/aot/compiler/ir/animations.mjs +16 -0
- package/aot/compiler/ir/app.mjs +16 -0
- package/aot/compiler/ir/boot.mjs +16 -0
- package/aot/compiler/ir/expressions.mjs +16 -0
- package/aot/compiler/ir/functions.mjs +16 -0
- package/aot/compiler/ir/keyboard.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +107 -0
- package/aot/compiler/ir/pan-responder.mjs +16 -0
- package/aot/compiler/ir/reads.mjs +351 -0
- package/aot/compiler/ir/source.mjs +16 -0
- package/aot/compiler/ir/statements.mjs +16 -0
- package/aot/compiler/ir/text.mjs +16 -0
- package/aot/compiler/ir/types.mjs +185 -0
- package/aot/compiler/ir/vectors.mjs +16 -0
- package/aot/compiler/keyboard.mjs +152 -0
- package/aot/compiler/lower.mjs +311 -0
- package/aot/compiler/nodes.mjs +688 -0
- package/aot/compiler/options.mjs +55 -0
- package/aot/compiler/out.mjs +94 -0
- package/aot/compiler/pan-responder.mjs +354 -0
- package/aot/compiler/parse.mjs +300 -0
- package/aot/compiler/static-eval.mjs +454 -0
- package/aot/compiler/style-text.mjs +807 -0
- package/aot/compiler/svg.mjs +1167 -0
- package/aot/compiler/types.mjs +16 -0
- package/aot/screenshot-smoke.mjs +95 -55
- package/aot/style-map.mjs +366 -304
- package/cli.mjs +5 -8
- package/package.json +14 -6
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/FlatList.js +4 -4
- package/src/props.js +1 -1
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 Cory Lamming
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
17
|
+
- Imports
|
|
18
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
|
+
import { BootReadError, BootValue, buildValueOf, containsBoot, foldBoot, isBoot, isContainer, } from './boot.mjs';
|
|
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
|
+
};
|
|
34
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
35
|
+
- Implementation
|
|
36
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
37
|
+
/**
|
|
38
|
+
* evalStatic at a boundary that REQUIRES a compile-time constant. On a fold failure it rethrows as a
|
|
39
|
+
* LOCATED aotError with a clear message (+ optional hint), instead of letting evalStatic's bare
|
|
40
|
+
* control-flow error ("cannot statically resolve identifier …") leak to the user without a location.
|
|
41
|
+
*
|
|
42
|
+
* @param node The expression to fold.
|
|
43
|
+
* @param scope Compile-time constants in reach.
|
|
44
|
+
* @param message The error to report when it does not fold.
|
|
45
|
+
* @param hint How to rewrite it, if there is a known way.
|
|
46
|
+
*
|
|
47
|
+
* @returns The folded value.
|
|
48
|
+
*/
|
|
49
|
+
export function evalStaticOrThrow(node, scope, message, hint) {
|
|
50
|
+
try {
|
|
51
|
+
return evalStatic(node, scope);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
const error = aotError(message, hint);
|
|
55
|
+
if (node && node.loc) {
|
|
56
|
+
error.aotLoc = node.loc.start;
|
|
57
|
+
}
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Folds an expression to the value JS would give it when every name it reads is a compile-time constant.
|
|
63
|
+
* Styles and state initial values fold through here. A caller that can also handle a runtime value catches
|
|
64
|
+
* the error thrown on anything dynamic (a state reference, say) and emits C instead.
|
|
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
|
+
*
|
|
70
|
+
* @param node The expression.
|
|
71
|
+
* @param scope Compile-time constants in reach.
|
|
72
|
+
* @param mode How a value derived from the panel size folds.
|
|
73
|
+
*
|
|
74
|
+
* @returns The value: a number, string, boolean, null, or an object or array of those.
|
|
75
|
+
*
|
|
76
|
+
* @throws An error when any part of the expression is not a compile-time constant.
|
|
77
|
+
*/
|
|
78
|
+
export function evalStatic(node, scope, mode = 'fold') {
|
|
79
|
+
const value = evaluate(node, scope, { scope, snapshot: null });
|
|
80
|
+
if (mode === 'boot')
|
|
81
|
+
return value;
|
|
82
|
+
if (mode === 'fold')
|
|
83
|
+
return foldBoot(value);
|
|
84
|
+
if (containsBoot(value)) {
|
|
85
|
+
throw new BootReadError();
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The names a boot value's expression reads, copied once per fold: the scope a const was folded in can lose
|
|
91
|
+
* or rebind names afterward.
|
|
92
|
+
*
|
|
93
|
+
* @param fold The fold.
|
|
94
|
+
*
|
|
95
|
+
* @returns The copy.
|
|
96
|
+
*/
|
|
97
|
+
function snapshotOf(fold) {
|
|
98
|
+
if (!fold.snapshot) {
|
|
99
|
+
const copy = {};
|
|
100
|
+
for (const name in fold.scope) {
|
|
101
|
+
copy[name] = fold.scope[name];
|
|
102
|
+
}
|
|
103
|
+
fold.snapshot = copy;
|
|
104
|
+
}
|
|
105
|
+
return fold.snapshot;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* A boot value for an expression the fold could only take so far because it reads one.
|
|
109
|
+
*
|
|
110
|
+
* @param node The expression.
|
|
111
|
+
* @param value What it is on the build's panel.
|
|
112
|
+
* @param fold The fold.
|
|
113
|
+
* @param lowerable Boot can work it out from the expression.
|
|
114
|
+
* @param basis What a fold of it assumes when boot cannot work it out.
|
|
115
|
+
*
|
|
116
|
+
* @returns The boot value.
|
|
117
|
+
*/
|
|
118
|
+
const bootOf = (node, value, fold, lowerable = true, basis = []) => new BootValue(node, snapshotOf(fold), buildValueOf(value), lowerable, null, null, basis);
|
|
119
|
+
/**
|
|
120
|
+
* `test ? consequent : alternate` with a test that depends on the panel. Two objects with the same keys merge
|
|
121
|
+
* into one whose differing entries each pick at boot, so a branch that changes only values stays one value;
|
|
122
|
+
* anything else is one boot value.
|
|
123
|
+
*
|
|
124
|
+
* @param node The expression.
|
|
125
|
+
* @param test The test, as a boot value.
|
|
126
|
+
* @param fold The fold.
|
|
127
|
+
*
|
|
128
|
+
* @returns The value.
|
|
129
|
+
*/
|
|
130
|
+
function bootConditional(node, test, fold) {
|
|
131
|
+
const taken = buildValueOf(test) ? node.consequent : node.alternate;
|
|
132
|
+
const takenValue = evaluate(taken, fold.scope, fold);
|
|
133
|
+
let otherValue;
|
|
134
|
+
try {
|
|
135
|
+
otherValue = evaluate(taken === node.consequent ? node.alternate : node.consequent, fold.scope, fold);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// The branch the build's panel does not take does not fold, so boot cannot pick between them.
|
|
139
|
+
return bootOf(node, takenValue, fold, false, [test]);
|
|
140
|
+
}
|
|
141
|
+
const [consequent, alternate] = taken === node.consequent
|
|
142
|
+
? [takenValue, otherValue]
|
|
143
|
+
: [otherValue, takenValue];
|
|
144
|
+
return mergeBranches(node, node.consequent, node.alternate, [
|
|
145
|
+
consequent,
|
|
146
|
+
alternate,
|
|
147
|
+
takenValue,
|
|
148
|
+
]);
|
|
149
|
+
/**
|
|
150
|
+
* Merges two branch values at `at`, entry by entry where both are objects. An entry only one branch has is
|
|
151
|
+
* taken at the build's panel, which assumes the test.
|
|
152
|
+
*
|
|
153
|
+
* @param at The expression the merged value stands for.
|
|
154
|
+
* @param consequentNode The consequent branch's expression.
|
|
155
|
+
* @param alternateNode The alternate branch's expression.
|
|
156
|
+
* @param values The consequent's value, the alternate's, and the one the build's panel takes.
|
|
157
|
+
*
|
|
158
|
+
* @returns The merged value.
|
|
159
|
+
*/
|
|
160
|
+
function mergeBranches(at, consequentNode, alternateNode, [whenTrue, whenFalse, built]) {
|
|
161
|
+
if (whenTrue === whenFalse)
|
|
162
|
+
return whenTrue;
|
|
163
|
+
const mergeable = isContainer(whenTrue) &&
|
|
164
|
+
isContainer(whenFalse) &&
|
|
165
|
+
!Array.isArray(whenTrue) &&
|
|
166
|
+
!Array.isArray(whenFalse);
|
|
167
|
+
if (!mergeable) {
|
|
168
|
+
return bootOf(at, built, fold, !isContainer(buildValueOf(built)), [test]);
|
|
169
|
+
}
|
|
170
|
+
// Each entry is `test ? (consequent).key : (alternate).key`, which boot works out on its own.
|
|
171
|
+
const merged = {};
|
|
172
|
+
const keys = new Set([...Object.keys(whenTrue), ...Object.keys(whenFalse)]);
|
|
173
|
+
for (const key of keys) {
|
|
174
|
+
if (!Object.hasOwn(whenTrue, key) || !Object.hasOwn(whenFalse, key)) {
|
|
175
|
+
const builtEntry = built[key];
|
|
176
|
+
merged[key] = bootOf(at, builtEntry, fold, false, [test]);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const entryOf = (object) => ({
|
|
180
|
+
type: 'MemberExpression',
|
|
181
|
+
object,
|
|
182
|
+
property: { type: 'StringLiteral', value: key },
|
|
183
|
+
computed: true,
|
|
184
|
+
loc: object.loc,
|
|
185
|
+
});
|
|
186
|
+
const consequentEntry = entryOf(consequentNode);
|
|
187
|
+
const alternateEntry = entryOf(alternateNode);
|
|
188
|
+
merged[key] = mergeBranches({
|
|
189
|
+
type: 'ConditionalExpression',
|
|
190
|
+
test: node.test,
|
|
191
|
+
consequent: consequentEntry,
|
|
192
|
+
alternate: alternateEntry,
|
|
193
|
+
loc: at.loc,
|
|
194
|
+
}, consequentEntry, alternateEntry, [
|
|
195
|
+
whenTrue[key],
|
|
196
|
+
whenFalse[key],
|
|
197
|
+
built[key],
|
|
198
|
+
]);
|
|
199
|
+
}
|
|
200
|
+
return merged;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* evalStatic's fold, which keeps a value derived from the panel size as a boot value.
|
|
205
|
+
*
|
|
206
|
+
* @param node The expression.
|
|
207
|
+
* @param scope Compile-time constants in reach.
|
|
208
|
+
* @param fold The fold this expression is part of.
|
|
209
|
+
*
|
|
210
|
+
* @returns The value.
|
|
211
|
+
*/
|
|
212
|
+
function evaluate(node, scope, fold) {
|
|
213
|
+
switch (node.type) {
|
|
214
|
+
case 'NumericLiteral':
|
|
215
|
+
case 'StringLiteral':
|
|
216
|
+
case 'BooleanLiteral':
|
|
217
|
+
return node.value;
|
|
218
|
+
case 'NullLiteral':
|
|
219
|
+
return null;
|
|
220
|
+
case 'UnaryExpression': {
|
|
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);
|
|
241
|
+
}
|
|
242
|
+
case 'BinaryExpression': {
|
|
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;
|
|
249
|
+
}
|
|
250
|
+
case 'LogicalExpression': {
|
|
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;
|
|
257
|
+
}
|
|
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]);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
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
|
+
}
|
|
278
|
+
case 'Identifier':
|
|
279
|
+
if (node.name in scope)
|
|
280
|
+
return scope[node.name];
|
|
281
|
+
throw new Error(`AOT: cannot statically resolve identifier "${node.name}"`);
|
|
282
|
+
case 'MemberExpression': {
|
|
283
|
+
const object = evaluate(node.object, scope, fold);
|
|
284
|
+
const key = node.computed
|
|
285
|
+
? evaluate(node.property, scope, fold)
|
|
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
|
+
}
|
|
298
|
+
if (object == null) {
|
|
299
|
+
throw new Error(`AOT: member access on null/undefined ("${key}")`);
|
|
300
|
+
}
|
|
301
|
+
return object[key];
|
|
302
|
+
}
|
|
303
|
+
case 'ObjectExpression': {
|
|
304
|
+
const folded = {};
|
|
305
|
+
for (const prop of node.properties) {
|
|
306
|
+
if (prop.type !== 'ObjectProperty') {
|
|
307
|
+
throw new Error('AOT: object spreads/methods not supported in static objects');
|
|
308
|
+
}
|
|
309
|
+
const key = prop.computed
|
|
310
|
+
? evaluate(prop.key, scope, fold)
|
|
311
|
+
: (prop.key.name ??
|
|
312
|
+
prop.key.value);
|
|
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);
|
|
317
|
+
}
|
|
318
|
+
return folded;
|
|
319
|
+
}
|
|
320
|
+
case 'ArrayExpression':
|
|
321
|
+
return node.elements.map(element => element ? evaluate(element, scope, fold) : null);
|
|
322
|
+
case 'CallExpression': {
|
|
323
|
+
// `StyleSheet.create({…})` folds to its argument; any other call is dynamic.
|
|
324
|
+
const callee = node.callee;
|
|
325
|
+
if (callee.type === 'MemberExpression' &&
|
|
326
|
+
callee.object.name === 'StyleSheet' &&
|
|
327
|
+
callee.property.name === 'create') {
|
|
328
|
+
return evaluate(node.arguments[0], scope, fold);
|
|
329
|
+
}
|
|
330
|
+
// Math.min / max / floor / ceil / round / trunc / abs fold as JS works them out; others are dynamic.
|
|
331
|
+
const mathFn = callee.type === 'MemberExpression' &&
|
|
332
|
+
!callee.computed &&
|
|
333
|
+
callee.object.type === 'Identifier' &&
|
|
334
|
+
callee.object.name === 'Math' &&
|
|
335
|
+
!('Math' in scope)
|
|
336
|
+
? callee.property.name
|
|
337
|
+
: null;
|
|
338
|
+
if (mathFn && Object.hasOwn(STATIC_MATH, mathFn)) {
|
|
339
|
+
const args = node.arguments.map(arg => {
|
|
340
|
+
if (arg.type === 'SpreadElement') {
|
|
341
|
+
throw new Error('AOT: cannot statically evaluate a spread');
|
|
342
|
+
}
|
|
343
|
+
return evaluate(arg, scope, fold);
|
|
344
|
+
});
|
|
345
|
+
const result = STATIC_MATH[mathFn](...args.map(buildValueOf));
|
|
346
|
+
return args.some(isBoot) ? bootOf(node, result, fold) : result;
|
|
347
|
+
}
|
|
348
|
+
throw new Error(`AOT: cannot statically evaluate call expression`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
throw new Error(`AOT: unsupported expression "${node.type}" in static context`);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* A binary operator applied as JS applies it.
|
|
355
|
+
*
|
|
356
|
+
* @param operator The operator.
|
|
357
|
+
* @param left The left operand.
|
|
358
|
+
* @param right The right operand.
|
|
359
|
+
*
|
|
360
|
+
* @returns The result.
|
|
361
|
+
*/
|
|
362
|
+
function binary(operator, left, right) {
|
|
363
|
+
switch (operator) {
|
|
364
|
+
case '+':
|
|
365
|
+
return left + right;
|
|
366
|
+
case '-':
|
|
367
|
+
return left - right;
|
|
368
|
+
case '*':
|
|
369
|
+
return left * right;
|
|
370
|
+
case '/':
|
|
371
|
+
return left / right;
|
|
372
|
+
case '%':
|
|
373
|
+
return left % right;
|
|
374
|
+
case '<':
|
|
375
|
+
return left < right;
|
|
376
|
+
case '>':
|
|
377
|
+
return left > right;
|
|
378
|
+
case '<=':
|
|
379
|
+
return left <= right;
|
|
380
|
+
case '>=':
|
|
381
|
+
return left >= right;
|
|
382
|
+
case '==':
|
|
383
|
+
case '===':
|
|
384
|
+
return left === right;
|
|
385
|
+
case '!=':
|
|
386
|
+
case '!==':
|
|
387
|
+
return left !== right;
|
|
388
|
+
}
|
|
389
|
+
throw new Error('AOT: unsupported expression "BinaryExpression" in static context');
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* `scope` minus every name `env` binds at RUNTIME — locals, state, refs, animated values, and the event /
|
|
393
|
+
* gesture params of the handler being compiled. emitExpr resolves those before env.consts, so a fold
|
|
394
|
+
* that consulted the raw scope would silently swap a module const in for the value the code actually
|
|
395
|
+
* uses. Every env-driven constant fold goes through this; the JSX-side folds keep the same rule by
|
|
396
|
+
* deleting a name from their scope copy when they bind it (see emitComponent / emitDynamicMap).
|
|
397
|
+
*
|
|
398
|
+
* @param env The expression environment.
|
|
399
|
+
* @param scope The constants a fold would otherwise see.
|
|
400
|
+
*
|
|
401
|
+
* @returns `scope` itself when nothing shadows it, else a copy without the shadowed names.
|
|
402
|
+
*/
|
|
403
|
+
export function foldScope(env, scope) {
|
|
404
|
+
const runtimeNames = new Set([
|
|
405
|
+
...(env.locals?.keys() ?? []),
|
|
406
|
+
...(env.state?.keys() ?? []),
|
|
407
|
+
...(env.refs?.keys() ?? []),
|
|
408
|
+
...(env.animations?.keys() ?? []),
|
|
409
|
+
]);
|
|
410
|
+
if (env.event) {
|
|
411
|
+
runtimeNames.add(env.event);
|
|
412
|
+
}
|
|
413
|
+
if (env.gesture) {
|
|
414
|
+
runtimeNames.add(env.gesture);
|
|
415
|
+
}
|
|
416
|
+
// Copy the scope only when a runtime name actually shadows one of its constants.
|
|
417
|
+
let hasShadowedName = false;
|
|
418
|
+
for (const name of runtimeNames) {
|
|
419
|
+
if (name in scope) {
|
|
420
|
+
hasShadowedName = true;
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
if (!hasShadowedName)
|
|
425
|
+
return scope;
|
|
426
|
+
return Object.fromEntries(Object.entries(scope).filter(([name]) => !runtimeNames.has(name)));
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* `scope` with the global `undefined` bound, so a fold can resolve the name.
|
|
430
|
+
*
|
|
431
|
+
* @param scope Compile-time constants in reach.
|
|
432
|
+
*
|
|
433
|
+
* @returns A scope that reads through to `scope` and also binds `undefined`.
|
|
434
|
+
*/
|
|
435
|
+
export const withUndefined = (scope) => Object.assign(Object.create(scope), { undefined });
|
|
436
|
+
/**
|
|
437
|
+
* evalStatic with a fallback default when the node is absent or not foldable.
|
|
438
|
+
*
|
|
439
|
+
* @param node The expression, if there is one.
|
|
440
|
+
* @param env The expression environment; its constants are what the fold sees.
|
|
441
|
+
* @param defaultValue The value to use when the node is absent or does not fold.
|
|
442
|
+
*
|
|
443
|
+
* @returns The folded value, or `defaultValue`.
|
|
444
|
+
*/
|
|
445
|
+
export function evalStaticOr(node, env, defaultValue) {
|
|
446
|
+
if (!node)
|
|
447
|
+
return defaultValue;
|
|
448
|
+
try {
|
|
449
|
+
return evalStatic(node, foldScope(env, env.consts ?? {}));
|
|
450
|
+
}
|
|
451
|
+
catch {
|
|
452
|
+
return defaultValue;
|
|
453
|
+
}
|
|
454
|
+
}
|