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