OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 51 matching lines...) Loading... |
62 assertTrue(e.hasOwnProperty('message')); | 62 assertTrue(e.hasOwnProperty('message')); |
63 assertTrue(e.hasOwnProperty('stack')); | 63 assertTrue(e.hasOwnProperty('stack')); |
64 | 64 |
65 var e = %MakeReferenceError("my_test_error", [0, 1]); | 65 var e = %MakeReferenceError("my_test_error", [0, 1]); |
66 assertTrue(e.hasOwnProperty('stack')); | 66 assertTrue(e.hasOwnProperty('stack')); |
67 | 67 |
68 // Check that intercepting property access from toString is prevented for | 68 // Check that intercepting property access from toString is prevented for |
69 // compiler errors. This is not specified, but allowing interception | 69 // compiler errors. This is not specified, but allowing interception |
70 // through a getter can leak error objects from different | 70 // through a getter can leak error objects from different |
71 // script tags in the same context in a browser setting. | 71 // script tags in the same context in a browser setting. |
72 var errors = [SyntaxError, ReferenceError, TypeError]; | 72 var errors = [SyntaxError, ReferenceError, TypeError, RangeError, URIError]; |
| 73 var error_triggers = ["syntax error", |
| 74 "var error = reference", |
| 75 "undefined()", |
| 76 "String.fromCodePoint(0xFFFFFF)", |
| 77 "decodeURI('%F')"]; |
73 for (var i in errors) { | 78 for (var i in errors) { |
74 var name = errors[i].prototype.toString(); | 79 var name = errors[i].name; |
| 80 |
75 // Monkey-patch prototype. | 81 // Monkey-patch prototype. |
76 var props = ["name", "message", "stack"]; | 82 var props = ["name", "message", "stack"]; |
77 for (var j in props) { | 83 for (var j in props) { |
78 errors[i].prototype.__defineGetter__(props[j], fail); | 84 errors[i].prototype.__defineGetter__(props[j], fail); |
79 } | 85 } |
80 // String conversion should not invoke monkey-patched getters on prototype. | 86 // String conversion should not invoke monkey-patched getters on prototype. |
81 var e = new errors[i]; | 87 var error; |
82 assertEquals(name, e.toString()); | 88 try { |
| 89 eval(error_triggers[i]); |
| 90 } catch (e) { |
| 91 error = e; |
| 92 } |
| 93 assertTrue(error.toString().startsWith(name)); |
| 94 |
| 95 // Deleting message on the error (exposing the getter) is fine. |
| 96 delete error.message; |
| 97 assertEquals(name, error.toString()); |
| 98 |
| 99 // Custom properties shadowing the name are fine. |
| 100 var myerror = { name: "myerror", message: "mymessage"}; |
| 101 myerror.__proto__ = error; |
| 102 assertEquals("myerror: mymessage", myerror.toString()); |
| 103 |
83 // Custom getters in actual objects are welcome. | 104 // Custom getters in actual objects are welcome. |
84 e.__defineGetter__("name", function() { return "mine"; }); | 105 error.__defineGetter__("name", function() { return "mine"; }); |
85 assertEquals("mine", e.toString()); | 106 assertEquals("mine", error.toString()); |
| 107 |
| 108 // Custom properties shadowing the name are fine. |
| 109 var myerror2 = { message: "mymessage"}; |
| 110 myerror2.__proto__ = error; |
| 111 assertEquals("mine: mymessage", myerror2.toString()); |
86 } | 112 } |
87 | 113 |
88 // Monkey-patching non-static errors should still be observable. | 114 // Monkey-patching non-internal errors should still be observable. |
89 function MyError() {} | 115 function MyError() {} |
90 MyError.prototype = new Error; | 116 MyError.prototype = new Error; |
91 var errors = [Error, RangeError, EvalError, URIError, MyError]; | 117 var errors = [Error, RangeError, EvalError, URIError, |
| 118 SyntaxError, ReferenceError, TypeError, MyError]; |
92 for (var i in errors) { | 119 for (var i in errors) { |
93 errors[i].prototype.__defineGetter__("name", function() { return "my"; }); | 120 errors[i].prototype.__defineGetter__("name", function() { return "my"; }); |
94 errors[i].prototype.__defineGetter__("message", function() { return "moo"; }); | 121 errors[i].prototype.__defineGetter__("message", function() { return "moo"; }); |
95 var e = new errors[i]; | 122 var e = new errors[i]; |
96 assertEquals("my: moo", e.toString()); | 123 assertEquals("my: moo", e.toString()); |
97 } | 124 } |
98 | 125 |
99 | 126 |
100 Error.prototype.toString = Object.prototype.toString; | 127 Error.prototype.toString = Object.prototype.toString; |
101 assertEquals("[object Error]", Error.prototype.toString()); | 128 assertEquals("[object Error]", Error.prototype.toString()); |
102 assertEquals(Object.prototype, Error.prototype.__proto__); | 129 assertEquals(Object.prototype, Error.prototype.__proto__); |
103 var e = new Error("foo"); | 130 var e = new Error("foo"); |
104 assertEquals("[object Error]", e.toString()); | 131 assertEquals("[object Error]", e.toString()); |
OLD | NEW |