Skip to content

Passing a pointer via the data argument #651

Closed

Description

@sauleanf

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?

Activity

  1. KevinEady commented on Jan 18, 2020

    @KevinEady
    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?

  2. alexisfrjp commented on Feb 3, 2020

    @alexisfrjp

    Can be done using External, example: #584

  3. gabrielschulhof commented on Feb 3, 2020

    @gabrielschulhof
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions