OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 (function(global, utils) { | |
6 | |
7 'use strict'; | |
8 | |
9 %CheckIsBootstrapping(); | |
10 | |
11 var GlobalArray = global.Array; | |
12 | |
13 // ------------------------------------------------------------------- | |
14 | |
15 // Proposed for ES7 | |
16 // https://github.com/tc39/Array.prototype.includes | |
17 // 46c7532ec8499dea3e51aeb940d09e07547ed3f5 | |
18 function InnerArrayIncludes(searchElement, fromIndex, array, length) { | |
19 if (length === 0) { | |
20 return false; | |
21 } | |
22 | |
23 var n = TO_INTEGER(fromIndex); | |
24 | |
25 var k; | |
26 if (n >= 0) { | |
27 k = n; | |
28 } else { | |
29 k = length + n; | |
30 if (k < 0) { | |
31 k = 0; | |
32 } | |
33 } | |
34 | |
35 while (k < length) { | |
36 var elementK = array[k]; | |
37 if ($sameValueZero(searchElement, elementK)) { | |
38 return true; | |
39 } | |
40 | |
41 ++k; | |
42 } | |
43 | |
44 return false; | |
45 } | |
46 | |
47 | |
48 function ArrayIncludes(searchElement, fromIndex) { | |
49 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.includes"); | |
50 | |
51 var array = TO_OBJECT(this); | |
52 var length = TO_LENGTH(array.length); | |
53 | |
54 return InnerArrayIncludes(searchElement, fromIndex, array, length); | |
55 } | |
56 | |
57 | |
58 function TypedArrayIncludes(searchElement, fromIndex) { | |
59 if (!%_IsTypedArray(this)) throw MakeTypeError(kNotTypedArray); | |
60 | |
61 var length = %_TypedArrayGetLength(this); | |
62 | |
63 return InnerArrayIncludes(searchElement, fromIndex, this, length); | |
64 } | |
65 | |
66 // ------------------------------------------------------------------- | |
67 | |
68 %FunctionSetLength(ArrayIncludes, 1); | |
69 %FunctionSetLength(TypedArrayIncludes, 1); | |
70 | |
71 // Set up the non-enumerable function on the Array prototype object. | |
72 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [ | |
73 "includes", ArrayIncludes | |
74 ]); | |
75 | |
76 // Set up the non-enumerable function on the typed array prototypes. | |
77 // This duplicates some of the machinery in harmony-typedarray.js in order to | |
78 // keep includes behind the separate --harmony-array-includes flag. | |
79 // TODO(littledan): Fix the TypedArray proto chain (bug v8:4085). | |
80 | |
81 macro TYPED_ARRAYS(FUNCTION) | |
82 // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. | |
83 FUNCTION(Uint8Array) | |
84 FUNCTION(Int8Array) | |
85 FUNCTION(Uint16Array) | |
86 FUNCTION(Int16Array) | |
87 FUNCTION(Uint32Array) | |
88 FUNCTION(Int32Array) | |
89 FUNCTION(Float32Array) | |
90 FUNCTION(Float64Array) | |
91 FUNCTION(Uint8ClampedArray) | |
92 endmacro | |
93 | |
94 macro DECLARE_GLOBALS(NAME) | |
95 var GlobalNAME = global.NAME; | |
96 endmacro | |
97 | |
98 macro EXTEND_TYPED_ARRAY(NAME) | |
99 // Set up non-enumerable functions on the prototype object. | |
100 utils.InstallFunctions(GlobalNAME.prototype, DONT_ENUM, [ | |
101 "includes", TypedArrayIncludes | |
102 ]); | |
103 endmacro | |
104 | |
105 TYPED_ARRAYS(DECLARE_GLOBALS) | |
106 TYPED_ARRAYS(EXTEND_TYPED_ARRAY) | |
107 | |
108 }) | |
OLD | NEW |