Skip to content

Commit

Permalink
use messages for typedarray ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Dec 21, 2024
1 parent f7531eb commit 65bb240
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,17 +555,26 @@ protected static NativeTypedArrayView<?> js_constructor(
}

if ((byteOff < 0) || (byteOff > na.getLength())) {
throw ScriptRuntime.rangeError("offset out of range");
String msg = ScriptRuntime.getMessageById("msg.typed.array.bad.offset", byteOff);
throw ScriptRuntime.rangeError(msg);
}
if ((byteLen < 0) || ((byteOff + byteLen) > na.getLength())) {
throw ScriptRuntime.rangeError("length out of range");
String msg = ScriptRuntime.getMessageById("msg.typed.array.bad.length", byteLen);
throw ScriptRuntime.rangeError(msg);
}
if ((byteOff % bytesPerElement) != 0) {
throw ScriptRuntime.rangeError("offset must be a multiple of the byte size");
String msg =
ScriptRuntime.getMessageById(
"msg.typed.array.bad.offset.byte.size", byteOff, bytesPerElement);
throw ScriptRuntime.rangeError(msg);
}
if ((byteLen % bytesPerElement) != 0) {
throw ScriptRuntime.rangeError(
"offset and buffer must be a multiple of the byte size");
String msg =
ScriptRuntime.getMessageById(
"msg.typed.array.bad.buffer.length.byte.size",
byteLen,
bytesPerElement);
throw ScriptRuntime.rangeError(msg);
}

