patch 9.1.0548: it's not possible to get a unique id for some vars

Problem:  it's not possible to get a unique id for some vars
Solution: Add the id() Vim script function, which returns a unique
          identifier for object, dict, list, job, blob or channel
          variables (Ernie Rael)

fixes: #14374
closes: #15145

Signed-off-by: Ernie Rael <errael@raelity.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Ernie Rael
2024-07-09 18:39:52 +02:00
committed by Christian Brabandt
parent 73a810817b
commit c8e158be0e
7 changed files with 104 additions and 1 deletions

View File

@ -82,6 +82,7 @@ static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
static void f_hlID(typval_T *argvars, typval_T *rettv);
static void f_hlexists(typval_T *argvars, typval_T *rettv);
static void f_hostname(typval_T *argvars, typval_T *rettv);
static void f_id(typval_T *argvars, typval_T *rettv);
static void f_index(typval_T *argvars, typval_T *rettv);
static void f_indexof(typval_T *argvars, typval_T *rettv);
static void f_input(typval_T *argvars, typval_T *rettv);
@ -2207,6 +2208,8 @@ static funcentry_T global_functions[] =
ret_string, f_hostname},
{"iconv", 3, 3, FEARG_1, arg3_string,
ret_string, f_iconv},
{"id", 1, 1, FEARG_1, NULL,
ret_string, f_id},
{"indent", 1, 1, FEARG_1, arg1_lnum,
ret_number, f_indent},
{"index", 2, 4, FEARG_1, arg24_index,
@ -7516,6 +7519,40 @@ f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
rettv->vval.v_string = vim_strsave(hostname);
}
/*
* "id()" function
* Identity. Return address of item as a hex string, %p format.
* Currently only valid for object/container types.
* Return empty string if not an object.
*/
void
f_id(typval_T *argvars, typval_T *rettv)
{
char_u numbuf[NUMBUFLEN];
switch (argvars[0].v_type)
{
case VAR_LIST:
case VAR_DICT:
case VAR_OBJECT:
case VAR_JOB:
case VAR_CHANNEL:
case VAR_BLOB:
// Assume pointer value in typval_T vval union at common location.
if (argvars[0].vval.v_object != NULL)
vim_snprintf((char*)numbuf, sizeof(numbuf), "%p",
(void *)argvars[0].vval.v_object);
else
numbuf[0] = NUL;
break;
default:
numbuf[0] = NUL;
}
rettv->v_type = VAR_STRING;
rettv->vval.v_string = vim_strsave(numbuf);
}
/*
* "index()" function
*/