Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bit field formatter #29

Closed
Pumpuli opened this issue Jan 27, 2016 · 3 comments · Fixed by #203
Closed

Bit field formatter #29

Pumpuli opened this issue Jan 27, 2016 · 3 comments · Fixed by #203

Comments

@Pumpuli
Copy link

Pumpuli commented Jan 27, 2016

You can't use formatters with bitfields other than the last one in a sequence, since the bitfield reading code generation gets deferred until the last one, but the formatter code is inserted after each field in a sequence. Thus all the other bit field values get overwritten when the actual reading and bit shifting code is executed, because the formatters were called earlier.

EDIT:

It's been almost five years, but I thought I'd add a concrete example. 😄 I'm using binary-parser@1.6.2 for these.

Here's a simple parser that parses uint8 values, and uses formatter for those (although these formatters just return a constant string).

new Parser()
    .uint8('a', { formatter: () => 'first' })
    .uint8('b', { formatter: () => 'second' })
    .uint8('c', { formatter: () => 'third' });

The generated code is fine. First we read the value to the vars and then we immediately replace that value with the formatted version.

vars.a = dataView.getUint8(offset, false);
offset += 1;
vars.a = (() => 'first').call(this, vars.a);

vars.b = dataView.getUint8(offset, false);
offset += 1;
vars.b = (() => 'second').call(this, vars.b);

vars.c = dataView.getUint8(offset, false);
offset += 1;
vars.c = (() => 'third').call(this, vars.b);

Here's the same thing, except we're parsing bitfields. We're still using formatters.

new Parser()
    .bit8('a', { formatter: () => 'first' })
    .bit8('b', { formatter: () => 'second' })
    .bit8('c', { formatter: () => 'third' });

Now there's a bug in the generated code:

vars.a = (() => 'first').call(this, vars.a);  // Formatter called with vars.a being undefined
vars.b = (() => 'second').call(this, vars.b); // Same with vars.b
var $tmp1 = dataView.getUint16(offset);
var $tmp2 = dataView.getUint8(offset + 2);
var $tmp0 = ($tmp1 << 8) | $tmp2;
offset += 3;
vars.a = $tmp0 >> 16 & 255; // The values are finally parsed
vars.b = $tmp0 >> 8 & 255;
vars.c = $tmp0 >> 0 & 255;
vars.c = (() => 'third').call(this, vars.c); // But only the last one is properly formatted

When parsing multiple successive bitfields, the code that populates the varibles is "deferred" to the end. However, the code that calls the formatters is not deferred. Thus the formatters get called too early, when the values don't exist yet.

The "deferring" feature here causes the issue. Or rather the lack of deferring for the formatters. The bitfield parsing code is only generated when there are no more bitfields in the succession. But the code that calls the formatters is generated every time.

functoid pushed a commit to functoid/binary-parser that referenced this issue Feb 29, 2016
@wang701
Copy link

wang701 commented Apr 26, 2016

👍

@kaYcee
Copy link

kaYcee commented Nov 13, 2016

Can functoid's patch please be merged with the main repository (and eventually be made available through npm)? Thanks!

@jayeheffernan
Copy link

This is a problem for me too. E.g.:

new Parser()
    .bit1('first', { formatter: val => !!val } )
    .bit1('second', { formatter: val => !!val })
    .getCode()

The formatter for first is called beforefirst and second are read (then finally the formatter for second is called. The result is that second is a boolean but first is an integer.

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 a pull request may close this issue.

4 participants