add 'pseudo_block' helper

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-11-26 09:30:47 +01:00
parent 1182e7f5d7
commit ccde914a4c
2 changed files with 42 additions and 0 deletions

View File

@ -99,6 +99,11 @@ extern "C" {
pub fn RSPL_gv_stashsv(name: *const SV, flags: i32) -> *mut HV;
pub fn RSPL_sv_bless(sv: *mut SV, stash: *mut HV) -> *mut SV;
pub fn RSPL_ENTER();
pub fn RSPL_SAVETMPS();
pub fn RSPL_FREETMPS();
pub fn RSPL_LEAVE();
}
/// Argument marker for the stack.
@ -233,3 +238,24 @@ pub fn stack_push(value: crate::Mortal) {
pub unsafe fn croak(sv: *mut SV) -> ! {
RSPL_croak_sv(sv);
}
/// Create a pseudo-block for mortals & temps to be freed after it.
/// This calls `ENTER; SAVETMPS;` before and `FREETMPS; LEAVE;` after the provided closure.
pub fn pseudo_block<F, R>(func: F) -> R
where
F: FnOnce() -> R,
{
unsafe {
RSPL_ENTER();
RSPL_SAVETMPS();
}
let res = func();
unsafe {
RSPL_FREETMPS();
RSPL_LEAVE();
}
res
}

View File

@ -278,6 +278,22 @@ extern SV* RSPL_sv_bless(SV *sv, HV *stash) {
return sv_bless(sv, stash);
}
extern void RSPL_ENTER() {
ENTER;
}
extern void RSPL_SAVETMPS() {
SAVETMPS;
}
extern void RSPL_FREETMPS() {
FREETMPS;
}
extern void RSPL_LEAVE() {
LEAVE;
}
/*
These make are convoluted brainfarts:
SVt_NULL undef