Skip to main content

hyprstream_ts_codegen/ts_codegen/
message.rs

1//! Generates the `capnp.ts` shared runtime.
2//!
3//! This is a self-contained Cap'n Proto single-segment message builder/reader
4//! for TypeScript. No external dependencies required.
5
6/// Generate the capnp.ts runtime file content.
7pub fn generate_capnp_runtime() -> String {
8    r##"// Generated by hyprstream-ts-codegen — DO NOT EDIT
9//
10// Cap'n Proto single-segment message builder/reader for TypeScript.
11// Implements the Cap'n Proto wire format without external dependencies.
12//
13// Message frame: [4B: segment_count-1] [4B: segment0_size_words] [segment0_bytes...]
14// Struct pointer: [offset_words:30 | 0b00:2] [data_words:16 | ptr_words:16]
15// List pointer:   [offset_words:30 | 0b01:2] [element_size:3 | element_count:29]
16// Text/Data:      List(UInt8) with NUL terminator for Text
17
18const WORD_SIZE = 8;
19
20// Module-level singletons — avoid per-call allocation
21const encoder = new TextEncoder();
22const decoder = new TextDecoder();
23
24/**
25 * Single-segment Cap'n Proto message builder / arena.
26 *
27 * JS-heap backed — grows on demand via Uint8Array reallocation.
28 */
29export class CapnpArena {
30  private buf: DataView;
31  private raw: Uint8Array;
32  /** Total allocated size in bytes */
33  private capacity: number;
34  /** Next free byte offset (for appending pointer targets) */
35  private allocOffset: number;
36  /** Byte offset where the root struct's data section starts */
37  private readonly rootDataOffset: number;
38  /** Byte offset where the root struct's pointer section starts */
39  private readonly rootPtrOffset: number;
40  private readonly dataWords: number;
41  private readonly ptrWords: number;
42
43  constructor(
44    dataWords: number,
45    ptrWords: number,
46  ) {
47    this.dataWords = dataWords;
48    this.ptrWords = ptrWords;
49
50    const structSize = (dataWords + ptrWords) * WORD_SIZE;
51    const HEADER = 16;
52
53    this.capacity = HEADER + structSize + 1024;
54    this.raw = new Uint8Array(this.capacity);
55    this.buf = new DataView(this.raw.buffer, this.raw.byteOffset, this.raw.byteLength);
56
57    this.rootDataOffset = HEADER;
58    this.rootPtrOffset  = HEADER + dataWords * WORD_SIZE;
59    this.allocOffset    = HEADER + structSize;
60  }
61
62  /** The raw backing buffer (for diagnostics / direct access). */
63  backingBuffer(): Uint8Array { return this.raw; }
64
65  /** Ensure capacity for `needed` more bytes, growing if necessary. */
66  private ensureCapacity(needed: number): void {
67    if (this.allocOffset + needed <= this.capacity) return;
68    const newCap = Math.max(this.capacity * 2, this.allocOffset + needed + 256);
69    const newBuf = new Uint8Array(newCap);
70    newBuf.set(this.raw);
71    this.raw = newBuf;
72    this.buf = new DataView(this.raw.buffer, this.raw.byteOffset, this.raw.byteLength);
73    this.capacity = newCap;
74  }
75
76  /** Word-align the allocation offset. */
77  private alignAlloc(): void {
78    const rem = this.allocOffset % WORD_SIZE;
79    if (rem !== 0) {
80      this.allocOffset += WORD_SIZE - rem;
81    }
82  }
83
84  // --- Data section setters ---
85
86  setBool(byteOffset: number, bitIndex: number, value: boolean): void {
87    const off = this.rootDataOffset + byteOffset;
88    if (value) {
89      this.raw[off] |= 1 << bitIndex;
90    } else {
91      this.raw[off] &= ~(1 << bitIndex);
92    }
93  }
94
95  setUint8(byteOffset: number, value: number): void {
96    this.buf.setUint8(this.rootDataOffset + byteOffset, value);
97  }
98
99  setUint16(byteOffset: number, value: number): void {
100    this.buf.setUint16(this.rootDataOffset + byteOffset, value, true);
101  }
102
103  setUint32(byteOffset: number, value: number): void {
104    this.buf.setUint32(this.rootDataOffset + byteOffset, value, true);
105  }
106
107  setUint64(byteOffset: number, value: bigint): void {
108    this.buf.setBigUint64(this.rootDataOffset + byteOffset, value, true);
109  }
110
111  setInt8(byteOffset: number, value: number): void {
112    this.buf.setInt8(this.rootDataOffset + byteOffset, value);
113  }
114
115  setInt16(byteOffset: number, value: number): void {
116    this.buf.setInt16(this.rootDataOffset + byteOffset, value, true);
117  }
118
119  setInt32(byteOffset: number, value: number): void {
120    this.buf.setInt32(this.rootDataOffset + byteOffset, value, true);
121  }
122
123  setInt64(byteOffset: number, value: bigint): void {
124    this.buf.setBigInt64(this.rootDataOffset + byteOffset, value, true);
125  }
126
127  setFloat32(byteOffset: number, value: number): void {
128    this.buf.setFloat32(this.rootDataOffset + byteOffset, value, true);
129  }
130
131  setFloat64(byteOffset: number, value: number): void {
132    this.buf.setFloat64(this.rootDataOffset + byteOffset, value, true);
133  }
134
135  // --- Pointer section setters ---
136
137  /**
138   * Set a Text pointer at the given pointer index.
139   * Encodes as a list of bytes with NUL terminator.
140   */
141  setText(ptrIndex: number, value: string): void {
142    const encoded = encoder.encode(value);
143    const byteLen = encoded.length + 1; // +1 for NUL
144    const wordLen = Math.ceil(byteLen / WORD_SIZE);
145
146    this.alignAlloc();
147    this.ensureCapacity(wordLen * WORD_SIZE);
148
149    const targetOffset = this.allocOffset;
150
151    // Copy string bytes + NUL
152    this.raw.set(encoded, targetOffset);
153    this.raw[targetOffset + encoded.length] = 0; // NUL
154
155    // Write list pointer: element_size=2 (byte), count=byteLen
156    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
157    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
158    // List pointer: B=1, C=offset, D=element_size(2=byte), E=count
159    const lo = ((offsetWords << 2) | 1) >>> 0;
160    const hi = ((byteLen << 3) | 2) >>> 0;
161    this.buf.setUint32(ptrOffset, lo, true);
162    this.buf.setUint32(ptrOffset + 4, hi, true);
163
164    this.allocOffset = targetOffset + wordLen * WORD_SIZE;
165  }
166
167  /**
168   * Set a Data pointer at the given pointer index.
169   * Encodes as a list of bytes (no NUL terminator).
170   */
171  setData(ptrIndex: number, value: Uint8Array): void {
172    const byteLen = value.length;
173    const wordLen = Math.ceil(byteLen / WORD_SIZE) || 0;
174
175    if (byteLen === 0) return; // null pointer = zero bytes = already zero
176
177    this.alignAlloc();
178    this.ensureCapacity(wordLen * WORD_SIZE);
179
180    const targetOffset = this.allocOffset;
181    this.raw.set(value, targetOffset);
182
183    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
184    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
185    const lo = ((offsetWords << 2) | 1) >>> 0;
186    const hi = ((byteLen << 3) | 2) >>> 0;
187    this.buf.setUint32(ptrOffset, lo, true);
188    this.buf.setUint32(ptrOffset + 4, hi, true);
189
190    this.allocOffset = targetOffset + wordLen * WORD_SIZE;
191  }
192
193  /**
194   * Set a Text list pointer at the given pointer index.
195   * Encodes as a composite list of struct pointers.
196   */
197  setTextList(ptrIndex: number, values: string[]): void {
198    if (values.length === 0) return;
199
200    this.alignAlloc();
201
202    // For List(Text), each element is a pointer (1 pointer word per element)
203    // Composite list: tag word + N * (0 data + 1 pointer) words
204    const tagOffset = this.allocOffset;
205    const elementCount = values.length;
206    // Each element is a struct with 0 data words and 1 pointer word
207    const elementWords = 1; // 0 data + 1 pointer = 1 word per element
208    const totalWords = 1 + elementCount * elementWords; // 1 tag + N elements
209
210    this.ensureCapacity(totalWords * WORD_SIZE + values.reduce((a, v) => a + v.length + 9, 0));
211
212    // Write tag word (struct pointer describing element shape)
213    // Struct pointer: offset = elementCount, data_words = 0, ptr_words = 1
214    const tagLo = ((elementCount << 2) | 0) >>> 0;
215    const tagHi = ((0 << 0) | (1 << 16)) >>> 0;
216    this.buf.setUint32(tagOffset, tagLo, true);
217    this.buf.setUint32(tagOffset + 4, tagHi, true);
218
219    this.allocOffset = tagOffset + WORD_SIZE + elementCount * elementWords * WORD_SIZE;
220
221    // Write list pointer
222    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
223    const listTargetOffset = tagOffset;
224    const offsetWords = (listTargetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
225    // Composite list: element_size=7 (composite), count=totalWords (words after tag)
226    const lo = ((offsetWords << 2) | 1) >>> 0;
227    const hi = ((elementCount * elementWords << 3) | 7) >>> 0;
228    this.buf.setUint32(ptrOffset, lo, true);
229    this.buf.setUint32(ptrOffset + 4, hi, true);
230
231    // Now write each element's pointer (pointing to text data)
232    for (let i = 0; i < values.length; i++) {
233      const elementPtrOffset = tagOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
234      // Write text data after all elements
235      const encoded = encoder.encode(values[i]);
236      const byteLen = encoded.length + 1;
237      const wordLen = Math.ceil(byteLen / WORD_SIZE);
238
239      this.alignAlloc();
240      this.ensureCapacity(wordLen * WORD_SIZE);
241
242      const textOffset = this.allocOffset;
243      this.raw.set(encoded, textOffset);
244      this.raw[textOffset + encoded.length] = 0;
245
246      const textOffsetWords = (textOffset - elementPtrOffset - WORD_SIZE) / WORD_SIZE;
247      const textLo = ((textOffsetWords << 2) | 1) >>> 0;
248      const textHi = ((byteLen << 3) | 2) >>> 0;
249      this.buf.setUint32(elementPtrOffset, textLo, true);
250      this.buf.setUint32(elementPtrOffset + 4, textHi, true);
251
252      this.allocOffset = textOffset + wordLen * WORD_SIZE;
253    }
254  }
255
256  /**
257   * Set a Data list pointer (List(Data)) at the given pointer index.
258   * Encodes as a composite list of byte-blob pointers (no NUL terminator).
259   */
260  setDataList(ptrIndex: number, values: Uint8Array[]): void {
261    if (values.length === 0) return;
262
263    this.alignAlloc();
264
265    const tagOffset = this.allocOffset;
266    const elementCount = values.length;
267    const elementWords = 1; // 0 data + 1 pointer per element
268    const totalWords = 1 + elementCount * elementWords;
269
270    this.ensureCapacity(totalWords * WORD_SIZE + values.reduce((a, v) => a + v.length + 8, 0));
271
272    const tagLo = ((elementCount << 2) | 0) >>> 0;
273    const tagHi = ((0 << 0) | (1 << 16)) >>> 0;
274    this.buf.setUint32(tagOffset, tagLo, true);
275    this.buf.setUint32(tagOffset + 4, tagHi, true);
276
277    this.allocOffset = tagOffset + WORD_SIZE + elementCount * elementWords * WORD_SIZE;
278
279    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
280    const offsetWords = (tagOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
281    const lo = ((offsetWords << 2) | 1) >>> 0;
282    const hi = ((elementCount * elementWords << 3) | 7) >>> 0;
283    this.buf.setUint32(ptrOffset, lo, true);
284    this.buf.setUint32(ptrOffset + 4, hi, true);
285
286    for (let i = 0; i < values.length; i++) {
287      const elementPtrOffset = tagOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
288      const byteLen = values[i].length;
289      const wordLen = Math.ceil(byteLen / WORD_SIZE);
290
291      this.alignAlloc();
292      this.ensureCapacity(wordLen * WORD_SIZE);
293
294      const dataOffset = this.allocOffset;
295      this.raw.set(values[i], dataOffset);
296
297      const dataOffsetWords = (dataOffset - elementPtrOffset - WORD_SIZE) / WORD_SIZE;
298      const dataLo = ((dataOffsetWords << 2) | 1) >>> 0;
299      const dataHi = ((byteLen << 3) | 2) >>> 0;
300      this.buf.setUint32(elementPtrOffset, dataLo, true);
301      this.buf.setUint32(elementPtrOffset + 4, dataHi, true);
302
303      this.allocOffset = dataOffset + wordLen * WORD_SIZE;
304    }
305  }
306
307  /**
308   * Allocate a fixed-width primitive list (List(Bool)/List(UIntN)/List(IntN)/List(FloatN))
309   * pointer at `ptrIndex`. `elemSize` is the capnp list element-size tag
310   * (1=bit, 2=1-byte, 3=2-byte, 4=4-byte, 5=8-byte); `byteWidth` is the
311   * per-element byte width (ignored for bit-packed bool lists).
312   * Returns the byte offset of the first element, or -1 for an empty list.
313   */
314  /**
315   * Same as `allocPrimitiveList`, but takes the absolute byte offset of the
316   * pointer word to write (rather than deriving it from `ptrIndex` against
317   * the root pointer section). Shared by the root-level list setters and by
318   * `List(List(_))` nested-list setters, whose inner-list pointer lives at a
319   * computed offset inside the allocated outer pointer list, not at a
320   * `ptrIndex` slot.
321   */
322  private allocPrimitiveListAt(ptrOffset: number, count: number, elemSize: number, byteWidth: number): number {
323    if (count === 0) return -1;
324
325    this.alignAlloc();
326    const totalBytes = elemSize === 1 ? Math.ceil(count / 8) : count * byteWidth;
327    const wordLen = Math.ceil(totalBytes / WORD_SIZE);
328    this.ensureCapacity(wordLen * WORD_SIZE);
329
330    const targetOffset = this.allocOffset;
331    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
332    const lo = ((offsetWords << 2) | 1) >>> 0;
333    const hi = ((count << 3) | elemSize) >>> 0;
334    this.buf.setUint32(ptrOffset, lo, true);
335    this.buf.setUint32(ptrOffset + 4, hi, true);
336
337    this.allocOffset = targetOffset + wordLen * WORD_SIZE;
338    return targetOffset;
339  }
340
341  private allocPrimitiveList(ptrIndex: number, count: number, elemSize: number, byteWidth: number): number {
342    return this.allocPrimitiveListAt(this.rootPtrOffset + ptrIndex * WORD_SIZE, count, elemSize, byteWidth);
343  }
344
345  /**
346   * Allocate the outer pointer-list structure for a `List(List(<inner>))`
347   * field at `ptrIndex`: a plain pointer list (element_size=6) with one
348   * pointer-word slot reserved per outer element. Each slot is later filled
349   * in by `allocPrimitiveListAt` when the corresponding inner list is
350   * written. Returns the byte offset of the first outer-element pointer
351   * slot, or -1 for an empty outer list (leaving the field's pointer null).
352   */
353  private allocNestedListOuter(ptrIndex: number, outerCount: number): number {
354    if (outerCount === 0) return -1;
355
356    this.alignAlloc();
357    this.ensureCapacity(outerCount * WORD_SIZE);
358
359    const outerBase = this.allocOffset;
360    this.allocOffset = outerBase + outerCount * WORD_SIZE;
361
362    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
363    const offsetWords = (outerBase - ptrOffset - WORD_SIZE) / WORD_SIZE;
364    const lo = ((offsetWords << 2) | 1) >>> 0;
365    const hi = ((outerCount << 3) | 6) >>> 0; // element_size=6: pointer list
366    this.buf.setUint32(ptrOffset, lo, true);
367    this.buf.setUint32(ptrOffset + 4, hi, true);
368
369    return outerBase;
370  }
371
372  setBoolList(ptrIndex: number, values: boolean[]): void {
373    const base = this.allocPrimitiveList(ptrIndex, values.length, 1, 0);
374    if (base < 0) return;
375    for (let i = 0; i < values.length; i++) {
376      if (values[i]) this.raw[base + (i >> 3)] |= 1 << (i & 7);
377    }
378  }
379
380  setUint8List(ptrIndex: number, values: number[]): void {
381    const base = this.allocPrimitiveList(ptrIndex, values.length, 2, 1);
382    if (base < 0) return;
383    for (let i = 0; i < values.length; i++) this.buf.setUint8(base + i, values[i]);
384  }
385
386  setInt8List(ptrIndex: number, values: number[]): void {
387    const base = this.allocPrimitiveList(ptrIndex, values.length, 2, 1);
388    if (base < 0) return;
389    for (let i = 0; i < values.length; i++) this.buf.setInt8(base + i, values[i]);
390  }
391
392  setUint16List(ptrIndex: number, values: number[]): void {
393    const base = this.allocPrimitiveList(ptrIndex, values.length, 3, 2);
394    if (base < 0) return;
395    for (let i = 0; i < values.length; i++) this.buf.setUint16(base + i * 2, values[i], true);
396  }
397
398  setInt16List(ptrIndex: number, values: number[]): void {
399    const base = this.allocPrimitiveList(ptrIndex, values.length, 3, 2);
400    if (base < 0) return;
401    for (let i = 0; i < values.length; i++) this.buf.setInt16(base + i * 2, values[i], true);
402  }
403
404  setUint32List(ptrIndex: number, values: number[]): void {
405    const base = this.allocPrimitiveList(ptrIndex, values.length, 4, 4);
406    if (base < 0) return;
407    for (let i = 0; i < values.length; i++) this.buf.setUint32(base + i * 4, values[i], true);
408  }
409
410  setInt32List(ptrIndex: number, values: number[]): void {
411    const base = this.allocPrimitiveList(ptrIndex, values.length, 4, 4);
412    if (base < 0) return;
413    for (let i = 0; i < values.length; i++) this.buf.setInt32(base + i * 4, values[i], true);
414  }
415
416  setFloat32List(ptrIndex: number, values: number[]): void {
417    const base = this.allocPrimitiveList(ptrIndex, values.length, 4, 4);
418    if (base < 0) return;
419    for (let i = 0; i < values.length; i++) this.buf.setFloat32(base + i * 4, values[i], true);
420  }
421
422  setUint64List(ptrIndex: number, values: bigint[]): void {
423    const base = this.allocPrimitiveList(ptrIndex, values.length, 5, 8);
424    if (base < 0) return;
425    for (let i = 0; i < values.length; i++) this.buf.setBigUint64(base + i * 8, values[i], true);
426  }
427
428  setInt64List(ptrIndex: number, values: bigint[]): void {
429    const base = this.allocPrimitiveList(ptrIndex, values.length, 5, 8);
430    if (base < 0) return;
431    for (let i = 0; i < values.length; i++) this.buf.setBigInt64(base + i * 8, values[i], true);
432  }
433
434  setFloat64List(ptrIndex: number, values: number[]): void {
435    const base = this.allocPrimitiveList(ptrIndex, values.length, 5, 8);
436    if (base < 0) return;
437    for (let i = 0; i < values.length; i++) this.buf.setFloat64(base + i * 8, values[i], true);
438  }
439
440  // --- Nested List(List(<primitive>)) setters ---
441  //
442  // Each outer element is a pointer to an inner primitive list, allocated via
443  // `allocNestedListOuter` (outer pointer-list shell) + `allocPrimitiveListAt`
444  // (each inner list, written at the outer element's reserved pointer slot).
445
446  setBoolListList(ptrIndex: number, values: boolean[][]): void {
447    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
448    if (outerBase < 0) return;
449    for (let i = 0; i < values.length; i++) {
450      const inner = values[i];
451      if (inner.length === 0) continue;
452      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 1, 0);
453      for (let j = 0; j < inner.length; j++) {
454        if (inner[j]) this.raw[base + (j >> 3)] |= 1 << (j & 7);
455      }
456    }
457  }
458
459  setUint8ListList(ptrIndex: number, values: number[][]): void {
460    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
461    if (outerBase < 0) return;
462    for (let i = 0; i < values.length; i++) {
463      const inner = values[i];
464      if (inner.length === 0) continue;
465      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 2, 1);
466      for (let j = 0; j < inner.length; j++) this.buf.setUint8(base + j, inner[j]);
467    }
468  }
469
470  setInt8ListList(ptrIndex: number, values: number[][]): void {
471    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
472    if (outerBase < 0) return;
473    for (let i = 0; i < values.length; i++) {
474      const inner = values[i];
475      if (inner.length === 0) continue;
476      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 2, 1);
477      for (let j = 0; j < inner.length; j++) this.buf.setInt8(base + j, inner[j]);
478    }
479  }
480
481  setUint16ListList(ptrIndex: number, values: number[][]): void {
482    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
483    if (outerBase < 0) return;
484    for (let i = 0; i < values.length; i++) {
485      const inner = values[i];
486      if (inner.length === 0) continue;
487      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 3, 2);
488      for (let j = 0; j < inner.length; j++) this.buf.setUint16(base + j * 2, inner[j], true);
489    }
490  }
491
492  setInt16ListList(ptrIndex: number, values: number[][]): void {
493    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
494    if (outerBase < 0) return;
495    for (let i = 0; i < values.length; i++) {
496      const inner = values[i];
497      if (inner.length === 0) continue;
498      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 3, 2);
499      for (let j = 0; j < inner.length; j++) this.buf.setInt16(base + j * 2, inner[j], true);
500    }
501  }
502
503  setUint32ListList(ptrIndex: number, values: number[][]): void {
504    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
505    if (outerBase < 0) return;
506    for (let i = 0; i < values.length; i++) {
507      const inner = values[i];
508      if (inner.length === 0) continue;
509      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 4, 4);
510      for (let j = 0; j < inner.length; j++) this.buf.setUint32(base + j * 4, inner[j], true);
511    }
512  }
513
514  setInt32ListList(ptrIndex: number, values: number[][]): void {
515    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
516    if (outerBase < 0) return;
517    for (let i = 0; i < values.length; i++) {
518      const inner = values[i];
519      if (inner.length === 0) continue;
520      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 4, 4);
521      for (let j = 0; j < inner.length; j++) this.buf.setInt32(base + j * 4, inner[j], true);
522    }
523  }
524
525  setFloat32ListList(ptrIndex: number, values: number[][]): void {
526    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
527    if (outerBase < 0) return;
528    for (let i = 0; i < values.length; i++) {
529      const inner = values[i];
530      if (inner.length === 0) continue;
531      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 4, 4);
532      for (let j = 0; j < inner.length; j++) this.buf.setFloat32(base + j * 4, inner[j], true);
533    }
534  }
535
536  setUint64ListList(ptrIndex: number, values: bigint[][]): void {
537    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
538    if (outerBase < 0) return;
539    for (let i = 0; i < values.length; i++) {
540      const inner = values[i];
541      if (inner.length === 0) continue;
542      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 5, 8);
543      for (let j = 0; j < inner.length; j++) this.buf.setBigUint64(base + j * 8, inner[j], true);
544    }
545  }
546
547  setInt64ListList(ptrIndex: number, values: bigint[][]): void {
548    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
549    if (outerBase < 0) return;
550    for (let i = 0; i < values.length; i++) {
551      const inner = values[i];
552      if (inner.length === 0) continue;
553      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 5, 8);
554      for (let j = 0; j < inner.length; j++) this.buf.setBigInt64(base + j * 8, inner[j], true);
555    }
556  }
557
558  setFloat64ListList(ptrIndex: number, values: number[][]): void {
559    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
560    if (outerBase < 0) return;
561    for (let i = 0; i < values.length; i++) {
562      const inner = values[i];
563      if (inner.length === 0) continue;
564      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 5, 8);
565      for (let j = 0; j < inner.length; j++) this.buf.setFloat64(base + j * 8, inner[j], true);
566    }
567  }
568
569  /**
570   * Initialize a sub-struct at the given pointer index.
571   * Returns a StructBuilder for writing fields into the sub-struct.
572   */
573  initStruct(ptrIndex: number, dataWords: number, ptrWords: number): StructBuilder {
574    const structSize = (dataWords + ptrWords) * WORD_SIZE;
575    this.alignAlloc();
576    this.ensureCapacity(structSize);
577
578    const targetOffset = this.allocOffset;
579
580    // Write struct pointer
581    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
582    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
583    const lo = ((offsetWords << 2) | 0) >>> 0;
584    const hi = ((dataWords & 0xFFFF) | ((ptrWords & 0xFFFF) << 16)) >>> 0;
585    this.buf.setUint32(ptrOffset, lo, true);
586    this.buf.setUint32(ptrOffset + 4, hi, true);
587
588    this.allocOffset = targetOffset + structSize;
589
590    return new StructBuilder(this, targetOffset, dataWords, ptrWords);
591  }
592
593  /**
594   * Initialize a List(Struct) composite list at the given pointer index.
595   * Returns a StructBuilder[] for writing fields into each element.
596   */
597  initStructList(ptrIndex: number, count: number, dataWords: number, ptrWords: number): StructBuilder[] {
598    if (count === 0) return [];
599
600    this.alignAlloc();
601    const tagOffset = this.allocOffset;
602    const elementWords = dataWords + ptrWords;
603    const totalBodyWords = count * elementWords;
604
605    this.ensureCapacity((1 + totalBodyWords) * WORD_SIZE);
606
607    // Write tag word (struct pointer describing element shape)
608    const tagLo = ((count << 2) | 0) >>> 0;
609    const tagHi = ((dataWords & 0xFFFF) | ((ptrWords & 0xFFFF) << 16)) >>> 0;
610    this.buf.setUint32(tagOffset, tagLo, true);
611    this.buf.setUint32(tagOffset + 4, tagHi, true);
612
613    // Reserve space for all elements (pointer targets will be appended after)
614    this.allocOffset = tagOffset + WORD_SIZE + totalBodyWords * WORD_SIZE;
615
616    // Write composite list pointer
617    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
618    const offsetWords = (tagOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
619    const lo = ((offsetWords << 2) | 1) >>> 0;
620    const hi = ((totalBodyWords << 3) | 7) >>> 0;
621    this.buf.setUint32(ptrOffset, lo, true);
622    this.buf.setUint32(ptrOffset + 4, hi, true);
623
624    // Create StructBuilder for each element slot
625    const builders: StructBuilder[] = [];
626    for (let i = 0; i < count; i++) {
627      const elemStart = tagOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
628      builders.push(new StructBuilder(this, elemStart, dataWords, ptrWords));
629    }
630    return builders;
631  }
632
633  /** Finalize the message and return the wire-format bytes (view, no copy). */
634  finish(): Uint8Array {
635    // Write root struct pointer at bytes 8-15.
636    // The root struct data starts at byte 16 = 0 words after the end of this pointer word.
637    // Struct pointer: lo = (offsetWords << 2) | 0b00, hi = dataWords | (ptrWords << 16)
638    this.buf.setUint32(8, 0, true); // lo: offset_words=0, type=struct(0b00)
639    this.buf.setUint32(12, (this.dataWords & 0xffff) | ((this.ptrWords & 0xffff) << 16), true);
640
641    // Segment covers bytes 8..allocOffset (everything after the 8-byte segment table).
642    const segmentBytes = this.allocOffset - 8;
643    const segmentWords = Math.ceil(segmentBytes / WORD_SIZE);
644
645    // Write segment table
646    this.buf.setUint32(0, 0, true); // segment count - 1 = 0 (single segment)
647    this.buf.setUint32(4, segmentWords, true);
648
649    const view = this.raw.subarray(0, 8 + segmentWords * WORD_SIZE);
650
651    return view;
652  }
653
654  /** Access the underlying buffer (for StructBuilder). */
655  _getRaw(): Uint8Array { return this.raw; }
656  _getBuf(): DataView { return this.buf; }
657  _getAllocOffset(): number { return this.allocOffset; }
658  _setAllocOffset(off: number): void { this.allocOffset = off; }
659  _ensureCapacity(n: number): void { this.ensureCapacity(n); }
660  _alignAlloc(): void { this.alignAlloc(); }
661}
662
663/**
664 * Builder for a sub-struct within a message.
665 */
666export class StructBuilder {
667  private msg: CapnpArena;
668  private dataOffset: number;
669  private ptrOffset: number;
670
671  constructor(
672    msg: CapnpArena,
673    structStart: number,
674    dataWords: number,
675    _ptrWords: number,
676  ) {
677    this.msg = msg;
678    this.dataOffset = structStart;
679    this.ptrOffset = structStart + dataWords * WORD_SIZE;
680  }
681
682  setBool(byteOffset: number, bitIndex: number, value: boolean): void {
683    const off = this.dataOffset + byteOffset;
684    const raw = this.msg._getRaw();
685    if (value) {
686      raw[off] |= 1 << bitIndex;
687    } else {
688      raw[off] &= ~(1 << bitIndex);
689    }
690  }
691
692  setUint8(byteOffset: number, value: number): void {
693    this.msg._getBuf().setUint8(this.dataOffset + byteOffset, value);
694  }
695
696  setUint16(byteOffset: number, value: number): void {
697    this.msg._getBuf().setUint16(this.dataOffset + byteOffset, value, true);
698  }
699
700  setUint32(byteOffset: number, value: number): void {
701    this.msg._getBuf().setUint32(this.dataOffset + byteOffset, value, true);
702  }
703
704  setUint64(byteOffset: number, value: bigint): void {
705    this.msg._getBuf().setBigUint64(this.dataOffset + byteOffset, value, true);
706  }
707
708  setInt8(byteOffset: number, value: number): void {
709    this.msg._getBuf().setInt8(this.dataOffset + byteOffset, value);
710  }
711
712  setInt16(byteOffset: number, value: number): void {
713    this.msg._getBuf().setInt16(this.dataOffset + byteOffset, value, true);
714  }
715
716  setInt32(byteOffset: number, value: number): void {
717    this.msg._getBuf().setInt32(this.dataOffset + byteOffset, value, true);
718  }
719
720  setInt64(byteOffset: number, value: bigint): void {
721    this.msg._getBuf().setBigInt64(this.dataOffset + byteOffset, value, true);
722  }
723
724  setFloat32(byteOffset: number, value: number): void {
725    this.msg._getBuf().setFloat32(this.dataOffset + byteOffset, value, true);
726  }
727
728  setFloat64(byteOffset: number, value: number): void {
729    this.msg._getBuf().setFloat64(this.dataOffset + byteOffset, value, true);
730  }
731
732  setText(ptrIndex: number, value: string): void {
733    const encoded = encoder.encode(value);
734    const byteLen = encoded.length + 1;
735    const wordLen = Math.ceil(byteLen / WORD_SIZE);
736
737    this.msg._alignAlloc();
738    this.msg._ensureCapacity(wordLen * WORD_SIZE);
739
740    const targetOffset = this.msg._getAllocOffset();
741    const raw = this.msg._getRaw();
742    raw.set(encoded, targetOffset);
743    raw[targetOffset + encoded.length] = 0;
744
745    const ptrOffset = this.ptrOffset + ptrIndex * WORD_SIZE;
746    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
747    const lo = ((offsetWords << 2) | 1) >>> 0;
748    const hi = ((byteLen << 3) | 2) >>> 0;
749    this.msg._getBuf().setUint32(ptrOffset, lo, true);
750    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
751
752    this.msg._setAllocOffset(targetOffset + wordLen * WORD_SIZE);
753  }
754
755  setData(ptrIndex: number, value: Uint8Array): void {
756    if (value.length === 0) return;
757    const byteLen = value.length;
758    const wordLen = Math.ceil(byteLen / WORD_SIZE);
759
760    this.msg._alignAlloc();
761    this.msg._ensureCapacity(wordLen * WORD_SIZE);
762
763    const targetOffset = this.msg._getAllocOffset();
764    this.msg._getRaw().set(value, targetOffset);
765
766    const ptrOffset = this.ptrOffset + ptrIndex * WORD_SIZE;
767    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
768    const lo = ((offsetWords << 2) | 1) >>> 0;
769    const hi = ((byteLen << 3) | 2) >>> 0;
770    this.msg._getBuf().setUint32(ptrOffset, lo, true);
771    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
772
773    this.msg._setAllocOffset(targetOffset + wordLen * WORD_SIZE);
774  }
775
776  /**
777   * Set a Data list pointer (List(Data)) at the given pointer index within this struct.
778   * Encodes as a composite list of byte-blob pointers (no NUL terminator).
779   */
780  setDataList(ptrIndex: number, values: Uint8Array[]): void {
781    if (values.length === 0) return;
782
783    this.msg._alignAlloc();
784
785    const tagOffset = this.msg._getAllocOffset();
786    const elementCount = values.length;
787    const elementWords = 1;
788    const totalWords = 1 + elementCount * elementWords;
789
790    this.msg._ensureCapacity(totalWords * WORD_SIZE + values.reduce((a, v) => a + v.length + 8, 0));
791
792    const tagLo = ((elementCount << 2) | 0) >>> 0;
793    const tagHi = ((0 << 0) | (1 << 16)) >>> 0;
794    this.msg._getBuf().setUint32(tagOffset, tagLo, true);
795    this.msg._getBuf().setUint32(tagOffset + 4, tagHi, true);
796
797    this.msg._setAllocOffset(tagOffset + WORD_SIZE + elementCount * elementWords * WORD_SIZE);
798
799    const ptrOffset = this.ptrOffset + ptrIndex * WORD_SIZE;
800    const offsetWords = (tagOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
801    const lo = ((offsetWords << 2) | 1) >>> 0;
802    const hi = ((elementCount * elementWords << 3) | 7) >>> 0;
803    this.msg._getBuf().setUint32(ptrOffset, lo, true);
804    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
805
806    for (let i = 0; i < values.length; i++) {
807      const elementPtrOffset = tagOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
808      const byteLen = values[i].length;
809      const wordLen = Math.ceil(byteLen / WORD_SIZE);
810
811      this.msg._alignAlloc();
812      this.msg._ensureCapacity(wordLen * WORD_SIZE);
813
814      const dataOffset = this.msg._getAllocOffset();
815      this.msg._getRaw().set(values[i], dataOffset);
816
817      const dataOffsetWords = (dataOffset - elementPtrOffset - WORD_SIZE) / WORD_SIZE;
818      const dataLo = ((dataOffsetWords << 2) | 1) >>> 0;
819      const dataHi = ((byteLen << 3) | 2) >>> 0;
820      this.msg._getBuf().setUint32(elementPtrOffset, dataLo, true);
821      this.msg._getBuf().setUint32(elementPtrOffset + 4, dataHi, true);
822
823      this.msg._setAllocOffset(dataOffset + wordLen * WORD_SIZE);
824    }
825  }
826
827  /**
828   * Allocate a fixed-width primitive list (List(Bool)/List(UIntN)/List(IntN)/List(FloatN))
829   * pointer at `ptrIndex` within this struct. `elemSize` is the capnp list
830   * element-size tag (1=bit, 2=1-byte, 3=2-byte, 4=4-byte, 5=8-byte);
831   * `byteWidth` is the per-element byte width (ignored for bit-packed bool lists).
832   * Returns the byte offset of the first element, or -1 for an empty list.
833   */
834  private allocPrimitiveListAt(ptrOffset: number, count: number, elemSize: number, byteWidth: number): number {
835    if (count === 0) return -1;
836
837    this.msg._alignAlloc();
838    const totalBytes = elemSize === 1 ? Math.ceil(count / 8) : count * byteWidth;
839    const wordLen = Math.ceil(totalBytes / WORD_SIZE);
840    this.msg._ensureCapacity(wordLen * WORD_SIZE);
841
842    const targetOffset = this.msg._getAllocOffset();
843    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
844    const lo = ((offsetWords << 2) | 1) >>> 0;
845    const hi = ((count << 3) | elemSize) >>> 0;
846    this.msg._getBuf().setUint32(ptrOffset, lo, true);
847    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
848
849    this.msg._setAllocOffset(targetOffset + wordLen * WORD_SIZE);
850    return targetOffset;
851  }
852
853  private allocPrimitiveList(ptrIndex: number, count: number, elemSize: number, byteWidth: number): number {
854    return this.allocPrimitiveListAt(this.ptrOffset + ptrIndex * WORD_SIZE, count, elemSize, byteWidth);
855  }
856
857  /**
858   * Allocate the outer pointer-list structure for a `List(List(<inner>))`
859   * field at `ptrIndex` within this struct. See `CapnpArena.allocNestedListOuter`
860   * for the encoding this mirrors.
861   */
862  private allocNestedListOuter(ptrIndex: number, outerCount: number): number {
863    if (outerCount === 0) return -1;
864
865    this.msg._alignAlloc();
866    this.msg._ensureCapacity(outerCount * WORD_SIZE);
867
868    const outerBase = this.msg._getAllocOffset();
869    this.msg._setAllocOffset(outerBase + outerCount * WORD_SIZE);
870
871    const ptrOffset = this.ptrOffset + ptrIndex * WORD_SIZE;
872    const offsetWords = (outerBase - ptrOffset - WORD_SIZE) / WORD_SIZE;
873    const lo = ((offsetWords << 2) | 1) >>> 0;
874    const hi = ((outerCount << 3) | 6) >>> 0; // element_size=6: pointer list
875    this.msg._getBuf().setUint32(ptrOffset, lo, true);
876    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
877
878    return outerBase;
879  }
880
881  setBoolList(ptrIndex: number, values: boolean[]): void {
882    const base = this.allocPrimitiveList(ptrIndex, values.length, 1, 0);
883    if (base < 0) return;
884    const raw = this.msg._getRaw();
885    for (let i = 0; i < values.length; i++) {
886      if (values[i]) raw[base + (i >> 3)] |= 1 << (i & 7);
887    }
888  }
889
890  setUint8List(ptrIndex: number, values: number[]): void {
891    const base = this.allocPrimitiveList(ptrIndex, values.length, 2, 1);
892    if (base < 0) return;
893    for (let i = 0; i < values.length; i++) this.msg._getBuf().setUint8(base + i, values[i]);
894  }
895
896  setInt8List(ptrIndex: number, values: number[]): void {
897    const base = this.allocPrimitiveList(ptrIndex, values.length, 2, 1);
898    if (base < 0) return;
899    for (let i = 0; i < values.length; i++) this.msg._getBuf().setInt8(base + i, values[i]);
900  }
901
902  setUint16List(ptrIndex: number, values: number[]): void {
903    const base = this.allocPrimitiveList(ptrIndex, values.length, 3, 2);
904    if (base < 0) return;
905    for (let i = 0; i < values.length; i++) this.msg._getBuf().setUint16(base + i * 2, values[i], true);
906  }
907
908  setInt16List(ptrIndex: number, values: number[]): void {
909    const base = this.allocPrimitiveList(ptrIndex, values.length, 3, 2);
910    if (base < 0) return;
911    for (let i = 0; i < values.length; i++) this.msg._getBuf().setInt16(base + i * 2, values[i], true);
912  }
913
914  setUint32List(ptrIndex: number, values: number[]): void {
915    const base = this.allocPrimitiveList(ptrIndex, values.length, 4, 4);
916    if (base < 0) return;
917    for (let i = 0; i < values.length; i++) this.msg._getBuf().setUint32(base + i * 4, values[i], true);
918  }
919
920  setInt32List(ptrIndex: number, values: number[]): void {
921    const base = this.allocPrimitiveList(ptrIndex, values.length, 4, 4);
922    if (base < 0) return;
923    for (let i = 0; i < values.length; i++) this.msg._getBuf().setInt32(base + i * 4, values[i], true);
924  }
925
926  setFloat32List(ptrIndex: number, values: number[]): void {
927    const base = this.allocPrimitiveList(ptrIndex, values.length, 4, 4);
928    if (base < 0) return;
929    for (let i = 0; i < values.length; i++) this.msg._getBuf().setFloat32(base + i * 4, values[i], true);
930  }
931
932  setUint64List(ptrIndex: number, values: bigint[]): void {
933    const base = this.allocPrimitiveList(ptrIndex, values.length, 5, 8);
934    if (base < 0) return;
935    for (let i = 0; i < values.length; i++) this.msg._getBuf().setBigUint64(base + i * 8, values[i], true);
936  }
937
938  setInt64List(ptrIndex: number, values: bigint[]): void {
939    const base = this.allocPrimitiveList(ptrIndex, values.length, 5, 8);
940    if (base < 0) return;
941    for (let i = 0; i < values.length; i++) this.msg._getBuf().setBigInt64(base + i * 8, values[i], true);
942  }
943
944  setFloat64List(ptrIndex: number, values: number[]): void {
945    const base = this.allocPrimitiveList(ptrIndex, values.length, 5, 8);
946    if (base < 0) return;
947    for (let i = 0; i < values.length; i++) this.msg._getBuf().setFloat64(base + i * 8, values[i], true);
948  }
949
950  // --- Nested List(List(<primitive>)) setters (see CapnpArena's for docs) ---
951
952  setBoolListList(ptrIndex: number, values: boolean[][]): void {
953    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
954    if (outerBase < 0) return;
955    for (let i = 0; i < values.length; i++) {
956      const inner = values[i];
957      if (inner.length === 0) continue;
958      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 1, 0);
959      // Re-fetch after alloc: allocPrimitiveListAt can grow the arena,
960      // detaching any previously captured raw buffer.
961      const raw = this.msg._getRaw();
962      for (let j = 0; j < inner.length; j++) {
963        if (inner[j]) raw[base + (j >> 3)] |= 1 << (j & 7);
964      }
965    }
966  }
967
968  setUint8ListList(ptrIndex: number, values: number[][]): void {
969    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
970    if (outerBase < 0) return;
971    for (let i = 0; i < values.length; i++) {
972      const inner = values[i];
973      if (inner.length === 0) continue;
974      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 2, 1);
975      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setUint8(base + j, inner[j]);
976    }
977  }
978
979  setInt8ListList(ptrIndex: number, values: number[][]): void {
980    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
981    if (outerBase < 0) return;
982    for (let i = 0; i < values.length; i++) {
983      const inner = values[i];
984      if (inner.length === 0) continue;
985      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 2, 1);
986      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setInt8(base + j, inner[j]);
987    }
988  }
989
990  setUint16ListList(ptrIndex: number, values: number[][]): void {
991    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
992    if (outerBase < 0) return;
993    for (let i = 0; i < values.length; i++) {
994      const inner = values[i];
995      if (inner.length === 0) continue;
996      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 3, 2);
997      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setUint16(base + j * 2, inner[j], true);
998    }
999  }
1000
1001  setInt16ListList(ptrIndex: number, values: number[][]): void {
1002    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
1003    if (outerBase < 0) return;
1004    for (let i = 0; i < values.length; i++) {
1005      const inner = values[i];
1006      if (inner.length === 0) continue;
1007      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 3, 2);
1008      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setInt16(base + j * 2, inner[j], true);
1009    }
1010  }
1011
1012  setUint32ListList(ptrIndex: number, values: number[][]): void {
1013    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
1014    if (outerBase < 0) return;
1015    for (let i = 0; i < values.length; i++) {
1016      const inner = values[i];
1017      if (inner.length === 0) continue;
1018      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 4, 4);
1019      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setUint32(base + j * 4, inner[j], true);
1020    }
1021  }
1022
1023  setInt32ListList(ptrIndex: number, values: number[][]): void {
1024    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
1025    if (outerBase < 0) return;
1026    for (let i = 0; i < values.length; i++) {
1027      const inner = values[i];
1028      if (inner.length === 0) continue;
1029      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 4, 4);
1030      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setInt32(base + j * 4, inner[j], true);
1031    }
1032  }
1033
1034  setFloat32ListList(ptrIndex: number, values: number[][]): void {
1035    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
1036    if (outerBase < 0) return;
1037    for (let i = 0; i < values.length; i++) {
1038      const inner = values[i];
1039      if (inner.length === 0) continue;
1040      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 4, 4);
1041      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setFloat32(base + j * 4, inner[j], true);
1042    }
1043  }
1044
1045  setUint64ListList(ptrIndex: number, values: bigint[][]): void {
1046    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
1047    if (outerBase < 0) return;
1048    for (let i = 0; i < values.length; i++) {
1049      const inner = values[i];
1050      if (inner.length === 0) continue;
1051      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 5, 8);
1052      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setBigUint64(base + j * 8, inner[j], true);
1053    }
1054  }
1055
1056  setInt64ListList(ptrIndex: number, values: bigint[][]): void {
1057    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
1058    if (outerBase < 0) return;
1059    for (let i = 0; i < values.length; i++) {
1060      const inner = values[i];
1061      if (inner.length === 0) continue;
1062      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 5, 8);
1063      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setBigInt64(base + j * 8, inner[j], true);
1064    }
1065  }
1066
1067  setFloat64ListList(ptrIndex: number, values: number[][]): void {
1068    const outerBase = this.allocNestedListOuter(ptrIndex, values.length);
1069    if (outerBase < 0) return;
1070    for (let i = 0; i < values.length; i++) {
1071      const inner = values[i];
1072      if (inner.length === 0) continue;
1073      const base = this.allocPrimitiveListAt(outerBase + i * WORD_SIZE, inner.length, 5, 8);
1074      for (let j = 0; j < inner.length; j++) this.msg._getBuf().setFloat64(base + j * 8, inner[j], true);
1075    }
1076  }
1077
1078  /**
1079   * Initialize a sub-struct at the given pointer index within this struct.
1080   * Returns a StructBuilder for writing fields into the nested struct.
1081   */
1082  initStruct(ptrIndex: number, dataWords: number, ptrWords: number): StructBuilder {
1083    const structSize = (dataWords + ptrWords) * WORD_SIZE;
1084    this.msg._alignAlloc();
1085    this.msg._ensureCapacity(structSize);
1086
1087    const targetOffset = this.msg._getAllocOffset();
1088
1089    const ptrOffset = this.ptrOffset + ptrIndex * WORD_SIZE;
1090    const offsetWords = (targetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
1091    const lo = ((offsetWords << 2) | 0) >>> 0;
1092    const hi = ((dataWords & 0xFFFF) | ((ptrWords & 0xFFFF) << 16)) >>> 0;
1093    this.msg._getBuf().setUint32(ptrOffset, lo, true);
1094    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
1095
1096    this.msg._setAllocOffset(targetOffset + structSize);
1097
1098    return new StructBuilder(this.msg, targetOffset, dataWords, ptrWords);
1099  }
1100
1101  /**
1102   * Set a Text list pointer at the given pointer index within this struct.
1103   */
1104  setTextList(ptrIndex: number, values: string[]): void {
1105    if (values.length === 0) return;
1106
1107    this.msg._alignAlloc();
1108    const tagOffset = this.msg._getAllocOffset();
1109    const elementCount = values.length;
1110    const elementWords = 1;
1111    const totalWords = 1 + elementCount * elementWords;
1112
1113    this.msg._ensureCapacity(totalWords * WORD_SIZE + values.reduce((a, v) => a + v.length + 9, 0));
1114
1115    const tagLo = ((elementCount << 2) | 0) >>> 0;
1116    const tagHi = ((0 << 0) | (1 << 16)) >>> 0;
1117    this.msg._getBuf().setUint32(tagOffset, tagLo, true);
1118    this.msg._getBuf().setUint32(tagOffset + 4, tagHi, true);
1119
1120    this.msg._setAllocOffset(tagOffset + WORD_SIZE + elementCount * elementWords * WORD_SIZE);
1121
1122    const ptrOffset = this.ptrOffset + ptrIndex * WORD_SIZE;
1123    const listTargetOffset = tagOffset;
1124    const offsetWords = (listTargetOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
1125    const lo = ((offsetWords << 2) | 1) >>> 0;
1126    const hi = ((elementCount * elementWords << 3) | 7) >>> 0;
1127    this.msg._getBuf().setUint32(ptrOffset, lo, true);
1128    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
1129
1130    for (let i = 0; i < values.length; i++) {
1131      const elementPtrOffset = tagOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
1132      const encoded = encoder.encode(values[i]);
1133      const byteLen = encoded.length + 1;
1134      const wordLen = Math.ceil(byteLen / WORD_SIZE);
1135
1136      this.msg._alignAlloc();
1137      this.msg._ensureCapacity(wordLen * WORD_SIZE);
1138
1139      const textOffset = this.msg._getAllocOffset();
1140      this.msg._getRaw().set(encoded, textOffset);
1141      this.msg._getRaw()[textOffset + encoded.length] = 0;
1142
1143      const textOffsetWords = (textOffset - elementPtrOffset - WORD_SIZE) / WORD_SIZE;
1144      const textLo = ((textOffsetWords << 2) | 1) >>> 0;
1145      const textHi = ((byteLen << 3) | 2) >>> 0;
1146      this.msg._getBuf().setUint32(elementPtrOffset, textLo, true);
1147      this.msg._getBuf().setUint32(elementPtrOffset + 4, textHi, true);
1148
1149      this.msg._setAllocOffset(textOffset + wordLen * WORD_SIZE);
1150    }
1151  }
1152
1153  initStructList(ptrIndex: number, count: number, dataWords: number, ptrWords: number): StructBuilder[] {
1154    if (count === 0) return [];
1155
1156    this.msg._alignAlloc();
1157    const tagOffset = this.msg._getAllocOffset();
1158    const elementWords = dataWords + ptrWords;
1159    const totalBodyWords = count * elementWords;
1160
1161    this.msg._ensureCapacity((1 + totalBodyWords) * WORD_SIZE);
1162
1163    const tagLo = ((count << 2) | 0) >>> 0;
1164    const tagHi = ((dataWords & 0xFFFF) | ((ptrWords & 0xFFFF) << 16)) >>> 0;
1165    this.msg._getBuf().setUint32(tagOffset, tagLo, true);
1166    this.msg._getBuf().setUint32(tagOffset + 4, tagHi, true);
1167
1168    this.msg._setAllocOffset(tagOffset + WORD_SIZE + totalBodyWords * WORD_SIZE);
1169
1170    const ptrOffset = this.ptrOffset + ptrIndex * WORD_SIZE;
1171    const offsetWords = (tagOffset - ptrOffset - WORD_SIZE) / WORD_SIZE;
1172    const lo = ((offsetWords << 2) | 1) >>> 0;
1173    const hi = ((totalBodyWords << 3) | 7) >>> 0;
1174    this.msg._getBuf().setUint32(ptrOffset, lo, true);
1175    this.msg._getBuf().setUint32(ptrOffset + 4, hi, true);
1176
1177    const builders: StructBuilder[] = [];
1178    for (let i = 0; i < count; i++) {
1179      const elemStart = tagOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
1180      builders.push(new StructBuilder(this.msg, elemStart, dataWords, ptrWords));
1181    }
1182    return builders;
1183  }
1184}
1185
1186/**
1187 * Cap'n Proto message reader with multi-segment + far pointer support.
1188 *
1189 * Reads struct fields at known offsets from a wire-format message.
1190 * Handles both single-segment and multi-segment messages, resolving
1191 * far pointers (type=2) that reference data in other segments.
1192 */
1193export class CapnpReader {
1194  private buf: DataView;
1195  private raw: Uint8Array;
1196  /** Byte offset where the root struct's data section starts */
1197  private readonly rootDataOffset: number;
1198  /** Byte offset where the root struct's pointer section starts */
1199  private readonly rootPtrOffset: number;
1200  private readonly dataWords: number;
1201  /** Byte offsets of each segment start (for far pointer resolution) */
1202  private readonly segmentOffsets: number[];
1203
1204  /**
1205   * Parse a Cap'n Proto message from wire-format bytes.
1206   * @param bytes Wire-format message bytes
1207   * @param dataWords Expected data word count of the root struct
1208   * @param ptrWords Expected pointer count of the root struct
1209   */
1210  constructor(bytes: Uint8Array, dataWords: number, _ptrWords: number) {
1211    this.raw = bytes;
1212    this.buf = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1213    this.dataWords = dataWords;
1214    this.segmentOffsets = [];
1215
1216    // Standard Cap'n Proto format:
1217    //   Segment table: [num_segments-1 (u32), seg0_size (u32), seg1_size (u32), ...]
1218    //   Padded to 8-byte boundary, then segment data follows.
1219    //   Root struct pointer is the first word of segment 0.
1220    let rootDataOffset = 8; // fallback for malformed messages
1221    if (bytes.length >= 8) {
1222      const numSegments = (this.buf.getUint32(0, true)) + 1;
1223      const tableBytes = 4 + numSegments * 4;
1224      const segDataStart = Math.ceil(tableBytes / 8) * 8;
1225
1226      // Build segment offset table for far pointer resolution
1227      let offset = segDataStart;
1228      for (let i = 0; i < numSegments; i++) {
1229        this.segmentOffsets.push(offset);
1230        const segWords = this.buf.getUint32(4 + i * 4, true);
1231        offset += segWords * WORD_SIZE;
1232      }
1233
1234      // Root struct pointer is at segDataStart (first word of segment 0)
1235      if (bytes.length >= segDataStart + 8) {
1236        const rootPtrLo = this.buf.getUint32(segDataStart, true);
1237        if ((rootPtrLo & 3) === 0) {
1238          const offsetWords = rootPtrLo >> 2;
1239          rootDataOffset = segDataStart + 8 + offsetWords * 8;
1240        } else {
1241          rootDataOffset = segDataStart;
1242        }
1243      }
1244    }
1245    this.rootDataOffset = rootDataOffset;
1246    this.rootPtrOffset = rootDataOffset + dataWords * WORD_SIZE;
1247  }
1248
1249  /**
1250   * Resolve a pointer, following far pointers (type=2) to the target segment.
1251   * Returns the resolved pointer words and their byte offset, or null if null pointer.
1252   */
1253  private resolvePtr(ptrOffset: number): { lo: number; hi: number; offset: number } | null {
1254    let lo = this.buf.getUint32(ptrOffset, true);
1255    let hi = this.buf.getUint32(ptrOffset + 4, true);
1256    if (lo === 0 && hi === 0) return null;
1257    if ((lo & 3) === 2) {
1258      // Far pointer: follow to target segment
1259      const farOffsetWords = lo >>> 3;
1260      const segmentId = hi;
1261      if (segmentId >= this.segmentOffsets.length) return null;
1262      const targetOffset = this.segmentOffsets[segmentId] + farOffsetWords * WORD_SIZE;
1263      if ((lo >> 2) & 1) return null; // double-far not supported
1264      lo = this.buf.getUint32(targetOffset, true);
1265      hi = this.buf.getUint32(targetOffset + 4, true);
1266      return { lo, hi, offset: targetOffset };
1267    }
1268    return { lo, hi, offset: ptrOffset };
1269  }
1270
1271  // --- Data section getters ---
1272
1273  getBool(byteOffset: number, bitIndex: number): boolean {
1274    return (this.raw[this.rootDataOffset + byteOffset] & (1 << bitIndex)) !== 0;
1275  }
1276
1277  getUint8(byteOffset: number): number {
1278    return this.buf.getUint8(this.rootDataOffset + byteOffset);
1279  }
1280
1281  getUint16(byteOffset: number): number {
1282    return this.buf.getUint16(this.rootDataOffset + byteOffset, true);
1283  }
1284
1285  getUint32(byteOffset: number): number {
1286    return this.buf.getUint32(this.rootDataOffset + byteOffset, true);
1287  }
1288
1289  getUint64(byteOffset: number): bigint {
1290    return this.buf.getBigUint64(this.rootDataOffset + byteOffset, true);
1291  }
1292
1293  getInt8(byteOffset: number): number {
1294    return this.buf.getInt8(this.rootDataOffset + byteOffset);
1295  }
1296
1297  getInt16(byteOffset: number): number {
1298    return this.buf.getInt16(this.rootDataOffset + byteOffset, true);
1299  }
1300
1301  getInt32(byteOffset: number): number {
1302    return this.buf.getInt32(this.rootDataOffset + byteOffset, true);
1303  }
1304
1305  getInt64(byteOffset: number): bigint {
1306    return this.buf.getBigInt64(this.rootDataOffset + byteOffset, true);
1307  }
1308
1309  getFloat32(byteOffset: number): number {
1310    return this.buf.getFloat32(this.rootDataOffset + byteOffset, true);
1311  }
1312
1313  getFloat64(byteOffset: number): number {
1314    return this.buf.getFloat64(this.rootDataOffset + byteOffset, true);
1315  }
1316
1317  // --- Pointer section getters ---
1318
1319  /** Read a Text pointer at the given pointer index. Returns empty string if null. */
1320  getText(ptrIndex: number): string {
1321    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1322    const resolved = this.resolvePtr(ptrOffset);
1323    if (!resolved) return '';
1324    const { lo, hi, offset } = resolved;
1325    if ((lo & 3) !== 1) return '';
1326    const offsetWords = (lo >> 2) | 0;
1327    const byteCount = hi >>> 3;
1328    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1329    const strLen = byteCount > 0 ? byteCount - 1 : 0;
1330    return decoder.decode(this.raw.subarray(targetOffset, targetOffset + strLen));
1331  }
1332
1333  /** Read a Data pointer at the given pointer index. Returns empty array if null. */
1334  getData(ptrIndex: number): Uint8Array {
1335    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1336    const resolved = this.resolvePtr(ptrOffset);
1337    if (!resolved) return new Uint8Array(0);
1338    const { lo, hi, offset } = resolved;
1339    if ((lo & 3) !== 1) return new Uint8Array(0);
1340    const offsetWords = (lo >> 2) | 0;
1341    const byteCount = hi >>> 3;
1342    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1343    return this.raw.subarray(targetOffset, targetOffset + byteCount);
1344  }
1345
1346  /** Read a struct pointer and return a StructReader. Returns null if pointer is null. */
1347  getStruct(ptrIndex: number, dataWords: number, ptrWords: number): StructReader | null {
1348    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1349    const resolved = this.resolvePtr(ptrOffset);
1350    if (!resolved) return null;
1351    const { lo, offset } = resolved;
1352    if ((lo & 3) !== 0) return null;
1353    const offsetWords = (lo >> 2) | 0;
1354    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1355    return new StructReader(this.raw, this.buf, targetOffset, dataWords, ptrWords, this.segmentOffsets);
1356  }
1357
1358  /** Read a List(Text) pointer (supports both pointer-list and composite encodings). */
1359  getTextList(ptrIndex: number): string[] {
1360    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1361    const resolved = this.resolvePtr(ptrOffset);
1362    if (!resolved) return [];
1363    const { lo, hi, offset } = resolved;
1364    if ((lo & 3) !== 1) return [];
1365
1366    const offsetWords = (lo >> 2) | 0;
1367    const elementSize = hi & 7;
1368    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1369
1370    if (elementSize === 6) {
1371      // Pointer list: each element is a pointer to a text blob
1372      const elementCount = hi >>> 3;
1373      const results: string[] = [];
1374      for (let i = 0; i < elementCount; i++) {
1375        const elemOffset = targetOffset + i * WORD_SIZE;
1376        const elemLo = this.buf.getUint32(elemOffset, true);
1377        const elemHi = this.buf.getUint32(elemOffset + 4, true);
1378        if (elemLo === 0 && elemHi === 0) { results.push(''); continue; }
1379        const elemOffsetWords = (elemLo >> 2) | 0;
1380        const byteCount = elemHi >>> 3;
1381        const textOffset = elemOffset + WORD_SIZE + elemOffsetWords * WORD_SIZE;
1382        const strLen = byteCount > 0 ? byteCount - 1 : 0;
1383        results.push(decoder.decode(this.raw.subarray(textOffset, textOffset + strLen)));
1384      }
1385      return results;
1386    }
1387
1388    if (elementSize !== 7) return []; // unsupported element size
1389
1390    // Composite list: tag word + elements
1391    const tagLo = this.buf.getUint32(targetOffset, true);
1392    const elementCount = (tagLo >> 2) | 0;
1393
1394    const results: string[] = [];
1395    for (let i = 0; i < elementCount; i++) {
1396      const elemOffset = targetOffset + WORD_SIZE + i * WORD_SIZE;
1397      const elemLo = this.buf.getUint32(elemOffset, true);
1398      const elemHi = this.buf.getUint32(elemOffset + 4, true);
1399
1400      if (elemLo === 0 && elemHi === 0) {
1401        results.push('');
1402        continue;
1403      }
1404
1405      const elemOffsetWords = (elemLo >> 2) | 0;
1406      const byteCount = elemHi >>> 3;
1407      const textOffset = elemOffset + WORD_SIZE + elemOffsetWords * WORD_SIZE;
1408      const strLen = byteCount > 0 ? byteCount - 1 : 0;
1409      results.push(decoder.decode(this.raw.subarray(textOffset, textOffset + strLen)));
1410    }
1411
1412    return results;
1413  }
1414
1415  /** Read a List(Data) pointer (supports both pointer-list and composite encodings). */
1416  getDataList(ptrIndex: number): Uint8Array[] {
1417    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1418    const resolved = this.resolvePtr(ptrOffset);
1419    if (!resolved) return [];
1420    const { lo, hi, offset } = resolved;
1421    if ((lo & 3) !== 1) return [];
1422
1423    const offsetWords = (lo >> 2) | 0;
1424    const elementSize = hi & 7;
1425    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1426
1427    // Pointer list (element_size=6, the canonical List(Data) encoding — what
1428    // the Rust side emits) or composite list (element_size=7): both carry one
1429    // pointer per element, differing only in where the count lives and where
1430    // the elements start.
1431    let elementCount: number;
1432    let elemBase: number;
1433    if (elementSize === 6) {
1434      elementCount = hi >>> 3;
1435      elemBase = targetOffset;
1436    } else if (elementSize === 7) {
1437      const tagLo = this.buf.getUint32(targetOffset, true);
1438      elementCount = (tagLo >> 2) | 0;
1439      elemBase = targetOffset + WORD_SIZE;
1440    } else {
1441      return [];
1442    }
1443
1444    const results: Uint8Array[] = [];
1445    for (let i = 0; i < elementCount; i++) {
1446      const elemOffset = elemBase + i * WORD_SIZE;
1447      const elemLo = this.buf.getUint32(elemOffset, true);
1448      const elemHi = this.buf.getUint32(elemOffset + 4, true);
1449
1450      if (elemLo === 0 && elemHi === 0) {
1451        results.push(new Uint8Array(0));
1452        continue;
1453      }
1454
1455      const elemOffsetWords = (elemLo >> 2) | 0;
1456      const byteCount = elemHi >>> 3;
1457      const dataOffset = elemOffset + WORD_SIZE + elemOffsetWords * WORD_SIZE;
1458      results.push(this.raw.subarray(dataOffset, dataOffset + byteCount));
1459    }
1460
1461    return results;
1462  }
1463
1464  /**
1465   * Read a fixed-width primitive list (List(Bool)/List(UIntN)/List(IntN)/List(FloatN)).
1466   * `expectedElemSize` is the capnp list element-size tag this getter accepts
1467   * (1=bit, 2=1-byte, 3=2-byte, 4=4-byte, 5=8-byte); `byteWidth` is the
1468   * per-element byte width (ignored for bit-packed bool lists). Returns `[]`
1469   * if the pointer is null or was encoded with a different element size.
1470   */
1471  private readPrimitiveList<T>(
1472    ptrIndex: number,
1473    expectedElemSize: number,
1474    byteWidth: number,
1475    read: (byteOffset: number, bitIndex: number) => T,
1476  ): T[] {
1477    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1478    const resolved = this.resolvePtr(ptrOffset);
1479    if (!resolved) return [];
1480    const { lo, hi, offset } = resolved;
1481    if ((lo & 3) !== 1) return [];
1482
1483    const offsetWords = (lo >> 2) | 0;
1484    const elementSize = hi & 7;
1485    if (elementSize !== expectedElemSize) return [];
1486    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1487    const count = hi >>> 3;
1488
1489    const results: T[] = [];
1490    for (let i = 0; i < count; i++) {
1491      results.push(
1492        byteWidth === 0
1493          ? read(targetOffset + (i >> 3), i & 7)
1494          : read(targetOffset + i * byteWidth, 0),
1495      );
1496    }
1497    return results;
1498  }
1499
1500  getBoolList(ptrIndex: number): boolean[] {
1501    return this.readPrimitiveList(ptrIndex, 1, 0, (byteOffset, bitIndex) => (this.raw[byteOffset] & (1 << bitIndex)) !== 0);
1502  }
1503
1504  getUint8List(ptrIndex: number): number[] {
1505    return this.readPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getUint8(o));
1506  }
1507
1508  getInt8List(ptrIndex: number): number[] {
1509    return this.readPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getInt8(o));
1510  }
1511
1512  getUint16List(ptrIndex: number): number[] {
1513    return this.readPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getUint16(o, true));
1514  }
1515
1516  getInt16List(ptrIndex: number): number[] {
1517    return this.readPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getInt16(o, true));
1518  }
1519
1520  getUint32List(ptrIndex: number): number[] {
1521    return this.readPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getUint32(o, true));
1522  }
1523
1524  getInt32List(ptrIndex: number): number[] {
1525    return this.readPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getInt32(o, true));
1526  }
1527
1528  getFloat32List(ptrIndex: number): number[] {
1529    return this.readPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getFloat32(o, true));
1530  }
1531
1532  getUint64List(ptrIndex: number): bigint[] {
1533    return this.readPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigUint64(o, true));
1534  }
1535
1536  getInt64List(ptrIndex: number): bigint[] {
1537    return this.readPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigInt64(o, true));
1538  }
1539
1540  getFloat64List(ptrIndex: number): number[] {
1541    return this.readPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getFloat64(o, true));
1542  }
1543
1544  /**
1545   * Read a `List(List(<inner>))` field: a pointer list whose elements are
1546   * themselves primitive lists (List(Bool)/List(UIntN)/List(IntN)/List(FloatN)).
1547   * Mirrors `readPrimitiveList`, resolving one extra level of pointer
1548   * indirection per outer element. Supports both the canonical pointer-list
1549   * outer encoding (element_size=6) and composite (element_size=7, used when
1550   * the outer list happens to be optimized as a single-pointer struct list).
1551   * An outer element whose inner pointer is null, or whose encoded element
1552   * size doesn't match `innerElemSize`, reads as `[]` for that element.
1553   */
1554  private readNestedPrimitiveList<T>(
1555    ptrIndex: number,
1556    innerElemSize: number,
1557    innerByteWidth: number,
1558    readInner: (byteOffset: number, bitIndex: number) => T,
1559  ): T[][] {
1560    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1561    const resolved = this.resolvePtr(ptrOffset);
1562    if (!resolved) return [];
1563    const { lo, hi, offset } = resolved;
1564    if ((lo & 3) !== 1) return [];
1565
1566    const offsetWords = (lo >> 2) | 0;
1567    const outerElementSize = hi & 7;
1568    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1569
1570    let outerCount: number;
1571    let outerBase: number;
1572    if (outerElementSize === 6) {
1573      outerCount = hi >>> 3;
1574      outerBase = targetOffset;
1575    } else if (outerElementSize === 7) {
1576      const tagLo = this.buf.getUint32(targetOffset, true);
1577      outerCount = (tagLo >> 2) | 0;
1578      outerBase = targetOffset + WORD_SIZE;
1579    } else {
1580      return [];
1581    }
1582
1583    const results: T[][] = [];
1584    for (let i = 0; i < outerCount; i++) {
1585      const elemPtrOffset = outerBase + i * WORD_SIZE;
1586      const elemResolved = this.resolvePtr(elemPtrOffset);
1587      if (!elemResolved) { results.push([]); continue; }
1588      const { lo: iLo, hi: iHi, offset: iOffset } = elemResolved;
1589      if ((iLo & 3) !== 1 || (iHi & 7) !== innerElemSize) { results.push([]); continue; }
1590      const iOffsetWords = (iLo >> 2) | 0;
1591      const innerTarget = iOffset + WORD_SIZE + iOffsetWords * WORD_SIZE;
1592      const innerCount = iHi >>> 3;
1593      const inner: T[] = [];
1594      for (let j = 0; j < innerCount; j++) {
1595        inner.push(
1596          innerByteWidth === 0
1597            ? readInner(innerTarget + (j >> 3), j & 7)
1598            : readInner(innerTarget + j * innerByteWidth, 0),
1599        );
1600      }
1601      results.push(inner);
1602    }
1603    return results;
1604  }
1605
1606  getBoolListList(ptrIndex: number): boolean[][] {
1607    return this.readNestedPrimitiveList(ptrIndex, 1, 0, (byteOffset, bitIndex) => (this.raw[byteOffset] & (1 << bitIndex)) !== 0);
1608  }
1609
1610  getUint8ListList(ptrIndex: number): number[][] {
1611    return this.readNestedPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getUint8(o));
1612  }
1613
1614  getInt8ListList(ptrIndex: number): number[][] {
1615    return this.readNestedPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getInt8(o));
1616  }
1617
1618  getUint16ListList(ptrIndex: number): number[][] {
1619    return this.readNestedPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getUint16(o, true));
1620  }
1621
1622  getInt16ListList(ptrIndex: number): number[][] {
1623    return this.readNestedPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getInt16(o, true));
1624  }
1625
1626  getUint32ListList(ptrIndex: number): number[][] {
1627    return this.readNestedPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getUint32(o, true));
1628  }
1629
1630  getInt32ListList(ptrIndex: number): number[][] {
1631    return this.readNestedPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getInt32(o, true));
1632  }
1633
1634  getFloat32ListList(ptrIndex: number): number[][] {
1635    return this.readNestedPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getFloat32(o, true));
1636  }
1637
1638  getUint64ListList(ptrIndex: number): bigint[][] {
1639    return this.readNestedPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigUint64(o, true));
1640  }
1641
1642  getInt64ListList(ptrIndex: number): bigint[][] {
1643    return this.readNestedPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigInt64(o, true));
1644  }
1645
1646  getFloat64ListList(ptrIndex: number): number[][] {
1647    return this.readNestedPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getFloat64(o, true));
1648  }
1649
1650  /** Read a List(Struct) composite list pointer. Returns StructReader[] using wire-format shape from tag word. */
1651  getStructList(ptrIndex: number, _dataWords: number, _ptrWords: number): StructReader[] {
1652    const ptrOffset = this.rootPtrOffset + ptrIndex * WORD_SIZE;
1653    const resolved = this.resolvePtr(ptrOffset);
1654    if (!resolved) return [];
1655    const { lo, hi, offset } = resolved;
1656    if ((lo & 3) !== 1) return [];
1657
1658    const offsetWords = (lo >> 2) | 0;
1659    const elementSize = hi & 7;
1660    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1661
1662    if (elementSize !== 7) return []; // not composite
1663
1664    // Read tag word — struct pointer encoding element shape
1665    const tagLo = this.buf.getUint32(targetOffset, true);
1666    const tagHi = this.buf.getUint32(targetOffset + 4, true);
1667    const elementCount = (tagLo >> 2) | 0;
1668    const tagDataWords = tagHi & 0xFFFF;
1669    const tagPtrWords = (tagHi >> 16) & 0xFFFF;
1670    const elementWords = tagDataWords + tagPtrWords;
1671
1672    const results: StructReader[] = [];
1673    for (let i = 0; i < elementCount; i++) {
1674      const elemDataOffset = targetOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
1675      results.push(new StructReader(this.raw, this.buf, elemDataOffset, tagDataWords, tagPtrWords, this.segmentOffsets));
1676    }
1677    return results;
1678  }
1679}
1680
1681/**
1682 * Reader for a sub-struct within a message.
1683 * Supports far pointer resolution for multi-segment messages.
1684 */
1685export class StructReader {
1686  constructor(
1687    private raw: Uint8Array,
1688    private buf: DataView,
1689    private dataOffset: number,
1690    private dataWords: number,
1691    private ptrWords: number,
1692    private segmentOffsets: readonly number[] = [],
1693  ) {}
1694
1695  /** Resolve a pointer, following far pointers (type=2) to the target segment. */
1696  private resolvePtr(ptrOffset: number): { lo: number; hi: number; offset: number } | null {
1697    let lo = this.buf.getUint32(ptrOffset, true);
1698    let hi = this.buf.getUint32(ptrOffset + 4, true);
1699    if (lo === 0 && hi === 0) return null;
1700    if ((lo & 3) === 2) {
1701      const farOffsetWords = lo >>> 3;
1702      const segmentId = hi;
1703      if (segmentId >= this.segmentOffsets.length) return null;
1704      const targetOffset = this.segmentOffsets[segmentId] + farOffsetWords * WORD_SIZE;
1705      if ((lo >> 2) & 1) return null; // double-far not supported
1706      lo = this.buf.getUint32(targetOffset, true);
1707      hi = this.buf.getUint32(targetOffset + 4, true);
1708      return { lo, hi, offset: targetOffset };
1709    }
1710    return { lo, hi, offset: ptrOffset };
1711  }
1712
1713  getBool(byteOffset: number, bitIndex: number): boolean {
1714    return (this.raw[this.dataOffset + byteOffset] & (1 << bitIndex)) !== 0;
1715  }
1716
1717  getUint8(byteOffset: number): number {
1718    return this.buf.getUint8(this.dataOffset + byteOffset);
1719  }
1720
1721  getUint16(byteOffset: number): number {
1722    return this.buf.getUint16(this.dataOffset + byteOffset, true);
1723  }
1724
1725  getUint32(byteOffset: number): number {
1726    return this.buf.getUint32(this.dataOffset + byteOffset, true);
1727  }
1728
1729  getUint64(byteOffset: number): bigint {
1730    return this.buf.getBigUint64(this.dataOffset + byteOffset, true);
1731  }
1732
1733  getInt8(byteOffset: number): number {
1734    return this.buf.getInt8(this.dataOffset + byteOffset);
1735  }
1736
1737  getInt16(byteOffset: number): number {
1738    return this.buf.getInt16(this.dataOffset + byteOffset, true);
1739  }
1740
1741  getInt32(byteOffset: number): number {
1742    return this.buf.getInt32(this.dataOffset + byteOffset, true);
1743  }
1744
1745  getInt64(byteOffset: number): bigint {
1746    return this.buf.getBigInt64(this.dataOffset + byteOffset, true);
1747  }
1748
1749  getFloat32(byteOffset: number): number {
1750    return this.buf.getFloat32(this.dataOffset + byteOffset, true);
1751  }
1752
1753  getFloat64(byteOffset: number): number {
1754    return this.buf.getFloat64(this.dataOffset + byteOffset, true);
1755  }
1756
1757  getText(ptrIndex: number): string {
1758    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
1759    const resolved = this.resolvePtr(ptrOffset);
1760    if (!resolved) return '';
1761    const { lo, hi, offset } = resolved;
1762    if ((lo & 3) !== 1) return '';
1763    const offsetWords = (lo >> 2) | 0;
1764    const byteCount = hi >>> 3;
1765    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1766    const strLen = byteCount > 0 ? byteCount - 1 : 0;
1767    return decoder.decode(this.raw.subarray(targetOffset, targetOffset + strLen));
1768  }
1769
1770  getData(ptrIndex: number): Uint8Array {
1771    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
1772    const resolved = this.resolvePtr(ptrOffset);
1773    if (!resolved) return new Uint8Array(0);
1774    const { lo, hi, offset } = resolved;
1775    if ((lo & 3) !== 1) return new Uint8Array(0);
1776    const offsetWords = (lo >> 2) | 0;
1777    const byteCount = hi >>> 3;
1778    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1779    return this.raw.subarray(targetOffset, targetOffset + byteCount);
1780  }
1781
1782  getStruct(ptrIndex: number, dataWords: number, ptrWords: number): StructReader | null {
1783    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
1784    const resolved = this.resolvePtr(ptrOffset);
1785    if (!resolved) return null;
1786    const { lo, offset } = resolved;
1787    if ((lo & 3) !== 0) return null;
1788    const offsetWords = (lo >> 2) | 0;
1789    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1790    return new StructReader(this.raw, this.buf, targetOffset, dataWords, ptrWords, this.segmentOffsets);
1791  }
1792
1793  getTextList(ptrIndex: number): string[] {
1794    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
1795    const resolved = this.resolvePtr(ptrOffset);
1796    if (!resolved) return [];
1797    const { lo, hi, offset } = resolved;
1798    if ((lo & 3) !== 1) return [];
1799
1800    const offsetWords = (lo >> 2) | 0;
1801    const elementSize = hi & 7;
1802    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1803
1804    if (elementSize === 6) {
1805      const elementCount = hi >>> 3;
1806      const results: string[] = [];
1807      for (let i = 0; i < elementCount; i++) {
1808        const elemOffset = targetOffset + i * WORD_SIZE;
1809        const elemLo = this.buf.getUint32(elemOffset, true);
1810        const elemHi = this.buf.getUint32(elemOffset + 4, true);
1811        if (elemLo === 0 && elemHi === 0) { results.push(''); continue; }
1812        const elemOffsetWords = (elemLo >> 2) | 0;
1813        const byteCount = elemHi >>> 3;
1814        const textOffset = elemOffset + WORD_SIZE + elemOffsetWords * WORD_SIZE;
1815        const strLen = byteCount > 0 ? byteCount - 1 : 0;
1816        results.push(decoder.decode(this.raw.subarray(textOffset, textOffset + strLen)));
1817      }
1818      return results;
1819    }
1820
1821    if (elementSize !== 7) return [];
1822
1823    const tagLo = this.buf.getUint32(targetOffset, true);
1824    const elementCount = (tagLo >> 2) | 0;
1825
1826    const results: string[] = [];
1827    for (let i = 0; i < elementCount; i++) {
1828      const elemOffset = targetOffset + WORD_SIZE + i * WORD_SIZE;
1829      const elemLo = this.buf.getUint32(elemOffset, true);
1830      const elemHi = this.buf.getUint32(elemOffset + 4, true);
1831      if (elemLo === 0 && elemHi === 0) { results.push(''); continue; }
1832      const elemOffsetWords = (elemLo >> 2) | 0;
1833      const byteCount = elemHi >>> 3;
1834      const textOffset = elemOffset + WORD_SIZE + elemOffsetWords * WORD_SIZE;
1835      const strLen = byteCount > 0 ? byteCount - 1 : 0;
1836      results.push(decoder.decode(this.raw.subarray(textOffset, textOffset + strLen)));
1837    }
1838    return results;
1839  }
1840
1841  getDataList(ptrIndex: number): Uint8Array[] {
1842    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
1843    const resolved = this.resolvePtr(ptrOffset);
1844    if (!resolved) return [];
1845    const { lo, hi, offset } = resolved;
1846    if ((lo & 3) !== 1) return [];
1847
1848    const offsetWords = (lo >> 2) | 0;
1849    const elementSize = hi & 7;
1850    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1851
1852    if (elementSize !== 7) return [];
1853    const tagLo = this.buf.getUint32(targetOffset, true);
1854    const elementCount = (tagLo >> 2) | 0;
1855
1856    const results: Uint8Array[] = [];
1857    for (let i = 0; i < elementCount; i++) {
1858      const elemOffset = targetOffset + WORD_SIZE + i * WORD_SIZE;
1859      const elemLo = this.buf.getUint32(elemOffset, true);
1860      const elemHi = this.buf.getUint32(elemOffset + 4, true);
1861      if (elemLo === 0 && elemHi === 0) { results.push(new Uint8Array(0)); continue; }
1862      const elemOffsetWords = (elemLo >> 2) | 0;
1863      const byteCount = elemHi >>> 3;
1864      const dataOffset = elemOffset + WORD_SIZE + elemOffsetWords * WORD_SIZE;
1865      results.push(this.raw.subarray(dataOffset, dataOffset + byteCount));
1866    }
1867    return results;
1868  }
1869
1870  /**
1871   * Read a fixed-width primitive list (List(Bool)/List(UIntN)/List(IntN)/List(FloatN))
1872   * within this struct. See `CapnpReader.readPrimitiveList` for the element-size
1873   * encoding this mirrors.
1874   */
1875  private readPrimitiveList<T>(
1876    ptrIndex: number,
1877    expectedElemSize: number,
1878    byteWidth: number,
1879    read: (byteOffset: number, bitIndex: number) => T,
1880  ): T[] {
1881    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
1882    const resolved = this.resolvePtr(ptrOffset);
1883    if (!resolved) return [];
1884    const { lo, hi, offset } = resolved;
1885    if ((lo & 3) !== 1) return [];
1886
1887    const offsetWords = (lo >> 2) | 0;
1888    const elementSize = hi & 7;
1889    if (elementSize !== expectedElemSize) return [];
1890    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1891    const count = hi >>> 3;
1892
1893    const results: T[] = [];
1894    for (let i = 0; i < count; i++) {
1895      results.push(
1896        byteWidth === 0
1897          ? read(targetOffset + (i >> 3), i & 7)
1898          : read(targetOffset + i * byteWidth, 0),
1899      );
1900    }
1901    return results;
1902  }
1903
1904  getBoolList(ptrIndex: number): boolean[] {
1905    return this.readPrimitiveList(ptrIndex, 1, 0, (byteOffset, bitIndex) => (this.raw[byteOffset] & (1 << bitIndex)) !== 0);
1906  }
1907
1908  getUint8List(ptrIndex: number): number[] {
1909    return this.readPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getUint8(o));
1910  }
1911
1912  getInt8List(ptrIndex: number): number[] {
1913    return this.readPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getInt8(o));
1914  }
1915
1916  getUint16List(ptrIndex: number): number[] {
1917    return this.readPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getUint16(o, true));
1918  }
1919
1920  getInt16List(ptrIndex: number): number[] {
1921    return this.readPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getInt16(o, true));
1922  }
1923
1924  getUint32List(ptrIndex: number): number[] {
1925    return this.readPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getUint32(o, true));
1926  }
1927
1928  getInt32List(ptrIndex: number): number[] {
1929    return this.readPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getInt32(o, true));
1930  }
1931
1932  getFloat32List(ptrIndex: number): number[] {
1933    return this.readPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getFloat32(o, true));
1934  }
1935
1936  getUint64List(ptrIndex: number): bigint[] {
1937    return this.readPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigUint64(o, true));
1938  }
1939
1940  getInt64List(ptrIndex: number): bigint[] {
1941    return this.readPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigInt64(o, true));
1942  }
1943
1944  getFloat64List(ptrIndex: number): number[] {
1945    return this.readPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getFloat64(o, true));
1946  }
1947
1948  /**
1949   * Read a `List(List(<inner>))` field within this struct. See
1950   * `CapnpReader.readNestedPrimitiveList` for the encoding this mirrors.
1951   */
1952  private readNestedPrimitiveList<T>(
1953    ptrIndex: number,
1954    innerElemSize: number,
1955    innerByteWidth: number,
1956    readInner: (byteOffset: number, bitIndex: number) => T,
1957  ): T[][] {
1958    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
1959    const resolved = this.resolvePtr(ptrOffset);
1960    if (!resolved) return [];
1961    const { lo, hi, offset } = resolved;
1962    if ((lo & 3) !== 1) return [];
1963
1964    const offsetWords = (lo >> 2) | 0;
1965    const outerElementSize = hi & 7;
1966    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
1967
1968    let outerCount: number;
1969    let outerBase: number;
1970    if (outerElementSize === 6) {
1971      outerCount = hi >>> 3;
1972      outerBase = targetOffset;
1973    } else if (outerElementSize === 7) {
1974      const tagLo = this.buf.getUint32(targetOffset, true);
1975      outerCount = (tagLo >> 2) | 0;
1976      outerBase = targetOffset + WORD_SIZE;
1977    } else {
1978      return [];
1979    }
1980
1981    const results: T[][] = [];
1982    for (let i = 0; i < outerCount; i++) {
1983      const elemPtrOffset = outerBase + i * WORD_SIZE;
1984      const elemResolved = this.resolvePtr(elemPtrOffset);
1985      if (!elemResolved) { results.push([]); continue; }
1986      const { lo: iLo, hi: iHi, offset: iOffset } = elemResolved;
1987      if ((iLo & 3) !== 1 || (iHi & 7) !== innerElemSize) { results.push([]); continue; }
1988      const iOffsetWords = (iLo >> 2) | 0;
1989      const innerTarget = iOffset + WORD_SIZE + iOffsetWords * WORD_SIZE;
1990      const innerCount = iHi >>> 3;
1991      const inner: T[] = [];
1992      for (let j = 0; j < innerCount; j++) {
1993        inner.push(
1994          innerByteWidth === 0
1995            ? readInner(innerTarget + (j >> 3), j & 7)
1996            : readInner(innerTarget + j * innerByteWidth, 0),
1997        );
1998      }
1999      results.push(inner);
2000    }
2001    return results;
2002  }
2003
2004  getBoolListList(ptrIndex: number): boolean[][] {
2005    return this.readNestedPrimitiveList(ptrIndex, 1, 0, (byteOffset, bitIndex) => (this.raw[byteOffset] & (1 << bitIndex)) !== 0);
2006  }
2007
2008  getUint8ListList(ptrIndex: number): number[][] {
2009    return this.readNestedPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getUint8(o));
2010  }
2011
2012  getInt8ListList(ptrIndex: number): number[][] {
2013    return this.readNestedPrimitiveList(ptrIndex, 2, 1, (o) => this.buf.getInt8(o));
2014  }
2015
2016  getUint16ListList(ptrIndex: number): number[][] {
2017    return this.readNestedPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getUint16(o, true));
2018  }
2019
2020  getInt16ListList(ptrIndex: number): number[][] {
2021    return this.readNestedPrimitiveList(ptrIndex, 3, 2, (o) => this.buf.getInt16(o, true));
2022  }
2023
2024  getUint32ListList(ptrIndex: number): number[][] {
2025    return this.readNestedPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getUint32(o, true));
2026  }
2027
2028  getInt32ListList(ptrIndex: number): number[][] {
2029    return this.readNestedPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getInt32(o, true));
2030  }
2031
2032  getFloat32ListList(ptrIndex: number): number[][] {
2033    return this.readNestedPrimitiveList(ptrIndex, 4, 4, (o) => this.buf.getFloat32(o, true));
2034  }
2035
2036  getUint64ListList(ptrIndex: number): bigint[][] {
2037    return this.readNestedPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigUint64(o, true));
2038  }
2039
2040  getInt64ListList(ptrIndex: number): bigint[][] {
2041    return this.readNestedPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getBigInt64(o, true));
2042  }
2043
2044  getFloat64ListList(ptrIndex: number): number[][] {
2045    return this.readNestedPrimitiveList(ptrIndex, 5, 8, (o) => this.buf.getFloat64(o, true));
2046  }
2047
2048  getStructList(ptrIndex: number, _dataWords: number, _ptrWords: number): StructReader[] {
2049    const ptrOffset = this.dataOffset + this.dataWords * WORD_SIZE + ptrIndex * WORD_SIZE;
2050    const resolved = this.resolvePtr(ptrOffset);
2051    if (!resolved) return [];
2052    const { lo, hi, offset } = resolved;
2053    if ((lo & 3) !== 1) return [];
2054
2055    const offsetWords = (lo >> 2) | 0;
2056    const elementSize = hi & 7;
2057    const targetOffset = offset + WORD_SIZE + offsetWords * WORD_SIZE;
2058
2059    if (elementSize !== 7) return [];
2060    const tagLo = this.buf.getUint32(targetOffset, true);
2061    const tagHi = this.buf.getUint32(targetOffset + 4, true);
2062    const elementCount = (tagLo >> 2) | 0;
2063    const tagDataWords = tagHi & 0xFFFF;
2064    const tagPtrWords = (tagHi >> 16) & 0xFFFF;
2065    const elementWords = tagDataWords + tagPtrWords;
2066
2067    const results: StructReader[] = [];
2068    for (let i = 0; i < elementCount; i++) {
2069      const elemDataOffset = targetOffset + WORD_SIZE + i * elementWords * WORD_SIZE;
2070      results.push(new StructReader(this.raw, this.buf, elemDataOffset, tagDataWords, tagPtrWords, this.segmentOffsets));
2071    }
2072    return results;
2073  }
2074}
2075
2076/** Backwards-compatible alias — prefer CapnpArena for new code. */
2077export const CapnpMessageBuilder = CapnpArena;
2078"##.to_owned()
2079}