Ruby support #1

Open
jaroslaw wants to merge 165 commits from ruby into main
3 changed files with 18 additions and 7 deletions
Showing only changes of commit e95f025b07 - Show all commits

View file

@ -1,3 +1,3 @@
def test_func() def test_func()
rust_func() rust_func
end end

View file

@ -308,7 +308,10 @@ pub trait Runtime: Resource + Default {
/// Provides immutable reference to raw scripting engine instance. /// Provides immutable reference to raw scripting engine instance.
/// Can be used to directly interact with an interpreter to use interfaces /// Can be used to directly interact with an interpreter to use interfaces
/// that bevy_scriptum does not provided adapters for. /// that bevy_scriptum does not provided adapters for.
fn with_engine<T>(&self, f: impl FnOnce(&Self::RawEngine) -> T) -> T; fn with_engine<T: Send + 'static>(
&self,
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
) -> T;
fn eval( fn eval(
&self, &self,

View file

@ -1,3 +1,4 @@
// TODO: make sure ruby is statically linked
use std::{ use std::{
sync::LazyLock, sync::LazyLock,
thread::{self, JoinHandle}, thread::{self, JoinHandle},
@ -115,7 +116,10 @@ impl Runtime for RubyRuntime {
f(&mut magnus::Ruby::get().unwrap()) f(&mut magnus::Ruby::get().unwrap())
} }
fn with_engine<T>(&self, f: impl FnOnce(&Self::RawEngine) -> T) -> T { fn with_engine<T: Send + 'static>(
&self,
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
) -> T {
RUBY_THREAD.execute_in(Box::new(move |ruby| f(&ruby))) RUBY_THREAD.execute_in(Box::new(move |ruby| f(&ruby)))
} }
@ -136,7 +140,7 @@ impl Runtime for RubyRuntime {
&mut self, &mut self,
name: String, name: String,
_arg_types: Vec<std::any::TypeId>, _arg_types: Vec<std::any::TypeId>,
_f: impl Fn( f: impl Fn(
Self::CallContext, Self::CallContext,
Vec<Self::Value>, Vec<Self::Value>,
) -> Result< ) -> Result<
@ -146,13 +150,17 @@ impl Runtime for RubyRuntime {
+ Sync + Sync
+ 'static, + 'static,
) -> Result<(), crate::ScriptingError> { ) -> Result<(), crate::ScriptingError> {
fn callback(_val: magnus::Value) -> magnus::Value { fn callback() -> magnus::Value {
let ruby = magnus::Ruby::get().unwrap(); let ruby = magnus::Ruby::get().unwrap();
let method_name: magnus::value::StaticSymbol =
ruby.class_object().funcall("__method__", ()).unwrap();
let method_name = method_name.to_string();
dbg!(method_name);
ruby.qnil().as_value() ruby.qnil().as_value()
} }
RUBY_THREAD.execute_in(Box::new(move |ruby| { RUBY_THREAD.execute_in(Box::new(move |ruby| {
ruby.define_global_function(&name, function!(callback, 1)); ruby.define_global_function(&name, function!(callback, 0));
RubyValue(()) RubyValue(())
})); }));
@ -168,7 +176,7 @@ impl Runtime for RubyRuntime {
) -> Result<Self::Value, crate::ScriptingError> { ) -> Result<Self::Value, crate::ScriptingError> {
let name = name.to_string(); let name = name.to_string();
RUBY_THREAD.execute_in(Box::new(move |ruby| { RUBY_THREAD.execute_in(Box::new(move |ruby| {
let _: Result<magnus::Value, _> = ruby.class_object().funcall(name, ()); let _: magnus::Value = ruby.class_object().funcall(name, ()).unwrap();
RubyValue(()) RubyValue(())
})); }));