blob: e457ece3c5527c8b0d3e0369b332e0062a4d8872 [file] [log] [blame]
kasperl@chromium.org2abc4502009-07-02 07:00:29 +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
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000028function 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
66function testValue() {
67 Number.prototype.causeError = function () { FAIL; };
68 (1).causeError();
69}
70
71function testConstructor() {
72 function Plonk() { FAIL; }
73 new Plonk();
74}
75
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000076function testRenamedMethod() {
77 function a$b$c$d() { return FAIL; }
78 function Wookie() { }
79 Wookie.prototype.d = a$b$c$d;
80 (new Wookie).d();
81}
82
83function testAnonymousMethod() {
84 (function () { FAIL }).call([1, 2, 3]);
85}
86
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000087// Utility function for testing that the expected strings occur
88// in the stack trace produced when running the given function.
89function testTrace(fun, expected) {
90 var threw = false;
91 try {
92 fun();
93 } catch (e) {
94 for (var i = 0; i < expected.length; i++) {
95 assertTrue(e.stack.indexOf(expected[i]) != -1);
96 }
97 threw = true;
98 }
99 assertTrue(threw);
100}
101
102// Test that the error constructor is not shown in the trace
103function testCallerCensorship() {
104 var threw = false;
105 try {
106 FAIL;
107 } catch (e) {
108 assertEquals(-1, e.stack.indexOf('at new ReferenceError'));
109 threw = true;
110 }
111 assertTrue(threw);
112}
113
114// Test that the explicit constructor call is shown in the trace
115function testUnintendedCallerCensorship() {
116 var threw = false;
117 try {
118 new ReferenceError({
119 toString: function () {
120 FAIL;
121 }
122 });
123 } catch (e) {
124 assertTrue(e.stack.indexOf('at new ReferenceError') != -1);
125 threw = true;
126 }
127 assertTrue(threw);
128}
129
130// If an error occurs while the stack trace is being formatted it should
131// be handled gracefully.
132function testErrorsDuringFormatting() {
133 function Nasty() { }
134 Nasty.prototype.foo = function () { throw new RangeError(); };
135 var n = new Nasty();
136 n.__defineGetter__('constructor', function () { CONS_FAIL; });
137 var threw = false;
138 try {
139 n.foo();
140 } catch (e) {
141 threw = true;
142 assertTrue(e.stack.indexOf('<error: ReferenceError') != -1);
143 }
144 assertTrue(threw);
145 threw = false;
146 // Now we can't even format the message saying that we couldn't format
147 // the stack frame. Put that in your pipe and smoke it!
148 ReferenceError.prototype.toString = function () { NESTED_FAIL; };
149 try {
150 n.foo();
151 } catch (e) {
152 threw = true;
153 assertTrue(e.stack.indexOf('<error>') != -1);
154 }
155 assertTrue(threw);
156}
157
158testTrace(testArrayNative, ["Array.map (native)"]);
159testTrace(testNested, ["at one", "at two", "at three"]);
160testTrace(testMethodNameInference, ["at Foo.bar"]);
161testTrace(testImplicitConversion, ["at Nirk.valueOf"]);
162testTrace(testEval, ["at Doo (eval at testEval"]);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000163testTrace(testNestedEval, ["eval at Inner (eval at Outer"]);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000164testTrace(testValue, ["at Number.causeError"]);
165testTrace(testConstructor, ["new Plonk"]);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000166testTrace(testRenamedMethod, ["Wookie.a$b$c$d [as d]"]);
167testTrace(testAnonymousMethod, ["Array.<anonymous>"]);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000168
169testCallerCensorship();
170testUnintendedCallerCensorship();
171testErrorsDuringFormatting();