Allow all ArrayBufferView types as Buffers
Right now, many modules allow the use of plain Uint8Array
s in addition to Buffer
s. This PR begins the work to expand such support to all ArrayBufferView
types (including all TypedArray
types and DataView
).
This work was inspired by https://github.com/nodejs/node/issues/1826, which requests support for ArrayBuffer
as well. But ArrayBuffer
is treated separately from its views in V8's API and it is easier to get started with the work here.
The first two commits add the necessary infrastructure for consuming and testing all ArrayBufferView
types. The last two demonstrate how to update the JS layer for it. (Specifically, using the new isArrayBufferView
utility function and update usage of buf.length
to buf.byteLength
).
I don't intend this PR to make its way into 8.0.0.
Relevant discussions on IRC with @addaleax
2017-03-23 20:20:54 TimothyGu So I was looking at https://github.com/nodejs/node/issues/1826
2017-03-23 20:21:23 TimothyGu As we are trying to support Uint8Array in C++ binding it doesn't
look much more work to support all the other ArrayBufferView types
2017-03-23 20:21:40 TimothyGu including all TypedArray and DataView
2017-03-23 20:21:54 addaleax right … that was also pointed out to me in the last streams wg
meeting
2017-03-23 20:22:32 addaleax I understand where it’s coming from, but so far I have wanted to
keep the changes pretty minimal
2017-03-23 20:23:06 addaleax bc going from Buffer support to Uint8Array support is, for the most
part, a tiny change
2017-03-23 20:23:27 addaleax if somebody (you?) wants to work on this, yay
2017-03-23 20:23:40 TimothyGu TBH it doesn't take much of a change on the C++ side to get it
working
2017-03-23 20:23:46 TimothyGu http://sprunge.us/SgET
2017-03-23 20:23:54 TimothyGu or http://sprunge.us/SgET?diff
2017-03-23 20:25:12 TimothyGu so I thought maybe we could just support all of them at the same
time
2017-03-23 20:25:25 addaleax that’s nice, yes
...
2017-03-23 20:28:20 addaleax re: generic ArrayBufferView support … the one reason I’ve not been
doing that is that my personal goal is to give users a more
standard way of passing in binary data, and Uint8Arrays work just
fine for that purpose. the other reason is that there is at least a
small bit of ambiguity, because interpreting e.g. a Uint16Array as
a Uint8Array can be done in two ways (entry-for-entry or via the...
2017-03-23 20:28:21 addaleax ...underlying storage)
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
documentation is changed or added -
commit message follows commit guidelines