Patch 8.2.3557
Problem:    Vim9: cannot call imported funcref at script level.
Solution:   Check for an imported function. (closes #9007)
Files:      src/userfunc.c, src/testdir/test_vim9_script.vim


*** ../vim-8.2.3556/src/userfunc.c      2021-09-18 14:25:49.272973758 +0100
--- src/userfunc.c      2021-10-22 22:17:08.513844876 +0100
***************
*** 1548,1556 ****
--- 1548,1558 ----
        int         no_autoload)
  {
      dictitem_T        *v;
+     typval_T  *tv = NULL;
      int               cc;
      char_u    *s = NULL;
      hashtab_T *ht;
+     int               did_type = FALSE;
  
      if (partialp != NULL)
        *partialp = NULL;
***************
*** 1562,1581 ****
      name[*lenp] = cc;
      if (v != NULL)
      {
!       if (v->di_tv.v_type == VAR_FUNC)
        {
!           if (v->di_tv.vval.v_string == NULL)
            {
                *lenp = 0;
                return (char_u *)"";    // just in case
            }
!           s = v->di_tv.vval.v_string;
            *lenp = (int)STRLEN(s);
        }
  
!       if (v->di_tv.v_type == VAR_PARTIAL)
        {
!           partial_T *pt = v->di_tv.vval.v_partial;
  
            if (pt == NULL)
            {
--- 1564,1622 ----
      name[*lenp] = cc;
      if (v != NULL)
      {
!       tv = &v->di_tv;
!     }
!     else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
!     {
!       imported_T  *import;
!       char_u      *p = name;
!       int         len = *lenp;
! 
!       if (STRNCMP(name, "s:", 2) == 0)
!       {
!           p = name + 2;
!           len -= 2;
!       }
!       import = find_imported(p, len, NULL);
! 
!       // imported variable from another script
!       if (import != NULL)
!       {
!           if (import->imp_funcname != NULL)
!           {
!               s = import->imp_funcname;
!               *lenp = (int)STRLEN(s);
!               return s;
!           }
!           // TODO: what if (import->imp_flags & IMP_FLAGS_STAR)
!           {
!               scriptitem_T    *si = SCRIPT_ITEM(import->imp_sid);
!               svar_T          *sv = ((svar_T *)si->sn_var_vals.ga_data)
!                                                   + import->imp_var_vals_idx;
!               tv = sv->sv_tv;
!               if (type != NULL)
!                   *type = sv->sv_type;
!               did_type = TRUE;
!           }
!       }
!     }
! 
!     if (tv != NULL)
!     {
!       if (tv->v_type == VAR_FUNC)
        {
!           if (tv->vval.v_string == NULL)
            {
                *lenp = 0;
                return (char_u *)"";    // just in case
            }
!           s = tv->vval.v_string;
            *lenp = (int)STRLEN(s);
        }
  
!       if (tv->v_type == VAR_PARTIAL)
        {
!           partial_T *pt = tv->vval.v_partial;
  
            if (pt == NULL)
            {
***************
*** 1590,1598 ****
  
        if (s != NULL)
        {
!           if (type != NULL && ht == get_script_local_ht())
            {
!               svar_T  *sv = find_typval_in_script(&v->di_tv);
  
                if (sv != NULL)
                    *type = sv->sv_type;
--- 1631,1639 ----
  
        if (s != NULL)
        {
!           if (!did_type && type != NULL && ht == get_script_local_ht())
            {
!               svar_T  *sv = find_typval_in_script(tv);
  
                if (sv != NULL)
                    *type = sv->sv_type;
*** ../vim-8.2.3556/src/testdir/test_vim9_script.vim    2021-09-26 
20:14:35.023216490 +0100
--- src/testdir/test_vim9_script.vim    2021-10-22 22:15:22.924449096 +0100
***************
*** 1477,1482 ****
--- 1477,1509 ----
    delete('Xvim9_script')
  enddef
  
+ def Test_import_funcref()
+   var lines =<< trim END
+       vim9script
+       export def F(): number
+         return 42
+       enddef
+       export const G = F
+   END
+   writefile(lines, 'Xlib.vim')
+ 
+   lines =<< trim END
+       vim9script
+       import {G} from './Xlib.vim'
+       const Foo = G()
+       assert_equal(42, Foo)
+ 
+       def DoTest()
+         const Goo = G()
+         assert_equal(42, Foo)
+       enddef
+       DoTest()
+   END
+   CheckScriptSuccess(lines)
+ 
+   delete('Xlib.vim')
+ enddef
+ 
  def Test_import_star_fails()
    writefile([], 'Xfoo.vim')
    var lines =<< trim END
*** ../vim-8.2.3556/src/version.c       2021-10-22 20:56:35.211118936 +0100
--- src/version.c       2021-10-22 22:10:21.104472631 +0100
***************
*** 759,760 ****
--- 759,762 ----
  {   /* Add new patch number below this line */
+ /**/
+     3557,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
226. You sit down at the computer right after dinner and your spouse
     says "See you in the morning."

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20211022211824.67159C80053%40moolenaar.net.

Raspunde prin e-mail lui