bp3d-os 2.2.3

Operating System tools designed for BlockProject3D
Documentation
// Copyright (c) 2025, BlockProject 3D
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice,
//       this list of conditions and the following disclaimer in the documentation
//       and/or other materials provided with the distribution.
//     * Neither the name of BlockProject 3D nor the names of its contributors
//       may be used to endorse or promote products derived from this software
//       without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::module::library::types::{OsLibrary, VirtualLibrary};
use crate::module::library::Library;
use crate::module::loader::ModuleLoader;
use crate::module::Module;
use crate::module::Result;
use std::path::Path;
use std::sync::MutexGuard;

/// Represents a handle to a [Module] stored in the application's [ModuleLoader].
pub trait ModuleHandle {
    /// The type of [Library] to return.
    type Library: Library;

    /// Returns a reference to the stored [Module] type.
    fn get(&self) -> &Module<Self::Library>;
}

struct VirtualLibraryHandle<'a> {
    loader: &'a ModuleLoader,
    id: usize,
}

impl<'a> VirtualLibraryHandle<'a> {
    fn new(loader: &'a ModuleLoader, id: usize) -> VirtualLibraryHandle<'a> {
        VirtualLibraryHandle { loader, id }
    }
}

impl<'a> ModuleHandle for VirtualLibraryHandle<'a> {
    type Library = VirtualLibrary;

    fn get(&self) -> &Module<VirtualLibrary> {
        self.loader.builtin_modules.get(&self.id).unwrap()
    }
}

struct OsLibraryHandle<'a> {
    loader: &'a ModuleLoader,
    id: usize,
}

impl<'a> OsLibraryHandle<'a> {
    fn new(loader: &'a ModuleLoader, id: usize) -> OsLibraryHandle<'a> {
        OsLibraryHandle { loader, id }
    }
}

impl<'a> ModuleHandle for OsLibraryHandle<'a> {
    type Library = OsLibrary;

    fn get(&self) -> &Module<OsLibrary> {
        self.loader.modules.get(&self.id).unwrap()
    }
}

/// A structure that represents a lock to the application's [ModuleLoader].
pub struct Lock<'a> {
    pub(super) lock: MutexGuard<'a, ModuleLoader>,
}

impl<'a> Lock<'a> {
    /// Attempts to load the given builtin module from its name.
    ///
    /// # Arguments
    ///
    /// * `name`: the name of the builtin module to load.
    ///
    /// returns: Result<&Module<VirtualLibrary>, Error>
    ///
    /// # Safety
    ///
    /// This function assumes the module to be loaded, if it exists has the correct format otherwise
    /// this function is UB.
    pub unsafe fn load_builtin(&mut self, name: &str) -> Result<impl ModuleHandle + '_> {
        self.lock
            ._load_builtin(name)
            .map(|id| VirtualLibraryHandle::new(&self.lock, id))
    }

    /// Attempts to load a module from the specified name which is dynamically linked in the current
    /// running software.
    ///
    /// # Arguments
    ///
    /// * `name`: the name of the module to be loaded.
    ///
    /// returns: Result<&Module, Error>
    ///
    /// # Safety
    ///
    /// This function assumes the module to be loaded, if it exists has the correct format otherwise
    /// this function is UB.
    pub unsafe fn load_self(&mut self, name: &str) -> Result<impl ModuleHandle + '_> {
        self.lock
            ._load_self(name)
            .map(|id| OsLibraryHandle::new(&self.lock, id))
    }

    /// Attempts to load a module from the specified name.
    ///
    /// This function already does check for the version of rustc and dependencies for Rust based
    /// modules to ensure maximum ABI compatibility.
    ///
    /// This function assumes the code to be loaded is trusted and delegates this operation to the
    /// underlying OS.
    ///
    /// # Arguments
    ///
    /// * `name`: the name of the module to be loaded.
    ///
    /// returns: ()
    ///
    /// # Safety
    ///
    /// It is assumed that the module is intended to be used with this instance of [ModuleLoader];
    /// if not, this function is UB. Additionally, if some dependency used in public facing APIs
    /// for the module are not added with [add_public_dependency](Self::add_public_dependency),
    /// this is also UB.
    pub unsafe fn load(&mut self, name: &str) -> Result<impl ModuleHandle + '_> {
        self.lock
            ._load(name)
            .map(|id| OsLibraryHandle::new(&self.lock, id))
    }

    /// Attempts to unload the given module.
    ///
    /// # Arguments
    ///
    /// * `name`: the name of the module to unload.
    ///
    /// returns: ()
    pub fn unload(&mut self, name: &str) -> Result<()> {
        self.lock._unload(name)
    }

    /// Adds the given path to the path search list.
    ///
    /// # Arguments
    ///
    /// * `path`: the path to include.
    ///
    /// returns: ()
    pub fn add_search_path(&mut self, path: impl AsRef<Path>) {
        self.lock._add_search_path(path);
    }

    /// Removes the given path to the path search list.
    ///
    /// # Arguments
    ///
    /// * `path`: the path to remove.
    ///
    /// returns: ()
    pub fn remove_search_path(&mut self, path: impl AsRef<Path>) {
        self.lock._remove_search_path(path);
    }

    /// Adds a public facing API dependency to the list of dependency for version checks.
    ///
    /// This is used to check if there are any ABI incompatibilities between dependency versions
    /// when loading a Rust based module.
    ///
    /// # Arguments
    ///
    /// * `name`: the name of the dependency.
    /// * `version`: the version of the dependency.
    ///
    /// returns: ()
    pub fn add_public_dependency<'b>(
        &mut self,
        name: &str,
        version: &str,
        features: impl IntoIterator<Item = &'b str>,
    ) {
        self.lock._add_public_dependency(name, version, features);
    }

    /// Returns the builtin module identified by the name `name`, returns [None] if the module is
    /// not loaded.
    pub fn get_builtin(&self, name: &str) -> Option<impl ModuleHandle + '_> {
        self.lock
            ._get_builtin(name)
            .map(|id| VirtualLibraryHandle::new(&self.lock, id))
    }

    /// Returns the module identified by the name `name`, returns [None] if the module is
    /// not loaded.
    pub fn get_module(&self, name: &str) -> Option<impl ModuleHandle + '_> {
        self.lock
            ._get_module(name)
            .map(|id| OsLibraryHandle::new(&self.lock, id))
    }
}