#[cfg(target_family = "wasm")]
use ::core::arch::wasm32::{memory_grow, memory_size};
#[doc = crate::_tags!(runtime namespace)]
#[derive(Debug)]
pub struct Wasm;
impl Wasm {
pub const PAGE_BYTES: usize = 65_536;
pub const MAX_PAGES: usize = 65_536;
pub const MAX_MEMORY: usize = Self::MAX_PAGES.saturating_mul(Self::PAGE_BYTES);
pub const fn is_wasm_target() -> bool {
cfg!(target_family = "wasm")
}
pub const fn has_atomics() -> bool {
cfg!(target_feature = "atomics")
}
pub fn has_bulk_memory() -> bool {
cfg!(target_feature = "bulk-memory")
}
pub fn has_mutable_globals() -> bool {
cfg!(target_feature = "mutable-globals")
}
pub fn has_simd() -> bool {
cfg!(target_feature = "simd128")
}
#[cfg(feature = "unsafe_layout")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_layout")))]
pub fn heap_base() -> usize {
#[cfg(target_family = "wasm")]
return &raw const __heap_base as usize;
#[cfg(not(target_family = "wasm"))]
0
}
#[allow(rustdoc::broken_intra_doc_links, reason = "cross-platform")]
pub fn memory_pages() -> usize {
#[cfg(target_family = "wasm")]
return memory_size(0);
#[cfg(not(target_family = "wasm"))]
0
}
pub fn memory_bytes() -> usize {
Self::memory_pages().saturating_mul(Self::PAGE_BYTES)
}
#[inline(always)]
#[allow(unused_variables, rustdoc::broken_intra_doc_links, reason = "cross-platform")]
pub fn memory_grow(delta: usize) -> usize {
#[cfg(target_family = "wasm")]
return memory_grow(0, delta);
#[cfg(not(target_family = "wasm"))]
usize::MAX
}
#[allow(unused_variables, rustdoc::broken_intra_doc_links, reason = "cross-platform")]
pub fn memory_grow_checked(delta: usize) -> Option<usize> {
#[cfg(target_family = "wasm")]
{
let result = memory_grow(0, delta);
if result == usize::MAX { None } else { Some(result) }
}
#[cfg(not(target_family = "wasm"))]
None
}
pub fn memory_can_grow() -> bool {
#[cfg(target_family = "wasm")]
return Self::memory_pages() < Self::MAX_PAGES;
#[cfg(not(target_family = "wasm"))]
false
}
#[inline(always)]
pub fn remaining_memory() -> usize {
Self::MAX_MEMORY.saturating_sub(Self::memory_bytes())
}
}
#[cfg(not(feature = "safe_sys"))]
#[cfg(feature = "unsafe_layout")]
unsafe extern "C" {
static __heap_base: u8;
}