aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval/userfunc.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2025-03-04 07:37:14 +0800
committerGitHub <noreply@github.com>2025-03-03 23:37:14 +0000
commit47cfe901d739149f88b6f917fc1f310727700b40 (patch)
treee667b0c753bc286b746ada0d8a5cddde459d106c /src/nvim/eval/userfunc.c
parent8ce504820af04194a41acbe1f4c61cf12bd5feb5 (diff)
downloadrneovim-47cfe901d739149f88b6f917fc1f310727700b40.tar.gz
rneovim-47cfe901d739149f88b6f917fc1f310727700b40.tar.bz2
rneovim-47cfe901d739149f88b6f917fc1f310727700b40.zip
vim-patch:9.1.1169: using global variable for get_insert()/get_lambda_name() (#32713)
Problem: using global variable for get_insert()/get_lambda_name() (after v9.1.1151) Solution: let the functions return a string_T object instead (Yee Cheng Chin) In vim/vim#16720, `get_insert()` was modified to store a string length in a global variable to be queried immediately by another `get_insert_len()` function, which is somewhat fragile. Instead, just have the function itself return a `string_T` object instead. Also do the same for `get_lambda_name()` which has similar issues. closes: vim/vim#16775 https://github.com/vim/vim/commit/0b5fe420715786249cd121d845dbd6a81f962d1b Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
Diffstat (limited to 'src/nvim/eval/userfunc.c')
-rw-r--r--src/nvim/eval/userfunc.c31
1 files changed, 8 insertions, 23 deletions
diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c
index f386dd28b9..c5357d507c 100644
--- a/src/nvim/eval/userfunc.c
+++ b/src/nvim/eval/userfunc.c
@@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>
+#include "nvim/api/private/defs.h"
#include "nvim/ascii_defs.h"
#include "nvim/autocmd.h"
#include "nvim/autocmd_defs.h"
@@ -265,29 +266,16 @@ static void register_closure(ufunc_T *fp)
}
static char lambda_name[8 + NUMBUFLEN];
-static size_t lambda_namelen = 0;
/// @return a name for a lambda. Returned in static memory.
-char *get_lambda_name(void)
+static String get_lambda_name(void)
{
static int lambda_no = 0;
int n = snprintf(lambda_name, sizeof(lambda_name), "<lambda>%d", ++lambda_no);
- if (n < 1) {
- lambda_namelen = 0;
- } else if (n >= (int)sizeof(lambda_name)) {
- lambda_namelen = sizeof(lambda_name) - 1;
- } else {
- lambda_namelen = (size_t)n;
- }
- return lambda_name;
-}
-
-/// Get the length of the last lambda name.
-size_t get_lambda_name_len(void)
-{
- return lambda_namelen;
+ return cbuf_as_string(lambda_name,
+ n < 1 ? 0 : (size_t)MIN(n, (int)sizeof(lambda_name) - 1));
}
/// Allocate a "ufunc_T" for a function called "name".
@@ -371,10 +359,8 @@ int get_lambda_tv(char **arg, typval_T *rettv, evalarg_T *evalarg)
int flags = 0;
garray_T newlines;
- char *name = get_lambda_name();
- size_t namelen = get_lambda_name_len();
-
- fp = alloc_ufunc(name, namelen);
+ String name = get_lambda_name();
+ fp = alloc_ufunc(name.data, name.size);
pt = xcalloc(1, sizeof(partial_T));
ga_init(&newlines, (int)sizeof(char *), 1);
@@ -4142,9 +4128,8 @@ bool set_ref_in_func(char *name, ufunc_T *fp_in, int copyID)
/// Registers a luaref as a lambda.
char *register_luafunc(LuaRef ref)
{
- char *name = get_lambda_name();
- size_t namelen = get_lambda_name_len();
- ufunc_T *fp = alloc_ufunc(name, namelen);
+ String name = get_lambda_name();
+ ufunc_T *fp = alloc_ufunc(name.data, name.size);
fp->uf_refcount = 1;
fp->uf_varargs = true;