0

Passing an argument from Node.js to the C++ function, and returning the result to Node.js via a callback function.

Following the Node.js Addons Callback example, the example has been modified to involve a Number instead of a String.

Here's the example codeset from the Node.js Addons:

Local<Function> cb = Local<Function>::Cast(args[0]);
const unsigned argc = 1;
Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "hello world") };
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);

And here's the modified codeset, where value is a Number from a position in an Array:

Local<Function> cb = Local<Function>::Cast(args[1]);
const unsigned argc = 2;
Local<Value> argv[argc] = {Number::New(isolate, value)};
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
1
  • where is the segfault happening? Commented Jul 20, 2015 at 6:34

1 Answer 1

2

Your problem is that you define an array of size 2 for your arguments, you call the callback function passing arg count as 2 as well, but your initialize your array to a single value. Either change argc = 1, or initialize 2 values in the argv array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.