wpilog-parser 2.3.0 → 2.4.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/README.md +6 -2
- package/dist/{catalog-CYDrFwrh.mjs → catalog-BT6geQr0.mjs} +521 -249
- package/dist/cli.mjs +20 -10
- package/dist/index.d.mts +79 -67
- package/dist/index.mjs +2 -2
- package/package.json +13 -5
- package/skills/analyze-wpilog/SKILL.md +8 -8
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { defineDiagnostics } from "nostics";
|
|
1
2
|
import { CstParser, Lexer, createToken } from "chevrotain";
|
|
2
3
|
//#region src/byte-offset.ts
|
|
3
4
|
var ByteOffset = class {
|
|
@@ -26,71 +27,133 @@ var ByteOffset = class {
|
|
|
26
27
|
}
|
|
27
28
|
};
|
|
28
29
|
//#endregion
|
|
30
|
+
//#region src/diagnostics.ts
|
|
31
|
+
/** Stable codes for errors callers can act on. */
|
|
32
|
+
const diagnostics = defineDiagnostics({ codes: {
|
|
33
|
+
WPILOG_C0001: {
|
|
34
|
+
why: (p) => `Unknown command \`${p.command}\``,
|
|
35
|
+
fix: "Run `wpilog --help` to see the available commands."
|
|
36
|
+
},
|
|
37
|
+
WPILOG_C0002: {
|
|
38
|
+
why: "Output flags cannot be combined",
|
|
39
|
+
fix: "Choose one of --json, --jsonl, --ndjson, or --csv."
|
|
40
|
+
},
|
|
41
|
+
WPILOG_C0003: {
|
|
42
|
+
why: (p) => `File not found: ${p.filePath}`,
|
|
43
|
+
fix: "Check the path and try again with an existing WPILOG file."
|
|
44
|
+
},
|
|
45
|
+
WPILOG_R0001: {
|
|
46
|
+
why: (p) => `Invalid control record type ${p.type}`,
|
|
47
|
+
fix: "Check that the input is an uncorrupted WPILOG file."
|
|
48
|
+
},
|
|
49
|
+
WPILOG_R0002: {
|
|
50
|
+
why: "Not a WPILOG file (truncated header)",
|
|
51
|
+
fix: "Provide a complete WPILOG file."
|
|
52
|
+
},
|
|
53
|
+
WPILOG_R0003: {
|
|
54
|
+
why: "Not a WPILOG file (invalid magic bytes)",
|
|
55
|
+
fix: "Provide a file in WPILOG format."
|
|
56
|
+
},
|
|
57
|
+
WPILOG_R0004: {
|
|
58
|
+
why: "Not a WPILOG file (truncated extra header)",
|
|
59
|
+
fix: "Provide a complete WPILOG file."
|
|
60
|
+
},
|
|
61
|
+
WPILOG_R0005: {
|
|
62
|
+
why: (p) => `Invalid boolean value ${p.byte}`,
|
|
63
|
+
fix: "Use 0 or 1 for boolean values in the log."
|
|
64
|
+
},
|
|
65
|
+
WPILOG_R0006: {
|
|
66
|
+
why: (p) => `No type registered for entry ID ${p.entryId}`,
|
|
67
|
+
fix: "Include a Start control record for this entry before its data records."
|
|
68
|
+
},
|
|
69
|
+
WPILOG_R0007: {
|
|
70
|
+
why: "Failed to lex struct specification",
|
|
71
|
+
fix: "Check the struct specification for invalid characters or tokens."
|
|
72
|
+
},
|
|
73
|
+
WPILOG_R0008: {
|
|
74
|
+
why: "Failed to parse struct specification",
|
|
75
|
+
fix: "Check the struct specification syntax."
|
|
76
|
+
},
|
|
77
|
+
WPILOG_R0009: {
|
|
78
|
+
why: "Enums must be integers",
|
|
79
|
+
fix: "Use an integer type for enum members."
|
|
80
|
+
},
|
|
81
|
+
WPILOG_R0010: {
|
|
82
|
+
why: (p) => `Invalid ${p.width}-bit field for ${p.type} (maximum ${p.maxBits} bits)`,
|
|
83
|
+
fix: (p) => `Use a bit width from 1 to ${p.maxBits}.`
|
|
84
|
+
},
|
|
85
|
+
WPILOG_R0011: {
|
|
86
|
+
why: "Bit-field members must be integers or booleans",
|
|
87
|
+
fix: "Use an integer or boolean type for bit-field members."
|
|
88
|
+
},
|
|
89
|
+
WPILOG_R0012: {
|
|
90
|
+
why: (p) => `Cycle detected in the dependency graph for ${p.name}`,
|
|
91
|
+
fix: "Remove the circular struct reference."
|
|
92
|
+
},
|
|
93
|
+
WPILOG_R0013: {
|
|
94
|
+
why: (p) => `Unknown struct definition: ${p.name}`,
|
|
95
|
+
fix: "Register the struct definition before decoding its records."
|
|
96
|
+
}
|
|
97
|
+
} });
|
|
98
|
+
//#endregion
|
|
29
99
|
//#region src/types.ts
|
|
30
100
|
/** Control record types in the WPILOG format. */
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}({});
|
|
101
|
+
const ControlRecordType = {
|
|
102
|
+
Start: 0,
|
|
103
|
+
Finish: 1,
|
|
104
|
+
SetMetadata: 2
|
|
105
|
+
};
|
|
37
106
|
/** Discriminated union tag for decoded record types. */
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}({});
|
|
107
|
+
const RecordType = {
|
|
108
|
+
Control: "control",
|
|
109
|
+
Boolean: "boolean",
|
|
110
|
+
Int64: "int64",
|
|
111
|
+
Float: "float",
|
|
112
|
+
Double: "double",
|
|
113
|
+
String: "string",
|
|
114
|
+
BooleanArray: "boolean[]",
|
|
115
|
+
Int64Array: "int64[]",
|
|
116
|
+
FloatArray: "float[]",
|
|
117
|
+
DoubleArray: "double[]",
|
|
118
|
+
StringArray: "string[]",
|
|
119
|
+
Struct: "struct",
|
|
120
|
+
StructArray: "struct[]",
|
|
121
|
+
Raw: "raw"
|
|
122
|
+
};
|
|
55
123
|
/** Type guard that narrows a {@link DecodedRecord} to a {@link DataRecord}. */
|
|
56
124
|
function isDataRecord(record) {
|
|
57
|
-
return record.type !==
|
|
125
|
+
return record.type !== RecordType.Control;
|
|
58
126
|
}
|
|
59
127
|
//#endregion
|
|
60
128
|
//#region src/read-records.ts
|
|
61
129
|
const TEXT_ENCODER = new TextEncoder();
|
|
62
130
|
const TEXT_DECODER$1 = new TextDecoder();
|
|
63
131
|
const MAGIC = TEXT_ENCODER.encode("WPILOG");
|
|
132
|
+
function readString(payload, view, offset) {
|
|
133
|
+
const length = view.getUint32(offset.get(), true);
|
|
134
|
+
offset.advance32();
|
|
135
|
+
const value = TEXT_DECODER$1.decode(payload.subarray(offset.get(), offset.get() + length));
|
|
136
|
+
offset.advance(length);
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
64
139
|
function readControlRecordPayload(payload) {
|
|
65
140
|
const offset = new ByteOffset();
|
|
66
141
|
const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
67
142
|
const type = view.getUint8(offset.get());
|
|
68
143
|
offset.advance8();
|
|
69
144
|
switch (type) {
|
|
70
|
-
case
|
|
145
|
+
case ControlRecordType.Start: {
|
|
71
146
|
const entryId = view.getUint32(offset.get(), true);
|
|
72
147
|
offset.advance32();
|
|
73
|
-
const entryNameLength = view.getUint32(offset.get(), true);
|
|
74
|
-
offset.advance32();
|
|
75
|
-
const entryName = TEXT_DECODER$1.decode(payload.subarray(offset.get(), offset.get() + entryNameLength));
|
|
76
|
-
offset.advance(entryNameLength);
|
|
77
|
-
const entryTypeLength = view.getUint32(offset.get(), true);
|
|
78
|
-
offset.advance32();
|
|
79
|
-
const entryType = TEXT_DECODER$1.decode(payload.subarray(offset.get(), offset.get() + entryTypeLength));
|
|
80
|
-
offset.advance(entryTypeLength);
|
|
81
|
-
const entryMetadataLength = view.getUint32(offset.get(), true);
|
|
82
|
-
offset.advance32();
|
|
83
|
-
const entryMetadata = TEXT_DECODER$1.decode(payload.subarray(offset.get(), offset.get() + entryMetadataLength));
|
|
84
|
-
offset.advance(entryMetadataLength);
|
|
85
148
|
return {
|
|
86
149
|
controlRecordType: type,
|
|
87
150
|
entryId,
|
|
88
|
-
entryName,
|
|
89
|
-
entryType,
|
|
90
|
-
entryMetadata
|
|
151
|
+
entryName: readString(payload, view, offset),
|
|
152
|
+
entryType: readString(payload, view, offset),
|
|
153
|
+
entryMetadata: readString(payload, view, offset)
|
|
91
154
|
};
|
|
92
155
|
}
|
|
93
|
-
case
|
|
156
|
+
case ControlRecordType.Finish: {
|
|
94
157
|
const entryId = view.getUint32(offset.get(), true);
|
|
95
158
|
offset.advance32();
|
|
96
159
|
return {
|
|
@@ -98,26 +161,42 @@ function readControlRecordPayload(payload) {
|
|
|
98
161
|
entryId
|
|
99
162
|
};
|
|
100
163
|
}
|
|
101
|
-
case
|
|
164
|
+
case ControlRecordType.SetMetadata: {
|
|
102
165
|
const entryId = view.getUint32(offset.get(), true);
|
|
103
166
|
offset.advance32();
|
|
104
|
-
const entryMetadataLength = view.getUint32(offset.get(), true);
|
|
105
|
-
offset.advance32();
|
|
106
|
-
const entryMetadata = TEXT_DECODER$1.decode(payload.subarray(offset.get(), offset.get() + entryMetadataLength));
|
|
107
|
-
offset.advance(entryMetadataLength);
|
|
108
167
|
return {
|
|
109
168
|
controlRecordType: type,
|
|
110
169
|
entryId,
|
|
111
|
-
entryMetadata
|
|
170
|
+
entryMetadata: readString(payload, view, offset)
|
|
112
171
|
};
|
|
113
172
|
}
|
|
114
|
-
default: throw
|
|
173
|
+
default: throw diagnostics.WPILOG_R0001({ type });
|
|
115
174
|
}
|
|
116
175
|
}
|
|
117
176
|
function toUint8Array(input) {
|
|
118
177
|
if (input instanceof Uint8Array) return input;
|
|
119
178
|
return new Uint8Array(input);
|
|
120
179
|
}
|
|
180
|
+
function openDataLog(input) {
|
|
181
|
+
const bytes = toUint8Array(input);
|
|
182
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
183
|
+
if (bytes.byteLength < 12) throw diagnostics.WPILOG_R0002();
|
|
184
|
+
for (let i = 0; i < MAGIC.byteLength; i++) if (bytes[i] !== MAGIC[i]) throw diagnostics.WPILOG_R0003();
|
|
185
|
+
const recordsOffset = 12 + view.getUint32(8, true);
|
|
186
|
+
if (recordsOffset > bytes.byteLength) throw diagnostics.WPILOG_R0004();
|
|
187
|
+
return {
|
|
188
|
+
bytes,
|
|
189
|
+
view,
|
|
190
|
+
header: {
|
|
191
|
+
version: {
|
|
192
|
+
major: view.getUint8(7),
|
|
193
|
+
minor: view.getUint8(6)
|
|
194
|
+
},
|
|
195
|
+
extraHeader: TEXT_DECODER$1.decode(bytes.subarray(12, recordsOffset))
|
|
196
|
+
},
|
|
197
|
+
recordsOffset
|
|
198
|
+
};
|
|
199
|
+
}
|
|
121
200
|
function readVarInt(view, offset, length) {
|
|
122
201
|
switch (length) {
|
|
123
202
|
case 1: return view.getUint8(offset);
|
|
@@ -165,38 +244,74 @@ function readTimestamp(view, offset, length) {
|
|
|
165
244
|
default: throw new RangeError(`Invalid timestamp length ${length}`);
|
|
166
245
|
}
|
|
167
246
|
}
|
|
247
|
+
/** Mutable record cursor used by the fused decoder to avoid intermediate record allocations. */
|
|
248
|
+
var RecordCursor = class {
|
|
249
|
+
bytes;
|
|
250
|
+
view;
|
|
251
|
+
header;
|
|
252
|
+
entryId = 0;
|
|
253
|
+
timestamp = 0n;
|
|
254
|
+
payloadOffset = 0;
|
|
255
|
+
payloadSize = 0;
|
|
256
|
+
offset;
|
|
257
|
+
constructor(input) {
|
|
258
|
+
const source = openDataLog(input);
|
|
259
|
+
this.bytes = source.bytes;
|
|
260
|
+
this.view = source.view;
|
|
261
|
+
this.header = source.header;
|
|
262
|
+
this.offset = source.recordsOffset;
|
|
263
|
+
}
|
|
264
|
+
next() {
|
|
265
|
+
const total = this.bytes.byteLength;
|
|
266
|
+
let offset = this.offset;
|
|
267
|
+
if (offset >= total) return false;
|
|
268
|
+
const view = this.view;
|
|
269
|
+
const bitfield = view.getUint8(offset++);
|
|
270
|
+
const entryIdLength = 1 + (bitfield & 3);
|
|
271
|
+
const payloadSizeLength = 1 + (bitfield >> 2 & 3);
|
|
272
|
+
const timestampLength = 1 + (bitfield >> 4 & 7);
|
|
273
|
+
if (offset + entryIdLength + payloadSizeLength + timestampLength > total) {
|
|
274
|
+
this.offset = total;
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
const entryId = readVarInt(view, offset, entryIdLength);
|
|
278
|
+
offset += entryIdLength;
|
|
279
|
+
const payloadSize = readVarInt(view, offset, payloadSizeLength);
|
|
280
|
+
offset += payloadSizeLength;
|
|
281
|
+
const timestamp = readTimestamp(view, offset, timestampLength);
|
|
282
|
+
offset += timestampLength;
|
|
283
|
+
if (offset + payloadSize > total) {
|
|
284
|
+
this.offset = total;
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
this.entryId = entryId;
|
|
288
|
+
this.timestamp = timestamp;
|
|
289
|
+
this.payloadOffset = offset;
|
|
290
|
+
this.payloadSize = payloadSize;
|
|
291
|
+
this.offset = offset + payloadSize;
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
getPayload() {
|
|
295
|
+
return this.bytes.subarray(this.payloadOffset, this.payloadOffset + this.payloadSize);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
168
298
|
/**
|
|
169
299
|
* Read raw WPILOG records from an in-memory buffer.
|
|
170
300
|
*
|
|
171
301
|
* Yields a header record first, then control and data records in order.
|
|
172
|
-
* Data record payloads are not decoded
|
|
302
|
+
* Data record payloads are not decoded. Prefer `parseDataLog` when raw records
|
|
303
|
+
* or an intermediate transformation are not needed.
|
|
173
304
|
*/
|
|
174
305
|
function* readRecords(input) {
|
|
175
|
-
const bytes =
|
|
176
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
306
|
+
const { bytes, view, header, recordsOffset } = openDataLog(input);
|
|
177
307
|
const total = bytes.byteLength;
|
|
178
|
-
|
|
179
|
-
for (let i = 0; i < MAGIC.byteLength; i++) if (bytes[i] !== MAGIC[i]) throw new Error("Not a WPILOG file (invalid magic bytes)");
|
|
180
|
-
const versionMinor = view.getUint8(6);
|
|
181
|
-
const versionMajor = view.getUint8(7);
|
|
182
|
-
const extraHeaderLength = view.getUint32(8, true);
|
|
183
|
-
let offset = 12;
|
|
184
|
-
if (offset + extraHeaderLength > total) throw new Error("Not a WPILOG file (truncated extra header)");
|
|
185
|
-
const extraHeader = TEXT_DECODER$1.decode(bytes.subarray(offset, offset + extraHeaderLength));
|
|
186
|
-
offset += extraHeaderLength;
|
|
308
|
+
let offset = recordsOffset;
|
|
187
309
|
yield {
|
|
188
310
|
kind: "header",
|
|
189
|
-
header
|
|
190
|
-
version: {
|
|
191
|
-
major: versionMajor,
|
|
192
|
-
minor: versionMinor
|
|
193
|
-
},
|
|
194
|
-
extraHeader
|
|
195
|
-
}
|
|
311
|
+
header
|
|
196
312
|
};
|
|
197
313
|
while (offset < total) {
|
|
198
|
-
const bitfield = view.getUint8(offset);
|
|
199
|
-
offset += 1;
|
|
314
|
+
const bitfield = view.getUint8(offset++);
|
|
200
315
|
const entryIdLength = 1 + (bitfield & 3);
|
|
201
316
|
const payloadSizeLength = 1 + (bitfield >> 2 & 3);
|
|
202
317
|
const timestampLength = 1 + (bitfield >> 4 & 7);
|
|
@@ -228,25 +343,24 @@ function* readRecords(input) {
|
|
|
228
343
|
}
|
|
229
344
|
//#endregion
|
|
230
345
|
//#region src/struct/types.ts
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
346
|
+
const KnownStructTypeName = {
|
|
347
|
+
Boolean: "bool",
|
|
348
|
+
Character: "char",
|
|
349
|
+
Int8: "int8",
|
|
350
|
+
Int16: "int16",
|
|
351
|
+
Int32: "int32",
|
|
352
|
+
Int64: "int64",
|
|
353
|
+
Uint8: "uint8",
|
|
354
|
+
Uint16: "uint16",
|
|
355
|
+
Uint32: "uint32",
|
|
356
|
+
Uint64: "uint64",
|
|
357
|
+
Float32: "float32",
|
|
243
358
|
/** Equivalent to {@link KnownStructTypeName.Float32} */
|
|
244
|
-
|
|
245
|
-
|
|
359
|
+
Float: "float",
|
|
360
|
+
Float64: "float64",
|
|
246
361
|
/** Equivalent to {@link KnownStructTypeName.Float64} */
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}({});
|
|
362
|
+
Double: "double"
|
|
363
|
+
};
|
|
250
364
|
//#endregion
|
|
251
365
|
//#region src/struct/struct-dependency-graph.ts
|
|
252
366
|
const EMPTY_SET = /* @__PURE__ */ new Set();
|
|
@@ -256,7 +370,7 @@ var StructDependencyGraph = class {
|
|
|
256
370
|
const existing = this.allDependencies.get(name);
|
|
257
371
|
if (existing) for (const innerStructName of innerStructNames) existing.add(innerStructName);
|
|
258
372
|
else this.allDependencies.set(name, new Set(innerStructNames));
|
|
259
|
-
if (this.hasCycle(name)) throw
|
|
373
|
+
if (this.hasCycle(name)) throw diagnostics.WPILOG_R0012({ name });
|
|
260
374
|
}
|
|
261
375
|
hasCycle(current, path = /* @__PURE__ */ new Set()) {
|
|
262
376
|
if (path.has(current)) return true;
|
|
@@ -297,9 +411,9 @@ var StructDependencyGraph = class {
|
|
|
297
411
|
//#endregion
|
|
298
412
|
//#region src/struct/struct-decode-queue.ts
|
|
299
413
|
var StructDecodeQueue = class {
|
|
300
|
-
onStructDefined;
|
|
301
414
|
graph = new StructDependencyGraph();
|
|
302
415
|
queuedRecords = /* @__PURE__ */ new Map();
|
|
416
|
+
onStructDefined;
|
|
303
417
|
constructor(onStructDefined) {
|
|
304
418
|
this.onStructDefined = onStructDefined;
|
|
305
419
|
}
|
|
@@ -323,6 +437,60 @@ var StructDecodeQueue = class {
|
|
|
323
437
|
}
|
|
324
438
|
};
|
|
325
439
|
//#endregion
|
|
440
|
+
//#region src/struct/bit-field.ts
|
|
441
|
+
var BitFieldTracker = class {
|
|
442
|
+
storageByteLength = 0;
|
|
443
|
+
bitShift = 0;
|
|
444
|
+
next(member, memberByteLength) {
|
|
445
|
+
const bitWidth = member.bitWidth;
|
|
446
|
+
if (bitWidth === void 0 || bitWidth === 0 || bitWidth === memberByteLength * 8) return;
|
|
447
|
+
let bytesToAdvance = 0;
|
|
448
|
+
let storageByteLength = memberByteLength;
|
|
449
|
+
if (member.value === KnownStructTypeName.Boolean && this.storageByteLength !== 0 && this.bitShift + 1 <= this.storageByteLength * 8) storageByteLength = this.storageByteLength;
|
|
450
|
+
else if (storageByteLength !== this.storageByteLength || this.bitShift + bitWidth > storageByteLength * 8) bytesToAdvance = this.flush();
|
|
451
|
+
const layout = {
|
|
452
|
+
bytesToAdvance,
|
|
453
|
+
storageByteLength,
|
|
454
|
+
bitShift: this.bitShift,
|
|
455
|
+
bitWidth
|
|
456
|
+
};
|
|
457
|
+
this.storageByteLength = storageByteLength;
|
|
458
|
+
this.bitShift += bitWidth;
|
|
459
|
+
return layout;
|
|
460
|
+
}
|
|
461
|
+
flush() {
|
|
462
|
+
const bytesToAdvance = this.storageByteLength;
|
|
463
|
+
this.storageByteLength = 0;
|
|
464
|
+
this.bitShift = 0;
|
|
465
|
+
return bytesToAdvance;
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
function decodeBitField(view, offset, member, layout) {
|
|
469
|
+
let value = readUnsignedInteger(view, offset, layout.storageByteLength);
|
|
470
|
+
value = value >> BigInt(layout.bitShift) & (1n << BigInt(layout.bitWidth)) - 1n;
|
|
471
|
+
if (member.value === KnownStructTypeName.Boolean) return value !== 0n;
|
|
472
|
+
if (isSignedInteger(member.value) && (value & 1n << BigInt(layout.bitWidth - 1)) !== 0n) value -= 1n << BigInt(layout.bitWidth);
|
|
473
|
+
return member.value === KnownStructTypeName.Int64 || member.value === KnownStructTypeName.Uint64 ? value : Number(value);
|
|
474
|
+
}
|
|
475
|
+
function readUnsignedInteger(view, offset, byteLength) {
|
|
476
|
+
switch (byteLength) {
|
|
477
|
+
case 1: return BigInt(view.getUint8(offset));
|
|
478
|
+
case 2: return BigInt(view.getUint16(offset, true));
|
|
479
|
+
case 4: return BigInt(view.getUint32(offset, true));
|
|
480
|
+
case 8: return view.getBigUint64(offset, true);
|
|
481
|
+
default: throw new TypeError(`Invalid bit-field storage size: ${byteLength}`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function isSignedInteger(type) {
|
|
485
|
+
switch (type) {
|
|
486
|
+
case KnownStructTypeName.Int8:
|
|
487
|
+
case KnownStructTypeName.Int16:
|
|
488
|
+
case KnownStructTypeName.Int32:
|
|
489
|
+
case KnownStructTypeName.Int64: return true;
|
|
490
|
+
default: return false;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
//#endregion
|
|
326
494
|
//#region src/struct/lexer.ts
|
|
327
495
|
const Comma = createToken({
|
|
328
496
|
name: "Comma",
|
|
@@ -477,15 +645,15 @@ var StructAstVisitor = class extends BaseStructVisitorWithDefaults {
|
|
|
477
645
|
}
|
|
478
646
|
enumSpecification(children, dataType) {
|
|
479
647
|
switch (dataType) {
|
|
480
|
-
case
|
|
481
|
-
case
|
|
482
|
-
case
|
|
483
|
-
case
|
|
484
|
-
case
|
|
485
|
-
case
|
|
486
|
-
case
|
|
487
|
-
case
|
|
488
|
-
default: throw
|
|
648
|
+
case KnownStructTypeName.Int8:
|
|
649
|
+
case KnownStructTypeName.Uint8:
|
|
650
|
+
case KnownStructTypeName.Int16:
|
|
651
|
+
case KnownStructTypeName.Uint16:
|
|
652
|
+
case KnownStructTypeName.Int32:
|
|
653
|
+
case KnownStructTypeName.Uint32:
|
|
654
|
+
case KnownStructTypeName.Int64:
|
|
655
|
+
case KnownStructTypeName.Uint64: break;
|
|
656
|
+
default: throw diagnostics.WPILOG_R0009();
|
|
489
657
|
}
|
|
490
658
|
const entries = children.enumMember?.map((member) => this.enumMember(member.children));
|
|
491
659
|
return new Map(entries);
|
|
@@ -499,26 +667,46 @@ var StructAstVisitor = class extends BaseStructVisitorWithDefaults {
|
|
|
499
667
|
const [value] = children.Integer;
|
|
500
668
|
const parsed = Number(value.image);
|
|
501
669
|
switch (dataType) {
|
|
502
|
-
case
|
|
503
|
-
if (parsed !== 1) throw
|
|
670
|
+
case KnownStructTypeName.Boolean:
|
|
671
|
+
if (parsed !== 1) throw diagnostics.WPILOG_R0010({
|
|
672
|
+
type: dataType,
|
|
673
|
+
width: parsed,
|
|
674
|
+
maxBits: 1
|
|
675
|
+
});
|
|
504
676
|
break;
|
|
505
|
-
case
|
|
506
|
-
case
|
|
507
|
-
if (parsed > 8) throw
|
|
677
|
+
case KnownStructTypeName.Int8:
|
|
678
|
+
case KnownStructTypeName.Uint8:
|
|
679
|
+
if (parsed > 8) throw diagnostics.WPILOG_R0010({
|
|
680
|
+
type: dataType,
|
|
681
|
+
width: parsed,
|
|
682
|
+
maxBits: 8
|
|
683
|
+
});
|
|
508
684
|
break;
|
|
509
|
-
case
|
|
510
|
-
case
|
|
511
|
-
if (parsed > 16) throw
|
|
685
|
+
case KnownStructTypeName.Int16:
|
|
686
|
+
case KnownStructTypeName.Uint16:
|
|
687
|
+
if (parsed > 16) throw diagnostics.WPILOG_R0010({
|
|
688
|
+
type: dataType,
|
|
689
|
+
width: parsed,
|
|
690
|
+
maxBits: 16
|
|
691
|
+
});
|
|
512
692
|
break;
|
|
513
|
-
case
|
|
514
|
-
case
|
|
515
|
-
if (parsed > 32) throw
|
|
693
|
+
case KnownStructTypeName.Int32:
|
|
694
|
+
case KnownStructTypeName.Uint32:
|
|
695
|
+
if (parsed > 32) throw diagnostics.WPILOG_R0010({
|
|
696
|
+
type: dataType,
|
|
697
|
+
width: parsed,
|
|
698
|
+
maxBits: 32
|
|
699
|
+
});
|
|
516
700
|
break;
|
|
517
|
-
case
|
|
518
|
-
case
|
|
519
|
-
if (parsed > 64) throw
|
|
701
|
+
case KnownStructTypeName.Int64:
|
|
702
|
+
case KnownStructTypeName.Uint64:
|
|
703
|
+
if (parsed > 64) throw diagnostics.WPILOG_R0010({
|
|
704
|
+
type: dataType,
|
|
705
|
+
width: parsed,
|
|
706
|
+
maxBits: 64
|
|
707
|
+
});
|
|
520
708
|
break;
|
|
521
|
-
default: throw
|
|
709
|
+
default: throw diagnostics.WPILOG_R0011();
|
|
522
710
|
}
|
|
523
711
|
return parsed;
|
|
524
712
|
}
|
|
@@ -539,10 +727,10 @@ function parseStructSpecification(declaration) {
|
|
|
539
727
|
const existing = cache.get(declaration);
|
|
540
728
|
if (existing) return existing;
|
|
541
729
|
const lexingResult = lexer.tokenize(declaration);
|
|
542
|
-
if (lexingResult.errors.length > 0) throw new AggregateError(lexingResult.errors
|
|
730
|
+
if (lexingResult.errors.length > 0) throw diagnostics.WPILOG_R0007({ cause: new AggregateError(lexingResult.errors) });
|
|
543
731
|
parser.input = lexingResult.tokens;
|
|
544
732
|
const cstNode = parser.structSpecification();
|
|
545
|
-
if (parser.errors.length > 0) throw new AggregateError(parser.errors
|
|
733
|
+
if (parser.errors.length > 0) throw diagnostics.WPILOG_R0008({ cause: new AggregateError(parser.errors) });
|
|
546
734
|
const created = structAstVisitor.structSpecification(cstNode.children);
|
|
547
735
|
cache.set(declaration, created);
|
|
548
736
|
return created;
|
|
@@ -550,10 +738,10 @@ function parseStructSpecification(declaration) {
|
|
|
550
738
|
//#endregion
|
|
551
739
|
//#region src/struct/struct-registry.ts
|
|
552
740
|
var StructRegistry = class StructRegistry {
|
|
553
|
-
structDecodeQueue;
|
|
554
741
|
static TEXT_DECODER = new TextDecoder("utf-8");
|
|
555
742
|
definitions = /* @__PURE__ */ new Map();
|
|
556
743
|
byteLengths = /* @__PURE__ */ new Map();
|
|
744
|
+
structDecodeQueue;
|
|
557
745
|
constructor(structDecodeQueue) {
|
|
558
746
|
this.structDecodeQueue = structDecodeQueue;
|
|
559
747
|
}
|
|
@@ -564,7 +752,7 @@ var StructRegistry = class StructRegistry {
|
|
|
564
752
|
}
|
|
565
753
|
getDefinition(name) {
|
|
566
754
|
const definition = this.definitions.get(name);
|
|
567
|
-
if (!definition) throw
|
|
755
|
+
if (!definition) throw diagnostics.WPILOG_R0013({ name });
|
|
568
756
|
return definition;
|
|
569
757
|
}
|
|
570
758
|
decodeArray(structName, payload) {
|
|
@@ -586,10 +774,18 @@ var StructRegistry = class StructRegistry {
|
|
|
586
774
|
const specification = this.getDefinition(structName);
|
|
587
775
|
const result = /* @__PURE__ */ new Map();
|
|
588
776
|
const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
777
|
+
const bitFields = new BitFieldTracker();
|
|
589
778
|
for (const member of specification) {
|
|
590
|
-
|
|
779
|
+
const memberByteLength = this.calculateByteLength(member);
|
|
780
|
+
const bitField = typeof memberByteLength === "number" ? bitFields.next(member, memberByteLength) : void 0;
|
|
781
|
+
if (bitField) {
|
|
782
|
+
offset.advance(bitField.bytesToAdvance);
|
|
783
|
+
result.set(member.name, decodeBitField(view, offset.get(), member, bitField));
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
offset.advance(bitFields.flush());
|
|
591
787
|
switch (member.value) {
|
|
592
|
-
case
|
|
788
|
+
case KnownStructTypeName.Boolean:
|
|
593
789
|
if (member.arraySize !== void 0) {
|
|
594
790
|
const array = [];
|
|
595
791
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -602,7 +798,7 @@ var StructRegistry = class StructRegistry {
|
|
|
602
798
|
offset.advance8();
|
|
603
799
|
}
|
|
604
800
|
break;
|
|
605
|
-
case
|
|
801
|
+
case KnownStructTypeName.Int8:
|
|
606
802
|
if (member.arraySize !== void 0) {
|
|
607
803
|
const array = [];
|
|
608
804
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -615,7 +811,7 @@ var StructRegistry = class StructRegistry {
|
|
|
615
811
|
offset.advance8();
|
|
616
812
|
}
|
|
617
813
|
break;
|
|
618
|
-
case
|
|
814
|
+
case KnownStructTypeName.Int16:
|
|
619
815
|
if (member.arraySize !== void 0) {
|
|
620
816
|
const array = [];
|
|
621
817
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -628,7 +824,7 @@ var StructRegistry = class StructRegistry {
|
|
|
628
824
|
offset.advance16();
|
|
629
825
|
}
|
|
630
826
|
break;
|
|
631
|
-
case
|
|
827
|
+
case KnownStructTypeName.Int32:
|
|
632
828
|
if (member.arraySize !== void 0) {
|
|
633
829
|
const array = [];
|
|
634
830
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -641,7 +837,7 @@ var StructRegistry = class StructRegistry {
|
|
|
641
837
|
offset.advance32();
|
|
642
838
|
}
|
|
643
839
|
break;
|
|
644
|
-
case
|
|
840
|
+
case KnownStructTypeName.Int64:
|
|
645
841
|
if (member.arraySize !== void 0) {
|
|
646
842
|
const array = [];
|
|
647
843
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -654,7 +850,7 @@ var StructRegistry = class StructRegistry {
|
|
|
654
850
|
offset.advance64();
|
|
655
851
|
}
|
|
656
852
|
break;
|
|
657
|
-
case
|
|
853
|
+
case KnownStructTypeName.Uint8:
|
|
658
854
|
if (member.arraySize !== void 0) {
|
|
659
855
|
const array = [];
|
|
660
856
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -667,7 +863,7 @@ var StructRegistry = class StructRegistry {
|
|
|
667
863
|
offset.advance8();
|
|
668
864
|
}
|
|
669
865
|
break;
|
|
670
|
-
case
|
|
866
|
+
case KnownStructTypeName.Uint16:
|
|
671
867
|
if (member.arraySize !== void 0) {
|
|
672
868
|
const array = [];
|
|
673
869
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -680,7 +876,7 @@ var StructRegistry = class StructRegistry {
|
|
|
680
876
|
offset.advance16();
|
|
681
877
|
}
|
|
682
878
|
break;
|
|
683
|
-
case
|
|
879
|
+
case KnownStructTypeName.Uint32:
|
|
684
880
|
if (member.arraySize !== void 0) {
|
|
685
881
|
const array = [];
|
|
686
882
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -693,7 +889,7 @@ var StructRegistry = class StructRegistry {
|
|
|
693
889
|
offset.advance32();
|
|
694
890
|
}
|
|
695
891
|
break;
|
|
696
|
-
case
|
|
892
|
+
case KnownStructTypeName.Uint64:
|
|
697
893
|
if (member.arraySize !== void 0) {
|
|
698
894
|
const array = [];
|
|
699
895
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -706,8 +902,8 @@ var StructRegistry = class StructRegistry {
|
|
|
706
902
|
offset.advance64();
|
|
707
903
|
}
|
|
708
904
|
break;
|
|
709
|
-
case
|
|
710
|
-
case
|
|
905
|
+
case KnownStructTypeName.Float32:
|
|
906
|
+
case KnownStructTypeName.Float:
|
|
711
907
|
if (member.arraySize !== void 0) {
|
|
712
908
|
const array = [];
|
|
713
909
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -720,8 +916,8 @@ var StructRegistry = class StructRegistry {
|
|
|
720
916
|
offset.advance32();
|
|
721
917
|
}
|
|
722
918
|
break;
|
|
723
|
-
case
|
|
724
|
-
case
|
|
919
|
+
case KnownStructTypeName.Float64:
|
|
920
|
+
case KnownStructTypeName.Double:
|
|
725
921
|
if (member.arraySize !== void 0) {
|
|
726
922
|
const array = [];
|
|
727
923
|
for (let i = 0; i < member.arraySize; i++) {
|
|
@@ -734,7 +930,7 @@ var StructRegistry = class StructRegistry {
|
|
|
734
930
|
offset.advance64();
|
|
735
931
|
}
|
|
736
932
|
break;
|
|
737
|
-
case
|
|
933
|
+
case KnownStructTypeName.Character:
|
|
738
934
|
if (member.arraySize !== void 0) result.set(member.name, StructRegistry.TEXT_DECODER.decode(payload.subarray(offset.get(), offset.advance(member.arraySize).get())));
|
|
739
935
|
else result.set(member.name, StructRegistry.TEXT_DECODER.decode(payload.subarray(offset.get(), offset.advance8().get())));
|
|
740
936
|
break;
|
|
@@ -753,6 +949,7 @@ var StructRegistry = class StructRegistry {
|
|
|
753
949
|
}
|
|
754
950
|
}
|
|
755
951
|
}
|
|
952
|
+
offset.advance(bitFields.flush());
|
|
756
953
|
return result;
|
|
757
954
|
}
|
|
758
955
|
getByteLength(name) {
|
|
@@ -760,42 +957,48 @@ var StructRegistry = class StructRegistry {
|
|
|
760
957
|
if (existing !== void 0) return existing;
|
|
761
958
|
const definition = this.getDefinition(name);
|
|
762
959
|
let totalByteLength = 0;
|
|
960
|
+
const bitFields = new BitFieldTracker();
|
|
763
961
|
for (const member of definition) {
|
|
764
962
|
const memberByteLengthOrBlocker = this.calculateByteLength(member);
|
|
765
963
|
if (typeof memberByteLengthOrBlocker === "string") return memberByteLengthOrBlocker;
|
|
766
|
-
|
|
964
|
+
const bitField = bitFields.next(member, memberByteLengthOrBlocker);
|
|
965
|
+
if (bitField) {
|
|
966
|
+
totalByteLength += bitField.bytesToAdvance;
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
totalByteLength += bitFields.flush() + memberByteLengthOrBlocker;
|
|
767
970
|
}
|
|
971
|
+
totalByteLength += bitFields.flush();
|
|
768
972
|
this.byteLengths.set(name, totalByteLength);
|
|
769
973
|
return totalByteLength;
|
|
770
974
|
}
|
|
771
975
|
calculateByteLength(member) {
|
|
772
|
-
if (member.bitWidth) throw new Error("Bit-field members are not implemented");
|
|
773
976
|
let byteLengthForOne = 0;
|
|
774
977
|
switch (member.value) {
|
|
775
|
-
case
|
|
776
|
-
case
|
|
777
|
-
case
|
|
778
|
-
case
|
|
978
|
+
case KnownStructTypeName.Boolean:
|
|
979
|
+
case KnownStructTypeName.Character:
|
|
980
|
+
case KnownStructTypeName.Int8:
|
|
981
|
+
case KnownStructTypeName.Uint8:
|
|
779
982
|
byteLengthForOne = 1;
|
|
780
983
|
break;
|
|
781
|
-
case
|
|
782
|
-
case
|
|
984
|
+
case KnownStructTypeName.Int16:
|
|
985
|
+
case KnownStructTypeName.Uint16:
|
|
783
986
|
byteLengthForOne = 2;
|
|
784
987
|
break;
|
|
785
|
-
case
|
|
786
|
-
case
|
|
988
|
+
case KnownStructTypeName.Int32:
|
|
989
|
+
case KnownStructTypeName.Uint32:
|
|
787
990
|
byteLengthForOne = 4;
|
|
788
991
|
break;
|
|
789
|
-
case
|
|
790
|
-
case
|
|
992
|
+
case KnownStructTypeName.Int64:
|
|
993
|
+
case KnownStructTypeName.Uint64:
|
|
791
994
|
byteLengthForOne = 8;
|
|
792
995
|
break;
|
|
793
|
-
case
|
|
794
|
-
case
|
|
996
|
+
case KnownStructTypeName.Float32:
|
|
997
|
+
case KnownStructTypeName.Float:
|
|
795
998
|
byteLengthForOne = 4;
|
|
796
999
|
break;
|
|
797
|
-
case
|
|
798
|
-
case
|
|
1000
|
+
case KnownStructTypeName.Float64:
|
|
1001
|
+
case KnownStructTypeName.Double:
|
|
799
1002
|
byteLengthForOne = 8;
|
|
800
1003
|
break;
|
|
801
1004
|
default: {
|
|
@@ -814,17 +1017,133 @@ var StructRegistry = class StructRegistry {
|
|
|
814
1017
|
const TEXT_DECODER = new TextDecoder();
|
|
815
1018
|
const STRUCT_PREFIX = "struct:";
|
|
816
1019
|
const STRUCT_ARRAY_SUFFIX = "[]";
|
|
1020
|
+
const STRUCT_SCHEMA_NAME_PREFIX = "/.schema/struct:";
|
|
817
1021
|
function byteToBoolean(byte) {
|
|
818
1022
|
switch (byte) {
|
|
819
1023
|
case 0: return false;
|
|
820
1024
|
case 1: return true;
|
|
821
|
-
default: throw
|
|
1025
|
+
default: throw diagnostics.WPILOG_R0005({ byte });
|
|
822
1026
|
}
|
|
823
1027
|
}
|
|
824
1028
|
function normalizeEntryName(rawName) {
|
|
825
1029
|
if (rawName.startsWith("/")) return rawName;
|
|
826
1030
|
return `/${rawName}`;
|
|
827
1031
|
}
|
|
1032
|
+
var RecordDecoder = class {
|
|
1033
|
+
delayedRecords = [];
|
|
1034
|
+
context = /* @__PURE__ */ new Map();
|
|
1035
|
+
recordContexts = /* @__PURE__ */ new WeakMap();
|
|
1036
|
+
structDecodeQueue;
|
|
1037
|
+
structRegistry;
|
|
1038
|
+
options;
|
|
1039
|
+
constructor(options) {
|
|
1040
|
+
this.options = options;
|
|
1041
|
+
this.structDecodeQueue = new StructDecodeQueue((_structName, queuedRecords) => {
|
|
1042
|
+
for (const raw of queuedRecords) {
|
|
1043
|
+
const context = this.recordContexts.get(raw);
|
|
1044
|
+
if (!context) continue;
|
|
1045
|
+
const { entryType, name, metadata } = context;
|
|
1046
|
+
const structName = entryType.slice(7);
|
|
1047
|
+
const base = {
|
|
1048
|
+
entryId: raw.entryId,
|
|
1049
|
+
timestamp: raw.timestamp,
|
|
1050
|
+
name,
|
|
1051
|
+
metadata,
|
|
1052
|
+
structName
|
|
1053
|
+
};
|
|
1054
|
+
if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
|
|
1055
|
+
const payload = this.structRegistry.decodeArray(structName, raw.payload);
|
|
1056
|
+
if (typeof payload !== "string") this.delayedRecords.push({
|
|
1057
|
+
...base,
|
|
1058
|
+
type: RecordType.StructArray,
|
|
1059
|
+
payload
|
|
1060
|
+
});
|
|
1061
|
+
} else {
|
|
1062
|
+
const payload = this.structRegistry.decode(structName, raw.payload);
|
|
1063
|
+
if (typeof payload !== "string") this.delayedRecords.push({
|
|
1064
|
+
...base,
|
|
1065
|
+
type: RecordType.Struct,
|
|
1066
|
+
payload
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1071
|
+
this.structRegistry = new StructRegistry(this.structDecodeQueue);
|
|
1072
|
+
}
|
|
1073
|
+
decodeControl(entryId, timestamp, payload) {
|
|
1074
|
+
switch (payload.controlRecordType) {
|
|
1075
|
+
case ControlRecordType.Start:
|
|
1076
|
+
this.context.set(payload.entryId, {
|
|
1077
|
+
name: normalizeEntryName(payload.entryName),
|
|
1078
|
+
entryType: payload.entryType,
|
|
1079
|
+
metadata: payload.entryMetadata
|
|
1080
|
+
});
|
|
1081
|
+
break;
|
|
1082
|
+
case ControlRecordType.Finish:
|
|
1083
|
+
this.context.delete(payload.entryId);
|
|
1084
|
+
break;
|
|
1085
|
+
case ControlRecordType.SetMetadata: {
|
|
1086
|
+
const existing = this.context.get(payload.entryId);
|
|
1087
|
+
if (existing) this.context.set(payload.entryId, {
|
|
1088
|
+
...existing,
|
|
1089
|
+
metadata: payload.entryMetadata
|
|
1090
|
+
});
|
|
1091
|
+
break;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
return {
|
|
1095
|
+
entryId,
|
|
1096
|
+
timestamp,
|
|
1097
|
+
type: RecordType.Control,
|
|
1098
|
+
payload
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
decodeData(entryId, timestamp, bytes, view, payloadOffset, payloadSize) {
|
|
1102
|
+
const context = this.context.get(entryId);
|
|
1103
|
+
if (!context) {
|
|
1104
|
+
if (this.options.strict) throw diagnostics.WPILOG_R0006({ entryId });
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
const base = {
|
|
1108
|
+
entryId,
|
|
1109
|
+
timestamp,
|
|
1110
|
+
name: context.name,
|
|
1111
|
+
metadata: context.metadata
|
|
1112
|
+
};
|
|
1113
|
+
switch (context.entryType) {
|
|
1114
|
+
case "boolean": return {
|
|
1115
|
+
...base,
|
|
1116
|
+
type: RecordType.Boolean,
|
|
1117
|
+
payload: byteToBoolean(view.getUint8(payloadOffset))
|
|
1118
|
+
};
|
|
1119
|
+
case "int64": return {
|
|
1120
|
+
...base,
|
|
1121
|
+
type: RecordType.Int64,
|
|
1122
|
+
payload: view.getBigInt64(payloadOffset, true)
|
|
1123
|
+
};
|
|
1124
|
+
case "float": return {
|
|
1125
|
+
...base,
|
|
1126
|
+
type: RecordType.Float,
|
|
1127
|
+
payload: view.getFloat32(payloadOffset, true)
|
|
1128
|
+
};
|
|
1129
|
+
case "double": return {
|
|
1130
|
+
...base,
|
|
1131
|
+
type: RecordType.Double,
|
|
1132
|
+
payload: view.getFloat64(payloadOffset, true)
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
const payload = payloadOffset === 0 && payloadSize === bytes.byteLength ? bytes : bytes.subarray(payloadOffset, payloadOffset + payloadSize);
|
|
1136
|
+
const raw = {
|
|
1137
|
+
entryId,
|
|
1138
|
+
timestamp,
|
|
1139
|
+
payload
|
|
1140
|
+
};
|
|
1141
|
+
const payloadView = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
1142
|
+
const decoded = decodeNonScalarPayload(raw, context.entryType, context.name, context.metadata, payloadView, this.structRegistry, this.structDecodeQueue);
|
|
1143
|
+
if (!decoded) this.recordContexts.set(raw, context);
|
|
1144
|
+
return decoded;
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
828
1147
|
/**
|
|
829
1148
|
* Decode raw WPILOG records into typed values.
|
|
830
1149
|
*
|
|
@@ -835,77 +1154,51 @@ function normalizeEntryName(rawName) {
|
|
|
835
1154
|
* control record are silently skipped. Pass `{ strict: true }` to throw instead.
|
|
836
1155
|
*/
|
|
837
1156
|
function* decodeRecords(records, options = { strict: false }) {
|
|
838
|
-
const
|
|
839
|
-
const structDecodeQueue = new StructDecodeQueue((structName, queuedRecords) => {
|
|
840
|
-
for (const raw of queuedRecords) {
|
|
841
|
-
const ctx = context.get(raw.entryId);
|
|
842
|
-
if (!ctx) continue;
|
|
843
|
-
const entryType = ctx.entryType;
|
|
844
|
-
const name = normalizeEntryName(ctx.entryName);
|
|
845
|
-
const metadata = ctx.entryMetadata;
|
|
846
|
-
if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
|
|
847
|
-
const normalizedName = entryType.slice(7);
|
|
848
|
-
const decoded = structRegistry.decodeArray(normalizedName, raw.payload);
|
|
849
|
-
if (typeof decoded !== "string") asyncDecodedStructs.push({
|
|
850
|
-
entryId: raw.entryId,
|
|
851
|
-
timestamp: raw.timestamp,
|
|
852
|
-
name,
|
|
853
|
-
metadata,
|
|
854
|
-
type: "struct[]",
|
|
855
|
-
structName: normalizedName,
|
|
856
|
-
payload: decoded
|
|
857
|
-
});
|
|
858
|
-
} else {
|
|
859
|
-
const normalizedName = entryType.slice(7);
|
|
860
|
-
const decoded = structRegistry.decode(normalizedName, raw.payload);
|
|
861
|
-
if (typeof decoded !== "string") asyncDecodedStructs.push({
|
|
862
|
-
entryId: raw.entryId,
|
|
863
|
-
timestamp: raw.timestamp,
|
|
864
|
-
name,
|
|
865
|
-
metadata,
|
|
866
|
-
type: "struct",
|
|
867
|
-
structName: normalizedName,
|
|
868
|
-
payload: decoded
|
|
869
|
-
});
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
});
|
|
873
|
-
const structRegistry = new StructRegistry(structDecodeQueue);
|
|
874
|
-
const context = /* @__PURE__ */ new Map();
|
|
1157
|
+
const decoder = new RecordDecoder(options);
|
|
875
1158
|
for (const readRecord of records) {
|
|
876
1159
|
if (readRecord.kind === "header") continue;
|
|
877
1160
|
if (readRecord.kind === "control") {
|
|
878
|
-
|
|
879
|
-
switch (controlPayload.controlRecordType) {
|
|
880
|
-
case 0:
|
|
881
|
-
context.set(controlPayload.entryId, controlPayload);
|
|
882
|
-
break;
|
|
883
|
-
case 1: context.delete(controlPayload.entryId);
|
|
884
|
-
}
|
|
885
|
-
yield {
|
|
886
|
-
entryId: readRecord.entryId,
|
|
887
|
-
timestamp: readRecord.timestamp,
|
|
888
|
-
type: "control",
|
|
889
|
-
payload: controlPayload
|
|
890
|
-
};
|
|
1161
|
+
yield decoder.decodeControl(readRecord.entryId, readRecord.timestamp, readRecord.payload);
|
|
891
1162
|
continue;
|
|
892
1163
|
}
|
|
893
1164
|
const raw = readRecord.record;
|
|
894
|
-
const
|
|
895
|
-
|
|
896
|
-
|
|
1165
|
+
const view = new DataView(raw.payload.buffer, raw.payload.byteOffset, raw.payload.byteLength);
|
|
1166
|
+
const decoded = decoder.decodeData(raw.entryId, raw.timestamp, raw.payload, view, 0, raw.payload.byteLength);
|
|
1167
|
+
if (decoded) yield decoded;
|
|
1168
|
+
if (decoder.delayedRecords.length > 0) {
|
|
1169
|
+
yield* decoder.delayedRecords;
|
|
1170
|
+
decoder.delayedRecords.length = 0;
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* Read and decode a WPILOG buffer in a single pass.
|
|
1176
|
+
*
|
|
1177
|
+
* This is the preferred API for fully decoded records. It avoids allocating the
|
|
1178
|
+
* intermediate records produced by composing {@link readRecords} and
|
|
1179
|
+
* {@link decodeRecords} while preserving the same output and strict-mode behavior.
|
|
1180
|
+
*/
|
|
1181
|
+
function* parseDataLog(input, options = { strict: false }) {
|
|
1182
|
+
const cursor = new RecordCursor(input);
|
|
1183
|
+
const decoder = new RecordDecoder(options);
|
|
1184
|
+
while (cursor.next()) {
|
|
1185
|
+
if (cursor.entryId === 0) {
|
|
1186
|
+
yield decoder.decodeControl(cursor.entryId, cursor.timestamp, readControlRecordPayload(cursor.getPayload()));
|
|
897
1187
|
continue;
|
|
898
1188
|
}
|
|
899
|
-
const
|
|
900
|
-
const metadata = recordContext.entryMetadata;
|
|
901
|
-
const view = new DataView(raw.payload.buffer, raw.payload.byteOffset, raw.payload.byteLength);
|
|
902
|
-
const decoded = decodePayload(raw, recordContext.entryType, name, metadata, view, structRegistry, structDecodeQueue);
|
|
1189
|
+
const decoded = decoder.decodeData(cursor.entryId, cursor.timestamp, cursor.bytes, cursor.view, cursor.payloadOffset, cursor.payloadSize);
|
|
903
1190
|
if (decoded) yield decoded;
|
|
904
|
-
|
|
905
|
-
|
|
1191
|
+
if (decoder.delayedRecords.length > 0) {
|
|
1192
|
+
yield* decoder.delayedRecords;
|
|
1193
|
+
decoder.delayedRecords.length = 0;
|
|
1194
|
+
}
|
|
906
1195
|
}
|
|
907
1196
|
}
|
|
908
|
-
|
|
1197
|
+
/**
|
|
1198
|
+
* Decode non-scalar payloads that require a byte slice or payload-local view.
|
|
1199
|
+
* Fixed-width scalar types return from {@link RecordDecoder#decodeData} before this function so their payloads can be read directly from the input buffer.
|
|
1200
|
+
*/
|
|
1201
|
+
function decodeNonScalarPayload(raw, entryType, name, metadata, view, structRegistry, structDecodeQueue) {
|
|
909
1202
|
const base = {
|
|
910
1203
|
entryId: raw.entryId,
|
|
911
1204
|
timestamp: raw.timestamp,
|
|
@@ -913,29 +1206,9 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
|
|
|
913
1206
|
metadata
|
|
914
1207
|
};
|
|
915
1208
|
switch (entryType) {
|
|
916
|
-
case "boolean": return {
|
|
917
|
-
...base,
|
|
918
|
-
type: "boolean",
|
|
919
|
-
payload: byteToBoolean(view.getUint8(0))
|
|
920
|
-
};
|
|
921
|
-
case "int64": return {
|
|
922
|
-
...base,
|
|
923
|
-
type: "int64",
|
|
924
|
-
payload: view.getBigInt64(0, true)
|
|
925
|
-
};
|
|
926
|
-
case "float": return {
|
|
927
|
-
...base,
|
|
928
|
-
type: "float",
|
|
929
|
-
payload: view.getFloat32(0, true)
|
|
930
|
-
};
|
|
931
|
-
case "double": return {
|
|
932
|
-
...base,
|
|
933
|
-
type: "double",
|
|
934
|
-
payload: view.getFloat64(0, true)
|
|
935
|
-
};
|
|
936
1209
|
case "string": return {
|
|
937
1210
|
...base,
|
|
938
|
-
type:
|
|
1211
|
+
type: RecordType.String,
|
|
939
1212
|
payload: TEXT_DECODER.decode(raw.payload)
|
|
940
1213
|
};
|
|
941
1214
|
case "boolean[]": {
|
|
@@ -943,7 +1216,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
|
|
|
943
1216
|
for (let i = 0; i < raw.payload.byteLength; i++) payload.push(byteToBoolean(view.getUint8(i)));
|
|
944
1217
|
return {
|
|
945
1218
|
...base,
|
|
946
|
-
type:
|
|
1219
|
+
type: RecordType.BooleanArray,
|
|
947
1220
|
payload
|
|
948
1221
|
};
|
|
949
1222
|
}
|
|
@@ -952,7 +1225,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
|
|
|
952
1225
|
for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getBigUint64(i, true));
|
|
953
1226
|
return {
|
|
954
1227
|
...base,
|
|
955
|
-
type:
|
|
1228
|
+
type: RecordType.Int64Array,
|
|
956
1229
|
payload
|
|
957
1230
|
};
|
|
958
1231
|
}
|
|
@@ -961,7 +1234,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
|
|
|
961
1234
|
for (let i = 0; i < raw.payload.byteLength; i += 4) payload.push(view.getFloat32(i, true));
|
|
962
1235
|
return {
|
|
963
1236
|
...base,
|
|
964
|
-
type:
|
|
1237
|
+
type: RecordType.FloatArray,
|
|
965
1238
|
payload
|
|
966
1239
|
};
|
|
967
1240
|
}
|
|
@@ -970,7 +1243,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
|
|
|
970
1243
|
for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getFloat64(i, true));
|
|
971
1244
|
return {
|
|
972
1245
|
...base,
|
|
973
|
-
type:
|
|
1246
|
+
type: RecordType.DoubleArray,
|
|
974
1247
|
payload
|
|
975
1248
|
};
|
|
976
1249
|
}
|
|
@@ -988,52 +1261,51 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
|
|
|
988
1261
|
}
|
|
989
1262
|
return {
|
|
990
1263
|
...base,
|
|
991
|
-
type:
|
|
1264
|
+
type: RecordType.StringArray,
|
|
992
1265
|
payload
|
|
993
1266
|
};
|
|
994
1267
|
}
|
|
995
1268
|
case "structschema": {
|
|
996
|
-
const structName = name.slice(16);
|
|
1269
|
+
const structName = name.slice(name.indexOf(STRUCT_SCHEMA_NAME_PREFIX) + 16);
|
|
997
1270
|
const payload = TEXT_DECODER.decode(raw.payload);
|
|
998
1271
|
structRegistry.register(structName, payload);
|
|
999
1272
|
return {
|
|
1000
1273
|
...base,
|
|
1001
|
-
type:
|
|
1274
|
+
type: RecordType.String,
|
|
1002
1275
|
payload
|
|
1003
1276
|
};
|
|
1004
1277
|
}
|
|
1005
1278
|
default:
|
|
1006
1279
|
if (entryType.startsWith(STRUCT_PREFIX)) {
|
|
1280
|
+
const structName = entryType.slice(7);
|
|
1007
1281
|
if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
|
|
1008
|
-
const
|
|
1009
|
-
const decoded = structRegistry.decodeArray(normalizedStructName, raw.payload);
|
|
1282
|
+
const decoded = structRegistry.decodeArray(structName, raw.payload);
|
|
1010
1283
|
if (typeof decoded === "string") {
|
|
1011
1284
|
structDecodeQueue.queueStructRecord(decoded, raw);
|
|
1012
1285
|
return;
|
|
1013
1286
|
}
|
|
1014
1287
|
return {
|
|
1015
1288
|
...base,
|
|
1016
|
-
type:
|
|
1017
|
-
structName
|
|
1289
|
+
type: RecordType.StructArray,
|
|
1290
|
+
structName,
|
|
1018
1291
|
payload: decoded
|
|
1019
1292
|
};
|
|
1020
1293
|
}
|
|
1021
|
-
const
|
|
1022
|
-
const decoded = structRegistry.decode(normalizedStructName, raw.payload);
|
|
1294
|
+
const decoded = structRegistry.decode(structName, raw.payload);
|
|
1023
1295
|
if (typeof decoded === "string") {
|
|
1024
1296
|
structDecodeQueue.queueStructRecord(decoded, raw);
|
|
1025
1297
|
return;
|
|
1026
1298
|
}
|
|
1027
1299
|
return {
|
|
1028
1300
|
...base,
|
|
1029
|
-
type:
|
|
1030
|
-
structName
|
|
1301
|
+
type: RecordType.Struct,
|
|
1302
|
+
structName,
|
|
1031
1303
|
payload: decoded
|
|
1032
1304
|
};
|
|
1033
1305
|
}
|
|
1034
1306
|
return {
|
|
1035
1307
|
...base,
|
|
1036
|
-
type:
|
|
1308
|
+
type: RecordType.Raw,
|
|
1037
1309
|
payload: raw.payload
|
|
1038
1310
|
};
|
|
1039
1311
|
}
|
|
@@ -1045,13 +1317,13 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
|
|
|
1045
1317
|
*
|
|
1046
1318
|
* Iterates through records, collecting Start control records.
|
|
1047
1319
|
* Data record bytes are still read from the stream but not decoded,
|
|
1048
|
-
* making this much cheaper than full decoding via {@link
|
|
1320
|
+
* making this much cheaper than full decoding via {@link parseDataLog}.
|
|
1049
1321
|
*/
|
|
1050
1322
|
function* catalogEntries(records) {
|
|
1051
1323
|
const entries = /* @__PURE__ */ new Map();
|
|
1052
1324
|
for (const record of records) {
|
|
1053
1325
|
if (record.kind !== "control") continue;
|
|
1054
|
-
if (record.payload.controlRecordType ===
|
|
1326
|
+
if (record.payload.controlRecordType === ControlRecordType.Start) {
|
|
1055
1327
|
const entry = {
|
|
1056
1328
|
entryId: record.payload.entryId,
|
|
1057
1329
|
name: normalizeEntryName(record.payload.entryName),
|
|
@@ -1061,11 +1333,11 @@ function* catalogEntries(records) {
|
|
|
1061
1333
|
entries.set(record.payload.entryId, entry);
|
|
1062
1334
|
yield entry;
|
|
1063
1335
|
}
|
|
1064
|
-
if (record.payload.controlRecordType ===
|
|
1336
|
+
if (record.payload.controlRecordType === ControlRecordType.SetMetadata) {
|
|
1065
1337
|
const existing = entries.get(record.payload.entryId);
|
|
1066
1338
|
if (existing) existing.metadata = record.payload.entryMetadata;
|
|
1067
1339
|
}
|
|
1068
1340
|
}
|
|
1069
1341
|
}
|
|
1070
1342
|
//#endregion
|
|
1071
|
-
export {
|
|
1343
|
+
export { KnownStructTypeName as a, RecordType as c, parseStructSpecification as i, isDataRecord as l, decodeRecords as n, readRecords as o, parseDataLog as r, ControlRecordType as s, catalogEntries as t, diagnostics as u };
|