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); }); });
function controlEval(input, context, filename, callback) { debuglog('eval:', input); function returnToCallback(error, result) { debuglog('end-eval:', input, error); callback(error, result); } try { const code = prepareControlCode(input); const result = vm.runInContext(code, context, filename); if (result && typeof result.then === 'function') { toCallback(result, returnToCallback); return; } returnToCallback(null, result); } catch (e) { returnToCallback(e); } }
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}); } catch (err) { console.error ("Error registering API function: \n"+err.stack);
describe('runInContext', () => { beforeEach(() => { currentHook = require('vm').runInContext; }); afterEach(() => { const vm = require('vm'); hook.hookRunInContext(matcher, scriptTransformer); s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}), assert.equal(s, 42); hook.unhookRunInContext(); s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}), coverageVariable: 'fakeCoverageVar' }); s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}), assert.deepEqual(global.fakeCoverageVar, { success: true }); hook.unhookRunInContext(); s = vm.runInContext( '(function () { return 10; }());', vm.createContext({}),
var result if(code != undefined){ result = vm.runInContext(code , context, '<kernel>'); } else { result = 'undefined'
r.context = clientContext const ClientContextPromise = vm.runInContext('Promise', clientContext)
sandbox = createSandbox(filename, socket); vm.createContext(sandbox); vm.runInContext(content, sandbox, { filename: uid(worker, socket), displayErrors: true
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 => { sendError(JSONRPC_ERRORS.INTERNAL_ERROR, "An internal server error occurred while processing your request.", sessionObj);
result = await runInContext(content, ctx, { filename: 'aetheryx.repl' });