blob: da5d7affb7b03bb1477e215b9df72c9215ef5b72 [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:
274 semsg(_(e_using_typealias_as_variable),
275 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:
395 semsg(_(e_using_typealias_as_variable),
396 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/*
1007 * Give an error and return FAIL unless "args[idx]" is a class or a list.
1008 */
1009 int
1010check_for_class_or_list_arg(typval_T *args, int idx)
1011{
1012 if (args[idx].v_type != VAR_CLASS && args[idx].v_type != VAR_LIST)
1013 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001014 semsg(_(e_list_or_class_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001015 return FAIL;
1016 }
1017 return OK;
1018}
1019
1020/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001021 * Get the string value of a variable.
1022 * If it is a Number variable, the number is converted into a string.
1023 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1024 * tv_get_string_buf() uses a given buffer.
1025 * If the String variable has never been set, return an empty string.
1026 * Never returns NULL;
1027 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1028 * NULL on error.
1029 */
1030 char_u *
1031tv_get_string(typval_T *varp)
1032{
1033 static char_u mybuf[NUMBUFLEN];
1034
1035 return tv_get_string_buf(varp, mybuf);
1036}
1037
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001038/*
1039 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1040 */
1041 char_u *
1042tv_get_string_strict(typval_T *varp)
1043{
1044 static char_u mybuf[NUMBUFLEN];
1045 char_u *res = tv_get_string_buf_chk_strict(
1046 varp, mybuf, in_vim9script());
1047
1048 return res != NULL ? res : (char_u *)"";
1049}
1050
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001051 char_u *
1052tv_get_string_buf(typval_T *varp, char_u *buf)
1053{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001054 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001055
1056 return res != NULL ? res : (char_u *)"";
1057}
1058
1059/*
1060 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1061 */
1062 char_u *
1063tv_get_string_chk(typval_T *varp)
1064{
1065 static char_u mybuf[NUMBUFLEN];
1066
1067 return tv_get_string_buf_chk(varp, mybuf);
1068}
1069
1070 char_u *
1071tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1072{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001073 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1074}
1075
1076 char_u *
1077tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1078{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001079 switch (varp->v_type)
1080 {
1081 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001082 if (strict)
1083 {
1084 emsg(_(e_using_number_as_string));
1085 break;
1086 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001087 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1088 (varnumber_T)varp->vval.v_number);
1089 return buf;
1090 case VAR_FUNC:
1091 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001092 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001093 break;
1094 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001095 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001096 break;
1097 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001098 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001099 break;
1100 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001101 if (strict)
1102 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001103 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001104 break;
1105 }
1106 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1107 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001108 case VAR_STRING:
1109 if (varp->vval.v_string != NULL)
1110 return varp->vval.v_string;
1111 return (char_u *)"";
1112 case VAR_BOOL:
1113 case VAR_SPECIAL:
1114 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1115 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001116 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001117 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001118 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001119 case VAR_CLASS:
1120 emsg(_(e_using_class_as_string));
1121 break;
1122 case VAR_OBJECT:
1123 emsg(_(e_using_object_as_string));
1124 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001125 case VAR_JOB:
1126#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001127 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001128 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001129 semsg(_(e_using_invalid_value_as_string_str), "job");
1130 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001131 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001132 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001133#endif
1134 break;
1135 case VAR_CHANNEL:
1136#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001137 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001138 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001139 semsg(_(e_using_invalid_value_as_string_str), "channel");
1140 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001141 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001142 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001143#endif
1144 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001145 case VAR_VOID:
1146 emsg(_(e_cannot_use_void_value));
1147 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001148 case VAR_TYPEALIAS:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001149 case VAR_UNKNOWN:
1150 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001151 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001152 semsg(_(e_using_invalid_value_as_string_str),
1153 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001154 break;
1155 }
1156 return NULL;
1157}
1158
1159/*
1160 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1161 * string() on Dict, List, etc.
1162 */
1163 char_u *
1164tv_stringify(typval_T *varp, char_u *buf)
1165{
1166 if (varp->v_type == VAR_LIST
1167 || varp->v_type == VAR_DICT
1168 || varp->v_type == VAR_BLOB
1169 || varp->v_type == VAR_FUNC
1170 || varp->v_type == VAR_PARTIAL
1171 || varp->v_type == VAR_FLOAT)
1172 {
1173 typval_T tmp;
1174
1175 f_string(varp, &tmp);
1176 tv_get_string_buf(&tmp, buf);
1177 clear_tv(varp);
1178 *varp = tmp;
1179 return tmp.vval.v_string;
1180 }
1181 return tv_get_string_buf(varp, buf);
1182}
1183
1184/*
1185 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1186 * Also give an error message, using "name" or _("name") when use_gettext is
1187 * TRUE.
1188 */
1189 int
1190tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1191{
1192 int lock = 0;
1193
1194 switch (tv->v_type)
1195 {
1196 case VAR_BLOB:
1197 if (tv->vval.v_blob != NULL)
1198 lock = tv->vval.v_blob->bv_lock;
1199 break;
1200 case VAR_LIST:
1201 if (tv->vval.v_list != NULL)
1202 lock = tv->vval.v_list->lv_lock;
1203 break;
1204 case VAR_DICT:
1205 if (tv->vval.v_dict != NULL)
1206 lock = tv->vval.v_dict->dv_lock;
1207 break;
1208 default:
1209 break;
1210 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001211 return value_check_lock(tv->v_lock, name, use_gettext)
1212 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001213}
1214
1215/*
1216 * Copy the values from typval_T "from" to typval_T "to".
1217 * When needed allocates string or increases reference count.
1218 * Does not make a copy of a list, blob or dict but copies the reference!
1219 * It is OK for "from" and "to" to point to the same item. This is used to
1220 * make a copy later.
1221 */
1222 void
1223copy_tv(typval_T *from, typval_T *to)
1224{
1225 to->v_type = from->v_type;
1226 to->v_lock = 0;
1227 switch (from->v_type)
1228 {
1229 case VAR_NUMBER:
1230 case VAR_BOOL:
1231 case VAR_SPECIAL:
1232 to->vval.v_number = from->vval.v_number;
1233 break;
1234 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001235 to->vval.v_float = from->vval.v_float;
1236 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001237 case VAR_JOB:
1238#ifdef FEAT_JOB_CHANNEL
1239 to->vval.v_job = from->vval.v_job;
1240 if (to->vval.v_job != NULL)
1241 ++to->vval.v_job->jv_refcount;
1242 break;
1243#endif
1244 case VAR_CHANNEL:
1245#ifdef FEAT_JOB_CHANNEL
1246 to->vval.v_channel = from->vval.v_channel;
1247 if (to->vval.v_channel != NULL)
1248 ++to->vval.v_channel->ch_refcount;
1249 break;
1250#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001251 case VAR_INSTR:
1252 to->vval.v_instr = from->vval.v_instr;
1253 break;
1254
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001255 case VAR_CLASS:
1256 copy_class(from, to);
1257 break;
1258
1259 case VAR_OBJECT:
1260 copy_object(from, to);
1261 break;
1262
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001263 case VAR_STRING:
1264 case VAR_FUNC:
1265 if (from->vval.v_string == NULL)
1266 to->vval.v_string = NULL;
1267 else
1268 {
1269 to->vval.v_string = vim_strsave(from->vval.v_string);
1270 if (from->v_type == VAR_FUNC)
1271 func_ref(to->vval.v_string);
1272 }
1273 break;
1274 case VAR_PARTIAL:
1275 if (from->vval.v_partial == NULL)
1276 to->vval.v_partial = NULL;
1277 else
1278 {
1279 to->vval.v_partial = from->vval.v_partial;
1280 ++to->vval.v_partial->pt_refcount;
1281 }
1282 break;
1283 case VAR_BLOB:
1284 if (from->vval.v_blob == NULL)
1285 to->vval.v_blob = NULL;
1286 else
1287 {
1288 to->vval.v_blob = from->vval.v_blob;
1289 ++to->vval.v_blob->bv_refcount;
1290 }
1291 break;
1292 case VAR_LIST:
1293 if (from->vval.v_list == NULL)
1294 to->vval.v_list = NULL;
1295 else
1296 {
1297 to->vval.v_list = from->vval.v_list;
1298 ++to->vval.v_list->lv_refcount;
1299 }
1300 break;
1301 case VAR_DICT:
1302 if (from->vval.v_dict == NULL)
1303 to->vval.v_dict = NULL;
1304 else
1305 {
1306 to->vval.v_dict = from->vval.v_dict;
1307 ++to->vval.v_dict->dv_refcount;
1308 }
1309 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001310 case VAR_TYPEALIAS:
1311 if (from->vval.v_typealias == NULL)
1312 to->vval.v_typealias = NULL;
1313 else
1314 {
1315 to->vval.v_typealias = from->vval.v_typealias;
1316 ++to->vval.v_typealias->ta_refcount;
1317 }
1318 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001319 case VAR_VOID:
1320 emsg(_(e_cannot_use_void_value));
1321 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001322 case VAR_UNKNOWN:
1323 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001324 internal_error_no_abort("copy_tv(UNKNOWN)");
1325 break;
1326 }
1327}
1328
1329/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001330 * Compare "tv1" and "tv2".
1331 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001332 */
1333 int
1334typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001335 typval_T *tv1, // first operand
1336 typval_T *tv2, // second operand
1337 exprtype_T type, // operator
1338 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001339{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001340 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001341 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001342 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1343
Bram Moolenaar265f8112021-12-19 12:33:05 +00001344 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001345 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001346 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001347 // it means TRUE.
1348 n1 = (type == EXPR_ISNOT);
1349 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001350 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1351 || (tv2->v_type == VAR_SPECIAL
1352 && tv2->vval.v_number == VVAL_NULL))
1353 && tv1->v_type != tv2->v_type
1354 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1355 {
1356 n1 = typval_compare_null(tv1, tv2);
1357 if (n1 == MAYBE)
1358 {
1359 clear_tv(tv1);
1360 return FAIL;
1361 }
1362 if (type == EXPR_NEQUAL)
1363 n1 = !n1;
1364 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001365 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001366 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001367 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001368 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001369 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001370 return FAIL;
1371 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001372 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001373 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001374 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001375 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001376 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001377 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001378 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001379 return FAIL;
1380 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001381 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001382 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001383 else if (tv1->v_type == VAR_CLASS || tv2->v_type == VAR_CLASS)
1384 {
1385 if (typval_compare_class(tv1, tv2, type, ic, &res) == FAIL)
1386 {
1387 clear_tv(tv1);
1388 return FAIL;
1389 }
1390 n1 = res;
1391 }
1392 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1393 {
1394 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1395 {
1396 clear_tv(tv1);
1397 return FAIL;
1398 }
1399 n1 = res;
1400 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001401 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001402 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001403 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001404 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001405 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001406 return FAIL;
1407 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001408 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001409 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001410 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1411 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001412 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001413 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001414 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001415 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001416 return FAIL;
1417 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001418 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001419 }
1420
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001421 // If one of the two variables is a float, compare as a float.
1422 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001423 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001424 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1425 {
1426 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001427 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001428
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001429 f1 = tv_get_float_chk(tv1, &error);
1430 if (!error)
1431 f2 = tv_get_float_chk(tv2, &error);
1432 if (error)
1433 {
1434 clear_tv(tv1);
1435 return FAIL;
1436 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001437 n1 = FALSE;
1438 switch (type)
1439 {
1440 case EXPR_IS:
1441 case EXPR_EQUAL: n1 = (f1 == f2); break;
1442 case EXPR_ISNOT:
1443 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1444 case EXPR_GREATER: n1 = (f1 > f2); break;
1445 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1446 case EXPR_SMALLER: n1 = (f1 < f2); break;
1447 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1448 case EXPR_UNKNOWN:
1449 case EXPR_MATCH:
1450 default: break; // avoid gcc warning
1451 }
1452 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001453
1454 // If one of the two variables is a number, compare as a number.
1455 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001456 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001457 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1458 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001459 int error = FALSE;
1460
1461 n1 = tv_get_number_chk(tv1, &error);
1462 if (!error)
1463 n2 = tv_get_number_chk(tv2, &error);
1464 if (error)
1465 {
1466 clear_tv(tv1);
1467 return FAIL;
1468 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001469 switch (type)
1470 {
1471 case EXPR_IS:
1472 case EXPR_EQUAL: n1 = (n1 == n2); break;
1473 case EXPR_ISNOT:
1474 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1475 case EXPR_GREATER: n1 = (n1 > n2); break;
1476 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1477 case EXPR_SMALLER: n1 = (n1 < n2); break;
1478 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1479 case EXPR_UNKNOWN:
1480 case EXPR_MATCH:
1481 default: break; // avoid gcc warning
1482 }
1483 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001484 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1485 || tv2->v_type == VAR_BOOL
1486 || (tv1->v_type == VAR_SPECIAL
1487 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001488 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001489 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001490 {
1491 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001492 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1493 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001494 return FAIL;
1495 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001496 n1 = tv1->vval.v_number;
1497 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001498 switch (type)
1499 {
1500 case EXPR_IS:
1501 case EXPR_EQUAL: n1 = (n1 == n2); break;
1502 case EXPR_ISNOT:
1503 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1504 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001505 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001506 vartype_name(tv1->v_type));
1507 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001508 return FAIL;
1509 }
1510 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001511#ifdef FEAT_JOB_CHANNEL
1512 else if (tv1->v_type == tv2->v_type
1513 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1514 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1515 {
1516 if (tv1->v_type == VAR_CHANNEL)
1517 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1518 else
1519 n1 = tv1->vval.v_job == tv2->vval.v_job;
1520 if (type == EXPR_NEQUAL)
1521 n1 = !n1;
1522 }
1523#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001524 else
1525 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001526 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001527 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001528 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001529 return FAIL;
1530 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001531 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001532 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001533 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001534 if (in_vim9script())
1535 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001536 tv1->v_type = VAR_BOOL;
1537 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001538 }
1539 else
1540 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001541 tv1->v_type = VAR_NUMBER;
1542 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001543 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001544
1545 return OK;
1546}
1547
Bram Moolenaar34453202021-01-31 13:08:38 +01001548/*
dundargocc57b5bc2022-11-02 13:30:51 +00001549 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001550 * Put the result, false or true, in "res".
1551 * Return FAIL and give an error message when the comparison can't be done.
1552 */
1553 int
1554typval_compare_list(
1555 typval_T *tv1,
1556 typval_T *tv2,
1557 exprtype_T type,
1558 int ic,
1559 int *res)
1560{
1561 int val = 0;
1562
1563 if (type == EXPR_IS || type == EXPR_ISNOT)
1564 {
1565 val = (tv1->v_type == tv2->v_type
1566 && tv1->vval.v_list == tv2->vval.v_list);
1567 if (type == EXPR_ISNOT)
1568 val = !val;
1569 }
1570 else if (tv1->v_type != tv2->v_type
1571 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1572 {
1573 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001574 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001575 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001576 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001577 return FAIL;
1578 }
1579 else
1580 {
1581 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1582 ic, FALSE);
1583 if (type == EXPR_NEQUAL)
1584 val = !val;
1585 }
1586 *res = val;
1587 return OK;
1588}
1589
1590/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001591 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001592 */
1593 int
1594typval_compare_null(typval_T *tv1, typval_T *tv2)
1595{
1596 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1597 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1598 {
1599 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1600
1601 switch (tv->v_type)
1602 {
1603 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001604#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001605 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001606#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001607 // TODO: null_class handling
1608 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001609 case VAR_DICT: return tv->vval.v_dict == NULL;
1610 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001611#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001612 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001613#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001614 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001615 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001616 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1617 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001618
1619 case VAR_NUMBER: if (!in_vim9script())
1620 return tv->vval.v_number == 0;
1621 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001622 case VAR_FLOAT: if (!in_vim9script())
1623 return tv->vval.v_float == 0.0;
1624 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001625 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001626 default: break;
1627 }
1628 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001629 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001630 // we won't give an error
1631 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001632}
1633
1634/*
dundargocc57b5bc2022-11-02 13:30:51 +00001635 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001636 * Put the result, false or true, in "res".
1637 * Return FAIL and give an error message when the comparison can't be done.
1638 */
1639 int
1640typval_compare_blob(
1641 typval_T *tv1,
1642 typval_T *tv2,
1643 exprtype_T type,
1644 int *res)
1645{
1646 int val = 0;
1647
1648 if (type == EXPR_IS || type == EXPR_ISNOT)
1649 {
1650 val = (tv1->v_type == tv2->v_type
1651 && tv1->vval.v_blob == tv2->vval.v_blob);
1652 if (type == EXPR_ISNOT)
1653 val = !val;
1654 }
1655 else if (tv1->v_type != tv2->v_type
1656 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1657 {
1658 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001659 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001660 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001661 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001662 return FAIL;
1663 }
1664 else
1665 {
1666 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1667 if (type == EXPR_NEQUAL)
1668 val = !val;
1669 }
1670 *res = val;
1671 return OK;
1672}
1673
1674/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001675 * Compare "tv1" to "tv2" as classes according to "type".
1676 * Put the result, false or true, in "res".
1677 * Return FAIL and give an error message when the comparison can't be done.
1678 */
1679 int
1680typval_compare_class(
1681 typval_T *tv1,
1682 typval_T *tv2,
1683 exprtype_T type UNUSED,
1684 int ic UNUSED,
1685 int *res)
1686{
1687 // TODO: use "type"
1688 *res = tv1->vval.v_class == tv2->vval.v_class;
1689 return OK;
1690}
1691
1692/*
1693 * Compare "tv1" to "tv2" as objects according to "type".
1694 * Put the result, false or true, in "res".
1695 * Return FAIL and give an error message when the comparison can't be done.
1696 */
1697 int
1698typval_compare_object(
1699 typval_T *tv1,
1700 typval_T *tv2,
1701 exprtype_T type,
1702 int ic,
1703 int *res)
1704{
1705 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1706
1707 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1708 {
1709 *res = res_match;
1710 return OK;
1711 }
1712 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1713 {
1714 *res = !res_match;
1715 return OK;
1716 }
1717
1718 class_T *cl1 = tv1->vval.v_object->obj_class;
1719 class_T *cl2 = tv2->vval.v_object->obj_class;
1720 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1721 {
1722 *res = !res_match;
1723 return OK;
1724 }
1725
1726 object_T *obj1 = tv1->vval.v_object;
1727 object_T *obj2 = tv2->vval.v_object;
1728 if (type == EXPR_IS || type == EXPR_ISNOT)
1729 {
1730 *res = obj1 == obj2 ? res_match : !res_match;
1731 return OK;
1732 }
1733
1734 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1735 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1736 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1737 {
1738 *res = !res_match;
1739 return OK;
1740 }
1741 *res = res_match;
1742 return OK;
1743}
1744
1745/*
dundargocc57b5bc2022-11-02 13:30:51 +00001746 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001747 * Put the result, false or true, in "res".
1748 * Return FAIL and give an error message when the comparison can't be done.
1749 */
1750 int
1751typval_compare_dict(
1752 typval_T *tv1,
1753 typval_T *tv2,
1754 exprtype_T type,
1755 int ic,
1756 int *res)
1757{
1758 int val;
1759
1760 if (type == EXPR_IS || type == EXPR_ISNOT)
1761 {
1762 val = (tv1->v_type == tv2->v_type
1763 && tv1->vval.v_dict == tv2->vval.v_dict);
1764 if (type == EXPR_ISNOT)
1765 val = !val;
1766 }
1767 else if (tv1->v_type != tv2->v_type
1768 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1769 {
1770 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001771 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001772 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001773 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001774 return FAIL;
1775 }
1776 else
1777 {
1778 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1779 if (type == EXPR_NEQUAL)
1780 val = !val;
1781 }
1782 *res = val;
1783 return OK;
1784}
1785
1786/*
dundargocc57b5bc2022-11-02 13:30:51 +00001787 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001788 * Put the result, false or true, in "res".
1789 * Return FAIL and give an error message when the comparison can't be done.
1790 */
1791 int
1792typval_compare_func(
1793 typval_T *tv1,
1794 typval_T *tv2,
1795 exprtype_T type,
1796 int ic,
1797 int *res)
1798{
1799 int val = 0;
1800
1801 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1802 && type != EXPR_IS && type != EXPR_ISNOT)
1803 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001804 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001805 return FAIL;
1806 }
1807 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1808 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1809 // When both partials are NULL, then they are equal.
1810 // Otherwise they are not equal.
1811 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1812 else if (type == EXPR_IS || type == EXPR_ISNOT)
1813 {
1814 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1815 // strings are considered the same if their value is
1816 // the same
1817 val = tv_equal(tv1, tv2, ic, FALSE);
1818 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1819 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1820 else
1821 val = FALSE;
1822 }
1823 else
1824 val = tv_equal(tv1, tv2, ic, FALSE);
1825 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1826 val = !val;
1827 *res = val;
1828 return OK;
1829}
1830
1831/*
1832 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1833 * Put the result, false or true, in "res".
1834 * Return FAIL and give an error message when the comparison can't be done.
1835 */
1836 int
1837typval_compare_string(
1838 typval_T *tv1,
1839 typval_T *tv2,
1840 exprtype_T type,
1841 int ic,
1842 int *res)
1843{
1844 int i = 0;
1845 int val = FALSE;
1846 char_u *s1, *s2;
1847 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1848
1849 if (in_vim9script()
1850 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1851 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1852 {
1853 semsg(_(e_cannot_compare_str_with_str),
1854 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1855 return FAIL;
1856 }
1857 s1 = tv_get_string_buf(tv1, buf1);
1858 s2 = tv_get_string_buf(tv2, buf2);
1859 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1860 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1861 switch (type)
1862 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001863 case EXPR_IS: if (in_vim9script())
1864 {
1865 // Really check it is the same string, not just
1866 // the same value.
1867 val = tv1->vval.v_string == tv2->vval.v_string;
1868 break;
1869 }
1870 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001871 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001872 case EXPR_ISNOT: if (in_vim9script())
1873 {
1874 // Really check it is not the same string, not
1875 // just a different value.
1876 val = tv1->vval.v_string != tv2->vval.v_string;
1877 break;
1878 }
1879 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001880 case EXPR_NEQUAL: val = (i != 0); break;
1881 case EXPR_GREATER: val = (i > 0); break;
1882 case EXPR_GEQUAL: val = (i >= 0); break;
1883 case EXPR_SMALLER: val = (i < 0); break;
1884 case EXPR_SEQUAL: val = (i <= 0); break;
1885
1886 case EXPR_MATCH:
1887 case EXPR_NOMATCH:
1888 val = pattern_match(s2, s1, ic);
1889 if (type == EXPR_NOMATCH)
1890 val = !val;
1891 break;
1892
1893 default: break; // avoid gcc warning
1894 }
1895 *res = val;
1896 return OK;
1897}
1898/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001899 * Convert any type to a string, never give an error.
1900 * When "quotes" is TRUE add quotes to a string.
1901 * Returns an allocated string.
1902 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001903 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001904typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001905{
1906 char_u *tofree;
1907 char_u numbuf[NUMBUFLEN];
1908 char_u *ret = NULL;
1909
1910 if (arg == NULL)
1911 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001912 if (!quotes && arg->v_type == VAR_STRING)
1913 {
1914 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1915 : arg->vval.v_string);
1916 }
1917 else
1918 {
1919 ret = tv2string(arg, &tofree, numbuf, 0);
1920 // Make a copy if we have a value but it's not in allocated memory.
1921 if (ret != NULL && tofree == NULL)
1922 ret = vim_strsave(ret);
1923 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001924 return ret;
1925}
1926
1927/*
1928 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1929 * or it refers to a List or Dictionary that is locked.
1930 */
1931 int
1932tv_islocked(typval_T *tv)
1933{
1934 return (tv->v_lock & VAR_LOCKED)
1935 || (tv->v_type == VAR_LIST
1936 && tv->vval.v_list != NULL
1937 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1938 || (tv->v_type == VAR_DICT
1939 && tv->vval.v_dict != NULL
1940 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1941}
1942
1943 static int
1944func_equal(
1945 typval_T *tv1,
1946 typval_T *tv2,
1947 int ic) // ignore case
1948{
1949 char_u *s1, *s2;
1950 dict_T *d1, *d2;
1951 int a1, a2;
1952 int i;
1953
1954 // empty and NULL function name considered the same
1955 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1956 : partial_name(tv1->vval.v_partial);
1957 if (s1 != NULL && *s1 == NUL)
1958 s1 = NULL;
1959 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1960 : partial_name(tv2->vval.v_partial);
1961 if (s2 != NULL && *s2 == NUL)
1962 s2 = NULL;
1963 if (s1 == NULL || s2 == NULL)
1964 {
1965 if (s1 != s2)
1966 return FALSE;
1967 }
1968 else if (STRCMP(s1, s2) != 0)
1969 return FALSE;
1970
1971 // empty dict and NULL dict is different
1972 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1973 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1974 if (d1 == NULL || d2 == NULL)
1975 {
1976 if (d1 != d2)
1977 return FALSE;
1978 }
1979 else if (!dict_equal(d1, d2, ic, TRUE))
1980 return FALSE;
1981
1982 // empty list and no list considered the same
1983 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1984 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1985 if (a1 != a2)
1986 return FALSE;
1987 for (i = 0; i < a1; ++i)
1988 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1989 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1990 return FALSE;
1991
1992 return TRUE;
1993}
1994
1995/*
1996 * Return TRUE if "tv1" and "tv2" have the same value.
1997 * Compares the items just like "==" would compare them, but strings and
1998 * numbers are different. Floats and numbers are also different.
1999 */
2000 int
2001tv_equal(
2002 typval_T *tv1,
2003 typval_T *tv2,
2004 int ic, // ignore case
2005 int recursive) // TRUE when used recursively
2006{
2007 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2008 char_u *s1, *s2;
2009 static int recursive_cnt = 0; // catch recursive loops
2010 int r;
2011 static int tv_equal_recurse_limit;
2012
2013 // Catch lists and dicts that have an endless loop by limiting
2014 // recursiveness to a limit. We guess they are equal then.
2015 // A fixed limit has the problem of still taking an awful long time.
2016 // Reduce the limit every time running into it. That should work fine for
2017 // deeply linked structures that are not recursively linked and catch
2018 // recursiveness quickly.
2019 if (!recursive)
2020 tv_equal_recurse_limit = 1000;
2021 if (recursive_cnt >= tv_equal_recurse_limit)
2022 {
2023 --tv_equal_recurse_limit;
2024 return TRUE;
2025 }
2026
2027 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2028 // arguments.
2029 if ((tv1->v_type == VAR_FUNC
2030 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2031 && (tv2->v_type == VAR_FUNC
2032 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2033 {
2034 ++recursive_cnt;
2035 r = func_equal(tv1, tv2, ic);
2036 --recursive_cnt;
2037 return r;
2038 }
2039
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002040 if (tv1->v_type != tv2->v_type
2041 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2042 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002043 return FALSE;
2044
2045 switch (tv1->v_type)
2046 {
2047 case VAR_LIST:
2048 ++recursive_cnt;
2049 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2050 --recursive_cnt;
2051 return r;
2052
2053 case VAR_DICT:
2054 ++recursive_cnt;
2055 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2056 --recursive_cnt;
2057 return r;
2058
2059 case VAR_BLOB:
2060 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2061
2062 case VAR_NUMBER:
2063 case VAR_BOOL:
2064 case VAR_SPECIAL:
2065 return tv1->vval.v_number == tv2->vval.v_number;
2066
2067 case VAR_STRING:
2068 s1 = tv_get_string_buf(tv1, buf1);
2069 s2 = tv_get_string_buf(tv2, buf2);
2070 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2071
2072 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002073 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002074 case VAR_JOB:
2075#ifdef FEAT_JOB_CHANNEL
2076 return tv1->vval.v_job == tv2->vval.v_job;
2077#endif
2078 case VAR_CHANNEL:
2079#ifdef FEAT_JOB_CHANNEL
2080 return tv1->vval.v_channel == tv2->vval.v_channel;
2081#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002082 case VAR_INSTR:
2083 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002084
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002085 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002086 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002087 return tv1->vval.v_class == tv2->vval.v_class;
2088
2089 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002090 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2091 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002092
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002093 case VAR_PARTIAL:
2094 return tv1->vval.v_partial == tv2->vval.v_partial;
2095
2096 case VAR_FUNC:
2097 return tv1->vval.v_string == tv2->vval.v_string;
2098
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002099 case VAR_TYPEALIAS:
2100 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2101
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002102 case VAR_UNKNOWN:
2103 case VAR_ANY:
2104 case VAR_VOID:
2105 break;
2106 }
2107
2108 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2109 // does not equal anything, not even itself.
2110 return FALSE;
2111}
2112
2113/*
2114 * Get an option value.
2115 * "arg" points to the '&' or '+' before the option name.
2116 * "arg" is advanced to character after the option name.
2117 * Return OK or FAIL.
2118 */
2119 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002120eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002121 char_u **arg,
2122 typval_T *rettv, // when NULL, only check if option exists
2123 int evaluate)
2124{
2125 char_u *option_end;
2126 long numval;
2127 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002128 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002129 int c;
2130 int working = (**arg == '+'); // has("+option")
2131 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002132 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002133
2134 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002135 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002136 if (option_end == NULL)
2137 {
2138 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002139 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002140 return FAIL;
2141 }
2142
2143 if (!evaluate)
2144 {
2145 *arg = option_end;
2146 return OK;
2147 }
2148
2149 c = *option_end;
2150 *option_end = NUL;
2151 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002152 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002153
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002154 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002155 {
2156 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002157 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002158 ret = FAIL;
2159 }
2160 else if (rettv != NULL)
2161 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002162 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002163 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002164 {
2165 rettv->v_type = VAR_STRING;
2166 rettv->vval.v_string = NULL;
2167 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002168 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002169 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002170 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2171 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002172 rettv->vval.v_number = 0;
2173 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002174 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002175 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002176 if (in_vim9script() && opt_type == gov_bool)
2177 {
2178 rettv->v_type = VAR_BOOL;
2179 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2180 }
2181 else
2182 {
2183 rettv->v_type = VAR_NUMBER;
2184 rettv->vval.v_number = numval;
2185 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002186 }
2187 else // string option
2188 {
2189 rettv->v_type = VAR_STRING;
2190 rettv->vval.v_string = stringval;
2191 }
2192 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002193 else if (working && (opt_type == gov_hidden_bool
2194 || opt_type == gov_hidden_number
2195 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002196 ret = FAIL;
2197
2198 *option_end = c; // put back for error messages
2199 *arg = option_end;
2200
2201 return ret;
2202}
2203
2204/*
2205 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2206 * Return OK or FAIL.
2207 */
2208 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002209eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002210 char_u **arg,
2211 typval_T *rettv,
2212 int evaluate,
2213 int want_string UNUSED)
2214{
2215 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002216 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002217 char_u *p;
2218 int get_float = FALSE;
2219
2220 // We accept a float when the format matches
2221 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2222 // strict to avoid backwards compatibility problems.
2223 // With script version 2 and later the leading digit can be
2224 // omitted.
2225 // Don't look for a float after the "." operator, so that
2226 // ":let vers = 1.2.3" doesn't fail.
2227 if (**arg == '.')
2228 p = *arg;
2229 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002230 {
2231 p = *arg + 1;
2232 if (skip_quotes)
2233 for (;;)
2234 {
2235 if (*p == '\'')
2236 ++p;
2237 if (!vim_isdigit(*p))
2238 break;
2239 p = skipdigits(p);
2240 }
2241 else
2242 p = skipdigits(p);
2243 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002244 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2245 {
2246 get_float = TRUE;
2247 p = skipdigits(p + 2);
2248 if (*p == 'e' || *p == 'E')
2249 {
2250 ++p;
2251 if (*p == '-' || *p == '+')
2252 ++p;
2253 if (!vim_isdigit(*p))
2254 get_float = FALSE;
2255 else
2256 p = skipdigits(p + 1);
2257 }
2258 if (ASCII_ISALPHA(*p) || *p == '.')
2259 get_float = FALSE;
2260 }
2261 if (get_float)
2262 {
2263 float_T f;
2264
Bram Moolenaar29500652021-08-08 15:43:34 +02002265 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002266 if (evaluate)
2267 {
2268 rettv->v_type = VAR_FLOAT;
2269 rettv->vval.v_float = f;
2270 }
2271 }
2272 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002273 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2274 {
2275 char_u *bp;
2276 blob_T *blob = NULL; // init for gcc
2277
2278 // Blob constant: 0z0123456789abcdef
2279 if (evaluate)
2280 blob = blob_alloc();
2281 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2282 {
2283 if (!vim_isxdigit(bp[1]))
2284 {
2285 if (blob != NULL)
2286 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002287 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002288 ga_clear(&blob->bv_ga);
2289 VIM_CLEAR(blob);
2290 }
2291 return FAIL;
2292 }
2293 if (blob != NULL)
2294 ga_append(&blob->bv_ga,
2295 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2296 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2297 ++bp;
2298 }
2299 if (blob != NULL)
2300 rettv_blob_set(rettv, blob);
2301 *arg = bp;
2302 }
2303 else
2304 {
2305 varnumber_T n;
2306
2307 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002308 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002309 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002310 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002311 if (len == 0)
2312 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002313 if (evaluate)
2314 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002315 return FAIL;
2316 }
2317 *arg += len;
2318 if (evaluate)
2319 {
2320 rettv->v_type = VAR_NUMBER;
2321 rettv->vval.v_number = n;
2322 }
2323 }
2324 return OK;
2325}
2326
2327/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002328 * Evaluate a string constant and put the result in "rettv".
2329 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2330 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2331 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002332 * Return OK or FAIL.
2333 */
2334 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002335eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002336{
2337 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002338 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002339 int extra = interpolate ? 1 : 0;
2340 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002341 int len;
2342
2343 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002344 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002345 {
2346 if (*p == '\\' && p[1] != NUL)
2347 {
2348 ++p;
2349 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002350 // to 9 characters (6 for the char and 3 for a modifier):
2351 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002352 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002353 {
2354 int modifiers = 0;
2355 int flags = FSK_KEYCODE | FSK_IN_STRING;
2356
zeertzjqdb088872022-05-02 22:53:45 +01002357 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002358
2359 // Skip to the '>' to avoid using '{' inside for string
2360 // interpolation.
2361 if (p[1] != '*')
2362 flags |= FSK_SIMPLIFY;
2363 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2364 --p; // leave "p" on the ">"
2365 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002366 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002367 else if (interpolate && (*p == '{' || *p == '}'))
2368 {
2369 if (*p == '{' && p[1] != '{') // start of expression
2370 break;
2371 ++p;
2372 if (p[-1] == '}' && *p != '}') // single '}' is an error
2373 {
2374 semsg(_(e_stray_closing_curly_str), *arg);
2375 return FAIL;
2376 }
2377 --extra; // "{{" becomes "{", "}}" becomes "}"
2378 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002379 }
2380
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002381 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002382 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002383 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002384 return FAIL;
2385 }
2386
2387 // If only parsing, set *arg and return here
2388 if (!evaluate)
2389 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002390 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002391 return OK;
2392 }
2393
2394 // Copy the string into allocated memory, handling backslashed
2395 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002396 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002397 len = (int)(p - *arg + extra);
2398 rettv->vval.v_string = alloc(len);
2399 if (rettv->vval.v_string == NULL)
2400 return FAIL;
2401 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002402
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002403 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002404 {
2405 if (*p == '\\')
2406 {
2407 switch (*++p)
2408 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002409 case 'b': *end++ = BS; ++p; break;
2410 case 'e': *end++ = ESC; ++p; break;
2411 case 'f': *end++ = FF; ++p; break;
2412 case 'n': *end++ = NL; ++p; break;
2413 case 'r': *end++ = CAR; ++p; break;
2414 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002415
2416 case 'X': // hex: "\x1", "\x12"
2417 case 'x':
2418 case 'u': // Unicode: "\u0023"
2419 case 'U':
2420 if (vim_isxdigit(p[1]))
2421 {
2422 int n, nr;
2423 int c = toupper(*p);
2424
2425 if (c == 'X')
2426 n = 2;
2427 else if (*p == 'u')
2428 n = 4;
2429 else
2430 n = 8;
2431 nr = 0;
2432 while (--n >= 0 && vim_isxdigit(p[1]))
2433 {
2434 ++p;
2435 nr = (nr << 4) + hex2nr(*p);
2436 }
2437 ++p;
2438 // For "\u" store the number according to
2439 // 'encoding'.
2440 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002441 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002442 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002443 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002444 }
2445 break;
2446
2447 // octal: "\1", "\12", "\123"
2448 case '0':
2449 case '1':
2450 case '2':
2451 case '3':
2452 case '4':
2453 case '5':
2454 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002455 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002456 if (*p >= '0' && *p <= '7')
2457 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002458 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002459 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002460 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002461 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002462 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002463 break;
2464
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002465 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002466 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002467 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002468 int flags = FSK_KEYCODE | FSK_IN_STRING;
2469
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002470 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002471 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002472 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002473 if (extra != 0)
2474 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002475 end += extra;
2476 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002477 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002478 break;
2479 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002480 }
2481 // FALLTHROUGH
2482
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002483 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002484 break;
2485 }
2486 }
2487 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002488 {
2489 if (interpolate && (*p == '{' || *p == '}'))
2490 {
2491 if (*p == '{' && p[1] != '{') // start of expression
2492 break;
2493 ++p; // reduce "{{" to "{" and "}}" to "}"
2494 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002495 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002496 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002497 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002498 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002499 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002500 ++p;
2501 *arg = p;
2502
2503 return OK;
2504}
2505
2506/*
2507 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002508 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2509 * Return OK when a "rettv" was set to the string.
2510 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002511 */
2512 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002513eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002514{
2515 char_u *p;
2516 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002517 int reduce = interpolate ? -1 : 0;
2518 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002519
2520 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002521 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002522 {
2523 if (*p == '\'')
2524 {
2525 if (p[1] != '\'')
2526 break;
2527 ++reduce;
2528 ++p;
2529 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002530 else if (interpolate)
2531 {
2532 if (*p == '{')
2533 {
2534 if (p[1] != '{')
2535 break;
2536 ++p;
2537 ++reduce;
2538 }
2539 else if (*p == '}')
2540 {
2541 ++p;
2542 if (*p != '}')
2543 {
2544 semsg(_(e_stray_closing_curly_str), *arg);
2545 return FAIL;
2546 }
2547 ++reduce;
2548 }
2549 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002550 }
2551
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002552 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002553 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002554 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002555 return FAIL;
2556 }
2557
2558 // If only parsing return after setting "*arg"
2559 if (!evaluate)
2560 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002561 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002562 return OK;
2563 }
2564
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002565 // Copy the string into allocated memory, handling '' to ' reduction and
2566 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002567 str = alloc((p - *arg) - reduce);
2568 if (str == NULL)
2569 return FAIL;
2570 rettv->v_type = VAR_STRING;
2571 rettv->vval.v_string = str;
2572
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002573 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002574 {
2575 if (*p == '\'')
2576 {
2577 if (p[1] != '\'')
2578 break;
2579 ++p;
2580 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002581 else if (interpolate && (*p == '{' || *p == '}'))
2582 {
2583 if (*p == '{' && p[1] != '{')
2584 break;
2585 ++p;
2586 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002587 MB_COPY_CHAR(p, str);
2588 }
2589 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002590 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002591
2592 return OK;
2593}
2594
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002595/*
2596 * Evaluate a single or double quoted string possibly containing expressions.
2597 * "arg" points to the '$'. The result is put in "rettv".
2598 * Returns OK or FAIL.
2599 */
LemonBoy2eaef102022-05-06 13:14:50 +01002600 int
2601eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2602{
2603 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002604 int ret = OK;
2605 int quote;
2606 garray_T ga;
2607 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002608
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002609 ga_init2(&ga, 1, 80);
2610
2611 // *arg is on the '$' character, move it to the first string character.
2612 ++*arg;
2613 quote = **arg;
2614 ++*arg;
2615
2616 for (;;)
2617 {
2618 // Get the string up to the matching quote or to a single '{'.
2619 // "arg" is advanced to either the quote or the '{'.
2620 if (quote == '"')
2621 ret = eval_string(arg, &tv, evaluate, TRUE);
2622 else
2623 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2624 if (ret == FAIL)
2625 break;
2626 if (evaluate)
2627 {
2628 ga_concat(&ga, tv.vval.v_string);
2629 clear_tv(&tv);
2630 }
2631
2632 if (**arg != '{')
2633 {
2634 // found terminating quote
2635 ++*arg;
2636 break;
2637 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002638 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002639 if (p == NULL)
2640 {
2641 ret = FAIL;
2642 break;
2643 }
2644 *arg = p;
2645 }
LemonBoy2eaef102022-05-06 13:14:50 +01002646
2647 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002648 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2649 {
2650 ga_clear(&ga);
2651 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002652 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002653 }
LemonBoy2eaef102022-05-06 13:14:50 +01002654
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002655 rettv->vval.v_string = ga.ga_data;
2656 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002657}
2658
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002659/*
2660 * Return a string with the string representation of a variable.
2661 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2662 * "numbuf" is used for a number.
2663 * Puts quotes around strings, so that they can be parsed back by eval().
2664 * May return NULL.
2665 */
2666 char_u *
2667tv2string(
2668 typval_T *tv,
2669 char_u **tofree,
2670 char_u *numbuf,
2671 int copyID)
2672{
2673 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2674}
2675
2676/*
2677 * Get the value of an environment variable.
2678 * "arg" is pointing to the '$'. It is advanced to after the name.
2679 * If the environment variable was not set, silently assume it is empty.
2680 * Return FAIL if the name is invalid.
2681 */
2682 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002683eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002684{
2685 char_u *string = NULL;
2686 int len;
2687 int cc;
2688 char_u *name;
2689 int mustfree = FALSE;
2690
2691 ++*arg;
2692 name = *arg;
2693 len = get_env_len(arg);
2694 if (evaluate)
2695 {
2696 if (len == 0)
2697 return FAIL; // invalid empty name
2698
2699 cc = name[len];
2700 name[len] = NUL;
2701 // first try vim_getenv(), fast for normal environment vars
2702 string = vim_getenv(name, &mustfree);
2703 if (string != NULL && *string != NUL)
2704 {
2705 if (!mustfree)
2706 string = vim_strsave(string);
2707 }
2708 else
2709 {
2710 if (mustfree)
2711 vim_free(string);
2712
2713 // next try expanding things like $VIM and ${HOME}
2714 string = expand_env_save(name - 1);
2715 if (string != NULL && *string == '$')
2716 VIM_CLEAR(string);
2717 }
2718 name[len] = cc;
2719
2720 rettv->v_type = VAR_STRING;
2721 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002722 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002723 }
2724
2725 return OK;
2726}
2727
2728/*
2729 * Get the lnum from the first argument.
2730 * Also accepts ".", "$", etc., but that only works for the current buffer.
2731 * Returns -1 on error.
2732 */
2733 linenr_T
2734tv_get_lnum(typval_T *argvars)
2735{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002736 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002737 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002738
Bram Moolenaar56acb092020-08-16 14:48:19 +02002739 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2740 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002741 if (lnum <= 0 && did_emsg_before == did_emsg
2742 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002743 {
2744 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002745 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002746
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002747 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002748 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002749 if (fp != NULL)
2750 lnum = fp->lnum;
2751 }
2752 return lnum;
2753}
2754
2755/*
2756 * Get the lnum from the first argument.
2757 * Also accepts "$", then "buf" is used.
2758 * Returns 0 on error.
2759 */
2760 linenr_T
2761tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2762{
2763 if (argvars[0].v_type == VAR_STRING
2764 && argvars[0].vval.v_string != NULL
2765 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002766 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002767 && buf != NULL)
2768 return buf->b_ml.ml_line_count;
2769 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2770}
2771
2772/*
2773 * Get buffer by number or pattern.
2774 */
2775 buf_T *
2776tv_get_buf(typval_T *tv, int curtab_only)
2777{
2778 char_u *name = tv->vval.v_string;
2779 buf_T *buf;
2780
2781 if (tv->v_type == VAR_NUMBER)
2782 return buflist_findnr((int)tv->vval.v_number);
2783 if (tv->v_type != VAR_STRING)
2784 return NULL;
2785 if (name == NULL || *name == NUL)
2786 return curbuf;
2787 if (name[0] == '$' && name[1] == NUL)
2788 return lastbuf;
2789
2790 buf = buflist_find_by_name(name, curtab_only);
2791
2792 // If not found, try expanding the name, like done for bufexists().
2793 if (buf == NULL)
2794 buf = find_buffer(tv);
2795
2796 return buf;
2797}
2798
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002799/*
2800 * Like tv_get_buf() but give an error message is the type is wrong.
2801 */
2802 buf_T *
2803tv_get_buf_from_arg(typval_T *tv)
2804{
2805 buf_T *buf;
2806
2807 ++emsg_off;
2808 buf = tv_get_buf(tv, FALSE);
2809 --emsg_off;
2810 if (buf == NULL
2811 && tv->v_type != VAR_NUMBER
2812 && tv->v_type != VAR_STRING)
2813 // issue errmsg for type error
2814 (void)tv_get_number(tv);
2815 return buf;
2816}
2817
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002818#endif // FEAT_EVAL