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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2018-12-31 16:43:26 +08:00
parent 97f59b9567
commit 842a35fbac
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
2 changed files with 78 additions and 76 deletions

View File

@ -36,21 +36,7 @@ const {
swap64: _swap64, swap64: _swap64,
kMaxLength, kMaxLength,
kStringMaxLength, kStringMaxLength,
zeroFill: bindingZeroFill, zeroFill: bindingZeroFill
// Additional Buffer methods
asciiSlice,
base64Slice,
latin1Slice,
hexSlice,
ucs2Slice,
utf8Slice,
asciiWrite,
base64Write,
latin1Write,
hexWrite,
ucs2Write,
utf8Write
} = internalBinding('buffer'); } = internalBinding('buffer');
const { const {
getOwnNonIndexProperties, getOwnNonIndexProperties,
@ -87,30 +73,14 @@ const {
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { validateString } = require('internal/validators'); const { validateString } = require('internal/validators');
const internalBuffer = require('internal/buffer'); const {
FastBuffer,
addBufferPrototypeMethods
} = require('internal/buffer');
class FastBuffer extends Uint8Array {}
FastBuffer.prototype.constructor = Buffer; FastBuffer.prototype.constructor = Buffer;
internalBuffer.FastBuffer = FastBuffer;
Buffer.prototype = FastBuffer.prototype; Buffer.prototype = FastBuffer.prototype;
addBufferPrototypeMethods(Buffer.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;
const constants = Object.defineProperties({}, { const constants = Object.defineProperties({}, {
MAX_LENGTH: { MAX_LENGTH: {

View File

@ -6,6 +6,20 @@ const {
ERR_OUT_OF_RANGE ERR_OUT_OF_RANGE
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { validateNumber } = require('internal/validators'); 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. // Temporary buffers to convert numbers.
const float32Array = new Float32Array(1); const float32Array = new Float32Array(1);
@ -771,45 +785,63 @@ function writeFloatBackwards(val, offset = 0) {
return offset; return offset;
} }
// FastBuffer wil be inserted here by lib/buffer.js class FastBuffer extends Uint8Array {}
module.exports = {
// Container to export all read write functions. function addBufferPrototypeMethods(proto) {
readWrites: { proto.readUIntLE = readUIntLE;
readUIntLE, proto.readUInt32LE = readUInt32LE;
readUInt32LE, proto.readUInt16LE = readUInt16LE;
readUInt16LE, proto.readUInt8 = readUInt8;
readUInt8, proto.readUIntBE = readUIntBE;
readUIntBE, proto.readUInt32BE = readUInt32BE;
readUInt32BE, proto.readUInt16BE = readUInt16BE;
readUInt16BE, proto.readIntLE = readIntLE;
readIntLE, proto.readInt32LE = readInt32LE;
readInt32LE, proto.readInt16LE = readInt16LE;
readInt16LE, proto.readInt8 = readInt8;
readInt8, proto.readIntBE = readIntBE;
readIntBE, proto.readInt32BE = readInt32BE;
readInt32BE, proto.readInt16BE = readInt16BE;
readInt16BE,
writeUIntLE, proto.writeUIntLE = writeUIntLE;
writeUInt32LE, proto.writeUInt32LE = writeUInt32LE;
writeUInt16LE, proto.writeUInt16LE = writeUInt16LE;
writeUInt8, proto.writeUInt8 = writeUInt8;
writeUIntBE, proto.writeUIntBE = writeUIntBE;
writeUInt32BE, proto.writeUInt32BE = writeUInt32BE;
writeUInt16BE, proto.writeUInt16BE = writeUInt16BE;
writeIntLE, proto.writeIntLE = writeIntLE;
writeInt32LE, proto.writeInt32LE = writeInt32LE;
writeInt16LE, proto.writeInt16LE = writeInt16LE;
writeInt8, proto.writeInt8 = writeInt8;
writeIntBE, proto.writeIntBE = writeIntBE;
writeInt32BE, proto.writeInt32BE = writeInt32BE;
writeInt16BE, proto.writeInt16BE = writeInt16BE;
readFloatLE: bigEndian ? readFloatBackwards : readFloatForwards,
readFloatBE: bigEndian ? readFloatForwards : readFloatBackwards, proto.readFloatLE = bigEndian ? readFloatBackwards : readFloatForwards;
readDoubleLE: bigEndian ? readDoubleBackwards : readDoubleForwards, proto.readFloatBE = bigEndian ? readFloatForwards : readFloatBackwards;
readDoubleBE: bigEndian ? readDoubleForwards : readDoubleBackwards, proto.readDoubleLE = bigEndian ? readDoubleBackwards : readDoubleForwards;
writeFloatLE: bigEndian ? writeFloatBackwards : writeFloatForwards, proto.readDoubleBE = bigEndian ? readDoubleForwards : readDoubleBackwards;
writeFloatBE: bigEndian ? writeFloatForwards : writeFloatBackwards, proto.writeFloatLE = bigEndian ? writeFloatBackwards : writeFloatForwards;
writeDoubleLE: bigEndian ? writeDoubleBackwards : writeDoubleForwards, proto.writeFloatBE = bigEndian ? writeFloatForwards : writeFloatBackwards;
writeDoubleBE: bigEndian ? writeDoubleForwards : writeDoubleBackwards 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 = {
FastBuffer,
addBufferPrototypeMethods
}; };