use std::ptr::NonNull;
use libperl_sys::{CV, OP, PADLIST, SV, svtype};
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Cv(NonNull<CV>);
impl Cv {
#[inline]
pub unsafe fn from_raw_unchecked(p: *mut CV) -> Self {
debug_assert!(!p.is_null(), "Cv::from_raw_unchecked received a null pointer");
Cv(unsafe { NonNull::new_unchecked(p) })
}
#[inline]
pub fn from_raw(p: *mut CV) -> Option<Self> {
NonNull::new(p).map(Cv)
}
#[inline]
pub fn from_coderef(sv: *mut SV) -> Option<Cv> {
if sv.is_null() || unsafe { libperl_sys::SvROK(sv) } == 0 {
return None;
}
let target = unsafe { libperl_sys::SvRV(sv) };
if unsafe { libperl_sys::SvTYPE(target) } != svtype::SVt_PVCV {
return None;
}
Some(unsafe { Cv::from_raw_unchecked(target as *mut CV) })
}
#[inline]
pub fn as_ptr(&self) -> *mut CV {
self.0.as_ptr()
}
#[inline]
pub fn is_xsub(&self) -> bool {
unsafe { libperl_sys::CvISXSUB(self.as_ptr()) != 0 }
}
#[inline]
pub fn root(&self) -> *const OP {
if self.is_xsub() {
std::ptr::null()
} else {
unsafe { libperl_sys::CvROOT(self.as_ptr()) }
}
}
#[inline]
pub fn start(&self) -> *const OP {
if self.is_xsub() {
std::ptr::null()
} else {
unsafe { libperl_sys::CvSTART(self.as_ptr()) }
}
}
#[inline]
pub fn padlist(&self) -> *const PADLIST {
if self.is_xsub() {
std::ptr::null()
} else {
unsafe { libperl_sys::CvPADLIST(self.as_ptr()) }
}
}
pub fn file(&self) -> Option<String> {
let p = unsafe { libperl_sys::CvFILE(self.as_ptr()) };
if p.is_null() {
None
} else {
Some(
unsafe { std::ffi::CStr::from_ptr(p) }
.to_string_lossy()
.into_owned(),
)
}
}
pub fn proto(&self) -> Option<String> {
let sv = self.as_ptr() as *mut SV;
unsafe {
if libperl_sys::SvPOK(sv) == 0 {
return None;
}
let pv = libperl_sys::SvPVX_const(sv);
if pv.is_null() {
return None;
}
let len = libperl_sys::SvCUR(sv);
let bytes = std::slice::from_raw_parts(pv as *const u8, len as usize);
Some(String::from_utf8_lossy(bytes).into_owned())
}
}
}