blob: e57d8981503ce90a9e38c0bd89d8f9eb3b1dffaf [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/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200530 * Check for an optional bool argument at 'idx'.
531 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200532 */
533 int
534check_for_opt_bool_arg(typval_T *args, int idx)
535{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200536 if (args[idx].v_type == VAR_UNKNOWN)
537 return OK;
538 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200539}
540
541/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200542 * Give an error and return FAIL unless "args[idx]" is a blob.
543 */
544 int
545check_for_blob_arg(typval_T *args, int idx)
546{
547 if (args[idx].v_type != VAR_BLOB)
548 {
rbtnnddc80af2021-12-17 18:01:31 +0000549 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200550 return FAIL;
551 }
552 return OK;
553}
554
555/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200556 * Give an error and return FAIL unless "args[idx]" is a list.
557 */
558 int
559check_for_list_arg(typval_T *args, int idx)
560{
561 if (args[idx].v_type != VAR_LIST)
562 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200563 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200564 return FAIL;
565 }
566 return OK;
567}
568
569/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100570 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
571 */
572 int
573check_for_nonnull_list_arg(typval_T *args, int idx)
574{
575 if (check_for_list_arg(args, idx) == FAIL)
576 return FAIL;
577
578 if (args[idx].vval.v_list == NULL)
579 {
580 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
581 return FAIL;
582 }
583 return OK;
584}
585
586/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200587 * Check for an optional list argument at 'idx'
588 */
589 int
590check_for_opt_list_arg(typval_T *args, int idx)
591{
592 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100593 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200594}
595
596/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200597 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200598 */
599 int
600check_for_dict_arg(typval_T *args, int idx)
601{
602 if (args[idx].v_type != VAR_DICT)
603 {
rbtnnddc80af2021-12-17 18:01:31 +0000604 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200605 return FAIL;
606 }
607 return OK;
608}
609
610/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100611 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
612 */
613 int
614check_for_nonnull_dict_arg(typval_T *args, int idx)
615{
616 if (check_for_dict_arg(args, idx) == FAIL)
617 return FAIL;
618
619 if (args[idx].vval.v_dict == NULL)
620 {
621 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
622 return FAIL;
623 }
624 return OK;
625}
626
627/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200628 * Check for an optional dict argument at 'idx'
629 */
630 int
631check_for_opt_dict_arg(typval_T *args, int idx)
632{
633 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100634 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200635}
636
Yegappan Lakshmananfa378352024-02-01 22:05:27 +0100637/*
638 * Check for an optional non-NULL dict argument at 'idx'
639 */
640 int
641check_for_opt_nonnull_dict_arg(typval_T *args, int idx)
642{
643 return (args[idx].v_type == VAR_UNKNOWN
644 || check_for_nonnull_dict_arg(args, idx) != FAIL) ? OK : FAIL;
645}
646
Dominique Pelle748b3082022-01-08 12:41:16 +0000647#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200648/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200649 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
650 */
651 int
652check_for_chan_or_job_arg(typval_T *args, int idx)
653{
654 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
655 {
rbtnnddc80af2021-12-17 18:01:31 +0000656 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200657 return FAIL;
658 }
659 return OK;
660}
661
662/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200663 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
664 * job.
665 */
666 int
667check_for_opt_chan_or_job_arg(typval_T *args, int idx)
668{
669 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100670 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200671}
672
673/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200674 * Give an error and return FAIL unless "args[idx]" is a job.
675 */
676 int
677check_for_job_arg(typval_T *args, int idx)
678{
679 if (args[idx].v_type != VAR_JOB)
680 {
rbtnnddc80af2021-12-17 18:01:31 +0000681 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200682 return FAIL;
683 }
684 return OK;
685}
686
687/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200688 * Check for an optional job argument at 'idx'.
689 */
690 int
691check_for_opt_job_arg(typval_T *args, int idx)
692{
693 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100694 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200695}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000696#else
697/*
698 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
699 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
700 */
701 int
702check_for_opt_chan_or_job_arg(typval_T *args, int idx)
703{
704 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
705}
Dominique Pelle748b3082022-01-08 12:41:16 +0000706#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200707
708/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200709 * Give an error and return FAIL unless "args[idx]" is a string or
710 * a number.
711 */
712 int
713check_for_string_or_number_arg(typval_T *args, int idx)
714{
715 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
716 {
rbtnnddc80af2021-12-17 18:01:31 +0000717 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200718 return FAIL;
719 }
720 return OK;
721}
722
723/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200724 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200725 */
726 int
727check_for_opt_string_or_number_arg(typval_T *args, int idx)
728{
729 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100730 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200731}
732
733/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200734 * Give an error and return FAIL unless "args[idx]" is a buffer number.
735 * Buffer number can be a number or a string.
736 */
737 int
738check_for_buffer_arg(typval_T *args, int idx)
739{
740 return check_for_string_or_number_arg(args, idx);
741}
742
743/*
744 * Check for an optional buffer argument at 'idx'
745 */
746 int
747check_for_opt_buffer_arg(typval_T *args, int idx)
748{
749 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100750 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200751}
752
753/*
754 * Give an error and return FAIL unless "args[idx]" is a line number.
755 * Line number can be a number or a string.
756 */
757 int
758check_for_lnum_arg(typval_T *args, int idx)
759{
760 return check_for_string_or_number_arg(args, idx);
761}
762
763/*
764 * Check for an optional line number argument at 'idx'
765 */
766 int
767check_for_opt_lnum_arg(typval_T *args, int idx)
768{
769 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100770 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200771}
772
Dominique Pelle748b3082022-01-08 12:41:16 +0000773#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200774/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200775 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200776 */
777 int
778check_for_string_or_blob_arg(typval_T *args, int idx)
779{
780 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
781 {
rbtnnddc80af2021-12-17 18:01:31 +0000782 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200783 return FAIL;
784 }
785 return OK;
786}
Dominique Pelle748b3082022-01-08 12:41:16 +0000787#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200788
789/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200790 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200791 */
792 int
793check_for_string_or_list_arg(typval_T *args, int idx)
794{
795 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
796 {
rbtnnddc80af2021-12-17 18:01:31 +0000797 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200798 return FAIL;
799 }
800 return OK;
801}
802
803/*
rbtnn0ccb5842021-12-18 18:33:46 +0000804 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
805 * blob.
806 */
807 int
808check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
809{
810 if (args[idx].v_type != VAR_STRING
811 && args[idx].v_type != VAR_LIST
812 && args[idx].v_type != VAR_BLOB)
813 {
814 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
815 return FAIL;
816 }
817 return OK;
818}
819
820/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200821 * Check for an optional string or list argument at 'idx'
822 */
823 int
824check_for_opt_string_or_list_arg(typval_T *args, int idx)
825{
826 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100827 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200828}
829
830/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200831 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
832 */
833 int
834check_for_string_or_dict_arg(typval_T *args, int idx)
835{
836 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
837 {
rbtnnddc80af2021-12-17 18:01:31 +0000838 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200839 return FAIL;
840 }
841 return OK;
842}
843
844/*
845 * Give an error and return FAIL unless "args[idx]" is a string or a number
846 * or a list.
847 */
848 int
849check_for_string_or_number_or_list_arg(typval_T *args, int idx)
850{
851 if (args[idx].v_type != VAR_STRING
852 && args[idx].v_type != VAR_NUMBER
853 && args[idx].v_type != VAR_LIST)
854 {
rbtnn0ccb5842021-12-18 18:33:46 +0000855 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200856 return FAIL;
857 }
858 return OK;
859}
860
861/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200862 * Give an error and return FAIL unless "args[idx]" is an optional string
863 * or number or a list
864 */
865 int
866check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
867{
868 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100869 || check_for_string_or_number_or_list_arg(args, idx)
870 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200871}
872
873/*
Bakudankun375141e2022-09-09 18:46:47 +0100874 * Give an error and return FAIL unless "args[idx]" is a string or a number
875 * or a list or a blob.
876 */
877 int
878check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
879{
880 if (args[idx].v_type != VAR_STRING
881 && args[idx].v_type != VAR_NUMBER
882 && args[idx].v_type != VAR_LIST
883 && args[idx].v_type != VAR_BLOB)
884 {
885 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
886 return FAIL;
887 }
888 return OK;
889}
890
891/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200892 * Give an error and return FAIL unless "args[idx]" is a string or a list
893 * or a dict.
894 */
895 int
896check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
897{
898 if (args[idx].v_type != VAR_STRING
899 && args[idx].v_type != VAR_LIST
900 && args[idx].v_type != VAR_DICT)
901 {
rbtnnddc80af2021-12-17 18:01:31 +0000902 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200903 return FAIL;
904 }
905 return OK;
906}
907
908/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000909 * Give an error and return FAIL unless "args[idx]" is a string
910 * or a function reference.
911 */
912 int
913check_for_string_or_func_arg(typval_T *args, int idx)
914{
915 if (args[idx].v_type != VAR_PARTIAL
916 && args[idx].v_type != VAR_FUNC
917 && args[idx].v_type != VAR_STRING)
918 {
919 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
920 return FAIL;
921 }
922 return OK;
923}
924
925/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200926 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
927 */
928 int
929check_for_list_or_blob_arg(typval_T *args, int idx)
930{
931 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
932 {
rbtnn0ccb5842021-12-18 18:33:46 +0000933 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200934 return FAIL;
935 }
936 return OK;
937}
938
939/*
940 * Give an error and return FAIL unless "args[idx]" is a list or dict
941 */
942 int
943check_for_list_or_dict_arg(typval_T *args, int idx)
944{
945 if (args[idx].v_type != VAR_LIST
946 && args[idx].v_type != VAR_DICT)
947 {
rbtnnddc80af2021-12-17 18:01:31 +0000948 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200949 return FAIL;
950 }
951 return OK;
952}
953
954/*
955 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
956 * blob.
957 */
958 int
959check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
960{
961 if (args[idx].v_type != VAR_LIST
962 && args[idx].v_type != VAR_DICT
963 && args[idx].v_type != VAR_BLOB)
964 {
rbtnnddc80af2021-12-17 18:01:31 +0000965 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200966 return FAIL;
967 }
968 return OK;
969}
970
971/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000972 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
973 * blob or a string.
974 */
975 int
976check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
977{
978 if (args[idx].v_type != VAR_LIST
979 && args[idx].v_type != VAR_DICT
980 && args[idx].v_type != VAR_BLOB
981 && args[idx].v_type != VAR_STRING)
982 {
rbtnnddc80af2021-12-17 18:01:31 +0000983 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000984 return FAIL;
985 }
986 return OK;
987}
988
989/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200990 * Give an error and return FAIL unless "args[idx]" is an optional buffer
991 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200992 */
993 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200994check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200995{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200996 if (args[idx].v_type != VAR_UNKNOWN
997 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200998 && args[idx].v_type != VAR_NUMBER
999 && args[idx].v_type != VAR_DICT)
1000 {
rbtnnddc80af2021-12-17 18:01:31 +00001001 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02001002 return FAIL;
1003 }
1004 return OK;
1005}
1006
1007/*
LemonBoyafe04662023-08-23 21:08:11 +02001008 * Give an error and return FAIL unless "args[idx]" is an object.
1009 */
1010 int
1011check_for_object_arg(typval_T *args, int idx)
1012{
1013 if (args[idx].v_type != VAR_OBJECT)
1014 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001015 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001016 return FAIL;
1017 }
1018 return OK;
1019}
1020
1021/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001022 * Returns TRUE if "tv" is a type alias for a class
1023 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01001024 static int
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001025tv_class_alias(typval_T *tv)
1026{
1027 return tv->v_type == VAR_TYPEALIAS &&
1028 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1029}
1030
1031/*
Ernie Rael2025af12023-12-12 16:58:00 +01001032 * Give an error and return FAIL unless "args[idx]" is a class
1033 * or class typealias.
LemonBoyafe04662023-08-23 21:08:11 +02001034 */
1035 int
Ernie Rael2025af12023-12-12 16:58:00 +01001036check_for_class_or_typealias_args(typval_T *args, int idx)
LemonBoyafe04662023-08-23 21:08:11 +02001037{
Ernie Rael2025af12023-12-12 16:58:00 +01001038 for (int i = idx; args[i].v_type != VAR_UNKNOWN; ++i)
LemonBoyafe04662023-08-23 21:08:11 +02001039 {
Ernie Rael2025af12023-12-12 16:58:00 +01001040 if (args[i].v_type != VAR_CLASS && !tv_class_alias(&args[idx]))
1041 {
1042 semsg(_(e_class_or_typealias_required_for_argument_nr), i + 1);
1043 return FAIL;
1044 }
LemonBoyafe04662023-08-23 21:08:11 +02001045 }
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001050 * Get the string value of a variable.
1051 * If it is a Number variable, the number is converted into a string.
1052 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1053 * tv_get_string_buf() uses a given buffer.
1054 * If the String variable has never been set, return an empty string.
1055 * Never returns NULL;
1056 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1057 * NULL on error.
1058 */
1059 char_u *
1060tv_get_string(typval_T *varp)
1061{
1062 static char_u mybuf[NUMBUFLEN];
1063
1064 return tv_get_string_buf(varp, mybuf);
1065}
1066
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001067/*
1068 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1069 */
1070 char_u *
1071tv_get_string_strict(typval_T *varp)
1072{
1073 static char_u mybuf[NUMBUFLEN];
1074 char_u *res = tv_get_string_buf_chk_strict(
1075 varp, mybuf, in_vim9script());
1076
1077 return res != NULL ? res : (char_u *)"";
1078}
1079
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001080 char_u *
1081tv_get_string_buf(typval_T *varp, char_u *buf)
1082{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001083 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001084
1085 return res != NULL ? res : (char_u *)"";
1086}
1087
1088/*
1089 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1090 */
1091 char_u *
1092tv_get_string_chk(typval_T *varp)
1093{
1094 static char_u mybuf[NUMBUFLEN];
1095
1096 return tv_get_string_buf_chk(varp, mybuf);
1097}
1098
1099 char_u *
1100tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1101{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001102 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1103}
1104
1105 char_u *
1106tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1107{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001108 switch (varp->v_type)
1109 {
1110 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001111 if (strict)
1112 {
1113 emsg(_(e_using_number_as_string));
1114 break;
1115 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001116 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1117 (varnumber_T)varp->vval.v_number);
1118 return buf;
1119 case VAR_FUNC:
1120 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001121 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001122 break;
1123 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001124 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001125 break;
1126 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001127 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001128 break;
1129 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001130 if (strict)
1131 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001132 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001133 break;
1134 }
1135 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1136 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001137 case VAR_STRING:
1138 if (varp->vval.v_string != NULL)
1139 return varp->vval.v_string;
1140 return (char_u *)"";
1141 case VAR_BOOL:
1142 case VAR_SPECIAL:
1143 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1144 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001145 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001146 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001147 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001148 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +01001149 case VAR_TYPEALIAS:
1150 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001151 break;
1152 case VAR_OBJECT:
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001153 {
Ernie Raelbe828252024-07-26 18:37:02 +02001154 if (varp->vval.v_object == NULL)
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001155 emsg(_(e_using_object_as_string));
Ernie Raelbe828252024-07-26 18:37:02 +02001156 else
1157 {
1158 class_T *cl = varp->vval.v_object->obj_class;
1159 if (cl != NULL && IS_ENUM(cl))
1160 semsg(_(e_using_enum_str_as_string), cl->class_name);
1161 else
1162 emsg(_(e_using_object_as_string));
1163 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001164 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001165 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001166 case VAR_JOB:
1167#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001168 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001169 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001170 semsg(_(e_using_invalid_value_as_string_str), "job");
1171 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001172 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001173 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001174#endif
1175 break;
1176 case VAR_CHANNEL:
1177#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001178 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001179 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001180 semsg(_(e_using_invalid_value_as_string_str), "channel");
1181 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001182 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001183 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001184#endif
1185 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001186 case VAR_VOID:
1187 emsg(_(e_cannot_use_void_value));
1188 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001189 case VAR_UNKNOWN:
1190 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001191 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001192 semsg(_(e_using_invalid_value_as_string_str),
1193 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001194 break;
1195 }
1196 return NULL;
1197}
1198
1199/*
1200 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1201 * string() on Dict, List, etc.
1202 */
1203 char_u *
1204tv_stringify(typval_T *varp, char_u *buf)
1205{
1206 if (varp->v_type == VAR_LIST
1207 || varp->v_type == VAR_DICT
1208 || varp->v_type == VAR_BLOB
1209 || varp->v_type == VAR_FUNC
1210 || varp->v_type == VAR_PARTIAL
1211 || varp->v_type == VAR_FLOAT)
1212 {
1213 typval_T tmp;
1214
1215 f_string(varp, &tmp);
1216 tv_get_string_buf(&tmp, buf);
1217 clear_tv(varp);
1218 *varp = tmp;
1219 return tmp.vval.v_string;
1220 }
1221 return tv_get_string_buf(varp, buf);
1222}
1223
1224/*
1225 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1226 * Also give an error message, using "name" or _("name") when use_gettext is
1227 * TRUE.
1228 */
1229 int
1230tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1231{
1232 int lock = 0;
1233
1234 switch (tv->v_type)
1235 {
1236 case VAR_BLOB:
1237 if (tv->vval.v_blob != NULL)
1238 lock = tv->vval.v_blob->bv_lock;
1239 break;
1240 case VAR_LIST:
1241 if (tv->vval.v_list != NULL)
1242 lock = tv->vval.v_list->lv_lock;
1243 break;
1244 case VAR_DICT:
1245 if (tv->vval.v_dict != NULL)
1246 lock = tv->vval.v_dict->dv_lock;
1247 break;
1248 default:
1249 break;
1250 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001251 return value_check_lock(tv->v_lock, name, use_gettext)
1252 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001253}
1254
1255/*
1256 * Copy the values from typval_T "from" to typval_T "to".
1257 * When needed allocates string or increases reference count.
1258 * Does not make a copy of a list, blob or dict but copies the reference!
1259 * It is OK for "from" and "to" to point to the same item. This is used to
1260 * make a copy later.
1261 */
1262 void
1263copy_tv(typval_T *from, typval_T *to)
1264{
1265 to->v_type = from->v_type;
1266 to->v_lock = 0;
1267 switch (from->v_type)
1268 {
1269 case VAR_NUMBER:
1270 case VAR_BOOL:
1271 case VAR_SPECIAL:
1272 to->vval.v_number = from->vval.v_number;
1273 break;
1274 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001275 to->vval.v_float = from->vval.v_float;
1276 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 case VAR_JOB:
1278#ifdef FEAT_JOB_CHANNEL
1279 to->vval.v_job = from->vval.v_job;
1280 if (to->vval.v_job != NULL)
1281 ++to->vval.v_job->jv_refcount;
1282 break;
1283#endif
1284 case VAR_CHANNEL:
1285#ifdef FEAT_JOB_CHANNEL
1286 to->vval.v_channel = from->vval.v_channel;
1287 if (to->vval.v_channel != NULL)
1288 ++to->vval.v_channel->ch_refcount;
1289 break;
1290#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001291 case VAR_INSTR:
1292 to->vval.v_instr = from->vval.v_instr;
1293 break;
1294
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001295 case VAR_CLASS:
1296 copy_class(from, to);
1297 break;
1298
1299 case VAR_OBJECT:
1300 copy_object(from, to);
1301 break;
1302
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001303 case VAR_STRING:
1304 case VAR_FUNC:
1305 if (from->vval.v_string == NULL)
1306 to->vval.v_string = NULL;
1307 else
1308 {
1309 to->vval.v_string = vim_strsave(from->vval.v_string);
1310 if (from->v_type == VAR_FUNC)
1311 func_ref(to->vval.v_string);
1312 }
1313 break;
1314 case VAR_PARTIAL:
1315 if (from->vval.v_partial == NULL)
1316 to->vval.v_partial = NULL;
1317 else
1318 {
1319 to->vval.v_partial = from->vval.v_partial;
1320 ++to->vval.v_partial->pt_refcount;
1321 }
1322 break;
1323 case VAR_BLOB:
1324 if (from->vval.v_blob == NULL)
1325 to->vval.v_blob = NULL;
1326 else
1327 {
1328 to->vval.v_blob = from->vval.v_blob;
1329 ++to->vval.v_blob->bv_refcount;
1330 }
1331 break;
1332 case VAR_LIST:
1333 if (from->vval.v_list == NULL)
1334 to->vval.v_list = NULL;
1335 else
1336 {
1337 to->vval.v_list = from->vval.v_list;
1338 ++to->vval.v_list->lv_refcount;
1339 }
1340 break;
1341 case VAR_DICT:
1342 if (from->vval.v_dict == NULL)
1343 to->vval.v_dict = NULL;
1344 else
1345 {
1346 to->vval.v_dict = from->vval.v_dict;
1347 ++to->vval.v_dict->dv_refcount;
1348 }
1349 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001350 case VAR_TYPEALIAS:
1351 if (from->vval.v_typealias == NULL)
1352 to->vval.v_typealias = NULL;
1353 else
1354 {
1355 to->vval.v_typealias = from->vval.v_typealias;
1356 ++to->vval.v_typealias->ta_refcount;
1357 }
1358 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001359 case VAR_VOID:
1360 emsg(_(e_cannot_use_void_value));
1361 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001362 case VAR_UNKNOWN:
1363 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001364 internal_error_no_abort("copy_tv(UNKNOWN)");
1365 break;
1366 }
1367}
1368
1369/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001370 * Compare "tv1" and "tv2".
1371 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001372 */
1373 int
1374typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001375 typval_T *tv1, // first operand
1376 typval_T *tv2, // second operand
1377 exprtype_T type, // operator
1378 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001379{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001380 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001381 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001382 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1383
Ernie Raele75fde62023-12-21 17:18:54 +01001384 if (check_typval_is_value(tv1) == FAIL
1385 || check_typval_is_value(tv2) == FAIL)
1386 {
1387 clear_tv(tv1);
1388 return FAIL;
1389 }
1390 else if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001391 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001392 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001393 // it means TRUE.
1394 n1 = (type == EXPR_ISNOT);
1395 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001396 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1397 || (tv2->v_type == VAR_SPECIAL
1398 && tv2->vval.v_number == VVAL_NULL))
1399 && tv1->v_type != tv2->v_type
1400 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1401 {
1402 n1 = typval_compare_null(tv1, tv2);
1403 if (n1 == MAYBE)
1404 {
1405 clear_tv(tv1);
1406 return FAIL;
1407 }
1408 if (type == EXPR_NEQUAL)
1409 n1 = !n1;
1410 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001411 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001412 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001413 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001414 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001415 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001416 return FAIL;
1417 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001418 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001419 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001420 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001421 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001422 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001423 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001424 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001425 return FAIL;
1426 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001427 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001428 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001429 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1430 {
1431 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1432 {
1433 clear_tv(tv1);
1434 return FAIL;
1435 }
1436 n1 = res;
1437 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001438 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001439 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001440 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001441 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001442 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001443 return FAIL;
1444 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001445 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001446 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001447 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1448 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001449 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001450 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001451 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001452 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001453 return FAIL;
1454 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001455 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001456 }
1457
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001458 // If one of the two variables is a float, compare as a float.
1459 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001460 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001461 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1462 {
1463 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001464 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001465
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001466 f1 = tv_get_float_chk(tv1, &error);
1467 if (!error)
1468 f2 = tv_get_float_chk(tv2, &error);
1469 if (error)
1470 {
1471 clear_tv(tv1);
1472 return FAIL;
1473 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001474 n1 = FALSE;
1475 switch (type)
1476 {
1477 case EXPR_IS:
1478 case EXPR_EQUAL: n1 = (f1 == f2); break;
1479 case EXPR_ISNOT:
1480 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1481 case EXPR_GREATER: n1 = (f1 > f2); break;
1482 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1483 case EXPR_SMALLER: n1 = (f1 < f2); break;
1484 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1485 case EXPR_UNKNOWN:
1486 case EXPR_MATCH:
1487 default: break; // avoid gcc warning
1488 }
1489 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001490
1491 // If one of the two variables is a number, compare as a number.
1492 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001493 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001494 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1495 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001496 int error = FALSE;
1497
1498 n1 = tv_get_number_chk(tv1, &error);
1499 if (!error)
1500 n2 = tv_get_number_chk(tv2, &error);
1501 if (error)
1502 {
1503 clear_tv(tv1);
1504 return FAIL;
1505 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001506 switch (type)
1507 {
1508 case EXPR_IS:
1509 case EXPR_EQUAL: n1 = (n1 == n2); break;
1510 case EXPR_ISNOT:
1511 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1512 case EXPR_GREATER: n1 = (n1 > n2); break;
1513 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1514 case EXPR_SMALLER: n1 = (n1 < n2); break;
1515 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1516 case EXPR_UNKNOWN:
1517 case EXPR_MATCH:
1518 default: break; // avoid gcc warning
1519 }
1520 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001521 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1522 || tv2->v_type == VAR_BOOL
1523 || (tv1->v_type == VAR_SPECIAL
1524 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001525 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001526 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001527 {
1528 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001529 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1530 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001531 return FAIL;
1532 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001533 n1 = tv1->vval.v_number;
1534 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001535 switch (type)
1536 {
1537 case EXPR_IS:
1538 case EXPR_EQUAL: n1 = (n1 == n2); break;
1539 case EXPR_ISNOT:
1540 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1541 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001542 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001543 vartype_name(tv1->v_type));
1544 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001545 return FAIL;
1546 }
1547 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001548#ifdef FEAT_JOB_CHANNEL
1549 else if (tv1->v_type == tv2->v_type
1550 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1551 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1552 {
1553 if (tv1->v_type == VAR_CHANNEL)
1554 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1555 else
1556 n1 = tv1->vval.v_job == tv2->vval.v_job;
1557 if (type == EXPR_NEQUAL)
1558 n1 = !n1;
1559 }
1560#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001561 else
1562 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001563 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001564 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001565 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001566 return FAIL;
1567 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001568 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001569 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001570 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001571 if (in_vim9script())
1572 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001573 tv1->v_type = VAR_BOOL;
1574 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001575 }
1576 else
1577 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001578 tv1->v_type = VAR_NUMBER;
1579 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001580 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001581
1582 return OK;
1583}
1584
Bram Moolenaar34453202021-01-31 13:08:38 +01001585/*
dundargocc57b5bc2022-11-02 13:30:51 +00001586 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001587 * Put the result, false or true, in "res".
1588 * Return FAIL and give an error message when the comparison can't be done.
1589 */
1590 int
1591typval_compare_list(
1592 typval_T *tv1,
1593 typval_T *tv2,
1594 exprtype_T type,
1595 int ic,
1596 int *res)
1597{
1598 int val = 0;
1599
1600 if (type == EXPR_IS || type == EXPR_ISNOT)
1601 {
1602 val = (tv1->v_type == tv2->v_type
1603 && tv1->vval.v_list == tv2->vval.v_list);
1604 if (type == EXPR_ISNOT)
1605 val = !val;
1606 }
1607 else if (tv1->v_type != tv2->v_type
1608 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1609 {
1610 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001611 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001612 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001613 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001614 return FAIL;
1615 }
1616 else
1617 {
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001618 val = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001619 if (type == EXPR_NEQUAL)
1620 val = !val;
1621 }
1622 *res = val;
1623 return OK;
1624}
1625
1626/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001627 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001628 */
1629 int
1630typval_compare_null(typval_T *tv1, typval_T *tv2)
1631{
1632 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1633 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1634 {
1635 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1636
1637 switch (tv->v_type)
1638 {
1639 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001640#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001641 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001642#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001643 // TODO: null_class handling
1644 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001645 case VAR_DICT: return tv->vval.v_dict == NULL;
1646 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001647#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001648 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001649#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001650 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001651 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001652 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1653 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001654
1655 case VAR_NUMBER: if (!in_vim9script())
1656 return tv->vval.v_number == 0;
1657 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001658 case VAR_FLOAT: if (!in_vim9script())
1659 return tv->vval.v_float == 0.0;
1660 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001661 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001662 default: break;
1663 }
1664 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001665 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001666 // we won't give an error
1667 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001668}
1669
1670/*
dundargocc57b5bc2022-11-02 13:30:51 +00001671 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001672 * Put the result, false or true, in "res".
1673 * Return FAIL and give an error message when the comparison can't be done.
1674 */
1675 int
1676typval_compare_blob(
1677 typval_T *tv1,
1678 typval_T *tv2,
1679 exprtype_T type,
1680 int *res)
1681{
1682 int val = 0;
1683
1684 if (type == EXPR_IS || type == EXPR_ISNOT)
1685 {
1686 val = (tv1->v_type == tv2->v_type
1687 && tv1->vval.v_blob == tv2->vval.v_blob);
1688 if (type == EXPR_ISNOT)
1689 val = !val;
1690 }
1691 else if (tv1->v_type != tv2->v_type
1692 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1693 {
1694 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001695 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001696 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001697 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001698 return FAIL;
1699 }
1700 else
1701 {
1702 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1703 if (type == EXPR_NEQUAL)
1704 val = !val;
1705 }
1706 *res = val;
1707 return OK;
1708}
1709
1710/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001711 * Compare "tv1" to "tv2" as objects according to "type".
1712 * Put the result, false or true, in "res".
1713 * Return FAIL and give an error message when the comparison can't be done.
1714 */
1715 int
1716typval_compare_object(
1717 typval_T *tv1,
1718 typval_T *tv2,
1719 exprtype_T type,
1720 int ic,
1721 int *res)
1722{
1723 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1724
1725 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1726 {
1727 *res = res_match;
1728 return OK;
1729 }
1730 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1731 {
1732 *res = !res_match;
1733 return OK;
1734 }
1735
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001736 object_T *obj1 = tv1->vval.v_object;
1737 object_T *obj2 = tv2->vval.v_object;
1738 if (type == EXPR_IS || type == EXPR_ISNOT)
1739 {
1740 *res = obj1 == obj2 ? res_match : !res_match;
1741 return OK;
1742 }
1743
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001744 *res = object_equal(obj1, obj2, ic) ? res_match : !res_match;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001745 return OK;
1746}
1747
1748/*
dundargocc57b5bc2022-11-02 13:30:51 +00001749 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001750 * Put the result, false or true, in "res".
1751 * Return FAIL and give an error message when the comparison can't be done.
1752 */
1753 int
1754typval_compare_dict(
1755 typval_T *tv1,
1756 typval_T *tv2,
1757 exprtype_T type,
1758 int ic,
1759 int *res)
1760{
1761 int val;
1762
1763 if (type == EXPR_IS || type == EXPR_ISNOT)
1764 {
1765 val = (tv1->v_type == tv2->v_type
1766 && tv1->vval.v_dict == tv2->vval.v_dict);
1767 if (type == EXPR_ISNOT)
1768 val = !val;
1769 }
1770 else if (tv1->v_type != tv2->v_type
1771 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1772 {
1773 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001774 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001775 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001776 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001777 return FAIL;
1778 }
1779 else
1780 {
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001781 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001782 if (type == EXPR_NEQUAL)
1783 val = !val;
1784 }
1785 *res = val;
1786 return OK;
1787}
1788
1789/*
dundargocc57b5bc2022-11-02 13:30:51 +00001790 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001791 * Put the result, false or true, in "res".
1792 * Return FAIL and give an error message when the comparison can't be done.
1793 */
1794 int
1795typval_compare_func(
1796 typval_T *tv1,
1797 typval_T *tv2,
1798 exprtype_T type,
1799 int ic,
1800 int *res)
1801{
1802 int val = 0;
1803
1804 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1805 && type != EXPR_IS && type != EXPR_ISNOT)
1806 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001807 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001808 return FAIL;
1809 }
1810 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1811 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1812 // When both partials are NULL, then they are equal.
1813 // Otherwise they are not equal.
1814 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1815 else if (type == EXPR_IS || type == EXPR_ISNOT)
1816 {
1817 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1818 // strings are considered the same if their value is
1819 // the same
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001820 val = tv_equal(tv1, tv2, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001821 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1822 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1823 else
1824 val = FALSE;
1825 }
1826 else
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001827 val = tv_equal(tv1, tv2, ic);
Bram Moolenaar265f8112021-12-19 12:33:05 +00001828 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1829 val = !val;
1830 *res = val;
1831 return OK;
1832}
1833
1834/*
1835 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1836 * Put the result, false or true, in "res".
1837 * Return FAIL and give an error message when the comparison can't be done.
1838 */
1839 int
1840typval_compare_string(
1841 typval_T *tv1,
1842 typval_T *tv2,
1843 exprtype_T type,
1844 int ic,
1845 int *res)
1846{
1847 int i = 0;
1848 int val = FALSE;
1849 char_u *s1, *s2;
1850 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1851
1852 if (in_vim9script()
1853 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1854 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1855 {
1856 semsg(_(e_cannot_compare_str_with_str),
1857 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1858 return FAIL;
1859 }
1860 s1 = tv_get_string_buf(tv1, buf1);
1861 s2 = tv_get_string_buf(tv2, buf2);
1862 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1863 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1864 switch (type)
1865 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001866 case EXPR_IS: if (in_vim9script())
1867 {
1868 // Really check it is the same string, not just
1869 // the same value.
1870 val = tv1->vval.v_string == tv2->vval.v_string;
1871 break;
1872 }
1873 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001874 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001875 case EXPR_ISNOT: if (in_vim9script())
1876 {
1877 // Really check it is not the same string, not
1878 // just a different value.
1879 val = tv1->vval.v_string != tv2->vval.v_string;
1880 break;
1881 }
1882 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001883 case EXPR_NEQUAL: val = (i != 0); break;
1884 case EXPR_GREATER: val = (i > 0); break;
1885 case EXPR_GEQUAL: val = (i >= 0); break;
1886 case EXPR_SMALLER: val = (i < 0); break;
1887 case EXPR_SEQUAL: val = (i <= 0); break;
1888
1889 case EXPR_MATCH:
1890 case EXPR_NOMATCH:
1891 val = pattern_match(s2, s1, ic);
1892 if (type == EXPR_NOMATCH)
1893 val = !val;
1894 break;
1895
1896 default: break; // avoid gcc warning
1897 }
1898 *res = val;
1899 return OK;
1900}
1901/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001902 * Convert any type to a string, never give an error.
1903 * When "quotes" is TRUE add quotes to a string.
1904 * Returns an allocated string.
1905 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001906 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001907typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001908{
1909 char_u *tofree;
1910 char_u numbuf[NUMBUFLEN];
1911 char_u *ret = NULL;
1912
1913 if (arg == NULL)
1914 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001915 if (!quotes && arg->v_type == VAR_STRING)
1916 {
1917 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1918 : arg->vval.v_string);
1919 }
1920 else
1921 {
1922 ret = tv2string(arg, &tofree, numbuf, 0);
1923 // Make a copy if we have a value but it's not in allocated memory.
1924 if (ret != NULL && tofree == NULL)
1925 ret = vim_strsave(ret);
1926 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001927 return ret;
1928}
1929
1930/*
1931 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1932 * or it refers to a List or Dictionary that is locked.
1933 */
1934 int
1935tv_islocked(typval_T *tv)
1936{
1937 return (tv->v_lock & VAR_LOCKED)
1938 || (tv->v_type == VAR_LIST
1939 && tv->vval.v_list != NULL
1940 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1941 || (tv->v_type == VAR_DICT
1942 && tv->vval.v_dict != NULL
1943 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1944}
1945
1946 static int
1947func_equal(
1948 typval_T *tv1,
1949 typval_T *tv2,
1950 int ic) // ignore case
1951{
1952 char_u *s1, *s2;
1953 dict_T *d1, *d2;
1954 int a1, a2;
1955 int i;
1956
1957 // empty and NULL function name considered the same
1958 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1959 : partial_name(tv1->vval.v_partial);
1960 if (s1 != NULL && *s1 == NUL)
1961 s1 = NULL;
1962 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1963 : partial_name(tv2->vval.v_partial);
1964 if (s2 != NULL && *s2 == NUL)
1965 s2 = NULL;
1966 if (s1 == NULL || s2 == NULL)
1967 {
1968 if (s1 != s2)
1969 return FALSE;
1970 }
1971 else if (STRCMP(s1, s2) != 0)
1972 return FALSE;
1973
1974 // empty dict and NULL dict is different
1975 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1976 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1977 if (d1 == NULL || d2 == NULL)
1978 {
1979 if (d1 != d2)
1980 return FALSE;
1981 }
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001982 else if (!dict_equal(d1, d2, ic))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001983 return FALSE;
1984
1985 // empty list and no list considered the same
1986 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1987 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1988 if (a1 != a2)
1989 return FALSE;
1990 for (i = 0; i < a1; ++i)
1991 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001992 tv2->vval.v_partial->pt_argv + i, ic))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001993 return FALSE;
1994
1995 return TRUE;
1996}
1997
1998/*
1999 * Return TRUE if "tv1" and "tv2" have the same value.
2000 * Compares the items just like "==" would compare them, but strings and
2001 * numbers are different. Floats and numbers are also different.
2002 */
2003 int
2004tv_equal(
2005 typval_T *tv1,
2006 typval_T *tv2,
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002007 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002008{
2009 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2010 char_u *s1, *s2;
2011 static int recursive_cnt = 0; // catch recursive loops
2012 int r;
2013 static int tv_equal_recurse_limit;
2014
2015 // Catch lists and dicts that have an endless loop by limiting
2016 // recursiveness to a limit. We guess they are equal then.
2017 // A fixed limit has the problem of still taking an awful long time.
2018 // Reduce the limit every time running into it. That should work fine for
2019 // deeply linked structures that are not recursively linked and catch
2020 // recursiveness quickly.
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002021 if (recursive_cnt == 0)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002022 tv_equal_recurse_limit = 1000;
2023 if (recursive_cnt >= tv_equal_recurse_limit)
2024 {
2025 --tv_equal_recurse_limit;
2026 return TRUE;
2027 }
2028
2029 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2030 // arguments.
2031 if ((tv1->v_type == VAR_FUNC
2032 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2033 && (tv2->v_type == VAR_FUNC
2034 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2035 {
2036 ++recursive_cnt;
2037 r = func_equal(tv1, tv2, ic);
2038 --recursive_cnt;
2039 return r;
2040 }
2041
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002042 if (tv1->v_type != tv2->v_type
2043 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2044 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002045 return FALSE;
2046
2047 switch (tv1->v_type)
2048 {
2049 case VAR_LIST:
2050 ++recursive_cnt;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002051 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002052 --recursive_cnt;
2053 return r;
2054
2055 case VAR_DICT:
2056 ++recursive_cnt;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002057 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002058 --recursive_cnt;
2059 return r;
2060
2061 case VAR_BLOB:
2062 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2063
2064 case VAR_NUMBER:
2065 case VAR_BOOL:
2066 case VAR_SPECIAL:
2067 return tv1->vval.v_number == tv2->vval.v_number;
2068
2069 case VAR_STRING:
2070 s1 = tv_get_string_buf(tv1, buf1);
2071 s2 = tv_get_string_buf(tv2, buf2);
2072 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2073
2074 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002075 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002076 case VAR_JOB:
2077#ifdef FEAT_JOB_CHANNEL
2078 return tv1->vval.v_job == tv2->vval.v_job;
2079#endif
2080 case VAR_CHANNEL:
2081#ifdef FEAT_JOB_CHANNEL
2082 return tv1->vval.v_channel == tv2->vval.v_channel;
2083#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002084 case VAR_INSTR:
2085 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002086
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002087 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002088 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002089 return tv1->vval.v_class == tv2->vval.v_class;
2090
2091 case VAR_OBJECT:
Ernie Raelf0e69142024-06-22 11:12:00 +02002092 ++recursive_cnt;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02002093 r = object_equal(tv1->vval.v_object, tv2->vval.v_object, ic);
Ernie Raelf0e69142024-06-22 11:12:00 +02002094 --recursive_cnt;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002095 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002096
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002097 case VAR_PARTIAL:
2098 return tv1->vval.v_partial == tv2->vval.v_partial;
2099
2100 case VAR_FUNC:
2101 return tv1->vval.v_string == tv2->vval.v_string;
2102
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002103 case VAR_TYPEALIAS:
2104 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2105
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002106 case VAR_UNKNOWN:
2107 case VAR_ANY:
2108 case VAR_VOID:
2109 break;
2110 }
2111
2112 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2113 // does not equal anything, not even itself.
2114 return FALSE;
2115}
2116
2117/*
2118 * Get an option value.
2119 * "arg" points to the '&' or '+' before the option name.
2120 * "arg" is advanced to character after the option name.
2121 * Return OK or FAIL.
2122 */
2123 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002124eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002125 char_u **arg,
2126 typval_T *rettv, // when NULL, only check if option exists
2127 int evaluate)
2128{
2129 char_u *option_end;
2130 long numval;
2131 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002132 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002133 int c;
2134 int working = (**arg == '+'); // has("+option")
2135 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002136 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002137
2138 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002139 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002140 if (option_end == NULL)
2141 {
2142 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002143 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002144 return FAIL;
2145 }
2146
2147 if (!evaluate)
2148 {
2149 *arg = option_end;
2150 return OK;
2151 }
2152
2153 c = *option_end;
2154 *option_end = NUL;
2155 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002156 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002157
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002158 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002159 {
2160 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002161 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002162 ret = FAIL;
2163 }
2164 else if (rettv != NULL)
2165 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002166 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002167 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002168 {
2169 rettv->v_type = VAR_STRING;
2170 rettv->vval.v_string = NULL;
2171 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002172 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002173 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002174 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2175 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002176 rettv->vval.v_number = 0;
2177 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002178 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002179 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002180 if (in_vim9script() && opt_type == gov_bool)
2181 {
2182 rettv->v_type = VAR_BOOL;
2183 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2184 }
2185 else
2186 {
2187 rettv->v_type = VAR_NUMBER;
2188 rettv->vval.v_number = numval;
2189 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002190 }
2191 else // string option
2192 {
2193 rettv->v_type = VAR_STRING;
2194 rettv->vval.v_string = stringval;
2195 }
2196 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002197 else if (working && (opt_type == gov_hidden_bool
2198 || opt_type == gov_hidden_number
2199 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002200 ret = FAIL;
2201
2202 *option_end = c; // put back for error messages
2203 *arg = option_end;
2204
2205 return ret;
2206}
2207
2208/*
2209 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2210 * Return OK or FAIL.
2211 */
2212 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002213eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002214 char_u **arg,
2215 typval_T *rettv,
2216 int evaluate,
Dominique Pellé0268ff32024-07-28 21:12:20 +02002217 int want_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002218{
2219 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002220 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002221 char_u *p;
2222 int get_float = FALSE;
2223
2224 // We accept a float when the format matches
2225 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2226 // strict to avoid backwards compatibility problems.
2227 // With script version 2 and later the leading digit can be
2228 // omitted.
2229 // Don't look for a float after the "." operator, so that
2230 // ":let vers = 1.2.3" doesn't fail.
2231 if (**arg == '.')
2232 p = *arg;
2233 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002234 {
2235 p = *arg + 1;
2236 if (skip_quotes)
2237 for (;;)
2238 {
2239 if (*p == '\'')
2240 ++p;
2241 if (!vim_isdigit(*p))
2242 break;
2243 p = skipdigits(p);
2244 }
2245 else
2246 p = skipdigits(p);
2247 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002248 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2249 {
2250 get_float = TRUE;
2251 p = skipdigits(p + 2);
2252 if (*p == 'e' || *p == 'E')
2253 {
2254 ++p;
2255 if (*p == '-' || *p == '+')
2256 ++p;
2257 if (!vim_isdigit(*p))
2258 get_float = FALSE;
2259 else
2260 p = skipdigits(p + 1);
2261 }
2262 if (ASCII_ISALPHA(*p) || *p == '.')
2263 get_float = FALSE;
2264 }
2265 if (get_float)
2266 {
2267 float_T f;
2268
Bram Moolenaar29500652021-08-08 15:43:34 +02002269 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002270 if (evaluate)
2271 {
2272 rettv->v_type = VAR_FLOAT;
2273 rettv->vval.v_float = f;
2274 }
2275 }
2276 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002277 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2278 {
2279 char_u *bp;
2280 blob_T *blob = NULL; // init for gcc
2281
2282 // Blob constant: 0z0123456789abcdef
2283 if (evaluate)
2284 blob = blob_alloc();
2285 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2286 {
2287 if (!vim_isxdigit(bp[1]))
2288 {
2289 if (blob != NULL)
2290 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002291 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002292 ga_clear(&blob->bv_ga);
2293 VIM_CLEAR(blob);
2294 }
2295 return FAIL;
2296 }
2297 if (blob != NULL)
2298 ga_append(&blob->bv_ga,
2299 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2300 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2301 ++bp;
2302 }
2303 if (blob != NULL)
2304 rettv_blob_set(rettv, blob);
2305 *arg = bp;
2306 }
2307 else
2308 {
2309 varnumber_T n;
2310
2311 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002312 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002313 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002314 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002315 if (len == 0)
2316 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002317 if (evaluate)
2318 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002319 return FAIL;
2320 }
2321 *arg += len;
2322 if (evaluate)
2323 {
2324 rettv->v_type = VAR_NUMBER;
2325 rettv->vval.v_number = n;
2326 }
2327 }
2328 return OK;
2329}
2330
2331/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002332 * Evaluate a string constant and put the result in "rettv".
2333 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2334 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2335 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002336 * Return OK or FAIL.
2337 */
2338 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002339eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002340{
2341 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002342 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002343 int extra = interpolate ? 1 : 0;
2344 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002345 int len;
2346
2347 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002348 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002349 {
2350 if (*p == '\\' && p[1] != NUL)
2351 {
2352 ++p;
2353 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002354 // to 9 characters (6 for the char and 3 for a modifier):
2355 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002356 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002357 {
2358 int modifiers = 0;
2359 int flags = FSK_KEYCODE | FSK_IN_STRING;
2360
zeertzjqdb088872022-05-02 22:53:45 +01002361 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002362
2363 // Skip to the '>' to avoid using '{' inside for string
2364 // interpolation.
2365 if (p[1] != '*')
2366 flags |= FSK_SIMPLIFY;
2367 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2368 --p; // leave "p" on the ">"
2369 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002370 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002371 else if (interpolate && (*p == '{' || *p == '}'))
2372 {
2373 if (*p == '{' && p[1] != '{') // start of expression
2374 break;
2375 ++p;
2376 if (p[-1] == '}' && *p != '}') // single '}' is an error
2377 {
2378 semsg(_(e_stray_closing_curly_str), *arg);
2379 return FAIL;
2380 }
2381 --extra; // "{{" becomes "{", "}}" becomes "}"
2382 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002383 }
2384
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002385 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002386 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002387 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002388 return FAIL;
2389 }
2390
2391 // If only parsing, set *arg and return here
2392 if (!evaluate)
2393 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002394 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002395 return OK;
2396 }
2397
2398 // Copy the string into allocated memory, handling backslashed
2399 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002400 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002401 len = (int)(p - *arg + extra);
2402 rettv->vval.v_string = alloc(len);
2403 if (rettv->vval.v_string == NULL)
2404 return FAIL;
2405 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002406
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002407 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002408 {
2409 if (*p == '\\')
2410 {
2411 switch (*++p)
2412 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002413 case 'b': *end++ = BS; ++p; break;
2414 case 'e': *end++ = ESC; ++p; break;
2415 case 'f': *end++ = FF; ++p; break;
2416 case 'n': *end++ = NL; ++p; break;
2417 case 'r': *end++ = CAR; ++p; break;
2418 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002419
2420 case 'X': // hex: "\x1", "\x12"
2421 case 'x':
2422 case 'u': // Unicode: "\u0023"
2423 case 'U':
2424 if (vim_isxdigit(p[1]))
2425 {
2426 int n, nr;
Keith Thompson184f71c2024-01-04 21:19:04 +01002427 int c = SAFE_toupper(*p);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002428
2429 if (c == 'X')
2430 n = 2;
2431 else if (*p == 'u')
2432 n = 4;
2433 else
2434 n = 8;
2435 nr = 0;
2436 while (--n >= 0 && vim_isxdigit(p[1]))
2437 {
2438 ++p;
2439 nr = (nr << 4) + hex2nr(*p);
2440 }
2441 ++p;
2442 // For "\u" store the number according to
2443 // 'encoding'.
2444 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002445 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002446 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002447 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002448 }
2449 break;
2450
2451 // octal: "\1", "\12", "\123"
2452 case '0':
2453 case '1':
2454 case '2':
2455 case '3':
2456 case '4':
2457 case '5':
2458 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002459 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002460 if (*p >= '0' && *p <= '7')
2461 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002462 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002463 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002464 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002465 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002466 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002467 break;
2468
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002469 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002470 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002471 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002472 int flags = FSK_KEYCODE | FSK_IN_STRING;
2473
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002474 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002475 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002476 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002477 if (extra != 0)
2478 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002479 end += extra;
2480 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002481 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002482 break;
2483 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002484 }
2485 // FALLTHROUGH
2486
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002487 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002488 break;
2489 }
2490 }
2491 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002492 {
2493 if (interpolate && (*p == '{' || *p == '}'))
2494 {
2495 if (*p == '{' && p[1] != '{') // start of expression
2496 break;
2497 ++p; // reduce "{{" to "{" and "}}" to "}"
2498 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002499 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002500 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002501 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002502 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002503 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002504 ++p;
2505 *arg = p;
2506
2507 return OK;
2508}
2509
2510/*
2511 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002512 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2513 * Return OK when a "rettv" was set to the string.
2514 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002515 */
2516 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002517eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002518{
2519 char_u *p;
2520 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002521 int reduce = interpolate ? -1 : 0;
2522 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002523
2524 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002525 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002526 {
2527 if (*p == '\'')
2528 {
2529 if (p[1] != '\'')
2530 break;
2531 ++reduce;
2532 ++p;
2533 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002534 else if (interpolate)
2535 {
2536 if (*p == '{')
2537 {
2538 if (p[1] != '{')
2539 break;
2540 ++p;
2541 ++reduce;
2542 }
2543 else if (*p == '}')
2544 {
2545 ++p;
2546 if (*p != '}')
2547 {
2548 semsg(_(e_stray_closing_curly_str), *arg);
2549 return FAIL;
2550 }
2551 ++reduce;
2552 }
2553 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002554 }
2555
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002556 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002557 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002558 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002559 return FAIL;
2560 }
2561
2562 // If only parsing return after setting "*arg"
2563 if (!evaluate)
2564 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002565 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002566 return OK;
2567 }
2568
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002569 // Copy the string into allocated memory, handling '' to ' reduction and
2570 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002571 str = alloc((p - *arg) - reduce);
2572 if (str == NULL)
2573 return FAIL;
2574 rettv->v_type = VAR_STRING;
2575 rettv->vval.v_string = str;
2576
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002577 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002578 {
2579 if (*p == '\'')
2580 {
2581 if (p[1] != '\'')
2582 break;
2583 ++p;
2584 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002585 else if (interpolate && (*p == '{' || *p == '}'))
2586 {
2587 if (*p == '{' && p[1] != '{')
2588 break;
2589 ++p;
2590 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002591 MB_COPY_CHAR(p, str);
2592 }
2593 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002594 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002595
2596 return OK;
2597}
2598
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002599/*
2600 * Evaluate a single or double quoted string possibly containing expressions.
2601 * "arg" points to the '$'. The result is put in "rettv".
2602 * Returns OK or FAIL.
2603 */
LemonBoy2eaef102022-05-06 13:14:50 +01002604 int
2605eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2606{
2607 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002608 int ret = OK;
2609 int quote;
2610 garray_T ga;
2611 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002612
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002613 ga_init2(&ga, 1, 80);
2614
2615 // *arg is on the '$' character, move it to the first string character.
2616 ++*arg;
2617 quote = **arg;
2618 ++*arg;
2619
2620 for (;;)
2621 {
2622 // Get the string up to the matching quote or to a single '{'.
2623 // "arg" is advanced to either the quote or the '{'.
2624 if (quote == '"')
2625 ret = eval_string(arg, &tv, evaluate, TRUE);
2626 else
2627 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2628 if (ret == FAIL)
2629 break;
2630 if (evaluate)
2631 {
2632 ga_concat(&ga, tv.vval.v_string);
2633 clear_tv(&tv);
2634 }
2635
2636 if (**arg != '{')
2637 {
2638 // found terminating quote
2639 ++*arg;
2640 break;
2641 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002642 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002643 if (p == NULL)
2644 {
2645 ret = FAIL;
2646 break;
2647 }
2648 *arg = p;
2649 }
LemonBoy2eaef102022-05-06 13:14:50 +01002650
2651 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002652 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2653 {
2654 ga_clear(&ga);
2655 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002656 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002657 }
LemonBoy2eaef102022-05-06 13:14:50 +01002658
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002659 rettv->vval.v_string = ga.ga_data;
2660 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002661}
2662
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002663/*
2664 * Return a string with the string representation of a variable.
2665 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2666 * "numbuf" is used for a number.
2667 * Puts quotes around strings, so that they can be parsed back by eval().
2668 * May return NULL.
2669 */
2670 char_u *
2671tv2string(
2672 typval_T *tv,
2673 char_u **tofree,
2674 char_u *numbuf,
2675 int copyID)
2676{
2677 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2678}
2679
2680/*
2681 * Get the value of an environment variable.
2682 * "arg" is pointing to the '$'. It is advanced to after the name.
2683 * If the environment variable was not set, silently assume it is empty.
2684 * Return FAIL if the name is invalid.
2685 */
2686 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002687eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002688{
2689 char_u *string = NULL;
2690 int len;
2691 int cc;
2692 char_u *name;
2693 int mustfree = FALSE;
2694
2695 ++*arg;
2696 name = *arg;
2697 len = get_env_len(arg);
2698 if (evaluate)
2699 {
2700 if (len == 0)
2701 return FAIL; // invalid empty name
2702
2703 cc = name[len];
2704 name[len] = NUL;
2705 // first try vim_getenv(), fast for normal environment vars
2706 string = vim_getenv(name, &mustfree);
2707 if (string != NULL && *string != NUL)
2708 {
2709 if (!mustfree)
2710 string = vim_strsave(string);
2711 }
2712 else
2713 {
2714 if (mustfree)
2715 vim_free(string);
2716
2717 // next try expanding things like $VIM and ${HOME}
2718 string = expand_env_save(name - 1);
2719 if (string != NULL && *string == '$')
2720 VIM_CLEAR(string);
2721 }
2722 name[len] = cc;
2723
2724 rettv->v_type = VAR_STRING;
2725 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002726 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002727 }
2728
2729 return OK;
2730}
2731
2732/*
2733 * Get the lnum from the first argument.
2734 * Also accepts ".", "$", etc., but that only works for the current buffer.
2735 * Returns -1 on error.
2736 */
2737 linenr_T
2738tv_get_lnum(typval_T *argvars)
2739{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002740 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002741 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002742
Bram Moolenaar56acb092020-08-16 14:48:19 +02002743 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2744 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002745 if (lnum <= 0 && did_emsg_before == did_emsg
2746 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002747 {
2748 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002749 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002750
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002751 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002752 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002753 if (fp != NULL)
2754 lnum = fp->lnum;
2755 }
2756 return lnum;
2757}
2758
2759/*
2760 * Get the lnum from the first argument.
2761 * Also accepts "$", then "buf" is used.
2762 * Returns 0 on error.
2763 */
2764 linenr_T
2765tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2766{
2767 if (argvars[0].v_type == VAR_STRING
2768 && argvars[0].vval.v_string != NULL
2769 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002770 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002771 && buf != NULL)
2772 return buf->b_ml.ml_line_count;
2773 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2774}
2775
2776/*
2777 * Get buffer by number or pattern.
2778 */
2779 buf_T *
2780tv_get_buf(typval_T *tv, int curtab_only)
2781{
2782 char_u *name = tv->vval.v_string;
2783 buf_T *buf;
2784
2785 if (tv->v_type == VAR_NUMBER)
2786 return buflist_findnr((int)tv->vval.v_number);
2787 if (tv->v_type != VAR_STRING)
2788 return NULL;
2789 if (name == NULL || *name == NUL)
2790 return curbuf;
2791 if (name[0] == '$' && name[1] == NUL)
2792 return lastbuf;
2793
2794 buf = buflist_find_by_name(name, curtab_only);
2795
2796 // If not found, try expanding the name, like done for bufexists().
2797 if (buf == NULL)
2798 buf = find_buffer(tv);
2799
2800 return buf;
2801}
2802
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002803/*
2804 * Like tv_get_buf() but give an error message is the type is wrong.
2805 */
2806 buf_T *
2807tv_get_buf_from_arg(typval_T *tv)
2808{
2809 buf_T *buf;
2810
2811 ++emsg_off;
2812 buf = tv_get_buf(tv, FALSE);
2813 --emsg_off;
2814 if (buf == NULL
2815 && tv->v_type != VAR_NUMBER
2816 && tv->v_type != VAR_STRING)
2817 // issue errmsg for type error
2818 (void)tv_get_number(tv);
2819 return buf;
2820}
2821
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002822#endif // FEAT_EVAL