diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2019-06-04 14:54:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-04 14:54:44 +0200 |
commit | 3adb8a10b157bc5c20ab1bdcbaa6f6aa9e36a5fc (patch) | |
tree | 471fa95580c6597a6bc1f922f9c401744e49de73 /src/nvim/lua/executor.c | |
parent | db415bde5f1e366fea09ad572cf37a84894d61d4 (diff) | |
parent | f5c56f03bb9ee25c3d931034497dc76a5591b770 (diff) | |
download | rneovim-3adb8a10b157bc5c20ab1bdcbaa6f6aa9e36a5fc.tar.gz rneovim-3adb8a10b157bc5c20ab1bdcbaa6f6aa9e36a5fc.tar.bz2 rneovim-3adb8a10b157bc5c20ab1bdcbaa6f6aa9e36a5fc.zip |
Merge pull request #9170 from bfredl/lua_cb
lua callbacks for nvim_buf_attach
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 /// |