summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLua Team <team@lua.org>2004-09-21 12:00:00 +0000
committerrepogen <>2004-09-21 12:00:00 +0000
commitd8fd22e11b391cf183068049bebbee9702c8f78f (patch)
tree12d2fd56e51ab38e8ad660c34aca2975d1a273ea
parent5d480731503a315eab9d6ab9426e3d4cfd5e52f8 (diff)
downloadlua-github-d8fd22e11b391cf183068049bebbee9702c8f78f.tar.gz
Lua 5.1-work25.1-work2
-rw-r--r--MANIFEST226
-rw-r--r--include/lua.h17
-rw-r--r--include/luaconf.h6
-rw-r--r--src/lapi.c19
-rw-r--r--src/ldo.c76
-rw-r--r--src/ldo.h6
-rw-r--r--src/lgc.c33
-rw-r--r--src/lgc.h6
-rw-r--r--src/lib/lauxlib.c3
-rw-r--r--src/lib/lbaselib.c75
-rw-r--r--src/llimits.h6
-rw-r--r--src/lstate.c6
-rw-r--r--src/lstate.h4
-rw-r--r--src/lua/README23
-rw-r--r--src/lvm.c4
15 files changed, 289 insertions, 221 deletions
diff --git a/MANIFEST b/MANIFEST
index fdb5d2a2..7c81b934 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -1,114 +1,114 @@
-MANIFEST contents of Lua 5.1 (work1) distribution on Wed Sep 1 22:19:31 BRT 2004
-lua-5.1-work1
-lua-5.1-work1/COPYRIGHT
-lua-5.1-work1/HISTORY
-lua-5.1-work1/INSTALL
-lua-5.1-work1/MANIFEST
-lua-5.1-work1/Makefile
-lua-5.1-work1/README
-lua-5.1-work1/bin
-lua-5.1-work1/build
-lua-5.1-work1/config
-lua-5.1-work1/etc
-lua-5.1-work1/etc/Makefile
-lua-5.1-work1/etc/README
-lua-5.1-work1/etc/all.c
-lua-5.1-work1/etc/lua.ico
-lua-5.1-work1/etc/min.c
-lua-5.1-work1/etc/noparser.c
-lua-5.1-work1/etc/saconfig.c
-lua-5.1-work1/include
-lua-5.1-work1/include/Makefile
-lua-5.1-work1/include/lauxlib.h
-lua-5.1-work1/include/lua.h
-lua-5.1-work1/include/luaconf.h
-lua-5.1-work1/include/lualib.h
-lua-5.1-work1/lib
-lua-5.1-work1/src
-lua-5.1-work1/src/Makefile
-lua-5.1-work1/src/README
-lua-5.1-work1/src/lapi.c
-lua-5.1-work1/src/lapi.h
-lua-5.1-work1/src/lcode.c
-lua-5.1-work1/src/lcode.h
-lua-5.1-work1/src/ldebug.c
-lua-5.1-work1/src/ldebug.h
-lua-5.1-work1/src/ldo.c
-lua-5.1-work1/src/ldo.h
-lua-5.1-work1/src/ldump.c
-lua-5.1-work1/src/lfunc.c
-lua-5.1-work1/src/lfunc.h
-lua-5.1-work1/src/lgc.c
-lua-5.1-work1/src/lgc.h
-lua-5.1-work1/src/lib
-lua-5.1-work1/src/lib/Makefile
-lua-5.1-work1/src/lib/README
-lua-5.1-work1/src/lib/lauxlib.c
-lua-5.1-work1/src/lib/lbaselib.c
-lua-5.1-work1/src/lib/ldblib.c
-lua-5.1-work1/src/lib/linit.c
-lua-5.1-work1/src/lib/liolib.c
-lua-5.1-work1/src/lib/lmathlib.c
-lua-5.1-work1/src/lib/loadlib.c
-lua-5.1-work1/src/lib/loslib.c
-lua-5.1-work1/src/lib/lstrlib.c
-lua-5.1-work1/src/lib/ltablib.c
-lua-5.1-work1/src/llex.c
-lua-5.1-work1/src/llex.h
-lua-5.1-work1/src/llimits.h
-lua-5.1-work1/src/lmem.c
-lua-5.1-work1/src/lmem.h
-lua-5.1-work1/src/lobject.c
-lua-5.1-work1/src/lobject.h
-lua-5.1-work1/src/lopcodes.c
-lua-5.1-work1/src/lopcodes.h
-lua-5.1-work1/src/lparser.c
-lua-5.1-work1/src/lparser.h
-lua-5.1-work1/src/lstate.c
-lua-5.1-work1/src/lstate.h
-lua-5.1-work1/src/lstring.c
-lua-5.1-work1/src/lstring.h
-lua-5.1-work1/src/ltable.c
-lua-5.1-work1/src/ltable.h
-lua-5.1-work1/src/ltm.c
-lua-5.1-work1/src/ltm.h
-lua-5.1-work1/src/lua
-lua-5.1-work1/src/lua/Makefile
-lua-5.1-work1/src/lua/README
-lua-5.1-work1/src/lua/lua.c
-lua-5.1-work1/src/luac
-lua-5.1-work1/src/luac/Makefile
-lua-5.1-work1/src/luac/README
-lua-5.1-work1/src/luac/luac.c
-lua-5.1-work1/src/luac/print.c
-lua-5.1-work1/src/lundump.c
-lua-5.1-work1/src/lundump.h
-lua-5.1-work1/src/lvm.c
-lua-5.1-work1/src/lvm.h
-lua-5.1-work1/src/lzio.c
-lua-5.1-work1/src/lzio.h
-lua-5.1-work1/test
-lua-5.1-work1/test/README
-lua-5.1-work1/test/bisect.lua
-lua-5.1-work1/test/cf.lua
-lua-5.1-work1/test/echo.lua
-lua-5.1-work1/test/env.lua
-lua-5.1-work1/test/factorial.lua
-lua-5.1-work1/test/fib.lua
-lua-5.1-work1/test/fibfor.lua
-lua-5.1-work1/test/globals.lua
-lua-5.1-work1/test/hello.lua
-lua-5.1-work1/test/life.lua
-lua-5.1-work1/test/lua
-lua-5.1-work1/test/luac
-lua-5.1-work1/test/luac.lua
-lua-5.1-work1/test/printf.lua
-lua-5.1-work1/test/readonly.lua
-lua-5.1-work1/test/sieve.lua
-lua-5.1-work1/test/sort.lua
-lua-5.1-work1/test/table.lua
-lua-5.1-work1/test/trace-calls.lua
-lua-5.1-work1/test/trace-globals.lua
-lua-5.1-work1/test/undefined.lua
-lua-5.1-work1/test/xd.lua
+MANIFEST contents of Lua 5.1 (work2) distribution on Mon Sep 20 21:28:06 BRT 2004
+lua-5.1-work2
+lua-5.1-work2/COPYRIGHT
+lua-5.1-work2/HISTORY
+lua-5.1-work2/INSTALL
+lua-5.1-work2/MANIFEST
+lua-5.1-work2/Makefile
+lua-5.1-work2/README
+lua-5.1-work2/bin
+lua-5.1-work2/build
+lua-5.1-work2/config
+lua-5.1-work2/etc
+lua-5.1-work2/etc/Makefile
+lua-5.1-work2/etc/README
+lua-5.1-work2/etc/all.c
+lua-5.1-work2/etc/lua.ico
+lua-5.1-work2/etc/min.c
+lua-5.1-work2/etc/noparser.c
+lua-5.1-work2/etc/saconfig.c
+lua-5.1-work2/include
+lua-5.1-work2/include/Makefile
+lua-5.1-work2/include/lauxlib.h
+lua-5.1-work2/include/lua.h
+lua-5.1-work2/include/luaconf.h
+lua-5.1-work2/include/lualib.h
+lua-5.1-work2/lib
+lua-5.1-work2/src
+lua-5.1-work2/src/Makefile
+lua-5.1-work2/src/README
+lua-5.1-work2/src/lapi.c
+lua-5.1-work2/src/lapi.h
+lua-5.1-work2/src/lcode.c
+lua-5.1-work2/src/lcode.h
+lua-5.1-work2/src/ldebug.c
+lua-5.1-work2/src/ldebug.h
+lua-5.1-work2/src/ldo.c
+lua-5.1-work2/src/ldo.h
+lua-5.1-work2/src/ldump.c
+lua-5.1-work2/src/lfunc.c
+lua-5.1-work2/src/lfunc.h
+lua-5.1-work2/src/lgc.c
+lua-5.1-work2/src/lgc.h
+lua-5.1-work2/src/lib
+lua-5.1-work2/src/lib/Makefile
+lua-5.1-work2/src/lib/README
+lua-5.1-work2/src/lib/lauxlib.c
+lua-5.1-work2/src/lib/lbaselib.c
+lua-5.1-work2/src/lib/ldblib.c
+lua-5.1-work2/src/lib/linit.c
+lua-5.1-work2/src/lib/liolib.c
+lua-5.1-work2/src/lib/lmathlib.c
+lua-5.1-work2/src/lib/loadlib.c
+lua-5.1-work2/src/lib/loslib.c
+lua-5.1-work2/src/lib/lstrlib.c
+lua-5.1-work2/src/lib/ltablib.c
+lua-5.1-work2/src/llex.c
+lua-5.1-work2/src/llex.h
+lua-5.1-work2/src/llimits.h
+lua-5.1-work2/src/lmem.c
+lua-5.1-work2/src/lmem.h
+lua-5.1-work2/src/lobject.c
+lua-5.1-work2/src/lobject.h
+lua-5.1-work2/src/lopcodes.c
+lua-5.1-work2/src/lopcodes.h
+lua-5.1-work2/src/lparser.c
+lua-5.1-work2/src/lparser.h
+lua-5.1-work2/src/lstate.c
+lua-5.1-work2/src/lstate.h
+lua-5.1-work2/src/lstring.c
+lua-5.1-work2/src/lstring.h
+lua-5.1-work2/src/ltable.c
+lua-5.1-work2/src/ltable.h
+lua-5.1-work2/src/ltm.c
+lua-5.1-work2/src/ltm.h
+lua-5.1-work2/src/lua
+lua-5.1-work2/src/lua/Makefile
+lua-5.1-work2/src/lua/README
+lua-5.1-work2/src/lua/lua.c
+lua-5.1-work2/src/luac
+lua-5.1-work2/src/luac/Makefile
+lua-5.1-work2/src/luac/README
+lua-5.1-work2/src/luac/luac.c
+lua-5.1-work2/src/luac/print.c
+lua-5.1-work2/src/lundump.c
+lua-5.1-work2/src/lundump.h
+lua-5.1-work2/src/lvm.c
+lua-5.1-work2/src/lvm.h
+lua-5.1-work2/src/lzio.c
+lua-5.1-work2/src/lzio.h
+lua-5.1-work2/test
+lua-5.1-work2/test/README
+lua-5.1-work2/test/bisect.lua
+lua-5.1-work2/test/cf.lua
+lua-5.1-work2/test/echo.lua
+lua-5.1-work2/test/env.lua
+lua-5.1-work2/test/factorial.lua
+lua-5.1-work2/test/fib.lua
+lua-5.1-work2/test/fibfor.lua
+lua-5.1-work2/test/globals.lua
+lua-5.1-work2/test/hello.lua
+lua-5.1-work2/test/life.lua
+lua-5.1-work2/test/lua
+lua-5.1-work2/test/luac
+lua-5.1-work2/test/luac.lua
+lua-5.1-work2/test/printf.lua
+lua-5.1-work2/test/readonly.lua
+lua-5.1-work2/test/sieve.lua
+lua-5.1-work2/test/sort.lua
+lua-5.1-work2/test/table.lua
+lua-5.1-work2/test/trace-calls.lua
+lua-5.1-work2/test/trace-globals.lua
+lua-5.1-work2/test/undefined.lua
+lua-5.1-work2/test/xd.lua
END OF MANIFEST
diff --git a/include/lua.h b/include/lua.h
index e513dc3c..ac5435e7 100644
--- a/include/lua.h
+++ b/include/lua.h
@@ -1,5 +1,5 @@
/*
-** $Id: lua.h,v 1.192 2004/06/04 15:30:53 roberto Exp $
+** $Id: lua.h,v 1.193 2004/09/15 20:39:42 roberto Exp $
** Lua - An Extensible Extension Language
** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
** http://www.lua.org mailto:info@lua.org
@@ -17,7 +17,7 @@
#include "luaconf.h"
-#define LUA_VERSION "Lua 5.1 (work1)"
+#define LUA_VERSION "Lua 5.1 (work2)"
#define LUA_COPYRIGHT "Copyright (C) 1994-2004 Tecgraf, PUC-Rio"
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
@@ -37,11 +37,12 @@
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
-/* error codes for `lua_pcall' */
-#define LUA_ERRRUN 1
-#define LUA_ERRSYNTAX 2
-#define LUA_ERRMEM 3
-#define LUA_ERRERR 4
+/* 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;
@@ -165,6 +166,7 @@ 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);
/*
@@ -208,6 +210,7 @@ LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data);
*/
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
diff --git a/include/luaconf.h b/include/luaconf.h
index 56567c00..7ef4cedf 100644
--- a/include/luaconf.h
+++ b/include/luaconf.h
@@ -1,5 +1,5 @@
/*
-** $Id: luaconf.h,v 1.11 2004/08/30 18:35:14 roberto Exp $
+** $Id: luaconf.h,v 1.12 2004/09/10 17:30:46 roberto Exp $
** Configuration file for Lua
** See Copyright Notice in lua.h
*/
@@ -60,7 +60,7 @@
#define LUA_FIRSTINDEX 1
/* assertions in Lua (mainly for internal debugging) */
-#define lua_assert(c) /* empty */
+#define lua_assert(c) ((void)0)
/* }====================================================== */
@@ -123,7 +123,7 @@
#ifdef LUA_CORE
/* LUA-C API assertions */
-#define api_check(L, o) /* empty */
+#define api_check(L, o) lua_assert(o)
/* an unsigned integer with at least 32 bits */
diff --git a/src/lapi.c b/src/lapi.c
index eb2c266e..ce32f3b0 100644
--- a/src/lapi.c
+++ b/src/lapi.c
@@ -1,5 +1,5 @@
/*
-** $Id: lapi.c,v 2.18 2004/08/30 13:44:44 roberto Exp $
+** $Id: lapi.c,v 2.19 2004/09/15 20:39:42 roberto Exp $
** Lua API
** See Copyright Notice in lua.h
*/
@@ -97,6 +97,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
if (from == to) return;
lua_lock(to);
api_checknelems(from, n);
+ api_check(L, G(from) == G(to));
from->top -= n;
for (i = 0; i < n; i++) {
setobj2s(to, to->top, from->top + i);
@@ -479,6 +480,15 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
}
+LUA_API int lua_pushthread (lua_State *L) {
+ lua_lock(L);
+ setthvalue(L, L->top, L);
+ api_incr_top(L);
+ lua_unlock(L);
+ return (G(L)->mainthread == L);
+}
+
+
/*
** get functions (Lua -> stack)
@@ -650,7 +660,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
case LUA_TTABLE: {
hvalue(obj)->metatable = mt;
if (mt)
- luaC_objbarrier(L, hvalue(obj), mt);
+ luaC_objbarriert(L, hvalue(obj), mt);
break;
}
case LUA_TUSERDATA: {
@@ -816,6 +826,11 @@ LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
}
+LUA_API int lua_threadstatus (lua_State *L) {
+ return L->status;
+}
+
+
/*
** Garbage-collection function
*/
diff --git a/src/ldo.c b/src/ldo.c
index 46ad459a..ad9cbc3d 100644
--- a/src/ldo.c
+++ b/src/ldo.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldo.c,v 2.7 2004/06/02 19:07:55 roberto Exp $
+** $Id: ldo.c,v 2.10 2004/09/15 20:39:42 roberto Exp $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -93,7 +93,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
static void restore_stack_limit (lua_State *L) {
- L->stack_last = L->stack+L->stacksize-1;
+ lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
if (L->size_ci > LUA_MAXCALLS) { /* there was an overflow? */
int inuse = (L->ci - L->base_ci);
if (inuse + 1 < LUA_MAXCALLS) /* can `undo' overflow? */
@@ -121,9 +121,11 @@ static void correctstack (lua_State *L, TValue *oldstack) {
void luaD_reallocstack (lua_State *L, int newsize) {
TValue *oldstack = L->stack;
- luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
- L->stacksize = newsize;
- L->stack_last = L->stack+newsize-1-EXTRA_STACK;
+ int realsize = newsize + 1 + EXTRA_STACK;
+ lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
+ luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
+ L->stacksize = realsize;
+ L->stack_last = L->stack+newsize;
correctstack(L, oldstack);
}
@@ -133,7 +135,7 @@ void luaD_reallocCI (lua_State *L, int newsize) {
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
L->size_ci = cast(unsigned short, newsize);
L->ci = (L->ci - oldci) + L->base_ci;
- L->end_ci = L->base_ci + L->size_ci;
+ L->end_ci = L->base_ci + L->size_ci - 1;
}
@@ -141,11 +143,11 @@ void luaD_growstack (lua_State *L, int n) {
if (n <= L->stacksize) /* double size is enough? */
luaD_reallocstack(L, 2*L->stacksize);
else
- luaD_reallocstack(L, L->stacksize + n + EXTRA_STACK);
+ luaD_reallocstack(L, L->stacksize + n);
}
-static void luaD_growCI (lua_State *L) {
+static CallInfo *luaD_growCI (lua_State *L) {
if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */
luaD_throw(L, LUA_ERRERR);
else {
@@ -153,6 +155,7 @@ static void luaD_growCI (lua_State *L) {
if (L->size_ci > LUA_MAXCALLS)
luaG_runerror(L, "stack overflow");
}
+ return ++L->ci;
}
@@ -170,6 +173,7 @@ void luaD_callhook (lua_State *L, int event, int line) {
ar.i_ci = L->ci - L->base_ci;
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
L->ci->top = L->top + LUA_MINSTACK;
+ lua_assert(L->ci->top <= L->stack_last);
L->allowhook = 0; /* cannot call hooks inside a hook */
lua_unlock(L);
(*hook)(L, &ar);
@@ -193,6 +197,7 @@ static StkId adjust_varargs (lua_State *L, int nfixargs, int actual,
}
if (style != NEWSTYLEVARARG) { /* compatibility with old-style vararg */
int nvar = actual - nfixargs; /* number of extra arguments */
+ luaC_checkGC(L);
htab = luaH_new(L, nvar, 1); /* create `arg' table */
for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
setobj2n(L, luaH_setnum(L, htab, i+LUA_FIRSTINDEX), L->top - nvar + i);
@@ -231,31 +236,39 @@ static StkId tryfuncTM (lua_State *L, StkId func) {
}
+
+#define inc_ci(L) \
+ ((L->ci == L->end_ci) ? luaD_growCI(L) : \
+ (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
+
+
int luaD_precall (lua_State *L, StkId func, int nresults) {
LClosure *cl;
ptrdiff_t funcr;
if (!ttisfunction(func)) /* `func' is not a function? */
func = tryfuncTM(L, func); /* check the `function' tag method */
funcr = savestack(L, func);
- if (L->ci + 1 == L->end_ci) luaD_growCI(L);
- else condhardstacktests(luaD_reallocCI(L, L->size_ci));
cl = &clvalue(func)->l;
if (!cl->isC) { /* Lua function? prepare its call */
CallInfo *ci;
StkId st, base;
Proto *p = cl->p;
- luaD_checkstack(L, p->maxstacksize);
- func = restorestack(L, funcr);
if (p->is_vararg) { /* varargs? */
- int nargs = L->top - func - 1;
+ int nargs = L->top - restorestack(L, funcr) - 1;
+ luaD_checkstack(L, p->maxstacksize + nargs);
base = adjust_varargs(L, p->numparams, nargs, p->is_vararg);
+ func = restorestack(L, funcr);
}
- else
+ else {
+ luaD_checkstack(L, p->maxstacksize);
+ func = restorestack(L, funcr);
base = func + 1;
- ci = ++L->ci; /* now `enter' new function */
+ }
+ ci = inc_ci(L); /* now `enter' new function */
ci->func = func;
L->base = ci->base = base;
ci->top = L->base + p->maxstacksize;
+ lua_assert(ci->top <= L->stack_last);
ci->savedpc = p->code; /* starting point */
ci->tailcalls = 0;
ci->nresults = nresults;
@@ -268,10 +281,11 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
CallInfo *ci;
int n;
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
- ci = ++L->ci; /* now `enter' new function */
+ ci = inc_ci(L); /* now `enter' new function */
ci->func = restorestack(L, funcr);
L->base = ci->base = ci->func + 1;
ci->top = L->top + LUA_MINSTACK;
+ lua_assert(ci->top <= L->stack_last);
if (L->hookmask & LUA_MASKCALL)
luaD_callhook(L, LUA_HOOKCALL, -1);
lua_unlock(L);
@@ -344,7 +358,7 @@ static void resume (lua_State *L, void *ud) {
StkId firstResult;
int nargs = *cast(int *, ud);
CallInfo *ci = L->ci;
- if (!L->isSuspended) {
+ if (L->status != LUA_YIELD) {
lua_assert(ci == L->base_ci && nargs < L->top - L->base);
luaD_precall(L, L->top - (nargs + 1), LUA_MULTRET); /* start coroutine */
}
@@ -358,10 +372,11 @@ static void resume (lua_State *L, void *ud) {
if (nresults >= 0) L->top = L->ci->top;
} /* else yielded inside a hook: just continue its execution */
}
- L->isSuspended = 0;
+ L->status = 0;
firstResult = luaV_execute(L, L->ci - L->base_ci);
- if (firstResult != NULL) /* return? */
+ if (firstResult != NULL) { /* return? */
luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
+ }
}
@@ -379,25 +394,20 @@ LUA_API int lua_resume (lua_State *L, int nargs) {
lu_byte old_allowhooks;
lua_lock(L);
lua_assert(L->errfunc == 0 && L->nCcalls == 0);
- if (!L->isSuspended) {
- if (L->ci == L->base_ci) { /* no activation record? */
- if (nargs >= L->top - L->base)
- return resume_error(L, "cannot resume dead coroutine");
- }
- else
+ if (L->status != LUA_YIELD) {
+ if (L->status != 0)
+ return resume_error(L, "cannot resume dead coroutine");
+ else if (L->ci != L->base_ci)
return resume_error(L, "cannot resume non-suspended coroutine");
}
old_allowhooks = L->allowhook;
status = luaD_rawrunprotected(L, resume, &nargs);
if (status != 0) { /* error? */
- L->ci = L->base_ci; /* go back to initial level */
- L->base = L->ci->base;
- L->nCcalls = 0;
- luaF_close(L, L->base); /* close eventual pending closures */
- seterrorobj(L, status, L->base);
- L->allowhook = old_allowhooks;
- restore_stack_limit(L);
+ L->status = status; /* mark thread as `dead' */
+ seterrorobj(L, status, L->top);
}
+ else
+ status = L->status;
lua_unlock(L);
return status;
}
@@ -417,7 +427,7 @@ LUA_API int lua_yield (lua_State *L, int nresults) {
L->top = L->base + nresults;
}
} /* else it's an yield inside a hook: nothing to do */
- L->isSuspended = 1;
+ L->status = LUA_YIELD;
lua_unlock(L);
return -1;
}
diff --git a/src/ldo.h b/src/ldo.h
index d4072b60..ff5b652c 100644
--- a/src/ldo.h
+++ b/src/ldo.h
@@ -1,5 +1,5 @@
/*
-** $Id: ldo.h,v 2.2 2004/05/14 19:25:09 roberto Exp $
+** $Id: ldo.h,v 2.3 2004/09/08 14:23:09 roberto Exp $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -17,7 +17,7 @@
** macro to control inclusion of some hard tests on stack reallocation
*/
#ifndef HARDSTACKTESTS
-#define condhardstacktests(x) { /* empty */ }
+#define condhardstacktests(x) ((void)0)
#else
#define condhardstacktests(x) x
#endif
@@ -26,7 +26,7 @@
#define luaD_checkstack(L,n) \
if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \
luaD_growstack(L, n); \
- else condhardstacktests(luaD_reallocstack(L, L->stacksize));
+ else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1));
#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
diff --git a/src/lgc.c b/src/lgc.c
index 67a5f1f6..b3661fd7 100644
--- a/src/lgc.c
+++ b/src/lgc.c
@@ -1,5 +1,5 @@
/*
-** $Id: lgc.c,v 2.10 2004/08/30 13:44:44 roberto Exp $
+** $Id: lgc.c,v 2.12 2004/09/15 20:38:15 roberto Exp $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@@ -239,14 +239,17 @@ static void traverseclosure (global_State *g, Closure *cl) {
static void checkstacksizes (lua_State *L, StkId max) {
- int used = L->ci - L->base_ci; /* number of `ci' in use */
- if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
+ int ci_used = L->ci - L->base_ci; /* number of `ci' in use */
+ int s_used = max - L->stack; /* part of stack in use */
+ if (L->size_ci > LUA_MAXCALLS) /* handling overflow? */
+ return; /* do not touch the stacks */
+ if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
- else condhardstacktests(luaD_reallocCI(L, L->size_ci));
- used = max - L->stack; /* part of stack in use */
- if (4*used < L->stacksize && 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
+ condhardstacktests(luaD_reallocCI(L, ci_used + 1));
+ if (4*s_used < L->stacksize &&
+ 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
- else condhardstacktests(luaD_reallocstack(L, L->stacksize));
+ condhardstacktests(luaD_reallocstack(L, s_used));
}
@@ -496,7 +499,7 @@ void luaC_freeall (lua_State *L) {
/* mark root set */
static void markroot (lua_State *L) {
global_State *g = G(L);
- lua_assert(g->gray == NULL);
+ g->gray = NULL;
g->grayagain = NULL;
g->weak = NULL;
markobject(g, g->mainthread);
@@ -510,6 +513,7 @@ static void markroot (lua_State *L) {
static void remarkupvals (global_State *g) {
GCObject *o;
for (o = obj2gco(g->mainthread); o; o = o->gch.next) {
+ lua_assert(!isblack(o));
if (iswhite(o)) {
GCObject *curr;
for (curr = gco2th(o)->openupval; curr != NULL; curr = curr->gch.next) {
@@ -526,7 +530,8 @@ static void remarkupvals (global_State *g) {
static void atomic (lua_State *L) {
global_State *g = G(L);
int aux;
- lua_assert(g->gray == NULL);
+ /* remark objects cautch by write barrier */
+ propagateall(g);
/* remark occasional upvalues of (maybe) dead threads */
remarkupvals(g);
/* remark weak tables */
@@ -666,10 +671,12 @@ void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
global_State *g = G(L);
lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
lua_assert(g->gcgenerational || g->gcstate != GCSfinalize);
- if (g->gcstate != GCSpropagate) /* sweeping phases? */
- black2gray(o); /* just mark as gray to avoid other barriers */
- else /* breaking invariant! */
- reallymarkobject(g, v); /* restore it */
+ lua_assert(ttype(&o->gch) != LUA_TTABLE);
+ /* must keep invariant? */
+ if (g->gcstate == GCSpropagate || g->gcgenerational)
+ reallymarkobject(g, v); /* restore invariant */
+ else /* don't mind */
+ makewhite(g, o); /* mark as white just to avoid other barriers */
}
diff --git a/src/lgc.h b/src/lgc.h
index 47a5e932..81f6da5f 100644
--- a/src/lgc.h
+++ b/src/lgc.h
@@ -1,5 +1,5 @@
/*
-** $Id: lgc.h,v 2.8 2004/08/30 13:44:44 roberto Exp $
+** $Id: lgc.h,v 2.9 2004/09/15 20:38:15 roberto Exp $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@@ -86,6 +86,10 @@
{ if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
+#define luaC_objbarriert(L,p,o) \
+ { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
+ luaC_barrierback(L,obj2gco(p),obj2gco(o)); }
+
size_t luaC_separateudata (lua_State *L, int all);
void luaC_callGCTM (lua_State *L);
void luaC_freeall (lua_State *L);
diff --git a/src/lib/lauxlib.c b/src/lib/lauxlib.c
index cd4e75f8..7c14d001 100644
--- a/src/lib/lauxlib.c
+++ b/src/lib/lauxlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.c,v 1.123 2004/08/30 18:35:14 roberto Exp $
+** $Id: lauxlib.c,v 1.124 2004/09/03 13:17:14 roberto Exp $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -632,6 +632,7 @@ LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
+ (void)osize;
if (nsize == 0) {
free(ptr);
return NULL;
diff --git a/src/lib/lbaselib.c b/src/lib/lbaselib.c
index f734f358..82a8f509 100644
--- a/src/lib/lbaselib.c
+++ b/src/lib/lbaselib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lbaselib.c,v 1.156 2004/08/30 18:35:14 roberto Exp $
+** $Id: lbaselib.c,v 1.158 2004/09/15 20:39:42 roberto Exp $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -273,25 +273,25 @@ static int luaB_loadfile (lua_State *L) {
}
-struct Aux_load {
- int func;
- int res;
-};
-
-
+/*
+** Reader for generic `load' function: `lua_load' uses the
+** stack for internal stuff, so the reader cannot change the
+** stack top. Instead, it keeps its resulting string in a
+** reserved slot inside the stack.
+*/
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
- struct Aux_load *al = (struct Aux_load *)ud;
- luaL_unref(L, al->res, LUA_REGISTRYINDEX);
- lua_getref(L, al->func);
- lua_call(L, 0, 1);
+ luaL_checkstack(L, 2, "too many nested functions");
+ lua_pushvalue(L, 1); /* get function */
+ lua_call(L, 0, 1); /* call it */
if (lua_isnil(L, -1)) {
*size = 0;
return NULL;
}
else if (lua_isstring(L, -1)) {
- const char *res = lua_tostring(L, -1);
- *size = lua_strlen(L, -1);
- al->res = luaL_ref(L, LUA_REGISTRYINDEX);
+ const char *res;
+ lua_replace(L, 3); /* save string in a reserved stack slot */
+ res = lua_tostring(L, 3);
+ *size = lua_strlen(L, 3);
return res;
}
else luaL_error(L, "reader function must return a string");
@@ -300,16 +300,11 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
static int luaB_load (lua_State *L) {
- struct Aux_load al;
int status;
const char *cname = luaL_optstring(L, 2, "=(load)");
luaL_checktype(L, 1, LUA_TFUNCTION);
- lua_settop(L, 1);
- al.func = luaL_ref(L, LUA_REGISTRYINDEX);
- al.res = LUA_REFNIL;
- status = lua_load(L, generic_reader, &al, cname);
- luaL_unref(L, al.func, LUA_REGISTRYINDEX);
- luaL_unref(L, al.res, LUA_REGISTRYINDEX);
+ lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
+ status = lua_load(L, generic_reader, NULL, cname);
return load_aux(L, status);
}
@@ -528,9 +523,13 @@ static int auxresume (lua_State *L, lua_State *co, int narg) {
int status;
if (!lua_checkstack(co, narg))
luaL_error(L, "too many arguments to resume");
+ if (lua_threadstatus(co) == 0 && lua_gettop(co) == 0) {
+ lua_pushliteral(L, "cannot resume dead coroutine");
+ return -1; /* error flag */
+ }
lua_xmove(L, co, narg);
status = lua_resume(co, narg);
- if (status == 0) {
+ if (status == 0 || status == LUA_YIELD) {
int nres = lua_gettop(co);
if (!lua_checkstack(L, nres))
luaL_error(L, "too many results to resume");
@@ -604,22 +603,44 @@ static int luaB_costatus (lua_State *L) {
luaL_argcheck(L, co, 1, "coroutine expected");
if (L == co) lua_pushliteral(L, "running");
else {
- lua_Debug ar;
- if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0)
- lua_pushliteral(L, "dead");
- else
- lua_pushliteral(L, "suspended");
+ switch (lua_threadstatus(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_cocurrent (lua_State *L) {
+ if (lua_pushthread(L))
+ return 0; /* main thread is not a coroutine */
+ else
+ return 1;
+}
+
+
static const luaL_reg co_funcs[] = {
{"create", luaB_cocreate},
{"wrap", luaB_cowrap},
{"resume", luaB_coresume},
{"yield", luaB_yield},
{"status", luaB_costatus},
+ {"current", luaB_cocurrent},
{NULL, NULL}
};
diff --git a/src/llimits.h b/src/llimits.h
index ea0caf7b..95a51a0b 100644
--- a/src/llimits.h
+++ b/src/llimits.h
@@ -1,5 +1,5 @@
/*
-** $Id: llimits.h,v 1.59 2004/06/23 15:57:29 roberto Exp $
+** $Id: llimits.h,v 1.60 2004/09/10 17:30:46 roberto Exp $
** Limits, basic types, and some other `installation-dependent' definitions
** See Copyright Notice in lua.h
*/
@@ -63,9 +63,7 @@ typedef LUSER_ALIGNMENT_T L_Umaxalign;
typedef LUA_UACNUMBER l_uacNumber;
-#ifndef check_exp
-#define check_exp(c,e) (e)
-#endif
+#define check_exp(c,e) (lua_assert(c), (e))
#ifndef UNUSED
diff --git a/src/lstate.c b/src/lstate.c
index e0a439b1..9f55f997 100644
--- a/src/lstate.c
+++ b/src/lstate.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.c,v 2.12 2004/08/30 13:44:44 roberto Exp $
+** $Id: lstate.c,v 2.14 2004/09/15 20:39:42 roberto Exp $
** Global State
** See Copyright Notice in lua.h
*/
@@ -62,7 +62,7 @@ static void stack_init (lua_State *L1, lua_State *L) {
L1->base = L1->ci->base = L1->top;
L1->ci->top = L1->top + LUA_MINSTACK;
L1->size_ci = BASIC_CI_SIZE;
- L1->end_ci = L1->base_ci + L1->size_ci;
+ L1->end_ci = L1->base_ci + L1->size_ci - 1;
}
@@ -114,7 +114,7 @@ static void preinit_state (lua_State *L, global_State *g) {
L->openupval = NULL;
L->size_ci = 0;
L->nCcalls = 0;
- L->isSuspended = 0;
+ L->status = 0;
L->base_ci = L->ci = NULL;
L->errfunc = 0;
setnilvalue(gt(L));
diff --git a/src/lstate.h b/src/lstate.h
index 0a178fef..d7583d27 100644
--- a/src/lstate.h
+++ b/src/lstate.h
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.h,v 2.7 2004/08/30 13:44:44 roberto Exp $
+** $Id: lstate.h,v 2.8 2004/09/15 20:39:42 roberto Exp $
** Global State
** See Copyright Notice in lua.h
*/
@@ -111,7 +111,7 @@ struct lua_State {
unsigned short nCcalls; /* number of nested C calls */
lu_byte hookmask;
lu_byte allowhook;
- lu_byte isSuspended;
+ lu_byte status;
int basehookcount;
int hookcount;
lua_Hook hook;
diff --git a/src/lua/README b/src/lua/README
index 1bd07af6..525f6c15 100644
--- a/src/lua/README
+++ b/src/lua/README
@@ -8,13 +8,19 @@ Usage: lua [options] [script [args]]. Available options are:
-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 (almost) unmodified, as follows:
+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)
@@ -22,11 +28,14 @@ then you can use this interpreter (almost) unmodified, as follows:
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) to be "openstdlibs(L)+myinit(L)".
- Here, openstdlibs is a function in lua.c that 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.
+* 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).
-* Finally, remember to link your C code when building lua.
+Just remember to link your C code when building lua!
-For other customizations, see ../../etc/config.c.
+For other customizations, see ../../include/luaconf.h .
diff --git a/src/lvm.c b/src/lvm.c
index 05af9df1..94f98147 100644
--- a/src/lvm.c
+++ b/src/lvm.c
@@ -1,5 +1,5 @@
/*
-** $Id: lvm.c,v 2.13 2004/08/12 14:19:51 roberto Exp $
+** $Id: lvm.c,v 2.14 2004/09/15 20:39:42 roberto Exp $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -398,7 +398,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
traceexec(L, pc); /***/
- if (L->isSuspended) { /* did hook yield? */
+ if (L->status == LUA_YIELD) { /* did hook yield? */
L->ci->savedpc = pc - 1;
return NULL;
}