Created
May 19, 2012 23:05
-
-
Save drewish/2732711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Based off of http://sambro.is-super-awesome.com/2011/03/03/creating-a-proper-buffer-in-a-node-c-addon/ | |
*/ | |
static Local<Object> makeBuffer(char* data, size_t size) { | |
HandleScope scope; | |
// It ends up being kind of a pain to convert a slow buffer into a fast | |
// one since the fast part is implemented in JavaScript. | |
Local<Buffer> slowBuffer = Buffer::New(data, size); | |
// First get the Buffer from global scope... | |
Local<Object> global = Context::GetCurrent()->Global(); | |
Local<Value> bv = global->Get(String::NewSymbol("Buffer")); | |
assert(bv->IsFunction()); | |
Local<Function> b = Local<Function>::Cast(bv); | |
// ...call Buffer() with the slow buffer and get a fast buffer back... | |
Handle<Value> argv[3] = { slowBuffer->handle_, Integer::New(size), Integer::New(0) }; | |
Local<Object> fastBuffer = b->NewInstance(3, argv); | |
return scope.Close(fastBuffer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment