Revision: 6540
Author: [email protected]
Date: Mon Jan 31 06:54:53 2011
Log: ArraySplice builtin should return empty array and not alter receiver
if invoked with no arguments.
Review URL: http://codereview.chromium.org/6357025
http://code.google.com/p/v8/source/detail?r=6540
Modified:
/branches/bleeding_edge/src/array.js
/branches/bleeding_edge/src/builtins.cc
/branches/bleeding_edge/test/mjsunit/array-splice.js
=======================================
--- /branches/bleeding_edge/src/array.js Mon Jan 10 03:44:54 2011
+++ /branches/bleeding_edge/src/array.js Mon Jan 31 06:54:53 2011
@@ -603,16 +603,17 @@
}
// SpiderMonkey, TraceMonkey and JSC treat the case where no delete
count is
- // given differently from when an undefined delete count is given.
+ // given as a request to delete all the elements from the start.
+ // And it differs from the case of undefined delete count.
// This does not follow ECMA-262, but we do the same for
// compatibility.
var del_count = 0;
- if (num_arguments > 1) {
+ if (num_arguments == 1) {
+ del_count = len - start_i;
+ } else {
del_count = TO_INTEGER(delete_count);
if (del_count < 0) del_count = 0;
if (del_count > len - start_i) del_count = len - start_i;
- } else {
- del_count = len - start_i;
}
var deleted_elements = [];
=======================================
--- /branches/bleeding_edge/src/builtins.cc Fri Jan 28 07:07:04 2011
+++ /branches/bleeding_edge/src/builtins.cc Mon Jan 31 06:54:53 2011
@@ -750,19 +750,26 @@
: Min(relative_start, len);
// SpiderMonkey, TraceMonkey and JSC treat the case where no delete
count is
- // given differently from when an undefined delete count is given.
+ // given as a request to delete all the elements from the start.
+ // And it differs from the case of undefined delete count.
// This does not follow ECMA-262, but we do the same for
// compatibility.
- int delete_count = len;
- if (n_arguments > 1) {
- Object* arg2 = args[2];
- if (arg2->IsSmi()) {
- delete_count = Smi::cast(arg2)->value();
- } else {
- return CallJsBuiltin("ArraySplice", args);
- }
- }
- int actual_delete_count = Min(Max(delete_count, 0), len - actual_start);
+ int actual_delete_count;
+ if (n_arguments == 1) {
+ ASSERT(len - actual_start >= 0);
+ actual_delete_count = len - actual_start;
+ } else {
+ int value = 0; // ToInteger(undefined) == 0
+ if (n_arguments > 1) {
+ Object* arg2 = args[2];
+ if (arg2->IsSmi()) {
+ value = Smi::cast(arg2)->value();
+ } else {
+ return CallJsBuiltin("ArraySplice", args);
+ }
+ }
+ actual_delete_count = Min(Max(value, 0), len - actual_start);
+ }
JSArray* result_array = NULL;
if (actual_delete_count == 0) {
=======================================
--- /branches/bleeding_edge/test/mjsunit/array-splice.js Tue Dec 7
03:01:02 2010
+++ /branches/bleeding_edge/test/mjsunit/array-splice.js Mon Jan 31
06:54:53 2011
@@ -339,6 +339,20 @@
})();
+// Check the case of JS builtin .splice()
+(function() {
+ for (var i = 0; i < 7; i++) {
+ var array = [1, 2, 3, 4];
+ Array.prototype[3] = 'foo'; // To force JS builtin.
+
+ var spliced = array.splice();
+
+ assertEquals([], spliced);
+ assertEquals([1, 2, 3, 4], array);
+ }
+})();
+
+
// Check the behaviour when approaching maximal values for length.
(function() {
for (var i = 0; i < 7; i++) {
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev