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:
Anna Henningsen authored and Node.js GitHub Bot committed 2026-06-05 12:53:43 +00:00
1 parent 3518f93c4b
commit 215027c8ed
1 file changed
+13 -2
+13 -2
View File
@@ -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_;
};