|
21 | 21 |
|
22 | 22 | var binding = process.binding('evals');
|
23 | 23 |
|
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); |
27 | 49 | };
|
28 | 50 |
|
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