kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame^] | 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 | |
| 29 | // This file relies on the fact that the following declaration has been made |
| 30 | // in runtime.js: |
| 31 | // const $Object = global.Object; |
| 32 | const $WeakMap = global.WeakMap; |
| 33 | |
| 34 | // ------------------------------------------------------------------- |
| 35 | |
| 36 | // Set the WeakMap function and constructor. |
| 37 | %SetCode($WeakMap, function(x) { |
| 38 | if (%_IsConstructCall()) { |
| 39 | %WeakMapInitialize(this); |
| 40 | } else { |
| 41 | return new $WeakMap(); |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | |
| 46 | function WeakMapGet(key) { |
| 47 | if (!IS_SPEC_OBJECT(key)) { |
| 48 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 49 | } |
| 50 | return %WeakMapGet(this, key); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | function WeakMapSet(key, value) { |
| 55 | if (!IS_SPEC_OBJECT(key)) { |
| 56 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 57 | } |
| 58 | return %WeakMapSet(this, key, value); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | function WeakMapHas(key) { |
| 63 | if (!IS_SPEC_OBJECT(key)) { |
| 64 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 65 | } |
| 66 | return !IS_UNDEFINED(%WeakMapGet(this, key)); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | function WeakMapDelete(key) { |
| 71 | if (!IS_SPEC_OBJECT(key)) { |
| 72 | throw %MakeTypeError('invalid_weakmap_key', [this, key]); |
| 73 | } |
| 74 | if (!IS_UNDEFINED(%WeakMapGet(this, key))) { |
| 75 | %WeakMapSet(this, key, void 0); |
| 76 | return true; |
| 77 | } else { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // ------------------------------------------------------------------- |
| 83 | |
| 84 | function SetupWeakMap() { |
| 85 | // Setup the non-enumerable functions on the WeakMap prototype object. |
| 86 | InstallFunctionsOnHiddenPrototype($WeakMap.prototype, DONT_ENUM, $Array( |
| 87 | "get", WeakMapGet, |
| 88 | "set", WeakMapSet, |
| 89 | "has", WeakMapHas, |
| 90 | "delete", WeakMapDelete |
| 91 | )); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | SetupWeakMap(); |