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

how to get the Napi::Function constructor of an object? #897

Closed
sezanzeb opened this issue Feb 1, 2021 · 5 comments
Closed

how to get the Napi::Function constructor of an object? #897

sezanzeb opened this issue Feb 1, 2021 · 5 comments
Labels

Comments

@sezanzeb
Copy link

sezanzeb commented Feb 1, 2021

I can't figure out how to get the Napi::Function& constructor of an object. I would like to use InstanceOf (https://github.com/nodejs/node-addon-api/blob/main/doc/object.md#instanceof)

It looks like a constructor is extracted from an object using Napi::Type::Type, like in https://github.com/nodejs/node-addon-api/blob/main/doc/function.md#constructor, but when I use Napi::Date::Date, I get the following problem:

error: cannot convert ‘<unresolved overloaded function type>’ to ‘const Napi::Function&’
   59 |       bool is_date = value.ToObject().InstanceOf(Napi::Date::Date);
      |                            

I don't know much about c++ by the way

@KevinEady
Copy link
Contributor

There is no way to retrieve an object's constructor within Node API. You might be able to call Object.getPrototypeOf(), maybe like (untested):

auto objGlobal = env.Global().Get("Object");
auto getPrototypeOf = objGlobal.As<Object>().Get("getPrototypeOf").As<Function>();
auto prototype = getPrototypeOf.Call(objGlobal, { value }); // where `value` is the object whose prototype you want to receive

@sezanzeb
Copy link
Author

sezanzeb commented Feb 2, 2021

thanks, but I think I'm not trying to get an objects constructor, but rather the constructor for a given type.

How would you recommend to check if an object is of type Date? I'll try value.ToObject().Has('getTime') tomorrow

@NickNaso
Copy link
Member

NickNaso commented Feb 2, 2021

Hi @sezanzeb,
to check if a value is a JavaScript object you could use the Napi::Value::IsDate method see: https://github.com/nodejs/node-addon-api/blob/main/doc/value.md#isdate
Es.

Napi::Value Method(const Napi::CallbackInfo& info) {
  bool isDate = info[0].IsDate();
  return Napi::Boolean::New(env, isDate);
}

I hope tha this help you.

@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2021

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

@github-actions github-actions bot added the stale label May 4, 2021
@mhdawson
Copy link
Member

mhdawson commented May 6, 2021

Closing since there as been no follow up since Nick's response. Please let us know if that was not the right thing to do.

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

No branches or pull requests

4 participants