1// Copyright 2008 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 setter accessors added to the prototype chain are called
29// when setting properties.
30
31// Test that accessors added to the prototype chain are called
32// eventhough there are inline caches for setting the property
33
34function F() {
35  this.x = 42;
36  this.y = 87;
37}
38
39// Force the inline caches to monomorphic state.
40new F(); new F();
41
42// Add a setter for x to Object.prototype and make sure it gets
43// called.
44var result_x;
45Object.prototype.__defineSetter__('x', function(value) { result_x = value; });
46var f = new F();
47assertEquals(42, result_x);
48assertTrue(typeof f.x == 'undefined');
49
50// Add a setter for y by changing the prototype of f and make sure
51// that gets called too.
52var result_y;
53var proto = new Object();
54proto.__defineSetter__('y', function (value) { result_y = value; });
55var f = new F();
56f.y = undefined;
57f.__proto__ = proto;
58F.call(f);
59assertEquals(87, result_y);
60assertTrue(typeof f.y == 'undefined');
61
62
63// Test the same issue in the runtime system.  Make sure that
64// accessors added to the prototype chain are called instead of
65// following map transitions.
66//
67// Create two objects.
68var result_z;
69var o1 = new Object();
70var o2 = new Object();
71// Add a z property to o1 to create a map transition.
72o1.z = 32;
73// Add a z accessor in the prototype chain for o1 and o2.
74Object.prototype.__defineSetter__('z', function(value) { result_z = value; });
75// The accessor should be called for o2.
76o2.z = 27;
77assertEquals(27, result_z);
78assertTrue(typeof o2.z == 'undefined');
79