1196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org// Copyright 2014 the V8 project authors. All rights reserved.
2196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org// Use of this source code is governed by a BSD-style license that can be
3196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org// found in the LICENSE file.
4196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
5196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org'use strict';
6196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
7196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
8196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org// This file relies on the fact that the following declaration has been made
9196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org// in runtime.js:
10196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org// var $Set = global.Set;
11196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org// var $Map = global.Map;
12196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
13196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
14196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction SetIteratorConstructor(set, kind) {
15196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %SetIteratorInitialize(this, set, kind);
16196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
17196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
18196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
19196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction SetIteratorNextJS() {
20196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  if (!IS_SET_ITERATOR(this)) {
21196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    throw MakeTypeError('incompatible_method_receiver',
22196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org                        ['Set Iterator.prototype.next', this]);
23196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  }
2447390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org
2547390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  var value_array = [UNDEFINED, UNDEFINED];
2647390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  var entry = {value: value_array, done: false};
2747390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  switch (%SetIteratorNext(this, value_array)) {
2847390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org    case 0:
2947390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      entry.value = UNDEFINED;
3047390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      entry.done = true;
3147390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      break;
3247390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org    case ITERATOR_KIND_VALUES:
3347390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      entry.value = value_array[0];
3447390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      break;
3547390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org    case ITERATOR_KIND_ENTRIES:
3647390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      value_array[1] = value_array[0];
3747390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      break;
3847390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  }
3947390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org
4047390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  return entry;
41196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
42196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
43196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
44196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction SetIteratorSymbolIterator() {
45196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  return this;
46196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
47196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
48196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
49196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction SetEntries() {
50196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  if (!IS_SET(this)) {
51196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    throw MakeTypeError('incompatible_method_receiver',
52196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org                        ['Set.prototype.entries', this]);
53196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  }
54196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  return new SetIterator(this, ITERATOR_KIND_ENTRIES);
55196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
56196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
57196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
58196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction SetValues() {
59196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  if (!IS_SET(this)) {
60196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    throw MakeTypeError('incompatible_method_receiver',
61196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org                        ['Set.prototype.values', this]);
62196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  }
63196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  return new SetIterator(this, ITERATOR_KIND_VALUES);
64196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
65196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
66196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
67196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction SetUpSetIterator() {
68196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %CheckIsBootstrapping();
69196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
70196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %SetCode(SetIterator, SetIteratorConstructor);
71196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %FunctionSetPrototype(SetIterator, new $Object());
72196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %FunctionSetInstanceClassName(SetIterator, 'Set Iterator');
73196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  InstallFunctions(SetIterator.prototype, DONT_ENUM, $Array(
74196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    'next', SetIteratorNextJS
75196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  ));
76196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
77196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %FunctionSetName(SetIteratorSymbolIterator, '[Symbol.iterator]');
789bf7aff6cc5ed8807b7b2abc11b6cf77b928ded1machenbach@chromium.org  %AddNamedProperty(SetIterator.prototype, symbolIterator,
79196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org      SetIteratorSymbolIterator, DONT_ENUM);
80196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
81196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
82196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgSetUpSetIterator();
83196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
84196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
85196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction ExtendSetPrototype() {
86196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %CheckIsBootstrapping();
87196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
88196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  InstallFunctions($Set.prototype, DONT_ENUM, $Array(
89196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    'entries', SetEntries,
905de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org    'keys', SetValues,
91196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    'values', SetValues
92196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  ));
93196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
949bf7aff6cc5ed8807b7b2abc11b6cf77b928ded1machenbach@chromium.org  %AddNamedProperty($Set.prototype, symbolIterator, SetValues, DONT_ENUM);
95196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
96196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
97196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgExtendSetPrototype();
98196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
99196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
100196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
101196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction MapIteratorConstructor(map, kind) {
102196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %MapIteratorInitialize(this, map, kind);
103196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
104196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
105196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
106196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction MapIteratorSymbolIterator() {
107196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  return this;
108196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
109196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
110196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
111196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction MapIteratorNextJS() {
112196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  if (!IS_MAP_ITERATOR(this)) {
113196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    throw MakeTypeError('incompatible_method_receiver',
114196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org                        ['Map Iterator.prototype.next', this]);
115196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  }
11647390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org
11747390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  var value_array = [UNDEFINED, UNDEFINED];
11847390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  var entry = {value: value_array, done: false};
11947390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  switch (%MapIteratorNext(this, value_array)) {
12047390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org    case 0:
12147390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      entry.value = UNDEFINED;
12247390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      entry.done = true;
12347390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      break;
12447390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org    case ITERATOR_KIND_KEYS:
12547390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      entry.value = value_array[0];
12647390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      break;
12747390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org    case ITERATOR_KIND_VALUES:
12847390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      entry.value = value_array[1];
12947390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org      break;
13047390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org    // ITERATOR_KIND_ENTRIES does not need any processing.
13147390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  }
13247390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org
13347390597afd6b17870f41dfb5dd8c057aea1f068machenbach@chromium.org  return entry;
134196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
135196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
136196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
137196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction MapEntries() {
138196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  if (!IS_MAP(this)) {
139196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    throw MakeTypeError('incompatible_method_receiver',
140196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org                        ['Map.prototype.entries', this]);
141196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  }
142196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  return new MapIterator(this, ITERATOR_KIND_ENTRIES);
143196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
144196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
145196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
146196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction MapKeys() {
147196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  if (!IS_MAP(this)) {
148196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    throw MakeTypeError('incompatible_method_receiver',
149196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org                        ['Map.prototype.keys', this]);
150196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  }
151196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  return new MapIterator(this, ITERATOR_KIND_KEYS);
152196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
153196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
154196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
155196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction MapValues() {
156196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  if (!IS_MAP(this)) {
157196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    throw MakeTypeError('incompatible_method_receiver',
158196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org                        ['Map.prototype.values', this]);
159196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  }
160196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  return new MapIterator(this, ITERATOR_KIND_VALUES);
161196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
162196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
163196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
164196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction SetUpMapIterator() {
165196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %CheckIsBootstrapping();
166196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
167196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %SetCode(MapIterator, MapIteratorConstructor);
168196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %FunctionSetPrototype(MapIterator, new $Object());
169196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %FunctionSetInstanceClassName(MapIterator, 'Map Iterator');
170196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  InstallFunctions(MapIterator.prototype, DONT_ENUM, $Array(
171196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    'next', MapIteratorNextJS
172196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  ));
173196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
174196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %FunctionSetName(MapIteratorSymbolIterator, '[Symbol.iterator]');
1759bf7aff6cc5ed8807b7b2abc11b6cf77b928ded1machenbach@chromium.org  %AddNamedProperty(MapIterator.prototype, symbolIterator,
176196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org      MapIteratorSymbolIterator, DONT_ENUM);
177196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
178196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
179196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgSetUpMapIterator();
180196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
181196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
182196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgfunction ExtendMapPrototype() {
183196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  %CheckIsBootstrapping();
184196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
185196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  InstallFunctions($Map.prototype, DONT_ENUM, $Array(
186196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    'entries', MapEntries,
187196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    'keys', MapKeys,
188196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org    'values', MapValues
189196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org  ));
190196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
1919bf7aff6cc5ed8807b7b2abc11b6cf77b928ded1machenbach@chromium.org  %AddNamedProperty($Map.prototype, symbolIterator, MapEntries, DONT_ENUM);
192196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org}
193196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org
194196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.orgExtendMapPrototype();
195