collection.js revision 592a9fc1d8ea420377a2e7efd0600e20b058be2b
1// Copyright 2011 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
4// met:
5//
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.
15//
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.
27
28
29const $Set = global.Set;
30const $Map = global.Map;
31const $WeakMap = global.WeakMap;
32
33//-------------------------------------------------------------------
34
35// Global sentinel to be used instead of undefined keys, which are not
36// supported internally but required for Harmony sets and maps.
37var undefined_sentinel = {};
38
39
40function SetConstructor() {
41  if (%_IsConstructCall()) {
42    %SetInitialize(this);
43  } else {
44    return new $Set();
45  }
46}
47
48
49function SetAdd(key) {
50  if (IS_UNDEFINED(key)) {
51    key = undefined_sentinel;
52  }
53  return %SetAdd(this, key);
54}
55
56
57function SetHas(key) {
58  if (IS_UNDEFINED(key)) {
59    key = undefined_sentinel;
60  }
61  return %SetHas(this, key);
62}
63
64
65function SetDelete(key) {
66  if (IS_UNDEFINED(key)) {
67    key = undefined_sentinel;
68  }
69  return %SetDelete(this, key);
70}
71
72
73function MapConstructor() {
74  if (%_IsConstructCall()) {
75    %MapInitialize(this);
76  } else {
77    return new $Map();
78  }
79}
80
81
82function MapGet(key) {
83  if (IS_UNDEFINED(key)) {
84    key = undefined_sentinel;
85  }
86  return %MapGet(this, key);
87}
88
89
90function MapSet(key, value) {
91  if (IS_UNDEFINED(key)) {
92    key = undefined_sentinel;
93  }
94  return %MapSet(this, key, value);
95}
96
97
98function MapHas(key) {
99  if (IS_UNDEFINED(key)) {
100    key = undefined_sentinel;
101  }
102  return !IS_UNDEFINED(%MapGet(this, key));
103}
104
105
106function MapDelete(key) {
107  if (IS_UNDEFINED(key)) {
108    key = undefined_sentinel;
109  }
110  if (!IS_UNDEFINED(%MapGet(this, key))) {
111    %MapSet(this, key, void 0);
112    return true;
113  } else {
114    return false;
115  }
116}
117
118
119function WeakMapConstructor() {
120  if (%_IsConstructCall()) {
121    %WeakMapInitialize(this);
122  } else {
123    return new $WeakMap();
124  }
125}
126
127
128function WeakMapGet(key) {
129  if (!IS_SPEC_OBJECT(key)) {
130    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
131  }
132  return %WeakMapGet(this, key);
133}
134
135
136function WeakMapSet(key, value) {
137  if (!IS_SPEC_OBJECT(key)) {
138    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
139  }
140  return %WeakMapSet(this, key, value);
141}
142
143
144function WeakMapHas(key) {
145  if (!IS_SPEC_OBJECT(key)) {
146    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
147  }
148  return !IS_UNDEFINED(%WeakMapGet(this, key));
149}
150
151
152function WeakMapDelete(key) {
153  if (!IS_SPEC_OBJECT(key)) {
154    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
155  }
156  if (!IS_UNDEFINED(%WeakMapGet(this, key))) {
157    %WeakMapSet(this, key, void 0);
158    return true;
159  } else {
160    return false;
161  }
162}
163
164// -------------------------------------------------------------------
165
166(function () {
167  %CheckIsBootstrapping();
168
169  // Set up the Set and Map constructor function.
170  %SetCode($Set, SetConstructor);
171  %SetCode($Map, MapConstructor);
172
173  // Set up the constructor property on the Set and Map prototype object.
174  %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
175  %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
176
177  // Set up the non-enumerable functions on the Set prototype object.
178  InstallFunctions($Set.prototype, DONT_ENUM, $Array(
179    "add", SetAdd,
180    "has", SetHas,
181    "delete", SetDelete
182  ));
183
184  // Set up the non-enumerable functions on the Map prototype object.
185  InstallFunctions($Map.prototype, DONT_ENUM, $Array(
186    "get", MapGet,
187    "set", MapSet,
188    "has", MapHas,
189    "delete", MapDelete
190  ));
191
192  // Set up the WeakMap constructor function.
193  %SetCode($WeakMap, WeakMapConstructor);
194
195  // Set up the constructor property on the WeakMap prototype object.
196  %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
197
198  // Set up the non-enumerable functions on the WeakMap prototype object.
199  InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
200    "get", WeakMapGet,
201    "set", WeakMapSet,
202    "has", WeakMapHas,
203    "delete", WeakMapDelete
204  ));
205})();
206