103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Copyright 2009 the V8 project authors. All rights reserved.
203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// modification, are permitted provided that the following conditions are
403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// met:
503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//
603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//       notice, this list of conditions and the following disclaimer.
803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//       copyright notice, this list of conditions and the following
1003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//       disclaimer in the documentation and/or other materials provided
1103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//       with the distribution.
1203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
1303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//       contributors may be used to endorse or promote products derived
1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//       from this software without specific prior written permission.
1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)//
1603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)function testMethodNameInference() {
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  function Foo() { }
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Foo.prototype.bar = function () { FAIL; };
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  (new Foo).bar();
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccifunction testNested() {
3503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  function one() {
3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    function two() {
3703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      function three() {
3803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        FAIL;
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      }
4003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      three();
4103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    two();
4303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
4403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  one();
4503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
4603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
4703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)function testArrayNative() {
4803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  [1, 2, 3].map(function () { FAIL; });
4903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
5003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
5103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)function testImplicitConversion() {
5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  function Nirk() { }
5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  Nirk.prototype.valueOf = function () { FAIL; };
5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return 1 + (new Nirk);
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
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
66function testEvalWithSourceURL() {
67  eval("function Doo() { FAIL; }; Doo();\n//# sourceURL=res://name");
68}
69
70function 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
76function 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
97function CustomError(message, stripPoint) {
98  this.message = message;
99  Error.captureStackTrace(this, stripPoint);
100}
101
102CustomError.prototype.toString = function () {
103  return "CustomError: " + this.message;
104};
105
106function testDefaultCustomError() {
107  throw new CustomError("hep-hey", undefined);
108}
109
110function testStrippedCustomError() {
111  throw new CustomError("hep-hey", CustomError);
112}
113
114MyObj = function() { FAIL; }
115
116MyObjCreator = function() {}
117
118MyObjCreator.prototype.Create = function() {
119  return new MyObj();
120}
121
122function testClassNames() {
123  (new MyObjCreator).Create();
124}
125
126// Utility function for testing that the expected strings occur
127// in the stack trace produced when running the given function.
128function testTrace(name, fun, expected, unexpected) {
129  var threw = false;
130  try {
131    fun();
132  } catch (e) {
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);
136    }
137    if (unexpected) {
138      for (var i = 0; i < unexpected.length; i++) {
139        assertEquals(e.stack.indexOf(unexpected[i]), -1,
140                     name + " contains unexpected[" + i + "]");
141      }
142    }
143    threw = true;
144  }
145  assertTrue(threw, name + " didn't throw");
146}
147
148// Test that the error constructor is not shown in the trace
149function testCallerCensorship() {
150  var threw = false;
151  try {
152    FAIL;
153  } catch (e) {
154    assertEquals(-1, e.stack.indexOf('at new ReferenceError'),
155                 "CallerCensorship contained new ReferenceError");
156    threw = true;
157  }
158  assertTrue(threw, "CallerCensorship didn't throw");
159}
160
161// Test that the explicit constructor call is shown in the trace
162function testUnintendedCallerCensorship() {
163  var threw = false;
164  try {
165    new ReferenceError({
166      toString: function () {
167        FAIL;
168      }
169    });
170  } catch (e) {
171    assertTrue(e.stack.indexOf('at new ReferenceError') != -1,
172               "UnintendedCallerCensorship didn't contain new ReferenceError");
173    threw = true;
174  }
175  assertTrue(threw, "UnintendedCallerCensorship didn't throw");
176}
177
178// If an error occurs while the stack trace is being formatted it should
179// be handled gracefully.
180function testErrorsDuringFormatting() {
181  function Nasty() { }
182  Nasty.prototype.foo = function () { throw new RangeError(); };
183  var n = new Nasty();
184  n.__defineGetter__('constructor', function () { CONS_FAIL; });
185  var threw = false;
186  try {
187    n.foo();
188  } catch (e) {
189    threw = true;
190    assertTrue(e.stack.indexOf('<error: ReferenceError') != -1,
191               "ErrorsDuringFormatting didn't contain error: ReferenceError");
192  }
193  assertTrue(threw, "ErrorsDuringFormatting didn't throw");
194  threw = false;
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; };
198  try {
199    n.foo();
200  } catch (e) {
201    threw = true;
202    assertTrue(e.stack.indexOf('<error>') != -1,
203               "ErrorsDuringFormatting didn't contain <error>");
204  }
205  assertTrue(threw, "ErrorsDuringFormatting didnt' throw (2)");
206}
207
208
209// Poisonous object that throws a reference error if attempted converted to
210// a primitive values.
211var thrower = { valueOf: function() { FAIL; },
212                toString: function() { FAIL; } };
213
214// Tests that a native constructor function is included in the
215// stack trace.
216function testTraceNativeConstructor(nativeFunc) {
217  var nativeFuncName = nativeFunc.name;
218  try {
219    new nativeFunc(thrower);
220    assertUnreachable(nativeFuncName);
221  } catch (e) {
222    assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
223  }
224}
225
226// Tests that a native conversion function is included in the
227// stack trace.
228function testTraceNativeConversion(nativeFunc) {
229  var nativeFuncName = nativeFunc.name;
230  try {
231    nativeFunc(thrower);
232    assertUnreachable(nativeFuncName);
233  } catch (e) {
234    assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName);
235  }
236}
237
238
239function testOmittedBuiltin(throwing, omitted) {
240  try {
241    throwing();
242    assertUnreachable(omitted);
243  } catch (e) {
244    assertTrue(e.stack.indexOf(omitted) < 0, omitted);
245  }
246}
247
248
249testTrace("testArrayNative", testArrayNative, ["Array.map (native)"]);
250testTrace("testNested", testNested, ["at one", "at two", "at three"]);
251testTrace("testMethodNameInference", testMethodNameInference, ["at Foo.bar"]);
252testTrace("testImplicitConversion", testImplicitConversion, ["at Nirk.valueOf"]);
253testTrace("testEval", testEval, ["at Doo (eval at testEval"]);
254testTrace("testNestedEval", testNestedEval, ["eval at Inner (eval at Outer"]);
255testTrace("testEvalWithSourceURL", testEvalWithSourceURL,
256    [ "at Doo (res://name:1:18)" ]);
257testTrace("testNestedEvalWithSourceURL", testNestedEvalWithSourceURL,
258    [" at Inner (res://inner-eval:1:20)",
259     " at Outer (res://outer-eval:1:37)"]);
260testTrace("testValue", testValue, ["at Number.causeError"]);
261testTrace("testConstructor", testConstructor, ["new Plonk"]);
262testTrace("testRenamedMethod", testRenamedMethod, ["Wookie.a$b$c$d [as d]"]);
263testTrace("testAnonymousMethod", testAnonymousMethod, ["Array.<anonymous>"]);
264testTrace("testDefaultCustomError", testDefaultCustomError,
265    ["hep-hey", "new CustomError"],
266    ["collectStackTrace"]);
267testTrace("testStrippedCustomError", testStrippedCustomError, ["hep-hey"],
268    ["new CustomError", "collectStackTrace"]);
269testTrace("testClassNames", testClassNames,
270          ["new MyObj", "MyObjCreator.Create"], ["as Create"]);
271testCallerCensorship();
272testUnintendedCallerCensorship();
273testErrorsDuringFormatting();
274
275testTraceNativeConversion(String);  // Does ToString on argument.
276testTraceNativeConversion(Number);  // Does ToNumber on argument.
277testTraceNativeConversion(RegExp);  // Does ToString on argument.
278
279testTraceNativeConstructor(String);  // Does ToString on argument.
280testTraceNativeConstructor(Number);  // Does ToNumber on argument.
281testTraceNativeConstructor(RegExp);  // Does ToString on argument.
282testTraceNativeConstructor(Date);    // Does ToNumber on argument.
283
284// Omitted because QuickSort has builtins object as receiver, and is non-native
285// builtin.
286testOmittedBuiltin(function(){ [thrower, 2].sort(function (a,b) {
287                                                     (b < a) - (a < b); });
288                   }, "QuickSort");
289
290// Omitted because ADD from runtime.js is non-native builtin.
291testOmittedBuiltin(function(){ thrower + 2; }, "ADD");
292
293var error = new Error();
294error.toString = function() { assertUnreachable(); };
295error.stack;
296
297error = new Error();
298error.name = { toString: function() { assertUnreachable(); }};
299error.message = { toString: function() {  assertUnreachable(); }};
300error.stack;
301
302error = new Error();
303Array.prototype.push = function(x) { assertUnreachable(); };
304Array.prototype.join = function(x) { assertUnreachable(); };
305error.stack;
306
307var fired = false;
308error = new Error({ toString: function() { fired = true; } });
309assertTrue(fired);
310error.stack;
311assertTrue(fired);
312
313// Check that throwing exception in a custom stack trace formatting function
314// does not lead to recursion.
315Error.prepareStackTrace = function() { throw new Error("abc"); };
316var message;
317try {
318  try {
319    throw new Error();
320  } catch (e) {
321    e.stack;
322  }
323} catch (e) {
324  message = e.message;
325}
326
327assertEquals("abc", message);
328
329// Test that modifying Error.prepareStackTrace by itself works.
330Error.prepareStackTrace = function() { Error.prepareStackTrace = "custom"; };
331new Error().stack;
332
333assertEquals("custom", Error.prepareStackTrace);
334
335// Check that the formatted stack trace can be set to undefined.
336error = new Error();
337error.stack = undefined;
338assertEquals(undefined, error.stack);
339
340// Check that the stack trace accessors are not forcibly set.
341var my_error = {};
342Object.freeze(my_error);
343assertThrows(function() { Error.captureStackTrace(my_error); });
344
345my_error = {};
346Object.preventExtensions(my_error);
347assertThrows(function() { Error.captureStackTrace(my_error); });
348
349var fake_error = {};
350my_error = new Error();
351var stolen_getter = Object.getOwnPropertyDescriptor(my_error, 'stack').get;
352Object.defineProperty(fake_error, 'stack', { get: stolen_getter });
353assertEquals(undefined, fake_error.stack);
354