1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // This file relies on the fact that the following declaration has been made
32 // var $Array = global.Array;
34 var $Set = global.Set;
35 var $Map = global.Map;
37 // Global sentinel to be used instead of undefined keys, which are not
38 // supported internally but required for Harmony sets and maps.
39 var undefined_sentinel = {};
42 // Map and Set uses SameValueZero which means that +0 and -0 should be treated
44 function NormalizeKey(key) {
45 if (IS_UNDEFINED(key)) {
46 return undefined_sentinel;
57 // -------------------------------------------------------------------
60 function SetConstructor() {
61 if (%_IsConstructCall()) {
64 throw MakeTypeError('constructor_not_function', ['Set']);
69 function SetAdd(key) {
71 throw MakeTypeError('incompatible_method_receiver',
72 ['Set.prototype.add', this]);
74 return %SetAdd(this, NormalizeKey(key));
78 function SetHas(key) {
80 throw MakeTypeError('incompatible_method_receiver',
81 ['Set.prototype.has', this]);
83 return %SetHas(this, NormalizeKey(key));
87 function SetDelete(key) {
89 throw MakeTypeError('incompatible_method_receiver',
90 ['Set.prototype.delete', this]);
92 key = NormalizeKey(key);
93 if (%SetHas(this, key)) {
94 %SetDelete(this, key);
102 function SetGetSize() {
104 throw MakeTypeError('incompatible_method_receiver',
105 ['Set.prototype.size', this]);
107 return %SetGetSize(this);
111 function SetClear() {
113 throw MakeTypeError('incompatible_method_receiver',
114 ['Set.prototype.clear', this]);
116 // Replace the internal table with a new empty table.
117 %SetInitialize(this);
121 // -------------------------------------------------------------------
123 function SetUpSet() {
124 %CheckIsBootstrapping();
126 %SetCode($Set, SetConstructor);
127 %FunctionSetPrototype($Set, new $Object());
128 %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
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(
143 // -------------------------------------------------------------------
146 function MapConstructor() {
147 if (%_IsConstructCall()) {
148 %MapInitialize(this);
150 throw MakeTypeError('constructor_not_function', ['Map']);
155 function MapGet(key) {
157 throw MakeTypeError('incompatible_method_receiver',
158 ['Map.prototype.get', this]);
160 return %MapGet(this, NormalizeKey(key));
164 function MapSet(key, value) {
166 throw MakeTypeError('incompatible_method_receiver',
167 ['Map.prototype.set', this]);
169 return %MapSet(this, NormalizeKey(key), value);
173 function MapHas(key) {
175 throw MakeTypeError('incompatible_method_receiver',
176 ['Map.prototype.has', this]);
178 return %MapHas(this, NormalizeKey(key));
182 function MapDelete(key) {
184 throw MakeTypeError('incompatible_method_receiver',
185 ['Map.prototype.delete', this]);
187 return %MapDelete(this, NormalizeKey(key));
191 function MapGetSize() {
193 throw MakeTypeError('incompatible_method_receiver',
194 ['Map.prototype.size', this]);
196 return %MapGetSize(this);
200 function MapClear() {
202 throw MakeTypeError('incompatible_method_receiver',
203 ['Map.prototype.clear', this]);
205 // Replace the internal table with a new empty table.
206 %MapInitialize(this);
210 // -------------------------------------------------------------------
212 function SetUpMap() {
213 %CheckIsBootstrapping();
215 %SetCode($Map, MapConstructor);
216 %FunctionSetPrototype($Map, new $Object());
217 %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
219 // Set up the non-enumerable functions on the Map prototype object.
220 InstallGetter($Map.prototype, "size", MapGetSize);
221 InstallFunctions($Map.prototype, DONT_ENUM, $Array(