*arg = skipwhite(*arg);
/* Handle following '[', '(' and '.' for expr[expr], expr.name,
- * expr(expr). */
+ * expr(expr), expr->name(expr) */
if (ret == OK)
ret = handle_subscript(arg, rettv, evaluate, TRUE);
// Locate the method name.
name = *arg;
- for (len = 0; ASCII_ISALNUM(name[len]) || name[len] == '_'; ++len)
+ for (len = 0; eval_isnamec(name[len]); ++len)
;
if (len == 0)
{
}
*arg += len;
+ // TODO: if "name" is a function reference, resolve it.
+
vim_memset(&funcexe, 0, sizeof(funcexe));
funcexe.evaluate = evaluate;
funcexe.basetv = &base;
endfunc
func Test_user_func()
- let g:FuncRef=function("FuncWithRef")
+ let g:FuncRef = function("FuncWithRef")
let g:counter = 0
inoremap <expr> ( ListItem()
inoremap <expr> [ ListReset()
call assert_equal(9, g:retval)
call assert_equal(333, g:FuncRef(333))
+ let g:retval = "nop"
+ call assert_equal('xxx4asdf', "xxx"->Table(4, "asdf"))
+ call assert_equal('fail', 45->Compute(0, "retval"))
+ call assert_equal('nop', g:retval)
+ call assert_equal('ok', 45->Compute(5, "retval"))
+ call assert_equal(9, g:retval)
+ " call assert_equal(333, 333->g:FuncRef())
+
enew
normal oXX+-XX
\ .. " endfunction",
\ execute('func Args2'))
endfunc
+
+func s:addFoo(lead)
+ return a:lead .. 'foo'
+endfunc
+
+func Test_user_method()
+ eval 'bar'->s:addFoo()->assert_equal('barfoo')
+endfunc
int argcount = argcount_in;
typval_T *argvars = argvars_in;
dict_T *selfdict = funcexe->selfdict;
- typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
+ typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
+ // "funcexe->basetv" is not NULL
int argv_clear = 0;
partial_T *partial = funcexe->partial;
/*
* User defined function.
*/
- if (funcexe->basetv != NULL)
- // TODO: support User function: base->Method()
- fp = NULL;
- else if (partial != NULL && partial->pt_func != NULL)
+ if (partial != NULL && partial->pt_func != NULL)
fp = partial->pt_func;
else
fp = find_func(rfname);
argcount = funcexe->argv_func(argcount, argvars,
fp->uf_args.ga_len);
+ if (funcexe->basetv != NULL)
+ {
+ // Method call: base->Method()
+ mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
+ argv[0] = *funcexe->basetv;
+ argcount++;
+ }
+ else
+ memcpy(argv, argvars, sizeof(typval_T) * argcount);
+
if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
*funcexe->doesrange = TRUE;
if (argcount < fp->uf_args.ga_len - fp->uf_def_args.ga_len)
did_save_redo = TRUE;
}
++fp->uf_calls;
- call_user_func(fp, argcount, argvars, rettv,
+ call_user_func(fp, argcount, argv, rettv,
funcexe->firstline, funcexe->lastline,
(fp->uf_flags & FC_DICT) ? selfdict : NULL);
if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
else if (funcexe->basetv != NULL)
{
/*
- * Find the method name in the table, call its implementation.
+ * expr->method(): Find the method name in the table, call its
+ * implementation with the base as one of the arguments.
*/
error = call_internal_method(fname, argcount, argvars, rettv,
funcexe->basetv);