buffer: error and misc cleanup
Changed types of errors thrown to be more indicative of what the error represents. Also removed a few unnecessary uses of the v8 fully quantified typename.
This commit is contained in:
parent
49175e6ae2
commit
cbe3941db9
@ -84,7 +84,7 @@ SlowBuffer.prototype.toString = function(encoding, start, end) {
|
|||||||
return this.ucs2Slice(start, end);
|
return this.ucs2Slice(start, end);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error('Unknown encoding: ' + encoding);
|
throw new TypeError('Unknown encoding: ' + encoding);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -104,14 +104,14 @@ SlowBuffer.prototype.hexWrite = function(string, offset, length) {
|
|||||||
// must be an even number of digits
|
// must be an even number of digits
|
||||||
var strLen = string.length;
|
var strLen = string.length;
|
||||||
if (strLen % 2) {
|
if (strLen % 2) {
|
||||||
throw new Error('Invalid hex string');
|
throw new TypeError('Invalid hex string');
|
||||||
}
|
}
|
||||||
if (length > strLen / 2) {
|
if (length > strLen / 2) {
|
||||||
length = strLen / 2;
|
length = strLen / 2;
|
||||||
}
|
}
|
||||||
for (var i = 0; i < length; i++) {
|
for (var i = 0; i < length; i++) {
|
||||||
var byte = parseInt(string.substr(i * 2, 2), 16);
|
var byte = parseInt(string.substr(i * 2, 2), 16);
|
||||||
if (isNaN(byte)) throw new Error('Invalid hex string');
|
if (isNaN(byte)) throw new TypeError('Invalid hex string');
|
||||||
this[offset + i] = byte;
|
this[offset + i] = byte;
|
||||||
}
|
}
|
||||||
SlowBuffer._charsWritten = i * 2;
|
SlowBuffer._charsWritten = i * 2;
|
||||||
@ -170,7 +170,7 @@ SlowBuffer.prototype.write = function(string, offset, length, encoding) {
|
|||||||
return this.ucs2Write(string, offset, length);
|
return this.ucs2Write(string, offset, length);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error('Unknown encoding: ' + encoding);
|
throw new TypeError('Unknown encoding: ' + encoding);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -180,10 +180,10 @@ SlowBuffer.prototype.slice = function(start, end) {
|
|||||||
if (end === undefined) end = this.length;
|
if (end === undefined) end = this.length;
|
||||||
|
|
||||||
if (end > this.length) {
|
if (end > this.length) {
|
||||||
throw new Error('oob');
|
throw new RangeError('end > this.length');
|
||||||
}
|
}
|
||||||
if (start > end) {
|
if (start > end) {
|
||||||
throw new Error('oob');
|
throw new RangeError('start > end');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Buffer(this, end - start, +start);
|
return new Buffer(this, end - start, +start);
|
||||||
@ -212,7 +212,7 @@ function Buffer(subject, encoding, offset) {
|
|||||||
// Are we slicing?
|
// Are we slicing?
|
||||||
if (typeof offset === 'number') {
|
if (typeof offset === 'number') {
|
||||||
if (!Buffer.isBuffer(subject)) {
|
if (!Buffer.isBuffer(subject)) {
|
||||||
throw new Error('First argument must be a Buffer when slicing');
|
throw new TypeError('First argument must be a Buffer when slicing');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.length = coerce(encoding);
|
this.length = coerce(encoding);
|
||||||
@ -234,7 +234,7 @@ function Buffer(subject, encoding, offset) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error('First argument needs to be a number, ' +
|
throw new TypeError('First argument needs to be a number, ' +
|
||||||
'array or string.');
|
'array or string.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,15 +336,17 @@ Buffer.prototype.inspect = function inspect() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Buffer.prototype.get = function get(i) {
|
Buffer.prototype.get = function get(offset) {
|
||||||
if (i < 0 || i >= this.length) throw new Error('oob');
|
if (offset < 0 || offset >= this.length)
|
||||||
return this.parent[this.offset + i];
|
throw new RangeError('offset is out of bounds');
|
||||||
|
return this.parent[this.offset + offset];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Buffer.prototype.set = function set(i, v) {
|
Buffer.prototype.set = function set(offset, v) {
|
||||||
if (i < 0 || i >= this.length) throw new Error('oob');
|
if (offset < 0 || offset >= this.length)
|
||||||
return this.parent[this.offset + i] = v;
|
throw new RangeError('offset is out of bounds');
|
||||||
|
return this.parent[this.offset + offset] = v;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -408,7 +410,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error('Unknown encoding: ' + encoding);
|
throw new TypeError('Unknown encoding: ' + encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer._charsWritten = SlowBuffer._charsWritten;
|
Buffer._charsWritten = SlowBuffer._charsWritten;
|
||||||
@ -465,7 +467,7 @@ Buffer.prototype.toString = function(encoding, start, end) {
|
|||||||
return this.parent.ucs2Slice(start, end);
|
return this.parent.ucs2Slice(start, end);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error('Unknown encoding: ' + encoding);
|
throw new TypeError('Unknown encoding: ' + encoding);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -484,21 +486,21 @@ Buffer.prototype.fill = function fill(value, start, end) {
|
|||||||
value = value.charCodeAt(0);
|
value = value.charCodeAt(0);
|
||||||
}
|
}
|
||||||
if (!(typeof value === 'number') || isNaN(value)) {
|
if (!(typeof value === 'number') || isNaN(value)) {
|
||||||
throw new Error('value is not a number');
|
throw new TypeError('value is not a number');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end < start) throw new Error('end < start');
|
if (end < start) throw new RangeError('end < start');
|
||||||
|
|
||||||
// Fill 0 bytes; we're done
|
// Fill 0 bytes; we're done
|
||||||
if (end === start) return 0;
|
if (end === start) return 0;
|
||||||
if (this.length == 0) return 0;
|
if (this.length == 0) return 0;
|
||||||
|
|
||||||
if (start < 0 || start >= this.length) {
|
if (start < 0 || start >= this.length) {
|
||||||
throw new Error('start out of bounds');
|
throw new RangeError('start out of bounds');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end < 0 || end > this.length) {
|
if (end < 0 || end > this.length) {
|
||||||
throw new Error('end out of bounds');
|
throw new RangeError('end out of bounds');
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.parent.fill(value,
|
return this.parent.fill(value,
|
||||||
@ -509,7 +511,7 @@ Buffer.prototype.fill = function fill(value, start, end) {
|
|||||||
|
|
||||||
Buffer.concat = function(list, length) {
|
Buffer.concat = function(list, length) {
|
||||||
if (!Array.isArray(list)) {
|
if (!Array.isArray(list)) {
|
||||||
throw new Error('Usage: Buffer.concat(list, [length])');
|
throw new TypeError('Usage: Buffer.concat(list, [length])');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (list.length === 0) {
|
if (list.length === 0) {
|
||||||
@ -572,9 +574,12 @@ Buffer.prototype.copy = function(target, target_start, start, end) {
|
|||||||
// slice(start, end)
|
// slice(start, end)
|
||||||
Buffer.prototype.slice = function(start, end) {
|
Buffer.prototype.slice = function(start, end) {
|
||||||
if (end === undefined) end = this.length;
|
if (end === undefined) end = this.length;
|
||||||
if (end > this.length) throw new Error('oob');
|
if (end > this.length)
|
||||||
if (start > end) throw new Error('oob');
|
throw new RangeError('end > this.length');
|
||||||
if (start < 0) throw new Error('start out of bounds');
|
if (start > end)
|
||||||
|
throw new RangeError('start > end');
|
||||||
|
if (start < 0)
|
||||||
|
throw new RangeError('start < 0');
|
||||||
return new Buffer(this.parent, end - start, +start + this.offset);
|
return new Buffer(this.parent, end - start, +start + this.offset);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -395,8 +395,7 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
|
|||||||
Buffer *source = ObjectWrap::Unwrap<Buffer>(args.This());
|
Buffer *source = ObjectWrap::Unwrap<Buffer>(args.This());
|
||||||
|
|
||||||
if (!Buffer::HasInstance(args[0])) {
|
if (!Buffer::HasInstance(args[0])) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("First arg should be a Buffer");
|
||||||
"First arg should be a Buffer")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<Object> target = args[0]->ToObject();
|
Local<Object> target = args[0]->ToObject();
|
||||||
@ -408,8 +407,7 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
|
|||||||
: args[3]->Uint32Value();
|
: args[3]->Uint32Value();
|
||||||
|
|
||||||
if (source_end < source_start) {
|
if (source_end < source_start) {
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowRangeError("sourceEnd < sourceStart");
|
||||||
"sourceEnd < sourceStart")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy 0 bytes; we're done
|
// Copy 0 bytes; we're done
|
||||||
@ -418,18 +416,15 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (target_start >= target_length) {
|
if (target_start >= target_length) {
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowRangeError("targetStart out of bounds");
|
||||||
"targetStart out of bounds")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source_start >= source->length_) {
|
if (source_start >= source->length_) {
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowRangeError("sourceStart out of bounds");
|
||||||
"sourceStart out of bounds")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source_end > source->length_) {
|
if (source_end > source->length_) {
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowRangeError("sourceEnd out of bounds");
|
||||||
"sourceEnd out of bounds")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t to_copy = MIN(MIN(source_end - source_start,
|
size_t to_copy = MIN(MIN(source_end - source_start,
|
||||||
@ -468,8 +463,7 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (length > 0 && offset >= buffer->length_) {
|
if (length > 0 && offset >= buffer->length_) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Offset is out of bounds");
|
||||||
"Offset is out of bounds")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t max_length = args[2]->IsUndefined() ? buffer->length_ - offset
|
size_t max_length = args[2]->IsUndefined() ? buffer->length_ - offset
|
||||||
@ -500,8 +494,7 @@ Handle<Value> Buffer::Ucs2Write(const Arguments &args) {
|
|||||||
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
||||||
|
|
||||||
if (!args[0]->IsString()) {
|
if (!args[0]->IsString()) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Argument must be a string");
|
||||||
"Argument must be a string")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<String> s = args[0]->ToString();
|
Local<String> s = args[0]->ToString();
|
||||||
@ -539,8 +532,7 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
|
|||||||
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
||||||
|
|
||||||
if (!args[0]->IsString()) {
|
if (!args[0]->IsString()) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Argument must be a string");
|
||||||
"Argument must be a string")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<String> s = args[0]->ToString();
|
Local<String> s = args[0]->ToString();
|
||||||
@ -548,8 +540,7 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
|
|||||||
size_t offset = args[1]->Int32Value();
|
size_t offset = args[1]->Int32Value();
|
||||||
|
|
||||||
if (length > 0 && offset >= buffer->length_) {
|
if (length > 0 && offset >= buffer->length_) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Offset is out of bounds");
|
||||||
"Offset is out of bounds")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t max_length = args[2]->IsUndefined() ? buffer->length_ - offset
|
size_t max_length = args[2]->IsUndefined() ? buffer->length_ - offset
|
||||||
@ -578,8 +569,7 @@ Handle<Value> Buffer::Base64Write(const Arguments &args) {
|
|||||||
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
||||||
|
|
||||||
if (!args[0]->IsString()) {
|
if (!args[0]->IsString()) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Argument must be a string");
|
||||||
"Argument must be a string")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::AsciiValue s(args[0]);
|
String::AsciiValue s(args[0]);
|
||||||
@ -590,8 +580,7 @@ Handle<Value> Buffer::Base64Write(const Arguments &args) {
|
|||||||
max_length = MIN(length, MIN(buffer->length_ - offset, max_length));
|
max_length = MIN(length, MIN(buffer->length_ - offset, max_length));
|
||||||
|
|
||||||
if (max_length && offset >= buffer->length_) {
|
if (max_length && offset >= buffer->length_) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Offset is out of bounds");
|
||||||
"Offset is out of bounds")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char a, b, c, d;
|
char a, b, c, d;
|
||||||
@ -643,8 +632,7 @@ Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
|
|||||||
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
|
||||||
|
|
||||||
if (!args[0]->IsString()) {
|
if (!args[0]->IsString()) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Argument must be a string");
|
||||||
"Argument must be a string")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<String> s = args[0]->ToString();
|
Local<String> s = args[0]->ToString();
|
||||||
@ -652,8 +640,7 @@ Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
|
|||||||
size_t offset = args[1]->Int32Value();
|
size_t offset = args[1]->Int32Value();
|
||||||
|
|
||||||
if (s->Length() > 0 && offset >= buffer->length_) {
|
if (s->Length() > 0 && offset >= buffer->length_) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Offset is out of bounds");
|
||||||
"Offset is out of bounds")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *p = (char*)buffer->data_ + offset;
|
char *p = (char*)buffer->data_ + offset;
|
||||||
@ -793,8 +780,7 @@ Handle<Value> Buffer::ByteLength(const Arguments &args) {
|
|||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
if (!args[0]->IsString()) {
|
if (!args[0]->IsString()) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("Argument must be a string");
|
||||||
"Argument must be a string")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<String> s = args[0]->ToString();
|
Local<String> s = args[0]->ToString();
|
||||||
@ -808,8 +794,7 @@ Handle<Value> Buffer::MakeFastBuffer(const Arguments &args) {
|
|||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
if (!Buffer::HasInstance(args[0])) {
|
if (!Buffer::HasInstance(args[0])) {
|
||||||
return ThrowException(Exception::TypeError(String::New(
|
return ThrowTypeError("First argument must be a Buffer");
|
||||||
"First argument must be a Buffer")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
|
||||||
@ -838,9 +823,9 @@ Handle<Value> Buffer::MakeFastBuffer(const Arguments &args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Buffer::HasInstance(v8::Handle<v8::Value> val) {
|
bool Buffer::HasInstance(Handle<Value> val) {
|
||||||
if (!val->IsObject()) return false;
|
if (!val->IsObject()) return false;
|
||||||
v8::Local<v8::Object> obj = val->ToObject();
|
Local<Object> obj = val->ToObject();
|
||||||
|
|
||||||
if (obj->GetIndexedPropertiesExternalArrayDataType() == kExternalUnsignedByteArray)
|
if (obj->GetIndexedPropertiesExternalArrayDataType() == kExternalUnsignedByteArray)
|
||||||
return true;
|
return true;
|
||||||
@ -853,7 +838,7 @@ bool Buffer::HasInstance(v8::Handle<v8::Value> val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class RetainedBufferInfo: public v8::RetainedObjectInfo {
|
class RetainedBufferInfo: public RetainedObjectInfo {
|
||||||
public:
|
public:
|
||||||
RetainedBufferInfo(Buffer* buffer);
|
RetainedBufferInfo(Buffer* buffer);
|
||||||
virtual void Dispose();
|
virtual void Dispose();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user