Add new Harmony methods to Array.prototype object.

Array.prototype.find
Array.prototype.findIndex

http://people.mozilla.org/~jorendorff/es6-draft.html

BUG=v8:2776,v8:2777
TEST=mjsunit/harmony/array-find,mjsunit/harmony/array-findindex
R=mstarzinger@chromium.org

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

Patch from Ioseb Dzmanashvili <ioseb.dzmanashvili@gmail.com>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@16025 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent d2c443b7
......@@ -2085,6 +2085,11 @@ bool Genesis::InstallExperimentalNatives() {
"native harmony-string.js") == 0) {
if (!CompileExperimentalBuiltin(isolate(), i)) return false;
}
if (FLAG_harmony_arrays &&
strcmp(ExperimentalNatives::GetScriptName(i).start(),
"native harmony-array.js") == 0) {
if (!CompileExperimentalBuiltin(isolate(), i)) return false;
}
}
InstallExperimentalNativeFunctions();
......
......@@ -174,6 +174,7 @@ DEFINE_bool(harmony_iteration, false, "enable harmony iteration (for-of)")
DEFINE_bool(harmony_numeric_literals, false,
"enable harmony numeric literals (0o77, 0b11)")
DEFINE_bool(harmony_strings, false, "enable harmony string")
DEFINE_bool(harmony_arrays, false, "enable harmony arrays")
DEFINE_bool(harmony, false, "enable all harmony features (except typeof)")
DEFINE_implication(harmony, harmony_scoping)
DEFINE_implication(harmony, harmony_modules)
......@@ -185,6 +186,7 @@ DEFINE_implication(harmony, harmony_generators)
DEFINE_implication(harmony, harmony_iteration)
DEFINE_implication(harmony, harmony_numeric_literals)
DEFINE_implication(harmony, harmony_strings)
DEFINE_implication(harmony, harmony_arrays)
DEFINE_implication(harmony_modules, harmony_scoping)
DEFINE_implication(harmony_observation, harmony_collections)
// TODO[dslomov] add harmony => harmony_typed_arrays
......
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'use strict';
// This file relies on the fact that the following declaration has been made
// in runtime.js:
// var $Array = global.Array;
// -------------------------------------------------------------------
// ES6 draft 07-15-13, section 15.4.3.23
function ArrayFind(predicate /* thisArg */) { // length == 1
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
throw MakeTypeError("called_on_null_or_undefined",
["Array.prototype.find"]);
}
var array = ToObject(this);
var length = ToInteger(array.length);
if (!IS_SPEC_FUNCTION(predicate)) {
throw MakeTypeError('called_non_callable', [predicate]);
}
var thisArg;
if (%_ArgumentsLength() > 1) {
thisArg = %_Arguments(1);
}
if (IS_NULL_OR_UNDEFINED(thisArg)) {
thisArg = %GetDefaultReceiver(predicate) || thisArg;
} else if (!IS_SPEC_OBJECT(thisArg) && %IsClassicModeFunction(predicate)) {
thisArg = ToObject(thisArg);
}
for (var i = 0; i < length; i++) {
if (i in array) {
var element = array[i];
if (%_CallFunction(thisArg, element, i, array, predicate)) {
return element;
}
}
}
return;
}
// ES6 draft 07-15-13, section 15.4.3.24
function ArrayFindIndex(predicate /* thisArg */) { // length == 1
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
throw MakeTypeError("called_on_null_or_undefined",
["Array.prototype.findIndex"]);
}
var array = ToObject(this);
var length = ToInteger(array.length);
if (!IS_SPEC_FUNCTION(predicate)) {
throw MakeTypeError('called_non_callable', [predicate]);
}
var thisArg;
if (%_ArgumentsLength() > 1) {
thisArg = %_Arguments(1);
}
if (IS_NULL_OR_UNDEFINED(thisArg)) {
thisArg = %GetDefaultReceiver(predicate) || thisArg;
} else if (!IS_SPEC_OBJECT(thisArg) && %IsClassicModeFunction(predicate)) {
thisArg = ToObject(thisArg);
}
for (var i = 0; i < length; i++) {
if (i in array) {
var element = array[i];
if (%_CallFunction(thisArg, element, i, array, predicate)) {
return i;
}
}
}
return -1;
}
// -------------------------------------------------------------------
function HarmonyArrayExtendArrayPrototype() {
%CheckIsBootstrapping();
// Set up the non-enumerable functions on the Array prototype object.
InstallFunctions($Array.prototype, DONT_ENUM, $Array(
"find", ArrayFind,
"findIndex", ArrayFindIndex
));
}
HarmonyArrayExtendArrayPrototype();
\ No newline at end of file
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Flags: --harmony-arrays
assertEquals(1, Array.prototype.find.length);
var a = [21, 22, 23, 24];
assertEquals(undefined, a.find(function() { return false; }));
assertEquals(21, a.find(function() { return true; }));
assertEquals(undefined, a.find(function(val) { return 121 === val; }));
assertEquals(24, a.find(function(val) { return 24 === val; }));
assertEquals(23, a.find(function(val) { return 23 === val; }), null);
assertEquals(22, a.find(function(val) { return 22 === val; }), undefined);
//
// Test predicate is not called when array is empty
//
(function() {
var a = [];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
a.find(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assertEquals(-1, l);
assertEquals(-1, o);
assertEquals(-1, v);
assertEquals(-1, k);
})();
//
// Test predicate is called with correct argumetns
//
(function() {
var a = ["b"];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var found = a.find(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assertArrayEquals(a, o);
assertEquals(a.length, l);
assertEquals("b", v);
assertEquals(0, k);
assertEquals(undefined, found);
})();
//
// Test predicate is called array.length times
//
(function() {
var a = [1, 2, 3, 4, 5];
var l = 0;
var found = a.find(function() {
l++;
return false;
});
assertEquals(a.length, l);
assertEquals(undefined, found);
})();
//
// Test Array.prototype.find works with String
//
(function() {
var a = "abcd";
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var found = Array.prototype.find.call(a, function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return false;
});
assertEquals(a, o);
assertEquals(a.length, l);
assertEquals("d", v);
assertEquals(3, k);
assertEquals(undefined, found);
found = Array.prototype.find.apply(a, [function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return true;
}]);
assertEquals(a, o);
assertEquals(a.length, l);
assertEquals("a", v);
assertEquals(0, k);
assertEquals("a", found);
})();
//
// Test Array.prototype.find works with exotic object
//
(function() {
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var a = {
prop1: "val1",
prop2: "val2",
isValid: function() {
return this.prop1 === "val1" && this.prop2 === "val2";
}
};
Array.prototype.push.apply(a, [30, 31, 32]);
var found = Array.prototype.find.call(a, function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return !obj.isValid();
});
assertArrayEquals(a, o);
assertEquals(3, l);
assertEquals(32, v);
assertEquals(2, k);
assertEquals(undefined, found);
})();
//
// Test array modifications
//
(function() {
var a = [1, 2, 3];
var found = a.find(function(val) { a.push(val); return false; });
assertArrayEquals([1, 2, 3, 1, 2, 3], a);
assertEquals(6, a.length);
assertEquals(undefined, found);
a = [1, 2, 3];
found = a.find(function(val, key) { a[key] = ++val; return false; });
assertArrayEquals([2, 3, 4], a);
assertEquals(3, a.length);
assertEquals(undefined, found);
})();
//
// Test predicate is only called for existing elements
//
(function() {
var a = new Array(30);
a[11] = 21;
a[7] = 10;
a[29] = 31;
var count = 0;
a.find(function() { count++; return false; });
assertEquals(3, count);
})();
//
// Test thisArg
//
(function() {
// Test String as a thisArg
var found = [1, 2, 3].find(function(val, key) {
return this.charAt(Number(key)) === String(val);
}, "321");
assertEquals(2, found);
// Test object as a thisArg
var thisArg = {
elementAt: function(key) {
return this[key];
}
};
Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
found = ["a", "b", "c"].find(function(val, key) {
return this.elementAt(key) === val;
}, thisArg);
assertEquals("b", found);
})();
// Test exceptions
assertThrows('Array.prototype.find.call(null, function() { })',
TypeError);
assertThrows('Array.prototype.find.call(undefined, function() { })',
TypeError);
assertThrows('Array.prototype.find.apply(null, function() { }, [])',
TypeError);
assertThrows('Array.prototype.find.apply(undefined, function() { }, [])',
TypeError);
assertThrows('[].find(null)', TypeError);
assertThrows('[].find(undefined)', TypeError);
assertThrows('[].find(0)', TypeError);
assertThrows('[].find(true)', TypeError);
assertThrows('[].find(false)', TypeError);
assertThrows('[].find("")', TypeError);
assertThrows('[].find({})', TypeError);
assertThrows('[].find([])', TypeError);
assertThrows('[].find(/\d+/)', TypeError);
assertThrows('Array.prototype.find.call({}, null)', TypeError);
assertThrows('Array.prototype.find.call({}, undefined)', TypeError);
assertThrows('Array.prototype.find.call({}, 0)', TypeError);
assertThrows('Array.prototype.find.call({}, true)', TypeError);
assertThrows('Array.prototype.find.call({}, false)', TypeError);
assertThrows('Array.prototype.find.call({}, "")', TypeError);
assertThrows('Array.prototype.find.call({}, {})', TypeError);
assertThrows('Array.prototype.find.call({}, [])', TypeError);
assertThrows('Array.prototype.find.call({}, /\d+/)', TypeError);
assertThrows('Array.prototype.find.apply({}, null, [])', TypeError);
assertThrows('Array.prototype.find.apply({}, undefined, [])', TypeError);
assertThrows('Array.prototype.find.apply({}, 0, [])', TypeError);
assertThrows('Array.prototype.find.apply({}, true, [])', TypeError);
assertThrows('Array.prototype.find.apply({}, false, [])', TypeError);
assertThrows('Array.prototype.find.apply({}, "", [])', TypeError);
assertThrows('Array.prototype.find.apply({}, {}, [])', TypeError);
assertThrows('Array.prototype.find.apply({}, [], [])', TypeError);
assertThrows('Array.prototype.find.apply({}, /\d+/, [])', TypeError);
\ No newline at end of file
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Flags: --harmony-arrays
assertEquals(1, Array.prototype.findIndex.length);
var a = [21, 22, 23, 24];
assertEquals(-1, a.findIndex(function() { return false; }));
assertEquals(-1, a.findIndex(function(val) { return 121 === val; }));
assertEquals(0, a.findIndex(function() { return true; }));
assertEquals(1, a.findIndex(function(val) { return 22 === val; }), undefined);
assertEquals(2, a.findIndex(function(val) { return 23 === val; }), null);
assertEquals(3, a.findIndex(function(val) { return 24 === val; }));
//
// Test predicate is not called when array is empty
//
(function() {
var a = [];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
a.findIndex(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assertEquals(-1, l);
assertEquals(-1, o);
assertEquals(-1, v);
assertEquals(-1, k);
})();
//
// Test predicate is called with correct argumetns
//
(function() {
var a = ["b"];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var index = a.findIndex(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assertArrayEquals(a, o);
assertEquals(a.length, l);
assertEquals("b", v);
assertEquals(0, k);
assertEquals(-1, index);
})();
//
// Test predicate is called array.length times
//
(function() {
var a = [1, 2, 3, 4, 5];
var l = 0;
a.findIndex(function() {
l++;
return false;
});
assertEquals(a.length, l);
})();
//
// Test Array.prototype.findIndex works with String
//
(function() {
var a = "abcd";
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return false;
});
assertEquals(a, o);
assertEquals(a.length, l);
assertEquals("d", v);
assertEquals(3, k);
assertEquals(-1, index);
index = Array.prototype.findIndex.apply(a, [function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return true;
}]);
assertEquals(a, o);
assertEquals(a.length, l);
assertEquals("a", v);
assertEquals(0, k);
assertEquals(0, index);
})();
//
// Test Array.prototype.findIndex works with exotic object
//
(function() {
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var a = {
prop1: "val1",
prop2: "val2",
isValid: function() {
return this.prop1 === "val1" && this.prop2 === "val2";
}
};
Array.prototype.push.apply(a, [30, 31, 32]);
var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return !obj.isValid();
});
assertArrayEquals(a, o);
assertEquals(3, l);
assertEquals(32, v);
assertEquals(2, k);
assertEquals(-1, index);
})();
//
// Test array modifications
//
(function() {
var a = [1, 2, 3];
a.findIndex(function(val) { a.push(val); return false; });
assertArrayEquals([1, 2, 3, 1, 2, 3], a);
assertEquals(6, a.length);
a = [1, 2, 3];
a.findIndex(function(val, key) { a[key] = ++val; return false; });
assertArrayEquals([2, 3, 4], a);
assertEquals(3, a.length);
})();
//
// Test predicate is only called for existing elements
//
(function() {
var a = new Array(30);
a[11] = 21;
a[7] = 10;
a[29] = 31;
var count = 0;
a.findIndex(function() { count++; return false; });
assertEquals(3, count);
})();
//
// Test thisArg
//
(function() {
// Test String as a thisArg
var index = [1, 2, 3].findIndex(function(val, key) {
return this.charAt(Number(key)) === String(val);
}, "321");
assertEquals(1, index);
// Test object as a thisArg
var thisArg = {
elementAt: function(key) {
return this[key];
}
};
Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
index = ["a", "b", "c"].findIndex(function(val, key) {
return this.elementAt(key) === val;
}, thisArg);
assertEquals(1, index);
})();
// Test exceptions
assertThrows('Array.prototype.findIndex.call(null, function() { })',
TypeError);
assertThrows('Array.prototype.findIndex.call(undefined, function() { })',
TypeError);
assertThrows('Array.prototype.findIndex.apply(null, function() { }, [])',
TypeError);
assertThrows('Array.prototype.findIndex.apply(undefined, function() { }, [])',
TypeError);
assertThrows('[].findIndex(null)', TypeError);
assertThrows('[].findIndex(undefined)', TypeError);
assertThrows('[].findIndex(0)', TypeError);
assertThrows('[].findIndex(true)', TypeError);
assertThrows('[].findIndex(false)', TypeError);
assertThrows('[].findIndex("")', TypeError);
assertThrows('[].findIndex({})', TypeError);
assertThrows('[].findIndex([])', TypeError);
assertThrows('[].findIndex(/\d+/)', TypeError);
assertThrows('Array.prototype.findIndex.call({}, null)', TypeError);
assertThrows('Array.prototype.findIndex.call({}, undefined)', TypeError);
assertThrows('Array.prototype.findIndex.call({}, 0)', TypeError);
assertThrows('Array.prototype.findIndex.call({}, true)', TypeError);
assertThrows('Array.prototype.findIndex.call({}, false)', TypeError);
assertThrows('Array.prototype.findIndex.call({}, "")', TypeError);
assertThrows('Array.prototype.findIndex.call({}, {})', TypeError);
assertThrows('Array.prototype.findIndex.call({}, [])', TypeError);
assertThrows('Array.prototype.findIndex.call({}, /\d+/)', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, null, [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, undefined, [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, 0, [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, true, [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, false, [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, "", [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, {}, [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, [], [])', TypeError);
assertThrows('Array.prototype.findIndex.apply({}, /\d+/, [])', TypeError);
\ No newline at end of file
......@@ -905,7 +905,8 @@
'../../src/typedarray.js',
'../../src/generator.js',
'../../src/array-iterator.js',
'../../src/harmony-string.js'
'../../src/harmony-string.js',
'../../src/harmony-array.js',
],
'i18n_library_files': [
'../../src/extensions/i18n/header.js',
......
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