From e5eb7cc4f56175067eb7b7c54247eb96e8218a0e Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Sat, 24 May 2025 22:47:12 +0200 Subject: [PATCH] add example --- assets/examples/ruby/function_params.rb | 2 +- examples/ruby/function_params.rs | 34 ++++++++++--------------- src/runtimes/ruby.rs | 11 ++++++++ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/assets/examples/ruby/function_params.rb b/assets/examples/ruby/function_params.rb index 7e87c8f..ea73e66 100644 --- a/assets/examples/ruby/function_params.rb +++ b/assets/examples/ruby/function_params.rb @@ -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 diff --git a/examples/ruby/function_params.rs b/examples/ruby/function_params.rs index 0999a5a..fab2d46 100644 --- a/examples/ruby/function_params.rs +++ b/examples/ruby/function_params.rs @@ -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| { + 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| { - // runtime.with_engine(|engine| { - // println!( - // "called with i64: {} and dynamically typed array: [{:?}]", - // x, - // engine - // .registry_value::(&y) - // .unwrap() - // .pairs::() - // .map(|pair| pair.unwrap()) - // .map(|(_, v)| format!("{:?}", v)) - // .collect::>() - // .join(",") - // ); - // }); - // }, - // ); }) .add_systems(Startup, startup) .run(); diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index 9abab09..7c4e7a8 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -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 FuncArgs<'_, RubyValue, RubyRuntime> for Vec { } } +pub struct RArray(pub Opaque); + +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>