Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: migrate to direct invocations
  • Loading branch information
nbbeeken committed Jan 14, 2026
commit 066ccc6f80e3d2be8b9a525bb3129465ec0d1d3e
77 changes: 36 additions & 41 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const {
ArrayBufferIsView,
ArrayIsArray,
ArrayPrototypeForEach,
FunctionPrototypeCall,
MathFloor,
MathMin,
MathTrunc,
Expand Down Expand Up @@ -73,6 +72,17 @@ const {
kStringMaxLength,
atob: _atob,
btoa: _btoa,
asciiSlice,
base64Slice,
base64urlSlice,
latin1Slice,
hexSlice,
ucs2Slice,
utf8Slice,
base64Write,
base64urlWrite,
hexWrite,
ucs2Write,
} = internalBinding('buffer');
const {
constants: {
Expand Down Expand Up @@ -128,29 +138,15 @@ const {
markAsUntransferable,
addBufferPrototypeMethods,
createUnsafeBuffer,
asciiWrite,
latin1Write,
utf8Write,
} = require('internal/buffer');

FastBuffer.prototype.constructor = Buffer;
Buffer.prototype = FastBuffer.prototype;
addBufferPrototypeMethods(Buffer.prototype);

const {
asciiWrite,
latin1Write,
utf8Write,
asciiSlice,
base64Slice,
base64urlSlice,
latin1Slice,
hexSlice,
ucs2Slice,
utf8Slice,
base64Write,
base64urlWrite,
hexWrite,
ucs2Write,
} = Buffer.prototype;

const constants = ObjectDefineProperties({}, {
MAX_LENGTH: {
__proto__: null,
Expand Down Expand Up @@ -652,44 +648,44 @@ const encodingOps = {
encoding: 'utf8',
encodingVal: encodingsMap.utf8,
byteLength: byteLengthUtf8,
write: (buf, string, offset, len) => FunctionPrototypeCall(utf8Write, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(utf8Slice, buf, start, end),
write: utf8Write,
slice: utf8Slice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfString(buf, val, byteOffset, encodingsMap.utf8, dir),
},
ucs2: {
encoding: 'ucs2',
encodingVal: encodingsMap.utf16le,
byteLength: (string) => string.length * 2,
write: (buf, string, offset, len) => FunctionPrototypeCall(ucs2Write, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(ucs2Slice, buf, start, end),
write: ucs2Write,
slice: ucs2Slice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir),
},
utf16le: {
encoding: 'utf16le',
encodingVal: encodingsMap.utf16le,
byteLength: (string) => string.length * 2,
write: (buf, string, offset, len) => FunctionPrototypeCall(ucs2Write, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(ucs2Slice, buf, start, end),
write: ucs2Write,
slice: ucs2Slice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir),
},
latin1: {
encoding: 'latin1',
encodingVal: encodingsMap.latin1,
byteLength: (string) => string.length,
write: (buf, string, offset, len) => FunctionPrototypeCall(latin1Write, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(latin1Slice, buf, start, end),
write: latin1Write,
slice: latin1Slice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfString(buf, val, byteOffset, encodingsMap.latin1, dir),
},
ascii: {
encoding: 'ascii',
encodingVal: encodingsMap.ascii,
byteLength: (string) => string.length,
write: (buf, string, offset, len) => FunctionPrototypeCall(asciiWrite, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(asciiSlice, buf, start, end),
write: asciiWrite,
slice: asciiSlice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfBuffer(buf,
fromStringFast(val, encodingOps.ascii),
Expand All @@ -701,8 +697,8 @@ const encodingOps = {
encoding: 'base64',
encodingVal: encodingsMap.base64,
byteLength: (string) => base64ByteLength(string, string.length),
write: (buf, string, offset, len) => FunctionPrototypeCall(base64Write, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(base64Slice, buf, start, end),
write: base64Write,
slice: base64Slice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfBuffer(buf,
fromStringFast(val, encodingOps.base64),
Expand All @@ -714,9 +710,8 @@ const encodingOps = {
encoding: 'base64url',
encodingVal: encodingsMap.base64url,
byteLength: (string) => base64ByteLength(string, string.length),
write: (buf, string, offset, len) =>
FunctionPrototypeCall(base64urlWrite, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(base64urlSlice, buf, start, end),
write: base64urlWrite,
slice: base64urlSlice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfBuffer(buf,
fromStringFast(val, encodingOps.base64url),
Expand All @@ -728,8 +723,8 @@ const encodingOps = {
encoding: 'hex',
encodingVal: encodingsMap.hex,
byteLength: (string) => string.length >>> 1,
write: (buf, string, offset, len) => FunctionPrototypeCall(hexWrite, buf, string, offset, len),
slice: (buf, start, end) => FunctionPrototypeCall(hexSlice, buf, start, end),
write: hexWrite,
slice: hexSlice,
indexOf: (buf, val, byteOffset, dir) =>
indexOfBuffer(buf,
fromStringFast(val, encodingOps.hex),
Expand Down Expand Up @@ -854,7 +849,7 @@ Buffer.prototype.copy =
// to their upper/lower bounds if the value passed is out of range.
Buffer.prototype.toString = function toString(encoding, start, end) {
if (arguments.length === 0) {
return FunctionPrototypeCall(utf8Slice, this, 0, this.length);
return utf8Slice(this, 0, this.length);
}

const len = this.length;
Expand All @@ -875,7 +870,7 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
return '';

if (encoding === undefined)
return FunctionPrototypeCall(utf8Slice, this, start, end);
return utf8Slice(this, start, end);

const ops = getEncodingOps(encoding);
if (ops === undefined)
Expand Down Expand Up @@ -906,7 +901,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
const actualMax = MathMin(max, this.length);
const remaining = this.length - max;
let str = StringPrototypeTrim(RegExpPrototypeSymbolReplace(
/(.{2})/g, FunctionPrototypeCall(hexSlice, this, 0, actualMax), '$1 '));
/(.{2})/g, hexSlice(this, 0, actualMax), '$1 '));
if (remaining > 0)
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
// Inspect special properties as well, if possible.
Expand Down Expand Up @@ -1130,7 +1125,7 @@ function _fill(buf, value, offset, end, encoding) {
Buffer.prototype.write = function write(string, offset, length, encoding) {
// Buffer#write(string);
if (offset === undefined) {
return FunctionPrototypeCall(utf8Write, this, string, 0, this.length);
return utf8Write(this, string, 0, this.length);
}
// Buffer#write(string, encoding)
if (length === undefined && typeof offset === 'string') {
Expand All @@ -1157,9 +1152,9 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
}

if (!encoding || encoding === 'utf8')
return FunctionPrototypeCall(utf8Write, this, string, offset, length);
return utf8Write(this, string, offset, length);
if (encoding === 'ascii')
return FunctionPrototypeCall(asciiWrite, this, string, offset, length);
return asciiWrite(this, string, offset, length);

const ops = getEncodingOps(encoding);
if (ops === undefined)
Expand Down
Loading