std_converter.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
  5. #define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
  6. #include <cstddef>
  7. #include <functional>
  8. #include <map>
  9. #include <set>
  10. #include <type_traits>
  11. #include <utility>
  12. #include "gin/converter.h"
  13. #include "base/strings/string_util.h"
  14. #if BUILDFLAG(IS_WIN)
  15. #include "base/strings/string_util_win.h"
  16. #endif
  17. namespace gin {
  18. // Make it possible to convert move-only types.
  19. template <typename T>
  20. v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T&& input) {
  21. return Converter<typename std::remove_reference<T>::type>::ToV8(
  22. isolate, std::forward<T>(input));
  23. }
  24. #if !BUILDFLAG(IS_LINUX)
  25. template <>
  26. struct Converter<unsigned long> { // NOLINT(runtime/int)
  27. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  28. unsigned long val) { // NOLINT(runtime/int)
  29. return v8::Integer::New(isolate, val);
  30. }
  31. static bool FromV8(v8::Isolate* isolate,
  32. v8::Local<v8::Value> val,
  33. unsigned long* out) { // NOLINT(runtime/int)
  34. auto maybe = val->IntegerValue(isolate->GetCurrentContext());
  35. if (maybe.IsNothing())
  36. return false;
  37. *out = maybe.FromJust();
  38. return true;
  39. }
  40. };
  41. #endif
  42. template <>
  43. struct Converter<std::nullptr_t> {
  44. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, std::nullptr_t val) {
  45. return v8::Null(isolate);
  46. }
  47. };
  48. template <>
  49. struct Converter<const char*> {
  50. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val) {
  51. return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal)
  52. .ToLocalChecked();
  53. }
  54. };
  55. template <>
  56. struct Converter<char[]> {
  57. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val) {
  58. return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal)
  59. .ToLocalChecked();
  60. }
  61. };
  62. template <size_t n>
  63. struct Converter<char[n]> {
  64. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val) {
  65. return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal,
  66. n - 1)
  67. .ToLocalChecked();
  68. }
  69. };
  70. template <>
  71. struct Converter<v8::Local<v8::Array>> {
  72. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  73. v8::Local<v8::Array> val) {
  74. return val;
  75. }
  76. static bool FromV8(v8::Isolate* isolate,
  77. v8::Local<v8::Value> val,
  78. v8::Local<v8::Array>* out) {
  79. if (!val->IsArray())
  80. return false;
  81. *out = val.As<v8::Array>();
  82. return true;
  83. }
  84. };
  85. template <>
  86. struct Converter<v8::Local<v8::String>> {
  87. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  88. v8::Local<v8::String> val) {
  89. return val;
  90. }
  91. static bool FromV8(v8::Isolate* isolate,
  92. v8::Local<v8::Value> val,
  93. v8::Local<v8::String>* out) {
  94. if (!val->IsString())
  95. return false;
  96. *out = val.As<v8::String>();
  97. return true;
  98. }
  99. };
  100. template <typename T>
  101. struct Converter<std::set<T>> {
  102. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  103. const std::set<T>& val) {
  104. v8::Local<v8::Array> result(
  105. v8::Array::New(isolate, static_cast<int>(val.size())));
  106. auto context = isolate->GetCurrentContext();
  107. typename std::set<T>::const_iterator it;
  108. int i;
  109. for (i = 0, it = val.begin(); it != val.end(); ++it, ++i)
  110. result->Set(context, i, Converter<T>::ToV8(isolate, *it)).Check();
  111. return result;
  112. }
  113. static bool FromV8(v8::Isolate* isolate,
  114. v8::Local<v8::Value> val,
  115. std::set<T>* out) {
  116. if (!val->IsArray())
  117. return false;
  118. auto context = isolate->GetCurrentContext();
  119. std::set<T> result;
  120. v8::Local<v8::Array> array = val.As<v8::Array>();
  121. uint32_t length = array->Length();
  122. for (uint32_t i = 0; i < length; ++i) {
  123. T item;
  124. if (!Converter<T>::FromV8(isolate,
  125. array->Get(context, i).ToLocalChecked(), &item))
  126. return false;
  127. result.insert(item);
  128. }
  129. out->swap(result);
  130. return true;
  131. }
  132. };
  133. template <typename K, typename V>
  134. struct Converter<std::map<K, V>> {
  135. static bool FromV8(v8::Isolate* isolate,
  136. v8::Local<v8::Value> value,
  137. std::map<K, V>* out) {
  138. if (!value->IsObject())
  139. return false;
  140. out->clear();
  141. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  142. v8::Local<v8::Object> obj = value.As<v8::Object>();
  143. v8::Local<v8::Array> keys = obj->GetPropertyNames(context).ToLocalChecked();
  144. for (uint32_t i = 0; i < keys->Length(); ++i) {
  145. v8::MaybeLocal<v8::Value> maybe_v8key = keys->Get(context, i);
  146. if (maybe_v8key.IsEmpty())
  147. return false;
  148. v8::Local<v8::Value> v8key = maybe_v8key.ToLocalChecked();
  149. v8::MaybeLocal<v8::Value> maybe_v8value = obj->Get(context, v8key);
  150. if (maybe_v8value.IsEmpty())
  151. return false;
  152. K key;
  153. V out_value;
  154. if (!ConvertFromV8(isolate, v8key, &key) ||
  155. !ConvertFromV8(isolate, maybe_v8value.ToLocalChecked(), &out_value))
  156. return false;
  157. (*out)[key] = std::move(out_value);
  158. }
  159. return true;
  160. }
  161. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  162. const std::map<K, V>& dict) {
  163. v8::Local<v8::Object> obj = v8::Object::New(isolate);
  164. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  165. for (const auto& it : dict) {
  166. if (obj->Set(context, ConvertToV8(isolate, it.first),
  167. ConvertToV8(isolate, it.second))
  168. .IsNothing())
  169. break;
  170. }
  171. return obj;
  172. }
  173. };
  174. #if BUILDFLAG(IS_WIN)
  175. template <>
  176. struct Converter<std::wstring> {
  177. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  178. const std::wstring& val) {
  179. return Converter<std::u16string>::ToV8(isolate, base::AsString16(val));
  180. }
  181. static bool FromV8(v8::Isolate* isolate,
  182. v8::Local<v8::Value> val,
  183. std::wstring* out) {
  184. if (!val->IsString())
  185. return false;
  186. std::u16string str;
  187. if (Converter<std::u16string>::FromV8(isolate, val, &str)) {
  188. *out = base::AsWString(str);
  189. return true;
  190. } else {
  191. return false;
  192. }
  193. }
  194. };
  195. #endif
  196. namespace detail {
  197. // Get a key from `key_val` and check `lookup` for a matching entry.
  198. // Return true iff a match is found, and set `*out` to the entry's value.
  199. template <typename KeyType, typename Out, typename Map>
  200. bool FromV8WithLookup(v8::Isolate* isolate,
  201. v8::Local<v8::Value> key_val,
  202. const Map& table,
  203. Out* out,
  204. std::function<void(KeyType&)> key_transform = {}) {
  205. static_assert(std::is_same_v<typename Map::mapped_type, Out>);
  206. auto key = KeyType{};
  207. if (!ConvertFromV8(isolate, key_val, &key))
  208. return false;
  209. if (key_transform)
  210. key_transform(key);
  211. if (const auto* iter = table.find(key); iter != table.end()) {
  212. *out = iter->second;
  213. return true;
  214. }
  215. return false;
  216. }
  217. } // namespace detail
  218. // Convert `key_val to a string key and check `lookup` for a matching entry.
  219. // Return true iff a match is found, and set `*out` to the entry's value.
  220. template <typename Out, typename Map>
  221. bool FromV8WithLookup(v8::Isolate* isolate,
  222. v8::Local<v8::Value> key_val,
  223. const Map& table,
  224. Out* out) {
  225. return detail::FromV8WithLookup<std::string>(isolate, key_val, table, out);
  226. }
  227. // Convert `key_val` to a lowercase string key and check `lookup` for a matching
  228. // entry. Return true iff a match is found, and set `*out` to the entry's value.
  229. template <typename Out, typename Map>
  230. bool FromV8WithLowerLookup(v8::Isolate* isolate,
  231. v8::Local<v8::Value> key_val,
  232. const Map& table,
  233. Out* out) {
  234. static constexpr auto to_lower_ascii_inplace = [](std::string& str) {
  235. for (auto& ch : str)
  236. ch = base::ToLowerASCII(ch);
  237. };
  238. return detail::FromV8WithLookup<std::string>(isolate, key_val, table, out,
  239. to_lower_ascii_inplace);
  240. }
  241. } // namespace gin
  242. #endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_