jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 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 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 28 | "use strict"; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 29 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 30 | var $Set = global.Set; |
| 31 | var $Map = global.Map; |
| 32 | var $WeakMap = global.WeakMap; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 33 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 34 | //------------------------------------------------------------------- |
| 35 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 36 | // Global sentinel to be used instead of undefined keys, which are not |
| 37 | // supported internally but required for Harmony sets and maps. |
| 38 | var undefined_sentinel = {}; |
| 39 | |
| 40 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 41 | function SetConstructor() { |
| 42 | if (%_IsConstructCall()) { |
| 43 | %SetInitialize(this); |
| 44 | } else { |
| 45 | return new $Set(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | function SetAdd(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 51 | if (!IS_SET(this)) { |
| 52 | throw MakeTypeError('incompatible_method_receiver', |
| 53 | ['Set.prototype.add', this]); |
| 54 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 55 | if (IS_UNDEFINED(key)) { |
| 56 | key = undefined_sentinel; |
| 57 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 58 | return %SetAdd(this, key); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | function SetHas(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 63 | if (!IS_SET(this)) { |
| 64 | throw MakeTypeError('incompatible_method_receiver', |
| 65 | ['Set.prototype.has', this]); |
| 66 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 67 | if (IS_UNDEFINED(key)) { |
| 68 | key = undefined_sentinel; |
| 69 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 70 | return %SetHas(this, key); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | function SetDelete(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 75 | if (!IS_SET(this)) { |
| 76 | throw MakeTypeError('incompatible_method_receiver', |
| 77 | ['Set.prototype.delete', this]); |
| 78 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 79 | if (IS_UNDEFINED(key)) { |
| 80 | key = undefined_sentinel; |
| 81 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 82 | if (%SetHas(this, key)) { |
| 83 | %SetDelete(this, key); |
| 84 | return true; |
| 85 | } else { |
| 86 | return false; |
| 87 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 91 | function SetGetSize() { |
| 92 | if (!IS_SET(this)) { |
| 93 | throw MakeTypeError('incompatible_method_receiver', |
| 94 | ['Set.prototype.size', this]); |
| 95 | } |
| 96 | return %SetGetSize(this); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | function SetClear() { |
| 101 | if (!IS_SET(this)) { |
| 102 | throw MakeTypeError('incompatible_method_receiver', |
| 103 | ['Set.prototype.clear', this]); |
| 104 | } |
| 105 | // Replace the internal table with a new empty table. |
| 106 | %SetInitialize(this); |
| 107 | } |
| 108 | |
| 109 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 110 | function MapConstructor() { |
| 111 | if (%_IsConstructCall()) { |
| 112 | %MapInitialize(this); |
| 113 | } else { |
| 114 | return new $Map(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | function MapGet(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 120 | if (!IS_MAP(this)) { |
| 121 | throw MakeTypeError('incompatible_method_receiver', |
| 122 | ['Map.prototype.get', this]); |
| 123 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 124 | if (IS_UNDEFINED(key)) { |
| 125 | key = undefined_sentinel; |
| 126 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 127 | return %MapGet(this, key); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | function MapSet(key, value) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 132 | if (!IS_MAP(this)) { |
| 133 | throw MakeTypeError('incompatible_method_receiver', |
| 134 | ['Map.prototype.set', this]); |
| 135 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 136 | if (IS_UNDEFINED(key)) { |
| 137 | key = undefined_sentinel; |
| 138 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 139 | return %MapSet(this, key, value); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | function MapHas(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 144 | if (!IS_MAP(this)) { |
| 145 | throw MakeTypeError('incompatible_method_receiver', |
| 146 | ['Map.prototype.has', this]); |
| 147 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 148 | if (IS_UNDEFINED(key)) { |
| 149 | key = undefined_sentinel; |
| 150 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 151 | return %MapHas(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
| 155 | function MapDelete(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 156 | if (!IS_MAP(this)) { |
| 157 | throw MakeTypeError('incompatible_method_receiver', |
| 158 | ['Map.prototype.delete', this]); |
| 159 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 160 | if (IS_UNDEFINED(key)) { |
| 161 | key = undefined_sentinel; |
| 162 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 163 | return %MapDelete(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 166 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 167 | function MapGetSize() { |
| 168 | if (!IS_MAP(this)) { |
| 169 | throw MakeTypeError('incompatible_method_receiver', |
| 170 | ['Map.prototype.size', this]); |
| 171 | } |
| 172 | return %MapGetSize(this); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | function MapClear() { |
| 177 | if (!IS_MAP(this)) { |
| 178 | throw MakeTypeError('incompatible_method_receiver', |
| 179 | ['Map.prototype.clear', this]); |
| 180 | } |
| 181 | // Replace the internal table with a new empty table. |
| 182 | %MapInitialize(this); |
| 183 | } |
| 184 | |
| 185 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 186 | function WeakMapConstructor() { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 187 | if (%_IsConstructCall()) { |
| 188 | %WeakMapInitialize(this); |
| 189 | } else { |
| 190 | return new $WeakMap(); |
| 191 | } |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 192 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | function WeakMapGet(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 196 | if (!IS_WEAKMAP(this)) { |
| 197 | throw MakeTypeError('incompatible_method_receiver', |
| 198 | ['WeakMap.prototype.get', this]); |
| 199 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 200 | if (!IS_SPEC_OBJECT(key)) { |
| 201 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 202 | } |
| 203 | return %WeakMapGet(this, key); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | function WeakMapSet(key, value) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 208 | if (!IS_WEAKMAP(this)) { |
| 209 | throw MakeTypeError('incompatible_method_receiver', |
| 210 | ['WeakMap.prototype.set', this]); |
| 211 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 212 | if (!IS_SPEC_OBJECT(key)) { |
| 213 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 214 | } |
| 215 | return %WeakMapSet(this, key, value); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | function WeakMapHas(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 220 | if (!IS_WEAKMAP(this)) { |
| 221 | throw MakeTypeError('incompatible_method_receiver', |
| 222 | ['WeakMap.prototype.has', this]); |
| 223 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 224 | if (!IS_SPEC_OBJECT(key)) { |
| 225 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 226 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 227 | return %WeakMapHas(this, key); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | function WeakMapDelete(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 232 | if (!IS_WEAKMAP(this)) { |
| 233 | throw MakeTypeError('incompatible_method_receiver', |
| 234 | ['WeakMap.prototype.delete', this]); |
| 235 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 236 | if (!IS_SPEC_OBJECT(key)) { |
| 237 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 238 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 239 | return %WeakMapDelete(this, key); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // ------------------------------------------------------------------- |
| 243 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 244 | (function () { |
| 245 | %CheckIsBootstrapping(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 246 | |
| 247 | // Set up the Set and Map constructor function. |
| 248 | %SetCode($Set, SetConstructor); |
| 249 | %SetCode($Map, MapConstructor); |
| 250 | |
| 251 | // Set up the constructor property on the Set and Map prototype object. |
| 252 | %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM); |
| 253 | %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM); |
| 254 | |
| 255 | // Set up the non-enumerable functions on the Set prototype object. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 256 | InstallGetter($Set.prototype, "size", SetGetSize); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 257 | InstallFunctions($Set.prototype, DONT_ENUM, $Array( |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 258 | "add", SetAdd, |
| 259 | "has", SetHas, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 260 | "delete", SetDelete, |
| 261 | "clear", SetClear |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 262 | )); |
| 263 | |
| 264 | // Set up the non-enumerable functions on the Map prototype object. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 265 | InstallGetter($Map.prototype, "size", MapGetSize); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 266 | InstallFunctions($Map.prototype, DONT_ENUM, $Array( |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 267 | "get", MapGet, |
| 268 | "set", MapSet, |
| 269 | "has", MapHas, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 270 | "delete", MapDelete, |
| 271 | "clear", MapClear |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 272 | )); |
| 273 | |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 274 | // Set up the WeakMap constructor function. |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 275 | %SetCode($WeakMap, WeakMapConstructor); |
| 276 | |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 277 | // Set up the constructor property on the WeakMap prototype object. |
| 278 | %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM); |
| 279 | |
| 280 | // Set up the non-enumerable functions on the WeakMap prototype object. |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 281 | InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array( |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 282 | "get", WeakMapGet, |
| 283 | "set", WeakMapSet, |
| 284 | "has", WeakMapHas, |
| 285 | "delete", WeakMapDelete |
| 286 | )); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 287 | })(); |