eval: improve ex_execute (#12445)

This commit is contained in:
erw7 2020-07-20 18:01:14 +09:00 committed by GitHub
parent 398201cfab
commit 950ca6abcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 36 deletions

View File

@ -8539,27 +8539,6 @@ void set_selfdict(typval_T *const rettv, dict_T *const selfdict)
make_partial(selfdict, rettv);
}
// Turn a typeval into a string. Similar to tv_get_string_buf() but uses
// string() on Dict, List, etc.
static const char *tv_stringify(typval_T *varp, char *buf)
FUNC_ATTR_NONNULL_ALL
{
if (varp->v_type == VAR_LIST
|| varp->v_type == VAR_DICT
|| varp->v_type == VAR_FUNC
|| varp->v_type == VAR_PARTIAL
|| varp->v_type == VAR_FLOAT) {
typval_T tmp;
f_string(varp, &tmp, NULL);
const char *const res = tv_get_string_buf(&tmp, buf);
tv_clear(varp);
*varp = tmp;
return res;
}
return tv_get_string_buf(varp, buf);
}
// Find variable "name" in the list of variables.
// Return a pointer to it if found, NULL if not found.
// Careful: "a:0" variables don't have a name.
@ -9406,16 +9385,20 @@ void ex_execute(exarg_T *eap)
}
if (!eap->skip) {
char buf[NUMBUFLEN];
const char *const argstr = eap->cmdidx == CMD_execute
? tv_get_string_buf(&rettv, buf)
: tv_stringify(&rettv, buf);
? tv_get_string(&rettv)
: rettv.v_type == VAR_STRING
? encode_tv2echo(&rettv, NULL)
: encode_tv2string(&rettv, NULL);
const size_t len = strlen(argstr);
ga_grow(&ga, len + 2);
if (!GA_EMPTY(&ga)) {
((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
}
memcpy((char_u *)(ga.ga_data) + ga.ga_len, argstr, len + 1);
if (eap->cmdidx != CMD_execute) {
xfree((void *)argstr);
}
ga.ga_len += len;
}

View File

@ -71,18 +71,18 @@ describe(':echo :echon :echomsg :echoerr', function()
eq('v:true', funcs.String(true))
eq('v:false', funcs.String(false))
eq('v:null', funcs.String(NIL))
eq('true', eval('StringMsg(v:true)'))
eq('false', eval('StringMsg(v:false)'))
eq('null', eval('StringMsg(v:null)'))
eq('true', funcs.StringMsg(true))
eq('false', funcs.StringMsg(false))
eq('null', funcs.StringMsg(NIL))
eq('true', eval('StringErr(v:true)'))
eq('false', eval('StringErr(v:false)'))
eq('null', eval('StringErr(v:null)'))
eq('true', funcs.StringErr(true))
eq('false', funcs.StringErr(false))
eq('null', funcs.StringErr(NIL))
eq('v:true', eval('StringMsg(v:true)'))
eq('v:false', eval('StringMsg(v:false)'))
eq('v:null', eval('StringMsg(v:null)'))
eq('v:true', funcs.StringMsg(true))
eq('v:false', funcs.StringMsg(false))
eq('v:null', funcs.StringMsg(NIL))
eq('v:true', eval('StringErr(v:true)'))
eq('v:false', eval('StringErr(v:false)'))
eq('v:null', eval('StringErr(v:null)'))
eq('v:true', funcs.StringErr(true))
eq('v:false', funcs.StringErr(false))
eq('v:null', funcs.StringErr(NIL))
end)
it('dumps values with at most six digits after the decimal point',