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