From 8e299b09cc4a16697ea754241cf31972437ec970 Mon Sep 17 00:00:00 2001 From: Michael Diolosa Date: Mon, 5 Jan 2015 15:40:04 -0500 Subject: [PATCH] Fix issue #3498 --- lib/coffee-script/coffee-script.js | 27 +++++++++++++++------------ src/coffee-script.coffee | 13 ++++++++----- test/eval.coffee | 3 ++- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index e619d667..f5928ec4 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -120,24 +120,27 @@ }; exports["eval"] = function(code, options) { - var Module, Script, js, k, o, r, sandbox, v, _i, _len, _module, _ref, _ref1, _require; + var Module, createContext, isContext, js, k, o, r, sandbox, v, _i, _len, _module, _ref, _ref1, _ref2, _ref3, _require; if (options == null) { options = {}; } if (!(code = code.trim())) { return; } - Script = vm.Script; - if (Script) { + createContext = (_ref = vm.Script.createContext) != null ? _ref : vm.createContext; + isContext = (_ref1 = vm.isContext) != null ? _ref1 : function(ctx) { + return options.sandbox instanceof createContext().constructor; + }; + if (createContext) { if (options.sandbox != null) { - if (options.sandbox instanceof Script.createContext().constructor) { + if (isContext(options.sandbox)) { sandbox = options.sandbox; } else { - sandbox = Script.createContext(); - _ref = options.sandbox; - for (k in _ref) { - if (!__hasProp.call(_ref, k)) continue; - v = _ref[k]; + sandbox = createContext(); + _ref2 = options.sandbox; + for (k in _ref2) { + if (!__hasProp.call(_ref2, k)) continue; + v = _ref2[k]; sandbox[k] = v; } } @@ -154,9 +157,9 @@ return Module._load(path, _module, true); }; _module.filename = sandbox.__filename; - _ref1 = Object.getOwnPropertyNames(require); - for (_i = 0, _len = _ref1.length; _i < _len; _i++) { - r = _ref1[_i]; + _ref3 = Object.getOwnPropertyNames(require); + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + r = _ref3[_i]; if (r !== 'paths') { _require[r] = require[r]; } diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 74c82946..ef711238 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -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 @@ -303,4 +307,3 @@ Error.prepareStackTrace = (err, stack) -> " at #{formatSourcePosition frame, getSourceMapping}" "#{err.toString()}\n#{frames.join '\n'}\n" - diff --git a/test/eval.coffee b/test/eval.coffee index d42a704f..4b6b9add 100644 --- a/test/eval.coffee +++ b/test/eval.coffee @@ -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!'