From 16b322b707eebe105225b987f3ff91793f76e32d Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Wed, 14 May 2025 16:55:12 +0200 Subject: [PATCH] promises almost work --- assets/tests/ruby/return_via_promise.rb | 4 ++-- src/runtimes/ruby.rs | 29 ++++++++++++++++--------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/assets/tests/ruby/return_via_promise.rb b/assets/tests/ruby/return_via_promise.rb index c1ec94b..fcffa83 100644 --- a/assets/tests/ruby/return_via_promise.rb +++ b/assets/tests/ruby/return_via_promise.rb @@ -3,7 +3,7 @@ $state = { } def test_func - rust_func.and_then do |x| + rust_func.and_then lambda { |x| $state['x'] = x - end + } end diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index 8fcc2c3..1cb56dc 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -24,7 +24,7 @@ use bevy::{ }; use magnus::{ data_type_builder, function, method::ReturnValue, try_convert, value::Lazy, DataType, - DataTypeFunctions, IntoValue, RClass, Ruby, TryConvert, TypedData, + DataTypeFunctions, IntoValue, RClass, RObject, Ruby, TryConvert, TypedData, }; use magnus::{prelude::*, rb_sys::FromRawValue}; use rb_sys::{rb_define_global_function, rb_scan_args, VALUE}; @@ -127,11 +127,10 @@ unsafe impl TypedData for Promise<(), RubyValue> { } } -fn then(r_self: magnus::Value) -> magnus::Value { - dbg!(r_self); - panic!(); +fn then(r_self: magnus::Value, callback: magnus::Value) -> magnus::Value { + let promise: &Promise<(), RubyValue> = TryConvert::try_convert(r_self).unwrap(); let ruby = Ruby::get().unwrap(); - ruby.qnil().as_value() + promise.clone().then(RubyValue::new(callback)).into_value() } impl Default for RubyRuntime { @@ -149,7 +148,7 @@ impl Default for RubyRuntime { // TODO: maybe put promise in a module , maybe do so for other runtimes too let promise = ruby.define_class("Promise", ruby.class_object()).unwrap(); promise - .define_method("and_then", magnus::method!(then, 0)) + .define_method("and_then", magnus::method!(then, 1)) .unwrap(); })); // engine @@ -350,14 +349,24 @@ impl Runtime for RubyRuntime { })) } - // TODO: does this have a test? + // TODO: add test fn call_fn_from_value( &self, - _value: &Self::Value, + value: &Self::Value, _context: &Self::CallContext, - _args: Vec, + args: Vec, ) -> Result { - todo!() + let ruby = Ruby::get().unwrap(); + + let f: RObject = TryConvert::try_convert(ruby.get_inner(value.0)).unwrap(); + let result: magnus::Value = f.funcall("call", ()).unwrap(); + Ok(RubyValue::new(result)) + // self.ruby_thread + // .as_ref() + // .unwrap() + // .execute(Box::new(move |ruby| { + // todo!(); + // })) } fn is_current_thread() -> bool {