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

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 '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 05-22-14, section 22.1.3.3
14 function ArrayCopyWithin(target, start /* [ , end ] */ ) { // length == 2
15 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");
adamk 2015/04/20 17:26:14 For consistency I feel like we might as well leave
16
17 var array = ToObject(this);
18 var length = ToInteger(array.length);
19 var to;
20 var from;
21 var relativeEnd = length;
22 var end;
23 var final;
24 var count;
25 var direction = 1;
26 var fromVal;
27
28 target = ToInteger(target);
29 if (target < 0) {
30 to = MathMax(length + target, 0);
31 } else {
32 to = MathMin(target, length);
33 }
34
35 start = ToInteger(start);
36
37 if (start < 0) {
38 from = MathMax(length + start, 0);
39 } else {
40 from = MathMin(start, length);
41 }
42
43 if (%_ArgumentsLength() > 2) {
44 end = %_Arguments(2);
45 if (!IS_UNDEFINED(end)) {
46 relativeEnd = ToInteger(end);
47 }
48 }
49
50 if (relativeEnd < 0) {
51 final = MathMax(length + relativeEnd, 0);
52 } else {
53 final = MathMin(relativeEnd, length);
54 }
55
56 count = MathMin(final - from, length - to);
57
58 if (from < to && to < (from + count)) {
59 direction = -1;
60 from = from + count - 1;
61 to = to + count - 1;
62 }
63
64 while (count > 0) {
65 if (from in array) {
66 fromVal = array[from];
67 array[to] = fromVal;
68 } else {
69 delete array[to];
70 }
71 from = from + direction;
72 to = to + direction;
73 count--;
74 }
75
76 return array;
77 }
78
13 // ES6 draft 07-15-13, section 15.4.3.23 79 // ES6 draft 07-15-13, section 15.4.3.23
14 function ArrayFind(predicate /* thisArg */) { // length == 1 80 function ArrayFind(predicate /* thisArg */) { // length == 1
15 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); 81 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
16 82
17 var array = ToObject(this); 83 var array = ToObject(this);
18 var length = ToInteger(array.length); 84 var length = ToInteger(array.length);
19 85
20 if (!IS_SPEC_FUNCTION(predicate)) { 86 if (!IS_SPEC_FUNCTION(predicate)) {
21 throw MakeTypeError(kCalledNonCallable, predicate); 87 throw MakeTypeError(kCalledNonCallable, predicate);
22 } 88 }
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 %FunctionSetLength(ArrayFrom, 1); 291 %FunctionSetLength(ArrayFrom, 1);
226 292
227 // Set up non-enumerable functions on the Array object. 293 // Set up non-enumerable functions on the Array object.
228 InstallFunctions($Array, DONT_ENUM, [ 294 InstallFunctions($Array, DONT_ENUM, [
229 "from", ArrayFrom, 295 "from", ArrayFrom,
230 "of", ArrayOf 296 "of", ArrayOf
231 ]); 297 ]);
232 298
233 // Set up the non-enumerable functions on the Array prototype object. 299 // Set up the non-enumerable functions on the Array prototype object.
234 InstallFunctions($Array.prototype, DONT_ENUM, [ 300 InstallFunctions($Array.prototype, DONT_ENUM, [
301 "copyWithin", ArrayCopyWithin,
235 "find", ArrayFind, 302 "find", ArrayFind,
236 "findIndex", ArrayFindIndex, 303 "findIndex", ArrayFindIndex,
237 "fill", ArrayFill 304 "fill", ArrayFill
238 ]); 305 ]);
239 } 306 }
240 307
241 HarmonyArrayExtendArrayPrototype(); 308 HarmonyArrayExtendArrayPrototype();
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