From 81bffbd147cd24580ac92fa9d9d85121151ca01f Mon Sep 17 00:00:00 2001 From: Oliver Marriott Date: Sat, 19 Feb 2022 22:56:50 +1100 Subject: feat: call __tostring on lua errors if possible before reporting to user --- src/nvim/lua/executor.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 029e7eb660..a61273084c 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -78,7 +78,22 @@ static void nlua_error(lua_State *const lstate, const char *const msg) FUNC_ATTR_NONNULL_ALL { size_t len; - const char *const str = lua_tolstring(lstate, -1, &len); + const char *str = NULL; + + if (luaL_getmetafield(lstate, -1, "__tostring")) { + if (lua_isfunction(lstate, -1) && luaL_callmeta(lstate, -2, "__tostring")) { + // call __tostring, convert the result and pop result. + str = lua_tolstring(lstate, -1, &len); + lua_pop(lstate, 1); + } + // pop __tostring. + lua_pop(lstate, 1); + } + + if (!str) { + // defer to lua default conversion, this will render tables as [NULL]. + str = lua_tolstring(lstate, -1, &len); + } msg_ext_set_kind("lua_error"); semsg_multiline(msg, (int)len, str); -- cgit