Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: gin/converter.h

Issue 89723002: Convert the rest of the functions in core.cc to use CreateFunctionTemplate. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase+comments Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/array_buffer.cc ('k') | gin/converter.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef GIN_CONVERTER_H_ 5 #ifndef GIN_CONVERTER_H_
6 #define GIN_CONVERTER_H_ 6 #define GIN_CONVERTER_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "v8/include/v8.h" 11 #include "v8/include/v8.h"
12 12
13 namespace gin { 13 namespace gin {
14 14
15 template<typename T> 15 template<typename T>
16 struct Converter {}; 16 struct Converter {};
17 17
18 template<> 18 template<>
19 struct Converter<bool> { 19 struct Converter<bool> {
20 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 20 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
21 bool val); 21 bool val);
22 static bool FromV8(v8::Handle<v8::Value> val, 22 static bool FromV8(v8::Isolate* isolate,
23 v8::Handle<v8::Value> val,
23 bool* out); 24 bool* out);
24 }; 25 };
25 26
26 template<> 27 template<>
27 struct Converter<int32_t> { 28 struct Converter<int32_t> {
28 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 29 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
29 int32_t val); 30 int32_t val);
30 static bool FromV8(v8::Handle<v8::Value> val, 31 static bool FromV8(v8::Isolate* isolate,
32 v8::Handle<v8::Value> val,
31 int32_t* out); 33 int32_t* out);
32 }; 34 };
33 35
34 template<> 36 template<>
35 struct Converter<uint32_t> { 37 struct Converter<uint32_t> {
36 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 38 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
37 uint32_t val); 39 uint32_t val);
38 static bool FromV8(v8::Handle<v8::Value> val, 40 static bool FromV8(v8::Isolate* isolate,
41 v8::Handle<v8::Value> val,
39 uint32_t* out); 42 uint32_t* out);
40 }; 43 };
41 44
42 template<> 45 template<>
43 struct Converter<int64_t> { 46 struct Converter<int64_t> {
44 // Warning: JavaScript cannot represent 64 integers precisely. 47 // Warning: JavaScript cannot represent 64 integers precisely.
45 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 48 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
46 int64_t val); 49 int64_t val);
47 static bool FromV8(v8::Handle<v8::Value> val, 50 static bool FromV8(v8::Isolate* isolate,
51 v8::Handle<v8::Value> val,
48 int64_t* out); 52 int64_t* out);
49 }; 53 };
50 54
51 template<> 55 template<>
52 struct Converter<uint64_t> { 56 struct Converter<uint64_t> {
53 // Warning: JavaScript cannot represent 64 integers precisely. 57 // Warning: JavaScript cannot represent 64 integers precisely.
54 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 58 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
55 uint64_t val); 59 uint64_t val);
56 static bool FromV8(v8::Handle<v8::Value> val, 60 static bool FromV8(v8::Isolate* isolate,
61 v8::Handle<v8::Value> val,
57 uint64_t* out); 62 uint64_t* out);
58 }; 63 };
59 64
60 template<> 65 template<>
61 struct Converter<double> { 66 struct Converter<double> {
62 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 67 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
63 double val); 68 double val);
64 static bool FromV8(v8::Handle<v8::Value> val, 69 static bool FromV8(v8::Isolate* isolate,
70 v8::Handle<v8::Value> val,
65 double* out); 71 double* out);
66 }; 72 };
67 73
68 template<> 74 template<>
69 struct Converter<std::string> { 75 struct Converter<std::string> {
70 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 76 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
71 const std::string& val); 77 const std::string& val);
72 static bool FromV8(v8::Handle<v8::Value> val, 78 static bool FromV8(v8::Isolate* isolate,
79 v8::Handle<v8::Value> val,
73 std::string* out); 80 std::string* out);
74 }; 81 };
75 82
76 template<> 83 template<>
77 struct Converter<v8::Handle<v8::Function> > { 84 struct Converter<v8::Handle<v8::Function> > {
78 static bool FromV8(v8::Handle<v8::Value> val, 85 static bool FromV8(v8::Isolate* isolate,
86 v8::Handle<v8::Value> val,
79 v8::Handle<v8::Function>* out); 87 v8::Handle<v8::Function>* out);
80 }; 88 };
81 89
82 template<> 90 template<>
83 struct Converter<v8::Handle<v8::Object> > { 91 struct Converter<v8::Handle<v8::Object> > {
84 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 92 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
85 v8::Handle<v8::Object> val); 93 v8::Handle<v8::Object> val);
86 static bool FromV8(v8::Handle<v8::Value> val, 94 static bool FromV8(v8::Isolate* isolate,
95 v8::Handle<v8::Value> val,
87 v8::Handle<v8::Object>* out); 96 v8::Handle<v8::Object>* out);
88 }; 97 };
89 98
90 template<> 99 template<>
91 struct Converter<v8::Handle<v8::ArrayBuffer> > { 100 struct Converter<v8::Handle<v8::ArrayBuffer> > {
92 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 101 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
93 v8::Handle<v8::ArrayBuffer> val); 102 v8::Handle<v8::ArrayBuffer> val);
94 static bool FromV8(v8::Handle<v8::Value> val, 103 static bool FromV8(v8::Isolate* isolate,
104 v8::Handle<v8::Value> val,
95 v8::Handle<v8::ArrayBuffer>* out); 105 v8::Handle<v8::ArrayBuffer>* out);
96 }; 106 };
97 107
98 template<> 108 template<>
99 struct Converter<v8::Handle<v8::External> > { 109 struct Converter<v8::Handle<v8::External> > {
100 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 110 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
101 v8::Handle<v8::External> val); 111 v8::Handle<v8::External> val);
102 static bool FromV8(v8::Handle<v8::Value> val, 112 static bool FromV8(v8::Isolate* isolate,
113 v8::Handle<v8::Value> val,
103 v8::Handle<v8::External>* out); 114 v8::Handle<v8::External>* out);
104 }; 115 };
105 116
106 template<> 117 template<>
107 struct Converter<v8::Handle<v8::Value> > { 118 struct Converter<v8::Handle<v8::Value> > {
108 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 119 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
109 v8::Handle<v8::Value> val); 120 v8::Handle<v8::Value> val);
110 static bool FromV8(v8::Handle<v8::Value> val, 121 static bool FromV8(v8::Isolate* isolate,
122 v8::Handle<v8::Value> val,
111 v8::Handle<v8::Value>* out); 123 v8::Handle<v8::Value>* out);
112 }; 124 };
113 125
114 template<typename T> 126 template<typename T>
115 struct Converter<std::vector<T> > { 127 struct Converter<std::vector<T> > {
116 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, 128 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
117 const std::vector<T>& val) { 129 const std::vector<T>& val) {
118 v8::Handle<v8::Array> result(v8::Array::New(static_cast<int>(val.size()))); 130 v8::Handle<v8::Array> result(v8::Array::New(static_cast<int>(val.size())));
119 for (size_t i = 0; i < val.size(); ++i) { 131 for (size_t i = 0; i < val.size(); ++i) {
120 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i])); 132 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
121 } 133 }
122 return result; 134 return result;
123 } 135 }
124 136
125 static bool FromV8(v8::Handle<v8::Value> val, 137 static bool FromV8(v8::Isolate* isolate,
138 v8::Handle<v8::Value> val,
126 std::vector<T>* out) { 139 std::vector<T>* out) {
127 if (!val->IsArray()) 140 if (!val->IsArray())
128 return false; 141 return false;
129 142
130 std::vector<T> result; 143 std::vector<T> result;
131 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val)); 144 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
132 uint32_t length = array->Length(); 145 uint32_t length = array->Length();
133 for (uint32_t i = 0; i < length; ++i) { 146 for (uint32_t i = 0; i < length; ++i) {
134 T item; 147 T item;
135 if (!Converter<T>::FromV8(array->Get(i), &item)) 148 if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
136 return false; 149 return false;
137 result.push_back(item); 150 result.push_back(item);
138 } 151 }
139 152
140 out->swap(result); 153 out->swap(result);
141 return true; 154 return true;
142 } 155 }
143 }; 156 };
144 157
145 // Convenience functions that deduce T. 158 // Convenience functions that deduce T.
146 template<typename T> 159 template<typename T>
147 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, 160 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
148 T input) { 161 T input) {
149 return Converter<T>::ToV8(isolate, input); 162 return Converter<T>::ToV8(isolate, input);
150 } 163 }
151 164
152 inline v8::Handle<v8::String> StringToV8( 165 inline v8::Handle<v8::String> StringToV8(v8::Isolate* isolate,
153 v8::Isolate* isolate, std::string input) { 166 std::string input) {
154 return ConvertToV8(isolate, input).As<v8::String>(); 167 return ConvertToV8(isolate, input).As<v8::String>();
155 } 168 }
156 169
157 v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate, 170 v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
158 const std::string& val); 171 const std::string& val);
159 172
160 template<typename T> 173 template<typename T>
161 bool ConvertFromV8(v8::Handle<v8::Value> input, T* result) { 174 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input,
162 return Converter<T>::FromV8(input, result); 175 T* result) {
176 return Converter<T>::FromV8(isolate, input, result);
163 } 177 }
164 178
165 std::string V8ToString(v8::Handle<v8::Value> value); 179 std::string V8ToString(v8::Handle<v8::Value> value);
166 180
167 } // namespace gin 181 } // namespace gin
168 182
169 #endif // GIN_CONVERTER_H_ 183 #endif // GIN_CONVERTER_H_
OLDNEW
« no previous file with comments | « gin/array_buffer.cc ('k') | gin/converter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698