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 committed 2019-01-10 16:59:27 +08:00
1 parent 97f59b9567
commit 842a35fbac
2 files changed
+78 -76

No files matched your search

+6 -36
View File
@@ -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,
@@ -87,30 +73,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: {