2011-12-18 13:21:00 -05:00
|
|
|
if vm = require? 'vm'
|
2011-09-04 13:57:09 -04:00
|
|
|
|
2011-12-18 13:21:00 -05:00
|
|
|
test "CoffeeScript.eval runs in the global context by default", ->
|
|
|
|
global.punctuation = '!'
|
|
|
|
code = '''
|
|
|
|
global.fhqwhgads = "global superpower#{global.punctuation}"
|
|
|
|
'''
|
|
|
|
result = CoffeeScript.eval code
|
|
|
|
eq result, 'global superpower!'
|
|
|
|
eq fhqwhgads, 'global superpower!'
|
2011-12-24 07:04:34 -05:00
|
|
|
|
2011-12-18 13:21:00 -05:00
|
|
|
test "CoffeeScript.eval can run in, and modify, a Script context sandbox", ->
|
2015-01-05 15:40:04 -05:00
|
|
|
createContext = vm.Script.createContext ? vm.createContext
|
|
|
|
sandbox = createContext()
|
2011-11-08 18:01:45 -05:00
|
|
|
sandbox.foo = 'bar'
|
|
|
|
code = '''
|
|
|
|
global.foo = 'not bar!'
|
|
|
|
'''
|
|
|
|
result = CoffeeScript.eval code, {sandbox}
|
|
|
|
eq result, 'not bar!'
|
|
|
|
eq sandbox.foo, 'not bar!'
|
2011-12-24 07:04:34 -05:00
|
|
|
|
2011-12-18 13:21:00 -05:00
|
|
|
test "CoffeeScript.eval can run in, but cannot modify, an ordinary object sandbox", ->
|
|
|
|
sandbox = {foo: 'bar'}
|
|
|
|
code = '''
|
|
|
|
global.foo = 'not bar!'
|
|
|
|
'''
|
|
|
|
result = CoffeeScript.eval code, {sandbox}
|
|
|
|
eq result, 'not bar!'
|
2011-12-24 07:04:34 -05:00
|
|
|
eq sandbox.foo, 'bar'
|