Skip to content

Commit 45cf6e9

Browse files
committed
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)
1 parent 29ae377 commit 45cf6e9

File tree

8 files changed

+48
-60
lines changed

8 files changed

+48
-60
lines changed

src/dict.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,23 @@ rettv_dict_alloc(typval_T *rettv)
5959
if (d == NULL)
6060
return FAIL;
6161

62-
rettv->vval.v_dict = d;
63-
rettv->v_type = VAR_DICT;
62+
rettv_dict_set(rettv, d);
6463
rettv->v_lock = 0;
65-
++d->dv_refcount;
6664
return OK;
6765
}
6866

67+
/*
68+
* Set a dictionary as the return value
69+
*/
70+
void
71+
rettv_dict_set(typval_T *rettv, dict_T *d)
72+
{
73+
rettv->v_type = VAR_DICT;
74+
rettv->vval.v_dict = d;
75+
if (d != NULL)
76+
++d->dv_refcount;
77+
}
78+
6979
/*
7080
* Free a Dictionary, including all non-container items it contains.
7181
* Ignores the reference count.
@@ -646,11 +656,7 @@ get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
646656

647657
*arg = skipwhite(*arg + 1);
648658
if (evaluate)
649-
{
650-
rettv->v_type = VAR_DICT;
651-
rettv->vval.v_dict = d;
652-
++d->dv_refcount;
653-
}
659+
rettv_dict_set(rettv, d);
654660

655661
return OK;
656662
}

src/eval.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4665,9 +4665,7 @@ eval_index(
46654665
item = item->li_next;
46664666
}
46674667
clear_tv(rettv);
4668-
rettv->v_type = VAR_LIST;
4669-
rettv->vval.v_list = l;
4670-
++l->lv_refcount;
4668+
rettv_list_set(rettv, l);
46714669
}
46724670
else
46734671
{
@@ -8486,9 +8484,7 @@ getwinvar(
84868484

84878485
if (opts != NULL)
84888486
{
8489-
rettv->v_type = VAR_DICT;
8490-
rettv->vval.v_dict = opts;
8491-
++opts->dv_refcount;
8487+
rettv_dict_set(rettv, opts);
84928488
done = TRUE;
84938489
}
84948490
}

src/evalfunc.c

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,8 +3005,7 @@ f_expand(typval_T *argvars, typval_T *rettv)
30053005
&& get_tv_number_chk(&argvars[2], &error)
30063006
&& !error)
30073007
{
3008-
rettv->v_type = VAR_LIST;
3009-
rettv->vval.v_list = NULL;
3008+
rettv_list_set(rettv, NULL);
30103009
}
30113010

30123011
s = get_tv_string(&argvars[0]);
@@ -3909,12 +3908,7 @@ f_get(typval_T *argvars, typval_T *rettv)
39093908
}
39103909
}
39113910
else if (STRCMP(what, "dict") == 0)
3912-
{
3913-
rettv->v_type = VAR_DICT;
3914-
rettv->vval.v_dict = pt->pt_dict;
3915-
if (pt->pt_dict != NULL)
3916-
++pt->pt_dict->dv_refcount;
3917-
}
3911+
rettv_dict_set(rettv, pt->pt_dict);
39183912
else if (STRCMP(what, "args") == 0)
39193913
{
39203914
rettv->v_type = VAR_LIST;
@@ -4214,9 +4208,7 @@ f_getbufvar(typval_T *argvars, typval_T *rettv)
42144208

42154209
if (opts != NULL)
42164210
{
4217-
rettv->v_type = VAR_DICT;
4218-
rettv->vval.v_dict = opts;
4219-
++opts->dv_refcount;
4211+
rettv_dict_set(rettv, opts);
42204212
done = TRUE;
42214213
}
42224214
}
@@ -5372,8 +5364,7 @@ f_glob(typval_T *argvars, typval_T *rettv)
53725364
{
53735365
if (get_tv_number_chk(&argvars[2], &error))
53745366
{
5375-
rettv->v_type = VAR_LIST;
5376-
rettv->vval.v_list = NULL;
5367+
rettv_list_set(rettv, NULL);
53775368
}
53785369
if (argvars[3].v_type != VAR_UNKNOWN
53795370
&& get_tv_number_chk(&argvars[3], &error))
@@ -5429,8 +5420,7 @@ f_globpath(typval_T *argvars, typval_T *rettv)
54295420
{
54305421
if (get_tv_number_chk(&argvars[3], &error))
54315422
{
5432-
rettv->v_type = VAR_LIST;
5433-
rettv->vval.v_list = NULL;
5423+
rettv_list_set(rettv, NULL);
54345424
}
54355425
if (argvars[4].v_type != VAR_UNKNOWN
54365426
&& get_tv_number_chk(&argvars[4], &error))
@@ -9152,9 +9142,7 @@ f_reverse(typval_T *argvars, typval_T *rettv)
91529142
list_append(l, li);
91539143
li = ni;
91549144
}
9155-
rettv->vval.v_list = l;
9156-
rettv->v_type = VAR_LIST;
9157-
++l->lv_refcount;
9145+
rettv_list_set(rettv, l);
91589146
l->lv_idx = l->lv_len - l->lv_idx - 1;
91599147
}
91609148
}
@@ -10742,9 +10730,7 @@ do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1074210730
(char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1074310731
TRUE))
1074410732
goto theend;
10745-
rettv->vval.v_list = l;
10746-
rettv->v_type = VAR_LIST;
10747-
++l->lv_refcount;
10733+
rettv_list_set(rettv, l);
1074810734

1074910735
len = list_len(l);
1075010736
if (len <= 1)
@@ -11832,8 +11818,7 @@ f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
1183211818
char_u str[NUMBUFLEN];
1183311819
#endif
1183411820

11835-
rettv->v_type = VAR_LIST;
11836-
rettv->vval.v_list = NULL;
11821+
rettv_list_set(rettv, NULL);
1183711822

1183811823
#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
1183911824
lnum = get_tv_lnum(argvars); /* -1 on type error */
@@ -11890,8 +11875,7 @@ f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
1189011875
int id;
1189111876
#endif
1189211877

11893-
rettv->v_type = VAR_LIST;
11894-
rettv->vval.v_list = NULL;
11878+
rettv_list_set(rettv, NULL);
1189511879

1189611880
#ifdef FEAT_SYN_HL
1189711881
lnum = get_tv_lnum(argvars); /* -1 on type error */
@@ -12057,9 +12041,7 @@ get_cmd_output_as_rettv(
1205712041
list_append(list, li);
1205812042
}
1205912043

12060-
++list->lv_refcount;
12061-
rettv->v_type = VAR_LIST;
12062-
rettv->vval.v_list = list;
12044+
rettv_list_set(rettv, list);
1206312045
list = NULL;
1206412046
}
1206512047
else
@@ -12465,8 +12447,7 @@ f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
1246512447
static void
1246612448
f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
1246712449
{
12468-
rettv->v_type = VAR_DICT;
12469-
rettv->vval.v_dict = NULL;
12450+
rettv_dict_set(rettv, NULL);
1247012451
}
1247112452

1247212453
#ifdef FEAT_JOB_CHANNEL
@@ -12481,8 +12462,7 @@ f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
1248112462
static void
1248212463
f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
1248312464
{
12484-
rettv->v_type = VAR_LIST;
12485-
rettv->vval.v_list = NULL;
12465+
rettv_list_set(rettv, NULL);
1248612466
}
1248712467

1248812468
static void

src/if_perl.xs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,9 +1136,7 @@ perl_to_vim(SV *sv, typval_T *rettv)
11361136
}
11371137
}
11381138

1139-
list->lv_refcount++;
1140-
rettv->v_type = VAR_LIST;
1141-
rettv->vval.v_list = list;
1139+
rettv_list_set(rettv, list);
11421140
break;
11431141
}
11441142
case SVt_PVHV: /* dictionary */
@@ -1192,9 +1190,7 @@ perl_to_vim(SV *sv, typval_T *rettv)
11921190
}
11931191
}
11941192

1195-
dict->dv_refcount++;
1196-
rettv->v_type = VAR_DICT;
1197-
rettv->vval.v_dict = dict;
1193+
rettv_dict_set(rettv, dict);
11981194
break;
11991195
}
12001196
default: /* not convertible */

src/list.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,23 @@ rettv_list_alloc(typval_T *rettv)
9797
if (l == NULL)
9898
return FAIL;
9999

100-
rettv->vval.v_list = l;
101-
rettv->v_type = VAR_LIST;
102100
rettv->v_lock = 0;
103-
++l->lv_refcount;
101+
rettv_list_set(rettv, l);
104102
return OK;
105103
}
106104

105+
/*
106+
* Set a list as the return value
107+
*/
108+
void
109+
rettv_list_set(typval_T *rettv, list_T *l)
110+
{
111+
rettv->v_type = VAR_LIST;
112+
rettv->vval.v_list = l;
113+
if (l != NULL)
114+
++l->lv_refcount;
115+
}
116+
107117
/*
108118
* Unreference a list: decrement the reference count and free it when it
109119
* becomes zero.
@@ -875,11 +885,7 @@ get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
875885

876886
*arg = skipwhite(*arg + 1);
877887
if (evaluate)
878-
{
879-
rettv->v_type = VAR_LIST;
880-
rettv->vval.v_list = l;
881-
++l->lv_refcount;
882-
}
888+
rettv_list_set(rettv, l);
883889

884890
return OK;
885891
}

src/proto/dict.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* dict.c */
22
dict_T *dict_alloc(void);
33
int rettv_dict_alloc(typval_T *rettv);
4+
void rettv_dict_set(typval_T *rettv, dict_T *d);
45
void dict_unref(dict_T *d);
56
int dict_free_nonref(int copyID);
67
void dict_free_items(int copyID);

src/proto/list.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ void list_rem_watch(list_T *l, listwatch_T *lwrem);
44
void list_fix_watch(list_T *l, listitem_T *item);
55
list_T *list_alloc(void);
66
int rettv_list_alloc(typval_T *rettv);
7+
void rettv_list_set(typval_T *rettv, list_T *l);
78
void list_unref(list_T *l);
89
int list_free_nonref(int copyID);
910
void list_free_items(int copyID);

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
593,
767769
/**/
768770
592,
769771
/**/

0 commit comments

Comments
 (0)