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