mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
vim-patch:8.1.1820: using expr->FuncRef() does not work
Problem: Using expr->FuncRef() does not work.
Solution: Make FuncRef work as a method.
761fdf01c6
This commit is contained in:
parent
8d1ca37d1f
commit
d41b87e070
148
src/nvim/eval.c
148
src/nvim/eval.c
@ -3146,6 +3146,65 @@ static int pattern_match(char_u *pat, char_u *text, bool ic)
|
|||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handle a name followed by "(". Both for just "name(arg)" and for
|
||||||
|
/// "expr->name(arg)".
|
||||||
|
//
|
||||||
|
/// @param arg Points to "(", will be advanced
|
||||||
|
/// @param basetv "expr" for "expr->name(arg)"
|
||||||
|
//
|
||||||
|
/// @return OK or FAIL.
|
||||||
|
static int eval_func(char_u **const arg, char_u *const name, const int name_len,
|
||||||
|
typval_T *const rettv, const bool evaluate,
|
||||||
|
typval_T *const basetv)
|
||||||
|
FUNC_ATTR_NONNULL_ARG(1, 2, 4)
|
||||||
|
{
|
||||||
|
char_u *s = name;
|
||||||
|
int len = name_len;
|
||||||
|
|
||||||
|
if (!evaluate) {
|
||||||
|
check_vars((const char *)s, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If "s" is the name of a variable of type VAR_FUNC
|
||||||
|
// use its contents.
|
||||||
|
partial_T *partial;
|
||||||
|
s = deref_func_name((const char *)s, &len, &partial, !evaluate);
|
||||||
|
|
||||||
|
// Need to make a copy, in case evaluating the arguments makes
|
||||||
|
// the name invalid.
|
||||||
|
s = xmemdupz(s, len);
|
||||||
|
|
||||||
|
// Invoke the function.
|
||||||
|
funcexe_T funcexe = FUNCEXE_INIT;
|
||||||
|
funcexe.firstline = curwin->w_cursor.lnum;
|
||||||
|
funcexe.lastline = curwin->w_cursor.lnum;
|
||||||
|
funcexe.evaluate = evaluate;
|
||||||
|
funcexe.partial = partial;
|
||||||
|
funcexe.basetv = basetv;
|
||||||
|
int ret = get_func_tv(s, len, rettv, arg, &funcexe);
|
||||||
|
|
||||||
|
xfree(s);
|
||||||
|
|
||||||
|
// If evaluate is false rettv->v_type was not set in
|
||||||
|
// get_func_tv, but it's needed in handle_subscript() to parse
|
||||||
|
// what follows. So set it here.
|
||||||
|
if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(') {
|
||||||
|
rettv->vval.v_string = (char_u *)tv_empty_string;
|
||||||
|
rettv->v_type = VAR_FUNC;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop the expression evaluation when immediately
|
||||||
|
// aborting on error, or when an interrupt occurred or
|
||||||
|
// an exception was thrown but not caught.
|
||||||
|
if (evaluate && aborting()) {
|
||||||
|
if (ret == OK) {
|
||||||
|
tv_clear(rettv);
|
||||||
|
}
|
||||||
|
ret = FAIL;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(ZyX-I): move to eval/expressions
|
// TODO(ZyX-I): move to eval/expressions
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -3977,47 +4036,7 @@ static int eval7(
|
|||||||
ret = FAIL;
|
ret = FAIL;
|
||||||
} else {
|
} else {
|
||||||
if (**arg == '(') { // recursive!
|
if (**arg == '(') { // recursive!
|
||||||
partial_T *partial;
|
ret = eval_func(arg, s, len, rettv, evaluate, NULL);
|
||||||
|
|
||||||
if (!evaluate) {
|
|
||||||
check_vars((const char *)s, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If "s" is the name of a variable of type VAR_FUNC
|
|
||||||
// use its contents.
|
|
||||||
s = deref_func_name((const char *)s, &len, &partial, !evaluate);
|
|
||||||
|
|
||||||
// Need to make a copy, in case evaluating the arguments makes
|
|
||||||
// the name invalid.
|
|
||||||
s = xmemdupz(s, len);
|
|
||||||
|
|
||||||
// Invoke the function.
|
|
||||||
funcexe_T funcexe = FUNCEXE_INIT;
|
|
||||||
funcexe.firstline = curwin->w_cursor.lnum;
|
|
||||||
funcexe.lastline = curwin->w_cursor.lnum;
|
|
||||||
funcexe.evaluate = evaluate;
|
|
||||||
funcexe.partial = partial;
|
|
||||||
ret = get_func_tv(s, len, rettv, arg, &funcexe);
|
|
||||||
|
|
||||||
xfree(s);
|
|
||||||
|
|
||||||
// If evaluate is false rettv->v_type was not set in
|
|
||||||
// get_func_tv, but it's needed in handle_subscript() to parse
|
|
||||||
// what follows. So set it here.
|
|
||||||
if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(') {
|
|
||||||
rettv->vval.v_string = (char_u *)tv_empty_string;
|
|
||||||
rettv->v_type = VAR_FUNC;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop the expression evaluation when immediately
|
|
||||||
// aborting on error, or when an interrupt occurred or
|
|
||||||
// an exception was thrown but not caught.
|
|
||||||
if (evaluate && aborting()) {
|
|
||||||
if (ret == OK) {
|
|
||||||
tv_clear(rettv);
|
|
||||||
}
|
|
||||||
ret = FAIL;
|
|
||||||
}
|
|
||||||
} else if (evaluate) {
|
} else if (evaluate) {
|
||||||
ret = get_var_tv((const char *)s, len, rettv, NULL, true, false);
|
ret = get_var_tv((const char *)s, len, rettv, NULL, true, false);
|
||||||
} else {
|
} else {
|
||||||
@ -4090,37 +4109,35 @@ static int eval_method(char_u **const arg, typval_T *const rettv,
|
|||||||
{
|
{
|
||||||
// Skip over the ->.
|
// Skip over the ->.
|
||||||
*arg += 2;
|
*arg += 2;
|
||||||
|
typval_T base = *rettv;
|
||||||
|
rettv->v_type = VAR_UNKNOWN;
|
||||||
|
|
||||||
// Locate the method name.
|
// Locate the method name.
|
||||||
const char_u *const name = *arg;
|
char_u *name = *arg;
|
||||||
size_t len;
|
char_u *alias;
|
||||||
for (len = 0; eval_isnamec(name[len]); len++) {
|
|
||||||
|
const int len
|
||||||
|
= get_name_len((const char **)arg, (char **)&alias, evaluate, true);
|
||||||
|
if (alias != NULL) {
|
||||||
|
name = alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len == 0) {
|
int ret;
|
||||||
|
if (len <= 0) {
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
EMSG(_("E260: Missing name after ->"));
|
EMSG(_("E260: Missing name after ->"));
|
||||||
}
|
}
|
||||||
return FAIL;
|
ret = FAIL;
|
||||||
}
|
} else {
|
||||||
|
if (**arg != '(') {
|
||||||
// Check for the "(". Skip over white space after it.
|
|
||||||
if (name[len] != '(') {
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
EMSG2(_(e_missingparen), name);
|
EMSG2(_(e_missingparen), name);
|
||||||
}
|
}
|
||||||
return FAIL;
|
ret = FAIL;
|
||||||
|
} else {
|
||||||
|
ret = eval_func(arg, name, len, rettv, evaluate, &base);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*arg += len;
|
|
||||||
|
|
||||||
// TODO(seandewar): if "name" is a function reference, resolve it.
|
|
||||||
|
|
||||||
typval_T base = *rettv;
|
|
||||||
funcexe_T funcexe = FUNCEXE_INIT;
|
|
||||||
funcexe.evaluate = evaluate;
|
|
||||||
funcexe.basetv = &base;
|
|
||||||
rettv->v_type = VAR_UNKNOWN;
|
|
||||||
int ret = get_func_tv(name, len, rettv, arg, &funcexe);
|
|
||||||
|
|
||||||
// Clear the funcref afterwards, so that deleting it while
|
// Clear the funcref afterwards, so that deleting it while
|
||||||
// evaluating the arguments is possible (see test55).
|
// evaluating the arguments is possible (see test55).
|
||||||
@ -4128,15 +4145,6 @@ static int eval_method(char_u **const arg, typval_T *const rettv,
|
|||||||
tv_clear(&base);
|
tv_clear(&base);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop the expression evaluation when immediately aborting on
|
|
||||||
// error, or when an interrupt occurred or an exception was thrown
|
|
||||||
// but not caught.
|
|
||||||
if (aborting()) {
|
|
||||||
if (ret == OK) {
|
|
||||||
tv_clear(rettv);
|
|
||||||
}
|
|
||||||
ret = FAIL;
|
|
||||||
}
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1450,6 +1450,7 @@ call_func(
|
|||||||
typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
|
typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
|
||||||
// "funcexe->basetv" is not NULL
|
// "funcexe->basetv" is not NULL
|
||||||
int argv_clear = 0;
|
int argv_clear = 0;
|
||||||
|
int argv_base = 0;
|
||||||
partial_T *partial = funcexe->partial;
|
partial_T *partial = funcexe->partial;
|
||||||
|
|
||||||
// Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
|
// Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
|
||||||
@ -1550,8 +1551,8 @@ call_func(
|
|||||||
memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
|
memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
|
||||||
argv[0] = *funcexe->basetv;
|
argv[0] = *funcexe->basetv;
|
||||||
argcount++;
|
argcount++;
|
||||||
} else {
|
argvars = argv;
|
||||||
memcpy(argv, argvars, sizeof(typval_T) * argcount);
|
argv_base = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL) {
|
if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL) {
|
||||||
@ -1565,7 +1566,7 @@ call_func(
|
|||||||
error = ERROR_DICT;
|
error = ERROR_DICT;
|
||||||
} else {
|
} else {
|
||||||
// Call the user function.
|
// Call the user function.
|
||||||
call_user_func(fp, argcount, argv, rettv, funcexe->firstline,
|
call_user_func(fp, argcount, argvars, rettv, funcexe->firstline,
|
||||||
funcexe->lastline,
|
funcexe->lastline,
|
||||||
(fp->uf_flags & FC_DICT) ? selfdict : NULL);
|
(fp->uf_flags & FC_DICT) ? selfdict : NULL);
|
||||||
error = ERROR_NONE;
|
error = ERROR_NONE;
|
||||||
@ -1602,9 +1603,11 @@ theend:
|
|||||||
user_func_error(error, (name != NULL) ? name : funcname);
|
user_func_error(error, (name != NULL) ? name : funcname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clear the copies made from the partial
|
||||||
while (argv_clear > 0) {
|
while (argv_clear > 0) {
|
||||||
tv_clear(&argv[--argv_clear]);
|
tv_clear(&argv[--argv_clear + argv_base]);
|
||||||
}
|
}
|
||||||
|
|
||||||
xfree(tofree);
|
xfree(tofree);
|
||||||
xfree(name);
|
xfree(name);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
" Tests for ->method()
|
" Tests for ->method()
|
||||||
|
|
||||||
func Test_list()
|
func Test_list_method()
|
||||||
let l = [1, 2, 3]
|
let l = [1, 2, 3]
|
||||||
call assert_equal([1, 2, 3, 4], [1, 2, 3]->add(4))
|
call assert_equal([1, 2, 3, 4], [1, 2, 3]->add(4))
|
||||||
eval l->assert_equal(l)
|
eval l->assert_equal(l)
|
||||||
@ -34,7 +34,7 @@ func Test_list()
|
|||||||
call assert_fails('eval l->values()', 'E715:')
|
call assert_fails('eval l->values()', 'E715:')
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
func Test_dict()
|
func Test_dict_method()
|
||||||
let d = #{one: 1, two: 2, three: 3}
|
let d = #{one: 1, two: 2, three: 3}
|
||||||
|
|
||||||
call assert_equal(d, d->copy())
|
call assert_equal(d, d->copy())
|
||||||
@ -71,7 +71,7 @@ func Test_dict()
|
|||||||
call assert_equal([1, 2, 3], d->values())
|
call assert_equal([1, 2, 3], d->values())
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
func Test_string()
|
func Test_string_method()
|
||||||
call assert_equal(['1', '2', '3'], '1 2 3'->split())
|
call assert_equal(['1', '2', '3'], '1 2 3'->split())
|
||||||
call assert_equal([1, 2, 3], '1 2 3'->split()->map({i, v -> str2nr(v)}))
|
call assert_equal([1, 2, 3], '1 2 3'->split()->map({i, v -> str2nr(v)}))
|
||||||
call assert_equal([65, 66, 67], 'ABC'->str2list())
|
call assert_equal([65, 66, 67], 'ABC'->str2list())
|
||||||
@ -81,7 +81,7 @@ func Test_string()
|
|||||||
call assert_equal('axc', 'abc'->substitute('b', 'x', ''))
|
call assert_equal('axc', 'abc'->substitute('b', 'x', ''))
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
func Test_append()
|
func Test_method_append()
|
||||||
new
|
new
|
||||||
eval ['one', 'two', 'three']->append(1)
|
eval ['one', 'two', 'three']->append(1)
|
||||||
call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
|
call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
|
||||||
@ -95,4 +95,17 @@ func Test_append()
|
|||||||
exe 'bwipe! ' .. bnr
|
exe 'bwipe! ' .. bnr
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_method_funcref()
|
||||||
|
func Concat(one, two, three)
|
||||||
|
return a:one .. a:two .. a:three
|
||||||
|
endfunc
|
||||||
|
let FuncRef = function('Concat')
|
||||||
|
eval 'foo'->FuncRef('bar', 'tail')->assert_equal('foobartail')
|
||||||
|
|
||||||
|
let Partial = function('Concat', ['two'])
|
||||||
|
eval 'one'->Partial('three')->assert_equal('onetwothree')
|
||||||
|
|
||||||
|
delfunc Concat
|
||||||
|
endfunc
|
||||||
|
|
||||||
" vim: shiftwidth=2 sts=2 expandtab
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
Loading…
Reference in New Issue
Block a user