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 // -----------------------------------------------------------------------
16 var arrayIterationKindSymbol =
17 utils.ImportNow("array_iteration_kind_symbol");
18 var arrayIteratorNextIndexSymbol =
19 utils.ImportNow("array_iterator_next_symbol");
20 var arrayIteratorObjectSymbol =
21 utils.ImportNow("array_iterator_object_symbol");
22 var GlobalArray = global.Array;
23 var iteratorSymbol = utils.ImportNow("iterator_symbol");
24 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
26 macro TYPED_ARRAYS(FUNCTION)
33 FUNCTION(Float32Array)
34 FUNCTION(Float64Array)
35 FUNCTION(Uint8ClampedArray)
38 macro COPY_FROM_GLOBAL(NAME)
39 var GlobalNAME = global.NAME;
42 TYPED_ARRAYS(COPY_FROM_GLOBAL)
44 // -----------------------------------------------------------------------
46 function ArrayIterator() {}
49 // TODO(wingo): Update section numbers when ES6 has stabilized. The
50 // section numbers below are already out of date as of the May 2014
54 // 15.4.5.1 CreateArrayIterator Abstract Operation
55 function CreateArrayIterator(array, kind) {
56 var object = TO_OBJECT(array);
57 var iterator = new ArrayIterator;
58 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object);
59 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0);
60 SET_PRIVATE(iterator, arrayIterationKindSymbol, kind);
65 // 15.19.4.3.4 CreateItrResultObject
66 function CreateIteratorResultObject(value, done) {
67 return {value: value, done: done};
71 // 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator]
72 function ArrayIteratorIterator() {
77 // 15.4.5.2.2 ArrayIterator.prototype.next( )
78 function ArrayIteratorNext() {
81 if (!IS_SPEC_OBJECT(iterator) ||
82 !HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) {
83 throw MakeTypeError(kIncompatibleMethodReceiver,
84 'Array Iterator.prototype.next', this);
87 var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol);
88 if (IS_UNDEFINED(array)) {
89 return CreateIteratorResultObject(UNDEFINED, true);
92 var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol);
93 var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol);
94 var length = TO_UINT32(array.length);
96 // "sparse" is never used.
98 if (index >= length) {
99 SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED);
100 return CreateIteratorResultObject(UNDEFINED, true);
103 SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1);
105 if (itemKind == ITERATOR_KIND_VALUES) {
106 return CreateIteratorResultObject(array[index], false);
109 if (itemKind == ITERATOR_KIND_ENTRIES) {
110 return CreateIteratorResultObject([index, array[index]], false);
113 return CreateIteratorResultObject(index, false);
117 function ArrayEntries() {
118 return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES);
122 function ArrayValues() {
123 return CreateArrayIterator(this, ITERATOR_KIND_VALUES);
127 function ArrayKeys() {
128 return CreateArrayIterator(this, ITERATOR_KIND_KEYS);
132 %FunctionSetPrototype(ArrayIterator, {__proto__: $iteratorPrototype});
133 %FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator');
135 utils.InstallFunctions(ArrayIterator.prototype, DONT_ENUM, [
136 'next', ArrayIteratorNext
138 utils.SetFunctionName(ArrayIteratorIterator, iteratorSymbol);
139 %AddNamedProperty(ArrayIterator.prototype, iteratorSymbol,
140 ArrayIteratorIterator, DONT_ENUM);
141 %AddNamedProperty(ArrayIterator.prototype, toStringTagSymbol,
142 "Array Iterator", READ_ONLY | DONT_ENUM);
144 utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
145 // No 'values' since it breaks webcompat: http://crbug.com/409858
146 'entries', ArrayEntries,
150 // TODO(adam): Remove this call once 'values' is in the above
151 // InstallFunctions block, as it'll be redundant.
152 utils.SetFunctionName(ArrayValues, 'values');
154 %AddNamedProperty(GlobalArray.prototype, iteratorSymbol, ArrayValues,
157 macro EXTEND_TYPED_ARRAY(NAME)
158 %AddNamedProperty(GlobalNAME.prototype, 'entries', ArrayEntries, DONT_ENUM);
159 %AddNamedProperty(GlobalNAME.prototype, 'values', ArrayValues, DONT_ENUM);
160 %AddNamedProperty(GlobalNAME.prototype, 'keys', ArrayKeys, DONT_ENUM);
161 %AddNamedProperty(GlobalNAME.prototype, iteratorSymbol, ArrayValues,
165 TYPED_ARRAYS(EXTEND_TYPED_ARRAY)
167 // -------------------------------------------------------------------
170 utils.Export(function(to) {
171 to.ArrayIteratorCreateResultObject = CreateIteratorResultObject;
174 $arrayValues = ArrayValues;
176 %InstallToContext(["array_values_iterator", ArrayValues]);