]> granicus.if.org Git - vim/commitdiff
patch 8.2.2682: Vim9: cannot find Name.Func from "import * as Name" v8.2.2682
authorBram Moolenaar <Bram@vim.org>
Thu, 1 Apr 2021 10:57:57 +0000 (12:57 +0200)
committerBram Moolenaar <Bram@vim.org>
Thu, 1 Apr 2021 10:57:57 +0000 (12:57 +0200)
Problem:    Vim9: cannot find Name.Func from "import * as Name". (Alexander
            Goussas)
Solution:   When no variable found try finding a function. (closes #8045)
            Check that the function was exported.

src/testdir/test_vim9_script.vim
src/version.c
src/vim9compile.c
src/vim9script.c

index cd8ac091d6d83b6dc6364b5d07ce8514e1f04479..0d49a42d9fd6f609c16799ffbeb826b13ffa05a9 100644 (file)
@@ -1623,6 +1623,10 @@ def Test_vim9script_funcref()
       export def FastSort(): list<number>
         return range(5)->sort(Compare)
       enddef
+
+      export def GetString(arg: string): string
+        return arg
+      enddef
   END
   writefile(sortlines, 'Xsort.vim')
 
@@ -1633,6 +1637,19 @@ def Test_vim9script_funcref()
       g:result = FastSort()
     enddef
     Test()
+
+    # using a function imported with "as"
+    import * as anAlias from './Xsort.vim'
+    assert_equal('yes', anAlias.GetString('yes'))
+
+    # using the function from a compiled function
+    def TestMore(): string
+      return anAlias.GetString('text')
+    enddef
+    assert_equal('text', TestMore())
+
+    # error when using a function that isn't exported
+    assert_fails('anAlias.Compare(1, 2)', 'E1049:')
   END
   writefile(lines, 'Xscript.vim')
 
index 104073d316cc65725e6848d36fb84c2ac6fff701..54370d3eb22d2491ca73968879489ece7901055f 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2682,
 /**/
     2681,
 /**/
index 72387610dfa620cf8cefb85fd40d57cd005a7dca..fa73de23da04f413245f3f2eec1eaeed47e5d566 100644 (file)
@@ -2720,11 +2720,17 @@ compile_load_scriptvar(
                                                                   cctx, TRUE);
            *p = cc;
            p = skipwhite(p);
+           *end = p;
 
-           // TODO: what if it is a function?
            if (idx < 0)
+           {
+               if (*p == '(' && ufunc != NULL)
+               {
+                   generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
+                   return OK;
+               }
                return FAIL;
-           *end = p;
+           }
 
            generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
                    import->imp_sid,
index 9839a06cfb9293e27df6c3e29166bf3f5eee55fe..a72ef55095b822a0057f754beda9c1aa14f9bcaf 100644 (file)
@@ -298,8 +298,7 @@ find_exported(
     svar_T     *sv;
     scriptitem_T *script = SCRIPT_ITEM(sid);
 
-    // find name in "script"
-    // TODO: also find script-local user function
+    // Find name in "script".
     idx = get_script_item_idx(sid, name, 0, cctx);
     if (idx >= 0)
     {
@@ -341,6 +340,13 @@ find_exported(
                semsg(_(e_item_not_found_in_script_str), name);
            return -1;
        }
+       else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
+       {
+           if (verbose)
+               semsg(_(e_item_not_exported_in_script_str), name);
+           *ufunc = NULL;
+           return -1;
+       }
     }
 
     return idx;