Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram: refactor to use more primordials #36286

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 16 additions & 10 deletions lib/dgram.js
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
@@ -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