1 // Copyright 2009 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
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.
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.
28 function testMethodNameInference() {
30 Foo.prototype.bar = function () { FAIL; };
34 function testNested() {
47 function testArrayNative() {
48 [1, 2, 3].map(function () { FAIL; });
51 function testImplicitConversion() {
53 Nirk.prototype.valueOf = function () { FAIL; };
54 return 1 + (new Nirk);
58 eval("function Doo() { FAIL; }; Doo();");
61 function testNestedEval() {
63 eval("function Outer() { eval('function Inner() { eval(x); }'); Inner(); }; Outer();");
66 function testEvalWithSourceURL() {
67 eval("function Doo() { FAIL; }; Doo();\n//# sourceURL=res://name");
70 function testNestedEvalWithSourceURL() {
72 var innerEval = 'function Inner() { eval(x); }\n//@ sourceURL=res://inner-eval';
73 eval("function Outer() { eval(innerEval); Inner(); }; Outer();\n//# sourceURL=res://outer-eval");
76 function testValue() {
77 Number.prototype.causeError = function () { FAIL; };
81 function testConstructor() {
82 function Plonk() { FAIL; }
86 function testRenamedMethod() {
87 function a$b$c$d() { return FAIL; }
89 Wookie.prototype.d = a$b$c$d;
93 function testAnonymousMethod() {
94 (function () { FAIL }).call([1, 2, 3]);
97 function CustomError(message, stripPoint) {
98 this.message = message;
99 Error.captureStackTrace(this, stripPoint);
102 CustomError.prototype.toString = function () {
103 return "CustomError: " + this.message;
106 function testDefaultCustomError() {
107 throw new CustomError("hep-hey", undefined);
110 function testStrippedCustomError() {
111 throw new CustomError("hep-hey", CustomError);
114 MyObj = function() { FAIL; }
116 MyObjCreator = function() {}
118 MyObjCreator.prototype.Create = function() {
122 function testClassNames() {
123 (new MyObjCreator).Create();
126 // Utility function for testing that the expected strings occur
127 // in the stack trace produced when running the given function.
128 function testTrace(name, fun, expected, unexpected) {
133 for (var i = 0; i < expected.length; i++) {
134 assertTrue(e.stack.indexOf(expected[i]) != -1,
135 name + " doesn't contain expected[" + i + "] stack = " + e.stack);
138 for (var i = 0; i < unexpected.length; i++) {
139 assertEquals(e.stack.indexOf(unexpected[i]), -1,
140 name + " contains unexpected[" + i + "]");
145 assertTrue(threw, name + " didn't throw");
148 // Test that the error constructor is not shown in the trace
149 function testCallerCensorship() {
154 assertEquals(-1, e.stack.indexOf('at new ReferenceError'),
155 "CallerCensorship contained new ReferenceError");
158 assertTrue(threw, "CallerCensorship didn't throw");
161 // Test that the explicit constructor call is shown in the trace
162 function testUnintendedCallerCensorship() {
166 toString: function () {
171 assertTrue(e.stack.indexOf('at new ReferenceError') != -1,
172 "UnintendedCallerCensorship didn't contain new ReferenceError");
175 assertTrue(threw, "UnintendedCallerCensorship didn't throw");
178 // If an error occurs while the stack trace is being formatted it should
179 // be handled gracefully.
180 function testErrorsDuringFormatting() {
182 Nasty.prototype.foo = function () { throw new RangeError(); };
184 n.__defineGetter__('constructor', function () { CONS_FAIL; });
190 assertTrue(e.stack.indexOf('<error: ReferenceError') != -1,
191 "ErrorsDuringFormatting didn't contain error: ReferenceError");
193 assertTrue(threw, "ErrorsDuringFormatting didn't throw");
195 // Now we can't even format the message saying that we couldn't format
196 // the stack frame. Put that in your pipe and smoke it!
197 ReferenceError.prototype.toString = function () { NESTED_FAIL; };
202 assertTrue(e.stack.indexOf('<error>') != -1,
203 "ErrorsDuringFormatting didn't contain <error>");
205 assertTrue(threw, "ErrorsDuringFormatting didnt' throw (2)");
209 // Poisonous object that throws a reference error if attempted converted to
210 // a primitive values.
211 var thrower = { valueOf: function() { FAIL; },
212 toString: function() { FAIL; } };
214 // Tests that a native constructor function is included in the
216 function testTraceNativeConstructor(nativeFunc) {
217 var nativeFuncName = nativeFunc.name;
219 new nativeFunc(thrower);
220 assertUnreachable(nativeFuncName);
222 assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
226 // Tests that a native conversion function is included in the
228 function testTraceNativeConversion(nativeFunc) {
229 var nativeFuncName = nativeFunc.name;
232 assertUnreachable(nativeFuncName);
234 assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
239 function testOmittedBuiltin(throwing, omitted) {
242 assertUnreachable(omitted);
244 assertTrue(e.stack.indexOf(omitted) < 0, omitted);
249 testTrace("testArrayNative", testArrayNative, ["Array.map (native)"]);
250 testTrace("testNested", testNested, ["at one", "at two", "at three"]);
251 testTrace("testMethodNameInference", testMethodNameInference, ["at Foo.bar"]);
252 testTrace("testImplicitConversion", testImplicitConversion, ["at Nirk.valueOf"]);
253 testTrace("testEval", testEval, ["at Doo (eval at testEval"]);
254 testTrace("testNestedEval", testNestedEval, ["eval at Inner (eval at Outer"]);
255 testTrace("testEvalWithSourceURL", testEvalWithSourceURL,
256 [ "at Doo (res://name:1:18)" ]);
257 testTrace("testNestedEvalWithSourceURL", testNestedEvalWithSourceURL,
258 [" at Inner (res://inner-eval:1:20)",
259 " at Outer (res://outer-eval:1:37)"]);
260 testTrace("testValue", testValue, ["at Number.causeError"]);
261 testTrace("testConstructor", testConstructor, ["new Plonk"]);
262 testTrace("testRenamedMethod", testRenamedMethod, ["Wookie.a$b$c$d [as d]"]);
263 testTrace("testAnonymousMethod", testAnonymousMethod, ["Array.<anonymous>"]);
264 testTrace("testDefaultCustomError", testDefaultCustomError,
265 ["hep-hey", "new CustomError"],
266 ["collectStackTrace"]);
267 testTrace("testStrippedCustomError", testStrippedCustomError, ["hep-hey"],
268 ["new CustomError", "collectStackTrace"]);
269 testTrace("testClassNames", testClassNames,
270 ["new MyObj", "MyObjCreator.Create"], ["as Create"]);
271 testCallerCensorship();
272 testUnintendedCallerCensorship();
273 testErrorsDuringFormatting();
275 testTraceNativeConversion(String); // Does ToString on argument.
276 testTraceNativeConversion(Number); // Does ToNumber on argument.
277 testTraceNativeConversion(RegExp); // Does ToString on argument.
279 testTraceNativeConstructor(String); // Does ToString on argument.
280 testTraceNativeConstructor(Number); // Does ToNumber on argument.
281 testTraceNativeConstructor(RegExp); // Does ToString on argument.
282 testTraceNativeConstructor(Date); // Does ToNumber on argument.
284 // Omitted because QuickSort has builtins object as receiver, and is non-native
286 testOmittedBuiltin(function(){ [thrower, 2].sort(function (a,b) {
287 (b < a) - (a < b); });
290 // Omitted because ADD from runtime.js is non-native builtin.
291 testOmittedBuiltin(function(){ thrower + 2; }, "ADD");
293 var error = new Error();
294 error.toString = function() { assertUnreachable(); };
298 error.name = { toString: function() { assertUnreachable(); }};
299 error.message = { toString: function() { assertUnreachable(); }};
303 Array.prototype.push = function(x) { assertUnreachable(); };
304 Array.prototype.join = function(x) { assertUnreachable(); };
308 error = new Error({ toString: function() { fired = true; } });
313 // Check that throwing exception in a custom stack trace formatting function
314 // does not lead to recursion.
315 Error.prepareStackTrace = function() { throw new Error("abc"); };
327 assertEquals("abc", message);
329 // Test that modifying Error.prepareStackTrace by itself works.
330 Error.prepareStackTrace = function() { Error.prepareStackTrace = "custom"; };
333 assertEquals("custom", Error.prepareStackTrace);
335 // Check that the formatted stack trace can be set to undefined.
337 error.stack = undefined;
338 assertEquals(undefined, error.stack);
340 // Check that the stack trace accessors are not forcibly set.
342 Object.freeze(my_error);
343 assertThrows(function() { Error.captureStackTrace(my_error); });
346 Object.preventExtensions(my_error);
347 assertThrows(function() { Error.captureStackTrace(my_error); });
350 my_error = new Error();
351 var stolen_getter = Object.getOwnPropertyDescriptor(my_error, 'stack').get;
352 Object.defineProperty(fake_error, 'stack', { get: stolen_getter });
353 assertEquals(undefined, fake_error.stack);