patch 8.2.1023: Vim9: redefining a function uses a new index every time
Problem: Vim9: redefining a function uses a new index every time.
Solution: When redefining a function clear the contents and re-use the
index.
diff --git a/src/userfunc.c b/src/userfunc.c
index bd229b4..97c8f87 100644
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -409,7 +409,7 @@
fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
if (fp == NULL)
goto errret;
- fp->uf_dfunc_idx = UF_NOT_COMPILED;
+ fp->uf_def_status = UF_NOT_COMPILED;
pt = ALLOC_CLEAR_ONE(partial_T);
if (pt == NULL)
goto errret;
@@ -1001,7 +1001,7 @@
{
// When there is a def-function index do not actually remove the
// function, so we can find the index when defining the function again.
- if (fp->uf_dfunc_idx >= 0)
+ if (fp->uf_def_status == UF_COMPILED)
fp->uf_flags |= FC_DEAD;
else
hash_remove(&func_hashtab, hi);
@@ -1046,7 +1046,7 @@
// clear this function
func_clear_items(fp);
funccal_unref(fp->uf_scoped, fp, force);
- delete_def_function(fp);
+ clear_def_function(fp);
}
/*
@@ -1074,7 +1074,8 @@
func_clear_free(ufunc_T *fp, int force)
{
func_clear(fp, force);
- func_free(fp, force);
+ if (force || fp->uf_dfunc_idx == 0)
+ func_free(fp, force);
}
@@ -1137,7 +1138,7 @@
ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
func_ptr_ref(fp);
- if (fp->uf_dfunc_idx != UF_NOT_COMPILED)
+ if (fp->uf_def_status != UF_NOT_COMPILED)
{
estack_push_ufunc(fp, 1);
save_current_sctx = current_sctx;
@@ -1662,7 +1663,7 @@
// clear the def function index now
fp = HI2UF(hi);
fp->uf_flags &= ~FC_DEAD;
- fp->uf_dfunc_idx = UF_NOT_COMPILED;
+ fp->uf_def_status = UF_NOT_COMPILED;
// Only free functions that are not refcounted, those are
// supposed to be freed when no longer referenced.
@@ -2058,7 +2059,7 @@
msg_start();
if (indent)
msg_puts(" ");
- if (fp->uf_dfunc_idx != UF_NOT_COMPILED)
+ if (fp->uf_def_status != UF_NOT_COMPILED)
msg_puts("def ");
else
msg_puts("function ");
@@ -2107,7 +2108,7 @@
}
msg_putchar(')');
- if (fp->uf_dfunc_idx != UF_NOT_COMPILED)
+ if (fp->uf_def_status != UF_NOT_COMPILED)
{
if (fp->uf_ret_type != &t_void)
{
@@ -2624,7 +2625,7 @@
if (!got_int)
{
msg_putchar('\n');
- if (fp->uf_dfunc_idx != UF_NOT_COMPILED)
+ if (fp->uf_def_status != UF_NOT_COMPILED)
msg_puts(" enddef");
else
msg_puts(" endfunction");
@@ -3097,6 +3098,7 @@
fp->uf_profiling = FALSE;
fp->uf_prof_initialized = FALSE;
#endif
+ clear_def_function(fp);
}
}
}
@@ -3162,7 +3164,7 @@
fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
if (fp == NULL)
goto erret;
- fp->uf_dfunc_idx = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
+ fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
: UF_NOT_COMPILED;
if (fudi.fd_dict != NULL)
@@ -3219,7 +3221,7 @@
{
int lnum_save = SOURCING_LNUM;
- fp->uf_dfunc_idx = UF_TO_BE_COMPILED;
+ fp->uf_def_status = UF_TO_BE_COMPILED;
// error messages are for the first function line
SOURCING_LNUM = sourcing_lnum_top;
@@ -3289,7 +3291,7 @@
SOURCING_LNUM = lnum_save;
}
else
- fp->uf_dfunc_idx = UF_NOT_COMPILED;
+ fp->uf_def_status = UF_NOT_COMPILED;
fp->uf_lines = newlines;
if ((flags & FC_CLOSURE) != 0)
@@ -3372,7 +3374,7 @@
--todo;
ufunc = HI2UF(hi);
if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
- && ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED)
+ && ufunc->uf_def_status == UF_TO_BE_COMPILED)
{
compile_def_function(ufunc, FALSE, NULL);