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 testFunctionName() {
98 function gen(name, counter) {
99 var f = function foo() {
103 gen(name, counter - 1)();
106 Object.defineProperty(f, 'name', {get: function(){ throw 239; }});
107 } else if (counter == 3) {
108 Object.defineProperty(f, 'name', {value: 'boo' + '_' + counter});
110 Object.defineProperty(f, 'name', {writable: true});
114 f.name = name + '_' + counter;
121 function testFunctionInferredName() {
128 function CustomError(message, stripPoint) {
129 this.message = message;
130 Error.captureStackTrace(this, stripPoint);
133 CustomError.prototype.toString = function () {
134 return "CustomError: " + this.message;
137 function testDefaultCustomError() {
138 throw new CustomError("hep-hey", undefined);
141 function testStrippedCustomError() {
142 throw new CustomError("hep-hey", CustomError);
145 MyObj = function() { FAIL; }
147 MyObjCreator = function() {}
149 MyObjCreator.prototype.Create = function() {
153 function testClassNames() {
154 (new MyObjCreator).Create();
157 // Utility function for testing that the expected strings occur
158 // in the stack trace produced when running the given function.
159 function testTrace(name, fun, expected, unexpected) {
164 for (var i = 0; i < expected.length; i++) {
165 assertTrue(e.stack.indexOf(expected[i]) != -1,
166 name + " doesn't contain expected[" + i + "] stack = " + e.stack);
169 for (var i = 0; i < unexpected.length; i++) {
170 assertEquals(e.stack.indexOf(unexpected[i]), -1,
171 name + " contains unexpected[" + i + "]");
176 assertTrue(threw, name + " didn't throw");
179 // Test that the error constructor is not shown in the trace
180 function testCallerCensorship() {
185 assertEquals(-1, e.stack.indexOf('at new ReferenceError'),
186 "CallerCensorship contained new ReferenceError");
189 assertTrue(threw, "CallerCensorship didn't throw");
192 // Test that the explicit constructor call is shown in the trace
193 function testUnintendedCallerCensorship() {
197 toString: function () {
202 assertTrue(e.stack.indexOf('at new ReferenceError') != -1,
203 "UnintendedCallerCensorship didn't contain new ReferenceError");
206 assertTrue(threw, "UnintendedCallerCensorship didn't throw");
209 // If an error occurs while the stack trace is being formatted it should
210 // be handled gracefully.
211 function testErrorsDuringFormatting() {
213 Nasty.prototype.foo = function () { throw new RangeError(); };
215 n.__defineGetter__('constructor', function () { CONS_FAIL; });
221 assertTrue(e.stack.indexOf('<error: ReferenceError') != -1,
222 "ErrorsDuringFormatting didn't contain error: ReferenceError");
224 assertTrue(threw, "ErrorsDuringFormatting didn't throw");
226 // Now we can't even format the message saying that we couldn't format
227 // the stack frame. Put that in your pipe and smoke it!
228 ReferenceError.prototype.toString = function () { NESTED_FAIL; };
233 assertTrue(e.stack.indexOf('<error>') != -1,
234 "ErrorsDuringFormatting didn't contain <error>");
236 assertTrue(threw, "ErrorsDuringFormatting didnt' throw (2)");
240 // Poisonous object that throws a reference error if attempted converted to
241 // a primitive values.
242 var thrower = { valueOf: function() { FAIL; },
243 toString: function() { FAIL; } };
245 // Tests that a native constructor function is included in the
247 function testTraceNativeConstructor(nativeFunc) {
248 var nativeFuncName = nativeFunc.name;
250 new nativeFunc(thrower);
251 assertUnreachable(nativeFuncName);
253 assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
257 // Tests that a native conversion function is included in the
259 function testTraceNativeConversion(nativeFunc) {
260 var nativeFuncName = nativeFunc.name;
263 assertUnreachable(nativeFuncName);
265 assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
270 function testOmittedBuiltin(throwing, omitted) {
273 assertUnreachable(omitted);
275 assertTrue(e.stack.indexOf(omitted) < 0, omitted);
280 testTrace("testArrayNative", testArrayNative, ["Array.map (native)"]);
281 testTrace("testNested", testNested, ["at one", "at two", "at three"]);
282 testTrace("testMethodNameInference", testMethodNameInference, ["at Foo.bar"]);
283 testTrace("testImplicitConversion", testImplicitConversion, ["at Nirk.valueOf"]);
284 testTrace("testEval", testEval, ["at Doo (eval at testEval"]);
285 testTrace("testNestedEval", testNestedEval, ["eval at Inner (eval at Outer"]);
286 testTrace("testEvalWithSourceURL", testEvalWithSourceURL,
287 [ "at Doo (res://name:1:18)" ]);
288 testTrace("testNestedEvalWithSourceURL", testNestedEvalWithSourceURL,
289 [" at Inner (res://inner-eval:1:20)",
290 " at Outer (res://outer-eval:1:37)"]);
291 testTrace("testValue", testValue, ["at Number.causeError"]);
292 testTrace("testConstructor", testConstructor, ["new Plonk"]);
293 testTrace("testRenamedMethod", testRenamedMethod, ["Wookie.a$b$c$d [as d]"]);
294 testTrace("testAnonymousMethod", testAnonymousMethod, ["Array.<anonymous>"]);
295 testTrace("testFunctionName", testFunctionName,
296 [" at foo_0 ", " at foo_1", " at foo ", " at boo_3 ", " at foo "]);
297 testTrace("testFunctionInferredName", testFunctionInferredName, [" at f "]);
298 testTrace("testDefaultCustomError", testDefaultCustomError,
299 ["hep-hey", "new CustomError"],
300 ["collectStackTrace"]);
301 testTrace("testStrippedCustomError", testStrippedCustomError, ["hep-hey"],
302 ["new CustomError", "collectStackTrace"]);
303 testTrace("testClassNames", testClassNames,
304 ["new MyObj", "MyObjCreator.Create"], ["as Create"]);
305 testCallerCensorship();
306 testUnintendedCallerCensorship();
307 testErrorsDuringFormatting();
309 testTraceNativeConversion(String); // Does ToString on argument.
310 testTraceNativeConversion(Number); // Does ToNumber on argument.
311 testTraceNativeConversion(RegExp); // Does ToString on argument.
313 testTraceNativeConstructor(String); // Does ToString on argument.
314 testTraceNativeConstructor(Number); // Does ToNumber on argument.
315 testTraceNativeConstructor(RegExp); // Does ToString on argument.
316 testTraceNativeConstructor(Date); // Does ToNumber on argument.
318 // Omitted because QuickSort has builtins object as receiver, and is non-native
320 testOmittedBuiltin(function(){ [thrower, 2].sort(function (a,b) {
321 (b < a) - (a < b); });
324 // Omitted because ADD from runtime.js is non-native builtin.
325 testOmittedBuiltin(function(){ thrower + 2; }, "ADD");
327 var error = new Error();
328 error.toString = function() { assertUnreachable(); };
332 error.name = { toString: function() { assertUnreachable(); }};
333 error.message = { toString: function() { assertUnreachable(); }};
337 Array.prototype.push = function(x) { assertUnreachable(); };
338 Array.prototype.join = function(x) { assertUnreachable(); };
342 error = new Error({ toString: function() { fired = true; } });
347 // Check that throwing exception in a custom stack trace formatting function
348 // does not lead to recursion.
349 Error.prepareStackTrace = function() { throw new Error("abc"); };
361 assertEquals("abc", message);
363 // Test that modifying Error.prepareStackTrace by itself works.
364 Error.prepareStackTrace = function() { Error.prepareStackTrace = "custom"; };
367 assertEquals("custom", Error.prepareStackTrace);
369 // Check that the formatted stack trace can be set to undefined.
371 error.stack = undefined;
372 assertEquals(undefined, error.stack);
374 // Check that the stack trace accessors are not forcibly set.
376 Object.freeze(my_error);
377 assertThrows(function() { Error.captureStackTrace(my_error); });
380 Object.preventExtensions(my_error);
381 assertThrows(function() { Error.captureStackTrace(my_error); });
384 my_error = new Error();
385 var stolen_getter = Object.getOwnPropertyDescriptor(my_error, 'stack').get;
386 Object.defineProperty(fake_error, 'stack', { get: stolen_getter });
387 assertEquals(undefined, fake_error.stack);