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.
7 (function(global, utils) {
11 %CheckIsBootstrapping();
13 var GlobalArray = global.Array;
15 macro TYPED_ARRAYS(FUNCTION)
22 FUNCTION(Float32Array)
23 FUNCTION(Float64Array)
24 FUNCTION(Uint8ClampedArray)
27 macro COPY_FROM_GLOBAL(NAME)
28 var GlobalNAME = global.NAME;
31 TYPED_ARRAYS(COPY_FROM_GLOBAL)
33 var arrayIteratorObjectSymbol = GLOBAL_PRIVATE("ArrayIterator#object");
34 var arrayIteratorNextIndexSymbol = GLOBAL_PRIVATE("ArrayIterator#next");
35 var arrayIterationKindSymbol = GLOBAL_PRIVATE("ArrayIterator#kind");
38 function ArrayIterator() {}
41 // TODO(wingo): Update section numbers when ES6 has stabilized. The
42 // section numbers below are already out of date as of the May 2014
46 // 15.4.5.1 CreateArrayIterator Abstract Operation
47 function CreateArrayIterator(array, kind) {
48 var object = $toObject(array);
49 var iterator = new ArrayIterator;
50 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object);
51 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0);
52 SET_PRIVATE(iterator, arrayIterationKindSymbol, kind);
57 // 15.19.4.3.4 CreateItrResultObject
58 function CreateIteratorResultObject(value, done) {
59 return {value: value, done: done};
63 // 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator]
64 function ArrayIteratorIterator() {
69 // 15.4.5.2.2 ArrayIterator.prototype.next( )
70 function ArrayIteratorNext() {
71 var iterator = $toObject(this);
73 if (!HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) {
74 throw MakeTypeError(kIncompatibleMethodReceiver,
75 'Array Iterator.prototype.next', this);
78 var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol);
79 if (IS_UNDEFINED(array)) {
80 return CreateIteratorResultObject(UNDEFINED, true);
83 var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol);
84 var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol);
85 var length = TO_UINT32(array.length);
87 // "sparse" is never used.
89 if (index >= length) {
90 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED);
91 return CreateIteratorResultObject(UNDEFINED, true);
94 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1);
96 if (itemKind == ITERATOR_KIND_VALUES) {
97 return CreateIteratorResultObject(array[index], false);
100 if (itemKind == ITERATOR_KIND_ENTRIES) {
101 return CreateIteratorResultObject([index, array[index]], false);
104 return CreateIteratorResultObject(index, false);
108 function ArrayEntries() {
109 return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES);
113 function ArrayValues() {
114 return CreateArrayIterator(this, ITERATOR_KIND_VALUES);
118 function ArrayKeys() {
119 return CreateArrayIterator(this, ITERATOR_KIND_KEYS);
123 %FunctionSetPrototype(ArrayIterator, {__proto__: $iteratorPrototype});
124 %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator');
126 utils.InstallFunctions(ArrayIterator.prototype, DONT_ENUM, [
127 'next', ArrayIteratorNext
129 utils.SetFunctionName(ArrayIteratorIterator, symbolIterator);
130 %AddNamedProperty(ArrayIterator.prototype, symbolIterator,
131 ArrayIteratorIterator, DONT_ENUM);
132 %AddNamedProperty(ArrayIterator.prototype, symbolToStringTag,
133 "Array Iterator", READ_ONLY | DONT_ENUM);
135 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
136 // No 'values' since it breaks webcompat: http://crbug.com/409858
137 'entries', ArrayEntries,
141 // TODO(adam): Remove this call once 'values' is in the above
142 // InstallFunctions block, as it'll be redundant.
143 utils.SetFunctionName(ArrayValues, 'values');
145 %AddNamedProperty(GlobalArray.prototype, symbolIterator, ArrayValues,
148 macro EXTEND_TYPED_ARRAY(NAME)
149 %AddNamedProperty(GlobalNAME.prototype, 'entries', ArrayEntries, DONT_ENUM);
150 %AddNamedProperty(GlobalNAME.prototype, 'values', ArrayValues, DONT_ENUM);
151 %AddNamedProperty(GlobalNAME.prototype, 'keys', ArrayKeys, DONT_ENUM);
152 %AddNamedProperty(GlobalNAME.prototype, symbolIterator, ArrayValues,
156 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
158 // -------------------------------------------------------------------
161 utils.Export(function(to) {
162 to.ArrayIteratorCreateResultObject = CreateIteratorResultObject;
165 $arrayValues = ArrayValues;