Skip to content

Call deferred bit field formatters with their declaring scope as this - #343

Merged
keichi merged 1 commit into
masterfrom
fix-bit-formatter-context
Nov 22, 2026
Merged

keichi merged 1 commit into
masterfrom
fix-bit-formatter-context

Conversation

@keichi

@keichi keichi commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Same class of bug as #341, for formatters. Bit fields are queued and flushed together; when the run continues into a nest, the flush happens inside the nested scope, and generateFormatter resolved this via ctx.generateVariable() at flush time. The outer field's formatter was therefore called with the nested object as this:

new Parser()
  .uint8('a')
  .bit4('x', { formatter: function (x) { return this.a + x; } })
  .nest('n', { type: new Parser().bit4('y') })
  .parse(Uint8Array.of(10, 0x35));
// master: { a: 10, x: NaN, n: { y: 5 } }  (this === vars.n)
// fixed:  { a: 10, x: 13,  n: { y: 5 } }

generateFormatter now accepts the this variable, and generateBit captures the scope when the field is queued. Non-bit callers are unchanged.

Adds a regression test that fails on master. Independent of #341 (no conflicts).

@keichi
keichi merged commit cd9e70b into master Nov 22, 2026
6 checks passed
@keichi
keichi deleted the fix-bit-formatter-context branch November 22, 2026 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant