blob: af96b3170b64803e74f4b3c6f566c7af0c392af8 [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
Dominique Pelle748b3082022-01-08 12:41:16 +0000626#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200627/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200628 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
629 */
630 int
631check_for_chan_or_job_arg(typval_T *args, int idx)
632{
633 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
634 {
rbtnnddc80af2021-12-17 18:01:31 +0000635 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200636 return FAIL;
637 }
638 return OK;
639}
640
641/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200642 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
643 * job.
644 */
645 int
646check_for_opt_chan_or_job_arg(typval_T *args, int idx)
647{
648 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100649 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200650}
651
652/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200653 * Give an error and return FAIL unless "args[idx]" is a job.
654 */
655 int
656check_for_job_arg(typval_T *args, int idx)
657{
658 if (args[idx].v_type != VAR_JOB)
659 {
rbtnnddc80af2021-12-17 18:01:31 +0000660 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200661 return FAIL;
662 }
663 return OK;
664}
665
666/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200667 * Check for an optional job argument at 'idx'.
668 */
669 int
670check_for_opt_job_arg(typval_T *args, int idx)
671{
672 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100673 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200674}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000675#else
676/*
677 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
678 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
679 */
680 int
681check_for_opt_chan_or_job_arg(typval_T *args, int idx)
682{
683 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
684}
Dominique Pelle748b3082022-01-08 12:41:16 +0000685#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200686
687/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200688 * Give an error and return FAIL unless "args[idx]" is a string or
689 * a number.
690 */
691 int
692check_for_string_or_number_arg(typval_T *args, int idx)
693{
694 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
695 {
rbtnnddc80af2021-12-17 18:01:31 +0000696 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200697 return FAIL;
698 }
699 return OK;
700}
701
702/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200703 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200704 */
705 int
706check_for_opt_string_or_number_arg(typval_T *args, int idx)
707{
708 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100709 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200710}
711
712/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200713 * Give an error and return FAIL unless "args[idx]" is a buffer number.
714 * Buffer number can be a number or a string.
715 */
716 int
717check_for_buffer_arg(typval_T *args, int idx)
718{
719 return check_for_string_or_number_arg(args, idx);
720}
721
722/*
723 * Check for an optional buffer argument at 'idx'
724 */
725 int
726check_for_opt_buffer_arg(typval_T *args, int idx)
727{
728 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100729 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200730}
731
732/*
733 * Give an error and return FAIL unless "args[idx]" is a line number.
734 * Line number can be a number or a string.
735 */
736 int
737check_for_lnum_arg(typval_T *args, int idx)
738{
739 return check_for_string_or_number_arg(args, idx);
740}
741
742/*
743 * Check for an optional line number argument at 'idx'
744 */
745 int
746check_for_opt_lnum_arg(typval_T *args, int idx)
747{
748 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100749 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200750}
751
Dominique Pelle748b3082022-01-08 12:41:16 +0000752#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200753/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200754 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200755 */
756 int
757check_for_string_or_blob_arg(typval_T *args, int idx)
758{
759 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
760 {
rbtnnddc80af2021-12-17 18:01:31 +0000761 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200762 return FAIL;
763 }
764 return OK;
765}
Dominique Pelle748b3082022-01-08 12:41:16 +0000766#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200767
768/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200769 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200770 */
771 int
772check_for_string_or_list_arg(typval_T *args, int idx)
773{
774 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
775 {
rbtnnddc80af2021-12-17 18:01:31 +0000776 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200777 return FAIL;
778 }
779 return OK;
780}
781
782/*
rbtnn0ccb5842021-12-18 18:33:46 +0000783 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
784 * blob.
785 */
786 int
787check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
788{
789 if (args[idx].v_type != VAR_STRING
790 && args[idx].v_type != VAR_LIST
791 && args[idx].v_type != VAR_BLOB)
792 {
793 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
794 return FAIL;
795 }
796 return OK;
797}
798
799/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200800 * Check for an optional string or list argument at 'idx'
801 */
802 int
803check_for_opt_string_or_list_arg(typval_T *args, int idx)
804{
805 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100806 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200807}
808
809/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200810 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
811 */
812 int
813check_for_string_or_dict_arg(typval_T *args, int idx)
814{
815 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
816 {
rbtnnddc80af2021-12-17 18:01:31 +0000817 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200818 return FAIL;
819 }
820 return OK;
821}
822
823/*
824 * Give an error and return FAIL unless "args[idx]" is a string or a number
825 * or a list.
826 */
827 int
828check_for_string_or_number_or_list_arg(typval_T *args, int idx)
829{
830 if (args[idx].v_type != VAR_STRING
831 && args[idx].v_type != VAR_NUMBER
832 && args[idx].v_type != VAR_LIST)
833 {
rbtnn0ccb5842021-12-18 18:33:46 +0000834 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200835 return FAIL;
836 }
837 return OK;
838}
839
840/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200841 * Give an error and return FAIL unless "args[idx]" is an optional string
842 * or number or a list
843 */
844 int
845check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
846{
847 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100848 || check_for_string_or_number_or_list_arg(args, idx)
849 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200850}
851
852/*
Bakudankun375141e2022-09-09 18:46:47 +0100853 * Give an error and return FAIL unless "args[idx]" is a string or a number
854 * or a list or a blob.
855 */
856 int
857check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
858{
859 if (args[idx].v_type != VAR_STRING
860 && args[idx].v_type != VAR_NUMBER
861 && args[idx].v_type != VAR_LIST
862 && args[idx].v_type != VAR_BLOB)
863 {
864 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
865 return FAIL;
866 }
867 return OK;
868}
869
870/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200871 * Give an error and return FAIL unless "args[idx]" is a string or a list
872 * or a dict.
873 */
874 int
875check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
876{
877 if (args[idx].v_type != VAR_STRING
878 && args[idx].v_type != VAR_LIST
879 && args[idx].v_type != VAR_DICT)
880 {
rbtnnddc80af2021-12-17 18:01:31 +0000881 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200882 return FAIL;
883 }
884 return OK;
885}
886
887/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000888 * Give an error and return FAIL unless "args[idx]" is a string
889 * or a function reference.
890 */
891 int
892check_for_string_or_func_arg(typval_T *args, int idx)
893{
894 if (args[idx].v_type != VAR_PARTIAL
895 && args[idx].v_type != VAR_FUNC
896 && args[idx].v_type != VAR_STRING)
897 {
898 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
899 return FAIL;
900 }
901 return OK;
902}
903
904/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200905 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
906 */
907 int
908check_for_list_or_blob_arg(typval_T *args, int idx)
909{
910 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
911 {
rbtnn0ccb5842021-12-18 18:33:46 +0000912 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200913 return FAIL;
914 }
915 return OK;
916}
917
918/*
919 * Give an error and return FAIL unless "args[idx]" is a list or dict
920 */
921 int
922check_for_list_or_dict_arg(typval_T *args, int idx)
923{
924 if (args[idx].v_type != VAR_LIST
925 && args[idx].v_type != VAR_DICT)
926 {
rbtnnddc80af2021-12-17 18:01:31 +0000927 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200928 return FAIL;
929 }
930 return OK;
931}
932
933/*
934 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
935 * blob.
936 */
937 int
938check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
939{
940 if (args[idx].v_type != VAR_LIST
941 && args[idx].v_type != VAR_DICT
942 && args[idx].v_type != VAR_BLOB)
943 {
rbtnnddc80af2021-12-17 18:01:31 +0000944 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200945 return FAIL;
946 }
947 return OK;
948}
949
950/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000951 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
952 * blob or a string.
953 */
954 int
955check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
956{
957 if (args[idx].v_type != VAR_LIST
958 && args[idx].v_type != VAR_DICT
959 && args[idx].v_type != VAR_BLOB
960 && args[idx].v_type != VAR_STRING)
961 {
rbtnnddc80af2021-12-17 18:01:31 +0000962 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000963 return FAIL;
964 }
965 return OK;
966}
967
968/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200969 * Give an error and return FAIL unless "args[idx]" is an optional buffer
970 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200971 */
972 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200973check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200974{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200975 if (args[idx].v_type != VAR_UNKNOWN
976 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200977 && args[idx].v_type != VAR_NUMBER
978 && args[idx].v_type != VAR_DICT)
979 {
rbtnnddc80af2021-12-17 18:01:31 +0000980 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200981 return FAIL;
982 }
983 return OK;
984}
985
986/*
LemonBoyafe04662023-08-23 21:08:11 +0200987 * Give an error and return FAIL unless "args[idx]" is an object.
988 */
989 int
990check_for_object_arg(typval_T *args, int idx)
991{
992 if (args[idx].v_type != VAR_OBJECT)
993 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200994 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +0200995 return FAIL;
996 }
997 return OK;
998}
999
1000/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001001 * Returns TRUE if "tv" is a type alias for a class
1002 */
1003 int
1004tv_class_alias(typval_T *tv)
1005{
1006 return tv->v_type == VAR_TYPEALIAS &&
1007 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1008}
1009
1010/*
Ernie Rael2025af12023-12-12 16:58:00 +01001011 * Give an error and return FAIL unless "args[idx]" is a class
1012 * or class typealias.
LemonBoyafe04662023-08-23 21:08:11 +02001013 */
1014 int
Ernie Rael2025af12023-12-12 16:58:00 +01001015check_for_class_or_typealias_args(typval_T *args, int idx)
LemonBoyafe04662023-08-23 21:08:11 +02001016{
Ernie Rael2025af12023-12-12 16:58:00 +01001017 for (int i = idx; args[i].v_type != VAR_UNKNOWN; ++i)
LemonBoyafe04662023-08-23 21:08:11 +02001018 {
Ernie Rael2025af12023-12-12 16:58:00 +01001019 if (args[i].v_type != VAR_CLASS && !tv_class_alias(&args[idx]))
1020 {
1021 semsg(_(e_class_or_typealias_required_for_argument_nr), i + 1);
1022 return FAIL;
1023 }
LemonBoyafe04662023-08-23 21:08:11 +02001024 }
1025 return OK;
1026}
1027
1028/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001029 * Get the string value of a variable.
1030 * If it is a Number variable, the number is converted into a string.
1031 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1032 * tv_get_string_buf() uses a given buffer.
1033 * If the String variable has never been set, return an empty string.
1034 * Never returns NULL;
1035 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1036 * NULL on error.
1037 */
1038 char_u *
1039tv_get_string(typval_T *varp)
1040{
1041 static char_u mybuf[NUMBUFLEN];
1042
1043 return tv_get_string_buf(varp, mybuf);
1044}
1045
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001046/*
1047 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1048 */
1049 char_u *
1050tv_get_string_strict(typval_T *varp)
1051{
1052 static char_u mybuf[NUMBUFLEN];
1053 char_u *res = tv_get_string_buf_chk_strict(
1054 varp, mybuf, in_vim9script());
1055
1056 return res != NULL ? res : (char_u *)"";
1057}
1058
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001059 char_u *
1060tv_get_string_buf(typval_T *varp, char_u *buf)
1061{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001062 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001063
1064 return res != NULL ? res : (char_u *)"";
1065}
1066
1067/*
1068 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1069 */
1070 char_u *
1071tv_get_string_chk(typval_T *varp)
1072{
1073 static char_u mybuf[NUMBUFLEN];
1074
1075 return tv_get_string_buf_chk(varp, mybuf);
1076}
1077
1078 char_u *
1079tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1080{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001081 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1082}
1083
1084 char_u *
1085tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1086{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001087 switch (varp->v_type)
1088 {
1089 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001090 if (strict)
1091 {
1092 emsg(_(e_using_number_as_string));
1093 break;
1094 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001095 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1096 (varnumber_T)varp->vval.v_number);
1097 return buf;
1098 case VAR_FUNC:
1099 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001100 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001101 break;
1102 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001103 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001104 break;
1105 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001106 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001107 break;
1108 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001109 if (strict)
1110 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001111 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001112 break;
1113 }
1114 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1115 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001116 case VAR_STRING:
1117 if (varp->vval.v_string != NULL)
1118 return varp->vval.v_string;
1119 return (char_u *)"";
1120 case VAR_BOOL:
1121 case VAR_SPECIAL:
1122 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1123 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001124 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001125 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001126 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001127 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +01001128 case VAR_TYPEALIAS:
1129 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001130 break;
1131 case VAR_OBJECT:
1132 emsg(_(e_using_object_as_string));
1133 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001134 case VAR_JOB:
1135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001136 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001137 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001138 semsg(_(e_using_invalid_value_as_string_str), "job");
1139 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001140 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001141 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142#endif
1143 break;
1144 case VAR_CHANNEL:
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), "channel");
1149 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001150 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001151 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001152#endif
1153 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001154 case VAR_VOID:
1155 emsg(_(e_cannot_use_void_value));
1156 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001157 case VAR_UNKNOWN:
1158 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001159 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001160 semsg(_(e_using_invalid_value_as_string_str),
1161 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001162 break;
1163 }
1164 return NULL;
1165}
1166
1167/*
1168 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1169 * string() on Dict, List, etc.
1170 */
1171 char_u *
1172tv_stringify(typval_T *varp, char_u *buf)
1173{
1174 if (varp->v_type == VAR_LIST
1175 || varp->v_type == VAR_DICT
1176 || varp->v_type == VAR_BLOB
1177 || varp->v_type == VAR_FUNC
1178 || varp->v_type == VAR_PARTIAL
1179 || varp->v_type == VAR_FLOAT)
1180 {
1181 typval_T tmp;
1182
1183 f_string(varp, &tmp);
1184 tv_get_string_buf(&tmp, buf);
1185 clear_tv(varp);
1186 *varp = tmp;
1187 return tmp.vval.v_string;
1188 }
1189 return tv_get_string_buf(varp, buf);
1190}
1191
1192/*
1193 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1194 * Also give an error message, using "name" or _("name") when use_gettext is
1195 * TRUE.
1196 */
1197 int
1198tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1199{
1200 int lock = 0;
1201
1202 switch (tv->v_type)
1203 {
1204 case VAR_BLOB:
1205 if (tv->vval.v_blob != NULL)
1206 lock = tv->vval.v_blob->bv_lock;
1207 break;
1208 case VAR_LIST:
1209 if (tv->vval.v_list != NULL)
1210 lock = tv->vval.v_list->lv_lock;
1211 break;
1212 case VAR_DICT:
1213 if (tv->vval.v_dict != NULL)
1214 lock = tv->vval.v_dict->dv_lock;
1215 break;
1216 default:
1217 break;
1218 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001219 return value_check_lock(tv->v_lock, name, use_gettext)
1220 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001221}
1222
1223/*
1224 * Copy the values from typval_T "from" to typval_T "to".
1225 * When needed allocates string or increases reference count.
1226 * Does not make a copy of a list, blob or dict but copies the reference!
1227 * It is OK for "from" and "to" to point to the same item. This is used to
1228 * make a copy later.
1229 */
1230 void
1231copy_tv(typval_T *from, typval_T *to)
1232{
1233 to->v_type = from->v_type;
1234 to->v_lock = 0;
1235 switch (from->v_type)
1236 {
1237 case VAR_NUMBER:
1238 case VAR_BOOL:
1239 case VAR_SPECIAL:
1240 to->vval.v_number = from->vval.v_number;
1241 break;
1242 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001243 to->vval.v_float = from->vval.v_float;
1244 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001245 case VAR_JOB:
1246#ifdef FEAT_JOB_CHANNEL
1247 to->vval.v_job = from->vval.v_job;
1248 if (to->vval.v_job != NULL)
1249 ++to->vval.v_job->jv_refcount;
1250 break;
1251#endif
1252 case VAR_CHANNEL:
1253#ifdef FEAT_JOB_CHANNEL
1254 to->vval.v_channel = from->vval.v_channel;
1255 if (to->vval.v_channel != NULL)
1256 ++to->vval.v_channel->ch_refcount;
1257 break;
1258#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001259 case VAR_INSTR:
1260 to->vval.v_instr = from->vval.v_instr;
1261 break;
1262
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001263 case VAR_CLASS:
1264 copy_class(from, to);
1265 break;
1266
1267 case VAR_OBJECT:
1268 copy_object(from, to);
1269 break;
1270
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001271 case VAR_STRING:
1272 case VAR_FUNC:
1273 if (from->vval.v_string == NULL)
1274 to->vval.v_string = NULL;
1275 else
1276 {
1277 to->vval.v_string = vim_strsave(from->vval.v_string);
1278 if (from->v_type == VAR_FUNC)
1279 func_ref(to->vval.v_string);
1280 }
1281 break;
1282 case VAR_PARTIAL:
1283 if (from->vval.v_partial == NULL)
1284 to->vval.v_partial = NULL;
1285 else
1286 {
1287 to->vval.v_partial = from->vval.v_partial;
1288 ++to->vval.v_partial->pt_refcount;
1289 }
1290 break;
1291 case VAR_BLOB:
1292 if (from->vval.v_blob == NULL)
1293 to->vval.v_blob = NULL;
1294 else
1295 {
1296 to->vval.v_blob = from->vval.v_blob;
1297 ++to->vval.v_blob->bv_refcount;
1298 }
1299 break;
1300 case VAR_LIST:
1301 if (from->vval.v_list == NULL)
1302 to->vval.v_list = NULL;
1303 else
1304 {
1305 to->vval.v_list = from->vval.v_list;
1306 ++to->vval.v_list->lv_refcount;
1307 }
1308 break;
1309 case VAR_DICT:
1310 if (from->vval.v_dict == NULL)
1311 to->vval.v_dict = NULL;
1312 else
1313 {
1314 to->vval.v_dict = from->vval.v_dict;
1315 ++to->vval.v_dict->dv_refcount;
1316 }
1317 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001318 case VAR_TYPEALIAS:
1319 if (from->vval.v_typealias == NULL)
1320 to->vval.v_typealias = NULL;
1321 else
1322 {
1323 to->vval.v_typealias = from->vval.v_typealias;
1324 ++to->vval.v_typealias->ta_refcount;
1325 }
1326 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001327 case VAR_VOID:
1328 emsg(_(e_cannot_use_void_value));
1329 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001330 case VAR_UNKNOWN:
1331 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001332 internal_error_no_abort("copy_tv(UNKNOWN)");
1333 break;
1334 }
1335}
1336
1337/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001338 * Compare "tv1" and "tv2".
1339 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001340 */
1341 int
1342typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001343 typval_T *tv1, // first operand
1344 typval_T *tv2, // second operand
1345 exprtype_T type, // operator
1346 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001347{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001348 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001349 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001350 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1351
Ernie Raele75fde62023-12-21 17:18:54 +01001352 if (check_typval_is_value(tv1) == FAIL
1353 || check_typval_is_value(tv2) == FAIL)
1354 {
1355 clear_tv(tv1);
1356 return FAIL;
1357 }
1358 else if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001359 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001360 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001361 // it means TRUE.
1362 n1 = (type == EXPR_ISNOT);
1363 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001364 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1365 || (tv2->v_type == VAR_SPECIAL
1366 && tv2->vval.v_number == VVAL_NULL))
1367 && tv1->v_type != tv2->v_type
1368 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1369 {
1370 n1 = typval_compare_null(tv1, tv2);
1371 if (n1 == MAYBE)
1372 {
1373 clear_tv(tv1);
1374 return FAIL;
1375 }
1376 if (type == EXPR_NEQUAL)
1377 n1 = !n1;
1378 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001379 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001380 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001381 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001382 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001383 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001384 return FAIL;
1385 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001386 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001387 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001388 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001389 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001390 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001391 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001392 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001393 return FAIL;
1394 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001395 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001396 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001397 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1398 {
1399 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1400 {
1401 clear_tv(tv1);
1402 return FAIL;
1403 }
1404 n1 = res;
1405 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001406 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001407 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001408 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001409 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001410 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001411 return FAIL;
1412 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001413 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001414 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001415 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1416 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001417 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001418 if (typval_compare_func(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 }
1425
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001426 // If one of the two variables is a float, compare as a float.
1427 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001428 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001429 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1430 {
1431 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001432 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001434 f1 = tv_get_float_chk(tv1, &error);
1435 if (!error)
1436 f2 = tv_get_float_chk(tv2, &error);
1437 if (error)
1438 {
1439 clear_tv(tv1);
1440 return FAIL;
1441 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001442 n1 = FALSE;
1443 switch (type)
1444 {
1445 case EXPR_IS:
1446 case EXPR_EQUAL: n1 = (f1 == f2); break;
1447 case EXPR_ISNOT:
1448 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1449 case EXPR_GREATER: n1 = (f1 > f2); break;
1450 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1451 case EXPR_SMALLER: n1 = (f1 < f2); break;
1452 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1453 case EXPR_UNKNOWN:
1454 case EXPR_MATCH:
1455 default: break; // avoid gcc warning
1456 }
1457 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001458
1459 // If one of the two variables is a number, compare as a number.
1460 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001461 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001462 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1463 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001464 int error = FALSE;
1465
1466 n1 = tv_get_number_chk(tv1, &error);
1467 if (!error)
1468 n2 = tv_get_number_chk(tv2, &error);
1469 if (error)
1470 {
1471 clear_tv(tv1);
1472 return FAIL;
1473 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001474 switch (type)
1475 {
1476 case EXPR_IS:
1477 case EXPR_EQUAL: n1 = (n1 == n2); break;
1478 case EXPR_ISNOT:
1479 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1480 case EXPR_GREATER: n1 = (n1 > n2); break;
1481 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1482 case EXPR_SMALLER: n1 = (n1 < n2); break;
1483 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1484 case EXPR_UNKNOWN:
1485 case EXPR_MATCH:
1486 default: break; // avoid gcc warning
1487 }
1488 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001489 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1490 || tv2->v_type == VAR_BOOL
1491 || (tv1->v_type == VAR_SPECIAL
1492 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001493 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001494 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001495 {
1496 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001497 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1498 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001499 return FAIL;
1500 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001501 n1 = tv1->vval.v_number;
1502 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001503 switch (type)
1504 {
1505 case EXPR_IS:
1506 case EXPR_EQUAL: n1 = (n1 == n2); break;
1507 case EXPR_ISNOT:
1508 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1509 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001510 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001511 vartype_name(tv1->v_type));
1512 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001513 return FAIL;
1514 }
1515 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001516#ifdef FEAT_JOB_CHANNEL
1517 else if (tv1->v_type == tv2->v_type
1518 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1519 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1520 {
1521 if (tv1->v_type == VAR_CHANNEL)
1522 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1523 else
1524 n1 = tv1->vval.v_job == tv2->vval.v_job;
1525 if (type == EXPR_NEQUAL)
1526 n1 = !n1;
1527 }
1528#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001529 else
1530 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001531 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001532 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001533 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001534 return FAIL;
1535 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001536 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001537 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001538 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001539 if (in_vim9script())
1540 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001541 tv1->v_type = VAR_BOOL;
1542 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001543 }
1544 else
1545 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001546 tv1->v_type = VAR_NUMBER;
1547 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001548 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001549
1550 return OK;
1551}
1552
Bram Moolenaar34453202021-01-31 13:08:38 +01001553/*
dundargocc57b5bc2022-11-02 13:30:51 +00001554 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001555 * Put the result, false or true, in "res".
1556 * Return FAIL and give an error message when the comparison can't be done.
1557 */
1558 int
1559typval_compare_list(
1560 typval_T *tv1,
1561 typval_T *tv2,
1562 exprtype_T type,
1563 int ic,
1564 int *res)
1565{
1566 int val = 0;
1567
1568 if (type == EXPR_IS || type == EXPR_ISNOT)
1569 {
1570 val = (tv1->v_type == tv2->v_type
1571 && tv1->vval.v_list == tv2->vval.v_list);
1572 if (type == EXPR_ISNOT)
1573 val = !val;
1574 }
1575 else if (tv1->v_type != tv2->v_type
1576 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1577 {
1578 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001579 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001580 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001581 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001582 return FAIL;
1583 }
1584 else
1585 {
1586 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1587 ic, FALSE);
1588 if (type == EXPR_NEQUAL)
1589 val = !val;
1590 }
1591 *res = val;
1592 return OK;
1593}
1594
1595/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001596 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001597 */
1598 int
1599typval_compare_null(typval_T *tv1, typval_T *tv2)
1600{
1601 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1602 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1603 {
1604 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1605
1606 switch (tv->v_type)
1607 {
1608 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001609#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001610 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001611#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001612 // TODO: null_class handling
1613 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001614 case VAR_DICT: return tv->vval.v_dict == NULL;
1615 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001616#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001617 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001618#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001619 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001620 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001621 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1622 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001623
1624 case VAR_NUMBER: if (!in_vim9script())
1625 return tv->vval.v_number == 0;
1626 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001627 case VAR_FLOAT: if (!in_vim9script())
1628 return tv->vval.v_float == 0.0;
1629 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001630 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001631 default: break;
1632 }
1633 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001634 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001635 // we won't give an error
1636 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001637}
1638
1639/*
dundargocc57b5bc2022-11-02 13:30:51 +00001640 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001641 * Put the result, false or true, in "res".
1642 * Return FAIL and give an error message when the comparison can't be done.
1643 */
1644 int
1645typval_compare_blob(
1646 typval_T *tv1,
1647 typval_T *tv2,
1648 exprtype_T type,
1649 int *res)
1650{
1651 int val = 0;
1652
1653 if (type == EXPR_IS || type == EXPR_ISNOT)
1654 {
1655 val = (tv1->v_type == tv2->v_type
1656 && tv1->vval.v_blob == tv2->vval.v_blob);
1657 if (type == EXPR_ISNOT)
1658 val = !val;
1659 }
1660 else if (tv1->v_type != tv2->v_type
1661 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1662 {
1663 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001664 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001665 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001666 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001667 return FAIL;
1668 }
1669 else
1670 {
1671 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1672 if (type == EXPR_NEQUAL)
1673 val = !val;
1674 }
1675 *res = val;
1676 return OK;
1677}
1678
1679/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001680 * Compare "tv1" to "tv2" as classes according to "type".
1681 * Put the result, false or true, in "res".
1682 * Return FAIL and give an error message when the comparison can't be done.
1683 */
1684 int
1685typval_compare_class(
1686 typval_T *tv1,
1687 typval_T *tv2,
1688 exprtype_T type UNUSED,
1689 int ic UNUSED,
1690 int *res)
1691{
1692 // TODO: use "type"
1693 *res = tv1->vval.v_class == tv2->vval.v_class;
1694 return OK;
1695}
1696
1697/*
1698 * Compare "tv1" to "tv2" as objects according to "type".
1699 * Put the result, false or true, in "res".
1700 * Return FAIL and give an error message when the comparison can't be done.
1701 */
1702 int
1703typval_compare_object(
1704 typval_T *tv1,
1705 typval_T *tv2,
1706 exprtype_T type,
1707 int ic,
1708 int *res)
1709{
1710 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1711
1712 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1713 {
1714 *res = res_match;
1715 return OK;
1716 }
1717 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1718 {
1719 *res = !res_match;
1720 return OK;
1721 }
1722
1723 class_T *cl1 = tv1->vval.v_object->obj_class;
1724 class_T *cl2 = tv2->vval.v_object->obj_class;
1725 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1726 {
1727 *res = !res_match;
1728 return OK;
1729 }
1730
1731 object_T *obj1 = tv1->vval.v_object;
1732 object_T *obj2 = tv2->vval.v_object;
1733 if (type == EXPR_IS || type == EXPR_ISNOT)
1734 {
1735 *res = obj1 == obj2 ? res_match : !res_match;
1736 return OK;
1737 }
1738
1739 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1740 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1741 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1742 {
1743 *res = !res_match;
1744 return OK;
1745 }
1746 *res = res_match;
1747 return OK;
1748}
1749
1750/*
dundargocc57b5bc2022-11-02 13:30:51 +00001751 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001752 * Put the result, false or true, in "res".
1753 * Return FAIL and give an error message when the comparison can't be done.
1754 */
1755 int
1756typval_compare_dict(
1757 typval_T *tv1,
1758 typval_T *tv2,
1759 exprtype_T type,
1760 int ic,
1761 int *res)
1762{
1763 int val;
1764
1765 if (type == EXPR_IS || type == EXPR_ISNOT)
1766 {
1767 val = (tv1->v_type == tv2->v_type
1768 && tv1->vval.v_dict == tv2->vval.v_dict);
1769 if (type == EXPR_ISNOT)
1770 val = !val;
1771 }
1772 else if (tv1->v_type != tv2->v_type
1773 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1774 {
1775 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001776 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001777 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001778 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001779 return FAIL;
1780 }
1781 else
1782 {
1783 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1784 if (type == EXPR_NEQUAL)
1785 val = !val;
1786 }
1787 *res = val;
1788 return OK;
1789}
1790
1791/*
dundargocc57b5bc2022-11-02 13:30:51 +00001792 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001793 * Put the result, false or true, in "res".
1794 * Return FAIL and give an error message when the comparison can't be done.
1795 */
1796 int
1797typval_compare_func(
1798 typval_T *tv1,
1799 typval_T *tv2,
1800 exprtype_T type,
1801 int ic,
1802 int *res)
1803{
1804 int val = 0;
1805
1806 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1807 && type != EXPR_IS && type != EXPR_ISNOT)
1808 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001809 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001810 return FAIL;
1811 }
1812 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1813 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1814 // When both partials are NULL, then they are equal.
1815 // Otherwise they are not equal.
1816 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1817 else if (type == EXPR_IS || type == EXPR_ISNOT)
1818 {
1819 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1820 // strings are considered the same if their value is
1821 // the same
1822 val = tv_equal(tv1, tv2, ic, FALSE);
1823 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1824 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1825 else
1826 val = FALSE;
1827 }
1828 else
1829 val = tv_equal(tv1, tv2, ic, FALSE);
1830 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1831 val = !val;
1832 *res = val;
1833 return OK;
1834}
1835
1836/*
1837 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1838 * Put the result, false or true, in "res".
1839 * Return FAIL and give an error message when the comparison can't be done.
1840 */
1841 int
1842typval_compare_string(
1843 typval_T *tv1,
1844 typval_T *tv2,
1845 exprtype_T type,
1846 int ic,
1847 int *res)
1848{
1849 int i = 0;
1850 int val = FALSE;
1851 char_u *s1, *s2;
1852 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1853
1854 if (in_vim9script()
1855 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1856 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1857 {
1858 semsg(_(e_cannot_compare_str_with_str),
1859 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1860 return FAIL;
1861 }
1862 s1 = tv_get_string_buf(tv1, buf1);
1863 s2 = tv_get_string_buf(tv2, buf2);
1864 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1865 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1866 switch (type)
1867 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001868 case EXPR_IS: if (in_vim9script())
1869 {
1870 // Really check it is the same string, not just
1871 // the same value.
1872 val = tv1->vval.v_string == tv2->vval.v_string;
1873 break;
1874 }
1875 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001876 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001877 case EXPR_ISNOT: if (in_vim9script())
1878 {
1879 // Really check it is not the same string, not
1880 // just a different value.
1881 val = tv1->vval.v_string != tv2->vval.v_string;
1882 break;
1883 }
1884 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001885 case EXPR_NEQUAL: val = (i != 0); break;
1886 case EXPR_GREATER: val = (i > 0); break;
1887 case EXPR_GEQUAL: val = (i >= 0); break;
1888 case EXPR_SMALLER: val = (i < 0); break;
1889 case EXPR_SEQUAL: val = (i <= 0); break;
1890
1891 case EXPR_MATCH:
1892 case EXPR_NOMATCH:
1893 val = pattern_match(s2, s1, ic);
1894 if (type == EXPR_NOMATCH)
1895 val = !val;
1896 break;
1897
1898 default: break; // avoid gcc warning
1899 }
1900 *res = val;
1901 return OK;
1902}
1903/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001904 * Convert any type to a string, never give an error.
1905 * When "quotes" is TRUE add quotes to a string.
1906 * Returns an allocated string.
1907 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001908 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001909typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001910{
1911 char_u *tofree;
1912 char_u numbuf[NUMBUFLEN];
1913 char_u *ret = NULL;
1914
1915 if (arg == NULL)
1916 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001917 if (!quotes && arg->v_type == VAR_STRING)
1918 {
1919 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1920 : arg->vval.v_string);
1921 }
1922 else
1923 {
1924 ret = tv2string(arg, &tofree, numbuf, 0);
1925 // Make a copy if we have a value but it's not in allocated memory.
1926 if (ret != NULL && tofree == NULL)
1927 ret = vim_strsave(ret);
1928 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001929 return ret;
1930}
1931
1932/*
1933 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1934 * or it refers to a List or Dictionary that is locked.
1935 */
1936 int
1937tv_islocked(typval_T *tv)
1938{
1939 return (tv->v_lock & VAR_LOCKED)
1940 || (tv->v_type == VAR_LIST
1941 && tv->vval.v_list != NULL
1942 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1943 || (tv->v_type == VAR_DICT
1944 && tv->vval.v_dict != NULL
1945 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1946}
1947
1948 static int
1949func_equal(
1950 typval_T *tv1,
1951 typval_T *tv2,
1952 int ic) // ignore case
1953{
1954 char_u *s1, *s2;
1955 dict_T *d1, *d2;
1956 int a1, a2;
1957 int i;
1958
1959 // empty and NULL function name considered the same
1960 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1961 : partial_name(tv1->vval.v_partial);
1962 if (s1 != NULL && *s1 == NUL)
1963 s1 = NULL;
1964 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1965 : partial_name(tv2->vval.v_partial);
1966 if (s2 != NULL && *s2 == NUL)
1967 s2 = NULL;
1968 if (s1 == NULL || s2 == NULL)
1969 {
1970 if (s1 != s2)
1971 return FALSE;
1972 }
1973 else if (STRCMP(s1, s2) != 0)
1974 return FALSE;
1975
1976 // empty dict and NULL dict is different
1977 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1978 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1979 if (d1 == NULL || d2 == NULL)
1980 {
1981 if (d1 != d2)
1982 return FALSE;
1983 }
1984 else if (!dict_equal(d1, d2, ic, TRUE))
1985 return FALSE;
1986
1987 // empty list and no list considered the same
1988 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1989 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1990 if (a1 != a2)
1991 return FALSE;
1992 for (i = 0; i < a1; ++i)
1993 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1994 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1995 return FALSE;
1996
1997 return TRUE;
1998}
1999
2000/*
2001 * Return TRUE if "tv1" and "tv2" have the same value.
2002 * Compares the items just like "==" would compare them, but strings and
2003 * numbers are different. Floats and numbers are also different.
2004 */
2005 int
2006tv_equal(
2007 typval_T *tv1,
2008 typval_T *tv2,
2009 int ic, // ignore case
2010 int recursive) // TRUE when used recursively
2011{
2012 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2013 char_u *s1, *s2;
2014 static int recursive_cnt = 0; // catch recursive loops
2015 int r;
2016 static int tv_equal_recurse_limit;
2017
2018 // Catch lists and dicts that have an endless loop by limiting
2019 // recursiveness to a limit. We guess they are equal then.
2020 // A fixed limit has the problem of still taking an awful long time.
2021 // Reduce the limit every time running into it. That should work fine for
2022 // deeply linked structures that are not recursively linked and catch
2023 // recursiveness quickly.
2024 if (!recursive)
2025 tv_equal_recurse_limit = 1000;
2026 if (recursive_cnt >= tv_equal_recurse_limit)
2027 {
2028 --tv_equal_recurse_limit;
2029 return TRUE;
2030 }
2031
2032 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2033 // arguments.
2034 if ((tv1->v_type == VAR_FUNC
2035 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2036 && (tv2->v_type == VAR_FUNC
2037 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2038 {
2039 ++recursive_cnt;
2040 r = func_equal(tv1, tv2, ic);
2041 --recursive_cnt;
2042 return r;
2043 }
2044
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002045 if (tv1->v_type != tv2->v_type
2046 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2047 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002048 return FALSE;
2049
2050 switch (tv1->v_type)
2051 {
2052 case VAR_LIST:
2053 ++recursive_cnt;
2054 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2055 --recursive_cnt;
2056 return r;
2057
2058 case VAR_DICT:
2059 ++recursive_cnt;
2060 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2061 --recursive_cnt;
2062 return r;
2063
2064 case VAR_BLOB:
2065 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2066
2067 case VAR_NUMBER:
2068 case VAR_BOOL:
2069 case VAR_SPECIAL:
2070 return tv1->vval.v_number == tv2->vval.v_number;
2071
2072 case VAR_STRING:
2073 s1 = tv_get_string_buf(tv1, buf1);
2074 s2 = tv_get_string_buf(tv2, buf2);
2075 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2076
2077 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002078 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002079 case VAR_JOB:
2080#ifdef FEAT_JOB_CHANNEL
2081 return tv1->vval.v_job == tv2->vval.v_job;
2082#endif
2083 case VAR_CHANNEL:
2084#ifdef FEAT_JOB_CHANNEL
2085 return tv1->vval.v_channel == tv2->vval.v_channel;
2086#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002087 case VAR_INSTR:
2088 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002089
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002090 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002091 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002092 return tv1->vval.v_class == tv2->vval.v_class;
2093
2094 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002095 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2096 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002097
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002098 case VAR_PARTIAL:
2099 return tv1->vval.v_partial == tv2->vval.v_partial;
2100
2101 case VAR_FUNC:
2102 return tv1->vval.v_string == tv2->vval.v_string;
2103
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002104 case VAR_TYPEALIAS:
2105 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2106
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107 case VAR_UNKNOWN:
2108 case VAR_ANY:
2109 case VAR_VOID:
2110 break;
2111 }
2112
2113 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2114 // does not equal anything, not even itself.
2115 return FALSE;
2116}
2117
2118/*
2119 * Get an option value.
2120 * "arg" points to the '&' or '+' before the option name.
2121 * "arg" is advanced to character after the option name.
2122 * Return OK or FAIL.
2123 */
2124 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002125eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002126 char_u **arg,
2127 typval_T *rettv, // when NULL, only check if option exists
2128 int evaluate)
2129{
2130 char_u *option_end;
2131 long numval;
2132 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002133 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002134 int c;
2135 int working = (**arg == '+'); // has("+option")
2136 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002137 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002138
2139 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002140 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002141 if (option_end == NULL)
2142 {
2143 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002144 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002145 return FAIL;
2146 }
2147
2148 if (!evaluate)
2149 {
2150 *arg = option_end;
2151 return OK;
2152 }
2153
2154 c = *option_end;
2155 *option_end = NUL;
2156 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002157 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002158
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002159 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002160 {
2161 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002162 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002163 ret = FAIL;
2164 }
2165 else if (rettv != NULL)
2166 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002167 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002168 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002169 {
2170 rettv->v_type = VAR_STRING;
2171 rettv->vval.v_string = NULL;
2172 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002173 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002174 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002175 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2176 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002177 rettv->vval.v_number = 0;
2178 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002179 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002180 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002181 if (in_vim9script() && opt_type == gov_bool)
2182 {
2183 rettv->v_type = VAR_BOOL;
2184 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2185 }
2186 else
2187 {
2188 rettv->v_type = VAR_NUMBER;
2189 rettv->vval.v_number = numval;
2190 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002191 }
2192 else // string option
2193 {
2194 rettv->v_type = VAR_STRING;
2195 rettv->vval.v_string = stringval;
2196 }
2197 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002198 else if (working && (opt_type == gov_hidden_bool
2199 || opt_type == gov_hidden_number
2200 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002201 ret = FAIL;
2202
2203 *option_end = c; // put back for error messages
2204 *arg = option_end;
2205
2206 return ret;
2207}
2208
2209/*
2210 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2211 * Return OK or FAIL.
2212 */
2213 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002214eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002215 char_u **arg,
2216 typval_T *rettv,
2217 int evaluate,
2218 int want_string UNUSED)
2219{
2220 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002221 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002222 char_u *p;
2223 int get_float = FALSE;
2224
2225 // We accept a float when the format matches
2226 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2227 // strict to avoid backwards compatibility problems.
2228 // With script version 2 and later the leading digit can be
2229 // omitted.
2230 // Don't look for a float after the "." operator, so that
2231 // ":let vers = 1.2.3" doesn't fail.
2232 if (**arg == '.')
2233 p = *arg;
2234 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002235 {
2236 p = *arg + 1;
2237 if (skip_quotes)
2238 for (;;)
2239 {
2240 if (*p == '\'')
2241 ++p;
2242 if (!vim_isdigit(*p))
2243 break;
2244 p = skipdigits(p);
2245 }
2246 else
2247 p = skipdigits(p);
2248 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002249 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2250 {
2251 get_float = TRUE;
2252 p = skipdigits(p + 2);
2253 if (*p == 'e' || *p == 'E')
2254 {
2255 ++p;
2256 if (*p == '-' || *p == '+')
2257 ++p;
2258 if (!vim_isdigit(*p))
2259 get_float = FALSE;
2260 else
2261 p = skipdigits(p + 1);
2262 }
2263 if (ASCII_ISALPHA(*p) || *p == '.')
2264 get_float = FALSE;
2265 }
2266 if (get_float)
2267 {
2268 float_T f;
2269
Bram Moolenaar29500652021-08-08 15:43:34 +02002270 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002271 if (evaluate)
2272 {
2273 rettv->v_type = VAR_FLOAT;
2274 rettv->vval.v_float = f;
2275 }
2276 }
2277 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002278 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2279 {
2280 char_u *bp;
2281 blob_T *blob = NULL; // init for gcc
2282
2283 // Blob constant: 0z0123456789abcdef
2284 if (evaluate)
2285 blob = blob_alloc();
2286 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2287 {
2288 if (!vim_isxdigit(bp[1]))
2289 {
2290 if (blob != NULL)
2291 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002292 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002293 ga_clear(&blob->bv_ga);
2294 VIM_CLEAR(blob);
2295 }
2296 return FAIL;
2297 }
2298 if (blob != NULL)
2299 ga_append(&blob->bv_ga,
2300 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2301 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2302 ++bp;
2303 }
2304 if (blob != NULL)
2305 rettv_blob_set(rettv, blob);
2306 *arg = bp;
2307 }
2308 else
2309 {
2310 varnumber_T n;
2311
2312 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002313 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002314 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002315 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002316 if (len == 0)
2317 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002318 if (evaluate)
2319 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002320 return FAIL;
2321 }
2322 *arg += len;
2323 if (evaluate)
2324 {
2325 rettv->v_type = VAR_NUMBER;
2326 rettv->vval.v_number = n;
2327 }
2328 }
2329 return OK;
2330}
2331
2332/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002333 * Evaluate a string constant and put the result in "rettv".
2334 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2335 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2336 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002337 * Return OK or FAIL.
2338 */
2339 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002340eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002341{
2342 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002343 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002344 int extra = interpolate ? 1 : 0;
2345 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002346 int len;
2347
2348 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002349 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002350 {
2351 if (*p == '\\' && p[1] != NUL)
2352 {
2353 ++p;
2354 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002355 // to 9 characters (6 for the char and 3 for a modifier):
2356 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002357 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002358 {
2359 int modifiers = 0;
2360 int flags = FSK_KEYCODE | FSK_IN_STRING;
2361
zeertzjqdb088872022-05-02 22:53:45 +01002362 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002363
2364 // Skip to the '>' to avoid using '{' inside for string
2365 // interpolation.
2366 if (p[1] != '*')
2367 flags |= FSK_SIMPLIFY;
2368 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2369 --p; // leave "p" on the ">"
2370 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002371 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002372 else if (interpolate && (*p == '{' || *p == '}'))
2373 {
2374 if (*p == '{' && p[1] != '{') // start of expression
2375 break;
2376 ++p;
2377 if (p[-1] == '}' && *p != '}') // single '}' is an error
2378 {
2379 semsg(_(e_stray_closing_curly_str), *arg);
2380 return FAIL;
2381 }
2382 --extra; // "{{" becomes "{", "}}" becomes "}"
2383 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002384 }
2385
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002386 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002387 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002388 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002389 return FAIL;
2390 }
2391
2392 // If only parsing, set *arg and return here
2393 if (!evaluate)
2394 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002395 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002396 return OK;
2397 }
2398
2399 // Copy the string into allocated memory, handling backslashed
2400 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002401 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002402 len = (int)(p - *arg + extra);
2403 rettv->vval.v_string = alloc(len);
2404 if (rettv->vval.v_string == NULL)
2405 return FAIL;
2406 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002407
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002408 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002409 {
2410 if (*p == '\\')
2411 {
2412 switch (*++p)
2413 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002414 case 'b': *end++ = BS; ++p; break;
2415 case 'e': *end++ = ESC; ++p; break;
2416 case 'f': *end++ = FF; ++p; break;
2417 case 'n': *end++ = NL; ++p; break;
2418 case 'r': *end++ = CAR; ++p; break;
2419 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002420
2421 case 'X': // hex: "\x1", "\x12"
2422 case 'x':
2423 case 'u': // Unicode: "\u0023"
2424 case 'U':
2425 if (vim_isxdigit(p[1]))
2426 {
2427 int n, nr;
Keith Thompson184f71c2024-01-04 21:19:04 +01002428 int c = SAFE_toupper(*p);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002429
2430 if (c == 'X')
2431 n = 2;
2432 else if (*p == 'u')
2433 n = 4;
2434 else
2435 n = 8;
2436 nr = 0;
2437 while (--n >= 0 && vim_isxdigit(p[1]))
2438 {
2439 ++p;
2440 nr = (nr << 4) + hex2nr(*p);
2441 }
2442 ++p;
2443 // For "\u" store the number according to
2444 // 'encoding'.
2445 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002446 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002447 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002448 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002449 }
2450 break;
2451
2452 // octal: "\1", "\12", "\123"
2453 case '0':
2454 case '1':
2455 case '2':
2456 case '3':
2457 case '4':
2458 case '5':
2459 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002460 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002461 if (*p >= '0' && *p <= '7')
2462 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002463 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002464 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002465 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002466 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002467 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002468 break;
2469
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002470 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002471 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002472 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002473 int flags = FSK_KEYCODE | FSK_IN_STRING;
2474
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002475 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002476 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002477 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002478 if (extra != 0)
2479 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002480 end += extra;
2481 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002482 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002483 break;
2484 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002485 }
2486 // FALLTHROUGH
2487
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002488 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002489 break;
2490 }
2491 }
2492 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002493 {
2494 if (interpolate && (*p == '{' || *p == '}'))
2495 {
2496 if (*p == '{' && p[1] != '{') // start of expression
2497 break;
2498 ++p; // reduce "{{" to "{" and "}}" to "}"
2499 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002500 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002501 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002502 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002503 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002504 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002505 ++p;
2506 *arg = p;
2507
2508 return OK;
2509}
2510
2511/*
2512 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002513 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2514 * Return OK when a "rettv" was set to the string.
2515 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002516 */
2517 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002518eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002519{
2520 char_u *p;
2521 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002522 int reduce = interpolate ? -1 : 0;
2523 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002524
2525 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002526 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002527 {
2528 if (*p == '\'')
2529 {
2530 if (p[1] != '\'')
2531 break;
2532 ++reduce;
2533 ++p;
2534 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002535 else if (interpolate)
2536 {
2537 if (*p == '{')
2538 {
2539 if (p[1] != '{')
2540 break;
2541 ++p;
2542 ++reduce;
2543 }
2544 else if (*p == '}')
2545 {
2546 ++p;
2547 if (*p != '}')
2548 {
2549 semsg(_(e_stray_closing_curly_str), *arg);
2550 return FAIL;
2551 }
2552 ++reduce;
2553 }
2554 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002555 }
2556
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002557 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002558 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002559 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002560 return FAIL;
2561 }
2562
2563 // If only parsing return after setting "*arg"
2564 if (!evaluate)
2565 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002566 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002567 return OK;
2568 }
2569
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002570 // Copy the string into allocated memory, handling '' to ' reduction and
2571 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002572 str = alloc((p - *arg) - reduce);
2573 if (str == NULL)
2574 return FAIL;
2575 rettv->v_type = VAR_STRING;
2576 rettv->vval.v_string = str;
2577
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002578 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002579 {
2580 if (*p == '\'')
2581 {
2582 if (p[1] != '\'')
2583 break;
2584 ++p;
2585 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002586 else if (interpolate && (*p == '{' || *p == '}'))
2587 {
2588 if (*p == '{' && p[1] != '{')
2589 break;
2590 ++p;
2591 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002592 MB_COPY_CHAR(p, str);
2593 }
2594 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002595 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002596
2597 return OK;
2598}
2599
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002600/*
2601 * Evaluate a single or double quoted string possibly containing expressions.
2602 * "arg" points to the '$'. The result is put in "rettv".
2603 * Returns OK or FAIL.
2604 */
LemonBoy2eaef102022-05-06 13:14:50 +01002605 int
2606eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2607{
2608 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002609 int ret = OK;
2610 int quote;
2611 garray_T ga;
2612 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002613
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002614 ga_init2(&ga, 1, 80);
2615
2616 // *arg is on the '$' character, move it to the first string character.
2617 ++*arg;
2618 quote = **arg;
2619 ++*arg;
2620
2621 for (;;)
2622 {
2623 // Get the string up to the matching quote or to a single '{'.
2624 // "arg" is advanced to either the quote or the '{'.
2625 if (quote == '"')
2626 ret = eval_string(arg, &tv, evaluate, TRUE);
2627 else
2628 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2629 if (ret == FAIL)
2630 break;
2631 if (evaluate)
2632 {
2633 ga_concat(&ga, tv.vval.v_string);
2634 clear_tv(&tv);
2635 }
2636
2637 if (**arg != '{')
2638 {
2639 // found terminating quote
2640 ++*arg;
2641 break;
2642 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002643 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002644 if (p == NULL)
2645 {
2646 ret = FAIL;
2647 break;
2648 }
2649 *arg = p;
2650 }
LemonBoy2eaef102022-05-06 13:14:50 +01002651
2652 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002653 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2654 {
2655 ga_clear(&ga);
2656 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002657 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002658 }
LemonBoy2eaef102022-05-06 13:14:50 +01002659
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002660 rettv->vval.v_string = ga.ga_data;
2661 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002662}
2663
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002664/*
2665 * Return a string with the string representation of a variable.
2666 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2667 * "numbuf" is used for a number.
2668 * Puts quotes around strings, so that they can be parsed back by eval().
2669 * May return NULL.
2670 */
2671 char_u *
2672tv2string(
2673 typval_T *tv,
2674 char_u **tofree,
2675 char_u *numbuf,
2676 int copyID)
2677{
2678 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2679}
2680
2681/*
2682 * Get the value of an environment variable.
2683 * "arg" is pointing to the '$'. It is advanced to after the name.
2684 * If the environment variable was not set, silently assume it is empty.
2685 * Return FAIL if the name is invalid.
2686 */
2687 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002688eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002689{
2690 char_u *string = NULL;
2691 int len;
2692 int cc;
2693 char_u *name;
2694 int mustfree = FALSE;
2695
2696 ++*arg;
2697 name = *arg;
2698 len = get_env_len(arg);
2699 if (evaluate)
2700 {
2701 if (len == 0)
2702 return FAIL; // invalid empty name
2703
2704 cc = name[len];
2705 name[len] = NUL;
2706 // first try vim_getenv(), fast for normal environment vars
2707 string = vim_getenv(name, &mustfree);
2708 if (string != NULL && *string != NUL)
2709 {
2710 if (!mustfree)
2711 string = vim_strsave(string);
2712 }
2713 else
2714 {
2715 if (mustfree)
2716 vim_free(string);
2717
2718 // next try expanding things like $VIM and ${HOME}
2719 string = expand_env_save(name - 1);
2720 if (string != NULL && *string == '$')
2721 VIM_CLEAR(string);
2722 }
2723 name[len] = cc;
2724
2725 rettv->v_type = VAR_STRING;
2726 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002727 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002728 }
2729
2730 return OK;
2731}
2732
2733/*
2734 * Get the lnum from the first argument.
2735 * Also accepts ".", "$", etc., but that only works for the current buffer.
2736 * Returns -1 on error.
2737 */
2738 linenr_T
2739tv_get_lnum(typval_T *argvars)
2740{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002741 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002742 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002743
Bram Moolenaar56acb092020-08-16 14:48:19 +02002744 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2745 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002746 if (lnum <= 0 && did_emsg_before == did_emsg
2747 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002748 {
2749 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002750 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002751
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002752 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002753 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002754 if (fp != NULL)
2755 lnum = fp->lnum;
2756 }
2757 return lnum;
2758}
2759
2760/*
2761 * Get the lnum from the first argument.
2762 * Also accepts "$", then "buf" is used.
2763 * Returns 0 on error.
2764 */
2765 linenr_T
2766tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2767{
2768 if (argvars[0].v_type == VAR_STRING
2769 && argvars[0].vval.v_string != NULL
2770 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002771 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002772 && buf != NULL)
2773 return buf->b_ml.ml_line_count;
2774 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2775}
2776
2777/*
2778 * Get buffer by number or pattern.
2779 */
2780 buf_T *
2781tv_get_buf(typval_T *tv, int curtab_only)
2782{
2783 char_u *name = tv->vval.v_string;
2784 buf_T *buf;
2785
2786 if (tv->v_type == VAR_NUMBER)
2787 return buflist_findnr((int)tv->vval.v_number);
2788 if (tv->v_type != VAR_STRING)
2789 return NULL;
2790 if (name == NULL || *name == NUL)
2791 return curbuf;
2792 if (name[0] == '$' && name[1] == NUL)
2793 return lastbuf;
2794
2795 buf = buflist_find_by_name(name, curtab_only);
2796
2797 // If not found, try expanding the name, like done for bufexists().
2798 if (buf == NULL)
2799 buf = find_buffer(tv);
2800
2801 return buf;
2802}
2803
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002804/*
2805 * Like tv_get_buf() but give an error message is the type is wrong.
2806 */
2807 buf_T *
2808tv_get_buf_from_arg(typval_T *tv)
2809{
2810 buf_T *buf;
2811
2812 ++emsg_off;
2813 buf = tv_get_buf(tv, FALSE);
2814 --emsg_off;
2815 if (buf == NULL
2816 && tv->v_type != VAR_NUMBER
2817 && tv->v_type != VAR_STRING)
2818 // issue errmsg for type error
2819 (void)tv_get_number(tv);
2820 return buf;
2821}
2822
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002823#endif // FEAT_EVAL