1
0
forked from aniani/vim

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>
This commit is contained in:
Yegappan Lakshmanan
2025-02-27 19:12:00 +01:00
committed by Christian Brabandt
parent 3d75ec7401
commit e9ae35f265
4 changed files with 111 additions and 72 deletions

View File

@@ -3087,6 +3087,9 @@ eval_variable(
dictitem_T *v = find_var_in_ht(ht, 0, name, dictitem_T *v = find_var_in_ht(ht, 0, name,
flags & EVAL_VAR_NOAUTOLOAD); flags & EVAL_VAR_NOAUTOLOAD);
if (v == NULL)
v = find_var_autoload_prefix(name, sid, NULL, NULL);
if (v != NULL) if (v != NULL)
{ {
tv = &v->di_tv; tv = &v->di_tv;

View File

@@ -3523,77 +3523,111 @@ def Test_use_imported_class_as_type()
source Xdir/import/a.vim source Xdir/import/a.vim
enddef enddef
" FIXME: The following test currently fails. " Test for using an autoloaded class from another autoloaded script
" " Test for using an autoloaded class from another autoloaded script def Test_class_from_auloaded_script()
" def Test_class_from_auloaded_script() mkdir('Xdir', 'R')
" mkdir('Xdir', 'R') var save_rtp = &rtp
" var save_rtp = &rtp &rtp = getcwd()
" &rtp = getcwd() exe 'set rtp^=' .. getcwd() .. '/Xdir'
" exe 'set rtp^=' .. getcwd() .. '/Xdir'
" mkdir('Xdir/autoload/SomeClass/bar', 'p')
" mkdir('Xdir/autoload/SomeClass/bar', 'p')
" var lines =<< trim END
" var lines =<< trim END vim9script
" vim9script
" export class Baz
" export class Baz static var v1: string = "v1"
" static var v1: string = "v1" var v2: string = "v2"
" var v2: string = "v2" def GetName(): string
" def GetName(): string return "baz"
" return "baz" enddef
" enddef endclass
" endclass END
" END writefile(lines, 'Xdir/autoload/SomeClass/bar/baz.vim', 'D')
" writefile(lines, 'Xdir/autoload/SomeClass/bar/baz.vim', 'D')
" lines =<< trim END
" lines =<< trim END vim9script
" vim9script
" import autoload './bar/baz.vim'
" import autoload './bar/baz.vim'
" export def MyTestFoo(): string
" export def MyTestFoo(): string assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
" assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"') assert_fails('var x = baz.Baz.foobar', 'E1337: Class variable "foobar" not found in class "Baz"')
" assert_fails('var x = baz.Baz.foobar', 'E1337: Class variable "foobar" not found in class "Baz"')
" const instance = baz.Baz.new()
" const instance = baz.Baz.new() return $'{instance.GetName()} {baz.Baz.v1} {instance.v2}'
" return $'{instance.GetName()} {baz.Baz.v1} {instance.v2}' enddef
" enddef END
" END writefile(lines, 'Xdir/autoload/SomeClass/foo.vim', 'D')
" writefile(lines, 'Xdir/autoload/SomeClass/foo.vim', 'D')
" lines =<< trim END
" lines =<< trim END vim9script
" vim9script
" import autoload 'SomeClass/foo.vim'
" import autoload 'SomeClass/foo.vim' import autoload 'SomeClass/bar/baz.vim'
" import autoload 'SomeClass/bar/baz.vim'
" def NotInAutoload()
" def NotInAutoload() # Use non-existing class method and variable
" # Use non-existing class method and variable assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
" assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
" var caught_exception = false
" var caught_exception = false try
" try var x = baz.Baz.foobar
" var x = baz.Baz.foobar catch /E1337: Class variable "foobar" not found in class "Baz"/
" catch /E1337: Class variable "foobar" not found in class "Baz"/ caught_exception = true
" caught_exception = true endtry
" endtry assert_true(caught_exception)
" assert_true(caught_exception)
" const instance = baz.Baz.new()
" const instance = baz.Baz.new() assert_equal("baz v1 v2", $'{instance.GetName()} {baz.Baz.v1} {instance.v2}')
" assert_equal("baz v1 v2", $'{instance.GetName()} {baz.Baz.v1} {instance.v2}') enddef
" enddef
" def InAutoload()
" def InAutoload() assert_equal("baz v1 v2", foo.MyTestFoo())
" assert_equal("baz v1 v2", foo.MyTestFoo()) enddef
" enddef
" NotInAutoload()
" NotInAutoload() InAutoload()
" InAutoload() END
" END v9.CheckScriptSuccess(lines)
" v9.CheckScriptSuccess(lines)
" &rtp = save_rtp
" &rtp = save_rtp enddef
" enddef
" Test for using an autoloaded enum from another script
def Test_enum_from_auloaded_script()
mkdir('Xdir', 'R')
var save_rtp = &rtp
&rtp = getcwd()
exe 'set rtp^=' .. getcwd() .. '/Xdir'
mkdir('Xdir/autoload/', 'p')
var lines =<< trim END
vim9script
export enum Color
Red,
Green,
Blue
endenum
END
writefile(lines, 'Xdir/autoload/color.vim', 'D')
lines =<< trim END
vim9script
import autoload 'color.vim'
def CheckColor()
var c = color.Color.Green
assert_equal('Green', c.name)
enddef
CheckColor()
END
v9.CheckScriptSuccess(lines)
&rtp = save_rtp
enddef
" Test for using a non-exported constant as an instance variable initiazer in an " Test for using a non-exported constant as an instance variable initiazer in an
" imported class " imported class

View File

@@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1154,
/**/ /**/
1153, 1153,
/**/ /**/

View File

@@ -2063,7 +2063,7 @@ early_ret:
tv.v_type = VAR_CLASS; tv.v_type = VAR_CLASS;
tv.vval.v_class = cl; tv.vval.v_class = cl;
SOURCING_LNUM = start_lnum; SOURCING_LNUM = start_lnum;
int rc = set_var_const(cl->class_name, current_sctx.sc_sid, NULL, &tv, FALSE, 0, 0); int rc = set_var_const(cl->class_name, 0, NULL, &tv, FALSE, 0, 0);
if (rc == FAIL) if (rc == FAIL)
goto cleanup; goto cleanup;