embedded-react 0.19.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/NOTICE +5 -0
  2. package/README.md +8 -3
  3. package/aot/app-file-loader.mjs +116 -0
  4. package/aot/compile.mjs +180 -90
  5. package/aot/compiler/animations.mjs +568 -317
  6. package/aot/compiler/boot.mjs +97 -35
  7. package/aot/compiler/branch-merge.mjs +4 -3
  8. package/aot/compiler/check.mjs +70 -18
  9. package/aot/compiler/collect.mjs +787 -288
  10. package/aot/compiler/contexts.mjs +391 -0
  11. package/aot/compiler/control-flow.mjs +439 -268
  12. package/aot/compiler/custom-hooks.mjs +686 -0
  13. package/aot/compiler/diagnostics.mjs +22 -25
  14. package/aot/compiler/elements/activity-indicator.mjs +25 -24
  15. package/aot/compiler/elements/button.mjs +182 -0
  16. package/aot/compiler/elements/dial.mjs +91 -77
  17. package/aot/compiler/elements/flat-list.mjs +24 -22
  18. package/aot/compiler/elements/image-background.mjs +76 -0
  19. package/aot/compiler/elements/image.mjs +52 -50
  20. package/aot/compiler/elements/modal.mjs +31 -30
  21. package/aot/compiler/elements/props.mjs +61 -26
  22. package/aot/compiler/elements/rewrite.mjs +118 -0
  23. package/aot/compiler/elements/section-list.mjs +370 -0
  24. package/aot/compiler/elements/switch.mjs +42 -34
  25. package/aot/compiler/elements/text-input.mjs +34 -32
  26. package/aot/compiler/expressions.mjs +1685 -392
  27. package/aot/compiler/handlers.mjs +2029 -547
  28. package/aot/compiler/helper-values.mjs +312 -0
  29. package/aot/compiler/ir/functions.mjs +11 -1
  30. package/aot/compiler/ir/nodes.mjs +43 -4
  31. package/aot/compiler/ir/reads.mjs +223 -22
  32. package/aot/compiler/ir/text.mjs +5 -1
  33. package/aot/compiler/ir/types.mjs +26 -14
  34. package/aot/compiler/ir/vectors.mjs +5 -1
  35. package/aot/compiler/kept-states.mjs +177 -0
  36. package/aot/compiler/keyboard.mjs +17 -7
  37. package/aot/compiler/list-items.mjs +226 -0
  38. package/aot/compiler/list-order.mjs +586 -0
  39. package/aot/compiler/list-split.mjs +259 -0
  40. package/aot/compiler/loops.mjs +582 -0
  41. package/aot/compiler/lower.mjs +246 -111
  42. package/aot/compiler/native-files.mjs +439 -0
  43. package/aot/compiler/native-header.mjs +335 -0
  44. package/aot/compiler/native-modules.mjs +1173 -0
  45. package/aot/compiler/nodes.mjs +195 -172
  46. package/aot/compiler/options.mjs +37 -31
  47. package/aot/compiler/out.mjs +32 -23
  48. package/aot/compiler/package/animations.mjs +779 -0
  49. package/aot/compiler/package/arena.mjs +252 -0
  50. package/aot/compiler/package/assemble.mjs +175 -0
  51. package/aot/compiler/package/bytecode.mjs +315 -0
  52. package/aot/compiler/package/checks.mjs +1026 -0
  53. package/aot/compiler/package/codegen.mjs +2168 -0
  54. package/aot/compiler/package/enums.mjs +148 -0
  55. package/aot/compiler/package/format.mjs +771 -0
  56. package/aot/compiler/package/layouts.mjs +136 -0
  57. package/aot/compiler/package/lower.mjs +2103 -0
  58. package/aot/compiler/package/props.mjs +118 -0
  59. package/aot/compiler/package/string-caps.mjs +515 -0
  60. package/aot/compiler/package/tables.mjs +127 -0
  61. package/aot/compiler/package/vectors.mjs +502 -0
  62. package/aot/compiler/package/writer.mjs +686 -0
  63. package/aot/compiler/pan-responder.mjs +69 -61
  64. package/aot/compiler/parse.mjs +23 -21
  65. package/aot/compiler/record-shapes.mjs +246 -0
  66. package/aot/compiler/records.mjs +654 -0
  67. package/aot/compiler/reducers.mjs +272 -0
  68. package/aot/compiler/static-eval.mjs +340 -30
  69. package/aot/compiler/style-text.mjs +178 -174
  70. package/aot/compiler/svg.mjs +480 -251
  71. package/aot/compiler/switch-cases.mjs +198 -0
  72. package/aot/compiler/tagged-records.mjs +450 -0
  73. package/aot/compiler/typing.mjs +307 -0
  74. package/aot/compiler/updaters.mjs +320 -0
  75. package/aot/compiler/window-dimensions.mjs +94 -0
  76. package/aot/compiler/worked-values.mjs +319 -0
  77. package/aot/demo-file.mjs +30 -0
  78. package/aot/screenshot-smoke.mjs +54 -40
  79. package/aot/style-map.mjs +152 -92
  80. package/assets/bake-font.mjs +123 -77
  81. package/assets/bake-image.mjs +39 -22
  82. package/assets/bake-svg.mjs +361 -262
  83. package/assets/build-builtin-font.mjs +11 -7
  84. package/assets/emit-c.mjs +77 -58
  85. package/assets/emit-container.mjs +123 -37
  86. package/assets/emit-pack.mjs +77 -67
  87. package/assets/font-config.mjs +9 -6
  88. package/assets/font-sizes.mjs +163 -123
  89. package/assets/glyph-coverage.mjs +74 -54
  90. package/assets/index.mjs +31 -9
  91. package/assets/rasterize.mjs +126 -99
  92. package/assets/svg-loader.mjs +8 -8
  93. package/build.mjs +23 -13
  94. package/cli.mjs +402 -172
  95. package/compile-bin.mjs +3 -3
  96. package/flash.mjs +537 -0
  97. package/hotreload/dev-device.mjs +90 -33
  98. package/hotreload/pack-app.mjs +55 -43
  99. package/hotreload/pack-split.mjs +62 -64
  100. package/hotreload/package-transfer.mjs +175 -0
  101. package/hotreload/protocol.mjs +50 -37
  102. package/hotreload/uploader.mjs +38 -27
  103. package/hotreload/watch-tree.mjs +19 -19
  104. package/pack-container.mjs +37 -21
  105. package/package.json +7 -8
  106. package/persist-transform.mjs +34 -28
  107. package/qjsc-wasm.mjs +25 -14
  108. package/sim/embedded-react.cjs +1 -1
  109. package/sim/embedded-react.js +1 -1
  110. package/sim/embedded-react.wasm +0 -0
  111. package/sim/index.html +94 -85
  112. package/sim-server.mjs +113 -94
  113. package/src/embedded-react/Animated.js +24 -24
  114. package/src/embedded-react/Dimensions.js +37 -0
  115. package/src/embedded-react/Easing.js +1 -1
  116. package/src/embedded-react/FlatList.js +3 -3
  117. package/src/embedded-react/LayoutAnimation.js +8 -5
  118. package/src/embedded-react/PanResponder.js +75 -71
  119. package/src/embedded-react/Pressable.js +2 -8
  120. package/src/embedded-react/SectionList.js +7 -9
  121. package/src/embedded-react/StyleSheet.js +4 -3
  122. package/src/embedded-react/TouchableOpacity.js +13 -8
  123. package/src/embedded-react/index.d.ts +88 -8
  124. package/src/embedded-react/index.js +2 -0
  125. package/src/embedded-react/layout-anim-config.js +10 -4
  126. package/src/embedded-react/list-child.js +5 -5
  127. package/src/embedded-react/named-colors.js +1 -1
  128. package/src/embedded-react/nativeModule.js +462 -0
  129. package/src/embedded-react/split-style.js +11 -11
  130. package/src/embedded-react/svg-ops.js +760 -593
  131. package/src/embedded-react/useHostValue.js +3 -3
  132. package/src/embedded-react/usePersistentState.js +6 -6
  133. package/src/embedded-react/warn-props.js +1 -1
  134. package/src/host-config.js +60 -53
  135. package/src/props.js +49 -41
  136. package/src/renderer.js +7 -4
  137. package/aot/compiler/assemble.mjs +0 -944
  138. package/aot/compiler/c/animations.mjs +0 -436
  139. package/aot/compiler/c/boot.mjs +0 -127
  140. package/aot/compiler/c/expressions.mjs +0 -270
  141. package/aot/compiler/c/functions.mjs +0 -268
  142. package/aot/compiler/c/keyboard.mjs +0 -96
  143. package/aot/compiler/c/nodes.mjs +0 -303
  144. package/aot/compiler/c/pan-responder.mjs +0 -136
  145. package/aot/compiler/c/statements.mjs +0 -319
  146. package/aot/compiler/c/text.mjs +0 -83
  147. package/aot/compiler/c/vectors.mjs +0 -381
  148. package/aot/compiler/c-syntax.mjs +0 -127
