Two forms of scoping are required when evaluating PaperScripts: with(){} and a function scope, so local vars are not trying to override properties on the scope through with(){} (e.g. var view = ...).

This commit is contained in:
Jürg Lehni 2011-05-16 11:00:33 +01:00
parent 2773ef693a
commit 71bf4fdbca

View file

@ -153,51 +153,57 @@ var PaperScript = this.PaperScript = new function() {
} }
} }
//#endif // BROWSER //#endif // BROWSER
with (scope) { // Safe one indentation by grouping try and with var doc = scope.document,
paper = scope; view = doc.activeView,
var doc = scope.document, // TODO: Add support for multiple tools
view = doc.activeView, tool = scope.tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
// TODO: Add support for multiple tools && new Tool(null, scope),
tool = scope.tool = res;
/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code) // Define variables for potential handlers, so eval() calls below to
&& new Tool(null, scope); // fetch their values do not require try-catch around them.
// Define variables for potential handlers, so eval() calls below to // Set currently active scope.
// fetch their values do not require try-catch around them. paper = scope;
var onEditOptions, onSelect, onDeselect, onReselect, onMouseDown, // Use with(){} in order to make the scope the current 'global' scope
onMouseUp, onMouseDrag, onMouseMove, onKeyDown, onKeyUp, // instead of window.
onFrame, onResize; with (scope) {
var res = eval(compile(code)); // Within this, use a function scope, so local variables to not try
if (tool) { // and set themselves on the paper object.
// We could do this instead to avoid eval(), but it's more code: (function() {
// tool.onEditOptions = onEditOptions; var onEditOptions, onSelect, onDeselect, onReselect, onMouseDown,
// tool.onSelect = onSelect; onMouseUp, onMouseDrag, onMouseMove, onKeyDown, onKeyUp,
// tool.onDeselect = onDeselect; onFrame, onResize,
// tool.onReselect = onReselect; handlers = [ 'onEditOptions', 'onSelect', 'onDeselect',
// tool.onMouseDown = onMouseDown;
// tool.onMouseUp = onMouseUp;
// tool.onMouseDrag = onMouseDrag;
// tool.onMouseMove = onMouseMove;
// tool.onKeyDown = onKeyDown;
// tool.onKeyUp = onKeyUp;
Base.each(['onEditOptions', 'onSelect', 'onDeselect',
'onReselect', 'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onReselect', 'onMouseDown', 'onMouseUp', 'onMouseDrag',
'onMouseMove', 'onKeyDown', 'onKeyUp'], 'onMouseMove', 'onKeyDown', 'onKeyUp'];
function(key) { res = eval(compile(code));
if (tool) {
// We could do this instead to avoid eval(), but it's longer
// tool.onEditOptions = onEditOptions;
// tool.onSelect = onSelect;
// tool.onDeselect = onDeselect;
// tool.onReselect = onReselect;
// tool.onMouseDown = onMouseDown;
// tool.onMouseUp = onMouseUp;
// tool.onMouseDrag = onMouseDrag;
// tool.onMouseMove = onMouseMove;
// tool.onKeyDown = onKeyDown;
// tool.onKeyUp = onKeyUp;
Base.each(handlers, function(key) {
tool[key] = eval(key); tool[key] = eval(key);
} });
);
}
if (view) {
view.onResize = onResize;
if (onFrame) {
view.setOnFrame(onFrame);
} else {
// Automatically draw view at the end.
view.draw();
} }
} if (view) {
return res; view.onResize = onResize;
if (onFrame) {
view.setOnFrame(onFrame);
} else {
// Automatically draw view at the end.
view.draw();
}
}
}).call(scope);
} }
return res;
} }
//#ifdef BROWSER //#ifdef BROWSER