Navigation Menu

Skip to content

Commit

Permalink
Fixes #1035, #1425, and #1444: (another) overhaul of REPL and
Browse files Browse the repository at this point in the history
CoffeeScript.eval. Instead of writing about all the changes and why I
made those decisions, I'll just answer any questions in the commit
comments, so add a commit comment if you want to question anything.
Thanks to @TrevorBurnham and @satyr for their help/contributions. Also,
closes #1487. And still no REPL tests...
  • Loading branch information
michaelficarra committed Jul 7, 2011
1 parent 4ff0035 commit 40ee30e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
60 changes: 41 additions & 19 deletions lib/coffee-script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 7 additions & 17 deletions lib/repl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 25 additions & 12 deletions src/coffee-script.coffee
Expand Up @@ -8,7 +8,8 @@

fs = require 'fs'
path = require 'path'
vm = require 'vm'
{Script} = require 'vm'
Module = require 'module'
{Lexer,RESERVED} = require './lexer'
{parser} = require './parser'

Expand Down Expand Up @@ -78,20 +79,32 @@ 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[g] = global[g] for g in Object.getOwnPropertyNames global
sandbox.global = sandbox
sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox
sandbox = Script.createContext()
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
if options.sandbox?
if options.sandbox instanceof sandbox.constructor
sandbox = options.sandbox
else
sandbox[k] = v for own k, v of options.sandbox
sandbox.__filename = options.filename || 'eval'
sandbox.__dirname = path.dirname sandbox.__filename
o = {}; o[k] = v for k, v of options
# define module/require only if they chose not to specify their own
unless sandbox.module or sandbox.require
Module = require 'module'
sandbox.module = _module = new Module(options.modulename || 'eval')
sandbox.require = _require = (path) -> Module._load path, _module
_module.filename = sandbox.__filename
_require[r] = require[r] for r in Object.getOwnPropertyNames require
# use the same hack node currently uses for their own REPL
_require.paths = _module.paths = Module._nodeModulePaths process.cwd()
_require.resolve = (request) -> Module._resolveFilename request, _module
o = {}
o[k] = v for own k, v of options
o.bare = on # ensure return value
js = compile "_=(#{code}\n)", o
vm.runInNewContext js, sandbox, sandbox.__filename
js = compile "(#{code}\n)", o
_ = Script.runInContext js, sandbox
sandbox._ = _ if _?
_

# Instantiate a Lexer for our use here.
lexer = new Lexer
Expand Down
11 changes: 4 additions & 7 deletions src/repl.coffee
Expand Up @@ -9,6 +9,7 @@ CoffeeScript = require './coffee-script'
readline = require 'readline'
{inspect} = require 'util'
{Script} = require 'vm'
Module = require 'module'

# REPL Setup

Expand All @@ -32,12 +33,8 @@ 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 = Script.createContext()
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
(buffer) ->
code = backlog += '\n' + buffer.toString()
if code[code.length - 1] is '\\'
Expand All @@ -46,8 +43,8 @@ run = do ->
try
val = CoffeeScript.eval code, {
sandbox,
bare: on,
filename: 'repl'
modulename: 'repl'
}
unless val is undefined
process.stdout.write inspect(val, no, 2, enableColours) + '\n'
Expand Down

0 comments on commit 40ee30e

Please sign in to comment.