From 8e84ccb50215d406cbb9b0eadc9e5ab2967a2bf3 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 31 Dec 2018 16:43:26 +0800 Subject: [PATCH] buffer: move Buffer prototype wiring into internal/buffer.js Instead of exposing the Buffer prototype methods through an object in `internal/buffer.js` and then iterating over it to put the methods on the prototype, create a function in `internal/buffer.js` to do this. Also moves the creaton of the `FastBuffer` class into `internal/buffer.js` and expose it directly instead of writing it onto that module later. PR-URL: https://github.com/nodejs/node/pull/25292 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/buffer.js | 42 +++------------- lib/internal/buffer.js | 112 ++++++++++++++++++++++++++--------------- 2 files changed, 78 insertions(+), 76 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index e40432a0a8246e..1d98b26d0d49ba 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -36,21 +36,7 @@ const { swap64: _swap64, kMaxLength, kStringMaxLength, - zeroFill: bindingZeroFill, - - // Additional Buffer methods - asciiSlice, - base64Slice, - latin1Slice, - hexSlice, - ucs2Slice, - utf8Slice, - asciiWrite, - base64Write, - latin1Write, - hexWrite, - ucs2Write, - utf8Write + zeroFill: bindingZeroFill } = internalBinding('buffer'); const { getOwnNonIndexProperties, @@ -88,30 +74,14 @@ const { } = require('internal/errors').codes; const { validateString } = require('internal/validators'); -const internalBuffer = require('internal/buffer'); +const { + FastBuffer, + addBufferPrototypeMethods +} = require('internal/buffer'); -class FastBuffer extends Uint8Array {} FastBuffer.prototype.constructor = Buffer; -internalBuffer.FastBuffer = FastBuffer; - Buffer.prototype = FastBuffer.prototype; - -for (const [name, method] of Object.entries(internalBuffer.readWrites)) { - Buffer.prototype[name] = method; -} - -Buffer.prototype.asciiSlice = asciiSlice; -Buffer.prototype.base64Slice = base64Slice; -Buffer.prototype.latin1Slice = latin1Slice; -Buffer.prototype.hexSlice = hexSlice; -Buffer.prototype.ucs2Slice = ucs2Slice; -Buffer.prototype.utf8Slice = utf8Slice; -Buffer.prototype.asciiWrite = asciiWrite; -Buffer.prototype.base64Write = base64Write; -Buffer.prototype.latin1Write = latin1Write; -Buffer.prototype.hexWrite = hexWrite; -Buffer.prototype.ucs2Write = ucs2Write; -Buffer.prototype.utf8Write = utf8Write; +addBufferPrototypeMethods(Buffer.prototype); const constants = Object.defineProperties({}, { MAX_LENGTH: { diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index ee92b57f519827..56b73f5489ec35 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -6,6 +6,20 @@ const { ERR_OUT_OF_RANGE } = require('internal/errors').codes; const { validateNumber } = require('internal/validators'); +const { + asciiSlice, + base64Slice, + latin1Slice, + hexSlice, + ucs2Slice, + utf8Slice, + asciiWrite, + base64Write, + latin1Write, + hexWrite, + ucs2Write, + utf8Write +} = internalBinding('buffer'); // Temporary buffers to convert numbers. const float32Array = new Float32Array(1); @@ -771,45 +785,63 @@ function writeFloatBackwards(val, offset = 0) { return offset; } -// FastBuffer wil be inserted here by lib/buffer.js +class FastBuffer extends Uint8Array {} + +function addBufferPrototypeMethods(proto) { + proto.readUIntLE = readUIntLE; + proto.readUInt32LE = readUInt32LE; + proto.readUInt16LE = readUInt16LE; + proto.readUInt8 = readUInt8; + proto.readUIntBE = readUIntBE; + proto.readUInt32BE = readUInt32BE; + proto.readUInt16BE = readUInt16BE; + proto.readIntLE = readIntLE; + proto.readInt32LE = readInt32LE; + proto.readInt16LE = readInt16LE; + proto.readInt8 = readInt8; + proto.readIntBE = readIntBE; + proto.readInt32BE = readInt32BE; + proto.readInt16BE = readInt16BE; + + proto.writeUIntLE = writeUIntLE; + proto.writeUInt32LE = writeUInt32LE; + proto.writeUInt16LE = writeUInt16LE; + proto.writeUInt8 = writeUInt8; + proto.writeUIntBE = writeUIntBE; + proto.writeUInt32BE = writeUInt32BE; + proto.writeUInt16BE = writeUInt16BE; + proto.writeIntLE = writeIntLE; + proto.writeInt32LE = writeInt32LE; + proto.writeInt16LE = writeInt16LE; + proto.writeInt8 = writeInt8; + proto.writeIntBE = writeIntBE; + proto.writeInt32BE = writeInt32BE; + proto.writeInt16BE = writeInt16BE; + + proto.readFloatLE = bigEndian ? readFloatBackwards : readFloatForwards; + proto.readFloatBE = bigEndian ? readFloatForwards : readFloatBackwards; + proto.readDoubleLE = bigEndian ? readDoubleBackwards : readDoubleForwards; + proto.readDoubleBE = bigEndian ? readDoubleForwards : readDoubleBackwards; + proto.writeFloatLE = bigEndian ? writeFloatBackwards : writeFloatForwards; + proto.writeFloatBE = bigEndian ? writeFloatForwards : writeFloatBackwards; + proto.writeDoubleLE = bigEndian ? writeDoubleBackwards : writeDoubleForwards; + proto.writeDoubleBE = bigEndian ? writeDoubleForwards : writeDoubleBackwards; + + proto.asciiSlice = asciiSlice; + proto.base64Slice = base64Slice; + proto.latin1Slice = latin1Slice; + proto.hexSlice = hexSlice; + proto.ucs2Slice = ucs2Slice; + proto.utf8Slice = utf8Slice; + proto.asciiWrite = asciiWrite; + proto.base64Write = base64Write; + proto.latin1Write = latin1Write; + proto.hexWrite = hexWrite; + proto.ucs2Write = ucs2Write; + proto.utf8Write = utf8Write; +} + module.exports = { - // Container to export all read write functions. - readWrites: { - readUIntLE, - readUInt32LE, - readUInt16LE, - readUInt8, - readUIntBE, - readUInt32BE, - readUInt16BE, - readIntLE, - readInt32LE, - readInt16LE, - readInt8, - readIntBE, - readInt32BE, - readInt16BE, - writeUIntLE, - writeUInt32LE, - writeUInt16LE, - writeUInt8, - writeUIntBE, - writeUInt32BE, - writeUInt16BE, - writeIntLE, - writeInt32LE, - writeInt16LE, - writeInt8, - writeIntBE, - writeInt32BE, - writeInt16BE, - readFloatLE: bigEndian ? readFloatBackwards : readFloatForwards, - readFloatBE: bigEndian ? readFloatForwards : readFloatBackwards, - readDoubleLE: bigEndian ? readDoubleBackwards : readDoubleForwards, - readDoubleBE: bigEndian ? readDoubleForwards : readDoubleBackwards, - writeFloatLE: bigEndian ? writeFloatBackwards : writeFloatForwards, - writeFloatBE: bigEndian ? writeFloatForwards : writeFloatBackwards, - writeDoubleLE: bigEndian ? writeDoubleBackwards : writeDoubleForwards, - writeDoubleBE: bigEndian ? writeDoubleForwards : writeDoubleBackwards - } + FastBuffer, + addBufferPrototypeMethods };