-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discussion] FFI - Giving Buffer more low-level C functionality #1750
Changes from all commits
c1d2975
1871fbf
ca55494
6c32b7e
1f65088
b8eac6d
a9cdb38
e338a92
af8bff4
0f1e401
2d54386
db39bc2
34feb80
89e7d61
5e9c2a4
db6aea9
1fdf531
ff5a094
dea8c4d
4dae6be
619d136
6e07bbc
e6f3262
565a1e8
66d68ef
51e9556
2b6ec58
c84e890
001b072
86ff08c
319e593
924da7f
ed4d220
13c77bd
61c5c7c
a9e8f62
f0cec38
3f0d7a0
ea44b75
7326597
2ace08f
e7f8d77
4ee5354
f364f1b
13b5d67
f451ee7
39d468c
0c80b8c
1867bd6
b929ed0
0e5cbe5
d6e37cd
01a3ad3
df5bda7
8477e98
3887226
c79e4f7
91e35b4
797ef99
475aabb
ce00983
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,7 +398,7 @@ Buffer.prototype.inspect = function inspect() { | |
if (this.length > max) | ||
str += ' ... '; | ||
} | ||
return '<' + this.constructor.name + ' ' + str + '>'; | ||
return '<' + this.constructor.name + '@0x' + this.address() + ' ' + str + '>'; | ||
}; | ||
|
||
|
||
|
@@ -454,6 +454,13 @@ Buffer.prototype.fill = function fill(val, start, end) { | |
}; | ||
|
||
|
||
Buffer.prototype.address = function address() { | ||
if (!(this instanceof Buffer)) | ||
throw new TypeError('this must be a Buffer'); | ||
return binding.address(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should have preliminary checks here that check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a note, both this check and the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JS-side There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. They should in fact have these checks. EDIT: I'll take care of the other cases in a different PR. Thanks for bringing them up. |
||
}; | ||
|
||
|
||
// XXX remove in v0.13 | ||
Buffer.prototype.get = util.deprecate(function get(offset) { | ||
offset = ~~offset; | ||
|
@@ -818,6 +825,38 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) { | |
}; | ||
|
||
|
||
Buffer.prototype.readInt64LE = function readInt64LE(offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkOffset(offset, 8, this.length); | ||
return binding.readInt64LE(this, offset); | ||
}; | ||
|
||
|
||
Buffer.prototype.readInt64BE = function readInt64BE(offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkOffset(offset, 8, this.length); | ||
return binding.readInt64BE(this, offset); | ||
}; | ||
|
||
|
||
Buffer.prototype.readUInt64LE = function readUInt64LE(offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkOffset(offset, 8, this.length); | ||
return binding.readUInt64LE(this, offset); | ||
}; | ||
|
||
|
||
Buffer.prototype.readUInt64BE = function readUInt64BE(offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkOffset(offset, 8, this.length); | ||
return binding.readUInt64BE(this, offset); | ||
}; | ||
|
||
|
||
function checkInt(buffer, value, offset, ext, max, min) { | ||
if (!(buffer instanceof Buffer)) | ||
throw new TypeError('buffer must be a Buffer instance'); | ||
|
@@ -1071,6 +1110,72 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) { | |
return offset + 8; | ||
}; | ||
|
||
|
||
function checkInt64(buffer, value, offset, ext) { | ||
if (!(buffer instanceof Buffer)) | ||
throw new TypeError('buffer must be a Buffer instance'); | ||
if (!(typeof value === 'string' || typeof value === 'number')) | ||
throw new TypeError('must pass a "string" or "number" for value'); | ||
if (offset + ext > buffer.length) | ||
throw new RangeError('index out of range'); | ||
} | ||
|
||
|
||
Buffer.prototype.writeInt64LE = function writeInt64LE(val, offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkInt64(this, val, offset, 8); | ||
return binding.writeInt64LE(this, val, offset); | ||
}; | ||
|
||
|
||
Buffer.prototype.writeInt64BE = function writeInt64BE(val, offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkInt64(this, val, offset, 8); | ||
return binding.writeInt64BE(this, val, offset); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For all these |
||
}; | ||
|
||
|
||
Buffer.prototype.writeUInt64LE = function writeUInt64LE(val, offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkInt64(this, val, offset, 8); | ||
return binding.writeUInt64LE(this, val, offset); | ||
}; | ||
|
||
|
||
Buffer.prototype.writeUInt64BE = function writeUInt64BE(val, offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkInt64(this, val, offset, 8); | ||
return binding.writeUInt64BE(this, val, offset); | ||
}; | ||
|
||
|
||
function checkPointer(buffer, value, offset) { | ||
if (!(buffer instanceof Buffer)) | ||
throw new TypeError('buffer must be a Buffer instance'); | ||
if (!(value === null || value instanceof Buffer)) | ||
throw new TypeError('value must be a Buffer instance or null'); | ||
} | ||
|
||
Buffer.prototype.writePointerLE = function writePointerLE(val, offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkPointer(this, val, offset); | ||
return binding.writePointerLE(this, val, offset); | ||
}; | ||
|
||
|
||
Buffer.prototype.writePointerBE = function writePointerBE(val, offset, noAssert) { | ||
offset = offset >>> 0; | ||
if (!noAssert) | ||
checkPointer(this, val, offset); | ||
return binding.writePointerBE(this, val, offset); | ||
}; | ||
|
||
|
||
// ES6 iterator | ||
|
||
var ITERATOR_KIND_KEYS = 1; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like including the address on
inspect()
isn't useful for the> 95% of use cases, and since there's now theaddress()
function it can be retrieved at any time.