blob: c2f0fcc4d9b8f30ad1302cc67b748f152fdef44a [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);
Bram Moolenaarcd524592016-07-17 14:57:05 +020033 if (d != NULL)
34 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010035 // Add the dict to the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +020036 if (first_dict != NULL)
37 first_dict->dv_used_prev = d;
38 d->dv_used_next = first_dict;
39 d->dv_used_prev = NULL;
40 first_dict = d;
41
42 hash_init(&d->dv_hashtab);
43 d->dv_lock = 0;
44 d->dv_scope = 0;
45 d->dv_refcount = 0;
46 d->dv_copyID = 0;
47 }
48 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 Moolenaar5d18efe2019-12-01 21:11:22 +0100125 // Lock the hashtab, we don't want it to resize while freeing items.
Bram Moolenaar89483d42020-05-10 15:24:44 +0200126 hash_lock(ht);
127 todo = (int)ht->ht_used;
128 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200129 {
130 if (!HASHITEM_EMPTY(hi))
131 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100132 // Remove the item before deleting it, just in case there is
133 // something recursive causing trouble.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200134 di = HI2DI(hi);
Bram Moolenaar89483d42020-05-10 15:24:44 +0200135 hash_remove(ht, hi);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100136 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200137 --todo;
138 }
139 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100140
Bram Moolenaar89483d42020-05-10 15:24:44 +0200141 // The hashtab is still locked, it has to be re-initialized anyway.
142 hash_clear(ht);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200143}
144
145 static void
146dict_free_dict(dict_T *d)
147{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100148 // Remove the dict from the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200149 if (d->dv_used_prev == NULL)
150 first_dict = d->dv_used_next;
151 else
152 d->dv_used_prev->dv_used_next = d->dv_used_next;
153 if (d->dv_used_next != NULL)
154 d->dv_used_next->dv_used_prev = d->dv_used_prev;
155 vim_free(d);
156}
157
158 static void
159dict_free(dict_T *d)
160{
161 if (!in_free_unref_items)
162 {
163 dict_free_contents(d);
164 dict_free_dict(d);
165 }
166}
167
168/*
169 * Unreference a Dictionary: decrement the reference count and free it when it
170 * becomes zero.
171 */
172 void
173dict_unref(dict_T *d)
174{
175 if (d != NULL && --d->dv_refcount <= 0)
176 dict_free(d);
177}
178
179/*
180 * Go through the list of dicts and free items without the copyID.
181 * Returns TRUE if something was freed.
182 */
183 int
184dict_free_nonref(int copyID)
185{
186 dict_T *dd;
187 int did_free = FALSE;
188
189 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
190 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100192 // Free the Dictionary and ordinary items it contains, but don't
193 // recurse into Lists and Dictionaries, they will be in the list
194 // of dicts or list of lists.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200195 dict_free_contents(dd);
196 did_free = TRUE;
197 }
198 return did_free;
199}
200
201 void
202dict_free_items(int copyID)
203{
204 dict_T *dd, *dd_next;
205
206 for (dd = first_dict; dd != NULL; dd = dd_next)
207 {
208 dd_next = dd->dv_used_next;
209 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
210 dict_free_dict(dd);
211 }
212}
213
214/*
215 * Allocate a Dictionary item.
216 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200217 * Note that the type and value of the item "di_tv" still needs to be
218 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200219 * Returns NULL when out of memory.
220 */
221 dictitem_T *
222dictitem_alloc(char_u *key)
223{
224 dictitem_T *di;
Bram Moolenaarb4168fd2021-12-22 20:29:09 +0000225 size_t len = STRLEN(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200226
Bram Moolenaarb4168fd2021-12-22 20:29:09 +0000227 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200228 if (di != NULL)
229 {
Bram Moolenaarb4168fd2021-12-22 20:29:09 +0000230 mch_memmove(di->di_key, key, len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200231 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200232 di->di_tv.v_lock = 0;
Bram Moolenaar3b318512021-09-06 19:19:45 +0200233 di->di_tv.v_type = VAR_UNKNOWN;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200234 }
235 return di;
236}
237
238/*
239 * Make a copy of a Dictionary item.
240 */
241 static dictitem_T *
242dictitem_copy(dictitem_T *org)
243{
244 dictitem_T *di;
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200245 size_t len = STRLEN(org->di_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200246
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200247 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200248 if (di != NULL)
249 {
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200250 mch_memmove(di->di_key, org->di_key, len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200251 di->di_flags = DI_FLAGS_ALLOC;
252 copy_tv(&org->di_tv, &di->di_tv);
253 }
254 return di;
255}
256
257/*
258 * Remove item "item" from Dictionary "dict" and free it.
259 */
260 void
261dictitem_remove(dict_T *dict, dictitem_T *item)
262{
263 hashitem_T *hi;
264
265 hi = hash_find(&dict->dv_hashtab, item->di_key);
266 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100267 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200268 else
269 hash_remove(&dict->dv_hashtab, hi);
270 dictitem_free(item);
271}
272
273/*
274 * Free a dict item. Also clears the value.
275 */
276 void
277dictitem_free(dictitem_T *item)
278{
279 clear_tv(&item->di_tv);
280 if (item->di_flags & DI_FLAGS_ALLOC)
281 vim_free(item);
282}
283
284/*
285 * Make a copy of dict "d". Shallow if "deep" is FALSE.
286 * The refcount of the new dict is set to 1.
Bram Moolenaar381692b2022-02-02 20:01:27 +0000287 * See item_copy() for "top" and "copyID".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200288 * Returns NULL when out of memory.
289 */
290 dict_T *
Bram Moolenaar381692b2022-02-02 20:01:27 +0000291dict_copy(dict_T *orig, int deep, int top, int copyID)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200292{
293 dict_T *copy;
294 dictitem_T *di;
295 int todo;
296 hashitem_T *hi;
297
298 if (orig == NULL)
299 return NULL;
300
301 copy = dict_alloc();
302 if (copy != NULL)
303 {
304 if (copyID != 0)
305 {
306 orig->dv_copyID = copyID;
307 orig->dv_copydict = copy;
308 }
Bram Moolenaare0c2b2c2022-02-05 14:05:51 +0000309 if (orig->dv_type == NULL || top || deep)
310 copy->dv_type = NULL;
311 else
312 copy->dv_type = alloc_type(orig->dv_type);
Bram Moolenaar381692b2022-02-02 20:01:27 +0000313
Bram Moolenaarcd524592016-07-17 14:57:05 +0200314 todo = (int)orig->dv_hashtab.ht_used;
315 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
316 {
317 if (!HASHITEM_EMPTY(hi))
318 {
319 --todo;
320
321 di = dictitem_alloc(hi->hi_key);
322 if (di == NULL)
323 break;
324 if (deep)
325 {
Bram Moolenaar381692b2022-02-02 20:01:27 +0000326 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv,
327 deep, FALSE, copyID) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200328 {
329 vim_free(di);
330 break;
331 }
332 }
333 else
334 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
335 if (dict_add(copy, di) == FAIL)
336 {
337 dictitem_free(di);
338 break;
339 }
340 }
341 }
342
343 ++copy->dv_refcount;
344 if (todo > 0)
345 {
346 dict_unref(copy);
347 copy = NULL;
348 }
349 }
350
351 return copy;
352}
353
354/*
zeertzjq91c75d12022-11-05 20:21:58 +0000355 * Check for adding a function to g: or s: (in Vim9 script) or l:.
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200356 * If the name is wrong give an error message and return TRUE.
357 */
358 int
359dict_wrong_func_name(dict_T *d, typval_T *tv, char_u *name)
360{
361 return (d == get_globvar_dict()
Bram Moolenaarb54abee2021-06-02 11:49:23 +0200362 || (in_vim9script() && SCRIPT_ID_VALID(current_sctx.sc_sid)
363 && d == &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict)
364 || &d->dv_hashtab == get_funccal_local_ht())
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200365 && (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
366 && var_wrong_func_name(name, TRUE);
367}
368
369/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200370 * Add item "item" to Dictionary "d".
371 * Returns FAIL when out of memory and when key already exists.
372 */
373 int
374dict_add(dict_T *d, dictitem_T *item)
375{
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200376 if (dict_wrong_func_name(d, &item->di_tv, item->di_key))
377 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200378 return hash_add(&d->dv_hashtab, item->di_key);
379}
380
381/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200382 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200383 * Returns FAIL when out of memory and when key already exists.
384 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200385 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100386dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200387{
388 dictitem_T *item;
389
390 item = dictitem_alloc((char_u *)key);
391 if (item == NULL)
392 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100393 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200394 item->di_tv.vval.v_number = nr;
395 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200396 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200397 dictitem_free(item);
398 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200399 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200400 return OK;
401}
402
403/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200404 * Add a number entry to dictionary "d".
405 * Returns FAIL when out of memory and when key already exists.
406 */
407 int
408dict_add_number(dict_T *d, char *key, varnumber_T nr)
409{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100410 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200411}
412
413/*
414 * Add a special entry to dictionary "d".
415 * Returns FAIL when out of memory and when key already exists.
416 */
417 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100418dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200419{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100420 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200421}
422
423/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200424 * Add a string entry to dictionary "d".
425 * Returns FAIL when out of memory and when key already exists.
426 */
427 int
428dict_add_string(dict_T *d, char *key, char_u *str)
429{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100430 return dict_add_string_len(d, key, str, -1);
431}
432
433/*
434 * Add a string entry to dictionary "d".
435 * "str" will be copied to allocated memory.
436 * When "len" is -1 use the whole string, otherwise only this many bytes.
437 * Returns FAIL when out of memory and when key already exists.
438 */
439 int
440dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
441{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200442 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100443 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200444
445 item = dictitem_alloc((char_u *)key);
446 if (item == NULL)
447 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200448 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100449 if (str != NULL)
450 {
451 if (len == -1)
452 val = vim_strsave(str);
453 else
454 val = vim_strnsave(str, len);
455 }
456 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200457 if (dict_add(d, item) == FAIL)
458 {
459 dictitem_free(item);
460 return FAIL;
461 }
462 return OK;
463}
464
465/*
466 * Add a list entry to dictionary "d".
467 * Returns FAIL when out of memory and when key already exists.
468 */
469 int
470dict_add_list(dict_T *d, char *key, list_T *list)
471{
472 dictitem_T *item;
473
474 item = dictitem_alloc((char_u *)key);
475 if (item == NULL)
476 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200477 item->di_tv.v_type = VAR_LIST;
478 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100479 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200480 if (dict_add(d, item) == FAIL)
481 {
482 dictitem_free(item);
483 return FAIL;
484 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200485 return OK;
486}
487
488/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100489 * Add a typval_T entry to dictionary "d".
490 * Returns FAIL when out of memory and when key already exists.
491 */
492 int
493dict_add_tv(dict_T *d, char *key, typval_T *tv)
494{
495 dictitem_T *item;
496
497 item = dictitem_alloc((char_u *)key);
498 if (item == NULL)
499 return FAIL;
500 copy_tv(tv, &item->di_tv);
501 if (dict_add(d, item) == FAIL)
502 {
503 dictitem_free(item);
504 return FAIL;
505 }
506 return OK;
507}
508
509/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200510 * Add a callback to dictionary "d".
511 * Returns FAIL when out of memory and when key already exists.
512 */
513 int
514dict_add_callback(dict_T *d, char *key, callback_T *cb)
515{
516 dictitem_T *item;
517
518 item = dictitem_alloc((char_u *)key);
519 if (item == NULL)
520 return FAIL;
521 put_callback(cb, &item->di_tv);
522 if (dict_add(d, item) == FAIL)
523 {
524 dictitem_free(item);
525 return FAIL;
526 }
527 return OK;
528}
529
530/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200531 * Initializes "iter" for iterating over dictionary items with
532 * dict_iterate_next().
533 * If "var" is not a Dict or an empty Dict then there will be nothing to
534 * iterate over, no error is given.
535 * NOTE: The dictionary must not change until iterating is finished!
536 */
537 void
538dict_iterate_start(typval_T *var, dict_iterator_T *iter)
539{
540 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
541 iter->dit_todo = 0;
542 else
543 {
544 dict_T *d = var->vval.v_dict;
545
546 iter->dit_todo = d->dv_hashtab.ht_used;
547 iter->dit_hi = d->dv_hashtab.ht_array;
548 }
549}
550
551/*
552 * Iterate over the items referred to by "iter". It should be initialized with
553 * dict_iterate_start().
554 * Returns a pointer to the key.
555 * "*tv_result" is set to point to the value for that key.
556 * If there are no more items, NULL is returned.
557 */
558 char_u *
559dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
560{
561 dictitem_T *di;
562 char_u *result;
563
564 if (iter->dit_todo == 0)
565 return NULL;
566
567 while (HASHITEM_EMPTY(iter->dit_hi))
568 ++iter->dit_hi;
569
570 di = HI2DI(iter->dit_hi);
571 result = di->di_key;
572 *tv_result = &di->di_tv;
573
574 --iter->dit_todo;
575 ++iter->dit_hi;
576 return result;
577}
578
579/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200580 * Add a dict entry to dictionary "d".
581 * Returns FAIL when out of memory and when key already exists.
582 */
583 int
584dict_add_dict(dict_T *d, char *key, dict_T *dict)
585{
586 dictitem_T *item;
587
588 item = dictitem_alloc((char_u *)key);
589 if (item == NULL)
590 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200591 item->di_tv.v_type = VAR_DICT;
592 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100593 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200594 if (dict_add(d, item) == FAIL)
595 {
596 dictitem_free(item);
597 return FAIL;
598 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200599 return OK;
600}
601
602/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200603 * Get the number of items in a Dictionary.
604 */
605 long
606dict_len(dict_T *d)
607{
608 if (d == NULL)
609 return 0L;
610 return (long)d->dv_hashtab.ht_used;
611}
612
613/*
614 * Find item "key[len]" in Dictionary "d".
615 * If "len" is negative use strlen(key).
616 * Returns NULL when not found.
617 */
618 dictitem_T *
619dict_find(dict_T *d, char_u *key, int len)
620{
621#define AKEYLEN 200
622 char_u buf[AKEYLEN];
623 char_u *akey;
624 char_u *tofree = NULL;
625 hashitem_T *hi;
626
627 if (d == NULL)
628 return NULL;
629 if (len < 0)
630 akey = key;
631 else if (len >= AKEYLEN)
632 {
633 tofree = akey = vim_strnsave(key, len);
634 if (akey == NULL)
635 return NULL;
636 }
637 else
638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100639 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200640 vim_strncpy(buf, key, len);
641 akey = buf;
642 }
643
644 hi = hash_find(&d->dv_hashtab, akey);
645 vim_free(tofree);
646 if (HASHITEM_EMPTY(hi))
647 return NULL;
648 return HI2DI(hi);
649}
650
651/*
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100652 * Returns TRUE if "key" is present in Dictionary "d".
653 */
654 int
655dict_has_key(dict_T *d, char *key)
656{
657 return dict_find(d, (char_u *)key, -1) != NULL;
658}
659
660/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100661 * Get a typval_T item from a dictionary and copy it into "rettv".
662 * Returns FAIL if the entry doesn't exist or out of memory.
663 */
664 int
Bram Moolenaard61efa52022-07-23 09:52:04 +0100665dict_get_tv(dict_T *d, char *key, typval_T *rettv)
Bram Moolenaar08928322020-01-04 14:32:48 +0100666{
667 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100668
Bram Moolenaard61efa52022-07-23 09:52:04 +0100669 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaar08928322020-01-04 14:32:48 +0100670 if (di == NULL)
671 return FAIL;
672 copy_tv(&di->di_tv, rettv);
673 return OK;
674}
675
676/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200677 * Get a string item from a dictionary.
678 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200679 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200680 * Returns NULL if the entry doesn't exist or out of memory.
681 */
682 char_u *
Bram Moolenaard61efa52022-07-23 09:52:04 +0100683dict_get_string(dict_T *d, char *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200684{
685 dictitem_T *di;
686 char_u *s;
687
Bram Moolenaard61efa52022-07-23 09:52:04 +0100688 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200689 if (di == NULL)
690 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100691 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200692 if (save && s != NULL)
693 s = vim_strsave(s);
694 return s;
695}
696
697/*
698 * Get a number item from a dictionary.
699 * Returns 0 if the entry doesn't exist.
700 */
701 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100702dict_get_number(dict_T *d, char *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200703{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200704 return dict_get_number_def(d, key, 0);
705}
706
707/*
708 * Get a number item from a dictionary.
709 * Returns "def" if the entry doesn't exist.
710 */
711 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100712dict_get_number_def(dict_T *d, char *key, int def)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200713{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200714 dictitem_T *di;
715
Bram Moolenaard61efa52022-07-23 09:52:04 +0100716 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200717 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200718 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100719 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200720}
721
722/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200723 * Get a number item from a dictionary.
724 * Returns 0 if the entry doesn't exist.
725 * Give an error if the entry is not a number.
726 */
727 varnumber_T
728dict_get_number_check(dict_T *d, char_u *key)
729{
730 dictitem_T *di;
731
732 di = dict_find(d, key, -1);
733 if (di == NULL)
734 return 0;
735 if (di->di_tv.v_type != VAR_NUMBER)
736 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000737 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200738 return 0;
739 }
740 return tv_get_number(&di->di_tv);
741}
742
743/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200744 * Get a bool item (number or true/false) from a dictionary.
745 * Returns "def" if the entry doesn't exist.
746 */
747 varnumber_T
Bram Moolenaard61efa52022-07-23 09:52:04 +0100748dict_get_bool(dict_T *d, char *key, int def)
Bram Moolenaar55881332020-08-18 13:04:15 +0200749{
750 dictitem_T *di;
751
Bram Moolenaard61efa52022-07-23 09:52:04 +0100752 di = dict_find(d, (char_u *)key, -1);
Bram Moolenaar55881332020-08-18 13:04:15 +0200753 if (di == NULL)
754 return def;
755 return tv_get_bool(&di->di_tv);
756}
757
758/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200759 * Return an allocated string with the string representation of a Dictionary.
760 * May return NULL.
761 */
762 char_u *
763dict2string(typval_T *tv, int copyID, int restore_copyID)
764{
765 garray_T ga;
766 int first = TRUE;
767 char_u *tofree;
768 char_u numbuf[NUMBUFLEN];
769 hashitem_T *hi;
770 char_u *s;
771 dict_T *d;
772 int todo;
773
774 if ((d = tv->vval.v_dict) == NULL)
775 return NULL;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000776 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200777 ga_append(&ga, '{');
778
779 todo = (int)d->dv_hashtab.ht_used;
780 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
781 {
782 if (!HASHITEM_EMPTY(hi))
783 {
784 --todo;
785
786 if (first)
787 first = FALSE;
788 else
789 ga_concat(&ga, (char_u *)", ");
790
791 tofree = string_quote(hi->hi_key, FALSE);
792 if (tofree != NULL)
793 {
794 ga_concat(&ga, tofree);
795 vim_free(tofree);
796 }
797 ga_concat(&ga, (char_u *)": ");
798 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
799 FALSE, restore_copyID, TRUE);
800 if (s != NULL)
801 ga_concat(&ga, s);
802 vim_free(tofree);
803 if (s == NULL || did_echo_string_emsg)
804 break;
805 line_breakcheck();
806
807 }
808 }
809 if (todo > 0)
810 {
811 vim_free(ga.ga_data);
812 return NULL;
813 }
814
815 ga_append(&ga, '}');
816 ga_append(&ga, NUL);
817 return (char_u *)ga.ga_data;
818}
819
820/*
Bram Moolenaare0de1712020-12-02 17:36:54 +0100821 * Advance over a literal key, including "-". If the first character is not a
822 * literal key character then "key" is returned.
823 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200824 static char_u *
Bram Moolenaare0de1712020-12-02 17:36:54 +0100825skip_literal_key(char_u *key)
826{
827 char_u *p;
828
829 for (p = key; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
830 ;
831 return p;
832}
833
834/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200835 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200836 * Return FAIL when there is no valid key.
837 */
838 static int
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100839get_literal_key_tv(char_u **arg, typval_T *tv)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200840{
Bram Moolenaare0de1712020-12-02 17:36:54 +0100841 char_u *p = skip_literal_key(*arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200842
Bram Moolenaare0de1712020-12-02 17:36:54 +0100843 if (p == *arg)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200844 return FAIL;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200845 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200846 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200847
Bram Moolenaardb199212020-08-12 18:01:53 +0200848 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200849 return OK;
850}
851
852/*
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100853 * Get a literal key for a Vim9 dict:
854 * {"name": value},
855 * {'name': value},
856 * {name: value} use "name" as a literal key
857 * Return the key in allocated memory or NULL in the case of an error.
858 * "arg" is advanced to just after the key.
859 */
860 char_u *
861get_literal_key(char_u **arg)
862{
863 char_u *key;
864 char_u *end;
865 typval_T rettv;
866
867 if (**arg == '\'')
868 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100869 if (eval_lit_string(arg, &rettv, TRUE, FALSE) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100870 return NULL;
871 key = rettv.vval.v_string;
872 }
873 else if (**arg == '"')
874 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100875 if (eval_string(arg, &rettv, TRUE, FALSE) == FAIL)
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100876 return NULL;
877 key = rettv.vval.v_string;
878 }
879 else
880 {
881 end = skip_literal_key(*arg);
882 if (end == *arg)
883 {
884 semsg(_(e_invalid_key_str), *arg);
885 return NULL;
886 }
887 key = vim_strnsave(*arg, end - *arg);
888 *arg = end;
889 }
890 return key;
891}
892
893/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200894 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200895 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200896 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200897 * Return OK or FAIL. Returns NOTDONE for {expr}.
898 */
899 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200900eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200901{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200902 int evaluate = evalarg == NULL ? FALSE
903 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200904 dict_T *d = NULL;
905 typval_T tvkey;
906 typval_T tv;
907 char_u *key = NULL;
908 dictitem_T *item;
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000909 char_u *curly_expr = skipwhite(*arg + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200910 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200911 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200912 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200913
ii147c7e1e92022-09-07 19:40:17 +0100914 // First check if it's not a curly-braces expression: {expr}.
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000915 // Must do this without evaluating, otherwise a function may be called
916 // twice. Unfortunately this means we need to call eval1() twice for the
917 // first item.
ii147c7e1e92022-09-07 19:40:17 +0100918 // "{}" is an empty Dictionary.
919 // "#{abc}" is never a curly-braces expression.
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000920 if (!vim9script
921 && *curly_expr != '}'
ii147c7e1e92022-09-07 19:40:17 +0100922 && !literal
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000923 && eval1(&curly_expr, &tv, NULL) == OK
924 && *skipwhite(curly_expr) == '}')
925 return NOTDONE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200926
927 if (evaluate)
928 {
929 d = dict_alloc();
930 if (d == NULL)
931 return FAIL;
932 }
933 tvkey.v_type = VAR_UNKNOWN;
934 tv.v_type = VAR_UNKNOWN;
935
Bram Moolenaar962d7212020-07-04 14:15:00 +0200936 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200937 while (**arg != '}' && **arg != NUL)
938 {
Bram Moolenaare0de1712020-12-02 17:36:54 +0100939 int has_bracket = vim9script && **arg == '[';
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100940
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100941 if (literal)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100942 {
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100943 if (get_literal_key_tv(arg, &tvkey) == FAIL)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100944 goto failret;
945 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100946 else if (vim9script && !has_bracket)
947 {
948 tvkey.vval.v_string = get_literal_key(arg);
949 if (tvkey.vval.v_string == NULL)
950 goto failret;
951 tvkey.v_type = VAR_STRING;
952 }
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100953 else
954 {
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100955 if (has_bracket)
956 *arg = skipwhite(*arg + 1);
957 if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!
958 goto failret;
959 if (has_bracket)
960 {
961 *arg = skipwhite(*arg);
962 if (**arg != ']')
963 {
964 emsg(_(e_missing_matching_bracket_after_dict_key));
Bram Moolenaar8bb0f542020-12-06 16:03:55 +0100965 clear_tv(&tvkey);
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100966 return FAIL;
967 }
968 ++*arg;
969 }
970 }
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200971
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200972 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200973 // previously, so only require it in Vim9 script.
974 if (!vim9script)
975 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200976 if (**arg != ':')
977 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200978 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +0100979 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200980 else
Bram Moolenaar74409f62022-01-01 15:58:22 +0000981 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200982 clear_tv(&tvkey);
983 goto failret;
984 }
985 if (evaluate)
986 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100987 if (tvkey.v_type == VAR_FLOAT)
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100988 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100989 tvkey.vval.v_string = typval_tostring(&tvkey, TRUE);
990 tvkey.v_type = VAR_STRING;
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100991 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100992 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200993 if (key == NULL)
994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100995 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200996 clear_tv(&tvkey);
997 goto failret;
998 }
999 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001000 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1001 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001002 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaarab19d492020-06-27 17:04:05 +02001003 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001004 goto failret;
1005 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001006
Bram Moolenaar962d7212020-07-04 14:15:00 +02001007 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001008 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +02001009 {
1010 if (evaluate)
1011 clear_tv(&tvkey);
1012 goto failret;
1013 }
1014 if (evaluate)
1015 {
1016 item = dict_find(d, key, -1);
1017 if (item != NULL)
1018 {
dundargocc57b5bc2022-11-02 13:30:51 +00001019 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001020 clear_tv(&tvkey);
1021 clear_tv(&tv);
1022 goto failret;
1023 }
1024 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001025 if (item != NULL)
1026 {
1027 item->di_tv = tv;
1028 item->di_tv.v_lock = 0;
1029 if (dict_add(d, item) == FAIL)
1030 dictitem_free(item);
1031 }
1032 }
Bram Moolenaara8931942019-09-28 17:25:10 +02001033 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001034
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001035 // the comma should come right after the value, but this wasn't checked
1036 // previously, so only require it in Vim9 script.
1037 if (!vim9script)
1038 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001039 had_comma = **arg == ',';
1040 if (had_comma)
1041 {
1042 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1043 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001044 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001045 goto failret;
1046 }
1047 *arg = skipwhite(*arg + 1);
1048 }
1049
1050 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +02001051 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001052 if (**arg == '}')
1053 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001054 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001055 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001056 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001057 semsg(_(e_no_white_space_allowed_before_str_str), ",", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001058 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001059 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001060 goto failret;
1061 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001062 }
1063
1064 if (**arg != '}')
1065 {
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001066 if (evalarg != NULL)
1067 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001068failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +02001069 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001070 dict_free(d);
1071 return FAIL;
1072 }
1073
Bram Moolenaarb07a39d2020-10-22 19:00:01 +02001074 *arg = *arg + 1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001075 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001076 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001077
1078 return OK;
1079}
1080
1081/*
1082 * Go over all entries in "d2" and add them to "d1".
1083 * When "action" is "error" then a duplicate key is an error.
1084 * When "action" is "force" then a duplicate key is overwritten.
1085 * Otherwise duplicate keys are ignored ("action" is "keep").
1086 */
1087 void
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001088dict_extend(dict_T *d1, dict_T *d2, char_u *action, char *func_name)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001089{
1090 dictitem_T *di1;
1091 hashitem_T *hi2;
1092 int todo;
1093 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001094 type_T *type;
1095
1096 if (d1->dv_type != NULL && d1->dv_type->tt_member != NULL)
1097 type = d1->dv_type->tt_member;
1098 else
1099 type = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001100
1101 todo = (int)d2->dv_hashtab.ht_used;
1102 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
1103 {
1104 if (!HASHITEM_EMPTY(hi2))
1105 {
1106 --todo;
1107 di1 = dict_find(d1, hi2->hi_key, -1);
zeertzjq91c75d12022-11-05 20:21:58 +00001108 // Check the key to be valid when adding to any scope.
1109 if (d1->dv_scope != 0 && !valid_varname(hi2->hi_key, -1, TRUE))
1110 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001111
1112 if (type != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001113 && check_typval_arg_type(type, &HI2DI(hi2)->di_tv,
1114 func_name, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001115 break;
1116
Bram Moolenaarcd524592016-07-17 14:57:05 +02001117 if (di1 == NULL)
1118 {
1119 di1 = dictitem_copy(HI2DI(hi2));
1120 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1121 dictitem_free(di1);
1122 }
1123 else if (*action == 'e')
1124 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001125 semsg(_(e_key_already_exists_str), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001126 break;
1127 }
1128 else if (*action == 'f' && HI2DI(hi2) != di1)
1129 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001130 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001131 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001132 break;
zeertzjq91c75d12022-11-05 20:21:58 +00001133 // Disallow replacing a builtin function.
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001134 if (dict_wrong_func_name(d1, &HI2DI(hi2)->di_tv, hi2->hi_key))
1135 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001136 clear_tv(&di1->di_tv);
1137 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1138 }
1139 }
1140 }
1141}
1142
1143/*
1144 * Return the dictitem that an entry in a hashtable points to.
1145 */
1146 dictitem_T *
1147dict_lookup(hashitem_T *hi)
1148{
1149 return HI2DI(hi);
1150}
1151
1152/*
1153 * Return TRUE when two dictionaries have exactly the same key/values.
1154 */
1155 int
1156dict_equal(
1157 dict_T *d1,
1158 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001159 int ic, // ignore case for strings
1160 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001161{
1162 hashitem_T *hi;
1163 dictitem_T *item2;
1164 int todo;
1165
Bram Moolenaarcd524592016-07-17 14:57:05 +02001166 if (d1 == d2)
1167 return TRUE;
1168 if (dict_len(d1) != dict_len(d2))
1169 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001170 if (dict_len(d1) == 0)
1171 // empty and NULL dicts are considered equal
1172 return TRUE;
1173 if (d1 == NULL || d2 == NULL)
1174 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001175
1176 todo = (int)d1->dv_hashtab.ht_used;
1177 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1178 {
1179 if (!HASHITEM_EMPTY(hi))
1180 {
1181 item2 = dict_find(d2, hi->hi_key, -1);
1182 if (item2 == NULL)
1183 return FALSE;
1184 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1185 return FALSE;
1186 --todo;
1187 }
1188 }
1189 return TRUE;
1190}
1191
1192/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001193 * Count the number of times item "needle" occurs in Dict "d". Case is ignored
1194 * if "ic" is TRUE.
1195 */
1196 long
1197dict_count(dict_T *d, typval_T *needle, int ic)
1198{
1199 int todo;
1200 hashitem_T *hi;
1201 long n = 0;
1202
1203 if (d == NULL)
1204 return 0;
1205
1206 todo = (int)d->dv_hashtab.ht_used;
1207 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1208 {
1209 if (!HASHITEM_EMPTY(hi))
1210 {
1211 --todo;
1212 if (tv_equal(&HI2DI(hi)->di_tv, needle, ic, FALSE))
1213 ++n;
1214 }
1215 }
1216
1217 return n;
1218}
1219
1220/*
1221 * extend() a Dict. Append Dict argvars[1] to Dict argvars[0] and return the
1222 * resulting Dict in "rettv". "is_new" is TRUE for extendnew().
1223 */
1224 void
1225dict_extend_func(
1226 typval_T *argvars,
1227 type_T *type,
1228 char *func_name,
1229 char_u *arg_errmsg,
1230 int is_new,
1231 typval_T *rettv)
1232{
1233 dict_T *d1, *d2;
1234 char_u *action;
1235 int i;
1236
1237 d1 = argvars[0].vval.v_dict;
1238 if (d1 == NULL)
1239 {
1240 emsg(_(e_cannot_extend_null_dict));
1241 return;
1242 }
1243 d2 = argvars[1].vval.v_dict;
1244 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
1245 && d2 != NULL)
1246 {
1247 if (is_new)
1248 {
Bram Moolenaar381692b2022-02-02 20:01:27 +00001249 d1 = dict_copy(d1, FALSE, TRUE, get_copyID());
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001250 if (d1 == NULL)
1251 return;
1252 }
1253
1254 // Check the third argument.
1255 if (argvars[2].v_type != VAR_UNKNOWN)
1256 {
1257 static char *(av[]) = {"keep", "force", "error"};
1258
1259 action = tv_get_string_chk(&argvars[2]);
1260 if (action == NULL)
1261 return;
1262 for (i = 0; i < 3; ++i)
1263 if (STRCMP(action, av[i]) == 0)
1264 break;
1265 if (i == 3)
1266 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001267 semsg(_(e_invalid_argument_str), action);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001268 return;
1269 }
1270 }
1271 else
1272 action = (char_u *)"force";
1273
1274 if (type != NULL && check_typval_arg_type(type, &argvars[1],
1275 func_name, 2) == FAIL)
1276 return;
1277 dict_extend(d1, d2, action, func_name);
1278
1279 if (is_new)
1280 {
1281 rettv->v_type = VAR_DICT;
1282 rettv->vval.v_dict = d1;
1283 rettv->v_lock = FALSE;
1284 }
1285 else
1286 copy_tv(&argvars[0], rettv);
1287 }
1288}
1289
1290/*
1291 * Implementation of map() and filter() for a Dict. Apply "expr" to every
1292 * item in Dict "d" and return the result in "rettv".
1293 */
1294 void
1295dict_filter_map(
1296 dict_T *d,
1297 filtermap_T filtermap,
1298 type_T *argtype,
1299 char *func_name,
1300 char_u *arg_errmsg,
1301 typval_T *expr,
1302 typval_T *rettv)
1303{
1304 int prev_lock;
1305 dict_T *d_ret = NULL;
1306 hashtab_T *ht;
1307 hashitem_T *hi;
1308 dictitem_T *di;
1309 int todo;
1310 int rem;
Bram Moolenaar82418262022-09-28 16:16:15 +01001311 typval_T newtv;
1312 funccall_T *fc;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001313
1314 if (filtermap == FILTERMAP_MAPNEW)
1315 {
1316 rettv->v_type = VAR_DICT;
1317 rettv->vval.v_dict = NULL;
1318 }
1319 if (d == NULL
1320 || (filtermap == FILTERMAP_FILTER
1321 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1322 return;
1323
1324 prev_lock = d->dv_lock;
1325
1326 if (filtermap == FILTERMAP_MAPNEW)
1327 {
1328 if (rettv_dict_alloc(rettv) == FAIL)
1329 return;
1330 d_ret = rettv->vval.v_dict;
1331 }
1332
Bram Moolenaar82418262022-09-28 16:16:15 +01001333 // Create one funccal_T for all eval_expr_typval() calls.
1334 fc = eval_expr_get_funccal(expr, &newtv);
1335
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001336 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
1337 d->dv_lock = VAR_LOCKED;
1338 ht = &d->dv_hashtab;
1339 hash_lock(ht);
1340 todo = (int)ht->ht_used;
1341 for (hi = ht->ht_array; todo > 0; ++hi)
1342 {
1343 if (!HASHITEM_EMPTY(hi))
1344 {
1345 int r;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001346
1347 --todo;
1348 di = HI2DI(hi);
1349 if (filtermap == FILTERMAP_MAP
1350 && (value_check_lock(di->di_tv.v_lock,
1351 arg_errmsg, TRUE)
1352 || var_check_ro(di->di_flags,
1353 arg_errmsg, TRUE)))
1354 break;
1355 set_vim_var_string(VV_KEY, di->di_key, -1);
1356 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaar82418262022-09-28 16:16:15 +01001357 r = filter_map_one(&di->di_tv, expr, filtermap, fc, &newtv, &rem);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001358 clear_tv(get_vim_var_tv(VV_KEY));
1359 if (r == FAIL || did_emsg)
1360 {
1361 clear_tv(&newtv);
1362 break;
1363 }
1364 if (filtermap == FILTERMAP_MAP)
1365 {
1366 if (argtype != NULL && check_typval_arg_type(
Bram Moolenaar078a4612022-01-04 15:17:03 +00001367 argtype->tt_member, &newtv, func_name, 0) == FAIL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001368 {
1369 clear_tv(&newtv);
1370 break;
1371 }
1372 // map(): replace the dict item value
1373 clear_tv(&di->di_tv);
1374 newtv.v_lock = 0;
1375 di->di_tv = newtv;
1376 }
1377 else if (filtermap == FILTERMAP_MAPNEW)
1378 {
1379 // mapnew(): add the item value to the new dict
1380 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
1381 clear_tv(&newtv);
1382 if (r == FAIL)
1383 break;
1384 }
1385 else if (filtermap == FILTERMAP_FILTER && rem)
1386 {
1387 // filter(false): remove the item from the dict
1388 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1389 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1390 break;
1391 dictitem_remove(d, di);
1392 }
1393 }
1394 }
1395 hash_unlock(ht);
1396 d->dv_lock = prev_lock;
Bram Moolenaar82418262022-09-28 16:16:15 +01001397 if (fc != NULL)
1398 remove_funccal();
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001399}
1400
1401/*
1402 * "remove({dict})" function
1403 */
1404 void
1405dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1406{
1407 dict_T *d;
1408 char_u *key;
1409 dictitem_T *di;
1410
1411 if (argvars[2].v_type != VAR_UNKNOWN)
1412 {
1413 semsg(_(e_too_many_arguments_for_function_str), "remove()");
1414 return;
1415 }
1416
1417 d = argvars[0].vval.v_dict;
1418 if (d == NULL || value_check_lock(d->dv_lock, arg_errmsg, TRUE))
1419 return;
1420
1421 key = tv_get_string_chk(&argvars[1]);
1422 if (key == NULL)
1423 return;
1424
1425 di = dict_find(d, key, -1);
1426 if (di == NULL)
1427 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001428 semsg(_(e_key_not_present_in_dictionary), key);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001429 return;
1430 }
1431
1432 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1433 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1434 return;
1435
1436 *rettv = di->di_tv;
1437 init_tv(&di->di_tv);
1438 dictitem_remove(d, di);
1439}
1440
Bram Moolenaar976f8592022-08-30 14:34:52 +01001441typedef enum {
1442 DICT2LIST_KEYS,
1443 DICT2LIST_VALUES,
1444 DICT2LIST_ITEMS,
1445} dict2list_T;
1446
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001447/*
Bram Moolenaar976f8592022-08-30 14:34:52 +01001448 * Turn a dict into a list.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001449 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001450 static void
Bram Moolenaar976f8592022-08-30 14:34:52 +01001451dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001452{
1453 list_T *l2;
1454 dictitem_T *di;
1455 hashitem_T *hi;
1456 listitem_T *li;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001457 dict_T *d;
1458 int todo;
1459
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001460 if (rettv_list_alloc(rettv) == FAIL)
1461 return;
1462
Bram Moolenaar976f8592022-08-30 14:34:52 +01001463 if ((what == DICT2LIST_ITEMS
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001464 ? check_for_string_or_list_or_dict_arg(argvars, 0)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001465 : check_for_dict_arg(argvars, 0)) == FAIL)
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001466 return;
1467
Bram Moolenaar976f8592022-08-30 14:34:52 +01001468 d = argvars[0].vval.v_dict;
1469 if (d == NULL)
Bram Moolenaaref982572021-08-12 19:27:57 +02001470 // empty dict behaves like an empty dict
1471 return;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001472
1473 todo = (int)d->dv_hashtab.ht_used;
1474 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1475 {
1476 if (!HASHITEM_EMPTY(hi))
1477 {
1478 --todo;
1479 di = HI2DI(hi);
1480
1481 li = listitem_alloc();
1482 if (li == NULL)
1483 break;
1484 list_append(rettv->vval.v_list, li);
1485
Bram Moolenaar976f8592022-08-30 14:34:52 +01001486 if (what == DICT2LIST_KEYS)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001488 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001489 li->li_tv.v_type = VAR_STRING;
1490 li->li_tv.v_lock = 0;
1491 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1492 }
Bram Moolenaar976f8592022-08-30 14:34:52 +01001493 else if (what == DICT2LIST_VALUES)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001495 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001496 copy_tv(&di->di_tv, &li->li_tv);
1497 }
1498 else
1499 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001500 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001501 l2 = list_alloc();
1502 li->li_tv.v_type = VAR_LIST;
1503 li->li_tv.v_lock = 0;
1504 li->li_tv.vval.v_list = l2;
1505 if (l2 == NULL)
1506 break;
1507 ++l2->lv_refcount;
1508
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001509 if (list_append_string(l2, di->di_key, -1) == FAIL
1510 || list_append_tv(l2, &di->di_tv) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001511 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001512 }
1513 }
1514 }
1515}
1516
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001517/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001518 * "items(dict)" function
1519 */
1520 void
1521f_items(typval_T *argvars, typval_T *rettv)
1522{
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001523 if (argvars[0].v_type == VAR_STRING)
1524 string2items(argvars, rettv);
1525 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar976f8592022-08-30 14:34:52 +01001526 list2items(argvars, rettv);
1527 else
1528 dict2list(argvars, rettv, DICT2LIST_ITEMS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001529}
1530
1531/*
1532 * "keys()" function
1533 */
1534 void
1535f_keys(typval_T *argvars, typval_T *rettv)
1536{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001537 dict2list(argvars, rettv, DICT2LIST_KEYS);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001538}
1539
1540/*
1541 * "values(dict)" function
1542 */
1543 void
1544f_values(typval_T *argvars, typval_T *rettv)
1545{
Bram Moolenaar976f8592022-08-30 14:34:52 +01001546 dict2list(argvars, rettv, DICT2LIST_VALUES);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001547}
1548
1549/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001550 * Make each item in the dict readonly (not the value of the item).
1551 */
1552 void
1553dict_set_items_ro(dict_T *di)
1554{
1555 int todo = (int)di->dv_hashtab.ht_used;
1556 hashitem_T *hi;
1557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001558 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001559 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1560 {
1561 if (HASHITEM_EMPTY(hi))
1562 continue;
1563 --todo;
1564 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1565 }
1566}
1567
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001568/*
1569 * "has_key()" function
1570 */
1571 void
1572f_has_key(typval_T *argvars, typval_T *rettv)
1573{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001574 if (in_vim9script()
1575 && (check_for_dict_arg(argvars, 0) == FAIL
1576 || check_for_string_or_number_arg(argvars, 1) == FAIL))
1577 return;
1578
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001579 if (check_for_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001580 return;
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001581
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001582 if (argvars[0].vval.v_dict == NULL)
1583 return;
1584
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001585 rettv->vval.v_number = dict_has_key(argvars[0].vval.v_dict,
1586 (char *)tv_get_string(&argvars[1]));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001587}
1588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001589#endif // defined(FEAT_EVAL)