embedded-react 0.19.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +8 -3
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +180 -90
  5. package/aot/compiler/animations.mjs +568 -317
  6. package/aot/compiler/boot.mjs +97 -35
  7. package/aot/compiler/branch-merge.mjs +4 -3
  8. package/aot/compiler/check.mjs +70 -18
  9. package/aot/compiler/collect.mjs +787 -288
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +439 -268
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/diagnostics.mjs +22 -25
  14. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  15. package/aot/compiler/elements/button.mjs +182 -0
  16. package/aot/compiler/elements/dial.mjs +91 -77
  17. package/aot/compiler/elements/flat-list.mjs +24 -22
  18. package/aot/compiler/elements/image-background.mjs +76 -0
  19. package/aot/compiler/elements/image.mjs +52 -50
  20. package/aot/compiler/elements/modal.mjs +31 -30
  21. package/aot/compiler/elements/props.mjs +61 -26
  22. package/aot/compiler/elements/rewrite.mjs +118 -0
  23. package/aot/compiler/elements/section-list.mjs +370 -0
  24. package/aot/compiler/elements/switch.mjs +42 -34
  25. package/aot/compiler/elements/text-input.mjs +34 -32
  26. package/aot/compiler/expressions.mjs +1685 -392
  27. package/aot/compiler/handlers.mjs +2029 -547
  28. package/aot/compiler/helper-values.mjs +312 -0
  29. package/aot/compiler/ir/functions.mjs +11 -1
  30. package/aot/compiler/ir/nodes.mjs +43 -4
  31. package/aot/compiler/ir/reads.mjs +223 -22
  32. package/aot/compiler/ir/text.mjs +5 -1
  33. package/aot/compiler/ir/types.mjs +26 -14
  34. package/aot/compiler/ir/vectors.mjs +5 -1
  35. package/aot/compiler/kept-states.mjs +177 -0
  36. package/aot/compiler/keyboard.mjs +17 -7
  37. package/aot/compiler/list-items.mjs +226 -0
  38. package/aot/compiler/list-order.mjs +586 -0
  39. package/aot/compiler/list-split.mjs +259 -0
  40. package/aot/compiler/loops.mjs +582 -0
  41. package/aot/compiler/lower.mjs +246 -111
  42. package/aot/compiler/native-files.mjs +439 -0
  43. package/aot/compiler/native-header.mjs +335 -0
  44. package/aot/compiler/native-modules.mjs +1173 -0
  45. package/aot/compiler/nodes.mjs +195 -172
  46. package/aot/compiler/options.mjs +37 -31
  47. package/aot/compiler/out.mjs +32 -23
  48. package/aot/compiler/package/animations.mjs +779 -0
  49. package/aot/compiler/package/arena.mjs +252 -0
  50. package/aot/compiler/package/assemble.mjs +175 -0
  51. package/aot/compiler/package/bytecode.mjs +315 -0
  52. package/aot/compiler/package/checks.mjs +1026 -0
  53. package/aot/compiler/package/codegen.mjs +2168 -0
  54. package/aot/compiler/package/enums.mjs +148 -0
  55. package/aot/compiler/package/format.mjs +771 -0
  56. package/aot/compiler/package/layouts.mjs +136 -0
  57. package/aot/compiler/package/lower.mjs +2103 -0
  58. package/aot/compiler/package/props.mjs +118 -0
  59. package/aot/compiler/package/string-caps.mjs +515 -0
  60. package/aot/compiler/package/tables.mjs +127 -0
  61. package/aot/compiler/package/vectors.mjs +502 -0
  62. package/aot/compiler/package/writer.mjs +686 -0
  63. package/aot/compiler/pan-responder.mjs +69 -61
  64. package/aot/compiler/parse.mjs +23 -21
  65. package/aot/compiler/record-shapes.mjs +246 -0
  66. package/aot/compiler/records.mjs +654 -0
  67. package/aot/compiler/reducers.mjs +272 -0
  68. package/aot/compiler/static-eval.mjs +340 -30
  69. package/aot/compiler/style-text.mjs +178 -174
  70. package/aot/compiler/svg.mjs +480 -251
  71. package/aot/compiler/switch-cases.mjs +198 -0
  72. package/aot/compiler/tagged-records.mjs +450 -0
  73. package/aot/compiler/typing.mjs +307 -0
  74. package/aot/compiler/updaters.mjs +320 -0
  75. package/aot/compiler/window-dimensions.mjs +94 -0
  76. package/aot/compiler/worked-values.mjs +319 -0
  77. package/aot/demo-file.mjs +30 -0
  78. package/aot/screenshot-smoke.mjs +54 -40
  79. package/aot/style-map.mjs +152 -92
  80. package/assets/bake-font.mjs +123 -77
  81. package/assets/bake-image.mjs +39 -22
  82. package/assets/bake-svg.mjs +361 -262
  83. package/assets/build-builtin-font.mjs +11 -7
  84. package/assets/emit-c.mjs +77 -58
  85. package/assets/emit-container.mjs +123 -37
  86. package/assets/emit-pack.mjs +77 -67
  87. package/assets/font-config.mjs +9 -6
  88. package/assets/font-sizes.mjs +163 -123
  89. package/assets/glyph-coverage.mjs +74 -54
  90. package/assets/index.mjs +31 -9
  91. package/assets/rasterize.mjs +126 -99
  92. package/assets/svg-loader.mjs +8 -8
  93. package/build.mjs +23 -13
  94. package/cli.mjs +402 -172
  95. package/compile-bin.mjs +3 -3
  96. package/flash.mjs +537 -0
  97. package/hotreload/dev-device.mjs +90 -33
  98. package/hotreload/pack-app.mjs +55 -43
  99. package/hotreload/pack-split.mjs +62 -64
  100. package/hotreload/package-transfer.mjs +175 -0
  101. package/hotreload/protocol.mjs +50 -37
  102. package/hotreload/uploader.mjs +38 -27
  103. package/hotreload/watch-tree.mjs +19 -19
  104. package/pack-container.mjs +37 -21
  105. package/package.json +7 -8
  106. package/persist-transform.mjs +34 -28
  107. package/qjsc-wasm.mjs +25 -14
  108. package/sim/embedded-react.cjs +1 -1
  109. package/sim/embedded-react.js +1 -1
  110. package/sim/embedded-react.wasm +0 -0
  111. package/sim/index.html +94 -85
  112. package/sim-server.mjs +113 -94
  113. package/src/embedded-react/Animated.js +24 -24
  114. package/src/embedded-react/Dimensions.js +37 -0
  115. package/src/embedded-react/Easing.js +1 -1
  116. package/src/embedded-react/FlatList.js +3 -3
  117. package/src/embedded-react/LayoutAnimation.js +8 -5
  118. package/src/embedded-react/PanResponder.js +75 -71
  119. package/src/embedded-react/Pressable.js +2 -8
  120. package/src/embedded-react/SectionList.js +7 -9
  121. package/src/embedded-react/StyleSheet.js +4 -3
  122. package/src/embedded-react/TouchableOpacity.js +13 -8
  123. package/src/embedded-react/index.d.ts +88 -8
  124. package/src/embedded-react/index.js +2 -0
  125. package/src/embedded-react/layout-anim-config.js +10 -4
  126. package/src/embedded-react/list-child.js +5 -5
  127. package/src/embedded-react/named-colors.js +1 -1
  128. package/src/embedded-react/nativeModule.js +462 -0
  129. package/src/embedded-react/split-style.js +11 -11
  130. package/src/embedded-react/svg-ops.js +760 -593
  131. package/src/embedded-react/useHostValue.js +3 -3
  132. package/src/embedded-react/usePersistentState.js +6 -6
  133. package/src/embedded-react/warn-props.js +1 -1
  134. package/src/host-config.js +60 -53
  135. package/src/props.js +49 -41
  136. package/src/renderer.js +7 -4
  137. package/aot/compiler/assemble.mjs +0 -944
  138. package/aot/compiler/c/animations.mjs +0 -436
  139. package/aot/compiler/c/boot.mjs +0 -127
  140. package/aot/compiler/c/expressions.mjs +0 -270
  141. package/aot/compiler/c/functions.mjs +0 -268
  142. package/aot/compiler/c/keyboard.mjs +0 -96
  143. package/aot/compiler/c/nodes.mjs +0 -303
  144. package/aot/compiler/c/pan-responder.mjs +0 -136
  145. package/aot/compiler/c/statements.mjs +0 -319
  146. package/aot/compiler/c/text.mjs +0 -83
  147. package/aot/compiler/c/vectors.mjs +0 -381
  148. package/aot/compiler/c-syntax.mjs +0 -127
