-
Notifications
You must be signed in to change notification settings - Fork 14
Normative: throw whenever creating TA from OOB TA #72
Conversation
Any thoughts on this, @syg? |
gh-75 addresses this concern in a different way than what we've discussed so far (see gh-74). Since we won't know which of the approaches the proposal's going to take until after the next TC39 meeting, I'm going to hold off on updating this patch until that decision has been made. |
@jugglinmike Consensus was reached in committee for #75 and that has been merged, FYI. |
@syg If I understand correctly, we'd like the new ArrayBuffer to have a length that matches the cached length of the source ArrayBuffer, but we want the actual copying to be limited by the sneakily-updated length. For example (adapting your code from gh-75): const rab = new ArrayBuffer(4 * 8, { maxByteLength: 1024 });
const ta = new Float64Array(rab, 0, 4);
for (let i = 0; i < 4; ++i) {
ta[i] = i;
}
class MyArray extends Uint32Array {
constructor(...params) {
super(...params);
}
static get [Symbol.species]() {
rab.resize(2 * 8);
return Uint32Array;
}
};
const newTa = new MyArray(ta);
assert(ta.length == 2); // Since it's length-tracking
assert(newTa.length == 4); // Since it snapshotted the length
assert(newTa[0] == 0);
assert(newTa[1] == 1);
assert(newTa[2] == 0);
assert(newTa[3] == 0); That's what I've tried to implement with the latest commit on this branch. |
That was my intention, exactly. |
1. <ins>Let _data_ be ? AllocateArrayBuffer(_bufferConstructor_, _byteLength_).</ins> | ||
1. <ins>If IsIntegerIndexedObjectOutOfBounds(_srcArray_, _getSrcBufferByteLength_) is *true*, throw a *TypeError* exception.</ins> | ||
1. <ins>Set _elementLength_ to IntegerIndexedObjectLength(_srcArray_, _getSrcBufferByteLength_).</ins> | ||
1. <ins>Set _byteLength_ to _elementSize_ × _elementLength_.</ins> |
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.
The new byteLength might be greater than the old byteLength. byteLength should only be updated if the buffer was shrunk. Alternatively, you could assign the new byte length to a newByteLength, and pass min(newByteLength, _byteLength) to CopyDataBlockBytes.
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.
Persisting the variable _byteLength_
might be confusing in what is becoming a somewhat complicated algorithm (readers might not recognize that despite its authoritative name, its value is not the byte length). There's also _elementLength_
to consider, which maybe ought to be complimented with a corresponding _newElementLength_
(more for coherence than a direct need).
I think we can use the new buffer's [[ArrayBufferByteLength]] slot to get the prior value of _byteLength_
without having to communicate "newness" or "oldness" through variable names. I've pushed up a commit to demonstrate what I have in mind. Does that seem appropriate to you?
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.
We can't use [[ArrayBufferByteLength]] for growable SharedArrayBuffer
s, which stores its byte length in a separate shared block. That's why the idempotent getter exists.
I retract my comment, I missed the operative keyword "new". Using the new buffer's [[ArrayBufferByteLength]]. That works for me.
Any thoughts on this latest revision, @syg? |
Resolves gh-57. Duplicating the boundary-checking step seems like the most straightforward solution to me. Alternatively, we could interrupt the branches to perform the bounds checking that's common to both: