OLD | NEW |
(Empty) | |
| 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 |
| 32 function 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 |
| 50 |
| 51 // Test constructor returning nothing in all contexts. |
| 52 function c1(a, b, counter) { |
| 53 this.x = a + b; |
| 54 counter.value++; |
| 55 } |
| 56 function c1_value_context(a, b, counter) { |
| 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 |
| 74 |
| 75 // Test constructor returning an object in all contexts. |
| 76 function c2(a, b, counter) { |
| 77 var obj = new Object(); |
| 78 obj.x = a + b; |
| 79 counter.value++; |
| 80 return obj; |
| 81 } |
| 82 function c2_value_context(a, b, counter) { |
| 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 |
| 100 |
| 101 // Test constructor returning a primitive value in all contexts. |
| 102 function c3(a, b, counter) { |
| 103 this.x = a + b; |
| 104 counter.value++; |
| 105 return "not an object"; |
| 106 } |
| 107 function c3_value_context(a, b, counter) { |
| 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 |
| 125 |
| 126 // Test constructor called with too many arguments. |
| 127 function c_too_many(a, b) { |
| 128 this.x = a + b; |
| 129 } |
| 130 function f_too_many(a, b, c) { |
| 131 var obj = new c_too_many(a, b, c); |
| 132 return obj.x; |
| 133 } |
| 134 assertEquals(23, f_too_many(11, 12, 1)); |
| 135 assertEquals(42, f_too_many(23, 19, 1)); |
| 136 %OptimizeFunctionOnNextCall(f_too_many); |
| 137 assertEquals(43, f_too_many(1, 42, 1)); |
| 138 assertEquals("foobar", f_too_many("foo", "bar", "baz")) |
| 139 |
| 140 |
| 141 // Test constructor called with too few arguments. |
| 142 function c_too_few(a, b) { |
| 143 assertSame(undefined, b); |
| 144 this.x = a + 1; |
| 145 } |
| 146 function f_too_few(a) { |
| 147 var obj = new c_too_few(a); |
| 148 return obj.x; |
| 149 } |
| 150 assertEquals(12, f_too_few(11)); |
| 151 assertEquals(24, f_too_few(23)); |
| 152 %OptimizeFunctionOnNextCall(f_too_few); |
| 153 assertEquals(2, f_too_few(1)); |
| 154 assertEquals("foo1", f_too_few("foo")) |
| 155 |
| 156 |
| 157 // Test constructor that cannot be inlined. |
| 158 function c_unsupported_syntax(a, b, counter) { |
| 159 try { |
| 160 this.x = a + b; |
| 161 counter.value++; |
| 162 } catch(e) { |
| 163 throw new Error(); |
| 164 } |
| 165 } |
| 166 function f_unsupported_syntax(a, b, counter) { |
| 167 var obj = new c_unsupported_syntax(a, b, counter); |
| 168 return obj.x; |
| 169 } |
| 170 TestInlinedConstructor(f_unsupported_syntax); |
OLD | NEW |