Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
| 34 | function F() { |
| 35 | this.x = 42; |
| 36 | this.y = 87; |
| 37 | } |
| 38 | |
| 39 | // Force the inline caches to monomorphic state. |
| 40 | new F(); new F(); |
| 41 | |
| 42 | // Add a setter for x to Object.prototype and make sure it gets |
| 43 | // called. |
| 44 | var result_x; |
| 45 | Object.prototype.__defineSetter__('x', function(value) { result_x = value; }); |
| 46 | var f = new F(); |
| 47 | assertEquals(42, result_x); |
| 48 | assertTrue(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. |
| 52 | var result_y; |
| 53 | var proto = new Object(); |
| 54 | proto.__defineSetter__('y', function (value) { result_y = value; }); |
| 55 | var f = new F(); |
| 56 | f.y = undefined; |
| 57 | f.__proto__ = proto; |
| 58 | F.call(f); |
| 59 | assertEquals(87, result_y); |
| 60 | assertTrue(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. |
| 68 | var result_z; |
| 69 | var o1 = new Object(); |
| 70 | var o2 = new Object(); |
| 71 | // Add a z property to o1 to create a map transition. |
| 72 | o1.z = 32; |
| 73 | // Add a z accessor in the prototype chain for o1 and o2. |
| 74 | Object.prototype.__defineSetter__('z', function(value) { result_z = value; }); |
| 75 | // The accessor should be called for o2. |
| 76 | o2.z = 27; |
| 77 | assertEquals(27, result_z); |
| 78 | assertTrue(typeof o2.z == 'undefined'); |