dgram: refactor to use more primordials

PR-URL: https://github.com/nodejs/node/pull/36286
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Antoine du Hamel
2020-11-20 00:15:02 +01:00
committed by Node.js GitHub Bot
parent d1e4d34afa
commit e074bee7da
2 changed files with 19 additions and 12 deletions

View File

@@ -24,8 +24,12 @@
const {
Array,
ArrayIsArray,
ArrayPrototypePush,
FunctionPrototypeBind,
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
} = primordials;
const errors = require('internal/errors');
@@ -87,7 +91,7 @@ const exceptionWithHostPort = errors.exceptionWithHostPort;
function Socket(type, listener) {
EventEmitter.call(this);
FunctionPrototypeCall(EventEmitter, this);
let lookup;
let recvBufferSize;
let sendBufferSize;
@@ -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);
@@ -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]);
};
@@ -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);
}
@@ -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;
}
@@ -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;
}

View File

@@ -1,6 +1,7 @@
'use strict';
const {
FunctionPrototypeBind,
Symbol,
} = primordials;
@@ -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;