From d4501b17e2d453b3c528c5806c4604b18c5c8f2b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 15 Nov 2020 20:34:00 +0100 Subject: [PATCH] process: refactor to use more primordials MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/36212 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell Reviewed-By: Pranshu Srivastava Reviewed-By: Rich Trott --- lib/internal/process/per_thread.js | 49 +++++++++++++++--------------- lib/internal/process/promises.js | 14 +++++---- lib/internal/process/signal.js | 7 +++-- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index b061dd340c78cc..b16705f9fb95b0 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -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; @@ -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. @@ -256,19 +259,19 @@ 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; @@ -276,11 +279,11 @@ function buildAllowedFlags() { } if (isAccepted) { let canonical = from; - if (canonical.endsWith('=')) - canonical = canonical.substr(0, canonical.length - 1); - if (canonical.endsWith(' ')) - canonical = canonical.substr(0, canonical.length - 4); - allowedNodeEnvironmentFlags.push(canonical); + if (StringPrototypeEndsWith(canonical, '=')) + canonical = StringPrototypeSlice(canonical, 0, canonical.length - 1); + if (StringPrototypeEndsWith(canonical, ' ')) + canonical = StringPrototypeSlice(canonical, 0, canonical.length - 4); + ArrayPrototypePush(allowedNodeEnvironmentFlags, canonical); } } @@ -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); @@ -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; } diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index ad21152dd12a08..023f7df0360d02 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -1,9 +1,11 @@ 'use strict'; const { + ArrayPrototypePush, + ArrayPrototypeShift, Error, ObjectDefineProperty, - WeakMap, + SafeWeakMap, } = primordials; const { @@ -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; @@ -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); } @@ -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; } @@ -178,7 +180,7 @@ 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); } @@ -186,7 +188,7 @@ function processPromiseRejections() { let len = pendingUnhandledRejections.length; while (len--) { - const promise = pendingUnhandledRejections.shift(); + const promise = ArrayPrototypeShift(pendingUnhandledRejections); const promiseInfo = maybeUnhandledPromises.get(promise); if (promiseInfo === undefined) { continue; diff --git a/lib/internal/process/signal.js b/lib/internal/process/signal.js index abc2b93dbfb7f3..0631fe2b29ab01 100644 --- a/lib/internal/process/signal.js +++ b/lib/internal/process/signal.js @@ -1,7 +1,8 @@ 'use strict'; const { - Map, + FunctionPrototypeBind, + SafeMap, } = primordials; const { @@ -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; @@ -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);