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