feat(eval): exists() function supports checking v:lua functions (#26485)

Problem:  Vimscript function exists() can't check v:lua functions.
Solution: Add support for v:lua functions to exists().
This commit is contained in:
Raphael
2023-12-12 19:06:22 +08:00
committed by GitHub
parent e69834744b
commit 1d4a5cd185
4 changed files with 49 additions and 0 deletions

View File

@@ -2313,3 +2313,21 @@ void nlua_init_defaults(void)
os_errmsg("\n");
}
}
/// check lua function exist
bool nlua_func_exists(const char *lua_funcname)
{
MAXSIZE_TEMP_ARRAY(args, 1);
size_t length = strlen(lua_funcname) + 8;
char *str = xmalloc(length);
vim_snprintf(str, length, "return %s", lua_funcname);
ADD_C(args, CSTR_AS_OBJ(str));
Error err = ERROR_INIT;
Object result = NLUA_EXEC_STATIC("return type(loadstring(...)()) =='function'", args, &err);
xfree(str);
if (result.type != kObjectTypeBoolean) {
return false;
}
return result.data.boolean;
}