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
@@ -19,7 +19,7 @@
19
19
  // (there is no Python step). font_registry.c falls back to g_inter_sizes[] for any text without a
20
20
  // matching custom family.
21
21
  //
22
- // cd bridges/quickjs/js && npm run build:builtin-font
22
+ // cd packages/embedded-react && npm run build:builtin-font
23
23
  //
24
24
  // Regenerating changes glyph metrics slightly vs a prior rasterizer, so re-run the engine text tests
25
25
  // (test_text, yoga_parity) and re-flash to eyeball on-device after changing it.
@@ -29,10 +29,11 @@ import {writeFileSync} from 'node:fs';
29
29
  import {bakeFont, BUILTIN_EXTRAS, BUILTIN_SIZES} from './bake-font.mjs';
30
30
  import {emitBuiltinFont} from './emit-c.mjs';
31
31
 
32
- const here = dirname(fileURLToPath(import.meta.url)); // bridges/quickjs/js/assets
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');
32
+ const here = dirname(fileURLToPath(import.meta.url)); // packages/embedded-react/assets
33
+ const repoRoot = resolve(here, '../../..');
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,8 +62,8 @@ function glyphInit(g) {
54
62
  */
55
63
  export function emitAssetsC({headerName, images = [], fonts = []}) {
56
64
  const guard = 'ER_' + cIdent(headerName).toUpperCase();
57
- const h =
58
- `/* Generated by bridges/quickjs/js/assets — DO NOT EDIT. */\n` +
65
+ const header =
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` +
61
69
  ` /** @brief Registers every baked image and font with the engine (call once at boot, after\n` +
@@ -65,7 +73,9 @@ export function emitAssetsC({headerName, images = [], fonts = []}) {
65
73
  `#endif\n`;
66
74
 
67
75
  const lines = [];
68
- lines.push(`/* Generated by bridges/quickjs/js/assets — DO NOT EDIT. */`);
76
+ lines.push(
77
+ `/* Generated by packages/embedded-react/assets — DO NOT EDIT. */`,
78
+ );
69
79
  lines.push(
70
80
  `/* Images: premultiplied ARGB8888 (or RGB565 when baked 16-bit), referenced by pointer. Fonts: baked BitmapFont per size. */`,
71
81
  );
@@ -79,26 +89,28 @@ export function emitAssetsC({headerName, images = [], fonts = []}) {
79
89
  lines.push(``);
80
90
 
81
91
  // --- Images ---------------------------------------------------------------------------------
82
- for (const img of images) {
83
- const id = cIdent(img.name);
84
- const is565 = img.format === 'rgb565';
92
+ for (const image of images) {
93
+ const identifier = cIdent(image.name);
94
+ const is565 = image.format === 'rgb565';
85
95
  const hex = Array.from(
86
- img.pixels,
87
- p =>
96
+ image.pixels,
97
+ pixel =>
88
98
  '0x' +
89
- p
99
+ pixel
90
100
  .toString(16)
91
101
  .toUpperCase()
92
- .padStart(is565 ? 4 : 8, '0') +
102
+ .padStart(is565 ? RGB565_HEX_DIGITS : ARGB8888_HEX_DIGITS, '0') +
93
103
  'u',
94
104
  );
95
105
  lines.push(
96
- `/* image "${img.name}" — ${img.width}x${img.height}${is565 ? ' (RGB565)' : ''} */`,
106
+ `/* image "${image.name}" — ${image.width}x${image.height}${is565 ? ' (RGB565)' : ''} */`,
107
+ );
108
+ lines.push(
109
+ `static const ${is565 ? 'uint16_t' : 'uint32_t'} ${identifier}_px[] = {`,
97
110
  );
98
111
  lines.push(
99
- `static const ${is565 ? 'uint16_t' : 'uint32_t'} ${id}_px[] = {`,
112
+ rows(hex, is565 ? RGB565_WORDS_PER_LINE : ARGB8888_WORDS_PER_LINE),
100
113
  );
101
- lines.push(rows(hex, is565 ? 12 : 8));
102
114
  lines.push(`};`);
103
115
  lines.push(``);
104
116
  }
@@ -106,14 +118,14 @@ export function emitAssetsC({headerName, images = [], fonts = []}) {
106
118
  // --- Fonts ----------------------------------------------------------------------------------
107
119
  const fontRegistrations = [];
108
120
  for (const font of fonts) {
109
- const famId = cIdent(font.family);
121
+ const familyIdentifier = cIdent(font.family);
110
122
  lines.push(
111
- `/* font "${font.family}" — ${font.sizes.map(s => `${s.pixelSize}px`).join(', ')} */`,
123
+ `/* font "${font.family}" — ${font.sizes.map(size => `${size.pixelSize}px`).join(', ')} */`,
112
124
  );
113
- emitFontSizes(lines, font, famId, 'static const '); // file-local: only er_register_assets refers to these
114
- 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) {
115
127
  fontRegistrations.push(
116
- ` 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});`,
117
129
  );
118
130
  }
119
131
  }
@@ -121,70 +133,79 @@ export function emitAssetsC({headerName, images = [], fonts = []}) {
121
133
  // --- Registration entry point ---------------------------------------------------------------
122
134
  lines.push(`void er_register_assets(void)`);
123
135
  lines.push(`{`);
124
- for (const img of images) {
125
- const loadFn =
126
- 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';
127
139
  lines.push(
128
- ` ${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});`,
129
141
  );
130
142
  }
131
- for (const reg of fontRegistrations) lines.push(reg);
143
+ for (const registration of fontRegistrations) lines.push(registration);
132
144
  if (images.length === 0 && fontRegistrations.length === 0) {
133
145
  lines.push(` /* no baked assets */`);
134
146
  }
135
147
  lines.push(`}`);
136
148
  lines.push(``);
137
149
 
138
- return {c: lines.join('\n'), h};
150
+ return {c: lines.join('\n'), h: header};
139
151
  }
140
152
 
141
153
  /** Emits the per-size BitmapFont blocks (bitmap + glyphs + extras + struct) for one font. */
142
154
  function emitFontSizes(lines, font, symbol, fontStorage) {
143
- for (const sz of font.sizes) {
144
- const prefix = `s_${symbol}_${sz.pixelSize}`;
145
- const hex = sz.bitmap.map(
146
- 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'),
147
160
  );
148
161
  lines.push(
149
- `static const uint8_t ${prefix}_bitmap[${sz.bitmap.length || 1}] = {`,
162
+ `static const uint8_t ${prefix}_bitmap[${size.bitmap.length || 1}] = {`,
163
+ );
164
+ lines.push(
165
+ size.bitmap.length ? rows(hex, BITMAP_BYTES_PER_LINE) : ' 0x00,',
150
166
  );
151
- lines.push(sz.bitmap.length ? rows(hex, 16) : ' 0x00,');
152
167
  lines.push(`};`);
153
168
  lines.push(``);
154
169
 
155
170
  lines.push(
156
- `static const GlyphInfo ${prefix}_glyphs[${sz.dense.length}] = {`,
171
+ `static const GlyphInfo ${prefix}_glyphs[${size.dense.length}] = {`,
157
172
  );
158
- for (const g of sz.dense) lines.push(` ${glyphInit(g)},`);
173
+ for (const glyph of size.dense) lines.push(` ${glyphInit(glyph)},`);
159
174
  lines.push(`};`);
160
175
  lines.push(``);
161
176
 
162
- if (sz.extras.length) {
177
+ if (size.extras.length) {
163
178
  lines.push(
164
- `static const ExtraGlyph ${prefix}_extras[${sz.extras.length}] = {`,
179
+ `static const ExtraGlyph ${prefix}_extras[${size.extras.length}] = {`,
165
180
  );
166
- for (const e of sz.extras) {
181
+ for (const extra of size.extras) {
167
182
  lines.push(
168
- ` { .codepoint = 0x${e.codepoint.toString(16).toUpperCase()}, .info = ${glyphInit(e.info)} },`,
183
+ ` { .codepoint = 0x${extra.codepoint.toString(16).toUpperCase()}, .info = ${glyphInit(extra.info)} },`,
169
184
  );
170
185
  }
171
186
  lines.push(`};`);
172
187
  lines.push(``);
173
188
  }
174
189
 
175
- lines.push(`${fontStorage}BitmapFont g_font_${symbol}_${sz.pixelSize} = {`);
190
+ lines.push(
191
+ `${fontStorage}BitmapFont g_font_${symbol}_${size.pixelSize} = {`,
192
+ );
176
193
  lines.push(` .bitmap = ${prefix}_bitmap,`);
177
194
  lines.push(` .glyphs = ${prefix}_glyphs,`);
178
195
  lines.push(
179
- ` .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()},`,
180
204
  );
181
- lines.push(` .extras_count = ${sz.extras.length},`);
182
- lines.push(` .first = 0x${sz.first.toString(16).toUpperCase()},`);
183
- lines.push(` .last = 0x${sz.last.toString(16).toUpperCase()},`);
184
- lines.push(` .pixel_size = ${sz.pixelSize},`);
185
- lines.push(` .line_height = ${sz.lineHeight},`);
186
- lines.push(` .baseline = ${sz.baseline},`);
187
- 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},`);
188
209
  lines.push(`};`);
189
210
  lines.push(``);
190
211
  }
@@ -202,16 +223,16 @@ function emitFontSizes(lines, font, symbol, fontStorage) {
202
223
  * @returns {string} The .c source text.
203
224
  */
204
225
  export function emitBuiltinFont({font, symbol = 'inter', sourceName = ''}) {
205
- const sizesStr = font.sizes.map(s => s.pixelSize).join(',');
206
- 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;
207
228
  const lines = [];
208
229
  lines.push(
209
- `/* AUTO-GENERATED by bridges/quickjs/js/assets/build-builtin-font.mjs — do not edit by hand.`,
230
+ `/* AUTO-GENERATED by packages/embedded-react/assets/build-builtin-font.mjs — do not edit by hand.`,
210
231
  );
211
232
  lines.push(` * Source : ${sourceName}`);
212
- lines.push(` * Sizes : ${sizesStr} (bpp ${bpp}, family "${font.family}")`);
233
+ lines.push(` * Sizes : ${sizeList} (bpp ${bpp}, family "${font.family}")`);
213
234
  lines.push(
214
- ` * Regenerate: cd bridges/quickjs/js && npm run build:builtin-font`,
235
+ ` * Regenerate: cd packages/embedded-react && npm run build:builtin-font`,
215
236
  );
216
237
  lines.push(` */`);
217
238
  lines.push(`#include "font_bitmap.h"`);
@@ -221,8 +242,8 @@ export function emitBuiltinFont({font, symbol = 'inter', sourceName = ''}) {
221
242
  emitFontSizes(lines, font, symbol, 'const '); // file-scope const: referenced by g_<symbol>_sizes
222
243
 
223
244
  lines.push(`const BitmapFont *const g_${symbol}_sizes[] = {`);
224
- for (const sz of font.sizes)
225
- lines.push(` &g_font_${symbol}_${sz.pixelSize},`);
245
+ for (const size of font.sizes)
246
+ lines.push(` &g_font_${symbol}_${size.pixelSize},`);
226
247
  lines.push(`};`);
227
248
  lines.push(
228
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;
@@ -57,22 +67,22 @@ export const SECTION_VENDOR_BYTECODE = 3;
57
67
 
58
68
  /**
59
69
  * CRC-32/IEEE (zlib polynomial 0xEDB88320), computed without a lookup table to match er_crc32() in
60
- * bridges/quickjs/er_runtime.c byte-for-byte — the one checksum of the whole delivery pipeline (this
70
+ * runtime/er_container.c byte-for-byte — the one checksum of the whole delivery pipeline (this
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
  }