Hi Bram,

On Thu, Dec 9, 2021 at 6:24 AM Bram Moolenaar <[email protected]> wrote:
>
>
> Patch 8.2.3765
> Problem:    Vim9: cannot use a lambda for 'opfunc' and others.
> Solution:   Convert the lambda to a string.
>
> ***************
> *** 2349,2359 ****
>                         if (s == NULL)
>                             s = (char_u *)"";
>                     }
>                     else
>                         // must be VAR_NUMBER, CHECKTYPE makes sure
>                         n = tv->vval.v_number;
> !                   msg = set_option_value(iptr->isn_arg.storeopt.so_name,
> !                                       n, s, 
> iptr->isn_arg.storeopt.so_flags);
>                     clear_tv(tv);
>                     if (msg != NULL)
>                     {
> --- 2353,2374 ----
>                         if (s == NULL)
>                             s = (char_u *)"";
>                     }
> +                   else if (iptr->isn_type == ISN_STOREFUNCOPT)
> +                   {
> +                       SOURCING_LNUM = iptr->isn_lnum;
> +                       cb = get_callback(tv);
> +                       if (cb.cb_name == NULL || *cb.cb_name == NUL)
> +                       {
> +                           clear_tv(tv);
> +                           free_callback(&cb);
> +                           goto on_error;
> +                       }
> +                       s = cb.cb_name;

Instead of the above, I think we should use tv2string() here to convert the
lambda/funcref to a string. The diff to do this is below.

diff --git a/src/vim9execute.c b/src/vim9execute.c
index 874e57c58..fa31d9180 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -1646,6 +1646,7 @@ exec_instructions(ectx_T *ectx)
     int                ret = FAIL;
     int                save_trylevel_at_start = ectx->ec_trylevel_at_start;
     int                dict_stack_len_at_start = dict_stack.ga_len;
+    char_u     *tofree = NULL;

     // Start execution at the first instruction.
     ectx->ec_iidx = 0;
@@ -2344,6 +2345,7 @@ exec_instructions(ectx_T *ectx)
                    char_u      *s = NULL;
                    char        *msg;
                    callback_T  cb = {NULL, NULL, 0};
+                   char_u      numbuf[NUMBUFLEN];

                    --ectx->ec_stack.ga_len;
                    tv = STACK_TV_BOT(0);
@@ -2356,14 +2358,11 @@ exec_instructions(ectx_T *ectx)
                    else if (iptr->isn_type == ISN_STOREFUNCOPT)
                    {
                        SOURCING_LNUM = iptr->isn_lnum;
-                       cb = get_callback(tv);
-                       if (cb.cb_name == NULL || *cb.cb_name == NUL)
-                       {
-                           clear_tv(tv);
-                           free_callback(&cb);
-                           goto on_error;
-                       }
-                       s = cb.cb_name;
+                       // If the option can be set to a function reference or
+                       // a lambda and the passed value is a function
+                       // reference, then convert it to the name (string) of
+                       // the function reference.
+                       s = tv2string(tv, &tofree, numbuf, 0);
                    }
                    else
                        // must be VAR_NUMBER, CHECKTYPE makes sure
@@ -4642,6 +4641,7 @@ on_fatal_error:
 done:
     ret = OK;
 theend:
+    vim_free(tofree);
     dict_stack_clear(dict_stack_len_at_start);
     ectx->ec_trylevel_at_start = save_trylevel_at_start;
     return ret;


Regards,
Yegappan

-- 
-- 
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/CAAW7x7%3DRS-cHRpSXP9Rw7EtypZNzV3yaX2yn4LLdTRaizOT9Bw%40mail.gmail.com.

Raspunde prin e-mail lui