From 65bb24080d5e7d965cb334baa9e39844e422abbc Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Sat, 21 Dec 2024 13:45:30 +0100 Subject: [PATCH] use messages for typedarray ctor --- .../typedarrays/NativeTypedArrayView.java | 23 +- .../javascript/resources/Messages.properties | 11 +- .../jstests/harmony/typed-array-ctor.js | 210 ++++++++++++++++++ .../jstests/harmony/typed-array-set.js | 8 +- 4 files changed, 240 insertions(+), 12 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/typedarrays/NativeTypedArrayView.java b/rhino/src/main/java/org/mozilla/javascript/typedarrays/NativeTypedArrayView.java index 56264f0d71..3e19331205 100644 --- a/rhino/src/main/java/org/mozilla/javascript/typedarrays/NativeTypedArrayView.java +++ b/rhino/src/main/java/org/mozilla/javascript/typedarrays/NativeTypedArrayView.java @@ -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); @@ -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); } @@ -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) { diff --git a/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties b/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties index fc5b9e2750..38c84c1c5f 100644 --- a/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties +++ b/rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties @@ -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 diff --git a/tests/testsrc/jstests/harmony/typed-array-ctor.js b/tests/testsrc/jstests/harmony/typed-array-ctor.js index 70f8f3e45b..899ec1acf2 100644 --- a/tests/testsrc/jstests/harmony/typed-array-ctor.js +++ b/tests/testsrc/jstests/harmony/typed-array-ctor.js @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); diff --git a/tests/testsrc/jstests/harmony/typed-array-set.js b/tests/testsrc/jstests/harmony/typed-array-set.js index 833849d6e9..5097e7f205 100644 --- a/tests/testsrc/jstests/harmony/typed-array-set.js +++ b/tests/testsrc/jstests/harmony/typed-array-set.js @@ -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()); @@ -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());