Skip to content

Commit

Permalink
lib: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36140
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
aduh95 authored and codebytere committed Nov 22, 2020
1 parent f0aed8c commit 1a4d43f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
39 changes: 24 additions & 15 deletions lib/internal/main/print_help.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
'use strict';

const {
ArrayPrototypeConcat,
ArrayPrototypeSort,
Boolean,
Map,
MathFloor,
MathMax,
ObjectKeys,
RegExp,
StringPrototypeTrimLeft,
StringPrototypeRepeat,
StringPrototypeReplace,
SafeMap,
} = primordials;

const { types } = internalBinding('options');
Expand All @@ -23,7 +28,7 @@ for (const key of ObjectKeys(types))
// Environment variables are parsed ad-hoc throughout the code base,
// so we gather the documentation here.
const { hasIntl, hasSmallICU, hasNodeOptions } = internalBinding('config');
const envVars = new Map([
const envVars = new SafeMap(ArrayPrototypeConcat([
['NODE_DEBUG', { helpText: "','-separated list of core modules that " +
'should print debug information' }],
['NODE_DEBUG_NATIVE', { helpText: "','-separated list of C++ core debug " +
Expand Down Expand Up @@ -51,28 +56,30 @@ const envVars = new Map([
'to' }],
['UV_THREADPOOL_SIZE', { helpText: 'sets the number of threads used in ' +
'libuv\'s threadpool' }]
].concat(hasIntl ? [
], hasIntl ? [
['NODE_ICU_DATA', { helpText: 'data path for ICU (Intl object) data' +
hasSmallICU ? '' : ' (will extend linked-in data)' }]
] : []).concat(hasNodeOptions ? [
] : []), (hasNodeOptions ? [
['NODE_OPTIONS', { helpText: 'set CLI options in the environment via a ' +
'space-separated list' }]
] : []).concat(hasCrypto ? [
] : []), hasCrypto ? [
['OPENSSL_CONF', { helpText: 'load OpenSSL configuration from file' }],
['SSL_CERT_DIR', { helpText: 'sets OpenSSL\'s directory of trusted ' +
'certificates when used in conjunction with --use-openssl-ca' }],
['SSL_CERT_FILE', { helpText: 'sets OpenSSL\'s trusted certificate file ' +
'when used in conjunction with --use-openssl-ca' }],
] : []));
] : []);


function indent(text, depth) {
return text.replace(/^/gm, ' '.repeat(depth));
return StringPrototypeReplace(text, /^/gm, StringPrototypeRepeat(' ', depth));
}

function fold(text, width) {
return text.replace(new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
(_, newLine, end) => newLine + (end === ' ' ? '\n' : ''));
return StringPrototypeReplace(text,
new RegExp(`([^\n]{0,${width}})( |$)`, 'g'),
(_, newLine, end) =>
newLine + (end === ' ' ? '\n' : ''));
}

function getArgDescription(type) {
Expand All @@ -94,13 +101,15 @@ function getArgDescription(type) {
}
}

function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
function format(
{ options, aliases = new SafeMap(), firstColumn, secondColumn }
) {
let text = '';
let maxFirstColumnUsed = 0;

for (const [
name, { helpText, type, value }
] of [...options.entries()].sort()) {
] of ArrayPrototypeSort([...options.entries()])) {
if (!helpText) continue;

let displayName = name;
Expand Down Expand Up @@ -136,12 +145,12 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
text += displayName;
maxFirstColumnUsed = MathMax(maxFirstColumnUsed, displayName.length);
if (displayName.length >= firstColumn)
text += '\n' + ' '.repeat(firstColumn);
text += '\n' + StringPrototypeRepeat(' ', firstColumn);
else
text += ' '.repeat(firstColumn - displayName.length);
text += StringPrototypeRepeat(' ', firstColumn - displayName.length);

text += indent(fold(displayHelpText, secondColumn),
firstColumn).trimLeft() + '\n';
text += indent(StringPrototypeTrimLeft(fold(displayHelpText, secondColumn),
firstColumn)) + '\n';
}

if (maxFirstColumnUsed < firstColumn - 4) {
Expand Down
11 changes: 7 additions & 4 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
// message port.

const {
ArrayPrototypeConcat,
ArrayPrototypeSplice,
ObjectDefineProperty,
PromisePrototypeCatch,
} = primordials;

const {
Expand Down Expand Up @@ -122,7 +125,7 @@ port.on('message', (message) => {
loadPreloadModules();
initializeFrozenIntrinsics();
if (argv !== undefined) {
process.argv = process.argv.concat(argv);
process.argv = ArrayPrototypeConcat(process.argv, argv);
}
publicWorker.parentPort = publicPort;
publicWorker.workerData = workerData;
Expand Down Expand Up @@ -159,18 +162,18 @@ port.on('message', (message) => {
enumerable: true,
value: filename,
});
process.argv.splice(1, 0, name);
ArrayPrototypeSplice(process.argv, 1, 0, name);
evalScript(name, filename);
} else if (doEval === 'module') {
const { evalModule } = require('internal/process/execution');
evalModule(filename).catch((e) => {
PromisePrototypeCatch(evalModule(filename), (e) => {
workerOnGlobalUncaughtException(e, true);
});
} else {
// script filename
// runMain here might be monkey-patched by users in --require.
// XXX: the monkey-patchability here should probably be deprecated.
process.argv.splice(1, 0, filename);
ArrayPrototypeSplice(process.argv, 1, 0, filename);
CJSLoader.Module.runMain(filename);
}
} else if (message.type === STDIO_PAYLOAD) {
Expand Down

0 comments on commit 1a4d43f

Please sign in to comment.