@@ -0,0 +1,127 @@
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
+ * The tables a program's pieces share: strings, constants and prop sets, each entry stored once.
18
+ */
19
+ /*----------------------------------------------------------------------------------------------------------------------
20
+ - Imports
21
+ ---------------------------------------------------------------------------------------------------------------------*/
22
+ import { aotError } from '../diagnostics.mjs';
23
+ import { MAX_IMPORTS } from '../native-modules.mjs';
24
+ import { NONE } from './format.mjs';
25
+ /*----------------------------------------------------------------------------------------------------------------------
26
+ - Classes
27
+ ---------------------------------------------------------------------------------------------------------------------*/
28
+ /**
29
+ * The shared tables, filled as the program is lowered.
30
+ *
31
+ * @class ProgramTables
32
+ */
33
+ export class ProgramTables {
34
+ strings = [];
35
+ const32 = [];
36
+ const64 = [];
37
+ propSets = [];
38
+ imports = [];
39
+ stringIndex = new Map();
40
+ importIndex = new Map();
41
+ const32Index = new Map();
42
+ const64Index = new Map();
43
+ propSetIndex = new Map();
44
+ /**
45
+ * A string's index, adding it the first time.
46
+ *
47
+ * @param text The string.
48
+ *
49
+ * @returns Its index.
50
+ */
51
+ string(text) {
52
+ return intern(this.stringIndex, this.strings, text);
53
+ }
54
+ /**
55
+ * A 32-bit constant's index, adding it the first time.
56
+ *
57
+ * @param bits Its bits, as an unsigned number.
58
+ *
59
+ * @returns Its index.
60
+ */
61
+ constant32(bits) {
62
+ return intern(this.const32Index, this.const32, bits >>> 0);
63
+ }
64
+ /**
65
+ * A 64-bit constant's index, adding it the first time.
66
+ *
67
+ * @param bits Its bits.
68
+ *
69
+ * @returns Its index.
70
+ */
71
+ constant64(bits) {
72
+ return intern(this.const64Index, this.const64, BigInt.asUintN(64, bits));
73
+ }
74
+ /**
75
+ * A native import's index, adding it the first time.
76
+ *
77
+ * @param nativeImport The method.
78
+ *
79
+ * @returns Its index.
80
+ */
81
+ nativeImport(nativeImport) {
82
+ const key = `${nativeImport.name} ${nativeImport.version} ${nativeImport.signature}`;
83
+ if (!this.importIndex.has(key) && this.imports.length === MAX_IMPORTS) {
84
+ throw aotError(`AOT: an app may call at most ${MAX_IMPORTS} native methods`, 'call fewer native methods, or have one method do more. An optional module counts every method its ' +
85
+ 'interface declares.');
86
+ }
87
+ return intern(this.importIndex, this.imports, {
88
+ name: this.string(nativeImport.name),
89
+ version: nativeImport.version,
90
+ signature: this.string(nativeImport.signature),
91
+ module: nativeImport.module ?? NONE,
92
+ }, key);
93
+ }
94
+ /**
95
+ * A prop set's index, adding it the first time.
96
+ *
97
+ * @param entries The set's entries, applied in order.
98
+ *
99
+ * @returns Its index, or NONE for an empty set.
100
+ */
101
+ propSet(entries) {
102
+ return entries.length
103
+ ? intern(this.propSetIndex, this.propSets, entries, JSON.stringify(entries))
104
+ : NONE;
105
+ }
106
+ }
107
+ /*----------------------------------------------------------------------------------------------------------------------
108
+ - Implementation
109
+ ---------------------------------------------------------------------------------------------------------------------*/
110
+ /**
111
+ * A value's index in a table, adding it the first time.
112
+ *
113
+ * @param index The table's index, by key.
114
+ * @param table The table.
115
+ * @param value The value.
116
+ * @param key Its key, when it isn't the value itself.
117
+ *
118
+ * @returns Its index.
119
+ */
120
+ function intern(index, table, value, key = value) {
121
+ let at = index.get(key);
122
+ if (at === undefined) {
123
+ at = table.push(value) - 1;
124
+ index.set(key, at);
125
+ }
126
+ return at;
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
+ }