Commit 4161b54d authored by d's avatar d Committed by Commit bot

Add Array.prototype.includes

Requires adding a SameValueZero implementation.

LOG=Y
BUG=v8:3575
R=dslomov@chromium.org, arv@chromium.org
TEST=added to test262

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

Cr-Commit-Position: refs/heads/master@{#25735}
parent 017eace7
......@@ -239,6 +239,7 @@ action("js2c_experimental") {
"src/generator.js",
"src/harmony-string.js",
"src/harmony-array.js",
"src/harmony-array-includes.js",
"src/harmony-typedarray.js",
"src/harmony-classes.js",
"src/harmony-tostring.js",
......
......@@ -1582,6 +1582,7 @@ EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_scoping)
EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_modules)
EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_strings)
EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_arrays)
EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_array_includes)
EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_classes)
EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_object_literals)
EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_regexps)
......@@ -1611,6 +1612,7 @@ EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_scoping)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_modules)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_strings)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_arrays)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_array_includes)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_classes)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_object_literals)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_arrow_functions)
......@@ -2154,6 +2156,8 @@ bool Genesis::InstallNatives() {
bool Genesis::InstallExperimentalNatives() {
static const char* harmony_arrays_natives[] = {
"native harmony-array.js", "native harmony-typedarray.js", NULL};
static const char* harmony_array_includes_natives[] = {
"native harmony-array-includes.js", NULL};
static const char* harmony_proxies_natives[] = {"native proxy.js", NULL};
static const char* harmony_strings_natives[] = {"native harmony-string.js",
NULL};
......
......@@ -175,6 +175,7 @@ DEFINE_IMPLICATION(es_staging, harmony)
#define HARMONY_INPROGRESS(V) \
V(harmony_modules, "harmony modules (implies block scoping)") \
V(harmony_arrays, "harmony array methods") \
V(harmony_array_includes, "harmony Array.prototype.includes") \
V(harmony_regexps, "harmony regular expression extensions") \
V(harmony_arrow_functions, "harmony arrow functions") \
V(harmony_proxies, "harmony proxies") \
......
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
// This file relies on the fact that the following declaration has been made
// in runtime.js:
// var $Array = global.Array;
// -------------------------------------------------------------------
// Proposed for ES7
// https://github.com/tc39/Array.prototype.includes
// 6e3b78c927aeda20b9d40e81303f9d44596cd904
function ArrayIncludes(searchElement, fromIndex) {
var array = ToObject(this);
var len = ToLength(array.length);
if (len === 0) {
return false;
}
var n = ToInteger(fromIndex);
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {
k = 0;
}
}
while (k < len) {
var elementK = array[k];
if (SameValueZero(searchElement, elementK)) {
return true;
}
++k;
}
return false;
}
// -------------------------------------------------------------------
function HarmonyArrayIncludesExtendArrayPrototype() {
%CheckIsBootstrapping();
%FunctionSetLength(ArrayIncludes, 1);
// Set up the non-enumerable functions on the Array prototype object.
InstallFunctions($Array.prototype, DONT_ENUM, $Array(
"includes", ArrayIncludes
));
}
HarmonyArrayIncludesExtendArrayPrototype();
......@@ -601,6 +601,15 @@ function SameValue(x, y) {
return x === y;
}
// ES6, section 7.2.4
function SameValueZero(x, y) {
if (typeof x != typeof y) return false;
if (IS_NUMBER(x)) {
if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
}
return x === y;
}
/* ---------------------------------
- - - U t i l i t i e s - - -
......
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-array-includes
// Ported from
// https://github.com/tc39/Array.prototype.includes/blob/master/test/number-this.js
// using https://www.npmjs.org/package/test262-to-mjsunit
// Array.prototype.includes should use ToObject on this, so that when called
// with a number, it picks up numeric properties from Number.prototype
(function() {
Number.prototype[0] = "a";
Number.prototype[1] = "b";
Object.defineProperty(Number.prototype, 2, {
get: function() {
assertEquals("object", typeof this);
return "c";
}
});
Number.prototype.length = 3;
assertTrue(Array.prototype.includes.call(5, "a"));
assertTrue(Array.prototype.includes.call(5, "b"));
assertTrue(Array.prototype.includes.call(5, "c"));
assertFalse(Array.prototype.includes.call(5, "d"));
})();
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-array-includes
// Ported from
// https://github.com/tc39/Array.prototype.includes/blob/master/test/number-this.js
// using https://www.npmjs.org/package/test262-to-mjsunit
// Array.prototype.includes should use ToObject on this, so that when called
// with a number, it picks up numeric properties from Number.prototype (even in
// strict mode)
(function() {
"use strict";
Number.prototype[0] = "a";
Number.prototype[1] = "b";
Object.defineProperty(Number.prototype, 2, {
get: function() {
assertEquals("object", typeof this);
return "c";
}
});
Number.prototype.length = 3;
assertTrue(Array.prototype.includes.call(5, "a"));
assertTrue(Array.prototype.includes.call(5, "b"));
assertTrue(Array.prototype.includes.call(5, "c"));
assertFalse(Array.prototype.includes.call(5, "d"));
})();
This diff is collapsed.
......@@ -1634,6 +1634,7 @@
'../../src/generator.js',
'../../src/harmony-string.js',
'../../src/harmony-array.js',
'../../src/harmony-array-includes.js',
'../../src/harmony-tostring.js',
'../../src/harmony-typedarray.js',
'../../src/harmony-classes.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