OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 'use strict'; | |
29 | |
30 // This file relies on the fact that the following declaration has been made | |
31 // in runtime.js: | |
32 // var $Array = global.Array; | |
33 | |
34 // ------------------------------------------------------------------- | |
35 | |
36 // ES6 draft 07-15-13, section 15.4.3.23 | |
37 function ArrayFind(predicate /* thisArg */) { // length == 1 | |
38 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | |
39 throw MakeTypeError("called_on_null_or_undefined", | |
40 ["Array.prototype.find"]); | |
41 } | |
42 | |
43 if (!IS_SPEC_FUNCTION(predicate)) { | |
44 throw MakeTypeError('called_non_callable', [predicate]); | |
45 } | |
46 | |
47 var array = ToObject(this); | |
48 var length = ToUint32(array.length); | |
Michael Starzinger
2013/08/01 16:21:02
The access to "array.length" is observable and nee
| |
49 var thisArg; | |
50 if (%_ArgumentsLength() > 1) { | |
51 thisArg = %_Arguments(1); | |
52 } | |
53 | |
54 for(var i = 0; i < length; i++) { | |
55 var key = %ToName(i); | |
56 if (%HasProperty(array, key)) { | |
Michael Starzinger
2013/08/01 16:21:02
Better use the "in" keyword here, that's more effi
| |
57 var element = array[key]; | |
58 if (%_CallFunction(thisArg, element, i, array, predicate)) { | |
Michael Starzinger
2013/08/01 16:21:02
When using the optimized %_CallFunction intrinsic
| |
59 return element; | |
60 } | |
61 } | |
62 } | |
63 | |
64 return; | |
65 } | |
66 | |
67 | |
68 // ES6 draft 07-15-13, section 15.4.3.24 | |
69 function ArrayFindIndex(predicate /* thisArg */) { // length == 1 | |
Michael Starzinger
2013/08/01 16:21:02
Most comments from above apply to this function as
| |
70 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | |
71 throw MakeTypeError("called_on_null_or_undefined", | |
72 ["Array.prototype.findIndex"]); | |
73 } | |
74 | |
75 if (!IS_SPEC_FUNCTION(predicate)) { | |
76 throw MakeTypeError('called_non_callable', [predicate]); | |
77 } | |
78 | |
79 var array = ToObject(this); | |
80 var length = ToUint32(array.length); | |
81 var thisArg; | |
82 if (%_ArgumentsLength() > 1) { | |
83 thisArg = %_Arguments(1); | |
84 } | |
85 | |
86 for(var i = 0; i < length; i++) { | |
87 var key = %ToName(i); | |
88 if (%HasProperty(array, key)) { | |
89 var element = array[key]; | |
90 if (%_CallFunction(thisArg, element, i, array, predicate)) { | |
91 return i; | |
92 } | |
93 } | |
94 } | |
95 | |
96 return -1; | |
97 } | |
98 | |
99 | |
100 // ------------------------------------------------------------------- | |
101 | |
102 function HarmonyArrayExtendArrayPrototype() { | |
103 %CheckIsBootstrapping(); | |
104 | |
105 // Set up the non-enumerable functions on the Array prototype object. | |
106 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | |
107 "find", ArrayFind, | |
108 "findIndex", ArrayFindIndex | |
109 )); | |
110 } | |
111 | |
112 HarmonyArrayExtendArrayPrototype(); | |
OLD | NEW |