enddef
+def FuncWithForwardCall(): string
+ return DefinedLater("yes")
+enddef
+
+def DefinedLater(arg: string): string
+ return arg
+enddef
+
+def Test_disassemble_update_instr()
+ let res = execute('disass FuncWithForwardCall')
+ assert_match('FuncWithForwardCall.*'
+ \ .. 'return DefinedLater("yes").*'
+ \ .. '\d PUSHS "yes".*'
+ \ .. '\d UCALL DefinedLater(argc 1).*'
+ \ .. '\d CHECKTYPE string stack\[-1].*'
+ \ .. '\d RETURN.*'
+ \, res)
+
+ " Calling the function will change UCALL into the faster DCALL
+ assert_equal('yes', FuncWithForwardCall())
+
+ res = execute('disass FuncWithForwardCall')
+ assert_match('FuncWithForwardCall.*'
+ \ .. 'return DefinedLater("yes").*'
+ \ .. '\d PUSHS "yes".*'
+ \ .. '\d DCALL DefinedLater(argc 1).*'
+ \ .. '\d CHECKTYPE string stack\[-1].*'
+ \ .. '\d RETURN.*'
+ \, res)
+enddef
+
+
def FuncWithDefault(arg: string = 'default'): string
return arg
enddef
/*
* Execute a user defined function.
+ * "iptr" can be used to replace the instruction with a more efficient one.
*/
static int
-call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx)
+call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
{
typval_T argvars[MAX_FUNC_ARGS];
funcexe_T funcexe;
int idx;
if (ufunc->uf_dfunc_idx >= 0)
- // The function has been compiled, can call it quickly.
+ {
+ // The function has been compiled, can call it quickly. For a function
+ // that was defined later: we can call it directly next time.
+ if (iptr != NULL)
+ {
+ iptr->isn_type = ISN_DCALL;
+ iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
+ iptr->isn_arg.dfunc.cdf_argcount = argcount;
+ }
return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
+ }
if (call_prepare(argcount, argvars, ectx) == FAIL)
return FAIL;
/*
* Execute a function by "name".
* This can be a builtin function or a user function.
+ * "iptr" can be used to replace the instruction with a more efficient one.
* Returns FAIL if not found without an error message.
*/
static int
-call_by_name(char_u *name, int argcount, ectx_T *ectx)
+call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
{
ufunc_T *ufunc;
ufunc = find_func(name, NULL);
if (ufunc != NULL)
- return call_ufunc(ufunc, argcount, ectx);
+ return call_ufunc(ufunc, argcount, ectx, iptr);
return FAIL;
}
partial_T *pt = tv->vval.v_partial;
if (pt->pt_func != NULL)
- return call_ufunc(pt->pt_func, argcount, ectx);
+ return call_ufunc(pt->pt_func, argcount, ectx, NULL);
name = pt->pt_name;
}
else
name = tv->vval.v_string;
- if (call_by_name(name, argcount, ectx) == FAIL)
+ if (call_by_name(name, argcount, ectx, NULL) == FAIL)
{
if (called_emsg == called_emsg_before)
semsg(_(e_unknownfunc), name);
/*
* Execute a function by "name".
* This can be a builtin function, user function or a funcref.
+ * "iptr" can be used to replace the instruction with a more efficient one.
*/
static int
-call_eval_func(char_u *name, int argcount, ectx_T *ectx)
+call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
{
int called_emsg_before = called_emsg;
- if (call_by_name(name, argcount, ectx) == FAIL
+ if (call_by_name(name, argcount, ectx, iptr) == FAIL
&& called_emsg == called_emsg_before)
{
// "name" may be a variable that is a funcref or partial
SOURCING_LNUM = iptr->isn_lnum;
if (call_eval_func(cufunc->cuf_name,
- cufunc->cuf_argcount, &ectx) == FAIL)
+ cufunc->cuf_argcount, &ectx, iptr) == FAIL)
goto failed;
}
break;
tv = STACK_TV_BOT(-1);
if (check_not_string(tv) == FAIL)
- {
- --ectx.ec_stack.ga_len;
goto failed;
- }
(void)tv_get_number_chk(tv, &error);
if (error)
goto failed;
ret = OK;
failed:
+ // When failed need to unwind the call stack.
+ while (ectx.ec_frame != initial_frame_ptr)
+ func_return(&ectx);
+
for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
clear_tv(STACK_TV(idx));
vim_free(ectx.ec_stack.ga_data);