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 | |
| 91 | function MapConstructor() { |
| 92 | if (%_IsConstructCall()) { |
| 93 | %MapInitialize(this); |
| 94 | } else { |
| 95 | return new $Map(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | function MapGet(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 101 | if (!IS_MAP(this)) { |
| 102 | throw MakeTypeError('incompatible_method_receiver', |
| 103 | ['Map.prototype.get', this]); |
| 104 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 105 | if (IS_UNDEFINED(key)) { |
| 106 | key = undefined_sentinel; |
| 107 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 108 | return %MapGet(this, key); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | function MapSet(key, value) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 113 | if (!IS_MAP(this)) { |
| 114 | throw MakeTypeError('incompatible_method_receiver', |
| 115 | ['Map.prototype.set', this]); |
| 116 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 117 | if (IS_UNDEFINED(key)) { |
| 118 | key = undefined_sentinel; |
| 119 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 120 | return %MapSet(this, key, value); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | function MapHas(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 125 | if (!IS_MAP(this)) { |
| 126 | throw MakeTypeError('incompatible_method_receiver', |
| 127 | ['Map.prototype.has', this]); |
| 128 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 129 | if (IS_UNDEFINED(key)) { |
| 130 | key = undefined_sentinel; |
| 131 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 132 | return %MapHas(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | function MapDelete(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 137 | if (!IS_MAP(this)) { |
| 138 | throw MakeTypeError('incompatible_method_receiver', |
| 139 | ['Map.prototype.delete', this]); |
| 140 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 141 | if (IS_UNDEFINED(key)) { |
| 142 | key = undefined_sentinel; |
| 143 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 144 | return %MapDelete(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 145 | } |
| 146 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 147 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 148 | function WeakMapConstructor() { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 149 | if (%_IsConstructCall()) { |
| 150 | %WeakMapInitialize(this); |
| 151 | } else { |
| 152 | return new $WeakMap(); |
| 153 | } |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 154 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | function WeakMapGet(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 158 | if (!IS_WEAKMAP(this)) { |
| 159 | throw MakeTypeError('incompatible_method_receiver', |
| 160 | ['WeakMap.prototype.get', this]); |
| 161 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 162 | if (!IS_SPEC_OBJECT(key)) { |
| 163 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 164 | } |
| 165 | return %WeakMapGet(this, key); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | function WeakMapSet(key, value) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 170 | if (!IS_WEAKMAP(this)) { |
| 171 | throw MakeTypeError('incompatible_method_receiver', |
| 172 | ['WeakMap.prototype.set', this]); |
| 173 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 174 | if (!IS_SPEC_OBJECT(key)) { |
| 175 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 176 | } |
| 177 | return %WeakMapSet(this, key, value); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | function WeakMapHas(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 182 | if (!IS_WEAKMAP(this)) { |
| 183 | throw MakeTypeError('incompatible_method_receiver', |
| 184 | ['WeakMap.prototype.has', this]); |
| 185 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 186 | if (!IS_SPEC_OBJECT(key)) { |
| 187 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 188 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 189 | return %WeakMapHas(this, key); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
| 193 | function WeakMapDelete(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 194 | if (!IS_WEAKMAP(this)) { |
| 195 | throw MakeTypeError('incompatible_method_receiver', |
| 196 | ['WeakMap.prototype.delete', this]); |
| 197 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 198 | if (!IS_SPEC_OBJECT(key)) { |
| 199 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 200 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 201 | return %WeakMapDelete(this, key); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // ------------------------------------------------------------------- |
| 205 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 206 | (function () { |
| 207 | %CheckIsBootstrapping(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 208 | |
| 209 | // Set up the Set and Map constructor function. |
| 210 | %SetCode($Set, SetConstructor); |
| 211 | %SetCode($Map, MapConstructor); |
| 212 | |
| 213 | // Set up the constructor property on the Set and Map prototype object. |
| 214 | %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM); |
| 215 | %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM); |
| 216 | |
| 217 | // Set up the non-enumerable functions on the Set prototype object. |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 218 | InstallFunctions($Set.prototype, DONT_ENUM, $Array( |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 219 | "add", SetAdd, |
| 220 | "has", SetHas, |
| 221 | "delete", SetDelete |
| 222 | )); |
| 223 | |
| 224 | // Set up the non-enumerable functions on the Map prototype object. |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 225 | InstallFunctions($Map.prototype, DONT_ENUM, $Array( |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 226 | "get", MapGet, |
| 227 | "set", MapSet, |
| 228 | "has", MapHas, |
| 229 | "delete", MapDelete |
| 230 | )); |
| 231 | |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 232 | // Set up the WeakMap constructor function. |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 233 | %SetCode($WeakMap, WeakMapConstructor); |
| 234 | |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 235 | // Set up the constructor property on the WeakMap prototype object. |
| 236 | %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM); |
| 237 | |
| 238 | // Set up the non-enumerable functions on the WeakMap prototype object. |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 239 | InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array( |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 240 | "get", WeakMapGet, |
| 241 | "set", WeakMapSet, |
| 242 | "has", WeakMapHas, |
| 243 | "delete", WeakMapDelete |
| 244 | )); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 245 | })(); |