From e2a548a534a55525eac00dd321a096d4dedef080 Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 5 Jul 2011 22:54:30 -0400 Subject: [PATCH 1/2] Fixes #1035: `eval` now uses a real `Module` instance --- lib/coffee-script.js | 31 ++++++++++++++++--------------- lib/repl.js | 16 ++-------------- src/coffee-script.coffee | 16 +++++++++------- src/repl.coffee | 7 +------ 4 files changed, 28 insertions(+), 42 deletions(-) diff --git a/lib/coffee-script.js b/lib/coffee-script.js index 04620e1862..2fd8a106c1 100755 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -1,8 +1,9 @@ (function() { - var Lexer, RESERVED, compile, fs, lexer, parser, path, vm, _ref; + var Lexer, Module, RESERVED, compile, fs, lexer, parser, path, vm, _ref; fs = require('fs'); path = require('path'); vm = require('vm'); + Module = require('module').Module; _ref = require('./lexer'), Lexer = _ref.Lexer, RESERVED = _ref.RESERVED; parser = require('./parser').parser; if (require.extensions) { @@ -45,7 +46,7 @@ } }; exports.run = function(code, options) { - var Module, mainModule; + var mainModule; mainModule = require.main; mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.'; mainModule.moduleCache && (mainModule.moduleCache = {}); @@ -60,31 +61,31 @@ } }; exports.eval = function(code, options) { - var g, js, k, o, sandbox, v, _i, _len, _ref2; + var g, js, k, o, sandbox, v, _i, _len, _ref2, _ref3; if (options == null) { options = {}; } if (!(code = code.trim())) { return; } - sandbox = options.sandbox; - if (!sandbox) { - sandbox = { - require: require, - module: { - exports: {} - } + sandbox = (_ref2 = options.sandbox) != null ? _ref2 : {}; + if (!(sandbox && sandbox.require)) { + sandbox.module = new Module('repl'); + sandbox.require = function(path) { + return Module._load(path, sandbox.module); }; - _ref2 = Object.getOwnPropertyNames(global); - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - g = _ref2[_i]; + _ref3 = Object.getOwnPropertyNames(global); + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + g = _ref3[_i]; sandbox[g] = global[g]; } sandbox.global = sandbox; sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox; + sandbox.global = sandbox; + sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox; + sandbox.__filename = sandbox.module.filename = options.filename || 'eval'; + sandbox.__dirname = path.dirname(sandbox.__filename); } - sandbox.__filename = options.filename || 'eval'; - sandbox.__dirname = path.dirname(sandbox.__filename); o = {}; for (k in options) { v = options[k]; diff --git a/lib/repl.js b/lib/repl.js index 5f468bc641..059ec94156 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -16,20 +16,8 @@ }; backlog = ''; run = (function() { - var g, sandbox, _i, _len, _ref; - sandbox = { - require: require, - module: { - exports: {} - } - }; - _ref = Object.getOwnPropertyNames(global); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - g = _ref[_i]; - sandbox[g] = global[g]; - } - sandbox.global = sandbox; - sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox; + var sandbox; + sandbox = {}; return function(buffer) { var code, val; code = backlog += '\n' + buffer.toString(); diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 9c6c2e6d9a..f99ae32c89 100755 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -9,6 +9,7 @@ fs = require 'fs' path = require 'path' vm = require 'vm' +{Module} = require 'module' {Lexer,RESERVED} = require './lexer' {parser} = require './parser' @@ -78,16 +79,17 @@ exports.run = (code, options) -> # The CoffeeScript REPL uses this to run the input. exports.eval = (code, options = {}) -> return unless code = code.trim() - sandbox = options.sandbox - unless sandbox - sandbox = - require: require - module : { exports: {} } + sandbox = options.sandbox ? {} + unless sandbox and sandbox.require + sandbox.module = new Module('repl') + sandbox.require = (path) -> Module._load path, sandbox.module sandbox[g] = global[g] for g in Object.getOwnPropertyNames global sandbox.global = sandbox sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox - sandbox.__filename = options.filename || 'eval' - sandbox.__dirname = path.dirname sandbox.__filename + sandbox.global = sandbox + sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox + sandbox.__filename = sandbox.module.filename = options.filename || 'eval' + sandbox.__dirname = path.dirname sandbox.__filename o = {}; o[k] = v for k, v of options o.bare = on # ensure return value js = compile "_=(#{code}\n)", o diff --git a/src/repl.coffee b/src/repl.coffee index 1e42e0378d..039bbdd620 100644 --- a/src/repl.coffee +++ b/src/repl.coffee @@ -32,12 +32,7 @@ backlog = '' # Attempt to evaluate the command. If there's an exception, print it out instead # of exiting. run = do -> - sandbox = - require: require - module : { exports: {} } - sandbox[g] = global[g] for g in Object.getOwnPropertyNames global - sandbox.global = sandbox - sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox + sandbox = {} # properties are set by `CoffeeScript.eval` (buffer) -> code = backlog += '\n' + buffer.toString() if code[code.length - 1] is '\\' From e16e5ec4c377d0daed8fe6471a71c5039dae10ec Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Tue, 5 Jul 2011 23:18:42 -0400 Subject: [PATCH 2/2] Adding sandbox's missing require features --- lib/coffee-script.js | 8 +++++++- src/coffee-script.coffee | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/coffee-script.js b/lib/coffee-script.js index 2fd8a106c1..e8c9a5bd1b 100755 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -61,7 +61,7 @@ } }; exports.eval = function(code, options) { - var g, js, k, o, sandbox, v, _i, _len, _ref2, _ref3; + var g, js, k, o, sandbox, v, x, _i, _len, _ref2, _ref3; if (options == null) { options = {}; } @@ -74,6 +74,12 @@ sandbox.require = function(path) { return Module._load(path, sandbox.module); }; + for (x in require) { + sandbox.require[x] = require[x]; + } + sandbox.require.resolve = function(request) { + return Module._resolveFilename(request, sandbox.module); + }; _ref3 = Object.getOwnPropertyNames(global); for (_i = 0, _len = _ref3.length; _i < _len; _i++) { g = _ref3[_i]; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index f99ae32c89..10cb10ba31 100755 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -83,6 +83,8 @@ exports.eval = (code, options = {}) -> unless sandbox and sandbox.require sandbox.module = new Module('repl') sandbox.require = (path) -> Module._load path, sandbox.module + sandbox.require[x] = require[x] for x of require + sandbox.require.resolve = (request) -> Module._resolveFilename request, sandbox.module sandbox[g] = global[g] for g in Object.getOwnPropertyNames global sandbox.global = sandbox sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox