From 3d713baa4c33baebfac82b242e8897504592c590 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 21 Jul 2014 13:07:56 +0800 Subject: [PATCH] Enable converting ScopedPersistent. --- native_mate/scoped_persistent.h | 68 ++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/native_mate/scoped_persistent.h b/native_mate/scoped_persistent.h index c653a1040a9..4d7b0c07459 100644 --- a/native_mate/scoped_persistent.h +++ b/native_mate/scoped_persistent.h @@ -6,6 +6,7 @@ #define NATIVE_MATE_SCOPED_PERSISTENT_H_ #include "base/memory/ref_counted.h" +#include "native_mate/converter.h" #include "v8/include/v8.h" namespace mate { @@ -15,10 +16,11 @@ namespace mate { template class ScopedPersistent { public: - ScopedPersistent() { + ScopedPersistent(v8::Isolate* isolate = NULL) : isolate_(isolate) { } - explicit ScopedPersistent(v8::Handle handle) { + ScopedPersistent(v8::Handle handle, v8::Isolate* isolate = NULL) + : isolate_(isolate) { reset(v8::Handle::Cast(handle)); } @@ -27,10 +29,16 @@ class ScopedPersistent { } void reset(v8::Handle handle) { - if (!handle.IsEmpty()) - handle_.Reset(GetIsolate(handle), handle); - else + reset(GetIsolate(handle), handle); + } + + void reset(v8::Isolate* isolate, v8::Handle handle) { + if (!handle.IsEmpty()) { + isolate_ = isolate; + handle_.Reset(isolate, handle); + } else { reset(); + } } void reset() { @@ -42,9 +50,7 @@ class ScopedPersistent { } v8::Handle NewHandle() const { - if (handle_.IsEmpty()) - return v8::Local(); - return v8::Local::New(GetIsolate(handle_), handle_); + return NewHandle(GetIsolate(handle_)); } v8::Handle NewHandle(v8::Isolate* isolate) const { @@ -59,29 +65,38 @@ class ScopedPersistent { handle_.SetWeak(parameter, callback); } + v8::Isolate* isolate() const { return isolate_; } + private: template - static v8::Isolate* GetIsolate(v8::Handle object_handle) { + v8::Isolate* GetIsolate(v8::Handle object_handle) const { // Only works for v8::Object and its subclasses. Add specialisations for // anything else. if (!object_handle.IsEmpty()) return GetIsolate(object_handle->CreationContext()); - return v8::Isolate::GetCurrent(); + return GetIsolate(); } - static v8::Isolate* GetIsolate(v8::Handle context_handle) { + v8::Isolate* GetIsolate(v8::Handle context_handle) const { if (!context_handle.IsEmpty()) return context_handle->GetIsolate(); - return v8::Isolate::GetCurrent(); + return GetIsolate(); } - static v8::Isolate* GetIsolate( - v8::Handle template_handle) { - return v8::Isolate::GetCurrent(); + v8::Isolate* GetIsolate( + v8::Handle template_handle) const { + return GetIsolate(); } template - static v8::Isolate* GetIsolate(const U& any_handle) { - return v8::Isolate::GetCurrent(); + v8::Isolate* GetIsolate(const U& any_handle) const { + return GetIsolate(); + } + v8::Isolate* GetIsolate() const { + if (isolate_) + return isolate_; + else + return v8::Isolate::GetCurrent(); } + v8::Isolate* isolate_; v8::Persistent handle_; DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); @@ -106,6 +121,25 @@ class RefCountedPersistent : public ScopedPersistent, DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent); }; +template +struct Converter > { + static v8::Handle ToV8(v8::Isolate* isolate, + const ScopedPersistent& val) { + return val.NewHandle(isolate); + } + + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + ScopedPersistent* out) { + v8::Handle converted; + if (!Converter >::FromV8(isolate, val, &converted)) + return false; + + out->reset(isolate, converted); + return true; + } +}; + } // namespace mate #endif // NATIVE_MATE_SCOPED_PERSISTENT_H_