Browse Source

Add C++ API for constructing fast buffer from string

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
dcc4fffe4d
  1. 16
      src/node_buffer.cc
  2. 3
      src/node_buffer.h

16
src/node_buffer.cc

@ -82,6 +82,22 @@ static size_t ByteLength (Handle<String> string, enum encoding enc) {
}
Handle<Object> Buffer::New(Handle<String> string) {
HandleScope scope;
// get Buffer from global scope.
Local<Object> global = v8::Context::GetCurrent()->Global();
Local<Value> bv = global->Get(String::NewSymbol("Buffer"));
assert(bv->IsFunction());
Local<Function> b = Local<Function>::Cast(bv);
Local<Value> argv[1] = { Local<Value>::New(string) };
Local<Object> instance = b->NewInstance(1, argv);
return scope.Close(instance);
}
Buffer* Buffer::New(size_t length) {
HandleScope scope;

3
src/node_buffer.h

@ -24,6 +24,9 @@ class Buffer : public ObjectWrap {
typedef void (*free_callback)(char *data, void *hint);
// C++ API for constructing fast buffer
static v8::Handle<v8::Object> New(v8::Handle<v8::String> string);
static void Initialize(v8::Handle<v8::Object> target);
static Buffer* New(size_t length); // public constructor
static Buffer* New(char *data, size_t len); // public constructor

Loading…
Cancel
Save