From 8d69d8387e28b1e35c418837ca99385531ba88eb Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 20 Nov 2020 00:15:02 +0100 Subject: [PATCH] dgram: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36286 Reviewed-By: Rich Trott --- lib/dgram.js | 26 ++++++++++++++++---------- lib/internal/dgram.js | 5 +++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 1c1b4781617695..79501847784d1f 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -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; } diff --git a/lib/internal/dgram.js b/lib/internal/dgram.js index 8a8d9ba8c0ddd4..950a82392c8e63 100644 --- a/lib/internal/dgram.js +++ b/lib/internal/dgram.js @@ -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;