diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 208 | ||||
-rw-r--r-- | src/README | 5 | ||||
-rw-r--r-- | src/lapi.c | 8 | ||||
-rw-r--r-- | src/lauxlib.c (renamed from src/lib/lauxlib.c) | 25 | ||||
-rw-r--r-- | src/lauxlib.h | 145 | ||||
-rw-r--r-- | src/lbaselib.c (renamed from src/lib/lbaselib.c) | 20 | ||||
-rw-r--r-- | src/ldblib.c (renamed from src/lib/ldblib.c) | 0 | ||||
-rw-r--r-- | src/ldebug.c | 4 | ||||
-rw-r--r-- | src/lgc.c | 10 | ||||
-rw-r--r-- | src/lib/Makefile | 27 | ||||
-rw-r--r-- | src/lib/README | 8 | ||||
-rw-r--r-- | src/lib/loadlib.c | 429 | ||||
-rw-r--r-- | src/linit.c (renamed from src/lib/linit.c) | 0 | ||||
-rw-r--r-- | src/liolib.c (renamed from src/lib/liolib.c) | 0 | ||||
-rw-r--r-- | src/llimits.h | 4 | ||||
-rw-r--r-- | src/lmathlib.c (renamed from src/lib/lmathlib.c) | 0 | ||||
-rw-r--r-- | src/loadlib.c | 464 | ||||
-rw-r--r-- | src/loslib.c (renamed from src/lib/loslib.c) | 0 | ||||
-rw-r--r-- | src/lparser.c | 37 | ||||
-rw-r--r-- | src/lstate.c | 4 | ||||
-rw-r--r-- | src/lstate.h | 4 | ||||
-rw-r--r-- | src/lstrlib.c (renamed from src/lib/lstrlib.c) | 0 | ||||
-rw-r--r-- | src/ltablib.c (renamed from src/lib/ltablib.c) | 30 | ||||
-rw-r--r-- | src/lua.c (renamed from src/lua/lua.c) | 0 | ||||
-rw-r--r-- | src/lua.h | 383 | ||||
-rw-r--r-- | src/lua/Makefile | 31 | ||||
-rw-r--r-- | src/lua/README | 41 | ||||
-rw-r--r-- | src/luac.c (renamed from src/luac/luac.c) | 0 | ||||
-rw-r--r-- | src/luac/Makefile | 31 | ||||
-rw-r--r-- | src/luac/README | 18 | ||||
-rw-r--r-- | src/luaconf.h | 386 | ||||
-rw-r--r-- | src/lualib.h | 47 | ||||
-rw-r--r-- | src/print.c (renamed from src/luac/print.c) | 0 |
33 files changed, 1628 insertions, 741 deletions
diff --git a/src/Makefile b/src/Makefile index 7812b918..cf7ff181 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,83 +1,135 @@ -# makefile for Lua core library - -LUA= .. - -include $(LUA)/config - -OBJS= \ - lapi.o \ - lcode.o \ - ldebug.o \ - ldo.o \ - ldump.o \ - lfunc.o \ - lgc.o \ - llex.o \ - lmem.o \ - lobject.o \ - lopcodes.o \ - lparser.o \ - lstate.o \ - lstring.o \ - ltable.o \ - ltm.o \ - lundump.o \ - lvm.o \ - lzio.o - -SRCS= \ - lapi.c \ - lcode.c \ - ldebug.c \ - ldo.c \ - ldump.c \ - lfunc.c \ - lgc.c \ - llex.c \ - lmem.c \ - lobject.c \ - lopcodes.c \ - lparser.c \ - lstate.c \ - lstring.c \ - ltable.c \ - ltm.c \ - lundump.c \ - lvm.c \ - lzio.c \ - lapi.h \ - lcode.h \ - ldebug.h \ - ldo.h \ - lfunc.h \ - lgc.h \ - llex.h \ - llimits.h \ - lmem.h \ - lobject.h \ - lopcodes.h \ - lparser.h \ - lstate.h \ - lstring.h \ - ltable.h \ - ltm.h \ - lundump.h \ - lvm.h \ - lzio.h - -T= $(LIB)/liblua.a - -all: $T - -$T: $(OBJS) - $(AR) $@ $(OBJS) +# makefile for building Lua +# see INSTALL for installation instructions +# see ../Makefile and luaconf.h for further customization + +# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= + +CC= gcc +CFLAGS= -O2 -Wall $(MYCFLAGS) +MYCFLAGS= +MYLDFLAGS= -Wl,-E # enable dynamic loading in Linux +MYLIBS= -lm -ldl # enable dynamic loading in Linux +AR= ar rcu +RANLIB= ranlib +RM= rm -f + +# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= + +CORE_T= liblua.a +CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \ + lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \ + lundump.o lvm.o lzio.o + +LIB_T= liblualib.a +LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \ + lstrlib.o loadlib.o linit.o + +LUA_T= lua +LUA_O= lua.o + +LUAC_T= luac +LUAC_O= luac.o print.o lauxlib.o + +ALL_T= $(CORE_T) $(LIB_T) $(LUA_T) $(LUAC_T) +ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O) +ALL_A= $(CORE_T) $(LIB_T) + +all: $(ALL_T) + +o: $(ALL_O) + +a: $(ALL_A) + +$(CORE_T): $(CORE_O) + $(AR) $@ $? $(RANLIB) $@ +$(LIB_T): $(LIB_O) + $(AR) $@ $? + $(RANLIB) $@ + +$(LUA_T): $(LUA_O) $(CORE_T) $(LIB_T) + $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) -L. -llua -llualib $(MYLIBS) + +$(LUAC_T): $(LUAC_O) $(CORE_T) + $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) -L. -llua + clean: - rm -f $(OBJS) $T + $(RM) $(ALL_T) $(ALL_O) + +depend: + @$(CC) $(CFLAGS) -MM *.c + +echo: + @echo "CC = $(CC)" + @echo "CFLAGS = $(CFLAGS)" + @echo "MYCFLAGS = $(MYCFLAGS)" + @echo "MYLDFLAGS = $(MYLDFLAGS)" + @echo "MYLIBS = $(MYLIBS)" + @echo "AR = $(AR)" + @echo "RANLIB = $(RANLIB)" + @echo "RM = $(RM)" + +# DO NOT DELETE -co: - co -q -f -M $(SRCS) +lapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \ + lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \ + lundump.h lvm.h +lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h +lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h +lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ + lzio.h lmem.h lopcodes.h lparser.h ltable.h ldebug.h lstate.h ltm.h \ + ldo.h lgc.h +ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h +ldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \ + llex.h lzio.h lmem.h lopcodes.h lparser.h ltable.h ldebug.h lstate.h \ + ltm.h ldo.h lfunc.h lstring.h lgc.h lvm.h +ldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ + lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h ltable.h \ + lstring.h lundump.h lvm.h +ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lopcodes.h lstate.h \ + ltm.h lzio.h lmem.h lundump.h +lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \ + lstate.h ltm.h lzio.h +lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ + lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h +linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h +liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h +llex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \ + lzio.h lmem.h llex.h lparser.h ltable.h lstring.h lgc.h +lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h +lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ + ltm.h lzio.h lmem.h ldo.h +loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h +lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \ + ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h +lopcodes.o: lopcodes.c lua.h luaconf.h lobject.h llimits.h lopcodes.h +loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h +lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ + lzio.h lmem.h lopcodes.h lparser.h ltable.h ldebug.h lstate.h ltm.h \ + ldo.h lfunc.h lstring.h lgc.h +lstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ + ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h +lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ + ltm.h lzio.h lstring.h lgc.h +lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h +ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ + ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h +ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h +ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ + lmem.h lstring.h lgc.h ltable.h +lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h +luac.o: luac.c lua.h luaconf.h lauxlib.h ldo.h lobject.h llimits.h \ + lstate.h ltm.h lzio.h lmem.h lfunc.h lopcodes.h lstring.h lgc.h \ + lundump.h +lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lopcodes.h lstring.h lgc.h \ + lundump.h +lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ + lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h +lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ + lzio.h +print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \ + ltm.h lzio.h lmem.h lopcodes.h lundump.h -klean: clean - rm -f $(SRCS) +# (end of Makefile) diff --git a/src/README b/src/README deleted file mode 100644 index 915f30b4..00000000 --- a/src/README +++ /dev/null @@ -1,5 +0,0 @@ -This is the Lua core. - -The standard Lua library are in lib/. -The standalone interpreter is in lua/. -The standalone compiler is in luac/. @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.22 2004/12/06 17:53:42 roberto Exp $ +** $Id: lapi.c,v 2.23 2004/12/13 12:15:11 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -867,9 +867,9 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { luaC_step(L); break; } - case LUA_GCSETSTEPMUL: { - res = g->stepmul; - g->stepmul = data; + case LUA_GCSETPACE: { + res = g->gcpace; + g->gcpace = data; break; } case LUA_GCSETINCMODE: { diff --git a/src/lib/lauxlib.c b/src/lauxlib.c index 615be8f2..5bbe918b 100644 --- a/src/lib/lauxlib.c +++ b/src/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.126 2004/09/29 21:03:14 roberto Exp $ +** $Id: lauxlib.c,v 1.127 2004/12/20 13:47:29 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -375,9 +375,19 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, } +static int readable (const char *fname) { + int err; + FILE *f = fopen(fname, "r"); /* try to open file */ + if (f == NULL) return 0; /* open failed */ + getc(f); /* try to read it */ + err = ferror(f); + fclose(f); + return (err == 0); +} + + LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, const char *path) { - FILE *f; const char *p = path; for (;;) { const char *fname; @@ -387,15 +397,8 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, } fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); lua_remove(L, -2); /* remove path template */ - f = fopen(fname, "r"); /* try to open it */ - if (f) { - int err; - getc(f); /* try to read file */ - err = ferror(f); - fclose(f); - if (err == 0) /* open and read sucessful? */ - return fname; /* return that file name */ - } + if (readable(fname)) /* does file exist and is readable? */ + return fname; /* return that file name */ lua_pop(L, 1); /* remove file name */ } } diff --git a/src/lauxlib.h b/src/lauxlib.h new file mode 100644 index 00000000..b47663ce --- /dev/null +++ b/src/lauxlib.h @@ -0,0 +1,145 @@ +/* +** $Id: lauxlib.h,v 1.73 2004/10/18 12:51:44 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include <stddef.h> +#include <stdio.h> + +#include "lua.h" + + +/* extra error code for `luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + + +typedef struct luaL_reg { + const char *name; + lua_CFunction func; +} luaL_reg; + + +LUALIB_API void luaL_openlib (lua_State *L, const char *libname, + const luaL_reg *l, int nup); +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e); +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e); +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname); +LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg); +LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l); +LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg, + const char *def, size_t *l); +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg); +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def); + +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int numArg); +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int nArg, + lua_Integer def); + +LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg); +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t); +LUALIB_API void luaL_checkany (lua_State *L, int narg); + +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname); +LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname); +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname); + +LUALIB_API void luaL_where (lua_State *L, int lvl); +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); + +LUALIB_API int luaL_findstring (const char *st, const char *const lst[]); + +LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, + const char *path); + +LUALIB_API int luaL_ref (lua_State *L, int t); +LUALIB_API void luaL_unref (lua_State *L, int t, int ref); + +LUALIB_API int luaL_getn (lua_State *L, int t); +LUALIB_API void luaL_setn (lua_State *L, int t, int n); + +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename); +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, + const char *name); + +LUALIB_API lua_State *(luaL_newstate) (void); + + +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, + const char *r); +LUALIB_API const char *luaL_getfield (lua_State *L, int idx, const char *fname); +LUALIB_API const char *luaL_setfield (lua_State *L, int idx, const char *fname); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define luaL_argcheck(L, cond,numarg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + + +typedef struct luaL_Buffer { + char *p; /* current position in buffer */ + int lvl; /* number of strings in the stack (level) */ + lua_State *L; + char buffer[LUAL_BUFFERSIZE]; +} luaL_Buffer; + +#define luaL_putchar(B,c) \ + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ + (*(B)->p++ = (char)(c))) + +#define luaL_addsize(B,n) ((B)->p += (n)) + +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B); +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B); +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s); +LUALIB_API void luaL_addvalue (luaL_Buffer *B); +LUALIB_API void luaL_pushresult (luaL_Buffer *B); + + +/* }====================================================== */ + + +/* compatibility with ref system */ + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) + +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) + +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) + + +#endif + + diff --git a/src/lib/lbaselib.c b/src/lbaselib.c index 8c628097..3b034bbc 100644 --- a/src/lib/lbaselib.c +++ b/src/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.161 2004/12/06 17:53:42 roberto Exp $ +** $Id: lbaselib.c,v 1.163 2004/12/13 12:15:11 roberto Exp $ ** Basic library ** See Copyright Notice in lua.h */ @@ -182,9 +182,9 @@ static int luaB_gcinfo (lua_State *L) { static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "setstepmul", "setincmode", NULL}; + "count", "step", "setpace", "setincmode", NULL}; static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETSTEPMUL, LUA_GCSETINCMODE}; + LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPACE, LUA_GCSETINCMODE}; int o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts); int ex = luaL_optint(L, 2, 0); luaL_argcheck(L, o >= 0, 1, "invalid option"); @@ -396,18 +396,8 @@ static int luaB_tostring (lua_State *L) { case LUA_TNIL: lua_pushliteral(L, "nil"); break; - case LUA_TTABLE: - lua_pushfstring(L, "table: %p", lua_topointer(L, 1)); - break; - case LUA_TFUNCTION: - lua_pushfstring(L, "function: %p", lua_topointer(L, 1)); - break; - case LUA_TUSERDATA: - case LUA_TLIGHTUSERDATA: - lua_pushfstring(L, "userdata: %p", lua_topointer(L, 1)); - break; - case LUA_TTHREAD: - lua_pushfstring(L, "thread: %p", lua_topointer(L, 1)); + default: + lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); break; } return 1; diff --git a/src/lib/ldblib.c b/src/ldblib.c index 36d647aa..36d647aa 100644 --- a/src/lib/ldblib.c +++ b/src/ldblib.c diff --git a/src/ldebug.c b/src/ldebug.c index 17727d37..0456669f 100644 --- a/src/ldebug.c +++ b/src/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.11 2004/12/03 20:35:33 roberto Exp $ +** $Id: ldebug.c,v 2.12 2004/12/20 15:50:00 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -477,7 +477,7 @@ static const char *getobjname (CallInfo *ci, int stackpos, const char **name) { } case OP_GETUPVAL: { int u = GETARG_B(i); /* upvalue index */ - *name = getstr(p->upvalues[u]); + *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; return "upvalue"; } case OP_SELF: { @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.18 2004/12/06 17:53:42 roberto Exp $ +** $Id: lgc.c,v 2.19 2004/12/13 12:15:11 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -27,6 +27,7 @@ #define GCSWEEPMAX 10 #define GCSWEEPCOST 30 #define GCFINALIZECOST 100 +#define GCSTEPMUL 8 #define FIXEDMASK bitmask(FIXEDBIT) @@ -621,18 +622,17 @@ static l_mem singlestep (lua_State *L) { void luaC_step (lua_State *L) { global_State *g = G(L); - l_mem lim = (g->totalbytes - (g->GCthreshold - GCSTEPSIZE)) * g->stepmul; + l_mem lim = (g->totalbytes - (g->GCthreshold - GCSTEPSIZE)) * GCSTEPMUL; do { lim -= singlestep(L); if (g->gcstate == GCSpause) break; } while (lim > 0 || !g->incgc); - if (g->incgc) + if (g->gcstate != GCSpause) g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/STEPMUL; */ else { lua_assert(g->totalbytes >= g->estimate); - lua_assert(g->gcstate == GCSpause); - g->GCthreshold = 2*g->estimate; + g->GCthreshold = g->estimate + ((g->estimate/GCDIV) * g->gcpace); } } diff --git a/src/lib/Makefile b/src/lib/Makefile deleted file mode 100644 index 74f65215..00000000 --- a/src/lib/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -# makefile for Lua standard library - -LUA= ../.. - -include $(LUA)/config - -EXTRA_DEFS= $(POPEN) $(TMPNAM) $(DEGREES) $(LOADLIB) - -OBJS= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o -SRCS= lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c ltablib.c lstrlib.c loadlib.c linit.c - -T= $(LIB)/liblualib.a - -all: $T - -$T: $(OBJS) - $(AR) $@ $(OBJS) - $(RANLIB) $@ - -clean: - rm -f $(OBJS) $T - -co: - co -q -f -M $(SRCS) - -klean: clean - rm -f $(SRCS) diff --git a/src/lib/README b/src/lib/README deleted file mode 100644 index c5600b8e..00000000 --- a/src/lib/README +++ /dev/null @@ -1,8 +0,0 @@ -This is the standard Lua library. - -The code of the standard library can be read as an example of how to export -C functions to Lua. The easiest library to read is lmathlib.c. - -The library is implemented entirely on top of the official Lua API as declared -in lua.h, using lauxlib.c, which contains several useful functions for writing -libraries. We encourage developers to use lauxlib.c in their own libraries. diff --git a/src/lib/loadlib.c b/src/lib/loadlib.c deleted file mode 100644 index 63bc0bcc..00000000 --- a/src/lib/loadlib.c +++ /dev/null @@ -1,429 +0,0 @@ -/* -** $Id: loadlib.c,v 1.11 2004/11/19 15:52:12 roberto Exp $ -** Dynamic library loader for Lua -** See Copyright Notice in lua.h -* -* This Lua library exports a single function, called loadlib, which -* is called from Lua as loadlib(lib,init), where lib is the full -* name of the library to be loaded (including the complete path) and -* init is the name of a function to be called after the library is -* loaded. Typically, this function will register other functions, thus -* making the complete library available to Lua. The init function is -* *not* automatically called by loadlib. Instead, loadlib returns the -* init function as a Lua function that the client can call when it -* thinks is appropriate. In the case of errors, loadlib returns nil and -* two strings describing the error. The first string is supplied by -* the operating system; it should be informative and useful for error -* messages. The second string is "open", "init", or "absent" to identify -* the error and is meant to be used for making decisions without having -* to look into the first string (whose format is system-dependent). -* This module contains an implementation of loadlib for Unix systems -* that have dlfcn, an implementation for Darwin (Mac OS X), an -* implementation for Windows, and a stub for other systems. See -* the list at the end of this file for some links to available -* implementations of dlfcn and interfaces to other native dynamic -* loaders on top of which loadlib could be implemented. -*/ - - -#include <stdlib.h> -#include <string.h> - - -#define loadlib_c -#define LUA_LIB - -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" - - -#define ERR_OPEN 1 -#define ERR_FUNCTION 2 - - -static void registerlib (lua_State *L, const void *lib); - - -#if defined(USE_DLOPEN) -/* -* This is an implementation of loadlib based on the dlfcn interface. -* The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, -* NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least -* as an emulation layer on top of native functions. -*/ - -#include <dlfcn.h> - -#define freelib dlclose - -static int loadlib(lua_State *L, const char *path, const char *init) -{ - void *lib=dlopen(path,RTLD_NOW); - if (lib!=NULL) - { - lua_CFunction f=(lua_CFunction) dlsym(lib,init); - if (f!=NULL) - { - registerlib(L, lib); - lua_pushcfunction(L,f); - return 0; - } - } - /* else return appropriate error message */ - lua_pushstring(L,dlerror()); - if (lib!=NULL) { - dlclose(lib); - return ERR_FUNCTION; /* error loading function */ - } - else return ERR_OPEN; /* error loading library */ -} - - - -#elif defined(USE_DLL) -/* -* This is an implementation of loadlib for Windows using native functions. -*/ - -#include <windows.h> - -#define freelib(lib) FreeLibrary((HINSTANCE)lib) - -static void pusherror(lua_State *L) -{ - int error=GetLastError(); - char buffer[128]; - if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, - 0, error, 0, buffer, sizeof(buffer), 0)) - lua_pushstring(L,buffer); - else - lua_pushfstring(L,"system error %d\n",error); -} - -static int loadlib(lua_State *L, const char *path, const char *init) -{ - HINSTANCE lib=LoadLibrary(path); - if (lib!=NULL) - { - lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init); - if (f!=NULL) - { - registerlib(L, lib); - lua_pushcfunction(L,f); - return 1; - } - } - pusherror(L); - if (lib!=NULL) { - FreeLibrary(lib); - return ERR_OPEN; - } - else return ERR_FUNCTION; -} - - - -/* Native Mac OS X / Darwin Implementation */ -#elif defined(USE_DYLD) - -#include <mach-o/dyld.h> - - -/* Mach cannot unload images (?) */ -#define freelib(lib) ((void)lib) - -static void pusherror (lua_State *L) -{ - const char *err_str; - const char *err_file; - NSLinkEditErrors err; - int err_num; - NSLinkEditError(&err, &err_num, &err_file, &err_str); - lua_pushstring(L, err_str); -} - -static int loadlib (lua_State *L, const char *path, const char *init) { - const struct mach_header *lib; - /* this would be a rare case, but prevents crashing if it happens */ - if(!_dyld_present()) { - lua_pushstring(L,"dyld not present."); - return ERR_OPEN; - } - lib = NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR); - if(lib != NULL) { - NSSymbol init_sym = NSLookupSymbolInImage(lib, init, - NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | - NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); - if(init_sym != NULL) { - lua_CFunction f = (lua_CFunction)NSAddressOfSymbol(init_sym); - registerlib(L, lib); - lua_pushcfunction(L,f); - return 0; - } - } - /* else an error ocurred */ - pusherror(L); - /* Can't unload image */ - return (lib != NULL) ? ERR_FUNCTION : ERR_OPEN; -} - - - -#else -/* Fallback for other systems */ - - -#define freelib(lib) ((void)lib) - -static int loadlib(lua_State *L) -{ - registerlib(L, NULL); /* to avoid warnings */ - lua_pushnil(L); - lua_pushliteral(L,"`loadlib' not supported"); - lua_pushliteral(L,"absent"); - return 3; -} - -#endif - - -/* -** common code for all implementations: put a library into the registry -** so that, when Lua closes its state, the library's __gc metamethod -** will be called to unload the library. -*/ -static void registerlib (lua_State *L, const void *lib) -{ - const void **plib = (const void **)lua_newuserdata(L, sizeof(const void *)); - *plib = lib; - luaL_getmetatable(L, "_LOADLIB"); - lua_setmetatable(L, -2); - lua_pushboolean(L, 1); - lua_settable(L, LUA_REGISTRYINDEX); /* registry[lib] = true */ -} - -/* -** __gc tag method: calls library's `freelib' function with the lib -** handle -*/ -static int gctm (lua_State *L) -{ - void *lib = *(void **)luaL_checkudata(L, 1, "_LOADLIB"); - freelib(lib); - return 0; -} - - -static int loadlib1 (lua_State *L) { - const char *path=luaL_checkstring(L,1); - const char *init=luaL_checkstring(L,2); - int res = loadlib(L,path,init); - if (res == 0) /* no error? */ - return 1; /* function is at stack top */ - else { /* error */ - lua_pushnil(L); - lua_insert(L,-2); /* insert nil before error message */ - lua_pushstring(L, (res==ERR_OPEN)?"open":"init"); - return 3; - } -} - - - -/* -** {====================================================== -** `require' and `module' functions -** ======================================================= -*/ - - -static const char *loadLua (lua_State *L, const char *fname, const char *name) { - const char *path; - /* try first `LUA_PATH' for compatibility */ - lua_getglobal(L, "LUA_PATH"); - path = lua_tostring(L, -1); - if (!path) { - lua_pop(L, 1); - luaL_getfield(L, LUA_GLOBALSINDEX, "package.path"); - path = lua_tostring(L, -1); - } - if (path == NULL) - luaL_error(L, "`package.path' must be a string"); - fname = luaL_searchpath(L, fname, path); - if (fname == NULL) return path; /* library not found in this path */ - if (luaL_loadfile(L, fname) != 0) - luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1)); - return NULL; /* library loaded successfully */ -} - - -static const char *loadC (lua_State *L, const char *fname, const char *name) { - const char *path; - const char *funcname; - luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath"); - path = lua_tostring(L, -1); - if (path == NULL) - luaL_error(L, "global _CPATH must be a string"); - fname = luaL_searchpath(L, fname, path); - if (fname == NULL) return path; /* library not found in this path */ - funcname = luaL_gsub(L, name, ".", ""); - funcname = lua_pushfstring(L, "%s%s", LUA_POF, funcname); - if (loadlib(L, fname, funcname) != 0) - luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1)); - return NULL; /* library loaded successfully */ -} - - -static int ll_require (lua_State *L) { - const char *name = luaL_checkstring(L, 1); - lua_settop(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, 2, name); - if (lua_toboolean(L, -1)) /* is it there? */ - return 1; /* package is already loaded; return its result */ - /* else must load it; first mark it as loaded */ - lua_pushboolean(L, 1); - lua_setfield(L, 2, name); /* _LOADED[name] = true */ - /* check whether it is preloaded */ - lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); - lua_getfield(L, -1, name); - if (lua_isnil(L, -1)) { /* no preload function for that module? */ - const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP); - const char *cpath = loadC(L, fname, name); /* try a C module */ - if (cpath) { /* not found? */ - const char *path = loadLua(L, fname, name); /* try a Lua module */ - if (path) { /* yet not found? */ - lua_pushnil(L); - lua_setfield(L, 2, name); /* unmark _LOADED[name] */ - return luaL_error(L, "package `%s' not found\n" - " cpath: %s\n path: %s", - name, cpath, path); - } - } - } - lua_pushvalue(L, 1); /* pass name as argument to module */ - lua_call(L, 1, 1); /* run loaded module */ - if (!lua_isnil(L, -1)) /* non-nil return? */ - lua_setfield(L, 2, name); /* update _LOADED[name] with returned value */ - lua_getfield(L, 2, name); /* return _LOADED[name] */ - return 1; -} - - -static void setfenv (lua_State *L) { - lua_Debug ar; - lua_getstack(L, 1, &ar); - lua_getinfo(L, "f", &ar); - lua_pushvalue(L, -2); - lua_setfenv(L, -2); -} - - -static int ll_module (lua_State *L) { - const char *modname = luaL_checkstring(L, 1); - const char *dot; - lua_settop(L, 1); - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - /* try to find given table */ - luaL_getfield(L, LUA_GLOBALSINDEX, modname); - if (lua_isnil(L, -1)) { /* not found? */ - lua_pop(L, 1); /* remove previous result */ - lua_newtable(L); /* create it */ - /* register it with given name */ - lua_pushvalue(L, -1); - luaL_setfield(L, LUA_GLOBALSINDEX, modname); - } - else if (!lua_istable(L, -1)) - return luaL_error(L, "name conflict for module `%s'", modname); - /* check whether table already has a _NAME field */ - lua_getfield(L, -1, "_NAME"); - if (!lua_isnil(L, -1)) /* is table an initialized module? */ - lua_pop(L, 1); - else { /* no; initialize it */ - lua_pop(L, 1); - lua_newtable(L); /* create new metatable */ - lua_pushvalue(L, LUA_GLOBALSINDEX); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - lua_setmetatable(L, -2); - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; - else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, dot - modname); - lua_setfield(L, -2, "_PACKAGE"); - } - lua_pushvalue(L, -1); - lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ - setfenv(L); - return 0; -} - - -/* }====================================================== */ - - -static const luaL_reg ll_funcs[] = { - {"loadlib", loadlib1}, - {"require", ll_require}, - {"module", ll_module}, - {NULL, NULL} -}; - - - -LUALIB_API int luaopen_loadlib (lua_State *L) { - const char *path; - /* create new type _LOADLIB */ - luaL_newmetatable(L, "_LOADLIB"); - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); - /* create `package' table */ - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setglobal(L, "package"); - /* set field `path' */ - path = getenv(LUA_PATH); - if (path == NULL) path = LUA_PATH_DEFAULT; - lua_pushstring(L, path); - lua_setfield(L, -2, "path"); - /* set field `cpath' */ - path = getenv(LUA_CPATH); - if (path == NULL) path = LUA_CPATH_DEFAULT; - lua_pushstring(L, path); - lua_setfield(L, -2, "cpath"); - /* set field `loaded' */ - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_setfield(L, -2, "loaded"); - /* set field `preload' */ - lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); - lua_setfield(L, -2, "preload"); - lua_pushvalue(L, LUA_GLOBALSINDEX); - luaL_openlib(L, NULL, ll_funcs, 0); /* open lib into global table */ - return 1; -} - -/* -* Here are some links to available implementations of dlfcn and -* interfaces to other native dynamic loaders on top of which loadlib -* could be implemented. Please send contributions and corrections to us. -* -* AIX -* Starting with AIX 4.2, dlfcn is included in the base OS. -* There is also an emulation package available. -* http://www.faqs.org/faqs/aix-faq/part4/section-21.html -* -* HPUX -* HPUX 11 has dlfcn. For HPUX 10 use shl_*. -* http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html -* http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html -* -* Macintosh, Windows -* http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html -* -* GLIB has wrapper code for BeOS, OS2, Unix and Windows -* http://cvs.gnome.org/lxr/source/glib/gmodule/ -* -*/ diff --git a/src/lib/linit.c b/src/linit.c index 0d5f9cd4..0d5f9cd4 100644 --- a/src/lib/linit.c +++ b/src/linit.c diff --git a/src/lib/liolib.c b/src/liolib.c index ca9b8025..ca9b8025 100644 --- a/src/lib/liolib.c +++ b/src/liolib.c diff --git a/src/llimits.h b/src/llimits.h index 6be658f0..c3d5d532 100644 --- a/src/llimits.h +++ b/src/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.61 2004/11/24 18:55:56 roberto Exp $ +** $Id: llimits.h,v 1.62 2004/12/13 12:15:11 roberto Exp $ ** Limits, basic types, and some other `installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -73,6 +73,8 @@ typedef LUA_UACNUMBER l_uacNumber; typedef lu_int32 Instruction; +/* divisor for GC pace */ +#define GCDIV 8 /* maximum stack for a Lua function */ #define MAXSTACK 250 diff --git a/src/lib/lmathlib.c b/src/lmathlib.c index fcd2eefb..fcd2eefb 100644 --- a/src/lib/lmathlib.c +++ b/src/lmathlib.c diff --git a/src/loadlib.c b/src/loadlib.c new file mode 100644 index 00000000..4adcbdc0 --- /dev/null +++ b/src/loadlib.c @@ -0,0 +1,464 @@ +/* +** $Id: loadlib.c,v 1.15 2004/12/29 18:56:34 roberto Exp $ +** Dynamic library loader for Lua +** See Copyright Notice in lua.h +* +* This module contains an implementation of loadlib for Unix systems +* that have dlfcn, an implementation for Darwin (Mac OS X), an +* implementation for Windows, and a stub for other systems. +*/ + + +#include <stdlib.h> +#include <string.h> + + +#define loadlib_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + + +#define LIBPREFIX "LOADLIB: " + +#define POF LUA_POF +#define LIB_FAIL "open" + + +static void ll_unloadlib (void *lib); +static void *ll_load (lua_State *L, const char *path); +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); + + + +#if defined(USE_DLOPEN) +/* +** {======================================================================== +** This is an implementation of loadlib based on the dlfcn interface. +** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, +** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least +** as an emulation layer on top of native functions. +** ========================================================================= +*/ + +#include <dlfcn.h> + +static void ll_unloadlib (void *lib) { + dlclose(lib); +} + + +static void *ll_load (lua_State *L, const char *path) { + void *lib = dlopen(path, RTLD_NOW); + if (lib == NULL) lua_pushstring(L, dlerror()); + return lib; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + lua_CFunction f = (lua_CFunction)dlsym(lib, sym); + if (f == NULL) lua_pushstring(L, dlerror()); + return f; +} + +/* }====================================================== */ + + + +#elif defined(USE_DLL) +/* +** {====================================================================== +** This is an implementation of loadlib for Windows using native functions. +** ======================================================================= +*/ + +#include <windows.h> + + +static void pusherror (lua_State *L) { + int error = GetLastError(); + char buffer[128]; + if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, error, 0, buffer, sizeof(buffer), NULL)) + lua_pushstring(L,buffer); + else + lua_pushfstring(L, "system error %d\n", error); +} + +static void ll_unloadlib (void *lib) { + FreeLibrary((HINSTANCE)lib); +} + + +static void *ll_load (lua_State *L, const char *path) { + HINSTANCE lib = LoadLibrary(path); + if (lib == NULL) pusherror(L); + return lib; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); + if (f == NULL) pusherror(L); + return f; +} + +/* }====================================================== */ + + + +#elif defined(USE_DYLD) +/* +** {====================================================================== +** Native Mac OS X / Darwin Implementation +** ======================================================================= +*/ + +#include <mach-o/dyld.h> + + +#undef POF +#define POF "_" LUA_POF + + +static void pusherror (lua_State *L) { + const char *err_str; + const char *err_file; + NSLinkEditErrors err; + int err_num; + NSLinkEditError(&err, &err_num, &err_file, &err_str); + lua_pushstring(L, err_str); +} + + +static const char *errorfromcode (NSObjectFileImageReturnCode ret) { + switch (ret) { + case NSObjectFileImageInappropriateFile: + return "file is not a bundle"; + case NSObjectFileImageArch: + return "library is for wrong CPU type"; + case NSObjectFileImageFormat: + return "bad format"; + case NSObjectFileImageAccess: + return "cannot access file"; + case NSObjectFileImageFailure: + default: + return "unable to load library"; + } +} + + +static void ll_unloadlib (void *lib) { + NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); +} + + +static void *ll_load (lua_State *L, const char *path) { + NSObjectFileImage img; + NSObjectFileImageReturnCode ret; + /* this would be a rare case, but prevents crashing if it happens */ + if(!_dyld_present()) { + lua_pushliteral(L, "dyld not present"); + return NULL; + } + ret = NSCreateObjectFileImageFromFile(path, &img); + if (ret == NSObjectFileImageSuccess) { + NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | + NSLINKMODULE_OPTION_RETURN_ON_ERROR); + NSDestroyObjectFileImage(img); + if (mod == NULL) pusherror(L); + return mod; + } + lua_pushstring(L, errorfromcode(ret)); + return NULL; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); + if (nss == NULL) { + lua_pushfstring(L, "symbol `%s' not found", sym); + return NULL; + } + return (lua_CFunction)NSAddressOfSymbol(nss); +} + +/* }====================================================== */ + + + +#else +/* +** {====================================================== +** Fallback for other systems +** ======================================================= +*/ + +#undef LIB_FAIL +#define LIB_FAIL "absent" + + +static void ll_unloadlib (void *lib) { + (void)lib; /* to avoid warnings */ +} + + +static void *ll_load (lua_State *L, const char *path) { + (void)path; /* to avoid warnings */ + lua_pushliteral(L,"`loadlib' not supported"); + return NULL; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + (void)lib; (void)sym; /* to avoid warnings */ + lua_pushliteral(L,"`loadlib' not supported"); + return NULL; +} + +/* }====================================================== */ +#endif + + + +static void **ll_register (lua_State *L, const char *path) { + void **plib; + lua_pushfstring(L, "%s%s", LIBPREFIX, path); + lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ + if (!lua_isnil(L, -1)) /* is there an entry? */ + plib = (void **)lua_touserdata(L, -1); + else { /* no entry yet; create one */ + lua_pop(L, 1); + plib = (void **)lua_newuserdata(L, sizeof(const void *)); + *plib = NULL; + luaL_getmetatable(L, "_LOADLIB"); + lua_setmetatable(L, -2); + lua_pushfstring(L, "%s%s", LIBPREFIX, path); + lua_pushvalue(L, -2); + lua_settable(L, LUA_REGISTRYINDEX); + } + return plib; +} + + +/* +** __gc tag method: calls library's `ll_unloadlib' function with the lib +** handle +*/ +static int gctm (lua_State *L) { + void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); + if (lib) { + if (*lib) ll_unloadlib(*lib); + *lib = NULL; /* mark library as closed */ + } + return 0; +} + + +static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { + const char *reason; + void **reg = ll_register(L, path); + if (*reg == NULL) *reg = ll_load(L, path); + if (*reg == NULL) + reason = LIB_FAIL; + else { + lua_CFunction f = ll_sym(L, *reg, sym); + if (f) { + lua_pushcfunction(L, f); + return 1; /* return function */ + } + reason = "init"; + } + lua_pushnil(L); + lua_insert(L, -2); + lua_pushstring(L, reason); + return 3; /* return nil, ll_error, reason */ +} + + +static int ll_loadlib (lua_State *L) { + const char *path = luaL_checkstring(L, 1); + const char *init = luaL_checkstring(L, 2); + return ll_loadfunc(L, path, init); +} + + + +/* +** {====================================================== +** `require' and `module' functions +** ======================================================= +*/ + + +static const char *loadLua (lua_State *L, const char *fname, const char *name) { + const char *path; + /* try first `LUA_PATH' for compatibility */ + lua_getglobal(L, "LUA_PATH"); + path = lua_tostring(L, -1); + if (!path) { + lua_pop(L, 1); + luaL_getfield(L, LUA_GLOBALSINDEX, "package.path"); + path = lua_tostring(L, -1); + } + if (path == NULL) + luaL_error(L, "`package.path' must be a string"); + fname = luaL_searchpath(L, fname, path); + if (fname == NULL) return path; /* library not found in this path */ + if (luaL_loadfile(L, fname) != 0) + luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1)); + return NULL; /* library loaded successfully */ +} + + +static const char *loadC (lua_State *L, const char *fname, const char *name) { + const char *path; + const char *funcname; + luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath"); + path = lua_tostring(L, -1); + if (path == NULL) + luaL_error(L, "`package.cpath' must be a string"); + fname = luaL_searchpath(L, fname, path); + if (fname == NULL) return path; /* library not found in this path */ + funcname = luaL_gsub(L, name, ".", LUA_OFSEP); + funcname = lua_pushfstring(L, "%s%s", POF, funcname); + if (ll_loadfunc(L, fname, funcname) != 1) + luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -2)); + return NULL; /* library loaded successfully */ +} + + +static int ll_require (lua_State *L) { + const char *name = luaL_checkstring(L, 1); + lua_settop(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, 2, name); + if (lua_toboolean(L, -1)) /* is it there? */ + return 1; /* package is already loaded; return its result */ + /* else must load it; first mark it as loaded */ + lua_pushboolean(L, 1); + lua_setfield(L, 2, name); /* _LOADED[name] = true */ + /* check whether it is preloaded */ + lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); + lua_getfield(L, -1, name); + if (lua_isnil(L, -1)) { /* no preload function for that module? */ + const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP); + const char *cpath = loadC(L, fname, name); /* try a C module */ + if (cpath) { /* not found? */ + const char *path = loadLua(L, fname, name); /* try a Lua module */ + if (path) { /* yet not found? */ + lua_pushnil(L); + lua_setfield(L, 2, name); /* unmark _LOADED[name] */ + return luaL_error(L, "package `%s' not found\n" + " cpath: %s\n path: %s", + name, cpath, path); + } + } + } + lua_pushvalue(L, 1); /* pass name as argument to module */ + lua_call(L, 1, 1); /* run loaded module */ + if (!lua_isnil(L, -1)) /* non-nil return? */ + lua_setfield(L, 2, name); /* update _LOADED[name] with returned value */ + lua_getfield(L, 2, name); /* return _LOADED[name] */ + return 1; +} + + +static void setfenv (lua_State *L) { + lua_Debug ar; + lua_getstack(L, 1, &ar); + lua_getinfo(L, "f", &ar); + lua_pushvalue(L, -2); + lua_setfenv(L, -2); +} + + +static int ll_module (lua_State *L) { + const char *modname = luaL_checkstring(L, 1); + const char *dot; + lua_settop(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + /* try to find given table */ + luaL_getfield(L, LUA_GLOBALSINDEX, modname); + if (lua_isnil(L, -1)) { /* not found? */ + lua_pop(L, 1); /* remove previous result */ + lua_newtable(L); /* create it */ + /* register it with given name */ + lua_pushvalue(L, -1); + luaL_setfield(L, LUA_GLOBALSINDEX, modname); + } + else if (!lua_istable(L, -1)) + return luaL_error(L, "name conflict for module `%s'", modname); + /* check whether table already has a _NAME field */ + lua_getfield(L, -1, "_NAME"); + if (!lua_isnil(L, -1)) /* is table an initialized module? */ + lua_pop(L, 1); + else { /* no; initialize it */ + lua_pop(L, 1); + lua_newtable(L); /* create new metatable */ + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setfield(L, -2, "__index"); /* mt.__index = _G */ + lua_setmetatable(L, -2); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "_M"); /* module._M = module */ + lua_pushstring(L, modname); + lua_setfield(L, -2, "_NAME"); + dot = strrchr(modname, '.'); /* look for last dot in module name */ + if (dot == NULL) dot = modname; + else dot++; + /* set _PACKAGE as package name (full module name minus last part) */ + lua_pushlstring(L, modname, dot - modname); + lua_setfield(L, -2, "_PACKAGE"); + } + lua_pushvalue(L, -1); + lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ + setfenv(L); + return 0; +} + + +/* }====================================================== */ + + +static const luaL_reg ll_funcs[] = { + {"loadlib", ll_loadlib}, + {"require", ll_require}, + {"module", ll_module}, + {NULL, NULL} +}; + + + +LUALIB_API int luaopen_loadlib (lua_State *L) { + const char *path; + /* create new type _LOADLIB */ + luaL_newmetatable(L, "_LOADLIB"); + lua_pushcfunction(L, gctm); + lua_setfield(L, -2, "__gc"); + /* create `package' table */ + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setglobal(L, "package"); + /* set field `path' */ + path = getenv(LUA_PATH); + if (path == NULL) path = LUA_PATH_DEFAULT; + lua_pushstring(L, path); + lua_setfield(L, -2, "path"); + /* set field `cpath' */ + path = getenv(LUA_CPATH); + if (path == NULL) path = LUA_CPATH_DEFAULT; + lua_pushstring(L, path); + lua_setfield(L, -2, "cpath"); + /* set field `loaded' */ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_setfield(L, -2, "loaded"); + /* set field `preload' */ + lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); + lua_setfield(L, -2, "preload"); + lua_pushvalue(L, LUA_GLOBALSINDEX); + luaL_openlib(L, NULL, ll_funcs, 0); /* open lib into global table */ + return 1; +} + diff --git a/src/lib/loslib.c b/src/loslib.c index 4f1d2d03..4f1d2d03 100644 --- a/src/lib/loslib.c +++ b/src/loslib.c diff --git a/src/lparser.c b/src/lparser.c index b40cf7c7..b6b3c0a0 100644 --- a/src/lparser.c +++ b/src/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.10 2004/12/03 20:50:25 roberto Exp $ +** $Id: lparser.c,v 2.11 2004/12/07 18:31:16 roberto Exp $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -109,10 +109,15 @@ static int testnext (LexState *ls, int c) { static void check (LexState *ls, int c) { - if (!testnext(ls, c)) + if (ls->t.token != c) error_expected(ls, c); } +static void checknext (LexState *ls, int c) { + check(ls, c); + next(ls); +} + #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } @@ -133,7 +138,7 @@ static void check_match (LexState *ls, int what, int who, int where) { static TString *str_checkname (LexState *ls) { TString *ts; - if (ls->t.token != TK_NAME) error_expected(ls, TK_NAME); + check(ls, TK_NAME); ts = ls->t.seminfo.ts; next(ls); return ts; @@ -396,7 +401,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { funcstate.f->is_vararg = NEWSTYLEVARARG; next(&lexstate); /* read first token */ chunk(&lexstate); - check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected"); + check(&lexstate, TK_EOS); close_func(&lexstate); lua_assert(funcstate.prev == NULL); lua_assert(funcstate.f->nups == 0); @@ -428,7 +433,7 @@ static void yindex (LexState *ls, expdesc *v) { next(ls); /* skip the '[' */ expr(ls, v); luaK_exp2val(ls->fs, v); - check(ls, ']'); + checknext(ls, ']'); } @@ -460,7 +465,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { } else /* ls->t.token == '[' */ yindex(ls, &key); - check(ls, '='); + checknext(ls, '='); luaK_exp2RK(fs, &key); expr(ls, &val); luaK_codeABC(fs, OP_SETTABLE, cc->t->info, luaK_exp2RK(fs, &key), @@ -514,7 +519,7 @@ static void constructor (LexState *ls, expdesc *t) { init_exp(t, VRELOCABLE, pc); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ - check(ls, '{'); + checknext(ls, '{'); do { lua_assert(cc.v.k == VVOID || cc.tostore > 0); testnext(ls, ';'); /* compatibility only */ @@ -584,13 +589,13 @@ static void body (LexState *ls, expdesc *e, int needself, int line) { FuncState new_fs; open_func(ls, &new_fs); new_fs.f->lineDefined = line; - check(ls, '('); + checknext(ls, '('); if (needself) { new_localvarliteral(ls, "self", 0); adjustlocalvars(ls, 1); } parlist(ls); - check(ls, ')'); + checknext(ls, ')'); chunk(ls); check_match(ls, TK_END, TK_FUNCTION, line); close_func(ls); @@ -944,7 +949,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { } else { /* assignment -> `=' explist1 */ int nexps; - check(ls, '='); + checknext(ls, '='); nexps = explist1(ls, &e); if (nexps != nvars) { adjust_assign(ls, nvars, nexps, &e); @@ -1011,7 +1016,7 @@ static void whilestat (LexState *ls, int line) { codeexp[i] = fs->f->code[expinit + i]; fs->pc = expinit; /* remove `exp' code */ enterblock(fs, &bl, 1); - check(ls, TK_DO); + checknext(ls, TK_DO); blockinit = luaK_getlabel(fs); block(ls); luaK_patchtohere(fs, whileinit); /* initial jump jumps to here */ @@ -1059,7 +1064,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { FuncState *fs = ls->fs; int prep, endfor; adjustlocalvars(ls, 3); /* control variables */ - check(ls, TK_DO); + checknext(ls, TK_DO); prep = luaK_codeAsBx(fs, (isnum ? OP_FORPREP : OP_TFORPREP), base, NO_JUMP); enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); @@ -1082,9 +1087,9 @@ static void fornum (LexState *ls, TString *varname, int line) { new_localvarliteral(ls, "(for limit)", 1); new_localvarliteral(ls, "(for step)", 2); new_localvar(ls, varname, 3); - check(ls, '='); + checknext(ls, '='); exp1(ls); /* initial value */ - check(ls, ','); + checknext(ls, ','); exp1(ls); /* limit */ if (testnext(ls, ',')) exp1(ls); /* optional step */ @@ -1111,7 +1116,7 @@ static void forlist (LexState *ls, TString *indexname) { new_localvar(ls, indexname, nvars++); while (testnext(ls, ',')) new_localvar(ls, str_checkname(ls), nvars++); - check(ls, TK_IN); + checknext(ls, TK_IN); line = ls->linenumber; adjust_assign(ls, 3, explist1(ls, &e), &e); luaK_checkstack(fs, 3); /* extra space to call generator */ @@ -1142,7 +1147,7 @@ static int test_then_block (LexState *ls) { int flist; next(ls); /* skip IF or ELSEIF */ flist = cond(ls); - check(ls, TK_THEN); + checknext(ls, TK_THEN); block(ls); /* `then' part */ return flist; } diff --git a/src/lstate.c b/src/lstate.c index dfd35be6..bd238c58 100644 --- a/src/lstate.c +++ b/src/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.18 2004/12/06 17:53:42 roberto Exp $ +** $Id: lstate.c,v 2.19 2004/12/13 12:15:11 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -193,7 +193,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { setnilvalue(gval(g->dummynode)); gnext(g->dummynode) = NULL; g->totalbytes = sizeof(LG); - g->stepmul = STEPMUL; + g->gcpace = GCDIV; g->incgc = 1; if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { /* memory allocation error: free partial state */ diff --git a/src/lstate.h b/src/lstate.h index 81fb75a0..4f29190a 100644 --- a/src/lstate.h +++ b/src/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.9 2004/12/06 17:53:42 roberto Exp $ +** $Id: lstate.h,v 2.10 2004/12/13 12:15:11 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -85,7 +85,7 @@ typedef struct global_State { lu_mem totalbytes; /* number of bytes currently allocated */ lu_mem estimate; /* an estimate of number of bytes actually in use */ lu_mem prevestimate; /* previous estimate */ - int stepmul; /* relative `speed' of the GC */ + int gcpace; /* relative `speed' of the GC */ int incgc; /* 0 if GC is done non-incrementally */ lua_CFunction panic; /* to be called in unprotected errors */ TValue _registry; diff --git a/src/lib/lstrlib.c b/src/lstrlib.c index a9c002fb..a9c002fb 100644 --- a/src/lib/lstrlib.c +++ b/src/lstrlib.c diff --git a/src/lib/ltablib.c b/src/ltablib.c index ab953c61..792bf313 100644 --- a/src/lib/ltablib.c +++ b/src/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.26 2004/06/15 13:37:21 roberto Exp $ +** $Id: ltablib.c,v 1.27 2004/12/07 18:28:47 roberto Exp $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -19,7 +19,7 @@ #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) -static int luaB_foreachi (lua_State *L) { +static int foreachi (lua_State *L) { int i; int n = aux_getn(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); @@ -36,7 +36,7 @@ static int luaB_foreachi (lua_State *L) { } -static int luaB_foreach (lua_State *L) { +static int foreach (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TFUNCTION); lua_pushnil(L); /* first key */ @@ -54,13 +54,13 @@ static int luaB_foreach (lua_State *L) { } -static int luaB_getn (lua_State *L) { +static int getn (lua_State *L) { lua_pushinteger(L, aux_getn(L, 1)); return 1; } -static int luaB_setn (lua_State *L) { +static int setn (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_setn(L, 1, luaL_checkint(L, 2)); lua_pushvalue(L, 1); @@ -68,7 +68,7 @@ static int luaB_setn (lua_State *L) { } -static int luaB_tinsert (lua_State *L) { +static int tinsert (lua_State *L) { int v = lua_gettop(L); /* number of arguments */ int e = aux_getn(L, 1) + LUA_FIRSTINDEX; /* first empty element */ int pos; /* where to insert new element */ @@ -90,7 +90,7 @@ static int luaB_tinsert (lua_State *L) { } -static int luaB_tremove (lua_State *L) { +static int tremove (lua_State *L) { int e = aux_getn(L, 1) + LUA_FIRSTINDEX - 1; int pos = luaL_optint(L, 2, e); if (e < LUA_FIRSTINDEX) return 0; /* table is `empty' */ @@ -220,7 +220,7 @@ static void auxsort (lua_State *L, int l, int u) { } /* repeat the routine for the larger one */ } -static int luaB_sort (lua_State *L) { +static int sort (lua_State *L) { int n = aux_getn(L, 1); luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ @@ -235,13 +235,13 @@ static int luaB_sort (lua_State *L) { static const luaL_reg tab_funcs[] = { {"concat", str_concat}, - {"foreach", luaB_foreach}, - {"foreachi", luaB_foreachi}, - {"getn", luaB_getn}, - {"setn", luaB_setn}, - {"sort", luaB_sort}, - {"insert", luaB_tinsert}, - {"remove", luaB_tremove}, + {"foreach", foreach}, + {"foreachi", foreachi}, + {"getn", getn}, + {"setn", setn}, + {"sort", sort}, + {"insert", tinsert}, + {"remove", tremove}, {NULL, NULL} }; diff --git a/src/lua/lua.c b/src/lua.c index 6a21a6ae..6a21a6ae 100644 --- a/src/lua/lua.c +++ b/src/lua.c diff --git a/src/lua.h b/src/lua.h new file mode 100644 index 00000000..25fc8405 --- /dev/null +++ b/src/lua.h @@ -0,0 +1,383 @@ +/* +** $Id: lua.h,v 1.197 2004/12/13 12:15:11 roberto Exp $ +** Lua - An Extensible Extension Language +** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil +** http://www.lua.org mailto:info@lua.org +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include <stdarg.h> +#include <stddef.h> + + +#include "luaconf.h" + + +#define LUA_VERSION "Lua 5.1 (work4)" +#define LUA_COPYRIGHT "Copyright (C) 1994-2004 Tecgraf, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" + + +/* mark for precompiled code (`<esc>Lua') */ +#define LUA_SIGNATURE "\033Lua" + +/* option for multiple returns in `lua_pcall' and `lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** pseudo-indices +*/ +#define LUA_REGISTRYINDEX (-10000) +#define LUA_GLOBALSINDEX (-10001) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) + + +/* return codes for `lua_pcall', `lua_resume', and `lua_threadstatus' */ +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + + +typedef struct lua_State lua_State; + +typedef int (*lua_CFunction) (lua_State *L); + + +/* +** functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Chunkwriter) (lua_State *L, const void* p, + size_t sz, void* ud); + + +/* +** prototype for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + + +/* first index for arrays */ +#define LUA_FIRSTINDEX 1 + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* +** generic extra include file +*/ +#ifdef LUA_USER_H +#include LUA_USER_H +#endif + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + + + +/* +** state manipulation +*/ +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud); +LUA_API void lua_close (lua_State *L); +LUA_API lua_State *lua_newthread (lua_State *L); + +LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf); + + +/* +** basic stack manipulation +*/ +LUA_API int lua_gettop (lua_State *L); +LUA_API void lua_settop (lua_State *L, int idx); +LUA_API void lua_pushvalue (lua_State *L, int idx); +LUA_API void lua_remove (lua_State *L, int idx); +LUA_API void lua_insert (lua_State *L, int idx); +LUA_API void lua_replace (lua_State *L, int idx); +LUA_API int lua_checkstack (lua_State *L, int sz); + +LUA_API void lua_xmove (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int lua_isnumber (lua_State *L, int idx); +LUA_API int lua_isstring (lua_State *L, int idx); +LUA_API int lua_iscfunction (lua_State *L, int idx); +LUA_API int lua_isuserdata (lua_State *L, int idx); +LUA_API int lua_type (lua_State *L, int idx); +LUA_API const char *lua_typename (lua_State *L, int tp); + +LUA_API int lua_equal (lua_State *L, int idx1, int idx2); +LUA_API int lua_rawequal (lua_State *L, int idx1, int idx2); +LUA_API int lua_lessthan (lua_State *L, int idx1, int idx2); + +LUA_API lua_Number lua_tonumber (lua_State *L, int idx); +LUA_API lua_Integer lua_tointeger (lua_State *L, int idx); +LUA_API int lua_toboolean (lua_State *L, int idx); +LUA_API const char *lua_tostring (lua_State *L, int idx); +LUA_API size_t lua_objsize (lua_State *L, int idx); +LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx); +LUA_API void *lua_touserdata (lua_State *L, int idx); +LUA_API lua_State *lua_tothread (lua_State *L, int idx); +LUA_API const void *lua_topointer (lua_State *L, int idx); + + +/* +** push functions (C -> stack) +*/ +LUA_API void lua_pushnil (lua_State *L); +LUA_API void lua_pushnumber (lua_State *L, lua_Number n); +LUA_API void lua_pushinteger (lua_State *L, lua_Integer n); +LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t l); +LUA_API void lua_pushstring (lua_State *L, const char *s); +LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...); +LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); +LUA_API void lua_pushboolean (lua_State *L, int b); +LUA_API void lua_pushlightuserdata (lua_State *L, void *p); +LUA_API int lua_pushthread (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API void lua_gettable (lua_State *L, int idx); +LUA_API void lua_getfield (lua_State *L, int idx, const char *k); +LUA_API void lua_rawget (lua_State *L, int idx); +LUA_API void lua_rawgeti (lua_State *L, int idx, int n); +LUA_API void lua_createtable (lua_State *L, int narr, int nrec); +LUA_API void *lua_newuserdata (lua_State *L, size_t sz); +LUA_API int lua_getmetatable (lua_State *L, int objindex); +LUA_API void lua_getfenv (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void lua_settable (lua_State *L, int idx); +LUA_API void lua_setfield (lua_State *L, int idx, const char *k); +LUA_API void lua_rawset (lua_State *L, int idx); +LUA_API void lua_rawseti (lua_State *L, int idx, int n); +LUA_API int lua_setmetatable (lua_State *L, int objindex); +LUA_API int lua_setfenv (lua_State *L, int idx); + + +/* +** `load' and `call' functions (load and run Lua code) +*/ +LUA_API void lua_call (lua_State *L, int nargs, int nresults); +LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc); +LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud); +LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *dt, + const char *chunkname); + +LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data); + + +/* +** coroutine functions +*/ +LUA_API int lua_yield (lua_State *L, int nresults); +LUA_API int lua_resume (lua_State *L, int narg); +LUA_API int lua_threadstatus (lua_State *L); + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCSTEP 4 +#define LUA_GCSETPACE 5 +#define LUA_GCSETINCMODE 6 + +LUA_API int lua_gc (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API const char *lua_version (void); + +LUA_API int lua_error (lua_State *L); + +LUA_API int lua_next (lua_State *L, int idx); + +LUA_API void lua_concat (lua_State *L, int n); + +LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_strlen(L,i) lua_objsize(L, (i)) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) + +#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) +#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) + + + +/* +** compatibility macros and functions +*/ + +#define lua_open() luaL_newstate() + +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) + +#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) + + + + + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILRET 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); + +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook lua_gethook (lua_State *L); +LUA_API int lua_gethookmask (lua_State *L); +LUA_API int lua_gethookcount (lua_State *L); + + +#define LUA_IDSIZE 60 + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) `global', `local', `field', `method' */ + const char *what; /* (S) `Lua', `C', `main', `tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int nups; /* (u) number of upvalues */ + int linedefined; /* (S) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + int i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2004 Tecgraf, PUC-Rio. All rights reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/src/lua/Makefile b/src/lua/Makefile deleted file mode 100644 index aa52832f..00000000 --- a/src/lua/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -# makefile for Lua interpreter - -LUA= ../.. - -include $(LUA)/config - -EXTRA_DEFS= $(USERCONF) -OBJS= lua.o -SRCS= lua.c - -T= $(BIN)/lua - -all: $T - -$T: $(OBJS) $(LIB)/liblua.a $(LIB)/liblualib.a - $(CC) -o $@ $(MYLDFLAGS) $(OBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(DLLIB) - -$(LIB)/liblua.a: - cd ..; $(MAKE) - -$(LIB)/liblualib.a: - cd ../lib; $(MAKE) - -clean: - rm -f $(OBJS) $T - -co: - co -q -f -M $(SRCS) - -klean: clean - rm -f $(SRCS) diff --git a/src/lua/README b/src/lua/README deleted file mode 100644 index 525f6c15..00000000 --- a/src/lua/README +++ /dev/null @@ -1,41 +0,0 @@ -This is lua, the stand-alone Lua interpreter. -It can be used as a batch interpreter and also interactively. -There are man pages for it in both nroff and html in ../../doc. - -Usage: lua [options] [script [args]]. Available options are: - - execute stdin as a file - -e stat execute string `stat' - -i enter interactive mode after executing `script' - -l name load and run library `name' - -v show version information - -w catch use of undefined global variables - -- stop handling options - -This interpreter is suitable for using Lua as a stand-alone language; it loads -all standard libraries. For a minimal interpreter, see ../../etc/min.c. - -If your application simply exports new functions to Lua (which is common), -then you can use this interpreter unmodified by putting your functions into -a library and loading it dynamically (if you have built support for dynamic -libraries). - -If you need to add your library statically, then you can use this interpreter -almost unmodified, as follows: - -* First, define a function - void myinit (lua_State *L) - in your own code. In this function, you should do whatever initializations - are needed by your application, typically exporting your functions to Lua. - (Of course, you can use any name instead of "myinit".) - -* Then, #define lua_userinit(L) in luaconf.h to call luaopen_stdlibs and - possibly luaopen_stdlibs, which opens all standard libraries. If you don't - need them, just don't call openstdlibs and open any standard libraries that - you do need in myinit. - -* Alternatively, write your own luaopen_stdlibs; the linker will use your - version (if you ask politely). - -Just remember to link your C code when building lua! - -For other customizations, see ../../include/luaconf.h . diff --git a/src/luac/luac.c b/src/luac.c index dfbcab05..dfbcab05 100644 --- a/src/luac/luac.c +++ b/src/luac.c diff --git a/src/luac/Makefile b/src/luac/Makefile deleted file mode 100644 index 509577bb..00000000 --- a/src/luac/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -# makefile for Lua compiler - -LUA= ../.. - -include $(LUA)/config - -INCS= -I$(INC) -I.. $(EXTRA_INCS) -OBJS= luac.o print.o -SRCS= luac.c print.c - -T= $(BIN)/luac - -all: $T - -$T: $(OBJS) $(LIB)/liblua.a ../lib/lauxlib.o - $(CC) -o $@ $(MYLDFLAGS) $(OBJS) ../lib/lauxlib.o -L$(LIB) -llua $(EXTRA_LIBS) - -$(LIB)/liblua.a: - cd ..; $(MAKE) - -../lib/lauxlib.o: - cd ../lib; $(MAKE) lauxlib.o - -clean: - rm -f $(OBJS) $T - -co: - co -q -f -M $(SRCS) - -klean: clean - rm -f $(SRCS) diff --git a/src/luac/README b/src/luac/README deleted file mode 100644 index 00dd1cd4..00000000 --- a/src/luac/README +++ /dev/null @@ -1,18 +0,0 @@ -This is luac, the Lua compiler. -There are man pages for it in both nroff and html in ../../doc. - -luac translates Lua programs into binary files that can be loaded later. -The main advantages of pre-compiling chunks are: faster loading, protecting -source code from user changes, and off-line syntax error detection. -luac can also be used to learn about the Lua virtual machine. - -Usage: luac [options] [filenames]. Available options are: - - process stdin - -l list - -o name output to file `name' (default is "luac.out") - -p parse only - -s strip debug information - -v show version information - -- stop handling options - -luac is also an example of how to use the internals of Lua (politely). diff --git a/src/luaconf.h b/src/luaconf.h new file mode 100644 index 00000000..7ea477e2 --- /dev/null +++ b/src/luaconf.h @@ -0,0 +1,386 @@ +/* +** $Id: luaconf.h,v 1.22 2004/12/27 15:58:15 roberto Exp $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef lconfig_h +#define lconfig_h + +#include <limits.h> +#include <stddef.h> + + +/* +** {====================================================== +** Index (search for keyword to find corresponding entry) +** ======================================================= +*/ + + +/* }====================================================== */ + + + + +/* +** {====================================================== +** Generic configuration +** ======================================================= +*/ + +/* default path */ +#if defined(_WIN32) +#define LUA_ROOT "C:\\Program Files\\Lua51" +#define LUA_LDIR LUA_ROOT "\\lua" +#define LUA_CDIR LUA_ROOT "\\dll" +#define LUA_PATH_DEFAULT \ + "?.lua;" LUA_LDIR "\\?.lua;" LUA_LDIR "\\?\\init.lua" +#define LUA_CPATH_DEFAULT "?.dll;" LUA_CDIR "\\?.dll" + +#else +#define LUA_ROOT "/usr/local" +#define LUA_LDIR LUA_ROOT "/share/lua/5.1" +#define LUA_CDIR LUA_ROOT "/lib/lua/5.1" +#define LUA_PATH_DEFAULT \ + "./?.lua;" LUA_LDIR "/?.lua;" LUA_LDIR "/?/init.lua" +#define LUA_CPATH_DEFAULT "./?.so;" LUA_CDIR "/?.so" +#endif + + + +/* type of numbers in Lua */ +#define LUA_NUMBER double + +/* formats for Lua numbers */ +#define LUA_NUMBER_SCAN "%lf" +#define LUA_NUMBER_FMT "%.14g" + + +/* +** type for integer functions +** on most machines, `ptrdiff_t' gives a reasonable size for integers +*/ +#define LUA_INTEGER ptrdiff_t + + +/* mark for all API functions */ +#define LUA_API extern + +/* mark for auxlib functions */ +#define LUALIB_API extern + +/* buffer size used by lauxlib buffer system */ +#define LUAL_BUFFERSIZE BUFSIZ + + +/* assertions in Lua (mainly for internal debugging) */ +#define lua_assert(c) ((void)0) + +/* }====================================================== */ + + + +/* +** {====================================================== +** Stand-alone configuration +** ======================================================= +*/ + +#ifdef lua_c + +/* definition of `isatty' */ +#ifdef _POSIX_C_SOURCE +#include <unistd.h> +#define stdin_is_tty() isatty(0) +#elif defined(_WIN32) +#include <io.h> +#include <fcntl.h> +#define stdin_is_tty() _isatty(_fileno(stdin)) +#else +#define stdin_is_tty() 1 /* assume stdin is a tty */ +#endif + + +#define PROMPT "> " +#define PROMPT2 ">> " +#define PROGNAME "lua" + + +/* +** this macro allows you to open other libraries when starting the +** stand-alone interpreter +*/ +#define lua_userinit(L) luaopen_stdlibs(L) +/* +** #define lua_userinit(L) { int luaopen_mylibs(lua_State *L); \ +** luaopen_stdlibs(L); luaopen_mylibs(L); } +*/ + + + +/* +** this macro can be used by some `history' system to save lines +** read in manual input +*/ +#define lua_saveline(L,line) /* empty */ + + + +#endif + +/* }====================================================== */ + + + +/* +** {====================================================== +** Core configuration +** ======================================================= +*/ + +#ifdef LUA_CORE + +/* LUA-C API assertions */ +#define api_check(L,o) lua_assert(o) + + +/* number of bits in an `int' */ +/* avoid overflows in comparison */ +#if INT_MAX-20 < 32760 +#define LUA_BITSINT 16 +#elif INT_MAX > 2147483640L +/* `int' has at least 32 bits */ +#define LUA_BITSINT 32 +#else +#error "you must define LUA_BITSINT with number of bits in an integer" +#endif + + +/* +** L_UINT32: unsigned integer with at least 32 bits +** L_INT32: signed integer with at least 32 bits +** LU_MEM: an unsigned integer big enough to count the total memory used by Lua +** L_MEM: a signed integer big enough to count the total memory used by Lua +*/ +#if LUA_BITSINT >= 32 +#define LUA_UINT32 unsigned int +#define LUA_INT32 int +#define LUA_MAXINT32 INT_MAX +#define LU_MEM size_t +#define L_MEM ptrdiff_t +#else +/* 16-bit ints */ +#define LUA_UINT32 unsigned long +#define LUA_INT32 long +#define LUA_MAXINT32 LONG_MAX +#define LU_MEM LUA_UINT32 +#define L_MEM ptrdiff_t +#endif + + +/* maximum depth for calls (unsigned short) */ +#define LUA_MAXCALLS 10000 + +/* +** maximum depth for C calls (unsigned short): Not too big, or may +** overflow the C stack... +*/ +#define LUA_MAXCCALLS 200 + + +/* maximum size for the virtual stack of a C function */ +#define MAXCSTACK 2048 + + +/* +** maximum number of syntactical nested non-terminals: Not too big, +** or may overflow the C stack... +*/ +#define LUA_MAXPARSERLEVEL 200 + + +/* maximum number of variables declared in a function */ +#define MAXVARS 200 /* <MAXSTACK */ + + +/* maximum number of upvalues per function */ +#define MAXUPVALUES 60 /* <MAXSTACK */ + + +/* maximum size of expressions for optimizing `while' code */ +#define MAXEXPWHILE 100 + + +/* function to convert a lua_Number to int (with any rounding method) */ +#if defined(__GNUC__) && defined(__i386) +#define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st") + +#elif defined(_WIN32) && defined(_M_IX86) +#include <math.h> +#pragma warning(disable: 4514) +__inline int l_lrint (double flt) +{ int i; + _asm { + fld flt + fistp i + }; + return i; +} +#define lua_number2int(i,d) ((i)=l_lrint((d))) + +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199900L) +/* on machines compliant with C99, you can try `lrint' */ +#include <math.h> +#define lua_number2int(i,d) ((i)=lrint(d)) + +#else +#define lua_number2int(i,d) ((i)=(int)(d)) + +#endif + + +/* function to convert a lua_Number to lua_Integer (with any rounding method) */ +#define lua_number2integer(i,n) lua_number2int((i), (n)) + + +/* function to convert a lua_Number to a string */ +#include <stdio.h> +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) + +/* function to convert a string to a lua_Number */ +#define lua_str2number(s,p) strtod((s), (p)) + + + +/* result of a `usual argument conversion' over lua_Number */ +#define LUA_UACNUMBER double + + + +/* type to ensure maximum alignment */ +#define LUSER_ALIGNMENT_T union { double u; void *s; long l; } + + +/* exception handling */ +#ifndef __cplusplus +/* default handling with long jumps */ +#include <setjmp.h> +#define L_THROW(L,c) longjmp((c)->b, 1) +#define L_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } +#define l_jmpbuf jmp_buf + +#else +/* C++ exceptions */ +#define L_THROW(L,c) throw(c) +#define L_TRY(L,c,a) try { a } catch(...) \ + { if ((c)->status == 0) (c)->status = -1; } +#define l_jmpbuf int /* dummy variable */ +#endif + + + +/* +** macros for thread synchronization inside Lua core machine: +** all accesses to the global state and to global objects are synchronized. +** Because threads can read the stack of other threads +** (when running garbage collection), +** a thread must also synchronize any write-access to its own stack. +** Unsynchronized accesses are allowed only when reading its own stack, +** or when reading immutable fields from global objects +** (such as string values and udata values). +*/ +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) + +/* +** this macro allows a thread switch in appropriate places in the Lua +** core +*/ +#define lua_threadyield(L) {lua_unlock(L); lua_lock(L);} + + + +/* allows user-specific initialization on new threads */ +#define lua_userstateopen(L) /* empty */ + + +#endif + +/* }====================================================== */ + + + +/* +** {====================================================== +** Library configuration +** ======================================================= +*/ + +#ifdef LUA_LIB + + + +/* `assert' options */ + +/* environment variables that hold the search path for packages */ +#define LUA_PATH "LUA_PATH" +#define LUA_CPATH "LUA_CPATH" + +/* prefix for open functions in C libraries */ +#define LUA_POF "luaopen_" + +/* separator for open functions in C libraries */ +#define LUA_OFSEP "" + +/* directory separator (for submodules) */ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + +/* separator of templates in a path */ +#define LUA_PATH_SEP ';' + +/* wild char in each template */ +#define LUA_PATH_MARK "?" + + +/* maximum number of captures in pattern-matching */ +#define MAX_CAPTURES 32 /* arbitrary limit */ + + +/* +** by default, gcc does not get `tmpname' +*/ +#ifdef __GNUC__ +#define USE_TMPNAME 0 +#else +#define USE_TMPNAME 1 +#endif + + +/* +** Configuration for loadlib +*/ +#if defined(_WIN32) +#define USE_DLL +#elif defined(__APPLE__) && defined(__MACH__) +#define USE_DYLD +#elif defined(__linux) || defined(sun) || defined(sgi) || defined(BSD) +#define USE_DLOPEN +#endif + + +#endif + +/* }====================================================== */ + + + + +/* Local configuration */ + +#endif diff --git a/src/lualib.h b/src/lualib.h new file mode 100644 index 00000000..c8114ed2 --- /dev/null +++ b/src/lualib.h @@ -0,0 +1,47 @@ +/* +** $Id: lualib.h,v 1.32 2004/07/09 15:47:48 roberto Exp $ +** Lua standard libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lualib_h +#define lualib_h + +#include "lua.h" + + +/* Key to file-handle type */ +#define LUA_FILEHANDLE "FILE*" + + +#define LUA_COLIBNAME "coroutine" +LUALIB_API int luaopen_base (lua_State *L); + +#define LUA_TABLIBNAME "table" +LUALIB_API int luaopen_table (lua_State *L); + +#define LUA_IOLIBNAME "io" +LUALIB_API int luaopen_io (lua_State *L); + +#define LUA_OSLIBNAME "os" +LUALIB_API int luaopen_os (lua_State *L); + +#define LUA_STRLIBNAME "string" +LUALIB_API int luaopen_string (lua_State *L); + +#define LUA_MATHLIBNAME "math" +LUALIB_API int luaopen_math (lua_State *L); + +#define LUA_DBLIBNAME "debug" +LUALIB_API int luaopen_debug (lua_State *L); + + +LUALIB_API int luaopen_loadlib (lua_State *L); + + +/* open all previous libraries */ +LUALIB_API int luaopen_stdlibs (lua_State *L); + + +#endif diff --git a/src/luac/print.c b/src/print.c index f273ebfc..f273ebfc 100644 --- a/src/luac/print.c +++ b/src/print.c |