diff --git a/native_mate/dictionary.cc b/native_mate/dictionary.cc index 168e5e75833..6c154eaad45 100644 --- a/native_mate/dictionary.cc +++ b/native_mate/dictionary.cc @@ -19,9 +19,13 @@ Dictionary::Dictionary(v8::Isolate* isolate, Dictionary::~Dictionary() { } +v8::Handle Dictionary::GetHandle() const { + return object_; +} + v8::Handle Converter::ToV8(v8::Isolate* isolate, Dictionary val) { - return val.object_; + return val.GetHandle(); } bool Converter::FromV8(v8::Isolate* isolate, diff --git a/native_mate/dictionary.h b/native_mate/dictionary.h index f901e7e27dc..fab215e2126 100644 --- a/native_mate/dictionary.h +++ b/native_mate/dictionary.h @@ -30,30 +30,33 @@ class Dictionary { template bool Get(const base::StringPiece& key, T* out) const { - v8::Handle val = object_->Get(StringToV8(isolate_, key)); + v8::Handle val = GetHandle()->Get(StringToV8(isolate_, key)); return ConvertFromV8(isolate_, val, out); } template 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 bool SetMethod(const base::StringPiece& key, const T& callback) { - return object_->Set( + return GetHandle()->Set( StringToV8(isolate_, key), CallbackTraits::CreateTemplate(isolate_, callback)->GetFunction()); } - v8::Handle object() const { return object_; } + bool IsEmpty() const { return isolate() == NULL; } + + virtual v8::Handle GetHandle() const; + v8::Isolate* isolate() const { return isolate_; } - private: - friend struct Converter; - - // TODO(aa): Remove this. Instead, get via FromV8(), Set(), and Get(). + protected: v8::Isolate* isolate_; + + private: v8::Handle object_; }; diff --git a/native_mate/persistent_dictionary.cc b/native_mate/persistent_dictionary.cc new file mode 100644 index 00000000000..05c83e5aa8d --- /dev/null +++ b/native_mate/persistent_dictionary.cc @@ -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 object) + : handle_(new RefCountedPersistent(isolate, object)) { + isolate_ = isolate; +} + +PersistentDictionary::~PersistentDictionary() { +} + +v8::Handle PersistentDictionary::GetHandle() const { + return handle_->NewHandle(); +} + +bool Converter::FromV8(v8::Isolate* isolate, + v8::Handle val, + PersistentDictionary* out) { + if (!val->IsObject()) + return false; + *out = PersistentDictionary(isolate, v8::Handle::Cast(val)); + return true; +} + +} // namespace mate diff --git a/native_mate/persistent_dictionary.h b/native_mate/persistent_dictionary.h new file mode 100644 index 00000000000..c0e79e54b17 --- /dev/null +++ b/native_mate/persistent_dictionary.h @@ -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 object); + virtual ~PersistentDictionary(); + + virtual v8::Handle GetHandle() const OVERRIDE; + + private: + scoped_refptr > handle_; +}; + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + PersistentDictionary* out); +}; + +} // namespace mate + +#endif // NATIVE_MATE_PERSISTENT_DICTIONARY_H_ diff --git a/native_mate_files.gypi b/native_mate_files.gypi index e67e5597371..bc245f831a6 100644 --- a/native_mate_files.gypi +++ b/native_mate_files.gypi @@ -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',