From 85c655a91c4e140d5d60d1e9f8fc9c79599a29be Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Wed, 9 Nov 2016 17:46:26 -0800 Subject: [PATCH] fix for constructor destruction after timeout --- src/js_realm.hpp | 6 +++--- src/js_types.hpp | 5 +++++ src/jsc/jsc_protected.hpp | 7 +++++++ src/node/node_protected.hpp | 22 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 9634a431..a670224c 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -147,7 +147,7 @@ class RealmClass : public ClassDefinition> { public: static FunctionType create_constructor(ContextType); - static Protected s_constructor; + static Global s_constructor; // methods static void objects(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); @@ -258,7 +258,7 @@ public: }; template -Protected RealmClass::s_constructor; +Global RealmClass::s_constructor; template inline typename T::Function RealmClass::create_constructor(ContextType ctx) { @@ -279,7 +279,7 @@ inline typename T::Function RealmClass::create_constructor(ContextType ctx) { Object::set_property(ctx, realm_constructor, "Sync", sync_constructor, attributes); #endif - s_constructor = Protected(ctx, realm_constructor); + s_constructor = Global(ctx, realm_constructor); return realm_constructor; } diff --git a/src/js_types.hpp b/src/js_types.hpp index 6c2e78a1..7cd56b91 100644 --- a/src/js_types.hpp +++ b/src/js_types.hpp @@ -258,6 +258,11 @@ class Protected { }; }; +template +class Global { + operator ValueType() const; +}; + template struct Exception : public std::runtime_error { using ContextType = typename T::Context; diff --git a/src/jsc/jsc_protected.hpp b/src/jsc/jsc_protected.hpp index 10c25fa1..d569fc89 100644 --- a/src/jsc/jsc_protected.hpp +++ b/src/jsc/jsc_protected.hpp @@ -119,5 +119,12 @@ class Protected : public Protected { } }; +template<> +class Global : public Protected { + public: + Global() : Protected() {} + Global(JSContextRef ctx, JSObjectRef value) : Protected(ctx, value) {} +}; + } // js } // realm diff --git a/src/node/node_protected.hpp b/src/node/node_protected.hpp index c48cc027..97ac12da 100644 --- a/src/node/node_protected.hpp +++ b/src/node/node_protected.hpp @@ -94,5 +94,27 @@ class Protected : public node::Protected { Protected(v8::Isolate* isolate, v8::Local object) : node::Protected(object) {} }; +template +struct GlobalCopyablePersistentTraits { + typedef v8::Persistent> CopyablePersistent; + static const bool kResetInDestructor = false; + template + static inline void Copy(const v8::Persistent &source, CopyablePersistent *dest) {} +}; + +template<> +class Global { + // TODO: Figure out why Nan::CopyablePersistentTraits causes a build failure. + Nan::Persistent> m_value; + +public: + Global() {} + Global(v8::Isolate* isolate, v8::Local value) : m_value(value) {} + + operator v8::Local() const { + return Nan::New(m_value); + } +}; + } // js } // realm