2013-08-02 10:57:48 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-08-02 10:57:48 +00:00
|
|
|
|
|
|
|
'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
|
2014-01-31 10:05:58 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
|
2013-08-02 10:57:48 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var needs_wrapper = false;
|
2013-08-02 10:57:48 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(thisArg)) {
|
|
|
|
thisArg = %GetDefaultReceiver(predicate) || thisArg;
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
} else {
|
|
|
|
needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
|
2013-08-02 10:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (i in array) {
|
|
|
|
var element = array[i];
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg;
|
|
|
|
if (%_CallFunction(newThisArg, element, i, array, predicate)) {
|
2013-08-02 10:57:48 +00:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ES6 draft 07-15-13, section 15.4.3.24
|
|
|
|
function ArrayFindIndex(predicate /* thisArg */) { // length == 1
|
2014-01-31 10:05:58 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex");
|
2013-08-02 10:57:48 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var needs_wrapper = false;
|
2013-08-02 10:57:48 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(thisArg)) {
|
|
|
|
thisArg = %GetDefaultReceiver(predicate) || thisArg;
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
} else {
|
|
|
|
needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg);
|
2013-08-02 10:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
if (i in array) {
|
|
|
|
var element = array[i];
|
Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper for calls.
When the receiver is a primitive value, it's cast to an Object before entering the loop. Instead, it should be cast to an Object for each function call while in the loop.
BUG=v8:3536
LOG=Y
R=arv@chromium.org, svenpanne@chromium.org, wingo@igalia.com
Review URL: https://codereview.chromium.org/553413002
Patch from Diego Pino <dpino@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24620 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-15 09:11:32 +00:00
|
|
|
var newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg;
|
|
|
|
if (%_CallFunction(newThisArg, element, i, array, predicate)) {
|
2013-08-02 10:57:48 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-30 08:28:29 +00:00
|
|
|
// ES6, draft 04-05-14, section 22.1.3.6
|
|
|
|
function ArrayFill(value /* [, start [, end ] ] */) { // length == 1
|
|
|
|
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill");
|
|
|
|
|
|
|
|
var array = ToObject(this);
|
|
|
|
var length = TO_UINT32(array.length);
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
var end = length;
|
|
|
|
|
|
|
|
if (%_ArgumentsLength() > 1) {
|
|
|
|
i = %_Arguments(1);
|
|
|
|
i = IS_UNDEFINED(i) ? 0 : TO_INTEGER(i);
|
|
|
|
if (%_ArgumentsLength() > 2) {
|
|
|
|
end = %_Arguments(2);
|
|
|
|
end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < 0) {
|
|
|
|
i += length;
|
|
|
|
if (i < 0) i = 0;
|
|
|
|
} else {
|
|
|
|
if (i > length) i = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end < 0) {
|
|
|
|
end += length;
|
|
|
|
if (end < 0) end = 0;
|
|
|
|
} else {
|
|
|
|
if (end > length) end = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((end - i) > 0 && ObjectIsFrozen(array)) {
|
|
|
|
throw MakeTypeError("array_functions_on_frozen",
|
|
|
|
["Array.prototype.fill"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < end; i++)
|
|
|
|
array[i] = value;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-12-11 17:16:21 +00:00
|
|
|
// ES6, draft 10-14-14, section 22.1.2.1
|
|
|
|
function ArrayFrom(arrayLike, mapfn, receiver) {
|
|
|
|
var items = ToObject(arrayLike);
|
|
|
|
var mapping = !IS_UNDEFINED(mapfn);
|
|
|
|
|
|
|
|
if (mapping) {
|
|
|
|
if (!IS_SPEC_FUNCTION(mapfn)) {
|
|
|
|
throw MakeTypeError('called_non_callable', [ mapfn ]);
|
|
|
|
} else if (IS_NULL_OR_UNDEFINED(receiver)) {
|
|
|
|
receiver = %GetDefaultReceiver(mapfn) || receiver;
|
|
|
|
} else if (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(mapfn)) {
|
|
|
|
receiver = ToObject(receiver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:36:03 +00:00
|
|
|
var iterable = GetMethod(items, symbolIterator);
|
2014-12-11 17:16:21 +00:00
|
|
|
var k;
|
|
|
|
var result;
|
|
|
|
var mappedValue;
|
|
|
|
var nextValue;
|
|
|
|
|
|
|
|
if (!IS_UNDEFINED(iterable)) {
|
2015-01-19 12:22:22 +00:00
|
|
|
result = %IsConstructor(this) ? new this() : [];
|
2014-12-11 17:16:21 +00:00
|
|
|
|
2015-01-29 02:36:03 +00:00
|
|
|
var iterator = GetIterator(items, iterable);
|
|
|
|
|
2014-12-11 17:16:21 +00:00
|
|
|
k = 0;
|
2015-01-29 02:36:03 +00:00
|
|
|
while (true) {
|
|
|
|
var next = iterator.next();
|
|
|
|
|
|
|
|
if (!IS_OBJECT(next)) {
|
|
|
|
throw MakeTypeError("iterator_result_not_an_object", [next]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next.done) {
|
|
|
|
result.length = k;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextValue = next.value;
|
|
|
|
if (mapping) {
|
|
|
|
mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
|
|
|
|
} else {
|
|
|
|
mappedValue = nextValue;
|
|
|
|
}
|
2014-12-11 17:16:21 +00:00
|
|
|
%AddElement(result, k++, mappedValue, NONE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var len = ToLength(items.length);
|
2015-01-19 12:22:22 +00:00
|
|
|
result = %IsConstructor(this) ? new this(len) : new $Array(len);
|
2014-12-11 17:16:21 +00:00
|
|
|
|
|
|
|
for (k = 0; k < len; ++k) {
|
|
|
|
nextValue = items[k];
|
2015-01-29 02:36:03 +00:00
|
|
|
if (mapping) {
|
|
|
|
mappedValue = %_CallFunction(receiver, nextValue, k, mapfn);
|
|
|
|
} else {
|
|
|
|
mappedValue = nextValue;
|
|
|
|
}
|
2014-12-11 17:16:21 +00:00
|
|
|
%AddElement(result, k, mappedValue, NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.length = k;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:38:38 +00:00
|
|
|
// ES6, draft 05-22-14, section 22.1.2.3
|
|
|
|
function ArrayOf() {
|
|
|
|
var length = %_ArgumentsLength();
|
|
|
|
var constructor = this;
|
|
|
|
// TODO: Implement IsConstructor (ES6 section 7.2.5)
|
2015-01-19 12:22:22 +00:00
|
|
|
var array = %IsConstructor(constructor) ? new constructor(length) : [];
|
2014-08-19 11:38:38 +00:00
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
%AddElement(array, i, %_Arguments(i), NONE);
|
|
|
|
}
|
|
|
|
array.length = length;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2013-08-02 10:57:48 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
2014-12-12 18:38:40 +00:00
|
|
|
function HarmonyArrayExtendSymbolPrototype() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
|
|
|
InstallConstants($Symbol, $Array(
|
|
|
|
// TODO(dslomov, caitp): Move to symbol.js when shipping
|
|
|
|
"isConcatSpreadable", symbolIsConcatSpreadable
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
HarmonyArrayExtendSymbolPrototype();
|
|
|
|
|
2013-08-02 10:57:48 +00:00
|
|
|
function HarmonyArrayExtendArrayPrototype() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
2014-12-11 17:16:21 +00:00
|
|
|
%FunctionSetLength(ArrayFrom, 1);
|
|
|
|
|
2014-08-19 11:38:38 +00:00
|
|
|
// Set up non-enumerable functions on the Array object.
|
|
|
|
InstallFunctions($Array, DONT_ENUM, $Array(
|
2014-12-11 17:16:21 +00:00
|
|
|
"from", ArrayFrom,
|
2014-08-19 11:38:38 +00:00
|
|
|
"of", ArrayOf
|
|
|
|
));
|
|
|
|
|
2013-08-02 10:57:48 +00:00
|
|
|
// Set up the non-enumerable functions on the Array prototype object.
|
|
|
|
InstallFunctions($Array.prototype, DONT_ENUM, $Array(
|
|
|
|
"find", ArrayFind,
|
2014-04-30 08:28:29 +00:00
|
|
|
"findIndex", ArrayFindIndex,
|
|
|
|
"fill", ArrayFill
|
2013-08-02 10:57:48 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2013-11-22 13:50:39 +00:00
|
|
|
HarmonyArrayExtendArrayPrototype();
|