From 88c9bf99de50df5575cce13e8e6c278b9f1dc0d0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 20 Mar 2002 09:54:08 -0300 Subject: [PATCH] standard libraries in packages --- lauxlib.c | 21 +++++-- lauxlib.h | 7 ++- lbaselib.c | 158 +++++++++++++++++++++++++++++----------------------- ldblib.c | 94 ++++++++++++++++++++++++++++++- liolib.c | 160 ++++++++++++++--------------------------------------- lmathlib.c | 60 +++++++++++--------- lstrlib.c | 30 +++++----- ltests.c | 14 ++--- 8 files changed, 292 insertions(+), 252 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index ceef7026..db7ab9b3 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.60 2002/02/14 21:41:53 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.61 2002/03/07 18:15:10 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -116,10 +116,21 @@ LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) { } -LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n) { - int i; - for (i=0; iname; l++) { + lua_pushstring(L, l->name); + lua_pushcfunction(L, l->func); + lua_settable(L, -3); + } +} + + +LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, + const luaL_reg *l) { + lua_pushstring(L, libname); + lua_newtable(L); + luaL_openlib(L, l); + lua_settable(L, LUA_GLOBALSINDEX); } diff --git a/lauxlib.h b/lauxlib.h index 57d259c0..250c7da9 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ +** $Id: lauxlib.h,v 1.42 2002/02/05 22:36:52 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -27,7 +27,9 @@ typedef struct luaL_reg { } luaL_reg; -LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n); +LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l); +LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, + const luaL_reg *l); LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname); LUALIB_API void luaL_argerror (lua_State *L, int numarg, const char *extramsg); @@ -67,7 +69,6 @@ LUALIB_API const char *luaL_errstr (int errcode); #define luaL_check_long(L,n) ((long)luaL_check_number(L, n)) #define luaL_opt_int(L,n,d) ((int)luaL_opt_number(L, n,d)) #define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d)) -#define luaL_openl(L,a) luaL_openlib(L, a, (sizeof(a)/sizeof(a[0]))) /* diff --git a/lbaselib.c b/lbaselib.c index 363acdd9..c760172c 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ +** $Id: lbaselib.c,v 1.59 2002/02/14 21:42:22 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -19,12 +19,6 @@ -static void aux_setn (lua_State *L, int t, int n) { - lua_pushliteral(L, "n"); - lua_pushnumber(L, n); - lua_rawset(L, t); -} - /* ** If your system does not support `stderr', redefine this function, or @@ -124,17 +118,6 @@ static int luaB_error (lua_State *L) { return 0; /* to avoid warnings */ } -static int luaB_setglobal (lua_State *L) { - luaL_check_any(L, 2); - lua_setglobal(L, luaL_check_string(L, 1)); - return 0; -} - -static int luaB_getglobal (lua_State *L) { - lua_getglobal(L, luaL_check_string(L, 1)); - return 1; -} - static int luaB_metatable (lua_State *L) { luaL_check_type(L, 1, LUA_TTABLE); @@ -191,12 +174,7 @@ static int luaB_collectgarbage (lua_State *L) { static int luaB_type (lua_State *L) { luaL_check_any(L, 1); - if (lua_isnone(L, 2)) - lua_pushstring(L, lua_typename(L, lua_type(L, 1))); - else { - lua_pushboolean(L, - (strcmp(lua_typename(L, lua_type(L, 1)), luaL_check_string(L, 2)) == 0)); - } + lua_pushstring(L, lua_typename(L, lua_type(L, 1))); return 1; } @@ -248,6 +226,7 @@ static int luaB_loadstring (lua_State *L) { return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop); } + static int luaB_dofile (lua_State *L) { int oldtop = lua_gettop(L); const char *fname = luaL_opt_string(L, 1, NULL); @@ -411,6 +390,50 @@ static int luaB_tostring (lua_State *L) { } +static const luaL_reg base_funcs[] = { + {LUA_ALERT, luaB__ALERT}, + {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, + {"error", luaB_error}, + {"metatable", luaB_metatable}, + {"globals", luaB_globals}, + {"next", luaB_next}, + {"print", luaB_print}, + {"tonumber", luaB_tonumber}, + {"tostring", luaB_tostring}, + {"type", luaB_type}, + {"assert", luaB_assert}, + {"unpack", luaB_unpack}, + {"rawget", luaB_rawget}, + {"rawset", luaB_rawset}, + {"call", luaB_call}, + {"collectgarbage", luaB_collectgarbage}, + {"gcinfo", luaB_gcinfo}, + {"loadfile", luaB_loadfile}, + {"loadstring", luaB_loadstring}, + {"dofile", luaB_dofile}, + {"dostring", luaB_dostring}, + {NULL, NULL} +}; + + +static void base_open (lua_State *L) { + lua_pushliteral(L, "_G"); + lua_pushvalue(L, LUA_GLOBALSINDEX); + luaL_openlib(L, base_funcs); /* open lib into global table */ + lua_pushliteral(L, "_VERSION"); + lua_pushliteral(L, LUA_VERSION); + lua_settable(L, -3); /* set global _VERSION */ + lua_settable(L, -1); /* set global _G */ +} + + +/* +** {====================================================== +** Coroutine library +** ======================================================= +*/ + + static int luaB_resume (lua_State *L) { lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1)); if (lua_resume(L, co) != 0) @@ -457,6 +480,26 @@ static int luaB_yield (lua_State *L) { return lua_yield(L, lua_gettop(L)); } +static const luaL_reg co_funcs[] = { + {"create", luaB_coroutine}, + {"yield", luaB_yield}, + {NULL, NULL} +}; + + +static void co_open (lua_State *L) { + luaL_opennamedlib(L, "co", co_funcs); + /* create metatable for coroutines */ + lua_pushliteral(L, "Coroutine"); + lua_newtable(L); + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, gc_coroutine); + lua_rawset(L, -3); + lua_rawset(L, LUA_REGISTRYINDEX); +} + +/* }====================================================== */ + /* ** {====================================================== @@ -499,6 +542,13 @@ static int luaB_foreach (lua_State *L) { } +static void aux_setn (lua_State *L, int t, int n) { + lua_pushliteral(L, "n"); + lua_pushnumber(L, n); + lua_rawset(L, t); +} + + static int luaB_getn (lua_State *L) { luaL_check_type(L, 1, LUA_TTABLE); lua_pushnumber(L, lua_getn(L, 1)); @@ -653,61 +703,29 @@ static int luaB_sort (lua_State *L) { /* }====================================================== */ + +static const luaL_reg array_funcs[] = { + {"foreach", luaB_foreach}, + {"foreachi", luaB_foreachi}, + {"getn", luaB_getn}, + {"sort", luaB_sort}, + {"insert", luaB_tinsert}, + {"remove", luaB_tremove}, + {NULL, NULL} +}; + /* }====================================================== */ -static const luaL_reg base_funcs[] = { - {LUA_ALERT, luaB__ALERT}, - {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, - {"call", luaB_call}, - {"collectgarbage", luaB_collectgarbage}, - {"coroutine", luaB_coroutine}, - {"dofile", luaB_dofile}, - {"dostring", luaB_dostring}, - {"error", luaB_error}, - {"metatable", luaB_metatable}, - {"foreach", luaB_foreach}, - {"foreachi", luaB_foreachi}, - {"gcinfo", luaB_gcinfo}, - {"getglobal", luaB_getglobal}, /* compatibility with 4.0 */ - {"globals", luaB_globals}, - {"loadfile", luaB_loadfile}, - {"loadstring", luaB_loadstring}, - {"next", luaB_next}, - {"print", luaB_print}, - {"rawget", luaB_rawget}, - {"rawset", luaB_rawset}, - {"setglobal", luaB_setglobal}, /* compatibility with 4.0 */ - {"tonumber", luaB_tonumber}, - {"tostring", luaB_tostring}, - {"type", luaB_type}, - {"assert", luaB_assert}, - {"getn", luaB_getn}, - {"sort", luaB_sort}, - {"tinsert", luaB_tinsert}, - {"tremove", luaB_tremove}, - {"unpack", luaB_unpack}, - {"yield", luaB_yield} -}; - - - LUALIB_API int lua_baselibopen (lua_State *L) { - luaL_openl(L, base_funcs); - lua_pushliteral(L, LUA_VERSION); - lua_setglobal(L, "_VERSION"); + base_open(L); + co_open(L); + luaL_opennamedlib(L, "A", array_funcs); /* `require' needs an empty table as upvalue */ lua_newtable(L); lua_pushcclosure(L, luaB_require, 1); lua_setglobal(L, "require"); - /* create metatable for coroutines */ - lua_pushliteral(L, "Coroutine"); - lua_newtable(L); - lua_pushliteral(L, "gc"); - lua_pushcfunction(L, gc_coroutine); - lua_rawset(L, -3); - lua_rawset(L, LUA_REGISTRYINDEX); return 0; } diff --git a/ldblib.c b/ldblib.c index 58eb56c9..e379acfd 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ +** $Id: ldblib.c,v 1.43 2002/02/07 17:24:32 roberto Exp roberto $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -167,17 +167,105 @@ static int setlinehook (lua_State *L) { } +static int debug (lua_State *L) { + for (;;) { + char buffer[250]; + fprintf(stderr, "lua_debug> "); + if (fgets(buffer, sizeof(buffer), stdin) == 0 || + strcmp(buffer, "cont\n") == 0) + return 0; + lua_dostring(L, buffer); + lua_settop(L, 0); /* remove eventual returns */ + } +} + + +#define LEVELS1 12 /* size of the first part of the stack */ +#define LEVELS2 10 /* size of the second part of the stack */ + +static int errorfb (lua_State *L) { + int level = 1; /* skip level 0 (it's this function) */ + int firstpart = 1; /* still before eventual `...' */ + lua_Debug ar; + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addstring(&b, "error: "); + luaL_addstring(&b, luaL_check_string(L, 1)); + luaL_addstring(&b, "\n"); + while (lua_getstack(L, level++, &ar)) { + char buff[120]; /* enough to fit following `sprintf's */ + if (level == 2) + luaL_addstring(&b, "stack traceback:\n"); + else if (level > LEVELS1 && firstpart) { + /* no more than `LEVELS2' more levels? */ + if (!lua_getstack(L, level+LEVELS2, &ar)) + level--; /* keep going */ + else { + luaL_addstring(&b, " ...\n"); /* too many levels */ + while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ + level++; + } + firstpart = 0; + continue; + } + sprintf(buff, "%4d: ", level-1); + luaL_addstring(&b, buff); + lua_getinfo(L, "Snl", &ar); + switch (*ar.namewhat) { + case 'g': case 'l': /* global, local */ + sprintf(buff, "function `%.50s'", ar.name); + break; + case 'f': /* field */ + sprintf(buff, "method `%.50s'", ar.name); + break; + case 't': /* tag method */ + sprintf(buff, "`%.50s' tag method", ar.name); + break; + default: { + if (*ar.what == 'm') /* main? */ + sprintf(buff, "main of %.70s", ar.short_src); + else if (*ar.what == 'C') /* C function? */ + sprintf(buff, "%.70s", ar.short_src); + else + sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src); + ar.source = NULL; /* do not print source again */ + } + } + luaL_addstring(&b, buff); + if (ar.currentline > 0) { + sprintf(buff, " at line %d", ar.currentline); + luaL_addstring(&b, buff); + } + if (ar.source) { + sprintf(buff, " [%.70s]", ar.short_src); + luaL_addstring(&b, buff); + } + luaL_addstring(&b, "\n"); + } + luaL_pushresult(&b); + lua_getglobal(L, LUA_ALERT); + if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ + lua_pushvalue(L, -2); /* error message */ + lua_rawcall(L, 1, 0); + } + return 0; +} + + static const luaL_reg dblib[] = { {"getlocal", getlocal}, {"getinfo", getinfo}, {"setcallhook", setcallhook}, {"setlinehook", setlinehook}, - {"setlocal", setlocal} + {"setlocal", setlocal}, + {"debug", debug}, + {NULL, NULL} }; LUALIB_API int lua_dblibopen (lua_State *L) { - luaL_openl(L, dblib); + luaL_opennamedlib(L, "dbg", dblib); + lua_register(L, LUA_ERRORMESSAGE, errorfb); return 0; } diff --git a/liolib.c b/liolib.c index 3ac3146f..ab394eb4 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ +** $Id: liolib.c,v 1.131 2002/02/08 22:39:56 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -20,6 +20,12 @@ +/* +** {====================================================== +** FILE Operations +** ======================================================= +*/ + #ifdef POPEN /* FILE *popen(); @@ -41,7 +47,7 @@ int pclose(); */ static const char *const filenames[] = {"_INPUT", "_OUTPUT"}; -static const char *const basicfiles[] = {"_STDIN", "_STDOUT"}; +static const char *const basicfiles[] = {"stdin", "stdout"}; static int pushresult (lua_State *L, int i) { @@ -58,15 +64,6 @@ static int pushresult (lua_State *L, int i) { } - -/* -** {====================================================== -** FILE Operations -** ======================================================= -*/ - - - static int checkfile (lua_State *L, int findex, const char *tname) { int res; lua_getmetatable(L, findex); @@ -116,8 +113,9 @@ static void newfile (lua_State *L, FILE *f) { static void newfilewithname (lua_State *L, FILE *f, const char *name) { + lua_pushstring(L, name); newfile(L, f); - lua_setglobal(L, name); + lua_settable(L, -3); } @@ -136,8 +134,11 @@ static int setnewfile (lua_State *L, FILE *f, int inout) { static void resetfile (lua_State *L, int inout) { - lua_getglobal(L, basicfiles[inout]); + lua_getglobal(L, "io"); + lua_pushstring(L, basicfiles[inout]); + lua_gettable(L, -2); lua_setglobal(L, filenames[inout]); + lua_pop(L, 1); } @@ -410,6 +411,20 @@ static int io_flush (lua_State *L) { return pushresult(L, fflush(f) == 0); } +static const luaL_reg iolib[] = { + {"appendto", io_appendto}, + {"close", io_close}, + {"flush", io_flush}, + {"open", io_open}, + {"read", io_read}, + {"readfrom", io_readfrom}, + {"seek", io_seek}, + {"tmpfile", io_tmpfile}, + {"write", io_write}, + {"writeto", io_writeto}, + {NULL, NULL} +}; + /* }====================================================== */ @@ -445,7 +460,6 @@ static int io_tmpname (lua_State *L) { } - static int io_getenv (lua_State *L) { lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */ return 1; @@ -580,128 +594,30 @@ static int io_exit (lua_State *L) { return 0; /* to avoid warnings */ } -/* }====================================================== */ - - - -static int io_debug (lua_State *L) { - for (;;) { - char buffer[250]; - fprintf(stderr, "lua_debug> "); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || - strcmp(buffer, "cont\n") == 0) - return 0; - lua_dostring(L, buffer); - lua_settop(L, 0); /* remove eventual returns */ - } -} - - -#define LEVELS1 12 /* size of the first part of the stack */ -#define LEVELS2 10 /* size of the second part of the stack */ - -static int errorfb (lua_State *L) { - int level = 1; /* skip level 0 (it's this function) */ - int firstpart = 1; /* still before eventual `...' */ - lua_Debug ar; - luaL_Buffer b; - luaL_buffinit(L, &b); - luaL_addstring(&b, "error: "); - luaL_addstring(&b, luaL_check_string(L, 1)); - luaL_addstring(&b, "\n"); - while (lua_getstack(L, level++, &ar)) { - char buff[120]; /* enough to fit following `sprintf's */ - if (level == 2) - luaL_addstring(&b, "stack traceback:\n"); - else if (level > LEVELS1 && firstpart) { - /* no more than `LEVELS2' more levels? */ - if (!lua_getstack(L, level+LEVELS2, &ar)) - level--; /* keep going */ - else { - luaL_addstring(&b, " ...\n"); /* too many levels */ - while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ - level++; - } - firstpart = 0; - continue; - } - sprintf(buff, "%4d: ", level-1); - luaL_addstring(&b, buff); - lua_getinfo(L, "Snl", &ar); - switch (*ar.namewhat) { - case 'g': case 'l': /* global, local */ - sprintf(buff, "function `%.50s'", ar.name); - break; - case 'f': /* field */ - sprintf(buff, "method `%.50s'", ar.name); - break; - case 't': /* tag method */ - sprintf(buff, "`%.50s' tag method", ar.name); - break; - default: { - if (*ar.what == 'm') /* main? */ - sprintf(buff, "main of %.70s", ar.short_src); - else if (*ar.what == 'C') /* C function? */ - sprintf(buff, "%.70s", ar.short_src); - else - sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src); - ar.source = NULL; /* do not print source again */ - } - } - luaL_addstring(&b, buff); - if (ar.currentline > 0) { - sprintf(buff, " at line %d", ar.currentline); - luaL_addstring(&b, buff); - } - if (ar.source) { - sprintf(buff, " [%.70s]", ar.short_src); - luaL_addstring(&b, buff); - } - luaL_addstring(&b, "\n"); - } - luaL_pushresult(&b); - lua_getglobal(L, LUA_ALERT); - if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ - lua_pushvalue(L, -2); /* error message */ - lua_rawcall(L, 1, 0); - } - return 0; -} - - - -static const luaL_reg iolib[] = { - {"appendto", io_appendto}, +static const luaL_reg syslib[] = { {"clock", io_clock}, - {"closefile", io_close}, {"date", io_date}, - {"debug", io_debug}, {"difftime", io_difftime}, {"execute", io_execute}, {"exit", io_exit}, - {"flush", io_flush}, {"getenv", io_getenv}, - {"openfile", io_open}, - {"read", io_read}, - {"readfrom", io_readfrom}, {"remove", io_remove}, {"rename", io_rename}, - {"seek", io_seek}, {"setlocale", io_setloc}, {"time", io_time}, - {"tmpfile", io_tmpfile}, {"tmpname", io_tmpname}, - {"write", io_write}, - {"writeto", io_writeto}, - {LUA_ERRORMESSAGE, errorfb} + {NULL, NULL} }; +/* }====================================================== */ + + LUALIB_API int lua_iolibopen (lua_State *L) { lua_pushliteral(L, FILEHANDLE); lua_newtable(L); /* meta table for FILEHANDLE */ /* close files when collected */ - lua_pushliteral(L, "gc"); + lua_pushliteral(L, "__gc"); lua_pushcfunction(L, file_collect); lua_rawset(L, -3); /* put new metatable into registry */ @@ -710,11 +626,15 @@ LUALIB_API int lua_iolibopen (lua_State *L) { lua_pushliteral(L, CLOSEDFILEHANDLE); lua_newtable(L); lua_rawset(L, LUA_REGISTRYINDEX); - luaL_openl(L, iolib); + luaL_opennamedlib(L, "os", syslib); + lua_pushliteral(L, "io"); + lua_newtable(L); + luaL_openlib(L, iolib); /* predefined file handles */ newfilewithname(L, stdin, basicfiles[INFILE]); newfilewithname(L, stdout, basicfiles[OUTFILE]); - newfilewithname(L, stderr, "_STDERR"); + newfilewithname(L, stderr, "stderr"); + lua_settable(L, LUA_GLOBALSINDEX); resetfile(L, INFILE); resetfile(L, OUTFILE); return 0; diff --git a/lmathlib.c b/lmathlib.c index e5be054a..8906944d 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $ +** $Id: lmathlib.c,v 1.40 2001/12/05 20:15:18 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -200,39 +200,45 @@ static int math_randomseed (lua_State *L) { static const luaL_reg mathlib[] = { -{"abs", math_abs}, -{"sin", math_sin}, -{"cos", math_cos}, -{"tan", math_tan}, -{"asin", math_asin}, -{"acos", math_acos}, -{"atan", math_atan}, -{"atan2", math_atan2}, -{"ceil", math_ceil}, -{"floor", math_floor}, -{"mod", math_mod}, -{"frexp", math_frexp}, -{"ldexp", math_ldexp}, -{"sqrt", math_sqrt}, -{"min", math_min}, -{"max", math_max}, -{"log", math_log}, -{"log10", math_log10}, -{"exp", math_exp}, -{"deg", math_deg}, -{"pow", math_pow}, -{"rad", math_rad}, -{"random", math_random}, -{"randomseed", math_randomseed} + {"abs", math_abs}, + {"sin", math_sin}, + {"cos", math_cos}, + {"tan", math_tan}, + {"asin", math_asin}, + {"acos", math_acos}, + {"atan", math_atan}, + {"atan2", math_atan2}, + {"ceil", math_ceil}, + {"floor", math_floor}, + {"mod", math_mod}, + {"frexp", math_frexp}, + {"ldexp", math_ldexp}, + {"sqrt", math_sqrt}, + {"min", math_min}, + {"max", math_max}, + {"log", math_log}, + {"log10", math_log10}, + {"exp", math_exp}, + {"deg", math_deg}, + {"pow", math_pow}, + {"rad", math_rad}, + {"random", math_random}, + {"randomseed", math_randomseed}, + {NULL, NULL} }; + /* ** Open math library */ LUALIB_API int lua_mathlibopen (lua_State *L) { - luaL_openl(L, mathlib); + lua_pushliteral(L, "math"); + lua_newtable(L); + luaL_openlib(L, mathlib); + lua_pushliteral(L, "pi"); lua_pushnumber(L, PI); - lua_setglobal(L, "PI"); + lua_settable(L, -3); + lua_settable(L, LUA_GLOBALSINDEX); return 0; } diff --git a/lstrlib.c b/lstrlib.c index 7ad674bd..ff8eba33 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.77 2002/02/08 22:39:36 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.78 2002/03/11 13:29:40 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -707,18 +707,19 @@ static int str_format (lua_State *L) { static const luaL_reg strlib[] = { -{"strlen", str_len}, -{"strsub", str_sub}, -{"strlower", str_lower}, -{"strupper", str_upper}, -{"strchar", str_char}, -{"strrep", str_rep}, -{"strbyte", str_byte}, -{"concat", str_concat}, -{"format", str_format}, -{"strfind", str_find}, -{"gfind", gfind}, -{"gsub", str_gsub} + {"len", str_len}, + {"sub", str_sub}, + {"lower", str_lower}, + {"upper", str_upper}, + {"char", str_char}, + {"rep", str_rep}, + {"byte", str_byte}, + {"concat", str_concat}, + {"format", str_format}, + {"find", str_find}, + {"gfind", gfind}, + {"gsub", str_gsub}, + {NULL, NULL} }; @@ -726,6 +727,7 @@ static const luaL_reg strlib[] = { ** Open string library */ LUALIB_API int lua_strlibopen (lua_State *L) { - luaL_openl(L, strlib); + luaL_opennamedlib(L, "str", strlib); return 0; } + diff --git a/ltests.c b/ltests.c index 6dbc2bb1..72487ad8 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 1.111 2002/03/04 21:29:41 roberto Exp roberto $ +** $Id: ltests.c,v 1.112 2002/03/14 18:01:52 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -666,7 +666,8 @@ static const struct luaL_reg tests_funcs[] = { {"closestate", closestate}, {"doremote", doremote}, {"log2", log2_aux}, - {"totalmem", mem_query} + {"totalmem", mem_query}, + {NULL, NULL} }; @@ -681,14 +682,7 @@ static void fim (void) { void luaB_opentests (lua_State *L) { *cast(int **, L) = &islocked; /* init lock */ lua_state = L; /* keep first state to be opened */ - /* open lib in a new table */ - lua_newtable(L); - lua_getglobals(L); - lua_pushvalue(L, -2); - lua_setglobals(L); - luaL_openl(L, tests_funcs); /* open functions inside new table */ - lua_setglobals(L); /* restore old table of globals */ - lua_setglobal(L, "T"); /* set new table as global T */ + luaL_opennamedlib(L, "T", tests_funcs); atexit(fim); }