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.
Files changed (117) hide show
  1. package/NOTICE +1 -1
  2. package/README.md +8 -4
  3. package/aot/compile.mjs +178 -58
  4. package/aot/compiler/animations.mjs +290 -281
  5. package/aot/compiler/boot.mjs +70 -33
  6. package/aot/compiler/branch-merge.mjs +4 -3
  7. package/aot/compiler/check.mjs +9 -9
  8. package/aot/compiler/collect.mjs +324 -205
  9. package/aot/compiler/control-flow.mjs +209 -197
  10. package/aot/compiler/diagnostics.mjs +22 -25
  11. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  12. package/aot/compiler/elements/dial.mjs +91 -77
  13. package/aot/compiler/elements/flat-list.mjs +24 -22
  14. package/aot/compiler/elements/image.mjs +51 -50
  15. package/aot/compiler/elements/modal.mjs +31 -30
  16. package/aot/compiler/elements/props.mjs +23 -23
  17. package/aot/compiler/elements/switch.mjs +42 -34
  18. package/aot/compiler/elements/text-input.mjs +34 -32
  19. package/aot/compiler/expressions.mjs +372 -333
  20. package/aot/compiler/handlers.mjs +473 -387
  21. package/aot/compiler/ir/functions.mjs +11 -1
  22. package/aot/compiler/ir/nodes.mjs +4 -4
  23. package/aot/compiler/ir/reads.mjs +19 -19
  24. package/aot/compiler/ir/types.mjs +14 -14
  25. package/aot/compiler/ir/vectors.mjs +5 -1
  26. package/aot/compiler/keyboard.mjs +17 -7
  27. package/aot/compiler/lower.mjs +77 -80
  28. package/aot/compiler/nodes.mjs +169 -152
  29. package/aot/compiler/options.mjs +35 -30
  30. package/aot/compiler/out.mjs +18 -22
  31. package/aot/compiler/package/animations.mjs +669 -0
  32. package/aot/compiler/package/arena.mjs +252 -0
  33. package/aot/compiler/package/assemble.mjs +177 -0
  34. package/aot/compiler/package/bytecode.mjs +286 -0
  35. package/aot/compiler/package/checks.mjs +1022 -0
  36. package/aot/compiler/package/codegen.mjs +1329 -0
  37. package/aot/compiler/package/enums.mjs +138 -0
  38. package/aot/compiler/package/format.mjs +760 -0
  39. package/aot/compiler/package/layouts.mjs +136 -0
  40. package/aot/compiler/package/lower.mjs +1606 -0
  41. package/aot/compiler/package/props.mjs +118 -0
  42. package/aot/compiler/package/tables.mjs +103 -0
  43. package/aot/compiler/package/vectors.mjs +502 -0
  44. package/aot/compiler/package/writer.mjs +664 -0
  45. package/aot/compiler/pan-responder.mjs +69 -61
  46. package/aot/compiler/parse.mjs +21 -21
  47. package/aot/compiler/static-eval.mjs +25 -23
  48. package/aot/compiler/style-text.mjs +161 -172
  49. package/aot/compiler/svg.mjs +477 -251
  50. package/aot/screenshot-smoke.mjs +55 -41
  51. package/aot/style-map.mjs +102 -90
  52. package/assets/bake-font.mjs +123 -77
  53. package/assets/bake-image.mjs +39 -22
  54. package/assets/bake-svg.mjs +361 -262
  55. package/assets/build-builtin-font.mjs +14 -10
  56. package/assets/emit-c.mjs +83 -62
  57. package/assets/emit-container.mjs +124 -38
  58. package/assets/emit-pack.mjs +77 -67
  59. package/assets/font-config.mjs +9 -6
  60. package/assets/font-sizes.mjs +163 -123
  61. package/assets/glyph-coverage.mjs +74 -54
  62. package/assets/index.mjs +31 -9
  63. package/assets/rasterize.mjs +126 -99
  64. package/assets/svg-loader.mjs +8 -8
  65. package/build.mjs +16 -14
  66. package/cli.mjs +359 -172
  67. package/compile-bin.mjs +5 -5
  68. package/flash.mjs +537 -0
  69. package/hotreload/dev-device.mjs +90 -33
  70. package/hotreload/pack-app.mjs +55 -43
  71. package/hotreload/pack-split.mjs +46 -37
  72. package/hotreload/package-transfer.mjs +175 -0
  73. package/hotreload/protocol.mjs +51 -38
  74. package/hotreload/uploader.mjs +38 -27
  75. package/hotreload/watch-tree.mjs +19 -19
  76. package/pack-container.mjs +31 -23
  77. package/package.json +11 -12
  78. package/persist-transform.mjs +34 -28
  79. package/qjsc-wasm.mjs +25 -14
  80. package/sim/embedded-react.cjs +1 -1
  81. package/sim/embedded-react.js +1 -1
  82. package/sim/embedded-react.wasm +0 -0
  83. package/sim/index.html +94 -85
  84. package/sim-server.mjs +101 -82
  85. package/src/embedded-react/Animated.js +24 -24
  86. package/src/embedded-react/Easing.js +1 -1
  87. package/src/embedded-react/FlatList.js +4 -4
  88. package/src/embedded-react/LayoutAnimation.js +8 -5
  89. package/src/embedded-react/PanResponder.js +75 -71
  90. package/src/embedded-react/Pressable.js +2 -8
  91. package/src/embedded-react/SectionList.js +6 -6
  92. package/src/embedded-react/StyleSheet.js +4 -3
  93. package/src/embedded-react/TouchableOpacity.js +13 -8
  94. package/src/embedded-react/index.d.ts +5 -5
  95. package/src/embedded-react/layout-anim-config.js +10 -4
  96. package/src/embedded-react/list-child.js +5 -5
  97. package/src/embedded-react/named-colors.js +1 -1
  98. package/src/embedded-react/split-style.js +11 -11
  99. package/src/embedded-react/svg-ops.js +760 -593
  100. package/src/embedded-react/useHostValue.js +3 -3
  101. package/src/embedded-react/usePersistentState.js +6 -6
  102. package/src/embedded-react/warn-props.js +1 -1
  103. package/src/host-config.js +60 -53
  104. package/src/props.js +49 -41
  105. package/src/renderer.js +7 -4
  106. package/aot/compiler/assemble.mjs +0 -944
  107. package/aot/compiler/c/animations.mjs +0 -436
  108. package/aot/compiler/c/boot.mjs +0 -127
  109. package/aot/compiler/c/expressions.mjs +0 -270
  110. package/aot/compiler/c/functions.mjs +0 -268
  111. package/aot/compiler/c/keyboard.mjs +0 -96
  112. package/aot/compiler/c/nodes.mjs +0 -303
  113. package/aot/compiler/c/pan-responder.mjs +0 -136
  114. package/aot/compiler/c/statements.mjs +0 -319
  115. package/aot/compiler/c/text.mjs +0 -83
  116. package/aot/compiler/c/vectors.mjs +0 -381
  117. package/aot/compiler/c-syntax.mjs +0 -127
