OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 // This file relies on the fact that the following declaration has been made | 7 // This file relies on the fact that the following declaration has been made |
8 // in runtime.js: | 8 // in runtime.js: |
9 // var $Array = global.Array; | 9 // var $Array = global.Array; |
10 | 10 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 if ((end - i) > 0 && ObjectIsFrozen(array)) { | 120 if ((end - i) > 0 && ObjectIsFrozen(array)) { |
121 throw MakeTypeError("array_functions_on_frozen", | 121 throw MakeTypeError("array_functions_on_frozen", |
122 ["Array.prototype.fill"]); | 122 ["Array.prototype.fill"]); |
123 } | 123 } |
124 | 124 |
125 for (; i < end; i++) | 125 for (; i < end; i++) |
126 array[i] = value; | 126 array[i] = value; |
127 return array; | 127 return array; |
128 } | 128 } |
129 | 129 |
130 // Proposed for ES7 | |
131 // https://github.com/tc39/Array.prototype.includes | |
132 // 6e3b78c927aeda20b9d40e81303f9d44596cd904 | |
133 function ArrayIncludes(searchElement, fromIndex) { | |
Dmitry Lomov (no reviews)
2014/12/02 12:25:25
Since it is not a ES6 feature, Array.prototype.inc
| |
134 var array = ToObject(this); | |
135 var len = ToLength(array.length); | |
136 | |
137 if (len === 0) { | |
138 return false; | |
139 } | |
140 | |
141 var n = ToInteger(fromIndex); | |
142 | |
143 var k; | |
144 if (n >= 0) { | |
145 k = n; | |
146 } else { | |
147 k = len + n; | |
148 if (k < 0) { | |
149 k = 0; | |
150 } | |
151 } | |
152 | |
153 while (k < len) { | |
154 var elementK = array[k]; | |
155 if (SameValueZero(searchElement, elementK)) { | |
156 return true; | |
157 } | |
158 | |
159 ++k; | |
160 } | |
161 | |
162 return false; | |
163 } | |
164 | |
130 // ES6, draft 05-22-14, section 22.1.2.3 | 165 // ES6, draft 05-22-14, section 22.1.2.3 |
131 function ArrayOf() { | 166 function ArrayOf() { |
132 var length = %_ArgumentsLength(); | 167 var length = %_ArgumentsLength(); |
133 var constructor = this; | 168 var constructor = this; |
134 // TODO: Implement IsConstructor (ES6 section 7.2.5) | 169 // TODO: Implement IsConstructor (ES6 section 7.2.5) |
135 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; | 170 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; |
136 for (var i = 0; i < length; i++) { | 171 for (var i = 0; i < length; i++) { |
137 %AddElement(array, i, %_Arguments(i), NONE); | 172 %AddElement(array, i, %_Arguments(i), NONE); |
138 } | 173 } |
139 array.length = length; | 174 array.length = length; |
140 return array; | 175 return array; |
141 } | 176 } |
142 | 177 |
143 // ------------------------------------------------------------------- | 178 // ------------------------------------------------------------------- |
144 | 179 |
145 function HarmonyArrayExtendArrayPrototype() { | 180 function HarmonyArrayExtendArrayPrototype() { |
146 %CheckIsBootstrapping(); | 181 %CheckIsBootstrapping(); |
147 | 182 |
148 // Set up non-enumerable functions on the Array object. | 183 // Set up non-enumerable functions on the Array object. |
149 InstallFunctions($Array, DONT_ENUM, $Array( | 184 InstallFunctions($Array, DONT_ENUM, $Array( |
150 "of", ArrayOf | 185 "of", ArrayOf |
151 )); | 186 )); |
152 | 187 |
188 %FunctionSetLength(ArrayIncludes, 1); | |
189 | |
153 // Set up the non-enumerable functions on the Array prototype object. | 190 // Set up the non-enumerable functions on the Array prototype object. |
154 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | 191 InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
192 "includes", ArrayIncludes, | |
155 "find", ArrayFind, | 193 "find", ArrayFind, |
156 "findIndex", ArrayFindIndex, | 194 "findIndex", ArrayFindIndex, |
157 "fill", ArrayFill | 195 "fill", ArrayFill |
158 )); | 196 )); |
159 } | 197 } |
160 | 198 |
161 HarmonyArrayExtendArrayPrototype(); | 199 HarmonyArrayExtendArrayPrototype(); |
OLD | NEW |