Only expose globals if they are in use in the PaperScript code.

This commit is contained in:
Jürg Lehni 2014-01-04 22:22:19 +01:00
parent 227d511fc0
commit 22e62a0527

View file

@ -248,23 +248,28 @@ var PaperScript = Base.exports.PaperScript = (function() {
// undefined arguments, so that their name exists, rather than // undefined arguments, so that their name exists, rather than
// injecting a code line that defines them as variables. // injecting a code line that defines them as variables.
// They are exported again at the end of the function. // They are exported again at the end of the function.
handlers = ['onFrame', 'onResize'].concat(toolHandlers); handlers = ['onFrame', 'onResize'].concat(toolHandlers),
code = compile(code); // compile a list of paramter names for all variables that need to
// compile a list of paramter names for all variables that need to // appear as globals inside the script. At the same time, also
// appear as globals inside the script. At the same time, also collect // collect their values, so we can pass them on as arguments in the
// their values, so we can pass them on as arguments in the function // function call.
// call. params = [],
var params = ['_$_', '$_', 'view', 'tool'], args = [],
args = [_$_, $_ , view, tool],
func; func;
// Look through all enumerable properties on the scope and expose these code = compile(code);
// too as pseudo-globals. function expose(scope, hidden) {
for (var key in scope) { // Look through all enumerable properties on the scope and expose
if (!/^_/.test(key)) { // these too as pseudo-globals, but only if they seem to be in use.
params.push(key); for (var key in scope) {
args.push(scope[key]); if ((hidden || !/^_/.test(key)) && new RegExp(
'\\b' + key.replace(/\$/g, '\\$') + '\\b').test(code)) {
params.push(key);
args.push(scope[key]);
}
} }
} }
expose({ _$_: _$_, $_: $_, view: view, tool: tool }, true);
expose(scope);
// Finally define the handler variable names as parameters and compose // Finally define the handler variable names as parameters and compose
// the string describing the properties for the returned object at the // the string describing the properties for the returned object at the
// end of the code execution, so we can retrieve their values from the // end of the code execution, so we can retrieve their values from the