• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 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// Make sure it's an error if @@hasInstance isn't a function.
6(function() {
7  var F = {};
8  F[Symbol.hasInstance] = null;
9  assertThrows(function() { 0 instanceof F; }, TypeError);
10})();
11
12// Make sure the result is coerced to boolean.
13(function() {
14  var F = {};
15  F[Symbol.hasInstance] = function() { return undefined; };
16  assertEquals(0 instanceof F, false);
17  F[Symbol.hasInstance] = function() { return null; };
18  assertEquals(0 instanceof F, false);
19  F[Symbol.hasInstance] = function() { return true; };
20  assertEquals(0 instanceof F, true);
21})();
22
23// Make sure if @@hasInstance throws, we catch it.
24(function() {
25  var F = {};
26  F[Symbol.hasInstance] = function() { throw new Error("always throws"); }
27  try {
28    0 instanceof F;
29  } catch (e) {
30    assertEquals(e.message, "always throws");
31  }
32})();
33
34// @@hasInstance works for bound functions.
35(function() {
36  var BC = function() {};
37  var bc = new BC();
38  var bound = BC.bind();
39  assertEquals(bound[Symbol.hasInstance](bc), true);
40  assertEquals(bound[Symbol.hasInstance]([]), false);
41})();
42
43// if OrdinaryHasInstance is passed a non-callable receiver, return false.
44assertEquals(Function.prototype[Symbol.hasInstance].call(Array, []), true);
45assertEquals(Function.prototype[Symbol.hasInstance].call({}, {}), false);
46
47// OrdinaryHasInstance passed a non-object argument returns false.
48assertEquals(Function.prototype[Symbol.hasInstance].call(Array, 0), false);
49
50// Cannot assign to @@hasInstance with %FunctionPrototype%.
51(function() {
52  "use strict";
53  function F() {}
54  assertThrows(function() { F[Symbol.hasInstance] = (v) => v }, TypeError);
55})();
56
57// Check correct invocation of @@hasInstance handler on function instance.
58(function() {
59  function F() {}
60  var counter = 0;
61  var proto = Object.getPrototypeOf(F);
62  Object.setPrototypeOf(F, null);
63  F[Symbol.hasInstance] = function(v) { ++counter; return true };
64  Object.setPrototypeOf(F, proto);
65  assertTrue(1 instanceof F);
66  assertEquals(1, counter);
67})();
68