Skip to content

Fix assert/formatter scope for bit fields spanning into a nest - #323

Open
keichi wants to merge 2 commits into
masterfrom
fix-bit-assert-scope
Open

keichi wants to merge 2 commits into
masterfrom
fix-bit-assert-scope

Conversation

@keichi

@keichi keichi commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Problem

Bit fields are deferred until the whole bit group is read. When the group spans into a nest (whose first field is a bit field), the deferred fields are flushed inside the nest's scope. generateAssert and generateFormatter resolved the variable path / this with ctx.generateVariable() at flush time, so they referenced the nested scope instead of the field's own scope.

Repro

new Parser()
  .bit4("x", { assert: 0xa })
  .nest("n", { type: new Parser().bit4("y") })
  .parse(Buffer.from([0xab]));
// Error: Assertion error: vars.n.x is 10

A formatter on x would likewise be called with this bound to vars.n instead of vars.

Fix

generateAssert takes an optional pre-resolved varName and generateFormatter an optional thisVar. generateBit captures both at deferral time and passes them when flushing. Added a regression test.

@keichi
keichi force-pushed the fix-bit-assert-scope branch from 790c132 to 69a321f Compare October 18, 2026 13:55
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