mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Blacklisting certain globals from REPL rather than whitelisting (fixes #1654)
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
(function() {
|
||||
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, g, getCompletions, inspect, nonContextGlobals, readline, repl, run, sandbox, stdin, stdout, _i, _len;
|
||||
var ACCESSOR, CoffeeScript, Module, REPL_PROMPT, REPL_PROMPT_CONTINUATION, SIMPLEVAR, Script, autocomplete, backlog, completeAttribute, completeVariable, enableColours, error, excludedGlobals, g, getCompletions, inspect, nonContextGlobals, readline, repl, run, sandbox, stdin, stdout;
|
||||
var __hasProp = Object.prototype.hasOwnProperty, __indexOf = Array.prototype.indexOf || function(item) {
|
||||
for (var i = 0, l = this.length; i < l; i++) {
|
||||
if (__hasProp.call(this, i) && this[i] === item) return i;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
CoffeeScript = require('./coffee-script');
|
||||
readline = require('readline');
|
||||
inspect = require('util').inspect;
|
||||
@@ -18,10 +24,10 @@
|
||||
};
|
||||
backlog = '';
|
||||
sandbox = Script.createContext();
|
||||
excludedGlobals = ['global', 'GLOBAL', 'root'];
|
||||
nonContextGlobals = ['Buffer', 'console', 'process', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout'];
|
||||
for (_i = 0, _len = nonContextGlobals.length; _i < _len; _i++) {
|
||||
g = nonContextGlobals[_i];
|
||||
sandbox[g] = global[g];
|
||||
for (g in global) {
|
||||
if (__indexOf.call(excludedGlobals, g) < 0) sandbox[g] = global[g];
|
||||
}
|
||||
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
|
||||
run = function(buffer) {
|
||||
@@ -80,11 +86,11 @@
|
||||
if (free != null) {
|
||||
vars = Script.runInContext('Object.getOwnPropertyNames(this)', sandbox);
|
||||
keywords = (function() {
|
||||
var _j, _len2, _ref2, _results;
|
||||
var _i, _len, _ref2, _results;
|
||||
_ref2 = CoffeeScript.RESERVED;
|
||||
_results = [];
|
||||
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
|
||||
r = _ref2[_j];
|
||||
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
||||
r = _ref2[_i];
|
||||
if (r.slice(0, 2) !== '__') _results.push(r);
|
||||
}
|
||||
return _results;
|
||||
@@ -95,10 +101,10 @@
|
||||
}
|
||||
};
|
||||
getCompletions = function(prefix, candidates) {
|
||||
var el, _j, _len2, _results;
|
||||
var el, _i, _len, _results;
|
||||
_results = [];
|
||||
for (_j = 0, _len2 = candidates.length; _j < _len2; _j++) {
|
||||
el = candidates[_j];
|
||||
for (_i = 0, _len = candidates.length; _i < _len; _i++) {
|
||||
el = candidates[_i];
|
||||
if (el.indexOf(prefix) === 0) _results.push(el);
|
||||
}
|
||||
return _results;
|
||||
|
||||
@@ -33,12 +33,15 @@ backlog = ''
|
||||
|
||||
# The REPL context; must be visible outside `run` to allow for tab completion
|
||||
sandbox = Script.createContext()
|
||||
excludedGlobals = [
|
||||
'global', 'GLOBAL', 'root'
|
||||
]
|
||||
nonContextGlobals = [
|
||||
'Buffer', 'console', 'process'
|
||||
'setInterval', 'clearInterval'
|
||||
'setTimeout', 'clearTimeout'
|
||||
]
|
||||
sandbox[g] = global[g] for g in nonContextGlobals
|
||||
sandbox[g] = global[g] for g of global when g not in excludedGlobals
|
||||
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
|
||||
|
||||
# The main REPL function. **run** is called every time a line of code is entered.
|
||||
|
||||
Reference in New Issue
Block a user