simple-inlining.js revision b0fe1620dcb4135ac3ab2d66ff93072373911299
1// 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
28// Test that we can inline a function that returns a constant.
29function TestInlineConstant(o) {
30  // Effect context.
31  o.f();
32  // Value context.
33  var x = o.f();
34  assertEquals(42, x);
35  assertEquals(42, o.f());
36  // Test context.
37  if (!o.f()) {
38    assertTrue(false);  // Should not happen.
39  }
40}
41
42var o1 = {};
43o1.f = function() { return 42; };
44for (var i = 0; i < 10000; i++) TestInlineConstant(o1);
45TestInlineConstant({f: o1.f});
46
47
48// Test that we can inline a function that returns 'this'.
49function TestInlineThis(o) {
50  // Effect context.
51  o.g();
52  // Value context.
53  var x = o.g();
54  assertEquals(o, x);
55  assertEquals(o, o.g());
56  // Test context.
57  if (!o.g()) {
58    assertTrue(false);  // Should not happen.
59  }
60}
61
62var o2 = {};
63o2.g = function() { return this; };
64for (var i = 0; i < 10000; i++) TestInlineThis(o2);
65TestInlineThis({g: o2.g});
66
67
68// Test that we can inline a function that returns 'this.x'.
69function TestInlineThisX(o) {
70  // Effect context.
71  o.h();
72  // Value context.
73  var x = o.h();
74  assertEquals(42, x);
75  assertEquals(42, o.h());
76  // Test context.
77  if (!o.h()) {
78    assertTrue(false);  // Should not happen.
79  }
80}
81
82var o3 = {y:0,x:42};
83o3.h = function() { return this.x; };
84for (var i = 0; i < 10000; i++) TestInlineThisX(o3);
85TestInlineThisX({h: o3.h, x:42});
86
87
88// Test that we can inline a function that returns 'this.x.length'.
89function TestInlineThisXLength(o) {
90  // Effect context.
91  o.h();
92  // Value context.
93  var x = o.h();
94  assertEquals(3, x);
95  assertEquals(3, o.h());
96  // Test context.
97  if (!o.h()) {
98    assertTrue(false);  // Should not happen.
99  }
100}
101
102var o4 = {x:[1,2,3]};
103o4.h = function() { return this.x.length; };
104for (var i = 0; i < 10000; i++) TestInlineThisXLength(o4);
105TestInlineThisXLength({h: o4.h, x:[1,2,3]});
106
107
108// Test that we can inline a function that returns 'this.x.y'.
109function TestInlineThisXY(o) {
110  // Effect context.
111  o.h();
112  // Value context.
113  var x = o.h();
114  assertEquals(42, x);
115  assertEquals(42, o.h());
116  // Test context.
117  if (!o.h()) {
118    assertTrue(false);  // Should not happen.
119  }
120}
121
122var o6 = {y:42}
123var o5 = {e:o6};
124o5.h = function() { return this.e.y; };
125for (var i = 0; i < 10000; i++) TestInlineThisXY(o5);
126TestInlineThisXY({h: o5.h, e:o6});
127
128
129// Test that we can inline a function that returns 'this.x.length'.
130function TestInlineThisX0(o) {
131  // Effect context.
132  o.foo();
133  // Value context.
134  var x = o.foo();
135  assertEquals(42, x);
136  assertEquals(42, o.foo());
137  // Test context.
138  if (!o.foo()) {
139    assertTrue(false);  // Should not happen.
140  }
141}
142
143var o7 = {x:[42,43,44]};
144o7.foo = function() { return this.x[0]; };
145for (var i = 0; i < 10000; i++) TestInlineThisX0(o7);
146TestInlineThisX0({foo: o7.foo, x:[42,0,0]});
147