Add C++ API for constructing fast buffer from string

This commit is contained in:
Ryan Dahl 2010-10-26 13:43:58 -07:00
parent d1e5fbdde4
commit dcc4fffe4d
2 changed files with 19 additions and 0 deletions

View File

@ -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;

View File

@ -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