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

Implement bit fields #12

Open
gmarty opened this issue Jul 26, 2012 · 4 comments
Open

Implement bit fields #12

gmarty opened this issue Jul 26, 2012 · 4 comments

Comments

@gmarty
Copy link

gmarty commented Jul 26, 2012

The next big thing that LLJS Structs currently lack compared to C∕C++ are bit fields. A syntax similar to this snippet would be great:

struct BitFieldStruct {
  int x: 1;
  int y: 1;
  int padding: 6;
};

The static analysis should make sure that the sum of all consecutive bitfields is a multiple of 8.
Then at runtime, set values would be clamped between 0 and Math.pow(2, size_of_bitfield).
The storage on the stack would be super efficient as retrieving a value is as easy as doing stack[index] >> bit_field_offset & size_of_bit_field.

I'm not sure though how to handle endianess.

Do you think that it is something that could be implemented?

@rinon
Copy link
Collaborator

rinon commented Jul 27, 2012

This is possible, but what would the benefit of doing this in the language be, rather than implementing struct member functions that get and set the values into a byte field properly? Member functions are certainly more verbose, but I honestly don't know how often this comes up to know if that's a big deal. Haven't used bit fields before, personally. Do you need to do this a lot?

@gmarty
Copy link
Author

gmarty commented Jul 28, 2012

All the issues I opened recenty (#10, #11, #12) are because I'd like to have a binary parser in LLJS.
Have a look at these codes:

Basically, you feed binary data and a struct type to a parser with a API looking like this:

struct messageDef {
    u32 id;
    u32 color;
    u8 message[16];
};
let messageDef msg = parse(buffer, messageDef);

And you get the data of the buffer parsed to a messageDef struct.

But there are complex, yet common cases such as bit fields or back references:

struct def {
    int x: 1; // Bit fields
    int y: 1;
    int padding: 6;
    u16 length;
    coordDef coords[length]; // Back reference
    messageDef messages[length];
};

Here both coordDef and messageDef arrays length depends on the value of length member. So it must be assigned at runtime.

I understand LLJS logic is not ready for such a feature, and there's probably a lot of work to do before implementing a binary parser. But that would worth it as working with buffer of binary data in JavaScript would then be very easy.

@mbebenita
Copy link
Owner

We can definitely add support for bit fields, it wouldn't be that hard. But supporting back references like you mentioned is not possible, ... the size/layout of the struct needs to be known at compile time.

@benvanik
Copy link

I too would really like bitfields - primarily for file IO and network packet mangling. It's really nice to be able to overlay structs right on binary data and get the values out in sensible ways. Accessor functions can help, but are not always the right answer.

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

No branches or pull requests

4 participants