buildl-lua 0.3.1

Lua build-file evaluation for buildl, on the airsl embedded runtime.
Documentation
//! The curated airsl modules as the engine installs them.
//!
//! Its own file because it changes airsl modules rather than adding one. `airsstack.path` is
//! pure path arithmetic except `absolute`, which resolves against the process working directory:
//! a build file using it would declare different text depending on where `buildl` was run from,
//! and that difference cannot be seen by evaluating the file twice in one process. The
//! `airsstack` table does not exist yet while modules install, so the function is removed from
//! the module's own table, here, rather than from `airsstack.path` afterwards.
//!
//! The same moment is the only one at which a module's table is known to be untouched by the
//! build file, so [`Recorded`] copies each module's table into the registry as it installs, for
//! the plugins a build file uses to be given their own copies of.
//!
//! Responsibilities: [`CuratedPath`] and [`Recorded`].
//!
//! Non-responsibilities: everything else the modules do, which is airsl's, and what a plugin's
//! environment does with the copies, which `plugin` owns.

use airsl::description::{Function, LuaType, ModuleDescription};
use airsl::mlua::{self, Lua, Table, Value};
use airsl::modules::Path;
use airsl::{HostModule, InstallContext, ModuleName};

use crate::snapshot::copy_table;

/// The Lua registry key holding the table of curated modules [`Recorded`] fills, by module name.
pub(crate) const CURATED_REGISTRY_KEY: &str = "buildl.curated";

/// A curated module that records a copy of its table in the registry once it has installed, so the
/// copy holds the module as it was before any build file touched it.
#[derive(Debug)]
pub(crate) struct Recorded<M>(M);

impl<M: HostModule> Recorded<M> {
    /// `module`, recorded as it installs.
    pub(crate) const fn new(module: M) -> Self {
        Self(module)
    }
}

impl<M: HostModule> HostModule for Recorded<M> {
    fn name(&self) -> &ModuleName {
        self.0.name()
    }

    fn describe(&self) -> airsl::Result<ModuleDescription> {
        // Recording copies the installed table without adding a key, so the
        // description is the wrapped module's own.
        self.0.describe()
    }

    fn install(&self, lua: &Lua, table: &Table, context: &InstallContext<'_>) -> airsl::Result<()> {
        self.0.install(lua, table, context)?;
        let name = self.0.name().as_str();
        record(lua, name, table).map_err(|error| airsl::Error::lua(name, error))
    }
}

/// Adds a copy of `table` to the registry's table of curated modules, under `name`.
fn record(lua: &Lua, name: &str, table: &Table) -> mlua::Result<()> {
    let recorded =
        if let Value::Table(recorded) = lua.named_registry_value::<Value>(CURATED_REGISTRY_KEY)? {
            recorded
        } else {
            let recorded = lua.create_table()?;
            lua.set_named_registry_value(CURATED_REGISTRY_KEY, recorded.clone())?;
            recorded
        };
    recorded.set(name, copy_table(lua, table)?)
}

/// `airsstack.path`, minus `absolute`.
#[derive(Debug, Default)]
pub(crate) struct CuratedPath(Path);

impl HostModule for CuratedPath {
    fn name(&self) -> &ModuleName {
        self.0.name()
    }

    fn describe(&self) -> airsl::Result<ModuleDescription> {
        // `install` removes `absolute`, so the description is airsl's own
        // without it.
        ModuleDescription::builder("Lexical path manipulation that touches no file.")
            .function(
                Function::new("join", "Joins path parts with the path separator.")
                    .variadic(LuaType::STRING, "The parts to join.")
                    .returns(LuaType::STRING),
            )
            .function(
                Function::new("dirname", "Returns the directory part of a path.")
                    .param("path", LuaType::STRING, "The path to read.")
                    .returns(LuaType::STRING),
            )
            .function(
                Function::new("basename", "Returns the last component of a path.")
                    .param("path", LuaType::STRING, "The path to read.")
                    .returns(LuaType::STRING),
            )
            .function(
                Function::new(
                    "stem",
                    "Returns the last component of a path without its extension.",
                )
                .param("path", LuaType::STRING, "The path to read.")
                .returns(LuaType::STRING),
            )
            .function(
                Function::new(
                    "ext",
                    "Returns the extension of a path, or an empty string when it has none.",
                )
                .param("path", LuaType::STRING, "The path to read.")
                .returns(LuaType::STRING),
            )
            .function(
                Function::new(
                    "normalize",
                    "Collapses redundant separators and dot components in a path.",
                )
                .param("path", LuaType::STRING, "The path to read.")
                .returns(LuaType::STRING),
            )
            .function(
                Function::new("relative_to", "Expresses a path relative to a base path.")
                    .param("path", LuaType::STRING, "The path to express.")
                    .param("base", LuaType::STRING, "The path to measure it from.")
                    .returns(LuaType::STRING),
            )
            .function(
                Function::new(
                    "is_absolute",
                    "Reports whether a path is absolute on this platform.",
                )
                .param("path", LuaType::STRING, "The path to read.")
                .returns(LuaType::BOOLEAN),
            )
            .build()
    }

