Chromium Code Reviews| 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 var $innerArrayCopyWithin; | 5 var $innerArrayCopyWithin; |
| 6 var $innerArrayFill; | |
| 7 var $innerArrayFind; | |
| 8 var $innerArrayFindIndex; | |
| 6 | 9 |
| 7 (function(global, exports) { | 10 (function(global, exports) { |
| 8 | 11 |
| 9 'use strict'; | 12 'use strict'; |
| 10 | 13 |
| 11 %CheckIsBootstrapping(); | 14 %CheckIsBootstrapping(); |
| 12 | 15 |
| 13 var GlobalArray = global.Array; | 16 var GlobalArray = global.Array; |
| 14 var GlobalSymbol = global.Symbol; | 17 var GlobalSymbol = global.Symbol; |
| 15 | 18 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 // ES6 draft 03-17-15, section 22.1.3.3 | 69 // ES6 draft 03-17-15, section 22.1.3.3 |
| 67 function ArrayCopyWithin(target, start, end) { | 70 function ArrayCopyWithin(target, start, end) { |
| 68 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin"); | 71 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin"); |
| 69 | 72 |
| 70 var array = TO_OBJECT_INLINE(this); | 73 var array = TO_OBJECT_INLINE(this); |
| 71 var length = $toLength(array.length); | 74 var length = $toLength(array.length); |
| 72 | 75 |
| 73 return InnerArrayCopyWithin(target, start, end, array, length); | 76 return InnerArrayCopyWithin(target, start, end, array, length); |
| 74 } | 77 } |
| 75 | 78 |
| 76 // ES6 draft 07-15-13, section 15.4.3.23 | 79 function InnerArrayFind(predicate, thisArg, array, length) { |
| 77 function ArrayFind(predicate /* thisArg */) { // length == 1 | |
| 78 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); | |
| 79 | |
| 80 var array = $toObject(this); | |
| 81 var length = $toInteger(array.length); | |
| 82 | |
| 83 if (!IS_SPEC_FUNCTION(predicate)) { | 80 if (!IS_SPEC_FUNCTION(predicate)) { |
| 84 throw MakeTypeError(kCalledNonCallable, predicate); | 81 throw MakeTypeError(kCalledNonCallable, predicate); |
| 85 } | 82 } |
| 86 | 83 |
| 87 var thisArg; | |
| 88 if (%_ArgumentsLength() > 1) { | |
| 89 thisArg = %_Arguments(1); | |
| 90 } | |
| 91 | |
| 92 var needs_wrapper = false; | 84 var needs_wrapper = false; |
| 93 if (IS_NULL(thisArg)) { | 85 if (IS_NULL(thisArg)) { |
| 94 if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED; | 86 if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED; |
| 95 } else if (!IS_UNDEFINED(thisArg)) { | 87 } else if (!IS_UNDEFINED(thisArg)) { |
| 96 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); | 88 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); |
| 97 } | 89 } |
| 98 | 90 |
| 99 for (var i = 0; i < length; i++) { | 91 for (var i = 0; i < length; i++) { |
| 100 if (i in array) { | 92 if (i in array) { |
| 101 var element = array[i]; | 93 var element = array[i]; |
| 102 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; | 94 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; |
| 103 if (%_CallFunction(newThisArg, element, i, array, predicate)) { | 95 if (%_CallFunction(newThisArg, element, i, array, predicate)) { |
| 104 return element; | 96 return element; |
| 105 } | 97 } |
| 106 } | 98 } |
| 107 } | 99 } |
| 108 | 100 |
| 109 return; | 101 return; |
| 110 } | 102 } |
| 103 $innerArrayFind = InnerArrayFind; | |
| 111 | 104 |
| 112 | 105 // ES6 draft 07-15-13, section 15.4.3.23 |
| 113 // ES6 draft 07-15-13, section 15.4.3.24 | 106 function ArrayFind(predicate, thisArg) { |
| 114 function ArrayFindIndex(predicate /* thisArg */) { // length == 1 | 107 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); |
| 115 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex"); | |
| 116 | 108 |
| 117 var array = $toObject(this); | 109 var array = $toObject(this); |
| 118 var length = $toInteger(array.length); | 110 var length = $toInteger(array.length); |
| 119 | 111 |
| 112 return InnerArrayFind(predicate, thisArg, array, length); | |
| 113 } | |
| 114 %FunctionSetLength(ArrayFind, 1); | |
| 115 | |
| 116 function InnerArrayFindIndex(predicate, thisArg, array, length) { | |
| 120 if (!IS_SPEC_FUNCTION(predicate)) { | 117 if (!IS_SPEC_FUNCTION(predicate)) { |
| 121 throw MakeTypeError(kCalledNonCallable, predicate); | 118 throw MakeTypeError(kCalledNonCallable, predicate); |
| 122 } | 119 } |
| 123 | 120 |
| 124 var thisArg; | |
| 125 if (%_ArgumentsLength() > 1) { | |
| 126 thisArg = %_Arguments(1); | |
| 127 } | |
| 128 | |
| 129 var needs_wrapper = false; | 121 var needs_wrapper = false; |
| 130 if (IS_NULL(thisArg)) { | 122 if (IS_NULL(thisArg)) { |
| 131 if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED; | 123 if (%IsSloppyModeFunction(predicate)) thisArg = UNDEFINED; |
| 132 } else if (!IS_UNDEFINED(thisArg)) { | 124 } else if (!IS_UNDEFINED(thisArg)) { |
| 133 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); | 125 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); |
| 134 } | 126 } |
| 135 | 127 |
| 136 for (var i = 0; i < length; i++) { | 128 for (var i = 0; i < length; i++) { |
| 137 if (i in array) { | 129 if (i in array) { |
| 138 var element = array[i]; | 130 var element = array[i]; |
| 139 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; | 131 var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; |
| 140 if (%_CallFunction(newThisArg, element, i, array, predicate)) { | 132 if (%_CallFunction(newThisArg, element, i, array, predicate)) { |
| 141 return i; | 133 return i; |
| 142 } | 134 } |
| 143 } | 135 } |
| 144 } | 136 } |
| 145 | 137 |
| 146 return -1; | 138 return -1; |
| 147 } | 139 } |
| 140 $innerArrayFindIndex = InnerArrayFindIndex; | |
| 148 | 141 |
| 142 // ES6 draft 07-15-13, section 15.4.3.24 | |
| 143 function ArrayFindIndex(predicate, thisArg) { | |
| 144 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex"); | |
| 145 | |
| 146 var array = $toObject(this); | |
| 147 var length = $toInteger(array.length); | |
| 148 | |
| 149 return InnerArrayFindIndex(predicate, thisArg, array, length); | |
| 150 } | |
| 151 %FunctionSetLength(ArrayFindIndex, 1); | |
|
adamk
2015/05/12 23:18:43
Please move this down to the other FunctionSetLeng
| |
| 149 | 152 |
| 150 // ES6, draft 04-05-14, section 22.1.3.6 | 153 // ES6, draft 04-05-14, section 22.1.3.6 |
| 151 function ArrayFill(value /* [, start [, end ] ] */) { // length == 1 | 154 function InnerArrayFill(value, start, end, array, length) { |
| 152 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill"); | 155 var i = IS_UNDEFINED(start) ? 0 : TO_INTEGER(start); |
| 153 | 156 var end = IS_UNDEFINED(end) ? length : TO_INTEGER(end); |
| 154 var array = $toObject(this); | |
| 155 var length = TO_UINT32(array.length); | |
| 156 | |
| 157 var i = 0; | |
| 158 var end = length; | |
| 159 | |
| 160 if (%_ArgumentsLength() > 1) { | |
| 161 i = %_Arguments(1); | |
| 162 i = IS_UNDEFINED(i) ? 0 : TO_INTEGER(i); | |
| 163 if (%_ArgumentsLength() > 2) { | |
| 164 end = %_Arguments(2); | |
| 165 end = IS_UNDEFINED(end) ? length : TO_INTEGER(end); | |
| 166 } | |
| 167 } | |
| 168 | 157 |
| 169 if (i < 0) { | 158 if (i < 0) { |
| 170 i += length; | 159 i += length; |
| 171 if (i < 0) i = 0; | 160 if (i < 0) i = 0; |
| 172 } else { | 161 } else { |
| 173 if (i > length) i = length; | 162 if (i > length) i = length; |
| 174 } | 163 } |
| 175 | 164 |
| 176 if (end < 0) { | 165 if (end < 0) { |
| 177 end += length; | 166 end += length; |
| 178 if (end < 0) end = 0; | 167 if (end < 0) end = 0; |
| 179 } else { | 168 } else { |
| 180 if (end > length) end = length; | 169 if (end > length) end = length; |
| 181 } | 170 } |
| 182 | 171 |
| 183 if ((end - i) > 0 && $objectIsFrozen(array)) { | 172 if ((end - i) > 0 && $objectIsFrozen(array)) { |
| 184 throw MakeTypeError(kArrayFunctionsOnFrozen); | 173 throw MakeTypeError(kArrayFunctionsOnFrozen); |
| 185 } | 174 } |
| 186 | 175 |
| 187 for (; i < end; i++) | 176 for (; i < end; i++) |
| 188 array[i] = value; | 177 array[i] = value; |
| 189 return array; | 178 return array; |
| 190 } | 179 } |
| 180 $innerArrayFill = InnerArrayFill; | |
| 181 | |
| 182 // ES6, draft 04-05-14, section 22.1.3.6 | |
| 183 function ArrayFill(value, start, end) { | |
| 184 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.fill"); | |
| 185 | |
| 186 var array = $toObject(this); | |
| 187 var length = TO_UINT32(array.length); | |
| 188 | |
| 189 return InnerArrayFill(value, start, end, array, length); | |
| 190 } | |
| 191 %FunctionSetLength(ArrayFill, 1); | |
|
adamk
2015/05/12 23:18:43
Same here.
| |
| 191 | 192 |
| 192 // ES6, draft 10-14-14, section 22.1.2.1 | 193 // ES6, draft 10-14-14, section 22.1.2.1 |
| 193 function ArrayFrom(arrayLike, mapfn, receiver) { | 194 function ArrayFrom(arrayLike, mapfn, receiver) { |
| 194 var items = $toObject(arrayLike); | 195 var items = $toObject(arrayLike); |
| 195 var mapping = !IS_UNDEFINED(mapfn); | 196 var mapping = !IS_UNDEFINED(mapfn); |
| 196 | 197 |
| 197 if (mapping) { | 198 if (mapping) { |
| 198 if (!IS_SPEC_FUNCTION(mapfn)) { | 199 if (!IS_SPEC_FUNCTION(mapfn)) { |
| 199 throw MakeTypeError(kCalledNonCallable, mapfn); | 200 throw MakeTypeError(kCalledNonCallable, mapfn); |
| 200 } else if (%IsSloppyModeFunction(mapfn)) { | 201 } else if (%IsSloppyModeFunction(mapfn)) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 | 289 |
| 289 // Set up the non-enumerable functions on the Array prototype object. | 290 // Set up the non-enumerable functions on the Array prototype object. |
| 290 $installFunctions(GlobalArray.prototype, DONT_ENUM, [ | 291 $installFunctions(GlobalArray.prototype, DONT_ENUM, [ |
| 291 "copyWithin", ArrayCopyWithin, | 292 "copyWithin", ArrayCopyWithin, |
| 292 "find", ArrayFind, | 293 "find", ArrayFind, |
| 293 "findIndex", ArrayFindIndex, | 294 "findIndex", ArrayFindIndex, |
| 294 "fill", ArrayFill | 295 "fill", ArrayFill |
| 295 ]); | 296 ]); |
| 296 | 297 |
| 297 }) | 298 }) |
| OLD | NEW |