From c5a9f54695b1cbb2742a2497f9bf7f8f82d8e93e Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Fri, 16 May 2025 07:00:00 +0200 Subject: [PATCH] pass entity --- assets/tests/ruby/pass_entity_from_script.rb | 3 ++ src/runtimes/lua.rs | 11 ++-- src/runtimes/rhai.rs | 11 ++-- src/runtimes/ruby.rs | 20 ++++++++ tests/tests.rs | 53 ++++++++++++++++---- 5 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 assets/tests/ruby/pass_entity_from_script.rb diff --git a/assets/tests/ruby/pass_entity_from_script.rb b/assets/tests/ruby/pass_entity_from_script.rb new file mode 100644 index 0000000..d62632b --- /dev/null +++ b/assets/tests/ruby/pass_entity_from_script.rb @@ -0,0 +1,3 @@ +def test_func + rust_func($entity) +end diff --git a/src/runtimes/lua.rs b/src/runtimes/lua.rs index eda2ba2..7ce1745 100644 --- a/src/runtimes/lua.rs +++ b/src/runtimes/lua.rs @@ -1,6 +1,7 @@ use bevy::{ asset::Asset, ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel}, + log, math::Vec3, reflect::TypePath, }; @@ -260,16 +261,18 @@ impl Runtime for LuaRuntime { fn with_engine_thread_mut( &mut self, - _f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, + f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, ) -> T { - todo!() + log::warn!("runtime can be used on current thread, wil run on current thread"); + self.with_engine_mut(f) } fn with_engine_thread( &self, - _f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, + f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, ) -> T { - todo!() + log::warn!("runtime can be used on current thread, wil run on current thread"); + self.with_engine(f) } fn is_current_thread() -> bool { diff --git a/src/runtimes/rhai.rs b/src/runtimes/rhai.rs index 5f486e8..5228075 100644 --- a/src/runtimes/rhai.rs +++ b/src/runtimes/rhai.rs @@ -3,6 +3,7 @@ use std::fmt::Debug; use bevy::{ asset::Asset, ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel}, + log, math::Vec3, reflect::TypePath, }; @@ -163,16 +164,18 @@ impl Runtime for RhaiRuntime { fn with_engine_thread_mut( &mut self, - _f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, + f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, ) -> T { - todo!() + log::warn!("runtime can be used on current thread, wil run on current thread"); + self.with_engine_mut(f) } fn with_engine_thread( &self, - _f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, + f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, ) -> T { - todo!() + log::warn!("runtime can be used on current thread, wil run on current thread"); + self.with_engine(f) } fn is_current_thread() -> bool { diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index 83306a0..c0bf625 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -127,6 +127,12 @@ unsafe impl TypedData for Promise<(), RubyValue> { } } +impl TryConvert for Promise<(), RubyValue> { + fn try_convert(val: magnus::Value) -> Result { + TryConvert::try_convert(val).map(|p: &Self| p.clone()) + } +} + fn then(r_self: magnus::Value) -> magnus::Value { let promise: &Promise<(), RubyValue> = TryConvert::try_convert(r_self).unwrap(); let ruby = Ruby::get().unwrap(); @@ -143,6 +149,7 @@ fn then(r_self: magnus::Value) -> magnus::Value { .into_value() } +#[derive(Clone)] #[magnus::wrap(class = "BevyEntity")] pub struct BevyEntity(pub Entity); @@ -152,6 +159,13 @@ impl BevyEntity { } } +impl TryConvert for BevyEntity { + fn try_convert(val: magnus::Value) -> Result { + TryConvert::try_convert(val).map(|p: &Self| p.clone()) + } +} + +#[derive(Clone)] #[magnus::wrap(class = "BevyVec3")] pub struct BevyVec3(pub Vec3); @@ -169,6 +183,12 @@ impl BevyVec3 { } } +impl TryConvert for BevyVec3 { + fn try_convert(val: magnus::Value) -> Result { + TryConvert::try_convert(val).map(|p: &Self| p.clone()) + } +} + impl Default for RubyRuntime { fn default() -> Self { let (lock, cvar) = &*Arc::clone(&RUBY_THREAD); diff --git a/tests/tests.rs b/tests/tests.rs index b3c7a3a..83b5332 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -65,7 +65,7 @@ trait AssertStateKeyValue { #[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))] macro_rules! scripting_tests { - ($runtime:ty, $script:literal, $extension:literal) => { + ($runtime:ty, $script:literal, $extension:literal, $entity_type: ty) => { use super::*; #[test] @@ -380,7 +380,7 @@ macro_rules! scripting_tests { } #[test] - fn entity_variable_is_available_in_callback() { + fn entity_variable_index_is_available_in_callback() { let mut app = build_test_app(); #[derive(Default, Resource)] @@ -412,12 +412,12 @@ macro_rules! scripting_tests { } #[test] - fn entity_variable_is_available_in_eval() { + fn entity_variable_index_is_available_in_eval() { let mut app = build_test_app(); #[derive(Default, Resource)] struct State { - index: u32, + index: Option, } app.world_mut().init_resource::(); @@ -426,7 +426,7 @@ macro_rules! scripting_tests { runtime.add_function( String::from("rust_func"), |In((index,)): In<(u32,)>, mut res: ResMut| { - res.index = index; + res.index = Some(index); }, ); }); @@ -439,7 +439,39 @@ macro_rules! scripting_tests { assert_eq!( app.world().get_resource::().unwrap().index, - entity.index() + Some(entity.index()) + ); + } + + #[test] + fn pass_entity_from_script() { + let mut app = build_test_app(); + + #[derive(Default, Resource)] + struct State { + index: Option, + } + + app.world_mut().init_resource::(); + + app.add_scripting::<$runtime>(|runtime| { + runtime.add_function( + String::from("rust_func"), + |In((entity,)): In<($entity_type,)>, mut res: ResMut| { + res.index = Some(entity.0.index()); + }, + ); + }); + + let entity = run_script::<$runtime, _, _>( + &mut app, + format!("tests/{}/pass_entity_from_script.{}", $script, $extension).to_string(), + call_script_on_update_from_rust::<$runtime>, + ); + + assert_eq!( + app.world().get_resource::().unwrap().index, + Some(entity.index()) ); } }; @@ -450,6 +482,9 @@ mod rhai_tests { use bevy::prelude::*; use bevy_scriptum::runtimes::rhai::prelude::*; + #[derive(Clone)] + struct BevyEntity(Entity); + impl AssertStateKeyValue for RhaiRuntime { type ScriptData = RhaiScriptData; @@ -472,7 +507,7 @@ mod rhai_tests { } } - scripting_tests!(RhaiRuntime, "rhai", "rhai"); + scripting_tests!(RhaiRuntime, "rhai", "rhai", BevyEntity); } #[cfg(feature = "lua")] @@ -514,7 +549,7 @@ mod lua_tests { } } - scripting_tests!(LuaRuntime, "lua", "lua"); + scripting_tests!(LuaRuntime, "lua", "lua", BevyEntity); } #[cfg(feature = "ruby")] @@ -563,5 +598,5 @@ mod ruby_tests { } } - scripting_tests!(RubyRuntime, "ruby", "rb"); + scripting_tests!(RubyRuntime, "ruby", "rb", BevyEntity); }