diff options
Diffstat (limited to 'src/nvim/lua/executor.c')
-rw-r--r-- | src/nvim/lua/executor.c | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 4e94c10283..fa8a67ff39 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -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 /// |