Enable converting v8 object to dict type.

This commit is contained in:
Cheng Zhao 2013-11-18 18:15:34 +08:00
parent 28b692aeaa
commit f8f09eb974

View file

@ -5,6 +5,7 @@
#ifndef COMMON_V8_CONVERSIONS_H_
#define COMMON_V8_CONVERSIONS_H_
#include <map>
#include <string>
#include <vector>
@ -47,6 +48,18 @@ struct FromV8Value {
return array;
}
operator std::map<std::string, std::string>() {
std::map<std::string, std::string> dict;
v8::Handle<v8::Object> v8_dict = value_->ToObject();
v8::Handle<v8::Array> v8_keys = v8_dict->GetOwnPropertyNames();
for (uint32_t i = 0; i < v8_keys->Length(); ++i) {
std::string key = FromV8Value(v8_keys->Get(i));
dict[key] = std::string(FromV8Value(v8_dict->Get(i)));
}
return dict;
}
operator atom::NativeWindow*() {
using atom::api::Window;
if (value_->IsObject()) {
@ -143,6 +156,12 @@ bool V8ValueCanBeConvertedTo<std::vector<std::string>>(
return value->IsArray();
}
template<> inline
bool V8ValueCanBeConvertedTo<std::map<std::string, std::string>>(
v8::Handle<v8::Value> value) {
return value->IsObject();
}
template<> inline
bool V8ValueCanBeConvertedTo<atom::NativeWindow*>(v8::Handle<v8::Value> value) {
using atom::api::Window;