async
Some checks failed
Book / test (pull_request) Has been cancelled
Rust / build (pull_request) Has been cancelled

This commit is contained in:
Jaroslaw Konik 2025-05-30 18:45:48 +02:00
parent bd0c5e184e
commit d572faf95a
2 changed files with 35 additions and 12 deletions

View file

@ -1,13 +1,21 @@
a = get_player_name
b = a
puts '0'
puts a.await
puts '1'
u = get_player_name
puts b.await
puts '2'
z = get_player_name
puts z
puts z.await
puts "end"
def async_fun
async do
a = get_player_name
b = a
puts '0'
puts a.await
puts '1'
u = get_player_name
puts b.await
puts '2'
z = get_player_name
puts z
puts z.await
puts "end"
end
end
async_fun.await
puts "after await"
quit

View file

@ -213,6 +213,19 @@ impl TryConvert for BevyEntity {
#[magnus::wrap(class = "Bevy::Vec3")]
pub struct BevyVec3(pub Vec3);
pub fn async_function() {
let ruby = Ruby::get().unwrap();
let fiber = ruby
.fiber_new_from_fn(Default::default(), move |ruby, _args, _block| {
let p = ruby.block_proc().unwrap();
p.call::<_, value::Value>(()).unwrap();
Ok(())
})
.unwrap();
fiber.resume::<_, magnus::Value>(()).unwrap();
}
impl BevyVec3 {
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self(Vec3::new(x, y, z))
@ -291,6 +304,8 @@ impl Default for RubyRuntime {
vec3.define_method("x", method!(BevyVec3::x, 0))?;
vec3.define_method("y", method!(BevyVec3::y, 0))?;
vec3.define_method("z", method!(BevyVec3::z, 0))?;
ruby.define_global_function("async", function!(async_function, 0));
Ok::<(), ScriptingError>(())
}))
.expect("Failed to define builtin types");