diff --git a/stacktrace.js b/stacktrace.js index 62ddad7..a8c3d99 100644 --- a/stacktrace.js +++ b/stacktrace.js @@ -280,16 +280,20 @@ // Safari 5-, IE 9-, and others other: function(curr) { - var ANON = '{anonymous}', fnRE = /function\s*([\w\-$]+)?\s*\(/i, stack = [], fn, args, maxStackSize = 10; + var ANON = '{anonymous}', fnRE = /function(?:\s+([\w$]+))?\s*\(/, stack = [], fn, args, maxStackSize = 10; var slice = Array.prototype.slice; - while (curr && curr['arguments'] && stack.length < maxStackSize) { + while (curr && stack.length < maxStackSize) { fn = fnRE.test(curr.toString()) ? RegExp.$1 || ANON : ANON; - args = slice.call(curr['arguments'] || []); + try { + args = slice.call(curr['arguments'] || []); + } catch (e) { + args = ['Cannot access arguments: ' + e]; + } stack[stack.length] = fn + '(' + this.stringifyArguments(args) + ')'; try { curr = curr.caller; } catch (e) { - stack[stack.length] = '' + e; + stack[stack.length] = 'Cannot access caller: ' + e; break; } } diff --git a/test/TestPhantomJS.js b/test/TestPhantomJS.js new file mode 100644 index 0000000..817e458 --- /dev/null +++ b/test/TestPhantomJS.js @@ -0,0 +1,32 @@ +/*global require,phantom*/ +var printStackTrace = require('../stacktrace.js'); +var exLab = require('../test/functional/ExceptionLab.js'); +//console.log(exLab.getExceptionProps); + +function f1() { + try { + this.undef(); + } catch (e) { + console.log(exLab.getExceptionProps(e)); + //console.log(e.stackArray); + //console.log(exLab.getExceptionProps(e.stackArray[0])); + //console.log(exLab.getExceptionProps(e.stackArray[1])); + //console.log(exLab.getExceptionProps(e.stackArray[2])); + //console.log(exLab.getExceptionProps(e.stackArray[3])); + + console.log('stack:', printStackTrace({e: e})); + var p = new printStackTrace.implementation(); + console.log('other:', p.run(e, 'other')); + } +} + +function f2() { + f1(0, 'abc', f1, {a: 0}); +} + +(function longName_$1() { + "use strict"; + f2(); +}()); + +phantom.exit(); diff --git a/test/TestStacktrace.js b/test/TestStacktrace.js index 0acc428..bb63b45 100644 --- a/test/TestStacktrace.js +++ b/test/TestStacktrace.js @@ -532,54 +532,36 @@ }); test("other", function() { - var mode = pst.mode(UnitTest.fn.createGenericError()); - var frame = function(args, fun, caller) { - this['arguments'] = args; - this.caller = caller; - this.fun = fun; - }; - frame.prototype.toString = function() { - return 'function ' + this.fun + '() {}'; - }; - function f10() { - } + expect(5); + var results = []; - var frame_f2 = new frame([], '', undefined); - var frame_f1 = new frame([1, 'abc', f10, { - 1: { - 2: { - 3: 4 - } + function f1() { + try { + this.undef(); + } catch (e) { + var p = impl(); + results = p.run(e, 'other'); } - }], 'FUNCTION f1 (a,b,c)', frame_f2); - - expect(mode == 'other' ? 4 : 2); - var message = pst.other(frame_f1); - equals(message[0].indexOf('f1(1,"abc",#function,#object)') >= 0, true, 'f1'); - equals(message[1].indexOf('{anonymous}()') >= 0, true, 'f2 anonymous'); + } - if (mode == 'other') { - function f1(arg1, arg2) { - var message = pst.other(arguments.callee); - //equals(message.join("\n"), '', 'debug'); - equals(message[0].indexOf('f1(1,"abc",#function,#object)') >= 0, true, 'f1'); - equals(message[1].indexOf('{anonymous}()') >= 0, true, 'f2 anonymous'); - } + function f2() { + f1(0, 'abc', f1, {a: 0}); + } - var f2 = function() { - f1(1, 'abc', f10, { - 1: { - 2: { - 3: 4 - } - } - }); - }; + (function longName_$1() { f2(); - } + }()); + + ok(results.length >= 3, 'Call chain should contain at least 4 frames'); + //equals(results, '', 'debug'); + equals(results[1], 'f1(0,"abc",#function,#object)'); + equals(results[2], 'f2()'); + equals(results[3], 'longName_$1()'); + equals(results[4], '{anonymous}()'); }); test("other in strict mode", function() { + expect(3); var results = []; var p = impl(); @@ -587,7 +569,6 @@ try { this.undef(); } catch (e) { - debugger; results = p.run(e, 'other'); } } @@ -603,7 +584,7 @@ f3(); - ok(results.length >= 3, 'Stack should contain at least 3 frames in non-strict mode'); + ok(results.length >= 3, 'Call chain should contain at least 3 frames (2 non-strict and 1 strict)'); //equals(results, '', 'debug'); equals(results[1], 'f1()'); equals(results[2], 'f2()');