From eea734aa881002e90bd9130171a2b94cd9dc3267 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Mar 1997 14:02:29 -0300 Subject: new module 'auxlib' centralizes functions to get/check parameters. --- auxlib.c | 47 ++++++++++++++++++++++++++++++++ inout.c | 61 +++++++++++------------------------------ iolib.c | 24 ++++++++--------- lua.h | 13 ++++++++- lualib.h | 7 +---- makefile | 8 +++--- mathlib.c | 50 +++++++++++++++++----------------- strlib.c | 93 ++++++++++++++++++++------------------------------------------- 8 files changed, 146 insertions(+), 157 deletions(-) create mode 100644 auxlib.c diff --git a/auxlib.c b/auxlib.c new file mode 100644 index 00000000..0bee6430 --- /dev/null +++ b/auxlib.c @@ -0,0 +1,47 @@ +char *rcs_auxlib="$Id: $"; + +#include + +#include "lua.h" + + +void luaL_arg_check(int cond, char *funcname, int numarg, char *extramsg) +{ + if (!cond) { + char buff[100]; + if (extramsg == NULL) + sprintf(buff, "bad argument #%d to function `%s'", numarg, funcname); + else + sprintf(buff, "bad argument #%d to function `%s' (%s)", + numarg, funcname, extramsg); + lua_error(buff); + } +} + +char *luaL_check_string (int numArg, char *funcname) +{ + lua_Object o = lua_getparam(numArg); + luaL_arg_check(lua_isstring(o), funcname, numArg, "string expected"); + return lua_getstring(o); +} + +char *luaL_opt_string (int numArg, char *def, char *funcname) +{ + return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : + luaL_check_string(numArg, funcname); +} + +double luaL_check_number (int numArg, char *funcname) +{ + lua_Object o = lua_getparam(numArg); + luaL_arg_check(lua_isnumber(o), funcname, numArg, "number expected"); + return lua_getnumber(o); +} + + +double luaL_opt_number (int numArg, double def, char *funcname) +{ + return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : + luaL_check_number(numArg, funcname); +} + diff --git a/inout.c b/inout.c index cfe0e78e..b0b3d2c3 100644 --- a/inout.c +++ b/inout.c @@ -5,7 +5,7 @@ ** Also provides some predefined lua functions. */ -char *rcs_inout="$Id: inout.c,v 2.44 1997/02/26 17:38:41 roberto Unstable roberto $"; +char *rcs_inout="$Id: inout.c,v 2.45 1997/03/11 18:44:28 roberto Exp roberto $"; #include #include @@ -101,32 +101,6 @@ void lua_closestring (void) } -static void check_arg (int cond, char *func) -{ - if (!cond) - { - char buff[100]; - sprintf(buff, "incorrect argument to function `%s'", func); - lua_error(buff); - } -} - -static char *check_string (int numArg, char *funcname) -{ - lua_Object o = lua_getparam(numArg); - check_arg(lua_isstring(o), funcname); - return lua_getstring(o); -} - -static int check_number (int numArg, char *funcname) -{ - lua_Object o = lua_getparam(numArg); - check_arg(lua_isnumber(o), funcname); - return (int)lua_getnumber(o); -} - - - static int passresults (void) { int arg = 0; @@ -141,7 +115,7 @@ static int passresults (void) */ static void lua_internaldostring (void) { - if (lua_dostring(check_string(1, "dostring")) == 0) + if (lua_dostring(luaL_check_string(1, "dostring")) == 0) if (passresults() == 0) lua_pushuserdata(NULL); /* at least one result to signal no errors */ } @@ -151,13 +125,7 @@ static void lua_internaldostring (void) */ static void lua_internaldofile (void) { - lua_Object obj = lua_getparam (1); - char *fname = NULL; - if (lua_isstring(obj)) - fname = lua_getstring(obj); - else if (obj != LUA_NOOBJECT) - lua_error("invalid argument to function `dofile'"); - /* else fname = NULL */ + char *fname = luaL_opt_string(1, NULL, "dofile"); if (lua_dofile(fname) == 0) if (passresults() == 0) lua_pushuserdata(NULL); /* at least one result to signal no errors */ @@ -247,15 +215,15 @@ static void luaI_assert (void) static void luaI_setglobal (void) { lua_Object value = lua_getparam(2); - check_arg(value != LUA_NOOBJECT, "setglobal"); + luaL_arg_check(value != LUA_NOOBJECT, "setglobal", 2, NULL); lua_pushobject(value); - lua_storeglobal(check_string(1, "setglobal")); + lua_storeglobal(luaL_check_string(1, "setglobal")); lua_pushobject(value); /* return given value */ } static void luaI_getglobal (void) { - lua_pushobject(lua_getglobal(check_string(1, "getglobal"))); + lua_pushobject(lua_getglobal(luaL_check_string(1, "getglobal"))); } #define MAXPARAMS 256 @@ -265,8 +233,8 @@ static void luaI_call (void) lua_Object arg = lua_getparam(2); lua_Object temp, params[MAXPARAMS]; int narg, i; - check_arg(lua_istable(arg), "call"); - check_arg(lua_isfunction(f), "call"); + luaL_arg_check(lua_isfunction(f), "call", 1, "function expected"); + luaL_arg_check(lua_istable(arg), "call", 2, "table expected"); /* narg = arg.n */ lua_pushobject(arg); lua_pushstring("n"); @@ -296,21 +264,22 @@ static void luaI_call (void) static void luaIl_settag (void) { lua_Object o = lua_getparam(1); - check_arg(o != LUA_NOOBJECT, "settag"); + luaL_arg_check(o != LUA_NOOBJECT, "settag", 1, NULL); lua_pushobject(o); - lua_settag(check_number(2, "settag")); + lua_settag(luaL_check_number(2, "settag")); } static void luaIl_newtag (void) { - lua_pushnumber(lua_newtag(check_string(1, "newtag"))); + lua_pushnumber(lua_newtag(luaL_check_string(1, "newtag"))); } static void basicindex (void) { lua_Object t = lua_getparam(1); lua_Object i = lua_getparam(2); - check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT, "basicindex"); + luaL_arg_check(t != LUA_NOOBJECT, "basicindex", 1, NULL); + luaL_arg_check(i != LUA_NOOBJECT, "basicindex", 2, NULL); lua_pushobject(t); lua_pushobject(i); lua_pushobject(lua_basicindex()); @@ -321,8 +290,8 @@ static void basicstoreindex (void) lua_Object t = lua_getparam(1); lua_Object i = lua_getparam(2); lua_Object v = lua_getparam(3); - check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, - "basicindex"); + luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, + "basicindex", 0, NULL); lua_pushobject(t); lua_pushobject(i); lua_pushobject(v); diff --git a/iolib.c b/iolib.c index bb352009..8315707f 100644 --- a/iolib.c +++ b/iolib.c @@ -58,7 +58,7 @@ static void io_readfrom (void) else if (lua_isuserdata(f)) lua_infile = lua_getuserdata(f); else { - char *s = lua_check_string(1, "readfrom"); + char *s = luaL_check_string(1, "readfrom"); FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); if (fp) lua_infile = fp; @@ -79,7 +79,7 @@ static void io_writeto (void) else if (lua_isuserdata(f)) lua_outfile = lua_getuserdata(f); else { - char *s = lua_check_string(1, "writeto"); + char *s = luaL_check_string(1, "writeto"); FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); if (fp) lua_outfile = fp; @@ -94,7 +94,7 @@ static void io_writeto (void) static void io_appendto (void) { - char *s = lua_check_string(1, "appendto"); + char *s = luaL_check_string(1, "appendto"); FILE *fp = fopen (s, "a"); if (fp != NULL) { lua_outfile = fp; @@ -110,7 +110,7 @@ static void io_appendto (void) static void io_read (void) { char *buff; - char *p = lua_opt_string(1, "[^\n]*{\n}", "read"); + char *p = luaL_opt_string(1, "[^\n]*{\n}", "read"); int inskip = 0; /* to control {skips} */ int c = NEED_OTHER; luaI_addchar(0); @@ -161,7 +161,7 @@ static void io_write (void) int arg = 1; int status = 1; char *s; - while ((s = lua_opt_string(arg++, NULL, "write")) != NULL) + while ((s = luaL_opt_string(arg++, NULL, "write")) != NULL) status = status && (fputs(s, lua_outfile) != EOF); pushresult(status); } @@ -169,20 +169,20 @@ static void io_write (void) static void io_execute (void) { - lua_pushnumber(system(lua_check_string(1, "execute"))); + lua_pushnumber(system(luaL_check_string(1, "execute"))); } static void io_remove (void) { - pushresult(remove(lua_check_string(1, "remove")) == 0); + pushresult(remove(luaL_check_string(1, "remove")) == 0); } static void io_rename (void) { - pushresult(rename(lua_check_string(1, "rename"), - lua_check_string(2, "rename")) == 0); + pushresult(rename(luaL_check_string(1, "rename"), + luaL_check_string(2, "rename")) == 0); } @@ -195,7 +195,7 @@ static void io_tmpname (void) static void io_getenv (void) { - lua_pushstring(getenv(lua_check_string(1, "getenv"))); /* if NULL push nil */ + lua_pushstring(getenv(luaL_check_string(1, "getenv"))); /* if NULL push nil */ } @@ -203,7 +203,7 @@ static void io_date (void) { time_t t; struct tm *tm; - char *s = lua_opt_string(1, "%c", "date"); + char *s = luaL_opt_string(1, "%c", "date"); char b[BUFSIZ]; time(&t); tm = localtime(&t); if (strftime(b,sizeof(b),s,tm)) @@ -269,7 +269,7 @@ static void lua_printstack (FILE *f) static void errorfb (void) { - char *s = lua_opt_string(1, "(no messsage)", NULL); + char *s = luaL_opt_string(1, "(no messsage)", NULL); fprintf(stderr, "lua: %s\n", s); lua_printstack(stderr); } diff --git a/lua.h b/lua.h index 623465e1..2aeb9981 100644 --- a/lua.h +++ b/lua.h @@ -2,7 +2,7 @@ ** LUA - Linguagem para Usuarios de Aplicacao ** Grupo de Tecnologia em Computacao Grafica ** TeCGraf - PUC-Rio -** $Id: lua.h,v 3.34 1997/02/20 15:51:14 roberto Exp roberto $ +** $Id: lua.h,v 3.35 1997/02/26 17:38:41 roberto Unstable roberto $ */ @@ -80,6 +80,7 @@ void lua_unref (int ref); lua_Object lua_createtable (void); +/* =============================================================== */ /* some useful macros */ #define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) @@ -89,7 +90,17 @@ lua_Object lua_createtable (void); #define lua_pushuserdata(u) lua_pushusertag(u, 0) +/* =============================================================== */ +/* Auxiliar functions for libraries */ +void luaL_arg_check(int cond, char *funcname, int numarg, char *extramsg); +char *luaL_check_string (int numArg, char *funcname); +char *luaL_opt_string (int numArg, char *def, char *funcname); +double luaL_check_number (int numArg, char *funcname); +double luaL_opt_number (int numArg, double def, char *funcname); + + +/* =============================================================== */ /* for compatibility with old versions. Avoid using these macros */ #define lua_type(o) (lua_tag(o)) diff --git a/lualib.h b/lualib.h index ba2d108d..6deb9e97 100644 --- a/lualib.h +++ b/lualib.h @@ -2,7 +2,7 @@ ** Libraries to be used in LUA programs ** Grupo de Tecnologia em Computacao Grafica ** TeCGraf - PUC-Rio -** $Id: lualib.h,v 1.9 1996/08/01 14:55:33 roberto Exp roberto $ +** $Id: lualib.h,v 1.10 1996/08/05 20:55:24 roberto Exp roberto $ */ #ifndef lualib_h @@ -23,11 +23,6 @@ struct lua_reg { }; void luaI_openlib (struct lua_reg *l, int n); -void lua_arg_check(int cond, char *funcname); -char *lua_check_string (int numArg, char *funcname); -char *lua_opt_string (int numArg, char *def, char *funcname); -double lua_check_number (int numArg, char *funcname); -long lua_opt_number (int numArg, long def, char *funcname); char *luaI_addchar (int c); void luaI_addquoted (char *s); diff --git a/makefile b/makefile index 2f65498e..8e23fcdd 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -# $Id: makefile,v 1.27 1996/11/06 20:48:03 roberto Exp roberto $ +# $Id: makefile,v 1.28 1997/03/05 13:37:04 roberto Exp roberto $ #configuration @@ -30,7 +30,8 @@ LUAOBJS = \ fallback.o \ mem.o \ func.o \ - undump.o + undump.o \ + auxlib.o LIBOBJS = \ iolib.o \ @@ -75,6 +76,7 @@ clear : co $@ +auxlib.o: auxlib.c lua.h fallback.o: fallback.c mem.h fallback.h lua.h opcode.h types.h tree.h \ func.h table.h hash.h func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \ @@ -97,6 +99,6 @@ strlib.o: strlib.c lua.h lualib.h table.o: table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h \ table.h inout.h fallback.h luadebug.h tree.o: tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h \ - table.h + table.h fallback.h undump.o: undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h \ undump.h diff --git a/mathlib.c b/mathlib.c index 896ec1f5..0ed1a9a6 100644 --- a/mathlib.c +++ b/mathlib.c @@ -3,7 +3,7 @@ ** Mathematics library to LUA */ -char *rcs_mathlib="$Id: mathlib.c,v 1.17 1996/04/30 21:13:55 roberto Exp roberto $"; +char *rcs_mathlib="$Id: mathlib.c,v 1.18 1996/08/01 14:55:33 roberto Exp roberto $"; #include #include @@ -19,7 +19,7 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.17 1996/04/30 21:13:55 roberto Exp roberto static void math_abs (void) { - double d = lua_check_number(1, "abs"); + double d = luaL_check_number(1, "abs"); if (d < 0) d = -d; lua_pushnumber (d); } @@ -27,7 +27,7 @@ static void math_abs (void) static void math_sin (void) { - double d = lua_check_number(1, "sin"); + double d = luaL_check_number(1, "sin"); lua_pushnumber (sin(TORAD(d))); } @@ -35,7 +35,7 @@ static void math_sin (void) static void math_cos (void) { - double d = lua_check_number(1, "cos"); + double d = luaL_check_number(1, "cos"); lua_pushnumber (cos(TORAD(d))); } @@ -43,64 +43,64 @@ static void math_cos (void) static void math_tan (void) { - double d = lua_check_number(1, "tan"); + double d = luaL_check_number(1, "tan"); lua_pushnumber (tan(TORAD(d))); } static void math_asin (void) { - double d = lua_check_number(1, "asin"); + double d = luaL_check_number(1, "asin"); lua_pushnumber (TODEGREE(asin(d))); } static void math_acos (void) { - double d = lua_check_number(1, "acos"); + double d = luaL_check_number(1, "acos"); lua_pushnumber (TODEGREE(acos(d))); } static void math_atan (void) { - double d = lua_check_number(1, "atan"); + double d = luaL_check_number(1, "atan"); lua_pushnumber (TODEGREE(atan(d))); } static void math_atan2 (void) { - double d1 = lua_check_number(1, "atan2"); - double d2 = lua_check_number(2, "atan2"); + double d1 = luaL_check_number(1, "atan2"); + double d2 = luaL_check_number(2, "atan2"); lua_pushnumber (TODEGREE(atan2(d1, d2))); } static void math_ceil (void) { - double d = lua_check_number(1, "ceil"); + double d = luaL_check_number(1, "ceil"); lua_pushnumber (ceil(d)); } static void math_floor (void) { - double d = lua_check_number(1, "floor"); + double d = luaL_check_number(1, "floor"); lua_pushnumber (floor(d)); } static void math_mod (void) { - float x = lua_check_number(1, "mod"); - float y = lua_check_number(2, "mod"); + float x = luaL_check_number(1, "mod"); + float y = luaL_check_number(2, "mod"); lua_pushnumber(fmod(x, y)); } static void math_sqrt (void) { - double d = lua_check_number(1, "sqrt"); + double d = luaL_check_number(1, "sqrt"); lua_pushnumber (sqrt(d)); } @@ -131,10 +131,10 @@ static void math_pow (void) static void math_min (void) { int i=1; - double dmin = lua_check_number(i, "min"); + double dmin = luaL_check_number(i, "min"); while (lua_getparam(++i) != LUA_NOOBJECT) { - double d = lua_check_number(i, "min"); + double d = luaL_check_number(i, "min"); if (d < dmin) dmin = d; } lua_pushnumber (dmin); @@ -143,10 +143,10 @@ static void math_min (void) static void math_max (void) { int i=1; - double dmax = lua_check_number(i, "max"); + double dmax = luaL_check_number(i, "max"); while (lua_getparam(++i) != LUA_NOOBJECT) { - double d = lua_check_number(i, "max"); + double d = luaL_check_number(i, "max"); if (d > dmax) dmax = d; } lua_pushnumber (dmax); @@ -154,33 +154,33 @@ static void math_max (void) static void math_log (void) { - double d = lua_check_number(1, "log"); + double d = luaL_check_number(1, "log"); lua_pushnumber (log(d)); } static void math_log10 (void) { - double d = lua_check_number(1, "log10"); + double d = luaL_check_number(1, "log10"); lua_pushnumber (log10(d)); } static void math_exp (void) { - double d = lua_check_number(1, "exp"); + double d = luaL_check_number(1, "exp"); lua_pushnumber (exp(d)); } static void math_deg (void) { - float d = lua_check_number(1, "deg"); + float d = luaL_check_number(1, "deg"); lua_pushnumber (d*180./PI); } static void math_rad (void) { - float d = lua_check_number(1, "rad"); + float d = luaL_check_number(1, "rad"); lua_pushnumber (d/180.*PI); } @@ -191,7 +191,7 @@ static void math_random (void) static void math_randomseed (void) { - srand(lua_check_number(1, "randomseed")); + srand(luaL_check_number(1, "randomseed")); } diff --git a/strlib.c b/strlib.c index fef0e1ef..3db67936 100644 --- a/strlib.c +++ b/strlib.c @@ -3,7 +3,7 @@ ** String library to LUA */ -char *rcs_strlib="$Id: strlib.c,v 1.34 1996/11/22 13:08:02 roberto Exp roberto $"; +char *rcs_strlib="$Id: strlib.c,v 1.35 1997/02/21 15:21:34 roberto Exp roberto $"; #include #include @@ -42,41 +42,6 @@ static char *openspace (unsigned long size) return buff+lbuffer.size; } -void lua_arg_check(int cond, char *funcname) -{ - if (!cond) { - char buff[100]; - sprintf(buff, "incorrect argument to function `%s'", funcname); - lua_error(buff); - } -} - -char *lua_check_string (int numArg, char *funcname) -{ - lua_Object o = lua_getparam(numArg); - lua_arg_check(lua_isstring(o), funcname); - return lua_getstring(o); -} - -char *lua_opt_string (int numArg, char *def, char *funcname) -{ - return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : - lua_check_string(numArg, funcname); -} - -double lua_check_number (int numArg, char *funcname) -{ - lua_Object o = lua_getparam(numArg); - lua_arg_check(lua_isnumber(o), funcname); - return lua_getnumber(o); -} - -long lua_opt_number (int numArg, long def, char *funcname) -{ - return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : - (long)lua_check_number(numArg, funcname); -} - char *luaI_addchar (int c) { if (lbuffer.size >= lbuffer.max) @@ -104,8 +69,8 @@ static void addstr (char *s) */ static void str_tok (void) { - char *s1 = lua_check_string(1, "strtok"); - char *del = lua_check_string(2, "strtok"); + char *s1 = luaL_check_string(1, "strtok"); + char *del = luaL_check_string(2, "strtok"); lua_Object t = lua_createtable(); int i = 1; /* As strtok changes s1, and s1 is "constant", make a copy of it */ @@ -127,7 +92,7 @@ static void str_tok (void) */ static void str_len (void) { - lua_pushnumber(strlen(lua_check_string(1, "strlen"))); + lua_pushnumber(strlen(luaL_check_string(1, "strlen"))); } /* @@ -135,9 +100,9 @@ static void str_len (void) */ static void str_sub (void) { - char *s = lua_check_string(1, "strsub"); - long start = (long)lua_check_number(2, "strsub"); - long end = lua_opt_number(3, strlen(s), "strsub"); + char *s = luaL_check_string(1, "strsub"); + long start = (long)luaL_check_number(2, "strsub"); + long end = (long)luaL_opt_number(3, strlen(s), "strsub"); if (1 <= start && start <= end && end <= strlen(s)) { luaI_addchar(0); addnchar(s+start-1, end-start+1); @@ -151,7 +116,7 @@ static void str_sub (void) */ static void str_lower (void) { - char *s = lua_check_string(1, "strlower"); + char *s = luaL_check_string(1, "strlower"); luaI_addchar(0); while (*s) luaI_addchar(tolower((unsigned char)*s++)); @@ -163,7 +128,7 @@ static void str_lower (void) */ static void str_upper (void) { - char *s = lua_check_string(1, "strupper"); + char *s = luaL_check_string(1, "strupper"); luaI_addchar(0); while (*s) luaI_addchar(toupper((unsigned char)*s++)); @@ -172,8 +137,8 @@ static void str_upper (void) static void str_rep (void) { - char *s = lua_check_string(1, "strrep"); - int n = (int)lua_check_number(2, "strrep"); + char *s = luaL_check_string(1, "strrep"); + int n = (int)luaL_check_number(2, "strrep"); luaI_addchar(0); while (n-- > 0) addstr(s); @@ -185,9 +150,9 @@ static void str_rep (void) */ static void str_ascii (void) { - char *s = lua_check_string(1, "ascii"); - long pos = lua_opt_number(2, 1, "ascii") - 1; - lua_arg_check(0<=pos && pos