Skip to content
Open
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
4 changes: 4 additions & 0 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ export class Parser {
this.sanitizeFieldName(alias);
aliasRegistry.set(alias, this);
this.alias = alias;
this.compiled = undefined;
return this;
}

Expand Down Expand Up @@ -963,6 +964,7 @@ export class Parser {

useContextVars(useContextVariables = true): this {
this.useContextVariables = useContextVariables;
this.compiled = undefined;

return this;
}
Expand All @@ -973,6 +975,7 @@ export class Parser {
}

this.constructorFn = constructorFn;
this.compiled = undefined;

return this;
}
Expand Down Expand Up @@ -1143,6 +1146,7 @@ export class Parser {
this.next = parser;
}
this.head = parser;
this.compiled = undefined;

return this;
}
Expand Down
42 changes: 42 additions & 0 deletions test/compiled_mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Parser } from "../lib/binary_parser";

describe("changes after compilation", () => {
for (const explicit of [false, true]) {
it(`includes fields appended after ${explicit ? "compile" : "parse"}`, () => {
const parser = new Parser().uint8("first");
const input = new Uint8Array([1, 2]);
if (explicit) parser.compile();
else expect(parser.parse(input)).toStrictEqual({ first: 1 });
parser.uint8("second");
expect(parser.parse(input)).toStrictEqual({ first: 1, second: 2 });
});
}

it("uses a constructor selected after the first parse", () => {
class Result {
kind = "custom";
}
const parser = new Parser().uint8("value");
parser.parse(new Uint8Array([1]));
parser.create(Result);
const result = parser.parse(new Uint8Array([2]));
expect(result).toBeInstanceOf(Result);
expect(result.kind).toBe("custom");
expect(result.value).toBe(2);
});

it("honors context toggles after earlier parses", () => {
const child = new Parser().uint8("value", {
formatter(this: any) {
return this.$parent !== undefined;
},
});
const parser = new Parser().nest("child", { type: child });
const input = new Uint8Array([1]);
expect(parser.parse(input).child.value).toBe(false);
parser.useContextVars();
expect(parser.parse(input).child.value).toBe(true);
parser.useContextVars(false);
expect(parser.parse(input).child.value).toBe(false);
});
});