@@ -55,17 +55,14 @@ struct napi_env__ { |
55 | 55 | CHECK_EQ(isolate, context->GetIsolate()); |
56 | 56 | napi_clear_last_error(this); |
57 | 57 | } |
58 | | - virtual ~napi_env__() { FinalizeAll(); } |
59 | | - v8::Isolate* const isolate; // Shortcut for context()->GetIsolate() |
60 | | - v8impl::Persistent<v8::Context> context_persistent; |
61 | 58 |
|
62 | 59 | inline v8::Local<v8::Context> context() const { |
63 | 60 | return v8impl::PersistentToLocal::Strong(context_persistent); |
64 | 61 | } |
65 | 62 |
|
66 | 63 | inline void Ref() { refs++; } |
67 | 64 | inline void Unref() { |
68 | | - if (--refs == 0) delete this; |
| 65 | + if (--refs == 0) DeleteMe(); |
69 | 66 | } |
70 | 67 |
|
71 | 68 | virtual bool can_call_into_js() const { return true; } |
@@ -99,16 +96,20 @@ struct napi_env__ { |
99 | 96 | CallIntoModule([&](napi_env env) { cb(env, data, hint); }); |
100 | 97 | } |
101 | 98 |
|
102 | | - void FinalizeAll() { |
| 99 | + virtual void DeleteMe() { |
103 | 100 | // First we must finalize those references that have `napi_finalizer` |
104 | 101 | // callbacks. The reason is that addons might store other references which |
105 | 102 | // they delete during their `napi_finalizer` callbacks. If we deleted such |
106 | 103 | // references here first, they would be doubly deleted when the |
107 | 104 | // `napi_finalizer` deleted them subsequently. |
108 | 105 | v8impl::RefTracker::FinalizeAll(&finalizing_reflist); |
109 | 106 | v8impl::RefTracker::FinalizeAll(&reflist); |
| 107 | + delete this; |
110 | 108 | } |
111 | 109 |
|
| 110 | + v8::Isolate* const isolate; // Shortcut for context()->GetIsolate() |
| 111 | + v8impl::Persistent<v8::Context> context_persistent; |
| 112 | + |
112 | 113 | v8impl::Persistent<v8::Value> last_exception; |
113 | 114 |
|
114 | 115 | // We store references in two different lists, depending on whether they have |
@@ -121,6 +122,11 @@ struct napi_env__ { |
121 | 122 | int open_callback_scopes = 0; |
122 | 123 | int refs = 1; |
123 | 124 | void* instance_data = nullptr; |
| 125 | + |
| 126 | + protected: |
| 127 | + // Should not be deleted directly. Delete with `napi_env__::DeleteMe()` |
| 128 | + // instead. |
| 129 | + virtual ~napi_env__() = default; |
124 | 130 | }; |
125 | 131 |
|
126 | 132 | // This class is used to keep a napi_env live in a way that |
|