Skip to content

Commit 3f236b6

Browse files
authored
Merge pull request #168 from Rzial/parsing-context-fix
Added parsing context to `.readUntil()` and `.formatter()`
2 parents c211c1f + ee6f6a4 commit 3f236b6

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/binary_parser.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface ParserOptions {
1111
type?: string | Parser;
1212
formatter?: (item: any) => string | number;
1313
encoding?: string;
14-
readUntil?: 'eof';
14+
readUntil?: 'eof' | ((item: any, buffer: Buffer) => boolean);
1515
greedy?: boolean;
1616
choices?: { [key: number]: string | Parser };
1717
defaultChoice?: string | Parser;
@@ -1029,7 +1029,7 @@ export class Parser {
10291029
ctx.pushCode(`${cur} = dataView.getUint8(offset);`);
10301030
const func = ctx.addImport(pred);
10311031
ctx.pushCode(
1032-
`if (${func}.call(this, ${cur}, buffer.subarray(offset))) break;`
1032+
`if (${func}.call(${ctx.generateVariable()}, ${cur}, buffer.subarray(offset))) break;`
10331033
);
10341034
ctx.pushCode(`offset += 1;`);
10351035
ctx.pushCode(`}`);
@@ -1115,7 +1115,7 @@ export class Parser {
11151115
const pred = this.options.readUntil;
11161116
const func = ctx.addImport(pred);
11171117
ctx.pushCode(
1118-
`while (!${func}.call(this, ${item}, buffer.subarray(offset)));`
1118+
`while (!${func}.call(${ctx.generateVariable()}, ${item}, buffer.subarray(offset)));`
11191119
);
11201120
}
11211121
}
@@ -1207,7 +1207,7 @@ export class Parser {
12071207
ctx.pushCode(`${cur} = dataView.getUint8(offset);`);
12081208
const func = ctx.addImport(pred);
12091209
ctx.pushCode(
1210-
`if (${func}.call(this, ${cur}, buffer.subarray(offset))) break;`
1210+
`if (${func}.call(${ctx.generateVariable()}, ${cur}, buffer.subarray(offset))) break;`
12111211
);
12121212
ctx.pushCode(`offset += 1;`);
12131213
ctx.pushCode(`}`);
@@ -1266,7 +1266,9 @@ export class Parser {
12661266
) {
12671267
if (typeof formatter === 'function') {
12681268
const func = ctx.addImport(formatter);
1269-
ctx.pushCode(`${varName} = ${func}.call(this, ${varName});`);
1269+
ctx.pushCode(
1270+
`${varName} = ${func}.call(${ctx.generateVariable()}, ${varName});`
1271+
);
12701272
}
12711273
}
12721274

0 commit comments

Comments
 (0)