    fn install(&self, lua: &Lua, table: &Table, context: &InstallContext<'_>) -> airsl::Result<()> {
        self.0.install(lua, table, context)?;
        table
            .set("absolute", Value::Nil)
            .map_err(|error| airsl::Error::lua("path", error))
    }
}

#[cfg(test)]
mod tests {
    #![expect(
        clippy::unwrap_used,
        reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
    )]

    use airsl::mlua::{Lua, Table};
    use airsl::{Engine, HostModule, InstallContext, ModuleName, ModuleSet, Policy, Script};

    use super::{CURATED_REGISTRY_KEY, CuratedPath, Recorded};

    /// A module that, as it installs, reads back the curated modules recorded before it and
    /// leaves one of them in its own table.
    struct Reader(ModuleName);

    impl HostModule for Reader {
        fn name(&self) -> &ModuleName {
            &self.0
        }

        fn describe(&self) -> airsl::Result<airsl::description::ModuleDescription> {
            airsl::description::ModuleDescription::builder("Reads back a recorded module.")
                .field(
                    "path",
                    airsl::description::LuaType::TABLE,
                    "The recorded path module.",
                )
                .build()
        }

        fn install(
            &self,
            lua: &Lua,
            table: &Table,
            _context: &InstallContext<'_>,
        ) -> airsl::Result<()> {
            let recorded: Table = lua.named_registry_value(CURATED_REGISTRY_KEY).unwrap();
            table
                .set("path", recorded.get::<Table>("path").unwrap())
                .unwrap();
            Ok(())
        }
    }

    #[test]
    fn records_a_copy_of_the_module_table_as_it_installed() {
        // dyn: `ModuleSet::insert` takes `Box<dyn HostModule>`; the set is airsl's extension seam.
        let mut modules = ModuleSet::new();
        modules
            .insert(Box::new(Recorded::new(CuratedPath::default())))
            .unwrap();
        modules
            .insert(Box::new(Reader(ModuleName::new("reader").unwrap())))
            .unwrap();
        let engine = Engine::builder()
            .policy(Policy::pure())
            .stdlib(modules)
            .build()
            .unwrap();
        let result = engine
            .eval_to::<String>(
                &Script::from_source(
                    "local copy = airsstack.reader.path\n\
                     local same = copy == airsstack.path\n\
                     airsstack.path.stem = nil\n\
                     return tostring(same) .. ' ' .. type(copy.stem) .. ' ' .. type(copy.absolute)",
                    "build.lua",
                )
                .unwrap(),
            )
            .unwrap();
        assert_eq!(result, "false function nil");
    }

    #[test]
    fn the_recorded_descriptions_match_what_install_sets() {
        let root = airsl::RootTable::default();
        airsl::luals::verify(&CuratedPath::default(), &root).unwrap();
        airsl::luals::verify(&Recorded::new(CuratedPath::default()), &root).unwrap();
    }

    #[test]
    fn keeps_path_arithmetic_and_drops_absolute() {
        // dyn: `ModuleSet::insert` takes `Box<dyn HostModule>`; the set is airsl's extension seam.
        let mut modules = ModuleSet::new();
        modules.insert(Box::new(CuratedPath::default())).unwrap();
        let engine = Engine::builder()
            .policy(Policy::pure())
            .stdlib(modules)
            .build()
            .unwrap();
        let result = engine
            .eval_to::<String>(
                &Script::from_source(
                    "return type(airsstack.path.absolute) .. ' ' .. airsstack.path.stem('src/a.c')",
                    "build.lua",
                )
                .unwrap(),
            )
            .unwrap();
        assert_eq!(result, "nil a");
    }
}