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

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: Created 6 years, 5 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
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
adamk 2015/04/17 22:34:17 Another option here is to name the argument and us
caitp (gmail) 2015/04/18 04:50:29 Done, I prefer the %FunctionSetLength() approach
15 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.copyWithin");
16
17 var array = ToObject(this);
adamk 2015/04/17 22:34:16 TO_OBJECT_INLINE
caitp (gmail) 2015/04/18 04:50:29 Done.
18 var length = ToInteger(array.length);
adamk 2015/04/17 22:34:17 TO_UINT32 (hmm, we should figure out this ToLength
caitp (gmail) 2015/04/18 04:50:30 We've had ToLength() for a while --- not sure how
19 var to;
20 var from;
21 var relativeEnd = length;
adamk 2015/04/17 22:34:16 I'd move this down to its initialization to make i
caitp (gmail) 2015/04/18 04:50:29 Done.
22 var end;
23 var final;
24 var count;
25 var direction = 1;
adamk 2015/04/17 22:34:16 Same here, moving this down will make it clearer I
caitp (gmail) 2015/04/18 04:50:30 Done.
26 var fromVal;
27
28 target = ToInteger(target);
adamk 2015/04/17 22:34:17 TO_INTEGER (and similar below)
caitp (gmail) 2015/04/18 04:50:30 Done.
29 if (target < 0) {
30 to = MathMax(length + target, 0);
adamk 2015/04/17 22:34:17 I think this is now spelled $max()
caitp (gmail) 2015/04/18 04:50:29 Done.
31 } else {
32 to = MathMin(target, length);
adamk 2015/04/17 22:34:17 and this is $min()
caitp (gmail) 2015/04/18 04:50:29 Done.
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('called_non_callable', [predicate]); 87 throw MakeTypeError('called_non_callable', [predicate]);
22 } 88 }
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 return array; 189 return array;
124 } 190 }
125 191
126 // ------------------------------------------------------------------- 192 // -------------------------------------------------------------------
127 193
128 function HarmonyArrayExtendArrayPrototype() { 194 function HarmonyArrayExtendArrayPrototype() {
129 %CheckIsBootstrapping(); 195 %CheckIsBootstrapping();
130 196
131 // Set up the non-enumerable functions on the Array prototype object. 197 // Set up the non-enumerable functions on the Array prototype object.
132 InstallFunctions($Array.prototype, DONT_ENUM, $Array( 198 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
199 "copyWithin", ArrayCopyWithin,
133 "find", ArrayFind, 200 "find", ArrayFind,
134 "findIndex", ArrayFindIndex, 201 "findIndex", ArrayFindIndex,
135 "fill", ArrayFill 202 "fill", ArrayFill
136 )); 203 ));
137 } 204 }
138 205
139 HarmonyArrayExtendArrayPrototype(); 206 HarmonyArrayExtendArrayPrototype();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/array-copywithin.js » ('j') | test/mjsunit/harmony/array-copywithin.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698