blob: d1fa227d1d40a08fc2b5978f1a41d6afd8d5da6c [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:
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100269 {
270 class_T *cl = varp->vval.v_object->obj_class;
271 if (cl != NULL && IS_ENUM(cl))
272 semsg(_(e_using_enum_str_as_number), cl->class_name);
273 else
274 emsg(_(e_using_object_as_number));
275 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000276 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200277 case VAR_VOID:
278 emsg(_(e_cannot_use_void_value));
279 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200280 case VAR_UNKNOWN:
281 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200282 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200283 internal_error_no_abort("tv_get_number(UNKNOWN)");
284 break;
285 }
286 if (denote == NULL) // useful for values that must be unsigned
287 n = -1;
288 else
289 *denote = TRUE;
290 return n;
291}
292
Bram Moolenaar36967b32020-08-17 21:41:02 +0200293/*
294 * Get the number value of a variable.
295 * If it is a String variable, uses vim_str2nr().
296 * For incompatible types, return 0.
297 * tv_get_number_chk() is similar to tv_get_number(), but informs the
298 * caller of incompatible types: it sets *denote to TRUE if "denote"
299 * is not NULL or returns -1 otherwise.
300 */
301 varnumber_T
302tv_get_number(typval_T *varp)
303{
304 int error = FALSE;
305
306 return tv_get_number_chk(varp, &error); // return 0L on error
307}
308
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000309/*
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200310 * Like tv_get_number() but in Vim9 script do convert a number in a string to a
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000311 * number without giving an error.
312 */
313 varnumber_T
314tv_to_number(typval_T *varp)
315{
316 int error = FALSE;
317
318 return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
319}
320
Bram Moolenaar36967b32020-08-17 21:41:02 +0200321 varnumber_T
322tv_get_number_chk(typval_T *varp, int *denote)
323{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000324 return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200325}
326
327/*
328 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200329 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200330 */
331 varnumber_T
332tv_get_bool(typval_T *varp)
333{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000334 return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200335}
336
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200337/*
338 * Get the boolean value of "varp". This is like tv_get_number_chk(),
339 * but in Vim9 script accepts Number and Bool.
340 */
341 varnumber_T
342tv_get_bool_chk(typval_T *varp, int *denote)
343{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000344 return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200345}
346
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000347 static float_T
348tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200349{
350 switch (varp->v_type)
351 {
352 case VAR_NUMBER:
353 return (float_T)(varp->vval.v_number);
354 case VAR_FLOAT:
355 return varp->vval.v_float;
356 case VAR_FUNC:
357 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000358 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200359 break;
360 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000361 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200362 break;
363 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000364 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200365 break;
366 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000367 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200368 break;
369 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000370 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200371 break;
372 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000373 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200374 break;
375 case VAR_JOB:
376# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000377 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200378 break;
379# endif
380 case VAR_CHANNEL:
381# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000382 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200383 break;
384# endif
385 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000386 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200387 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +0100389 case VAR_TYPEALIAS:
390 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000391 break;
392 case VAR_OBJECT:
393 emsg(_(e_using_object_as_float));
394 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200395 case VAR_VOID:
396 emsg(_(e_cannot_use_void_value));
397 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200398 case VAR_UNKNOWN:
399 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200400 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200401 internal_error_no_abort("tv_get_float(UNKNOWN)");
402 break;
403 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000404 if (error != NULL)
405 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200406 return 0;
407}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000408
409 float_T
410tv_get_float(typval_T *varp)
411{
412 return tv_get_float_chk(varp, NULL);
413}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200414
415/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100416 * Give an error and return FAIL unless "args[idx]" is unknown
417 */
418 int
419check_for_unknown_arg(typval_T *args, int idx)
420{
421 if (args[idx].v_type != VAR_UNKNOWN)
422 {
423 semsg(_(e_too_many_arguments), idx + 1);
424 return FAIL;
425 }
426 return OK;
427}
428
429/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200430 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100431 */
432 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100433check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100434{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100435 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100436 {
rbtnnddc80af2021-12-17 18:01:31 +0000437 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100438 return FAIL;
439 }
440 return OK;
441}
442
443/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100444 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100445 */
446 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100447check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100448{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100449 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100450 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100451 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100452 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200453 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100454 return FAIL;
455 }
456 return OK;
457}
458
459/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200460 * Check for an optional string argument at 'idx'
461 */
462 int
463check_for_opt_string_arg(typval_T *args, int idx)
464{
465 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100466 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200467}
468
469/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200470 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200471 */
472 int
473check_for_number_arg(typval_T *args, int idx)
474{
475 if (args[idx].v_type != VAR_NUMBER)
476 {
rbtnnddc80af2021-12-17 18:01:31 +0000477 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200478 return FAIL;
479 }
480 return OK;
481}
482
483/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200484 * Check for an optional number argument at 'idx'
485 */
486 int
487check_for_opt_number_arg(typval_T *args, int idx)
488{
489 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100490 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200491}
492
493/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200494 * Give an error and return FAIL unless "args[idx]" is a float or a number.
495 */
496 int
497check_for_float_or_nr_arg(typval_T *args, int idx)
498{
499 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
500 {
rbtnnddc80af2021-12-17 18:01:31 +0000501 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200502 return FAIL;
503 }
504 return OK;
505}
506
507/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200508 * Give an error and return FAIL unless "args[idx]" is a bool.
509 */
510 int
511check_for_bool_arg(typval_T *args, int idx)
512{
513 if (args[idx].v_type != VAR_BOOL
514 && !(args[idx].v_type == VAR_NUMBER
515 && (args[idx].vval.v_number == 0
516 || args[idx].vval.v_number == 1)))
517 {
rbtnnddc80af2021-12-17 18:01:31 +0000518 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200519 return FAIL;
520 }
521 return OK;
522}
523
524/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200525 * Check for an optional bool argument at 'idx'.
526 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200527 */
528 int
529check_for_opt_bool_arg(typval_T *args, int idx)
530{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200531 if (args[idx].v_type == VAR_UNKNOWN)
532 return OK;
533 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200534}
535
536/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200537 * Give an error and return FAIL unless "args[idx]" is a blob.
538 */
539 int
540check_for_blob_arg(typval_T *args, int idx)
541{
542 if (args[idx].v_type != VAR_BLOB)
543 {
rbtnnddc80af2021-12-17 18:01:31 +0000544 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200545 return FAIL;
546 }
547 return OK;
548}
549
550/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200551 * Give an error and return FAIL unless "args[idx]" is a list.
552 */
553 int
554check_for_list_arg(typval_T *args, int idx)
555{
556 if (args[idx].v_type != VAR_LIST)
557 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200558 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200559 return FAIL;
560 }
561 return OK;
562}
563
564/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100565 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
566 */
567 int
568check_for_nonnull_list_arg(typval_T *args, int idx)
569{
570 if (check_for_list_arg(args, idx) == FAIL)
571 return FAIL;
572
573 if (args[idx].vval.v_list == NULL)
574 {
575 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
576 return FAIL;
577 }
578 return OK;
579}
580
581/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200582 * Check for an optional list argument at 'idx'
583 */
584 int
585check_for_opt_list_arg(typval_T *args, int idx)
586{
587 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100588 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589}
590
591/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200592 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200593 */
594 int
595check_for_dict_arg(typval_T *args, int idx)
596{
597 if (args[idx].v_type != VAR_DICT)
598 {
rbtnnddc80af2021-12-17 18:01:31 +0000599 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200600 return FAIL;
601 }
602 return OK;
603}
604
605/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100606 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
607 */
608 int
609check_for_nonnull_dict_arg(typval_T *args, int idx)
610{
611 if (check_for_dict_arg(args, idx) == FAIL)
612 return FAIL;
613
614 if (args[idx].vval.v_dict == NULL)
615 {
616 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
617 return FAIL;
618 }
619 return OK;
620}
621
622/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200623 * Check for an optional dict argument at 'idx'
624 */
625 int
626check_for_opt_dict_arg(typval_T *args, int idx)
627{
628 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100629 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200630}
631
Yegappan Lakshmananfa378352024-02-01 22:05:27 +0100632/*
633 * Check for an optional non-NULL dict argument at 'idx'
634 */
635 int
636check_for_opt_nonnull_dict_arg(typval_T *args, int idx)
637{
638 return (args[idx].v_type == VAR_UNKNOWN
639 || check_for_nonnull_dict_arg(args, idx) != FAIL) ? OK : FAIL;
640}
641
Dominique Pelle748b3082022-01-08 12:41:16 +0000642#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200643/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200644 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
645 */
646 int
647check_for_chan_or_job_arg(typval_T *args, int idx)
648{
649 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
650 {
rbtnnddc80af2021-12-17 18:01:31 +0000651 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200652 return FAIL;
653 }
654 return OK;
655}
656
657/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200658 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
659 * job.
660 */
661 int
662check_for_opt_chan_or_job_arg(typval_T *args, int idx)
663{
664 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100665 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200666}
667
668/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200669 * Give an error and return FAIL unless "args[idx]" is a job.
670 */
671 int
672check_for_job_arg(typval_T *args, int idx)
673{
674 if (args[idx].v_type != VAR_JOB)
675 {
rbtnnddc80af2021-12-17 18:01:31 +0000676 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200677 return FAIL;
678 }
679 return OK;
680}
681
682/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200683 * Check for an optional job argument at 'idx'.
684 */
685 int
686check_for_opt_job_arg(typval_T *args, int idx)
687{
688 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100689 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200690}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000691#else
692/*
693 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
694 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
695 */
696 int
697check_for_opt_chan_or_job_arg(typval_T *args, int idx)
698{
699 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
700}
Dominique Pelle748b3082022-01-08 12:41:16 +0000701#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200702
703/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200704 * Give an error and return FAIL unless "args[idx]" is a string or
705 * a number.
706 */
707 int
708check_for_string_or_number_arg(typval_T *args, int idx)
709{
710 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
711 {
rbtnnddc80af2021-12-17 18:01:31 +0000712 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200713 return FAIL;
714 }
715 return OK;
716}
717
718/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200719 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200720 */
721 int
722check_for_opt_string_or_number_arg(typval_T *args, int idx)
723{
724 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100725 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200726}
727
728/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200729 * Give an error and return FAIL unless "args[idx]" is a buffer number.
730 * Buffer number can be a number or a string.
731 */
732 int
733check_for_buffer_arg(typval_T *args, int idx)
734{
735 return check_for_string_or_number_arg(args, idx);
736}
737
738/*
739 * Check for an optional buffer argument at 'idx'
740 */
741 int
742check_for_opt_buffer_arg(typval_T *args, int idx)
743{
744 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100745 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200746}
747
748/*
749 * Give an error and return FAIL unless "args[idx]" is a line number.
750 * Line number can be a number or a string.
751 */
752 int
753check_for_lnum_arg(typval_T *args, int idx)
754{
755 return check_for_string_or_number_arg(args, idx);
756}
757
758/*
759 * Check for an optional line number argument at 'idx'
760 */
761 int
762check_for_opt_lnum_arg(typval_T *args, int idx)
763{
764 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100765 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200766}
767
Dominique Pelle748b3082022-01-08 12:41:16 +0000768#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200769/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200770 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200771 */
772 int
773check_for_string_or_blob_arg(typval_T *args, int idx)
774{
775 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
776 {
rbtnnddc80af2021-12-17 18:01:31 +0000777 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200778 return FAIL;
779 }
780 return OK;
781}
Dominique Pelle748b3082022-01-08 12:41:16 +0000782#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200783
784/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200785 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200786 */
787 int
788check_for_string_or_list_arg(typval_T *args, int idx)
789{
790 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
791 {
rbtnnddc80af2021-12-17 18:01:31 +0000792 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200793 return FAIL;
794 }
795 return OK;
796}
797
798/*
rbtnn0ccb5842021-12-18 18:33:46 +0000799 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
800 * blob.
801 */
802 int
803check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
804{
805 if (args[idx].v_type != VAR_STRING
806 && args[idx].v_type != VAR_LIST
807 && args[idx].v_type != VAR_BLOB)
808 {
809 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
810 return FAIL;
811 }
812 return OK;
813}
814
815/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200816 * Check for an optional string or list argument at 'idx'
817 */
818 int
819check_for_opt_string_or_list_arg(typval_T *args, int idx)
820{
821 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100822 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200823}
824
825/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200826 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
827 */
828 int
829check_for_string_or_dict_arg(typval_T *args, int idx)
830{
831 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
832 {
rbtnnddc80af2021-12-17 18:01:31 +0000833 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200834 return FAIL;
835 }
836 return OK;
837}
838
839/*
840 * Give an error and return FAIL unless "args[idx]" is a string or a number
841 * or a list.
842 */
843 int
844check_for_string_or_number_or_list_arg(typval_T *args, int idx)
845{
846 if (args[idx].v_type != VAR_STRING
847 && args[idx].v_type != VAR_NUMBER
848 && args[idx].v_type != VAR_LIST)
849 {
rbtnn0ccb5842021-12-18 18:33:46 +0000850 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200851 return FAIL;
852 }
853 return OK;
854}
855
856/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200857 * Give an error and return FAIL unless "args[idx]" is an optional string
858 * or number or a list
859 */
860 int
861check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
862{
863 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100864 || check_for_string_or_number_or_list_arg(args, idx)
865 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200866}
867
868/*
Bakudankun375141e2022-09-09 18:46:47 +0100869 * Give an error and return FAIL unless "args[idx]" is a string or a number
870 * or a list or a blob.
871 */
872 int
873check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
874{
875 if (args[idx].v_type != VAR_STRING
876 && args[idx].v_type != VAR_NUMBER
877 && args[idx].v_type != VAR_LIST
878 && args[idx].v_type != VAR_BLOB)
879 {
880 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
881 return FAIL;
882 }
883 return OK;
884}
885
886/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200887 * Give an error and return FAIL unless "args[idx]" is a string or a list
888 * or a dict.
889 */
890 int
891check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
892{
893 if (args[idx].v_type != VAR_STRING
894 && args[idx].v_type != VAR_LIST
895 && args[idx].v_type != VAR_DICT)
896 {
rbtnnddc80af2021-12-17 18:01:31 +0000897 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200898 return FAIL;
899 }
900 return OK;
901}
902
903/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000904 * Give an error and return FAIL unless "args[idx]" is a string
905 * or a function reference.
906 */
907 int
908check_for_string_or_func_arg(typval_T *args, int idx)
909{
910 if (args[idx].v_type != VAR_PARTIAL
911 && args[idx].v_type != VAR_FUNC
912 && args[idx].v_type != VAR_STRING)
913 {
914 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
915 return FAIL;
916 }
917 return OK;
918}
919
920/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200921 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
922 */
923 int
924check_for_list_or_blob_arg(typval_T *args, int idx)
925{
926 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
927 {
rbtnn0ccb5842021-12-18 18:33:46 +0000928 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200929 return FAIL;
930 }
931 return OK;
932}
933
934/*
935 * Give an error and return FAIL unless "args[idx]" is a list or dict
936 */
937 int
938check_for_list_or_dict_arg(typval_T *args, int idx)
939{
940 if (args[idx].v_type != VAR_LIST
941 && args[idx].v_type != VAR_DICT)
942 {
rbtnnddc80af2021-12-17 18:01:31 +0000943 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200944 return FAIL;
945 }
946 return OK;
947}
948
949/*
950 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
951 * blob.
952 */
953 int
954check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
955{
956 if (args[idx].v_type != VAR_LIST
957 && args[idx].v_type != VAR_DICT
958 && args[idx].v_type != VAR_BLOB)
959 {
rbtnnddc80af2021-12-17 18:01:31 +0000960 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200961 return FAIL;
962 }
963 return OK;
964}
965
966/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000967 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
968 * blob or a string.
969 */
970 int
971check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
972{
973 if (args[idx].v_type != VAR_LIST
974 && args[idx].v_type != VAR_DICT
975 && args[idx].v_type != VAR_BLOB
976 && args[idx].v_type != VAR_STRING)
977 {
rbtnnddc80af2021-12-17 18:01:31 +0000978 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000979 return FAIL;
980 }
981 return OK;
982}
983
984/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200985 * Give an error and return FAIL unless "args[idx]" is an optional buffer
986 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200987 */
988 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200989check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200990{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200991 if (args[idx].v_type != VAR_UNKNOWN
992 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200993 && args[idx].v_type != VAR_NUMBER
994 && args[idx].v_type != VAR_DICT)
995 {
rbtnnddc80af2021-12-17 18:01:31 +0000996 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200997 return FAIL;
998 }
999 return OK;
1000}
1001
1002/*
LemonBoyafe04662023-08-23 21:08:11 +02001003 * Give an error and return FAIL unless "args[idx]" is an object.
1004 */
1005 int
1006check_for_object_arg(typval_T *args, int idx)
1007{
1008 if (args[idx].v_type != VAR_OBJECT)
1009 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001010 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001011 return FAIL;
1012 }
1013 return OK;
1014}
1015
1016/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001017 * Returns TRUE if "tv" is a type alias for a class
1018 */
1019 int
1020tv_class_alias(typval_T *tv)
1021{
1022 return tv->v_type == VAR_TYPEALIAS &&
1023 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1024}
1025
1026/*
Ernie Rael2025af12023-12-12 16:58:00 +01001027 * Give an error and return FAIL unless "args[idx]" is a class
1028 * or class typealias.
LemonBoyafe04662023-08-23 21:08:11 +02001029 */
1030 int
Ernie Rael2025af12023-12-12 16:58:00 +01001031check_for_class_or_typealias_args(typval_T *args, int idx)
LemonBoyafe04662023-08-23 21:08:11 +02001032{
Ernie Rael2025af12023-12-12 16:58:00 +01001033 for (int i = idx; args[i].v_type != VAR_UNKNOWN; ++i)
LemonBoyafe04662023-08-23 21:08:11 +02001034 {
Ernie Rael2025af12023-12-12 16:58:00 +01001035 if (args[i].v_type != VAR_CLASS && !tv_class_alias(&args[idx]))
1036 {
1037 semsg(_(e_class_or_typealias_required_for_argument_nr), i + 1);
1038 return FAIL;
1039 }
LemonBoyafe04662023-08-23 21:08:11 +02001040 }
1041 return OK;
1042}
1043
1044/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001045 * Get the string value of a variable.
1046 * If it is a Number variable, the number is converted into a string.
1047 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1048 * tv_get_string_buf() uses a given buffer.
1049 * If the String variable has never been set, return an empty string.
1050 * Never returns NULL;
1051 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1052 * NULL on error.
1053 */
1054 char_u *
1055tv_get_string(typval_T *varp)
1056{
1057 static char_u mybuf[NUMBUFLEN];
1058
1059 return tv_get_string_buf(varp, mybuf);
1060}
1061
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001062/*
1063 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1064 */
1065 char_u *
1066tv_get_string_strict(typval_T *varp)
1067{
1068 static char_u mybuf[NUMBUFLEN];
1069 char_u *res = tv_get_string_buf_chk_strict(
1070 varp, mybuf, in_vim9script());
1071
1072 return res != NULL ? res : (char_u *)"";
1073}
1074
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001075 char_u *
1076tv_get_string_buf(typval_T *varp, char_u *buf)
1077{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001078 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001079
1080 return res != NULL ? res : (char_u *)"";
1081}
1082
1083/*
1084 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1085 */
1086 char_u *
1087tv_get_string_chk(typval_T *varp)
1088{
1089 static char_u mybuf[NUMBUFLEN];
1090
1091 return tv_get_string_buf_chk(varp, mybuf);
1092}
1093
1094 char_u *
1095tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1096{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001097 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1098}
1099
1100 char_u *
1101tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1102{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001103 switch (varp->v_type)
1104 {
1105 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001106 if (strict)
1107 {
1108 emsg(_(e_using_number_as_string));
1109 break;
1110 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001111 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1112 (varnumber_T)varp->vval.v_number);
1113 return buf;
1114 case VAR_FUNC:
1115 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001116 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001117 break;
1118 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001119 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001120 break;
1121 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001122 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001123 break;
1124 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001125 if (strict)
1126 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001127 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001128 break;
1129 }
1130 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1131 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001132 case VAR_STRING:
1133 if (varp->vval.v_string != NULL)
1134 return varp->vval.v_string;
1135 return (char_u *)"";
1136 case VAR_BOOL:
1137 case VAR_SPECIAL:
1138 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1139 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001140 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001141 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001143 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +01001144 case VAR_TYPEALIAS:
1145 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001146 break;
1147 case VAR_OBJECT:
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001148 {
1149 class_T *cl = varp->vval.v_object->obj_class;
1150 if (cl != NULL && IS_ENUM(cl))
1151 semsg(_(e_using_enum_str_as_string), cl->class_name);
1152 else
1153 emsg(_(e_using_object_as_string));
1154 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001155 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001156 case VAR_JOB:
1157#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001158 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001159 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001160 semsg(_(e_using_invalid_value_as_string_str), "job");
1161 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001162 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001163 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001164#endif
1165 break;
1166 case VAR_CHANNEL:
1167#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001168 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001169 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001170 semsg(_(e_using_invalid_value_as_string_str), "channel");
1171 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001172 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001173 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001174#endif
1175 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001176 case VAR_VOID:
1177 emsg(_(e_cannot_use_void_value));
1178 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001179 case VAR_UNKNOWN:
1180 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001181 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001182 semsg(_(e_using_invalid_value_as_string_str),
1183 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001184 break;
1185 }
1186 return NULL;
1187}
1188
1189/*
1190 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1191 * string() on Dict, List, etc.
1192 */
1193 char_u *
1194tv_stringify(typval_T *varp, char_u *buf)
1195{
1196 if (varp->v_type == VAR_LIST
1197 || varp->v_type == VAR_DICT
1198 || varp->v_type == VAR_BLOB
1199 || varp->v_type == VAR_FUNC
1200 || varp->v_type == VAR_PARTIAL
1201 || varp->v_type == VAR_FLOAT)
1202 {
1203 typval_T tmp;
1204
1205 f_string(varp, &tmp);
1206 tv_get_string_buf(&tmp, buf);
1207 clear_tv(varp);
1208 *varp = tmp;
1209 return tmp.vval.v_string;
1210 }
1211 return tv_get_string_buf(varp, buf);
1212}
1213
1214/*
1215 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1216 * Also give an error message, using "name" or _("name") when use_gettext is
1217 * TRUE.
1218 */
1219 int
1220tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1221{
1222 int lock = 0;
1223
1224 switch (tv->v_type)
1225 {
1226 case VAR_BLOB:
1227 if (tv->vval.v_blob != NULL)
1228 lock = tv->vval.v_blob->bv_lock;
1229 break;
1230 case VAR_LIST:
1231 if (tv->vval.v_list != NULL)
1232 lock = tv->vval.v_list->lv_lock;
1233 break;
1234 case VAR_DICT:
1235 if (tv->vval.v_dict != NULL)
1236 lock = tv->vval.v_dict->dv_lock;
1237 break;
1238 default:
1239 break;
1240 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001241 return value_check_lock(tv->v_lock, name, use_gettext)
1242 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001243}
1244
1245/*
1246 * Copy the values from typval_T "from" to typval_T "to".
1247 * When needed allocates string or increases reference count.
1248 * Does not make a copy of a list, blob or dict but copies the reference!
1249 * It is OK for "from" and "to" to point to the same item. This is used to
1250 * make a copy later.
1251 */
1252 void
1253copy_tv(typval_T *from, typval_T *to)
1254{
1255 to->v_type = from->v_type;
1256 to->v_lock = 0;
1257 switch (from->v_type)
1258 {
1259 case VAR_NUMBER:
1260 case VAR_BOOL:
1261 case VAR_SPECIAL:
1262 to->vval.v_number = from->vval.v_number;
1263 break;
1264 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001265 to->vval.v_float = from->vval.v_float;
1266 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001267 case VAR_JOB:
1268#ifdef FEAT_JOB_CHANNEL
1269 to->vval.v_job = from->vval.v_job;
1270 if (to->vval.v_job != NULL)
1271 ++to->vval.v_job->jv_refcount;
1272 break;
1273#endif
1274 case VAR_CHANNEL:
1275#ifdef FEAT_JOB_CHANNEL
1276 to->vval.v_channel = from->vval.v_channel;
1277 if (to->vval.v_channel != NULL)
1278 ++to->vval.v_channel->ch_refcount;
1279 break;
1280#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001281 case VAR_INSTR:
1282 to->vval.v_instr = from->vval.v_instr;
1283 break;
1284
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001285 case VAR_CLASS:
1286 copy_class(from, to);
1287 break;
1288
1289 case VAR_OBJECT:
1290 copy_object(from, to);
1291 break;
1292
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001293 case VAR_STRING:
1294 case VAR_FUNC:
1295 if (from->vval.v_string == NULL)
1296 to->vval.v_string = NULL;
1297 else
1298 {
1299 to->vval.v_string = vim_strsave(from->vval.v_string);
1300 if (from->v_type == VAR_FUNC)
1301 func_ref(to->vval.v_string);
1302 }
1303 break;
1304 case VAR_PARTIAL:
1305 if (from->vval.v_partial == NULL)
1306 to->vval.v_partial = NULL;
1307 else
1308 {
1309 to->vval.v_partial = from->vval.v_partial;
1310 ++to->vval.v_partial->pt_refcount;
1311 }
1312 break;
1313 case VAR_BLOB:
1314 if (from->vval.v_blob == NULL)
1315 to->vval.v_blob = NULL;
1316 else
1317 {
1318 to->vval.v_blob = from->vval.v_blob;
1319 ++to->vval.v_blob->bv_refcount;
1320 }
1321 break;
1322 case VAR_LIST:
1323 if (from->vval.v_list == NULL)
1324 to->vval.v_list = NULL;
1325 else
1326 {
1327 to->vval.v_list = from->vval.v_list;
1328 ++to->vval.v_list->lv_refcount;
1329 }
1330 break;
1331 case VAR_DICT:
1332 if (from->vval.v_dict == NULL)
1333 to->vval.v_dict = NULL;
1334 else
1335 {
1336 to->vval.v_dict = from->vval.v_dict;
1337 ++to->vval.v_dict->dv_refcount;
1338 }
1339 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001340 case VAR_TYPEALIAS:
1341 if (from->vval.v_typealias == NULL)
1342 to->vval.v_typealias = NULL;
1343 else
1344 {
1345 to->vval.v_typealias = from->vval.v_typealias;
1346 ++to->vval.v_typealias->ta_refcount;
1347 }
1348 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001349 case VAR_VOID:
1350 emsg(_(e_cannot_use_void_value));
1351 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001352 case VAR_UNKNOWN:
1353 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001354 internal_error_no_abort("copy_tv(UNKNOWN)");
1355 break;
1356 }
1357}
1358
1359/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001360 * Compare "tv1" and "tv2".
1361 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001362 */
1363 int
1364typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001365 typval_T *tv1, // first operand
1366 typval_T *tv2, // second operand
1367 exprtype_T type, // operator
1368 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001369{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001370 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001371 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001372 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1373
Ernie Raele75fde62023-12-21 17:18:54 +01001374 if (check_typval_is_value(tv1) == FAIL
1375 || check_typval_is_value(tv2) == FAIL)
1376 {
1377 clear_tv(tv1);
1378 return FAIL;
1379 }
1380 else if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001381 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001382 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001383 // it means TRUE.
1384 n1 = (type == EXPR_ISNOT);
1385 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001386 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1387 || (tv2->v_type == VAR_SPECIAL
1388 && tv2->vval.v_number == VVAL_NULL))
1389 && tv1->v_type != tv2->v_type
1390 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1391 {
1392 n1 = typval_compare_null(tv1, tv2);
1393 if (n1 == MAYBE)
1394 {
1395 clear_tv(tv1);
1396 return FAIL;
1397 }
1398 if (type == EXPR_NEQUAL)
1399 n1 = !n1;
1400 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001401 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001402 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001403 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001404 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001405 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001406 return FAIL;
1407 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001408 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001409 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001410 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001411 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001412 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001413 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001414 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001415 return FAIL;
1416 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001417 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001418 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001419 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1420 {
1421 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1422 {
1423 clear_tv(tv1);
1424 return FAIL;
1425 }
1426 n1 = res;
1427 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001428 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001429 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001430 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001431 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001432 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433 return FAIL;
1434 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001435 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001436 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001437 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1438 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001439 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001440 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001441 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001442 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001443 return FAIL;
1444 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001445 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001446 }
1447
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001448 // If one of the two variables is a float, compare as a float.
1449 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001450 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001451 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1452 {
1453 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001454 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001455
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001456 f1 = tv_get_float_chk(tv1, &error);
1457 if (!error)
1458 f2 = tv_get_float_chk(tv2, &error);
1459 if (error)
1460 {
1461 clear_tv(tv1);
1462 return FAIL;
1463 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001464 n1 = FALSE;
1465 switch (type)
1466 {
1467 case EXPR_IS:
1468 case EXPR_EQUAL: n1 = (f1 == f2); break;
1469 case EXPR_ISNOT:
1470 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1471 case EXPR_GREATER: n1 = (f1 > f2); break;
1472 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1473 case EXPR_SMALLER: n1 = (f1 < f2); break;
1474 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1475 case EXPR_UNKNOWN:
1476 case EXPR_MATCH:
1477 default: break; // avoid gcc warning
1478 }
1479 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001480
1481 // If one of the two variables is a number, compare as a number.
1482 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001483 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001484 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1485 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001486 int error = FALSE;
1487
1488 n1 = tv_get_number_chk(tv1, &error);
1489 if (!error)
1490 n2 = tv_get_number_chk(tv2, &error);
1491 if (error)
1492 {
1493 clear_tv(tv1);
1494 return FAIL;
1495 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001496 switch (type)
1497 {
1498 case EXPR_IS:
1499 case EXPR_EQUAL: n1 = (n1 == n2); break;
1500 case EXPR_ISNOT:
1501 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1502 case EXPR_GREATER: n1 = (n1 > n2); break;
1503 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1504 case EXPR_SMALLER: n1 = (n1 < n2); break;
1505 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1506 case EXPR_UNKNOWN:
1507 case EXPR_MATCH:
1508 default: break; // avoid gcc warning
1509 }
1510 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001511 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1512 || tv2->v_type == VAR_BOOL
1513 || (tv1->v_type == VAR_SPECIAL
1514 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001515 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001516 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001517 {
1518 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001519 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1520 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001521 return FAIL;
1522 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001523 n1 = tv1->vval.v_number;
1524 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001525 switch (type)
1526 {
1527 case EXPR_IS:
1528 case EXPR_EQUAL: n1 = (n1 == n2); break;
1529 case EXPR_ISNOT:
1530 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1531 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001532 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001533 vartype_name(tv1->v_type));
1534 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001535 return FAIL;
1536 }
1537 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001538#ifdef FEAT_JOB_CHANNEL
1539 else if (tv1->v_type == tv2->v_type
1540 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1541 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1542 {
1543 if (tv1->v_type == VAR_CHANNEL)
1544 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1545 else
1546 n1 = tv1->vval.v_job == tv2->vval.v_job;
1547 if (type == EXPR_NEQUAL)
1548 n1 = !n1;
1549 }
1550#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001551 else
1552 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001553 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001554 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001555 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001556 return FAIL;
1557 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001558 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001559 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001560 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001561 if (in_vim9script())
1562 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001563 tv1->v_type = VAR_BOOL;
1564 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001565 }
1566 else
1567 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001568 tv1->v_type = VAR_NUMBER;
1569 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001570 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001571
1572 return OK;
1573}
1574
Bram Moolenaar34453202021-01-31 13:08:38 +01001575/*
dundargocc57b5bc2022-11-02 13:30:51 +00001576 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001577 * Put the result, false or true, in "res".
1578 * Return FAIL and give an error message when the comparison can't be done.
1579 */
1580 int
1581typval_compare_list(
1582 typval_T *tv1,
1583 typval_T *tv2,
1584 exprtype_T type,
1585 int ic,
1586 int *res)
1587{
1588 int val = 0;
1589
1590 if (type == EXPR_IS || type == EXPR_ISNOT)
1591 {
1592 val = (tv1->v_type == tv2->v_type
1593 && tv1->vval.v_list == tv2->vval.v_list);
1594 if (type == EXPR_ISNOT)
1595 val = !val;
1596 }
1597 else if (tv1->v_type != tv2->v_type
1598 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1599 {
1600 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001601 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001602 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001603 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001604 return FAIL;
1605 }
1606 else
1607 {
1608 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1609 ic, FALSE);
1610 if (type == EXPR_NEQUAL)
1611 val = !val;
1612 }
1613 *res = val;
1614 return OK;
1615}
1616
1617/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001618 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001619 */
1620 int
1621typval_compare_null(typval_T *tv1, typval_T *tv2)
1622{
1623 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1624 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1625 {
1626 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1627
1628 switch (tv->v_type)
1629 {
1630 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001631#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001632 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001633#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001634 // TODO: null_class handling
1635 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001636 case VAR_DICT: return tv->vval.v_dict == NULL;
1637 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001638#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001639 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001640#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001641 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001642 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001643 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1644 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001645
1646 case VAR_NUMBER: if (!in_vim9script())
1647 return tv->vval.v_number == 0;
1648 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001649 case VAR_FLOAT: if (!in_vim9script())
1650 return tv->vval.v_float == 0.0;
1651 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001652 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001653 default: break;
1654 }
1655 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001656 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001657 // we won't give an error
1658 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001659}
1660
1661/*
dundargocc57b5bc2022-11-02 13:30:51 +00001662 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001663 * Put the result, false or true, in "res".
1664 * Return FAIL and give an error message when the comparison can't be done.
1665 */
1666 int
1667typval_compare_blob(
1668 typval_T *tv1,
1669 typval_T *tv2,
1670 exprtype_T type,
1671 int *res)
1672{
1673 int val = 0;
1674
1675 if (type == EXPR_IS || type == EXPR_ISNOT)
1676 {
1677 val = (tv1->v_type == tv2->v_type
1678 && tv1->vval.v_blob == tv2->vval.v_blob);
1679 if (type == EXPR_ISNOT)
1680 val = !val;
1681 }
1682 else if (tv1->v_type != tv2->v_type
1683 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1684 {
1685 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001686 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001687 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001688 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001689 return FAIL;
1690 }
1691 else
1692 {
1693 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1694 if (type == EXPR_NEQUAL)
1695 val = !val;
1696 }
1697 *res = val;
1698 return OK;
1699}
1700
1701/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001702 * Compare "tv1" to "tv2" as classes according to "type".
1703 * Put the result, false or true, in "res".
1704 * Return FAIL and give an error message when the comparison can't be done.
1705 */
1706 int
1707typval_compare_class(
1708 typval_T *tv1,
1709 typval_T *tv2,
1710 exprtype_T type UNUSED,
1711 int ic UNUSED,
1712 int *res)
1713{
1714 // TODO: use "type"
1715 *res = tv1->vval.v_class == tv2->vval.v_class;
1716 return OK;
1717}
1718
1719/*
1720 * Compare "tv1" to "tv2" as objects according to "type".
1721 * Put the result, false or true, in "res".
1722 * Return FAIL and give an error message when the comparison can't be done.
1723 */
1724 int
1725typval_compare_object(
1726 typval_T *tv1,
1727 typval_T *tv2,
1728 exprtype_T type,
1729 int ic,
1730 int *res)
1731{
1732 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1733
1734 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1735 {
1736 *res = res_match;
1737 return OK;
1738 }
1739 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1740 {
1741 *res = !res_match;
1742 return OK;
1743 }
1744
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001745 object_T *obj1 = tv1->vval.v_object;
1746 object_T *obj2 = tv2->vval.v_object;
1747 if (type == EXPR_IS || type == EXPR_ISNOT)
1748 {
1749 *res = obj1 == obj2 ? res_match : !res_match;
1750 return OK;
1751 }
1752
LemonBoy7b29cc92024-06-22 17:25:07 +02001753 *res = object_equal(obj1, obj2, ic, FALSE) ? res_match : !res_match;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001754 return OK;
1755}
1756
1757/*
dundargocc57b5bc2022-11-02 13:30:51 +00001758 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001759 * Put the result, false or true, in "res".
1760 * Return FAIL and give an error message when the comparison can't be done.
1761 */
1762 int
1763typval_compare_dict(
1764 typval_T *tv1,
1765 typval_T *tv2,
1766 exprtype_T type,
1767 int ic,
1768 int *res)
1769{
1770 int val;
1771
1772 if (type == EXPR_IS || type == EXPR_ISNOT)
1773 {
1774 val = (tv1->v_type == tv2->v_type
1775 && tv1->vval.v_dict == tv2->vval.v_dict);
1776 if (type == EXPR_ISNOT)
1777 val = !val;
1778 }
1779 else if (tv1->v_type != tv2->v_type
1780 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1781 {
1782 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001783 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001784 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001785 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001786 return FAIL;
1787 }
1788 else
1789 {
1790 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1791 if (type == EXPR_NEQUAL)
1792 val = !val;
1793 }
1794 *res = val;
1795 return OK;
1796}
1797
1798/*
dundargocc57b5bc2022-11-02 13:30:51 +00001799 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001800 * Put the result, false or true, in "res".
1801 * Return FAIL and give an error message when the comparison can't be done.
1802 */
1803 int
1804typval_compare_func(
1805 typval_T *tv1,
1806 typval_T *tv2,
1807 exprtype_T type,
1808 int ic,
1809 int *res)
1810{
1811 int val = 0;
1812
1813 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1814 && type != EXPR_IS && type != EXPR_ISNOT)
1815 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001816 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001817 return FAIL;
1818 }
1819 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1820 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1821 // When both partials are NULL, then they are equal.
1822 // Otherwise they are not equal.
1823 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1824 else if (type == EXPR_IS || type == EXPR_ISNOT)
1825 {
1826 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1827 // strings are considered the same if their value is
1828 // the same
1829 val = tv_equal(tv1, tv2, ic, FALSE);
1830 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1831 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1832 else
1833 val = FALSE;
1834 }
1835 else
1836 val = tv_equal(tv1, tv2, ic, FALSE);
1837 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1838 val = !val;
1839 *res = val;
1840 return OK;
1841}
1842
1843/*
1844 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1845 * Put the result, false or true, in "res".
1846 * Return FAIL and give an error message when the comparison can't be done.
1847 */
1848 int
1849typval_compare_string(
1850 typval_T *tv1,
1851 typval_T *tv2,
1852 exprtype_T type,
1853 int ic,
1854 int *res)
1855{
1856 int i = 0;
1857 int val = FALSE;
1858 char_u *s1, *s2;
1859 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1860
1861 if (in_vim9script()
1862 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1863 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1864 {
1865 semsg(_(e_cannot_compare_str_with_str),
1866 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1867 return FAIL;
1868 }
1869 s1 = tv_get_string_buf(tv1, buf1);
1870 s2 = tv_get_string_buf(tv2, buf2);
1871 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1872 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1873 switch (type)
1874 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001875 case EXPR_IS: if (in_vim9script())
1876 {
1877 // Really check it is the same string, not just
1878 // the same value.
1879 val = tv1->vval.v_string == tv2->vval.v_string;
1880 break;
1881 }
1882 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001883 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001884 case EXPR_ISNOT: if (in_vim9script())
1885 {
1886 // Really check it is not the same string, not
1887 // just a different value.
1888 val = tv1->vval.v_string != tv2->vval.v_string;
1889 break;
1890 }
1891 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001892 case EXPR_NEQUAL: val = (i != 0); break;
1893 case EXPR_GREATER: val = (i > 0); break;
1894 case EXPR_GEQUAL: val = (i >= 0); break;
1895 case EXPR_SMALLER: val = (i < 0); break;
1896 case EXPR_SEQUAL: val = (i <= 0); break;
1897
1898 case EXPR_MATCH:
1899 case EXPR_NOMATCH:
1900 val = pattern_match(s2, s1, ic);
1901 if (type == EXPR_NOMATCH)
1902 val = !val;
1903 break;
1904
1905 default: break; // avoid gcc warning
1906 }
1907 *res = val;
1908 return OK;
1909}
1910/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001911 * Convert any type to a string, never give an error.
1912 * When "quotes" is TRUE add quotes to a string.
1913 * Returns an allocated string.
1914 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001915 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001916typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001917{
1918 char_u *tofree;
1919 char_u numbuf[NUMBUFLEN];
1920 char_u *ret = NULL;
1921
1922 if (arg == NULL)
1923 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001924 if (!quotes && arg->v_type == VAR_STRING)
1925 {
1926 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1927 : arg->vval.v_string);
1928 }
1929 else
1930 {
1931 ret = tv2string(arg, &tofree, numbuf, 0);
1932 // Make a copy if we have a value but it's not in allocated memory.
1933 if (ret != NULL && tofree == NULL)
1934 ret = vim_strsave(ret);
1935 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001936 return ret;
1937}
1938
1939/*
1940 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1941 * or it refers to a List or Dictionary that is locked.
1942 */
1943 int
1944tv_islocked(typval_T *tv)
1945{
1946 return (tv->v_lock & VAR_LOCKED)
1947 || (tv->v_type == VAR_LIST
1948 && tv->vval.v_list != NULL
1949 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1950 || (tv->v_type == VAR_DICT
1951 && tv->vval.v_dict != NULL
1952 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1953}
1954
1955 static int
1956func_equal(
1957 typval_T *tv1,
1958 typval_T *tv2,
1959 int ic) // ignore case
1960{
1961 char_u *s1, *s2;
1962 dict_T *d1, *d2;
1963 int a1, a2;
1964 int i;
1965
1966 // empty and NULL function name considered the same
1967 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1968 : partial_name(tv1->vval.v_partial);
1969 if (s1 != NULL && *s1 == NUL)
1970 s1 = NULL;
1971 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1972 : partial_name(tv2->vval.v_partial);
1973 if (s2 != NULL && *s2 == NUL)
1974 s2 = NULL;
1975 if (s1 == NULL || s2 == NULL)
1976 {
1977 if (s1 != s2)
1978 return FALSE;
1979 }
1980 else if (STRCMP(s1, s2) != 0)
1981 return FALSE;
1982
1983 // empty dict and NULL dict is different
1984 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1985 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1986 if (d1 == NULL || d2 == NULL)
1987 {
1988 if (d1 != d2)
1989 return FALSE;
1990 }
1991 else if (!dict_equal(d1, d2, ic, TRUE))
1992 return FALSE;
1993
1994 // empty list and no list considered the same
1995 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1996 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1997 if (a1 != a2)
1998 return FALSE;
1999 for (i = 0; i < a1; ++i)
2000 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
2001 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
2002 return FALSE;
2003
2004 return TRUE;
2005}
2006
2007/*
2008 * Return TRUE if "tv1" and "tv2" have the same value.
2009 * Compares the items just like "==" would compare them, but strings and
2010 * numbers are different. Floats and numbers are also different.
2011 */
2012 int
2013tv_equal(
2014 typval_T *tv1,
2015 typval_T *tv2,
2016 int ic, // ignore case
2017 int recursive) // TRUE when used recursively
2018{
2019 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2020 char_u *s1, *s2;
2021 static int recursive_cnt = 0; // catch recursive loops
2022 int r;
2023 static int tv_equal_recurse_limit;
2024
2025 // Catch lists and dicts that have an endless loop by limiting
2026 // recursiveness to a limit. We guess they are equal then.
2027 // A fixed limit has the problem of still taking an awful long time.
2028 // Reduce the limit every time running into it. That should work fine for
2029 // deeply linked structures that are not recursively linked and catch
2030 // recursiveness quickly.
2031 if (!recursive)
2032 tv_equal_recurse_limit = 1000;
2033 if (recursive_cnt >= tv_equal_recurse_limit)
2034 {
2035 --tv_equal_recurse_limit;
2036 return TRUE;
2037 }
2038
2039 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2040 // arguments.
2041 if ((tv1->v_type == VAR_FUNC
2042 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2043 && (tv2->v_type == VAR_FUNC
2044 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2045 {
2046 ++recursive_cnt;
2047 r = func_equal(tv1, tv2, ic);
2048 --recursive_cnt;
2049 return r;
2050 }
2051
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002052 if (tv1->v_type != tv2->v_type
2053 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2054 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002055 return FALSE;
2056
2057 switch (tv1->v_type)
2058 {
2059 case VAR_LIST:
2060 ++recursive_cnt;
2061 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2062 --recursive_cnt;
2063 return r;
2064
2065 case VAR_DICT:
2066 ++recursive_cnt;
2067 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2068 --recursive_cnt;
2069 return r;
2070
2071 case VAR_BLOB:
2072 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2073
2074 case VAR_NUMBER:
2075 case VAR_BOOL:
2076 case VAR_SPECIAL:
2077 return tv1->vval.v_number == tv2->vval.v_number;
2078
2079 case VAR_STRING:
2080 s1 = tv_get_string_buf(tv1, buf1);
2081 s2 = tv_get_string_buf(tv2, buf2);
2082 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2083
2084 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002085 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002086 case VAR_JOB:
2087#ifdef FEAT_JOB_CHANNEL
2088 return tv1->vval.v_job == tv2->vval.v_job;
2089#endif
2090 case VAR_CHANNEL:
2091#ifdef FEAT_JOB_CHANNEL
2092 return tv1->vval.v_channel == tv2->vval.v_channel;
2093#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002094 case VAR_INSTR:
2095 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002096
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002097 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002098 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002099 return tv1->vval.v_class == tv2->vval.v_class;
2100
2101 case VAR_OBJECT:
Ernie Raelf0e69142024-06-22 11:12:00 +02002102 ++recursive_cnt;
LemonBoy7b29cc92024-06-22 17:25:07 +02002103 r = object_equal(tv1->vval.v_object, tv2->vval.v_object, ic, TRUE);
Ernie Raelf0e69142024-06-22 11:12:00 +02002104 --recursive_cnt;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002105 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002106
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107 case VAR_PARTIAL:
2108 return tv1->vval.v_partial == tv2->vval.v_partial;
2109
2110 case VAR_FUNC:
2111 return tv1->vval.v_string == tv2->vval.v_string;
2112
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002113 case VAR_TYPEALIAS:
2114 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2115
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002116 case VAR_UNKNOWN:
2117 case VAR_ANY:
2118 case VAR_VOID:
2119 break;
2120 }
2121
2122 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2123 // does not equal anything, not even itself.
2124 return FALSE;
2125}
2126
2127/*
2128 * Get an option value.
2129 * "arg" points to the '&' or '+' before the option name.
2130 * "arg" is advanced to character after the option name.
2131 * Return OK or FAIL.
2132 */
2133 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002134eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002135 char_u **arg,
2136 typval_T *rettv, // when NULL, only check if option exists
2137 int evaluate)
2138{
2139 char_u *option_end;
2140 long numval;
2141 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002142 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002143 int c;
2144 int working = (**arg == '+'); // has("+option")
2145 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002146 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002147
2148 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002149 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002150 if (option_end == NULL)
2151 {
2152 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002153 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002154 return FAIL;
2155 }
2156
2157 if (!evaluate)
2158 {
2159 *arg = option_end;
2160 return OK;
2161 }
2162
2163 c = *option_end;
2164 *option_end = NUL;
2165 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002166 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002167
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002168 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002169 {
2170 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002171 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002172 ret = FAIL;
2173 }
2174 else if (rettv != NULL)
2175 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002176 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002177 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002178 {
2179 rettv->v_type = VAR_STRING;
2180 rettv->vval.v_string = NULL;
2181 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002182 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002183 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002184 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2185 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002186 rettv->vval.v_number = 0;
2187 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002188 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002190 if (in_vim9script() && opt_type == gov_bool)
2191 {
2192 rettv->v_type = VAR_BOOL;
2193 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2194 }
2195 else
2196 {
2197 rettv->v_type = VAR_NUMBER;
2198 rettv->vval.v_number = numval;
2199 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002200 }
2201 else // string option
2202 {
2203 rettv->v_type = VAR_STRING;
2204 rettv->vval.v_string = stringval;
2205 }
2206 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002207 else if (working && (opt_type == gov_hidden_bool
2208 || opt_type == gov_hidden_number
2209 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002210 ret = FAIL;
2211
2212 *option_end = c; // put back for error messages
2213 *arg = option_end;
2214
2215 return ret;
2216}
2217
2218/*
2219 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2220 * Return OK or FAIL.
2221 */
2222 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002223eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002224 char_u **arg,
2225 typval_T *rettv,
2226 int evaluate,
2227 int want_string UNUSED)
2228{
2229 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002230 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002231 char_u *p;
2232 int get_float = FALSE;
2233
2234 // We accept a float when the format matches
2235 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2236 // strict to avoid backwards compatibility problems.
2237 // With script version 2 and later the leading digit can be
2238 // omitted.
2239 // Don't look for a float after the "." operator, so that
2240 // ":let vers = 1.2.3" doesn't fail.
2241 if (**arg == '.')
2242 p = *arg;
2243 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002244 {
2245 p = *arg + 1;
2246 if (skip_quotes)
2247 for (;;)
2248 {
2249 if (*p == '\'')
2250 ++p;
2251 if (!vim_isdigit(*p))
2252 break;
2253 p = skipdigits(p);
2254 }
2255 else
2256 p = skipdigits(p);
2257 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002258 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2259 {
2260 get_float = TRUE;
2261 p = skipdigits(p + 2);
2262 if (*p == 'e' || *p == 'E')
2263 {
2264 ++p;
2265 if (*p == '-' || *p == '+')
2266 ++p;
2267 if (!vim_isdigit(*p))
2268 get_float = FALSE;
2269 else
2270 p = skipdigits(p + 1);
2271 }
2272 if (ASCII_ISALPHA(*p) || *p == '.')
2273 get_float = FALSE;
2274 }
2275 if (get_float)
2276 {
2277 float_T f;
2278
Bram Moolenaar29500652021-08-08 15:43:34 +02002279 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002280 if (evaluate)
2281 {
2282 rettv->v_type = VAR_FLOAT;
2283 rettv->vval.v_float = f;
2284 }
2285 }
2286 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002287 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2288 {
2289 char_u *bp;
2290 blob_T *blob = NULL; // init for gcc
2291
2292 // Blob constant: 0z0123456789abcdef
2293 if (evaluate)
2294 blob = blob_alloc();
2295 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2296 {
2297 if (!vim_isxdigit(bp[1]))
2298 {
2299 if (blob != NULL)
2300 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002301 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002302 ga_clear(&blob->bv_ga);
2303 VIM_CLEAR(blob);
2304 }
2305 return FAIL;
2306 }
2307 if (blob != NULL)
2308 ga_append(&blob->bv_ga,
2309 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2310 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2311 ++bp;
2312 }
2313 if (blob != NULL)
2314 rettv_blob_set(rettv, blob);
2315 *arg = bp;
2316 }
2317 else
2318 {
2319 varnumber_T n;
2320
2321 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002322 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002323 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002324 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002325 if (len == 0)
2326 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002327 if (evaluate)
2328 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002329 return FAIL;
2330 }
2331 *arg += len;
2332 if (evaluate)
2333 {
2334 rettv->v_type = VAR_NUMBER;
2335 rettv->vval.v_number = n;
2336 }
2337 }
2338 return OK;
2339}
2340
2341/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002342 * Evaluate a string constant and put the result in "rettv".
2343 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2344 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2345 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002346 * Return OK or FAIL.
2347 */
2348 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002349eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002350{
2351 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002352 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002353 int extra = interpolate ? 1 : 0;
2354 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002355 int len;
2356
2357 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002358 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002359 {
2360 if (*p == '\\' && p[1] != NUL)
2361 {
2362 ++p;
2363 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002364 // to 9 characters (6 for the char and 3 for a modifier):
2365 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002366 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002367 {
2368 int modifiers = 0;
2369 int flags = FSK_KEYCODE | FSK_IN_STRING;
2370
zeertzjqdb088872022-05-02 22:53:45 +01002371 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002372
2373 // Skip to the '>' to avoid using '{' inside for string
2374 // interpolation.
2375 if (p[1] != '*')
2376 flags |= FSK_SIMPLIFY;
2377 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2378 --p; // leave "p" on the ">"
2379 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002380 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002381 else if (interpolate && (*p == '{' || *p == '}'))
2382 {
2383 if (*p == '{' && p[1] != '{') // start of expression
2384 break;
2385 ++p;
2386 if (p[-1] == '}' && *p != '}') // single '}' is an error
2387 {
2388 semsg(_(e_stray_closing_curly_str), *arg);
2389 return FAIL;
2390 }
2391 --extra; // "{{" becomes "{", "}}" becomes "}"
2392 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002393 }
2394
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002395 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002396 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002397 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002398 return FAIL;
2399 }
2400
2401 // If only parsing, set *arg and return here
2402 if (!evaluate)
2403 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002404 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002405 return OK;
2406 }
2407
2408 // Copy the string into allocated memory, handling backslashed
2409 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002410 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002411 len = (int)(p - *arg + extra);
2412 rettv->vval.v_string = alloc(len);
2413 if (rettv->vval.v_string == NULL)
2414 return FAIL;
2415 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002416
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002417 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002418 {
2419 if (*p == '\\')
2420 {
2421 switch (*++p)
2422 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002423 case 'b': *end++ = BS; ++p; break;
2424 case 'e': *end++ = ESC; ++p; break;
2425 case 'f': *end++ = FF; ++p; break;
2426 case 'n': *end++ = NL; ++p; break;
2427 case 'r': *end++ = CAR; ++p; break;
2428 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002429
2430 case 'X': // hex: "\x1", "\x12"
2431 case 'x':
2432 case 'u': // Unicode: "\u0023"
2433 case 'U':
2434 if (vim_isxdigit(p[1]))
2435 {
2436 int n, nr;
Keith Thompson184f71c2024-01-04 21:19:04 +01002437 int c = SAFE_toupper(*p);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002438
2439 if (c == 'X')
2440 n = 2;
2441 else if (*p == 'u')
2442 n = 4;
2443 else
2444 n = 8;
2445 nr = 0;
2446 while (--n >= 0 && vim_isxdigit(p[1]))
2447 {
2448 ++p;
2449 nr = (nr << 4) + hex2nr(*p);
2450 }
2451 ++p;
2452 // For "\u" store the number according to
2453 // 'encoding'.
2454 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002455 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002456 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002457 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002458 }
2459 break;
2460
2461 // octal: "\1", "\12", "\123"
2462 case '0':
2463 case '1':
2464 case '2':
2465 case '3':
2466 case '4':
2467 case '5':
2468 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002469 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002470 if (*p >= '0' && *p <= '7')
2471 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002472 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002473 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002474 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002475 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002476 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002477 break;
2478
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002479 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002480 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002481 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002482 int flags = FSK_KEYCODE | FSK_IN_STRING;
2483
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002484 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002485 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002486 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002487 if (extra != 0)
2488 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002489 end += extra;
2490 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002491 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002492 break;
2493 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002494 }
2495 // FALLTHROUGH
2496
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002497 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002498 break;
2499 }
2500 }
2501 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002502 {
2503 if (interpolate && (*p == '{' || *p == '}'))
2504 {
2505 if (*p == '{' && p[1] != '{') // start of expression
2506 break;
2507 ++p; // reduce "{{" to "{" and "}}" to "}"
2508 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002509 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002510 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002511 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002512 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002513 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002514 ++p;
2515 *arg = p;
2516
2517 return OK;
2518}
2519
2520/*
2521 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002522 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2523 * Return OK when a "rettv" was set to the string.
2524 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002525 */
2526 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002527eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002528{
2529 char_u *p;
2530 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002531 int reduce = interpolate ? -1 : 0;
2532 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002533
2534 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002535 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002536 {
2537 if (*p == '\'')
2538 {
2539 if (p[1] != '\'')
2540 break;
2541 ++reduce;
2542 ++p;
2543 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002544 else if (interpolate)
2545 {
2546 if (*p == '{')
2547 {
2548 if (p[1] != '{')
2549 break;
2550 ++p;
2551 ++reduce;
2552 }
2553 else if (*p == '}')
2554 {
2555 ++p;
2556 if (*p != '}')
2557 {
2558 semsg(_(e_stray_closing_curly_str), *arg);
2559 return FAIL;
2560 }
2561 ++reduce;
2562 }
2563 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002564 }
2565
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002566 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002567 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002568 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002569 return FAIL;
2570 }
2571
2572 // If only parsing return after setting "*arg"
2573 if (!evaluate)
2574 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002575 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002576 return OK;
2577 }
2578
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002579 // Copy the string into allocated memory, handling '' to ' reduction and
2580 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002581 str = alloc((p - *arg) - reduce);
2582 if (str == NULL)
2583 return FAIL;
2584 rettv->v_type = VAR_STRING;
2585 rettv->vval.v_string = str;
2586
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002587 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002588 {
2589 if (*p == '\'')
2590 {
2591 if (p[1] != '\'')
2592 break;
2593 ++p;
2594 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002595 else if (interpolate && (*p == '{' || *p == '}'))
2596 {
2597 if (*p == '{' && p[1] != '{')
2598 break;
2599 ++p;
2600 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002601 MB_COPY_CHAR(p, str);
2602 }
2603 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002604 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002605
2606 return OK;
2607}
2608
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002609/*
2610 * Evaluate a single or double quoted string possibly containing expressions.
2611 * "arg" points to the '$'. The result is put in "rettv".
2612 * Returns OK or FAIL.
2613 */
LemonBoy2eaef102022-05-06 13:14:50 +01002614 int
2615eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2616{
2617 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002618 int ret = OK;
2619 int quote;
2620 garray_T ga;
2621 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002622
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002623 ga_init2(&ga, 1, 80);
2624
2625 // *arg is on the '$' character, move it to the first string character.
2626 ++*arg;
2627 quote = **arg;
2628 ++*arg;
2629
2630 for (;;)
2631 {
2632 // Get the string up to the matching quote or to a single '{'.
2633 // "arg" is advanced to either the quote or the '{'.
2634 if (quote == '"')
2635 ret = eval_string(arg, &tv, evaluate, TRUE);
2636 else
2637 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2638 if (ret == FAIL)
2639 break;
2640 if (evaluate)
2641 {
2642 ga_concat(&ga, tv.vval.v_string);
2643 clear_tv(&tv);
2644 }
2645
2646 if (**arg != '{')
2647 {
2648 // found terminating quote
2649 ++*arg;
2650 break;
2651 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002652 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002653 if (p == NULL)
2654 {
2655 ret = FAIL;
2656 break;
2657 }
2658 *arg = p;
2659 }
LemonBoy2eaef102022-05-06 13:14:50 +01002660
2661 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002662 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2663 {
2664 ga_clear(&ga);
2665 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002666 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002667 }
LemonBoy2eaef102022-05-06 13:14:50 +01002668
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002669 rettv->vval.v_string = ga.ga_data;
2670 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002671}
2672
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002673/*
2674 * Return a string with the string representation of a variable.
2675 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2676 * "numbuf" is used for a number.
2677 * Puts quotes around strings, so that they can be parsed back by eval().
2678 * May return NULL.
2679 */
2680 char_u *
2681tv2string(
2682 typval_T *tv,
2683 char_u **tofree,
2684 char_u *numbuf,
2685 int copyID)
2686{
2687 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2688}
2689
2690/*
2691 * Get the value of an environment variable.
2692 * "arg" is pointing to the '$'. It is advanced to after the name.
2693 * If the environment variable was not set, silently assume it is empty.
2694 * Return FAIL if the name is invalid.
2695 */
2696 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002697eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002698{
2699 char_u *string = NULL;
2700 int len;
2701 int cc;
2702 char_u *name;
2703 int mustfree = FALSE;
2704
2705 ++*arg;
2706 name = *arg;
2707 len = get_env_len(arg);
2708 if (evaluate)
2709 {
2710 if (len == 0)
2711 return FAIL; // invalid empty name
2712
2713 cc = name[len];
2714 name[len] = NUL;
2715 // first try vim_getenv(), fast for normal environment vars
2716 string = vim_getenv(name, &mustfree);
2717 if (string != NULL && *string != NUL)
2718 {
2719 if (!mustfree)
2720 string = vim_strsave(string);
2721 }
2722 else
2723 {
2724 if (mustfree)
2725 vim_free(string);
2726
2727 // next try expanding things like $VIM and ${HOME}
2728 string = expand_env_save(name - 1);
2729 if (string != NULL && *string == '$')
2730 VIM_CLEAR(string);
2731 }
2732 name[len] = cc;
2733
2734 rettv->v_type = VAR_STRING;
2735 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002736 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002737 }
2738
2739 return OK;
2740}
2741
2742/*
2743 * Get the lnum from the first argument.
2744 * Also accepts ".", "$", etc., but that only works for the current buffer.
2745 * Returns -1 on error.
2746 */
2747 linenr_T
2748tv_get_lnum(typval_T *argvars)
2749{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002750 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002751 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002752
Bram Moolenaar56acb092020-08-16 14:48:19 +02002753 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2754 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002755 if (lnum <= 0 && did_emsg_before == did_emsg
2756 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002757 {
2758 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002759 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002760
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002761 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002762 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002763 if (fp != NULL)
2764 lnum = fp->lnum;
2765 }
2766 return lnum;
2767}
2768
2769/*
2770 * Get the lnum from the first argument.
2771 * Also accepts "$", then "buf" is used.
2772 * Returns 0 on error.
2773 */
2774 linenr_T
2775tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2776{
2777 if (argvars[0].v_type == VAR_STRING
2778 && argvars[0].vval.v_string != NULL
2779 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002780 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002781 && buf != NULL)
2782 return buf->b_ml.ml_line_count;
2783 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2784}
2785
2786/*
2787 * Get buffer by number or pattern.
2788 */
2789 buf_T *
2790tv_get_buf(typval_T *tv, int curtab_only)
2791{
2792 char_u *name = tv->vval.v_string;
2793 buf_T *buf;
2794
2795 if (tv->v_type == VAR_NUMBER)
2796 return buflist_findnr((int)tv->vval.v_number);
2797 if (tv->v_type != VAR_STRING)
2798 return NULL;
2799 if (name == NULL || *name == NUL)
2800 return curbuf;
2801 if (name[0] == '$' && name[1] == NUL)
2802 return lastbuf;
2803
2804 buf = buflist_find_by_name(name, curtab_only);
2805
2806 // If not found, try expanding the name, like done for bufexists().
2807 if (buf == NULL)
2808 buf = find_buffer(tv);
2809
2810 return buf;
2811}
2812
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002813/*
2814 * Like tv_get_buf() but give an error message is the type is wrong.
2815 */
2816 buf_T *
2817tv_get_buf_from_arg(typval_T *tv)
2818{
2819 buf_T *buf;
2820
2821 ++emsg_off;
2822 buf = tv_get_buf(tv, FALSE);
2823 --emsg_off;
2824 if (buf == NULL
2825 && tv->v_type != VAR_NUMBER
2826 && tv->v_type != VAR_STRING)
2827 // issue errmsg for type error
2828 (void)tv_get_number(tv);
2829 return buf;
2830}
2831
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002832#endif // FEAT_EVAL