deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / stack-traces.js
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
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 function testMethodNameInference() {
29   function Foo() { }
30   Foo.prototype.bar = function () { FAIL; };
31   (new Foo).bar();
32 }
33
34 function testNested() {
35   function one() {
36     function two() {
37       function three() {
38         FAIL;
39       }
40       three();
41     }
42     two();
43   }
44   one();
45 }
46
47 function testArrayNative() {
48   [1, 2, 3].map(function () { FAIL; });
49 }
50
51 function testImplicitConversion() {
52   function Nirk() { }
53   Nirk.prototype.valueOf = function () { FAIL; };
54   return 1 + (new Nirk);
55 }
56
57 function testEval() {
58   eval("function Doo() { FAIL; }; Doo();");
59 }
60
61 function testNestedEval() {
62   var x = "FAIL";
63   eval("function Outer() { eval('function Inner() { eval(x); }'); Inner(); }; Outer();");
64 }
65
66 function testEvalWithSourceURL() {
67   eval("function Doo() { FAIL; }; Doo();\n//# sourceURL=res://name");
68 }
69
70 function testNestedEvalWithSourceURL() {
71   var x = "FAIL";
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");
74 }
75
76 function testValue() {
77   Number.prototype.causeError = function () { FAIL; };
78   (1).causeError();
79 }
80
81 function testConstructor() {
82   function Plonk() { FAIL; }
83   new Plonk();
84 }
85
86 function 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
93 function testAnonymousMethod() {
94   (function () { FAIL }).call([1, 2, 3]);
95 }
96
97 function 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
121 function testFunctionInferredName() {
122   var f = function() {
123     FAIL;
124   }
125   f();
126 }
127
128 function CustomError(message, stripPoint) {
129   this.message = message;
130   Error.captureStackTrace(this, stripPoint);
131 }
132
133 CustomError.prototype.toString = function () {
134   return "CustomError: " + this.message;
135 };
136
137 function testDefaultCustomError() {
138   throw new CustomError("hep-hey", undefined);
139 }
140
141 function testStrippedCustomError() {
142   throw new CustomError("hep-hey", CustomError);
143 }
144
145 MyObj = function() { FAIL; }
146
147 MyObjCreator = function() {}
148
149 MyObjCreator.prototype.Create = function() {
150   return new MyObj();
151 }
152
153 function testClassNames() {
154   (new MyObjCreator).Create();
155 }
156
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) {
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,
166                  name + " doesn't contain expected[" + i + "] stack = " + e.stack);
167     }
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
180 function 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
193 function 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.
211 function testErrorsDuringFormatting() {
212   function Nasty() { }
213   Nasty.prototype.foo = function () { throw new RangeError(); };
214   var n = new Nasty();
215   n.__defineGetter__('constructor', function () { CONS_FAIL; });
216   var threw = false;
217   try {
218     n.foo();
219   } catch (e) {
220     threw = true;
221     assertTrue(e.stack.indexOf('<error: ReferenceError') != -1,
222                "ErrorsDuringFormatting didn't contain error: ReferenceError");
223   }
224   assertTrue(threw, "ErrorsDuringFormatting didn't throw");
225   threw = false;
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; };
229   try {
230     n.foo();
231   } catch (e) {
232     threw = true;
233     assertTrue(e.stack.indexOf('<error>') != -1,
234                "ErrorsDuringFormatting didn't contain <error>");
235   }
236   assertTrue(threw, "ErrorsDuringFormatting didnt' throw (2)");
237 }
238
239
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; } };
244
245 // Tests that a native constructor function is included in the
246 // stack trace.
247 function testTraceNativeConstructor(nativeFunc) {
248   var nativeFuncName = nativeFunc.name;
249   try {
250     new nativeFunc(thrower);
251     assertUnreachable(nativeFuncName);
252   } catch (e) {
253     assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
254   }
255 }
256
257 // Tests that a native conversion function is included in the
258 // stack trace.
259 function testTraceNativeConversion(nativeFunc) {
260   var nativeFuncName = nativeFunc.name;
261   try {
262     nativeFunc(thrower);
263     assertUnreachable(nativeFuncName);
264   } catch (e) {
265     assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
266   }
267 }
268
269
270 function testOmittedBuiltin(throwing, omitted) {
271   try {
272     throwing();
273     assertUnreachable(omitted);
274   } catch (e) {
275     assertTrue(e.stack.indexOf(omitted) < 0, omitted);
276   }
277 }
278
279
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();
308
309 testTraceNativeConversion(String);  // Does ToString on argument.
310 testTraceNativeConversion(Number);  // Does ToNumber on argument.
311 testTraceNativeConversion(RegExp);  // Does ToString on argument.
312
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.
317
318 // Omitted because QuickSort has builtins object as receiver, and is non-native
319 // builtin.
320 testOmittedBuiltin(function(){ [thrower, 2].sort(function (a,b) {
321                                                      (b < a) - (a < b); });
322                    }, "QuickSort");
323
324 // Omitted because ADD from runtime.js is non-native builtin.
325 testOmittedBuiltin(function(){ thrower + 2; }, "ADD");
326
327 var error = new Error();
328 error.toString = function() { assertUnreachable(); };
329 error.stack;
330
331 error = new Error();
332 error.name = { toString: function() { assertUnreachable(); }};
333 error.message = { toString: function() {  assertUnreachable(); }};
334 error.stack;
335
336 error = new Error();
337 Array.prototype.push = function(x) { assertUnreachable(); };
338 Array.prototype.join = function(x) { assertUnreachable(); };
339 error.stack;
340
341 var fired = false;
342 error = new Error({ toString: function() { fired = true; } });
343 assertTrue(fired);
344 error.stack;
345 assertTrue(fired);
346
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"); };
350 var message;
351 try {
352   try {
353     throw new Error();
354   } catch (e) {
355     e.stack;
356   }
357 } catch (e) {
358   message = e.message;
359 }
360
361 assertEquals("abc", message);
362
363 // Test that modifying Error.prepareStackTrace by itself works.
364 Error.prepareStackTrace = function() { Error.prepareStackTrace = "custom"; };
365 new Error().stack;
366
367 assertEquals("custom", Error.prepareStackTrace);
368
369 // Check that the formatted stack trace can be set to undefined.
370 error = new Error();
371 error.stack = undefined;
372 assertEquals(undefined, error.stack);
373
374 // Check that the stack trace accessors are not forcibly set.
375 var my_error = {};
376 Object.freeze(my_error);
377 assertThrows(function() { Error.captureStackTrace(my_error); });
378
379 my_error = {};
380 Object.preventExtensions(my_error);
381 assertThrows(function() { Error.captureStackTrace(my_error); });
382
383 var fake_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);