| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Tools for parsing and pretty printing error stack traces. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.setTestOnly('goog.testing.stacktrace'); |
| 21 |
goog.provide('goog.testing.stacktrace'); |
| 22 |
goog.provide('goog.testing.stacktrace.Frame'); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Class representing one stack frame. |
| 28 |
* @param {string} context Context object, empty in case of global functions or |
| 29 |
* if the browser doesn't provide this information. |
| 30 |
* @param {string} name Function name, empty in case of anonymous functions. |
| 31 |
* @param {string} alias Alias of the function if available. For example the |
| 32 |
* function name will be 'c' and the alias will be 'b' if the function is |
| 33 |
* defined as <code>a.b = function c() {};</code>. |
| 34 |
* @param {string} path File path or URL including line number and optionally |
| 35 |
* column number separated by colons. |
| 36 |
* @constructor |
| 37 |
* @final |
| 38 |
*/ |
| 39 |
goog.testing.stacktrace.Frame = function(context, name, alias, path) { |
| 40 |
this.context_ = context; |
| 41 |
this.name_ = name; |
| 42 |
this.alias_ = alias; |
| 43 |
this.path_ = path; |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* @return {string} The function name or empty string if the function is |
| 49 |
* anonymous and the object field which it's assigned to is unknown. |
| 50 |
*/ |
| 51 |
goog.testing.stacktrace.Frame.prototype.getName = function() { |
| 52 |
return this.name_; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* @return {boolean} Whether the stack frame contains an anonymous function. |
| 58 |
*/ |
| 59 |
goog.testing.stacktrace.Frame.prototype.isAnonymous = function() { |
| 60 |
return !this.name_ || this.context_ == '[object Object]'; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Brings one frame of the stack trace into a common format across browsers. |
| 66 |
* @return {string} Pretty printed stack frame. |
| 67 |
*/ |
| 68 |
goog.testing.stacktrace.Frame.prototype.toCanonicalString = function() { |
| 69 |
var htmlEscape = goog.testing.stacktrace.htmlEscape_; |
| 70 |
var deobfuscate = goog.testing.stacktrace.maybeDeobfuscateFunctionName_; |
| 71 |
|
| 72 |
var canonical = [ |
| 73 |
this.context_ ? htmlEscape(this.context_) + '.' : '', |
| 74 |
this.name_ ? htmlEscape(deobfuscate(this.name_)) : 'anonymous', |
| 75 |
this.alias_ ? ' [as ' + htmlEscape(deobfuscate(this.alias_)) + ']' : '' |
| 76 |
]; |
| 77 |
|
| 78 |
if (this.path_) { |
| 79 |
canonical.push(' at '); |
| 80 |
canonical.push(htmlEscape(this.path_)); |
| 81 |
} |
| 82 |
return canonical.join(''); |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Maximum number of steps while the call chain is followed. |
| 88 |
* @private {number} |
| 89 |
* @const |
| 90 |
*/ |
| 91 |
goog.testing.stacktrace.MAX_DEPTH_ = 20; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Maximum length of a string that can be matched with a RegExp on |
| 96 |
* Firefox 3x. Exceeding this approximate length will cause string.match |
| 97 |
* to exceed Firefox's stack quota. This situation can be encountered |
| 98 |
* when goog.globalEval is invoked with a long argument; such as |
| 99 |
* when loading a module. |
| 100 |
* @private {number} |
| 101 |
* @const |
| 102 |
*/ |
| 103 |
goog.testing.stacktrace.MAX_FIREFOX_FRAMESTRING_LENGTH_ = 500000; |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* RegExp pattern for JavaScript identifiers. We don't support Unicode |
| 108 |
* identifiers defined in ECMAScript v3. |
| 109 |
* @private {string} |
| 110 |
* @const |
| 111 |
*/ |
| 112 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ = '[a-zA-Z_$][\\w$]*'; |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* RegExp pattern for function name alias in the V8 stack trace. |
| 117 |
* @private {string} |
| 118 |
* @const |
| 119 |
*/ |
| 120 |
goog.testing.stacktrace.V8_ALIAS_PATTERN_ = |
| 121 |
'(?: \\[as (' + goog.testing.stacktrace.IDENTIFIER_PATTERN_ + ')\\])?'; |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* RegExp pattern for the context of a function call in a V8 stack trace. |
| 126 |
* Creates an optional submatch for the namespace identifier including the |
| 127 |
* "new" keyword for constructor calls (e.g. "new foo.Bar"). |
| 128 |
* @private {string} |
| 129 |
* @const |
| 130 |
*/ |
| 131 |
goog.testing.stacktrace.V8_CONTEXT_PATTERN_ = |
| 132 |
'(?:((?:new )?(?:\\[object Object\\]|' + |
| 133 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + '(?:\\.' + |
| 134 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + ')*))\\.)?'; |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* RegExp pattern for function names and constructor calls in the V8 stack |
| 139 |
* trace. |
| 140 |
* @private {string} |
| 141 |
* @const |
| 142 |
*/ |
| 143 |
goog.testing.stacktrace.V8_FUNCTION_NAME_PATTERN_ = '(?:new )?(?:' + |
| 144 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + '|<anonymous>)'; |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* RegExp pattern for function call in the V8 stack trace. Creates 3 submatches |
| 149 |
* with context object (optional), function name and function alias (optional). |
| 150 |
* @private {string} |
| 151 |
* @const |
| 152 |
*/ |
| 153 |
goog.testing.stacktrace.V8_FUNCTION_CALL_PATTERN_ = ' ' + |
| 154 |
goog.testing.stacktrace.V8_CONTEXT_PATTERN_ + '(' + |
| 155 |
goog.testing.stacktrace.V8_FUNCTION_NAME_PATTERN_ + ')' + |
| 156 |
goog.testing.stacktrace.V8_ALIAS_PATTERN_; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* RegExp pattern for an URL + position inside the file. |
| 161 |
* @private {string} |
| 162 |
* @const |
| 163 |
*/ |
| 164 |
goog.testing.stacktrace.URL_PATTERN_ = |
| 165 |
'((?:http|https|file)://[^\\s)]+|javascript:.*)'; |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* RegExp pattern for an URL + line number + column number in V8. |
| 170 |
* The URL is either in submatch 1 or submatch 2. |
| 171 |
* @private {string} |
| 172 |
* @const |
| 173 |
*/ |
| 174 |
goog.testing.stacktrace.CHROME_URL_PATTERN_ = ' (?:' + |
| 175 |
'\\(unknown source\\)' + |
| 176 |
'|' + |
| 177 |
'\\(native\\)' + |
| 178 |
'|' + |
| 179 |
'\\((.+)\\)|(.+))'; |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Regular expression for parsing one stack frame in V8. For more information |
| 184 |
* on V8 stack frame formats, see |
| 185 |
* https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi. |
| 186 |
* @private {!RegExp} |
| 187 |
* @const |
| 188 |
*/ |
| 189 |
goog.testing.stacktrace.V8_STACK_FRAME_REGEXP_ = new RegExp( |
| 190 |
'^ at' + |
| 191 |
'(?:' + goog.testing.stacktrace.V8_FUNCTION_CALL_PATTERN_ + ')?' + |
| 192 |
goog.testing.stacktrace.CHROME_URL_PATTERN_ + '$'); |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* RegExp pattern for function call in the Firefox stack trace. |
| 197 |
* Creates 2 submatches with function name (optional) and arguments. |
| 198 |
* |
| 199 |
* Modern FF produces stack traces like: |
| 200 |
* foo@url:1:2 |
| 201 |
* a.b.foo@url:3:4 |
| 202 |
* |
| 203 |
* @private {string} |
| 204 |
* @const |
| 205 |
*/ |
| 206 |
goog.testing.stacktrace.FIREFOX_FUNCTION_CALL_PATTERN_ = '(' + |
| 207 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + '(?:\\.' + |
| 208 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + ')*' + |
| 209 |
')?' + |
| 210 |
'(\\(.*\\))?@'; |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* Regular expression for parsing one stack frame in Firefox. |
| 215 |
* @private {!RegExp} |
| 216 |
* @const |
| 217 |
*/ |
| 218 |
goog.testing.stacktrace.FIREFOX_STACK_FRAME_REGEXP_ = new RegExp( |
| 219 |
'^' + goog.testing.stacktrace.FIREFOX_FUNCTION_CALL_PATTERN_ + '(?::0|' + |
| 220 |
goog.testing.stacktrace.URL_PATTERN_ + ')$'); |
| 221 |
|
| 222 |
|
| 223 |
/** |
| 224 |
* RegExp pattern for an anonymous function call in an Opera stack frame. |
| 225 |
* Creates 2 (optional) submatches: the context object and function name. |
| 226 |
* @private {string} |
| 227 |
* @const |
| 228 |
*/ |
| 229 |
goog.testing.stacktrace.OPERA_ANONYMOUS_FUNCTION_NAME_PATTERN_ = |
| 230 |
'<anonymous function(?:\\: ' + |
| 231 |
'(?:(' + goog.testing.stacktrace.IDENTIFIER_PATTERN_ + '(?:\\.' + |
| 232 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + ')*)\\.)?' + |
| 233 |
'(' + goog.testing.stacktrace.IDENTIFIER_PATTERN_ + '))?>'; |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* RegExp pattern for a function call in an Opera stack frame. |
| 238 |
* Creates 4 (optional) submatches: the function name (if not anonymous), |
| 239 |
* the aliased context object and function name (if anonymous), and the |
| 240 |
* function call arguments. |
| 241 |
* @private {string} |
| 242 |
* @const |
| 243 |
*/ |
| 244 |
goog.testing.stacktrace.OPERA_FUNCTION_CALL_PATTERN_ = '(?:(?:(' + |
| 245 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + ')|' + |
| 246 |
goog.testing.stacktrace.OPERA_ANONYMOUS_FUNCTION_NAME_PATTERN_ + |
| 247 |
')(\\(.*\\)))?@'; |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Regular expression for parsing on stack frame in Opera 11.68 - 12.17. |
| 252 |
* Newer versions of Opera use V8 and stack frames should match against |
| 253 |
* goog.testing.stacktrace.V8_STACK_FRAME_REGEXP_. |
| 254 |
* @private {!RegExp} |
| 255 |
* @const |
| 256 |
*/ |
| 257 |
goog.testing.stacktrace.OPERA_STACK_FRAME_REGEXP_ = new RegExp( |
| 258 |
'^' + goog.testing.stacktrace.OPERA_FUNCTION_CALL_PATTERN_ + |
| 259 |
goog.testing.stacktrace.URL_PATTERN_ + '?$'); |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Regular expression for finding the function name in its source. |
| 264 |
* @private {!RegExp} |
| 265 |
* @const |
| 266 |
*/ |
| 267 |
goog.testing.stacktrace.FUNCTION_SOURCE_REGEXP_ = new RegExp( |
| 268 |
'^function (' + goog.testing.stacktrace.IDENTIFIER_PATTERN_ + ')'); |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* RegExp pattern for function call in a IE stack trace. This expression allows |
| 273 |
* for identifiers like 'Anonymous function', 'eval code', and 'Global code'. |
| 274 |
* @private {string} |
| 275 |
* @const |
| 276 |
*/ |
| 277 |
goog.testing.stacktrace.IE_FUNCTION_CALL_PATTERN_ = '(' + |
| 278 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + '(?:\\.' + |
| 279 |
goog.testing.stacktrace.IDENTIFIER_PATTERN_ + ')*' + |
| 280 |
'(?:\\s+\\w+)*)'; |
| 281 |
|
| 282 |
|
| 283 |
/** |
| 284 |
* Regular expression for parsing a stack frame in IE. |
| 285 |
* @private {!RegExp} |
| 286 |
* @const |
| 287 |
*/ |
| 288 |
goog.testing.stacktrace.IE_STACK_FRAME_REGEXP_ = new RegExp( |
| 289 |
'^ at ' + goog.testing.stacktrace.IE_FUNCTION_CALL_PATTERN_ + '\\s*\\(' + |
| 290 |
'(' + |
| 291 |
'eval code:[^)]*' + |
| 292 |
'|' + |
| 293 |
'Unknown script code:[^)]*' + |
| 294 |
'|' + goog.testing.stacktrace.URL_PATTERN_ + ')\\)?$'); |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Creates a stack trace by following the call chain. Based on |
| 299 |
* {@link goog.debug.getStacktrace}. |
| 300 |
* @return {!Array<!goog.testing.stacktrace.Frame>} Stack frames. |
| 301 |
* @private |
| 302 |
* @suppress {es5Strict} |
| 303 |
*/ |
| 304 |
goog.testing.stacktrace.followCallChain_ = function() { |
| 305 |
var frames = []; |
| 306 |
var fn = arguments.callee.caller; |
| 307 |
var depth = 0; |
| 308 |
|
| 309 |
while (fn && depth < goog.testing.stacktrace.MAX_DEPTH_) { |
| 310 |
var fnString = Function.prototype.toString.call(fn); |
| 311 |
var match = fnString.match(goog.testing.stacktrace.FUNCTION_SOURCE_REGEXP_); |
| 312 |
var functionName = match ? match[1] : ''; |
| 313 |
|
| 314 |
frames.push(new goog.testing.stacktrace.Frame('', functionName, '', '')); |
| 315 |
|
| 316 |
|
| 317 |
try { |
| 318 |
fn = fn.caller; |
| 319 |
} catch (e) { |
| 320 |
break; |
| 321 |
} |
| 322 |
depth++; |
| 323 |
} |
| 324 |
|
| 325 |
return frames; |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
/** |
| 330 |
* Parses one stack frame. |
| 331 |
* @param {string} frameStr The stack frame as string. |
| 332 |
* @return {goog.testing.stacktrace.Frame} Stack frame object or null if the |
| 333 |
* parsing failed. |
| 334 |
* @private |
| 335 |
*/ |
| 336 |
goog.testing.stacktrace.parseStackFrame_ = function(frameStr) { |
| 337 |
// This match includes newer versions of Opera (15+). |
| 338 |
var m = frameStr.match(goog.testing.stacktrace.V8_STACK_FRAME_REGEXP_); |
| 339 |
if (m) { |
| 340 |
return new goog.testing.stacktrace.Frame( |
| 341 |
m[1] || '', m[2] || '', m[3] || '', m[4] || m[5] || m[6] || ''); |
| 342 |
} |
| 343 |
|
| 344 |
// TODO(johnlenz): remove this. It seems like if this was useful it would |
| 345 |
// need to be before the V8 check. |
| 346 |
if (frameStr.length > |
| 347 |
goog.testing.stacktrace.MAX_FIREFOX_FRAMESTRING_LENGTH_) { |
| 348 |
return null; |
| 349 |
} |
| 350 |
|
| 351 |
m = frameStr.match(goog.testing.stacktrace.FIREFOX_STACK_FRAME_REGEXP_); |
| 352 |
if (m) { |
| 353 |
return new goog.testing.stacktrace.Frame('', m[1] || '', '', m[3] || ''); |
| 354 |
} |
| 355 |
|
| 356 |
// Match against Presto Opera 11.68 - 12.17. |
| 357 |
m = frameStr.match(goog.testing.stacktrace.OPERA_STACK_FRAME_REGEXP_); |
| 358 |
if (m) { |
| 359 |
return new goog.testing.stacktrace.Frame( |
| 360 |
m[2] || '', m[1] || m[3] || '', '', m[5] || ''); |
| 361 |
} |
| 362 |
|
| 363 |
m = frameStr.match(goog.testing.stacktrace.IE_STACK_FRAME_REGEXP_); |
| 364 |
if (m) { |
| 365 |
return new goog.testing.stacktrace.Frame('', m[1] || '', '', m[2] || ''); |
| 366 |
} |
| 367 |
|
| 368 |
return null; |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
/** |
| 373 |
* Function to deobfuscate function names. |
| 374 |
* @type {function(string): string} |
| 375 |
* @private |
| 376 |
*/ |
| 377 |
goog.testing.stacktrace.deobfuscateFunctionName_; |
| 378 |
|
| 379 |
|
| 380 |
/** |
| 381 |
* Sets function to deobfuscate function names. |
| 382 |
* @param {function(string): string} fn function to deobfuscate function names. |
| 383 |
*/ |
| 384 |
goog.testing.stacktrace.setDeobfuscateFunctionName = function(fn) { |
| 385 |
goog.testing.stacktrace.deobfuscateFunctionName_ = fn; |
| 386 |
}; |
| 387 |
|
| 388 |
|
| 389 |
/** |
| 390 |
* Deobfuscates a compiled function name with the function passed to |
| 391 |
* {@link #setDeobfuscateFunctionName}. Returns the original function name if |
| 392 |
* the deobfuscator hasn't been set. |
| 393 |
* @param {string} name The function name to deobfuscate. |
| 394 |
* @return {string} The deobfuscated function name. |
| 395 |
* @private |
| 396 |
*/ |
| 397 |
goog.testing.stacktrace.maybeDeobfuscateFunctionName_ = function(name) { |
| 398 |
return goog.testing.stacktrace.deobfuscateFunctionName_ ? |
| 399 |
goog.testing.stacktrace.deobfuscateFunctionName_(name) : |
| 400 |
name; |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
/** |
| 405 |
* Escapes the special character in HTML. |
| 406 |
* @param {string} text Plain text. |
| 407 |
* @return {string} Escaped text. |
| 408 |
* @private |
| 409 |
*/ |
| 410 |
goog.testing.stacktrace.htmlEscape_ = function(text) { |
| 411 |
return text.replace(/&/g, '&') |
| 412 |
.replace(/</g, '<') |
| 413 |
.replace(/>/g, '>') |
| 414 |
.replace(/"/g, '"'); |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* Converts the stack frames into canonical format. Chops the beginning and the |
| 420 |
* end of it which come from the testing environment, not from the test itself. |
| 421 |
* @param {!Array<goog.testing.stacktrace.Frame>} frames The frames. |
| 422 |
* @return {string} Canonical, pretty printed stack trace. |
| 423 |
* @private |
| 424 |
*/ |
| 425 |
goog.testing.stacktrace.framesToString_ = function(frames) { |
| 426 |
// Removes the anonymous calls from the end of the stack trace (they come |
| 427 |
// from testrunner.js, testcase.js and asserts.js), so the stack trace will |
| 428 |
// end with the test... method. |
| 429 |
var lastIndex = frames.length - 1; |
| 430 |
while (frames[lastIndex] && frames[lastIndex].isAnonymous()) { |
| 431 |
lastIndex--; |
| 432 |
} |
| 433 |
|
| 434 |
// Removes the beginning of the stack trace until the call of the private |
| 435 |
// _assert function (inclusive), so the stack trace will begin with a public |
| 436 |
// asserter. Does nothing if _assert is not present in the stack trace. |
| 437 |
var privateAssertIndex = -1; |
| 438 |
for (var i = 0; i < frames.length; i++) { |
| 439 |
if (frames[i] && frames[i].getName() == '_assert') { |
| 440 |
privateAssertIndex = i; |
| 441 |
break; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
var canonical = []; |
| 446 |
for (var i = privateAssertIndex + 1; i <= lastIndex; i++) { |
| 447 |
canonical.push('> '); |
| 448 |
if (frames[i]) { |
| 449 |
canonical.push(frames[i].toCanonicalString()); |
| 450 |
} else { |
| 451 |
canonical.push('(unknown)'); |
| 452 |
} |
| 453 |
canonical.push('\n'); |
| 454 |
} |
| 455 |
return canonical.join(''); |
| 456 |
}; |
| 457 |
|
| 458 |
|
| 459 |
/** |
| 460 |
* Parses the browser's native stack trace. |
| 461 |
* @param {string} stack Stack trace. |
| 462 |
* @return {!Array<goog.testing.stacktrace.Frame>} Stack frames. The |
| 463 |
* unrecognized frames will be nulled out. |
| 464 |
* @private |
| 465 |
*/ |
| 466 |
goog.testing.stacktrace.parse_ = function(stack) { |
| 467 |
var lines = stack.replace(/\s*$/, '').split('\n'); |
| 468 |
var frames = []; |
| 469 |
for (var i = 0; i < lines.length; i++) { |
| 470 |
frames.push(goog.testing.stacktrace.parseStackFrame_(lines[i])); |
| 471 |
} |
| 472 |
return frames; |
| 473 |
}; |
| 474 |
|
| 475 |
|
| 476 |
/** |
| 477 |
* Brings the stack trace into a common format across browsers. |
| 478 |
* @param {string} stack Browser-specific stack trace. |
| 479 |
* @return {string} Same stack trace in common format. |
| 480 |
*/ |
| 481 |
goog.testing.stacktrace.canonicalize = function(stack) { |
| 482 |
var frames = goog.testing.stacktrace.parse_(stack); |
| 483 |
return goog.testing.stacktrace.framesToString_(frames); |
| 484 |
}; |
| 485 |
|
| 486 |
|
| 487 |
/** |
| 488 |
* Returns the native stack trace. |
| 489 |
* @return {string|!Array<!CallSite>} |
| 490 |
* @private |
| 491 |
*/ |
| 492 |
goog.testing.stacktrace.getNativeStack_ = function() { |
| 493 |
var tmpError = new Error(); |
| 494 |
if (tmpError.stack) { |
| 495 |
return tmpError.stack; |
| 496 |
} |
| 497 |
|
| 498 |
// IE10 will only create a stack trace when the Error is thrown. |
| 499 |
// We use null.x() to throw an exception because the closure compiler may |
| 500 |
// replace "throw" with a function call in an attempt to minimize the binary |
| 501 |
// size, which in turn has the side effect of adding an unwanted stack frame. |
| 502 |
try { |
| 503 |
null.x(); |
| 504 |
} catch (e) { |
| 505 |
return e.stack; |
| 506 |
} |
| 507 |
return ''; |
| 508 |
}; |
| 509 |
|
| 510 |
|
| 511 |
/** |
| 512 |
* Gets the native stack trace if available otherwise follows the call chain. |
| 513 |
* @return {string} The stack trace in canonical format. |
| 514 |
*/ |
| 515 |
goog.testing.stacktrace.get = function() { |
| 516 |
var stack = goog.testing.stacktrace.getNativeStack_(); |
| 517 |
var frames; |
| 518 |
if (!stack) { |
| 519 |
frames = goog.testing.stacktrace.followCallChain_(); |
| 520 |
} else if (goog.isArray(stack)) { |
| 521 |
frames = goog.testing.stacktrace.callSitesToFrames_(stack); |
| 522 |
} else { |
| 523 |
frames = goog.testing.stacktrace.parse_(stack); |
| 524 |
} |
| 525 |
return goog.testing.stacktrace.framesToString_(frames); |
| 526 |
}; |
| 527 |
|
| 528 |
|
| 529 |
/** |
| 530 |
* Converts an array of CallSite (elements of a stack trace in V8) to an array |
| 531 |
* of Frames. |
| 532 |
* @param {!Array<!CallSite>} stack The stack as an array of CallSites. |
| 533 |
* @return {!Array<!goog.testing.stacktrace.Frame>} The stack as an array of |
| 534 |
* Frames. |
| 535 |
* @private |
| 536 |
*/ |
| 537 |
goog.testing.stacktrace.callSitesToFrames_ = function(stack) { |
| 538 |
var frames = []; |
| 539 |
for (var i = 0; i < stack.length; i++) { |
| 540 |
var callSite = stack[i]; |
| 541 |
var functionName = callSite.getFunctionName() || 'unknown'; |
| 542 |
var fileName = callSite.getFileName(); |
| 543 |
var path = fileName ? |
| 544 |
fileName + ':' + callSite.getLineNumber() + ':' + |
| 545 |
callSite.getColumnNumber() : |
| 546 |
'unknown'; |
| 547 |
frames.push(new goog.testing.stacktrace.Frame('', functionName, '', path)); |
| 548 |
} |
| 549 |
return frames; |
| 550 |
}; |
| 551 |
|
| 552 |
|
| 553 |
goog.exportSymbol( |
| 554 |
'setDeobfuscateFunctionName', |
| 555 |
goog.testing.stacktrace.setDeobfuscateFunctionName); |