Skip to content

Commit e6dfd59

Browse files
committed
lib: pass internalBinding more implicitly
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 <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent b3f9b38 commit e6dfd59

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,4 @@ globals:
197197
LTTNG_HTTP_SERVER_RESPONSE: false
198198
LTTNG_NET_SERVER_CONNECTION: false
199199
LTTNG_NET_STREAM_END: false
200+
internalBinding: false

lib/internal/bootstrap_node.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'use strict';
99

1010
(function(process) {
11+
let internalBinding;
1112

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

2122
setupProcessObject();
2223

24+
internalBinding = process._internalBinding;
25+
delete process._internalBinding;
26+
2327
// do this good and early, since it handles errors.
2428
setupProcessFatal();
2529

@@ -574,7 +578,7 @@
574578
};
575579

576580
NativeModule.wrapper = [
577-
'(function (exports, require, module, __filename, __dirname) { ',
581+
'(function (exports, require, module, internalBinding) {',
578582
'\n});'
579583
];
580584

@@ -590,7 +594,7 @@
590594
lineOffset: 0,
591595
displayErrors: true
592596
});
593-
fn(this.exports, NativeModule.require, this, this.filename);
597+
fn(this.exports, NativeModule.require, this, internalBinding);
594598

595599
this.loaded = true;
596600
} finally {

lib/internal/loader/ModuleWrap.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
const { ModuleWrap } =
4-
require('internal/process').internalBinding('module_wrap');
3+
const { ModuleWrap } = internalBinding('module_wrap');
54
const debug = require('util').debuglog('esm');
65
const ArrayJoin = Function.call.bind(Array.prototype.join);
76
const ArrayMap = Function.call.bind(Array.prototype.map);

lib/internal/loader/search.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { URL } = require('url');
44
const CJSmodule = require('module');
55
const errors = require('internal/errors');
6-
const { resolve } = require('internal/process').internalBinding('module_wrap');
6+
const { resolve } = internalBinding('module_wrap');
77

88
module.exports = (target, base) => {
99
if (base === undefined) {

lib/internal/process.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ const errors = require('internal/errors');
44
const util = require('util');
55
const constants = process.binding('constants').os.signals;
66

7-
const internalBinding = process._internalBinding;
8-
delete process._internalBinding;
9-
107
const assert = process.assert = function(x, msg) {
118
if (!x) throw new errors.Error('ERR_ASSERTION', msg || 'assertion error');
129
};
@@ -259,6 +256,5 @@ module.exports = {
259256
setupKillAndExit,
260257
setupSignalHandlers,
261258
setupChannel,
262-
setupRawDebug,
263-
internalBinding
259+
setupRawDebug
264260
};

lib/module.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ Module._extensions = Object.create(null);
8080
var modulePaths = [];
8181
Module.globalPaths = [];
8282

83-
Module.wrapper = NativeModule.wrapper;
84-
Module.wrap = NativeModule.wrap;
83+
Module.wrap = function(script) {
84+
return Module.wrapper[0] + script + Module.wrapper[1];
85+
};
86+
87+
Module.wrapper = [
88+
'(function (exports, require, module, __filename, __dirname) { ',
89+
'\n});'
90+
];
8591

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

0 commit comments

Comments
 (0)