blob: 7acc9c114c854fee1f63d18e8dd36612b610ffdd [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 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// Testing that optimization of top-level object initialization doesn't
29// break V8.
30
31var x = new Object();
32x.a = 7;
33x.b = function() { return 42; };
34x.c = 88;
35x.d = "A Man Called Horse";
36
37assertEquals(7, x.a);
38assertEquals(42, x.b());
39assertEquals(88, x.c);
40assertEquals("A Man Called Horse", x.d);
41
42var y = new Object();
43y.a = 7;
44y.b = function() { return 42; };
45y.c = 12 * y.a;
46y.d = "A Man Called Horse";
47
48assertEquals(84, y.c);
49
50var z = new Object();
51z.a = 3;
52function forty_two() { return 42; };
53z.a += 4;
54z.b = forty_two;
55z.c = 12;
56z.c *= z.a;
57z.d = "A Man Called Horse";
58
59assertEquals(84, z.c);
60
61var x1 = new Object();
62var x2 = new Object();
63x1.a = 7;
64x1.b = function() { return 42; };
65x2.a = 7;
66x2.b = function() { return 42; };
67x1.c = 88;
68x1.d = "A Man Called Horse";
69x2.c = 88;
70x2.d = "A Man Called Horse";
71
72assertEquals(7, x1.a);
73assertEquals(42, x1.b());
74assertEquals(88, x1.c);
75assertEquals("A Man Called Horse", x1.d);
76
77assertEquals(7, x2.a);
78assertEquals(42, x2.b());
79assertEquals(88, x2.c);
80assertEquals("A Man Called Horse", x2.d);
81
82function Calculator(x, y) {
83 this.x = x;
84 this.y = y;
85}
86
87Calculator.prototype.sum = function() { return this.x + this.y; };
88Calculator.prototype.difference = function() { return this.x - this.y; };
89Calculator.prototype.product = function() { return this.x * this.y; };
90Calculator.prototype.quotient = function() { return this.x / this.y; };
91
92var calc = new Calculator(20, 10);
93assertEquals(30, calc.sum());
94assertEquals(10, calc.difference());
95assertEquals(200, calc.product());
96assertEquals(2, calc.quotient());
97
98var x = new Object();
99x.__defineGetter__('a', function() { return 7 });
100x.b = function() { return 42; };
101x.c = 88;
102x.d = "A Man Called Horse";
103
104assertEquals(7, x.a);
105assertEquals(42, x.b());
106assertEquals(88, x.c);
107assertEquals("A Man Called Horse", x.d);