Skip to content

Commit 8d8d226

Browse files
aduh95danielleadams
authored andcommitted
buffer: refactor to use more primordials
PR-URL: #36166 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 802d44b commit 8d8d226

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

lib/buffer.js

+33-25
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 {
@@ -108,12 +116,9 @@ const {
108116
createUnsafeBuffer
109117
} = require('internal/buffer');
110118

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

118123
FastBuffer.prototype.constructor = Buffer;
119124
Buffer.prototype = FastBuffer.prototype;
@@ -246,7 +251,7 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
246251
if (sourceStart !== 0 || sourceEnd < source.length)
247252
source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb);
248253

249-
target.set(source, targetStart);
254+
TypedArrayPrototypeSet(target, source, targetStart);
250255

251256
return nb;
252257
}
@@ -480,7 +485,7 @@ function fromArrayLike(obj) {
480485
if (obj.length > (poolSize - poolOffset))
481486
createPool();
482487
const b = new FastBuffer(allocPool, poolOffset, obj.length);
483-
b.set(obj, 0);
488+
TypedArrayPrototypeSet(b, obj, 0);
484489
poolOffset += obj.length;
485490
alignPool();
486491
return b;
@@ -566,17 +571,17 @@ Buffer.concat = function concat(list, length) {
566571
// Zero-fill the remaining bytes if the specified `length` was more than
567572
// the actual total length, i.e. if we have some remaining allocated bytes
568573
// there were not initialized.
569-
TypedArrayFill.call(buffer, 0, pos, length);
574+
TypedArrayPrototypeFill(buffer, 0, pos, length);
570575
}
571576

572577
return buffer;
573578
};
574579

575580
function base64ByteLength(str, bytes) {
576581
// Handle padding
577-
if (str.charCodeAt(bytes - 1) === 0x3D)
582+
if (StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
578583
bytes--;
579-
if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3D)
584+
if (bytes > 1 && StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
580585
bytes--;
581586

582587
// Base64 ratio: 3/4
@@ -666,38 +671,40 @@ function getEncodingOps(encoding) {
666671
case 4:
667672
if (encoding === 'utf8') return encodingOps.utf8;
668673
if (encoding === 'ucs2') return encodingOps.ucs2;
669-
encoding = encoding.toLowerCase();
674+
encoding = StringPrototypeToLowerCase(encoding);
670675
if (encoding === 'utf8') return encodingOps.utf8;
671676
if (encoding === 'ucs2') return encodingOps.ucs2;
672677
break;
673678
case 5:
674679
if (encoding === 'utf-8') return encodingOps.utf8;
675680
if (encoding === 'ascii') return encodingOps.ascii;
676681
if (encoding === 'ucs-2') return encodingOps.ucs2;
677-
encoding = encoding.toLowerCase();
682+
encoding = StringPrototypeToLowerCase(encoding);
678683
if (encoding === 'utf-8') return encodingOps.utf8;
679684
if (encoding === 'ascii') return encodingOps.ascii;
680685
if (encoding === 'ucs-2') return encodingOps.ucs2;
681686
break;
682687
case 7:
683-
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
688+
if (encoding === 'utf16le' ||
689+
StringPrototypeToLowerCase(encoding) === 'utf16le')
684690
return encodingOps.utf16le;
685691
break;
686692
case 8:
687-
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
693+
if (encoding === 'utf-16le' ||
694+
StringPrototypeToLowerCase(encoding) === 'utf-16le')
688695
return encodingOps.utf16le;
689696
break;
690697
case 6:
691698
if (encoding === 'latin1' || encoding === 'binary')
692699
return encodingOps.latin1;
693700
if (encoding === 'base64') return encodingOps.base64;
694-
encoding = encoding.toLowerCase();
701+
encoding = StringPrototypeToLowerCase(encoding);
695702
if (encoding === 'latin1' || encoding === 'binary')
696703
return encodingOps.latin1;
697704
if (encoding === 'base64') return encodingOps.base64;
698705
break;
699706
case 3:
700-
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
707+
if (encoding === 'hex' || StringPrototypeToLowerCase(encoding) === 'hex')
701708
return encodingOps.hex;
702709
break;
703710
}
@@ -810,7 +817,8 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
810817
const max = INSPECT_MAX_BYTES;
811818
const actualMax = MathMin(max, this.length);
812819
const remaining = this.length - max;
813-
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
820+
let str = StringPrototypeTrim(StringPrototypeReplace(
821+
this.hexSlice(0, actualMax), /(.{2})/g, '$1 '));
814822
if (remaining > 0)
815823
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
816824
// Inspect special properties as well, if possible.
@@ -827,11 +835,11 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
827835
str += ', ';
828836
// '[Object: null prototype] {'.length === 26
829837
// This is guarded with a test.
830-
str += utilInspect(obj, {
838+
str += StringPrototypeSlice(utilInspect(obj, {
831839
...ctx,
832840
breakLength: Infinity,
833841
compact: true
834-
}).slice(27, -2);
842+
}), 27, -2);
835843
}
836844
}
837845
return `<${this.constructor.name} ${str}>`;
@@ -975,12 +983,12 @@ function _fill(buf, value, offset, end, encoding) {
975983
} else if (value.length === 1) {
976984
// Fast path: If `value` fits into a single byte, use that numeric value.
977985
if (normalizedEncoding === 'utf8') {
978-
const code = value.charCodeAt(0);
986+
const code = StringPrototypeCharCodeAt(value, 0);
979987
if (code < 128) {
980988
value = code;
981989
}
982990
} else if (normalizedEncoding === 'latin1') {
983-
value = value.charCodeAt(0);
991+
value = StringPrototypeCharCodeAt(value, 0);
984992
}
985993
}
986994
} else {
@@ -1005,12 +1013,12 @@ function _fill(buf, value, offset, end, encoding) {
10051013

10061014
if (typeof value === 'number') {
10071015
// OOB check
1008-
const byteLen = TypedArrayProto_byteLength.call(buf);
1016+
const byteLen = TypedArrayProto_byteLength(buf);
10091017
const fillLength = end - offset;
10101018
if (offset > end || fillLength + offset > byteLen)
10111019
throw new ERR_BUFFER_OUT_OF_BOUNDS();
10121020

1013-
TypedArrayFill.call(buf, value, offset, end);
1021+
TypedArrayPrototypeFill(buf, value, offset, end);
10141022
} else {
10151023
const res = bindingFill(buf, value, offset, end, encoding);
10161024
if (res < 0) {

0 commit comments

Comments
 (0)