Work to get C++ fast buffers. incomplete
This commit is contained in:
parent
2dda6be799
commit
1cf538a60a
@ -153,6 +153,40 @@ Buffer* Buffer::New(size_t size) {
|
||||
}
|
||||
|
||||
|
||||
char* Buffer::Data(Handle<Object> obj) {
|
||||
if (obj->HasIndexedPropertiesInPixelData()) {
|
||||
return (char*)obj->GetIndexedPropertiesPixelData();
|
||||
}
|
||||
|
||||
HandleScope scope;
|
||||
|
||||
// Return true for "SlowBuffer"
|
||||
if (constructor_template->HasInstance(obj)) {
|
||||
return ObjectWrap::Unwrap<Buffer>(obj)->data();
|
||||
}
|
||||
|
||||
// Not a buffer.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
size_t Buffer::Length(Handle<Object> obj) {
|
||||
if (obj->HasIndexedPropertiesInPixelData()) {
|
||||
return (size_t)obj->GetIndexedPropertiesPixelDataLength();
|
||||
}
|
||||
|
||||
HandleScope scope;
|
||||
|
||||
// Return true for "SlowBuffer"
|
||||
if (constructor_template->HasInstance(obj)) {
|
||||
return ObjectWrap::Unwrap<Buffer>(obj)->length();
|
||||
}
|
||||
|
||||
// Not a buffer.
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Buffer::New(const Arguments &args) {
|
||||
HandleScope scope;
|
||||
|
||||
@ -274,7 +308,7 @@ Handle<Value> Buffer::BinarySlice(const Arguments &args) {
|
||||
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
|
||||
SLICE_ARGS(args[0], args[1])
|
||||
|
||||
const char *data = const_cast<char*>(parent->data() + start);
|
||||
char *data = parent->data() + start;
|
||||
//Local<String> string = String::New(data, end - start);
|
||||
|
||||
Local<Value> b = Encode(data, end - start, BINARY);
|
||||
@ -296,9 +330,8 @@ Handle<Value> Buffer::AsciiSlice(const Arguments &args) {
|
||||
assert(parent->blob_->refs >= 2);
|
||||
#endif
|
||||
|
||||
const char *data = const_cast<char*>(parent->data() + start);
|
||||
Local<String> string = String::New(data, end - start);
|
||||
|
||||
char* data = parent->data() + start;
|
||||
Local<String> string = String::New(reinterpret_cast<char*>(data), end - start);
|
||||
|
||||
return scope.Close(string);
|
||||
}
|
||||
@ -308,8 +341,8 @@ Handle<Value> Buffer::Utf8Slice(const Arguments &args) {
|
||||
HandleScope scope;
|
||||
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
|
||||
SLICE_ARGS(args[0], args[1])
|
||||
const char *data = const_cast<char*>(parent->data() + start);
|
||||
Local<String> string = String::New(data, end - start);
|
||||
char *data = parent->data() + start;
|
||||
Local<String> string = String::New(reinterpret_cast<char*>(data), end - start);
|
||||
return scope.Close(string);
|
||||
}
|
||||
|
||||
@ -489,11 +522,11 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
|
||||
"Offset is out of bounds")));
|
||||
}
|
||||
|
||||
const char *p = buffer->data() + offset;
|
||||
char* p = buffer->data() + offset;
|
||||
|
||||
int char_written;
|
||||
|
||||
int written = s->WriteUtf8((char*)p,
|
||||
int written = s->WriteUtf8(reinterpret_cast<char*>(p),
|
||||
buffer->length_ - offset,
|
||||
&char_written,
|
||||
String::HINT_MANY_WRITES_EXPECTED);
|
||||
@ -527,11 +560,14 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
|
||||
"Offset is out of bounds")));
|
||||
}
|
||||
|
||||
const char *p = buffer->data() + offset;
|
||||
char *p = buffer->data() + offset;
|
||||
|
||||
size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset);
|
||||
|
||||
int written = s->WriteAscii((char*)p, 0, towrite, String::HINT_MANY_WRITES_EXPECTED);
|
||||
int written = s->WriteAscii(reinterpret_cast<char*>(p),
|
||||
0,
|
||||
towrite,
|
||||
String::HINT_MANY_WRITES_EXPECTED);
|
||||
return scope.Close(Integer::New(written));
|
||||
}
|
||||
|
||||
@ -574,7 +610,7 @@ Handle<Value> Buffer::Base64Write(const Arguments &args) {
|
||||
}
|
||||
|
||||
char a, b, c, d;
|
||||
char *dst = buffer->data();
|
||||
char* dst = buffer->data();
|
||||
const char *src = *s;
|
||||
const char *const srcEnd = src + s.length();
|
||||
|
||||
@ -726,6 +762,18 @@ Handle<Value> Buffer::MakeFastBuffer(const Arguments &args) {
|
||||
}
|
||||
|
||||
|
||||
bool Buffer::HasInstance(v8::Handle<v8::Value> val) {
|
||||
if (!val->IsObject()) return false;
|
||||
v8::Local<v8::Object> obj = val->ToObject();
|
||||
|
||||
if (obj->HasIndexedPropertiesInPixelData()) return true;
|
||||
|
||||
// Return true for "SlowBuffer"
|
||||
if (constructor_template->HasInstance(obj)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void Buffer::Initialize(Handle<Object> target) {
|
||||
HandleScope scope;
|
||||
|
@ -34,11 +34,10 @@ class Buffer : public ObjectWrap {
|
||||
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
static Buffer* New(size_t length); // public constructor
|
||||
static inline bool HasInstance(v8::Handle<v8::Value> val) {
|
||||
if (!val->IsObject()) return false;
|
||||
v8::Local<v8::Object> obj = val->ToObject();
|
||||
return constructor_template->HasInstance(obj);
|
||||
}
|
||||
static bool HasInstance(v8::Handle<v8::Value> val);
|
||||
|
||||
static char* Data(v8::Handle<v8::Object>);
|
||||
static size_t Length(v8::Handle<v8::Object>);
|
||||
|
||||
char* data();
|
||||
size_t length() const { return length_; }
|
||||
|
@ -626,24 +626,26 @@ static Handle<Value> Write(const Arguments& args) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Second argument needs to be a buffer")));
|
||||
}
|
||||
|
||||
Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t off = args[2]->Int32Value();
|
||||
if (off >= buffer->length()) {
|
||||
if (off >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset is out of bounds")));
|
||||
}
|
||||
|
||||
ssize_t len = args[3]->Int32Value();
|
||||
if (off + len > buffer->length()) {
|
||||
if (off + len > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Length is extends beyond buffer")));
|
||||
}
|
||||
|
||||
off_t pos = GET_OFFSET(args[4]);
|
||||
|
||||
char * buf = (char*)buffer->data() + off;
|
||||
char * buf = (char*)buffer_data + off;
|
||||
Local<Value> cb = args[5];
|
||||
|
||||
if (cb->IsFunction()) {
|
||||
@ -688,23 +690,25 @@ static Handle<Value> Read(const Arguments& args) {
|
||||
String::New("Second argument needs to be a buffer")));
|
||||
}
|
||||
|
||||
Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t off = args[2]->Int32Value();
|
||||
if (off >= buffer->length()) {
|
||||
if (off >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset is out of bounds")));
|
||||
}
|
||||
|
||||
len = args[3]->Int32Value();
|
||||
if (off + len > buffer->length()) {
|
||||
if (off + len > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Length is extends beyond buffer")));
|
||||
}
|
||||
|
||||
pos = GET_OFFSET(args[4]);
|
||||
|
||||
buf = (char*)buffer->data() + off;
|
||||
buf = buffer_data + off;
|
||||
|
||||
cb = args[5];
|
||||
|
||||
|
@ -538,21 +538,23 @@ static Handle<Value> Read(const Arguments& args) {
|
||||
String::New("Second argument should be a buffer")));
|
||||
}
|
||||
|
||||
Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t off = args[2]->Int32Value();
|
||||
if (off >= buffer->length()) {
|
||||
if (off >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset is out of bounds")));
|
||||
}
|
||||
|
||||
size_t len = args[3]->Int32Value();
|
||||
if (off + len > buffer->length()) {
|
||||
if (off + len > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Length is extends beyond buffer")));
|
||||
}
|
||||
|
||||
ssize_t bytes_read = read(fd, (char*)buffer->data() + off, len);
|
||||
ssize_t bytes_read = read(fd, (char*)buffer_data + off, len);
|
||||
|
||||
if (bytes_read < 0) {
|
||||
if (errno == EAGAIN || errno == EINTR) return Null();
|
||||
@ -583,16 +585,18 @@ static Handle<Value> RecvFrom(const Arguments& args) {
|
||||
String::New("Second argument should be a buffer")));
|
||||
}
|
||||
|
||||
Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t off = args[2]->Int32Value();
|
||||
if (off >= buffer->length()) {
|
||||
if (off >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset is out of bounds")));
|
||||
}
|
||||
|
||||
size_t len = args[3]->Int32Value();
|
||||
if (off + len > buffer->length()) {
|
||||
if (off + len > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Length is extends beyond buffer")));
|
||||
}
|
||||
@ -602,7 +606,7 @@ static Handle<Value> RecvFrom(const Arguments& args) {
|
||||
struct sockaddr_storage address_storage;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
|
||||
ssize_t bytes_read = recvfrom(fd, (char*)buffer->data() + off, len, flags,
|
||||
ssize_t bytes_read = recvfrom(fd, (char*)buffer_data + off, len, flags,
|
||||
(struct sockaddr*) &address_storage, &addrlen);
|
||||
|
||||
if (bytes_read < 0) {
|
||||
@ -639,22 +643,24 @@ static Handle<Value> RecvMsg(const Arguments& args) {
|
||||
String::New("Second argument should be a buffer")));
|
||||
}
|
||||
|
||||
Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t off = args[2]->Int32Value();
|
||||
if (off >= buffer->length()) {
|
||||
if (off >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset is out of bounds")));
|
||||
}
|
||||
|
||||
size_t len = args[3]->Int32Value();
|
||||
if (off + len > buffer->length()) {
|
||||
if (off + len > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Length is extends beyond buffer")));
|
||||
}
|
||||
|
||||
struct iovec iov[1];
|
||||
iov[0].iov_base = (char*)buffer->data() + off;
|
||||
iov[0].iov_base = (char*)buffer_data + off;
|
||||
iov[0].iov_len = len;
|
||||
|
||||
struct msghdr msg;
|
||||
@ -732,21 +738,23 @@ static Handle<Value> Write(const Arguments& args) {
|
||||
String::New("Second argument should be a buffer")));
|
||||
}
|
||||
|
||||
Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t off = args[2]->Int32Value();
|
||||
if (off >= buffer->length()) {
|
||||
if (off >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset is out of bounds")));
|
||||
}
|
||||
|
||||
size_t len = args[3]->Int32Value();
|
||||
if (off + len > buffer->length()) {
|
||||
if (off + len > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Length is extends beyond buffer")));
|
||||
}
|
||||
|
||||
ssize_t written = write(fd, (char*)buffer->data() + off, len);
|
||||
ssize_t written = write(fd, buffer_data + off, len);
|
||||
|
||||
if (written < 0) {
|
||||
if (errno == EAGAIN || errno == EINTR) {
|
||||
@ -790,7 +798,9 @@ static Handle<Value> SendMsg(const Arguments& args) {
|
||||
String::New("Expected either a string or a buffer")));
|
||||
}
|
||||
|
||||
Buffer *buf = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t offset = 0;
|
||||
if (args.Length() >= 3 && !args[2]->IsUndefined()) {
|
||||
@ -800,13 +810,13 @@ static Handle<Value> SendMsg(const Arguments& args) {
|
||||
}
|
||||
|
||||
offset = args[2]->Uint32Value();
|
||||
if (offset >= buf->length()) {
|
||||
if (offset >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset into buffer too large")));
|
||||
}
|
||||
}
|
||||
|
||||
size_t length = buf->length() - offset;
|
||||
size_t length = buffer_length - offset;
|
||||
if (args.Length() >= 4 && !args[3]->IsUndefined()) {
|
||||
if (!args[3]->IsUint32()) {
|
||||
return ThrowException(Exception::TypeError(
|
||||
@ -814,13 +824,13 @@ static Handle<Value> SendMsg(const Arguments& args) {
|
||||
}
|
||||
|
||||
length = args[3]->Uint32Value();
|
||||
if (offset + length > buf->length()) {
|
||||
if (offset + length > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("offset + length beyond buffer length")));
|
||||
}
|
||||
}
|
||||
|
||||
iov.iov_base = buf->data() + offset;
|
||||
iov.iov_base = buffer_data + offset;
|
||||
iov.iov_len = length;
|
||||
|
||||
int fd_to_send = -1;
|
||||
@ -911,7 +921,9 @@ static Handle<Value> SendTo(const Arguments& args) {
|
||||
String::New("Expected either a string or a buffer")));
|
||||
}
|
||||
|
||||
Buffer *buf = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject());
|
||||
Local<Object> buffer_obj = args[1]->ToObject();
|
||||
char *buffer_data = Buffer::Data(buffer_obj);
|
||||
size_t buffer_length = Buffer::Length(buffer_obj);
|
||||
|
||||
size_t offset = 0;
|
||||
if (args.Length() >= 3 && !args[2]->IsUndefined()) {
|
||||
@ -921,13 +933,13 @@ static Handle<Value> SendTo(const Arguments& args) {
|
||||
}
|
||||
|
||||
offset = args[2]->Uint32Value();
|
||||
if (offset >= buf->length()) {
|
||||
if (offset >= buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("Offset into buffer too large")));
|
||||
}
|
||||
}
|
||||
|
||||
size_t length = buf->length() - offset;
|
||||
size_t length = buffer_length - offset;
|
||||
if (args.Length() >= 4 && !args[3]->IsUndefined()) {
|
||||
if (!args[3]->IsUint32()) {
|
||||
return ThrowException(Exception::TypeError(
|
||||
@ -935,7 +947,7 @@ static Handle<Value> SendTo(const Arguments& args) {
|
||||
}
|
||||
|
||||
length = args[3]->Uint32Value();
|
||||
if (offset + length > buf->length()) {
|
||||
if (offset + length > buffer_length) {
|
||||
return ThrowException(Exception::Error(
|
||||
String::New("offset + length beyond buffer length")));
|
||||
}
|
||||
@ -954,7 +966,7 @@ static Handle<Value> SendTo(const Arguments& args) {
|
||||
Handle<Value> error = ParseAddressArgs(args[5], args[6], false);
|
||||
if (!error.IsEmpty()) return ThrowException(error);
|
||||
|
||||
ssize_t written = sendto(fd, buf->data() + offset, length, flags, addr, addrlen);
|
||||
ssize_t written = sendto(fd, buffer_data + offset, length, flags, addr, addrlen);
|
||||
|
||||
if (written < 0) {
|
||||
if (errno == EAGAIN || errno == EINTR) return Null();
|
||||
|
Loading…
x
Reference in New Issue
Block a user