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 ;
1
7
use neon:: prelude:: * ;
2
8
use neon:: types:: buffer:: TypedArray as _;
3
9
use serde:: { Deserialize , Serialize } ;
4
10
use std:: cell:: RefCell ;
5
- use std:: sync:: Mutex ;
6
11
use std:: path:: Path ;
7
- use adblock:: Engine as EngineInternal ;
8
- use adblock:: lists:: { RuleTypes , FilterFormat , FilterListMetadata , FilterSet as FilterSetInternal , ParseOptions } ;
9
- use adblock:: resources:: Resource ;
10
- use adblock:: resources:: resource_assembler:: assemble_web_accessible_resources;
12
+ use std:: sync:: Mutex ;
11
13
12
14
/// Use the JS context's JSON.stringify and JSON.parse as an FFI, at least until
13
15
/// https://github.com/neon-bindings/neon/pull/953 is available
@@ -17,14 +19,18 @@ mod json_ffi {
17
19
18
20
/// Call `JSON.stringify` to convert the input to a `JsString`, then call serde_json to parse
19
21
/// it to an instance of a native Rust type
20
- pub fn from_js < ' a , C : Context < ' a > , T : DeserializeOwned > ( cx : & mut C , input : Handle < JsValue > ) -> NeonResult < T > {
22
+ pub fn from_js < ' a , C : Context < ' a > , T : DeserializeOwned > (
23
+ cx : & mut C ,
24
+ input : Handle < JsValue > ,
25
+ ) -> NeonResult < T > {
21
26
let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?;
22
27
let json_stringify: Handle < JsFunction > = json. get ( cx, "stringify" ) ?;
23
28
24
29
let undefined = JsUndefined :: new ( cx) ;
25
30
let js_string = json_stringify
26
31
. call ( cx, undefined, [ input] ) ?
27
- . downcast :: < JsString , _ > ( cx) . or_throw ( cx) ?;
32
+ . downcast :: < JsString , _ > ( cx)
33
+ . or_throw ( cx) ?;
28
34
29
35
match serde_json:: from_str ( & js_string. value ( cx) ) {
30
36
Ok ( v) => Ok ( v) ,
@@ -34,16 +40,16 @@ mod json_ffi {
34
40
35
41
/// Use `serde_json` to stringify the input, then call `JSON.parse` to convert it to a
36
42
/// `JsValue`
37
- pub fn to_js < ' a , C : Context < ' a > , T : serde:: Serialize > ( cx : & mut C , input : & T ) -> JsResult < ' a , JsValue > {
43
+ pub fn to_js < ' a , C : Context < ' a > , T : serde:: Serialize > (
44
+ cx : & mut C ,
45
+ input : & T ,
46
+ ) -> JsResult < ' a , JsValue > {
38
47
let input_handle = JsString :: new ( cx, serde_json:: to_string ( & input) . unwrap ( ) ) ;
39
48
40
49
let json: Handle < JsObject > = cx. global ( ) . get ( cx, "JSON" ) ?;
41
50
let json_parse: Handle < JsFunction > = json. get ( cx, "parse" ) ?;
42
51
43
- json_parse
44
- . call_with ( cx)
45
- . arg ( input_handle)
46
- . apply ( cx)
52
+ json_parse. call_with ( cx) . arg ( input_handle) . apply ( cx)
47
53
}
48
54
}
49
55
@@ -61,10 +67,16 @@ impl FilterSet {
61
67
fn add_filters ( & self , rules : & [ String ] , opts : ParseOptions ) -> FilterListMetadata {
62
68
self . 0 . borrow_mut ( ) . add_filters ( rules, opts)
63
69
}
64
- fn add_filter ( & self , filter : & str , opts : ParseOptions ) -> Result < ( ) , adblock:: lists:: FilterParseError > {
70
+ fn add_filter (
71
+ & self ,
72
+ filter : & str ,
73
+ opts : ParseOptions ,
74
+ ) -> Result < ( ) , adblock:: lists:: FilterParseError > {
65
75
self . 0 . borrow_mut ( ) . add_filter ( filter, opts)
66
76
}
67
- fn into_content_blocking ( & self ) -> Result < ( Vec < adblock:: content_blocking:: CbRule > , Vec < String > ) , ( ) > {
77
+ fn into_content_blocking (
78
+ & self ,
79
+ ) -> Result < ( Vec < adblock:: content_blocking:: CbRule > , Vec < String > ) , ( ) > {
68
80
self . 0 . borrow ( ) . clone ( ) . into_content_blocking ( )
69
81
}
70
82
}
@@ -74,7 +86,10 @@ impl Finalize for FilterSet {}
74
86
fn create_filter_set ( mut cx : FunctionContext ) -> JsResult < JsBox < FilterSet > > {
75
87
match cx. argument_opt ( 0 ) {
76
88
Some ( arg) => {
77
- let debug: bool = arg. downcast :: < JsBoolean , _ > ( & mut cx) . or_throw ( & mut cx) ?. value ( & mut cx) ;
89
+ let debug: bool = arg
90
+ . downcast :: < JsBoolean , _ > ( & mut cx)
91
+ . or_throw ( & mut cx) ?
92
+ . value ( & mut cx) ;
78
93
Ok ( cx. boxed ( FilterSet :: new ( debug) ) )
79
94
}
80
95
None => Ok ( cx. boxed ( FilterSet :: default ( ) ) ) ,
@@ -158,9 +173,7 @@ fn engine_constructor(mut cx: FunctionContext) -> JsResult<JsBox<Engine>> {
158
173
} ;
159
174
EngineInternal :: from_filter_set ( rules, optimize)
160
175
}
161
- None => {
162
- EngineInternal :: from_filter_set ( rules, true )
163
- } ,
176
+ None => EngineInternal :: from_filter_set ( rules, true ) ,
164
177
} ;
165
178
Ok ( cx. boxed ( Engine ( Mutex :: new ( engine_internal) ) ) )
166
179
}
@@ -175,7 +188,9 @@ fn engine_check(mut cx: FunctionContext) -> JsResult<JsValue> {
175
188
let debug = match cx. argument_opt ( 4 ) {
176
189
Some ( arg) => {
177
190
// Throw if the argument exists and it cannot be downcasted to a boolean
178
- arg. downcast :: < JsBoolean , _ > ( & mut cx) . or_throw ( & mut cx) ?. value ( & mut cx)
191
+ arg. downcast :: < JsBoolean , _ > ( & mut cx)
192
+ . or_throw ( & mut cx) ?
193
+ . value ( & mut cx)
179
194
}
180
195
None => false ,
181
196
} ;
@@ -230,10 +245,10 @@ fn engine_url_cosmetic_resources(mut cx: FunctionContext) -> JsResult<JsValue> {
230
245
json_ffi:: to_js ( & mut cx, & result)
231
246
}
232
247
233
- fn engine_serialize_raw ( mut cx : FunctionContext ) -> JsResult < JsArrayBuffer > {
248
+ fn engine_serialize ( mut cx : FunctionContext ) -> JsResult < JsArrayBuffer > {
234
249
let this = cx. argument :: < JsBox < Engine > > ( 0 ) ?;
235
250
let serialized = if let Ok ( engine) = this. 0 . lock ( ) {
236
- engine. serialize_raw ( ) . unwrap ( )
251
+ engine. serialize ( ) . unwrap ( )
237
252
} else {
238
253
cx. throw_error ( "Failed to acquire lock on engine" ) ?
239
254
} ;
@@ -336,14 +351,25 @@ fn ublock_resources(mut cx: FunctionContext) -> JsResult<JsValue> {
336
351
let redirect_resources_path: String = cx. argument :: < JsString > ( 1 ) ?. value ( & mut cx) ;
337
352
// `scriptlets_path` is optional, since adblock-rust parsing that file is now deprecated.
338
353
let scriptlets_path = match cx. argument_opt ( 2 ) {
339
- Some ( arg) => Some ( arg. downcast :: < JsString , _ > ( & mut cx) . or_throw ( & mut cx) ?. value ( & mut cx) ) ,
354
+ Some ( arg) => Some (
355
+ arg. downcast :: < JsString , _ > ( & mut cx)
356
+ . or_throw ( & mut cx) ?
357
+ . value ( & mut cx) ,
358
+ ) ,
340
359
None => None ,
341
360
} ;
342
361
343
- let mut resources = assemble_web_accessible_resources ( & Path :: new ( & web_accessible_resource_dir) , & Path :: new ( & redirect_resources_path) ) ;
362
+ let mut resources = assemble_web_accessible_resources (
363
+ & Path :: new ( & web_accessible_resource_dir) ,
364
+ & Path :: new ( & redirect_resources_path) ,
365
+ ) ;
344
366
if let Some ( scriptlets_path) = scriptlets_path {
345
367
#[ allow( deprecated) ]
346
- resources. append ( & mut adblock:: resources:: resource_assembler:: assemble_scriptlet_resources ( & Path :: new ( & scriptlets_path) ) ) ;
368
+ resources. append (
369
+ & mut adblock:: resources:: resource_assembler:: assemble_scriptlet_resources ( & Path :: new (
370
+ & scriptlets_path,
371
+ ) ) ,
372
+ ) ;
347
373
}
348
374
349
375
json_ffi:: to_js ( & mut cx, & resources)
@@ -380,13 +406,19 @@ register_module!(mut m, {
380
406
m. export_function( "FilterSet_constructor" , create_filter_set) ?;
381
407
m. export_function( "FilterSet_addFilters" , filter_set_add_filters) ?;
382
408
m. export_function( "FilterSet_addFilter" , filter_set_add_filter) ?;
383
- m. export_function( "FilterSet_intoContentBlocking" , filter_set_into_content_blocking) ?;
409
+ m. export_function(
410
+ "FilterSet_intoContentBlocking" ,
411
+ filter_set_into_content_blocking,
412
+ ) ?;
384
413
385
414
m. export_function( "Engine_constructor" , engine_constructor) ?;
386
415
m. export_function( "Engine_check" , engine_check) ?;
387
416
m. export_function( "Engine_urlCosmeticResources" , engine_url_cosmetic_resources) ?;
388
- m. export_function( "Engine_hiddenClassIdSelectors" , engine_hidden_class_id_selectors) ?;
389
- m. export_function( "Engine_serializeRaw" , engine_serialize_raw) ?;
417
+ m. export_function(
418
+ "Engine_hiddenClassIdSelectors" ,
419
+ engine_hidden_class_id_selectors,
420
+ ) ?;
421
+ m. export_function( "Engine_serialize" , engine_serialize) ?;
390
422
m. export_function( "Engine_deserialize" , engine_deserialize) ?;
391
423
m. export_function( "Engine_enableTag" , engine_enable_tag) ?;
392
424
m. export_function( "Engine_useResources" , engine_use_resources) ?;
0 commit comments