From 97f022b5e75f124f08fa44794f65fe1b112818eb Mon Sep 17 00:00:00 2001 From: Max Bernstein Date: Thu, 13 Mar 2025 12:16:58 -0400 Subject: Print Ruby exception in test utils --- yjit/bindgen/src/main.rs | 1 + zjit.c | 9 +++++++++ zjit/src/cruby.rs | 6 ++++-- zjit/src/cruby_bindings.inc.rs | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/yjit/bindgen/src/main.rs b/yjit/bindgen/src/main.rs index df675304e8..b9f3651e0e 100644 --- a/yjit/bindgen/src/main.rs +++ b/yjit/bindgen/src/main.rs @@ -359,6 +359,7 @@ fn main() { .allowlist_function("rb_yjit_invokeblock_sp_pops") .allowlist_function("rb_yjit_set_exception_return") .allowlist_function("rb_yjit_str_concat_codepoint") + .allowlist_function("rb_zjit_print_exception") .allowlist_type("robject_offsets") .allowlist_type("rstring_offsets") diff --git a/zjit.c b/zjit.c index af11ac2949..21c257cd32 100644 --- a/zjit.c +++ b/zjit.c @@ -681,5 +681,14 @@ rb_iseq_set_zjit_payload(const rb_iseq_t *iseq, void *payload) // Primitives used by zjit.rb VALUE rb_zjit_assert_compiles(rb_execution_context_t *ec, VALUE self); +void +rb_zjit_print_exception(void) +{ + VALUE exception = rb_errinfo(); + rb_set_errinfo(Qnil); + assert(RTEST(exception)); + rb_warn("Ruby error: %"PRIsVALUE"", rb_funcall(exception, rb_intern("full_message"), 0)); +} + // Preprocessed zjit.rb generated during build #include "zjit.rbinc" diff --git a/zjit/src/cruby.rs b/zjit/src/cruby.rs index dff8d301d6..c0093dd284 100644 --- a/zjit/src/cruby.rs +++ b/zjit/src/cruby.rs @@ -939,8 +939,10 @@ pub mod test_utils { let mut state: c_int = 0; unsafe { super::rb_protect(Some(callback_wrapper), VALUE((&mut data) as *mut _ as usize), &mut state) }; - // TODO(alan): there should be a way to print the exception instead of swallowing it - assert_eq!(0, state, "Exceptional unwind in callback. Ruby exception?"); + if state != 0 { + unsafe { rb_zjit_print_exception(); } + assert_eq!(0, state, "Exceptional unwind in callback. Ruby exception?"); + } result.expect("Callback did not set result") } diff --git a/zjit/src/cruby_bindings.inc.rs b/zjit/src/cruby_bindings.inc.rs index 6c5a7c82bf..01da0bff47 100644 --- a/zjit/src/cruby_bindings.inc.rs +++ b/zjit/src/cruby_bindings.inc.rs @@ -1023,4 +1023,5 @@ unsafe extern "C" { pub fn rb_RCLASS_ORIGIN(c: VALUE) -> VALUE; pub fn rb_iseq_get_zjit_payload(iseq: *const rb_iseq_t) -> *mut ::std::os::raw::c_void; pub fn rb_iseq_set_zjit_payload(iseq: *const rb_iseq_t, payload: *mut ::std::os::raw::c_void); + pub fn rb_zjit_print_exception(); } -- cgit v1.2.3