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

5
(function(global, utils) {
6

7
"use strict";
8

9
%CheckIsBootstrapping();
10

11 12 13
// -------------------------------------------------------------------
// Imports

14 15
var GlobalMap = global.Map;
var GlobalSet = global.Set;
16
var iteratorSymbol = utils.ImportNow("iterator_symbol");
17
var MapIterator = utils.ImportNow("MapIterator");
18
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
19
var SetIterator = utils.ImportNow("SetIterator");
20

21
// -------------------------------------------------------------------
22 23 24 25 26 27 28 29

function SetIteratorConstructor(set, kind) {
  %SetIteratorInitialize(this, set, kind);
}


function SetIteratorNextJS() {
  if (!IS_SET_ITERATOR(this)) {
30
    throw %make_type_error(kIncompatibleMethodReceiver,
31
                        'Set Iterator.prototype.next', this);
32
  }
33 34

  var value_array = [UNDEFINED, UNDEFINED];
35
  var result = %_CreateIterResultObject(value_array, false);
36 37
  switch (%SetIteratorNext(this, value_array)) {
    case 0:
38 39
      result.value = UNDEFINED;
      result.done = true;
40 41
      break;
    case ITERATOR_KIND_VALUES:
42
      result.value = value_array[0];
43 44 45 46 47 48
      break;
    case ITERATOR_KIND_ENTRIES:
      value_array[1] = value_array[0];
      break;
  }

49
  return result;
50 51 52 53 54
}


function SetEntries() {
  if (!IS_SET(this)) {
55
    throw %make_type_error(kIncompatibleMethodReceiver,
56
                        'Set.prototype.entries', this);
57 58 59 60 61 62 63
  }
  return new SetIterator(this, ITERATOR_KIND_ENTRIES);
}


function SetValues() {
  if (!IS_SET(this)) {
64
    throw %make_type_error(kIncompatibleMethodReceiver,
65
                        'Set.prototype.values', this);
66 67 68 69
  }
  return new SetIterator(this, ITERATOR_KIND_VALUES);
}

70
// -------------------------------------------------------------------
71

72 73
%SetCode(SetIterator, SetIteratorConstructor);
%FunctionSetInstanceClassName(SetIterator, 'Set Iterator');
74
utils.InstallFunctions(SetIterator.prototype, DONT_ENUM, [
75 76
  'next', SetIteratorNextJS
]);
77

78
%AddNamedProperty(SetIterator.prototype, toStringTagSymbol,
79
    "Set Iterator", READ_ONLY | DONT_ENUM);
80

81
utils.InstallFunctions(GlobalSet.prototype, DONT_ENUM, [
82 83 84 85
  'entries', SetEntries,
  'keys', SetValues,
  'values', SetValues
]);
86

87
%AddNamedProperty(GlobalSet.prototype, iteratorSymbol, SetValues, DONT_ENUM);
88

89
// -------------------------------------------------------------------
90 91 92 93 94 95 96 97

function MapIteratorConstructor(map, kind) {
  %MapIteratorInitialize(this, map, kind);
}


function MapIteratorNextJS() {
  if (!IS_MAP_ITERATOR(this)) {
98
    throw %make_type_error(kIncompatibleMethodReceiver,
99
                        'Map Iterator.prototype.next', this);
100
  }
101 102

  var value_array = [UNDEFINED, UNDEFINED];
103
  var result = %_CreateIterResultObject(value_array, false);
104 105
  switch (%MapIteratorNext(this, value_array)) {
    case 0:
106 107
      result.value = UNDEFINED;
      result.done = true;
108 109
      break;
    case ITERATOR_KIND_KEYS:
110
      result.value = value_array[0];
111 112
      break;
    case ITERATOR_KIND_VALUES:
113
      result.value = value_array[1];
114 115 116 117
      break;
    // ITERATOR_KIND_ENTRIES does not need any processing.
  }

118
  return result;
119 120 121 122 123
}


function MapEntries() {
  if (!IS_MAP(this)) {
124
    throw %make_type_error(kIncompatibleMethodReceiver,
125
                        'Map.prototype.entries', this);
126 127 128 129 130 131 132
  }
  return new MapIterator(this, ITERATOR_KIND_ENTRIES);
}


function MapKeys() {
  if (!IS_MAP(this)) {
133
    throw %make_type_error(kIncompatibleMethodReceiver,
134
                        'Map.prototype.keys', this);
135 136 137 138 139 140 141
  }
  return new MapIterator(this, ITERATOR_KIND_KEYS);
}


function MapValues() {
  if (!IS_MAP(this)) {
142
    throw %make_type_error(kIncompatibleMethodReceiver,
143
                        'Map.prototype.values', this);
144 145 146 147
  }
  return new MapIterator(this, ITERATOR_KIND_VALUES);
}

148
// -------------------------------------------------------------------
149

150 151
%SetCode(MapIterator, MapIteratorConstructor);
%FunctionSetInstanceClassName(MapIterator, 'Map Iterator');
152
utils.InstallFunctions(MapIterator.prototype, DONT_ENUM, [
153 154
  'next', MapIteratorNextJS
]);
155

156
%AddNamedProperty(MapIterator.prototype, toStringTagSymbol,
157
    "Map Iterator", READ_ONLY | DONT_ENUM);
158 159


160
utils.InstallFunctions(GlobalMap.prototype, DONT_ENUM, [
161 162 163 164
  'entries', MapEntries,
  'keys', MapKeys,
  'values', MapValues
]);
165

166
%AddNamedProperty(GlobalMap.prototype, iteratorSymbol, MapEntries, DONT_ENUM);
167

168 169 170 171 172 173 174 175 176
// -------------------------------------------------------------------
// Exports

utils.Export(function(to) {
  to.MapEntries = MapEntries;
  to.MapIteratorNext = MapIteratorNextJS;
  to.SetIteratorNext = SetIteratorNextJS;
  to.SetValues = SetValues;
});
177

178
})