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.
This commit is contained in:
parent
29463cb60c
commit
44daa9836b
36
lib/vm.js
36
lib/vm.js
@ -21,12 +21,34 @@
|
|||||||
|
|
||||||
var binding = process.binding('evals');
|
var binding = process.binding('evals');
|
||||||
|
|
||||||
exports.Script = binding.NodeScript;
|
module.exports = Script;
|
||||||
exports.createScript = function(code, ctx, name) {
|
Script.Script = Script;
|
||||||
return new exports.Script(code, ctx, name);
|
|
||||||
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.createContext = binding.NodeScript.createContext;
|
Script.createScript = function(code, ctx, name) {
|
||||||
exports.runInContext = binding.NodeScript.runInContext;
|
return new Script(code, ctx, name);
|
||||||
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…
x
Reference in New Issue
Block a user