0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 8.2.0605: Vim9: cannot unlet an environment variable

Problem:    Vim9: cannot unlet an environment variable.
Solution:   Implement unlet for $VAR.
This commit is contained in:
Bram Moolenaar
2020-04-19 18:27:26 +02:00
parent eb58a24658
commit 7bdaea6e0d
6 changed files with 27 additions and 5 deletions

View File

@@ -130,6 +130,7 @@ def s:ScriptFuncUnlet()
g:somevar = "value" g:somevar = "value"
unlet g:somevar unlet g:somevar
unlet! g:somevar unlet! g:somevar
unlet $SOMEVAR
enddef enddef
def Test_disassemble_unlet() def Test_disassemble_unlet()
@@ -141,7 +142,9 @@ def Test_disassemble_unlet()
'unlet g:somevar.*' .. 'unlet g:somevar.*' ..
'\d UNLET g:somevar.*' .. '\d UNLET g:somevar.*' ..
'unlet! g:somevar.*' .. 'unlet! g:somevar.*' ..
'\d UNLET! g:somevar.*', '\d UNLET! g:somevar.*' ..
'unlet $SOMEVAR.*' ..
'\d UNLETENV $SOMEVAR.*',
res) res)
enddef enddef

View File

@@ -289,6 +289,11 @@ def Test_unlet()
' unlet s:svar', ' unlet s:svar',
'enddef', 'enddef',
], 'E1081:') ], 'E1081:')
$ENVVAR = 'foobar'
assert_equal('foobar', $ENVVAR)
unlet $ENVVAR
assert_equal('', $ENVVAR)
enddef enddef
func Test_wrong_type() func Test_wrong_type()

View File

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

View File

@@ -45,6 +45,7 @@ typedef enum {
ISN_STORENR, // store number into local variable isn_arg.storenr.stnr_idx ISN_STORENR, // store number into local variable isn_arg.storenr.stnr_idx
ISN_UNLET, // unlet variable isn_arg.unlet.ul_name ISN_UNLET, // unlet variable isn_arg.unlet.ul_name
ISN_UNLETENV, // unlet environment variable isn_arg.unlet.ul_name
// constants // constants
ISN_PUSHNR, // push number isn_arg.number ISN_PUSHNR, // push number isn_arg.number

View File

@@ -990,12 +990,12 @@ generate_LOADV(
* Generate an ISN_UNLET instruction. * Generate an ISN_UNLET instruction.
*/ */
static int static int
generate_UNLET(cctx_T *cctx, char_u *name, int forceit) generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
{ {
isn_T *isn; isn_T *isn;
RETURN_OK_IF_SKIP(cctx); RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_UNLET)) == NULL) if ((isn = generate_instr(cctx, isn_type)) == NULL)
return FAIL; return FAIL;
isn->isn_arg.unlet.ul_name = vim_strsave(name); isn->isn_arg.unlet.ul_name = vim_strsave(name);
isn->isn_arg.unlet.ul_forceit = forceit; isn->isn_arg.unlet.ul_forceit = forceit;
@@ -4594,10 +4594,12 @@ compile_unlet(
// Normal name. Only supports g:, w:, t: and b: namespaces. // Normal name. Only supports g:, w:, t: and b: namespaces.
*name_end = NUL; *name_end = NUL;
if (check_vim9_unlet(p) == FAIL) if (*p == '$')
ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
else if (check_vim9_unlet(p) == FAIL)
ret = FAIL; ret = FAIL;
else else
ret = generate_UNLET(cctx, p, eap->forceit); ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
*name_end = cc; *name_end = cc;
return ret; return ret;
@@ -6363,6 +6365,7 @@ delete_instr(isn_T *isn)
break; break;
case ISN_UNLET: case ISN_UNLET:
case ISN_UNLETENV:
vim_free(isn->isn_arg.unlet.ul_name); vim_free(isn->isn_arg.unlet.ul_name);
break; break;

View File

@@ -1073,6 +1073,9 @@ call_def_function(
iptr->isn_arg.unlet.ul_forceit) == FAIL) iptr->isn_arg.unlet.ul_forceit) == FAIL)
goto failed; goto failed;
break; break;
case ISN_UNLETENV:
vim_unsetenv(iptr->isn_arg.unlet.ul_name);
break;
// create a list from items on the stack; uses a single allocation // create a list from items on the stack; uses a single allocation
// for the list header and the items // for the list header and the items
@@ -2119,6 +2122,11 @@ ex_disassemble(exarg_T *eap)
iptr->isn_arg.unlet.ul_forceit ? "!" : "", iptr->isn_arg.unlet.ul_forceit ? "!" : "",
iptr->isn_arg.unlet.ul_name); iptr->isn_arg.unlet.ul_name);
break; break;
case ISN_UNLETENV:
smsg("%4d UNLETENV%s $%s", current,
iptr->isn_arg.unlet.ul_forceit ? "!" : "",
iptr->isn_arg.unlet.ul_name);
break;
case ISN_NEWLIST: case ISN_NEWLIST:
smsg("%4d NEWLIST size %lld", current, smsg("%4d NEWLIST size %lld", current,
(long long)(iptr->isn_arg.number)); (long long)(iptr->isn_arg.number));