diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 38ae9764..3dd34f4b 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -1,5 +1,5 @@ (function() { - var Lexer, RESERVED, compile, fs, lexer, parser, path, _ref; + var Lexer, RESERVED, compile, fs, lexer, parser, path, vm, _ref; var __hasProp = Object.prototype.hasOwnProperty; fs = require('fs'); @@ -10,6 +10,8 @@ parser = require('./parser').parser; + vm = require('vm'); + if (require.extensions) { require.extensions['.coffee'] = function(module, filename) { var content; @@ -74,14 +76,13 @@ var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref2, _ref3, _require; if (options == null) options = {}; if (!(code = code.trim())) return; - Script = require('vm').Script; + Script = vm.Script; if (Script) { - sandbox = Script.createContext(); - sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox; if (options.sandbox != null) { - if (options.sandbox instanceof sandbox.constructor) { + if (options.sandbox instanceof Script.createContext().constructor) { sandbox = options.sandbox; } else { + sandbox = Script.createContext(); _ref2 = options.sandbox; for (k in _ref2) { if (!__hasProp.call(_ref2, k)) continue; @@ -89,14 +90,17 @@ sandbox[k] = v; } } + sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox; + } else { + sandbox = global; } sandbox.__filename = options.filename || 'eval'; sandbox.__dirname = path.dirname(sandbox.__filename); - if (!(sandbox.module || sandbox.require)) { + if (!(sandbox !== global || sandbox.module || sandbox.require)) { Module = require('module'); sandbox.module = _module = new Module(options.modulename || 'eval'); sandbox.require = _require = function(path) { - return Module._load(path, _module); + return Module._load(path, _module, true); }; _module.filename = sandbox.__filename; _ref3 = Object.getOwnPropertyNames(require); @@ -118,10 +122,10 @@ } o.bare = true; js = compile(code, o); - if (Script) { - return Script.runInContext(js, sandbox); + if (sandbox === global) { + return vm.runInThisContext(js); } else { - return eval(js); + return vm.runInContext(js, sandbox); } }; diff --git a/lib/coffee-script/repl.js b/lib/coffee-script/repl.js index 6374a89e..b936a9b5 100644 --- a/lib/coffee-script/repl.js +++ b/lib/coffee-script/repl.js @@ -1,5 +1,5 @@ (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, getCompletions, inspect, readline, repl, run, stdin, stdout; CoffeeScript = require('./coffee-script'); @@ -31,17 +31,6 @@ backlog = ''; - sandbox = Script.createContext(); - - nonContextGlobals = ['Buffer', 'console', 'process', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout']; - - for (_i = 0, _len = nonContextGlobals.length; _i < _len; _i++) { - g = nonContextGlobals[_i]; - sandbox[g] = global[g]; - } - - sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox; - run = function(buffer) { var code, returnValue, _; if (!buffer.toString().trim() && !backlog) { @@ -58,14 +47,13 @@ repl.setPrompt(REPL_PROMPT); backlog = ''; try { - _ = sandbox._; + _ = global._; returnValue = CoffeeScript.eval("_=(" + code + "\n)", { - sandbox: sandbox, filename: 'repl', modulename: 'repl' }); if (returnValue === void 0) { - sandbox._ = _; + global._ = _; } else { process.stdout.write(inspect(returnValue, false, 2, enableColours) + '\n'); } @@ -88,7 +76,7 @@ if (match = text.match(ACCESSOR)) { all = match[0], obj = match[1], prefix = match[2]; try { - val = Script.runInContext(obj, sandbox); + val = Script.runInThisContext(obj); } catch (error) { return; } @@ -101,13 +89,13 @@ var completions, free, keywords, possibilities, r, vars, _ref; free = (_ref = text.match(SIMPLEVAR)) != null ? _ref[1] : void 0; if (free != null) { - vars = Script.runInContext('Object.getOwnPropertyNames(this)', sandbox); + vars = Script.runInThisContext('Object.getOwnPropertyNames(this)'); 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; @@ -119,10 +107,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;