embedded-react 0.19.0 → 0.20.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/README.md +7 -3
- package/aot/compile.mjs +178 -58
- package/aot/compiler/animations.mjs +290 -281
- package/aot/compiler/boot.mjs +70 -33
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +9 -9
- package/aot/compiler/collect.mjs +324 -205
- package/aot/compiler/control-flow.mjs +209 -197
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image.mjs +51 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +23 -23
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +372 -333
- package/aot/compiler/handlers.mjs +473 -387
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +4 -4
- package/aot/compiler/ir/reads.mjs +19 -19
- package/aot/compiler/ir/types.mjs +14 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/lower.mjs +77 -80
- package/aot/compiler/nodes.mjs +169 -152
- package/aot/compiler/options.mjs +35 -30
- package/aot/compiler/out.mjs +18 -22
- package/aot/compiler/package/animations.mjs +669 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +177 -0
- package/aot/compiler/package/bytecode.mjs +286 -0
- package/aot/compiler/package/checks.mjs +1022 -0
- package/aot/compiler/package/codegen.mjs +1329 -0
- package/aot/compiler/package/enums.mjs +138 -0
- package/aot/compiler/package/format.mjs +760 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +1606 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/tables.mjs +103 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +664 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +21 -21
- package/aot/compiler/static-eval.mjs +25 -23
- package/aot/compiler/style-text.mjs +161 -172
- package/aot/compiler/svg.mjs +477 -251
- package/aot/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +102 -90
- 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 +14 -12
- package/cli.mjs +359 -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 +46 -37
- 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 +28 -20
- 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 +101 -82
- package/src/embedded-react/Animated.js +24 -24
- 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 +6 -6
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +5 -5
- 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/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
|
@@ -19,20 +19,19 @@
|
|
|
19
19
|
import { parseColor, parsePath } from '../../src/embedded-react/svg-ops.js';
|
|
20
20
|
import { aotError, constantText, withLoc } from './diagnostics.mjs';
|
|
21
21
|
import { evalStatic } from './static-eval.mjs';
|
|
22
|
-
import { mix64Error, staticInt, isTime64, constDivisor,
|
|
22
|
+
import { mix64Error, staticInt, isTime64, constDivisor, lowerSourcedWide, lowerSource, slotValueOf, lowerExpr, lowerExprWide, lowerText, } from './expressions.mjs';
|
|
23
23
|
import { isFn } from './collect.mjs';
|
|
24
24
|
import { isAnimatedCall, resolveAnim, animValues, lowerAnimateStart, stopWaitOwner, } from './animations.mjs';
|
|
25
|
-
import { svgNum, CAP_MAP, JOIN_MAP, arcTape, circleTape, rectTape, lineTape, checkedTapeNumber, checkVectorPaints, vectorPaintOf, } from './svg.mjs';
|
|
26
|
-
import { armedFlag } from './
|
|
27
|
-
import {
|
|
28
|
-
import { FLOAT, INT, isConvertible64, isIntSlot, isTime, refType, slotOf, } from './ir/types.mjs';
|
|
25
|
+
import { svgNum, CAP_MAP, JOIN_MAP, arcTape, circleTape, rectTape, lineTape, checkedTapeNumber, checkVectorPaints, vectorPaintOf, DEFAULT_MITER_LIMIT, } from './svg.mjs';
|
|
26
|
+
import { armedFlag } from './ir/functions.mjs';
|
|
27
|
+
import { FLOAT, INT, isConvertible64, isIntSlot, isTime, refType, slotOf, stateType, } from './ir/types.mjs';
|
|
29
28
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
30
29
|
- Constants
|
|
31
30
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
32
31
|
/** The constant 0, which a remainder by 0 or ±1 stores. */
|
|
33
32
|
const ZERO = { kind: 'number', value: 0, type: INT };
|
|
34
33
|
/** Each updateVector shape key and the numbers its array takes, for errors. */
|
|
35
|
-
const
|
|
34
|
+
const UPDATE_VECTOR_ARGUMENTS = {
|
|
36
35
|
arc: '[cx, cy, r, startAngle, endAngle]',
|
|
37
36
|
circle: '[cx, cy, r]',
|
|
38
37
|
rect: '[x, y, width, height] or [x, y, width, height, rx, ry]',
|
|
@@ -46,12 +45,12 @@ const UPDATE_VECTOR_ARGS = {
|
|
|
46
45
|
*
|
|
47
46
|
* @param listState The list state.
|
|
48
47
|
* @param nextList The setter's argument: the list's next value.
|
|
49
|
-
* @param
|
|
48
|
+
* @param environment The expression environment.
|
|
50
49
|
*
|
|
51
50
|
* @returns The statements.
|
|
52
51
|
*/
|
|
53
|
-
function lowerListOp(listState, nextList,
|
|
54
|
-
const {
|
|
52
|
+
function lowerListOp(listState, nextList, environment) {
|
|
53
|
+
const { capacity: capacity, struct } = listState;
|
|
55
54
|
// setItems([...items, a, b]) — append; setItems([]) — clear.
|
|
56
55
|
if (nextList.type === 'ArrayExpression') {
|
|
57
56
|
if (nextList.elements.length === 0) {
|
|
@@ -93,14 +92,14 @@ function lowerListOp(listState, nextList, env) {
|
|
|
93
92
|
});
|
|
94
93
|
}
|
|
95
94
|
else if (field.kind === 'string') {
|
|
96
|
-
const text = lowerText(valueNode,
|
|
97
|
-
checkConstantFits(text, `field "${field.key}" of list state "${listState.name}"`,
|
|
95
|
+
const text = lowerText(valueNode, environment);
|
|
96
|
+
checkConstantFits(text, `field "${field.key}" of list state "${listState.name}"`, environment);
|
|
98
97
|
values.push({ field, text });
|
|
99
98
|
}
|
|
100
99
|
else {
|
|
101
100
|
values.push({
|
|
102
101
|
field,
|
|
103
|
-
value: storeValue(lowerExpr(valueNode,
|
|
102
|
+
value: storeValue(lowerExpr(valueNode, environment), field.kind),
|
|
104
103
|
});
|
|
105
104
|
}
|
|
106
105
|
}
|
|
@@ -114,14 +113,14 @@ function lowerListOp(listState, nextList, env) {
|
|
|
114
113
|
nextList.callee.object.name === listState.name &&
|
|
115
114
|
nextList.callee.property.name === 'slice') {
|
|
116
115
|
const [start, end] = nextList.arguments;
|
|
117
|
-
if (start && staticInt(start,
|
|
118
|
-
throw aotError(`AOT: only ${listState.name}.slice(0, end) is supported on a list`, `a list is a fixed
|
|
116
|
+
if (start && staticInt(start, environment) !== 0) {
|
|
117
|
+
throw aotError(`AOT: only ${listState.name}.slice(0, end) is supported on a list`, `a list is a fixed pool and a count, so a slice can only drop items from the end — ` +
|
|
119
118
|
`e.g. ${listState.name}.slice(0, -1) drops the last one.`);
|
|
120
119
|
}
|
|
121
120
|
if (!end)
|
|
122
121
|
return [];
|
|
123
122
|
// Settle a constant end here, clamped to ±capacity (past which nothing changes), so it needs no 64-bit math.
|
|
124
|
-
const staticEnd = staticInt(end,
|
|
123
|
+
const staticEnd = staticInt(end, environment);
|
|
125
124
|
if (staticEnd !== null) {
|
|
126
125
|
const clampedEnd = Math.max(-capacity, Math.min(capacity, staticEnd));
|
|
127
126
|
if (clampedEnd === -1) {
|
|
@@ -138,9 +137,13 @@ function lowerListOp(listState, nextList, env) {
|
|
|
138
137
|
},
|
|
139
138
|
];
|
|
140
139
|
}
|
|
141
|
-
// A runtime end
|
|
140
|
+
// A runtime end applies the same JS rules when it runs.
|
|
142
141
|
return [
|
|
143
|
-
{
|
|
142
|
+
{
|
|
143
|
+
kind: 'sliceItems',
|
|
144
|
+
list: listState,
|
|
145
|
+
end: asInt(lowerExpr(end, environment)),
|
|
146
|
+
},
|
|
144
147
|
];
|
|
145
148
|
}
|
|
146
149
|
throw new Error(`AOT: unsupported list operation on "${listState.name}" (use [...${listState.name}, ` +
|
|
@@ -157,31 +160,31 @@ function blockList(node) {
|
|
|
157
160
|
return node.type === 'BlockStatement' ? node.body : [node];
|
|
158
161
|
}
|
|
159
162
|
/**
|
|
160
|
-
* Checks a value written to a numeric state or ref slot, whose
|
|
161
|
-
* written to an int slot is recorded in
|
|
162
|
-
* 64-bit timestamp, and compileWidened compiles again with those slots widened to
|
|
163
|
+
* Checks a value written to a numeric state or ref slot, whose kind came from its initial value. Every value
|
|
164
|
+
* written to an int slot is recorded in environment.stores, from which the typing pass infers the slots that hold a
|
|
165
|
+
* 64-bit timestamp, and compileWidened compiles again with those slots widened to 64 bits. A timestamp cannot
|
|
163
166
|
* go into a float or boolean slot (a 64-bit constant can go into a float one), and a widened slot takes no
|
|
164
167
|
* floats.
|
|
165
168
|
*
|
|
166
169
|
* @param lowered The value: its source, and its type.
|
|
167
|
-
* @param slotType The slot's
|
|
168
|
-
* @param
|
|
170
|
+
* @param slotType The slot's kind.
|
|
171
|
+
* @param isBoolean The slot holds a boolean.
|
|
169
172
|
* @param widened What the slot holds, if it was widened to 64 bits.
|
|
170
|
-
* @param
|
|
173
|
+
* @param slotName The slot's name, for environment.stores.
|
|
171
174
|
* @param slotDescription The slot, for the message.
|
|
172
|
-
* @param
|
|
175
|
+
* @param environment The expression environment.
|
|
173
176
|
*/
|
|
174
|
-
function storeCheck(lowered, slotType,
|
|
175
|
-
const value = lowered.
|
|
176
|
-
if ((slotType === 'int' && !
|
|
177
|
-
|
|
178
|
-
slot:
|
|
177
|
+
function storeCheck(lowered, slotType, isBoolean, widened, slotName, slotDescription, environment) {
|
|
178
|
+
const value = lowered.expression;
|
|
179
|
+
if ((slotType === 'int' && !isBoolean) || widened === 'number') {
|
|
180
|
+
environment.stores.push({
|
|
181
|
+
slot: slotName,
|
|
179
182
|
value: lowered.source,
|
|
180
|
-
math64: Boolean(
|
|
183
|
+
math64: Boolean(environment.math64),
|
|
181
184
|
});
|
|
182
185
|
}
|
|
183
186
|
if (value.type.kind === 'int64' && slotType !== 'i64') {
|
|
184
|
-
if (slotType === 'int' && !
|
|
187
|
+
if (slotType === 'int' && !isBoolean)
|
|
185
188
|
return;
|
|
186
189
|
if (isConvertible64(value.type) && slotType === 'float')
|
|
187
190
|
return;
|
|
@@ -192,7 +195,7 @@ function storeCheck(lowered, slotType, isBool, widened, slotCName, slotDescripti
|
|
|
192
195
|
}
|
|
193
196
|
}
|
|
194
197
|
/**
|
|
195
|
-
* `value` for an int destination. A float goes through
|
|
198
|
+
* `value` for an int destination. A float goes through a checked conversion: a plain one is undefined for
|
|
196
199
|
* NaN or a value past the int range.
|
|
197
200
|
*
|
|
198
201
|
* @param value The lowered value.
|
|
@@ -206,7 +209,7 @@ const asInt = (value) => value.type.kind === 'float'
|
|
|
206
209
|
* `value` for a slot of `slotType`: an int slot takes a float the way asInt does.
|
|
207
210
|
*
|
|
208
211
|
* @param value The lowered value.
|
|
209
|
-
* @param slotType The slot's
|
|
212
|
+
* @param slotType The slot's kind.
|
|
210
213
|
*
|
|
211
214
|
* @returns The value the slot stores.
|
|
212
215
|
*/
|
|
@@ -224,21 +227,21 @@ const refRead = (ref) => ({
|
|
|
224
227
|
type: refType(ref),
|
|
225
228
|
});
|
|
226
229
|
/**
|
|
227
|
-
* Refuses a constant string too long for the buffer it is written into, which
|
|
230
|
+
* Refuses a constant string too long for the buffer it is written into, which would cut it short. A string
|
|
228
231
|
* built at run time is checked when it is written instead.
|
|
229
232
|
*
|
|
230
233
|
* @param text The text written.
|
|
231
234
|
* @param what What it is written into, for the error.
|
|
232
|
-
* @param
|
|
235
|
+
* @param environment The expression environment.
|
|
233
236
|
*/
|
|
234
|
-
function checkConstantFits(text, what,
|
|
237
|
+
function checkConstantFits(text, what, environment) {
|
|
235
238
|
const constant = constantText(text);
|
|
236
239
|
if (constant === null) {
|
|
237
240
|
return;
|
|
238
241
|
}
|
|
239
242
|
const bytes = Buffer.byteLength(constant, 'utf8');
|
|
240
|
-
if (bytes >=
|
|
241
|
-
throw aotError(`AOT: ${what} is set to a ${bytes}-byte string, but a string holds ${
|
|
243
|
+
if (bytes >= environment.caps.listStrCap) {
|
|
244
|
+
throw aotError(`AOT: ${what} is set to a ${bytes}-byte string, but a string holds ${environment.caps.listStrCap - 1} bytes`, 'raise ER_AOT_LIST_STR_CAP (the listStrCap option), or shorten the string.');
|
|
242
245
|
}
|
|
243
246
|
}
|
|
244
247
|
/**
|
|
@@ -247,24 +250,26 @@ function checkConstantFits(text, what, env) {
|
|
|
247
250
|
*
|
|
248
251
|
* @param scalarState The state slot.
|
|
249
252
|
* @param valueNode The value.
|
|
250
|
-
* @param
|
|
253
|
+
* @param environment The expression environment.
|
|
251
254
|
*
|
|
252
255
|
* @returns The write.
|
|
253
256
|
*/
|
|
254
|
-
function lowerScalarSet(scalarState, valueNode,
|
|
255
|
-
if (scalarState.
|
|
257
|
+
function lowerScalarSet(scalarState, valueNode, environment) {
|
|
258
|
+
if (scalarState.slotKind !== 'string') {
|
|
256
259
|
// A 64-bit slot takes int `+ - *` worked out in 64 bits.
|
|
257
|
-
const lowered = lowerSource(valueNode, scalarState.
|
|
258
|
-
|
|
259
|
-
|
|
260
|
+
const lowered = lowerSource(valueNode, scalarState.slotKind === 'i64'
|
|
261
|
+
? { ...environment, math64: true }
|
|
262
|
+
: environment);
|
|
263
|
+
const value = lowered.expression;
|
|
264
|
+
storeCheck(lowered, scalarState.slotKind, scalarState.isBoolean, scalarState.widened, scalarState.slotName, `state "${scalarState.name}"`, environment);
|
|
260
265
|
return {
|
|
261
266
|
kind: 'setState',
|
|
262
267
|
state: scalarState,
|
|
263
|
-
value: storeValue(value, scalarState.
|
|
268
|
+
value: storeValue(value, scalarState.slotKind),
|
|
264
269
|
};
|
|
265
270
|
}
|
|
266
|
-
const text = lowerText(valueNode,
|
|
267
|
-
checkConstantFits(text, `state "${scalarState.name}"`,
|
|
271
|
+
const text = lowerText(valueNode, environment);
|
|
272
|
+
checkConstantFits(text, `state "${scalarState.name}"`, environment);
|
|
268
273
|
return {
|
|
269
274
|
kind: 'setStateText',
|
|
270
275
|
state: scalarState,
|
|
@@ -275,17 +280,17 @@ function lowerScalarSet(scalarState, valueNode, env) {
|
|
|
275
280
|
* Resolves `node` to its ref when it is `<ref>.current` member access on a known value ref.
|
|
276
281
|
*
|
|
277
282
|
* @param node An assignment or update target.
|
|
278
|
-
* @param
|
|
283
|
+
* @param environment The expression environment.
|
|
279
284
|
*
|
|
280
285
|
* @returns The ref, marked used, or null when the node is not such an access.
|
|
281
286
|
*/
|
|
282
|
-
function refTarget(node,
|
|
287
|
+
function refTarget(node, environment) {
|
|
283
288
|
if (node?.type === 'MemberExpression' &&
|
|
284
289
|
!node.computed &&
|
|
285
290
|
node.object.type === 'Identifier' &&
|
|
286
291
|
node.property.name === 'current' &&
|
|
287
|
-
|
|
288
|
-
const ref =
|
|
292
|
+
environment.refs?.has(node.object.name)) {
|
|
293
|
+
const ref = environment.refs.get(node.object.name);
|
|
289
294
|
ref.used = true;
|
|
290
295
|
return ref;
|
|
291
296
|
}
|
|
@@ -298,12 +303,12 @@ function refTarget(node, env) {
|
|
|
298
303
|
* be unique in the handler block they are written into.
|
|
299
304
|
*
|
|
300
305
|
* @param shapeNode The shape's object literal.
|
|
301
|
-
* @param
|
|
306
|
+
* @param environment The expression environment.
|
|
302
307
|
* @param tag Names the shape's locals.
|
|
303
308
|
*
|
|
304
309
|
* @returns The shape's tape slots, the locals they need, and its paint.
|
|
305
310
|
*/
|
|
306
|
-
function imperativeShape(shapeNode,
|
|
311
|
+
function imperativeShape(shapeNode, environment, tag) {
|
|
307
312
|
// Index the shape's properties by name.
|
|
308
313
|
if (shapeNode?.type !== 'ObjectExpression') {
|
|
309
314
|
throw new Error('AOT: each updateVector shape must be an object literal');
|
|
@@ -321,7 +326,7 @@ function imperativeShape(shapeNode, env, tag) {
|
|
|
321
326
|
throw new Error(`AOT: updateVector "${key}" must be an array literal`);
|
|
322
327
|
}
|
|
323
328
|
const elements = props[key].elements;
|
|
324
|
-
const shapeHint = `${key}: ${
|
|
329
|
+
const shapeHint = `${key}: ${UPDATE_VECTOR_ARGUMENTS[key]}.`;
|
|
325
330
|
if (elements.length < required) {
|
|
326
331
|
throw aotError(`AOT: updateVector "${key}" needs ${required} numbers (got ${elements.length})`, shapeHint);
|
|
327
332
|
}
|
|
@@ -330,7 +335,7 @@ function imperativeShape(shapeNode, env, tag) {
|
|
|
330
335
|
if (!valueNode) {
|
|
331
336
|
throw aotError(`AOT: updateVector "${key}" has an empty slot at position ${position}`, shapeHint);
|
|
332
337
|
}
|
|
333
|
-
return { kind: 'float', value: lowerExpr(valueNode,
|
|
338
|
+
return { kind: 'float', value: lowerExpr(valueNode, environment) };
|
|
334
339
|
});
|
|
335
340
|
};
|
|
336
341
|
let geometry;
|
|
@@ -352,7 +357,7 @@ function imperativeShape(shapeNode, env, tag) {
|
|
|
352
357
|
}
|
|
353
358
|
else if (props.path) {
|
|
354
359
|
geometry = {
|
|
355
|
-
entries: parsePath(String(evalStatic(props.path,
|
|
360
|
+
entries: parsePath(String(evalStatic(props.path, environment.constants ?? {}))).map((value) => checkedTapeNumber(value)),
|
|
356
361
|
locals: [],
|
|
357
362
|
};
|
|
358
363
|
}
|
|
@@ -364,7 +369,7 @@ function imperativeShape(shapeNode, env, tag) {
|
|
|
364
369
|
if (props[key] == null)
|
|
365
370
|
return fallback;
|
|
366
371
|
try {
|
|
367
|
-
return evalStatic(props[key],
|
|
372
|
+
return evalStatic(props[key], environment.constants ?? {});
|
|
368
373
|
}
|
|
369
374
|
catch {
|
|
370
375
|
throw new Error(`AOT: updateVector paint "${key}" must be static`);
|
|
@@ -374,7 +379,7 @@ function imperativeShape(shapeNode, env, tag) {
|
|
|
374
379
|
parseColor(paintValue('fill', 'none')),
|
|
375
380
|
parseColor(paintValue('stroke', 'none')),
|
|
376
381
|
svgNum(paintValue('strokeWidth', 1), 1),
|
|
377
|
-
svgNum(paintValue('miter',
|
|
382
|
+
svgNum(paintValue('miter', DEFAULT_MITER_LIMIT), DEFAULT_MITER_LIMIT),
|
|
378
383
|
CAP_MAP[paintValue('cap', 'butt')] ?? 0,
|
|
379
384
|
JOIN_MAP[paintValue('join', 'miter')] ?? 0,
|
|
380
385
|
paintValue('fillRule', 'nonzero') === 'evenodd' ? 1 : 0,
|
|
@@ -383,58 +388,60 @@ function imperativeShape(shapeNode, env, tag) {
|
|
|
383
388
|
}
|
|
384
389
|
/**
|
|
385
390
|
* Lowers `updateVector(nodeRef, shapes, [x,y,w,h]?)`: fill a mutable op-tape, push it to the node, and
|
|
386
|
-
* (optionally) hint the dirty sub-rect — the imperative fast path (drag) that bypasses
|
|
387
|
-
* tables are declared
|
|
391
|
+
* (optionally) hint the dirty sub-rect — the imperative fast path (drag) that bypasses the update. The call's
|
|
392
|
+
* tables are declared here.
|
|
388
393
|
*
|
|
389
|
-
* @param
|
|
390
|
-
* @param
|
|
391
|
-
* @param
|
|
394
|
+
* @param expression The updateVector call.
|
|
395
|
+
* @param environment The expression environment.
|
|
396
|
+
* @param context The statement compiler's context.
|
|
392
397
|
*
|
|
393
398
|
* @returns The statement.
|
|
394
399
|
*/
|
|
395
|
-
function lowerUpdateVector(
|
|
400
|
+
function lowerUpdateVector(expression, environment, context) {
|
|
396
401
|
// The first argument must be a node ref, and the second an array literal of shapes.
|
|
397
|
-
const
|
|
398
|
-
const [
|
|
399
|
-
const ref =
|
|
402
|
+
const output = context.output;
|
|
403
|
+
const [refArgument, shapesArgument, dirtyArgument] = expression.arguments;
|
|
404
|
+
const ref = refArgument?.type === 'Identifier'
|
|
405
|
+
? environment.refs?.get(refArgument.name)
|
|
406
|
+
: null;
|
|
400
407
|
if (ref?.kind !== 'node') {
|
|
401
408
|
throw new Error('AOT: updateVector(ref, …) first arg must be a node ref (const r = useRef())');
|
|
402
409
|
}
|
|
403
410
|
ref.used = true;
|
|
404
|
-
if (
|
|
411
|
+
if (shapesArgument?.type !== 'ArrayExpression') {
|
|
405
412
|
throw new Error('AOT: updateVector(ref, shapes, …) shapes must be an array literal');
|
|
406
413
|
}
|
|
407
414
|
// Claim the id first so two updateVector calls in one handler get distinct locals, then lower each shape.
|
|
408
|
-
const vectorId =
|
|
415
|
+
const vectorId = output.svgCount++;
|
|
409
416
|
const update = {
|
|
410
417
|
kind: 'updateVector',
|
|
411
418
|
id: vectorId,
|
|
412
|
-
node: ref.
|
|
419
|
+
node: ref.slotName,
|
|
413
420
|
locals: [],
|
|
414
421
|
tape: [],
|
|
415
422
|
paints: [],
|
|
416
423
|
dirty: null,
|
|
417
424
|
};
|
|
418
|
-
for (const shapeNode of
|
|
419
|
-
const shape = imperativeShape(shapeNode,
|
|
425
|
+
for (const shapeNode of shapesArgument.elements) {
|
|
426
|
+
const shape = imperativeShape(shapeNode, environment, `uv${vectorId}_${update.paints.length}`);
|
|
420
427
|
update.locals.push(...shape.locals);
|
|
421
|
-
update.tape.push({ kind: 'op',
|
|
428
|
+
update.tape.push({ kind: 'op', operation: 'ER_VECTOR_OP_SHAPE' }, checkedTapeNumber(update.paints.length), ...shape.entries);
|
|
422
429
|
update.paints.push(shape.paint);
|
|
423
430
|
}
|
|
424
|
-
// Declare the op-tape and its paint table
|
|
431
|
+
// Declare the op-tape and its paint table.
|
|
425
432
|
checkVectorPaints(update.paints);
|
|
426
|
-
|
|
427
|
-
out.vectors.push({ kind: 'updateVector', update });
|
|
433
|
+
output.vectors.push({ kind: 'updateVector', update });
|
|
428
434
|
// Hint the dirty sub-rect when the call gives one.
|
|
429
|
-
if (
|
|
430
|
-
if (
|
|
435
|
+
if (dirtyArgument) {
|
|
436
|
+
if (dirtyArgument.type !== 'ArrayExpression' ||
|
|
437
|
+
dirtyArgument.elements.length < 4) {
|
|
431
438
|
throw new Error('AOT: updateVector dirtyRect must be a [x, y, w, h] array literal');
|
|
432
439
|
}
|
|
433
|
-
update.dirty =
|
|
440
|
+
update.dirty = dirtyArgument.elements.slice(0, 4).map((edgeNode, edge) => {
|
|
434
441
|
if (!edgeNode) {
|
|
435
442
|
throw aotError(`AOT: updateVector dirtyRect has an empty slot at position ${edge}`);
|
|
436
443
|
}
|
|
437
|
-
return lowerExpr(edgeNode,
|
|
444
|
+
return lowerExpr(edgeNode, environment);
|
|
438
445
|
});
|
|
439
446
|
}
|
|
440
447
|
return update;
|
|
@@ -442,30 +449,29 @@ function lowerUpdateVector(expr, env, ctx) {
|
|
|
442
449
|
/**
|
|
443
450
|
* Lowers setInterval/setTimeout(cb, ms) to a timer, and registers cb as a timer function of its own.
|
|
444
451
|
*
|
|
445
|
-
* @param
|
|
446
|
-
* @param
|
|
452
|
+
* @param expression The setInterval / setTimeout call.
|
|
453
|
+
* @param environment The expression environment.
|
|
447
454
|
* @param state The component's state table.
|
|
448
|
-
* @param
|
|
455
|
+
* @param context The statement compiler's context.
|
|
449
456
|
*
|
|
450
457
|
* @returns The timer.
|
|
451
458
|
*/
|
|
452
|
-
function lowerTimer(
|
|
453
|
-
const
|
|
454
|
-
if (!isFn(
|
|
459
|
+
function lowerTimer(expression, environment, state, context) {
|
|
460
|
+
const callback = expression.arguments[0];
|
|
461
|
+
if (!isFn(callback)) {
|
|
455
462
|
throw aotError('AOT: a setInterval/setTimeout callback must be an inline function', 'pass an inline arrow, e.g. setInterval(() => setTick((t) => t + 1), 1000).');
|
|
456
463
|
}
|
|
457
464
|
// Any delay is accepted here; the printer converts a timestamp or float one the way Flow A's setTimeout does.
|
|
458
|
-
const delay =
|
|
459
|
-
? lowerExprWide(
|
|
465
|
+
const delay = expression.arguments[1]
|
|
466
|
+
? lowerExprWide(expression.arguments[1], environment)
|
|
460
467
|
: null;
|
|
461
|
-
const isRepeating =
|
|
468
|
+
const isRepeating = expression.callee.name === 'setInterval';
|
|
462
469
|
// Reserve the timer's slot before compiling its body, which may add timers of its own.
|
|
463
|
-
const slot =
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
return { delay, isRepeating, fn: timerFnName };
|
|
470
|
+
const slot = context.output.timerFunctions.length;
|
|
471
|
+
const timerFunctionName = `timer_${slot}`;
|
|
472
|
+
context.output.timerFunctions.push({ name: timerFunctionName, callback: null });
|
|
473
|
+
context.output.timerFunctions[slot].callback = lowerHandler(callback, environment, state, context.output);
|
|
474
|
+
return { delay, isRepeating, functionName: timerFunctionName };
|
|
469
475
|
}
|
|
470
476
|
/**
|
|
471
477
|
* Inlines a handler-statement call to a helper / useCallback: binds the call's args to the helper's params
|
|
@@ -473,37 +479,37 @@ function lowerTimer(expr, env, state, ctx) {
|
|
|
473
479
|
*
|
|
474
480
|
* @param helperName The helper's name.
|
|
475
481
|
* @param helper The helper.
|
|
476
|
-
* @param
|
|
477
|
-
* @param
|
|
482
|
+
* @param callArguments The call's arguments.
|
|
483
|
+
* @param environment The expression environment.
|
|
478
484
|
* @param state The component's state table.
|
|
479
|
-
* @param
|
|
485
|
+
* @param context The statement compiler's context.
|
|
480
486
|
*
|
|
481
487
|
* @returns The helper's body as statements.
|
|
482
488
|
*/
|
|
483
|
-
function inlineHelperCall(helperName, helper,
|
|
489
|
+
function inlineHelperCall(helperName, helper, callArguments, environment, state, context) {
|
|
484
490
|
// A helper already being inlined further up this call chain is recursive.
|
|
485
|
-
|
|
486
|
-
if (
|
|
487
|
-
throw aotError(`AOT: helper "${helperName}" is recursive — can't be inlined into a handler`, 'handlers are flattened to straight-line
|
|
491
|
+
context.inlining = context.inlining ?? new Set();
|
|
492
|
+
if (context.inlining.has(helperName)) {
|
|
493
|
+
throw aotError(`AOT: helper "${helperName}" is recursive — can't be inlined into a handler`, 'handlers are flattened to straight-line code, so a helper that calls itself (directly ' +
|
|
488
494
|
'or via another helper) has no base case to unroll. Move recursive logic out of the ' +
|
|
489
495
|
'handler, or precompute the value.');
|
|
490
496
|
}
|
|
491
|
-
// Bind each passed argument to its parameter as a local holding the argument's
|
|
492
|
-
const locals = new Map(
|
|
493
|
-
helper.params.forEach((
|
|
494
|
-
if (
|
|
497
|
+
// Bind each passed argument to its parameter as a local holding the argument's expression.
|
|
498
|
+
const locals = new Map(environment.locals);
|
|
499
|
+
helper.params.forEach((parameter, parameterIndex) => {
|
|
500
|
+
if (parameter.type !== 'Identifier') {
|
|
495
501
|
throw aotError(`AOT: helper "${helperName}" must take simple (identifier) params to be inlined`, 'a helper called from a handler must use plain positional params (e.g. `(a, b) => …`) ' +
|
|
496
502
|
'— destructuring or default params in the signature are not supported.');
|
|
497
503
|
}
|
|
498
|
-
if (
|
|
499
|
-
const
|
|
500
|
-
locals.set(
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
504
|
+
if (callArguments[parameterIndex]) {
|
|
505
|
+
const argumentExpression = lowerSourcedWide(callArguments[parameterIndex], environment);
|
|
506
|
+
locals.set(parameter.name, {
|
|
507
|
+
slotKind: argumentExpression.slotKind,
|
|
508
|
+
isBoolean: argumentExpression.isBoolean,
|
|
509
|
+
isLiteral: argumentExpression.isLiteral,
|
|
510
|
+
wide: argumentExpression.wide,
|
|
511
|
+
source: argumentExpression.source,
|
|
512
|
+
expression: argumentExpression.expression,
|
|
507
513
|
});
|
|
508
514
|
}
|
|
509
515
|
});
|
|
@@ -513,361 +519,396 @@ function inlineHelperCall(helperName, helper, args, env, state, ctx) {
|
|
|
513
519
|
? body.body
|
|
514
520
|
: [{ type: 'ExpressionStatement', expression: body }];
|
|
515
521
|
// The body is spliced into the caller, so reject its `return`, even in an effect body where one is allowed.
|
|
516
|
-
const outerAllowReturn =
|
|
517
|
-
|
|
518
|
-
|
|
522
|
+
const outerAllowReturn = context.allowReturn;
|
|
523
|
+
context.inlining.add(helperName);
|
|
524
|
+
context.allowReturn = false;
|
|
519
525
|
try {
|
|
520
|
-
return lowerStmts(statements, { ...
|
|
526
|
+
return lowerStmts(statements, { ...environment, locals }, state, context);
|
|
521
527
|
}
|
|
522
528
|
finally {
|
|
523
|
-
|
|
524
|
-
|
|
529
|
+
context.allowReturn = outerAllowReturn;
|
|
530
|
+
context.inlining.delete(helperName);
|
|
525
531
|
}
|
|
526
532
|
}
|
|
527
533
|
/**
|
|
528
534
|
* Lowers one handler ExpressionStatement: a state setter, ref mutation, timer, updateVector, helper call, or
|
|
529
535
|
* Animated.*(...).start() / .stop() (see lowerHandlerExpr, the located entry point).
|
|
530
536
|
*
|
|
531
|
-
* @param
|
|
532
|
-
* @param
|
|
537
|
+
* @param expression The statement's expression.
|
|
538
|
+
* @param environment The expression environment.
|
|
533
539
|
* @param state The component's state table.
|
|
534
|
-
* @param
|
|
540
|
+
* @param context The statement compiler's context.
|
|
535
541
|
*
|
|
536
542
|
* @returns The statements.
|
|
537
543
|
*/
|
|
538
|
-
function lowerHandlerExprImpl(
|
|
539
|
-
// updateVector(ref, shapes, dirtyRect?) — imperative vector redraw (no
|
|
540
|
-
if (
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
return [lowerUpdateVector(
|
|
544
|
+
function lowerHandlerExprImpl(expression, environment, state, context) {
|
|
545
|
+
// updateVector(ref, shapes, dirtyRect?) — imperative vector redraw (no update).
|
|
546
|
+
if (expression.type === 'CallExpression' &&
|
|
547
|
+
expression.callee.type === 'Identifier' &&
|
|
548
|
+
expression.callee.name === 'updateVector') {
|
|
549
|
+
return [lowerUpdateVector(expression, environment, context)];
|
|
544
550
|
}
|
|
545
551
|
// setInterval / setTimeout(cb, ms) → register a host-tick timer (the returned id is discarded here).
|
|
546
|
-
if (
|
|
547
|
-
|
|
548
|
-
(
|
|
552
|
+
if (expression.type === 'CallExpression' &&
|
|
553
|
+
expression.callee.type === 'Identifier' &&
|
|
554
|
+
(expression.callee.name === 'setInterval' ||
|
|
555
|
+
expression.callee.name === 'setTimeout')) {
|
|
549
556
|
return [
|
|
550
557
|
{
|
|
551
558
|
kind: 'startTimer',
|
|
552
|
-
timer: lowerTimer(
|
|
559
|
+
timer: lowerTimer(expression, environment, state, context),
|
|
553
560
|
idLocal: null,
|
|
554
561
|
},
|
|
555
562
|
];
|
|
556
563
|
}
|
|
557
564
|
// clearInterval / clearTimeout(id) → deactivate the timer slot.
|
|
558
|
-
if (
|
|
559
|
-
|
|
560
|
-
(
|
|
561
|
-
|
|
562
|
-
return [
|
|
565
|
+
if (expression.type === 'CallExpression' &&
|
|
566
|
+
expression.callee.type === 'Identifier' &&
|
|
567
|
+
(expression.callee.name === 'clearInterval' ||
|
|
568
|
+
expression.callee.name === 'clearTimeout')) {
|
|
569
|
+
return [
|
|
570
|
+
{
|
|
571
|
+
kind: 'clearTimer',
|
|
572
|
+
id: asInt(lowerExpr(expression.arguments[0], environment)),
|
|
573
|
+
},
|
|
574
|
+
];
|
|
563
575
|
}
|
|
564
576
|
// `ref.current = expr` / `ref.current += expr` — a value ref write; does NOT trigger a re-render.
|
|
565
|
-
if (
|
|
566
|
-
const ref = refTarget(
|
|
577
|
+
if (expression.type === 'AssignmentExpression') {
|
|
578
|
+
const ref = refTarget(expression.left, environment);
|
|
567
579
|
if (!ref) {
|
|
568
580
|
throw new Error('AOT: the only assignment allowed in a handler is `ref.current = ...`');
|
|
569
581
|
}
|
|
570
582
|
// Lower the right side in the ref's width and check it fits; a 64-bit `%=` keeps its constant divisor.
|
|
571
|
-
const
|
|
572
|
-
const loweredValue = lowerSource(
|
|
573
|
-
const
|
|
583
|
+
const refEnvironment = ref.slotKind === 'i64' ? { ...environment, math64: true } : environment;
|
|
584
|
+
const loweredValue = lowerSource(expression.right, refEnvironment);
|
|
585
|
+
const valueExpression = loweredValue.expression;
|
|
574
586
|
const refHoldsTime = isTime(refType(ref));
|
|
575
|
-
if (
|
|
587
|
+
if (expression.operator === '/=' &&
|
|
588
|
+
(refHoldsTime || isTime64(valueExpression))) {
|
|
576
589
|
throw aotError('AOT: `/=` on a 64-bit time value is not supported', 'JS division gives a fraction; write ref.current = Math.floor(ref.current / n) for whole units.');
|
|
577
590
|
}
|
|
578
|
-
const
|
|
579
|
-
|
|
591
|
+
const wideModuloDivisor = expression.operator === '%=' &&
|
|
592
|
+
(refHoldsTime || isTime64(valueExpression))
|
|
593
|
+
? constDivisor(expression.right, environment)
|
|
580
594
|
: null;
|
|
581
|
-
storeCheck(loweredValue, ref.
|
|
595
|
+
storeCheck(loweredValue, ref.slotKind, false, ref.kind === 'value' ? ref.widened : undefined, ref.slotName, `ref "${expression.left.object.name}"`, environment);
|
|
582
596
|
// Lower whole-ref `+= -= *=`, float `/=` and float `%=` as the binary op to share its checked and float math.
|
|
583
|
-
const isWholeRef = ref.
|
|
584
|
-
const
|
|
585
|
-
const
|
|
586
|
-
(ref.
|
|
597
|
+
const isWholeRef = ref.slotKind === 'int' || ref.slotKind === 'i64';
|
|
598
|
+
const binaryOperation = { '+=': '+', '-=': '-', '*=': '*' }[expression.operator];
|
|
599
|
+
const isFloatModulo = expression.operator === '%=' &&
|
|
600
|
+
(ref.slotKind === 'float' || valueExpression.type.kind === 'float');
|
|
587
601
|
if ((isWholeRef &&
|
|
588
|
-
(
|
|
589
|
-
(
|
|
590
|
-
|
|
591
|
-
|
|
602
|
+
(binaryOperation ||
|
|
603
|
+
(expression.operator === '/=' &&
|
|
604
|
+
valueExpression.type.kind === 'float'))) ||
|
|
605
|
+
isFloatModulo) {
|
|
606
|
+
const resultExpression = lowerExprWide({
|
|
592
607
|
type: 'BinaryExpression',
|
|
593
|
-
operator:
|
|
594
|
-
left:
|
|
595
|
-
right:
|
|
596
|
-
loc:
|
|
597
|
-
},
|
|
608
|
+
operator: binaryOperation ?? expression.operator.slice(0, -1),
|
|
609
|
+
left: expression.left,
|
|
610
|
+
right: expression.right,
|
|
611
|
+
loc: expression.loc,
|
|
612
|
+
}, refEnvironment);
|
|
598
613
|
return [
|
|
599
614
|
{
|
|
600
615
|
kind: 'writeRef',
|
|
601
616
|
ref,
|
|
602
|
-
|
|
603
|
-
value: storeValue(
|
|
617
|
+
operator: '=',
|
|
618
|
+
value: storeValue(resultExpression, ref.slotKind),
|
|
604
619
|
},
|
|
605
620
|
];
|
|
606
621
|
}
|
|
607
|
-
// Whole-number `/=` and `%=` use
|
|
622
|
+
// Whole-number `/=` and `%=` use the checked helpers for JS's answer, in 64 bits on a 64-bit ref that holds no
|
|
608
623
|
// time; a constant divisor other than 0 or -1 needs no check.
|
|
609
|
-
const isWholeValue = isIntSlot(
|
|
610
|
-
(
|
|
611
|
-
const refBits = ref.
|
|
612
|
-
if (((ref.
|
|
613
|
-
(ref.
|
|
614
|
-
(
|
|
615
|
-
const staticDivisor = staticInt(
|
|
616
|
-
if (
|
|
624
|
+
const isWholeValue = isIntSlot(valueExpression.type) ||
|
|
625
|
+
(valueExpression.type.kind === 'int64' && !isTime64(valueExpression));
|
|
626
|
+
const refBits = ref.slotKind === 'i64' ? 64 : 32;
|
|
627
|
+
if (((ref.slotKind === 'int' && isIntSlot(valueExpression.type)) ||
|
|
628
|
+
(ref.slotKind === 'i64' && !refHoldsTime && isWholeValue)) &&
|
|
629
|
+
(expression.operator === '/=' || expression.operator === '%=')) {
|
|
630
|
+
const staticDivisor = staticInt(expression.right, environment);
|
|
631
|
+
if (expression.operator === '/=' &&
|
|
617
632
|
(staticDivisor === null || staticDivisor === 0 || staticDivisor === -1)) {
|
|
618
633
|
return [
|
|
619
634
|
{
|
|
620
635
|
kind: 'writeRef',
|
|
621
636
|
ref,
|
|
622
|
-
|
|
637
|
+
operator: '=',
|
|
623
638
|
value: {
|
|
624
639
|
kind: 'helper',
|
|
625
640
|
helper: 'div',
|
|
626
641
|
bits: refBits,
|
|
627
|
-
|
|
642
|
+
callArguments: [refRead(ref), valueExpression],
|
|
628
643
|
type: refType(ref),
|
|
629
644
|
},
|
|
630
645
|
},
|
|
631
646
|
];
|
|
632
647
|
}
|
|
633
|
-
if (
|
|
648
|
+
if (expression.operator === '%=' &&
|
|
634
649
|
(staticDivisor === 0 || staticDivisor === -1)) {
|
|
635
|
-
return [{ kind: 'writeRef', ref,
|
|
650
|
+
return [{ kind: 'writeRef', ref, operator: '=', value: ZERO }];
|
|
636
651
|
}
|
|
637
|
-
if (
|
|
652
|
+
if (expression.operator === '%=' && staticDivisor === null) {
|
|
638
653
|
return [
|
|
639
654
|
{
|
|
640
655
|
kind: 'writeRef',
|
|
641
656
|
ref,
|
|
642
|
-
|
|
657
|
+
operator: '=',
|
|
643
658
|
value: {
|
|
644
659
|
kind: 'helper',
|
|
645
660
|
helper: 'mod',
|
|
646
661
|
bits: refBits,
|
|
647
|
-
|
|
662
|
+
callArguments: [refRead(ref), valueExpression],
|
|
648
663
|
type: refType(ref),
|
|
649
664
|
},
|
|
650
665
|
},
|
|
651
666
|
];
|
|
652
667
|
}
|
|
653
668
|
}
|
|
654
|
-
// A 64-bit `%=` by 0 or ±1 stores 0: no remainder, JS's NaN as 0, and no
|
|
655
|
-
if (
|
|
656
|
-
|
|
669
|
+
// A 64-bit `%=` by 0 or ±1 stores 0: no remainder, JS's NaN as 0, and no overflow on INT64_MIN % -1.
|
|
670
|
+
if (wideModuloDivisor === 0 ||
|
|
671
|
+
wideModuloDivisor === 1 ||
|
|
672
|
+
wideModuloDivisor === -1) {
|
|
673
|
+
return [{ kind: 'writeRef', ref, operator: '=', value: ZERO }];
|
|
657
674
|
}
|
|
658
675
|
// Everything else is a plain assignment or compound assignment.
|
|
659
676
|
return [
|
|
660
677
|
{
|
|
661
678
|
kind: 'writeRef',
|
|
662
679
|
ref,
|
|
663
|
-
|
|
664
|
-
value:
|
|
665
|
-
? storeValue(
|
|
666
|
-
:
|
|
680
|
+
operator: expression.operator,
|
|
681
|
+
value: expression.operator === '='
|
|
682
|
+
? storeValue(valueExpression, ref.slotKind)
|
|
683
|
+
: valueExpression,
|
|
667
684
|
},
|
|
668
685
|
];
|
|
669
686
|
}
|
|
670
687
|
// `ref.current++` / `ref.current--`; a whole-number ref steps through the checked `+ 1` / `- 1`.
|
|
671
|
-
if (
|
|
672
|
-
const ref = refTarget(
|
|
688
|
+
if (expression.type === 'UpdateExpression') {
|
|
689
|
+
const ref = refTarget(expression.argument, environment);
|
|
673
690
|
if (!ref) {
|
|
674
691
|
throw new Error('AOT: the only ++/-- allowed in a handler is on `ref.current`');
|
|
675
692
|
}
|
|
676
|
-
if (ref.
|
|
693
|
+
if (ref.slotKind === 'int' || ref.slotKind === 'i64') {
|
|
677
694
|
return [
|
|
678
695
|
{
|
|
679
696
|
kind: 'writeRef',
|
|
680
697
|
ref,
|
|
681
|
-
|
|
698
|
+
operator: '=',
|
|
682
699
|
value: {
|
|
683
700
|
kind: 'helper',
|
|
684
|
-
helper:
|
|
685
|
-
bits: ref.
|
|
686
|
-
|
|
701
|
+
helper: expression.operator === '++' ? 'add' : 'sub',
|
|
702
|
+
bits: ref.slotKind === 'i64' ? 64 : 32,
|
|
703
|
+
callArguments: [
|
|
704
|
+
refRead(ref),
|
|
705
|
+
{ kind: 'number', value: 1, type: INT },
|
|
706
|
+
],
|
|
687
707
|
type: refType(ref),
|
|
688
708
|
},
|
|
689
709
|
},
|
|
690
710
|
];
|
|
691
711
|
}
|
|
692
|
-
return [{ kind: 'stepRef', ref,
|
|
712
|
+
return [{ kind: 'stepRef', ref, operator: expression.operator }];
|
|
693
713
|
}
|
|
694
714
|
// `anim.stop()` freezes each driven value; the cancel reports !finished, which ends any sequence or loop chain.
|
|
695
|
-
if (
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
isAnimatedCall(resolveAnim(
|
|
715
|
+
if (expression.type === 'CallExpression' &&
|
|
716
|
+
expression.callee.type === 'MemberExpression' &&
|
|
717
|
+
expression.callee.property.name === 'stop' &&
|
|
718
|
+
isAnimatedCall(resolveAnim(expression.callee.object, environment))) {
|
|
699
719
|
return [
|
|
700
720
|
{
|
|
701
721
|
kind: 'stopAnimations',
|
|
702
|
-
handles: [...animValues(
|
|
703
|
-
waitOwner: stopWaitOwner(
|
|
722
|
+
handles: [...animValues(expression.callee.object, environment)],
|
|
723
|
+
waitOwner: stopWaitOwner(expression.callee.object, environment, context),
|
|
704
724
|
},
|
|
705
725
|
];
|
|
706
726
|
}
|
|
707
|
-
// Animated.*(…).start(), atomic or composed, is native-driven, so it sets no state and needs no
|
|
708
|
-
if (
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
return [lowerAnimateStart(
|
|
727
|
+
// Animated.*(…).start(), atomic or composed, is native-driven, so it sets no state and needs no update.
|
|
728
|
+
if (expression.type === 'CallExpression' &&
|
|
729
|
+
expression.callee.type === 'MemberExpression' &&
|
|
730
|
+
expression.callee.property.name === 'start') {
|
|
731
|
+
return [lowerAnimateStart(expression, environment, state, context)];
|
|
712
732
|
}
|
|
713
733
|
// What is left must be a plain call: to a helper or a state setter.
|
|
714
|
-
if (
|
|
734
|
+
if (expression.type !== 'CallExpression' ||
|
|
735
|
+
expression.callee.type !== 'Identifier') {
|
|
715
736
|
throw aotError('AOT: a handler statement must be a state setter, a ref write, or Animated.timing/spring(...).start()', 'each statement in a handler must be one of: setX(value) / setX(prev => …), a ' +
|
|
716
737
|
'`ref.current = …` write, an `updateVector(…)` call, or `Animated.timing|spring(v, ' +
|
|
717
738
|
'…).start()`. Wrap conditional logic in `if (…) { … }`.');
|
|
718
739
|
}
|
|
719
740
|
// A call to a helper / useCallback (e.g. `reset();`) → inline its body here so handlers can compose logic.
|
|
720
|
-
const
|
|
721
|
-
|
|
722
|
-
|
|
741
|
+
const helperFunction = environment.helpers?.get(expression.callee.name) ??
|
|
742
|
+
environment.callbacks?.get(expression.callee.name);
|
|
743
|
+
if (helperFunction) {
|
|
744
|
+
return inlineHelperCall(expression.callee.name, helperFunction, expression.arguments, environment, state, context);
|
|
723
745
|
}
|
|
724
746
|
// A state setter. Flag the state change, which tells the caller to re-apply state once the body is done.
|
|
725
|
-
const targetState = state.bySetter.get(
|
|
747
|
+
const targetState = state.bySetter.get(expression.callee.name);
|
|
726
748
|
if (!targetState) {
|
|
727
|
-
throw aotError(`AOT: "${
|
|
728
|
-
`a ref write, updateVector(…), or Animated…start(). "${
|
|
729
|
-
`of those — arbitrary functions (fetch, console.*, helpers) can't be lowered
|
|
749
|
+
throw aotError(`AOT: "${expression.callee.name}" is not a known state setter`, `a handler can only call a setter from this component's own useState (e.g. setCount), ` +
|
|
750
|
+
`a ref write, updateVector(…), or Animated…start(). "${expression.callee.name}" isn't one ` +
|
|
751
|
+
`of those — arbitrary functions (fetch, console.*, helpers) can't be lowered.`);
|
|
730
752
|
}
|
|
731
|
-
|
|
732
|
-
const
|
|
753
|
+
context.stateChanged = true;
|
|
754
|
+
const setterArgument = expression.arguments[0];
|
|
733
755
|
if (targetState.kind === 'list') {
|
|
734
|
-
return lowerListOp(targetState,
|
|
756
|
+
return lowerListOp(targetState, setterArgument, environment);
|
|
735
757
|
}
|
|
736
758
|
// A scalar setter takes either an updater function or the new value itself.
|
|
737
|
-
if (
|
|
738
|
-
(
|
|
739
|
-
|
|
759
|
+
if (setterArgument &&
|
|
760
|
+
(setterArgument.type === 'ArrowFunctionExpression' ||
|
|
761
|
+
setterArgument.type === 'FunctionExpression')) {
|
|
740
762
|
// setState(prev => expr): bind the param to the current value, typed as a read of it, and assign the result.
|
|
741
|
-
const
|
|
742
|
-
const locals = new Map(
|
|
743
|
-
if (
|
|
744
|
-
locals.set(
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
763
|
+
const previousParameter = setterArgument.params[0]?.name;
|
|
764
|
+
const locals = new Map(environment.locals);
|
|
765
|
+
if (previousParameter) {
|
|
766
|
+
locals.set(previousParameter, {
|
|
767
|
+
slotKind: targetState.slotKind,
|
|
768
|
+
isBoolean: targetState.isBoolean,
|
|
769
|
+
...(targetState.widened === 'number' ? { isLiteral: true } : {}),
|
|
770
|
+
source: { kind: 'state', state: targetState, location: undefined },
|
|
771
|
+
expression: {
|
|
772
|
+
kind: 'state',
|
|
773
|
+
state: targetState,
|
|
774
|
+
type: stateType(targetState),
|
|
775
|
+
},
|
|
750
776
|
});
|
|
751
777
|
}
|
|
752
|
-
if (
|
|
778
|
+
if (setterArgument.body.type === 'BlockStatement') {
|
|
753
779
|
throw new Error('AOT: updater function must be a single expression (for now)');
|
|
754
780
|
}
|
|
755
|
-
return [
|
|
781
|
+
return [
|
|
782
|
+
lowerScalarSet(targetState, setterArgument.body, {
|
|
783
|
+
...environment,
|
|
784
|
+
locals,
|
|
785
|
+
}),
|
|
786
|
+
];
|
|
756
787
|
}
|
|
757
|
-
return [lowerScalarSet(targetState,
|
|
788
|
+
return [lowerScalarSet(targetState, setterArgument, environment)];
|
|
758
789
|
}
|
|
759
790
|
/** lowerHandlerExprImpl, with the expression's source location attached to any AOT error it throws. */
|
|
760
|
-
const
|
|
791
|
+
const lowerHandlerExpression = withLoc(lowerHandlerExprImpl);
|
|
761
792
|
/**
|
|
762
793
|
* Lowers a list of handler statements to the IR. This one-statement lowering runs event handlers, effect bodies
|
|
763
794
|
* (useEffect), timer callbacks, and inlined helper calls. Supports: `const x = expr` (a local, visible to later
|
|
764
795
|
* statements), `if (cond) {...} else {...}`, state setters (list setters included), ref writes, timers,
|
|
765
|
-
* updateVector() and Animated.*(...).start(). `
|
|
766
|
-
*
|
|
767
|
-
* function of its own). `
|
|
796
|
+
* updateVector() and Animated.*(...).start(). `context` accumulates `stateChanged` (→ a trailing update)
|
|
797
|
+
* and `usedReturn` (an early `return` was lowered, so the body needs a
|
|
798
|
+
* function of its own). `context.allowReturn` and `context.bodyList` mark which body a `return` may exit and which
|
|
768
799
|
* statement of it is the tail.
|
|
769
800
|
*
|
|
770
801
|
* @param statements The statements.
|
|
771
|
-
* @param
|
|
802
|
+
* @param environment The expression environment.
|
|
772
803
|
* @param state The component's state table.
|
|
773
|
-
* @param
|
|
804
|
+
* @param context The statement compiler's context.
|
|
774
805
|
*
|
|
775
806
|
* @returns The lowered statements.
|
|
776
807
|
*/
|
|
777
|
-
export function lowerStmts(statements,
|
|
808
|
+
export function lowerStmts(statements, environment, state, context) {
|
|
778
809
|
const lowered = [];
|
|
810
|
+
// What each statement lowers to is marked with where it is, once the next one starts.
|
|
811
|
+
let from = 0;
|
|
812
|
+
let location;
|
|
779
813
|
for (let statementIndex = 0; statementIndex < statements.length; statementIndex++) {
|
|
814
|
+
markLoc(lowered, from, location);
|
|
780
815
|
const statement = statements[statementIndex];
|
|
816
|
+
from = lowered.length;
|
|
817
|
+
location = startOf(statement);
|
|
781
818
|
// `const x = …` declares a local that the statements after it can read.
|
|
782
819
|
if (statement.type === 'VariableDeclaration') {
|
|
783
|
-
for (const
|
|
784
|
-
if (
|
|
820
|
+
for (const declarator of statement.declarations) {
|
|
821
|
+
if (declarator.id.type !== 'Identifier') {
|
|
785
822
|
throw new Error('AOT: destructuring a handler local is not supported');
|
|
786
823
|
}
|
|
787
|
-
if (!
|
|
824
|
+
if (!declarator.init) {
|
|
788
825
|
throw new Error('AOT: a handler local must have an initializer');
|
|
789
826
|
}
|
|
790
|
-
// `const anim = Animated.…` holds no
|
|
791
|
-
if (isAnimatedCall(
|
|
792
|
-
const locals = new Map(
|
|
793
|
-
locals.delete(
|
|
794
|
-
|
|
795
|
-
...
|
|
827
|
+
// `const anim = Animated.…` holds no value; `anim.start()`/`anim.stop()` compile the animation it names.
|
|
828
|
+
if (isAnimatedCall(declarator.init)) {
|
|
829
|
+
const locals = new Map(environment.locals);
|
|
830
|
+
locals.delete(declarator.id.name);
|
|
831
|
+
environment = {
|
|
832
|
+
...environment,
|
|
796
833
|
locals,
|
|
797
|
-
|
|
834
|
+
animationLocals: new Map(environment.animationLocals).set(declarator.id.name, declarator.init),
|
|
798
835
|
};
|
|
799
836
|
continue;
|
|
800
837
|
}
|
|
801
|
-
// A dep-driven effect's cleanup outlives the call, so the body's locals become
|
|
802
|
-
const isHoisted = Boolean(
|
|
803
|
-
const
|
|
804
|
-
? `${
|
|
805
|
-
: `
|
|
838
|
+
// A dep-driven effect's cleanup outlives the call, so the body's locals become slots.
|
|
839
|
+
const isHoisted = Boolean(context.hoist && statements === context.bodyList);
|
|
840
|
+
const localName = isHoisted
|
|
841
|
+
? `${context.hoist.prefix}${declarator.id.name}`
|
|
842
|
+
: `local_${declarator.id.name}`;
|
|
806
843
|
// `const id = setInterval/setTimeout(…)` is an int timer-id local, so a later clearInterval(id) resolves.
|
|
807
|
-
if (
|
|
808
|
-
|
|
809
|
-
(
|
|
810
|
-
|
|
844
|
+
if (declarator.init.type === 'CallExpression' &&
|
|
845
|
+
declarator.init.callee.type === 'Identifier' &&
|
|
846
|
+
(declarator.init.callee.name === 'setInterval' ||
|
|
847
|
+
declarator.init.callee.name === 'setTimeout')) {
|
|
811
848
|
// The id is only needed for a later clear*(); a mount effect drops its cleanup, so mark it used.
|
|
812
849
|
if (isHoisted) {
|
|
813
|
-
|
|
850
|
+
context.hoist.declarations.push({
|
|
851
|
+
kind: 'local',
|
|
852
|
+
name: localName,
|
|
853
|
+
slotKind: 'int',
|
|
854
|
+
});
|
|
814
855
|
}
|
|
815
856
|
lowered.push({
|
|
816
857
|
kind: 'startTimer',
|
|
817
|
-
timer: lowerTimer(
|
|
818
|
-
idLocal: { name:
|
|
858
|
+
timer: lowerTimer(declarator.init, environment, state, context),
|
|
859
|
+
idLocal: { name: localName, isHoisted },
|
|
819
860
|
});
|
|
820
|
-
|
|
821
|
-
...
|
|
822
|
-
locals: new Map(
|
|
823
|
-
|
|
824
|
-
|
|
861
|
+
environment = {
|
|
862
|
+
...environment,
|
|
863
|
+
locals: new Map(environment.locals).set(declarator.id.name, {
|
|
864
|
+
name: localName,
|
|
865
|
+
slotKind: 'int',
|
|
825
866
|
}),
|
|
826
867
|
};
|
|
827
868
|
continue;
|
|
828
869
|
}
|
|
829
870
|
// Any other initializer must lower to a number, a boolean or a string.
|
|
830
|
-
const { source:
|
|
831
|
-
if (
|
|
832
|
-
if (
|
|
833
|
-
checkConstantFits({ parts: [{ kind: 'literal', text:
|
|
871
|
+
const { source: initialSource, expression: initialExpression } = lowerSource(declarator.init, environment);
|
|
872
|
+
if (initialExpression.type.kind === 'string') {
|
|
873
|
+
if (initialExpression.kind === 'string') {
|
|
874
|
+
checkConstantFits({ parts: [{ kind: 'literal', text: initialExpression.value }] }, `local "${declarator.id.name}"`, environment);
|
|
834
875
|
}
|
|
835
876
|
// Copy into a buffer of its own; aliasing a state slot would hide a self-read like `setLabel(t + '!')`.
|
|
836
877
|
if (isHoisted) {
|
|
837
|
-
|
|
878
|
+
context.hoist.declarations.push({
|
|
838
879
|
kind: 'local',
|
|
839
|
-
name:
|
|
840
|
-
|
|
880
|
+
name: localName,
|
|
881
|
+
slotKind: 'string',
|
|
841
882
|
});
|
|
842
883
|
}
|
|
843
884
|
}
|
|
844
885
|
else {
|
|
845
|
-
const slot = slotOf(
|
|
886
|
+
const slot = slotOf(initialExpression.type);
|
|
846
887
|
if (slot !== 'int' && slot !== 'float' && slot !== 'i64') {
|
|
847
888
|
throw aotError('AOT: a handler local must hold a number, a boolean or a string', 'bind the value itself — e.g. `const id = item.id` — rather than a list item or a node ref.');
|
|
848
889
|
}
|
|
849
890
|
if (isHoisted) {
|
|
850
|
-
|
|
891
|
+
context.hoist.declarations.push({
|
|
851
892
|
kind: 'local',
|
|
852
|
-
name:
|
|
853
|
-
|
|
893
|
+
name: localName,
|
|
894
|
+
slotKind: slot,
|
|
854
895
|
});
|
|
855
896
|
}
|
|
856
897
|
}
|
|
857
898
|
lowered.push({
|
|
858
899
|
kind: 'declareLocal',
|
|
859
|
-
name:
|
|
860
|
-
|
|
900
|
+
name: localName,
|
|
901
|
+
initializer: initialExpression,
|
|
861
902
|
isHoisted,
|
|
862
|
-
|
|
903
|
+
stringCapacity: environment.caps.listStrCap,
|
|
863
904
|
});
|
|
864
|
-
// Later statements read the name as the
|
|
865
|
-
|
|
866
|
-
...
|
|
867
|
-
locals: new Map(
|
|
868
|
-
...
|
|
869
|
-
|
|
870
|
-
source:
|
|
905
|
+
// Later statements read the name as the local.
|
|
906
|
+
environment = {
|
|
907
|
+
...environment,
|
|
908
|
+
locals: new Map(environment.locals).set(declarator.id.name, {
|
|
909
|
+
...slotValueOf(initialExpression),
|
|
910
|
+
name: localName,
|
|
911
|
+
source: initialSource,
|
|
871
912
|
}),
|
|
872
913
|
};
|
|
873
914
|
}
|
|
@@ -875,15 +916,16 @@ export function lowerStmts(statements, env, state, ctx) {
|
|
|
875
916
|
}
|
|
876
917
|
// A tail `return` is the cleanup, dropped on mount and armed if dep-driven; an earlier one is a guard.
|
|
877
918
|
if (statement.type === 'ReturnStatement') {
|
|
878
|
-
if (!
|
|
919
|
+
if (!context.allowReturn) {
|
|
879
920
|
const error = aotError('AOT: `return` is only supported inside a useEffect body', 'a handler, timer, animation or inlined-helper body cannot return early — flatten the logic into if/else.');
|
|
880
921
|
if (statement.loc) {
|
|
881
922
|
error.aotLoc = statement.loc.start;
|
|
882
923
|
}
|
|
883
924
|
throw error;
|
|
884
925
|
}
|
|
885
|
-
const isTail = statements ===
|
|
886
|
-
|
|
926
|
+
const isTail = statements === context.bodyList &&
|
|
927
|
+
statementIndex === statements.length - 1;
|
|
928
|
+
if (context.dependencyDriven && isFn(statement.argument) && !isTail) {
|
|
887
929
|
const error = aotError('AOT: a useEffect cleanup must be the last statement of the effect body', 'a cleanup returned from inside an `if` would be dropped. Return `undefined` from the ' +
|
|
888
930
|
'guard and put the single `return () => …` at the end of the body.');
|
|
889
931
|
if (statement.loc) {
|
|
@@ -892,22 +934,22 @@ export function lowerStmts(statements, env, state, ctx) {
|
|
|
892
934
|
throw error;
|
|
893
935
|
}
|
|
894
936
|
if (isTail) {
|
|
895
|
-
if (
|
|
896
|
-
|
|
897
|
-
lowered.push({ kind: 'armCleanup', flag:
|
|
937
|
+
if (context.cleanup && isFn(statement.argument)) {
|
|
938
|
+
context.cleanup.emit(statement.argument, environment);
|
|
939
|
+
lowered.push({ kind: 'armCleanup', flag: context.cleanup.armed });
|
|
898
940
|
}
|
|
899
941
|
continue;
|
|
900
942
|
}
|
|
901
|
-
|
|
943
|
+
context.usedReturn = true;
|
|
902
944
|
lowered.push({ kind: 'return' });
|
|
903
945
|
continue;
|
|
904
946
|
}
|
|
905
947
|
// `if (…) { … } else { … }` lowers each branch in order: the test, then the consequent, then the alternate.
|
|
906
948
|
if (statement.type === 'IfStatement') {
|
|
907
|
-
const test = lowerExprWide(statement.test,
|
|
908
|
-
const consequent = lowerStmts(blockList(statement.consequent),
|
|
949
|
+
const test = lowerExprWide(statement.test, environment);
|
|
950
|
+
const consequent = lowerStmts(blockList(statement.consequent), environment, state, context);
|
|
909
951
|
const alternate = statement.alternate
|
|
910
|
-
? lowerStmts(blockList(statement.alternate),
|
|
952
|
+
? lowerStmts(blockList(statement.alternate), environment, state, context)
|
|
911
953
|
: null;
|
|
912
954
|
lowered.push({ kind: 'if', test, consequent, alternate });
|
|
913
955
|
continue;
|
|
@@ -919,192 +961,236 @@ export function lowerStmts(statements, env, state, ctx) {
|
|
|
919
961
|
'(for/while), switch and try/catch are not lowered — precompute values or flatten the ' +
|
|
920
962
|
'logic into if/else.');
|
|
921
963
|
}
|
|
922
|
-
lowered.push(...
|
|
964
|
+
lowered.push(...lowerHandlerExpression(statement.expression, environment, state, context));
|
|
923
965
|
}
|
|
966
|
+
markLoc(lowered, from, location);
|
|
924
967
|
return lowered;
|
|
925
968
|
}
|
|
969
|
+
/**
|
|
970
|
+
* Where a statement starts. An expression body made into a statement has no location of its own.
|
|
971
|
+
*
|
|
972
|
+
* @param statement The statement.
|
|
973
|
+
*
|
|
974
|
+
* @returns Its start, if it has one.
|
|
975
|
+
*/
|
|
976
|
+
function startOf(statement) {
|
|
977
|
+
return (statement.loc?.start ??
|
|
978
|
+
(statement.type === 'ExpressionStatement'
|
|
979
|
+
? statement.expression.loc?.start
|
|
980
|
+
: undefined));
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Marks the statements lowered from one source statement with its location. One nested inside it, or inlined
|
|
984
|
+
* from a helper, keeps its own.
|
|
985
|
+
*
|
|
986
|
+
* @param lowered The statements lowered so far.
|
|
987
|
+
* @param from The first one the source statement lowered to.
|
|
988
|
+
* @param location Where the source statement is.
|
|
989
|
+
*/
|
|
990
|
+
function markLoc(lowered, from, location) {
|
|
991
|
+
for (let index = from; index < lowered.length; index++) {
|
|
992
|
+
lowered[index].location ??= location;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
926
995
|
/**
|
|
927
996
|
* Lowers a useEffect (App or child). Two shapes:
|
|
928
|
-
* - `useEffect(fn, [])` — MOUNT-ONCE: body runs after the initial
|
|
929
|
-
* - `useEffect(fn, [dep…])` — DEP-DRIVEN: body becomes a
|
|
930
|
-
* then again from
|
|
931
|
-
* compiled WITHOUT a trailing
|
|
932
|
-
* sets happens on the next
|
|
933
|
-
* A dep-driven `return () => …` becomes `
|
|
997
|
+
* - `useEffect(fn, [])` — MOUNT-ONCE: body runs after the initial the update (in the build).
|
|
998
|
+
* - `useEffect(fn, [dep…])` — DEP-DRIVEN: body becomes a function of its own; runs once at mount,
|
|
999
|
+
* then again from the update whenever a SCALAR dep changes (compared against a stored prev). The body is
|
|
1000
|
+
* compiled WITHOUT a trailing update — it runs INSIDE an update / at mount, so re-applying state it
|
|
1001
|
+
* sets happens on the next update (one-frame), and it can never re-enter an update (no infinite loop).
|
|
1002
|
+
* A dep-driven `return () => …` becomes `effect_N_cleanup()`, run at the top of the next re-run.
|
|
934
1003
|
*
|
|
935
1004
|
* @param effect The effect.
|
|
936
|
-
* @param
|
|
1005
|
+
* @param environment The expression environment.
|
|
937
1006
|
* @param state The component's state table.
|
|
938
|
-
* @param
|
|
1007
|
+
* @param output Everything emitted so far; the effect is added to it.
|
|
939
1008
|
*/
|
|
940
|
-
export function lowerEffect(effect,
|
|
1009
|
+
export function lowerEffect(effect, environment, state, output) {
|
|
941
1010
|
// An expression body is a single statement.
|
|
942
|
-
const body = effect.
|
|
1011
|
+
const body = effect.functionNode.body;
|
|
943
1012
|
const statements = body.type === 'BlockStatement'
|
|
944
1013
|
? body.body
|
|
945
1014
|
: [{ type: 'ExpressionStatement', expression: body }];
|
|
946
|
-
// A mount-once effect (`[]`) runs once, from
|
|
947
|
-
const isMount = effect.
|
|
948
|
-
effect.
|
|
1015
|
+
// A mount-once effect (`[]`) runs once, from the build.
|
|
1016
|
+
const isMount = effect.dependencies.type === 'ArrayExpression' &&
|
|
1017
|
+
effect.dependencies.elements.length === 0;
|
|
949
1018
|
if (isMount) {
|
|
950
|
-
const
|
|
1019
|
+
const context = {
|
|
951
1020
|
stateChanged: false,
|
|
952
|
-
|
|
953
|
-
out,
|
|
1021
|
+
output,
|
|
954
1022
|
allowReturn: true,
|
|
955
1023
|
bodyList: statements,
|
|
956
1024
|
};
|
|
957
|
-
const lowered = lowerStmts(statements,
|
|
958
|
-
if (!
|
|
959
|
-
|
|
1025
|
+
const lowered = lowerStmts(statements, environment, state, context);
|
|
1026
|
+
if (!context.usedReturn) {
|
|
1027
|
+
output.mountEffects.push({
|
|
960
1028
|
kind: 'inline',
|
|
961
|
-
callback: { body: lowered, updatesState:
|
|
1029
|
+
callback: { body: lowered, updatesState: context.stateChanged },
|
|
962
1030
|
});
|
|
963
1031
|
return;
|
|
964
1032
|
}
|
|
965
1033
|
// With an early `return` the body gets its own function, so it cannot skip later mount effects.
|
|
966
|
-
const name = `
|
|
967
|
-
|
|
968
|
-
|
|
1034
|
+
const name = `effect_${output.effectCount++}`;
|
|
1035
|
+
output.effectFunctions.push({ kind: 'mount', name, body: lowered });
|
|
1036
|
+
output.mountEffects.push({
|
|
969
1037
|
kind: 'call',
|
|
970
1038
|
name,
|
|
971
|
-
updatesState:
|
|
1039
|
+
updatesState: context.stateChanged,
|
|
972
1040
|
});
|
|
973
1041
|
return;
|
|
974
1042
|
}
|
|
975
|
-
// A dep-driven effect gets its own
|
|
976
|
-
if (effect.
|
|
1043
|
+
// A dep-driven effect gets its own function; each dep must be scalar, so a change shows against a stored copy.
|
|
1044
|
+
if (effect.dependencies.type !== 'ArrayExpression') {
|
|
977
1045
|
throw aotError('AOT: a useEffect dependency list must be an array literal', 'pass `[]` (run once) or `[a, b]` (re-run when a/b change).');
|
|
978
1046
|
}
|
|
979
|
-
const effectId =
|
|
980
|
-
const
|
|
981
|
-
if (!
|
|
1047
|
+
const effectId = output.effectCount++;
|
|
1048
|
+
const dependencies = effect.dependencies.elements.map(dependencyNode => {
|
|
1049
|
+
if (!dependencyNode) {
|
|
982
1050
|
throw aotError('AOT: a useEffect dependency must be an expression');
|
|
983
1051
|
}
|
|
984
|
-
const
|
|
985
|
-
if (!['int', 'float', 'i64', 'string'].includes(
|
|
1052
|
+
const dependencyExpression = lowerExprWide(dependencyNode, environment);
|
|
1053
|
+
if (!['int', 'float', 'i64', 'string'].includes(slotValueOf(dependencyExpression).slotKind)) {
|
|
986
1054
|
throw aotError('AOT: useEffect dependencies must be scalar (number / bool / string)', 'depend on scalar state values; object/array dependencies are not yet supported.');
|
|
987
1055
|
}
|
|
988
|
-
return
|
|
1056
|
+
return dependencyExpression;
|
|
989
1057
|
});
|
|
990
1058
|
// React runs the previous cleanup before a re-run; an `armed` flag records whether the last run reached it.
|
|
991
1059
|
const lastStatement = statements[statements.length - 1];
|
|
992
1060
|
const hasCleanup = lastStatement?.type === 'ReturnStatement' && isFn(lastStatement.argument);
|
|
993
|
-
const
|
|
1061
|
+
const dependencyEffect = {
|
|
994
1062
|
id: effectId,
|
|
995
|
-
|
|
1063
|
+
dependencies,
|
|
996
1064
|
body: [],
|
|
997
1065
|
hasCleanup,
|
|
998
1066
|
cleanup: null,
|
|
999
1067
|
};
|
|
1000
|
-
const
|
|
1068
|
+
const context = {
|
|
1001
1069
|
stateChanged: false,
|
|
1002
|
-
|
|
1003
|
-
out,
|
|
1070
|
+
output,
|
|
1004
1071
|
allowReturn: true,
|
|
1005
1072
|
bodyList: statements,
|
|
1006
|
-
|
|
1073
|
+
dependencyDriven: true,
|
|
1007
1074
|
};
|
|
1008
1075
|
// With a cleanup, hoist the body's locals and lower the tail `return () => …` into `<effect>_cleanup()`.
|
|
1009
1076
|
if (hasCleanup) {
|
|
1010
|
-
|
|
1011
|
-
|
|
1077
|
+
context.hoist = {
|
|
1078
|
+
prefix: `effect${effectId}_local_`,
|
|
1079
|
+
declarations: output.effectDeclarations,
|
|
1080
|
+
};
|
|
1081
|
+
context.cleanup = {
|
|
1012
1082
|
armed: armedFlag(effectId),
|
|
1013
|
-
emit: (
|
|
1014
|
-
const cleanupBody =
|
|
1083
|
+
emit: (cleanupFunction, cleanupEnvironment) => {
|
|
1084
|
+
const cleanupBody = cleanupFunction.body;
|
|
1015
1085
|
const cleanupStatements = cleanupBody.type === 'BlockStatement'
|
|
1016
1086
|
? cleanupBody.body
|
|
1017
1087
|
: [{ type: 'ExpressionStatement', expression: cleanupBody }];
|
|
1018
|
-
const
|
|
1088
|
+
const cleanupContext = {
|
|
1019
1089
|
stateChanged: false,
|
|
1020
|
-
|
|
1021
|
-
out,
|
|
1090
|
+
output,
|
|
1022
1091
|
};
|
|
1023
|
-
|
|
1024
|
-
|
|
1092
|
+
dependencyEffect.cleanup = lowerStmts(cleanupStatements, cleanupEnvironment, state, cleanupContext);
|
|
1093
|
+
output.effectFunctions.push({
|
|
1094
|
+
kind: 'cleanup',
|
|
1095
|
+
effect: dependencyEffect,
|
|
1096
|
+
});
|
|
1025
1097
|
},
|
|
1026
1098
|
};
|
|
1027
|
-
|
|
1099
|
+
output.effectDeclarations.push({ kind: 'armed', effect: dependencyEffect });
|
|
1028
1100
|
}
|
|
1029
1101
|
// The effect function runs the last run's cleanup first, if that run armed it, then the body.
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
// A copy of each dep, taken at mount;
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1102
|
+
dependencyEffect.body = lowerStmts(statements, environment, state, context);
|
|
1103
|
+
output.effectFunctions.push({ kind: 'effect', effect: dependencyEffect });
|
|
1104
|
+
// A copy of each dep, taken at mount; the update re-runs the effect when one no longer matches its copy.
|
|
1105
|
+
output.effectDeclarations.push({ kind: 'deps', effect: dependencyEffect });
|
|
1106
|
+
output.mountEffects.push({ kind: 'depEffect', effect: dependencyEffect });
|
|
1107
|
+
output.dependencyEffects.push(dependencyEffect);
|
|
1036
1108
|
}
|
|
1037
1109
|
/**
|
|
1038
1110
|
* Lowers an event handler to a callback. Its first parameter is the event, whose touch fields read
|
|
1039
1111
|
* EREventData; a PanResponder callback's second is RN's gestureState (see lowerGestureField).
|
|
1040
1112
|
*
|
|
1041
|
-
* @param
|
|
1042
|
-
* @param
|
|
1113
|
+
* @param functionNode The handler.
|
|
1114
|
+
* @param environment The expression environment.
|
|
1043
1115
|
* @param state The component's state table.
|
|
1044
|
-
* @param
|
|
1116
|
+
* @param output Everything emitted so far.
|
|
1045
1117
|
* @param pan The PanResponder the handler belongs to, if any.
|
|
1046
1118
|
*
|
|
1047
1119
|
* @returns The callback.
|
|
1048
1120
|
*/
|
|
1049
|
-
export function lowerHandler(
|
|
1121
|
+
export function lowerHandler(functionNode, environment, state, output, pan = null) {
|
|
1050
1122
|
// An expression body is a single statement.
|
|
1051
|
-
const body =
|
|
1123
|
+
const body = functionNode.body;
|
|
1052
1124
|
const statements = body.type === 'BlockStatement'
|
|
1053
1125
|
? body.body
|
|
1054
1126
|
: [{ type: 'ExpressionStatement', expression: body }];
|
|
1055
1127
|
// The first parameter is the event (fields map to EREventData); a PanResponder's second is RN's gestureState.
|
|
1056
|
-
const
|
|
1057
|
-
|
|
1058
|
-
? fnNode.params[1].name
|
|
1128
|
+
const eventParameter = functionNode.params[0]?.type === 'Identifier'
|
|
1129
|
+
? functionNode.params[0].name
|
|
1059
1130
|
: null;
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1131
|
+
const gestureParameter = pan && functionNode.params[1]?.type === 'Identifier'
|
|
1132
|
+
? functionNode.params[1].name
|
|
1133
|
+
: null;
|
|
1134
|
+
let handlerEnvironment = environment;
|
|
1135
|
+
if (eventParameter) {
|
|
1136
|
+
handlerEnvironment = { ...handlerEnvironment, event: eventParameter };
|
|
1063
1137
|
}
|
|
1064
|
-
if (
|
|
1065
|
-
|
|
1138
|
+
if (gestureParameter) {
|
|
1139
|
+
handlerEnvironment = {
|
|
1140
|
+
...handlerEnvironment,
|
|
1141
|
+
gesture: gestureParameter,
|
|
1142
|
+
pan,
|
|
1143
|
+
};
|
|
1066
1144
|
}
|
|
1067
1145
|
// The body re-applies state-dependent props once at its end if it set state.
|
|
1068
|
-
const
|
|
1146
|
+
const context = { stateChanged: false, output };
|
|
1069
1147
|
return {
|
|
1070
|
-
body: lowerStmts(statements,
|
|
1071
|
-
updatesState:
|
|
1148
|
+
body: lowerStmts(statements, handlerEnvironment, state, context),
|
|
1149
|
+
updatesState: context.stateChanged,
|
|
1072
1150
|
};
|
|
1073
1151
|
}
|
|
1074
1152
|
/**
|
|
1075
1153
|
* Lowers a value-callback (e.g., Switch onValueChange) — binds its first param to `valueCode`, not an event.
|
|
1076
1154
|
*
|
|
1077
|
-
* @param
|
|
1078
|
-
* @param
|
|
1079
|
-
* @param env The expression environment.
|
|
1155
|
+
* @param functionNode The callback.
|
|
1156
|
+
* @param environment The expression environment.
|
|
1080
1157
|
* @param state The component's state table.
|
|
1081
|
-
* @param
|
|
1082
|
-
* @param
|
|
1083
|
-
* @param
|
|
1084
|
-
* @param
|
|
1158
|
+
* @param output Everything emitted so far.
|
|
1159
|
+
* @param slotKind The value's kind.
|
|
1160
|
+
* @param isBoolean The value is a boolean, so text prints it as true/false.
|
|
1161
|
+
* @param valueExpression The value as a typed expression.
|
|
1162
|
+
* @param secondValueExpression The second parameter's value as a typed expression, if any.
|
|
1085
1163
|
*
|
|
1086
1164
|
* @returns The callback.
|
|
1087
1165
|
*/
|
|
1088
|
-
export function lowerValueHandler(
|
|
1166
|
+
export function lowerValueHandler(functionNode, environment, state, output, slotKind, isBoolean, valueExpression, secondValueExpression) {
|
|
1089
1167
|
// A <Switch> hands its callback a boolean; carry that so `'on=' + v` prints true/false, not 1/0.
|
|
1090
|
-
const
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1168
|
+
const valueParameter = functionNode.params[0]?.type === 'Identifier'
|
|
1169
|
+
? functionNode.params[0].name
|
|
1170
|
+
: null;
|
|
1171
|
+
const locals = new Map(environment.locals);
|
|
1172
|
+
if (valueParameter) {
|
|
1173
|
+
locals.set(valueParameter, {
|
|
1174
|
+
slotKind,
|
|
1175
|
+
isBoolean,
|
|
1176
|
+
expression: valueExpression,
|
|
1177
|
+
});
|
|
1094
1178
|
}
|
|
1095
|
-
// A RANGE <Dial>'s second parameter binds the same way, lowering to
|
|
1096
|
-
const
|
|
1097
|
-
|
|
1098
|
-
|
|
1179
|
+
// A RANGE <Dial>'s second parameter binds the same way, lowering to the event's value_start with no object on device.
|
|
1180
|
+
const secondParameter = functionNode.params[1]?.type === 'Identifier'
|
|
1181
|
+
? functionNode.params[1].name
|
|
1182
|
+
: null;
|
|
1183
|
+
if (secondParameter && secondValueExpression) {
|
|
1184
|
+
locals.set(secondParameter, { slotKind, expression: secondValueExpression });
|
|
1099
1185
|
}
|
|
1100
1186
|
// Lower the body (an expression body is a single statement); it re-applies state once if it set any.
|
|
1101
|
-
const
|
|
1102
|
-
const body =
|
|
1187
|
+
const context = { stateChanged: false, output };
|
|
1188
|
+
const body = functionNode.body;
|
|
1103
1189
|
const statements = body.type === 'BlockStatement'
|
|
1104
1190
|
? body.body
|
|
1105
1191
|
: [{ type: 'ExpressionStatement', expression: body }];
|
|
1106
1192
|
return {
|
|
1107
|
-
body: lowerStmts(statements, { ...
|
|
1108
|
-
updatesState:
|
|
1193
|
+
body: lowerStmts(statements, { ...environment, locals }, state, context),
|
|
1194
|
+
updatesState: context.stateChanged,
|
|
1109
1195
|
};
|
|
1110
1196
|
}
|