use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};
use airsl::mlua::{Function, Lua, Table, Value};
use buildl_core::EvaluationFailure;
use crate::module::{guarded, plugin_chunk_name};
use crate::refusal::Refusal;
use crate::staging::Staging;
use crate::values;
#[derive(Debug)]
struct Loader {
dir: PathBuf,
label: String,
environment: Table,
staging: Arc<Mutex<Staging>>,
loaded: Mutex<BTreeMap<PathBuf, Value>>,
loading: Mutex<BTreeSet<PathBuf>>,
}
enum Found {
Loaded(Value),
Fresh {
file: PathBuf,
chunk: String,
text: String,
},
}
pub(super) fn function(
lua: &Lua,
staging: &Arc<Mutex<Staging>>,
environment: &Table,
dir: &Path,
label: &str,
entry: &Path,
) -> airsl::mlua::Result<Function> {
let loader = Loader {
dir: dir.to_path_buf(),
label: label.to_owned(),
environment: environment.clone(),
staging: Arc::clone(staging),
loaded: Mutex::new(BTreeMap::new()),
loading: Mutex::new(BTreeSet::from([entry.to_path_buf()])),
};
lua.create_function(move |lua, name: Value| loader.require(lua, &name))
}
impl Loader {
fn require(&self, lua: &Lua, name: &Value) -> airsl::mlua::Result<Value> {
let found = guarded(lua, &self.staging, "require", |_| self.find(name))?;
let (file, chunk, text) = match found {
Found::Loaded(value) => return Ok(value),
Found::Fresh { file, chunk, text } => (file, chunk, text),
};
self.loading
.lock()
.unwrap_or_else(PoisonError::into_inner)
.insert(file.clone());
let ran = lua
.load(text)
.set_name(plugin_chunk_name(&chunk))
.set_environment(self.environment.clone())
.eval::<Value>();
self.loading
.lock()
.unwrap_or_else(PoisonError::into_inner)
.remove(&file);
let value = match ran? {
Value::Nil => Value::Boolean(true),
value => value,
};
self.loaded
.lock()
.unwrap_or_else(PoisonError::into_inner)
.insert(file, value.clone());
Ok(value)
}
fn find(&self, name: &Value) -> Result<Found, Refusal> {
let name = values::string(name, "module")?;
let (file, parts) =
module_file(&self.dir, &name).map_err(|refusal| refusal.about(&name))?;
if let Some(value) = self
.loaded
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(&file)
{
return Ok(Found::Loaded(value.clone()));
}
if self
.loading
.lock()
.unwrap_or_else(PoisonError::into_inner)
.contains(&file)
{
return Err(Refusal::new(
EvaluationFailure::Refused,
"the module requires itself, directly or through the modules it requires",
)
.about(&name));
}
let text = fs::read_to_string(&file).map_err(|error| {
Refusal::new(
EvaluationFailure::Runtime,
format!("{}: {error}", file.display()),
)
.about(&name)
})?;
Ok(Found::Fresh {
file,
chunk: format!("{}/{}.lua", self.label, parts.join("/")),
text,
})
}
}
fn parts(name: &str) -> Result<Vec<&str>, Refusal> {
let parts: Vec<&str> = name.split('.').collect();
if parts
.iter()
.any(|part| part.is_empty() || part.contains(['/', '\\']))
{
return Err(Refusal::new(
EvaluationFailure::Refused,
"a module name is names joined by dots, none empty and none holding a slash",
));
}
Ok(parts)
}
fn module_file<'a>(dir: &Path, name: &'a str) -> Result<(PathBuf, Vec<&'a str>), Refusal> {
let parts = parts(name)?;
let mut path = dir.to_path_buf();
path.extend(&parts);
path.as_mut_os_string().push(".lua");
let file = fs::canonicalize(&path).map_err(|error| {
let failure = if error.kind() == ErrorKind::NotFound {
EvaluationFailure::Refused
} else {
EvaluationFailure::Runtime
};
Refusal::new(
failure,
format!("no module file {}: {error}", path.display()),
)
})?;
if !file.starts_with(dir) {
return Err(Refusal::new(
EvaluationFailure::Refused,
"the module resolves outside the plugin directory",
));
}
Ok((file, parts))
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::symlink;
use buildl_core::EvaluationFailure;
use super::{module_file, parts};
#[test]
fn a_dotted_name_splits_into_its_parts() {
assert_eq!(parts("a.b.c").unwrap(), ["a", "b", "c"]);
assert_eq!(parts("single").unwrap(), ["single"]);
}
#[test]
fn an_empty_part_a_slash_or_a_backslash_is_refused() {
for name in [
"", ".", "a.", ".a", "a..b", "..", "../a", "a/b", "a\\b", "/a",
] {
let refusal = parts(name).unwrap_err();
assert_eq!(refusal.failure(), &EvaluationFailure::Refused, "{name:?}");
}
}
#[test]
fn a_module_resolves_to_its_canonical_file() {
let dir = tempfile::tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
fs::create_dir_all(root.join("lib")).unwrap();
fs::write(root.join("lib/util.lua"), "").unwrap();
let (file, parts) = module_file(&root, "lib.util").unwrap();
assert_eq!(file, root.join("lib/util.lua"));
assert_eq!(parts, ["lib", "util"]);
}
#[test]
fn a_missing_module_is_refused() {
let dir = tempfile::tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
let refusal = module_file(&root, "nothing").unwrap_err();
assert_eq!(refusal.failure(), &EvaluationFailure::Refused);
}
#[cfg(unix)]
#[test]
fn a_link_to_a_file_outside_the_directory_is_refused() {
let dir = tempfile::tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
let plugin = root.join("plugin");
fs::create_dir_all(plugin.join("sub")).unwrap();
fs::write(root.join("outside.lua"), "").unwrap();
fs::create_dir_all(root.join("elsewhere")).unwrap();
fs::write(root.join("elsewhere/x.lua"), "").unwrap();
symlink(root.join("outside.lua"), plugin.join("file.lua")).unwrap();
symlink(root.join("elsewhere"), plugin.join("dir")).unwrap();
for name in ["file", "dir.x"] {
let refusal = module_file(&plugin, name).unwrap_err();
assert_eq!(refusal.failure(), &EvaluationFailure::Refused, "{name}");
assert!(
refusal
.diagnostic("require", None)
.as_str()
.contains("outside the plugin directory"),
"{name}"
);
}
}
#[cfg(unix)]
#[test]
fn a_link_that_stays_inside_the_directory_is_allowed() {
let dir = tempfile::tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
fs::write(root.join("real.lua"), "").unwrap();
symlink(root.join("real.lua"), root.join("alias.lua")).unwrap();
let (file, _) = module_file(&root, "alias").unwrap();
assert_eq!(file, root.join("real.lua"));
}
}