miden_stdlib/
lib.rs

1#![no_std]
2
3extern crate alloc;
4
5use alloc::sync::Arc;
6
7use assembly::{
8    mast::MastForest,
9    utils::{sync::LazyLock, Deserializable},
10    Library,
11};
12
13// STANDARD LIBRARY
14// ================================================================================================
15
16/// TODO: add docs
17#[derive(Clone)]
18pub struct StdLibrary(Library);
19
20impl AsRef<Library> for StdLibrary {
21    fn as_ref(&self) -> &Library {
22        &self.0
23    }
24}
25
26impl From<StdLibrary> for Library {
27    fn from(value: StdLibrary) -> Self {
28        value.0
29    }
30}
31
32impl StdLibrary {
33    /// Serialized representation of the Miden standard library.
34    pub const SERIALIZED: &'static [u8] =
35        include_bytes!(concat!(env!("OUT_DIR"), "/assets/std.masl"));
36
37    /// Returns a reference to the [MastForest] underlying the Miden standard library.
38    pub fn mast_forest(&self) -> &Arc<MastForest> {
39        self.0.mast_forest()
40    }
41}
42
43impl Default for StdLibrary {
44    fn default() -> Self {
45        static STDLIB: LazyLock<StdLibrary> = LazyLock::new(|| {
46            let contents =
47                Library::read_from_bytes(StdLibrary::SERIALIZED).expect("failed to read std masl!");
48            StdLibrary(contents)
49        });
50        STDLIB.clone()
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use assembly::LibraryPath;
57
58    use super::*;
59
60    #[test]
61    fn test_compile() {
62        let path = "std::math::u64::overflowing_add".parse::<LibraryPath>().unwrap();
63        let stdlib = StdLibrary::default();
64        let exists = stdlib.0.module_infos().any(|module| {
65            module
66                .procedures()
67                .any(|(_, proc)| module.path().clone().append(&proc.name).unwrap() == path)
68        });
69
70        assert!(exists);
71    }
72}