Skip to content

Commit e837911

Browse files
Merge pull request #75 from stacktracejs/phantomjs_strict_mode
Surrounded access to `arguments` in `try .. catch` to avoid #73
2 parents 3bd4428 + 2bab1a1 commit e837911

File tree

3 files changed

+63
-46
lines changed

3 files changed

+63
-46
lines changed

stacktrace.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,20 @@
280280

281281
// Safari 5-, IE 9-, and others
282282
other: function(curr) {
283-
var ANON = '{anonymous}', fnRE = /function\s*([\w\-$]+)?\s*\(/i, stack = [], fn, args, maxStackSize = 10;
283+
var ANON = '{anonymous}', fnRE = /function(?:\s+([\w$]+))?\s*\(/, stack = [], fn, args, maxStackSize = 10;
284284
var slice = Array.prototype.slice;
285-
while (curr && curr['arguments'] && stack.length < maxStackSize) {
285+
while (curr && stack.length < maxStackSize) {
286286
fn = fnRE.test(curr.toString()) ? RegExp.$1 || ANON : ANON;
287-
args = slice.call(curr['arguments'] || []);
287+
try {
288+
args = slice.call(curr['arguments'] || []);
289+
} catch (e) {
290+
args = ['Cannot access arguments: ' + e];
291+
}
288292
stack[stack.length] = fn + '(' + this.stringifyArguments(args) + ')';
289293
try {
290294
curr = curr.caller;
291295
} catch (e) {
292-
stack[stack.length] = '' + e;
296+
stack[stack.length] = 'Cannot access caller: ' + e;
293297
break;
294298
}
295299
}

test/TestPhantomJS.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*global require,phantom*/
2+
var printStackTrace = require('../stacktrace.js');
3+
var exLab = require('../test/functional/ExceptionLab.js');
4+
//console.log(exLab.getExceptionProps);
5+
6+
function f1() {
7+
try {
8+
this.undef();
9+
} catch (e) {
10+
console.log(exLab.getExceptionProps(e));
11+
//console.log(e.stackArray);
12+
//console.log(exLab.getExceptionProps(e.stackArray[0]));
13+
//console.log(exLab.getExceptionProps(e.stackArray[1]));
14+
//console.log(exLab.getExceptionProps(e.stackArray[2]));
15+
//console.log(exLab.getExceptionProps(e.stackArray[3]));
16+
17+
console.log('stack:', printStackTrace({e: e}));
18+
var p = new printStackTrace.implementation();
19+
console.log('other:', p.run(e, 'other'));
20+
}
21+
}
22+
23+
function f2() {
24+
f1(0, 'abc', f1, {a: 0});
25+
}
26+
27+
(function longName_$1() {
28+
"use strict";
29+
f2();
30+
}());
31+
32+
phantom.exit();

test/TestStacktrace.js

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -532,62 +532,43 @@
532532
});
533533

534534
test("other", function() {
535-
var mode = pst.mode(UnitTest.fn.createGenericError());
536-
var frame = function(args, fun, caller) {
537-
this['arguments'] = args;
538-
this.caller = caller;
539-
this.fun = fun;
540-
};
541-
frame.prototype.toString = function() {
542-
return 'function ' + this.fun + '() {}';
543-
};
544-
function f10() {
545-
}
535+
expect(5);
536+
var results = [];
546537

547-
var frame_f2 = new frame([], '', undefined);
548-
var frame_f1 = new frame([1, 'abc', f10, {
549-
1: {
550-
2: {
551-
3: 4
552-
}
538+
function f1() {
539+
try {
540+
this.undef();
541+
} catch (e) {
542+
var p = impl();
543+
results = p.run(e, 'other');
553544
}
554-
}], 'FUNCTION f1 (a,b,c)', frame_f2);
555-
556-
expect(mode == 'other' ? 4 : 2);
557-
var message = pst.other(frame_f1);
558-
equals(message[0].indexOf('f1(1,"abc",#function,#object)') >= 0, true, 'f1');
559-
equals(message[1].indexOf('{anonymous}()') >= 0, true, 'f2 anonymous');
545+
}
560546

561-
if (mode == 'other') {
562-
function f1(arg1, arg2) {
563-
var message = pst.other(arguments.callee);
564-
//equals(message.join("\n"), '', 'debug');
565-
equals(message[0].indexOf('f1(1,"abc",#function,#object)') >= 0, true, 'f1');
566-
equals(message[1].indexOf('{anonymous}()') >= 0, true, 'f2 anonymous');
567-
}
547+
function f2() {
548+
f1(0, 'abc', f1, {a: 0});
549+
}
568550

569-
var f2 = function() {
570-
f1(1, 'abc', f10, {
571-
1: {
572-
2: {
573-
3: 4
574-
}
575-
}
576-
});
577-
};
551+
(function longName_$1() {
578552
f2();
579-
}
553+
}());
554+
555+
ok(results.length >= 3, 'Call chain should contain at least 4 frames');
556+
//equals(results, '', 'debug');
557+
equals(results[1], 'f1(0,"abc",#function,#object)');
558+
equals(results[2], 'f2()');
559+
equals(results[3], 'longName_$1()');
560+
equals(results[4], '{anonymous}()');
580561
});
581562

582563
test("other in strict mode", function() {
564+
expect(3);
583565
var results = [];
584566
var p = impl();
585567

586568
function f1() {
587569
try {
588570
this.undef();
589571
} catch (e) {
590-
debugger;
591572
results = p.run(e, 'other');
592573
}
593574
}
@@ -603,7 +584,7 @@
603584

604585
f3();
605586

606-
ok(results.length >= 3, 'Stack should contain at least 3 frames in non-strict mode');
587+
ok(results.length >= 3, 'Call chain should contain at least 3 frames (2 non-strict and 1 strict)');
607588
//equals(results, '', 'debug');
608589
equals(results[1], 'f1()');
609590
equals(results[2], 'f2()');

0 commit comments

Comments
 (0)