Skip to content

Commit

Permalink
buffer: move Buffer prototype wiring into internal/buffer.js
Browse files Browse the repository at this point in the history
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: #25292
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
joyeecheung authored and BridgeAR committed Jan 16, 2019
1 parent 255f017 commit eacc151
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 76 deletions.
42 changes: 6 additions & 36 deletions lib/buffer.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down
112 changes: 72 additions & 40 deletions lib/internal/buffer.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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
};

0 comments on commit eacc151

Please sign in to comment.