0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

buffer: slow buffer copy compatibility fix

Fix issue where SlowBuffers couldn't be passed as target to Buffer
copy().

Also included checks to see if Argument parameters are defined before
assigning their values. This offered ~3x's performance gain.
This commit is contained in:
Trevor Norris 2013-01-22 09:09:12 -08:00 committed by Ben Noordhuis
parent 83154aa15d
commit 16bbeccd40
3 changed files with 14 additions and 6 deletions

View File

@ -573,8 +573,8 @@ Buffer.prototype.copy = function(target, target_start, start, end) {
end = target.length - target_start + start;
}
return this.parent.copy(target.parent,
target_start + target.offset,
return this.parent.copy(target.parent || target,
target_start + (target.offset || 0),
start + this.offset,
end + this.offset);
};

View File

@ -402,10 +402,10 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
Local<Object> target = args[0]->ToObject();
char* target_data = Buffer::Data(target);
size_t target_length = Buffer::Length(target);
size_t target_start = args[1]->Uint32Value();
size_t source_start = args[2]->Uint32Value();
size_t source_end = args[3]->IsUint32() ? args[3]->Uint32Value()
: source->length_;
size_t target_start = args[1]->IsUndefined() ? 0 : args[1]->Uint32Value();
size_t source_start = args[2]->IsUndefined() ? 0 : args[2]->Uint32Value();
size_t source_end = args[3]->IsUndefined() ? source->length_
: args[3]->Uint32Value();
if (source_end < source_start) {
return ThrowException(Exception::Error(String::New(

View File

@ -85,6 +85,14 @@ for (var i = 0; i < c.length; i++) {
assert.equal(i % 256, c[i]);
}
// copy from fast to slow buffer
var sb = new SlowBuffer(b.length);
var copied = b.copy(sb);
console.log('copied %d bytes from b into sb');
for (var i = 0; i < sb.length; i++) {
assert.strictEqual(sb[i], b[i]);
}
var caught_error = null;
// try to copy from before the beginning of b