Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 | // Test that getters can be defined and called with an index as a parameter. |
| 29 | |
| 30 | var o = {}; |
| 31 | o.x = 42; |
| 32 | o.__defineGetter__('0', function() { return o.x; }); |
| 33 | assertEquals(o.x, o[0]); |
| 34 | assertEquals(o.x, o.__lookupGetter__('0')()); |
| 35 | |
| 36 | o.__defineSetter__('0', function(y) { o.x = y; }); |
| 37 | assertEquals(o.x, o[0]); |
| 38 | assertEquals(o.x, o.__lookupGetter__('0')()); |
| 39 | o[0] = 21; |
| 40 | assertEquals(21, o.x); |
| 41 | o.__lookupSetter__(0)(7); |
| 42 | assertEquals(7, o.x); |
| 43 | |
| 44 | function Pair(x, y) { |
| 45 | this.x = x; |
| 46 | this.y = y; |
| 47 | }; |
| 48 | Pair.prototype.__defineGetter__('0', function() { return this.x; }); |
| 49 | Pair.prototype.__defineGetter__('1', function() { return this.y; }); |
| 50 | Pair.prototype.__defineSetter__('0', function(x) { this.x = x; }); |
| 51 | Pair.prototype.__defineSetter__('1', function(y) { this.y = y; }); |
| 52 | |
| 53 | var p = new Pair(2, 3); |
| 54 | assertEquals(2, p[0]); |
| 55 | assertEquals(3, p[1]); |
| 56 | p.x = 7; |
| 57 | p[1] = 8; |
| 58 | assertEquals(7, p[0]); |
| 59 | assertEquals(7, p.x); |
| 60 | assertEquals(8, p[1]); |
| 61 | assertEquals(8, p.y); |
| 62 | |
| 63 | |
| 64 | // Testing that a defined getter doesn't get lost due to inline caching. |
| 65 | var expected = {}; |
| 66 | var actual = {}; |
| 67 | for (var i = 0; i < 10; i++) { |
| 68 | expected[i] = actual[i] = i; |
| 69 | } |
| 70 | function testArray() { |
| 71 | for (var i = 0; i < 10; i++) { |
| 72 | assertEquals(expected[i], actual[i]); |
| 73 | } |
| 74 | } |
| 75 | actual[1000000] = -1; |
| 76 | testArray(); |
| 77 | testArray(); |
| 78 | actual.__defineGetter__('0', function() { return expected[0]; }); |
| 79 | expected[0] = 42; |
| 80 | testArray(); |
| 81 | expected[0] = 111; |
| 82 | testArray(); |
| 83 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 84 | // Using a setter where only a getter is defined does not throw an exception, |
| 85 | // unless we are in strict mode. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | var q = {}; |
| 87 | q.__defineGetter__('0', function() { return 42; }); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 88 | assertDoesNotThrow('q[0] = 7'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | |
| 90 | // Using a getter where only a setter is defined returns undefined. |
| 91 | var q1 = {}; |
| 92 | q1.__defineSetter__('0', function() {q1.b = 17;}); |
| 93 | assertEquals(q1[0], undefined); |
| 94 | // Setter works |
| 95 | q1[0] = 3; |
| 96 | assertEquals(q1[0], undefined); |
| 97 | assertEquals(q1.b, 17); |
| 98 | |
| 99 | // Complex case of using an undefined getter. |
| 100 | // From http://code.google.com/p/v8/issues/detail?id=298 |
| 101 | // Reported by nth10sd. |
| 102 | |
| 103 | a = function() {}; |
| 104 | __defineSetter__("0", function() {}); |
| 105 | if (a |= '') {}; |
| 106 | assertThrows('this[a].__parent__'); |
| 107 | assertEquals(a, 0); |
| 108 | assertEquals(this[a], undefined); |