vim-patch:8.2.5167: get(Fn, 'name') on funcref returns special byte code (#21112)

Problem:    get(Fn, 'name') on funcref returns special byte code.
Solution:   Use the printable name.

1ae8c262df

Cherry-pick printable_func_name() from patch 8.2.0149.

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq
2022-11-19 12:47:44 +08:00
committed by GitHub
parent f2b30b4d62
commit 92e51d7e4b
3 changed files with 22 additions and 4 deletions

View File

@@ -2528,13 +2528,17 @@ static void f_get(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
const char *const what = tv_get_string(&argvars[1]);
if (strcmp(what, "func") == 0 || strcmp(what, "name") == 0) {
const char *name = (const char *)partial_name(pt);
rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
const char *const n = (const char *)partial_name(pt);
assert(n != NULL);
rettv->vval.v_string = xstrdup(n);
assert(name != NULL);
if (rettv->v_type == VAR_FUNC) {
func_ref((char_u *)rettv->vval.v_string);
func_ref((char_u *)name);
}
if (*what == 'n' && pt->pt_name == NULL && pt->pt_func != NULL) {
// use <SNR> instead of the byte code
name = (const char *)printable_func_name(pt->pt_func);
}
rettv->vval.v_string = xstrdup(name);
} else if (strcmp(what, "dict") == 0) {
what_is_dict = true;
if (pt->pt_dict != NULL) {

View File

@@ -1652,6 +1652,11 @@ theend:
return ret;
}
char_u *printable_func_name(ufunc_T *fp)
{
return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
}
/// List the head of the function: "name(arg1, arg2)".
///
/// @param[in] fp Function pointer.

View File

@@ -133,11 +133,20 @@ func Test_get_lambda()
call assert_equal([], get(l:L, 'args'))
endfunc
func s:FooBar()
endfunc
" get({func}, {what} [, {default}])
func Test_get_func()
let l:F = function('tr')
call assert_equal('tr', get(l:F, 'name'))
call assert_equal(l:F, get(l:F, 'func'))
let Fb_func = function('s:FooBar')
call assert_match('<SNR>\d\+_FooBar', get(Fb_func, 'name'))
let Fb_ref = funcref('s:FooBar')
call assert_match('<SNR>\d\+_FooBar', get(Fb_ref, 'name'))
call assert_equal({'func has': 'no dict'}, get(l:F, 'dict', {'func has': 'no dict'}))
call assert_equal(0, get(l:F, 'dict'))
call assert_equal([], get(l:F, 'args'))