Skip to content

Commit f5b2115

Browse files
aduh95targos
authored andcommitted
dgram: refactor to use more primordials
PR-URL: #36286 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 32e10f3 commit f5b2115

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎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');
@@ -88,7 +92,7 @@ const exceptionWithHostPort = errors.exceptionWithHostPort;
8892

8993

9094
functionSocket(type,listener){
91-
EventEmitter.call(this);
95+
FunctionPrototypeCall(EventEmitter,this);
9296
letlookup;
9397
letrecvBufferSize;
9498
letsendBufferSize;
@@ -235,8 +239,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
235239
}
236240

237241
functiononListening(){
238-
removeListeners.call(this);
239-
cb.call(this);
242+
FunctionPrototypeCall(removeListeners,this);
243+
FunctionPrototypeCall(cb,this);
240244
}
241245

242246
this.on('error',removeListeners);
@@ -384,11 +388,12 @@ Socket.prototype.connect = function(port, address, callback) {
384388
this.bind({port: 0,exclusive: true},null);
385389

386390
if(state.bindState!==BIND_STATE_BOUND){
387-
enqueue(this,_connect.bind(this,port,address,callback));
391+
enqueue(this,FunctionPrototypeBind(_connect,this,
392+
port,address,callback));
388393
return;
389394
}
390395

391-
_connect.call(this,port,address,callback);
396+
ReflectApply(_connect,this,[port,address,callback]);
392397
};
393398

394399

@@ -513,13 +518,13 @@ function enqueue(self, toEnqueue) {
513518
self.once(EventEmitter.errorMonitor,onListenError);
514519
self.once('listening',onListenSuccess);
515520
}
516-
state.queue.push(toEnqueue);
521+
ArrayPrototypePush(state.queue,toEnqueue);
517522
}
518523

519524

520525
functiononListenSuccess(){
521526
this.removeListener(EventEmitter.errorMonitor,onListenError);
522-
clearQueue.call(this);
527+
FunctionPrototypeCall(clearQueue,this);
523528
}
524529

525530

@@ -640,12 +645,13 @@ Socket.prototype.send = function(buffer,
640645
this.bind({port: 0,exclusive: true},null);
641646

642647
if(list.length===0)
643-
list.push(Buffer.alloc(0));
648+
ArrayPrototypePush(list,Buffer.alloc(0));
644649

645650
// If the socket hasn't been bound yet, push the outbound packet onto the
646651
// send queue and send after binding is complete.
647652
if(state.bindState!==BIND_STATE_BOUND){
648-
enqueue(this,this.send.bind(this,list,port,address,callback));
653+
enqueue(this,FunctionPrototypeBind(this.send,this,
654+
list,port,address,callback));
649655
return;
650656
}
651657

@@ -727,7 +733,7 @@ Socket.prototype.close = function(callback) {
727733
this.on('close',callback);
728734

729735
if(queue!==undefined){
730-
queue.push(this.close.bind(this));
736+
ArrayPrototypePush(queue,FunctionPrototypeBind(this.close,this));
731737
returnthis;
732738
}
733739

‎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)