Skip to content

Commit 5acf0a5

Browse files
aduh95targos
authored andcommitted
buffer: refactor to use more primordials
PR-URL: #36166 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent ee44447 commit 5acf0a5

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

lib/buffer.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ const {
3535
ObjectDefineProperties,
3636
ObjectDefineProperty,
3737
ObjectGetOwnPropertyDescriptor,
38-
ObjectGetPrototypeOf,
3938
ObjectSetPrototypeOf,
39+
StringPrototypeCharCodeAt,
40+
StringPrototypeReplace,
41+
StringPrototypeSlice,
42+
StringPrototypeToLowerCase,
43+
StringPrototypeTrim,
4044
SymbolSpecies,
4145
SymbolToPrimitive,
46+
TypedArrayPrototype,
47+
TypedArrayPrototypeFill,
48+
TypedArrayPrototypeSet,
4249
Uint8Array,
4350
Uint8ArrayPrototype,
51+
uncurryThis,
4452
} = primordials;
4553

4654
const {
@@ -109,12 +117,9 @@ const {
109117
addBufferPrototypeMethods
110118
} = require('internal/buffer');
111119

112-
const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8ArrayPrototype);
113-
114-
const TypedArrayProto_byteLength =
115-
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
116-
'byteLength').get;
117-
const TypedArrayFill = TypedArrayPrototype.fill;
120+
const TypedArrayProto_byteLength = uncurryThis(
121+
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
122+
'byteLength').get);
118123

119124
FastBuffer.prototype.constructor = Buffer;
120125
Buffer.prototype = FastBuffer.prototype;
@@ -262,7 +267,7 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
262267
if (sourceStart !== 0 || sourceEnd < source.length)
263268
source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb);
264269

265-
target.set(source, targetStart);
270+
TypedArrayPrototypeSet(target, source, targetStart);
266271

267272
return nb;
268273
}
@@ -496,7 +501,7 @@ function fromArrayLike(obj) {
496501
if (obj.length > (poolSize - poolOffset))
497502
createPool();
498503
const b = new FastBuffer(allocPool, poolOffset, obj.length);
499-
b.set(obj, 0);
504+
TypedArrayPrototypeSet(b, obj, 0);
500505
poolOffset += obj.length;
501506
alignPool();
502507
return b;
@@ -582,17 +587,17 @@ Buffer.concat = function concat(list, length) {
582587
// Zero-fill the remaining bytes if the specified `length` was more than
583588
// the actual total length, i.e. if we have some remaining allocated bytes
584589
// there were not initialized.
585-
TypedArrayFill.call(buffer, 0, pos, length);
590+
TypedArrayPrototypeFill(buffer, 0, pos, length);
586591
}
587592

588593
return buffer;
589594
};
590595

591596
function base64ByteLength(str, bytes) {
592597
// Handle padding
593-
if (str.charCodeAt(bytes - 1) === 0x3D)
598+
if (StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
594599
bytes--;
595-
if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3D)
600+
if (bytes > 1 && StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
596601
bytes--;
597602

598603
// Base64 ratio: 3/4
@@ -682,38 +687,40 @@ function getEncodingOps(encoding) {
682687
case 4:
683688
if (encoding === 'utf8') return encodingOps.utf8;
684689
if (encoding === 'ucs2') return encodingOps.ucs2;
685-
encoding = encoding.toLowerCase();
690+
encoding = StringPrototypeToLowerCase(encoding);
686691
if (encoding === 'utf8') return encodingOps.utf8;
687692
if (encoding === 'ucs2') return encodingOps.ucs2;
688693
break;
689694
case 5:
690695
if (encoding === 'utf-8') return encodingOps.utf8;
691696
if (encoding === 'ascii') return encodingOps.ascii;
692697
if (encoding === 'ucs-2') return encodingOps.ucs2;
693-
encoding = encoding.toLowerCase();
698+
encoding = StringPrototypeToLowerCase(encoding);
694699
if (encoding === 'utf-8') return encodingOps.utf8;
695700
if (encoding === 'ascii') return encodingOps.ascii;
696701
if (encoding === 'ucs-2') return encodingOps.ucs2;
697702
break;
698703
case 7:
699-
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
704+
if (encoding === 'utf16le' ||
705+
StringPrototypeToLowerCase(encoding) === 'utf16le')
700706
return encodingOps.utf16le;
701707
break;
702708
case 8:
703-
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
709+
if (encoding === 'utf-16le' ||
710+
StringPrototypeToLowerCase(encoding) === 'utf-16le')
704711
return encodingOps.utf16le;
705712
break;
706713
case 6:
707714
if (encoding === 'latin1' || encoding === 'binary')
708715
return encodingOps.latin1;
709716
if (encoding === 'base64') return encodingOps.base64;
710-
encoding = encoding.toLowerCase();
717+
encoding = StringPrototypeToLowerCase(encoding);
711718
if (encoding === 'latin1' || encoding === 'binary')
712719
return encodingOps.latin1;
713720
if (encoding === 'base64') return encodingOps.base64;
714721
break;
715722
case 3:
716-
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
723+
if (encoding === 'hex' || StringPrototypeToLowerCase(encoding) === 'hex')
717724
return encodingOps.hex;
718725
break;
719726
}
@@ -826,7 +833,8 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
826833
const max = INSPECT_MAX_BYTES;
827834
const actualMax = MathMin(max, this.length);
828835
const remaining = this.length - max;
829-
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
836+
let str = StringPrototypeTrim(StringPrototypeReplace(
837+
this.hexSlice(0, actualMax), /(.{2})/g, '$1 '));
830838
if (remaining > 0)
831839
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
832840
// Inspect special properties as well, if possible.
@@ -843,11 +851,11 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
843851
str += ', ';
844852
// '[Object: null prototype] {'.length === 26
845853
// This is guarded with a test.
846-
str += utilInspect(obj, {
854+
str += StringPrototypeSlice(utilInspect(obj, {
847855
...ctx,
848856
breakLength: Infinity,
849857
compact: true
850-
}).slice(27, -2);
858+
}), 27, -2);
851859
}
852860
}
853861
return `<${this.constructor.name} ${str}>`;
@@ -991,12 +999,12 @@ function _fill(buf, value, offset, end, encoding) {
991999
} else if (value.length === 1) {
9921000
// Fast path: If `value` fits into a single byte, use that numeric value.
9931001
if (normalizedEncoding === 'utf8') {
994-
const code = value.charCodeAt(0);
1002+
const code = StringPrototypeCharCodeAt(value, 0);
9951003
if (code < 128) {
9961004
value = code;
9971005
}
9981006
} else if (normalizedEncoding === 'latin1') {
999-
value = value.charCodeAt(0);
1007+
value = StringPrototypeCharCodeAt(value, 0);
10001008
}
10011009
}
10021010
} else {
@@ -1021,12 +1029,12 @@ function _fill(buf, value, offset, end, encoding) {
10211029

10221030
if (typeof value === 'number') {
10231031
// OOB check
1024-
const byteLen = TypedArrayProto_byteLength.call(buf);
1032+
const byteLen = TypedArrayProto_byteLength(buf);
10251033
const fillLength = end - offset;
10261034
if (offset > end || fillLength + offset > byteLen)
10271035
throw new ERR_BUFFER_OUT_OF_BOUNDS();
10281036

1029-
TypedArrayFill.call(buf, value, offset, end);
1037+
TypedArrayPrototypeFill(buf, value, offset, end);
10301038
} else {
10311039
const res = bindingFill(buf, value, offset, end, encoding);
10321040
if (res < 0) {

0 commit comments

Comments
 (0)