object-isprototypeof.js 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2017 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 corner cases with null/undefined receivers.
(function() {
  function foo(x, y) { return Object.prototype.isPrototypeOf.call(x, y); }

11
  %PrepareFunctionForOptimization(foo);
12 13 14 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 46
  assertThrows(() => foo(null, {}));
  assertThrows(() => foo(undefined, {}));
  assertThrows(() => foo(null, []));
  assertThrows(() => foo(undefined, []));
  assertFalse(foo(null, 0));
  assertFalse(foo(undefined, 0));
  assertFalse(foo(null, ""));
  assertFalse(foo(undefined, ""));
  assertFalse(foo(null, null));
  assertFalse(foo(undefined, null));
  assertFalse(foo(null, undefined));
  assertFalse(foo(undefined, undefined));
  %OptimizeFunctionOnNextCall(foo);
  assertThrows(() => foo(null, {}));
  assertThrows(() => foo(undefined, {}));
  assertThrows(() => foo(null, []));
  assertThrows(() => foo(undefined, []));
  assertFalse(foo(null, 0));
  assertFalse(foo(undefined, 0));
  assertFalse(foo(null, ""));
  assertFalse(foo(undefined, ""));
  assertFalse(foo(null, null));
  assertFalse(foo(undefined, null));
  assertFalse(foo(null, undefined));
  assertFalse(foo(undefined, undefined));
})();

// Test general constructor prototype case.
(function() {
  function A() {}
  A.prototype = {};
  var a = new A;

  function foo(x) { return A.prototype.isPrototypeOf(x); }

47
  %PrepareFunctionForOptimization(foo);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  assertFalse(foo(0));
  assertFalse(foo(""));
  assertFalse(foo(null));
  assertFalse(foo(undefined));
  assertFalse(foo({}));
  assertFalse(foo([]));
  assertTrue(foo(a));
  assertTrue(foo(new A));
  assertTrue(foo({__proto__: a}));
  assertTrue(foo({__proto__: A.prototype}));
  %OptimizeFunctionOnNextCall(foo);
  assertFalse(foo(0));
  assertFalse(foo(""));
  assertFalse(foo(null));
  assertFalse(foo(undefined));
  assertFalse(foo({}));
  assertFalse(foo([]));
  assertTrue(foo(a));
  assertTrue(foo(new A));
  assertTrue(foo({__proto__: a}));
  assertTrue(foo({__proto__: A.prototype}));
})();

// Test known primitive values.
(function() {
  function A() {}
  A.prototype = {};
  var a = new A;

  function foo() { return A.prototype.isPrototypeOf(0); }

79
  %PrepareFunctionForOptimization(foo);
80 81 82 83 84 85 86 87 88 89 90 91
  assertFalse(foo());
  assertFalse(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertFalse(foo());
})();
(function() {
  function A() {}
  A.prototype = {};
  var a = new A;

  function foo() { return A.prototype.isPrototypeOf(null); }

92
  %PrepareFunctionForOptimization(foo);
93 94 95 96 97 98 99 100 101 102 103 104
  assertFalse(foo());
  assertFalse(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertFalse(foo());
})();
(function() {
  function A() {}
  A.prototype = {};
  var a = new A;

  function foo() { return A.prototype.isPrototypeOf(undefined); }

105
  %PrepareFunctionForOptimization(foo);
106 107 108 109 110 111 112 113 114 115 116 117 118 119
  assertFalse(foo());
  assertFalse(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertFalse(foo());
})();

// Test constant-folded prototype chain checks.
(function() {
  function A() {}
  A.prototype = {};
  var a = new A;

  function foo() { return A.prototype.isPrototypeOf(a); }

120
  %PrepareFunctionForOptimization(foo);
121 122 123 124 125 126 127 128 129 130 131 132
  assertTrue(foo());
  assertTrue(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertTrue(foo());
})();
(function() {
  function A() {}
  var a = new A;
  A.prototype = {};

  function foo() { return A.prototype.isPrototypeOf(a); }

133
  %PrepareFunctionForOptimization(foo);
134 135 136 137 138 139 140 141 142 143 144 145
  assertFalse(foo());
  assertFalse(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertFalse(foo());
})();

// Test Array prototype chain checks.
(function() {
  var a = [];

  function foo() { return Array.prototype.isPrototypeOf(a); }

146
  %PrepareFunctionForOptimization(foo);
147 148 149 150 151 152 153 154 155 156
  assertTrue(foo());
  assertTrue(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertTrue(foo());
})();
(function() {
  var a = [];

  function foo() { return Object.prototype.isPrototypeOf(a); }

157
  %PrepareFunctionForOptimization(foo);
158 159 160 161 162
  assertTrue(foo());
  assertTrue(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertTrue(foo());
})();
163 164 165 166 167 168 169 170 171 172 173 174 175
(function() {
  function A() {}
  A.prototype = {};
  var a = {__proto__: new A, gaga: 42};

  function foo() { a.gaga; return A.prototype.isPrototypeOf(a); }

  %PrepareFunctionForOptimization(foo);
  assertTrue(foo());
  assertTrue(foo());
  %OptimizeFunctionOnNextCall(foo);
  assertTrue(foo());
})();