From 4b53d558f135a4a31a9a6bedf037be978ac17be1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCrg=20Lehni?= <juerg@scratchdisk.com>
Date: Mon, 24 Jun 2013 04:40:07 -0700
Subject: [PATCH] Refactor PaperScript code so it can be moved outside of main
 paper scope.

Allowing for better minifaction and the potential use of strict mode due to absence of with() statements inside the main paper scope.
---
 src/core/PaperScope.js  |  2 +-
 src/core/PaperScript.js | 28 ++++++++++++++++------------
 src/export.js           |  9 +++++----
 src/paper.js            |  6 ++++--
 4 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/src/core/PaperScope.js b/src/core/PaperScope.js
index 85289c32..481b92a8 100644
--- a/src/core/PaperScope.js
+++ b/src/core/PaperScope.js
@@ -120,7 +120,7 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
 	 */
 
 	evaluate: function(code) {
-		var res = PaperScript.evaluate(code, this);
+		var res = paper.PaperScript.evaluate(code, this);
 		View.updateFocus();
 		return res;
 	},
diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js
index c7aef9ed..9210e419 100644
--- a/src/core/PaperScript.js
+++ b/src/core/PaperScript.js
@@ -14,16 +14,19 @@
  * @name PaperScript
  * @namespace
  */
+// Note that due to the use of with(), PaperScript gets compiled outside the
+// main paper scope, and is added to the PaperScope class. This allows for
+// better minification and the future use of strict mode once it makes sense
+// in terms of performance.
+paper.PaperScope.prototype.PaperScript = new function() {
+	// Locally override define, so acorn.js does not export itself
+	var define = null;
+	/*#*/ if (options.parser == 'acorn') {
+	/*#*/ include('../../lib/acorn-min.js');
+	/*#*/ } else if (options.parser == 'esprima') {
+	/*#*/ include('../../lib/esprima-min.js');
+	/*#*/ }
 
-// Locally override define, so acorn.js does not export itself
-var define = null;
-/*#*/ if (options.parser == 'acorn') {
-/*#*/ include('../../lib/acorn-min.js');
-/*#*/ } else if (options.parser == 'esprima') {
-/*#*/ include('../../lib/esprima-min.js');
-/*#*/ }
- 
-var PaperScript = new function() {
 	// Operators to overload
 
 	var binaryOperators = {
@@ -230,7 +233,7 @@ var PaperScript = new function() {
 				// 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)) {
-					Base.each(Tool.prototype._events, function(key) {
+					Base.each(paper.Tool.prototype._events, function(key) {
 						var value = eval(key);
 						if (value) {
 							// Use the getTool accessor that handles auto tool
@@ -273,7 +276,8 @@ var PaperScript = new function() {
 	}
 
 	function load() {
-		var scripts = document.getElementsByTagName('script');
+		var scripts = document.getElementsByTagName('script'),
+			PaperScope = paper.PaperScope;
 		for (var i = 0, l = scripts.length; i < l; i++) {
 			var script = scripts[i];
 			// Only load this script if it not loaded already.
@@ -313,7 +317,7 @@ var PaperScript = new function() {
 		// Handle it asynchronously
 		setTimeout(load);
 	} else {
-		DomEvent.add(window, { load: load });
+		paper.DomEvent.add(window, { load: load });
 	}
 
 	return {
diff --git a/src/export.js b/src/export.js
index abc438ed..541be03c 100644
--- a/src/export.js
+++ b/src/export.js
@@ -10,12 +10,13 @@
  * All rights reserved.
  */
 
-// First add Base, PaperScript and Numerical to exports, then inject all exports
-// into PaperScope, and create the initial paper object, all in one statement:
+// First add Base and Numerical to exports, then inject all exports into
+// PaperScope, and create the initial paper object, all in one statement:
 paper = new (PaperScope.inject(Base.merge(Base.exports, {
 	// Mark fields as enumeralbe so PaperScope.inject can pick them up
 	enumerable: true,
 	Base: Base,
-	PaperScript: PaperScript,
-	Numerical: Numerical
+	Numerical: Numerical,
+	DomElement: DomElement,
+	DomEvent: DomEvent
 })))();
diff --git a/src/paper.js b/src/paper.js
index fc5de717..c86c4229 100644
--- a/src/paper.js
+++ b/src/paper.js
@@ -124,12 +124,14 @@ var paper = new function() {
 /*#*/ include('svg/SVGImport.js');
 /*#*/ } // options.svg
 
-/*#*/ include('core/PaperScript.js');
-
 /*#*/ include('export.js');
 return paper;
 };
 
+// include PaperScript separately outside the main paper scope, due to its use
+// of with(). This also simplifies making its inclusion optional.
+/*#*/ include('core/PaperScript.js');
+
 // Support AMD (e.g. require.js)
 if (typeof define === 'function' && define.amd)
 	define(paper);