Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: refactor to use more primordials #36267

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/internal/worker.js
Expand Up @@ -4,16 +4,21 @@

const {
ArrayIsArray,
ArrayPrototypeMap,
ArrayPrototypePush,
Float64Array,
FunctionPrototypeBind,
JSONStringify,
MathMax,
ObjectCreate,
ObjectEntries,
Promise,
PromiseResolve,
RegExpPrototypeTest,
String,
Symbol,
SymbolFor,
TypedArrayPrototypeFill,
Uint32Array,
} = primordials;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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']) {
Expand All @@ -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();
Expand Down Expand Up @@ -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')
Expand Down
21 changes: 13 additions & 8 deletions lib/internal/worker/io.js
@@ -1,13 +1,17 @@
'use strict';

const {
ArrayPrototypeMap,
ArrayPrototypePush,
FunctionPrototypeCall,
ObjectAssign,
ObjectCreate,
ObjectDefineProperty,
ObjectDefineProperties,
ObjectGetOwnPropertyDescriptors,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
} = primordials;

Expand Down Expand Up @@ -134,7 +138,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 });
},
Expand Down Expand Up @@ -178,7 +182,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, {
Expand All @@ -189,7 +193,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 ? {
Expand Down Expand Up @@ -217,18 +221,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);
}
}

Expand Down Expand Up @@ -282,9 +286,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();
}
Expand Down
7 changes: 5 additions & 2 deletions 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,
Expand All @@ -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)) {
Expand Down