@@ -12,6 +12,7 @@ use syn::{spanned::Spanned, ImplItemFn, ItemImpl};
1212/// - `name`: the name to use to suffix the generated function, i.e. `test_fn` will generate `register_test_fn. Defaults to `functions`
1313/// - `remote`: If true the original impl block will be ignored, and only the function registrations will be generated
1414/// - `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
1516#[ proc_macro_attribute]
1617pub fn script_bindings (
1718 args : proc_macro:: TokenStream ,
@@ -45,10 +46,15 @@ pub fn script_bindings(
4546 let bms_core_path = & args. bms_core_path ;
4647
4748 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+ } ;
4854
4955 let out = quote_spanned ! { impl_span=>
5056 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)
5258 #( #function_registrations) * ;
5359 }
5460
@@ -65,6 +71,8 @@ struct Args {
6571 pub remote : bool ,
6672 /// If set the path to override bms imports
6773 pub bms_core_path : syn:: Path ,
74+ /// If true will use `new_unregistered` instead of `new` for the namespace builder
75+ pub unregistered : bool ,
6876}
6977
7078impl syn:: parse:: Parse for Args {
@@ -75,6 +83,7 @@ impl syn::parse::Parse for Args {
7583
7684 let mut name = syn:: Ident :: new ( "functions" , Span :: call_site ( ) ) ;
7785 let mut remote = false ;
86+ let mut unregistered = false ;
7887 let mut bms_core_path =
7988 syn:: Path :: from ( syn:: Ident :: new ( "bevy_mod_scripting" , Span :: call_site ( ) ) ) ;
8089 bms_core_path. segments . push ( syn:: PathSegment {
@@ -88,6 +97,9 @@ impl syn::parse::Parse for Args {
8897 if path. is_ident ( "remote" ) {
8998 remote = true ;
9099 continue ;
100+ } else if path. is_ident ( "unregistered" ) {
101+ unregistered = true ;
102+ continue ;
91103 }
92104 }
93105 syn:: Meta :: NameValue ( name_value) => {
@@ -107,23 +119,24 @@ impl syn::parse::Parse for Args {
107119 }
108120 }
109121 }
110- _ => { }
122+ _ => {
123+ unknown_spans. push ( ( pair. span ( ) , "Unsupported meta kind for script_bindings" ) ) ;
124+ continue ;
125+ }
111126 }
112127
113- unknown_spans. push ( pair. span ( ) ) ;
128+ unknown_spans. push ( ( pair. span ( ) , "Unknown argument to script_bindings" ) ) ;
114129 }
115130
116131 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 ) ) ;
121133 }
122134
123135 Ok ( Self {
124136 remote,
125137 bms_core_path,
126138 name,
139+ unregistered,
127140 } )
128141 }
129142}
@@ -152,7 +165,8 @@ fn process_impl_fn(fun: &ImplItemFn) -> TokenStream {
152165 . map ( |s| syn:: LitStr :: new ( & s, Span :: call_site ( ) ) )
153166 . unwrap_or ( syn:: LitStr :: new ( "" , Span :: call_site ( ) ) ) ;
154167 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=>
156170 . register_documented(
157171 #fun_name,
158172 |#args| #body,
0 commit comments