From: Cheng Zhao Date: Tue, 7 Jan 2014 12:23:46 +0000 (+0800) Subject: Support converting v8 value to arbitrary vector or map. X-Git-Tag: v0.8.2^2~1 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=bcdc44c1c66f35a9cbc0f140d3b5cbdc4eb3845a;p=electron.git Support converting v8 value to arbitrary vector or map. --- diff --git a/common/v8/native_type_conversions.h b/common/v8/native_type_conversions.h index 05bc883a..2beed8be 100644 --- a/common/v8/native_type_conversions.h +++ b/common/v8/native_type_conversions.h @@ -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::vector array; + template + operator std::vector() { + std::vector array; v8::Handle v8_array = v8::Handle::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::map dict; + template + operator std::map() { + std::map dict; v8::Handle v8_dict = value_->ToObject(); v8::Handle v8_keys = v8_dict->GetOwnPropertyNames(); for (uint32_t i = 0; i < v8_keys->Length(); ++i) { v8::Handle 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 ToV8Value(void* whatever) { return v8::Undefined(); } -inline -v8::Handle ToV8Value(const std::vector& paths) { - v8::Handle result = v8::Array::New(paths.size()); - for (size_t i = 0; i < paths.size(); ++i) - result->Set(i, ToV8Value(paths[i])); +template inline +v8::Handle ToV8Value(const std::vector& arr) { + v8::Handle result = v8::Array::New(arr.size()); + for (size_t i = 0; i < arr.size(); ++i) + result->Set(i, ToV8Value(arr[i])); return result; }