mirror of
https://github.com/neovim/neovim.git
synced 2026-08-27 17:41:48 +00:00
fix(lua): give nicer error message when trying to index vim.NIL
The added test shows the context, one expected a JSON field
to be a Object but it was a null value
pros: shows `vim.NIL` instead of `a userdata`
cons: the context `field 'foo'` is lost. I think this is generated
with internal magic which is hard to replicate.
(cherry picked from commit 71b28da0f4)
This commit is contained in:
committed by
github-actions[bot]
parent
6da50f145a
commit
a4985ac902
@@ -626,6 +626,10 @@ static void nlua_common_vim_init(lua_State *lstate, bool is_thread, bool is_stan
|
||||
lua_createtable(lstate, 0, 0);
|
||||
lua_pushcfunction(lstate, &nlua_nil_tostring);
|
||||
lua_setfield(lstate, -2, "__tostring");
|
||||
lua_pushcfunction(lstate, &nlua_nil_index);
|
||||
lua_setfield(lstate, -2, "__index");
|
||||
lua_pushcfunction(lstate, &nlua_nil_index);
|
||||
lua_setfield(lstate, -2, "__newindex");
|
||||
lua_setmetatable(lstate, -2);
|
||||
ref_state->nil_ref = nlua_ref(lstate, ref_state, -1);
|
||||
lua_pushvalue(lstate, -1);
|
||||
@@ -1329,6 +1333,11 @@ static int nlua_nil_tostring(lua_State *lstate)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nlua_nil_index(lua_State *lstate)
|
||||
{
|
||||
return luaL_error(lstate, "attempt to index vim.NIL");
|
||||
}
|
||||
|
||||
static int nlua_empty_dict_tostring(lua_State *lstate)
|
||||
{
|
||||
lua_pushstring(lstate, "vim.empty_dict()");
|
||||
|
||||
@@ -178,6 +178,13 @@ describe('vim.json.decode()', function()
|
||||
pcall_err(exec_lua, [[return vim.json.decode('{"a":1/*x*/0}', { skip_comments = true })]])
|
||||
)
|
||||
end)
|
||||
|
||||
it('gives nice error message when attempting to index into null value', function()
|
||||
exec_lua [[ parsed = vim.json.decode('{"foo": null}') ]]
|
||||
|
||||
eq('attempt to index vim.NIL', pcall_err(exec_lua, [[ return parsed.foo.sub_field ]]))
|
||||
eq('attempt to index vim.NIL', pcall_err(exec_lua, [[ parsed.foo.new_field = 3 ]]))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('vim.json.encode()', function()
|
||||
|
||||
Reference in New Issue
Block a user