Binary data: Structs and ArrayBuffers

# Jussi Kalliokoski (12 years ago)

I was just reading through the binary data proposal [1] and I have a few comments / questions:

First of all, how will this integrate with the Typed Arrays? Will a struct have an intrinsic ArrayBuffer? What about an ArrayType instance? If they do, how will it react to typeless properties? Are typeless properties allowed in the first place (IIRC there was a talk I watched where David Herman or someone else said that this might be)? Will you be able to extract structs out of an ArrayBuffer?

What about strings? Looking through the binary data related proposals, there seems to be no good way of extracting strings from binary data. Should we have, for example StringType(DOMString encoding, uint length, boolean isPadded=false) for Structs? Or, should DataView have a method for extracting a string from it? What about storing one?

Pointers. Now it's useful if a struct can contain an array of arbitrary size, but we don't have pointers. We can't let the struct be of arbitrary size either. What are the thoughts on this?

Arrays inside structs. Are there plans for this? They're not absolutely necessary, but often quite handy anyway, for example:

StructType({ unique: uint32[4] })

Is way simpler to manage than:

StructType({ unique0: uint32, unique1: uint32, unique2: uint32, unique3: uint32 })

Also, what's the plan with Typed Arrays anyway? Are we going to adopt them as a part of JS or leave it as an extension?

Binary stuff is hard in a language like JavaScript, but I think that ultimately we'll get something workable, and would love to see more discussion around this!

Cheers, Jussi

[1] harmony:binary_data

# Andrea Giammarchi (12 years ago)

On Mon, Dec 3, 2012 at 11:53 AM, Jussi Kalliokoski < jussi.kalliokoski at gmail.com> wrote:

I was just reading through the binary data proposal [1] and I have a few comments / questions:

First of all, how will this integrate with the Typed Arrays?

Typed Arrays are already using similar logic where Float32Array is, as example, a new ArrayType(float32, length)

Will a struct have an intrinsic ArrayBuffer? What about an ArrayType instance?

I believe Float32Array should be instanceof ArrayType

If they do, how will it react to typeless properties? Are typeless properties allowed in the first place (IIRC there was a talk I watched where David Herman or someone else said that this might be)? Will you be able to extract structs out of an ArrayBuffer?

I hope so ... MyStructArray = new ArrayType(new StructType({myStruct:fields}), length) so that new MyStructArray([st])[0] will be your struct?

Pointers. Now it's useful if a struct can contain an array of arbitrary size, but we don't have pointers. We can't let the struct be of arbitrary size either. What are the thoughts on this?

what is an arbitrary size struct? what do you mean?

Arrays inside structs. Are there plans for this?

I think supported already

const Pixel = new StructType({ point: Point2D, color: Color });

same could be {color: Uint8Array} ... isn't it ? is as RGBA

Also, what's the plan with Typed Arrays anyway? Are we going to adopt them as a part of JS or leave it as an extension?

predefined handy natives?

Binary stuff is hard in a language like JavaScript,

not really, JS has type checks since ever, just consider TypeError constructor ;-)

but I think that ultimately we'll get something workable, and would love to see more discussion around this!

for what is worth it, I wrote a way to test now this stuff in 2011 ... webreflection.blogspot.com/2011/09/introduction-to-js-ctypes.html

That is (was?) a workable shim for typed stuff in JS, grab any FF and test it

br

# Kenneth Russell (12 years ago)

On Mon, Dec 3, 2012 at 11:53 AM, Jussi Kalliokoski <jussi.kalliokoski at gmail.com> wrote:

I was just reading through the binary data proposal [1] and I have a few comments / questions:

First of all, how will this integrate with the Typed Arrays? Will a struct have an intrinsic ArrayBuffer? What about an ArrayType instance? If they do, how will it react to typeless properties? Are typeless properties allowed in the first place (IIRC there was a talk I watched where David Herman or someone else said that this might be)? Will you be able to extract structs out of an ArrayBuffer?

What about strings? Looking through the binary data related proposals, there seems to be no good way of extracting strings from binary data. Should we have, for example StringType(DOMString encoding, uint length, boolean isPadded=false) for Structs? Or, should DataView have a method for extracting a string from it? What about storing one?

