embedded-react 0.19.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 +8 -3
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +180 -90
- package/aot/compiler/animations.mjs +568 -317
- package/aot/compiler/boot.mjs +97 -35
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +70 -18
- package/aot/compiler/collect.mjs +787 -288
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +439 -268
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +52 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +61 -26
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +1685 -392
- package/aot/compiler/handlers.mjs +2029 -547
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +43 -4
- package/aot/compiler/ir/reads.mjs +223 -22
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +26 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard.mjs +17 -7
- 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 +246 -111
- 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 +195 -172
- package/aot/compiler/options.mjs +37 -31
- package/aot/compiler/out.mjs +32 -23
- package/aot/compiler/package/animations.mjs +779 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +175 -0
- package/aot/compiler/package/bytecode.mjs +315 -0
- package/aot/compiler/package/checks.mjs +1026 -0
- package/aot/compiler/package/codegen.mjs +2168 -0
- package/aot/compiler/package/enums.mjs +148 -0
- package/aot/compiler/package/format.mjs +771 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +2103 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +127 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +686 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +23 -21
- 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 +340 -30
- package/aot/compiler/style-text.mjs +178 -174
- package/aot/compiler/svg.mjs +480 -251
- 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/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +152 -92
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +11 -7
- package/assets/emit-c.mjs +77 -58
- package/assets/emit-container.mjs +123 -37
- package/assets/emit-pack.mjs +77 -67
- package/assets/font-config.mjs +9 -6
- package/assets/font-sizes.mjs +163 -123
- package/assets/glyph-coverage.mjs +74 -54
- package/assets/index.mjs +31 -9
- package/assets/rasterize.mjs +126 -99
- package/assets/svg-loader.mjs +8 -8
- package/build.mjs +23 -13
- package/cli.mjs +402 -172
- package/compile-bin.mjs +3 -3
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +62 -64
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +50 -37
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +37 -21
- package/package.json +7 -8
- package/persist-transform.mjs +34 -28
- package/qjsc-wasm.mjs +25 -14
- package/sim/embedded-react.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +94 -85
- package/sim-server.mjs +113 -94
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Dimensions.js +37 -0
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +3 -3
- package/src/embedded-react/LayoutAnimation.js +8 -5
- package/src/embedded-react/PanResponder.js +75 -71
- package/src/embedded-react/Pressable.js +2 -8
- package/src/embedded-react/SectionList.js +7 -9
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +88 -8
- package/src/embedded-react/index.js +2 -0
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/nativeModule.js +462 -0
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
|
@@ -0,0 +1,654 @@
|
|
|
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 { holdLater, readStateOf, shapeOf, typedLater } from './typing.mjs';
|
|
20
|
+
import { walkedEntries, walkedKind } from './typing.mjs';
|
|
21
|
+
import { aotError } from './diagnostics.mjs';
|
|
22
|
+
import { HiddenField, RuntimeLeaf, evalStatic, foldScope, rethrowFoldLimit, } from './static-eval.mjs';
|
|
23
|
+
import { isTagOf, narrowOn, narrowed, shapesOf, tagText, } from './tagged-records.mjs';
|
|
24
|
+
import { STRING, typeOfSlotValue } from './ir/types.mjs';
|
|
25
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
26
|
+
- Classes
|
|
27
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
28
|
+
/**
|
|
29
|
+
* A field of an object written in code, not yet lowered.
|
|
30
|
+
*
|
|
31
|
+
* @class AstLeaf
|
|
32
|
+
*/
|
|
33
|
+
export class AstLeaf {
|
|
34
|
+
node;
|
|
35
|
+
/** Locals the node reads that the environment lacks: runtime values an object held. */
|
|
36
|
+
locals;
|
|
37
|
+
/**
|
|
38
|
+
* @param node The field's value.
|
|
39
|
+
* @param locals Locals it reads that the environment lacks.
|
|
40
|
+
*/
|
|
41
|
+
constructor(node, locals = new Map()) {
|
|
42
|
+
this.node = node;
|
|
43
|
+
this.locals = locals;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
47
|
+
- Implementation
|
|
48
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
49
|
+
/** Counts the names lowering makes up, which a `$` keeps apart from any JS name. */
|
|
50
|
+
let syntheticCount = 0;
|
|
51
|
+
/**
|
|
52
|
+
* Whether a value is an object of fields, not a leaf such as a RuntimeLeaf.
|
|
53
|
+
*
|
|
54
|
+
* @param value The value.
|
|
55
|
+
*
|
|
56
|
+
* @returns Whether it is.
|
|
57
|
+
*/
|
|
58
|
+
export function isObjectTree(value) {
|
|
59
|
+
if (typeof value !== 'object' || value === null)
|
|
60
|
+
return false;
|
|
61
|
+
const prototype = Object.getPrototypeOf(value);
|
|
62
|
+
return prototype === Object.prototype || prototype === null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The key an object property is written with.
|
|
66
|
+
*
|
|
67
|
+
* @param property The property.
|
|
68
|
+
*
|
|
69
|
+
* @returns The key.
|
|
70
|
+
*/
|
|
71
|
+
export function propertyKey(property) {
|
|
72
|
+
const { key } = property;
|
|
73
|
+
if (!property.computed && key.type === 'Identifier')
|
|
74
|
+
return key.name;
|
|
75
|
+
if (key.type === 'StringLiteral')
|
|
76
|
+
return key.value;
|
|
77
|
+
if (key.type === 'NumericLiteral')
|
|
78
|
+
return String(key.value);
|
|
79
|
+
throw aotError('AOT: an object field needs a fixed name', 'write the key out: {label: …}. Computed keys are not lowered.');
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The fields of an object an expression names: an object literal (spreads too), or a name or member that folds to
|
|
83
|
+
* an object (an object state, an object built in render or in the handler, a constant).
|
|
84
|
+
*
|
|
85
|
+
* @param node The expression.
|
|
86
|
+
* @param environment The expression environment.
|
|
87
|
+
*
|
|
88
|
+
* @returns Its fields, or null when it names no object.
|
|
89
|
+
*/
|
|
90
|
+
export function objectTreeOf(node, environment) {
|
|
91
|
+
if (node.type === 'TSAsExpression' ||
|
|
92
|
+
node.type === 'TSSatisfiesExpression' ||
|
|
93
|
+
node.type === 'TSNonNullExpression') {
|
|
94
|
+
return objectTreeOf(node.expression, environment);
|
|
95
|
+
}
|
|
96
|
+
if (node.type === 'ObjectExpression') {
|
|
97
|
+
const tree = {};
|
|
98
|
+
for (const property of node.properties) {
|
|
99
|
+
if (property.type === 'SpreadElement') {
|
|
100
|
+
const spread = objectTreeOf(property.argument, environment);
|
|
101
|
+
if (!spread) {
|
|
102
|
+
throw aotError('AOT: only an object can be spread into an object', 'spread an object state or an object built from fields: {...p, x: 1}.');
|
|
103
|
+
}
|
|
104
|
+
Object.assign(tree, spread);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (property.type !== 'ObjectProperty') {
|
|
108
|
+
throw aotError('AOT: an object can only hold plain fields and spreads', 'write each field out: {label: "A", value: n}. Methods are not lowered.');
|
|
109
|
+
}
|
|
110
|
+
tree[propertyKey(property)] =
|
|
111
|
+
objectTreeOf(property.value, environment) ??
|
|
112
|
+
new AstLeaf(property.value);
|
|
113
|
+
}
|
|
114
|
+
return tree;
|
|
115
|
+
}
|
|
116
|
+
// `cond ? a : b` of two objects with the same fields: each field picks its side.
|
|
117
|
+
if (node.type === 'ConditionalExpression') {
|
|
118
|
+
const narrowing = narrowOn(node.test, environment);
|
|
119
|
+
const consequent = objectTreeOf(node.consequent, narrowed(environment, narrowing.whenTrue));
|
|
120
|
+
const alternate = objectTreeOf(node.alternate, narrowed(environment, narrowing.whenFalse));
|
|
121
|
+
if (!consequent || !alternate)
|
|
122
|
+
return null;
|
|
123
|
+
const sides = [flattenTree(consequent), flattenTree(alternate)];
|
|
124
|
+
const keys = [...sides[0].keys()];
|
|
125
|
+
if (keys.length !== sides[1].size || keys.some(key => !sides[1].has(key))) {
|
|
126
|
+
throw aotError('AOT: both sides of a ternary must be objects with the same fields', 'give each side every field: cond ? {...p, x: 1} : {...p, x: 2}.');
|
|
127
|
+
}
|
|
128
|
+
const tree = {};
|
|
129
|
+
for (const key of keys) {
|
|
130
|
+
const [consequentLeaf, alternateLeaf] = sides.map(side => leafNode(side.get(key), environment));
|
|
131
|
+
const added = new Map([
|
|
132
|
+
...consequentLeaf.environment.locals,
|
|
133
|
+
...alternateLeaf.environment.locals,
|
|
134
|
+
].filter(([name]) => !environment.locals.has(name)));
|
|
135
|
+
let branch = tree;
|
|
136
|
+
const path = key.split('.');
|
|
137
|
+
for (const part of path.slice(0, -1)) {
|
|
138
|
+
branch = (branch[part] ??= {});
|
|
139
|
+
}
|
|
140
|
+
branch[path.at(-1)] = new AstLeaf({
|
|
141
|
+
type: 'ConditionalExpression',
|
|
142
|
+
test: node.test,
|
|
143
|
+
consequent: consequentLeaf.node,
|
|
144
|
+
alternate: alternateLeaf.node,
|
|
145
|
+
}, added);
|
|
146
|
+
}
|
|
147
|
+
return tree;
|
|
148
|
+
}
|
|
149
|
+
if (node.type !== 'Identifier' &&
|
|
150
|
+
node.type !== 'MemberExpression' &&
|
|
151
|
+
node.type !== 'OptionalMemberExpression') {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const value = evalStatic(node, foldScope(environment, environment.constants ?? {}), 'boot');
|
|
156
|
+
return isObjectTree(value) ? value : null;
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
rethrowFoldLimit(error);
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* An object's fields by their paths joined with dots.
|
|
165
|
+
*
|
|
166
|
+
* @param tree The object.
|
|
167
|
+
*
|
|
168
|
+
* @returns Its fields.
|
|
169
|
+
*/
|
|
170
|
+
export function flattenTree(tree) {
|
|
171
|
+
const fields = new Map();
|
|
172
|
+
const walk = (object, path) => {
|
|
173
|
+
for (const [key, value] of Object.entries(object)) {
|
|
174
|
+
if (key === '' || key.includes('.')) {
|
|
175
|
+
throw aotError(`AOT: an object has a field named "${key}"`, 'name each field with letters, digits or `_`.');
|
|
176
|
+
}
|
|
177
|
+
if (isObjectTree(value)) {
|
|
178
|
+
walk(value, [...path, key]);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
fields.set([...path, key].join('.'), value);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
walk(tree, []);
|
|
186
|
+
return fields;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* An expression for one field of an object, and the environment it lowers in: what was written, a local that stands
|
|
190
|
+
* for a runtime value, or a literal for a constant.
|
|
191
|
+
*
|
|
192
|
+
* @param value The field.
|
|
193
|
+
* @param environment The expression environment.
|
|
194
|
+
*
|
|
195
|
+
* @returns The expression and its environment.
|
|
196
|
+
*/
|
|
197
|
+
export function leafNode(value, environment) {
|
|
198
|
+
if (value instanceof AstLeaf) {
|
|
199
|
+
return {
|
|
200
|
+
node: value.node,
|
|
201
|
+
environment: value.locals.size
|
|
202
|
+
? {
|
|
203
|
+
...environment,
|
|
204
|
+
locals: new Map([...environment.locals, ...value.locals]),
|
|
205
|
+
}
|
|
206
|
+
: environment,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
if (value instanceof HiddenField)
|
|
210
|
+
throw value.error();
|
|
211
|
+
if (value instanceof RuntimeLeaf) {
|
|
212
|
+
const name = `field$${syntheticCount++}`;
|
|
213
|
+
return {
|
|
214
|
+
node: { type: 'Identifier', name },
|
|
215
|
+
environment: {
|
|
216
|
+
...environment,
|
|
217
|
+
locals: new Map(environment.locals).set(name, value.local),
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (typeof value === 'number') {
|
|
222
|
+
return {
|
|
223
|
+
node: value < 0 || Object.is(value, -0)
|
|
224
|
+
? {
|
|
225
|
+
type: 'UnaryExpression',
|
|
226
|
+
operator: '-',
|
|
227
|
+
prefix: true,
|
|
228
|
+
argument: { type: 'NumericLiteral', value: -value },
|
|
229
|
+
}
|
|
230
|
+
: { type: 'NumericLiteral', value },
|
|
231
|
+
environment,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (typeof value === 'string') {
|
|
235
|
+
return {
|
|
236
|
+
node: { type: 'StringLiteral', value },
|
|
237
|
+
environment,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
if (typeof value === 'boolean') {
|
|
241
|
+
return {
|
|
242
|
+
node: { type: 'BooleanLiteral', value },
|
|
243
|
+
environment,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
throw aotError(`AOT: a field holding ${value === undefined ? 'undefined' : value === null ? 'null' : 'a ' + typeof value} cannot be stored`, "give each field a number, a string or a boolean: 0, '' or false.");
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* The field a member of an object names, `p.pos.x`, as the object holds it.
|
|
250
|
+
*
|
|
251
|
+
* @param node The expression.
|
|
252
|
+
* @param environment The expression environment.
|
|
253
|
+
*
|
|
254
|
+
* @returns The field, or undefined when it names none.
|
|
255
|
+
*/
|
|
256
|
+
function fieldRead(node, environment) {
|
|
257
|
+
if (node.type !== 'MemberExpression')
|
|
258
|
+
return undefined;
|
|
259
|
+
const key = !node.computed
|
|
260
|
+
? node.property.name
|
|
261
|
+
: node.property.type === 'StringLiteral'
|
|
262
|
+
? node.property.value
|
|
263
|
+
: null;
|
|
264
|
+
const object = objectTreeOf(node.object, environment);
|
|
265
|
+
return object && key !== null ? object[key] : undefined;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Whether a lowered value reads any of a set of states.
|
|
269
|
+
*
|
|
270
|
+
* @param value The value: IR, at any depth.
|
|
271
|
+
* @param states The states.
|
|
272
|
+
*
|
|
273
|
+
* @returns Whether it does.
|
|
274
|
+
*/
|
|
275
|
+
function readsAny(value, states) {
|
|
276
|
+
const seen = new Set();
|
|
277
|
+
const walk = (part) => {
|
|
278
|
+
if (typeof part !== 'object' || part === null || seen.has(part)) {
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
seen.add(part);
|
|
282
|
+
const record = part;
|
|
283
|
+
if (walkedKind(record) === 'state' &&
|
|
284
|
+
states.has(record.state)) {
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
return walkedEntries(record).some(([, field]) => walk(field));
|
|
288
|
+
};
|
|
289
|
+
return walk(value);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* The fields of the object a setter is given.
|
|
293
|
+
*
|
|
294
|
+
* @param record The object state.
|
|
295
|
+
* @param valueNode The new object.
|
|
296
|
+
* @param environment The expression environment.
|
|
297
|
+
*
|
|
298
|
+
* @returns Its fields.
|
|
299
|
+
*/
|
|
300
|
+
function givenLeaves(record, valueNode, environment) {
|
|
301
|
+
const setter = record.setter ?? `set ${record.name}`;
|
|
302
|
+
const tree = valueNode ? objectTreeOf(valueNode, environment) : null;
|
|
303
|
+
if (!tree) {
|
|
304
|
+
throw aotError(`AOT: ${setter} needs an object with the fields of "${record.name}"`, `pass an object: ${setter}({...${record.name}, field: value}).`);
|
|
305
|
+
}
|
|
306
|
+
const leaves = flattenTree(tree);
|
|
307
|
+
for (const key of leaves.keys()) {
|
|
308
|
+
if (!record.fields.has(key) && key !== record.tag?.key) {
|
|
309
|
+
throw aotError(`AOT: state "${record.name}" has no field "${key}"`, 'an object state holds the fields it starts with; give the field a starting value in useState.');
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return leaves;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Lowers a write to an object state: every field set from the new object, a field left as it is skipped. A value
|
|
316
|
+
* that reads a field written before it is worked out first, so `{x: p.y, y: p.x}` swaps.
|
|
317
|
+
*
|
|
318
|
+
* @param record The object state.
|
|
319
|
+
* @param valueNode The new object.
|
|
320
|
+
* @param environment The expression environment.
|
|
321
|
+
* @param lowering How its writes are lowered.
|
|
322
|
+
*
|
|
323
|
+
* @returns The writes.
|
|
324
|
+
*/
|
|
325
|
+
export function lowerRecordSet(record, valueNode, environment, lowering) {
|
|
326
|
+
if (record.tag) {
|
|
327
|
+
return lowerTaggedSet(record, valueNode, environment, lowering);
|
|
328
|
+
}
|
|
329
|
+
const setter = record.setter ?? `set ${record.name}`;
|
|
330
|
+
const leaves = givenLeaves(record, valueNode, environment);
|
|
331
|
+
for (const key of record.fields.keys()) {
|
|
332
|
+
if (!leaves.has(key)) {
|
|
333
|
+
throw aotError(`AOT: ${setter} leaves out "${record.name}.${key}"`, `an object state keeps every field it starts with; spread the old value: ${setter}({...${record.name}, …}).`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return writeFields([...record.fields].map(([key, field]) => [field, leaves.get(key)]), environment, lowering.field);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* A field's value when it folds to a constant.
|
|
340
|
+
*
|
|
341
|
+
* @param value The field.
|
|
342
|
+
* @param environment The expression environment.
|
|
343
|
+
*
|
|
344
|
+
* @returns The constant, or the field as it is.
|
|
345
|
+
*/
|
|
346
|
+
function constantOf(value, environment) {
|
|
347
|
+
if (!(value instanceof AstLeaf) || value.locals.size)
|
|
348
|
+
return value;
|
|
349
|
+
try {
|
|
350
|
+
return evalStatic(value.node, foldScope(environment, environment.constants ?? {}));
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
rethrowFoldLimit(error);
|
|
354
|
+
return value;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Lowers a write to a tagged object state. An object with its tag written out sets that shape: the hidden tag, and
|
|
359
|
+
* each of the shape's fields. One that keeps the tag (a spread of the state) keeps the shape. A ternary sets the
|
|
360
|
+
* shape its test picks.
|
|
361
|
+
*
|
|
362
|
+
* @param record The object state.
|
|
363
|
+
* @param valueNode The new object.
|
|
364
|
+
* @param environment The expression environment.
|
|
365
|
+
* @param lowering How its writes are lowered.
|
|
366
|
+
*
|
|
367
|
+
* @returns The writes.
|
|
368
|
+
*/
|
|
369
|
+
function lowerTaggedSet(record, valueNode, environment, lowering) {
|
|
370
|
+
const { name, tag } = record;
|
|
371
|
+
const setter = record.setter ?? `set ${name}`;
|
|
372
|
+
if (valueNode?.type === 'ConditionalExpression') {
|
|
373
|
+
const narrowing = narrowOn(valueNode.test, environment);
|
|
374
|
+
const consequent = lowerTaggedSet(record, valueNode.consequent, narrowed(environment, narrowing.whenTrue), lowering);
|
|
375
|
+
const alternate = lowerTaggedSet(record, valueNode.alternate, narrowed(environment, narrowing.whenFalse), lowering);
|
|
376
|
+
if (!consequent.length && !alternate.length)
|
|
377
|
+
return [];
|
|
378
|
+
return [
|
|
379
|
+
{
|
|
380
|
+
kind: 'if',
|
|
381
|
+
test: lowering.test(valueNode.test, environment),
|
|
382
|
+
consequent,
|
|
383
|
+
alternate: alternate.length ? alternate : null,
|
|
384
|
+
},
|
|
385
|
+
];
|
|
386
|
+
}
|
|
387
|
+
const leaves = givenLeaves(record, valueNode, environment);
|
|
388
|
+
const leavesOut = (key, hint) => aotError(`AOT: ${setter} leaves out "${name}.${key}"`, hint);
|
|
389
|
+
const tagValue = constantOf(leaves.get(tag.key), environment);
|
|
390
|
+
// A spread of the state keeps its shape.
|
|
391
|
+
if (isTagOf(tagValue, record)) {
|
|
392
|
+
for (const key of record.fields.keys()) {
|
|
393
|
+
if (!leaves.has(key)) {
|
|
394
|
+
throw leavesOut(key, `spread the old value to keep its shape: ${setter}({...${name}, …}).`);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return [
|
|
398
|
+
{ kind: 'keepField', state: tag.state },
|
|
399
|
+
...writeFields([...record.fields].map(([key, field]) => [field, leaves.get(key)]), environment, lowering.field),
|
|
400
|
+
];
|
|
401
|
+
}
|
|
402
|
+
if (typeof tagValue !== 'string' &&
|
|
403
|
+
typeof tagValue !== 'number' &&
|
|
404
|
+
typeof tagValue !== 'boolean') {
|
|
405
|
+
throw aotError(`AOT: ${setter} needs to know which shape "${name}" takes`, `write the tag out, ${setter}({${tag.key}: ${tagText(tag.values[0])}, …}), or test ${name}.${tag.key} first.`);
|
|
406
|
+
}
|
|
407
|
+
const shape = tag.values.indexOf(tagValue);
|
|
408
|
+
if (shape < 0) {
|
|
409
|
+
throw aotError(`AOT: state "${name}" has no shape with ${tag.key} ${tagText(tagValue)}`, `its shapes are its initial value and the objects ${setter} is given in full; ` +
|
|
410
|
+
`write this one out in full once: ${setter}({${tag.key}: ${tagText(tagValue)}, …}).`);
|
|
411
|
+
}
|
|
412
|
+
const fields = tag.shapes[shape];
|
|
413
|
+
for (const key of fields) {
|
|
414
|
+
if (!leaves.has(key) || leaves.get(key) instanceof HiddenField) {
|
|
415
|
+
throw leavesOut(key, `give every field of the ${tagText(tagValue)} shape a value.`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
for (const [key, value] of leaves) {
|
|
419
|
+
if (key === tag.key || fields.has(key) || value instanceof HiddenField) {
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
throw aotError(`AOT: the ${tagText(tagValue)} shape of "${name}" has no field "${key}"`, `write the shape out with its own fields: ${setter}({${tag.key}: ${tagText(tagValue)}, …}).`);
|
|
423
|
+
}
|
|
424
|
+
// The tag is written unless the state can only have that shape here: then it is kept.
|
|
425
|
+
const here = environment.locals.has(name)
|
|
426
|
+
? null
|
|
427
|
+
: shapesOf(environment.constants?.[name]);
|
|
428
|
+
const keepsTag = here?.length === 1 && here[0] === shape;
|
|
429
|
+
const entries = keepsTag
|
|
430
|
+
? []
|
|
431
|
+
: [[tag.state, shape]];
|
|
432
|
+
for (const [key, field] of record.fields) {
|
|
433
|
+
if (fields.has(key))
|
|
434
|
+
entries.push([field, leaves.get(key)]);
|
|
435
|
+
}
|
|
436
|
+
const writes = writeFields(entries, environment, lowering.field);
|
|
437
|
+
return keepsTag ? [{ kind: 'keepField', state: tag.state }, ...writes] : writes;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Lowers the writes of values to fields: a field set to itself is kept, and a value that reads a field written
|
|
441
|
+
* before it is worked out first.
|
|
442
|
+
*
|
|
443
|
+
* @param entries Each field and its value.
|
|
444
|
+
* @param environment The expression environment.
|
|
445
|
+
* @param lowerField Lowers a write of an expression to one field.
|
|
446
|
+
*
|
|
447
|
+
* @returns The writes.
|
|
448
|
+
*/
|
|
449
|
+
function writeFields(entries, environment, lowerField) {
|
|
450
|
+
// A field set to itself keeps its value.
|
|
451
|
+
const writes = [];
|
|
452
|
+
for (const [field, value] of entries) {
|
|
453
|
+
if (value instanceof HiddenField && value.field === field) {
|
|
454
|
+
writes.push({ field, statement: { kind: 'keepField', state: field } });
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
const read = value instanceof AstLeaf && !value.locals.size
|
|
458
|
+
? fieldRead(value.node, environment)
|
|
459
|
+
: value;
|
|
460
|
+
const expression = read instanceof RuntimeLeaf ? read.local.expression : undefined;
|
|
461
|
+
if (expression && readStateOf(expression) === field) {
|
|
462
|
+
writes.push({ field, statement: { kind: 'keepField', state: field } });
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
const leaf = leafNode(value, environment);
|
|
466
|
+
writes.push({
|
|
467
|
+
field,
|
|
468
|
+
statement: lowerField(field, leaf.node, leaf.environment),
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
// Stage each value that reads a field written before it.
|
|
472
|
+
const staged = [];
|
|
473
|
+
const written = new Set();
|
|
474
|
+
for (const write of writes) {
|
|
475
|
+
const { statement, field } = write;
|
|
476
|
+
if (statement.kind === 'keepField')
|
|
477
|
+
continue;
|
|
478
|
+
if (written.size && readsAny(statement, written)) {
|
|
479
|
+
const name = `local_${field.slotName}`;
|
|
480
|
+
if (statement.kind === 'setState') {
|
|
481
|
+
staged.push({
|
|
482
|
+
kind: 'declareLocal',
|
|
483
|
+
name,
|
|
484
|
+
initializer: statement.value,
|
|
485
|
+
isHoisted: false,
|
|
486
|
+
});
|
|
487
|
+
const { value } = statement;
|
|
488
|
+
const local = holdLater({ name }, shapeOf(value), () => ({
|
|
489
|
+
slotKind: field.slotKind,
|
|
490
|
+
}));
|
|
491
|
+
write.statement = {
|
|
492
|
+
...statement,
|
|
493
|
+
value: typedLater(shapeOf(value), () => ({ kind: 'local', local, type: value.type }), [value]),
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
else if (statement.kind === 'setStateText') {
|
|
497
|
+
staged.push({
|
|
498
|
+
kind: 'declareLocal',
|
|
499
|
+
name,
|
|
500
|
+
initializer: { kind: 'string', value: '', type: STRING },
|
|
501
|
+
text: statement.text,
|
|
502
|
+
isHoisted: false,
|
|
503
|
+
});
|
|
504
|
+
const local = { slotKind: 'string', name };
|
|
505
|
+
write.statement = {
|
|
506
|
+
...statement,
|
|
507
|
+
text: {
|
|
508
|
+
parts: [
|
|
509
|
+
{
|
|
510
|
+
kind: 'value',
|
|
511
|
+
value: { kind: 'local', local, type: typeOfSlotValue(local) },
|
|
512
|
+
},
|
|
513
|
+
],
|
|
514
|
+
},
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
written.add(field);
|
|
519
|
+
}
|
|
520
|
+
return [...staged, ...writes.map(write => write.statement)];
|
|
521
|
+
}
|
|
522
|
+
const identifier = (name) => ({ type: 'Identifier', name });
|
|
523
|
+
const memberOf = (object, key) => ({
|
|
524
|
+
type: 'MemberExpression',
|
|
525
|
+
object,
|
|
526
|
+
property: { type: 'StringLiteral', value: key },
|
|
527
|
+
computed: true,
|
|
528
|
+
});
|
|
529
|
+
const declaratorOf = (id, init) => ({ type: 'VariableDeclarator', id, init });
|
|
530
|
+
/**
|
|
531
|
+
* The declarators an object pattern destructures into, one per name, each reading its field of `base`.
|
|
532
|
+
*
|
|
533
|
+
* @param pattern The pattern.
|
|
534
|
+
* @param base What it destructures.
|
|
535
|
+
* @param tree The object's fields.
|
|
536
|
+
*
|
|
537
|
+
* @returns The declarators.
|
|
538
|
+
*/
|
|
539
|
+
function patternDeclarators(pattern, base, tree) {
|
|
540
|
+
const declarators = [];
|
|
541
|
+
const taken = new Set();
|
|
542
|
+
for (const property of pattern.properties) {
|
|
543
|
+
if (property.type === 'RestElement') {
|
|
544
|
+
if (property.argument.type !== 'Identifier') {
|
|
545
|
+
throw aotError('AOT: a rest element must be a plain name');
|
|
546
|
+
}
|
|
547
|
+
const rest = Object.keys(tree).filter(key => !taken.has(key));
|
|
548
|
+
declarators.push(declaratorOf(property.argument, {
|
|
549
|
+
type: 'ObjectExpression',
|
|
550
|
+
properties: rest.map(key => ({
|
|
551
|
+
type: 'ObjectProperty',
|
|
552
|
+
key: { type: 'StringLiteral', value: key },
|
|
553
|
+
value: memberOf(base, key),
|
|
554
|
+
computed: false,
|
|
555
|
+
shorthand: false,
|
|
556
|
+
})),
|
|
557
|
+
}));
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
const key = propertyKey(property);
|
|
561
|
+
taken.add(key);
|
|
562
|
+
if (!Object.hasOwn(tree, key)) {
|
|
563
|
+
throw aotError(`AOT: the object has no field "${key}" to destructure`, 'destructure only the fields the object holds.');
|
|
564
|
+
}
|
|
565
|
+
const { value } = property;
|
|
566
|
+
if (value.type === 'Identifier' || value.type === 'ObjectPattern') {
|
|
567
|
+
declarators.push(declaratorOf(value, memberOf(base, key)));
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
throw aotError('AOT: a destructured field must be a plain name or a nested object pattern', 'defaults never apply, since every field holds a value; drop the `= …`.');
|
|
571
|
+
}
|
|
572
|
+
return declarators;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Expands a handler's `const` of an object, or a destructuring of one, into a local per field: what JS would
|
|
576
|
+
* read from the object is worked out where the `const` is.
|
|
577
|
+
*
|
|
578
|
+
* @param declarator The declarator.
|
|
579
|
+
* @param environment The expression environment.
|
|
580
|
+
*
|
|
581
|
+
* @returns The expansion, or null when it declares no object.
|
|
582
|
+
*/
|
|
583
|
+
export function expandObjectDeclarator(declarator, environment) {
|
|
584
|
+
const { id, init } = declarator;
|
|
585
|
+
if (!init)
|
|
586
|
+
return null;
|
|
587
|
+
if (id.type === 'ObjectPattern') {
|
|
588
|
+
const tree = objectTreeOf(init, environment);
|
|
589
|
+
if (!tree) {
|
|
590
|
+
throw aotError('AOT: only an object can be destructured', 'destructure an object state, or an object built from fields.');
|
|
591
|
+
}
|
|
592
|
+
if (init.type === 'Identifier' || init.type === 'MemberExpression') {
|
|
593
|
+
return {
|
|
594
|
+
entries: patternDeclarators(id, init, tree),
|
|
595
|
+
locals: new Map(),
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
const temporary = identifier(`object$${syntheticCount++}`);
|
|
599
|
+
return {
|
|
600
|
+
entries: [
|
|
601
|
+
declaratorOf(temporary, init),
|
|
602
|
+
...patternDeclarators(id, temporary, tree),
|
|
603
|
+
],
|
|
604
|
+
locals: new Map(),
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
if (id.type !== 'Identifier')
|
|
608
|
+
return null;
|
|
609
|
+
const tree = objectTreeOf(init, environment);
|
|
610
|
+
if (!tree)
|
|
611
|
+
return null;
|
|
612
|
+
const entries = [];
|
|
613
|
+
const locals = new Map();
|
|
614
|
+
for (const [key, value] of flattenTree(tree)) {
|
|
615
|
+
const fieldName = identifier(`${id.name}.${key}`);
|
|
616
|
+
if (value instanceof AstLeaf) {
|
|
617
|
+
for (const [name, local] of value.locals)
|
|
618
|
+
locals.set(name, local);
|
|
619
|
+
entries.push(declaratorOf(fieldName, value.node));
|
|
620
|
+
}
|
|
621
|
+
else if (value instanceof RuntimeLeaf) {
|
|
622
|
+
const name = `field$${syntheticCount++}`;
|
|
623
|
+
locals.set(name, value.local);
|
|
624
|
+
entries.push(declaratorOf(fieldName, identifier(name)));
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
entries.push({ kind: 'bindObject', name: id.name, tree });
|
|
628
|
+
return { entries, locals };
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* An object whose fields are read from the locals its declaration expanded to; a constant field stays one.
|
|
632
|
+
*
|
|
633
|
+
* @param binding The object and its name.
|
|
634
|
+
* @param locals The locals in reach.
|
|
635
|
+
*
|
|
636
|
+
* @returns The object.
|
|
637
|
+
*/
|
|
638
|
+
export function boundObjectOf({ name, tree }, locals) {
|
|
639
|
+
const rebuild = (object, path) => Object.fromEntries(Object.entries(object).map(([key, value]) => {
|
|
640
|
+
const fieldPath = [...path, key];
|
|
641
|
+
if (isObjectTree(value))
|
|
642
|
+
return [key, rebuild(value, fieldPath)];
|
|
643
|
+
if (value instanceof AstLeaf || value instanceof RuntimeLeaf) {
|
|
644
|
+
const fieldName = `${name}.${fieldPath.join('.')}`;
|
|
645
|
+
const local = locals.get(fieldName);
|
|
646
|
+
if (!local) {
|
|
647
|
+
throw new Error(`AOT internal: no local for ${fieldName}`);
|
|
648
|
+
}
|
|
649
|
+
return [key, new RuntimeLeaf(local)];
|
|
650
|
+
}
|
|
651
|
+
return [key, value];
|
|
652
|
+
}));
|
|
653
|
+
return rebuild(tree, []);
|
|
654
|
+
}
|