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

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

Issue 553413002: Array.prototype.{every, filter, find, findIndex, forEach, map, some}: Use fresh primitive wrapper f… (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created wrapper. Added tests for strict mode. Fixed nits. Created 6 years, 3 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 07-15-13, section 15.4.3.23 13 // ES6 draft 07-15-13, section 15.4.3.23
14 function ArrayFind(predicate /* thisArg */) { // length == 1 14 function ArrayFind(predicate /* receiver */) { // length == 1
wingo 2014/09/23 18:39:01 So in the last patch I wanted to avoid new_thisArg
Diego Pino 2014/09/24 10:13:01 OK, understood. It's worth noticing that array fu
15 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find"); 15 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.find");
16 16
17 var array = ToObject(this); 17 var array = ToObject(this);
18 var length = ToInteger(array.length); 18 var length = ToInteger(array.length);
19 19
20 if (!IS_SPEC_FUNCTION(predicate)) { 20 if (!IS_SPEC_FUNCTION(predicate)) {
21 throw MakeTypeError('called_non_callable', [predicate]); 21 throw MakeTypeError('called_non_callable', [predicate]);
22 } 22 }
23 23
24 var thisArg; 24 var receiver;
25 if (%_ArgumentsLength() > 1) { 25 if (%_ArgumentsLength() > 1) {
26 thisArg = %_Arguments(1); 26 receiver = %_Arguments(1);
27 } 27 }
28 28
29 if (IS_NULL_OR_UNDEFINED(thisArg)) { 29 if (IS_NULL_OR_UNDEFINED(receiver)) {
30 thisArg = %GetDefaultReceiver(predicate) || thisArg; 30 receiver = %GetDefaultReceiver(predicate) || receiver;
31 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) {
32 thisArg = ToObject(thisArg);
33 } 31 }
32 var needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, receiver);
34 33
35 for (var i = 0; i < length; i++) { 34 for (var i = 0; i < length; i++) {
36 if (i in array) { 35 if (i in array) {
37 var element = array[i]; 36 var element = array[i];
38 if (%_CallFunction(thisArg, element, i, array, predicate)) { 37 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
38 if (%_CallFunction(new_receiver, element, i, array, predicate)) {
39 return element; 39 return element;
40 } 40 }
41 } 41 }
42 } 42 }
43 43
44 return; 44 return;
45 } 45 }
46 46
47 47
48 // ES6 draft 07-15-13, section 15.4.3.24 48 // ES6 draft 07-15-13, section 15.4.3.24
49 function ArrayFindIndex(predicate /* thisArg */) { // length == 1 49 function ArrayFindIndex(predicate /* receiver */) { // length == 1
50 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex"); 50 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex");
51 51
52 var array = ToObject(this); 52 var array = ToObject(this);
53 var length = ToInteger(array.length); 53 var length = ToInteger(array.length);
54 54
55 if (!IS_SPEC_FUNCTION(predicate)) { 55 if (!IS_SPEC_FUNCTION(predicate)) {
56 throw MakeTypeError('called_non_callable', [predicate]); 56 throw MakeTypeError('called_non_callable', [predicate]);
57 } 57 }
58 58
59 var thisArg; 59 var receiver;
60 if (%_ArgumentsLength() > 1) { 60 if (%_ArgumentsLength() > 1) {
61 thisArg = %_Arguments(1); 61 receiver = %_Arguments(1);
62 } 62 }
63 63
64 if (IS_NULL_OR_UNDEFINED(thisArg)) { 64 if (IS_NULL_OR_UNDEFINED(receiver)) {
65 thisArg = %GetDefaultReceiver(predicate) || thisArg; 65 receiver = %GetDefaultReceiver(predicate) || receiver;
66 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) {
67 thisArg = ToObject(thisArg);
68 } 66 }
67 var needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, receiver);
69 68
70 for (var i = 0; i < length; i++) { 69 for (var i = 0; i < length; i++) {
71 if (i in array) { 70 if (i in array) {
72 var element = array[i]; 71 var element = array[i];
73 if (%_CallFunction(thisArg, element, i, array, predicate)) { 72 var new_receiver = needs_wrapper ? ToObject(receiver) : receiver;
73 if (%_CallFunction(new_receiver, element, i, array, predicate)) {
74 return i; 74 return i;
75 } 75 }
76 } 76 }
77 } 77 }
78 78
79 return -1; 79 return -1;
80 } 80 }
81 81
82 82
83 // ES6, draft 04-05-14, section 22.1.3.6 83 // ES6, draft 04-05-14, section 22.1.3.6
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 148
149 // Set up the non-enumerable functions on the Array prototype object. 149 // Set up the non-enumerable functions on the Array prototype object.
150 InstallFunctions($Array.prototype, DONT_ENUM, $Array( 150 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
151 "find", ArrayFind, 151 "find", ArrayFind,
152 "findIndex", ArrayFindIndex, 152 "findIndex", ArrayFindIndex,
153 "fill", ArrayFill 153 "fill", ArrayFill
154 )); 154 ));
155 } 155 }
156 156
157 HarmonyArrayExtendArrayPrototype(); 157 HarmonyArrayExtendArrayPrototype();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698