1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 description( 25 "This performs a number of different tests on JavaScript getters and setters." 26 ); 27 28 debug("the get set object declaration syntax"); 29 var o1 = { 'a':7, get b() { return this.a + 1 }, set b(x) { this.a = x } } 30 shouldBe("o1.b", "8"); 31 o1.b = 10; 32 shouldBe("o1.b", "11"); 33 34 debug("__defineGetter__ and __defineSetter__"); 35 var o2 = new Object() 36 o2.a = 7; 37 o2.__defineGetter__('b', function getB() { return this.a + 1} ) 38 o2.__defineSetter__('b', function setB(x) { this.a = x}) 39 shouldBe("o2.b", "8"); 40 o2.b = 10; 41 shouldBe("o2.b", "11"); 42 43 debug("Setting a value without having a setter"); 44 var o3 = { get x() { return 42; } } 45 shouldBe("o3.x = 10; o3.x", "42"); 46 47 debug("Getting a value without having a getter"); 48 var o4 = { set x(y) { }} 49 shouldBeUndefined("o4.x"); 50 51 debug("__lookupGetter__ and __lookupSetter__"); 52 var o4 = new Object() 53 function getB() { return this.a } 54 function setB(x) { this.a = x } 55 o4.__defineGetter__('b', getB) 56 o4.__defineSetter__('b', setB) 57 58 shouldBe("o4.__lookupGetter__('b')", "getB"); 59 shouldBe("o4.__lookupSetter__('b')", "setB"); 60 61 debug("__defineGetter__ and __defineSetter__ with various invalid arguments"); 62 var numExceptions = 0; 63 var o5 = new Object(); 64 shouldThrow("o5.__defineSetter__('a', null)"); 65 shouldThrow("o5.__defineSetter__('a', o5)"); 66 shouldThrow("o5.__defineGetter__('a', null)"); 67 shouldThrow("o5.__defineGetter__('a', o5)"); 68 69 debug("setters and getters with exceptions"); 70 var o6 = { get x() { throw 'Exception in get'}, set x(f) { throw 'Exception in set'}} 71 var x = 0; 72 var numExceptions = 0; 73 shouldThrow("x = o6.x"); 74 shouldBe("x", "0"); 75 shouldThrow("o6.x = 42"); 76 77 debug("Defining a setter should also define a getter for the same property which returns undefined. Thus, a getter defined on the prototype should not be called."); 78 o7 = { 'a':7, set x(b) { this.a = b; }} 79 o7.prototype = { get x() { return 42; }} 80 shouldBeUndefined("o7.x") 81 82 debug("If an object has a property and its prototype has a setter function for that property, then setting the property should set the property directly and not call the setter function."); 83 var o8 = new Object() 84 o8.numSets = 0; 85 o8.x = 10; 86 o8.__proto__.__defineSetter__('x', function() { this.numSets++; }) 87 o8.x = 20; 88 shouldBe("o8.numSets", "0"); 89 90 ({getter:"foo", b:"bar"}); 91 testObj=({get getter(){return 'getter was called.'}, b: 'bar'}) 92 shouldBe("typeof testObj.getter", "'string'"); 93 94 debug("the get set with string property name"); 95 var o9 = { 'a':7, get 'b'() { return this.a + 1 }, set 'b'(x) { this.a = x } } 96 shouldBe("o9.b", "8"); 97 o9.b = 10; 98 shouldBe("o9.b", "11"); 99 100 debug("the get set with numeric property name"); 101 var o10 = { 'a':7, get 42() { return this.a + 1 }, set 42(x) { this.a = x } } 102 shouldBe("o10[42]", "8"); 103 o10[42] = 10; 104 shouldBe("o10[42]", "11"); 105