Commit 56eb46e0 authored by antonm@chromium.org's avatar antonm@chromium.org

Fix a special case (zero length result array).

Review URL: http://codereview.chromium.org/669075

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4026 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent bc890ec2
...@@ -251,6 +251,16 @@ static Object* AllocateJSArray() { ...@@ -251,6 +251,16 @@ static Object* AllocateJSArray() {
} }
static Object* AllocateEmptyJSArray() {
Object* result = AllocateJSArray();
if (result->IsFailure()) return result;
JSArray* result_array = JSArray::cast(result);
result_array->set_length(Smi::FromInt(0));
result_array->set_elements(Heap::empty_fixed_array());
return result_array;
}
static void CopyElements(AssertNoAllocation* no_gc, static void CopyElements(AssertNoAllocation* no_gc,
FixedArray* dst, FixedArray* dst,
int dst_index, int dst_index,
...@@ -535,8 +545,8 @@ BUILTIN(ArraySlice) { ...@@ -535,8 +545,8 @@ BUILTIN(ArraySlice) {
// Calculate the length of result array. // Calculate the length of result array.
int result_len = final - k; int result_len = final - k;
if (result_len < 0) { if (result_len <= 0) {
result_len = 0; return AllocateEmptyJSArray();
} }
Object* result = AllocateJSArray(); Object* result = AllocateJSArray();
...@@ -606,6 +616,9 @@ BUILTIN(ArraySplice) { ...@@ -606,6 +616,9 @@ BUILTIN(ArraySplice) {
} }
} }
int actualDeleteCount = Min(Max(deleteCount, 0), len - actualStart); int actualDeleteCount = Min(Max(deleteCount, 0), len - actualStart);
if (actualDeleteCount == 0) {
return AllocateEmptyJSArray();
}
// Allocate result array. // Allocate result array.
Object* result = AllocateJSArray(); Object* result = AllocateJSArray();
......
...@@ -36,6 +36,17 @@ ...@@ -36,6 +36,17 @@
})(); })();
// Check various variants of empty array's slicing.
(function() {
for (var i = 0; i < 7; i++) {
assertEquals([], [].slice(0, 0));
assertEquals([], [].slice(1, 0));
assertEquals([], [].slice(0, 1));
assertEquals([], [].slice(-1, 0));
}
})();
// Check various forms of arguments omission. // Check various forms of arguments omission.
(function() { (function() {
var array = new Array(7); var array = new Array(7);
......
...@@ -42,6 +42,17 @@ ...@@ -42,6 +42,17 @@
})(); })();
// Check various variants of empty array's splicing.
(function() {
for (var i = 0; i < 7; i++) {
assertEquals([], [].splice(0, 0));
assertEquals([], [].splice(1, 0));
assertEquals([], [].splice(0, 1));
assertEquals([], [].splice(-1, 0));
}
})();
// Check various forms of arguments omission. // Check various forms of arguments omission.
(function() { (function() {
var array; var array;
......
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