Enable converting ScopedPersistent.
This commit is contained in:
parent
14c851e9f8
commit
3d713baa4c
1 changed files with 51 additions and 17 deletions
|
@ -6,6 +6,7 @@
|
||||||
#define NATIVE_MATE_SCOPED_PERSISTENT_H_
|
#define NATIVE_MATE_SCOPED_PERSISTENT_H_
|
||||||
|
|
||||||
#include "base/memory/ref_counted.h"
|
#include "base/memory/ref_counted.h"
|
||||||
|
#include "native_mate/converter.h"
|
||||||
#include "v8/include/v8.h"
|
#include "v8/include/v8.h"
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
@ -15,10 +16,11 @@ namespace mate {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class ScopedPersistent {
|
class ScopedPersistent {
|
||||||
public:
|
public:
|
||||||
ScopedPersistent() {
|
ScopedPersistent(v8::Isolate* isolate = NULL) : isolate_(isolate) {
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit ScopedPersistent(v8::Handle<v8::Value> handle) {
|
ScopedPersistent(v8::Handle<v8::Value> handle, v8::Isolate* isolate = NULL)
|
||||||
|
: isolate_(isolate) {
|
||||||
reset(v8::Handle<T>::Cast(handle));
|
reset(v8::Handle<T>::Cast(handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,10 +29,16 @@ class ScopedPersistent {
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset(v8::Handle<T> handle) {
|
void reset(v8::Handle<T> handle) {
|
||||||
if (!handle.IsEmpty())
|
reset(GetIsolate(handle), handle);
|
||||||
handle_.Reset(GetIsolate(handle), handle);
|
}
|
||||||
else
|
|
||||||
|
void reset(v8::Isolate* isolate, v8::Handle<T> handle) {
|
||||||
|
if (!handle.IsEmpty()) {
|
||||||
|
isolate_ = isolate;
|
||||||
|
handle_.Reset(isolate, handle);
|
||||||
|
} else {
|
||||||
reset();
|
reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
|
@ -42,9 +50,7 @@ class ScopedPersistent {
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Handle<T> NewHandle() const {
|
v8::Handle<T> NewHandle() const {
|
||||||
if (handle_.IsEmpty())
|
return NewHandle(GetIsolate(handle_));
|
||||||
return v8::Local<T>();
|
|
||||||
return v8::Local<T>::New(GetIsolate(handle_), handle_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
|
v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
|
||||||
|
@ -59,29 +65,38 @@ class ScopedPersistent {
|
||||||
handle_.SetWeak(parameter, callback);
|
handle_.SetWeak(parameter, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Isolate* isolate() const { return isolate_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename U>
|
template <typename U>
|
||||||
static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) {
|
v8::Isolate* GetIsolate(v8::Handle<U> object_handle) const {
|
||||||
// Only works for v8::Object and its subclasses. Add specialisations for
|
// Only works for v8::Object and its subclasses. Add specialisations for
|
||||||
// anything else.
|
// anything else.
|
||||||
if (!object_handle.IsEmpty())
|
if (!object_handle.IsEmpty())
|
||||||
return GetIsolate(object_handle->CreationContext());
|
return GetIsolate(object_handle->CreationContext());
|
||||||
return v8::Isolate::GetCurrent();
|
return GetIsolate();
|
||||||
}
|
}
|
||||||
static v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) {
|
v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) const {
|
||||||
if (!context_handle.IsEmpty())
|
if (!context_handle.IsEmpty())
|
||||||
return context_handle->GetIsolate();
|
return context_handle->GetIsolate();
|
||||||
return v8::Isolate::GetCurrent();
|
return GetIsolate();
|
||||||
}
|
}
|
||||||
static v8::Isolate* GetIsolate(
|
v8::Isolate* GetIsolate(
|
||||||
v8::Handle<v8::ObjectTemplate> template_handle) {
|
v8::Handle<v8::ObjectTemplate> template_handle) const {
|
||||||
return v8::Isolate::GetCurrent();
|
return GetIsolate();
|
||||||
}
|
}
|
||||||
template <typename U>
|
template <typename U>
|
||||||
static v8::Isolate* GetIsolate(const U& any_handle) {
|
v8::Isolate* GetIsolate(const U& any_handle) const {
|
||||||
return v8::Isolate::GetCurrent();
|
return GetIsolate();
|
||||||
|
}
|
||||||
|
v8::Isolate* GetIsolate() const {
|
||||||
|
if (isolate_)
|
||||||
|
return isolate_;
|
||||||
|
else
|
||||||
|
return v8::Isolate::GetCurrent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Isolate* isolate_;
|
||||||
v8::Persistent<T> handle_;
|
v8::Persistent<T> handle_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(ScopedPersistent);
|
DISALLOW_COPY_AND_ASSIGN(ScopedPersistent);
|
||||||
|
@ -106,6 +121,25 @@ class RefCountedPersistent : public ScopedPersistent<T>,
|
||||||
DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent);
|
DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Converter<ScopedPersistent<T> > {
|
||||||
|
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
|
const ScopedPersistent<T>& val) {
|
||||||
|
return val.NewHandle(isolate);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Handle<v8::Value> val,
|
||||||
|
ScopedPersistent<T>* out) {
|
||||||
|
v8::Handle<T> converted;
|
||||||
|
if (!Converter<v8::Handle<T> >::FromV8(isolate, val, &converted))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
out->reset(isolate, converted);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
||||||
#endif // NATIVE_MATE_SCOPED_PERSISTENT_H_
|
#endif // NATIVE_MATE_SCOPED_PERSISTENT_H_
|
||||||
|
|
Loading…
Reference in a new issue