artichoke_backend/state/
mod.rsuse core::hash::BuildHasherDefault;
use std::collections::hash_map::RandomState;
use intaglio::bytes::SymbolTable;
use mezzaluna_type_registry::Registry;
use rustc_hash::FxHasher;
use crate::class;
#[cfg(feature = "core-random")]
use crate::extn::core::random::Random;
use crate::interpreter::InterpreterAllocError;
use crate::load_path;
use crate::module;
use crate::sys;
pub mod output;
pub mod parser;
type SymbolTableHasher = BuildHasherDefault<FxHasher>;
#[derive(Debug)]
pub struct State {
pub parser: Option<parser::State>,
pub classes: Registry<class::Spec>,
pub modules: Registry<module::Spec>,
pub load_path_vfs: load_path::Adapter,
#[cfg(feature = "core-regexp")]
pub regexp: spinoso_regexp::State,
pub symbols: SymbolTable<SymbolTableHasher>,
pub output: output::Strategy,
pub hash_builder: RandomState,
#[cfg(feature = "core-random")]
pub prng: Random,
}
impl State {
#[cfg_attr(feature = "core-regexp", doc = "- `Regexp` [global state][regexp-state].")]
#[cfg_attr(feature = "core-random", doc = "- [Intepreter-level PRNG].")]
#[cfg_attr(feature = "core-regexp", doc = "[regexp-state]: spinoso_regexp::State")]
#[cfg_attr(feature = "core-random", doc = "[Intepreter-level PRNG]: Random")]
pub fn new() -> Result<Self, InterpreterAllocError> {
Ok(Self {
parser: None,
classes: Registry::new(),
modules: Registry::new(),
load_path_vfs: load_path::Adapter::new(),
#[cfg(feature = "core-regexp")]
regexp: spinoso_regexp::State::new(),
symbols: SymbolTable::with_hasher(SymbolTableHasher::default()),
output: output::Strategy::new(),
hash_builder: RandomState::new(),
#[cfg(feature = "core-random")]
prng: Random::new().map_err(|_| InterpreterAllocError::new())?,
})
}
#[doc(hidden)]
pub(crate) fn try_init_parser(&mut self, mrb: &mut sys::mrb_state) {
if let Some(parser) = parser::State::new(mrb) {
if let Some(old_parser) = self.parser.replace(parser) {
old_parser.close(mrb);
}
}
}
}