1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 * SUMMARY: Testing that Error.stack distinguishes between:
11 * A) top-level calls: myFunc();
12 * B) no-name function calls: function() { myFunc();} ()
14 * The stack frame for A) should begin with '@'
15 * The stack frame for B) should begin with '()'
17 * This behavior was coded by Brendan during his fix for bug 127136.
18 * See http://bugzilla.mozilla.org/show_bug.cgi?id=127136#c13
20 * Note: our function getStackFrames(err) orders the array of stack frames
21 * so that the 0th element will correspond to the highest frame, i.e. will
22 * correspond to a line in top-level code. The 1st element will correspond
23 * to the function that is called first, and so on...
25 * NOTE: At present Rhino does not have an Error.stack property. It is an
26 * ECMA extension, see http://bugzilla.mozilla.org/show_bug.cgi?id=123177
28 //-----------------------------------------------------------------------------
30 var BUGNUMBER = '(none)';
31 var summary = 'Testing Error.stack';
35 var actualvalues = [];
37 var expectedvalues = [];
61 throw new Error('meep!');
71 stackFrames = getStackFrames(myErr);
72 status = inSection(1);
73 actual = stackFrames[0].substring(0,1);
77 status = inSection(2);
78 actual = stackFrames[1].substring(0,2);
82 status = inSection(3);
83 actual = stackFrames[2].substring(0,2);
87 status = inSection(4);
88 actual = stackFrames[3].substring(0,2);
92 status = inSection(5);
93 actual = stackFrames[4].substring(0,2);
99 myErr = A('44:foo','13:bar');
100 stackFrames = getStackFrames(myErr);
101 status = inSection(6);
102 actual = stackFrames[0].substring(0,1);
106 status = inSection(7);
107 actual = stackFrames[1].substring(0,2);
111 status = inSection(8);
112 actual = stackFrames[2].substring(0,2);
116 status = inSection(9);
117 actual = stackFrames[3].substring(0,2);
121 status = inSection(10);
122 actual = stackFrames[4].substring(0,2);
129 * Make the first frame occur in a function with an empty name -
131 myErr = function() { return A(44,13); } ();
132 stackFrames = getStackFrames(myErr);
133 status = inSection(11);
134 actual = stackFrames[0].substring(0,1);
138 status = inSection(12);
139 actual = stackFrames[1].substring(0,7);
143 status = inSection(13);
144 actual = stackFrames[2].substring(0,2);
148 // etc. for the rest of the frames as above
153 * Make the first frame occur in a function with name 'anonymous' -
155 var f = Function('return A(44,13);');
157 stackFrames = getStackFrames(myErr);
158 status = inSection(14);
159 actual = stackFrames[0].substring(0,1);
163 status = inSection(15);
164 actual = stackFrames[1].substring(0,10);
165 expect = 'anonymous@';
168 status = inSection(16);
169 actual = stackFrames[2].substring(0,2);
173 // etc. for the rest of the frames as above
178 * Make a user-defined error via the Error() function -
180 var message = 'Hi there!'; var fileName = 'file name'; var lineNumber = 0;
181 myErr = Error(message, fileName, lineNumber);
182 stackFrames = getStackFrames(myErr);
183 status = inSection(17);
184 actual = stackFrames[0].substring(0,1);
190 * Now use the |new| keyword. Re-use the same params -
192 myErr = new Error(message, fileName, lineNumber);
193 stackFrames = getStackFrames(myErr);
194 status = inSection(18);
195 actual = stackFrames[0].substring(0,1);
202 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
209 * Split the string |err.stack| along its '\n' delimiter.
210 * As of 2002-02-28 |err.stack| ends with the delimiter, so
211 * the resulting array has an empty string as its last element.
213 * Pop that useless element off before doing anything.
214 * Then reverse the array, for convenience of indexing -
216 function getStackFrames(err)
218 var arr = err.stack.split('\n');
220 return arr.reverse();
226 statusitems[UBound] = status;
227 actualvalues[UBound] = actual;
228 expectedvalues[UBound] = expect;
235 printBugNumber(BUGNUMBER);
236 printStatus(summary);
238 for (var i=0; i<UBound; i++)
240 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);