When expecting a positive integer...

Dmitry Lomov dslomov at google.com
Tue Apr 9 03:32:50 PDT 2013

Regarding TypedArrays:
- ToUint32 is definitely not sufficient for typed array indices because
typed arrays are allowed to be longer that 2^32.
- However the specified "clamping" of negative feels counter-intuitive to
me: under the current spec "arr[-32] = 42" should assign 42 to arr[0]. This
is not what current implementations of typed arrays, and this semantics
seems confusing and dangerous.

Also, the systematic usage of ToPositiveInteger throughout the typed
array/array buffer spec causes some counter-intuitive (or, at least,
legacy-incompatible) behavior. For example, under the current spec "new
Float32Array(-10)" silently allocates an array of length 0. Current typed
array implementations throw in that case, and I think a case could be made
that throwing here is better than allowing sloppy code.

If people agree this is generally a thing to be avoided, I am happy to
collect a systematic list of these issues and suggest fixes - but maybe I
am missing something and that has some deep motivation?

Kind regards,

On Tue, Apr 9, 2013 at 8:05 AM, Domenic Denicola <
domenic at domenicdenicola.com> wrote:

> I notice that operations accepting positive integers do various
> somewhat-inconsistent things as of the latest ES6 spec:
> Various typed array things do `ToPositiveInteger`, a new operation as of
> ES6:
>   - `ArrayBuffer(length)` and `TypedArray(length)`
>   - Constructing a typed array, and `TypedArray.prototype.set`, coerce
> both the passed object's `length` property and the numeric byte-offset
> argument
>   - Most interestingly, `TypedArray.prototype.@@elementGet(index)` and
> `@@elementSet(index, value)` do a `ToPositiveInteger` on `index`. So
> `typedArray[-123] === typedArray[0]`, and `typedArray[-321] = 5` gives
> `typedArray[0] === 5`.
> Whereas arrays use `ToUint32`, but sometimes throw `RangeError`s:
>   - Indices generally "work" if `ToString(ToUint32(index)) !== index`;
> otherwise they're treated as ordinary properties.
>  - When getting a length from any array-like, you just do `ToUint32` on it.
>  - But when setting a length, you validate so that if `ToUint32(length)
> !== length`, a `RangeError` is thrown.
> Finally, one lone place throws `RangeError`s after conversion:
>  - `String.prototype.repeat(count)` throws if `ToInteger(count)` is
> negative or infinite.
> ---
> I was curious if there were any interesting rationales driving these
> decisions. (And I fully appreciate there might not be; it could just be
> compatibility constraints.) Or, to put it another way, I was curious what
> would be considered "idiomatic" JS, e.g. in the same way that the spec sets
> a precedent of `ToString`-ing all string inputs to its functions.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130409/3a85964f/attachment.html>

More information about the es-discuss mailing list