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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ These options can be used in all parsers.
result will be compared with it with `===` (strict equality check), and an
exception is thrown if they mismatch. On the other hand, if `assert` is a
function, that function is executed with one argument (the parsed result)
and if it returns false, an exception is thrown.
and if it returns false, an exception is thrown. Inside the function, `this`
is the object that the field belongs to (the same as in `formatter`).

```javascript
// simple magic number validation
Expand Down
10 changes: 6 additions & 4 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@ export class Parser {
private generateAssert(
ctx: Context,
varName = ctx.generateVariable(this.varName),
thisVar = ctx.generateVariable(),
) {
if (this.options.assert == null) {
return;
Expand All @@ -1239,7 +1240,7 @@ export class Parser {
case "function":
{
const func = ctx.addImport(this.options.assert);
ctx.pushCode(`if (!${func}.call(vars, ${varName})) {`);
ctx.pushCode(`if (!${func}.call(${thisVar}, ${varName})) {`);
}
break;
case "number":
Expand Down Expand Up @@ -1291,10 +1292,11 @@ export class Parser {
// 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);
// Bit fields may be flushed inside a nested scope, so bind the formatter's
// `this` to the scope the field was declared in.
// Bit fields may be flushed inside a nested scope, so bind the assert's and
// formatter's `this` to the scope the field was declared in.
const thisVar = ctx.generateVariable();
parser.generateAssert = (c: Context, v?: string) =>
this.generateAssert(c, v, thisVar);
parser.generateFormatter = (c: Context, v: string, f: Function) =>
this.generateFormatter(c, v, f, thisVar);
parser.varName = ctx.generateVariable(this.varName);
Expand Down
36 changes: 36 additions & 0 deletions test/composite_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,42 @@ function compositeParserTests(
parser3.parse(buffer6);
}).toThrow();
});

it("should call assert with the enclosing object as this", () => {
const sumOfAB = {
assert(this: { a: number; b: number }, x: number | string) {
return this.a + this.b === x;
},
};
const item = new Parser().uint8("a").uint8("b").uint8("c", sumOfAB);
const bits = new Parser().bit4("a").bit2("b").bit2("c", sumOfAB);

const nested = new Parser().nest("n", { type: item });
expect(nested.parse(factory([1, 2, 3]))).toStrictEqual({
n: { a: 1, b: 2, c: 3 },
});
expect(() => nested.parse(factory([1, 2, 4]))).toThrow(
"Assertion error",
);

const array = new Parser().array("items", { type: item, length: 2 });
expect(array.parse(factory([1, 2, 3, 4, 5, 9]))).toStrictEqual({
items: [
{ a: 1, b: 2, c: 3 },
{ a: 4, b: 5, c: 9 },
],
});
expect(() => array.parse(factory([1, 2, 3, 4, 5, 6]))).toThrow(
"Assertion error",
);

// a=1, b=2, c=3
const bitNest = new Parser().nest("n", { type: bits });
expect(bitNest.parse(factory([0x1b]))).toStrictEqual({
n: { a: 1, b: 2, c: 3 },
});
expect(() => bitNest.parse(factory([0x1a]))).toThrow("Assertion error");
});
});

describe("Parse other fields after bit", () => {
Expand Down