 Chromium Code Reviews
 Chromium Code Reviews Issue 376623004:
  [es6] implement Array.prototype.copyWithin()  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
    
  
    Issue 376623004:
  [es6] implement Array.prototype.copyWithin()  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge| 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 | 
| 11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- | 
| 12 | 12 | 
| 13 // ES6 draft 03-17-15, section 22.1.3.3 | |
| 14 function ArrayCopyWithin(target, start, end) { | |
| 15 var array = TO_OBJECT_INLINE(this); | |
| 16 var length = ToLength(array.length); | |
| 
adamk
2015/04/20 17:26:14
Please add a test with an object whose length > 2^
 
caitp (gmail)
2015/04/20 17:53:54
Done.
 | |
| 17 | |
| 18 target = TO_INTEGER(target); | |
| 19 var to; | |
| 20 if (target < 0) { | |
| 21 to = $max(length + target, 0); | |
| 22 } else { | |
| 23 to = $min(target, length); | |
| 24 } | |
| 25 | |
| 26 start = TO_INTEGER(start); | |
| 27 var from; | |
| 28 if (start < 0) { | |
| 29 from = $max(length + start, 0); | |
| 30 } else { | |
| 31 from = $min(start, length); | |
| 32 } | |
| 33 | |
| 34 end = IS_UNDEFINED(end) ? length : TO_INTEGER(end); | |
| 35 var final; | |
| 36 if (end < 0) { | |
| 37 final = $max(length + end, 0); | |
| 38 } else { | |
| 39 final = $min(end, length); | |
| 40 } | |
| 41 | |
| 42 var count = $min(final - from, length - to); | |
| 43 if (from < to && to < (from + count)) { | |
| 44 var direction = -1; | |
| 45 from = from + count - 1; | |
| 46 to = to + count - 1; | |
| 47 } else { | |
| 48 var direction = 1; | |
| 
adamk
2015/04/20 17:26:14
One more style nit: move this to just before the i
 
caitp (gmail)
2015/04/20 17:53:54
Done.
 | |
| 49 } | |
| 50 | |
| 51 while (count > 0) { | |
| 52 if (from in array) { | |
| 53 array[to] = array[from]; | |
| 54 } else { | |
| 55 delete array[to]; | |
| 56 } | |
| 57 from = from + direction; | |
| 58 to = to + direction; | |
| 59 count--; | |
| 60 } | |
| 61 | |
| 62 return array; | |
| 63 } | |
| 64 | |
| 13 // ES6 draft 07-15-13, section 15.4.3.23 | 65 // ES6 draft 07-15-13, section 15.4.3.23 | 
| 14 function ArrayFind(predicate /* thisArg */) { // length == 1 | 66 function ArrayFind(predicate /* thisArg */) { // length == 1 | 
| 15 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); | 67 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); | 
| 16 | 68 | 
| 17 var array = ToObject(this); | 69 var array = ToObject(this); | 
| 18 var length = ToInteger(array.length); | 70 var length = ToInteger(array.length); | 
| 19 | 71 | 
| 20 if (!IS_SPEC_FUNCTION(predicate)) { | 72 if (!IS_SPEC_FUNCTION(predicate)) { | 
| 21 throw MakeTypeError(kCalledNonCallable, predicate); | 73 throw MakeTypeError(kCalledNonCallable, predicate); | 
| 22 } | 74 } | 
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 // TODO(dslomov, caitp): Move to symbol.js when shipping | 267 // TODO(dslomov, caitp): Move to symbol.js when shipping | 
| 216 "isConcatSpreadable", symbolIsConcatSpreadable | 268 "isConcatSpreadable", symbolIsConcatSpreadable | 
| 217 ]); | 269 ]); | 
| 218 } | 270 } | 
| 219 | 271 | 
| 220 HarmonyArrayExtendSymbolPrototype(); | 272 HarmonyArrayExtendSymbolPrototype(); | 
| 221 | 273 | 
| 222 function HarmonyArrayExtendArrayPrototype() { | 274 function HarmonyArrayExtendArrayPrototype() { | 
| 223 %CheckIsBootstrapping(); | 275 %CheckIsBootstrapping(); | 
| 224 | 276 | 
| 277 %FunctionSetLength(ArrayCopyWithin, 2); | |
| 225 %FunctionSetLength(ArrayFrom, 1); | 278 %FunctionSetLength(ArrayFrom, 1); | 
| 226 | 279 | 
| 227 // Set up non-enumerable functions on the Array object. | 280 // Set up non-enumerable functions on the Array object. | 
| 228 InstallFunctions($Array, DONT_ENUM, [ | 281 InstallFunctions($Array, DONT_ENUM, [ | 
| 229 "from", ArrayFrom, | 282 "from", ArrayFrom, | 
| 230 "of", ArrayOf | 283 "of", ArrayOf | 
| 231 ]); | 284 ]); | 
| 232 | 285 | 
| 233 // Set up the non-enumerable functions on the Array prototype object. | 286 // Set up the non-enumerable functions on the Array prototype object. | 
| 234 InstallFunctions($Array.prototype, DONT_ENUM, [ | 287 InstallFunctions($Array.prototype, DONT_ENUM, [ | 
| 288 "copyWithin", ArrayCopyWithin, | |
| 235 "find", ArrayFind, | 289 "find", ArrayFind, | 
| 236 "findIndex", ArrayFindIndex, | 290 "findIndex", ArrayFindIndex, | 
| 237 "fill", ArrayFill | 291 "fill", ArrayFill | 
| 238 ]); | 292 ]); | 
| 239 } | 293 } | 
| 240 | 294 | 
| 241 HarmonyArrayExtendArrayPrototype(); | 295 HarmonyArrayExtendArrayPrototype(); | 
| OLD | NEW |