From dcc4fffe4dc7ccec3db2d1b55ab3a42a492a0c98 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 26 Oct 2010 13:43:58 -0700 Subject: [PATCH] Add C++ API for constructing fast buffer from string --- src/node_buffer.cc | 16 ++++++++++++++++ src/node_buffer.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index bac41cf7810..8d7fc6dc91e 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -82,6 +82,22 @@ static size_t ByteLength (Handle string, enum encoding enc) { } +Handle Buffer::New(Handle string) { + HandleScope scope; + + // get Buffer from global scope. + Local global = v8::Context::GetCurrent()->Global(); + Local bv = global->Get(String::NewSymbol("Buffer")); + assert(bv->IsFunction()); + Local b = Local::Cast(bv); + + Local argv[1] = { Local::New(string) }; + Local instance = b->NewInstance(1, argv); + + return scope.Close(instance); +} + + Buffer* Buffer::New(size_t length) { HandleScope scope; diff --git a/src/node_buffer.h b/src/node_buffer.h index 3221417f2f3..47b9961e454 100644 --- a/src/node_buffer.h +++ b/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 New(v8::Handle string); + static void Initialize(v8::Handle target); static Buffer* New(size_t length); // public constructor static Buffer* New(char *data, size_t len); // public constructor