1// Copyright 2012 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: --allow-natives-syntax
29
30// Test materialization of the arguments object when deoptimizing a
31// strict mode closure after modifying an argument.
32
33(function () {
34  var forceDeopt = 0;
35  function inner(x) {
36    "use strict";
37    x = 2;
38    // Do not remove this %DebugPrint as it makes sure the deopt happens
39    // after the assignment and is not hoisted above the assignment.
40    %DebugPrint(arguments[0]);
41    forceDeopt + 1;
42    return arguments[0];
43  }
44
45  assertEquals(1, inner(1));
46  assertEquals(1, inner(1));
47  %OptimizeFunctionOnNextCall(inner);
48  assertEquals(1, inner(1));
49  forceDeopt = "not a number";
50  assertEquals(1, inner(1));
51})();
52
53
54// Test materialization of the arguments object when deoptimizing an
55// inlined strict mode closure after modifying an argument.
56
57(function () {
58  var forceDeopt = 0;
59  function inner(x) {
60    "use strict";
61    x = 2;
62    // Do not remove this %DebugPrint as it makes sure the deopt happens
63    // after the assignment and is not hoisted above the assignment.
64    %DebugPrint(arguments[0]);
65    forceDeopt + 1;
66    return arguments[0];
67  }
68
69  function outer(x) {
70    return inner(x);
71  }
72
73  assertEquals(1, outer(1));
74  assertEquals(1, outer(1));
75  %OptimizeFunctionOnNextCall(outer);
76  assertEquals(1, outer(1));
77  forceDeopt = "not a number";
78  assertEquals(1, outer(1));
79})();
80
81
82// Test materialization of the multiple arguments objects when
83// deoptimizing several inlined closure after modifying an argument.
84
85(function () {
86  var forceDeopt = 0;
87  function inner(x,y,z) {
88    "use strict";
89    x = 3;
90    // Do not remove this %DebugPrint as it makes sure the deopt happens
91    // after the assignment and is not hoisted above the assignment.
92    %DebugPrint(arguments[0]);
93    forceDeopt + 1;
94    return arguments[0];
95  }
96
97  function middle(x) {
98    "use strict";
99    x = 2;
100    return inner(10*x, 20*x, 30*x) + arguments[0];
101  }
102
103  function outer(x) {
104   return middle(x);
105  }
106
107  assertEquals(21, outer(1));
108  assertEquals(21, outer(1));
109  %OptimizeFunctionOnNextCall(outer);
110  assertEquals(21, outer(1));
111  forceDeopt = "not a number";
112  assertEquals(21, outer(1));
113})();
114