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
3 // found in the LICENSE file.
8 // This file relies on the fact that the following declaration has been made
10 // var $Array = global.Array;
13 var arrayIteratorObjectSymbol = GLOBAL_PRIVATE("ArrayIterator#object");
14 var arrayIteratorNextIndexSymbol = GLOBAL_PRIVATE("ArrayIterator#next");
15 var arrayIterationKindSymbol = GLOBAL_PRIVATE("ArrayIterator#kind");
18 function ArrayIterator() {}
21 // TODO(wingo): Update section numbers when ES6 has stabilized. The
22 // section numbers below are already out of date as of the May 2014
26 // 15.4.5.1 CreateArrayIterator Abstract Operation
27 function CreateArrayIterator(array, kind) {
28 var object = ToObject(array);
29 var iterator = new ArrayIterator;
30 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object);
31 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0);
32 SET_PRIVATE(iterator, arrayIterationKindSymbol, kind);
37 // 15.19.4.3.4 CreateItrResultObject
38 function CreateIteratorResultObject(value, done) {
39 return {value: value, done: done};
43 // 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator]
44 function ArrayIteratorIterator() {
49 // 15.4.5.2.2 ArrayIterator.prototype.next( )
50 function ArrayIteratorNext() {
51 var iterator = ToObject(this);
53 if (!HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) {
54 throw MakeTypeError('incompatible_method_receiver',
55 ['Array Iterator.prototype.next']);
58 var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol);
59 if (IS_UNDEFINED(array)) {
60 return CreateIteratorResultObject(UNDEFINED, true);
63 var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol);
64 var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol);
65 var length = TO_UINT32(array.length);
67 // "sparse" is never used.
69 if (index >= length) {
70 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED);
71 return CreateIteratorResultObject(UNDEFINED, true);
74 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1);
76 if (itemKind == ITERATOR_KIND_VALUES) {
77 return CreateIteratorResultObject(array[index], false);
80 if (itemKind == ITERATOR_KIND_ENTRIES) {
81 return CreateIteratorResultObject([index, array[index]], false);
84 return CreateIteratorResultObject(index, false);
88 function ArrayEntries() {
89 return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES);
93 function ArrayValues() {
94 return CreateArrayIterator(this, ITERATOR_KIND_VALUES);
98 function ArrayKeys() {
99 return CreateArrayIterator(this, ITERATOR_KIND_KEYS);
103 function SetUpArrayIterator() {
104 %CheckIsBootstrapping();
106 %FunctionSetPrototype(ArrayIterator, new $Object());
107 %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator');
109 InstallFunctions(ArrayIterator.prototype, DONT_ENUM, $Array(
110 'next', ArrayIteratorNext
112 %FunctionSetName(ArrayIteratorIterator, '[Symbol.iterator]');
113 %AddNamedProperty(ArrayIterator.prototype, symbolIterator,
114 ArrayIteratorIterator, DONT_ENUM);
116 SetUpArrayIterator();
119 function ExtendArrayPrototype() {
120 %CheckIsBootstrapping();
122 InstallFunctions($Array.prototype, DONT_ENUM, $Array(
123 'entries', ArrayEntries,
124 'values', ArrayValues,
128 %AddNamedProperty($Array.prototype, symbolIterator, ArrayValues, DONT_ENUM);
130 ExtendArrayPrototype();
133 function ExtendTypedArrayPrototypes() {
134 %CheckIsBootstrapping();
136 macro TYPED_ARRAYS(FUNCTION)
139 FUNCTION(Uint16Array)
141 FUNCTION(Uint32Array)
143 FUNCTION(Float32Array)
144 FUNCTION(Float64Array)
145 FUNCTION(Uint8ClampedArray)
148 macro EXTEND_TYPED_ARRAY(NAME)
149 %AddNamedProperty($NAME.prototype, 'entries', ArrayEntries, DONT_ENUM);
150 %AddNamedProperty($NAME.prototype, 'values', ArrayValues, DONT_ENUM);
151 %AddNamedProperty($NAME.prototype, 'keys', ArrayKeys, DONT_ENUM);
152 %AddNamedProperty($NAME.prototype, symbolIterator, ArrayValues, DONT_ENUM);
155 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
157 ExtendTypedArrayPrototypes();