Skip to content

Commit

Permalink
process: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36212
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Pranshu Srivastava <rexagod@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and Trott committed Nov 24, 2020
1 parent 68795f9 commit d4501b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
49 changes: 24 additions & 25 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@

const {
ArrayIsArray,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSplice,
BigUint64Array,
Float64Array,
NumberMAX_SAFE_INTEGER,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectFreeze,
ObjectGetOwnPropertyDescriptors,
ReflectApply,
RegExpPrototypeTest,
Set,
SetPrototype,
SetPrototypeHas,
SafeSet,
StringPrototypeEndsWith,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeStartsWith,
Uint32Array,
} = primordials;

Expand Down Expand Up @@ -94,7 +97,7 @@ function wrapProcessMethods(binding) {
} = binding;

function _rawDebug(...args) {
binding._rawDebug(format.apply(null, args));
binding._rawDebug(ReflectApply(format, null, args));
}

// Create the argument array that will be passed to the native function.
Expand Down Expand Up @@ -256,31 +259,31 @@ function buildAllowedFlags() {
const allowedNodeEnvironmentFlags = [];
for (const [name, info] of options) {
if (info.envVarSettings === kAllowedInEnvironment) {
allowedNodeEnvironmentFlags.push(name);
ArrayPrototypePush(allowedNodeEnvironmentFlags, name);
}
}

for (const [ from, expansion ] of aliases) {
let isAccepted = true;
for (const to of expansion) {
if (!to.startsWith('-') || to === '--') continue;
if (!StringPrototypeStartsWith(to, '-') || to === '--') continue;
const recursiveExpansion = aliases.get(to);
if (recursiveExpansion) {
if (recursiveExpansion[0] === to)
recursiveExpansion.splice(0, 1);
expansion.push(...recursiveExpansion);
ArrayPrototypeSplice(recursiveExpansion, 0, 1);
ArrayPrototypePush(expansion, ...recursiveExpansion);
continue;
}
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
if (!isAccepted) break;
}
if (isAccepted) {
let canonical = from;
if (canonical.endsWith('='))
canonical = canonical.substr(0, canonical.length - 1);
if (canonical.endsWith(' <arg>'))
canonical = canonical.substr(0, canonical.length - 4);
allowedNodeEnvironmentFlags.push(canonical);
if (StringPrototypeEndsWith(canonical, '='))
canonical = StringPrototypeSlice(canonical, 0, canonical.length - 1);
if (StringPrototypeEndsWith(canonical, ' <arg>'))
canonical = StringPrototypeSlice(canonical, 0, canonical.length - 4);
ArrayPrototypePush(allowedNodeEnvironmentFlags, canonical);
}
}

Expand All @@ -289,14 +292,10 @@ function buildAllowedFlags() {

// Save these for comparison against flags provided to
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
// Avoid interference w/ user code by flattening `Set.prototype` into
// each object.
const nodeFlags = ObjectDefineProperties(
new Set(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
ObjectGetOwnPropertyDescriptors(SetPrototype)
);

class NodeEnvironmentFlagsSet extends Set {
const nodeFlags = new SafeSet(ArrayPrototypeMap(allowedNodeEnvironmentFlags,
trimLeadingDashes));

class NodeEnvironmentFlagsSet extends SafeSet {
constructor(...args) {
super(...args);

Expand Down Expand Up @@ -328,9 +327,9 @@ function buildAllowedFlags() {
key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-');
if (RegExpPrototypeTest(leadingDashesRegex, key)) {
key = StringPrototypeReplace(key, trailingValuesRegex, '');
return SetPrototypeHas(this, key);
return super.has(key);
}
return SetPrototypeHas(nodeFlags, key);
return nodeFlags.has(key);
}
return false;
}
Expand Down
14 changes: 8 additions & 6 deletions lib/internal/process/promises.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const {
ArrayPrototypePush,
ArrayPrototypeShift,
Error,
ObjectDefineProperty,
WeakMap,
SafeWeakMap,
} = primordials;

const {
Expand All @@ -25,7 +27,7 @@ const {
// *Must* match Environment::TickInfo::Fields in src/env.h.
const kHasRejectionToWarn = 1;

const maybeUnhandledPromises = new WeakMap();
const maybeUnhandledPromises = new SafeWeakMap();
const pendingUnhandledRejections = [];
const asyncHandledRejections = [];
let lastPromiseId = 0;
Expand Down Expand Up @@ -121,7 +123,7 @@ function unhandledRejection(promise, reason) {
domain: process.domain
});
// This causes the promise to be referenced at least for one tick.
pendingUnhandledRejections.push(promise);
ArrayPrototypePush(pendingUnhandledRejections, promise);
setHasRejectionToWarn(true);
}

Expand All @@ -137,7 +139,7 @@ function handledRejection(promise) {
`asynchronously (rejection id: ${uid})`);
warning.name = 'PromiseRejectionHandledWarning';
warning.id = uid;
asyncHandledRejections.push({ promise, warning });
ArrayPrototypePush(asyncHandledRejections, { promise, warning });
setHasRejectionToWarn(true);
return;
}
Expand Down Expand Up @@ -178,15 +180,15 @@ function processPromiseRejections() {
let maybeScheduledTicksOrMicrotasks = asyncHandledRejections.length > 0;

while (asyncHandledRejections.length > 0) {
const { promise, warning } = asyncHandledRejections.shift();
const { promise, warning } = ArrayPrototypeShift(asyncHandledRejections);
if (!process.emit('rejectionHandled', promise)) {
process.emitWarning(warning);
}
}

let len = pendingUnhandledRejections.length;
while (len--) {
const promise = pendingUnhandledRejections.shift();
const promise = ArrayPrototypeShift(pendingUnhandledRejections);
const promiseInfo = maybeUnhandledPromises.get(promise);
if (promiseInfo === undefined) {
continue;
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/process/signal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const {
Map,
FunctionPrototypeBind,
SafeMap,
} = primordials;

const {
Expand All @@ -11,7 +12,7 @@ const {
const { signals } = internalBinding('constants').os;

let Signal;
const signalWraps = new Map();
const signalWraps = new SafeMap();

function isSignal(event) {
return typeof event === 'string' && signals[event] !== undefined;
Expand All @@ -26,7 +27,7 @@ function startListeningIfSignal(type) {

wrap.unref();

wrap.onsignal = process.emit.bind(process, type, type);
wrap.onsignal = FunctionPrototypeBind(process.emit, process, type, type);

const signum = signals[type];
const err = wrap.start(signum);
Expand Down

0 comments on commit d4501b1

Please sign in to comment.