blob: d43ca863e917667829f98d817cd5c7584c0e88bd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarcd524592016-07-17 14:57:05 +02002 *
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 * dict.c: Dictionary support
12 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020013
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010018// List head for garbage collection. Although there can be a reference loop
19// from partial to dict to partial, we don't need to keep track of the partial,
20// since it will get freed when the dict is unused and gets freed.
21static dict_T *first_dict = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +020022
23/*
24 * Allocate an empty header for a dictionary.
Bram Moolenaar22286892020-11-05 20:50:51 +010025 * Caller should take care of the reference count.
Bram Moolenaarcd524592016-07-17 14:57:05 +020026 */
27 dict_T *
28dict_alloc(void)
29{
30 dict_T *d;
31
Bram Moolenaaradc67142019-06-22 01:40:42 +020032 d = ALLOC_CLEAR_ONE(dict_T);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +000033 if (d == NULL)
34 return NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +020035
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +000036 // Add the dict to the list of dicts for garbage collection.
37 if (first_dict != NULL)
38 first_dict->dv_used_prev = d;
39 d->dv_used_next = first_dict;
40 d->dv_used_prev = NULL;
41 first_dict = d;
42
43 hash_init(&d->dv_hashtab);
44 d->dv_lock = 0;
45 d->dv_scope = 0;
46 d->dv_refcount = 0;
47 d->dv_copyID = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +020048 return d;
49}
50
Bram Moolenaarf49cc602018-11-11 15:21:05 +010051/*
52 * dict_alloc() with an ID for alloc_fail().
53 */
54 dict_T *
55dict_alloc_id(alloc_id_T id UNUSED)
56{
57#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +020058 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +010059 return NULL;
60#endif
61 return (dict_alloc());
62}
63
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010064 dict_T *
65dict_alloc_lock(int lock)
66{
67 dict_T *d = dict_alloc();
68
69 if (d != NULL)
70 d->dv_lock = lock;
71 return d;
72}
73
Bram Moolenaarcd524592016-07-17 14:57:05 +020074/*
75 * Allocate an empty dict for a return value.
76 * Returns OK or FAIL.
77 */
78 int
79rettv_dict_alloc(typval_T *rettv)
80{
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010081 dict_T *d = dict_alloc_lock(0);
Bram Moolenaarcd524592016-07-17 14:57:05 +020082
83 if (d == NULL)
84 return FAIL;
85
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020086 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +020087 return OK;
88}
89
90/*
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020091 * Set a dictionary as the return value
92 */
93 void
94rettv_dict_set(typval_T *rettv, dict_T *d)
95{
96 rettv->v_type = VAR_DICT;
97 rettv->vval.v_dict = d;
98 if (d != NULL)
99 ++d->dv_refcount;
100}
101
102/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200103 * Free a Dictionary, including all non-container items it contains.
104 * Ignores the reference count.
105 */
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100106 void
Bram Moolenaarcd524592016-07-17 14:57:05 +0200107dict_free_contents(dict_T *d)
108{
Bram Moolenaar89483d42020-05-10 15:24:44 +0200109 hashtab_free_contents(&d->dv_hashtab);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100110 free_type(d->dv_type);
111 d->dv_type = NULL;
Bram Moolenaar89483d42020-05-10 15:24:44 +0200112}
113
114/*
115 * Clear hashtab "ht" and dict items it contains.
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100116 * If "ht" is not freed then you should call hash_init() next!
Bram Moolenaar89483d42020-05-10 15:24:44 +0200117 */
118 void
119hashtab_free_contents(hashtab_T *ht)
120{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200121 int todo;
122 hashitem_T *hi;
123 dictitem_T *di;
124
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000125 if (check_hashtab_frozen(ht, "clear dict"))
126 return;
127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100128 // Lock the hashtab, we don't want it to resize while freeing items.
Bram Moolenaar89483d42020-05-10 15:24:44 +0200129 hash_lock(ht);
130 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000131 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200132 {
133 if (!HASHITEM_EMPTY(hi))
134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100135 // Remove the item before deleting it, just in case there is
136 // something recursive causing trouble.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200137 di = HI2DI(hi);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000138 hash_remove(ht, hi, "clear dict");
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100139 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200140 --todo;
141 }
142 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100143
Bram Moolenaar89483d42020-05-10 15:24:44 +0200144 // The hashtab is still locked, it has to be re-initialized anyway.
145 hash_clear(ht);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200146}
147
148 static void
149dict_free_dict(dict_T *d)
150{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100151 // Remove the dict from the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200152 if (d->dv_used_prev == NULL)
153 first_dict = d->dv_used_next;
154 else
155 d->dv_used_prev->dv_used_next = d->dv_used_next;
156 if (d->dv_used_next != NULL)
157 d->dv_used_next->dv_used_prev = d->dv_used_prev;
158 vim_free(d);
159}
160
161 static void
162dict_free(dict_T *d)
163{
164 if (!in_free_unref_items)
165 {
166 dict_free_contents(d);
167 dict_free_dict(d);
168 }
169}
170
171/*
172 * Unreference a Dictionary: decrement the reference count and free it when it
173 * becomes zero.
174 */
175 void
176dict_unref(dict_T *d)
177{
178 if (d != NULL && --d->dv_refcount <= 0)
179 dict_free(d);
180}
181
182/*
183 * Go through the list of dicts and free items without the copyID.
184 * Returns TRUE if something was freed.
185 */
186 int
187dict_free_nonref(int copyID)
188{
189 dict_T *dd;
190 int did_free = FALSE;
191
192 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
193 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100195 // Free the Dictionary and ordinary items it contains, but don't
196 // recurse into Lists and Dictionaries, they will be in the list
197 // of dicts or list of lists.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200198 dict_free_contents(dd);
199 did_free = TRUE;
200 }
201 return did_free;
202}
203
204 void
205dict_free_items(int copyID)
206{
207 dict_T *dd, *dd_next;
208
209 for (dd = first_dict; dd != NULL; dd = dd_next)
210 {
211 dd_next = dd->dv_used_next;
212 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
213 dict_free_dict(dd);
214 }
215}
216
217/*
218 * Allocate a Dictionary item.
219 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200220 * Note that the type and value of the item "di_tv" still needs to be
221 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200222 * Returns NULL when out of memory.
223 */
224 dictitem_T *
225dictitem_alloc(char_u *key)
226{
227 dictitem_T *di;
Bram Moolenaarb4168fd2021-12-22 20:29:09 +0000228 size_t len = STRLEN(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200229
Bram Moolenaarb4168fd2021-12-22 20:29:09 +0000230 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000231 if (di == NULL)
232 return NULL;
233
234 mch_memmove(di->di_key, key, len + 1);
235 di->di_flags = DI_FLAGS_ALLOC;
236 di->di_tv.v_lock = 0;
237 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200238 return di;
239}
240
241/*
242 * Make a copy of a Dictionary item.
243 */
244 static dictitem_T *
245dictitem_copy(dictitem_T *org)
246{
247 dictitem_T *di;
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200248 size_t len = STRLEN(org->di_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200249
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200250 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000251 if (di == NULL)
252 return NULL;
253
254 mch_memmove(di->di_key, org->di_key, len + 1);
255 di->di_flags = DI_FLAGS_ALLOC;
256 copy_tv(&org->di_tv, &di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200257 return di;
258}
259
260/*
261 * Remove item "item" from Dictionary "dict" and free it.
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000262 * "command" is used for the error message when the hashtab if frozen.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200263 */
264 void
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000265dictitem_remove(dict_T *dict, dictitem_T *item, char *command)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200266{
267 hashitem_T *hi;
268
269 hi = hash_find(&dict->dv_hashtab, item->di_key);
270 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100271 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200272 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000273 hash_remove(&dict->dv_hashtab, hi, command);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200274 dictitem_free(item);
275}
276
277/*
278 * Free a dict item. Also clears the value.
279 */
280 void
281dictitem_free(dictitem_T *item)
282{
283 clear_tv(&item->di_tv);
284 if (item->di_flags & DI_FLAGS_ALLOC)
285 vim_free(item);
286}
287
288/*
289 * Make a copy of dict "d". Shallow if "deep" is FALSE.
290 * The refcount of the new dict is set to 1.
Bram Moolenaar381692b2022-02-02 20:01:27 +0000291 * See item_copy() for "top" and "copyID".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200292 * Returns NULL when out of memory.
293 */
294 dict_T *
Bram Moolenaar381692b2022-02-02 20:01:27 +0000295dict_copy(dict_T *orig, int deep, int top, int copyID)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200296{
297 dict_T *copy;
298 dictitem_T *di;
299 int todo;
300 hashitem_T *hi;
301
302 if (orig == NULL)
303 return NULL;
304
305 copy = dict_alloc();
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000306 if (copy == NULL)
307 return NULL;
308
309 if (copyID != 0)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200310 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000311 orig->dv_copyID = copyID;
312 orig->dv_copydict = copy;
313 }
314 if (orig->dv_type == NULL || top || deep)
315 copy->dv_type = NULL;
316 else
317 copy->dv_type = alloc_type(orig->dv_type);
Bram Moolenaar381692b2022-02-02 20:01:27 +0000318
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000319 todo = (int)orig->dv_hashtab.ht_used;
320 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
321 {
322 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200323 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000324 --todo;
325
326 di = dictitem_alloc(hi->hi_key);
327 if (di == NULL)
328 break;
329 if (deep)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200330 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000331 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv,
332 deep, FALSE, copyID) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200333 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000334 vim_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200335 break;
336 }
337 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000338 else
339 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
340 if (dict_add(copy, di) == FAIL)
341 {
342 dictitem_free(di);
343 break;
344 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200345 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000346 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200347
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000348 ++copy->dv_refcount;
349 if (todo > 0)
350 {
351 dict_unref(copy);
352 copy = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200353 }
354
355 return copy;
356}
357
358/*
zeertzjq91c75d12022-11-05 20:21:58 +0000359 * Check for adding a function to g: or s: (in Vim9 script) or l:.
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200360 * If the name is wrong give an error message and return TRUE.
361 */
362 int
363dict_wrong_func_name(dict_T *d, typval_T *tv, char_u *name)
364{
365 return (d == get_globvar_dict()
Bram Moolenaarb54abee2021-06-02 11:49:23 +0200366 || (in_vim9script() && SCRIPT_ID_VALID(current_sctx.sc_sid)
367 && d == &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict)
368 || &d->dv_hashtab == get_funccal_local_ht())
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200369 && (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
370 && var_wrong_func_name(name, TRUE);
371}
372
373/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200374 * Add item "item" to Dictionary "d".
375 * Returns FAIL when out of memory and when key already exists.
376 */
377 int
378dict_add(dict_T *d, dictitem_T *item)
379{
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200380 if (dict_wrong_func_name(d, &item->di_tv, item->di_key))
381 return FAIL;
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000382 return hash_add(&d->dv_hashtab, item->di_key, "add to dictionary");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200383}
384
385/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200386 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200387 * Returns FAIL when out of memory and when key already exists.
388 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200389 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100390dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200391{
392 dictitem_T *item;
393
394 item = dictitem_alloc((char_u *)key);
395 if (item == NULL)
396 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100397 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200398 item->di_tv.vval.v_number = nr;
399 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200400 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200401 dictitem_free(item);
402 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200403 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200404 return OK;
405}
406
407/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200408 * Add a number entry to dictionary "d".
409 * Returns FAIL when out of memory and when key already exists.
410 */
411 int
412dict_add_number(dict_T *d, char *key, varnumber_T nr)
413{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100414 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200415}
416
417/*
418 * Add a special entry to dictionary "d".
419 * Returns FAIL when out of memory and when key already exists.
420 */
421 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100422dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200423{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100424 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200425}
426
427/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200428 * Add a string entry to dictionary "d".
429 * Returns FAIL when out of memory and when key already exists.
430 */
431 int
432dict_add_string(dict_T *d, char *key, char_u *str)
433{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100434 return dict_add_string_len(d, key, str, -1);
435}
436
437/*
438 * Add a string entry to dictionary "d".
439 * "str" will be copied to allocated memory.
440 * When "len" is -1 use the whole string, otherwise only this many bytes.
441 * Returns FAIL when out of memory and when key already exists.
442 */
443 int
444dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
445{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200446 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100447 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200448
449 item = dictitem_alloc((char_u *)key);
450 if (item == NULL)
451 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200452 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100453 if (str != NULL)
454 {
455 if (len == -1)
456 val = vim_strsave(str);
457 else
458 val = vim_strnsave(str, len);
459 }
460 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200461 if (dict_add(d, item) == FAIL)
462 {
463 dictitem_free(item);
464 return FAIL;
465 }
466 return OK;
467}
468
469/*
470 * Add a list entry to dictionary "d".
471 * Returns FAIL when out of memory and when key already exists.
472 */
473 int
474dict_add_list(dict_T *d, char *key, list_T *list)
475{
476 dictitem_T *item;
477
478 item = dictitem_alloc((char_u *)key);
479 if (item == NULL)
480 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200481 item->di_tv.v_type = VAR_LIST;
482 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100483 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200484 if (dict_add(d, item) == FAIL)
485 {
486 dictitem_free(item);
487 return FAIL;
488 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200489 return OK;
490}
491
492/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100493 * Add a typval_T entry to dictionary "d".
494 * Returns FAIL when out of memory and when key already exists.
495 */
496 int
497dict_add_tv(dict_T *d, char *key, typval_T *tv)
498{
499 dictitem_T *item;
500
501 item = dictitem_alloc((char_u *)key);
502 if (item == NULL)
503 return FAIL;
504 copy_tv(tv, &item->di_tv);
505 if (dict_add(d, item) == FAIL)
506 {
507 dictitem_free(item);
508 return FAIL;
509 }
510 return OK;
511}
512
513/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200514 * Add a callback to dictionary "d".
515 * Returns FAIL when out of memory and when key already exists.
516 */
517 int
518dict_add_callback(dict_T *d, char *key, callback_T *cb)
519{
520 dictitem_T *item;
521
522 item = dictitem_alloc((char_u *)key);
523 if (item == NULL)
524 return FAIL;
525 put_callback(cb, &item->di_tv);
526 if (dict_add(d, item) == FAIL)
527 {
528 dictitem_free(item);
529 return FAIL;
530 }
531 return OK;
532}
533
534/*
ichizok663d18d2025-01-02 18:06:00 +0100535 * Add a function entry to dictionary "d".
536 * Returns FAIL when out of memory and when key already exists.
537 */
538 int
539dict_add_func(dict_T *d, char *key, ufunc_T *fp)
540{
541 dictitem_T *item;
542
543 item = dictitem_alloc((char_u *)key);
544 if (item == NULL)
545 return FAIL;
546 item->di_tv.v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +0100547 item->di_tv.vval.v_string = vim_strnsave(fp->uf_name, fp->uf_namelen);
ichizok663d18d2025-01-02 18:06:00 +0100548 if (dict_add(d, item) == FAIL)
549 {
550 dictitem_free(item);
551 return FAIL;
552 }
553 func_ref(item->di_tv.vval.v_string);
554 return OK;
555}
556
557/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200558 * Initializes "iter" for iterating over dictionary items with
559 * dict_iterate_next().
560 * If "var" is not a Dict or an empty Dict then there will be nothing to
561 * iterate over, no error is given.
562 * NOTE: The dictionary must not change until iterating is finished!
563 */
564 void
565dict_iterate_start(typval_T *var, dict_iterator_T *iter)
566{
567 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
568 iter->dit_todo = 0;
569 else
570 {
571 dict_T *d = var->vval.v_dict;
572
573 iter->dit_todo = d->dv_hashtab.ht_used;
574 iter->dit_hi = d->dv_hashtab.ht_array;
575 }
576}
577
578/*
579 * Iterate over the items referred to by "iter". It should be initialized with
580 * dict_iterate_start().
581 * Returns a pointer to the key.
582 * "*tv_result" is set to point to the value for that key.
583 * If there are no more items, NULL is returned.
584 */
585 char_u *
586dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
587{
588 dictitem_T *di;
589 char_u *result;
590
591 if (iter->dit_todo == 0)
592 return NULL;
593
594 while (HASHITEM_EMPTY(iter->dit_hi))
595 ++iter->dit_hi;
596
597 di = HI2DI(iter->dit_hi);
598 result = di->di_key;
599 *tv_result = &di->di_tv;
600
601 --iter->dit_todo;
602 ++iter->dit_hi;
603 return result;
604}
605
606/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200607 * Add a dict entry to dictionary "d".
608 * Returns FAIL when out of memory and when key already exists.
609 */
610 int
611dict_add_dict(dict_T *d, char *key, dict_T *dict)
612{
613 dictitem_T *item;
614
615 item = dictitem_alloc((char_u *)key);
616 if (item == NULL)
617 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200618 item->di_tv.v_type = VAR_DICT;
619 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100620 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200621 if (dict_add(d, item) == FAIL)
622 {
623 dictitem_free(item);
624 return FAIL;
625 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200626 return OK;
627}
628
629/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200630 * Get the number of items in a Dictionary.
631 */
632 long
633dict_len(dict_T *d)
634{
635 if (d == NULL)
636 return 0L;
637 return (long)d->dv_hashtab.ht_used;
638}
639
640/*
641 * Find item "key[len]" in Dictionary "d".
642 * If "len" is negative use strlen(key).
643 * Returns NULL when not found.
644 */
645 dictitem_T *
646dict_find(dict_T *d, char_u *key, int len)
647{
648#define AKEYLEN 200
649 char_u buf[AKEYLEN];
650 char_u *akey;
651 char_u *tofree = NULL;
652 hashitem_T *hi;
653
654 if (d == NULL)
655 return NULL;
656 if (len < 0)
657 akey = key;
658 else if (len >= AKEYLEN)
659 {
660 tofree = akey = vim_strnsave(key, len);
661 if (akey == NULL)
662 return NULL;
663 }
664 else
665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100666 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200667 vim_strncpy(buf, key, len);
668 akey = buf;
669 }
670
671 hi = hash_find(&d->dv_hashtab, akey);
672 vim_free(tofree);
673 if (HASHITEM_EMPTY(hi))
674 return NULL;
675 return HI2DI(hi);
676}
677
678/*
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100679 * Returns TRUE if "key" is present in Dictionary "d".
680 */
681 int
682dict_has_key(dict_T *d, char *key)
683{
684 return dict_find(d, (char_u *)key, -1) != NULL;
685}
686
687/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100688 * Get a typval_T item from a dictionary and copy it into "rettv".
689 * Returns FAIL if the entry doesn't exist or out of memory.
690 */
691 int
Bram Moolenaard61efa52022-07-23 09:52:04 +0100692dict_get_tv(dict_T *d, char *key, typval_T *rettv)
Bram Moolenaar08928322020-01-04 14:32:48 +0100693{
694 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100695
Bram Moolenaard61efa52022-07-23 09:52:04 +0100696 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaar08928322020-01-04 14:32:48 +0100697 if (di == NULL)
698 return FAIL;
699 copy_tv(&di->di_tv, rettv);
700 return OK;
701}
702
703/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200704 * Get a string item from a dictionary.
705 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200706 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200707 * Returns NULL if the entry doesn't exist or out of memory.
708 */
709 char_u *
Bram Moolenaard61efa52022-07-23 09:52:04 +0100710dict_get_string(dict_T *d, char *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200711{
712 dictitem_T *di;
713 char_u *s;
714
Bram Moolenaard61efa52022-07-23 09:52:04 +0100715 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200716 if (di == NULL)
717 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100718 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200719 if (save && s != NULL)
720 s = vim_strsave(s);
721 return s;
722}
723
724/*
725 * Get a number item from a dictionary.
726 * Returns 0 if the entry doesn't exist.
727 */
728 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100729dict_get_number(dict_T *d, char *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200730{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200731 return dict_get_number_def(d, key, 0);
732}
733
734/*
735 * Get a number item from a dictionary.
736 * Returns "def" if the entry doesn't exist.
737 */
738 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100739dict_get_number_def(dict_T *d, char *key, int def)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200740{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200741 dictitem_T *di;
742
Bram Moolenaard61efa52022-07-23 09:52:04 +0100743 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200744 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200745 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100746 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200747}
748
749/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200750 * Get a number item from a dictionary.
751 * Returns 0 if the entry doesn't exist.
752 * Give an error if the entry is not a number.
753 */
754 varnumber_T
755dict_get_number_check(dict_T *d, char_u *key)
756{
757 dictitem_T *di;
758
759 di = dict_find(d, key, -1);
760 if (di == NULL)
761 return 0;
762 if (di->di_tv.v_type != VAR_NUMBER)
763 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000764 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200765 return 0;
766 }
767 return tv_get_number(&di->di_tv);
768}
769
770/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200771 * Get a bool item (number or true/false) from a dictionary.
772 * Returns "def" if the entry doesn't exist.
773 */
774 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100775dict_get_bool(dict_T *d, char *key, int def)
Bram Moolenaar55881332020-08-18 13:04:15 +0200776{
777 dictitem_T *di;
778
Bram Moolenaard61efa52022-07-23 09:52:04 +0100779 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200780 if (di == NULL)
781 return def;
782 return tv_get_bool(&di->di_tv);
783}
784
785/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200786 * Return an allocated string with the string representation of a Dictionary.
787 * May return NULL.
788 */
789 char_u *
790dict2string(typval_T *tv, int copyID, int restore_copyID)
791{
792 garray_T ga;
793 int first = TRUE;
794 char_u *tofree;
795 char_u numbuf[NUMBUFLEN];
796 hashitem_T *hi;
797 char_u *s;
798 dict_T *d;
799 int todo;
800
801 if ((d = tv->vval.v_dict) == NULL)
802 return NULL;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000803 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200804 ga_append(&ga, '{');
805
806 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000807 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200808 {
809 if (!HASHITEM_EMPTY(hi))
810 {
811 --todo;
812
813 if (first)
814 first = FALSE;
815 else
816 ga_concat(&ga, (char_u *)", ");
817
818 tofree = string_quote(hi->hi_key, FALSE);
819 if (tofree != NULL)
820 {
821 ga_concat(&ga, tofree);
822 vim_free(tofree);
823 }
824 ga_concat(&ga, (char_u *)": ");
825 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
826 FALSE, restore_copyID, TRUE);
827 if (s != NULL)
828 ga_concat(&ga, s);
829 vim_free(tofree);
830 if (s == NULL || did_echo_string_emsg)
831 break;
832 line_breakcheck();
833
834 }
835 }
836 if (todo > 0)
837 {
838 vim_free(ga.ga_data);
839 return NULL;
840 }
841
842 ga_append(&ga, '}');
843 ga_append(&ga, NUL);
844 return (char_u *)ga.ga_data;
845}
846
847/*
Bram Moolenaare0de1712020-12-02 17:36:54 +0100848 * Advance over a literal key, including "-". If the first character is not a
849 * literal key character then "key" is returned.
850 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200851 static char_u *
Bram Moolenaare0de1712020-12-02 17:36:54 +0100852skip_literal_key(char_u *key)
853{
854 char_u *p;
855
856 for (p = key; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
857 ;
858 return p;
859}
860
861/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200862 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200863 * Return FAIL when there is no valid key.
864 */
865 static int
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100866get_literal_key_tv(char_u **arg, typval_T *tv)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200867{
Bram Moolenaare0de1712020-12-02 17:36:54 +0100868 char_u *p = skip_literal_key(*arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200869
Bram Moolenaare0de1712020-12-02 17:36:54 +0100870 if (p == *arg)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200871 return FAIL;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200872 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200873 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200874
Bram Moolenaardb199212020-08-12 18:01:53 +0200875 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200876 return OK;
877}
878
879/*
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100880 * Get a literal key for a Vim9 dict:
881 * {"name": value},
882 * {'name': value},
883 * {name: value} use "name" as a literal key
884 * Return the key in allocated memory or NULL in the case of an error.
885 * "arg" is advanced to just after the key.
886 */
887 char_u *
888get_literal_key(char_u **arg)
889{
890 char_u *key;
891 char_u *end;
892 typval_T rettv;
893
894 if (**arg == '\'')
895 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100896 if (eval_lit_string(arg, &rettv, TRUE, FALSE) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100897 return NULL;
898 key = rettv.vval.v_string;
899 }
900 else if (**arg == '"')
901 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100902 if (eval_string(arg, &rettv, TRUE, FALSE) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100903 return NULL;
904 key = rettv.vval.v_string;
905 }
906 else
907 {
908 end = skip_literal_key(*arg);
909 if (end == *arg)
910 {
911 semsg(_(e_invalid_key_str), *arg);
912 return NULL;
913 }
914 key = vim_strnsave(*arg, end - *arg);
915 *arg = end;
916 }
917 return key;
918}
919
920/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200921 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200922 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200923 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200924 * Return OK or FAIL. Returns NOTDONE for {expr}.
925 */
926 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200927eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200928{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200929 int evaluate = evalarg == NULL ? FALSE
zeertzjqe7d49462023-04-16 20:53:55 +0100930 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200931 dict_T *d = NULL;
932 typval_T tvkey;
933 typval_T tv;
934 char_u *key = NULL;
935 dictitem_T *item;
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000936 char_u *curly_expr = skipwhite(*arg + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200937 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200938 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200939 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200940
ii147c7e1e92022-09-07 19:40:17 +0100941 // First check if it's not a curly-braces expression: {expr}.
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000942 // Must do this without evaluating, otherwise a function may be called
943 // twice. Unfortunately this means we need to call eval1() twice for the
944 // first item.
ii147c7e1e92022-09-07 19:40:17 +0100945 // "{}" is an empty Dictionary.
946 // "#{abc}" is never a curly-braces expression.
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000947 if (!vim9script
948 && *curly_expr != '}'
ii147c7e1e92022-09-07 19:40:17 +0100949 && !literal
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000950 && eval1(&curly_expr, &tv, NULL) == OK
951 && *skipwhite(curly_expr) == '}')
952 return NOTDONE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200953
954 if (evaluate)
955 {
956 d = dict_alloc();
957 if (d == NULL)
958 return FAIL;
959 }
960 tvkey.v_type = VAR_UNKNOWN;
961 tv.v_type = VAR_UNKNOWN;
962
Bram Moolenaar962d7212020-07-04 14:15:00 +0200963 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200964 while (**arg != '}' && **arg != NUL)
965 {
Bram Moolenaare0de1712020-12-02 17:36:54 +0100966 int has_bracket = vim9script && **arg == '[';
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100967
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100968 if (literal)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100969 {
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100970 if (get_literal_key_tv(arg, &tvkey) == FAIL)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100971 goto failret;
972 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100973 else if (vim9script && !has_bracket)
974 {
975 tvkey.vval.v_string = get_literal_key(arg);
976 if (tvkey.vval.v_string == NULL)
977 goto failret;
978 tvkey.v_type = VAR_STRING;
979 }
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100980 else
981 {
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100982 if (has_bracket)
983 *arg = skipwhite(*arg + 1);
984 if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!
985 goto failret;
986 if (has_bracket)
987 {
988 *arg = skipwhite(*arg);
989 if (**arg != ']')
990 {
991 emsg(_(e_missing_matching_bracket_after_dict_key));
Bram Moolenaar8bb0f542020-12-06 16:03:55 +0100992 clear_tv(&tvkey);
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100993 return FAIL;
994 }
995 ++*arg;
996 }
997 }
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200998
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200999 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +02001000 // previously, so only require it in Vim9 script.
1001 if (!vim9script)
1002 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001003 if (**arg != ':')
1004 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001005 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001006 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001007 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001008 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001009 clear_tv(&tvkey);
1010 goto failret;
1011 }
1012 if (evaluate)
1013 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01001014 if (tvkey.v_type == VAR_FLOAT)
Bram Moolenaar9987fb02020-12-15 21:41:56 +01001015 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01001016 tvkey.vval.v_string = typval_tostring(&tvkey, TRUE);
1017 tvkey.v_type = VAR_STRING;
Bram Moolenaar9987fb02020-12-15 21:41:56 +01001018 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001019 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001020 if (key == NULL)
1021 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001022 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +02001023 clear_tv(&tvkey);
1024 goto failret;
1025 }
1026 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001027 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1028 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001029 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaarab19d492020-06-27 17:04:05 +02001030 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001031 goto failret;
1032 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001033
Bram Moolenaar962d7212020-07-04 14:15:00 +02001034 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001035 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +02001036 {
1037 if (evaluate)
1038 clear_tv(&tvkey);
1039 goto failret;
1040 }
Ernie Raelfa831102023-12-14 20:06:39 +01001041 if (check_typval_is_value(&tv) == FAIL)
1042 {
1043 if (evaluate)
1044 {
1045 clear_tv(&tvkey);
1046 clear_tv(&tv);
1047 }
1048 goto failret;
1049 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001050 if (evaluate)
1051 {
1052 item = dict_find(d, key, -1);
1053 if (item != NULL)
1054 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001055 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001056 clear_tv(&tvkey);
1057 clear_tv(&tv);
1058 goto failret;
1059 }
1060 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001061 if (item != NULL)
1062 {
1063 item->di_tv = tv;
1064 item->di_tv.v_lock = 0;
1065 if (dict_add(d, item) == FAIL)
1066 dictitem_free(item);
1067 }
1068 }
Bram Moolenaara8931942019-09-28 17:25:10 +02001069 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001070
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001071 // the comma should come right after the value, but this wasn't checked
1072 // previously, so only require it in Vim9 script.
1073 if (!vim9script)
1074 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001075 had_comma = **arg == ',';
1076 if (had_comma)
1077 {
1078 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1079 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001080 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001081 goto failret;
1082 }
1083 *arg = skipwhite(*arg + 1);
1084 }
1085
1086 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +02001087 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001088 if (**arg == '}')
1089 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001090 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001091 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001092 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001093 semsg(_(e_no_white_space_allowed_before_str_str), ",", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001094 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001095 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001096 goto failret;
1097 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001098 }
1099
1100 if (**arg != '}')
1101 {
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001102 if (evalarg != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001103 semsg(_(e_missing_dict_end_str), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001104failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +02001105 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001106 dict_free(d);
1107 return FAIL;
1108 }
1109
Bram Moolenaarb07a39d2020-10-22 19:00:01 +02001110 *arg = *arg + 1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001111 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001112 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001113
1114 return OK;
1115}
1116
1117/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001118 * Evaluate a literal dictionary: #{key: val, key: val}
1119 * "*arg" points to the "#".
1120 * On return, "*arg" points to the character after the Dict.
1121 * Return OK or FAIL. Returns NOTDONE for {expr}.
1122 */
1123 int
1124eval_lit_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
1125{
1126 int vim9script = in_vim9script();
1127 int ret = OK;
1128
1129 if (vim9script)
1130 {
1131 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
1132 }
1133 else if ((*arg)[1] == '{')
1134 {
1135 ++*arg;
1136 ret = eval_dict(arg, rettv, evalarg, TRUE);
1137 }
1138 else
1139 ret = NOTDONE;
1140
1141 return ret;
1142}
1143
1144/*
Bram Moolenaarcd524592016-07-17 14:57:05 +02001145 * Go over all entries in "d2" and add them to "d1".
1146 * When "action" is "error" then a duplicate key is an error.
1147 * When "action" is "force" then a duplicate key is overwritten.
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001148 * When "action" is "move" then move items instead of copying.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001149 * Otherwise duplicate keys are ignored ("action" is "keep").
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001150 * "func_name" is used for reporting where an error occurred.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001151 */
1152 void
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001153dict_extend(dict_T *d1, dict_T *d2, char_u *action, char *func_name)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001154{
1155 dictitem_T *di1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001156 int todo;
1157 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001158 type_T *type;
1159
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001160 if (check_hashtab_frozen(&d1->dv_hashtab, "extend"))
1161 return;
1162
1163 if (*action == 'm')
1164 {
1165 if (check_hashtab_frozen(&d2->dv_hashtab, "extend"))
1166 return;
1167 hash_lock(&d2->dv_hashtab); // don't rehash on hash_remove()
1168 }
1169
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001170 if (d1->dv_type != NULL && d1->dv_type->tt_member != NULL)
1171 type = d1->dv_type->tt_member;
1172 else
1173 type = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001174
1175 todo = (int)d2->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001176 hashitem_T *hi2;
1177 FOR_ALL_HASHTAB_ITEMS(&d2->dv_hashtab, hi2, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001178 {
1179 if (!HASHITEM_EMPTY(hi2))
1180 {
1181 --todo;
1182 di1 = dict_find(d1, hi2->hi_key, -1);
zeertzjq91c75d12022-11-05 20:21:58 +00001183 // Check the key to be valid when adding to any scope.
1184 if (d1->dv_scope != 0 && !valid_varname(hi2->hi_key, -1, TRUE))
1185 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001186
1187 if (type != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001188 && check_typval_arg_type(type, &HI2DI(hi2)->di_tv,
1189 func_name, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001190 break;
1191
Bram Moolenaarcd524592016-07-17 14:57:05 +02001192 if (di1 == NULL)
1193 {
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001194 if (*action == 'm')
1195 {
Bram Moolenaarbc222152022-11-23 11:34:01 +00001196 // Cheap way to move a dict item from "d2" to "d1".
1197 // If dict_add() fails then "d2" won't be empty.
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001198 di1 = HI2DI(hi2);
Bram Moolenaarbc222152022-11-23 11:34:01 +00001199 if (dict_add(d1, di1) == OK)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001200 hash_remove(&d2->dv_hashtab, hi2, "extend");
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001201 }
1202 else
1203 {
1204 di1 = dictitem_copy(HI2DI(hi2));
1205 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1206 dictitem_free(di1);
1207 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001208 }
1209 else if (*action == 'e')
1210 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001211 semsg(_(e_key_already_exists_str), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001212 break;
1213 }
1214 else if (*action == 'f' && HI2DI(hi2) != di1)
1215 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001216 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001217 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001218 break;
zeertzjq91c75d12022-11-05 20:21:58 +00001219 // Disallow replacing a builtin function.
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001220 if (dict_wrong_func_name(d1, &HI2DI(hi2)->di_tv, hi2->hi_key))
1221 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001222 clear_tv(&di1->di_tv);
1223 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1224 }
1225 }
1226 }
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001227
1228 if (*action == 'm')
1229 hash_unlock(&d2->dv_hashtab);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001230}
1231
1232/*
1233 * Return the dictitem that an entry in a hashtable points to.
1234 */
1235 dictitem_T *
1236dict_lookup(hashitem_T *hi)
1237{
1238 return HI2DI(hi);
1239}
1240
1241/*
1242 * Return TRUE when two dictionaries have exactly the same key/values.
1243 */
1244 int
1245dict_equal(
1246 dict_T *d1,
1247 dict_T *d2,
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001248 int ic) // ignore case for strings
Bram Moolenaarcd524592016-07-17 14:57:05 +02001249{
1250 hashitem_T *hi;
1251 dictitem_T *item2;
1252 int todo;
1253
Bram Moolenaarcd524592016-07-17 14:57:05 +02001254 if (d1 == d2)
1255 return TRUE;
1256 if (dict_len(d1) != dict_len(d2))
1257 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001258 if (dict_len(d1) == 0)
1259 // empty and NULL dicts are considered equal
1260 return TRUE;
1261 if (d1 == NULL || d2 == NULL)
1262 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001263
1264 todo = (int)d1->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001265 FOR_ALL_HASHTAB_ITEMS(&d1->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001266 {
1267 if (!HASHITEM_EMPTY(hi))
1268 {
1269 item2 = dict_find(d2, hi->hi_key, -1);
1270 if (item2 == NULL)
1271 return FALSE;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001272 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001273 return FALSE;
1274 --todo;
1275 }
1276 }
1277 return TRUE;
1278}
1279
1280/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001281 * Count the number of times item "needle" occurs in Dict "d". Case is ignored
1282 * if "ic" is TRUE.
1283 */
1284 long
1285dict_count(dict_T *d, typval_T *needle, int ic)
1286{
1287 int todo;
1288 hashitem_T *hi;
1289 long n = 0;
1290
1291 if (d == NULL)
1292 return 0;
1293
1294 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001295 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001296 {
1297 if (!HASHITEM_EMPTY(hi))
1298 {
1299 --todo;
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001300 if (tv_equal(&HI2DI(hi)->di_tv, needle, ic))
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001301 ++n;
1302 }
1303 }
1304
1305 return n;
1306}
1307
1308/*
1309 * extend() a Dict. Append Dict argvars[1] to Dict argvars[0] and return the
1310 * resulting Dict in "rettv". "is_new" is TRUE for extendnew().
1311 */
1312 void
1313dict_extend_func(
1314 typval_T *argvars,
1315 type_T *type,
1316 char *func_name,
1317 char_u *arg_errmsg,
1318 int is_new,
1319 typval_T *rettv)
1320{
1321 dict_T *d1, *d2;
1322 char_u *action;
1323 int i;
1324
1325 d1 = argvars[0].vval.v_dict;
1326 if (d1 == NULL)
1327 {
1328 emsg(_(e_cannot_extend_null_dict));
1329 return;
1330 }
1331 d2 = argvars[1].vval.v_dict;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001332 if (d2 == NULL)
1333 return;
1334
1335 if (!is_new && value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
1336 return;
1337
1338 if (is_new)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001339 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001340 d1 = dict_copy(d1, FALSE, TRUE, get_copyID());
1341 if (d1 == NULL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001342 return;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001343 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001344
1345 // Check the third argument.
1346 if (argvars[2].v_type != VAR_UNKNOWN)
1347 {
1348 static char *(av[]) = {"keep", "force", "error"};
1349
1350 action = tv_get_string_chk(&argvars[2]);
1351 if (action == NULL)
Christian Brabandt29269a72024-04-16 22:44:31 +02001352 {
1353 if (is_new)
1354 dict_unref(d1);
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001355 return;
Christian Brabandt29269a72024-04-16 22:44:31 +02001356 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001357 for (i = 0; i < 3; ++i)
1358 if (STRCMP(action, av[i]) == 0)
1359 break;
1360 if (i == 3)
1361 {
Christian Brabandt29269a72024-04-16 22:44:31 +02001362 if (is_new)
1363 dict_unref(d1);
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001364 semsg(_(e_invalid_argument_str), action);
1365 return;
1366 }
1367 }
1368 else
1369 action = (char_u *)"force";
1370
1371 if (type != NULL && check_typval_arg_type(type, &argvars[1],
Ernie Raele6d40dc2023-03-19 21:23:38 +00001372 func_name, 2) == FAIL)
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001373 return;
1374 dict_extend(d1, d2, action, func_name);
1375
1376 if (is_new)
1377 {
1378 rettv->v_type = VAR_DICT;
1379 rettv->vval.v_dict = d1;
1380 rettv->v_lock = FALSE;
1381 }
1382 else
1383 copy_tv(&argvars[0], rettv);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001384}
1385
1386/*
Ernie Raele79e2072024-01-13 11:47:33 +01001387 * Implementation of map(), filter(), foreach() for a Dict. Apply "expr" to
1388 * every item in Dict "d" and return the result in "rettv".
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001389 */
1390 void
1391dict_filter_map(
1392 dict_T *d,
1393 filtermap_T filtermap,
1394 type_T *argtype,
1395 char *func_name,
1396 char_u *arg_errmsg,
1397 typval_T *expr,
1398 typval_T *rettv)
1399{
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001400 dict_T *d_ret = NULL;
1401 hashtab_T *ht;
1402 hashitem_T *hi;
1403 dictitem_T *di;
1404 int todo;
1405 int rem;
Bram Moolenaar82418262022-09-28 16:16:15 +01001406 typval_T newtv;
1407 funccall_T *fc;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001408
1409 if (filtermap == FILTERMAP_MAPNEW)
1410 {
1411 rettv->v_type = VAR_DICT;
1412 rettv->vval.v_dict = NULL;
1413 }
1414 if (d == NULL
1415 || (filtermap == FILTERMAP_FILTER
1416 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1417 return;
1418
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001419 if (filtermap == FILTERMAP_MAPNEW)
1420 {
1421 if (rettv_dict_alloc(rettv) == FAIL)
1422 return;
1423 d_ret = rettv->vval.v_dict;
1424 }
1425
zeertzjqe7d49462023-04-16 20:53:55 +01001426 // Create one funccall_T for all eval_expr_typval() calls.
Bram Moolenaar82418262022-09-28 16:16:15 +01001427 fc = eval_expr_get_funccal(expr, &newtv);
1428
Ernie Raele6d40dc2023-03-19 21:23:38 +00001429 int prev_lock = d->dv_lock;
1430 if (d->dv_lock == 0)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001431 d->dv_lock = VAR_LOCKED;
1432 ht = &d->dv_hashtab;
1433 hash_lock(ht);
1434 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001435 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001436 {
1437 if (!HASHITEM_EMPTY(hi))
1438 {
1439 int r;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001440
1441 --todo;
1442 di = HI2DI(hi);
1443 if (filtermap == FILTERMAP_MAP
1444 && (value_check_lock(di->di_tv.v_lock,
1445 arg_errmsg, TRUE)
1446 || var_check_ro(di->di_flags,
1447 arg_errmsg, TRUE)))
1448 break;
1449 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar82418262022-09-28 16:16:15 +01001450 r = filter_map_one(&di->di_tv, expr, filtermap, fc, &newtv, &rem);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001451 clear_tv(get_vim_var_tv(VV_KEY));
1452 if (r == FAIL || did_emsg)
1453 {
1454 clear_tv(&newtv);
1455 break;
1456 }
1457 if (filtermap == FILTERMAP_MAP)
1458 {
1459 if (argtype != NULL && check_typval_arg_type(
Bram Moolenaar078a4612022-01-04 15:17:03 +00001460 argtype->tt_member, &newtv, func_name, 0) == FAIL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001461 {
1462 clear_tv(&newtv);
1463 break;
1464 }
1465 // map(): replace the dict item value
1466 clear_tv(&di->di_tv);
1467 newtv.v_lock = 0;
1468 di->di_tv = newtv;
1469 }
1470 else if (filtermap == FILTERMAP_MAPNEW)
1471 {
1472 // mapnew(): add the item value to the new dict
1473 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
1474 clear_tv(&newtv);
1475 if (r == FAIL)
1476 break;
1477 }
1478 else if (filtermap == FILTERMAP_FILTER && rem)
1479 {
1480 // filter(false): remove the item from the dict
1481 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1482 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1483 break;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001484 dictitem_remove(d, di, "filter");
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001485 }
1486 }
1487 }
1488 hash_unlock(ht);
1489 d->dv_lock = prev_lock;
Bram Moolenaar82418262022-09-28 16:16:15 +01001490 if (fc != NULL)
1491 remove_funccal();
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001492}
1493
1494/*
1495 * "remove({dict})" function
1496 */
1497 void
1498dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1499{
1500 dict_T *d;
1501 char_u *key;
1502 dictitem_T *di;
1503
1504 if (argvars[2].v_type != VAR_UNKNOWN)
1505 {
1506 semsg(_(e_too_many_arguments_for_function_str), "remove()");
1507 return;
1508 }
1509
1510 d = argvars[0].vval.v_dict;
1511 if (d == NULL || value_check_lock(d->dv_lock, arg_errmsg, TRUE))
1512 return;
1513
1514 key = tv_get_string_chk(&argvars[1]);
1515 if (key == NULL)
1516 return;
1517
1518 di = dict_find(d, key, -1);
1519 if (di == NULL)
1520 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001521 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001522 return;
1523 }
1524
1525 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1526 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1527 return;
1528
1529 *rettv = di->di_tv;
1530 init_tv(&di->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001531 dictitem_remove(d, di, "remove()");
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001532}
1533
Bram Moolenaar976f8592022-08-30 14:34:52 +01001534typedef enum {
1535 DICT2LIST_KEYS,
1536 DICT2LIST_VALUES,
1537 DICT2LIST_ITEMS,
1538} dict2list_T;
1539
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001540/*
Bram Moolenaar976f8592022-08-30 14:34:52 +01001541 * Turn a dict into a list.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001542 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001543 static void
Bram Moolenaar976f8592022-08-30 14:34:52 +01001544dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001545{
1546 list_T *l2;
1547 dictitem_T *di;
1548 hashitem_T *hi;
1549 listitem_T *li;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001550 dict_T *d;
1551 int todo;
1552
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001553 if (rettv_list_alloc(rettv) == FAIL)
1554 return;
1555
Bram Moolenaar976f8592022-08-30 14:34:52 +01001556 if ((what == DICT2LIST_ITEMS
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001557 ? check_for_string_list_tuple_or_dict_arg(argvars, 0)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001558 : check_for_dict_arg(argvars, 0)) == FAIL)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001559 return;
1560
Bram Moolenaar976f8592022-08-30 14:34:52 +01001561 d = argvars[0].vval.v_dict;
1562 if (d == NULL)
zeertzjqfc305842023-08-19 13:27:03 +02001563 // NULL dict behaves like an empty dict
Bram Moolenaaref982572021-08-12 19:27:57 +02001564 return;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001565
1566 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001567 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001568 {
1569 if (!HASHITEM_EMPTY(hi))
1570 {
1571 --todo;
1572 di = HI2DI(hi);
1573
1574 li = listitem_alloc();
1575 if (li == NULL)
1576 break;
1577 list_append(rettv->vval.v_list, li);
1578
Bram Moolenaar976f8592022-08-30 14:34:52 +01001579 if (what == DICT2LIST_KEYS)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001581 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001582 li->li_tv.v_type = VAR_STRING;
1583 li->li_tv.v_lock = 0;
1584 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1585 }
Bram Moolenaar976f8592022-08-30 14:34:52 +01001586 else if (what == DICT2LIST_VALUES)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001588 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001589 copy_tv(&di->di_tv, &li->li_tv);
1590 }
1591 else
1592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001593 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001594 l2 = list_alloc();
1595 li->li_tv.v_type = VAR_LIST;
1596 li->li_tv.v_lock = 0;
1597 li->li_tv.vval.v_list = l2;
1598 if (l2 == NULL)
1599 break;
1600 ++l2->lv_refcount;
1601
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001602 if (list_append_string(l2, di->di_key, -1) == FAIL
1603 || list_append_tv(l2, &di->di_tv) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001604 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001605 }
1606 }
1607 }
1608}
1609
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001610/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001611 * "items(dict)" function
1612 */
1613 void
1614f_items(typval_T *argvars, typval_T *rettv)
1615{
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001616 if (argvars[0].v_type == VAR_STRING)
1617 string2items(argvars, rettv);
1618 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001619 list2items(argvars, rettv);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001620 else if (argvars[0].v_type == VAR_TUPLE)
1621 tuple2items(argvars, rettv);
Bram Moolenaar976f8592022-08-30 14:34:52 +01001622 else
1623 dict2list(argvars, rettv, DICT2LIST_ITEMS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001624}
1625
1626/*
1627 * "keys()" function
1628 */
1629 void
1630f_keys(typval_T *argvars, typval_T *rettv)
1631{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001632 dict2list(argvars, rettv, DICT2LIST_KEYS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001633}
1634
1635/*
1636 * "values(dict)" function
1637 */
1638 void
1639f_values(typval_T *argvars, typval_T *rettv)
1640{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001641 dict2list(argvars, rettv, DICT2LIST_VALUES);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001642}
1643
1644/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001645 * Make each item in the dict readonly (not the value of the item).
1646 */
1647 void
1648dict_set_items_ro(dict_T *di)
1649{
1650 int todo = (int)di->dv_hashtab.ht_used;
1651 hashitem_T *hi;
1652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001653 // Set readonly
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001654 FOR_ALL_HASHTAB_ITEMS(&di->dv_hashtab, hi, todo)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001655 {
1656 if (HASHITEM_EMPTY(hi))
1657 continue;
1658 --todo;
1659 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1660 }
1661}
1662
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001663/*
1664 * "has_key()" function
1665 */
1666 void
1667f_has_key(typval_T *argvars, typval_T *rettv)
1668{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001669 if (in_vim9script()
1670 && (check_for_dict_arg(argvars, 0) == FAIL
1671 || check_for_string_or_number_arg(argvars, 1) == FAIL))
1672 return;
1673
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001674 if (check_for_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001675 return;
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001676
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001677 if (argvars[0].vval.v_dict == NULL)
1678 return;
1679
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001680 rettv->vval.v_number = dict_has_key(argvars[0].vval.v_dict,
1681 (char *)tv_get_string(&argvars[1]));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001682}
1683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001684#endif // defined(FEAT_EVAL)