blob: 078e2eb058b9bf08b0df1c1c5d8d30618eed1021 [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:
265 emsg(_(e_using_class_as_number));
266 break;
267 case VAR_OBJECT:
268 emsg(_(e_using_object_as_number));
269 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200270 case VAR_VOID:
271 emsg(_(e_cannot_use_void_value));
272 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200273 case VAR_TYPEALIAS:
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +0200274 semsg(_(e_using_typealias_as_number),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200275 varp->vval.v_typealias->ta_name);
276 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200277 case VAR_UNKNOWN:
278 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200279 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200280 internal_error_no_abort("tv_get_number(UNKNOWN)");
281 break;
282 }
283 if (denote == NULL) // useful for values that must be unsigned
284 n = -1;
285 else
286 *denote = TRUE;
287 return n;
288}
289
Bram Moolenaar36967b32020-08-17 21:41:02 +0200290/*
291 * Get the number value of a variable.
292 * If it is a String variable, uses vim_str2nr().
293 * For incompatible types, return 0.
294 * tv_get_number_chk() is similar to tv_get_number(), but informs the
295 * caller of incompatible types: it sets *denote to TRUE if "denote"
296 * is not NULL or returns -1 otherwise.
297 */
298 varnumber_T
299tv_get_number(typval_T *varp)
300{
301 int error = FALSE;
302
303 return tv_get_number_chk(varp, &error); // return 0L on error
304}
305
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000306/*
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200307 * Like tv_get_number() but in Vim9 script do convert a number in a string to a
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000308 * number without giving an error.
309 */
310 varnumber_T
311tv_to_number(typval_T *varp)
312{
313 int error = FALSE;
314
315 return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
316}
317
Bram Moolenaar36967b32020-08-17 21:41:02 +0200318 varnumber_T
319tv_get_number_chk(typval_T *varp, int *denote)
320{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000321 return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200322}
323
324/*
325 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200326 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200327 */
328 varnumber_T
329tv_get_bool(typval_T *varp)
330{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000331 return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200332}
333
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200334/*
335 * Get the boolean value of "varp". This is like tv_get_number_chk(),
336 * but in Vim9 script accepts Number and Bool.
337 */
338 varnumber_T
339tv_get_bool_chk(typval_T *varp, int *denote)
340{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000341 return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200342}
343
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000344 static float_T
345tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200346{
347 switch (varp->v_type)
348 {
349 case VAR_NUMBER:
350 return (float_T)(varp->vval.v_number);
351 case VAR_FLOAT:
352 return varp->vval.v_float;
353 case VAR_FUNC:
354 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000355 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200356 break;
357 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000358 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200359 break;
360 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000361 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200362 break;
363 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000364 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200365 break;
366 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000367 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200368 break;
369 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000370 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200371 break;
372 case VAR_JOB:
373# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000374 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200375 break;
376# endif
377 case VAR_CHANNEL:
378# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000379 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200380 break;
381# endif
382 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000383 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200384 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 case VAR_CLASS:
386 emsg(_(e_using_class_as_float));
387 break;
388 case VAR_OBJECT:
389 emsg(_(e_using_object_as_float));
390 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200391 case VAR_VOID:
392 emsg(_(e_cannot_use_void_value));
393 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200394 case VAR_TYPEALIAS:
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +0200395 semsg(_(e_using_typealias_as_float),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200396 varp->vval.v_typealias->ta_name);
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
Dominique Pelle748b3082022-01-08 12:41:16 +0000632#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200633/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200634 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
635 */
636 int
637check_for_chan_or_job_arg(typval_T *args, int idx)
638{
639 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
640 {
rbtnnddc80af2021-12-17 18:01:31 +0000641 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200642 return FAIL;
643 }
644 return OK;
645}
646
647/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200648 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
649 * job.
650 */
651 int
652check_for_opt_chan_or_job_arg(typval_T *args, int idx)
653{
654 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100655 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200656}
657
658/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200659 * Give an error and return FAIL unless "args[idx]" is a job.
660 */
661 int
662check_for_job_arg(typval_T *args, int idx)
663{
664 if (args[idx].v_type != VAR_JOB)
665 {
rbtnnddc80af2021-12-17 18:01:31 +0000666 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200667 return FAIL;
668 }
669 return OK;
670}
671
672/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200673 * Check for an optional job argument at 'idx'.
674 */
675 int
676check_for_opt_job_arg(typval_T *args, int idx)
677{
678 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100679 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200680}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000681#else
682/*
683 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
684 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
685 */
686 int
687check_for_opt_chan_or_job_arg(typval_T *args, int idx)
688{
689 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
690}
Dominique Pelle748b3082022-01-08 12:41:16 +0000691#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200692
693/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200694 * Give an error and return FAIL unless "args[idx]" is a string or
695 * a number.
696 */
697 int
698check_for_string_or_number_arg(typval_T *args, int idx)
699{
700 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
701 {
rbtnnddc80af2021-12-17 18:01:31 +0000702 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200703 return FAIL;
704 }
705 return OK;
706}
707
708/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200709 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200710 */
711 int
712check_for_opt_string_or_number_arg(typval_T *args, int idx)
713{
714 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100715 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200716}
717
718/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200719 * Give an error and return FAIL unless "args[idx]" is a buffer number.
720 * Buffer number can be a number or a string.
721 */
722 int
723check_for_buffer_arg(typval_T *args, int idx)
724{
725 return check_for_string_or_number_arg(args, idx);
726}
727
728/*
729 * Check for an optional buffer argument at 'idx'
730 */
731 int
732check_for_opt_buffer_arg(typval_T *args, int idx)
733{
734 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100735 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200736}
737
738/*
739 * Give an error and return FAIL unless "args[idx]" is a line number.
740 * Line number can be a number or a string.
741 */
742 int
743check_for_lnum_arg(typval_T *args, int idx)
744{
745 return check_for_string_or_number_arg(args, idx);
746}
747
748/*
749 * Check for an optional line number argument at 'idx'
750 */
751 int
752check_for_opt_lnum_arg(typval_T *args, int idx)
753{
754 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100755 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200756}
757
Dominique Pelle748b3082022-01-08 12:41:16 +0000758#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200759/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200760 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200761 */
762 int
763check_for_string_or_blob_arg(typval_T *args, int idx)
764{
765 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
766 {
rbtnnddc80af2021-12-17 18:01:31 +0000767 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200768 return FAIL;
769 }
770 return OK;
771}
Dominique Pelle748b3082022-01-08 12:41:16 +0000772#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200773
774/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200775 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200776 */
777 int
778check_for_string_or_list_arg(typval_T *args, int idx)
779{
780 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
781 {
rbtnnddc80af2021-12-17 18:01:31 +0000782 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200783 return FAIL;
784 }
785 return OK;
786}
787
788/*
rbtnn0ccb5842021-12-18 18:33:46 +0000789 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
790 * blob.
791 */
792 int
793check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
794{
795 if (args[idx].v_type != VAR_STRING
796 && args[idx].v_type != VAR_LIST
797 && args[idx].v_type != VAR_BLOB)
798 {
799 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
800 return FAIL;
801 }
802 return OK;
803}
804
805/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200806 * Check for an optional string or list argument at 'idx'
807 */
808 int
809check_for_opt_string_or_list_arg(typval_T *args, int idx)
810{
811 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100812 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200813}
814
815/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200816 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
817 */
818 int
819check_for_string_or_dict_arg(typval_T *args, int idx)
820{
821 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
822 {
rbtnnddc80af2021-12-17 18:01:31 +0000823 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200824 return FAIL;
825 }
826 return OK;
827}
828
829/*
830 * Give an error and return FAIL unless "args[idx]" is a string or a number
831 * or a list.
832 */
833 int
834check_for_string_or_number_or_list_arg(typval_T *args, int idx)
835{
836 if (args[idx].v_type != VAR_STRING
837 && args[idx].v_type != VAR_NUMBER
838 && args[idx].v_type != VAR_LIST)
839 {
rbtnn0ccb5842021-12-18 18:33:46 +0000840 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200841 return FAIL;
842 }
843 return OK;
844}
845
846/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200847 * Give an error and return FAIL unless "args[idx]" is an optional string
848 * or number or a list
849 */
850 int
851check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
852{
853 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100854 || check_for_string_or_number_or_list_arg(args, idx)
855 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200856}
857
858/*
Bakudankun375141e2022-09-09 18:46:47 +0100859 * Give an error and return FAIL unless "args[idx]" is a string or a number
860 * or a list or a blob.
861 */
862 int
863check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
864{
865 if (args[idx].v_type != VAR_STRING
866 && args[idx].v_type != VAR_NUMBER
867 && args[idx].v_type != VAR_LIST
868 && args[idx].v_type != VAR_BLOB)
869 {
870 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
871 return FAIL;
872 }
873 return OK;
874}
875
876/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200877 * Give an error and return FAIL unless "args[idx]" is a string or a list
878 * or a dict.
879 */
880 int
881check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
882{
883 if (args[idx].v_type != VAR_STRING
884 && args[idx].v_type != VAR_LIST
885 && args[idx].v_type != VAR_DICT)
886 {
rbtnnddc80af2021-12-17 18:01:31 +0000887 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200888 return FAIL;
889 }
890 return OK;
891}
892
893/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000894 * Give an error and return FAIL unless "args[idx]" is a string
895 * or a function reference.
896 */
897 int
898check_for_string_or_func_arg(typval_T *args, int idx)
899{
900 if (args[idx].v_type != VAR_PARTIAL
901 && args[idx].v_type != VAR_FUNC
902 && args[idx].v_type != VAR_STRING)
903 {
904 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
905 return FAIL;
906 }
907 return OK;
908}
909
910/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200911 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
912 */
913 int
914check_for_list_or_blob_arg(typval_T *args, int idx)
915{
916 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
917 {
rbtnn0ccb5842021-12-18 18:33:46 +0000918 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200919 return FAIL;
920 }
921 return OK;
922}
923
924/*
925 * Give an error and return FAIL unless "args[idx]" is a list or dict
926 */
927 int
928check_for_list_or_dict_arg(typval_T *args, int idx)
929{
930 if (args[idx].v_type != VAR_LIST
931 && args[idx].v_type != VAR_DICT)
932 {
rbtnnddc80af2021-12-17 18:01:31 +0000933 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200934 return FAIL;
935 }
936 return OK;
937}
938
939/*
940 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
941 * blob.
942 */
943 int
944check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
945{
946 if (args[idx].v_type != VAR_LIST
947 && args[idx].v_type != VAR_DICT
948 && args[idx].v_type != VAR_BLOB)
949 {
rbtnnddc80af2021-12-17 18:01:31 +0000950 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200951 return FAIL;
952 }
953 return OK;
954}
955
956/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000957 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
958 * blob or a string.
959 */
960 int
961check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
962{
963 if (args[idx].v_type != VAR_LIST
964 && args[idx].v_type != VAR_DICT
965 && args[idx].v_type != VAR_BLOB
966 && args[idx].v_type != VAR_STRING)
967 {
rbtnnddc80af2021-12-17 18:01:31 +0000968 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000969 return FAIL;
970 }
971 return OK;
972}
973
974/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200975 * Give an error and return FAIL unless "args[idx]" is an optional buffer
976 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200977 */
978 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200979check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200980{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200981 if (args[idx].v_type != VAR_UNKNOWN
982 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200983 && args[idx].v_type != VAR_NUMBER
984 && args[idx].v_type != VAR_DICT)
985 {
rbtnnddc80af2021-12-17 18:01:31 +0000986 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200987 return FAIL;
988 }
989 return OK;
990}
991
992/*
LemonBoyafe04662023-08-23 21:08:11 +0200993 * Give an error and return FAIL unless "args[idx]" is an object.
994 */
995 int
996check_for_object_arg(typval_T *args, int idx)
997{
998 if (args[idx].v_type != VAR_OBJECT)
999 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001000 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001001 return FAIL;
1002 }
1003 return OK;
1004}
1005
1006/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001007 * Returns TRUE if "tv" is a type alias for a class
1008 */
1009 int
1010tv_class_alias(typval_T *tv)
1011{
1012 return tv->v_type == VAR_TYPEALIAS &&
1013 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1014}
1015
1016/*
LemonBoyafe04662023-08-23 21:08:11 +02001017 * Give an error and return FAIL unless "args[idx]" is a class or a list.
1018 */
1019 int
1020check_for_class_or_list_arg(typval_T *args, int idx)
1021{
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001022 if (args[idx].v_type != VAR_CLASS && args[idx].v_type != VAR_LIST
1023 && !tv_class_alias(&args[idx]))
LemonBoyafe04662023-08-23 21:08:11 +02001024 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001025 semsg(_(e_list_or_class_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001026 return FAIL;
1027 }
1028 return OK;
1029}
1030
1031/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001032 * Get the string value of a variable.
1033 * If it is a Number variable, the number is converted into a string.
1034 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1035 * tv_get_string_buf() uses a given buffer.
1036 * If the String variable has never been set, return an empty string.
1037 * Never returns NULL;
1038 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1039 * NULL on error.
1040 */
1041 char_u *
1042tv_get_string(typval_T *varp)
1043{
1044 static char_u mybuf[NUMBUFLEN];
1045
1046 return tv_get_string_buf(varp, mybuf);
1047}
1048
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001049/*
1050 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1051 */
1052 char_u *
1053tv_get_string_strict(typval_T *varp)
1054{
1055 static char_u mybuf[NUMBUFLEN];
1056 char_u *res = tv_get_string_buf_chk_strict(
1057 varp, mybuf, in_vim9script());
1058
1059 return res != NULL ? res : (char_u *)"";
1060}
1061
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001062 char_u *
1063tv_get_string_buf(typval_T *varp, char_u *buf)
1064{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001065 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001066
1067 return res != NULL ? res : (char_u *)"";
1068}
1069
1070/*
1071 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1072 */
1073 char_u *
1074tv_get_string_chk(typval_T *varp)
1075{
1076 static char_u mybuf[NUMBUFLEN];
1077
1078 return tv_get_string_buf_chk(varp, mybuf);
1079}
1080
1081 char_u *
1082tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1083{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001084 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1085}
1086
1087 char_u *
1088tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1089{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001090 switch (varp->v_type)
1091 {
1092 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001093 if (strict)
1094 {
1095 emsg(_(e_using_number_as_string));
1096 break;
1097 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001098 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1099 (varnumber_T)varp->vval.v_number);
1100 return buf;
1101 case VAR_FUNC:
1102 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001103 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001104 break;
1105 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001106 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001107 break;
1108 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001109 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001110 break;
1111 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001112 if (strict)
1113 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001114 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001115 break;
1116 }
1117 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1118 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001119 case VAR_STRING:
1120 if (varp->vval.v_string != NULL)
1121 return varp->vval.v_string;
1122 return (char_u *)"";
1123 case VAR_BOOL:
1124 case VAR_SPECIAL:
1125 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1126 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001127 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001128 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001129 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001130 case VAR_CLASS:
1131 emsg(_(e_using_class_as_string));
1132 break;
1133 case VAR_OBJECT:
1134 emsg(_(e_using_object_as_string));
1135 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001136 case VAR_JOB:
1137#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001138 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001139 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001140 semsg(_(e_using_invalid_value_as_string_str), "job");
1141 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001143 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001144#endif
1145 break;
1146 case VAR_CHANNEL:
1147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001148 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001149 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001150 semsg(_(e_using_invalid_value_as_string_str), "channel");
1151 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001152 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001153 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001154#endif
1155 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001156 case VAR_VOID:
1157 emsg(_(e_cannot_use_void_value));
1158 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001159 case VAR_TYPEALIAS:
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001160 semsg(_(e_using_typealias_as_string),
1161 varp->vval.v_typealias->ta_name);
1162 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001163 case VAR_UNKNOWN:
1164 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001165 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001166 semsg(_(e_using_invalid_value_as_string_str),
1167 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001168 break;
1169 }
1170 return NULL;
1171}
1172
1173/*
1174 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1175 * string() on Dict, List, etc.
1176 */
1177 char_u *
1178tv_stringify(typval_T *varp, char_u *buf)
1179{
1180 if (varp->v_type == VAR_LIST
1181 || varp->v_type == VAR_DICT
1182 || varp->v_type == VAR_BLOB
1183 || varp->v_type == VAR_FUNC
1184 || varp->v_type == VAR_PARTIAL
1185 || varp->v_type == VAR_FLOAT)
1186 {
1187 typval_T tmp;
1188
1189 f_string(varp, &tmp);
1190 tv_get_string_buf(&tmp, buf);
1191 clear_tv(varp);
1192 *varp = tmp;
1193 return tmp.vval.v_string;
1194 }
1195 return tv_get_string_buf(varp, buf);
1196}
1197
1198/*
1199 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1200 * Also give an error message, using "name" or _("name") when use_gettext is
1201 * TRUE.
1202 */
1203 int
1204tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1205{
1206 int lock = 0;
1207
1208 switch (tv->v_type)
1209 {
1210 case VAR_BLOB:
1211 if (tv->vval.v_blob != NULL)
1212 lock = tv->vval.v_blob->bv_lock;
1213 break;
1214 case VAR_LIST:
1215 if (tv->vval.v_list != NULL)
1216 lock = tv->vval.v_list->lv_lock;
1217 break;
1218 case VAR_DICT:
1219 if (tv->vval.v_dict != NULL)
1220 lock = tv->vval.v_dict->dv_lock;
1221 break;
1222 default:
1223 break;
1224 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001225 return value_check_lock(tv->v_lock, name, use_gettext)
1226 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001227}
1228
1229/*
1230 * Copy the values from typval_T "from" to typval_T "to".
1231 * When needed allocates string or increases reference count.
1232 * Does not make a copy of a list, blob or dict but copies the reference!
1233 * It is OK for "from" and "to" to point to the same item. This is used to
1234 * make a copy later.
1235 */
1236 void
1237copy_tv(typval_T *from, typval_T *to)
1238{
1239 to->v_type = from->v_type;
1240 to->v_lock = 0;
1241 switch (from->v_type)
1242 {
1243 case VAR_NUMBER:
1244 case VAR_BOOL:
1245 case VAR_SPECIAL:
1246 to->vval.v_number = from->vval.v_number;
1247 break;
1248 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001249 to->vval.v_float = from->vval.v_float;
1250 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001251 case VAR_JOB:
1252#ifdef FEAT_JOB_CHANNEL
1253 to->vval.v_job = from->vval.v_job;
1254 if (to->vval.v_job != NULL)
1255 ++to->vval.v_job->jv_refcount;
1256 break;
1257#endif
1258 case VAR_CHANNEL:
1259#ifdef FEAT_JOB_CHANNEL
1260 to->vval.v_channel = from->vval.v_channel;
1261 if (to->vval.v_channel != NULL)
1262 ++to->vval.v_channel->ch_refcount;
1263 break;
1264#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001265 case VAR_INSTR:
1266 to->vval.v_instr = from->vval.v_instr;
1267 break;
1268
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001269 case VAR_CLASS:
1270 copy_class(from, to);
1271 break;
1272
1273 case VAR_OBJECT:
1274 copy_object(from, to);
1275 break;
1276
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 case VAR_STRING:
1278 case VAR_FUNC:
1279 if (from->vval.v_string == NULL)
1280 to->vval.v_string = NULL;
1281 else
1282 {
1283 to->vval.v_string = vim_strsave(from->vval.v_string);
1284 if (from->v_type == VAR_FUNC)
1285 func_ref(to->vval.v_string);
1286 }
1287 break;
1288 case VAR_PARTIAL:
1289 if (from->vval.v_partial == NULL)
1290 to->vval.v_partial = NULL;
1291 else
1292 {
1293 to->vval.v_partial = from->vval.v_partial;
1294 ++to->vval.v_partial->pt_refcount;
1295 }
1296 break;
1297 case VAR_BLOB:
1298 if (from->vval.v_blob == NULL)
1299 to->vval.v_blob = NULL;
1300 else
1301 {
1302 to->vval.v_blob = from->vval.v_blob;
1303 ++to->vval.v_blob->bv_refcount;
1304 }
1305 break;
1306 case VAR_LIST:
1307 if (from->vval.v_list == NULL)
1308 to->vval.v_list = NULL;
1309 else
1310 {
1311 to->vval.v_list = from->vval.v_list;
1312 ++to->vval.v_list->lv_refcount;
1313 }
1314 break;
1315 case VAR_DICT:
1316 if (from->vval.v_dict == NULL)
1317 to->vval.v_dict = NULL;
1318 else
1319 {
1320 to->vval.v_dict = from->vval.v_dict;
1321 ++to->vval.v_dict->dv_refcount;
1322 }
1323 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001324 case VAR_TYPEALIAS:
1325 if (from->vval.v_typealias == NULL)
1326 to->vval.v_typealias = NULL;
1327 else
1328 {
1329 to->vval.v_typealias = from->vval.v_typealias;
1330 ++to->vval.v_typealias->ta_refcount;
1331 }
1332 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001333 case VAR_VOID:
1334 emsg(_(e_cannot_use_void_value));
1335 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001336 case VAR_UNKNOWN:
1337 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001338 internal_error_no_abort("copy_tv(UNKNOWN)");
1339 break;
1340 }
1341}
1342
1343/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001344 * Compare "tv1" and "tv2".
1345 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001346 */
1347 int
1348typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001349 typval_T *tv1, // first operand
1350 typval_T *tv2, // second operand
1351 exprtype_T type, // operator
1352 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001353{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001354 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001355 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001356 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1357
Bram Moolenaar265f8112021-12-19 12:33:05 +00001358 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001359 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001360 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001361 // it means TRUE.
1362 n1 = (type == EXPR_ISNOT);
1363 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001364 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1365 || (tv2->v_type == VAR_SPECIAL
1366 && tv2->vval.v_number == VVAL_NULL))
1367 && tv1->v_type != tv2->v_type
1368 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1369 {
1370 n1 = typval_compare_null(tv1, tv2);
1371 if (n1 == MAYBE)
1372 {
1373 clear_tv(tv1);
1374 return FAIL;
1375 }
1376 if (type == EXPR_NEQUAL)
1377 n1 = !n1;
1378 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001379 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001380 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001381 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001382 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001383 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001384 return FAIL;
1385 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001386 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001387 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001388 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001389 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001390 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001391 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001392 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001393 return FAIL;
1394 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001395 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001396 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001397 else if (tv1->v_type == VAR_CLASS || tv2->v_type == VAR_CLASS)
1398 {
1399 if (typval_compare_class(tv1, tv2, type, ic, &res) == FAIL)
1400 {
1401 clear_tv(tv1);
1402 return FAIL;
1403 }
1404 n1 = res;
1405 }
1406 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1407 {
1408 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1409 {
1410 clear_tv(tv1);
1411 return FAIL;
1412 }
1413 n1 = res;
1414 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001415 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001416 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001417 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001418 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001419 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001420 return FAIL;
1421 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001422 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001423 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001424 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1425 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001426 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001427 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001428 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001429 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001430 return FAIL;
1431 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001432 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433 }
1434
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001435 // If one of the two variables is a float, compare as a float.
1436 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001437 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001438 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1439 {
1440 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001441 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001442
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001443 f1 = tv_get_float_chk(tv1, &error);
1444 if (!error)
1445 f2 = tv_get_float_chk(tv2, &error);
1446 if (error)
1447 {
1448 clear_tv(tv1);
1449 return FAIL;
1450 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001451 n1 = FALSE;
1452 switch (type)
1453 {
1454 case EXPR_IS:
1455 case EXPR_EQUAL: n1 = (f1 == f2); break;
1456 case EXPR_ISNOT:
1457 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1458 case EXPR_GREATER: n1 = (f1 > f2); break;
1459 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1460 case EXPR_SMALLER: n1 = (f1 < f2); break;
1461 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1462 case EXPR_UNKNOWN:
1463 case EXPR_MATCH:
1464 default: break; // avoid gcc warning
1465 }
1466 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001467
1468 // If one of the two variables is a number, compare as a number.
1469 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001470 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001471 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1472 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001473 int error = FALSE;
1474
1475 n1 = tv_get_number_chk(tv1, &error);
1476 if (!error)
1477 n2 = tv_get_number_chk(tv2, &error);
1478 if (error)
1479 {
1480 clear_tv(tv1);
1481 return FAIL;
1482 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001483 switch (type)
1484 {
1485 case EXPR_IS:
1486 case EXPR_EQUAL: n1 = (n1 == n2); break;
1487 case EXPR_ISNOT:
1488 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1489 case EXPR_GREATER: n1 = (n1 > n2); break;
1490 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1491 case EXPR_SMALLER: n1 = (n1 < n2); break;
1492 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1493 case EXPR_UNKNOWN:
1494 case EXPR_MATCH:
1495 default: break; // avoid gcc warning
1496 }
1497 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001498 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1499 || tv2->v_type == VAR_BOOL
1500 || (tv1->v_type == VAR_SPECIAL
1501 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001502 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001503 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001504 {
1505 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001506 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1507 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001508 return FAIL;
1509 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001510 n1 = tv1->vval.v_number;
1511 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001512 switch (type)
1513 {
1514 case EXPR_IS:
1515 case EXPR_EQUAL: n1 = (n1 == n2); break;
1516 case EXPR_ISNOT:
1517 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1518 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001519 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001520 vartype_name(tv1->v_type));
1521 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001522 return FAIL;
1523 }
1524 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001525#ifdef FEAT_JOB_CHANNEL
1526 else if (tv1->v_type == tv2->v_type
1527 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1528 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1529 {
1530 if (tv1->v_type == VAR_CHANNEL)
1531 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1532 else
1533 n1 = tv1->vval.v_job == tv2->vval.v_job;
1534 if (type == EXPR_NEQUAL)
1535 n1 = !n1;
1536 }
1537#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001538 else
1539 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001540 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001541 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001542 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001543 return FAIL;
1544 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001545 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001546 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001547 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001548 if (in_vim9script())
1549 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001550 tv1->v_type = VAR_BOOL;
1551 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001552 }
1553 else
1554 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001555 tv1->v_type = VAR_NUMBER;
1556 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001557 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001558
1559 return OK;
1560}
1561
Bram Moolenaar34453202021-01-31 13:08:38 +01001562/*
dundargocc57b5bc2022-11-02 13:30:51 +00001563 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001564 * Put the result, false or true, in "res".
1565 * Return FAIL and give an error message when the comparison can't be done.
1566 */
1567 int
1568typval_compare_list(
1569 typval_T *tv1,
1570 typval_T *tv2,
1571 exprtype_T type,
1572 int ic,
1573 int *res)
1574{
1575 int val = 0;
1576
1577 if (type == EXPR_IS || type == EXPR_ISNOT)
1578 {
1579 val = (tv1->v_type == tv2->v_type
1580 && tv1->vval.v_list == tv2->vval.v_list);
1581 if (type == EXPR_ISNOT)
1582 val = !val;
1583 }
1584 else if (tv1->v_type != tv2->v_type
1585 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1586 {
1587 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001588 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001589 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001590 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001591 return FAIL;
1592 }
1593 else
1594 {
1595 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1596 ic, FALSE);
1597 if (type == EXPR_NEQUAL)
1598 val = !val;
1599 }
1600 *res = val;
1601 return OK;
1602}
1603
1604/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001605 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001606 */
1607 int
1608typval_compare_null(typval_T *tv1, typval_T *tv2)
1609{
1610 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1611 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1612 {
1613 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1614
1615 switch (tv->v_type)
1616 {
1617 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001618#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001619 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001620#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001621 // TODO: null_class handling
1622 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001623 case VAR_DICT: return tv->vval.v_dict == NULL;
1624 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001625#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001626 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001627#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001628 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001629 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001630 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1631 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001632
1633 case VAR_NUMBER: if (!in_vim9script())
1634 return tv->vval.v_number == 0;
1635 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001636 case VAR_FLOAT: if (!in_vim9script())
1637 return tv->vval.v_float == 0.0;
1638 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001639 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001640 default: break;
1641 }
1642 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001643 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001644 // we won't give an error
1645 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001646}
1647
1648/*
dundargocc57b5bc2022-11-02 13:30:51 +00001649 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001650 * Put the result, false or true, in "res".
1651 * Return FAIL and give an error message when the comparison can't be done.
1652 */
1653 int
1654typval_compare_blob(
1655 typval_T *tv1,
1656 typval_T *tv2,
1657 exprtype_T type,
1658 int *res)
1659{
1660 int val = 0;
1661
1662 if (type == EXPR_IS || type == EXPR_ISNOT)
1663 {
1664 val = (tv1->v_type == tv2->v_type
1665 && tv1->vval.v_blob == tv2->vval.v_blob);
1666 if (type == EXPR_ISNOT)
1667 val = !val;
1668 }
1669 else if (tv1->v_type != tv2->v_type
1670 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1671 {
1672 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001673 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001674 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001675 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001676 return FAIL;
1677 }
1678 else
1679 {
1680 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1681 if (type == EXPR_NEQUAL)
1682 val = !val;
1683 }
1684 *res = val;
1685 return OK;
1686}
1687
1688/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001689 * Compare "tv1" to "tv2" as classes according to "type".
1690 * Put the result, false or true, in "res".
1691 * Return FAIL and give an error message when the comparison can't be done.
1692 */
1693 int
1694typval_compare_class(
1695 typval_T *tv1,
1696 typval_T *tv2,
1697 exprtype_T type UNUSED,
1698 int ic UNUSED,
1699 int *res)
1700{
1701 // TODO: use "type"
1702 *res = tv1->vval.v_class == tv2->vval.v_class;
1703 return OK;
1704}
1705
1706/*
1707 * Compare "tv1" to "tv2" as objects according to "type".
1708 * Put the result, false or true, in "res".
1709 * Return FAIL and give an error message when the comparison can't be done.
1710 */
1711 int
1712typval_compare_object(
1713 typval_T *tv1,
1714 typval_T *tv2,
1715 exprtype_T type,
1716 int ic,
1717 int *res)
1718{
1719 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1720
1721 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1722 {
1723 *res = res_match;
1724 return OK;
1725 }
1726 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1727 {
1728 *res = !res_match;
1729 return OK;
1730 }
1731
1732 class_T *cl1 = tv1->vval.v_object->obj_class;
1733 class_T *cl2 = tv2->vval.v_object->obj_class;
1734 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1735 {
1736 *res = !res_match;
1737 return OK;
1738 }
1739
1740 object_T *obj1 = tv1->vval.v_object;
1741 object_T *obj2 = tv2->vval.v_object;
1742 if (type == EXPR_IS || type == EXPR_ISNOT)
1743 {
1744 *res = obj1 == obj2 ? res_match : !res_match;
1745 return OK;
1746 }
1747
1748 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1749 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1750 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1751 {
1752 *res = !res_match;
1753 return OK;
1754 }
1755 *res = res_match;
1756 return OK;
1757}
1758
1759/*
dundargocc57b5bc2022-11-02 13:30:51 +00001760 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001761 * Put the result, false or true, in "res".
1762 * Return FAIL and give an error message when the comparison can't be done.
1763 */
1764 int
1765typval_compare_dict(
1766 typval_T *tv1,
1767 typval_T *tv2,
1768 exprtype_T type,
1769 int ic,
1770 int *res)
1771{
1772 int val;
1773
1774 if (type == EXPR_IS || type == EXPR_ISNOT)
1775 {
1776 val = (tv1->v_type == tv2->v_type
1777 && tv1->vval.v_dict == tv2->vval.v_dict);
1778 if (type == EXPR_ISNOT)
1779 val = !val;
1780 }
1781 else if (tv1->v_type != tv2->v_type
1782 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1783 {
1784 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001785 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001786 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001787 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001788 return FAIL;
1789 }
1790 else
1791 {
1792 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1793 if (type == EXPR_NEQUAL)
1794 val = !val;
1795 }
1796 *res = val;
1797 return OK;
1798}
1799
1800/*
dundargocc57b5bc2022-11-02 13:30:51 +00001801 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001802 * Put the result, false or true, in "res".
1803 * Return FAIL and give an error message when the comparison can't be done.
1804 */
1805 int
1806typval_compare_func(
1807 typval_T *tv1,
1808 typval_T *tv2,
1809 exprtype_T type,
1810 int ic,
1811 int *res)
1812{
1813 int val = 0;
1814
1815 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1816 && type != EXPR_IS && type != EXPR_ISNOT)
1817 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001818 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001819 return FAIL;
1820 }
1821 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1822 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1823 // When both partials are NULL, then they are equal.
1824 // Otherwise they are not equal.
1825 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1826 else if (type == EXPR_IS || type == EXPR_ISNOT)
1827 {
1828 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1829 // strings are considered the same if their value is
1830 // the same
1831 val = tv_equal(tv1, tv2, ic, FALSE);
1832 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1833 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1834 else
1835 val = FALSE;
1836 }
1837 else
1838 val = tv_equal(tv1, tv2, ic, FALSE);
1839 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1840 val = !val;
1841 *res = val;
1842 return OK;
1843}
1844
1845/*
1846 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1847 * Put the result, false or true, in "res".
1848 * Return FAIL and give an error message when the comparison can't be done.
1849 */
1850 int
1851typval_compare_string(
1852 typval_T *tv1,
1853 typval_T *tv2,
1854 exprtype_T type,
1855 int ic,
1856 int *res)
1857{
1858 int i = 0;
1859 int val = FALSE;
1860 char_u *s1, *s2;
1861 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1862
1863 if (in_vim9script()
1864 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1865 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1866 {
1867 semsg(_(e_cannot_compare_str_with_str),
1868 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1869 return FAIL;
1870 }
1871 s1 = tv_get_string_buf(tv1, buf1);
1872 s2 = tv_get_string_buf(tv2, buf2);
1873 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1874 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1875 switch (type)
1876 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001877 case EXPR_IS: if (in_vim9script())
1878 {
1879 // Really check it is the same string, not just
1880 // the same value.
1881 val = tv1->vval.v_string == tv2->vval.v_string;
1882 break;
1883 }
1884 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001885 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001886 case EXPR_ISNOT: if (in_vim9script())
1887 {
1888 // Really check it is not the same string, not
1889 // just a different value.
1890 val = tv1->vval.v_string != tv2->vval.v_string;
1891 break;
1892 }
1893 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001894 case EXPR_NEQUAL: val = (i != 0); break;
1895 case EXPR_GREATER: val = (i > 0); break;
1896 case EXPR_GEQUAL: val = (i >= 0); break;
1897 case EXPR_SMALLER: val = (i < 0); break;
1898 case EXPR_SEQUAL: val = (i <= 0); break;
1899
1900 case EXPR_MATCH:
1901 case EXPR_NOMATCH:
1902 val = pattern_match(s2, s1, ic);
1903 if (type == EXPR_NOMATCH)
1904 val = !val;
1905 break;
1906
1907 default: break; // avoid gcc warning
1908 }
1909 *res = val;
1910 return OK;
1911}
1912/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001913 * Convert any type to a string, never give an error.
1914 * When "quotes" is TRUE add quotes to a string.
1915 * Returns an allocated string.
1916 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001917 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001918typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001919{
1920 char_u *tofree;
1921 char_u numbuf[NUMBUFLEN];
1922 char_u *ret = NULL;
1923
1924 if (arg == NULL)
1925 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001926 if (!quotes && arg->v_type == VAR_STRING)
1927 {
1928 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1929 : arg->vval.v_string);
1930 }
1931 else
1932 {
1933 ret = tv2string(arg, &tofree, numbuf, 0);
1934 // Make a copy if we have a value but it's not in allocated memory.
1935 if (ret != NULL && tofree == NULL)
1936 ret = vim_strsave(ret);
1937 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001938 return ret;
1939}
1940
1941/*
1942 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1943 * or it refers to a List or Dictionary that is locked.
1944 */
1945 int
1946tv_islocked(typval_T *tv)
1947{
1948 return (tv->v_lock & VAR_LOCKED)
1949 || (tv->v_type == VAR_LIST
1950 && tv->vval.v_list != NULL
1951 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1952 || (tv->v_type == VAR_DICT
1953 && tv->vval.v_dict != NULL
1954 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1955}
1956
1957 static int
1958func_equal(
1959 typval_T *tv1,
1960 typval_T *tv2,
1961 int ic) // ignore case
1962{
1963 char_u *s1, *s2;
1964 dict_T *d1, *d2;
1965 int a1, a2;
1966 int i;
1967
1968 // empty and NULL function name considered the same
1969 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1970 : partial_name(tv1->vval.v_partial);
1971 if (s1 != NULL && *s1 == NUL)
1972 s1 = NULL;
1973 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1974 : partial_name(tv2->vval.v_partial);
1975 if (s2 != NULL && *s2 == NUL)
1976 s2 = NULL;
1977 if (s1 == NULL || s2 == NULL)
1978 {
1979 if (s1 != s2)
1980 return FALSE;
1981 }
1982 else if (STRCMP(s1, s2) != 0)
1983 return FALSE;
1984
1985 // empty dict and NULL dict is different
1986 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1987 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1988 if (d1 == NULL || d2 == NULL)
1989 {
1990 if (d1 != d2)
1991 return FALSE;
1992 }
1993 else if (!dict_equal(d1, d2, ic, TRUE))
1994 return FALSE;
1995
1996 // empty list and no list considered the same
1997 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1998 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1999 if (a1 != a2)
2000 return FALSE;
2001 for (i = 0; i < a1; ++i)
2002 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
2003 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
2004 return FALSE;
2005
2006 return TRUE;
2007}
2008
2009/*
2010 * Return TRUE if "tv1" and "tv2" have the same value.
2011 * Compares the items just like "==" would compare them, but strings and
2012 * numbers are different. Floats and numbers are also different.
2013 */
2014 int
2015tv_equal(
2016 typval_T *tv1,
2017 typval_T *tv2,
2018 int ic, // ignore case
2019 int recursive) // TRUE when used recursively
2020{
2021 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2022 char_u *s1, *s2;
2023 static int recursive_cnt = 0; // catch recursive loops
2024 int r;
2025 static int tv_equal_recurse_limit;
2026
2027 // Catch lists and dicts that have an endless loop by limiting
2028 // recursiveness to a limit. We guess they are equal then.
2029 // A fixed limit has the problem of still taking an awful long time.
2030 // Reduce the limit every time running into it. That should work fine for
2031 // deeply linked structures that are not recursively linked and catch
2032 // recursiveness quickly.
2033 if (!recursive)
2034 tv_equal_recurse_limit = 1000;
2035 if (recursive_cnt >= tv_equal_recurse_limit)
2036 {
2037 --tv_equal_recurse_limit;
2038 return TRUE;
2039 }
2040
2041 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2042 // arguments.
2043 if ((tv1->v_type == VAR_FUNC
2044 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2045 && (tv2->v_type == VAR_FUNC
2046 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2047 {
2048 ++recursive_cnt;
2049 r = func_equal(tv1, tv2, ic);
2050 --recursive_cnt;
2051 return r;
2052 }
2053
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002054 if (tv1->v_type != tv2->v_type
2055 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2056 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002057 return FALSE;
2058
2059 switch (tv1->v_type)
2060 {
2061 case VAR_LIST:
2062 ++recursive_cnt;
2063 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2064 --recursive_cnt;
2065 return r;
2066
2067 case VAR_DICT:
2068 ++recursive_cnt;
2069 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2070 --recursive_cnt;
2071 return r;
2072
2073 case VAR_BLOB:
2074 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2075
2076 case VAR_NUMBER:
2077 case VAR_BOOL:
2078 case VAR_SPECIAL:
2079 return tv1->vval.v_number == tv2->vval.v_number;
2080
2081 case VAR_STRING:
2082 s1 = tv_get_string_buf(tv1, buf1);
2083 s2 = tv_get_string_buf(tv2, buf2);
2084 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2085
2086 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002087 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002088 case VAR_JOB:
2089#ifdef FEAT_JOB_CHANNEL
2090 return tv1->vval.v_job == tv2->vval.v_job;
2091#endif
2092 case VAR_CHANNEL:
2093#ifdef FEAT_JOB_CHANNEL
2094 return tv1->vval.v_channel == tv2->vval.v_channel;
2095#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002096 case VAR_INSTR:
2097 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002098
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002099 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002100 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002101 return tv1->vval.v_class == tv2->vval.v_class;
2102
2103 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002104 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2105 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002106
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107 case VAR_PARTIAL:
2108 return tv1->vval.v_partial == tv2->vval.v_partial;
2109
2110 case VAR_FUNC:
2111 return tv1->vval.v_string == tv2->vval.v_string;
2112
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002113 case VAR_TYPEALIAS:
2114 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2115
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002116 case VAR_UNKNOWN:
2117 case VAR_ANY:
2118 case VAR_VOID:
2119 break;
2120 }
2121
2122 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2123 // does not equal anything, not even itself.
2124 return FALSE;
2125}
2126
2127/*
2128 * Get an option value.
2129 * "arg" points to the '&' or '+' before the option name.
2130 * "arg" is advanced to character after the option name.
2131 * Return OK or FAIL.
2132 */
2133 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002134eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002135 char_u **arg,
2136 typval_T *rettv, // when NULL, only check if option exists
2137 int evaluate)
2138{
2139 char_u *option_end;
2140 long numval;
2141 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002142 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002143 int c;
2144 int working = (**arg == '+'); // has("+option")
2145 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002146 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002147
2148 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002149 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002150 if (option_end == NULL)
2151 {
2152 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002153 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002154 return FAIL;
2155 }
2156
2157 if (!evaluate)
2158 {
2159 *arg = option_end;
2160 return OK;
2161 }
2162
2163 c = *option_end;
2164 *option_end = NUL;
2165 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002166 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002167
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002168 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002169 {
2170 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002171 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002172 ret = FAIL;
2173 }
2174 else if (rettv != NULL)
2175 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002176 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002177 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002178 {
2179 rettv->v_type = VAR_STRING;
2180 rettv->vval.v_string = NULL;
2181 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002182 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002183 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002184 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2185 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002186 rettv->vval.v_number = 0;
2187 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002188 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002190 if (in_vim9script() && opt_type == gov_bool)
2191 {
2192 rettv->v_type = VAR_BOOL;
2193 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2194 }
2195 else
2196 {
2197 rettv->v_type = VAR_NUMBER;
2198 rettv->vval.v_number = numval;
2199 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002200 }
2201 else // string option
2202 {
2203 rettv->v_type = VAR_STRING;
2204 rettv->vval.v_string = stringval;
2205 }
2206 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002207 else if (working && (opt_type == gov_hidden_bool
2208 || opt_type == gov_hidden_number
2209 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002210 ret = FAIL;
2211
2212 *option_end = c; // put back for error messages
2213 *arg = option_end;
2214
2215 return ret;
2216}
2217
2218/*
2219 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2220 * Return OK or FAIL.
2221 */
2222 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002223eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002224 char_u **arg,
2225 typval_T *rettv,
2226 int evaluate,
2227 int want_string UNUSED)
2228{
2229 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002230 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002231 char_u *p;
2232 int get_float = FALSE;
2233
2234 // We accept a float when the format matches
2235 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2236 // strict to avoid backwards compatibility problems.
2237 // With script version 2 and later the leading digit can be
2238 // omitted.
2239 // Don't look for a float after the "." operator, so that
2240 // ":let vers = 1.2.3" doesn't fail.
2241 if (**arg == '.')
2242 p = *arg;
2243 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002244 {
2245 p = *arg + 1;
2246 if (skip_quotes)
2247 for (;;)
2248 {
2249 if (*p == '\'')
2250 ++p;
2251 if (!vim_isdigit(*p))
2252 break;
2253 p = skipdigits(p);
2254 }
2255 else
2256 p = skipdigits(p);
2257 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002258 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2259 {
2260 get_float = TRUE;
2261 p = skipdigits(p + 2);
2262 if (*p == 'e' || *p == 'E')
2263 {
2264 ++p;
2265 if (*p == '-' || *p == '+')
2266 ++p;
2267 if (!vim_isdigit(*p))
2268 get_float = FALSE;
2269 else
2270 p = skipdigits(p + 1);
2271 }
2272 if (ASCII_ISALPHA(*p) || *p == '.')
2273 get_float = FALSE;
2274 }
2275 if (get_float)
2276 {
2277 float_T f;
2278
Bram Moolenaar29500652021-08-08 15:43:34 +02002279 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002280 if (evaluate)
2281 {
2282 rettv->v_type = VAR_FLOAT;
2283 rettv->vval.v_float = f;
2284 }
2285 }
2286 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002287 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2288 {
2289 char_u *bp;
2290 blob_T *blob = NULL; // init for gcc
2291
2292 // Blob constant: 0z0123456789abcdef
2293 if (evaluate)
2294 blob = blob_alloc();
2295 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2296 {
2297 if (!vim_isxdigit(bp[1]))
2298 {
2299 if (blob != NULL)
2300 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002301 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002302 ga_clear(&blob->bv_ga);
2303 VIM_CLEAR(blob);
2304 }
2305 return FAIL;
2306 }
2307 if (blob != NULL)
2308 ga_append(&blob->bv_ga,
2309 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2310 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2311 ++bp;
2312 }
2313 if (blob != NULL)
2314 rettv_blob_set(rettv, blob);
2315 *arg = bp;
2316 }
2317 else
2318 {
2319 varnumber_T n;
2320
2321 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002322 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002323 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002324 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002325 if (len == 0)
2326 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002327 if (evaluate)
2328 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002329 return FAIL;
2330 }
2331 *arg += len;
2332 if (evaluate)
2333 {
2334 rettv->v_type = VAR_NUMBER;
2335 rettv->vval.v_number = n;
2336 }
2337 }
2338 return OK;
2339}
2340
2341/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002342 * Evaluate a string constant and put the result in "rettv".
2343 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2344 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2345 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002346 * Return OK or FAIL.
2347 */
2348 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002349eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002350{
2351 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002352 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002353 int extra = interpolate ? 1 : 0;
2354 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002355 int len;
2356
2357 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002358 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002359 {
2360 if (*p == '\\' && p[1] != NUL)
2361 {
2362 ++p;
2363 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002364 // to 9 characters (6 for the char and 3 for a modifier):
2365 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002366 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002367 {
2368 int modifiers = 0;
2369 int flags = FSK_KEYCODE | FSK_IN_STRING;
2370
zeertzjqdb088872022-05-02 22:53:45 +01002371 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002372
2373 // Skip to the '>' to avoid using '{' inside for string
2374 // interpolation.
2375 if (p[1] != '*')
2376 flags |= FSK_SIMPLIFY;
2377 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2378 --p; // leave "p" on the ">"
2379 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002380 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002381 else if (interpolate && (*p == '{' || *p == '}'))
2382 {
2383 if (*p == '{' && p[1] != '{') // start of expression
2384 break;
2385 ++p;
2386 if (p[-1] == '}' && *p != '}') // single '}' is an error
2387 {
2388 semsg(_(e_stray_closing_curly_str), *arg);
2389 return FAIL;
2390 }
2391 --extra; // "{{" becomes "{", "}}" becomes "}"
2392 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002393 }
2394
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002395 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002396 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002397 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002398 return FAIL;
2399 }
2400
2401 // If only parsing, set *arg and return here
2402 if (!evaluate)
2403 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002404 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002405 return OK;
2406 }
2407
2408 // Copy the string into allocated memory, handling backslashed
2409 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002410 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002411 len = (int)(p - *arg + extra);
2412 rettv->vval.v_string = alloc(len);
2413 if (rettv->vval.v_string == NULL)
2414 return FAIL;
2415 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002416
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002417 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002418 {
2419 if (*p == '\\')
2420 {
2421 switch (*++p)
2422 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002423 case 'b': *end++ = BS; ++p; break;
2424 case 'e': *end++ = ESC; ++p; break;
2425 case 'f': *end++ = FF; ++p; break;
2426 case 'n': *end++ = NL; ++p; break;
2427 case 'r': *end++ = CAR; ++p; break;
2428 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002429
2430 case 'X': // hex: "\x1", "\x12"
2431 case 'x':
2432 case 'u': // Unicode: "\u0023"
2433 case 'U':
2434 if (vim_isxdigit(p[1]))
2435 {
2436 int n, nr;
2437 int c = toupper(*p);
2438
2439 if (c == 'X')
2440 n = 2;
2441 else if (*p == 'u')
2442 n = 4;
2443 else
2444 n = 8;
2445 nr = 0;
2446 while (--n >= 0 && vim_isxdigit(p[1]))
2447 {
2448 ++p;
2449 nr = (nr << 4) + hex2nr(*p);
2450 }
2451 ++p;
2452 // For "\u" store the number according to
2453 // 'encoding'.
2454 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002455 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002456 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002457 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002458 }
2459 break;
2460
2461 // octal: "\1", "\12", "\123"
2462 case '0':
2463 case '1':
2464 case '2':
2465 case '3':
2466 case '4':
2467 case '5':
2468 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002469 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002470 if (*p >= '0' && *p <= '7')
2471 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002472 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002473 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002474 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002475 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002476 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002477 break;
2478
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002479 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002480 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002481 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002482 int flags = FSK_KEYCODE | FSK_IN_STRING;
2483
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002484 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002485 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002486 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002487 if (extra != 0)
2488 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002489 end += extra;
2490 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002491 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002492 break;
2493 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002494 }
2495 // FALLTHROUGH
2496
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002497 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002498 break;
2499 }
2500 }
2501 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002502 {
2503 if (interpolate && (*p == '{' || *p == '}'))
2504 {
2505 if (*p == '{' && p[1] != '{') // start of expression
2506 break;
2507 ++p; // reduce "{{" to "{" and "}}" to "}"
2508 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002509 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002510 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002511 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002512 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002513 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002514 ++p;
2515 *arg = p;
2516
2517 return OK;
2518}
2519
2520/*
2521 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002522 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2523 * Return OK when a "rettv" was set to the string.
2524 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002525 */
2526 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002527eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002528{
2529 char_u *p;
2530 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002531 int reduce = interpolate ? -1 : 0;
2532 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002533
2534 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002535 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002536 {
2537 if (*p == '\'')
2538 {
2539 if (p[1] != '\'')
2540 break;
2541 ++reduce;
2542 ++p;
2543 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002544 else if (interpolate)
2545 {
2546 if (*p == '{')
2547 {
2548 if (p[1] != '{')
2549 break;
2550 ++p;
2551 ++reduce;
2552 }
2553 else if (*p == '}')
2554 {
2555 ++p;
2556 if (*p != '}')
2557 {
2558 semsg(_(e_stray_closing_curly_str), *arg);
2559 return FAIL;
2560 }
2561 ++reduce;
2562 }
2563 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002564 }
2565
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002566 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002567 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002568 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002569 return FAIL;
2570 }
2571
2572 // If only parsing return after setting "*arg"
2573 if (!evaluate)
2574 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002575 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002576 return OK;
2577 }
2578
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002579 // Copy the string into allocated memory, handling '' to ' reduction and
2580 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002581 str = alloc((p - *arg) - reduce);
2582 if (str == NULL)
2583 return FAIL;
2584 rettv->v_type = VAR_STRING;
2585 rettv->vval.v_string = str;
2586
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002587 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002588 {
2589 if (*p == '\'')
2590 {
2591 if (p[1] != '\'')
2592 break;
2593 ++p;
2594 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002595 else if (interpolate && (*p == '{' || *p == '}'))
2596 {
2597 if (*p == '{' && p[1] != '{')
2598 break;
2599 ++p;
2600 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002601 MB_COPY_CHAR(p, str);
2602 }
2603 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002604 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002605
2606 return OK;
2607}
2608
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002609/*
2610 * Evaluate a single or double quoted string possibly containing expressions.
2611 * "arg" points to the '$'. The result is put in "rettv".
2612 * Returns OK or FAIL.
2613 */
LemonBoy2eaef102022-05-06 13:14:50 +01002614 int
2615eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2616{
2617 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002618 int ret = OK;
2619 int quote;
2620 garray_T ga;
2621 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002622
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002623 ga_init2(&ga, 1, 80);
2624
2625 // *arg is on the '$' character, move it to the first string character.
2626 ++*arg;
2627 quote = **arg;
2628 ++*arg;
2629
2630 for (;;)
2631 {
2632 // Get the string up to the matching quote or to a single '{'.
2633 // "arg" is advanced to either the quote or the '{'.
2634 if (quote == '"')
2635 ret = eval_string(arg, &tv, evaluate, TRUE);
2636 else
2637 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2638 if (ret == FAIL)
2639 break;
2640 if (evaluate)
2641 {
2642 ga_concat(&ga, tv.vval.v_string);
2643 clear_tv(&tv);
2644 }
2645
2646 if (**arg != '{')
2647 {
2648 // found terminating quote
2649 ++*arg;
2650 break;
2651 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002652 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002653 if (p == NULL)
2654 {
2655 ret = FAIL;
2656 break;
2657 }
2658 *arg = p;
2659 }
LemonBoy2eaef102022-05-06 13:14:50 +01002660
2661 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002662 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2663 {
2664 ga_clear(&ga);
2665 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002666 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002667 }
LemonBoy2eaef102022-05-06 13:14:50 +01002668
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002669 rettv->vval.v_string = ga.ga_data;
2670 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002671}
2672
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002673/*
2674 * Return a string with the string representation of a variable.
2675 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2676 * "numbuf" is used for a number.
2677 * Puts quotes around strings, so that they can be parsed back by eval().
2678 * May return NULL.
2679 */
2680 char_u *
2681tv2string(
2682 typval_T *tv,
2683 char_u **tofree,
2684 char_u *numbuf,
2685 int copyID)
2686{
2687 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2688}
2689
2690/*
2691 * Get the value of an environment variable.
2692 * "arg" is pointing to the '$'. It is advanced to after the name.
2693 * If the environment variable was not set, silently assume it is empty.
2694 * Return FAIL if the name is invalid.
2695 */
2696 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002697eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002698{
2699 char_u *string = NULL;
2700 int len;
2701 int cc;
2702 char_u *name;
2703 int mustfree = FALSE;
2704
2705 ++*arg;
2706 name = *arg;
2707 len = get_env_len(arg);
2708 if (evaluate)
2709 {
2710 if (len == 0)
2711 return FAIL; // invalid empty name
2712
2713 cc = name[len];
2714 name[len] = NUL;
2715 // first try vim_getenv(), fast for normal environment vars
2716 string = vim_getenv(name, &mustfree);
2717 if (string != NULL && *string != NUL)
2718 {
2719 if (!mustfree)
2720 string = vim_strsave(string);
2721 }
2722 else
2723 {
2724 if (mustfree)
2725 vim_free(string);
2726
2727 // next try expanding things like $VIM and ${HOME}
2728 string = expand_env_save(name - 1);
2729 if (string != NULL && *string == '$')
2730 VIM_CLEAR(string);
2731 }
2732 name[len] = cc;
2733
2734 rettv->v_type = VAR_STRING;
2735 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002736 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002737 }
2738
2739 return OK;
2740}
2741
2742/*
2743 * Get the lnum from the first argument.
2744 * Also accepts ".", "$", etc., but that only works for the current buffer.
2745 * Returns -1 on error.
2746 */
2747 linenr_T
2748tv_get_lnum(typval_T *argvars)
2749{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002750 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002751 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002752
Bram Moolenaar56acb092020-08-16 14:48:19 +02002753 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2754 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002755 if (lnum <= 0 && did_emsg_before == did_emsg
2756 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002757 {
2758 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002759 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002760
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002761 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002762 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002763 if (fp != NULL)
2764 lnum = fp->lnum;
2765 }
2766 return lnum;
2767}
2768
2769/*
2770 * Get the lnum from the first argument.
2771 * Also accepts "$", then "buf" is used.
2772 * Returns 0 on error.
2773 */
2774 linenr_T
2775tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2776{
2777 if (argvars[0].v_type == VAR_STRING
2778 && argvars[0].vval.v_string != NULL
2779 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002780 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002781 && buf != NULL)
2782 return buf->b_ml.ml_line_count;
2783 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2784}
2785
2786/*
2787 * Get buffer by number or pattern.
2788 */
2789 buf_T *
2790tv_get_buf(typval_T *tv, int curtab_only)
2791{
2792 char_u *name = tv->vval.v_string;
2793 buf_T *buf;
2794
2795 if (tv->v_type == VAR_NUMBER)
2796 return buflist_findnr((int)tv->vval.v_number);
2797 if (tv->v_type != VAR_STRING)
2798 return NULL;
2799 if (name == NULL || *name == NUL)
2800 return curbuf;
2801 if (name[0] == '$' && name[1] == NUL)
2802 return lastbuf;
2803
2804 buf = buflist_find_by_name(name, curtab_only);
2805
2806 // If not found, try expanding the name, like done for bufexists().
2807 if (buf == NULL)
2808 buf = find_buffer(tv);
2809
2810 return buf;
2811}
2812
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002813/*
2814 * Like tv_get_buf() but give an error message is the type is wrong.
2815 */
2816 buf_T *
2817tv_get_buf_from_arg(typval_T *tv)
2818{
2819 buf_T *buf;
2820
2821 ++emsg_off;
2822 buf = tv_get_buf(tv, FALSE);
2823 --emsg_off;
2824 if (buf == NULL
2825 && tv->v_type != VAR_NUMBER
2826 && tv->v_type != VAR_STRING)
2827 // issue errmsg for type error
2828 (void)tv_get_number(tv);
2829 return buf;
2830}
2831
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002832#endif // FEAT_EVAL