|
19 | 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 | 21 |
|
22 | | -var binding = process.binding('evals'); |
23 | | - |
24 | | -module.exports = Script; |
25 | | -Script.Script = Script; |
| 22 | +var binding = process.binding('contextify'); |
| 23 | +var Script = binding.ContextifyScript; |
26 | 24 | var util = require('util'); |
27 | 25 |
|
28 | | -function Script(code, ctx, filename) { |
29 | | - if (!(this instanceof Script)) { |
30 | | - return new Script(code, ctx, filename); |
| 26 | +// The binding provides a few useful primitives: |
| 27 | +// - ContextifyScript(code, [filename]), with methods: |
| 28 | +// - runInThisContext() |
| 29 | +// - runInContext(sandbox, [timeout]) |
| 30 | +// - makeContext(sandbox) |
| 31 | +// From this we build the entire documented API. |
| 32 | + |
| 33 | +Script.prototype.runInNewContext = function(initSandbox, timeout) { |
| 34 | + var context = exports.createContext(initSandbox); |
| 35 | + return this.runInContext(context, timeout); |
| 36 | +}; |
| 37 | + |
| 38 | +exports.Script = Script; |
| 39 | + |
| 40 | +exports.createScript = function(code, filename) { |
| 41 | + return new Script(code, filename); |
| 42 | +}; |
| 43 | + |
| 44 | +exports.createContext = function(initSandbox) { |
| 45 | + if (util.isUndefined(initSandbox)) { |
| 46 | + initSandbox = {}; |
31 | 47 | } |
32 | 48 |
|
33 | | - var ns = new binding.NodeScript(code, ctx, filename); |
34 | | - |
35 | | - // bind all methods to this Script object |
36 | | - Object.keys(binding.NodeScript.prototype).forEach(function(f) { |
37 | | - if (util.isFunction(binding.NodeScript.prototype[f])) { |
38 | | - this[f] = function() { |
39 | | - if (!(this instanceof Script)) { |
40 | | - throw new TypeError('invalid call to ' + f); |
41 | | - } |
42 | | - return ns[f].apply(ns, arguments); |
43 | | - }; |
44 | | - } |
45 | | - }, this); |
46 | | -} |
47 | | - |
48 | | -Script.createScript = function(code, ctx, name) { |
49 | | - return new Script(code, ctx, name); |
| 49 | + binding.makeContext(initSandbox); |
| 50 | + |
| 51 | + return initSandbox; |
| 52 | +}; |
| 53 | + |
| 54 | +exports.runInContext = function(code, sandbox, filename, timeout) { |
| 55 | + var script = exports.createScript(code, filename); |
| 56 | + return script.runInContext(sandbox, timeout); |
50 | 57 | }; |
51 | 58 |
|
52 | | -Script.createContext = binding.NodeScript.createContext; |
53 | | -Script.runInContext = binding.NodeScript.runInContext; |
54 | | -Script.runInThisContext = binding.NodeScript.runInThisContext; |
55 | | -Script.runInNewContext = binding.NodeScript.runInNewContext; |
| 59 | +exports.runInNewContext = function(code, sandbox, filename, timeout) { |
| 60 | + var script = exports.createScript(code, filename); |
| 61 | + return script.runInNewContext(sandbox, timeout); |
| 62 | +}; |
| 63 | + |
| 64 | +exports.runInThisContext = function(code, filename, timeout) { |
| 65 | + var script = exports.createScript(code, filename); |
| 66 | + return script.runInThisContext(timeout); |
| 67 | +}; |
0 commit comments