alloc-object.js revision 3ef787dbeca8a5fb1086949cda830dccee07bfbd
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 --expose-gc --inline-construct
29
30// Test that inlined object allocation works for different layouts of
31// objects (e.g. in object properties, slack tracking in progress or
32// changing of function prototypes)
33
34function test_helper(construct, a, b) {
35  return new construct(a, b);
36}
37
38function test(construct) {
39  %DeoptimizeFunction(test);
40  test_helper(construct, 0, 0);
41  test_helper(construct, 0, 0);
42  %OptimizeFunctionOnNextCall(test_helper);
43  // Test adding a new property after allocation was inlined.
44  var o = test_helper(construct, 1, 2);
45  o.z = 3;
46  assertEquals(1, o.x);
47  assertEquals(2, o.y);
48  assertEquals(3, o.z);
49  // Test changing the prototype after allocation was inlined.
50  construct.prototype = { z:6 };
51  var o = test_helper(construct, 4, 5);
52  assertEquals(4, o.x);
53  assertEquals(5, o.y);
54  assertEquals(6, o.z);
55  %DeoptimizeFunction(test_helper);
56  gc();  // Makes V8 forget about type information for test_helper.
57}
58
59function finalize_slack_tracking(construct) {
60  // Value chosen based on kGenerousAllocationCount = 8.
61  for (var i = 0; i < 8; i++) {
62    new construct(0, 0);
63  }
64}
65
66
67// Both properties are pre-allocated in object properties.
68function ConstructInObjectPreAllocated(a, b) {
69  this.x = a;
70  this.y = b;
71}
72finalize_slack_tracking(ConstructInObjectPreAllocated);
73test(ConstructInObjectPreAllocated);
74
75
76// Both properties are unused in object properties.
77function ConstructInObjectUnused(a, b) {
78  this.x = a < 0 ? 0 : a;
79  this.y = b > 0 ? b : 0;
80}
81finalize_slack_tracking(ConstructInObjectUnused);
82test(ConstructInObjectUnused);
83
84
85// Test inlined allocation while slack tracking is still in progress.
86function ConstructWhileSlackTracking(a, b) {
87  this.x = a;
88  this.y = b;
89}
90test(ConstructWhileSlackTracking);
91