From 2de803c250de373186afbbea0a5978f54c52850c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Mar 1997 12:30:50 -0300 Subject: new header 'auxlib.h' + new function luaL_verror --- auxlib.c | 27 ++++++++++++++++++++++----- iolib.c | 12 ++++++------ lex.c | 7 +++---- lualib.h | 9 ++------- mathlib.c | 7 ++++--- strlib.c | 15 ++++----------- 6 files changed, 41 insertions(+), 36 deletions(-) diff --git a/auxlib.c b/auxlib.c index 0bee6430..ea3b7678 100644 --- a/auxlib.c +++ b/auxlib.c @@ -1,20 +1,20 @@ -char *rcs_auxlib="$Id: $"; +char *rcs_auxlib="$Id: auxlib.c,v 1.1 1997/03/17 17:02:29 roberto Exp roberto $"; #include +#include #include "lua.h" +#include "auxlib.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); + luaL_verror("bad argument #%d to function `%s'", numarg, funcname); else - sprintf(buff, "bad argument #%d to function `%s' (%s)", + luaL_verror("bad argument #%d to function `%s' (%s)", numarg, funcname, extramsg); - lua_error(buff); } } @@ -45,3 +45,20 @@ double luaL_opt_number (int numArg, double def, char *funcname) luaL_check_number(numArg, funcname); } +void luaL_openlib (struct luaL_reg *l, int n) +{ + int i; + for (i=0; i #include "lua.h" +#include "auxlib.h" #include "luadebug.h" #include "lualib.h" @@ -234,13 +235,12 @@ static void io_debug (void) static void lua_printstack (FILE *f) { - int level = 0; + int level = 1; /* skip level 0 (it's this function) */ lua_Object func; - fprintf(f, "Active Stack:\n"); while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) { char *name; int currentline; - fprintf(f, "\t"); + fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t"); switch (*lua_getobjname(func, &name)) { case 'g': fprintf(f, "function %s", name); @@ -275,7 +275,7 @@ static void errorfb (void) } -static struct lua_reg iolib[] = { +static struct luaL_reg iolib[] = { {"readfrom", io_readfrom}, {"writeto", io_writeto}, {"appendto", io_appendto}, @@ -295,6 +295,6 @@ static struct lua_reg iolib[] = { void iolib_open (void) { lua_infile=stdin; lua_outfile=stdout; - luaI_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); - lua_setfallback("error", errorfb); + luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); + lua_setglobalmethod("error", errorfb); } diff --git a/lex.c b/lex.c index aa8bd588..76c48040 100644 --- a/lex.c +++ b/lex.c @@ -1,9 +1,10 @@ -char *rcs_lex = "$Id: lex.c,v 2.41 1996/11/22 13:08:02 roberto Exp roberto $"; +char *rcs_lex = "$Id: lex.c,v 2.42 1997/02/07 13:49:46 roberto Exp roberto $"; #include #include +#include "auxlib.h" #include "mem.h" #include "tree.h" #include "table.h" @@ -32,10 +33,8 @@ void lua_setinput (Input fn) static void luaI_auxsyntaxerror (char *s, char *token) { - char msg[256]; - sprintf (msg,"%s;\n> last token read: \"%s\" at line %d in file %s", + luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s", s, token, lua_linenumber, lua_parsedfile); - lua_error (msg); } void luaI_syntaxerror (char *s) diff --git a/lualib.h b/lualib.h index 6deb9e97..09af12fb 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.10 1996/08/05 20:55:24 roberto Exp roberto $ +** $Id: lualib.h,v 1.11 1997/03/17 17:01:10 roberto Exp roberto $ */ #ifndef lualib_h @@ -15,14 +15,9 @@ void strlib_open (void); void mathlib_open (void); -/* auxiliar functions (private) */ -struct lua_reg { - char *name; - lua_CFunction func; -}; +/* auxiliar functions (private) */ -void luaI_openlib (struct lua_reg *l, int n); char *luaI_addchar (int c); void luaI_addquoted (char *s); diff --git a/mathlib.c b/mathlib.c index 0ed1a9a6..798ba322 100644 --- a/mathlib.c +++ b/mathlib.c @@ -3,12 +3,13 @@ ** Mathematics library to LUA */ -char *rcs_mathlib="$Id: mathlib.c,v 1.18 1996/08/01 14:55:33 roberto Exp roberto $"; +char *rcs_mathlib="$Id: mathlib.c,v 1.19 1997/03/17 17:01:10 roberto Exp roberto $"; #include #include #include "lualib.h" +#include "auxlib.h" #include "lua.h" #ifndef PI @@ -195,7 +196,7 @@ static void math_randomseed (void) } -static struct lua_reg mathlib[] = { +static struct luaL_reg mathlib[] = { {"abs", math_abs}, {"sin", math_sin}, {"cos", math_cos}, @@ -224,7 +225,7 @@ static struct lua_reg mathlib[] = { */ void mathlib_open (void) { - luaI_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); + luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1); } diff --git a/strlib.c b/strlib.c index 3db67936..122a4fb1 100644 --- a/strlib.c +++ b/strlib.c @@ -3,7 +3,7 @@ ** String library to LUA */ -char *rcs_strlib="$Id: strlib.c,v 1.35 1997/02/21 15:21:34 roberto Exp roberto $"; +char *rcs_strlib="$Id: strlib.c,v 1.36 1997/03/17 17:01:10 roberto Exp roberto $"; #include #include @@ -11,6 +11,7 @@ char *rcs_strlib="$Id: strlib.c,v 1.35 1997/02/21 15:21:34 roberto Exp roberto $ #include #include "lua.h" +#include "auxlib.h" #include "lualib.h" @@ -518,15 +519,7 @@ static void str_format (void) } -void luaI_openlib (struct lua_reg *l, int n) -{ - int i; - for (i=0; i