aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/executor.c
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2017-05-09 08:55:04 +0200
committerBjörn Linse <bjorn.linse@gmail.com>2017-05-13 15:03:42 +0200
commitf424189093ab23fb727a996d317ff19d4d3f0b63 (patch)
tree582a5a9a74a8a4f32c4fc0a9dea165efc3490089 /src/nvim/lua/executor.c
parent12fb634fe691a6ccfbd9bc7b44510c782af6c70a (diff)
downloadrneovim-f424189093ab23fb727a996d317ff19d4d3f0b63.tar.gz
rneovim-f424189093ab23fb727a996d317ff19d4d3f0b63.tar.bz2
rneovim-f424189093ab23fb727a996d317ff19d4d3f0b63.zip
api: execute lua directly from the remote api
Diffstat (limited to 'src/nvim/lua/executor.c')
-rw-r--r--src/nvim/lua/executor.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index 7cf326aef5..a7d5af36a1 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -373,6 +373,46 @@ static int nlua_eval_lua_string(lua_State *const lstate)
return 0;
}
+/// Evaluate lua string
+///
+/// Expects four values on the stack: string to evaluate, pointer to args array,
+/// and locations where result and error are saved, respectively. Always
+/// returns nothing (from the lua point of view).
+static int nlua_exec_lua_string_api(lua_State *const lstate)
+ FUNC_ATTR_NONNULL_ALL
+{
+ const String *str = (const String *)lua_touserdata(lstate, 1);
+ const Array *args = (const Array *)lua_touserdata(lstate, 2);
+ Object *retval = (Object *)lua_touserdata(lstate, 3);
+ Error *err = (Error *)lua_touserdata(lstate, 4);
+
+ lua_pop(lstate, 4);
+
+ if (luaL_loadbuffer(lstate, str->data, str->size, "<nvim>")) {
+ size_t len;
+ const char *str = lua_tolstring(lstate, -1, &len);
+ api_set_error(err, kErrorTypeValidation,
+ "Error loading lua: %.*s", (int)len, str);
+ return 0;
+ }
+
+ for (size_t i = 0; i < args->size; i++) {
+ nlua_push_Object(lstate, args->items[i]);
+ }
+
+ if (lua_pcall(lstate, (int)args->size, 1, 0)) {
+ size_t len;
+ const char *str = lua_tolstring(lstate, -1, &len);
+ api_set_error(err, kErrorTypeException,
+ "Error executing lua: %.*s", (int)len, str);
+ return 0;
+ }
+
+ *retval = nlua_pop_Object(lstate, err);
+
+ return 0;
+}
+
/// Print as a Vim message
///
/// @param lstate Lua interpreter state.
@@ -516,6 +556,28 @@ void executor_eval_lua(const String str, typval_T *const arg,
(void *)&str, arg, ret_tv);
}
+/// Execute lua string
+///
+/// Used for nvim_execute_lua().
+///
+/// @param[in] str String to execute.
+/// @param[in] args array of ... args
+/// @param[out] err Location where error will be saved.
+///
+/// @return Return value of the execution.
+Object executor_exec_lua_api(const String str, const Array args, Error *err)
+{
+ if (global_lstate == NULL) {
+ global_lstate = init_lua();
+ }
+
+ Object retval = NIL;
+ NLUA_CALL_C_FUNCTION_4(global_lstate, nlua_exec_lua_string_api, 0,
+ (void *)&str, (void *)&args, &retval, err);
+ return retval;
+}
+
+
/// Run lua string
///
/// Used for :lua.