fizzyx 0.1.0

Safe, ergonomic Rust bindings for the Fizzy WebAssembly interpreter.
Documentation
//! Engine and configuration.

use fizzyx_sys as sys;
use std::rc::Rc;

/// The default hard limit for linear memory growth, in 64 KiB pages.
///
/// This mirrors Fizzy's own default of 4096 pages (256 MiB).
pub const DEFAULT_MEMORY_PAGES_LIMIT: u32 = sys::FizzyMemoryPagesLimitDefault;

/// Configuration for an [`Engine`].
///
/// Fizzy is configured almost entirely at compile time, so the only runtime
/// knob is the hard limit on linear memory growth applied at instantiation.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Config {
    memory_pages_limit: u32,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            memory_pages_limit: DEFAULT_MEMORY_PAGES_LIMIT,
        }
    }
}

impl Config {
    /// Creates a new [`Config`] with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the hard limit for linear memory growth, in 64 KiB pages.
    ///
    /// The value is clamped to Fizzy's absolute maximum of 65536 pages (4 GiB).
    pub fn memory_pages_limit(&mut self, pages: u32) -> &mut Self {
        self.memory_pages_limit = pages.min(65536);
        self
    }

    /// Returns the configured hard limit for linear memory growth, in pages.
    pub fn get_memory_pages_limit(&self) -> u32 {
        self.memory_pages_limit
    }
}

/// A Fizzy engine.
///
/// The engine holds shared [`Config`] and is cheap to clone; clones refer to the
/// same underlying configuration.
#[derive(Debug, Clone)]
pub struct Engine {
    inner: Rc<Config>,
}

impl Default for Engine {
    fn default() -> Self {
        Self::new(&Config::default())
    }
}

impl Engine {
    /// Creates a new [`Engine`] with the given [`Config`].
    pub fn new(config: &Config) -> Self {
        Self {
            inner: Rc::new(*config),
        }
    }

    /// Returns the [`Config`] this engine was created with.
    pub fn config(&self) -> &Config {
        &self.inner
    }

    pub(crate) fn memory_pages_limit(&self) -> u32 {
        self.inner.memory_pages_limit
    }
}