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

doc: update examples for context sensitivity #1013

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions doc/class_property_descriptor.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Example : public Napi::ObjectWrap<Example> {
Example(const Napi::CallbackInfo &info);

private:
static Napi::FunctionReference constructor;
double _value;
Napi::Value GetValue(const Napi::CallbackInfo &info);
void SetValue(const Napi::CallbackInfo &info, const Napi::Value &value);
Expand All @@ -31,8 +30,9 @@ Napi::Object Example::Init(Napi::Env env, Napi::Object exports) {
InstanceAccessor<&Example::GetValue>("readOnlyProp")
});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Napi::FunctionReference *constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);
Copy link
Member

Choose a reason for hiding this comment

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

A finalizer is required for this FunctionReference pointer right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @legendecas ,

Good question... I was under the impression that the instance data would be deleted, so there would be no need for a Finalizer.

Is my understanding correct?

Copy link
Member

Choose a reason for hiding this comment

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

Eh, IIUC, it's node-addon-api's default finalizer deleted the data pointer: https://github.com/nodejs/node-addon-api/blob/main/napi.h#L208. Not quite intuitive tho... So no finalizer is required here.

Copy link
Member

@legendecas legendecas Jun 22, 2021

Choose a reason for hiding this comment

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

The Delete you point out only deleting the RefBase structure allocated in Node.js core, which would invoke the finalizer provided by node-addon-api. (Update: no, the lines don't invoke finalizers as they are replacing existing instance data)

exports.Set("Example", func);

return exports;
Expand All @@ -45,8 +45,6 @@ Example::Example(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Example>(inf
this->_value = value.DoubleValue();
}

Napi::FunctionReference Example::constructor;

Napi::Value Example::GetValue(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
return Napi::Number::New(env, this->_value);
Expand Down