std_converter.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <size_t N>
  49. struct Converter<char[N]> {
  50. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char (&val)[N]) {
  51. return v8::String::NewFromUtf8Literal(isolate, val);
  52. }
  53. };
  54. template <>
  55. struct Converter<const char*> {
  56. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val) {
  57. return v8::String::NewFromUtf8(isolate, val ? val : "",
  58. v8::NewStringType::kNormal)
  59. .ToLocalChecked();
  60. }
  61. };
  62. template <>
  63. struct Converter<v8::Local<v8::Array>> {
  64. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  65. v8::Local<v8::Array> val) {
  66. return val;
  67. }
  68. static bool FromV8(v8::Isolate* isolate,
  69. v8::Local<v8::Value> val,
  70. v8::Local<v8::Array>* out) {
  71. if (!val->IsArray())
  72. return false;
  73. *out = val.As<v8::Array>();
  74. return true;
  75. }
  76. };
  77. template <>
  78. struct Converter<v8::Local<v8::String>> {
  79. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  80. v8::Local<v8::String> val) {
  81. return val;
  82. }
  83. static bool FromV8(v8::Isolate* isolate,
  84. v8::Local<v8::Value> val,
  85. v8::Local<v8::String>* out) {
  86. if (!val->IsString())
  87. return false;
  88. *out = val.As<v8::String>();
  89. return true;
  90. }
  91. };
  92. template <typename T>
  93. struct Converter<std::set<T>> {
  94. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  95. const std::set<T>& val) {
  96. v8::Local<v8::Array> result(
  97. v8::Array::New(isolate, static_cast<int>(val.size())));
  98. auto context = isolate->GetCurrentContext();
  99. typename std::set<T>::const_iterator it;
  100. int i;
  101. for (i = 0, it = val.begin(); it != val.end(); ++it, ++i)
  102. result->Set(context, i, Converter<T>::ToV8(isolate, *it)).Check();
  103. return result;
  104. }
  105. static bool FromV8(v8::Isolate* isolate,
  106. v8::Local<v8::Value> val,
  107. std::set<T>* out) {
  108. if (!val->IsArray())
  109. return false;
  110. auto context = isolate->GetCurrentContext();
  111. std::set<T> result;
  112. v8::Local<v8::Array> array = val.As<v8::Array>();
  113. uint32_t length = array->Length();
  114. for (uint32_t i = 0; i < length; ++i) {
  115. T item;
  116. if (!Converter<T>::FromV8(isolate,
  117. array->Get(context, i).ToLocalChecked(), &item))
  118. return false;
  119. result.insert(item);
  120. }
  121. out->swap(result);
  122. return true;
  123. }
  124. };
  125. template <typename K, typename V>
  126. struct Converter<std::map<K, V>> {
  127. static bool FromV8(v8::Isolate* isolate,
  128. v8::Local<v8::Value> value,
  129. std::map<K, V>* out) {
  130. if (!value->IsObject())
  131. return false;
  132. out->clear();
  133. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  134. v8::Local<v8::Object> obj = value.As<v8::Object>();
  135. v8::Local<v8::Array> keys = obj->GetPropertyNames(context).ToLocalChecked();
  136. for (uint32_t i = 0; i < keys->Length(); ++i) {
  137. v8::MaybeLocal<v8::Value> maybe_v8key = keys->Get(context, i);
  138. if (maybe_v8key.IsEmpty())
  139. return false;
  140. v8::Local<v8::Value> v8key = maybe_v8key.ToLocalChecked();
  141. v8::MaybeLocal<v8::Value> maybe_v8value = obj->Get(context, v8key);
  142. if (maybe_v8value.IsEmpty())
  143. return false;
  144. K key;
  145. V out_value;
  146. if (!ConvertFromV8(isolate, v8key, &key) ||
  147. !ConvertFromV8(isolate, maybe_v8value.ToLocalChecked(), &out_value))
  148. return false;
  149. (*out)[key] = std::move(out_value);
  150. }
  151. return true;
  152. }
  153. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  154. const std::map<K, V>& dict) {
  155. v8::Local<v8::Object> obj = v8::Object::New(isolate);
  156. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  157. for (const auto& it : dict) {
  158. if (obj->Set(context, ConvertToV8(isolate, it.first),
  159. ConvertToV8(isolate, it.second))
  160. .IsNothing())
  161. break;
  162. }
  163. return obj;
  164. }
  165. };
  166. #if BUILDFLAG(IS_WIN)
  167. template <>
  168. struct Converter<std::wstring> {
  169. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  170. const std::wstring& val) {
  171. return Converter<std::u16string>::ToV8(isolate, base::AsString16(val));
  172. }
  173. static bool FromV8(v8::Isolate* isolate,
  174. v8::Local<v8::Value> val,
  175. std::wstring* out) {
  176. if (!val->IsString())
  177. return false;
  178. std::u16string str;
  179. if (Converter<std::u16string>::FromV8(isolate, val, &str)) {
  180. *out = base::AsWString(str);
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. }
  186. };
  187. #endif
  188. namespace detail {
  189. // Get a key from `key_val` and check `lookup` for a matching entry.
  190. // Return true iff a match is found, and set `*out` to the entry's value.
  191. template <typename KeyType, typename Out, typename Map>
  192. bool FromV8WithLookup(v8::Isolate* isolate,
  193. v8::Local<v8::Value> key_val,
  194. const Map& table,
  195. Out* out,
  196. std::function<void(KeyType&)> key_transform = {}) {
  197. static_assert(std::is_same_v<typename Map::mapped_type, Out>);
  198. auto key = KeyType{};
  199. if (!ConvertFromV8(isolate, key_val, &key))
  200. return false;
  201. if (key_transform)
  202. key_transform(key);
  203. if (const auto* iter = table.find(key); iter != table.end()) {
  204. *out = iter->second;
  205. return true;
  206. }
  207. return false;
  208. }
  209. } // namespace detail
  210. // Convert `key_val to a string key and check `lookup` for a matching entry.
  211. // Return true iff a match is found, and set `*out` to the entry's value.
  212. template <typename Out, typename Map>
  213. bool FromV8WithLookup(v8::Isolate* isolate,
  214. v8::Local<v8::Value> key_val,
  215. const Map& table,
  216. Out* out) {
  217. return detail::FromV8WithLookup<std::string>(isolate, key_val, table, out);
  218. }
  219. // Convert `key_val` to a lowercase string key and check `lookup` for a matching
  220. // entry. Return true iff a match is found, and set `*out` to the entry's value.
  221. template <typename Out, typename Map>
  222. bool FromV8WithLowerLookup(v8::Isolate* isolate,
  223. v8::Local<v8::Value> key_val,
  224. const Map& table,
  225. Out* out) {
  226. static constexpr auto to_lower_ascii_inplace = [](std::string& str) {
  227. for (auto& ch : str)
  228. ch = base::ToLowerASCII(ch);
  229. };
  230. return detail::FromV8WithLookup<std::string>(isolate, key_val, table, out,
  231. to_lower_ascii_inplace);
  232. }
  233. } // namespace gin
  234. #endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_