eval: Move free_tv to eval/typval.h, remove most of its usages

This commit is contained in:
ZyX
2016-08-28 09:15:28 +03:00
parent 233b0c93bb
commit 50ebd1dff5
5 changed files with 66 additions and 66 deletions

View File

@@ -182,19 +182,20 @@ Object nvim_eval(String expr, Error *err)
Object rv = OBJECT_INIT;
// Evaluate the expression
try_start();
typval_T *expr_result = eval_expr((char_u *)expr.data, NULL);
if (!expr_result) {
typval_T rettv;
if (eval0((char_u *)expr.data, &rettv, NULL, true) == FAIL) {
api_set_error(err, Exception, "Failed to evaluate expression");
}
if (!try_end(err)) {
// No errors, convert the result
rv = vim_to_object(expr_result);
rv = vim_to_object(&rettv);
}
// Free the vim object
free_tv(expr_result);
// Free the Vim object
tv_clear(&rettv);
return rv;
}

View File

@@ -719,13 +719,12 @@ int current_func_returned(void)
*/
void set_internal_string_var(char_u *name, char_u *value)
{
char_u *val = vim_strsave(value);
typval_T *tvp = xcalloc(1, sizeof(typval_T));
const typval_T tv = {
.v_type = VAR_STRING,
.vval.v_string = value,
};
tvp->v_type = VAR_STRING;
tvp->vval.v_string = val;
set_var((const char *)name, tvp, false);
free_tv(tvp);
set_var((const char *)name, (typval_T *)&tv, true);
}
static lval_T *redir_lval = NULL;
@@ -3264,7 +3263,7 @@ typedef enum {
* Note: "rettv.v_lock" is not set.
* Return OK or FAIL.
*/
static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate)
int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate)
{
int ret;
char_u *p;
@@ -18404,38 +18403,6 @@ void set_selfdict(typval_T *rettv, dict_T *selfdict)
}
}
/*
* Free the memory for a variable type-value.
*/
void free_tv(typval_T *varp)
{
if (varp != NULL) {
switch (varp->v_type) {
case VAR_FUNC:
func_unref(varp->vval.v_string);
// FALLTHROUGH
case VAR_STRING:
xfree(varp->vval.v_string);
break;
case VAR_PARTIAL:
partial_unref(varp->vval.v_partial);
break;
case VAR_LIST:
tv_list_unref(varp->vval.v_list);
break;
case VAR_DICT:
tv_dict_unref(varp->vval.v_dict);
break;
case VAR_SPECIAL:
case VAR_NUMBER:
case VAR_FLOAT:
case VAR_UNKNOWN:
break;
}
xfree(varp);
}
}
// TODO(ZyX-I): move to eval/typval
/// Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
@@ -21525,14 +21492,6 @@ int do_return(exarg_T *eap, int reanimate, int is_cmd, void *rettv)
return idx < 0;
}
/*
* Free the variable with a pending return value.
*/
void discard_pending_return(void *rettv)
{
free_tv((typval_T *)rettv);
}
/*
* Generate a return command for producing the value of "rettv". The result
* is an allocated string. Used by report_pending() for verbose messages.

View File

@@ -1742,6 +1742,46 @@ void tv_clear(typval_T *tv)
}
}
//{{{3 Free
/// Free allocated VimL object and value stored inside
///
/// @param tv Object to free.
void tv_free(typval_T *tv)
{
if (tv != NULL) {
switch (tv->v_type) {
case VAR_PARTIAL: {
partial_unref(tv->vval.v_partial);
break;
}
case VAR_FUNC: {
func_unref(tv->vval.v_string);
// FALLTHROUGH
}
case VAR_STRING: {
xfree(tv->vval.v_string);
break;
}
case VAR_LIST: {
tv_list_unref(tv->vval.v_list);
break;
}
case VAR_DICT: {
tv_dict_unref(tv->vval.v_dict);
break;
}
case VAR_SPECIAL:
case VAR_NUMBER:
case VAR_FLOAT:
case VAR_UNKNOWN: {
break;
}
}
xfree(tv);
}
}
//{{{2 Locks
/// Lock or unlock an item

View File

@@ -14,6 +14,7 @@
#include "nvim/ex_eval.h"
#include "nvim/charset.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/message.h"
@@ -21,8 +22,6 @@
#include "nvim/regexp.h"
#include "nvim/strings.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_eval.c.generated.h"
#endif
@@ -59,12 +58,14 @@
* is an error exception.) - The macros can be defined as expressions checking
* for a variable that is allowed to be changed during execution of a script.
*/
/* Values used for the Vim release. */
# define THROW_ON_ERROR TRUE
# define THROW_ON_ERROR_TRUE
# define THROW_ON_INTERRUPT TRUE
# define THROW_ON_INTERRUPT_TRUE
// Values used for the Vim release.
#define THROW_ON_ERROR true
#define THROW_ON_ERROR_TRUE
#define THROW_ON_INTERRUPT true
#define THROW_ON_INTERRUPT_TRUE
#define discard_pending_return(p) tv_free((typval_T *)(p))
/*
* When several errors appear in a row, setting "force_abort" is delayed until

View File

@@ -4372,7 +4372,6 @@ void ex_cbuffer(exarg_T *eap)
*/
void ex_cexpr(exarg_T *eap)
{
typval_T *tv;
qf_info_T *qi = &ql_info;
const char *au_name = NULL;
@@ -4412,11 +4411,11 @@ void ex_cexpr(exarg_T *eap)
/* Evaluate the expression. When the result is a string or a list we can
* use it to fill the errorlist. */
tv = eval_expr(eap->arg, NULL);
if (tv != NULL) {
if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
|| (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)) {
if (qf_init_ext(qi, NULL, NULL, tv, p_efm,
typval_T tv;
if (eval0(eap->arg, &tv, NULL, true) != FAIL) {
if ((tv.v_type == VAR_STRING && tv.vval.v_string != NULL)
|| (tv.v_type == VAR_LIST && tv.vval.v_list != NULL)) {
if (qf_init_ext(qi, NULL, NULL, &tv, p_efm,
(eap->cmdidx != CMD_caddexpr
&& eap->cmdidx != CMD_laddexpr),
(linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0) {
@@ -4431,7 +4430,7 @@ void ex_cexpr(exarg_T *eap)
} else {
EMSG(_("E777: String or List expected"));
}
free_tv(tv);
tv_clear(&tv);
}
}