node-addon-examples
node-addon-examples copied to clipboard
Make callback with buffer as argument
Hello,
I'm having trouble making a callback and passing a Buffer as the argument.
Look at the code here:
v8::Local<v8::Function> callback;
void SetCallback(const Nan::FunctionCallbackInfo<v8::Value>& info) {
callback = info[0].As<v8::Function>();
}
void do_callback() {
int size = 100;
v8::MaybeLocal<v8::Object> obj = Nan::NewBuffer(size);
const unsigned argc = 1;
args[1] = { obj.ToLocalChecked() };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, args);
}
I always get "segmentation fault" and I do not know why..
To keep the code simple I am just intializing a new Buffer with size only..
However i would like to intialize the Buffer with data like so:
char* data =...
v8::MaybeLocal<v8::Object> obj = Nan::NewBuffer(data, size);
My goal is to callback a javascript function with a Buffer that is wrapped around an existing char pointer..
Please help.
Thank you!