Skip to content

Commit cbbc48f

Browse files
committed
patch 8.2.4131: Vim9: calling function in autoload import does not work
Problem: Vim9: calling function in autoload import does not work in a :def function. Solution: When a variable is not found and a PCALL follows use a funcref. (closes #9550)
1 parent 31dcc8d commit cbbc48f

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/testdir/test_vim9_import.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,10 @@ def Test_vim9script_autoload_call()
12641264
var lines =<< trim END
12651265
vim9script autoload
12661266

1267+
export def RetArg(arg: string): string
1268+
return arg
1269+
enddef
1270+
12671271
export def Getother()
12681272
g:result = 'other'
12691273
enddef
@@ -1273,6 +1277,13 @@ def Test_vim9script_autoload_call()
12731277
lines =<< trim END
12741278
vim9script
12751279
import autoload 'another.vim'
1280+
1281+
# compile this before 'another.vim' is loaded
1282+
def CallAnother()
1283+
assert_equal('foo', 'foo'->another.RetArg())
1284+
enddef
1285+
CallAnother()
1286+
12761287
call another.Getother()
12771288
assert_equal('other', g:result)
12781289
END

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
4131,
753755
/**/
754756
4130,
755757
/**/

src/vim9execute.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,10 +2200,12 @@ exec_instructions(ectx_T *ectx)
22002200
case ISN_LOADW:
22012201
case ISN_LOADT:
22022202
{
2203-
dictitem_T *di = NULL;
2204-
hashtab_T *ht = NULL;
2205-
char namespace;
2203+
dictitem_T *di = NULL;
2204+
hashtab_T *ht = NULL;
2205+
char namespace;
22062206

2207+
if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2208+
goto theend;
22072209
switch (iptr->isn_type)
22082210
{
22092211
case ISN_LOADG:
@@ -2227,14 +2229,38 @@ exec_instructions(ectx_T *ectx)
22272229
}
22282230
di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
22292231

2230-
if (di == NULL && ht == get_globvar_ht())
2232+
if (di == NULL && ht == get_globvar_ht()
2233+
&& vim_strchr(iptr->isn_arg.string,
2234+
AUTOLOAD_CHAR) != NULL)
22312235
{
2232-
// may need to load autoload script
2236+
// Global variable has an autoload name, may still need
2237+
// to load the script.
22332238
if (script_autoload(iptr->isn_arg.string, FALSE))
22342239
di = find_var_in_ht(ht, 0,
22352240
iptr->isn_arg.string, TRUE);
22362241
if (did_emsg)
22372242
goto on_error;
2243+
if (di == NULL)
2244+
{
2245+
isn_T *next = &ectx->ec_instr[ectx->ec_iidx];
2246+
2247+
// When compiling "script.Func()" when "script" is
2248+
// an autoload import then this results in
2249+
// "LOADG script#Func" because we don't know if it
2250+
// is a funcref variable or a function name. In
2251+
// that case a PCALL follows, push the function
2252+
// name instead.
2253+
if (next->isn_type == ISN_PCALL)
2254+
{
2255+
tv = STACK_TV_BOT(0);
2256+
tv->v_type = VAR_FUNC;
2257+
tv->v_lock = 0;
2258+
tv->vval.v_string =
2259+
vim_strsave(iptr->isn_arg.string);
2260+
++ectx->ec_stack.ga_len;
2261+
break;
2262+
}
2263+
}
22382264
}
22392265

22402266
if (di == NULL)
@@ -2246,8 +2272,6 @@ exec_instructions(ectx_T *ectx)
22462272
}
22472273
else
22482274
{
2249-
if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2250-
goto theend;
22512275
copy_tv(&di->di_tv, STACK_TV_BOT(0));
22522276
++ectx->ec_stack.ga_len;
22532277
}

0 commit comments

Comments
 (0)