blob: c78995d80e51d6d87d4673a982d556b1a20b00e8 [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/*
1095 * Go over all entries in "d2" and add them to "d1".
1096 * When "action" is "error" then a duplicate key is an error.
1097 * When "action" is "force" then a duplicate key is overwritten.
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001098 * When "action" is "move" then move items instead of copying.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001099 * Otherwise duplicate keys are ignored ("action" is "keep").
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001100 * "func_name" is used for reporting where an error occurred.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001101 */
1102 void
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001103dict_extend(dict_T *d1, dict_T *d2, char_u *action, char *func_name)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001104{
1105 dictitem_T *di1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001106 int todo;
1107 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001108 type_T *type;
1109
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001110 if (check_hashtab_frozen(&d1->dv_hashtab, "extend"))
1111 return;
1112
1113 if (*action == 'm')
1114 {
1115 if (check_hashtab_frozen(&d2->dv_hashtab, "extend"))
1116 return;
1117 hash_lock(&d2->dv_hashtab); // don't rehash on hash_remove()
1118 }
1119
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001120 if (d1->dv_type != NULL && d1->dv_type->tt_member != NULL)
1121 type = d1->dv_type->tt_member;
1122 else
1123 type = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001124
1125 todo = (int)d2->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001126 hashitem_T *hi2;
1127 FOR_ALL_HASHTAB_ITEMS(&d2->dv_hashtab, hi2, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001128 {
1129 if (!HASHITEM_EMPTY(hi2))
1130 {
1131 --todo;
1132 di1 = dict_find(d1, hi2->hi_key, -1);
zeertzjq91c75d12022-11-05 20:21:58 +00001133 // Check the key to be valid when adding to any scope.
1134 if (d1->dv_scope != 0 && !valid_varname(hi2->hi_key, -1, TRUE))
1135 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001136
1137 if (type != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001138 && check_typval_arg_type(type, &HI2DI(hi2)->di_tv,
1139 func_name, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001140 break;
1141
Bram Moolenaarcd524592016-07-17 14:57:05 +02001142 if (di1 == NULL)
1143 {
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001144 if (*action == 'm')
1145 {
Bram Moolenaarbc222152022-11-23 11:34:01 +00001146 // Cheap way to move a dict item from "d2" to "d1".
1147 // If dict_add() fails then "d2" won't be empty.
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001148 di1 = HI2DI(hi2);
Bram Moolenaarbc222152022-11-23 11:34:01 +00001149 if (dict_add(d1, di1) == OK)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001150 hash_remove(&d2->dv_hashtab, hi2, "extend");
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001151 }
1152 else
1153 {
1154 di1 = dictitem_copy(HI2DI(hi2));
1155 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1156 dictitem_free(di1);
1157 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001158 }
1159 else if (*action == 'e')
1160 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001161 semsg(_(e_key_already_exists_str), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001162 break;
1163 }
1164 else if (*action == 'f' && HI2DI(hi2) != di1)
1165 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001166 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001167 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001168 break;
zeertzjq91c75d12022-11-05 20:21:58 +00001169 // Disallow replacing a builtin function.
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001170 if (dict_wrong_func_name(d1, &HI2DI(hi2)->di_tv, hi2->hi_key))
1171 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001172 clear_tv(&di1->di_tv);
1173 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1174 }
1175 }
1176 }
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001177
1178 if (*action == 'm')
1179 hash_unlock(&d2->dv_hashtab);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001180}
1181
1182/*
1183 * Return the dictitem that an entry in a hashtable points to.
1184 */
1185 dictitem_T *
1186dict_lookup(hashitem_T *hi)
1187{
1188 return HI2DI(hi);
1189}
1190
1191/*
1192 * Return TRUE when two dictionaries have exactly the same key/values.
1193 */
1194 int
1195dict_equal(
1196 dict_T *d1,
1197 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001198 int ic, // ignore case for strings
1199 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001200{
1201 hashitem_T *hi;
1202 dictitem_T *item2;
1203 int todo;
1204
Bram Moolenaarcd524592016-07-17 14:57:05 +02001205 if (d1 == d2)
1206 return TRUE;
1207 if (dict_len(d1) != dict_len(d2))
1208 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001209 if (dict_len(d1) == 0)
1210 // empty and NULL dicts are considered equal
1211 return TRUE;
1212 if (d1 == NULL || d2 == NULL)
1213 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001214
1215 todo = (int)d1->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001216 FOR_ALL_HASHTAB_ITEMS(&d1->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001217 {
1218 if (!HASHITEM_EMPTY(hi))
1219 {
1220 item2 = dict_find(d2, hi->hi_key, -1);
1221 if (item2 == NULL)
1222 return FALSE;
1223 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1224 return FALSE;
1225 --todo;
1226 }
1227 }
1228 return TRUE;
1229}
1230
1231/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001232 * Count the number of times item "needle" occurs in Dict "d". Case is ignored
1233 * if "ic" is TRUE.
1234 */
1235 long
1236dict_count(dict_T *d, typval_T *needle, int ic)
1237{
1238 int todo;
1239 hashitem_T *hi;
1240 long n = 0;
1241
1242 if (d == NULL)
1243 return 0;
1244
1245 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001246 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001247 {
1248 if (!HASHITEM_EMPTY(hi))
1249 {
1250 --todo;
1251 if (tv_equal(&HI2DI(hi)->di_tv, needle, ic, FALSE))
1252 ++n;
1253 }
1254 }
1255
1256 return n;
1257}
1258
1259/*
1260 * extend() a Dict. Append Dict argvars[1] to Dict argvars[0] and return the
1261 * resulting Dict in "rettv". "is_new" is TRUE for extendnew().
1262 */
1263 void
1264dict_extend_func(
1265 typval_T *argvars,
1266 type_T *type,
1267 char *func_name,
1268 char_u *arg_errmsg,
1269 int is_new,
1270 typval_T *rettv)
1271{
1272 dict_T *d1, *d2;
1273 char_u *action;
1274 int i;
1275
1276 d1 = argvars[0].vval.v_dict;
1277 if (d1 == NULL)
1278 {
1279 emsg(_(e_cannot_extend_null_dict));
1280 return;
1281 }
1282 d2 = argvars[1].vval.v_dict;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001283 if (d2 == NULL)
1284 return;
1285
1286 if (!is_new && value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
1287 return;
1288
1289 if (is_new)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001290 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001291 d1 = dict_copy(d1, FALSE, TRUE, get_copyID());
1292 if (d1 == NULL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001293 return;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001294 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001295
1296 // Check the third argument.
1297 if (argvars[2].v_type != VAR_UNKNOWN)
1298 {
1299 static char *(av[]) = {"keep", "force", "error"};
1300
1301 action = tv_get_string_chk(&argvars[2]);
1302 if (action == NULL)
Christian Brabandt29269a72024-04-16 22:44:31 +02001303 {
1304 if (is_new)
1305 dict_unref(d1);
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001306 return;
Christian Brabandt29269a72024-04-16 22:44:31 +02001307 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001308 for (i = 0; i < 3; ++i)
1309 if (STRCMP(action, av[i]) == 0)
1310 break;
1311 if (i == 3)
1312 {
Christian Brabandt29269a72024-04-16 22:44:31 +02001313 if (is_new)
1314 dict_unref(d1);
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001315 semsg(_(e_invalid_argument_str), action);
1316 return;
1317 }
1318 }
1319 else
1320 action = (char_u *)"force";
1321
1322 if (type != NULL && check_typval_arg_type(type, &argvars[1],
Ernie Raele6d40dc2023-03-19 21:23:38 +00001323 func_name, 2) == FAIL)
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00001324 return;
1325 dict_extend(d1, d2, action, func_name);
1326
1327 if (is_new)
1328 {
1329 rettv->v_type = VAR_DICT;
1330 rettv->vval.v_dict = d1;
1331 rettv->v_lock = FALSE;
1332 }
1333 else
1334 copy_tv(&argvars[0], rettv);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001335}
1336
1337/*
Ernie Raele79e2072024-01-13 11:47:33 +01001338 * Implementation of map(), filter(), foreach() for a Dict. Apply "expr" to
1339 * every item in Dict "d" and return the result in "rettv".
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001340 */
1341 void
1342dict_filter_map(
1343 dict_T *d,
1344 filtermap_T filtermap,
1345 type_T *argtype,
1346 char *func_name,
1347 char_u *arg_errmsg,
1348 typval_T *expr,
1349 typval_T *rettv)
1350{
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001351 dict_T *d_ret = NULL;
1352 hashtab_T *ht;
1353 hashitem_T *hi;
1354 dictitem_T *di;
1355 int todo;
1356 int rem;
Bram Moolenaar82418262022-09-28 16:16:15 +01001357 typval_T newtv;
1358 funccall_T *fc;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001359
1360 if (filtermap == FILTERMAP_MAPNEW)
1361 {
1362 rettv->v_type = VAR_DICT;
1363 rettv->vval.v_dict = NULL;
1364 }
1365 if (d == NULL
1366 || (filtermap == FILTERMAP_FILTER
1367 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1368 return;
1369
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001370 if (filtermap == FILTERMAP_MAPNEW)
1371 {
1372 if (rettv_dict_alloc(rettv) == FAIL)
1373 return;
1374 d_ret = rettv->vval.v_dict;
1375 }
1376
zeertzjqe7d49462023-04-16 20:53:55 +01001377 // Create one funccall_T for all eval_expr_typval() calls.
Bram Moolenaar82418262022-09-28 16:16:15 +01001378 fc = eval_expr_get_funccal(expr, &newtv);
1379
Ernie Raele6d40dc2023-03-19 21:23:38 +00001380 int prev_lock = d->dv_lock;
1381 if (d->dv_lock == 0)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001382 d->dv_lock = VAR_LOCKED;
1383 ht = &d->dv_hashtab;
1384 hash_lock(ht);
1385 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001386 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001387 {
1388 if (!HASHITEM_EMPTY(hi))
1389 {
1390 int r;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001391
1392 --todo;
1393 di = HI2DI(hi);
1394 if (filtermap == FILTERMAP_MAP
1395 && (value_check_lock(di->di_tv.v_lock,
1396 arg_errmsg, TRUE)
1397 || var_check_ro(di->di_flags,
1398 arg_errmsg, TRUE)))
1399 break;
1400 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar82418262022-09-28 16:16:15 +01001401 r = filter_map_one(&di->di_tv, expr, filtermap, fc, &newtv, &rem);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001402 clear_tv(get_vim_var_tv(VV_KEY));
1403 if (r == FAIL || did_emsg)
1404 {
1405 clear_tv(&newtv);
1406 break;
1407 }
1408 if (filtermap == FILTERMAP_MAP)
1409 {
1410 if (argtype != NULL && check_typval_arg_type(
Bram Moolenaar078a4612022-01-04 15:17:03 +00001411 argtype->tt_member, &newtv, func_name, 0) == FAIL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001412 {
1413 clear_tv(&newtv);
1414 break;
1415 }
1416 // map(): replace the dict item value
1417 clear_tv(&di->di_tv);
1418 newtv.v_lock = 0;
1419 di->di_tv = newtv;
1420 }
1421 else if (filtermap == FILTERMAP_MAPNEW)
1422 {
1423 // mapnew(): add the item value to the new dict
1424 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
1425 clear_tv(&newtv);
1426 if (r == FAIL)
1427 break;
1428 }
1429 else if (filtermap == FILTERMAP_FILTER && rem)
1430 {
1431 // filter(false): remove the item from the dict
1432 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1433 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1434 break;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001435 dictitem_remove(d, di, "filter");
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001436 }
1437 }
1438 }
1439 hash_unlock(ht);
1440 d->dv_lock = prev_lock;
Bram Moolenaar82418262022-09-28 16:16:15 +01001441 if (fc != NULL)
1442 remove_funccal();
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001443}
1444
1445/*
1446 * "remove({dict})" function
1447 */
1448 void
1449dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1450{
1451 dict_T *d;
1452 char_u *key;
1453 dictitem_T *di;
1454
1455 if (argvars[2].v_type != VAR_UNKNOWN)
1456 {
1457 semsg(_(e_too_many_arguments_for_function_str), "remove()");
1458 return;
1459 }
1460
1461 d = argvars[0].vval.v_dict;
1462 if (d == NULL || value_check_lock(d->dv_lock, arg_errmsg, TRUE))
1463 return;
1464
1465 key = tv_get_string_chk(&argvars[1]);
1466 if (key == NULL)
1467 return;
1468
1469 di = dict_find(d, key, -1);
1470 if (di == NULL)
1471 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001472 semsg(_(e_key_not_present_in_dictionary_str), key);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001473 return;
1474 }
1475
1476 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1477 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1478 return;
1479
1480 *rettv = di->di_tv;
1481 init_tv(&di->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001482 dictitem_remove(d, di, "remove()");
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001483}
1484
Bram Moolenaar976f8592022-08-30 14:34:52 +01001485typedef enum {
1486 DICT2LIST_KEYS,
1487 DICT2LIST_VALUES,
1488 DICT2LIST_ITEMS,
1489} dict2list_T;
1490
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001491/*
Bram Moolenaar976f8592022-08-30 14:34:52 +01001492 * Turn a dict into a list.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001493 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001494 static void
Bram Moolenaar976f8592022-08-30 14:34:52 +01001495dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001496{
1497 list_T *l2;
1498 dictitem_T *di;
1499 hashitem_T *hi;
1500 listitem_T *li;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001501 dict_T *d;
1502 int todo;
1503
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001504 if (rettv_list_alloc(rettv) == FAIL)
1505 return;
1506
Bram Moolenaar976f8592022-08-30 14:34:52 +01001507 if ((what == DICT2LIST_ITEMS
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001508 ? check_for_string_or_list_or_dict_arg(argvars, 0)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001509 : check_for_dict_arg(argvars, 0)) == FAIL)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001510 return;
1511
Bram Moolenaar976f8592022-08-30 14:34:52 +01001512 d = argvars[0].vval.v_dict;
1513 if (d == NULL)
zeertzjqfc305842023-08-19 13:27:03 +02001514 // NULL dict behaves like an empty dict
Bram Moolenaaref982572021-08-12 19:27:57 +02001515 return;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001516
1517 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001518 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001519 {
1520 if (!HASHITEM_EMPTY(hi))
1521 {
1522 --todo;
1523 di = HI2DI(hi);
1524
1525 li = listitem_alloc();
1526 if (li == NULL)
1527 break;
1528 list_append(rettv->vval.v_list, li);
1529
Bram Moolenaar976f8592022-08-30 14:34:52 +01001530 if (what == DICT2LIST_KEYS)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001531 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001532 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001533 li->li_tv.v_type = VAR_STRING;
1534 li->li_tv.v_lock = 0;
1535 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1536 }
Bram Moolenaar976f8592022-08-30 14:34:52 +01001537 else if (what == DICT2LIST_VALUES)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001539 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001540 copy_tv(&di->di_tv, &li->li_tv);
1541 }
1542 else
1543 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001544 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001545 l2 = list_alloc();
1546 li->li_tv.v_type = VAR_LIST;
1547 li->li_tv.v_lock = 0;
1548 li->li_tv.vval.v_list = l2;
1549 if (l2 == NULL)
1550 break;
1551 ++l2->lv_refcount;
1552
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001553 if (list_append_string(l2, di->di_key, -1) == FAIL
1554 || list_append_tv(l2, &di->di_tv) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001555 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001556 }
1557 }
1558 }
1559}
1560
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001561/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001562 * "items(dict)" function
1563 */
1564 void
1565f_items(typval_T *argvars, typval_T *rettv)
1566{
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001567 if (argvars[0].v_type == VAR_STRING)
1568 string2items(argvars, rettv);
1569 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001570 list2items(argvars, rettv);
1571 else
1572 dict2list(argvars, rettv, DICT2LIST_ITEMS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001573}
1574
1575/*
1576 * "keys()" function
1577 */
1578 void
1579f_keys(typval_T *argvars, typval_T *rettv)
1580{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001581 dict2list(argvars, rettv, DICT2LIST_KEYS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001582}
1583
1584/*
1585 * "values(dict)" function
1586 */
1587 void
1588f_values(typval_T *argvars, typval_T *rettv)
1589{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001590 dict2list(argvars, rettv, DICT2LIST_VALUES);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001591}
1592
1593/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001594 * Make each item in the dict readonly (not the value of the item).
1595 */
1596 void
1597dict_set_items_ro(dict_T *di)
1598{
1599 int todo = (int)di->dv_hashtab.ht_used;
1600 hashitem_T *hi;
1601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001602 // Set readonly
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001603 FOR_ALL_HASHTAB_ITEMS(&di->dv_hashtab, hi, todo)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001604 {
1605 if (HASHITEM_EMPTY(hi))
1606 continue;
1607 --todo;
1608 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1609 }
1610}
1611
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001612/*
1613 * "has_key()" function
1614 */
1615 void
1616f_has_key(typval_T *argvars, typval_T *rettv)
1617{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001618 if (in_vim9script()
1619 && (check_for_dict_arg(argvars, 0) == FAIL
1620 || check_for_string_or_number_arg(argvars, 1) == FAIL))
1621 return;
1622
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001623 if (check_for_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001624 return;
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001625
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001626 if (argvars[0].vval.v_dict == NULL)
1627 return;
1628
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001629 rettv->vval.v_number = dict_has_key(argvars[0].vval.v_dict,
1630 (char *)tv_get_string(&argvars[1]));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001631}
1632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001633#endif // defined(FEAT_EVAL)