From 3d4785c174542647fa398bec186d191503ea64b7 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 16 Nov 2020 17:22:35 +0100 Subject: [PATCH] worker: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36267 Reviewed-By: Rich Trott --- lib/internal/worker.js | 16 +++++++++++----- lib/internal/worker/io.js | 21 +++++++++++++-------- lib/internal/worker/js_transferable.js | 7 +++++-- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/internal/worker.js b/lib/internal/worker.js index d38649c7fb1583..0965c5690fd06b 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -4,16 +4,21 @@ const { ArrayIsArray, + ArrayPrototypeMap, + ArrayPrototypePush, Float64Array, + FunctionPrototypeBind, JSONStringify, MathMax, ObjectCreate, ObjectEntries, Promise, PromiseResolve, + RegExpPrototypeTest, String, Symbol, SymbolFor, + TypedArrayPrototypeFill, Uint32Array, } = primordials; @@ -104,7 +109,7 @@ class Worker extends EventEmitter { if (!ArrayIsArray(options.argv)) { throw new ERR_INVALID_ARG_TYPE('options.argv', 'Array', options.argv); } - argv = options.argv.map(String); + argv = ArrayPrototypeMap(options.argv, String); } let url, doEval; @@ -133,7 +138,8 @@ class Worker extends EventEmitter { ['string', 'URL'], filename ); - } else if (path.isAbsolute(filename) || /^\.\.?[\\/]/.test(filename)) { + } else if (path.isAbsolute(filename) || + RegExpPrototypeTest(/^\.\.?[\\/]/, filename)) { filename = path.resolve(filename); url = pathToFileURL(filename); } else { @@ -203,7 +209,7 @@ class Worker extends EventEmitter { const transferList = [port2]; // If transferList is provided. if (options.transferList) - transferList.push(...options.transferList); + ArrayPrototypePush(transferList, ...options.transferList); this[kPublicPort] = port1; for (const event of ['message', 'messageerror']) { @@ -230,7 +236,7 @@ class Worker extends EventEmitter { this[kLoopStartTime] = -1; this[kIsOnline] = false; this.performance = { - eventLoopUtilization: eventLoopUtilization.bind(this), + eventLoopUtilization: FunctionPrototypeBind(eventLoopUtilization, this), }; // Actually start the new thread now that everything is in place. this[kHandle].startThread(); @@ -402,7 +408,7 @@ function pipeWithoutWarning(source, dest) { const resourceLimitsArray = new Float64Array(kTotalResourceLimitCount); function parseResourceLimits(obj) { const ret = resourceLimitsArray; - ret.fill(-1); + TypedArrayPrototypeFill(ret, -1); if (typeof obj !== 'object' || obj === null) return ret; if (typeof obj.maxOldGenerationSizeMb === 'number') diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 406c8e77fd3375..2066cad1c84c05 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -1,12 +1,16 @@ 'use strict'; const { + ArrayPrototypeMap, + ArrayPrototypePush, + FunctionPrototypeCall, ObjectAssign, ObjectCreate, ObjectDefineProperty, ObjectGetOwnPropertyDescriptors, ObjectGetPrototypeOf, ObjectSetPrototypeOf, + ReflectApply, Symbol, } = primordials; @@ -87,7 +91,7 @@ ObjectDefineProperty( { value: function(data, type) { if (type !== 'message' && type !== 'messageerror') { - return originalCreateEvent.call(this, data, type); + return ReflectApply(originalCreateEvent, this, arguments); } return new MessageEvent(data, this, type); }, @@ -131,7 +135,7 @@ ObjectDefineProperty(MessagePort.prototype, handleOnCloseSymbol, { MessagePort.prototype.close = function(cb) { if (typeof cb === 'function') this.once('close', cb); - MessagePortPrototype.close.call(this); + FunctionPrototypeCall(MessagePortPrototype.close, this); }; ObjectDefineProperty(MessagePort.prototype, inspect.custom, { @@ -142,7 +146,7 @@ ObjectDefineProperty(MessagePort.prototype, inspect.custom, { try { // This may throw when `this` does not refer to a native object, // e.g. when accessing the prototype directly. - ref = MessagePortPrototype.hasRef.call(this); + ref = FunctionPrototypeCall(MessagePortPrototype.hasRef, this); } catch { return this; } return ObjectAssign(ObjectCreate(MessagePort.prototype), ref === undefined ? { @@ -170,18 +174,18 @@ function setupPortReferencing(port, eventEmitter, eventName) { const origNewListener = eventEmitter[kNewListener]; eventEmitter[kNewListener] = function(size, type, ...args) { if (type === eventName) newListener(size - 1); - return origNewListener.call(this, size, type, ...args); + return ReflectApply(origNewListener, this, arguments); }; const origRemoveListener = eventEmitter[kRemoveListener]; eventEmitter[kRemoveListener] = function(size, type, ...args) { if (type === eventName) removeListener(size); - return origRemoveListener.call(this, size, type, ...args); + return ReflectApply(origRemoveListener, this, arguments); }; function newListener(size) { if (size === 0) { port.ref(); - MessagePortPrototype.start.call(port); + FunctionPrototypeCall(MessagePortPrototype.start, port); } } @@ -235,9 +239,10 @@ class WritableWorkerStdio extends Writable { this[kPort].postMessage({ type: messageTypes.STDIO_PAYLOAD, stream: this[kName], - chunks: chunks.map(({ chunk, encoding }) => ({ chunk, encoding })) + chunks: ArrayPrototypeMap(chunks, + ({ chunk, encoding }) => ({ chunk, encoding })), }); - this[kWritableCallbacks].push(cb); + ArrayPrototypePush(this[kWritableCallbacks], cb); if (this[kPort][kWaitingStreams]++ === 0) this[kPort].ref(); } diff --git a/lib/internal/worker/js_transferable.js b/lib/internal/worker/js_transferable.js index 707fd03f2f6d0e..5b822ef8a0bcb1 100644 --- a/lib/internal/worker/js_transferable.js +++ b/lib/internal/worker/js_transferable.js @@ -1,5 +1,8 @@ 'use strict'; -const { Error } = primordials; +const { + Error, + StringPrototypeSplit, +} = primordials; const { messaging_deserialize_symbol, messaging_transfer_symbol, @@ -16,7 +19,7 @@ function setup() { // from .postMessage() calls. The format of `deserializeInfo` is generally // 'module:Constructor', e.g. 'internal/fs/promises:FileHandle'. setDeserializerCreateObjectFunction((deserializeInfo) => { - const [ module, ctor ] = deserializeInfo.split(':'); + const [ module, ctor ] = StringPrototypeSplit(deserializeInfo, ':'); const Ctor = require(module)[ctor]; if (typeof Ctor !== 'function' || !(Ctor.prototype instanceof JSTransferable)) {