diff options
Diffstat (limited to 'deps/v8/src/js/harmony-array-includes.js')
-rw-r--r-- | deps/v8/src/js/harmony-array-includes.js | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/deps/v8/src/js/harmony-array-includes.js b/deps/v8/src/js/harmony-array-includes.js new file mode 100644 index 0000000000..bb1f01cd57 --- /dev/null +++ b/deps/v8/src/js/harmony-array-includes.js @@ -0,0 +1,118 @@ +// 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. + +(function(global, utils) { + +'use strict'; + +%CheckIsBootstrapping(); + +// ------------------------------------------------------------------- +// Imports + +var GlobalArray = global.Array; +var MakeTypeError; +var SameValueZero; + +utils.Import(function(from) { + MakeTypeError = from.MakeTypeError; + SameValueZero = from.SameValueZero; +}); + +// ------------------------------------------------------------------- + +// Proposed for ES7 +// https://github.com/tc39/Array.prototype.includes +// 46c7532ec8499dea3e51aeb940d09e07547ed3f5 +function InnerArrayIncludes(searchElement, fromIndex, array, length) { + if (length === 0) { + return false; + } + + var n = TO_INTEGER(fromIndex); + + var k; + if (n >= 0) { + k = n; + } else { + k = length + n; + if (k < 0) { + k = 0; + } + } + + while (k < length) { + var elementK = array[k]; + if (SameValueZero(searchElement, elementK)) { + return true; + } + + ++k; + } + + return false; +} + + +function ArrayIncludes(searchElement, fromIndex) { + CHECK_OBJECT_COERCIBLE(this, "Array.prototype.includes"); + + var array = TO_OBJECT(this); + var length = TO_LENGTH(array.length); + + return InnerArrayIncludes(searchElement, fromIndex, array, length); +} + + +function TypedArrayIncludes(searchElement, fromIndex) { + if (!%_IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); + + var length = %_TypedArrayGetLength(this); + + return InnerArrayIncludes(searchElement, fromIndex, this, length); +} + +// ------------------------------------------------------------------- + +%FunctionSetLength(ArrayIncludes, 1); +%FunctionSetLength(TypedArrayIncludes, 1); + +// Set up the non-enumerable function on the Array prototype object. +utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [ + "includes", ArrayIncludes +]); + +// Set up the non-enumerable function on the typed array prototypes. +// This duplicates some of the machinery in harmony-typedarray.js in order to +// keep includes behind the separate --harmony-array-includes flag. +// TODO(littledan): Fix the TypedArray proto chain (bug v8:4085). + +macro TYPED_ARRAYS(FUNCTION) +// arrayIds below should be synchronized with Runtime_TypedArrayInitialize. +FUNCTION(Uint8Array) +FUNCTION(Int8Array) +FUNCTION(Uint16Array) +FUNCTION(Int16Array) +FUNCTION(Uint32Array) +FUNCTION(Int32Array) +FUNCTION(Float32Array) +FUNCTION(Float64Array) +FUNCTION(Uint8ClampedArray) +endmacro + +macro DECLARE_GLOBALS(NAME) +var GlobalNAME = global.NAME; +endmacro + +macro EXTEND_TYPED_ARRAY(NAME) +// Set up non-enumerable functions on the prototype object. +utils.InstallFunctions(GlobalNAME.prototype, DONT_ENUM, [ + "includes", TypedArrayIncludes +]); +endmacro + +TYPED_ARRAYS(DECLARE_GLOBALS) +TYPED_ARRAYS(EXTEND_TYPED_ARRAY) + +}) |