use core::ptr::NonNull;
#[derive(Clone, Copy)]
#[repr(C)]
pub struct RawEnv {
pub(crate) context: Option<NonNull<()>>,
pub(crate) unit: Option<NonNull<()>>,
pub(crate) diagnostics: Option<NonNull<()>>,
}
impl RawEnv {
pub const fn null() -> RawEnv {
RawEnv {
context: None,
unit: None,
diagnostics: None,
}
}
}
#[macro_export]
macro_rules! static_env {
() => {
$crate::no_std::__static_env!();
};
}
#[cfg(feature = "std")]
#[macro_export]
#[doc(hidden)]
macro_rules! __static_env {
() => {};
}
#[cfg(not(feature = "std"))]
#[macro_export]
#[doc(hidden)]
macro_rules! __static_env {
() => {
const _: () = {
use $crate::no_std::RawEnv;
static mut BUDGET: usize = usize::MAX;
static mut MEMORY: usize = usize::MAX;
static mut RAW_ENV: RawEnv = RawEnv::null();
#[no_mangle]
extern "C" fn __rune_alloc_abort() -> ! {
::core::intrinsics::abort()
}
#[no_mangle]
extern "C" fn __rune_alloc_memory_take(amount: usize) -> bool {
unsafe {
if MEMORY == usize::MAX {
return true;
}
if MEMORY >= amount {
MEMORY -= amount;
return true;
}
return false;
}
}
#[no_mangle]
extern "C" fn __rune_alloc_memory_release(amount: usize) {
unsafe {
if MEMORY == usize::MAX {
return;
}
MEMORY = MEMORY.saturating_add(amount);
}
}
#[no_mangle]
extern "C" fn __rune_alloc_memory_get() -> usize {
unsafe { MEMORY }
}
#[no_mangle]
extern "C" fn __rune_alloc_memory_replace(value: usize) -> usize {
unsafe { core::ptr::replace(core::ptr::addr_of_mut!(MEMORY), value) }
}
#[no_mangle]
extern "C" fn __rune_budget_replace(value: usize) -> usize {
unsafe { core::ptr::replace(core::ptr::addr_of_mut!(BUDGET), value) }
}
#[no_mangle]
extern "C" fn __rune_budget_get() -> usize {
unsafe { BUDGET }
}
#[no_mangle]
extern "C" fn __rune_env_get() -> RawEnv {
unsafe { RAW_ENV }
}
#[no_mangle]
extern "C" fn __rune_env_replace(env: RawEnv) -> RawEnv {
unsafe { core::ptr::replace(core::ptr::addr_of_mut!(RAW_ENV), env) }
}
};
};
}
#[doc(hidden)]
pub use __static_env;
#[doc(inline)]
pub use static_env;