Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions stacktrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/TestPhantomJS.js
Original file line number Diff line number Diff line change
@@ -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();
65 changes: 23 additions & 42 deletions test/TestStacktrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,62 +532,43 @@
});

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();

function f1() {
try {
this.undef();
} catch (e) {
debugger;
results = p.run(e, 'other');
}
}
Expand All @@ -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()');
Expand Down