instanceof-proxies.js 1.73 KB
Newer Older
1 2 3 4 5
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


6
// Flags: --allow-natives-syntax
7 8 9

// Test instanceof with proxies.

10
(function TestInstanceOfWithProxies() {
11 12 13
  function foo(x) {
    return x instanceof Array;
  }
14
  %PrepareFunctionForOptimization(foo);
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  assertTrue(foo([]));
  assertFalse(foo({}));
  %OptimizeFunctionOnNextCall(foo);
  assertTrue(foo([]));
  assertFalse(foo({}));

  var handler = {
    getPrototypeOf: function(target) { return Array.prototype; }
  };
  var p = new Proxy({}, handler);
  assertTrue(foo(p));
  var o = {};
  o.__proto__ = p;
  assertTrue(foo(o));

  // Make sure we are also correct if the handler throws.
  handler.getPrototypeOf = function(target) {
    throw "uncooperative";
  }
  assertThrows("foo(o)");

  // Including if the optimized function has a catch handler.
  function foo_catch(x) {
    try {
      x instanceof Array;
    } catch(e) {
      assertEquals("uncooperative", e);
      return true;
    }
    return false;
  }
46
  %PrepareFunctionForOptimization(foo_catch);
47 48 49 50 51
  assertTrue(foo_catch(o));
  %OptimizeFunctionOnNextCall(foo_catch);
  assertTrue(foo_catch(o));
  handler.getPrototypeOf = function(target) { return Array.prototype; }
  assertFalse(foo_catch(o));
52
})();
53

54 55 56 57 58 59 60 61 62 63 64

(function testInstanceOfWithRecursiveProxy() {
  // Make sure we gracefully deal with recursive proxies.
  var proxy = new Proxy({},{});
  proxy.__proto__ = proxy;
  // instanceof will cause an inifinite prototype walk.
  assertThrows(() => { proxy instanceof Object }, RangeError);

  var proxy2 = new Proxy({}, {getPrototypeOf() { return proxy2 }});
  assertThrows(() => { proxy instanceof Object }, RangeError);
})();