]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0263: Vim9: Problem with lambda blocks in enums and classes v9.1.0263
authorYegappan Lakshmanan <yegappan@yahoo.com>
Thu, 4 Apr 2024 19:42:07 +0000 (21:42 +0200)
committerChristian Brabandt <cb@256bit.org>
Thu, 4 Apr 2024 19:42:07 +0000 (21:42 +0200)
Problem:  Vim9: Problem with lambda blocks in enums and classes
          (Aliaksei Budavei)
Solution: Support evaluating lambda blocks from a string, skip over
          comments (Yegappan Lakshmanan)

fixes: #14350
closes: #14405

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/eval.c
src/testdir/test_vim9_class.vim
src/testdir/test_vim9_enum.vim
src/testdir/test_vim9_import.vim
src/testdir/test_vim9_script.vim
src/userfunc.c
src/version.c

index a4fde0ef292e67cabe7e3368d9f9333392580c9c..187b4d4d87e50e7c146f2e51704b082d9b3868de 100644 (file)
@@ -549,6 +549,7 @@ skip_expr_concatenate(
                                    ((char_u **)gap->ga_data)[gap->ga_len - 1];
                ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
                ga_clear_strings(gap);
+               ga_clear(freegap);
            }
            else
            {
@@ -1203,7 +1204,7 @@ get_lval_imported(
 
     dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
     if (di == NULL)
-       // variable is not found
+       // script is autoloaded.  So variable will be found later
        goto success;
 
     *dip = di;
index 6eafe00373647ca8f50ca1d529a381692d6710c9..77a2102c2d69138d7047b2756c2b22dfbc51f160 100644 (file)
@@ -10553,4 +10553,34 @@ def Test_protected_funcref()
   v9.CheckScriptFailure(lines, 'E1333: Cannot access protected variable "_Id" in class "Test2"', 5)
 enddef
 
+" Test for using lambda block in classes
+def Test_lambda_block_in_class()
+  # This used to crash Vim
+  var lines =<< trim END
+    vim9script
+    class IdClass1
+      const Id: func(number): number = (num: number): number => {
+        # Return a ID
+        return num * 10
+      }
+    endclass
+    var id = IdClass1.new()
+    assert_equal(20, id.Id(2))
+  END
+  v9.CheckScriptSuccess(lines)
+
+  # This used to crash Vim
+  lines =<< trim END
+    vim9script
+    class IdClass2
+      static const Id: func(number): number = (num: number): number => {
+        # Return a ID
+        return num * 2
+      }
+    endclass
+    assert_equal(16, IdClass2.Id(8))
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 5f9fbff6d33603e9559d79b43bd3b642a201e1df..274b556b7769aafa40a3b2faff353f879f48be08 100644 (file)
@@ -1503,4 +1503,35 @@ def Test_use_enum_values_in_class_variable()
   v9.CheckSourceSuccess(lines)
 enddef
 
+" Test for using lambda block in enums
+def Test_lambda_block_in_enum()
+  # This used to crash Vim
+  var lines =<< trim END
+    vim9script
+    enum IdEnum1
+      ID1
+      const Id: func(number): number = (num: number): number => {
+        # Return a ID
+        return num / 2
+      }
+    endenum
+    assert_equal(5, IdEnum1.ID1.Id(10))
+  END
+  v9.CheckScriptSuccess(lines)
+
+  # This used to crash Vim
+  lines =<< trim END
+    vim9script
+    enum IdEnum2
+      ID1
+      static const Id: func(number): number = (num: number): number => {
+        # Return a ID
+        return num + 2
+      }
+    endenum
+    assert_equal(12, IdEnum2.Id(10))
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 8d68f1e47ed9aecd59c7665db5badb51055d6c07..67b67595f5143c8cbb97fdbeddfa7d4ceff1d595 100644 (file)
@@ -2993,4 +2993,30 @@ def Test_import_autloaded_script()
   &rtp = save_rtp
 enddef
 
+" Test for autoloading an imported dict func
+def Test_autoload_import_dict_func()
+  mkdir('Xdir/autoload', 'pR')
+  var lines =<< trim END
+    vim9script
+    export var al_exported_nr: number = 33
+    def Al_AddNum(n: number)
+      al_exported_nr += n
+    enddef
+    export var al_exportedDict: dict<func> = {Fn: Al_AddNum}
+  END
+  writefile(lines, 'Xdir/autoload/Xdictfunc.vim')
+
+  var save_rtp = &rtp
+  exe 'set rtp^=' .. getcwd() .. '/Xdir'
+  lines =<< trim END
+    import './Xdir/autoload/Xdictfunc.vim'
+    call Xdictfunc#al_exportedDict.Fn(5)
+    call assert_equal(38, Xdictfunc#al_exported_nr)
+    call call(Xdictfunc#al_exportedDict.Fn, [3])
+    call assert_equal(41, Xdictfunc#al_exported_nr)
+  END
+  v9.CheckScriptSuccess(lines)
+  &rtp = save_rtp
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 0e2133492768bfed7ef4f9f8b1cc3ef6d23e4e65..cd693e5993e23bee9bd2607d046c245cda323603 100644 (file)
@@ -4931,6 +4931,16 @@ def Test_for_empty_line_after_lambda()
   v9.CheckSourceSuccess(lines)
 enddef
 
+" Test for evaluating a lambda block from a string
+def Test_eval_lambda_block()
+  var lines =<< trim END
+    vim9script
+    var Fn = eval("(x: number): number => {\nreturn x * 2\n}")
+    assert_equal(6, Fn(3))
+  END
+  v9.CheckSourceSuccess(lines)
+enddef
+
 " Keep this last, it messes up highlighting.
 def Test_substitute_cmd()
   new
index 9a307966b722e6cacbf2f8ebab7937cdf4b1c9f7..b023c3a966af6dde708db5297825fb5c38d494a2 100644 (file)
@@ -1335,6 +1335,7 @@ lambda_function_body(
     char_u     *name;
     int                lnum_save = -1;
     linenr_T   sourcing_lnum_top = SOURCING_LNUM;
+    char_u     *line_arg = NULL;
 
     *arg = skipwhite(*arg + 1);
     if (**arg == '|' || !ends_excmd2(start, *arg))
@@ -1343,6 +1344,12 @@ lambda_function_body(
        return FAIL;
     }
 
+    // When there is a line break use what follows for the lambda body.
+    // Makes lambda body initializers work for object and enum member
+    // variables.
+    if (**arg == '\n')
+       line_arg = *arg + 1;
+
     CLEAR_FIELD(eap);
     eap.cmdidx = CMD_block;
     eap.forceit = FALSE;
@@ -1357,7 +1364,7 @@ lambda_function_body(
     }
 
     ga_init2(&newlines, sizeof(char_u *), 10);
-    if (get_function_body(&eap, &newlines, NULL,
+    if (get_function_body(&eap, &newlines, line_arg,
                                             &evalarg->eval_tofree_ga) == FAIL)
        goto erret;
 
@@ -1372,7 +1379,12 @@ lambda_function_body(
 
        for (idx = 0; idx < newlines.ga_len; ++idx)
        {
-           char_u  *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
+           char_u  *p = ((char_u **)newlines.ga_data)[idx];
+           if (p == NULL)
+               // comment line in the lambda body
+               continue;
+
+           p = skipwhite(p);
 
            if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
                goto erret;
index d31484a4e9ecb8d1b74ab9705447f0ba5a292c32..1a82d9b3ab8829a83ffebc74cc986ba5e298c235 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    263,
 /**/
     262,
 /**/