//! Per-call host borrow slots for script plugins. //! //! Host functions outlive any single callback, while each call's borrow (`Read`, `Host`, `Setup`, `View`, `Changed`) //! lives only for that call. An [`Enter`] / [`EnterChanged`] guard installs the erased pointer and restores the //! previous value on drop (including unwind). #![allow(unsafe_code)] use std::cell::Cell; use std::marker::PhantomData; use crate::abi::{self, v2}; #[derive(Clone, Copy)] pub(crate) enum Current { Idle, Read(*const (dyn abi::Read + 'static)), Host(*const (dyn abi::Host + 'static)), Setup(*mut abi::Setup<'static>), View(*mut v2::View<'static>), } impl Current { pub(crate) fn reading<'a>(read: &'a (dyn abi::Read + 'a)) -> Self { let read: *const (dyn abi::Read + 'a) = read; // SAFETY: only the lifetime is erased; `Enter` keeps the pointer for no longer than the borrow. Self::Read(unsafe { std::mem::transmute::<*const (dyn abi::Read + 'a), *const (dyn abi::Read + 'static)>(read) }) } pub(crate) fn acting<'a>(host: &'a (dyn abi::Host + 'a)) -> Self { let host: *const (dyn abi::Host + 'a) = host; // SAFETY: as for `reading`. Self::Host(unsafe { std::mem::transmute::<*const (dyn abi::Host + 'a), *const (dyn abi::Host + 'static)>(host) }) } pub(crate) fn setting_up(setup: &mut abi::Setup<'_>) -> Self { Self::Setup((setup as *mut abi::Setup<'_>).cast()) } pub(crate) fn viewing(view: &mut v2::View<'_>) -> Self { Self::View((view as *mut v2::View<'_>).cast()) } pub(crate) fn read<'a>(self) -> Result<&'a dyn abi::Read, &'static str> { // SAFETY: pointer is the running call's borrow, live while its `Enter` guard is active on this thread. unsafe { match self { Self::Idle => Err("the host is reachable only while the plugin's call runs"), Self::Read(read) => Ok(&*read), Self::Host(host) => Ok(&*host as &dyn abi::Read), Self::Setup(setup) => Ok((*setup).host() as &dyn abi::Read), Self::View(view) => Ok((*view).read), } } } pub(crate) fn host<'a>(self) -> Result<&'a dyn abi::Host, &'static str> { // SAFETY: as for `read`. unsafe { match self { Self::Host(host) => Ok(&*host), Self::Setup(setup) => Ok((*setup).host()), Self::Idle => Err("the host is reachable only while the plugin's call runs"), Self::Read(_) | Self::View(_) => Err("a render or a view may read, and ask for nothing"), } } } pub(crate) fn setup<'a>(self) -> Option<&'a mut abi::Setup<'static>> { match self { // SAFETY: as for `read`; setup methods run one at a time on this thread and none retains the reference. Self::Setup(setup) => Some(unsafe { &mut *setup }), _ => None, } } pub(crate) fn view<'a>(self) -> Option<&'a mut v2::View<'static>> { match self { // SAFETY: as for `setup`; memo builds enter their inner view before calling back in. Self::View(view) => Some(unsafe { &mut *view }), _ => None, } } } pub(crate) struct Slot(Cell); impl Slot { pub(crate) const fn new() -> Self { Self(Cell::new(Current::Idle)) } pub(crate) fn get(&self) -> Current { self.0.get() } } pub(crate) struct Enter<'a> { slot: &'a Slot, previous: Current, } impl<'a> Enter<'a> { pub(crate) fn new(slot: &'a Slot, current: Current) -> Self { Self { slot, previous: slot.0.replace(current) } } } impl Drop for Enter<'_> { fn drop(&mut self) { self.slot.0.set(self.previous); } } pub(crate) struct ChangedSlot(Cell<*const abi::Changed>); impl ChangedSlot { pub(crate) const fn new() -> Self { Self(Cell::new(std::ptr::null())) } pub(crate) fn get<'a>(&self) -> Option<&'a abi::Changed> { let ptr = self.0.get(); // SAFETY: non-null only while `EnterChanged` holds a live borrow on this thread. (!ptr.is_null()).then(|| unsafe { &*ptr }) } } pub(crate) struct EnterChanged<'a> { slot: &'a ChangedSlot, previous: *const abi::Changed, _borrow: PhantomData<&'a abi::Changed>, } impl<'a> EnterChanged<'a> { pub(crate) fn new(slot: &'a ChangedSlot, changed: &'a abi::Changed) -> Self { Self { slot, previous: slot.0.replace(std::ptr::from_ref(changed)), _borrow: PhantomData } } } impl Drop for EnterChanged<'_> { fn drop(&mut self) { self.slot.0.set(self.previous); } }