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 --inline-construct
29
30// Test inlining of constructor calls.
31
32function TestInlinedConstructor(constructor, closure) {
33  var result;
34  var counter = { value:0 };
35  var noDeopt = { deopt:0 };
36  var forceDeopt = { /*empty*/ };
37
38  result = closure(constructor, 11, noDeopt, counter);
39  assertEquals(11, result);
40  assertEquals(1, counter.value);
41
42  result = closure(constructor, 23, noDeopt, counter);
43  assertEquals(23, result);
44  assertEquals(2, counter.value);
45
46  %OptimizeFunctionOnNextCall(closure);
47  result = closure(constructor, 42, noDeopt, counter);
48  assertEquals(42, result);
49  assertEquals(3, counter.value);
50
51  result = closure(constructor, 127, forceDeopt, counter);
52  assertEquals(127, result)
53  assertEquals(4, counter.value);
54
55  %DeoptimizeFunction(closure);
56  %ClearFunctionTypeFeedback(closure);
57  %ClearFunctionTypeFeedback(constructor);
58}
59
60function value_context(constructor, val, deopt, counter) {
61  var obj = new constructor(val, deopt, counter);
62  return obj.x;
63}
64
65function test_context(constructor, val, deopt, counter) {
66  if (!new constructor(val, deopt, counter)) {
67    assertUnreachable("should not happen");
68  }
69  return val;
70}
71
72function effect_context(constructor, val, deopt, counter) {
73  new constructor(val, deopt, counter);
74  return val;
75}
76
77function TestInAllContexts(constructor) {
78  TestInlinedConstructor(constructor, value_context);
79  TestInlinedConstructor(constructor, test_context);
80  TestInlinedConstructor(constructor, effect_context);
81}
82
83
84// Test constructor returning nothing in all contexts.
85function c1(val, deopt, counter) {
86  deopt.deopt;
87  this.x = val;
88  counter.value++;
89}
90TestInAllContexts(c1);
91
92
93// Test constructor returning an object in all contexts.
94function c2(val, deopt, counter) {
95  var obj = {};
96  deopt.deopt;
97  obj.x = val;
98  counter.value++;
99  return obj;
100}
101TestInAllContexts(c2);
102
103
104// Test constructor returning a primitive value in all contexts.
105function c3(val, deopt, counter) {
106  deopt.deopt;
107  this.x = val;
108  counter.value++;
109  return "not an object";
110}
111TestInAllContexts(c3);
112
113
114// Test constructor called with too many arguments.
115function c_too_many(a, b) {
116  this.x = a + b;
117}
118function f_too_many(a, b, c) {
119  var obj = new c_too_many(a, b, c);
120  return obj.x;
121}
122assertEquals(23, f_too_many(11, 12, 1));
123assertEquals(42, f_too_many(23, 19, 1));
124%OptimizeFunctionOnNextCall(f_too_many);
125assertEquals(43, f_too_many(1, 42, 1));
126assertEquals("foobar", f_too_many("foo", "bar", "baz"))
127
128
129// Test constructor called with too few arguments.
130function c_too_few(a, b) {
131  assertSame(undefined, b);
132  this.x = a + 1;
133}
134function f_too_few(a) {
135  var obj = new c_too_few(a);
136  return obj.x;
137}
138assertEquals(12, f_too_few(11));
139assertEquals(24, f_too_few(23));
140%OptimizeFunctionOnNextCall(f_too_few);
141assertEquals(2, f_too_few(1));
142assertEquals("foo1", f_too_few("foo"))
143
144
145// Test constructor that cannot be inlined.
146function c_unsupported_syntax(val, deopt, counter) {
147  try {
148    deopt.deopt;
149    this.x = val;
150    counter.value++;
151  } catch(e) {
152    throw new Error();
153  }
154}
155TestInAllContexts(c_unsupported_syntax);
156
157
158// Regression test: Inlined constructors called as functions do not get their
159// implicit receiver object set to undefined, even in strict mode.
160function c_strict(val, deopt, counter) {
161  "use strict";
162  deopt.deopt;
163  this.x = val;
164  counter.value++;
165}
166TestInAllContexts(c_strict);
167