Problem: Vim9: after reloading a script variable index may be invalid.
Solution: When the sequence number doesn't match give an error for using a
script-local variable from a compiled function. (closes #7547)
INIT(= N_("E1147: List not set"));
EXTERN char e_cannot_index_str[]
INIT(= N_("E1148: Cannot index a %s"));
+EXTERN char e_script_variable_invalid_after_reload_in_function_str[]
+ INIT(= N_("E1149: Script variable is invalid after reload in function %s"));
if (ret_sid != NULL)
*ret_sid = current_sctx.sc_sid;
}
+ si->sn_script_seq = current_sctx.sc_seq;
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
} imported_T;
/*
- * Growarray to store info about already sourced scripts.
- * For Unix also store the dev/ino, so that we don't have to stat() each
- * script when going through the list.
+ * Info about an already sourced scripts.
*/
typedef struct
{
char_u *sn_name;
+ int sn_script_seq; // latest sctx_T sc_seq value
// "sn_vars" stores the s: variables currently valid. When leaving a block
// variables local to that block are removed.
CheckDefAndScriptFailure(lines, 'E1144:', 1)
enddef
+def Test_script_var_gone_when_sourced_twice()
+ var lines =<< trim END
+ vim9script
+ if exists('g:guard')
+ finish
+ endif
+ g:guard = 1
+ var name = 'thename'
+ def g:GetName(): string
+ return name
+ enddef
+ def g:SetName(arg: string)
+ name = arg
+ enddef
+ END
+ writefile(lines, 'XscriptTwice.vim')
+ so XscriptTwice.vim
+ assert_equal('thename', g:GetName())
+ g:SetName('newname')
+ assert_equal('newname', g:GetName())
+ so XscriptTwice.vim
+ assert_fails('call g:GetName()', 'E1149:')
+ assert_fails('call g:SetName("x")', 'E1149:')
+
+ delfunc g:GetName
+ delfunc g:SetName
+ delete('XscriptTwice.vim')
+ unlet g:guard
+enddef
+
+def Test_import_gone_when_sourced_twice()
+ var exportlines =<< trim END
+ vim9script
+ if exists('g:guard')
+ finish
+ endif
+ g:guard = 1
+ export var name = 'someName'
+ END
+ writefile(exportlines, 'XexportScript.vim')
+
+ var lines =<< trim END
+ vim9script
+ import name from './XexportScript.vim'
+ def g:GetName(): string
+ return name
+ enddef
+ END
+ writefile(lines, 'XscriptImport.vim')
+ so XscriptImport.vim
+ assert_equal('someName', g:GetName())
+
+ so XexportScript.vim
+ assert_fails('call g:GetName()', 'E1149:')
+
+ delfunc g:GetName
+ delete('XexportScript.vim')
+ delete('XscriptImport.vim')
+ unlet g:guard
+enddef
+
" Keep this last, it messes up highlighting.
def Test_substitute_cmd()
new
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 2208,
/**/
2207,
/**/
// arguments to ISN_LOADSCRIPT and ISN_STORESCRIPT
typedef struct {
- int script_sid; // script ID
- int script_idx; // index in sn_var_vals
+ int sref_sid; // script ID
+ int sref_idx; // index in sn_var_vals
+ int sref_seq; // sn_script_seq when compiled
+} scriptref_T;
+
+typedef struct {
+ scriptref_T *scriptref;
} script_T;
// arguments to ISN_UNLET
int df_idx; // index in def_functions
int df_deleted; // if TRUE function was deleted
char_u *df_name; // name used for error messages
+ int df_script_seq; // Value of sctx_T sc_seq when the function
+ // was compiled.
garray_T df_def_args_isn; // default argument instructions
isn_T *df_instr; // function body to be executed
type_T *type)
{
isn_T *isn;
+ scriptref_T *sref;
+ scriptitem_T *si = SCRIPT_ITEM(sid);
RETURN_OK_IF_SKIP(cctx);
if (isn_type == ISN_LOADSCRIPT)
isn = generate_instr_drop(cctx, isn_type, 1);
if (isn == NULL)
return FAIL;
- isn->isn_arg.script.script_sid = sid;
- isn->isn_arg.script.script_idx = idx;
+
+ // This requires three arguments, which doesn't fit in an instruction, thus
+ // we need to allocate a struct for this.
+ sref = ALLOC_ONE(scriptref_T);
+ if (sref == NULL)
+ return FAIL;
+ isn->isn_arg.script.scriptref = sref;
+ sref->sref_sid = sid;
+ sref->sref_idx = idx;
+ sref->sref_seq = si->sn_script_seq;
return OK;
}
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
dfunc->df_deleted = FALSE;
+ dfunc->df_script_seq = current_sctx.sc_seq;
dfunc->df_instr = instr->ga_data;
dfunc->df_instr_count = instr->ga_len;
dfunc->df_varcount = cctx.ctx_locals_count;
vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
break;
+ case ISN_LOADSCRIPT:
+ case ISN_STORESCRIPT:
+ vim_free(isn->isn_arg.script.scriptref);
+ break;
+
case ISN_2BOOL:
case ISN_2STRING:
case ISN_2STRING_ANY:
case ISN_LOADGDICT:
case ISN_LOADOUTER:
case ISN_LOADREG:
- case ISN_LOADSCRIPT:
case ISN_LOADTDICT:
case ISN_LOADV:
case ISN_LOADWDICT:
case ISN_STORENR:
case ISN_STOREOUTER:
case ISN_STOREREG:
- case ISN_STORESCRIPT:
case ISN_STOREV:
case ISN_STRINDEX:
case ISN_STRSLICE:
// load s: variable in Vim9 script
case ISN_LOADSCRIPT:
{
- scriptitem_T *si =
- SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
+ scriptref_T *sref = iptr->isn_arg.script.scriptref;
+ dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ + ectx.ec_dfunc_idx;
+ scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
svar_T *sv;
- sv = ((svar_T *)si->sn_var_vals.ga_data)
- + iptr->isn_arg.script.script_idx;
+ if (sref->sref_seq != si->sn_script_seq)
+ {
+ // The script was reloaded after the function was
+ // compiled, the script_idx may not be valid.
+ semsg(_(e_script_variable_invalid_after_reload_in_function_str),
+ dfunc->df_ufunc->uf_name_exp);
+ goto failed;
+ }
+ sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
allocate_if_null(sv->sv_tv);
if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
goto failed;
// store script-local variable in Vim9 script
case ISN_STORESCRIPT:
{
- scriptitem_T *si = SCRIPT_ITEM(
- iptr->isn_arg.script.script_sid);
- svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
- + iptr->isn_arg.script.script_idx;
+ scriptref_T *sref = iptr->isn_arg.script.scriptref;
+ dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ + ectx.ec_dfunc_idx;
+ scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
+ svar_T *sv;
+ if (sref->sref_seq != si->sn_script_seq)
+ {
+ // The script was reloaded after the function was
+ // compiled, the script_idx may not be valid.
+ SOURCING_LNUM = iptr->isn_lnum;
+ semsg(_(e_script_variable_invalid_after_reload_in_function_str),
+ dfunc->df_ufunc->uf_name_exp);
+ goto failed;
+ }
+ sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
--ectx.ec_stack.ga_len;
clear_tv(sv->sv_tv);
*sv->sv_tv = *STACK_TV_BOT(0);
break;
case ISN_LOADSCRIPT:
{
- scriptitem_T *si =
- SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
+ scriptref_T *sref = iptr->isn_arg.script.scriptref;
+ scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
- + iptr->isn_arg.script.script_idx;
+ + sref->sref_idx;
smsg("%4d LOADSCRIPT %s-%d from %s", current,
sv->sv_name,
- iptr->isn_arg.script.script_idx,
+ sref->sref_idx,
si->sn_name);
}
break;
break;
case ISN_STORESCRIPT:
{
- scriptitem_T *si =
- SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
+ scriptref_T *sref = iptr->isn_arg.script.scriptref;
+ scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
- + iptr->isn_arg.script.script_idx;
+ + sref->sref_idx;
smsg("%4d STORESCRIPT %s-%d in %s", current,
sv->sv_name,
- iptr->isn_arg.script.script_idx,
+ sref->sref_idx,
si->sn_name);
}
break;