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

Commit

Permalink
Pass all globals through "require"
Browse files Browse the repository at this point in the history
(cherry picked from commit 7d01541)
  • Loading branch information
zcbenz authored and codebytere committed Jul 30, 2018
1 parent 4d8b8f6 commit a010945
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 9 additions & 4 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
writable: false
});

// Store the global context into a local variable, so we can refrence them in
// this scope even after we deleted them from global context.
const localGlobal = global;

// Set up process.binding() and process._linkedBinding()
{
const bindingObj = ObjectCreate(null);
Expand Down Expand Up @@ -217,9 +221,10 @@
};

NativeModule.wrapper = [
'(function (exports, require, module, process) {',
'\n});'
];
'(function (exports, require, module, process, global, Buffer) { ' +
'return function (exports, require, module, process) { ',
'\n}.call(this, exports, require, module, process); });'
]

const getOwn = (target, property, receiver) => {
return ReflectApply(ObjectHasOwnProperty, target, [property]) ?
Expand Down Expand Up @@ -262,7 +267,7 @@
const requireFn = this.id.startsWith('internal/deps/') ?
NativeModule.requireForDeps :
NativeModule.require;
fn(this.exports, requireFn, this, process);
fn(this.exports, requireFn, this, process, localGlobal, localGlobal.Buffer);

if (config.experimentalModules && !NativeModule.isInternal(this.id)) {
this.exportKeys = ObjectKeys(this.exports);
Expand Down
15 changes: 11 additions & 4 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ const {
CHAR_9,
} = require('internal/constants');

// Store the global context into a local variable, so we can refrence them in
// this scope even after we deleted them from global context.
const localGlobal = global;

function stat(filename) {
filename = path.toNamespacedPath(filename);
const cache = stat.cache;
Expand Down Expand Up @@ -126,8 +130,9 @@ Module.wrap = function(script) {
};

Module.wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'\n});'
'(function (exports, require, module, __filename, __dirname, process, global, Buffer) { ' +
'return function (exports, require, module, __filename, __dirname) { ',
'\n}.call(this, exports, require, module, __filename, __dirname); });'
];

const debug = util.debuglog('module');
Expand Down Expand Up @@ -684,10 +689,12 @@ Module.prototype._compile = function(content, filename) {
var result;
if (inspectorWrapper) {
result = inspectorWrapper(compiledWrapper, this.exports, this.exports,
require, this, filename, dirname);
require, this, filename, dirname, process,
localGlobal, localGlobal.Buffer);
} else {
result = compiledWrapper.call(this.exports, this.exports, require, this,
filename, dirname);
filename, dirname, process, localGlobal,
localGlobal.Buffer);
}
if (depth === 0) stat.cache = null;
return result;
Expand Down

0 comments on commit a010945

Please sign in to comment.