mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Prevent leakage of bundled Acorn into window.acorn
And fix Acorn loading under load.js
This commit is contained in:
parent
62a0c9c36b
commit
6ee59cd46e
1 changed files with 22 additions and 13 deletions
|
@ -15,18 +15,31 @@
|
||||||
* @namespace
|
* @namespace
|
||||||
*/
|
*/
|
||||||
Base.exports.PaperScript = function() {
|
Base.exports.PaperScript = function() {
|
||||||
// Locally turn of exports and define for inlined acorn.
|
// `this` == global scope, as the function is called with `.call(this);`
|
||||||
// Just declaring the local vars is enough, as they will be undefined.
|
var global = this,
|
||||||
var exports, define,
|
// See if there is a global Acorn in the browser already.
|
||||||
acorn = this.acorn;
|
acorn = global.acorn;
|
||||||
// Try importing an outside version of acorn first, and fall back on the
|
// Also try importing an outside version of Acorn, and fall back on the
|
||||||
// internal v0.5, which is kept at that version of small size, for now.
|
// internal v0.5.0, which is kept at that version of small size, for now.
|
||||||
if (typeof require !== 'undefined') {
|
if (!acorn && typeof require !== 'undefined') {
|
||||||
try { acorn = require('acorn'); } catch(e) {}
|
try { acorn = require('acorn'); } catch(e) {}
|
||||||
}
|
}
|
||||||
|
// If no Acorn was found, load the bundled version.
|
||||||
if (!acorn) {
|
if (!acorn) {
|
||||||
/*#*/ include('../../node_modules/acorn/acorn.min.js', { exports: false });
|
// Provide our own local exports and module object so that Acorn gets
|
||||||
acorn = this.acorn;
|
// 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
|
// Operators to overload
|
||||||
|
@ -96,10 +109,6 @@ Base.exports.PaperScript = function() {
|
||||||
|
|
||||||
// AST Helpers
|
// AST Helpers
|
||||||
|
|
||||||
function parse(code, options) {
|
|
||||||
return acorn.parse(code, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compiles PaperScript code into JavaScript code.
|
* Compiles PaperScript code into JavaScript code.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue