Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 7d01541

Browse files
zcbenzalexeykuzmin
authored andcommitted
Pass all globals through "require"
1 parent 0ea1fcf commit 7d01541

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

lib/internal/bootstrap/loaders.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
writable: false
5252
});
5353

54+
// Store the global context into a local variable, so we can refrence them in
55+
// this scope even after we deleted them from global context.
56+
const localGlobal = global;
57+
5458
// Set up process.binding() and process._linkedBinding()
5559
{
5660
const bindingObj = Object.create(null);
@@ -189,9 +193,10 @@
189193
};
190194

191195
NativeModule.wrapper = [
192-
'(function (exports, require, module, process) {',
193-
'\n});'
194-
];
196+
'(function (exports, require, module, process, global, Buffer) { ' +
197+
'return function (exports, require, module, process) { ',
198+
'\n}.call(this, exports, require, module, process); });'
199+
]
195200

196201
NativeModule.prototype.compile = function() {
197202
let source = NativeModule.getSource(this.id);
@@ -206,7 +211,7 @@
206211
const requireFn = this.id.startsWith('internal/deps/') ?
207212
NativeModule.requireForDeps :
208213
NativeModule.require;
209-
fn(this.exports, requireFn, this, process);
214+
fn(this.exports, requireFn, this, process, localGlobal, localGlobal.Buffer);
210215

211216
this.loaded = true;
212217
} finally {

lib/internal/modules/cjs/loader.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ const {
7171
CHAR_9,
7272
} = require('internal/constants');
7373

74+
// Store the global context into a local variable, so we can refrence them in
75+
// this scope even after we deleted them from global context.
76+
const localGlobal = global;
77+
7478
function stat(filename) {
7579
filename = path.toNamespacedPath(filename);
7680
const cache = stat.cache;
@@ -117,8 +121,9 @@ Module.wrap = function(script) {
117121
};
118122

119123
Module.wrapper = [
120-
'(function (exports, require, module, __filename, __dirname) { ',
121-
'\n});'
124+
'(function (exports, require, module, __filename, __dirname, process, global, Buffer) { ' +
125+
'return function (exports, require, module, __filename, __dirname) { ',
126+
'\n}.call(this, exports, require, module, __filename, __dirname); });'
122127
];
123128

124129
const debug = util.debuglog('module');
@@ -673,10 +678,12 @@ Module.prototype._compile = function(content, filename) {
673678
var result;
674679
if (inspectorWrapper) {
675680
result = inspectorWrapper(compiledWrapper, this.exports, this.exports,
676-
require, this, filename, dirname);
681+
require, this, filename, dirname, process,
682+
localGlobal, localGlobal.Buffer);
677683
} else {
678684
result = compiledWrapper.call(this.exports, this.exports, require, this,
679-
filename, dirname);
685+
filename, dirname, process, localGlobal,
686+
localGlobal.Buffer);
680687
}
681688
if (depth === 0) stat.cache = null;
682689
return result;

0 commit comments

Comments
 (0)