1// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --expose-gc --allow-natives-syntax
29
30function clear_all_ics() {
31  %NotifyContextDisposed();
32  gc();
33  gc();
34  gc();
35}
36
37
38// Test: verify that a monomorphic call retains the structural knowledge
39// of a global call, correctly throwing either ReferenceError or
40// TypeError on undefined depending on how the call is made.
41(function() {
42  foo = function(arg) { return arg + 1; }
43
44  function f() { foo(1); }
45
46  // Drive to monomorphic
47  f(); f(); f();
48
49  delete foo;
50  assertThrows(function() { f(); }, ReferenceError);
51  foo = function(arg) { return arg * 2; }
52  assertDoesNotThrow(function() { f(); });
53  f(); f(); f();
54  delete foo;
55  assertThrows(function() { f(); }, ReferenceError);
56  clear_all_ics();
57  foo = function(arg) { return arg * 3; }
58  f();
59  %OptimizeFunctionOnNextCall(f);
60  f();
61  delete foo;
62  assertThrows(function() { f(); }, ReferenceError);
63
64  foo = function(arg) { return arg * 3; }
65  function g() { this.foo(1); }
66  g(); g(); g();
67  delete foo;
68  assertThrows(function() { g(); }, TypeError);
69  foo = function(arg) { return arg * 3; }
70  g();
71  %OptimizeFunctionOnNextCall(g);
72  g();
73  delete foo;
74  assertThrows(function() { g(); }, TypeError);
75})();
76
77
78// Test: verify that a load with IC does the right thing.
79(function() {
80  var foo = function() { return a; }
81  a = 3;
82  foo(); foo(); foo();
83  delete a;
84  assertThrows(function() { foo(); }, ReferenceError);
85  a = "hi";
86  foo();
87  clear_all_ics();
88  foo();
89  %OptimizeFunctionOnNextCall(foo);
90  foo();
91  delete a;
92  assertThrows(function() { foo(); }, ReferenceError);
93  foo = function() { return this.a; }
94  assertDoesNotThrow(function() { foo(); });
95})();
96
97
98// Test: verify that a store with IC does the right thing.
99// If store is contextual and strict mode is set, throw a ReferenceError
100// if the variable isn't found.
101(function() {
102  var foo = function() { a = 3; }
103  var bar = function() { "use strict"; a = 3; }
104  foo(); foo(); foo();
105  delete a;
106  assertThrows(function() { bar(); }, ReferenceError);
107  a = 6;
108  foo(); foo(); foo();
109  bar(); bar();
110  clear_all_ics();
111  bar();
112  %OptimizeFunctionOnNextCall(bar);
113  bar();
114  delete a;
115  assertThrows(function() { bar(); }, ReferenceError);
116})();
117