Patch 8.2.4072
Problem:    Vim9: compiling function fails when autoload script is not loaded
            yet.
Solution:   Depend on runtime loading.
Files:      src/vim9expr.c, src/vim9script.c, src/vim9instr.c,
            src/vim9execute.c, src/testdir/test_vim9_import.vim


*** ../vim-8.2.4071/src/vim9expr.c      2022-01-09 13:36:20.351866599 +0000
--- src/vim9expr.c      2022-01-12 19:41:59.720402918 +0000
***************
*** 274,280 ****
--- 274,283 ----
        int     cc;
        ufunc_T *ufunc;
        type_T  *type;
+       int     done = FALSE;
+       int     res = OK;
  
        // TODO: if this is an autoload import do something else.
        // Need to lookup the member.
        if (*p != '.')
        {
***************
*** 295,305 ****
        cc = *p;
        *p = NUL;
  
!       idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
                                                                   cctx, TRUE);
        *p = cc;
        p = skipwhite(p);
        *end = p;
  
        if (idx < 0)
        {
--- 298,328 ----
        cc = *p;
        *p = NUL;
  
!       si = SCRIPT_ITEM(import->imp_sid);
!       if (si->sn_autoload_prefix != NULL
!                                       && si->sn_state == SN_STATE_NOT_LOADED)
!       {
!           char_u  *auto_name = concat_str(si->sn_autoload_prefix, exp_name);
! 
!           // autoload script must be loaded later, access by the autoload
!           // name.
!           if (cc == '(')
!               res = generate_PUSHFUNC(cctx, auto_name, &t_func_any);
!           else
!               res = generate_LOAD(cctx, ISN_LOADG, 0, auto_name, &t_any);
!           vim_free(auto_name);
!           done = TRUE;
!       }
!       else
!       {
!           idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
                                                                   cctx, TRUE);
+       }
        *p = cc;
        p = skipwhite(p);
        *end = p;
+       if (done)
+           return res;
  
        if (idx < 0)
        {
*** ../vim-8.2.4071/src/vim9script.c    2022-01-12 11:46:36.314615880 +0000
--- src/vim9script.c    2022-01-12 18:56:46.303771217 +0000
***************
*** 488,494 ****
        // we need a scriptitem without loading the script
        sid = find_script_in_rtp(from_name);
        vim_free(from_name);
!       res = SCRIPT_ID_VALID(sid) ? OK : FAIL;
      }
      else
      {
--- 488,503 ----
        // we need a scriptitem without loading the script
        sid = find_script_in_rtp(from_name);
        vim_free(from_name);
!       if (SCRIPT_ID_VALID(sid))
!       {
!           scriptitem_T    *si = SCRIPT_ITEM(sid);
! 
!           if (si->sn_autoload_prefix == NULL)
!               si->sn_autoload_prefix = get_autoload_prefix(si);
!           res = OK;
!       }
!       else
!           res = FAIL;
      }
      else
      {
*** ../vim-8.2.4071/src/vim9instr.c     2022-01-08 18:43:36.877446896 +0000
--- src/vim9instr.c     2022-01-12 19:26:57.314895149 +0000
***************
*** 714,720 ****
  
  /*
   * Generate an ISN_PUSHFUNC instruction with name "name".
-  * Consumes "name".
   */
      int
  generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
--- 714,719 ----
***************
*** 727,733 ****
        return FAIL;
      if (name == NULL)
        funcname = NULL;
!     else if (*name == K_SPECIAL)  // script-local
        funcname = vim_strsave(name);
      else
      {
--- 726,733 ----
        return FAIL;
      if (name == NULL)
        funcname = NULL;
!     else if (*name == K_SPECIAL                                   // 
script-local
!           || vim_strchr(name, AUTOLOAD_CHAR) != NULL)     // autoload
        funcname = vim_strsave(name);
      else
      {
*** ../vim-8.2.4071/src/vim9execute.c   2022-01-08 16:19:18.513639814 +0000
--- src/vim9execute.c   2022-01-12 19:10:36.345548845 +0000
***************
*** 2227,2232 ****
--- 2227,2242 ----
                    }
                    di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
  
+                   if (di == NULL && ht == get_globvar_ht())
+                   {
+                       // may need to load autoload script
+                       if (script_autoload(iptr->isn_arg.string, FALSE))
+                           di = find_var_in_ht(ht, 0,
+                                                  iptr->isn_arg.string, TRUE);
+                       if (did_emsg)
+                           goto on_error;
+                   }
+ 
                    if (di == NULL)
                    {
                        SOURCING_LNUM = iptr->isn_lnum;
*** ../vim-8.2.4071/src/testdir/test_vim9_import.vim    2022-01-12 
15:15:22.871212756 +0000
--- src/testdir/test_vim9_import.vim    2022-01-12 17:35:45.518595651 +0000
***************
*** 1218,1223 ****
--- 1218,1259 ----
    &rtp = save_rtp
  enddef
  
+ def Test_import_autoload_postponed()
+   mkdir('Xdir/autoload', 'p')
+   var save_rtp = &rtp
+   exe 'set rtp^=' .. getcwd() .. '/Xdir'
+ 
+   var lines =<< trim END
+       vim9script autoload
+ 
+       g:loaded_postponed = 'true'
+       export var variable = 'bla'
+       export def Function(): string
+         return 'bla'
+       enddef
+   END
+   writefile(lines, 'Xdir/autoload/postponed.vim')
+ 
+   lines =<< trim END
+       vim9script
+ 
+       import autoload 'postponed.vim'
+       def Tryit()
+         echo postponed.variable
+         echo postponed.Function()
+       enddef
+       defcompile
+   END
+   CheckScriptSuccess(lines)
+   assert_false(exists('g:loaded_postponed'))
+   CheckScriptSuccess(lines + ['Tryit()'])
+   assert_equal('true', g:loaded_postponed)
+ 
+   unlet g:loaded_postponed
+   delete('Xdir', 'rf')
+   &rtp = save_rtp
+ enddef
+ 
  def Test_autoload_mapping()
    mkdir('Xdir/autoload', 'p')
    var save_rtp = &rtp
*** ../vim-8.2.4071/src/version.c       2022-01-12 16:18:13.801613093 +0000
--- src/version.c       2022-01-12 17:11:58.215471409 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4072,
  /**/

-- 
A successful man is one who makes more money than his wife can spend.
A successful woman is one who can find such a man.

 /// 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/20220112195434.DBD6F1C065D%40moolenaar.net.

Raspunde prin e-mail lui