@@ -0,0 +1,664 @@
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
+ * Writes a lowered program as the bytes of a Flow B program section: the header, panels, needs and table
18
+ * directory, then each table, as website/docs/internals/flow-b-packages.md lays them out.
19
+ */
20
+ /*----------------------------------------------------------------------------------------------------------------------
21
+ - Imports
22
+ ---------------------------------------------------------------------------------------------------------------------*/
23
+ import { crc32 } from '../../../assets/emit-container.mjs';
24
+ import { checksTable } from './checks.mjs';
25
+ import { DEBUG_ENTRY_SIZE, DEBUG_HEADER_SIZE, DIRECTORY_SIZE, ENGINE_ABI, FORMAT_MAJOR, FORMAT_MINOR, GRADIENT_SIZE, HEADER_SIZE, NEED_SIZE, NONE, PAINT_SIZE, PANEL_SIZE, Table, TAPE_HEADER_SIZE, TAPE_SIZE, } from './format.mjs';
26
+ /*----------------------------------------------------------------------------------------------------------------------
27
+ - Constants
28
+ ---------------------------------------------------------------------------------------------------------------------*/
29
+ /** The breakpoints an interpolation record has room for (ER_INTERPOLATE_MAX_POINTS). */
30
+ const MAX_POINTS = 8;
31
+ /** Bytes a writer starts with; it doubles as it fills. */
32
+ const INITIAL_BUFFER_SIZE = 256;
33
+ /*----------------------------------------------------------------------------------------------------------------------
34
+ - Classes
35
+ ---------------------------------------------------------------------------------------------------------------------*/
36
+ /**
37
+ * Little-endian bytes, grown as they are written.
38
+ *
39
+ * @class ByteWriter
40
+ */
41
+ export class ByteWriter {
42
+ buffer = new Uint8Array(INITIAL_BUFFER_SIZE);
43
+ view = new DataView(this.buffer.buffer);
44
+ length = 0;
45
+ reserve(bytes) {
46
+ const at = this.length;
47
+ if (at + bytes > this.buffer.length) {
48
+ const grown = new Uint8Array(Math.max(this.buffer.length * 2, at + bytes));
49
+ grown.set(this.buffer);
50
+ this.buffer = grown;
51
+ this.view = new DataView(grown.buffer);
52
+ }
53
+ this.length += bytes;
54
+ return at;
55
+ }
56
+ // Each writer reserves before it touches the buffer, since reserving may replace it.
57
+ u8(value) {
58
+ const at = this.reserve(1);
59
+ this.view.setUint8(at, value);
60
+ }
61
+ u16(value) {
62
+ const at = this.reserve(2);
63
+ this.view.setUint16(at, value, true);
64
+ }
65
+ u32(value) {
66
+ const at = this.reserve(4);
67
+ this.view.setUint32(at, value >>> 0, true);
68
+ }
69
+ f32(value) {
70
+ const at = this.reserve(4);
71
+ this.view.setFloat32(at, value, true);
72
+ }
73
+ bytes(data) {
74
+ const at = this.reserve(data.length);
75
+ this.buffer.set(data, at);
76
+ }
77
+ /** Zeros up to the next multiple of 4. */
78
+ pad4() {
79
+ while (this.length % 4) {
80
+ this.u8(0);
81
+ }
82
+ }
83
+ /** Overwrites a u32 already written. */
84
+ putU32(at, value) {
85
+ this.view.setUint32(at, value >>> 0, true);
86
+ }
87
+ done() {
88
+ return this.buffer.slice(0, this.length);
89
+ }
90
+ }
91
+ /**
92
+ * Every panel's function bodies, each stored once: the code and types tables every panel's functions point
93
+ * into. A jump is relative to its function, so a body reads the same wherever it sits.
94
+ *
95
+ * @class Bodies
96
+ */
97
+ class Bodies {
98
+ code = [];
99
+ types = [];
100
+ codeAt = new Map();
101
+ typesAt = new Map();
102
+ /** Where a function's code and register types sit, storing each the first time it is seen. */
103
+ place(programFunction) {
104
+ return {
105
+ code: Bodies.store(this.codeAt, this.code, programFunction.code),
106
+ types: Bodies.store(this.typesAt, this.types, programFunction.types),
107
+ };
108
+ }
109
+ static store(placed, into, values) {
110
+ const key = values.join(',');
111
+ let at = placed.get(key);
112
+ if (at === undefined) {
113
+ at = into.length;
114
+ into.push(...values);
115
+ placed.set(key, at);
116
+ }
117
+ return at;
118
+ }
119
+ }
120
+ /*----------------------------------------------------------------------------------------------------------------------
121
+ - Implementation
122
+ ---------------------------------------------------------------------------------------------------------------------*/
123
+ /**
124
+ * A table with a header array: its count, the `first`/`count` header of each group, then every group's records.
125
+ *
126
+ * @param groups Each group's records.
127
+ * @param writeRecord Writes one record.
128
+ *
129
+ * @returns The table's bytes.
130
+ */
131
+ function headedTable(groups, writeRecord) {
132
+ const writer = new ByteWriter();
133
+ writer.u32(groups.length);
134
+ let first = 0;
135
+ for (const group of groups) {
136
+ writer.u16(first);
137
+ writer.u16(group.length);
138
+ first += group.length;
139
+ }
140
+ for (const group of groups) {
141
+ for (const record of group) {
142
+ writeRecord(writer, record);
143
+ }
144
+ }
145
+ return writer.done();
146
+ }
147
+ /**
148
+ * Bytes padded with zeros to a multiple of 4.
149
+ *
150
+ * @param bytes The bytes.
151
+ *
152
+ * @returns The padded bytes.
153
+ */
154
+ function padded(bytes) {
155
+ const writer = new ByteWriter();
156
+ writer.bytes(bytes);
157
+ writer.pad4();
158
+ return writer.done();
159
+ }
160
+ /**
161
+ * The strings table: the count, each string's byte offset from the table's start, then the NUL-terminated UTF-8.
162
+ *
163
+ * @param strings The strings.
164
+ *
165
+ * @returns The table's bytes.
166
+ */
167
+ export function stringsTable(strings) {
168
+ const writer = new ByteWriter();
169
+ writer.u32(strings.length);
170
+ const encoded = strings.map(text => new TextEncoder().encode(`${text}\0`));
171
+ let offset = 4 + strings.length * 4;
172
+ for (const bytes of encoded) {
173
+ writer.u32(offset);
174
+ offset += bytes.length;
175
+ }
176
+ for (const bytes of encoded) {
177
+ writer.bytes(bytes);
178
+ }
179
+ writer.pad4();
180
+ return writer.done();
181
+ }
182
+ /**
183
+ * Every table the program has, by id, but the code and types its functions are placed in. Tables the runtime
184
+ * requires are written even when empty.
185
+ *
186
+ * @param program The program.
187
+ * @param bodies Where its function bodies go.
188
+ *
189
+ * @returns The tables.
190
+ */
191
+ function tablesOf(program, bodies) {
192
+ const tables = new Map();
193
+ const table = (id, write) => {
194
+ const writer = new ByteWriter();
195
+ write(writer);
196
+ tables.set(id, writer.done());
197
+ };
198
+ if (program.strings.length) {
199
+ tables.set(Table.strings, stringsTable(program.strings));
200
+ }
201
+ if (program.const32.length) {
202
+ table(Table.const32, writer => program.const32.forEach(bits => writer.u32(bits)));
203
+ }
204
+ if (program.const64.length) {
205
+ table(Table.const64, writer => {
206
+ for (const value of program.const64) {
207
+ const bits = BigInt.asUintN(64, value);
208
+ writer.u32(Number(bits & 0xffffffffn));
209
+ writer.u32(Number(bits >> 32n));
210
+ }
211
+ });
212
+ }
213
+ table(Table.arena, writer => {
214
+ for (const region of program.regions) {
215
+ writer.u16(region.kind);
216
+ writer.u16(region.count);
217
+ writer.u32(region.offset);
218
+ writer.u32(region.bytes);
219
+ }
220
+ });
221
+ table(Table.slots, writer => {
222
+ for (const slot of program.slots) {
223
+ writer.u8(slot.type);
224
+ writer.u8(slot.flags);
225
+ writer.u16(slot.dirtyBit);
226
+ writer.u32(slot.offset);
227
+ writer.u32(slot.init);
228
+ writer.u16(slot.cap);
229
+ writer.u16(slot.aux);
230
+ }
231
+ });
232
+ if (program.enums.length) {
233
+ const enums = headedTable(program.enums, (writer, value) => writer.u16(value));
234
+ tables.set(Table.enums, padded(enums));
235
+ }
236
+ if (program.rows.length) {
237
+ table(Table.rows, writer => {
238
+ writer.u32(program.rows.length);
239
+ let first = 0;
240
+ for (const row of program.rows) {
241
+ writer.u16(row.size);
242
+ writer.u16(first);
243
+ writer.u16(row.fields.length);
244
+ writer.u16(0);
245
+ first += row.fields.length;
246
+ }
247
+ for (const field of program.rows.flatMap(row => row.fields)) {
248
+ writer.u8(field.type);
249
+ writer.u8(0);
250
+ writer.u16(field.cap);
251
+ writer.u32(field.offset);
252
+ }
253
+ });
254
+ }
255
+ if (program.propSets.length) {
256
+ tables.set(Table.props, headedTable(program.propSets, (writer, entry) => {
257
+ writer.u16(entry.prop);
258
+ writer.u8(entry.kind);
259
+ writer.u8(0);
260
+ writer.u32(entry.value);
261
+ }));
262
+ }
263
+ table(Table.templates, writer => {
264
+ writer.u32(program.templates.length);
265
+ let firstNode = 0;
266
+ for (const template of program.templates) {
267
+ writer.u16(firstNode);
268
+ writer.u16(template.nodes.length);
269
+ writer.u16(template.kind);
270
+ writer.u16(0);
271
+ firstNode += template.nodes.length;
272
+ }
273
+ for (const node of program.templates.flatMap(template => template.nodes)) {
274
+ writer.u8(node.type);
275
+ writer.u8(0);
276
+ writer.u16(node.children);
277
+ writer.u16(node.props);
278
+ writer.u16(node.handle);
279
+ writer.u16(node.bindings);
280
+ writer.u8(node.bindingCount);
281
+ writer.u8(0);
282
+ }
283
+ });
284
+ if (program.bindings.length) {
285
+ table(Table.bindings, writer => {
286
+ for (const binding of program.bindings) {
287
+ writer.u8(binding.kind);
288
+ writer.u8(binding.event);
289
+ writer.u16(binding.target);
290
+ }
291
+ });
292
+ }
293
+ table(Table.functions, writer => {
294
+ for (const programFunction of program.functions) {
295
+ const { code, types } = bodies.place(programFunction);
296
+ writer.u32(code);
297
+ writer.u32(programFunction.code.length);
298
+ writer.u32(types);
299
+ writer.u8(programFunction.types.length);
300
+ writer.u8(programFunction.parameters);
301
+ writer.u8(programFunction.result);
302
+ writer.u8(programFunction.kind);
303
+ }
304
+ });
305
+ if (program.animations.length) {
306
+ table(Table.animations, writer => {
307
+ for (const animation of program.animations) {
308
+ writer.u8(animation.kind);
309
+ writer.u8(animation.easing);
310
+ writer.u8(animation.flags);
311
+ writer.u8(0);
312
+ writer.u16(animation.target);
313
+ writer.u16(animation.prop);
314
+ writer.u16(animation.first);
315
+ writer.u16(animation.count);
316
+ writer.u32(animation.amount);
317
+ writer.u32(animation.delayMs);
318
+ writer.u32(animation.to);
319
+ animation.parameters.forEach(bits => writer.u32(bits));
320
+ }
321
+ });
322
+ }
323
+ if (program.inputs.length) {
324
+ table(Table.inputs, writer => {
325
+ for (const input of program.inputs) {
326
+ writer.u16(input.name);
327
+ writer.u16(input.slot);
328
+ }
329
+ });
330
+ }
331
+ if (program.interpolations.length) {
332
+ table(Table.interpolations, writer => {
333
+ for (const interpolation of program.interpolations) {
334
+ writer.u16(interpolation.slot);
335
+ writer.u8(interpolation.input.length);
336
+ writer.u8(interpolation.flags);
337
+ writer.u8(interpolation.extrapolateLeft);
338
+ writer.u8(interpolation.extrapolateRight);
339
+ writer.u16(0);
340
+ for (const values of [interpolation.input, interpolation.output]) {
341
+ for (let point = 0; point < MAX_POINTS; point++) {
342
+ writer.u32(values[point] ?? 0);
343
+ }
344
+ }
345
+ }
346
+ });
347
+ }
348
+ if (program.tapes.length) {
349
+ tables.set(Table.tapes, tapesTable(program.tapes));
350
+ }
351
+ if (program.keyboard) {
352
+ tables.set(Table.keyboard, keyboardTable(program.keyboard));
353
+ }
354
+ return tables;
355
+ }
356
+ /**
357
+ * The tapes table: its header, the tape records, then every tape's floats, paints, gradients, and stops, each
358
+ * tape's after the one before it.
359
+ *
360
+ * @param tapes The tapes.
361
+ *
362
+ * @returns The table's bytes.
363
+ */
364
+ function tapesTable(tapes) {
365
+ const floatCount = tapes.reduce((count, tape) => count + tape.floats.length, 0);
366
+ const paints = tapes.flatMap(tape => tape.paints);
367
+ const gradients = tapes.flatMap(tape => tape.gradients);
368
+ const floatsAt = TAPE_HEADER_SIZE + tapes.length * TAPE_SIZE;
369
+ const paintsAt = floatsAt + floatCount * 4;
370
+ const gradientsAt = paintsAt + paints.length * PAINT_SIZE;
371
+ const writer = new ByteWriter();
372
+ writer.u32(tapes.length);
373
+ writer.u32(paintsAt);
374
+ writer.u32(gradientsAt);
375
+ writer.u32(gradientsAt + gradients.length * GRADIENT_SIZE);
376
+ let floatFirst = 0;
377
+ let paintFirst = 0;
378
+ let gradientFirst = 0;
379
+ for (const tape of tapes) {
380
+ writer.u32(floatsAt + floatFirst * 4);
381
+ writer.u16(tape.floats.length);
382
+ writer.u16(paintFirst);
383
+ writer.u16(tape.paints.length);
384
+ writer.u16(gradientFirst);
385
+ writer.u16(tape.gradients.length);
386
+ writer.u16(0);
387
+ floatFirst += tape.floats.length;
388
+ paintFirst += tape.paints.length;
389
+ gradientFirst += tape.gradients.length;
390
+ }
391
+ tapes.forEach(tape => tape.floats.forEach(value => writer.f32(value)));
392
+ for (const paint of paints) {
393
+ writer.u32(paint.fill);
394
+ writer.u32(paint.stroke);
395
+ writer.f32(paint.strokeWidth);
396
+ writer.f32(paint.miter);
397
+ writer.u8(paint.cap);
398
+ writer.u8(paint.join);
399
+ writer.u8(paint.fillRule);
400
+ writer.u8(0);
401
+ writer.u16(paint.fillGradient);
402
+ writer.u16(paint.strokeGradient);
403
+ }
404
+ let stopFirst = 0;
405
+ for (const gradient of gradients) {
406
+ writer.u8(gradient.type);
407
+ writer.u8(gradient.stops.length);
408
+ writer.u16(stopFirst);
409
+ for (const value of [
410
+ gradient.ax,
411
+ gradient.ay,
412
+ gradient.bx,
413
+ gradient.by,
414
+ gradient.radius,
415
+ ]) {
416
+ writer.f32(value);
417
+ }
418
+ stopFirst += gradient.stops.length;
419
+ }
420
+ for (const stop of gradients.flatMap(gradient => gradient.stops)) {
421
+ writer.u32(stop.color);
422
+ writer.f32(stop.position);
423
+ }
424
+ return writer.done();
425
+ }
426
+ /**
427
+ * The keyboard table: its look, then its layers, rows and keys, each group's after the one before it.
428
+ *
429
+ * @param keyboard The keyboard.
430
+ *
431
+ * @returns The table's bytes.
432
+ */
433
+ function keyboardTable(keyboard) {
434
+ const rows = keyboard.layers.flat();
435
+ const keys = rows.flat();
436
+ const writer = new ByteWriter();
437
+ writer.u16(keyboard.layers.length);
438
+ writer.u16(rows.length);
439
+ writer.u16(keys.length);
440
+ writer.u16(keyboard.rowHeight);
441
+ writer.u8(keyboard.gridColumns);
442
+ writer.u8(keyboard.keyGap);
443
+ writer.u8(keyboard.keyRadius);
444
+ writer.u8(keyboard.fontSize);
445
+ writer.u32(keyboard.panelColor);
446
+ writer.u32(keyboard.keyColor);
447
+ writer.u32(keyboard.keyActiveColor);
448
+ writer.u32(keyboard.labelColor);
449
+ const groups = (sizes) => {
450
+ let first = 0;
451
+ for (const size of sizes) {
452
+ writer.u16(first);
453
+ writer.u16(size);
454
+ first += size;
455
+ }
456
+ };
457
+ groups(keyboard.layers.map(layer => layer.length));
458
+ groups(rows.map(row => row.length));
459
+ for (const key of keys) {
460
+ writer.u16(key.label);
461
+ writer.u16(key.text);
462
+ writer.u8(key.type);
463
+ writer.u8(key.layer);
464
+ writer.u8(key.span);
465
+ writer.u8(key.highlight);
466
+ }
467
+ return writer.done();
468
+ }
469
+ /** Whether two tables hold the same bytes. */
470
+ function sameBytes(left, right) {
471
+ return (left.length === right.length &&
472
+ left.every((byte, index) => byte === right[index]));
473
+ }
474
+ /**
475
+ * Each panel's needs: one for every panel where they agree, else one for each panel that has it, since only the
476
+ * chosen layout's have to fit.
477
+ *
478
+ * @param programs Each panel's program.
479
+ *
480
+ * @returns The needs, with the panel each is for.
481
+ */
482
+ function panelNeeds(programs) {
483
+ const resources = [
484
+ ...new Set(programs.flatMap(program => program.needs.map(need => need.resource))),
485
+ ].sort((left, right) => left - right);
486
+ return resources.flatMap(resource => {
487
+ const amounts = programs.map(program => program.needs.find(need => need.resource === resource)?.amount ?? 0);
488
+ return amounts.every(amount => amount === amounts[0])
489
+ ? [{ resource, amount: amounts[0], panel: NONE }]
490
+ : amounts.flatMap((amount, panel) => amount ? [{ resource, amount, panel }] : []);
491
+ });
492
+ }
493
+ /**
494
+ * Writes a program section, its build_id worked out over the finished bytes. Each program is one layout's. A
495
+ * table that is the same for every panel is listed once for all of them; any other is listed for each panel
496
+ * that has it, and panels whose copies are the same share its bytes. A function body the same in several
497
+ * panels is stored once.
498
+ *
499
+ * @param programs Each panel's program, in the order the panel table lists them.
500
+ *
501
+ * @returns The section's bytes, its build_id, and each table's size.
502
+ */
503
+ export function writeProgram(programs) {
504
+ // Every panel's functions point into one code table and one types table.
505
+ const bodies = new Bodies();
506
+ const panelTables = programs.map(program => tablesOf(program, bodies));
507
+ const checks = checksTable(programs.map(program => program.panels[0].check));
508
+ const types = new ByteWriter();
509
+ bodies.types.forEach(type => types.u8(type));
510
+ types.pad4();
511
+ const code = new ByteWriter();
512
+ bodies.code.forEach(word => code.u32(word));
513
+ for (const tables of panelTables) {
514
+ tables.set(Table.types, types.done());
515
+ tables.set(Table.code, code.done());
516
+ if (checks.words.length) {
517
+ const words = new ByteWriter();
518
+ checks.words.forEach(word => words.u32(word));
519
+ tables.set(Table.checks, words.done());
520
+ }
521
+ }
522
+ const ids = [...new Set(panelTables.flatMap(tables => [...tables.keys()]))];
523
+ ids.sort((left, right) => left - right);
524
+ // The bytes stored, and the directory entries that name them.
525
+ const stored = [];
526
+ const entries = [];
527
+ const sizes = [];
528
+ for (const id of ids) {
529
+ const copies = panelTables.map(tables => tables.get(id));
530
+ const first = copies[0];
531
+ if (first && copies.every(copy => copy && sameBytes(copy, first))) {
532
+ entries.push({ id, panel: NONE, data: stored.push(first) - 1 });
533
+ sizes.push({ id, bytes: [first.length] });
534
+ continue;
535
+ }
536
+ const ownCopies = [];
537
+ copies.forEach((copy, panel) => {
538
+ if (!copy) {
539
+ return;
540
+ }
541
+ let data = ownCopies.find(index => sameBytes(stored[index], copy));
542
+ if (data === undefined) {
543
+ data = stored.push(copy) - 1;
544
+ ownCopies.push(data);
545
+ }
546
+ entries.push({ id, panel, data });
547
+ });
548
+ sizes.push({ id, bytes: ownCopies.map(index => stored[index].length) });
549
+ }
550
+ const panels = programs.map(program => program.panels[0]);
551
+ const needs = panelNeeds(programs);
552
+ const writer = new ByteWriter();
553
+ writer.u16(FORMAT_MAJOR);
554
+ writer.u16(FORMAT_MINOR);
555
+ writer.u16(ENGINE_ABI);
556
+ writer.u16(programs[0].flags);
557
+ const buildIdAt = writer.length;
558
+ writer.u32(0);
559
+ writer.u32(Math.max(...panels.map(panel => panel.arenaBytes)));
560
+ writer.u16(Math.max(...panels.map(panel => panel.nodeCount)));
561
+ writer.u16(panels.length);
562
+ writer.u16(needs.length);
563
+ writer.u16(entries.length);
564
+ panels.forEach((panel, index) => {
565
+ writer.u32(checks.starts[index]);
566
+ writer.u32(panel.arenaBytes);
567
+ writer.u16(panel.nodeCount);
568
+ writer.u16(panel.rootTemplate);
569
+ writer.u16(panel.bootFunction);
570
+ writer.u16(panel.updateFunction);
571
+ });
572
+ for (const need of needs) {
573
+ writer.u16(need.resource);
574
+ writer.u16(need.panel);
575
+ writer.u32(need.amount);
576
+ }
577
+ // The directory, then the tables, each starting 4-aligned since every one is a multiple of 4 long.
578
+ const offsets = [];
579
+ let offset = HEADER_SIZE +
580
+ panels.length * PANEL_SIZE +
581
+ needs.length * NEED_SIZE +
582
+ entries.length * DIRECTORY_SIZE;
583
+ for (const bytes of stored) {
584
+ offsets.push(offset);
585
+ offset += bytes.length;
586
+ }
587
+ for (const entry of entries) {
588
+ writer.u16(entry.id);
589
+ writer.u16(entry.panel);
590
+ writer.u32(offsets[entry.data]);
591
+ writer.u32(stored[entry.data].length);
592
+ }
593
+ for (const bytes of stored) {
594
+ writer.bytes(bytes);
595
+ }
596
+ const bytes = writer.done();
597
+ const buildId = crc32(bytes);
598
+ new DataView(bytes.buffer).setUint32(buildIdAt, buildId, true);
599
+ return { bytes, buildId, tables: sizes };
600
+ }
601
+ /**
602
+ * One panel's debug table: where each function's code is in the source, and each function's name.
603
+ *
604
+ * @param functions The panel's functions.
605
+ * @param buildId The program section's build_id.
606
+ * @param file The source file, as the error screen names it.
607
+ * @param panel The panel it is for, or NONE for every panel.
608
+ *
609
+ * @returns The table's bytes.
610
+ */
611
+ function debugTable(functions, buildId, file, panel) {
612
+ const strings = [file];
613
+ const string = (text) => {
614
+ const at = strings.indexOf(text);
615
+ return at >= 0 ? at : strings.push(text) - 1;
616
+ };
617
+ const names = functions.map(programFunction => string(programFunction.debug?.name ?? ''));
618
+ const entries = functions.flatMap((programFunction, index) => (programFunction.debug?.lines ?? []).map(line => ({
619
+ ...line,
620
+ function: index,
621
+ })));
622
+ const stringBytes = stringsTable(strings);
623
+ const stringsAt = DEBUG_HEADER_SIZE +
624
+ entries.length * DEBUG_ENTRY_SIZE +
625
+ Math.ceil(names.length / 2) * 4;
626
+ const writer = new ByteWriter();
627
+ writer.u32(buildId);
628
+ writer.u32(entries.length);
629
+ writer.u16(functions.length);
630
+ writer.u16(panel);
631
+ writer.u32(stringsAt);
632
+ writer.u32(stringsAt + stringBytes.length);
633
+ for (const entry of entries) {
634
+ writer.u16(entry.function);
635
+ writer.u16(0);
636
+ writer.u32(entry.offset);
637
+ writer.u16(0);
638
+ // 1-based, as the compiler's errors show it.
639
+ writer.u16(Math.min(entry.column + 1, NONE));
640
+ writer.u32(entry.line);
641
+ }
642
+ names.forEach(name => writer.u16(name));
643
+ writer.pad4();
644
+ writer.bytes(stringBytes);
645
+ return writer.done();
646
+ }
647
+ /**
648
+ * Writes the debug section: a table for every panel when the panels' tables are the same, else one for each.
649
+ *
650
+ * @param programs Each panel's program, in the order the panel table lists them.
651
+ * @param buildId The program section's build_id.
652
+ * @param file The source file, as the error screen names it.
653
+ *
654
+ * @returns The section's bytes.
655
+ */
656
+ export function writeDebug(programs, buildId, file) {
657
+ const shared = programs.map(program => debugTable(program.functions, buildId, file, NONE));
658
+ if (shared.every(table => sameBytes(table, shared[0]))) {
659
+ return shared[0];
660
+ }
661
+ const writer = new ByteWriter();
662
+ programs.forEach((program, panel) => writer.bytes(debugTable(program.functions, buildId, file, panel)));
663
+ return writer.done();
664
+ }