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 // and in symbol.js | |
34 // var $Symbol = global.Symbol; | |
35 | |
36 var s = $Symbol(); | |
wingo
2013/06/13 04:26:18
Is this variable used?
arv (Not doing code reviews)
2013/06/13 13:54:27
Done.
| |
37 | |
38 var ARRAY_ITERATOR_KIND_KEYS = 1; | |
39 var ARRAY_ITERATOR_KIND_VALUES = 2; | |
40 var ARRAY_ITERATOR_KIND_ENTRIES = 3; | |
41 // The spec draft also has "sparse" but it is never used. | |
42 | |
43 var iteratorObjectSymbol = $Symbol(); | |
44 var arrayIteratorNextIndexSymbol = $Symbol(); | |
45 var arrayIterationKindSymbol = $Symbol(); | |
46 | |
47 function ArrayIterator() {} | |
48 | |
49 // 15.4.5.1 CreateArrayIterator Abstract Operation | |
50 function CreateArrayIterator(array, kind) { | |
51 var object = ToObject(array); | |
52 var iterator = new ArrayIterator; | |
53 iterator[iteratorObjectSymbol] = object; | |
54 iterator[arrayIteratorNextIndexSymbol] = 0; | |
55 iterator[arrayIterationKindSymbol] = kind; | |
56 return iterator; | |
57 } | |
58 | |
59 // 15.19.4.3.4 CreateItrResultObject | |
60 function CreateIteratorResultObject(value, done) { | |
61 return {value: value, done: done}; | |
62 } | |
63 | |
64 // 15.4.5.2.2 ArrayIterator.prototype.next( ) | |
65 function ArrayIteratorNext() { | |
66 var iterator = ToObject(this); | |
67 var array = iterator[iteratorObjectSymbol]; | |
68 if (!array) { | |
69 throw MakeTypeError('incompatible_method_receiver', | |
70 ['Array Iterator.prototype.next']); | |
71 } | |
72 | |
rossberg
2013/06/13 08:22:02
Nit: duplicated empty line
arv (Not doing code reviews)
2013/06/13 13:54:27
Done.
| |
73 | |
74 var index = iterator[arrayIteratorNextIndexSymbol]; | |
75 var itemKind = iterator[arrayIterationKindSymbol]; | |
76 var length = TO_UINT32(array.length); | |
77 | |
78 // "sparse" is never used. | |
79 | |
80 if (index >= length) { | |
81 iterator[arrayIteratorNextIndexSymbol] = 1 / 0; // Infinity | |
82 return CreateIteratorResultObject(void 0, true); | |
83 } | |
84 | |
85 var elementKey = ToString(index); | |
86 iterator[arrayIteratorNextIndexSymbol] = index + 1; | |
87 | |
88 if (itemKind == ARRAY_ITERATOR_KIND_VALUES) | |
89 return CreateIteratorResultObject(array[elementKey], false); | |
90 | |
91 if (itemKind == ARRAY_ITERATOR_KIND_ENTRIES) | |
92 return CreateIteratorResultObject([elementKey, array[elementKey]], false); | |
93 | |
94 return CreateIteratorResultObject(elementKey, false); | |
95 } | |
96 | |
97 function ArrayEntries() { | |
98 return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_ENTRIES); | |
99 } | |
100 | |
101 function ArrayValues() { | |
102 return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_VALUES); | |
103 } | |
104 | |
105 function ArrayKeys() { | |
106 return CreateArrayIterator(this, ARRAY_ITERATOR_KIND_KEYS); | |
107 } | |
108 | |
109 function SetUpArrayIterator() { | |
110 %CheckIsBootstrapping(); | |
111 | |
112 %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator'); | |
113 %FunctionSetReadOnlyPrototype(ArrayIterator); | |
114 | |
115 InstallFunctions(ArrayIterator.prototype, DONT_ENUM, $Array( | |
116 'next', ArrayIteratorNext | |
117 )); | |
118 } | |
119 | |
120 SetUpArrayIterator(); | |
121 | |
122 function ExtendArrayPrototype() { | |
123 %CheckIsBootstrapping(); | |
124 | |
125 InstallFunctions($Array.prototype, DONT_ENUM, $Array( | |
126 'entries', ArrayEntries, | |
127 'values', ArrayValues, | |
128 'keys', ArrayKeys | |
129 )); | |
130 } | |
131 | |
132 ExtendArrayPrototype(); | |
OLD | NEW |