Skip to content

Commit 6b08928

Browse files
committed
NetworkFilterList impl replaced with flatbuffers.
1 parent b01147d commit 6b08928

19 files changed

+252
-717
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ harness = false
9393
[features]
9494
# If disabling default features, consider explicitly re-enabling the
9595
# "embedded-domain-resolver" feature.
96-
default = ["embedded-domain-resolver", "full-regex-handling", "unsync-regex-caching"]
96+
default = ["embedded-domain-resolver", "full-regex-handling", "unsync-regex-caching", "flatbuffers-storage"]
9797
full-regex-handling = []
9898
unsync-regex-caching = [] # disables `Send` and `Sync` on `Engine`.
9999
regex-debug-info = []

benches/bench_serialization.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ fn serialization(c: &mut Criterion) {
1818
]);
1919

2020
let engine = Engine::from_rules(full_rules, Default::default());
21-
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
21+
b.iter(|| assert!(engine.serialize().unwrap().len() > 0))
2222
});
2323
group.bench_function("el", move |b| {
2424
let full_rules = rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]);
2525

2626
let engine = Engine::from_rules(full_rules, Default::default());
27-
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
27+
b.iter(|| assert!(engine.serialize().unwrap().len() > 0))
2828
});
2929
group.bench_function("slimlist", move |b| {
3030
let full_rules = rules_from_lists(&["data/slim-list.txt"]);
3131

3232
let engine = Engine::from_rules(full_rules, Default::default());
33-
b.iter(|| assert!(engine.serialize_raw().unwrap().len() > 0))
33+
b.iter(|| assert!(engine.serialize().unwrap().len() > 0))
3434
});
3535

3636
group.finish();
@@ -48,7 +48,7 @@ fn deserialization(c: &mut Criterion) {
4848
]);
4949

5050
let engine = Engine::from_rules(full_rules, Default::default());
51-
let serialized = engine.serialize_raw().unwrap();
51+
let serialized = engine.serialize().unwrap();
5252

