diff --git a/src/callback.rs b/src/callback.rs index f3b544e..c8bedbd 100644 --- a/src/callback.rs +++ b/src/callback.rs @@ -78,7 +78,7 @@ where inner_system.apply_deferred(world); let mut runtime = world.get_resource_mut::().expect("No runtime resource"); - if R::is_current_thread() { + if R::needs_own_thread() { runtime.with_engine_mut(move |engine| { Out::into_runtime_value_with_engine(result, engine) }) @@ -110,7 +110,7 @@ macro_rules! impl_tuple { inner_system.initialize(world); let system_fn = move |args: In>, world: &mut World| { let mut runtime = world.get_resource_mut::().expect("No runtime resource"); - let args = if RN::is_current_thread() { + let args = if RN::needs_own_thread() { runtime.with_engine_mut(move |engine| { ( $($t::from_runtime_value_with_engine(args.get($idx).expect(&format!("Failed to get function argument for index {}", $idx)).clone(), engine), )+ @@ -127,7 +127,7 @@ macro_rules! impl_tuple { let result = inner_system.run(args, world); inner_system.apply_deferred(world); let mut runtime = world.get_resource_mut::().expect("No runtime resource"); - if RN::is_current_thread() { + if RN::needs_own_thread() { runtime.with_engine_mut(move |engine| { Out::into_runtime_value_with_engine(result, engine) }) diff --git a/src/lib.rs b/src/lib.rs index ceb7496..fb5ced6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -300,7 +300,7 @@ pub trait Runtime: Resource + Default { type Value: Send + Clone; type RawEngine; - fn is_current_thread() -> bool; + fn needs_own_thread() -> bool; /// Provides mutable reference to raw scripting engine instance. /// Can be used to directly interact with an interpreter to use interfaces diff --git a/src/runtimes/lua.rs b/src/runtimes/lua.rs index 256c249..4ee5bab 100644 --- a/src/runtimes/lua.rs +++ b/src/runtimes/lua.rs @@ -299,7 +299,7 @@ impl Runtime for LuaRuntime { self.with_engine(f) } - fn is_current_thread() -> bool { + fn needs_own_thread() -> bool { true } } diff --git a/src/runtimes/rhai.rs b/src/runtimes/rhai.rs index 09eb02e..6807450 100644 --- a/src/runtimes/rhai.rs +++ b/src/runtimes/rhai.rs @@ -208,7 +208,7 @@ impl Runtime for RhaiRuntime { self.with_engine(f) } - fn is_current_thread() -> bool { + fn needs_own_thread() -> bool { true } } diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index a4fe595..e0f706f 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -291,6 +291,28 @@ impl RubyValue { } } +impl RubyRuntime { + fn execute_in_thread( + &self, + f: impl FnOnce(&magnus::Ruby) -> T + Send + 'static, + ) -> T { + self.ruby_thread + .as_ref() + .unwrap() + .execute(Box::new(move |ruby| f(&ruby))) + } + + fn execute_in_thread_mut( + &self, + f: impl FnOnce(&mut magnus::Ruby) -> T + Send + 'static, + ) -> T { + self.ruby_thread + .as_ref() + .unwrap() + .execute(Box::new(move |mut ruby| f(&mut ruby))) + } +} + impl Runtime for RubyRuntime { type Schedule = RubySchedule; @@ -308,20 +330,14 @@ impl Runtime for RubyRuntime { &mut self, f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, ) -> T { - self.ruby_thread - .as_ref() - .unwrap() - .execute(Box::new(move |mut ruby| f(&mut ruby))) + self.execute_in_thread_mut(f) } fn with_engine_thread( &self, f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, ) -> T { - self.ruby_thread - .as_ref() - .unwrap() - .execute(Box::new(move |ruby| f(&ruby))) + self.execute_in_thread(f) } fn with_engine_mut(&mut self, _f: impl FnOnce(&mut Self::RawEngine) -> T) -> T { @@ -338,22 +354,19 @@ impl Runtime for RubyRuntime { entity: bevy::prelude::Entity, ) -> Result { let script = script.0.clone(); - self.ruby_thread - .as_ref() - .unwrap() - .execute(Box::new(move |ruby| { - let var = ruby - .class_object() - .const_get::<_, RModule>("Bevy") - .unwrap() - .const_get::<_, RClass>("Entity") - .unwrap(); - var.ivar_set("_current", BevyEntity(entity)).unwrap(); - let value = ruby.eval::(&script).unwrap(); - var.ivar_set("_current", ruby.qnil().as_value()).unwrap(); + self.execute_in_thread(Box::new(move |ruby: &Ruby| { + let var = ruby + .class_object() + .const_get::<_, RModule>("Bevy") + .unwrap() + .const_get::<_, RClass>("Entity") + .unwrap(); + var.ivar_set("_current", BevyEntity(entity)).unwrap(); + let value = ruby.eval::(&script).unwrap(); + var.ivar_set("_current", ruby.qnil().as_value()).unwrap(); - RubyValue::new(value) - })); + RubyValue::new(value) + })); Ok(RubyScriptData) } @@ -403,13 +416,10 @@ impl Runtime for RubyRuntime { result.into_value() } - self.ruby_thread - .as_ref() - .unwrap() - .execute(Box::new(move |ruby| { - ruby.define_global_function(&name, function!(callback, -1)); - RubyValue::nil(&ruby) - })); + self.execute_in_thread(Box::new(move |ruby: &Ruby| { + ruby.define_global_function(&name, function!(callback, -1)); + RubyValue::nil(&ruby) + })); Ok(()) } @@ -422,30 +432,26 @@ impl Runtime for RubyRuntime { args: impl for<'a> crate::FuncArgs<'a, Self::Value, Self> + Send + 'static, ) -> Result { let name = name.to_string(); - self.ruby_thread - .as_ref() - .unwrap() - .execute(Box::new(move |ruby| { - let var = ruby - .class_object() - .const_get::<_, RModule>("Bevy") - .unwrap() - .const_get::<_, RClass>("Entity") - .unwrap(); - var.ivar_set("_current", BevyEntity(entity)).unwrap(); + self.execute_in_thread(Box::new(move |ruby: &Ruby| { + let var = ruby + .class_object() + .const_get::<_, RModule>("Bevy") + .unwrap() + .const_get::<_, RClass>("Entity") + .unwrap(); + var.ivar_set("_current", BevyEntity(entity)).unwrap(); - let args: Vec<_> = args - .parse(&ruby) - .into_iter() - .map(|a| ruby.get_inner(a.0)) - .collect(); - let return_value: magnus::Value = - ruby.class_object().funcall(name, args.as_slice())?; + let args: Vec<_> = args + .parse(&ruby) + .into_iter() + .map(|a| ruby.get_inner(a.0)) + .collect(); + let return_value: magnus::Value = ruby.class_object().funcall(name, args.as_slice())?; - var.ivar_set("_current", ruby.qnil().as_value()).unwrap(); + var.ivar_set("_current", ruby.qnil().as_value()).unwrap(); - Ok(RubyValue::new(return_value)) - })) + Ok(RubyValue::new(return_value)) + })) } fn call_fn_from_value( @@ -471,7 +477,7 @@ impl Runtime for RubyRuntime { })) } - fn is_current_thread() -> bool { + fn needs_own_thread() -> bool { false } }