From 3768aaaea406dd32db2f74395f6414bcd26dba74 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 24 May 2010 12:59:22 -0700 Subject: [PATCH] Create a public Buffer constructor for use in addons. --- src/node_buffer.cc | 9 +++++++++ src/node_buffer.h | 17 +++++++++-------- src/node_object_wrap.h | 1 - 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 5d5a389fb1e..5682f93791b 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -119,6 +119,15 @@ class AsciiSliceExt: public String::ExternalAsciiStringResource { }; #endif +Buffer* Buffer::New(size_t size) { + HandleScope scope; + + Local arg = Integer::NewFromUnsigned(size); + Local b = constructor_template->GetFunction()->NewInstance(1, &arg); + + return ObjectWrap::Unwrap(b); +} + Handle Buffer::New(const Arguments &args) { HandleScope scope; diff --git a/src/node_buffer.h b/src/node_buffer.h index d7c849a7baa..dd9d0db51cf 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -30,13 +30,10 @@ struct Blob_; class Buffer : public ObjectWrap { public: - static v8::Persistent constructor_template; - - Buffer(size_t length); - Buffer(Buffer *parent, size_t start, size_t end); ~Buffer(); static void Initialize(v8::Handle target); + static Buffer* New(size_t length); // public constructor static inline bool HasInstance(v8::Handle val) { if (!val->IsObject()) return false; v8::Local obj = val->ToObject(); @@ -47,7 +44,12 @@ class Buffer : public ObjectWrap { size_t length() const { return length_; } struct Blob_* blob() const { return blob_; } - protected: + int AsciiWrite(char *string, int offset, int length); + int Utf8Write(char *string, int offset, int length); + + private: + static v8::Persistent constructor_template; + static v8::Handle New(const v8::Arguments &args); static v8::Handle Slice(const v8::Arguments &args); static v8::Handle BinarySlice(const v8::Arguments &args); @@ -60,10 +62,9 @@ class Buffer : public ObjectWrap { static v8::Handle Unpack(const v8::Arguments &args); static v8::Handle Copy(const v8::Arguments &args); - int AsciiWrite(char *string, int offset, int length); - int Utf8Write(char *string, int offset, int length); + Buffer(size_t length); + Buffer(Buffer *parent, size_t start, size_t end); - private: size_t off_; // offset inside blob_ size_t length_; // length inside blob_ struct Blob_ *blob_; diff --git a/src/node_object_wrap.h b/src/node_object_wrap.h index 7e26f25b0dd..534567b2e76 100644 --- a/src/node_object_wrap.h +++ b/src/node_object_wrap.h @@ -13,7 +13,6 @@ class ObjectWrap { } virtual ~ObjectWrap ( ) { - assert(handle_.IsNearDeath()); handle_->SetInternalField(0, v8::Undefined()); handle_.Dispose(); handle_.Clear();