Skip to content

Commit 9dc831c

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 545c046 commit 9dc831c

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
@@ -1218,24 +1218,24 @@ pub mod test_utils {
12181218
pub fn with_rubyvm<T>(mut func: impl FnMut() -> T) -> T {
12191219
RUBY_VM_INIT.call_once(boot_rubyvm);
12201220

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

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

0 commit comments

Comments
 (0)