embedded-react 0.15.0 → 0.16.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 +806 -0
- package/aot/compiler/c/animations.mjs +362 -0
- package/aot/compiler/c/expressions.mjs +215 -0
- package/aot/compiler/c/functions.mjs +268 -0
- package/aot/compiler/c/keyboard.mjs +96 -0
- package/aot/compiler/c/nodes.mjs +282 -0
- package/aot/compiler/c/pan-responder.mjs +136 -0
- package/aot/compiler/c/statements.mjs +242 -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 +743 -0
- package/aot/compiler/control-flow.mjs +683 -0
- package/aot/compiler/diagnostics.mjs +137 -0
- package/aot/compiler/elements/activity-indicator.mjs +160 -0
- package/aot/compiler/elements/dial.mjs +343 -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 +1479 -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/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 +83 -0
- package/aot/compiler/ir/pan-responder.mjs +16 -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 +282 -0
- package/aot/compiler/nodes.mjs +683 -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 +228 -0
- package/aot/compiler/style-text.mjs +648 -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,300 @@
|
|
|
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 { parse } from '@babel/parser';
|
|
20
|
+
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
import { isUndeclaredAttribute } from './elements/props.mjs';
|
|
22
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
23
|
+
- Constants
|
|
24
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
25
|
+
// Type-only expression wrappers: `x as T`, `x satisfies T`, `x!`, `<T>x`, `f<T>` — unwrap to the inner expr.
|
|
26
|
+
const TS_EXPR_WRAPPERS = new Set([
|
|
27
|
+
'TSAsExpression',
|
|
28
|
+
'TSSatisfiesExpression',
|
|
29
|
+
'TSNonNullExpression',
|
|
30
|
+
'TSTypeAssertion',
|
|
31
|
+
'TSInstantiationExpression',
|
|
32
|
+
]);
|
|
33
|
+
// Type-only declarations (no runtime presence) — dropped from any statement/body list.
|
|
34
|
+
const TS_TYPE_DECLS = new Set([
|
|
35
|
+
'TSInterfaceDeclaration',
|
|
36
|
+
'TSTypeAliasDeclaration',
|
|
37
|
+
'TSDeclareFunction',
|
|
38
|
+
]);
|
|
39
|
+
// Type-only fields hung off otherwise-runtime nodes — deleted so nothing downstream traverses into them.
|
|
40
|
+
const TS_TYPE_FIELDS = [
|
|
41
|
+
'typeAnnotation',
|
|
42
|
+
'returnType',
|
|
43
|
+
'typeParameters',
|
|
44
|
+
'typeArguments',
|
|
45
|
+
'accessibility',
|
|
46
|
+
'definite',
|
|
47
|
+
'declare',
|
|
48
|
+
'readonly',
|
|
49
|
+
'override',
|
|
50
|
+
'abstract',
|
|
51
|
+
];
|
|
52
|
+
// Keys that never hold child AST nodes — skip them so the scrub stays cheap and never mangles metadata.
|
|
53
|
+
const TS_SKIP_KEYS = new Set([
|
|
54
|
+
'loc',
|
|
55
|
+
'start',
|
|
56
|
+
'end',
|
|
57
|
+
'range',
|
|
58
|
+
'leadingComments',
|
|
59
|
+
'trailingComments',
|
|
60
|
+
'innerComments',
|
|
61
|
+
'comments',
|
|
62
|
+
'extra',
|
|
63
|
+
'tokens',
|
|
64
|
+
]);
|
|
65
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
66
|
+
- Implementation
|
|
67
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
68
|
+
/**
|
|
69
|
+
* Is this app a TypeScript entry? Driven by the filename extension, or an explicit opts.ts (for tests).
|
|
70
|
+
*
|
|
71
|
+
* @param opts compileSource's options.
|
|
72
|
+
*
|
|
73
|
+
* @returns Whether to parse the source as TypeScript.
|
|
74
|
+
*/
|
|
75
|
+
const isTsEntry = (opts) => opts.ts ?? /\.[cm]?tsx$/.test(opts.filename || '');
|
|
76
|
+
/**
|
|
77
|
+
* The Babel parser plugins for an entry.
|
|
78
|
+
*
|
|
79
|
+
* @param isTypeScript Whether the entry is TypeScript.
|
|
80
|
+
*
|
|
81
|
+
* @returns JSX, plus TypeScript when asked.
|
|
82
|
+
*/
|
|
83
|
+
const parserPlugins = (isTypeScript) => isTypeScript ? ['jsx', 'typescript'] : ['jsx'];
|
|
84
|
+
/**
|
|
85
|
+
* True for nodes that carry no runtime meaning and must be removed from a statement/specifier list.
|
|
86
|
+
*
|
|
87
|
+
* @param node An element of an AST list.
|
|
88
|
+
*
|
|
89
|
+
* @returns Whether it is type-only.
|
|
90
|
+
*/
|
|
91
|
+
const isTypeOnly = (node) => !!node &&
|
|
92
|
+
(TS_TYPE_DECLS.has(node.type) ||
|
|
93
|
+
// `import type ... ` / `export type ...`, and per-specifier `import { type X }`.
|
|
94
|
+
((node.type === 'ImportDeclaration' || node.type === 'ImportSpecifier') &&
|
|
95
|
+
node.importKind === 'type') ||
|
|
96
|
+
((node.type === 'ExportNamedDeclaration' ||
|
|
97
|
+
node.type === 'ExportSpecifier') &&
|
|
98
|
+
node.exportKind === 'type'));
|
|
99
|
+
/**
|
|
100
|
+
* Follows a chain of type-only expression wrappers to the runtime expression underneath.
|
|
101
|
+
*
|
|
102
|
+
* @param node An expression, possibly wrapped in `as` / `satisfies` / `!` / `<T>`.
|
|
103
|
+
*
|
|
104
|
+
* @returns The expression under every wrapper.
|
|
105
|
+
*/
|
|
106
|
+
const unwrapTs = (node) => {
|
|
107
|
+
while (node && TS_EXPR_WRAPPERS.has(node.type)) {
|
|
108
|
+
node = node.expression;
|
|
109
|
+
}
|
|
110
|
+
return node;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Strips every TypeScript-only construct from a parsed AST, in place: drop type declarations and type
|
|
114
|
+
* imports, unwrap `as`/`!`/`<T>` expression wrappers, and delete type-annotation fields. Remaining nodes
|
|
115
|
+
* keep their .loc, so the compiler's error code-frames stay accurate.
|
|
116
|
+
*
|
|
117
|
+
* @param root The parsed file.
|
|
118
|
+
*
|
|
119
|
+
* @returns The same file, scrubbed.
|
|
120
|
+
*/
|
|
121
|
+
function stripTypeScript(root) {
|
|
122
|
+
// A walk over every key of every node, so a node here is a plain record of child values.
|
|
123
|
+
const visit = (node) => {
|
|
124
|
+
if (!node || typeof node !== 'object')
|
|
125
|
+
return;
|
|
126
|
+
// Delete the node's own type-only fields, then walk its children.
|
|
127
|
+
for (const field of TS_TYPE_FIELDS) {
|
|
128
|
+
if (field in node) {
|
|
129
|
+
delete node[field];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
for (const key of Object.keys(node)) {
|
|
133
|
+
if (TS_SKIP_KEYS.has(key))
|
|
134
|
+
continue;
|
|
135
|
+
let value = node[key];
|
|
136
|
+
if (Array.isArray(value)) {
|
|
137
|
+
// A list (statements, specifiers, arguments …): drop type-only entries and unwrap wrapped ones.
|
|
138
|
+
const kept = [];
|
|
139
|
+
for (let item of value) {
|
|
140
|
+
if (isTypeOnly(item))
|
|
141
|
+
continue;
|
|
142
|
+
if (item && TS_EXPR_WRAPPERS.has(item.type)) {
|
|
143
|
+
item = unwrapTs(item);
|
|
144
|
+
}
|
|
145
|
+
kept.push(item);
|
|
146
|
+
visit(item);
|
|
147
|
+
}
|
|
148
|
+
node[key] = kept;
|
|
149
|
+
}
|
|
150
|
+
else if (value && typeof value.type === 'string') {
|
|
151
|
+
// A single child node: replace a type-only wrapper with the expression under it.
|
|
152
|
+
if (TS_EXPR_WRAPPERS.has(value.type)) {
|
|
153
|
+
value = unwrapTs(value);
|
|
154
|
+
node[key] = value;
|
|
155
|
+
}
|
|
156
|
+
visit(value);
|
|
157
|
+
}
|
|
158
|
+
else if (value && typeof value === 'object') {
|
|
159
|
+
visit(value);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
visit(root);
|
|
164
|
+
return root;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* One pass over the parsed program that fixes what `undefined` means before anything is compiled.
|
|
168
|
+
*
|
|
169
|
+
* - A JSX attribute whose value is `{undefined}` is DROPPED. Flow A's buildProps omits an undefined prop,
|
|
170
|
+
* so the node keeps its default — `visible={undefined}` stays visible. Doing it here gives the twenty-odd
|
|
171
|
+
* prop readers that view at once, instead of each deciding what an undefined value means.
|
|
172
|
+
* A value that only becomes undefined in some scope — a prop a child was never given — is dropped per
|
|
173
|
+
* emission by omitUndefinedAttrs. This literal pass still matters for <Svg> shapes, which bypass it. An
|
|
174
|
+
* attribute a built-in element does not declare is kept, for the element to refuse by name.
|
|
175
|
+
* - A BINDING named `undefined` is refused. JS allows shadowing the global, but Flow B gives `undefined`
|
|
176
|
+
* a meaning of its own (an omitted prop, empty text), and a shadowing local would silently lose to it.
|
|
177
|
+
*
|
|
178
|
+
* @param ast The parsed file, rewritten in place.
|
|
179
|
+
*/
|
|
180
|
+
export function normalizeUndefined(ast) {
|
|
181
|
+
// Refuse an identifier that binds the name `undefined`, pointing the error at it.
|
|
182
|
+
const rejectUndefinedName = (id) => {
|
|
183
|
+
if (id?.type !== 'Identifier' || id.name !== 'undefined')
|
|
184
|
+
return;
|
|
185
|
+
const error = aotError('AOT: `undefined` cannot be used as a name', 'Flow B reads `undefined` as the global everywhere — an omitted prop, or empty text — ' +
|
|
186
|
+
'so a binding with that name would never be read. Rename it.');
|
|
187
|
+
if (id.loc) {
|
|
188
|
+
error.aotLoc = id.loc.start;
|
|
189
|
+
}
|
|
190
|
+
throw error;
|
|
191
|
+
};
|
|
192
|
+
// Refuse `undefined` anywhere in a binding pattern: plain, destructured, defaulted or rest.
|
|
193
|
+
const rejectUndefinedBindings = (patternNode) => {
|
|
194
|
+
if (!patternNode)
|
|
195
|
+
return;
|
|
196
|
+
if (patternNode.type === 'Identifier') {
|
|
197
|
+
rejectUndefinedName(patternNode);
|
|
198
|
+
}
|
|
199
|
+
else if (patternNode.type === 'ArrayPattern') {
|
|
200
|
+
patternNode.elements.forEach(rejectUndefinedBindings);
|
|
201
|
+
}
|
|
202
|
+
else if (patternNode.type === 'ObjectPattern') {
|
|
203
|
+
patternNode.properties.forEach(property => rejectUndefinedBindings(property.type === 'RestElement' ? property.argument : property.value));
|
|
204
|
+
}
|
|
205
|
+
else if (patternNode.type === 'AssignmentPattern') {
|
|
206
|
+
rejectUndefinedBindings(patternNode.left);
|
|
207
|
+
}
|
|
208
|
+
else if (patternNode.type === 'RestElement') {
|
|
209
|
+
rejectUndefinedBindings(patternNode.argument);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
// A JSX attribute written as exactly `name={undefined}`.
|
|
213
|
+
const isUndefinedAttr = (attr) => attr.type === 'JSXAttribute' &&
|
|
214
|
+
attr.value?.type === 'JSXExpressionContainer' &&
|
|
215
|
+
attr.value.expression.type === 'Identifier' &&
|
|
216
|
+
attr.value.expression.name === 'undefined';
|
|
217
|
+
// Keys that never hold child nodes.
|
|
218
|
+
const SKIP = new Set([
|
|
219
|
+
'loc',
|
|
220
|
+
'start',
|
|
221
|
+
'end',
|
|
222
|
+
'extra',
|
|
223
|
+
'leadingComments',
|
|
224
|
+
'trailingComments',
|
|
225
|
+
'innerComments',
|
|
226
|
+
]);
|
|
227
|
+
// A walk over every key of every node, so a node here is a plain record of child values.
|
|
228
|
+
const visit = (node) => {
|
|
229
|
+
// Check the names each declaring node binds and drop `{undefined}` attributes from a JSX element.
|
|
230
|
+
if (!node || typeof node.type !== 'string')
|
|
231
|
+
return;
|
|
232
|
+
switch (node.type) {
|
|
233
|
+
case 'VariableDeclarator':
|
|
234
|
+
rejectUndefinedBindings(node.id);
|
|
235
|
+
break;
|
|
236
|
+
case 'FunctionDeclaration':
|
|
237
|
+
case 'FunctionExpression':
|
|
238
|
+
case 'ArrowFunctionExpression':
|
|
239
|
+
case 'ObjectMethod':
|
|
240
|
+
case 'ClassMethod':
|
|
241
|
+
if (node.id) {
|
|
242
|
+
rejectUndefinedName(node.id);
|
|
243
|
+
}
|
|
244
|
+
node.params.forEach(rejectUndefinedBindings);
|
|
245
|
+
break;
|
|
246
|
+
case 'CatchClause':
|
|
247
|
+
rejectUndefinedBindings(node.param);
|
|
248
|
+
break;
|
|
249
|
+
case 'ClassDeclaration':
|
|
250
|
+
case 'ClassExpression':
|
|
251
|
+
if (node.id) {
|
|
252
|
+
rejectUndefinedName(node.id);
|
|
253
|
+
}
|
|
254
|
+
break;
|
|
255
|
+
case 'ImportSpecifier':
|
|
256
|
+
case 'ImportDefaultSpecifier':
|
|
257
|
+
case 'ImportNamespaceSpecifier':
|
|
258
|
+
rejectUndefinedName(node.local);
|
|
259
|
+
break;
|
|
260
|
+
case 'JSXOpeningElement':
|
|
261
|
+
// An undeclared prop stays, so the element refuses it by name whatever its value.
|
|
262
|
+
node.attributes = node.attributes.filter((attr) => !isUndefinedAttr(attr) ||
|
|
263
|
+
isUndeclaredAttribute(node, attr));
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
// Then walk every child node.
|
|
267
|
+
for (const key of Object.keys(node)) {
|
|
268
|
+
if (SKIP.has(key))
|
|
269
|
+
continue;
|
|
270
|
+
const value = node[key];
|
|
271
|
+
if (Array.isArray(value)) {
|
|
272
|
+
value.forEach(visit);
|
|
273
|
+
}
|
|
274
|
+
else if (value && typeof value.type === 'string') {
|
|
275
|
+
visit(value);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
visit(ast);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Parses an app entry to a JS+JSX AST, transparently stripping TypeScript when the entry is .tsx (JSX is not
|
|
283
|
+
* allowed in a .ts file). The strip is faithful: an App.tsx compiles to the same C as its untyped App.jsx twin.
|
|
284
|
+
*
|
|
285
|
+
* @param src The entry's source.
|
|
286
|
+
* @param opts compileSource's options: its filename (or `ts`) decides whether it is TypeScript.
|
|
287
|
+
*
|
|
288
|
+
* @returns The parsed file.
|
|
289
|
+
*/
|
|
290
|
+
export function parseApp(src, opts = {}) {
|
|
291
|
+
const isTypeScript = isTsEntry(opts);
|
|
292
|
+
const ast = parse(src, {
|
|
293
|
+
sourceType: 'module',
|
|
294
|
+
plugins: parserPlugins(isTypeScript),
|
|
295
|
+
});
|
|
296
|
+
if (isTypeScript) {
|
|
297
|
+
stripTypeScript(ast);
|
|
298
|
+
}
|
|
299
|
+
return ast;
|
|
300
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
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 { aotError } from './diagnostics.mjs';
|
|
20
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
21
|
+
- Implementation
|
|
22
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
23
|
+
/**
|
|
24
|
+
* evalStatic at a boundary that REQUIRES a compile-time constant. On a fold failure it rethrows as a
|
|
25
|
+
* LOCATED aotError with a clear message (+ optional hint), instead of letting evalStatic's bare
|
|
26
|
+
* control-flow error ("cannot statically resolve identifier …") leak to the user without a location.
|
|
27
|
+
*
|
|
28
|
+
* @param node The expression to fold.
|
|
29
|
+
* @param scope Compile-time constants in reach.
|
|
30
|
+
* @param message The error to report when it does not fold.
|
|
31
|
+
* @param hint How to rewrite it, if there is a known way.
|
|
32
|
+
*
|
|
33
|
+
* @returns The folded value.
|
|
34
|
+
*/
|
|
35
|
+
export function evalStaticOrThrow(node, scope, message, hint) {
|
|
36
|
+
try {
|
|
37
|
+
return evalStatic(node, scope);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
const error = aotError(message, hint);
|
|
41
|
+
if (node && node.loc) {
|
|
42
|
+
error.aotLoc = node.loc.start;
|
|
43
|
+
}
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Folds an expression to the value JS would give it when every name it reads is a compile-time constant.
|
|
49
|
+
* Styles and state initial values fold through here. A caller that can also handle a runtime value catches
|
|
50
|
+
* the error thrown on anything dynamic (a state reference, say) and emits C instead.
|
|
51
|
+
*
|
|
52
|
+
* @param node The expression.
|
|
53
|
+
* @param scope Compile-time constants in reach.
|
|
54
|
+
*
|
|
55
|
+
* @returns The value: a number, string, boolean, null, or an object or array of those.
|
|
56
|
+
*
|
|
57
|
+
* @throws An error when any part of the expression is not a compile-time constant.
|
|
58
|
+
*/
|
|
59
|
+
export function evalStatic(node, scope) {
|
|
60
|
+
switch (node.type) {
|
|
61
|
+
case 'NumericLiteral':
|
|
62
|
+
case 'StringLiteral':
|
|
63
|
+
case 'BooleanLiteral':
|
|
64
|
+
return node.value;
|
|
65
|
+
case 'NullLiteral':
|
|
66
|
+
return null;
|
|
67
|
+
case 'UnaryExpression': {
|
|
68
|
+
const operand = evalStatic(node.argument, scope);
|
|
69
|
+
if (node.operator === '-')
|
|
70
|
+
return -operand;
|
|
71
|
+
if (node.operator === '+')
|
|
72
|
+
return +operand;
|
|
73
|
+
if (node.operator === '!')
|
|
74
|
+
return !operand;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case 'BinaryExpression': {
|
|
78
|
+
const left = evalStatic(node.left, scope);
|
|
79
|
+
const right = evalStatic(node.right, scope);
|
|
80
|
+
switch (node.operator) {
|
|
81
|
+
case '+':
|
|
82
|
+
return left + right;
|
|
83
|
+
case '-':
|
|
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;
|
|
107
|
+
}
|
|
108
|
+
case 'LogicalExpression': {
|
|
109
|
+
const left = evalStatic(node.left, scope);
|
|
110
|
+
if (node.operator === '&&') {
|
|
111
|
+
return left ? evalStatic(node.right, scope) : left;
|
|
112
|
+
}
|
|
113
|
+
if (node.operator === '||') {
|
|
114
|
+
return left ? left : evalStatic(node.right, scope);
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case 'ConditionalExpression':
|
|
119
|
+
return evalStatic(node.test, scope)
|
|
120
|
+
? evalStatic(node.consequent, scope)
|
|
121
|
+
: evalStatic(node.alternate, scope);
|
|
122
|
+
case 'Identifier':
|
|
123
|
+
if (node.name in scope)
|
|
124
|
+
return scope[node.name];
|
|
125
|
+
throw new Error(`AOT: cannot statically resolve identifier "${node.name}"`);
|
|
126
|
+
case 'MemberExpression': {
|
|
127
|
+
const object = evalStatic(node.object, scope);
|
|
128
|
+
const key = node.computed
|
|
129
|
+
? evalStatic(node.property, scope)
|
|
130
|
+
: node.property.name;
|
|
131
|
+
if (object == null) {
|
|
132
|
+
throw new Error(`AOT: member access on null/undefined ("${key}")`);
|
|
133
|
+
}
|
|
134
|
+
return object[key];
|
|
135
|
+
}
|
|
136
|
+
case 'ObjectExpression': {
|
|
137
|
+
const folded = {};
|
|
138
|
+
for (const prop of node.properties) {
|
|
139
|
+
if (prop.type !== 'ObjectProperty') {
|
|
140
|
+
throw new Error('AOT: object spreads/methods not supported in static objects');
|
|
141
|
+
}
|
|
142
|
+
const key = prop.computed
|
|
143
|
+
? evalStatic(prop.key, scope)
|
|
144
|
+
: (prop.key.name ??
|
|
145
|
+
prop.key.value);
|
|
146
|
+
folded[key] = evalStatic(prop.value, scope);
|
|
147
|
+
}
|
|
148
|
+
return folded;
|
|
149
|
+
}
|
|
150
|
+
case 'ArrayExpression':
|
|
151
|
+
return node.elements.map(element => element ? evalStatic(element, scope) : null);
|
|
152
|
+
case 'CallExpression': {
|
|
153
|
+
// `StyleSheet.create({…})` folds to its argument; any other call is dynamic.
|
|
154
|
+
const callee = node.callee;
|
|
155
|
+
if (callee.type === 'MemberExpression' &&
|
|
156
|
+
callee.object.name === 'StyleSheet' &&
|
|
157
|
+
callee.property.name === 'create') {
|
|
158
|
+
return evalStatic(node.arguments[0], scope);
|
|
159
|
+
}
|
|
160
|
+
throw new Error(`AOT: cannot statically evaluate call expression`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
throw new Error(`AOT: unsupported expression "${node.type}" in static context`);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* `scope` minus every name `env` binds at RUNTIME — locals, state, refs, animated values, and the event /
|
|
167
|
+
* gesture params of the handler being compiled. emitExpr resolves those before env.consts, so a fold
|
|
168
|
+
* that consulted the raw scope would silently swap a module const in for the value the code actually
|
|
169
|
+
* uses. Every env-driven constant fold goes through this; the JSX-side folds keep the same rule by
|
|
170
|
+
* deleting a name from their scope copy when they bind it (see emitComponent / emitDynamicMap).
|
|
171
|
+
*
|
|
172
|
+
* @param env The expression environment.
|
|
173
|
+
* @param scope The constants a fold would otherwise see.
|
|
174
|
+
*
|
|
175
|
+
* @returns `scope` itself when nothing shadows it, else a copy without the shadowed names.
|
|
176
|
+
*/
|
|
177
|
+
export function foldScope(env, scope) {
|
|
178
|
+
const runtimeNames = new Set([
|
|
179
|
+
...(env.locals?.keys() ?? []),
|
|
180
|
+
...(env.state?.keys() ?? []),
|
|
181
|
+
...(env.refs?.keys() ?? []),
|
|
182
|
+
...(env.animations?.keys() ?? []),
|
|
183
|
+
]);
|
|
184
|
+
if (env.event) {
|
|
185
|
+
runtimeNames.add(env.event);
|
|
186
|
+
}
|
|
187
|
+
if (env.gesture) {
|
|
188
|
+
runtimeNames.add(env.gesture);
|
|
189
|
+
}
|
|
190
|
+
// Copy the scope only when a runtime name actually shadows one of its constants.
|
|
191
|
+
let hasShadowedName = false;
|
|
192
|
+
for (const name of runtimeNames) {
|
|
193
|
+
if (name in scope) {
|
|
194
|
+
hasShadowedName = true;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (!hasShadowedName)
|
|
199
|
+
return scope;
|
|
200
|
+
return Object.fromEntries(Object.entries(scope).filter(([name]) => !runtimeNames.has(name)));
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* `scope` with the global `undefined` bound, so a fold can resolve the name.
|
|
204
|
+
*
|
|
205
|
+
* @param scope Compile-time constants in reach.
|
|
206
|
+
*
|
|
207
|
+
* @returns A scope that reads through to `scope` and also binds `undefined`.
|
|
208
|
+
*/
|
|
209
|
+
export const withUndefined = (scope) => Object.assign(Object.create(scope), { undefined });
|
|
210
|
+
/**
|
|
211
|
+
* evalStatic with a fallback default when the node is absent or not foldable.
|
|
212
|
+
*
|
|
213
|
+
* @param node The expression, if there is one.
|
|
214
|
+
* @param env The expression environment; its constants are what the fold sees.
|
|
215
|
+
* @param defaultValue The value to use when the node is absent or does not fold.
|
|
216
|
+
*
|
|
217
|
+
* @returns The folded value, or `defaultValue`.
|
|
218
|
+
*/
|
|
219
|
+
export function evalStaticOr(node, env, defaultValue) {
|
|
220
|
+
if (!node)
|
|
221
|
+
return defaultValue;
|
|
222
|
+
try {
|
|
223
|
+
return evalStatic(node, foldScope(env, env.consts ?? {}));
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
return defaultValue;
|
|
227
|
+
}
|
|
228
|
+
}
|