add example

This commit is contained in:
Jaroslaw Konik 2025-05-24 22:47:12 +02:00
parent 20bf7121ff
commit e5eb7cc4f5
3 changed files with 25 additions and 22 deletions

View file

@ -1,6 +1,6 @@
fun_with_string_param("hello")
fun_with_i64_param(5)
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: for every runtime add example for wrapping with BevyEntity and BevyVec

View file

@ -1,7 +1,6 @@
use bevy::prelude::*;
use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::ruby::magnus;
use bevy_scriptum::runtimes::ruby::prelude::*;
use bevy_scriptum::runtimes::ruby::{prelude::*, RArray};
fn main() {
App::new()
@ -28,26 +27,19 @@ fn main() {
|In((x, y)): In<(i64, String)>| {
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)
.run();

View file

@ -5,6 +5,7 @@ use std::{
thread::{self, JoinHandle},
};
use ::magnus::value::Opaque;
use bevy::{
asset::Asset,
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 {
($($idx:tt $t:tt),+) => {
impl<'a, $($t: IntoValue,)+> FuncArgs<'a, RubyValue, RubyRuntime>