| 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/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
* |
| 8 |
* Date: 28 Feb 2002 |
| 9 |
* SUMMARY: Testing that Error.stack distinguishes between: |
| 10 |
* |
| 11 |
* A) top-level calls: myFunc(); |
| 12 |
* B) no-name function calls: function() { myFunc();} () |
| 13 |
* |
| 14 |
* The stack frame for A) should begin with '@' |
| 15 |
* The stack frame for B) should begin with '()' |
| 16 |
* |
| 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 |
| 19 |
* |
| 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... |
| 24 |
* |
| 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 |
| 27 |
*/ |
| 28 |
//----------------------------------------------------------------------------- |
| 29 |
var UBound = 0; |
| 30 |
var BUGNUMBER = '(none)'; |
| 31 |
var summary = 'Testing Error.stack'; |
| 32 |
var status = ''; |
| 33 |
var statusitems = []; |
| 34 |
var actual = ''; |
| 35 |
var actualvalues = []; |
| 36 |
var expect= ''; |
| 37 |
var expectedvalues = []; |
| 38 |
var myErr = ''; |
| 39 |
var stackFrames = ''; |
| 40 |
|
| 41 |
|
| 42 |
function A(x,y) |
| 43 |
{ |
| 44 |
return B(x+1,y+1); |
| 45 |
} |
| 46 |
|
| 47 |
function B(x,z) |
| 48 |
{ |
| 49 |
return C(x+1,z+1); |
| 50 |
} |
| 51 |
|
| 52 |
function C(x,y) |
| 53 |
{ |
| 54 |
return D(x+1,y+1); |
| 55 |
} |
| 56 |
|
| 57 |
function D(x,z) |
| 58 |
{ |
| 59 |
try |
| 60 |
{ |
| 61 |
throw new Error('meep!'); |
| 62 |
} |
| 63 |
catch (e) |
| 64 |
{ |
| 65 |
return e; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
myErr = A(44,13); |
| 71 |
stackFrames = getStackFrames(myErr); |
| 72 |
status = inSection(1); |
| 73 |
actual = stackFrames[0].substring(0,1); |
| 74 |
expect = '@'; |
| 75 |
addThis(); |
| 76 |
|
| 77 |
status = inSection(2); |
| 78 |
actual = stackFrames[1].substring(0,2); |
| 79 |
expect = 'A@'; |
| 80 |
addThis(); |
| 81 |
|
| 82 |
status = inSection(3); |
| 83 |
actual = stackFrames[2].substring(0,2); |
| 84 |
expect = 'B@'; |
| 85 |
addThis(); |
| 86 |
|
| 87 |
status = inSection(4); |
| 88 |
actual = stackFrames[3].substring(0,2); |
| 89 |
expect = 'C@'; |
| 90 |
addThis(); |
| 91 |
|
| 92 |
status = inSection(5); |
| 93 |
actual = stackFrames[4].substring(0,2); |
| 94 |
expect = 'D@'; |
| 95 |
addThis(); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
myErr = A('44:foo','13:bar'); |
| 100 |
stackFrames = getStackFrames(myErr); |
| 101 |
status = inSection(6); |
| 102 |
actual = stackFrames[0].substring(0,1); |
| 103 |
expect = '@'; |
| 104 |
addThis(); |
| 105 |
|
| 106 |
status = inSection(7); |
| 107 |
actual = stackFrames[1].substring(0,2); |
| 108 |
expect = 'A@'; |
| 109 |
addThis(); |
| 110 |
|
| 111 |
status = inSection(8); |
| 112 |
actual = stackFrames[2].substring(0,2); |
| 113 |
expect = 'B@'; |
| 114 |
addThis(); |
| 115 |
|
| 116 |
status = inSection(9); |
| 117 |
actual = stackFrames[3].substring(0,2); |
| 118 |
expect = 'C@'; |
| 119 |
addThis(); |
| 120 |
|
| 121 |
status = inSection(10); |
| 122 |
actual = stackFrames[4].substring(0,2); |
| 123 |
expect = 'D@';; |
| 124 |
addThis(); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
/* |
| 129 |
* Make the first frame occur in a function with an empty name - |
| 130 |
*/ |
| 131 |
myErr = function() { return A(44,13); } (); |
| 132 |
stackFrames = getStackFrames(myErr); |
| 133 |
status = inSection(11); |
| 134 |
actual = stackFrames[0].substring(0,1); |
| 135 |
expect = '@'; |
| 136 |
addThis(); |
| 137 |
|
| 138 |
status = inSection(12); |
| 139 |
actual = stackFrames[1].substring(0,7); |
| 140 |
expect = 'myErr<@'; |
| 141 |
addThis(); |
| 142 |
|
| 143 |
status = inSection(13); |
| 144 |
actual = stackFrames[2].substring(0,2); |
| 145 |
expect = 'A@'; |
| 146 |
addThis(); |
| 147 |
|
| 148 |
// etc. for the rest of the frames as above |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
/* |
| 153 |
* Make the first frame occur in a function with name 'anonymous' - |
| 154 |
*/ |
| 155 |
var f = Function('return A(44,13);'); |
| 156 |
myErr = f(); |
| 157 |
stackFrames = getStackFrames(myErr); |
| 158 |
status = inSection(14); |
| 159 |
actual = stackFrames[0].substring(0,1); |
| 160 |
expect = '@'; |
| 161 |
addThis(); |
| 162 |
|
| 163 |
status = inSection(15); |
| 164 |
actual = stackFrames[1].substring(0,10); |
| 165 |
expect = 'anonymous@'; |
| 166 |
addThis(); |
| 167 |
|
| 168 |
status = inSection(16); |
| 169 |
actual = stackFrames[2].substring(0,2); |
| 170 |
expect = 'A@'; |
| 171 |
addThis(); |
| 172 |
|
| 173 |
// etc. for the rest of the frames as above |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/* |
| 178 |
* Make a user-defined error via the Error() function - |
| 179 |
*/ |
| 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); |
| 185 |
expect = '@'; |
| 186 |
addThis(); |
| 187 |
|
| 188 |
|
| 189 |
/* |
| 190 |
* Now use the |new| keyword. Re-use the same params - |
| 191 |
*/ |
| 192 |
myErr = new Error(message, fileName, lineNumber); |
| 193 |
stackFrames = getStackFrames(myErr); |
| 194 |
status = inSection(18); |
| 195 |
actual = stackFrames[0].substring(0,1); |
| 196 |
expect = '@'; |
| 197 |
addThis(); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
//----------------------------------------------------------------------------- |
| 203 |
test(); |
| 204 |
//----------------------------------------------------------------------------- |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
/* |
| 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. |
| 212 |
* |
| 213 |
* Pop that useless element off before doing anything. |
| 214 |
* Then reverse the array, for convenience of indexing - |
| 215 |
*/ |
| 216 |
function getStackFrames(err) |
| 217 |
{ |
| 218 |
var arr = err.stack.split('\n'); |
| 219 |
arr.pop(); |
| 220 |
return arr.reverse(); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
function addThis() |
| 225 |
{ |
| 226 |
statusitems[UBound] = status; |
| 227 |
actualvalues[UBound] = actual; |
| 228 |
expectedvalues[UBound] = expect; |
| 229 |
UBound++; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
function test() |
| 234 |
{ |
| 235 |
printBugNumber(BUGNUMBER); |
| 236 |
printStatus(summary); |
| 237 |
|
| 238 |
for (var i=0; i<UBound; i++) |
| 239 |
{ |
| 240 |
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 241 |
} |
| 242 |
} |