OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --allow-natives-syntax --inline-construct | 28 // Flags: --allow-natives-syntax --expose-gc --inline-construct |
29 | 29 |
30 // Test inlining of constructor calls. | 30 // Test inlining of constructor calls. |
31 | 31 |
32 function TestInlinedConstructor(closure) { | 32 function TestInlinedConstructor(closure) { |
33 var result; | 33 var result; |
34 var counter = { value:0 }; | 34 var counter = { value:0 }; |
35 result = closure(11, 12, counter); | 35 result = closure(11, 12, counter); |
36 assertEquals(23, result); | 36 assertEquals(23, result); |
37 assertEquals(1, counter.value); | 37 assertEquals(1, counter.value); |
38 result = closure(23, 19, counter); | 38 result = closure(23, 19, counter); |
39 assertEquals(42, result); | 39 assertEquals(42, result); |
40 assertEquals(2, counter.value); | 40 assertEquals(2, counter.value); |
41 %OptimizeFunctionOnNextCall(closure); | 41 %OptimizeFunctionOnNextCall(closure); |
42 result = closure(1, 42, counter) | 42 result = closure(1, 42, counter) |
43 assertEquals(43, result); | 43 assertEquals(43, result); |
44 assertEquals(3, counter.value); | 44 assertEquals(3, counter.value); |
45 result = closure("foo", "bar", counter) | 45 result = closure("foo", "bar", counter) |
46 assertEquals("foobar", result) | 46 assertEquals("foobar", result) |
47 assertEquals(4, counter.value); | 47 assertEquals(4, counter.value); |
48 } | 48 } |
49 | 49 |
| 50 function 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 |
50 | 74 |
51 // Test constructor returning nothing in all contexts. | 75 // Test constructor returning nothing in all contexts. |
52 function c1(a, b, counter) { | 76 function c1(a, b, counter) { |
53 this.x = a + b; | 77 this.x = a + b; |
54 counter.value++; | 78 counter.value++; |
55 } | 79 } |
56 function c1_value_context(a, b, counter) { | 80 TestInAllContexts(c1); |
57 var obj = new c1(a, b, counter); | |
58 return obj.x; | |
59 } | |
60 function c1_test_context(a, b, counter) { | |
61 if (!new c1(a, b, counter)) { | |
62 assertUnreachable("should not happen"); | |
63 } | |
64 return a + b; | |
65 } | |
66 function c1_effect_context(a, b, counter) { | |
67 new c1(a, b, counter); | |
68 return a + b; | |
69 } | |
70 TestInlinedConstructor(c1_value_context); | |
71 TestInlinedConstructor(c1_test_context); | |
72 TestInlinedConstructor(c1_effect_context); | |
73 | 81 |
74 | 82 |
75 // Test constructor returning an object in all contexts. | 83 // Test constructor returning an object in all contexts. |
76 function c2(a, b, counter) { | 84 function c2(a, b, counter) { |
77 var obj = new Object(); | 85 var obj = new Object(); |
78 obj.x = a + b; | 86 obj.x = a + b; |
79 counter.value++; | 87 counter.value++; |
80 return obj; | 88 return obj; |
81 } | 89 } |
82 function c2_value_context(a, b, counter) { | 90 TestInAllContexts(c2); |
83 var obj = new c2(a, b, counter); | |
84 return obj.x; | |
85 } | |
86 function c2_test_context(a, b, counter) { | |
87 if (!new c2(a, b, counter)) { | |
88 assertUnreachable("should not happen"); | |
89 } | |
90 return a + b; | |
91 } | |
92 function c2_effect_context(a, b, counter) { | |
93 new c2(a, b, counter); | |
94 return a + b; | |
95 } | |
96 TestInlinedConstructor(c2_value_context); | |
97 TestInlinedConstructor(c2_test_context); | |
98 TestInlinedConstructor(c2_effect_context); | |
99 | 91 |
100 | 92 |
101 // Test constructor returning a primitive value in all contexts. | 93 // Test constructor returning a primitive value in all contexts. |
102 function c3(a, b, counter) { | 94 function c3(a, b, counter) { |
103 this.x = a + b; | 95 this.x = a + b; |
104 counter.value++; | 96 counter.value++; |
105 return "not an object"; | 97 return "not an object"; |
106 } | 98 } |
107 function c3_value_context(a, b, counter) { | 99 TestInAllContexts(c3); |
108 var obj = new c3(a, b, counter); | |
109 return obj.x; | |
110 } | |
111 function c3_test_context(a, b, counter) { | |
112 if (!new c3(a, b, counter)) { | |
113 assertUnreachable("should not happen"); | |
114 } | |
115 return a + b; | |
116 } | |
117 function c3_effect_context(a, b, counter) { | |
118 new c3(a, b, counter); | |
119 return a + b; | |
120 } | |
121 TestInlinedConstructor(c3_value_context); | |
122 TestInlinedConstructor(c3_test_context); | |
123 TestInlinedConstructor(c3_effect_context); | |
124 | 100 |
125 | 101 |
126 // Test constructor called with too many arguments. | 102 // Test constructor called with too many arguments. |
127 function c_too_many(a, b) { | 103 function c_too_many(a, b) { |
128 this.x = a + b; | 104 this.x = a + b; |
129 } | 105 } |
130 function f_too_many(a, b, c) { | 106 function f_too_many(a, b, c) { |
131 var obj = new c_too_many(a, b, c); | 107 var obj = new c_too_many(a, b, c); |
132 return obj.x; | 108 return obj.x; |
133 } | 109 } |
(...skipping 22 matching lines...) Expand all Loading... |
156 | 132 |
157 // Test constructor that cannot be inlined. | 133 // Test constructor that cannot be inlined. |
158 function c_unsupported_syntax(a, b, counter) { | 134 function c_unsupported_syntax(a, b, counter) { |
159 try { | 135 try { |
160 this.x = a + b; | 136 this.x = a + b; |
161 counter.value++; | 137 counter.value++; |
162 } catch(e) { | 138 } catch(e) { |
163 throw new Error(); | 139 throw new Error(); |
164 } | 140 } |
165 } | 141 } |
166 function f_unsupported_syntax(a, b, counter) { | 142 TestInAllContexts(c_unsupported_syntax); |
167 var obj = new c_unsupported_syntax(a, b, counter); | 143 |
168 return obj.x; | 144 |
| 145 // Regression test: Inlined constructors called as functions do not get their |
| 146 // implicit receiver object set to undefined, even in strict mode. |
| 147 function c_strict(a, b, counter) { |
| 148 "use strict"; |
| 149 this.x = a + b; |
| 150 counter.value++; |
169 } | 151 } |
170 TestInlinedConstructor(f_unsupported_syntax); | 152 TestInAllContexts(c_strict); |
OLD | NEW |