use mlua::{Function, Lua, Value};
pub(crate) fn new_hmac<M: hmac::digest::KeyInit>(key: &[u8]) -> M {
#[allow(clippy::expect_used)]
<M as hmac::digest::KeyInit>::new_from_slice(key).expect("HMAC accepts any key length")
}
pub(crate) const MAX_JSON_DEPTH: usize = 128;
pub(crate) fn check_json_depth(value: &Value) -> mlua::Result<()> {
depth_walk(value, MAX_JSON_DEPTH)
}
fn depth_walk(value: &Value, remaining: usize) -> mlua::Result<()> {
let Value::Table(table) = value else {
return Ok(());
};
if remaining == 0 {
return Err(mlua::Error::RuntimeError(format!(
"json value nested deeper than {MAX_JSON_DEPTH} levels"
)));
}
table.for_each(|key: Value, item: Value| {
depth_walk(&key, remaining - 1)?;
depth_walk(&item, remaining - 1)
})
}
pub(crate) fn create_debug_fn(lua: &Lua) -> mlua::Result<Function> {
lua.create_function(|_, value: Value| {
tracing::debug!("[lua] {value:#?}");
Ok(())
})
}
pub fn error_lua_value(lua: &Lua, info: &nitr_core::ErrorInfo) -> mlua::Result<mlua::Table> {
let t = lua.create_table()?;
t.raw_set("message", info.message.as_str())?;
t.raw_set("kind", info.kind)?;
if let Some(source) = &info.source {
t.raw_set("source", source.as_str())?;
}
if let Some(line) = info.line {
t.raw_set("line", line)?;
}
if let Some(module) = &info.module {
t.raw_set("module", module.as_str())?;
}
if let Some(traceback) = &info.traceback {
t.raw_set("traceback", traceback.as_str())?;
}
if !info.cause.is_empty() {
let causes = lua.create_table_from(
info.cause
.iter()
.enumerate()
.map(|(i, c)| (i + 1, c.as_str())),
)?;
t.raw_set("cause", causes)?;
}
t.raw_set("__concise", info.concise())?;
let pretty = if console_wants_color() {
info.concise_colored()
} else {
info.concise()
};
t.raw_set("pretty", pretty)?;
t.set_metatable(Some(error_metatable(lua)?))?;
Ok(t)
}
fn console_wants_color() -> bool {
use std::io::IsTerminal as _;
std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none_or(|v| v.is_empty())
}
fn error_metatable(lua: &Lua) -> mlua::Result<mlua::Table> {
if let Ok(mt) = lua.named_registry_value::<mlua::Table>("nitr.error_mt") {
return Ok(mt);
}
let mt = lua.create_table()?;
mt.set(
"__tostring",
lua.create_function(|_, t: mlua::Table| t.raw_get::<String>("__concise"))?,
)?;
mt.set(
"__concat",
lua.create_function(|lua, (a, b): (Value, Value)| {
let tostring: Function = lua.globals().get("tostring")?;
let a: String = tostring.call(a)?;
let b: String = tostring.call(b)?;
Ok(a + &b)
})?,
)?;
lua.set_named_registry_value("nitr.error_mt", &mt)?;
Ok(mt)
}
pub(crate) fn create_errinfo_fn(lua: &Lua) -> mlua::Result<Function> {
lua.create_function(|lua, value: Value| {
let info = match &value {
Value::Error(err) => {
nitr_core::ErrorInfo::from_error(&nitr_core::Error::Lua((**err).clone()))
}
Value::String(s) => nitr_core::ErrorInfo::from_message(&s.to_string_lossy()),
Value::Table(t) if t.raw_get::<Option<String>>("__concise")?.is_some() => {
return Ok(t.clone());
}
other => {
let tostring: Function = lua.globals().get("tostring")?;
let text: String = tostring.call(other)?;
nitr_core::ErrorInfo::from_message(&text)
}
};
error_lua_value(lua, &info)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_values_stringify_and_concatenate_as_the_concise_form() {
let lua = Lua::new();
let info = nitr_core::ErrorInfo::from_message("app.lua:7: boom");
let err = error_lua_value(&lua, &info).expect("value");
lua.globals().set("err", err).expect("set");
let (text, concat, message, line): (String, String, String, u32) = lua
.load(r#"return tostring(err), "got: " .. err, err.message, err.line"#)
.eval()
.expect("eval");
assert!(text.contains("boom"), "tostring: {text}");
assert!(text.contains("app.lua:7"), "tostring: {text}");
assert_eq!(concat, format!("got: {text}"));
assert_eq!(message, "boom");
assert_eq!(line, 7);
}
#[test]
fn errinfo_classifies_and_is_idempotent() {
let lua = Lua::new();
lua.globals()
.set("errinfo", create_errinfo_fn(&lua).expect("fn"))
.expect("set");
let (message, kind, twice_same): (String, String, bool) = lua
.load(
r#"local ok, caught = pcall(function() error("nope", 0) end)
assert(not ok)
local info = errinfo(caught)
local again = errinfo(info)
return info.message, info.kind, rawequal(info, again)"#,
)
.eval()
.expect("eval");
assert_eq!(message, "nope");
assert_eq!(kind, "lua");
assert!(twice_same, "a structured value passes through unchanged");
}
fn deep_table(lua: &Lua, depth: usize) -> Value {
let root = lua.create_table().expect("table");
let mut cur = root.clone();
for _ in 1..depth {
let next = lua.create_table().expect("table");
cur.set("x", next.clone()).expect("set");
cur = next;
}
Value::Table(root)
}
#[test]
fn json_depth_boundary_is_exact() {
let lua = Lua::new();
assert!(check_json_depth(&deep_table(&lua, MAX_JSON_DEPTH)).is_ok());
let err = check_json_depth(&deep_table(&lua, MAX_JSON_DEPTH + 1)).expect_err("129 deep");
assert!(
err.to_string().contains("nested deeper than 128 levels"),
"got: {err}"
);
assert!(check_json_depth(&Value::Integer(7)).is_ok());
}
#[test]
fn a_cyclic_table_reports_depth_instead_of_hanging() {
let lua = Lua::new();
let t = lua.create_table().expect("table");
t.set("me", t.clone()).expect("set");
let err = check_json_depth(&Value::Table(t)).expect_err("cycle");
assert!(err.to_string().contains("nested deeper"), "got: {err}");
}
#[test]
fn deep_table_keys_are_bounded_too() {
let lua = Lua::new();
let outer = lua.create_table().expect("table");
outer
.set(deep_table(&lua, MAX_JSON_DEPTH), true)
.expect("set");
assert!(check_json_depth(&Value::Table(outer)).is_err());
}
}