]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1154: Vim9: not able to use autoload class accross scripts v9.1.1154
authorYegappan Lakshmanan <yegappan@yahoo.com>
Thu, 27 Feb 2025 18:12:00 +0000 (19:12 +0100)
committerChristian Brabandt <cb@256bit.org>
Thu, 27 Feb 2025 18:12:00 +0000 (19:12 +0100)
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>
src/evalvars.c
src/testdir/test_vim9_import.vim
src/version.c
src/vim9class.c

index 1b11f2847d17a6fc0bbc74196a008a46e0c1d36b..2745ac271a6a733ae8ce7356e53d68066eb191de 100644 (file)
@@ -3087,6 +3087,9 @@ eval_variable(
                        dictitem_T *v = find_var_in_ht(ht, 0, name,
                                                  flags & EVAL_VAR_NOAUTOLOAD);
 
+                       if (v == NULL)
+                           v = find_var_autoload_prefix(name, sid, NULL, NULL);
+
                        if (v != NULL)
                        {
                            tv = &v->di_tv;
index aa81851b47ba70669bfa9fe2a6e7ba9a47fde6ba..8376a9316e1f06247459b0a7f62e189c9b479fa6 100644 (file)
@@ -3523,77 +3523,111 @@ def Test_use_imported_class_as_type()
   source Xdir/import/a.vim
 enddef
 
-" FIXME: The following test currently fails.
-" " Test for using an autoloaded class from another autoloaded script
-" def Test_class_from_auloaded_script()
-"   mkdir('Xdir', 'R')
-"   var save_rtp = &rtp
-"   &rtp = getcwd()
-"   exe 'set rtp^=' .. getcwd() .. '/Xdir'
-"
-"   mkdir('Xdir/autoload/SomeClass/bar', 'p')
-"
-"   var lines =<< trim END
-"     vim9script
-"
-"     export class Baz
-"       static var v1: string = "v1"
-"       var v2: string = "v2"
-"       def GetName(): string
-"         return "baz"
-"       enddef
-"     endclass
-"   END
-"   writefile(lines, 'Xdir/autoload/SomeClass/bar/baz.vim', 'D')
-"
-"   lines =<< trim END
-"     vim9script
-"
-"     import autoload './bar/baz.vim'
-"
-"     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.foobar', 'E1337: Class variable "foobar" not found in class "Baz"')
-"
-"       const instance = baz.Baz.new()
-"       return $'{instance.GetName()} {baz.Baz.v1} {instance.v2}'
-"     enddef
-"   END
-"   writefile(lines, 'Xdir/autoload/SomeClass/foo.vim', 'D')
-"
-"   lines =<< trim END
-"     vim9script
-"
-"     import autoload 'SomeClass/foo.vim'
-"     import autoload 'SomeClass/bar/baz.vim'
-"
-"     def NotInAutoload()
-"       # Use non-existing class method and variable
-"       assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
-"
-"       var caught_exception = false
-"       try
-"         var x = baz.Baz.foobar
-"       catch /E1337: Class variable "foobar" not found in class "Baz"/
-"         caught_exception = true
-"       endtry
-"       assert_true(caught_exception)
-"
-"       const instance = baz.Baz.new()
-"       assert_equal("baz v1 v2", $'{instance.GetName()} {baz.Baz.v1} {instance.v2}')
-"     enddef
-"
-"     def InAutoload()
-"       assert_equal("baz v1 v2", foo.MyTestFoo())
-"     enddef
-"
-"     NotInAutoload()
-"     InAutoload()
-"   END
-"   v9.CheckScriptSuccess(lines)
-"
-"   &rtp = save_rtp
-" enddef
+" Test for using an autoloaded class from another autoloaded script
+def Test_class_from_auloaded_script()
+  mkdir('Xdir', 'R')
+  var save_rtp = &rtp
+  &rtp = getcwd()
+  exe 'set rtp^=' .. getcwd() .. '/Xdir'
+
+  mkdir('Xdir/autoload/SomeClass/bar', 'p')
+
+  var lines =<< trim END
+    vim9script
+
+    export class Baz
+      static var v1: string = "v1"
+      var v2: string = "v2"
+      def GetName(): string
+        return "baz"
+      enddef
+    endclass
+  END
+  writefile(lines, 'Xdir/autoload/SomeClass/bar/baz.vim', 'D')
+
+  lines =<< trim END
+    vim9script
+
+    import autoload './bar/baz.vim'
+
+    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.foobar', 'E1337: Class variable "foobar" not found in class "Baz"')
+
+      const instance = baz.Baz.new()
+      return $'{instance.GetName()} {baz.Baz.v1} {instance.v2}'
+    enddef
+  END
+  writefile(lines, 'Xdir/autoload/SomeClass/foo.vim', 'D')
+
+  lines =<< trim END
+    vim9script
+
+    import autoload 'SomeClass/foo.vim'
+    import autoload 'SomeClass/bar/baz.vim'
+
+    def NotInAutoload()
+      # Use non-existing class method and variable
+      assert_fails('var x = baz.Baz.NonExisting()', 'E1325: Method "NonExisting" not found in class "Baz"')
+
+      var caught_exception = false
+      try
+        var x = baz.Baz.foobar
+      catch /E1337: Class variable "foobar" not found in class "Baz"/
+        caught_exception = true
+      endtry
+      assert_true(caught_exception)
+
+      const instance = baz.Baz.new()
+      assert_equal("baz v1 v2", $'{instance.GetName()} {baz.Baz.v1} {instance.v2}')
+    enddef
+
+    def InAutoload()
+      assert_equal("baz v1 v2", foo.MyTestFoo())
+    enddef
+
+    NotInAutoload()
+    InAutoload()
+  END
+  v9.CheckScriptSuccess(lines)
+
+  &rtp = save_rtp
+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
 " imported class
index 2d2188a87d9fa652a41d03ff5fd816c5a529f8fa..6e668fc6a566437aa91135bed52f0663a371e3e1 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1154,
 /**/
     1153,
 /**/
index cbb91f4fe0b53dca10c093f65d17e7a6b17ce6ca..1c54474e45638ea0967be2a57f56bd6918133ca1 100644 (file)
@@ -2063,7 +2063,7 @@ early_ret:
     tv.v_type = VAR_CLASS;
     tv.vval.v_class = cl;
     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)
        goto cleanup;