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 ;
1
8
use neon:: prelude:: * ;
2
9
use neon:: types:: buffer:: TypedArray as _;
3
10
use serde:: { Deserialize , Serialize } ;
4
11
use std:: cell:: RefCell ;
5
- use std:: sync:: Mutex ;
6
12
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 ;
12
14
13
15
/// Use the JS context's JSON.stringify and JSON.parse as an FFI, at least until
14
16
/// https://github.com/neon-bindings/neon/pull/953 is available
@@ -18,14 +20,18 @@ mod json_ffi {
18
20
19
21
/// Call `JSON.stringify` to convert the input to a `JsString`, then call serde_json to parse
20
22
/// 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 > {
22
27
let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?;
23
28
let json_stringify: Handle < JsFunction > = json. get ( cx, "stringify" ) ?;
24
29
25
30
let undefined = JsUndefined :: new ( cx) ;
26
31
let js_string = json_stringify
27
32
. call ( cx, undefined, [ input] ) ?
28
- . downcast :: < JsString , _ > ( cx) . or_throw ( cx) ?;
33
+ . downcast :: < JsString , _ > ( cx)
34
+ . or_throw ( cx) ?;
29
35
30
36
match serde_json:: from_str ( & js_string. value ( cx) ) {
31
37
Ok ( v) => Ok ( v) ,
@@ -35,16 +41,16 @@ mod json_ffi {
35
41
36
42
/// Use `serde_json` to stringify the input, then call `JSON.parse` to convert it to a
37
43
/// `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 > {
39
48
let input_handle = JsString :: new ( cx, serde_json:: to_string ( & input) . unwrap ( ) ) ;
40
49
41
50
let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?;
42
51
let json_parse: Handle < JsFunction > = json. get ( cx, "parse" ) ?;
43
52
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)
48
54
}
49
55
}
50
56
@@ -62,10 +68,16 @@ impl FilterSet {
62
68
fn add_filters ( & self , rules : & [ String ] , opts : ParseOptions ) -> FilterListMetadata {
63
69
self . 0 . borrow_mut ( ) . add_filters ( rules, opts)
64
70
}
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 > {
66
76
self . 0 . borrow_mut ( ) . add_filter ( filter, opts)
67
77
}
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 > ) , ( ) > {
69
81
self . 0 . borrow ( ) . clone ( ) . into_content_blocking ( )
70
82
}
71
83
}
@@ -75,7 +87,10 @@ impl Finalize for FilterSet {}
75
87
fn create_filter_set ( mut cx : FunctionContext ) -> JsResult < JsBox < FilterSet > > {
76
88
match cx. argument_opt ( 0 ) {
77
89
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) ;
79
94
Ok ( cx. boxed ( FilterSet :: new ( debug) ) )
80
95
}
81
96
None => Ok ( cx. boxed ( FilterSet :: default ( ) ) ) ,
@@ -159,9 +174,7 @@ fn engine_constructor(mut cx: FunctionContext) -> JsResult<JsBox<Engine>> {
159
174
} ;
160
175
EngineInternal :: from_filter_set ( rules, optimize)
161
176
}
162
- None => {
163
- EngineInternal :: from_filter_set ( rules, true )
164
- } ,
177
+ None => EngineInternal :: from_filter_set ( rules, true ) ,
165
178
} ;
166
179
Ok ( cx. boxed ( Engine ( Mutex :: new ( engine_internal) ) ) )
167
180
}
@@ -176,7 +189,9 @@ fn engine_check(mut cx: FunctionContext) -> JsResult<JsValue> {
176
189
let debug = match cx. argument_opt ( 4 ) {
177
190
Some ( arg) => {
178
191
// 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)
180
195
}
181
196
None => false ,
182
197
} ;
@@ -231,10 +246,10 @@ fn engine_url_cosmetic_resources(mut cx: FunctionContext) -> JsResult<JsValue> {
231
246
json_ffi:: to_js ( & mut cx, & result)
232
247
}
233
248
234
- fn engine_serialize_raw ( mut cx : FunctionContext ) -> JsResult < JsArrayBuffer > {
249
+ fn engine_serialize ( mut cx : FunctionContext ) -> JsResult < JsArrayBuffer > {
235
250
let this = cx. argument :: < JsBox < Engine > > ( 0 ) ?;
236
251
let serialized = if let Ok ( engine) = this. 0 . lock ( ) {
237
- engine. serialize_raw ( ) . unwrap ( )
252
+ engine. serialize ( ) . unwrap ( )
238
253
} else {
239
254
cx. throw_error ( "Failed to acquire lock on engine" ) ?
240
255
} ;
@@ -337,14 +352,25 @@ fn ublock_resources(mut cx: FunctionContext) -> JsResult<JsValue> {
337
352
let redirect_resources_path: String = cx. argument :: < JsString > ( 1 ) ?. value ( & mut cx) ;
338
353
// `scriptlets_path` is optional, since adblock-rust parsing that file is now deprecated.
339
354
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
+ ) ,
341
360
None => None ,
342
361
} ;
343
362
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
+ ) ;
345
367
if let Some ( scriptlets_path) = scriptlets_path {
346
368
#[ 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
+ ) ;
348
374
}
349
375
350
376
json_ffi:: to_js ( & mut cx, & resources)
@@ -381,13 +407,19 @@ register_module!(mut m, {
381
407
m. export_function( "FilterSet_constructor" , create_filter_set) ?;
382
408
m. export_function( "FilterSet_addFilters" , filter_set_add_filters) ?;
383
409
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
+ ) ?;
385
414
386
415
m. export_function( "Engine_constructor" , engine_constructor) ?;
387
416
m. export_function( "Engine_check" , engine_check) ?;
388
417
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) ?;
391
423
m. export_function( "Engine_deserialize" , engine_deserialize) ?;
392
424
m. export_function( "Engine_enableTag" , engine_enable_tag) ?;
393
425
m. export_function( "Engine_useResources" , engine_use_resources) ?;
0 commit comments