Open gmarty opened 12 years ago
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. 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.
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 doingstack[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?