get by name

This commit is contained in:
Jaroslaw Konik 2025-05-12 12:15:41 +02:00
parent 19bb6514ed
commit c46391871f
2 changed files with 8 additions and 6 deletions

View file

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

View file

@ -1,5 +1,6 @@
// TODO: make sure ruby is statically linked // TODO: make sure ruby is statically linked
use std::{ use std::{
collections::HashMap,
sync::{LazyLock, Mutex}, sync::{LazyLock, Mutex},
thread::{self, JoinHandle}, thread::{self, JoinHandle},
}; };
@ -156,7 +157,8 @@ impl Runtime for RubyRuntime {
) -> Result<(), crate::ScriptingError> { ) -> Result<(), crate::ScriptingError> {
static RUBY_CALLBACKS: LazyLock< static RUBY_CALLBACKS: LazyLock<
Mutex< Mutex<
Vec< HashMap<
String,
Box< Box<
dyn Fn( dyn Fn(
(), (),
@ -170,17 +172,17 @@ impl Runtime for RubyRuntime {
>, >,
>, >,
>, >,
> = LazyLock::new(|| Mutex::new(Vec::new())); > = LazyLock::new(|| Mutex::new(HashMap::new()));
let mut callbacks = RUBY_CALLBACKS.lock().unwrap(); let mut callbacks = RUBY_CALLBACKS.lock().unwrap();
callbacks.push(Box::new(f)); callbacks.insert(name.clone(), Box::new(f));
fn callback() -> magnus::Value { fn callback() -> magnus::Value {
let ruby = magnus::Ruby::get().unwrap(); let ruby = magnus::Ruby::get().unwrap();
let method_name: magnus::value::StaticSymbol = let method_name: magnus::value::StaticSymbol =
ruby.class_object().funcall("__method__", ()).unwrap(); ruby.class_object().funcall("__method__", ()).unwrap();
let method_name = method_name.to_string(); let method_name = method_name.to_string();
let mut callbacks = RUBY_CALLBACKS.lock().unwrap(); let callbacks = RUBY_CALLBACKS.lock().unwrap();
let f = callbacks.pop().unwrap(); let f = callbacks.get(&method_name).unwrap();
f((), vec![]); f((), vec![]);
ruby.qnil().as_value() ruby.qnil().as_value()
} }