@naeemo/capnp 0.1.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.
package/dist/index.js ADDED
@@ -0,0 +1,747 @@
1
+ //#region src/core/pointer.ts
2
+ /**
3
+ * Cap'n Proto 指针编解码
4
+ * 纯 TypeScript 实现
5
+ */
6
+ let PointerTag = /* @__PURE__ */ function(PointerTag) {
7
+ PointerTag[PointerTag["STRUCT"] = 0] = "STRUCT";
8
+ PointerTag[PointerTag["LIST"] = 1] = "LIST";
9
+ PointerTag[PointerTag["FAR"] = 2] = "FAR";
10
+ PointerTag[PointerTag["OTHER"] = 3] = "OTHER";
11
+ return PointerTag;
12
+ }({});
13
+ let ElementSize = /* @__PURE__ */ function(ElementSize) {
14
+ ElementSize[ElementSize["VOID"] = 0] = "VOID";
15
+ ElementSize[ElementSize["BIT"] = 1] = "BIT";
16
+ ElementSize[ElementSize["BYTE"] = 2] = "BYTE";
17
+ ElementSize[ElementSize["TWO_BYTES"] = 3] = "TWO_BYTES";
18
+ ElementSize[ElementSize["FOUR_BYTES"] = 4] = "FOUR_BYTES";
19
+ ElementSize[ElementSize["EIGHT_BYTES"] = 5] = "EIGHT_BYTES";
20
+ ElementSize[ElementSize["POINTER"] = 6] = "POINTER";
21
+ ElementSize[ElementSize["COMPOSITE"] = 7] = "COMPOSITE";
22
+ return ElementSize;
23
+ }({});
24
+ /**
25
+ * 解码指针(64位)
26
+ */
27
+ function decodePointer(ptr) {
28
+ const tag = Number(ptr & BigInt(3));
29
+ switch (tag) {
30
+ case PointerTag.STRUCT: {
31
+ const offset = Number(ptr >> BigInt(2)) & 1073741823;
32
+ return {
33
+ tag,
34
+ offset: offset >= 536870912 ? offset - 1073741824 : offset,
35
+ dataWords: Number(ptr >> BigInt(32) & BigInt(65535)),
36
+ pointerCount: Number(ptr >> BigInt(48) & BigInt(65535))
37
+ };
38
+ }
39
+ case PointerTag.LIST: {
40
+ const offset = Number(ptr >> BigInt(2)) & 1073741823;
41
+ return {
42
+ tag,
43
+ offset: offset >= 536870912 ? offset - 1073741824 : offset,
44
+ elementSize: Number(ptr >> BigInt(32) & BigInt(7)),
45
+ elementCount: Number(ptr >> BigInt(35) & BigInt(536870911))
46
+ };
47
+ }
48
+ case PointerTag.FAR: {
49
+ const doubleFar = Boolean(ptr >> BigInt(2) & BigInt(1));
50
+ const targetOffset = Number(ptr >> BigInt(3) & BigInt(536870911));
51
+ return {
52
+ tag,
53
+ doubleFar,
54
+ targetSegment: Number(ptr >> BigInt(32) & BigInt(4294967295)),
55
+ targetOffset
56
+ };
57
+ }
58
+ default: return { tag: PointerTag.OTHER };
59
+ }
60
+ }
61
+ /**
62
+ * 编码 Struct 指针
63
+ */
64
+ function encodeStructPointer(offset, dataWords, pointerCount) {
65
+ return (BigInt(offset < 0 ? offset + 1073741824 : offset) & BigInt(1073741823)) << BigInt(2) | BigInt(dataWords) << BigInt(32) | BigInt(pointerCount) << BigInt(48);
66
+ }
67
+ /**
68
+ * 编码 List 指针
69
+ */
70
+ function encodeListPointer(offset, elementSize, elementCount) {
71
+ return (BigInt(offset < 0 ? offset + 1073741824 : offset) & BigInt(1073741823)) << BigInt(2) | BigInt(1) | BigInt(elementSize) << BigInt(32) | BigInt(elementCount) << BigInt(35);
72
+ }
73
+
74
+ //#endregion
75
+ //#region src/core/segment.ts
76
+ /**
77
+ * Cap'n Proto Segment 管理
78
+ * 纯 TypeScript 实现
79
+ */
80
+ const WORD_SIZE = 8;
81
+ var Segment = class Segment {
82
+ buffer;
83
+ view;
84
+ _size;
85
+ constructor(initialCapacity = 1024) {
86
+ this.buffer = new ArrayBuffer(initialCapacity);
87
+ this.view = new DataView(this.buffer);
88
+ this._size = 0;
89
+ }
90
+ /**
91
+ * 从现有 buffer 创建(用于读取)
92
+ */
93
+ static fromBuffer(buffer) {
94
+ const seg = new Segment(0);
95
+ seg.buffer = buffer;
96
+ seg.view = new DataView(buffer);
97
+ seg._size = buffer.byteLength;
98
+ return seg;
99
+ }
100
+ /**
101
+ * 确保容量足够
102
+ */
103
+ ensureCapacity(minBytes) {
104
+ if (this.buffer.byteLength >= minBytes) return;
105
+ let newCapacity = this.buffer.byteLength * 2;
106
+ while (newCapacity < minBytes) newCapacity *= 2;
107
+ const newBuffer = new ArrayBuffer(newCapacity);
108
+ new Uint8Array(newBuffer).set(new Uint8Array(this.buffer, 0, this._size));
109
+ this.buffer = newBuffer;
110
+ this.view = new DataView(newBuffer);
111
+ }
112
+ /**
113
+ * 分配空间,返回字偏移
114
+ */
115
+ allocate(words) {
116
+ const bytes = words * WORD_SIZE;
117
+ const offset = this._size;
118
+ this.ensureCapacity(offset + bytes);
119
+ this._size = offset + bytes;
120
+ return offset / WORD_SIZE;
121
+ }
122
+ /**
123
+ * 获取字(64位)
124
+ */
125
+ getWord(wordOffset) {
126
+ const byteOffset = wordOffset * WORD_SIZE;
127
+ const low = BigInt(this.view.getUint32(byteOffset, true));
128
+ return BigInt(this.view.getUint32(byteOffset + 4, true)) << BigInt(32) | low;
129
+ }
130
+ /**
131
+ * 设置字(64位)
132
+ */
133
+ setWord(wordOffset, value) {
134
+ const byteOffset = wordOffset * WORD_SIZE;
135
+ this.view.setUint32(byteOffset, Number(value & BigInt(4294967295)), true);
136
+ this.view.setUint32(byteOffset + 4, Number(value >> BigInt(32)), true);
137
+ }
138
+ /**
139
+ * 获取原始 buffer(只读到 _size)
140
+ */
141
+ asUint8Array() {
142
+ return new Uint8Array(this.buffer, 0, this._size);
143
+ }
144
+ /**
145
+ * 获取字数量
146
+ */
147
+ get wordCount() {
148
+ return this._size / WORD_SIZE;
149
+ }
150
+ /**
151
+ * 获取字节数量
152
+ */
153
+ get byteLength() {
154
+ return this._size;
155
+ }
156
+ /**
157
+ * 获取 DataView
158
+ */
159
+ get dataView() {
160
+ return this.view;
161
+ }
162
+ };
163
+
164
+ //#endregion
165
+ //#region src/core/message-builder.ts
166
+ /**
167
+ * Cap'n Proto MessageBuilder
168
+ * 纯 TypeScript 实现
169
+ */
170
+ var MessageBuilder = class {
171
+ segment;
172
+ rootSet = false;
173
+ constructor() {
174
+ this.segment = new Segment(1024);
175
+ this.segment.allocate(1);
176
+ }
177
+ /**
178
+ * 初始化根结构
179
+ */
180
+ initRoot(dataWords, pointerCount) {
181
+ if (this.rootSet) throw new Error("Root already initialized");
182
+ const size = dataWords + pointerCount;
183
+ const structOffset = this.segment.allocate(size);
184
+ const rootPtr = encodeStructPointer(structOffset - 1, dataWords, pointerCount);
185
+ this.segment.setWord(0, rootPtr);
186
+ this.rootSet = true;
187
+ return new StructBuilder(this, 0, structOffset, dataWords, pointerCount);
188
+ }
189
+ /**
190
+ * 序列化为 ArrayBuffer
191
+ */
192
+ toArrayBuffer() {
193
+ const segmentData = this.segment.asUint8Array();
194
+ const wordCount = this.segment.wordCount;
195
+ const header = /* @__PURE__ */ new ArrayBuffer(8);
196
+ const headerView = new DataView(header);
197
+ headerView.setUint32(0, 0, true);
198
+ headerView.setUint32(4, wordCount, true);
199
+ const result = new Uint8Array(8 + segmentData.byteLength);
200
+ result.set(new Uint8Array(header), 0);
201
+ result.set(segmentData, 8);
202
+ return result.buffer;
203
+ }
204
+ /**
205
+ * 获取段(内部使用)
206
+ */
207
+ getSegment() {
208
+ return this.segment;
209
+ }
210
+ };
211
+ /**
212
+ * 结构构建器
213
+ */
214
+ var StructBuilder = class StructBuilder {
215
+ constructor(message, segmentIndex, wordOffset, dataWords, pointerCount) {
216
+ this.message = message;
217
+ this.segmentIndex = segmentIndex;
218
+ this.wordOffset = wordOffset;
219
+ this.dataWords = dataWords;
220
+ this.pointerCount = pointerCount;
221
+ }
222
+ /**
223
+ * 设置 bool 字段
224
+ */
225
+ setBool(bitOffset, value) {
226
+ const byteOffset = Math.floor(bitOffset / 8);
227
+ const bitInByte = bitOffset % 8;
228
+ const view = this.message.getSegment().dataView;
229
+ const offset = this.wordOffset * WORD_SIZE + byteOffset;
230
+ const current = view.getUint8(offset);
231
+ const newValue = value ? current | 1 << bitInByte : current & ~(1 << bitInByte);
232
+ view.setUint8(offset, newValue);
233
+ }
234
+ /**
235
+ * 设置 int8 字段
236
+ */
237
+ setInt8(byteOffset, value) {
238
+ this.message.getSegment().dataView.setInt8(this.wordOffset * WORD_SIZE + byteOffset, value);
239
+ }
240
+ /**
241
+ * 设置 int16 字段
242
+ */
243
+ setInt16(byteOffset, value) {
244
+ this.message.getSegment().dataView.setInt16(this.wordOffset * WORD_SIZE + byteOffset, value, true);
245
+ }
246
+ /**
247
+ * 设置 int32 字段
248
+ */
249
+ setInt32(byteOffset, value) {
250
+ this.message.getSegment().dataView.setInt32(this.wordOffset * WORD_SIZE + byteOffset, value, true);
251
+ }
252
+ /**
253
+ * 设置 int64 字段
254
+ */
255
+ setInt64(byteOffset, value) {
256
+ const segment = this.message.getSegment();
257
+ const offset = this.wordOffset * WORD_SIZE + byteOffset;
258
+ segment.dataView.setUint32(offset, Number(value & BigInt(4294967295)), true);
259
+ segment.dataView.setInt32(offset + 4, Number(value >> BigInt(32)), true);
260
+ }
261
+ /**
262
+ * 设置 uint8 字段
263
+ */
264
+ setUint8(byteOffset, value) {
265
+ this.message.getSegment().dataView.setUint8(this.wordOffset * WORD_SIZE + byteOffset, value);
266
+ }
267
+ /**
268
+ * 设置 uint16 字段
269
+ */
270
+ setUint16(byteOffset, value) {
271
+ this.message.getSegment().dataView.setUint16(this.wordOffset * WORD_SIZE + byteOffset, value, true);
272
+ }
273
+ /**
274
+ * 设置 uint32 字段
275
+ */
276
+ setUint32(byteOffset, value) {
277
+ this.message.getSegment().dataView.setUint32(this.wordOffset * WORD_SIZE + byteOffset, value, true);
278
+ }
279
+ /**
280
+ * 设置 uint64 字段
281
+ */
282
+ setUint64(byteOffset, value) {
283
+ const segment = this.message.getSegment();
284
+ const offset = this.wordOffset * WORD_SIZE + byteOffset;
285
+ segment.dataView.setUint32(offset, Number(value & BigInt(4294967295)), true);
286
+ segment.dataView.setUint32(offset + 4, Number(value >> BigInt(32)), true);
287
+ }
288
+ /**
289
+ * 设置 float32 字段
290
+ */
291
+ setFloat32(byteOffset, value) {
292
+ this.message.getSegment().dataView.setFloat32(this.wordOffset * WORD_SIZE + byteOffset, value, true);
293
+ }
294
+ /**
295
+ * 设置 float64 字段
296
+ */
297
+ setFloat64(byteOffset, value) {
298
+ this.message.getSegment().dataView.setFloat64(this.wordOffset * WORD_SIZE + byteOffset, value, true);
299
+ }
300
+ /**
301
+ * 获取 uint16 字段(用于 UnionBuilder 读取 tag)
302
+ */
303
+ getUint16(byteOffset) {
304
+ return this.message.getSegment().dataView.getUint16(this.wordOffset * WORD_SIZE + byteOffset, true);
305
+ }
306
+ /**
307
+ * 设置文本字段
308
+ */
309
+ setText(pointerIndex, value) {
310
+ const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
311
+ const segment = this.message.getSegment();
312
+ const bytes = new TextEncoder().encode(`${value}\0`);
313
+ const wordCount = Math.ceil(bytes.length / WORD_SIZE);
314
+ const listOffset = segment.allocate(wordCount);
315
+ new Uint8Array(segment.dataView.buffer, listOffset * WORD_SIZE, bytes.length).set(bytes);
316
+ const ptr = encodeListPointer(listOffset - ptrOffset - 1, ElementSize.BYTE, bytes.length);
317
+ segment.setWord(ptrOffset, ptr);
318
+ }
319
+ /**
320
+ * 初始化嵌套结构
321
+ */
322
+ initStruct(pointerIndex, dataWords, pointerCount) {
323
+ const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
324
+ const segment = this.message.getSegment();
325
+ const size = dataWords + pointerCount;
326
+ const structOffset = segment.allocate(size);
327
+ const ptr = encodeStructPointer(structOffset - ptrOffset - 1, dataWords, pointerCount);
328
+ segment.setWord(ptrOffset, ptr);
329
+ return new StructBuilder(this.message, 0, structOffset, dataWords, pointerCount);
330
+ }
331
+ /**
332
+ * 初始化列表
333
+ */
334
+ initList(pointerIndex, elementSize, elementCount, structSize) {
335
+ const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
336
+ const segment = this.message.getSegment();
337
+ let elementWords = 1;
338
+ if (elementSize === ElementSize.BYTE) elementWords = 1;
339
+ else if (elementSize === ElementSize.TWO_BYTES) elementWords = 1;
340
+ else if (elementSize === ElementSize.FOUR_BYTES) elementWords = 1;
341
+ else if (elementSize === ElementSize.EIGHT_BYTES) elementWords = 1;
342
+ else if (elementSize === ElementSize.COMPOSITE && structSize) elementWords = structSize.dataWords + structSize.pointerCount;
343
+ const totalWords = elementWords * elementCount;
344
+ const listOffset = segment.allocate(totalWords);
345
+ const ptr = encodeListPointer(listOffset - ptrOffset - 1, elementSize, elementCount);
346
+ segment.setWord(ptrOffset, ptr);
347
+ return new ListBuilder(this.message, elementSize, elementCount, structSize, listOffset);
348
+ }
349
+ };
350
+
351
+ //#endregion
352
+ //#region src/core/list.ts
353
+ /**
354
+ * Cap'n Proto List 实现
355
+ * 纯 TypeScript
356
+ */
357
+ /**
358
+ * ListReader - 读取列表
359
+ */
360
+ var ListReader = class {
361
+ segment;
362
+ startOffset;
363
+ constructor(message, segmentIndex, elementSize, elementCount, structSize, wordOffset) {
364
+ this.message = message;
365
+ this.elementSize = elementSize;
366
+ this.elementCount = elementCount;
367
+ this.structSize = structSize;
368
+ this.segment = message.getSegment(segmentIndex);
369
+ this.startOffset = wordOffset;
370
+ }
371
+ /**
372
+ * 列表长度
373
+ */
374
+ get length() {
375
+ return this.elementCount;
376
+ }
377
+ /**
378
+ * 获取元素(基础类型)
379
+ */
380
+ getPrimitive(index) {
381
+ if (index < 0 || index >= this.elementCount) throw new RangeError("Index out of bounds");
382
+ switch (this.elementSize) {
383
+ case ElementSize.BIT: {
384
+ const byteOffset = Math.floor(index / 8);
385
+ const bitInByte = index % 8;
386
+ return (this.segment.dataView.getUint8(this.startOffset * WORD_SIZE + byteOffset) & 1 << bitInByte) !== 0 ? 1 : 0;
387
+ }
388
+ case ElementSize.BYTE: return this.segment.dataView.getUint8(this.startOffset * WORD_SIZE + index);
389
+ case ElementSize.TWO_BYTES: return this.segment.dataView.getUint16(this.startOffset * WORD_SIZE + index * 2, true);
390
+ case ElementSize.FOUR_BYTES: return this.segment.dataView.getUint32(this.startOffset * WORD_SIZE + index * 4, true);
391
+ case ElementSize.EIGHT_BYTES: {
392
+ const offset = this.startOffset * WORD_SIZE + index * 8;
393
+ const low = BigInt(this.segment.dataView.getUint32(offset, true));
394
+ return BigInt(this.segment.dataView.getUint32(offset + 4, true)) << BigInt(32) | low;
395
+ }
396
+ default: throw new Error(`Unsupported element size: ${this.elementSize}`);
397
+ }
398
+ }
399
+ /**
400
+ * 获取结构元素
401
+ */
402
+ getStruct(index) {
403
+ if (!this.structSize) throw new Error("Not a struct list");
404
+ const { dataWords, pointerCount } = this.structSize;
405
+ const size = dataWords + pointerCount;
406
+ const offset = this.startOffset + index * size;
407
+ return new StructReader(this.message, 0, offset, dataWords, pointerCount);
408
+ }
409
+ /**
410
+ * 迭代器
411
+ */
412
+ *[Symbol.iterator]() {
413
+ for (let i = 0; i < this.elementCount; i++) yield this.getPrimitive(i);
414
+ }
415
+ };
416
+ /**
417
+ * ListBuilder - 构建列表
418
+ */
419
+ var ListBuilder = class {
420
+ segment;
421
+ startOffset;
422
+ constructor(message, elementSize, elementCount, structSize, wordOffset) {
423
+ this.message = message;
424
+ this.elementSize = elementSize;
425
+ this.elementCount = elementCount;
426
+ this.structSize = structSize;
427
+ this.segment = message.getSegment();
428
+ this.startOffset = wordOffset;
429
+ }
430
+ /**
431
+ * 列表长度
432
+ */
433
+ get length() {
434
+ return this.elementCount;
435
+ }
436
+ /**
437
+ * 设置基础类型元素
438
+ */
439
+ setPrimitive(index, value) {
440
+ if (index < 0 || index >= this.elementCount) throw new RangeError("Index out of bounds");
441
+ switch (this.elementSize) {
442
+ case ElementSize.BIT: {
443
+ const byteOffset = Math.floor(index / 8);
444
+ const bitInByte = index % 8;
445
+ const offset = this.startOffset * WORD_SIZE + byteOffset;
446
+ const current = this.segment.dataView.getUint8(offset);
447
+ const newValue = value ? current | 1 << bitInByte : current & ~(1 << bitInByte);
448
+ this.segment.dataView.setUint8(offset, newValue);
449
+ break;
450
+ }
451
+ case ElementSize.BYTE:
452
+ this.segment.dataView.setUint8(this.startOffset * WORD_SIZE + index, Number(value));
453
+ break;
454
+ case ElementSize.TWO_BYTES:
455
+ this.segment.dataView.setUint16(this.startOffset * WORD_SIZE + index * 2, Number(value), true);
456
+ break;
457
+ case ElementSize.FOUR_BYTES:
458
+ this.segment.dataView.setUint32(this.startOffset * WORD_SIZE + index * 4, Number(value), true);
459
+ break;
460
+ case ElementSize.EIGHT_BYTES: {
461
+ const offset = this.startOffset * WORD_SIZE + index * 8;
462
+ const bigValue = value;
463
+ this.segment.dataView.setUint32(offset, Number(bigValue & BigInt(4294967295)), true);
464
+ this.segment.dataView.setUint32(offset + 4, Number(bigValue >> BigInt(32)), true);
465
+ break;
466
+ }
467
+ default: throw new Error(`Unsupported element size: ${this.elementSize}`);
468
+ }
469
+ }
470
+ /**
471
+ * 获取结构元素(用于修改)
472
+ */
473
+ getStruct(index) {
474
+ if (!this.structSize) throw new Error("Not a struct list");
475
+ const { dataWords, pointerCount } = this.structSize;
476
+ const size = dataWords + pointerCount;
477
+ const offset = this.startOffset + index * size;
478
+ return new StructBuilder(this.message, 0, offset, dataWords, pointerCount);
479
+ }
480
+ };
481
+
482
+ //#endregion
483
+ //#region src/core/message-reader.ts
484
+ /**
485
+ * Cap'n Proto MessageReader
486
+ * 纯 TypeScript 实现
487
+ */
488
+ var MessageReader = class {
489
+ segments;
490
+ constructor(buffer) {
491
+ const uint8Array = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
492
+ this.segments = [];
493
+ if (uint8Array.byteLength < 8) return;
494
+ const view = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
495
+ const firstWordLow = view.getUint32(0, true);
496
+ const firstWordHigh = view.getUint32(4, true);
497
+ const segmentCount = (firstWordLow & 4294967295) + 1;
498
+ const firstSegmentSize = firstWordHigh;
499
+ let offset = 8;
500
+ const segmentSizes = [firstSegmentSize];
501
+ for (let i = 1; i < segmentCount; i++) {
502
+ if (offset + 4 > uint8Array.byteLength) {
503
+ this.segments = [];
504
+ return;
505
+ }
506
+ segmentSizes.push(view.getUint32(offset, true));
507
+ offset += 4;
508
+ }
509
+ offset = offset + 7 & -8;
510
+ if (offset > uint8Array.byteLength) {
511
+ this.segments = [];
512
+ return;
513
+ }
514
+ this.segments = [];
515
+ for (const size of segmentSizes) {
516
+ if (offset + size * WORD_SIZE > uint8Array.byteLength) break;
517
+ const segmentBuffer = uint8Array.slice(offset, offset + size * WORD_SIZE);
518
+ this.segments.push(Segment.fromBuffer(segmentBuffer.buffer));
519
+ offset += size * WORD_SIZE;
520
+ }
521
+ }
522
+ /**
523
+ * 获取根结构
524
+ */
525
+ getRoot(_dataWords, _pointerCount) {
526
+ const segment = this.segments[0];
527
+ const ptr = decodePointer(segment.getWord(0));
528
+ if (ptr.tag !== PointerTag.STRUCT) throw new Error("Root pointer is not a struct");
529
+ const structPtr = ptr;
530
+ const dataOffset = 1 + structPtr.offset;
531
+ return new StructReader(this, 0, dataOffset, structPtr.dataWords, structPtr.pointerCount);
532
+ }
533
+ /**
534
+ * 获取段
535
+ */
536
+ getSegment(index) {
537
+ return this.segments[index];
538
+ }
539
+ /**
540
+ * 段数量
541
+ */
542
+ get segmentCount() {
543
+ return this.segments.length;
544
+ }
545
+ };
546
+ /**
547
+ * 结构读取器
548
+ */
549
+ var StructReader = class StructReader {
550
+ constructor(message, segmentIndex, wordOffset, dataWords, pointerCount) {
551
+ this.message = message;
552
+ this.segmentIndex = segmentIndex;
553
+ this.wordOffset = wordOffset;
554
+ this.dataWords = dataWords;
555
+ this.pointerCount = pointerCount;
556
+ }
557
+ /**
558
+ * 获取 bool 字段
559
+ */
560
+ getBool(bitOffset) {
561
+ const byteOffset = Math.floor(bitOffset / 8);
562
+ const bitInByte = bitOffset % 8;
563
+ return (this.message.getSegment(this.segmentIndex).dataView.getUint8(this.wordOffset * WORD_SIZE + byteOffset) & 1 << bitInByte) !== 0;
564
+ }
565
+ /**
566
+ * 获取 int8 字段
567
+ */
568
+ getInt8(byteOffset) {
569
+ return this.message.getSegment(this.segmentIndex).dataView.getInt8(this.wordOffset * WORD_SIZE + byteOffset);
570
+ }
571
+ /**
572
+ * 获取 int16 字段
573
+ */
574
+ getInt16(byteOffset) {
575
+ return this.message.getSegment(this.segmentIndex).dataView.getInt16(this.wordOffset * WORD_SIZE + byteOffset, true);
576
+ }
577
+ /**
578
+ * 获取 int32 字段
579
+ */
580
+ getInt32(byteOffset) {
581
+ return this.message.getSegment(this.segmentIndex).dataView.getInt32(this.wordOffset * WORD_SIZE + byteOffset, true);
582
+ }
583
+ /**
584
+ * 获取 int64 字段
585
+ */
586
+ getInt64(byteOffset) {
587
+ const segment = this.message.getSegment(this.segmentIndex);
588
+ const offset = this.wordOffset * WORD_SIZE + byteOffset;
589
+ const low = BigInt(segment.dataView.getUint32(offset, true));
590
+ return BigInt(segment.dataView.getInt32(offset + 4, true)) << BigInt(32) | low;
591
+ }
592
+ /**
593
+ * 获取 uint8 字段
594
+ */
595
+ getUint8(byteOffset) {
596
+ return this.message.getSegment(this.segmentIndex).dataView.getUint8(this.wordOffset * WORD_SIZE + byteOffset);
597
+ }
598
+ /**
599
+ * 获取 uint16 字段
600
+ */
601
+ getUint16(byteOffset) {
602
+ return this.message.getSegment(this.segmentIndex).dataView.getUint16(this.wordOffset * WORD_SIZE + byteOffset, true);
603
+ }
604
+ /**
605
+ * 获取 uint32 字段
606
+ */
607
+ getUint32(byteOffset) {
608
+ return this.message.getSegment(this.segmentIndex).dataView.getUint32(this.wordOffset * WORD_SIZE + byteOffset, true);
609
+ }
610
+ /**
611
+ * 获取 uint64 字段
612
+ */
613
+ getUint64(byteOffset) {
614
+ const segment = this.message.getSegment(this.segmentIndex);
615
+ const offset = this.wordOffset * WORD_SIZE + byteOffset;
616
+ const low = BigInt(segment.dataView.getUint32(offset, true));
617
+ return BigInt(segment.dataView.getUint32(offset + 4, true)) << BigInt(32) | low;
618
+ }
619
+ /**
620
+ * 获取 float32 字段
621
+ */
622
+ getFloat32(byteOffset) {
623
+ return this.message.getSegment(this.segmentIndex).dataView.getFloat32(this.wordOffset * WORD_SIZE + byteOffset, true);
624
+ }
625
+ /**
626
+ * 获取 float64 字段
627
+ */
628
+ getFloat64(byteOffset) {
629
+ return this.message.getSegment(this.segmentIndex).dataView.getFloat64(this.wordOffset * WORD_SIZE + byteOffset, true);
630
+ }
631
+ /**
632
+ * 获取文本字段
633
+ */
634
+ getText(pointerIndex) {
635
+ const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
636
+ const segment = this.message.getSegment(this.segmentIndex);
637
+ const ptrValue = segment.getWord(ptrOffset);
638
+ if (ptrValue === 0n) return "";
639
+ const ptr = decodePointer(ptrValue);
640
+ if (ptr.tag !== PointerTag.LIST) return "";
641
+ const listPtr = ptr;
642
+ const targetOffset = ptrOffset + 1 + listPtr.offset;
643
+ const bytes = new Uint8Array(segment.dataView.buffer, targetOffset * WORD_SIZE, listPtr.elementCount - 1);
644
+ return new TextDecoder().decode(bytes);
645
+ }
646
+ /**
647
+ * 获取嵌套结构
648
+ */
649
+ getStruct(pointerIndex, _dataWords, _pointerCount) {
650
+ const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
651
+ const ptrValue = this.message.getSegment(this.segmentIndex).getWord(ptrOffset);
652
+ if (ptrValue === 0n) return void 0;
653
+ const ptr = decodePointer(ptrValue);
654
+ if (ptr.tag !== PointerTag.STRUCT) return void 0;
655
+ const structPtr = ptr;
656
+ const targetOffset = ptrOffset + 1 + structPtr.offset;
657
+ return new StructReader(this.message, this.segmentIndex, targetOffset, structPtr.dataWords, structPtr.pointerCount);
658
+ }
659
+ /**
660
+ * 获取列表
661
+ */
662
+ getList(pointerIndex, _elementSize, structSize) {
663
+ const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
664
+ const ptrValue = this.message.getSegment(this.segmentIndex).getWord(ptrOffset);
665
+ if (ptrValue === 0n) return void 0;
666
+ const ptr = decodePointer(ptrValue);
667
+ if (ptr.tag !== PointerTag.LIST) return void 0;
668
+ const listPtr = ptr;
669
+ const targetOffset = ptrOffset + 1 + listPtr.offset;
670
+ return new ListReader(this.message, this.segmentIndex, listPtr.elementSize, listPtr.elementCount, structSize, targetOffset);
671
+ }
672
+ };
673
+
674
+ //#endregion
675
+ //#region src/core/union.ts
676
+ /**
677
+ * UnionReader - 读取 Union 的 tag 和 variant
678
+ */
679
+ var UnionReader = class {
680
+ constructor(struct, tagOffset, variants) {
681
+ this.struct = struct;
682
+ this.tagOffset = tagOffset;
683
+ this.variants = variants;
684
+ }
685
+ /**
686
+ * 获取当前激活的 variant tag
687
+ */
688
+ getTag() {
689
+ return this.struct.getUint16(this.tagOffset);
690
+ }
691
+ /**
692
+ * 获取当前激活的 variant 名称
693
+ */
694
+ getVariantName() {
695
+ return this.variants.get(this.getTag());
696
+ }
697
+ /**
698
+ * 检查是否是某个 variant
699
+ */
700
+ is(variantTag) {
701
+ return this.getTag() === variantTag;
702
+ }
703
+ };
704
+ /**
705
+ * UnionBuilder - 设置 Union 的 tag 和 variant
706
+ */
707
+ var UnionBuilder = class {
708
+ constructor(struct, tagOffset) {
709
+ this.struct = struct;
710
+ this.tagOffset = tagOffset;
711
+ }
712
+ /**
713
+ * 获取当前 tag
714
+ */
715
+ getTag() {
716
+ return this.struct.getUint16(this.tagOffset);
717
+ }
718
+ /**
719
+ * 设置 tag
720
+ */
721
+ setTag(tag) {
722
+ this.struct.setUint16(this.tagOffset, tag);
723
+ }
724
+ /**
725
+ * 初始化某个 variant(自动设置 tag)
726
+ */
727
+ initVariant(tag, initFn) {
728
+ this.setTag(tag);
729
+ initFn();
730
+ }
731
+ };
732
+ /**
733
+ * 创建 UnionReader 的工厂函数
734
+ */
735
+ function createUnionReader(struct, tagOffset, variants) {
736
+ return new UnionReader(struct, tagOffset, new Map(Object.entries(variants).map(([k, v]) => [Number(k), v])));
737
+ }
738
+ /**
739
+ * 创建 UnionBuilder 的工厂函数
740
+ */
741
+ function createUnionBuilder(struct, tagOffset) {
742
+ return new UnionBuilder(struct, tagOffset);
743
+ }
744
+
745
+ //#endregion
746
+ export { ElementSize, ListBuilder, ListReader, MessageBuilder, MessageReader, PointerTag, Segment, StructBuilder, StructReader, UnionBuilder, UnionReader, WORD_SIZE, createUnionBuilder, createUnionReader, decodePointer, encodeListPointer, encodeStructPointer };
747
+ //# sourceMappingURL=index.js.map