Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,17 @@ export class Parser {
return this;
}

// Call code generator for this parser
// Generate consecutive fields without consuming one stack frame per field.
private generate(ctx: Context) {
let parser: Parser | undefined = this;
while (parser) {
parser.generateField(ctx);
parser = parser.next;
}
return ctx;
}

private generateField(ctx: Context) {
if (this.type) {
switch (this.type) {
case "uint8":
Expand Down Expand Up @@ -1036,8 +1045,6 @@ export class Parser {
if (this.options.formatter && this.type !== "bit") {
this.generateFormatter(ctx, varName, this.options.formatter);
}

return this.generateNext(ctx);
}

private generateAssert(ctx: Context) {
Expand Down Expand Up @@ -1075,15 +1082,6 @@ export class Parser {
ctx.pushCode("}");
}

// Recursively call code generators and append results
private generateNext(ctx: Context): Context {
if (this.next) {
ctx = this.next.generate(ctx);
}

return ctx;
}

private nextNotBit() {
// Used to test if next type is a bitN or not
if (this.next) {
Expand All @@ -1109,12 +1107,12 @@ export class Parser {
}

private generateBit(ctx: Context) {
// TODO find better method to handle nested bit fields
const parser = JSON.parse(JSON.stringify(this));
// Retain only this field's metadata, not the rest of the parser chain.
const parser = new Parser();
parser.options = this.options;
parser.generateAssert = this.generateAssert.bind(this);
parser.generateFormatter = this.generateFormatter.bind(this);
parser.varName = ctx.generateVariable(parser.varName);
parser.varName = ctx.generateVariable(this.varName);
ctx.bitFields.push(parser);

if (!this.next || this.nextNotBit()) {
Expand Down
61 changes: 61 additions & 0 deletions test/primitive_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,67 @@ function primitiveParserTests(
expect(parser.parse(buf)).toStrictEqual({ a: 0xa, b: 0xbc, c: 0xd });
expect(options).toStrictEqual({});
});

for (const endianness of ["big", "little"] as const) {
it(`should compile thousands of ${endianness}-endian bit fields`, () => {
const parser = new Parser().endianness(endianness);
const expected: Record<string, number> = {};
const bytes = [];
for (let i = 0; i < 6000; ++i) {
parser.bit1(`flag${i}`);
expected[`flag${i}`] = i % 2;
if (i % 8 === 0) bytes.push(endianness === "big" ? 0x55 : 0xaa);
}
expect(parser.parse(factory(bytes))).toStrictEqual(expected);
});
}

it("should compile a large bit schema followed by other field types", () => {
const parser = new Parser();
const expected: Record<string, unknown> = {};
for (let i = 0; i < 1200; ++i) {
parser.bit1(`flag${i}`, {
assert: (value) => value === i % 2,
formatter: (value) => value !== 0,
});
expected[`flag${i}`] = i % 2 !== 0;
}
for (let i = 0; i < 50; ++i) {
parser.uint8(`byte${i}`);
expected[`byte${i}`] = i;
}
for (let i = 0; i < 20; ++i) {
parser.string(`text${i}`, { length: 1 });
expected[`text${i}`] = String.fromCharCode(65 + i);
}
const bytes = [
...new Array(150).fill(0x55),
...Array.from({ length: 50 }, (_, i) => i),
...Array.from({ length: 20 }, (_, i) => 65 + i),
];
expect(parser.parse(factory(bytes))).toStrictEqual(expected);
bytes[0] = 0;
expect(() => parser.parse(factory(bytes))).toThrow("Assertion error");
});

it("should preserve reusable nested bit parsers and callbacks", () => {
const nibble = new Parser().bit4("value", {
assert: (value) => value > 0,
formatter: (value) => value * 10,
});
const parser = new Parser()
.nest("first", { type: nibble })
.nest("second", { type: nibble });
expect(parser.parse(factory([0xa0, 0xb0]))).toStrictEqual({
first: { value: 100 },
second: { value: 110 },
});
expect(nibble.parse(factory([0xc0]))).toStrictEqual({ value: 120 });
expect(parser.parse(factory([0xd0, 0xe0]))).toStrictEqual({
first: { value: 130 },
second: { value: 140 },
});
});
});

describe("String parser", () => {
Expand Down
Loading