Ruby support #1
2 changed files with 14 additions and 2 deletions
|
|
@ -32,6 +32,7 @@ mlua = { version = "0.9.8", features = [
|
||||||
"send",
|
"send",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
magnus = { version = "0.7.1", optional = true, features = ["embed"] }
|
magnus = { version = "0.7.1", optional = true, features = ["embed"] }
|
||||||
|
crossbeam-channel = "0.5.15"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "call_function_from_rust_rhai"
|
name = "call_function_from_rust_rhai"
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
use std::{
|
use std::{
|
||||||
cell::{LazyCell, OnceCell},
|
cell::{LazyCell, OnceCell},
|
||||||
sync::{LazyLock, Mutex, OnceLock},
|
sync::{LazyLock, Mutex, OnceLock},
|
||||||
|
|
@ -25,6 +26,7 @@ use crate::{
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
pub struct RubyRuntime {
|
pub struct RubyRuntime {
|
||||||
ruby_thread: Option<JoinHandle<()>>,
|
ruby_thread: Option<JoinHandle<()>>,
|
||||||
|
ruby_thread_sender: Option<crossbeam_channel::Sender<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(ScheduleLabel, Clone, PartialEq, Eq, Debug, Hash, Default)]
|
#[derive(ScheduleLabel, Clone, PartialEq, Eq, Debug, Hash, Default)]
|
||||||
|
|
@ -57,18 +59,23 @@ static RUBY_ENGINE: LazyLock<Mutex<RubyEngine>> =
|
||||||
|
|
||||||
impl Default for RubyRuntime {
|
impl Default for RubyRuntime {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let ruby_thread = thread::spawn(|| {
|
let (ruby_thread_sender, ruby_thread_receiver) = crossbeam_channel::unbounded::<()>();
|
||||||
|
let ruby_thread = thread::spawn(move || {
|
||||||
let _cleanup = LazyLock::force(&RUBY_ENGINE);
|
let _cleanup = LazyLock::force(&RUBY_ENGINE);
|
||||||
while true {}
|
while let Ok(val) = ruby_thread_receiver.recv() {
|
||||||
|
println!("received");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Self {
|
Self {
|
||||||
ruby_thread: Some(ruby_thread),
|
ruby_thread: Some(ruby_thread),
|
||||||
|
ruby_thread_sender: Some(ruby_thread_sender),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for RubyRuntime {
|
impl Drop for RubyRuntime {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
drop(self.ruby_thread_sender.take().unwrap());
|
||||||
let ruby_thread = self.ruby_thread.take().unwrap();
|
let ruby_thread = self.ruby_thread.take().unwrap();
|
||||||
ruby_thread.join().unwrap();
|
ruby_thread.join().unwrap();
|
||||||
}
|
}
|
||||||
|
|
@ -131,7 +138,11 @@ impl Runtime for RubyRuntime {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let sender = self.ruby_thread_sender.as_ref().clone();
|
||||||
|
let x = 5;
|
||||||
|
|
||||||
fn callback() -> magnus::Value {
|
fn callback() -> magnus::Value {
|
||||||
|
// sender.unwrap().send(());
|
||||||
let ruby = magnus::Ruby::get().unwrap();
|
let ruby = magnus::Ruby::get().unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
FUN.pop().unwrap()();
|
FUN.pop().unwrap()();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue