blob: d10f315d1a303b2616e99353f5821537935753ed [file] [log] [blame]
[email protected]a22998a2013-11-10 05:00:501// 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
[email protected]b520e132013-11-29 03:21:4811#include "base/strings/string_piece.h"
[email protected]a22998a2013-11-10 05:00:5012#include "v8/include/v8.h"
13
14namespace gin {
15
16template<typename T>
17struct Converter {};
18
19template<>
20struct Converter<bool> {
21 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
22 bool val);
[email protected]7618ebbb2013-11-27 03:38:2623 static bool FromV8(v8::Isolate* isolate,
24 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5025 bool* out);
26};
27
28template<>
29struct Converter<int32_t> {
30 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
31 int32_t val);
[email protected]7618ebbb2013-11-27 03:38:2632 static bool FromV8(v8::Isolate* isolate,
33 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5034 int32_t* out);
35};
36
37template<>
38struct Converter<uint32_t> {
39 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
40 uint32_t val);
[email protected]7618ebbb2013-11-27 03:38:2641 static bool FromV8(v8::Isolate* isolate,
42 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5043 uint32_t* out);
44};
45
46template<>
[email protected]e87f3122013-11-12 00:41:2747struct 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);
[email protected]7618ebbb2013-11-27 03:38:2651 static bool FromV8(v8::Isolate* isolate,
52 v8::Handle<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2753 int64_t* out);
54};
55
56template<>
57struct 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);
[email protected]7618ebbb2013-11-27 03:38:2661 static bool FromV8(v8::Isolate* isolate,
62 v8::Handle<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2763 uint64_t* out);
64};
65
66template<>
[email protected]a22998a2013-11-10 05:00:5067struct Converter<double> {
68 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
69 double val);
[email protected]7618ebbb2013-11-27 03:38:2670 static bool FromV8(v8::Isolate* isolate,
71 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5072 double* out);
73};
74
75template<>
[email protected]b520e132013-11-29 03:21:4876struct 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.
80};
81
82template<>
[email protected]a22998a2013-11-10 05:00:5083struct Converter<std::string> {
84 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
85 const std::string& val);
[email protected]7618ebbb2013-11-27 03:38:2686 static bool FromV8(v8::Isolate* isolate,
87 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5088 std::string* out);
89};
90
91template<>
92struct Converter<v8::Handle<v8::Function> > {
[email protected]7618ebbb2013-11-27 03:38:2693 static bool FromV8(v8::Isolate* isolate,
94 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5095 v8::Handle<v8::Function>* out);
96};
97
98template<>
99struct Converter<v8::Handle<v8::Object> > {
[email protected]e87f3122013-11-12 00:41:27100 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
101 v8::Handle<v8::Object> val);
[email protected]7618ebbb2013-11-27 03:38:26102 static bool FromV8(v8::Isolate* isolate,
103 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:50104 v8::Handle<v8::Object>* out);
105};
106
[email protected]97f21ca2013-11-17 17:46:07107template<>
[email protected]ec95fdf2013-11-24 19:10:11108struct Converter<v8::Handle<v8::ArrayBuffer> > {
109 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
110 v8::Handle<v8::ArrayBuffer> val);
[email protected]7618ebbb2013-11-27 03:38:26111 static bool FromV8(v8::Isolate* isolate,
112 v8::Handle<v8::Value> val,
[email protected]ec95fdf2013-11-24 19:10:11113 v8::Handle<v8::ArrayBuffer>* out);
114};
115
116template<>
[email protected]97f21ca2013-11-17 17:46:07117struct Converter<v8::Handle<v8::External> > {
118 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
119 v8::Handle<v8::External> val);
[email protected]7618ebbb2013-11-27 03:38:26120 static bool FromV8(v8::Isolate* isolate,
121 v8::Handle<v8::Value> val,
[email protected]97f21ca2013-11-17 17:46:07122 v8::Handle<v8::External>* out);
123};
124
125template<>
126struct Converter<v8::Handle<v8::Value> > {
127 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
128 v8::Handle<v8::Value> val);
[email protected]7618ebbb2013-11-27 03:38:26129 static bool FromV8(v8::Isolate* isolate,
130 v8::Handle<v8::Value> val,
[email protected]97f21ca2013-11-17 17:46:07131 v8::Handle<v8::Value>* out);
132};
133
[email protected]a22998a2013-11-10 05:00:50134template<typename T>
135struct Converter<std::vector<T> > {
136 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
137 const std::vector<T>& val) {
[email protected]91cd4fe2013-11-28 09:31:58138 v8::Handle<v8::Array> result(
139 v8::Array::New(isolate, static_cast<int>(val.size())));
[email protected]a22998a2013-11-10 05:00:50140 for (size_t i = 0; i < val.size(); ++i) {
141 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
142 }
143 return result;
144 }
145
[email protected]7618ebbb2013-11-27 03:38:26146 static bool FromV8(v8::Isolate* isolate,
147 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:50148 std::vector<T>* out) {
149 if (!val->IsArray())
150 return false;
151
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;
[email protected]7618ebbb2013-11-27 03:38:26157 if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
[email protected]a22998a2013-11-10 05:00:50158 return false;
159 result.push_back(item);
160 }
161
162 out->swap(result);
163 return true;
164 }
165};
166
167// Convenience functions that deduce T.
168template<typename T>
169v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
170 T input) {
171 return Converter<T>::ToV8(isolate, input);
172}
173
[email protected]7618ebbb2013-11-27 03:38:26174inline v8::Handle<v8::String> StringToV8(v8::Isolate* isolate,
[email protected]b520e132013-11-29 03:21:48175 const base::StringPiece& input) {
[email protected]a22998a2013-11-10 05:00:50176 return ConvertToV8(isolate, input).As<v8::String>();
177}
178
[email protected]e87f3122013-11-12 00:41:27179v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
[email protected]b520e132013-11-29 03:21:48180 const base::StringPiece& val);
[email protected]e87f3122013-11-12 00:41:27181
[email protected]a22998a2013-11-10 05:00:50182template<typename T>
[email protected]7618ebbb2013-11-27 03:38:26183bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input,
184 T* result) {
185 return Converter<T>::FromV8(isolate, input, result);
[email protected]a22998a2013-11-10 05:00:50186}
187
[email protected]2f703422013-11-25 21:26:15188std::string V8ToString(v8::Handle<v8::Value> value);
189
[email protected]a22998a2013-11-10 05:00:50190} // namespace gin
191
192#endif // GIN_CONVERTER_H_