Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Create a public Buffer constructor for use in addons.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 24, 2010
1 parent c4876d0 commit 3768aaa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/node_buffer.cc
Expand Up @@ -119,6 +119,15 @@ class AsciiSliceExt: public String::ExternalAsciiStringResource {
};
#endif

Buffer* Buffer::New(size_t size) {
HandleScope scope;

Local<Value> arg = Integer::NewFromUnsigned(size);
Local<Object> b = constructor_template->GetFunction()->NewInstance(1, &arg);

return ObjectWrap::Unwrap<Buffer>(b);
}


Handle<Value> Buffer::New(const Arguments &args) {
HandleScope scope;
Expand Down
17 changes: 9 additions & 8 deletions src/node_buffer.h
Expand Up @@ -30,13 +30,10 @@ struct Blob_;

class Buffer : public ObjectWrap {
public:
static v8::Persistent<v8::FunctionTemplate> constructor_template;

Buffer(size_t length);
Buffer(Buffer *parent, size_t start, size_t end);
~Buffer();

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();
Expand All @@ -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<v8::FunctionTemplate> constructor_template;

static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> Slice(const v8::Arguments &args);
static v8::Handle<v8::Value> BinarySlice(const v8::Arguments &args);
Expand All @@ -60,10 +62,9 @@ class Buffer : public ObjectWrap {
static v8::Handle<v8::Value> Unpack(const v8::Arguments &args);
static v8::Handle<v8::Value> 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_;
Expand Down
1 change: 0 additions & 1 deletion src/node_object_wrap.h
Expand Up @@ -13,7 +13,6 @@ class ObjectWrap {
}

virtual ~ObjectWrap ( ) {
assert(handle_.IsNearDeath());
handle_->SetInternalField(0, v8::Undefined());
handle_.Dispose();
handle_.Clear();
Expand Down

0 comments on commit 3768aaa

Please sign in to comment.