Skip to content

Commit

Permalink
bootstrapper: move internalBinding to NativeModule
Browse files Browse the repository at this point in the history
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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>

Backport-PR-URL: #23661
Backport-Reviewed-By: Gus Caplan <me@gus.host>
Backport-Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Backport-Reviewed-By: Michaël Zasso <targos@protonmail.com>
Backport-Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Backport-Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
devsnek authored and MylesBorins committed Nov 29, 2018
1 parent c854879 commit e872089
Show file tree
Hide file tree
Showing 51 changed files with 59 additions and 67 deletions.
1 change: 1 addition & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ globals:
DCHECK_LE: false
DCHECK_LT: false
DCHECK_NE: false
internalBinding: false
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
const util = require('util');
const net = require('net');
const url = require('url');
const { HTTPParser } = process.binding('http_parser');
const { HTTPParser } = internalBinding('http_parser');
const assert = require('assert').ok;
const {
_checkIsHttpToken: checkIsHttpToken,
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

'use strict';

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

const FreeList = require('internal/freelist');
const { ondrain } = require('internal/http');
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

const util = require('util');
const net = require('net');
const { HTTPParser } = process.binding('http_parser');
const { HTTPParser } = internalBinding('http_parser');
const assert = require('assert').ok;
const {
parsers,
Expand Down
2 changes: 1 addition & 1 deletion lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const { SSL_OP_CIPHER_SERVER_PREFERENCE } = process.binding('constants').crypto;
// Lazily loaded
var crypto = null;

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

function SecureContext(secureProtocol, secureOptions, context) {
if (!(this instanceof SecureContext)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const common = require('_tls_common');
const { StreamWrap } = require('_stream_wrap');
const { Buffer } = require('buffer');
const debug = util.debuglog('tls');
const tls_wrap = process.binding('tls_wrap');
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
const { TCP, constants: TCPConstants } = internalBinding('tcp_wrap');
const tls_wrap = internalBinding('tls_wrap');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
const { owner_symbol } = require('internal/async_hooks').symbols;
const {
SecureContext: NativeSecureContext
Expand Down
1 change: 0 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const {
// that test/parallel/test-buffer-bindingobj-no-zerofill.js is written.
let isAnyArrayBuffer;
try {
const { internalBinding } = require('internal/bootstrap/loaders');
isAnyArrayBuffer = internalBinding('types').isAnyArrayBuffer;
} catch (e) {
isAnyArrayBuffer = require('util').types.isAnyArrayBuffer;
Expand Down
2 changes: 1 addition & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
const { isArrayBufferView } = require('internal/util/types');
const debug = util.debuglog('child_process');
const { Buffer } = require('buffer');
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
const {
ERR_INVALID_ARG_VALUE,
ERR_CHILD_PROCESS_IPC_REQUIRED,
Expand Down
2 changes: 1 addition & 1 deletion lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const {
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
const { previewEntries } = process.binding('util');
const { previewEntries } = internalBinding('util');
const { Buffer: { isBuffer } } = require('buffer');
const util = require('util');
const {
Expand Down
2 changes: 1 addition & 1 deletion lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

'use strict';

const cares = process.binding('cares_wrap');
const cares = internalBinding('cares_wrap');
const { isIP, isIPv4, isLegalPort } = require('internal/net');
const { customPromisifyArgs } = require('internal/util');
const errors = require('internal/errors');
Expand Down
1 change: 0 additions & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const {
ERR_UNHANDLED_ERROR
} = require('internal/errors').codes;
const { createHook } = require('async_hooks');
const { internalBinding } = require('internal/bootstrap/loaders');

// overwrite process.domain with a getter/setter that will allow for more
// effective optimizations
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
ERR_ASYNC_TYPE,
ERR_INVALID_ASYNC_ID
} = require('internal/errors').codes;
const async_wrap = process.binding('async_wrap');
const async_wrap = internalBinding('async_wrap');
/* async_hook_fields is a Uint32Array wrapping the uint32_t array of
* Environment::AsyncHooks::fields_[]. Each index tracks the number of active
* hooks for each type.
Expand Down
1 change: 0 additions & 1 deletion lib/internal/bash_completion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const { internalBinding } = require('internal/bootstrap/loaders');
const { getOptions } = internalBinding('options');

function print(stream) {
Expand Down
14 changes: 11 additions & 3 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@
internalBinding = function internalBinding(module) {
let mod = bindingObj[module];
if (typeof mod !== 'object') {
mod = bindingObj[module] = getInternalBinding(module);
try {
mod = getInternalBinding(module);
} catch {
// v10.x only: Fall back to `process.binding()`,
// to avoid future merge conflicts when backporting changes that use
// `internalBinding()` to v10.x.
mod = process.binding(module);
}
bindingObj[module] = mod;
moduleLoadList.push(`Internal Binding ${module}`);
}
return mod;
Expand Down Expand Up @@ -223,7 +231,7 @@
};

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

Expand Down Expand Up @@ -294,7 +302,7 @@
const requireFn = this.id.startsWith('internal/deps/') ?
NativeModule.requireForDeps :
NativeModule.require;
fn(this.exports, requireFn, this, process);
fn(this.exports, requireFn, this, process, internalBinding);

if (config.experimentalModules && !NativeModule.isInternal(this.id)) {
this.exportKeys = ObjectKeys(this.exports);
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const dgram = require('dgram');
const util = require('util');
const assert = require('assert');

const { Process } = process.binding('process_wrap');
const { WriteWrap } = process.binding('stream_wrap');
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
const { TTY } = process.binding('tty_wrap');
const { TCP } = process.binding('tcp_wrap');
const { UDP } = process.binding('udp_wrap');
const { Process } = internalBinding('process_wrap');
const { WriteWrap } = internalBinding('stream_wrap');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
const { TCP } = internalBinding('tcp_wrap');
const { TTY } = internalBinding('tty_wrap');
const { UDP } = internalBinding('udp_wrap');
const SocketList = require('internal/socket_list');
const { owner_symbol } = require('internal/async_hooks').symbols;
const { convertToValidSignal } = require('internal/util');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const assert = require('assert');
const net = require('net');
const { sendHelper } = require('internal/cluster/utils');
const uv = process.binding('uv');
const uv = internalBinding('uv');

module.exports = RoundRobinHandle;

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/keygen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { AsyncWrap, Providers } = process.binding('async_wrap');
const { AsyncWrap, Providers } = internalBinding('async_wrap');
const {
generateKeyPairRSA,
generateKeyPairDSA,
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { AsyncWrap, Providers } = process.binding('async_wrap');
const { AsyncWrap, Providers } = internalBinding('async_wrap');
const { Buffer } = require('buffer');
const { pbkdf2: _pbkdf2 } = process.binding('crypto');
const { validateUint32 } = require('internal/validators');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { AsyncWrap, Providers } = process.binding('async_wrap');
const { AsyncWrap, Providers } = internalBinding('async_wrap');
const { Buffer, kMaxLength } = require('buffer');
const { randomBytes: _randomBytes } = process.binding('crypto');
const {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { AsyncWrap, Providers } = process.binding('async_wrap');
const { AsyncWrap, Providers } = internalBinding('async_wrap');
const { Buffer } = require('buffer');
const { scrypt: _scrypt } = process.binding('crypto');
const { validateUint32 } = require('internal/validators');
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const {
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const {
Sign: _Sign,
Verify: _Verify
} = process.binding('crypto');
const { Sign: _Sign, Verify: _Verify } = internalBinding('crypto');
const {
RSA_PSS_SALTLEN_AUTO,
RSA_PKCS1_PADDING
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/dgram.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const assert = require('assert');
const { codes } = require('internal/errors');
const { UDP } = process.binding('udp_wrap');
const { UDP } = internalBinding('udp_wrap');
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
const kStateSymbol = Symbol('state symbol');
let dns; // Lazy load for startup performance.
Expand Down
1 change: 0 additions & 1 deletion lib/internal/domexception.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

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

Expand Down
1 change: 0 additions & 1 deletion lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const {

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

const { internalBinding } = require('internal/bootstrap/loaders');
const {
isArrayBuffer
} = internalBinding('types');
Expand Down
1 change: 0 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const {
owner_symbol,
},
} = require('internal/async_hooks');
const { internalBinding } = require('internal/bootstrap/loaders');
const {
codes: {
ERR_HTTP2_ALTSVC_INVALID_ORIGIN,
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const binding = process.binding('http2');
const binding = internalBinding('http2');
const {
ERR_HTTP2_HEADER_SINGLE_VALUE,
ERR_HTTP2_INVALID_CONNECTION_HEADERS,
Expand Down
1 change: 0 additions & 1 deletion lib/internal/modules/esm/create_dynamic_module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const { internalBinding } = require('internal/bootstrap/loaders');
const { ModuleWrap } = internalBinding('module_wrap');
const debug = require('util').debuglog('esm');
const ArrayJoin = Function.call.bind(Array.prototype.join);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { URL } = require('url');
const CJSmodule = require('internal/modules/cjs/loader');
const internalFS = require('internal/fs/utils');
const { NativeModule, internalBinding } = require('internal/bootstrap/loaders');
const { NativeModule } = require('internal/bootstrap/loaders');
const { extname } = require('path');
const { realpathSync } = require('fs');
const preserveSymlinks = !!process.binding('config').preserveSymlinks;
Expand Down
1 change: 0 additions & 1 deletion lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const { internalBinding } = require('internal/bootstrap/loaders');
const { ModuleWrap } = internalBinding('module_wrap');
const { SafeSet, SafePromise } = require('internal/safe_globals');
const { decorateErrorStack } = require('internal/util');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { NativeModule, internalBinding } = require('internal/bootstrap/loaders');
const { NativeModule } = require('internal/bootstrap/loaders');
const { ModuleWrap } = internalBinding('module_wrap');
const {
stripShebang,
Expand Down
1 change: 0 additions & 1 deletion lib/internal/print_help.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const { internalBinding } = require('internal/bootstrap/loaders');
const { getOptions, types } = internalBinding('options');

const typeLookup = [];
Expand Down
1 change: 0 additions & 1 deletion lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const { internalBinding } = require('internal/bootstrap/loaders');
const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/process/promises.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { safeToString } = process.binding('util');
const { safeToString } = internalBinding('util');

const maybeUnhandledPromises = new WeakMap();
const pendingUnhandledRejections = [];
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/process/stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function getMainThreadStdio() {

function getStdin() {
if (stdin) return stdin;
const tty_wrap = process.binding('tty_wrap');
const tty_wrap = internalBinding('tty_wrap');
const fd = 0;

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

function createWritableStdioStream(fd) {
var stream;
const tty_wrap = process.binding('tty_wrap');
const tty_wrap = internalBinding('tty_wrap');

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

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const { Buffer } = require('buffer');
const { WriteWrap } = process.binding('stream_wrap');
const { UV_EOF } = process.binding('uv');
const { WriteWrap } = internalBinding('stream_wrap');
const { UV_EOF } = internalBinding('uv');
const { errnoException } = require('internal/errors');
const { owner_symbol } = require('internal/async_hooks').symbols;

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/test/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ process.emitWarning(
// These exports should be scoped as specifically as possible
// to avoid exposing APIs because even with that warning and
// this file being internal people will still try to abuse it.
const { internalBinding } = require('internal/bootstrap/loaders');
module.exports = {
ModuleWrap: internalBinding('module_wrap').ModuleWrap,
internalBinding
};
1 change: 0 additions & 1 deletion lib/internal/test/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ process.emitWarning(
'tracked by any versioning system or deprecation process.',
'internal/test/heap');

const { internalBinding } = require('internal/bootstrap/loaders');
const { createHeapDump, buildEmbedderGraph } = internalBinding('heap_utils');
const assert = require('assert');

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/trace_events_async_hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

exports.setup = function(traceEvents, traceEventCategory) {
const async_wrap = process.binding('async_wrap');
const async_wrap = internalBinding('async_wrap');
const async_hooks = require('async_hooks');

// Use small letters such that chrome://tracing groups by the name.
Expand Down
1 change: 0 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {
ERR_UNKNOWN_SIGNAL
} = require('internal/errors').codes;
const { signals } = process.binding('constants').os;

const {
getHiddenValue,
setHiddenValue,
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

const { compare } = process.binding('buffer');
const { isArrayBufferView } = require('internal/util/types');
const { internalBinding } = require('internal/bootstrap/loaders');
const { isDate, isMap, isRegExp, isSet } = internalBinding('types');
const {
isDate,
isMap,
isRegExp,
isSet
} = internalBinding('types');
const {
getOwnNonIndexProperties,
propertyFilter: {
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const { internalBinding } = require('internal/bootstrap/loaders');

const {
getOwnNonIndexProperties,
getPromiseDetails,
Expand Down
Loading

0 comments on commit e872089

Please sign in to comment.