Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Wrap NodeScript binding class in JavaScript layer
Browse files Browse the repository at this point in the history
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 44daa98
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/vm.js
Expand Up @@ -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.