Improve error handling of PaperScripts on Firefox.

This commit is contained in:
Jürg Lehni 2013-11-27 19:07:02 +01:00
parent ff538870e0
commit ed8de613c4
2 changed files with 56 additions and 13 deletions

View file

@ -76,12 +76,30 @@ var Callback = {
var handlers = this._handlers && this._handlers[type];
if (!handlers)
return false;
var args = [].slice.call(arguments, 1);
for (var i in handlers) {
// When the handler function returns false, prevent the default
// behaviour of the event by calling stop() on it.
if (handlers[i].apply(this, args) === false && event && event.stop)
event.stop();
var args = [].slice.call(arguments, 1),
PaperScript = paper.PaperScript,
handleException = PaperScript && PaperScript.handleException;
function callHandlers() {
for (var i in handlers) {
// When the handler function returns false, prevent the default
// behaviour of the event by calling stop() on it.
if (handlers[i].apply(this, args) === false
&& event && event.stop)
event.stop();
}
}
// See PaperScript.handleException for an explanation of the following.
// Firefox is to blame for the necessity of this...
if (handleException) {
try {
callHandlers();
} catch (e) {
handleException(e);
}
} else {
callHandlers();
}
return true;
},

View file

@ -262,18 +262,43 @@ paper.PaperScope.prototype.PaperScript = (function(root) {
onMouseDown, onMouseUp, onMouseDrag, onMouseMove,
onKeyDown, onKeyUp, onFrame, onResize;
code = compile(code);
/*#*/ if (options.environment == 'browser') {
if (root.InstallTrigger) { // Firefox
// Add a semi-colon at the start so Firefox doesn't swallow
// empty lines and shift error messages.
code = ';' + code;
// On Firefox, all error numbers inside evaled code are
// relative to the line where the eval happened. Totally
// silly, but that's how it is. So we're exposing it through
// PaperScript.lineNumberBase, to remove it again from
// reported errors:
PaperScript.lineNumberBase = new Error().lineNumber + 1;
// silly, but that's how it is. So we're calculating the
// base of lineNumbers, to remove it again from reported
// errors. Luckily, Firefox is the only browser where we can
// define the lineNumber for exceptions.
var handle = PaperScript.handleException;
if (!handle) {
handle = PaperScript.handleException = function(e) {
throw e.lineNumber >= lineNumber
? new Error(e.message, e.fileName,
e.lineNumber - lineNumber)
: e;
}
// We're using a crazy hack to detect wether the library
// is minified or not: By generating a second error on
// the 2nd line and using the difference in line numbers
// to calculate the offset to the eval, it works in both
// casees.
var lineNumber = new Error().lineNumber;
lineNumber += (new Error().lineNumber - lineNumber) * 3;
}
try {
// Add a semi-colon at the start so Firefox doesn't
// swallow empty lines and shift error messages.
res = eval(';' + code);
} catch (e) {
handle(e);
}
} else {
res = eval(code);
}
/*#*/ } else { // !options.environment == 'browser'
res = eval(code);
/*#*/ } // !options.environment == 'browser'
// Only look for tool handlers if something resembling their
// name is contained in the code.
if (/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)) {