Reviewers: arv, rossberg,

Description:
Add Array.prototype.contains

Requires adding ToLength and SameValueZero implementations.

LOG=Y
BUG=none
[email protected], [email protected]
TEST=added to test262

Please review this at https://codereview.chromium.org/579973002/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+58, -0 lines):
  M src/harmony-array.js
  M src/runtime.js


Index: src/harmony-array.js
diff --git a/src/harmony-array.js b/src/harmony-array.js
index 88b878f0a76ef594f64f7905d61459c05749724b..286265b9b91cba4cf40a7b5cf5fac2e53240109f 100644
--- a/src/harmony-array.js
+++ b/src/harmony-array.js
@@ -123,6 +123,45 @@ function ArrayFill(value /* [, start [, end ] ] */) { // length == 1
   return array;
 }

+// Proposed for ES7
+// https://github.com/domenic/Array.prototype.contains/
+// 3fb531d074eddf5d23e0a46b57ea48932e1fac69
+function ArrayContains(searchElement /*, fromIndex */) { // length == 1
+  var O = ToObject(this);
+  var len = ToLength(O.length);
+
+  if (len === 0) {
+    return false;
+  }
+
+  var n = ToInteger(%_Arguments(1));
+
+  if (n >= len) {
+    return false;
+  }
+
+  var k;
+  if (n >= 0) {
+    k = n;
+  } else {
+    k = len - MathAbs(n);
+    if (k < 0) {
+      k = 0;
+    }
+  }
+
+  while (k < len) {
+    var elementK = O[k];
+    if (SameValueZero(searchElement, elementK) === true) {
+      return true;
+    }
+
+    ++k;
+  }
+
+  return false;
+}
+
 // ES6, draft 05-22-14, section 22.1.2.3
 function ArrayOf() {
   var length = %_ArgumentsLength();
@@ -148,6 +187,7 @@ function HarmonyArrayExtendArrayPrototype() {

   // Set up the non-enumerable functions on the Array prototype object.
   InstallFunctions($Array.prototype, DONT_ENUM, $Array(
+    "contains", ArrayContains,
     "find", ArrayFind,
     "findIndex", ArrayFindIndex,
     "fill", ArrayFill
Index: src/runtime.js
diff --git a/src/runtime.js b/src/runtime.js
index d9e1fe5c994030f40461167faac8b3df4d60ff22..d4849d9b131fcaea70efbadbe5cf64b39c460502 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -576,6 +576,15 @@ function ToInt32(x) {
   return %NumberToJSInt32(ToNumber(x));
 }

+// ES6, section 7.1.15
+function ToLength(x) {
+  if (%_IsSmi(x) && x > 0) return x; // not >= since -0 >= +0
+
+  var len = ToInteger(x);
+  if (len <= 0) return 0;
+  return MathMin(len, $Number.MAX_SAFE_INTEGER);
+}
+

 // ES5, section 9.12
 function SameValue(x, y) {
@@ -590,6 +599,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   - - -


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to