console: new Console(dest, dest) var context = vm.createContext(sandbox)
function runScript(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string , cb: (err?: Error | string , data?: any) => void) { if (handle === 'client') { let agent = _agent as MasterAgent; if (context === 'all') { cb('context error'); return; } agent.request(context, WatchServerModule.moduleId, { comd: comd, param: param, context: context }, function (err, msg) { cb(null, msg); }); } else if (handle === 'monitor') { let agent = _agent as MonitorAgent; let ctx = { app: app, result: null as any }; try { vm.createContext(ctx); vm.runInNewContext('result = ' + param, ctx); cb(null, util.inspect(ctx.result)); } catch (e) { cb(null, e.stack); } } }
script.runInContext(vm.createContext(sandbox), { timeout: 500 });
describe('context', function() { it('should work with errors from different contexts', function(done) { const err = new Error(); const verr = new VError(err); assert.ok(_.isError(verr.cause())); const context = vm.createContext({ callback: function callback(err2) { assert.ok(_.isError(err2)); const verr2 = new VError(err); assert.ok(_.isError(verr2.cause())); done(); } }); vm.runInContext('callback(new Error())', context); }); });
async.waterfall([ // Enlist all unit test files async.apply(recursive, 'test/vm'), // Bundle the unit test suite function (files, next) { var specs, bundler = browserify('test/vm/_bootstrap.js'); (specs = files.filter(function (file) { // extract all test files return (file.substr(-8) === '.test.js'); })).forEach(function (file) { // @hack to allow mocha.addFile to work correctly in the Node VM bundler.require('./' + file, {expose: file}); }); bundler.bundle(function (err, bundle) { next(err, specs, bundle); }); }, // Run the tests in the VM function (specs, bundle, next) { var context = vm.createContext({console, setTimeout, clearTimeout, __next: next, __specs: specs}); context.global = context; // @hack to make the context work correctly vm.runInContext(bundle.toString(), context, {displayErrors: true}); } ], exit);
var context = vm.createContext(vmContext); try { vm.runInContext(script, context, {timeout:rpc_options.api_timelimit});
s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}), '/bar/foo.js' ); s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}), '/bar/foo.js' ); s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}), '/bar/foo.js' ); s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}), '/bar/foo.js' ); const s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}) ); assert.equal(s, 10);
const clientContext = vm.createContext(client) clientContext.module = r.context.module clientContext.require = r.context.require
var vmContext = new Object(); vmContext = Object.assign(rpc_options.exposed_api_objects, vmContext); var context = vm.createContext(vmContext); vm.runInContext(script, context, {timeout:rpc_options.api_timelimit}); context[requestMethod](sessionObj).catch(err => {
} else { sandbox = createSandbox(filename, socket); vm.createContext(sandbox); vm.runInContext(content, sandbox, { filename: uid(worker, socket),
}; createContext(ctx); return ctx; };