Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/api.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Fixed annotation for hash buffers Created Jan. 28, 2016, 5:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/api.h ('k') | compiled/debug.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <exception>
2
3 #include <emscripten.h>
4 #include <emscripten/trace.h>
5
6 #include "Filter.h"
7 #include "InvalidFilter.h"
8 #include "ActiveFilter.h"
9 #include "RegExpFilter.h"
10 #include "ElemHideBase.h"
11 #include "CSSPropertyFilter.h"
12 #include "String.h"
13
14 #define DO_PRAGMA(pragma) _Pragma(#pragma)
15 #define INSERT_MESSAGE(m) DO_PRAGMA(comment(m))
16
17 #define CLASS(name) \
18 extern "C"\
19 INSERT_MESSAGE(class name)
20
21 #define AUGMENTS(name) INSERT_MESSAGE(augments name)
22 #define CLASS_INIT(func) INSERT_MESSAGE(class_init func)
23
24 #define METHOD(name, impl) INSERT_MESSAGE(method name impl)
25 #define CLASS_METHOD(name, impl) INSERT_MESSAGE(class_method name impl)
26 #define STRING_ARG(pos) INSERT_MESSAGE(string_arg pos)
27 #define RETURNS_STRING INSERT_MESSAGE(string_result)
28 #define RETURNS_POINTER(cls) INSERT_MESSAGE(pointer_result cls)
29
30 #define READONLY_PROPERTY(name, getter) INSERT_MESSAGE(property name getter)
31 #define PROPERTY(name, getter, setter) INSERT_MESSAGE(property name getter sette r)
32 #define READONLY_STRING_PROPERTY(name, getter) INSERT_MESSAGE(string_property na me getter)
33 #define STRING_PROPERTY(name, getter, setter) INSERT_MESSAGE(string_property nam e getter setter)
34 #define STATIC_PROPERTY(name, value) INSERT_MESSAGE(static_property name value)
35
36 #define DIFFERENTIATOR(cls, func) INSERT_MESSAGE(differentiator cls func)
37 #define ASSOCIATE(value, cls) INSERT_MESSAGE(differentiator_mapping value cls)
38
39 #if defined(__EMSCRIPTEN_TRACING__)
40 struct InitTracing
41 {
42 InitTracing()
43 {
44 emscripten_trace_configure("http://127.0.0.1:5000/", "MyApplication");
45 }
46
47 ~InitTracing()
48 {
49 emscripten_trace_close();
50 }
51 };
52
53 InitTracing foo;
54 #endif
55
56 #define EXPOSE_FILTER_PROPERTY(class, type, name, getter, setter) \
57 type EMSCRIPTEN_KEEPALIVE class##_##getter(const FilterPtr& filter)\
58 {\
59 return std::dynamic_pointer_cast<class>(filter)->getter();\
60 }\
61 void EMSCRIPTEN_KEEPALIVE class##_##setter(\
62 const FilterPtr& filter, type value)\
63 {\
64 std::dynamic_pointer_cast<class>(filter)->setter(value);\
65 }\
66 PROPERTY(name, class##_##getter, class##_##setter)
67
68 #pragma clang diagnostic push
69 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
70
71 extern "C"
72 {
73 size_t EMSCRIPTEN_KEEPALIVE GetSizeofString()
74 {
75 return sizeof(String);
76 }
77
78 void EMSCRIPTEN_KEEPALIVE InitString(String* str, String::value_type* data,
79 String::size_type len)
80 {
81 // String is already allocated on stack, we merely need to call constructor.
82 new (str) String(data, len);
83 }
84
85 void EMSCRIPTEN_KEEPALIVE DestroyString(String* str)
86 {
87 // Stack memory will be freed automatically, we need to call destructor
88 // explicitly however.
89 str->~String();
90 }
91
92 String::size_type EMSCRIPTEN_KEEPALIVE GetStringLength(const String& str)
93 {
94 return str.length();
95 }
96
97 const String::value_type* EMSCRIPTEN_KEEPALIVE GetStringData(const String& str )
98 {
99 return str.data();
100 }
101
102 FilterPtr* EMSCRIPTEN_KEEPALIVE CreatePointer()
103 {
104 FilterPtr* result = new FilterPtr();
105 annotate_address(result, "FilterPtr");
106 return result;
107 }
108
109 void EMSCRIPTEN_KEEPALIVE DeletePointer(FilterPtr* ptr)
110 {
111 delete ptr;
112 }
113 }
114
115 DIFFERENTIATOR(Filter, Filter_GetType)
116 ASSOCIATE(1, InvalidFilter)
117 ASSOCIATE(2, CommentFilter)
118 ASSOCIATE(3, RegExpFilter)
119 ASSOCIATE(4, WhitelistFilter)
120 ASSOCIATE(5, ElemHideFilter)
121 ASSOCIATE(6, ElemHideException)
122 ASSOCIATE(7, CSSPropertyFilter)
123
124 CLASS(Filter)
125 {
126 String EMSCRIPTEN_KEEPALIVE Filter_GetText(const FilterPtr& filter)
127 {
128 return filter->GetText();
129 }
130 READONLY_STRING_PROPERTY(text, Filter_GetText)
131
132 Filter::Type EMSCRIPTEN_KEEPALIVE Filter_GetType(const FilterPtr& filter)
133 {
134 return filter->GetType();
135 }
136
137 String EMSCRIPTEN_KEEPALIVE Filter_Serialize(const FilterPtr& filter)
138 {
139 return filter->Serialize();
140 }
141 METHOD(serialize, Filter_Serialize) RETURNS_STRING
142
143 FilterPtr EMSCRIPTEN_KEEPALIVE Filter_FromText(String& text)
144 {
145 return Filter::FromText(text);
146 }
147 CLASS_METHOD(fromText, Filter_FromText) STRING_ARG(0) RETURNS_POINTER(Filter)
148
149 String EMSCRIPTEN_KEEPALIVE Filter_Normalize(String& text)
150 {
151 return Filter::Normalize(text);
152 }
153 CLASS_METHOD(normalize, Filter_Normalize) STRING_ARG(0) RETURNS_STRING
154 }
155
156 CLASS(InvalidFilter)
157 {
158 AUGMENTS(Filter)
159 STATIC_PROPERTY(type, "invalid")
160
161 String EMSCRIPTEN_KEEPALIVE InvalidFilter_GetReason(const FilterPtr& filter)
162 {
163 return std::dynamic_pointer_cast<InvalidFilter>(filter)->GetReason();
164 }
165 READONLY_STRING_PROPERTY(reason, InvalidFilter_GetReason)
166 }
167
168 CLASS(CommentFilter)
169 {
170 AUGMENTS(Filter)
171 STATIC_PROPERTY(type, "comment")
172 }
173
174 CLASS(ActiveFilter)
175 {
176 AUGMENTS(Filter)
177
178 EXPOSE_FILTER_PROPERTY(ActiveFilter, bool, disabled, GetDisabled, SetDisabled) ;
179 EXPOSE_FILTER_PROPERTY(ActiveFilter, unsigned int, hitCount, GetHitCount, SetH itCount);
180 EXPOSE_FILTER_PROPERTY(ActiveFilter, unsigned int, lastHit, GetLastHit, SetLas tHit);
181
182 bool EMSCRIPTEN_KEEPALIVE ActiveFilter_IsActiveOnDomain(
183 const FilterPtr& filter, String& domain, String& sitekey)
184 {
185 return std::dynamic_pointer_cast<ActiveFilter>(filter)->IsActiveOnDomain(
186 domain, sitekey
187 );
188 }
189 METHOD(isActiveOnDomain, ActiveFilter_IsActiveOnDomain) STRING_ARG(0) STRING_A RG(1)
190
191 bool EMSCRIPTEN_KEEPALIVE ActiveFilter_IsActiveOnlyOnDomain(
192 const FilterPtr& filter, String& domain)
193 {
194 return std::dynamic_pointer_cast<ActiveFilter>(filter)->IsActiveOnlyOnDomain (
195 domain
196 );
197 }
198 METHOD(isActiveOnlyOnDomain, ActiveFilter_IsActiveOnlyOnDomain) STRING_ARG(0)
199
200 bool EMSCRIPTEN_KEEPALIVE ActiveFilter_IsGeneric(const FilterPtr& filter)
201 {
202 return std::dynamic_pointer_cast<ActiveFilter>(filter)->IsGeneric();
203 }
204 METHOD(isGeneric, ActiveFilter_IsGeneric)
205 }
206
207 CLASS(RegExpFilter)
208 {
209 AUGMENTS(ActiveFilter)
210 STATIC_PROPERTY(type, "blocking")
211
212 bool EMSCRIPTEN_KEEPALIVE RegExpFilter_Matches(const FilterPtr& filter,
213 String& location, int typeMask, String& docDomain, bool thirdParty,
214 String& sitekey)
215 {
216 return std::dynamic_pointer_cast<RegExpFilter>(filter)->Matches(
217 location, typeMask, docDomain, thirdParty, sitekey
218 );
219 }
220 METHOD(matches, RegExpFilter_Matches) STRING_ARG(0) STRING_ARG(2) STRING_ARG(4 )
221
222 void EMSCRIPTEN_KEEPALIVE RegExpFilter_InitJSTypes()
223 {
224 RegExpFilter::InitJSTypes();
225 }
226 CLASS_INIT(RegExpFilter_InitJSTypes)
227 }
228
229 CLASS(WhitelistFilter)
230 {
231 AUGMENTS(RegExpFilter)
232 STATIC_PROPERTY(type, "whitelist")
233 }
234
235 CLASS(ElemHideBase)
236 {
237 AUGMENTS(ActiveFilter)
238
239 String EMSCRIPTEN_KEEPALIVE ElemHideBase_GetSelector(
240 const FilterPtr& filter)
241 {
242 return std::dynamic_pointer_cast<ElemHideBase>(filter)->GetSelector();
243 }
244 READONLY_STRING_PROPERTY(selector, ElemHideBase_GetSelector)
245
246 String EMSCRIPTEN_KEEPALIVE ElemHideBase_GetSelectorDomain(
247 const FilterPtr& filter)
248 {
249 return std::dynamic_pointer_cast<ElemHideBase>(filter)->GetSelectorDomain();
250 }
251 READONLY_STRING_PROPERTY(selectorDomain, ElemHideBase_GetSelectorDomain)
252 }
253
254 CLASS(ElemHideFilter)
255 {
256 AUGMENTS(ElemHideBase)
257 STATIC_PROPERTY(type, "elemhide")
258 }
259
260 CLASS(ElemHideException)
261 {
262 AUGMENTS(ElemHideBase)
263 STATIC_PROPERTY(type, "elemhideexception")
264 }
265
266 CLASS(CSSPropertyFilter)
267 {
268 AUGMENTS(ElemHideBase)
269 STATIC_PROPERTY(type, "cssproperty")
270
271 String EMSCRIPTEN_KEEPALIVE CSSPropertyFilter_GetRegExpString(
272 const FilterPtr& filter)
273 {
274 return std::dynamic_pointer_cast<CSSPropertyFilter>(filter)->GetRegExpString ();
275 }
276 READONLY_STRING_PROPERTY(regexpString, CSSPropertyFilter_GetRegExpString)
277
278 String EMSCRIPTEN_KEEPALIVE CSSPropertyFilter_GetSelectorPrefix(
279 const FilterPtr& filter)
280 {
281 return std::dynamic_pointer_cast<CSSPropertyFilter>(filter)->GetSelectorPref ix();
282 }
283 READONLY_STRING_PROPERTY(selectorPrefix, CSSPropertyFilter_GetSelectorPrefix)
284
285 String EMSCRIPTEN_KEEPALIVE CSSPropertyFilter_GetSelectorSuffix(
286 const FilterPtr& filter)
287 {
288 return std::dynamic_pointer_cast<CSSPropertyFilter>(filter)->GetSelectorSuff ix();
289 }
290 READONLY_STRING_PROPERTY(selectorSuffix, CSSPropertyFilter_GetSelectorSuffix)
291 }
292
293 #pragma clang diagnostic pop
OLDNEW
« no previous file with comments | « compiled/api.h ('k') | compiled/debug.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld