mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
src: add cleanup hooks to node::ObjectWrap
Add cleanup hooks to make sure that addons making use of this legacy API can do so safely in Worker threads or embedder-controlled Node.js instances. Refs: https://github.com/nodejs/node/pull/63575 Fixes: https://github.com/nodejs/node/issues/63540 Signed-off-by: Anna Henningsen <anna@addaleax.net> PR-URL: https://github.com/nodejs/node/pull/63642 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
1 parent
3518f93c4b
commit
215027c8ed
1 file changed
+13
-2
+13
-2
@@ -22,9 +22,8 @@
|
||||
#ifndef SRC_NODE_OBJECT_WRAP_H_
|
||||
#define SRC_NODE_OBJECT_WRAP_H_
|
||||
|
||||
#include "v8.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "node.h"
|
||||
|
||||
namespace node {
|
||||
|
||||
@@ -32,10 +31,12 @@ class ObjectWrap {
|
||||
public:
|
||||
ObjectWrap() {
|
||||
refs_ = 0;
|
||||
AddCleanupHook();
|
||||
}
|
||||
|
||||
|
||||
virtual ~ObjectWrap() {
|
||||
RemoveCleanupHook();
|
||||
if (persistent().IsEmpty())
|
||||
return;
|
||||
persistent().ClearWeak();
|
||||
@@ -125,6 +126,16 @@ class ObjectWrap {
|
||||
delete wrap;
|
||||
}
|
||||
|
||||
void AddCleanupHook() {
|
||||
AddEnvironmentCleanupHook(v8::Isolate::GetCurrent(), CleanupHook, this);
|
||||
}
|
||||
|
||||
void RemoveCleanupHook() {
|
||||
RemoveEnvironmentCleanupHook(v8::Isolate::GetCurrent(), CleanupHook, this);
|
||||
}
|
||||
|
||||
static void CleanupHook(void* arg) { delete static_cast<ObjectWrap*>(arg); }
|
||||
|
||||
// NOLINTNEXTLINE(runtime/v8_persistent)
|
||||
v8::Persistent<v8::Object> handle_;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user