Add PersistentDictionary.

This commit is contained in:
Cheng Zhao 2014-09-09 14:12:59 +08:00
parent 742923c73d
commit 12f4e9b7ea
5 changed files with 88 additions and 9 deletions

View file

@ -19,9 +19,13 @@ Dictionary::Dictionary(v8::Isolate* isolate,
Dictionary::~Dictionary() {
}
v8::Handle<v8::Object> Dictionary::GetHandle() const {
return object_;
}
v8::Handle<v8::Value> Converter<Dictionary>::ToV8(v8::Isolate* isolate,
Dictionary val) {
return val.object_;
return val.GetHandle();
}
bool Converter<Dictionary>::FromV8(v8::Isolate* isolate,

View file

@ -30,30 +30,33 @@ class Dictionary {
template<typename T>
bool Get(const base::StringPiece& key, T* out) const {
v8::Handle<v8::Value> val = object_->Get(StringToV8(isolate_, key));
v8::Handle<v8::Value> val = GetHandle()->Get(StringToV8(isolate_, key));
return ConvertFromV8(isolate_, val, out);
}
template<typename T>
bool Set(const base::StringPiece& key, T val) {
return object_->Set(StringToV8(isolate_, key), ConvertToV8(isolate_, val));
return GetHandle()->Set(StringToV8(isolate_, key),
ConvertToV8(isolate_, val));
}
template<typename T>
bool SetMethod(const base::StringPiece& key, const T& callback) {
return object_->Set(
return GetHandle()->Set(
StringToV8(isolate_, key),
CallbackTraits<T>::CreateTemplate(isolate_, callback)->GetFunction());
}
v8::Handle<v8::Object> object() const { return object_; }
bool IsEmpty() const { return isolate() == NULL; }
virtual v8::Handle<v8::Object> GetHandle() const;
v8::Isolate* isolate() const { return isolate_; }
private:
friend struct Converter<Dictionary>;
// TODO(aa): Remove this. Instead, get via FromV8(), Set(), and Get().
protected:
v8::Isolate* isolate_;
private:
v8::Handle<v8::Object> object_;
};

View file

@ -0,0 +1,34 @@
// 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.
#include "native_mate/persistent_dictionary.h"
namespace mate {
PersistentDictionary::PersistentDictionary() {
}
PersistentDictionary::PersistentDictionary(v8::Isolate* isolate,
v8::Handle<v8::Object> object)
: handle_(new RefCountedPersistent<v8::Object>(isolate, object)) {
isolate_ = isolate;
}
PersistentDictionary::~PersistentDictionary() {
}
v8::Handle<v8::Object> PersistentDictionary::GetHandle() const {
return handle_->NewHandle();
}
bool Converter<PersistentDictionary>::FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
PersistentDictionary* out) {
if (!val->IsObject())
return false;
*out = PersistentDictionary(isolate, v8::Handle<v8::Object>::Cast(val));
return true;
}
} // namespace mate

View file

@ -0,0 +1,36 @@
// 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_PERSISTENT_DICTIONARY_H_
#define NATIVE_MATE_PERSISTENT_DICTIONARY_H_
#include "native_mate/dictionary.h"
#include "native_mate/scoped_persistent.h"
namespace mate {
// Like Dictionary, but stores object in persistent handle so you can keep it
// safely on heap.
class PersistentDictionary : public Dictionary {
public:
PersistentDictionary();
PersistentDictionary(v8::Isolate* isolate, v8::Handle<v8::Object> object);
virtual ~PersistentDictionary();
virtual v8::Handle<v8::Object> GetHandle() const OVERRIDE;
private:
scoped_refptr<RefCountedPersistent<v8::Object> > handle_;
};
template<>
struct Converter<PersistentDictionary> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
PersistentDictionary* out);
};
} // namespace mate
#endif // NATIVE_MATE_PERSISTENT_DICTIONARY_H_

View file

@ -17,6 +17,8 @@
'native_mate/locker.h',
'native_mate/object_template_builder.cc',
'native_mate/object_template_builder.h',
'native_mate/persistent_dictionary.cc',
'native_mate/persistent_dictionary.h',
'native_mate/scoped_persistent.h',
'native_mate/template_util.h',
'native_mate/try_catch.cc',