From 25d21384ae8dd46ac3ef7c02c2acfbbbd3764fae Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 17 Dec 2021 09:17:09 +0100 Subject: [PATCH] perlmod: add Value::new_xsub as an unsafe fn Signed-off-by: Wolfgang Bumiller --- perlmod/src/value.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/perlmod/src/value.rs b/perlmod/src/value.rs index a9564d4..a733530 100644 --- a/perlmod/src/value.rs +++ b/perlmod/src/value.rs @@ -6,9 +6,9 @@ use std::fmt; use serde::{Deserialize, Serialize}; use crate::ffi::{self, SV}; -use crate::raw_value; use crate::scalar::ScalarRef; use crate::Error; +use crate::{perl_fn, raw_value}; use crate::{Array, Hash, Scalar}; /// A higher level value. This is basically an [`SV`] already cast to [`AV`](crate::ffi::AV) or @@ -52,6 +52,23 @@ impl Value { Value::Scalar(Scalar::new_bytes(s)) } + /// Create a new reference code reference. + /// + /// # Safety + /// + /// It is up to the user to ensure that only a valid perl XSUB is used. You should know what + /// you're doing, as perl code WILL execute the function and expect it to behave in a + /// particular way! + pub unsafe fn new_xsub(xsub: perl_fn!(extern "C" fn(*mut crate::ffi::CV))) -> Self { + Self::from_raw_move(crate::ffi::RSPL_newXS_flags( + std::ptr::null(), + xsub as _, + std::ptr::null(), + std::ptr::null(), + 0, + ) as _) + } + /// If the value is an array, returns the associated [`Array`]. pub fn as_array(&self) -> Option<&Array> { match self {