1 // Copyright 2011 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 // Flags: --allow-natives-syntax 29 30 // Check that message and name are not enumerable on Error objects. 31 var desc = Object.getOwnPropertyDescriptor(Error.prototype, 'name'); 32 assertFalse(desc['enumerable']); 33 desc = Object.getOwnPropertyDescriptor(Error.prototype, 'message'); 34 assertFalse(desc['enumerable']); 35 36 var e = new Error("foobar"); 37 desc = Object.getOwnPropertyDescriptor(e, 'message'); 38 assertFalse(desc['enumerable']); 39 desc = Object.getOwnPropertyDescriptor(e, 'stack'); 40 assertFalse(desc['enumerable']); 41 42 var e = new Error(); 43 assertFalse(e.hasOwnProperty('message')); 44 45 // name is not tested above, but in addition we should have no enumerable 46 // properties, so we simply assert that. 47 for (var v in e) { 48 assertUnreachable(); 49 } 50 51 // Check that error construction does not call setters for the 52 // properties on error objects in prototypes. 53 function fail() { assertUnreachable(); }; 54 ReferenceError.prototype.__defineSetter__('name', fail); 55 ReferenceError.prototype.__defineSetter__('message', fail); 56 ReferenceError.prototype.__defineSetter__('stack', fail); 57 58 var e = new ReferenceError(); 59 assertTrue(e.hasOwnProperty('stack')); 60 61 var e = new ReferenceError('123'); 62 assertTrue(e.hasOwnProperty('message')); 63 assertTrue(e.hasOwnProperty('stack')); 64 65 var e = %MakeReferenceError("my_test_error", [0, 1]); 66 assertTrue(e.hasOwnProperty('stack')); 67 68 // Check that intercepting property access from toString is prevented for 69 // compiler errors. This is not specified, but allowing interception 70 // through a getter can leak error objects from different 71 // script tags in the same context in a browser setting. 72 var errors = [SyntaxError, ReferenceError, TypeError]; 73 for (var i in errors) { 74 var name = errors[i].prototype.toString(); 75 // Monkey-patch prototype. 76 var props = ["name", "message", "stack"]; 77 for (var j in props) { 78 errors[i].prototype.__defineGetter__(props[j], fail); 79 } 80 // String conversion should not invoke monkey-patched getters on prototype. 81 var e = new errors[i]; 82 assertEquals(name, e.toString()); 83 // Custom getters in actual objects are welcome. 84 e.__defineGetter__("name", function() { return "mine"; }); 85 assertEquals("mine", e.toString()); 86 } 87 88 // Monkey-patching non-static errors should still be observable. 89 function MyError() {} 90 MyError.prototype = new Error; 91 var errors = [Error, RangeError, EvalError, URIError, MyError]; 92 for (var i in errors) { 93 errors[i].prototype.__defineGetter__("name", function() { return "my"; }); 94 errors[i].prototype.__defineGetter__("message", function() { return "moo"; }); 95 var e = new errors[i]; 96 assertEquals("my: moo", e.toString()); 97 } 98 99 100 Error.prototype.toString = Object.prototype.toString; 101 assertEquals("[object Error]", Error.prototype.toString()); 102 assertEquals(Object.prototype, Error.prototype.__proto__); 103 var e = new Error("foo"); 104 assertEquals("[object Error]", e.toString()); 105