OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1983 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1994 | 1994 |
1995 TestIsEnumerableThrow(Proxy.create({ | 1995 TestIsEnumerableThrow(Proxy.create({ |
1996 get: function(pr, pk) { throw "myexn" } | 1996 get: function(pr, pk) { throw "myexn" } |
1997 })) | 1997 })) |
1998 | 1998 |
1999 TestIsEnumerableThrow(Proxy.create({ | 1999 TestIsEnumerableThrow(Proxy.create({ |
2000 get: function(pr, pk) { | 2000 get: function(pr, pk) { |
2001 return function(k) { throw "myexn" } | 2001 return function(k) { throw "myexn" } |
2002 } | 2002 } |
2003 })) | 2003 })) |
2004 | |
2005 | |
2006 | |
2007 // Calling (call, Function.prototype.call, Function.prototype.apply, | |
2008 // Function.prototype.bind). | |
2009 | |
2010 var global_object = this | |
2011 var receiver | |
2012 | |
2013 function CreateFrozen(handler, callTrap, constructTrap) { | |
2014 if (handler.fix === undefined) handler.fix = function() { return {} } | |
2015 var f = Proxy.createFunction(handler, callTrap, constructTrap) | |
2016 Object.freeze(f) | |
2017 return f | |
2018 } | |
2019 | |
2020 function TestCall(isStrict, callTrap) { | |
2021 assertEquals(42, callTrap(5, 37)) | |
2022 // TODO(rossberg): unrelated bug: this does not succeed for optimized code: | |
2023 // assertEquals(isStrict ? undefined : global_object, receiver) | |
2024 | |
2025 var f = Proxy.createFunction({}, callTrap) | |
2026 receiver = 333 | |
2027 assertEquals(42, f(11, 31)) | |
2028 assertEquals(isStrict ? undefined : global_object, receiver) | |
2029 var o = {} | |
2030 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | |
2031 assertEquals(o, receiver) | |
2032 assertEquals(43, Function.prototype.call.call(f, null, 20, 23)) | |
2033 assertEquals(isStrict ? null : global_object, receiver) | |
2034 assertEquals(44, Function.prototype.call.call(f, 2, 21, 23)) | |
2035 assertEquals(2, receiver.valueOf()) | |
2036 receiver = 333 | |
2037 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | |
2038 assertEquals(o, receiver) | |
2039 var ff = Function.prototype.bind.call(f, o, 12) | |
2040 receiver = 333 | |
2041 assertEquals(42, ff(30)) | |
2042 assertEquals(o, receiver) | |
2043 receiver = 333 | |
2044 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) | |
2045 assertEquals(o, receiver) | |
2046 | |
2047 var f = CreateFrozen({}, callTrap) | |
2048 receiver = 333 | |
2049 assertEquals(42, f(11, 31)) | |
2050 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. | |
2051 // assertEquals(isStrict ? undefined : global, receiver) | |
2052 receiver = 333 | |
2053 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | |
2054 assertEquals(o, receiver) | |
2055 receiver = 333 | |
2056 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | |
2057 assertEquals(o, receiver) | |
2058 receiver = 333 | |
2059 assertEquals(42, ff(30)) | |
2060 assertEquals(o, receiver) | |
2061 receiver = 333 | |
2062 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) | |
2063 assertEquals(o, receiver) | |
2064 } | |
2065 | |
2066 TestCall(false, function(x, y) { | |
2067 receiver = this; return x + y | |
2068 }) | |
2069 | |
2070 TestCall(true, function(x, y) { | |
2071 "use strict"; | |
2072 receiver = this; return x + y | |
2073 }) | |
2074 | |
2075 TestCall(false, Proxy.createFunction({}, function(x, y) { | |
2076 receiver = this; return x + y | |
2077 })) | |
2078 | |
2079 TestCall(true, Proxy.createFunction({}, function(x, y) { | |
2080 "use strict"; | |
2081 receiver = this; return x + y | |
2082 })) | |
2083 | |
2084 TestCall(false, CreateFrozen({}, function(x, y) { | |
2085 receiver = this; return x + y | |
2086 })) | |
2087 | |
2088 | |
2089 function TestCallThrow(callTrap) { | |
2090 var f = Proxy.createFunction({}, callTrap) | |
2091 assertThrows(function(){ f(11) }, "myexn") | |
2092 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | |
2093 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | |
2094 | |
2095 var f = CreateFrozen({}, callTrap) | |
2096 assertThrows(function(){ f(11) }, "myexn") | |
2097 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | |
2098 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | |
2099 } | |
2100 | |
2101 TestCallThrow(function() { throw "myexn" }) | |
2102 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) | |
2103 TestCallThrow(CreateFrozen({}, function() { throw "myexn" })) | |
2104 | |
2105 | |
2106 | |
2107 // Construction (new). | |
2108 | |
2109 var prototype = {} | |
2110 var receiver | |
2111 | |
2112 var handlerWithPrototype = { | |
2113 fix: function() { return {prototype: prototype} }, | |
2114 get: function(r, n) { assertEquals("prototype", n); return prototype } | |
2115 } | |
2116 | |
2117 var handlerSansPrototype = { | |
2118 fix: function() { return {} }, | |
2119 get: function(r, n) { assertEquals("prototype", n); return undefined } | |
2120 } | |
2121 | |
2122 function ReturnUndef(x, y) { "use strict"; receiver = this; this.sum = x + y } | |
2123 function ReturnThis(x, y) { "use strict"; receiver = this; this.sum = x + y; ret
urn this } | |
2124 function ReturnNew(x, y) { "use strict"; receiver = this; return {sum: x + y} } | |
2125 function ReturnNewWithProto(x, y) { | |
2126 "use strict"; | |
2127 receiver = this; | |
2128 var result = Object.create(prototype) | |
2129 result.sum = x + y | |
2130 return result | |
2131 } | |
2132 | |
2133 function TestConstruct(proto, constructTrap) { | |
2134 TestConstruct2(proto, constructTrap, handlerWithPrototype) | |
2135 TestConstruct2(proto, constructTrap, handlerSansPrototype) | |
2136 } | |
2137 | |
2138 function TestConstruct2(proto, constructTrap, handler) { | |
2139 var f = Proxy.createFunction(handler, function() {}, constructTrap) | |
2140 var o = new f(11, 31) | |
2141 // TODO(rossberg): doesn't hold, due to unrelated bug. | |
2142 // assertEquals(undefined, receiver) | |
2143 assertEquals(42, o.sum) | |
2144 assertSame(proto, Object.getPrototypeOf(o)) | |
2145 | |
2146 var f = CreateFrozen(handler, function() {}, constructTrap) | |
2147 var o = new f(11, 32) | |
2148 // TODO(rossberg): doesn't hold, due to unrelated bug. | |
2149 // assertEquals(undefined, receiver) | |
2150 assertEquals(43, o.sum) | |
2151 assertSame(proto, Object.getPrototypeOf(o)) | |
2152 } | |
2153 | |
2154 TestConstruct(Object.prototype, ReturnNew) | |
2155 TestConstruct(prototype, ReturnNewWithProto) | |
2156 | |
2157 TestConstruct(Object.prototype, Proxy.createFunction({}, ReturnNew)) | |
2158 TestConstruct(prototype, Proxy.createFunction({}, ReturnNewWithProto)) | |
2159 | |
2160 TestConstruct(Object.prototype, CreateFrozen({}, ReturnNew)) | |
2161 TestConstruct(prototype, CreateFrozen({}, ReturnNewWithProto)) | |
2162 | |
2163 | |
2164 function TestConstructFromCall(proto, returnsThis, callTrap) { | |
2165 TestConstructFromCall2(proto, returnsThis, callTrap, handlerWithPrototype) | |
2166 TestConstructFromCall2(proto, returnsThis, callTrap, handlerSansPrototype) | |
2167 } | |
2168 | |
2169 function TestConstructFromCall2(proto, returnsThis, callTrap, handler) { | |
2170 var f = Proxy.createFunction(handler, callTrap) | |
2171 var o = new f(11, 31) | |
2172 if (returnsThis) assertEquals(o, receiver) | |
2173 assertEquals(42, o.sum) | |
2174 assertSame(proto, Object.getPrototypeOf(o)) | |
2175 | |
2176 var f = CreateFrozen(handler, callTrap) | |
2177 var o = new f(11, 32) | |
2178 if (returnsThis) assertEquals(o, receiver) | |
2179 assertEquals(43, o.sum) | |
2180 assertSame(proto, Object.getPrototypeOf(o)) | |
2181 } | |
2182 | |
2183 TestConstructFromCall(Object.prototype, true, ReturnUndef) | |
2184 TestConstructFromCall(Object.prototype, true, ReturnThis) | |
2185 TestConstructFromCall(Object.prototype, false, ReturnNew) | |
2186 TestConstructFromCall(prototype, false, ReturnNewWithProto) | |
2187 | |
2188 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnUnd
ef)) | |
2189 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnThi
s)) | |
2190 TestConstructFromCall(Object.prototype, false, Proxy.createFunction({}, ReturnNe
w)) | |
2191 TestConstructFromCall(prototype, false, Proxy.createFunction({}, ReturnNewWithPr
oto)) | |
2192 | |
2193 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnUndef)) | |
2194 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnThis)) | |
2195 TestConstructFromCall(Object.prototype, false, CreateFrozen({}, ReturnNew)) | |
2196 TestConstructFromCall(prototype, false, CreateFrozen({}, ReturnNewWithProto)) | |
2197 | |
2198 ReturnUndef.prototype = prototype | |
2199 ReturnThis.prototype = prototype | |
2200 ReturnNew.prototype = prototype | |
2201 ReturnNewWithProto.prototype = prototype | |
2202 | |
2203 TestConstructFromCall(prototype, true, ReturnUndef) | |
2204 TestConstructFromCall(prototype, true, ReturnThis) | |
2205 TestConstructFromCall(Object.prototype, false, ReturnNew) | |
2206 TestConstructFromCall(prototype, false, ReturnNewWithProto) | |
2207 | |
2208 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnUnd
ef)) | |
2209 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnThi
s)) | |
2210 TestConstructFromCall(Object.prototype, false, Proxy.createFunction({}, ReturnNe
w)) | |
2211 TestConstructFromCall(prototype, false, Proxy.createFunction({}, ReturnNewWithPr
oto)) | |
2212 | |
2213 TestConstructFromCall(prototype, true, Proxy.createFunction(handlerWithPrototype
, ReturnUndef)) | |
2214 TestConstructFromCall(prototype, true, Proxy.createFunction(handlerWithPrototype
, ReturnThis)) | |
2215 TestConstructFromCall(Object.prototype, false, Proxy.createFunction(handlerWithP
rototype, ReturnNew)) | |
2216 TestConstructFromCall(prototype, false, Proxy.createFunction(handlerWithPrototyp
e, ReturnNewWithProto)) | |
2217 | |
2218 TestConstructFromCall(prototype, true, CreateFrozen(handlerWithPrototype, Return
Undef)) | |
2219 TestConstructFromCall(prototype, true, CreateFrozen(handlerWithPrototype, Return
This)) | |
2220 TestConstructFromCall(Object.prototype, false, CreateFrozen(handlerWithPrototype
, ReturnNew)) | |
2221 TestConstructFromCall(prototype, false, CreateFrozen(handlerWithPrototype, Retur
nNewWithProto)) | |
2222 | |
2223 | |
2224 function TestConstructThrow(trap) { | |
2225 TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}}, trap)) | |
2226 TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}}, | |
2227 function() {}, trap)) | |
2228 } | |
2229 | |
2230 function TestConstructThrow2(f) { | |
2231 assertThrows(function(){ new f(11) }, "myexn") | |
2232 Object.freeze(f) | |
2233 assertThrows(function(){ new f(11) }, "myexn") | |
2234 } | |
2235 | |
2236 TestConstructThrow(function() { throw "myexn" }) | |
2237 TestConstructThrow(Proxy.createFunction({}, function() { throw "myexn" })) | |
2238 TestConstructThrow(CreateFrozen({}, function() { throw "myexn" })) | |
2239 | |
2240 | |
2241 | |
2242 // Getters and setters. | |
2243 | |
2244 var value | |
2245 var receiver | |
2246 | |
2247 function TestAccessorCall(getterCallTrap, setterCallTrap) { | |
2248 var handler = {fix: function() { return {} }} | |
2249 var pgetter = Proxy.createFunction(handler, getterCallTrap) | |
2250 var psetter = Proxy.createFunction(handler, setterCallTrap) | |
2251 | |
2252 var o = {} | |
2253 var oo = Object.create(o) | |
2254 Object.defineProperty(o, "a", {get: pgetter, set: psetter}) | |
2255 Object.defineProperty(o, "b", {get: pgetter}) | |
2256 Object.defineProperty(o, "c", {set: psetter}) | |
2257 Object.defineProperty(o, "3", {get: pgetter, set: psetter}) | |
2258 Object.defineProperty(oo, "a", {value: 43}) | |
2259 | |
2260 receiver = "" | |
2261 assertEquals(42, o.a) | |
2262 assertSame(o, receiver) | |
2263 receiver = "" | |
2264 assertEquals(42, o.b) | |
2265 assertSame(o, receiver) | |
2266 receiver = "" | |
2267 assertEquals(undefined, o.c) | |
2268 assertEquals("", receiver) | |
2269 receiver = "" | |
2270 assertEquals(42, o["a"]) | |
2271 assertSame(o, receiver) | |
2272 receiver = "" | |
2273 assertEquals(42, o[3]) | |
2274 assertSame(o, receiver) | |
2275 | |
2276 receiver = "" | |
2277 assertEquals(43, oo.a) | |
2278 assertEquals("", receiver) | |
2279 receiver = "" | |
2280 assertEquals(42, oo.b) | |
2281 assertSame(o, receiver) | |
2282 receiver = "" | |
2283 assertEquals(undefined, oo.c) | |
2284 assertEquals("", receiver) | |
2285 receiver = "" | |
2286 assertEquals(43, oo["a"]) | |
2287 assertEquals("", receiver) | |
2288 receiver = "" | |
2289 assertEquals(42, oo[3]) | |
2290 assertSame(o, receiver) | |
2291 | |
2292 receiver = "" | |
2293 assertEquals(50, o.a = 50) | |
2294 assertSame(o, receiver) | |
2295 assertEquals(50, value) | |
2296 receiver = "" | |
2297 assertEquals(51, o.b = 51) | |
2298 assertEquals("", receiver) | |
2299 assertEquals(50, value) // no setter | |
2300 assertThrows(function() { "use strict"; o.b = 51 }, TypeError) | |
2301 receiver = "" | |
2302 assertEquals(52, o.c = 52) | |
2303 assertSame(o, receiver) | |
2304 assertEquals(52, value) | |
2305 receiver = "" | |
2306 assertEquals(53, o["a"] = 53) | |
2307 assertSame(o, receiver) | |
2308 assertEquals(53, value) | |
2309 receiver = "" | |
2310 assertEquals(54, o[3] = 54) | |
2311 assertSame(o, receiver) | |
2312 assertEquals(54, value) | |
2313 | |
2314 value = 0 | |
2315 receiver = "" | |
2316 assertEquals(60, oo.a = 60) | |
2317 assertEquals("", receiver) | |
2318 assertEquals(0, value) // oo has own 'a' | |
2319 assertEquals(61, oo.b = 61) | |
2320 assertSame("", receiver) | |
2321 assertEquals(0, value) // no setter | |
2322 assertThrows(function() { "use strict"; oo.b = 61 }, TypeError) | |
2323 receiver = "" | |
2324 assertEquals(62, oo.c = 62) | |
2325 assertSame(oo, receiver) | |
2326 assertEquals(62, value) | |
2327 receiver = "" | |
2328 assertEquals(63, oo["c"] = 63) | |
2329 assertSame(oo, receiver) | |
2330 assertEquals(63, value) | |
2331 receiver = "" | |
2332 assertEquals(64, oo[3] = 64) | |
2333 assertSame(oo, receiver) | |
2334 assertEquals(64, value) | |
2335 } | |
2336 | |
2337 TestAccessorCall( | |
2338 function() { receiver = this; return 42 }, | |
2339 function(x) { receiver = this; value = x } | |
2340 ) | |
2341 | |
2342 TestAccessorCall( | |
2343 function() { "use strict"; receiver = this; return 42 }, | |
2344 function(x) { "use strict"; receiver = this; value = x } | |
2345 ) | |
2346 | |
2347 TestAccessorCall( | |
2348 Proxy.createFunction({}, function() { receiver = this; return 42 }), | |
2349 Proxy.createFunction({}, function(x) { receiver = this; value = x }) | |
2350 ) | |
2351 | |
2352 TestAccessorCall( | |
2353 CreateFrozen({}, function() { receiver = this; return 42 }), | |
2354 CreateFrozen({}, function(x) { receiver = this; value = x }) | |
2355 ) | |
OLD | NEW |