Revert of https://codereview.chromium.org/94133003/
[chromium-blink-merge.git] / gin / converter.h
blobd10f315d1a303b2616e99353f5821537935753ed
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_
8 #include <string>
9 #include <vector>
11 #include "base/strings/string_piece.h"
12 #include "v8/include/v8.h"
14 namespace gin {
16 template<typename T>
17 struct Converter {};
19 template<>
20 struct Converter<bool> {
21 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
22 bool val);
23 static bool FromV8(v8::Isolate* isolate,
24 v8::Handle<v8::Value> val,
25 bool* out);
28 template<>
29 struct Converter<int32_t> {
30 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
31 int32_t val);
32 static bool FromV8(v8::Isolate* isolate,
33 v8::Handle<v8::Value> val,
34 int32_t* out);
37 template<>
38 struct Converter<uint32_t> {
39 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
40 uint32_t val);
41 static bool FromV8(v8::Isolate* isolate,
42 v8::Handle<v8::Value> val,
43 uint32_t* out);
46 template<>
47 struct Converter<int64_t> {
48 // Warning: JavaScript cannot represent 64 integers precisely.
49 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
50 int64_t val);
51 static bool FromV8(v8::Isolate* isolate,
52 v8::Handle<v8::Value> val,
53 int64_t* out);
56 template<>
57 struct Converter<uint64_t> {
58 // Warning: JavaScript cannot represent 64 integers precisely.
59 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
60 uint64_t val);
61 static bool FromV8(v8::Isolate* isolate,
62 v8::Handle<v8::Value> val,
63 uint64_t* out);
66 template<>
67 struct Converter<double> {
68 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
69 double val);
70 static bool FromV8(v8::Isolate* isolate,
71 v8::Handle<v8::Value> val,
72 double* out);
75 template<>
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.
82 template<>
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,
88 std::string* out);
91 template<>
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);
98 template<>
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);
107 template<>
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);
116 template<>
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);
125 template<>
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);
134 template<typename T>
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]));
143 return result;
146 static bool FromV8(v8::Isolate* isolate,
147 v8::Handle<v8::Value> val,
148 std::vector<T>* out) {
149 if (!val->IsArray())
150 return false;
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) {
156 T item;
157 if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
158 return false;
159 result.push_back(item);
162 out->swap(result);
163 return true;
167 // Convenience functions that deduce T.
168 template<typename T>
169 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
170 T input) {
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);
182 template<typename T>
183 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input,
184 T* result) {
185 return Converter<T>::FromV8(isolate, input, result);
188 std::string V8ToString(v8::Handle<v8::Value> value);
190 } // namespace gin
192 #endif // GIN_CONVERTER_H_