diff --git a/assets/tests/ruby/script_function_gets_called_from_rust.rb b/assets/tests/ruby/script_function_gets_called_from_rust.rb index aaf3683..2ed4705 100644 --- a/assets/tests/ruby/script_function_gets_called_from_rust.rb +++ b/assets/tests/ruby/script_function_gets_called_from_rust.rb @@ -1,7 +1,7 @@ -$state = { - times_called: 0 +STATE = { + "times_called" => 0 } def test_func - $state[:times_called] += 1 + STATE["times_called"] += 1 end diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index 66f9afc..bd1b9bd 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -50,21 +50,16 @@ fn hello(subject: String) -> String { struct RubyEngine(Cleanup); +// TODO: Add SAFETY? unsafe impl Send for RubyEngine {} -static RUBY_ENGINE: OnceLock> = OnceLock::new(); +// TODO: thread local +static RUBY_ENGINE: LazyLock> = + LazyLock::new(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); impl Default for RubyRuntime { fn default() -> Self { - RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); - // TODO: Add SAFETY? - - // engine.define_global_function("hello", magnus::function!(hello, 1)); - // engine - // .eval::(r#"puts hello("world")"#) - // .unwrap(); - // - // Self { engine } + LazyLock::force(&RUBY_ENGINE); Self {} } } @@ -98,10 +93,10 @@ impl Runtime for RubyRuntime { script: &Self::ScriptAsset, entity: bevy::prelude::Entity, ) -> Result { - let engine = - RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); - let engine = engine.lock().unwrap(); - engine.0.eval::(&script.0); + // let engine = + // RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); + let ruby = magnus::Ruby::get().unwrap(); + ruby.eval::(&script.0); Ok(RubyScriptData) } @@ -129,11 +124,9 @@ impl Runtime for RubyRuntime { entity: bevy::prelude::Entity, args: impl for<'a> crate::FuncArgs<'a, Self::Value, Self>, ) -> Result { - let engine = - RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); let ruby = magnus::Ruby::get().unwrap(); let _: magnus::value::Value = ruby.class_object().funcall(name, ()).unwrap(); - todo!(); + Ok(RubyValue(())) } fn call_fn_from_value( diff --git a/tests/tests.rs b/tests/tests.rs index 1b98a95..8d2a022 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -487,12 +487,19 @@ mod lua_tests { mod ruby_tests { use bevy::prelude::*; use bevy_scriptum::runtimes::ruby::{prelude::*, RubyScriptData}; + use magnus::{value::ReprValue, Module, Object, Ruby}; impl AssertStateKeyValue for RubyRuntime { type ScriptData = RubyScriptData; fn assert_state_key_value_i64(world: &World, _entity_id: Entity, key: &str, value: i64) { - todo!(); + let state: magnus::value::Value = Ruby::get() + .unwrap() + .class_object() + .const_get("STATE") + .unwrap(); + let res: i64 = state.funcall_public("[]", (key.to_string(),)).unwrap(); + assert_eq!(res, value) } fn assert_state_key_value_i32(world: &World, _entity_id: Entity, key: &str, value: i32) {