blob: 4ff7043059706944fc4b6b46115bb541032b877f [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
11#include "v8/include/v8.h"
12
13namespace gin {
14
15template<typename T>
16struct Converter {};
17
18template<>
19struct Converter<bool> {
20 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
21 bool val);
[email protected]7618ebbb2013-11-27 03:38:2622 static bool FromV8(v8::Isolate* isolate,
23 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5024 bool* out);
25};
26
27template<>
28struct Converter<int32_t> {
29 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
30 int32_t val);
[email protected]7618ebbb2013-11-27 03:38:2631 static bool FromV8(v8::Isolate* isolate,
32 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5033 int32_t* out);
34};
35
36template<>
37struct Converter<uint32_t> {
38 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
39 uint32_t val);
[email protected]7618ebbb2013-11-27 03:38:2640 static bool FromV8(v8::Isolate* isolate,
41 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5042 uint32_t* out);
43};
44
45template<>
[email protected]e87f3122013-11-12 00:41:2746struct Converter<int64_t> {
47 // Warning: JavaScript cannot represent 64 integers precisely.
48 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
49 int64_t val);
[email protected]7618ebbb2013-11-27 03:38:2650 static bool FromV8(v8::Isolate* isolate,
51 v8::Handle<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2752 int64_t* out);
53};
54
55template<>
56struct Converter<uint64_t> {
57 // Warning: JavaScript cannot represent 64 integers precisely.
58 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
59 uint64_t val);
[email protected]7618ebbb2013-11-27 03:38:2660 static bool FromV8(v8::Isolate* isolate,
61 v8::Handle<v8::Value> val,
[email protected]e87f3122013-11-12 00:41:2762 uint64_t* out);
63};
64
65template<>
[email protected]a22998a2013-11-10 05:00:5066struct Converter<double> {
67 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
68 double val);
[email protected]7618ebbb2013-11-27 03:38:2669 static bool FromV8(v8::Isolate* isolate,
70 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5071 double* out);
72};
73
74template<>
75struct Converter<std::string> {
76 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
77 const std::string& val);
[email protected]7618ebbb2013-11-27 03:38:2678 static bool FromV8(v8::Isolate* isolate,
79 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5080 std::string* out);
81};
82
83template<>
84struct Converter<v8::Handle<v8::Function> > {
[email protected]7618ebbb2013-11-27 03:38:2685 static bool FromV8(v8::Isolate* isolate,
86 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5087 v8::Handle<v8::Function>* out);
88};
89
90template<>
91struct Converter<v8::Handle<v8::Object> > {
[email protected]e87f3122013-11-12 00:41:2792 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
93 v8::Handle<v8::Object> val);
[email protected]7618ebbb2013-11-27 03:38:2694 static bool FromV8(v8::Isolate* isolate,
95 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:5096 v8::Handle<v8::Object>* out);
97};
98
[email protected]97f21ca2013-11-17 17:46:0799template<>
[email protected]ec95fdf2013-11-24 19:10:11100struct Converter<v8::Handle<v8::ArrayBuffer> > {
101 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
102 v8::Handle<v8::ArrayBuffer> val);
[email protected]7618ebbb2013-11-27 03:38:26103 static bool FromV8(v8::Isolate* isolate,
104 v8::Handle<v8::Value> val,
[email protected]ec95fdf2013-11-24 19:10:11105 v8::Handle<v8::ArrayBuffer>* out);
106};
107
108template<>
[email protected]97f21ca2013-11-17 17:46:07109struct Converter<v8::Handle<v8::External> > {
110 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
111 v8::Handle<v8::External> val);
[email protected]7618ebbb2013-11-27 03:38:26112 static bool FromV8(v8::Isolate* isolate,
113 v8::Handle<v8::Value> val,
[email protected]97f21ca2013-11-17 17:46:07114 v8::Handle<v8::External>* out);
115};
116
117template<>
118struct Converter<v8::Handle<v8::Value> > {
119 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
120 v8::Handle<v8::Value> val);
[email protected]7618ebbb2013-11-27 03:38:26121 static bool FromV8(v8::Isolate* isolate,
122 v8::Handle<v8::Value> val,
[email protected]97f21ca2013-11-17 17:46:07123 v8::Handle<v8::Value>* out);
124};
125
[email protected]a22998a2013-11-10 05:00:50126template<typename T>
127struct Converter<std::vector<T> > {
128 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
129 const std::vector<T>& val) {
[email protected]91cd4fe2013-11-28 09:31:58130 v8::Handle<v8::Array> result(
131 v8::Array::New(isolate, static_cast<int>(val.size())));
[email protected]a22998a2013-11-10 05:00:50132 for (size_t i = 0; i < val.size(); ++i) {
133 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
134 }
135 return result;
136 }
137
[email protected]7618ebbb2013-11-27 03:38:26138 static bool FromV8(v8::Isolate* isolate,
139 v8::Handle<v8::Value> val,
[email protected]a22998a2013-11-10 05:00:50140 std::vector<T>* out) {
141 if (!val->IsArray())
142 return false;
143
144 std::vector<T> result;
145 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
146 uint32_t length = array->Length();
147 for (uint32_t i = 0; i < length; ++i) {
148 T item;
[email protected]7618ebbb2013-11-27 03:38:26149 if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
[email protected]a22998a2013-11-10 05:00:50150 return false;
151 result.push_back(item);
152 }
153
154 out->swap(result);
155 return true;
156 }
157};
158
159// Convenience functions that deduce T.
160template<typename T>
161v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
162 T input) {
163 return Converter<T>::ToV8(isolate, input);
164}
165
[email protected]7618ebbb2013-11-27 03:38:26166inline v8::Handle<v8::String> StringToV8(v8::Isolate* isolate,
167 std::string input) {
[email protected]a22998a2013-11-10 05:00:50168 return ConvertToV8(isolate, input).As<v8::String>();
169}
170
[email protected]e87f3122013-11-12 00:41:27171v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
172 const std::string& val);
173
[email protected]a22998a2013-11-10 05:00:50174template<typename T>
[email protected]7618ebbb2013-11-27 03:38:26175bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input,
176 T* result) {
177 return Converter<T>::FromV8(isolate, input, result);
[email protected]a22998a2013-11-10 05:00:50178}
179
[email protected]2f703422013-11-25 21:26:15180std::string V8ToString(v8::Handle<v8::Value> value);
181
[email protected]a22998a2013-11-10 05:00:50182} // namespace gin
183
184#endif // GIN_CONVERTER_H_