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.
Files changed (116) hide show
  1. package/README.md +7 -3
  2. package/aot/compile.mjs +178 -58
  3. package/aot/compiler/animations.mjs +290 -281
  4. package/aot/compiler/boot.mjs +70 -33
  5. package/aot/compiler/branch-merge.mjs +4 -3
  6. package/aot/compiler/check.mjs +9 -9
  7. package/aot/compiler/collect.mjs +324 -205
  8. package/aot/compiler/control-flow.mjs +209 -197
  9. package/aot/compiler/diagnostics.mjs +22 -25
  10. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  11. package/aot/compiler/elements/dial.mjs +91 -77
  12. package/aot/compiler/elements/flat-list.mjs +24 -22
  13. package/aot/compiler/elements/image.mjs +51 -50
  14. package/aot/compiler/elements/modal.mjs +31 -30
  15. package/aot/compiler/elements/props.mjs +23 -23
  16. package/aot/compiler/elements/switch.mjs +42 -34
  17. package/aot/compiler/elements/text-input.mjs +34 -32
  18. package/aot/compiler/expressions.mjs +372 -333
  19. package/aot/compiler/handlers.mjs +473 -387
  20. package/aot/compiler/ir/functions.mjs +11 -1
  21. package/aot/compiler/ir/nodes.mjs +4 -4
  22. package/aot/compiler/ir/reads.mjs +19 -19
  23. package/aot/compiler/ir/types.mjs +14 -14
  24. package/aot/compiler/ir/vectors.mjs +5 -1
  25. package/aot/compiler/keyboard.mjs +17 -7
  26. package/aot/compiler/lower.mjs +77 -80
  27. package/aot/compiler/nodes.mjs +169 -152
  28. package/aot/compiler/options.mjs +35 -30
  29. package/aot/compiler/out.mjs +18 -22
  30. package/aot/compiler/package/animations.mjs +669 -0
  31. package/aot/compiler/package/arena.mjs +252 -0
  32. package/aot/compiler/package/assemble.mjs +177 -0
  33. package/aot/compiler/package/bytecode.mjs +286 -0
  34. package/aot/compiler/package/checks.mjs +1022 -0
  35. package/aot/compiler/package/codegen.mjs +1329 -0
  36. package/aot/compiler/package/enums.mjs +138 -0
  37. package/aot/compiler/package/format.mjs +760 -0
  38. package/aot/compiler/package/layouts.mjs +136 -0
  39. package/aot/compiler/package/lower.mjs +1606 -0
  40. package/aot/compiler/package/props.mjs +118 -0
  41. package/aot/compiler/package/tables.mjs +103 -0
  42. package/aot/compiler/package/vectors.mjs +502 -0
  43. package/aot/compiler/package/writer.mjs +664 -0
  44. package/aot/compiler/pan-responder.mjs +69 -61
  45. package/aot/compiler/parse.mjs +21 -21
  46. package/aot/compiler/static-eval.mjs +25 -23
  47. package/aot/compiler/style-text.mjs +161 -172
  48. package/aot/compiler/svg.mjs +477 -251
  49. package/aot/screenshot-smoke.mjs +54 -40
  50. package/aot/style-map.mjs +102 -90
  51. package/assets/bake-font.mjs +123 -77
  52. package/assets/bake-image.mjs +39 -22
  53. package/assets/bake-svg.mjs +361 -262
  54. package/assets/build-builtin-font.mjs +11 -7
  55. package/assets/emit-c.mjs +77 -58
  56. package/assets/emit-container.mjs +123 -37
  57. package/assets/emit-pack.mjs +77 -67
  58. package/assets/font-config.mjs +9 -6
  59. package/assets/font-sizes.mjs +163 -123
  60. package/assets/glyph-coverage.mjs +74 -54
  61. package/assets/index.mjs +31 -9
  62. package/assets/rasterize.mjs +126 -99
  63. package/assets/svg-loader.mjs +8 -8
  64. package/build.mjs +14 -12
  65. package/cli.mjs +359 -172
  66. package/compile-bin.mjs +3 -3
  67. package/flash.mjs +537 -0
  68. package/hotreload/dev-device.mjs +90 -33
  69. package/hotreload/pack-app.mjs +55 -43
  70. package/hotreload/pack-split.mjs +46 -37
  71. package/hotreload/package-transfer.mjs +175 -0
  72. package/hotreload/protocol.mjs +50 -37
  73. package/hotreload/uploader.mjs +38 -27
  74. package/hotreload/watch-tree.mjs +19 -19
  75. package/pack-container.mjs +28 -20
  76. package/package.json +7 -8
  77. package/persist-transform.mjs +34 -28
  78. package/qjsc-wasm.mjs +25 -14
  79. package/sim/embedded-react.cjs +1 -1
  80. package/sim/embedded-react.js +1 -1
  81. package/sim/embedded-react.wasm +0 -0
  82. package/sim/index.html +94 -85
  83. package/sim-server.mjs +101 -82
  84. package/src/embedded-react/Animated.js +24 -24
  85. package/src/embedded-react/Easing.js +1 -1
  86. package/src/embedded-react/FlatList.js +3 -3
  87. package/src/embedded-react/LayoutAnimation.js +8 -5
  88. package/src/embedded-react/PanResponder.js +75 -71
  89. package/src/embedded-react/Pressable.js +2 -8
  90. package/src/embedded-react/SectionList.js +6 -6
  91. package/src/embedded-react/StyleSheet.js +4 -3
  92. package/src/embedded-react/TouchableOpacity.js +13 -8
  93. package/src/embedded-react/index.d.ts +5 -5
  94. package/src/embedded-react/layout-anim-config.js +10 -4
  95. package/src/embedded-react/list-child.js +5 -5
  96. package/src/embedded-react/named-colors.js +1 -1
  97. package/src/embedded-react/split-style.js +11 -11
  98. package/src/embedded-react/svg-ops.js +760 -593
  99. package/src/embedded-react/useHostValue.js +3 -3
  100. package/src/embedded-react/usePersistentState.js +6 -6
  101. package/src/embedded-react/warn-props.js +1 -1
  102. package/src/host-config.js +60 -53
  103. package/src/props.js +49 -41
  104. package/src/renderer.js +7 -4
  105. package/aot/compiler/assemble.mjs +0 -944
  106. package/aot/compiler/c/animations.mjs +0 -436
  107. package/aot/compiler/c/boot.mjs +0 -127
  108. package/aot/compiler/c/expressions.mjs +0 -270
  109. package/aot/compiler/c/functions.mjs +0 -268
  110. package/aot/compiler/c/keyboard.mjs +0 -96
  111. package/aot/compiler/c/nodes.mjs +0 -303
  112. package/aot/compiler/c/pan-responder.mjs +0 -136
  113. package/aot/compiler/c/statements.mjs +0 -319
  114. package/aot/compiler/c/text.mjs +0 -83
  115. package/aot/compiler/c/vectors.mjs +0 -381
  116. package/aot/compiler/c-syntax.mjs +0 -127
