Support converting v8 value to arbitrary vector or map.

This commit is contained in:
Cheng Zhao 2014-01-07 20:23:46 +08:00
parent 4ae2738ce5
commit bcdc44c1c6

View file

@ -17,7 +17,6 @@
#include "common/swap_or_assign.h"
#include "common/v8/scoped_persistent.h"
#include "common/v8/v8_value_converter.h"
#include "content/public/renderer/v8_value_converter.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
@ -73,8 +72,9 @@ struct FromV8Value {
converter->FromV8Value(value_, v8::Context::GetCurrent()));
}
operator std::vector<std::string>() {
std::vector<std::string> array;
template<class T>
operator std::vector<T>() {
std::vector<T> array;
v8::Handle<v8::Array> v8_array = v8::Handle<v8::Array>::Cast(value_);
for (uint32_t i = 0; i < v8_array->Length(); ++i)
array.push_back(FromV8Value(v8_array->Get(i)));
@ -82,14 +82,15 @@ struct FromV8Value {
return array;
}
operator std::map<std::string, std::string>() {
std::map<std::string, std::string> dict;
template<class K, class V>
operator std::map<K, V>() {
std::map<K, V> 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) {
v8::Handle<v8::Value> v8_key = v8_keys->Get(i);
std::string key = FromV8Value(v8_key);
dict[key] = std::string(FromV8Value(v8_dict->Get(v8_key)));
K key = FromV8Value(v8_key);
dict[key] = V(FromV8Value(v8_dict->Get(v8_key)));
}
return dict;
@ -156,11 +157,11 @@ inline v8::Handle<v8::Value> ToV8Value(void* whatever) {
return v8::Undefined();
}
inline
v8::Handle<v8::Value> ToV8Value(const std::vector<base::FilePath>& paths) {
v8::Handle<v8::Array> result = v8::Array::New(paths.size());
for (size_t i = 0; i < paths.size(); ++i)
result->Set(i, ToV8Value(paths[i]));
template<class T> inline
v8::Handle<v8::Value> ToV8Value(const std::vector<T>& arr) {
v8::Handle<v8::Array> result = v8::Array::New(arr.size());
for (size_t i = 0; i < arr.size(); ++i)
result->Set(i, ToV8Value(arr[i]));
return result;
}