title: Getting values out of Node core vm module
body: <p>How do you get the value in the context of a vm in node. </p>
  
  <p>If I create a context and run a script in it do I use <code>util.inspect([[context]])</code>? </p>
  
  <p>And if I do does it come back serialized?</p>
  
  <p>for some reason when I <code>util.inspect("null")</code> it comes back with 
  <code>'\'null'\'</code></p>
  
  <p>Does any one know why, and also what the best way to get values out of a context run in nodes vm module?</p>
  
  <p>I've also done some micro benchmarks and it seems that creating a new context is much slower than running it in <code>thisContext</code>. Also when I get rid of the timeout its seem like it run <strong>much</strong> faster.</p>
  
  <p>So I would like to use this context without having to use globals to not cause leakage, and also try to speed up the performance of this.</p>
  
  <p>The context of this code is that I'm trying to sandbox a catastrophic regex. </p>
  
  <pre class="lang-js prettyprint-override"><code>global.mapperContext = {
    result: false
  };
  
  const scriptString = `
    global.mapperContext.result = true;
  
    if ("${string}".match(${regex})) {
      global.mapperContext.result = false;
    }
  `;
  
  const vmScript = new vm.script(scriptString);
  
  try {
    vmScript.runInThisContext({ timeout: 1000 });
  } catch (e) {
    // Do something with error
  }
  
  // This works but I dont like attaching things to the global object.
  // I could cause memory leaks...
  console.log(global.mapperContext); 
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>const mapperContext = { result };
  
  const scriptString = `
  (function IIFE() {
  
    result = true;
  
    if ("${string}".match(${regex})) {
      result = false;
    }
  
  })()
  `;
  
  const sandbox = vm.createContext(mapperContext);
  const script = new vm.script(scriptString);
  
  script.runInContext(script, sandbox, {timeout: 1000});
  
  // This result is serialized and is hard to parse.
  util.inspect(sandbox.result);
  </code></pre>