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

Commit a010945

Browse files
zcbenzcodebytere
authored andcommitted
Pass all globals through "require"
(cherry picked from commit 7d01541)
1 parent 4d8b8f6 commit a010945

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

lib/internal/bootstrap/loaders.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
writable: false
7171
});
7272

73+
// Store the global context into a local variable, so we can refrence them in
74+
// this scope even after we deleted them from global context.
75+
const localGlobal = global;
76+
7377
// Set up process.binding() and process._linkedBinding()
7478
{
7579
const bindingObj = ObjectCreate(null);
@@ -217,9 +221,10 @@
217221
};
218222

219223
NativeModule.wrapper = [
220-
'(function (exports, require, module, process) {',
221-
'\n});'
222-
];
224+
'(function (exports, require, module, process, global, Buffer) { ' +
225+
'return function (exports, require, module, process) { ',
226+
'\n}.call(this, exports, require, module, process); });'
227+
]
223228

224229
const getOwn = (target, property, receiver) => {
225230
return ReflectApply(ObjectHasOwnProperty, target, [property]) ?
@@ -262,7 +267,7 @@
262267
const requireFn = this.id.startsWith('internal/deps/') ?
263268
NativeModule.requireForDeps :
264269
NativeModule.require;
265-
fn(this.exports, requireFn, this, process);
270+
fn(this.exports, requireFn, this, process, localGlobal, localGlobal.Buffer);
266271

267272
if (config.experimentalModules && !NativeModule.isInternal(this.id)) {
268273
this.exportKeys = ObjectKeys(this.exports);

lib/internal/modules/cjs/loader.js

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

83+
// Store the global context into a local variable, so we can refrence them in
84+
// this scope even after we deleted them from global context.
85+
const localGlobal = global;
86+
8387
function stat(filename) {
8488
filename = path.toNamespacedPath(filename);
8589
const cache = stat.cache;
@@ -126,8 +130,9 @@ Module.wrap = function(script) {
126130
};
127131

128132
Module.wrapper = [
129-
'(function (exports, require, module, __filename, __dirname) { ',
130-
'\n});'
133+
'(function (exports, require, module, __filename, __dirname, process, global, Buffer) { ' +
134+
'return function (exports, require, module, __filename, __dirname) { ',
135+
'\n}.call(this, exports, require, module, __filename, __dirname); });'
131136
];
132137

133138
const debug = util.debuglog('module');
@@ -684,10 +689,12 @@ Module.prototype._compile = function(content, filename) {
684689
var result;
685690
if (inspectorWrapper) {
686691
result = inspectorWrapper(compiledWrapper, this.exports, this.exports,
687-
require, this, filename, dirname);
692+
require, this, filename, dirname, process,
693+
localGlobal, localGlobal.Buffer);
688694
} else {
689695
result = compiledWrapper.call(this.exports, this.exports, require, this,
690-
filename, dirname);
696+
filename, dirname, process, localGlobal,
697+
localGlobal.Buffer);
691698
}
692699
if (depth === 0) stat.cache = null;
693700
return result;

0 commit comments

Comments
 (0)