blob: 76ce405a70d44e2508ef1c844f05e50d4e02fb5b [file] [log] [blame]
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * typval.c: functions that deal with a typval
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate memory for a variable type-value, and make it empty (0 or NULL
20 * value).
21 */
22 typval_T *
23alloc_tv(void)
24{
25 return ALLOC_CLEAR_ONE(typval_T);
26}
27
28/*
29 * Allocate memory for a variable type-value, and assign a string to it.
30 * The string "s" must have been allocated, it is consumed.
31 * Return NULL for out of memory, the variable otherwise.
32 */
33 typval_T *
34alloc_string_tv(char_u *s)
35{
36 typval_T *rettv;
37
38 rettv = alloc_tv();
39 if (rettv != NULL)
40 {
41 rettv->v_type = VAR_STRING;
42 rettv->vval.v_string = s;
43 }
44 else
45 vim_free(s);
46 return rettv;
47}
48
49/*
50 * Free the memory for a variable type-value.
51 */
52 void
53free_tv(typval_T *varp)
54{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000055 if (varp == NULL)
56 return;
Bram Moolenaar00b28d62022-12-08 15:32:33 +000057
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000058 switch (varp->v_type)
59 {
60 case VAR_FUNC:
61 func_unref(varp->vval.v_string);
62 // FALLTHROUGH
63 case VAR_STRING:
64 vim_free(varp->vval.v_string);
65 break;
66 case VAR_PARTIAL:
67 partial_unref(varp->vval.v_partial);
68 break;
69 case VAR_BLOB:
70 blob_unref(varp->vval.v_blob);
71 break;
72 case VAR_LIST:
73 list_unref(varp->vval.v_list);
74 break;
75 case VAR_DICT:
76 dict_unref(varp->vval.v_dict);
77 break;
78 case VAR_JOB:
79#ifdef FEAT_JOB_CHANNEL
80 job_unref(varp->vval.v_job);
81 break;
82#endif
83 case VAR_CHANNEL:
84#ifdef FEAT_JOB_CHANNEL
85 channel_unref(varp->vval.v_channel);
86 break;
87#endif
88 case VAR_CLASS:
89 class_unref(varp->vval.v_class);
90 break;
91 case VAR_OBJECT:
92 object_unref(varp->vval.v_object);
93 break;
94
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +020095 case VAR_TYPEALIAS:
96 typealias_unref(varp->vval.v_typealias);
97 break;
98
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000099 case VAR_NUMBER:
100 case VAR_FLOAT:
101 case VAR_ANY:
102 case VAR_UNKNOWN:
103 case VAR_VOID:
104 case VAR_BOOL:
105 case VAR_SPECIAL:
106 case VAR_INSTR:
107 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200108 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000109 vim_free(varp);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200110}
111
112/*
113 * Free the memory for a variable value and set the value to NULL or 0.
114 */
115 void
116clear_tv(typval_T *varp)
117{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000118 if (varp == NULL)
119 return;
120
121 switch (varp->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200122 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000123 case VAR_FUNC:
124 func_unref(varp->vval.v_string);
125 // FALLTHROUGH
126 case VAR_STRING:
127 VIM_CLEAR(varp->vval.v_string);
128 break;
129 case VAR_PARTIAL:
130 partial_unref(varp->vval.v_partial);
131 varp->vval.v_partial = NULL;
132 break;
133 case VAR_BLOB:
134 blob_unref(varp->vval.v_blob);
135 varp->vval.v_blob = NULL;
136 break;
137 case VAR_LIST:
138 list_unref(varp->vval.v_list);
139 varp->vval.v_list = NULL;
140 break;
141 case VAR_DICT:
142 dict_unref(varp->vval.v_dict);
143 varp->vval.v_dict = NULL;
144 break;
145 case VAR_NUMBER:
146 case VAR_BOOL:
147 case VAR_SPECIAL:
148 varp->vval.v_number = 0;
149 break;
150 case VAR_FLOAT:
151 varp->vval.v_float = 0.0;
152 break;
153 case VAR_JOB:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200154#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000155 job_unref(varp->vval.v_job);
156 varp->vval.v_job = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200157#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000158 break;
159 case VAR_CHANNEL:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200160#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000161 channel_unref(varp->vval.v_channel);
162 varp->vval.v_channel = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200163#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000164 break;
165 case VAR_INSTR:
166 VIM_CLEAR(varp->vval.v_instr);
167 break;
168 case VAR_CLASS:
169 class_unref(varp->vval.v_class);
170 varp->vval.v_class = NULL;
171 break;
172 case VAR_OBJECT:
173 object_unref(varp->vval.v_object);
174 varp->vval.v_object = NULL;
175 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200176 case VAR_TYPEALIAS:
177 typealias_unref(varp->vval.v_typealias);
178 varp->vval.v_typealias = NULL;
179 break;
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000180 case VAR_UNKNOWN:
181 case VAR_ANY:
182 case VAR_VOID:
183 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200184 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000185 varp->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200186}
187
188/*
189 * Set the value of a variable to NULL without freeing items.
190 */
191 void
192init_tv(typval_T *varp)
193{
194 if (varp != NULL)
195 CLEAR_POINTER(varp);
196}
197
Bram Moolenaar36967b32020-08-17 21:41:02 +0200198 static varnumber_T
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000199tv_get_bool_or_number_chk(
200 typval_T *varp,
201 int *denote,
202 int want_bool,
203 int vim9_string_error) // in Vim9 using a string is an error
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200204{
205 varnumber_T n = 0L;
206
207 switch (varp->v_type)
208 {
209 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200210 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200211 && varp->vval.v_number != 1)
212 {
213 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
214 break;
215 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200216 return varp->vval.v_number;
217 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000218 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200219 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200220 case VAR_FUNC:
221 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000222 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200223 break;
224 case VAR_STRING:
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000225 if (vim9_string_error && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200226 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100227 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200228 break;
229 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200230 if (varp->vval.v_string != NULL)
231 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000232 STR2NR_ALL, &n, NULL, 0, FALSE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200233 return n;
234 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000235 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 break;
237 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000238 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200239 break;
240 case VAR_BOOL:
241 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200242 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200243 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100244 if (varp->v_type == VAR_BOOL)
245 emsg(_(e_using_bool_as_number));
246 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000247 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200248 break;
249 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200250 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
251 case VAR_JOB:
252#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000253 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200254 break;
255#endif
256 case VAR_CHANNEL:
257#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000258 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200259 break;
260#endif
261 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000262 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200263 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000264 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +0100265 case VAR_TYPEALIAS:
266 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000267 break;
268 case VAR_OBJECT:
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100269 {
270 class_T *cl = varp->vval.v_object->obj_class;
271 if (cl != NULL && IS_ENUM(cl))
272 semsg(_(e_using_enum_str_as_number), cl->class_name);
273 else
274 emsg(_(e_using_object_as_number));
275 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000276 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200277 case VAR_VOID:
278 emsg(_(e_cannot_use_void_value));
279 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200280 case VAR_UNKNOWN:
281 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200282 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200283 internal_error_no_abort("tv_get_number(UNKNOWN)");
284 break;
285 }
286 if (denote == NULL) // useful for values that must be unsigned
287 n = -1;
288 else
289 *denote = TRUE;
290 return n;
291}
292
Bram Moolenaar36967b32020-08-17 21:41:02 +0200293/*
294 * Get the number value of a variable.
295 * If it is a String variable, uses vim_str2nr().
296 * For incompatible types, return 0.
297 * tv_get_number_chk() is similar to tv_get_number(), but informs the
298 * caller of incompatible types: it sets *denote to TRUE if "denote"
299 * is not NULL or returns -1 otherwise.
300 */
301 varnumber_T
302tv_get_number(typval_T *varp)
303{
304 int error = FALSE;
305
306 return tv_get_number_chk(varp, &error); // return 0L on error
307}
308
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000309/*
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200310 * Like tv_get_number() but in Vim9 script do convert a number in a string to a
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000311 * number without giving an error.
312 */
313 varnumber_T
314tv_to_number(typval_T *varp)
315{
316 int error = FALSE;
317
318 return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
319}
320
Bram Moolenaar36967b32020-08-17 21:41:02 +0200321 varnumber_T
322tv_get_number_chk(typval_T *varp, int *denote)
323{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000324 return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200325}
326
327/*
328 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200329 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200330 */
331 varnumber_T
332tv_get_bool(typval_T *varp)
333{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000334 return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200335}
336
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200337/*
338 * Get the boolean value of "varp". This is like tv_get_number_chk(),
339 * but in Vim9 script accepts Number and Bool.
340 */
341 varnumber_T
342tv_get_bool_chk(typval_T *varp, int *denote)
343{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000344 return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200345}
346
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000347 static float_T
348tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200349{
350 switch (varp->v_type)
351 {
352 case VAR_NUMBER:
353 return (float_T)(varp->vval.v_number);
354 case VAR_FLOAT:
355 return varp->vval.v_float;
356 case VAR_FUNC:
357 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000358 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200359 break;
360 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000361 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200362 break;
363 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000364 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200365 break;
366 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000367 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200368 break;
369 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000370 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200371 break;
372 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000373 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200374 break;
375 case VAR_JOB:
376# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000377 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200378 break;
379# endif
380 case VAR_CHANNEL:
381# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000382 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200383 break;
384# endif
385 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000386 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200387 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000388 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +0100389 case VAR_TYPEALIAS:
390 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000391 break;
392 case VAR_OBJECT:
393 emsg(_(e_using_object_as_float));
394 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200395 case VAR_VOID:
396 emsg(_(e_cannot_use_void_value));
397 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200398 case VAR_UNKNOWN:
399 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200400 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200401 internal_error_no_abort("tv_get_float(UNKNOWN)");
402 break;
403 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000404 if (error != NULL)
405 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200406 return 0;
407}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000408
409 float_T
410tv_get_float(typval_T *varp)
411{
412 return tv_get_float_chk(varp, NULL);
413}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200414
415/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100416 * Give an error and return FAIL unless "args[idx]" is unknown
417 */
418 int
419check_for_unknown_arg(typval_T *args, int idx)
420{
421 if (args[idx].v_type != VAR_UNKNOWN)
422 {
423 semsg(_(e_too_many_arguments), idx + 1);
424 return FAIL;
425 }
426 return OK;
427}
428
429/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200430 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100431 */
432 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100433check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100434{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100435 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100436 {
rbtnnddc80af2021-12-17 18:01:31 +0000437 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100438 return FAIL;
439 }
440 return OK;
441}
442
443/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100444 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100445 */
446 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100447check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100448{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100449 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100450 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100451 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100452 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200453 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100454 return FAIL;
455 }
456 return OK;
457}
458
459/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200460 * Check for an optional string argument at 'idx'
461 */
462 int
463check_for_opt_string_arg(typval_T *args, int idx)
464{
465 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100466 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200467}
468
469/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200470 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200471 */
472 int
473check_for_number_arg(typval_T *args, int idx)
474{
475 if (args[idx].v_type != VAR_NUMBER)
476 {
rbtnnddc80af2021-12-17 18:01:31 +0000477 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200478 return FAIL;
479 }
480 return OK;
481}
482
483/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200484 * Check for an optional number argument at 'idx'
485 */
486 int
487check_for_opt_number_arg(typval_T *args, int idx)
488{
489 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100490 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200491}
492
493/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200494 * Give an error and return FAIL unless "args[idx]" is a float or a number.
495 */
496 int
497check_for_float_or_nr_arg(typval_T *args, int idx)
498{
499 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
500 {
rbtnnddc80af2021-12-17 18:01:31 +0000501 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200502 return FAIL;
503 }
504 return OK;
505}
506
507/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200508 * Give an error and return FAIL unless "args[idx]" is a bool.
509 */
510 int
511check_for_bool_arg(typval_T *args, int idx)
512{
513 if (args[idx].v_type != VAR_BOOL
514 && !(args[idx].v_type == VAR_NUMBER
515 && (args[idx].vval.v_number == 0
516 || args[idx].vval.v_number == 1)))
517 {
rbtnnddc80af2021-12-17 18:01:31 +0000518 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200519 return FAIL;
520 }
521 return OK;
522}
523
524/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200525 * Check for an optional bool argument at 'idx'.
526 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200527 */
528 int
529check_for_opt_bool_arg(typval_T *args, int idx)
530{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200531 if (args[idx].v_type == VAR_UNKNOWN)
532 return OK;
533 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200534}
535
536/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200537 * Give an error and return FAIL unless "args[idx]" is a blob.
538 */
539 int
540check_for_blob_arg(typval_T *args, int idx)
541{
542 if (args[idx].v_type != VAR_BLOB)
543 {
rbtnnddc80af2021-12-17 18:01:31 +0000544 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200545 return FAIL;
546 }
547 return OK;
548}
549
550/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200551 * Give an error and return FAIL unless "args[idx]" is a list.
552 */
553 int
554check_for_list_arg(typval_T *args, int idx)
555{
556 if (args[idx].v_type != VAR_LIST)
557 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200558 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200559 return FAIL;
560 }
561 return OK;
562}
563
564/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100565 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
566 */
567 int
568check_for_nonnull_list_arg(typval_T *args, int idx)
569{
570 if (check_for_list_arg(args, idx) == FAIL)
571 return FAIL;
572
573 if (args[idx].vval.v_list == NULL)
574 {
575 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
576 return FAIL;
577 }
578 return OK;
579}
580
581/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200582 * Check for an optional list argument at 'idx'
583 */
584 int
585check_for_opt_list_arg(typval_T *args, int idx)
586{
587 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100588 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589}
590
591/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200592 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200593 */
594 int
595check_for_dict_arg(typval_T *args, int idx)
596{
597 if (args[idx].v_type != VAR_DICT)
598 {
rbtnnddc80af2021-12-17 18:01:31 +0000599 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200600 return FAIL;
601 }
602 return OK;
603}
604
605/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100606 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
607 */
608 int
609check_for_nonnull_dict_arg(typval_T *args, int idx)
610{
611 if (check_for_dict_arg(args, idx) == FAIL)
612 return FAIL;
613
614 if (args[idx].vval.v_dict == NULL)
615 {
616 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
617 return FAIL;
618 }
619 return OK;
620}
621
622/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200623 * Check for an optional dict argument at 'idx'
624 */
625 int
626check_for_opt_dict_arg(typval_T *args, int idx)
627{
628 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100629 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200630}
631
Yegappan Lakshmananfa378352024-02-01 22:05:27 +0100632/*
633 * Check for an optional non-NULL dict argument at 'idx'
634 */
635 int
636check_for_opt_nonnull_dict_arg(typval_T *args, int idx)
637{
638 return (args[idx].v_type == VAR_UNKNOWN
639 || check_for_nonnull_dict_arg(args, idx) != FAIL) ? OK : FAIL;
640}
641
Dominique Pelle748b3082022-01-08 12:41:16 +0000642#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200643/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200644 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
645 */
646 int
647check_for_chan_or_job_arg(typval_T *args, int idx)
648{
649 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
650 {
rbtnnddc80af2021-12-17 18:01:31 +0000651 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200652 return FAIL;
653 }
654 return OK;
655}
656
657/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200658 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
659 * job.
660 */
661 int
662check_for_opt_chan_or_job_arg(typval_T *args, int idx)
663{
664 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100665 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200666}
667
668/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200669 * Give an error and return FAIL unless "args[idx]" is a job.
670 */
671 int
672check_for_job_arg(typval_T *args, int idx)
673{
674 if (args[idx].v_type != VAR_JOB)
675 {
rbtnnddc80af2021-12-17 18:01:31 +0000676 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200677 return FAIL;
678 }
679 return OK;
680}
681
682/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200683 * Check for an optional job argument at 'idx'.
684 */
685 int
686check_for_opt_job_arg(typval_T *args, int idx)
687{
688 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100689 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200690}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000691#else
692/*
693 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
694 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
695 */
696 int
697check_for_opt_chan_or_job_arg(typval_T *args, int idx)
698{
699 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
700}
Dominique Pelle748b3082022-01-08 12:41:16 +0000701#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200702
703/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200704 * Give an error and return FAIL unless "args[idx]" is a string or
705 * a number.
706 */
707 int
708check_for_string_or_number_arg(typval_T *args, int idx)
709{
710 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
711 {
rbtnnddc80af2021-12-17 18:01:31 +0000712 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200713 return FAIL;
714 }
715 return OK;
716}
717
718/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200719 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200720 */
721 int
722check_for_opt_string_or_number_arg(typval_T *args, int idx)
723{
724 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100725 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200726}
727
728/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200729 * Give an error and return FAIL unless "args[idx]" is a buffer number.
730 * Buffer number can be a number or a string.
731 */
732 int
733check_for_buffer_arg(typval_T *args, int idx)
734{
735 return check_for_string_or_number_arg(args, idx);
736}
737
738/*
739 * Check for an optional buffer argument at 'idx'
740 */
741 int
742check_for_opt_buffer_arg(typval_T *args, int idx)
743{
744 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100745 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200746}
747
748/*
749 * Give an error and return FAIL unless "args[idx]" is a line number.
750 * Line number can be a number or a string.
751 */
752 int
753check_for_lnum_arg(typval_T *args, int idx)
754{
755 return check_for_string_or_number_arg(args, idx);
756}
757
758/*
759 * Check for an optional line number argument at 'idx'
760 */
761 int
762check_for_opt_lnum_arg(typval_T *args, int idx)
763{
764 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100765 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200766}
767
Dominique Pelle748b3082022-01-08 12:41:16 +0000768#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200769/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200770 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200771 */
772 int
773check_for_string_or_blob_arg(typval_T *args, int idx)
774{
775 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
776 {
rbtnnddc80af2021-12-17 18:01:31 +0000777 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200778 return FAIL;
779 }
780 return OK;
781}
Dominique Pelle748b3082022-01-08 12:41:16 +0000782#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200783
784/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200785 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200786 */
787 int
788check_for_string_or_list_arg(typval_T *args, int idx)
789{
790 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
791 {
rbtnnddc80af2021-12-17 18:01:31 +0000792 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200793 return FAIL;
794 }
795 return OK;
796}
797
798/*
rbtnn0ccb5842021-12-18 18:33:46 +0000799 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
800 * blob.
801 */
802 int
803check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
804{
805 if (args[idx].v_type != VAR_STRING
806 && args[idx].v_type != VAR_LIST
807 && args[idx].v_type != VAR_BLOB)
808 {
809 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
810 return FAIL;
811 }
812 return OK;
813}
814
815/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200816 * Check for an optional string or list argument at 'idx'
817 */
818 int
819check_for_opt_string_or_list_arg(typval_T *args, int idx)
820{
821 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100822 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200823}
824
825/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200826 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
827 */
828 int
829check_for_string_or_dict_arg(typval_T *args, int idx)
830{
831 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
832 {
rbtnnddc80af2021-12-17 18:01:31 +0000833 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200834 return FAIL;
835 }
836 return OK;
837}
838
839/*
840 * Give an error and return FAIL unless "args[idx]" is a string or a number
841 * or a list.
842 */
843 int
844check_for_string_or_number_or_list_arg(typval_T *args, int idx)
845{
846 if (args[idx].v_type != VAR_STRING
847 && args[idx].v_type != VAR_NUMBER
848 && args[idx].v_type != VAR_LIST)
849 {
rbtnn0ccb5842021-12-18 18:33:46 +0000850 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200851 return FAIL;
852 }
853 return OK;
854}
855
856/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200857 * Give an error and return FAIL unless "args[idx]" is an optional string
858 * or number or a list
859 */
860 int
861check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
862{
863 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100864 || check_for_string_or_number_or_list_arg(args, idx)
865 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200866}
867
868/*
Bakudankun375141e2022-09-09 18:46:47 +0100869 * Give an error and return FAIL unless "args[idx]" is a string or a number
870 * or a list or a blob.
871 */
872 int
873check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
874{
875 if (args[idx].v_type != VAR_STRING
876 && args[idx].v_type != VAR_NUMBER
877 && args[idx].v_type != VAR_LIST
878 && args[idx].v_type != VAR_BLOB)
879 {
880 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
881 return FAIL;
882 }
883 return OK;
884}
885
886/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200887 * Give an error and return FAIL unless "args[idx]" is a string or a list
888 * or a dict.
889 */
890 int
891check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
892{
893 if (args[idx].v_type != VAR_STRING
894 && args[idx].v_type != VAR_LIST
895 && args[idx].v_type != VAR_DICT)
896 {
rbtnnddc80af2021-12-17 18:01:31 +0000897 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200898 return FAIL;
899 }
900 return OK;
901}
902
903/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000904 * Give an error and return FAIL unless "args[idx]" is a string
905 * or a function reference.
906 */
907 int
908check_for_string_or_func_arg(typval_T *args, int idx)
909{
910 if (args[idx].v_type != VAR_PARTIAL
911 && args[idx].v_type != VAR_FUNC
912 && args[idx].v_type != VAR_STRING)
913 {
914 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
915 return FAIL;
916 }
917 return OK;
918}
919
920/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200921 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
922 */
923 int
924check_for_list_or_blob_arg(typval_T *args, int idx)
925{
926 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
927 {
rbtnn0ccb5842021-12-18 18:33:46 +0000928 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200929 return FAIL;
930 }
931 return OK;
932}
933
934/*
935 * Give an error and return FAIL unless "args[idx]" is a list or dict
936 */
937 int
938check_for_list_or_dict_arg(typval_T *args, int idx)
939{
940 if (args[idx].v_type != VAR_LIST
941 && args[idx].v_type != VAR_DICT)
942 {
rbtnnddc80af2021-12-17 18:01:31 +0000943 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200944 return FAIL;
945 }
946 return OK;
947}
948
949/*
950 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
951 * blob.
952 */
953 int
954check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
955{
956 if (args[idx].v_type != VAR_LIST
957 && args[idx].v_type != VAR_DICT
958 && args[idx].v_type != VAR_BLOB)
959 {
rbtnnddc80af2021-12-17 18:01:31 +0000960 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200961 return FAIL;
962 }
963 return OK;
964}
965
966/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000967 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
968 * blob or a string.
969 */
970 int
971check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
972{
973 if (args[idx].v_type != VAR_LIST
974 && args[idx].v_type != VAR_DICT
975 && args[idx].v_type != VAR_BLOB
976 && args[idx].v_type != VAR_STRING)
977 {
rbtnnddc80af2021-12-17 18:01:31 +0000978 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000979 return FAIL;
980 }
981 return OK;
982}
983
984/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200985 * Give an error and return FAIL unless "args[idx]" is an optional buffer
986 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200987 */
988 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200989check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200990{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200991 if (args[idx].v_type != VAR_UNKNOWN
992 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200993 && args[idx].v_type != VAR_NUMBER
994 && args[idx].v_type != VAR_DICT)
995 {
rbtnnddc80af2021-12-17 18:01:31 +0000996 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200997 return FAIL;
998 }
999 return OK;
1000}
1001
1002/*
LemonBoyafe04662023-08-23 21:08:11 +02001003 * Give an error and return FAIL unless "args[idx]" is an object.
1004 */
1005 int
1006check_for_object_arg(typval_T *args, int idx)
1007{
1008 if (args[idx].v_type != VAR_OBJECT)
1009 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001010 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001011 return FAIL;
1012 }
1013 return OK;
1014}
1015
1016/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001017 * Returns TRUE if "tv" is a type alias for a class
1018 */
1019 int
1020tv_class_alias(typval_T *tv)
1021{
1022 return tv->v_type == VAR_TYPEALIAS &&
1023 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1024}
1025
1026/*
Ernie Rael2025af12023-12-12 16:58:00 +01001027 * Give an error and return FAIL unless "args[idx]" is a class
1028 * or class typealias.
LemonBoyafe04662023-08-23 21:08:11 +02001029 */
1030 int
Ernie Rael2025af12023-12-12 16:58:00 +01001031check_for_class_or_typealias_args(typval_T *args, int idx)
LemonBoyafe04662023-08-23 21:08:11 +02001032{
Ernie Rael2025af12023-12-12 16:58:00 +01001033 for (int i = idx; args[i].v_type != VAR_UNKNOWN; ++i)
LemonBoyafe04662023-08-23 21:08:11 +02001034 {
Ernie Rael2025af12023-12-12 16:58:00 +01001035 if (args[i].v_type != VAR_CLASS && !tv_class_alias(&args[idx]))
1036 {
1037 semsg(_(e_class_or_typealias_required_for_argument_nr), i + 1);
1038 return FAIL;
1039 }
LemonBoyafe04662023-08-23 21:08:11 +02001040 }
1041 return OK;
1042}
1043
1044/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001045 * Get the string value of a variable.
1046 * If it is a Number variable, the number is converted into a string.
1047 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1048 * tv_get_string_buf() uses a given buffer.
1049 * If the String variable has never been set, return an empty string.
1050 * Never returns NULL;
1051 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1052 * NULL on error.
1053 */
1054 char_u *
1055tv_get_string(typval_T *varp)
1056{
1057 static char_u mybuf[NUMBUFLEN];
1058
1059 return tv_get_string_buf(varp, mybuf);
1060}
1061
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001062/*
1063 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1064 */
1065 char_u *
1066tv_get_string_strict(typval_T *varp)
1067{
1068 static char_u mybuf[NUMBUFLEN];
1069 char_u *res = tv_get_string_buf_chk_strict(
1070 varp, mybuf, in_vim9script());
1071
1072 return res != NULL ? res : (char_u *)"";
1073}
1074
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001075 char_u *
1076tv_get_string_buf(typval_T *varp, char_u *buf)
1077{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001078 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001079
1080 return res != NULL ? res : (char_u *)"";
1081}
1082
1083/*
1084 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1085 */
1086 char_u *
1087tv_get_string_chk(typval_T *varp)
1088{
1089 static char_u mybuf[NUMBUFLEN];
1090
1091 return tv_get_string_buf_chk(varp, mybuf);
1092}
1093
1094 char_u *
1095tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1096{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001097 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1098}
1099
1100 char_u *
1101tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1102{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001103 switch (varp->v_type)
1104 {
1105 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001106 if (strict)
1107 {
1108 emsg(_(e_using_number_as_string));
1109 break;
1110 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001111 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1112 (varnumber_T)varp->vval.v_number);
1113 return buf;
1114 case VAR_FUNC:
1115 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001116 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001117 break;
1118 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001119 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001120 break;
1121 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001122 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001123 break;
1124 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001125 if (strict)
1126 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001127 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001128 break;
1129 }
1130 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1131 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001132 case VAR_STRING:
1133 if (varp->vval.v_string != NULL)
1134 return varp->vval.v_string;
1135 return (char_u *)"";
1136 case VAR_BOOL:
1137 case VAR_SPECIAL:
1138 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1139 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001140 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001141 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001143 case VAR_CLASS:
Ernie Raele75fde62023-12-21 17:18:54 +01001144 case VAR_TYPEALIAS:
1145 check_typval_is_value(varp);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001146 break;
1147 case VAR_OBJECT:
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001148 {
1149 class_T *cl = varp->vval.v_object->obj_class;
1150 if (cl != NULL && IS_ENUM(cl))
1151 semsg(_(e_using_enum_str_as_string), cl->class_name);
1152 else
1153 emsg(_(e_using_object_as_string));
1154 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001155 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001156 case VAR_JOB:
1157#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001158 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001159 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001160 semsg(_(e_using_invalid_value_as_string_str), "job");
1161 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001162 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001163 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001164#endif
1165 break;
1166 case VAR_CHANNEL:
1167#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001168 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001169 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001170 semsg(_(e_using_invalid_value_as_string_str), "channel");
1171 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001172 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001173 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001174#endif
1175 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001176 case VAR_VOID:
1177 emsg(_(e_cannot_use_void_value));
1178 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001179 case VAR_UNKNOWN:
1180 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001181 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001182 semsg(_(e_using_invalid_value_as_string_str),
1183 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001184 break;
1185 }
1186 return NULL;
1187}
1188
1189/*
1190 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1191 * string() on Dict, List, etc.
1192 */
1193 char_u *
1194tv_stringify(typval_T *varp, char_u *buf)
1195{
1196 if (varp->v_type == VAR_LIST
1197 || varp->v_type == VAR_DICT
1198 || varp->v_type == VAR_BLOB
1199 || varp->v_type == VAR_FUNC
1200 || varp->v_type == VAR_PARTIAL
1201 || varp->v_type == VAR_FLOAT)
1202 {
1203 typval_T tmp;
1204
1205 f_string(varp, &tmp);
1206 tv_get_string_buf(&tmp, buf);
1207 clear_tv(varp);
1208 *varp = tmp;
1209 return tmp.vval.v_string;
1210 }
1211 return tv_get_string_buf(varp, buf);
1212}
1213
1214/*
1215 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1216 * Also give an error message, using "name" or _("name") when use_gettext is
1217 * TRUE.
1218 */
1219 int
1220tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1221{
1222 int lock = 0;
1223
1224 switch (tv->v_type)
1225 {
1226 case VAR_BLOB:
1227 if (tv->vval.v_blob != NULL)
1228 lock = tv->vval.v_blob->bv_lock;
1229 break;
1230 case VAR_LIST:
1231 if (tv->vval.v_list != NULL)
1232 lock = tv->vval.v_list->lv_lock;
1233 break;
1234 case VAR_DICT:
1235 if (tv->vval.v_dict != NULL)
1236 lock = tv->vval.v_dict->dv_lock;
1237 break;
1238 default:
1239 break;
1240 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001241 return value_check_lock(tv->v_lock, name, use_gettext)
1242 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001243}
1244
1245/*
1246 * Copy the values from typval_T "from" to typval_T "to".
1247 * When needed allocates string or increases reference count.
1248 * Does not make a copy of a list, blob or dict but copies the reference!
1249 * It is OK for "from" and "to" to point to the same item. This is used to
1250 * make a copy later.
1251 */
1252 void
1253copy_tv(typval_T *from, typval_T *to)
1254{
1255 to->v_type = from->v_type;
1256 to->v_lock = 0;
1257 switch (from->v_type)
1258 {
1259 case VAR_NUMBER:
1260 case VAR_BOOL:
1261 case VAR_SPECIAL:
1262 to->vval.v_number = from->vval.v_number;
1263 break;
1264 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001265 to->vval.v_float = from->vval.v_float;
1266 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001267 case VAR_JOB:
1268#ifdef FEAT_JOB_CHANNEL
1269 to->vval.v_job = from->vval.v_job;
1270 if (to->vval.v_job != NULL)
1271 ++to->vval.v_job->jv_refcount;
1272 break;
1273#endif
1274 case VAR_CHANNEL:
1275#ifdef FEAT_JOB_CHANNEL
1276 to->vval.v_channel = from->vval.v_channel;
1277 if (to->vval.v_channel != NULL)
1278 ++to->vval.v_channel->ch_refcount;
1279 break;
1280#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001281 case VAR_INSTR:
1282 to->vval.v_instr = from->vval.v_instr;
1283 break;
1284
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001285 case VAR_CLASS:
1286 copy_class(from, to);
1287 break;
1288
1289 case VAR_OBJECT:
1290 copy_object(from, to);
1291 break;
1292
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001293 case VAR_STRING:
1294 case VAR_FUNC:
1295 if (from->vval.v_string == NULL)
1296 to->vval.v_string = NULL;
1297 else
1298 {
1299 to->vval.v_string = vim_strsave(from->vval.v_string);
1300 if (from->v_type == VAR_FUNC)
1301 func_ref(to->vval.v_string);
1302 }
1303 break;
1304 case VAR_PARTIAL:
1305 if (from->vval.v_partial == NULL)
1306 to->vval.v_partial = NULL;
1307 else
1308 {
1309 to->vval.v_partial = from->vval.v_partial;
1310 ++to->vval.v_partial->pt_refcount;
1311 }
1312 break;
1313 case VAR_BLOB:
1314 if (from->vval.v_blob == NULL)
1315 to->vval.v_blob = NULL;
1316 else
1317 {
1318 to->vval.v_blob = from->vval.v_blob;
1319 ++to->vval.v_blob->bv_refcount;
1320 }
1321 break;
1322 case VAR_LIST:
1323 if (from->vval.v_list == NULL)
1324 to->vval.v_list = NULL;
1325 else
1326 {
1327 to->vval.v_list = from->vval.v_list;
1328 ++to->vval.v_list->lv_refcount;
1329 }
1330 break;
1331 case VAR_DICT:
1332 if (from->vval.v_dict == NULL)
1333 to->vval.v_dict = NULL;
1334 else
1335 {
1336 to->vval.v_dict = from->vval.v_dict;
1337 ++to->vval.v_dict->dv_refcount;
1338 }
1339 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001340 case VAR_TYPEALIAS:
1341 if (from->vval.v_typealias == NULL)
1342 to->vval.v_typealias = NULL;
1343 else
1344 {
1345 to->vval.v_typealias = from->vval.v_typealias;
1346 ++to->vval.v_typealias->ta_refcount;
1347 }
1348 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001349 case VAR_VOID:
1350 emsg(_(e_cannot_use_void_value));
1351 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001352 case VAR_UNKNOWN:
1353 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001354 internal_error_no_abort("copy_tv(UNKNOWN)");
1355 break;
1356 }
1357}
1358
1359/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001360 * Compare "tv1" and "tv2".
1361 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001362 */
1363 int
1364typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001365 typval_T *tv1, // first operand
1366 typval_T *tv2, // second operand
1367 exprtype_T type, // operator
1368 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001369{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001370 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001371 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001372 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1373
Ernie Raele75fde62023-12-21 17:18:54 +01001374 if (check_typval_is_value(tv1) == FAIL
1375 || check_typval_is_value(tv2) == FAIL)
1376 {
1377 clear_tv(tv1);
1378 return FAIL;
1379 }
1380 else if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001381 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001382 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001383 // it means TRUE.
1384 n1 = (type == EXPR_ISNOT);
1385 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001386 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1387 || (tv2->v_type == VAR_SPECIAL
1388 && tv2->vval.v_number == VVAL_NULL))
1389 && tv1->v_type != tv2->v_type
1390 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1391 {
1392 n1 = typval_compare_null(tv1, tv2);
1393 if (n1 == MAYBE)
1394 {
1395 clear_tv(tv1);
1396 return FAIL;
1397 }
1398 if (type == EXPR_NEQUAL)
1399 n1 = !n1;
1400 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001401 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001402 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001403 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001404 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001405 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001406 return FAIL;
1407 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001408 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001409 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001410 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001411 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001412 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001413 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001414 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001415 return FAIL;
1416 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001417 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001418 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001419 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1420 {
1421 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1422 {
1423 clear_tv(tv1);
1424 return FAIL;
1425 }
1426 n1 = res;
1427 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001428 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001429 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001430 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001431 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001432 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433 return FAIL;
1434 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001435 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001436 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001437 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1438 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001439 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001440 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001441 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001442 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001443 return FAIL;
1444 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001445 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001446 }
1447
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001448 // If one of the two variables is a float, compare as a float.
1449 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001450 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001451 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1452 {
1453 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001454 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001455
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001456 f1 = tv_get_float_chk(tv1, &error);
1457 if (!error)
1458 f2 = tv_get_float_chk(tv2, &error);
1459 if (error)
1460 {
1461 clear_tv(tv1);
1462 return FAIL;
1463 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001464 n1 = FALSE;
1465 switch (type)
1466 {
1467 case EXPR_IS:
1468 case EXPR_EQUAL: n1 = (f1 == f2); break;
1469 case EXPR_ISNOT:
1470 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1471 case EXPR_GREATER: n1 = (f1 > f2); break;
1472 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1473 case EXPR_SMALLER: n1 = (f1 < f2); break;
1474 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1475 case EXPR_UNKNOWN:
1476 case EXPR_MATCH:
1477 default: break; // avoid gcc warning
1478 }
1479 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001480
1481 // If one of the two variables is a number, compare as a number.
1482 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001483 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001484 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1485 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001486 int error = FALSE;
1487
1488 n1 = tv_get_number_chk(tv1, &error);
1489 if (!error)
1490 n2 = tv_get_number_chk(tv2, &error);
1491 if (error)
1492 {
1493 clear_tv(tv1);
1494 return FAIL;
1495 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001496 switch (type)
1497 {
1498 case EXPR_IS:
1499 case EXPR_EQUAL: n1 = (n1 == n2); break;
1500 case EXPR_ISNOT:
1501 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1502 case EXPR_GREATER: n1 = (n1 > n2); break;
1503 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1504 case EXPR_SMALLER: n1 = (n1 < n2); break;
1505 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1506 case EXPR_UNKNOWN:
1507 case EXPR_MATCH:
1508 default: break; // avoid gcc warning
1509 }
1510 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001511 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1512 || tv2->v_type == VAR_BOOL
1513 || (tv1->v_type == VAR_SPECIAL
1514 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001515 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001516 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001517 {
1518 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001519 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1520 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001521 return FAIL;
1522 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001523 n1 = tv1->vval.v_number;
1524 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001525 switch (type)
1526 {
1527 case EXPR_IS:
1528 case EXPR_EQUAL: n1 = (n1 == n2); break;
1529 case EXPR_ISNOT:
1530 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1531 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001532 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001533 vartype_name(tv1->v_type));
1534 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001535 return FAIL;
1536 }
1537 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001538#ifdef FEAT_JOB_CHANNEL
1539 else if (tv1->v_type == tv2->v_type
1540 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1541 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1542 {
1543 if (tv1->v_type == VAR_CHANNEL)
1544 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1545 else
1546 n1 = tv1->vval.v_job == tv2->vval.v_job;
1547 if (type == EXPR_NEQUAL)
1548 n1 = !n1;
1549 }
1550#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001551 else
1552 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001553 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001554 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001555 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001556 return FAIL;
1557 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001558 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001559 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001560 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001561 if (in_vim9script())
1562 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001563 tv1->v_type = VAR_BOOL;
1564 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001565 }
1566 else
1567 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001568 tv1->v_type = VAR_NUMBER;
1569 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001570 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001571
1572 return OK;
1573}
1574
Bram Moolenaar34453202021-01-31 13:08:38 +01001575/*
dundargocc57b5bc2022-11-02 13:30:51 +00001576 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001577 * Put the result, false or true, in "res".
1578 * Return FAIL and give an error message when the comparison can't be done.
1579 */
1580 int
1581typval_compare_list(
1582 typval_T *tv1,
1583 typval_T *tv2,
1584 exprtype_T type,
1585 int ic,
1586 int *res)
1587{
1588 int val = 0;
1589
1590 if (type == EXPR_IS || type == EXPR_ISNOT)
1591 {
1592 val = (tv1->v_type == tv2->v_type
1593 && tv1->vval.v_list == tv2->vval.v_list);
1594 if (type == EXPR_ISNOT)
1595 val = !val;
1596 }
1597 else if (tv1->v_type != tv2->v_type
1598 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1599 {
1600 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001601 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001602 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001603 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001604 return FAIL;
1605 }
1606 else
1607 {
1608 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1609 ic, FALSE);
1610 if (type == EXPR_NEQUAL)
1611 val = !val;
1612 }
1613 *res = val;
1614 return OK;
1615}
1616
1617/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001618 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001619 */
1620 int
1621typval_compare_null(typval_T *tv1, typval_T *tv2)
1622{
1623 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1624 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1625 {
1626 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1627
1628 switch (tv->v_type)
1629 {
1630 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001631#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001632 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001633#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001634 // TODO: null_class handling
1635 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001636 case VAR_DICT: return tv->vval.v_dict == NULL;
1637 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001638#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001639 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001640#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001641 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001642 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001643 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1644 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001645
1646 case VAR_NUMBER: if (!in_vim9script())
1647 return tv->vval.v_number == 0;
1648 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001649 case VAR_FLOAT: if (!in_vim9script())
1650 return tv->vval.v_float == 0.0;
1651 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001652 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001653 default: break;
1654 }
1655 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001656 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001657 // we won't give an error
1658 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001659}
1660
1661/*
dundargocc57b5bc2022-11-02 13:30:51 +00001662 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001663 * Put the result, false or true, in "res".
1664 * Return FAIL and give an error message when the comparison can't be done.
1665 */
1666 int
1667typval_compare_blob(
1668 typval_T *tv1,
1669 typval_T *tv2,
1670 exprtype_T type,
1671 int *res)
1672{
1673 int val = 0;
1674
1675 if (type == EXPR_IS || type == EXPR_ISNOT)
1676 {
1677 val = (tv1->v_type == tv2->v_type
1678 && tv1->vval.v_blob == tv2->vval.v_blob);
1679 if (type == EXPR_ISNOT)
1680 val = !val;
1681 }
1682 else if (tv1->v_type != tv2->v_type
1683 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1684 {
1685 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001686 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001687 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001688 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001689 return FAIL;
1690 }
1691 else
1692 {
1693 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1694 if (type == EXPR_NEQUAL)
1695 val = !val;
1696 }
1697 *res = val;
1698 return OK;
1699}
1700
1701/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001702 * Compare "tv1" to "tv2" as classes according to "type".
1703 * Put the result, false or true, in "res".
1704 * Return FAIL and give an error message when the comparison can't be done.
1705 */
1706 int
1707typval_compare_class(
1708 typval_T *tv1,
1709 typval_T *tv2,
1710 exprtype_T type UNUSED,
1711 int ic UNUSED,
1712 int *res)
1713{
1714 // TODO: use "type"
1715 *res = tv1->vval.v_class == tv2->vval.v_class;
1716 return OK;
1717}
1718
1719/*
1720 * Compare "tv1" to "tv2" as objects according to "type".
1721 * Put the result, false or true, in "res".
1722 * Return FAIL and give an error message when the comparison can't be done.
1723 */
1724 int
1725typval_compare_object(
1726 typval_T *tv1,
1727 typval_T *tv2,
1728 exprtype_T type,
1729 int ic,
1730 int *res)
1731{
1732 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1733
1734 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1735 {
1736 *res = res_match;
1737 return OK;
1738 }
1739 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1740 {
1741 *res = !res_match;
1742 return OK;
1743 }
1744
1745 class_T *cl1 = tv1->vval.v_object->obj_class;
1746 class_T *cl2 = tv2->vval.v_object->obj_class;
1747 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1748 {
1749 *res = !res_match;
1750 return OK;
1751 }
1752
1753 object_T *obj1 = tv1->vval.v_object;
1754 object_T *obj2 = tv2->vval.v_object;
1755 if (type == EXPR_IS || type == EXPR_ISNOT)
1756 {
1757 *res = obj1 == obj2 ? res_match : !res_match;
1758 return OK;
1759 }
1760
1761 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1762 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1763 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1764 {
1765 *res = !res_match;
1766 return OK;
1767 }
1768 *res = res_match;
1769 return OK;
1770}
1771
1772/*
dundargocc57b5bc2022-11-02 13:30:51 +00001773 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001774 * Put the result, false or true, in "res".
1775 * Return FAIL and give an error message when the comparison can't be done.
1776 */
1777 int
1778typval_compare_dict(
1779 typval_T *tv1,
1780 typval_T *tv2,
1781 exprtype_T type,
1782 int ic,
1783 int *res)
1784{
1785 int val;
1786
1787 if (type == EXPR_IS || type == EXPR_ISNOT)
1788 {
1789 val = (tv1->v_type == tv2->v_type
1790 && tv1->vval.v_dict == tv2->vval.v_dict);
1791 if (type == EXPR_ISNOT)
1792 val = !val;
1793 }
1794 else if (tv1->v_type != tv2->v_type
1795 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1796 {
1797 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001798 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001799 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001800 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001801 return FAIL;
1802 }
1803 else
1804 {
1805 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1806 if (type == EXPR_NEQUAL)
1807 val = !val;
1808 }
1809 *res = val;
1810 return OK;
1811}
1812
1813/*
dundargocc57b5bc2022-11-02 13:30:51 +00001814 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001815 * Put the result, false or true, in "res".
1816 * Return FAIL and give an error message when the comparison can't be done.
1817 */
1818 int
1819typval_compare_func(
1820 typval_T *tv1,
1821 typval_T *tv2,
1822 exprtype_T type,
1823 int ic,
1824 int *res)
1825{
1826 int val = 0;
1827
1828 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1829 && type != EXPR_IS && type != EXPR_ISNOT)
1830 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001831 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001832 return FAIL;
1833 }
1834 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1835 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1836 // When both partials are NULL, then they are equal.
1837 // Otherwise they are not equal.
1838 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1839 else if (type == EXPR_IS || type == EXPR_ISNOT)
1840 {
1841 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1842 // strings are considered the same if their value is
1843 // the same
1844 val = tv_equal(tv1, tv2, ic, FALSE);
1845 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1846 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1847 else
1848 val = FALSE;
1849 }
1850 else
1851 val = tv_equal(tv1, tv2, ic, FALSE);
1852 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1853 val = !val;
1854 *res = val;
1855 return OK;
1856}
1857
1858/*
1859 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1860 * Put the result, false or true, in "res".
1861 * Return FAIL and give an error message when the comparison can't be done.
1862 */
1863 int
1864typval_compare_string(
1865 typval_T *tv1,
1866 typval_T *tv2,
1867 exprtype_T type,
1868 int ic,
1869 int *res)
1870{
1871 int i = 0;
1872 int val = FALSE;
1873 char_u *s1, *s2;
1874 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1875
1876 if (in_vim9script()
1877 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1878 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1879 {
1880 semsg(_(e_cannot_compare_str_with_str),
1881 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1882 return FAIL;
1883 }
1884 s1 = tv_get_string_buf(tv1, buf1);
1885 s2 = tv_get_string_buf(tv2, buf2);
1886 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1887 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1888 switch (type)
1889 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001890 case EXPR_IS: if (in_vim9script())
1891 {
1892 // Really check it is the same string, not just
1893 // the same value.
1894 val = tv1->vval.v_string == tv2->vval.v_string;
1895 break;
1896 }
1897 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001898 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001899 case EXPR_ISNOT: if (in_vim9script())
1900 {
1901 // Really check it is not the same string, not
1902 // just a different value.
1903 val = tv1->vval.v_string != tv2->vval.v_string;
1904 break;
1905 }
1906 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001907 case EXPR_NEQUAL: val = (i != 0); break;
1908 case EXPR_GREATER: val = (i > 0); break;
1909 case EXPR_GEQUAL: val = (i >= 0); break;
1910 case EXPR_SMALLER: val = (i < 0); break;
1911 case EXPR_SEQUAL: val = (i <= 0); break;
1912
1913 case EXPR_MATCH:
1914 case EXPR_NOMATCH:
1915 val = pattern_match(s2, s1, ic);
1916 if (type == EXPR_NOMATCH)
1917 val = !val;
1918 break;
1919
1920 default: break; // avoid gcc warning
1921 }
1922 *res = val;
1923 return OK;
1924}
1925/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001926 * Convert any type to a string, never give an error.
1927 * When "quotes" is TRUE add quotes to a string.
1928 * Returns an allocated string.
1929 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001930 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001931typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001932{
1933 char_u *tofree;
1934 char_u numbuf[NUMBUFLEN];
1935 char_u *ret = NULL;
1936
1937 if (arg == NULL)
1938 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001939 if (!quotes && arg->v_type == VAR_STRING)
1940 {
1941 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1942 : arg->vval.v_string);
1943 }
1944 else
1945 {
1946 ret = tv2string(arg, &tofree, numbuf, 0);
1947 // Make a copy if we have a value but it's not in allocated memory.
1948 if (ret != NULL && tofree == NULL)
1949 ret = vim_strsave(ret);
1950 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001951 return ret;
1952}
1953
1954/*
1955 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1956 * or it refers to a List or Dictionary that is locked.
1957 */
1958 int
1959tv_islocked(typval_T *tv)
1960{
1961 return (tv->v_lock & VAR_LOCKED)
1962 || (tv->v_type == VAR_LIST
1963 && tv->vval.v_list != NULL
1964 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1965 || (tv->v_type == VAR_DICT
1966 && tv->vval.v_dict != NULL
1967 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1968}
1969
1970 static int
1971func_equal(
1972 typval_T *tv1,
1973 typval_T *tv2,
1974 int ic) // ignore case
1975{
1976 char_u *s1, *s2;
1977 dict_T *d1, *d2;
1978 int a1, a2;
1979 int i;
1980
1981 // empty and NULL function name considered the same
1982 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1983 : partial_name(tv1->vval.v_partial);
1984 if (s1 != NULL && *s1 == NUL)
1985 s1 = NULL;
1986 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1987 : partial_name(tv2->vval.v_partial);
1988 if (s2 != NULL && *s2 == NUL)
1989 s2 = NULL;
1990 if (s1 == NULL || s2 == NULL)
1991 {
1992 if (s1 != s2)
1993 return FALSE;
1994 }
1995 else if (STRCMP(s1, s2) != 0)
1996 return FALSE;
1997
1998 // empty dict and NULL dict is different
1999 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
2000 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
2001 if (d1 == NULL || d2 == NULL)
2002 {
2003 if (d1 != d2)
2004 return FALSE;
2005 }
2006 else if (!dict_equal(d1, d2, ic, TRUE))
2007 return FALSE;
2008
2009 // empty list and no list considered the same
2010 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
2011 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
2012 if (a1 != a2)
2013 return FALSE;
2014 for (i = 0; i < a1; ++i)
2015 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
2016 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
2017 return FALSE;
2018
2019 return TRUE;
2020}
2021
2022/*
2023 * Return TRUE if "tv1" and "tv2" have the same value.
2024 * Compares the items just like "==" would compare them, but strings and
2025 * numbers are different. Floats and numbers are also different.
2026 */
2027 int
2028tv_equal(
2029 typval_T *tv1,
2030 typval_T *tv2,
2031 int ic, // ignore case
2032 int recursive) // TRUE when used recursively
2033{
2034 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2035 char_u *s1, *s2;
2036 static int recursive_cnt = 0; // catch recursive loops
2037 int r;
2038 static int tv_equal_recurse_limit;
2039
2040 // Catch lists and dicts that have an endless loop by limiting
2041 // recursiveness to a limit. We guess they are equal then.
2042 // A fixed limit has the problem of still taking an awful long time.
2043 // Reduce the limit every time running into it. That should work fine for
2044 // deeply linked structures that are not recursively linked and catch
2045 // recursiveness quickly.
2046 if (!recursive)
2047 tv_equal_recurse_limit = 1000;
2048 if (recursive_cnt >= tv_equal_recurse_limit)
2049 {
2050 --tv_equal_recurse_limit;
2051 return TRUE;
2052 }
2053
2054 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2055 // arguments.
2056 if ((tv1->v_type == VAR_FUNC
2057 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2058 && (tv2->v_type == VAR_FUNC
2059 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2060 {
2061 ++recursive_cnt;
2062 r = func_equal(tv1, tv2, ic);
2063 --recursive_cnt;
2064 return r;
2065 }
2066
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002067 if (tv1->v_type != tv2->v_type
2068 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2069 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002070 return FALSE;
2071
2072 switch (tv1->v_type)
2073 {
2074 case VAR_LIST:
2075 ++recursive_cnt;
2076 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2077 --recursive_cnt;
2078 return r;
2079
2080 case VAR_DICT:
2081 ++recursive_cnt;
2082 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2083 --recursive_cnt;
2084 return r;
2085
2086 case VAR_BLOB:
2087 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2088
2089 case VAR_NUMBER:
2090 case VAR_BOOL:
2091 case VAR_SPECIAL:
2092 return tv1->vval.v_number == tv2->vval.v_number;
2093
2094 case VAR_STRING:
2095 s1 = tv_get_string_buf(tv1, buf1);
2096 s2 = tv_get_string_buf(tv2, buf2);
2097 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2098
2099 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002100 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002101 case VAR_JOB:
2102#ifdef FEAT_JOB_CHANNEL
2103 return tv1->vval.v_job == tv2->vval.v_job;
2104#endif
2105 case VAR_CHANNEL:
2106#ifdef FEAT_JOB_CHANNEL
2107 return tv1->vval.v_channel == tv2->vval.v_channel;
2108#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002109 case VAR_INSTR:
2110 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002111
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002112 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002113 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002114 return tv1->vval.v_class == tv2->vval.v_class;
2115
2116 case VAR_OBJECT:
Ernie Raelf0e69142024-06-22 11:12:00 +02002117 ++recursive_cnt;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002118 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
Ernie Raelf0e69142024-06-22 11:12:00 +02002119 --recursive_cnt;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002120 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002121
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002122 case VAR_PARTIAL:
2123 return tv1->vval.v_partial == tv2->vval.v_partial;
2124
2125 case VAR_FUNC:
2126 return tv1->vval.v_string == tv2->vval.v_string;
2127
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002128 case VAR_TYPEALIAS:
2129 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2130
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002131 case VAR_UNKNOWN:
2132 case VAR_ANY:
2133 case VAR_VOID:
2134 break;
2135 }
2136
2137 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2138 // does not equal anything, not even itself.
2139 return FALSE;
2140}
2141
2142/*
2143 * Get an option value.
2144 * "arg" points to the '&' or '+' before the option name.
2145 * "arg" is advanced to character after the option name.
2146 * Return OK or FAIL.
2147 */
2148 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002149eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002150 char_u **arg,
2151 typval_T *rettv, // when NULL, only check if option exists
2152 int evaluate)
2153{
2154 char_u *option_end;
2155 long numval;
2156 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002157 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002158 int c;
2159 int working = (**arg == '+'); // has("+option")
2160 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002161 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002162
2163 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002164 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002165 if (option_end == NULL)
2166 {
2167 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002168 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002169 return FAIL;
2170 }
2171
2172 if (!evaluate)
2173 {
2174 *arg = option_end;
2175 return OK;
2176 }
2177
2178 c = *option_end;
2179 *option_end = NUL;
2180 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002181 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002182
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002183 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002184 {
2185 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002186 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002187 ret = FAIL;
2188 }
2189 else if (rettv != NULL)
2190 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002191 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002192 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002193 {
2194 rettv->v_type = VAR_STRING;
2195 rettv->vval.v_string = NULL;
2196 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002197 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002198 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002199 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2200 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002201 rettv->vval.v_number = 0;
2202 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002203 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002204 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002205 if (in_vim9script() && opt_type == gov_bool)
2206 {
2207 rettv->v_type = VAR_BOOL;
2208 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2209 }
2210 else
2211 {
2212 rettv->v_type = VAR_NUMBER;
2213 rettv->vval.v_number = numval;
2214 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002215 }
2216 else // string option
2217 {
2218 rettv->v_type = VAR_STRING;
2219 rettv->vval.v_string = stringval;
2220 }
2221 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002222 else if (working && (opt_type == gov_hidden_bool
2223 || opt_type == gov_hidden_number
2224 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002225 ret = FAIL;
2226
2227 *option_end = c; // put back for error messages
2228 *arg = option_end;
2229
2230 return ret;
2231}
2232
2233/*
2234 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2235 * Return OK or FAIL.
2236 */
2237 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002238eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002239 char_u **arg,
2240 typval_T *rettv,
2241 int evaluate,
2242 int want_string UNUSED)
2243{
2244 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002245 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002246 char_u *p;
2247 int get_float = FALSE;
2248
2249 // We accept a float when the format matches
2250 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2251 // strict to avoid backwards compatibility problems.
2252 // With script version 2 and later the leading digit can be
2253 // omitted.
2254 // Don't look for a float after the "." operator, so that
2255 // ":let vers = 1.2.3" doesn't fail.
2256 if (**arg == '.')
2257 p = *arg;
2258 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002259 {
2260 p = *arg + 1;
2261 if (skip_quotes)
2262 for (;;)
2263 {
2264 if (*p == '\'')
2265 ++p;
2266 if (!vim_isdigit(*p))
2267 break;
2268 p = skipdigits(p);
2269 }
2270 else
2271 p = skipdigits(p);
2272 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002273 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2274 {
2275 get_float = TRUE;
2276 p = skipdigits(p + 2);
2277 if (*p == 'e' || *p == 'E')
2278 {
2279 ++p;
2280 if (*p == '-' || *p == '+')
2281 ++p;
2282 if (!vim_isdigit(*p))
2283 get_float = FALSE;
2284 else
2285 p = skipdigits(p + 1);
2286 }
2287 if (ASCII_ISALPHA(*p) || *p == '.')
2288 get_float = FALSE;
2289 }
2290 if (get_float)
2291 {
2292 float_T f;
2293
Bram Moolenaar29500652021-08-08 15:43:34 +02002294 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002295 if (evaluate)
2296 {
2297 rettv->v_type = VAR_FLOAT;
2298 rettv->vval.v_float = f;
2299 }
2300 }
2301 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002302 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2303 {
2304 char_u *bp;
2305 blob_T *blob = NULL; // init for gcc
2306
2307 // Blob constant: 0z0123456789abcdef
2308 if (evaluate)
2309 blob = blob_alloc();
2310 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2311 {
2312 if (!vim_isxdigit(bp[1]))
2313 {
2314 if (blob != NULL)
2315 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002316 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002317 ga_clear(&blob->bv_ga);
2318 VIM_CLEAR(blob);
2319 }
2320 return FAIL;
2321 }
2322 if (blob != NULL)
2323 ga_append(&blob->bv_ga,
2324 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2325 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2326 ++bp;
2327 }
2328 if (blob != NULL)
2329 rettv_blob_set(rettv, blob);
2330 *arg = bp;
2331 }
2332 else
2333 {
2334 varnumber_T n;
2335
2336 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002337 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002338 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002339 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002340 if (len == 0)
2341 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002342 if (evaluate)
2343 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002344 return FAIL;
2345 }
2346 *arg += len;
2347 if (evaluate)
2348 {
2349 rettv->v_type = VAR_NUMBER;
2350 rettv->vval.v_number = n;
2351 }
2352 }
2353 return OK;
2354}
2355
2356/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002357 * Evaluate a string constant and put the result in "rettv".
2358 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2359 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2360 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002361 * Return OK or FAIL.
2362 */
2363 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002364eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002365{
2366 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002367 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002368 int extra = interpolate ? 1 : 0;
2369 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002370 int len;
2371
2372 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002373 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002374 {
2375 if (*p == '\\' && p[1] != NUL)
2376 {
2377 ++p;
2378 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002379 // to 9 characters (6 for the char and 3 for a modifier):
2380 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002381 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002382 {
2383 int modifiers = 0;
2384 int flags = FSK_KEYCODE | FSK_IN_STRING;
2385
zeertzjqdb088872022-05-02 22:53:45 +01002386 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002387
2388 // Skip to the '>' to avoid using '{' inside for string
2389 // interpolation.
2390 if (p[1] != '*')
2391 flags |= FSK_SIMPLIFY;
2392 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2393 --p; // leave "p" on the ">"
2394 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002395 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002396 else if (interpolate && (*p == '{' || *p == '}'))
2397 {
2398 if (*p == '{' && p[1] != '{') // start of expression
2399 break;
2400 ++p;
2401 if (p[-1] == '}' && *p != '}') // single '}' is an error
2402 {
2403 semsg(_(e_stray_closing_curly_str), *arg);
2404 return FAIL;
2405 }
2406 --extra; // "{{" becomes "{", "}}" becomes "}"
2407 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002408 }
2409
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002410 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002411 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002412 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002413 return FAIL;
2414 }
2415
2416 // If only parsing, set *arg and return here
2417 if (!evaluate)
2418 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002419 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002420 return OK;
2421 }
2422
2423 // Copy the string into allocated memory, handling backslashed
2424 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002425 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002426 len = (int)(p - *arg + extra);
2427 rettv->vval.v_string = alloc(len);
2428 if (rettv->vval.v_string == NULL)
2429 return FAIL;
2430 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002431
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002432 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002433 {
2434 if (*p == '\\')
2435 {
2436 switch (*++p)
2437 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002438 case 'b': *end++ = BS; ++p; break;
2439 case 'e': *end++ = ESC; ++p; break;
2440 case 'f': *end++ = FF; ++p; break;
2441 case 'n': *end++ = NL; ++p; break;
2442 case 'r': *end++ = CAR; ++p; break;
2443 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002444
2445 case 'X': // hex: "\x1", "\x12"
2446 case 'x':
2447 case 'u': // Unicode: "\u0023"
2448 case 'U':
2449 if (vim_isxdigit(p[1]))
2450 {
2451 int n, nr;
Keith Thompson184f71c2024-01-04 21:19:04 +01002452 int c = SAFE_toupper(*p);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002453
2454 if (c == 'X')
2455 n = 2;
2456 else if (*p == 'u')
2457 n = 4;
2458 else
2459 n = 8;
2460 nr = 0;
2461 while (--n >= 0 && vim_isxdigit(p[1]))
2462 {
2463 ++p;
2464 nr = (nr << 4) + hex2nr(*p);
2465 }
2466 ++p;
2467 // For "\u" store the number according to
2468 // 'encoding'.
2469 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002470 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002471 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002472 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002473 }
2474 break;
2475
2476 // octal: "\1", "\12", "\123"
2477 case '0':
2478 case '1':
2479 case '2':
2480 case '3':
2481 case '4':
2482 case '5':
2483 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002484 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002485 if (*p >= '0' && *p <= '7')
2486 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002487 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002488 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002489 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002490 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002491 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002492 break;
2493
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002494 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002495 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002496 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002497 int flags = FSK_KEYCODE | FSK_IN_STRING;
2498
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002499 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002500 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002501 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002502 if (extra != 0)
2503 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002504 end += extra;
2505 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002506 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002507 break;
2508 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002509 }
2510 // FALLTHROUGH
2511
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002512 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002513 break;
2514 }
2515 }
2516 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002517 {
2518 if (interpolate && (*p == '{' || *p == '}'))
2519 {
2520 if (*p == '{' && p[1] != '{') // start of expression
2521 break;
2522 ++p; // reduce "{{" to "{" and "}}" to "}"
2523 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002524 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002525 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002526 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002527 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002528 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002529 ++p;
2530 *arg = p;
2531
2532 return OK;
2533}
2534
2535/*
2536 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002537 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2538 * Return OK when a "rettv" was set to the string.
2539 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002540 */
2541 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002542eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002543{
2544 char_u *p;
2545 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002546 int reduce = interpolate ? -1 : 0;
2547 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002548
2549 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002550 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002551 {
2552 if (*p == '\'')
2553 {
2554 if (p[1] != '\'')
2555 break;
2556 ++reduce;
2557 ++p;
2558 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002559 else if (interpolate)
2560 {
2561 if (*p == '{')
2562 {
2563 if (p[1] != '{')
2564 break;
2565 ++p;
2566 ++reduce;
2567 }
2568 else if (*p == '}')
2569 {
2570 ++p;
2571 if (*p != '}')
2572 {
2573 semsg(_(e_stray_closing_curly_str), *arg);
2574 return FAIL;
2575 }
2576 ++reduce;
2577 }
2578 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002579 }
2580
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002581 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002582 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002583 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002584 return FAIL;
2585 }
2586
2587 // If only parsing return after setting "*arg"
2588 if (!evaluate)
2589 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002590 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002591 return OK;
2592 }
2593
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002594 // Copy the string into allocated memory, handling '' to ' reduction and
2595 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002596 str = alloc((p - *arg) - reduce);
2597 if (str == NULL)
2598 return FAIL;
2599 rettv->v_type = VAR_STRING;
2600 rettv->vval.v_string = str;
2601
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002602 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002603 {
2604 if (*p == '\'')
2605 {
2606 if (p[1] != '\'')
2607 break;
2608 ++p;
2609 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002610 else if (interpolate && (*p == '{' || *p == '}'))
2611 {
2612 if (*p == '{' && p[1] != '{')
2613 break;
2614 ++p;
2615 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002616 MB_COPY_CHAR(p, str);
2617 }
2618 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002619 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002620
2621 return OK;
2622}
2623
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002624/*
2625 * Evaluate a single or double quoted string possibly containing expressions.
2626 * "arg" points to the '$'. The result is put in "rettv".
2627 * Returns OK or FAIL.
2628 */
LemonBoy2eaef102022-05-06 13:14:50 +01002629 int
2630eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2631{
2632 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002633 int ret = OK;
2634 int quote;
2635 garray_T ga;
2636 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002637
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002638 ga_init2(&ga, 1, 80);
2639
2640 // *arg is on the '$' character, move it to the first string character.
2641 ++*arg;
2642 quote = **arg;
2643 ++*arg;
2644
2645 for (;;)
2646 {
2647 // Get the string up to the matching quote or to a single '{'.
2648 // "arg" is advanced to either the quote or the '{'.
2649 if (quote == '"')
2650 ret = eval_string(arg, &tv, evaluate, TRUE);
2651 else
2652 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2653 if (ret == FAIL)
2654 break;
2655 if (evaluate)
2656 {
2657 ga_concat(&ga, tv.vval.v_string);
2658 clear_tv(&tv);
2659 }
2660
2661 if (**arg != '{')
2662 {
2663 // found terminating quote
2664 ++*arg;
2665 break;
2666 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002667 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002668 if (p == NULL)
2669 {
2670 ret = FAIL;
2671 break;
2672 }
2673 *arg = p;
2674 }
LemonBoy2eaef102022-05-06 13:14:50 +01002675
2676 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002677 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2678 {
2679 ga_clear(&ga);
2680 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002681 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002682 }
LemonBoy2eaef102022-05-06 13:14:50 +01002683
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002684 rettv->vval.v_string = ga.ga_data;
2685 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002686}
2687
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002688/*
2689 * Return a string with the string representation of a variable.
2690 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2691 * "numbuf" is used for a number.
2692 * Puts quotes around strings, so that they can be parsed back by eval().
2693 * May return NULL.
2694 */
2695 char_u *
2696tv2string(
2697 typval_T *tv,
2698 char_u **tofree,
2699 char_u *numbuf,
2700 int copyID)
2701{
2702 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2703}
2704
2705/*
2706 * Get the value of an environment variable.
2707 * "arg" is pointing to the '$'. It is advanced to after the name.
2708 * If the environment variable was not set, silently assume it is empty.
2709 * Return FAIL if the name is invalid.
2710 */
2711 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002712eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002713{
2714 char_u *string = NULL;
2715 int len;
2716 int cc;
2717 char_u *name;
2718 int mustfree = FALSE;
2719
2720 ++*arg;
2721 name = *arg;
2722 len = get_env_len(arg);
2723 if (evaluate)
2724 {
2725 if (len == 0)
2726 return FAIL; // invalid empty name
2727
2728 cc = name[len];
2729 name[len] = NUL;
2730 // first try vim_getenv(), fast for normal environment vars
2731 string = vim_getenv(name, &mustfree);
2732 if (string != NULL && *string != NUL)
2733 {
2734 if (!mustfree)
2735 string = vim_strsave(string);
2736 }
2737 else
2738 {
2739 if (mustfree)
2740 vim_free(string);
2741
2742 // next try expanding things like $VIM and ${HOME}
2743 string = expand_env_save(name - 1);
2744 if (string != NULL && *string == '$')
2745 VIM_CLEAR(string);
2746 }
2747 name[len] = cc;
2748
2749 rettv->v_type = VAR_STRING;
2750 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002751 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002752 }
2753
2754 return OK;
2755}
2756
2757/*
2758 * Get the lnum from the first argument.
2759 * Also accepts ".", "$", etc., but that only works for the current buffer.
2760 * Returns -1 on error.
2761 */
2762 linenr_T
2763tv_get_lnum(typval_T *argvars)
2764{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002765 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002766 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002767
Bram Moolenaar56acb092020-08-16 14:48:19 +02002768 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2769 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002770 if (lnum <= 0 && did_emsg_before == did_emsg
2771 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002772 {
2773 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002774 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002775
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002776 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002777 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002778 if (fp != NULL)
2779 lnum = fp->lnum;
2780 }
2781 return lnum;
2782}
2783
2784/*
2785 * Get the lnum from the first argument.
2786 * Also accepts "$", then "buf" is used.
2787 * Returns 0 on error.
2788 */
2789 linenr_T
2790tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2791{
2792 if (argvars[0].v_type == VAR_STRING
2793 && argvars[0].vval.v_string != NULL
2794 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002795 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002796 && buf != NULL)
2797 return buf->b_ml.ml_line_count;
2798 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2799}
2800
2801/*
2802 * Get buffer by number or pattern.
2803 */
2804 buf_T *
2805tv_get_buf(typval_T *tv, int curtab_only)
2806{
2807 char_u *name = tv->vval.v_string;
2808 buf_T *buf;
2809
2810 if (tv->v_type == VAR_NUMBER)
2811 return buflist_findnr((int)tv->vval.v_number);
2812 if (tv->v_type != VAR_STRING)
2813 return NULL;
2814 if (name == NULL || *name == NUL)
2815 return curbuf;
2816 if (name[0] == '$' && name[1] == NUL)
2817 return lastbuf;
2818
2819 buf = buflist_find_by_name(name, curtab_only);
2820
2821 // If not found, try expanding the name, like done for bufexists().
2822 if (buf == NULL)
2823 buf = find_buffer(tv);
2824
2825 return buf;
2826}
2827
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002828/*
2829 * Like tv_get_buf() but give an error message is the type is wrong.
2830 */
2831 buf_T *
2832tv_get_buf_from_arg(typval_T *tv)
2833{
2834 buf_T *buf;
2835
2836 ++emsg_off;
2837 buf = tv_get_buf(tv, FALSE);
2838 --emsg_off;
2839 if (buf == NULL
2840 && tv->v_type != VAR_NUMBER
2841 && tv->v_type != VAR_STRING)
2842 // issue errmsg for type error
2843 (void)tv_get_number(tv);
2844 return buf;
2845}
2846
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002847#endif // FEAT_EVAL