Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #3498 #3771

Merged
merged 1 commit into from
Jan 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions lib/coffee-script/coffee-script.js

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

13 changes: 8 additions & 5 deletions src/coffee-script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,17 @@ exports.run = (code, options = {}) ->
# The CoffeeScript REPL uses this to run the input.
exports.eval = (code, options = {}) ->
return unless code = code.trim()
Script = vm.Script
if Script
createContext = vm.Script.createContext ? vm.createContext

isContext = vm.isContext ? (ctx) ->
options.sandbox instanceof createContext().constructor

if createContext
if options.sandbox?
if options.sandbox instanceof Script.createContext().constructor
if isContext options.sandbox
sandbox = options.sandbox
else
sandbox = Script.createContext()
sandbox = createContext()
sandbox[k] = v for own k, v of options.sandbox
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
else
Expand Down Expand Up @@ -303,4 +307,3 @@ Error.prepareStackTrace = (err, stack) ->
" at #{formatSourcePosition frame, getSourceMapping}"

"#{err.toString()}\n#{frames.join '\n'}\n"

3 changes: 2 additions & 1 deletion test/eval.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ if vm = require? 'vm'
eq fhqwhgads, 'global superpower!'

test "CoffeeScript.eval can run in, and modify, a Script context sandbox", ->
sandbox = vm.Script.createContext()
createContext = vm.Script.createContext ? vm.createContext
sandbox = createContext()
sandbox.foo = 'bar'
code = '''
global.foo = 'not bar!'
Expand Down