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 = {
times_called: 0
STATE = {
"times_called" => 0
}
def test_func
$state[:times_called] += 1
STATE["times_called"] += 1
end

View file

@ -50,21 +50,16 @@ fn hello(subject: String) -> String {
struct RubyEngine(Cleanup);
// TODO: Add SAFETY?
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 {
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::<magnus::value::Qnil>(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<Self::ScriptData, crate::ScriptingError> {
let engine =
RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() })));
let engine = engine.lock().unwrap();
engine.0.eval::<magnus::value::Qnil>(&script.0);
// let engine =
// RUBY_ENGINE.get_or_init(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() })));
let ruby = magnus::Ruby::get().unwrap();
ruby.eval::<magnus::value::Qnil>(&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<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 _: magnus::value::Value = ruby.class_object().funcall(name, ()).unwrap();
todo!();
Ok(RubyValue(()))
}
fn call_fn_from_value(

View file

@ -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) {