diff options
author | bfredl <bjorn.linse@gmail.com> | 2022-03-18 00:57:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-18 00:57:48 +0100 |
commit | c1b98cfa5e94c28a792af67cf5bc53ef2c380681 (patch) | |
tree | 0f288ccdbf6c00344198382ba88df39a4a152a64 /src/nvim/lua/executor.c | |
parent | cac90d2de728181edce7ba38fb9ad588d231651b (diff) | |
parent | 81bffbd147cd24580ac92fa9d9d85121151ca01f (diff) | |
download | rneovim-c1b98cfa5e94c28a792af67cf5bc53ef2c380681.tar.gz rneovim-c1b98cfa5e94c28a792af67cf5bc53ef2c380681.tar.bz2 rneovim-c1b98cfa5e94c28a792af67cf5bc53ef2c380681.zip |
Merge pull request #17459 from rktjmp/lua-error-tostring
feat: __tostring lua errors if possible before showing in messages
Diffstat (limited to 'src/nvim/lua/executor.c')
-rw-r--r-- | src/nvim/lua/executor.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 054f6c9483..21625854fb 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -92,7 +92,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); |