This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer: make SlowBuffer inherit from Buffer
This frees us from manually having to copy over functions to SlowBuffer's prototype (which has bitten us multiple times in the past). As an added bonus, the `inspect()` function is now shared between Buffer and SlowBuffer, removing some duplicate code. Closes #3228.
- Loading branch information
1 parent
a811a4a
commit 38542f7
Showing
1 changed file
with
9 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,27 +33,17 @@ function binaryWarn() { | |
|
||
exports.INSPECT_MAX_BYTES = 50; | ||
|
||
// Make SlowBuffer inherit from Buffer. | ||
// This is an exception to the rule that __proto__ is not allowed in core. | ||
SlowBuffer.prototype.__proto__ = Buffer.prototype; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
TooTallNate
Author
|
||
|
||
|
||
function toHex(n) { | ||
if (n < 16) return '0' + n.toString(16); | ||
return n.toString(16); | ||
} | ||
|
||
|
||
SlowBuffer.prototype.inspect = function() { | ||
var out = [], | ||
len = this.length; | ||
for (var i = 0; i < len; i++) { | ||
out[i] = toHex(this[i]); | ||
if (i == exports.INSPECT_MAX_BYTES) { | ||
out[i + 1] = '...'; | ||
break; | ||
} | ||
} | ||
return '<SlowBuffer ' + out.join(' ') + '>'; | ||
}; | ||
|
||
|
||
SlowBuffer.prototype.hexSlice = function(start, end) { | ||
var len = this.length; | ||
|
||
|
@@ -302,24 +292,25 @@ function allocPool() { | |
|
||
// Static methods | ||
Buffer.isBuffer = function isBuffer(b) { | ||
return b instanceof Buffer || b instanceof SlowBuffer; | ||
return b instanceof Buffer; | ||
}; | ||
|
||
|
||
// Inspect | ||
Buffer.prototype.inspect = function inspect() { | ||
var out = [], | ||
len = this.length; | ||
len = this.length, | ||
name = this.constructor.name; | ||
|
||
for (var i = 0; i < len; i++) { | ||
out[i] = toHex(this.parent[i + this.offset]); | ||
out[i] = toHex(this[i]); | ||
if (i == exports.INSPECT_MAX_BYTES) { | ||
out[i + 1] = '...'; | ||
break; | ||
} | ||
} | ||
|
||
return '<Buffer ' + out.join(' ') + '>'; | ||
return '<' + name + ' ' + out.join(' ') + '>'; | ||
}; | ||
|
||
|
||
|
@@ -1143,32 +1134,3 @@ Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) { | |
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) { | ||
writeDouble(this, value, offset, true, noAssert); | ||
}; | ||
|
||
SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8; | ||
SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE; | ||
SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE; | ||
SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE; | ||
SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE; | ||
SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8; | ||
SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE; | ||
SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE; | ||
SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE; | ||
SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE; | ||
SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE; | ||
SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE; | ||
SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE; | ||
SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE; | ||
SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8; | ||
SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE; | ||
SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE; | ||
SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE; | ||
SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE; | ||
SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8; | ||
SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE; | ||
SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE; | ||
SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE; | ||
SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE; | ||
SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE; | ||
SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE; | ||
SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE; | ||
SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE; |
should be be able to use Object.create() no? same thing