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 {
498498// calling through N-API.
499499// Ref: benchmark/misc/function_call
500500// 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 {
508502 // Bind the lifecycle of `this` C++ object to a JavaScript object.
509503 // We never delete a CallbackBundle C++ object directly.
510504 void BindLifecycleTo (v8::Isolate* isolate, v8::Local<v8::Value> target) {
@@ -516,7 +510,8 @@ class CallbackBundle {
516510 void * cb_data; // The user provided callback data
517511 napi_callback function_or_getter;
518512 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+
520515 private:
521516 static void WeakCallback (v8::WeakCallbackInfo<CallbackBundle> const & info) {
522517 // 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 {
7070
7171namespace node {
7272
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+
7390#if NODE_MAJOR_VERSION < 8 || NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION < 2
7491typedef int async_id;
7592
You can’t perform that action at this time.
0 commit comments