diff options
Diffstat (limited to 'lib/vm.js')
-rw-r--r-- | lib/vm.js | 68 |
1 files changed, 40 insertions, 28 deletions
@@ -19,37 +19,49 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -var binding = process.binding('evals'); - -module.exports = Script; -Script.Script = Script; +var binding = process.binding('contextify'); +var Script = binding.ContextifyScript; var util = require('util'); -function Script(code, ctx, filename) { - if (!(this instanceof Script)) { - return new Script(code, ctx, filename); +// The binding provides a few useful primitives: +// - ContextifyScript(code, [filename]), with methods: +// - runInThisContext() +// - runInContext(sandbox, [timeout]) +// - makeContext(sandbox) +// From this we build the entire documented API. + +Script.prototype.runInNewContext = function(initSandbox, timeout) { + var context = exports.createContext(initSandbox); + return this.runInContext(context, timeout); +}; + +exports.Script = Script; + +exports.createScript = function(code, filename) { + return new Script(code, filename); +}; + +exports.createContext = function(initSandbox) { + if (util.isUndefined(initSandbox)) { + initSandbox = {}; } - var ns = new binding.NodeScript(code, ctx, filename); - - // bind all methods to this Script object - Object.keys(binding.NodeScript.prototype).forEach(function(f) { - if (util.isFunction(binding.NodeScript.prototype[f])) { - this[f] = function() { - if (!(this instanceof Script)) { - throw new TypeError('invalid call to ' + f); - } - return ns[f].apply(ns, arguments); - }; - } - }, this); -} - -Script.createScript = function(code, ctx, name) { - return new Script(code, ctx, name); + binding.makeContext(initSandbox); + + return initSandbox; +}; + +exports.runInContext = function(code, sandbox, filename, timeout) { + var script = exports.createScript(code, filename); + return script.runInContext(sandbox, timeout); }; -Script.createContext = binding.NodeScript.createContext; -Script.runInContext = binding.NodeScript.runInContext; -Script.runInThisContext = binding.NodeScript.runInThisContext; -Script.runInNewContext = binding.NodeScript.runInNewContext; +exports.runInNewContext = function(code, sandbox, filename, timeout) { + var script = exports.createScript(code, filename); + return script.runInNewContext(sandbox, timeout); +}; + +exports.runInThisContext = function(code, filename, timeout) { + var script = exports.createScript(code, filename); + return script.runInThisContext(timeout); +}; |