-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Normalize TypedArray, ArrayBuffer and DataView constructors #410
Conversation
I'd avoid adding explicit tests to check whether or not an optional parameter is present, if normal type conversions through ToInteger/ToLength are sufficient. And I still think it's important to find out why the current parameter validation steps were added. And if the validation steps in the DataView constructor are changed, we may also want to apply similar changes to GetViewValue/SetViewValue. |
I tried to follow other parts handling arguments that are not present, even when they get the undefined value.
I'm on the same boat.
+1, I'll include these |
@anba the new commits are addressing some extras, including the TypedArray (length) constructor. It still does not answer the parameter validation question. I'll check if I find anything. |
I guess it depends on the built-ins, for example Array.prototype.copyWithin, Array.prototype.fill, Array.prototype.includes, Array.prototype.indexOf and %TypedArray%.prototype.set all use implicit conversion instead of explicit checks for non-present parameters. |
1. If IsDetachedBuffer(_buffer_) is *true*, throw a *TypeError* exception. | ||
1. Let _bufferByteLength_ be the value of _buffer_'s [[ArrayBufferByteLength]] internal slot. | ||
1. If _offset_ > _bufferByteLength_, throw a *RangeError* exception. | ||
1. If _byteLength_ is *undefined*, then | ||
1. If _byteLength_ is either not present or *undefined*, then |
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.
On the last commit I removed the "not present" steps, but I wanted to keep this one as it still bugs me here.
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.
@anba ^^
@littledan I tried to address #468 on 0157b3f. I reused this same PR as it already touches the same steps. I can split it to a new PR if it works better. Note that for consistency this PR also removes the RangeError on |
@leobalter the fix-up looks good, modulo web compat concerns. Looks like a faithful implementation of @allenwb's suggestion, and I'm in favor of the spec change given that the compat issue is preexisting. |
1. Let _integerLength_ be ToInteger(_length_). | ||
1. If _integerLength_ < 0, throw a *RangeError* exception. | ||
1. Let _byteLength_ be ? ToLength(_integerLength_). | ||
1. Let _numberLength_ be ToNumber(_length_). |
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.
Looks like this is missing a ?
.
cdfd3f6
to
fe5fde3
Compare
I tried to address the last concerns pointed by @jorendorff but I'm afraid I might be bike shedding on the behaviors. I'll try to list them here:
|
@littledan @bterlson should I add this to the agenda? It might need consensus. |
@leobalter Yeah probably a good idea. I'll add the label. |
b202a08
to
b9b91b9
Compare
I believe this was discussed at tc39. @leobalter can you summarize the outcome of the discussion? I imagine we are free to fix #265? |
I've got new up-to-date tables including the expected results from the specs (same on ES6, ES7, and draft) and the expected results with the current PR. The changes on this PR are now updated to reflect this table. TypedArray( length )Results for
TypedArray( buffer, byteOffset, length )Results for
Note: as mentioned at #410 (comment) Infinity length may cause a RangeError at step 14.c:
ArrayBuffer( length )Results for
DataView( buffer, byteOffset, byteLength )Results for
Note: as mentioned at #410 (comment) Infinity length may cause a RangeError at step 11.b:
|
Excellent data. I'm happy with these semantics; they seem to be good with respect to both consistency and probable web compatibility (at least from an intersection semantics point of view). It's nice that it's shown here that new exceptions can be thrown in some cases where the ES2017 draft spec currently doesn't throw. @allenwb Have you found more information which you mentioned about previous motivation to not throw errors in some of these cases? Or are you skeptical of some of the checks which are removed here? |
I've also browsed at the Khronos spec, it wasn't helpful to improve my data: https://www.khronos.org/registry/typedarray/specs/latest/ Maybe I should seek for another info from Khronos to find some historical background. |
@leobalter Regarding the Khronos spec. I think the original author (Ken) just used WebIDL conventions to describe the signatures To understand them you have to look at the Khronos signatures and then look at the conversion rules for the WebIDL types used in the WebIDL spec that was contemporary with the Khronos spec. For example, x in the signature applicable to the above table is specified to be "unsigned long" so the this conversion applies to it. @littledan Regarding "previous motivations", I was thinking about things like the following. But I didn't finde anything that is immediately relevant. There are also probably closed issues bugs.ecmascript.org that may be relevant.
|
That's my bad on merging two different tables I've had made. The On this case, my data is wrong, but the RangeError would be preserved on the spec. |
That's very helpful, I cay update the table with this info if it helps, but I believe these changes are close to that spec, with a few minor differences. e.g.:
We are not doing this on the constructor, although a few implementations are casting Infinity to 0 as you can notice from the table. That's not reflected on the specs. |
@leobalter sorry, I missed your comment. Sure, conversion floats to integers without error for |
Thanks, @zloirock! @allenwb I added notes on #410 (comment) to mention the Infinity values may cause the RangeError. I also believe it might not trigger the RangeError all the time, assuming edit: the ToLength will convert |
This reached consensus in the last meeting of May 2016. @bterlson can we merge this? 👍 |
ES2016's [GetViewValue](https://tc39.github.io/ecma262/2016/#sec-getviewvalue) (and the SetViewValue) throws for undefined byteOffset arg, after tc39#410, [it casts to 0 using ToIndex](https://tc39.github.io/ecma262/#sec-getviewvalue) This change throws a TypeError only when the argument is absent. e.g.: `new DataView(buffer, 0).getFloat32()`
Ref #265
On these changes, the offset arguments have a consistent check to throw for values < 0, and length will pass through the ToLength.