blob: bf45b0b9289e359bdaf9a57a8c8a14e9c75a49f0 [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/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200535 * Initializes "iter" for iterating over dictionary items with
536 * dict_iterate_next().
537 * If "var" is not a Dict or an empty Dict then there will be nothing to
538 * iterate over, no error is given.
539 * NOTE: The dictionary must not change until iterating is finished!
540 */
541 void
542dict_iterate_start(typval_T *var, dict_iterator_T *iter)
543{
544 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
545 iter->dit_todo = 0;
546 else
547 {
548 dict_T *d = var->vval.v_dict;
549
550 iter->dit_todo = d->dv_hashtab.ht_used;
551 iter->dit_hi = d->dv_hashtab.ht_array;
552 }
553}
554
555/*
556 * Iterate over the items referred to by "iter". It should be initialized with
557 * dict_iterate_start().
558 * Returns a pointer to the key.
559 * "*tv_result" is set to point to the value for that key.
560 * If there are no more items, NULL is returned.
561 */
562 char_u *
563dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
564{
565 dictitem_T *di;
566 char_u *result;
567
568 if (iter->dit_todo == 0)
569 return NULL;
570
571 while (HASHITEM_EMPTY(iter->dit_hi))
572 ++iter->dit_hi;
573
574 di = HI2DI(iter->dit_hi);
575 result = di->di_key;
576 *tv_result = &di->di_tv;
577
578 --iter->dit_todo;
579 ++iter->dit_hi;
580 return result;
581}
582
583/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200584 * Add a dict entry to dictionary "d".
585 * Returns FAIL when out of memory and when key already exists.
586 */
587 int
588dict_add_dict(dict_T *d, char *key, dict_T *dict)
589{
590 dictitem_T *item;
591
592 item = dictitem_alloc((char_u *)key);
593 if (item == NULL)
594 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200595 item->di_tv.v_type = VAR_DICT;
596 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100597 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200598 if (dict_add(d, item) == FAIL)
599 {
600 dictitem_free(item);
601 return FAIL;
602 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200603 return OK;
604}
605
606/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200607 * Get the number of items in a Dictionary.
608 */
609 long
610dict_len(dict_T *d)
611{
612 if (d == NULL)
613 return 0L;
614 return (long)d->dv_hashtab.ht_used;
615}
616
617/*
618 * Find item "key[len]" in Dictionary "d".
619 * If "len" is negative use strlen(key).
620 * Returns NULL when not found.
621 */
622 dictitem_T *
623dict_find(dict_T *d, char_u *key, int len)
624{
625#define AKEYLEN 200
626 char_u buf[AKEYLEN];
627 char_u *akey;
628 char_u *tofree = NULL;
629 hashitem_T *hi;
630
631 if (d == NULL)
632 return NULL;
633 if (len < 0)
634 akey = key;
635 else if (len >= AKEYLEN)
636 {
637 tofree = akey = vim_strnsave(key, len);
638 if (akey == NULL)
639 return NULL;
640 }
641 else
642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100643 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200644 vim_strncpy(buf, key, len);
645 akey = buf;
646 }
647
648 hi = hash_find(&d->dv_hashtab, akey);
649 vim_free(tofree);
650 if (HASHITEM_EMPTY(hi))
651 return NULL;
652 return HI2DI(hi);
653}
654
655/*
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100656 * Returns TRUE if "key" is present in Dictionary "d".
657 */
658 int
659dict_has_key(dict_T *d, char *key)
660{
661 return dict_find(d, (char_u *)key, -1) != NULL;
662}
663
664/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100665 * Get a typval_T item from a dictionary and copy it into "rettv".
666 * Returns FAIL if the entry doesn't exist or out of memory.
667 */
668 int
Bram Moolenaard61efa52022-07-23 09:52:04 +0100669dict_get_tv(dict_T *d, char *key, typval_T *rettv)
Bram Moolenaar08928322020-01-04 14:32:48 +0100670{
671 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100672
Bram Moolenaard61efa52022-07-23 09:52:04 +0100673 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaar08928322020-01-04 14:32:48 +0100674 if (di == NULL)
675 return FAIL;
676 copy_tv(&di->di_tv, rettv);
677 return OK;
678}
679
680/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200681 * Get a string item from a dictionary.
682 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200683 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200684 * Returns NULL if the entry doesn't exist or out of memory.
685 */
686 char_u *
Bram Moolenaard61efa52022-07-23 09:52:04 +0100687dict_get_string(dict_T *d, char *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200688{
689 dictitem_T *di;
690 char_u *s;
691
Bram Moolenaard61efa52022-07-23 09:52:04 +0100692 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200693 if (di == NULL)
694 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100695 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200696 if (save && s != NULL)
697 s = vim_strsave(s);
698 return s;
699}
700
701/*
702 * Get a number item from a dictionary.
703 * Returns 0 if the entry doesn't exist.
704 */
705 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100706dict_get_number(dict_T *d, char *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200707{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200708 return dict_get_number_def(d, key, 0);
709}
710
711/*
712 * Get a number item from a dictionary.
713 * Returns "def" if the entry doesn't exist.
714 */
715 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100716dict_get_number_def(dict_T *d, char *key, int def)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200717{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200718 dictitem_T *di;
719
Bram Moolenaard61efa52022-07-23 09:52:04 +0100720 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200721 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200722 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100723 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200724}
725
726/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200727 * Get a number item from a dictionary.
728 * Returns 0 if the entry doesn't exist.
729 * Give an error if the entry is not a number.
730 */
731 varnumber_T
732dict_get_number_check(dict_T *d, char_u *key)
733{
734 dictitem_T *di;
735
736 di = dict_find(d, key, -1);
737 if (di == NULL)
738 return 0;
739 if (di->di_tv.v_type != VAR_NUMBER)
740 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000741 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200742 return 0;
743 }
744 return tv_get_number(&di->di_tv);
745}
746
747/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200748 * Get a bool item (number or true/false) from a dictionary.
749 * Returns "def" if the entry doesn't exist.
750 */
751 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100752dict_get_bool(dict_T *d, char *key, int def)
Bram Moolenaar55881332020-08-18 13:04:15 +0200753{
754 dictitem_T *di;
755
Bram Moolenaard61efa52022-07-23 09:52:04 +0100756 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200757 if (di == NULL)
758 return def;
759 return tv_get_bool(&di->di_tv);
760}
761
762/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200763 * Return an allocated string with the string representation of a Dictionary.
764 * May return NULL.
765 */
766 char_u *
767dict2string(typval_T *tv, int copyID, int restore_copyID)
768{
769 garray_T ga;
770 int first = TRUE;
771 char_u *tofree;
772 char_u numbuf[NUMBUFLEN];
773 hashitem_T *hi;
774 char_u *s;
775 dict_T *d;
776 int todo;
777
778 if ((d = tv->vval.v_dict) == NULL)
779 return NULL;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000780 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200781 ga_append(&ga, '{');
782
783 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +0000784 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200785 {
786 if (!HASHITEM_EMPTY(hi))
787 {
788 --todo;
789
790 if (first)
791 first = FALSE;
792 else
793 ga_concat(&ga, (char_u *)", ");
794
795 tofree = string_quote(hi->hi_key, FALSE);
796 if (tofree != NULL)
797 {
798 ga_concat(&ga, tofree);
799 vim_free(tofree);
800 }
801 ga_concat(&ga, (char_u *)": ");
802 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
803 FALSE, restore_copyID, TRUE);
804 if (s != NULL)
805 ga_concat(&ga, s);
806 vim_free(tofree);
807 if (s == NULL || did_echo_string_emsg)
808 break;
809 line_breakcheck();
810
811 }
812 }
813 if (todo > 0)
814 {
815 vim_free(ga.ga_data);
816 return NULL;
817 }
818
819 ga_append(&ga, '}');
820 ga_append(&ga, NUL);
821 return (char_u *)ga.ga_data;
822}
823
824/*
Bram Moolenaare0de1712020-12-02 17:36:54 +0100825 * Advance over a literal key, including "-". If the first character is not a
826 * literal key character then "key" is returned.
827 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200828 static char_u *
Bram Moolenaare0de1712020-12-02 17:36:54 +0100829skip_literal_key(char_u *key)
830{
831 char_u *p;
832
833 for (p = key; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
834 ;
835 return p;
836}
837
838/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200839 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200840 * Return FAIL when there is no valid key.
841 */
842 static int
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100843get_literal_key_tv(char_u **arg, typval_T *tv)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200844{
Bram Moolenaare0de1712020-12-02 17:36:54 +0100845 char_u *p = skip_literal_key(*arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200846
Bram Moolenaare0de1712020-12-02 17:36:54 +0100847 if (p == *arg)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200848 return FAIL;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200849 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200850 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200851
Bram Moolenaardb199212020-08-12 18:01:53 +0200852 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200853 return OK;
854}
855
856/*
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100857 * Get a literal key for a Vim9 dict:
858 * {"name": value},
859 * {'name': value},
860 * {name: value} use "name" as a literal key
861 * Return the key in allocated memory or NULL in the case of an error.
862 * "arg" is advanced to just after the key.
863 */
864 char_u *
865get_literal_key(char_u **arg)
866{
867 char_u *key;
868 char_u *end;
869 typval_T rettv;
870
871 if (**arg == '\'')
872 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100873 if (eval_lit_string(arg, &rettv, TRUE, FALSE) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100874 return NULL;
875 key = rettv.vval.v_string;
876 }
877 else if (**arg == '"')
878 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100879 if (eval_string(arg, &rettv, TRUE, FALSE) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100880 return NULL;
881 key = rettv.vval.v_string;
882 }
883 else
884 {
885 end = skip_literal_key(*arg);
886 if (end == *arg)
887 {
888 semsg(_(e_invalid_key_str), *arg);
889 return NULL;
890 }
891 key = vim_strnsave(*arg, end - *arg);
892 *arg = end;
893 }
894 return key;
895}
896
897/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200898 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200899 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200900 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200901 * Return OK or FAIL. Returns NOTDONE for {expr}.
902 */
903 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200904eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200905{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200906 int evaluate = evalarg == NULL ? FALSE
zeertzjqe7d49462023-04-16 20:53:55 +0100907 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200908 dict_T *d = NULL;
909 typval_T tvkey;
910 typval_T tv;
911 char_u *key = NULL;
912 dictitem_T *item;
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000913 char_u *curly_expr = skipwhite(*arg + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200914 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200915 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200916 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200917
ii147c7e1e92022-09-07 19:40:17 +0100918 // First check if it's not a curly-braces expression: {expr}.
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000919 // Must do this without evaluating, otherwise a function may be called
920 // twice. Unfortunately this means we need to call eval1() twice for the
921 // first item.
ii147c7e1e92022-09-07 19:40:17 +0100922 // "{}" is an empty Dictionary.
923 // "#{abc}" is never a curly-braces expression.
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000924 if (!vim9script
925 && *curly_expr != '}'
ii147c7e1e92022-09-07 19:40:17 +0100926 && !literal
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000927 && eval1(&curly_expr, &tv, NULL) == OK
928 && *skipwhite(curly_expr) == '}')
929 return NOTDONE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200930
931 if (evaluate)
932 {
933 d = dict_alloc();
934 if (d == NULL)
935 return FAIL;
936 }
937 tvkey.v_type = VAR_UNKNOWN;
938 tv.v_type = VAR_UNKNOWN;
939
Bram Moolenaar962d7212020-07-04 14:15:00 +0200940 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200941 while (**arg != '}' && **arg != NUL)
942 {
Bram Moolenaare0de1712020-12-02 17:36:54 +0100943 int has_bracket = vim9script && **arg == '[';
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100944
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100945 if (literal)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100946 {
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100947 if (get_literal_key_tv(arg, &tvkey) == FAIL)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100948 goto failret;
949 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100950 else if (vim9script && !has_bracket)
951 {
952 tvkey.vval.v_string = get_literal_key(arg);
953 if (tvkey.vval.v_string == NULL)
954 goto failret;
955 tvkey.v_type = VAR_STRING;
956 }
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100957 else
958 {
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100959 if (has_bracket)
960 *arg = skipwhite(*arg + 1);
961 if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!
962 goto failret;
963 if (has_bracket)
964 {
965 *arg = skipwhite(*arg);
966 if (**arg != ']')
967 {
968 emsg(_(e_missing_matching_bracket_after_dict_key));
Bram Moolenaar8bb0f542020-12-06 16:03:55 +0100969 clear_tv(&tvkey);
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100970 return FAIL;
971 }
972 ++*arg;
973 }
974 }
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200975
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200976 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200977 // previously, so only require it in Vim9 script.
978 if (!vim9script)
979 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200980 if (**arg != ':')
981 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200982 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +0100983 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200984 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000985 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200986 clear_tv(&tvkey);
987 goto failret;
988 }
989 if (evaluate)
990 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100991 if (tvkey.v_type == VAR_FLOAT)
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100992 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100993 tvkey.vval.v_string = typval_tostring(&tvkey, TRUE);
994 tvkey.v_type = VAR_STRING;
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100995 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100996 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200997 if (key == NULL)
998 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100999 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +02001000 clear_tv(&tvkey);
1001 goto failret;
1002 }
1003 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001004 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1005 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001006 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaarab19d492020-06-27 17:04:05 +02001007 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001008 goto failret;
1009 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001010
Bram Moolenaar962d7212020-07-04 14:15:00 +02001011 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001012 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +02001013 {
1014 if (evaluate)
1015 clear_tv(&tvkey);
1016 goto failret;
1017 }
Ernie Raelfa831102023-12-14 20:06:39 +01001018 if (check_typval_is_value(&tv) == FAIL)
1019 {
1020 if (evaluate)
1021 {
1022 clear_tv(&tvkey);
1023 clear_tv(&tv);
1024 }
1025 goto failret;
1026 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001027 if (evaluate)
1028 {
1029 item = dict_find(d, key, -1);
1030 if (item != NULL)
1031 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001032 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001033 clear_tv(&tvkey);
1034 clear_tv(&tv);
1035 goto failret;
1036 }
1037 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001038 if (item != NULL)
1039 {
1040 item->di_tv = tv;
1041 item->di_tv.v_lock = 0;
1042 if (dict_add(d, item) == FAIL)
1043 dictitem_free(item);
1044 }
1045 }
Bram Moolenaara8931942019-09-28 17:25:10 +02001046 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001047
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001048 // the comma should come right after the value, but this wasn't checked
1049 // previously, so only require it in Vim9 script.
1050 if (!vim9script)
1051 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001052 had_comma = **arg == ',';
1053 if (had_comma)
1054 {
1055 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1056 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001057 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001058 goto failret;
1059 }
1060 *arg = skipwhite(*arg + 1);
1061 }
1062
1063 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +02001064 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001065 if (**arg == '}')
1066 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001067 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001068 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001069 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001070 semsg(_(e_no_white_space_allowed_before_str_str), ",", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001071 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001072 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001073 goto failret;
1074 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001075 }
1076
1077 if (**arg != '}')
1078 {
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001079 if (evalarg != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001080 semsg(_(e_missing_dict_end_str), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001081failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +02001082 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001083 dict_free(d);
1084 return FAIL;
1085 }
1086
Bram Moolenaarb07a39d2020-10-22 19:00:01 +02001087 *arg = *arg + 1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001088 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001089 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001090
1091 return OK;
1092}
1093
1094/*
Yegappan Lakshmanan51c45e82024-05-30 07:50:08 +02001095 * Evaluate a literal dictionary: #{key: val, key: val}
1096 * "*arg" points to the "#".
1097 * On return, "*arg" points to the character after the Dict.
1098 * Return OK or FAIL. Returns NOTDONE for {expr}.
1099 */
1100 int
1101eval_lit_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
1102{
1103 int vim9script = in_vim9script();
1104 int ret = OK;
1105
1106 if (vim9script)
1107 {
1108 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
1109 }
1110 else if ((*arg)[1] == '{')
1111 {
1112 ++*arg;
1113 ret = eval_dict(arg, rettv, evalarg, TRUE);
1114 }
1115 else
1116 ret = NOTDONE;
1117
1118 return ret;
1119}
1120
1121/*
Bram Moolenaarcd524592016-07-17 14:57:05 +02001122 * Go over all entries in "d2" and add them to "d1".
1123 * When "action" is "error" then a duplicate key is an error.
1124 * When "action" is "force" then a duplicate key is overwritten.
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001125 * When "action" is "move" then move items instead of copying.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001126 * Otherwise duplicate keys are ignored ("action" is "keep").
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001127 * "func_name" is used for reporting where an error occurred.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001128 */
1129 void
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001130dict_extend(dict_T *d1, dict_T *d2, char_u *action, char *func_name)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001131{
1132 dictitem_T *di1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001133 int todo;
1134 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001135 type_T *type;
1136
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001137 if (check_hashtab_frozen(&d1->dv_hashtab, "extend"))
1138 return;
1139
1140 if (*action == 'm')
1141 {
1142 if (check_hashtab_frozen(&d2->dv_hashtab, "extend"))
1143 return;
1144 hash_lock(&d2->dv_hashtab); // don't rehash on hash_remove()
1145 }
1146
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001147 if (d1->dv_type != NULL && d1->dv_type->tt_member != NULL)
1148 type = d1->dv_type->tt_member;
1149 else
1150 type = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001151
1152 todo = (int)d2->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001153 hashitem_T *hi2;
1154 FOR_ALL_HASHTAB_ITEMS(&d2->dv_hashtab, hi2, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001155 {
1156 if (!HASHITEM_EMPTY(hi2))
1157 {
1158 --todo;
1159 di1 = dict_find(d1, hi2->hi_key, -1);
zeertzjq91c75d12022-11-05 20:21:58 +00001160 // Check the key to be valid when adding to any scope.
1161 if (d1->dv_scope != 0 && !valid_varname(hi2->hi_key, -1, TRUE))
1162 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001163
1164 if (type != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001165 && check_typval_arg_type(type, &HI2DI(hi2)->di_tv,
1166 func_name, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001167 break;
1168
Bram Moolenaarcd524592016-07-17 14:57:05 +02001169 if (di1 == NULL)
1170 {
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001171 if (*action == 'm')
1172 {
Bram Moolenaarbc222152022-11-23 11:34:01 +00001173 // Cheap way to move a dict item from "d2" to "d1".
1174 // If dict_add() fails then "d2" won't be empty.
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001175 di1 = HI2DI(hi2);
Bram Moolenaarbc222152022-11-23 11:34:01 +00001176 if (dict_add(d1, di1) == OK)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001177 hash_remove(&d2->dv_hashtab, hi2, "extend");
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001178 }
1179 else
1180 {
1181 di1 = dictitem_copy(HI2DI(hi2));
1182 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1183 dictitem_free(di1);
1184 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001185 }
1186 else if (*action == 'e')
1187 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001188 semsg(_(e_key_already_exists_str), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001189 break;
1190 }
1191 else if (*action == 'f' && HI2DI(hi2) != di1)
1192 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001193 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001194 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001195 break;
zeertzjq91c75d12022-11-05 20:21:58 +00001196 // Disallow replacing a builtin function.
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001197 if (dict_wrong_func_name(d1, &HI2DI(hi2)->di_tv, hi2->hi_key))
1198 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001199 clear_tv(&di1->di_tv);
1200 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1201 }
1202 }
1203 }
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001204
1205 if (*action == 'm')
1206 hash_unlock(&d2->dv_hashtab);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001207}
1208
1209/*
1210 * Return the dictitem that an entry in a hashtable points to.
1211 */
1212 dictitem_T *
1213dict_lookup(hashitem_T *hi)
1214{
1215 return HI2DI(hi);
1216}
1217
1218/*
1219 * Return TRUE when two dictionaries have exactly the same key/values.
1220 */
1221 int
1222dict_equal(
1223 dict_T *d1,
1224 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001225 int ic, // ignore case for strings
1226 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001227{
1228 hashitem_T *hi;
1229 dictitem_T *item2;
1230 int todo;
1231
Bram Moolenaarcd524592016-07-17 14:57:05 +02001232 if (d1 == d2)
1233 return TRUE;
1234 if (dict_len(d1) != dict_len(d2))
1235 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001236 if (dict_len(d1) == 0)
1237 // empty and NULL dicts are considered equal
1238 return TRUE;
1239 if (d1 == NULL || d2 == NULL)
1240 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001241
1242 todo = (int)d1->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001243 FOR_ALL_HASHTAB_ITEMS(&d1->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001244 {
1245 if (!HASHITEM_EMPTY(hi))
1246 {
1247 item2 = dict_find(d2, hi->hi_key, -1);
1248 if (item2 == NULL)
1249 return FALSE;
1250 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1251 return FALSE;
1252 --todo;
1253 }
1254 }
1255 return TRUE;
1256}
1257
1258/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001259 * Count the number of times item "needle" occurs in Dict "d". Case is ignored
1260 * if "ic" is TRUE.
1261 */
1262 long
1263dict_count(dict_T *d, typval_T *needle, int ic)
1264{
1265 int todo;
1266 hashitem_T *hi;
1267 long n = 0;
1268
1269 if (d == NULL)
1270 return 0;
1271
1272 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001273 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001274 {
1275 if (!HASHITEM_EMPTY(hi))
1276 {
1277 --todo;
1278 if (tv_equal(&HI2DI(hi)->di_tv, needle, ic, FALSE))
1279 ++n;
1280 }
1281 }
1282
1283 return n;
1284}
1285
1286/*
1287 * extend() a Dict. Append Dict argvars[1] to Dict argvars[0] and return the
1288 * resulting Dict in "rettv". "is_new" is TRUE for extendnew().
1289 */
1290 void
1291dict_extend_func(
1292 typval_T *argvars,
1293 type_T *type,
1294 char *func_name,
1295 char_u *arg_errmsg,
1296 int is_new,
1297 typval_T *rettv)
1298{
1299 dict_T *d1, *d2;
1300 char_u *action;
1301 int i;
1302
1303 d1 = argvars[0].vval.v_dict;
1304 if (d1 == NULL)
1305 {
1306 emsg(_(e_cannot_extend_null_dict));
1307 return;
1308 }
1309 d2 = argvars[1].vval.v_dict;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001310 if (d2 == NULL)
1311 return;
1312
1313 if (!is_new && value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
1314 return;
1315
1316 if (is_new)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001317 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001318 d1 = dict_copy(d1, FALSE, TRUE, get_copyID());
1319 if (d1 == NULL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001320 return;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001321 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001322
1323 // Check the third argument.
1324 if (argvars[2].v_type != VAR_UNKNOWN)
1325 {
1326 static char *(av[]) = {"keep", "force", "error"};
1327
1328 action = tv_get_string_chk(&argvars[2]);
1329 if (action == NULL)
Christian Brabandt29269a72024-04-16 22:44:31 +02001330 {
1331 if (is_new)
1332 dict_unref(d1);
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001333 return;
Christian Brabandt29269a72024-04-16 22:44:31 +02001334 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001335 for (i = 0; i < 3; ++i)
1336 if (STRCMP(action, av[i]) == 0)
1337 break;
1338 if (i == 3)
1339 {
Christian Brabandt29269a72024-04-16 22:44:31 +02001340 if (is_new)
1341 dict_unref(d1);
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001342 semsg(_(e_invalid_argument_str), action);
1343 return;
1344 }
1345 }
1346 else
1347 action = (char_u *)"force";
1348
1349 if (type != NULL && check_typval_arg_type(type, &argvars[1],
Ernie Raele6d40dc2023-03-19 21:23:38 +00001350 func_name, 2) == FAIL)
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001351 return;
1352 dict_extend(d1, d2, action, func_name);
1353
1354 if (is_new)
1355 {
1356 rettv->v_type = VAR_DICT;
1357 rettv->vval.v_dict = d1;
1358 rettv->v_lock = FALSE;
1359 }
1360 else
1361 copy_tv(&argvars[0], rettv);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001362}
1363
1364/*
Ernie Raele79e2072024-01-13 11:47:33 +01001365 * Implementation of map(), filter(), foreach() for a Dict. Apply "expr" to
1366 * every item in Dict "d" and return the result in "rettv".
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001367 */
1368 void
1369dict_filter_map(
1370 dict_T *d,
1371 filtermap_T filtermap,
1372 type_T *argtype,
1373 char *func_name,
1374 char_u *arg_errmsg,
1375 typval_T *expr,
1376 typval_T *rettv)
1377{
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001378 dict_T *d_ret = NULL;
1379 hashtab_T *ht;
1380 hashitem_T *hi;
1381 dictitem_T *di;
1382 int todo;
1383 int rem;
Bram Moolenaar82418262022-09-28 16:16:15 +01001384 typval_T newtv;
1385 funccall_T *fc;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001386
1387 if (filtermap == FILTERMAP_MAPNEW)
1388 {
1389 rettv->v_type = VAR_DICT;
1390 rettv->vval.v_dict = NULL;
1391 }
1392 if (d == NULL
1393 || (filtermap == FILTERMAP_FILTER
1394 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1395 return;
1396
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001397 if (filtermap == FILTERMAP_MAPNEW)
1398 {
1399 if (rettv_dict_alloc(rettv) == FAIL)
1400 return;
1401 d_ret = rettv->vval.v_dict;
1402 }
1403
zeertzjqe7d49462023-04-16 20:53:55 +01001404 // Create one funccall_T for all eval_expr_typval() calls.
Bram Moolenaar82418262022-09-28 16:16:15 +01001405 fc = eval_expr_get_funccal(expr, &newtv);
1406
Ernie Raele6d40dc2023-03-19 21:23:38 +00001407 int prev_lock = d->dv_lock;
1408 if (d->dv_lock == 0)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001409 d->dv_lock = VAR_LOCKED;
1410 ht = &d->dv_hashtab;
1411 hash_lock(ht);
1412 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001413 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001414 {
1415 if (!HASHITEM_EMPTY(hi))
1416 {
1417 int r;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001418
1419 --todo;
1420 di = HI2DI(hi);
1421 if (filtermap == FILTERMAP_MAP
1422 && (value_check_lock(di->di_tv.v_lock,
1423 arg_errmsg, TRUE)
1424 || var_check_ro(di->di_flags,
1425 arg_errmsg, TRUE)))
1426 break;
1427 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar82418262022-09-28 16:16:15 +01001428 r = filter_map_one(&di->di_tv, expr, filtermap, fc, &newtv, &rem);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001429 clear_tv(get_vim_var_tv(VV_KEY));
1430 if (r == FAIL || did_emsg)
1431 {
1432 clear_tv(&newtv);
1433 break;
1434 }
1435 if (filtermap == FILTERMAP_MAP)
1436 {
1437 if (argtype != NULL && check_typval_arg_type(
Bram Moolenaar078a4612022-01-04 15:17:03 +00001438 argtype->tt_member, &newtv, func_name, 0) == FAIL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001439 {
1440 clear_tv(&newtv);
1441 break;
1442 }
1443 // map(): replace the dict item value
1444 clear_tv(&di->di_tv);
1445 newtv.v_lock = 0;
1446 di->di_tv = newtv;
1447 }
1448 else if (filtermap == FILTERMAP_MAPNEW)
1449 {
1450 // mapnew(): add the item value to the new dict
1451 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
1452 clear_tv(&newtv);
1453 if (r == FAIL)
1454 break;
1455 }
1456 else if (filtermap == FILTERMAP_FILTER && rem)
1457 {
1458 // filter(false): remove the item from the dict
1459 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1460 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1461 break;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001462 dictitem_remove(d, di, "filter");
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001463 }
1464 }
1465 }
1466 hash_unlock(ht);
1467 d->dv_lock = prev_lock;
Bram Moolenaar82418262022-09-28 16:16:15 +01001468 if (fc != NULL)
1469 remove_funccal();
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001470}
1471
1472/*
1473 * "remove({dict})" function
1474 */
1475 void
1476dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1477{
1478 dict_T *d;
1479 char_u *key;
1480 dictitem_T *di;
1481
1482 if (argvars[2].v_type != VAR_UNKNOWN)
1483 {
1484 semsg(_(e_too_many_arguments_for_function_str), "remove()");
1485 return;
1486 }
1487
1488 d = argvars[0].vval.v_dict;
1489 if (d == NULL || value_check_lock(d->dv_lock, arg_errmsg, TRUE))
1490 return;
1491
1492 key = tv_get_string_chk(&argvars[1]);
1493 if (key == NULL)
1494 return;
1495
1496 di = dict_find(d, key, -1);
1497 if (di == NULL)
1498 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001499 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001500 return;
1501 }
1502
1503 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1504 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1505 return;
1506
1507 *rettv = di->di_tv;
1508 init_tv(&di->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001509 dictitem_remove(d, di, "remove()");
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001510}
1511
Bram Moolenaar976f8592022-08-30 14:34:52 +01001512typedef enum {
1513 DICT2LIST_KEYS,
1514 DICT2LIST_VALUES,
1515 DICT2LIST_ITEMS,
1516} dict2list_T;
1517
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001518/*
Bram Moolenaar976f8592022-08-30 14:34:52 +01001519 * Turn a dict into a list.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001520 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001521 static void
Bram Moolenaar976f8592022-08-30 14:34:52 +01001522dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001523{
1524 list_T *l2;
1525 dictitem_T *di;
1526 hashitem_T *hi;
1527 listitem_T *li;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001528 dict_T *d;
1529 int todo;
1530
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001531 if (rettv_list_alloc(rettv) == FAIL)
1532 return;
1533
Bram Moolenaar976f8592022-08-30 14:34:52 +01001534 if ((what == DICT2LIST_ITEMS
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001535 ? check_for_string_or_list_or_dict_arg(argvars, 0)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001536 : check_for_dict_arg(argvars, 0)) == FAIL)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001537 return;
1538
Bram Moolenaar976f8592022-08-30 14:34:52 +01001539 d = argvars[0].vval.v_dict;
1540 if (d == NULL)
zeertzjqfc305842023-08-19 13:27:03 +02001541 // NULL dict behaves like an empty dict
Bram Moolenaaref982572021-08-12 19:27:57 +02001542 return;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001543
1544 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001545 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001546 {
1547 if (!HASHITEM_EMPTY(hi))
1548 {
1549 --todo;
1550 di = HI2DI(hi);
1551
1552 li = listitem_alloc();
1553 if (li == NULL)
1554 break;
1555 list_append(rettv->vval.v_list, li);
1556
Bram Moolenaar976f8592022-08-30 14:34:52 +01001557 if (what == DICT2LIST_KEYS)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001558 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001559 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001560 li->li_tv.v_type = VAR_STRING;
1561 li->li_tv.v_lock = 0;
1562 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1563 }
Bram Moolenaar976f8592022-08-30 14:34:52 +01001564 else if (what == DICT2LIST_VALUES)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001567 copy_tv(&di->di_tv, &li->li_tv);
1568 }
1569 else
1570 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001571 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001572 l2 = list_alloc();
1573 li->li_tv.v_type = VAR_LIST;
1574 li->li_tv.v_lock = 0;
1575 li->li_tv.vval.v_list = l2;
1576 if (l2 == NULL)
1577 break;
1578 ++l2->lv_refcount;
1579
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001580 if (list_append_string(l2, di->di_key, -1) == FAIL
1581 || list_append_tv(l2, &di->di_tv) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001582 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001583 }
1584 }
1585 }
1586}
1587
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001588/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001589 * "items(dict)" function
1590 */
1591 void
1592f_items(typval_T *argvars, typval_T *rettv)
1593{
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001594 if (argvars[0].v_type == VAR_STRING)
1595 string2items(argvars, rettv);
1596 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001597 list2items(argvars, rettv);
1598 else
1599 dict2list(argvars, rettv, DICT2LIST_ITEMS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001600}
1601
1602/*
1603 * "keys()" function
1604 */
1605 void
1606f_keys(typval_T *argvars, typval_T *rettv)
1607{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001608 dict2list(argvars, rettv, DICT2LIST_KEYS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001609}
1610
1611/*
1612 * "values(dict)" function
1613 */
1614 void
1615f_values(typval_T *argvars, typval_T *rettv)
1616{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001617 dict2list(argvars, rettv, DICT2LIST_VALUES);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001618}
1619
1620/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001621 * Make each item in the dict readonly (not the value of the item).
1622 */
1623 void
1624dict_set_items_ro(dict_T *di)
1625{
1626 int todo = (int)di->dv_hashtab.ht_used;
1627 hashitem_T *hi;
1628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 // Set readonly
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001630 FOR_ALL_HASHTAB_ITEMS(&di->dv_hashtab, hi, todo)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001631 {
1632 if (HASHITEM_EMPTY(hi))
1633 continue;
1634 --todo;
1635 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1636 }
1637}
1638
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001639/*
1640 * "has_key()" function
1641 */
1642 void
1643f_has_key(typval_T *argvars, typval_T *rettv)
1644{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001645 if (in_vim9script()
1646 && (check_for_dict_arg(argvars, 0) == FAIL
1647 || check_for_string_or_number_arg(argvars, 1) == FAIL))
1648 return;
1649
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001650 if (check_for_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001651 return;
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001652
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001653 if (argvars[0].vval.v_dict == NULL)
1654 return;
1655
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001656 rettv->vval.v_number = dict_has_key(argvars[0].vval.v_dict,
1657 (char *)tv_get_string(&argvars[1]));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001658}
1659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001660#endif // defined(FEAT_EVAL)