@@ -0,0 +1,1022 @@
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
+ * A layout's check: the program the loader runs to learn whether a layout takes the screen. It is what the compile
18
+ * assumed of its panel, lowered to the stack machine in erb_format.h.
19
+ *
20
+ * The compiler runs the same program to find the layouts: exactly for one size (runCheck), and over a range of
21
+ * sizes (checkOverSizes), which gives a verdict for all of them at once. Both follow erb_verify.c's run_check().
22
+ */
23
+ /*----------------------------------------------------------------------------------------------------------------------
24
+ - Imports
25
+ ---------------------------------------------------------------------------------------------------------------------*/
26
+ import { CHECK_ALWAYS, CHECK_BUDGET, CHECK_LOCALS, CHECK_STACK, CheckDivide, CheckOp, MathFn, } from './format.mjs';
27
+ /*----------------------------------------------------------------------------------------------------------------------
28
+ - Constants
29
+ ---------------------------------------------------------------------------------------------------------------------*/
30
+ /** The most layouts a package may hold. An app that changes at more sizes fails the build instead of growing. */
31
+ export const MAX_LAYOUTS = 16;
32
+ /** What rounding adds before it floors: a half rounds up, as the VM's does. */
33
+ const ROUND_HALF_UP = 0.5;
34
+ /** The most steps a check may take, so a full package's checks always fit the loader's budget. */
35
+ const MAX_CHECK_STEPS = CHECK_BUDGET / MAX_LAYOUTS;
36
+ /** How far a fraction may stray from the build's: as c/boot.mts's guard allows, for a libm function's last digit. */
37
+ const FLOAT_TOLERANCE = 1e-9;
38
+ /** The most a pushi holds: its argument is 24 bits, signed. */
39
+ const PUSHI_MAX = 0x7fffff;
40
+ const INT_MAX = 2147483647;
41
+ const INT_MIN = -2147483648;
42
+ /** The IR's whole-number divisions, by helper name. */
43
+ const DIVISIONS = {
44
+ div: CheckDivide.trunc,
45
+ floordiv: CheckDivide.floor,
46
+ ceildiv: CheckDivide.ceil,
47
+ rounddiv: CheckDivide.round,
48
+ mod: CheckDivide.mod,
49
+ };
50
+ /*----------------------------------------------------------------------------------------------------------------------
51
+ - Lowering
52
+ ---------------------------------------------------------------------------------------------------------------------*/
53
+ /** Why a value can't be a check's: the check falls back to the exact size. */
54
+ class NotCheckable extends Error {
55
+ }
56
+ /**
57
+ * Lowers what one compile assumed to its check.
58
+ *
59
+ * @class CheckLowering
60
+ */
61
+ class CheckLowering {
62
+ steps = [];
63
+ reads = { width: false, height: false };
64
+ locals = new Map();
65
+ /** A boot string is one of the app's literals, so a check compares them as numbers. '' is false, as in JS. */
66
+ strings = new Map([['', 0]]);
67
+ depth = 0;
68
+ /**
69
+ * Adds a step, tracking the stack it uses.
70
+ *
71
+ * @param opcode The op.
72
+ * @param operand Its argument.
73
+ * @param pops What it pops.
74
+ * @param pushes What it pushes.
75
+ */
76
+ step(opcode, operand, pops, pushes) {
77
+ this.depth += pushes - pops;
78
+ // One step stays free for the end.
79
+ if (this.depth > CHECK_STACK || this.steps.length >= MAX_CHECK_STEPS - 1) {
80
+ throw new NotCheckable();
81
+ }
82
+ this.steps.push({ opcode, operand });
83
+ }
84
+ /**
85
+ * Pushes a number, in one word when it fits.
86
+ *
87
+ * @param value The number.
88
+ */
89
+ number(value) {
90
+ if (Number.isInteger(value) &&
91
+ Math.abs(value) <= PUSHI_MAX &&
92
+ !Object.is(value, -0)) {
93
+ this.step(CheckOp.pushi, value, 0, 1);
94
+ }
95
+ else {
96
+ this.step(CheckOp.pushk, value, 0, 1);
97
+ }
98
+ }
99
+ /**
100
+ * Pushes a string, as the number it is interned as.
101
+ *
102
+ * @param text The string.
103
+ */
104
+ string(text) {
105
+ let id = this.strings.get(text);
106
+ if (id === undefined) {
107
+ id = this.strings.size;
108
+ this.strings.set(text, id);
109
+ }
110
+ this.number(id);
111
+ }
112
+ /**
113
+ * Pushes a boot slot's value. Each is worked out once, in a local.
114
+ *
115
+ * @param slot The slot.
116
+ */
117
+ slot(slot) {
118
+ const { initializer } = slot;
119
+ if (initializer.kind === 'screen') {
120
+ this.reads[initializer.dimension] = true;
121
+ this.step(initializer.dimension === 'width' ? CheckOp.width : CheckOp.height, 0, 0, 1);
122
+ return;
123
+ }
124
+ let local = this.locals.get(slot);
125
+ if (local === undefined) {
126
+ local = this.locals.size;
127
+ if (local >= CHECK_LOCALS) {
128
+ throw new NotCheckable();
129
+ }
130
+ // Taken first: the expression's own slots take locals too.
131
+ this.locals.set(slot, local);
132
+ this.expr(initializer.expression);
133
+ this.step(CheckOp.store, local, 1, 0);
134
+ }
135
+ this.step(CheckOp.load, local, 0, 1);
136
+ }
137
+ /**
138
+ * Adds a two-operand step over two expressions.
139
+ *
140
+ * @param opcode The op.
141
+ * @param left The first operand.
142
+ * @param right The second.
143
+ * @param operand Its argument.
144
+ */
145
+ binary(opcode, left, right, operand = 0) {
146
+ this.expr(left);
147
+ this.expr(right);
148
+ this.step(opcode, operand, 2, 1);
149
+ }
150
+ /**
151
+ * Pushes an expression's value. Anything but what boot can work out is refused.
152
+ *
153
+ * @param expression The expression.
154
+ */
155
+ expr(expression) {
156
+ switch (expression.kind) {
157
+ case 'number':
158
+ this.number(expression.value);
159
+ return;
160
+ case 'string':
161
+ this.string(expression.value);
162
+ return;
163
+ case 'boolean':
164
+ this.number(expression.value ? 1 : 0);
165
+ return;
166
+ case 'boot':
167
+ this.slot(expression.slot);
168
+ return;
169
+ case 'pi':
170
+ this.number(Math.PI);
171
+ return;
172
+ case 'toFloat':
173
+ case 'wholeRound':
174
+ this.expr(expression.operand);
175
+ return;
176
+ case 'unary':
177
+ this.expr(expression.operand);
178
+ if (expression.operator === '-')
179
+ this.step(CheckOp.neg, 0, 1, 1);
180
+ if (expression.operator === '!')
181
+ this.step(CheckOp.not, 0, 1, 1);
182
+ return;
183
+ case 'helper':
184
+ this.helper(expression);
185
+ return;
186
+ case 'arithmetic':
187
+ if (expression.operator === '%') {
188
+ this.binary(CheckOp.math, expression.left, expression.right, MathFn.mod);
189
+ }
190
+ else {
191
+ const ops = { '+': CheckOp.add, '-': CheckOp.sub, '*': CheckOp.mul };
192
+ this.binary(ops[expression.operator], expression.left, expression.right);
193
+ }
194
+ return;
195
+ case 'floatDivide':
196
+ this.binary(CheckOp.div, expression.left, expression.right);
197
+ return;
198
+ case 'floatMath':
199
+ this.math(expression.mathFunction, expression.callArguments);
200
+ return;
201
+ case 'trunc':
202
+ case 'toInt':
203
+ this.expr(expression.operand);
204
+ this.step(CheckOp.toint, 0, 1, 1);
205
+ return;
206
+ case 'compare': {
207
+ const ops = {
208
+ '<': CheckOp.lt,
209
+ '>': CheckOp.gt,
210
+ '<=': CheckOp.le,
211
+ '>=': CheckOp.ge,
212
+ '==': CheckOp.eq,
213
+ '!=': CheckOp.ne,
214
+ };
215
+ this.binary(ops[expression.operator], expression.left, expression.right);
216
+ return;
217
+ }
218
+ case 'stringEquals':
219
+ this.binary(expression.operator === '==' ? CheckOp.eq : CheckOp.ne, expression.left, expression.right);
220
+ return;
221
+ case 'logical':
222
+ this.binary(expression.operator === '&&' ? CheckOp.and : CheckOp.or, expression.left, expression.right);
223
+ return;
224
+ case 'conditional':
225
+ this.expr(expression.test);
226
+ this.expr(expression.consequent);
227
+ this.expr(expression.alternate);
228
+ this.step(CheckOp.select, 0, 3, 1);
229
+ return;
230
+ default:
231
+ throw new NotCheckable();
232
+ }
233
+ }
234
+ /**
235
+ * Pushes a Math function's value.
236
+ *
237
+ * @param mathFunction The function's name.
238
+ * @param callArguments Its operands.
239
+ */
240
+ math(mathFunction, callArguments) {
241
+ callArguments.forEach(argument => this.expr(argument));
242
+ this.step(CheckOp.math, MathFn[mathFunction], callArguments.length, 1);
243
+ }
244
+ /**
245
+ * Pushes a saturating whole-number helper's value: the sum in doubles, cut to the int range, is the same. A
246
+ * 64-bit one is not something a check holds.
247
+ *
248
+ * @param expression The helper.
249
+ */
250
+ helper(expression) {
251
+ if (expression.bits !== 32) {
252
+ throw new NotCheckable();
253
+ }
254
+ const [left, right] = expression.callArguments;
255
+ const { helper } = expression;
256
+ switch (helper) {
257
+ case 'min':
258
+ case 'max':
259
+ this.math(helper, expression.callArguments);
260
+ return;
261
+ case 'div':
262
+ case 'floordiv':
263
+ case 'ceildiv':
264
+ case 'rounddiv':
265
+ case 'mod':
266
+ this.binary(CheckOp.idiv, left, right, DIVISIONS[helper]);
267
+ return;
268
+ case 'abs':
269
+ this.math('abs', expression.callArguments);
270
+ break;
271
+ case 'neg':
272
+ this.expr(left);
273
+ this.step(CheckOp.neg, 0, 1, 1);
274
+ break;
275
+ case 'add':
276
+ this.binary(CheckOp.add, left, right);
277
+ break;
278
+ case 'sub':
279
+ this.binary(CheckOp.sub, left, right);
280
+ break;
281
+ default:
282
+ this.binary(CheckOp.mul, left, right);
283
+ }
284
+ this.step(CheckOp.toint, 0, 1, 1);
285
+ }
286
+ /**
287
+ * Pushes whether one assumption holds.
288
+ *
289
+ * @param slot The slot it reads.
290
+ * @param value What the slot had on the compile's panel.
291
+ */
292
+ holds(slot, value) {
293
+ this.slot(slot);
294
+ if (typeof value === 'string') {
295
+ this.string(value);
296
+ this.step(CheckOp.eq, 0, 2, 1);
297
+ }
298
+ else if (typeof value === 'boolean') {
299
+ if (!value)
300
+ this.step(CheckOp.not, 0, 1, 1);
301
+ }
302
+ else if (slot.type.kind === 'float') {
303
+ this.number(value);
304
+ this.step(CheckOp.sub, 0, 2, 1);
305
+ this.step(CheckOp.math, MathFn.abs, 1, 1);
306
+ this.number(FLOAT_TOLERANCE * (1 + Math.abs(value)));
307
+ this.step(CheckOp.le, 0, 2, 1);
308
+ }
309
+ else {
310
+ this.number(value);
311
+ this.step(CheckOp.eq, 0, 2, 1);
312
+ }
313
+ }
314
+ /** Pops two truths, and pushes whether both hold. */
315
+ both() {
316
+ this.step(CheckOp.and, 0, 2, 1);
317
+ }
318
+ /** Ends the check. */
319
+ end() {
320
+ this.steps.push({ opcode: CheckOp.end, operand: 0 });
321
+ }
322
+ }
323
+ /**
324
+ * The check that takes one size only.
325
+ *
326
+ * @param screen The size.
327
+ * @param why The expression that makes it so.
328
+ *
329
+ * @returns The check.
330
+ */
331
+ function exactCheck(screen, why) {
332
+ const { pushi, width, height, eq, and, end } = CheckOp;
333
+ return {
334
+ steps: [
335
+ { opcode: width, operand: 0 },
336
+ { opcode: pushi, operand: screen.width },
337
+ { opcode: eq, operand: 0 },
338
+ { opcode: height, operand: 0 },
339
+ { opcode: pushi, operand: screen.height },
340
+ { opcode: eq, operand: 0 },
341
+ { opcode: and, operand: 0 },
342
+ { opcode: end, operand: 0 },
343
+ ],
344
+ reads: { width: true, height: true },
345
+ exactBecause: why,
346
+ };
347
+ }
348
+ /**
349
+ * Lowers what a compile assumed of its panel to the layout's check.
350
+ *
351
+ * @param assumptions What it assumed.
352
+ * @param screen The panel it was compiled for.
353
+ *
354
+ * @returns The check.
355
+ */
356
+ export function lowerCheck(assumptions, screen) {
357
+ if (!assumptions.length) {
358
+ return {
359
+ steps: null,
360
+ reads: { width: false, height: false },
361
+ exactBecause: null,
362
+ };
363
+ }
364
+ const lowering = new CheckLowering();
365
+ for (const [index, assumption] of assumptions.entries()) {
366
+ if (assumption.kind === 'panel') {
367
+ return exactCheck(screen, assumption.source ?? 'a value');
368
+ }
369
+ try {
370
+ lowering.holds(assumption.slot, assumption.value);
371
+ if (index)
372
+ lowering.both();
373
+ }
374
+ catch (error) {
375
+ if (error instanceof NotCheckable) {
376
+ return exactCheck(screen, assumption.source ?? assumption.slot.field);
377
+ }
378
+ throw error;
379
+ }
380
+ }
381
+ lowering.end();
382
+ return { steps: lowering.steps, reads: lowering.reads, exactBecause: null };
383
+ }
384
+ /**
385
+ * Packs a check's steps into words.
386
+ *
387
+ * @param steps The steps.
388
+ *
389
+ * @returns The words.
390
+ */
391
+ export function encodeCheck(steps) {
392
+ const words = [];
393
+ const bits = new DataView(new ArrayBuffer(8));
394
+ for (const { opcode, operand } of steps) {
395
+ if (opcode === CheckOp.pushk) {
396
+ bits.setFloat64(0, operand, true);
397
+ words.push(opcode, bits.getUint32(0, true), bits.getUint32(4, true));
398
+ }
399
+ else {
400
+ words.push((opcode | ((operand & 0xffffff) << 8)) >>> 0);
401
+ }
402
+ }
403
+ return words;
404
+ }
405
+ /**
406
+ * Lays out the checks table: each distinct check once.
407
+ *
408
+ * @param checks Each panel's check.
409
+ *
410
+ * @returns Each panel's first word (or CHECK_ALWAYS), and the table's words.
411
+ */
412
+ export function checksTable(checks) {
413
+ const words = [];
414
+ const known = new Map();
415
+ const starts = checks.map(({ steps }) => {
416
+ if (!steps)
417
+ return CHECK_ALWAYS;
418
+ const encoded = encodeCheck(steps);
419
+ const key = encoded.join();
420
+ let start = known.get(key);
421
+ if (start === undefined) {
422
+ start = words.length;
423
+ known.set(key, start);
424
+ words.push(...encoded);
425
+ }
426
+ return start;
427
+ });
428
+ return { starts, words };
429
+ }
430
+ /*----------------------------------------------------------------------------------------------------------------------
431
+ - Running a check for one size
432
+ ---------------------------------------------------------------------------------------------------------------------*/
433
+ /**
434
+ * A value's truth, as JS and the runtime read it.
435
+ *
436
+ * @param value The value.
437
+ *
438
+ * @returns True unless it is 0 or NaN.
439
+ */
440
+ const truth = (value) => value === value && value !== 0;
441
+ /**
442
+ * A double as an int, as the VM converts one: toward zero, NaN as 0, saturating.
443
+ *
444
+ * @param value The value.
445
+ *
446
+ * @returns The int.
447
+ */
448
+ function toInt(value) {
449
+ if (value !== value)
450
+ return 0;
451
+ if (value >= INT_MAX)
452
+ return INT_MAX;
453
+ if (value <= INT_MIN)
454
+ return INT_MIN;
455
+ return Math.trunc(value) + 0;
456
+ }
457
+ /**
458
+ * A whole-number division, with the VM's rules: dividing by 0 saturates to the dividend's sign.
459
+ *
460
+ * @param mode A CheckDivide value.
461
+ * @param dividendValue The dividend.
462
+ * @param divisorValue The divisor.
463
+ *
464
+ * @returns The result.
465
+ */
466
+ function divide(mode, dividendValue, divisorValue) {
467
+ const dividend = toInt(dividendValue);
468
+ const divisor = toInt(divisorValue);
469
+ if (mode === CheckDivide.mod) {
470
+ return divisor === 0 || divisor === -1 ? 0 : (dividend % divisor) + 0;
471
+ }
472
+ if (divisor === 0) {
473
+ return dividend > 0 ? INT_MAX : dividend < 0 ? INT_MIN : 0;
474
+ }
475
+ const quotient = dividend / divisor;
476
+ const rounded = mode === CheckDivide.floor
477
+ ? Math.floor(quotient)
478
+ : mode === CheckDivide.ceil
479
+ ? Math.ceil(quotient)
480
+ : mode === CheckDivide.round
481
+ ? Math.floor(quotient + ROUND_HALF_UP)
482
+ : Math.trunc(quotient);
483
+ return Math.min(INT_MAX, Math.max(INT_MIN, rounded)) + 0;
484
+ }
485
+ /**
486
+ * A Math function, as the VM's dmath() works it out.
487
+ *
488
+ * @param mathFunction A MathFn value.
489
+ * @param left The first operand.
490
+ * @param right The second, if it takes one.
491
+ *
492
+ * @returns The result.
493
+ */
494
+ function mathOf(mathFunction, left, right) {
495
+ switch (mathFunction) {
496
+ case MathFn.sin:
497
+ return Math.sin(left);
498
+ case MathFn.cos:
499
+ return Math.cos(left);
500
+ case MathFn.tan:
501
+ return Math.tan(left);
502
+ case MathFn.sqrt:
503
+ return Math.sqrt(left);
504
+ case MathFn.abs:
505
+ return Math.abs(left);
506
+ case MathFn.round: {
507
+ const floor = Math.floor(left);
508
+ return left - floor >= ROUND_HALF_UP ? floor + 1 : floor;
509
+ }
510
+ case MathFn.floor:
511
+ return Math.floor(left);
512
+ case MathFn.ceil:
513
+ return Math.ceil(left);
514
+ case MathFn.min:
515
+ return left !== left || right !== right
516
+ ? NaN
517
+ : left < right
518
+ ? left
519
+ : right;
520
+ case MathFn.max:
521
+ return left !== left || right !== right
522
+ ? NaN
523
+ : left > right
524
+ ? left
525
+ : right;
526
+ case MathFn.atan2:
527
+ return Math.atan2(left, right);
528
+ case MathFn.pow:
529
+ return Math.pow(left, right);
530
+ default:
531
+ return left % right;
532
+ }
533
+ }
534
+ /** Whether a Math function takes two operands. */
535
+ const isBinaryMath = (mathFunction) => mathFunction >= MathFn.min;
536
+ /**
537
+ * Runs a check for one size, as the loader does.
538
+ *
539
+ * @param check The check.
540
+ * @param width The screen's width.
541
+ * @param height The screen's height.
542
+ *
543
+ * @returns Whether the layout takes the screen.
544
+ */
545
+ export function runCheck(check, width, height) {
546
+ if (!check.steps)
547
+ return true;
548
+ const stack = [];
549
+ const locals = [];
550
+ for (const { opcode, operand } of check.steps) {
551
+ switch (opcode) {
552
+ case CheckOp.end:
553
+ return truth(stack.pop());
554
+ case CheckOp.width:
555
+ stack.push(width);
556
+ break;
557
+ case CheckOp.height:
558
+ stack.push(height);
559
+ break;
560
+ case CheckOp.pushi:
561
+ case CheckOp.pushk:
562
+ stack.push(operand);
563
+ break;
564
+ case CheckOp.load:
565
+ stack.push(locals[operand] ?? 0);
566
+ break;
567
+ case CheckOp.store:
568
+ locals[operand] = stack.pop();
569
+ break;
570
+ case CheckOp.neg:
571
+ stack.push(-stack.pop());
572
+ break;
573
+ case CheckOp.toint:
574
+ stack.push(toInt(stack.pop()));
575
+ break;
576
+ case CheckOp.not:
577
+ stack.push(truth(stack.pop()) ? 0 : 1);
578
+ break;
579
+ case CheckOp.math:
580
+ if (isBinaryMath(operand)) {
581
+ const right = stack.pop();
582
+ stack.push(mathOf(operand, stack.pop(), right));
583
+ }
584
+ else {
585
+ stack.push(mathOf(operand, stack.pop(), 0));
586
+ }
587
+ break;
588
+ case CheckOp.select: {
589
+ const alternate = stack.pop();
590
+ const consequent = stack.pop();
591
+ stack.push(truth(stack.pop()) ? consequent : alternate);
592
+ break;
593
+ }
594
+ default: {
595
+ const right = stack.pop();
596
+ stack.push(binaryOf(opcode, operand, stack.pop(), right));
597
+ }
598
+ }
599
+ }
600
+ throw new Error('AOT: a layout check has no end');
601
+ }
602
+ /**
603
+ * A two-operand step's result.
604
+ *
605
+ * @param opcode The op.
606
+ * @param operand Its argument.
607
+ * @param left The first operand.
608
+ * @param right The second.
609
+ *
610
+ * @returns The result.
611
+ */
612
+ function binaryOf(opcode, operand, left, right) {
613
+ switch (opcode) {
614
+ case CheckOp.add:
615
+ return left + right;
616
+ case CheckOp.sub:
617
+ return left - right;
618
+ case CheckOp.mul:
619
+ return left * right;
620
+ case CheckOp.div:
621
+ return left / right;
622
+ case CheckOp.idiv:
623
+ return divide(operand, left, right);
624
+ case CheckOp.lt:
625
+ return left < right ? 1 : 0;
626
+ case CheckOp.gt:
627
+ return left > right ? 1 : 0;
628
+ case CheckOp.le:
629
+ return left <= right ? 1 : 0;
630
+ case CheckOp.ge:
631
+ return left >= right ? 1 : 0;
632
+ case CheckOp.eq:
633
+ return left === right ? 1 : 0;
634
+ case CheckOp.ne:
635
+ return left !== right ? 1 : 0;
636
+ case CheckOp.and:
637
+ return truth(left) && truth(right) ? 1 : 0;
638
+ default:
639
+ return truth(left) || truth(right) ? 1 : 0;
640
+ }
641
+ }
642
+ /** A range that says nothing. */
643
+ const ANYTHING = { low: -Infinity, high: Infinity, includesNaN: true };
644
+ const FALSE = { low: 0, high: 0, includesNaN: false };
645
+ const TRUE = { low: 1, high: 1, includesNaN: false };
646
+ const EITHER = { low: 0, high: 1, includesNaN: false };
647
+ /**
648
+ * The range holding one value.
649
+ *
650
+ * @param value The value.
651
+ *
652
+ * @returns The range.
653
+ */
654
+ const only = (value) => value !== value ? ANYTHING : { low: value, high: value, includesNaN: false };
655
+ /**
656
+ * Whether a range holds one number, so the exact op applies.
657
+ *
658
+ * @param range The range.
659
+ *
660
+ * @returns True when it does.
661
+ */
662
+ const isOne = (range) => !range.includesNaN && range.low === range.high;
663
+ /**
664
+ * The range between the smallest and largest of some values.
665
+ *
666
+ * @param values The values; a NaN says nothing is known.
667
+ * @param includesNaN Whether the operands may be NaN.
668
+ *
669
+ * @returns The range.
670
+ */
671
+ function between(values, includesNaN) {
672
+ if (values.some(value => value !== value))
673
+ return ANYTHING;
674
+ return { low: Math.min(...values), high: Math.max(...values), includesNaN };
675
+ }
676
+ /**
677
+ * A range's truth.
678
+ *
679
+ * @param range The range.
680
+ *
681
+ * @returns True or false when every value in it agrees, else null.
682
+ */
683
+ function truthOf(range) {
684
+ if (!range.includesNaN && (range.low > 0 || range.high < 0))
685
+ return true;
686
+ if (range.low === 0 && range.high === 0)
687
+ return false;
688
+ return null;
689
+ }
690
+ /**
691
+ * A comparison over two ranges. NaN compares false, except for `!=`.
692
+ *
693
+ * @param opcode The op.
694
+ * @param left The first operand.
695
+ * @param right The second.
696
+ *
697
+ * @returns The range.
698
+ */
699
+ function compareRanges(opcode, left, right) {
700
+ const includesNaN = left.includesNaN || right.includesNaN;
701
+ let always;
702
+ let never;
703
+ switch (opcode) {
704
+ case CheckOp.lt:
705
+ always = left.high < right.low;
706
+ never = left.low >= right.high;
707
+ break;
708
+ case CheckOp.gt:
709
+ always = left.low > right.high;
710
+ never = left.high <= right.low;
711
+ break;
712
+ case CheckOp.le:
713
+ always = left.high <= right.low;
714
+ never = left.low > right.high;
715
+ break;
716
+ case CheckOp.ge:
717
+ always = left.low >= right.high;
718
+ never = left.high < right.low;
719
+ break;
720
+ case CheckOp.eq:
721
+ always = isOne(left) && isOne(right) && left.low === right.low;
722
+ never = left.high < right.low || left.low > right.high;
723
+ break;
724
+ default:
725
+ // A NaN makes `!=` true, so `always` stands whatever the operands' NaN.
726
+ if (left.high < right.low || left.low > right.high)
727
+ return TRUE;
728
+ return isOne(left) && isOne(right) && left.low === right.low
729
+ ? FALSE
730
+ : EITHER;
731
+ }
732
+ if (always && !includesNaN)
733
+ return TRUE;
734
+ return never ? FALSE : EITHER;
735
+ }
736
+ /**
737
+ * A Math function over ranges.
738
+ *
739
+ * @param mathFunction A MathFn value.
740
+ * @param left The first operand.
741
+ * @param right The second, if it takes one.
742
+ *
743
+ * @returns The range.
744
+ */
745
+ function mathOverRanges(mathFunction, left, right) {
746
+ if (isOne(left) && (!isBinaryMath(mathFunction) || isOne(right))) {
747
+ return only(mathOf(mathFunction, left.low, right.low));
748
+ }
749
+ const includesNaN = left.includesNaN || (isBinaryMath(mathFunction) && right.includesNaN);
750
+ switch (mathFunction) {
751
+ case MathFn.sin:
752
+ case MathFn.cos:
753
+ return { low: -1, high: 1, includesNaN: true };
754
+ case MathFn.sqrt:
755
+ return left.high < 0
756
+ ? ANYTHING
757
+ : {
758
+ low: Math.sqrt(Math.max(0, left.low)),
759
+ high: Math.sqrt(left.high),
760
+ includesNaN: includesNaN || left.low < 0,
761
+ };
762
+ case MathFn.abs:
763
+ return left.low >= 0
764
+ ? left
765
+ : left.high <= 0
766
+ ? { low: -left.high, high: -left.low, includesNaN: left.includesNaN }
767
+ : {
768
+ low: 0,
769
+ high: Math.max(-left.low, left.high),
770
+ includesNaN: left.includesNaN,
771
+ };
772
+ case MathFn.round:
773
+ case MathFn.floor:
774
+ case MathFn.ceil:
775
+ return {
776
+ low: mathOf(mathFunction, left.low, 0),
777
+ high: mathOf(mathFunction, left.high, 0),
778
+ includesNaN,
779
+ };
780
+ case MathFn.min:
781
+ return {
782
+ low: Math.min(left.low, right.low),
783
+ high: Math.min(left.high, right.high),
784
+ includesNaN,
785
+ };
786
+ case MathFn.max:
787
+ return {
788
+ low: Math.max(left.low, right.low),
789
+ high: Math.max(left.high, right.high),
790
+ includesNaN,
791
+ };
792
+ case MathFn.mod:
793
+ return isOne(right) && right.low > 0 && left.low >= 0
794
+ ? moduloRange(left, right.low)
795
+ : ANYTHING;
796
+ default:
797
+ return ANYTHING;
798
+ }
799
+ }
800
+ /**
801
+ * The remainder of a range of non-negative numbers by a positive one.
802
+ *
803
+ * @param left The dividends.
804
+ * @param divisor The divisor.
805
+ *
806
+ * @returns The range.
807
+ */
808
+ function moduloRange(left, divisor) {
809
+ const samePeriod = Math.floor(left.low / divisor) === Math.floor(left.high / divisor);
810
+ return samePeriod
811
+ ? {
812
+ low: left.low % divisor,
813
+ high: left.high % divisor,
814
+ includesNaN: left.includesNaN,
815
+ }
816
+ : { low: 0, high: divisor, includesNaN: left.includesNaN };
817
+ }
818
+ /**
819
+ * A whole-number division over ranges: by one positive divisor it rises with the dividend.
820
+ *
821
+ * @param mode A CheckDivide value.
822
+ * @param left The dividends.
823
+ * @param right The divisors.
824
+ *
825
+ * @returns The range.
826
+ */
827
+ function divideOverRanges(mode, left, right) {
828
+ if (isOne(left) && isOne(right)) {
829
+ return only(divide(mode, left.low, right.low));
830
+ }
831
+ if (!isOne(right) || right.low <= 0) {
832
+ return { low: INT_MIN, high: INT_MAX, includesNaN: false };
833
+ }
834
+ const whole = rangeToInt(left);
835
+ if (mode === CheckDivide.mod) {
836
+ return whole.low >= 0
837
+ ? moduloRange(whole, right.low)
838
+ : { low: -right.low, high: right.low, includesNaN: false };
839
+ }
840
+ return {
841
+ low: divide(mode, whole.low, right.low),
842
+ high: divide(mode, whole.high, right.low),
843
+ includesNaN: false,
844
+ };
845
+ }
846
+ /**
847
+ * A range of doubles as ints.
848
+ *
849
+ * @param range The range.
850
+ *
851
+ * @returns The range.
852
+ */
853
+ function rangeToInt(range) {
854
+ const low = toInt(range.low);
855
+ const high = toInt(range.high);
856
+ return range.includesNaN
857
+ ? { low: Math.min(low, 0), high: Math.max(high, 0), includesNaN: false }
858
+ : { low, high, includesNaN: false };
859
+ }
860
+ /**
861
+ * The product of two ranges.
862
+ *
863
+ * @param left The first.
864
+ * @param right The second.
865
+ *
866
+ * @returns The range.
867
+ */
868
+ const timesRange = (left, right) => between([
869
+ left.low * right.low,
870
+ left.low * right.high,
871
+ left.high * right.low,
872
+ left.high * right.high,
873
+ ], left.includesNaN || right.includesNaN);
874
+ /**
875
+ * The quotient of two ranges. A divisor that may be 0 says nothing.
876
+ *
877
+ * @param left The dividends.
878
+ * @param right The divisors.
879
+ *
880
+ * @returns The range.
881
+ */
882
+ const overRange = (left, right) => right.low <= 0 && right.high >= 0
883
+ ? ANYTHING
884
+ : between([
885
+ left.low / right.low,
886
+ left.low / right.high,
887
+ left.high / right.low,
888
+ left.high / right.high,
889
+ ], left.includesNaN || right.includesNaN);
890
+ /**
891
+ * A two-operand step over ranges.
892
+ *
893
+ * @param opcode The op.
894
+ * @param operand Its argument.
895
+ * @param left The first operand.
896
+ * @param right The second.
897
+ *
898
+ * @returns The range.
899
+ */
900
+ function binaryOverRanges(opcode, operand, left, right) {
901
+ const includesNaN = left.includesNaN || right.includesNaN;
902
+ switch (opcode) {
903
+ case CheckOp.add:
904
+ return between([left.low + right.low, left.high + right.high], includesNaN);
905
+ case CheckOp.sub:
906
+ return between([left.low - right.high, left.high - right.low], includesNaN);
907
+ case CheckOp.mul:
908
+ return timesRange(left, right);
909
+ case CheckOp.div:
910
+ return overRange(left, right);
911
+ case CheckOp.idiv:
912
+ return divideOverRanges(operand, left, right);
913
+ case CheckOp.and: {
914
+ const [first, second] = [truthOf(left), truthOf(right)];
915
+ if (first === false || second === false)
916
+ return FALSE;
917
+ return first && second ? TRUE : EITHER;
918
+ }
919
+ case CheckOp.or: {
920
+ const [first, second] = [truthOf(left), truthOf(right)];
921
+ if (first || second)
922
+ return TRUE;
923
+ return first === false && second === false ? FALSE : EITHER;
924
+ }
925
+ default:
926
+ return compareRanges(opcode, left, right);
927
+ }
928
+ }
929
+ /**
930
+ * Runs a check over a range of sizes.
931
+ *
932
+ * @param check The check.
933
+ * @param sizes The sizes.
934
+ *
935
+ * @returns 'pass' when the check passes for every one, 'fail' when it fails for every one, else 'unsure'.
936
+ */
937
+ export function checkOverSizes(check, sizes) {
938
+ if (!check.steps)
939
+ return 'pass';
940
+ // One size has an exact answer.
941
+ if ((sizes.minWidth === sizes.maxWidth || !check.reads.width) &&
942
+ (sizes.minHeight === sizes.maxHeight || !check.reads.height)) {
943
+ return runCheck(check, sizes.minWidth, sizes.minHeight) ? 'pass' : 'fail';
944
+ }
945
+ const stack = [];
946
+ const locals = [];
947
+ for (const { opcode, operand } of check.steps) {
948
+ switch (opcode) {
949
+ case CheckOp.end: {
950
+ const verdict = truthOf(stack.pop());
951
+ return verdict === null ? 'unsure' : verdict ? 'pass' : 'fail';
952
+ }
953
+ case CheckOp.width:
954
+ stack.push({
955
+ low: sizes.minWidth,
956
+ high: sizes.maxWidth,
957
+ includesNaN: false,
958
+ });
959
+ break;
960
+ case CheckOp.height:
961
+ stack.push({
962
+ low: sizes.minHeight,
963
+ high: sizes.maxHeight,
964
+ includesNaN: false,
965
+ });
966
+ break;
967
+ case CheckOp.pushi:
968
+ case CheckOp.pushk:
969
+ stack.push(only(operand));
970
+ break;
971
+ case CheckOp.load:
972
+ stack.push(locals[operand] ?? FALSE);
973
+ break;
974
+ case CheckOp.store:
975
+ locals[operand] = stack.pop();
976
+ break;
977
+ case CheckOp.neg: {
978
+ const { low, high, includesNaN } = stack.pop();
979
+ stack.push({ low: -high, high: -low, includesNaN });
980
+ break;
981
+ }
982
+ case CheckOp.toint:
983
+ stack.push(rangeToInt(stack.pop()));
984
+ break;
985
+ case CheckOp.not: {
986
+ const value = truthOf(stack.pop());
987
+ stack.push(value === null ? EITHER : value ? FALSE : TRUE);
988
+ break;
989
+ }
990
+ case CheckOp.math:
991
+ if (isBinaryMath(operand)) {
992
+ const right = stack.pop();
993
+ stack.push(mathOverRanges(operand, stack.pop(), right));
994
+ }
995
+ else {
996
+ stack.push(mathOverRanges(operand, stack.pop(), FALSE));
997
+ }
998
+ break;
999
+ case CheckOp.select: {
1000
+ const alternate = stack.pop();
1001
+ const consequent = stack.pop();
1002
+ const test = truthOf(stack.pop());
1003
+ if (test === null) {
1004
+ stack.push({
1005
+ low: Math.min(consequent.low, alternate.low),
1006
+ high: Math.max(consequent.high, alternate.high),
1007
+ includesNaN: consequent.includesNaN || alternate.includesNaN,
1008
+ });
1009
+ }
1010
+ else {
1011
+ stack.push(test ? consequent : alternate);
1012
+ }
1013
+ break;
1014
+ }
1015
+ default: {
1016
+ const right = stack.pop();
1017
+ stack.push(binaryOverRanges(opcode, operand, stack.pop(), right));
1018
+ }
1019
+ }
1020
+ }
1021
+ throw new Error('AOT: a layout check has no end');
1022
+ }