@@ -31,8 +31,9 @@ import {emitBuiltinFont} from './emit-c.mjs';
31
31
 
32
32
  const here = dirname(fileURLToPath(import.meta.url)); // packages/embedded-react/assets
33
33
  const repoRoot = resolve(here, '../../..');
34
- const FONT = resolve(repoRoot, 'assets/fonts/Inter-Regular.ttf');
35
- const OUT = resolve(repoRoot, 'engine/font/font_data.c');
34
+ const FONT_PATH = resolve(repoRoot, 'assets/fonts/Inter-Regular.ttf');
35
+ const OUTPUT_PATH = resolve(repoRoot, 'engine/font/font_data.c');
36
+ const BITS_PER_PIXEL = 4;
36
37
 
37
38
  // The built-in covers printable ASCII plus a fixed set of common symbols (degrees, arrows, math,
38
39
  // punctuation, etc.) the UI components rely on — kept stable across regenerations. The set lives in
@@ -41,18 +42,21 @@ const OUT = resolve(repoRoot, 'engine/font/font_data.c');
41
42
  const SIZES = BUILTIN_SIZES;
42
43
 
43
44
  const font = bakeFont({
44
- path: FONT,
45
+ path: FONT_PATH,
45
46
  family: 'Inter',
46
47
  sizes: SIZES,
47
- bpp: 4,
48
+ bpp: BITS_PER_PIXEL,
48
49
  glyphs: BUILTIN_EXTRAS,
49
50
  });
