| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_CONVERTER_H_ | 5 #ifndef GIN_CONVERTER_H_ |
| 6 #define GIN_CONVERTER_H_ | 6 #define GIN_CONVERTER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 13 #include "gin/gin_export.h" | 12 #include "gin/gin_export.h" |
| 14 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 15 | 14 |
| 16 namespace gin { | 15 namespace gin { |
| 17 | 16 |
| 18 template<typename KeyType> | |
| 19 bool SetProperty(v8::Isolate* isolate, | |
| 20 v8::Local<v8::Object> object, | |
| 21 KeyType key, | |
| 22 v8::Local<v8::Value> value) { | |
| 23 auto maybe = object->Set(isolate->GetCurrentContext(), key, value); | |
| 24 return !maybe.IsNothing() && maybe.FromJust(); | |
| 25 } | |
| 26 | |
| 27 template<typename T> | |
| 28 struct ToV8ReturnsMaybe { | |
| 29 static const bool value = false; | |
| 30 }; | |
| 31 | |
| 32 template<typename T, typename Enable = void> | 17 template<typename T, typename Enable = void> |
| 33 struct Converter {}; | 18 struct Converter {}; |
| 34 | 19 |
| 35 template<> | 20 template<> |
| 36 struct GIN_EXPORT Converter<bool> { | 21 struct GIN_EXPORT Converter<bool> { |
| 37 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 22 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 38 bool val); | 23 bool val); |
| 39 static bool FromV8(v8::Isolate* isolate, | 24 static bool FromV8(v8::Isolate* isolate, |
| 40 v8::Local<v8::Value> val, | 25 v8::Local<v8::Value> val, |
| 41 bool* out); | 26 bool* out); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 struct GIN_EXPORT Converter<double> { | 77 struct GIN_EXPORT Converter<double> { |
| 93 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 78 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 94 double val); | 79 double val); |
| 95 static bool FromV8(v8::Isolate* isolate, | 80 static bool FromV8(v8::Isolate* isolate, |
| 96 v8::Local<v8::Value> val, | 81 v8::Local<v8::Value> val, |
| 97 double* out); | 82 double* out); |
| 98 }; | 83 }; |
| 99 | 84 |
| 100 template<> | 85 template<> |
| 101 struct GIN_EXPORT Converter<base::StringPiece> { | 86 struct GIN_EXPORT Converter<base::StringPiece> { |
| 102 // This crashes when val.size() > v8::String::kMaxLength. | |
| 103 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 87 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 104 const base::StringPiece& val); | 88 const base::StringPiece& val); |
| 105 // No conversion out is possible because StringPiece does not contain storage. | 89 // No conversion out is possible because StringPiece does not contain storage. |
| 106 }; | 90 }; |
| 107 | 91 |
| 108 template<> | 92 template<> |
| 109 struct GIN_EXPORT Converter<std::string> { | 93 struct GIN_EXPORT Converter<std::string> { |
| 110 // This crashes when val.size() > v8::String::kMaxLength. | |
| 111 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 94 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 112 const std::string& val); | 95 const std::string& val); |
| 113 static bool FromV8(v8::Isolate* isolate, | 96 static bool FromV8(v8::Isolate* isolate, |
| 114 v8::Local<v8::Value> val, | 97 v8::Local<v8::Value> val, |
| 115 std::string* out); | 98 std::string* out); |
| 116 }; | 99 }; |
| 117 | 100 |
| 118 template<> | 101 template<> |
| 119 struct GIN_EXPORT Converter<v8::Local<v8::Function> > { | 102 struct GIN_EXPORT Converter<v8::Local<v8::Function> > { |
| 120 static bool FromV8(v8::Isolate* isolate, | 103 static bool FromV8(v8::Isolate* isolate, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 struct GIN_EXPORT Converter<v8::Local<v8::Value> > { | 136 struct GIN_EXPORT Converter<v8::Local<v8::Value> > { |
| 154 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, | 137 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 155 v8::Local<v8::Value> val); | 138 v8::Local<v8::Value> val); |
| 156 static bool FromV8(v8::Isolate* isolate, | 139 static bool FromV8(v8::Isolate* isolate, |
| 157 v8::Local<v8::Value> val, | 140 v8::Local<v8::Value> val, |
| 158 v8::Local<v8::Value>* out); | 141 v8::Local<v8::Value>* out); |
| 159 }; | 142 }; |
| 160 | 143 |
| 161 template<typename T> | 144 template<typename T> |
| 162 struct Converter<std::vector<T> > { | 145 struct Converter<std::vector<T> > { |
| 163 static v8::MaybeLocal<v8::Value> ToV8(v8::Local<v8::Context> context, | 146 static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 164 const std::vector<T>& val) { | 147 const std::vector<T>& val) { |
| 165 v8::Isolate* isolate = context->GetIsolate(); | |
| 166 v8::Local<v8::Array> result( | 148 v8::Local<v8::Array> result( |
| 167 v8::Array::New(isolate, static_cast<int>(val.size()))); | 149 v8::Array::New(isolate, static_cast<int>(val.size()))); |
| 168 for (uint32_t i = 0; i < val.size(); ++i) { | 150 for (size_t i = 0; i < val.size(); ++i) { |
| 169 auto maybe = result->Set(context, i, Converter<T>::ToV8(isolate, val[i])); | 151 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); |
| 170 if (maybe.IsNothing() || !maybe.FromJust()) | |
| 171 return v8::MaybeLocal<v8::Value>(); | |
| 172 } | 152 } |
| 173 return result; | 153 return result; |
| 174 } | 154 } |
| 175 | 155 |
| 176 static bool FromV8(v8::Isolate* isolate, | 156 static bool FromV8(v8::Isolate* isolate, |
| 177 v8::Local<v8::Value> val, | 157 v8::Local<v8::Value> val, |
| 178 std::vector<T>* out) { | 158 std::vector<T>* out) { |
| 179 if (!val->IsArray()) | 159 if (!val->IsArray()) |
| 180 return false; | 160 return false; |
| 181 | 161 |
| 182 std::vector<T> result; | 162 std::vector<T> result; |
| 183 v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); | 163 v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); |
| 184 uint32_t length = array->Length(); | 164 uint32_t length = array->Length(); |
| 185 for (uint32_t i = 0; i < length; ++i) { | 165 for (uint32_t i = 0; i < length; ++i) { |
| 186 v8::Local<v8::Value> v8_item; | |
| 187 if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item)) | |
| 188 return false; | |
| 189 T item; | 166 T item; |
| 190 if (!Converter<T>::FromV8(isolate, v8_item, &item)) | 167 if (!Converter<T>::FromV8(isolate, array->Get(i), &item)) |
| 191 return false; | 168 return false; |
| 192 result.push_back(item); | 169 result.push_back(item); |
| 193 } | 170 } |
| 194 | 171 |
| 195 out->swap(result); | 172 out->swap(result); |
| 196 return true; | 173 return true; |
| 197 } | 174 } |
| 198 }; | 175 }; |
| 199 | 176 |
| 200 template<typename T> | |
| 201 struct ToV8ReturnsMaybe<std::vector<T>> { | |
| 202 static const bool value = true; | |
| 203 }; | |
| 204 | |
| 205 // Convenience functions that deduce T. | 177 // Convenience functions that deduce T. |
| 206 template<typename T> | 178 template<typename T> |
| 207 v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) { | 179 v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) { |
| 208 return Converter<T>::ToV8(isolate, input); | 180 return Converter<T>::ToV8(isolate, input); |
| 209 } | 181 } |
| 210 | 182 |
| 211 template<typename T> | |
| 212 v8::MaybeLocal<v8::Value> ConvertToV8(v8::Local<v8::Context> context, T input) { | |
| 213 return Converter<T>::ToV8(context, input); | |
| 214 } | |
| 215 | |
| 216 template<typename T, bool = ToV8ReturnsMaybe<T>::value> struct ToV8Traits; | |
| 217 | |
| 218 template <typename T> | |
| 219 struct ToV8Traits<T, true> { | |
| 220 static bool TryConvertToV8(v8::Isolate* isolate, | |
| 221 T input, | |
| 222 v8::Local<v8::Value>* output) { | |
| 223 auto maybe = ConvertToV8(isolate->GetCurrentContext(), input); | |
| 224 if (maybe.IsEmpty()) | |
| 225 return false; | |
| 226 *output = maybe.ToLocalChecked(); | |
| 227 return true; | |
| 228 } | |
| 229 }; | |
| 230 | |
| 231 template <typename T> | |
| 232 struct ToV8Traits<T, false> { | |
| 233 static bool TryConvertToV8(v8::Isolate* isolate, | |
| 234 T input, | |
| 235 v8::Local<v8::Value>* output) { | |
| 236 *output = ConvertToV8(isolate, input); | |
| 237 return true; | |
| 238 } | |
| 239 }; | |
| 240 | |
| 241 template <typename T> | |
| 242 bool TryConvertToV8(v8::Isolate* isolate, | |
| 243 T input, | |
| 244 v8::Local<v8::Value>* output) { | |
| 245 return ToV8Traits<T>::TryConvertToV8(isolate, input, output); | |
| 246 } | |
| 247 | |
| 248 // This crashes when input.size() > v8::String::kMaxLength. | |
| 249 GIN_EXPORT inline v8::Local<v8::String> StringToV8( | 183 GIN_EXPORT inline v8::Local<v8::String> StringToV8( |
| 250 v8::Isolate* isolate, | 184 v8::Isolate* isolate, |
| 251 const base::StringPiece& input) { | 185 const base::StringPiece& input) { |
| 252 return ConvertToV8(isolate, input).As<v8::String>(); | 186 return ConvertToV8(isolate, input).As<v8::String>(); |
| 253 } | 187 } |
| 254 | 188 |
| 255 // This crashes when input.size() > v8::String::kMaxLength. | |
| 256 GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, | 189 GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, |
| 257 const base::StringPiece& val); | 190 const base::StringPiece& val); |
| 258 | 191 |
| 259 template<typename T> | 192 template<typename T> |
| 260 bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, | 193 bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, |
| 261 T* result) { | 194 T* result) { |
| 262 return Converter<T>::FromV8(isolate, input, result); | 195 return Converter<T>::FromV8(isolate, input, result); |
| 263 } | 196 } |
| 264 | 197 |
| 265 GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value); | 198 GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value); |
| 266 | 199 |
| 267 } // namespace gin | 200 } // namespace gin |
| 268 | 201 |
| 269 #endif // GIN_CONVERTER_H_ | 202 #endif // GIN_CONVERTER_H_ |
| OLD | NEW |