blob: 503f5a522370aa7c77a7efad7f7a07dfc657037b [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.
25 */
26 dict_T *
27dict_alloc(void)
28{
29 dict_T *d;
30
Bram Moolenaaradc67142019-06-22 01:40:42 +020031 d = ALLOC_CLEAR_ONE(dict_T);
Bram Moolenaarcd524592016-07-17 14:57:05 +020032 if (d != NULL)
33 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010034 // Add the dict to the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +020035 if (first_dict != NULL)
36 first_dict->dv_used_prev = d;
37 d->dv_used_next = first_dict;
38 d->dv_used_prev = NULL;
39 first_dict = d;
40
41 hash_init(&d->dv_hashtab);
42 d->dv_lock = 0;
43 d->dv_scope = 0;
44 d->dv_refcount = 0;
45 d->dv_copyID = 0;
46 }
47 return d;
48}
49
Bram Moolenaarf49cc602018-11-11 15:21:05 +010050/*
51 * dict_alloc() with an ID for alloc_fail().
52 */
53 dict_T *
54dict_alloc_id(alloc_id_T id UNUSED)
55{
56#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +020057 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +010058 return NULL;
59#endif
60 return (dict_alloc());
61}
62
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010063 dict_T *
64dict_alloc_lock(int lock)
65{
66 dict_T *d = dict_alloc();
67
68 if (d != NULL)
69 d->dv_lock = lock;
70 return d;
71}
72
Bram Moolenaarcd524592016-07-17 14:57:05 +020073/*
74 * Allocate an empty dict for a return value.
75 * Returns OK or FAIL.
76 */
77 int
78rettv_dict_alloc(typval_T *rettv)
79{
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010080 dict_T *d = dict_alloc_lock(0);
Bram Moolenaarcd524592016-07-17 14:57:05 +020081
82 if (d == NULL)
83 return FAIL;
84
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020085 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +020086 return OK;
87}
88
89/*
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020090 * Set a dictionary as the return value
91 */
92 void
93rettv_dict_set(typval_T *rettv, dict_T *d)
94{
95 rettv->v_type = VAR_DICT;
96 rettv->vval.v_dict = d;
97 if (d != NULL)
98 ++d->dv_refcount;
99}
100
101/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200102 * Free a Dictionary, including all non-container items it contains.
103 * Ignores the reference count.
104 */
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100105 void
Bram Moolenaarcd524592016-07-17 14:57:05 +0200106dict_free_contents(dict_T *d)
107{
Bram Moolenaar89483d42020-05-10 15:24:44 +0200108 hashtab_free_contents(&d->dv_hashtab);
109}
110
111/*
112 * Clear hashtab "ht" and dict items it contains.
113 */
114 void
115hashtab_free_contents(hashtab_T *ht)
116{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200117 int todo;
118 hashitem_T *hi;
119 dictitem_T *di;
120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100121 // Lock the hashtab, we don't want it to resize while freeing items.
Bram Moolenaar89483d42020-05-10 15:24:44 +0200122 hash_lock(ht);
123 todo = (int)ht->ht_used;
124 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200125 {
126 if (!HASHITEM_EMPTY(hi))
127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100128 // Remove the item before deleting it, just in case there is
129 // something recursive causing trouble.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200130 di = HI2DI(hi);
Bram Moolenaar89483d42020-05-10 15:24:44 +0200131 hash_remove(ht, hi);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100132 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200133 --todo;
134 }
135 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100136
Bram Moolenaar89483d42020-05-10 15:24:44 +0200137 // The hashtab is still locked, it has to be re-initialized anyway.
138 hash_clear(ht);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200139}
140
141 static void
142dict_free_dict(dict_T *d)
143{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100144 // Remove the dict from the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200145 if (d->dv_used_prev == NULL)
146 first_dict = d->dv_used_next;
147 else
148 d->dv_used_prev->dv_used_next = d->dv_used_next;
149 if (d->dv_used_next != NULL)
150 d->dv_used_next->dv_used_prev = d->dv_used_prev;
151 vim_free(d);
152}
153
154 static void
155dict_free(dict_T *d)
156{
157 if (!in_free_unref_items)
158 {
159 dict_free_contents(d);
160 dict_free_dict(d);
161 }
162}
163
164/*
165 * Unreference a Dictionary: decrement the reference count and free it when it
166 * becomes zero.
167 */
168 void
169dict_unref(dict_T *d)
170{
171 if (d != NULL && --d->dv_refcount <= 0)
172 dict_free(d);
173}
174
175/*
176 * Go through the list of dicts and free items without the copyID.
177 * Returns TRUE if something was freed.
178 */
179 int
180dict_free_nonref(int copyID)
181{
182 dict_T *dd;
183 int did_free = FALSE;
184
185 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
186 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100188 // Free the Dictionary and ordinary items it contains, but don't
189 // recurse into Lists and Dictionaries, they will be in the list
190 // of dicts or list of lists.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200191 dict_free_contents(dd);
192 did_free = TRUE;
193 }
194 return did_free;
195}
196
197 void
198dict_free_items(int copyID)
199{
200 dict_T *dd, *dd_next;
201
202 for (dd = first_dict; dd != NULL; dd = dd_next)
203 {
204 dd_next = dd->dv_used_next;
205 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
206 dict_free_dict(dd);
207 }
208}
209
210/*
211 * Allocate a Dictionary item.
212 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200213 * Note that the type and value of the item "di_tv" still needs to be
214 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200215 * Returns NULL when out of memory.
216 */
217 dictitem_T *
218dictitem_alloc(char_u *key)
219{
220 dictitem_T *di;
221
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200222 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200223 if (di != NULL)
224 {
225 STRCPY(di->di_key, key);
226 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200227 di->di_tv.v_lock = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200228 }
229 return di;
230}
231
232/*
233 * Make a copy of a Dictionary item.
234 */
235 static dictitem_T *
236dictitem_copy(dictitem_T *org)
237{
238 dictitem_T *di;
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200239 size_t len = STRLEN(org->di_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200240
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200241 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200242 if (di != NULL)
243 {
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200244 mch_memmove(di->di_key, org->di_key, len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200245 di->di_flags = DI_FLAGS_ALLOC;
246 copy_tv(&org->di_tv, &di->di_tv);
247 }
248 return di;
249}
250
251/*
252 * Remove item "item" from Dictionary "dict" and free it.
253 */
254 void
255dictitem_remove(dict_T *dict, dictitem_T *item)
256{
257 hashitem_T *hi;
258
259 hi = hash_find(&dict->dv_hashtab, item->di_key);
260 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100261 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200262 else
263 hash_remove(&dict->dv_hashtab, hi);
264 dictitem_free(item);
265}
266
267/*
268 * Free a dict item. Also clears the value.
269 */
270 void
271dictitem_free(dictitem_T *item)
272{
273 clear_tv(&item->di_tv);
274 if (item->di_flags & DI_FLAGS_ALLOC)
275 vim_free(item);
276}
277
278/*
279 * Make a copy of dict "d". Shallow if "deep" is FALSE.
280 * The refcount of the new dict is set to 1.
281 * See item_copy() for "copyID".
282 * Returns NULL when out of memory.
283 */
284 dict_T *
285dict_copy(dict_T *orig, int deep, int copyID)
286{
287 dict_T *copy;
288 dictitem_T *di;
289 int todo;
290 hashitem_T *hi;
291
292 if (orig == NULL)
293 return NULL;
294
295 copy = dict_alloc();
296 if (copy != NULL)
297 {
298 if (copyID != 0)
299 {
300 orig->dv_copyID = copyID;
301 orig->dv_copydict = copy;
302 }
303 todo = (int)orig->dv_hashtab.ht_used;
304 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
305 {
306 if (!HASHITEM_EMPTY(hi))
307 {
308 --todo;
309
310 di = dictitem_alloc(hi->hi_key);
311 if (di == NULL)
312 break;
313 if (deep)
314 {
315 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
316 copyID) == FAIL)
317 {
318 vim_free(di);
319 break;
320 }
321 }
322 else
323 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
324 if (dict_add(copy, di) == FAIL)
325 {
326 dictitem_free(di);
327 break;
328 }
329 }
330 }
331
332 ++copy->dv_refcount;
333 if (todo > 0)
334 {
335 dict_unref(copy);
336 copy = NULL;
337 }
338 }
339
340 return copy;
341}
342
343/*
344 * Add item "item" to Dictionary "d".
345 * Returns FAIL when out of memory and when key already exists.
346 */
347 int
348dict_add(dict_T *d, dictitem_T *item)
349{
350 return hash_add(&d->dv_hashtab, item->di_key);
351}
352
353/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200354 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200355 * Returns FAIL when out of memory and when key already exists.
356 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200357 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100358dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200359{
360 dictitem_T *item;
361
362 item = dictitem_alloc((char_u *)key);
363 if (item == NULL)
364 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100365 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200366 item->di_tv.vval.v_number = nr;
367 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200368 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200369 dictitem_free(item);
370 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200371 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200372 return OK;
373}
374
375/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200376 * Add a number entry to dictionary "d".
377 * Returns FAIL when out of memory and when key already exists.
378 */
379 int
380dict_add_number(dict_T *d, char *key, varnumber_T nr)
381{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100382 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200383}
384
385/*
386 * Add a special entry to dictionary "d".
387 * Returns FAIL when out of memory and when key already exists.
388 */
389 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100390dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200391{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100392 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200393}
394
395/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200396 * Add a string entry to dictionary "d".
397 * Returns FAIL when out of memory and when key already exists.
398 */
399 int
400dict_add_string(dict_T *d, char *key, char_u *str)
401{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100402 return dict_add_string_len(d, key, str, -1);
403}
404
405/*
406 * Add a string entry to dictionary "d".
407 * "str" will be copied to allocated memory.
408 * When "len" is -1 use the whole string, otherwise only this many bytes.
409 * Returns FAIL when out of memory and when key already exists.
410 */
411 int
412dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
413{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200414 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100415 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200416
417 item = dictitem_alloc((char_u *)key);
418 if (item == NULL)
419 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200420 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100421 if (str != NULL)
422 {
423 if (len == -1)
424 val = vim_strsave(str);
425 else
426 val = vim_strnsave(str, len);
427 }
428 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200429 if (dict_add(d, item) == FAIL)
430 {
431 dictitem_free(item);
432 return FAIL;
433 }
434 return OK;
435}
436
437/*
438 * Add a list entry to dictionary "d".
439 * Returns FAIL when out of memory and when key already exists.
440 */
441 int
442dict_add_list(dict_T *d, char *key, list_T *list)
443{
444 dictitem_T *item;
445
446 item = dictitem_alloc((char_u *)key);
447 if (item == NULL)
448 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200449 item->di_tv.v_type = VAR_LIST;
450 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100451 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200452 if (dict_add(d, item) == FAIL)
453 {
454 dictitem_free(item);
455 return FAIL;
456 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200457 return OK;
458}
459
460/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100461 * Add a typval_T entry to dictionary "d".
462 * Returns FAIL when out of memory and when key already exists.
463 */
464 int
465dict_add_tv(dict_T *d, char *key, typval_T *tv)
466{
467 dictitem_T *item;
468
469 item = dictitem_alloc((char_u *)key);
470 if (item == NULL)
471 return FAIL;
472 copy_tv(tv, &item->di_tv);
473 if (dict_add(d, item) == FAIL)
474 {
475 dictitem_free(item);
476 return FAIL;
477 }
478 return OK;
479}
480
481/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200482 * Add a callback to dictionary "d".
483 * Returns FAIL when out of memory and when key already exists.
484 */
485 int
486dict_add_callback(dict_T *d, char *key, callback_T *cb)
487{
488 dictitem_T *item;
489
490 item = dictitem_alloc((char_u *)key);
491 if (item == NULL)
492 return FAIL;
493 put_callback(cb, &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 Moolenaar45e18cb2019-04-28 18:05:35 +0200503 * Initializes "iter" for iterating over dictionary items with
504 * dict_iterate_next().
505 * If "var" is not a Dict or an empty Dict then there will be nothing to
506 * iterate over, no error is given.
507 * NOTE: The dictionary must not change until iterating is finished!
508 */
509 void
510dict_iterate_start(typval_T *var, dict_iterator_T *iter)
511{
512 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
513 iter->dit_todo = 0;
514 else
515 {
516 dict_T *d = var->vval.v_dict;
517
518 iter->dit_todo = d->dv_hashtab.ht_used;
519 iter->dit_hi = d->dv_hashtab.ht_array;
520 }
521}
522
523/*
524 * Iterate over the items referred to by "iter". It should be initialized with
525 * dict_iterate_start().
526 * Returns a pointer to the key.
527 * "*tv_result" is set to point to the value for that key.
528 * If there are no more items, NULL is returned.
529 */
530 char_u *
531dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
532{
533 dictitem_T *di;
534 char_u *result;
535
536 if (iter->dit_todo == 0)
537 return NULL;
538
539 while (HASHITEM_EMPTY(iter->dit_hi))
540 ++iter->dit_hi;
541
542 di = HI2DI(iter->dit_hi);
543 result = di->di_key;
544 *tv_result = &di->di_tv;
545
546 --iter->dit_todo;
547 ++iter->dit_hi;
548 return result;
549}
550
551/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200552 * Add a dict entry to dictionary "d".
553 * Returns FAIL when out of memory and when key already exists.
554 */
555 int
556dict_add_dict(dict_T *d, char *key, dict_T *dict)
557{
558 dictitem_T *item;
559
560 item = dictitem_alloc((char_u *)key);
561 if (item == NULL)
562 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200563 item->di_tv.v_type = VAR_DICT;
564 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100565 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200566 if (dict_add(d, item) == FAIL)
567 {
568 dictitem_free(item);
569 return FAIL;
570 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200571 return OK;
572}
573
574/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200575 * Get the number of items in a Dictionary.
576 */
577 long
578dict_len(dict_T *d)
579{
580 if (d == NULL)
581 return 0L;
582 return (long)d->dv_hashtab.ht_used;
583}
584
585/*
586 * Find item "key[len]" in Dictionary "d".
587 * If "len" is negative use strlen(key).
588 * Returns NULL when not found.
589 */
590 dictitem_T *
591dict_find(dict_T *d, char_u *key, int len)
592{
593#define AKEYLEN 200
594 char_u buf[AKEYLEN];
595 char_u *akey;
596 char_u *tofree = NULL;
597 hashitem_T *hi;
598
599 if (d == NULL)
600 return NULL;
601 if (len < 0)
602 akey = key;
603 else if (len >= AKEYLEN)
604 {
605 tofree = akey = vim_strnsave(key, len);
606 if (akey == NULL)
607 return NULL;
608 }
609 else
610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100611 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200612 vim_strncpy(buf, key, len);
613 akey = buf;
614 }
615
616 hi = hash_find(&d->dv_hashtab, akey);
617 vim_free(tofree);
618 if (HASHITEM_EMPTY(hi))
619 return NULL;
620 return HI2DI(hi);
621}
622
623/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100624 * Get a typval_T item from a dictionary and copy it into "rettv".
625 * Returns FAIL if the entry doesn't exist or out of memory.
626 */
627 int
628dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
629{
630 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100631
632 di = dict_find(d, key, -1);
633 if (di == NULL)
634 return FAIL;
635 copy_tv(&di->di_tv, rettv);
636 return OK;
637}
638
639/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200640 * Get a string item from a dictionary.
641 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200642 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200643 * Returns NULL if the entry doesn't exist or out of memory.
644 */
645 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100646dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200647{
648 dictitem_T *di;
649 char_u *s;
650
651 di = dict_find(d, key, -1);
652 if (di == NULL)
653 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100654 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200655 if (save && s != NULL)
656 s = vim_strsave(s);
657 return s;
658}
659
660/*
661 * Get a number item from a dictionary.
662 * Returns 0 if the entry doesn't exist.
663 */
664 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100665dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200666{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200667 return dict_get_number_def(d, key, 0);
668}
669
670/*
671 * Get a number item from a dictionary.
672 * Returns "def" if the entry doesn't exist.
673 */
674 varnumber_T
675dict_get_number_def(dict_T *d, char_u *key, int def)
676{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200677 dictitem_T *di;
678
679 di = dict_find(d, key, -1);
680 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200681 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100682 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200683}
684
685/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200686 * Get a number item from a dictionary.
687 * Returns 0 if the entry doesn't exist.
688 * Give an error if the entry is not a number.
689 */
690 varnumber_T
691dict_get_number_check(dict_T *d, char_u *key)
692{
693 dictitem_T *di;
694
695 di = dict_find(d, key, -1);
696 if (di == NULL)
697 return 0;
698 if (di->di_tv.v_type != VAR_NUMBER)
699 {
700 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
701 return 0;
702 }
703 return tv_get_number(&di->di_tv);
704}
705
706/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200707 * Get a bool item (number or true/false) from a dictionary.
708 * Returns "def" if the entry doesn't exist.
709 */
710 varnumber_T
711dict_get_bool(dict_T *d, char_u *key, int def)
712{
713 dictitem_T *di;
714
715 di = dict_find(d, key, -1);
716 if (di == NULL)
717 return def;
718 return tv_get_bool(&di->di_tv);
719}
720
721/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200722 * Return an allocated string with the string representation of a Dictionary.
723 * May return NULL.
724 */
725 char_u *
726dict2string(typval_T *tv, int copyID, int restore_copyID)
727{
728 garray_T ga;
729 int first = TRUE;
730 char_u *tofree;
731 char_u numbuf[NUMBUFLEN];
732 hashitem_T *hi;
733 char_u *s;
734 dict_T *d;
735 int todo;
736
737 if ((d = tv->vval.v_dict) == NULL)
738 return NULL;
739 ga_init2(&ga, (int)sizeof(char), 80);
740 ga_append(&ga, '{');
741
742 todo = (int)d->dv_hashtab.ht_used;
743 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
744 {
745 if (!HASHITEM_EMPTY(hi))
746 {
747 --todo;
748
749 if (first)
750 first = FALSE;
751 else
752 ga_concat(&ga, (char_u *)", ");
753
754 tofree = string_quote(hi->hi_key, FALSE);
755 if (tofree != NULL)
756 {
757 ga_concat(&ga, tofree);
758 vim_free(tofree);
759 }
760 ga_concat(&ga, (char_u *)": ");
761 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
762 FALSE, restore_copyID, TRUE);
763 if (s != NULL)
764 ga_concat(&ga, s);
765 vim_free(tofree);
766 if (s == NULL || did_echo_string_emsg)
767 break;
768 line_breakcheck();
769
770 }
771 }
772 if (todo > 0)
773 {
774 vim_free(ga.ga_data);
775 return NULL;
776 }
777
778 ga_append(&ga, '}');
779 ga_append(&ga, NUL);
780 return (char_u *)ga.ga_data;
781}
782
783/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200784 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200785 * Return FAIL when there is no valid key.
786 */
787 static int
788get_literal_key(char_u **arg, typval_T *tv)
789{
790 char_u *p;
791
792 if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-')
793 return FAIL;
794
795 for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
796 ;
797 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200798 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200799
Bram Moolenaardb199212020-08-12 18:01:53 +0200800 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200801 return OK;
802}
803
804/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200805 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200806 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200807 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200808 * Return OK or FAIL. Returns NOTDONE for {expr}.
809 */
810 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200811eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200812{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200813 int evaluate = evalarg == NULL ? FALSE
814 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200815 dict_T *d = NULL;
816 typval_T tvkey;
817 typval_T tv;
818 char_u *key = NULL;
819 dictitem_T *item;
820 char_u *start = skipwhite(*arg + 1);
821 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200822 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200823 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200824
Bram Moolenaarcd524592016-07-17 14:57:05 +0200825 /*
826 * First check if it's not a curly-braces thing: {expr}.
827 * Must do this without evaluating, otherwise a function may be called
828 * twice. Unfortunately this means we need to call eval1() twice for the
829 * first item.
830 * But {} is an empty Dictionary.
831 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200832 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200833 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200834 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200835 return FAIL;
Bram Moolenaarae95a392020-08-06 16:38:12 +0200836 if (*skipwhite(start) == '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200837 return NOTDONE;
838 }
839
840 if (evaluate)
841 {
842 d = dict_alloc();
843 if (d == NULL)
844 return FAIL;
845 }
846 tvkey.v_type = VAR_UNKNOWN;
847 tv.v_type = VAR_UNKNOWN;
848
Bram Moolenaar962d7212020-07-04 14:15:00 +0200849 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200850 while (**arg != '}' && **arg != NUL)
851 {
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200852 if ((literal
853 ? get_literal_key(arg, &tvkey)
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200854 : eval1(arg, &tvkey, evalarg)) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200855 goto failret;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200856
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200857 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200858 // previously, so only require it in Vim9 script.
859 if (!vim9script)
860 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200861 if (**arg != ':')
862 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200863 if (*skipwhite(*arg) == ':')
864 semsg(_(e_no_white_space_allowed_before_str), ":");
865 else
866 semsg(_(e_missing_dict_colon), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200867 clear_tv(&tvkey);
868 goto failret;
869 }
870 if (evaluate)
871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100872 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200873 if (key == NULL)
874 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100875 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200876 clear_tv(&tvkey);
877 goto failret;
878 }
879 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200880 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
881 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200882 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarab19d492020-06-27 17:04:05 +0200883 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200884 goto failret;
885 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200886
Bram Moolenaar962d7212020-07-04 14:15:00 +0200887 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200888 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200889 {
890 if (evaluate)
891 clear_tv(&tvkey);
892 goto failret;
893 }
894 if (evaluate)
895 {
896 item = dict_find(d, key, -1);
897 if (item != NULL)
898 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200899 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200900 clear_tv(&tvkey);
901 clear_tv(&tv);
902 goto failret;
903 }
904 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200905 if (item != NULL)
906 {
907 item->di_tv = tv;
908 item->di_tv.v_lock = 0;
909 if (dict_add(d, item) == FAIL)
910 dictitem_free(item);
911 }
912 }
Bram Moolenaara8931942019-09-28 17:25:10 +0200913 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200914
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200915 // the comma should come right after the value, but this wasn't checked
916 // previously, so only require it in Vim9 script.
917 if (!vim9script)
918 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200919 had_comma = **arg == ',';
920 if (had_comma)
921 {
922 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
923 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200924 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200925 goto failret;
926 }
927 *arg = skipwhite(*arg + 1);
928 }
929
930 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +0200931 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200932 if (**arg == '}')
933 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200934 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200935 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200936 if (**arg == ',')
937 semsg(_(e_no_white_space_allowed_before_str), ",");
938 else
939 semsg(_(e_missing_dict_comma), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200940 goto failret;
941 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200942 }
943
944 if (**arg != '}')
945 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200946 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200947failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +0200948 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200949 dict_free(d);
950 return FAIL;
951 }
952
953 *arg = skipwhite(*arg + 1);
954 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200955 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200956
957 return OK;
958}
959
960/*
961 * Go over all entries in "d2" and add them to "d1".
962 * When "action" is "error" then a duplicate key is an error.
963 * When "action" is "force" then a duplicate key is overwritten.
964 * Otherwise duplicate keys are ignored ("action" is "keep").
965 */
966 void
967dict_extend(dict_T *d1, dict_T *d2, char_u *action)
968{
969 dictitem_T *di1;
970 hashitem_T *hi2;
971 int todo;
972 char_u *arg_errmsg = (char_u *)N_("extend() argument");
973
974 todo = (int)d2->dv_hashtab.ht_used;
975 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
976 {
977 if (!HASHITEM_EMPTY(hi2))
978 {
979 --todo;
980 di1 = dict_find(d1, hi2->hi_key, -1);
981 if (d1->dv_scope != 0)
982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 // Disallow replacing a builtin function in l: and g:.
984 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200985 if (d1->dv_scope == VAR_DEF_SCOPE
986 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +0200987 && var_wrong_func_name(hi2->hi_key, di1 == NULL))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200988 break;
989 if (!valid_varname(hi2->hi_key))
990 break;
991 }
992 if (di1 == NULL)
993 {
994 di1 = dictitem_copy(HI2DI(hi2));
995 if (di1 != NULL && dict_add(d1, di1) == FAIL)
996 dictitem_free(di1);
997 }
998 else if (*action == 'e')
999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001000 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001001 break;
1002 }
1003 else if (*action == 'f' && HI2DI(hi2) != di1)
1004 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001005 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001006 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001007 break;
1008 clear_tv(&di1->di_tv);
1009 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1010 }
1011 }
1012 }
1013}
1014
1015/*
1016 * Return the dictitem that an entry in a hashtable points to.
1017 */
1018 dictitem_T *
1019dict_lookup(hashitem_T *hi)
1020{
1021 return HI2DI(hi);
1022}
1023
1024/*
1025 * Return TRUE when two dictionaries have exactly the same key/values.
1026 */
1027 int
1028dict_equal(
1029 dict_T *d1,
1030 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 int ic, // ignore case for strings
1032 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001033{
1034 hashitem_T *hi;
1035 dictitem_T *item2;
1036 int todo;
1037
Bram Moolenaarcd524592016-07-17 14:57:05 +02001038 if (d1 == d2)
1039 return TRUE;
1040 if (dict_len(d1) != dict_len(d2))
1041 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001042 if (dict_len(d1) == 0)
1043 // empty and NULL dicts are considered equal
1044 return TRUE;
1045 if (d1 == NULL || d2 == NULL)
1046 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001047
1048 todo = (int)d1->dv_hashtab.ht_used;
1049 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1050 {
1051 if (!HASHITEM_EMPTY(hi))
1052 {
1053 item2 = dict_find(d2, hi->hi_key, -1);
1054 if (item2 == NULL)
1055 return FALSE;
1056 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1057 return FALSE;
1058 --todo;
1059 }
1060 }
1061 return TRUE;
1062}
1063
1064/*
1065 * Turn a dict into a list:
1066 * "what" == 0: list of keys
1067 * "what" == 1: list of values
1068 * "what" == 2: list of items
1069 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001070 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001071dict_list(typval_T *argvars, typval_T *rettv, int what)
1072{
1073 list_T *l2;
1074 dictitem_T *di;
1075 hashitem_T *hi;
1076 listitem_T *li;
1077 listitem_T *li2;
1078 dict_T *d;
1079 int todo;
1080
1081 if (argvars[0].v_type != VAR_DICT)
1082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001083 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001084 return;
1085 }
1086 if ((d = argvars[0].vval.v_dict) == NULL)
1087 return;
1088
1089 if (rettv_list_alloc(rettv) == FAIL)
1090 return;
1091
1092 todo = (int)d->dv_hashtab.ht_used;
1093 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1094 {
1095 if (!HASHITEM_EMPTY(hi))
1096 {
1097 --todo;
1098 di = HI2DI(hi);
1099
1100 li = listitem_alloc();
1101 if (li == NULL)
1102 break;
1103 list_append(rettv->vval.v_list, li);
1104
1105 if (what == 0)
1106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001107 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001108 li->li_tv.v_type = VAR_STRING;
1109 li->li_tv.v_lock = 0;
1110 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1111 }
1112 else if (what == 1)
1113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001114 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001115 copy_tv(&di->di_tv, &li->li_tv);
1116 }
1117 else
1118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001119 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001120 l2 = list_alloc();
1121 li->li_tv.v_type = VAR_LIST;
1122 li->li_tv.v_lock = 0;
1123 li->li_tv.vval.v_list = l2;
1124 if (l2 == NULL)
1125 break;
1126 ++l2->lv_refcount;
1127
1128 li2 = listitem_alloc();
1129 if (li2 == NULL)
1130 break;
1131 list_append(l2, li2);
1132 li2->li_tv.v_type = VAR_STRING;
1133 li2->li_tv.v_lock = 0;
1134 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1135
1136 li2 = listitem_alloc();
1137 if (li2 == NULL)
1138 break;
1139 list_append(l2, li2);
1140 copy_tv(&di->di_tv, &li2->li_tv);
1141 }
1142 }
1143 }
1144}
1145
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001146/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001147 * "items(dict)" function
1148 */
1149 void
1150f_items(typval_T *argvars, typval_T *rettv)
1151{
1152 dict_list(argvars, rettv, 2);
1153}
1154
1155/*
1156 * "keys()" function
1157 */
1158 void
1159f_keys(typval_T *argvars, typval_T *rettv)
1160{
1161 dict_list(argvars, rettv, 0);
1162}
1163
1164/*
1165 * "values(dict)" function
1166 */
1167 void
1168f_values(typval_T *argvars, typval_T *rettv)
1169{
1170 dict_list(argvars, rettv, 1);
1171}
1172
1173/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001174 * Make each item in the dict readonly (not the value of the item).
1175 */
1176 void
1177dict_set_items_ro(dict_T *di)
1178{
1179 int todo = (int)di->dv_hashtab.ht_used;
1180 hashitem_T *hi;
1181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001182 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001183 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1184 {
1185 if (HASHITEM_EMPTY(hi))
1186 continue;
1187 --todo;
1188 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1189 }
1190}
1191
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001192/*
1193 * "has_key()" function
1194 */
1195 void
1196f_has_key(typval_T *argvars, typval_T *rettv)
1197{
1198 if (argvars[0].v_type != VAR_DICT)
1199 {
1200 emsg(_(e_dictreq));
1201 return;
1202 }
1203 if (argvars[0].vval.v_dict == NULL)
1204 return;
1205
1206 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1207 tv_get_string(&argvars[1]), -1) != NULL;
1208}
1209
1210/*
1211 * "remove({dict})" function
1212 */
1213 void
1214dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1215{
1216 dict_T *d;
1217 char_u *key;
1218 dictitem_T *di;
1219
1220 if (argvars[2].v_type != VAR_UNKNOWN)
1221 semsg(_(e_toomanyarg), "remove()");
1222 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001223 && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001224 {
1225 key = tv_get_string_chk(&argvars[1]);
1226 if (key != NULL)
1227 {
1228 di = dict_find(d, key, -1);
1229 if (di == NULL)
1230 semsg(_(e_dictkey), key);
1231 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1232 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1233 {
1234 *rettv = di->di_tv;
1235 init_tv(&di->di_tv);
1236 dictitem_remove(d, di);
1237 }
1238 }
1239 }
1240}
1241
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001242#endif // defined(FEAT_EVAL)