0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 8.0.0593: duplication of code for adding a list or dict return value

Problem:    Duplication of code for adding a list or dict return value.
Solution:   Add rettv_dict_set() and rettv_list_set(). (Yegappan Lakshmanan)
This commit is contained in:
Bram Moolenaar
2017-04-30 20:25:19 +02:00
parent 29ae377ea7
commit 45cf6e910c
8 changed files with 48 additions and 60 deletions

View File

@@ -59,13 +59,23 @@ rettv_dict_alloc(typval_T *rettv)
if (d == NULL) if (d == NULL)
return FAIL; return FAIL;
rettv->vval.v_dict = d; rettv_dict_set(rettv, d);
rettv->v_type = VAR_DICT;
rettv->v_lock = 0; rettv->v_lock = 0;
++d->dv_refcount;
return OK; return OK;
} }
/*
* Set a dictionary as the return value
*/
void
rettv_dict_set(typval_T *rettv, dict_T *d)
{
rettv->v_type = VAR_DICT;
rettv->vval.v_dict = d;
if (d != NULL)
++d->dv_refcount;
}
/* /*
* Free a Dictionary, including all non-container items it contains. * Free a Dictionary, including all non-container items it contains.
* Ignores the reference count. * Ignores the reference count.
@@ -646,11 +656,7 @@ failret:
*arg = skipwhite(*arg + 1); *arg = skipwhite(*arg + 1);
if (evaluate) if (evaluate)
{ rettv_dict_set(rettv, d);
rettv->v_type = VAR_DICT;
rettv->vval.v_dict = d;
++d->dv_refcount;
}
return OK; return OK;
} }

View File

@@ -4665,9 +4665,7 @@ eval_index(
item = item->li_next; item = item->li_next;
} }
clear_tv(rettv); clear_tv(rettv);
rettv->v_type = VAR_LIST; rettv_list_set(rettv, l);
rettv->vval.v_list = l;
++l->lv_refcount;
} }
else else
{ {
@@ -8486,9 +8484,7 @@ getwinvar(
if (opts != NULL) if (opts != NULL)
{ {
rettv->v_type = VAR_DICT; rettv_dict_set(rettv, opts);
rettv->vval.v_dict = opts;
++opts->dv_refcount;
done = TRUE; done = TRUE;
} }
} }

View File

@@ -3005,8 +3005,7 @@ f_expand(typval_T *argvars, typval_T *rettv)
&& get_tv_number_chk(&argvars[2], &error) && get_tv_number_chk(&argvars[2], &error)
&& !error) && !error)
{ {
rettv->v_type = VAR_LIST; rettv_list_set(rettv, NULL);
rettv->vval.v_list = NULL;
} }
s = get_tv_string(&argvars[0]); s = get_tv_string(&argvars[0]);
@@ -3909,12 +3908,7 @@ f_get(typval_T *argvars, typval_T *rettv)
} }
} }
else if (STRCMP(what, "dict") == 0) else if (STRCMP(what, "dict") == 0)
{ rettv_dict_set(rettv, pt->pt_dict);
rettv->v_type = VAR_DICT;
rettv->vval.v_dict = pt->pt_dict;
if (pt->pt_dict != NULL)
++pt->pt_dict->dv_refcount;
}
else if (STRCMP(what, "args") == 0) else if (STRCMP(what, "args") == 0)
{ {
rettv->v_type = VAR_LIST; rettv->v_type = VAR_LIST;
@@ -4214,9 +4208,7 @@ f_getbufvar(typval_T *argvars, typval_T *rettv)
if (opts != NULL) if (opts != NULL)
{ {
rettv->v_type = VAR_DICT; rettv_dict_set(rettv, opts);
rettv->vval.v_dict = opts;
++opts->dv_refcount;
done = TRUE; done = TRUE;
} }
} }
@@ -5372,8 +5364,7 @@ f_glob(typval_T *argvars, typval_T *rettv)
{ {
if (get_tv_number_chk(&argvars[2], &error)) if (get_tv_number_chk(&argvars[2], &error))
{ {
rettv->v_type = VAR_LIST; rettv_list_set(rettv, NULL);
rettv->vval.v_list = NULL;
} }
if (argvars[3].v_type != VAR_UNKNOWN if (argvars[3].v_type != VAR_UNKNOWN
&& get_tv_number_chk(&argvars[3], &error)) && get_tv_number_chk(&argvars[3], &error))
@@ -5429,8 +5420,7 @@ f_globpath(typval_T *argvars, typval_T *rettv)
{ {
if (get_tv_number_chk(&argvars[3], &error)) if (get_tv_number_chk(&argvars[3], &error))
{ {
rettv->v_type = VAR_LIST; rettv_list_set(rettv, NULL);
rettv->vval.v_list = NULL;
} }
if (argvars[4].v_type != VAR_UNKNOWN if (argvars[4].v_type != VAR_UNKNOWN
&& get_tv_number_chk(&argvars[4], &error)) && get_tv_number_chk(&argvars[4], &error))
@@ -9152,9 +9142,7 @@ f_reverse(typval_T *argvars, typval_T *rettv)
list_append(l, li); list_append(l, li);
li = ni; li = ni;
} }
rettv->vval.v_list = l; rettv_list_set(rettv, l);
rettv->v_type = VAR_LIST;
++l->lv_refcount;
l->lv_idx = l->lv_len - l->lv_idx - 1; l->lv_idx = l->lv_len - l->lv_idx - 1;
} }
} }
@@ -10742,9 +10730,7 @@ do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
(char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")), (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
TRUE)) TRUE))
goto theend; goto theend;
rettv->vval.v_list = l; rettv_list_set(rettv, l);
rettv->v_type = VAR_LIST;
++l->lv_refcount;
len = list_len(l); len = list_len(l);
if (len <= 1) if (len <= 1)
@@ -11832,8 +11818,7 @@ f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
char_u str[NUMBUFLEN]; char_u str[NUMBUFLEN];
#endif #endif
rettv->v_type = VAR_LIST; rettv_list_set(rettv, NULL);
rettv->vval.v_list = NULL;
#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL) #if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
lnum = get_tv_lnum(argvars); /* -1 on type error */ lnum = get_tv_lnum(argvars); /* -1 on type error */
@@ -11890,8 +11875,7 @@ f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
int id; int id;
#endif #endif
rettv->v_type = VAR_LIST; rettv_list_set(rettv, NULL);
rettv->vval.v_list = NULL;
#ifdef FEAT_SYN_HL #ifdef FEAT_SYN_HL
lnum = get_tv_lnum(argvars); /* -1 on type error */ lnum = get_tv_lnum(argvars); /* -1 on type error */
@@ -12057,9 +12041,7 @@ get_cmd_output_as_rettv(
list_append(list, li); list_append(list, li);
} }
++list->lv_refcount; rettv_list_set(rettv, list);
rettv->v_type = VAR_LIST;
rettv->vval.v_list = list;
list = NULL; list = NULL;
} }
else else
@@ -12465,8 +12447,7 @@ f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
static void static void
f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv) f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
{ {
rettv->v_type = VAR_DICT; rettv_dict_set(rettv, NULL);
rettv->vval.v_dict = NULL;
} }
#ifdef FEAT_JOB_CHANNEL #ifdef FEAT_JOB_CHANNEL
@@ -12481,8 +12462,7 @@ f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
static void static void
f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv) f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
{ {
rettv->v_type = VAR_LIST; rettv_list_set(rettv, NULL);
rettv->vval.v_list = NULL;
} }
static void static void

