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 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 /* thisArg */) { // length == 1 |
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 thisArg; |
25 if (%_ArgumentsLength() > 1) { | 25 if (%_ArgumentsLength() > 1) { |
26 thisArg = %_Arguments(1); | 26 thisArg = %_Arguments(1); |
27 } | 27 } |
28 | 28 |
29 if (IS_NULL_OR_UNDEFINED(thisArg)) { | 29 if (IS_NULL_OR_UNDEFINED(thisArg)) { |
30 thisArg = %GetDefaultReceiver(predicate) || thisArg; | 30 thisArg = %GetDefaultthisArg(predicate) || thisArg; |
wingo
2014/09/30 16:01:36
Typo. Did these tests pass for you? :)
| |
31 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) { | |
32 thisArg = ToObject(thisArg); | |
33 } | 31 } |
32 var needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); | |
wingo
2014/09/30 16:01:36
Here we are adding a test; SHOULD_CREATE_WRAPPER l
| |
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 newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg; |
38 if (%_CallFunction(newThisArg, 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 /* thisArg */) { // 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 thisArg; |
60 if (%_ArgumentsLength() > 1) { | 60 if (%_ArgumentsLength() > 1) { |
61 thisArg = %_Arguments(1); | 61 thisArg = %_Arguments(1); |
62 } | 62 } |
63 | 63 |
64 if (IS_NULL_OR_UNDEFINED(thisArg)) { | 64 if (IS_NULL_OR_UNDEFINED(thisArg)) { |
65 thisArg = %GetDefaultReceiver(predicate) || thisArg; | 65 thisArg = %GetDefaultthisArg(predicate) || thisArg; |
66 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) { | |
67 thisArg = ToObject(thisArg); | |
68 } | 66 } |
67 var needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); | |
wingo
2014/09/30 16:01:36
same here and in similar places in the patch
| |
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 newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg; |
73 if (%_CallFunction(newThisArg, 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 Loading... | |
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(); |
OLD | NEW |