1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_5/Exceptions/errstack-001.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,245 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * 1.11 + * Date: 28 Feb 2002 1.12 + * SUMMARY: Testing that Error.stack distinguishes between: 1.13 + * 1.14 + * A) top-level calls: myFunc(); 1.15 + * B) no-name function calls: function() { myFunc();} () 1.16 + * 1.17 + * The stack frame for A) should begin with '@' 1.18 + * The stack frame for B) should begin with '()' 1.19 + * 1.20 + * This behavior was coded by Brendan during his fix for bug 127136. 1.21 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=127136#c13 1.22 + * 1.23 + * Note: our function getStackFrames(err) orders the array of stack frames 1.24 + * so that the 0th element will correspond to the highest frame, i.e. will 1.25 + * correspond to a line in top-level code. The 1st element will correspond 1.26 + * to the function that is called first, and so on... 1.27 + * 1.28 + * NOTE: At present Rhino does not have an Error.stack property. It is an 1.29 + * ECMA extension, see http://bugzilla.mozilla.org/show_bug.cgi?id=123177 1.30 + */ 1.31 +//----------------------------------------------------------------------------- 1.32 +var UBound = 0; 1.33 +var BUGNUMBER = '(none)'; 1.34 +var summary = 'Testing Error.stack'; 1.35 +var status = ''; 1.36 +var statusitems = []; 1.37 +var actual = ''; 1.38 +var actualvalues = []; 1.39 +var expect= ''; 1.40 +var expectedvalues = []; 1.41 +var myErr = ''; 1.42 +var stackFrames = ''; 1.43 + 1.44 + 1.45 +function A(x,y) 1.46 +{ 1.47 + return B(x+1,y+1); 1.48 +} 1.49 + 1.50 +function B(x,z) 1.51 +{ 1.52 + return C(x+1,z+1); 1.53 +} 1.54 + 1.55 +function C(x,y) 1.56 +{ 1.57 + return D(x+1,y+1); 1.58 +} 1.59 + 1.60 +function D(x,z) 1.61 +{ 1.62 + try 1.63 + { 1.64 + throw new Error('meep!'); 1.65 + } 1.66 + catch (e) 1.67 + { 1.68 + return e; 1.69 + } 1.70 +} 1.71 + 1.72 + 1.73 +myErr = A(44,13); 1.74 +stackFrames = getStackFrames(myErr); 1.75 +status = inSection(1); 1.76 +actual = stackFrames[0].substring(0,1); 1.77 +expect = '@'; 1.78 +addThis(); 1.79 + 1.80 +status = inSection(2); 1.81 +actual = stackFrames[1].substring(0,2); 1.82 +expect = 'A@'; 1.83 +addThis(); 1.84 + 1.85 +status = inSection(3); 1.86 +actual = stackFrames[2].substring(0,2); 1.87 +expect = 'B@'; 1.88 +addThis(); 1.89 + 1.90 +status = inSection(4); 1.91 +actual = stackFrames[3].substring(0,2); 1.92 +expect = 'C@'; 1.93 +addThis(); 1.94 + 1.95 +status = inSection(5); 1.96 +actual = stackFrames[4].substring(0,2); 1.97 +expect = 'D@'; 1.98 +addThis(); 1.99 + 1.100 + 1.101 + 1.102 +myErr = A('44:foo','13:bar'); 1.103 +stackFrames = getStackFrames(myErr); 1.104 +status = inSection(6); 1.105 +actual = stackFrames[0].substring(0,1); 1.106 +expect = '@'; 1.107 +addThis(); 1.108 + 1.109 +status = inSection(7); 1.110 +actual = stackFrames[1].substring(0,2); 1.111 +expect = 'A@'; 1.112 +addThis(); 1.113 + 1.114 +status = inSection(8); 1.115 +actual = stackFrames[2].substring(0,2); 1.116 +expect = 'B@'; 1.117 +addThis(); 1.118 + 1.119 +status = inSection(9); 1.120 +actual = stackFrames[3].substring(0,2); 1.121 +expect = 'C@'; 1.122 +addThis(); 1.123 + 1.124 +status = inSection(10); 1.125 +actual = stackFrames[4].substring(0,2); 1.126 +expect = 'D@';; 1.127 +addThis(); 1.128 + 1.129 + 1.130 + 1.131 +/* 1.132 + * Make the first frame occur in a function with an empty name - 1.133 + */ 1.134 +myErr = function() { return A(44,13); } (); 1.135 +stackFrames = getStackFrames(myErr); 1.136 +status = inSection(11); 1.137 +actual = stackFrames[0].substring(0,1); 1.138 +expect = '@'; 1.139 +addThis(); 1.140 + 1.141 +status = inSection(12); 1.142 +actual = stackFrames[1].substring(0,7); 1.143 +expect = 'myErr<@'; 1.144 +addThis(); 1.145 + 1.146 +status = inSection(13); 1.147 +actual = stackFrames[2].substring(0,2); 1.148 +expect = 'A@'; 1.149 +addThis(); 1.150 + 1.151 +// etc. for the rest of the frames as above 1.152 + 1.153 + 1.154 + 1.155 +/* 1.156 + * Make the first frame occur in a function with name 'anonymous' - 1.157 + */ 1.158 +var f = Function('return A(44,13);'); 1.159 +myErr = f(); 1.160 +stackFrames = getStackFrames(myErr); 1.161 +status = inSection(14); 1.162 +actual = stackFrames[0].substring(0,1); 1.163 +expect = '@'; 1.164 +addThis(); 1.165 + 1.166 +status = inSection(15); 1.167 +actual = stackFrames[1].substring(0,10); 1.168 +expect = 'anonymous@'; 1.169 +addThis(); 1.170 + 1.171 +status = inSection(16); 1.172 +actual = stackFrames[2].substring(0,2); 1.173 +expect = 'A@'; 1.174 +addThis(); 1.175 + 1.176 +// etc. for the rest of the frames as above 1.177 + 1.178 + 1.179 + 1.180 +/* 1.181 + * Make a user-defined error via the Error() function - 1.182 + */ 1.183 +var message = 'Hi there!'; var fileName = 'file name'; var lineNumber = 0; 1.184 +myErr = Error(message, fileName, lineNumber); 1.185 +stackFrames = getStackFrames(myErr); 1.186 +status = inSection(17); 1.187 +actual = stackFrames[0].substring(0,1); 1.188 +expect = '@'; 1.189 +addThis(); 1.190 + 1.191 + 1.192 +/* 1.193 + * Now use the |new| keyword. Re-use the same params - 1.194 + */ 1.195 +myErr = new Error(message, fileName, lineNumber); 1.196 +stackFrames = getStackFrames(myErr); 1.197 +status = inSection(18); 1.198 +actual = stackFrames[0].substring(0,1); 1.199 +expect = '@'; 1.200 +addThis(); 1.201 + 1.202 + 1.203 + 1.204 + 1.205 +//----------------------------------------------------------------------------- 1.206 +test(); 1.207 +//----------------------------------------------------------------------------- 1.208 + 1.209 + 1.210 + 1.211 +/* 1.212 + * Split the string |err.stack| along its '\n' delimiter. 1.213 + * As of 2002-02-28 |err.stack| ends with the delimiter, so 1.214 + * the resulting array has an empty string as its last element. 1.215 + * 1.216 + * Pop that useless element off before doing anything. 1.217 + * Then reverse the array, for convenience of indexing - 1.218 + */ 1.219 +function getStackFrames(err) 1.220 +{ 1.221 + var arr = err.stack.split('\n'); 1.222 + arr.pop(); 1.223 + return arr.reverse(); 1.224 +} 1.225 + 1.226 + 1.227 +function addThis() 1.228 +{ 1.229 + statusitems[UBound] = status; 1.230 + actualvalues[UBound] = actual; 1.231 + expectedvalues[UBound] = expect; 1.232 + UBound++; 1.233 +} 1.234 + 1.235 + 1.236 +function test() 1.237 +{ 1.238 + enterFunc('test'); 1.239 + printBugNumber(BUGNUMBER); 1.240 + printStatus(summary); 1.241 + 1.242 + for (var i=0; i<UBound; i++) 1.243 + { 1.244 + reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 1.245 + } 1.246 + 1.247 + exitFunc ('test'); 1.248 +}