function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) {
function ConstructByArrayBuffer(obj, buffer, byteOffset, length) {
- var offset = IS_UNDEFINED(byteOffset) ? 0 : TO_POSITIVE_INTEGER(byteOffset);
+ var offset = ToPositiveInteger(byteOffset, "invalid_typed_array_length")
if (offset % elementSize !== 0) {
throw MakeRangeError("invalid_typed_array_alignment",
newByteLength = bufferByteLength - offset;
newLength = newByteLength / elementSize;
} else {
- var newLength = TO_POSITIVE_INTEGER(length);
+ var newLength = ToPositiveInteger(length, "invalid_typed_array_length");
newByteLength = newLength * elementSize;
}
if (offset + newByteLength > bufferByteLength) {
}
function ConstructByLength(obj, length) {
- var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length);
+ var l = ToPositiveInteger(length, "invalid_typed_array_length");
var byteLength = l * elementSize;
var buffer = new global.ArrayBuffer(byteLength);
%TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength);
function ConstructByArrayLike(obj, arrayLike) {
var length = arrayLike.length;
- var l = IS_UNDEFINED(length) ? 0 : TO_POSITIVE_INTEGER(length);
+ var l = ToPositiveInteger(length, "invalid_typed_array_length");
var byteLength = l * elementSize;
var buffer = new $ArrayBuffer(byteLength);
%TypedArrayInitialize(obj, arrayId, buffer, 0, byteLength);
}
function TypedArraySet(obj, offset) {
- var intOffset = IS_UNDEFINED(offset) ? 0 : TO_POSITIVE_INTEGER(offset);
+ var intOffset = IS_UNDEFINED(offset) ? 0 : TO_INTEGER(offset);
+ if (intOffset < 0) {
+ throw MakeTypeError("typed_array_set_negative_offset");
+ }
if (%TypedArraySetFastCases(this, obj, intOffset))
return;
throw MakeTypeError('data_view_not_array_buffer', []);
}
var bufferByteLength = %ArrayBufferGetByteLength(buffer);
- var offset = IS_UNDEFINED(byteOffset) ? 0 : TO_POSITIVE_INTEGER(byteOffset);
+ var offset = ToPositiveInteger(byteOffset, 'invalid_data_view_offset');
if (offset > bufferByteLength) {
- throw MakeRangeError('invalid_data_view_offset', []);
+ throw MakeRangeError('invalid_data_view_offset');
}
var length = IS_UNDEFINED(byteLength) ?
- bufferByteLength - offset : TO_POSITIVE_INTEGER(byteLength);
- if (offset + length > bufferByteLength) {
+ bufferByteLength - offset : TO_INTEGER(byteLength);
+ if (length < 0 || offset + length > bufferByteLength) {
throw new MakeRangeError('invalid_data_view_length');
}
%DataViewInitialize(this, buffer, offset, length);
return %DataViewGetByteLength(this);
}
+function ToPositiveDataViewOffset(offset) {
+ return ToPositiveInteger(offset, 'invalid_data_view_accessor_offset');
+}
+
function DataViewGetInt8(offset, little_endian) {
if (!IS_DATAVIEW(this)) {
throw MakeTypeError('incompatible_method_reciever',
['DataView.getInt8', this]);
}
- return %DataViewGetInt8(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ return %DataViewGetInt8(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetInt8(offset, value, little_endian) {
['DataView.setInt8', this]);
}
%DataViewSetInt8(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
throw MakeTypeError('incompatible_method_reciever',
['DataView.getUint8', this]);
}
- return %DataViewGetUint8(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ return %DataViewGetUint8(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetUint8(offset, value, little_endian) {
['DataView.setUint8', this]);
}
%DataViewSetUint8(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
throw MakeTypeError('incompatible_method_reciever',
['DataView.getInt16', this]);
}
- return %DataViewGetInt16(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ return %DataViewGetInt16(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetInt16(offset, value, little_endian) {
['DataView.setInt16', this]);
}
%DataViewSetInt16(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
throw MakeTypeError('incompatible_method_reciever',
['DataView.getUint16', this]);
}
- return %DataViewGetUint16(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ return %DataViewGetUint16(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetUint16(offset, value, little_endian) {
['DataView.setUint16', this]);
}
%DataViewSetUint16(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
throw MakeTypeError('incompatible_method_reciever',
['DataView.getInt32', this]);
}
- return %DataViewGetInt32(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ return %DataViewGetInt32(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetInt32(offset, value, little_endian) {
['DataView.setInt32', this]);
}
%DataViewSetInt32(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
throw MakeTypeError('incompatible_method_reciever',
['DataView.getUint32', this]);
}
- return %DataViewGetUint32(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ return %DataViewGetUint32(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetUint32(offset, value, little_endian) {
['DataView.setUint32', this]);
}
%DataViewSetUint32(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
throw MakeTypeError('incompatible_method_reciever',
['DataView.getFloat32', this]);
}
- return %DataViewGetFloat32(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ return %DataViewGetFloat32(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetFloat32(offset, value, little_endian) {
['DataView.setFloat32', this]);
}
%DataViewSetFloat32(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
throw MakeTypeError('incompatible_method_reciever',
['DataView.getFloat64', this]);
}
- return %DataViewGetFloat64(this, TO_POSITIVE_INTEGER(offset), !!little_endian);
+ offset = TO_INTEGER(offset);
+ if (offset < 0) {
+ throw MakeRangeError("invalid_data_view_accessor_offset");
+ }
+ return %DataViewGetFloat64(this,
+ ToPositiveDataViewOffset(offset),
+ !!little_endian);
}
function DataViewSetFloat64(offset, value, little_endian) {
throw MakeTypeError('incompatible_method_reciever',
['DataView.setFloat64', this]);
}
+ offset = TO_INTEGER(offset);
+ if (offset < 0) {
+ throw MakeRangeError("invalid_data_view_accessor_offset");
+ }
%DataViewSetFloat64(this,
- TO_POSITIVE_INTEGER(offset),
+ ToPositiveDataViewOffset(offset),
TO_NUMBER_INLINE(value),
!!little_endian);
}
function TestArrayBufferCreation() {
TestByteLength(1, 1);
TestByteLength(256, 256);
- TestByteLength(-10, 0);
TestByteLength(2.567, 2);
- TestByteLength(-2.567, 0);
TestByteLength("abc", 0);
TestByteLength(0, 0);
+ assertThrows(function() { new ArrayBuffer(-10); }, RangeError);
+ assertThrows(function() { new ArrayBuffer(-2.567); }, RangeError);
+
/* TODO[dslomov]: Reenable the test
assertThrows(function() {
var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF)
TestSlice(0, 0, 1, 20);
TestSlice(100, 100, 0, 100);
TestSlice(100, 100, 0, 1000);
+
TestSlice(0, 100, 5, 1);
TestSlice(1, 100, -11, -10);
TestSlice(10, 100, 90, "100");
TestSlice(10, 100, "90", "100");
- TestSlice(0, 100, 90, "abc");
+ TestSlice(0, 100, 90, "abc");
TestSlice(10, 100, "abc", 10);
TestSlice(10, 100, 0.96, 10.96);
TestSlice(10, 100, 0.01, 10.01);
TestSlice(10, 100, 0.01, 10.96);
-
TestSlice(10, 100, 90);
TestSlice(10, 100, -10);
}
assertSame(256, d3c.byteOffset);
assertSame(0, d3c.byteLength);
- // weird args
- var d4 = new DataView(ab, -1);
+ var d4 = new DataView(ab, 1, 3.1415926);
assertSame(ab, d4.buffer);
- assertSame(0, d4.byteOffset);
- assertSame(256, d4.byteLength);
-
- var d5 = new DataView(ab, 1, -1);
- assertSame(ab, d5.buffer);
- assertSame(1, d5.byteOffset);
- assertSame(0, d5.byteLength);
-
- var d6 = new DataView(ab, 1, 3.1415926);
- assertSame(ab, d6.buffer);
- assertSame(1, d6.byteOffset);
- assertSame(3, d6.byteLength);
+ assertSame(1, d4.byteOffset);
+ assertSame(3, d4.byteLength);
// error cases
+ assertThrows(function() { new DataView(ab, -1); }, RangeError);
+ assertThrows(function() { new DataView(ab, 1, -1); }, RangeError);
assertThrows(function() { new DataView(); }, TypeError);
assertThrows(function() { new DataView([]); }, TypeError);
assertThrows(function() { new DataView(ab, 257); }, RangeError);