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 var needs_wrapper = false; |
29 if (IS_NULL_OR_UNDEFINED(thisArg)) { | 30 if (IS_NULL_OR_UNDEFINED(thisArg)) { |
30 thisArg = %GetDefaultReceiver(predicate) || thisArg; | 31 thisArg = %GetDefaultReceiver(predicate) || thisArg; |
31 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) { | 32 } else { |
32 thisArg = ToObject(thisArg); | 33 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); |
33 } | 34 } |
34 | 35 |
35 for (var i = 0; i < length; i++) { | 36 for (var i = 0; i < length; i++) { |
36 if (i in array) { | 37 if (i in array) { |
37 var element = array[i]; | 38 var element = array[i]; |
38 if (%_CallFunction(thisArg, element, i, array, predicate)) { | 39 var newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg; |
| 40 if (%_CallFunction(newThisArg, element, i, array, predicate)) { |
39 return element; | 41 return element; |
40 } | 42 } |
41 } | 43 } |
42 } | 44 } |
43 | 45 |
44 return; | 46 return; |
45 } | 47 } |
46 | 48 |
47 | 49 |
48 // ES6 draft 07-15-13, section 15.4.3.24 | 50 // ES6 draft 07-15-13, section 15.4.3.24 |
49 function ArrayFindIndex(predicate /* thisArg */) { // length == 1 | 51 function ArrayFindIndex(predicate /* thisArg */) { // length == 1 |
50 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex"); | 52 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.findIndex"); |
51 | 53 |
52 var array = ToObject(this); | 54 var array = ToObject(this); |
53 var length = ToInteger(array.length); | 55 var length = ToInteger(array.length); |
54 | 56 |
55 if (!IS_SPEC_FUNCTION(predicate)) { | 57 if (!IS_SPEC_FUNCTION(predicate)) { |
56 throw MakeTypeError('called_non_callable', [predicate]); | 58 throw MakeTypeError('called_non_callable', [predicate]); |
57 } | 59 } |
58 | 60 |
59 var thisArg; | 61 var thisArg; |
60 if (%_ArgumentsLength() > 1) { | 62 if (%_ArgumentsLength() > 1) { |
61 thisArg = %_Arguments(1); | 63 thisArg = %_Arguments(1); |
62 } | 64 } |
63 | 65 |
| 66 var needs_wrapper = false; |
64 if (IS_NULL_OR_UNDEFINED(thisArg)) { | 67 if (IS_NULL_OR_UNDEFINED(thisArg)) { |
65 thisArg = %GetDefaultReceiver(predicate) || thisArg; | 68 thisArg = %GetDefaultReceiver(predicate) || thisArg; |
66 } else if (!IS_SPEC_OBJECT(thisArg) && %IsSloppyModeFunction(predicate)) { | 69 } else { |
67 thisArg = ToObject(thisArg); | 70 needs_wrapper = SHOULD_CREATE_WRAPPER(predicate, thisArg); |
68 } | 71 } |
69 | 72 |
70 for (var i = 0; i < length; i++) { | 73 for (var i = 0; i < length; i++) { |
71 if (i in array) { | 74 if (i in array) { |
72 var element = array[i]; | 75 var element = array[i]; |
73 if (%_CallFunction(thisArg, element, i, array, predicate)) { | 76 var newThisArg = needs_wrapper ? ToObject(thisArg) : thisArg; |
| 77 if (%_CallFunction(newThisArg, element, i, array, predicate)) { |
74 return i; | 78 return i; |
75 } | 79 } |
76 } | 80 } |
77 } | 81 } |
78 | 82 |
79 return -1; | 83 return -1; |
80 } | 84 } |
81 | 85 |
82 | 86 |
83 // ES6, draft 04-05-14, section 22.1.3.6 | 87 // ES6, draft 04-05-14, section 22.1.3.6 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 152 |
149 // Set up the non-enumerable functions on the Array prototype object. | 153 // Set up the non-enumerable functions on the Array prototype object. |
150 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | 154 InstallFunctions($Array.prototype, DONT_ENUM, $Array( |
151 "find", ArrayFind, | 155 "find", ArrayFind, |
152 "findIndex", ArrayFindIndex, | 156 "findIndex", ArrayFindIndex, |
153 "fill", ArrayFill | 157 "fill", ArrayFill |
154 )); | 158 )); |
155 } | 159 } |
156 | 160 |
157 HarmonyArrayExtendArrayPrototype(); | 161 HarmonyArrayExtendArrayPrototype(); |
OLD | NEW |