Skip to content

Commit 0542b03

Browse files
committed
ZJIT: Prefer raw pointer over references in with_ruby_vm()
When references show up on in the type declaration, it's an invitation to think about how long the lifetime implicit lifetime is. This code doesn't do anything tricky lifetime-wise, so it looks better declaring only raw pointers.
1 parent 4f841ae commit 0542b03

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

zjit/src/cruby.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,24 +1217,24 @@ pub mod test_utils {
12171217
pub fn with_rubyvm<T>(mut func: impl FnMut() -> T) -> T {
12181218
RUBY_VM_INIT.call_once(boot_rubyvm);
12191219

1220-
// Set up a callback wrapper to store a return value
1221-
let mut result: Option<T> = None;
1222-
let mut data: &mut dyn FnMut() = &mut || {
1223-
// Store the result externally
1224-
result.replace(func());
1225-
};
1226-
12271220
// Invoke callback through rb_protect() so exceptions don't crash the process.
12281221
// "Fun" double pointer dance to get a thin function pointer to pass through C
12291222
unsafe extern "C" fn callback_wrapper(data: VALUE) -> VALUE {
12301223
// SAFETY: shorter lifetime than the data local in the caller frame
1231-
let callback: *mut &mut dyn FnMut() = std::ptr::with_exposed_provenance_mut(data.0);
1232-
unsafe { (*callback)() };
1224+
let callback: *const *mut dyn FnMut() = std::ptr::with_exposed_provenance_mut(data.0);
1225+
unsafe { (**callback)() };
12331226
Qnil
12341227
}
12351228

1229+
// Set up a callback wrapper to store a return value
1230+
let mut result: Option<T> = None;
1231+
let mut func_wrapper = || {
1232+
result.replace(func());
1233+
};
1234+
let data: *mut dyn FnMut() = &raw mut func_wrapper;
1235+
let data: *const *mut dyn FnMut() = &raw const data;
12361236
let mut state: c_int = 0;
1237-
unsafe { super::rb_protect(Some(callback_wrapper), VALUE((&raw mut data).expose_provenance()), &mut state) };
1237+
unsafe { super::rb_protect(Some(callback_wrapper), VALUE(data.expose_provenance()), &mut state) };
12381238
if state != 0 {
12391239
unsafe { rb_zjit_print_exception(); }
12401240
assert_eq!(0, state, "Exceptional unwind in callback. Ruby exception?");

0 commit comments

Comments
 (0)