Skip to content

Commit

Permalink
Introduce NanGlobal
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Jul 4, 2015
1 parent 7fed696 commit 4408da1
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 5 deletions.
71 changes: 71 additions & 0 deletions nan_persistent_12_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,75 @@ template<typename T, typename M> class NanPersistent :
}
};

#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
template<typename T>
class NanGlobal : public v8::Global<T> {
public:
NAN_INLINE NanGlobal() : v8::Global<T>() {}

template<typename S> NAN_INLINE NanGlobal(v8::Handle<S> that) :
v8::Global<T>(v8::Isolate::GetCurrent(), that) {}

template<typename S>
NAN_INLINE NanGlobal(const v8::PersistentBase<S> &that) :
v8::Global<S>(v8::Isolate::GetCurrent(), that) {}

NAN_INLINE void Reset() { v8::PersistentBase<T>::Reset(); }

template <typename S>
NAN_INLINE void Reset(const v8::Handle<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}

template <typename S>
NAN_INLINE void Reset(const v8::PersistentBase<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}

template<typename P>
NAN_INLINE void SetWeak(
P *parameter
, typename NanWeakCallbackInfo<P>::Callback callback
, NanWeakCallbackType type) {
reinterpret_cast<NanPersistent<T>*>(this)->SetWeak(
parameter, callback, type);
}
};
#else
template<typename T>
class NanGlobal : public v8::UniquePersistent<T> {
public:
NAN_INLINE NanGlobal() : v8::UniquePersistent<T>() {}

template<typename S> NAN_INLINE NanGlobal(v8::Handle<S> that) :
v8::UniquePersistent<T>(v8::Isolate::GetCurrent(), that) {}

template<typename S>
NAN_INLINE NanGlobal(const v8::PersistentBase<S> &that) :
v8::UniquePersistent<S>(v8::Isolate::GetCurrent(), that) {}

NAN_INLINE void Reset() { v8::PersistentBase<T>::Reset(); }

template <typename S>
NAN_INLINE void Reset(const v8::Handle<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}

template <typename S>
NAN_INLINE void Reset(const v8::PersistentBase<S> &other) {
v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
}

template<typename P>
NAN_INLINE void SetWeak(
P *parameter
, typename NanWeakCallbackInfo<P>::Callback callback
, NanWeakCallbackType type) {
reinterpret_cast<NanPersistent<T>*>(this)->SetWeak(
parameter, callback, type);
}
};
#endif

#endif // NAN_PERSISTENT_12_INL_H_
57 changes: 57 additions & 0 deletions nan_persistent_pre_12_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NanPersistentBase {
v8::Persistent<T> persistent;
template<typename U, typename M>
friend v8::Local<U> NanNew(const NanPersistent<U, M> &p);
template<typename S> friend class NanReturnValue;

public:
NAN_INLINE NanPersistentBase() :
Expand Down Expand Up @@ -97,7 +98,9 @@ class NanPersistentBase {
private:
NAN_INLINE explicit NanPersistentBase(v8::Persistent<T> that) :
persistent(that) { }
NAN_INLINE explicit NanPersistentBase(T *val) : persistent(val) {}
template<typename S, typename M> friend class NanPersistent;
template<typename S> friend class NanGlobal;
};

template<typename T>
Expand Down Expand Up @@ -177,4 +180,58 @@ template<typename T, typename M> class NanPersistent :
}
};

template<typename T>
class NanGlobal : public NanPersistentBase<T> {
struct RValue {
NAN_INLINE explicit RValue(NanGlobal* obj) : object(obj) {}
NanGlobal* object;
};

public:
NAN_INLINE NanGlobal() : NanPersistentBase<T>(0) { }

template <typename S>
NAN_INLINE NanGlobal(v8::Handle<S> that)
: NanPersistentBase<T>(v8::Persistent<T>::New(that)) {
TYPE_CHECK(T, S);
}

template <typename S>
NAN_INLINE NanGlobal(const NanPersistentBase<S> &that)
: NanPersistentBase<T>(that) {
TYPE_CHECK(T, S);
}
/**
* Move constructor.
*/
NAN_INLINE NanGlobal(RValue rvalue)
: NanPersistentBase<T>(rvalue.object.persistent) {
rvalue.object->Reset();
}
NAN_INLINE ~NanGlobal() { this->Reset(); }
/**
* Move via assignment.
*/
template<typename S>
NAN_INLINE NanGlobal &operator=(NanGlobal<S> rhs) {
TYPE_CHECK(T, S);
this->Reset(rhs.persistent);
rhs.Reset();
return *this;
}
/**
* Cast operator for moves.
*/
NAN_INLINE operator RValue() { return RValue(this); }
/**
* Pass allows returning uniques from functions, etc.
*/
NanGlobal Pass() { return NanGlobal(RValue(this)); }

private:
NanGlobal(NanGlobal &);
void operator=(NanGlobal &);
template<typename S> friend class NanReturnValue;
};

#endif // NAN_PERSISTENT_PRE_12_INL_H_
10 changes: 5 additions & 5 deletions test/cpp/returnvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <nan.h>

NanPersistent<v8::Boolean> persistent;
NanGlobal<v8::Boolean> global;

NAN_METHOD(ReturnValue) {
if (args.Length() == 1) {
Expand All @@ -26,12 +26,12 @@ NAN_METHOD(ReturnString) {
NanReturnValue("yes, it works");
}

NAN_METHOD(ReturnPersistent) {
NanReturnValue(persistent);
NAN_METHOD(ReturnGlobal) {
NanReturnValue(global);
}

void Init (v8::Handle<v8::Object> target) {
persistent.Reset(NanNew(true));
global.Reset(NanNew(true));

target->Set(
NanNew<v8::String>("r")
Expand All @@ -47,7 +47,7 @@ void Init (v8::Handle<v8::Object> target) {
);
target->Set(
NanNew<v8::String>("q")
, NanNew<v8::FunctionTemplate>(ReturnPersistent)->GetFunction()
, NanNew<v8::FunctionTemplate>(ReturnGlobal)->GetFunction()
);
}

Expand Down

0 comments on commit 4408da1

Please sign in to comment.