0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 8.2.2301: Vim9: cannot unlet a dict or list item

Problem:    Vim9: cannot unlet a dict or list item.
Solution:   Add ISN_UNLETINDEX.  Refactor assignment code to use for unlet.
This commit is contained in:
Bram Moolenaar
2021-01-04 21:57:11 +01:00
parent d62d87d8f3
commit 752fc692ac
5 changed files with 689 additions and 437 deletions

View File

@@ -1349,14 +1349,47 @@ def Test_unlet()
assert_false(exists('s:somevar'))
unlet! s:somevar
# dict unlet
var dd = {a: 1, b: 2, c: 3}
unlet dd['a']
unlet dd.c
assert_equal({b: 2}, dd)
# list unlet
var ll = [1, 2, 3, 4]
unlet ll[1]
unlet ll[-1]
assert_equal([1, 3], ll)
# list of dict unlet
var dl = [{a: 1, b: 2}, {c: 3}]
unlet dl[0]['b']
assert_equal([{a: 1}, {c: 3}], dl)
CheckDefExecFailure([
'var ll = test_null_list()',
'unlet ll[0]',
], 'E684:')
CheckDefExecFailure([
'var ll = [1]',
'unlet ll[2]',
], 'E684:')
CheckDefExecFailure([
'var dd = test_null_dict()',
'unlet dd["a"]',
], 'E716:')
CheckDefExecFailure([
'var dd = {a: 1}',
'unlet dd["b"]',
], 'E716:')
# can compile unlet before variable exists
# This doesn't work yet
#g:someDict = {key: 'val'}
#var k = 'key'
#unlet g:someDict[k]
#assert_equal({}, g:someDict)
#unlet g:someDict
#assert_false(exists('g:someDict'))
g:someDict = {key: 'val'}
var k = 'key'
unlet g:someDict[k]
assert_equal({}, g:someDict)
unlet g:someDict
assert_false(exists('g:someDict'))
CheckScriptFailure([
'vim9script',

View File

@@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2301,
/**/
2300,
/**/

View File

@@ -60,6 +60,7 @@ typedef enum {
ISN_UNLET, // unlet variable isn_arg.unlet.ul_name
ISN_UNLETENV, // unlet environment variable isn_arg.unlet.ul_name
ISN_UNLETINDEX, // unlet item of list or dict
ISN_LOCKCONST, // lock constant value

File diff suppressed because it is too large Load Diff

View File

@@ -1783,6 +1783,10 @@ call_def_function(
typval_T *tv_dest = STACK_TV_BOT(-1);
int status = OK;
// Stack contains:
// -3 value to be stored
// -2 index
// -1 dict or list
tv = STACK_TV_BOT(-3);
SOURCING_LNUM = iptr->isn_lnum;
if (dest_type == VAR_ANY)
@@ -1898,6 +1902,91 @@ call_def_function(
}
break;
// unlet item in list or dict variable
case ISN_UNLETINDEX:
{
typval_T *tv_idx = STACK_TV_BOT(-2);
typval_T *tv_dest = STACK_TV_BOT(-1);
int status = OK;
// Stack contains:
// -2 index
// -1 dict or list
if (tv_dest->v_type == VAR_DICT)
{
// unlet a dict item, index must be a string
if (tv_idx->v_type != VAR_STRING)
{
semsg(_(e_expected_str_but_got_str),
vartype_name(VAR_STRING),
vartype_name(tv_idx->v_type));
status = FAIL;
}
else
{
dict_T *d = tv_dest->vval.v_dict;
char_u *key = tv_idx->vval.v_string;
dictitem_T *di = NULL;
if (key == NULL)
key = (char_u *)"";
if (d != NULL)
di = dict_find(d, key, (int)STRLEN(key));
if (di == NULL)
{
// NULL dict is equivalent to empty dict
semsg(_(e_dictkey), key);
status = FAIL;
}
else
{
// TODO: check for dict or item locked
dictitem_remove(d, di);
}
}
}
else if (tv_dest->v_type == VAR_LIST)
{
// unlet a List item, index must be a number
if (tv_idx->v_type != VAR_NUMBER)
{
semsg(_(e_expected_str_but_got_str),
vartype_name(VAR_NUMBER),
vartype_name(tv_idx->v_type));
status = FAIL;
}
else
{
list_T *l = tv_dest->vval.v_list;
varnumber_T n = tv_idx->vval.v_number;
listitem_T *li = NULL;
li = list_find(l, n);
if (li == NULL)
{
semsg(_(e_listidx), n);
status = FAIL;
}
else
// TODO: check for list or item locked
listitem_remove(l, li);
}
}
else
{
status = FAIL;
semsg(_(e_cannot_index_str),
vartype_name(tv_dest->v_type));
}
clear_tv(tv_idx);
clear_tv(tv_dest);
ectx.ec_stack.ga_len -= 2;
if (status == FAIL)
goto on_error;
}
break;
// push constant
case ISN_PUSHNR:
case ISN_PUSHBOOL:
@@ -3649,6 +3738,9 @@ ex_disassemble(exarg_T *eap)
iptr->isn_arg.unlet.ul_forceit ? "!" : "",
iptr->isn_arg.unlet.ul_name);
break;
case ISN_UNLETINDEX:
smsg("%4d UNLETINDEX", current);
break;
case ISN_LOCKCONST:
smsg("%4d LOCKCONST", current);
break;