Skip to content

Passing a pointer via the data argument #651

@sauleanf

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?

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