diff src/userfunc.c @ 22643:71b57779177d v8.2.1870

patch 8.2.1870: Vim9: no need to keep all script variables Commit: https://github.com/vim/vim/commit/39ca4127a094d8aca6f77c01be4f3fea506d5cb7 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Oct 20 14:25:07 2020 +0200 patch 8.2.1870: Vim9: no need to keep all script variables Problem: Vim9: no need to keep all script variables. Solution: Only keep script variables when a function was defined that could use them. Fix freeing static string on exit.
author Bram Moolenaar <Bram@vim.org>
date Tue, 20 Oct 2020 14:30:04 +0200
parents 107eae953b87
children 8a5369f5f2b4
line wrap: on
line diff
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -3471,27 +3471,35 @@ define_function(exarg_T *eap, char_u *na
 
     if (eap->cmdidx == CMD_def)
     {
-	int	lnum_save = SOURCING_LNUM;
+	int	    lnum_save = SOURCING_LNUM;
+	cstack_T    *cstack = eap->cstack;
 
 	fp->uf_def_status = UF_TO_BE_COMPILED;
 
 	// error messages are for the first function line
 	SOURCING_LNUM = sourcing_lnum_top;
 
-	if (eap->cstack != NULL && eap->cstack->cs_idx >= 0)
+	if (cstack != NULL && cstack->cs_idx >= 0)
 	{
-	    int count = eap->cstack->cs_idx + 1;
+	    int	    count = cstack->cs_idx + 1;
+	    int	    i;
 
 	    // The block context may be needed for script variables declared in
-	    // a block visible now but not when the function is compiled.
+	    // a block that is visible now but not when the function is called
+	    // later.
 	    fp->uf_block_ids = ALLOC_MULT(int, count);
 	    if (fp->uf_block_ids != NULL)
 	    {
-		mch_memmove(fp->uf_block_ids, eap->cstack->cs_block_id,
+		mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
 							  sizeof(int) * count);
 		fp->uf_block_depth = count;
 	    }
-	    // TODO: set flag in each block to indicate a function was defined
+
+	    // Set flag in each block to indicate a function was defined.  This
+	    // is used to keep the variable when leaving the block, see
+	    // hide_script_var().
+	    for (i = 0; i <= cstack->cs_idx; ++i)
+		cstack->cs_flags[i] |= CSF_FUNC_DEF;
 	}
 
 	// parse the argument types