Skip to content

Fix sizeOf() for bit fields, seek, aliases, pointer and saveOffset - #326

Open
keichi wants to merge 1 commit into
masterfrom
fix-sizeof
Open

keichi wants to merge 1 commit into
masterfrom
fix-sizeof

Conversation

@keichi

@keichi keichi commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Problem

sizeOf() returned wrong results for several fixed-size parsers:

new Parser().bit4("a").bit4("b").sizeOf();          // NaN (expected 1)
new Parser().seek("x").uint8("a").sizeOf();         // "0x1" (string concatenation)
new Parser().nest("n", { type: "A1" }).sizeOf();    // TypeError (alias type)
new Parser().array("a", { type: "A1", length: 2 }).sizeOf(); // NaN (alias type)
new Parser().saveOffset("o").uint8("a").sizeOf();   // NaN (expected 1)

Fix

  • Bit fields: consecutive bit fields are summed and converted to bytes with the same grouping as generateBit (groups up to 32 bits, reads of 8/16/24/32 bits).
  • seek: only counted when length is a number, otherwise NaN.
  • nest / array: alias string types are resolved via the alias registry.
  • pointer and saveOffset contribute 0 bytes.

Tests

Added regression tests in test/composite_parser.ts; the bit field test checks sizeOf() against the actual parse offset (saveOffset) for several bit layouts.

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