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

Add napi_finalize override to ObjectWrap #515

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 5 additions & 14 deletions doc/object_wrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,16 @@ property of the `Napi::CallbackInfo`.

Returns a `Napi::Function` representing the constructor function for the class.

### OverrideFinalizeCallback
### Finalize

Overrides default `Napi::ObjectWrap::FinalizeCallback` with a user defined finalizer.
Hooks into `Napi::ObjectWrap::FinalizeCallback` giving access to `Napi::Env`
mikepricedev marked this conversation as resolved.
Show resolved Hide resolved
before the native instance is freed.

```cpp
static void Napi::ObjectWrap::OverrideFinalizeCallback(T* instance,
napi_finalize finalizeCallback);
virtual void Finalize(Napi::Env env);
```

- `[in] instance`: `this` pointer from the native instance.
- `[in] finalizeCallback`: function that implements [napi_finalize](https://nodejs.org/api/n-api.html#n_api_napi_finalize "N-API documentation for napi_finalize.").

`Napi::ObjectWrap::OverrideFinalizeCallback` is protected and
intended to be called from a native instance method; for example, in the native
constructor.

NOTE: The default `Napi::ObjectWrap::FinalizeCallback` frees
the native instance. A user defined finalzier is, therefore,
responsible for freeing the native instance.
- `[in] env`: `Napi::Env`.

### StaticMethod

Expand Down
15 changes: 4 additions & 11 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3260,11 +3260,7 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
}

template <typename T>
inline void ObjectWrap<T>::OverrideFinalizeCallback(T* instance,
napi_finalize finalizeCallback) {
ObjectWrap<T>* base = instance;
base->_finalizeCallbackOverride = finalizeCallback;
}
inline void ObjectWrap<T>::Finalize(Napi::Env env) {}

template <typename T>
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
Expand Down Expand Up @@ -3407,13 +3403,10 @@ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
}

template <typename T>
inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void* data, void* hint) {
inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void* data, void* /*hint*/) {
T* instance = reinterpret_cast<T*>(data);
if(instance->_finalizeCallbackOverride == nullptr){
delete instance;
} else {
(*(instance->_finalizeCallbackOverride))(env, data, hint);
}
instance->Finalize(Napi::Env(env));
delete instance;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 1 addition & 3 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1657,10 +1657,8 @@ namespace Napi {
static PropertyDescriptor InstanceValue(Symbol name,
Napi::Value value,
napi_property_attributes attributes = napi_default);
protected:
static void OverrideFinalizeCallback(T* instance, napi_finalize finalizeCallback);
virtual void Finalize(Napi::Env env);
mikepricedev marked this conversation as resolved.
Show resolved Hide resolved
private:
napi_finalize _finalizeCallbackOverride = nullptr;
static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
static napi_value StaticVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
Expand Down