TooTallNate / ref-struct

Create ABI-compliant "struct" instances on top of Buffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Zero Length Array

blechatellier opened this issue · comments

Hello,

What's the best way to represent a zero length array in a struct please?

Tried ArrayType('char', 0) but the buffer is empty.

Thanks!

Can you show me the C definition of this struct? What purpose is a zero length array?

@TooTallNate here is the C definition, the zero length array is to receive variable length data (video frames).

typedef struct
{
unsigned long long pts;
unsigned int len;
char data[0];
}

I see. Pretty specific use case… I'm pondering if it belongs in ref-struct, or as a wrapper module around ref-struct (ref-struct-variable perhaps), which would take the raw Struct instance and tack on the dynamic data prop based on the len value.

I think it would be something like this:

var S = Struct({
  pts: 'longlong',
  len: 'int'
});

Object.defineProperty(S.prototype, 'data', {
  get: function () {
    return ref.reinterpret(this.ref(), this.len, S.size);
  }
});

var s = new S(someVideoFrameBuffer);
Buffer.isBuffer(s.data); // true
s.data.length === s.len; // true

Thanks for your help @TooTallNate - will investigate and report back!