blob: 9ce6dda3d22085227105790696ded1280553ce9e [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;
225
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200226 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200227 if (di != NULL)
228 {
229 STRCPY(di->di_key, key);
230 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200231 di->di_tv.v_lock = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200232 }
233 return di;
234}
235
236/*
237 * Make a copy of a Dictionary item.
238 */
239 static dictitem_T *
240dictitem_copy(dictitem_T *org)
241{
242 dictitem_T *di;
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200243 size_t len = STRLEN(org->di_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200244
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200245 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200246 if (di != NULL)
247 {
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200248 mch_memmove(di->di_key, org->di_key, len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200249 di->di_flags = DI_FLAGS_ALLOC;
250 copy_tv(&org->di_tv, &di->di_tv);
251 }
252 return di;
253}
254
255/*
256 * Remove item "item" from Dictionary "dict" and free it.
257 */
258 void
259dictitem_remove(dict_T *dict, dictitem_T *item)
260{
261 hashitem_T *hi;
262
263 hi = hash_find(&dict->dv_hashtab, item->di_key);
264 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100265 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200266 else
267 hash_remove(&dict->dv_hashtab, hi);
268 dictitem_free(item);
269}
270
271/*
272 * Free a dict item. Also clears the value.
273 */
274 void
275dictitem_free(dictitem_T *item)
276{
277 clear_tv(&item->di_tv);
278 if (item->di_flags & DI_FLAGS_ALLOC)
279 vim_free(item);
280}
281
282/*
283 * Make a copy of dict "d". Shallow if "deep" is FALSE.
284 * The refcount of the new dict is set to 1.
285 * See item_copy() for "copyID".
286 * Returns NULL when out of memory.
287 */
288 dict_T *
289dict_copy(dict_T *orig, int deep, int copyID)
290{
291 dict_T *copy;
292 dictitem_T *di;
293 int todo;
294 hashitem_T *hi;
295
296 if (orig == NULL)
297 return NULL;
298
299 copy = dict_alloc();
300 if (copy != NULL)
301 {
302 if (copyID != 0)
303 {
304 orig->dv_copyID = copyID;
305 orig->dv_copydict = copy;
306 }
307 todo = (int)orig->dv_hashtab.ht_used;
308 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
309 {
310 if (!HASHITEM_EMPTY(hi))
311 {
312 --todo;
313
314 di = dictitem_alloc(hi->hi_key);
315 if (di == NULL)
316 break;
317 if (deep)
318 {
319 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
320 copyID) == FAIL)
321 {
322 vim_free(di);
323 break;
324 }
325 }
326 else
327 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
328 if (dict_add(copy, di) == FAIL)
329 {
330 dictitem_free(di);
331 break;
332 }
333 }
334 }
335
336 ++copy->dv_refcount;
337 if (todo > 0)
338 {
339 dict_unref(copy);
340 copy = NULL;
341 }
342 }
343
344 return copy;
345}
346
347/*
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200348 * Check for adding a function to g: or s:.
349 * If the name is wrong give an error message and return TRUE.
350 */
351 int
352dict_wrong_func_name(dict_T *d, typval_T *tv, char_u *name)
353{
354 return (d == get_globvar_dict()
Bram Moolenaarb54abee2021-06-02 11:49:23 +0200355 || (in_vim9script() && SCRIPT_ID_VALID(current_sctx.sc_sid)
356 && d == &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict)
357 || &d->dv_hashtab == get_funccal_local_ht())
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200358 && (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
359 && var_wrong_func_name(name, TRUE);
360}
361
362/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200363 * Add item "item" to Dictionary "d".
364 * Returns FAIL when out of memory and when key already exists.
365 */
366 int
367dict_add(dict_T *d, dictitem_T *item)
368{
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200369 if (dict_wrong_func_name(d, &item->di_tv, item->di_key))
370 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200371 return hash_add(&d->dv_hashtab, item->di_key);
372}
373
374/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200375 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200376 * Returns FAIL when out of memory and when key already exists.
377 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200378 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100379dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200380{
381 dictitem_T *item;
382
383 item = dictitem_alloc((char_u *)key);
384 if (item == NULL)
385 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100386 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200387 item->di_tv.vval.v_number = nr;
388 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200389 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200390 dictitem_free(item);
391 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200392 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200393 return OK;
394}
395
396/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200397 * Add a number entry to dictionary "d".
398 * Returns FAIL when out of memory and when key already exists.
399 */
400 int
401dict_add_number(dict_T *d, char *key, varnumber_T nr)
402{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100403 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200404}
405
406/*
407 * Add a special entry to dictionary "d".
408 * Returns FAIL when out of memory and when key already exists.
409 */
410 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100411dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200412{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100413 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200414}
415
416/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200417 * Add a string entry to dictionary "d".
418 * Returns FAIL when out of memory and when key already exists.
419 */
420 int
421dict_add_string(dict_T *d, char *key, char_u *str)
422{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100423 return dict_add_string_len(d, key, str, -1);
424}
425
426/*
427 * Add a string entry to dictionary "d".
428 * "str" will be copied to allocated memory.
429 * When "len" is -1 use the whole string, otherwise only this many bytes.
430 * Returns FAIL when out of memory and when key already exists.
431 */
432 int
433dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
434{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200435 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100436 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200437
438 item = dictitem_alloc((char_u *)key);
439 if (item == NULL)
440 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200441 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100442 if (str != NULL)
443 {
444 if (len == -1)
445 val = vim_strsave(str);
446 else
447 val = vim_strnsave(str, len);
448 }
449 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200450 if (dict_add(d, item) == FAIL)
451 {
452 dictitem_free(item);
453 return FAIL;
454 }
455 return OK;
456}
457
458/*
459 * Add a list entry to dictionary "d".
460 * Returns FAIL when out of memory and when key already exists.
461 */
462 int
463dict_add_list(dict_T *d, char *key, list_T *list)
464{
465 dictitem_T *item;
466
467 item = dictitem_alloc((char_u *)key);
468 if (item == NULL)
469 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200470 item->di_tv.v_type = VAR_LIST;
471 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100472 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200473 if (dict_add(d, item) == FAIL)
474 {
475 dictitem_free(item);
476 return FAIL;
477 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200478 return OK;
479}
480
481/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100482 * Add a typval_T entry to dictionary "d".
483 * Returns FAIL when out of memory and when key already exists.
484 */
485 int
486dict_add_tv(dict_T *d, char *key, typval_T *tv)
487{
488 dictitem_T *item;
489
490 item = dictitem_alloc((char_u *)key);
491 if (item == NULL)
492 return FAIL;
493 copy_tv(tv, &item->di_tv);
494 if (dict_add(d, item) == FAIL)
495 {
496 dictitem_free(item);
497 return FAIL;
498 }
499 return OK;
500}
501
502/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200503 * Add a callback to dictionary "d".
504 * Returns FAIL when out of memory and when key already exists.
505 */
506 int
507dict_add_callback(dict_T *d, char *key, callback_T *cb)
508{
509 dictitem_T *item;
510
511 item = dictitem_alloc((char_u *)key);
512 if (item == NULL)
513 return FAIL;
514 put_callback(cb, &item->di_tv);
515 if (dict_add(d, item) == FAIL)
516 {
517 dictitem_free(item);
518 return FAIL;
519 }
520 return OK;
521}
522
523/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200524 * Initializes "iter" for iterating over dictionary items with
525 * dict_iterate_next().
526 * If "var" is not a Dict or an empty Dict then there will be nothing to
527 * iterate over, no error is given.
528 * NOTE: The dictionary must not change until iterating is finished!
529 */
530 void
531dict_iterate_start(typval_T *var, dict_iterator_T *iter)
532{
533 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
534 iter->dit_todo = 0;
535 else
536 {
537 dict_T *d = var->vval.v_dict;
538
539 iter->dit_todo = d->dv_hashtab.ht_used;
540 iter->dit_hi = d->dv_hashtab.ht_array;
541 }
542}
543
544/*
545 * Iterate over the items referred to by "iter". It should be initialized with
546 * dict_iterate_start().
547 * Returns a pointer to the key.
548 * "*tv_result" is set to point to the value for that key.
549 * If there are no more items, NULL is returned.
550 */
551 char_u *
552dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
553{
554 dictitem_T *di;
555 char_u *result;
556
557 if (iter->dit_todo == 0)
558 return NULL;
559
560 while (HASHITEM_EMPTY(iter->dit_hi))
561 ++iter->dit_hi;
562
563 di = HI2DI(iter->dit_hi);
564 result = di->di_key;
565 *tv_result = &di->di_tv;
566
567 --iter->dit_todo;
568 ++iter->dit_hi;
569 return result;
570}
571
572/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200573 * Add a dict entry to dictionary "d".
574 * Returns FAIL when out of memory and when key already exists.
575 */
576 int
577dict_add_dict(dict_T *d, char *key, dict_T *dict)
578{
579 dictitem_T *item;
580
581 item = dictitem_alloc((char_u *)key);
582 if (item == NULL)
583 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200584 item->di_tv.v_type = VAR_DICT;
585 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100586 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200587 if (dict_add(d, item) == FAIL)
588 {
589 dictitem_free(item);
590 return FAIL;
591 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200592 return OK;
593}
594
595/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200596 * Get the number of items in a Dictionary.
597 */
598 long
599dict_len(dict_T *d)
600{
601 if (d == NULL)
602 return 0L;
603 return (long)d->dv_hashtab.ht_used;
604}
605
606/*
607 * Find item "key[len]" in Dictionary "d".
608 * If "len" is negative use strlen(key).
609 * Returns NULL when not found.
610 */
611 dictitem_T *
612dict_find(dict_T *d, char_u *key, int len)
613{
614#define AKEYLEN 200
615 char_u buf[AKEYLEN];
616 char_u *akey;
617 char_u *tofree = NULL;
618 hashitem_T *hi;
619
620 if (d == NULL)
621 return NULL;
622 if (len < 0)
623 akey = key;
624 else if (len >= AKEYLEN)
625 {
626 tofree = akey = vim_strnsave(key, len);
627 if (akey == NULL)
628 return NULL;
629 }
630 else
631 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100632 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200633 vim_strncpy(buf, key, len);
634 akey = buf;
635 }
636
637 hi = hash_find(&d->dv_hashtab, akey);
638 vim_free(tofree);
639 if (HASHITEM_EMPTY(hi))
640 return NULL;
641 return HI2DI(hi);
642}
643
644/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100645 * Get a typval_T item from a dictionary and copy it into "rettv".
646 * Returns FAIL if the entry doesn't exist or out of memory.
647 */
648 int
649dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
650{
651 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100652
653 di = dict_find(d, key, -1);
654 if (di == NULL)
655 return FAIL;
656 copy_tv(&di->di_tv, rettv);
657 return OK;
658}
659
660/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200661 * Get a string item from a dictionary.
662 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200663 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200664 * Returns NULL if the entry doesn't exist or out of memory.
665 */
666 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100667dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200668{
669 dictitem_T *di;
670 char_u *s;
671
672 di = dict_find(d, key, -1);
673 if (di == NULL)
674 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100675 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200676 if (save && s != NULL)
677 s = vim_strsave(s);
678 return s;
679}
680
681/*
682 * Get a number item from a dictionary.
683 * Returns 0 if the entry doesn't exist.
684 */
685 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100686dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200687{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200688 return dict_get_number_def(d, key, 0);
689}
690
691/*
692 * Get a number item from a dictionary.
693 * Returns "def" if the entry doesn't exist.
694 */
695 varnumber_T
696dict_get_number_def(dict_T *d, char_u *key, int def)
697{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200698 dictitem_T *di;
699
700 di = dict_find(d, key, -1);
701 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200702 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100703 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200704}
705
706/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200707 * Get a number item from a dictionary.
708 * Returns 0 if the entry doesn't exist.
709 * Give an error if the entry is not a number.
710 */
711 varnumber_T
712dict_get_number_check(dict_T *d, char_u *key)
713{
714 dictitem_T *di;
715
716 di = dict_find(d, key, -1);
717 if (di == NULL)
718 return 0;
719 if (di->di_tv.v_type != VAR_NUMBER)
720 {
721 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
722 return 0;
723 }
724 return tv_get_number(&di->di_tv);
725}
726
727/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200728 * Get a bool item (number or true/false) from a dictionary.
729 * Returns "def" if the entry doesn't exist.
730 */
731 varnumber_T
732dict_get_bool(dict_T *d, char_u *key, int def)
733{
734 dictitem_T *di;
735
736 di = dict_find(d, key, -1);
737 if (di == NULL)
738 return def;
739 return tv_get_bool(&di->di_tv);
740}
741
742/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200743 * Return an allocated string with the string representation of a Dictionary.
744 * May return NULL.
745 */
746 char_u *
747dict2string(typval_T *tv, int copyID, int restore_copyID)
748{
749 garray_T ga;
750 int first = TRUE;
751 char_u *tofree;
752 char_u numbuf[NUMBUFLEN];
753 hashitem_T *hi;
754 char_u *s;
755 dict_T *d;
756 int todo;
757
758 if ((d = tv->vval.v_dict) == NULL)
759 return NULL;
760 ga_init2(&ga, (int)sizeof(char), 80);
761 ga_append(&ga, '{');
762
763 todo = (int)d->dv_hashtab.ht_used;
764 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
765 {
766 if (!HASHITEM_EMPTY(hi))
767 {
768 --todo;
769
770 if (first)
771 first = FALSE;
772 else
773 ga_concat(&ga, (char_u *)", ");
774
775 tofree = string_quote(hi->hi_key, FALSE);
776 if (tofree != NULL)
777 {
778 ga_concat(&ga, tofree);
779 vim_free(tofree);
780 }
781 ga_concat(&ga, (char_u *)": ");
782 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
783 FALSE, restore_copyID, TRUE);
784 if (s != NULL)
785 ga_concat(&ga, s);
786 vim_free(tofree);
787 if (s == NULL || did_echo_string_emsg)
788 break;
789 line_breakcheck();
790
791 }
792 }
793 if (todo > 0)
794 {
795 vim_free(ga.ga_data);
796 return NULL;
797 }
798
799 ga_append(&ga, '}');
800 ga_append(&ga, NUL);
801 return (char_u *)ga.ga_data;
802}
803
804/*
Bram Moolenaare0de1712020-12-02 17:36:54 +0100805 * Advance over a literal key, including "-". If the first character is not a
806 * literal key character then "key" is returned.
807 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200808 static char_u *
Bram Moolenaare0de1712020-12-02 17:36:54 +0100809skip_literal_key(char_u *key)
810{
811 char_u *p;
812
813 for (p = key; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
814 ;
815 return p;
816}
817
818/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200819 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200820 * Return FAIL when there is no valid key.
821 */
822 static int
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100823get_literal_key_tv(char_u **arg, typval_T *tv)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200824{
Bram Moolenaare0de1712020-12-02 17:36:54 +0100825 char_u *p = skip_literal_key(*arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200826
Bram Moolenaare0de1712020-12-02 17:36:54 +0100827 if (p == *arg)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200828 return FAIL;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200829 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200830 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200831
Bram Moolenaardb199212020-08-12 18:01:53 +0200832 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200833 return OK;
834}
835
836/*
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100837 * Get a literal key for a Vim9 dict:
838 * {"name": value},
839 * {'name': value},
840 * {name: value} use "name" as a literal key
841 * Return the key in allocated memory or NULL in the case of an error.
842 * "arg" is advanced to just after the key.
843 */
844 char_u *
845get_literal_key(char_u **arg)
846{
847 char_u *key;
848 char_u *end;
849 typval_T rettv;
850
851 if (**arg == '\'')
852 {
853 if (eval_lit_string(arg, &rettv, TRUE) == FAIL)
854 return NULL;
855 key = rettv.vval.v_string;
856 }
857 else if (**arg == '"')
858 {
859 if (eval_string(arg, &rettv, TRUE) == FAIL)
860 return NULL;
861 key = rettv.vval.v_string;
862 }
863 else
864 {
865 end = skip_literal_key(*arg);
866 if (end == *arg)
867 {
868 semsg(_(e_invalid_key_str), *arg);
869 return NULL;
870 }
871 key = vim_strnsave(*arg, end - *arg);
872 *arg = end;
873 }
874 return key;
875}
876
877/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200878 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200879 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200880 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200881 * Return OK or FAIL. Returns NOTDONE for {expr}.
882 */
883 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200884eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200885{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200886 int evaluate = evalarg == NULL ? FALSE
887 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200888 dict_T *d = NULL;
889 typval_T tvkey;
890 typval_T tv;
891 char_u *key = NULL;
892 dictitem_T *item;
893 char_u *start = skipwhite(*arg + 1);
894 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200895 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200896 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200897
Bram Moolenaarcd524592016-07-17 14:57:05 +0200898 /*
899 * First check if it's not a curly-braces thing: {expr}.
900 * Must do this without evaluating, otherwise a function may be called
901 * twice. Unfortunately this means we need to call eval1() twice for the
902 * first item.
903 * But {} is an empty Dictionary.
904 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200905 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200906 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200907 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200908 return FAIL;
Bram Moolenaarae95a392020-08-06 16:38:12 +0200909 if (*skipwhite(start) == '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200910 return NOTDONE;
911 }
912
913 if (evaluate)
914 {
915 d = dict_alloc();
916 if (d == NULL)
917 return FAIL;
918 }
919 tvkey.v_type = VAR_UNKNOWN;
920 tv.v_type = VAR_UNKNOWN;
921
Bram Moolenaar962d7212020-07-04 14:15:00 +0200922 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200923 while (**arg != '}' && **arg != NUL)
924 {
Bram Moolenaare0de1712020-12-02 17:36:54 +0100925 int has_bracket = vim9script && **arg == '[';
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100926
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100927 if (literal)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100928 {
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100929 if (get_literal_key_tv(arg, &tvkey) == FAIL)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100930 goto failret;
931 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100932 else if (vim9script && !has_bracket)
933 {
934 tvkey.vval.v_string = get_literal_key(arg);
935 if (tvkey.vval.v_string == NULL)
936 goto failret;
937 tvkey.v_type = VAR_STRING;
938 }
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100939 else
940 {
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100941 if (has_bracket)
942 *arg = skipwhite(*arg + 1);
943 if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!
944 goto failret;
945 if (has_bracket)
946 {
947 *arg = skipwhite(*arg);
948 if (**arg != ']')
949 {
950 emsg(_(e_missing_matching_bracket_after_dict_key));
Bram Moolenaar8bb0f542020-12-06 16:03:55 +0100951 clear_tv(&tvkey);
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100952 return FAIL;
953 }
954 ++*arg;
955 }
956 }
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200957
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200958 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200959 // previously, so only require it in Vim9 script.
960 if (!vim9script)
961 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200962 if (**arg != ':')
963 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200964 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +0100965 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200966 else
967 semsg(_(e_missing_dict_colon), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200968 clear_tv(&tvkey);
969 goto failret;
970 }
971 if (evaluate)
972 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100973#ifdef FEAT_FLOAT
974 if (tvkey.v_type == VAR_FLOAT)
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100975 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100976 tvkey.vval.v_string = typval_tostring(&tvkey, TRUE);
977 tvkey.v_type = VAR_STRING;
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100978 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100979#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100980 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200981 if (key == NULL)
982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200984 clear_tv(&tvkey);
985 goto failret;
986 }
987 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200988 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
989 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100990 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaarab19d492020-06-27 17:04:05 +0200991 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200992 goto failret;
993 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200994
Bram Moolenaar962d7212020-07-04 14:15:00 +0200995 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200996 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200997 {
998 if (evaluate)
999 clear_tv(&tvkey);
1000 goto failret;
1001 }
1002 if (evaluate)
1003 {
1004 item = dict_find(d, key, -1);
1005 if (item != NULL)
1006 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001007 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001008 clear_tv(&tvkey);
1009 clear_tv(&tv);
1010 goto failret;
1011 }
1012 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001013 if (item != NULL)
1014 {
1015 item->di_tv = tv;
1016 item->di_tv.v_lock = 0;
1017 if (dict_add(d, item) == FAIL)
1018 dictitem_free(item);
1019 }
1020 }
Bram Moolenaara8931942019-09-28 17:25:10 +02001021 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001022
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001023 // the comma should come right after the value, but this wasn't checked
1024 // previously, so only require it in Vim9 script.
1025 if (!vim9script)
1026 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001027 had_comma = **arg == ',';
1028 if (had_comma)
1029 {
1030 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1031 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001032 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001033 goto failret;
1034 }
1035 *arg = skipwhite(*arg + 1);
1036 }
1037
1038 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +02001039 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001040 if (**arg == '}')
1041 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001042 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001043 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001044 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001045 semsg(_(e_no_white_space_allowed_before_str_str), ",", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001046 else
1047 semsg(_(e_missing_dict_comma), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001048 goto failret;
1049 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001050 }
1051
1052 if (**arg != '}')
1053 {
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001054 if (evalarg != NULL)
1055 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001056failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +02001057 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001058 dict_free(d);
1059 return FAIL;
1060 }
1061
Bram Moolenaarb07a39d2020-10-22 19:00:01 +02001062 *arg = *arg + 1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001063 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001064 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001065
1066 return OK;
1067}
1068
1069/*
1070 * Go over all entries in "d2" and add them to "d1".
1071 * When "action" is "error" then a duplicate key is an error.
1072 * When "action" is "force" then a duplicate key is overwritten.
1073 * Otherwise duplicate keys are ignored ("action" is "keep").
1074 */
1075 void
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001076dict_extend(dict_T *d1, dict_T *d2, char_u *action, char *func_name)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001077{
1078 dictitem_T *di1;
1079 hashitem_T *hi2;
1080 int todo;
1081 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001082 type_T *type;
1083
1084 if (d1->dv_type != NULL && d1->dv_type->tt_member != NULL)
1085 type = d1->dv_type->tt_member;
1086 else
1087 type = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001088
1089 todo = (int)d2->dv_hashtab.ht_used;
1090 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
1091 {
1092 if (!HASHITEM_EMPTY(hi2))
1093 {
1094 --todo;
1095 di1 = dict_find(d1, hi2->hi_key, -1);
1096 if (d1->dv_scope != 0)
1097 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001098 // Disallow replacing a builtin function in l: and g:.
1099 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001100 if (d1->dv_scope == VAR_DEF_SCOPE
1101 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001102 && var_wrong_func_name(hi2->hi_key, di1 == NULL))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001103 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001104 if (!valid_varname(hi2->hi_key, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001105 break;
1106 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001107
1108 if (type != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001109 && check_typval_arg_type(type, &HI2DI(hi2)->di_tv,
1110 func_name, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001111 break;
1112
Bram Moolenaarcd524592016-07-17 14:57:05 +02001113 if (di1 == NULL)
1114 {
1115 di1 = dictitem_copy(HI2DI(hi2));
1116 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1117 dictitem_free(di1);
1118 }
1119 else if (*action == 'e')
1120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001121 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001122 break;
1123 }
1124 else if (*action == 'f' && HI2DI(hi2) != di1)
1125 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001126 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001127 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001128 break;
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001129 if (dict_wrong_func_name(d1, &HI2DI(hi2)->di_tv, hi2->hi_key))
1130 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001131 clear_tv(&di1->di_tv);
1132 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1133 }
1134 }
1135 }
1136}
1137
1138/*
1139 * Return the dictitem that an entry in a hashtable points to.
1140 */
1141 dictitem_T *
1142dict_lookup(hashitem_T *hi)
1143{
1144 return HI2DI(hi);
1145}
1146
1147/*
1148 * Return TRUE when two dictionaries have exactly the same key/values.
1149 */
1150 int
1151dict_equal(
1152 dict_T *d1,
1153 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 int ic, // ignore case for strings
1155 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001156{
1157 hashitem_T *hi;
1158 dictitem_T *item2;
1159 int todo;
1160
Bram Moolenaarcd524592016-07-17 14:57:05 +02001161 if (d1 == d2)
1162 return TRUE;
1163 if (dict_len(d1) != dict_len(d2))
1164 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001165 if (dict_len(d1) == 0)
1166 // empty and NULL dicts are considered equal
1167 return TRUE;
1168 if (d1 == NULL || d2 == NULL)
1169 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001170
1171 todo = (int)d1->dv_hashtab.ht_used;
1172 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1173 {
1174 if (!HASHITEM_EMPTY(hi))
1175 {
1176 item2 = dict_find(d2, hi->hi_key, -1);
1177 if (item2 == NULL)
1178 return FALSE;
1179 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1180 return FALSE;
1181 --todo;
1182 }
1183 }
1184 return TRUE;
1185}
1186
1187/*
1188 * Turn a dict into a list:
1189 * "what" == 0: list of keys
1190 * "what" == 1: list of values
1191 * "what" == 2: list of items
1192 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001193 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001194dict_list(typval_T *argvars, typval_T *rettv, int what)
1195{
1196 list_T *l2;
1197 dictitem_T *di;
1198 hashitem_T *hi;
1199 listitem_T *li;
1200 listitem_T *li2;
1201 dict_T *d;
1202 int todo;
1203
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001204 if (in_vim9script() && check_for_dict_arg(argvars, 0) == FAIL)
1205 return;
1206
Bram Moolenaarcd524592016-07-17 14:57:05 +02001207 if (argvars[0].v_type != VAR_DICT)
1208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001209 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001210 return;
1211 }
1212 if ((d = argvars[0].vval.v_dict) == NULL)
1213 return;
1214
1215 if (rettv_list_alloc(rettv) == FAIL)
1216 return;
1217
1218 todo = (int)d->dv_hashtab.ht_used;
1219 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1220 {
1221 if (!HASHITEM_EMPTY(hi))
1222 {
1223 --todo;
1224 di = HI2DI(hi);
1225
1226 li = listitem_alloc();
1227 if (li == NULL)
1228 break;
1229 list_append(rettv->vval.v_list, li);
1230
1231 if (what == 0)
1232 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001233 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001234 li->li_tv.v_type = VAR_STRING;
1235 li->li_tv.v_lock = 0;
1236 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1237 }
1238 else if (what == 1)
1239 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001241 copy_tv(&di->di_tv, &li->li_tv);
1242 }
1243 else
1244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001246 l2 = list_alloc();
1247 li->li_tv.v_type = VAR_LIST;
1248 li->li_tv.v_lock = 0;
1249 li->li_tv.vval.v_list = l2;
1250 if (l2 == NULL)
1251 break;
1252 ++l2->lv_refcount;
1253
1254 li2 = listitem_alloc();
1255 if (li2 == NULL)
1256 break;
1257 list_append(l2, li2);
1258 li2->li_tv.v_type = VAR_STRING;
1259 li2->li_tv.v_lock = 0;
1260 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1261
1262 li2 = listitem_alloc();
1263 if (li2 == NULL)
1264 break;
1265 list_append(l2, li2);
1266 copy_tv(&di->di_tv, &li2->li_tv);
1267 }
1268 }
1269 }
1270}
1271
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001272/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001273 * "items(dict)" function
1274 */
1275 void
1276f_items(typval_T *argvars, typval_T *rettv)
1277{
1278 dict_list(argvars, rettv, 2);
1279}
1280
1281/*
1282 * "keys()" function
1283 */
1284 void
1285f_keys(typval_T *argvars, typval_T *rettv)
1286{
1287 dict_list(argvars, rettv, 0);
1288}
1289
1290/*
1291 * "values(dict)" function
1292 */
1293 void
1294f_values(typval_T *argvars, typval_T *rettv)
1295{
1296 dict_list(argvars, rettv, 1);
1297}
1298
1299/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001300 * Make each item in the dict readonly (not the value of the item).
1301 */
1302 void
1303dict_set_items_ro(dict_T *di)
1304{
1305 int todo = (int)di->dv_hashtab.ht_used;
1306 hashitem_T *hi;
1307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001309 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1310 {
1311 if (HASHITEM_EMPTY(hi))
1312 continue;
1313 --todo;
1314 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1315 }
1316}
1317
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001318/*
1319 * "has_key()" function
1320 */
1321 void
1322f_has_key(typval_T *argvars, typval_T *rettv)
1323{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001324 if (in_vim9script()
1325 && (check_for_dict_arg(argvars, 0) == FAIL
1326 || check_for_string_or_number_arg(argvars, 1) == FAIL))
1327 return;
1328
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001329 if (argvars[0].v_type != VAR_DICT)
1330 {
1331 emsg(_(e_dictreq));
1332 return;
1333 }
1334 if (argvars[0].vval.v_dict == NULL)
1335 return;
1336
1337 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1338 tv_get_string(&argvars[1]), -1) != NULL;
1339}
1340
1341/*
1342 * "remove({dict})" function
1343 */
1344 void
1345dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1346{
1347 dict_T *d;
1348 char_u *key;
1349 dictitem_T *di;
1350
1351 if (argvars[2].v_type != VAR_UNKNOWN)
1352 semsg(_(e_toomanyarg), "remove()");
1353 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001354 && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001355 {
1356 key = tv_get_string_chk(&argvars[1]);
1357 if (key != NULL)
1358 {
1359 di = dict_find(d, key, -1);
1360 if (di == NULL)
1361 semsg(_(e_dictkey), key);
1362 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1363 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1364 {
1365 *rettv = di->di_tv;
1366 init_tv(&di->di_tv);
1367 dictitem_remove(d, di);
1368 }
1369 }
1370 }
1371}
1372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001373#endif // defined(FEAT_EVAL)