Hi,
I am writing a C++ module for node. I have a JS callback registered which will receive regular callbacks with binary data (audio data).
My C++ to call the callback looks a bit like:
UniquePersistent<v8::Function> dataCallback = // from somewhere...
HandleScope scope(isolate);
const unsigned argc = 1;
auto buffer = node::Buffer::New(dataSize);
std::memcpy(node::Buffer::Data(buffer), data.get(), dataSize);
Local<Value> argv[argc] = { buffer };
auto fn = Local<Function>::New(isolate, dataCallback);
auto context = isolate->GetCurrentContext();
auto global = context->Global();
fn->Call(global, argc, argv);
At the moment my callback is simply passing the buffer on to fs.writeFile(...):
var fs = require('fs');
mymodule.dosomething(function(data) {
fs.writeFile('raw_data', data, { flag: 'a' })
});
This appears to work, however the lifetime of the buffer object is suspect. This seems unsafe, as my understanding is that the Local<Buffer> is going to be cleared up when the HandleScope is destroyed after the callback has returned. Unfortunately, fs.writeFile() is probably still busy with the buffer.
The key would seem to be something to do with Persistent<> and UniquePersistent<> but I don't quite grok it.
Any tips, or pointers to examples would be great...