• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "base/strings/string_piece.h"
12 #include "gin/gin_export.h"
13 #include "v8/include/v8.h"
14 
15 namespace gin {
16 
17 template<typename T, typename Enable = void>
18 struct Converter {};
19 
20 template<>
21 struct GIN_EXPORT Converter<bool> {
22   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
23                                     bool val);
24   static bool FromV8(v8::Isolate* isolate,
25                      v8::Handle<v8::Value> val,
26                      bool* out);
27 };
28 
29 template<>
30 struct GIN_EXPORT Converter<int32_t> {
31   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
32                                     int32_t val);
33   static bool FromV8(v8::Isolate* isolate,
34                      v8::Handle<v8::Value> val,
35                      int32_t* out);
36 };
37 
38 template<>
39 struct GIN_EXPORT Converter<uint32_t> {
40   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
41                                     uint32_t val);
42   static bool FromV8(v8::Isolate* isolate,
43                      v8::Handle<v8::Value> val,
44                      uint32_t* out);
45 };
46 
47 template<>
48 struct GIN_EXPORT Converter<int64_t> {
49   // Warning: JavaScript cannot represent 64 integers precisely.
50   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
51                                     int64_t val);
52   static bool FromV8(v8::Isolate* isolate,
53                      v8::Handle<v8::Value> val,
54                      int64_t* out);
55 };
56 
57 template<>
58 struct GIN_EXPORT Converter<uint64_t> {
59   // Warning: JavaScript cannot represent 64 integers precisely.
60   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
61                                     uint64_t val);
62   static bool FromV8(v8::Isolate* isolate,
63                      v8::Handle<v8::Value> val,
64                      uint64_t* out);
65 };
66 
67 template<>
68 struct GIN_EXPORT Converter<double> {
69   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
70                                     double val);
71   static bool FromV8(v8::Isolate* isolate,
72                      v8::Handle<v8::Value> val,
73                      double* out);
74 };
75 
76 template<>
77 struct GIN_EXPORT Converter<base::StringPiece> {
78   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
79                                     const base::StringPiece& val);
80   // No conversion out is possible because StringPiece does not contain storage.
81 };
82 
83 template<>
84 struct GIN_EXPORT Converter<std::string> {
85   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
86                                     const std::string& val);
87   static bool FromV8(v8::Isolate* isolate,
88                      v8::Handle<v8::Value> val,
89                      std::string* out);
90 };
91 
92 template<>
93 struct GIN_EXPORT Converter<v8::Handle<v8::Function> > {
94   static bool FromV8(v8::Isolate* isolate,
95                      v8::Handle<v8::Value> val,
96                      v8::Handle<v8::Function>* out);
97 };
98 
99 template<>
100 struct GIN_EXPORT Converter<v8::Handle<v8::Object> > {
101   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
102                                     v8::Handle<v8::Object> val);
103   static bool FromV8(v8::Isolate* isolate,
104                      v8::Handle<v8::Value> val,
105                      v8::Handle<v8::Object>* out);
106 };
107 
108 template<>
109 struct GIN_EXPORT Converter<v8::Handle<v8::ArrayBuffer> > {
110   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
111                                     v8::Handle<v8::ArrayBuffer> val);
112   static bool FromV8(v8::Isolate* isolate,
113                      v8::Handle<v8::Value> val,
114                      v8::Handle<v8::ArrayBuffer>* out);
115 };
116 
117 template<>
118 struct GIN_EXPORT Converter<v8::Handle<v8::External> > {
119   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
120                                     v8::Handle<v8::External> val);
121   static bool FromV8(v8::Isolate* isolate,
122                      v8::Handle<v8::Value> val,
123                      v8::Handle<v8::External>* out);
124 };
125 
126 template<>
127 struct GIN_EXPORT Converter<v8::Handle<v8::Value> > {
128   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
129                                     v8::Handle<v8::Value> val);
130   static bool FromV8(v8::Isolate* isolate,
131                      v8::Handle<v8::Value> val,
132                      v8::Handle<v8::Value>* out);
133 };
134 
135 template<typename T>
136 struct Converter<std::vector<T> > {
137   static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
138                                     const std::vector<T>& val) {
139     v8::Handle<v8::Array> result(
140         v8::Array::New(isolate, static_cast<int>(val.size())));
141     for (size_t i = 0; i < val.size(); ++i) {
142       result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
143     }
144     return result;
145   }
146 
147   static bool FromV8(v8::Isolate* isolate,
148                      v8::Handle<v8::Value> val,
149                      std::vector<T>* out) {
150     if (!val->IsArray())
151       return false;
152 
153     std::vector<T> result;
154     v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
155     uint32_t length = array->Length();
156     for (uint32_t i = 0; i < length; ++i) {
157       T item;
158       if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
159         return false;
160       result.push_back(item);
161     }
162 
163     out->swap(result);
164     return true;
165   }
166 };
167 
168 // Convenience functions that deduce T.
169 template<typename T>
170 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
171                                   T input) {
172   return Converter<T>::ToV8(isolate, input);
173 }
174 
175 GIN_EXPORT inline v8::Handle<v8::String> StringToV8(
176     v8::Isolate* isolate,
177     const base::StringPiece& input) {
178   return ConvertToV8(isolate, input).As<v8::String>();
179 }
180 
181 GIN_EXPORT v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
182                                                  const base::StringPiece& val);
183 
184 template<typename T>
185 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input,
186                    T* result) {
187   return Converter<T>::FromV8(isolate, input, result);
188 }
189 
190 GIN_EXPORT std::string V8ToString(v8::Handle<v8::Value> value);
191 
192 }  // namespace gin
193 
194 #endif  // GIN_CONVERTER_H_
195