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

Unified Diff: compiled/api.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Improved performance Created Jan. 28, 2016, 2:31 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « compiled/api.h ('k') | compiled/debug.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiled/api.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/api.cpp
@@ -0,0 +1,293 @@
+#include <exception>
+
+#include <emscripten.h>
+#include <emscripten/trace.h>
+
+#include "Filter.h"
+#include "InvalidFilter.h"
+#include "ActiveFilter.h"
+#include "RegExpFilter.h"
+#include "ElemHideBase.h"
+#include "CSSPropertyFilter.h"
+#include "String.h"
+
+#define DO_PRAGMA(pragma) _Pragma(#pragma)
+#define INSERT_MESSAGE(m) DO_PRAGMA(comment(m))
+
+#define CLASS(name) \
+ extern "C"\
+ INSERT_MESSAGE(class name)
+
+#define AUGMENTS(name) INSERT_MESSAGE(augments name)
+#define CLASS_INIT(func) INSERT_MESSAGE(class_init func)
+
+#define METHOD(name, impl) INSERT_MESSAGE(method name impl)
+#define CLASS_METHOD(name, impl) INSERT_MESSAGE(class_method name impl)
+#define STRING_ARG(pos) INSERT_MESSAGE(string_arg pos)
+#define RETURNS_STRING INSERT_MESSAGE(string_result)
+#define RETURNS_POINTER(cls) INSERT_MESSAGE(pointer_result cls)
+
+#define READONLY_PROPERTY(name, getter) INSERT_MESSAGE(property name getter)
+#define PROPERTY(name, getter, setter) INSERT_MESSAGE(property name getter setter)
+#define READONLY_STRING_PROPERTY(name, getter) INSERT_MESSAGE(string_property name getter)
+#define STRING_PROPERTY(name, getter, setter) INSERT_MESSAGE(string_property name getter setter)
+#define STATIC_PROPERTY(name, value) INSERT_MESSAGE(static_property name value)
+
+#define DIFFERENTIATOR(cls, func) INSERT_MESSAGE(differentiator cls func)
+#define ASSOCIATE(value, cls) INSERT_MESSAGE(differentiator_mapping value cls)
+
+#if defined(__EMSCRIPTEN_TRACING__)
+struct InitTracing
+{
+ InitTracing()
+ {
+ emscripten_trace_configure("http://127.0.0.1:5000/", "MyApplication");
+ }
+
+ ~InitTracing()
+ {
+ emscripten_trace_close();
+ }
+};
+
+InitTracing foo;
+#endif
+
+#define EXPOSE_FILTER_PROPERTY(class, type, name, getter, setter) \
+ type EMSCRIPTEN_KEEPALIVE class##_##getter(const FilterPtr& filter)\
+ {\
+ return std::dynamic_pointer_cast<class>(filter)->getter();\
+ }\
+ void EMSCRIPTEN_KEEPALIVE class##_##setter(\
+ const FilterPtr& filter, type value)\
+ {\
+ std::dynamic_pointer_cast<class>(filter)->setter(value);\
+ }\
+ PROPERTY(name, class##_##getter, class##_##setter)
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
+
+extern "C"
+{
+ size_t EMSCRIPTEN_KEEPALIVE GetSizeofString()
+ {
+ return sizeof(String);
+ }
+
+ void EMSCRIPTEN_KEEPALIVE InitString(String* str, String::value_type* data,
+ String::size_type len)
+ {
+ // String is already allocated on stack, we merely need to call constructor.
+ new (str) String(data, len);
+ }
+
+ void EMSCRIPTEN_KEEPALIVE DestroyString(String* str)
+ {
+ // Stack memory will be freed automatically, we need to call destructor
+ // explicitly however.
+ str->~String();
+ }
+
+ String::size_type EMSCRIPTEN_KEEPALIVE GetStringLength(const String& str)
+ {
+ return str.length();
+ }
+
+ const String::value_type* EMSCRIPTEN_KEEPALIVE GetStringData(const String& str)
+ {
+ return str.data();
+ }
+
+ FilterPtr* EMSCRIPTEN_KEEPALIVE CreatePointer()
+ {
+ FilterPtr* result = new FilterPtr();
+ annotate_address(result, "FilterPtr");
+ return result;
+ }
+
+ void EMSCRIPTEN_KEEPALIVE DeletePointer(FilterPtr* ptr)
+ {
+ delete ptr;
+ }
+}
+
+DIFFERENTIATOR(Filter, Filter_GetType)
+ ASSOCIATE(1, InvalidFilter)
+ ASSOCIATE(2, CommentFilter)
+ ASSOCIATE(3, RegExpFilter)
+ ASSOCIATE(4, WhitelistFilter)
+ ASSOCIATE(5, ElemHideFilter)
+ ASSOCIATE(6, ElemHideException)
+ ASSOCIATE(7, CSSPropertyFilter)
+
+CLASS(Filter)
+{
+ String EMSCRIPTEN_KEEPALIVE Filter_GetText(const FilterPtr& filter)
+ {
+ return filter->GetText();
+ }
+ READONLY_STRING_PROPERTY(text, Filter_GetText)
+
+ Filter::Type EMSCRIPTEN_KEEPALIVE Filter_GetType(const FilterPtr& filter)
+ {
+ return filter->GetType();
+ }
+
+ String EMSCRIPTEN_KEEPALIVE Filter_Serialize(const FilterPtr& filter)
+ {
+ return filter->Serialize();
+ }
+ METHOD(serialize, Filter_Serialize) RETURNS_STRING
+
+ FilterPtr EMSCRIPTEN_KEEPALIVE Filter_FromText(String& text)
+ {
+ return Filter::FromText(text);
+ }
+ CLASS_METHOD(fromText, Filter_FromText) STRING_ARG(0) RETURNS_POINTER(Filter)
+
+ String EMSCRIPTEN_KEEPALIVE Filter_Normalize(String& text)
+ {
+ return Filter::Normalize(text);
+ }
+ CLASS_METHOD(normalize, Filter_Normalize) STRING_ARG(0) RETURNS_STRING
+}
+
+CLASS(InvalidFilter)
+{
+ AUGMENTS(Filter)
+ STATIC_PROPERTY(type, "invalid")
+
+ String EMSCRIPTEN_KEEPALIVE InvalidFilter_GetReason(const FilterPtr& filter)
+ {
+ return std::dynamic_pointer_cast<InvalidFilter>(filter)->GetReason();
+ }
+ READONLY_STRING_PROPERTY(reason, InvalidFilter_GetReason)
+}
+
+CLASS(CommentFilter)
+{
+ AUGMENTS(Filter)
+ STATIC_PROPERTY(type, "comment")
+}
+
+CLASS(ActiveFilter)
+{
+ AUGMENTS(Filter)
+
+ EXPOSE_FILTER_PROPERTY(ActiveFilter, bool, disabled, GetDisabled, SetDisabled);
+ EXPOSE_FILTER_PROPERTY(ActiveFilter, unsigned int, hitCount, GetHitCount, SetHitCount);
+ EXPOSE_FILTER_PROPERTY(ActiveFilter, unsigned int, lastHit, GetLastHit, SetLastHit);
+
+ bool EMSCRIPTEN_KEEPALIVE ActiveFilter_IsActiveOnDomain(
+ const FilterPtr& filter, String& domain, String& sitekey)
+ {
+ return std::dynamic_pointer_cast<ActiveFilter>(filter)->IsActiveOnDomain(
+ domain, sitekey
+ );
+ }
+ METHOD(isActiveOnDomain, ActiveFilter_IsActiveOnDomain) STRING_ARG(0) STRING_ARG(1)
+
+ bool EMSCRIPTEN_KEEPALIVE ActiveFilter_IsActiveOnlyOnDomain(
+ const FilterPtr& filter, String& domain)
+ {
+ return std::dynamic_pointer_cast<ActiveFilter>(filter)->IsActiveOnlyOnDomain(
+ domain
+ );
+ }
+ METHOD(isActiveOnlyOnDomain, ActiveFilter_IsActiveOnlyOnDomain) STRING_ARG(0)
+
+ bool EMSCRIPTEN_KEEPALIVE ActiveFilter_IsGeneric(const FilterPtr& filter)
+ {
+ return std::dynamic_pointer_cast<ActiveFilter>(filter)->IsGeneric();
+ }
+ METHOD(isGeneric, ActiveFilter_IsGeneric)
+}
+
+CLASS(RegExpFilter)
+{
+ AUGMENTS(ActiveFilter)
+ STATIC_PROPERTY(type, "blocking")
+
+ bool EMSCRIPTEN_KEEPALIVE RegExpFilter_Matches(const FilterPtr& filter,
+ String& location, int typeMask, String& docDomain, bool thirdParty,
+ String& sitekey)
+ {
+ return std::dynamic_pointer_cast<RegExpFilter>(filter)->Matches(
+ location, typeMask, docDomain, thirdParty, sitekey
+ );
+ }
+ METHOD(matches, RegExpFilter_Matches) STRING_ARG(0) STRING_ARG(2) STRING_ARG(4)
+
+ void EMSCRIPTEN_KEEPALIVE RegExpFilter_InitJSTypes()
+ {
+ RegExpFilter::InitJSTypes();
+ }
+ CLASS_INIT(RegExpFilter_InitJSTypes)
+}
+
+CLASS(WhitelistFilter)
+{
+ AUGMENTS(RegExpFilter)
+ STATIC_PROPERTY(type, "whitelist")
+}
+
+CLASS(ElemHideBase)
+{
+ AUGMENTS(ActiveFilter)
+
+ String EMSCRIPTEN_KEEPALIVE ElemHideBase_GetSelector(
+ const FilterPtr& filter)
+ {
+ return std::dynamic_pointer_cast<ElemHideBase>(filter)->GetSelector();
+ }
+ READONLY_STRING_PROPERTY(selector, ElemHideBase_GetSelector)
+
+ String EMSCRIPTEN_KEEPALIVE ElemHideBase_GetSelectorDomain(
+ const FilterPtr& filter)
+ {
+ return std::dynamic_pointer_cast<ElemHideBase>(filter)->GetSelectorDomain();
+ }
+ READONLY_STRING_PROPERTY(selectorDomain, ElemHideBase_GetSelectorDomain)
+}
+
+CLASS(ElemHideFilter)
+{
+ AUGMENTS(ElemHideBase)
+ STATIC_PROPERTY(type, "elemhide")
+}
+
+CLASS(ElemHideException)
+{
+ AUGMENTS(ElemHideBase)
+ STATIC_PROPERTY(type, "elemhideexception")
+}
+
+CLASS(CSSPropertyFilter)
+{
+ AUGMENTS(ElemHideBase)
+ STATIC_PROPERTY(type, "cssproperty")
+
+ String EMSCRIPTEN_KEEPALIVE CSSPropertyFilter_GetRegExpString(
+ const FilterPtr& filter)
+ {
+ return std::dynamic_pointer_cast<CSSPropertyFilter>(filter)->GetRegExpString();
+ }
+ READONLY_STRING_PROPERTY(regexpString, CSSPropertyFilter_GetRegExpString)
+
+ String EMSCRIPTEN_KEEPALIVE CSSPropertyFilter_GetSelectorPrefix(
+ const FilterPtr& filter)
+ {
+ return std::dynamic_pointer_cast<CSSPropertyFilter>(filter)->GetSelectorPrefix();
+ }
+ READONLY_STRING_PROPERTY(selectorPrefix, CSSPropertyFilter_GetSelectorPrefix)
+
+ String EMSCRIPTEN_KEEPALIVE CSSPropertyFilter_GetSelectorSuffix(
+ const FilterPtr& filter)
+ {
+ return std::dynamic_pointer_cast<CSSPropertyFilter>(filter)->GetSelectorSuffix();
+ }
+ READONLY_STRING_PROPERTY(selectorSuffix, CSSPropertyFilter_GetSelectorSuffix)
+}
+
+#pragma clang diagnostic pop
« 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