blob: 65cd912c32fb94fb1fd4afe3bc800c22da036f25 [file] [log] [blame]
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * typval.c: functions that deal with a typval
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate memory for a variable type-value, and make it empty (0 or NULL
20 * value).
21 */
22 typval_T *
23alloc_tv(void)
24{
25 return ALLOC_CLEAR_ONE(typval_T);
26}
27
28/*
29 * Allocate memory for a variable type-value, and assign a string to it.
30 * The string "s" must have been allocated, it is consumed.
31 * Return NULL for out of memory, the variable otherwise.
32 */
33 typval_T *
34alloc_string_tv(char_u *s)
35{
36 typval_T *rettv;
37
38 rettv = alloc_tv();
39 if (rettv != NULL)
40 {
41 rettv->v_type = VAR_STRING;
42 rettv->vval.v_string = s;
43 }
44 else
45 vim_free(s);
46 return rettv;
47}
48
49/*
50 * Free the memory for a variable type-value.
51 */
52 void
53free_tv(typval_T *varp)
54{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000055 if (varp == NULL)
56 return;
Bram Moolenaar00b28d62022-12-08 15:32:33 +000057
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000058 switch (varp->v_type)
59 {
60 case VAR_FUNC:
61 func_unref(varp->vval.v_string);
62 // FALLTHROUGH
63 case VAR_STRING:
64 vim_free(varp->vval.v_string);
65 break;
66 case VAR_PARTIAL:
67 partial_unref(varp->vval.v_partial);
68 break;
69 case VAR_BLOB:
70 blob_unref(varp->vval.v_blob);
71 break;
72 case VAR_LIST:
73 list_unref(varp->vval.v_list);
74 break;
75 case VAR_DICT:
76 dict_unref(varp->vval.v_dict);
77 break;
78 case VAR_JOB:
79#ifdef FEAT_JOB_CHANNEL
80 job_unref(varp->vval.v_job);
81 break;
82#endif
83 case VAR_CHANNEL:
84#ifdef FEAT_JOB_CHANNEL
85 channel_unref(varp->vval.v_channel);
86 break;
87#endif
88 case VAR_CLASS:
89 class_unref(varp->vval.v_class);
90 break;
91 case VAR_OBJECT:
92 object_unref(varp->vval.v_object);
93 break;
94
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +020095 case VAR_TYPEALIAS:
96 typealias_unref(varp->vval.v_typealias);
97 break;
98
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000099 case VAR_NUMBER:
100 case VAR_FLOAT:
101 case VAR_ANY:
102 case VAR_UNKNOWN:
103 case VAR_VOID:
104 case VAR_BOOL:
105 case VAR_SPECIAL:
106 case VAR_INSTR:
107 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200108 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000109 vim_free(varp);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200110}
111
112/*
113 * Free the memory for a variable value and set the value to NULL or 0.
114 */
115 void
116clear_tv(typval_T *varp)
117{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000118 if (varp == NULL)
119 return;
120
121 switch (varp->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200122 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000123 case VAR_FUNC:
124 func_unref(varp->vval.v_string);
125 // FALLTHROUGH
126 case VAR_STRING:
127 VIM_CLEAR(varp->vval.v_string);
128 break;
129 case VAR_PARTIAL:
130 partial_unref(varp->vval.v_partial);
131 varp->vval.v_partial = NULL;
132 break;
133 case VAR_BLOB:
134 blob_unref(varp->vval.v_blob);
135 varp->vval.v_blob = NULL;
136 break;
137 case VAR_LIST:
138 list_unref(varp->vval.v_list);
139 varp->vval.v_list = NULL;
140 break;
141 case VAR_DICT:
142 dict_unref(varp->vval.v_dict);
143 varp->vval.v_dict = NULL;
144 break;
145 case VAR_NUMBER:
146 case VAR_BOOL:
147 case VAR_SPECIAL:
148 varp->vval.v_number = 0;
149 break;
150 case VAR_FLOAT:
151 varp->vval.v_float = 0.0;
152 break;
153 case VAR_JOB:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200154#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000155 job_unref(varp->vval.v_job);
156 varp->vval.v_job = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200157#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000158 break;
159 case VAR_CHANNEL:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200160#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000161 channel_unref(varp->vval.v_channel);
162 varp->vval.v_channel = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200163#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000164 break;
165 case VAR_INSTR:
166 VIM_CLEAR(varp->vval.v_instr);
167 break;
168 case VAR_CLASS:
169 class_unref(varp->vval.v_class);
170 varp->vval.v_class = NULL;
171 break;
172 case VAR_OBJECT:
173 object_unref(varp->vval.v_object);
174 varp->vval.v_object = NULL;
175 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200176 case VAR_TYPEALIAS:
177 typealias_unref(varp->vval.v_typealias);
178 varp->vval.v_typealias = NULL;
179 break;
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000180 case VAR_UNKNOWN:
181 case VAR_ANY:
182 case VAR_VOID:
183 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200184 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000185 varp->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200186}
187
188/*
189 * Set the value of a variable to NULL without freeing items.
190 */
191 void
192init_tv(typval_T *varp)
193{
194 if (varp != NULL)
195 CLEAR_POINTER(varp);
196}
197
Bram Moolenaar36967b32020-08-17 21:41:02 +0200198 static varnumber_T
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000199tv_get_bool_or_number_chk(
200 typval_T *varp,
201 int *denote,
202 int want_bool,
203 int vim9_string_error) // in Vim9 using a string is an error
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200204{
205 varnumber_T n = 0L;
206
207 switch (varp->v_type)
208 {
209 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200210 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200211 && varp->vval.v_number != 1)
212 {
213 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
214 break;
215 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200216 return varp->vval.v_number;
217 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000218 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200219 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200220 case VAR_FUNC:
221 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000222 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200223 break;
224 case VAR_STRING:
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000225 if (vim9_string_error && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200226 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100227 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200228 break;
229 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200230 if (varp->vval.v_string != NULL)
231 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000232 STR2NR_ALL, &n, NULL, 0, FALSE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200233 return n;
234 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000235 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 break;
237 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000238 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200239 break;
240 case VAR_BOOL:
241 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200242 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200243 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100244 if (varp->v_type == VAR_BOOL)
245 emsg(_(e_using_bool_as_number));
246 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000247 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200248 break;
249 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200250 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
251 case VAR_JOB:
252#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000253 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200254 break;
255#endif
256 case VAR_CHANNEL:
257#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000258 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200259 break;
260#endif
261 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000262 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200263 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000264 case VAR_CLASS:
265 emsg(_(e_using_class_as_number));
266 break;
267 case VAR_OBJECT:
268 emsg(_(e_using_object_as_number));
269 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200270 case VAR_VOID:
271 emsg(_(e_cannot_use_void_value));
272 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200273 case VAR_TYPEALIAS:
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +0200274 semsg(_(e_using_typealias_as_number),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200275 varp->vval.v_typealias->ta_name);
276 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200277 case VAR_UNKNOWN:
278 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200279 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200280 internal_error_no_abort("tv_get_number(UNKNOWN)");
281 break;
282 }
283 if (denote == NULL) // useful for values that must be unsigned
284 n = -1;
285 else
286 *denote = TRUE;
287 return n;
288}
289
Bram Moolenaar36967b32020-08-17 21:41:02 +0200290/*
291 * Get the number value of a variable.
292 * If it is a String variable, uses vim_str2nr().
293 * For incompatible types, return 0.
294 * tv_get_number_chk() is similar to tv_get_number(), but informs the
295 * caller of incompatible types: it sets *denote to TRUE if "denote"
296 * is not NULL or returns -1 otherwise.
297 */
298 varnumber_T
299tv_get_number(typval_T *varp)
300{
301 int error = FALSE;
302
303 return tv_get_number_chk(varp, &error); // return 0L on error
304}
305
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000306/*
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200307 * Like tv_get_number() but in Vim9 script do convert a number in a string to a
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000308 * number without giving an error.
309 */
310 varnumber_T
311tv_to_number(typval_T *varp)
312{
313 int error = FALSE;
314
315 return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
316}
317
Bram Moolenaar36967b32020-08-17 21:41:02 +0200318 varnumber_T
319tv_get_number_chk(typval_T *varp, int *denote)
320{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000321 return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200322}
323
324/*
325 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200326 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200327 */
328 varnumber_T
329tv_get_bool(typval_T *varp)
330{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000331 return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200332}
333
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200334/*
335 * Get the boolean value of "varp". This is like tv_get_number_chk(),
336 * but in Vim9 script accepts Number and Bool.
337 */
338 varnumber_T
339tv_get_bool_chk(typval_T *varp, int *denote)
340{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000341 return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200342}
343
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000344 static float_T
345tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200346{
347 switch (varp->v_type)
348 {
349 case VAR_NUMBER:
350 return (float_T)(varp->vval.v_number);
351 case VAR_FLOAT:
352 return varp->vval.v_float;
353 case VAR_FUNC:
354 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000355 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200356 break;
357 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000358 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200359 break;
360 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000361 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200362 break;
363 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000364 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200365 break;
366 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000367 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200368 break;
369 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000370 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200371 break;
372 case VAR_JOB:
373# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000374 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200375 break;
376# endif
377 case VAR_CHANNEL:
378# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000379 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200380 break;
381# endif
382 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000383 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200384 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 case VAR_CLASS:
386 emsg(_(e_using_class_as_float));
387 break;
388 case VAR_OBJECT:
389 emsg(_(e_using_object_as_float));
390 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200391 case VAR_VOID:
392 emsg(_(e_cannot_use_void_value));
393 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200394 case VAR_TYPEALIAS:
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +0200395 semsg(_(e_using_typealias_as_float),
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200396 varp->vval.v_typealias->ta_name);
397 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200398 case VAR_UNKNOWN:
399 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200400 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200401 internal_error_no_abort("tv_get_float(UNKNOWN)");
402 break;
403 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000404 if (error != NULL)
405 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200406 return 0;
407}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000408
409 float_T
410tv_get_float(typval_T *varp)
411{
412 return tv_get_float_chk(varp, NULL);
413}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200414
415/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100416 * Give an error and return FAIL unless "args[idx]" is unknown
417 */
418 int
419check_for_unknown_arg(typval_T *args, int idx)
420{
421 if (args[idx].v_type != VAR_UNKNOWN)
422 {
423 semsg(_(e_too_many_arguments), idx + 1);
424 return FAIL;
425 }
426 return OK;
427}
428
429/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200430 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100431 */
432 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100433check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100434{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100435 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100436 {
rbtnnddc80af2021-12-17 18:01:31 +0000437 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100438 return FAIL;
439 }
440 return OK;
441}
442
443/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100444 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100445 */
446 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100447check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100448{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100449 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100450 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100451 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100452 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200453 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100454 return FAIL;
455 }
456 return OK;
457}
458
459/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200460 * Check for an optional string argument at 'idx'
461 */
462 int
463check_for_opt_string_arg(typval_T *args, int idx)
464{
465 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100466 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200467}
468
469/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200470 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200471 */
472 int
473check_for_number_arg(typval_T *args, int idx)
474{
475 if (args[idx].v_type != VAR_NUMBER)
476 {
rbtnnddc80af2021-12-17 18:01:31 +0000477 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200478 return FAIL;
479 }
480 return OK;
481}
482
483/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200484 * Check for an optional number argument at 'idx'
485 */
486 int
487check_for_opt_number_arg(typval_T *args, int idx)
488{
489 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100490 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200491}
492
493/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200494 * Give an error and return FAIL unless "args[idx]" is a float or a number.
495 */
496 int
497check_for_float_or_nr_arg(typval_T *args, int idx)
498{
499 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
500 {
rbtnnddc80af2021-12-17 18:01:31 +0000501 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200502 return FAIL;
503 }
504 return OK;
505}
506
507/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200508 * Give an error and return FAIL unless "args[idx]" is a bool.
509 */
510 int
511check_for_bool_arg(typval_T *args, int idx)
512{
513 if (args[idx].v_type != VAR_BOOL
514 && !(args[idx].v_type == VAR_NUMBER
515 && (args[idx].vval.v_number == 0
516 || args[idx].vval.v_number == 1)))
517 {
rbtnnddc80af2021-12-17 18:01:31 +0000518 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200519 return FAIL;
520 }
521 return OK;
522}
523
524/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200525 * Check for an optional bool argument at 'idx'.
526 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200527 */
528 int
529check_for_opt_bool_arg(typval_T *args, int idx)
530{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200531 if (args[idx].v_type == VAR_UNKNOWN)
532 return OK;
533 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200534}
535
536/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200537 * Give an error and return FAIL unless "args[idx]" is a blob.
538 */
539 int
540check_for_blob_arg(typval_T *args, int idx)
541{
542 if (args[idx].v_type != VAR_BLOB)
543 {
rbtnnddc80af2021-12-17 18:01:31 +0000544 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200545 return FAIL;
546 }
547 return OK;
548}
549
550/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200551 * Give an error and return FAIL unless "args[idx]" is a list.
552 */
553 int
554check_for_list_arg(typval_T *args, int idx)
555{
556 if (args[idx].v_type != VAR_LIST)
557 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200558 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200559 return FAIL;
560 }
561 return OK;
562}
563
564/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100565 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
566 */
567 int
568check_for_nonnull_list_arg(typval_T *args, int idx)
569{
570 if (check_for_list_arg(args, idx) == FAIL)
571 return FAIL;
572
573 if (args[idx].vval.v_list == NULL)
574 {
575 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
576 return FAIL;
577 }
578 return OK;
579}
580
581/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200582 * Check for an optional list argument at 'idx'
583 */
584 int
585check_for_opt_list_arg(typval_T *args, int idx)
586{
587 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100588 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589}
590
591/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200592 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200593 */
594 int
595check_for_dict_arg(typval_T *args, int idx)
596{
597 if (args[idx].v_type != VAR_DICT)
598 {
rbtnnddc80af2021-12-17 18:01:31 +0000599 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200600 return FAIL;
601 }
602 return OK;
603}
604
605/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100606 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
607 */
608 int
609check_for_nonnull_dict_arg(typval_T *args, int idx)
610{
611 if (check_for_dict_arg(args, idx) == FAIL)
612 return FAIL;
613
614 if (args[idx].vval.v_dict == NULL)
615 {
616 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
617 return FAIL;
618 }
619 return OK;
620}
621
622/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200623 * Check for an optional dict argument at 'idx'
624 */
625 int
626check_for_opt_dict_arg(typval_T *args, int idx)
627{
628 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100629 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200630}
631
Dominique Pelle748b3082022-01-08 12:41:16 +0000632#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200633/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200634 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
635 */
636 int
637check_for_chan_or_job_arg(typval_T *args, int idx)
638{
639 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
640 {
rbtnnddc80af2021-12-17 18:01:31 +0000641 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200642 return FAIL;
643 }
644 return OK;
645}
646
647/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200648 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
649 * job.
650 */
651 int
652check_for_opt_chan_or_job_arg(typval_T *args, int idx)
653{
654 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100655 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200656}
657
658/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200659 * Give an error and return FAIL unless "args[idx]" is a job.
660 */
661 int
662check_for_job_arg(typval_T *args, int idx)
663{
664 if (args[idx].v_type != VAR_JOB)
665 {
rbtnnddc80af2021-12-17 18:01:31 +0000666 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200667 return FAIL;
668 }
669 return OK;
670}
671
672/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200673 * Check for an optional job argument at 'idx'.
674 */
675 int
676check_for_opt_job_arg(typval_T *args, int idx)
677{
678 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100679 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200680}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000681#else
682/*
683 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
684 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
685 */
686 int
687check_for_opt_chan_or_job_arg(typval_T *args, int idx)
688{
689 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
690}
Dominique Pelle748b3082022-01-08 12:41:16 +0000691#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200692
693/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200694 * Give an error and return FAIL unless "args[idx]" is a string or
695 * a number.
696 */
697 int
698check_for_string_or_number_arg(typval_T *args, int idx)
699{
700 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
701 {
rbtnnddc80af2021-12-17 18:01:31 +0000702 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200703 return FAIL;
704 }
705 return OK;
706}
707
708/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200709 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200710 */
711 int
712check_for_opt_string_or_number_arg(typval_T *args, int idx)
713{
714 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100715 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200716}
717
718/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200719 * Give an error and return FAIL unless "args[idx]" is a buffer number.
720 * Buffer number can be a number or a string.
721 */
722 int
723check_for_buffer_arg(typval_T *args, int idx)
724{
725 return check_for_string_or_number_arg(args, idx);
726}
727
728/*
729 * Check for an optional buffer argument at 'idx'
730 */
731 int
732check_for_opt_buffer_arg(typval_T *args, int idx)
733{
734 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100735 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200736}
737
738/*
739 * Give an error and return FAIL unless "args[idx]" is a line number.
740 * Line number can be a number or a string.
741 */
742 int
743check_for_lnum_arg(typval_T *args, int idx)
744{
745 return check_for_string_or_number_arg(args, idx);
746}
747
748/*
749 * Check for an optional line number argument at 'idx'
750 */
751 int
752check_for_opt_lnum_arg(typval_T *args, int idx)
753{
754 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100755 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200756}
757
Dominique Pelle748b3082022-01-08 12:41:16 +0000758#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200759/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200760 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200761 */
762 int
763check_for_string_or_blob_arg(typval_T *args, int idx)
764{
765 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
766 {
rbtnnddc80af2021-12-17 18:01:31 +0000767 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200768 return FAIL;
769 }
770 return OK;
771}
Dominique Pelle748b3082022-01-08 12:41:16 +0000772#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200773
774/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200775 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200776 */
777 int
778check_for_string_or_list_arg(typval_T *args, int idx)
779{
780 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
781 {
rbtnnddc80af2021-12-17 18:01:31 +0000782 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200783 return FAIL;
784 }
785 return OK;
786}
787
788/*
rbtnn0ccb5842021-12-18 18:33:46 +0000789 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
790 * blob.
791 */
792 int
793check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
794{
795 if (args[idx].v_type != VAR_STRING
796 && args[idx].v_type != VAR_LIST
797 && args[idx].v_type != VAR_BLOB)
798 {
799 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
800 return FAIL;
801 }
802 return OK;
803}
804
805/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200806 * Check for an optional string or list argument at 'idx'
807 */
808 int
809check_for_opt_string_or_list_arg(typval_T *args, int idx)
810{
811 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100812 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200813}
814
815/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200816 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
817 */
818 int
819check_for_string_or_dict_arg(typval_T *args, int idx)
820{
821 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
822 {
rbtnnddc80af2021-12-17 18:01:31 +0000823 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200824 return FAIL;
825 }
826 return OK;
827}
828
829/*
830 * Give an error and return FAIL unless "args[idx]" is a string or a number
831 * or a list.
832 */
833 int
834check_for_string_or_number_or_list_arg(typval_T *args, int idx)
835{
836 if (args[idx].v_type != VAR_STRING
837 && args[idx].v_type != VAR_NUMBER
838 && args[idx].v_type != VAR_LIST)
839 {
rbtnn0ccb5842021-12-18 18:33:46 +0000840 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200841 return FAIL;
842 }
843 return OK;
844}
845
846/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200847 * Give an error and return FAIL unless "args[idx]" is an optional string
848 * or number or a list
849 */
850 int
851check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
852{
853 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100854 || check_for_string_or_number_or_list_arg(args, idx)
855 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200856}
857
858/*
Bakudankun375141e2022-09-09 18:46:47 +0100859 * Give an error and return FAIL unless "args[idx]" is a string or a number
860 * or a list or a blob.
861 */
862 int
863check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
864{
865 if (args[idx].v_type != VAR_STRING
866 && args[idx].v_type != VAR_NUMBER
867 && args[idx].v_type != VAR_LIST
868 && args[idx].v_type != VAR_BLOB)
869 {
870 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
871 return FAIL;
872 }
873 return OK;
874}
875
876/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200877 * Give an error and return FAIL unless "args[idx]" is a string or a list
878 * or a dict.
879 */
880 int
881check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
882{
883 if (args[idx].v_type != VAR_STRING
884 && args[idx].v_type != VAR_LIST
885 && args[idx].v_type != VAR_DICT)
886 {
rbtnnddc80af2021-12-17 18:01:31 +0000887 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200888 return FAIL;
889 }
890 return OK;
891}
892
893/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000894 * Give an error and return FAIL unless "args[idx]" is a string
895 * or a function reference.
896 */
897 int
898check_for_string_or_func_arg(typval_T *args, int idx)
899{
900 if (args[idx].v_type != VAR_PARTIAL
901 && args[idx].v_type != VAR_FUNC
902 && args[idx].v_type != VAR_STRING)
903 {
904 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
905 return FAIL;
906 }
907 return OK;
908}
909
910/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200911 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
912 */
913 int
914check_for_list_or_blob_arg(typval_T *args, int idx)
915{
916 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
917 {
rbtnn0ccb5842021-12-18 18:33:46 +0000918 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200919 return FAIL;
920 }
921 return OK;
922}
923
924/*
925 * Give an error and return FAIL unless "args[idx]" is a list or dict
926 */
927 int
928check_for_list_or_dict_arg(typval_T *args, int idx)
929{
930 if (args[idx].v_type != VAR_LIST
931 && args[idx].v_type != VAR_DICT)
932 {
rbtnnddc80af2021-12-17 18:01:31 +0000933 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200934 return FAIL;
935 }
936 return OK;
937}
938
939/*
940 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
941 * blob.
942 */
943 int
944check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
945{
946 if (args[idx].v_type != VAR_LIST
947 && args[idx].v_type != VAR_DICT
948 && args[idx].v_type != VAR_BLOB)
949 {
rbtnnddc80af2021-12-17 18:01:31 +0000950 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200951 return FAIL;
952 }
953 return OK;
954}
955
956/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000957 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
958 * blob or a string.
959 */
960 int
961check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
962{
963 if (args[idx].v_type != VAR_LIST
964 && args[idx].v_type != VAR_DICT
965 && args[idx].v_type != VAR_BLOB
966 && args[idx].v_type != VAR_STRING)
967 {
rbtnnddc80af2021-12-17 18:01:31 +0000968 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000969 return FAIL;
970 }
971 return OK;
972}
973
974/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200975 * Give an error and return FAIL unless "args[idx]" is an optional buffer
976 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200977 */
978 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200979check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200980{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200981 if (args[idx].v_type != VAR_UNKNOWN
982 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200983 && args[idx].v_type != VAR_NUMBER
984 && args[idx].v_type != VAR_DICT)
985 {
rbtnnddc80af2021-12-17 18:01:31 +0000986 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200987 return FAIL;
988 }
989 return OK;
990}
991
992/*
LemonBoyafe04662023-08-23 21:08:11 +0200993 * Give an error and return FAIL unless "args[idx]" is an object.
994 */
995 int
996check_for_object_arg(typval_T *args, int idx)
997{
998 if (args[idx].v_type != VAR_OBJECT)
999 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02001000 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +02001001 return FAIL;
1002 }
1003 return OK;
1004}
1005
1006/*
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001007 * Returns TRUE if "tv" is a type alias for a class
1008 */
1009 int
1010tv_class_alias(typval_T *tv)
1011{
1012 return tv->v_type == VAR_TYPEALIAS &&
1013 tv->vval.v_typealias->ta_type->tt_type == VAR_OBJECT;
1014}
1015
1016/*
Ernie Rael2025af12023-12-12 16:58:00 +01001017 * Give an error and return FAIL unless "args[idx]" is a class
1018 * or class typealias.
LemonBoyafe04662023-08-23 21:08:11 +02001019 */
1020 int
Ernie Rael2025af12023-12-12 16:58:00 +01001021check_for_class_or_typealias_args(typval_T *args, int idx)
LemonBoyafe04662023-08-23 21:08:11 +02001022{
Ernie Rael2025af12023-12-12 16:58:00 +01001023 for (int i = idx; args[i].v_type != VAR_UNKNOWN; ++i)
LemonBoyafe04662023-08-23 21:08:11 +02001024 {
Ernie Rael2025af12023-12-12 16:58:00 +01001025 if (args[i].v_type != VAR_CLASS && !tv_class_alias(&args[idx]))
1026 {
1027 semsg(_(e_class_or_typealias_required_for_argument_nr), i + 1);
1028 return FAIL;
1029 }
LemonBoyafe04662023-08-23 21:08:11 +02001030 }
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001035 * Get the string value of a variable.
1036 * If it is a Number variable, the number is converted into a string.
1037 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1038 * tv_get_string_buf() uses a given buffer.
1039 * If the String variable has never been set, return an empty string.
1040 * Never returns NULL;
1041 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1042 * NULL on error.
1043 */
1044 char_u *
1045tv_get_string(typval_T *varp)
1046{
1047 static char_u mybuf[NUMBUFLEN];
1048
1049 return tv_get_string_buf(varp, mybuf);
1050}
1051
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001052/*
1053 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1054 */
1055 char_u *
1056tv_get_string_strict(typval_T *varp)
1057{
1058 static char_u mybuf[NUMBUFLEN];
1059 char_u *res = tv_get_string_buf_chk_strict(
1060 varp, mybuf, in_vim9script());
1061
1062 return res != NULL ? res : (char_u *)"";
1063}
1064
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001065 char_u *
1066tv_get_string_buf(typval_T *varp, char_u *buf)
1067{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001068 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001069
1070 return res != NULL ? res : (char_u *)"";
1071}
1072
1073/*
1074 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1075 */
1076 char_u *
1077tv_get_string_chk(typval_T *varp)
1078{
1079 static char_u mybuf[NUMBUFLEN];
1080
1081 return tv_get_string_buf_chk(varp, mybuf);
1082}
1083
1084 char_u *
1085tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1086{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001087 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1088}
1089
1090 char_u *
1091tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1092{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001093 switch (varp->v_type)
1094 {
1095 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001096 if (strict)
1097 {
1098 emsg(_(e_using_number_as_string));
1099 break;
1100 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001101 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1102 (varnumber_T)varp->vval.v_number);
1103 return buf;
1104 case VAR_FUNC:
1105 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001106 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001107 break;
1108 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001109 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001110 break;
1111 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001112 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001113 break;
1114 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001115 if (strict)
1116 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001117 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001118 break;
1119 }
1120 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1121 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001122 case VAR_STRING:
1123 if (varp->vval.v_string != NULL)
1124 return varp->vval.v_string;
1125 return (char_u *)"";
1126 case VAR_BOOL:
1127 case VAR_SPECIAL:
1128 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1129 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001130 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001131 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001132 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001133 case VAR_CLASS:
1134 emsg(_(e_using_class_as_string));
1135 break;
1136 case VAR_OBJECT:
1137 emsg(_(e_using_object_as_string));
1138 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001139 case VAR_JOB:
1140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001141 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001143 semsg(_(e_using_invalid_value_as_string_str), "job");
1144 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001145 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001146 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001147#endif
1148 break;
1149 case VAR_CHANNEL:
1150#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001151 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001152 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001153 semsg(_(e_using_invalid_value_as_string_str), "channel");
1154 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001155 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001156 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001157#endif
1158 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001159 case VAR_VOID:
1160 emsg(_(e_cannot_use_void_value));
1161 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001162 case VAR_TYPEALIAS:
Yegappan Lakshmananfeaccd22023-10-28 15:53:55 +02001163 semsg(_(e_using_typealias_as_string),
1164 varp->vval.v_typealias->ta_name);
1165 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001166 case VAR_UNKNOWN:
1167 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001168 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001169 semsg(_(e_using_invalid_value_as_string_str),
1170 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001171 break;
1172 }
1173 return NULL;
1174}
1175
1176/*
1177 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1178 * string() on Dict, List, etc.
1179 */
1180 char_u *
1181tv_stringify(typval_T *varp, char_u *buf)
1182{
1183 if (varp->v_type == VAR_LIST
1184 || varp->v_type == VAR_DICT
1185 || varp->v_type == VAR_BLOB
1186 || varp->v_type == VAR_FUNC
1187 || varp->v_type == VAR_PARTIAL
1188 || varp->v_type == VAR_FLOAT)
1189 {
1190 typval_T tmp;
1191
1192 f_string(varp, &tmp);
1193 tv_get_string_buf(&tmp, buf);
1194 clear_tv(varp);
1195 *varp = tmp;
1196 return tmp.vval.v_string;
1197 }
1198 return tv_get_string_buf(varp, buf);
1199}
1200
1201/*
1202 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1203 * Also give an error message, using "name" or _("name") when use_gettext is
1204 * TRUE.
1205 */
1206 int
1207tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1208{
1209 int lock = 0;
1210
1211 switch (tv->v_type)
1212 {
1213 case VAR_BLOB:
1214 if (tv->vval.v_blob != NULL)
1215 lock = tv->vval.v_blob->bv_lock;
1216 break;
1217 case VAR_LIST:
1218 if (tv->vval.v_list != NULL)
1219 lock = tv->vval.v_list->lv_lock;
1220 break;
1221 case VAR_DICT:
1222 if (tv->vval.v_dict != NULL)
1223 lock = tv->vval.v_dict->dv_lock;
1224 break;
1225 default:
1226 break;
1227 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001228 return value_check_lock(tv->v_lock, name, use_gettext)
1229 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001230}
1231
1232/*
1233 * Copy the values from typval_T "from" to typval_T "to".
1234 * When needed allocates string or increases reference count.
1235 * Does not make a copy of a list, blob or dict but copies the reference!
1236 * It is OK for "from" and "to" to point to the same item. This is used to
1237 * make a copy later.
1238 */
1239 void
1240copy_tv(typval_T *from, typval_T *to)
1241{
1242 to->v_type = from->v_type;
1243 to->v_lock = 0;
1244 switch (from->v_type)
1245 {
1246 case VAR_NUMBER:
1247 case VAR_BOOL:
1248 case VAR_SPECIAL:
1249 to->vval.v_number = from->vval.v_number;
1250 break;
1251 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001252 to->vval.v_float = from->vval.v_float;
1253 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001254 case VAR_JOB:
1255#ifdef FEAT_JOB_CHANNEL
1256 to->vval.v_job = from->vval.v_job;
1257 if (to->vval.v_job != NULL)
1258 ++to->vval.v_job->jv_refcount;
1259 break;
1260#endif
1261 case VAR_CHANNEL:
1262#ifdef FEAT_JOB_CHANNEL
1263 to->vval.v_channel = from->vval.v_channel;
1264 if (to->vval.v_channel != NULL)
1265 ++to->vval.v_channel->ch_refcount;
1266 break;
1267#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001268 case VAR_INSTR:
1269 to->vval.v_instr = from->vval.v_instr;
1270 break;
1271
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001272 case VAR_CLASS:
1273 copy_class(from, to);
1274 break;
1275
1276 case VAR_OBJECT:
1277 copy_object(from, to);
1278 break;
1279
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001280 case VAR_STRING:
1281 case VAR_FUNC:
1282 if (from->vval.v_string == NULL)
1283 to->vval.v_string = NULL;
1284 else
1285 {
1286 to->vval.v_string = vim_strsave(from->vval.v_string);
1287 if (from->v_type == VAR_FUNC)
1288 func_ref(to->vval.v_string);
1289 }
1290 break;
1291 case VAR_PARTIAL:
1292 if (from->vval.v_partial == NULL)
1293 to->vval.v_partial = NULL;
1294 else
1295 {
1296 to->vval.v_partial = from->vval.v_partial;
1297 ++to->vval.v_partial->pt_refcount;
1298 }
1299 break;
1300 case VAR_BLOB:
1301 if (from->vval.v_blob == NULL)
1302 to->vval.v_blob = NULL;
1303 else
1304 {
1305 to->vval.v_blob = from->vval.v_blob;
1306 ++to->vval.v_blob->bv_refcount;
1307 }
1308 break;
1309 case VAR_LIST:
1310 if (from->vval.v_list == NULL)
1311 to->vval.v_list = NULL;
1312 else
1313 {
1314 to->vval.v_list = from->vval.v_list;
1315 ++to->vval.v_list->lv_refcount;
1316 }
1317 break;
1318 case VAR_DICT:
1319 if (from->vval.v_dict == NULL)
1320 to->vval.v_dict = NULL;
1321 else
1322 {
1323 to->vval.v_dict = from->vval.v_dict;
1324 ++to->vval.v_dict->dv_refcount;
1325 }
1326 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001327 case VAR_TYPEALIAS:
1328 if (from->vval.v_typealias == NULL)
1329 to->vval.v_typealias = NULL;
1330 else
1331 {
1332 to->vval.v_typealias = from->vval.v_typealias;
1333 ++to->vval.v_typealias->ta_refcount;
1334 }
1335 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001336 case VAR_VOID:
1337 emsg(_(e_cannot_use_void_value));
1338 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001339 case VAR_UNKNOWN:
1340 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001341 internal_error_no_abort("copy_tv(UNKNOWN)");
1342 break;
1343 }
1344}
1345
1346/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001347 * Compare "tv1" and "tv2".
1348 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001349 */
1350 int
1351typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001352 typval_T *tv1, // first operand
1353 typval_T *tv2, // second operand
1354 exprtype_T type, // operator
1355 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001356{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001357 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001358 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001359 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1360
Bram Moolenaar265f8112021-12-19 12:33:05 +00001361 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001362 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001363 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001364 // it means TRUE.
1365 n1 = (type == EXPR_ISNOT);
1366 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001367 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1368 || (tv2->v_type == VAR_SPECIAL
1369 && tv2->vval.v_number == VVAL_NULL))
1370 && tv1->v_type != tv2->v_type
1371 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1372 {
1373 n1 = typval_compare_null(tv1, tv2);
1374 if (n1 == MAYBE)
1375 {
1376 clear_tv(tv1);
1377 return FAIL;
1378 }
1379 if (type == EXPR_NEQUAL)
1380 n1 = !n1;
1381 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001382 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001383 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001384 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001385 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001386 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001387 return FAIL;
1388 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001389 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001390 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001391 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001392 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001393 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001394 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001395 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001396 return FAIL;
1397 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001398 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001399 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001400 else if (tv1->v_type == VAR_CLASS || tv2->v_type == VAR_CLASS)
1401 {
1402 if (typval_compare_class(tv1, tv2, type, ic, &res) == FAIL)
1403 {
1404 clear_tv(tv1);
1405 return FAIL;
1406 }
1407 n1 = res;
1408 }
1409 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1410 {
1411 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1412 {
1413 clear_tv(tv1);
1414 return FAIL;
1415 }
1416 n1 = res;
1417 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001418 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001419 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001420 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001421 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001422 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001423 return FAIL;
1424 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001425 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001426 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001427 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1428 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001429 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001430 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001431 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001432 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001433 return FAIL;
1434 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001435 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001436 }
1437
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001438 // If one of the two variables is a float, compare as a float.
1439 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001440 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001441 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1442 {
1443 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001444 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001445
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001446 f1 = tv_get_float_chk(tv1, &error);
1447 if (!error)
1448 f2 = tv_get_float_chk(tv2, &error);
1449 if (error)
1450 {
1451 clear_tv(tv1);
1452 return FAIL;
1453 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001454 n1 = FALSE;
1455 switch (type)
1456 {
1457 case EXPR_IS:
1458 case EXPR_EQUAL: n1 = (f1 == f2); break;
1459 case EXPR_ISNOT:
1460 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1461 case EXPR_GREATER: n1 = (f1 > f2); break;
1462 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1463 case EXPR_SMALLER: n1 = (f1 < f2); break;
1464 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1465 case EXPR_UNKNOWN:
1466 case EXPR_MATCH:
1467 default: break; // avoid gcc warning
1468 }
1469 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001470
1471 // If one of the two variables is a number, compare as a number.
1472 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001473 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001474 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1475 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001476 int error = FALSE;
1477
1478 n1 = tv_get_number_chk(tv1, &error);
1479 if (!error)
1480 n2 = tv_get_number_chk(tv2, &error);
1481 if (error)
1482 {
1483 clear_tv(tv1);
1484 return FAIL;
1485 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001486 switch (type)
1487 {
1488 case EXPR_IS:
1489 case EXPR_EQUAL: n1 = (n1 == n2); break;
1490 case EXPR_ISNOT:
1491 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1492 case EXPR_GREATER: n1 = (n1 > n2); break;
1493 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1494 case EXPR_SMALLER: n1 = (n1 < n2); break;
1495 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1496 case EXPR_UNKNOWN:
1497 case EXPR_MATCH:
1498 default: break; // avoid gcc warning
1499 }
1500 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001501 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1502 || tv2->v_type == VAR_BOOL
1503 || (tv1->v_type == VAR_SPECIAL
1504 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001505 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001506 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001507 {
1508 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001509 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1510 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001511 return FAIL;
1512 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001513 n1 = tv1->vval.v_number;
1514 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001515 switch (type)
1516 {
1517 case EXPR_IS:
1518 case EXPR_EQUAL: n1 = (n1 == n2); break;
1519 case EXPR_ISNOT:
1520 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1521 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001522 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001523 vartype_name(tv1->v_type));
1524 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001525 return FAIL;
1526 }
1527 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001528#ifdef FEAT_JOB_CHANNEL
1529 else if (tv1->v_type == tv2->v_type
1530 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1531 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1532 {
1533 if (tv1->v_type == VAR_CHANNEL)
1534 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1535 else
1536 n1 = tv1->vval.v_job == tv2->vval.v_job;
1537 if (type == EXPR_NEQUAL)
1538 n1 = !n1;
1539 }
1540#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001541 else
1542 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001543 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001544 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001545 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001546 return FAIL;
1547 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001548 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001549 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001550 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001551 if (in_vim9script())
1552 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001553 tv1->v_type = VAR_BOOL;
1554 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001555 }
1556 else
1557 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001558 tv1->v_type = VAR_NUMBER;
1559 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001560 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001561
1562 return OK;
1563}
1564
Bram Moolenaar34453202021-01-31 13:08:38 +01001565/*
dundargocc57b5bc2022-11-02 13:30:51 +00001566 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001567 * Put the result, false or true, in "res".
1568 * Return FAIL and give an error message when the comparison can't be done.
1569 */
1570 int
1571typval_compare_list(
1572 typval_T *tv1,
1573 typval_T *tv2,
1574 exprtype_T type,
1575 int ic,
1576 int *res)
1577{
1578 int val = 0;
1579
1580 if (type == EXPR_IS || type == EXPR_ISNOT)
1581 {
1582 val = (tv1->v_type == tv2->v_type
1583 && tv1->vval.v_list == tv2->vval.v_list);
1584 if (type == EXPR_ISNOT)
1585 val = !val;
1586 }
1587 else if (tv1->v_type != tv2->v_type
1588 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1589 {
1590 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001591 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001592 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001593 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001594 return FAIL;
1595 }
1596 else
1597 {
1598 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1599 ic, FALSE);
1600 if (type == EXPR_NEQUAL)
1601 val = !val;
1602 }
1603 *res = val;
1604 return OK;
1605}
1606
1607/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001608 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001609 */
1610 int
1611typval_compare_null(typval_T *tv1, typval_T *tv2)
1612{
1613 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1614 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1615 {
1616 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1617
1618 switch (tv->v_type)
1619 {
1620 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001621#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001622 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001623#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001624 // TODO: null_class handling
1625 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001626 case VAR_DICT: return tv->vval.v_dict == NULL;
1627 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001628#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001629 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001630#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001631 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001632 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001633 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1634 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001635
1636 case VAR_NUMBER: if (!in_vim9script())
1637 return tv->vval.v_number == 0;
1638 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001639 case VAR_FLOAT: if (!in_vim9script())
1640 return tv->vval.v_float == 0.0;
1641 break;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001642 case VAR_TYPEALIAS: return tv->vval.v_typealias == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001643 default: break;
1644 }
1645 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001646 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001647 // we won't give an error
1648 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001649}
1650
1651/*
dundargocc57b5bc2022-11-02 13:30:51 +00001652 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001653 * Put the result, false or true, in "res".
1654 * Return FAIL and give an error message when the comparison can't be done.
1655 */
1656 int
1657typval_compare_blob(
1658 typval_T *tv1,
1659 typval_T *tv2,
1660 exprtype_T type,
1661 int *res)
1662{
1663 int val = 0;
1664
1665 if (type == EXPR_IS || type == EXPR_ISNOT)
1666 {
1667 val = (tv1->v_type == tv2->v_type
1668 && tv1->vval.v_blob == tv2->vval.v_blob);
1669 if (type == EXPR_ISNOT)
1670 val = !val;
1671 }
1672 else if (tv1->v_type != tv2->v_type
1673 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1674 {
1675 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001676 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001677 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001678 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001679 return FAIL;
1680 }
1681 else
1682 {
1683 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1684 if (type == EXPR_NEQUAL)
1685 val = !val;
1686 }
1687 *res = val;
1688 return OK;
1689}
1690
1691/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001692 * Compare "tv1" to "tv2" as classes according to "type".
1693 * Put the result, false or true, in "res".
1694 * Return FAIL and give an error message when the comparison can't be done.
1695 */
1696 int
1697typval_compare_class(
1698 typval_T *tv1,
1699 typval_T *tv2,
1700 exprtype_T type UNUSED,
1701 int ic UNUSED,
1702 int *res)
1703{
1704 // TODO: use "type"
1705 *res = tv1->vval.v_class == tv2->vval.v_class;
1706 return OK;
1707}
1708
1709/*
1710 * Compare "tv1" to "tv2" as objects according to "type".
1711 * Put the result, false or true, in "res".
1712 * Return FAIL and give an error message when the comparison can't be done.
1713 */
1714 int
1715typval_compare_object(
1716 typval_T *tv1,
1717 typval_T *tv2,
1718 exprtype_T type,
1719 int ic,
1720 int *res)
1721{
1722 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1723
1724 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1725 {
1726 *res = res_match;
1727 return OK;
1728 }
1729 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1730 {
1731 *res = !res_match;
1732 return OK;
1733 }
1734
1735 class_T *cl1 = tv1->vval.v_object->obj_class;
1736 class_T *cl2 = tv2->vval.v_object->obj_class;
1737 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1738 {
1739 *res = !res_match;
1740 return OK;
1741 }
1742
1743 object_T *obj1 = tv1->vval.v_object;
1744 object_T *obj2 = tv2->vval.v_object;
1745 if (type == EXPR_IS || type == EXPR_ISNOT)
1746 {
1747 *res = obj1 == obj2 ? res_match : !res_match;
1748 return OK;
1749 }
1750
1751 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1752 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1753 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1754 {
1755 *res = !res_match;
1756 return OK;
1757 }
1758 *res = res_match;
1759 return OK;
1760}
1761
1762/*
dundargocc57b5bc2022-11-02 13:30:51 +00001763 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001764 * Put the result, false or true, in "res".
1765 * Return FAIL and give an error message when the comparison can't be done.
1766 */
1767 int
1768typval_compare_dict(
1769 typval_T *tv1,
1770 typval_T *tv2,
1771 exprtype_T type,
1772 int ic,
1773 int *res)
1774{
1775 int val;
1776
1777 if (type == EXPR_IS || type == EXPR_ISNOT)
1778 {
1779 val = (tv1->v_type == tv2->v_type
1780 && tv1->vval.v_dict == tv2->vval.v_dict);
1781 if (type == EXPR_ISNOT)
1782 val = !val;
1783 }
1784 else if (tv1->v_type != tv2->v_type
1785 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1786 {
1787 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001788 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001789 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001790 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001791 return FAIL;
1792 }
1793 else
1794 {
1795 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1796 if (type == EXPR_NEQUAL)
1797 val = !val;
1798 }
1799 *res = val;
1800 return OK;
1801}
1802
1803/*
dundargocc57b5bc2022-11-02 13:30:51 +00001804 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001805 * Put the result, false or true, in "res".
1806 * Return FAIL and give an error message when the comparison can't be done.
1807 */
1808 int
1809typval_compare_func(
1810 typval_T *tv1,
1811 typval_T *tv2,
1812 exprtype_T type,
1813 int ic,
1814 int *res)
1815{
1816 int val = 0;
1817
1818 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1819 && type != EXPR_IS && type != EXPR_ISNOT)
1820 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001821 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001822 return FAIL;
1823 }
1824 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1825 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1826 // When both partials are NULL, then they are equal.
1827 // Otherwise they are not equal.
1828 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1829 else if (type == EXPR_IS || type == EXPR_ISNOT)
1830 {
1831 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1832 // strings are considered the same if their value is
1833 // the same
1834 val = tv_equal(tv1, tv2, ic, FALSE);
1835 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1836 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1837 else
1838 val = FALSE;
1839 }
1840 else
1841 val = tv_equal(tv1, tv2, ic, FALSE);
1842 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1843 val = !val;
1844 *res = val;
1845 return OK;
1846}
1847
1848/*
1849 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1850 * Put the result, false or true, in "res".
1851 * Return FAIL and give an error message when the comparison can't be done.
1852 */
1853 int
1854typval_compare_string(
1855 typval_T *tv1,
1856 typval_T *tv2,
1857 exprtype_T type,
1858 int ic,
1859 int *res)
1860{
1861 int i = 0;
1862 int val = FALSE;
1863 char_u *s1, *s2;
1864 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1865
1866 if (in_vim9script()
1867 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1868 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1869 {
1870 semsg(_(e_cannot_compare_str_with_str),
1871 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1872 return FAIL;
1873 }
1874 s1 = tv_get_string_buf(tv1, buf1);
1875 s2 = tv_get_string_buf(tv2, buf2);
1876 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1877 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1878 switch (type)
1879 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001880 case EXPR_IS: if (in_vim9script())
1881 {
1882 // Really check it is the same string, not just
1883 // the same value.
1884 val = tv1->vval.v_string == tv2->vval.v_string;
1885 break;
1886 }
1887 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001888 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001889 case EXPR_ISNOT: if (in_vim9script())
1890 {
1891 // Really check it is not the same string, not
1892 // just a different value.
1893 val = tv1->vval.v_string != tv2->vval.v_string;
1894 break;
1895 }
1896 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001897 case EXPR_NEQUAL: val = (i != 0); break;
1898 case EXPR_GREATER: val = (i > 0); break;
1899 case EXPR_GEQUAL: val = (i >= 0); break;
1900 case EXPR_SMALLER: val = (i < 0); break;
1901 case EXPR_SEQUAL: val = (i <= 0); break;
1902
1903 case EXPR_MATCH:
1904 case EXPR_NOMATCH:
1905 val = pattern_match(s2, s1, ic);
1906 if (type == EXPR_NOMATCH)
1907 val = !val;
1908 break;
1909
1910 default: break; // avoid gcc warning
1911 }
1912 *res = val;
1913 return OK;
1914}
1915/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001916 * Convert any type to a string, never give an error.
1917 * When "quotes" is TRUE add quotes to a string.
1918 * Returns an allocated string.
1919 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001920 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001921typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001922{
1923 char_u *tofree;
1924 char_u numbuf[NUMBUFLEN];
1925 char_u *ret = NULL;
1926
1927 if (arg == NULL)
1928 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001929 if (!quotes && arg->v_type == VAR_STRING)
1930 {
1931 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1932 : arg->vval.v_string);
1933 }
1934 else
1935 {
1936 ret = tv2string(arg, &tofree, numbuf, 0);
1937 // Make a copy if we have a value but it's not in allocated memory.
1938 if (ret != NULL && tofree == NULL)
1939 ret = vim_strsave(ret);
1940 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001941 return ret;
1942}
1943
1944/*
1945 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1946 * or it refers to a List or Dictionary that is locked.
1947 */
1948 int
1949tv_islocked(typval_T *tv)
1950{
1951 return (tv->v_lock & VAR_LOCKED)
1952 || (tv->v_type == VAR_LIST
1953 && tv->vval.v_list != NULL
1954 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1955 || (tv->v_type == VAR_DICT
1956 && tv->vval.v_dict != NULL
1957 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1958}
1959
1960 static int
1961func_equal(
1962 typval_T *tv1,
1963 typval_T *tv2,
1964 int ic) // ignore case
1965{
1966 char_u *s1, *s2;
1967 dict_T *d1, *d2;
1968 int a1, a2;
1969 int i;
1970
1971 // empty and NULL function name considered the same
1972 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1973 : partial_name(tv1->vval.v_partial);
1974 if (s1 != NULL && *s1 == NUL)
1975 s1 = NULL;
1976 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1977 : partial_name(tv2->vval.v_partial);
1978 if (s2 != NULL && *s2 == NUL)
1979 s2 = NULL;
1980 if (s1 == NULL || s2 == NULL)
1981 {
1982 if (s1 != s2)
1983 return FALSE;
1984 }
1985 else if (STRCMP(s1, s2) != 0)
1986 return FALSE;
1987
1988 // empty dict and NULL dict is different
1989 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1990 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1991 if (d1 == NULL || d2 == NULL)
1992 {
1993 if (d1 != d2)
1994 return FALSE;
1995 }
1996 else if (!dict_equal(d1, d2, ic, TRUE))
1997 return FALSE;
1998
1999 // empty list and no list considered the same
2000 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
2001 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
2002 if (a1 != a2)
2003 return FALSE;
2004 for (i = 0; i < a1; ++i)
2005 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
2006 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
2007 return FALSE;
2008
2009 return TRUE;
2010}
2011
2012/*
2013 * Return TRUE if "tv1" and "tv2" have the same value.
2014 * Compares the items just like "==" would compare them, but strings and
2015 * numbers are different. Floats and numbers are also different.
2016 */
2017 int
2018tv_equal(
2019 typval_T *tv1,
2020 typval_T *tv2,
2021 int ic, // ignore case
2022 int recursive) // TRUE when used recursively
2023{
2024 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2025 char_u *s1, *s2;
2026 static int recursive_cnt = 0; // catch recursive loops
2027 int r;
2028 static int tv_equal_recurse_limit;
2029
2030 // Catch lists and dicts that have an endless loop by limiting
2031 // recursiveness to a limit. We guess they are equal then.
2032 // A fixed limit has the problem of still taking an awful long time.
2033 // Reduce the limit every time running into it. That should work fine for
2034 // deeply linked structures that are not recursively linked and catch
2035 // recursiveness quickly.
2036 if (!recursive)
2037 tv_equal_recurse_limit = 1000;
2038 if (recursive_cnt >= tv_equal_recurse_limit)
2039 {
2040 --tv_equal_recurse_limit;
2041 return TRUE;
2042 }
2043
2044 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2045 // arguments.
2046 if ((tv1->v_type == VAR_FUNC
2047 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2048 && (tv2->v_type == VAR_FUNC
2049 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2050 {
2051 ++recursive_cnt;
2052 r = func_equal(tv1, tv2, ic);
2053 --recursive_cnt;
2054 return r;
2055 }
2056
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002057 if (tv1->v_type != tv2->v_type
2058 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2059 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002060 return FALSE;
2061
2062 switch (tv1->v_type)
2063 {
2064 case VAR_LIST:
2065 ++recursive_cnt;
2066 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2067 --recursive_cnt;
2068 return r;
2069
2070 case VAR_DICT:
2071 ++recursive_cnt;
2072 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2073 --recursive_cnt;
2074 return r;
2075
2076 case VAR_BLOB:
2077 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2078
2079 case VAR_NUMBER:
2080 case VAR_BOOL:
2081 case VAR_SPECIAL:
2082 return tv1->vval.v_number == tv2->vval.v_number;
2083
2084 case VAR_STRING:
2085 s1 = tv_get_string_buf(tv1, buf1);
2086 s2 = tv_get_string_buf(tv2, buf2);
2087 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2088
2089 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002090 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002091 case VAR_JOB:
2092#ifdef FEAT_JOB_CHANNEL
2093 return tv1->vval.v_job == tv2->vval.v_job;
2094#endif
2095 case VAR_CHANNEL:
2096#ifdef FEAT_JOB_CHANNEL
2097 return tv1->vval.v_channel == tv2->vval.v_channel;
2098#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002099 case VAR_INSTR:
2100 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002101
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002102 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002103 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002104 return tv1->vval.v_class == tv2->vval.v_class;
2105
2106 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002107 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2108 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002109
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002110 case VAR_PARTIAL:
2111 return tv1->vval.v_partial == tv2->vval.v_partial;
2112
2113 case VAR_FUNC:
2114 return tv1->vval.v_string == tv2->vval.v_string;
2115
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002116 case VAR_TYPEALIAS:
2117 return tv1->vval.v_typealias == tv2->vval.v_typealias;
2118
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002119 case VAR_UNKNOWN:
2120 case VAR_ANY:
2121 case VAR_VOID:
2122 break;
2123 }
2124
2125 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2126 // does not equal anything, not even itself.
2127 return FALSE;
2128}
2129
2130/*
2131 * Get an option value.
2132 * "arg" points to the '&' or '+' before the option name.
2133 * "arg" is advanced to character after the option name.
2134 * Return OK or FAIL.
2135 */
2136 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002137eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002138 char_u **arg,
2139 typval_T *rettv, // when NULL, only check if option exists
2140 int evaluate)
2141{
2142 char_u *option_end;
2143 long numval;
2144 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002145 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002146 int c;
2147 int working = (**arg == '+'); // has("+option")
2148 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002149 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002150
2151 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002152 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002153 if (option_end == NULL)
2154 {
2155 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002156 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002157 return FAIL;
2158 }
2159
2160 if (!evaluate)
2161 {
2162 *arg = option_end;
2163 return OK;
2164 }
2165
2166 c = *option_end;
2167 *option_end = NUL;
2168 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002169 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002170
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002171 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002172 {
2173 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002174 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002175 ret = FAIL;
2176 }
2177 else if (rettv != NULL)
2178 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002179 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002180 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002181 {
2182 rettv->v_type = VAR_STRING;
2183 rettv->vval.v_string = NULL;
2184 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002185 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002186 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002187 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2188 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189 rettv->vval.v_number = 0;
2190 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002191 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002192 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002193 if (in_vim9script() && opt_type == gov_bool)
2194 {
2195 rettv->v_type = VAR_BOOL;
2196 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2197 }
2198 else
2199 {
2200 rettv->v_type = VAR_NUMBER;
2201 rettv->vval.v_number = numval;
2202 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002203 }
2204 else // string option
2205 {
2206 rettv->v_type = VAR_STRING;
2207 rettv->vval.v_string = stringval;
2208 }
2209 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002210 else if (working && (opt_type == gov_hidden_bool
2211 || opt_type == gov_hidden_number
2212 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002213 ret = FAIL;
2214
2215 *option_end = c; // put back for error messages
2216 *arg = option_end;
2217
2218 return ret;
2219}
2220
2221/*
2222 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2223 * Return OK or FAIL.
2224 */
2225 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002226eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002227 char_u **arg,
2228 typval_T *rettv,
2229 int evaluate,
2230 int want_string UNUSED)
2231{
2232 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002233 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002234 char_u *p;
2235 int get_float = FALSE;
2236
2237 // We accept a float when the format matches
2238 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2239 // strict to avoid backwards compatibility problems.
2240 // With script version 2 and later the leading digit can be
2241 // omitted.
2242 // Don't look for a float after the "." operator, so that
2243 // ":let vers = 1.2.3" doesn't fail.
2244 if (**arg == '.')
2245 p = *arg;
2246 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002247 {
2248 p = *arg + 1;
2249 if (skip_quotes)
2250 for (;;)
2251 {
2252 if (*p == '\'')
2253 ++p;
2254 if (!vim_isdigit(*p))
2255 break;
2256 p = skipdigits(p);
2257 }
2258 else
2259 p = skipdigits(p);
2260 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002261 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2262 {
2263 get_float = TRUE;
2264 p = skipdigits(p + 2);
2265 if (*p == 'e' || *p == 'E')
2266 {
2267 ++p;
2268 if (*p == '-' || *p == '+')
2269 ++p;
2270 if (!vim_isdigit(*p))
2271 get_float = FALSE;
2272 else
2273 p = skipdigits(p + 1);
2274 }
2275 if (ASCII_ISALPHA(*p) || *p == '.')
2276 get_float = FALSE;
2277 }
2278 if (get_float)
2279 {
2280 float_T f;
2281
Bram Moolenaar29500652021-08-08 15:43:34 +02002282 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002283 if (evaluate)
2284 {
2285 rettv->v_type = VAR_FLOAT;
2286 rettv->vval.v_float = f;
2287 }
2288 }
2289 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002290 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2291 {
2292 char_u *bp;
2293 blob_T *blob = NULL; // init for gcc
2294
2295 // Blob constant: 0z0123456789abcdef
2296 if (evaluate)
2297 blob = blob_alloc();
2298 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2299 {
2300 if (!vim_isxdigit(bp[1]))
2301 {
2302 if (blob != NULL)
2303 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002304 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002305 ga_clear(&blob->bv_ga);
2306 VIM_CLEAR(blob);
2307 }
2308 return FAIL;
2309 }
2310 if (blob != NULL)
2311 ga_append(&blob->bv_ga,
2312 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2313 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2314 ++bp;
2315 }
2316 if (blob != NULL)
2317 rettv_blob_set(rettv, blob);
2318 *arg = bp;
2319 }
2320 else
2321 {
2322 varnumber_T n;
2323
2324 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002325 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002326 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002327 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002328 if (len == 0)
2329 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002330 if (evaluate)
2331 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002332 return FAIL;
2333 }
2334 *arg += len;
2335 if (evaluate)
2336 {
2337 rettv->v_type = VAR_NUMBER;
2338 rettv->vval.v_number = n;
2339 }
2340 }
2341 return OK;
2342}
2343
2344/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002345 * Evaluate a string constant and put the result in "rettv".
2346 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2347 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2348 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002349 * Return OK or FAIL.
2350 */
2351 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002352eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002353{
2354 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002355 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002356 int extra = interpolate ? 1 : 0;
2357 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002358 int len;
2359
2360 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002361 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002362 {
2363 if (*p == '\\' && p[1] != NUL)
2364 {
2365 ++p;
2366 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002367 // to 9 characters (6 for the char and 3 for a modifier):
2368 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002369 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002370 {
2371 int modifiers = 0;
2372 int flags = FSK_KEYCODE | FSK_IN_STRING;
2373
zeertzjqdb088872022-05-02 22:53:45 +01002374 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002375
2376 // Skip to the '>' to avoid using '{' inside for string
2377 // interpolation.
2378 if (p[1] != '*')
2379 flags |= FSK_SIMPLIFY;
2380 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2381 --p; // leave "p" on the ">"
2382 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002383 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002384 else if (interpolate && (*p == '{' || *p == '}'))
2385 {
2386 if (*p == '{' && p[1] != '{') // start of expression
2387 break;
2388 ++p;
2389 if (p[-1] == '}' && *p != '}') // single '}' is an error
2390 {
2391 semsg(_(e_stray_closing_curly_str), *arg);
2392 return FAIL;
2393 }
2394 --extra; // "{{" becomes "{", "}}" becomes "}"
2395 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002396 }
2397
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002398 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002399 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002400 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002401 return FAIL;
2402 }
2403
2404 // If only parsing, set *arg and return here
2405 if (!evaluate)
2406 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002407 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002408 return OK;
2409 }
2410
2411 // Copy the string into allocated memory, handling backslashed
2412 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002413 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002414 len = (int)(p - *arg + extra);
2415 rettv->vval.v_string = alloc(len);
2416 if (rettv->vval.v_string == NULL)
2417 return FAIL;
2418 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002419
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002420 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002421 {
2422 if (*p == '\\')
2423 {
2424 switch (*++p)
2425 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002426 case 'b': *end++ = BS; ++p; break;
2427 case 'e': *end++ = ESC; ++p; break;
2428 case 'f': *end++ = FF; ++p; break;
2429 case 'n': *end++ = NL; ++p; break;
2430 case 'r': *end++ = CAR; ++p; break;
2431 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002432
2433 case 'X': // hex: "\x1", "\x12"
2434 case 'x':
2435 case 'u': // Unicode: "\u0023"
2436 case 'U':
2437 if (vim_isxdigit(p[1]))
2438 {
2439 int n, nr;
2440 int c = toupper(*p);
2441
2442 if (c == 'X')
2443 n = 2;
2444 else if (*p == 'u')
2445 n = 4;
2446 else
2447 n = 8;
2448 nr = 0;
2449 while (--n >= 0 && vim_isxdigit(p[1]))
2450 {
2451 ++p;
2452 nr = (nr << 4) + hex2nr(*p);
2453 }
2454 ++p;
2455 // For "\u" store the number according to
2456 // 'encoding'.
2457 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002458 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002459 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002460 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002461 }
2462 break;
2463
2464 // octal: "\1", "\12", "\123"
2465 case '0':
2466 case '1':
2467 case '2':
2468 case '3':
2469 case '4':
2470 case '5':
2471 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002472 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002473 if (*p >= '0' && *p <= '7')
2474 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002475 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002476 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002477 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002478 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002479 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002480 break;
2481
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002482 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002483 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002484 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002485 int flags = FSK_KEYCODE | FSK_IN_STRING;
2486
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002487 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002488 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002489 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002490 if (extra != 0)
2491 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002492 end += extra;
2493 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002494 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002495 break;
2496 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002497 }
2498 // FALLTHROUGH
2499
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002500 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002501 break;
2502 }
2503 }
2504 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002505 {
2506 if (interpolate && (*p == '{' || *p == '}'))
2507 {
2508 if (*p == '{' && p[1] != '{') // start of expression
2509 break;
2510 ++p; // reduce "{{" to "{" and "}}" to "}"
2511 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002512 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002513 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002514 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002515 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002516 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002517 ++p;
2518 *arg = p;
2519
2520 return OK;
2521}
2522
2523/*
2524 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002525 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2526 * Return OK when a "rettv" was set to the string.
2527 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002528 */
2529 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002530eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002531{
2532 char_u *p;
2533 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002534 int reduce = interpolate ? -1 : 0;
2535 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002536
2537 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002538 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002539 {
2540 if (*p == '\'')
2541 {
2542 if (p[1] != '\'')
2543 break;
2544 ++reduce;
2545 ++p;
2546 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002547 else if (interpolate)
2548 {
2549 if (*p == '{')
2550 {
2551 if (p[1] != '{')
2552 break;
2553 ++p;
2554 ++reduce;
2555 }
2556 else if (*p == '}')
2557 {
2558 ++p;
2559 if (*p != '}')
2560 {
2561 semsg(_(e_stray_closing_curly_str), *arg);
2562 return FAIL;
2563 }
2564 ++reduce;
2565 }
2566 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002567 }
2568
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002569 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002570 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002571 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002572 return FAIL;
2573 }
2574
2575 // If only parsing return after setting "*arg"
2576 if (!evaluate)
2577 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002578 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002579 return OK;
2580 }
2581
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002582 // Copy the string into allocated memory, handling '' to ' reduction and
2583 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002584 str = alloc((p - *arg) - reduce);
2585 if (str == NULL)
2586 return FAIL;
2587 rettv->v_type = VAR_STRING;
2588 rettv->vval.v_string = str;
2589
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002590 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002591 {
2592 if (*p == '\'')
2593 {
2594 if (p[1] != '\'')
2595 break;
2596 ++p;
2597 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002598 else if (interpolate && (*p == '{' || *p == '}'))
2599 {
2600 if (*p == '{' && p[1] != '{')
2601 break;
2602 ++p;
2603 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002604 MB_COPY_CHAR(p, str);
2605 }
2606 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002607 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002608
2609 return OK;
2610}
2611
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002612/*
2613 * Evaluate a single or double quoted string possibly containing expressions.
2614 * "arg" points to the '$'. The result is put in "rettv".
2615 * Returns OK or FAIL.
2616 */
LemonBoy2eaef102022-05-06 13:14:50 +01002617 int
2618eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2619{
2620 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002621 int ret = OK;
2622 int quote;
2623 garray_T ga;
2624 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002625
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002626 ga_init2(&ga, 1, 80);
2627
2628 // *arg is on the '$' character, move it to the first string character.
2629 ++*arg;
2630 quote = **arg;
2631 ++*arg;
2632
2633 for (;;)
2634 {
2635 // Get the string up to the matching quote or to a single '{'.
2636 // "arg" is advanced to either the quote or the '{'.
2637 if (quote == '"')
2638 ret = eval_string(arg, &tv, evaluate, TRUE);
2639 else
2640 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2641 if (ret == FAIL)
2642 break;
2643 if (evaluate)
2644 {
2645 ga_concat(&ga, tv.vval.v_string);
2646 clear_tv(&tv);
2647 }
2648
2649 if (**arg != '{')
2650 {
2651 // found terminating quote
2652 ++*arg;
2653 break;
2654 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002655 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002656 if (p == NULL)
2657 {
2658 ret = FAIL;
2659 break;
2660 }
2661 *arg = p;
2662 }
LemonBoy2eaef102022-05-06 13:14:50 +01002663
2664 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002665 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2666 {
2667 ga_clear(&ga);
2668 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002669 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002670 }
LemonBoy2eaef102022-05-06 13:14:50 +01002671
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002672 rettv->vval.v_string = ga.ga_data;
2673 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002674}
2675
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002676/*
2677 * Return a string with the string representation of a variable.
2678 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2679 * "numbuf" is used for a number.
2680 * Puts quotes around strings, so that they can be parsed back by eval().
2681 * May return NULL.
2682 */
2683 char_u *
2684tv2string(
2685 typval_T *tv,
2686 char_u **tofree,
2687 char_u *numbuf,
2688 int copyID)
2689{
2690 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2691}
2692
2693/*
2694 * Get the value of an environment variable.
2695 * "arg" is pointing to the '$'. It is advanced to after the name.
2696 * If the environment variable was not set, silently assume it is empty.
2697 * Return FAIL if the name is invalid.
2698 */
2699 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002700eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002701{
2702 char_u *string = NULL;
2703 int len;
2704 int cc;
2705 char_u *name;
2706 int mustfree = FALSE;
2707
2708 ++*arg;
2709 name = *arg;
2710 len = get_env_len(arg);
2711 if (evaluate)
2712 {
2713 if (len == 0)
2714 return FAIL; // invalid empty name
2715
2716 cc = name[len];
2717 name[len] = NUL;
2718 // first try vim_getenv(), fast for normal environment vars
2719 string = vim_getenv(name, &mustfree);
2720 if (string != NULL && *string != NUL)
2721 {
2722 if (!mustfree)
2723 string = vim_strsave(string);
2724 }
2725 else
2726 {
2727 if (mustfree)
2728 vim_free(string);
2729
2730 // next try expanding things like $VIM and ${HOME}
2731 string = expand_env_save(name - 1);
2732 if (string != NULL && *string == '$')
2733 VIM_CLEAR(string);
2734 }
2735 name[len] = cc;
2736
2737 rettv->v_type = VAR_STRING;
2738 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002739 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002740 }
2741
2742 return OK;
2743}
2744
2745/*
2746 * Get the lnum from the first argument.
2747 * Also accepts ".", "$", etc., but that only works for the current buffer.
2748 * Returns -1 on error.
2749 */
2750 linenr_T
2751tv_get_lnum(typval_T *argvars)
2752{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002753 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002754 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002755
Bram Moolenaar56acb092020-08-16 14:48:19 +02002756 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2757 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002758 if (lnum <= 0 && did_emsg_before == did_emsg
2759 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002760 {
2761 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002762 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002763
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002764 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002765 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002766 if (fp != NULL)
2767 lnum = fp->lnum;
2768 }
2769 return lnum;
2770}
2771
2772/*
2773 * Get the lnum from the first argument.
2774 * Also accepts "$", then "buf" is used.
2775 * Returns 0 on error.
2776 */
2777 linenr_T
2778tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2779{
2780 if (argvars[0].v_type == VAR_STRING
2781 && argvars[0].vval.v_string != NULL
2782 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002783 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002784 && buf != NULL)
2785 return buf->b_ml.ml_line_count;
2786 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2787}
2788
2789/*
2790 * Get buffer by number or pattern.
2791 */
2792 buf_T *
2793tv_get_buf(typval_T *tv, int curtab_only)
2794{
2795 char_u *name = tv->vval.v_string;
2796 buf_T *buf;
2797
2798 if (tv->v_type == VAR_NUMBER)
2799 return buflist_findnr((int)tv->vval.v_number);
2800 if (tv->v_type != VAR_STRING)
2801 return NULL;
2802 if (name == NULL || *name == NUL)
2803 return curbuf;
2804 if (name[0] == '$' && name[1] == NUL)
2805 return lastbuf;
2806
2807 buf = buflist_find_by_name(name, curtab_only);
2808
2809 // If not found, try expanding the name, like done for bufexists().
2810 if (buf == NULL)
2811 buf = find_buffer(tv);
2812
2813 return buf;
2814}
2815
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002816/*
2817 * Like tv_get_buf() but give an error message is the type is wrong.
2818 */
2819 buf_T *
2820tv_get_buf_from_arg(typval_T *tv)
2821{
2822 buf_T *buf;
2823
2824 ++emsg_off;
2825 buf = tv_get_buf(tv, FALSE);
2826 --emsg_off;
2827 if (buf == NULL
2828 && tv->v_type != VAR_NUMBER
2829 && tv->v_type != VAR_STRING)
2830 // issue errmsg for type error
2831 (void)tv_get_number(tv);
2832 return buf;
2833}
2834
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002835#endif // FEAT_EVAL