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/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "gin/gin_export.h" |
12 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
13 | 14 |
14 namespace gin { | 15 namespace gin { |
15 | 16 |
16 template<typename T, typename Enable = void> | 17 template<typename T, typename Enable = void> |
17 struct Converter {}; | 18 struct Converter {}; |
18 | 19 |
19 template<> | 20 template<> |
20 struct Converter<bool> { | 21 struct GIN_EXPORT Converter<bool> { |
21 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 22 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
22 bool val); | 23 bool val); |
23 static bool FromV8(v8::Isolate* isolate, | 24 static bool FromV8(v8::Isolate* isolate, |
24 v8::Handle<v8::Value> val, | 25 v8::Handle<v8::Value> val, |
25 bool* out); | 26 bool* out); |
26 }; | 27 }; |
27 | 28 |
28 template<> | 29 template<> |
29 struct Converter<int32_t> { | 30 struct GIN_EXPORT Converter<int32_t> { |
30 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 31 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
31 int32_t val); | 32 int32_t val); |
32 static bool FromV8(v8::Isolate* isolate, | 33 static bool FromV8(v8::Isolate* isolate, |
33 v8::Handle<v8::Value> val, | 34 v8::Handle<v8::Value> val, |
34 int32_t* out); | 35 int32_t* out); |
35 }; | 36 }; |
36 | 37 |
37 template<> | 38 template<> |
38 struct Converter<uint32_t> { | 39 struct GIN_EXPORT Converter<uint32_t> { |
39 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 40 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
40 uint32_t val); | 41 uint32_t val); |
41 static bool FromV8(v8::Isolate* isolate, | 42 static bool FromV8(v8::Isolate* isolate, |
42 v8::Handle<v8::Value> val, | 43 v8::Handle<v8::Value> val, |
43 uint32_t* out); | 44 uint32_t* out); |
44 }; | 45 }; |
45 | 46 |
46 template<> | 47 template<> |
47 struct Converter<int64_t> { | 48 struct GIN_EXPORT Converter<int64_t> { |
48 // Warning: JavaScript cannot represent 64 integers precisely. | 49 // Warning: JavaScript cannot represent 64 integers precisely. |
49 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 50 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
50 int64_t val); | 51 int64_t val); |
51 static bool FromV8(v8::Isolate* isolate, | 52 static bool FromV8(v8::Isolate* isolate, |
52 v8::Handle<v8::Value> val, | 53 v8::Handle<v8::Value> val, |
53 int64_t* out); | 54 int64_t* out); |
54 }; | 55 }; |
55 | 56 |
56 template<> | 57 template<> |
57 struct Converter<uint64_t> { | 58 struct GIN_EXPORT Converter<uint64_t> { |
58 // Warning: JavaScript cannot represent 64 integers precisely. | 59 // Warning: JavaScript cannot represent 64 integers precisely. |
59 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 60 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
60 uint64_t val); | 61 uint64_t val); |
61 static bool FromV8(v8::Isolate* isolate, | 62 static bool FromV8(v8::Isolate* isolate, |
62 v8::Handle<v8::Value> val, | 63 v8::Handle<v8::Value> val, |
63 uint64_t* out); | 64 uint64_t* out); |
64 }; | 65 }; |
65 | 66 |
66 template<> | 67 template<> |
67 struct Converter<double> { | 68 struct GIN_EXPORT Converter<double> { |
68 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 69 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
69 double val); | 70 double val); |
70 static bool FromV8(v8::Isolate* isolate, | 71 static bool FromV8(v8::Isolate* isolate, |
71 v8::Handle<v8::Value> val, | 72 v8::Handle<v8::Value> val, |
72 double* out); | 73 double* out); |
73 }; | 74 }; |
74 | 75 |
75 template<> | 76 template<> |
76 struct Converter<base::StringPiece> { | 77 struct GIN_EXPORT Converter<base::StringPiece> { |
77 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 78 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
78 const base::StringPiece& val); | 79 const base::StringPiece& val); |
79 // No conversion out is possible because StringPiece does not contain storage. | 80 // No conversion out is possible because StringPiece does not contain storage. |
80 }; | 81 }; |
81 | 82 |
82 template<> | 83 template<> |
83 struct Converter<std::string> { | 84 struct GIN_EXPORT Converter<std::string> { |
84 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 85 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
85 const std::string& val); | 86 const std::string& val); |
86 static bool FromV8(v8::Isolate* isolate, | 87 static bool FromV8(v8::Isolate* isolate, |
87 v8::Handle<v8::Value> val, | 88 v8::Handle<v8::Value> val, |
88 std::string* out); | 89 std::string* out); |
89 }; | 90 }; |
90 | 91 |
91 template<> | 92 template<> |
92 struct Converter<v8::Handle<v8::Function> > { | 93 struct GIN_EXPORT Converter<v8::Handle<v8::Function> > { |
93 static bool FromV8(v8::Isolate* isolate, | 94 static bool FromV8(v8::Isolate* isolate, |
94 v8::Handle<v8::Value> val, | 95 v8::Handle<v8::Value> val, |
95 v8::Handle<v8::Function>* out); | 96 v8::Handle<v8::Function>* out); |
96 }; | 97 }; |
97 | 98 |
98 template<> | 99 template<> |
99 struct Converter<v8::Handle<v8::Object> > { | 100 struct GIN_EXPORT Converter<v8::Handle<v8::Object> > { |
100 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 101 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
101 v8::Handle<v8::Object> val); | 102 v8::Handle<v8::Object> val); |
102 static bool FromV8(v8::Isolate* isolate, | 103 static bool FromV8(v8::Isolate* isolate, |
103 v8::Handle<v8::Value> val, | 104 v8::Handle<v8::Value> val, |
104 v8::Handle<v8::Object>* out); | 105 v8::Handle<v8::Object>* out); |
105 }; | 106 }; |
106 | 107 |
107 template<> | 108 template<> |
108 struct Converter<v8::Handle<v8::ArrayBuffer> > { | 109 struct GIN_EXPORT Converter<v8::Handle<v8::ArrayBuffer> > { |
109 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 110 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
110 v8::Handle<v8::ArrayBuffer> val); | 111 v8::Handle<v8::ArrayBuffer> val); |
111 static bool FromV8(v8::Isolate* isolate, | 112 static bool FromV8(v8::Isolate* isolate, |
112 v8::Handle<v8::Value> val, | 113 v8::Handle<v8::Value> val, |
113 v8::Handle<v8::ArrayBuffer>* out); | 114 v8::Handle<v8::ArrayBuffer>* out); |
114 }; | 115 }; |
115 | 116 |
116 template<> | 117 template<> |
117 struct Converter<v8::Handle<v8::External> > { | 118 struct GIN_EXPORT Converter<v8::Handle<v8::External> > { |
118 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 119 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
119 v8::Handle<v8::External> val); | 120 v8::Handle<v8::External> val); |
120 static bool FromV8(v8::Isolate* isolate, | 121 static bool FromV8(v8::Isolate* isolate, |
121 v8::Handle<v8::Value> val, | 122 v8::Handle<v8::Value> val, |
122 v8::Handle<v8::External>* out); | 123 v8::Handle<v8::External>* out); |
123 }; | 124 }; |
124 | 125 |
125 template<> | 126 template<> |
126 struct Converter<v8::Handle<v8::Value> > { | 127 struct GIN_EXPORT Converter<v8::Handle<v8::Value> > { |
127 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 128 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
128 v8::Handle<v8::Value> val); | 129 v8::Handle<v8::Value> val); |
129 static bool FromV8(v8::Isolate* isolate, | 130 static bool FromV8(v8::Isolate* isolate, |
130 v8::Handle<v8::Value> val, | 131 v8::Handle<v8::Value> val, |
131 v8::Handle<v8::Value>* out); | 132 v8::Handle<v8::Value>* out); |
132 }; | 133 }; |
133 | 134 |
134 template<typename T> | 135 template<typename T> |
135 struct Converter<std::vector<T> > { | 136 struct Converter<std::vector<T> > { |
136 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 137 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
(...skipping 27 matching lines...) Expand all Loading... |
164 } | 165 } |
165 }; | 166 }; |
166 | 167 |
167 // Convenience functions that deduce T. | 168 // Convenience functions that deduce T. |
168 template<typename T> | 169 template<typename T> |
169 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, | 170 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, |
170 T input) { | 171 T input) { |
171 return Converter<T>::ToV8(isolate, input); | 172 return Converter<T>::ToV8(isolate, input); |
172 } | 173 } |
173 | 174 |
174 inline v8::Handle<v8::String> StringToV8(v8::Isolate* isolate, | 175 GIN_EXPORT inline v8::Handle<v8::String> StringToV8( |
175 const base::StringPiece& input) { | 176 v8::Isolate* isolate, |
| 177 const base::StringPiece& input) { |
176 return ConvertToV8(isolate, input).As<v8::String>(); | 178 return ConvertToV8(isolate, input).As<v8::String>(); |
177 } | 179 } |
178 | 180 |
179 v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate, | 181 GIN_EXPORT v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate, |
180 const base::StringPiece& val); | 182 const base::StringPiece& val); |
181 | 183 |
182 template<typename T> | 184 template<typename T> |
183 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input, | 185 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input, |
184 T* result) { | 186 T* result) { |
185 return Converter<T>::FromV8(isolate, input, result); | 187 return Converter<T>::FromV8(isolate, input, result); |
186 } | 188 } |
187 | 189 |
188 std::string V8ToString(v8::Handle<v8::Value> value); | 190 GIN_EXPORT std::string V8ToString(v8::Handle<v8::Value> value); |
189 | 191 |
190 } // namespace gin | 192 } // namespace gin |
191 | 193 |
192 #endif // GIN_CONVERTER_H_ | 194 #endif // GIN_CONVERTER_H_ |
OLD | NEW |