@@ -863,6 +863,31 @@ allocate_if_null(typval_T *tv)
863863 }
864864}
865865
866+ static svar_T *
867+ get_script_svar (scriptref_T * sref , ectx_T * ectx )
868+ {
869+ scriptitem_T * si = SCRIPT_ITEM (sref -> sref_sid );
870+ dfunc_T * dfunc = ((dfunc_T * )def_functions .ga_data )
871+ + ectx -> ec_dfunc_idx ;
872+ svar_T * sv ;
873+
874+ if (sref -> sref_seq != si -> sn_script_seq )
875+ {
876+ // The script was reloaded after the function was
877+ // compiled, the script_idx may not be valid.
878+ semsg (_ (e_script_variable_invalid_after_reload_in_function_str ),
879+ dfunc -> df_ufunc -> uf_name_exp );
880+ return NULL ;
881+ }
882+ sv = ((svar_T * )si -> sn_var_vals .ga_data ) + sref -> sref_idx ;
883+ if (!equal_type (sv -> sv_type , sref -> sref_type ))
884+ {
885+ emsg (_ (e_script_variable_type_changed ));
886+ return NULL ;
887+ }
888+ return sv ;
889+ }
890+
866891/*
867892 * Execute a function by "name".
868893 * This can be a builtin function, user function or a funcref.
@@ -1406,20 +1431,11 @@ call_def_function(
14061431 case ISN_LOADSCRIPT :
14071432 {
14081433 scriptref_T * sref = iptr -> isn_arg .script .scriptref ;
1409- dfunc_T * dfunc = ((dfunc_T * )def_functions .ga_data )
1410- + ectx .ec_dfunc_idx ;
1411- scriptitem_T * si = SCRIPT_ITEM (sref -> sref_sid );
14121434 svar_T * sv ;
14131435
1414- if (sref -> sref_seq != si -> sn_script_seq )
1415- {
1416- // The script was reloaded after the function was
1417- // compiled, the script_idx may not be valid.
1418- semsg (_ (e_script_variable_invalid_after_reload_in_function_str ),
1419- dfunc -> df_ufunc -> uf_name_exp );
1436+ sv = get_script_svar (sref , & ectx );
1437+ if (sv == NULL )
14201438 goto failed ;
1421- }
1422- sv = ((svar_T * )si -> sn_var_vals .ga_data ) + sref -> sref_idx ;
14231439 allocate_if_null (sv -> sv_tv );
14241440 if (GA_GROW (& ectx .ec_stack , 1 ) == FAIL )
14251441 goto failed ;
@@ -1628,22 +1644,12 @@ call_def_function(
16281644 // store script-local variable in Vim9 script
16291645 case ISN_STORESCRIPT :
16301646 {
1631- scriptref_T * sref = iptr -> isn_arg .script .scriptref ;
1632- dfunc_T * dfunc = ((dfunc_T * )def_functions .ga_data )
1633- + ectx .ec_dfunc_idx ;
1634- scriptitem_T * si = SCRIPT_ITEM (sref -> sref_sid );
1635- svar_T * sv ;
1647+ scriptref_T * sref = iptr -> isn_arg .script .scriptref ;
1648+ svar_T * sv ;
16361649
1637- if (sref -> sref_seq != si -> sn_script_seq )
1638- {
1639- // The script was reloaded after the function was
1640- // compiled, the script_idx may not be valid.
1641- SOURCING_LNUM = iptr -> isn_lnum ;
1642- semsg (_ (e_script_variable_invalid_after_reload_in_function_str ),
1643- dfunc -> df_ufunc -> uf_name_exp );
1650+ sv = get_script_svar (sref , & ectx );
1651+ if (sv == NULL )
16441652 goto failed ;
1645- }
1646- sv = ((svar_T * )si -> sn_var_vals .ga_data ) + sref -> sref_idx ;
16471653 -- ectx .ec_stack .ga_len ;
16481654 clear_tv (sv -> sv_tv );
16491655 * sv -> sv_tv = * STACK_TV_BOT (0 );
0 commit comments