blob: 41de1463461260d4b1f7b288df178af8acbfd038 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
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
28function testMethodNameInference() {
29 function Foo() { }
30 Foo.prototype.bar = function () { FAIL; };
31 (new Foo).bar();
32}
33
34function testNested() {
35 function one() {
36 function two() {
37 function three() {
38 FAIL;
39 }
40 three();
41 }
42 two();
43 }
44 one();
45}
46
47function testArrayNative() {
48 [1, 2, 3].map(function () { FAIL; });
49}
50
51function testImplicitConversion() {
52 function Nirk() { }
53 Nirk.prototype.valueOf = function () { FAIL; };
54 return 1 + (new Nirk);
55}
56
57function testEval() {
58 eval("function Doo() { FAIL; }; Doo();");
59}
60
61function testNestedEval() {
62 var x = "FAIL";
63 eval("function Outer() { eval('function Inner() { eval(x); }'); Inner(); }; Outer();");
64}
65
Kristian Monsen0d5e1162010-09-30 15:31:59 +010066function testEvalWithSourceURL() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 eval("function Doo() { FAIL; }; Doo();\n//# sourceURL=res://name");
Kristian Monsen0d5e1162010-09-30 15:31:59 +010068}
69
70function testNestedEvalWithSourceURL() {
71 var x = "FAIL";
72 var innerEval = 'function Inner() { eval(x); }\n//@ sourceURL=res://inner-eval';
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 eval("function Outer() { eval(innerEval); Inner(); }; Outer();\n//# sourceURL=res://outer-eval");
Kristian Monsen0d5e1162010-09-30 15:31:59 +010074}
75
Steve Blocka7e24c12009-10-30 11:49:00 +000076function testValue() {
77 Number.prototype.causeError = function () { FAIL; };
78 (1).causeError();
79}
80
81function testConstructor() {
82 function Plonk() { FAIL; }
83 new Plonk();
84}
85
86function testRenamedMethod() {
87 function a$b$c$d() { return FAIL; }
88 function Wookie() { }
89 Wookie.prototype.d = a$b$c$d;
90 (new Wookie).d();
91}
92
93function testAnonymousMethod() {
94 (function () { FAIL }).call([1, 2, 3]);
95}
96
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097function testFunctionName() {
98 function gen(name, counter) {
99 var f = function foo() {
100 if (counter === 0) {
101 FAIL;
102 }
103 gen(name, counter - 1)();
104 }
105 if (counter === 4) {
106 Object.defineProperty(f, 'name', {get: function(){ throw 239; }});
107 } else if (counter == 3) {
108 Object.defineProperty(f, 'name', {value: 'boo' + '_' + counter});
109 } else {
110 Object.defineProperty(f, 'name', {writable: true});
111 if (counter === 2)
112 f.name = 42;
113 else
114 f.name = name + '_' + counter;
115 }
116 return f;
117 }
118 gen('foo', 4)();
119}
120
121function testFunctionInferredName() {
122 var f = function() {
123 FAIL;
124 }
125 f();
126}
127
Steve Blocka7e24c12009-10-30 11:49:00 +0000128function CustomError(message, stripPoint) {
129 this.message = message;
130 Error.captureStackTrace(this, stripPoint);
131}
132
133CustomError.prototype.toString = function () {
134 return "CustomError: " + this.message;
135};
136
137function testDefaultCustomError() {
138 throw new CustomError("hep-hey", undefined);
139}
140
141function testStrippedCustomError() {
142 throw new CustomError("hep-hey", CustomError);
143}
144
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145MyObj = function() { FAIL; }
146
147MyObjCreator = function() {}
148
149MyObjCreator.prototype.Create = function() {
150 return new MyObj();
151}
152
153function testClassNames() {
154 (new MyObjCreator).Create();
155}
156
Steve Blocka7e24c12009-10-30 11:49:00 +0000157// Utility function for testing that the expected strings occur
158// in the stack trace produced when running the given function.
159function testTrace(name, fun, expected, unexpected) {
160 var threw = false;
161 try {
162 fun();
163 } catch (e) {
164 for (var i = 0; i < expected.length; i++) {
165 assertTrue(e.stack.indexOf(expected[i]) != -1,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100166 name + " doesn't contain expected[" + i + "] stack = " + e.stack);
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 }
168 if (unexpected) {
169 for (var i = 0; i < unexpected.length; i++) {
170 assertEquals(e.stack.indexOf(unexpected[i]), -1,
171 name + " contains unexpected[" + i + "]");
172 }
173 }
174 threw = true;
175 }
176 assertTrue(threw, name + " didn't throw");
177}
178
179// Test that the error constructor is not shown in the trace
180function testCallerCensorship() {
181 var threw = false;
182 try {
183 FAIL;
184 } catch (e) {
185 assertEquals(-1, e.stack.indexOf('at new ReferenceError'),
186 "CallerCensorship contained new ReferenceError");
187 threw = true;
188 }
189 assertTrue(threw, "CallerCensorship didn't throw");
190}
191
192// Test that the explicit constructor call is shown in the trace
193function testUnintendedCallerCensorship() {
194 var threw = false;
195 try {
196 new ReferenceError({
197 toString: function () {
198 FAIL;
199 }
200 });
201 } catch (e) {
202 assertTrue(e.stack.indexOf('at new ReferenceError') != -1,
203 "UnintendedCallerCensorship didn't contain new ReferenceError");
204 threw = true;
205 }
206 assertTrue(threw, "UnintendedCallerCensorship didn't throw");
207}
208
209// If an error occurs while the stack trace is being formatted it should
210// be handled gracefully.
211function testErrorsDuringFormatting() {
212 function Nasty() { }
213 Nasty.prototype.foo = function () { throw new RangeError(); };
214 var n = new Nasty();
215 n.__defineGetter__('constructor', function () { CONS_FAIL; });
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 assertThrows(()=>n.foo(), RangeError);
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 // Now we can't even format the message saying that we couldn't format
218 // the stack frame. Put that in your pipe and smoke it!
219 ReferenceError.prototype.toString = function () { NESTED_FAIL; };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220 assertThrows(()=>n.foo(), RangeError);
Steve Blocka7e24c12009-10-30 11:49:00 +0000221}
222
223
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100224// Poisonous object that throws a reference error if attempted converted to
225// a primitive values.
226var thrower = { valueOf: function() { FAIL; },
227 toString: function() { FAIL; } };
228
229// Tests that a native constructor function is included in the
230// stack trace.
231function testTraceNativeConstructor(nativeFunc) {
232 var nativeFuncName = nativeFunc.name;
233 try {
234 new nativeFunc(thrower);
235 assertUnreachable(nativeFuncName);
236 } catch (e) {
237 assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
238 }
239}
240
241// Tests that a native conversion function is included in the
242// stack trace.
243function testTraceNativeConversion(nativeFunc) {
244 var nativeFuncName = nativeFunc.name;
245 try {
246 nativeFunc(thrower);
247 assertUnreachable(nativeFuncName);
248 } catch (e) {
249 assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
250 }
251}
252
253
254function testOmittedBuiltin(throwing, omitted) {
255 try {
256 throwing();
257 assertUnreachable(omitted);
258 } catch (e) {
259 assertTrue(e.stack.indexOf(omitted) < 0, omitted);
260 }
261}
262
263
Steve Blocka7e24c12009-10-30 11:49:00 +0000264testTrace("testArrayNative", testArrayNative, ["Array.map (native)"]);
265testTrace("testNested", testNested, ["at one", "at two", "at three"]);
266testTrace("testMethodNameInference", testMethodNameInference, ["at Foo.bar"]);
267testTrace("testImplicitConversion", testImplicitConversion, ["at Nirk.valueOf"]);
268testTrace("testEval", testEval, ["at Doo (eval at testEval"]);
269testTrace("testNestedEval", testNestedEval, ["eval at Inner (eval at Outer"]);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100270testTrace("testEvalWithSourceURL", testEvalWithSourceURL,
271 [ "at Doo (res://name:1:18)" ]);
272testTrace("testNestedEvalWithSourceURL", testNestedEvalWithSourceURL,
273 [" at Inner (res://inner-eval:1:20)",
274 " at Outer (res://outer-eval:1:37)"]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000275testTrace("testValue", testValue, ["at Number.causeError"]);
276testTrace("testConstructor", testConstructor, ["new Plonk"]);
277testTrace("testRenamedMethod", testRenamedMethod, ["Wookie.a$b$c$d [as d]"]);
278testTrace("testAnonymousMethod", testAnonymousMethod, ["Array.<anonymous>"]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000279testTrace("testFunctionName", testFunctionName,
280 [" at foo_0 ", " at foo_1", " at foo ", " at boo_3 ", " at foo "]);
281testTrace("testFunctionInferredName", testFunctionInferredName, [" at f "]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000282testTrace("testDefaultCustomError", testDefaultCustomError,
283 ["hep-hey", "new CustomError"],
284 ["collectStackTrace"]);
285testTrace("testStrippedCustomError", testStrippedCustomError, ["hep-hey"],
286 ["new CustomError", "collectStackTrace"]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287testTrace("testClassNames", testClassNames,
288 ["new MyObj", "MyObjCreator.Create"], ["as Create"]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289testCallerCensorship();
290testUnintendedCallerCensorship();
291testErrorsDuringFormatting();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100292
293testTraceNativeConversion(String); // Does ToString on argument.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100294testTraceNativeConversion(RegExp); // Does ToString on argument.
295
296testTraceNativeConstructor(String); // Does ToString on argument.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100297testTraceNativeConstructor(RegExp); // Does ToString on argument.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100298
299// Omitted because QuickSort has builtins object as receiver, and is non-native
300// builtin.
301testOmittedBuiltin(function(){ [thrower, 2].sort(function (a,b) {
302 (b < a) - (a < b); });
303 }, "QuickSort");
304
305// Omitted because ADD from runtime.js is non-native builtin.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306testOmittedBuiltin(function(){ thrower + 2; }, "ADD");
307
308var error = new Error();
309error.toString = function() { assertUnreachable(); };
310error.stack;
311
312error = new Error();
313error.name = { toString: function() { assertUnreachable(); }};
314error.message = { toString: function() { assertUnreachable(); }};
315error.stack;
316
317error = new Error();
318Array.prototype.push = function(x) { assertUnreachable(); };
319Array.prototype.join = function(x) { assertUnreachable(); };
320error.stack;
321
322var fired = false;
323error = new Error({ toString: function() { fired = true; } });
324assertTrue(fired);
325error.stack;
326assertTrue(fired);
327
328// Check that throwing exception in a custom stack trace formatting function
329// does not lead to recursion.
330Error.prepareStackTrace = function() { throw new Error("abc"); };
331var message;
332try {
333 try {
334 throw new Error();
335 } catch (e) {
336 e.stack;
337 }
338} catch (e) {
339 message = e.message;
340}
341
342assertEquals("abc", message);
343
344// Test that modifying Error.prepareStackTrace by itself works.
345Error.prepareStackTrace = function() { Error.prepareStackTrace = "custom"; };
346new Error().stack;
347
348assertEquals("custom", Error.prepareStackTrace);
349
350// Check that the formatted stack trace can be set to undefined.
351error = new Error();
352error.stack = undefined;
353assertEquals(undefined, error.stack);
354
355// Check that the stack trace accessors are not forcibly set.
356var my_error = {};
357Object.freeze(my_error);
358assertThrows(function() { Error.captureStackTrace(my_error); });
359
360my_error = {};
361Object.preventExtensions(my_error);
362assertThrows(function() { Error.captureStackTrace(my_error); });
363
364var fake_error = {};
365my_error = new Error();
366var stolen_getter = Object.getOwnPropertyDescriptor(my_error, 'stack').get;
367Object.defineProperty(fake_error, 'stack', { get: stolen_getter });
368assertEquals(undefined, fake_error.stack);