File tree Expand file tree Collapse file tree 2 files changed +20
-8
lines changed Expand file tree Collapse file tree 2 files changed +20
-8
lines changed Original file line number Diff line number Diff line change @@ -498,13 +498,7 @@ class TryCatch : public v8::TryCatch {
498
498
// calling through N-API.
499
499
// Ref: benchmark/misc/function_call
500
500
// Discussion (incl. perf. data): https://github.com/nodejs/node/pull/21072
501
- class CallbackBundle {
502
- public:
503
-
504
- ~CallbackBundle () {
505
- handle.ClearWeak ();
506
- handle.Reset ();
507
- }
501
+ struct CallbackBundle {
508
502
// Bind the lifecycle of `this` C++ object to a JavaScript object.
509
503
// We never delete a CallbackBundle C++ object directly.
510
504
void BindLifecycleTo (v8::Isolate* isolate, v8::Local<v8::Value> target) {
@@ -516,7 +510,8 @@ class CallbackBundle {
516
510
void * cb_data; // The user provided callback data
517
511
napi_callback function_or_getter;
518
512
napi_callback setter;
519
- v8::Persistent<v8::Value> handle; // Die with this JavaScript object
513
+ node::Persistent<v8::Value> handle; // Die with this JavaScript object
514
+
520
515
private:
521
516
static void WeakCallback (v8::WeakCallbackInfo<CallbackBundle> const & info) {
522
517
// Use the "WeakCallback mechanism" to delete the C++ `bundle` object.
Original file line number Diff line number Diff line change @@ -70,6 +70,23 @@ class CallbackScope {
70
70
71
71
namespace node {
72
72
73
+ // Copied from Node.js' src/node_persistent.h
74
+ template <typename T>
75
+ struct ResetInDestructorPersistentTraits {
76
+ static const bool kResetInDestructor = true ;
77
+ template <typename S, typename M>
78
+ // Disallow copy semantics by leaving this unimplemented.
79
+ inline static void Copy (
80
+ const v8::Persistent<S, M>&,
81
+ v8::Persistent<T, ResetInDestructorPersistentTraits<T>>*);
82
+ };
83
+
84
+ // v8::Persistent does not reset the object slot in its destructor. That is
85
+ // acknowledged as a flaw in the V8 API and expected to change in the future
86
+ // but for now node::Persistent is the easier and safer alternative.
87
+ template <typename T>
88
+ using Persistent = v8::Persistent<T, ResetInDestructorPersistentTraits<T>>;
89
+
73
90
#if NODE_MAJOR_VERSION < 8 || NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION < 2
74
91
typedef int async_id;
75
92
You can’t perform that action at this time.
0 commit comments