-
Notifications
You must be signed in to change notification settings - Fork 89
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
Comments
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? |
All the issues I opened recenty (#10, #11, #12) are because I'd like to have a binary parser in LLJS.
Basically, you feed binary data and a struct type to a parser with a API looking like this:
And you get the data of the buffer parsed to a But there are complex, yet common cases such as bit fields or back references:
Here both 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. |
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. |
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. |
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:
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
andMath.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?
The text was updated successfully, but these errors were encountered: