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.
5 #ifndef GIN_CONVERTER_H_
6 #define GIN_CONVERTER_H_
11 #include "base/strings/string_piece.h"
12 #include "gin/gin_export.h"
13 #include "v8/include/v8.h"
17 template<typename T
, typename Enable
= void>
21 struct GIN_EXPORT Converter
<bool> {
22 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
24 static bool FromV8(v8::Isolate
* isolate
,
25 v8::Handle
<v8::Value
> val
,
30 struct GIN_EXPORT Converter
<int32_t> {
31 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
33 static bool FromV8(v8::Isolate
* isolate
,
34 v8::Handle
<v8::Value
> val
,
39 struct GIN_EXPORT Converter
<uint32_t> {
40 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
42 static bool FromV8(v8::Isolate
* isolate
,
43 v8::Handle
<v8::Value
> val
,
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
,
52 static bool FromV8(v8::Isolate
* isolate
,
53 v8::Handle
<v8::Value
> val
,
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
,
62 static bool FromV8(v8::Isolate
* isolate
,
63 v8::Handle
<v8::Value
> val
,
68 struct GIN_EXPORT Converter
<float> {
69 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
71 static bool FromV8(v8::Isolate
* isolate
,
72 v8::Handle
<v8::Value
> val
,
77 struct GIN_EXPORT Converter
<double> {
78 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
80 static bool FromV8(v8::Isolate
* isolate
,
81 v8::Handle
<v8::Value
> val
,
86 struct GIN_EXPORT Converter
<base::StringPiece
> {
87 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
88 const base::StringPiece
& val
);
89 // No conversion out is possible because StringPiece does not contain storage.
93 struct GIN_EXPORT Converter
<std::string
> {
94 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
95 const std::string
& val
);
96 static bool FromV8(v8::Isolate
* isolate
,
97 v8::Handle
<v8::Value
> val
,
102 struct GIN_EXPORT Converter
<v8::Handle
<v8::Function
> > {
103 static bool FromV8(v8::Isolate
* isolate
,
104 v8::Handle
<v8::Value
> val
,
105 v8::Handle
<v8::Function
>* out
);
109 struct GIN_EXPORT Converter
<v8::Handle
<v8::Object
> > {
110 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
111 v8::Handle
<v8::Object
> val
);
112 static bool FromV8(v8::Isolate
* isolate
,
113 v8::Handle
<v8::Value
> val
,
114 v8::Handle
<v8::Object
>* out
);
118 struct GIN_EXPORT Converter
<v8::Handle
<v8::ArrayBuffer
> > {
119 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
120 v8::Handle
<v8::ArrayBuffer
> val
);
121 static bool FromV8(v8::Isolate
* isolate
,
122 v8::Handle
<v8::Value
> val
,
123 v8::Handle
<v8::ArrayBuffer
>* out
);
127 struct GIN_EXPORT Converter
<v8::Handle
<v8::External
> > {
128 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
129 v8::Handle
<v8::External
> val
);
130 static bool FromV8(v8::Isolate
* isolate
,
131 v8::Handle
<v8::Value
> val
,
132 v8::Handle
<v8::External
>* out
);
136 struct GIN_EXPORT Converter
<v8::Handle
<v8::Value
> > {
137 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
138 v8::Handle
<v8::Value
> val
);
139 static bool FromV8(v8::Isolate
* isolate
,
140 v8::Handle
<v8::Value
> val
,
141 v8::Handle
<v8::Value
>* out
);
145 struct Converter
<std::vector
<T
> > {
146 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
147 const std::vector
<T
>& val
) {
148 v8::Handle
<v8::Array
> result(
149 v8::Array::New(isolate
, static_cast<int>(val
.size())));
150 for (size_t i
= 0; i
< val
.size(); ++i
) {
151 result
->Set(static_cast<int>(i
), Converter
<T
>::ToV8(isolate
, val
[i
]));
156 static bool FromV8(v8::Isolate
* isolate
,
157 v8::Handle
<v8::Value
> val
,
158 std::vector
<T
>* out
) {
162 std::vector
<T
> result
;
163 v8::Handle
<v8::Array
> array(v8::Handle
<v8::Array
>::Cast(val
));
164 uint32_t length
= array
->Length();
165 for (uint32_t i
= 0; i
< length
; ++i
) {
167 if (!Converter
<T
>::FromV8(isolate
, array
->Get(i
), &item
))
169 result
.push_back(item
);
177 // Convenience functions that deduce T.
179 v8::Handle
<v8::Value
> ConvertToV8(v8::Isolate
* isolate
,
181 return Converter
<T
>::ToV8(isolate
, input
);
184 GIN_EXPORT
inline v8::Handle
<v8::String
> StringToV8(
185 v8::Isolate
* isolate
,
186 const base::StringPiece
& input
) {
187 return ConvertToV8(isolate
, input
).As
<v8::String
>();
190 GIN_EXPORT
v8::Handle
<v8::String
> StringToSymbol(v8::Isolate
* isolate
,
191 const base::StringPiece
& val
);
194 bool ConvertFromV8(v8::Isolate
* isolate
, v8::Handle
<v8::Value
> input
,
196 return Converter
<T
>::FromV8(isolate
, input
, result
);
199 GIN_EXPORT
std::string
V8ToString(v8::Handle
<v8::Value
> value
);
203 #endif // GIN_CONVERTER_H_