pass simple test

This commit is contained in:
Jaroslaw Konik 2025-05-09 13:40:29 +02:00
parent 417f4a1bfa
commit 6be5a4dec1
3 changed files with 21 additions and 21 deletions

View file

@ -1,7 +1,7 @@
$state = { STATE = {
times_called: 0 "times_called" => 0
} }
def test_func def test_func
$state[:times_called] += 1 STATE["times_called"] += 1
end end

View file

@ -50,21 +50,16 @@ fn hello(subject: String) -> String {
struct RubyEngine(Cleanup); struct RubyEngine(Cleanup);
// TODO: Add SAFETY?
unsafe impl Send for RubyEngine {} unsafe impl Send for RubyEngine {}
static RUBY_ENGINE: OnceLock<Mutex<RubyEngine>> = OnceLock::new(); // TODO: thread local
static RUBY_ENGINE: LazyLock<Mutex<RubyEngine>> =
LazyLock::new(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() })));
impl Default for RubyRuntime { impl Default for RubyRuntime {
fn default() -> Self { fn default() -> Self {
RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); LazyLock::force(&RUBY_ENGINE);
// TODO: Add SAFETY?
// engine.define_global_function("hello", magnus::function!(hello, 1));
// engine
// .eval::<magnus::value::Qnil>(r#"puts hello("world")"#)
// .unwrap();
//
// Self { engine }
Self {} Self {}
} }
} }
@ -98,10 +93,10 @@ impl Runtime for RubyRuntime {
script: &Self::ScriptAsset, script: &Self::ScriptAsset,
entity: bevy::prelude::Entity, entity: bevy::prelude::Entity,
) -> Result<Self::ScriptData, crate::ScriptingError> { ) -> Result<Self::ScriptData, crate::ScriptingError> {
let engine = // let engine =
RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); // RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() })));
let engine = engine.lock().unwrap(); let ruby = magnus::Ruby::get().unwrap();
engine.0.eval::<magnus::value::Qnil>(&script.0); ruby.eval::<magnus::value::Qnil>(&script.0);
Ok(RubyScriptData) Ok(RubyScriptData)
} }
@ -129,11 +124,9 @@ impl Runtime for RubyRuntime {
entity: bevy::prelude::Entity, entity: bevy::prelude::Entity,
args: impl for<'a> crate::FuncArgs<'a, Self::Value, Self>, args: impl for<'a> crate::FuncArgs<'a, Self::Value, Self>,
) -> Result<Self::Value, crate::ScriptingError> { ) -> Result<Self::Value, crate::ScriptingError> {
let engine =
RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() })));
let ruby = magnus::Ruby::get().unwrap(); let ruby = magnus::Ruby::get().unwrap();
let _: magnus::value::Value = ruby.class_object().funcall(name, ()).unwrap(); let _: magnus::value::Value = ruby.class_object().funcall(name, ()).unwrap();
todo!(); Ok(RubyValue(()))
} }
fn call_fn_from_value( fn call_fn_from_value(

View file

@ -487,12 +487,19 @@ mod lua_tests {
mod ruby_tests { mod ruby_tests {
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::runtimes::ruby::{prelude::*, RubyScriptData}; use bevy_scriptum::runtimes::ruby::{prelude::*, RubyScriptData};
use magnus::{value::ReprValue, Module, Object, Ruby};
impl AssertStateKeyValue for RubyRuntime { impl AssertStateKeyValue for RubyRuntime {
type ScriptData = RubyScriptData; type ScriptData = RubyScriptData;
fn assert_state_key_value_i64(world: &World, _entity_id: Entity, key: &str, value: i64) { 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) { fn assert_state_key_value_i32(world: &World, _entity_id: Entity, key: &str, value: i32) {