title: Node `vm.runInContext` strict mode with Global ReferenceError Still Sets Global Value
body: <p>I'm seeing unexpected behavior when using Node's <code>vm.runInContext</code> to evaluate strict mode code that references an undefined global variable. Here's the sample:</p>
  
  <pre><code>var vm = require('vm');
  var util = require('util');
  
  var ctx = {};
  vm.createContext(ctx);
  
  try {
      console.log(vm.runInContext("'use strict'; g = 3;", ctx));
  } catch (e) {
      console.log('error', e);
  }
  
  console.log('----');
  console.log(util.inspect(ctx));
  </code></pre>
  
  <p>Which prints:</p>
  
  <pre><code>error evalmachine.&lt;anonymous&gt;:1
  'use strict'; g = 3;
                  ^
  ReferenceError: g is not defined
      at evalmachine.&lt;anonymous&gt;:1:17
      at Object.exports.runInContext (vm.js:44:17)
      at Object.&lt;anonymous&gt; (C:\Users\matb\Documents\Visual Studio 2015\Projects\NodejsConsoleApp5\NodejsConsoleApp5\app.js:9:20)
      at Module._compile (module.js:541:32)
      at Object.Module._extensions..js (module.js:550:10)
      at Module.load (module.js:458:32)
      at tryModuleLoad (module.js:417:12)
      at Function.Module._load (module.js:409:3)
      at Timeout.Module.runMain [as _onTimeout] (module.js:575:10)
      at tryOnTimeout (timers.js:224:11)
  -----
  { g: 3 }
  </code></pre>
  
  <p>Since the first <code>vm.runInContext</code> call throws a <code>ReferenceError</code>, I expected <code>g</code> not to get defined at all, but <code>util.inspect(ctx)</code> clearly shows it has been set in the context. Later calls to <code>vm.runInContext</code> can also use <code>g</code> just fine.</p>
  
  <p>Can someone explain what is going on here? It's probably something simple, but the behavior caught me by surprise.</p>