Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(674)

Side by Side Diff: src/harmony-array.js

Issue 579973002: Add Array.prototype.contains (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/runtime.js » ('j') | src/runtime.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
caitp (gmail) 2014/09/18 01:28:31 arv@ has suggested using `%FunctionSetLength(funct
mathias 2014/09/18 06:13:07 Needs an extra space before `//`.
Dmitry Lomov (no reviews) 2014/09/18 06:26:45 Needs COERCIBLE check. (No strong preference on ho
domenic_domenicdenicola.com 2014/09/18 15:16:13 There is no COERCIBLE check in the spec for any of
Dmitry Lomov (no reviews) 2014/09/18 15:37:29 Hmm, I see. Could you file a bug?
130 var O = ToObject(this);
131 var len = ToLength(O.length);
132
133 if (len === 0) {
Dmitry Lomov (no reviews) 2014/09/18 06:26:45 Do you need this check? The below will work just f
domenic_domenicdenicola.com 2014/09/18 15:16:13 Yes, as otherwise we will trigger ToInteger on fro
Dmitry Lomov (no reviews) 2014/09/18 15:37:29 Got it.
134 return false;
135 }
136
137 var n = ToInteger(%_Arguments(1));
138
139 if (n >= len) {
Dmitry Lomov (no reviews) 2014/09/18 06:26:45 Do you need this check? 'while' below has a check
domenic_domenicdenicola.com 2014/09/18 15:16:13 Removed, tests still pass.
140 return false;
141 }
142
143 var k;
144 if (n >= 0) {
145 k = n;
146 } else {
147 k = len - MathAbs(n);
Dmitry Lomov (no reviews) 2014/09/18 06:26:45 Simply len + n?
domenic_domenicdenicola.com 2014/09/18 15:16:13 Weird, I wonder why the spec used abs for indexOf
148 if (k < 0) {
149 k = 0;
150 }
151 }
152
153 while (k < len) {
154 var elementK = O[k];
155 if (SameValueZero(searchElement, elementK) === true) {
Dmitry Lomov (no reviews) 2014/09/18 06:26:45 === true? Why not "if (SameValueZero(...))"
domenic_domenicdenicola.com 2014/09/18 15:16:13 Just going for spec-verbatimness. Will change.
156 return true;
157 }
158
159 ++k;
160 }
161
162 return false;
163 }
164
126 // ES6, draft 05-22-14, section 22.1.2.3 165 // ES6, draft 05-22-14, section 22.1.2.3
127 function ArrayOf() { 166 function ArrayOf() {
128 var length = %_ArgumentsLength(); 167 var length = %_ArgumentsLength();
129 var constructor = this; 168 var constructor = this;
130 // TODO: Implement IsConstructor (ES6 section 7.2.5) 169 // TODO: Implement IsConstructor (ES6 section 7.2.5)
131 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : []; 170 var array = IS_SPEC_FUNCTION(constructor) ? new constructor(length) : [];
132 for (var i = 0; i < length; i++) { 171 for (var i = 0; i < length; i++) {
133 %AddElement(array, i, %_Arguments(i), NONE); 172 %AddElement(array, i, %_Arguments(i), NONE);
134 } 173 }
135 array.length = length; 174 array.length = length;
136 return array; 175 return array;
137 } 176 }
138 177
139 // ------------------------------------------------------------------- 178 // -------------------------------------------------------------------
140 179
141 function HarmonyArrayExtendArrayPrototype() { 180 function HarmonyArrayExtendArrayPrototype() {
142 %CheckIsBootstrapping(); 181 %CheckIsBootstrapping();
143 182
144 // Set up non-enumerable functions on the Array object. 183 // Set up non-enumerable functions on the Array object.
145 InstallFunctions($Array, DONT_ENUM, $Array( 184 InstallFunctions($Array, DONT_ENUM, $Array(
146 "of", ArrayOf 185 "of", ArrayOf
147 )); 186 ));
148 187
149 // Set up the non-enumerable functions on the Array prototype object. 188 // Set up the non-enumerable functions on the Array prototype object.
150 InstallFunctions($Array.prototype, DONT_ENUM, $Array( 189 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
190 "contains", ArrayContains,
151 "find", ArrayFind, 191 "find", ArrayFind,
152 "findIndex", ArrayFindIndex, 192 "findIndex", ArrayFindIndex,
153 "fill", ArrayFill 193 "fill", ArrayFill
154 )); 194 ));
155 } 195 }
156 196
157 HarmonyArrayExtendArrayPrototype(); 197 HarmonyArrayExtendArrayPrototype();
OLDNEW
« no previous file with comments | « no previous file | src/runtime.js » ('j') | src/runtime.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698