Skip to content

Commit

Permalink
lib: pass internalBinding more implicitly
Browse files Browse the repository at this point in the history
Modify passing of the `internalBinding` function so that it’s
easier for core modules to adopt, and also not even accessible
through `--expose-internals`.

This also splits the module wrapper into a separate version for
internal bindings and for CJS modules, which seems like a good
idea given the different semantics.

PR-URL: #16218
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
addaleax committed Oct 18, 2017
1 parent b3f9b38 commit e6dfd59
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,4 @@ globals:
LTTNG_HTTP_SERVER_RESPONSE: false
LTTNG_NET_SERVER_CONNECTION: false
LTTNG_NET_STREAM_END: false
internalBinding: false
8 changes: 6 additions & 2 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'use strict';

(function(process) {
let internalBinding;

function startup() {
const EventEmitter = NativeModule.require('events');
Expand All @@ -20,6 +21,9 @@

setupProcessObject();

internalBinding = process._internalBinding;
delete process._internalBinding;

// do this good and early, since it handles errors.
setupProcessFatal();

Expand Down Expand Up @@ -574,7 +578,7 @@
};

NativeModule.wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'(function (exports, require, module, internalBinding) {',
'\n});'
];

Expand All @@ -590,7 +594,7 @@
lineOffset: 0,
displayErrors: true
});
fn(this.exports, NativeModule.require, this, this.filename);
fn(this.exports, NativeModule.require, this, internalBinding);

this.loaded = true;
} finally {
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/loader/ModuleWrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { ModuleWrap } =
require('internal/process').internalBinding('module_wrap');
const { ModuleWrap } = internalBinding('module_wrap');
const debug = require('util').debuglog('esm');
const ArrayJoin = Function.call.bind(Array.prototype.join);
const ArrayMap = Function.call.bind(Array.prototype.map);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/loader/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { URL } = require('url');
const CJSmodule = require('module');
const errors = require('internal/errors');
const { resolve } = require('internal/process').internalBinding('module_wrap');
const { resolve } = internalBinding('module_wrap');

module.exports = (target, base) => {
if (base === undefined) {
Expand Down
6 changes: 1 addition & 5 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ const errors = require('internal/errors');
const util = require('util');
const constants = process.binding('constants').os.signals;

const internalBinding = process._internalBinding;
delete process._internalBinding;

const assert = process.assert = function(x, msg) {
if (!x) throw new errors.Error('ERR_ASSERTION', msg || 'assertion error');
};
Expand Down Expand Up @@ -259,6 +256,5 @@ module.exports = {
setupKillAndExit,
setupSignalHandlers,
setupChannel,
setupRawDebug,
internalBinding
setupRawDebug
};
10 changes: 8 additions & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ Module._extensions = Object.create(null);
var modulePaths = [];
Module.globalPaths = [];

Module.wrapper = NativeModule.wrapper;
Module.wrap = NativeModule.wrap;
Module.wrap = function(script) {
return Module.wrapper[0] + script + Module.wrapper[1];
};

Module.wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'\n});'
];

const debug = util.debuglog('module');

Expand Down

0 comments on commit e6dfd59

Please sign in to comment.