blob: fa784cfc99e21c7c4b51c5797a2cfe7d68ab98eb [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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028// Flags: --allow-natives-syntax --inline-construct
Ben Murdoch3ef787d2012-04-12 10:51:47 +010029
30// Test inlining of constructor calls.
31
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032function TestInlinedConstructor(constructor, closure) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010033 var result;
34 var counter = { value:0 };
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 var noDeopt = { deopt:0 };
36 var forceDeopt = { /*empty*/ };
37
38 result = closure(constructor, 11, noDeopt, counter);
39 assertEquals(11, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010040 assertEquals(1, counter.value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041
42 result = closure(constructor, 23, noDeopt, counter);
43 assertEquals(23, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010044 assertEquals(2, counter.value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045
Ben Murdoch3ef787d2012-04-12 10:51:47 +010046 %OptimizeFunctionOnNextCall(closure);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 result = closure(constructor, 42, noDeopt, counter);
48 assertEquals(42, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010049 assertEquals(3, counter.value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050
51 result = closure(constructor, 127, forceDeopt, counter);
52 assertEquals(127, result)
Ben Murdoch3ef787d2012-04-12 10:51:47 +010053 assertEquals(4, counter.value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054
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;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010075}
76
77function TestInAllContexts(constructor) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 TestInlinedConstructor(constructor, value_context);
79 TestInlinedConstructor(constructor, test_context);
80 TestInlinedConstructor(constructor, effect_context);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010081}
82
83
84// Test constructor returning nothing in all contexts.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085function c1(val, deopt, counter) {
86 deopt.deopt;
87 this.x = val;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010088 counter.value++;
89}
90TestInAllContexts(c1);
91
92
93// Test constructor returning an object in all contexts.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094function c2(val, deopt, counter) {
95 var obj = {};
96 deopt.deopt;
97 obj.x = val;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010098 counter.value++;
99 return obj;
100}
101TestInAllContexts(c2);
102
103
104// Test constructor returning a primitive value in all contexts.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105function c3(val, deopt, counter) {
106 deopt.deopt;
107 this.x = val;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100108 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.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146function c_unsupported_syntax(val, deopt, counter) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100147 try {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 deopt.deopt;
149 this.x = val;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100150 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.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160function c_strict(val, deopt, counter) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100161 "use strict";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 deopt.deopt;
163 this.x = val;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100164 counter.value++;
165}
166TestInAllContexts(c_strict);