Skip to content

Commit

Permalink
Extract DataView before use
Browse files Browse the repository at this point in the history
  • Loading branch information
aapoalas committed Jul 30, 2023
1 parent b9131fb commit efd0922
Showing 1 changed file with 136 additions and 34 deletions.
170 changes: 136 additions & 34 deletions ext/node/polyfills/internal/buffer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,13 +1175,21 @@ Buffer.prototype.writeUint32BE =

function wrtBigUInt64LE(buf, value, offset, min, max) {
checkIntBI(value, min, max, buf, offset, 8);
buf[VIEW_SYMBOL].setBigUint64(offset, value, true);
/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
view.setBigUint64(offset, value, true);
return offset + 8;
}

function wrtBigUInt64BE(buf, value, offset, min, max) {
checkIntBI(value, min, max, buf, offset, 8);
buf[VIEW_SYMBOL].setBigUint64(offset, value, false);
/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
view.setBigUint64(offset, value, false);
return offset + 8;
}

Expand Down Expand Up @@ -1671,46 +1679,70 @@ function BufferBigIntNotDefined() {
function readUInt48LE(buf, offset = 0) {
checkBounds(buf, offset, 6);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE unsigned bits + 16 LE unsigned bits shifted 32 bits up
return buf[VIEW_SYMBOL].getUint32(offset, true) +
buf[VIEW_SYMBOL].getUint16(offset + 4, true) * 2 ** 32;
return view.getUint32(offset, true) +
view.getUint16(offset + 4, true) * 2 ** 32;
}

function readUInt40LE(buf, offset = 0) {
checkBounds(buf, offset, 5);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE unsigned bits + 8 unsigned bits shifted 32 bits up
return buf[VIEW_SYMBOL].getUint32(offset, true) +
return view.getUint32(offset, true) +
buf[offset + 4] * 2 ** 32;
}

function readUInt24LE(buf, offset = 0) {
checkBounds(buf, offset, 3);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 16 LE unsigned bits + 8 unsigned bits shifted 16 bits up
return buf[VIEW_SYMBOL].getUint16(offset, true) + buf[offset + 2] * 2 ** 16;
return view.getUint16(offset, true) + buf[offset + 2] * 2 ** 16;
}

function readUInt48BE(buf, offset = 0) {
checkBounds(buf, offset, 6);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE unsigned bits shifted 16 bits up + 16 BE unsigned bits
return buf[VIEW_SYMBOL].getUint32(offset, false) * 2 ** 16 +
buf[VIEW_SYMBOL].getUint16(offset + 4, false);
return view.getUint32(offset, false) * 2 ** 16 +
view.getUint16(offset + 4, false);
}

function readUInt40BE(buf, offset = 0) {
checkBounds(buf, offset, 5);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE unsigned bits shifted 8 bits up + 8 unsigned bits
return buf[VIEW_SYMBOL].getUint32(offset, false) * 2 ** 8 + buf[offset + 4];
return view.getUint32(offset, false) * 2 ** 8 + buf[offset + 4];
}

function readUInt24BE(buf, offset = 0) {
checkBounds(buf, offset, 3);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 16 BE unsigned bits shifted 8 bits up + 8 unsigned bits
return buf[VIEW_SYMBOL].getUint16(offset, false) * 2 ** 8 + buf[offset + 2];
return view.getUint16(offset, false) * 2 ** 8 + buf[offset + 2];
}

function readUInt16BE(offset = 0) {
Expand All @@ -1734,53 +1766,75 @@ function readInt24LE(buf, offset = 0) {
// Because of two's complement format the bottom bits can
// always be viewed as unsigned bits. If the top is positive
// then the bottom adds to the positivity. If the top is negative
/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// then the bottom reduces from the negativity. In either case
// the binary representation is the same.
return buf[VIEW_SYMBOL].getUint16(offset, true) +
buf[VIEW_SYMBOL].getInt8(offset + 2) * 2 ** 16;
return view.getUint16(offset, true) + view.getInt8(offset + 2) * 2 ** 16;
}

function readInt40LE(buf, offset = 0) {
checkBounds(this, offset, 5);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE unsigned bits + 8 signed bits shifted 32 bits up
// Because of two's complement the bottom can be read unsigned.
return buf[VIEW_SYMBOL].getUint32(offset, true) +
buf[VIEW_SYMBOL].getInt8(offset + 4) * 2 ** 32;
return view.getUint32(offset, true) + view.getInt8(offset + 4) * 2 ** 32;
}

function readInt48LE(buf, offset = 0) {
checkBounds(this, offset, 6);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE unsigned bits + 16 signed bits shifted 32 bits up
// Because of two's complement the bottom can be read unsigned.
return buf[VIEW_SYMBOL].getUint32(offset, true) +
buf[VIEW_SYMBOL].getInt16(offset + 4, true) * 2 ** 32;
return view.getUint32(offset, true) +
view.getInt16(offset + 4, true) * 2 ** 32;
}

function readInt24BE(buf, offset = 0) {
checkBounds(this, offset, 3);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 16 BE signed bits shifted 8 bits up + 8 unsigned bits
// Because of two's complement the top can be read unsigned.
return buf[VIEW_SYMBOL].getInt16(offset, false) * 2 ** 8 + buf[offset + 2];
return view.getInt16(offset, false) * 2 ** 8 + buf[offset + 2];
}

function readInt48BE(buf, offset = 0) {
checkBounds(this, offset, 6);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE signed bits shifted 16 bits up + 16 unsigned bits
// Because of two's complement the top can be read unsigned.
return buf[VIEW_SYMBOL].getInt32(offset, false) * 2 ** 16 +
buf[VIEW_SYMBOL].getUint16(offset + 4, false);
return view.getInt32(offset, false) * 2 ** 16 +
view.getUint16(offset + 4, false);
}

function readInt40BE(buf, offset = 0) {
checkBounds(this, offset, 5);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE signed bits shifted 8 bits up + 8 unsigned bits
// Because of two's complement the top can be read unsigned.
return buf[VIEW_SYMBOL].getInt32(offset, false) * 2 ** 8 + buf[offset + 4];
return view.getInt32(offset, false) * 2 ** 8 + buf[offset + 4];
}

function byteLengthUtf8(str) {
Expand Down Expand Up @@ -2071,9 +2125,13 @@ function writeU_Int16BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 2);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 16 BE signed bits
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint16(offset, value, false);
view.setUint16(offset, value, false);

return offset + 2;
}
Expand All @@ -2082,8 +2140,12 @@ function _writeUInt32LE(buf, value, offset) {
value = +value;
checkInt(value, 0, 0xffffffff, buf, offset, 4);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE unsigned bits
buf[VIEW_SYMBOL].setUint32(offset, value, true);
view.setUint32(offset, value, true);

return offset + 4;
}
Expand All @@ -2093,9 +2155,13 @@ function writeU_Int16LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 2);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 16 LE signed bits
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint16(offset, value, true);
view.setUint16(offset, value, true);

