From b307cf6c13b5eb7f819017cc3819fd0bc24b1c03 Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Fri, 9 May 2025 18:42:37 +0200 Subject: [PATCH] call rust from ruby --- src/runtimes/ruby.rs | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index 7e808f3..b1a712f 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -79,10 +79,10 @@ impl Runtime for RubyRuntime { type Value = RubyValue; - type RawEngine = (); + type RawEngine = magnus::Ruby; fn with_engine_mut(&mut self, f: impl FnOnce(&mut Self::RawEngine) -> T) -> T { - todo!() + f(&mut magnus::Ruby::get().unwrap()) } fn with_engine(&self, f: impl FnOnce(&Self::RawEngine) -> T) -> T { @@ -115,16 +115,22 @@ impl Runtime for RubyRuntime { ) -> Result<(), crate::ScriptingError> { let ruby = magnus::Ruby::get().unwrap(); - unsafe extern "C" fn callback(val: magnus::Value) -> magnus::Value { + static mut FUN: Vec> = Vec::new(); + unsafe { + FUN.push(Box::new(move || { + f((), vec![]).unwrap(); + })); + } + + fn callback() -> magnus::Value { let ruby = magnus::Ruby::get().unwrap(); - // f(); + unsafe { + FUN.pop().unwrap()(); + } ruby.qnil().as_value() } - ruby.define_global_function( - &name, - callback as unsafe extern "C" fn(magnus::Value) -> magnus::Value, - ); + ruby.define_global_function(&name, function!(callback, 0)); Ok(()) } @@ -155,25 +161,25 @@ pub mod prelude { } impl FromRuntimeValueWithEngine<'_, RubyRuntime> for T { - fn from_runtime_value_with_engine(value: RubyValue, engine: &()) -> Self { + fn from_runtime_value_with_engine(value: RubyValue, engine: &magnus::Ruby) -> Self { todo!(); } } impl IntoRuntimeValueWithEngine<'_, T, RubyRuntime> for T { - fn into_runtime_value_with_engine(value: T, engine: &()) -> RubyValue { - todo!(); + fn into_runtime_value_with_engine(value: T, engine: &magnus::Ruby) -> RubyValue { + RubyValue(()) } } impl FuncArgs<'_, RubyValue, RubyRuntime> for () { - fn parse(self, _engine: &()) -> Vec { + fn parse(self, _engine: &magnus::Ruby) -> Vec { Vec::new() } } impl FuncArgs<'_, RubyValue, RubyRuntime> for Vec { - fn parse(self, engine: &()) -> Vec { + fn parse(self, engine: &magnus::Ruby) -> Vec { self.into_iter().map(|x| RubyValue(())).collect() } } @@ -183,7 +189,7 @@ macro_rules! impl_tuple { impl<'a, $($t,)+> FuncArgs<'a, RubyValue, RubyRuntime> for ($($t,)+) { - fn parse(self, engine: &'a ()) -> Vec { + fn parse(self, engine: &'a magnus::Ruby) -> Vec { todo!(); } }