Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tonic/dart_converter.h

Issue 1244493002: Wholesale move sky/engine/tonic to tonic (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tonic/dart_class_provider.cc ('k') | tonic/dart_converter.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef SKY_ENGINE_TONIC_DART_CONVERTER_H_
6 #define SKY_ENGINE_TONIC_DART_CONVERTER_H_
7
8 #include <string>
9 #include "tonic/dart_state.h"
10 #include "tonic/dart_string.h"
11 #include "tonic/dart_string_cache.h"
12 #include "tonic/dart_value.h"
13 #include "sky/engine/wtf/text/StringUTF8Adaptor.h"
14 #include "sky/engine/wtf/text/WTFString.h"
15
16 namespace blink {
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 ////////////////////////////////////////////////////////////////////////////////
180 // Strings
181
182 template <>
183 struct DartConverter<String> {
184 static Dart_Handle ToDart(DartState* state, const String& val) {
185 if (val.isEmpty())
186 return Dart_EmptyString();
187 return Dart_HandleFromWeakPersistent(state->string_cache().Get(val.impl()));
188 }
189
190 static void SetReturnValue(Dart_NativeArguments args,
191 const String& val,
192 bool auto_scope = true) {
193 // TODO(abarth): What should we do with auto_scope?
194 if (val.isEmpty()) {
195 Dart_SetReturnValue(args, Dart_EmptyString());
196 return;
197 }
198 DartState* state = DartState::Current();
199 Dart_SetWeakHandleReturnValue(args, state->string_cache().Get(val.impl()));
200 }
201
202 static void SetReturnValueWithNullCheck(Dart_NativeArguments args,
203 const String& val,
204 bool auto_scope = true) {
205 if (val.isNull())
206 Dart_SetReturnValue(args, Dart_Null());
207 else
208 SetReturnValue(args, val, auto_scope);
209 }
210
211 static String FromDart(Dart_Handle handle) {
212 intptr_t char_size = 0;
213 intptr_t length = 0;
214 void* peer = nullptr;
215 Dart_Handle result =
216 Dart_StringGetProperties(handle, &char_size, &length, &peer);
217 if (peer)
218 return String(static_cast<StringImpl*>(peer));
219 if (Dart_IsError(result))
220 return String();
221 return ExternalizeDartString(handle);
222 }
223
224 static String FromArguments(Dart_NativeArguments args,
225 int index,
226 Dart_Handle& exception,
227 bool auto_scope = true) {
228 // TODO(abarth): What should we do with auto_scope?
229 void* peer = nullptr;
230 Dart_Handle handle = Dart_GetNativeStringArgument(args, index, &peer);
231 if (peer)
232 return reinterpret_cast<StringImpl*>(peer);
233 if (Dart_IsError(handle))
234 return String();
235 return ExternalizeDartString(handle);
236 }
237
238 static String FromArgumentsWithNullCheck(Dart_NativeArguments args,
239 int index,
240 Dart_Handle& exception,
241 bool auto_scope = true) {
242 // TODO(abarth): What should we do with auto_scope?
243 void* peer = nullptr;
244 Dart_Handle handle = Dart_GetNativeStringArgument(args, index, &peer);
245 if (peer)
246 return reinterpret_cast<StringImpl*>(peer);
247 if (Dart_IsError(handle) || Dart_IsNull(handle))
248 return String();
249 return ExternalizeDartString(handle);
250 }
251 };
252
253 template <>
254 struct DartConverter<AtomicString> {
255 static Dart_Handle ToDart(DartState* state, const AtomicString& val) {
256 return DartConverter<String>::ToDart(state, val.string());
257 }
258 };
259
260 ////////////////////////////////////////////////////////////////////////////////
261 // Collections
262
263 template <typename T>
264 struct DartConverter<Vector<T>> {
265 using ValueType = typename DartConverterTypes<T>::ValueType;
266 using ConverterType = typename DartConverterTypes<T>::ConverterType;
267
268 static Dart_Handle ToDart(const Vector<ValueType>& val) {
269 Dart_Handle list = Dart_NewList(val.size());
270 if (Dart_IsError(list))
271 return list;
272 for (size_t i = 0; i < val.size(); i++) {
273 Dart_Handle result =
274 Dart_ListSetAt(list, i,
275 DartConverter<ConverterType>::ToDart(val[i]));
276 if (Dart_IsError(result))
277 return result;
278 }
279 return list;
280 }
281
282 static Vector<ValueType> FromDart(Dart_Handle handle) {
283 Vector<ValueType> result;
284 if (!Dart_IsList(handle))
285 return result;
286 intptr_t length = 0;
287 Dart_ListLength(handle, &length);
288 result.reserveCapacity(length);
289 for (intptr_t i = 0; i < length; ++i) {
290 Dart_Handle item = Dart_ListGetAt(handle, i);
291 DCHECK(!Dart_IsError(item));
292 DCHECK(item);
293 result.append(DartConverter<ConverterType>::FromDart(item));
294 }
295 return result;
296 }
297
298 static Vector<ValueType> FromArguments(Dart_NativeArguments args,
299 int index,
300 Dart_Handle& exception,
301 bool auto_scope = true) {
302 // TODO(abarth): What should we do with auto_scope?
303 return FromDart(Dart_GetNativeArgument(args, index));
304 }
305 };
306
307 ////////////////////////////////////////////////////////////////////////////////
308 // DartValue
309
310 template <>
311 struct DartConverter<DartValue*> {
312 static Dart_Handle ToDart(DartState* state, DartValue* val) {
313 return val->dart_value();
314 }
315
316 static void SetReturnValue(Dart_NativeArguments args, DartValue* val) {
317 Dart_SetReturnValue(args, val->dart_value());
318 }
319
320 static PassRefPtr<DartValue> FromDart(Dart_Handle handle) {
321 return DartValue::Create(DartState::Current(), handle);
322 }
323
324 static PassRefPtr<DartValue> FromArguments(Dart_NativeArguments args,
325 int index,
326 Dart_Handle& exception,
327 bool auto_scope = true) {
328 // TODO(abarth): What should we do with auto_scope?
329 return FromDart(Dart_GetNativeArgument(args, index));
330 }
331 };
332
333 ////////////////////////////////////////////////////////////////////////////////
334 // Convience wrappers for commonly used conversions
335
336 inline Dart_Handle StringToDart(DartState* state, const String& val) {
337 return DartConverter<String>::ToDart(state, val);
338 }
339
340 inline Dart_Handle StringToDart(DartState* state, const AtomicString& val) {
341 return DartConverter<AtomicString>::ToDart(state, val);
342 }
343
344 inline String StringFromDart(Dart_Handle handle) {
345 return DartConverter<String>::FromDart(handle);
346 }
347
348 ////////////////////////////////////////////////////////////////////////////////
349 // Convience wrappers using type inference for ease of code generation
350
351 template <typename T>
352 inline Dart_Handle VectorToDart(const Vector<T>& val) {
353 return DartConverter<Vector<T>>::ToDart(val);
354 }
355
356 template<typename T>
357 Dart_Handle ToDart(const T& object) {
358 return DartConverter<T>::ToDart(object);
359 }
360
361 ////////////////////////////////////////////////////////////////////////////////
362 // std::string support (slower, but more convienent for some clients)
363
364 inline Dart_Handle StdStringToDart(const std::string& val) {
365 return Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(val.data()),
366 val.length());
367 }
368
369 inline std::string StdStringFromDart(Dart_Handle handle) {
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_
OLDNEW
« no previous file with comments | « tonic/dart_class_provider.cc ('k') | tonic/dart_converter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698