Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jashkenas/coffee-script
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jul 6, 2011
2 parents c4324f1 + b9c3e0e commit 83806a4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 54 deletions.
59 changes: 41 additions & 18 deletions lib/coffee-script.js

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

25 changes: 8 additions & 17 deletions lib/repl.js

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

36 changes: 25 additions & 11 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,33 @@ 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
_ = sandbox._
returnValue = Script.runInContext js, sandbox
sandbox._ = _ if returnValue is undefined
returnValue

# Instantiate a Lexer for our use here.
lexer = new Lexer
Expand Down
15 changes: 7 additions & 8 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 Expand Up @@ -102,6 +99,8 @@ else
repl = readline.createInterface stdin, stdout, autocomplete

repl.setPrompt 'coffee> '
repl.on 'close', -> stdin.destroy()
repl.on 'close', ->
process.stdout.write '\n'
stdin.destroy()
repl.on 'line', run
repl.prompt()

0 comments on commit 83806a4

Please sign in to comment.