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

Unified Diff: lib/filterClassesNew.js

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Back to manual approach for API Created Jan. 18, 2016, 12:41 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/shell.js ('k') | test.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterClassesNew.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/filterClassesNew.js
@@ -0,0 +1,75 @@
+"use strict";
+
+let compiled = require("./compiled");
+let {
+ createClass, wrapCall, stringArg, stringResult, defineProperty, defineMethod,
+ defineStringProperty,
+ _CreatePointer, _DeletePointer,
+ _Filter_GetType, _Filter_GetText, _Filter_FromText, _Filter_Normalize,
+ _InvalidFilter_GetReason,
+ _ActiveFilter_GetDisabled, _ActiveFilter_SetDisabled,
+ _ActiveFilter_GetHitCount, _ActiveFilter_SetHitCount,
+ _ActiveFilter_GetLastHit, _ActiveFilter_SetLastHit,
+ _RegExpFilter_Matches
+} = compiled;
+
+let constructors = Object.create(null);
+
+function createFilterClass(superclass, type, typeId)
+{
+ let result = createClass(superclass);
+ if (type)
+ Object.defineProperty(result.prototype, "type", {get: () => type, enumerable: true});
+ if (typeId)
+ constructors[typeId] = result;
+ return result;
+}
+
+function filterResult(call)
+{
+ return function()
+ {
+ let pointer = _CreatePointer();
+ try
+ {
+ let params = Array.slice(arguments);
+ params.unshift(pointer);
+
+ call.apply(this, params);
+ let type = _Filter_GetType(pointer);
+ if (type in constructors)
+ return new (constructors[type])(pointer);
+ else
+ throw new Error("Unexpected filter type " + type);
+ }
+ catch (e)
+ {
+ _DeletePointer(pointer);
+ throw e;
+ }
+ };
+}
+
+exports.Filter = createFilterClass();
+exports.Filter.fromText = wrapCall(stringArg(0, filterResult(_Filter_FromText)));
+exports.Filter.normalize = wrapCall(stringArg(0, stringResult(_Filter_Normalize)));
+defineStringProperty(exports.Filter, "text", _Filter_GetText);
+
+exports.InvalidFilter = createFilterClass(exports.Filter, "invalid", 1);
+defineStringProperty(exports.InvalidFilter, "reason", _InvalidFilter_GetReason);
+
+exports.CommentFilter = createFilterClass(exports.Filter, "comment", 2);
+
+exports.ActiveFilter = createFilterClass(exports.Filter);
+defineProperty(exports.ActiveFilter, "disabled", _ActiveFilter_GetDisabled, _ActiveFilter_SetDisabled);
+defineProperty(exports.ActiveFilter, "hitCount", _ActiveFilter_GetHitCount, _ActiveFilter_SetHitCount);
+defineProperty(exports.ActiveFilter, "lastHit", _ActiveFilter_GetLastHit, _ActiveFilter_SetLastHit);
+
+exports.RegExpFilter = createFilterClass(exports.ActiveFilter, "blocking", 3);
+defineMethod(exports.RegExpFilter, "matches", stringArg(1, _RegExpFilter_Matches));
+
+exports.WhiteListFilter = createFilterClass(exports.RegExpFilter, "whitelist", 4);
+exports.ElemHideBase = createFilterClass(exports.ActiveFilter);
+exports.ElemHideFilter = createFilterClass(exports.ElemHideBase, "elemhide", 5);
+exports.ElemHideException = createFilterClass(exports.ElemHideBase, "elemhideexception", 6);
+exports.CSSPropertyFilter = createFilterClass(exports.ElemHideBase, "cssproperty", 7);
« no previous file with comments | « compiled/shell.js ('k') | test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld