How to use vm - 10 common examples

To help you get started, we’ve selected a few vm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github graalvm / graaljs / test / parallel / test-vm-proxy-failure-CP.js View on Github external
'use strict';
require('../common');
const vm = require('vm');

// Check that we do not accidentally query attributes.
// Issue: https://github.com/nodejs/node/issues/11902
const handler = {
  getOwnPropertyDescriptor: (target, prop) => {
    throw new Error('whoops');
  }
};
const sandbox = new Proxy({ foo: 'bar' }, handler);
const context = vm.createContext(sandbox);

vm.runInContext('', context);
github jhanssen / jsh / src / node_modules / Service / Service.js View on Github external
then(function(script) {
                script = 'var module={};' + script + '(function(){var f=[];for(var i in module.exports){if(typeof(module.exports[i])===\'function\')f.push(i);} return f;})()';
                // jsh.log('running script', script);
                functions = vm.runInNewContext(script, undefined, serviceFile); // no async API for this?
                jsh.log('GOT FUNCTIONS', functions);
                return writeFile(manifestFile, functions.join(' '));
            }).
            then(function(err) {
github gabrielgrant / node-ember-precompile / src / context.js View on Github external
function getContext(sandboxExtras){
  sandbox = getBaseSandbox();
  sandboxExtras = sandboxExtras || {};
  for (var attrname in sandboxExtras){
    sandbox[attrname] = sandboxExtras[attrname];
  }
  
  // create a context for the vm using the sandbox data
  var context = vm.createContext(sandbox)

  // load Handlebars and Ember into the sandbox
  vm.runInContext(HANDLEBARSJS, context, 'handlebars.js')
  vm.runInContext(EMBERJS, context, 'ember.js')
  return context;
}
github batiste / CokeScript / test / basics.js View on Github external
function exe(js, context) {
  try {
    return vm.runInNewContext(js, context);
  } catch(e) {
    throw 'JS error\n' + js;
  }
}
github duojs / duo / test / cli.js View on Github external
function evaluate(js, ctx) {
  if (!ctx) ctx = { window: {}, document: {} };
  js = convert.removeComments(js);
  vm.runInNewContext('main =' + js + '(1)', ctx, 'main.vm');
  vm.runInNewContext('require =' + js + '', ctx, 'require.vm');
  return ctx;
}
github freeCodeCamp / freeCodeCamp / curriculum / test-challenges.js View on Github external
async function runScript(scriptString, sandbox) {
  const context = vm.createContext(sandbox);
  scriptString += `;
    (async () => {
      const testResult = eval(test);
      if (typeof testResult === 'function') {
        const __result = testResult(() => code);
        if (isPromise(__result)) {
          await __result;
        }
      }
    })();`;
  const script = new vm.Script(scriptString);
  script.runInContext(context);
}
github exokitxr / exokit / src / WindowBase.js View on Github external
function importScripts() {
  for (let i = 0; i < arguments.length; i++) {
    const importScriptPath = arguments[i];
    const importScriptSource = getScript(importScriptPath);
    vm.runInThisContext(importScriptSource, global, {
      filename: /^https?:/.test(importScriptPath) ? importScriptPath : 'data-url://',
    });
  }
}
global.importScripts = importScripts;
github Clouda-team / Cloudajs / sumeru / server / readClientFile.js View on Github external
var runnable = function(path, context) {
  var data = fs.readFileSync(path);
  if (!context)context ={};
  context.console=console;
  vm.runInNewContext(data, context, path);
}
var _getFilePackageByPath = function(path,filename){
github rethinkdb / horizon / plugins / src / permissions / template.js View on Github external
const make_template = (str) => {
  try {
    const sandbox = Object.assign({}, env);
    return vm.runInNewContext(str, sandbox);
  } catch (err) {
    throw remakeError(err);
  }
};
github pfrazee / libvms / lib / vm.js View on Github external
function evaluate (vm) {
  if (vm.hasEvaluated) {
    return
  }
  vm.script = new NodeVM.Script(vm.code)
  vm.sandbox = createNewSandbox(vm)
  vm.context = NodeVM.createContext(vm.sandbox)
  vm.script.runInContext(vm.context)
  vm.hasEvaluated = true
}

vm

NodeJS Core Module Extended

MIT
Latest version published 8 years ago

Package Health Score

47 / 100
Full package analysis