embedded-react 0.21.0 → 0.22.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 (99) hide show
  1. package/README.md +2 -2
  2. package/aot/compile.mjs +1 -1
  3. package/aot/compiler/alerts.mjs +604 -0
  4. package/aot/compiler/check.mjs +1 -1
  5. package/aot/compiler/collect.mjs +1 -1
  6. package/aot/compiler/elements/button.mjs +3 -3
  7. package/aot/compiler/elements/image-background.mjs +1 -1
  8. package/aot/compiler/elements/props.mjs +10 -6
  9. package/aot/compiler/elements/section-list.mjs +1 -1
  10. package/aot/compiler/expressions.mjs +29 -2
  11. package/aot/compiler/handlers.mjs +73 -16
  12. package/aot/compiler/keyboard-dismiss.mjs +99 -0
  13. package/aot/compiler/keyboard.mjs +1 -1
  14. package/aot/compiler/lower.mjs +12 -0
  15. package/aot/compiler/nodes.mjs +17 -9
  16. package/aot/compiler/out.mjs +4 -0
  17. package/aot/compiler/package/codegen.mjs +36 -2
  18. package/aot/compiler/package/format.mjs +5 -3
  19. package/aot/compiler/package/lower.mjs +53 -19
  20. package/aot/compiler/scroll-to.mjs +112 -0
  21. package/aot/compiler/style-text.mjs +2 -2
  22. package/aot/compiler/svg.mjs +6 -3
  23. package/aot/compiler/updaters.mjs +1 -0
  24. package/aot/style-map.mjs +44 -1
  25. package/assets/bake-font.mjs +187 -244
  26. package/assets/bake-image.mjs +47 -71
  27. package/assets/bake-svg.mjs +465 -533
  28. package/assets/build-builtin-font.mjs +14 -27
  29. package/assets/emit-c.mjs +124 -204
  30. package/assets/emit-container.mjs +129 -197
  31. package/assets/emit-pack.mjs +92 -136
  32. package/assets/font-config.mjs +19 -27
  33. package/assets/font-sizes.mjs +432 -507
  34. package/assets/glyph-coverage.mjs +170 -214
  35. package/assets/index.mjs +52 -97
  36. package/assets/rasterize.mjs +173 -194
  37. package/assets/svg-loader.mjs +42 -48
  38. package/build.mjs +84 -101
  39. package/cli.mjs +482 -621
  40. package/compile-bin.mjs +24 -36
  41. package/flash.mjs +297 -401
  42. package/hotreload/dev-device.mjs +229 -270
  43. package/hotreload/pack-app.mjs +118 -159
  44. package/hotreload/pack-split.mjs +144 -200
  45. package/hotreload/package-transfer.mjs +128 -131
  46. package/hotreload/protocol.mjs +118 -118
  47. package/hotreload/uploader.mjs +132 -159
  48. package/hotreload/watch-tree.mjs +56 -65
  49. package/pack-container.mjs +93 -123
  50. package/package.json +19 -16
  51. package/persist-transform.mjs +89 -105
  52. package/qjsc-wasm.mjs +47 -72
  53. package/sim/embedded-react.wasm +0 -0
  54. package/sim/index.html +1 -1
  55. package/sim-server.mjs +260 -354
  56. package/src/embedded-react/Alert.mjs +112 -0
  57. package/src/embedded-react/Animated.mjs +412 -0
  58. package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
  59. package/src/embedded-react/{Button.js → Button.mjs} +29 -53
  60. package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
  61. package/src/embedded-react/Easing.mjs +34 -0
  62. package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
  63. package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
  64. package/src/embedded-react/Keyboard.mjs +82 -0
  65. package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
  66. package/src/embedded-react/PanResponder.mjs +256 -0
  67. package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
  68. package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
  69. package/src/embedded-react/SectionList.mjs +103 -0
  70. package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
  71. package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
  72. package/src/embedded-react/background-image.mjs +321 -0
  73. package/src/embedded-react/{components.js → components.mjs} +1 -3
  74. package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
  75. package/src/embedded-react/index.d.ts +155 -2
  76. package/src/embedded-react/index.js +26 -19
  77. package/src/embedded-react/layout-anim-config.mjs +85 -0
  78. package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
  79. package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
  80. package/src/embedded-react/nativeModule.mjs +371 -0
  81. package/src/embedded-react/split-style.mjs +57 -0
  82. package/src/embedded-react/svg-ops.mjs +1075 -0
  83. package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
  84. package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
  85. package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
  86. package/src/host-config.mjs +512 -0
  87. package/src/{native-ui.js → native-ui.mjs} +1 -8
  88. package/src/props.mjs +341 -0
  89. package/src/{renderer.js → renderer.mjs} +30 -44
  90. package/src/embedded-react/Animated.js +0 -440
  91. package/src/embedded-react/Easing.js +0 -39
  92. package/src/embedded-react/PanResponder.js +0 -330
  93. package/src/embedded-react/SectionList.js +0 -146
  94. package/src/embedded-react/layout-anim-config.js +0 -101
  95. package/src/embedded-react/nativeModule.js +0 -462
  96. package/src/embedded-react/split-style.js +0 -58
  97. package/src/embedded-react/svg-ops.js +0 -1295
  98. package/src/host-config.js +0 -548
  99. package/src/props.js +0 -306
