blob: 551849bb07c6adf8aa1b1a9ed81741d3f33cd8e7 [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
90template<typename T>
91struct Converter<std::vector<T> > {
92 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
93 const std::vector<T>& val) {
94 v8::Handle<v8::Array> result(v8::Array::New(static_cast<int>(val.size())));
95 for (size_t i = 0; i < val.size(); ++i) {
96 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
97 }
98 return result;
99 }
100
101 static bool FromV8(v8::Handle<v8::Value> val,
102 std::vector<T>* out) {
103 if (!val->IsArray())
104 return false;
105
106 std::vector<T> result;
107 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
108 uint32_t length = array->Length();
109 for (uint32_t i = 0; i < length; ++i) {
110 T item;
111 if (!Converter<T>::FromV8(array->Get(i), &item))
112 return false;
113 result.push_back(item);
114 }
115
116 out->swap(result);
117 return true;
118 }
119};
120
121// Convenience functions that deduce T.
122template<typename T>
123v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
124 T input) {
125 return Converter<T>::ToV8(isolate, input);
126}
127
128inline v8::Handle<v8::String> StringToV8(
129 v8::Isolate* isolate, std::string input) {
130 return ConvertToV8(isolate, input).As<v8::String>();
131}
132
[email protected]e87f3122013-11-12 00:41:27133v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
134 const std::string& val);
135
[email protected]a22998a2013-11-10 05:00:50136template<typename T>
137bool ConvertFromV8(v8::Handle<v8::Value> input, T* result) {
138 return Converter<T>::FromV8(input, result);
139}
140
141} // namespace gin
142
143#endif // GIN_CONVERTER_H_