Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Wrap NodeScript binding class in JavaScript layer

This makes it easy to prevent errors where Script methods
are called on non-script objects, resulting in Assertion failures.
  • Loading branch information...
isaacs committed Feb 28, 2012
1 parent 29463cb commit 44daa9836b5bb4cfb8434b35bb1a12fc324223aa
Showing with 29 additions and 7 deletions.
  1. +29 −7 lib/vm.js
@@ -21,12 +21,34 @@

var binding = process.binding('evals');

exports.Script = binding.NodeScript;
exports.createScript = function(code, ctx, name) {
return new exports.Script(code, ctx, name);
module.exports = Script;
Script.Script = Script;

function Script(code, ctx, filename) {
if (!(this instanceof Script)) {
return new Script(code, ctx, filename);
}

var ns = new binding.NodeScript(code, ctx, filename);

// bind all methods to this Script object
Object.keys(binding.NodeScript.prototype).forEach(function(f) {
if (typeof binding.NodeScript.prototype[f] === 'function') {
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);
};

exports.createContext = binding.NodeScript.createContext;
exports.runInContext = binding.NodeScript.runInContext;
exports.runInThisContext = binding.NodeScript.runInThisContext;
exports.runInNewContext = binding.NodeScript.runInNewContext;
Script.createContext = binding.NodeScript.createContext;
Script.runInContext = binding.NodeScript.runInContext;
Script.runInThisContext = binding.NodeScript.runInThisContext;
Script.runInNewContext = binding.NodeScript.runInNewContext;

0 comments on commit 44daa98

Please sign in to comment.
You can’t perform that action at this time.