Ruby support #1

Open
jaroslaw wants to merge 165 commits from ruby into main
Showing only changes of commit 02d0662294 - Show all commits

View file

@ -1,6 +1,7 @@
use std::{ use std::{
cell::{LazyCell, OnceCell}, cell::{LazyCell, OnceCell},
sync::{LazyLock, Mutex, OnceLock}, sync::{LazyLock, Mutex, OnceLock},
thread::{self, JoinHandle},
}; };
use bevy::{ use bevy::{
@ -22,7 +23,9 @@ use crate::{
}; };
#[derive(Resource)] #[derive(Resource)]
pub struct RubyRuntime {} pub struct RubyRuntime {
ruby_thread: Option<JoinHandle<()>>,
}
#[derive(ScheduleLabel, Clone, PartialEq, Eq, Debug, Hash, Default)] #[derive(ScheduleLabel, Clone, PartialEq, Eq, Debug, Hash, Default)]
pub struct RubySchedule; pub struct RubySchedule;
@ -44,24 +47,30 @@ impl From<String> for RubyScript {
Self(value) Self(value)
} }
} }
fn hello(subject: String) -> String {
format!("hello, {}", subject)
}
struct RubyEngine(Cleanup); struct RubyEngine(Cleanup);
// TODO: Add SAFETY? // TODO: Add SAFETY?
unsafe impl Send for RubyEngine {} unsafe impl Send for RubyEngine {}
// TODO: thread local
static RUBY_ENGINE: LazyLock<Mutex<RubyEngine>> = static RUBY_ENGINE: LazyLock<Mutex<RubyEngine>> =
LazyLock::new(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() }))); LazyLock::new(|| Mutex::new(RubyEngine(unsafe { magnus::embed::init() })));
impl Default for RubyRuntime { impl Default for RubyRuntime {
fn default() -> Self { fn default() -> Self {
LazyLock::force(&RUBY_ENGINE); let ruby_thread = thread::spawn(|| {
Self {} let _cleanup = LazyLock::force(&RUBY_ENGINE);
while true {}
});
Self {
ruby_thread: Some(ruby_thread),
}
}
}
impl Drop for RubyRuntime {
fn drop(&mut self) {
let ruby_thread = self.ruby_thread.take().unwrap();
ruby_thread.join().unwrap();
} }
} }