Make callback with buffer as argument

Open
#58 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
cpp, nodejs
Domain
api, backend

Research direction

The relevant entry points are SetCallback and do_callback in the issue snippet; start by checking the NAN/V8 callback and Buffer APIs and reproduce the segmentation fault. Done means the callback reliably receives a Buffer, including the intended existing char pointer case, without crashing.

Written by the indexing model from the issue text.

Description

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!

Dominant language
C++
Stars
2.6k
Forks
602
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nodejs/node-addon-examples

All issues in nodejs/node-addon-examples

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.