aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/executor.c
diff options
context:
space:
mode:
authorOliver Marriott <hello@omarriott.com>2022-02-19 22:56:50 +1100
committerOliver Marriott <hello@omarriott.com>2022-02-25 11:17:58 +1100
commit81bffbd147cd24580ac92fa9d9d85121151ca01f (patch)
tree5f66c85a0984a64131503845cd094c902c74edc7 /src/nvim/lua/executor.c
parentf7803c82d4f8619778fe21bb0c8dfd281ef9f9db (diff)
downloadrneovim-81bffbd147cd24580ac92fa9d9d85121151ca01f.tar.gz
rneovim-81bffbd147cd24580ac92fa9d9d85121151ca01f.tar.bz2
rneovim-81bffbd147cd24580ac92fa9d9d85121151ca01f.zip
feat: call __tostring on lua errors if possible before reporting to user
Diffstat (limited to 'src/nvim/lua/executor.c')
-rw-r--r--src/nvim/lua/executor.c17
1 files changed, 16 insertions, 1 deletions
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);