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

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

Issue 376623004: [es6] implement Array.prototype.copyWithin() (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: rebase Created 5 years, 8 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 | test/mjsunit/harmony/array-copywithin.js » ('j') | no next file with comments »
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 (function() { 5 (function() {
6 6
7 'use strict'; 7 'use strict';
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
11 var GlobalArray = global.Array; 11 var GlobalArray = global.Array;
12 var GlobalSymbol = global.Symbol; 12 var GlobalSymbol = global.Symbol;
13 13
14 // ------------------------------------------------------------------- 14 // -------------------------------------------------------------------
15 15
16 // ES6 draft 03-17-15, section 22.1.3.3
17 function ArrayCopyWithin(target, start, end) {
18 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");
19
20 var array = TO_OBJECT_INLINE(this);
21 var length = ToLength(array.length);
22
23 target = TO_INTEGER(target);
24 var to;
25 if (target < 0) {
26 to = $max(length + target, 0);
27 } else {
28 to = $min(target, length);
29 }
30
31 start = TO_INTEGER(start);
32 var from;
33 if (start < 0) {
34 from = $max(length + start, 0);
35 } else {
36 from = $min(start, length);
37 }
38
39 end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
40 var final;
41 if (end < 0) {
42 final = $max(length + end, 0);
43 } else {
44 final = $min(end, length);
45 }
46
47 var count = $min(final - from, length - to);
48 var direction = 1;
49 if (from < to && to < (from + count)) {
50 direction = -1;
51 from = from + count - 1;
52 to = to + count - 1;
53 }
54
55 while (count > 0) {
56 if (from in array) {
57 array[to] = array[from];
58 } else {
59 delete array[to];
60 }
61 from = from + direction;
62 to = to + direction;
63 count--;
64 }
65
66 return array;
67 }
68
16 // ES6 draft 07-15-13, section 15.4.3.23 69 // ES6 draft 07-15-13, section 15.4.3.23
17 function ArrayFind(predicate /* thisArg */) { // length == 1 70 function ArrayFind(predicate /* thisArg */) { // length == 1
18 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); 71 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
19 72
20 var array = ToObject(this); 73 var array = ToObject(this);
21 var length = ToInteger(array.length); 74 var length = ToInteger(array.length);
22 75
23 if (!IS_SPEC_FUNCTION(predicate)) { 76 if (!IS_SPEC_FUNCTION(predicate)) {
24 throw MakeTypeError(kCalledNonCallable, predicate); 77 throw MakeTypeError(kCalledNonCallable, predicate);
25 } 78 }
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 return array; 262 return array;
210 } 263 }
211 264
212 // ------------------------------------------------------------------- 265 // -------------------------------------------------------------------
213 266
214 InstallConstants(GlobalSymbol, [ 267 InstallConstants(GlobalSymbol, [
215 // TODO(dslomov, caitp): Move to symbol.js when shipping 268 // TODO(dslomov, caitp): Move to symbol.js when shipping
216 "isConcatSpreadable", symbolIsConcatSpreadable 269 "isConcatSpreadable", symbolIsConcatSpreadable
217 ]); 270 ]);
218 271
272 %FunctionSetLength(ArrayCopyWithin, 2);
219 %FunctionSetLength(ArrayFrom, 1); 273 %FunctionSetLength(ArrayFrom, 1);
220 274
221 // Set up non-enumerable functions on the Array object. 275 // Set up non-enumerable functions on the Array object.
222 InstallFunctions(GlobalArray, DONT_ENUM, [ 276 InstallFunctions(GlobalArray, DONT_ENUM, [
223 "from", ArrayFrom, 277 "from", ArrayFrom,
224 "of", ArrayOf 278 "of", ArrayOf
225 ]); 279 ]);
226 280
227 // Set up the non-enumerable functions on the Array prototype object. 281 // Set up the non-enumerable functions on the Array prototype object.
228 InstallFunctions(GlobalArray.prototype, DONT_ENUM, [ 282 InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
283 "copyWithin", ArrayCopyWithin,
229 "find", ArrayFind, 284 "find", ArrayFind,
230 "findIndex", ArrayFindIndex, 285 "findIndex", ArrayFindIndex,
231 "fill", ArrayFill 286 "fill", ArrayFill
232 ]); 287 ]);
233 288
234 })(); 289 })();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/array-copywithin.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698