array-iterator.js 4.38 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
var $arrayValues;

7
(function(global, utils) {
8

9
"use strict";
10

11
%CheckIsBootstrapping();
12

13
var GlobalArray = global.Array;
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
macro TYPED_ARRAYS(FUNCTION)
  FUNCTION(Uint8Array)
  FUNCTION(Int8Array)
  FUNCTION(Uint16Array)
  FUNCTION(Int16Array)
  FUNCTION(Uint32Array)
  FUNCTION(Int32Array)
  FUNCTION(Float32Array)
  FUNCTION(Float64Array)
  FUNCTION(Uint8ClampedArray)
endmacro

macro COPY_FROM_GLOBAL(NAME)
  var GlobalNAME = global.NAME;
endmacro

TYPED_ARRAYS(COPY_FROM_GLOBAL)
32

33 34 35
var arrayIteratorObjectSymbol = GLOBAL_PRIVATE("ArrayIterator#object");
var arrayIteratorNextIndexSymbol = GLOBAL_PRIVATE("ArrayIterator#next");
var arrayIterationKindSymbol = GLOBAL_PRIVATE("ArrayIterator#kind");
36

37

38 39
function ArrayIterator() {}

40

41 42 43 44 45
// TODO(wingo): Update section numbers when ES6 has stabilized.  The
// section numbers below are already out of date as of the May 2014
// draft.


46 47
// 15.4.5.1 CreateArrayIterator Abstract Operation
function CreateArrayIterator(array, kind) {
48
  var object = $toObject(array);
49
  var iterator = new ArrayIterator;
50
  SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object);
51 52
  SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, 0);
  SET_PRIVATE(iterator, arrayIterationKindSymbol, kind);
53 54 55
  return iterator;
}

56

57 58 59 60 61
// 15.19.4.3.4 CreateItrResultObject
function CreateIteratorResultObject(value, done) {
  return {value: value, done: done};
}

62

63 64 65 66 67 68
// 22.1.5.2.2 %ArrayIteratorPrototype%[@@iterator]
function ArrayIteratorIterator() {
    return this;
}


69 70
// 15.4.5.2.2 ArrayIterator.prototype.next( )
function ArrayIteratorNext() {
71
  var iterator = $toObject(this);
72

73
  if (!HAS_DEFINED_PRIVATE(iterator, arrayIteratorNextIndexSymbol)) {
74 75
    throw MakeTypeError(kIncompatibleMethodReceiver,
                        'Array Iterator.prototype.next', this);
76 77
  }

78 79 80 81 82
  var array = GET_PRIVATE(iterator, arrayIteratorObjectSymbol);
  if (IS_UNDEFINED(array)) {
    return CreateIteratorResultObject(UNDEFINED, true);
  }

83 84
  var index = GET_PRIVATE(iterator, arrayIteratorNextIndexSymbol);
  var itemKind = GET_PRIVATE(iterator, arrayIterationKindSymbol);
85 86 87 88 89
  var length = TO_UINT32(array.length);

  // "sparse" is never used.

  if (index >= length) {
90
    SET_PRIVATE(iterator, arrayIteratorObjectSymbol, UNDEFINED);
91
    return CreateIteratorResultObject(UNDEFINED, true);
92 93
  }

94
  SET_PRIVATE(iterator, arrayIteratorNextIndexSymbol, index + 1);
95

96
  if (itemKind == ITERATOR_KIND_VALUES) {
97
    return CreateIteratorResultObject(array[index], false);
98
  }
99

100
  if (itemKind == ITERATOR_KIND_ENTRIES) {
101
    return CreateIteratorResultObject([index, array[index]], false);
102
  }
103

104
  return CreateIteratorResultObject(index, false);
105 106
}

107

108
function ArrayEntries() {
109
  return CreateArrayIterator(this, ITERATOR_KIND_ENTRIES);
110 111
}

112

113
function ArrayValues() {
114
  return CreateArrayIterator(this, ITERATOR_KIND_VALUES);
115 116
}

117

118
function ArrayKeys() {
119
  return CreateArrayIterator(this, ITERATOR_KIND_KEYS);
120 121
}

122

123
%FunctionSetPrototype(ArrayIterator, {__proto__: $iteratorPrototype});
124
%FunctionSetInstanceClassName(ArrayIterator, 'Array Iterator');
125

126
utils.InstallFunctions(ArrayIterator.prototype, DONT_ENUM, [
127 128
  'next', ArrayIteratorNext
]);
129
utils.SetFunctionName(ArrayIteratorIterator, symbolIterator);
130 131 132 133
%AddNamedProperty(ArrayIterator.prototype, symbolIterator,
                  ArrayIteratorIterator, DONT_ENUM);
%AddNamedProperty(ArrayIterator.prototype, symbolToStringTag,
                  "Array Iterator", READ_ONLY | DONT_ENUM);
134

135
utils.InstallFunctions(GlobalArray.prototype, DONT_ENUM, [
136 137 138 139
  // No 'values' since it breaks webcompat: http://crbug.com/409858
  'entries', ArrayEntries,
  'keys', ArrayKeys
]);
140

141 142
%AddNamedProperty(GlobalArray.prototype, symbolIterator, ArrayValues,
                  DONT_ENUM);
143 144

macro EXTEND_TYPED_ARRAY(NAME)
145 146 147 148 149
  %AddNamedProperty(GlobalNAME.prototype, 'entries', ArrayEntries, DONT_ENUM);
  %AddNamedProperty(GlobalNAME.prototype, 'values', ArrayValues, DONT_ENUM);
  %AddNamedProperty(GlobalNAME.prototype, 'keys', ArrayKeys, DONT_ENUM);
  %AddNamedProperty(GlobalNAME.prototype, symbolIterator, ArrayValues,
                    DONT_ENUM);
150 151
endmacro

152 153
TYPED_ARRAYS(EXTEND_TYPED_ARRAY)

154 155 156 157 158 159 160
// -------------------------------------------------------------------
// Exports

utils.Export(function(to) {
  to.ArrayIteratorCreateResultObject = CreateIteratorResultObject;
});

161 162
$arrayValues = ArrayValues;

163
})