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 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 34 | function ProxyCreate(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 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 44 | function ProxyCreateFunction(handler, callTrap, constructTrap) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 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 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 65 | %CheckIsBootstrapping() |
| 66 | InstallFunctions($Proxy, DONT_ENUM, [ |
| 67 | "create", ProxyCreate, |
| 68 | "createFunction", ProxyCreateFunction |
| 69 | ]) |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | //////////////////////////////////////////////////////////////////////////////// |
| 73 | // Builtins |
| 74 | //////////////////////////////////////////////////////////////////////////////// |
| 75 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 76 | function DerivedConstructTrap(callTrap) { |
| 77 | return function() { |
| 78 | var proto = this.prototype |
| 79 | if (!IS_SPEC_OBJECT(proto)) proto = $Object.prototype |
| 80 | var obj = new $Object() |
| 81 | obj.__proto__ = proto |
| 82 | var result = %Apply(callTrap, obj, arguments, 0, %_ArgumentsLength()); |
| 83 | return IS_SPEC_OBJECT(result) ? result : obj |
| 84 | } |
| 85 | } |
| 86 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 87 | function DelegateCallAndConstruct(callTrap, constructTrap) { |
| 88 | return function() { |
| 89 | return %Apply(%_IsConstructCall() ? constructTrap : callTrap, |
| 90 | this, arguments, 0, %_ArgumentsLength()) |
| 91 | } |
| 92 | } |
| 93 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 94 | function DerivedGetTrap(receiver, name) { |
| 95 | var desc = this.getPropertyDescriptor(name) |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 96 | if (IS_UNDEFINED(desc)) { return desc } |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 97 | if ('value' in desc) { |
| 98 | return desc.value |
| 99 | } else { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 100 | if (IS_UNDEFINED(desc.get)) { return desc.get } |
| 101 | // The proposal says: desc.get.call(receiver) |
| 102 | return %_CallFunction(receiver, desc.get) |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 105 | |
| 106 | function DerivedSetTrap(receiver, name, val) { |
| 107 | var desc = this.getOwnPropertyDescriptor(name) |
| 108 | if (desc) { |
| 109 | if ('writable' in desc) { |
| 110 | if (desc.writable) { |
| 111 | desc.value = val |
| 112 | this.defineProperty(name, desc) |
| 113 | return true |
| 114 | } else { |
| 115 | return false |
| 116 | } |
| 117 | } else { // accessor |
| 118 | if (desc.set) { |
| 119 | // The proposal says: desc.set.call(receiver, val) |
| 120 | %_CallFunction(receiver, val, desc.set) |
| 121 | return true |
| 122 | } else { |
| 123 | return false |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | desc = this.getPropertyDescriptor(name) |
| 128 | if (desc) { |
| 129 | if ('writable' in desc) { |
| 130 | if (desc.writable) { |
| 131 | // fall through |
| 132 | } else { |
| 133 | return false |
| 134 | } |
| 135 | } else { // accessor |
| 136 | if (desc.set) { |
| 137 | // The proposal says: desc.set.call(receiver, val) |
| 138 | %_CallFunction(receiver, val, desc.set) |
| 139 | return true |
| 140 | } else { |
| 141 | return false |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | this.defineProperty(name, { |
| 146 | value: val, |
| 147 | writable: true, |
| 148 | enumerable: true, |
| 149 | configurable: true}); |
| 150 | return true; |
| 151 | } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 152 | |
| 153 | function DerivedHasTrap(name) { |
| 154 | return !!this.getPropertyDescriptor(name) |
| 155 | } |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 156 | |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 157 | function DerivedHasOwnTrap(name) { |
| 158 | return !!this.getOwnPropertyDescriptor(name) |
| 159 | } |
| 160 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 161 | function DerivedKeysTrap() { |
| 162 | var names = this.getOwnPropertyNames() |
| 163 | var enumerableNames = [] |
| 164 | for (var i = 0, count = 0; i < names.length; ++i) { |
| 165 | var name = names[i] |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 166 | var desc = this.getOwnPropertyDescriptor(TO_STRING_INLINE(name)) |
| 167 | if (!IS_UNDEFINED(desc) && desc.enumerable) { |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 168 | enumerableNames[count++] = names[i] |
| 169 | } |
| 170 | } |
| 171 | return enumerableNames |
| 172 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 173 | |
| 174 | function DerivedEnumerateTrap() { |
| 175 | var names = this.getPropertyNames() |
| 176 | var enumerableNames = [] |
| 177 | for (var i = 0, count = 0; i < names.length; ++i) { |
| 178 | var name = names[i] |
| 179 | var desc = this.getPropertyDescriptor(TO_STRING_INLINE(name)) |
| 180 | if (!IS_UNDEFINED(desc) && desc.enumerable) { |
| 181 | enumerableNames[count++] = names[i] |
| 182 | } |
| 183 | } |
| 184 | return enumerableNames |
| 185 | } |
| 186 | |
| 187 | function ProxyEnumerate(proxy) { |
| 188 | var handler = %GetHandler(proxy) |
| 189 | if (IS_UNDEFINED(handler.enumerate)) { |
| 190 | return %Apply(DerivedEnumerateTrap, handler, [], 0, 0) |
| 191 | } else { |
| 192 | return ToStringArray(handler.enumerate(), "enumerate") |
| 193 | } |
| 194 | } |