luaref: simplify handling of table callables and fix leak in vim.fn.call(table)

I AM THE TABLE
This commit is contained in:
Björn Linse
2021-03-27 17:15:04 +01:00
parent 623fe4dc7e
commit 7e799502e5
6 changed files with 43 additions and 85 deletions

View File

@@ -810,6 +810,7 @@ static void f_call(typval_T *argvars, typval_T *rettv, FunPtr fptr)
return;
}
bool owned = false;
char_u *func;
partial_T *partial = NULL;
dict_T *selfdict = NULL;
@@ -820,6 +821,7 @@ static void f_call(typval_T *argvars, typval_T *rettv, FunPtr fptr)
func = partial_name(partial);
} else if (nlua_is_table_from_lua(&argvars[0])) {
func = nlua_register_table_as_callable(&argvars[0]);
owned = true;
} else {
func = (char_u *)tv_get_string(&argvars[0]);
}
@@ -837,6 +839,9 @@ static void f_call(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
func_call(func, &argvars[1], partial, selfdict, rettv);
if (owned) {
func_unref(func);
}
}
/*

View File

@@ -219,6 +219,7 @@ list_T *tv_list_alloc(const ptrdiff_t len)
list->lv_used_next = gc_first_list;
gc_first_list = list;
list_log(list, NULL, (void *)(uintptr_t)len, "alloc");
list->lua_table_ref = LUA_NOREF;
return list;
}
@@ -302,7 +303,7 @@ void tv_list_free_list(list_T *const l)
}
list_log(l, NULL, NULL, "freelist");
nlua_free_typval_list(l);
NLUA_CLEAR_REF(l->lua_table_ref);
xfree(l);
}
@@ -1404,6 +1405,8 @@ dict_T *tv_dict_alloc(void)
d->dv_copyID = 0;
QUEUE_INIT(&d->watchers);
d->lua_table_ref = LUA_NOREF;
return d;
}
@@ -1454,7 +1457,7 @@ void tv_dict_free_dict(dict_T *const d)
d->dv_used_next->dv_used_prev = d->dv_used_prev;
}
nlua_free_typval_dict(d);
NLUA_CLEAR_REF(d->lua_table_ref);
xfree(d);
}