From e75f2aa087db346efc4b530f9e1ce7d3a72a3434 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 13 Jun 2016 09:26:04 +0900 Subject: [PATCH] Add converter for std::map --- native_mate/converter.h | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/native_mate/converter.h b/native_mate/converter.h index 6e51bcdcd9f7..a891020b4029 100644 --- a/native_mate/converter.h +++ b/native_mate/converter.h @@ -5,6 +5,7 @@ #ifndef NATIVE_MATE_CONVERTER_H_ #define NATIVE_MATE_CONVERTER_H_ +#include #include #include #include @@ -136,6 +137,11 @@ struct Converter { std::string* out); }; +v8::Local StringToSymbol(v8::Isolate* isolate, + const base::StringPiece& input); + +std::string V8ToString(v8::Local value); + template<> struct Converter > { static bool FromV8(v8::Isolate* isolate, @@ -255,6 +261,26 @@ struct Converter > { } }; +template +struct Converter > { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + std::map * out) { + if (!val->IsObject()) + return false; + + v8::Local dict = val->ToObject(); + v8::Local keys = dict->GetOwnPropertyNames(); + for (uint32_t i = 0; i < keys->Length(); ++i) { + v8::Local key = keys->Get(i); + T value; + if (Converter::FromV8(isolate, dict->Get(key), &value)) + (*out)[V8ToString(key)] = std::move(value); + } + return true; + } +}; + // Convenience functions that deduce T. template v8::Local ConvertToV8(v8::Isolate* isolate, const T& input) { @@ -303,14 +329,6 @@ bool TryConvertToV8(v8::Isolate* isolate, v8::Local* output) { return ToV8Traits::TryConvertToV8(isolate, input, output); } -inline v8::Local StringToV8( - v8::Isolate* isolate, - const base::StringPiece& input) { - return ConvertToV8(isolate, input).As(); -} - -v8::Local StringToSymbol(v8::Isolate* isolate, - const base::StringPiece& input); template bool ConvertFromV8(v8::Isolate* isolate, v8::Local input, @@ -318,7 +336,11 @@ bool ConvertFromV8(v8::Isolate* isolate, v8::Local input, return Converter::FromV8(isolate, input, result); } -std::string V8ToString(v8::Local value); +inline v8::Local StringToV8( + v8::Isolate* isolate, + const base::StringPiece& input) { + return ConvertToV8(isolate, input).As(); +} } // namespace mate