From 4924fd51378f53609171b56b0a9cf9b013b7d77f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 12 Jul 2023 14:15:08 -0700 Subject: [PATCH] repl: improve printing of various lua types We can now delegate to the new mlua helpers for a lot of this stuff. We do still take first dibs on printing userdata so that we can use our dynamic data interpretation when available, but otherwise, we can now show reasonable information about things like builtin file objects, and include the pointer address of things like functions to help disambiguate them when printing them. refs: #3849 refs: https://github.com/khvzak/mlua/issues/291 --- luahelper/src/lib.rs | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/luahelper/src/lib.rs b/luahelper/src/lib.rs index a77425c4d..ade2c6bf3 100644 --- a/luahelper/src/lib.rs +++ b/luahelper/src/lib.rs @@ -307,14 +307,6 @@ impl<'lua> ValuePrinterHelper<'lua> { impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { match &self.value { - LuaValue::Nil => fmt.write_str("nil"), - LuaValue::Boolean(b) => fmt.write_str(if *b { "true" } else { "false" }), - LuaValue::Integer(i) => fmt.write_fmt(format_args!("{}", i)), - LuaValue::Number(i) => fmt.write_fmt(format_args!("{}", i)), - LuaValue::String(s) => match s.to_str() { - Ok(s) => fmt.write_fmt(format_args!("{:?}", s)), - Err(_) => fmt.write_fmt(format_args!("{:?}", s.as_bytes())), - }, LuaValue::Table(t) => { self.visited .borrow_mut() @@ -379,8 +371,8 @@ impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> { fmt.debug_map().entries(&map).finish() } } - LuaValue::UserData(ud) => match ud.get_metatable() { - Ok(mt) => { + LuaValue::UserData(ud) => { + if let Ok(mt) = ud.get_metatable() { if let Ok(to_dynamic) = mt.get::("__wezterm_to_dynamic") { return match to_dynamic.call(LuaValue::UserData(ud.clone())) { Ok(value) => Self { @@ -391,28 +383,17 @@ impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> { Err(err) => write!(fmt, "Error calling __wezterm_to_dynamic: {err}"), }; } - match mt.get::(mlua::MetaMethod::ToString) { - Ok(to_string) => match to_string.call(LuaValue::UserData(ud.clone())) { - Ok(value) => Self { - visited: Rc::clone(&self.visited), - value, - } - .fmt(fmt), - Err(err) => { - write!(fmt, "Error calling tostring: {err:#}") - } - }, - Err(err) => { - write!(fmt, "Error getting tostring: {err:#}") - } - } } - Err(_) => fmt.write_str("userdata"), - }, - LuaValue::LightUserData(_) => fmt.write_str("userdata"), - LuaValue::Thread(_) => fmt.write_str("thread"), - LuaValue::Function(_) => fmt.write_str("function"), + match self.value.to_string() { + Ok(s) => fmt.write_str(&s), + Err(err) => write!(fmt, "userdata ({err:#})"), + } + } LuaValue::Error(e) => fmt.write_fmt(format_args!("error {}", e)), + _ => match self.value.to_string() { + Ok(s) => fmt.write_str(&s), + Err(err) => write!(fmt, "({err:#})"), + }, } } }