return offset + 2;
}
Expand All @@ -2104,8 +2170,12 @@ function _writeUInt32BE(buf, value, offset) {
value = +value;
checkInt(value, 0, 0xffffffff, buf, offset, 4);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE unsigned bits
buf[VIEW_SYMBOL].setUint32(offset, value, false);
view.setUint32(offset, value, false);

return offset + 4;
}
Expand All @@ -2115,10 +2185,14 @@ function writeU_Int48BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 6);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE signed bits shifted 16 bits up + 16 BE unsigned bits
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint32(offset, Math.floor(value * 2 ** -16), false);
buf[VIEW_SYMBOL].setUint16(offset + 4, value, false);
view.setUint32(offset, Math.floor(value * 2 ** -16), false);
view.setUint16(offset + 4, value, false);
return offset + 6;
}

Expand All @@ -2127,9 +2201,13 @@ function writeU_Int40BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 5);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE signed bits shifted 8 bits up + 8 unsigned bits
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint32(offset, Math.floor(value * 2 ** -8), false);
view.setUint32(offset, Math.floor(value * 2 ** -8), false);
buf[offset + 4] = value;
return offset + 5;
}
Expand All @@ -2139,9 +2217,13 @@ function writeU_Int32BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 4);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 BE signed bits
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint32(offset, value, false);
view.setUint32(offset, value, false);

return offset + 4;
}
Expand All @@ -2151,9 +2233,13 @@ function writeU_Int24BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 3);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 16 BE signed bits shifted 8 bits up + 8 unsigned bits
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint16(offset, Math.floor(value * 2 ** -8), false);
view.setUint16(offset, Math.floor(value * 2 ** -8), false);
buf[offset + 2] = value;
return offset + 3;
}
Expand All @@ -2180,10 +2266,14 @@ function writeU_Int48LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 6);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE unsigned bits + 16 LE signed bits shifted 32 bits up
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint32(offset, value, true);
buf[VIEW_SYMBOL].setUint16(offset + 4, Math.floor(value * 2 ** -32), true);
view.setUint32(offset, value, true);
view.setUint16(offset + 4, Math.floor(value * 2 ** -32), true);
return offset + 6;
}

Expand All @@ -2192,9 +2282,13 @@ function writeU_Int40LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 5);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE unsigned bits + 8 signed bits shifted 32 bits up
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint32(offset, value, true);
view.setUint32(offset, value, true);
buf[offset + 4] = Math.floor(value * 2 ** -32);
return offset + 5;
}
Expand All @@ -2204,9 +2298,13 @@ function writeU_Int32LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 4);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 32 LE signed bits
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint32(offset, value, true);
view.setUint32(offset, value, true);
return offset + 4;
}

Expand All @@ -2215,9 +2313,13 @@ function writeU_Int24LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 3);

/**
* @type {DataView}
*/
const view = buf[VIEW_SYMBOL];
// 16 LE unsigned bits + 8 signed bits shifted 16 bits up
// Negative numbers properly become two's complement values.
buf[VIEW_SYMBOL].setUint16(offset, value, true);
view.setUint16(offset, value, true);
buf[offset + 2] = Math.floor(value * 2 ** -16);
return offset + 3;
}
Expand Down

0 comments on commit efd0922

Please sign in to comment.