OLD | NEW |
---|---|
(Empty) | |
1 // 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 | |
13 namespace gin { | |
14 | |
15 template<typename T> | |
16 struct Converter {}; | |
17 | |
18 template<> | |
19 struct 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 | |
26 template<> | |
27 struct 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 | |
34 template<> | |
35 struct 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 | |
42 template<> | |
43 struct 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 | |
50 template<> | |
51 struct 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 | |
58 template<> | |
59 struct Converter<v8::Handle<v8::Function> > { | |
60 static bool FromV8(v8::Handle<v8::Value> val, | |
61 v8::Handle<v8::Function>* out); | |
62 }; | |
63 | |
64 template<> | |
65 struct 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 | |
71 template<typename T> | |
72 struct 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. | |
103 template<typename T> | |
104 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, | |
105 T input) { | |
106 return Converter<T>::ToV8(isolate, input); | |
107 } | |
108 | |
109 inline v8::Handle<v8::String> StringToV8( | |
Aaron Boodman
2013/11/09 21:40:35
Why is this needed?
abarth-chromium
2013/11/10 02:12:20
Two reasons:
1) So you can write StringToV8("foo"
| |
110 v8::Isolate* isolate, std::string input) { | |
111 return ConvertToV8(isolate, input).As<v8::String>(); | |
112 } | |
113 | |
114 template<typename T> | |
115 bool 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_ | |
OLD | NEW |