Skip to content

Commit 3768aaa

Browse files
committed
Create a public Buffer constructor for use in addons.
1 parent c4876d0 commit 3768aaa

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/node_buffer.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ class AsciiSliceExt: public String::ExternalAsciiStringResource {
119119
};
120120
#endif
121121

122+
Buffer* Buffer::New(size_t size) {
123+
HandleScope scope;
124+
125+
Local<Value> arg = Integer::NewFromUnsigned(size);
126+
Local<Object> b = constructor_template->GetFunction()->NewInstance(1, &arg);
127+
128+
return ObjectWrap::Unwrap<Buffer>(b);
129+
}
130+
122131

123132
Handle<Value> Buffer::New(const Arguments &args) {
124133
HandleScope scope;

src/node_buffer.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ struct Blob_;
3030

3131
class Buffer : public ObjectWrap {
3232
public:
33-
static v8::Persistent<v8::FunctionTemplate> constructor_template;
34-
35-
Buffer(size_t length);
36-
Buffer(Buffer *parent, size_t start, size_t end);
3733
~Buffer();
3834

3935
static void Initialize(v8::Handle<v8::Object> target);
36+
static Buffer* New(size_t length); // public constructor
4037
static inline bool HasInstance(v8::Handle<v8::Value> val) {
4138
if (!val->IsObject()) return false;
4239
v8::Local<v8::Object> obj = val->ToObject();
@@ -47,7 +44,12 @@ class Buffer : public ObjectWrap {
4744
size_t length() const { return length_; }
4845
struct Blob_* blob() const { return blob_; }
4946

50-
protected:
47+
int AsciiWrite(char *string, int offset, int length);
48+
int Utf8Write(char *string, int offset, int length);
49+
50+
private:
51+
static v8::Persistent<v8::FunctionTemplate> constructor_template;
52+
5153
static v8::Handle<v8::Value> New(const v8::Arguments &args);
5254
static v8::Handle<v8::Value> Slice(const v8::Arguments &args);
5355
static v8::Handle<v8::Value> BinarySlice(const v8::Arguments &args);
@@ -60,10 +62,9 @@ class Buffer : public ObjectWrap {
6062
static v8::Handle<v8::Value> Unpack(const v8::Arguments &args);
6163
static v8::Handle<v8::Value> Copy(const v8::Arguments &args);
6264

63-
int AsciiWrite(char *string, int offset, int length);
64-
int Utf8Write(char *string, int offset, int length);
65+
Buffer(size_t length);
66+
Buffer(Buffer *parent, size_t start, size_t end);
6567

66-
private:
6768
size_t off_; // offset inside blob_
6869
size_t length_; // length inside blob_
6970
struct Blob_ *blob_;

src/node_object_wrap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class ObjectWrap {
1313
}
1414

1515
virtual ~ObjectWrap ( ) {
16-
assert(handle_.IsNearDeath());
1716
handle_->SetInternalField(0, v8::Undefined());
1817
handle_.Dispose();
1918
handle_.Clear();

0 commit comments

Comments
 (0)