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;
14 // Global sentinel to be used instead of undefined keys, which are not
15 // supported internally but required for Harmony sets and maps.
16 var undefined_sentinel = {};
19 // Map and Set uses SameValueZero which means that +0 and -0 should be treated
21 function NormalizeKey(key) {
22 if (IS_UNDEFINED(key)) {
23 return undefined_sentinel;
34 // -------------------------------------------------------------------
37 function SetConstructor() {
38 if (%_IsConstructCall()) {
41 throw MakeTypeError('constructor_not_function', ['Set']);
46 function SetAdd(key) {
48 throw MakeTypeError('incompatible_method_receiver',
49 ['Set.prototype.add', this]);
51 return %SetAdd(this, NormalizeKey(key));
55 function SetHas(key) {
57 throw MakeTypeError('incompatible_method_receiver',
58 ['Set.prototype.has', this]);
60 return %SetHas(this, NormalizeKey(key));
64 function SetDelete(key) {
66 throw MakeTypeError('incompatible_method_receiver',
67 ['Set.prototype.delete', this]);
69 key = NormalizeKey(key);
70 if (%SetHas(this, key)) {
71 %SetDelete(this, key);
79 function SetGetSize() {
81 throw MakeTypeError('incompatible_method_receiver',
82 ['Set.prototype.size', this]);
84 return %SetGetSize(this);
90 throw MakeTypeError('incompatible_method_receiver',
91 ['Set.prototype.clear', this]);
97 function SetForEach(f, receiver) {
99 throw MakeTypeError('incompatible_method_receiver',
100 ['Set.prototype.forEach', this]);
103 if (!IS_SPEC_FUNCTION(f)) {
104 throw MakeTypeError('called_non_callable', [f]);
107 var iterator = %SetCreateIterator(this, ITERATOR_KIND_VALUES);
110 while (!(entry = %SetIteratorNext(iterator)).done) {
111 %_CallFunction(receiver, entry.value, entry.value, this, f);
114 %SetIteratorClose(iterator);
119 // -------------------------------------------------------------------
121 function SetUpSet() {
122 %CheckIsBootstrapping();
124 %SetCode($Set, SetConstructor);
125 %FunctionSetPrototype($Set, new $Object());
126 %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
128 %FunctionSetLength(SetForEach, 1);
130 // Set up the non-enumerable functions on the Set prototype object.
131 InstallGetter($Set.prototype, "size", SetGetSize);
132 InstallFunctions($Set.prototype, DONT_ENUM, $Array(
137 "forEach", SetForEach
144 // -------------------------------------------------------------------
147 function MapConstructor() {
148 if (%_IsConstructCall()) {
149 %MapInitialize(this);
151 throw MakeTypeError('constructor_not_function', ['Map']);
156 function MapGet(key) {
158 throw MakeTypeError('incompatible_method_receiver',
159 ['Map.prototype.get', this]);
161 return %MapGet(this, NormalizeKey(key));
165 function MapSet(key, value) {
167 throw MakeTypeError('incompatible_method_receiver',
168 ['Map.prototype.set', this]);
170 return %MapSet(this, NormalizeKey(key), value);
174 function MapHas(key) {
176 throw MakeTypeError('incompatible_method_receiver',
177 ['Map.prototype.has', this]);
179 return %MapHas(this, NormalizeKey(key));
183 function MapDelete(key) {
185 throw MakeTypeError('incompatible_method_receiver',
186 ['Map.prototype.delete', this]);
188 return %MapDelete(this, NormalizeKey(key));
192 function MapGetSize() {
194 throw MakeTypeError('incompatible_method_receiver',
195 ['Map.prototype.size', this]);
197 return %MapGetSize(this);
201 function MapClear() {
203 throw MakeTypeError('incompatible_method_receiver',
204 ['Map.prototype.clear', this]);
210 function MapForEach(f, receiver) {
212 throw MakeTypeError('incompatible_method_receiver',
213 ['Map.prototype.forEach', this]);
216 if (!IS_SPEC_FUNCTION(f)) {
217 throw MakeTypeError('called_non_callable', [f]);
220 var iterator = %MapCreateIterator(this, ITERATOR_KIND_ENTRIES);
223 while (!(entry = %MapIteratorNext(iterator)).done) {
224 %_CallFunction(receiver, entry.value[1], entry.value[0], this, f);
227 %MapIteratorClose(iterator);
232 // -------------------------------------------------------------------
234 function SetUpMap() {
235 %CheckIsBootstrapping();
237 %SetCode($Map, MapConstructor);
238 %FunctionSetPrototype($Map, new $Object());
239 %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
241 %FunctionSetLength(MapForEach, 1);
243 // Set up the non-enumerable functions on the Map prototype object.
244 InstallGetter($Map.prototype, "size", MapGetSize);
245 InstallFunctions($Map.prototype, DONT_ENUM, $Array(
251 "forEach", MapForEach