Skip to content

Commit

Permalink
worker: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: nodejs#36267
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and cjihrig committed Dec 8, 2020
1 parent 26e0ffe commit 7ed9e0a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
16 changes: 11 additions & 5 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
'use strict';

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

Expand Down Expand Up @@ -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 });
},
Expand Down Expand Up @@ -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, {
Expand All @@ -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 ? {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/worker/js_transferable.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7ed9e0a

Please sign in to comment.