mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
worker: refactor to use more primordials
PR-URL: https://github.com/nodejs/node/pull/36267 Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
committed by
Node.js GitHub Bot
parent
f066ae4dd8
commit
325a1fcd64
@@ -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')
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayPrototypeMap,
|
||||
ArrayPrototypePush,
|
||||
FunctionPrototypeCall,
|
||||
ObjectAssign,
|
||||
ObjectCreate,
|
||||
ObjectDefineProperty,
|
||||
@@ -8,6 +11,7 @@ const {
|
||||
ObjectGetOwnPropertyDescriptors,
|
||||
ObjectGetPrototypeOf,
|
||||
ObjectSetPrototypeOf,
|
||||
ReflectApply,
|
||||
Symbol,
|
||||
} = primordials;
|
||||
|
||||
@@ -142,7 +146,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(type, { data });
|
||||
},
|
||||
@@ -186,7 +190,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, {
|
||||
@@ -197,7 +201,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 ? {
|
||||
@@ -225,18 +229,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,9 +294,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();
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user