Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef GIN_CONVERTER_H_ |
| 6 | #define GIN_CONVERTER_H_ |
| 7 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Jeremy Roman | ee67271a | 2023-11-14 16:36:22 | [diff] [blame^] | 10 | #include <concepts> |
Hans Wennborg | 5cd6d19 | 2020-06-18 11:14:56 | [diff] [blame] | 11 | #include <ostream> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 12 | #include <string> |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 13 | #include <type_traits> |
Joel Klinghed | 99d2a159 | 2023-11-07 19:22:32 | [diff] [blame] | 14 | #include <utility> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Hans Wennborg | c8b134b | 2020-06-19 21:15:39 | [diff] [blame] | 17 | #include "base/check.h" |
| 18 | #include "base/notreached.h" |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 19 | #include "base/strings/string_piece.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 20 | #include "gin/gin_export.h" |
Dan Elphick | 05acd60 | 2021-08-30 15:22:07 | [diff] [blame] | 21 | #include "v8/include/v8-container.h" |
| 22 | #include "v8/include/v8-forward.h" |
| 23 | #include "v8/include/v8-isolate.h" |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 24 | |
Keren Zhu | 1deb467 | 2022-01-14 19:11:51 | [diff] [blame] | 25 | namespace base { |
| 26 | class TimeTicks; |
| 27 | } |
| 28 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 29 | namespace gin { |
| 30 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 31 | template<typename KeyType> |
| 32 | bool SetProperty(v8::Isolate* isolate, |
| 33 | v8::Local<v8::Object> object, |
| 34 | KeyType key, |
| 35 | v8::Local<v8::Value> value) { |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 36 | auto maybe = |
| 37 | object->DefineOwnProperty(isolate->GetCurrentContext(), key, value); |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 38 | return !maybe.IsNothing() && maybe.FromJust(); |
| 39 | } |
| 40 | |
[email protected] | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 41 | template<typename T, typename Enable = void> |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 42 | struct Converter {}; |
| 43 | |
Jeremy Roman | ee67271a | 2023-11-14 16:36:22 | [diff] [blame^] | 44 | namespace internal { |
| 45 | |
| 46 | template <typename T> |
| 47 | concept ToV8ReturnsMaybe = requires(v8::Isolate* isolate, T value) { |
| 48 | { |
| 49 | Converter<T>::ToV8(isolate, value) |
| 50 | } -> std::same_as<v8::MaybeLocal<v8::Value>>; |
| 51 | }; |
| 52 | |
| 53 | } // namespace internal |
| 54 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 55 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 56 | struct GIN_EXPORT Converter<bool> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 57 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 58 | bool val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 59 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 60 | v8::Local<v8::Value> val, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 61 | bool* out); |
| 62 | }; |
| 63 | |
| 64 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 65 | struct GIN_EXPORT Converter<int32_t> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 66 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 67 | int32_t val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 68 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 69 | v8::Local<v8::Value> val, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 70 | int32_t* out); |
| 71 | }; |
| 72 | |
| 73 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 74 | struct GIN_EXPORT Converter<uint32_t> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 75 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 76 | uint32_t val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 77 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 78 | v8::Local<v8::Value> val, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 79 | uint32_t* out); |
| 80 | }; |
| 81 | |
| 82 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 83 | struct GIN_EXPORT Converter<int64_t> { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 84 | // Warning: JavaScript cannot represent 64 integers precisely. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 85 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 86 | int64_t val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 87 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 88 | v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 89 | int64_t* out); |
| 90 | }; |
| 91 | |
| 92 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 93 | struct GIN_EXPORT Converter<uint64_t> { |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 94 | // Warning: JavaScript cannot represent 64 integers precisely. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 95 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 96 | uint64_t val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 97 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 98 | v8::Local<v8::Value> val, |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 99 | uint64_t* out); |
| 100 | }; |
| 101 | |
| 102 | template<> |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 103 | struct GIN_EXPORT Converter<float> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 104 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 105 | float val); |
| 106 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 107 | v8::Local<v8::Value> val, |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 108 | float* out); |
| 109 | }; |
| 110 | |
| 111 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 112 | struct GIN_EXPORT Converter<double> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 113 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 114 | double val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 115 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 116 | v8::Local<v8::Value> val, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 117 | double* out); |
| 118 | }; |
| 119 | |
| 120 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 121 | struct GIN_EXPORT Converter<base::StringPiece> { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 122 | // This crashes when val.size() > v8::String::kMaxLength. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 123 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 124 | const base::StringPiece& val); |
| 125 | // No conversion out is possible because StringPiece does not contain storage. |
| 126 | }; |
| 127 | |
| 128 | template<> |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 129 | struct GIN_EXPORT Converter<std::string> { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 130 | // This crashes when val.size() > v8::String::kMaxLength. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 131 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 132 | const std::string& val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 133 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 134 | v8::Local<v8::Value> val, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 135 | std::string* out); |
| 136 | }; |
| 137 | |
Shimi Zhang | 15708bfc | 2019-08-13 19:24:48 | [diff] [blame] | 138 | template <> |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 139 | struct GIN_EXPORT Converter<std::u16string> { |
Shimi Zhang | 15708bfc | 2019-08-13 19:24:48 | [diff] [blame] | 140 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 141 | const std::u16string& val); |
Shimi Zhang | 15708bfc | 2019-08-13 19:24:48 | [diff] [blame] | 142 | static bool FromV8(v8::Isolate* isolate, |
| 143 | v8::Local<v8::Value> val, |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 144 | std::u16string* out); |
Shimi Zhang | 15708bfc | 2019-08-13 19:24:48 | [diff] [blame] | 145 | }; |
| 146 | |
Keren Zhu | 1deb467 | 2022-01-14 19:11:51 | [diff] [blame] | 147 | // Converter for C++ TimeTicks to Javascript BigInt (unit: microseconds). |
| 148 | // TimeTicks can't be converted using the existing Converter<int64_t> because |
| 149 | // the target type will be Number and will lose precision. |
| 150 | template <> |
| 151 | struct GIN_EXPORT Converter<base::TimeTicks> { |
| 152 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, base::TimeTicks val); |
| 153 | }; |
| 154 | |
Shimi Zhang | 15708bfc | 2019-08-13 19:24:48 | [diff] [blame] | 155 | template <> |
| 156 | struct GIN_EXPORT Converter<v8::Local<v8::Function>> { |
Nitish Sakhawalkar | f7bbaa52 | 2019-02-11 16:19:09 | [diff] [blame] | 157 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 158 | v8::Local<v8::Function> val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 159 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 160 | v8::Local<v8::Value> val, |
| 161 | v8::Local<v8::Function>* out); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 165 | struct GIN_EXPORT Converter<v8::Local<v8::Object> > { |
| 166 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 167 | v8::Local<v8::Object> val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 168 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 169 | v8::Local<v8::Value> val, |
| 170 | v8::Local<v8::Object>* out); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 171 | }; |
| 172 | |
Scott Graham | d2f4ed4b | 2018-10-17 01:57:27 | [diff] [blame] | 173 | template <> |
| 174 | struct GIN_EXPORT Converter<v8::Local<v8::Promise>> { |
| 175 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 176 | v8::Local<v8::Promise> val); |
| 177 | static bool FromV8(v8::Isolate* isolate, |
| 178 | v8::Local<v8::Value> val, |
| 179 | v8::Local<v8::Promise>* out); |
| 180 | }; |
| 181 | |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 182 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 183 | struct GIN_EXPORT Converter<v8::Local<v8::ArrayBuffer> > { |
| 184 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 185 | v8::Local<v8::ArrayBuffer> val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 186 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 187 | v8::Local<v8::Value> val, |
| 188 | v8::Local<v8::ArrayBuffer>* out); |
[email protected] | ec95fdf | 2013-11-24 19:10:11 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 192 | struct GIN_EXPORT Converter<v8::Local<v8::External> > { |
| 193 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 194 | v8::Local<v8::External> val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 195 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 196 | v8::Local<v8::Value> val, |
| 197 | v8::Local<v8::External>* out); |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 201 | struct GIN_EXPORT Converter<v8::Local<v8::Value> > { |
| 202 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 203 | v8::Local<v8::Value> val); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 204 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 205 | v8::Local<v8::Value> val, |
| 206 | v8::Local<v8::Value>* out); |
[email protected] | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 207 | }; |
| 208 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 209 | template<typename T> |
| 210 | struct Converter<std::vector<T> > { |
Jeremy Roman | ee67271a | 2023-11-14 16:36:22 | [diff] [blame^] | 211 | static std::conditional_t<internal::ToV8ReturnsMaybe<T>, |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 212 | v8::MaybeLocal<v8::Value>, |
| 213 | v8::Local<v8::Value>> |
| 214 | ToV8(v8::Isolate* isolate, const std::vector<T>& val) { |
| 215 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 216 | v8::Local<v8::Array> result( |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 217 | v8::Array::New(isolate, static_cast<int>(val.size()))); |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 218 | for (uint32_t i = 0; i < val.size(); ++i) { |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 219 | v8::MaybeLocal<v8::Value> maybe = Converter<T>::ToV8(isolate, val[i]); |
| 220 | v8::Local<v8::Value> element; |
| 221 | if (!maybe.ToLocal(&element)) |
| 222 | return {}; |
| 223 | bool property_created; |
| 224 | if (!result->CreateDataProperty(context, i, element) |
| 225 | .To(&property_created) || |
| 226 | !property_created) { |
| 227 | NOTREACHED() << "CreateDataProperty should always succeed here."; |
| 228 | } |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 229 | } |
| 230 | return result; |
| 231 | } |
| 232 | |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 233 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 234 | v8::Local<v8::Value> val, |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 235 | std::vector<T>* out) { |
| 236 | if (!val->IsArray()) |
| 237 | return false; |
| 238 | |
| 239 | std::vector<T> result; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 240 | v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 241 | uint32_t length = array->Length(); |
| 242 | for (uint32_t i = 0; i < length; ++i) { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 243 | v8::Local<v8::Value> v8_item; |
| 244 | if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item)) |
| 245 | return false; |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 246 | T item; |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 247 | if (!Converter<T>::FromV8(isolate, v8_item, &item)) |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 248 | return false; |
Joel Klinghed | 99d2a159 | 2023-11-07 19:22:32 | [diff] [blame] | 249 | result.push_back(std::move(item)); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | out->swap(result); |
| 253 | return true; |
| 254 | } |
| 255 | }; |
| 256 | |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 257 | template <typename T> |
| 258 | struct Converter<v8::LocalVector<T>> { |
Jeremy Roman | ee67271a | 2023-11-14 16:36:22 | [diff] [blame^] | 259 | static std::conditional_t<internal::ToV8ReturnsMaybe<v8::Local<T>>, |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 260 | v8::MaybeLocal<v8::Value>, |
| 261 | v8::Local<v8::Value>> |
| 262 | ToV8(v8::Isolate* isolate, const v8::LocalVector<T>& val) { |
| 263 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 264 | v8::Local<v8::Array> result( |
| 265 | v8::Array::New(isolate, static_cast<int>(val.size()))); |
| 266 | for (uint32_t i = 0; i < val.size(); ++i) { |
| 267 | v8::MaybeLocal<v8::Value> maybe = |
| 268 | Converter<v8::Local<T>>::ToV8(isolate, val[i]); |
| 269 | v8::Local<v8::Value> element; |
| 270 | if (!maybe.ToLocal(&element)) { |
| 271 | return {}; |
| 272 | } |
| 273 | bool property_created; |
| 274 | if (!result->CreateDataProperty(context, i, element) |
| 275 | .To(&property_created) || |
| 276 | !property_created) { |
| 277 | NOTREACHED() << "CreateDataProperty should always succeed here."; |
| 278 | } |
| 279 | } |
| 280 | return result; |
| 281 | } |
| 282 | |
| 283 | static bool FromV8(v8::Isolate* isolate, |
| 284 | v8::Local<v8::Value> val, |
| 285 | v8::LocalVector<T>* out) { |
| 286 | if (!val->IsArray()) { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | v8::LocalVector<T> result(isolate); |
| 291 | v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); |
| 292 | uint32_t length = array->Length(); |
| 293 | for (uint32_t i = 0; i < length; ++i) { |
| 294 | v8::Local<v8::Value> v8_item; |
| 295 | if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item)) { |
| 296 | return false; |
| 297 | } |
| 298 | v8::Local<T> item; |
| 299 | if (!Converter<v8::Local<T>>::FromV8(isolate, v8_item, &item)) { |
| 300 | return false; |
| 301 | } |
| 302 | result.push_back(item); |
| 303 | } |
| 304 | |
| 305 | out->swap(result); |
| 306 | return true; |
| 307 | } |
| 308 | }; |
| 309 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 310 | // Convenience functions that deduce T. |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 311 | template <typename T> |
Jeremy Roman | ee67271a | 2023-11-14 16:36:22 | [diff] [blame^] | 312 | std::conditional_t<internal::ToV8ReturnsMaybe<T>, |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 313 | v8::MaybeLocal<v8::Value>, |
| 314 | v8::Local<v8::Value>> |
Jeremy Apthorp | 6a348029 | 2018-04-26 18:53:59 | [diff] [blame] | 315 | ConvertToV8(v8::Isolate* isolate, const T& input) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 316 | return Converter<T>::ToV8(isolate, input); |
| 317 | } |
| 318 | |
Jeremy Roman | 44bda444b | 2018-03-16 17:33:45 | [diff] [blame] | 319 | template <typename T> |
Jeremy Roman | ee67271a | 2023-11-14 16:36:22 | [diff] [blame^] | 320 | bool TryConvertToV8(v8::Isolate* isolate, |
| 321 | const T& input, |
| 322 | v8::Local<v8::Value>* output) { |
| 323 | if constexpr (internal::ToV8ReturnsMaybe<T>) { |
| 324 | return ConvertToV8(isolate, input).ToLocal(output); |
| 325 | } else { |
| 326 | *output = ConvertToV8(isolate, input); |
| 327 | return true; |
| 328 | } |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // This crashes when input.size() > v8::String::kMaxLength. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 332 | GIN_EXPORT inline v8::Local<v8::String> StringToV8( |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 333 | v8::Isolate* isolate, |
| 334 | const base::StringPiece& input) { |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 335 | return ConvertToV8(isolate, input).As<v8::String>(); |
| 336 | } |
| 337 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 338 | // This crashes when input.size() > v8::String::kMaxLength. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 339 | GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 340 | const base::StringPiece& val); |
[email protected] | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 341 | |
Shimi Zhang | 15708bfc | 2019-08-13 19:24:48 | [diff] [blame] | 342 | // This crashes when input.size() > v8::String::kMaxLength. |
| 343 | GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, |
| 344 | const base::StringPiece16& val); |
| 345 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 346 | template<typename T> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 347 | bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 348 | T* result) { |
Dan Elphick | 712beaa | 2018-07-24 17:37:24 | [diff] [blame] | 349 | DCHECK(isolate); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 350 | return Converter<T>::FromV8(isolate, input, result); |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 351 | } |
| 352 | |
Dan Elphick | 38a50805 | 2018-07-23 22:19:53 | [diff] [blame] | 353 | GIN_EXPORT std::string V8ToString(v8::Isolate* isolate, |
| 354 | v8::Local<v8::Value> value); |
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 355 | |
[email protected] | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 356 | } // namespace gin |
| 357 | |
| 358 | #endif // GIN_CONVERTER_H_ |