• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5(function(global, utils) {
6
7"use strict";
8
9%CheckIsBootstrapping();
10
11// -----------------------------------------------------------------------
12// Imports
13
14var arrayIterationKindSymbol =
15    utils.ImportNow("array_iteration_kind_symbol");
16var arrayIteratorNextIndexSymbol =
17    utils.ImportNow("array_iterator_next_symbol");
18var arrayIteratorObjectSymbol =
19    utils.ImportNow("array_iterator_object_symbol");
20var GlobalArray = global.Array;
21var IteratorPrototype = utils.ImportNow("IteratorPrototype");
22var iteratorSymbol = utils.ImportNow("iterator_symbol");
23var MakeTypeError;
24var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
25var GlobalTypedArray = global.Uint8Array.__proto__;
26
27utils.Import(function(from) {
28  MakeTypeError = from.MakeTypeError;
29})
30
31// -----------------------------------------------------------------------
32
33function ArrayIterator() {}
34
35
36// TODO(wingo): Update section numbers when ES6 has stabilized.  The
37// section numbers below are already out of date as of the May 2014
38// draft.
39
40
41// 15.4.5.1 CreateArrayIterator Abstract Operation
42function CreateArrayIterator(array, kind) {
43  var object = TO_OBJECT(array);
44  var iterator = new ArrayIterator;
45  SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object);
46  SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0);
47  SET_PRIVATE(iterator, arrayIterationKindSymbol, kind);
48  return iterator;
49}
50
51
52// 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator]
53function ArrayIteratorIterator() {
54    return this;
55}
56
57
58// ES6 section 22.1.5.2.1 %ArrayIteratorPrototype%.next( )
59function ArrayIteratorNext() {
60  var iterator = this;
61  var value = UNDEFINED;
62  var done = true;
63
64  if (!IS_RECEIVER(iterator) ||
65      !HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) {
66    throw MakeTypeError(kIncompatibleMethodReceiver,
67                        'Array Iterator.prototype.next', this);
68  }
69
70  var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol);
71  if (!IS_UNDEFINED(array)) {
72    var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol);
73    var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol);
74    var length = TO_UINT32(array.length);
75
76    // "sparse" is never used.
77
78    if (index >= length) {
79      SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED);
80    } else {
81      SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1);
82
83      if (itemKind == ITERATOR_KIND_VALUES) {
84        value = array[index];
85      } else if (itemKind == ITERATOR_KIND_ENTRIES) {
86        value = [index, array[index]];
87      } else {
88        value = index;
89      }
90      done = false;
91    }
92  }
93
94  return %_CreateIterResultObject(value, done);
95}
96
97
98function ArrayEntries() {
99  return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES);
100}
101
102
103function ArrayValues() {
104  return CreateArrayIterator(this, ITERATOR_KIND_VALUES);
105}
106
107
108function ArrayKeys() {
109  return CreateArrayIterator(this, ITERATOR_KIND_KEYS);
110}
111
112
113%FunctionSetPrototype(ArrayIterator, {__proto__: IteratorPrototype});
114%FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator');
115
116utils.InstallFunctions(ArrayIterator.prototype, DONT_ENUM, [
117  'next', ArrayIteratorNext
118]);
119utils.SetFunctionName(ArrayIteratorIterator, iteratorSymbol);
120%AddNamedProperty(ArrayIterator.prototype, iteratorSymbol,
121                  ArrayIteratorIterator, DONT_ENUM);
122%AddNamedProperty(ArrayIterator.prototype, toStringTagSymbol,
123                  "Array Iterator", READ_ONLY | DONT_ENUM);
124
125utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
126  // No 'values' since it breaks webcompat: http://crbug.com/409858
127  'entries', ArrayEntries,
128  'keys', ArrayKeys
129]);
130
131// TODO(adam): Remove this call once 'values' is in the above
132// InstallFunctions block, as it'll be redundant.
133utils.SetFunctionName(ArrayValues, 'values');
134
135%AddNamedProperty(GlobalArray.prototype, iteratorSymbol, ArrayValues,
136                  DONT_ENUM);
137
138%AddNamedProperty(GlobalTypedArray.prototype,
139                  'entries', ArrayEntries, DONT_ENUM);
140%AddNamedProperty(GlobalTypedArray.prototype, 'values', ArrayValues, DONT_ENUM);
141%AddNamedProperty(GlobalTypedArray.prototype, 'keys', ArrayKeys, DONT_ENUM);
142%AddNamedProperty(GlobalTypedArray.prototype,
143                  iteratorSymbol, ArrayValues, DONT_ENUM);
144
145// -------------------------------------------------------------------
146// Exports
147
148utils.Export(function(to) {
149  to.ArrayValues = ArrayValues;
150});
151
152%InstallToContext(["array_values_iterator", ArrayValues]);
153
154})
155