Skip to content

feat: refactor core bindings to use new derive macro #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/bevy_mod_scripting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
filters: |
src:
- 'src/**'
- 'crates/**'
- 'examples/**'
- 'assets/**'
- 'docs/**'
- '.github/workflows/bevy_mod_scripting.yml'

Expand Down
30 changes: 22 additions & 8 deletions crates/bevy_mod_scripting_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use syn::{spanned::Spanned, ImplItemFn, ItemImpl};
/// - `name`: the name to use to suffix the generated function, i.e. `test_fn` will generate `register_test_fn. Defaults to `functions`
/// - `remote`: If true the original impl block will be ignored, and only the function registrations will be generated
/// - `bms_core_path`: If set the path to override bms imports, normally only used internally
/// - `unregistered`: If set, will use `new_unregistered` instead of `new` for the namespace builder
#[proc_macro_attribute]
pub fn script_bindings(
args: proc_macro::TokenStream,
Expand Down Expand Up @@ -45,10 +46,15 @@ pub fn script_bindings(
let bms_core_path = &args.bms_core_path;

let function_name = format_ident!("register_{}", args.name);
let builder_function_name = if args.unregistered {
format_ident!("new_unregistered")
} else {
format_ident!("new")
};

let out = quote_spanned! {impl_span=>
fn #function_name(world: &mut bevy::ecs::world::World) {
#bms_core_path::bindings::function::namespace::NamespaceBuilder::<#type_ident_with_generics>::new(world)
#bms_core_path::bindings::function::namespace::NamespaceBuilder::<#type_ident_with_generics>::#builder_function_name(world)
#(#function_registrations)*;
}

Expand All @@ -65,6 +71,8 @@ struct Args {
pub remote: bool,
/// If set the path to override bms imports
pub bms_core_path: syn::Path,
/// If true will use `new_unregistered` instead of `new` for the namespace builder
pub unregistered: bool,
}

impl syn::parse::Parse for Args {
Expand All @@ -75,6 +83,7 @@ impl syn::parse::Parse for Args {

let mut name = syn::Ident::new("functions", Span::call_site());
let mut remote = false;
let mut unregistered = false;
let mut bms_core_path =
syn::Path::from(syn::Ident::new("bevy_mod_scripting", Span::call_site()));
bms_core_path.segments.push(syn::PathSegment {
Expand All @@ -88,6 +97,9 @@ impl syn::parse::Parse for Args {
if path.is_ident("remote") {
remote = true;
continue;
} else if path.is_ident("unregistered") {
unregistered = true;
continue;
}
}
syn::Meta::NameValue(name_value) => {
Expand All @@ -107,23 +119,24 @@ impl syn::parse::Parse for Args {
}
}
}
_ => {}
_ => {
unknown_spans.push((pair.span(), "Unsupported meta kind for script_bindings"));
continue;
}
}

unknown_spans.push(pair.span());
unknown_spans.push((pair.span(), "Unknown argument to script_bindings"));
}

if !unknown_spans.is_empty() {
return Err(syn::Error::new(
unknown_spans[0],
"Unknown argument to script_bindings",
));
return Err(syn::Error::new(unknown_spans[0].0, unknown_spans[0].1));
}

Ok(Self {
remote,
bms_core_path,
name,
unregistered,
})
}
}
Expand Down Expand Up @@ -152,7 +165,8 @@ fn process_impl_fn(fun: &ImplItemFn) -> TokenStream {
.map(|s| syn::LitStr::new(&s, Span::call_site()))
.unwrap_or(syn::LitStr::new("", Span::call_site()));
let fun_name = syn::LitStr::new(&fun.sig.ident.to_string(), Span::call_site());
quote_spanned! {Span::call_site()=>
let fun_span = fun.sig.ident.span();
quote_spanned! {fun_span=>
.register_documented(
#fun_name,
|#args| #body,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_mod_scripting_functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ profiling = { workspace = true }
uuid = "1.11"
smol_str = "0.2.2"
bevy_mod_scripting_core = { workspace = true }
bevy_mod_scripting_derive = { workspace = true }

[lints]
workspace = true
Loading
Loading