Skip to content

Commit 21c4704

Browse files
aduh95danielleadams
authored andcommitted
worker: refactor to use more primordials
PR-URL: #36267 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent b20409e commit 21c4704

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

lib/internal/worker.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44

55
const {
66
ArrayIsArray,
7+
ArrayPrototypeMap,
8+
ArrayPrototypePush,
79
Float64Array,
10+
FunctionPrototypeBind,
811
JSONStringify,
912
MathMax,
1013
ObjectCreate,
1114
ObjectEntries,
1215
Promise,
1316
PromiseResolve,
17+
RegExpPrototypeTest,
1418
String,
1519
Symbol,
1620
SymbolFor,
21+
TypedArrayPrototypeFill,
1722
Uint32Array,
1823
} = primordials;
1924

@@ -104,7 +109,7 @@ class Worker extends EventEmitter {
104109
if (!ArrayIsArray(options.argv)) {
105110
throw new ERR_INVALID_ARG_TYPE('options.argv', 'Array', options.argv);
106111
}
107-
argv = options.argv.map(String);
112+
argv = ArrayPrototypeMap(options.argv, String);
108113
}
109114

110115
let url, doEval;
@@ -133,7 +138,8 @@ class Worker extends EventEmitter {
133138
['string', 'URL'],
134139
filename
135140
);
136-
} else if (path.isAbsolute(filename) || /^\.\.?[\\/]/.test(filename)) {
141+
} else if (path.isAbsolute(filename) ||
142+
RegExpPrototypeTest(/^\.\.?[\\/]/, filename)) {
137143
filename = path.resolve(filename);
138144
url = pathToFileURL(filename);
139145
} else {
@@ -203,7 +209,7 @@ class Worker extends EventEmitter {
203209
const transferList = [port2];
204210
// If transferList is provided.
205211
if (options.transferList)
206-
transferList.push(...options.transferList);
212+
ArrayPrototypePush(transferList, ...options.transferList);
207213

208214
this[kPublicPort] = port1;
209215
for (const event of ['message', 'messageerror']) {
@@ -230,7 +236,7 @@ class Worker extends EventEmitter {
230236
this[kLoopStartTime] = -1;
231237
this[kIsOnline] = false;
232238
this.performance = {
233-
eventLoopUtilization: eventLoopUtilization.bind(this),
239+
eventLoopUtilization: FunctionPrototypeBind(eventLoopUtilization, this),
234240
};
235241
// Actually start the new thread now that everything is in place.
236242
this[kHandle].startThread();
@@ -402,7 +408,7 @@ function pipeWithoutWarning(source, dest) {
402408
const resourceLimitsArray = new Float64Array(kTotalResourceLimitCount);
403409
function parseResourceLimits(obj) {
404410
const ret = resourceLimitsArray;
405-
ret.fill(-1);
411+
TypedArrayPrototypeFill(ret, -1);
406412
if (typeof obj !== 'object' || obj === null) return ret;
407413

408414
if (typeof obj.maxOldGenerationSizeMb === 'number')

lib/internal/worker/io.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypeMap,
5+
ArrayPrototypePush,
6+
FunctionPrototypeCall,
47
ObjectAssign,
58
ObjectCreate,
69
ObjectDefineProperty,
710
ObjectDefineProperties,
811
ObjectGetOwnPropertyDescriptors,
912
ObjectGetPrototypeOf,
1013
ObjectSetPrototypeOf,
14+
ReflectApply,
1115
Symbol,
1216
} = primordials;
1317

@@ -142,7 +146,7 @@ ObjectDefineProperty(
142146
{
143147
value: function(data, type) {
144148
if (type !== 'message' && type !== 'messageerror') {
145-
return originalCreateEvent.call(this, data, type);
149+
return ReflectApply(originalCreateEvent, this, arguments);
146150
}
147151
return new MessageEvent(type, { data });
148152
},
@@ -186,7 +190,7 @@ ObjectDefineProperty(MessagePort.prototype, handleOnCloseSymbol, {
186190
MessagePort.prototype.close = function(cb) {
187191
if (typeof cb === 'function')
188192
this.once('close', cb);
189-
MessagePortPrototype.close.call(this);
193+
FunctionPrototypeCall(MessagePortPrototype.close, this);
190194
};
191195

192196
ObjectDefineProperty(MessagePort.prototype, inspect.custom, {
@@ -197,7 +201,7 @@ ObjectDefineProperty(MessagePort.prototype, inspect.custom, {
197201
try {
198202
// This may throw when `this` does not refer to a native object,
199203
// e.g. when accessing the prototype directly.
200-
ref = MessagePortPrototype.hasRef.call(this);
204+
ref = FunctionPrototypeCall(MessagePortPrototype.hasRef, this);
201205
} catch { return this; }
202206
return ObjectAssign(ObjectCreate(MessagePort.prototype),
203207
ref === undefined ? {
@@ -225,18 +229,18 @@ function setupPortReferencing(port, eventEmitter, eventName) {
225229
const origNewListener = eventEmitter[kNewListener];
226230
eventEmitter[kNewListener] = function(size, type, ...args) {
227231
if (type === eventName) newListener(size - 1);
228-
return origNewListener.call(this, size, type, ...args);
232+
return ReflectApply(origNewListener, this, arguments);
229233
};
230234
const origRemoveListener = eventEmitter[kRemoveListener];
231235
eventEmitter[kRemoveListener] = function(size, type, ...args) {
232236
if (type === eventName) removeListener(size);
233-
return origRemoveListener.call(this, size, type, ...args);
237+
return ReflectApply(origRemoveListener, this, arguments);
234238
};
235239

236240
function newListener(size) {
237241
if (size === 0) {
238242
port.ref();
239-
MessagePortPrototype.start.call(port);
243+
FunctionPrototypeCall(MessagePortPrototype.start, port);
240244
}
241245
}
242246

@@ -290,9 +294,10 @@ class WritableWorkerStdio extends Writable {
290294
this[kPort].postMessage({
291295
type: messageTypes.STDIO_PAYLOAD,
292296
stream: this[kName],
293-
chunks: chunks.map(({ chunk, encoding }) => ({ chunk, encoding }))
297+
chunks: ArrayPrototypeMap(chunks,
298+
({ chunk, encoding }) => ({ chunk, encoding })),
294299
});
295-
this[kWritableCallbacks].push(cb);
300+
ArrayPrototypePush(this[kWritableCallbacks], cb);
296301
if (this[kPort][kWaitingStreams]++ === 0)
297302
this[kPort].ref();
298303
}

lib/internal/worker/js_transferable.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
2-
const { Error } = primordials;
2+
const {
3+
Error,
4+
StringPrototypeSplit,
5+
} = primordials;
36
const {
47
messaging_deserialize_symbol,
58
messaging_transfer_symbol,
@@ -16,7 +19,7 @@ function setup() {
1619
// from .postMessage() calls. The format of `deserializeInfo` is generally
1720
// 'module:Constructor', e.g. 'internal/fs/promises:FileHandle'.
1821
setDeserializerCreateObjectFunction((deserializeInfo) => {
19-
const [ module, ctor ] = deserializeInfo.split(':');
22+
const [ module, ctor ] = StringPrototypeSplit(deserializeInfo, ':');
2023
const Ctor = require(module)[ctor];
2124
if (typeof Ctor !== 'function' ||
2225
!(Ctor.prototype instanceof JSTransferable)) {

0 commit comments

Comments
 (0)