// Copyright (c) 2019 Slack Technologies, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_ #define ATOM_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_ #include #include #include #include "gin/converter.h" namespace gin { template struct Converter> { static bool FromV8(v8::Isolate* isolate, v8::Local val, std::map* out) { if (!val->IsObject()) return false; v8::Local context = isolate->GetCurrentContext(); v8::Local dict = val->ToObject(context).ToLocalChecked(); v8::Local keys = dict->GetOwnPropertyNames(context).ToLocalChecked(); for (uint32_t i = 0; i < keys->Length(); ++i) { v8::Local key = keys->Get(context, i).ToLocalChecked(); T value; if (Converter::FromV8( isolate, dict->Get(context, key).ToLocalChecked(), &value)) (*out)[gin::V8ToString(isolate, key)] = std::move(value); } return true; } static v8::Local ToV8(v8::Isolate* isolate, const std::map& val) { v8::Local result = v8::Object::New(isolate); auto context = isolate->GetCurrentContext(); for (auto i = val.begin(); i != val.end(); i++) { result ->Set(context, Converter::ToV8(isolate, i->first), Converter::ToV8(isolate, i->second)) .Check(); } return result; } }; } // namespace gin namespace mate { template struct Converter> { static bool FromV8(v8::Isolate* isolate, v8::Local val, std::map* out) { return gin::Converter>::FromV8(isolate, val, out); } static v8::Local ToV8(v8::Isolate* isolate, const std::map& val) { return gin::Converter>::ToV8(isolate, val); } }; } // namespace mate #endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_