Ruby support #1

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

View file

@ -1,6 +1,6 @@
fun_with_string_param("hello") fun_with_string_param("hello")
fun_with_i64_param(5) fun_with_i64_param(5)
fun_with_multiple_params(5, "hello") fun_with_multiple_params(5, "hello")
# fun_with_i64_and_array_param(5, [1, 2, "third element"]) fun_with_i64_and_array_param(5, [1, 2, "third element"])
# TODO: add test for heteregenous array # TODO: add test for heteregenous array
# TODO: for every runtime add example for wrapping with BevyEntity and BevyVec # TODO: for every runtime add example for wrapping with BevyEntity and BevyVec

View file

@ -1,7 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_scriptum::prelude::*; use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::ruby::magnus; use bevy_scriptum::runtimes::ruby::{prelude::*, RArray};
use bevy_scriptum::runtimes::ruby::prelude::*;
fn main() { fn main() {
App::new() App::new()
@ -28,26 +27,19 @@ fn main() {
|In((x, y)): In<(i64, String)>| { |In((x, y)): In<(i64, String)>| {
println!("called with i64: {} and string: '{}'", x, y); println!("called with i64: {} and string: '{}'", x, y);
}, },
)
.add_function(
String::from("fun_with_i64_and_array_param"),
|In((x, y)): In<(i64, RArray)>, runtime: Res<RubyRuntime>| {
runtime.with_engine_thread(move |ruby| {
println!(
"called with i64: {} and dynamically typed array: [{:?}]",
x,
ruby.get_inner(y.0)
);
});
},
); );
// .add_function(
// String::from("fun_with_i64_and_array_param"),
// |In((x, y)): In<(i64, magnus::value::RArray)>, runtime: Res<RubyRuntime>| {
// runtime.with_engine(|engine| {
// println!(
// "called with i64: {} and dynamically typed array: [{:?}]",
// x,
// engine
// .registry_value::<mlua::Table>(&y)
// .unwrap()
// .pairs::<usize, mlua::Value>()
// .map(|pair| pair.unwrap())
// .map(|(_, v)| format!("{:?}", v))
// .collect::<Vec<String>>()
// .join(",")
// );
// });
// },
// );
}) })
.add_systems(Startup, startup) .add_systems(Startup, startup)
.run(); .run();

View file

@ -5,6 +5,7 @@ use std::{
thread::{self, JoinHandle}, thread::{self, JoinHandle},
}; };
use ::magnus::value::Opaque;
use bevy::{ use bevy::{
asset::Asset, asset::Asset,
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel}, ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
@ -513,6 +514,16 @@ impl<T: IntoValue> FuncArgs<'_, RubyValue, RubyRuntime> for Vec<T> {
} }
} }
pub struct RArray(pub Opaque<magnus::RArray>);
impl FromRuntimeValueWithEngine<'_, RubyRuntime> for RArray {
fn from_runtime_value_with_engine(value: RubyValue, engine: &magnus::Ruby) -> Self {
let inner = engine.get_inner(value.0);
let array = magnus::RArray::try_convert(inner).unwrap();
RArray(Opaque::from(array))
}
}
macro_rules! impl_tuple { macro_rules! impl_tuple {
($($idx:tt $t:tt),+) => { ($($idx:tt $t:tt),+) => {
impl<'a, $($t: IntoValue,)+> FuncArgs<'a, RubyValue, RubyRuntime> impl<'a, $($t: IntoValue,)+> FuncArgs<'a, RubyValue, RubyRuntime>