use std::borrow::Cow;
use std::num::NonZeroUsize;
use airsl::mlua::Lua;
use crate::types::name::ToolName;
use crate::types::op::Origin;
use crate::types::report::Location;
use crate::types::tree::TreePath;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Caller {
file: TreePath,
line: NonZeroUsize,
}
impl Caller {
pub(super) fn location(&self, tool: &ToolName) -> Location {
Location::at_line(format!("{tool}/{}", self.file), self.line)
}
pub(super) fn origin(&self, tool: &ToolName) -> Origin {
Origin::Extension {
tool: tool.clone(),
file: self.file.clone(),
line: self.line,
}
}
}
pub(super) fn caller(lua: &Lua) -> Option<Caller> {
let mut level = 1;
loop {
let (source, line) = lua.inspect_stack(level, |frame| {
let source = frame.source().source.map(Cow::into_owned);
(source, frame.current_line())
})?;
let named = source
.as_deref()
.and_then(|source| source.strip_prefix('@'));
if let (Some(file), Some(line)) = (named, line.and_then(NonZeroUsize::new)) {
return Some(Caller {
file: TreePath::from_parts(&[file]),
line,
});
}
level += 1;
}
}
pub(super) fn locate(lua: &Lua, tool: &ToolName) -> Location {
caller(lua).map_or_else(
|| Location::file(tool.as_str()),
|caller| caller.location(tool),
)
}
#[cfg(test)]
mod tests {
use std::error::Error;
use airsl::Script;
use serde_json::{Value, json};
use super::*;
use crate::host::engine::build_engine;
use crate::host::events::Event;
use crate::host::testing::Probe;
type TestResult = Result<(), Box<dyn Error>>;
fn engine() -> Result<airsl::Engine, Box<dyn Error>> {
Ok(build_engine(Probe::new(ToolName::new("claude")?)?)?)
}
fn entry(engine: &airsl::Engine, source: &str) -> TestResult {
engine.eval(&Script::from_source(source, "adapter.lua")?)?;
Ok(())
}
fn string_of(value: Option<Value>) -> Result<String, Box<dyn Error>> {
match value {
Some(Value::String(text)) => Ok(text),
other => Err(format!("expected a string, got {other:?}").into()),
}
}
#[test]
fn names_the_entry_file_and_line_at_top_level() -> TestResult {
let engine = engine()?;
let shown: String = engine.eval_to(&Script::from_source(
"local a = 1\nlocal b = 2\nreturn airsstack.probe.where()",
"adapter.lua",
)?)?;
assert_eq!(shown, "claude/adapter.lua:3");
Ok(())
}
#[test]
fn names_the_line_inside_a_dispatched_handler() -> TestResult {
let engine = engine()?;
entry(
&engine,
"airsstack.ext.on('plan', function()\n return airsstack.probe.where()\nend)",
)?;
let shown = engine.dispatch(&Event::Plan.name()?, &json!({}))?;
assert_eq!(string_of(shown)?, "claude/adapter.lua:2");
Ok(())
}
#[test]
fn names_the_required_module_not_the_entry() -> TestResult {
let engine = engine()?;
let shown: String = engine.eval_to(&Script::from_source(
"local m = airsstack.probe.module('local M = {}\\nfunction M.go()\\n return airsstack.probe.where()\\nend\\nreturn M', 'lib/settings.lua')\nreturn m.go()",
"adapter.lua",
)?)?;
assert_eq!(shown, "claude/lib/settings.lua:3");
Ok(())
}
#[test]
fn skips_a_c_frame_to_the_pcall_line() -> TestResult {
let engine = engine()?;
let shown: String = engine.eval_to(&Script::from_source(
"local a = 1\nlocal b = 2\nlocal c = 3\nlocal _, where = pcall(airsstack.probe.where)\nreturn where",
"adapter.lua",
)?)?;
assert_eq!(shown, "claude/adapter.lua:4");
Ok(())
}
#[test]
fn gives_the_origin_of_the_call() -> TestResult {
let engine = engine()?;
let shown: String = engine.eval_to(&Script::from_source(
"\nreturn airsstack.probe.origin()",
"adapter.lua",
)?)?;
assert_eq!(shown, "claude/adapter.lua:2|adapter.lua:2");
Ok(())
}
}