From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Sun, 27 Mar 2016 14:42:26 +0900
Subject: Pass all globals through "require"

(cherry picked from commit 7d015419cb7a0ecfe6728431a4ed2056cd411d62)

diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 0a6788d1b848d860fa3fa3e857c7feab6f16311e..a6b01d7e143fa6ffeda6fa7723e279db7678ddd4 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -209,6 +209,13 @@ const {
   CHAR_FORWARD_SLASH,
 } = require('internal/constants');
 
+// Store the "global" variable from global scope into a local scope, so we can
+// still reference it from this file even after we deleted the "global" variable
+// from the global scope.
+const localGlobal = (typeof global !== 'undefined') ? global : undefined;
+// Do the same for "Buffer".
+const localBuffer = (typeof Buffer !== 'undefined') ? Buffer : undefined;
+
 const {
   isProxy,
 } = require('internal/util/types');
@@ -1807,9 +1814,12 @@ Module.prototype._compile = function(content, filename, format) {
   if (this[kIsMainSymbol] && getOptionValue('--inspect-brk')) {
     const { callAndPauseOnStart } = internalBinding('inspector');
     result = callAndPauseOnStart(compiledWrapper, thisValue, exports,
-                                 require, module, filename, dirname);
+                                 require, module, filename, dirname,
+                                 process, localGlobal, localBuffer);
   } else {
-    result = FunctionPrototypeCall(compiledWrapper, thisValue, exports, require, module, filename, dirname);
+    result = FunctionPrototypeCall(compiledWrapper, thisValue, exports,
+                                   require, module, filename, dirname,
+                                   process, localGlobal, localBuffer);
   }
   this[kIsExecuting] = false;
   if (requireDepth === 0) { statCache = null; }
