// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ #define SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ #include #include #include #include "gin/converter.h" #if defined(OS_WIN) #include "base/strings/string_util_win.h" #endif namespace gin { // Make it possible to convert move-only types. template v8::Local ConvertToV8(v8::Isolate* isolate, T&& input) { return Converter::type>::ToV8( isolate, std::move(input)); } #if !defined(OS_LINUX) && !defined(OS_FREEBSD) template <> struct Converter { // NOLINT(runtime/int) static v8::Local ToV8(v8::Isolate* isolate, unsigned long val) { // NOLINT(runtime/int) return v8::Integer::New(isolate, val); } static bool FromV8(v8::Isolate* isolate, v8::Local val, unsigned long* out) { // NOLINT(runtime/int) auto maybe = val->IntegerValue(isolate->GetCurrentContext()); if (maybe.IsNothing()) return false; *out = maybe.FromJust(); return true; } }; #endif template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, std::nullptr_t val) { return v8::Null(isolate); } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const char* val) { return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal) .ToLocalChecked(); } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const char* val) { return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal) .ToLocalChecked(); } }; template struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const char* val) { return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal, n - 1) .ToLocalChecked(); } }; template <> struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, v8::Local val) { return val; } static bool FromV8(v8::Isolate* isolate, v8::Local val, v8::Local* out) { if (!val->IsArray()) return false; *out = v8::Local::Cast(val); return true; } }; template <> struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, v8::Local val) { return val; } static bool FromV8(v8::Isolate* isolate, v8::Local val, v8::Local* out) { if (!val->IsString()) return false; *out = v8::Local::Cast(val); return true; } }; template struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, const std::set& val) { v8::Local result( v8::Array::New(isolate, static_cast(val.size()))); auto context = isolate->GetCurrentContext(); typename std::set::const_iterator it; int i; for (i = 0, it = val.begin(); it != val.end(); ++it, ++i) result->Set(context, i, Converter::ToV8(isolate, *it)).Check(); return result; } static bool FromV8(v8::Isolate* isolate, v8::Local val, std::set* out) { if (!val->IsArray()) return false; auto context = isolate->GetCurrentContext(); std::set result; v8::Local array(v8::Local::Cast(val)); uint32_t length = array->Length(); for (uint32_t i = 0; i < length; ++i) { T item; if (!Converter::FromV8(isolate, array->Get(context, i).ToLocalChecked(), &item)) return false; result.insert(item); } out->swap(result); return true; } }; template struct Converter> { static bool FromV8(v8::Isolate* isolate, v8::Local value, std::map* out) { if (!value->IsObject()) return false; out->clear(); v8::Local context = isolate->GetCurrentContext(); v8::Local obj = value.As(); v8::Local keys = obj->GetPropertyNames(context).ToLocalChecked(); for (uint32_t i = 0; i < keys->Length(); ++i) { v8::MaybeLocal maybe_v8key = keys->Get(context, i); if (maybe_v8key.IsEmpty()) return false; v8::Local v8key = maybe_v8key.ToLocalChecked(); v8::MaybeLocal maybe_v8value = obj->Get(context, v8key); if (maybe_v8value.IsEmpty()) return false; K key; V value; if (!ConvertFromV8(isolate, v8key, &key) || !ConvertFromV8(isolate, maybe_v8value.ToLocalChecked(), &value)) return false; (*out)[key] = std::move(value); } return true; } static v8::Local ToV8(v8::Isolate* isolate, const std::map& dict) { v8::Local obj = v8::Object::New(isolate); v8::Local context = isolate->GetCurrentContext(); for (const auto& it : dict) { if (obj->Set(context, ConvertToV8(isolate, it.first), ConvertToV8(isolate, it.second)) .IsNothing()) break; } return obj; } }; #if defined(OS_WIN) template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const std::wstring& val) { return Converter::ToV8(isolate, base::AsString16(val)); } static bool FromV8(v8::Isolate* isolate, v8::Local val, std::wstring* out) { if (!val->IsString()) return false; std::u16string str; if (Converter::FromV8(isolate, val, &str)) { *out = base::AsWString(str); return true; } else { return false; } } }; #endif } // namespace gin #endif // SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_