1 // Copyright 2012 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 // This file relies on the fact that the following declaration has been made
9 // var $Array = global.Array;
11 var $Set = global.Set;
12 var $Map = global.Map;
15 // -------------------------------------------------------------------
18 function SetConstructor(iterable) {
19 if (!%_IsConstructCall()) {
20 throw MakeTypeError('constructor_not_function', ['Set']);
25 if (!IS_NULL_OR_UNDEFINED(iterable)) {
26 iter = GetIterator(ToObject(iterable));
28 if (!IS_SPEC_FUNCTION(adder)) {
29 throw MakeTypeError('property_not_function', ['add', this]);
35 if (IS_UNDEFINED(iter)) return;
38 while (!(next = iter.next()).done) {
39 if (!IS_SPEC_OBJECT(next)) {
40 throw MakeTypeError('iterator_result_not_an_object', [next]);
42 %_CallFunction(this, next.value, adder);
47 function SetAddJS(key) {
49 throw MakeTypeError('incompatible_method_receiver',
50 ['Set.prototype.add', this]);
52 // Normalize -0 to +0 as required by the spec.
53 // Even though we use SameValueZero as the comparison for the keys we don't
54 // want to ever store -0 as the key since the key is directly exposed when
59 return %SetAdd(this, key);
63 function SetHasJS(key) {
65 throw MakeTypeError('incompatible_method_receiver',
66 ['Set.prototype.has', this]);
68 return %SetHas(this, key);
72 function SetDeleteJS(key) {
74 throw MakeTypeError('incompatible_method_receiver',
75 ['Set.prototype.delete', this]);
77 return %SetDelete(this, key);
81 function SetGetSizeJS() {
83 throw MakeTypeError('incompatible_method_receiver',
84 ['Set.prototype.size', this]);
86 return %SetGetSize(this);
90 function SetClearJS() {
92 throw MakeTypeError('incompatible_method_receiver',
93 ['Set.prototype.clear', this]);
99 function SetForEach(f, receiver) {
101 throw MakeTypeError('incompatible_method_receiver',
102 ['Set.prototype.forEach', this]);
105 if (!IS_SPEC_FUNCTION(f)) {
106 throw MakeTypeError('called_non_callable', [f]);
109 var iterator = new SetIterator(this, ITERATOR_KIND_VALUES);
111 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
112 var value_array = [UNDEFINED];
113 while (%SetIteratorNext(iterator, value_array)) {
114 if (stepping) %DebugPrepareStepInIfStepping(f);
115 key = value_array[0];
116 %_CallFunction(receiver, key, key, this, f);
121 // -------------------------------------------------------------------
123 function SetUpSet() {
124 %CheckIsBootstrapping();
126 %SetCode($Set, SetConstructor);
127 %FunctionSetPrototype($Set, new $Object());
128 %AddNamedProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
130 %FunctionSetLength(SetForEach, 1);
132 // Set up the non-enumerable functions on the Set prototype object.
133 InstallGetter($Set.prototype, "size", SetGetSizeJS);
134 InstallFunctions($Set.prototype, DONT_ENUM, $Array(
137 "delete", SetDeleteJS,
139 "forEach", SetForEach
146 // -------------------------------------------------------------------
149 function MapConstructor(iterable) {
150 if (!%_IsConstructCall()) {
151 throw MakeTypeError('constructor_not_function', ['Map']);
156 if (!IS_NULL_OR_UNDEFINED(iterable)) {
157 iter = GetIterator(ToObject(iterable));
159 if (!IS_SPEC_FUNCTION(adder)) {
160 throw MakeTypeError('property_not_function', ['set', this]);
164 %MapInitialize(this);
166 if (IS_UNDEFINED(iter)) return;
168 var next, done, nextItem;
169 while (!(next = iter.next()).done) {
170 if (!IS_SPEC_OBJECT(next)) {
171 throw MakeTypeError('iterator_result_not_an_object', [next]);
173 nextItem = next.value;
174 if (!IS_SPEC_OBJECT(nextItem)) {
175 throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
177 %_CallFunction(this, nextItem[0], nextItem[1], adder);
182 function MapGetJS(key) {
184 throw MakeTypeError('incompatible_method_receiver',
185 ['Map.prototype.get', this]);
187 return %MapGet(this, key);
191 function MapSetJS(key, value) {
193 throw MakeTypeError('incompatible_method_receiver',
194 ['Map.prototype.set', this]);
196 // Normalize -0 to +0 as required by the spec.
197 // Even though we use SameValueZero as the comparison for the keys we don't
198 // want to ever store -0 as the key since the key is directly exposed when
203 return %MapSet(this, key, value);
207 function MapHasJS(key) {
209 throw MakeTypeError('incompatible_method_receiver',
210 ['Map.prototype.has', this]);
212 return %MapHas(this, key);
216 function MapDeleteJS(key) {
218 throw MakeTypeError('incompatible_method_receiver',
219 ['Map.prototype.delete', this]);
221 return %MapDelete(this, key);
225 function MapGetSizeJS() {
227 throw MakeTypeError('incompatible_method_receiver',
228 ['Map.prototype.size', this]);
230 return %MapGetSize(this);
234 function MapClearJS() {
236 throw MakeTypeError('incompatible_method_receiver',
237 ['Map.prototype.clear', this]);
243 function MapForEach(f, receiver) {
245 throw MakeTypeError('incompatible_method_receiver',
246 ['Map.prototype.forEach', this]);
249 if (!IS_SPEC_FUNCTION(f)) {
250 throw MakeTypeError('called_non_callable', [f]);
253 var iterator = new MapIterator(this, ITERATOR_KIND_ENTRIES);
254 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(f);
255 var value_array = [UNDEFINED, UNDEFINED];
256 while (%MapIteratorNext(iterator, value_array)) {
257 if (stepping) %DebugPrepareStepInIfStepping(f);
258 %_CallFunction(receiver, value_array[1], value_array[0], this, f);
263 // -------------------------------------------------------------------
265 function SetUpMap() {
266 %CheckIsBootstrapping();
268 %SetCode($Map, MapConstructor);
269 %FunctionSetPrototype($Map, new $Object());
270 %AddNamedProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
272 %FunctionSetLength(MapForEach, 1);
274 // Set up the non-enumerable functions on the Map prototype object.
275 InstallGetter($Map.prototype, "size", MapGetSizeJS);
276 InstallFunctions($Map.prototype, DONT_ENUM, $Array(
280 "delete", MapDeleteJS,
282 "forEach", MapForEach