parse args

This commit is contained in:
Jaroslaw Konik 2025-05-14 10:48:20 +02:00
parent 356e552ce7
commit 7e886a5eef

View file

@ -124,7 +124,13 @@ impl Drop for RubyRuntime {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct RubyValue(()); pub struct RubyValue(magnus::value::Opaque<magnus::Value>);
impl RubyValue {
fn nil(ruby: &Ruby) -> Self {
Self(magnus::value::Opaque::from(ruby.qnil().as_value()))
}
}
impl Runtime for RubyRuntime { impl Runtime for RubyRuntime {
type Schedule = RubySchedule; type Schedule = RubySchedule;
@ -178,7 +184,7 @@ impl Runtime for RubyRuntime {
.unwrap() .unwrap()
.execute(Box::new(move |ruby| { .execute(Box::new(move |ruby| {
ruby.eval::<magnus::value::Value>(&script).unwrap(); ruby.eval::<magnus::value::Value>(&script).unwrap();
RubyValue(()) RubyValue::nil(&ruby)
})); }));
Ok(RubyScriptData) Ok(RubyScriptData)
} }
@ -212,48 +218,24 @@ impl Runtime for RubyRuntime {
let mut callbacks = RUBY_CALLBACKS.lock().unwrap(); let mut callbacks = RUBY_CALLBACKS.lock().unwrap();
callbacks.insert(name.clone(), Box::new(f)); callbacks.insert(name.clone(), Box::new(f));
unsafe extern "C" fn callback(argc: i32, argv: *mut VALUE, r_self: VALUE) -> VALUE { fn callback(args: &[magnus::Value]) -> magnus::Value {
let fmt = CString::new("1").unwrap();
let x: VALUE = Default::default();
rb_scan_args(argc, argv, fmt.as_ptr(), &x);
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 callbacks = RUBY_CALLBACKS.lock().unwrap(); let callbacks = RUBY_CALLBACKS.lock().unwrap();
let f = callbacks.get(&method_name).unwrap(); let f = callbacks.get(&method_name).unwrap();
f((), args.iter().map(|_arg| RubyValue::nil(&ruby)).collect())
let args = magnus::RArray::from_value(magnus::Value::from_raw(*argv).into()) .expect("failed to call callback");
.unwrap() ruby.qnil().as_value()
.to_value_array::<1>()
.expect("failed to get args array");
for arg in args {
dbg!(arg);
}
// let args = args
// .parse(&self.engine)
// .into_iter()
// .map(|a| a.0)
// .collect::<Vec<Dynamic>>();
f((), vec![]).expect("failed to call callback");
todo!()
} }
self.ruby_thread self.ruby_thread
.as_ref() .as_ref()
.unwrap() .unwrap()
.execute(Box::new(move |_ruby| { .execute(Box::new(move |ruby| {
let name = CString::new(name).unwrap(); ruby.define_global_function(&name, function!(callback, -1));
unsafe { RubyValue::nil(&ruby)
rb_define_global_function(
name.as_ptr(),
std::mem::transmute(callback as *mut c_void),
-1,
);
}
RubyValue(())
})); }));
Ok(()) Ok(())
@ -267,15 +249,14 @@ impl Runtime for RubyRuntime {
_args: impl for<'a> crate::FuncArgs<'a, Self::Value, Self>, _args: impl for<'a> crate::FuncArgs<'a, Self::Value, Self>,
) -> Result<Self::Value, crate::ScriptingError> { ) -> Result<Self::Value, crate::ScriptingError> {
let name = name.to_string(); let name = name.to_string();
self.ruby_thread Ok(self
.ruby_thread
.as_ref() .as_ref()
.unwrap() .unwrap()
.execute(Box::new(move |ruby| { .execute(Box::new(move |ruby| {
let _: magnus::Value = ruby.class_object().funcall(name, ()).unwrap(); let _: magnus::Value = ruby.class_object().funcall(name, ()).unwrap();
RubyValue(()) RubyValue::nil(&ruby)
})); })))
Ok(RubyValue(()))
} }
fn call_fn_from_value( fn call_fn_from_value(
@ -296,15 +277,16 @@ pub mod prelude {
pub use super::RubyRuntime; pub use super::RubyRuntime;
} }
impl<T> FromRuntimeValueWithEngine<'_, RubyRuntime> for T { // TODO: remove this default
impl<T: Default> FromRuntimeValueWithEngine<'_, RubyRuntime> for T {
fn from_runtime_value_with_engine(_value: RubyValue, _engine: &magnus::Ruby) -> Self { fn from_runtime_value_with_engine(_value: RubyValue, _engine: &magnus::Ruby) -> Self {
todo!(); T::default()
} }
} }
impl<T> IntoRuntimeValueWithEngine<'_, T, RubyRuntime> for T { impl<T> IntoRuntimeValueWithEngine<'_, T, RubyRuntime> for T {
fn into_runtime_value_with_engine(_value: T, _engine: &magnus::Ruby) -> RubyValue { fn into_runtime_value_with_engine(_value: T, engine: &magnus::Ruby) -> RubyValue {
RubyValue(()) RubyValue::nil(engine)
} }
} }
@ -315,8 +297,8 @@ impl FuncArgs<'_, RubyValue, RubyRuntime> for () {
} }
impl<T> FuncArgs<'_, RubyValue, RubyRuntime> for Vec<T> { impl<T> FuncArgs<'_, RubyValue, RubyRuntime> for Vec<T> {
fn parse(self, _engine: &magnus::Ruby) -> Vec<RubyValue> { fn parse(self, engine: &magnus::Ruby) -> Vec<RubyValue> {
self.into_iter().map(|_x| RubyValue(())).collect() self.into_iter().map(|_x| RubyValue::nil(engine)).collect()
} }
} }