@@ -2702,37 +2702,6 @@ inline Object FunctionReference::New(const std::vector<napi_value>& args) const
27022702// CallbackInfo class
27032703// //////////////////////////////////////////////////////////////////////////////
27042704
2705- class ObjectWrapConstructionContext {
2706- public:
2707- ObjectWrapConstructionContext (CallbackInfo* info) {
2708- info->_objectWrapConstructionContext = this ;
2709- }
2710-
2711- static inline void SetObjectWrapped (const CallbackInfo& info) {
2712- if (info._objectWrapConstructionContext == nullptr ) {
2713- Napi::Error::Fatal (" ObjectWrapConstructionContext::SetObjectWrapped" ,
2714- " _objectWrapConstructionContext is NULL" );
2715- }
2716- info._objectWrapConstructionContext ->_objectWrapped = true ;
2717- }
2718-
2719- inline void Cleanup (const CallbackInfo& info) {
2720- if (_objectWrapped) {
2721- napi_status status = napi_remove_wrap (info.Env (), info.This (), nullptr );
2722-
2723- // There's already a pending exception if we are at this point, so we have
2724- // no choice but to fatally fail here.
2725- NAPI_FATAL_IF_FAILED (status,
2726- " ObjectWrapConstructionContext::Cleanup" ,
2727- " Failed to remove wrap from unsuccessfully "
2728- " constructed ObjectWrap instance" );
2729- }
2730- }
2731-
2732- private:
2733- bool _objectWrapped = false ;
2734- };
2735-
27362705inline CallbackInfo::CallbackInfo (napi_env env, napi_callback_info info)
27372706 : _env(env), _info(info), _this(nullptr ), _dynamicArgs(nullptr ), _data(nullptr ) {
27382707 _argc = _staticArgCount;
@@ -3140,13 +3109,22 @@ inline ObjectWrap<T>::ObjectWrap(const Napi::CallbackInfo& callbackInfo) {
31403109 status = napi_wrap (env, wrapper, this , FinalizeCallback, nullptr , &ref);
31413110 NAPI_THROW_IF_FAILED_VOID (env, status);
31423111
3143- ObjectWrapConstructionContext::SetObjectWrapped (callbackInfo);
31443112 Reference<Object>* instanceRef = this ;
31453113 *instanceRef = Reference<Object>(env, ref);
31463114}
31473115
3148- template <typename T>
3149- inline ObjectWrap<T>::~ObjectWrap () {}
3116+ template <typename T>
3117+ inline ObjectWrap<T>::~ObjectWrap () {
3118+ // If the JS object still exists at this point, remove the finalizer added
3119+ // through `napi_wrap()`.
3120+ if (!IsEmpty ()) {
3121+ Object object = Value ();
3122+ // It is not valid to call `napi_remove_wrap()` with an empty `object`.
3123+ // This happens e.g. during garbage collection.
3124+ if (!object.IsEmpty ())
3125+ napi_remove_wrap (Env (), object, nullptr );
3126+ }
3127+ }
31503128
31513129template <typename T>
31523130inline T* ObjectWrap<T>::Unwrap(Object wrapper) {
@@ -3716,23 +3694,15 @@ inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
37163694
37173695 napi_value wrapper = details::WrapCallback ([&] {
37183696 CallbackInfo callbackInfo (env, info);
3719- ObjectWrapConstructionContext constructionContext (&callbackInfo);
37203697#ifdef NAPI_CPP_EXCEPTIONS
3721- try {
3722- new T (callbackInfo);
3723- } catch (const Error& e) {
3724- // Re-throw the error after removing the failed wrap.
3725- constructionContext.Cleanup (callbackInfo);
3726- throw e;
3727- }
3698+ new T (callbackInfo);
37283699#else
37293700 T* instance = new T (callbackInfo);
37303701 if (callbackInfo.Env ().IsExceptionPending ()) {
37313702 // We need to clear the exception so that removing the wrap might work.
37323703 Error e = callbackInfo.Env ().GetAndClearPendingException ();
3733- constructionContext.Cleanup (callbackInfo);
3734- e.ThrowAsJavaScriptException ();
37353704 delete instance;
3705+ e.ThrowAsJavaScriptException ();
37363706 }
37373707# endif // NAPI_CPP_EXCEPTIONS
37383708 return callbackInfo.This ();
@@ -3859,7 +3829,7 @@ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
38593829
38603830template <typename T>
38613831inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void * data, void * /* hint*/ ) {
3862- T * instance = reinterpret_cast <T *>(data);
3832+ ObjectWrap<T> * instance = static_cast <ObjectWrap<T> *>(data);
38633833 instance->Finalize (Napi::Env (env));
38643834 delete instance;
38653835}
0 commit comments