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