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 29 30 // Check that %NewObjectFromBound works correctly when called from optimized 31 // frame. 32 function foo1(x, y, z) { 33 assertEquals(1, x); 34 assertEquals(2, y); 35 assertEquals(3, z); 36 } 37 38 function foo2(x, y, z) { 39 assertEquals(1, x); 40 assertEquals(2, y); 41 assertEquals(undefined, z); 42 } 43 44 function foo3(x, y, z) { 45 assertEquals(1, x); 46 assertEquals(2, y); 47 assertEquals(3, z); 48 } 49 50 51 var foob1 = foo1.bind({}, 1); 52 var foob2 = foo2.bind({}, 1); 53 var foob3 = foo3.bind({}, 1); 54 55 56 function f1(y, z) { 57 return %NewObjectFromBound(foob1); 58 } 59 60 function f2(y, z) { 61 return %NewObjectFromBound(foob2); 62 } 63 64 function f3(y, z) { 65 return %NewObjectFromBound(foob3); 66 } 67 68 // Check that %NewObjectFromBound looks at correct frame for inlined function. 69 function g1(z, y) { 70 return f1(y, z); /* f should be inlined into g, note rotated arguments */ 71 } 72 73 function g2(z, y, x) { 74 return f2(y); /* f should be inlined into g, note argument count mismatch */ 75 } 76 77 function g3(z, y, x) { 78 return f3(x, y, z); /* f should be inlined into g, note argument count mismatch */ 79 } 80 81 // Check that %NewObjectFromBound looks at correct frame for inlined function. 82 function ff(x) { } 83 function h1(z2, y2) { 84 var local_z = z2 >> 1; 85 ff(local_z); 86 var local_y = y2 >> 1; 87 ff(local_y); 88 return f1(local_y, local_z); /* f should be inlined into h */ 89 } 90 91 function h2(z2, y2, x2) { 92 var local_z = z2 >> 1; 93 ff(local_z); 94 var local_y = y2 >> 1; 95 ff(local_y); 96 return f2(local_y); /* f should be inlined into h */ 97 } 98 99 function h3(z2, y2, x2) { 100 var local_z = z2 >> 1; 101 ff(local_z); 102 var local_y = y2 >> 1; 103 ff(local_y); 104 var local_x = x2 >> 1; 105 ff(local_x); 106 return f3(local_x, local_y, local_z); /* f should be inlined into h */ 107 } 108 109 110 function invoke(f, args) { 111 for (var i = 0; i < 5; i++) f.apply(this, args); 112 %OptimizeFunctionOnNextCall(f); 113 f.apply(this, args); 114 } 115 116 invoke(f1, [2, 3]); 117 invoke(f2, [2]); 118 invoke(f3, [2, 3, 4]); 119 invoke(g1, [3, 2]); 120 invoke(g2, [3, 2, 4]); 121 invoke(g3, [4, 3, 2]); 122 invoke(h1, [6, 4]); 123 invoke(h2, [6, 4, 8]); 124 invoke(h3, [8, 6, 4]); 125 126 // Check that %_IsConstructCall returns correct value when inlined 127 var NON_CONSTRUCT_MARKER = {}; 128 var CONSTRUCT_MARKER = {}; 129 function baz(x) { 130 return (!%_IsConstructCall()) ? NON_CONSTRUCT_MARKER : CONSTRUCT_MARKER; 131 } 132 133 function bar(x, y, z) { 134 var non_construct = baz(0); /* baz should be inlined */ 135 assertSame(non_construct, NON_CONSTRUCT_MARKER); 136 var non_construct = baz(); /* baz should be inlined */ 137 assertSame(non_construct, NON_CONSTRUCT_MARKER); 138 var non_construct = baz(0, 0); /* baz should be inlined */ 139 assertSame(non_construct, NON_CONSTRUCT_MARKER); 140 var construct = new baz(0); 141 assertSame(construct, CONSTRUCT_MARKER); 142 var construct = new baz(0, 0); 143 assertSame(construct, CONSTRUCT_MARKER); 144 } 145 146 invoke(bar, [1, 2, 3]); 147