@@ -13,239 +13,171 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
- // ERCF — the embedded-react config container: one self-describing blob holding an app's compiled
18
- // QuickJS bytecode plus its asset pack, with a version stamp and an integrity CRC. This is the
19
- // universal "a config" unit the device-agnostic loader (er_runtime_load_container, in the bridge)
20
- // verifies and runs — on the desktop demo, an ESP32-S3 partition, or an STM32H7 config region.
21
- //
22
- // Two CRCs are NOT the same thing: this internal CRC32 is embedded-react's own integrity check
23
- // (catches a corrupt/partially-written config). A bootloader's transfer/flash CRC (e.g. SREC_CAT on
24
- // the STM32H7) is a separate, project-specific step layered on top of this .erpkg by the firmware's
25
- // upload toolchain.
26
- //
27
- // Little-endian (matches the LE hosts: x86 desktop, ESP32-S3, STM32H7). Format:
28
- //
29
- // magic "ERCF" (4 bytes)
30
- // format_version u32 = 1
31
- // crc32 u32 — CRC-32/IEEE over every byte AFTER this field (offset 12 to end)
32
- // qjs_tag u16 len + bytes — QuickJS release the bytecode targets (loader rejects mismatch)
33
- // section_count u32
34
- // sections[section_count]: type u32, len u32, bytes
35
- // type 0 = alignment padding (zero bytes — carries no payload, skipped by loaders)
36
- // type 1 = QuickJS bytecode (the app — run last)
37
- // type 2 = ERPK asset pack (registered before the app mounts)
38
- // type 3 = QuickJS vendor bytecode (react + reconciler + lib — run FIRST, before the app). Optional;
39
- // present only for the vendor/app split used by incremental hot reload. A container with no
40
- // type-3 section is a plain monolithic app, loaded exactly as before (backwards-compatible).
41
- //
42
- // Alignment contract: the engine reads asset pixels IN PLACE from wherever the container sits (e.g.
43
- // memory-mapped flash), and the pack's internal pixel padding is computed relative to the pack's own
44
- // first byte. Those pads only mean anything at the absolute addresses the engine reads if the pack
45
- // itself starts on a 4-byte boundary — so the emitter inserts a type-0 padding section before the
46
- // asset pack whenever needed to 4-align the pack's first byte RELATIVE TO THE CONTAINER, and the host
47
- // must place the container at a 4-byte-aligned base address (linker scripts typically give 16; malloc
48
- // guarantees it). Loaders bounds-walk every section and dispatch only on types they know, so old
49
- // loaders skip the padding section untouched — no format version bump.
50
-
51
16
  const FORMAT_VERSION = 1;
52
-
53
17
  const HEADER_BYTES = 12; // magic + format_version + crc32
54
18
  const TAG_LENGTH_BYTES = 2;
55
19
  const SECTION_COUNT_BYTES = 4;
56
20
  const SECTION_HEADER_BYTES = 8; // type + len
57
21
  const SECTION_ALIGNMENT = 4;
58
-
59
22
  const CRC32_POLYNOMIAL = 0xedb88320;
60
23
  const CRC32_INVERT = 0xffffffff;
61
24
  const BITS_PER_BYTE = 8;
62
-
63
25
  export const SECTION_PAD = 0;
64
26
  export const SECTION_BYTECODE = 1;
65
27
  export const SECTION_ASSET_PACK = 2;
66
28
  export const SECTION_VENDOR_BYTECODE = 3;