50
51
  writeFileSync(
51
- OUT,
52
+ OUTPUT_PATH,
52
53
  emitBuiltinFont({font, symbol: 'inter', sourceName: 'Inter-Regular.ttf'}),
53
54
  );
54
55
 
55
- const bytes = font.sizes.reduce((n, s) => n + s.bitmap.length, 0);
56
+ const bitmapBytes = font.sizes.reduce(
57
+ (total, size) => total + size.bitmap.length,
58
+ 0,
59
+ );
56
60
  console.log(
57
- `Regenerated ${OUT}\n ${SIZES.length} sizes [${SIZES.join(',')}], bpp 4, ${font.sizes[0].extras.length} extra glyphs, ${bytes} bitmap bytes`,
61
+ `Regenerated ${OUTPUT_PATH}\n ${SIZES.length} sizes [${SIZES.join(',')}], bpp ${BITS_PER_PIXEL}, ${font.sizes[0].extras.length} extra glyphs, ${bitmapBytes} bitmap bytes`,
58
62
  );
package/assets/emit-c.mjs CHANGED
@@ -19,27 +19,35 @@
19
19
  // (er_scene.h, which exposes both er_image_load/er_font_register and the BitmapFont format), so it
20
20
  // drops into any target's build the same way the bundle does.
21
21
 
