Skip to content

Instantly share code, notes, and snippets.

@drewish
Created May 19, 2012 23:05
Show Gist options
  • Save drewish/2732711 to your computer and use it in GitHub Desktop.
Save drewish/2732711 to your computer and use it in GitHub Desktop.
/*
* 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