67
-
68
29
  /**
69
30
  * CRC-32/IEEE (zlib polynomial 0xEDB88320), computed without a lookup table to match er_crc32() in
70
31
  * runtime/er_container.c byte-for-byte — the one checksum of the whole delivery pipeline (this
71
32
  * container, the hot-reload transport frame that carries it, and the device's verification of both).
72
- * hotreload/protocol.mjs imports this rather than keeping its own.
33
+ * hotreload/protocol.mts imports this rather than keeping its own.
73
34
  *
74
- * @param {Buffer|Uint8Array} data Bytes to checksum.
75
- * @returns {number} The CRC as an unsigned 32-bit integer.
35
+ * @param data Bytes to checksum.
36
+ * @returns The CRC as an unsigned 32-bit integer.
76
37
  */
77
38
  export function crc32(data) {
78
- let crc = CRC32_INVERT;
79
- for (let index = 0; index < data.length; index++) {
80
- crc ^= data[index];
81
- for (let bit = 0; bit < BITS_PER_BYTE; bit++) {
82
- crc = (crc >>> 1) ^ (CRC32_POLYNOMIAL & -(crc & 1));
39
+ let crc = CRC32_INVERT;
40
+ for (let index = 0; index < data.length; index++) {
41
+ crc ^= data[index];
42
+ for (let bit = 0; bit < BITS_PER_BYTE; bit++) {
43
+ crc = (crc >>> 1) ^ (CRC32_POLYNOMIAL & -(crc & 1));
44
+ }
83
45
  }
84
- }
85
- return (crc ^ CRC32_INVERT) >>> 0;
46
+ return (crc ^ CRC32_INVERT) >>> 0;
86
47
  }
87
-
88
- /** Accumulates little-endian binary chunks (mirrors emit-pack.mjs's Writer). */
48
+ /** Accumulates little-endian binary chunks (mirrors emit-pack.mts's Writer). */
89
49
  class Writer {
90
- constructor() {
91
- this.chunks = [];
92
- }
93
- u16(value) {
94
- const buffer = Buffer.alloc(2);
95
- buffer.writeUInt16LE(value & 0xffff, 0);
96
- this.chunks.push(buffer);
97
- }
98
- u32(value) {
99
- const buffer = Buffer.alloc(4);
100
- buffer.writeUInt32LE(value >>> 0, 0);
101
- this.chunks.push(buffer);
102
- }
103
- str(text) {
104
- const buffer = Buffer.from(text, 'utf8');
105
- this.u16(buffer.length);
106
- this.chunks.push(buffer);
107
- }
108
- bytes(data) {
109
- this.chunks.push(Buffer.from(data));
110
- }
111
- done() {
112
- return Buffer.concat(this.chunks);
113
- }
50
+ chunks = [];
51
+ u16(value) {
52
+ const buffer = Buffer.alloc(2);
53
+ buffer.writeUInt16LE(value & 0xffff, 0);
54
+ this.chunks.push(buffer);
55
+ }
56
+ u32(value) {
57
+ const buffer = Buffer.alloc(4);
58
+ buffer.writeUInt32LE(value >>> 0, 0);
59
+ this.chunks.push(buffer);
60
+ }
61
+ str(text) {
62
+ const buffer = Buffer.from(text, 'utf8');
63
+ this.u16(buffer.length);
64
+ this.chunks.push(buffer);
65
+ }
66
+ bytes(data) {
67
+ this.chunks.push(Buffer.from(data));
68
+ }
69
+ done() {
70
+ return Buffer.concat(this.chunks);
71
+ }
114
72
  }
115
-
116
73
  /**
117
74
  * Serializes a compiled app + assets into an ERCF container.
118
75
  *
119
- * @param {object} opts
120
- * @param {Buffer|Uint8Array} opts.bytecode QuickJS bytecode blob (.qbc) for the app — required.
121
- * @param {Buffer|Uint8Array} [opts.vendorBytecode] QuickJS bytecode for the vendor chunk (react + reconciler
122
- * + lib). When present, the loader runs it BEFORE the app
123
- * (it installs the module registry the app require()s). Omit
124
- * for a monolithic app — output is then byte-identical to before.
125
- * @param {Buffer|Uint8Array} [opts.assetPack] ERPK asset pack bytes; omitted/empty → no asset section.
126
- * @param {string} opts.qjsTag QuickJS release tag the bytecode targets (e.g. "v0.15.0").
127
- * @returns {Buffer} The container bytes.
76
+ * @returns The container bytes.
128
77
  */
129
- export function emitContainer({bytecode, vendorBytecode, assetPack, qjsTag}) {
130
- if (!bytecode || !bytecode.length)
131
- throw new Error('emitContainer: bytecode is required');
132
- if (!qjsTag) throw new Error('emitContainer: qjsTag is required');
133
-
134
- // Build the body (everything after the crc32 field) first, then prepend magic+version+crc.
135
- const body = new Writer();
136
- body.str(qjsTag);
137
- const sections = [];
138
- // Order matters: vendor runs first (registers modules), assets register, then the app runs last.
139
- if (vendorBytecode && vendorBytecode.length)
140
- sections.push([SECTION_VENDOR_BYTECODE, vendorBytecode]);
141
- if (assetPack && assetPack.length)
142
- sections.push([SECTION_ASSET_PACK, assetPack]);
143
- sections.push([SECTION_BYTECODE, bytecode]);
144
-
145
- // 4-align the asset pack's first byte relative to the container (see the alignment contract above)
146
- // by inserting a type-0 padding section ahead of it when the layout calls for one. Walk the layout
147
- // once to place pads, then write — section_count must already include them.
148
- const paddedSections = [];
149
- let offset =
150
- HEADER_BYTES +
151
- TAG_LENGTH_BYTES +
152
- Buffer.byteLength(qjsTag, 'utf8') +
153
- SECTION_COUNT_BYTES;
154
- for (const [type, data] of sections) {
155
- if (type === SECTION_ASSET_PACK) {
156
- // Counts this section's own type+len header.
157
- const padding =
158
- (SECTION_ALIGNMENT -
159
- ((offset + SECTION_HEADER_BYTES) % SECTION_ALIGNMENT)) %
160
- SECTION_ALIGNMENT;
161
- if (padding) {
162
- paddedSections.push([SECTION_PAD, Buffer.alloc(padding)]);
163
- offset += SECTION_HEADER_BYTES + padding;
164
- }
78
+ export function emitContainer({ bytecode, vendorBytecode, assetPack, qjsTag, }) {
79
+ if (!bytecode || !bytecode.length)
80
+ throw new Error('emitContainer: bytecode is required');
81
+ if (!qjsTag)
82
+ throw new Error('emitContainer: qjsTag is required');
83
+ // Build the body (everything after the crc32 field) first, then prepend magic+version+crc.
84
+ const body = new Writer();
85
+ body.str(qjsTag);
86
+ const sections = [];
87
+ // Order matters: vendor runs first (registers modules), assets register, then the app runs last.
88
+ if (vendorBytecode && vendorBytecode.length)
89
+ sections.push([SECTION_VENDOR_BYTECODE, vendorBytecode]);
90
+ if (assetPack && assetPack.length)
91
+ sections.push([SECTION_ASSET_PACK, assetPack]);
92
+ sections.push([SECTION_BYTECODE, bytecode]);
93
+ // 4-align the asset pack's first byte relative to the container (see the alignment contract above)
94
+ // by inserting a type-0 padding section ahead of it when the layout calls for one. Walk the layout
95
+ // once to place pads, then write — section_count must already include them.
96
+ const paddedSections = [];
97
+ let offset = HEADER_BYTES +
98
+ TAG_LENGTH_BYTES +
99
+ Buffer.byteLength(qjsTag, 'utf8') +
100
+ SECTION_COUNT_BYTES;
101
+ for (const [type, data] of sections) {
102
+ if (type === SECTION_ASSET_PACK) {
103
+ // Counts this section's own type+len header.
104
+ const padding = (SECTION_ALIGNMENT -
105
+ ((offset + SECTION_HEADER_BYTES) % SECTION_ALIGNMENT)) %
106
+ SECTION_ALIGNMENT;
107
+ if (padding) {
108
+ paddedSections.push([SECTION_PAD, Buffer.alloc(padding)]);
109
+ offset += SECTION_HEADER_BYTES + padding;
110
+ }
111
+ }
112
+ paddedSections.push([type, data]);
113
+ offset += SECTION_HEADER_BYTES + data.length;
114
+ }
115
+ body.u32(paddedSections.length);
116
+ for (const [type, data] of paddedSections) {
117
+ body.u32(type);
118
+ body.u32(data.length);
119
+ body.bytes(data);
165
120
  }
166
- paddedSections.push([type, data]);
167
- offset += SECTION_HEADER_BYTES + data.length;
168
- }
169
-
170
- body.u32(paddedSections.length);
171
- for (const [type, data] of paddedSections) {
172
- body.u32(type);
173
- body.u32(data.length);
174
- body.bytes(data);
175
- }
176
- const bodyBytes = body.done();
177
-
178
- const header = new Writer();
179
- header.bytes(Buffer.from('ERCF', 'ascii'));
180
- header.u32(FORMAT_VERSION);
181
- header.u32(crc32(bodyBytes));
182
- return Buffer.concat([header.done(), bodyBytes]);
121
+ const bodyBytes = body.done();
122
+ const header = new Writer();
123
+ header.bytes(Buffer.from('ERCF', 'ascii'));
124
+ header.u32(FORMAT_VERSION);
125
+ header.u32(crc32(bodyBytes));
126
+ return Buffer.concat([header.done(), bodyBytes]);
183
127
  }
184
-
185
128
  export const SECTION_PROGRAM = 4;
186
129
  export const SECTION_DEBUG = 5;
187
-
188
130
  /**
189
131
  * Serializes a Flow B package into an ERCF container tagged `erb`: the asset pack, the program, then the debug
190
132
  * table. Every section starts 4-aligned in the container, since the runtime reads each in place.
191
133
  *
192
- * @param {object} opts
193
- * @param {Uint8Array} opts.program The program section (aot/compiler/package/writer.mts).
194
- * @param {Uint8Array} [opts.assetPack] ERPK asset pack bytes; omitted/empty → no asset section.
195
- * @param {Uint8Array} [opts.debug] The debug section; omitted for a release build.
196
- * @returns {{bytes: Buffer, sections: Array<{type:number, offset:number, bytes:number}>}} The container, and
197
- * where each non-padding section's data starts in it.
134
+ * @returns The container, and where each non-padding section's data starts in it.
198
135
  */
199
- export function emitPackageContainer({program, assetPack, debug}) {
200
- if (!program || !program.length)
201
- throw new Error('emitPackageContainer: program is required');
202
-
203
- const tag = 'erb';
204
- const sections = [];
205
- if (assetPack && assetPack.length)
206
- sections.push([SECTION_ASSET_PACK, assetPack]);
207
- sections.push([SECTION_PROGRAM, program]);
208
- if (debug && debug.length) sections.push([SECTION_DEBUG, debug]);
209
-
210
- const paddedSections = [];
211
- const placed = [];
212
- let offset =
213
- HEADER_BYTES +
214
- TAG_LENGTH_BYTES +
215
- Buffer.byteLength(tag, 'utf8') +
216
- SECTION_COUNT_BYTES;
217
- for (const [type, data] of sections) {
218
- if ((offset + SECTION_HEADER_BYTES) % SECTION_ALIGNMENT) {
219
- // A padding section has its own 8-byte header, so its data has to cover what is left after it.
220
- const padding =
221
- (SECTION_ALIGNMENT -
222
- ((offset + 2 * SECTION_HEADER_BYTES) % SECTION_ALIGNMENT)) %
223
- SECTION_ALIGNMENT;
224
- paddedSections.push([SECTION_PAD, Buffer.alloc(padding)]);
225
- offset += SECTION_HEADER_BYTES + padding;
136
+ export function emitPackageContainer({ program, assetPack, debug, }) {
137
+ if (!program || !program.length)
138
+ throw new Error('emitPackageContainer: program is required');
139
+ const tag = 'erb';
140
+ const sections = [];
141
+ if (assetPack && assetPack.length)
142
+ sections.push([SECTION_ASSET_PACK, assetPack]);
143
+ sections.push([SECTION_PROGRAM, program]);
144
+ if (debug && debug.length)
145
+ sections.push([SECTION_DEBUG, debug]);
146
+ const paddedSections = [];
147
+ const placed = [];
148
+ let offset = HEADER_BYTES +
149
+ TAG_LENGTH_BYTES +
150
+ Buffer.byteLength(tag, 'utf8') +
151
+ SECTION_COUNT_BYTES;
152
+ for (const [type, data] of sections) {
153
+ if ((offset + SECTION_HEADER_BYTES) % SECTION_ALIGNMENT) {
154
+ // A padding section has its own 8-byte header, so its data has to cover what is left after it.
155
+ const padding = (SECTION_ALIGNMENT -
156
+ ((offset + 2 * SECTION_HEADER_BYTES) % SECTION_ALIGNMENT)) %
157
+ SECTION_ALIGNMENT;
158
+ paddedSections.push([SECTION_PAD, Buffer.alloc(padding)]);
159
+ offset += SECTION_HEADER_BYTES + padding;
160
+ }
161
+ placed.push({
162
+ type,
163
+ offset: offset + SECTION_HEADER_BYTES,
164
+ bytes: data.length,
165
+ });
166
+ paddedSections.push([type, data]);
167
+ offset += SECTION_HEADER_BYTES + data.length;
168
+ }
169
+ const body = new Writer();
170
+ body.str(tag);
171
+ body.u32(paddedSections.length);
172
+ for (const [type, data] of paddedSections) {
173
+ body.u32(type);
174
+ body.u32(data.length);
175
+ body.bytes(data);
226
176
  }
227
- placed.push({
228
- type,
229
- offset: offset + SECTION_HEADER_BYTES,
230
- bytes: data.length,
231
- });
232
- paddedSections.push([type, data]);
233
- offset += SECTION_HEADER_BYTES + data.length;
234
- }
235
-
236
- const body = new Writer();
237
- body.str(tag);
238
- body.u32(paddedSections.length);
239
- for (const [type, data] of paddedSections) {
240
- body.u32(type);
241
- body.u32(data.length);
242
- body.bytes(data);
243
- }
244
- const bodyBytes = body.done();
245
-
246
- const header = new Writer();
247
- header.bytes(Buffer.from('ERCF', 'ascii'));
248
- header.u32(FORMAT_VERSION);
249
- header.u32(crc32(bodyBytes));
250
- return {bytes: Buffer.concat([header.done(), bodyBytes]), sections: placed};
177
+ const bodyBytes = body.done();
178
+ const header = new Writer();
179
+ header.bytes(Buffer.from('ERCF', 'ascii'));
180
+ header.u32(FORMAT_VERSION);
181
+ header.u32(crc32(bodyBytes));
182
+ return { bytes: Buffer.concat([header.done(), bodyBytes]), sections: placed };
251
183
  }
@@ -13,157 +13,113 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
-
17
- // ERPK — a binary asset pack for the simulator's runtime loader (see tools/simulator/README.md).
18
- //
19
- // Produced from the SAME JS bakers (bake-image.mjs / bake-font.mjs) as the device's baked C, so the
20
- // simulator renders pixel-identical images and fonts. The sim host (tools/simulator/asset_pack.c)
21
- // loads this at runtime and calls er_image_load / er_font_register, so assets hot-reload without a
22
- // sim rebuild. Little-endian (the simulator is x86). Format:
23
- //
24
- // magic "ERPK" (4 bytes), version u32 (1 or 2), n_images u32, n_font_sizes u32
25
- // images[n_images]: name(u16 len + bytes), width u32, height u32,
26
- // [v2 only: format u8 (0=ARGB8888, 1=RGB565), zero-pad to 4-byte alignment],
27
- // pixels (w*h * u32 ARGB, or w*h * u16 RGB565 when format=1)
28
- // fonts[n_font_sizes]: family(u16 len + bytes), pixel_size/line_height/baseline/format (4*u8),
29
- // first u16, last u16, glyph_count u16, extras_count u16, bitmap_len u32,
30
- // glyphs[glyph_count] (9B: off u32, w/h u8, xo/yo i8, adv u8),
31
- // extras[extras_count] (13B: codepoint u32 + the 9B glyph),
32
- // bitmap (bitmap_len bytes)
33
- //
34
- // Version 2 is now emitted ALWAYS. It was introduced for the 16-bit image bake, but its real value
35
- // is the per-image alignment pad: the engine reads pixels in place, and a DMA2D-class backend
36
- // refuses a word-misaligned ARGB8888 source (configuration error — silently absent image). v1 had
37
- // no alignment machinery, so whether an image's pixels landed word-aligned in flash was an accident
38
- // of the name strings — it varied build to build. Parsers still accept v1 packs from older tooling.
39
-
40
16
  const INT8_MIN = -128;
41
17
  const INT8_MAX = 127;
42
18
  const PIXEL_ALIGNMENT = 4;
43
19
  const FORMAT_ARGB8888 = 0;
44
20
  const FORMAT_RGB565 = 1;
45
-
46
21
  /** Accumulates little-endian binary chunks. */
47
22
  class Writer {
48
- constructor() {
49
- this.chunks = [];
50
- }
51
- u8(value) {
52
- const buffer = Buffer.alloc(1);
53
- buffer.writeUInt8(value & 0xff, 0);
54
- this.chunks.push(buffer);
55
- }
56
- i8(value) {
57
- const buffer = Buffer.alloc(1);
58
- buffer.writeInt8(Math.max(INT8_MIN, Math.min(INT8_MAX, value | 0)), 0);
59
- this.chunks.push(buffer);
60
- }
61
- u16(value) {
62
- const buffer = Buffer.alloc(2);
63
- buffer.writeUInt16LE(value & 0xffff, 0);
64
- this.chunks.push(buffer);
65
- }
66
- u32(value) {
67
- const buffer = Buffer.alloc(4);
68
- buffer.writeUInt32LE(value >>> 0, 0);
69
- this.chunks.push(buffer);
70
- }
71
- str(text) {
72
- const buffer = Buffer.from(text, 'utf8');
73
- this.u16(buffer.length);
74
- this.chunks.push(buffer);
75
- }
76
- bytes(data) {
77
- this.chunks.push(Buffer.from(data));
78
- }
79
- /** Zero-pads so the next write lands on an n-byte boundary from the pack start. */
80
- align(alignment) {
81
- const length = this.chunks.reduce(
82
- (total, chunk) => total + chunk.length,
83
- 0,
84
- );
85
- const padding = (alignment - (length % alignment)) % alignment;
86
- if (padding) this.chunks.push(Buffer.alloc(padding));
87
- }
88
- done() {
89
- return Buffer.concat(this.chunks);
90
- }
23
+ chunks = [];
24
+ u8(value) {
25
+ const buffer = Buffer.alloc(1);
26
+ buffer.writeUInt8(value & 0xff, 0);
27
+ this.chunks.push(buffer);
28
+ }
29
+ i8(value) {
30
+ const buffer = Buffer.alloc(1);
31
+ buffer.writeInt8(Math.max(INT8_MIN, Math.min(INT8_MAX, value | 0)), 0);
32
+ this.chunks.push(buffer);
33
+ }
34
+ u16(value) {
35
+ const buffer = Buffer.alloc(2);
36
+ buffer.writeUInt16LE(value & 0xffff, 0);
37
+ this.chunks.push(buffer);
38
+ }
39
+ u32(value) {
40
+ const buffer = Buffer.alloc(4);
41
+ buffer.writeUInt32LE(value >>> 0, 0);
42
+ this.chunks.push(buffer);
43
+ }
44
+ str(text) {
45
+ const buffer = Buffer.from(text, 'utf8');
46
+ this.u16(buffer.length);
47
+ this.chunks.push(buffer);
48
+ }
49
+ bytes(data) {
50
+ this.chunks.push(Buffer.from(data));
51
+ }
52
+ /** Zero-pads so the next write lands on an n-byte boundary from the pack start. */
53
+ align(alignment) {
54
+ const length = this.chunks.reduce((total, chunk) => total + chunk.length, 0);
55
+ const padding = (alignment - (length % alignment)) % alignment;
56
+ if (padding)
57
+ this.chunks.push(Buffer.alloc(padding));
58
+ }
59
+ done() {
60
+ return Buffer.concat(this.chunks);
61
+ }
91
62
  }
92
-
93
63
  /** Writes one glyph record (9 bytes). */
94
64
  function writeGlyph(writer, glyph) {
95
- writer.u32(glyph.bitmapOffset);
96
- writer.u8(glyph.width);
97
- writer.u8(glyph.height);
98
- writer.i8(glyph.xOffset);
99
- writer.i8(glyph.yOffset);
100
- writer.u8(glyph.advance);
65
+ writer.u32(glyph.bitmapOffset);
66
+ writer.u8(glyph.width);
67
+ writer.u8(glyph.height);
68
+ writer.i8(glyph.xOffset);
69
+ writer.i8(glyph.yOffset);
70
+ writer.u8(glyph.advance);
101
71
  }
102
-
103
72
  /**
104
73
  * Serializes baked images + fonts into an ERPK pack.
105
74
  *
106
- * @param {object} opts
107
- * @param {Array<object>} [opts.images] Results of bakeImage().
108
- * @param {Array<object>} [opts.fonts] Results of bakeFont().
109
- * @returns {Buffer} The pack bytes.
75
+ * @returns The pack bytes.
110
76
  */
111
- export function emitAssetPack({images = [], fonts = []}) {
112
- // Emit in a locale-independent sorted order: the discovery maps upstream are filled by esbuild
113
- // onLoad callbacks in whatever order the bundler visits files, which varies run to run — and an
114
- // unstable pack means unstable in-flash addresses and non-reproducible builds.
115
- const byName = key => (left, right) =>
116
- left[key] < right[key] ? -1 : left[key] > right[key] ? 1 : 0;
117
- images = [...images].sort(byName('name'));
118
- fonts = [...fonts].sort(byName('family'));
119
-
120
- const version = 2;
121
- const writer = new Writer();
122
- writer.bytes(Buffer.from('ERPK', 'ascii'));
123
- writer.u32(version);
124
- writer.u32(images.length);
125
- const fontSizes = fonts.reduce((total, font) => total + font.sizes.length, 0);
126
- writer.u32(fontSizes);
127
-
128
- for (const image of images) {
129
- writer.str(image.name);
130
- writer.u32(image.width);
131
- writer.u32(image.height);
132
- if (version >= 2) {
133
- writer.u8(image.format === 'rgb565' ? FORMAT_RGB565 : FORMAT_ARGB8888);
134
- writer.align(PIXEL_ALIGNMENT); // the engine reads the pixels in place; keep the pointer 4-byte aligned
77
+ export function emitAssetPack({ images = [], fonts = [], }) {
78
+ // Emit in a locale-independent sorted order: the discovery maps upstream are filled by esbuild
79
+ // onLoad callbacks in whatever order the bundler visits files, which varies run to run — and an
80
+ // unstable pack means unstable in-flash addresses and non-reproducible builds.
81
+ const byName = (key) => (left, right) => left[key] < right[key] ? -1 : left[key] > right[key] ? 1 : 0;
82
+ images = [...images].sort(byName('name'));
83
+ fonts = [...fonts].sort(byName('family'));
84
+ const version = 2;
85
+ const writer = new Writer();
86
+ writer.bytes(Buffer.from('ERPK', 'ascii'));
87
+ writer.u32(version);
88
+ writer.u32(images.length);
89
+ const fontSizes = fonts.reduce((total, font) => total + font.sizes.length, 0);
90
+ writer.u32(fontSizes);
91
+ for (const image of images) {
92
+ writer.str(image.name);
93
+ writer.u32(image.width);
94
+ writer.u32(image.height);
95
+ if (version >= 2) {
96
+ writer.u8(image.format === 'rgb565' ? FORMAT_RGB565 : FORMAT_ARGB8888);
97
+ writer.align(PIXEL_ALIGNMENT); // the engine reads the pixels in place; keep the pointer 4-byte aligned
98
+ }
99
+ // pixels is a Uint32Array (premultiplied ARGB words) or Uint16Array (RGB565);
100
+ // copy its LE bytes verbatim.
101
+ writer.bytes(Buffer.from(image.pixels.buffer, image.pixels.byteOffset, image.pixels.byteLength));
135
102
  }
136
- // pixels is a Uint32Array (premultiplied ARGB words) or Uint16Array (RGB565);
137
- // copy its LE bytes verbatim.
138
- writer.bytes(
139
- Buffer.from(
140
- image.pixels.buffer,
141
- image.pixels.byteOffset,
142
- image.pixels.byteLength,
143
- ),
144
- );
145
- }
146
-
147
- for (const font of fonts) {
148
- for (const size of font.sizes) {
149
- writer.str(font.family);
150
- writer.u8(size.pixelSize);
151
- writer.u8(size.lineHeight);
152
- writer.u8(size.baseline);
153
- writer.u8(size.bpp);
154
- writer.u16(size.first);
155
- writer.u16(size.last);
156
- writer.u16(size.dense.length);
157
- writer.u16(size.extras.length);
158
- writer.u32(size.bitmap.length);
159
- for (const glyph of size.dense) writeGlyph(writer, glyph);
160
- for (const extra of size.extras) {
161
- writer.u32(extra.codepoint);
162
- writeGlyph(writer, extra.info);
163
- }
164
- writer.bytes(Buffer.from(size.bitmap));
103
+ for (const font of fonts) {
104
+ for (const size of font.sizes) {
105
+ writer.str(font.family);
106
+ writer.u8(size.pixelSize);
107
+ writer.u8(size.lineHeight);
108
+ writer.u8(size.baseline);
109
+ writer.u8(size.bpp);
110
+ writer.u16(size.first);
111
+ writer.u16(size.last);
112
+ writer.u16(size.dense.length);
113
+ writer.u16(size.extras.length);
114
+ writer.u32(size.bitmap.length);
115
+ for (const glyph of size.dense)
116
+ writeGlyph(writer, glyph);
117
+ for (const extra of size.extras) {
118
+ writer.u32(extra.codepoint);
119
+ writeGlyph(writer, extra.info);
120
+ }
121
+ writer.bytes(Buffer.from(size.bitmap));
122
+ }
165
123
  }
166
- }
167
-
168
- return writer.done();
124
+ return writer.done();
169
125
  }