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 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 11 | #include "base/logging.h" |
aa@chromium.org | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 13 | #include "gin/gin_export.h" |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 14 | #include "v8/include/v8.h" |
| 15 | |
| 16 | namespace gin { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 17 | |
| 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 | }; |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 31 | |
aa@chromium.org | cf76c84a | 2013-12-06 14:07:07 | [diff] [blame] | 32 | template<typename T, typename Enable = void> |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 33 | struct Converter {}; |
| 34 | |
| 35 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 36 | struct GIN_EXPORT Converter<bool> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 37 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 38 | bool val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 39 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 40 | v8::Local<v8::Value> val, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 41 | bool* out); |
| 42 | }; |
| 43 | |
| 44 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 45 | struct GIN_EXPORT Converter<int32_t> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 46 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 47 | int32_t val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 48 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 49 | v8::Local<v8::Value> val, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 50 | int32_t* out); |
| 51 | }; |
| 52 | |
| 53 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 54 | struct GIN_EXPORT Converter<uint32_t> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 55 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 56 | uint32_t val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 57 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 58 | v8::Local<v8::Value> val, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 59 | uint32_t* out); |
| 60 | }; |
| 61 | |
| 62 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 63 | struct GIN_EXPORT Converter<int64_t> { |
abarth@chromium.org | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 64 | // Warning: JavaScript cannot represent 64 integers precisely. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 65 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
abarth@chromium.org | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 66 | int64_t val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 67 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 68 | v8::Local<v8::Value> val, |
abarth@chromium.org | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 69 | int64_t* out); |
| 70 | }; |
| 71 | |
| 72 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 73 | struct GIN_EXPORT Converter<uint64_t> { |
abarth@chromium.org | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 74 | // Warning: JavaScript cannot represent 64 integers precisely. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 75 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
abarth@chromium.org | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 76 | uint64_t val); |
aa@chromium.org | 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, |
abarth@chromium.org | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 79 | uint64_t* out); |
| 80 | }; |
| 81 | |
| 82 | template<> |
aa@chromium.org | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 83 | struct GIN_EXPORT Converter<float> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 84 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
aa@chromium.org | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 85 | float val); |
| 86 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 87 | v8::Local<v8::Value> val, |
aa@chromium.org | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 88 | float* out); |
| 89 | }; |
| 90 | |
| 91 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 92 | struct GIN_EXPORT Converter<double> { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 93 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 94 | double val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 95 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 96 | v8::Local<v8::Value> val, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 97 | double* out); |
aa@chromium.org | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 101 | struct GIN_EXPORT Converter<base::StringPiece> { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 102 | // This crashes when val.size() > v8::String::kMaxLength. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 103 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
aa@chromium.org | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 104 | const base::StringPiece& val); |
| 105 | // No conversion out is possible because StringPiece does not contain storage. |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | template<> |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 109 | struct GIN_EXPORT Converter<std::string> { |
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, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 112 | const std::string& val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 113 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 114 | v8::Local<v8::Value> val, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 115 | std::string* out); |
| 116 | }; |
| 117 | |
| 118 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 119 | struct GIN_EXPORT Converter<v8::Local<v8::Function> > { |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 120 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 121 | v8::Local<v8::Value> val, |
| 122 | v8::Local<v8::Function>* out); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 126 | struct GIN_EXPORT Converter<v8::Local<v8::Object> > { |
| 127 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 128 | v8::Local<v8::Object> val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 129 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 130 | v8::Local<v8::Value> val, |
| 131 | v8::Local<v8::Object>* out); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 132 | }; |
| 133 | |
abarth@chromium.org | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 134 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 135 | struct GIN_EXPORT Converter<v8::Local<v8::ArrayBuffer> > { |
| 136 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 137 | v8::Local<v8::ArrayBuffer> val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 138 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 139 | v8::Local<v8::Value> val, |
| 140 | v8::Local<v8::ArrayBuffer>* out); |
abarth@chromium.org | ec95fdf | 2013-11-24 19:10:11 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 144 | struct GIN_EXPORT Converter<v8::Local<v8::External> > { |
| 145 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 146 | v8::Local<v8::External> val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 147 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 148 | v8::Local<v8::Value> val, |
| 149 | v8::Local<v8::External>* out); |
abarth@chromium.org | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | template<> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 153 | struct GIN_EXPORT Converter<v8::Local<v8::Value> > { |
| 154 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 155 | v8::Local<v8::Value> val); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 156 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 157 | v8::Local<v8::Value> val, |
| 158 | v8::Local<v8::Value>* out); |
abarth@chromium.org | 97f21ca | 2013-11-17 17:46:07 | [diff] [blame] | 159 | }; |
| 160 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 161 | template<typename T> |
| 162 | struct Converter<std::vector<T> > { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 163 | static v8::MaybeLocal<v8::Value> ToV8(v8::Local<v8::Context> context, |
| 164 | const std::vector<T>& val) { |
| 165 | v8::Isolate* isolate = context->GetIsolate(); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 166 | v8::Local<v8::Array> result( |
jochen@chromium.org | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 167 | v8::Array::New(isolate, static_cast<int>(val.size()))); |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 168 | for (uint32_t i = 0; i < val.size(); ++i) { |
| 169 | auto maybe = result->Set(context, i, Converter<T>::ToV8(isolate, val[i])); |
| 170 | if (maybe.IsNothing() || !maybe.FromJust()) |
| 171 | return v8::MaybeLocal<v8::Value>(); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 172 | } |
| 173 | return result; |
| 174 | } |
| 175 | |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 176 | static bool FromV8(v8::Isolate* isolate, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 177 | v8::Local<v8::Value> val, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 178 | std::vector<T>* out) { |
| 179 | if (!val->IsArray()) |
| 180 | return false; |
| 181 | |
| 182 | std::vector<T> result; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 183 | v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val)); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 184 | uint32_t length = array->Length(); |
| 185 | for (uint32_t i = 0; i < length; ++i) { |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 186 | v8::Local<v8::Value> v8_item; |
| 187 | if (!array->Get(isolate->GetCurrentContext(), i).ToLocal(&v8_item)) |
| 188 | return false; |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 189 | T item; |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 190 | if (!Converter<T>::FromV8(isolate, v8_item, &item)) |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 191 | return false; |
| 192 | result.push_back(item); |
| 193 | } |
| 194 | |
| 195 | out->swap(result); |
| 196 | return true; |
| 197 | } |
| 198 | }; |
| 199 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 200 | template<typename T> |
| 201 | struct ToV8ReturnsMaybe<std::vector<T>> { |
| 202 | static const bool value = true; |
| 203 | }; |
| 204 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 205 | // Convenience functions that deduce T. |
| 206 | template<typename T> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 207 | v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) { |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 208 | return Converter<T>::ToV8(isolate, input); |
| 209 | } |
| 210 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 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. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 249 | GIN_EXPORT inline v8::Local<v8::String> StringToV8( |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 250 | v8::Isolate* isolate, |
| 251 | const base::StringPiece& input) { |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 252 | return ConvertToV8(isolate, input).As<v8::String>(); |
| 253 | } |
| 254 | |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame] | 255 | // This crashes when input.size() > v8::String::kMaxLength. |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 256 | GIN_EXPORT v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate, |
jochen@chromium.org | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 257 | const base::StringPiece& val); |
abarth@chromium.org | e87f312 | 2013-11-12 00:41:27 | [diff] [blame] | 258 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 259 | template<typename T> |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 260 | bool ConvertFromV8(v8::Isolate* isolate, v8::Local<v8::Value> input, |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 261 | T* result) { |
| 262 | return Converter<T>::FromV8(isolate, input, result); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 263 | } |
| 264 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 265 | GIN_EXPORT std::string V8ToString(v8::Local<v8::Value> value); |
abarth@chromium.org | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 266 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 267 | } // namespace gin |
| 268 | |
| 269 | #endif // GIN_CONVERTER_H_ |