blob: eb84be213d14e1a36ef5db44833a7dd1b6437ea9 [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<>
43struct Converter<double> {
44 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
45 double val);
46 static bool FromV8(v8::Handle<v8::Value> val,
47 double* out);
48};
49
50template<>
51struct Converter<std::string> {
52 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
53 const std::string& val);
54 static bool FromV8(v8::Handle<v8::Value> val,
55 std::string* out);
56};
57
58template<>
59struct Converter<v8::Handle<v8::Function> > {
60 static bool FromV8(v8::Handle<v8::Value> val,
61 v8::Handle<v8::Function>* out);
62};
63
64template<>
65struct Converter<v8::Handle<v8::Object> > {
66 static v8::Handle<v8::Value> ToV8(v8::Handle<v8::Object> val);
67 static bool FromV8(v8::Handle<v8::Value> val,
68 v8::Handle<v8::Object>* out);
69};
70
71template<typename T>
72struct Converter<std::vector<T> > {
73 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
74 const std::vector<T>& val) {
75 v8::Handle<v8::Array> result(v8::Array::New(static_cast<int>(val.size())));
76 for (size_t i = 0; i < val.size(); ++i) {
77 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
78 }
79 return result;
80 }
81
82 static bool FromV8(v8::Handle<v8::Value> val,
83 std::vector<T>* out) {
84 if (!val->IsArray())
85 return false;
86
87 std::vector<T> result;
88 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
89 uint32_t length = array->Length();
90 for (uint32_t i = 0; i < length; ++i) {
91 T item;
92 if (!Converter<T>::FromV8(array->Get(i), &item))
93 return false;
94 result.push_back(item);
95 }
96
97 out->swap(result);
98 return true;
99 }
100};
101
102// Convenience functions that deduce T.
103template<typename T>
104v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
105 T input) {
106 return Converter<T>::ToV8(isolate, input);
107}
108
109inline v8::Handle<v8::String> StringToV8(
110 v8::Isolate* isolate, std::string input) {
111 return ConvertToV8(isolate, input).As<v8::String>();
112}
113
114template<typename T>
115bool ConvertFromV8(v8::Handle<v8::Value> input, T* result) {
116 return Converter<T>::FromV8(input, result);
117}
118
119} // namespace gin
120
121#endif // GIN_CONVERTER_H_