-
Notifications
You must be signed in to change notification settings - Fork 195
Fix BufferSource algorithms for shared and resizable buffers
#1529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@annevk Could you assign reviewer(s) to this? Thanks! 🙏 |
annevk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did an editorial pass.
MattiasBuelens
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick self-review.
index.bs
Outdated
| 1. [=/Assert=]: |arrayBuffer| is an {{ArrayBuffer}} or {{SharedArrayBuffer}} object. | ||
| 1. Let |jsArrayBuffer| be the result of [=converted to a JavaScript value|converting=] | ||
| |arrayBuffer| to a JavaScript value. | ||
| 1. If [$IsDetachedBuffer$](|jsArrayBuffer|) is true, then return the empty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you pass a TA backed by a detached buffer, then the Set |offset| to |bufferSource|'s [=ArrayBufferView/byte offset=]. step above will throw. It's weird that passing a TA backed by a detached buffer throws while passing the detached buffer itself returns the empty byte sequence (in this step). It doesn't fundamentally break anything, but it's inconsistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, we should check if bufferSource's underlying buffer is detached before we try to read bufferSource's byte offset.
I'll move this to the preceding "if bufferSource is a buffer view type instance" step.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved the detached check all the way to the top. I also check if length is zero, so reading byte offset later on won't throw.
Funnily enough, reading bufferSource's byte length itself can still throw for an out-of-bounds DataView (as you already reported)... Do we want "get a copy of the bytes held" to propagate that error? Or should we return an empty byte sequence in that case? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it so we read both byte length and byte offset before we check if length is zero. That way, any exceptions from reading those properties will be thrown first.
6a04a16 to
a528e68
Compare
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
a2bf3b3 to
ce8c232
Compare
ce8c232 to
cf28af9
Compare
The rules of thumb here are:
|
Previously, many
BufferSourcealgorithms were using internal slots such as[[ByteOffset]]and[[ByteLength]]directly. However, with the addition ofSharedArrayBuffer(#353, #1311) and[AllowResizable](#982), this comes with extra caveats:SharedArrayBuffer, we need to useArrayBufferByteLengthto matchSharedArrayBuffer.prototype.byteLength.TypedArrayByteLength. Similarly, for a length-trackingDataView, we need to useGetViewByteLength.DataView, we first need to check if the view is still inbounds usingIsArrayBufferViewOutOfBounds.Transferring an
ArrayBuffermust also take into account whether it should remain resizable or not. Fortunately, we can use the newArrayBufferCopyAndDetachoperation for that.preserveResizabilityparameter to "transfer anArrayBuffer", so specifications can choose whether to preserve resizability or not. By default, this is not preserved. (Most specs aren't using[AllowResizable]yet anyway.)realmparameter of that algorithm, sinceArrayBufferCopyAndDetachdoesn't accept such a parameter. According to WebDex, no specs were actually using that parameter anyway.Open questions:
SharedArrayBuffer, what memory ordering should we use? ECMAScript seems to useSEQ-CSTpretty much everywhere (e.g.%TypedArray%.prototype.byteLength), but "get a copy of the bytes" usesUNORDEREDforGetValueFromBuffer.Fixes #1312.
Fixes #1385.
(See WHATWG Working Mode: Changes for more details.)
Preview | Diff