danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +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 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 28 | "use strict"; |
| 29 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 30 | global.Proxy = new $Object(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 31 | |
| 32 | var $Proxy = global.Proxy |
| 33 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 34 | $Proxy.create = function(handler, proto) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 35 | if (!IS_SPEC_OBJECT(handler)) |
| 36 | throw MakeTypeError("handler_non_object", ["create"]) |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 37 | if (IS_UNDEFINED(proto)) |
| 38 | proto = null |
| 39 | else if (!(IS_SPEC_OBJECT(proto) || proto === null)) |
| 40 | throw MakeTypeError("proto_non_object", ["create"]) |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 41 | return %CreateJSProxy(handler, proto) |
| 42 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 43 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 44 | $Proxy.createFunction = function(handler, callTrap, constructTrap) { |
| 45 | if (!IS_SPEC_OBJECT(handler)) |
| 46 | throw MakeTypeError("handler_non_object", ["create"]) |
| 47 | if (!IS_SPEC_FUNCTION(callTrap)) |
| 48 | throw MakeTypeError("trap_function_expected", ["createFunction", "call"]) |
| 49 | if (IS_UNDEFINED(constructTrap)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 50 | constructTrap = DerivedConstructTrap(callTrap) |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 51 | } else if (IS_SPEC_FUNCTION(constructTrap)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 52 | // Make sure the trap receives 'undefined' as this. |
| 53 | var construct = constructTrap |
| 54 | constructTrap = function() { |
| 55 | return %Apply(construct, void 0, arguments, 0, %_ArgumentsLength()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 56 | } |
| 57 | } else { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 58 | throw MakeTypeError("trap_function_expected", |
| 59 | ["createFunction", "construct"]) |
| 60 | } |
| 61 | return %CreateJSFunctionProxy( |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 62 | handler, callTrap, constructTrap, $Function.prototype) |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 63 | } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | |
| 67 | //////////////////////////////////////////////////////////////////////////////// |
| 68 | // Builtins |
| 69 | //////////////////////////////////////////////////////////////////////////////// |
| 70 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 71 | function DerivedConstructTrap(callTrap) { |
| 72 | return function() { |
| 73 | var proto = this.prototype |
| 74 | if (!IS_SPEC_OBJECT(proto)) proto = $Object.prototype |
| 75 | var obj = new $Object() |
| 76 | obj.__proto__ = proto |
| 77 | var result = %Apply(callTrap, obj, arguments, 0, %_ArgumentsLength()); |
| 78 | return IS_SPEC_OBJECT(result) ? result : obj |
| 79 | } |
| 80 | } |
| 81 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 82 | function DelegateCallAndConstruct(callTrap, constructTrap) { |
| 83 | return function() { |
| 84 | return %Apply(%_IsConstructCall() ? constructTrap : callTrap, |
| 85 | this, arguments, 0, %_ArgumentsLength()) |
| 86 | } |
| 87 | } |
| 88 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 89 | function DerivedGetTrap(receiver, name) { |
| 90 | var desc = this.getPropertyDescriptor(name) |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 91 | if (IS_UNDEFINED(desc)) { return desc } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 92 | if ('value' in desc) { |
| 93 | return desc.value |
| 94 | } else { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 95 | if (IS_UNDEFINED(desc.get)) { return desc.get } |
| 96 | // The proposal says: desc.get.call(receiver) |
| 97 | return %_CallFunction(receiver, desc.get) |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 100 | |
| 101 | function DerivedSetTrap(receiver, name, val) { |
| 102 | var desc = this.getOwnPropertyDescriptor(name) |
| 103 | if (desc) { |
| 104 | if ('writable' in desc) { |
| 105 | if (desc.writable) { |
| 106 | desc.value = val |
| 107 | this.defineProperty(name, desc) |
| 108 | return true |
| 109 | } else { |
| 110 | return false |
| 111 | } |
| 112 | } else { // accessor |
| 113 | if (desc.set) { |
| 114 | // The proposal says: desc.set.call(receiver, val) |
| 115 | %_CallFunction(receiver, val, desc.set) |
| 116 | return true |
| 117 | } else { |
| 118 | return false |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | desc = this.getPropertyDescriptor(name) |
| 123 | if (desc) { |
| 124 | if ('writable' in desc) { |
| 125 | if (desc.writable) { |
| 126 | // fall through |
| 127 | } else { |
| 128 | return false |
| 129 | } |
| 130 | } else { // accessor |
| 131 | if (desc.set) { |
| 132 | // The proposal says: desc.set.call(receiver, val) |
| 133 | %_CallFunction(receiver, val, desc.set) |
| 134 | return true |
| 135 | } else { |
| 136 | return false |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | this.defineProperty(name, { |
| 141 | value: val, |
| 142 | writable: true, |
| 143 | enumerable: true, |
| 144 | configurable: true}); |
| 145 | return true; |
| 146 | } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 147 | |
| 148 | function DerivedHasTrap(name) { |
| 149 | return !!this.getPropertyDescriptor(name) |
| 150 | } |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 151 | |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 152 | function DerivedHasOwnTrap(name) { |
| 153 | return !!this.getOwnPropertyDescriptor(name) |
| 154 | } |
| 155 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 156 | function DerivedKeysTrap() { |
| 157 | var names = this.getOwnPropertyNames() |
| 158 | var enumerableNames = [] |
| 159 | for (var i = 0, count = 0; i < names.length; ++i) { |
| 160 | var name = names[i] |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 161 | var desc = this.getOwnPropertyDescriptor(TO_STRING_INLINE(name)) |
| 162 | if (!IS_UNDEFINED(desc) && desc.enumerable) { |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 163 | enumerableNames[count++] = names[i] |
| 164 | } |
| 165 | } |
| 166 | return enumerableNames |
| 167 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 168 | |
| 169 | function DerivedEnumerateTrap() { |
| 170 | var names = this.getPropertyNames() |
| 171 | var enumerableNames = [] |
| 172 | for (var i = 0, count = 0; i < names.length; ++i) { |
| 173 | var name = names[i] |
| 174 | var desc = this.getPropertyDescriptor(TO_STRING_INLINE(name)) |
| 175 | if (!IS_UNDEFINED(desc) && desc.enumerable) { |
| 176 | enumerableNames[count++] = names[i] |
| 177 | } |
| 178 | } |
| 179 | return enumerableNames |
| 180 | } |
| 181 | |
| 182 | function ProxyEnumerate(proxy) { |
| 183 | var handler = %GetHandler(proxy) |
| 184 | if (IS_UNDEFINED(handler.enumerate)) { |
| 185 | return %Apply(DerivedEnumerateTrap, handler, [], 0, 0) |
| 186 | } else { |
| 187 | return ToStringArray(handler.enumerate(), "enumerate") |
| 188 | } |
| 189 | } |