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.
31var desc = Object.getOwnPropertyDescriptor(Error.prototype, 'name');
32assertFalse(desc['enumerable']);
33desc = Object.getOwnPropertyDescriptor(Error.prototype, 'message');
34assertFalse(desc['enumerable']);
35
36var e = new Error("foobar");
37desc = Object.getOwnPropertyDescriptor(e, 'message');
38assertFalse(desc['enumerable']);
39desc = Object.getOwnPropertyDescriptor(e, 'stack');
40assertFalse(desc['enumerable']);
41
42var e = new Error();
43assertFalse(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.
47for (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.
53function fail() { assertUnreachable(); };
54ReferenceError.prototype.__defineSetter__('name', fail);
55ReferenceError.prototype.__defineSetter__('message', fail);
56ReferenceError.prototype.__defineSetter__('stack', fail);
57
58var e = new ReferenceError();
59assertTrue(e.hasOwnProperty('stack'));
60
61var e = new ReferenceError('123');
62assertTrue(e.hasOwnProperty('message'));
63assertTrue(e.hasOwnProperty('stack'));
64
65var e = %MakeReferenceError("my_test_error", [0, 1]);
66assertTrue(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.
72var errors = [SyntaxError, ReferenceError, TypeError];
73for (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.
89function MyError() {}
90MyError.prototype = new Error;
91var errors = [Error, RangeError, EvalError, URIError, MyError];
92for (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
100Error.prototype.toString = Object.prototype.toString;
101assertEquals("[object Error]", Error.prototype.toString());
102assertEquals(Object.prototype, Error.prototype.__proto__);
103var e = new Error("foo");
104assertEquals("[object Error]", e.toString());
105