@@ -16,30 +16,11 @@ pub fn wasm_bindgen_test(
16
16
attr : proc_macro:: TokenStream ,
17
17
body : proc_macro:: TokenStream ,
18
18
) -> proc_macro:: TokenStream {
19
- let mut attr = attr. into_iter ( ) ;
20
- let mut r#async = false ;
19
+ let mut attributes = Attributes :: default ( ) ;
20
+ let attribute_parser = syn:: meta:: parser ( |meta| attributes. parse ( meta) ) ;
21
+
22
+ syn:: parse_macro_input!( attr with attribute_parser) ;
21
23
let mut should_panic = None ;
22
- while let Some ( token) = attr. next ( ) {
23
- match & token {
24
- proc_macro:: TokenTree :: Ident ( i) if i. to_string ( ) == "async" => r#async = true ,
25
- _ => {
26
- return compile_error (
27
- token. span ( ) . into ( ) ,
28
- "malformed `#[wasm_bindgen_test]` attribute" ,
29
- )
30
- }
31
- }
32
- match & attr. next ( ) {
33
- Some ( proc_macro:: TokenTree :: Punct ( op) ) if op. as_char ( ) == ',' => { }
34
- Some ( _) => {
35
- return compile_error (
36
- token. span ( ) . into ( ) ,
37
- "malformed `#[wasm_bindgen_test]` attribute" ,
38
- )
39
- }
40
- None => break ,
41
- }
42
- }
43
24
44
25
let mut body = TokenStream :: from ( body) . into_iter ( ) . peekable ( ) ;
45
26
@@ -64,7 +45,7 @@ pub fn wasm_bindgen_test(
64
45
leading_tokens. push ( token. clone ( ) ) ;
65
46
if let TokenTree :: Ident ( token) = token {
66
47
if token == "async" {
67
- r#async = true ;
48
+ attributes . r#async = true ;
68
49
}
69
50
if token == "fn" {
70
51
break ;
@@ -83,7 +64,7 @@ pub fn wasm_bindgen_test(
83
64
None => quote ! { :: core:: option:: Option :: None } ,
84
65
} ;
85
66
86
- let test_body = if r#async {
67
+ let test_body = if attributes . r#async {
87
68
quote ! { cx. execute_async( test_name, #ident, #should_panic) ; }
88
69
} else {
89
70
quote ! { cx. execute_sync( test_name, #ident, #should_panic) ; }
@@ -93,10 +74,11 @@ pub fn wasm_bindgen_test(
93
74
// later slurp up all of these functions and pass them as arguments to the
94
75
// main test harness. This is the entry point for all tests.
95
76
let name = format_ident ! ( "__wbgt_{}_{}" , ident, CNT . fetch_add( 1 , Ordering :: SeqCst ) ) ;
77
+ let wasm_bindgen_path = attributes. wasm_bindgen_path ;
96
78
tokens. extend (
97
79
quote ! {
98
80
#[ no_mangle]
99
- pub extern "C" fn #name( cx: & :: wasm_bindgen_test :: __rt:: Context ) {
81
+ pub extern "C" fn #name( cx: & #wasm_bindgen_path :: __rt:: Context ) {
100
82
let test_name = :: core:: concat!( :: core:: module_path!( ) , "::" , :: core:: stringify!( #ident) ) ;
101
83
#test_body
102
84
}
@@ -205,3 +187,30 @@ fn find_ident(iter: &mut impl Iterator<Item = TokenTree>) -> Option<Ident> {
205
187
fn compile_error ( span : Span , msg : & str ) -> proc_macro:: TokenStream {
206
188
quote_spanned ! { span => compile_error!( #msg) ; } . into ( )
207
189
}
190
+
191
+ struct Attributes {
192
+ r#async : bool ,
193
+ wasm_bindgen_path : syn:: Path ,
194
+ }
195
+
196
+ impl Default for Attributes {
197
+ fn default ( ) -> Self {
198
+ Self {
199
+ r#async : false ,
200
+ wasm_bindgen_path : syn:: parse_quote!( :: wasm_bindgen_test) ,
201
+ }
202
+ }
203
+ }
204
+
205
+ impl Attributes {
206
+ fn parse ( & mut self , meta : syn:: meta:: ParseNestedMeta ) -> syn:: parse:: Result < ( ) > {
207
+ if meta. path . is_ident ( "async" ) {
208
+ self . r#async = true ;
209
+ } else if meta. path . is_ident ( "crate" ) {
210
+ self . wasm_bindgen_path = meta. value ( ) ?. parse :: < syn:: Path > ( ) ?;
211
+ } else {
212
+ return Err ( meta. error ( "unknown attribute" ) ) ;
213
+ }
214
+ Ok ( ( ) )
215
+ }
216
+ }
0 commit comments