Skip to content

Commit be55e2a

Browse files
committed
Improve line number info for error messages
(Slightly.) Closes marijnh#7
1 parent 9ab2944 commit be55e2a

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

html/js/sandbox.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
(function() {
2-
"use strict";
1+
(function() {"use strict"; function timeout(win, f, ms) { win.__setTimeout(f, ms); };
2+
// The above is a kludge to make sure setTimeout calls are made from
3+
// line 1, which is where FF will start counting for its line numbers.
4+
5+
function parseStack(stack) {
6+
var found = [], m;
7+
var re = /([\w$]*)@.*?:(\d+)|\bat (?:([^\s(]+) \()?.*?:(\d+)/g;
8+
while (m = re.exec(stack)) {
9+
found.push({fn: m[1] || m[3] || null,
10+
line: m[2] || m[4]});
11+
}
12+
console.log(stack);
13+
return found;
14+
}
15+
function frameString(frame) {
16+
return "line " + frame.line + (frame.fn ? " in function " + frame.fn : "");
17+
}
318

419
var SandBox = window.SandBox = function(options) {
520
var frame = this.frame = document.createElement("iframe");
@@ -55,7 +70,7 @@
5570
this.startedAt = Date.now();
5671
this.extraSecs = 1;
5772
this.win.__c = 0;
58-
this.win.__setTimeout(preprocess(code, this), 0);
73+
timeout(this.win, preprocess(code, this), 0);
5974
},
6075
show: function(html, output) {
6176
var scriptTags = [], sandbox = this, doc = this.win.document;
@@ -96,9 +111,18 @@
96111
},
97112
error: function(exception) {
98113
if (!this.output) throw exception;
99-
var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(exception.stack);
100-
if (pos && pos[1].match(/sandbox/)) pos = null;
101-
this.output.out("error", [String(exception) + (pos ? " (line " + pos[2] + ")" : "")]);
114+
var stack = parseStack(exception.stack);
115+
this.output.out("error", [String(exception) + (stack.length ? " (" + frameString(stack[0]) + ")" : "")]);
116+
if (stack.length > 1) {
117+
this.output.div.lastChild.appendChild(document.createTextNode(" "));
118+
var mark = this.output.div.lastChild.appendChild(document.createElement("span"));
119+
mark.innerHTML = "…";
120+
mark.className = "sandbox-output-etc";
121+
mark.addEventListener("click", function(e) {
122+
mark.className = "";
123+
mark.innerHTML = "\n called from " + stack.slice(1).map(frameString).join("\n called from ");
124+
});
125+
}
102126
}
103127
};
104128

0 commit comments

Comments
 (0)