1/* 2 * Copyright (C) 2006 Apple Computer, 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 COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26function bludgeonArguments() { if (0) arguments; return function g() {} } 27h = bludgeonArguments(); 28gc(); 29 30var failed = false; 31function pass(msg) 32{ 33 print("PASS: " + msg, "green"); 34} 35 36function fail(msg) 37{ 38 print("FAIL: " + msg, "red"); 39 failed = true; 40} 41 42function shouldBe(a, b) 43{ 44 var evalA; 45 try { 46 evalA = eval(a); 47 } catch(e) { 48 evalA = e; 49 } 50 51 if (evalA == b || isNaN(evalA) && typeof evalA == 'number' && isNaN(b) && typeof b == 'number') 52 pass(a + " should be " + b + " and is."); 53 else 54 fail(a + " should be " + b + " but instead is " + evalA + "."); 55} 56 57function shouldThrow(a) 58{ 59 var evalA; 60 try { 61 eval(a); 62 } catch(e) { 63 pass(a + " threw: " + e); 64 return; 65 } 66 67 fail(a + " did not throw an exception."); 68} 69 70function globalStaticFunction() 71{ 72 return 4; 73} 74 75shouldBe("globalStaticValue", 3); 76shouldBe("globalStaticFunction()", 4); 77 78shouldBe("typeof MyObject", "function"); // our object implements 'call' 79MyObject.cantFind = 1; 80shouldBe("MyObject.cantFind", undefined); 81MyObject.regularType = 1; 82shouldBe("MyObject.regularType", 1); 83MyObject.alwaysOne = 2; 84shouldBe("MyObject.alwaysOne", 1); 85MyObject.cantDelete = 1; 86delete MyObject.cantDelete; 87shouldBe("MyObject.cantDelete", 1); 88shouldBe("delete MyObject.throwOnDelete", "an exception"); 89MyObject.cantSet = 1; 90shouldBe("MyObject.cantSet", undefined); 91shouldBe("MyObject.throwOnGet", "an exception"); 92shouldBe("MyObject.throwOnSet = 5", "an exception"); 93shouldBe("MyObject('throwOnCall')", "an exception"); 94shouldBe("new MyObject('throwOnConstruct')", "an exception"); 95shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception"); 96 97var foundMyPropertyName = false; 98var foundRegularType = false; 99for (var p in MyObject) { 100 if (p == "myPropertyName") 101 foundMyPropertyName = true; 102 if (p == "regularType") 103 foundRegularType = true; 104} 105 106if (foundMyPropertyName) 107 pass("MyObject.myPropertyName was enumerated"); 108else 109 fail("MyObject.myPropertyName was not enumerated"); 110 111if (foundRegularType) 112 pass("MyObject.regularType was enumerated"); 113else 114 fail("MyObject.regularType was not enumerated"); 115 116var alwaysOneDescriptor = Object.getOwnPropertyDescriptor(MyObject, "alwaysOne"); 117shouldBe('typeof alwaysOneDescriptor', "object"); 118shouldBe('alwaysOneDescriptor.value', MyObject.alwaysOne); 119shouldBe('alwaysOneDescriptor.configurable', true); 120shouldBe('alwaysOneDescriptor.enumerable', false); // Actually it is. 121var cantFindDescriptor = Object.getOwnPropertyDescriptor(MyObject, "cantFind"); 122shouldBe('typeof cantFindDescriptor', "object"); 123shouldBe('cantFindDescriptor.value', MyObject.cantFind); 124shouldBe('cantFindDescriptor.configurable', true); 125shouldBe('cantFindDescriptor.enumerable', false); 126try { 127 // If getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw. 128 Object.getOwnPropertyDescriptor(MyObject, "throwOnGet"); 129} catch (e) { 130 pass("getting property descriptor of throwOnGet threw exception"); 131} 132var myPropertyNameDescriptor = Object.getOwnPropertyDescriptor(MyObject, "myPropertyName"); 133shouldBe('typeof myPropertyNameDescriptor', "object"); 134shouldBe('myPropertyNameDescriptor.value', MyObject.myPropertyName); 135shouldBe('myPropertyNameDescriptor.configurable', true); 136shouldBe('myPropertyNameDescriptor.enumerable', false); // Actually it is. 137try { 138 // if getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw. 139 Object.getOwnPropertyDescriptor(MyObject, "hasPropertyLie"); 140} catch (e) { 141 pass("getting property descriptor of hasPropertyLie threw exception"); 142} 143shouldBe('Object.getOwnPropertyDescriptor(MyObject, "doesNotExist")', undefined); 144 145myObject = new MyObject(); 146 147shouldBe("delete MyObject.regularType", true); 148shouldBe("MyObject.regularType", undefined); 149shouldBe("MyObject(0)", 1); 150shouldBe("MyObject()", undefined); 151shouldBe("typeof myObject", "object"); 152shouldBe("MyObject ? 1 : 0", true); // toBoolean 153shouldBe("+MyObject", 1); // toNumber 154shouldBe("(MyObject.toString())", "[object MyObject]"); // toString 155shouldBe("String(MyObject)", "MyObjectAsString"); // type conversion to string 156shouldBe("MyObject - 0", 1); // toNumber 157 158shouldBe("typeof MyConstructor", "object"); 159constructedObject = new MyConstructor(1); 160shouldBe("typeof constructedObject", "object"); 161shouldBe("constructedObject.value", 1); 162shouldBe("myObject instanceof MyObject", true); 163shouldBe("(new Object()) instanceof MyObject", false); 164 165shouldThrow("MyObject.nullGetSet = 1"); 166shouldThrow("MyObject.nullGetSet"); 167shouldThrow("MyObject.nullCall()"); 168shouldThrow("MyObject.hasPropertyLie"); 169 170derived = new Derived(); 171 172shouldBe("derived instanceof Derived", true); 173shouldBe("derived instanceof Base", true); 174 175// base properties and functions return 1 when called/gotten; derived, 2 176shouldBe("derived.baseProtoDup()", 2); 177shouldBe("derived.baseProto()", 1); 178shouldBe("derived.baseDup", 2); 179shouldBe("derived.baseOnly", 1); 180shouldBe("derived.protoOnly()", 2); 181shouldBe("derived.protoDup", 2); 182shouldBe("derived.derivedOnly", 2) 183 184// base properties throw 1 when set; derived, 2 185shouldBe("derived.baseDup = 0", 2); 186shouldBe("derived.baseOnly = 0", 1); 187shouldBe("derived.derivedOnly = 0", 2) 188shouldBe("derived.protoDup = 0", 2); 189 190derived2 = new Derived2(); 191 192shouldBe("derived2 instanceof Derived2", true); 193shouldBe("derived2 instanceof Derived", true); 194shouldBe("derived2 instanceof Base", true); 195 196// base properties and functions return 1 when called/gotten; derived, 2 197shouldBe("derived2.baseProtoDup()", 2); 198shouldBe("derived2.baseProto()", 1); 199shouldBe("derived2.baseDup", 2); 200shouldBe("derived2.baseOnly", 1); 201shouldBe("derived2.protoOnly()", 2); 202shouldBe("derived2.protoDup", 2); 203shouldBe("derived2.derivedOnly", 2) 204 205// base properties throw 1 when set; derived, 2 206shouldBe("derived2.baseDup = 0", 2); 207shouldBe("derived2.baseOnly = 0", 1); 208shouldBe("derived2.derivedOnly = 0", 2) 209shouldBe("derived2.protoDup = 0", 2); 210 211shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProto")', undefined); 212shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProtoDup")', undefined); 213var baseDupDescriptor = Object.getOwnPropertyDescriptor(derived, "baseDup"); 214shouldBe('typeof baseDupDescriptor', "object"); 215shouldBe('baseDupDescriptor.value', derived.baseDup); 216shouldBe('baseDupDescriptor.configurable', true); 217shouldBe('baseDupDescriptor.enumerable', false); 218var baseOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "baseOnly"); 219shouldBe('typeof baseOnlyDescriptor', "object"); 220shouldBe('baseOnlyDescriptor.value', derived.baseOnly); 221shouldBe('baseOnlyDescriptor.configurable', true); 222shouldBe('baseOnlyDescriptor.enumerable', false); 223shouldBe('Object.getOwnPropertyDescriptor(derived, "protoOnly")', undefined); 224var protoDupDescriptor = Object.getOwnPropertyDescriptor(derived, "protoDup"); 225shouldBe('typeof protoDupDescriptor', "object"); 226shouldBe('protoDupDescriptor.value', derived.protoDup); 227shouldBe('protoDupDescriptor.configurable', true); 228shouldBe('protoDupDescriptor.enumerable', false); 229var derivedOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "derivedOnly"); 230shouldBe('typeof derivedOnlyDescriptor', "object"); 231shouldBe('derivedOnlyDescriptor.value', derived.derivedOnly); 232shouldBe('derivedOnlyDescriptor.configurable', true); 233shouldBe('derivedOnlyDescriptor.enumerable', false); 234 235shouldBe("undefined instanceof MyObject", false); 236EvilExceptionObject.hasInstance = function f() { return f(); }; 237EvilExceptionObject.__proto__ = undefined; 238shouldThrow("undefined instanceof EvilExceptionObject"); 239EvilExceptionObject.hasInstance = function () { return true; }; 240shouldBe("undefined instanceof EvilExceptionObject", true); 241 242EvilExceptionObject.toNumber = function f() { return f(); } 243shouldThrow("EvilExceptionObject*5"); 244EvilExceptionObject.toStringExplicit = function f() { return f(); } 245shouldThrow("String(EvilExceptionObject)"); 246 247shouldBe("EmptyObject", "[object CallbackObject]"); 248 249if (failed) 250 throw "Some tests failed"; 251 252