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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 if ((end - i) > 0 && ObjectIsFrozen(array)) { | 116 if ((end - i) > 0 && ObjectIsFrozen(array)) { |
117 throw MakeTypeError("array_functions_on_frozen", | 117 throw MakeTypeError("array_functions_on_frozen", |
118 ["Array.prototype.fill"]); | 118 ["Array.prototype.fill"]); |
119 } | 119 } |
120 | 120 |
121 for (; i < end; i++) | 121 for (; i < end; i++) |
122 array[i] = value; | 122 array[i] = value; |
123 return array; | 123 return array; |
124 } | 124 } |
125 | 125 |
126 // Proposed for ES7 | |
127 // https://github.com/domenic/Array.prototype.contains/ | |
128 // 3fb531d074eddf5d23e0a46b57ea48932e1fac69 | |
129 function ArrayContains(searchElement /*, fromIndex */) { // length == 1 | |
130 var O = ToObject(this); | |
arv (Not doing code reviews)
2014/09/18 15:28:02
Rename this to object or array.
| |
131 var len = ToLength(O.length); | |
132 | |
133 if (len === 0) { | |
134 return false; | |
135 } | |
136 | |
137 var n = ToInteger(%_Arguments(1)); | |
138 | |
139 var k; | |
140 if (n >= 0) { | |
141 k = n; | |
142 } else { | |
143 k = len + n; | |
144 if (k < 0) { | |
145 k = 0; | |
146 } | |
147 } | |
148 | |
149 while (k < len) { | |
150 var elementK = O[k]; | |
151 if (SameValueZero(searchElement, elementK)) { | |
152 return true; | |
153 } | |
154 | |
155 ++k; | |
156 } | |
157 | |
158 return false; | |
159 } | |
160 | |
126 // ES6, draft 05-22-14, section 22.1.2.3 | 161 // ES6, draft 05-22-14, section 22.1.2.3 |
127 function ArrayOf() { | 162 function ArrayOf() { |
128 var length = %_ArgumentsLength(); | 163 var length = %_ArgumentsLength(); |
129 var constructor = this; | 164 var constructor = this; |
130 // TODO: Implement IsConstructor (ES6 section 7.2.5) | 165 // TODO: Implement IsConstructor (ES6 section 7.2.5) |
131 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; | 166 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; |
132 for (var i = 0; i < length; i++) { | 167 for (var i = 0; i < length; i++) { |
133 %AddElement(array, i, %_Arguments(i), NONE); | 168 %AddElement(array, i, %_Arguments(i), NONE); |
134 } | 169 } |
135 array.length = length; | 170 array.length = length; |
136 return array; | 171 return array; |
137 } | 172 } |
138 | 173 |
139 // ------------------------------------------------------------------- | 174 // ------------------------------------------------------------------- |
140 | 175 |
141 function HarmonyArrayExtendArrayPrototype() { | 176 function HarmonyArrayExtendArrayPrototype() { |
142 %CheckIsBootstrapping(); | 177 %CheckIsBootstrapping(); |
143 | 178 |
144 // Set up non-enumerable functions on the Array object. | 179 // Set up non-enumerable functions on the Array object. |
145 InstallFunctions($Array, DONT_ENUM, $Array( | 180 InstallFunctions($Array, DONT_ENUM, $Array( |
146 "of", ArrayOf | 181 "of", ArrayOf |
147 )); | 182 )); |
148 | 183 |
149 // Set up the non-enumerable functions on the Array prototype object. | 184 // Set up the non-enumerable functions on the Array prototype object. |
150 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | 185 InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
186 "contains", ArrayContains, | |
151 "find", ArrayFind, | 187 "find", ArrayFind, |
152 "findIndex", ArrayFindIndex, | 188 "findIndex", ArrayFindIndex, |
153 "fill", ArrayFill | 189 "fill", ArrayFill |
154 )); | 190 )); |
155 } | 191 } |
156 | 192 |
157 HarmonyArrayExtendArrayPrototype(); | 193 HarmonyArrayExtendArrayPrototype(); |
OLD | NEW |