| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 SKY_ENGINE_TONIC_DART_CONVERTER_H_ | 5 #ifndef SKY_ENGINE_TONIC_DART_CONVERTER_WTF_H_ |
| 6 #define SKY_ENGINE_TONIC_DART_CONVERTER_H_ | 6 #define SKY_ENGINE_TONIC_DART_CONVERTER_WTF_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include "tonic/dart_state.h" | 9 #include "sky/engine/wtf/text/StringUTF8Adaptor.h" |
| 10 #include "sky/engine/wtf/text/WTFString.h" |
| 11 #include "tonic/dart_converter.h" |
| 10 #include "tonic/dart_string.h" | 12 #include "tonic/dart_string.h" |
| 11 #include "tonic/dart_string_cache.h" | 13 #include "tonic/dart_string_cache.h" |
| 12 #include "tonic/dart_value.h" | 14 #include "tonic/dart_value.h" |
| 13 #include "sky/engine/wtf/text/StringUTF8Adaptor.h" | |
| 14 #include "sky/engine/wtf/text/WTFString.h" | |
| 15 | 15 |
| 16 namespace blink { | 16 namespace blink { |
| 17 | 17 |
| 18 // DartConvert converts types back and forth from Sky to Dart. The template | |
| 19 // parameter |T| determines what kind of type conversion to perform. | |
| 20 template <typename T, typename Enable = void> | |
| 21 struct DartConverter { | |
| 22 }; | |
| 23 | |
| 24 // This is to work around the fact that typedefs do not create new types. If you | |
| 25 // have a typedef, and want it to use a different converter, specialize this | |
| 26 // template and override the types here. | |
| 27 // Ex: | |
| 28 // typedef int ColorType; // Want to use a different converter. | |
| 29 // class ColorConverterType {}; // Dummy type. | |
| 30 // template<> struct DartConvertType<ColorConverterType> { | |
| 31 // using ConverterType = ColorConverterType; | |
| 32 // using ValueType = ColorType; | |
| 33 // }; | |
| 34 template <typename T> | |
| 35 struct DartConverterTypes { | |
| 36 using ConverterType = T; | |
| 37 using ValueType = T; | |
| 38 }; | |
| 39 | |
| 40 //////////////////////////////////////////////////////////////////////////////// | |
| 41 // Boolean | |
| 42 | |
| 43 template <> | |
| 44 struct DartConverter<bool> { | |
| 45 static Dart_Handle ToDart(bool val) { return Dart_NewBoolean(val); } | |
| 46 | |
| 47 static void SetReturnValue(Dart_NativeArguments args, bool val) { | |
| 48 Dart_SetBooleanReturnValue(args, val); | |
| 49 } | |
| 50 | |
| 51 static bool FromDart(Dart_Handle handle) { | |
| 52 bool result = 0; | |
| 53 Dart_BooleanValue(handle, &result); | |
| 54 return result; | |
| 55 } | |
| 56 | |
| 57 static bool FromArguments(Dart_NativeArguments args, | |
| 58 int index, | |
| 59 Dart_Handle& exception) { | |
| 60 bool result = false; | |
| 61 Dart_GetNativeBooleanArgument(args, index, &result); | |
| 62 return result; | |
| 63 } | |
| 64 }; | |
| 65 | |
| 66 //////////////////////////////////////////////////////////////////////////////// | |
| 67 // Numbers | |
| 68 | |
| 69 template <typename T> | |
| 70 struct DartConverterInteger { | |
| 71 static Dart_Handle ToDart(T val) { return Dart_NewInteger(val); } | |
| 72 | |
| 73 static void SetReturnValue(Dart_NativeArguments args, T val) { | |
| 74 Dart_SetIntegerReturnValue(args, val); | |
| 75 } | |
| 76 | |
| 77 static T FromDart(Dart_Handle handle) { | |
| 78 int64_t result = 0; | |
| 79 Dart_IntegerToInt64(handle, &result); | |
| 80 return static_cast<T>(result); | |
| 81 } | |
| 82 | |
| 83 static T FromArguments(Dart_NativeArguments args, | |
| 84 int index, | |
| 85 Dart_Handle& exception) { | |
| 86 int64_t result = 0; | |
| 87 Dart_GetNativeIntegerArgument(args, index, &result); | |
| 88 return static_cast<T>(result); | |
| 89 } | |
| 90 }; | |
| 91 | |
| 92 template <> | |
| 93 struct DartConverter<int> : public DartConverterInteger<int> {}; | |
| 94 | |
| 95 template <> | |
| 96 struct DartConverter<unsigned> : public DartConverterInteger<unsigned> {}; | |
| 97 | |
| 98 template <> | |
| 99 struct DartConverter<long long> : public DartConverterInteger<long long> {}; | |
| 100 | |
| 101 template <> | |
| 102 struct DartConverter<unsigned long long> { | |
| 103 static Dart_Handle ToDart(unsigned long long val) { | |
| 104 // FIXME: WebIDL unsigned long long is guaranteed to fit into 64-bit | |
| 105 // unsigned, | |
| 106 // so we need a dart API for constructing an integer from uint64_t. | |
| 107 DCHECK(val <= 0x7fffffffffffffffLL); | |
| 108 return Dart_NewInteger(static_cast<int64_t>(val)); | |
| 109 } | |
| 110 | |
| 111 static void SetReturnValue(Dart_NativeArguments args, | |
| 112 unsigned long long val) { | |
| 113 DCHECK(val <= 0x7fffffffffffffffLL); | |
| 114 Dart_SetIntegerReturnValue(args, val); | |
| 115 } | |
| 116 | |
| 117 static unsigned long long FromDart(Dart_Handle handle) { | |
| 118 int64_t result = 0; | |
| 119 Dart_IntegerToInt64(handle, &result); | |
| 120 return result; | |
| 121 } | |
| 122 | |
| 123 static unsigned long long FromArguments(Dart_NativeArguments args, | |
| 124 int index, | |
| 125 Dart_Handle& exception) { | |
| 126 int64_t result = 0; | |
| 127 Dart_GetNativeIntegerArgument(args, index, &result); | |
| 128 return result; | |
| 129 } | |
| 130 }; | |
| 131 | |
| 132 template <typename T> | |
| 133 struct DartConverterFloatingPoint { | |
| 134 static Dart_Handle ToDart(T val) { return Dart_NewDouble(val); } | |
| 135 | |
| 136 static void SetReturnValue(Dart_NativeArguments args, T val) { | |
| 137 Dart_SetDoubleReturnValue(args, val); | |
| 138 } | |
| 139 | |
| 140 static T FromDart(Dart_Handle handle) { | |
| 141 double result = 0; | |
| 142 Dart_DoubleValue(handle, &result); | |
| 143 return result; | |
| 144 } | |
| 145 | |
| 146 static T FromArguments(Dart_NativeArguments args, | |
| 147 int index, | |
| 148 Dart_Handle& exception) { | |
| 149 double result = 0; | |
| 150 Dart_GetNativeDoubleArgument(args, index, &result); | |
| 151 return result; | |
| 152 } | |
| 153 }; | |
| 154 | |
| 155 template <> | |
| 156 struct DartConverter<float> : public DartConverterFloatingPoint<float> {}; | |
| 157 | |
| 158 template <> | |
| 159 struct DartConverter<double> : public DartConverterFloatingPoint<double> {}; | |
| 160 | |
| 161 //////////////////////////////////////////////////////////////////////////////// | |
| 162 // Enums | |
| 163 | |
| 164 template <typename T> | |
| 165 struct DartConverterEnum { | |
| 166 static T FromArguments(Dart_NativeArguments args, | |
| 167 int index, | |
| 168 Dart_Handle& exception) { | |
| 169 Dart_Handle enum_handle = Dart_GetNativeArgument(args, index); | |
| 170 Dart_Handle index_handle = | |
| 171 Dart_GetField(enum_handle, DartState::Current()->index_handle()); | |
| 172 | |
| 173 uint64_t enum_index = 0; | |
| 174 Dart_IntegerToUint64(index_handle, &enum_index); | |
| 175 return static_cast<T>(enum_index); | |
| 176 } | |
| 177 }; | |
| 178 | |
| 179 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
| 180 // Strings | 19 // Strings |
| 181 | 20 |
| 182 template <> | 21 template <> |
| 183 struct DartConverter<String> { | 22 struct DartConverter<String> { |
| 184 static Dart_Handle ToDart(DartState* state, const String& val) { | 23 static Dart_Handle ToDart(DartState* state, const String& val) { |
| 185 if (val.isEmpty()) | 24 if (val.isEmpty()) |
| 186 return Dart_EmptyString(); | 25 return Dart_EmptyString(); |
| 187 return Dart_HandleFromWeakPersistent(state->string_cache().Get(val.impl())); | 26 return Dart_HandleFromWeakPersistent(state->string_cache().Get(val.impl())); |
| 188 } | 27 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 template <typename T> | 190 template <typename T> |
| 352 inline Dart_Handle VectorToDart(const Vector<T>& val) { | 191 inline Dart_Handle VectorToDart(const Vector<T>& val) { |
| 353 return DartConverter<Vector<T>>::ToDart(val); | 192 return DartConverter<Vector<T>>::ToDart(val); |
| 354 } | 193 } |
| 355 | 194 |
| 356 template<typename T> | 195 template<typename T> |
| 357 Dart_Handle ToDart(const T& object) { | 196 Dart_Handle ToDart(const T& object) { |
| 358 return DartConverter<T>::ToDart(object); | 197 return DartConverter<T>::ToDart(object); |
| 359 } | 198 } |
| 360 | 199 |
| 361 //////////////////////////////////////////////////////////////////////////////// | 200 template<typename T> |
| 362 // std::string support (slower, but more convienent for some clients) | 201 struct DartConverter<RefPtr<T>> { |
| 202 static Dart_Handle ToDart(RefPtr<T> val) { |
| 203 return DartConverter<T*>::ToDart(val.get()); |
| 204 } |
| 363 | 205 |
| 364 inline Dart_Handle StdStringToDart(const std::string& val) { | 206 static RefPtr<T> FromDart(Dart_Handle handle) { |
| 365 return Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(val.data()), | 207 return DartConverter<T*>::FromDart(handle); |
| 366 val.length()); | 208 } |
| 367 } | 209 }; |
| 368 | 210 |
| 369 inline std::string StdStringFromDart(Dart_Handle handle) { | 211 #endif // SKY_ENGINE_TONIC_DART_CONVERTER_WTF_H_ |
| 370 String string = StringFromDart(handle); | |
| 371 StringUTF8Adaptor utf8(string); | |
| 372 return std::string(utf8.data(), utf8.length()); | |
| 373 } | |
| 374 | |
| 375 | |
| 376 // Alias Dart_NewStringFromCString for less typing. | |
| 377 inline Dart_Handle ToDart(const char* val) { | |
| 378 return Dart_NewStringFromCString(val); | |
| 379 } | |
| 380 | |
| 381 } // namespace blink | |
| 382 | |
| 383 #endif // SKY_ENGINE_TONIC_DART_CONVERTER_H_ | |
| OLD | NEW |