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_H_ |
6 #define SKY_ENGINE_TONIC_DART_CONVERTER_H_ | 6 #define SKY_ENGINE_TONIC_DART_CONVERTER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include "sky/engine/tonic/dart_state.h" | 9 #include "sky/engine/tonic/dart_state.h" |
10 #include "sky/engine/tonic/dart_string.h" | 10 #include "sky/engine/tonic/dart_string.h" |
11 #include "sky/engine/tonic/dart_string_cache.h" | 11 #include "sky/engine/tonic/dart_string_cache.h" |
12 #include "sky/engine/tonic/dart_value.h" | 12 #include "sky/engine/tonic/dart_value.h" |
13 #include "sky/engine/wtf/text/StringUTF8Adaptor.h" | 13 #include "sky/engine/wtf/text/StringUTF8Adaptor.h" |
14 #include "sky/engine/wtf/text/WTFString.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 | 18 // DartConvert converts types back and forth from Sky to Dart. The template |
19 // parameter |T| determines what kind of type conversion to perform. | 19 // parameter |T| determines what kind of type conversion to perform. |
20 template <typename T, typename Enable = void> | 20 template <typename T, typename Enable = void> |
21 struct DartConverter {}; | 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 }; |
22 | 39 |
23 //////////////////////////////////////////////////////////////////////////////// | 40 //////////////////////////////////////////////////////////////////////////////// |
24 // Boolean | 41 // Boolean |
25 | 42 |
26 template <> | 43 template <> |
27 struct DartConverter<bool> { | 44 struct DartConverter<bool> { |
28 static Dart_Handle ToDart(bool val) { return Dart_NewBoolean(val); } | 45 static Dart_Handle ToDart(bool val) { return Dart_NewBoolean(val); } |
29 | 46 |
30 static void SetReturnValue(Dart_NativeArguments args, bool val) { | 47 static void SetReturnValue(Dart_NativeArguments args, bool val) { |
31 Dart_SetBooleanReturnValue(args, val); | 48 Dart_SetBooleanReturnValue(args, val); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 static Dart_Handle ToDart(DartState* state, const AtomicString& val) { | 237 static Dart_Handle ToDart(DartState* state, const AtomicString& val) { |
221 return DartConverter<String>::ToDart(state, val.string()); | 238 return DartConverter<String>::ToDart(state, val.string()); |
222 } | 239 } |
223 }; | 240 }; |
224 | 241 |
225 //////////////////////////////////////////////////////////////////////////////// | 242 //////////////////////////////////////////////////////////////////////////////// |
226 // Collections | 243 // Collections |
227 | 244 |
228 template <typename T> | 245 template <typename T> |
229 struct DartConverter<Vector<T>> { | 246 struct DartConverter<Vector<T>> { |
230 static Dart_Handle ToDart(const Vector<T>& val) { | 247 using ValueType = typename DartConverterTypes<T>::ValueType; |
| 248 using ConverterType = typename DartConverterTypes<T>::ConverterType; |
| 249 |
| 250 static Dart_Handle ToDart(const Vector<ValueType>& val) { |
231 Dart_Handle list = Dart_NewList(val.size()); | 251 Dart_Handle list = Dart_NewList(val.size()); |
232 if (Dart_IsError(list)) | 252 if (Dart_IsError(list)) |
233 return list; | 253 return list; |
234 for (size_t i = 0; i < val.size(); i++) { | 254 for (size_t i = 0; i < val.size(); i++) { |
235 Dart_Handle result = | 255 Dart_Handle result = |
236 Dart_ListSetAt(list, i, DartConverter<T>::ToDart(val[i])); | 256 Dart_ListSetAt(list, i, |
| 257 DartConverter<ConverterType>::ToDart(val[i])); |
237 if (Dart_IsError(result)) | 258 if (Dart_IsError(result)) |
238 return result; | 259 return result; |
239 } | 260 } |
240 return list; | 261 return list; |
241 } | 262 } |
242 | 263 |
243 static Vector<T> FromDart(Dart_Handle handle) { | 264 static Vector<ValueType> FromDart(Dart_Handle handle) { |
244 Vector<T> result; | 265 Vector<ValueType> result; |
245 if (!Dart_IsList(handle)) | 266 if (!Dart_IsList(handle)) |
246 return result; | 267 return result; |
247 intptr_t length = 0; | 268 intptr_t length = 0; |
248 Dart_ListLength(handle, &length); | 269 Dart_ListLength(handle, &length); |
249 result.reserveCapacity(length); | 270 result.reserveCapacity(length); |
250 for (intptr_t i = 0; i < length; ++i) { | 271 for (intptr_t i = 0; i < length; ++i) { |
251 Dart_Handle item = Dart_ListGetAt(handle, i); | 272 Dart_Handle item = Dart_ListGetAt(handle, i); |
252 DCHECK(!Dart_IsError(item)); | 273 DCHECK(!Dart_IsError(item)); |
253 DCHECK(item); | 274 DCHECK(item); |
254 result.append(DartConverter<T>::FromDart(item)); | 275 result.append(DartConverter<ConverterType>::FromDart(item)); |
255 } | 276 } |
256 return result; | 277 return result; |
257 } | 278 } |
258 | 279 |
259 static Vector<T> FromArguments(Dart_NativeArguments args, | 280 static Vector<ValueType> FromArguments(Dart_NativeArguments args, |
260 int index, | 281 int index, |
261 Dart_Handle& exception, | 282 Dart_Handle& exception, |
262 bool auto_scope = true) { | 283 bool auto_scope = true) { |
263 // TODO(abarth): What should we do with auto_scope? | 284 // TODO(abarth): What should we do with auto_scope? |
264 return FromDart(Dart_GetNativeArgument(args, index)); | 285 return FromDart(Dart_GetNativeArgument(args, index)); |
265 } | 286 } |
266 }; | 287 }; |
267 | 288 |
268 //////////////////////////////////////////////////////////////////////////////// | 289 //////////////////////////////////////////////////////////////////////////////// |
269 // DartValue | 290 // DartValue |
270 | 291 |
271 template <> | 292 template <> |
272 struct DartConverter<DartValue*> { | 293 struct DartConverter<DartValue*> { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 | 356 |
336 | 357 |
337 // Alias Dart_NewStringFromCString for less typing. | 358 // Alias Dart_NewStringFromCString for less typing. |
338 inline Dart_Handle ToDart(const char* val) { | 359 inline Dart_Handle ToDart(const char* val) { |
339 return Dart_NewStringFromCString(val); | 360 return Dart_NewStringFromCString(val); |
340 } | 361 } |
341 | 362 |
342 } // namespace blink | 363 } // namespace blink |
343 | 364 |
344 #endif // SKY_ENGINE_TONIC_DART_CONVERTER_H_ | 365 #endif // SKY_ENGINE_TONIC_DART_CONVERTER_H_ |
OLD | NEW |