Compare commits

..

57 Commits

Author SHA1 Message Date
Roberto Ierusalimschy
523c5d8e1c last changes by lhf. 2000-11-07 10:44:44 -02:00
Roberto Ierusalimschy
40a4c76773 setlinehook/setcallhook return old hook 2000-11-06 15:58:38 -02:00
Roberto Ierusalimschy
1385d81d20 2000-11-06 11:45:18 -02:00
Roberto Ierusalimschy
906d5dcc41 details 2000-11-06 11:45:18 -02:00
Roberto Ierusalimschy
93a5649d40 hooks may call `error' to break a chunk. 2000-11-06 11:19:08 -02:00
Roberto Ierusalimschy
df416661cc many changes 2000-10-31 16:20:01 -02:00
Roberto Ierusalimschy
67c1afff59 lua_settagmethod does not return old tag method 2000-10-31 11:10:24 -02:00
Roberto Ierusalimschy
03770ecfc9 comments 2000-10-31 10:44:07 -02:00
Roberto Ierusalimschy
7409678b5d DEBUG renamed to LUA_DEBUG 2000-10-30 15:50:00 -02:00
Roberto Ierusalimschy
d1c689af40 subtelties in layout of TString 2000-10-30 15:49:19 -02:00
Roberto Ierusalimschy
37e9c2e744 macro DEBUG renamed to LUA_DEBUG 2000-10-30 14:29:59 -02:00
Roberto Ierusalimschy
e42a219eeb `lua_typename' accepts LUA_TNONE 2000-10-30 11:07:48 -02:00
Roberto Ierusalimschy
b3959d58ff 2000-10-30 10:50:09 -02:00
Roberto Ierusalimschy
f379d06e24 all API functions are declared in a single line (to facilitate pre-processing). 2000-10-30 10:38:50 -02:00
Roberto Ierusalimschy
728ff20701 details 2000-10-30 10:20:29 -02:00
Roberto Ierusalimschy
2cbbf3933a new macro LUALIB_API (so the lib can be a separate DLL) 2000-10-27 14:15:53 -02:00
Roberto Ierusalimschy
e2b6b7de1b details 2000-10-27 09:39:52 -02:00
Roberto Ierusalimschy
563b1f5704 new way to ensure uniqueness of registry keys 2000-10-26 17:04:22 -02:00
Roberto Ierusalimschy
4670476584 details. 2000-10-26 16:44:26 -02:00
Roberto Ierusalimschy
89f98c0995 in function `read_file', realloc() doesn't free the buffer if it can't
allocate new memory
2000-10-26 10:53:55 -02:00
Roberto Ierusalimschy
b892f0a877 new API function `createuserdata' 2000-10-26 10:47:05 -02:00
Roberto Ierusalimschy
aadc35449e lua_settag does not pop its argument 2000-10-24 17:19:15 -02:00
Roberto Ierusalimschy
cdc8139e29 registry mechanism 2000-10-24 17:12:06 -02:00
Roberto Ierusalimschy
e833bd47c9 comments 2000-10-24 14:05:59 -02:00
Roberto Ierusalimschy
b7ffb128cb it is unsafe to allow Lua to get gc tag methods 2000-10-20 14:57:42 -02:00
Roberto Ierusalimschy
64eecc0b82 new macro LUA_API 2000-10-20 14:39:03 -02:00
Roberto Ierusalimschy
8b88ab07f7 more controled use of `sprintf' 2000-10-20 14:36:32 -02:00
Roberto Ierusalimschy
2779ceeb12 ((n)-1) may be negative (-1), and so it is not wise to keep it as unsigned 2000-10-18 15:19:09 -02:00
Roberto Ierusalimschy
e93c4547fe no more USERINIT (macro) 2000-10-17 11:36:24 -02:00
Roberto Ierusalimschy
0ffc676ce7 details 2000-10-11 14:47:50 -02:00
Roberto Ierusalimschy
18fb3ddb89 details. 2000-10-10 17:52:58 -02:00
Roberto Ierusalimschy
2bddbe6603 cannot change filename' before eventual call to freopen' 2000-10-10 17:51:39 -02:00
Roberto Ierusalimschy
e323338fd0 `nlineinfo' must include end flag. 2000-10-10 17:51:15 -02:00
Roberto Ierusalimschy
46b543ebef better treatment for errors inside _ERRORMETHOD 2000-10-09 13:46:43 -02:00
Roberto Ierusalimschy
79909a92e1 details 2000-10-09 11:47:46 -02:00
Roberto Ierusalimschy
d6232a0b2e better treatment for source names 2000-10-09 11:47:32 -02:00
Roberto Ierusalimschy
ae63a0e692 new implementation for some API functions 2000-10-06 16:29:26 -03:00
Roberto Ierusalimschy
cd3d446957 detail 2000-10-06 16:28:47 -03:00
Roberto Ierusalimschy
f01e6c6f1d small optimization in `sort' 2000-10-06 16:13:29 -03:00
Roberto Ierusalimschy
ad3816d0d1 luaD_call is more uniform 2000-10-06 09:45:25 -03:00
Roberto Ierusalimschy
046a3d6173 tag methods are always functions, so don't need to store a whole object 2000-10-05 10:00:17 -03:00
Roberto Ierusalimschy
001f2bdd0e new definition for types-tags 2000-10-05 09:14:08 -03:00
Roberto Ierusalimschy
cd2ddaded9 call hooks can only be called when `pc' is active (that is, inside
`execute'...)
2000-10-04 09:16:08 -03:00
Roberto Ierusalimschy
d68209e822 details. 2000-10-03 11:27:44 -03:00
Roberto Ierusalimschy
1088cde03c easier way to change type Number 2000-10-03 11:03:21 -03:00
Roberto Ierusalimschy
6759f3ec5e no more `proprietary' convertion algorithm (too complex) 2000-10-03 11:03:21 -03:00
Roberto Ierusalimschy
f6834f4393 new API function `lua_type' + new type lua_Type 2000-10-02 17:10:55 -03:00
Roberto Ierusalimschy
78bc8e553d new API for garbage collector 2000-10-02 11:47:43 -03:00
Roberto Ierusalimschy
dad808a73a new way to count `nblocks' for GC (try to count bytes). 2000-09-29 09:42:13 -03:00
Roberto Ierusalimschy
ca7fd50a4e small optimizations 2000-09-29 09:40:56 -03:00
Roberto Ierusalimschy
282ab366f4 bug: parser overwrites semantic information when looking ahead 2000-09-27 14:41:58 -03:00
Roberto Ierusalimschy
444d6a106b lua_tag should return LUA_NOTAG for non-valid indices 2000-09-27 09:51:39 -03:00
Roberto Ierusalimschy
13635f7de7 new version of protected execution 2000-09-25 13:22:42 -03:00
Roberto Ierusalimschy
d8a442206d lua_state (now called `L') should not be global 2000-09-25 13:15:52 -03:00
Roberto Ierusalimschy
c9c6f9747c GC may crash when checking C closures 2000-09-25 11:52:10 -03:00
Roberto Ierusalimschy
c2aa7bd72d bug: lua_gettable does not get key from stack top 2000-09-25 11:48:42 -03:00
Roberto Ierusalimschy
f9dd50cefc `read("*w")' should return nil at EOF 2000-09-22 15:14:06 -03:00
47 changed files with 2081 additions and 1760 deletions

37
bugs
View File

@@ -198,3 +198,40 @@ Tue Aug 29 15:57:41 EST 2000
>> gc tag method for nil could call line hook
(by ry; since ?)
=================================================================
--- Version 4.0 Beta
** liolib.c
Fri Sep 22 15:12:37 EST 2000
>> `read("*w")' should return nil at EOF
(by roberto; since 4.0b)
** lvm.c
Mon Sep 25 11:47:48 EST 2000
>> lua_gettable does not get key from stack top
(by Philip Yi; since 4.0b)
** lgc.c
Mon Sep 25 11:50:48 EST 2000
>> GC may crash when checking locked C closures
(by Philip Yi; since 4.0b)
** lapi.c
Wed Sep 27 09:50:19 EST 2000
>> lua_tag should return LUA_NOTAG for non-valid indices
(by Paul Hankin; since 4.0b)
** llex.h / llex.c / lparser.c
Wed Sep 27 13:39:45 EST 2000
>> parser overwrites semantic information when looking ahead
>> (e.g. «a = {print'foo'}»)
(by Edgar Toernig; since 4.0b, deriving from previous bug)
** liolib.c
Thu Oct 26 10:50:46 EDT 2000
>> in function `read_file', realloc() doesn't free the buffer if it can't
>> allocate new memory
(by Mauro Vezzosi; since 4.0b)

274
lapi.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.98 2000/09/14 14:09:31 roberto Exp roberto $
** $Id: lapi.c,v 1.109 2000/10/26 12:47:05 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@@ -39,12 +39,22 @@ TObject *luaA_index (lua_State *L, int index) {
}
static TObject *luaA_indexAcceptable (lua_State *L, int index) {
if (index >= 0) {
TObject *o = L->Cbase+(index-1);
if (o >= L->top) return NULL;
else return o;
}
else return L->top+index;
}
void luaA_pushobject (lua_State *L, const TObject *o) {
*L->top = *o;
incr_top;
}
int lua_stackspace (lua_State *L) {
LUA_API int lua_stackspace (lua_State *L) {
return (L->stack_last - L->top);
}
@@ -55,12 +65,12 @@ int lua_stackspace (lua_State *L) {
*/
int lua_gettop (lua_State *L) {
LUA_API int lua_gettop (lua_State *L) {
return (L->top - L->Cbase);
}
void lua_settop (lua_State *L, int index) {
LUA_API void lua_settop (lua_State *L, int index) {
if (index >= 0)
luaD_adjusttop(L, L->Cbase, index);
else
@@ -68,15 +78,15 @@ void lua_settop (lua_State *L, int index) {
}
void lua_remove (lua_State *L, int index) {
StkId p = Index(L, index);
LUA_API void lua_remove (lua_State *L, int index) {
StkId p = luaA_index(L, index);
while (++p < L->top) *(p-1) = *p;
L->top--;
}
void lua_insert (lua_State *L, int index) {
StkId p = Index(L, index);
LUA_API void lua_insert (lua_State *L, int index) {
StkId p = luaA_index(L, index);
StkId q;
for (q = L->top; q>p; q--)
*q = *(q-1);
@@ -84,8 +94,8 @@ void lua_insert (lua_State *L, int index) {
}
void lua_pushvalue (lua_State *L, int index) {
*L->top = *Index(L, index);
LUA_API void lua_pushvalue (lua_State *L, int index) {
*L->top = *luaA_index(L, index);
api_incr_top(L);
}
@@ -96,87 +106,87 @@ void lua_pushvalue (lua_State *L, int index) {
*/
#define btest(L,i,value,default) { \
StkId o; \
if ((i) >= 0) { \
o = L->Cbase+((i)-1); \
if (o >= L->top) return (default); \
} \
else o = L->top+(i); \
return (value); }
#define access(L,i,test,default,value) { \
StkId o; \
if ((i) >= 0) { \
o = L->Cbase+((i)-1); \
if (o >= L->top) return (default); \
} \
else o = L->top+(i); \
return ((test) ? (value) : (default)); }
const char *lua_type (lua_State *L, int index) {
btest(L, index, luaO_typename(o), "NO VALUE");
LUA_API int lua_type (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL) ? LUA_TNONE : ttype(o);
}
int lua_iscfunction (lua_State *L, int index) {
btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
LUA_API const char *lua_typename (lua_State *L, int t) {
UNUSED(L);
return (t == LUA_TNONE) ? "no value" : luaO_typenames[t];
}
int lua_isnumber (lua_State *L, int index) {
btest(L, index, (tonumber(Index(L, index)) == 0), 0);
LUA_API int lua_iscfunction (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL) ? 0 : iscfunction(o);
}
int lua_tag (lua_State *L, int index) {
btest(L, index,
((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
luaT_effectivetag(L, o)), -1);
LUA_API int lua_isnumber (lua_State *L, int index) {
TObject *o = luaA_indexAcceptable(L, index);
return (o == NULL) ? 0 : (tonumber(o) == 0);
}
int lua_equal (lua_State *L, int index1, int index2) {
StkId o1 = Index(L, index1);
StkId o2 = Index(L, index2);
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
LUA_API int lua_isstring (lua_State *L, int index) {
int t = lua_type(L, index);
return (t == LUA_TSTRING || t == LUA_TNUMBER);
}
LUA_API int lua_tag (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL) ? LUA_NOTAG : luaT_tag(o);
}
LUA_API int lua_equal (lua_State *L, int index1, int index2) {
StkId o1 = luaA_indexAcceptable(L, index1);
StkId o2 = luaA_indexAcceptable(L, index2);
if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
else return luaO_equalObj(o1, o2);
}
int lua_lessthan (lua_State *L, int index1, int index2) {
StkId o1 = Index(L, index1);
StkId o2 = Index(L, index2);
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
StkId o1 = luaA_indexAcceptable(L, index1);
StkId o2 = luaA_indexAcceptable(L, index2);
if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
else return luaV_lessthan(L, o1, o2, L->top);
}
double lua_tonumber (lua_State *L, int index) {
access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
LUA_API double lua_tonumber (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL || tonumber(o)) ? 0 : nvalue(o);
}
const char *lua_tostring (lua_State *L, int index) {
luaC_checkGC(L); /* `tostring' may create a new string */
access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
LUA_API const char *lua_tostring (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL || tostring(L, o)) ? NULL : svalue(o);
}
size_t lua_strlen (lua_State *L, int index) {
access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
LUA_API size_t lua_strlen (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
}
lua_CFunction lua_tocfunction (lua_State *L, int index) {
access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
}
void *lua_touserdata (lua_State *L, int index) {
access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
LUA_API void *lua_touserdata (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
tsvalue(o)->u.d.value;
}
const void *lua_topointer (lua_State *L, int index) {
StkId o = Index(L, index);
LUA_API const void *lua_topointer (lua_State *L, int index) {
StkId o = luaA_indexAcceptable(L, index);
if (o == NULL) return NULL;
switch (ttype(o)) {
case TAG_TABLE:
case LUA_TTABLE:
return hvalue(o);
case TAG_CCLOSURE: case TAG_LCLOSURE:
case LUA_TFUNCTION:
return clvalue(o);
default: return NULL;
}
@@ -189,28 +199,27 @@ const void *lua_topointer (lua_State *L, int index) {
*/
void lua_pushnil (lua_State *L) {
ttype(L->top) = TAG_NIL;
LUA_API void lua_pushnil (lua_State *L) {
ttype(L->top) = LUA_TNIL;
api_incr_top(L);
}
void lua_pushnumber (lua_State *L, double n) {
ttype(L->top) = TAG_NUMBER;
LUA_API void lua_pushnumber (lua_State *L, double n) {
nvalue(L->top) = n;
ttype(L->top) = LUA_TNUMBER;
api_incr_top(L);
}
void lua_pushlstring (lua_State *L, const char *s, size_t len) {
luaC_checkGC(L);
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
tsvalue(L->top) = luaS_newlstr(L, s, len);
ttype(L->top) = TAG_STRING;
ttype(L->top) = LUA_TSTRING;
api_incr_top(L);
}
void lua_pushstring (lua_State *L, const char *s) {
LUA_API void lua_pushstring (lua_State *L, const char *s) {
if (s == NULL)
lua_pushnil(L);
else
@@ -218,18 +227,17 @@ void lua_pushstring (lua_State *L, const char *s) {
}
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
luaC_checkGC(L);
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
luaV_Cclosure(L, fn, n);
}
void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
luaC_checkGC(L);
if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
/* ORDER LUA_T */
if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag)))
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
tsvalue(L->top) = luaS_createudata(L, u, tag);
ttype(L->top) = TAG_USERDATA;
ttype(L->top) = LUA_TUSERDATA;
api_incr_top(L);
}
@@ -240,7 +248,7 @@ void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
*/
void lua_getglobal (lua_State *L, const char *name) {
LUA_API void lua_getglobal (lua_State *L, const char *name) {
StkId top = L->top;
*top = *luaV_getglobal(L, luaS_new(L, name));
L->top = top;
@@ -248,7 +256,7 @@ void lua_getglobal (lua_State *L, const char *name) {
}
void lua_gettable (lua_State *L, int index) {
LUA_API void lua_gettable (lua_State *L, int index) {
StkId t = Index(L, index);
StkId top = L->top;
*(top-1) = *luaV_gettable(L, t);
@@ -256,31 +264,31 @@ void lua_gettable (lua_State *L, int index) {
}
void lua_rawget (lua_State *L, int index) {
LUA_API void lua_rawget (lua_State *L, int index) {
StkId t = Index(L, index);
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
*(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
}
void lua_rawgeti (lua_State *L, int index, int n) {
LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
StkId o = Index(L, index);
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
*L->top = *luaH_getnum(hvalue(o), n);
api_incr_top(L);
}
void lua_getglobals (lua_State *L) {
LUA_API void lua_getglobals (lua_State *L) {
hvalue(L->top) = L->gt;
ttype(L->top) = TAG_TABLE;
ttype(L->top) = LUA_TTABLE;
api_incr_top(L);
}
int lua_getref (lua_State *L, int ref) {
LUA_API int lua_getref (lua_State *L, int ref) {
if (ref == LUA_REFNIL)
ttype(L->top) = TAG_NIL;
ttype(L->top) = LUA_TNIL;
else if (0 <= ref && ref < L->refSize &&
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
*L->top = L->refArray[ref].o;
@@ -291,10 +299,9 @@ int lua_getref (lua_State *L, int ref) {
}
void lua_newtable (lua_State *L) {
luaC_checkGC(L);
LUA_API void lua_newtable (lua_State *L) {
hvalue(L->top) = luaH_new(L, 0);
ttype(L->top) = TAG_TABLE;
ttype(L->top) = LUA_TTABLE;
api_incr_top(L);
}
@@ -305,14 +312,14 @@ void lua_newtable (lua_State *L) {
*/
void lua_setglobal (lua_State *L, const char *name) {
LUA_API void lua_setglobal (lua_State *L, const char *name) {
StkId top = L->top;
luaV_setglobal(L, luaS_new(L, name));
L->top = top-1; /* remove element from the top */
}
void lua_settable (lua_State *L, int index) {
LUA_API void lua_settable (lua_State *L, int index) {
StkId t = Index(L, index);
StkId top = L->top;
luaV_settable(L, t, top-2);
@@ -320,32 +327,32 @@ void lua_settable (lua_State *L, int index) {
}
void lua_rawset (lua_State *L, int index) {
LUA_API void lua_rawset (lua_State *L, int index) {
StkId t = Index(L, index);
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
*luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
L->top -= 2;
}
void lua_rawseti (lua_State *L, int index, int n) {
LUA_API void lua_rawseti (lua_State *L, int index, int n) {
StkId o = Index(L, index);
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected");
*luaH_setint(L, hvalue(o), n) = *(L->top-1);
L->top--;
}
void lua_setglobals (lua_State *L) {
LUA_API void lua_setglobals (lua_State *L) {
StkId newtable = --L->top;
LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected");
L->gt = hvalue(newtable);
}
int lua_ref (lua_State *L, int lock) {
LUA_API int lua_ref (lua_State *L, int lock) {
int ref;
if (ttype(L->top-1) == TAG_NIL)
if (ttype(L->top-1) == LUA_TNIL)
ref = LUA_REFNIL;
else {
if (L->refFree != NONEXT) { /* is there a free place? */
@@ -355,6 +362,7 @@ int lua_ref (lua_State *L, int lock) {
else { /* no more free places */
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
"reference table overflow", MAX_INT);
L->nblocks += sizeof(struct Ref);
ref = L->refSize++;
}
L->refArray[ref].o = *(L->top-1);
@@ -370,33 +378,57 @@ int lua_ref (lua_State *L, int lock) {
** (most of them are in ldo.c)
*/
void lua_rawcall (lua_State *L, int nargs, int nresults) {
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
luaD_call(L, L->top-(nargs+1), nresults);
}
/*
** Garbage-collection functions
*/
/* GC values are expressed in Kbytes: #bytes/2^10 */
#define GCscale(x) ((int)((x)>>10))
#define GCunscale(x) ((unsigned long)(x)<<10)
LUA_API int lua_getgcthreshold (lua_State *L) {
return GCscale(L->GCthreshold);
}
LUA_API int lua_getgccount (lua_State *L) {
return GCscale(L->nblocks);
}
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
if (newthreshold > GCscale(ULONG_MAX))
L->GCthreshold = ULONG_MAX;
else
L->GCthreshold = GCunscale(newthreshold);
luaC_checkGC(L);
}
/*
** miscellaneous functions
*/
void lua_settag (lua_State *L, int tag) {
LUA_API void lua_settag (lua_State *L, int tag) {
luaT_realtag(L, tag);
switch (ttype(L->top-1)) {
case TAG_TABLE:
case LUA_TTABLE:
hvalue(L->top-1)->htag = tag;
break;
case TAG_USERDATA:
case LUA_TUSERDATA:
tsvalue(L->top-1)->u.d.tag = tag;
break;
default:
luaO_verror(L, "cannot change the tag of a %.20s",
luaO_typename(L->top-1));
}
L->top--;
}
void lua_unref (lua_State *L, int ref) {
LUA_API void lua_unref (lua_State *L, int ref) {
if (ref >= 0) {
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
L->refArray[ref].st = L->refFree;
@@ -405,11 +437,11 @@ void lua_unref (lua_State *L, int ref) {
}
int lua_next (lua_State *L, int index) {
StkId t = Index(L, index);
LUA_API int lua_next (lua_State *L, int index) {
StkId t = luaA_index(L, index);
Node *n;
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
n = luaH_next(L, hvalue(t), Index(L, -1));
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
n = luaH_next(L, hvalue(t), luaA_index(L, -1));
if (n) {
*(L->top-1) = *key(n);
*L->top = *val(n);
@@ -423,18 +455,18 @@ int lua_next (lua_State *L, int index) {
}
int lua_getn (lua_State *L, int index) {
Hash *h = hvalue(Index(L, index));
LUA_API int lua_getn (lua_State *L, int index) {
Hash *h = hvalue(luaA_index(L, index));
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
if (ttype(value) == TAG_NUMBER)
if (ttype(value) == LUA_TNUMBER)
return (int)nvalue(value);
else {
Number max = 0;
int i = h->size;
Node *n = h->node;
while (i--) {
if (ttype(key(n)) == TAG_NUMBER &&
ttype(val(n)) != TAG_NIL &&
if (ttype(key(n)) == LUA_TNUMBER &&
ttype(val(n)) != LUA_TNIL &&
nvalue(key(n)) > max)
max = nvalue(key(n));
n++;
@@ -444,9 +476,19 @@ int lua_getn (lua_State *L, int index) {
}
void lua_concat (lua_State *L, int n) {
LUA_API void lua_concat (lua_State *L, int n) {
StkId top = L->top;
luaV_strconc(L, n, top);
L->top = top-(n-1);
luaC_checkGC(L);
}
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
TString *ts = luaS_newudata(L, size, NULL);
tsvalue(L->top) = ts;
ttype(L->top) = LUA_TUSERDATA;
api_incr_top(L);
return ts->u.d.value;
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.35 2000/09/11 20:29:27 roberto Exp roberto $
** $Id: lauxlib.c,v 1.42 2000/10/30 12:38:50 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -21,7 +21,7 @@
int luaL_findstring (const char *name, const char *const list[]) {
LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
int i;
for (i=0; list[i]; i++)
if (strcmp(list[i], name) == 0)
@@ -29,7 +29,7 @@ int luaL_findstring (const char *name, const char *const list[]) {
return -1; /* name not found */
}
void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
LUALIB_API void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
lua_Debug ar;
lua_getstack(L, 0, &ar);
lua_getinfo(L, "n", &ar);
@@ -40,75 +40,72 @@ void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
}
static void type_error (lua_State *L, int narg, const char *type_name) {
char buff[100];
const char *rt = lua_type(L, narg);
if (*rt == 'N') rt = "no value";
sprintf(buff, "%.10s expected, got %.10s", type_name, rt);
static void type_error (lua_State *L, int narg, int t) {
char buff[50];
sprintf(buff, "%.8s expected, got %.8s", lua_typename(L, t),
lua_typename(L, lua_type(L, narg)));
luaL_argerror(L, narg, buff);
}
void luaL_checkstack (lua_State *L, int space, const char *mes) {
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
if (space > lua_stackspace(L))
luaL_verror(L, "stack overflow (%.30s)", mes);
}
/*
** use the 3rd letter of type names for testing:
** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
*/
void luaL_checktype(lua_State *L, int narg, const char *tname) {
const char *rt = lua_type(L, narg);
if (!(*rt != 'N' && (tname[2] == 'y' || tname[2] == rt[2])))
type_error(L, narg, tname);
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
if (lua_type(L, narg) != t)
type_error(L, narg, t);
}
static const char *checkstr (lua_State *L, int narg, size_t *len) {
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
if (lua_type(L, narg) == LUA_TNONE)
luaL_argerror(L, narg, "value expected");
}
LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
const char *s = lua_tostring(L, narg);
if (!s) type_error(L, narg, "string");
if (!s) type_error(L, narg, LUA_TSTRING);
if (len) *len = lua_strlen(L, narg);
return s;
}
const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
return checkstr(L, narg, len);
}
const char *luaL_opt_lstr (lua_State *L, int narg, const char *def,
size_t *len) {
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
if (lua_isnull(L, narg)) {
if (len) *len = def ? strlen(def) : 0;
if (len)
*len = (def ? strlen(def) : 0);
return def;
}
else return checkstr(L, narg, len);
}
double luaL_check_number (lua_State *L, int narg) {
if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
return lua_tonumber(L, narg);
else return luaL_check_lstr(L, narg, len);
}
double luaL_opt_number (lua_State *L, int narg, double def) {
LUALIB_API double luaL_check_number (lua_State *L, int narg) {
double d = lua_tonumber(L, narg);
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
type_error(L, narg, LUA_TNUMBER);
return d;
}
LUALIB_API double luaL_opt_number (lua_State *L, int narg, double def) {
if (lua_isnull(L, narg)) return def;
else {
if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
return lua_tonumber(L, narg);
}
else return luaL_check_number(L, narg);
}
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
LUALIB_API void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
int i;
for (i=0; i<n; i++)
lua_register(L, l[i].name, l[i].func);
}
void luaL_verror (lua_State *L, const char *fmt, ...) {
LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...) {
char buff[500];
va_list argp;
va_start(argp, fmt);
@@ -165,25 +162,25 @@ static void adjuststack (luaL_Buffer *B) {
}
char *luaL_prepbuffer (luaL_Buffer *B) {
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
if (emptybuffer(B))
adjuststack(B);
return B->buffer;
}
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
while (l--)
luaL_putchar(B, *s++);
}
void luaL_addstring (luaL_Buffer *B, const char *s) {
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
luaL_addlstring(B, s, strlen(s));
}
void luaL_pushresult (luaL_Buffer *B) {
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
emptybuffer(B);
if (B->level == 0)
lua_pushlstring(B->L, NULL, 0);
@@ -193,7 +190,7 @@ void luaL_pushresult (luaL_Buffer *B) {
}
void luaL_addvalue (luaL_Buffer *B) {
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
lua_State *L = B->L;
size_t vl = lua_strlen(L, -1);
if (vl <= bufffree(B)) { /* fit into buffer? */
@@ -210,7 +207,7 @@ void luaL_addvalue (luaL_Buffer *B) {
}
void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
B->L = L;
B->p = B->buffer;
B->level = 0;

View File

@@ -1,5 +1,5 @@
/*
** $Id: lauxlib.h,v 1.24 2000/09/11 20:29:27 roberto Exp roberto $
** $Id: lauxlib.h,v 1.29 2000/10/27 16:15:53 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -15,25 +15,30 @@
#include "lua.h"
#ifndef LUALIB_API
#define LUALIB_API extern
#endif
struct luaL_reg {
const char *name;
lua_CFunction func;
};
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len);
const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def,
size_t *len);
double luaL_check_number (lua_State *L, int numArg);
double luaL_opt_number (lua_State *L, int numArg, double def);
LUALIB_API void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
LUALIB_API void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
LUALIB_API const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len);
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, size_t *len);
LUALIB_API double luaL_check_number (lua_State *L, int numArg);
LUALIB_API double luaL_opt_number (lua_State *L, int numArg, double def);
void luaL_checkstack (lua_State *L, int space, const char *msg);
void luaL_checktype (lua_State *L, int narg, const char *tname);
LUALIB_API void luaL_checkstack (lua_State *L, int space, 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);
void luaL_verror (lua_State *L, const char *fmt, ...);
int luaL_findstring (const char *name, const char *const list[]);
LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...);
LUALIB_API int luaL_findstring (const char *name, const char *const list[]);
@@ -61,7 +66,9 @@ int luaL_findstring (const char *name, const char *const list[]);
*/
#ifndef LUAL_BUFFERSIZE
#define LUAL_BUFFERSIZE BUFSIZ
#endif
typedef struct luaL_Buffer {
@@ -77,12 +84,12 @@ typedef struct luaL_Buffer {
#define luaL_addsize(B,n) ((B)->p += (n))
void luaL_buffinit (lua_State *L, luaL_Buffer *B);
char *luaL_prepbuffer (luaL_Buffer *B);
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
void luaL_addstring (luaL_Buffer *B, const char *s);
void luaL_addvalue (luaL_Buffer *B);
void luaL_pushresult (luaL_Buffer *B);
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);
/* }====================================================== */

View File

@@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.5 2000/09/14 14:09:31 roberto Exp roberto $
** $Id: lbaselib.c,v 1.16 2000/10/31 13:10:24 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -34,7 +34,7 @@ static int luaB__ALERT (lua_State *L) {
** The library `liolib' redefines _ERRORMESSAGE for better error information.
*/
static int luaB__ERRORMESSAGE (lua_State *L) {
luaL_checktype(L, 1, "string");
luaL_checktype(L, 1, LUA_TSTRING);
lua_getglobal(L, LUA_ALERT);
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
lua_Debug ar;
@@ -87,7 +87,7 @@ static int luaB_print (lua_State *L) {
static int luaB_tonumber (lua_State *L) {
int base = luaL_opt_int(L, 2, 10);
if (base == 10) { /* standard conversion */
luaL_checktype(L, 1, "any");
luaL_checkany(L, 1);
if (lua_isnumber(L, 1)) {
lua_pushnumber(L, lua_tonumber(L, 1));
return 1;
@@ -118,7 +118,7 @@ static int luaB_error (lua_State *L) {
}
static int luaB_setglobal (lua_State *L) {
luaL_checktype(L, 2, "any");
luaL_checkany(L, 2);
lua_setglobal(L, luaL_check_string(L, 1));
return 0;
}
@@ -129,17 +129,16 @@ static int luaB_getglobal (lua_State *L) {
}
static int luaB_tag (lua_State *L) {
luaL_checktype(L, 1, "any");
luaL_checkany(L, 1);
lua_pushnumber(L, lua_tag(L, 1));
return 1;
}
static int luaB_settag (lua_State *L) {
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, 1); /* push table */
lua_settag(L, luaL_check_int(L, 2));
lua_pop(L, 1); /* remove second argument */
return 1; /* return first argument */
return 1; /* return table */
}
static int luaB_newtag (lua_State *L) {
@@ -156,7 +155,7 @@ static int luaB_copytagmethods (lua_State *L) {
static int luaB_globals (lua_State *L) {
lua_getglobals(L); /* value to be returned */
if (!lua_isnull(L, 1)) {
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, 1); /* new table of globals */
lua_setglobals(L);
}
@@ -164,54 +163,66 @@ static int luaB_globals (lua_State *L) {
}
static int luaB_rawget (lua_State *L) {
luaL_checktype(L, 1, "table");
luaL_checktype(L, 2, "any");
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
lua_rawget(L, -2);
return 1;
}
static int luaB_rawset (lua_State *L) {
luaL_checktype(L, 1, "table");
luaL_checktype(L, 2, "any");
luaL_checktype(L, 3, "any");
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
lua_rawset(L, -3);
return 1;
}
static int luaB_settagmethod (lua_State *L) {
int tag = (int)luaL_check_int(L, 1);
int tag = luaL_check_int(L, 1);
const char *event = luaL_check_string(L, 2);
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
"function or nil expected");
lua_pushnil(L); /* to get its tag */
if (strcmp(event, "gc") == 0 && tag != lua_tag(L, -1))
if (strcmp(event, "gc") == 0)
lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
lua_pop(L, 1); /* remove the nil */
lua_gettagmethod(L, tag, event);
lua_pushvalue(L, 3);
lua_settagmethod(L, tag, event);
return 1;
}
static int luaB_gettagmethod (lua_State *L) {
lua_gettagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
int tag = luaL_check_int(L, 1);
const char *event = luaL_check_string(L, 2);
if (strcmp(event, "gc") == 0)
lua_error(L, "deprecated use: cannot get the `gc' tag method from Lua");
lua_gettagmethod(L, tag, event);
return 1;
}
static int luaB_gcinfo (lua_State *L) {
lua_pushnumber(L, lua_getgccount(L));
lua_pushnumber(L, lua_getgcthreshold(L));
return 2;
}
static int luaB_collectgarbage (lua_State *L) {
lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
return 1;
lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
return 0;
}
static int luaB_type (lua_State *L) {
luaL_checktype(L, 1, "any");
lua_pushstring(L, lua_type(L, 1));
luaL_checkany(L, 1);
lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
return 1;
}
static int luaB_next (lua_State *L) {
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
@@ -224,7 +235,8 @@ static int luaB_next (lua_State *L) {
static int passresults (lua_State *L, int status, int oldtop) {
static const char *const errornames[] =
{"ok", "run-time error", "file error", "syntax error", "memory error"};
{"ok", "run-time error", "file error", "syntax error",
"memory error", "error in error handling"};
if (status == 0) {
int nresults = lua_gettop(L) - oldtop;
if (nresults > 0)
@@ -264,7 +276,7 @@ static int luaB_call (lua_State *L) {
int err = 0; /* index of old error method */
int i, status;
int n;
luaL_checktype(L, 2, "table");
luaL_checktype(L, 2, LUA_TTABLE);
n = lua_getn(L, 2);
if (!lua_isnull(L, 4)) { /* set new error method */
lua_getglobal(L, LUA_ERRORMESSAGE);
@@ -298,23 +310,23 @@ static int luaB_call (lua_State *L) {
static int luaB_tostring (lua_State *L) {
char buff[64];
switch (lua_type(L, 1)[2]) {
case 'm': /* nuMber */
switch (lua_type(L, 1)) {
case LUA_TNUMBER:
lua_pushstring(L, lua_tostring(L, 1));
return 1;
case 'r': /* stRing */
case LUA_TSTRING:
lua_pushvalue(L, 1);
return 1;
case 'b': /* taBle */
case LUA_TTABLE:
sprintf(buff, "table: %p", lua_topointer(L, 1));
break;
case 'n': /* fuNction */
case LUA_TFUNCTION:
sprintf(buff, "function: %p", lua_topointer(L, 1));
break;
case 'e': /* usErdata */
case LUA_TUSERDATA:
sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1));
break;
case 'l': /* niL */
case LUA_TNIL:
lua_pushstring(L, "nil");
return 1;
default:
@@ -327,8 +339,8 @@ static int luaB_tostring (lua_State *L) {
static int luaB_foreachi (lua_State *L) {
int n, i;
luaL_checktype(L, 1, "table");
luaL_checktype(L, 2, "function");
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TFUNCTION);
n = lua_getn(L, 1);
for (i=1; i<=n; i++) {
lua_pushvalue(L, 2); /* function */
@@ -344,8 +356,8 @@ static int luaB_foreachi (lua_State *L) {
static int luaB_foreach (lua_State *L) {
luaL_checktype(L, 1, "table");
luaL_checktype(L, 2, "function");
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushnil(L); /* first index */
for (;;) {
if (lua_next(L, 1) == 0)
@@ -362,7 +374,7 @@ static int luaB_foreach (lua_State *L) {
static int luaB_assert (lua_State *L) {
luaL_checktype(L, 1, "any");
luaL_checkany(L, 1);
if (lua_isnil(L, 1))
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
return 0;
@@ -370,7 +382,7 @@ static int luaB_assert (lua_State *L) {
static int luaB_getn (lua_State *L) {
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushnumber(L, lua_getn(L, 1));
return 1;
}
@@ -379,7 +391,7 @@ static int luaB_getn (lua_State *L) {
static int luaB_tinsert (lua_State *L) {
int v = lua_gettop(L); /* last argument: to be inserted */
int n, pos;
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
n = lua_getn(L, 1);
if (v == 2) /* called with only 2 arguments */
pos = n+1;
@@ -400,7 +412,7 @@ static int luaB_tinsert (lua_State *L) {
static int luaB_tremove (lua_State *L) {
int pos, n;
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
n = lua_getn(L, 1);
pos = luaL_opt_int(L, 2, n);
if (n <= 0) return 0; /* table is "empty" */
@@ -428,75 +440,78 @@ static int luaB_tremove (lua_State *L) {
*/
static void swap (lua_State *L, int i, int j) {
lua_rawgeti(L, 1, i);
lua_rawgeti(L, 1, j);
static void set2 (lua_State *L, int i, int j) {
lua_rawseti(L, 1, i);
lua_rawseti(L, 1, j);
}
static int sort_comp (lua_State *L, int n, int r) {
static int sort_comp (lua_State *L, int a, int b) {
/* WARNING: the caller (auxsort) must ensure stack space */
int res;
if (!lua_isnil(L, 2)) { /* function? */
int res;
lua_pushvalue(L, 2);
if (r) {
lua_rawgeti(L, 1, n); /* a[n] */
lua_pushvalue(L, -3); /* pivot */
}
else {
lua_pushvalue(L, -2); /* pivot */
lua_rawgeti(L, 1, n); /* a[n] */
}
lua_pushvalue(L, a-1); /* -1 to compensate function */
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
lua_rawcall(L, 2, 1);
res = !lua_isnil(L, -1);
lua_pop(L, 1);
return res;
}
else { /* a < b? */
lua_rawgeti(L, 1, n); /* a[n] */
if (r)
res = lua_lessthan(L, -1, -2);
else
res = lua_lessthan(L, -2, -1);
}
lua_pop(L, 1);
return res;
else /* a < b? */
return lua_lessthan(L, a, b);
}
static void auxsort (lua_State *L, int l, int u) {
while (l < u) { /* for tail recursion */
int i, j;
luaL_checkstack(L, 4, "array too large");
/* sort elements a[l], a[(l+u)/2] and a[u] */
lua_rawgeti(L, 1, l);
lua_rawgeti(L, 1, u);
if (sort_comp(L, l, 0)) /* a[u] < a[l]? */
swap(L, l, u);
lua_pop(L, 1);
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
set2(L, l, u); /* swap a[l] - a[u] */
else
lua_pop(L, 2);
if (u-l == 1) break; /* only 2 elements */
i = (l+u)/2;
lua_rawgeti(L, 1, i); /* Pivot = a[i] */
if (sort_comp(L, l, 0)) /* a[i]<a[l]? */
swap(L, l, i);
lua_rawgeti(L, 1, i);
lua_rawgeti(L, 1, l);
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
set2(L, i, l);
else {
if (sort_comp(L, u, 1)) /* a[u]<a[i]? */
swap(L, i, u);
lua_pop(L, 1); /* remove a[l] */
lua_rawgeti(L, 1, u);
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
set2(L, i, u);
else
lua_pop(L, 2);
}
lua_pop(L, 1); /* pop old a[i] */
if (u-l == 2) break; /* only 3 elements */
lua_rawgeti(L, 1, i); /* Pivot */
swap(L, i, u-1); /* put median element as pivot (a[u-1]) */
/* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
lua_pushvalue(L, -1);
lua_rawgeti(L, 1, u-1);
set2(L, i, u-1);
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
i = l; j = u-1;
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
/* repeat i++ until a[i] >= P */
while (sort_comp(L, ++i, 1))
/* repeat ++i until a[i] >= P */
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
if (i>u) lua_error(L, "invalid order function for sorting");
/* repeat j-- until a[j] <= P */
while (sort_comp(L, --j, 0))
lua_pop(L, 1); /* remove a[i] */
}
/* repeat --j until a[j] <= P */
while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
if (j<l) lua_error(L, "invalid order function for sorting");
if (j<i) break;
swap(L, i, j);
lua_pop(L, 1); /* remove a[j] */
}
if (j<i) {
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
break;
}
set2(L, i, j);
}
swap(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
lua_rawgeti(L, 1, u-1);
lua_rawgeti(L, 1, i);
set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
/* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
if (i-l < u-i) {
@@ -505,17 +520,16 @@ static void auxsort (lua_State *L, int l, int u) {
else {
j=i+1; i=u; u=j-2;
}
lua_pop(L, 1); /* remove pivot from stack */
auxsort(L, j, i); /* call recursively the smaller one */
} /* repeat the routine for the larger one */
}
static int luaB_sort (lua_State *L) {
int n;
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
n = lua_getn(L, 1);
if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
luaL_checktype(L, 2, "function");
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 2); /* make sure there is two arguments */
auxsort(L, 1, n);
return 0;
@@ -542,7 +556,7 @@ static const struct luaL_reg deprecated_names [num_deprecated] = {
};
#ifdef LUA_DEPRECATETFUNCS
#ifdef LUA_DEPRECATEDFUNCS
/*
** call corresponding function inserting `globals' as first argument
@@ -601,6 +615,7 @@ static const struct luaL_reg base_funcs[] = {
{"error", luaB_error},
{"foreach", luaB_foreach},
{"foreachi", luaB_foreachi},
{"gcinfo", luaB_gcinfo},
{"getglobal", luaB_getglobal},
{"gettagmethod", luaB_gettagmethod},
{"globals", luaB_globals},
@@ -627,7 +642,7 @@ static const struct luaL_reg base_funcs[] = {
void lua_baselibopen (lua_State *L) {
LUALIB_API void lua_baselibopen (lua_State *L) {
luaL_openl(L, base_funcs);
lua_pushstring(L, LUA_VERSION);
lua_setglobal(L, "_VERSION");

View File

@@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 1.49 2000/08/15 13:18:28 roberto Exp roberto $
** $Id: lcode.c,v 1.50 2000/08/31 14:08:27 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@@ -420,13 +420,14 @@ void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) {
static void codelineinfo (FuncState *fs) {
Proto *f = fs->f;
LexState *ls = fs->ls;
if (ls->lastline > fs->lastline) {
luaM_growvector(fs->L, fs->f->lineinfo, fs->nlineinfo, 2, int,
luaM_growvector(fs->L, f->lineinfo, f->nlineinfo, 2, int,
"line info overflow", MAX_INT);
if (ls->lastline > fs->lastline+1)
fs->f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
fs->f->lineinfo[fs->nlineinfo++] = fs->pc;
f->lineinfo[f->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
f->lineinfo[f->nlineinfo++] = fs->pc;
fs->lastline = ls->lastline;
}
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: ldblib.c,v 1.20 2000/09/05 19:33:32 roberto Exp roberto $
** $Id: ldblib.c,v 1.28 2000/11/06 13:19:08 roberto Exp roberto $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
@@ -103,65 +103,73 @@ static int setlocal (lua_State *L) {
lua_Debug ar;
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
luaL_argerror(L, 1, "level out of range");
luaL_checktype(L, 3, "any");
luaL_checkany(L, 3);
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
return 1;
}
/*
** because of these variables, this module is not reentrant, and should
** not be used in multiple states
*/
static int linehook = LUA_NOREF; /* Lua reference to line hook function */
static int callhook = LUA_NOREF; /* Lua reference to call hook function */
/* dummy variables (to define unique addresses) */
static char key1, key2;
#define KEY_CALLHOOK (&key1)
#define KEY_LINEHOOK (&key2)
static void linef (lua_State *L, lua_Debug *ar) {
if (linehook != LUA_NOREF) {
lua_getref(L, linehook);
lua_pushnumber(L, ar->currentline);
lua_call(L, 1, 0);
static void hookf (lua_State *L, void *key) {
lua_getregistry(L);
lua_pushuserdata(L, key);
lua_gettable(L, -2);
if (lua_isfunction(L, -1)) {
lua_pushvalue(L, 1);
lua_rawcall(L, 1, 0);
}
else
lua_pop(L, 1); /* pop result from gettable */
lua_pop(L, 1); /* pop table */
}
static void callf (lua_State *L, lua_Debug *ar) {
if (callhook != LUA_NOREF) {
lua_getref(L, callhook);
lua_pushstring(L, ar->event);
lua_call(L, 1, 0);
}
lua_pushstring(L, ar->event);
hookf(L, KEY_CALLHOOK);
}
static void linef (lua_State *L, lua_Debug *ar) {
lua_pushnumber(L, ar->currentline);
hookf(L, KEY_LINEHOOK);
}
static void sethook (lua_State *L, void *key, lua_Hook hook,
lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
lua_settop(L, 1);
if (lua_isnil(L, 1))
(*sethookf)(L, NULL);
else if (lua_isfunction(L, 1))
(*sethookf)(L, hook);
else
luaL_argerror(L, 1, "function expected");
lua_getregistry(L);
lua_pushuserdata(L, key);
lua_pushvalue(L, -1); /* dup key */
lua_gettable(L, -3); /* get old value */
lua_pushvalue(L, -2); /* key (again) */
lua_pushvalue(L, 1);
lua_settable(L, -5); /* set new value */
}
static int setcallhook (lua_State *L) {
lua_unref(L, callhook);
if (lua_isnull(L, 1)) {
callhook = LUA_NOREF;
lua_setcallhook(L, NULL);
}
else {
callhook = lua_ref(L, 1);
lua_setcallhook(L, callf);
}
return 0;
sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
return 1;
}
static int setlinehook (lua_State *L) {
lua_unref(L, linehook);
if (lua_isnull(L, 1)) {
linehook = LUA_NOREF;
lua_setlinehook(L, NULL);
}
else {
linehook = lua_ref(L, 1);
lua_setlinehook(L, linef);
}
return 0;
sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
return 1;
}
@@ -174,7 +182,7 @@ static const struct luaL_reg dblib[] = {
};
void lua_dblibopen (lua_State *L) {
LUALIB_API void lua_dblibopen (lua_State *L) {
luaL_openl(L, dblib);
}

138
ldebug.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 1.41 2000/09/12 18:38:02 roberto Exp roberto $
** $Id: ldebug.c,v 1.49 2000/10/27 11:39:52 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@@ -23,34 +23,32 @@
#include "luadebug.h"
static const char *getfuncname (lua_State *L, StkId f, const char **name);
static void setnormalized (TObject *d, const TObject *s) {
switch (s->ttype) {
case TAG_CMARK: {
clvalue(d) = clvalue(s);
ttype(d) = TAG_CCLOSURE;
break;
}
case TAG_LMARK: {
clvalue(d) = infovalue(s)->func;
ttype(d) = TAG_LCLOSURE;
break;
}
default: *d = *s;
if (ttype(s) == LUA_TMARK) {
clvalue(d) = infovalue(s)->func;
ttype(d) = LUA_TFUNCTION;
}
else *d = *s;
}
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
static int isLmark (StkId o) {
return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC);
}
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
lua_Hook oldhook = L->callhook;
L->callhook = func;
return oldhook;
}
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
lua_Hook oldhook = L->linehook;
L->linehook = func;
return oldhook;
@@ -70,7 +68,7 @@ static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
}
int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
StkId f = aux_stackedfunction(L, level, L->top);
if (f == NULL) return 0; /* there is no such level */
else {
@@ -80,11 +78,11 @@ int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
}
static int lua_nups (StkId f) {
static int nups (StkId f) {
switch (ttype(f)) {
case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK:
case LUA_TFUNCTION:
return clvalue(f)->nupvalues;
case TAG_LMARK:
case LUA_TMARK:
return infovalue(f)->func->nupvalues;
default:
return 0;
@@ -94,8 +92,8 @@ static int lua_nups (StkId f) {
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
int refi;
if (lineinfo == NULL) return -1; /* no line info */
else if (pc == -1) return refline; /* function preamble */
if (lineinfo == NULL || pc == -1)
return -1; /* no line info or function is not active */
refi = prefi ? *prefi : 0;
if (lineinfo[refi] < 0)
refline += -lineinfo[refi++];
@@ -123,51 +121,54 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
}
static int lua_currentpc (StkId f) {
static int currentpc (StkId f) {
CallInfo *ci = infovalue(f);
LUA_ASSERT(ttype(f) == TAG_LMARK, "function has no pc");
return (*ci->pc - ci->func->f.l->code) - 1;
LUA_ASSERT(isLmark(f), "function has no pc");
if (ci->pc)
return (*ci->pc - ci->func->f.l->code) - 1;
else
return -1; /* function is not active */
}
static int lua_currentline (StkId f) {
if (ttype(f) != TAG_LMARK)
static int currentline (StkId f) {
if (!isLmark(f))
return -1; /* only active lua functions have current-line information */
else {
CallInfo *ci = infovalue(f);
int *lineinfo = ci->func->f.l->lineinfo;
return luaG_getline(lineinfo, lua_currentpc(f), 1, NULL);
return luaG_getline(lineinfo, currentpc(f), 1, NULL);
}
}
static Proto *getluaproto (StkId f) {
return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL;
return (isLmark(f) ? infovalue(f)->func->f.l : NULL);
}
const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int localnum) {
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
const char *name;
StkId f = ar->_func;
Proto *fp = getluaproto(f);
if (!fp) return NULL; /* `f' is not a Lua function? */
name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
name = luaF_getlocalname(fp, n, currentpc(f));
if (!name) return NULL;
luaA_pushobject(L, (f+1)+(localnum-1)); /* push value */
luaA_pushobject(L, (f+1)+(n-1)); /* push value */
return name;
}
const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int localnum) {
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
const char *name;
StkId f = ar->_func;
Proto *fp = getluaproto(f);
L->top--; /* pop new value */
if (!fp) return NULL; /* `f' is not a Lua function? */
name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
if (!name || name[0] == '*') return NULL; /* `*' starts private locals */
*((f+1)+(localnum-1)) = *L->top;
name = luaF_getlocalname(fp, n, currentpc(f));
if (!name || name[0] == '(') return NULL; /* `(' starts private locals */
*((f+1)+(n-1)) = *L->top;
return name;
}
@@ -179,22 +180,25 @@ static void infoLproto (lua_Debug *ar, Proto *f) {
}
static void lua_funcinfo (lua_Debug *ar, StkId func) {
static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
Closure *cl = NULL;
switch (ttype(func)) {
case TAG_LCLOSURE:
infoLproto(ar, clvalue(func)->f.l);
case LUA_TFUNCTION:
cl = clvalue(func);
break;
case TAG_LMARK:
infoLproto(ar, infovalue(func)->func->f.l);
break;
case TAG_CCLOSURE: case TAG_CMARK:
ar->source = "(C)";
ar->linedefined = -1;
ar->what = "C";
case LUA_TMARK:
cl = infovalue(func)->func;
break;
default:
LUA_INTERNALERROR("invalid `func' value");
lua_error(L, "value for `lua_getinfo' is not a function");
}
if (cl->isC) {
ar->source = "=C";
ar->linedefined = -1;
ar->what = "C";
}
else
infoLproto(ar, cl->f.l);
luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
if (ar->linedefined == 0)
ar->what = "main";
@@ -202,12 +206,14 @@ static void lua_funcinfo (lua_Debug *ar, StkId func) {
static const char *travtagmethods (lua_State *L, const TObject *o) {
int e;
for (e=0; e<IM_N; e++) {
int t;
for (t=0; t<=L->last_tag; t++)
if (luaO_equalObj(o, luaT_getim(L, t,e)))
return luaT_eventname[e];
if (ttype(o) == LUA_TFUNCTION) {
int e;
for (e=0; e<TM_N; e++) {
int t;
for (t=0; t<=L->last_tag; t++)
if (clvalue(o) == luaT_gettm(L, t, e))
return luaT_eventname[e];
}
}
return NULL;
}
@@ -218,14 +224,14 @@ static const char *travglobals (lua_State *L, const TObject *o) {
int i;
for (i=0; i<g->size; i++) {
if (luaO_equalObj(o, val(node(g, i))) &&
ttype(key(node(g, i))) == TAG_STRING)
ttype(key(node(g, i))) == LUA_TSTRING)
return tsvalue(key(node(g, i)))->str;
}
return NULL;
}
static void lua_getname (lua_State *L, StkId f, lua_Debug *ar) {
static void getname (lua_State *L, StkId f, lua_Debug *ar) {
TObject o;
setnormalized(&o, f);
/* try to find a name for given function */
@@ -238,7 +244,7 @@ static void lua_getname (lua_State *L, StkId f, lua_Debug *ar) {
}
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
StkId func;
int isactive = (*what != '>');
if (isactive)
@@ -250,21 +256,21 @@ int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
for (; *what; what++) {
switch (*what) {
case 'S': {
lua_funcinfo(ar, func);
funcinfo(L, ar, func);
break;
}
case 'l': {
ar->currentline = lua_currentline(func);
ar->currentline = currentline(func);
break;
}
case 'u': {
ar->nups = lua_nups(func);
ar->nups = nups(func);
break;
}
case 'n': {
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
if (ar->namewhat == NULL)
lua_getname(L, func, ar);
getname(L, func, ar);
break;
}
case 'f': {
@@ -377,11 +383,11 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
static const char *getobjname (lua_State *L, StkId obj, const char **name) {
StkId func = aux_stackedfunction(L, 0, obj);
if (func == NULL || ttype(func) != TAG_LMARK)
return NULL; /* not a Lua function */
if (!isLmark(func))
return NULL; /* not an active Lua function */
else {
Proto *p = infovalue(func)->func->f.l;
int pc = lua_currentpc(func);
int pc = currentpc(func);
int stackpos = obj - (func+1); /* func+1 == function base */
Instruction i = luaG_symbexec(p, pc, stackpos);
LUA_ASSERT(pc != -1, "function must be active");
@@ -409,11 +415,11 @@ static const char *getobjname (lua_State *L, StkId obj, const char **name) {
static const char *getfuncname (lua_State *L, StkId f, const char **name) {
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
if (func == NULL || ttype(func) != TAG_LMARK)
return NULL; /* not a Lua function */
if (!isLmark(func))
return NULL; /* not an active Lua function */
else {
Proto *p = infovalue(func)->func->f.l;
int pc = lua_currentpc(func);
int pc = currentpc(func);
Instruction i;
if (pc == -1) return NULL; /* function is not activated */
i = p->code[pc];
@@ -442,7 +448,7 @@ void luaG_typeerror (lua_State *L, StkId o, const char *op) {
}
void luaG_binerror (lua_State *L, StkId p1, lua_Type t, const char *op) {
void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) {
if (ttype(p1) == t) p1++;
LUA_ASSERT(ttype(p1) != t, "must be an error");
luaG_typeerror(L, p1, op);

View File

@@ -1,5 +1,5 @@
/*
** $Id: ldebug.h,v 1.4 2000/08/10 19:50:47 roberto Exp roberto $
** $Id: ldebug.h,v 1.6 2000/10/02 20:10:55 roberto Exp roberto $
** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h
*/
@@ -13,7 +13,7 @@
void luaG_typeerror (lua_State *L, StkId o, const char *op);
void luaG_binerror (lua_State *L, StkId p1, lua_Type t, const char *op);
void luaG_binerror (lua_State *L, StkId p1, int t, const char *op);
int luaG_getline (int *lineinfo, int pc, int refline, int *refi);
void luaG_ordererror (lua_State *L, StkId top);

294
ldo.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 1.95 2000/09/11 20:29:27 roberto Exp roberto $
** $Id: ldo.c,v 1.108 2000/10/20 16:39:03 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -33,6 +33,7 @@
void luaD_init (lua_State *L, int stacksize) {
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
L->nblocks += stacksize*sizeof(TObject);
L->stack_last = L->stack+(stacksize-1);
L->stacksize = stacksize;
L->Cbase = L->top = L->stack;
@@ -40,11 +41,10 @@ void luaD_init (lua_State *L, int stacksize) {
void luaD_checkstack (lua_State *L, int n) {
if (L->stack_last-L->top <= n) { /* stack overflow? */
if (L->stack_last - L->top <= n) { /* stack overflow? */
if (L->stack_last-L->stack > (L->stacksize-1)) {
/* overflow while handling overflow: do what?? */
L->top -= EXTRA_STACK;
lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
/* overflow while handling overflow */
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
}
else {
L->stack_last += EXTRA_STACK; /* to be used by error message */
@@ -72,7 +72,7 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) {
else {
luaD_checkstack(L, diff);
while (diff--)
ttype(L->top++) = TAG_NIL;
ttype(L->top++) = LUA_TNIL;
}
}
@@ -90,7 +90,7 @@ static void luaD_openstack (lua_State *L, StkId pos) {
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
StkId old_Cbase = L->Cbase;
StkId old_top = L->Cbase = L->top;
luaD_checkstack(L, LUA_MINSTACK); /* assures minimum stack size */
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
L->allowhooks = 0; /* cannot call hooks inside a hook */
(*hook)(L, ar);
LUA_ASSERT(L->allowhooks == 0, "invalid allow");
@@ -117,6 +117,7 @@ static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
lua_Debug ar;
ar._func = func;
ar.event = event;
infovalue(func)->pc = NULL; /* function is not active */
dohook(L, &ar, callhook);
}
}
@@ -127,7 +128,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
StkId old_Cbase = L->Cbase;
int n;
L->Cbase = base; /* new base for C function */
luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */
luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
*(L->top++) = cl->upvalue[n];
n = (*cl->f.c)(L); /* do the actual call */
@@ -136,10 +137,11 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
}
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) {
StkId base = L->top - nParams;
luaD_openstack(L, base);
*base = *f;
clvalue(base) = f;
ttype(base) = LUA_TFUNCTION;
luaD_call(L, base, nResults);
}
@@ -152,148 +154,116 @@ void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
** The number of results is nResults, unless nResults=LUA_MULTRET.
*/
void luaD_call (lua_State *L, StkId func, int nResults) {
lua_Hook callhook;
StkId firstResult;
lua_Hook callhook = L->callhook;
retry: /* for `function' tag method */
switch (ttype(func)) {
case TAG_LCLOSURE: {
CallInfo ci;
ci.func = clvalue(func);
ci.line = 0;
ttype(func) = TAG_LMARK;
infovalue(func) = &ci;
if (callhook)
luaD_callHook(L, func, callhook, "call");
firstResult = luaV_execute(L, ci.func, func+1);
break;
}
case TAG_CCLOSURE: {
ttype(func) = TAG_CMARK;
if (callhook)
luaD_callHook(L, func, callhook, "call");
firstResult = callCclosure(L, clvalue(func), func+1);
break;
}
default: { /* `func' is not a function; check the `function' tag method */
const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
if (ttype(im) == TAG_NIL)
luaG_typeerror(L, func, "call");
luaD_openstack(L, func);
*func = *im; /* tag method is the new function to be called */
goto retry; /* retry the call */
}
CallInfo ci;
Closure *cl;
if (ttype(func) != LUA_TFUNCTION) {
/* `func' is not a function; check the `function' tag method */
Closure *tm = luaT_gettmbyObj(L, func, TM_FUNCTION);
if (tm == NULL)
luaG_typeerror(L, func, "call");
luaD_openstack(L, func);
clvalue(func) = tm; /* tag method is the new function to be called */
ttype(func) = LUA_TFUNCTION;
}
cl = clvalue(func);
ci.func = cl;
infovalue(func) = &ci;
ttype(func) = LUA_TMARK;
callhook = L->callhook;
if (callhook)
luaD_callHook(L, func, callhook, "call");
firstResult = (cl->isC ? callCclosure(L, cl, func+1) :
luaV_execute(L, cl, func+1));
if (callhook) /* same hook that was active at entry */
luaD_callHook(L, func, callhook, "return");
/* adjust the number of results */
if (nResults == LUA_MULTRET)
nResults = L->top - firstResult;
else
luaD_adjusttop(L, firstResult, nResults);
LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag");
/* move results to `func' (to erase parameters and function) */
while (nResults) {
*func++ = *(L->top - nResults);
nResults--;
if (nResults == LUA_MULTRET) {
while (firstResult < L->top) /* copy all results */
*func++ = *firstResult++;
L->top = func;
}
L->top = func;
}
static void message (lua_State *L, const char *s) {
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
if (*luaO_typename(em) == 'f') {
*L->top = *em;
incr_top;
lua_pushstring(L, s);
luaD_call(L, L->top-2, 0);
else { /* copy at most `nResults' */
for (; nResults > 0 && firstResult < L->top; nResults--)
*func++ = *firstResult++;
L->top = func;
for (; nResults > 0; nResults--) { /* if there are not enough results */
ttype(L->top) = LUA_TNIL; /* adjust the stack */
incr_top; /* must check stack space */
}
}
}
void luaD_breakrun (lua_State *L, int errcode) {
if (L->errorJmp) {
L->errorJmp->status = errcode;
longjmp(L->errorJmp->b, 1);
}
else {
if (errcode != LUA_ERRMEM)
message(L, "unable to recover; exiting\n");
exit(EXIT_FAILURE);
}
}
/*
** Reports an error, and jumps up to the available recovery label
*/
void lua_error (lua_State *L, const char *s) {
if (s) message(L, s);
luaD_breakrun(L, LUA_ERRRUN);
}
static void chain_longjmp (lua_State *L, struct lua_longjmp *lj) {
lj->status = 0;
lj->base = L->Cbase;
lj->previous = L->errorJmp;
L->errorJmp = lj;
}
static int restore_longjmp (lua_State *L, struct lua_longjmp *lj) {
L->Cbase = lj->base;
L->errorJmp = lj->previous;
return lj->status;
luaC_checkGC(L);
}
/*
** Execute a protected call.
*/
int lua_call (lua_State *L, int nargs, int nresults) {
struct CallS { /* data to `f_call' */
StkId func;
int nresults;
};
static void f_call (lua_State *L, void *ud) {
struct CallS *c = (struct CallS *)ud;
luaD_call(L, c->func, c->nresults);
}
LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
StkId func = L->top - (nargs+1); /* function to be called */
struct lua_longjmp myErrorJmp;
chain_longjmp(L, &myErrorJmp);
if (setjmp(myErrorJmp.b) == 0) {
luaD_call(L, func, nresults);
}
else { /* an error occurred: restore the state */
L->top = func; /* remove garbage from the stack */
restore_stack_limit(L);
}
return restore_longjmp(L, &myErrorJmp);
struct CallS c;
int status;
c.func = func; c.nresults = nresults;
status = luaD_runprotected(L, f_call, &c);
if (status != 0) /* an error occurred? */
L->top = func; /* remove parameters from the stack */
return status;
}
/*
** Execute a protected parser.
*/
struct ParserS { /* data to `f_parser' */
ZIO *z;
int bin;
};
static void f_parser (lua_State *L, void *ud) {
struct ParserS *p = (struct ParserS *)ud;
Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
luaV_Lclosure(L, tf, 0);
}
static int protectedparser (lua_State *L, ZIO *z, int bin) {
struct lua_longjmp myErrorJmp;
struct ParserS p;
unsigned long old_blocks;
int status;
p.z = z; p.bin = bin;
luaC_checkGC(L);
old_blocks = L->nblocks;
chain_longjmp(L, &myErrorJmp);
if (setjmp(myErrorJmp.b) == 0) {
Proto *tf = bin ? luaU_undump(L, z) : luaY_parser(L, z);
luaV_Lclosure(L, tf, 0);
status = luaD_runprotected(L, f_parser, &p);
if (status == 0) {
/* add new memory to threshold (as it probably will stay) */
L->GCthreshold += (L->nblocks - old_blocks);
}
else { /* an error occurred: correct error code */
if (myErrorJmp.status == LUA_ERRRUN)
myErrorJmp.status = LUA_ERRSYNTAX;
}
/* add new memory to threshould (as it probably will stay) */
L->GCthreshold += (L->nblocks - old_blocks);
return restore_longjmp(L, &myErrorJmp); /* error code */
else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
status = LUA_ERRSYNTAX;
return status;
}
static int parse_file (lua_State *L, const char *filename) {
ZIO z;
char source[MAXFILENAME];
int status;
int bin; /* flag for file mode */
int c; /* look ahead char */
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
if (filename == NULL) filename = "(stdin)";
sprintf(source, "@%.*s", (int)sizeof(source)-2, filename);
c = fgetc(f);
ungetc(c, f);
bin = (c == ID_CHUNK);
@@ -301,7 +271,12 @@ static int parse_file (lua_State *L, const char *filename) {
f = freopen(filename, "rb", f); /* set binary mode */
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
}
luaZ_Fopen(&z, f, source);
lua_pushstring(L, "@");
lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename);
lua_concat(L, 2);
filename = lua_tostring(L, -1); /* filename = '@'..filename */
lua_pop(L, 1); /* OK: there is no GC during parser */
luaZ_Fopen(&z, f, filename);
status = protectedparser(L, &z, bin);
if (f != stdin)
fclose(f);
@@ -309,7 +284,7 @@ static int parse_file (lua_State *L, const char *filename) {
}
int lua_dofile (lua_State *L, const char *filename) {
LUA_API int lua_dofile (lua_State *L, const char *filename) {
int status = parse_file(L, filename);
if (status == 0) /* parse OK? */
status = lua_call(L, 0, LUA_MULTRET); /* call main */
@@ -326,8 +301,7 @@ static int parse_buffer (lua_State *L, const char *buff, size_t size,
}
int lua_dobuffer (lua_State *L, const char *buff, size_t size,
const char *name) {
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name) {
int status = parse_buffer(L, buff, size, name);
if (status == 0) /* parse OK? */
status = lua_call(L, 0, LUA_MULTRET); /* call main */
@@ -335,7 +309,77 @@ int lua_dobuffer (lua_State *L, const char *buff, size_t size,
}
int lua_dostring (lua_State *L, const char *str) {
LUA_API int lua_dostring (lua_State *L, const char *str) {
return lua_dobuffer(L, str, strlen(str), str);
}
/*
** {======================================================
** Error-recover functions (based on long jumps)
** =======================================================
*/
/* chain list of long jump buffers */
struct lua_longjmp {
jmp_buf b;
struct lua_longjmp *previous;
volatile int status; /* error code */
};
static void message (lua_State *L, const char *s) {
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
if (ttype(em) == LUA_TFUNCTION) {
*L->top = *em;
incr_top;
lua_pushstring(L, s);
luaD_call(L, L->top-2, 0);
}
}
/*
** Reports an error, and jumps up to the available recovery label
*/
LUA_API void lua_error (lua_State *L, const char *s) {
if (s) message(L, s);
luaD_breakrun(L, LUA_ERRRUN);
}
void luaD_breakrun (lua_State *L, int errcode) {
if (L->errorJmp) {
L->errorJmp->status = errcode;
longjmp(L->errorJmp->b, 1);
}
else {
if (errcode != LUA_ERRMEM)
message(L, "unable to recover; exiting\n");
exit(EXIT_FAILURE);
}
}
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
StkId oldCbase = L->Cbase;
StkId oldtop = L->top;
struct lua_longjmp lj;
int allowhooks = L->allowhooks;
lj.status = 0;
lj.previous = L->errorJmp; /* chain new error handler */
L->errorJmp = &lj;
if (setjmp(lj.b) == 0)
(*f)(L, ud);
else { /* an error occurred: restore the state */
L->allowhooks = allowhooks;
L->Cbase = oldCbase;
L->top = oldtop;
restore_stack_limit(L);
}
L->errorJmp = lj.previous; /* restore old error handler */
return lj.status;
}
/* }====================================================== */

8
ldo.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: ldo.h,v 1.23 2000/08/28 17:57:04 roberto Exp roberto $
** $Id: ldo.h,v 1.27 2000/10/05 13:00:17 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -23,9 +23,11 @@ void luaD_init (lua_State *L, int stacksize);
void luaD_adjusttop (lua_State *L, StkId base, int extra);
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
void luaD_call (lua_State *L, StkId func, int nResults);
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);
void luaD_breakrun (lua_State *L, int errcode);
void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults);
void luaD_checkstack (lua_State *L, int n);
void luaD_breakrun (lua_State *L, int errcode);
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud);
#endif

59
lfunc.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lfunc.c,v 1.29 2000/08/09 19:16:57 roberto Exp roberto $
** $Id: lfunc.c,v 1.33 2000/10/18 17:19:09 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/
@@ -13,47 +13,68 @@
#include "lmem.h"
#include "lstate.h"
#define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
#define gcsizeclosure(L, c) numblocks(L, c->nupvalues, sizeof(Closure))
#define sizeclosure(n) ((int)sizeof(Closure) + (int)sizeof(TObject)*((n)-1))
Closure *luaF_newclosure (lua_State *L, int nelems) {
Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
(lint32)sizeof(TObject)*(nelems-1));
int size = sizeclosure(nelems);
Closure *c = (Closure *)luaM_malloc(L, size);
c->next = L->rootcl;
L->rootcl = c;
c->mark = c;
c->nupvalues = nelems;
L->nblocks += gcsizeclosure(L, c);
L->nblocks += size;
return c;
}
Proto *luaF_newproto (lua_State *L) {
Proto *f = luaM_new(L, Proto);
f->code = NULL;
f->lineinfo = NULL;
f->lineDefined = 0;
f->source = NULL;
f->kstr = NULL;
f->nkstr = 0;
f->knum = NULL;
f->nknum = 0;
f->kstr = NULL;
f->nkstr = 0;
f->kproto = NULL;
f->nkproto = 0;
f->locvars = NULL;
f->nlocvars = 0;
f->next = L->rootproto;
L->rootproto = f;
f->code = NULL;
f->ncode = 0;
f->numparams = 0;
f->is_vararg = 0;
f->maxstacksize = 0;
f->marked = 0;
L->nblocks += gcsizeproto(L, f);
f->lineinfo = NULL;
f->nlineinfo = 0;
f->nlocvars = 0;
f->locvars = NULL;
f->lineDefined = 0;
f->source = NULL;
f->next = L->rootproto; /* chain in list of protos */
L->rootproto = f;
return f;
}
static size_t protosize (Proto *f) {
return sizeof(Proto)
+ f->nknum*sizeof(Number)
+ f->nkstr*sizeof(TString *)
+ f->nkproto*sizeof(Proto *)
+ f->ncode*sizeof(Instruction)
+ f->nlocvars*sizeof(struct LocVar)
+ f->nlineinfo*sizeof(int);
}
void luaF_protook (lua_State *L, Proto *f, int pc) {
f->ncode = pc; /* signal that proto was properly created */
L->nblocks += protosize(f);
}
void luaF_freeproto (lua_State *L, Proto *f) {
L->nblocks -= gcsizeproto(L, f);
if (f->ncode > 0) /* function was properly created? */
L->nblocks -= protosize(f);
luaM_free(L, f->code);
luaM_free(L, f->locvars);
luaM_free(L, f->kstr);
@@ -65,7 +86,7 @@ void luaF_freeproto (lua_State *L, Proto *f) {
void luaF_freeclosure (lua_State *L, Closure *c) {
L->nblocks -= gcsizeclosure(L, c);
L->nblocks -= sizeclosure(c->nupvalues);
luaM_free(L, c);
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: lfunc.h,v 1.11 2000/03/10 18:37:44 roberto Exp roberto $
** $Id: lfunc.h,v 1.12 2000/06/26 19:28:31 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/
@@ -13,6 +13,7 @@
Proto *luaF_newproto (lua_State *L);
void luaF_protook (lua_State *L, Proto *f, int pc);
Closure *luaF_newclosure (lua_State *L, int nelems);
void luaF_freeproto (lua_State *L, Proto *f);
void luaF_freeclosure (lua_State *L, Closure *c);

153
lgc.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lgc.c,v 1.65 2000/09/11 17:38:42 roberto Exp roberto $
** $Id: lgc.c,v 1.71 2000/10/05 13:00:17 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@@ -24,7 +24,7 @@ typedef struct GCState {
static int markobject (GCState *st, TObject *o);
static void markobject (GCState *st, TObject *o);
/* mark a string; marks larger than 1 cannot be changed */
@@ -63,49 +63,48 @@ static void marklock (lua_State *L, GCState *st) {
}
static void marktagmethods (lua_State *L, GCState *st) {
int e;
for (e=0; e<IM_N; e++) {
int t;
for (t=0; t<=L->last_tag; t++)
markobject(st, luaT_getim(L, t,e));
static void markclosure (GCState *st, Closure *cl) {
if (!ismarked(cl)) {
if (!cl->isC)
protomark(cl->f.l);
cl->mark = st->cmark; /* chain it for later traversal */
st->cmark = cl;
}
}
static int markobject (GCState *st, TObject *o) {
static void marktagmethods (lua_State *L, GCState *st) {
int e;
for (e=0; e<TM_N; e++) {
int t;
for (t=0; t<=L->last_tag; t++) {
Closure *cl = luaT_gettm(L, t, e);
if (cl) markclosure(st, cl);
}
}
}
static void markobject (GCState *st, TObject *o) {
switch (ttype(o)) {
case TAG_USERDATA: case TAG_STRING:
case LUA_TUSERDATA: case LUA_TSTRING:
strmark(tsvalue(o));
break;
case TAG_TABLE: {
case LUA_TMARK:
markclosure(st, infovalue(o)->func);
break;
case LUA_TFUNCTION:
markclosure(st, clvalue(o));
break;
case LUA_TTABLE: {
if (!ismarked(hvalue(o))) {
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
st->tmark = hvalue(o);
}
break;
}
case TAG_LMARK: {
Closure *cl = infovalue(o)->func;
if (!ismarked(cl)) {
protomark(cl->f.l);
cl->mark = st->cmark; /* chain it for later traversal */
st->cmark = cl;
}
break;
}
case TAG_LCLOSURE:
protomark(clvalue(o)->f.l);
/* go through */
case TAG_CCLOSURE: case TAG_CMARK:
if (!ismarked(clvalue(o))) {
clvalue(o)->mark = st->cmark; /* chain it for later traversal */
st->cmark = clvalue(o);
}
break;
default: break; /* numbers, etc */
}
return 0;
}
@@ -131,8 +130,8 @@ static void markall (lua_State *L) {
st.tmark = h->mark; /* remove it from list */
for (i=0; i<h->size; i++) {
Node *n = node(h, i);
if (ttype(key(n)) != TAG_NIL) {
if (ttype(val(n)) == TAG_NIL)
if (ttype(key(n)) != LUA_TNIL) {
if (ttype(val(n)) == LUA_TNIL)
luaH_remove(h, key(n)); /* dead element; try to remove it */
markobject(&st, &n->key);
markobject(&st, &n->val);
@@ -147,12 +146,12 @@ static void markall (lua_State *L) {
static int hasmark (const TObject *o) {
/* valid only for locked objects */
switch (o->ttype) {
case TAG_STRING: case TAG_USERDATA:
case LUA_TSTRING: case LUA_TUSERDATA:
return tsvalue(o)->marked;
case TAG_TABLE:
case LUA_TTABLE:
return ismarked(hvalue(o));
case TAG_LCLOSURE: case TAG_CCLOSURE:
return ismarked(clvalue(o)->mark);
case LUA_TFUNCTION:
return ismarked(clvalue(o));
default: /* number */
return 1;
}
@@ -235,18 +234,13 @@ static void checktab (lua_State *L, stringtable *tb) {
}
/*
** collect all elements with `marked' <= `limit'.
** with limit=0, that means all unmarked elements;
** with limit=MAX_INT, that means all elements.
*/
static void collectstringtab (lua_State *L, int limit) {
static void collectstrings (lua_State *L, int all) {
int i;
for (i=0; i<L->strt.size; i++) { /* for each list */
TString **p = &L->strt.hash[i];
TString *next;
while ((next = *p) != NULL) {
if ((int)next->marked > limit) { /* preserve? */
if (next->marked && !all) { /* preserve? */
if (next->marked < FIXMARK) /* does not change FIXMARKs */
next->marked = 0;
p = &next->nexthash;
@@ -254,7 +248,7 @@ static void collectstringtab (lua_State *L, int limit) {
else { /* collect */
*p = next->nexthash;
L->strt.nuse--;
L->nblocks -= gcsizestring(L, next->u.s.len);
L->nblocks -= sizestring(next->len);
luaM_free(L, next);
}
}
@@ -263,24 +257,23 @@ static void collectstringtab (lua_State *L, int limit) {
}
static void collectudatatab (lua_State *L, int all) {
static void collectudata (lua_State *L, int all) {
int i;
for (i=0; i<L->udt.size; i++) { /* for each list */
TString **p = &L->udt.hash[i];
TString *next;
while ((next = *p) != NULL) {
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
if ((int)next->marked > all) { /* preserve? */
if (next->marked && !all) { /* preserve? */
next->marked = 0;
p = &next->nexthash;
}
else { /* collect */
int tag = next->u.d.tag;
if (tag > L->last_tag) tag = TAG_USERDATA;
*p = next->nexthash;
next->nexthash = L->IMtable[tag].collected; /* chain udata */
L->IMtable[tag].collected = next;
L->nblocks -= gcsizeudata;
next->nexthash = L->TMtable[tag].collected; /* chain udata */
L->TMtable[tag].collected = next;
L->nblocks -= sizestring(next->len);
L->udt.nuse--;
}
}
@@ -289,14 +282,29 @@ static void collectudatatab (lua_State *L, int all) {
}
#define MINBUFFER 256
static void checkMbuffer (lua_State *L) {
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */
L->nblocks += (newsize - L->Mbuffsize)*sizeof(char);
L->Mbuffsize = newsize;
luaM_reallocvector(L, L->Mbuffer, newsize, char);
}
}
static void callgcTM (lua_State *L, const TObject *o) {
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
if (ttype(im) != TAG_NIL) {
Closure *tm = luaT_gettmbyObj(L, o, TM_GC);
if (tm != NULL) {
int oldah = L->allowhooks;
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
luaD_checkstack(L, 2);
*(L->top) = *im;
clvalue(L->top) = tm;
ttype(L->top) = LUA_TFUNCTION;
*(L->top+1) = *o;
L->top += 2;
luaD_call(L, L->top-2, 0);
L->allowhooks = oldah; /* restore hooks */
}
}
@@ -304,55 +312,42 @@ static void callgcTM (lua_State *L, const TObject *o) {
static void callgcTMudata (lua_State *L) {
int tag;
TObject o;
ttype(&o) = TAG_USERDATA;
for (tag=L->last_tag; tag>=0; tag--) {
TString *udata = L->IMtable[tag].collected;
L->IMtable[tag].collected = NULL;
while (udata) {
TString *next = udata->nexthash;
ttype(&o) = LUA_TUSERDATA;
L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */
for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */
TString *udata;
while ((udata = L->TMtable[tag].collected) != NULL) {
L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
tsvalue(&o) = udata;
callgcTM(L, &o);
luaM_free(L, udata);
udata = next;
}
}
}
void luaC_collect (lua_State *L, int all) {
int oldah = L->allowhooks;
L->allowhooks = 0; /* stop debug hooks during GC */
L->GCthreshold *= 4; /* to avoid GC during GC */
collectudatatab(L, all);
collectudata(L, all);
callgcTMudata(L);
collectstringtab(L, all?MAX_INT:0);
collectstrings(L, all);
collecttable(L);
collectproto(L);
collectclosure(L);
L->allowhooks = oldah; /* restore hooks */
}
#define MINBUFFER 256
long lua_collectgarbage (lua_State *L, long limit) {
unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
static void luaC_collectgarbage (lua_State *L) {
markall(L);
invalidaterefs(L);
invalidaterefs(L); /* check unlocked references */
luaC_collect(L, 0);
recovered = recovered - L->nblocks;
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
L->Mbuffsize /= 2; /* still larger than MINBUFFER */
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
}
checkMbuffer(L);
L->GCthreshold = 2*L->nblocks; /* set new threshold */
callgcTM(L, &luaO_nilobject);
return recovered;
}
void luaC_checkGC (lua_State *L) {
if (L->nblocks >= L->GCthreshold)
lua_collectgarbage(L, 0);
luaC_collectgarbage(L);
}

4
lgc.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: lgc.h,v 1.6 1999/10/04 17:51:04 roberto Exp roberto $
** $Id: lgc.h,v 1.7 1999/11/22 13:12:07 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@@ -11,8 +11,8 @@
#include "lobject.h"
void luaC_checkGC (lua_State *L);
void luaC_collect (lua_State *L, int all);
void luaC_checkGC (lua_State *L);
#endif

View File

@@ -1,5 +1,5 @@
/*
** $Id: liolib.c,v 1.83 2000/09/13 20:12:14 roberto Exp roberto $
** $Id: liolib.c,v 1.90 2000/10/27 16:15:53 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@@ -159,18 +159,9 @@ static int io_close (lua_State *L) {
static int file_collect (lua_State *L) {
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
lua_pop(L, 1); /* remove upvalue */
if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) {
/* collecting `ctrl' itself */
lua_unref(L, ctrl->ref[INFILE]);
lua_unref(L, ctrl->ref[OUTFILE]);
free(ctrl);
}
else { /* collecting a file: Close it */
FILE *f = getnonullfile(L, ctrl, 1);
if (f != stdin && f != stdout && f != stderr)
CLOSEFILE(L, f);
}
FILE *f = getnonullfile(L, ctrl, 1);
if (f != stdin && f != stdout && f != stderr)
CLOSEFILE(L, f);
return 0;
}
@@ -324,7 +315,7 @@ static int read_word (lua_State *L, FILE *f) {
}
ungetc(c, f);
luaL_pushresult(&b); /* close buffer */
return (lua_strlen(L, 1) > 0);
return (lua_strlen(L, -1) > 0);
}
@@ -354,9 +345,12 @@ static void read_file (lua_State *L, FILE *f) {
size_t size = BUFSIZ;
char *buffer = NULL;
for (;;) {
buffer = (char *)realloc(buffer, size);
if (buffer == NULL)
char *newbuffer = (char *)realloc(buffer, size);
if (newbuffer == NULL) {
free(buffer);
lua_error(L, "not enough memory to read a file");
}
buffer = newbuffer;
len += fread(buffer+len, sizeof(char), size-len, f);
if (len < size) break; /* did not read all it could */
size *= 2;
@@ -449,7 +443,7 @@ static int io_write (lua_State *L) {
if (f) arg++;
else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
for (; arg <= lastarg; arg++) {
if (lua_type(L, arg)[2] == 'm') { /* nuMber? */ /* LUA_NUMBER */
if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */
/* optimization: could be done exactly as for strings */
status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
}
@@ -690,14 +684,13 @@ static const struct luaL_reg iolibtag[] = {
static void openwithcontrol (lua_State *L) {
IOCtrl *ctrl = (IOCtrl *)malloc(sizeof(IOCtrl));
IOCtrl *ctrl = (IOCtrl *)lua_newuserdata(L, sizeof(IOCtrl));
unsigned int i;
int ctrltag = lua_newtag(L);
ctrl->iotag = lua_newtag(L);
ctrl->closedtag = lua_newtag(L);
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
/* put `ctrl' as upvalue for these functions */
lua_pushusertag(L, ctrl, ctrltag);
lua_pushvalue(L, -1);
lua_pushcclosure(L, iolibtag[i].func, 1);
lua_setglobal(L, iolibtag[i].name);
}
@@ -712,16 +705,13 @@ static void openwithcontrol (lua_State *L) {
setfilebyname(L, ctrl, stdin, "_STDIN");
setfilebyname(L, ctrl, stdout, "_STDOUT");
setfilebyname(L, ctrl, stderr, "_STDERR");
/* delete `ctrl' when collected */
lua_pushusertag(L, ctrl, ctrltag);
lua_pushcclosure(L, file_collect, 1);
lua_settagmethod(L, ctrltag, "gc");
/* close files when collected */
lua_copytagmethods(L, ctrl->iotag, ctrltag);
lua_pushcclosure(L, file_collect, 1); /* pops `ctrl' from stack */
lua_settagmethod(L, ctrl->iotag, "gc");
}
void lua_iolibopen (lua_State *L) {
LUALIB_API void lua_iolibopen (lua_State *L) {
luaL_openl(L, iolib);
openwithcontrol(L);
}

28
llex.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: llex.c,v 1.69 2000/09/11 17:38:42 roberto Exp roberto $
** $Id: llex.c,v 1.71 2000/09/27 17:41:58 roberto Exp roberto $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@@ -58,7 +58,7 @@ void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
char buff[MAXSRC];
luaO_chunkid(buff, ls->source->str, sizeof(buff));
luaO_verror(ls->L, "%.100s;\n last token read: `%.50s' at line %d in %.80s",
luaO_verror(ls->L, "%.99s;\n last token read: `%.30s' at line %d in %.80s",
s, token, ls->linenumber, buff);
}
@@ -146,7 +146,7 @@ static const char *readname (LexState *LS) {
/* LUA_NUMBER */
static void read_number (LexState *LS, int comma) {
static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
lua_State *L = LS->L;
size_t l = 0;
checkbuffer(L, 10, l);
@@ -178,12 +178,12 @@ static void read_number (LexState *LS, int comma) {
}
}
save(L, '\0', l);
if (!luaO_str2d(L->Mbuffer, &LS->t.seminfo.r))
if (!luaO_str2d(L->Mbuffer, &seminfo->r))
luaX_error(LS, "malformed number", TK_NUMBER);
}
static void read_long_string (LexState *LS) {
static void read_long_string (LexState *LS, SemInfo *seminfo) {
lua_State *L = LS->L;
int cont = 0;
size_t l = 0;
@@ -222,11 +222,11 @@ static void read_long_string (LexState *LS) {
} endloop:
save_and_next(L, LS, l); /* skip the second ']' */
save(L, '\0', l);
LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+2, l-5);
seminfo->ts = luaS_newlstr(L, L->Mbuffer+2, l-5);
}
static void read_string (LexState *LS, int del) {
static void read_string (LexState *LS, int del, SemInfo *seminfo) {
lua_State *L = LS->L;
size_t l = 0;
checkbuffer(L, 10, l);
@@ -274,11 +274,11 @@ static void read_string (LexState *LS, int del) {
}
save_and_next(L, LS, l); /* skip delimiter */
save(L, '\0', l);
LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+1, l-3);
seminfo->ts = luaS_newlstr(L, L->Mbuffer+1, l-3);
}
int luaX_lex (LexState *LS) {
int luaX_lex (LexState *LS, SemInfo *seminfo) {
for (;;) {
switch (LS->current) {
@@ -304,7 +304,7 @@ int luaX_lex (LexState *LS) {
next(LS);
if (LS->current != '[') return '[';
else {
read_long_string(LS);
read_long_string(LS, seminfo);
return TK_STRING;
}
@@ -330,7 +330,7 @@ int luaX_lex (LexState *LS) {
case '"':
case '\'':
read_string(LS, LS->current);
read_string(LS, LS->current, seminfo);
return TK_STRING;
case '.':
@@ -345,13 +345,13 @@ int luaX_lex (LexState *LS) {
}
else if (!isdigit(LS->current)) return '.';
else {
read_number(LS, 1);
read_number(LS, 1, seminfo);
return TK_NUMBER;
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
read_number(LS, 0);
read_number(LS, 0, seminfo);
return TK_NUMBER;
case EOZ:
@@ -371,7 +371,7 @@ int luaX_lex (LexState *LS) {
TString *ts = luaS_new(LS->L, readname(LS));
if (ts->marked >= RESERVEDMARK) /* reserved word? */
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
LS->t.seminfo.ts = ts;
seminfo->ts = ts;
return TK_NAME;
}
}

16
llex.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: llex.h,v 1.29 2000/06/19 18:05:14 roberto Exp roberto $
** $Id: llex.h,v 1.30 2000/06/21 18:13:56 roberto Exp roberto $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@@ -35,14 +35,18 @@ enum RESERVED {
#define NUM_RESERVED ((int)(TK_WHILE-FIRST_RESERVED+1))
typedef union {
Number r;
TString *ts;
} SemInfo; /* semantics information */
typedef struct Token {
int token;
union {
Number r;
TString *ts;
} seminfo; /* semantics information */
SemInfo seminfo;
} Token;
typedef struct LexState {
int current; /* current character */
Token t; /* current token */
@@ -58,7 +62,7 @@ typedef struct LexState {
void luaX_init (lua_State *L);
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source);
int luaX_lex (LexState *LS);
int luaX_lex (LexState *LS, SemInfo *seminfo);
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg);
void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
void luaX_error (LexState *ls, const char *s, int token);

View File

@@ -1,5 +1,5 @@
/*
** $Id: llimits.h,v 1.13 2000/08/28 17:57:04 roberto Exp roberto $
** $Id: llimits.h,v 1.18 2000/10/09 13:47:32 roberto Exp roberto $
** Limits, basic types, and some other "installation-dependent" definitions
** See Copyright Notice in lua.h
*/
@@ -41,6 +41,14 @@
typedef LUA_NUM_TYPE Number;
/* function to convert a Number to a string */
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
#define lua_number2str(s,n) sprintf((s), NUMBER_FMT, (n))
/* function to convert a string to a Number */
#define lua_str2number(s,p) strtod((s), (p))
typedef unsigned long lint32; /* unsigned int with at least 32 bits */
@@ -57,13 +65,6 @@ typedef unsigned long lint32; /* unsigned int with at least 32 bits */
#define IntPoint(p) (((unsigned long)(p)) >> 3)
/*
** number of `blocks' for garbage collection: each reference to other
** objects count 1, and each 32 bytes of `raw' memory count 1; we add
** 2 to the total as a minimum (and also to count the overhead of malloc)
*/
#define numblocks(L, o,b) ((o)+((b)>>5)+2)
#define MINPOWER2 4 /* minimum size for "growing" vectors */
@@ -75,9 +76,14 @@ typedef unsigned long lint32; /* unsigned int with at least 32 bits */
/* type to ensure maximum alignment */
union L_Umaxalign { double d; char *s; long l; };
/*
** type for virtual-machine instructions
** must be an unsigned with 4 bytes (see details in lopcodes.h)
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
** For a very small machine, you may change that to 2 bytes (and adjust
** the following limits accordingly)
*/
@@ -103,7 +109,7 @@ typedef unsigned long Instruction;
/*
** limits for opcode arguments.
** we use (signed) int to manipulate most arguments,
** so they must fit in BITS_INT-1 bits (-1 for signal)
** so they must fit in BITS_INT-1 bits (-1 for sign)
*/
#if SIZE_U < BITS_INT-1
#define MAXARG_U ((1<<SIZE_U)-1)
@@ -195,10 +201,4 @@ typedef unsigned long Instruction;
#endif
/* maximum part of a file name kept for error messages */
#ifndef MAXFILENAME
#define MAXFILENAME 260
#endif
#endif

View File

@@ -1,5 +1,5 @@
/*
** $Id: lmathlib.c,v 1.27 2000/08/28 17:57:04 roberto Exp roberto $
** $Id: lmathlib.c,v 1.31 2000/10/27 16:15:53 roberto Exp roberto $
** Standard mathematical library
** See Copyright Notice in lua.h
*/
@@ -228,12 +228,11 @@ static const struct luaL_reg mathlib[] = {
/*
** Open math library
*/
void lua_mathlibopen (lua_State *L) {
LUALIB_API void lua_mathlibopen (lua_State *L) {
luaL_openl(L, mathlib);
lua_pushnumber(L, 0); /* to get its tag */
lua_pushcfunction(L, math_pow);
lua_settagmethod(L, lua_tag(L, -2), "pow");
lua_pop(L, 1); /* remove number */
lua_pushnumber(L, PI); lua_setglobal(L, "PI");
lua_settagmethod(L, LUA_TNUMBER, "pow");
lua_pushnumber(L, PI);
lua_setglobal(L, "PI");
}

31
lmem.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lmem.c,v 1.35 2000/08/04 19:38:35 roberto Exp roberto $
** $Id: lmem.c,v 1.38 2000/10/26 12:47:05 roberto Exp roberto $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
@@ -15,18 +15,9 @@
#include "lstate.h"
/*
** Real ISO (ANSI) systems do not need these tests;
** but some systems (Sun OS) are not that ISO...
*/
#ifdef OLD_ANSI
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
#define free(b) if (b) (free)(b)
#endif
#ifdef DEBUG
#ifdef LUA_DEBUG
/*
** {======================================================================
** Controlled version for realloc.
@@ -38,17 +29,13 @@
#include <limits.h>
#include <string.h>
#undef realloc
#undef malloc
#undef free
#define realloc(b, s) debug_realloc(b, s)
#define malloc(b) debug_realloc(NULL, 0)
#define malloc(b) debug_realloc(NULL, b)
#define free(b) debug_realloc(b, 0)
/* ensures maximum alignment for HEADER */
union L_U { double d; char *s; long l; };
#define HEADER (sizeof(union L_U))
#define HEADER (sizeof(union L_Umaxalign))
#define MARKSIZE 16
#define MARK 0x55 /* 01010101 (a nice pattern) */
@@ -119,6 +106,16 @@ static void *debug_realloc (void *block, size_t size) {
/*
** Real ISO (ANSI) systems do not need these tests;
** but some systems (Sun OS) are not that ISO...
*/
#ifdef OLD_ANSI
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
#define free(b) if (b) (free)(b)
#endif
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
int inc, size_t size, const char *errormsg, size_t limit) {
size_t newn = nelems+inc;

4
lmem.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: lmem.h,v 1.14 2000/05/24 13:54:49 roberto Exp roberto $
** $Id: lmem.h,v 1.15 2000/08/07 18:39:16 roberto Exp roberto $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
@@ -30,7 +30,7 @@ void *luaM_growaux (lua_State *L, void *block, size_t nelems,
((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t)))
#ifdef DEBUG
#ifdef LUA_DEBUG
extern unsigned long memdebug_numblocks;
extern unsigned long memdebug_total;
extern unsigned long memdebug_maxmem;

139
lobject.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 1.47 2000/09/11 20:29:27 roberto Exp roberto $
** $Id: lobject.c,v 1.54 2000/10/10 19:53:20 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@@ -17,18 +17,15 @@
#include "lstate.h"
/*
** you can use the fact that the 3rd letter or each name is always different
** (e-m-r-b-n-l) to compare and switch these strings
*/
const char *const luaO_typenames[] = { /* ORDER LUA_T */
"userdata", "number", "string", "table", "function", "function", "nil",
"function", "function"
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
const char *const luaO_typenames[] = {
"userdata", "nil", "number", "string", "table", "function"
};
const TObject luaO_nilobject = {TAG_NIL, {NULL}};
/*
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
@@ -43,17 +40,17 @@ lint32 luaO_power2 (lint32 n) {
int luaO_equalObj (const TObject *t1, const TObject *t2) {
if (ttype(t1) != ttype(t2)) return 0;
switch (ttype(t1)) {
case TAG_NUMBER:
case LUA_TNUMBER:
return nvalue(t1) == nvalue(t2);
case TAG_STRING: case TAG_USERDATA:
case LUA_TSTRING: case LUA_TUSERDATA:
return tsvalue(t1) == tsvalue(t2);
case TAG_TABLE:
case LUA_TTABLE:
return hvalue(t1) == hvalue(t2);
case TAG_CCLOSURE: case TAG_LCLOSURE:
case LUA_TFUNCTION:
return clvalue(t1) == clvalue(t2);
default:
LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
return 1; /* TAG_NIL */
LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
return 1; /* LUA_TNIL */
}
}
@@ -61,76 +58,31 @@ int luaO_equalObj (const TObject *t1, const TObject *t2) {
char *luaO_openspace (lua_State *L, size_t n) {
if (n > L->Mbuffsize) {
luaM_reallocvector(L, L->Mbuffer, n, char);
L->nblocks += (n - L->Mbuffsize)*sizeof(char);
L->Mbuffsize = n;
}
return L->Mbuffer;
}
static double expten (unsigned int e) {
double exp = 10.0;
double res = 1.0;
for (; e; e>>=1) {
if (e & 1) res *= exp;
exp *= exp;
}
return res;
}
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
double a = 0.0;
int point = 0; /* number of decimal digits */
int sig;
while (isspace((unsigned char)*s)) s++;
sig = 0;
switch (*s) {
case '-': sig = 1; /* go through */
case '+': s++;
}
if (! (isdigit((unsigned char)*s) ||
(*s == '.' && isdigit((unsigned char)*(s+1)))))
return 0; /* not (at least one digit before or after the point) */
while (isdigit((unsigned char)*s))
a = 10.0*a + (*(s++)-'0');
if (*s == '.') {
s++;
while (isdigit((unsigned char)*s)) {
a = 10.0*a + (*(s++)-'0');
point++;
}
}
if (sig) a = -a;
if (*s == 'e' || *s == 'E') {
int e = 0;
s++;
sig = 0;
switch (*s) {
case '-': sig = 1; /* go through */
case '+': s++;
}
if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
do {
e = 10*e + (*(s++)-'0');
} while (isdigit((unsigned char)*s));
if (sig) e = -e;
point -= e;
}
while (isspace((unsigned char)*s)) s++;
if (*s != '\0') return 0; /* invalid trailing characters? */
if (point != 0) {
if (point > 0) a /= expten(point);
else a *= expten(-point);
}
*result = a;
char *endptr;
Number res = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* no conversion */
while (isspace((unsigned char)*endptr)) endptr++;
if (*endptr != '\0') return 0; /* invalid trailing characters? */
*result = res;
return 1;
}
/* this function needs to handle only '%d' and '%.XXXs' formats */
/* maximum length of a string format for `luaO_verror' */
#define MAX_VERROR 280
/* this function needs to handle only '%d' and '%.XXs' formats */
void luaO_verror (lua_State *L, const char *fmt, ...) {
char buff[500];
va_list argp;
char buff[MAX_VERROR]; /* to hold formatted message */
va_start(argp, fmt);
vsprintf(buff, fmt, argp);
va_end(argp);
@@ -138,23 +90,36 @@ void luaO_verror (lua_State *L, const char *fmt, ...) {
}
#define EXTRALEN sizeof("string \"...\"0")
void luaO_chunkid (char *out, const char *source, int len) {
if (*source == '(') {
strncpy(out, source+1, len-1); /* remove first char */
out[len-1] = '\0'; /* make sure `out' has an end */
out[strlen(out)-1] = '\0'; /* remove last char */
void luaO_chunkid (char *out, const char *source, int bufflen) {
if (*source == '=') {
strncpy(out, source+1, bufflen); /* remove first char */
out[bufflen-1] = '\0'; /* ensures null termination */
}
else {
len -= EXTRALEN;
if (*source == '@')
sprintf(out, "file `%.*s'", len, source+1);
if (*source == '@') {
int l;
source++; /* skip the `@' */
bufflen -= sizeof("file `...%s'");
l = strlen(source);
if (l>bufflen) {
source += (l-bufflen); /* get last part of file name */
sprintf(out, "file `...%.99s'", source);
}
else
sprintf(out, "file `%.99s'", source);
}
else {
const char *b = strchr(source , '\n'); /* stop at first new line */
int lim = (b && (b-source)<len) ? b-source : len;
sprintf(out, "string \"%.*s\"", lim, source);
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
int len = strcspn(source, "\n"); /* stop at first newline */
bufflen -= sizeof("string \"%.*s...\"");
if (len > bufflen) len = bufflen;
if (source[len] != '\0') { /* must truncate? */
strcpy(out, "string \"");
out += strlen(out);
strncpy(out, source, len);
strcpy(out+len, "...\"");
}
else
sprintf(out, "string \"%.99s\"", source);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 1.75 2000/09/11 17:38:42 roberto Exp roberto $
** $Id: lobject.h,v 1.81 2000/10/30 16:29:59 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@@ -12,7 +12,7 @@
#include "lua.h"
#ifdef DEBUG
#ifdef LUA_DEBUG
#undef NDEBUG
#include <assert.h>
#define LUA_INTERNALERROR(s) assert(((void)s,0))
@@ -23,7 +23,7 @@
#endif
#ifdef DEBUG
#ifdef LUA_DEBUG
/* to avoid warnings, and make sure value is really unused */
#define UNUSED(x) (x=0, (void)(x))
#else
@@ -31,41 +31,24 @@
#endif
/*
** Lua TYPES
** WARNING: if you change the order of this enumeration,
** grep "ORDER LUA_T"
*/
typedef enum {
TAG_USERDATA = 0, /* default tag for userdata */
TAG_NUMBER, /* fixed tag for numbers */
TAG_STRING, /* fixed tag for strings */
TAG_TABLE, /* default tag for tables */
TAG_LCLOSURE, /* fixed tag for Lua closures */
TAG_CCLOSURE, /* fixed tag for C closures */
TAG_NIL, /* last "pre-defined" tag */
/* mark for closures active in the stack */
#define LUA_TMARK 6
TAG_LMARK, /* mark for Lua closures */
TAG_CMARK /* mark for C closures */
} lua_Type;
/* tags for values visible from Lua == first user-created tag */
#define NUM_TAGS 7
#define NUM_TAGS 6
/*
** check whether `t' is a mark
*/
#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
/* check whether `t' is a mark */
#define is_T_MARK(t) ((t) == LUA_TMARK)
typedef union {
struct TString *ts; /* TAG_STRING, TAG_USERDATA */
struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_CMARK */
struct Hash *a; /* TAG_TABLE */
struct CallInfo *i; /* TAG_LMARK */
Number n; /* TAG_NUMBER */
struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */
struct Closure *cl; /* LUA_TFUNCTION */
struct Hash *a; /* LUA_TTABLE */
struct CallInfo *i; /* LUA_TLMARK */
Number n; /* LUA_TNUMBER */
} Value;
@@ -80,7 +63,7 @@ typedef union {
typedef struct lua_TObject {
lua_Type ttype;
int ttype;
Value value;
} TObject;
@@ -88,11 +71,18 @@ typedef struct lua_TObject {
/*
** String headers for string table
*/
/*
** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK
** tries to make sizeof(TString) a multiple of this granularity, to reduce
** waste of space.
*/
#define TSPACK ((int)sizeof(int))
typedef struct TString {
union {
struct { /* for strings */
unsigned long hash;
size_t len;
int constindex; /* hint to reuse constants */
} s;
struct { /* for userdata */
@@ -100,9 +90,10 @@ typedef struct TString {
void *value;
} d;
} u;
size_t len;
struct TString *nexthash; /* chain for hash table */
unsigned char marked;
char str[1]; /* variable length string!! must be the last field! */
int marked;
char str[TSPACK]; /* variable length string!! must be the last field! */
} TString;
@@ -116,14 +107,16 @@ typedef struct Proto {
int nkstr; /* size of `kstr' */
struct Proto **kproto; /* functions defined inside the function */
int nkproto; /* size of `kproto' */
Instruction *code; /* ends with opcode ENDCODE */
int numparams;
int is_vararg;
int maxstacksize;
Instruction *code;
int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
short numparams;
short is_vararg;
short maxstacksize;
short marked;
struct Proto *next;
int marked;
/* debug information */
int *lineinfo; /* map from opcodes to source lines */
int nlineinfo; /* size of `lineinfo' */
int nlocvars;
struct LocVar *locvars; /* information about local variables */
int lineDefined;
@@ -148,11 +141,15 @@ typedef struct Closure {
} f;
struct Closure *next;
struct Closure *mark; /* marked closures (point to itself when not marked) */
int nupvalues;
short isC; /* 0 for Lua functions, 1 for C functions */
short nupvalues;
TObject upvalue[1];
} Closure;
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
typedef struct Node {
TObject key;
TObject val;
@@ -187,10 +184,12 @@ typedef struct CallInfo {
} CallInfo;
extern const char *const luaO_typenames[];
extern const TObject luaO_nilobject;
extern const char *const luaO_typenames[];
#define luaO_typename(o) (luaO_typenames[ttype(o)])
#define luaO_typename(o) luaO_typenames[ttype(o)]
lint32 luaO_power2 (lint32 n);
char *luaO_openspace (lua_State *L, size_t n);

View File

@@ -1,5 +1,5 @@
/*
** $Id: lopcodes.h,v 1.66 2000/08/15 18:28:48 roberto Exp roberto $
** $Id: lopcodes.h,v 1.67 2000/08/29 14:48:16 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -91,7 +91,7 @@ OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
OP_PUSHNIL,/* U - nil_1-nil_u */
OP_POP,/* U a_u-a_1 - */
OP_PUSHINT,/* S - (Number)s */
OP_PUSHINT,/* S - (Number)s */
OP_PUSHSTRING,/* K - KSTR[k] */
OP_PUSHNUM,/* N - KNUM[n] */
OP_PUSHNEGNUM,/* N - -KNUM[n] */

View File

@@ -1,5 +1,5 @@
/*
** $Id: lparser.c,v 1.111 2000/08/31 14:08:27 roberto Exp roberto $
** $Id: lparser.c,v 1.115 2000/10/10 19:51:15 roberto Exp roberto $
** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h
*/
@@ -60,13 +60,13 @@ static void next (LexState *ls) {
ls->lookahead.token = TK_EOS; /* and discharge it */
}
else
ls->t.token = luaX_lex(ls); /* read next token */
ls->t.token = luaX_lex(ls, &ls->t.seminfo); /* read next token */
}
static void lookahead (LexState *ls) {
LUA_ASSERT(ls->lookahead.token == TK_EOS, "two look-aheads");
ls->lookahead.token = luaX_lex(ls);
ls->lookahead.token = luaX_lex(ls, &ls->lookahead.seminfo);
}
@@ -315,7 +315,6 @@ static void open_func (LexState *ls, FuncState *fs) {
f->source = ls->source;
fs->pc = 0;
fs->lasttarget = 0;
fs->nlineinfo = 0;
fs->lastline = 0;
fs->jlt = NO_JUMP;
f->code = NULL;
@@ -337,8 +336,9 @@ static void close_func (LexState *ls) {
luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
removelocalvars(ls, fs->nactloc);
luaM_reallocvector(L, f->locvars, f->nlocvars, LocVar);
luaM_reallocvector(L, f->lineinfo, fs->nlineinfo+1, int);
f->lineinfo[fs->nlineinfo] = MAX_INT; /* end flag */
luaM_reallocvector(L, f->lineinfo, f->nlineinfo+1, int);
f->lineinfo[f->nlineinfo++] = MAX_INT; /* end flag */
luaF_protook(L, f, fs->pc); /* proto is ok now */
ls->fs = fs->prev;
LUA_ASSERT(fs->bl == NULL, "wrong list end");
}
@@ -859,8 +859,8 @@ static void fornum (LexState *ls, TString *varname) {
else
luaK_code1(fs, OP_PUSHINT, 1); /* default step */
new_localvar(ls, varname, 0);
new_localvarstr(ls, "*limit*", 1);
new_localvarstr(ls, "*step*", 2);
new_localvarstr(ls, "(limit)", 1);
new_localvarstr(ls, "(step)", 2);
forbody(ls, 3, OP_FORPREP, OP_FORLOOP);
}
@@ -876,7 +876,7 @@ static void forlist (LexState *ls, TString *indexname) {
"`in' expected");
next(ls); /* skip `in' */
exp1(ls); /* table */
new_localvarstr(ls, "*table*", 0);
new_localvarstr(ls, "(table)", 0);
new_localvar(ls, indexname, 1);
new_localvar(ls, valname, 2);
forbody(ls, 3, OP_LFORPREP, OP_LFORLOOP);

View File

@@ -1,5 +1,5 @@
/*
** $Id: lparser.h,v 1.23 2000/08/22 17:44:17 roberto Exp roberto $
** $Id: lparser.h,v 1.25 2000/09/29 12:42:13 roberto Exp roberto $
** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h
*/
@@ -44,11 +44,10 @@ typedef struct FuncState {
int pc; /* next position to code */
int lasttarget; /* `pc' of last `jump target' */
int jlt; /* list of jumps to `lasttarget' */
int stacklevel; /* number of values on activation register */
int nactloc; /* number of active local variables */
int nupvalues; /* number of upvalues */
short stacklevel; /* number of values on activation register */
short nactloc; /* number of active local variables */
short nupvalues; /* number of upvalues */
int lastline; /* line where last `lineinfo' was generated */
int nlineinfo; /* index of next `lineinfo' to be generated */
struct Breaklabel *bl; /* chain of breakable blocks */
expdesc upvalues[MAXUPVALUES]; /* upvalues */
int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */

View File

@@ -1,5 +1,5 @@
/*
** $Id: lstate.c,v 1.39 2000/09/12 18:42:32 roberto Exp roberto $
** $Id: lstate.c,v 1.47 2000/10/26 12:47:05 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@@ -19,8 +19,8 @@
#include "ltm.h"
#ifdef DEBUG
extern lua_State *lua_state;
#ifdef LUA_DEBUG
static lua_State *lua_state = NULL;
void luaB_opentests (lua_State *L);
#endif
@@ -38,8 +38,32 @@ static int errormessage (lua_State *L) {
}
lua_State *lua_open (int stacksize) {
struct lua_longjmp myErrorJmp;
/*
** open parts that may cause memory-allocation errors
*/
static void f_luaopen (lua_State *L, void *ud) {
int stacksize = *(int *)ud;
if (stacksize == 0)
stacksize = DEFAULT_STACK_SIZE;
else
stacksize += LUA_MINSTACK;
L->gt = luaH_new(L, 10); /* table of globals */
luaD_init(L, stacksize);
luaS_init(L);
luaX_init(L);
luaT_init(L);
lua_newtable(L);
lua_ref(L, 1); /* create registry */
lua_register(L, LUA_ERRORMESSAGE, errormessage);
#ifdef LUA_DEBUG
luaB_opentests(L);
if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
#endif
LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
}
LUA_API lua_State *lua_open (int stacksize) {
lua_State *L = luaM_new(NULL, lua_State);
if (L == NULL) return NULL; /* memory allocation error */
L->stack = NULL;
@@ -52,50 +76,44 @@ lua_State *lua_open (int stacksize) {
L->rootproto = NULL;
L->rootcl = NULL;
L->roottable = NULL;
L->IMtable = NULL;
L->TMtable = NULL;
L->last_tag = -1;
L->refArray = NULL;
L->refSize = 0;
L->refFree = NONEXT;
L->nblocks = 0;
L->nblocks = sizeof(lua_State);
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
L->callhook = NULL;
L->linehook = NULL;
L->allowhooks = 1;
L->errorJmp = &myErrorJmp;
if (setjmp(myErrorJmp.b) == 0) { /* to catch memory allocation errors */
L->gt = luaH_new(L, 10);
luaD_init(L, (stacksize == 0) ? DEFAULT_STACK_SIZE :
stacksize+LUA_MINSTACK);
luaS_init(L);
luaX_init(L);
luaT_init(L);
lua_register(L, LUA_ERRORMESSAGE, errormessage);
#ifdef DEBUG
luaB_opentests(L);
#endif
L->GCthreshold = L->nblocks*4;
L->errorJmp = NULL;
return L;
}
else { /* memory allocation error: free partial state */
L->errorJmp = NULL;
if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
/* memory allocation error: free partial state */
lua_close(L);
return NULL;
}
L->GCthreshold = 2*L->nblocks;
return L;
}
void lua_close (lua_State *L) {
LUA_API void lua_close (lua_State *L) {
LUA_ASSERT(L != lua_state || lua_gettop(L) == 0, "garbage in C stack");
luaC_collect(L, 1); /* collect all elements */
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
LUA_ASSERT(L->roottable == NULL, "list should be empty");
luaS_freeall(L);
if (L->stack)
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
luaM_free(L, L->stack);
luaM_free(L, L->IMtable);
L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
luaM_free(L, L->TMtable);
L->nblocks -= (L->refSize)*sizeof(struct Ref);
luaM_free(L, L->refArray);
L->nblocks -= (L->Mbuffsize)*sizeof(char);
luaM_free(L, L->Mbuffer);
LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
luaM_free(L, L);
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");

View File

@@ -1,5 +1,5 @@
/*
** $Id: lstate.h,v 1.37 2000/08/28 17:57:04 roberto Exp roberto $
** $Id: lstate.h,v 1.40 2000/09/29 12:42:13 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@@ -7,8 +7,6 @@
#ifndef lstate_h
#define lstate_h
#include <setjmp.h>
#include "lobject.h"
#include "lua.h"
#include "luadebug.h"
@@ -33,15 +31,8 @@ struct Ref {
};
/*
** chain list of long jumps
*/
struct lua_longjmp {
jmp_buf b;
struct lua_longjmp *previous;
volatile int status; /* error code */
StkId base;
};
struct lua_longjmp; /* defined in ldo.c */
struct TM; /* defined in ltm.h */
typedef struct stringtable {
@@ -69,13 +60,13 @@ struct lua_State {
stringtable strt; /* hash table for strings */
stringtable udt; /* hash table for udata */
Hash *gt; /* table for globals */
struct IM *IMtable; /* table for tag methods */
int last_tag; /* last used tag in IMtable */
struct TM *TMtable; /* table for tag methods */
int last_tag; /* last used tag in TMtable */
struct Ref *refArray; /* locked objects */
int refSize; /* size of refArray */
int refFree; /* list of free positions in refArray */
unsigned long GCthreshold;
unsigned long nblocks; /* number of `blocks' currently allocated */
unsigned long nblocks; /* number of `bytes' currently allocated */
lua_Hook callhook;
lua_Hook linehook;
int allowhooks;

View File

@@ -1,5 +1,5 @@
/*
** $Id: lstring.c,v 1.41 2000/08/04 19:38:35 roberto Exp roberto $
** $Id: lstring.c,v 1.44 2000/10/26 12:47:05 roberto Exp roberto $
** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h
*/
@@ -15,10 +15,20 @@
#include "lstring.h"
/*
** type equivalent to TString, but with maximum alignment requirements
*/
union L_UTString {
TString ts;
union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
};
void luaS_init (lua_State *L) {
L->strt.hash = luaM_newvector(L, 1, TString *);
L->udt.hash = luaM_newvector(L, 1, TString *);
L->nblocks += 2*sizeof(TString *);
L->strt.size = L->udt.size = 1;
L->strt.nuse = L->udt.nuse = 0;
L->strt.hash[0] = L->udt.hash[0] = NULL;
@@ -27,6 +37,7 @@ void luaS_init (lua_State *L) {
void luaS_freeall (lua_State *L) {
LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
L->nblocks -= (L->strt.size + L->udt.size)*sizeof(TString *);
luaM_free(L, L->strt.hash);
LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
luaM_free(L, L->udt.hash);
@@ -61,6 +72,7 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
}
}
luaM_free(L, tb->hash);
L->nblocks += (newsize - tb->size)*sizeof(TString *);
tb->size = newsize;
tb->hash = newhash;
}
@@ -78,43 +90,54 @@ static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
unsigned long h = hash_s(str, l);
int h1 = h&(L->strt.size-1);
int h1 = h & (L->strt.size-1);
TString *ts;
for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
if (ts->len == l && (memcmp(str, ts->str, l) == 0))
return ts;
}
/* not found */
ts = (TString *)luaM_malloc(L, sizeof(TString)+(lint32)l*sizeof(char));
ts = (TString *)luaM_malloc(L, sizestring(l));
ts->marked = 0;
ts->nexthash = NULL;
ts->u.s.len = l;
ts->len = l;
ts->u.s.hash = h;
ts->u.s.constindex = 0;
memcpy(ts->str, str, l);
ts->str[l] = 0; /* ending 0 */
L->nblocks += gcsizestring(L, l);
L->nblocks += sizestring(l);
newentry(L, &L->strt, ts, h1); /* insert it on table */
return ts;
}
TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
union L_UTString *uts = (union L_UTString *)luaM_malloc(L,
(lint32)sizeof(union L_UTString)+s);
TString *ts = &uts->ts;
ts->marked = 0;
ts->nexthash = NULL;
ts->len = s;
ts->u.d.tag = 0;
ts->u.d.value = (udata == NULL) ? uts+1 : udata;
L->nblocks += sizestring(s);
/* insert it on table */
newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));
return ts;
}
TString *luaS_createudata (lua_State *L, void *udata, int tag) {
unsigned long h = IntPoint(udata);
int h1 = h&(L->udt.size-1);
int h1 = IntPoint(udata) & (L->udt.size-1);
TString *ts;
for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
return ts;
}
/* not found */
ts = luaM_new(L, TString);
ts->marked = 0;
ts->nexthash = NULL;
ts->u.d.value = udata;
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
L->nblocks += gcsizeudata;
newentry(L, &L->udt, ts, h1); /* insert it on table */
ts = luaS_newudata(L, 0, udata);
if (tag != LUA_ANYTAG)
ts->u.d.tag = tag;
return ts;
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: lstring.h,v 1.20 2000/05/10 16:33:20 roberto Exp roberto $
** $Id: lstring.h,v 1.23 2000/10/26 12:47:05 roberto Exp roberto $
** String table (keep all strings handled by Lua)
** See Copyright Notice in lua.h
*/
@@ -20,12 +20,13 @@
#define RESERVEDMARK 3
#define gcsizestring(L, l) numblocks(L, 0, sizeof(TString)+l)
#define gcsizeudata gcsizestring(L, 0)
#define sizestring(l) ((long)sizeof(TString) + \
((long)(l+1)-TSPACK)*(long)sizeof(char))
void luaS_init (lua_State *L);
void luaS_resize (lua_State *L, stringtable *tb, int newsize);
TString *luaS_newudata (lua_State *L, size_t s, void *udata);
TString *luaS_createudata (lua_State *L, void *udata, int tag);
void luaS_freeall (lua_State *L);
TString *luaS_newlstr (lua_State *L, const char *str, size_t l);

View File

@@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.52 2000/09/11 17:38:42 roberto Exp roberto $
** $Id: lstrlib.c,v 1.55 2000/10/20 16:39:03 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@@ -525,7 +525,7 @@ static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
luaL_putchar(b, '"');
}
/* maximum size of each formated item (> len(format('%99.99f', -1e308))) */
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
#define MAX_ITEM 512
/* maximum size of each format specification (such as '%-099.99d') */
#define MAX_FORMAT 20
@@ -616,6 +616,6 @@ static const struct luaL_reg strlib[] = {
/*
** Open string library
*/
void lua_strlibopen (lua_State *L) {
LUALIB_API void lua_strlibopen (lua_State *L) {
luaL_openl(L, strlib);
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: ltable.c,v 1.54 2000/08/31 14:08:27 roberto Exp roberto $
** $Id: ltable.c,v 1.57 2000/10/05 12:14:08 roberto Exp roberto $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -27,11 +27,11 @@
#include "ltable.h"
#define gcsize(L, n) numblocks(L, n*2, sizeof(Hash))
#define gcsize(L, n) (sizeof(Hash)+(n)*sizeof(Node))
#define TagDefault TAG_TABLE
#define TagDefault LUA_TTABLE
@@ -42,19 +42,19 @@
Node *luaH_mainposition (const Hash *t, const TObject *key) {
unsigned long h;
switch (ttype(key)) {
case TAG_NUMBER:
case LUA_TNUMBER:
h = (unsigned long)(long)nvalue(key);
break;
case TAG_STRING:
case LUA_TSTRING:
h = tsvalue(key)->u.s.hash;
break;
case TAG_USERDATA:
case LUA_TUSERDATA:
h = IntPoint(tsvalue(key));
break;
case TAG_TABLE:
case LUA_TTABLE:
h = IntPoint(hvalue(key));
break;
case TAG_LCLOSURE: case TAG_CCLOSURE:
case LUA_TFUNCTION:
h = IntPoint(clvalue(key));
break;
default:
@@ -84,7 +84,7 @@ static const TObject *luaH_getany (lua_State *L, const Hash *t,
const TObject *luaH_getnum (const Hash *t, Number key) {
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
do {
if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key)
if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key)
return &n->val;
n = n->next;
} while (n);
@@ -96,7 +96,7 @@ const TObject *luaH_getnum (const Hash *t, Number key) {
const TObject *luaH_getstr (const Hash *t, TString *key) {
Node *n = &t->node[key->u.s.hash&(t->size-1)];
do {
if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key)
if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key)
return &n->val;
n = n->next;
} while (n);
@@ -106,8 +106,8 @@ const TObject *luaH_getstr (const Hash *t, TString *key) {
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
switch (ttype(key)) {
case TAG_NUMBER: return luaH_getnum(t, nvalue(key));
case TAG_STRING: return luaH_getstr(t, tsvalue(key));
case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
default: return luaH_getany(L, t, key);
}
}
@@ -115,7 +115,7 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
int i;
if (ttype(key) == TAG_NIL)
if (ttype(key) == LUA_TNIL)
i = 0; /* first iteration */
else {
const TObject *v = luaH_get(L, t, key);
@@ -126,7 +126,7 @@ Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
}
for (; i<t->size; i++) {
Node *n = node(t, i);
if (ttype(val(n)) != TAG_NIL)
if (ttype(val(n)) != LUA_TNIL)
return n;
}
return NULL; /* no more elements */
@@ -138,8 +138,8 @@ Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
** hash, change `key' for a number with the same hash.
*/
void luaH_remove (Hash *t, TObject *key) {
if (ttype(key) == TAG_NUMBER ||
(ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30))
if (ttype(key) == LUA_TNUMBER ||
(ttype(key) == LUA_TSTRING && tsvalue(key)->len <= 30))
return; /* do not remove numbers nor small strings */
else {
/* try to find a number `n' with the same hash as `key' */
@@ -151,7 +151,7 @@ void luaH_remove (Hash *t, TObject *key) {
return; /* give up; (to avoid overflow) */
n += t->size;
}
ttype(key) = TAG_NUMBER;
ttype(key) = LUA_TNUMBER;
nvalue(key) = n;
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
}
@@ -164,7 +164,7 @@ static void setnodevector (lua_State *L, Hash *t, lint32 size) {
lua_error(L, "table overflow");
t->node = luaM_newvector(L, size, Node);
for (i=0; i<(int)size; i++) {
ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL;
ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL;
t->node[i].next = NULL;
}
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
@@ -200,7 +200,7 @@ static int numuse (const Hash *t) {
int realuse = 0;
int i;
for (i=0; i<size; i++) {
if (ttype(&v[i].val) != TAG_NIL)
if (ttype(&v[i].val) != LUA_TNIL)
realuse++;
}
return realuse;
@@ -222,7 +222,7 @@ static void rehash (lua_State *L, Hash *t) {
setnodevector(L, t, oldsize);
for (i=0; i<oldsize; i++) {
Node *old = nold+i;
if (ttype(&old->val) != TAG_NIL)
if (ttype(&old->val) != LUA_TNIL)
*luaH_set(L, t, &old->key) = old->val;
}
luaM_free(L, nold); /* free old array */
@@ -248,7 +248,7 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
else n = n->next;
} while (n);
/* `key' not found; must insert it */
if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */
Node *othern; /* main position of colliding node */
n = t->firstfree; /* get a free place */
/* is colliding node out of its main position? (can only happens if
@@ -269,7 +269,7 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
}
mp->key = *key;
for (;;) { /* correct `firstfree' */
if (ttype(&t->firstfree->key) == TAG_NIL)
if (ttype(&t->firstfree->key) == LUA_TNIL)
return &mp->val; /* OK; table still has a free place */
else if (t->firstfree == t->node) break; /* cannot decrement from here */
else (t->firstfree)--;
@@ -281,7 +281,7 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
TObject *luaH_setint (lua_State *L, Hash *t, int key) {
TObject index;
ttype(&index) = TAG_NUMBER;
ttype(&index) = LUA_TNUMBER;
nvalue(&index) = key;
return luaH_set(L, t, &index);
}
@@ -289,10 +289,10 @@ TObject *luaH_setint (lua_State *L, Hash *t, int key) {
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
TObject *value, index;
ttype(&index) = TAG_STRING;
ttype(&index) = LUA_TSTRING;
tsvalue(&index) = key;
value = luaH_set(L, t, &index);
ttype(value) = TAG_NUMBER;
ttype(value) = LUA_TNUMBER;
nvalue(value) = val;
}

113
ltests.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: ltests.c,v 1.41 2000/09/11 19:42:57 roberto Exp roberto $
** $Id: ltests.c,v 1.53 2000/10/30 16:29:59 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/
@@ -32,9 +32,9 @@ void luaB_opentests (lua_State *L);
/*
** The whole module only makes sense with DEBUG on
** The whole module only makes sense with LUA_DEBUG on
*/
#ifdef DEBUG
#ifdef LUA_DEBUG
@@ -72,7 +72,7 @@ static int pushop (lua_State *L, Proto *p, int pc) {
sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
switch ((enum Mode)luaK_opproperties[o].mode) {
case iO:
sprintf(buff+8, "%s", name);
sprintf(buff+8, "%-12s", name);
break;
case iU:
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
@@ -93,7 +93,8 @@ static int listcode (lua_State *L) {
int pc;
Proto *p;
int res;
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
p = clvalue(luaA_index(L, 1))->f.l;
lua_newtable(L);
setnameval(L, "maxstack", p->maxstacksize);
@@ -111,7 +112,8 @@ static int listcode (lua_State *L) {
static int liststrings (lua_State *L) {
Proto *p;
int i;
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
p = clvalue(luaA_index(L, 1))->f.l;
lua_newtable(L);
for (i=0; i<p->nkstr; i++) {
@@ -128,7 +130,8 @@ static int listlocals (lua_State *L) {
int pc = luaL_check_int(L, 2) - 1;
int i = 0;
const char *name;
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
p = clvalue(luaA_index(L, 1))->f.l;
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
lua_pushstring(L, name);
@@ -177,12 +180,12 @@ static int mem_query (lua_State *L) {
static int hash_query (lua_State *L) {
if (lua_isnull(L, 2)) {
luaL_arg_check(L, lua_tag(L, 1) == TAG_STRING, 1, "string expected");
luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected");
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
}
else {
Hash *t;
luaL_checktype(L, 2, "table");
luaL_checktype(L, 2, LUA_TTABLE);
t = hvalue(luaA_index(L, 2));
lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
}
@@ -193,7 +196,7 @@ static int hash_query (lua_State *L) {
static int table_query (lua_State *L) {
const Hash *t;
int i = luaL_opt_int(L, 2, -1);
luaL_checktype(L, 1, "table");
luaL_checktype(L, 1, LUA_TTABLE);
t = hvalue(luaA_index(L, 1));
if (i == -1) {
lua_pushnumber(L, t->size);
@@ -226,7 +229,7 @@ static int string_query (lua_State *L) {
TString *ts;
int n = 0;
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
ttype(L->top) = TAG_STRING;
ttype(L->top) = LUA_TSTRING;
tsvalue(L->top) = ts;
incr_top;
n++;
@@ -238,7 +241,7 @@ static int string_query (lua_State *L) {
static int tref (lua_State *L) {
luaL_checktype(L, 1, "any");
luaL_checkany(L, 1);
lua_pushvalue(L, 1);
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
return 1;
@@ -257,12 +260,15 @@ static int unref (lua_State *L) {
}
static int newuserdata (lua_State *L) {
lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
if (lua_isnumber(L, 2))
lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
else
lua_newuserdata(L, luaL_check_int(L, 1));
return 1;
}
static int udataval (lua_State *L) {
luaL_checktype(L, 1, "userdata");
luaL_checktype(L, 1, LUA_TUSERDATA);
lua_pushnumber(L, (int)lua_touserdata(L, 1));
return 1;
}
@@ -290,7 +296,7 @@ static int loadlib (lua_State *L) {
}
static int closestate (lua_State *L) {
luaL_checktype(L, 1, "userdata");
luaL_checktype(L, 1, LUA_TUSERDATA);
lua_close((lua_State *)lua_touserdata(L, 1));
return 0;
}
@@ -299,7 +305,7 @@ static int doremote (lua_State *L) {
lua_State *L1;
const char *code = luaL_check_string(L, 2);
int status;
luaL_checktype(L, 1, "userdata");
luaL_checktype(L, 1, LUA_TUSERDATA);
L1 = (lua_State *)lua_touserdata(L, 1);
status = lua_dostring(L1, code);
if (status != 0) {
@@ -316,8 +322,12 @@ static int doremote (lua_State *L) {
}
static int settagmethod (lua_State *L) {
luaL_checktype(L, 3, "any");
lua_settagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
int tag = luaL_check_int(L, 1);
const char *event = luaL_check_string(L, 2);
luaL_checkany(L, 3);
lua_gettagmethod(L, tag, event);
lua_pushvalue(L, 3);
lua_settagmethod(L, tag, event);
return 1;
}
@@ -386,6 +396,45 @@ static int testC (lua_State *L) {
for (;;) {
const char *inst = getname;
if EQ("") return 0;
else if EQ("isnumber") {
lua_pushnumber(L, lua_isnumber(L, getnum));
}
else if EQ("isstring") {
lua_pushnumber(L, lua_isstring(L, getnum));
}
else if EQ("istable") {
lua_pushnumber(L, lua_istable(L, getnum));
}
else if EQ("iscfunction") {
lua_pushnumber(L, lua_iscfunction(L, getnum));
}
else if EQ("isfunction") {
lua_pushnumber(L, lua_isfunction(L, getnum));
}
else if EQ("isuserdata") {
lua_pushnumber(L, lua_isuserdata(L, getnum));
}
else if EQ("isnil") {
lua_pushnumber(L, lua_isnil(L, getnum));
}
else if EQ("isnull") {
lua_pushnumber(L, lua_isnull(L, getnum));
}
else if EQ("tonumber") {
lua_pushnumber(L, lua_tonumber(L, getnum));
}
else if EQ("tostring") {
lua_pushstring(L, lua_tostring(L, getnum));
}
else if EQ("tonumber") {
lua_pushnumber(L, lua_tonumber(L, getnum));
}
else if EQ("strlen") {
lua_pushnumber(L, lua_strlen(L, getnum));
}
else if EQ("tocfunction") {
lua_pushcfunction(L, lua_tocfunction(L, getnum));
}
else if EQ("return") {
return getnum;
}
@@ -410,19 +459,43 @@ static int testC (lua_State *L) {
else if EQ("insert") {
lua_insert(L, getnum);
}
else if EQ("gettable") {
lua_gettable(L, getnum);
}
else if EQ("settable") {
lua_settable(L, getnum);
}
else if EQ("next") {
lua_next(L, -2);
}
else if EQ("concat") {
lua_concat(L, getnum);
}
else if EQ("call") {
else if EQ("rawcall") {
int narg = getnum;
int nres = getnum;
lua_rawcall(L, narg, nres);
}
else if EQ("call") {
int narg = getnum;
int nres = getnum;
lua_call(L, narg, nres);
}
else if EQ("dostring") {
lua_dostring(L, luaL_check_string(L, getnum));
}
else if EQ("settagmethod") {
int tag = getnum;
const char *event = getname;
lua_settagmethod(L, tag, event);
}
else if EQ("gettagmethod") {
int tag = getnum;
const char *event = getname;
lua_gettagmethod(L, tag, event);
}
else if EQ("type") {
lua_pushstring(L, lua_type(L, getnum));
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
}
else luaL_verror(L, "unknown instruction %.30s", buff);
}

100
ltm.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: ltm.c,v 1.48 2000/09/11 19:45:27 roberto Exp roberto $
** $Id: ltm.c,v 1.55 2000/10/20 16:39:03 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@@ -17,7 +17,7 @@
#include "ltm.h"
const char *const luaT_eventname[] = { /* ORDER IM */
const char *const luaT_eventname[] = { /* ORDER TM */
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
"le", "gt", "ge", /* deprecated options!! */
@@ -36,9 +36,9 @@ static int findevent (const char *name) {
static int luaI_checkevent (lua_State *L, const char *name, int t) {
int e = findevent(name);
if (e >= IM_N)
if (e >= TM_N)
luaO_verror(L, "event `%.50s' is deprecated", name);
if (e == IM_GC && t == TAG_TABLE)
if (e == TM_GC && t == LUA_TTABLE)
luaO_verror(L, "event `gc' for tables is deprecated");
if (e < 0)
luaO_verror(L, "`%.50s' is not a valid event name", name);
@@ -47,45 +47,46 @@ static int luaI_checkevent (lua_State *L, const char *name, int t) {
/* events in TAG_NIL are all allowed, since this is used as a
/* events in LUA_TNIL are all allowed, since this is used as a
* 'placeholder' for "default" fallbacks
*/
/* ORDER LUA_T, ORDER IM */
static const char luaT_validevents[NUM_TAGS][IM_N] = {
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
/* ORDER LUA_T, ORDER TM */
static const char luaT_validevents[NUM_TAGS][TM_N] = {
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TTABLE */
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
};
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
return (t >= NUM_TAGS) ? 1 : luaT_validevents[t][e];
}
static void init_entry (lua_State *L, int tag) {
int i;
for (i=0; i<IM_N; i++)
ttype(luaT_getim(L, tag, i)) = TAG_NIL;
L->IMtable[tag].collected = NULL;
for (i=0; i<TM_N; i++)
luaT_gettm(L, tag, i) = NULL;
L->TMtable[tag].collected = NULL;
}
void luaT_init (lua_State *L) {
int t;
luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
luaM_growvector(L, L->TMtable, 0, NUM_TAGS, struct TM, "", MAX_INT);
L->nblocks += NUM_TAGS*sizeof(struct TM);
L->last_tag = NUM_TAGS-1;
for (t=0; t<=L->last_tag; t++)
init_entry(L, t);
}
int lua_newtag (lua_State *L) {
luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
LUA_API int lua_newtag (lua_State *L) {
luaM_growvector(L, L->TMtable, L->last_tag, 1, struct TM,
"tag table overflow", MAX_INT);
L->nblocks += sizeof(struct TM);
L->last_tag++;
init_entry(L, L->last_tag);
return L->last_tag;
@@ -98,62 +99,65 @@ static void checktag (lua_State *L, int tag) {
}
void luaT_realtag (lua_State *L, int tag) {
if (!(NUM_TAGS <= tag && tag <= L->last_tag))
if (!validtag(tag))
luaO_verror(L, "tag %d was not created by `newtag'", tag);
}
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
int e;
checktag(L, tagto);
checktag(L, tagfrom);
for (e=0; e<IM_N; e++) {
for (e=0; e<TM_N; e++) {
if (luaT_validevent(tagto, e))
*luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
luaT_gettm(L, tagto, e) = luaT_gettm(L, tagfrom, e);
}
return tagto;
}
int luaT_effectivetag (lua_State *L, const TObject *o) {
lua_Type t = ttype(o);
int luaT_tag (const TObject *o) {
int t = ttype(o);
switch (t) {
case TAG_USERDATA: {
int tag = tsvalue(o)->u.d.tag;
return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
}
case TAG_TABLE: return hvalue(o)->htag;
default: return t;
case LUA_TUSERDATA: return tsvalue(o)->u.d.tag;
case LUA_TTABLE: return hvalue(o)->htag;
default: return t;
}
}
void lua_gettagmethod (lua_State *L, int t, const char *event) {
LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
int e;
e = luaI_checkevent(L, event, t);
checktag(L, t);
if (luaT_validevent(t, e))
*L->top = *luaT_getim(L, t,e);
if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) {
clvalue(L->top) = luaT_gettm(L, t, e);
ttype(L->top) = LUA_TFUNCTION;
}
else
ttype(L->top) = TAG_NIL;
ttype(L->top) = LUA_TNIL;
incr_top;
}
void lua_settagmethod (lua_State *L, int t, const char *event) {
TObject temp;
int e;
LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
"function or nil expected");
e = luaI_checkevent(L, event, t);
LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
int e = luaI_checkevent(L, event, t);
checktag(L, t);
if (!luaT_validevent(t, e))
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
luaT_eventname[e], luaO_typenames[t],
(t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
: "");
temp = *(L->top - 1);
*(L->top - 1) = *luaT_getim(L, t,e);
*luaT_getim(L, t, e) = temp;
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
" with default tag" : "");
switch (ttype(L->top - 1)) {
case LUA_TNIL:
luaT_gettm(L, t, e) = NULL;
break;
case LUA_TFUNCTION:
luaT_gettm(L, t, e) = clvalue(L->top - 1);
break;
default:
lua_error(L, "tag method must be a function (or nil)");
}
L->top--;
}

53
ltm.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: ltm.h,v 1.14 2000/08/07 20:21:34 roberto Exp roberto $
** $Id: ltm.h,v 1.17 2000/10/05 12:14:08 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@@ -13,43 +13,46 @@
/*
* WARNING: if you change the order of this enumeration,
* grep "ORDER IM"
* grep "ORDER TM"
*/
typedef enum {
IM_GETTABLE = 0,
IM_SETTABLE,
IM_INDEX,
IM_GETGLOBAL,
IM_SETGLOBAL,
IM_ADD,
IM_SUB,
IM_MUL,
IM_DIV,
IM_POW,
IM_UNM,
IM_LT,
IM_CONCAT,
IM_GC,
IM_FUNCTION,
IM_N /* number of elements in the enum */
} IMS;
TM_GETTABLE = 0,
TM_SETTABLE,
TM_INDEX,
TM_GETGLOBAL,
TM_SETGLOBAL,
TM_ADD,
TM_SUB,
TM_MUL,
TM_DIV,
TM_POW,
TM_UNM,
TM_LT,
TM_CONCAT,
TM_GC,
TM_FUNCTION,
TM_N /* number of elements in the enum */
} TMS;
struct IM {
TObject int_method[IM_N];
TString *collected; /* list of G. collected udata with this tag */
struct TM {
Closure *method[TM_N];
TString *collected; /* list of garbage-collected udata with this tag */
};
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(L, o),(e)))
#define luaT_gettm(L,tag,event) (L->TMtable[tag].method[event])
#define luaT_gettmbyObj(L,o,e) (luaT_gettm((L),luaT_tag(o),(e)))
#define validtag(t) (NUM_TAGS <= (t) && (t) <= L->last_tag)
extern const char *const luaT_eventname[];
void luaT_init (lua_State *L);
void luaT_realtag (lua_State *L, int tag);
int luaT_effectivetag (lua_State *L, const TObject *o);
int luaT_tag (const TObject *o);
int luaT_validevent (int t, int e); /* used by compatibility module */

23
lua.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lua.c,v 1.50 2000/09/05 19:33:32 roberto Exp roberto $
** $Id: lua.c,v 1.54 2000/10/17 13:36:24 roberto Exp roberto $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/
@@ -16,8 +16,7 @@
#include "lualib.h"
lua_State *lua_state = NULL;
#define L lua_state
static lua_State *L = NULL;
#ifndef PROMPT
@@ -49,18 +48,14 @@ static lua_Hook old_linehook = NULL;
static lua_Hook old_callhook = NULL;
#ifdef USERINIT
extern void USERINIT (void);
#else
#define USERINIT userinit
static void userinit (void) {
lua_baselibopen(L);
lua_iolibopen(L);
lua_strlibopen(L);
lua_mathlibopen(L);
lua_dblibopen(L);
/* add your libraries here */
}
#endif
static handler lreset (void) {
@@ -92,10 +87,12 @@ static int ldo (int (*f)(lua_State *l, const char *), const char *name) {
res = f(L, name); /* dostring | dofile */
lua_settop(L, top); /* remove eventual results */
signal(SIGINT, h); /* restore old action */
/* Lua gives no message in such cases, so lua.c provides one */
if (res == LUA_ERRMEM) {
/* Lua gives no message in such case, so lua.c provides one */
fprintf(stderr, "lua: memory allocation error\n");
}
else if (res == LUA_ERRERR)
fprintf(stderr, "lua: error in error message\n");
return res;
}
@@ -118,7 +115,7 @@ static void print_message (void) {
static void print_version (void) {
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
}
@@ -258,7 +255,7 @@ static int handle_argv (char *argv[], struct Options *opt) {
return EXIT_FAILURE;
}
if (ldo(lua_dostring, argv[i]) != 0) {
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
fprintf(stderr, "lua: error running argument `%.99s'\n", argv[i]);
return EXIT_FAILURE;
}
break;
@@ -292,7 +289,7 @@ static void getstacksize (int argc, char *argv[], struct Options *opt) {
if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
int stacksize = atoi(&argv[1][2]);
if (stacksize <= 0) {
fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
fprintf(stderr, "lua: invalid stack size ('%.20s')\n", &argv[1][2]);
exit(EXIT_FAILURE);
}
opt->stacksize = stacksize;
@@ -315,7 +312,7 @@ int main (int argc, char *argv[]) {
opt.toclose = 0;
getstacksize(argc, argv, &opt); /* handle option `-s' */
L = lua_open(opt.stacksize); /* create state */
USERINIT(); /* open libraries */
userinit(); /* open libraries */
register_getargs(argv); /* create `getargs' function */
status = handle_argv(argv+1, &opt);
if (opt.toclose)

166
lua.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.69 2000/09/14 14:09:31 roberto Exp roberto $
** $Id: lua.h,v 1.78 2000/10/30 12:38:50 roberto Exp roberto $
** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br
@@ -16,23 +16,36 @@
#include <stddef.h>
#define LUA_VERSION "Lua 4.0 (beta)"
/* mark for all API functions */
#ifndef LUA_API
#define LUA_API extern
#endif
#define LUA_VERSION "Lua 4.0"
#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
/* name of global variable with error handler */
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
/* pre-defined references */
#define LUA_NOREF (-2)
#define LUA_REFNIL (-1)
#define LUA_REFREGISTRY 0
/* pre-defined tags */
#define LUA_ANYTAG (-1)
#define LUA_NOTAG (-2)
/* option for multiple returns in lua_call */
#define LUA_MULTRET (-1)
/* minimum stack available for a C function */
#define LUA_MINSTACK 20
@@ -41,117 +54,136 @@
#define LUA_ERRFILE 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);
/*
** types returned by `lua_type'
*/
#define LUA_TNONE (-1)
#define LUA_TUSERDATA 0
#define LUA_TNIL 1
#define LUA_TNUMBER 2
#define LUA_TSTRING 3
#define LUA_TTABLE 4
#define LUA_TFUNCTION 5
/*
** state manipulation
*/
lua_State *lua_open (int stacksize);
void lua_close (lua_State *L);
LUA_API lua_State *lua_open (int stacksize);
LUA_API void lua_close (lua_State *L);
/*
** basic stack manipulation
*/
int lua_gettop (lua_State *L);
void lua_settop (lua_State *L, int index);
void lua_pushvalue (lua_State *L, int index);
void lua_remove (lua_State *L, int index);
void lua_insert (lua_State *L, int index);
int lua_stackspace (lua_State *L);
LUA_API int lua_gettop (lua_State *L);
LUA_API void lua_settop (lua_State *L, int index);
LUA_API void lua_pushvalue (lua_State *L, int index);
LUA_API void lua_remove (lua_State *L, int index);
LUA_API void lua_insert (lua_State *L, int index);
LUA_API int lua_stackspace (lua_State *L);
/*
** access functions (stack -> C)
*/
const char *lua_type (lua_State *L, int index);
int lua_isnumber (lua_State *L, int index);
int lua_iscfunction (lua_State *L, int index);
int lua_tag (lua_State *L, int index);
LUA_API int lua_type (lua_State *L, int index);
LUA_API const char *lua_typename (lua_State *L, int t);
LUA_API int lua_isnumber (lua_State *L, int index);
LUA_API int lua_isstring (lua_State *L, int index);
LUA_API int lua_iscfunction (lua_State *L, int index);
LUA_API int lua_tag (lua_State *L, int index);
int lua_equal (lua_State *L, int index1, int index2);
int lua_lessthan (lua_State *L, int index1, int index2);
LUA_API int lua_equal (lua_State *L, int index1, int index2);
LUA_API int lua_lessthan (lua_State *L, int index1, int index2);
double lua_tonumber (lua_State *L, int index);
const char *lua_tostring (lua_State *L, int index);
size_t lua_strlen (lua_State *L, int index);
lua_CFunction lua_tocfunction (lua_State *L, int index);
void *lua_touserdata (lua_State *L, int index);
const void *lua_topointer (lua_State *L, int index);
LUA_API double lua_tonumber (lua_State *L, int index);
LUA_API const char *lua_tostring (lua_State *L, int index);
LUA_API size_t lua_strlen (lua_State *L, int index);
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index);
LUA_API void *lua_touserdata (lua_State *L, int index);
LUA_API const void *lua_topointer (lua_State *L, int index);
/*
** push functions (C -> stack)
*/
void lua_pushnil (lua_State *L);
void lua_pushnumber (lua_State *L, double n);
void lua_pushlstring (lua_State *L, const char *s, size_t len);
void lua_pushstring (lua_State *L, const char *s);
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
void lua_pushusertag (lua_State *L, void *u, int tag);
LUA_API void lua_pushnil (lua_State *L);
LUA_API void lua_pushnumber (lua_State *L, double n);
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len);
LUA_API void lua_pushstring (lua_State *L, const char *s);
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
LUA_API void lua_pushusertag (lua_State *L, void *u, int tag);
/*
** get functions (Lua -> stack)
*/
void lua_getglobal (lua_State *L, const char *name);
void lua_gettable (lua_State *L, int index);
void lua_rawget (lua_State *L, int index);
void lua_rawgeti (lua_State *L, int index, int n);
void lua_getglobals (lua_State *L);
void lua_gettagmethod (lua_State *L, int tag, const char *event);
int lua_getref (lua_State *L, int ref);
void lua_newtable (lua_State *L);
LUA_API void lua_getglobal (lua_State *L, const char *name);
LUA_API void lua_gettable (lua_State *L, int index);
LUA_API void lua_rawget (lua_State *L, int index);
LUA_API void lua_rawgeti (lua_State *L, int index, int n);
LUA_API void lua_getglobals (lua_State *L);
LUA_API void lua_gettagmethod (lua_State *L, int tag, const char *event);
LUA_API int lua_getref (lua_State *L, int ref);
LUA_API void lua_newtable (lua_State *L);
/*
** set functions (stack -> Lua)
*/
void lua_setglobal (lua_State *L, const char *name);
void lua_settable (lua_State *L, int index);
void lua_rawset (lua_State *L, int index);
void lua_rawseti (lua_State *L, int index, int n);
void lua_setglobals (lua_State *L);
void lua_settagmethod (lua_State *L, int tag, const char *event);
int lua_ref (lua_State *L, int lock);
LUA_API void lua_setglobal (lua_State *L, const char *name);
LUA_API void lua_settable (lua_State *L, int index);
LUA_API void lua_rawset (lua_State *L, int index);
LUA_API void lua_rawseti (lua_State *L, int index, int n);
LUA_API void lua_setglobals (lua_State *L);
LUA_API void lua_settagmethod (lua_State *L, int tag, const char *event);
LUA_API int lua_ref (lua_State *L, int lock);
/*
** "do" functions (run Lua code)
*/
int lua_call (lua_State *L, int nargs, int nresults);
void lua_rawcall (lua_State *L, int nargs, int nresults);
int lua_dofile (lua_State *L, const char *filename);
int lua_dostring (lua_State *L, const char *str);
int lua_dobuffer (lua_State *L, const char *buff, size_t size,
const char *name);
LUA_API int lua_call (lua_State *L, int nargs, int nresults);
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
LUA_API int lua_dofile (lua_State *L, const char *filename);
LUA_API int lua_dostring (lua_State *L, const char *str);
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name);
/*
** Garbage-collection functions
*/
LUA_API int lua_getgcthreshold (lua_State *L);
LUA_API int lua_getgccount (lua_State *L);
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
/*
** miscellaneous functions
*/
int lua_newtag (lua_State *L);
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
void lua_settag (lua_State *L, int tag);
LUA_API int lua_newtag (lua_State *L);
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
LUA_API void lua_settag (lua_State *L, int tag);
void lua_error (lua_State *L, const char *s);
LUA_API void lua_error (lua_State *L, const char *s);
void lua_unref (lua_State *L, int ref);
LUA_API void lua_unref (lua_State *L, int ref);
long lua_collectgarbage (lua_State *L, long limit);
LUA_API int lua_next (lua_State *L, int index);
LUA_API int lua_getn (lua_State *L, int index);
int lua_next (lua_State *L, int index);
int lua_getn (lua_State *L, int index);
LUA_API void lua_concat (lua_State *L, int n);
void lua_concat (lua_State *L, int n);
LUA_API void *lua_newuserdata (lua_State *L, size_t size);
/*
@@ -167,12 +199,13 @@ void lua_concat (lua_State *L, int n);
#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
#define lua_isfunction(L,n) (*lua_type(L,n) == 'f')
#define lua_isstring(L,n) (lua_tostring(L,n) != 0)
#define lua_istable(L,n) (*lua_type(L,n) == 't')
#define lua_isuserdata(L,n) (*lua_type(L,n) == 'u')
#define lua_isnil(L,n) (lua_type(L,n)[2] == 'l')
#define lua_isnull(L,n) (*lua_type(L,n) == 'N')
#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
#define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA)
#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
#define lua_isnull(L,n) (lua_type(L,n) == LUA_TNONE)
#define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY)
#endif
@@ -212,3 +245,4 @@ void lua_concat (lua_State *L, int n);
*
* This implementation contains no third-party code.
******************************************************************************/

View File

@@ -1,5 +1,5 @@
/*
** $Id: luadebug.h,v 1.14 2000/09/11 20:29:27 roberto Exp roberto $
** $Id: luadebug.h,v 1.16 2000/10/20 16:39:03 roberto Exp roberto $
** Debugging API
** See Copyright Notice in lua.h
*/
@@ -17,13 +17,13 @@ typedef struct lua_Localvar lua_Localvar;
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
int lua_getstack (lua_State *L, int level, lua_Debug *ar);
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int localnum);
const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int localnum);
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_Hook lua_setcallhook (lua_State *L, lua_Hook func);
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
#define LUA_IDSIZE 60

View File

@@ -1,5 +1,5 @@
/*
** $Id: lualib.h,v 1.11 2000/09/05 19:33:32 roberto Exp roberto $
** $Id: lualib.h,v 1.13 2000/10/20 16:39:03 roberto Exp roberto $
** Lua standard libraries
** See Copyright Notice in lua.h
*/
@@ -11,13 +11,18 @@
#include "lua.h"
#ifndef LUALIB_API
#define LUALIB_API extern
#endif
#define LUA_ALERT "_ALERT"
void lua_baselibopen (lua_State *L);
void lua_iolibopen (lua_State *L);
void lua_strlibopen (lua_State *L);
void lua_mathlibopen (lua_State *L);
void lua_dblibopen (lua_State *L);
LUALIB_API void lua_baselibopen (lua_State *L);
LUALIB_API void lua_iolibopen (lua_State *L);
LUALIB_API void lua_strlibopen (lua_State *L);
LUALIB_API void lua_mathlibopen (lua_State *L);
LUALIB_API void lua_dblibopen (lua_State *L);

View File

@@ -1,5 +1,5 @@
/*
** $Id: lundump.c,v 1.32 2000/09/21 03:15:36 lhf Exp $
** $Id: lundump.c,v 1.33 2000/10/31 16:57:23 lhf Exp $
** load bytecodes from files
** See Copyright Notice in lua.h
*/
@@ -23,7 +23,7 @@ static const char* ZNAME (ZIO* Z)
static void unexpectedEOZ (lua_State* L, ZIO* Z)
{
luaO_verror(L,"unexpected end of file in `%.255s'",ZNAME(Z));
luaO_verror(L,"unexpected end of file in `%.99s'",ZNAME(Z));
}
static int ezgetc (lua_State* L, ZIO* Z)
@@ -107,7 +107,8 @@ static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
int size=LoadInt(L,Z,swap);
tf->code=luaM_newvector(L,size,Instruction);
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.255s'",ZNAME(Z));
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
luaF_protook(L,tf,size);
}
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
@@ -125,8 +126,8 @@ static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
{
int n=LoadInt(L,Z,swap);
if (n==0) return;
int n;
tf->nlineinfo=n=LoadInt(L,Z,swap);
tf->lineinfo=luaM_newvector(L,n,int);
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
}
@@ -157,10 +158,10 @@ static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
tf->numparams=LoadInt(L,Z,swap);
tf->is_vararg=LoadByte(L,Z);
tf->maxstacksize=LoadInt(L,Z,swap);
LoadCode(L,tf,Z,swap);
LoadLocals(L,tf,Z,swap);
LoadLines(L,tf,Z,swap);
LoadConstants(L,tf,Z,swap);
LoadCode(L,tf,Z,swap);
return tf;
}
@@ -169,15 +170,15 @@ static void LoadSignature (lua_State* L, ZIO* Z)
const char* s=SIGNATURE;
while (*s!=0 && ezgetc(L,Z)==*s)
++s;
if (*s!=0) luaO_verror(L,"bad signature in `%.255s'",ZNAME(Z));
if (*s!=0) luaO_verror(L,"bad signature in `%.99s'",ZNAME(Z));
}
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
{
int r=ezgetc(L,Z);
if (r!=s)
luaO_verror(L,"virtual machine mismatch in `%.255s':\n"
" %s is %d but read %d",ZNAME(Z),what,s,r);
luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
" %.20s is %d but read %d",ZNAME(Z),what,s,r);
}
#define TESTSIZE(s) TestSize(L,s,#s,Z)
@@ -190,11 +191,11 @@ static int LoadHeader (lua_State* L, ZIO* Z)
LoadSignature(L,Z);
version=ezgetc(L,Z);
if (version>VERSION)
luaO_verror(L,"`%.255s' too new:\n"
luaO_verror(L,"`%.99s' too new:\n"
" read version %d.%d; expected at most %d.%d",
ZNAME(Z),V(version),V(VERSION));
if (version<VERSION0) /* check last major change */
luaO_verror(L,"`%.255s' too old:\n"
luaO_verror(L,"`%.99s' too old:\n"
" read version %d.%d; expected at least %d.%d",
ZNAME(Z),V(version),V(VERSION));
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
@@ -207,9 +208,8 @@ static int LoadHeader (lua_State* L, ZIO* Z)
TESTSIZE(sizeof(Number));
f=LoadNumber(L,Z,swap);
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
luaO_verror(L,"unknown number format in `%.255s':\n"
" read " NUMBER_FMT "; expected " NUMBER_FMT,
ZNAME(Z),f,tf);
luaO_verror(L,"unknown number format in `%.99s':\n"
" read " NUMBER_FMT "; expected " NUMBER_FMT, ZNAME(Z),f,tf);
return swap;
}
@@ -230,7 +230,7 @@ Proto* luaU_undump (lua_State* L, ZIO* Z)
tf=LoadChunk(L,Z);
c=zgetc(Z);
if (c!=EOZ)
luaO_verror(L,"`%.255s' apparently contains more than one chunk",ZNAME(Z));
luaO_verror(L,"`%.99s' apparently contains more than one chunk",ZNAME(Z));
return tf;
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: lundump.h,v 1.19 2000/04/24 17:32:29 lhf Exp lhf $
** $Id: lundump.h,v 1.21 2000/10/31 16:57:23 lhf Exp $
** load pre-compiled Lua chunks
** See Copyright Notice in lua.h
*/
@@ -12,7 +12,6 @@
/* load one chunk */
Proto* luaU_undump (lua_State* L, ZIO* Z);
#define luaU_undump1 luaU_undump
/* find byte order */
int luaU_endianess (void);
@@ -24,15 +23,10 @@ int luaU_endianess (void);
#define SIGNATURE "Lua" /* ...followed by this signature */
/* formats for error messages */
#define xSOURCE "<%d:%.255s>"
#define SOURCE "<%.255s:%d>"
#define IN " in %p " SOURCE
#define INLOC tf,tf->source->str,tf->lineDefined
/* format for numbers in listings and error messages */
#ifndef NUMBER_FMT
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
#endif
#define SOURCE_FMT "<%d:%.99s>"
#define SOURCE tf->lineDefined,tf->source->str
#define IN_FMT " in %p " SOURCE_FMT
#define IN tf,SOURCE
/* a multiple of PI for testing native format */
/* multiplying by 1E8 gives non-trivial integer values */

200
lvm.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 1.135 2000/09/11 17:38:42 roberto Exp roberto $
** $Id: lvm.c,v 1.145 2000/10/06 12:45:25 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -39,26 +39,26 @@
int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
if (ttype(obj) != TAG_STRING)
int luaV_tonumber (TObject *obj) {
if (ttype(obj) != LUA_TSTRING)
return 1;
else {
if (!luaO_str2d(svalue(obj), &nvalue(obj)))
return 2;
ttype(obj) = TAG_NUMBER;
ttype(obj) = LUA_TNUMBER;
return 0;
}
}
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
if (ttype(obj) != TAG_NUMBER)
if (ttype(obj) != LUA_TNUMBER)
return 1;
else {
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
sprintf(s, "%.16g", (double)nvalue(obj));
lua_number2str(s, nvalue(obj)); /* convert `s' to number */
tsvalue(obj) = luaS_new(L, s);
ttype(obj) = TAG_STRING;
ttype(obj) = LUA_TSTRING;
return 0;
}
}
@@ -67,9 +67,9 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) {
CallInfo *ci = infovalue(base-1);
int *lineinfo = ci->func->f.l->lineinfo;
int pc = (*ci->pc - 1) - ci->func->f.l->code;
int pc = (*ci->pc - ci->func->f.l->code) - 1;
int newline;
if (ci->line == 0) { /* first time? */
if (pc == 0) { /* may be first time? */
ci->line = 1;
ci->refi = 0;
ci->lastpc = pc+1; /* make sure it will call linehook */
@@ -85,27 +85,29 @@ static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) {
}
static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) {
static Closure *luaV_closure (lua_State *L, int nelems) {
Closure *c = luaF_newclosure(L, nelems);
L->top -= nelems;
while (nelems--)
c->upvalue[nelems] = *(L->top+nelems);
ttype(L->top) = t;
clvalue(L->top) = c;
ttype(L->top) = LUA_TFUNCTION;
incr_top;
return c;
}
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems);
Closure *cl = luaV_closure(L, nelems);
cl->f.c = c;
cl->isC = 1;
}
void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems);
Closure *cl = luaV_closure(L, nelems);
cl->f.l = l;
cl->isC = 0;
}
@@ -114,27 +116,27 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
** Receives the table at `t' and the key at top.
*/
const TObject *luaV_gettable (lua_State *L, StkId t) {
const TObject *im;
Closure *tm;
int tg;
if (ttype(t) == TAG_TABLE && /* `t' is a table? */
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
ttype(luaT_getim(L, tg, IM_GETTABLE)) == TAG_NIL)) { /* or no TM? */
if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
luaT_gettm(L, tg, TM_GETTABLE) == NULL)) { /* or no TM? */
/* do a primitive get */
const TObject *h = luaH_get(L, hvalue(t), t+1);
const TObject *h = luaH_get(L, hvalue(t), L->top-1);
/* result is no nil or there is no `index' tag method? */
if (ttype(h) != TAG_NIL ||
(ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL))
if (ttype(h) != LUA_TNIL || ((tm=luaT_gettm(L, tg, TM_INDEX)) == NULL))
return h; /* return result */
/* else call `index' tag method */
}
else { /* try a 'gettable' TM */
im = luaT_getimbyObj(L, t, IM_GETTABLE);
else { /* try a `gettable' tag method */
tm = luaT_gettmbyObj(L, t, TM_GETTABLE);
}
if (ttype(im) != TAG_NIL) { /* is there a tag method? */
if (tm != NULL) { /* is there a tag method? */
luaD_checkstack(L, 2);
*(L->top+1) = *(L->top-1); /* key */
*L->top = *t; /* table */
*(L->top-1) = *im; /* tag method */
clvalue(L->top-1) = tm; /* tag method */
ttype(L->top-1) = LUA_TFUNCTION;
L->top += 2;
luaD_call(L, L->top - 3, 1);
return L->top - 1; /* call result */
@@ -151,18 +153,19 @@ const TObject *luaV_gettable (lua_State *L, StkId t) {
*/
void luaV_settable (lua_State *L, StkId t, StkId key) {
int tg;
if (ttype(t) == TAG_TABLE && /* `t' is a table? */
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */
if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
luaT_gettm(L, tg, TM_SETTABLE) == NULL)) /* or no TM? */
*luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */
else { /* try a `settable' tag method */
const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE);
if (ttype(im) != TAG_NIL) {
Closure *tm = luaT_gettmbyObj(L, t, TM_SETTABLE);
if (tm != NULL) {
luaD_checkstack(L, 3);
*(L->top+2) = *(L->top-1);
*(L->top+1) = *key;
*(L->top) = *t;
*(L->top-1) = *im;
clvalue(L->top-1) = tm;
ttype(L->top-1) = LUA_TFUNCTION;
L->top += 3;
luaD_call(L, L->top - 4, 0); /* call `settable' tag method */
}
@@ -174,14 +177,15 @@ void luaV_settable (lua_State *L, StkId t, StkId key) {
const TObject *luaV_getglobal (lua_State *L, TString *s) {
const TObject *value = luaH_getstr(L->gt, s);
TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
if (ttype(im) == TAG_NIL) /* is there a tag method? */
Closure *tm = luaT_gettmbyObj(L, value, TM_GETGLOBAL);
if (tm == NULL) /* is there a tag method? */
return value; /* default behavior */
else { /* tag method */
luaD_checkstack(L, 3);
*L->top = *im;
ttype(L->top+1) = TAG_STRING;
clvalue(L->top) = tm;
ttype(L->top) = LUA_TFUNCTION;
tsvalue(L->top+1) = s; /* global name */
ttype(L->top+1) = LUA_TSTRING;
*(L->top+2) = *value;
L->top += 3;
luaD_call(L, L->top - 3, 1);
@@ -192,15 +196,15 @@ const TObject *luaV_getglobal (lua_State *L, TString *s) {
void luaV_setglobal (lua_State *L, TString *s) {
const TObject *oldvalue = luaH_getstr(L->gt, s);
const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
if (ttype(im) == TAG_NIL) { /* is there a tag method? */
Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL);
if (tm == NULL) { /* is there a tag method? */
if (oldvalue != &luaO_nilobject) {
/* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
*(TObject *)oldvalue = *(L->top - 1);
}
else {
TObject key;
ttype(&key) = TAG_STRING;
ttype(&key) = LUA_TSTRING;
tsvalue(&key) = s;
*luaH_set(L, L->gt, &key) = *(L->top - 1);
}
@@ -209,44 +213,45 @@ void luaV_setglobal (lua_State *L, TString *s) {
luaD_checkstack(L, 3);
*(L->top+2) = *(L->top-1); /* new value */
*(L->top+1) = *oldvalue;
ttype(L->top) = TAG_STRING;
ttype(L->top) = LUA_TSTRING;
tsvalue(L->top) = s;
*(L->top-1) = *im;
clvalue(L->top-1) = tm;
ttype(L->top-1) = LUA_TFUNCTION;
L->top += 3;
luaD_call(L, L->top - 4, 0);
}
}
static int call_binTM (lua_State *L, StkId top, IMS event) {
static int call_binTM (lua_State *L, StkId top, TMS event) {
/* try first operand */
const TObject *im = luaT_getimbyObj(L, top-2, event);
Closure *tm = luaT_gettmbyObj(L, top-2, event);
L->top = top;
if (ttype(im) == TAG_NIL) {
im = luaT_getimbyObj(L, top-1, event); /* try second operand */
if (ttype(im) == TAG_NIL) {
im = luaT_getim(L, 0, event); /* try a `global' method */
if (ttype(im) == TAG_NIL)
if (tm == NULL) {
tm = luaT_gettmbyObj(L, top-1, event); /* try second operand */
if (tm == NULL) {
tm = luaT_gettm(L, 0, event); /* try a `global' method */
if (tm == NULL)
return 0; /* error */
}
}
lua_pushstring(L, luaT_eventname[event]);
luaD_callTM(L, im, 3, 1);
luaD_callTM(L, tm, 3, 1);
return 1;
}
static void call_arith (lua_State *L, StkId top, IMS event) {
static void call_arith (lua_State *L, StkId top, TMS event) {
if (!call_binTM(L, top, event))
luaG_binerror(L, top-2, TAG_NUMBER, "perform arithmetic on");
luaG_binerror(L, top-2, LUA_TNUMBER, "perform arithmetic on");
}
static int luaV_strcomp (const TString *ls, const TString *rs) {
const char *l = ls->str;
size_t ll = ls->u.s.len;
size_t ll = ls->len;
const char *r = rs->str;
size_t lr = rs->u.s.len;
size_t lr = rs->len;
for (;;) {
int temp = strcoll(l, r);
if (temp != 0) return temp;
@@ -265,18 +270,18 @@ static int luaV_strcomp (const TString *ls, const TString *rs) {
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
if (ttype(l) == TAG_NUMBER && ttype(r) == TAG_NUMBER)
if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
return (nvalue(l) < nvalue(r));
else if (ttype(l) == TAG_STRING && ttype(r) == TAG_STRING)
else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
else { /* call TM */
luaD_checkstack(L, 2);
*top++ = *l;
*top++ = *r;
if (!call_binTM(L, top, IM_LT))
if (!call_binTM(L, top, TM_LT))
luaG_ordererror(L, top-2);
L->top--;
return (ttype(L->top) != TAG_NIL);
return (ttype(L->top) != LUA_TNIL);
}
}
@@ -285,24 +290,24 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
do {
int n = 2; /* number of elements handled in this pass (at least 2) */
if (tostring(L, top-2) || tostring(L, top-1)) {
if (!call_binTM(L, top, IM_CONCAT))
luaG_binerror(L, top-2, TAG_STRING, "concat");
if (!call_binTM(L, top, TM_CONCAT))
luaG_binerror(L, top-2, LUA_TSTRING, "concat");
}
else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */
/* at least two string values; get as many as possible */
lint32 tl = (lint32)tsvalue(top-1)->u.s.len +
(lint32)tsvalue(top-2)->u.s.len;
lint32 tl = (lint32)tsvalue(top-1)->len +
(lint32)tsvalue(top-2)->len;
char *buffer;
int i;
while (n < total && !tostring(L, top-n-1)) { /* collect total length */
tl += tsvalue(top-n-1)->u.s.len;
tl += tsvalue(top-n-1)->len;
n++;
}
if (tl > MAX_SIZET) lua_error(L, "string size overflow");
buffer = luaO_openspace(L, tl);
tl = 0;
for (i=n; i>0; i--) { /* concat all strings */
size_t l = tsvalue(top-i)->u.s.len;
size_t l = tsvalue(top-i)->len;
memcpy(buffer+tl, tsvalue(top-i)->str, l);
tl += l;
}
@@ -322,7 +327,7 @@ static void luaV_pack (lua_State *L, StkId firstelem) {
/* store counter in field `n' */
luaH_setstrnum(L, htab, luaS_new(L, "n"), i);
L->top = firstelem; /* remove elements from the stack */
ttype(L->top) = TAG_TABLE;
ttype(L->top) = LUA_TTABLE;
hvalue(L->top) = htab;
incr_top;
}
@@ -344,17 +349,15 @@ static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
** Returns n such that the the results are between [n,top).
*/
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
const Proto *tf = cl->f.l;
const Proto *const tf = cl->f.l;
StkId top; /* keep top local, for performance */
const Instruction *pc = tf->code;
TString **kstr = tf->kstr;
lua_Hook linehook = L->linehook;
TString **const kstr = tf->kstr;
const lua_Hook linehook = L->linehook;
infovalue(base-1)->pc = &pc;
luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
if (tf->is_vararg) { /* varargs? */
if (tf->is_vararg) /* varargs? */
adjust_varargs(L, base, tf->numparams);
luaC_checkGC(L);
}
else
luaD_adjusttop(L, base, tf->numparams);
top = L->top;
@@ -365,7 +368,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
traceexec(L, base, top, linehook);
switch (GET_OPCODE(i)) {
case OP_END: {
return L->top; /* no results */
L->top = top;
return top;
}
case OP_RETURN: {
L->top = top;
@@ -388,7 +392,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
int n = GETARG_U(i);
LUA_ASSERT(n>0, "invalid argument");
do {
ttype(top++) = TAG_NIL;
ttype(top++) = LUA_TNIL;
} while (--n > 0);
break;
}
@@ -397,25 +401,25 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break;
}
case OP_PUSHINT: {
ttype(top) = TAG_NUMBER;
ttype(top) = LUA_TNUMBER;
nvalue(top) = (Number)GETARG_S(i);
top++;
break;
}
case OP_PUSHSTRING: {
ttype(top) = TAG_STRING;
ttype(top) = LUA_TSTRING;
tsvalue(top) = kstr[GETARG_U(i)];
top++;
break;
}
case OP_PUSHNUM: {
ttype(top) = TAG_NUMBER;
ttype(top) = LUA_TNUMBER;
nvalue(top) = tf->knum[GETARG_U(i)];
top++;
break;
}
case OP_PUSHNEGNUM: {
ttype(top) = TAG_NUMBER;
ttype(top) = LUA_TNUMBER;
nvalue(top) = -tf->knum[GETARG_U(i)];
top++;
break;
@@ -441,7 +445,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break;
}
case OP_GETDOTTED: {
ttype(top) = TAG_STRING;
ttype(top) = LUA_TSTRING;
tsvalue(top) = kstr[GETARG_U(i)];
L->top = top+1;
*(top-1) = *luaV_gettable(L, top-1);
@@ -456,7 +460,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
case OP_PUSHSELF: {
TObject receiver;
receiver = *(top-1);
ttype(top) = TAG_STRING;
ttype(top) = LUA_TSTRING;
tsvalue(top++) = kstr[GETARG_U(i)];
L->top = top;
*(top-2) = *luaV_gettable(L, top-2);
@@ -467,7 +471,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
L->top = top;
luaC_checkGC(L);
hvalue(top) = luaH_new(L, GETARG_U(i));
ttype(top) = TAG_TABLE;
ttype(top) = LUA_TTABLE;
top++;
break;
}
@@ -510,7 +514,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_ADD: {
if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_ADD);
call_arith(L, top, TM_ADD);
else
nvalue(top-2) += nvalue(top-1);
top--;
@@ -518,9 +522,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_ADDI: {
if (tonumber(top-1)) {
ttype(top) = TAG_NUMBER;
ttype(top) = LUA_TNUMBER;
nvalue(top) = (Number)GETARG_S(i);
call_arith(L, top+1, IM_ADD);
call_arith(L, top+1, TM_ADD);
}
else
nvalue(top-1) += (Number)GETARG_S(i);
@@ -528,7 +532,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_SUB: {
if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_SUB);
call_arith(L, top, TM_SUB);
else
nvalue(top-2) -= nvalue(top-1);
top--;
@@ -536,7 +540,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_MULT: {
if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_MUL);
call_arith(L, top, TM_MUL);
else
nvalue(top-2) *= nvalue(top-1);
top--;
@@ -544,14 +548,14 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_DIV: {
if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_DIV);
call_arith(L, top, TM_DIV);
else
nvalue(top-2) /= nvalue(top-1);
top--;
break;
}
case OP_POW: {
if (!call_binTM(L, top, IM_POW))
if (!call_binTM(L, top, TM_POW))
lua_error(L, "undefined operation");
top--;
break;
@@ -566,8 +570,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_MINUS: {
if (tonumber(top-1)) {
ttype(top) = TAG_NIL;
call_arith(L, top+1, IM_UNM);
ttype(top) = LUA_TNIL;
call_arith(L, top+1, TM_UNM);
}
else
nvalue(top-1) = -nvalue(top-1);
@@ -575,7 +579,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_NOT: {
ttype(top-1) =
(ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
(ttype(top-1) == LUA_TNIL) ? LUA_TNUMBER : LUA_TNIL;
nvalue(top-1) = 1;
break;
}
@@ -610,20 +614,20 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break;
}
case OP_JMPT: {
if (ttype(--top) != TAG_NIL) dojump(pc, i);
if (ttype(--top) != LUA_TNIL) dojump(pc, i);
break;
}
case OP_JMPF: {
if (ttype(--top) == TAG_NIL) dojump(pc, i);
if (ttype(--top) == LUA_TNIL) dojump(pc, i);
break;
}
case OP_JMPONT: {
if (ttype(top-1) == TAG_NIL) top--;
if (ttype(top-1) == LUA_TNIL) top--;
else dojump(pc, i);
break;
}
case OP_JMPONF: {
if (ttype(top-1) != TAG_NIL) top--;
if (ttype(top-1) != LUA_TNIL) top--;
else dojump(pc, i);
break;
}
@@ -632,7 +636,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break;
}
case OP_PUSHNILJMP: {
ttype(top++) = TAG_NIL;
ttype(top++) = LUA_TNIL;
pc++;
break;
}
@@ -652,9 +656,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break;
}
case OP_FORLOOP: {
LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step");
LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit");
if (ttype(top-3) != TAG_NUMBER)
LUA_ASSERT(ttype(top-1) == LUA_TNUMBER, "invalid step");
LUA_ASSERT(ttype(top-2) == LUA_TNUMBER, "invalid limit");
if (ttype(top-3) != LUA_TNUMBER)
lua_error(L, "`for' index must be a number");
nvalue(top-3) += nvalue(top-1); /* increment index */
if (nvalue(top-1) > 0 ?
@@ -667,7 +671,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_LFORPREP: {
Node *node;
if (ttype(top-1) != TAG_TABLE)
if (ttype(top-1) != LUA_TTABLE)
lua_error(L, "`for' table must be a table");
node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
if (node == NULL) { /* `empty' loop? */
@@ -683,7 +687,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
}
case OP_LFORLOOP: {
Node *node;
LUA_ASSERT(ttype(top-3) == TAG_TABLE, "invalid table");
LUA_ASSERT(ttype(top-3) == LUA_TTABLE, "invalid table");
node = luaH_next(L, hvalue(top-3), top-2);
if (node == NULL) /* end loop? */
top -= 3; /* remove table, key, and value */

6
lvm.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: lvm.h,v 1.25 2000/08/31 21:02:55 roberto Exp roberto $
** $Id: lvm.h,v 1.26 2000/09/05 19:33:32 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -13,8 +13,8 @@
#include "ltm.h"
#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0))
#define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0))
#define tonumber(o) ((ttype(o) != LUA_TNUMBER) && (luaV_tonumber(o) != 0))
#define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0))
int luaV_tonumber (TObject *obj);

5
lzio.h
View File

@@ -1,5 +1,5 @@
/*
** $Id: lzio.h,v 1.5 1999/08/16 20:52:00 roberto Exp roberto $
** $Id: lzio.h,v 1.6 2000/05/24 13:54:49 roberto Exp roberto $
** Buffered streams
** See Copyright Notice in lua.h
*/
@@ -33,9 +33,12 @@ size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
#define zname(z) ((z)->name)
/* --------- Private Part ------------------ */
#ifndef ZBSIZE
#define ZBSIZE 256 /* buffer size */
#endif
struct zio {
size_t n; /* bytes still unread */

View File

@@ -1,5 +1,5 @@
#
## $Id: makefile,v 1.27 2000/09/11 20:29:27 roberto Exp roberto $
## $Id: makefile,v 1.29 2000/10/30 17:50:00 roberto Exp roberto $
## Makefile
## See Copyright Notice in lua.h
#
@@ -23,11 +23,11 @@
# (only for compatibility with previous versions)
# define LUA_COMPAT_ARGRET for compatibility in the way function results
# are passed as arguments
# define LUA_DEPRECATETFUNCS to include obsolete functions
# define LUA_DEPRECATEDFUNCS to include obsolete functions
CONFIG = -DPOPEN -D_POSIX_SOURCE
#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
# -DLUA_DEPRECATETFUNCS
#CONFIG = -DOLD_ANSI -DLUA_DEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
# -DLUA_DEPRECATEDFUNCS
# Compilation parameters

1076
manual.tex

File diff suppressed because it is too large Load Diff