Skip to content

Commit

Permalink
new function lua_vpushstr' to replace uses of sprintf'
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-ieru committed May 7, 2002
1 parent 71144e3 commit dea6b6d
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 156 deletions.
11 changes: 9 additions & 2 deletions lapi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.188 2002/05/06 15:51:41 roberto Exp roberto $
** $Id: lapi.c,v 1.189 2002/05/06 19:05:10 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -354,6 +354,13 @@ LUA_API void lua_pushstring (lua_State *L, const char *s) {
}


LUA_API void lua_vpushstr (lua_State *L, const char *fmt, va_list argp) {
lua_lock(L);
luaO_vpushstr(L, fmt, argp);
lua_unlock(L);
}


LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
Closure *cl;
lua_lock(L);
Expand Down Expand Up @@ -514,7 +521,7 @@ LUA_API void lua_setmetatable (lua_State *L, int objindex) {
uvalue(obj)->uv.metatable = hvalue(mt);
break;
default:
luaO_verror(L, "cannot change the meta table of a %.20s",
luaO_verror(L, "cannot change the meta table of a %s",
luaT_typenames[ttype(obj)]);
}
lua_unlock(L);
Expand Down
34 changes: 3 additions & 31 deletions lauxlib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.67 2002/05/01 20:40:42 roberto Exp roberto $
** $Id: lauxlib.c,v 1.68 2002/05/06 19:05:10 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -142,46 +142,18 @@ LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
}


static void vstr (lua_State *L, const char *fmt, va_list argp) {
luaL_Buffer b;
luaL_buffinit(L, &b);
for (;;) {
const char *e = strchr(fmt, '%');
if (e == NULL) break;
luaL_addlstring(&b, fmt, e-fmt);
switch (*(e+1)) {
case 's':
luaL_addstring(&b, va_arg(argp, char *));
break;
case 'd':
lua_pushnumber(L, va_arg(argp, int));
luaL_addvalue (&b);
break;
case '%':
luaL_putchar(&b, '%');
break;
default:
lua_error(L, "invalid format option");
}
fmt = e+2;
}
luaL_addstring(&b, fmt);
luaL_pushresult(&b);
}


LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
vstr(L, fmt, argp);
lua_vpushstr(L, fmt, argp);
va_end(argp);
}


LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
vstr(L, fmt, argp);
lua_vpushstr(L, fmt, argp);
va_end(argp);
return lua_errorobj(L);
}
Expand Down
11 changes: 3 additions & 8 deletions lcode.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 1.97 2002/04/24 20:07:46 roberto Exp roberto $
** $Id: lcode.c,v 1.98 2002/05/06 15:51:41 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
Expand All @@ -23,11 +23,6 @@
#define hasjumps(e) ((e)->t != (e)->f)


void luaK_error (LexState *ls, const char *msg) {
luaX_error(ls, msg, ls->t.token);
}


void luaK_nil (FuncState *fs, int from, int n) {
Instruction *previous;
if (fs->pc > fs->lasttarget && /* no jumps to current position? */
Expand Down Expand Up @@ -67,7 +62,7 @@ static void luaK_fixjump (FuncState *fs, int pc, int dest) {
else { /* jump is relative to position following jump instruction */
int offset = dest-(pc+1);
if (abs(offset) > MAXARG_sBx)
luaK_error(fs->ls, "control structure too long");
luaX_syntaxerror(fs->ls, "control structure too long");
SETARG_sBx(*jmp, offset);
}
}
Expand Down Expand Up @@ -182,7 +177,7 @@ static void luaK_checkstack (FuncState *fs, int n) {
int newstack = fs->freereg + n;
if (newstack > fs->f->maxstacksize) {
if (newstack >= MAXSTACK)
luaK_error(fs->ls, "function or expression too complex");
luaX_syntaxerror(fs->ls, "function or expression too complex");
fs->f->maxstacksize = cast(lu_byte, newstack);
}
}
Expand Down
3 changes: 1 addition & 2 deletions lcode.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lcode.h,v 1.31 2002/04/09 18:49:30 roberto Exp roberto $
** $Id: lcode.h,v 1.32 2002/04/24 20:07:46 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -41,7 +41,6 @@ typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;

#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)

void luaK_error (LexState *ls, const char *msg);
int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
void luaK_nil (FuncState *fs, int from, int n);
Expand Down
9 changes: 4 additions & 5 deletions ldblib.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: ldblib.c,v 1.49 2002/05/01 20:40:42 roberto Exp roberto $
** $Id: ldblib.c,v 1.50 2002/05/06 19:05:10 roberto Exp roberto $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -34,17 +34,16 @@ static void settabsi (lua_State *L, const char *i, int v) {
static int getinfo (lua_State *L) {
lua_Debug ar;
const char *options = luaL_opt_string(L, 2, "flnSu");
char buff[20];
if (lua_isnumber(L, 1)) {
if (!lua_getstack(L, (int)(lua_tonumber(L, 1)), &ar)) {
lua_pushnil(L); /* level out of range */
return 1;
}
}
else if (lua_isfunction(L, 1)) {
luaL_vstr(L, ">%s", options);
options = lua_tostring(L, -1);
lua_pushvalue(L, 1);
sprintf(buff, ">%.10s", options);
options = buff;
}
else
return luaL_argerror(L, 1, "function or level expected");
Expand Down Expand Up @@ -170,7 +169,7 @@ static int setlinehook (lua_State *L) {
static int debug (lua_State *L) {
for (;;) {
char buffer[250];
fprintf(stderr, "lua_debug> ");
fputs("lua_debug> ", stderr);
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
strcmp(buffer, "cont\n") == 0)
return 0;
Expand Down
10 changes: 5 additions & 5 deletions ldebug.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 1.110 2002/04/24 20:07:46 roberto Exp roberto $
** $Id: ldebug.c,v 1.111 2002/05/02 13:06:20 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -487,10 +487,10 @@ void luaG_typeerror (lua_State *L, const TObject *o, const char *op) {
if (isinstack(L->ci, o))
kind = getobjname(L, L->ci, o - L->ci->base, &name);
if (kind)
luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
luaO_verror(L, "attempt to %s %s `%s' (a %s value)",
op, kind, name, t);
else
luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
luaO_verror(L, "attempt to %s a %s value", op, t);
}


Expand All @@ -513,8 +513,8 @@ void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
const char *t1 = luaT_typenames[ttype(p1)];
const char *t2 = luaT_typenames[ttype(p2)];
if (t1[2] == t2[2])
luaO_verror(L, "attempt to compare two %.10s values", t1);
luaO_verror(L, "attempt to compare two %s values", t1);
else
luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
luaO_verror(L, "attempt to compare %s with %s", t1, t2);
}

10 changes: 4 additions & 6 deletions ldo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 1.172 2002/04/22 14:40:50 roberto Exp roberto $
** $Id: ldo.c,v 1.173 2002/05/01 20:40:42 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -436,8 +436,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
}
else {
setobj(L->top++, &p.err);
if (status == LUA_ERRRUN) /* an error occurred: correct error code */
status = LUA_ERRSYNTAX;
lua_assert(status != LUA_ERRRUN);
}
lua_unlock(L);
return status;
Expand All @@ -459,9 +458,8 @@ static void message (lua_State *L, const TObject *msg, int nofunc) {
}
else { /* call error function */
setobj(L->top, m);
incr_top(L);
setobj(L->top, msg);
incr_top(L);
setobj(L->top + 1, msg);
L->top += 2;
luaD_call(L, L->top - 2, 1);
setobj(m, L->top - 1);
}
Expand Down
Loading

0 comments on commit dea6b6d

Please sign in to comment.