'luaO_pushvfstring' does not need to reallocate stack

(less error cases in the API)
This commit is contained in:
Roberto Ierusalimschy 2018-04-25 13:26:20 -03:00
parent 26eb144541
commit deb807837c
1 changed files with 12 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.123 2018/01/28 15:13:26 roberto Exp roberto $ ** $Id: lobject.c,v 2.124 2018/02/27 18:47:32 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -393,7 +393,7 @@ void luaO_tostring (lua_State *L, TValue *obj) {
static void pushstr (lua_State *L, const char *str, size_t l) { static void pushstr (lua_State *L, const char *str, size_t l) {
setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
luaD_inctop(L); L->top++;
} }
@ -402,11 +402,10 @@ static void pushstr (lua_State *L, const char *str, size_t l) {
conventional formats, plus Lua-specific '%I' and '%U' conventional formats, plus Lua-specific '%I' and '%U'
*/ */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
int n = 0; int n = 0; /* number of strings in the stack to concatenate */
for (;;) { const char *e; /* points to next conversion specifier */
const char *e = strchr(fmt, '%'); while ((e = strchr(fmt, '%')) != NULL) {
if (e == NULL) break; pushstr(L, fmt, e - fmt); /* string up to conversion specifier */
pushstr(L, fmt, e - fmt);
switch (*(e+1)) { switch (*(e+1)) {
case 's': { /* zero-terminated string */ case 's': { /* zero-terminated string */
const char *s = va_arg(argp, char *); const char *s = va_arg(argp, char *);
@ -433,7 +432,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
case 'f': { /* a 'lua_Number' */ case 'f': { /* a 'lua_Number' */
setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber))); setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber)));
top2str: /* convert the top element to a string */ top2str: /* convert the top element to a string */
luaD_inctop(L); L->top++;
luaO_tostring(L, s2v(L->top - 1)); luaO_tostring(L, s2v(L->top - 1));
break; break;
} }
@ -460,9 +459,12 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
} }
} }
n += 2; n += 2;
fmt = e+2; if (L->top + 2 > L->stack_last) { /* no free stack space? */
luaV_concat(L, n);
n = 1;
}
fmt = e + 2;
} }
luaD_checkstack(L, 1);
pushstr(L, fmt, strlen(fmt)); pushstr(L, fmt, strlen(fmt));
if (n > 0) luaV_concat(L, n + 1); if (n > 0) luaV_concat(L, n + 1);
return svalue(s2v(L->top - 1)); return svalue(s2v(L->top - 1));