From 4161e67f604ddd984464c9c346e5152e85347dd1 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 10 Jul 2022 21:19:10 -0700 Subject: [PATCH] lua: implicit tostring(userdata) when printing and ToDynamic I've been holding off from using UserData wrappers around things because it is awkward to match them up with the To/FromDynamic conversion. This commit aims to help with that by implicitly calling the tostring metamethod (if present) on a userdata both when printing (so the values look reasonable in the repl) and when mapping a lua value to a dynamic value. Provided that the end type is try_from=String this will allow the userdata value to be assigned as table value. --- luahelper/src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/luahelper/src/lib.rs b/luahelper/src/lib.rs index 2bc094698..d0a637c01 100644 --- a/luahelper/src/lib.rs +++ b/luahelper/src/lib.rs @@ -110,13 +110,45 @@ fn lua_value_to_dynamic_impl( LuaValue::Number(i) => DynValue::F64(i.into()), // Handle our special Null userdata case and map it to Null LuaValue::LightUserData(ud) if ud.0.is_null() => DynValue::Null, - LuaValue::LightUserData(_) | LuaValue::UserData(_) => { + LuaValue::LightUserData(_) => { return Err(mlua::Error::FromLuaConversionError { from: "userdata", to: "wezterm_dynamic::Value", message: None, }) } + LuaValue::UserData(ud) => match ud.get_metatable() { + Ok(mt) => { + match mt.get::(mlua::MetaMethod::ToString) { + Ok(to_string) => match to_string.call(LuaValue::UserData(ud.clone())) { + Ok(value) => { + return lua_value_to_dynamic_impl(value, visited); + } + Err(err) => { + return Err(mlua::Error::FromLuaConversionError { + from: "userdata", + to: "wezterm_dynamic::Value", + message: Some(format!("error calling tostring: {err:#}")), + }) + } + }, + Err(err) => { + return Err(mlua::Error::FromLuaConversionError { + from: "userdata", + to: "wezterm_dynamic::Value", + message: Some(format!("error getting tostring: {err:#}")), + }) + } + } + } + Err(err) => { + return Err(mlua::Error::FromLuaConversionError { + from: "userdata", + to: "wezterm_dynamic::Value", + message: Some(format!("error getting metatable: {err:#}")), + }) + } + }, LuaValue::Function(_) => { return Err(mlua::Error::FromLuaConversionError { from: "function", @@ -323,7 +355,29 @@ impl<'lua> std::fmt::Debug for ValuePrinterHelper<'lua> { fmt.debug_map().entries(&map).finish() } } - LuaValue::UserData(_) | LuaValue::LightUserData(_) => fmt.write_str("userdata"), + LuaValue::UserData(ud) => match ud.get_metatable() { + Ok(mt) => { + 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(err) => { + write!(fmt, "Error getting metatable: {err:#}") + } + }, + LuaValue::LightUserData(_) => fmt.write_str("userdata"), LuaValue::Thread(_) => fmt.write_str("thread"), LuaValue::Function(_) => fmt.write_str("function"), LuaValue::Error(e) => fmt.write_fmt(format_args!("error {}", e)),