22
+ const ARGB8888_HEX_DIGITS = 8;
23
+ const RGB565_HEX_DIGITS = 4;
24
+ const BYTE_HEX_DIGITS = 2;
25
+ const ARGB8888_WORDS_PER_LINE = 8;
26
+ const RGB565_WORDS_PER_LINE = 12;
27
+ const BITMAP_BYTES_PER_LINE = 16;
28
+ const DEFAULT_BPP = 4;
29
+
22
30
  /** Sanitizes an asset/family name into a C identifier fragment. */
23
31
  function cIdent(name) {
24
- let id = String(name).replace(/[^0-9a-zA-Z_]/g, '_');
25
- if (id && /[0-9]/.test(id[0])) id = '_' + id;
26
- return id || '_asset';
32
+ let identifier = String(name).replace(/[^0-9a-zA-Z_]/g, '_');
33
+ if (identifier && /[0-9]/.test(identifier[0])) identifier = '_' + identifier;
34
+ return identifier || '_asset';
27
35
  }
28
36
 
29
37
  /** Formats an array of byte/word strings into wrapped C initializer lines. */
30
38
  function rows(values, perLine, indent = ' ') {
31
- const out = [];
32
- for (let i = 0; i < values.length; i += perLine) {
33
- out.push(indent + values.slice(i, i + perLine).join(', ') + ',');
39
+ const lines = [];
40
+ for (let start = 0; start < values.length; start += perLine) {
41
+ lines.push(indent + values.slice(start, start + perLine).join(', ') + ',');
34
42
  }
35
- return out.join('\n');
43
+ return lines.join('\n');
36
44
  }
37
45
 
38
46
  /** Emits the GlyphInfo designated-initializer for one glyph. */
39
- function glyphInit(g) {
47
+ function glyphInit(glyph) {
40
48
  return (
41
- `{ .bitmap_offset = ${g.bitmapOffset}, .width = ${g.width}, .height = ${g.height}, ` +
42
- `.x_offset = ${g.xOffset}, .y_offset = ${g.yOffset}, .advance = ${g.advance} }`
49
+ `{ .bitmap_offset = ${glyph.bitmapOffset}, .width = ${glyph.width}, .height = ${glyph.height}, ` +
50
+ `.x_offset = ${glyph.xOffset}, .y_offset = ${glyph.yOffset}, .advance = ${glyph.advance} }`
43
51
  );
44
52
  }
45
53
 
@@ -54,7 +62,7 @@ function glyphInit(g) {
54
62
  */
55
63
  export function emitAssetsC({headerName, images = [], fonts = []}) {
56
64
  const guard = 'ER_' + cIdent(headerName).toUpperCase();
57
- const h =
65
+ const header =
58
66
  `/* Generated by packages/embedded-react/assets — DO NOT EDIT. */\n` +
59
67
  `#ifndef ${guard}\n#define ${guard}\n\n` +
60
68
  `#ifdef __cplusplus\nextern "C"\n{\n#endif\n\n` +
@@ -81,26 +89,28 @@ export function emitAssetsC({headerName, images = [], fonts = []}) {
81
89
  lines.push(``);
82
90
 
83
91
  // --- Images ---------------------------------------------------------------------------------
84
- for (const img of images) {
85
- const id = cIdent(img.name);
86
- const is565 = img.format === 'rgb565';
92
+ for (const image of images) {
93
+ const identifier = cIdent(image.name);
94
+ const is565 = image.format === 'rgb565';
87
95
  const hex = Array.from(
88
- img.pixels,
89
- p =>
96
+ image.pixels,
97
+ pixel =>
90
98
  '0x' +
91
- p
99
+ pixel
92
100
  .toString(16)
93
101
  .toUpperCase()
94
- .padStart(is565 ? 4 : 8, '0') +
102
+ .padStart(is565 ? RGB565_HEX_DIGITS : ARGB8888_HEX_DIGITS, '0') +
95
103
  'u',
96
104
  );
97
105
  lines.push(
98
- `/* image "${img.name}" — ${img.width}x${img.height}${is565 ? ' (RGB565)' : ''} */`,
106
+ `/* image "${image.name}" — ${image.width}x${image.height}${is565 ? ' (RGB565)' : ''} */`,
99
107
  );
100
108
  lines.push(
101
- `static const ${is565 ? 'uint16_t' : 'uint32_t'} ${id}_px[] = {`,
109
+ `static const ${is565 ? 'uint16_t' : 'uint32_t'} ${identifier}_px[] = {`,
110
+ );
111
+ lines.push(
112
+ rows(hex, is565 ? RGB565_WORDS_PER_LINE : ARGB8888_WORDS_PER_LINE),
102
113
  );
103
- lines.push(rows(hex, is565 ? 12 : 8));
104
114
  lines.push(`};`);
105
115
  lines.push(``);
106
116
  }
@@ -108,14 +118,14 @@ export function emitAssetsC({headerName, images = [], fonts = []}) {
108
118
  // --- Fonts ----------------------------------------------------------------------------------
109
119
  const fontRegistrations = [];
110
120
  for (const font of fonts) {
111
- const famId = cIdent(font.family);
121
+ const familyIdentifier = cIdent(font.family);
112
122
  lines.push(
113
- `/* font "${font.family}" — ${font.sizes.map(s => `${s.pixelSize}px`).join(', ')} */`,
123
+ `/* font "${font.family}" — ${font.sizes.map(size => `${size.pixelSize}px`).join(', ')} */`,
114
124
  );
115
- emitFontSizes(lines, font, famId, 'static const '); // file-local: only er_register_assets refers to these
116
- for (const sz of font.sizes) {
125
+ emitFontSizes(lines, font, familyIdentifier, 'static const '); // file-local: only er_register_assets refers to these
126
+ for (const size of font.sizes) {
117
127
  fontRegistrations.push(
118
- ` er_font_register(${JSON.stringify(font.family)}, &g_font_${famId}_${sz.pixelSize});`,
128
+ ` er_font_register(${JSON.stringify(font.family)}, &g_font_${familyIdentifier}_${size.pixelSize});`,
119
129
  );
120
130
  }
121
131
  }
@@ -123,70 +133,79 @@ export function emitAssetsC({headerName, images = [], fonts = []}) {
123
133
  // --- Registration entry point ---------------------------------------------------------------
124
134
  lines.push(`void er_register_assets(void)`);
125
135
  lines.push(`{`);
126
- for (const img of images) {
127
- const loadFn =
128
- img.format === 'rgb565' ? 'er_image_load_rgb565' : 'er_image_load';
136
+ for (const image of images) {
137
+ const loadFunction =
138
+ image.format === 'rgb565' ? 'er_image_load_rgb565' : 'er_image_load';
129
139
  lines.push(
130
- ` ${loadFn}(${JSON.stringify(img.name)}, ${cIdent(img.name)}_px, ${img.width}, ${img.height});`,
140
+ ` ${loadFunction}(${JSON.stringify(image.name)}, ${cIdent(image.name)}_px, ${image.width}, ${image.height});`,
131
141
  );
132
142
  }
133
- for (const reg of fontRegistrations) lines.push(reg);
143
+ for (const registration of fontRegistrations) lines.push(registration);
134
144
  if (images.length === 0 && fontRegistrations.length === 0) {
135
145
  lines.push(` /* no baked assets */`);
136
146
  }
137
147
  lines.push(`}`);
138
148
  lines.push(``);
139
149
 
140
- return {c: lines.join('\n'), h};
150
+ return {c: lines.join('\n'), h: header};
141
151
  }
142
152
 
143
153
  /** Emits the per-size BitmapFont blocks (bitmap + glyphs + extras + struct) for one font. */
144
154
  function emitFontSizes(lines, font, symbol, fontStorage) {
145
- for (const sz of font.sizes) {
146
- const prefix = `s_${symbol}_${sz.pixelSize}`;
147
- const hex = sz.bitmap.map(
148
- b => '0x' + b.toString(16).toUpperCase().padStart(2, '0'),
155
+ for (const size of font.sizes) {
156
+ const prefix = `s_${symbol}_${size.pixelSize}`;
157
+ const hex = size.bitmap.map(
158
+ byte =>
159
+ '0x' + byte.toString(16).toUpperCase().padStart(BYTE_HEX_DIGITS, '0'),
160
+ );
161
+ lines.push(
162
+ `static const uint8_t ${prefix}_bitmap[${size.bitmap.length || 1}] = {`,
149
163
  );
150
164
  lines.push(
151
- `static const uint8_t ${prefix}_bitmap[${sz.bitmap.length || 1}] = {`,
165
+ size.bitmap.length ? rows(hex, BITMAP_BYTES_PER_LINE) : ' 0x00,',
152
166
  );
153
- lines.push(sz.bitmap.length ? rows(hex, 16) : ' 0x00,');
154
167
  lines.push(`};`);
155
168
  lines.push(``);
156
169
 
157
170
  lines.push(
158
- `static const GlyphInfo ${prefix}_glyphs[${sz.dense.length}] = {`,
171
+ `static const GlyphInfo ${prefix}_glyphs[${size.dense.length}] = {`,
159
172
  );
160
- for (const g of sz.dense) lines.push(` ${glyphInit(g)},`);
173
+ for (const glyph of size.dense) lines.push(` ${glyphInit(glyph)},`);
161
174
  lines.push(`};`);
162
175
  lines.push(``);
163
176
 
164
- if (sz.extras.length) {
177
+ if (size.extras.length) {
165
178
  lines.push(
166
- `static const ExtraGlyph ${prefix}_extras[${sz.extras.length}] = {`,
179
+ `static const ExtraGlyph ${prefix}_extras[${size.extras.length}] = {`,
167
180
  );
168
- for (const e of sz.extras) {
181
+ for (const extra of size.extras) {
169
182
  lines.push(
170
- ` { .codepoint = 0x${e.codepoint.toString(16).toUpperCase()}, .info = ${glyphInit(e.info)} },`,
183
+ ` { .codepoint = 0x${extra.codepoint.toString(16).toUpperCase()}, .info = ${glyphInit(extra.info)} },`,
171
184
  );
172
185
  }
173
186
  lines.push(`};`);
174
187
  lines.push(``);
175
188
  }
176
189
 
177
- lines.push(`${fontStorage}BitmapFont g_font_${symbol}_${sz.pixelSize} = {`);
190
+ lines.push(
191
+ `${fontStorage}BitmapFont g_font_${symbol}_${size.pixelSize} = {`,
192
+ );
178
193
  lines.push(` .bitmap = ${prefix}_bitmap,`);
179
194
  lines.push(` .glyphs = ${prefix}_glyphs,`);
180
195
  lines.push(
181
- ` .extras = ${sz.extras.length ? `${prefix}_extras` : 'NULL'},`,
196
+ ` .extras = ${size.extras.length ? `${prefix}_extras` : 'NULL'},`,
197
+ );
198
+ lines.push(` .extras_count = ${size.extras.length},`);
199
+ lines.push(
200
+ ` .first = 0x${size.first.toString(16).toUpperCase()},`,
201
+ );
202
+ lines.push(
203
+ ` .last = 0x${size.last.toString(16).toUpperCase()},`,
182
204
  );
183
- lines.push(` .extras_count = ${sz.extras.length},`);
184
- lines.push(` .first = 0x${sz.first.toString(16).toUpperCase()},`);
185
- lines.push(` .last = 0x${sz.last.toString(16).toUpperCase()},`);
186
- lines.push(` .pixel_size = ${sz.pixelSize},`);
187
- lines.push(` .line_height = ${sz.lineHeight},`);
188
- lines.push(` .baseline = ${sz.baseline},`);
189
- lines.push(` .format = ${sz.bpp},`);
205
+ lines.push(` .pixel_size = ${size.pixelSize},`);
206
+ lines.push(` .line_height = ${size.lineHeight},`);
207
+ lines.push(` .baseline = ${size.baseline},`);
208
+ lines.push(` .format = ${size.bpp},`);
190
209
  lines.push(`};`);
191
210
  lines.push(``);
192
211
  }
@@ -204,14 +223,14 @@ function emitFontSizes(lines, font, symbol, fontStorage) {
204
223
  * @returns {string} The .c source text.
205
224
  */
206
225
  export function emitBuiltinFont({font, symbol = 'inter', sourceName = ''}) {
207
- const sizesStr = font.sizes.map(s => s.pixelSize).join(',');
208
- const bpp = font.sizes[0] ? font.sizes[0].bpp : 4;
226
+ const sizeList = font.sizes.map(size => size.pixelSize).join(',');
227
+ const bpp = font.sizes[0] ? font.sizes[0].bpp : DEFAULT_BPP;
209
228
  const lines = [];
210
229
  lines.push(
211
230
  `/* AUTO-GENERATED by packages/embedded-react/assets/build-builtin-font.mjs — do not edit by hand.`,
212
231
  );
213
232
  lines.push(` * Source : ${sourceName}`);
214
- lines.push(` * Sizes : ${sizesStr} (bpp ${bpp}, family "${font.family}")`);
233
+ lines.push(` * Sizes : ${sizeList} (bpp ${bpp}, family "${font.family}")`);
215
234
  lines.push(
216
235
  ` * Regenerate: cd packages/embedded-react && npm run build:builtin-font`,
217
236
  );
@@ -223,8 +242,8 @@ export function emitBuiltinFont({font, symbol = 'inter', sourceName = ''}) {
223
242
  emitFontSizes(lines, font, symbol, 'const '); // file-scope const: referenced by g_<symbol>_sizes
224
243
 
225
244
  lines.push(`const BitmapFont *const g_${symbol}_sizes[] = {`);
226
- for (const sz of font.sizes)
227
- lines.push(` &g_font_${symbol}_${sz.pixelSize},`);
245
+ for (const size of font.sizes)
246
+ lines.push(` &g_font_${symbol}_${size.pixelSize},`);
228
247
  lines.push(`};`);
229
248
  lines.push(
230
249
  `const size_t g_${symbol}_sizes_count = sizeof(g_${symbol}_sizes) / sizeof(g_${symbol}_sizes[0]);`,
@@ -50,6 +50,16 @@
50
50
 
51
51
  const FORMAT_VERSION = 1;
52
52
 
53
+ const HEADER_BYTES = 12; // magic + format_version + crc32
54
+ const TAG_LENGTH_BYTES = 2;
55
+ const SECTION_COUNT_BYTES = 4;
56
+ const SECTION_HEADER_BYTES = 8; // type + len
57
+ const SECTION_ALIGNMENT = 4;
58
+
59
+ const CRC32_POLYNOMIAL = 0xedb88320;
60
+ const CRC32_INVERT = 0xffffffff;
61
+ const BITS_PER_BYTE = 8;
62
+
53
63
  export const SECTION_PAD = 0;
54
64
  export const SECTION_BYTECODE = 1;
55
65
  export const SECTION_ASSET_PACK = 2;
@@ -61,18 +71,18 @@ export const SECTION_VENDOR_BYTECODE = 3;
61
71
  * container, the hot-reload transport frame that carries it, and the device's verification of both).
62
72
  * hotreload/protocol.mjs imports this rather than keeping its own.
63
73
  *
64
- * @param {Buffer|Uint8Array} buf Bytes to checksum.
74
+ * @param {Buffer|Uint8Array} data Bytes to checksum.
65
75
  * @returns {number} The CRC as an unsigned 32-bit integer.
66
76
  */
67
- export function crc32(buf) {
68
- let crc = 0xffffffff;
69
- for (let i = 0; i < buf.length; i++) {
70
- crc ^= buf[i];
71
- for (let b = 0; b < 8; b++) {
72
- crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1));
77
+ 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));
73
83
  }
74
84
  }
75
- return (crc ^ 0xffffffff) >>> 0;
85
+ return (crc ^ CRC32_INVERT) >>> 0;
76
86
  }
77
87
 
78
88
  /** Accumulates little-endian binary chunks (mirrors emit-pack.mjs's Writer). */
@@ -80,23 +90,23 @@ class Writer {
80
90
  constructor() {
81
91
  this.chunks = [];
82
92
  }
83
- u16(v) {
84
- const b = Buffer.alloc(2);
85
- b.writeUInt16LE(v & 0xffff, 0);
86
- this.chunks.push(b);
93
+ u16(value) {
94
+ const buffer = Buffer.alloc(2);
95
+ buffer.writeUInt16LE(value & 0xffff, 0);
96
+ this.chunks.push(buffer);
87
97
  }
88
- u32(v) {
89
- const b = Buffer.alloc(4);
90
- b.writeUInt32LE(v >>> 0, 0);
91
- this.chunks.push(b);
98
+ u32(value) {
99
+ const buffer = Buffer.alloc(4);
100
+ buffer.writeUInt32LE(value >>> 0, 0);
101
+ this.chunks.push(buffer);
92
102
  }
93
- str(s) {
94
- const b = Buffer.from(s, 'utf8');
95
- this.u16(b.length);
96
- this.chunks.push(b);
103
+ str(text) {
104
+ const buffer = Buffer.from(text, 'utf8');
105
+ this.u16(buffer.length);
106
+ this.chunks.push(buffer);
97
107
  }
98
- bytes(buf) {
99
- this.chunks.push(Buffer.from(buf));
108
+ bytes(data) {
109
+ this.chunks.push(Buffer.from(data));
100
110
  }
101
111
  done() {
102
112
  return Buffer.concat(this.chunks);
@@ -135,31 +145,107 @@ export function emitContainer({bytecode, vendorBytecode, assetPack, qjsTag}) {
135
145
  // 4-align the asset pack's first byte relative to the container (see the alignment contract above)
136
146
  // by inserting a type-0 padding section ahead of it when the layout calls for one. Walk the layout
137
147
  // once to place pads, then write — section_count must already include them.
138
- const withPads = [];
139
- let off = 12 + 2 + Buffer.byteLength(qjsTag, 'utf8') + 4; // header + tag + section_count
148
+ const paddedSections = [];
149
+ let offset =
150
+ HEADER_BYTES +
151
+ TAG_LENGTH_BYTES +
152
+ Buffer.byteLength(qjsTag, 'utf8') +
153
+ SECTION_COUNT_BYTES;
140
154
  for (const [type, data] of sections) {
141
155
  if (type === SECTION_ASSET_PACK) {
142
- const pad = (4 - ((off + 8) % 4)) % 4; // +8 = this section's own type+len header
143
- if (pad) {
144
- withPads.push([SECTION_PAD, Buffer.alloc(pad)]);
145
- off += 8 + pad;
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;
146
164
  }
147
165
  }
148
- withPads.push([type, data]);
149
- off += 8 + data.length;
166
+ paddedSections.push([type, data]);
167
+ offset += SECTION_HEADER_BYTES + data.length;
150
168
  }
151
169
 
152
- body.u32(withPads.length);
153
- for (const [type, data] of withPads) {
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]);
183
+ }
184
+
185
+ export const SECTION_PROGRAM = 4;
186
+ export const SECTION_DEBUG = 5;
187
+
188
+ /**
189
+ * Serializes a Flow B package into an ERCF container tagged `erb`: the asset pack, the program, then the debug
190
+ * table. Every section starts 4-aligned in the container, since the runtime reads each in place.
191
+ *
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.
198
+ */
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;
226
+ }
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) {
154
240
  body.u32(type);
155
241
  body.u32(data.length);
156
242
  body.bytes(data);
157
243
  }
158
244
  const bodyBytes = body.done();
159
245
 
160
- const head = new Writer();
161
- head.bytes(Buffer.from('ERCF', 'ascii'));
162
- head.u32(FORMAT_VERSION);
163
- head.u32(crc32(bodyBytes));
164
- return Buffer.concat([head.done(), bodyBytes]);
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};
165
251
  }