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 "v8/include/v8.h"
16 template<typename T
, typename Enable
= void>
20 struct Converter
<bool> {
21 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
23 static bool FromV8(v8::Isolate
* isolate
,
24 v8::Handle
<v8::Value
> val
,
29 struct Converter
<int32_t> {
30 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
32 static bool FromV8(v8::Isolate
* isolate
,
33 v8::Handle
<v8::Value
> val
,
38 struct Converter
<uint32_t> {
39 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
41 static bool FromV8(v8::Isolate
* isolate
,
42 v8::Handle
<v8::Value
> val
,
47 struct Converter
<int64_t> {
48 // Warning: JavaScript cannot represent 64 integers precisely.
49 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
51 static bool FromV8(v8::Isolate
* isolate
,
52 v8::Handle
<v8::Value
> val
,
57 struct Converter
<uint64_t> {
58 // Warning: JavaScript cannot represent 64 integers precisely.
59 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
61 static bool FromV8(v8::Isolate
* isolate
,
62 v8::Handle
<v8::Value
> val
,
67 struct Converter
<double> {
68 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
70 static bool FromV8(v8::Isolate
* isolate
,
71 v8::Handle
<v8::Value
> val
,
76 struct Converter
<base::StringPiece
> {
77 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
78 const base::StringPiece
& val
);
79 // No conversion out is possible because StringPiece does not contain storage.
83 struct Converter
<std::string
> {
84 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
85 const std::string
& val
);
86 static bool FromV8(v8::Isolate
* isolate
,
87 v8::Handle
<v8::Value
> val
,
92 struct Converter
<v8::Handle
<v8::Function
> > {
93 static bool FromV8(v8::Isolate
* isolate
,
94 v8::Handle
<v8::Value
> val
,
95 v8::Handle
<v8::Function
>* out
);
99 struct Converter
<v8::Handle
<v8::Object
> > {
100 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
101 v8::Handle
<v8::Object
> val
);
102 static bool FromV8(v8::Isolate
* isolate
,
103 v8::Handle
<v8::Value
> val
,
104 v8::Handle
<v8::Object
>* out
);
108 struct Converter
<v8::Handle
<v8::ArrayBuffer
> > {
109 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
110 v8::Handle
<v8::ArrayBuffer
> val
);
111 static bool FromV8(v8::Isolate
* isolate
,
112 v8::Handle
<v8::Value
> val
,
113 v8::Handle
<v8::ArrayBuffer
>* out
);
117 struct Converter
<v8::Handle
<v8::External
> > {
118 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
119 v8::Handle
<v8::External
> val
);
120 static bool FromV8(v8::Isolate
* isolate
,
121 v8::Handle
<v8::Value
> val
,
122 v8::Handle
<v8::External
>* out
);
126 struct Converter
<v8::Handle
<v8::Value
> > {
127 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
128 v8::Handle
<v8::Value
> val
);
129 static bool FromV8(v8::Isolate
* isolate
,
130 v8::Handle
<v8::Value
> val
,
131 v8::Handle
<v8::Value
>* out
);
135 struct Converter
<std::vector
<T
> > {
136 static v8::Handle
<v8::Value
> ToV8(v8::Isolate
* isolate
,
137 const std::vector
<T
>& val
) {
138 v8::Handle
<v8::Array
> result(
139 v8::Array::New(isolate
, static_cast<int>(val
.size())));
140 for (size_t i
= 0; i
< val
.size(); ++i
) {
141 result
->Set(static_cast<int>(i
), Converter
<T
>::ToV8(isolate
, val
[i
]));
146 static bool FromV8(v8::Isolate
* isolate
,
147 v8::Handle
<v8::Value
> val
,
148 std::vector
<T
>* out
) {
152 std::vector
<T
> result
;
153 v8::Handle
<v8::Array
> array(v8::Handle
<v8::Array
>::Cast(val
));
154 uint32_t length
= array
->Length();
155 for (uint32_t i
= 0; i
< length
; ++i
) {
157 if (!Converter
<T
>::FromV8(isolate
, array
->Get(i
), &item
))
159 result
.push_back(item
);
167 // Convenience functions that deduce T.
169 v8::Handle
<v8::Value
> ConvertToV8(v8::Isolate
* isolate
,
171 return Converter
<T
>::ToV8(isolate
, input
);
174 inline v8::Handle
<v8::String
> StringToV8(v8::Isolate
* isolate
,
175 const base::StringPiece
& input
) {
176 return ConvertToV8(isolate
, input
).As
<v8::String
>();
179 v8::Handle
<v8::String
> StringToSymbol(v8::Isolate
* isolate
,
180 const base::StringPiece
& val
);
183 bool ConvertFromV8(v8::Isolate
* isolate
, v8::Handle
<v8::Value
> input
,
185 return Converter
<T
>::FromV8(isolate
, input
, result
);
188 std::string
V8ToString(v8::Handle
<v8::Value
> value
);
192 #endif // GIN_CONVERTER_H_