return constructable.construct(na, byteOff, byteLen / bytesPerElement);
Expand Down Expand Up @@ -609,7 +618,7 @@ protected static NativeTypedArrayView<?> js_constructor(

private void setRange(NativeTypedArrayView<?> v, int off) {
if (off < 0 || off > length) {
String msg = ScriptRuntime.getMessageById("msg.typed.array.bad.offset");
String msg = ScriptRuntime.getMessageById("msg.typed.array.bad.offset", off);
throw ScriptRuntime.rangeError(msg);
}

Expand All @@ -636,7 +645,7 @@ private void setRange(NativeTypedArrayView<?> v, int off) {

private void setRange(NativeArray a, int off) {
if (off < 0 || off > length) {
String msg = ScriptRuntime.getMessageById("msg.typed.array.bad.offset");
String msg = ScriptRuntime.getMessageById("msg.typed.array.bad.offset", off);
throw ScriptRuntime.rangeError(msg);
}
if ((off + a.size()) > length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,16 @@ msg.typed.array.ctor.incompatible = \
Method %TypedArray%.prototype.{0} called on incompatible receiver

msg.typed.array.bad.offset = \
offset out of range
offset {0} out of range

msg.typed.array.bad.length = \
length {0} out of range

msg.typed.array.bad.offset.byte.size = \
offset {0} must be a multiple of the byte size {1}

msg.typed.array.bad.buffer.length.byte.size = \
used buffer length {0} must be a multiple of the byte size {1}

msg.typed.array.bad.source.array = \
source array is too long
Expand Down
210 changes: 210 additions & 0 deletions tests/testsrc/jstests/harmony/typed-array-ctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ load("testsrc/assert.js");
try { new Int8Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -2.68435457E8", msg);

msg = null;
try { new Int8Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Int8Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Int8Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -1 out of range", msg);

msg = null;
try { new Int8Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 3 out of range", msg);

var ta = new Int8Array(new ArrayBuffer(4), 3);
assertEquals("0", ta.toString());

ta = new Int8Array(new ArrayBuffer(5), 2);
assertEquals("0,0,0", ta.toString());

var one = new Int8Array([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -63,6 +85,28 @@ load("testsrc/assert.js");
try { new Uint8Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -2.68435457E8", msg);

msg = null;
try { new Uint8Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Uint8Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Uint8Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -1 out of range", msg);

msg = null;
try { new Uint8Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 3 out of range", msg);

var ta = new Uint8Array(new ArrayBuffer(4), 3);
assertEquals("0", ta.toString());

ta = new Uint8Array(new ArrayBuffer(5), 2);
assertEquals("0,0,0", ta.toString());

one = new Uint8Array([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -109,6 +153,30 @@ load("testsrc/assert.js");
try { new Int16Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -5.36870914E8", msg);

msg = null;
try { new Int16Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Int16Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Int16Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -2 out of range", msg);

msg = null;
try { new Int16Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 6 out of range", msg);

msg = null;
try { new Int16Array(new ArrayBuffer(4), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 must be a multiple of the byte size 2", msg);

msg = null;
try { new Int16Array(new ArrayBuffer(5), 2); } catch (e) { msg = e.toString();}
assertEquals("RangeError: used buffer length 3 must be a multiple of the byte size 2", msg);

one = new Int16Array([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -155,6 +223,30 @@ load("testsrc/assert.js");
try { new Uint16Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -5.36870914E8", msg);

msg = null;
try { new Uint16Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Uint16Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Uint16Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -2 out of range", msg);

msg = null;
try { new Uint16Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 6 out of range", msg);

msg = null;
try { new Uint16Array(new ArrayBuffer(4), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 must be a multiple of the byte size 2", msg);

msg = null;
try { new Uint16Array(new ArrayBuffer(5), 2); } catch (e) { msg = e.toString();}
assertEquals("RangeError: used buffer length 3 must be a multiple of the byte size 2", msg);

one = new Uint16Array([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -201,6 +293,30 @@ load("testsrc/assert.js");
try { new Int32Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -1.073741828E9", msg);

msg = null;
try { new Int32Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Int32Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Int32Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -4 out of range", msg);

msg = null;
try { new Int32Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 12 out of range", msg);

msg = null;
try { new Int32Array(new ArrayBuffer(4), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 must be a multiple of the byte size 4", msg);

msg = null;
try { new Int32Array(new ArrayBuffer(5), 4); } catch (e) { msg = e.toString();}
assertEquals("RangeError: used buffer length 1 must be a multiple of the byte size 4", msg);

one = new Int32Array([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -247,6 +363,30 @@ load("testsrc/assert.js");
try { new Uint32Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -1.073741828E9", msg);

msg = null;
try { new Uint32Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Uint32Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Uint32Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -4 out of range", msg);

msg = null;
try { new Uint32Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 12 out of range", msg);

msg = null;
try { new Uint32Array(new ArrayBuffer(4), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 must be a multiple of the byte size 4", msg);

msg = null;
try { new Uint32Array(new ArrayBuffer(5), 4); } catch (e) { msg = e.toString();}
assertEquals("RangeError: used buffer length 1 must be a multiple of the byte size 4", msg);

one = new Uint32Array([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -293,6 +433,28 @@ load("testsrc/assert.js");
try { new Uint8ClampedArray(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -2.68435457E8", msg);

msg = null;
try { new Uint8ClampedArray(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Uint8ClampedArray(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Uint8ClampedArray(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -1 out of range", msg);

msg = null;
try { new Uint8ClampedArray(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 3 out of range", msg);

var ta = new Uint8ClampedArray(new ArrayBuffer(4), 3);
assertEquals("0", ta.toString());

ta = new Uint8ClampedArray(new ArrayBuffer(5), 2);
assertEquals("0,0,0", ta.toString());

one = new Uint8ClampedArray([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -339,6 +501,30 @@ load("testsrc/assert.js");
try { new Float32Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -1.073741828E9", msg);

msg = null;
try { new Float32Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Float32Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Float32Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -4 out of range", msg);

msg = null;
try { new Float32Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 12 out of range", msg);

msg = null;
try { new Float32Array(new ArrayBuffer(4), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 must be a multiple of the byte size 4", msg);

msg = null;
try { new Float32Array(new ArrayBuffer(5), 4); } catch (e) { msg = e.toString();}
assertEquals("RangeError: used buffer length 1 must be a multiple of the byte size 4", msg);

one = new Float32Array([7]);
assertEquals("7", one.toString());

Expand Down Expand Up @@ -385,6 +571,30 @@ load("testsrc/assert.js");
try { new Float64Array(-268435457); } catch (e) { msg = e.toString();}
assertEquals("RangeError: Negative array length -2.147483656E9", msg);

msg = null;
try { new Float64Array(new ArrayBuffer(), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset -1 out of range", msg);

msg = null;
try { new Float64Array(new ArrayBuffer(2), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 out of range", msg);

msg = null;
try { new Float64Array(new ArrayBuffer(), 0, -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length -8 out of range", msg);

msg = null;
try { new Float64Array(new ArrayBuffer(2), 0, 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: length 24 out of range", msg);

msg = null;
try { new Float64Array(new ArrayBuffer(4), 3); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset 3 must be a multiple of the byte size 8", msg);

msg = null;
try { new Float64Array(new ArrayBuffer(9), 8); } catch (e) { msg = e.toString();}
assertEquals("RangeError: used buffer length 1 must be a multiple of the byte size 8", msg);

one = new Float64Array([7]);
assertEquals("7", one.toString());

Expand Down
8 changes: 4 additions & 4 deletions tests/testsrc/jstests/harmony/typed-array-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ for (var t = 0; t < types.length; t++) {

var msg = null;
try { arr.set([0], -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset out of range", msg);
assertEquals("RangeError: offset -1 out of range", msg);

arr.set([], 8);
assertEquals("8,9,0,1,2,3,0,0", arr.toString());

msg = null;
try { arr.set([], 9); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset out of range", msg);
assertEquals("RangeError: offset 9 out of range", msg);

arr.set([1], 7);
assertEquals("8,9,0,1,2,3,0,1", arr.toString());
Expand Down Expand Up @@ -63,14 +63,14 @@ for (var t = 0; t < types.length; t++) {

var msg = null;
try { arr.set(new type([0]), -1); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset out of range", msg);
assertEquals("RangeError: offset -1 out of range", msg);

arr.set(new type([]), 8);
assertEquals("8,9,0,1,2,3,0,0", arr.toString());

msg = null;
try { arr.set(new type([]), 9); } catch (e) { msg = e.toString();}
assertEquals("RangeError: offset out of range", msg);
assertEquals("RangeError: offset 9 out of range", msg);

arr.set(new type([1]), 7);
assertEquals("8,9,0,1,2,3,0,1", arr.toString());
Expand Down

0 comments on commit 65bb240

Please sign in to comment.