Browse Source

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.
v0.9.1-release
isaacs 13 years ago
parent
commit
44daa9836b
  1. 36
      lib/vm.js

36
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;

Loading…
Cancel
Save