@@ -12,6 +12,7 @@ use syn::{spanned::Spanned, ImplItemFn, ItemImpl};
12
12
/// - `name`: the name to use to suffix the generated function, i.e. `test_fn` will generate `register_test_fn. Defaults to `functions`
13
13
/// - `remote`: If true the original impl block will be ignored, and only the function registrations will be generated
14
14
/// - `bms_core_path`: If set the path to override bms imports, normally only used internally
15
+ /// - `unregistered`: If set, will use `new_unregistered` instead of `new` for the namespace builder
15
16
#[ proc_macro_attribute]
16
17
pub fn script_bindings (
17
18
args : proc_macro:: TokenStream ,
@@ -45,10 +46,15 @@ pub fn script_bindings(
45
46
let bms_core_path = & args. bms_core_path ;
46
47
47
48
let function_name = format_ident ! ( "register_{}" , args. name) ;
49
+ let builder_function_name = if args. unregistered {
50
+ format_ident ! ( "new_unregistered" )
51
+ } else {
52
+ format_ident ! ( "new" )
53
+ } ;
48
54
49
55
let out = quote_spanned ! { impl_span=>
50
56
fn #function_name( world: & mut bevy:: ecs:: world:: World ) {
51
- #bms_core_path:: bindings:: function:: namespace:: NamespaceBuilder :: <#type_ident_with_generics>:: new ( world)
57
+ #bms_core_path:: bindings:: function:: namespace:: NamespaceBuilder :: <#type_ident_with_generics>:: #builder_function_name ( world)
52
58
#( #function_registrations) * ;
53
59
}
54
60
@@ -65,6 +71,8 @@ struct Args {
65
71
pub remote : bool ,
66
72
/// If set the path to override bms imports
67
73
pub bms_core_path : syn:: Path ,
74
+ /// If true will use `new_unregistered` instead of `new` for the namespace builder
75
+ pub unregistered : bool ,
68
76
}
69
77
70
78
impl syn:: parse:: Parse for Args {
@@ -75,6 +83,7 @@ impl syn::parse::Parse for Args {
75
83
76
84
let mut name = syn:: Ident :: new ( "functions" , Span :: call_site ( ) ) ;
77
85
let mut remote = false ;
86
+ let mut unregistered = false ;
78
87
let mut bms_core_path =
79
88
syn:: Path :: from ( syn:: Ident :: new ( "bevy_mod_scripting" , Span :: call_site ( ) ) ) ;
80
89
bms_core_path. segments . push ( syn:: PathSegment {
@@ -88,6 +97,9 @@ impl syn::parse::Parse for Args {
88
97
if path. is_ident ( "remote" ) {
89
98
remote = true ;
90
99
continue ;
100
+ } else if path. is_ident ( "unregistered" ) {
101
+ unregistered = true ;
102
+ continue ;
91
103
}
92
104
}
93
105
syn:: Meta :: NameValue ( name_value) => {
@@ -107,23 +119,24 @@ impl syn::parse::Parse for Args {
107
119
}
108
120
}
109
121
}
110
- _ => { }
122
+ _ => {
123
+ unknown_spans. push ( ( pair. span ( ) , "Unsupported meta kind for script_bindings" ) ) ;
124
+ continue ;
125
+ }
111
126
}
112
127
113
- unknown_spans. push ( pair. span ( ) ) ;
128
+ unknown_spans. push ( ( pair. span ( ) , "Unknown argument to script_bindings" ) ) ;
114
129
}
115
130
116
131
if !unknown_spans. is_empty ( ) {
117
- return Err ( syn:: Error :: new (
118
- unknown_spans[ 0 ] ,
119
- "Unknown argument to script_bindings" ,
120
- ) ) ;
132
+ return Err ( syn:: Error :: new ( unknown_spans[ 0 ] . 0 , unknown_spans[ 0 ] . 1 ) ) ;
121
133
}
122
134
123
135
Ok ( Self {
124
136
remote,
125
137
bms_core_path,
126
138
name,
139
+ unregistered,
127
140
} )
128
141
}
129
142
}
@@ -152,7 +165,8 @@ fn process_impl_fn(fun: &ImplItemFn) -> TokenStream {
152
165
. map ( |s| syn:: LitStr :: new ( & s, Span :: call_site ( ) ) )
153
166
. unwrap_or ( syn:: LitStr :: new ( "" , Span :: call_site ( ) ) ) ;
154
167
let fun_name = syn:: LitStr :: new ( & fun. sig . ident . to_string ( ) , Span :: call_site ( ) ) ;
155
- quote_spanned ! { Span :: call_site( ) =>
168
+ let fun_span = fun. sig . ident . span ( ) ;
169
+ quote_spanned ! { fun_span=>
156
170
. register_documented(
157
171
#fun_name,
158
172
|#args| #body,
0 commit comments