Skip to content

Commit 8d69d83

Browse files
aduh95danielleadams
authored andcommitted
dgram: refactor to use more primordials
PR-URL: #36286 Reviewed-By: Rich Trott <[email protected]>
1 parent 22028aa commit 8d69d83

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

‎lib/dgram.js‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
const{
2525
Array,
2626
ArrayIsArray,
27+
ArrayPrototypePush,
28+
FunctionPrototypeBind,
29+
FunctionPrototypeCall,
2730
ObjectDefineProperty,
2831
ObjectSetPrototypeOf,
32+
ReflectApply,
2933
}=primordials;
3034

3135
consterrors=require('internal/errors');
@@ -87,7 +91,7 @@ const exceptionWithHostPort = errors.exceptionWithHostPort;
8791

8892

8993
functionSocket(type,listener){
90-
EventEmitter.call(this);
94+
FunctionPrototypeCall(EventEmitter,this);
9195
letlookup;
9296
letrecvBufferSize;
9397
letsendBufferSize;
@@ -220,8 +224,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */){
220224
}
221225

222226
functiononListening(){
223-
removeListeners.call(this);
224-
cb.call(this);
227+
FunctionPrototypeCall(removeListeners,this);
228+
FunctionPrototypeCall(cb,this);
225229
}
226230

227231
this.on('error',removeListeners);
@@ -369,11 +373,12 @@ Socket.prototype.connect = function(port, address, callback){
369373
this.bind({port: 0,exclusive: true},null);
370374

371375
if(state.bindState!==BIND_STATE_BOUND){
372-
enqueue(this,_connect.bind(this,port,address,callback));
376+
enqueue(this,FunctionPrototypeBind(_connect,this,
377+
port,address,callback));
373378
return;
374379
}
375380

376-
_connect.call(this,port,address,callback);
381+
ReflectApply(_connect,this,[port,address,callback]);
377382
};
378383

379384

@@ -498,13 +503,13 @@ function enqueue(self, toEnqueue){
498503
self.once(EventEmitter.errorMonitor,onListenError);
499504
self.once('listening',onListenSuccess);
500505
}
501-
state.queue.push(toEnqueue);
506+
ArrayPrototypePush(state.queue,toEnqueue);
502507
}
503508

504509

505510
functiononListenSuccess(){
506511
this.removeListener(EventEmitter.errorMonitor,onListenError);
507-
clearQueue.call(this);
512+
FunctionPrototypeCall(clearQueue,this);
508513
}
509514

510515

@@ -625,12 +630,13 @@ Socket.prototype.send = function(buffer,
625630
this.bind({port: 0,exclusive: true},null);
626631

627632
if(list.length===0)
628-
list.push(Buffer.alloc(0));
633+
ArrayPrototypePush(list,Buffer.alloc(0));
629634

630635
// If the socket hasn't been bound yet, push the outbound packet onto the
631636
// send queue and send after binding is complete.
632637
if(state.bindState!==BIND_STATE_BOUND){
633-
enqueue(this,this.send.bind(this,list,port,address,callback));
638+
enqueue(this,FunctionPrototypeBind(this.send,this,
639+
list,port,address,callback));
634640
return;
635641
}
636642

@@ -712,7 +718,7 @@ Socket.prototype.close = function(callback){
712718
this.on('close',callback);
713719

714720
if(queue!==undefined){
715-
queue.push(this.close.bind(this));
721+
ArrayPrototypePush(queue,FunctionPrototypeBind(this.close,this));
716722
returnthis;
717723
}
718724

‎lib/internal/dgram.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const{
4+
FunctionPrototypeBind,
45
Symbol,
56
}=primordials;
67

@@ -37,14 +38,14 @@ function newHandle(type, lookup){
3738
if(type==='udp4'){
3839
consthandle=newUDP();
3940

40-
handle.lookup=lookup4.bind(handle,lookup);
41+
handle.lookup=FunctionPrototypeBind(lookup4,handle,lookup);
4142
returnhandle;
4243
}
4344

4445
if(type==='udp6'){
4546
consthandle=newUDP();
4647

47-
handle.lookup=lookup6.bind(handle,lookup);
48+
handle.lookup=FunctionPrototypeBind(lookup6,handle,lookup);
4849
handle.bind=handle.bind6;
4950
handle.connect=handle.connect6;
5051
handle.send=handle.send6;

0 commit comments

Comments
(0)