Commit e6e0ee07 authored by dslomov@chromium.org's avatar dslomov@chromium.org

Update typed arrays behavior to match ES6 rev 15. Remove TO_POSITIVE_INTEGER...

Update typed arrays behavior to match ES6 rev 15. Remove TO_POSITIVE_INTEGER and throw on negative length arguments.

R=rossberg@chromium.org

Review URL: https://codereview.chromium.org/17572009

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@15298 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 8db56239
......@@ -31,12 +31,12 @@ var $ArrayBuffer = global.ArrayBuffer;
// -------------------------------------------------------------------
function ArrayBufferConstructor(byteLength) { // length = 1
function ArrayBufferConstructor(length) { // length = 1
if (%_IsConstructCall()) {
var l = TO_POSITIVE_INTEGER(byteLength);
%ArrayBufferInitialize(this, l);
var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length');
%ArrayBufferInitialize(this, byteLength);
} else {
return new $ArrayBuffer(byteLength);
return new $ArrayBuffer(length);
}
}
......@@ -70,6 +70,9 @@ function ArrayBufferSlice(start, end) {
fin = MathMin(relativeEnd, this.byteLength);
}
if (fin < first) {
fin = first;
}
var newLen = fin - first;
// TODO(dslomov): implement inheritance
var result = new $ArrayBuffer(newLen);
......
......@@ -145,7 +145,6 @@ const kBoundArgumentsStartIndex = 2;
macro NUMBER_IS_NAN(arg) = (!%_IsSmi(%IS_VAR(arg)) && !(arg == arg));
macro NUMBER_IS_FINITE(arg) = (%_IsSmi(%IS_VAR(arg)) || ((arg == arg) && (arg != 1/0) && (arg != -1/0)));
macro TO_INTEGER(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : %NumberToInteger(ToNumber(arg)));
macro TO_POSITIVE_INTEGER(arg) = (%_IsSmi(%IS_VAR(arg)) ? (arg > 0 ? arg : 0) : %NumberToPositiveInteger(ToNumber(arg)));
macro TO_INTEGER_MAP_MINUS_ZERO(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : %NumberToIntegerMapMinusZero(ToNumber(arg)));
macro TO_INT32(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : (arg >> 0));
macro TO_UINT32(arg) = (arg >>> 0);
......
......@@ -112,13 +112,15 @@ var kMessages = {
// RangeError
invalid_array_length: ["Invalid array length"],
invalid_array_buffer_length: ["Invalid array buffer length"],
invalid_typed_array_offset: ["Start offset is too large"],
invalid_typed_array_length: ["Length is too large"],
invalid_typed_array_offset: ["Start offset is too large:"],
invalid_typed_array_length: ["Invalid typed array length"],
invalid_typed_array_alignment: ["%0", "of", "%1", "should be a multiple of", "%3"],
typed_array_set_source_too_large:
["Source is too large"],
typed_array_set_negative_offset:
["Start offset is negative"],
invalid_data_view_offset: ["Start offset is outside the bounds of the buffer"],
invalid_data_view_length: ["Length is too large"],
invalid_data_view_length: ["Invalid data view length"],
invalid_data_view_accessor_offset:
["Offset is outside the bounds of the DataView"],
......
......@@ -658,7 +658,6 @@ function DefaultNumber(x) {
throw %MakeTypeError('cannot_convert_to_primitive', []);
}
// ECMA-262, section 8.6.2.6, page 28.
function DefaultString(x) {
var toString = x.toString;
......@@ -676,6 +675,12 @@ function DefaultString(x) {
throw %MakeTypeError('cannot_convert_to_primitive', []);
}
function ToPositiveInteger(x, rangeErrorName) {
var i = TO_INTEGER(x);
if (i < 0) throw %MakeRangeError(rangeErrorName);
return i;
}
// NOTE: Setting the prototype for Array must take place as early as
// possible due to code generation for array literals. When
......
This diff is collapsed.
......@@ -64,8 +64,7 @@ function checkGet(func, index, expected, littleEndian) {
function doGet() {
return view["get" + func](index, littleEndian);
}
if (index < 0) index = 0;
if (index + getElementSize(func) - 1 < view.byteLength)
if (index >=0 && index + getElementSize(func) - 1 < view.byteLength)
assertSame(expected, doGet());
else
assertThrows(doGet, RangeError);
......@@ -75,9 +74,8 @@ function checkSet(func, index, value, littleEndian) {
function doSet() {
view["set" + func](index, value, littleEndian);
}
actualIndex = index < 0 ? 0 : index;
if (actualIndex >= 0 &&
actualIndex + getElementSize(func) - 1 < view.byteLength) {
if (index >= 0 &&
index + getElementSize(func) - 1 < view.byteLength) {
assertSame(undefined, doSet());
checkGet(func, index, value, littleEndian);
} else {
......
......@@ -35,14 +35,15 @@ function TestByteLength(param, expectedByteLength) {
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)
......@@ -89,6 +90,7 @@ function TestArrayBufferSlice() {
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);
......@@ -99,7 +101,7 @@ function TestArrayBufferSlice() {
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);
......@@ -107,7 +109,6 @@ function TestArrayBufferSlice() {
TestSlice(10, 100, 0.01, 10.01);
TestSlice(10, 100, 0.01, 10.96);
TestSlice(10, 100, 90);
TestSlice(10, 100, -10);
}
......@@ -489,24 +490,15 @@ function TestDataViewConstructor() {
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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment