Ruby support #1

Open
jaroslaw wants to merge 165 commits from ruby into main
4 changed files with 29 additions and 35 deletions
Showing only changes of commit d02b6375c6 - Show all commits

View file

@ -260,14 +260,14 @@ impl Runtime for LuaRuntime {
fn with_engine_thread_mut<T: Send + 'static>( fn with_engine_thread_mut<T: Send + 'static>(
&mut self, &mut self,
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, _f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
) -> T { ) -> T {
todo!() todo!()
} }
fn with_engine_thread<T: Send + 'static>( fn with_engine_thread<T: Send + 'static>(
&self, &self,
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, _f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
) -> T { ) -> T {
todo!() todo!()
} }

View file

@ -163,14 +163,14 @@ impl Runtime for RhaiRuntime {
fn with_engine_thread_mut<T: Send + 'static>( fn with_engine_thread_mut<T: Send + 'static>(
&mut self, &mut self,
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, _f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
) -> T { ) -> T {
todo!() todo!()
} }
fn with_engine_thread<T: Send + 'static>( fn with_engine_thread<T: Send + 'static>(
&self, &self,
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, _f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
) -> T { ) -> T {
todo!() todo!()
} }

View file

@ -2,7 +2,7 @@
// TODO: make sure ruby is statically linked // TODO: make sure ruby is statically linked
use std::{ use std::{
collections::HashMap, collections::HashMap,
sync::{Arc, Condvar, LazyLock, Mutex, MutexGuard}, sync::{Arc, Condvar, LazyLock, Mutex},
thread::{self, JoinHandle}, thread::{self, JoinHandle},
}; };
@ -12,13 +12,12 @@ use bevy::{
reflect::TypePath, reflect::TypePath,
}; };
use magnus::Ruby; use magnus::Ruby;
use magnus::{embed::Cleanup, function, prelude::*}; use magnus::{function, prelude::*};
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::{
assets::GetExtensions, assets::GetExtensions,
callback::{FromRuntimeValueWithEngine, IntoRuntimeValueWithEngine}, callback::{FromRuntimeValueWithEngine, IntoRuntimeValueWithEngine},
runtimes::ruby,
FuncArgs, Runtime, FuncArgs, Runtime,
}; };
@ -47,10 +46,11 @@ impl From<String> for RubyScript {
Self(value) Self(value)
} }
} }
struct RubyEngine(Cleanup);
type RubyClosure = Box<dyn FnOnce(Ruby) + Send>;
struct RubyThread { struct RubyThread {
sender: Option<crossbeam_channel::Sender<Box<dyn FnOnce(Ruby) + Send>>>, sender: Option<crossbeam_channel::Sender<RubyClosure>>,
handle: Option<JoinHandle<()>>, handle: Option<JoinHandle<()>>,
} }
@ -157,11 +157,11 @@ impl Runtime for RubyRuntime {
.execute(Box::new(move |ruby| f(&ruby))) .execute(Box::new(move |ruby| f(&ruby)))
} }
fn with_engine_mut<T>(&mut self, f: impl FnOnce(&mut Self::RawEngine) -> T) -> T { fn with_engine_mut<T>(&mut self, _f: impl FnOnce(&mut Self::RawEngine) -> T) -> T {
unimplemented!(); unimplemented!();
} }
fn with_engine<T>(&self, f: impl FnOnce(&Self::RawEngine) -> T) -> T { fn with_engine<T>(&self, _f: impl FnOnce(&Self::RawEngine) -> T) -> T {
unimplemented!(); unimplemented!();
} }
@ -195,24 +195,18 @@ impl Runtime for RubyRuntime {
+ Sync + Sync
+ 'static, + 'static,
) -> Result<(), crate::ScriptingError> { ) -> Result<(), crate::ScriptingError> {
static RUBY_CALLBACKS: LazyLock< type CallbackClosure = Box<
Mutex<
HashMap<
String,
Box<
dyn Fn( dyn Fn(
(), (),
Vec<RubyValue>, Vec<RubyValue>,
) -> Result< )
crate::promise::Promise<(), RubyValue>, -> Result<crate::promise::Promise<(), RubyValue>, crate::ScriptingError>
crate::ScriptingError, + Send
> + Send
+ Sync + Sync
+ 'static, + 'static,
>, >;
>, static RUBY_CALLBACKS: LazyLock<Mutex<HashMap<String, CallbackClosure>>> =
>, LazyLock::new(|| Mutex::new(HashMap::new()));
> = LazyLock::new(|| Mutex::new(HashMap::new()));
let mut callbacks = RUBY_CALLBACKS.lock().unwrap(); let mut callbacks = RUBY_CALLBACKS.lock().unwrap();
callbacks.insert(name.clone(), Box::new(f)); callbacks.insert(name.clone(), Box::new(f));
@ -223,7 +217,7 @@ impl Runtime for RubyRuntime {
let method_name = method_name.to_string(); let method_name = method_name.to_string();
let callbacks = RUBY_CALLBACKS.lock().unwrap(); let callbacks = RUBY_CALLBACKS.lock().unwrap();
let f = callbacks.get(&method_name).unwrap(); let f = callbacks.get(&method_name).unwrap();
f((), vec![]); f((), vec![]).unwrap();
ruby.qnil().as_value() ruby.qnil().as_value()
} }

View file

@ -487,7 +487,7 @@ 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}; use magnus::{value::ReprValue, Module};
impl AssertStateKeyValue for RubyRuntime { impl AssertStateKeyValue for RubyRuntime {
type ScriptData = RubyScriptData; type ScriptData = RubyScriptData;
@ -502,15 +502,15 @@ mod ruby_tests {
}) })
} }
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) {
todo!(); todo!();
} }
fn assert_state_key_value_string( fn assert_state_key_value_string(
world: &World, _world: &World,
_entity_id: Entity, _entity_id: Entity,
key: &str, _key: &str,
value: &str, _value: &str,
) { ) {
todo!(); todo!();
} }