summaryrefslogtreecommitdiff
path: root/lib/lua/src/LuaLib
diff options
context:
space:
mode:
authorpixel <pixel>2008-02-17 00:35:20 +0000
committerpixel <pixel>2008-02-17 00:35:20 +0000
commit658b29261ca0a68f1c5f7f3c3efa20ab56a543b6 (patch)
treee3caffa9a9ed1d80e1f8e23d8e848176d5f08413 /lib/lua/src/LuaLib
parent546a2ad1f72410fd042625cd44d192804e3ea2d7 (diff)
Upgrading to Lua 5.1.3
Diffstat (limited to 'lib/lua/src/LuaLib')
-rw-r--r--lib/lua/src/LuaLib/lauxlib.c13
-rw-r--r--lib/lua/src/LuaLib/lbaselib.c78
-rw-r--r--lib/lua/src/LuaLib/ldblib.c30
-rw-r--r--lib/lua/src/LuaLib/liolib.c69
-rw-r--r--lib/lua/src/LuaLib/loslib.c3
-rw-r--r--lib/lua/src/LuaLib/lstrlib.c10
-rw-r--r--lib/lua/src/LuaLib/ltablib.c5
7 files changed, 118 insertions, 90 deletions
diff --git a/lib/lua/src/LuaLib/lauxlib.c b/lib/lua/src/LuaLib/lauxlib.c
index 9df860c..9807702 100644
--- a/lib/lua/src/LuaLib/lauxlib.c
+++ b/lib/lua/src/LuaLib/lauxlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.c,v 1.5 2007-07-27 10:05:55 pixel Exp $
+** $Id: lauxlib.c,v 1.6 2008-02-17 00:35:21 pixel Exp $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -244,7 +244,7 @@ LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
if (libname) {
int size = libsize(l);
/* check whether lib already exists */
- luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", size);
+ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
lua_getfield(L, -1, libname); /* get _LOADED[libname] */
if (!lua_istable(L, -1)) { /* not found? */
lua_pop(L, 1); /* remove previous result */
@@ -535,7 +535,7 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
return "\n";
}
if (feof(lf->f)) return NULL;
- *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
+ *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
return (*size > 0) ? lf->buff : NULL;
}
@@ -570,9 +570,8 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
if (c == '\n') c = getc(lf.f);
}
- if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */
- fclose(lf.f);
- lf.f = fopen(filename, "rb"); /* reopen in binary mode */
+ if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
+ lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
/* skip eventual `#!...' */
while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
@@ -581,7 +580,7 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
ungetc(c, lf.f);
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
readstatus = ferror(lf.f);
- if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
+ if (filename) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) {
lua_settop(L, fnameindex); /* ignore results from `lua_load' */
return errfile(L, "read", fnameindex);
diff --git a/lib/lua/src/LuaLib/lbaselib.c b/lib/lua/src/LuaLib/lbaselib.c
index 846f26f..7159a68 100644
--- a/lib/lua/src/LuaLib/lbaselib.c
+++ b/lib/lua/src/LuaLib/lbaselib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lbaselib.c,v 1.8 2007-07-27 10:05:55 pixel Exp $
+** $Id: lbaselib.c,v 1.9 2008-02-17 00:35:21 pixel Exp $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -477,15 +477,52 @@ static const luaL_Reg base_funcs[] = {
** =======================================================
*/
+#define CO_RUN 0 /* running */
+#define CO_SUS 1 /* suspended */
+#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
+#define CO_DEAD 3
+
+static const char *const statnames[] =
+ {"running", "suspended", "normal", "dead"};
+
+static int costatus (lua_State *L, lua_State *co) {
+ if (L == co) return CO_RUN;
+ switch (lua_status(co)) {
+ case LUA_YIELD:
+ return CO_SUS;
+ case 0: {
+ lua_Debug ar;
+ if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
+ return CO_NOR; /* it is running */
+ else if (lua_gettop(co) == 0)
+ return CO_DEAD;
+ else
+ return CO_SUS; /* initial state */
+ }
+ default: /* some error occured */
+ return CO_DEAD;
+ }
+}
+
+
+static int luaB_costatus (lua_State *L) {
+ lua_State *co = lua_tothread(L, 1);
+ luaL_argcheck(L, co, 1, "coroutine expected");
+ lua_pushstring(L, statnames[costatus(L, co)]);
+ return 1;
+}
+
+
static int auxresume (lua_State *L, lua_State *co, int narg) {
- int status;
+ int status = costatus(L, co);
if (!lua_checkstack(co, narg))
luaL_error(L, "too many arguments to resume");
- if (lua_status(co) == 0 && lua_gettop(co) == 0) {
- lua_pushliteral(L, "cannot resume dead coroutine");
+ if (status != CO_SUS) {
+ lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
return -1; /* error flag */
}
lua_xmove(L, co, narg);
+ lua_setlevel(L, co);
status = lua_resume(co, narg);
if (status == 0 || status == LUA_YIELD) {
int nres = lua_gettop(co);
@@ -556,39 +593,10 @@ static int luaB_yield (lua_State *L) {
}
-static int luaB_costatus (lua_State *L) {
- lua_State *co = lua_tothread(L, 1);
- luaL_argcheck(L, co, 1, "coroutine expected");
- if (L == co) lua_pushliteral(L, "running");
- else {
- switch (lua_status(co)) {
- case LUA_YIELD:
- lua_pushliteral(L, "suspended");
- break;
- case 0: {
- lua_Debug ar;
- if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
- lua_pushliteral(L, "normal"); /* it is running */
- else if (lua_gettop(co) == 0)
- lua_pushliteral(L, "dead");
- else
- lua_pushliteral(L, "suspended"); /* initial state */
- break;
- }
- default: /* some error occured */
- lua_pushliteral(L, "dead");
- break;
- }
- }
- return 1;
-}
-
-
static int luaB_corunning (lua_State *L) {
if (lua_pushthread(L))
- return 0; /* main thread is not a coroutine */
- else
- return 1;
+ lua_pushnil(L); /* main thread is not a coroutine */
+ return 1;
}
diff --git a/lib/lua/src/LuaLib/ldblib.c b/lib/lua/src/LuaLib/ldblib.c
index 076ddac..473fd7d 100644
--- a/lib/lua/src/LuaLib/ldblib.c
+++ b/lib/lua/src/LuaLib/ldblib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldblib.c,v 1.5 2007-07-27 10:05:55 pixel Exp $
+** $Id: ldblib.c,v 1.6 2008-02-17 00:35:21 pixel Exp $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
@@ -255,24 +255,25 @@ static void gethooktable (lua_State *L) {
static int db_sethook (lua_State *L) {
- int arg;
+ int arg, mask, count;
+ lua_Hook func;
lua_State *L1 = getthread(L, &arg);
if (lua_isnoneornil(L, arg+1)) {
lua_settop(L, arg+1);
- lua_sethook(L1, NULL, 0, 0); /* turn off hooks */
+ func = NULL; mask = 0; count = 0; /* turn off hooks */
}
else {
const char *smask = luaL_checkstring(L, arg+2);
- int count = luaL_optint(L, arg+3, 0);
luaL_checktype(L, arg+1, LUA_TFUNCTION);
- lua_sethook(L1, hookf, makemask(smask, count), count);
+ count = luaL_optint(L, arg+3, 0);
+ func = hookf; mask = makemask(smask, count);
}
- gethooktable(L1);
- lua_pushlightuserdata(L1, L1);
+ gethooktable(L);
+ lua_pushlightuserdata(L, L1);
lua_pushvalue(L, arg+1);
- lua_xmove(L, L1, 1);
- lua_rawset(L1, -3); /* set new hook */
- lua_pop(L1, 1); /* remove hook table */
+ lua_rawset(L, -3); /* set new hook */
+ lua_pop(L, 1); /* remove hook table */
+ lua_sethook(L1, func, mask, count); /* set hooks */
return 0;
}
@@ -286,11 +287,10 @@ static int db_gethook (lua_State *L) {
if (hook != NULL && hook != hookf) /* external hook? */
lua_pushliteral(L, "external hook");
else {
- gethooktable(L1);
- lua_pushlightuserdata(L1, L1);
- lua_rawget(L1, -2); /* get hook */
- lua_remove(L1, -2); /* remove hook table */
- lua_xmove(L1, L, 1);
+ gethooktable(L);
+ lua_pushlightuserdata(L, L1);
+ lua_rawget(L, -2); /* get hook */
+ lua_remove(L, -2); /* remove hook table */
}
lua_pushstring(L, unmakemask(mask, buff));
lua_pushinteger(L, lua_gethookcount(L1));
diff --git a/lib/lua/src/LuaLib/liolib.c b/lib/lua/src/LuaLib/liolib.c
index f9866c3..edd42f5 100644
--- a/lib/lua/src/LuaLib/liolib.c
+++ b/lib/lua/src/LuaLib/liolib.c
@@ -1,5 +1,5 @@
/*
-** $Id: liolib.c,v 1.7 2007-07-27 10:05:55 pixel Exp $
+** $Id: liolib.c,v 1.8 2008-02-17 00:35:21 pixel Exp $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@@ -51,7 +51,7 @@ static void fileerror (lua_State *L, int arg, const char *filename) {
}
-#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
+#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
static int io_type (lua_State *L) {
@@ -70,7 +70,7 @@ static int io_type (lua_State *L) {
static FILE *tofile (lua_State *L) {
- FILE **f = topfile(L);
+ FILE **f = tofilep(L);
if (*f == NULL)
luaL_error(L, "attempt to use a closed file");
return *f;
@@ -93,19 +93,31 @@ static FILE **newfile (lua_State *L) {
/*
-** this function has a separated environment, which defines the
-** correct __close for 'popen' files
+** function to (not) close the standard files stdin, stdout, and stderr
+*/
+static int io_noclose (lua_State *L) {
+ lua_pushnil(L);
+ lua_pushliteral(L, "cannot close standard file");
+ return 2;
+}
+
+
+/*
+** function to close 'popen' files
*/
static int io_pclose (lua_State *L) {
- FILE **p = topfile(L);
+ FILE **p = tofilep(L);
int ok = lua_pclose(L, *p);
*p = NULL;
return pushresult(L, ok, NULL);
}
+/*
+** function to close regular files
+*/
static int io_fclose (lua_State *L) {
- FILE **p = topfile(L);
+ FILE **p = tofilep(L);
int ok = (fclose(*p) == 0);
*p = NULL;
return pushresult(L, ok, NULL);
@@ -128,18 +140,18 @@ static int io_close (lua_State *L) {
static int io_gc (lua_State *L) {
- FILE *f = *topfile(L);
- /* ignore closed files and standard files */
- if (f != NULL && f != stdin && f != stdout && f != stderr)
+ FILE *f = *tofilep(L);
+ /* ignore closed files */
+ if (f != NULL)
aux_close(L);
return 0;
}
static int io_tostring (lua_State *L) {
- FILE *f = *topfile(L);
+ FILE *f = *tofilep(L);
if (f == NULL)
- lua_pushstring(L, "file (closed)");
+ lua_pushliteral(L, "file (closed)");
else
lua_pushfstring(L, "file (%p)", f);
return 1;
@@ -155,6 +167,10 @@ static int io_open (lua_State *L) {
}
+/*
+** this function has a separated environment, which defines the
+** correct __close for 'popen' files
+*/
static int io_popen (lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
@@ -280,7 +296,7 @@ static int read_line (lua_State *L, FILE *f) {
char *p = luaL_prepbuffer(&b);
if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
luaL_pushresult(&b); /* close buffer */
- return (lua_strlen(L, -1) > 0); /* check whether read something */
+ return (lua_objlen(L, -1) > 0); /* check whether read something */
}
l = strlen(p);
if (l == 0 || p[l-1] != '\n')
@@ -308,7 +324,7 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
n -= nr; /* still have to read `n' chars */
} while (n > 0 && nr == rlen); /* until end of count or eof */
luaL_pushresult(&b); /* close buffer */
- return (n == 0 || lua_strlen(L, -1) > 0);
+ return (n == 0 || lua_objlen(L, -1) > 0);
}
@@ -502,31 +518,36 @@ static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
lua_pushvalue(L, -1);
lua_rawseti(L, LUA_ENVIRONINDEX, k);
}
- lua_setfield(L, -2, fname);
+ lua_pushvalue(L, -2); /* copy environment */
+ lua_setfenv(L, -2); /* set it */
+ lua_setfield(L, -3, fname);
+}
+
+
+static void newfenv (lua_State *L, lua_CFunction cls) {
+ lua_createtable(L, 0, 1);
+ lua_pushcfunction(L, cls);
+ lua_setfield(L, -2, "__close");
}
LUALIB_API int luaopen_io (lua_State *L) {
createmeta(L);
/* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
- lua_createtable(L, 2, 1);
+ newfenv(L, io_fclose);
lua_replace(L, LUA_ENVIRONINDEX);
/* open library */
luaL_register(L, LUA_IOLIBNAME, iolib);
/* create (and set) default files */
+ newfenv(L, io_noclose); /* close function for default files */
createstdfile(L, stdin, IO_INPUT, "stdin");
createstdfile(L, stdout, IO_OUTPUT, "stdout");
createstdfile(L, stderr, 0, "stderr");
- /* create environment for 'popen' */
+ lua_pop(L, 1); /* pop environment for default files */
lua_getfield(L, -1, "popen");
- lua_createtable(L, 0, 1);
- lua_pushcfunction(L, io_pclose);
- lua_setfield(L, -2, "__close");
- lua_setfenv(L, -2);
+ newfenv(L, io_pclose); /* create environment for 'popen' */
+ lua_setfenv(L, -2); /* set fenv for 'popen' */
lua_pop(L, 1); /* pop 'popen' */
- /* set default close function */
- lua_pushcfunction(L, io_fclose);
- lua_setfield(L, LUA_ENVIRONINDEX, "__close");
return 1;
}
diff --git a/lib/lua/src/LuaLib/loslib.c b/lib/lua/src/LuaLib/loslib.c
index 11d665e..4592be1 100644
--- a/lib/lua/src/LuaLib/loslib.c
+++ b/lib/lua/src/LuaLib/loslib.c
@@ -1,5 +1,5 @@
/*
-** $Id: loslib.c,v 1.1 2007-07-27 15:47:33 pixel Exp $
+** $Id: loslib.c,v 1.2 2008-02-17 00:35:21 pixel Exp $
** Standard Operating System library
** See Copyright Notice in lua.h
*/
@@ -215,7 +215,6 @@ static int os_setlocale (lua_State *L) {
static int os_exit (lua_State *L) {
exit(luaL_optint(L, 1, EXIT_SUCCESS));
- return 0; /* to avoid warnings */
}
static const luaL_Reg syslib[] = {
diff --git a/lib/lua/src/LuaLib/lstrlib.c b/lib/lua/src/LuaLib/lstrlib.c
index 8310752..7198464 100644
--- a/lib/lua/src/LuaLib/lstrlib.c
+++ b/lib/lua/src/LuaLib/lstrlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstrlib.c,v 1.5 2007-07-27 10:05:55 pixel Exp $
+** $Id: lstrlib.c,v 1.6 2008-02-17 00:35:21 pixel Exp $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@@ -629,10 +629,6 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
lua_gettable(L, 3);
break;
}
- default: {
- luaL_argerror(L, 3, "string/function/table expected");
- return;
- }
}
if (!lua_toboolean(L, -1)) { /* nil or false? */
lua_pop(L, 1);
@@ -648,11 +644,15 @@ static int str_gsub (lua_State *L) {
size_t srcl;
const char *src = luaL_checklstring(L, 1, &srcl);
const char *p = luaL_checkstring(L, 2);
+ int tr = lua_type(L, 3);
int max_s = luaL_optint(L, 4, srcl+1);
int anchor = (*p == '^') ? (p++, 1) : 0;
int n = 0;
MatchState ms;
luaL_Buffer b;
+ luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
+ tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
+ "string/function/table expected");
luaL_buffinit(L, &b);
ms.L = L;
ms.src_init = src;
diff --git a/lib/lua/src/LuaLib/ltablib.c b/lib/lua/src/LuaLib/ltablib.c
index 6bd11fb..cc6be68 100644
--- a/lib/lua/src/LuaLib/ltablib.c
+++ b/lib/lua/src/LuaLib/ltablib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltablib.c,v 1.5 2007-07-27 10:05:55 pixel Exp $
+** $Id: ltablib.c,v 1.6 2008-02-17 00:35:21 pixel Exp $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@@ -118,7 +118,8 @@ static int tinsert (lua_State *L) {
static int tremove (lua_State *L) {
int e = aux_getn(L, 1);
int pos = luaL_optint(L, 2, e);
- if (e == 0) return 0; /* table is `empty' */
+ if (!(1 <= pos && pos <= e)) /* position is outside bounds? */
+ return 0; /* nothing to remove */
luaL_setn(L, 1, e - 1); /* t.n = n-1 */
lua_rawgeti(L, 1, pos); /* result = t[pos] */
for ( ;pos<e; pos++) {