View File

@@ -1136,9 +1136,7 @@ perl_to_vim(SV *sv, typval_T *rettv)
} }
} }
list->lv_refcount++; rettv_list_set(rettv, list);
rettv->v_type = VAR_LIST;
rettv->vval.v_list = list;
break; break;
} }
case SVt_PVHV: /* dictionary */ case SVt_PVHV: /* dictionary */
@@ -1192,9 +1190,7 @@ perl_to_vim(SV *sv, typval_T *rettv)
} }
} }
dict->dv_refcount++; rettv_dict_set(rettv, dict);
rettv->v_type = VAR_DICT;
rettv->vval.v_dict = dict;
break; break;
} }
default: /* not convertible */ default: /* not convertible */

View File

@@ -97,13 +97,23 @@ rettv_list_alloc(typval_T *rettv)
if (l == NULL) if (l == NULL)
return FAIL; return FAIL;
rettv->vval.v_list = l;
rettv->v_type = VAR_LIST;
rettv->v_lock = 0; rettv->v_lock = 0;
++l->lv_refcount; rettv_list_set(rettv, l);
return OK; return OK;
} }
/*
* Set a list as the return value
*/
void
rettv_list_set(typval_T *rettv, list_T *l)
{
rettv->v_type = VAR_LIST;
rettv->vval.v_list = l;
if (l != NULL)
++l->lv_refcount;
}
/* /*
* Unreference a list: decrement the reference count and free it when it * Unreference a list: decrement the reference count and free it when it
* becomes zero. * becomes zero.
@@ -875,11 +885,7 @@ failret:
*arg = skipwhite(*arg + 1); *arg = skipwhite(*arg + 1);
if (evaluate) if (evaluate)
{ rettv_list_set(rettv, l);
rettv->v_type = VAR_LIST;
rettv->vval.v_list = l;
++l->lv_refcount;
}
return OK; return OK;
} }

View File

@@ -1,6 +1,7 @@
/* dict.c */ /* dict.c */
dict_T *dict_alloc(void); dict_T *dict_alloc(void);
int rettv_dict_alloc(typval_T *rettv); int rettv_dict_alloc(typval_T *rettv);
void rettv_dict_set(typval_T *rettv, dict_T *d);
void dict_unref(dict_T *d); void dict_unref(dict_T *d);
int dict_free_nonref(int copyID); int dict_free_nonref(int copyID);
void dict_free_items(int copyID); void dict_free_items(int copyID);

View File

@@ -4,6 +4,7 @@ void list_rem_watch(list_T *l, listwatch_T *lwrem);
void list_fix_watch(list_T *l, listitem_T *item); void list_fix_watch(list_T *l, listitem_T *item);
list_T *list_alloc(void); list_T *list_alloc(void);
int rettv_list_alloc(typval_T *rettv); int rettv_list_alloc(typval_T *rettv);
void rettv_list_set(typval_T *rettv, list_T *l);
void list_unref(list_T *l); void list_unref(list_T *l);
int list_free_nonref(int copyID); int list_free_nonref(int copyID);
void list_free_items(int copyID); void list_free_items(int copyID);

View File

@@ -764,6 +764,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
593,
/**/ /**/
592, 592,
/**/ /**/