-
Notifications
You must be signed in to change notification settings - Fork 3
Factor out ArrayBuffer.prototype.transfer algorithm for use by other specs? #3
Comments
Also add https://html.spec.whatwg.org/multipage/structured-data.html to that list. We'll want this to preserve resizability: const arrayBuffer = new ArrayBuffer(1024, { maxByteLength: 2048 });
const transferredArrayBuffer = structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
console.log(transferredArrayBuffer.resizable === true);
Looks like I misread the explainer. 😅 As currently written,
So yes, there are compelling use cases! 😁
|
There are two topics here: factoring out the transfer AO and "should Re: factoring out the transfer AO, sounds like a good idea. Sounds like AO need the "preserve resizability" functionality, which the API currently doesn't do. And does it need the ability to create the new ArrayBuffer in a different realm? Re: transfer preserving the resizability in the API, thanks for the use cases. Adding a resizability-preserving A natural extension is to make the parameters mirror the constructor, i.e. One hiccup is that by default, if The current default behavior was motivated by the intuition that uses of But is that the wrong default? If the options bag is not passed, should it always preserve the resizability and pass along the current max byte length? That has other unfortunate corner cases that I think are slightly worse: if Addressing the direct use cases:
That approach allocates new ArrayBuffers for each BYOB read. Is that desirable? If the API isn't set in stone already, with resizable buffers, you can do this buffer locking by doing the following (which can reasonably be considered an abuse of the API, but is allowed by the spec):
Yes, I need to write the structured clone PR. But this particular use of transfer doesn't have realloc semantics, right? You can't say, "transfer, with this new size". |
cc @phoddie: Any opinions on Edit: Actually, upon re-reading what I wrote above, we can't fill in the max byte length by default when the options bag isn't present. That means there's no way to use So I think that means we'd have to live with typing |
Yes, the API is a bit finicky, since we have to teach users to replace their "reusable" buffer after each call to But I think it's unavoidable. If the readable byte stream is implemented in userland (through With the "flip a bit" approach, we would be able to prevent modifications to the BYOB reader's input buffer, but we wouldn't be able to describe how the BYOB request's buffer is constructed from that input buffer. Also, readable byte streams are already in Chrome and Firefox, so I'm afraid that part of the API is already set in stone. 😅
Correct, the size should stay the same during structured cloning. Note that even for the readable byte stream use case, we're not actually interested in resizing the buffer anywhere in the Streams specification. We just want to preserve resizability. Hmm, now I think about it: should a userland byte source be able to resize the BYOB request's buffer? With the current semantics, there's no need, because the byte source cannot respond with a view that's larger than the original request. But what if (for whatever reason) it did try to call |
Preserving resizability by default seems like it would match developer expectations. It also doesn't look too bad to implement in XS. |
Sorry, please see my edit above. After I asked this I realized there's no way to transfer to a fixed-length buffer if the arguments were to mirror the constructor and use an options bag. I'll bring this up as a discussion item at the next plenary. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
@domenic How does ArrayBufferCopyAndDetach look for your use cases? |
Looks perfect, thanks so much! |
Closing issue as resolved. |
We have a couple of specs on the web platform that want to transfer ArrayBuffers:
(whatwg/streams#1224 contains some discussion of having Streams defer to Web IDL.)
It was raised in whatwg/streams#1248 that probably when we transfer ArrayBuffers on the web platform, we should preserve their resizability. And apparently this spec has an algorithm which does that.
Could this spec factor that algorithm out into an abstract operation, which web platform specs could call?
The text was updated successfully, but these errors were encountered: