Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44daa98

Browse files
committed
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.
1 parent 29463cb commit 44daa98

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

lib/vm.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,34 @@
2121

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

24-
exports.Script = binding.NodeScript;
25-
exports.createScript = function(code, ctx, name) {
26-
return new exports.Script(code, ctx, name);
24+
module.exports = Script;
25+
Script.Script = Script;
26+
27+
function Script(code, ctx, filename) {
28+
if (!(this instanceof Script)) {
29+
return new Script(code, ctx, filename);
30+
}
31+
32+
var ns = new binding.NodeScript(code, ctx, filename);
33+
34+
// bind all methods to this Script object
35+
Object.keys(binding.NodeScript.prototype).forEach(function(f) {
36+
if (typeof binding.NodeScript.prototype[f] === 'function') {
37+
this[f] = function() {
38+
if (!(this instanceof Script)) {
39+
throw new TypeError('invalid call to '+f);
40+
}
41+
return ns[f].apply(ns, arguments);
42+
};
43+
}
44+
}, this);
45+
};
46+
47+
Script.createScript = function(code, ctx, name) {
48+
return new Script(code, ctx, name);
2749
};
2850

29-
exports.createContext = binding.NodeScript.createContext;
30-
exports.runInContext = binding.NodeScript.runInContext;
31-
exports.runInThisContext = binding.NodeScript.runInThisContext;
32-
exports.runInNewContext = binding.NodeScript.runInNewContext;
51+
Script.createContext = binding.NodeScript.createContext;
52+
Script.runInContext = binding.NodeScript.runInContext;
53+
Script.runInThisContext = binding.NodeScript.runInThisContext;
54+
Script.runInNewContext = binding.NodeScript.runInNewContext;

0 commit comments

Comments
 (0)