embedded-react 0.18.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/NOTICE +1 -1
- package/README.md +8 -4
- 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 +55 -41
- 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 +14 -10
- package/assets/emit-c.mjs +83 -62
- package/assets/emit-container.mjs +124 -38
- 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 +16 -14
- package/cli.mjs +359 -172
- package/compile-bin.mjs +5 -5
- 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 +51 -38
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +31 -23
- package/package.json +11 -12
- 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 +4 -4
- 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
|
@@ -0,0 +1,502 @@
|
|
|
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
|
+
* <Svg> drawings in a package.
|
|
18
|
+
*
|
|
19
|
+
* A drawing baked at build time is a tape, committed when its node is built. A drawing state drives is a tape
|
|
20
|
+
* too, its constant floats and paints in place: the update copies it into the scratch, works out each float,
|
|
21
|
+
* paint field and gradient field state drives, and commits it with er_node_set_vector_ops(). An updateVector call does the same from the code that calls it, then sets the
|
|
22
|
+
* damage hint.
|
|
23
|
+
*/
|
|
24
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
25
|
+
- Imports
|
|
26
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
27
|
+
import { aotError } from '../diagnostics.mjs';
|
|
28
|
+
import { MAX_GRADIENT_STOPS } from '../ir/vectors.mjs';
|
|
29
|
+
import { GradientField, MathFn, PaintField } from './format.mjs';
|
|
30
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
31
|
+
- Constants
|
|
32
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
33
|
+
/** The engine's vector opcodes, as their ER_VECTOR_OP_* names. */
|
|
34
|
+
const VECTOR_OPCODES = {
|
|
35
|
+
ER_VECTOR_OP_SHAPE: 0,
|
|
36
|
+
ER_VECTOR_OP_MOVE: 1,
|
|
37
|
+
ER_VECTOR_OP_LINE: 2,
|
|
38
|
+
ER_VECTOR_OP_QUAD: 3,
|
|
39
|
+
ER_VECTOR_OP_CUBIC: 4,
|
|
40
|
+
ER_VECTOR_OP_ARC: 5,
|
|
41
|
+
ER_VECTOR_OP_CLOSE: 6,
|
|
42
|
+
};
|
|
43
|
+
const CHAIN_OPCODES = { '+': 'FADD', '-': 'FSUB', '*': 'FMUL' };
|
|
44
|
+
/** Pi, as a float. */
|
|
45
|
+
const FLOAT_PI = Math.fround(Math.PI);
|
|
46
|
+
/** An arc's 0° points up, 90° from the math's; and the degrees in half a turn. */
|
|
47
|
+
const ARC_ZERO_DEGREES = 90;
|
|
48
|
+
const HALF_TURN_DEGREES = 180;
|
|
49
|
+
/** The bound a damage hint's edges are kept within. */
|
|
50
|
+
const DIRTY_BOUND = 1e9;
|
|
51
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
52
|
+
- Classes
|
|
53
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
54
|
+
/**
|
|
55
|
+
* The program's tapes, each made once, and the node each updateVector call draws on.
|
|
56
|
+
*
|
|
57
|
+
* @class VectorPlan
|
|
58
|
+
*/
|
|
59
|
+
export class VectorPlan {
|
|
60
|
+
tapes = [];
|
|
61
|
+
arena;
|
|
62
|
+
made = new Map();
|
|
63
|
+
/** Each node ref's node handle, by its slot, for the updateVector calls. */
|
|
64
|
+
refNodes = new Map();
|
|
65
|
+
constructor(arena) {
|
|
66
|
+
this.arena = arena;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A baked drawing's tape.
|
|
70
|
+
*
|
|
71
|
+
* @param tables The drawing.
|
|
72
|
+
*
|
|
73
|
+
* @returns The tape, or null for an empty drawing, which leaves its node blank.
|
|
74
|
+
*/
|
|
75
|
+
tableTape(tables) {
|
|
76
|
+
if (!tables.ops.length) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return this.tape(tables, () => ({
|
|
80
|
+
floats: tables.ops,
|
|
81
|
+
paints: tables.paints.map(bakedPaint),
|
|
82
|
+
gradients: tables.gradients.map(bakedGradient),
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A state-driven drawing's tape: what state drives is 0 until code works it out.
|
|
87
|
+
*
|
|
88
|
+
* @param vector The drawing.
|
|
89
|
+
*
|
|
90
|
+
* @returns The tape.
|
|
91
|
+
*/
|
|
92
|
+
drawingTape(vector) {
|
|
93
|
+
return this.scratchTape(vector, () => ({
|
|
94
|
+
floats: vector.tape.map(constantFloat),
|
|
95
|
+
paints: vector.paints.map(drawingPaint),
|
|
96
|
+
gradients: vector.gradients.map(drawingGradient),
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* An updateVector call's tape.
|
|
101
|
+
*
|
|
102
|
+
* @param update The call.
|
|
103
|
+
*
|
|
104
|
+
* @returns The tape.
|
|
105
|
+
*/
|
|
106
|
+
updateTape(update) {
|
|
107
|
+
return this.scratchTape(update, () => ({
|
|
108
|
+
floats: update.tape.map(constantFloat),
|
|
109
|
+
paints: update.paints.map(bakedPaint),
|
|
110
|
+
gradients: [],
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
/** A tape code copies into the scratch, which must hold it. */
|
|
114
|
+
scratchTape(key, make) {
|
|
115
|
+
const index = this.tape(key, make);
|
|
116
|
+
this.arena.tapeScratch(this.tapes[index].floats.length);
|
|
117
|
+
return index;
|
|
118
|
+
}
|
|
119
|
+
tape(key, make) {
|
|
120
|
+
const existing = this.made.get(key);
|
|
121
|
+
if (existing !== undefined) {
|
|
122
|
+
return existing;
|
|
123
|
+
}
|
|
124
|
+
const index = this.tapes.push(make()) - 1;
|
|
125
|
+
this.made.set(key, index);
|
|
126
|
+
return index;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
130
|
+
- Tables
|
|
131
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
132
|
+
/**
|
|
133
|
+
* A baked paint as the tape holds it.
|
|
134
|
+
*
|
|
135
|
+
* @param paint The paint.
|
|
136
|
+
*
|
|
137
|
+
* @returns The record.
|
|
138
|
+
*/
|
|
139
|
+
const bakedPaint = (paint) => ({
|
|
140
|
+
fill: paint.fill >>> 0,
|
|
141
|
+
stroke: paint.stroke >>> 0,
|
|
142
|
+
strokeWidth: paint.strokeWidth,
|
|
143
|
+
miter: paint.miter,
|
|
144
|
+
cap: paint.cap,
|
|
145
|
+
join: paint.join,
|
|
146
|
+
fillRule: paint.fillRule,
|
|
147
|
+
fillGradient: paint.fillGradient,
|
|
148
|
+
strokeGradient: paint.strokeGradient,
|
|
149
|
+
});
|
|
150
|
+
/**
|
|
151
|
+
* A baked gradient as the tape holds it, as printVectorGradient prints it. The check pass keeps a <Svg>'s own
|
|
152
|
+
* gradients to 2 to 8 stops, but an <Svg source> file's may have fewer, which the runtime doesn't load.
|
|
153
|
+
*
|
|
154
|
+
* @param gradient The gradient.
|
|
155
|
+
*
|
|
156
|
+
* @returns The record.
|
|
157
|
+
*/
|
|
158
|
+
function bakedGradient(gradient) {
|
|
159
|
+
const stops = (gradient.stops || []).slice(0, MAX_GRADIENT_STOPS);
|
|
160
|
+
if (stops.length < 2) {
|
|
161
|
+
throw aotError(`AOT: an <Svg> gradient has ${stops.length} stop${stops.length === 1 ? '' : 's'}, but a gradient needs at least two`, 'give the gradient in the SVG file two stops, or paint a solid color.');
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
type: gradient.type | 0,
|
|
165
|
+
stops: stops.map(stop => ({
|
|
166
|
+
color: stop.color >>> 0,
|
|
167
|
+
position: stop.offset,
|
|
168
|
+
})),
|
|
169
|
+
ax: gradient.ax || 0,
|
|
170
|
+
ay: gradient.ay || 0,
|
|
171
|
+
bx: gradient.bx || 0,
|
|
172
|
+
by: gradient.by || 0,
|
|
173
|
+
radius: gradient.r || 0,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Whether a tape slot is the same on every update.
|
|
178
|
+
*
|
|
179
|
+
* @param value The slot.
|
|
180
|
+
*
|
|
181
|
+
* @returns Whether it is.
|
|
182
|
+
*/
|
|
183
|
+
const isConstant = (value) => value.kind === 'op' || value.kind === 'number' || value.kind === 'fullTurn';
|
|
184
|
+
/**
|
|
185
|
+
* A tape slot as the tape holds it: its value when it is constant, else 0.
|
|
186
|
+
*
|
|
187
|
+
* @param value The slot.
|
|
188
|
+
*
|
|
189
|
+
* @returns The float.
|
|
190
|
+
*/
|
|
191
|
+
function constantFloat(value) {
|
|
192
|
+
switch (value.kind) {
|
|
193
|
+
case 'op':
|
|
194
|
+
return VECTOR_OPCODES[value.operation];
|
|
195
|
+
case 'number':
|
|
196
|
+
return value.value;
|
|
197
|
+
case 'fullTurn':
|
|
198
|
+
return Math.fround(2 * FLOAT_PI);
|
|
199
|
+
default:
|
|
200
|
+
return 0;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const constantColor = (color) => color.kind === 'static' ? color.argb >>> 0 : 0;
|
|
204
|
+
const constantGradient = (ref) => ref.when ? 0 : ref.index;
|
|
205
|
+
/**
|
|
206
|
+
* A state-driven drawing's paint as the tape holds it, what state drives left 0.
|
|
207
|
+
*
|
|
208
|
+
* @param paint The paint.
|
|
209
|
+
*
|
|
210
|
+
* @returns The record.
|
|
211
|
+
*/
|
|
212
|
+
const drawingPaint = (paint) => ({
|
|
213
|
+
fill: constantColor(paint.fill),
|
|
214
|
+
stroke: constantColor(paint.stroke),
|
|
215
|
+
strokeWidth: constantFloat(paint.strokeWidth),
|
|
216
|
+
miter: paint.miter,
|
|
217
|
+
cap: paint.cap,
|
|
218
|
+
join: paint.join,
|
|
219
|
+
fillRule: paint.fillRule,
|
|
220
|
+
fillGradient: constantGradient(paint.fillGradient),
|
|
221
|
+
strokeGradient: constantGradient(paint.strokeGradient),
|
|
222
|
+
});
|
|
223
|
+
/**
|
|
224
|
+
* A state-driven drawing's gradient as the tape holds it, what state drives left 0.
|
|
225
|
+
*
|
|
226
|
+
* @param gradient The gradient.
|
|
227
|
+
*
|
|
228
|
+
* @returns The record.
|
|
229
|
+
*/
|
|
230
|
+
const drawingGradient = (gradient) => ({
|
|
231
|
+
type: gradient.type,
|
|
232
|
+
stops: gradient.stops.map(stop => ({
|
|
233
|
+
color: constantColor(stop.color),
|
|
234
|
+
position: constantFloat(stop.offset),
|
|
235
|
+
})),
|
|
236
|
+
ax: constantFloat(gradient.ax),
|
|
237
|
+
ay: constantFloat(gradient.ay),
|
|
238
|
+
bx: constantFloat(gradient.bx),
|
|
239
|
+
by: constantFloat(gradient.by),
|
|
240
|
+
radius: constantFloat(gradient.r),
|
|
241
|
+
});
|
|
242
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
243
|
+
- Code
|
|
244
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
245
|
+
/**
|
|
246
|
+
* A tape slot worked out as a float.
|
|
247
|
+
*
|
|
248
|
+
* @param codegen The function's codegen.
|
|
249
|
+
* @param value The slot.
|
|
250
|
+
* @param locals The drawing's locals, worked out already.
|
|
251
|
+
*
|
|
252
|
+
* @returns A float register. A local's is its own: never write to it.
|
|
253
|
+
*/
|
|
254
|
+
function tapeValue(codegen, value, locals) {
|
|
255
|
+
const { builder } = codegen;
|
|
256
|
+
const result = () => builder.register('float');
|
|
257
|
+
switch (value.kind) {
|
|
258
|
+
case 'op':
|
|
259
|
+
case 'number':
|
|
260
|
+
case 'fullTurn':
|
|
261
|
+
return codegen.load('float', constantFloat(value));
|
|
262
|
+
case 'float':
|
|
263
|
+
return codegen.valueAs(value.value, 'float');
|
|
264
|
+
case 'local': {
|
|
265
|
+
const local = locals.get(value.name);
|
|
266
|
+
if (!local) {
|
|
267
|
+
throw new Error(`AOT internal: no tape local "${value.name}"`);
|
|
268
|
+
}
|
|
269
|
+
return local;
|
|
270
|
+
}
|
|
271
|
+
case 'chain': {
|
|
272
|
+
// Left to right.
|
|
273
|
+
let sum = tapeValue(codegen, value.first, locals);
|
|
274
|
+
for (const term of value.rest) {
|
|
275
|
+
const next = result();
|
|
276
|
+
builder.abc(CHAIN_OPCODES[term.operator], next.index, sum.index, tapeValue(codegen, term.value, locals).index);
|
|
277
|
+
sum = next;
|
|
278
|
+
}
|
|
279
|
+
return sum;
|
|
280
|
+
}
|
|
281
|
+
case 'radians': {
|
|
282
|
+
// (degrees - 90) * pi / 180
|
|
283
|
+
const radians = result();
|
|
284
|
+
builder.abc('FSUB', radians.index, tapeValue(codegen, value.degrees, locals).index, codegen.load('float', ARC_ZERO_DEGREES).index);
|
|
285
|
+
builder.abc('FMUL', radians.index, radians.index, codegen.load('float', FLOAT_PI).index);
|
|
286
|
+
builder.abc('FDIV', radians.index, radians.index, codegen.load('float', HALF_TURN_DEGREES).index);
|
|
287
|
+
return radians;
|
|
288
|
+
}
|
|
289
|
+
case 'clampRadius': {
|
|
290
|
+
// min(max(radius, 0), side / 2), passing over a NaN, where FMATH would keep it.
|
|
291
|
+
const radius = tapeValue(codegen, value.radius, locals);
|
|
292
|
+
const zero = codegen.load('float', 0);
|
|
293
|
+
const above = builder.register('bool');
|
|
294
|
+
builder.abc('FLT', above.index, zero.index, radius.index);
|
|
295
|
+
const atLeastZero = codegen.choose(above, () => radius, () => zero, 'float');
|
|
296
|
+
const half = result();
|
|
297
|
+
builder.abc('FMUL', half.index, tapeValue(codegen, value.side, locals).index, codegen.load('float', 0.5).index);
|
|
298
|
+
const below = builder.register('bool');
|
|
299
|
+
builder.abc('FLT', below.index, half.index, atLeastZero.index);
|
|
300
|
+
return codegen.choose(below, () => half, () => atLeastZero, 'float');
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Works out a drawing's locals, each after the ones before it.
|
|
306
|
+
*
|
|
307
|
+
* @param codegen The function's codegen.
|
|
308
|
+
* @param locals The locals.
|
|
309
|
+
*
|
|
310
|
+
* @returns Each one's register, by name.
|
|
311
|
+
*/
|
|
312
|
+
function drawingLocals(codegen, locals) {
|
|
313
|
+
const registers = new Map();
|
|
314
|
+
for (const local of locals) {
|
|
315
|
+
registers.set(local.name, tapeValue(codegen, local.value, registers));
|
|
316
|
+
}
|
|
317
|
+
return registers;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Sets each tape slot state drives in the scratch.
|
|
321
|
+
*
|
|
322
|
+
* @param codegen The function's codegen.
|
|
323
|
+
* @param tape The tape's slots.
|
|
324
|
+
* @param locals The drawing's locals.
|
|
325
|
+
*/
|
|
326
|
+
function setTape(codegen, tape, locals) {
|
|
327
|
+
tape.forEach((value, slot) => {
|
|
328
|
+
if (!isConstant(value)) {
|
|
329
|
+
codegen.scoped(() => codegen.builder.abx('VSET', tapeValue(codegen, value, locals).index, slot));
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Sets each paint and gradient field state drives in the scratch.
|
|
335
|
+
*
|
|
336
|
+
* @param codegen The function's codegen.
|
|
337
|
+
* @param vector The drawing.
|
|
338
|
+
* @param locals The drawing's locals.
|
|
339
|
+
*
|
|
340
|
+
* @returns Whether it set any, so the commit takes the scratch's paints.
|
|
341
|
+
*/
|
|
342
|
+
function setPaints(codegen, vector, locals) {
|
|
343
|
+
const { builder } = codegen;
|
|
344
|
+
let set = false;
|
|
345
|
+
const field = (opcode, index, code, value) => {
|
|
346
|
+
set = true;
|
|
347
|
+
codegen.scoped(() => builder.abc(opcode, value().index, index, code));
|
|
348
|
+
};
|
|
349
|
+
const color = (paintColor) => () => codegen.convert(codegen.propRegister(paintColor.value), 'int');
|
|
350
|
+
const float = (value) => () => tapeValue(codegen, value, locals);
|
|
351
|
+
vector.paints.forEach((paint, index) => {
|
|
352
|
+
if (paint.fill.kind === 'state') {
|
|
353
|
+
field('VPAINT', index, PaintField.fill, color(paint.fill));
|
|
354
|
+
}
|
|
355
|
+
if (paint.stroke.kind === 'state') {
|
|
356
|
+
field('VPAINT', index, PaintField.stroke, color(paint.stroke));
|
|
357
|
+
}
|
|
358
|
+
if (!isConstant(paint.strokeWidth)) {
|
|
359
|
+
field('VPAINT', index, PaintField.strokeWidth, float(paint.strokeWidth));
|
|
360
|
+
}
|
|
361
|
+
const gradients = [
|
|
362
|
+
[paint.fillGradient, PaintField.fillGradient],
|
|
363
|
+
[paint.strokeGradient, PaintField.strokeGradient],
|
|
364
|
+
];
|
|
365
|
+
for (const [ref, code] of gradients) {
|
|
366
|
+
const { when } = ref;
|
|
367
|
+
if (when) {
|
|
368
|
+
// ((test) ? index : 0), or the test negated.
|
|
369
|
+
field('VPAINT', index, code, () => codegen.choose(codegen.condition(when.test), () => codegen.load('int', when.negate ? 0 : ref.index), () => codegen.load('int', when.negate ? ref.index : 0), 'int'));
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
vector.gradients.forEach((gradient, index) => {
|
|
374
|
+
const geometry = [
|
|
375
|
+
[gradient.ax, GradientField.ax],
|
|
376
|
+
[gradient.ay, GradientField.ay],
|
|
377
|
+
[gradient.bx, GradientField.bx],
|
|
378
|
+
[gradient.by, GradientField.by],
|
|
379
|
+
[gradient.r, GradientField.radius],
|
|
380
|
+
];
|
|
381
|
+
for (const [value, code] of geometry) {
|
|
382
|
+
if (!isConstant(value)) {
|
|
383
|
+
field('VGRAD', index, code, float(value));
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
gradient.stops.forEach((stop, stopIndex) => {
|
|
387
|
+
const at = GradientField.stops + 2 * stopIndex;
|
|
388
|
+
if (stop.color.kind === 'state') {
|
|
389
|
+
field('VGRAD', index, at, color(stop.color));
|
|
390
|
+
}
|
|
391
|
+
if (!isConstant(stop.offset)) {
|
|
392
|
+
field('VGRAD', index, at + 1, float(stop.offset));
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
return set;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Builds a state-driven drawing from state and commits it to its node, with
|
|
400
|
+
* er_node_set_vector_ops().
|
|
401
|
+
*
|
|
402
|
+
* @param codegen The update function's codegen.
|
|
403
|
+
* @param vector The drawing.
|
|
404
|
+
* @param plan The program's tapes.
|
|
405
|
+
* @param node Loads the node's handle.
|
|
406
|
+
*/
|
|
407
|
+
export function commitDrawing(codegen, vector, plan, node) {
|
|
408
|
+
const { builder } = codegen;
|
|
409
|
+
const tape = plan.drawingTape(vector);
|
|
410
|
+
codegen.scoped(() => {
|
|
411
|
+
builder.abx('VTAPE', 0, tape);
|
|
412
|
+
const locals = drawingLocals(codegen, vector.locals);
|
|
413
|
+
setTape(codegen, vector.tape, locals);
|
|
414
|
+
const ownPaints = setPaints(codegen, vector, locals);
|
|
415
|
+
builder.abx(ownPaints ? 'VCOMMITP' : 'VCOMMIT', node().index, tape);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* An updateVector call: its tape worked out and handed to the node, then the damage hint.
|
|
420
|
+
*
|
|
421
|
+
* @param codegen The function's codegen.
|
|
422
|
+
* @param update The call.
|
|
423
|
+
* @param plan The program's tapes.
|
|
424
|
+
*/
|
|
425
|
+
export function updateVector(codegen, update, plan) {
|
|
426
|
+
const { builder } = codegen;
|
|
427
|
+
// A ref no node holds draws nothing.
|
|
428
|
+
const handle = plan.refNodes.get(update.node);
|
|
429
|
+
if (!handle) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const tape = plan.updateTape(update);
|
|
433
|
+
codegen.scoped(() => {
|
|
434
|
+
builder.abx('VTAPE', 0, tape);
|
|
435
|
+
const locals = drawingLocals(codegen, update.locals);
|
|
436
|
+
setTape(codegen, update.tape, locals);
|
|
437
|
+
const node = builder.register('node');
|
|
438
|
+
builder.abx('GET64', node.index, handle.index);
|
|
439
|
+
builder.abx('VCOMMIT', node.index, tape);
|
|
440
|
+
const { dirty } = update;
|
|
441
|
+
if (!dirty) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (dirty.some(edge => edge.type.kind === 'float')) {
|
|
445
|
+
floatDirty(codegen, node, dirty);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const edges = builder.consecutive(['int', 'int', 'int', 'int']);
|
|
449
|
+
dirty.forEach((edge, index) => codegen.scoped(() => builder.abc('MOVE', edges[index].index, codegen.valueAs(edge, 'int').index)));
|
|
450
|
+
builder.abc('VDIRTY', node.index, edges[0].index);
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* A damage hint worked out in floats: none when an edge isn't finite, else rounded
|
|
455
|
+
* out to whole pixels, each edge bounded.
|
|
456
|
+
*
|
|
457
|
+
* @param codegen The function's codegen.
|
|
458
|
+
* @param node The node.
|
|
459
|
+
* @param dirty The x, y, width and height.
|
|
460
|
+
*/
|
|
461
|
+
function floatDirty(codegen, node, dirty) {
|
|
462
|
+
const { builder } = codegen;
|
|
463
|
+
const skip = builder.label();
|
|
464
|
+
const [x, y, width, height] = dirty.map(edge => codegen.valueAs(edge, 'float'));
|
|
465
|
+
// A value is finite when it less itself is 0.
|
|
466
|
+
const zero = codegen.load('float', 0);
|
|
467
|
+
for (const edge of [x, y, width, height]) {
|
|
468
|
+
codegen.scoped(() => {
|
|
469
|
+
const difference = builder.register('float');
|
|
470
|
+
const finite = builder.register('bool');
|
|
471
|
+
builder.abc('FSUB', difference.index, edge.index, edge.index);
|
|
472
|
+
builder.abc('FEQ', finite.index, difference.index, zero.index);
|
|
473
|
+
builder.branch('JF', skip, finite);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
const sum = (left, right) => {
|
|
477
|
+
const total = builder.register('float');
|
|
478
|
+
builder.abc('FADD', total.index, left.index, right.index);
|
|
479
|
+
return total;
|
|
480
|
+
};
|
|
481
|
+
const low = codegen.load('float', -DIRTY_BOUND);
|
|
482
|
+
const high = codegen.load('float', DIRTY_BOUND);
|
|
483
|
+
const corners = [
|
|
484
|
+
[x, MathFn.floor],
|
|
485
|
+
[y, MathFn.floor],
|
|
486
|
+
[sum(x, width), MathFn.ceil],
|
|
487
|
+
[sum(y, height), MathFn.ceil],
|
|
488
|
+
].map(([value, rounding]) => {
|
|
489
|
+
const edge = builder.register('float');
|
|
490
|
+
builder.abc('FMATH', edge.index, value.index, value.index, rounding);
|
|
491
|
+
builder.abc('FMATH', edge.index, edge.index, low.index, MathFn.max);
|
|
492
|
+
builder.abc('FMATH', edge.index, edge.index, high.index, MathFn.min);
|
|
493
|
+
return codegen.convert(edge, 'int');
|
|
494
|
+
});
|
|
495
|
+
const rect = builder.consecutive(['int', 'int', 'int', 'int']);
|
|
496
|
+
builder.abc('MOVE', rect[0].index, corners[0].index);
|
|
497
|
+
builder.abc('MOVE', rect[1].index, corners[1].index);
|
|
498
|
+
builder.abc('SUB', rect[2].index, corners[2].index, corners[0].index);
|
|
499
|
+
builder.abc('SUB', rect[3].index, corners[3].index, corners[1].index);
|
|
500
|
+
builder.abc('VDIRTY', node.index, rect[0].index);
|
|
501
|
+
builder.place(skip);
|
|
502
|
+
}
|