fix for constructor destruction after timeout

This commit is contained in:
Ari Lazier 2016-11-09 17:46:26 -08:00
parent 4fc874f35c
commit 85c655a91c
4 changed files with 37 additions and 3 deletions

View File

@ -147,7 +147,7 @@ class RealmClass : public ClassDefinition<T, SharedRealm, ObservableClass<T>> {
public:
static FunctionType create_constructor(ContextType);
static Protected<FunctionType> s_constructor;
static Global<FunctionType> s_constructor;
// methods
static void objects(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
@ -258,7 +258,7 @@ public:
};
template<typename T>
Protected<typename T::Function> RealmClass<T>::s_constructor;
Global<typename T::Function> RealmClass<T>::s_constructor;
template<typename T>
inline typename T::Function RealmClass<T>::create_constructor(ContextType ctx) {
@ -279,7 +279,7 @@ inline typename T::Function RealmClass<T>::create_constructor(ContextType ctx) {
Object::set_property(ctx, realm_constructor, "Sync", sync_constructor, attributes);
#endif
s_constructor = Protected<FunctionType>(ctx, realm_constructor);
s_constructor = Global<FunctionType>(ctx, realm_constructor);
return realm_constructor;
}

View File

@ -258,6 +258,11 @@ class Protected {
};
};
template<typename ValueType>
class Global {
operator ValueType() const;
};
template<typename T>
struct Exception : public std::runtime_error {
using ContextType = typename T::Context;

View File

@ -119,5 +119,12 @@ class Protected<JSObjectRef> : public Protected<JSValueRef> {
}
};
template<>
class Global<JSObjectRef> : public Protected<JSObjectRef> {
public:
Global() : Protected<JSObjectRef>() {}
Global(JSContextRef ctx, JSObjectRef value) : Protected<JSObjectRef>(ctx, value) {}
};
} // js
} // realm

View File

@ -94,5 +94,27 @@ class Protected<node::Types::Function> : public node::Protected<v8::Function> {
Protected(v8::Isolate* isolate, v8::Local<v8::Function> object) : node::Protected<v8::Function>(object) {}
};
template<typename T>
struct GlobalCopyablePersistentTraits {
typedef v8::Persistent<T, GlobalCopyablePersistentTraits<T>> CopyablePersistent;
static const bool kResetInDestructor = false;
template<typename S, typename M>
static inline void Copy(const v8::Persistent<S, M> &source, CopyablePersistent *dest) {}
};
template<>
class Global<node::Types::Function> {
// TODO: Figure out why Nan::CopyablePersistentTraits causes a build failure.
Nan::Persistent<v8::Function, GlobalCopyablePersistentTraits<v8::Function>> m_value;
public:
Global() {}
Global(v8::Isolate* isolate, v8::Local<v8::Function> value) : m_value(value) {}
operator v8::Local<v8::Function>() const {
return Nan::New(m_value);
}
};
} // js
} // realm