Skip to content

Commit

Permalink
buffer: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36166
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Jun 11, 2021
1 parent ee44447 commit 5acf0a5
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
StringPrototypeCharCodeAt,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeToLowerCase,
StringPrototypeTrim,
SymbolSpecies,
SymbolToPrimitive,
TypedArrayPrototype,
TypedArrayPrototypeFill,
TypedArrayPrototypeSet,
Uint8Array,
Uint8ArrayPrototype,
uncurryThis,
} = primordials;

const {
Expand Down Expand Up @@ -109,12 +117,9 @@ const {
addBufferPrototypeMethods
} = require('internal/buffer');

const TypedArrayPrototype = ObjectGetPrototypeOf(Uint8ArrayPrototype);

const TypedArrayProto_byteLength =
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
'byteLength').get;
const TypedArrayFill = TypedArrayPrototype.fill;
const TypedArrayProto_byteLength = uncurryThis(
ObjectGetOwnPropertyDescriptor(TypedArrayPrototype,
'byteLength').get);

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

target.set(source, targetStart);
TypedArrayPrototypeSet(target, source, targetStart);

return nb;
}
Expand Down Expand Up @@ -496,7 +501,7 @@ function fromArrayLike(obj) {
if (obj.length > (poolSize - poolOffset))
createPool();
const b = new FastBuffer(allocPool, poolOffset, obj.length);
b.set(obj, 0);
TypedArrayPrototypeSet(b, obj, 0);
poolOffset += obj.length;
alignPool();
return b;
Expand Down Expand Up @@ -582,17 +587,17 @@ Buffer.concat = function concat(list, length) {
// Zero-fill the remaining bytes if the specified `length` was more than
// the actual total length, i.e. if we have some remaining allocated bytes
// there were not initialized.
TypedArrayFill.call(buffer, 0, pos, length);
TypedArrayPrototypeFill(buffer, 0, pos, length);
}

return buffer;
};

function base64ByteLength(str, bytes) {
// Handle padding
if (str.charCodeAt(bytes - 1) === 0x3D)
if (StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
bytes--;
if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3D)
if (bytes > 1 && StringPrototypeCharCodeAt(str, bytes - 1) === 0x3D)
bytes--;

// Base64 ratio: 3/4
Expand Down Expand Up @@ -682,38 +687,40 @@ function getEncodingOps(encoding) {
case 4:
if (encoding === 'utf8') return encodingOps.utf8;
if (encoding === 'ucs2') return encodingOps.ucs2;
encoding = encoding.toLowerCase();
encoding = StringPrototypeToLowerCase(encoding);
if (encoding === 'utf8') return encodingOps.utf8;
if (encoding === 'ucs2') return encodingOps.ucs2;
break;
case 5:
if (encoding === 'utf-8') return encodingOps.utf8;
if (encoding === 'ascii') return encodingOps.ascii;
if (encoding === 'ucs-2') return encodingOps.ucs2;
encoding = encoding.toLowerCase();
encoding = StringPrototypeToLowerCase(encoding);
if (encoding === 'utf-8') return encodingOps.utf8;
if (encoding === 'ascii') return encodingOps.ascii;
if (encoding === 'ucs-2') return encodingOps.ucs2;
break;
case 7:
if (encoding === 'utf16le' || encoding.toLowerCase() === 'utf16le')
if (encoding === 'utf16le' ||
StringPrototypeToLowerCase(encoding) === 'utf16le')
return encodingOps.utf16le;
break;
case 8:
if (encoding === 'utf-16le' || encoding.toLowerCase() === 'utf-16le')
if (encoding === 'utf-16le' ||
StringPrototypeToLowerCase(encoding) === 'utf-16le')
return encodingOps.utf16le;
break;
case 6:
if (encoding === 'latin1' || encoding === 'binary')
return encodingOps.latin1;
if (encoding === 'base64') return encodingOps.base64;
encoding = encoding.toLowerCase();
encoding = StringPrototypeToLowerCase(encoding);
if (encoding === 'latin1' || encoding === 'binary')
return encodingOps.latin1;
if (encoding === 'base64') return encodingOps.base64;
break;
case 3:
if (encoding === 'hex' || encoding.toLowerCase() === 'hex')
if (encoding === 'hex' || StringPrototypeToLowerCase(encoding) === 'hex')
return encodingOps.hex;
break;
}
Expand Down Expand Up @@ -826,7 +833,8 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
const max = INSPECT_MAX_BYTES;
const actualMax = MathMin(max, this.length);
const remaining = this.length - max;
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
let str = StringPrototypeTrim(StringPrototypeReplace(
this.hexSlice(0, actualMax), /(.{2})/g, '$1 '));
if (remaining > 0)
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
// Inspect special properties as well, if possible.
Expand All @@ -843,11 +851,11 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
str += ', ';
// '[Object: null prototype] {'.length === 26
// This is guarded with a test.
str += utilInspect(obj, {
str += StringPrototypeSlice(utilInspect(obj, {
...ctx,
breakLength: Infinity,
compact: true
}).slice(27, -2);
}), 27, -2);
}
}
return `<${this.constructor.name} ${str}>`;
Expand Down Expand Up @@ -991,12 +999,12 @@ function _fill(buf, value, offset, end, encoding) {
} else if (value.length === 1) {
// Fast path: If `value` fits into a single byte, use that numeric value.
if (normalizedEncoding === 'utf8') {
const code = value.charCodeAt(0);
const code = StringPrototypeCharCodeAt(value, 0);
if (code < 128) {
value = code;
}
} else if (normalizedEncoding === 'latin1') {
value = value.charCodeAt(0);
value = StringPrototypeCharCodeAt(value, 0);
}
}
} else {
Expand All @@ -1021,12 +1029,12 @@ function _fill(buf, value, offset, end, encoding) {

if (typeof value === 'number') {
// OOB check
const byteLen = TypedArrayProto_byteLength.call(buf);
const byteLen = TypedArrayProto_byteLength(buf);
const fillLength = end - offset;
if (offset > end || fillLength + offset > byteLen)
throw new ERR_BUFFER_OUT_OF_BOUNDS();

TypedArrayFill.call(buf, value, offset, end);
TypedArrayPrototypeFill(buf, value, offset, end);
} else {
const res = bindingFill(buf, value, offset, end, encoding);
if (res < 0) {
Expand Down

0 comments on commit 5acf0a5

Please sign in to comment.