Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 6 | // Flags: --allow-natives-syntax |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | |
| 8 | // Test instanceof with proxies. |
| 9 | |
| 10 | (function TestInstanceOfWithProxies() { |
| 11 | function foo(x) { |
| 12 | return x instanceof Array; |
| 13 | } |
| 14 | assertTrue(foo([])); |
| 15 | assertFalse(foo({})); |
| 16 | %OptimizeFunctionOnNextCall(foo); |
| 17 | assertTrue(foo([])); |
| 18 | assertFalse(foo({})); |
| 19 | |
| 20 | var handler = { |
| 21 | getPrototypeOf: function(target) { return Array.prototype; } |
| 22 | }; |
| 23 | var p = new Proxy({}, handler); |
| 24 | assertTrue(foo(p)); |
| 25 | var o = {}; |
| 26 | o.__proto__ = p; |
| 27 | assertTrue(foo(o)); |
| 28 | |
| 29 | // Make sure we are also correct if the handler throws. |
| 30 | handler.getPrototypeOf = function(target) { |
| 31 | throw "uncooperative"; |
| 32 | } |
| 33 | assertThrows("foo(o)"); |
| 34 | |
| 35 | // Including if the optimized function has a catch handler. |
| 36 | function foo_catch(x) { |
| 37 | try { |
| 38 | x instanceof Array; |
| 39 | } catch(e) { |
| 40 | assertEquals("uncooperative", e); |
| 41 | return true; |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | assertTrue(foo_catch(o)); |
| 46 | %OptimizeFunctionOnNextCall(foo_catch); |
| 47 | assertTrue(foo_catch(o)); |
| 48 | handler.getPrototypeOf = function(target) { return Array.prototype; } |
| 49 | assertFalse(foo_catch(o)); |
| 50 | })(); |
| 51 | |
| 52 | |
| 53 | (function testInstanceOfWithRecursiveProxy() { |
| 54 | // Make sure we gracefully deal with recursive proxies. |
| 55 | var proxy = new Proxy({},{}); |
| 56 | proxy.__proto__ = proxy; |
| 57 | // instanceof will cause an inifinite prototype walk. |
| 58 | assertThrows(() => { proxy instanceof Object }, RangeError); |
| 59 | |
| 60 | var proxy2 = new Proxy({}, {getPrototypeOf() { return proxy2 }}); |
| 61 | assertThrows(() => { proxy instanceof Object }, RangeError); |
| 62 | })(); |