embedded-react 0.20.0 → 0.21.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/NOTICE +5 -0
- package/README.md +1 -0
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +16 -46
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +61 -9
- package/aot/compiler/collect.mjs +516 -136
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +38 -3
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1351 -97
- package/aot/compiler/handlers.mjs +1761 -365
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/list-items.mjs +226 -0
- package/aot/compiler/list-order.mjs +586 -0
- package/aot/compiler/list-split.mjs +259 -0
- package/aot/compiler/loops.mjs +582 -0
- package/aot/compiler/lower.mjs +191 -53
- package/aot/compiler/native-files.mjs +439 -0
- package/aot/compiler/native-header.mjs +335 -0
- package/aot/compiler/native-modules.mjs +1173 -0
- package/aot/compiler/nodes.mjs +28 -22
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +14 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +870 -31
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +12 -1
- package/aot/compiler/package/lower.mjs +658 -161
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- package/aot/compiler/record-shapes.mjs +246 -0
- package/aot/compiler/records.mjs +654 -0
- package/aot/compiler/reducers.mjs +272 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +18 -3
- package/aot/compiler/svg.mjs +7 -4
- package/aot/compiler/switch-cases.mjs +198 -0
- package/aot/compiler/tagged-records.mjs +450 -0
- package/aot/compiler/typing.mjs +307 -0
- package/aot/compiler/updaters.mjs +320 -0
- package/aot/compiler/window-dimensions.mjs +94 -0
- package/aot/compiler/worked-values.mjs +319 -0
- package/aot/demo-file.mjs +30 -0
- package/aot/style-map.mjs +50 -2
- package/build.mjs +9 -1
- package/cli.mjs +44 -1
- package/hotreload/pack-split.mjs +29 -40
- package/pack-container.mjs +9 -1
- package/package.json +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim-server.mjs +22 -22
- package/src/embedded-react/Dimensions.js +37 -0
- package/src/embedded-react/SectionList.js +1 -3
- package/src/embedded-react/index.d.ts +83 -3
- package/src/embedded-react/index.js +2 -0
- package/src/embedded-react/nativeModule.js +462 -0
|
@@ -0,0 +1,246 @@
|
|
|
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
|
+
// record-shapes.mts — the shapes an object state takes: its initial value, and the objects its setter is given.
|
|
17
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
18
|
+
- Imports
|
|
19
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
20
|
+
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
import { flattenTree, isObjectTree, propertyKey } from './records.mjs';
|
|
22
|
+
import { evalStatic, rethrowFoldLimit } from './static-eval.mjs';
|
|
23
|
+
import { returnedValues } from './updaters.mjs';
|
|
24
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
25
|
+
- Constants
|
|
26
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
27
|
+
// Keys that never hold child AST nodes.
|
|
28
|
+
const SKIP_KEYS = new Set([
|
|
29
|
+
'loc',
|
|
30
|
+
'start',
|
|
31
|
+
'end',
|
|
32
|
+
'range',
|
|
33
|
+
'extra',
|
|
34
|
+
'leadingComments',
|
|
35
|
+
'trailingComments',
|
|
36
|
+
'innerComments',
|
|
37
|
+
]);
|
|
38
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
39
|
+
- Implementation
|
|
40
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
41
|
+
/**
|
|
42
|
+
* Whether a value can name a shape: a string, a finite number or a boolean.
|
|
43
|
+
*
|
|
44
|
+
* @param value The value.
|
|
45
|
+
*
|
|
46
|
+
* @returns Whether it can.
|
|
47
|
+
*/
|
|
48
|
+
const isTagValue = (value) => typeof value === 'string' ||
|
|
49
|
+
typeof value === 'boolean' ||
|
|
50
|
+
(typeof value === 'number' && Number.isFinite(value));
|
|
51
|
+
/**
|
|
52
|
+
* The fields of an object literal, nested ones flattened; a field that folds holds its value.
|
|
53
|
+
*
|
|
54
|
+
* @param node The object.
|
|
55
|
+
* @param scope Compile-time constants in reach.
|
|
56
|
+
*
|
|
57
|
+
* @returns The object.
|
|
58
|
+
*/
|
|
59
|
+
function writtenObjectOf(node, scope) {
|
|
60
|
+
const written = { fields: new Map(), spread: false, node };
|
|
61
|
+
const visit = (object, path) => {
|
|
62
|
+
for (const property of object.properties) {
|
|
63
|
+
if (property.type === 'SpreadElement') {
|
|
64
|
+
written.spread = true;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (property.type !== 'ObjectProperty')
|
|
68
|
+
continue;
|
|
69
|
+
let key;
|
|
70
|
+
try {
|
|
71
|
+
key = propertyKey(property);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
continue; // refused where the write is lowered
|
|
75
|
+
}
|
|
76
|
+
const fieldPath = [...path, key];
|
|
77
|
+
const { value: valueNode } = property;
|
|
78
|
+
if (valueNode.type === 'ObjectExpression') {
|
|
79
|
+
visit(valueNode, fieldPath);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
let value;
|
|
83
|
+
let known = true;
|
|
84
|
+
try {
|
|
85
|
+
value = evalStatic(valueNode, scope);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
rethrowFoldLimit(error);
|
|
89
|
+
known = false;
|
|
90
|
+
}
|
|
91
|
+
if (known && isObjectTree(value)) {
|
|
92
|
+
for (const [nestedKey, nested] of flattenTree(value)) {
|
|
93
|
+
written.fields.set([...fieldPath, nestedKey].join('.'), {
|
|
94
|
+
known: true,
|
|
95
|
+
value: nested,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
written.fields.set(fieldPath.join('.'), known ? { known, value, node: valueNode } : { known, node: valueNode });
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
visit(node, []);
|
|
104
|
+
return written;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The objects a value can be: itself, a side of a ternary, or what a function returns.
|
|
108
|
+
*
|
|
109
|
+
* @param node The value.
|
|
110
|
+
* @param scope Compile-time constants in reach.
|
|
111
|
+
* @param objects Takes the objects.
|
|
112
|
+
*/
|
|
113
|
+
function collectObjects(node, scope, objects) {
|
|
114
|
+
if (node.type === 'ObjectExpression') {
|
|
115
|
+
objects.push(writtenObjectOf(node, scope));
|
|
116
|
+
}
|
|
117
|
+
else if (node.type === 'ConditionalExpression') {
|
|
118
|
+
collectObjects(node.consequent, scope, objects);
|
|
119
|
+
collectObjects(node.alternate, scope, objects);
|
|
120
|
+
}
|
|
121
|
+
else if (node.type === 'ArrowFunctionExpression' ||
|
|
122
|
+
node.type === 'FunctionExpression' ||
|
|
123
|
+
node.type === 'FunctionDeclaration') {
|
|
124
|
+
const values = node.body.type === 'BlockStatement'
|
|
125
|
+
? returnedValues(node.body)
|
|
126
|
+
: [node.body];
|
|
127
|
+
values.forEach(value => collectObjects(value, scope, objects));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The objects a reducer returns.
|
|
132
|
+
*
|
|
133
|
+
* @param reducer The reducer.
|
|
134
|
+
* @param scope Compile-time constants in reach.
|
|
135
|
+
*
|
|
136
|
+
* @returns The objects.
|
|
137
|
+
*/
|
|
138
|
+
export function returnedObjects(reducer, scope) {
|
|
139
|
+
const objects = [];
|
|
140
|
+
collectObjects(reducer, scope, objects);
|
|
141
|
+
return objects;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The objects a setter is called with anywhere in a component: given directly, as a side of a ternary, or by an
|
|
145
|
+
* updater.
|
|
146
|
+
*
|
|
147
|
+
* @param body The component's body.
|
|
148
|
+
* @param setter The setter's name.
|
|
149
|
+
* @param scope Compile-time constants in reach.
|
|
150
|
+
*
|
|
151
|
+
* @returns The objects.
|
|
152
|
+
*/
|
|
153
|
+
export function writtenObjects(body, setter, scope) {
|
|
154
|
+
const objects = [];
|
|
155
|
+
const walk = (node) => {
|
|
156
|
+
if (Array.isArray(node)) {
|
|
157
|
+
node.forEach(walk);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (typeof node !== 'object' || node === null)
|
|
161
|
+
return;
|
|
162
|
+
const astNode = node;
|
|
163
|
+
if (astNode.type === 'CallExpression' &&
|
|
164
|
+
astNode.callee.type === 'Identifier' &&
|
|
165
|
+
astNode.callee.name === setter &&
|
|
166
|
+
astNode.arguments[0]) {
|
|
167
|
+
collectObjects(astNode.arguments[0], scope, objects);
|
|
168
|
+
}
|
|
169
|
+
for (const [key, child] of Object.entries(astNode)) {
|
|
170
|
+
if (!SKIP_KEYS.has(key))
|
|
171
|
+
walk(child);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
walk(body);
|
|
175
|
+
return objects;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The shapes an object state takes, when it takes more than one: the initial value and each object written out in
|
|
179
|
+
* full, told apart by a field that holds a constant in each, the first such field of the initial value.
|
|
180
|
+
*
|
|
181
|
+
* @param name The state's name.
|
|
182
|
+
* @param setter The setter's name.
|
|
183
|
+
* @param initial The initial value's fields.
|
|
184
|
+
* @param written The objects the setter is given.
|
|
185
|
+
* @param write How a write of an object is written, for errors: by default a call to the setter.
|
|
186
|
+
*
|
|
187
|
+
* @returns The shapes, or null when the state has one shape.
|
|
188
|
+
*/
|
|
189
|
+
export function recordShapesOf(name, setter, initial, written, write = object => `${setter}(${object})`) {
|
|
190
|
+
// A write that spreads the old value keeps its shape, so one that changes a constant of the initial value and adds
|
|
191
|
+
// a field no shape has is a new shape written as a spread.
|
|
192
|
+
const full = written.filter(object => !object.spread);
|
|
193
|
+
const known = new Set([
|
|
194
|
+
...initial.keys(),
|
|
195
|
+
...full.flatMap(object => [...object.fields.keys()]),
|
|
196
|
+
]);
|
|
197
|
+
for (const object of written) {
|
|
198
|
+
if (!object.spread)
|
|
199
|
+
continue;
|
|
200
|
+
const added = [...object.fields.keys()].find(key => !known.has(key));
|
|
201
|
+
const retagged = [...object.fields].find(([key, field]) => field.known &&
|
|
202
|
+
isTagValue(initial.get(key)) &&
|
|
203
|
+
field.value !== initial.get(key));
|
|
204
|
+
if (!added || !retagged)
|
|
205
|
+
continue;
|
|
206
|
+
const [tagKey, { value }] = retagged;
|
|
207
|
+
const error = aotError(`AOT: a write that spreads "${name}" keeps its shape, so it can't add "${added}"`, `to give ${name} another shape, write the new shape out in full: ` +
|
|
208
|
+
`${write(`{${tagKey}: ${typeof value === 'string' ? `'${value}'` : String(value)}, ${added}: …}`)}.`);
|
|
209
|
+
if (object.node.loc)
|
|
210
|
+
error.aotLoc = object.node.loc.start;
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
const keysDiffer = (object) => [...object.fields.keys()].some(key => !initial.has(key)) ||
|
|
214
|
+
(!object.spread && object.fields.size !== initial.size);
|
|
215
|
+
if (!full.length || !written.some(keysDiffer))
|
|
216
|
+
return null;
|
|
217
|
+
for (const [key, initialValue] of initial) {
|
|
218
|
+
if (key.includes('.') || !isTagValue(initialValue))
|
|
219
|
+
continue;
|
|
220
|
+
const values = [initialValue];
|
|
221
|
+
const shapes = [
|
|
222
|
+
new Set([...initial.keys()].filter(field => field !== key)),
|
|
223
|
+
];
|
|
224
|
+
const agrees = full.every(object => {
|
|
225
|
+
const tag = object.fields.get(key);
|
|
226
|
+
if (!tag?.known || typeof tag.value !== typeof initialValue)
|
|
227
|
+
return false;
|
|
228
|
+
if (!isTagValue(tag.value))
|
|
229
|
+
return false;
|
|
230
|
+
const fields = new Set([...object.fields.keys()].filter(field => field !== key));
|
|
231
|
+
const shape = values.indexOf(tag.value);
|
|
232
|
+
if (shape < 0) {
|
|
233
|
+
values.push(tag.value);
|
|
234
|
+
shapes.push(fields);
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
return (shapes[shape].size === fields.size &&
|
|
238
|
+
[...fields].every(field => shapes[shape].has(field)));
|
|
239
|
+
});
|
|
240
|
+
if (agrees && values.length > 1)
|
|
241
|
+
return { key, values, shapes };
|
|
242
|
+
}
|
|
243
|
+
throw aotError(`AOT: state "${name}" is written with objects of different fields`, `an object of several shapes needs a field naming the shape, with a constant written in each: ` +
|
|
244
|
+
`${write("{kind: 'busy', pct: 0}")}. Write each new shape out in full; a write that keeps the shape ` +
|
|
245
|
+
`spreads the old value: ${write(`{...${name}, pct: 1}`)}.`);
|
|
246
|
+
}
|