5353
b.iter(|| {
5454
let mut deserialized = Engine::default();
@@ -59,7 +59,7 @@ fn deserialization(c: &mut Criterion) {
5959
let full_rules = rules_from_lists(&["data/easylist.to/easylist/easylist.txt"]);
6060

6161
let engine = Engine::from_rules(full_rules, Default::default());
62-
let serialized = engine.serialize_raw().unwrap();
62+
let serialized = engine.serialize().unwrap();
6363

6464
b.iter(|| {
6565
let mut deserialized = Engine::default();
@@ -70,7 +70,7 @@ fn deserialization(c: &mut Criterion) {
7070
let full_rules = rules_from_lists(&["data/slim-list.txt"]);
7171

7272
let engine = Engine::from_rules(full_rules, Default::default());
73-
let serialized = engine.serialize_raw().unwrap();
73+
let serialized = engine.serialize().unwrap();
7474

7575
b.iter(|| {
7676
let mut deserialized = Engine::default();

examples/generate-dat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() {
2121
)
2222
.unwrap();
2323
assert!(engine.check_network_request(&request).exception.is_some());
24-
let serialized = engine.serialize_raw().expect("Could not serialize!");
24+
let serialized = engine.serialize().expect("Could not serialize!");
2525

2626
// Write to file
2727
let mut file = File::create("engine.dat").expect("Could not create serialization file");

js/src/lib.rs

+60-28
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
use adblock::lists::{
2+
FilterFormat, FilterListMetadata, FilterSet as FilterSetInternal, ParseOptions, RuleTypes,
3+
};
4+
use adblock::resources::resource_assembler::assemble_web_accessible_resources;
5+
use adblock::resources::Resource;
6+
use adblock::Engine as EngineInternal;
7+
use adblock::EngineSerializer as EngineSerializerInternal;
18
use neon::prelude::*;
29
use neon::types::buffer::TypedArray as _;
310
use serde::{Deserialize, Serialize};
411
use std::cell::RefCell;
5-
use std::sync::Mutex;
612
use std::path::Path;
7-
use adblock::Engine as EngineInternal;
8-
use adblock::EngineSerializer as EngineSerializerInternal;
9-
use adblock::lists::{RuleTypes, FilterFormat, FilterListMetadata, FilterSet as FilterSetInternal, ParseOptions};
10-
use adblock::resources::Resource;
11-
use adblock::resources::resource_assembler::assemble_web_accessible_resources;
13+
use std::sync::Mutex;
1214

1315
/// Use the JS context's JSON.stringify and JSON.parse as an FFI, at least until
1416
/// https://github.com/neon-bindings/neon/pull/953 is available
@@ -18,14 +20,18 @@ mod json_ffi {
1820

1921
/// Call `JSON.stringify` to convert the input to a `JsString`, then call serde_json to parse
2022
/// it to an instance of a native Rust type
21-
pub fn from_js<'a, C: Context<'a>, T: DeserializeOwned>(cx: &mut C, input: Handle<JsValue>) -> NeonResult<T> {
23+
pub fn from_js<'a, C: Context<'a>, T: DeserializeOwned>(
24+
cx: &mut C,
25+
input: Handle<JsValue>,
26+
) -> NeonResult<T> {
2227
let json: Handle<JsObject> = cx.global().get(cx, "JSON")?;
2328
let json_stringify: Handle<JsFunction> = json.get(cx, "stringify")?;
2429

2530
let undefined = JsUndefined::new(cx);
2631
let js_string = json_stringify
2732
.call(cx, undefined, [input])?
28-
.downcast::<JsString, _>(cx).or_throw(cx)?;
33+
.downcast::<JsString, _>(cx)
34+
.or_throw(cx)?;
2935

3036
match serde_json::from_str(&js_string.value(cx)) {
3137
Ok(v) => Ok(v),
@@ -35,16 +41,16 @@ mod json_ffi {
3541

3642
/// Use `serde_json` to stringify the input, then call `JSON.parse` to convert it to a
3743
/// `JsValue`
38-
pub fn to_js<'a, C: Context<'a>, T: serde::Serialize>(cx: &mut C, input: &T) -> JsResult<'a, JsValue> {
44+
pub fn to_js<'a, C: Context<'a>, T: serde::Serialize>(
45+
cx: &mut C,
46+
input: &T,
47+
) -> JsResult<'a, JsValue> {
3948
let input_handle = JsString::new(cx, serde_json::to_string(&input).unwrap());
4049

4150
let json: Handle<JsObject> = cx.global().get(cx, "JSON")?;
4251
let json_parse: Handle<JsFunction> = json.get(cx, "parse")?;
4352

44-
json_parse
45-
.call_with(cx)
46-
.arg(input_handle)
47-
.apply(cx)
53+
json_parse.call_with(cx).arg(input_handle).apply(cx)
4854
}
4955
}
5056

@@ -62,10 +68,16 @@ impl FilterSet {
6268
fn add_filters(&self, rules: &[String], opts: ParseOptions) -> FilterListMetadata {
6369
self.0.borrow_mut().add_filters(rules, opts)
6470
}
65-
fn add_filter(&self, filter: &str, opts: ParseOptions) -> Result<(), adblock::lists::FilterParseError> {
71+
fn add_filter(
72+
&self,
73+
filter: &str,
74+
opts: ParseOptions,
75+
) -> Result<(), adblock::lists::FilterParseError> {
6676
self.0.borrow_mut().add_filter(filter, opts)
6777
}
68-
fn into_content_blocking(&self) -> Result<(Vec<adblock::content_blocking::CbRule>, Vec<String>), ()> {
78+
fn into_content_blocking(
79+
&self,
80+
) -> Result<(Vec<adblock::content_blocking::CbRule>, Vec<String>), ()> {
6981
self.0.borrow().clone().into_content_blocking()
7082
}
7183
}
@@ -75,7 +87,10 @@ impl Finalize for FilterSet {}
7587
fn create_filter_set(mut cx: FunctionContext) -> JsResult<JsBox<FilterSet>> {
7688
match cx.argument_opt(0) {
7789
Some(arg) => {
78-
let debug: bool = arg.downcast::<JsBoolean, _>(&mut cx).or_throw(&mut cx)?.value(&mut cx);
90+
let debug: bool = arg
91+
.downcast::<JsBoolean, _>(&mut cx)
92+
.or_throw(&mut cx)?
93+
.value(&mut cx);
7994
Ok(cx.boxed(FilterSet::new(debug)))
8095
}
8196
None => Ok(cx.boxed(FilterSet::default())),
@@ -159,9 +174,7 @@ fn engine_constructor(mut cx: FunctionContext) -> JsResult<JsBox<Engine>> {
159174
};
160175
EngineInternal::from_filter_set(rules, optimize)
161176
}
162-
None => {
163-
EngineInternal::from_filter_set(rules, true)
164-
},
177+
None => EngineInternal::from_filter_set(rules, true),
165178
};
166179
Ok(cx.boxed(Engine(Mutex::new(engine_internal))))
167180
}
@@ -176,7 +189,9 @@ fn engine_check(mut cx: FunctionContext) -> JsResult<JsValue> {
176189
let debug = match cx.argument_opt(4) {
177190
Some(arg) => {
178191
// Throw if the argument exists and it cannot be downcasted to a boolean
179-
arg.downcast::<JsBoolean, _>(&mut cx).or_throw(&mut cx)?.value(&mut cx)
192+
arg.downcast::<JsBoolean, _>(&mut cx)
193+
.or_throw(&mut cx)?
194+
.value(&mut cx)
180195
}
181196
None => false,
182197
};
@@ -231,10 +246,10 @@ fn engine_url_cosmetic_resources(mut cx: FunctionContext) -> JsResult<JsValue> {
231246
json_ffi::to_js(&mut cx, &result)
232247
}
233248

234-
fn engine_serialize_raw(mut cx: FunctionContext) -> JsResult<JsArrayBuffer> {
249+
fn engine_serialize(mut cx: FunctionContext) -> JsResult<JsArrayBuffer> {
235250
let this = cx.argument::<JsBox<Engine>>(0)?;
236251
let serialized = if let Ok(engine) = this.0.lock() {
237-
engine.serialize_raw().unwrap()
252+
engine.serialize().unwrap()
238253
} else {
239254
cx.throw_error("Failed to acquire lock on engine")?
240255
};
@@ -337,14 +352,25 @@ fn ublock_resources(mut cx: FunctionContext) -> JsResult<JsValue> {
337352
let redirect_resources_path: String = cx.argument::<JsString>(1)?.value(&mut cx);
338353
// `scriptlets_path` is optional, since adblock-rust parsing that file is now deprecated.
339354
let scriptlets_path = match cx.argument_opt(2) {
340-
Some(arg) => Some(arg.downcast::<JsString, _>(&mut cx).or_throw(&mut cx)?.value(&mut cx)),
355+
Some(arg) => Some(
356+
arg.downcast::<JsString, _>(&mut cx)
357+
.or_throw(&mut cx)?
358+
.value(&mut cx),
359+
),
341360
None => None,
342361
};
343362

344-
let mut resources = assemble_web_accessible_resources(&Path::new(&web_accessible_resource_dir), &Path::new(&redirect_resources_path));
363+
let mut resources = assemble_web_accessible_resources(
364+
&Path::new(&web_accessible_resource_dir),
365+
&Path::new(&redirect_resources_path),
366+
);
345367
if let Some(scriptlets_path) = scriptlets_path {
346368
#[allow(deprecated)]
347-
resources.append(&mut adblock::resources::resource_assembler::assemble_scriptlet_resources(&Path::new(&scriptlets_path)));
369+
resources.append(
370+
&mut adblock::resources::resource_assembler::assemble_scriptlet_resources(&Path::new(
371+
&scriptlets_path,
372+
)),
373+
);
348374
}
349375

350376
json_ffi::to_js(&mut cx, &resources)
@@ -381,13 +407,19 @@ register_module!(mut m, {
381407
m.export_function("FilterSet_constructor", create_filter_set)?;
382408
m.export_function("FilterSet_addFilters", filter_set_add_filters)?;
383409
m.export_function("FilterSet_addFilter", filter_set_add_filter)?;
384-
m.export_function("FilterSet_intoContentBlocking", filter_set_into_content_blocking)?;
410+
m.export_function(
411+
"FilterSet_intoContentBlocking",
412+
filter_set_into_content_blocking,
413+
)?;
385414

386415
m.export_function("Engine_constructor", engine_constructor)?;
387416
m.export_function("Engine_check", engine_check)?;
388417
m.export_function("Engine_urlCosmeticResources", engine_url_cosmetic_resources)?;
389-
m.export_function("Engine_hiddenClassIdSelectors", engine_hidden_class_id_selectors)?;
390-
m.export_function("Engine_serializeRaw", engine_serialize_raw)?;
418+
m.export_function(
419+
"Engine_hiddenClassIdSelectors",
420+
engine_hidden_class_id_selectors,
421+
)?;
422+
m.export_function("Engine_serialize", engine_serialize)?;
391423
m.export_function("Engine_deserialize", engine_deserialize)?;
392424
m.export_function("Engine_enableTag", engine_enable_tag)?;
393425
m.export_function("Engine_useResources", engine_use_resources)?;

0 commit comments

Comments
 (0)