api: allow nvim_buf_attach from lua using callbacks

This commit is contained in:
Björn Linse
2019-05-29 10:05:00 +02:00
parent 4841c46e33
commit f5c56f03bb
20 changed files with 301 additions and 59 deletions

View File

@@ -363,6 +363,33 @@ static int nlua_getenv(lua_State *lstate)
}
#endif
/// add the value to the registry
LuaRef nlua_ref(lua_State *lstate, int index)
{
lua_pushvalue(lstate, index);
return luaL_ref(lstate, LUA_REGISTRYINDEX);
}
/// remove the value from the registry
void nlua_unref(lua_State *lstate, LuaRef ref)
{
if (ref > 0) {
luaL_unref(lstate, LUA_REGISTRYINDEX, ref);
}
}
void executor_free_luaref(LuaRef ref)
{
lua_State *const lstate = nlua_enter();
nlua_unref(lstate, ref);
}
/// push a value referenced in the regirstry
void nlua_pushref(lua_State *lstate, LuaRef ref)
{
lua_rawgeti(lstate, LUA_REGISTRYINDEX, ref);
}
/// Evaluate lua string
///
/// Used for luaeval().
@@ -451,9 +478,29 @@ Object executor_exec_lua_api(const String str, const Array args, Error *err)
return NIL;
}
return nlua_pop_Object(lstate, err);
return nlua_pop_Object(lstate, false, err);
}
Object executor_exec_lua_cb(LuaRef ref, const char *name, Array args)
{
lua_State *const lstate = nlua_enter();
nlua_pushref(lstate, ref);
lua_pushstring(lstate, name);
for (size_t i = 0; i < args.size; i++) {
nlua_push_Object(lstate, args.items[i]);
}
if (lua_pcall(lstate, (int)args.size+1, 1, 0)) {
// TODO(bfredl): callbacks:s might not always be msg-safe, for instance
// lua callbacks for redraw events. Later on let the caller deal with the
// error instead.
nlua_error(lstate, _("Error executing lua callback: %.*s"));
return NIL;
}
Error err = ERROR_INIT;
return nlua_pop_Object(lstate, false, &err);
}
/// Run lua string
///