Skip to content

Commit eacc151

Browse files
joyeecheungBridgeAR
authored andcommitted
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: #25292 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 255f017 commit eacc151

File tree

2 files changed

+78
-76
lines changed

2 files changed

+78
-76
lines changed

lib/buffer.js

+6-36
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,7 @@ const {
3636
swap64: _swap64,
3737
kMaxLength,
3838
kStringMaxLength,
39-
zeroFill: bindingZeroFill,
40-
41-
// Additional Buffer methods
42-
asciiSlice,
43-
base64Slice,
44-
latin1Slice,
45-
hexSlice,
46-
ucs2Slice,
47-
utf8Slice,
48-
asciiWrite,
49-
base64Write,
50-
latin1Write,
51-
hexWrite,
52-
ucs2Write,
53-
utf8Write
39+
zeroFill: bindingZeroFill
5440
} = internalBinding('buffer');
5541
const {
5642
getOwnNonIndexProperties,
@@ -88,30 +74,14 @@ const {
8874
} = require('internal/errors').codes;
8975
const { validateString } = require('internal/validators');
9076

91-
const internalBuffer = require('internal/buffer');
77+
const {
78+
FastBuffer,
79+
addBufferPrototypeMethods
80+
} = require('internal/buffer');
9281

93-
class FastBuffer extends Uint8Array {}
9482
FastBuffer.prototype.constructor = Buffer;
95-
internalBuffer.FastBuffer = FastBuffer;
96-
9783
Buffer.prototype = FastBuffer.prototype;
98-
99-
for (const [name, method] of Object.entries(internalBuffer.readWrites)) {
100-
Buffer.prototype[name] = method;
101-
}
102-
103-
Buffer.prototype.asciiSlice = asciiSlice;
104-
Buffer.prototype.base64Slice = base64Slice;
105-
Buffer.prototype.latin1Slice = latin1Slice;
106-
Buffer.prototype.hexSlice = hexSlice;
107-
Buffer.prototype.ucs2Slice = ucs2Slice;
108-
Buffer.prototype.utf8Slice = utf8Slice;
109-
Buffer.prototype.asciiWrite = asciiWrite;
110-
Buffer.prototype.base64Write = base64Write;
111-
Buffer.prototype.latin1Write = latin1Write;
112-
Buffer.prototype.hexWrite = hexWrite;
113-
Buffer.prototype.ucs2Write = ucs2Write;
114-
Buffer.prototype.utf8Write = utf8Write;
84+
addBufferPrototypeMethods(Buffer.prototype);
11585

11686
const constants = Object.defineProperties({}, {
11787
MAX_LENGTH: {

lib/internal/buffer.js

+72-40
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ const {
66
ERR_OUT_OF_RANGE
77
} = require('internal/errors').codes;
88
const { validateNumber } = require('internal/validators');
9+
const {
10+
asciiSlice,
11+
base64Slice,
12+
latin1Slice,
13+
hexSlice,
14+
ucs2Slice,
15+
utf8Slice,
16+
asciiWrite,
17+
base64Write,
18+
latin1Write,
19+
hexWrite,
20+
ucs2Write,
21+
utf8Write
22+
} = internalBinding('buffer');
923

1024
// Temporary buffers to convert numbers.
1125
const float32Array = new Float32Array(1);
@@ -771,45 +785,63 @@ function writeFloatBackwards(val, offset = 0) {
771785
return offset;
772786
}
773787

774-
// FastBuffer wil be inserted here by lib/buffer.js
788+
class FastBuffer extends Uint8Array {}
789+
790+
function addBufferPrototypeMethods(proto) {
791+
proto.readUIntLE = readUIntLE;
792+
proto.readUInt32LE = readUInt32LE;
793+
proto.readUInt16LE = readUInt16LE;
794+
proto.readUInt8 = readUInt8;
795+
proto.readUIntBE = readUIntBE;
796+
proto.readUInt32BE = readUInt32BE;
797+
proto.readUInt16BE = readUInt16BE;
798+
proto.readIntLE = readIntLE;
799+
proto.readInt32LE = readInt32LE;
800+
proto.readInt16LE = readInt16LE;
801+
proto.readInt8 = readInt8;
802+
proto.readIntBE = readIntBE;
803+
proto.readInt32BE = readInt32BE;
804+
proto.readInt16BE = readInt16BE;
805+
806+
proto.writeUIntLE = writeUIntLE;
807+
proto.writeUInt32LE = writeUInt32LE;
808+
proto.writeUInt16LE = writeUInt16LE;
809+
proto.writeUInt8 = writeUInt8;
810+
proto.writeUIntBE = writeUIntBE;
811+
proto.writeUInt32BE = writeUInt32BE;
812+
proto.writeUInt16BE = writeUInt16BE;
813+
proto.writeIntLE = writeIntLE;
814+
proto.writeInt32LE = writeInt32LE;
815+
proto.writeInt16LE = writeInt16LE;
816+
proto.writeInt8 = writeInt8;
817+
proto.writeIntBE = writeIntBE;
818+
proto.writeInt32BE = writeInt32BE;
819+
proto.writeInt16BE = writeInt16BE;
820+
821+
proto.readFloatLE = bigEndian ? readFloatBackwards : readFloatForwards;
822+
proto.readFloatBE = bigEndian ? readFloatForwards : readFloatBackwards;
823+
proto.readDoubleLE = bigEndian ? readDoubleBackwards : readDoubleForwards;
824+
proto.readDoubleBE = bigEndian ? readDoubleForwards : readDoubleBackwards;
825+
proto.writeFloatLE = bigEndian ? writeFloatBackwards : writeFloatForwards;
826+
proto.writeFloatBE = bigEndian ? writeFloatForwards : writeFloatBackwards;
827+
proto.writeDoubleLE = bigEndian ? writeDoubleBackwards : writeDoubleForwards;
828+
proto.writeDoubleBE = bigEndian ? writeDoubleForwards : writeDoubleBackwards;
829+
830+
proto.asciiSlice = asciiSlice;
831+
proto.base64Slice = base64Slice;
832+
proto.latin1Slice = latin1Slice;
833+
proto.hexSlice = hexSlice;
834+
proto.ucs2Slice = ucs2Slice;
835+
proto.utf8Slice = utf8Slice;
836+
proto.asciiWrite = asciiWrite;
837+
proto.base64Write = base64Write;
838+
proto.latin1Write = latin1Write;
839+
proto.hexWrite = hexWrite;
840+
proto.ucs2Write = ucs2Write;
841+
proto.utf8Write = utf8Write;
842+
}
843+
775844
module.exports = {
776-
// Container to export all read write functions.
777-
readWrites: {
778-
readUIntLE,
779-
readUInt32LE,
780-
readUInt16LE,
781-
readUInt8,
782-
readUIntBE,
783-
readUInt32BE,
784-
readUInt16BE,
785-
readIntLE,
786-
readInt32LE,
787-
readInt16LE,
788-
readInt8,
789-
readIntBE,
790-
readInt32BE,
791-
readInt16BE,
792-
writeUIntLE,
793-
writeUInt32LE,
794-
writeUInt16LE,
795-
writeUInt8,
796-
writeUIntBE,
797-
writeUInt32BE,
798-
writeUInt16BE,
799-
writeIntLE,
800-
writeInt32LE,
801-
writeInt16LE,
802-
writeInt8,
803-
writeIntBE,
804-
writeInt32BE,
805-
writeInt16BE,
806-
readFloatLE: bigEndian ? readFloatBackwards : readFloatForwards,
807-
readFloatBE: bigEndian ? readFloatForwards : readFloatBackwards,
808-
readDoubleLE: bigEndian ? readDoubleBackwards : readDoubleForwards,
809-
readDoubleBE: bigEndian ? readDoubleForwards : readDoubleBackwards,
810-
writeFloatLE: bigEndian ? writeFloatBackwards : writeFloatForwards,
811-
writeFloatBE: bigEndian ? writeFloatForwards : writeFloatBackwards,
812-
writeDoubleLE: bigEndian ? writeDoubleBackwards : writeDoubleForwards,
813-
writeDoubleBE: bigEndian ? writeDoubleForwards : writeDoubleBackwards
814-
}
845+
FastBuffer,
846+
addBufferPrototypeMethods
815847
};

0 commit comments

Comments
 (0)