-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Uint8Array(uint8arrayInstance) does not copy. #4714
Comments
Here is the workaround I've done for now in cifre: hookflash/obsolete.cifre@398ff3f |
From the spec:
So it's up to the implementation to decide how this is done. |
@trevnorris is technically correct - the best kind of correct - but it's probably best to follow browser behavior here. Principle of least surprise and all that. |
Can someone review bnoordhuis/node@94d2ad0? |
Fixed, for better or worse, in 01ee551. |
FWIW, I think the browser behavior that node will soon match is better. In practice there are many times I want to share the backing store and many times I want a copy. Being able to choose by either using a Uint8Array or it's ArrayBuffer makes working with these things easier. |
@bnoordhuis forgot to implement the same with DataViews: var ui = new Uint8Array(8);
var dv = new DataView(ui);
ui[0] === 0
dv.setUint8(0, 8);
ui[0] === 8; |
DataViews are meant to be interfaces, not copies. |
@Mithgol I realize that, but take into account:
So technically DataView's shouldn't be able to read in a TypedArray. Only an ArrayBuffer. There's a footnote that states:
But since node allows for it, think it's in the best interest that DataViews follow the TypedArray specification. |
Okay, fixed in 7b0770b. I guess it's technically a violation of the spec but we're not supposed to accept anything besides ArrayBuffers in the DataView constructor anyway. I'm reopening the issue because there's still a bug that needs to be addressed: the .buffer property on the array/view object points to the original, not the copy. |
@bnoordhuis Allowing typed arrays and data views to accept buffers is a nicety that was added pre-v0.8. See GH-4257. Think that specific change will need to be reverted. |
For the future reference, that's how you have just broken my jDataView/jDataView#26 simplification of the DataView module. |
Typed arrays still accept buffers. The reason the DataView constructor no longer does is that it creates some subtle semantic differences: Ergo, allowing buffers was a mistake and one that should be rectified sooner rather than later. |
And before someone brings it up, yes, you could make a Buffer object look like an ArrayBuffer by giving it a byteLength property but that doesn't make it one. For starters, the slice() method works different. |
@bnoordhuis please update Buffer documentation that states Buffers can be used with DataViews. |
Yes, yes. Done in 30b0bc4. |
for posterity, this discussion is continued in GH-4742. |
I ran into a bug in cifre when running node, where creating a new Uint8Array from another caused the new instance to share the same backing store as the original. In the browser this does not happen unless you explicitly pass in the underlying ArrayBuffer of the original into the second's constructor.
The text was updated successfully, but these errors were encountered: