blob: 62958f6302a612703fc9d35b557589fd66ca3bef [file] [log] [blame]
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * typval.c: functions that deal with a typval
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate memory for a variable type-value, and make it empty (0 or NULL
20 * value).
21 */
22 typval_T *
23alloc_tv(void)
24{
25 return ALLOC_CLEAR_ONE(typval_T);
26}
27
28/*
29 * Allocate memory for a variable type-value, and assign a string to it.
30 * The string "s" must have been allocated, it is consumed.
31 * Return NULL for out of memory, the variable otherwise.
32 */
33 typval_T *
34alloc_string_tv(char_u *s)
35{
36 typval_T *rettv;
37
38 rettv = alloc_tv();
39 if (rettv != NULL)
40 {
41 rettv->v_type = VAR_STRING;
42 rettv->vval.v_string = s;
43 }
44 else
45 vim_free(s);
46 return rettv;
47}
48
49/*
50 * Free the memory for a variable type-value.
51 */
52 void
53free_tv(typval_T *varp)
54{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000055 if (varp == NULL)
56 return;
Bram Moolenaar00b28d62022-12-08 15:32:33 +000057
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000058 switch (varp->v_type)
59 {
60 case VAR_FUNC:
61 func_unref(varp->vval.v_string);
62 // FALLTHROUGH
63 case VAR_STRING:
64 vim_free(varp->vval.v_string);
65 break;
66 case VAR_PARTIAL:
67 partial_unref(varp->vval.v_partial);
68 break;
69 case VAR_BLOB:
70 blob_unref(varp->vval.v_blob);
71 break;
72 case VAR_LIST:
73 list_unref(varp->vval.v_list);
74 break;
75 case VAR_DICT:
76 dict_unref(varp->vval.v_dict);
77 break;
78 case VAR_JOB:
79#ifdef FEAT_JOB_CHANNEL
80 job_unref(varp->vval.v_job);
81 break;
82#endif
83 case VAR_CHANNEL:
84#ifdef FEAT_JOB_CHANNEL
85 channel_unref(varp->vval.v_channel);
86 break;
87#endif
88 case VAR_CLASS:
89 class_unref(varp->vval.v_class);
90 break;
91 case VAR_OBJECT:
92 object_unref(varp->vval.v_object);
93 break;
94
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +020095 case VAR_TYPEALIAS:
96 typealias_unref(varp->vval.v_typealias);
97 break;
98
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000099 case VAR_NUMBER:
100 case VAR_FLOAT:
101 case VAR_ANY:
102 case VAR_UNKNOWN:
103 case VAR_VOID:
104 case VAR_BOOL:
105 case VAR_SPECIAL:
106 case VAR_INSTR:
107 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200108 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000109 vim_free(varp);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200110}
111
112/*
113 * Free the memory for a variable value and set the value to NULL or 0.
114 */
115 void
116clear_tv(typval_T *varp)
117{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000118 if (varp == NULL)
119 return;
120
121 switch (varp->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200122 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000123 case VAR_FUNC:
124 func_unref(varp->vval.v_string);
125 // FALLTHROUGH
126 case VAR_STRING:
127 VIM_CLEAR(varp->vval.v_string);
128 break;
129 case VAR_PARTIAL:
130 partial_unref(varp->vval.v_partial);
131 varp->vval.v_partial = NULL;
132 break;
133 case VAR_BLOB:
134 blob_unref(varp->vval.v_blob);
135 varp->vval.v_blob = NULL;
136 break;
137 case VAR_LIST:
138 list_unref(varp->vval.v_list);
139 varp->vval.v_list = NULL;
140 break;
141 case VAR_DICT:
142 dict_unref(varp->vval.v_dict);
143 varp->vval.v_dict = NULL;
144 break;
145 case VAR_NUMBER:
146 case VAR_BOOL:
147 case VAR_SPECIAL:
148 varp->vval.v_number = 0;
149 break;
150 case VAR_FLOAT:
151 varp->vval.v_float = 0.0;
152 break;
153 case VAR_JOB:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200154#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000155 job_unref(varp->vval.v_job);
156 varp->vval.v_job = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200157#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000158 break;
159 case VAR_CHANNEL:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200160#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000161 channel_unref(varp->vval.v_channel);
162 varp->vval.v_channel = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200163#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000164 break;
165 case VAR_INSTR:
166 VIM_CLEAR(varp->vval.v_instr);
167 break;
168 case VAR_CLASS:
169 class_unref(varp->vval.v_class);
170 varp->vval.v_class = NULL;
171 break;
172 case VAR_OBJECT:
173 object_unref(varp->vval.v_object);
174 varp->vval.v_object = NULL;
175 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200176 case VAR_TYPEALIAS:
177 typealias_unref(varp->vval.v_typealias);
178 varp->vval.v_typealias = NULL;
179 break;
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000180 case VAR_UNKNOWN:
181 case VAR_ANY:
182 case VAR_VOID:
183 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200184 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000185 varp->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200186}
187
188/*
189 * Set the value of a variable to NULL without freeing items.
190 */
191 void
192init_tv(typval_T *varp)
193{
194 if (varp != NULL)
195 CLEAR_POINTER(varp);
196}
197
Bram Moolenaar36967b32020-08-17 21:41:02 +0200198 static varnumber_T
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000199tv_get_bool_or_number_chk(
200 typval_T *varp,
201 int *denote,
202 int want_bool,
203 int vim9_string_error) // in Vim9 using a string is an error
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200204{
205 varnumber_T n = 0L;
206
207 switch (varp->v_type)
208 {
209 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200210 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200211 && varp->vval.v_number != 1)
212 {
213 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
214 break;
215 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200216 return varp->vval.v_number;
217 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000218 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200219 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200220 case VAR_FUNC:
221 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000222 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200223 break;
224 case VAR_STRING:
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000225 if (vim9_string_error && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200226 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100227 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200228 break;
229 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200230 if (varp->vval.v_string != NULL)
231 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000232 STR2NR_ALL, &n, NULL, 0, FALSE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200233 return n;
234 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000235 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 break;
237 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000238 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200239 break;
240 case VAR_BOOL:
241 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200242 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200243 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100244 if (varp->v_type == VAR_BOOL)
245 emsg(_(e_using_bool_as_number));
246 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000247 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200248 break;
249 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200250 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
251 case VAR_JOB:
252#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000253 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200254 break;
255#endif
256 case VAR_CHANNEL:
257#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000258 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200259 break;
260#endif
261 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000262 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200263 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000264 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +0100265 case VAR_TYPEALIAS:
266 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000267 break;
268 case VAR_OBJECT:
269 emsg(_(e_using_object_as_number));
270 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200271 case VAR_VOID:
272 emsg(_(e_cannot_use_void_value));
273 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200274 case VAR_UNKNOWN:
275 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200276 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200277 internal_error_no_abort("tv_get_number(UNKNOWN)");
278 break;
279 }
280 if (denote == NULL) // useful for values that must be unsigned
281 n = -1;
282 else
283 *denote = TRUE;
284 return n;
285}
286
Bram Moolenaar36967b32020-08-17 21:41:02 +0200287/*
288 * Get the number value of a variable.
289 * If it is a String variable, uses vim_str2nr().
290 * For incompatible types, return 0.
291 * tv_get_number_chk() is similar to tv_get_number(), but informs the
292 * caller of incompatible types: it sets *denote to TRUE if "denote"
293 * is not NULL or returns -1 otherwise.
294 */
295 varnumber_T
296tv_get_number(typval_T *varp)
297{
298 int error = FALSE;
299
300 return tv_get_number_chk(varp, &error); // return 0L on error
301}
302
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000303/*
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200304 * Like tv_get_number() but in Vim9 script do convert a number in a string to a
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000305 * number without giving an error.
306 */
307 varnumber_T
308tv_to_number(typval_T *varp)
309{
310 int error = FALSE;
311
312 return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
313}
314
Bram Moolenaar36967b32020-08-17 21:41:02 +0200315 varnumber_T
316tv_get_number_chk(typval_T *varp, int *denote)
317{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000318 return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200319}
320
321/*
322 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200323 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200324 */
325 varnumber_T
326tv_get_bool(typval_T *varp)
327{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000328 return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200329}
330
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200331/*
332 * Get the boolean value of "varp". This is like tv_get_number_chk(),
333 * but in Vim9 script accepts Number and Bool.
334 */
335 varnumber_T
336tv_get_bool_chk(typval_T *varp, int *denote)
337{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000338 return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200339}
340
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000341 static float_T
342tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200343{
344 switch (varp->v_type)
345 {
346 case VAR_NUMBER:
347 return (float_T)(varp->vval.v_number);
348 case VAR_FLOAT:
349 return varp->vval.v_float;
350 case VAR_FUNC:
351 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000352 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200353 break;
354 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000355 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200356 break;
357 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000358 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200359 break;
360 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000361 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200362 break;
363 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000364 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200365 break;
366 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000367 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200368 break;
369 case VAR_JOB:
370# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000371 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200372 break;
373# endif
374 case VAR_CHANNEL:
375# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000376 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200377 break;
378# endif
379 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000380 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200381 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000382 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +0100383 case VAR_TYPEALIAS:
384 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 break;
386 case VAR_OBJECT:
387 emsg(_(e_using_object_as_float));
388 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200389 case VAR_VOID:
390 emsg(_(e_cannot_use_void_value));
391 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200392 case VAR_UNKNOWN:
393 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200394 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200395 internal_error_no_abort("tv_get_float(UNKNOWN)");
396 break;
397 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000398 if (error != NULL)
399 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200400 return 0;
401}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000402
403 float_T
404tv_get_float(typval_T *varp)
405{
406 return tv_get_float_chk(varp, NULL);
407}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200408
409/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100410 * Give an error and return FAIL unless "args[idx]" is unknown
411 */
412 int
413check_for_unknown_arg(typval_T *args, int idx)
414{
415 if (args[idx].v_type != VAR_UNKNOWN)
416 {
417 semsg(_(e_too_many_arguments), idx + 1);
418 return FAIL;
419 }
420 return OK;
421}
422
423/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200424 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100425 */
426 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100427check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100428{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100429 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100430 {
rbtnnddc80af2021-12-17 18:01:31 +0000431 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100432 return FAIL;
433 }
434 return OK;
435}
436
437/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100438 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100439 */
440 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100441check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100442{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100443 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100444 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100445 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100446 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200447 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100448 return FAIL;
449 }
450 return OK;
451}
452
453/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200454 * Check for an optional string argument at 'idx'
455 */
456 int
457check_for_opt_string_arg(typval_T *args, int idx)
458{
459 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100460 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200461}
462
463/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200464 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200465 */
466 int
467check_for_number_arg(typval_T *args, int idx)
468{
469 if (args[idx].v_type != VAR_NUMBER)
470 {
rbtnnddc80af2021-12-17 18:01:31 +0000471 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200472 return FAIL;
473 }
474 return OK;
475}
476
477/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200478 * Check for an optional number argument at 'idx'
479 */
480 int
481check_for_opt_number_arg(typval_T *args, int idx)
482{
483 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100484 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200485}
486
487/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200488 * Give an error and return FAIL unless "args[idx]" is a float or a number.
489 */
490 int
491check_for_float_or_nr_arg(typval_T *args, int idx)
492{
493 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
494 {
rbtnnddc80af2021-12-17 18:01:31 +0000495 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200496 return FAIL;
497 }
498 return OK;
499}
500
501/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200502 * Give an error and return FAIL unless "args[idx]" is a bool.
503 */
504 int
505check_for_bool_arg(typval_T *args, int idx)
506{
507 if (args[idx].v_type != VAR_BOOL
508 && !(args[idx].v_type == VAR_NUMBER
509 && (args[idx].vval.v_number == 0
510 || args[idx].vval.v_number == 1)))
511 {
rbtnnddc80af2021-12-17 18:01:31 +0000512 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200513 return FAIL;
514 }
515 return OK;
516}
517
518/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200519 * Check for an optional bool argument at 'idx'.
520 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200521 */
522 int
523check_for_opt_bool_arg(typval_T *args, int idx)
524{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200525 if (args[idx].v_type == VAR_UNKNOWN)
526 return OK;
527 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200528}
529
530/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200531 * Give an error and return FAIL unless "args[idx]" is a blob.
532 */
533 int
534check_for_blob_arg(typval_T *args, int idx)
535{
536 if (args[idx].v_type != VAR_BLOB)
537 {
rbtnnddc80af2021-12-17 18:01:31 +0000538 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200539 return FAIL;
540 }
541 return OK;
542}
543
544/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200545 * Give an error and return FAIL unless "args[idx]" is a list.
546 */
547 int
548check_for_list_arg(typval_T *args, int idx)
549{
550 if (args[idx].v_type != VAR_LIST)
551 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200552 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200553 return FAIL;
554 }
555 return OK;
556}
557
558/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100559 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
560 */
561 int
562check_for_nonnull_list_arg(typval_T *args, int idx)
563{
564 if (check_for_list_arg(args, idx) == FAIL)
565 return FAIL;
566
567 if (args[idx].vval.v_list == NULL)
568 {
569 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
570 return FAIL;
571 }
572 return OK;
573}
574
575/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200576 * Check for an optional list argument at 'idx'
577 */
578 int
579check_for_opt_list_arg(typval_T *args, int idx)
580{
581 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100582 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200583}
584
585/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200586 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200587 */
588 int
589check_for_dict_arg(typval_T *args, int idx)
590{
591 if (args[idx].v_type != VAR_DICT)
592 {
rbtnnddc80af2021-12-17 18:01:31 +0000593 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200594 return FAIL;
595 }
596 return OK;
597}
598
599/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100600 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
601 */
602 int
603check_for_nonnull_dict_arg(typval_T *args, int idx)
604{
605 if (check_for_dict_arg(args, idx) == FAIL)
606 return FAIL;
607
608 if (args[idx].vval.v_dict == NULL)
609 {
610 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
611 return FAIL;
612 }
613 return OK;
614}
615
616/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200617 * Check for an optional dict argument at 'idx'
618 */
619 int
620check_for_opt_dict_arg(typval_T *args, int idx)
621{
622 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100623 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200624}
625
Yegappan Lakshmananfa378352024-02-01 22:05:27 +0100626/*
627 * Check for an optional non-NULL dict argument at 'idx'
628 */
629 int
630check_for_opt_nonnull_dict_arg(typval_T *args, int idx)
631{
632 return (args[idx].v_type == VAR_UNKNOWN
633 || check_for_nonnull_dict_arg(args, idx) != FAIL) ? OK : FAIL;
634}
635
Dominique Pelle748b3082022-01-08 12:41:16 +0000636#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200637/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200638 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
639 */
640 int
641check_for_chan_or_job_arg(typval_T *args, int idx)
642{
643 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
644 {
rbtnnddc80af2021-12-17 18:01:31 +0000645 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200646 return FAIL;
647 }
648 return OK;
649}
650
651/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200652 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
653 * job.
654 */
655 int
656check_for_opt_chan_or_job_arg(typval_T *args, int idx)
657{
658 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100659 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200660}
661
662/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200663 * Give an error and return FAIL unless "args[idx]" is a job.
664 */
665 int
666check_for_job_arg(typval_T *args, int idx)
667{
668 if (args[idx].v_type != VAR_JOB)
669 {
rbtnnddc80af2021-12-17 18:01:31 +0000670 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200671 return FAIL;
672 }
673 return OK;
674}
675
676/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200677 * Check for an optional job argument at 'idx'.
678 */
679 int
680check_for_opt_job_arg(typval_T *args, int idx)
681{
682 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100683 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200684}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000685#else
686/*
687 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
688 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
689 */
690 int
691check_for_opt_chan_or_job_arg(typval_T *args, int idx)
692{
693 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
694}
Dominique Pelle748b3082022-01-08 12:41:16 +0000695#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200696
697/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200698 * Give an error and return FAIL unless "args[idx]" is a string or
699 * a number.
700 */
701 int
702check_for_string_or_number_arg(typval_T *args, int idx)
703{
704 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
705 {
rbtnnddc80af2021-12-17 18:01:31 +0000706 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200707 return FAIL;
708 }
709 return OK;
710}
711
712/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200713 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200714 */
715 int
716check_for_opt_string_or_number_arg(typval_T *args, int idx)
717{
718 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100719 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200720}
721
722/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200723 * Give an error and return FAIL unless "args[idx]" is a buffer number.
724 * Buffer number can be a number or a string.
725 */
726 int
727check_for_buffer_arg(typval_T *args, int idx)
728{
729 return check_for_string_or_number_arg(args, idx);
730}
731
732/*
733 * Check for an optional buffer argument at 'idx'
734 */
735 int
736check_for_opt_buffer_arg(typval_T *args, int idx)
737{
738 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100739 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200740}
741
742/*
743 * Give an error and return FAIL unless "args[idx]" is a line number.
744 * Line number can be a number or a string.
745 */
746 int
747check_for_lnum_arg(typval_T *args, int idx)
748{
749 return check_for_string_or_number_arg(args, idx);
750}
751
752/*
753 * Check for an optional line number argument at 'idx'
754 */
755 int
756check_for_opt_lnum_arg(typval_T *args, int idx)
757{
758 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100759 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200760}
761
Dominique Pelle748b3082022-01-08 12:41:16 +0000762#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200763/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200764 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200765 */
766 int
767check_for_string_or_blob_arg(typval_T *args, int idx)
768{
769 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
770 {
rbtnnddc80af2021-12-17 18:01:31 +0000771 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200772 return FAIL;
773 }
774 return OK;
775}
Dominique Pelle748b3082022-01-08 12:41:16 +0000776#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200777
778/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200779 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200780 */
781 int
782check_for_string_or_list_arg(typval_T *args, int idx)
783{
784 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
785 {
rbtnnddc80af2021-12-17 18:01:31 +0000786 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200787 return FAIL;
788 }
789 return OK;
790}
791
792/*
rbtnn0ccb5842021-12-18 18:33:46 +0000793 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
794 * blob.
795 */
796 int
797check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
798{
799 if (args[idx].v_type != VAR_STRING
800 && args[idx].v_type != VAR_LIST
801 && args[idx].v_type != VAR_BLOB)
802 {
803 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
804 return FAIL;
805 }
806 return OK;
807}
808
809/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200810 * Check for an optional string or list argument at 'idx'
811 */
812 int
813check_for_opt_string_or_list_arg(typval_T *args, int idx)
814{
815 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100816 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200817}
818
819/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200820 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
821 */
822 int
823check_for_string_or_dict_arg(typval_T *args, int idx)
824{
825 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
826 {
rbtnnddc80af2021-12-17 18:01:31 +0000827 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200828 return FAIL;
829 }
830 return OK;
831}
832
833/*
834 * Give an error and return FAIL unless "args[idx]" is a string or a number
835 * or a list.
836 */
837 int
838check_for_string_or_number_or_list_arg(typval_T *args, int idx)
839{
840 if (args[idx].v_type != VAR_STRING
841 && args[idx].v_type != VAR_NUMBER
842 && args[idx].v_type != VAR_LIST)
843 {
rbtnn0ccb5842021-12-18 18:33:46 +0000844 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200845 return FAIL;
846 }
847 return OK;
848}
849
850/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200851 * Give an error and return FAIL unless "args[idx]" is an optional string
852 * or number or a list
853 */
854 int
855check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
856{
857 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100858 || check_for_string_or_number_or_list_arg(args, idx)
859 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200860}
861
862/*
Bakudankun375141e2022-09-09 18:46:47 +0100863 * Give an error and return FAIL unless "args[idx]" is a string or a number
864 * or a list or a blob.
865 */
866 int
867check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
868{
869 if (args[idx].v_type != VAR_STRING
870 && args[idx].v_type != VAR_NUMBER
871 && args[idx].v_type != VAR_LIST
872 && args[idx].v_type != VAR_BLOB)
873 {
874 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
875 return FAIL;
876 }
877 return OK;
878}
879
880/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200881 * Give an error and return FAIL unless "args[idx]" is a string or a list
882 * or a dict.
883 */
884 int
885check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
886{
887 if (args[idx].v_type != VAR_STRING
888 && args[idx].v_type != VAR_LIST
889 && args[idx].v_type != VAR_DICT)
890 {
rbtnnddc80af2021-12-17 18:01:31 +0000891 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200892 return FAIL;
893 }
894 return OK;
895}
896
897/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000898 * Give an error and return FAIL unless "args[idx]" is a string
899 * or a function reference.
900 */
901 int
902check_for_string_or_func_arg(typval_T *args, int idx)
903{
904 if (args[idx].v_type != VAR_PARTIAL
905 && args[idx].v_type != VAR_FUNC
906 && args[idx].v_type != VAR_STRING)
907 {
908 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
909 return FAIL;
910 }
911 return OK;
912}
913
914/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200915 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
916 */
917 int
918check_for_list_or_blob_arg(typval_T *args, int idx)
919{
920 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
921 {
rbtnn0ccb5842021-12-18 18:33:46 +0000922 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200923 return FAIL;
924 }
925 return OK;
926}
927
928/*
929 * Give an error and return FAIL unless "args[idx]" is a list or dict
930 */
931 int
932check_for_list_or_dict_arg(typval_T *args, int idx)
933{
934 if (args[idx].v_type != VAR_LIST
935 && args[idx].v_type != VAR_DICT)
936 {
rbtnnddc80af2021-12-17 18:01:31 +0000937 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200938 return FAIL;
939 }
940 return OK;
941}
942
943/*
944 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
945 * blob.
946 */
947 int
948check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
949{
950 if (args[idx].v_type != VAR_LIST
951 && args[idx].v_type != VAR_DICT
952 && args[idx].v_type != VAR_BLOB)
953 {
rbtnnddc80af2021-12-17 18:01:31 +0000954 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200955 return FAIL;
956 }
957 return OK;
958}
959
960/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000961 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
962 * blob or a string.
963 */
964 int
965check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
966{
967 if (args[idx].v_type != VAR_LIST
968 && args[idx].v_type != VAR_DICT
969 && args[idx].v_type != VAR_BLOB
970 && args[idx].v_type != VAR_STRING)
971 {
rbtnnddc80af2021-12-17 18:01:31 +0000972 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000973 return FAIL;
974 }
975 return OK;
976}
977
978/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200979 * Give an error and return FAIL unless "args[idx]" is an optional buffer
980 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200981 */
982 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200983check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200984{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200985 if (args[idx].v_type != VAR_UNKNOWN
986 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200987 && args[idx].v_type != VAR_NUMBER
988 && args[idx].v_type != VAR_DICT)
989 {
rbtnnddc80af2021-12-17 18:01:31 +0000990 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200991 return FAIL;
992 }
993 return OK;
994}
995
996/*
LemonBoyafe04662023-08-23 21:08:11 +0200997 * Give an error and return FAIL unless "args[idx]" is an object.
998 */
999 int
1000check_for_object_arg(typval_T *args, int idx)
1001{
1002 if (args[idx].v_type != VAR_OBJECT)
1003 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001004 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001005 return FAIL;
1006 }
1007 return OK;
1008}
1009
1010/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001011 * Returns TRUE if "tv" is a type alias for a class
1012 */
1013 int
1014tv_class_alias(typval_T *tv)
1015{
1016 return tv->v_type == VAR_TYPEALIAS &&
1017 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1018}
1019
1020/*
Ernie Rael2025af12023-12-12 16:58:00 +01001021 * Give an error and return FAIL unless "args[idx]" is a class
1022 * or class typealias.
LemonBoyafe04662023-08-23 21:08:11 +02001023 */
1024 int
Ernie Rael2025af12023-12-12 16:58:00 +01001025check_for_class_or_typealias_args(typval_T *args, int idx)
LemonBoyafe04662023-08-23 21:08:11 +02001026{
Ernie Rael2025af12023-12-12 16:58:00 +01001027 for (int i = idx; args[i].v_type != VAR_UNKNOWN; ++i)
LemonBoyafe04662023-08-23 21:08:11 +02001028 {
Ernie Rael2025af12023-12-12 16:58:00 +01001029 if (args[i].v_type != VAR_CLASS && !tv_class_alias(&args[idx]))
1030 {
1031 semsg(_(e_class_or_typealias_required_for_argument_nr), i + 1);
1032 return FAIL;
1033 }
LemonBoyafe04662023-08-23 21:08:11 +02001034 }
1035 return OK;
1036}
1037
1038/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001039 * Get the string value of a variable.
1040 * If it is a Number variable, the number is converted into a string.
1041 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1042 * tv_get_string_buf() uses a given buffer.
1043 * If the String variable has never been set, return an empty string.
1044 * Never returns NULL;
1045 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1046 * NULL on error.
1047 */
1048 char_u *
1049tv_get_string(typval_T *varp)
1050{
1051 static char_u mybuf[NUMBUFLEN];
1052
1053 return tv_get_string_buf(varp, mybuf);
1054}
1055
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001056/*
1057 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1058 */
1059 char_u *
1060tv_get_string_strict(typval_T *varp)
1061{
1062 static char_u mybuf[NUMBUFLEN];
1063 char_u *res = tv_get_string_buf_chk_strict(
1064 varp, mybuf, in_vim9script());
1065
1066 return res != NULL ? res : (char_u *)"";
1067}
1068
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001069 char_u *
1070tv_get_string_buf(typval_T *varp, char_u *buf)
1071{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001072 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001073
1074 return res != NULL ? res : (char_u *)"";
1075}
1076
1077/*
1078 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1079 */
1080 char_u *
1081tv_get_string_chk(typval_T *varp)
1082{
1083 static char_u mybuf[NUMBUFLEN];
1084
1085 return tv_get_string_buf_chk(varp, mybuf);
1086}
1087
1088 char_u *
1089tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1090{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001091 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1092}
1093
1094 char_u *
1095tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1096{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001097 switch (varp->v_type)
1098 {
1099 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001100 if (strict)
1101 {
1102 emsg(_(e_using_number_as_string));
1103 break;
1104 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001105 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1106 (varnumber_T)varp->vval.v_number);
1107 return buf;
1108 case VAR_FUNC:
1109 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001110 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001111 break;
1112 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001113 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001114 break;
1115 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001116 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001117 break;
1118 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001119 if (strict)
1120 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001121 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001122 break;
1123 }
1124 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1125 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001126 case VAR_STRING:
1127 if (varp->vval.v_string != NULL)
1128 return varp->vval.v_string;
1129 return (char_u *)"";
1130 case VAR_BOOL:
1131 case VAR_SPECIAL:
1132 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1133 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001134 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001135 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001136 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001137 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +01001138 case VAR_TYPEALIAS:
1139 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001140 break;
1141 case VAR_OBJECT:
1142 emsg(_(e_using_object_as_string));
1143 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001144 case VAR_JOB:
1145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001146 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001147 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001148 semsg(_(e_using_invalid_value_as_string_str), "job");
1149 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001150 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001151 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001152#endif
1153 break;
1154 case VAR_CHANNEL:
1155#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001156 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001157 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001158 semsg(_(e_using_invalid_value_as_string_str), "channel");
1159 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001160 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001161 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001162#endif
1163 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001164 case VAR_VOID:
1165 emsg(_(e_cannot_use_void_value));
1166 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001167 case VAR_UNKNOWN:
1168 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001169 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001170 semsg(_(e_using_invalid_value_as_string_str),
1171 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001172 break;
1173 }
1174 return NULL;
1175}
1176
1177/*
1178 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1179 * string() on Dict, List, etc.
1180 */
1181 char_u *
1182tv_stringify(typval_T *varp, char_u *buf)
1183{
1184 if (varp->v_type == VAR_LIST
1185 || varp->v_type == VAR_DICT
1186 || varp->v_type == VAR_BLOB
1187 || varp->v_type == VAR_FUNC
1188 || varp->v_type == VAR_PARTIAL
1189 || varp->v_type == VAR_FLOAT)
1190 {
1191 typval_T tmp;
1192
1193 f_string(varp, &tmp);
1194 tv_get_string_buf(&tmp, buf);
1195 clear_tv(varp);
1196 *varp = tmp;
1197 return tmp.vval.v_string;
1198 }
1199 return tv_get_string_buf(varp, buf);
1200}
1201
1202/*
1203 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1204 * Also give an error message, using "name" or _("name") when use_gettext is
1205 * TRUE.
1206 */
1207 int
1208tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1209{
1210 int lock = 0;
1211
1212 switch (tv->v_type)
1213 {
1214 case VAR_BLOB:
1215 if (tv->vval.v_blob != NULL)
1216 lock = tv->vval.v_blob->bv_lock;
1217 break;
1218 case VAR_LIST:
1219 if (tv->vval.v_list != NULL)
1220 lock = tv->vval.v_list->lv_lock;
1221 break;
1222 case VAR_DICT:
1223 if (tv->vval.v_dict != NULL)
1224 lock = tv->vval.v_dict->dv_lock;
1225 break;
1226 default:
1227 break;
1228 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001229 return value_check_lock(tv->v_lock, name, use_gettext)
1230 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001231}
1232
1233/*
1234 * Copy the values from typval_T "from" to typval_T "to".
1235 * When needed allocates string or increases reference count.
1236 * Does not make a copy of a list, blob or dict but copies the reference!
1237 * It is OK for "from" and "to" to point to the same item. This is used to
1238 * make a copy later.
1239 */
1240 void
1241copy_tv(typval_T *from, typval_T *to)
1242{
1243 to->v_type = from->v_type;
1244 to->v_lock = 0;
1245 switch (from->v_type)
1246 {
1247 case VAR_NUMBER:
1248 case VAR_BOOL:
1249 case VAR_SPECIAL:
1250 to->vval.v_number = from->vval.v_number;
1251 break;
1252 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001253 to->vval.v_float = from->vval.v_float;
1254 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001255 case VAR_JOB:
1256#ifdef FEAT_JOB_CHANNEL
1257 to->vval.v_job = from->vval.v_job;
1258 if (to->vval.v_job != NULL)
1259 ++to->vval.v_job->jv_refcount;
1260 break;
1261#endif
1262 case VAR_CHANNEL:
1263#ifdef FEAT_JOB_CHANNEL
1264 to->vval.v_channel = from->vval.v_channel;
1265 if (to->vval.v_channel != NULL)
1266 ++to->vval.v_channel->ch_refcount;
1267 break;
1268#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001269 case VAR_INSTR:
1270 to->vval.v_instr = from->vval.v_instr;
1271 break;
1272
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001273 case VAR_CLASS:
1274 copy_class(from, to);
1275 break;
1276
1277 case VAR_OBJECT:
1278 copy_object(from, to);
1279 break;
1280
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001281 case VAR_STRING:
1282 case VAR_FUNC:
1283 if (from->vval.v_string == NULL)
1284 to->vval.v_string = NULL;
1285 else
1286 {
1287 to->vval.v_string = vim_strsave(from->vval.v_string);
1288 if (from->v_type == VAR_FUNC)
1289 func_ref(to->vval.v_string);
1290 }
1291 break;
1292 case VAR_PARTIAL:
1293 if (from->vval.v_partial == NULL)
1294 to->vval.v_partial = NULL;
1295 else
1296 {
1297 to->vval.v_partial = from->vval.v_partial;
1298 ++to->vval.v_partial->pt_refcount;
1299 }
1300 break;
1301 case VAR_BLOB:
1302 if (from->vval.v_blob == NULL)
1303 to->vval.v_blob = NULL;
1304 else
1305 {
1306 to->vval.v_blob = from->vval.v_blob;
1307 ++to->vval.v_blob->bv_refcount;
1308 }
1309 break;
1310 case VAR_LIST:
1311 if (from->vval.v_list == NULL)
1312 to->vval.v_list = NULL;
1313 else
1314 {
1315 to->vval.v_list = from->vval.v_list;
1316 ++to->vval.v_list->lv_refcount;
1317 }
1318 break;
1319 case VAR_DICT:
1320 if (from->vval.v_dict == NULL)
1321 to->vval.v_dict = NULL;
1322 else
1323 {
1324 to->vval.v_dict = from->vval.v_dict;
1325 ++to->vval.v_dict->dv_refcount;
1326 }
1327 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001328 case VAR_TYPEALIAS:
1329 if (from->vval.v_typealias == NULL)
1330 to->vval.v_typealias = NULL;
1331 else
1332 {
1333 to->vval.v_typealias = from->vval.v_typealias;
1334 ++to->vval.v_typealias->ta_refcount;
1335 }
1336 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001337 case VAR_VOID:
1338 emsg(_(e_cannot_use_void_value));
1339 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001340 case VAR_UNKNOWN:
1341 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001342 internal_error_no_abort("copy_tv(UNKNOWN)");
1343 break;
1344 }
1345}
1346
1347/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001348 * Compare "tv1" and "tv2".
1349 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001350 */
1351 int
1352typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001353 typval_T *tv1, // first operand
1354 typval_T *tv2, // second operand
1355 exprtype_T type, // operator
1356 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001357{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001358 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001359 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001360 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1361
Ernie Raele75fde62023-12-21 17:18:54 +01001362 if (check_typval_is_value(tv1) == FAIL
1363 || check_typval_is_value(tv2) == FAIL)
1364 {
1365 clear_tv(tv1);
1366 return FAIL;
1367 }
1368 else if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001369 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001370 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001371 // it means TRUE.
1372 n1 = (type == EXPR_ISNOT);
1373 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001374 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1375 || (tv2->v_type == VAR_SPECIAL
1376 && tv2->vval.v_number == VVAL_NULL))
1377 && tv1->v_type != tv2->v_type
1378 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1379 {
1380 n1 = typval_compare_null(tv1, tv2);
1381 if (n1 == MAYBE)
1382 {
1383 clear_tv(tv1);
1384 return FAIL;
1385 }
1386 if (type == EXPR_NEQUAL)
1387 n1 = !n1;
1388 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001389 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001390 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001391 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001392 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001393 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001394 return FAIL;
1395 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001396 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001397 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001398 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001399 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001400 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001401 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001402 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001403 return FAIL;
1404 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001405 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001406 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001407 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1408 {
1409 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1410 {
1411 clear_tv(tv1);
1412 return FAIL;
1413 }
1414 n1 = res;
1415 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001416 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001417 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001418 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001419 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001420 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001421 return FAIL;
1422 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001423 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001424 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001425 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1426 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001427 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001428 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001429 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001430 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001431 return FAIL;
1432 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001433 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001434 }
1435
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001436 // If one of the two variables is a float, compare as a float.
1437 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001438 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001439 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1440 {
1441 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001442 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001443
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001444 f1 = tv_get_float_chk(tv1, &error);
1445 if (!error)
1446 f2 = tv_get_float_chk(tv2, &error);
1447 if (error)
1448 {
1449 clear_tv(tv1);
1450 return FAIL;
1451 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001452 n1 = FALSE;
1453 switch (type)
1454 {
1455 case EXPR_IS:
1456 case EXPR_EQUAL: n1 = (f1 == f2); break;
1457 case EXPR_ISNOT:
1458 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1459 case EXPR_GREATER: n1 = (f1 > f2); break;
1460 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1461 case EXPR_SMALLER: n1 = (f1 < f2); break;
1462 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1463 case EXPR_UNKNOWN:
1464 case EXPR_MATCH:
1465 default: break; // avoid gcc warning
1466 }
1467 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001468
1469 // If one of the two variables is a number, compare as a number.
1470 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001471 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001472 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1473 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001474 int error = FALSE;
1475
1476 n1 = tv_get_number_chk(tv1, &error);
1477 if (!error)
1478 n2 = tv_get_number_chk(tv2, &error);
1479 if (error)
1480 {
1481 clear_tv(tv1);
1482 return FAIL;
1483 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001484 switch (type)
1485 {
1486 case EXPR_IS:
1487 case EXPR_EQUAL: n1 = (n1 == n2); break;
1488 case EXPR_ISNOT:
1489 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1490 case EXPR_GREATER: n1 = (n1 > n2); break;
1491 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1492 case EXPR_SMALLER: n1 = (n1 < n2); break;
1493 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1494 case EXPR_UNKNOWN:
1495 case EXPR_MATCH:
1496 default: break; // avoid gcc warning
1497 }
1498 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001499 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1500 || tv2->v_type == VAR_BOOL
1501 || (tv1->v_type == VAR_SPECIAL
1502 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001503 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001504 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001505 {
1506 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001507 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1508 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001509 return FAIL;
1510 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001511 n1 = tv1->vval.v_number;
1512 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001513 switch (type)
1514 {
1515 case EXPR_IS:
1516 case EXPR_EQUAL: n1 = (n1 == n2); break;
1517 case EXPR_ISNOT:
1518 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1519 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001520 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001521 vartype_name(tv1->v_type));
1522 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001523 return FAIL;
1524 }
1525 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001526#ifdef FEAT_JOB_CHANNEL
1527 else if (tv1->v_type == tv2->v_type
1528 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1529 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1530 {
1531 if (tv1->v_type == VAR_CHANNEL)
1532 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1533 else
1534 n1 = tv1->vval.v_job == tv2->vval.v_job;
1535 if (type == EXPR_NEQUAL)
1536 n1 = !n1;
1537 }
1538#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001539 else
1540 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001541 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001542 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001543 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001544 return FAIL;
1545 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001546 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001547 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001548 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001549 if (in_vim9script())
1550 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001551 tv1->v_type = VAR_BOOL;
1552 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001553 }
1554 else
1555 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001556 tv1->v_type = VAR_NUMBER;
1557 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001558 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001559
1560 return OK;
1561}
1562
Bram Moolenaar34453202021-01-31 13:08:38 +01001563/*
dundargocc57b5bc2022-11-02 13:30:51 +00001564 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001565 * Put the result, false or true, in "res".
1566 * Return FAIL and give an error message when the comparison can't be done.
1567 */
1568 int
1569typval_compare_list(
1570 typval_T *tv1,
1571 typval_T *tv2,
1572 exprtype_T type,
1573 int ic,
1574 int *res)
1575{
1576 int val = 0;
1577
1578 if (type == EXPR_IS || type == EXPR_ISNOT)
1579 {
1580 val = (tv1->v_type == tv2->v_type
1581 && tv1->vval.v_list == tv2->vval.v_list);
1582 if (type == EXPR_ISNOT)
1583 val = !val;
1584 }
1585 else if (tv1->v_type != tv2->v_type
1586 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1587 {
1588 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001589 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001590 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001591 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001592 return FAIL;
1593 }
1594 else
1595 {
1596 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1597 ic, FALSE);
1598 if (type == EXPR_NEQUAL)
1599 val = !val;
1600 }
1601 *res = val;
1602 return OK;
1603}
1604
1605/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001606 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001607 */
1608 int
1609typval_compare_null(typval_T *tv1, typval_T *tv2)
1610{
1611 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1612 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1613 {
1614 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1615
1616 switch (tv->v_type)
1617 {
1618 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001619#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001620 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001621#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001622 // TODO: null_class handling
1623 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001624 case VAR_DICT: return tv->vval.v_dict == NULL;
1625 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001626#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001627 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001628#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001629 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001630 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001631 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1632 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001633
1634 case VAR_NUMBER: if (!in_vim9script())
1635 return tv->vval.v_number == 0;
1636 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001637 case VAR_FLOAT: if (!in_vim9script())
1638 return tv->vval.v_float == 0.0;
1639 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001640 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001641 default: break;
1642 }
1643 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001644 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001645 // we won't give an error
1646 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001647}
1648
1649/*
dundargocc57b5bc2022-11-02 13:30:51 +00001650 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001651 * Put the result, false or true, in "res".
1652 * Return FAIL and give an error message when the comparison can't be done.
1653 */
1654 int
1655typval_compare_blob(
1656 typval_T *tv1,
1657 typval_T *tv2,
1658 exprtype_T type,
1659 int *res)
1660{
1661 int val = 0;
1662
1663 if (type == EXPR_IS || type == EXPR_ISNOT)
1664 {
1665 val = (tv1->v_type == tv2->v_type
1666 && tv1->vval.v_blob == tv2->vval.v_blob);
1667 if (type == EXPR_ISNOT)
1668 val = !val;
1669 }
1670 else if (tv1->v_type != tv2->v_type
1671 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1672 {
1673 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001674 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001675 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001676 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001677 return FAIL;
1678 }
1679 else
1680 {
1681 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1682 if (type == EXPR_NEQUAL)
1683 val = !val;
1684 }
1685 *res = val;
1686 return OK;
1687}
1688
1689/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001690 * Compare "tv1" to "tv2" as classes according to "type".
1691 * Put the result, false or true, in "res".
1692 * Return FAIL and give an error message when the comparison can't be done.
1693 */
1694 int
1695typval_compare_class(
1696 typval_T *tv1,
1697 typval_T *tv2,
1698 exprtype_T type UNUSED,
1699 int ic UNUSED,
1700 int *res)
1701{
1702 // TODO: use "type"
1703 *res = tv1->vval.v_class == tv2->vval.v_class;
1704 return OK;
1705}
1706
1707/*
1708 * Compare "tv1" to "tv2" as objects according to "type".
1709 * Put the result, false or true, in "res".
1710 * Return FAIL and give an error message when the comparison can't be done.
1711 */
1712 int
1713typval_compare_object(
1714 typval_T *tv1,
1715 typval_T *tv2,
1716 exprtype_T type,
1717 int ic,
1718 int *res)
1719{
1720 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1721
1722 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1723 {
1724 *res = res_match;
1725 return OK;
1726 }
1727 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1728 {
1729 *res = !res_match;
1730 return OK;
1731 }
1732
1733 class_T *cl1 = tv1->vval.v_object->obj_class;
1734 class_T *cl2 = tv2->vval.v_object->obj_class;
1735 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1736 {
1737 *res = !res_match;
1738 return OK;
1739 }
1740
1741 object_T *obj1 = tv1->vval.v_object;
1742 object_T *obj2 = tv2->vval.v_object;
1743 if (type == EXPR_IS || type == EXPR_ISNOT)
1744 {
1745 *res = obj1 == obj2 ? res_match : !res_match;
1746 return OK;
1747 }
1748
1749 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1750 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1751 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1752 {
1753 *res = !res_match;
1754 return OK;
1755 }
1756 *res = res_match;
1757 return OK;
1758}
1759
1760/*
dundargocc57b5bc2022-11-02 13:30:51 +00001761 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001762 * Put the result, false or true, in "res".
1763 * Return FAIL and give an error message when the comparison can't be done.
1764 */
1765 int
1766typval_compare_dict(
1767 typval_T *tv1,
1768 typval_T *tv2,
1769 exprtype_T type,
1770 int ic,
1771 int *res)
1772{
1773 int val;
1774
1775 if (type == EXPR_IS || type == EXPR_ISNOT)
1776 {
1777 val = (tv1->v_type == tv2->v_type
1778 && tv1->vval.v_dict == tv2->vval.v_dict);
1779 if (type == EXPR_ISNOT)
1780 val = !val;
1781 }
1782 else if (tv1->v_type != tv2->v_type
1783 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1784 {
1785 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001786 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001787 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001788 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001789 return FAIL;
1790 }
1791 else
1792 {
1793 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1794 if (type == EXPR_NEQUAL)
1795 val = !val;
1796 }
1797 *res = val;
1798 return OK;
1799}
1800
1801/*
dundargocc57b5bc2022-11-02 13:30:51 +00001802 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001803 * Put the result, false or true, in "res".
1804 * Return FAIL and give an error message when the comparison can't be done.
1805 */
1806 int
1807typval_compare_func(
1808 typval_T *tv1,
1809 typval_T *tv2,
1810 exprtype_T type,
1811 int ic,
1812 int *res)
1813{
1814 int val = 0;
1815
1816 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1817 && type != EXPR_IS && type != EXPR_ISNOT)
1818 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001819 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001820 return FAIL;
1821 }
1822 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1823 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1824 // When both partials are NULL, then they are equal.
1825 // Otherwise they are not equal.
1826 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1827 else if (type == EXPR_IS || type == EXPR_ISNOT)
1828 {
1829 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1830 // strings are considered the same if their value is
1831 // the same
1832 val = tv_equal(tv1, tv2, ic, FALSE);
1833 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1834 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1835 else
1836 val = FALSE;
1837 }
1838 else
1839 val = tv_equal(tv1, tv2, ic, FALSE);
1840 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1841 val = !val;
1842 *res = val;
1843 return OK;
1844}
1845
1846/*
1847 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1848 * Put the result, false or true, in "res".
1849 * Return FAIL and give an error message when the comparison can't be done.
1850 */
1851 int
1852typval_compare_string(
1853 typval_T *tv1,
1854 typval_T *tv2,
1855 exprtype_T type,
1856 int ic,
1857 int *res)
1858{
1859 int i = 0;
1860 int val = FALSE;
1861 char_u *s1, *s2;
1862 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1863
1864 if (in_vim9script()
1865 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1866 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1867 {
1868 semsg(_(e_cannot_compare_str_with_str),
1869 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1870 return FAIL;
1871 }
1872 s1 = tv_get_string_buf(tv1, buf1);
1873 s2 = tv_get_string_buf(tv2, buf2);
1874 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1875 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1876 switch (type)
1877 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001878 case EXPR_IS: if (in_vim9script())
1879 {
1880 // Really check it is the same string, not just
1881 // the same value.
1882 val = tv1->vval.v_string == tv2->vval.v_string;
1883 break;
1884 }
1885 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001886 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001887 case EXPR_ISNOT: if (in_vim9script())
1888 {
1889 // Really check it is not the same string, not
1890 // just a different value.
1891 val = tv1->vval.v_string != tv2->vval.v_string;
1892 break;
1893 }
1894 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001895 case EXPR_NEQUAL: val = (i != 0); break;
1896 case EXPR_GREATER: val = (i > 0); break;
1897 case EXPR_GEQUAL: val = (i >= 0); break;
1898 case EXPR_SMALLER: val = (i < 0); break;
1899 case EXPR_SEQUAL: val = (i <= 0); break;
1900
1901 case EXPR_MATCH:
1902 case EXPR_NOMATCH:
1903 val = pattern_match(s2, s1, ic);
1904 if (type == EXPR_NOMATCH)
1905 val = !val;
1906 break;
1907
1908 default: break; // avoid gcc warning
1909 }
1910 *res = val;
1911 return OK;
1912}
1913/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001914 * Convert any type to a string, never give an error.
1915 * When "quotes" is TRUE add quotes to a string.
1916 * Returns an allocated string.
1917 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001918 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001919typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001920{
1921 char_u *tofree;
1922 char_u numbuf[NUMBUFLEN];
1923 char_u *ret = NULL;
1924
1925 if (arg == NULL)
1926 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001927 if (!quotes && arg->v_type == VAR_STRING)
1928 {
1929 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1930 : arg->vval.v_string);
1931 }
1932 else
1933 {
1934 ret = tv2string(arg, &tofree, numbuf, 0);
1935 // Make a copy if we have a value but it's not in allocated memory.
1936 if (ret != NULL && tofree == NULL)
1937 ret = vim_strsave(ret);
1938 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001939 return ret;
1940}
1941
1942/*
1943 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1944 * or it refers to a List or Dictionary that is locked.
1945 */
1946 int
1947tv_islocked(typval_T *tv)
1948{
1949 return (tv->v_lock & VAR_LOCKED)
1950 || (tv->v_type == VAR_LIST
1951 && tv->vval.v_list != NULL
1952 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1953 || (tv->v_type == VAR_DICT
1954 && tv->vval.v_dict != NULL
1955 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1956}
1957
1958 static int
1959func_equal(
1960 typval_T *tv1,
1961 typval_T *tv2,
1962 int ic) // ignore case
1963{
1964 char_u *s1, *s2;
1965 dict_T *d1, *d2;
1966 int a1, a2;
1967 int i;
1968
1969 // empty and NULL function name considered the same
1970 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1971 : partial_name(tv1->vval.v_partial);
1972 if (s1 != NULL && *s1 == NUL)
1973 s1 = NULL;
1974 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1975 : partial_name(tv2->vval.v_partial);
1976 if (s2 != NULL && *s2 == NUL)
1977 s2 = NULL;
1978 if (s1 == NULL || s2 == NULL)
1979 {
1980 if (s1 != s2)
1981 return FALSE;
1982 }
1983 else if (STRCMP(s1, s2) != 0)
1984 return FALSE;
1985
1986 // empty dict and NULL dict is different
1987 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1988 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1989 if (d1 == NULL || d2 == NULL)
1990 {
1991 if (d1 != d2)
1992 return FALSE;
1993 }
1994 else if (!dict_equal(d1, d2, ic, TRUE))
1995 return FALSE;
1996
1997 // empty list and no list considered the same
1998 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1999 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
2000 if (a1 != a2)
2001 return FALSE;
2002 for (i = 0; i < a1; ++i)
2003 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
2004 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
2005 return FALSE;
2006
2007 return TRUE;
2008}
2009
2010/*
2011 * Return TRUE if "tv1" and "tv2" have the same value.
2012 * Compares the items just like "==" would compare them, but strings and
2013 * numbers are different. Floats and numbers are also different.
2014 */
2015 int
2016tv_equal(
2017 typval_T *tv1,
2018 typval_T *tv2,
2019 int ic, // ignore case
2020 int recursive) // TRUE when used recursively
2021{
2022 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2023 char_u *s1, *s2;
2024 static int recursive_cnt = 0; // catch recursive loops
2025 int r;
2026 static int tv_equal_recurse_limit;
2027
2028 // Catch lists and dicts that have an endless loop by limiting
2029 // recursiveness to a limit. We guess they are equal then.
2030 // A fixed limit has the problem of still taking an awful long time.
2031 // Reduce the limit every time running into it. That should work fine for
2032 // deeply linked structures that are not recursively linked and catch
2033 // recursiveness quickly.
2034 if (!recursive)
2035 tv_equal_recurse_limit = 1000;
2036 if (recursive_cnt >= tv_equal_recurse_limit)
2037 {
2038 --tv_equal_recurse_limit;
2039 return TRUE;
2040 }
2041
2042 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2043 // arguments.
2044 if ((tv1->v_type == VAR_FUNC
2045 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2046 && (tv2->v_type == VAR_FUNC
2047 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2048 {
2049 ++recursive_cnt;
2050 r = func_equal(tv1, tv2, ic);
2051 --recursive_cnt;
2052 return r;
2053 }
2054
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002055 if (tv1->v_type != tv2->v_type
2056 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2057 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002058 return FALSE;
2059
2060 switch (tv1->v_type)
2061 {
2062 case VAR_LIST:
2063 ++recursive_cnt;
2064 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2065 --recursive_cnt;
2066 return r;
2067
2068 case VAR_DICT:
2069 ++recursive_cnt;
2070 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2071 --recursive_cnt;
2072 return r;
2073
2074 case VAR_BLOB:
2075 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2076
2077 case VAR_NUMBER:
2078 case VAR_BOOL:
2079 case VAR_SPECIAL:
2080 return tv1->vval.v_number == tv2->vval.v_number;
2081
2082 case VAR_STRING:
2083 s1 = tv_get_string_buf(tv1, buf1);
2084 s2 = tv_get_string_buf(tv2, buf2);
2085 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2086
2087 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002088 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002089 case VAR_JOB:
2090#ifdef FEAT_JOB_CHANNEL
2091 return tv1->vval.v_job == tv2->vval.v_job;
2092#endif
2093 case VAR_CHANNEL:
2094#ifdef FEAT_JOB_CHANNEL
2095 return tv1->vval.v_channel == tv2->vval.v_channel;
2096#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002097 case VAR_INSTR:
2098 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002099
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002100 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002101 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002102 return tv1->vval.v_class == tv2->vval.v_class;
2103
2104 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002105 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2106 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002107
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002108 case VAR_PARTIAL:
2109 return tv1->vval.v_partial == tv2->vval.v_partial;
2110
2111 case VAR_FUNC:
2112 return tv1->vval.v_string == tv2->vval.v_string;
2113
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002114 case VAR_TYPEALIAS:
2115 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2116
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002117 case VAR_UNKNOWN:
2118 case VAR_ANY:
2119 case VAR_VOID:
2120 break;
2121 }
2122
2123 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2124 // does not equal anything, not even itself.
2125 return FALSE;
2126}
2127
2128/*
2129 * Get an option value.
2130 * "arg" points to the '&' or '+' before the option name.
2131 * "arg" is advanced to character after the option name.
2132 * Return OK or FAIL.
2133 */
2134 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002135eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002136 char_u **arg,
2137 typval_T *rettv, // when NULL, only check if option exists
2138 int evaluate)
2139{
2140 char_u *option_end;
2141 long numval;
2142 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002143 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002144 int c;
2145 int working = (**arg == '+'); // has("+option")
2146 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002147 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002148
2149 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002150 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002151 if (option_end == NULL)
2152 {
2153 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002154 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002155 return FAIL;
2156 }
2157
2158 if (!evaluate)
2159 {
2160 *arg = option_end;
2161 return OK;
2162 }
2163
2164 c = *option_end;
2165 *option_end = NUL;
2166 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002167 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002168
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002169 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002170 {
2171 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002172 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002173 ret = FAIL;
2174 }
2175 else if (rettv != NULL)
2176 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002177 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002178 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002179 {
2180 rettv->v_type = VAR_STRING;
2181 rettv->vval.v_string = NULL;
2182 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002183 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002184 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002185 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2186 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002187 rettv->vval.v_number = 0;
2188 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002189 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002190 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002191 if (in_vim9script() && opt_type == gov_bool)
2192 {
2193 rettv->v_type = VAR_BOOL;
2194 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2195 }
2196 else
2197 {
2198 rettv->v_type = VAR_NUMBER;
2199 rettv->vval.v_number = numval;
2200 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002201 }
2202 else // string option
2203 {
2204 rettv->v_type = VAR_STRING;
2205 rettv->vval.v_string = stringval;
2206 }
2207 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002208 else if (working && (opt_type == gov_hidden_bool
2209 || opt_type == gov_hidden_number
2210 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002211 ret = FAIL;
2212
2213 *option_end = c; // put back for error messages
2214 *arg = option_end;
2215
2216 return ret;
2217}
2218
2219/*
2220 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2221 * Return OK or FAIL.
2222 */
2223 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002224eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002225 char_u **arg,
2226 typval_T *rettv,
2227 int evaluate,
2228 int want_string UNUSED)
2229{
2230 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002231 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002232 char_u *p;
2233 int get_float = FALSE;
2234
2235 // We accept a float when the format matches
2236 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2237 // strict to avoid backwards compatibility problems.
2238 // With script version 2 and later the leading digit can be
2239 // omitted.
2240 // Don't look for a float after the "." operator, so that
2241 // ":let vers = 1.2.3" doesn't fail.
2242 if (**arg == '.')
2243 p = *arg;
2244 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002245 {
2246 p = *arg + 1;
2247 if (skip_quotes)
2248 for (;;)
2249 {
2250 if (*p == '\'')
2251 ++p;
2252 if (!vim_isdigit(*p))
2253 break;
2254 p = skipdigits(p);
2255 }
2256 else
2257 p = skipdigits(p);
2258 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002259 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2260 {
2261 get_float = TRUE;
2262 p = skipdigits(p + 2);
2263 if (*p == 'e' || *p == 'E')
2264 {
2265 ++p;
2266 if (*p == '-' || *p == '+')
2267 ++p;
2268 if (!vim_isdigit(*p))
2269 get_float = FALSE;
2270 else
2271 p = skipdigits(p + 1);
2272 }
2273 if (ASCII_ISALPHA(*p) || *p == '.')
2274 get_float = FALSE;
2275 }
2276 if (get_float)
2277 {
2278 float_T f;
2279
Bram Moolenaar29500652021-08-08 15:43:34 +02002280 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002281 if (evaluate)
2282 {
2283 rettv->v_type = VAR_FLOAT;
2284 rettv->vval.v_float = f;
2285 }
2286 }
2287 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002288 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2289 {
2290 char_u *bp;
2291 blob_T *blob = NULL; // init for gcc
2292
2293 // Blob constant: 0z0123456789abcdef
2294 if (evaluate)
2295 blob = blob_alloc();
2296 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2297 {
2298 if (!vim_isxdigit(bp[1]))
2299 {
2300 if (blob != NULL)
2301 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002302 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002303 ga_clear(&blob->bv_ga);
2304 VIM_CLEAR(blob);
2305 }
2306 return FAIL;
2307 }
2308 if (blob != NULL)
2309 ga_append(&blob->bv_ga,
2310 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2311 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2312 ++bp;
2313 }
2314 if (blob != NULL)
2315 rettv_blob_set(rettv, blob);
2316 *arg = bp;
2317 }
2318 else
2319 {
2320 varnumber_T n;
2321
2322 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002323 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002324 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002325 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002326 if (len == 0)
2327 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002328 if (evaluate)
2329 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002330 return FAIL;
2331 }
2332 *arg += len;
2333 if (evaluate)
2334 {
2335 rettv->v_type = VAR_NUMBER;
2336 rettv->vval.v_number = n;
2337 }
2338 }
2339 return OK;
2340}
2341
2342/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002343 * Evaluate a string constant and put the result in "rettv".
2344 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2345 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2346 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002347 * Return OK or FAIL.
2348 */
2349 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002350eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002351{
2352 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002353 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002354 int extra = interpolate ? 1 : 0;
2355 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002356 int len;
2357
2358 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002359 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002360 {
2361 if (*p == '\\' && p[1] != NUL)
2362 {
2363 ++p;
2364 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002365 // to 9 characters (6 for the char and 3 for a modifier):
2366 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002367 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002368 {
2369 int modifiers = 0;
2370 int flags = FSK_KEYCODE | FSK_IN_STRING;
2371
zeertzjqdb088872022-05-02 22:53:45 +01002372 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002373
2374 // Skip to the '>' to avoid using '{' inside for string
2375 // interpolation.
2376 if (p[1] != '*')
2377 flags |= FSK_SIMPLIFY;
2378 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2379 --p; // leave "p" on the ">"
2380 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002381 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002382 else if (interpolate && (*p == '{' || *p == '}'))
2383 {
2384 if (*p == '{' && p[1] != '{') // start of expression
2385 break;
2386 ++p;
2387 if (p[-1] == '}' && *p != '}') // single '}' is an error
2388 {
2389 semsg(_(e_stray_closing_curly_str), *arg);
2390 return FAIL;
2391 }
2392 --extra; // "{{" becomes "{", "}}" becomes "}"
2393 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002394 }
2395
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002396 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002397 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002398 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002399 return FAIL;
2400 }
2401
2402 // If only parsing, set *arg and return here
2403 if (!evaluate)
2404 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002405 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002406 return OK;
2407 }
2408
2409 // Copy the string into allocated memory, handling backslashed
2410 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002411 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002412 len = (int)(p - *arg + extra);
2413 rettv->vval.v_string = alloc(len);
2414 if (rettv->vval.v_string == NULL)
2415 return FAIL;
2416 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002417
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002418 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002419 {
2420 if (*p == '\\')
2421 {
2422 switch (*++p)
2423 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002424 case 'b': *end++ = BS; ++p; break;
2425 case 'e': *end++ = ESC; ++p; break;
2426 case 'f': *end++ = FF; ++p; break;
2427 case 'n': *end++ = NL; ++p; break;
2428 case 'r': *end++ = CAR; ++p; break;
2429 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002430
2431 case 'X': // hex: "\x1", "\x12"
2432 case 'x':
2433 case 'u': // Unicode: "\u0023"
2434 case 'U':
2435 if (vim_isxdigit(p[1]))
2436 {
2437 int n, nr;
Keith Thompson184f71c2024-01-04 21:19:04 +01002438 int c = SAFE_toupper(*p);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002439
2440 if (c == 'X')
2441 n = 2;
2442 else if (*p == 'u')
2443 n = 4;
2444 else
2445 n = 8;
2446 nr = 0;
2447 while (--n >= 0 && vim_isxdigit(p[1]))
2448 {
2449 ++p;
2450 nr = (nr << 4) + hex2nr(*p);
2451 }
2452 ++p;
2453 // For "\u" store the number according to
2454 // 'encoding'.
2455 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002456 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002457 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002458 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002459 }
2460 break;
2461
2462 // octal: "\1", "\12", "\123"
2463 case '0':
2464 case '1':
2465 case '2':
2466 case '3':
2467 case '4':
2468 case '5':
2469 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002470 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002471 if (*p >= '0' && *p <= '7')
2472 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002473 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002474 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002475 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002476 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002477 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002478 break;
2479
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002480 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002481 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002482 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002483 int flags = FSK_KEYCODE | FSK_IN_STRING;
2484
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002485 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002486 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002487 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002488 if (extra != 0)
2489 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002490 end += extra;
2491 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002492 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002493 break;
2494 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002495 }
2496 // FALLTHROUGH
2497
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002498 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002499 break;
2500 }
2501 }
2502 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002503 {
2504 if (interpolate && (*p == '{' || *p == '}'))
2505 {
2506 if (*p == '{' && p[1] != '{') // start of expression
2507 break;
2508 ++p; // reduce "{{" to "{" and "}}" to "}"
2509 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002510 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002511 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002512 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002513 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002514 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002515 ++p;
2516 *arg = p;
2517
2518 return OK;
2519}
2520
2521/*
2522 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002523 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2524 * Return OK when a "rettv" was set to the string.
2525 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002526 */
2527 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002528eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002529{
2530 char_u *p;
2531 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002532 int reduce = interpolate ? -1 : 0;
2533 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002534
2535 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002536 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002537 {
2538 if (*p == '\'')
2539 {
2540 if (p[1] != '\'')
2541 break;
2542 ++reduce;
2543 ++p;
2544 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002545 else if (interpolate)
2546 {
2547 if (*p == '{')
2548 {
2549 if (p[1] != '{')
2550 break;
2551 ++p;
2552 ++reduce;
2553 }
2554 else if (*p == '}')
2555 {
2556 ++p;
2557 if (*p != '}')
2558 {
2559 semsg(_(e_stray_closing_curly_str), *arg);
2560 return FAIL;
2561 }
2562 ++reduce;
2563 }
2564 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002565 }
2566
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002567 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002568 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002569 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002570 return FAIL;
2571 }
2572
2573 // If only parsing return after setting "*arg"
2574 if (!evaluate)
2575 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002576 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002577 return OK;
2578 }
2579
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002580 // Copy the string into allocated memory, handling '' to ' reduction and
2581 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002582 str = alloc((p - *arg) - reduce);
2583 if (str == NULL)
2584 return FAIL;
2585 rettv->v_type = VAR_STRING;
2586 rettv->vval.v_string = str;
2587
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002588 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002589 {
2590 if (*p == '\'')
2591 {
2592 if (p[1] != '\'')
2593 break;
2594 ++p;
2595 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002596 else if (interpolate && (*p == '{' || *p == '}'))
2597 {
2598 if (*p == '{' && p[1] != '{')
2599 break;
2600 ++p;
2601 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002602 MB_COPY_CHAR(p, str);
2603 }
2604 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002605 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002606
2607 return OK;
2608}
2609
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002610/*
2611 * Evaluate a single or double quoted string possibly containing expressions.
2612 * "arg" points to the '$'. The result is put in "rettv".
2613 * Returns OK or FAIL.
2614 */
LemonBoy2eaef102022-05-06 13:14:50 +01002615 int
2616eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2617{
2618 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002619 int ret = OK;
2620 int quote;
2621 garray_T ga;
2622 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002623
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002624 ga_init2(&ga, 1, 80);
2625
2626 // *arg is on the '$' character, move it to the first string character.
2627 ++*arg;
2628 quote = **arg;
2629 ++*arg;
2630
2631 for (;;)
2632 {
2633 // Get the string up to the matching quote or to a single '{'.
2634 // "arg" is advanced to either the quote or the '{'.
2635 if (quote == '"')
2636 ret = eval_string(arg, &tv, evaluate, TRUE);
2637 else
2638 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2639 if (ret == FAIL)
2640 break;
2641 if (evaluate)
2642 {
2643 ga_concat(&ga, tv.vval.v_string);
2644 clear_tv(&tv);
2645 }
2646
2647 if (**arg != '{')
2648 {
2649 // found terminating quote
2650 ++*arg;
2651 break;
2652 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002653 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002654 if (p == NULL)
2655 {
2656 ret = FAIL;
2657 break;
2658 }
2659 *arg = p;
2660 }
LemonBoy2eaef102022-05-06 13:14:50 +01002661
2662 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002663 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2664 {
2665 ga_clear(&ga);
2666 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002667 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002668 }
LemonBoy2eaef102022-05-06 13:14:50 +01002669
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002670 rettv->vval.v_string = ga.ga_data;
2671 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002672}
2673
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002674/*
2675 * Return a string with the string representation of a variable.
2676 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2677 * "numbuf" is used for a number.
2678 * Puts quotes around strings, so that they can be parsed back by eval().
2679 * May return NULL.
2680 */
2681 char_u *
2682tv2string(
2683 typval_T *tv,
2684 char_u **tofree,
2685 char_u *numbuf,
2686 int copyID)
2687{
2688 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2689}
2690
2691/*
2692 * Get the value of an environment variable.
2693 * "arg" is pointing to the '$'. It is advanced to after the name.
2694 * If the environment variable was not set, silently assume it is empty.
2695 * Return FAIL if the name is invalid.
2696 */
2697 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002698eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002699{
2700 char_u *string = NULL;
2701 int len;
2702 int cc;
2703 char_u *name;
2704 int mustfree = FALSE;
2705
2706 ++*arg;
2707 name = *arg;
2708 len = get_env_len(arg);
2709 if (evaluate)
2710 {
2711 if (len == 0)
2712 return FAIL; // invalid empty name
2713
2714 cc = name[len];
2715 name[len] = NUL;
2716 // first try vim_getenv(), fast for normal environment vars
2717 string = vim_getenv(name, &mustfree);
2718 if (string != NULL && *string != NUL)
2719 {
2720 if (!mustfree)
2721 string = vim_strsave(string);
2722 }
2723 else
2724 {
2725 if (mustfree)
2726 vim_free(string);
2727
2728 // next try expanding things like $VIM and ${HOME}
2729 string = expand_env_save(name - 1);
2730 if (string != NULL && *string == '$')
2731 VIM_CLEAR(string);
2732 }
2733 name[len] = cc;
2734
2735 rettv->v_type = VAR_STRING;
2736 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002737 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002738 }
2739
2740 return OK;
2741}
2742
2743/*
2744 * Get the lnum from the first argument.
2745 * Also accepts ".", "$", etc., but that only works for the current buffer.
2746 * Returns -1 on error.
2747 */
2748 linenr_T
2749tv_get_lnum(typval_T *argvars)
2750{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002751 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002752 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002753
Bram Moolenaar56acb092020-08-16 14:48:19 +02002754 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2755 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002756 if (lnum <= 0 && did_emsg_before == did_emsg
2757 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002758 {
2759 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002760 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002761
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002762 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002763 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002764 if (fp != NULL)
2765 lnum = fp->lnum;
2766 }
2767 return lnum;
2768}
2769
2770/*
2771 * Get the lnum from the first argument.
2772 * Also accepts "$", then "buf" is used.
2773 * Returns 0 on error.
2774 */
2775 linenr_T
2776tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2777{
2778 if (argvars[0].v_type == VAR_STRING
2779 && argvars[0].vval.v_string != NULL
2780 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002781 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002782 && buf != NULL)
2783 return buf->b_ml.ml_line_count;
2784 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2785}
2786
2787/*
2788 * Get buffer by number or pattern.
2789 */
2790 buf_T *
2791tv_get_buf(typval_T *tv, int curtab_only)
2792{
2793 char_u *name = tv->vval.v_string;
2794 buf_T *buf;
2795
2796 if (tv->v_type == VAR_NUMBER)
2797 return buflist_findnr((int)tv->vval.v_number);
2798 if (tv->v_type != VAR_STRING)
2799 return NULL;
2800 if (name == NULL || *name == NUL)
2801 return curbuf;
2802 if (name[0] == '$' && name[1] == NUL)
2803 return lastbuf;
2804
2805 buf = buflist_find_by_name(name, curtab_only);
2806
2807 // If not found, try expanding the name, like done for bufexists().
2808 if (buf == NULL)
2809 buf = find_buffer(tv);
2810
2811 return buf;
2812}
2813
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002814/*
2815 * Like tv_get_buf() but give an error message is the type is wrong.
2816 */
2817 buf_T *
2818tv_get_buf_from_arg(typval_T *tv)
2819{
2820 buf_T *buf;
2821
2822 ++emsg_off;
2823 buf = tv_get_buf(tv, FALSE);
2824 --emsg_off;
2825 if (buf == NULL
2826 && tv->v_type != VAR_NUMBER
2827 && tv->v_type != VAR_STRING)
2828 // issue errmsg for type error
2829 (void)tv_get_number(tv);
2830 return buf;
2831}
2832
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002833#endif // FEAT_EVAL