lua: cleanup naming conventions of executor functions

This commit is contained in:
Björn Linse
2020-09-03 11:11:53 +02:00
parent 5581ac88c6
commit 5f3ddb195e
11 changed files with 64 additions and 46 deletions

View File

@@ -212,10 +212,10 @@ Boolean nvim_buf_attach(uint64_t channel_id,
error:
// TODO(bfredl): ASAN build should check that the ref table is empty?
executor_free_luaref(cb.on_lines);
executor_free_luaref(cb.on_bytes);
executor_free_luaref(cb.on_changedtick);
executor_free_luaref(cb.on_detach);
api_free_luaref(cb.on_lines);
api_free_luaref(cb.on_bytes);
api_free_luaref(cb.on_changedtick);
api_free_luaref(cb.on_detach);
return false;
}
@@ -247,10 +247,10 @@ Boolean nvim_buf_detach(uint64_t channel_id,
static void buf_clear_luahl(buf_T *buf, bool force)
{
if (buf->b_luahl || force) {
executor_free_luaref(buf->b_luahl_start);
executor_free_luaref(buf->b_luahl_window);
executor_free_luaref(buf->b_luahl_line);
executor_free_luaref(buf->b_luahl_end);
api_free_luaref(buf->b_luahl_start);
api_free_luaref(buf->b_luahl_window);
api_free_luaref(buf->b_luahl_line);
api_free_luaref(buf->b_luahl_end);
}
buf->b_luahl_start = LUA_NOREF;
buf->b_luahl_window = LUA_NOREF;

View File

@@ -1198,7 +1198,7 @@ void api_free_object(Object value)
break;
case kObjectTypeLuaRef:
executor_free_luaref(value.data.luaref);
api_free_luaref(value.data.luaref);
break;
default:

View File

@@ -475,7 +475,7 @@ Object nvim_execute_lua(String code, Array args, Error *err)
FUNC_API_DEPRECATED_SINCE(7)
FUNC_API_REMOTE_ONLY
{
return executor_exec_lua_api(code, args, err);
return nlua_exec(code, args, err);
}
/// Execute Lua code. Parameters (if any) are available as `...` inside the
@@ -494,7 +494,7 @@ Object nvim_exec_lua(String code, Array args, Error *err)
FUNC_API_SINCE(7)
FUNC_API_REMOTE_ONLY
{
return executor_exec_lua_api(code, args, err);
return nlua_exec(code, args, err);
}
/// Calls a VimL function.
@@ -2477,7 +2477,7 @@ Array nvim_get_proc_children(Integer pid, Error *err)
Array a = ARRAY_DICT_INIT;
ADD(a, INTEGER_OBJ(pid));
String s = cstr_to_string("return vim._os_proc_children(select(1, ...))");
Object o = nvim_exec_lua(s, a, err);
Object o = nlua_exec(s, a, err);
api_free_string(s);
api_free_array(a);
if (o.type == kObjectTypeArray) {
@@ -2523,7 +2523,7 @@ Object nvim_get_proc(Integer pid, Error *err)
Array a = ARRAY_DICT_INIT;
ADD(a, INTEGER_OBJ(pid));
String s = cstr_to_string("return vim._os_proc_info(select(1, ...))");
Object o = nvim_exec_lua(s, a, err);
Object o = nlua_exec(s, a, err);
api_free_string(s);
api_free_array(a);
if (o.type == kObjectTypeArray && o.data.array.size == 0) {

View File

@@ -158,7 +158,7 @@ void buf_updates_unregister_all(buf_T *buf)
args.items[0] = BUFFER_OBJ(buf->handle);
textlock++;
executor_exec_lua_cb(cb.on_detach, "detach", args, false, NULL);
nlua_call_ref(cb.on_detach, "detach", args, false, NULL);
textlock--;
}
free_update_callbacks(cb);
@@ -266,7 +266,7 @@ void buf_updates_send_changes(buf_T *buf,
args.items[7] = INTEGER_OBJ((Integer)deleted_codeunits);
}
textlock++;
Object res = executor_exec_lua_cb(cb.on_lines, "lines", args, true, NULL);
Object res = nlua_call_ref(cb.on_lines, "lines", args, true, NULL);
textlock--;
if (res.type == kObjectTypeBoolean && res.data.boolean == true) {
@@ -317,7 +317,7 @@ void buf_updates_send_splice(
args.items[10] = INTEGER_OBJ(new_byte);
textlock++;
Object res = executor_exec_lua_cb(cb.on_bytes, "bytes", args, true, NULL);
Object res = nlua_call_ref(cb.on_bytes, "bytes", args, true, NULL);
textlock--;
if (res.type == kObjectTypeBoolean && res.data.boolean == true) {
@@ -352,8 +352,8 @@ void buf_updates_changedtick(buf_T *buf)
args.items[1] = INTEGER_OBJ(buf_get_changedtick(buf));
textlock++;
Object res = executor_exec_lua_cb(cb.on_changedtick, "changedtick",
args, true, NULL);
Object res = nlua_call_ref(cb.on_changedtick, "changedtick",
args, true, NULL);
textlock--;
if (res.type == kObjectTypeBoolean && res.data.boolean == true) {
@@ -387,6 +387,6 @@ void buf_updates_changedtick_single(buf_T *buf, uint64_t channel_id)
static void free_update_callbacks(BufUpdateCallbacks cb)
{
executor_free_luaref(cb.on_lines);
executor_free_luaref(cb.on_changedtick);
api_free_luaref(cb.on_lines);
api_free_luaref(cb.on_changedtick);
}

View File

@@ -5482,7 +5482,7 @@ static void f_luaeval(typval_T *argvars, typval_T *rettv, FunPtr fptr)
return;
}
executor_eval_lua(cstr_as_string((char *)str), &argvars[1], rettv);
nlua_typval_eval(cstr_as_string((char *)str), &argvars[1], rettv);
}
/*

View File

@@ -1395,8 +1395,7 @@ call_func(
if (is_luafunc(partial)) {
if (len > 0) {
error = ERROR_NONE;
executor_call_lua((const char *)funcname, len,
argvars, argcount, rettv);
nlua_typval_call((const char *)funcname, len, argvars, argcount, rettv);
}
} else if (!builtin_function((const char *)rfname, -1)) {
// User defined function.

View File

@@ -1249,6 +1249,13 @@ type_error:
return ret;
}
LuaRef nlua_pop_LuaRef(lua_State *const lstate, Error *err)
{
LuaRef rv = nlua_ref(lstate, -1);
lua_pop(lstate, 1);
return rv;
}
#define GENERATE_INDEX_FUNCTION(type) \
type nlua_pop_##type(lua_State *lstate, Error *err) \
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT \

View File

@@ -845,7 +845,7 @@ void nlua_unref(lua_State *lstate, LuaRef ref)
}
}
void executor_free_luaref(LuaRef ref)
void api_free_luaref(LuaRef ref)
{
lua_State *const lstate = nlua_enter();
nlua_unref(lstate, ref);
@@ -879,8 +879,8 @@ LuaRef nlua_newref(lua_State *lstate, LuaRef original_ref)
/// @param[out] ret_tv Location where result will be saved.
///
/// @return Result of the execution.
void executor_eval_lua(const String str, typval_T *const arg,
typval_T *const ret_tv)
void nlua_typval_eval(const String str, typval_T *const arg,
typval_T *const ret_tv)
FUNC_ATTR_NONNULL_ALL
{
#define EVALHEADER "local _A=select(1,...) return ("
@@ -902,8 +902,8 @@ void executor_eval_lua(const String str, typval_T *const arg,
}
}
void executor_call_lua(const char *str, size_t len, typval_T *const args,
int argcount, typval_T *ret_tv)
void nlua_typval_call(const char *str, size_t len, typval_T *const args,
int argcount, typval_T *ret_tv)
FUNC_ATTR_NONNULL_ALL
{
#define CALLHEADER "return "
@@ -1006,14 +1006,14 @@ int typval_exec_lua_callable(
/// Execute Lua string
///
/// Used for nvim_exec_lua().
/// Used for nvim_exec_lua() and internally to execute a lua string.
///
/// @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)
Object nlua_exec(const String str, const Array args, Error *err)
{
lua_State *const lstate = nlua_enter();
@@ -1040,17 +1040,30 @@ Object executor_exec_lua_api(const String str, const Array args, Error *err)
return nlua_pop_Object(lstate, false, err);
}
Object executor_exec_lua_cb(LuaRef ref, const char *name, Array args,
bool retval, Error *err)
/// call a LuaRef as a function (or table with __call metamethod)
///
/// @param ref the reference to call (not consumed)
/// @param name if non-NULL, sent to callback as first arg
/// if NULL, only args are used
/// @param retval if true, convert return value to Object
/// if false, discard return value
/// @param err Error details, if any (if NULL, errors are echoed)
/// @return Return value of function, if retval was set. Otherwise NIL.
Object nlua_call_ref(LuaRef ref, const char *name, Array args,
bool retval, Error *err)
{
lua_State *const lstate = nlua_enter();
nlua_pushref(lstate, ref);
lua_pushstring(lstate, name);
int nargs = (int)args.size;
if (name != NULL) {
lua_pushstring(lstate, name);
nargs++;
}
for (size_t i = 0; i < args.size; i++) {
nlua_push_Object(lstate, args.items[i], false);
}
if (lua_pcall(lstate, (int)args.size+1, retval ? 1 : 0, 0)) {
if (lua_pcall(lstate, nargs, retval ? 1 : 0, 0)) {
// if err is passed, the caller will deal with the error.
if (err) {
size_t len;

View File

@@ -513,7 +513,7 @@ int update_screen(int type)
FIXED_TEMP_ARRAY(args, 2);
args.items[0] = BUFFER_OBJ(buf->handle);
args.items[1] = INTEGER_OBJ(display_tick);
executor_exec_lua_cb(buf->b_luahl_start, "start", args, false, &err);
nlua_call_ref(buf->b_luahl_start, "start", args, false, &err);
if (ERROR_SET(&err)) {
ELOG("error in luahl start: %s", err.msg);
api_clear_error(&err);
@@ -1251,7 +1251,7 @@ static void win_update(win_T *wp)
args.items[3] = INTEGER_OBJ(knownmax);
// TODO(bfredl): we could allow this callback to change mod_top, mod_bot.
// For now the "start" callback is expected to use nvim__buf_redraw_range.
executor_exec_lua_cb(buf->b_luahl_window, "window", args, false, &err);
nlua_call_ref(buf->b_luahl_window, "window", args, false, &err);
if (ERROR_SET(&err)) {
ELOG("error in luahl window: %s", err.msg);
api_clear_error(&err);
@@ -2356,8 +2356,7 @@ win_line (
args.items[2] = INTEGER_OBJ(lnum-1);
lua_attr_active = true;
extra_check = true;
Object o = executor_exec_lua_cb(buf->b_luahl_line, "line",
args, true, &err);
Object o = nlua_call_ref(buf->b_luahl_line, "line", args, true, &err);
lua_attr_active = false;
if (o.type == kObjectTypeString) {
// TODO(bfredl): this is a bit of a hack. A final API should use an

View File

@@ -16,7 +16,7 @@ typedef uint32_t u8char_T;
// Opaque handle used by API clients to refer to various objects in vim
typedef int handle_T;
// Opaque handle to a lua value. Must be free with `executor_free_luaref` when
// Opaque handle to a lua value. Must be free with `api_free_luaref` when
// not needed anymore! LUA_NOREF represents missing reference, i e to indicate
// absent callback etc.
typedef int LuaRef;

View File

@@ -2119,13 +2119,13 @@ void list_in_columns(char_u **items, int size, int current)
void list_lua_version(void)
{
typval_T luaver_tv;
typval_T arg = { .v_type = VAR_UNKNOWN }; // No args.
char *luaver_expr = "((jit and jit.version) and jit.version or _VERSION)";
executor_eval_lua(cstr_as_string(luaver_expr), &arg, &luaver_tv);
assert(luaver_tv.v_type == VAR_STRING);
MSG(luaver_tv.vval.v_string);
xfree(luaver_tv.vval.v_string);
char *code = "return ((jit and jit.version) and jit.version or _VERSION)";
Error err = ERROR_INIT;
Object ret = nlua_exec(cstr_as_string(code), (Array)ARRAY_DICT_INIT, &err);
assert(!ERROR_SET(&err));
assert(ret.type == kObjectTypeString);
MSG(ret.data.string.data);
api_free_object(ret);
}
void list_version(void)