From 5f9191c9eec0d1f060c3283aa1cc1cdf8edd0bb2 Mon Sep 17 00:00:00 2001 From: makspll Date: Mon, 31 Mar 2025 20:23:25 +0100 Subject: [PATCH] feat: reduces size of `ScriptValue` to 64 bytes, moves some dynamic function methods into function info --- .../src/bindings/function/script_function.rs | 78 +++++-------------- .../src/docgen/info.rs | 12 +++ 2 files changed, 31 insertions(+), 59 deletions(-) diff --git a/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs b/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs index 543d5fe701..0b255995dd 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs @@ -70,24 +70,24 @@ impl FunctionCallContext { } } -#[derive(Clone, Reflect)] +#[derive(Reflect, Clone)] #[reflect(opaque)] /// A dynamic script function. pub struct DynamicScriptFunction { /// The meta information about the function - pub info: FunctionInfo, + pub info: Arc, // TODO: info about the function, this is hard right now because of non 'static lifetimes in wrappers, we can't use TypePath etc func: Arc< dyn Fn(FunctionCallContext, VecDeque) -> ScriptValue + Send + Sync + 'static, >, } -#[derive(Clone, Reflect)] +#[derive(Reflect, Clone)] #[reflect(opaque)] /// A dynamic mutable script function. pub struct DynamicScriptFunctionMut { /// The meta information about the function - pub info: FunctionInfo, + pub info: Arc, func: Arc< RwLock< // I'd rather consume an option or something instead of having the RWLock but I just wanna get this release out @@ -129,30 +129,9 @@ impl DynamicScriptFunction { } /// Set the meta information about the function - pub fn with_info(self, info: FunctionInfo) -> Self { - Self { info, ..self } - } - - /// Set the name of the function - pub fn with_name>>(self, name: N) -> Self { - Self { - info: FunctionInfo { - name: name.into(), - ..self.info - }, - func: self.func, - } - } - - /// Set the namespace of the function - pub fn with_namespace(self, namespace: Namespace) -> Self { - Self { - info: FunctionInfo { - namespace, - ..self.info - }, - func: self.func, - } + pub fn with_info(mut self, info: FunctionInfo) -> Self { + self.info = Arc::new(info); + self } } @@ -187,30 +166,9 @@ impl DynamicScriptFunctionMut { } /// Set the meta information about the function - pub fn with_info(self, info: FunctionInfo) -> Self { - Self { info, ..self } - } - - /// Set the name of the function - pub fn with_name>>(self, name: N) -> Self { - Self { - info: FunctionInfo { - name: name.into(), - ..self.info - }, - func: self.func, - } - } - - /// Set the namespace of the function - pub fn with_namespace(self, namespace: Namespace) -> Self { - Self { - info: FunctionInfo { - namespace, - ..self.info - }, - func: self.func, - } + pub fn with_info(mut self, info: FunctionInfo) -> Self { + self.info = Arc::new(info); + self } } @@ -248,10 +206,11 @@ where { fn from(fn_: F) -> Self { DynamicScriptFunction { - info: FunctionInfo::default(), + info: FunctionInfo::default() + .with_name(std::any::type_name::()) + .into(), func: Arc::new(fn_), } - .with_name(std::any::type_name::()) } } @@ -261,10 +220,11 @@ where { fn from(fn_: F) -> Self { DynamicScriptFunctionMut { - info: FunctionInfo::default(), + info: FunctionInfo::default() + .with_name(std::any::type_name::()) + .into(), func: Arc::new(RwLock::new(fn_)), } - .with_name(std::any::type_name::()) } } @@ -711,7 +671,7 @@ mod test { #[test] fn test_invalid_amount_of_args_errors_nicely() { let fn_ = |a: usize, b: usize| a + b; - let script_function = fn_.into_dynamic_script_function().with_name("my_fn"); + let script_function = fn_.into_dynamic_script_function(); with_local_world(|| { let out = script_function.call( @@ -723,7 +683,7 @@ mod test { assert_eq!( out.unwrap_err(), InteropError::function_interop_error( - "my_fn", + " usize>>::into_dynamic_script_function::{{closure}}", Namespace::Global, InteropError::argument_count_mismatch(2, 1) ) @@ -737,7 +697,7 @@ mod test { struct Comp; let fn_ = |_a: crate::bindings::function::from::Mut| 0usize; - let script_function = fn_.into_dynamic_script_function().with_name("my_fn"); + let script_function = fn_.into_dynamic_script_function(); with_local_world(|| { let out = script_function.call( diff --git a/crates/bevy_mod_scripting_core/src/docgen/info.rs b/crates/bevy_mod_scripting_core/src/docgen/info.rs index 1bb4c25fa1..b6a9ce5ae1 100644 --- a/crates/bevy_mod_scripting_core/src/docgen/info.rs +++ b/crates/bevy_mod_scripting_core/src/docgen/info.rs @@ -58,6 +58,18 @@ impl FunctionInfo { } } + /// Set the name of the function info. + pub fn with_name(mut self, name: impl Into>) -> Self { + self.name = name.into(); + self + } + + /// Set the namespace of the function info. + pub fn with_namespace(mut self, namespace: Namespace) -> Self { + self.namespace = namespace; + self + } + /// Add an argument to the function info. pub fn add_arg( mut self,