Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a pointer via the data argument #651

Closed
sauleanf opened this issue Jan 18, 2020 · 3 comments
Closed

Passing a pointer via the data argument #651

sauleanf opened this issue Jan 18, 2020 · 3 comments

Comments

@sauleanf
Copy link

I have been messing around with objects and functions.

Napi::Value position(const Napi::CallbackInfo& info) {
  float* a = (float*) info.Data();
  return Napi::Number::New(info.Env(), a);
}

void myClass::getStuff(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();

  float* a = new float(As<Napi::Number>().FloatValue());
  Napi::Function cb = info[3].As<Napi::Function>();

  auto function1 = Napi::PropertyDescriptor::Function("function1", function1, napi_default, a);
 
  Napi::Object obj = Napi::Object::New(env);
  obj.DefineProperties({ function1 });

  cb.Call(env.Global(), { obj });
}

When I try to call function1 in javascript, I get nothing. I was able to verify that (int*) info.Data() returns nullptr even though I passed a pointer to an integer on the heap. Am I doing this wrong? How should I approach this?

@KevinEady
Copy link
Contributor

Hi @f-s-plus-plus ,

I noticed that our CI tests do not include a test for a function property descriptor with user data. I have since created a PR to add one. Take a look at the files changed, maybe that will help?

@alexisfrjp
Copy link

Can be done using External, example: #584

@gabrielschulhof
Copy link
Contributor

@sauleanf please have a look at #652, which adds a test to ensure that passing external data to a binding works correctly. The test can also serve as an example of how to do that. If you are still having trouble, please feel free to re-open this issue or open a new one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants