Skip to content

Commit

Permalink
dgram: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36286
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Dec 7, 2020
1 parent 22028aa commit 8d69d83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
26 changes: 16 additions & 10 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
const {
Array,
ArrayIsArray,
ArrayPrototypePush,
FunctionPrototypeBind,
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
} = primordials;

const errors = require('internal/errors');
Expand Down Expand Up @@ -87,7 +91,7 @@ const exceptionWithHostPort = errors.exceptionWithHostPort;


function Socket(type, listener) {
EventEmitter.call(this);
FunctionPrototypeCall(EventEmitter, this);
let lookup;
let recvBufferSize;
let sendBufferSize;
Expand Down Expand Up @@ -220,8 +224,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
}

function onListening() {
removeListeners.call(this);
cb.call(this);
FunctionPrototypeCall(removeListeners, this);
FunctionPrototypeCall(cb, this);
}

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

if (state.bindState !== BIND_STATE_BOUND) {
enqueue(this, _connect.bind(this, port, address, callback));
enqueue(this, FunctionPrototypeBind(_connect, this,
port, address, callback));
return;
}

_connect.call(this, port, address, callback);
ReflectApply(_connect, this, [port, address, callback]);
};


Expand Down Expand Up @@ -498,13 +503,13 @@ function enqueue(self, toEnqueue) {
self.once(EventEmitter.errorMonitor, onListenError);
self.once('listening', onListenSuccess);
}
state.queue.push(toEnqueue);
ArrayPrototypePush(state.queue, toEnqueue);
}


function onListenSuccess() {
this.removeListener(EventEmitter.errorMonitor, onListenError);
clearQueue.call(this);
FunctionPrototypeCall(clearQueue, this);
}


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

if (list.length === 0)
list.push(Buffer.alloc(0));
ArrayPrototypePush(list, Buffer.alloc(0));

// If the socket hasn't been bound yet, push the outbound packet onto the
// send queue and send after binding is complete.
if (state.bindState !== BIND_STATE_BOUND) {
enqueue(this, this.send.bind(this, list, port, address, callback));
enqueue(this, FunctionPrototypeBind(this.send, this,
list, port, address, callback));
return;
}

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

if (queue !== undefined) {
queue.push(this.close.bind(this));
ArrayPrototypePush(queue, FunctionPrototypeBind(this.close, this));
return this;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/internal/dgram.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
FunctionPrototypeBind,
Symbol,
} = primordials;

Expand Down Expand Up @@ -37,14 +38,14 @@ function newHandle(type, lookup) {
if (type === 'udp4') {
const handle = new UDP();

handle.lookup = lookup4.bind(handle, lookup);
handle.lookup = FunctionPrototypeBind(lookup4, handle, lookup);
return handle;
}

if (type === 'udp6') {
const handle = new UDP();

handle.lookup = lookup6.bind(handle, lookup);
handle.lookup = FunctionPrototypeBind(lookup6, handle, lookup);
handle.bind = handle.bind6;
handle.connect = handle.connect6;
handle.send = handle.send6;
Expand Down

0 comments on commit 8d69d83

Please sign in to comment.