Skip to content

Commit e9ae35f

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.1154: Vim9: not able to use autoload class accross scripts
Problem: Vim9: not able to use autoload class accross scripts Solution: make it work, re-enable the test (Yegappan Lakshmanan) fixes: #15031 closes: #16748 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 3d75ec7 commit e9ae35f

4 files changed

Lines changed: 111 additions & 72 deletions

File tree

src/evalvars.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,9 @@ eval_variable(
30873087
dictitem_T *v = find_var_in_ht(ht, 0, name,
30883088
flags & EVAL_VAR_NOAUTOLOAD);
30893089

3090+
if (v == NULL)
3091+
v = find_var_autoload_prefix(name, sid, NULL, NULL);
3092+
30903093
if (v != NULL)
30913094
{
30923095
tv = &v->di_tv;

src/testdir/test_vim9_import.vim

Lines changed: 105 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,77 +3523,111 @@ def Test_use_imported_class_as_type()
35233523
source Xdir/import/a.vim
35243524
enddef
35253525

3526-
" FIXME: The following test currently fails.
3527-
" " Test for using an autoloaded class from another autoloaded script
3528-
" def Test_class_from_auloaded_script()
3529-
" mkdir('Xdir', 'R')
3530-
" var save_rtp = &rtp
3531-
" &rtp = getcwd()
3532-
" exe 'set rtp^=' .. getcwd() .. '/Xdir'
3533-
"
3534-
" mkdir('Xdir/autoload/SomeClass/bar', 'p')
3535-
"
3536-
" var lines =<< trim END
3537-
" vim9script
3538-
"
3539-
" export class Baz
3540-
" static var v1: string = "v1"
3541-
" var v2: string = "v2"
3542-
" def GetName(): string
3543-
" return "baz"
3544-
" enddef
3545-
" endclass
3546-
" END
3547-
" writefile(lines, 'Xdir/autoload/SomeClass/bar/baz.vim', 'D')
3548-
"
3549-
" lines =<< trim END
3550-
" vim9script
3551-
"
3552-
" import autoload './bar/baz.vim'
3553-
"
3554-
" export def MyTestFoo(): string
3555-
" assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
3556-
" assert_fails('var x = baz.Baz.foobar', 'E1337: Class variable "foobar" not found in class "Baz"')
3557-
"
3558-
" const instance = baz.Baz.new()
3559-
" return $'{instance.GetName()} {baz.Baz.v1} {instance.v2}'
3560-
" enddef
3561-
" END
3562-
" writefile(lines, 'Xdir/autoload/SomeClass/foo.vim', 'D')
3563-
"
3564-
" lines =<< trim END
3565-
" vim9script
3566-
"
3567-
" import autoload 'SomeClass/foo.vim'
3568-
" import autoload 'SomeClass/bar/baz.vim'
3569-
"
3570-
" def NotInAutoload()
3571-
" # Use non-existing class method and variable
3572-
" assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
3573-
"
3574-
" var caught_exception = false
3575-
" try
3576-
" var x = baz.Baz.foobar
3577-
" catch /E1337: Class variable "foobar" not found in class "Baz"/
3578-
" caught_exception = true
3579-
" endtry
3580-
" assert_true(caught_exception)
3581-
"
3582-
" const instance = baz.Baz.new()
3583-
" assert_equal("baz v1 v2", $'{instance.GetName()} {baz.Baz.v1} {instance.v2}')
3584-
" enddef
3585-
"
3586-
" def InAutoload()
3587-
" assert_equal("baz v1 v2", foo.MyTestFoo())
3588-
" enddef
3589-
"
3590-
" NotInAutoload()
3591-
" InAutoload()
3592-
" END
3593-
" v9.CheckScriptSuccess(lines)
3594-
"
3595-
" &rtp = save_rtp
3596-
" enddef
3526+
" Test for using an autoloaded class from another autoloaded script
3527+
def Test_class_from_auloaded_script()
3528+
mkdir('Xdir', 'R')
3529+
var save_rtp = &rtp
3530+
&rtp = getcwd()
3531+
exe 'set rtp^=' .. getcwd() .. '/Xdir'
3532+
3533+
mkdir('Xdir/autoload/SomeClass/bar', 'p')
3534+
3535+
var lines =<< trim END
3536+
vim9script
3537+
3538+
export class Baz
3539+
static var v1: string = "v1"
3540+
var v2: string = "v2"
3541+
def GetName(): string
3542+
return "baz"
3543+
enddef
3544+
endclass
3545+
END
3546+
writefile(lines, 'Xdir/autoload/SomeClass/bar/baz.vim', 'D')
3547+
3548+
lines =<< trim END
3549+
vim9script
3550+
3551+
import autoload './bar/baz.vim'
3552+
3553+
export def MyTestFoo(): string
3554+
assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
3555+
assert_fails('var x = baz.Baz.foobar', 'E1337: Class variable "foobar" not found in class "Baz"')
3556+
3557+
const instance = baz.Baz.new()
3558+
return $'{instance.GetName()} {baz.Baz.v1} {instance.v2}'
3559+
enddef
3560+
END
3561+
writefile(lines, 'Xdir/autoload/SomeClass/foo.vim', 'D')
3562+
3563+
lines =<< trim END
3564+
vim9script
3565+
3566+
import autoload 'SomeClass/foo.vim'
3567+
import autoload 'SomeClass/bar/baz.vim'
3568+
3569+
def NotInAutoload()
3570+
# Use non-existing class method and variable
3571+
assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
3572+
3573+
var caught_exception = false
3574+
try
3575+
var x = baz.Baz.foobar
3576+
catch /E1337: Class variable "foobar" not found in class "Baz"/
3577+
caught_exception = true
3578+
endtry
3579+
assert_true(caught_exception)
3580+
3581+
const instance = baz.Baz.new()
3582+
assert_equal("baz v1 v2", $'{instance.GetName()} {baz.Baz.v1} {instance.v2}')
3583+
enddef
3584+
3585+
def InAutoload()
3586+
assert_equal("baz v1 v2", foo.MyTestFoo())
3587+
enddef
3588+
3589+
NotInAutoload()
3590+
InAutoload()
3591+
END
3592+
v9.CheckScriptSuccess(lines)
3593+
3594+
&rtp = save_rtp
3595+
enddef
3596+
3597+
" Test for using an autoloaded enum from another script
3598+
def Test_enum_from_auloaded_script()
3599+
mkdir('Xdir', 'R')
3600+
var save_rtp = &rtp
3601+
&rtp = getcwd()
3602+
exe 'set rtp^=' .. getcwd() .. '/Xdir'
3603+
3604+
mkdir('Xdir/autoload/', 'p')
3605+
3606+
var lines =<< trim END
3607+
vim9script
3608+
export enum Color
3609+
Red,
3610+
Green,
3611+
Blue
3612+
endenum
3613+
END
3614+
writefile(lines, 'Xdir/autoload/color.vim', 'D')
3615+
3616+
lines =<< trim END
3617+
vim9script
3618+
3619+
import autoload 'color.vim'
3620+
3621+
def CheckColor()
3622+
var c = color.Color.Green
3623+
assert_equal('Green', c.name)
3624+
enddef
3625+
CheckColor()
3626+
END
3627+
v9.CheckScriptSuccess(lines)
3628+
3629+
&rtp = save_rtp
3630+
enddef
35973631

35983632
" Test for using a non-exported constant as an instance variable initiazer in an
35993633
" imported class

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1154,
707709
/**/
708710
1153,
709711
/**/

src/vim9class.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ ex_class(exarg_T *eap)
20632063
tv.v_type = VAR_CLASS;
20642064
tv.vval.v_class = cl;
20652065
SOURCING_LNUM = start_lnum;
2066-
int rc = set_var_const(cl->class_name, current_sctx.sc_sid, NULL, &tv, FALSE, 0, 0);
2066+
int rc = set_var_const(cl->class_name, 0, NULL, &tv, FALSE, 0, 0);
20672067
if (rc == FAIL)
20682068
goto cleanup;
20692069

0 commit comments

Comments
 (0)