Skip to content

Commit 2ff776f

Browse files
author
Gabriel Schulhof
committed
backport node::Persistent
This reduces the delta in src/node_api.cc for the definition of the `CallbackBundle` structure back to zero wrt. upstream by copying the `node::Persistent` class template from upstream's src/node_persistent.h. PR-URL: #300 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 9816197 commit 2ff776f

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/node_api.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff 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.

src/node_internals.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ class CallbackScope {
7070

7171
namespace 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
7491
typedef int async_id;
7592

0 commit comments

Comments
 (0)