Skip to content

Commit 49389c4

Browse files
committed
Wrap Function::coverage callback to RefCell (Luau)
1 parent 13dc2b5 commit 49389c4

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/function.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl Function {
434434
/// [`Compiler::set_coverage_level`]: crate::chunk::Compiler::set_coverage_level
435435
#[cfg(any(feature = "luau", doc))]
436436
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
437-
pub fn coverage<F>(&self, mut func: F)
437+
pub fn coverage<F>(&self, func: F)
438438
where
439439
F: FnMut(CoverageInfo),
440440
{
@@ -454,13 +454,16 @@ impl Function {
454454
} else {
455455
None
456456
};
457-
let rust_callback = &mut *(data as *mut F);
458-
rust_callback(CoverageInfo {
459-
function,
460-
line_defined,
461-
depth,
462-
hits: slice::from_raw_parts(hits, size).to_vec(),
463-
});
457+
let rust_callback = &*(data as *const RefCell<F>);
458+
if let Ok(mut rust_callback) = rust_callback.try_borrow_mut() {
459+
// Call the Rust callback with CoverageInfo
460+
rust_callback(CoverageInfo {
461+
function,
462+
line_defined,
463+
depth,
464+
hits: slice::from_raw_parts(hits, size).to_vec(),
465+
});
466+
}
464467
}
465468

466469
let lua = self.0.lua.lock();
@@ -470,7 +473,8 @@ impl Function {
470473
assert_stack(state, 1);
471474

472475
lua.push_ref(&self.0);
473-
let func_ptr = &mut func as *mut F as *mut c_void;
476+
let func = RefCell::new(func);
477+
let func_ptr = &func as *const RefCell<F> as *mut c_void;
474478
ffi::lua_getcoverage(state, -1, func_ptr, callback::<F>);
475479
}
476480
}

0 commit comments

Comments
 (0)