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

Fixed example in addon.md. #820

Merged
merged 3 commits into from
Oct 13, 2020
Merged

Fixed example in addon.md. #820

merged 3 commits into from
Oct 13, 2020

Conversation

nempoBu4
Copy link
Contributor

@nempoBu4 nempoBu4 commented Oct 8, 2020

No description provided.

InstanceValue("subObject", DefineProperties(Napi::Object::New(), {
InstanceMethod("decrement", &ExampleAddon::Decrement
})), napi_enumerable)
InstanceValue("subObject", DefineProperties(Napi::Object::New(env), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand adding the env to New() but not removing napi_enumerable as the signature seems like it requires that:

inline ClassPropertyDescriptor<T> InstanceWrap<T>::InstanceValue(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to InstanceWrap doc:

template <typename T>
static Napi::ClassPropertyDescriptor<T>
Napi::InstanceWrap<T>::InstanceValue(const char* utf8name,
                            Napi::Value value,
                            napi_property_attributes attributes = napi_default);

napi_enumerable can be set but it is not required. I have verified that this code works.

But if you think that napi_enumerable needs to be included into this example then it still needs to be fixed because of wrong position of brackets:

InstanceValue("subObject", DefineProperties(Napi::Object::New(env), {
    InstanceMethod("decrement", &ExampleAddon::Decrement) // <- missing bracket
})/*) <- wrong bracket */, napi_enumerable)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without napi_enumerable the items cannot be seen from JS without some effort. Please leave it in place! Everything else LGTM.

Copy link
Contributor Author

@nempoBu4 nempoBu4 Oct 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrielschulhof I thought napi_enumerable means this items can be seen through for..in iteration or Object.keys as stated in the MDN's Enumerability and ownership of properties article. I thought without napi_enumerable the property still can be accessed through dot (obj.property) or brackets (obj['property']) or destructuring assignment ({ property } = obj) and there is no harm to remove napi_enumerable because there is no for..in iteration in addon.md.

So, I was wrong. I put napi_enumerable back in place. Does it mean that napi_enumerable needs to be added to other items too?

DefineAddon(exports, {
  InstanceMethod("increment", &ExampleAddon::Increment, napi_enumerable), // <- here

  // We can also attach plain objects to `exports`, and instance methods as
  // properties of those sub-objects.
  InstanceValue("subObject", DefineProperties(Napi::Object::New(env), {
    InstanceMethod("decrement", &ExampleAddon::Decrement, napi_enumerable) // <- and here
  }), napi_enumerable)
});

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mhdawson mhdawson merged commit 826e466 into nodejs:master Oct 13, 2020
Superlokkus pushed a commit to Superlokkus/node-addon-api that referenced this pull request Nov 20, 2020
PR-URL: nodejs#820
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
PR-URL: nodejs/node-addon-api#820
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
PR-URL: nodejs/node-addon-api#820
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
PR-URL: nodejs/node-addon-api#820
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
PR-URL: nodejs/node-addon-api#820
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
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

Successfully merging this pull request may close these issues.

3 participants