v8/test/mjsunit/es6/instanceof-proxies.js
Mythri A 55e8d61391 [Test] Add %PrepareForOptimization in tests
With bytecode flushing and lazy feedback allocation, we need to call
%PrepareForOptimization before we call %OptimizeFunctionOnNextCall

Bug: v8:8801, v8:8394
Change-Id: I1f84477a8cef27b4cff61b54daf6fe1a9e5f8e76
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1591775
Commit-Queue: Ross McIlroy <rmcilroy@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61148}
2019-05-02 09:43:17 +00:00

65 lines
1.7 KiB
JavaScript

// 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.
// Flags: --allow-natives-syntax
// Test instanceof with proxies.
(function TestInstanceOfWithProxies() {
function foo(x) {
return x instanceof Array;
}
%PrepareFunctionForOptimization(foo);
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;
}
%PrepareFunctionForOptimization(foo_catch);
assertTrue(foo_catch(o));
%OptimizeFunctionOnNextCall(foo_catch);
assertTrue(foo_catch(o));
handler.getPrototypeOf = function(target) { return Array.prototype; }
assertFalse(foo_catch(o));
})();
(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);
})();