blob: 33af4522cc1e751ac69e526e4faee9377492df11 [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);
22 static bool FromV8(v8::Handle<v8::Value> val,
23 bool* out);
24};
25
26template<>
27struct Converter<int32_t> {
28 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
29 int32_t val);
30 static bool FromV8(v8::Handle<v8::Value> val,
31 int32_t* out);
32};
33
34template<>
35struct Converter<uint32_t> {
36 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
37 uint32_t val);
38 static bool FromV8(v8::Handle<v8::Value> val,
39 uint32_t* out);
40};
41
42template<>
[email protected]e87f3122013-11-12 00:41:2743struct Converter<int64_t> {
44 // Warning: JavaScript cannot represent 64 integers precisely.
45 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
46 int64_t val);
47 static bool FromV8(v8::Handle<v8::Value> val,
48 int64_t* out);
49};
50
51template<>
52struct Converter<uint64_t> {
53 // Warning: JavaScript cannot represent 64 integers precisely.
54 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
55 uint64_t val);
56 static bool FromV8(v8::Handle<v8::Value> val,
57 uint64_t* out);
58};
59
60template<>
[email protected]a22998a2013-11-10 05:00:5061struct Converter<double> {
62 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
63 double val);
64 static bool FromV8(v8::Handle<v8::Value> val,
65 double* out);
66};
67
68template<>
69struct Converter<std::string> {
70 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
71 const std::string& val);
72 static bool FromV8(v8::Handle<v8::Value> val,
73 std::string* out);
74};
75
76template<>
77struct Converter<v8::Handle<v8::Function> > {
78 static bool FromV8(v8::Handle<v8::Value> val,
79 v8::Handle<v8::Function>* out);
80};
81
82template<>
83struct Converter<v8::Handle<v8::Object> > {
[email protected]e87f3122013-11-12 00:41:2784 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
85 v8::Handle<v8::Object> val);
[email protected]a22998a2013-11-10 05:00:5086 static bool FromV8(v8::Handle<v8::Value> val,
87 v8::Handle<v8::Object>* out);
88};
89
[email protected]97f21ca2013-11-17 17:46:0790template<>
91struct Converter<v8::Handle<v8::External> > {
92 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
93 v8::Handle<v8::External> val);
94 static bool FromV8(v8::Handle<v8::Value> val,
95 v8::Handle<v8::External>* out);
96};
97
98template<>
99struct Converter<v8::Handle<v8::Value> > {
100 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
101 v8::Handle<v8::Value> val);
102 static bool FromV8(v8::Handle<v8::Value> val,
103 v8::Handle<v8::Value>* out);
104};
105
[email protected]a22998a2013-11-10 05:00:50106template<typename T>
107struct Converter<std::vector<T> > {
108 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
109 const std::vector<T>& val) {
110 v8::Handle<v8::Array> result(v8::Array::New(static_cast<int>(val.size())));
111 for (size_t i = 0; i < val.size(); ++i) {
112 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
113 }
114 return result;
115 }
116
117 static bool FromV8(v8::Handle<v8::Value> val,
118 std::vector<T>* out) {
119 if (!val->IsArray())
120 return false;
121
122 std::vector<T> result;
123 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
124 uint32_t length = array->Length();
125 for (uint32_t i = 0; i < length; ++i) {
126 T item;
127 if (!Converter<T>::FromV8(array->Get(i), &item))
128 return false;
129 result.push_back(item);
130 }
131
132 out->swap(result);
133 return true;
134 }
135};
136
137// Convenience functions that deduce T.
138template<typename T>
139v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
140 T input) {
141 return Converter<T>::ToV8(isolate, input);
142}
143
144inline v8::Handle<v8::String> StringToV8(
145 v8::Isolate* isolate, std::string input) {
146 return ConvertToV8(isolate, input).As<v8::String>();
147}
148
[email protected]e87f3122013-11-12 00:41:27149v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
150 const std::string& val);
151
[email protected]a22998a2013-11-10 05:00:50152template<typename T>
153bool ConvertFromV8(v8::Handle<v8::Value> input, T* result) {
154 return Converter<T>::FromV8(input, result);
155}
156
157} // namespace gin
158
159#endif // GIN_CONVERTER_H_