blob: cd39a0d97316dc413d8c3be2a83e9f3d5e3cc0a8 [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 {
Ernie Raelbe828252024-07-26 18:37:02 +0200270 if (varp->vval.v_object == NULL)
271 emsg(_(e_using_object_as_string));
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100272 else
Ernie Raelbe828252024-07-26 18:37:02 +0200273 {
274 class_T *cl = varp->vval.v_object->obj_class;
275 if (cl != NULL && IS_ENUM(cl))
276 semsg(_(e_using_enum_str_as_number), cl->class_name);
277 else
278 emsg(_(e_using_object_as_number));
279 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100280 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000281 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200282 case VAR_VOID:
283 emsg(_(e_cannot_use_void_value));
284 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200285 case VAR_UNKNOWN:
286 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200287 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200288 internal_error_no_abort("tv_get_number(UNKNOWN)");
289 break;
290 }
291 if (denote == NULL) // useful for values that must be unsigned
292 n = -1;
293 else
294 *denote = TRUE;
295 return n;
296}
297
Bram Moolenaar36967b32020-08-17 21:41:02 +0200298/*
299 * Get the number value of a variable.
300 * If it is a String variable, uses vim_str2nr().
301 * For incompatible types, return 0.
302 * tv_get_number_chk() is similar to tv_get_number(), but informs the
303 * caller of incompatible types: it sets *denote to TRUE if "denote"
304 * is not NULL or returns -1 otherwise.
305 */
306 varnumber_T
307tv_get_number(typval_T *varp)
308{
309 int error = FALSE;
310
311 return tv_get_number_chk(varp, &error); // return 0L on error
312}
313
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000314/*
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200315 * Like tv_get_number() but in Vim9 script do convert a number in a string to a
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000316 * number without giving an error.
317 */
318 varnumber_T
319tv_to_number(typval_T *varp)
320{
321 int error = FALSE;
322
323 return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
324}
325
Bram Moolenaar36967b32020-08-17 21:41:02 +0200326 varnumber_T
327tv_get_number_chk(typval_T *varp, int *denote)
328{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000329 return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200330}
331
332/*
333 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200334 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200335 */
336 varnumber_T
337tv_get_bool(typval_T *varp)
338{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000339 return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200340}
341
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200342/*
343 * Get the boolean value of "varp". This is like tv_get_number_chk(),
344 * but in Vim9 script accepts Number and Bool.
345 */
346 varnumber_T
347tv_get_bool_chk(typval_T *varp, int *denote)
348{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000349 return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200350}
351
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000352 static float_T
353tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200354{
355 switch (varp->v_type)
356 {
357 case VAR_NUMBER:
358 return (float_T)(varp->vval.v_number);
359 case VAR_FLOAT:
360 return varp->vval.v_float;
361 case VAR_FUNC:
362 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000363 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200364 break;
365 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000366 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200367 break;
368 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000369 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200370 break;
371 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000372 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200373 break;
374 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000375 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200376 break;
377 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000378 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200379 break;
380 case VAR_JOB:
381# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000382 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200383 break;
384# endif
385 case VAR_CHANNEL:
386# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000387 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200388 break;
389# endif
390 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000391 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200392 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000393 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +0100394 case VAR_TYPEALIAS:
395 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000396 break;
397 case VAR_OBJECT:
398 emsg(_(e_using_object_as_float));
399 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200400 case VAR_VOID:
401 emsg(_(e_cannot_use_void_value));
402 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200403 case VAR_UNKNOWN:
404 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200405 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200406 internal_error_no_abort("tv_get_float(UNKNOWN)");
407 break;
408 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000409 if (error != NULL)
410 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200411 return 0;
412}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000413
414 float_T
415tv_get_float(typval_T *varp)
416{
417 return tv_get_float_chk(varp, NULL);
418}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200419
420/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100421 * Give an error and return FAIL unless "args[idx]" is unknown
422 */
423 int
424check_for_unknown_arg(typval_T *args, int idx)
425{
426 if (args[idx].v_type != VAR_UNKNOWN)
427 {
428 semsg(_(e_too_many_arguments), idx + 1);
429 return FAIL;
430 }
431 return OK;
432}
433
434/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200435 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100436 */
437 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100438check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100439{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100440 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100441 {
rbtnnddc80af2021-12-17 18:01:31 +0000442 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100443 return FAIL;
444 }
445 return OK;
446}
447
448/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100449 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100450 */
451 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100452check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100453{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100454 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100455 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100456 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100457 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200458 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100459 return FAIL;
460 }
461 return OK;
462}
463
464/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200465 * Check for an optional string argument at 'idx'
466 */
467 int
468check_for_opt_string_arg(typval_T *args, int idx)
469{
470 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100471 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200472}
473
474/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200475 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200476 */
477 int
478check_for_number_arg(typval_T *args, int idx)
479{
480 if (args[idx].v_type != VAR_NUMBER)
481 {
rbtnnddc80af2021-12-17 18:01:31 +0000482 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200483 return FAIL;
484 }
485 return OK;
486}
487
488/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200489 * Check for an optional number argument at 'idx'
490 */
491 int
492check_for_opt_number_arg(typval_T *args, int idx)
493{
494 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100495 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200496}
497
498/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200499 * Give an error and return FAIL unless "args[idx]" is a float or a number.
500 */
501 int
502check_for_float_or_nr_arg(typval_T *args, int idx)
503{
504 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
505 {
rbtnnddc80af2021-12-17 18:01:31 +0000506 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200507 return FAIL;
508 }
509 return OK;
510}
511
512/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200513 * Give an error and return FAIL unless "args[idx]" is a bool.
514 */
515 int
516check_for_bool_arg(typval_T *args, int idx)
517{
518 if (args[idx].v_type != VAR_BOOL
519 && !(args[idx].v_type == VAR_NUMBER
520 && (args[idx].vval.v_number == 0
521 || args[idx].vval.v_number == 1)))
522 {
rbtnnddc80af2021-12-17 18:01:31 +0000523 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200524 return FAIL;
525 }
526 return OK;
527}
528
529/*
zeertzjqe0a2ab32025-02-02 09:14:35 +0100530 * Give an error and return FAIL unless "args[idx]" is a bool or a number.
531 */
532 int
533check_for_bool_or_number_arg(typval_T *args, int idx)
534{
535 if (args[idx].v_type != VAR_BOOL && args[idx].v_type != VAR_NUMBER)
536 {
537 semsg(_(e_bool_or_number_required_for_argument_nr), idx + 1);
538 return FAIL;
539 }
540 return OK;
541}
542
543/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200544 * Check for an optional bool argument at 'idx'.
545 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200546 */
547 int
548check_for_opt_bool_arg(typval_T *args, int idx)
549{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200550 if (args[idx].v_type == VAR_UNKNOWN)
551 return OK;
552 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200553}
554
555/*
zeertzjqe0a2ab32025-02-02 09:14:35 +0100556 * Check for an optional bool or number argument at 'idx'.
557 * Return FAIL if the type is wrong.
558 */
559 int
560check_for_opt_bool_or_number_arg(typval_T *args, int idx)
561{
562 if (args[idx].v_type == VAR_UNKNOWN)
563 return OK;
564 return check_for_bool_or_number_arg(args, idx);
565}
566
567/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200568 * Give an error and return FAIL unless "args[idx]" is a blob.
569 */
570 int
571check_for_blob_arg(typval_T *args, int idx)
572{
573 if (args[idx].v_type != VAR_BLOB)
574 {
rbtnnddc80af2021-12-17 18:01:31 +0000575 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200576 return FAIL;
577 }
578 return OK;
579}
580
581/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200582 * Give an error and return FAIL unless "args[idx]" is a list.
583 */
584 int
585check_for_list_arg(typval_T *args, int idx)
586{
587 if (args[idx].v_type != VAR_LIST)
588 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200589 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200590 return FAIL;
591 }
592 return OK;
593}
594
595/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100596 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
597 */
598 int
599check_for_nonnull_list_arg(typval_T *args, int idx)
600{
601 if (check_for_list_arg(args, idx) == FAIL)
602 return FAIL;
603
604 if (args[idx].vval.v_list == NULL)
605 {
606 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
607 return FAIL;
608 }
609 return OK;
610}
611
612/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200613 * Check for an optional list argument at 'idx'
614 */
615 int
616check_for_opt_list_arg(typval_T *args, int idx)
617{
618 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100619 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200620}
621
622/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200623 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200624 */
625 int
626check_for_dict_arg(typval_T *args, int idx)
627{
628 if (args[idx].v_type != VAR_DICT)
629 {
rbtnnddc80af2021-12-17 18:01:31 +0000630 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200631 return FAIL;
632 }
633 return OK;
634}
635
636/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100637 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
638 */
639 int
640check_for_nonnull_dict_arg(typval_T *args, int idx)
641{
642 if (check_for_dict_arg(args, idx) == FAIL)
643 return FAIL;
644
645 if (args[idx].vval.v_dict == NULL)
646 {
647 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
648 return FAIL;
649 }
650 return OK;
651}
652
653/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200654 * Check for an optional dict argument at 'idx'
655 */
656 int
657check_for_opt_dict_arg(typval_T *args, int idx)
658{
659 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100660 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200661}
662
Yegappan Lakshmananfa378352024-02-01 22:05:27 +0100663/*
664 * Check for an optional non-NULL dict argument at 'idx'
665 */
666 int
667check_for_opt_nonnull_dict_arg(typval_T *args, int idx)
668{
669 return (args[idx].v_type == VAR_UNKNOWN
670 || check_for_nonnull_dict_arg(args, idx) != FAIL) ? OK : FAIL;
671}
672
Dominique Pelle748b3082022-01-08 12:41:16 +0000673#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200674/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200675 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
676 */
677 int
678check_for_chan_or_job_arg(typval_T *args, int idx)
679{
680 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
681 {
rbtnnddc80af2021-12-17 18:01:31 +0000682 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200683 return FAIL;
684 }
685 return OK;
686}
687
688/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200689 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
690 * job.
691 */
692 int
693check_for_opt_chan_or_job_arg(typval_T *args, int idx)
694{
695 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100696 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200697}
698
699/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200700 * Give an error and return FAIL unless "args[idx]" is a job.
701 */
702 int
703check_for_job_arg(typval_T *args, int idx)
704{
705 if (args[idx].v_type != VAR_JOB)
706 {
rbtnnddc80af2021-12-17 18:01:31 +0000707 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200708 return FAIL;
709 }
710 return OK;
711}
712
713/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200714 * Check for an optional job argument at 'idx'.
715 */
716 int
717check_for_opt_job_arg(typval_T *args, int idx)
718{
719 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100720 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200721}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000722#else
723/*
724 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
725 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
726 */
727 int
728check_for_opt_chan_or_job_arg(typval_T *args, int idx)
729{
730 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
731}
Dominique Pelle748b3082022-01-08 12:41:16 +0000732#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200733
734/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200735 * Give an error and return FAIL unless "args[idx]" is a string or
736 * a number.
737 */
738 int
739check_for_string_or_number_arg(typval_T *args, int idx)
740{
741 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
742 {
rbtnnddc80af2021-12-17 18:01:31 +0000743 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200744 return FAIL;
745 }
746 return OK;
747}
748
749/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200750 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200751 */
752 int
753check_for_opt_string_or_number_arg(typval_T *args, int idx)
754{
755 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100756 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200757}
758
759/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200760 * Give an error and return FAIL unless "args[idx]" is a buffer number.
761 * Buffer number can be a number or a string.
762 */
763 int
764check_for_buffer_arg(typval_T *args, int idx)
765{
766 return check_for_string_or_number_arg(args, idx);
767}
768
769/*
770 * Check for an optional buffer argument at 'idx'
771 */
772 int
773check_for_opt_buffer_arg(typval_T *args, int idx)
774{
775 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100776 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200777}
778
779/*
780 * Give an error and return FAIL unless "args[idx]" is a line number.
781 * Line number can be a number or a string.
782 */
783 int
784check_for_lnum_arg(typval_T *args, int idx)
785{
786 return check_for_string_or_number_arg(args, idx);
787}
788
789/*
790 * Check for an optional line number argument at 'idx'
791 */
792 int
793check_for_opt_lnum_arg(typval_T *args, int idx)
794{
795 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100796 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200797}
798
Dominique Pelle748b3082022-01-08 12:41:16 +0000799#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200800/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200801 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200802 */
803 int
804check_for_string_or_blob_arg(typval_T *args, int idx)
805{
806 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
807 {
rbtnnddc80af2021-12-17 18:01:31 +0000808 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200809 return FAIL;
810 }
811 return OK;
812}
Dominique Pelle748b3082022-01-08 12:41:16 +0000813#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200814
815/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200816 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200817 */
818 int
819check_for_string_or_list_arg(typval_T *args, int idx)
820{
821 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
822 {
rbtnnddc80af2021-12-17 18:01:31 +0000823 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200824 return FAIL;
825 }
826 return OK;
827}
828
829/*
rbtnn0ccb5842021-12-18 18:33:46 +0000830 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
831 * blob.
832 */
833 int
834check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
835{
836 if (args[idx].v_type != VAR_STRING
837 && args[idx].v_type != VAR_LIST
838 && args[idx].v_type != VAR_BLOB)
839 {
840 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
841 return FAIL;
842 }
843 return OK;
844}
845
846/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200847 * Check for an optional string or list argument at 'idx'
848 */
849 int
850check_for_opt_string_or_list_arg(typval_T *args, int idx)
851{
852 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100853 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200854}
855
856/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200857 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
858 */
859 int
860check_for_string_or_dict_arg(typval_T *args, int idx)
861{
862 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
863 {
rbtnnddc80af2021-12-17 18:01:31 +0000864 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200865 return FAIL;
866 }
867 return OK;
868}
869
870/*
871 * Give an error and return FAIL unless "args[idx]" is a string or a number
872 * or a list.
873 */
874 int
875check_for_string_or_number_or_list_arg(typval_T *args, int idx)
876{
877 if (args[idx].v_type != VAR_STRING
878 && args[idx].v_type != VAR_NUMBER
879 && args[idx].v_type != VAR_LIST)
880 {
rbtnn0ccb5842021-12-18 18:33:46 +0000881 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200882 return FAIL;
883 }
884 return OK;
885}
886
887/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200888 * Give an error and return FAIL unless "args[idx]" is an optional string
889 * or number or a list
890 */
891 int
892check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
893{
894 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100895 || check_for_string_or_number_or_list_arg(args, idx)
896 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200897}
898
899/*
Bakudankun375141e2022-09-09 18:46:47 +0100900 * Give an error and return FAIL unless "args[idx]" is a string or a number
901 * or a list or a blob.
902 */
903 int
904check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
905{
906 if (args[idx].v_type != VAR_STRING
907 && args[idx].v_type != VAR_NUMBER
908 && args[idx].v_type != VAR_LIST
909 && args[idx].v_type != VAR_BLOB)
910 {
911 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
912 return FAIL;
913 }
914 return OK;
915}
916
917/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200918 * Give an error and return FAIL unless "args[idx]" is a string or a list
919 * or a dict.
920 */
921 int
922check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
923{
924 if (args[idx].v_type != VAR_STRING
925 && args[idx].v_type != VAR_LIST
926 && args[idx].v_type != VAR_DICT)
927 {
rbtnnddc80af2021-12-17 18:01:31 +0000928 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200929 return FAIL;
930 }
931 return OK;
932}
933
934/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000935 * Give an error and return FAIL unless "args[idx]" is a string
936 * or a function reference.
937 */
938 int
939check_for_string_or_func_arg(typval_T *args, int idx)
940{
941 if (args[idx].v_type != VAR_PARTIAL
942 && args[idx].v_type != VAR_FUNC
943 && args[idx].v_type != VAR_STRING)
944 {
945 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
946 return FAIL;
947 }
948 return OK;
949}
950
951/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200952 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
953 */
954 int
955check_for_list_or_blob_arg(typval_T *args, int idx)
956{
957 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
958 {
rbtnn0ccb5842021-12-18 18:33:46 +0000959 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200960 return FAIL;
961 }
962 return OK;
963}
964
965/*
966 * Give an error and return FAIL unless "args[idx]" is a list or dict
967 */
968 int
969check_for_list_or_dict_arg(typval_T *args, int idx)
970{
971 if (args[idx].v_type != VAR_LIST
972 && args[idx].v_type != VAR_DICT)
973 {
rbtnnddc80af2021-12-17 18:01:31 +0000974 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200975 return FAIL;
976 }
977 return OK;
978}
979
980/*
981 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
982 * blob.
983 */
984 int
985check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
986{
987 if (args[idx].v_type != VAR_LIST
988 && args[idx].v_type != VAR_DICT
989 && args[idx].v_type != VAR_BLOB)
990 {
rbtnnddc80af2021-12-17 18:01:31 +0000991 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200992 return FAIL;
993 }
994 return OK;
995}
996
997/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000998 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
999 * blob or a string.
1000 */
1001 int
1002check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
1003{
1004 if (args[idx].v_type != VAR_LIST
1005 && args[idx].v_type != VAR_DICT
1006 && args[idx].v_type != VAR_BLOB
1007 && args[idx].v_type != VAR_STRING)
1008 {
rbtnnddc80af2021-12-17 18:01:31 +00001009 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +00001010 return FAIL;
1011 }
1012 return OK;
1013}
1014
1015/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001016 * Give an error and return FAIL unless "args[idx]" is an optional buffer
1017 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001018 */
1019 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001020check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001021{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001022 if (args[idx].v_type != VAR_UNKNOWN
1023 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001024 && args[idx].v_type != VAR_NUMBER
1025 && args[idx].v_type != VAR_DICT)
1026 {
rbtnnddc80af2021-12-17 18:01:31 +00001027 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001028 return FAIL;
1029 }
1030 return OK;
1031}
1032
1033/*
LemonBoyafe04662023-08-23 21:08:11 +02001034 * Give an error and return FAIL unless "args[idx]" is an object.
1035 */
1036 int
1037check_for_object_arg(typval_T *args, int idx)
1038{
1039 if (args[idx].v_type != VAR_OBJECT)
1040 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001041 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001042 return FAIL;
1043 }
1044 return OK;
1045}
1046
1047/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001048 * Returns TRUE if "tv" is a type alias for a class
1049 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01001050 static int
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001051tv_class_alias(typval_T *tv)
1052{
1053 return tv->v_type == VAR_TYPEALIAS &&
1054 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1055}
1056
1057/*
Ernie Rael2025af12023-12-12 16:58:00 +01001058 * Give an error and return FAIL unless "args[idx]" is a class
1059 * or class typealias.
LemonBoyafe04662023-08-23 21:08:11 +02001060 */
1061 int
Ernie Rael2025af12023-12-12 16:58:00 +01001062check_for_class_or_typealias_args(typval_T *args, int idx)
LemonBoyafe04662023-08-23 21:08:11 +02001063{
Ernie Rael2025af12023-12-12 16:58:00 +01001064 for (int i = idx; args[i].v_type != VAR_UNKNOWN; ++i)
LemonBoyafe04662023-08-23 21:08:11 +02001065 {
Ernie Rael2025af12023-12-12 16:58:00 +01001066 if (args[i].v_type != VAR_CLASS && !tv_class_alias(&args[idx]))
1067 {
1068 semsg(_(e_class_or_typealias_required_for_argument_nr), i + 1);
1069 return FAIL;
1070 }
LemonBoyafe04662023-08-23 21:08:11 +02001071 }
1072 return OK;
1073}
1074
1075/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001076 * Get the string value of a variable.
1077 * If it is a Number variable, the number is converted into a string.
1078 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1079 * tv_get_string_buf() uses a given buffer.
1080 * If the String variable has never been set, return an empty string.
1081 * Never returns NULL;
1082 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1083 * NULL on error.
1084 */
1085 char_u *
1086tv_get_string(typval_T *varp)
1087{
1088 static char_u mybuf[NUMBUFLEN];
1089
1090 return tv_get_string_buf(varp, mybuf);
1091}
1092
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001093/*
1094 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1095 */
1096 char_u *
1097tv_get_string_strict(typval_T *varp)
1098{
1099 static char_u mybuf[NUMBUFLEN];
1100 char_u *res = tv_get_string_buf_chk_strict(
1101 varp, mybuf, in_vim9script());
1102
1103 return res != NULL ? res : (char_u *)"";
1104}
1105
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001106 char_u *
1107tv_get_string_buf(typval_T *varp, char_u *buf)
1108{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001109 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001110
1111 return res != NULL ? res : (char_u *)"";
1112}
1113
1114/*
1115 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1116 */
1117 char_u *
1118tv_get_string_chk(typval_T *varp)
1119{
1120 static char_u mybuf[NUMBUFLEN];
1121
1122 return tv_get_string_buf_chk(varp, mybuf);
1123}
1124
1125 char_u *
1126tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1127{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001128 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1129}
1130
1131 char_u *
1132tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1133{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001134 switch (varp->v_type)
1135 {
1136 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001137 if (strict)
1138 {
1139 emsg(_(e_using_number_as_string));
1140 break;
1141 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1143 (varnumber_T)varp->vval.v_number);
1144 return buf;
1145 case VAR_FUNC:
1146 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001147 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001148 break;
1149 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001150 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001151 break;
1152 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001153 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001154 break;
1155 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001156 if (strict)
1157 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001158 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001159 break;
1160 }
1161 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1162 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001163 case VAR_STRING:
1164 if (varp->vval.v_string != NULL)
1165 return varp->vval.v_string;
1166 return (char_u *)"";
1167 case VAR_BOOL:
1168 case VAR_SPECIAL:
1169 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1170 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001171 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001172 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001173 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001174 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +01001175 case VAR_TYPEALIAS:
1176 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001177 break;
1178 case VAR_OBJECT:
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001179 {
Ernie Raelbe828252024-07-26 18:37:02 +02001180 if (varp->vval.v_object == NULL)
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001181 emsg(_(e_using_object_as_string));
Ernie Raelbe828252024-07-26 18:37:02 +02001182 else
1183 {
1184 class_T *cl = varp->vval.v_object->obj_class;
1185 if (cl != NULL && IS_ENUM(cl))
1186 semsg(_(e_using_enum_str_as_string), cl->class_name);
1187 else
1188 emsg(_(e_using_object_as_string));
1189 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001190 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001191 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001192 case VAR_JOB:
1193#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001194 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001195 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001196 semsg(_(e_using_invalid_value_as_string_str), "job");
1197 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001198 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001199 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001200#endif
1201 break;
1202 case VAR_CHANNEL:
1203#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001204 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001205 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001206 semsg(_(e_using_invalid_value_as_string_str), "channel");
1207 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001208 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001209 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001210#endif
1211 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001212 case VAR_VOID:
1213 emsg(_(e_cannot_use_void_value));
1214 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001215 case VAR_UNKNOWN:
1216 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001217 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001218 semsg(_(e_using_invalid_value_as_string_str),
1219 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001220 break;
1221 }
1222 return NULL;
1223}
1224
1225/*
1226 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1227 * string() on Dict, List, etc.
1228 */
1229 char_u *
1230tv_stringify(typval_T *varp, char_u *buf)
1231{
1232 if (varp->v_type == VAR_LIST
1233 || varp->v_type == VAR_DICT
1234 || varp->v_type == VAR_BLOB
1235 || varp->v_type == VAR_FUNC
1236 || varp->v_type == VAR_PARTIAL
1237 || varp->v_type == VAR_FLOAT)
1238 {
1239 typval_T tmp;
1240
1241 f_string(varp, &tmp);
1242 tv_get_string_buf(&tmp, buf);
1243 clear_tv(varp);
1244 *varp = tmp;
1245 return tmp.vval.v_string;
1246 }
1247 return tv_get_string_buf(varp, buf);
1248}
1249
1250/*
1251 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1252 * Also give an error message, using "name" or _("name") when use_gettext is
1253 * TRUE.
1254 */
1255 int
1256tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1257{
1258 int lock = 0;
1259
1260 switch (tv->v_type)
1261 {
1262 case VAR_BLOB:
1263 if (tv->vval.v_blob != NULL)
1264 lock = tv->vval.v_blob->bv_lock;
1265 break;
1266 case VAR_LIST:
1267 if (tv->vval.v_list != NULL)
1268 lock = tv->vval.v_list->lv_lock;
1269 break;
1270 case VAR_DICT:
1271 if (tv->vval.v_dict != NULL)
1272 lock = tv->vval.v_dict->dv_lock;
1273 break;
1274 default:
1275 break;
1276 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001277 return value_check_lock(tv->v_lock, name, use_gettext)
1278 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001279}
1280
1281/*
1282 * Copy the values from typval_T "from" to typval_T "to".
1283 * When needed allocates string or increases reference count.
1284 * Does not make a copy of a list, blob or dict but copies the reference!
1285 * It is OK for "from" and "to" to point to the same item. This is used to
1286 * make a copy later.
1287 */
1288 void
1289copy_tv(typval_T *from, typval_T *to)
1290{
1291 to->v_type = from->v_type;
1292 to->v_lock = 0;
1293 switch (from->v_type)
1294 {
1295 case VAR_NUMBER:
1296 case VAR_BOOL:
1297 case VAR_SPECIAL:
1298 to->vval.v_number = from->vval.v_number;
1299 break;
1300 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001301 to->vval.v_float = from->vval.v_float;
1302 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001303 case VAR_JOB:
1304#ifdef FEAT_JOB_CHANNEL
1305 to->vval.v_job = from->vval.v_job;
1306 if (to->vval.v_job != NULL)
1307 ++to->vval.v_job->jv_refcount;
1308 break;
1309#endif
1310 case VAR_CHANNEL:
1311#ifdef FEAT_JOB_CHANNEL
1312 to->vval.v_channel = from->vval.v_channel;
1313 if (to->vval.v_channel != NULL)
1314 ++to->vval.v_channel->ch_refcount;
1315 break;
1316#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001317 case VAR_INSTR:
1318 to->vval.v_instr = from->vval.v_instr;
1319 break;
1320
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001321 case VAR_CLASS:
1322 copy_class(from, to);
1323 break;
1324
1325 case VAR_OBJECT:
1326 copy_object(from, to);
1327 break;
1328
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001329 case VAR_STRING:
1330 case VAR_FUNC:
1331 if (from->vval.v_string == NULL)
1332 to->vval.v_string = NULL;
1333 else
1334 {
1335 to->vval.v_string = vim_strsave(from->vval.v_string);
1336 if (from->v_type == VAR_FUNC)
1337 func_ref(to->vval.v_string);
1338 }
1339 break;
1340 case VAR_PARTIAL:
1341 if (from->vval.v_partial == NULL)
1342 to->vval.v_partial = NULL;
1343 else
1344 {
1345 to->vval.v_partial = from->vval.v_partial;
1346 ++to->vval.v_partial->pt_refcount;
1347 }
1348 break;
1349 case VAR_BLOB:
1350 if (from->vval.v_blob == NULL)
1351 to->vval.v_blob = NULL;
1352 else
1353 {
1354 to->vval.v_blob = from->vval.v_blob;
1355 ++to->vval.v_blob->bv_refcount;
1356 }
1357 break;
1358 case VAR_LIST:
1359 if (from->vval.v_list == NULL)
1360 to->vval.v_list = NULL;
1361 else
1362 {
1363 to->vval.v_list = from->vval.v_list;
1364 ++to->vval.v_list->lv_refcount;
1365 }
1366 break;
1367 case VAR_DICT:
1368 if (from->vval.v_dict == NULL)
1369 to->vval.v_dict = NULL;
1370 else
1371 {
1372 to->vval.v_dict = from->vval.v_dict;
1373 ++to->vval.v_dict->dv_refcount;
1374 }
1375 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001376 case VAR_TYPEALIAS:
1377 if (from->vval.v_typealias == NULL)
1378 to->vval.v_typealias = NULL;
1379 else
1380 {
1381 to->vval.v_typealias = from->vval.v_typealias;
1382 ++to->vval.v_typealias->ta_refcount;
1383 }
1384 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001385 case VAR_VOID:
1386 emsg(_(e_cannot_use_void_value));
1387 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001388 case VAR_UNKNOWN:
1389 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001390 internal_error_no_abort("copy_tv(UNKNOWN)");
1391 break;
1392 }
1393}
1394
1395/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001396 * Compare "tv1" and "tv2".
1397 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001398 */
1399 int
1400typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001401 typval_T *tv1, // first operand
1402 typval_T *tv2, // second operand
1403 exprtype_T type, // operator
1404 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001405{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001406 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001407 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001408 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1409
Ernie Raele75fde62023-12-21 17:18:54 +01001410 if (check_typval_is_value(tv1) == FAIL
1411 || check_typval_is_value(tv2) == FAIL)
1412 {
1413 clear_tv(tv1);
1414 return FAIL;
1415 }
1416 else if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001417 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001418 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001419 // it means TRUE.
1420 n1 = (type == EXPR_ISNOT);
1421 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001422 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1423 || (tv2->v_type == VAR_SPECIAL
1424 && tv2->vval.v_number == VVAL_NULL))
1425 && tv1->v_type != tv2->v_type
1426 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1427 {
1428 n1 = typval_compare_null(tv1, tv2);
1429 if (n1 == MAYBE)
1430 {
1431 clear_tv(tv1);
1432 return FAIL;
1433 }
1434 if (type == EXPR_NEQUAL)
1435 n1 = !n1;
1436 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001437 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001438 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001439 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001440 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001441 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001442 return FAIL;
1443 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001444 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001445 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001446 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001447 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001448 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001449 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001450 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001451 return FAIL;
1452 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001453 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001454 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001455 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1456 {
1457 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1458 {
1459 clear_tv(tv1);
1460 return FAIL;
1461 }
1462 n1 = res;
1463 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001464 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001465 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001466 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001467 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001468 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001469 return FAIL;
1470 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001471 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001472 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001473 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1474 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001475 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001476 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001477 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001478 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001479 return FAIL;
1480 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001481 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001482 }
1483
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001484 // If one of the two variables is a float, compare as a float.
1485 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001486 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001487 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1488 {
1489 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001490 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001491
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001492 f1 = tv_get_float_chk(tv1, &error);
1493 if (!error)
1494 f2 = tv_get_float_chk(tv2, &error);
1495 if (error)
1496 {
1497 clear_tv(tv1);
1498 return FAIL;
1499 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001500 n1 = FALSE;
1501 switch (type)
1502 {
1503 case EXPR_IS:
1504 case EXPR_EQUAL: n1 = (f1 == f2); break;
1505 case EXPR_ISNOT:
1506 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1507 case EXPR_GREATER: n1 = (f1 > f2); break;
1508 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1509 case EXPR_SMALLER: n1 = (f1 < f2); break;
1510 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1511 case EXPR_UNKNOWN:
1512 case EXPR_MATCH:
1513 default: break; // avoid gcc warning
1514 }
1515 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001516
1517 // If one of the two variables is a number, compare as a number.
1518 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001519 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001520 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1521 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001522 int error = FALSE;
1523
1524 n1 = tv_get_number_chk(tv1, &error);
1525 if (!error)
1526 n2 = tv_get_number_chk(tv2, &error);
1527 if (error)
1528 {
1529 clear_tv(tv1);
1530 return FAIL;
1531 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001532 switch (type)
1533 {
1534 case EXPR_IS:
1535 case EXPR_EQUAL: n1 = (n1 == n2); break;
1536 case EXPR_ISNOT:
1537 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1538 case EXPR_GREATER: n1 = (n1 > n2); break;
1539 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1540 case EXPR_SMALLER: n1 = (n1 < n2); break;
1541 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1542 case EXPR_UNKNOWN:
1543 case EXPR_MATCH:
1544 default: break; // avoid gcc warning
1545 }
1546 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001547 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1548 || tv2->v_type == VAR_BOOL
1549 || (tv1->v_type == VAR_SPECIAL
1550 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001551 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001552 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001553 {
1554 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001555 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1556 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001557 return FAIL;
1558 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001559 n1 = tv1->vval.v_number;
1560 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001561 switch (type)
1562 {
1563 case EXPR_IS:
1564 case EXPR_EQUAL: n1 = (n1 == n2); break;
1565 case EXPR_ISNOT:
1566 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1567 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001568 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001569 vartype_name(tv1->v_type));
1570 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001571 return FAIL;
1572 }
1573 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001574#ifdef FEAT_JOB_CHANNEL
1575 else if (tv1->v_type == tv2->v_type
1576 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1577 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1578 {
1579 if (tv1->v_type == VAR_CHANNEL)
1580 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1581 else
1582 n1 = tv1->vval.v_job == tv2->vval.v_job;
1583 if (type == EXPR_NEQUAL)
1584 n1 = !n1;
1585 }
1586#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001587 else
1588 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001589 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001590 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001591 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001592 return FAIL;
1593 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001594 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001595 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001596 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001597 if (in_vim9script())
1598 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001599 tv1->v_type = VAR_BOOL;
1600 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001601 }
1602 else
1603 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001604 tv1->v_type = VAR_NUMBER;
1605 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001606 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001607
1608 return OK;
1609}
1610
Bram Moolenaar34453202021-01-31 13:08:38 +01001611/*
dundargocc57b5bc2022-11-02 13:30:51 +00001612 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001613 * Put the result, false or true, in "res".
1614 * Return FAIL and give an error message when the comparison can't be done.
1615 */
1616 int
1617typval_compare_list(
1618 typval_T *tv1,
1619 typval_T *tv2,
1620 exprtype_T type,
1621 int ic,
1622 int *res)
1623{
1624 int val = 0;
1625
1626 if (type == EXPR_IS || type == EXPR_ISNOT)
1627 {
1628 val = (tv1->v_type == tv2->v_type
1629 && tv1->vval.v_list == tv2->vval.v_list);
1630 if (type == EXPR_ISNOT)
1631 val = !val;
1632 }
1633 else if (tv1->v_type != tv2->v_type
1634 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1635 {
1636 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001637 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001638 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001639 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001640 return FAIL;
1641 }
1642 else
1643 {
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001644 val = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001645 if (type == EXPR_NEQUAL)
1646 val = !val;
1647 }
1648 *res = val;
1649 return OK;
1650}
1651
1652/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001653 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001654 */
1655 int
1656typval_compare_null(typval_T *tv1, typval_T *tv2)
1657{
1658 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1659 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1660 {
1661 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1662
1663 switch (tv->v_type)
1664 {
1665 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001666#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001667 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001668#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001669 // TODO: null_class handling
1670 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001671 case VAR_DICT: return tv->vval.v_dict == NULL;
1672 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001673#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001674 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001675#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001676 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001677 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001678 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1679 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001680
1681 case VAR_NUMBER: if (!in_vim9script())
1682 return tv->vval.v_number == 0;
1683 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001684 case VAR_FLOAT: if (!in_vim9script())
1685 return tv->vval.v_float == 0.0;
1686 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001687 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001688 default: break;
1689 }
1690 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001691 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001692 // we won't give an error
1693 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001694}
1695
1696/*
dundargocc57b5bc2022-11-02 13:30:51 +00001697 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001698 * Put the result, false or true, in "res".
1699 * Return FAIL and give an error message when the comparison can't be done.
1700 */
1701 int
1702typval_compare_blob(
1703 typval_T *tv1,
1704 typval_T *tv2,
1705 exprtype_T type,
1706 int *res)
1707{
1708 int val = 0;
1709
1710 if (type == EXPR_IS || type == EXPR_ISNOT)
1711 {
1712 val = (tv1->v_type == tv2->v_type
1713 && tv1->vval.v_blob == tv2->vval.v_blob);
1714 if (type == EXPR_ISNOT)
1715 val = !val;
1716 }
1717 else if (tv1->v_type != tv2->v_type
1718 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1719 {
1720 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001721 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001722 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001723 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001724 return FAIL;
1725 }
1726 else
1727 {
1728 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1729 if (type == EXPR_NEQUAL)
1730 val = !val;
1731 }
1732 *res = val;
1733 return OK;
1734}
1735
1736/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001737 * Compare "tv1" to "tv2" as objects according to "type".
1738 * Put the result, false or true, in "res".
1739 * Return FAIL and give an error message when the comparison can't be done.
1740 */
1741 int
1742typval_compare_object(
1743 typval_T *tv1,
1744 typval_T *tv2,
1745 exprtype_T type,
1746 int ic,
1747 int *res)
1748{
1749 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1750
1751 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1752 {
1753 *res = res_match;
1754 return OK;
1755 }
1756 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1757 {
1758 *res = !res_match;
1759 return OK;
1760 }
1761
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001762 object_T *obj1 = tv1->vval.v_object;
1763 object_T *obj2 = tv2->vval.v_object;
1764 if (type == EXPR_IS || type == EXPR_ISNOT)
1765 {
1766 *res = obj1 == obj2 ? res_match : !res_match;
1767 return OK;
1768 }
1769
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001770 *res = object_equal(obj1, obj2, ic) ? res_match : !res_match;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001771 return OK;
1772}
1773
1774/*
dundargocc57b5bc2022-11-02 13:30:51 +00001775 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001776 * Put the result, false or true, in "res".
1777 * Return FAIL and give an error message when the comparison can't be done.
1778 */
1779 int
1780typval_compare_dict(
1781 typval_T *tv1,
1782 typval_T *tv2,
1783 exprtype_T type,
1784 int ic,
1785 int *res)
1786{
1787 int val;
1788
1789 if (type == EXPR_IS || type == EXPR_ISNOT)
1790 {
1791 val = (tv1->v_type == tv2->v_type
1792 && tv1->vval.v_dict == tv2->vval.v_dict);
1793 if (type == EXPR_ISNOT)
1794 val = !val;
1795 }
1796 else if (tv1->v_type != tv2->v_type
1797 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1798 {
1799 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001800 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001801 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001802 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001803 return FAIL;
1804 }
1805 else
1806 {
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001807 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001808 if (type == EXPR_NEQUAL)
1809 val = !val;
1810 }
1811 *res = val;
1812 return OK;
1813}
1814
1815/*
dundargocc57b5bc2022-11-02 13:30:51 +00001816 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001817 * Put the result, false or true, in "res".
1818 * Return FAIL and give an error message when the comparison can't be done.
1819 */
1820 int
1821typval_compare_func(
1822 typval_T *tv1,
1823 typval_T *tv2,
1824 exprtype_T type,
1825 int ic,
1826 int *res)
1827{
1828 int val = 0;
1829
1830 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1831 && type != EXPR_IS && type != EXPR_ISNOT)
1832 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001833 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001834 return FAIL;
1835 }
1836 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1837 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1838 // When both partials are NULL, then they are equal.
1839 // Otherwise they are not equal.
1840 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1841 else if (type == EXPR_IS || type == EXPR_ISNOT)
1842 {
1843 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1844 // strings are considered the same if their value is
1845 // the same
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001846 val = tv_equal(tv1, tv2, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001847 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1848 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1849 else
1850 val = FALSE;
1851 }
1852 else
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001853 val = tv_equal(tv1, tv2, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001854 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1855 val = !val;
1856 *res = val;
1857 return OK;
1858}
1859
1860/*
1861 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1862 * Put the result, false or true, in "res".
1863 * Return FAIL and give an error message when the comparison can't be done.
1864 */
1865 int
1866typval_compare_string(
1867 typval_T *tv1,
1868 typval_T *tv2,
1869 exprtype_T type,
1870 int ic,
1871 int *res)
1872{
1873 int i = 0;
1874 int val = FALSE;
1875 char_u *s1, *s2;
1876 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1877
1878 if (in_vim9script()
1879 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1880 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1881 {
1882 semsg(_(e_cannot_compare_str_with_str),
1883 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1884 return FAIL;
1885 }
1886 s1 = tv_get_string_buf(tv1, buf1);
1887 s2 = tv_get_string_buf(tv2, buf2);
1888 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1889 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1890 switch (type)
1891 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001892 case EXPR_IS: if (in_vim9script())
1893 {
1894 // Really check it is the same string, not just
1895 // the same value.
1896 val = tv1->vval.v_string == tv2->vval.v_string;
1897 break;
1898 }
1899 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001900 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001901 case EXPR_ISNOT: if (in_vim9script())
1902 {
1903 // Really check it is not the same string, not
1904 // just a different value.
1905 val = tv1->vval.v_string != tv2->vval.v_string;
1906 break;
1907 }
1908 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001909 case EXPR_NEQUAL: val = (i != 0); break;
1910 case EXPR_GREATER: val = (i > 0); break;
1911 case EXPR_GEQUAL: val = (i >= 0); break;
1912 case EXPR_SMALLER: val = (i < 0); break;
1913 case EXPR_SEQUAL: val = (i <= 0); break;
1914
1915 case EXPR_MATCH:
1916 case EXPR_NOMATCH:
1917 val = pattern_match(s2, s1, ic);
1918 if (type == EXPR_NOMATCH)
1919 val = !val;
1920 break;
1921
1922 default: break; // avoid gcc warning
1923 }
1924 *res = val;
1925 return OK;
1926}
1927/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001928 * Convert any type to a string, never give an error.
1929 * When "quotes" is TRUE add quotes to a string.
1930 * Returns an allocated string.
1931 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001932 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001933typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001934{
1935 char_u *tofree;
1936 char_u numbuf[NUMBUFLEN];
1937 char_u *ret = NULL;
1938
1939 if (arg == NULL)
1940 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001941 if (!quotes && arg->v_type == VAR_STRING)
1942 {
1943 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1944 : arg->vval.v_string);
1945 }
1946 else
1947 {
1948 ret = tv2string(arg, &tofree, numbuf, 0);
1949 // Make a copy if we have a value but it's not in allocated memory.
1950 if (ret != NULL && tofree == NULL)
1951 ret = vim_strsave(ret);
1952 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001953 return ret;
1954}
1955
1956/*
1957 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1958 * or it refers to a List or Dictionary that is locked.
1959 */
1960 int
1961tv_islocked(typval_T *tv)
1962{
1963 return (tv->v_lock & VAR_LOCKED)
1964 || (tv->v_type == VAR_LIST
1965 && tv->vval.v_list != NULL
1966 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1967 || (tv->v_type == VAR_DICT
1968 && tv->vval.v_dict != NULL
1969 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1970}
1971
1972 static int
1973func_equal(
1974 typval_T *tv1,
1975 typval_T *tv2,
1976 int ic) // ignore case
1977{
1978 char_u *s1, *s2;
1979 dict_T *d1, *d2;
1980 int a1, a2;
1981 int i;
1982
1983 // empty and NULL function name considered the same
1984 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1985 : partial_name(tv1->vval.v_partial);
1986 if (s1 != NULL && *s1 == NUL)
1987 s1 = NULL;
1988 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1989 : partial_name(tv2->vval.v_partial);
1990 if (s2 != NULL && *s2 == NUL)
1991 s2 = NULL;
1992 if (s1 == NULL || s2 == NULL)
1993 {
1994 if (s1 != s2)
1995 return FALSE;
1996 }
1997 else if (STRCMP(s1, s2) != 0)
1998 return FALSE;
1999
2000 // empty dict and NULL dict is different
2001 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
2002 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
2003 if (d1 == NULL || d2 == NULL)
2004 {
2005 if (d1 != d2)
2006 return FALSE;
2007 }
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002008 else if (!dict_equal(d1, d2, ic))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002009 return FALSE;
2010
2011 // empty list and no list considered the same
2012 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
2013 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
2014 if (a1 != a2)
2015 return FALSE;
2016 for (i = 0; i < a1; ++i)
2017 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002018 tv2->vval.v_partial->pt_argv + i, ic))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002019 return FALSE;
2020
2021 return TRUE;
2022}
2023
2024/*
2025 * Return TRUE if "tv1" and "tv2" have the same value.
2026 * Compares the items just like "==" would compare them, but strings and
2027 * numbers are different. Floats and numbers are also different.
2028 */
2029 int
2030tv_equal(
2031 typval_T *tv1,
2032 typval_T *tv2,
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002033 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002034{
2035 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2036 char_u *s1, *s2;
2037 static int recursive_cnt = 0; // catch recursive loops
2038 int r;
2039 static int tv_equal_recurse_limit;
2040
2041 // Catch lists and dicts that have an endless loop by limiting
2042 // recursiveness to a limit. We guess they are equal then.
2043 // A fixed limit has the problem of still taking an awful long time.
2044 // Reduce the limit every time running into it. That should work fine for
2045 // deeply linked structures that are not recursively linked and catch
2046 // recursiveness quickly.
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002047 if (recursive_cnt == 0)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002048 tv_equal_recurse_limit = 1000;
2049 if (recursive_cnt >= tv_equal_recurse_limit)
2050 {
2051 --tv_equal_recurse_limit;
2052 return TRUE;
2053 }
2054
2055 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2056 // arguments.
2057 if ((tv1->v_type == VAR_FUNC
2058 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2059 && (tv2->v_type == VAR_FUNC
2060 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2061 {
2062 ++recursive_cnt;
2063 r = func_equal(tv1, tv2, ic);
2064 --recursive_cnt;
2065 return r;
2066 }
2067
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002068 if (tv1->v_type != tv2->v_type
2069 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2070 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002071 return FALSE;
2072
2073 switch (tv1->v_type)
2074 {
2075 case VAR_LIST:
2076 ++recursive_cnt;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002077 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002078 --recursive_cnt;
2079 return r;
2080
2081 case VAR_DICT:
2082 ++recursive_cnt;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002083 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002084 --recursive_cnt;
2085 return r;
2086
2087 case VAR_BLOB:
2088 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2089
2090 case VAR_NUMBER:
2091 case VAR_BOOL:
2092 case VAR_SPECIAL:
2093 return tv1->vval.v_number == tv2->vval.v_number;
2094
2095 case VAR_STRING:
2096 s1 = tv_get_string_buf(tv1, buf1);
2097 s2 = tv_get_string_buf(tv2, buf2);
2098 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2099
2100 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002101 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002102 case VAR_JOB:
2103#ifdef FEAT_JOB_CHANNEL
2104 return tv1->vval.v_job == tv2->vval.v_job;
2105#endif
2106 case VAR_CHANNEL:
2107#ifdef FEAT_JOB_CHANNEL
2108 return tv1->vval.v_channel == tv2->vval.v_channel;
2109#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002110 case VAR_INSTR:
2111 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002112
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002113 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002114 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002115 return tv1->vval.v_class == tv2->vval.v_class;
2116
2117 case VAR_OBJECT:
Ernie Raelf0e69142024-06-22 11:12:00 +02002118 ++recursive_cnt;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002119 r = object_equal(tv1->vval.v_object, tv2->vval.v_object, ic);
Ernie Raelf0e69142024-06-22 11:12:00 +02002120 --recursive_cnt;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002121 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002122
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002123 case VAR_PARTIAL:
2124 return tv1->vval.v_partial == tv2->vval.v_partial;
2125
2126 case VAR_FUNC:
2127 return tv1->vval.v_string == tv2->vval.v_string;
2128
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002129 case VAR_TYPEALIAS:
2130 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2131
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002132 case VAR_UNKNOWN:
2133 case VAR_ANY:
2134 case VAR_VOID:
2135 break;
2136 }
2137
2138 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2139 // does not equal anything, not even itself.
2140 return FALSE;
2141}
2142
2143/*
2144 * Get an option value.
2145 * "arg" points to the '&' or '+' before the option name.
2146 * "arg" is advanced to character after the option name.
2147 * Return OK or FAIL.
2148 */
2149 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002150eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002151 char_u **arg,
2152 typval_T *rettv, // when NULL, only check if option exists
2153 int evaluate)
2154{
2155 char_u *option_end;
2156 long numval;
2157 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002158 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002159 int c;
2160 int working = (**arg == '+'); // has("+option")
2161 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002162 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002163
2164 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002165 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002166 if (option_end == NULL)
2167 {
2168 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002169 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002170 return FAIL;
2171 }
2172
2173 if (!evaluate)
2174 {
2175 *arg = option_end;
2176 return OK;
2177 }
2178
2179 c = *option_end;
2180 *option_end = NUL;
2181 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002182 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002183
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002184 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002185 {
2186 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002187 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002188 ret = FAIL;
2189 }
2190 else if (rettv != NULL)
2191 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002192 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002193 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002194 {
2195 rettv->v_type = VAR_STRING;
2196 rettv->vval.v_string = NULL;
2197 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002198 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002199 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002200 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2201 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002202 rettv->vval.v_number = 0;
2203 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002204 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002205 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002206 if (in_vim9script() && opt_type == gov_bool)
2207 {
2208 rettv->v_type = VAR_BOOL;
2209 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2210 }
2211 else
2212 {
2213 rettv->v_type = VAR_NUMBER;
2214 rettv->vval.v_number = numval;
2215 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002216 }
2217 else // string option
2218 {
2219 rettv->v_type = VAR_STRING;
2220 rettv->vval.v_string = stringval;
2221 }
2222 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002223 else if (working && (opt_type == gov_hidden_bool
2224 || opt_type == gov_hidden_number
2225 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002226 ret = FAIL;
2227
2228 *option_end = c; // put back for error messages
2229 *arg = option_end;
2230
2231 return ret;
2232}
2233
2234/*
2235 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2236 * Return OK or FAIL.
2237 */
2238 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002239eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002240 char_u **arg,
2241 typval_T *rettv,
2242 int evaluate,
Dominique Pellé0268ff32024-07-28 21:12:20 +02002243 int want_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002244{
2245 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002246 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002247 char_u *p;
2248 int get_float = FALSE;
2249
2250 // We accept a float when the format matches
2251 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2252 // strict to avoid backwards compatibility problems.
2253 // With script version 2 and later the leading digit can be
2254 // omitted.
2255 // Don't look for a float after the "." operator, so that
2256 // ":let vers = 1.2.3" doesn't fail.
2257 if (**arg == '.')
2258 p = *arg;
2259 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002260 {
2261 p = *arg + 1;
2262 if (skip_quotes)
2263 for (;;)
2264 {
2265 if (*p == '\'')
2266 ++p;
2267 if (!vim_isdigit(*p))
2268 break;
2269 p = skipdigits(p);
2270 }
2271 else
2272 p = skipdigits(p);
2273 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002274 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2275 {
2276 get_float = TRUE;
2277 p = skipdigits(p + 2);
2278 if (*p == 'e' || *p == 'E')
2279 {
2280 ++p;
2281 if (*p == '-' || *p == '+')
2282 ++p;
2283 if (!vim_isdigit(*p))
2284 get_float = FALSE;
2285 else
2286 p = skipdigits(p + 1);
2287 }
2288 if (ASCII_ISALPHA(*p) || *p == '.')
2289 get_float = FALSE;
2290 }
2291 if (get_float)
2292 {
2293 float_T f;
2294
Bram Moolenaar29500652021-08-08 15:43:34 +02002295 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002296 if (evaluate)
2297 {
2298 rettv->v_type = VAR_FLOAT;
2299 rettv->vval.v_float = f;
2300 }
2301 }
2302 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002303 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2304 {
2305 char_u *bp;
2306 blob_T *blob = NULL; // init for gcc
2307
2308 // Blob constant: 0z0123456789abcdef
2309 if (evaluate)
2310 blob = blob_alloc();
2311 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2312 {
2313 if (!vim_isxdigit(bp[1]))
2314 {
2315 if (blob != NULL)
2316 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002317 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002318 ga_clear(&blob->bv_ga);
2319 VIM_CLEAR(blob);
2320 }
2321 return FAIL;
2322 }
2323 if (blob != NULL)
2324 ga_append(&blob->bv_ga,
2325 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2326 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2327 ++bp;
2328 }
2329 if (blob != NULL)
2330 rettv_blob_set(rettv, blob);
2331 *arg = bp;
2332 }
2333 else
2334 {
2335 varnumber_T n;
2336
2337 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002338 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002339 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002340 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002341 if (len == 0)
2342 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002343 if (evaluate)
2344 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002345 return FAIL;
2346 }
2347 *arg += len;
2348 if (evaluate)
2349 {
2350 rettv->v_type = VAR_NUMBER;
2351 rettv->vval.v_number = n;
2352 }
2353 }
2354 return OK;
2355}
2356
2357/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002358 * Evaluate a string constant and put the result in "rettv".
2359 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2360 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2361 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002362 * Return OK or FAIL.
2363 */
2364 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002365eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002366{
2367 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002368 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002369 int extra = interpolate ? 1 : 0;
2370 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002371 int len;
2372
2373 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002374 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002375 {
2376 if (*p == '\\' && p[1] != NUL)
2377 {
2378 ++p;
2379 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002380 // to 9 characters (6 for the char and 3 for a modifier):
2381 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002382 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002383 {
2384 int modifiers = 0;
2385 int flags = FSK_KEYCODE | FSK_IN_STRING;
2386
zeertzjqdb088872022-05-02 22:53:45 +01002387 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002388
2389 // Skip to the '>' to avoid using '{' inside for string
2390 // interpolation.
2391 if (p[1] != '*')
2392 flags |= FSK_SIMPLIFY;
2393 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2394 --p; // leave "p" on the ">"
2395 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002396 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002397 else if (interpolate && (*p == '{' || *p == '}'))
2398 {
2399 if (*p == '{' && p[1] != '{') // start of expression
2400 break;
2401 ++p;
2402 if (p[-1] == '}' && *p != '}') // single '}' is an error
2403 {
2404 semsg(_(e_stray_closing_curly_str), *arg);
2405 return FAIL;
2406 }
2407 --extra; // "{{" becomes "{", "}}" becomes "}"
2408 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002409 }
2410
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002411 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002412 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002413 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002414 return FAIL;
2415 }
2416
2417 // If only parsing, set *arg and return here
2418 if (!evaluate)
2419 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002420 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002421 return OK;
2422 }
2423
2424 // Copy the string into allocated memory, handling backslashed
2425 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002426 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002427 len = (int)(p - *arg + extra);
2428 rettv->vval.v_string = alloc(len);
2429 if (rettv->vval.v_string == NULL)
2430 return FAIL;
2431 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002432
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002433 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002434 {
2435 if (*p == '\\')
2436 {
2437 switch (*++p)
2438 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002439 case 'b': *end++ = BS; ++p; break;
2440 case 'e': *end++ = ESC; ++p; break;
2441 case 'f': *end++ = FF; ++p; break;
2442 case 'n': *end++ = NL; ++p; break;
2443 case 'r': *end++ = CAR; ++p; break;
2444 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002445
2446 case 'X': // hex: "\x1", "\x12"
2447 case 'x':
2448 case 'u': // Unicode: "\u0023"
2449 case 'U':
2450 if (vim_isxdigit(p[1]))
2451 {
2452 int n, nr;
Keith Thompson184f71c2024-01-04 21:19:04 +01002453 int c = SAFE_toupper(*p);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002454
2455 if (c == 'X')
2456 n = 2;
2457 else if (*p == 'u')
2458 n = 4;
2459 else
2460 n = 8;
2461 nr = 0;
2462 while (--n >= 0 && vim_isxdigit(p[1]))
2463 {
2464 ++p;
2465 nr = (nr << 4) + hex2nr(*p);
2466 }
2467 ++p;
2468 // For "\u" store the number according to
2469 // 'encoding'.
2470 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002471 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002472 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002473 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002474 }
2475 break;
2476
2477 // octal: "\1", "\12", "\123"
2478 case '0':
2479 case '1':
2480 case '2':
2481 case '3':
2482 case '4':
2483 case '5':
2484 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002485 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002486 if (*p >= '0' && *p <= '7')
2487 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002488 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002489 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002490 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002491 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002492 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002493 break;
2494
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002495 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002496 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002497 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002498 int flags = FSK_KEYCODE | FSK_IN_STRING;
2499
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002500 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002501 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002502 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002503 if (extra != 0)
2504 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002505 end += extra;
2506 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002507 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002508 break;
2509 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002510 }
2511 // FALLTHROUGH
2512
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002513 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002514 break;
2515 }
2516 }
2517 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002518 {
2519 if (interpolate && (*p == '{' || *p == '}'))
2520 {
2521 if (*p == '{' && p[1] != '{') // start of expression
2522 break;
2523 ++p; // reduce "{{" to "{" and "}}" to "}"
2524 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002525 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002526 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002527 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002528 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002529 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002530 ++p;
2531 *arg = p;
2532
2533 return OK;
2534}
2535
2536/*
2537 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002538 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2539 * Return OK when a "rettv" was set to the string.
2540 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002541 */
2542 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002543eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002544{
2545 char_u *p;
2546 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002547 int reduce = interpolate ? -1 : 0;
2548 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002549
2550 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002551 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002552 {
2553 if (*p == '\'')
2554 {
2555 if (p[1] != '\'')
2556 break;
2557 ++reduce;
2558 ++p;
2559 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002560 else if (interpolate)
2561 {
2562 if (*p == '{')
2563 {
2564 if (p[1] != '{')
2565 break;
2566 ++p;
2567 ++reduce;
2568 }
2569 else if (*p == '}')
2570 {
2571 ++p;
2572 if (*p != '}')
2573 {
2574 semsg(_(e_stray_closing_curly_str), *arg);
2575 return FAIL;
2576 }
2577 ++reduce;
2578 }
2579 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002580 }
2581
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002582 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002583 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002584 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002585 return FAIL;
2586 }
2587
2588 // If only parsing return after setting "*arg"
2589 if (!evaluate)
2590 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002591 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002592 return OK;
2593 }
2594
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002595 // Copy the string into allocated memory, handling '' to ' reduction and
2596 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002597 str = alloc((p - *arg) - reduce);
2598 if (str == NULL)
2599 return FAIL;
2600 rettv->v_type = VAR_STRING;
2601 rettv->vval.v_string = str;
2602
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002603 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002604 {
2605 if (*p == '\'')
2606 {
2607 if (p[1] != '\'')
2608 break;
2609 ++p;
2610 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002611 else if (interpolate && (*p == '{' || *p == '}'))
2612 {
2613 if (*p == '{' && p[1] != '{')
2614 break;
2615 ++p;
2616 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002617 MB_COPY_CHAR(p, str);
2618 }
2619 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002620 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002621
2622 return OK;
2623}
2624
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002625/*
2626 * Evaluate a single or double quoted string possibly containing expressions.
2627 * "arg" points to the '$'. The result is put in "rettv".
2628 * Returns OK or FAIL.
2629 */
LemonBoy2eaef102022-05-06 13:14:50 +01002630 int
2631eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2632{
2633 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002634 int ret = OK;
2635 int quote;
2636 garray_T ga;
2637 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002638
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002639 ga_init2(&ga, 1, 80);
2640
2641 // *arg is on the '$' character, move it to the first string character.
2642 ++*arg;
2643 quote = **arg;
2644 ++*arg;
2645
2646 for (;;)
2647 {
2648 // Get the string up to the matching quote or to a single '{'.
2649 // "arg" is advanced to either the quote or the '{'.
2650 if (quote == '"')
2651 ret = eval_string(arg, &tv, evaluate, TRUE);
2652 else
2653 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2654 if (ret == FAIL)
2655 break;
2656 if (evaluate)
2657 {
2658 ga_concat(&ga, tv.vval.v_string);
2659 clear_tv(&tv);
2660 }
2661
2662 if (**arg != '{')
2663 {
2664 // found terminating quote
2665 ++*arg;
2666 break;
2667 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002668 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002669 if (p == NULL)
2670 {
2671 ret = FAIL;
2672 break;
2673 }
2674 *arg = p;
2675 }
LemonBoy2eaef102022-05-06 13:14:50 +01002676
2677 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002678 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2679 {
2680 ga_clear(&ga);
2681 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002682 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002683 }
LemonBoy2eaef102022-05-06 13:14:50 +01002684
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002685 rettv->vval.v_string = ga.ga_data;
2686 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002687}
2688
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002689/*
2690 * Return a string with the string representation of a variable.
2691 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2692 * "numbuf" is used for a number.
2693 * Puts quotes around strings, so that they can be parsed back by eval().
2694 * May return NULL.
2695 */
2696 char_u *
2697tv2string(
2698 typval_T *tv,
2699 char_u **tofree,
2700 char_u *numbuf,
2701 int copyID)
2702{
2703 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2704}
2705
2706/*
2707 * Get the value of an environment variable.
2708 * "arg" is pointing to the '$'. It is advanced to after the name.
2709 * If the environment variable was not set, silently assume it is empty.
2710 * Return FAIL if the name is invalid.
2711 */
2712 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002713eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002714{
2715 char_u *string = NULL;
2716 int len;
2717 int cc;
2718 char_u *name;
2719 int mustfree = FALSE;
2720
2721 ++*arg;
2722 name = *arg;
2723 len = get_env_len(arg);
2724 if (evaluate)
2725 {
2726 if (len == 0)
2727 return FAIL; // invalid empty name
2728
2729 cc = name[len];
2730 name[len] = NUL;
2731 // first try vim_getenv(), fast for normal environment vars
2732 string = vim_getenv(name, &mustfree);
2733 if (string != NULL && *string != NUL)
2734 {
2735 if (!mustfree)
2736 string = vim_strsave(string);
2737 }
2738 else
2739 {
2740 if (mustfree)
2741 vim_free(string);
2742
2743 // next try expanding things like $VIM and ${HOME}
2744 string = expand_env_save(name - 1);
2745 if (string != NULL && *string == '$')
2746 VIM_CLEAR(string);
2747 }
2748 name[len] = cc;
2749
2750 rettv->v_type = VAR_STRING;
2751 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002752 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002753 }
2754
2755 return OK;
2756}
2757
2758/*
2759 * Get the lnum from the first argument.
2760 * Also accepts ".", "$", etc., but that only works for the current buffer.
2761 * Returns -1 on error.
2762 */
2763 linenr_T
2764tv_get_lnum(typval_T *argvars)
2765{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002766 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002767 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002768
Bram Moolenaar56acb092020-08-16 14:48:19 +02002769 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2770 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002771 if (lnum <= 0 && did_emsg_before == did_emsg
2772 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002773 {
2774 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002775 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002776
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002777 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002778 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002779 if (fp != NULL)
2780 lnum = fp->lnum;
2781 }
2782 return lnum;
2783}
2784
2785/*
2786 * Get the lnum from the first argument.
2787 * Also accepts "$", then "buf" is used.
2788 * Returns 0 on error.
2789 */
2790 linenr_T
2791tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2792{
2793 if (argvars[0].v_type == VAR_STRING
2794 && argvars[0].vval.v_string != NULL
2795 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002796 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002797 && buf != NULL)
2798 return buf->b_ml.ml_line_count;
2799 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2800}
2801
2802/*
2803 * Get buffer by number or pattern.
2804 */
2805 buf_T *
2806tv_get_buf(typval_T *tv, int curtab_only)
2807{
2808 char_u *name = tv->vval.v_string;
2809 buf_T *buf;
2810
2811 if (tv->v_type == VAR_NUMBER)
2812 return buflist_findnr((int)tv->vval.v_number);
2813 if (tv->v_type != VAR_STRING)
2814 return NULL;
2815 if (name == NULL || *name == NUL)
2816 return curbuf;
2817 if (name[0] == '$' && name[1] == NUL)
2818 return lastbuf;
2819
2820 buf = buflist_find_by_name(name, curtab_only);
2821
2822 // If not found, try expanding the name, like done for bufexists().
2823 if (buf == NULL)
2824 buf = find_buffer(tv);
2825
2826 return buf;
2827}
2828
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002829/*
2830 * Like tv_get_buf() but give an error message is the type is wrong.
2831 */
2832 buf_T *
2833tv_get_buf_from_arg(typval_T *tv)
2834{
2835 buf_T *buf;
2836
2837 ++emsg_off;
2838 buf = tv_get_buf(tv, FALSE);
2839 --emsg_off;
2840 if (buf == NULL
2841 && tv->v_type != VAR_NUMBER
2842 && tv->v_type != VAR_STRING)
2843 // issue errmsg for type error
2844 (void)tv_get_number(tv);
2845 return buf;
2846}
2847
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002848#endif // FEAT_EVAL