2014-04-15 03:04:36 +00:00
|
|
|
// Copyright 2014 Cheng Zhao. All rights reserved.
|
|
|
|
// Use of this source code is governed by MIT license that can be found in the
|
|
|
|
// LICENSE file.
|
|
|
|
|
|
|
|
#ifndef NATIVE_MATE_SCOPED_PERSISTENT_H_
|
|
|
|
#define NATIVE_MATE_SCOPED_PERSISTENT_H_
|
|
|
|
|
|
|
|
#include "base/memory/ref_counted.h"
|
2014-07-21 05:07:56 +00:00
|
|
|
#include "native_mate/converter.h"
|
2014-04-15 03:04:36 +00:00
|
|
|
#include "v8/include/v8.h"
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
// A v8::Persistent handle to a V8 value which destroys and clears the
|
|
|
|
// underlying handle on destruction.
|
|
|
|
template <typename T>
|
|
|
|
class ScopedPersistent {
|
|
|
|
public:
|
2014-07-21 05:07:56 +00:00
|
|
|
ScopedPersistent(v8::Isolate* isolate = NULL) : isolate_(isolate) {
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
2014-07-21 05:07:56 +00:00
|
|
|
ScopedPersistent(v8::Handle<v8::Value> handle, v8::Isolate* isolate = NULL)
|
|
|
|
: isolate_(isolate) {
|
2014-04-18 08:45:44 +00:00
|
|
|
reset(v8::Handle<T>::Cast(handle));
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedPersistent() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(v8::Handle<T> handle) {
|
2014-07-21 05:07:56 +00:00
|
|
|
reset(GetIsolate(handle), handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(v8::Isolate* isolate, v8::Handle<T> handle) {
|
|
|
|
if (!handle.IsEmpty()) {
|
|
|
|
isolate_ = isolate;
|
2014-08-10 04:17:47 +00:00
|
|
|
MATE_PERSISTENT_ASSIGN(T, isolate, handle_, handle);
|
2014-07-21 05:07:56 +00:00
|
|
|
} else {
|
2014-04-15 03:04:36 +00:00
|
|
|
reset();
|
2014-07-21 05:07:56 +00:00
|
|
|
}
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
2014-08-10 04:17:47 +00:00
|
|
|
MATE_PERSISTENT_RESET(handle_);
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
return handle_.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<T> NewHandle() const {
|
2014-07-21 05:07:56 +00:00
|
|
|
return NewHandle(GetIsolate(handle_));
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
|
|
|
|
if (handle_.IsEmpty())
|
|
|
|
return v8::Local<T>();
|
|
|
|
return v8::Local<T>::New(isolate, handle_);
|
|
|
|
}
|
|
|
|
|
2014-08-10 04:17:47 +00:00
|
|
|
template<typename P, typename C>
|
|
|
|
void SetWeak(P* parameter, C callback) {
|
|
|
|
MATE_PERSISTENT_SET_WEAK(handle_, parameter, callback);
|
2014-06-28 13:26:34 +00:00
|
|
|
}
|
|
|
|
|
2014-07-21 05:07:56 +00:00
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
|
|
|
|
2014-04-15 03:04:36 +00:00
|
|
|
private:
|
|
|
|
template <typename U>
|
2014-07-21 05:07:56 +00:00
|
|
|
v8::Isolate* GetIsolate(v8::Handle<U> object_handle) const {
|
2014-04-15 03:04:36 +00:00
|
|
|
// Only works for v8::Object and its subclasses. Add specialisations for
|
|
|
|
// anything else.
|
|
|
|
if (!object_handle.IsEmpty())
|
|
|
|
return GetIsolate(object_handle->CreationContext());
|
2014-07-21 05:07:56 +00:00
|
|
|
return GetIsolate();
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
2014-08-10 04:17:47 +00:00
|
|
|
#if NODE_VERSION_AT_LEAST(0, 11, 0)
|
2014-07-21 05:07:56 +00:00
|
|
|
v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) const {
|
2014-04-15 03:04:36 +00:00
|
|
|
if (!context_handle.IsEmpty())
|
|
|
|
return context_handle->GetIsolate();
|
2014-07-21 05:07:56 +00:00
|
|
|
return GetIsolate();
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
2014-08-10 04:17:47 +00:00
|
|
|
#endif
|
2014-07-21 05:07:56 +00:00
|
|
|
v8::Isolate* GetIsolate(
|
|
|
|
v8::Handle<v8::ObjectTemplate> template_handle) const {
|
|
|
|
return GetIsolate();
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
template <typename U>
|
2014-07-21 05:07:56 +00:00
|
|
|
v8::Isolate* GetIsolate(const U& any_handle) const {
|
|
|
|
return GetIsolate();
|
|
|
|
}
|
|
|
|
v8::Isolate* GetIsolate() const {
|
|
|
|
if (isolate_)
|
|
|
|
return isolate_;
|
|
|
|
else
|
|
|
|
return v8::Isolate::GetCurrent();
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
2014-07-21 05:07:56 +00:00
|
|
|
v8::Isolate* isolate_;
|
2014-04-15 03:04:36 +00:00
|
|
|
v8::Persistent<T> handle_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScopedPersistent);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class RefCountedPersistent : public ScopedPersistent<T>,
|
|
|
|
public base::RefCounted<RefCountedPersistent<T>> {
|
|
|
|
public:
|
|
|
|
RefCountedPersistent() {}
|
|
|
|
|
2014-04-18 08:45:44 +00:00
|
|
|
explicit RefCountedPersistent(v8::Handle<v8::Value> handle)
|
2014-04-15 03:04:36 +00:00
|
|
|
: ScopedPersistent<T>(handle) {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
friend class base::RefCounted<RefCountedPersistent<T>>;
|
|
|
|
|
|
|
|
~RefCountedPersistent() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent);
|
|
|
|
};
|
|
|
|
|
2014-07-21 05:07:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-15 03:04:36 +00:00
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
#endif // NATIVE_MATE_SCOPED_PERSISTENT_H_
|