fix(lua): show error message when failing to set variable

This commit is contained in:
zeertzjq 2023-09-23 15:27:40 +08:00
parent c0a29931e2
commit 2c5d373ede
3 changed files with 8 additions and 0 deletions

View File

@ -359,6 +359,9 @@ int nlua_setvar(lua_State *lstate)
Error err = ERROR_INIT;
dictitem_T *di = dict_check_writable(dict, key, del, &err);
if (ERROR_SET(&err)) {
nlua_push_errstr(lstate, "%s", err.msg);
api_clear_error(&err);
lua_error(lstate);
return 0;
}

View File

@ -1353,6 +1353,9 @@ describe('API', function()
-- Set readonly v: var.
eq('Key is read-only: count',
pcall_err(request, 'nvim_set_vvar', 'count', 42))
-- Set non-existent v: var.
eq('Dictionary is locked',
pcall_err(request, 'nvim_set_vvar', 'nosuchvar', 42))
-- Set writable v: var.
meths.set_vvar('errmsg', 'set by API')
eq('set by API', meths.get_vvar('errmsg'))

View File

@ -1491,6 +1491,8 @@ describe('lua stdlib', function()
eq(NIL, funcs.luaeval "vim.v.null")
matches([[attempt to index .* nil value]],
pcall_err(exec_lua, 'return vim.v[0].progpath'))
eq('Key is read-only: count', pcall_err(exec_lua, 'vim.v.count = 42'))
eq('Dictionary is locked', pcall_err(exec_lua, 'vim.v.nosuchvar = 42'))
end)
it('vim.bo', function()