Conversions between strings and typed arrays have been specified separately from the core typed array specification to keep the latter spec simple. The current effort, encoding.spec.whatwg.org/#api , looks like it has reached stability and is ready to be implemented. I'm not sure how this work would integrate with the ES6 spec.

# David Herman (12 years ago)

On Dec 3, 2012, at 11:53 AM, Jussi Kalliokoski <jussi.kalliokoski at gmail.com> wrote:

I was just reading through the binary data proposal [1] and I have a few comments / questions:

First of all, how will this integrate with the Typed Arrays?

Typed Arrays will become a special case of binary data objects. In other words, the binary data API generalizes typed arrays completely.

Will a struct have an intrinsic ArrayBuffer?

Yes, but with some exceptions. We will be extending the API to allow fields of type "object" (basically, pointers to JS objects) or the type "any" (basically, any JS value whatsoever), but those types will not be allowed to expose their ArrayBuffer.

What about an ArrayType instance? If they do, how will it react to typeless properties? Are typeless properties allowed in the first place (IIRC there was a talk I watched where David Herman or someone else said that this might be)? Will you be able to extract structs out of an ArrayBuffer?

Same story: if you have the type "object" or the type "any" you can't get at the underlying ArrayBuffer.

What about strings? Looking through the binary data related proposals, there seems to be no good way of extracting strings from binary data. Should we have, for example StringType(DOMString encoding, uint length, boolean isPadded=false) for Structs? Or, should DataView have a method for extracting a string from it? What about storing one?

As Ken says, the WhatWG string encoding/decoding spec is the way to do that. The important thing to recognize is that a string formats are just that -- encoding/decoding formats -- rather than "types." There isn't and won't be a string type, or a UTF8 type, or anything like that in the API.

Pointers. Now it's useful if a struct can contain an array of arbitrary size, but we don't have pointers. We can't let the struct be of arbitrary size either. What are the thoughts on this?

See above.

Arrays inside structs. Are there plans for this? They're not absolutely necessary, but often quite handy anyway, for example:

StructType({ unique: uint32[4] })

That's definitely supported.

Also, what's the plan with Typed Arrays anyway? Are we going to adopt them as a part of JS or leave it as an extension?

Fully adopted.

Binary stuff is hard in a language like JavaScript, but I think that ultimately we'll get something workable, and would love to see more discussion around this!

I think binary data will be very nice in ES6, and I'm excited about where it's headed. We've started implementation in SpiderMonkey and hopefully before too long people will be able to start experimenting with it in nightly builds of Firefox.

Thanks for your questions!

# Jussi Kalliokoski (12 years ago)

On Tue, Dec 4, 2012 at 3:34 AM, David Herman <dherman at mozilla.com> wrote:

Typed Arrays will become a special case of binary data objects. In other words, the binary data API generalizes typed arrays completely.

Sounds like a plan!

Yes, but with some exceptions. We will be extending the API to allow fields of type "object" (basically, pointers to JS objects) or the type "any" (basically, any JS value whatsoever), but those types will not be allowed to expose their ArrayBuffer.

I see, makes sense.

Same story: if you have the type "object" or the type "any" you can't get at the underlying ArrayBuffer.

As well as this.

As Ken says, the WhatWG string encoding/decoding spec is the way to do

that. The important thing to recognize is that a string formats are just that -- encoding/decoding formats -- rather than "types." There isn't and won't be a string type, or a UTF8 type, or anything like that in the API.

Sounds good!

Arrays inside structs. Are there plans for this? They're not absolutely necessary, but often quite handy anyway, for example:

StructType({ unique: uint32[4] })

That's definitely supported.

Woohoo!

Also, what's the plan with Typed Arrays anyway? Are we going to adopt them as a part of JS or leave it as an extension?

Fully adopted.

Excellent.

Binary stuff is hard in a language like JavaScript, but I think that ultimately we'll get something workable, and would love to see more discussion around this!

I think binary data will be very nice in ES6, and I'm excited about where it's headed. We've started implementation in SpiderMonkey and hopefully before too long people will be able to start experimenting with it in nightly builds of Firefox.

I for one am eager to get experimenting with this! I think this will be quite useful especially for our JS codecs project.

Thanks for your questions!

Thanks for your answers!