Problem: Vim9: Reloading marks a :def function as deleted.
Solution: Clear the function contents but keep the index.
def g:SomeFunc()
....
-There is one gotcha: If a compiled function is replaced and it is called from
-another compiled function that is not replaced, it will try to call the
-function from before it was replaced, which no longer exists. This doesn't
-work: >
- vimscript noclear
-
- def ReplaceMe()
- echo 'function redefined every time'
- enddef
-
- if exists('s:loaded') | finish | endif
- var s:loaded = true
-
- def NotReplaced()
- ReplaceMe() # Error if ReplaceMe() was redefined
- enddef
-
Variable declarations with :var, :final and :const ~
*vim9-declaration* *:var*
var s:notReloaded = 'yes'
s:reloaded = 'first'
def g:Values(): list<string>
- return [s:reloaded, s:notReloaded, Once()]
- enddef
- def g:CallAgain(): string
- return Again()
+ return [s:reloaded, s:notReloaded, Again(), Once()]
enddef
def Once(): string
g:loadCount = 0
source XReloaded
assert_equal(1, g:loadCount)
- assert_equal(['first', 'yes', 'once'], g:Values())
- assert_equal('again', g:CallAgain())
+ assert_equal(['first', 'yes', 'again', 'once'], g:Values())
source XReloaded
assert_equal(2, g:loadCount)
- assert_equal(['init', 'yes', 'once'], g:Values())
- assert_fails('call g:CallAgain()', 'E933:')
+ assert_equal(['init', 'yes', 'again', 'once'], g:Values())
source XReloaded
assert_equal(3, g:loadCount)
- assert_equal(['init', 'yes', 'once'], g:Values())
- assert_fails('call g:CallAgain()', 'E933:')
+ assert_equal(['init', 'yes', 'again', 'once'], g:Values())
delete('Xreloaded')
delfunc g:Values
- delfunc g:CallAgain
unlet g:loadCount
enddef
fp->uf_profiling = FALSE;
fp->uf_prof_initialized = FALSE;
#endif
- unlink_def_function(fp);
+ fp->uf_def_status = UF_NOT_COMPILED;
}
}
}
fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
if (fp == NULL)
goto erret;
- fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
- : UF_NOT_COMPILED;
if (fudi.fd_dict != NULL)
{
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 2223,
/**/
2222,
/**/
int ctx_has_cmdmod; // ISN_CMDMOD was generated
};
-static void delete_def_function_contents(dfunc_T *dfunc);
+static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
/*
* Lookup variable "name" in the local scope and return it in "lvar".
int new_def_function = FALSE;
// When using a function that was compiled before: Free old instructions.
- // Otherwise add a new entry in "def_functions".
+ // The index is reused. Otherwise add a new entry in "def_functions".
if (ufunc->uf_dfunc_idx > 0)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
- delete_def_function_contents(dfunc);
+ delete_def_function_contents(dfunc, FALSE);
}
else
{
* Free all instructions for "dfunc" except df_name.
*/
static void
-delete_def_function_contents(dfunc_T *dfunc)
+delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
{
int idx;
for (idx = 0; idx < dfunc->df_instr_count; ++idx)
delete_instr(dfunc->df_instr + idx);
VIM_CLEAR(dfunc->df_instr);
+ dfunc->df_instr = NULL;
}
- dfunc->df_deleted = TRUE;
+ if (mark_deleted)
+ dfunc->df_deleted = TRUE;
+ if (dfunc->df_ufunc != NULL)
+ dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
}
/*
+ ufunc->uf_dfunc_idx;
if (--dfunc->df_refcount <= 0)
- delete_def_function_contents(dfunc);
+ delete_def_function_contents(dfunc, TRUE);
ufunc->uf_def_status = UF_NOT_COMPILED;
ufunc->uf_dfunc_idx = 0;
if (dfunc->df_ufunc == ufunc)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
- delete_def_function_contents(dfunc);
+ delete_def_function_contents(dfunc, TRUE);
vim_free(dfunc->df_name);
}