Skip to content

Commit c39b910

Browse files
devsnekMylesBorins
authored andcommitted
bootstrapper: move internalBinding to NativeModule
internalBinding is used so often that it should just automatically be available for usage in internals. PR-URL: #23025 Refs: 2a9eb31 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Backport-PR-URL: #23661 Backport-Reviewed-By: Gus Caplan <[email protected]> Backport-Reviewed-By: Richard Lau <[email protected]> Backport-Reviewed-By: Michaël Zasso <[email protected]> Backport-Reviewed-By: Joyee Cheung <[email protected]> Backport-Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 25d7c02 commit c39b910

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+59
-67
lines changed

lib/.eslintrc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ globals:
4444
DCHECK_LE: false
4545
DCHECK_LT: false
4646
DCHECK_NE: false
47+
internalBinding: false

lib/_http_client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
const util = require('util');
2525
const net = require('net');
2626
const url = require('url');
27-
const { HTTPParser } = process.binding('http_parser');
27+
const { HTTPParser } = internalBinding('http_parser');
2828
const assert = require('assert').ok;
2929
const {
3030
_checkIsHttpToken: checkIsHttpToken,

lib/_http_common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
'use strict';
2323

24-
const { methods, HTTPParser } = process.binding('http_parser');
24+
const { methods, HTTPParser } = internalBinding('http_parser');
2525

2626
const FreeList = require('internal/freelist');
2727
const { ondrain } = require('internal/http');

lib/_http_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
const util = require('util');
2525
const net = require('net');
26-
const { HTTPParser } = process.binding('http_parser');
26+
const { HTTPParser } = internalBinding('http_parser');
2727
const assert = require('assert').ok;
2828
const {
2929
parsers,

lib/_tls_common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const { SSL_OP_CIPHER_SERVER_PREFERENCE } = process.binding('constants').crypto;
3434
// Lazily loaded
3535
var crypto = null;
3636

37-
const { SecureContext: NativeSecureContext } = process.binding('crypto');
37+
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
3838

3939
function SecureContext(secureProtocol, secureOptions, context) {
4040
if (!(this instanceof SecureContext)) {

lib/_tls_wrap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ const common = require('_tls_common');
3232
const { StreamWrap } = require('_stream_wrap');
3333
const { Buffer } = require('buffer');
3434
const debug = util.debuglog('tls');
35-
const tls_wrap = process.binding('tls_wrap');
36-
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
37-
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
35+
const { TCP, constants: TCPConstants } = internalBinding('tcp_wrap');
36+
const tls_wrap = internalBinding('tls_wrap');
37+
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
3838
const { owner_symbol } = require('internal/async_hooks').symbols;
3939
const {
4040
SecureContext: NativeSecureContext

lib/buffer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const {
4141
// that test/parallel/test-buffer-bindingobj-no-zerofill.js is written.
4242
let isAnyArrayBuffer;
4343
try {
44-
const { internalBinding } = require('internal/bootstrap/loaders');
4544
isAnyArrayBuffer = internalBinding('types').isAnyArrayBuffer;
4645
} catch (e) {
4746
isAnyArrayBuffer = require('util').types.isAnyArrayBuffer;

lib/child_process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const {
2828
const { isArrayBufferView } = require('internal/util/types');
2929
const debug = util.debuglog('child_process');
3030
const { Buffer } = require('buffer');
31-
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
31+
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
3232
const {
3333
ERR_INVALID_ARG_VALUE,
3434
ERR_CHILD_PROCESS_IPC_REQUIRED,

lib/console.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const {
2929
ERR_INVALID_ARG_VALUE,
3030
},
3131
} = require('internal/errors');
32-
const { previewEntries } = process.binding('util');
32+
const { previewEntries } = internalBinding('util');
3333
const { Buffer: { isBuffer } } = require('buffer');
3434
const util = require('util');
3535
const {

lib/dns.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
'use strict';
2323

24-
const cares = process.binding('cares_wrap');
24+
const cares = internalBinding('cares_wrap');
2525
const { isIP, isIPv4, isLegalPort } = require('internal/net');
2626
const { customPromisifyArgs } = require('internal/util');
2727
const errors = require('internal/errors');

lib/domain.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const {
3434
ERR_UNHANDLED_ERROR
3535
} = require('internal/errors').codes;
3636
const { createHook } = require('async_hooks');
37-
const { internalBinding } = require('internal/bootstrap/loaders');
3837

3938
// overwrite process.domain with a getter/setter that will allow for more
4039
// effective optimizations

lib/internal/async_hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
ERR_ASYNC_TYPE,
55
ERR_INVALID_ASYNC_ID
66
} = require('internal/errors').codes;
7-
const async_wrap = process.binding('async_wrap');
7+
const async_wrap = internalBinding('async_wrap');
88
/* async_hook_fields is a Uint32Array wrapping the uint32_t array of
99
* Environment::AsyncHooks::fields_[]. Each index tracks the number of active
1010
* hooks for each type.

lib/internal/bash_completion.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
const { internalBinding } = require('internal/bootstrap/loaders');
32
const { getOptions } = internalBinding('options');
43

54
function print(stream) {

lib/internal/bootstrap/loaders.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,15 @@
100100
internalBinding = function internalBinding(module) {
101101
let mod = bindingObj[module];
102102
if (typeof mod !== 'object') {
103-
mod = bindingObj[module] = getInternalBinding(module);
103+
try {
104+
mod = getInternalBinding(module);
105+
} catch {
106+
// v10.x only: Fall back to `process.binding()`,
107+
// to avoid future merge conflicts when backporting changes that use
108+
// `internalBinding()` to v10.x.
109+
mod = process.binding(module);
110+
}
111+
bindingObj[module] = mod;
104112
moduleLoadList.push(`Internal Binding ${module}`);
105113
}
106114
return mod;
@@ -223,7 +231,7 @@
223231
};
224232

225233
NativeModule.wrapper = [
226-
'(function (exports, require, module, process) {',
234+
'(function (exports, require, module, process, internalBinding) {',
227235
'\n});'
228236
];
229237

@@ -294,7 +302,7 @@
294302
const requireFn = this.id.startsWith('internal/deps/') ?
295303
NativeModule.requireForDeps :
296304
NativeModule.require;
297-
fn(this.exports, requireFn, this, process);
305+
fn(this.exports, requireFn, this, process, internalBinding);
298306

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

lib/internal/child_process.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ const dgram = require('dgram');
2121
const util = require('util');
2222
const assert = require('assert');
2323

24-
const { Process } = process.binding('process_wrap');
25-
const { WriteWrap } = process.binding('stream_wrap');
26-
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
27-
const { TTY } = process.binding('tty_wrap');
28-
const { TCP } = process.binding('tcp_wrap');
29-
const { UDP } = process.binding('udp_wrap');
24+
const { Process } = internalBinding('process_wrap');
25+
const { WriteWrap } = internalBinding('stream_wrap');
26+
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
27+
const { TCP } = internalBinding('tcp_wrap');
28+
const { TTY } = internalBinding('tty_wrap');
29+
const { UDP } = internalBinding('udp_wrap');
3030
const SocketList = require('internal/socket_list');
3131
const { owner_symbol } = require('internal/async_hooks').symbols;
3232
const { convertToValidSignal } = require('internal/util');

lib/internal/cluster/round_robin_handle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const assert = require('assert');
33
const net = require('net');
44
const { sendHelper } = require('internal/cluster/utils');
5-
const uv = process.binding('uv');
5+
const uv = internalBinding('uv');
66

77
module.exports = RoundRobinHandle;
88

lib/internal/crypto/keygen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { AsyncWrap, Providers } = process.binding('async_wrap');
3+
const { AsyncWrap, Providers } = internalBinding('async_wrap');
44
const {
55
generateKeyPairRSA,
66
generateKeyPairDSA,

lib/internal/crypto/pbkdf2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { AsyncWrap, Providers } = process.binding('async_wrap');
3+
const { AsyncWrap, Providers } = internalBinding('async_wrap');
44
const { Buffer } = require('buffer');
55
const { pbkdf2: _pbkdf2 } = process.binding('crypto');
66
const { validateUint32 } = require('internal/validators');

lib/internal/crypto/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { AsyncWrap, Providers } = process.binding('async_wrap');
3+
const { AsyncWrap, Providers } = internalBinding('async_wrap');
44
const { Buffer, kMaxLength } = require('buffer');
55
const { randomBytes: _randomBytes } = process.binding('crypto');
66
const {

lib/internal/crypto/scrypt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { AsyncWrap, Providers } = process.binding('async_wrap');
3+
const { AsyncWrap, Providers } = internalBinding('async_wrap');
44
const { Buffer } = require('buffer');
55
const { scrypt: _scrypt } = process.binding('crypto');
66
const { validateUint32 } = require('internal/validators');

lib/internal/crypto/sig.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ const {
55
ERR_INVALID_OPT_VALUE
66
} = require('internal/errors').codes;
77
const { validateString } = require('internal/validators');
8-
const {
9-
Sign: _Sign,
10-
Verify: _Verify
11-
} = process.binding('crypto');
8+
const { Sign: _Sign, Verify: _Verify } = internalBinding('crypto');
129
const {
1310
RSA_PSS_SALTLEN_AUTO,
1411
RSA_PKCS1_PADDING

lib/internal/dgram.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const assert = require('assert');
33
const { codes } = require('internal/errors');
4-
const { UDP } = process.binding('udp_wrap');
4+
const { UDP } = internalBinding('udp_wrap');
55
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
66
const kStateSymbol = Symbol('state symbol');
77
let dns; // Lazy load for startup performance.

lib/internal/domexception.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const { internalBinding } = require('internal/bootstrap/loaders');
43
const { registerDOMException } = internalBinding('messaging');
54
const { ERR_INVALID_THIS } = require('internal/errors').codes;
65

lib/internal/encoding.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const {
2323

2424
const { isArrayBufferView } = require('internal/util/types');
2525

26-
const { internalBinding } = require('internal/bootstrap/loaders');
2726
const {
2827
isArrayBuffer
2928
} = internalBinding('types');

lib/internal/http2/core.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const {
3131
owner_symbol,
3232
},
3333
} = require('internal/async_hooks');
34-
const { internalBinding } = require('internal/bootstrap/loaders');
3534
const {
3635
codes: {
3736
ERR_HTTP2_ALTSVC_INVALID_ORIGIN,

lib/internal/http2/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const binding = process.binding('http2');
3+
const binding = internalBinding('http2');
44
const {
55
ERR_HTTP2_HEADER_SINGLE_VALUE,
66
ERR_HTTP2_INVALID_CONNECTION_HEADERS,

lib/internal/modules/esm/create_dynamic_module.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const { internalBinding } = require('internal/bootstrap/loaders');
43
const { ModuleWrap } = internalBinding('module_wrap');
54
const debug = require('util').debuglog('esm');
65
const ArrayJoin = Function.call.bind(Array.prototype.join);

lib/internal/modules/esm/default_resolve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { URL } = require('url');
44
const CJSmodule = require('internal/modules/cjs/loader');
55
const internalFS = require('internal/fs/utils');
6-
const { NativeModule, internalBinding } = require('internal/bootstrap/loaders');
6+
const { NativeModule } = require('internal/bootstrap/loaders');
77
const { extname } = require('path');
88
const { realpathSync } = require('fs');
99
const preserveSymlinks = !!process.binding('config').preserveSymlinks;

lib/internal/modules/esm/module_job.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const { internalBinding } = require('internal/bootstrap/loaders');
43
const { ModuleWrap } = internalBinding('module_wrap');
54
const { SafeSet, SafePromise } = require('internal/safe_globals');
65
const { decorateErrorStack } = require('internal/util');

lib/internal/modules/esm/translators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { NativeModule, internalBinding } = require('internal/bootstrap/loaders');
3+
const { NativeModule } = require('internal/bootstrap/loaders');
44
const { ModuleWrap } = internalBinding('module_wrap');
55
const {
66
stripShebang,

lib/internal/print_help.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
const { internalBinding } = require('internal/bootstrap/loaders');
32
const { getOptions, types } = internalBinding('options');
43

54
const typeLookup = [];

lib/internal/process/esm_loader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const { internalBinding } = require('internal/bootstrap/loaders');
43
const {
54
setImportModuleDynamicallyCallback,
65
setInitializeImportMetaObjectCallback

lib/internal/process/promises.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { safeToString } = process.binding('util');
3+
const { safeToString } = internalBinding('util');
44

55
const maybeUnhandledPromises = new WeakMap();
66
const pendingUnhandledRejections = [];

lib/internal/process/stdio.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function getMainThreadStdio() {
4141

4242
function getStdin() {
4343
if (stdin) return stdin;
44-
const tty_wrap = process.binding('tty_wrap');
44+
const tty_wrap = internalBinding('tty_wrap');
4545
const fd = 0;
4646

4747
switch (tty_wrap.guessHandleType(fd)) {
@@ -157,7 +157,7 @@ function setupProcessStdio({ getStdout, getStdin, getStderr }) {
157157

158158
function createWritableStdioStream(fd) {
159159
var stream;
160-
const tty_wrap = process.binding('tty_wrap');
160+
const tty_wrap = internalBinding('tty_wrap');
161161

162162
// Note stream._type is used for test-module-load-list.js
163163

lib/internal/stream_base_commons.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
const { Buffer } = require('buffer');
4-
const { WriteWrap } = process.binding('stream_wrap');
5-
const { UV_EOF } = process.binding('uv');
4+
const { WriteWrap } = internalBinding('stream_wrap');
5+
const { UV_EOF } = internalBinding('uv');
66
const { errnoException } = require('internal/errors');
77
const { owner_symbol } = require('internal/async_hooks').symbols;
88

lib/internal/test/binding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ process.emitWarning(
88
// These exports should be scoped as specifically as possible
99
// to avoid exposing APIs because even with that warning and
1010
// this file being internal people will still try to abuse it.
11-
const { internalBinding } = require('internal/bootstrap/loaders');
1211
module.exports = {
1312
ModuleWrap: internalBinding('module_wrap').ModuleWrap,
13+
internalBinding
1414
};

lib/internal/test/heap.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ process.emitWarning(
55
'tracked by any versioning system or deprecation process.',
66
'internal/test/heap');
77

8-
const { internalBinding } = require('internal/bootstrap/loaders');
98
const { createHeapDump, buildEmbedderGraph } = internalBinding('heap_utils');
109
const assert = require('assert');
1110

lib/internal/trace_events_async_hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
exports.setup = function(traceEvents, traceEventCategory) {
4-
const async_wrap = process.binding('async_wrap');
4+
const async_wrap = internalBinding('async_wrap');
55
const async_hooks = require('async_hooks');
66

77
// Use small letters such that chrome://tracing groups by the name.

lib/internal/util.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
ERR_UNKNOWN_SIGNAL
77
} = require('internal/errors').codes;
88
const { signals } = process.binding('constants').os;
9-
109
const {
1110
getHiddenValue,
1211
setHiddenValue,

lib/internal/util/comparisons.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
const { compare } = process.binding('buffer');
44
const { isArrayBufferView } = require('internal/util/types');
5-
const { internalBinding } = require('internal/bootstrap/loaders');
6-
const { isDate, isMap, isRegExp, isSet } = internalBinding('types');
5+
const {
6+
isDate,
7+
isMap,
8+
isRegExp,
9+
isSet
10+
} = internalBinding('types');
711
const {
812
getOwnNonIndexProperties,
913
propertyFilter: {

lib/internal/util/inspect.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const { internalBinding } = require('internal/bootstrap/loaders');
4-
53
const {
64
getOwnNonIndexProperties,
75
getPromiseDetails,

0 commit comments

Comments
 (0)