embedded-react 0.20.0 → 0.22.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 +3 -2
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +17 -47
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +62 -10
- package/aot/compiler/collect.mjs +517 -137
- 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 +47 -8
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1378 -97
- package/aot/compiler/handlers.mjs +1807 -354
- 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/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- 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 +203 -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 +45 -31
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +18 -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 +906 -33
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +16 -3
- package/aot/compiler/package/lower.mjs +707 -176
- 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/scroll-to.mjs +112 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +20 -5
- package/aot/compiler/svg.mjs +13 -7
- 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 +321 -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 +94 -3
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +85 -94
- package/cli.mjs +490 -586
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +146 -213
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +94 -116
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/Dimensions.mjs +32 -0
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +238 -5
- package/src/embedded-react/index.js +26 -17
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -148
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
|
@@ -0,0 +1,307 @@
|
|
|
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
|
+
* What lowering holds in place of a typed value. A number's width is not known until every store into every slot
|
|
18
|
+
* has been lowered, so lowering leaves each expression untyped and the typing pass fills it in, in place. Until
|
|
19
|
+
* then lowering can ask only for a value's shape, which no width changes.
|
|
20
|
+
*/
|
|
21
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
22
|
+
- Imports
|
|
23
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
24
|
+
import { defersTyping, whenTyped } from './check.mjs';
|
|
25
|
+
/** Holds what lowering knows of an expression the typing pass has yet to fill in. */
|
|
26
|
+
const PENDING = Symbol('pending');
|
|
27
|
+
/** What a slot records about its value, which the typing pass decides for a number. */
|
|
28
|
+
const SLOT_FACETS = ['slotKind', 'isBoolean', 'isLiteral', 'wide'];
|
|
29
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
30
|
+
- Implementation
|
|
31
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
32
|
+
/**
|
|
33
|
+
* Makes reading a field fail until the typing pass sets it.
|
|
34
|
+
*
|
|
35
|
+
* @param target The object.
|
|
36
|
+
* @param field The field.
|
|
37
|
+
*/
|
|
38
|
+
function hideUntilTyped(target, field) {
|
|
39
|
+
Object.defineProperty(target, field, {
|
|
40
|
+
get() {
|
|
41
|
+
throw new Error(`AOT internal: "${field}" was read before the typing pass set it`);
|
|
42
|
+
},
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The shape of a value of a type.
|
|
49
|
+
*
|
|
50
|
+
* @param type The type.
|
|
51
|
+
*
|
|
52
|
+
* @returns The shape.
|
|
53
|
+
*/
|
|
54
|
+
export function shapeOfType(type) {
|
|
55
|
+
switch (type.kind) {
|
|
56
|
+
case 'string':
|
|
57
|
+
case 'bool':
|
|
58
|
+
case 'struct':
|
|
59
|
+
case 'node':
|
|
60
|
+
return type.kind;
|
|
61
|
+
default:
|
|
62
|
+
return 'number';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The shape of an expression's value, typed or not.
|
|
67
|
+
*
|
|
68
|
+
* @param expression The expression.
|
|
69
|
+
*
|
|
70
|
+
* @returns The shape.
|
|
71
|
+
*/
|
|
72
|
+
export function shapeOf(expression) {
|
|
73
|
+
const pending = expression[PENDING];
|
|
74
|
+
return pending?.shape ?? shapeOfType(expression.type);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* What an expression is, where that is known before it is typed: a typed expression is itself, and an untyped
|
|
78
|
+
* one whatever its source fixes it as.
|
|
79
|
+
*
|
|
80
|
+
* @param expression The expression.
|
|
81
|
+
*
|
|
82
|
+
* @returns The expression it is, or null for one the typing pass works out.
|
|
83
|
+
*/
|
|
84
|
+
export function settledOf(expression) {
|
|
85
|
+
const pending = expression[PENDING];
|
|
86
|
+
if (!pending)
|
|
87
|
+
return expression;
|
|
88
|
+
return pending.source?.kind === 'fixed'
|
|
89
|
+
? settledOf(pending.source.expression)
|
|
90
|
+
: null;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The state an expression is a bare read of, typed or not.
|
|
94
|
+
*
|
|
95
|
+
* @param expression The expression.
|
|
96
|
+
*
|
|
97
|
+
* @returns The state, or null.
|
|
98
|
+
*/
|
|
99
|
+
export function readStateOf(expression) {
|
|
100
|
+
const pending = expression[PENDING];
|
|
101
|
+
if (pending) {
|
|
102
|
+
return pending.source?.kind === 'state' ? pending.source.state : null;
|
|
103
|
+
}
|
|
104
|
+
return expression.kind === 'state' ? expression.state : null;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The local an expression is a bare read of, typed or not.
|
|
108
|
+
*
|
|
109
|
+
* @param expression The expression.
|
|
110
|
+
*
|
|
111
|
+
* @returns The local, or null.
|
|
112
|
+
*/
|
|
113
|
+
export function readLocalOf(expression) {
|
|
114
|
+
const pending = expression[PENDING];
|
|
115
|
+
if (pending) {
|
|
116
|
+
return pending.source?.kind === 'local' ? pending.source.local : null;
|
|
117
|
+
}
|
|
118
|
+
return expression.kind === 'local' ? expression.local : null;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* An expression the typing pass works out. Outside a compile it is worked out at once.
|
|
122
|
+
*
|
|
123
|
+
* @param shape Its value's shape.
|
|
124
|
+
* @param build Works it out, once every type it reads is known.
|
|
125
|
+
* @param from What it is typed from: one source expression, or the expressions it is worked out from.
|
|
126
|
+
*
|
|
127
|
+
* @returns The expression, which lowering can place but not read.
|
|
128
|
+
*/
|
|
129
|
+
export function typedLater(shape, build, from) {
|
|
130
|
+
if (!defersTyping())
|
|
131
|
+
return build();
|
|
132
|
+
const pending = {};
|
|
133
|
+
Object.defineProperty(pending, PENDING, {
|
|
134
|
+
value: (Array.isArray(from)
|
|
135
|
+
? { shape, from }
|
|
136
|
+
: { shape, source: from }),
|
|
137
|
+
configurable: true,
|
|
138
|
+
});
|
|
139
|
+
hideUntilTyped(pending, 'kind');
|
|
140
|
+
hideUntilTyped(pending, 'type');
|
|
141
|
+
whenTyped(() => {
|
|
142
|
+
const typed = build();
|
|
143
|
+
const fields = pending;
|
|
144
|
+
delete fields[PENDING];
|
|
145
|
+
delete fields.kind;
|
|
146
|
+
delete fields.type;
|
|
147
|
+
Object.assign(pending, typed);
|
|
148
|
+
});
|
|
149
|
+
return pending;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The fields a walk over lowered values goes on into. An expression the typing pass has yet to fill in is
|
|
153
|
+
* walked as what it is typed from, which reads at least what it will; a field the pass has yet to set is left
|
|
154
|
+
* out.
|
|
155
|
+
*
|
|
156
|
+
* @param value The object.
|
|
157
|
+
*
|
|
158
|
+
* @returns Each field's name and value.
|
|
159
|
+
*/
|
|
160
|
+
export function walkedEntries(value) {
|
|
161
|
+
const pending = value[PENDING];
|
|
162
|
+
if (pending) {
|
|
163
|
+
return [
|
|
164
|
+
['typedFrom', pending.source],
|
|
165
|
+
['workedFrom', pending.from],
|
|
166
|
+
];
|
|
167
|
+
}
|
|
168
|
+
return Object.entries(Object.getOwnPropertyDescriptors(value)).flatMap(([name, descriptor]) => descriptor.enumerable && 'value' in descriptor
|
|
169
|
+
? [[name, descriptor.value]]
|
|
170
|
+
: []);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The kind of a lowered value a walk is on, if it has one yet.
|
|
174
|
+
*
|
|
175
|
+
* @param value The object.
|
|
176
|
+
*
|
|
177
|
+
* @returns Its `kind`, or undefined for an expression the typing pass has yet to fill in.
|
|
178
|
+
*/
|
|
179
|
+
export const walkedKind = (value) => value[PENDING]
|
|
180
|
+
? undefined
|
|
181
|
+
: value.kind;
|
|
182
|
+
/**
|
|
183
|
+
* Has the typing pass set fields of a statement or a declaration. Until then they cannot be read, so a copy
|
|
184
|
+
* taken before the pass fails instead of going without them.
|
|
185
|
+
*
|
|
186
|
+
* @param target The object.
|
|
187
|
+
* @param names The fields.
|
|
188
|
+
* @param values Works out the fields; one left undefined is left out.
|
|
189
|
+
*/
|
|
190
|
+
export function setLater(target, names, values) {
|
|
191
|
+
if (defersTyping()) {
|
|
192
|
+
names.forEach(name => hideUntilTyped(target, name));
|
|
193
|
+
}
|
|
194
|
+
whenTyped(() => {
|
|
195
|
+
const typed = values();
|
|
196
|
+
for (const name of names) {
|
|
197
|
+
delete target[name];
|
|
198
|
+
if (typed[name] !== undefined) {
|
|
199
|
+
target[name] = typed[name];
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Whether the typing pass has yet to say what a slot holds.
|
|
206
|
+
*
|
|
207
|
+
* @param slot The slot's record: a local, or a state.
|
|
208
|
+
*
|
|
209
|
+
* @returns True until its kind is set.
|
|
210
|
+
*/
|
|
211
|
+
export const holdsUntyped = (slot) => Object.getOwnPropertyDescriptor(slot, 'slotKind')?.get !== undefined;
|
|
212
|
+
/**
|
|
213
|
+
* The shape of the value a local holds.
|
|
214
|
+
*
|
|
215
|
+
* @param local The local.
|
|
216
|
+
*
|
|
217
|
+
* @returns The shape.
|
|
218
|
+
*/
|
|
219
|
+
export function localShape(local) {
|
|
220
|
+
if (holdsUntyped(local))
|
|
221
|
+
return 'number';
|
|
222
|
+
if (local.isBoolean)
|
|
223
|
+
return 'bool';
|
|
224
|
+
if (local.slotKind === 'string')
|
|
225
|
+
return 'string';
|
|
226
|
+
if (local.slotKind === undefined)
|
|
227
|
+
return 'struct';
|
|
228
|
+
return local.slotKind === 'node' ? 'node' : 'number';
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Has a slot record what the typing pass finds its value to be. A string's or a boolean's is known at once.
|
|
232
|
+
*
|
|
233
|
+
* @param target The slot's record: a local, say.
|
|
234
|
+
* @param shape The value's shape.
|
|
235
|
+
* @param value What the slot records about it, once it is typed.
|
|
236
|
+
*
|
|
237
|
+
* @returns The record.
|
|
238
|
+
*/
|
|
239
|
+
export function holdLater(target, shape, value) {
|
|
240
|
+
const held = target;
|
|
241
|
+
const set = () => {
|
|
242
|
+
const facets = value();
|
|
243
|
+
for (const facet of SLOT_FACETS) {
|
|
244
|
+
delete held[facet];
|
|
245
|
+
if (facets[facet] !== undefined) {
|
|
246
|
+
held[facet] = facets[facet];
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
if (!defersTyping()) {
|
|
251
|
+
set();
|
|
252
|
+
return held;
|
|
253
|
+
}
|
|
254
|
+
if (shape === 'string') {
|
|
255
|
+
held.slotKind = 'string';
|
|
256
|
+
}
|
|
257
|
+
else if (shape === 'bool') {
|
|
258
|
+
held.slotKind = 'int';
|
|
259
|
+
held.isBoolean = true;
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
hideUntilTyped(held, 'slotKind');
|
|
263
|
+
}
|
|
264
|
+
whenTyped(set);
|
|
265
|
+
return held;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Has a slot record what another records, which the typing pass may have yet to find.
|
|
269
|
+
*
|
|
270
|
+
* @param target The slot's record.
|
|
271
|
+
* @param from The record it copies.
|
|
272
|
+
*
|
|
273
|
+
* @returns The record.
|
|
274
|
+
*/
|
|
275
|
+
export function holdSame(target, from) {
|
|
276
|
+
if (holdsUntyped(from)) {
|
|
277
|
+
return holdLater(target, 'number', () => from);
|
|
278
|
+
}
|
|
279
|
+
const held = target;
|
|
280
|
+
for (const facet of SLOT_FACETS) {
|
|
281
|
+
if (from[facet] !== undefined) {
|
|
282
|
+
held[facet] = from[facet];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return held;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* A local that holds what another holds, which the typing pass may have yet to find.
|
|
289
|
+
*
|
|
290
|
+
* @param from The local it copies.
|
|
291
|
+
* @param rest Its own fields.
|
|
292
|
+
*
|
|
293
|
+
* @returns The local.
|
|
294
|
+
*/
|
|
295
|
+
export function copyLocal(from, rest) {
|
|
296
|
+
if (!holdsUntyped(from)) {
|
|
297
|
+
return { ...from, ...rest };
|
|
298
|
+
}
|
|
299
|
+
const copy = {};
|
|
300
|
+
for (const key of Object.keys(from)) {
|
|
301
|
+
if (!SLOT_FACETS.includes(key)) {
|
|
302
|
+
copy[key] = from[key];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
Object.assign(copy, rest);
|
|
306
|
+
return holdLater(copy, 'number', () => from);
|
|
307
|
+
}
|
|
@@ -0,0 +1,321 @@
|
|
|
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
|
+
* Updaters with a block body: `setN(v => { … return next; })`. The body runs in the handler as its statements do, and
|
|
18
|
+
* each `return next` sets the state through an updater of its own, `setN(v => next)`, then leaves the body.
|
|
19
|
+
*/
|
|
20
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
21
|
+
- Imports
|
|
22
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
23
|
+
import { aotError } from './diagnostics.mjs';
|
|
24
|
+
import { exits } from './helper-values.mjs';
|
|
25
|
+
import { switchAsIf } from './switch-cases.mjs';
|
|
26
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
27
|
+
- Implementation
|
|
28
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
29
|
+
/** Calls an updater can't make: they start or stop something. */
|
|
30
|
+
const SIDE_EFFECT_CALLS = new Set([
|
|
31
|
+
'setInterval',
|
|
32
|
+
'setTimeout',
|
|
33
|
+
'clearInterval',
|
|
34
|
+
'clearTimeout',
|
|
35
|
+
'updateVector',
|
|
36
|
+
'scrollTo',
|
|
37
|
+
]);
|
|
38
|
+
/** The sets a `return` in an updater makes. */
|
|
39
|
+
const returnSets = new WeakSet();
|
|
40
|
+
/**
|
|
41
|
+
* Whether a call is a set a `return` in an updater makes. It leaves any loop in the updater at once, and the setter
|
|
42
|
+
* call the updater was given is checked against the loops outside it.
|
|
43
|
+
*
|
|
44
|
+
* @param node The call.
|
|
45
|
+
*
|
|
46
|
+
* @returns Whether it is.
|
|
47
|
+
*/
|
|
48
|
+
export const isReturnSet = (node) => returnSets.has(node);
|
|
49
|
+
/**
|
|
50
|
+
* An error located at a node.
|
|
51
|
+
*
|
|
52
|
+
* @param node The node.
|
|
53
|
+
* @param message What is wrong.
|
|
54
|
+
* @param hint How to rewrite it.
|
|
55
|
+
*
|
|
56
|
+
* @returns The error.
|
|
57
|
+
*/
|
|
58
|
+
function locatedError(node, message, hint) {
|
|
59
|
+
const error = aotError(message, hint);
|
|
60
|
+
if (node.loc)
|
|
61
|
+
error.aotLoc = node.loc.start;
|
|
62
|
+
return error;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The names a pattern declares.
|
|
66
|
+
*
|
|
67
|
+
* @param pattern The pattern.
|
|
68
|
+
*
|
|
69
|
+
* @returns The names.
|
|
70
|
+
*/
|
|
71
|
+
export function patternNames(pattern) {
|
|
72
|
+
if (!pattern)
|
|
73
|
+
return [];
|
|
74
|
+
switch (pattern.type) {
|
|
75
|
+
case 'Identifier':
|
|
76
|
+
return [pattern.name];
|
|
77
|
+
case 'ObjectPattern':
|
|
78
|
+
return pattern.properties.flatMap(property => property.type === 'RestElement'
|
|
79
|
+
? patternNames(property.argument)
|
|
80
|
+
: patternNames(property.value));
|
|
81
|
+
case 'ArrayPattern':
|
|
82
|
+
return pattern.elements.flatMap(element => patternNames(element));
|
|
83
|
+
case 'AssignmentPattern':
|
|
84
|
+
return patternNames(pattern.left);
|
|
85
|
+
case 'RestElement':
|
|
86
|
+
return patternNames(pattern.argument);
|
|
87
|
+
default:
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The names statements declare at their own level.
|
|
93
|
+
*
|
|
94
|
+
* @param statements The statements.
|
|
95
|
+
*
|
|
96
|
+
* @returns The names.
|
|
97
|
+
*/
|
|
98
|
+
export const declaredIn = (statements) => statements.flatMap(statement => statement?.type === 'VariableDeclaration'
|
|
99
|
+
? statement.declarations.flatMap(({ id }) => patternNames(id))
|
|
100
|
+
: []);
|
|
101
|
+
/**
|
|
102
|
+
* Each statement in a body, nested ones included, and not those of a function inside it, with the body's names in
|
|
103
|
+
* scope where it is: those its own block declares and the blocks around it, and a loop's head.
|
|
104
|
+
*
|
|
105
|
+
* @param statements The body.
|
|
106
|
+
* @param visit Called with each statement and the names in scope there.
|
|
107
|
+
* @param outer The names in scope around the body.
|
|
108
|
+
*/
|
|
109
|
+
function eachStatement(statements, visit, outer = new Set()) {
|
|
110
|
+
const inScope = new Set([...outer, ...declaredIn(statements)]);
|
|
111
|
+
const inner = (body, names = inScope) => eachStatement(body, visit, names);
|
|
112
|
+
for (const statement of statements) {
|
|
113
|
+
visit(statement, inScope);
|
|
114
|
+
switch (statement.type) {
|
|
115
|
+
case 'BlockStatement':
|
|
116
|
+
inner(statement.body);
|
|
117
|
+
break;
|
|
118
|
+
case 'IfStatement':
|
|
119
|
+
inner([statement.consequent]);
|
|
120
|
+
if (statement.alternate)
|
|
121
|
+
inner([statement.alternate]);
|
|
122
|
+
break;
|
|
123
|
+
// As it runs: each case a branch of its own, then the cases it falls through to.
|
|
124
|
+
case 'SwitchStatement':
|
|
125
|
+
inner([switchAsIf(statement)]);
|
|
126
|
+
break;
|
|
127
|
+
case 'ForStatement':
|
|
128
|
+
inner([statement.body], new Set([...inScope, ...declaredIn([statement.init])]));
|
|
129
|
+
break;
|
|
130
|
+
case 'ForOfStatement':
|
|
131
|
+
case 'ForInStatement':
|
|
132
|
+
inner([statement.body], new Set([...inScope, ...declaredIn([statement.left])]));
|
|
133
|
+
break;
|
|
134
|
+
case 'WhileStatement':
|
|
135
|
+
case 'DoWhileStatement':
|
|
136
|
+
case 'LabeledStatement':
|
|
137
|
+
inner([statement.body]);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The values a block body returns, from any branch. A function inside it returns its own.
|
|
144
|
+
*
|
|
145
|
+
* @param body The body.
|
|
146
|
+
*
|
|
147
|
+
* @returns The returned values.
|
|
148
|
+
*/
|
|
149
|
+
export function returnedValues(body) {
|
|
150
|
+
const values = [];
|
|
151
|
+
eachStatement(body.body, statement => {
|
|
152
|
+
if (statement.type === 'ReturnStatement' && statement.argument) {
|
|
153
|
+
values.push(statement.argument);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
return values;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Checks a body only works out a value: it writes only its own locals in scope where it writes them, calls
|
|
160
|
+
* nothing as a statement or to start a timer, and throws nothing. Each way out returns the value.
|
|
161
|
+
*
|
|
162
|
+
* @param body The body.
|
|
163
|
+
* @param what What it is, for errors.
|
|
164
|
+
* @param wording How its errors put it.
|
|
165
|
+
*/
|
|
166
|
+
function checkPureBody(body, what, wording) {
|
|
167
|
+
const statements = body.body;
|
|
168
|
+
const { pureHint } = wording;
|
|
169
|
+
eachStatement(statements, (statement, inScope) => {
|
|
170
|
+
if (statement.type === 'ThrowStatement') {
|
|
171
|
+
throw locatedError(statement, `AOT: ${what} can reach a \`throw\``, wording.throwHint);
|
|
172
|
+
}
|
|
173
|
+
if (statement.type === 'ReturnStatement' && !statement.argument) {
|
|
174
|
+
throw locatedError(statement, `AOT: each \`return\` in ${what} must give ${wording.gives}`, wording.returnHint);
|
|
175
|
+
}
|
|
176
|
+
if (statement.type === 'VariableDeclaration') {
|
|
177
|
+
for (const { init } of statement.declarations) {
|
|
178
|
+
if (init?.type === 'CallExpression' &&
|
|
179
|
+
init.callee.type === 'Identifier' &&
|
|
180
|
+
SIDE_EFFECT_CALLS.has(init.callee.name)) {
|
|
181
|
+
throw locatedError(init, `AOT: ${what} can't call ${init.callee.name}`, pureHint);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (statement.type !== 'ExpressionStatement')
|
|
186
|
+
return;
|
|
187
|
+
const { expression } = statement;
|
|
188
|
+
const written = expression.type === 'AssignmentExpression'
|
|
189
|
+
? expression.left
|
|
190
|
+
: expression.type === 'UpdateExpression'
|
|
191
|
+
? expression.argument
|
|
192
|
+
: null;
|
|
193
|
+
if (written?.type === 'Identifier' && inScope.has(written.name))
|
|
194
|
+
return;
|
|
195
|
+
const called = expression.type === 'CallExpression' &&
|
|
196
|
+
expression.callee.type === 'Identifier'
|
|
197
|
+
? expression.callee.name
|
|
198
|
+
: null;
|
|
199
|
+
throw locatedError(statement, called
|
|
200
|
+
? `AOT: ${what} can't call ${called}`
|
|
201
|
+
: written
|
|
202
|
+
? `AOT: ${what} can only write its own locals`
|
|
203
|
+
: `AOT: ${what} can only work out ${wording.gives}`, pureHint);
|
|
204
|
+
});
|
|
205
|
+
if (!exits(statements)) {
|
|
206
|
+
throw locatedError(body, `AOT: ${what} can end without returning ${wording.gives}`, wording.endHint);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Checks an updater's body only works out the new value (see checkPureBody).
|
|
211
|
+
*
|
|
212
|
+
* @param updater The updater.
|
|
213
|
+
* @param what What it is, for errors.
|
|
214
|
+
*/
|
|
215
|
+
function checkUpdaterBody(updater, what) {
|
|
216
|
+
checkPureBody(updater.body, what, {
|
|
217
|
+
gives: 'the new value',
|
|
218
|
+
returnHint: 'return the value to set, e.g. return next;',
|
|
219
|
+
pureHint: `${what.startsWith('reducer') ? 'a reducer' : 'an updater'} only works out the new value from the old ` +
|
|
220
|
+
'one. Set other state, write refs and start timers in the handler, outside it.',
|
|
221
|
+
throwHint: "Flow B has no exceptions. A reducer's throw for an action type no dispatch sends is dropped, so " +
|
|
222
|
+
'test the type directly: switch (action.type) { … default: throw … }.',
|
|
223
|
+
endHint: 'return the value to set at the end of the updater, and from each branch that ends it.',
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Checks the body of a memo or a helper used as a value, which loops, only works out its value (see checkPureBody).
|
|
228
|
+
*
|
|
229
|
+
* @param body The body.
|
|
230
|
+
* @param what What it is, for errors: `the useMemo for "total"` or `helper "sum"`.
|
|
231
|
+
* @param isMemo It is a memo's.
|
|
232
|
+
*/
|
|
233
|
+
export function checkValueBody(body, what, isMemo) {
|
|
234
|
+
checkPureBody(body, what, {
|
|
235
|
+
gives: 'a value',
|
|
236
|
+
returnHint: 'return the value, e.g. return total;',
|
|
237
|
+
pureHint: isMemo
|
|
238
|
+
? 'a memo works out a value without side effects. Set state in a handler or an effect.'
|
|
239
|
+
: 'a helper whose result is used works out a value without side effects. Call one that sets ' +
|
|
240
|
+
'state as a statement of its own.',
|
|
241
|
+
throwHint: 'Flow B has no exceptions. Return a value the caller can test instead.',
|
|
242
|
+
endHint: `return a value at the end of the ${isMemo ? 'memo' : 'helper'}, and from each branch that ends it.`,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* A statement with each `return next` in it made `return setter(previous => next)`.
|
|
247
|
+
*
|
|
248
|
+
* @param statement The statement.
|
|
249
|
+
* @param setOf The set call for a returned value.
|
|
250
|
+
*
|
|
251
|
+
* @returns The statement.
|
|
252
|
+
*/
|
|
253
|
+
function settingReturns(statement, setOf) {
|
|
254
|
+
const each = (inner) => settingReturns(inner, setOf);
|
|
255
|
+
switch (statement.type) {
|
|
256
|
+
case 'ReturnStatement':
|
|
257
|
+
return statement.argument
|
|
258
|
+
? { ...statement, argument: setOf(statement.argument) }
|
|
259
|
+
: statement;
|
|
260
|
+
case 'BlockStatement':
|
|
261
|
+
return { ...statement, body: statement.body.map(each) };
|
|
262
|
+
case 'IfStatement':
|
|
263
|
+
return {
|
|
264
|
+
...statement,
|
|
265
|
+
consequent: each(statement.consequent),
|
|
266
|
+
alternate: statement.alternate ? each(statement.alternate) : null,
|
|
267
|
+
};
|
|
268
|
+
case 'SwitchStatement':
|
|
269
|
+
return {
|
|
270
|
+
...statement,
|
|
271
|
+
cases: statement.cases.map(switchCase => ({
|
|
272
|
+
...switchCase,
|
|
273
|
+
consequent: switchCase.consequent.map(each),
|
|
274
|
+
})),
|
|
275
|
+
};
|
|
276
|
+
case 'ForStatement':
|
|
277
|
+
case 'ForOfStatement':
|
|
278
|
+
case 'ForInStatement':
|
|
279
|
+
case 'WhileStatement':
|
|
280
|
+
case 'DoWhileStatement':
|
|
281
|
+
case 'LabeledStatement':
|
|
282
|
+
return { ...statement, body: each(statement.body) };
|
|
283
|
+
default:
|
|
284
|
+
return statement;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* The statements a block-bodied updater runs in the handler: its body, with each `return next` setting the state
|
|
289
|
+
* through an updater of its own, so the setter lowers it as it does any updater. That updater reads the parameter as
|
|
290
|
+
* the body has it bound (narrowed by a test on a tag), except a list's, which a list op needs by name.
|
|
291
|
+
*
|
|
292
|
+
* @param setter The setter's name.
|
|
293
|
+
* @param updater The updater.
|
|
294
|
+
* @param keepsParameter Whether each set names the parameter again.
|
|
295
|
+
* @param what What it is, for errors: by default `setN's updater`.
|
|
296
|
+
*
|
|
297
|
+
* @returns The statements.
|
|
298
|
+
*/
|
|
299
|
+
export function updaterStatements(setter, updater, keepsParameter, what = `${setter}'s updater`) {
|
|
300
|
+
checkUpdaterBody(updater, what);
|
|
301
|
+
const setOf = (value) => {
|
|
302
|
+
const call = {
|
|
303
|
+
type: 'CallExpression',
|
|
304
|
+
callee: { type: 'Identifier', name: setter },
|
|
305
|
+
arguments: [
|
|
306
|
+
{
|
|
307
|
+
type: 'ArrowFunctionExpression',
|
|
308
|
+
params: keepsParameter ? updater.params : [],
|
|
309
|
+
body: value,
|
|
310
|
+
expression: true,
|
|
311
|
+
async: false,
|
|
312
|
+
loc: value.loc,
|
|
313
|
+
},
|
|
314
|
+
],
|
|
315
|
+
loc: value.loc,
|
|
316
|
+
};
|
|
317
|
+
returnSets.add(call);
|
|
318
|
+
return call;
|
|
319
|
+
};
|
|
320
|
+
return updater.body.body.map(statement => settingReturns(statement, setOf));
|
|
321
|
+
}
|