Skip to content

Commit 3d4785c

Browse files
aduh95targos
authored andcommitted
worker: refactor to use more primordials
PR-URL: #36267 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 6799738 commit 3d4785c

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

‎lib/internal/worker.js‎

Lines changed: 11 additions & 5 deletions
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
thrownewERR_INVALID_ARG_TYPE('options.argv','Array',options.argv);
106111
}
107-
argv=options.argv.map(String);
112+
argv=ArrayPrototypeMap(options.argv,String);
108113
}
109114

110115
leturl,doEval;
@@ -133,7 +138,8 @@ class Worker extends EventEmitter {
133138
['string','URL'],
134139
filename
135140
);
136-
}elseif(path.isAbsolute(filename)||/^\.\.?[\\/]/.test(filename)){
141+
}elseif(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
consttransferList=[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(consteventof['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
constresourceLimitsArray=newFloat64Array(kTotalResourceLimitCount);
403409
functionparseResourceLimits(obj){
404410
constret=resourceLimitsArray;
405-
ret.fill(-1);
411+
TypedArrayPrototypeFill(ret,-1);
406412
if(typeofobj!=='object'||obj===null)returnret;
407413

408414
if(typeofobj.maxOldGenerationSizeMb==='number')

‎lib/internal/worker/io.js‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
'use strict';
22

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

@@ -87,7 +91,7 @@ ObjectDefineProperty(
8791
{
8892
value: function(data,type){
8993
if(type!=='message'&&type!=='messageerror'){
90-
returnoriginalCreateEvent.call(this,data,type);
94+
returnReflectApply(originalCreateEvent,this,arguments);
9195
}
9296
returnnewMessageEvent(data,this,type);
9397
},
@@ -131,7 +135,7 @@ ObjectDefineProperty(MessagePort.prototype, handleOnCloseSymbol, {
131135
MessagePort.prototype.close=function(cb){
132136
if(typeofcb==='function')
133137
this.once('close',cb);
134-
MessagePortPrototype.close.call(this);
138+
FunctionPrototypeCall(MessagePortPrototype.close,this);
135139
};
136140

137141
ObjectDefineProperty(MessagePort.prototype,inspect.custom,{
@@ -142,7 +146,7 @@ ObjectDefineProperty(MessagePort.prototype, inspect.custom, {
142146
try{
143147
// This may throw when `this` does not refer to a native object,
144148
// e.g. when accessing the prototype directly.
145-
ref=MessagePortPrototype.hasRef.call(this);
149+
ref=FunctionPrototypeCall(MessagePortPrototype.hasRef,this);
146150
}catch{returnthis;}
147151
returnObjectAssign(ObjectCreate(MessagePort.prototype),
148152
ref===undefined ? {
@@ -170,18 +174,18 @@ function setupPortReferencing(port, eventEmitter, eventName) {
170174
constorigNewListener=eventEmitter[kNewListener];
171175
eventEmitter[kNewListener]=function(size,type, ...args){
172176
if(type===eventName)newListener(size-1);
173-
returnorigNewListener.call(this,size,type, ...args);
177+
returnReflectApply(origNewListener,this,arguments);
174178
};
175179
constorigRemoveListener=eventEmitter[kRemoveListener];
176180
eventEmitter[kRemoveListener]=function(size,type, ...args){
177181
if(type===eventName)removeListener(size);
178-
returnorigRemoveListener.call(this,size,type, ...args);
182+
returnReflectApply(origRemoveListener,this,arguments);
179183
};
180184

181185
functionnewListener(size){
182186
if(size===0){
183187
port.ref();
184-
MessagePortPrototype.start.call(port);
188+
FunctionPrototypeCall(MessagePortPrototype.start,port);
185189
}
186190
}
187191

@@ -235,9 +239,10 @@ class WritableWorkerStdio extends Writable {
235239
this[kPort].postMessage({
236240
type: messageTypes.STDIO_PAYLOAD,
237241
stream: this[kName],
238-
chunks: chunks.map(({ chunk, encoding })=>({ chunk, encoding }))
242+
chunks: ArrayPrototypeMap(chunks,
243+
({ chunk, encoding })=>({ chunk, encoding })),
239244
});
240-
this[kWritableCallbacks].push(cb);
245+
ArrayPrototypePush(this[kWritableCallbacks],cb);
241246
if(this[kPort][kWaitingStreams]++===0)
242247
this[kPort].ref();
243248
}

‎lib/internal/worker/js_transferable.js‎

Lines changed: 5 additions & 2 deletions
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
constCtor=require(module)[ctor];
2124
if(typeofCtor!=='function'||
2225
!(Ctor.prototypeinstanceofJSTransferable)){

0 commit comments

Comments
 (0)