Prevent leakage of bundled Acorn into window.acorn

And fix Acorn loading under load.js
This commit is contained in:
Jürg Lehni 2017-03-10 23:56:06 +01:00
parent 62a0c9c36b
commit 6ee59cd46e

View file

@ -15,18 +15,31 @@
* @namespace
*/
Base.exports.PaperScript = function() {
// Locally turn of exports and define for inlined acorn.
// Just declaring the local vars is enough, as they will be undefined.
var exports, define,
acorn = this.acorn;
// Try importing an outside version of acorn first, and fall back on the
// internal v0.5, which is kept at that version of small size, for now.
if (typeof require !== 'undefined') {
// `this` == global scope, as the function is called with `.call(this);`
var global = this,
// See if there is a global Acorn in the browser already.
acorn = global.acorn;
// Also try importing an outside version of Acorn, and fall back on the
// internal v0.5.0, which is kept at that version of small size, for now.
if (!acorn && typeof require !== 'undefined') {
try { acorn = require('acorn'); } catch(e) {}
}
// If no Acorn was found, load the bundled version.
if (!acorn) {
/*#*/ include('../../node_modules/acorn/acorn.min.js', { exports: false });
acorn = this.acorn;
// Provide our own local exports and module object so that Acorn gets
// assigned to it and ends up in the local acorn object.
var exports, module;
acorn = exports = module = {};
/*#*/ include('../../node_modules/acorn/acorn.js', { exports: false });
// Clear object again if it wasn't loaded here; for load.js, see below.
if (!acorn.version)
acorn = null;
}
function parse(code, options) {
// NOTE: When using load.js, Acorn will end up in global.acorn and will
// not be immediately available, so we need to check for it here again:
return (acorn || global.acorn).parse(code, options);
}
// Operators to overload
@ -96,10 +109,6 @@ Base.exports.PaperScript = function() {
// AST Helpers
function parse(code, options) {
return acorn.parse(code, options);
}
/**
* Compiles PaperScript code into JavaScript code.
*