blob: 8bd37eae20af727a3fc664f10b54fed2e44129c7 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 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 Murdoch8b112d22011-06-08 16:22:53 +010028// Flags: --allow-natives-syntax
29
Ben Murdochb0fe1622011-05-05 13:52:32 +010030// Test that we can inline a function that returns a constant.
31function TestInlineConstant(o) {
32 // Effect context.
33 o.f();
34 // Value context.
35 var x = o.f();
36 assertEquals(42, x);
37 assertEquals(42, o.f());
38 // Test context.
39 if (!o.f()) {
40 assertTrue(false); // Should not happen.
41 }
42}
43
44var o1 = {};
45o1.f = function() { return 42; };
Ben Murdoch8b112d22011-06-08 16:22:53 +010046for (var i = 0; i < 5; i++) TestInlineConstant(o1);
47%OptimizeFunctionOnNextCall(TestInlineConstant);
48TestInlineConstant(o1);
Ben Murdochb0fe1622011-05-05 13:52:32 +010049TestInlineConstant({f: o1.f});
50
51
52// Test that we can inline a function that returns 'this'.
53function TestInlineThis(o) {
54 // Effect context.
55 o.g();
56 // Value context.
57 var x = o.g();
58 assertEquals(o, x);
59 assertEquals(o, o.g());
60 // Test context.
61 if (!o.g()) {
62 assertTrue(false); // Should not happen.
63 }
64}
65
66var o2 = {};
67o2.g = function() { return this; };
Ben Murdoch8b112d22011-06-08 16:22:53 +010068for (var i = 0; i < 5; i++) TestInlineThis(o2);
69%OptimizeFunctionOnNextCall(TestInlineThis);
70TestInlineThis(o2);
Ben Murdochb0fe1622011-05-05 13:52:32 +010071TestInlineThis({g: o2.g});
72
73
74// Test that we can inline a function that returns 'this.x'.
75function TestInlineThisX(o) {
76 // Effect context.
77 o.h();
78 // Value context.
79 var x = o.h();
80 assertEquals(42, x);
81 assertEquals(42, o.h());
82 // Test context.
83 if (!o.h()) {
84 assertTrue(false); // Should not happen.
85 }
86}
87
88var o3 = {y:0,x:42};
89o3.h = function() { return this.x; };
Ben Murdoch8b112d22011-06-08 16:22:53 +010090for (var i = 0; i < 5; i++) TestInlineThisX(o3);
91%OptimizeFunctionOnNextCall(TestInlineThisX);
92TestInlineThisX(o3);
Ben Murdochb0fe1622011-05-05 13:52:32 +010093TestInlineThisX({h: o3.h, x:42});
94
95
96// Test that we can inline a function that returns 'this.x.length'.
97function TestInlineThisXLength(o) {
98 // Effect context.
99 o.h();
100 // Value context.
101 var x = o.h();
102 assertEquals(3, x);
103 assertEquals(3, o.h());
104 // Test context.
105 if (!o.h()) {
106 assertTrue(false); // Should not happen.
107 }
108}
109
110var o4 = {x:[1,2,3]};
111o4.h = function() { return this.x.length; };
Ben Murdoch8b112d22011-06-08 16:22:53 +0100112for (var i = 0; i < 5; i++) TestInlineThisXLength(o4);
113%OptimizeFunctionOnNextCall(TestInlineThisXLength);
114TestInlineThisXLength(o4);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100115TestInlineThisXLength({h: o4.h, x:[1,2,3]});
116
117
118// Test that we can inline a function that returns 'this.x.y'.
119function TestInlineThisXY(o) {
120 // Effect context.
121 o.h();
122 // Value context.
123 var x = o.h();
124 assertEquals(42, x);
125 assertEquals(42, o.h());
126 // Test context.
127 if (!o.h()) {
128 assertTrue(false); // Should not happen.
129 }
130}
131
132var o6 = {y:42}
133var o5 = {e:o6};
134o5.h = function() { return this.e.y; };
Ben Murdoch8b112d22011-06-08 16:22:53 +0100135for (var i = 0; i < 5; i++) TestInlineThisXY(o5);
136%OptimizeFunctionOnNextCall(TestInlineThisXY);
137TestInlineThisXY(o5);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100138TestInlineThisXY({h: o5.h, e:o6});
139
140
141// Test that we can inline a function that returns 'this.x.length'.
142function TestInlineThisX0(o) {
143 // Effect context.
144 o.foo();
145 // Value context.
146 var x = o.foo();
147 assertEquals(42, x);
148 assertEquals(42, o.foo());
149 // Test context.
150 if (!o.foo()) {
151 assertTrue(false); // Should not happen.
152 }
153}
154
155var o7 = {x:[42,43,44]};
156o7.foo = function() { return this.x[0]; };
Ben Murdoch8b112d22011-06-08 16:22:53 +0100157for (var i = 0; i < 5; i++) TestInlineThisX0(o7);
158%OptimizeFunctionOnNextCall(TestInlineThisX0);
159TestInlineThisX0(o7);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100160TestInlineThisX0({foo: o7.foo, x:[42,0,0]});