diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-20 05:33:10 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-29 19:22:23 +0800 |
commit | c66c0732e0887c3f07a3e387d8a90b432eae134e (patch) | |
tree | 178a8258bd387778182d12206c1f0845ace4feba /lib | |
parent | 71a380b9feb693062bf7d6c42499fb4b759a1783 (diff) | |
download | android-node-v8-c66c0732e0887c3f07a3e387d8a90b432eae134e.tar.gz android-node-v8-c66c0732e0887c3f07a3e387d8a90b432eae134e.tar.bz2 android-node-v8-c66c0732e0887c3f07a3e387d8a90b432eae134e.zip |
src: lazily load internalBinding('uv') and build the errmap lazily
This removes the `internalBinding('uv')` call from the normal
bootstrap for now, and avoids building `errmap` by default which
expands to a lot of calls into V8.
PR-URL: https://github.com/nodejs/node/pull/25143
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/errors.js | 29 | ||||
-rw-r--r-- | lib/internal/util.js | 15 |
2 files changed, 33 insertions, 11 deletions
diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 8be692ef57..dae7f8600d 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -15,11 +15,6 @@ const kInfo = Symbol('info'); const messages = new Map(); const codes = {}; -const { - errmap, - UV_EAI_NODATA, - UV_EAI_NONAME -} = internalBinding('uv'); const { kMaxLength } = internalBinding('buffer'); const { defineProperty } = Object; @@ -237,6 +232,24 @@ function getMessage(key, args) { return util.format.apply(null, args); } +let uvBinding; + +function lazyUv() { + if (!uvBinding) { + uvBinding = internalBinding('uv'); + } + return uvBinding; +} + +function lazyErrmapGet(name) { + uvBinding = lazyUv(); + if (!uvBinding.errmap) { + uvBinding.errmap = uvBinding.getErrorMap(); + } + return uvBinding.errmap.get(name); +} + + /** * This creates an error compatible with errors produced in the C++ * function UVException using a context object with data assembled in C++. @@ -247,7 +260,7 @@ function getMessage(key, args) { * @returns {Error} */ function uvException(ctx) { - const [ code, uvmsg ] = errmap.get(ctx.errno); + const [ code, uvmsg ] = lazyErrmapGet(ctx.errno); let message = `${code}: ${ctx.message || uvmsg}, ${ctx.syscall}`; let path; @@ -303,7 +316,7 @@ function uvException(ctx) { * @returns {Error} */ function uvExceptionWithHostPort(err, syscall, address, port) { - const [ code, uvmsg ] = errmap.get(err); + const [ code, uvmsg ] = lazyErrmapGet(err); const message = `${syscall} ${code}: ${uvmsg}`; let details = ''; @@ -421,7 +434,7 @@ function dnsException(code, syscall, hostname) { if (typeof code === 'number') { // FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass // the true error to the user. ENOTFOUND is not even a proper POSIX error! - if (code === UV_EAI_NODATA || code === UV_EAI_NONAME) { + if (code === lazyUv().UV_EAI_NODATA || code === lazyUv().UV_EAI_NONAME) { code = 'ENOTFOUND'; // Fabricated error name. } else { code = lazyInternalUtil().getSystemErrorName(code); diff --git a/lib/internal/util.js b/lib/internal/util.js index 16c1e3370c..902375afdb 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -16,8 +16,6 @@ const { isNativeError } = require('internal/util/types'); -const { errmap } = internalBinding('uv'); - const noCrypto = !process.versions.openssl; const experimentalWarnings = new Set(); @@ -250,8 +248,19 @@ function getConstructorOf(obj) { return null; } +let uvBinding; +function lazyErrmapGet(name) { + if (!uvBinding) { + uvBinding = internalBinding('uv'); + } + if (!uvBinding.errmap) { + uvBinding.errmap = uvBinding.getErrorMap(); + } + return uvBinding.errmap.get(name); +} + function getSystemErrorName(err) { - const entry = errmap.get(err); + const entry = lazyErrmapGet(err); return entry ? entry[0] : `Unknown system error ${err}`; } |