Closed Bug 1227207 Opened 6 years ago Closed 6 years ago

TypedArray constructor throws TypeError when it should throw RangeError

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla49
Tracking Status
firefox49 --- fixed

People

(Reporter: lth, Assigned: lth)

References

(Blocks 1 open bug)

Details

Attachments

(1 file)

Today's mozilla-inbound.  I think it's been this way for a while.

The spec 22.2.1.5 says that TypedArray should throw RangeError if the start offset does not divide the element size or if the end offset minus the start offset does not divide the element size.  SpiderMonkey throws TypeError in both cases.

js> var AB = new ArrayBuffer(12)
js> new Int32Array(AB, 2)
typein:3:1 TypeError: invalid arguments
Stack:
  @typein:3:1
js> var BC = new ArrayBuffer(14)
js> new Int32Array(BC)
typein:5:1 TypeError: invalid arguments
Stack:
  @typein:5:1
js>
Blocks: es6
Jason, isn't there a bug for tracking spec deviations like this?
Flags: needinfo?(jorendorff)
No, there's not a separate meta bug for functionality that's implemented in a buggy way. Bug 694100 is the right block-ee for all deviations from spec.
Flags: needinfo?(jorendorff)
Attachment #8753827 - Flags: review?(jorendorff)
Assignee: nobody → lhansen
Status: NEW → ASSIGNED
Comment on attachment 8753827 [details] [diff] [review]
throw correct error type

Review of attachment 8753827 [details] [diff] [review]:
-----------------------------------------------------------------

Thanks!

::: js/src/vm/TypedArrayObject.cpp
@@ +604,5 @@
>              buffer = static_cast<ArrayBufferObjectMaybeShared*>(&AsSharedArrayBuffer(bufobj));
>          }
>  
>          if (byteOffset > buffer->byteLength() || byteOffset % sizeof(NativeType) != 0) {
> +            JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_BAD_INDEX);

The error message this makes is technically correct and *maybe* better than "invalid arguments" but not ideal.
We could splurge and add another error message to js.msg. Your call.
Attachment #8753827 - Flags: review?(jorendorff) → review+
https://hg.mozilla.org/mozilla-central/rev/8302c77d1f23
Status: ASSIGNED → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla49
You need to log in before you can comment on or make changes to this bug.