diff --git a/README.md b/README.md index dc8e9ad..284f6e4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/binary_parser.ts b/lib/binary_parser.ts index 230d16b..7ddb24b 100644 --- a/lib/binary_parser.ts +++ b/lib/binary_parser.ts @@ -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; @@ -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": @@ -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); diff --git a/test/composite_parser.ts b/test/composite_parser.ts index 08d4738..a0f6856 100644 --- a/test/composite_parser.ts +++ b/test/composite_parser.ts @@ -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", () => {