Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 char TypeInfo<T>::s_typeIDHelper; | 112 char TypeInfo<T>::s_typeIDHelper; |
113 | 113 |
114 struct FunctionInfo | 114 struct FunctionInfo |
115 { | 115 { |
116 TypeCategory returnType; | 116 TypeCategory returnType; |
117 TYPEID pointerType; | 117 TYPEID pointerType; |
118 std::vector<TypeCategory> args; | 118 std::vector<TypeCategory> args; |
119 bool instance_function; | 119 bool instance_function; |
120 int effectiveArgs; | 120 int effectiveArgs; |
121 TypeCategory effectiveReturnType; | 121 TypeCategory effectiveReturnType; |
122 char name[1024]; | 122 std::string name; |
123 | 123 |
124 FunctionInfo(); | 124 FunctionInfo(); |
125 | 125 |
126 FunctionInfo(TypeCategory returnType, TYPEID pointerType, | 126 FunctionInfo(TypeCategory returnType, TYPEID pointerType, |
127 std::initializer_list<TypeCategory> argTypes, bool instance_function, | 127 std::initializer_list<TypeCategory> argTypes, bool instance_function, |
128 void* function); | 128 void* function); |
129 | 129 |
130 template<typename ReturnType, typename... Args> | 130 template<typename ReturnType, typename... Args> |
131 FunctionInfo(ReturnType (*function)(Args...)) | 131 FunctionInfo(ReturnType (*function)(Args...)) |
132 : FunctionInfo(TypeInfo<ReturnType>(), | 132 : FunctionInfo(TypeInfo<ReturnType>(), |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 std::string name; | 173 std::string name; |
174 FunctionInfo call; | 174 FunctionInfo call; |
175 }; | 175 }; |
176 | 176 |
177 struct DifferentiatorInfo | 177 struct DifferentiatorInfo |
178 { | 178 { |
179 size_t offset; | 179 size_t offset; |
180 std::vector<std::pair<int, std::string>> mapping; | 180 std::vector<std::pair<int, std::string>> mapping; |
181 }; | 181 }; |
182 | 182 |
183 typedef std::vector<PropertyInfo> PropertyList; | 183 typedef std::vector<PropertyInfo> Properties; |
sergei
2017/05/08 10:54:39
It's usually a bad practice to call something like
Wladimir Palant
2017/05/08 12:55:41
Done.
| |
184 typedef std::vector<MethodInfo> MethodList; | 184 typedef std::vector<MethodInfo> Methods; |
185 | 185 |
186 struct ClassInfo | 186 struct ClassInfo |
187 { | 187 { |
188 TYPEID id; | 188 TYPEID id; |
189 TYPEID baseClass; | 189 TYPEID baseClass; |
190 std::string name; | 190 std::string name; |
191 PropertyList properties; | 191 Properties properties; |
192 MethodList methods; | 192 Methods methods; |
193 DifferentiatorInfo subclass_differentiator; | 193 DifferentiatorInfo subclass_differentiator; |
194 ptrdiff_t ref_counted_offset; | 194 ptrdiff_t ref_counted_offset; |
195 }; | 195 FunctionInfo instanceGetter; |
196 | |
197 struct NamespaceInfo | |
198 { | |
199 std::string name; | |
200 PropertyList properties; | |
201 MethodList methods; | |
202 }; | 196 }; |
203 | 197 |
204 void register_class(const char* name, TYPEID classID, TYPEID baseClassID, | 198 void register_class(const char* name, TYPEID classID, TYPEID baseClassID, |
205 ptrdiff_t ref_counted_offset); | 199 ptrdiff_t ref_counted_offset, |
200 const FunctionInfo& instanceGetter = FunctionInfo()); | |
206 | 201 |
207 void register_property(TYPEID classID, const char* name, | 202 void register_property(TYPEID classID, const char* name, |
208 const FunctionInfo& getter, const FunctionInfo& setter, | 203 const FunctionInfo& getter, const FunctionInfo& setter, |
209 const char* jsValue = ""); | 204 const char* jsValue = ""); |
210 | 205 |
211 void register_method(TYPEID classID, const char* name, | 206 void register_method(TYPEID classID, const char* name, |
212 const FunctionInfo& call); | 207 const FunctionInfo& call); |
213 | 208 |
214 void register_differentiator(TYPEID classID, size_t offset, | 209 void register_differentiator(TYPEID classID, size_t offset, |
215 std::vector<std::pair<int, std::string>>& mapping); | 210 std::vector<std::pair<int, std::string>>& mapping); |
216 | 211 |
217 void register_namespace(const char* name); | 212 std::string wrapCall(const FunctionInfo& call, bool isFunction = true, |
218 | 213 const FunctionInfo& instanceGetter = FunctionInfo()); |
219 void register_namespace_property(const char* namespaceName, const char* name, | |
220 const FunctionInfo& getter, const FunctionInfo& setter); | |
221 | |
222 void register_namespace_method(const char* namespaceName, const char* name, | |
223 const FunctionInfo& call); | |
224 | |
225 std::string generateCall(const FunctionInfo& call, | |
226 std::vector<std::string>& params); | |
227 | |
228 std::string wrapCall(const FunctionInfo& call, bool isFunction = true); | |
229 | |
230 void printHelpers(); | |
231 | |
232 void printClass(const ClassInfo& cls); | |
233 } | 214 } |
234 | 215 |
235 template<typename ClassType, | 216 template<typename ClassType, |
236 typename BaseClass = bindings_internal::NoBaseClass, | 217 typename BaseClass = bindings_internal::NoBaseClass, |
237 typename std::enable_if<std::is_base_of<ref_counted, ClassType>::value>::typ e* = nullptr> | 218 typename std::enable_if<std::is_base_of<ref_counted, ClassType>::value>::typ e* = nullptr> |
238 class class_ | 219 class class_ |
239 { | 220 { |
240 public: | 221 public: |
241 class_(const char* name) | 222 class_(const char* name) |
242 { | 223 { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 std::vector<std::pair<int, std::string>> mapping; | 287 std::vector<std::pair<int, std::string>> mapping; |
307 for (const auto& item : list) | 288 for (const auto& item : list) |
308 mapping.emplace_back(item.first, item.second); | 289 mapping.emplace_back(item.first, item.second); |
309 | 290 |
310 bindings_internal::register_differentiator( | 291 bindings_internal::register_differentiator( |
311 bindings_internal::TypeInfo<ClassType>(), offset, mapping); | 292 bindings_internal::TypeInfo<ClassType>(), offset, mapping); |
312 return *this; | 293 return *this; |
313 } | 294 } |
314 }; | 295 }; |
315 | 296 |
316 class namespace_ | 297 template<typename ClassType> |
298 class singleton | |
317 { | 299 { |
318 private: | |
319 const char* mName; | |
320 | |
321 public: | 300 public: |
322 namespace_(const char* name) | 301 singleton(const char* name, ClassType* (*instanceGetter)()) |
323 : mName(name) | 302 { |
324 { | 303 bindings_internal::register_class(name, |
325 bindings_internal::register_namespace(name); | 304 bindings_internal::TypeInfo<ClassType>(), |
305 bindings_internal::TypeInfo<bindings_internal::NoBaseClass>(), | |
306 0, | |
307 instanceGetter | |
308 ); | |
326 } | 309 } |
327 | 310 |
328 template<typename FieldType> | 311 template<typename FieldType> |
329 namespace_& property(const char* name, | 312 const singleton& property(const char* name, |
330 FieldType (*getter)(), | 313 FieldType (ClassType::*getter)() const, |
331 void (*setter)(FieldType) = nullptr) | 314 void (ClassType::*setter)(FieldType) = nullptr) const |
332 { | 315 { |
333 bindings_internal::register_namespace_property(mName, name, getter, | 316 bindings_internal::register_property( |
334 setter); | 317 bindings_internal::TypeInfo<ClassType>(), name, getter, setter); |
335 return *this; | 318 return *this; |
336 } | 319 } |
337 | 320 |
338 template<typename ReturnType, typename... Args> | 321 template<typename ReturnType, typename... Args> |
339 namespace_& function(const char* name, ReturnType (*method)(Args...)) | 322 const singleton& function(const char* name, ReturnType (ClassType::*method)(Ar gs...)) const |
340 { | 323 { |
341 bindings_internal::register_namespace_method(mName, name, method); | 324 bindings_internal::register_method( |
325 bindings_internal::TypeInfo<ClassType>(), name, method); | |
326 return *this; | |
327 } | |
328 | |
329 template<typename ReturnType, typename... Args> | |
330 const singleton& function(const char* name, ReturnType (ClassType::*method)(Ar gs...) const) const | |
331 { | |
332 bindings_internal::register_method( | |
333 bindings_internal::TypeInfo<ClassType>(), name, method); | |
342 return *this; | 334 return *this; |
343 } | 335 } |
344 }; | 336 }; |
345 | 337 |
346 void printBindings(); | 338 void printBindings(); |
LEFT | RIGHT |