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