mirror of
https://github.com/lua/lua.git
synced 2026-08-04 04:19:06 +00:00
Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
523c5d8e1c | ||
|
|
40a4c76773 | ||
|
|
1385d81d20 | ||
|
|
906d5dcc41 | ||
|
|
93a5649d40 | ||
|
|
df416661cc | ||
|
|
67c1afff59 | ||
|
|
03770ecfc9 | ||
|
|
7409678b5d | ||
|
|
d1c689af40 | ||
|
|
37e9c2e744 | ||
|
|
e42a219eeb | ||
|
|
b3959d58ff | ||
|
|
f379d06e24 | ||
|
|
728ff20701 | ||
|
|
2cbbf3933a | ||
|
|
e2b6b7de1b | ||
|
|
563b1f5704 | ||
|
|
4670476584 | ||
|
|
89f98c0995 | ||
|
|
b892f0a877 | ||
|
|
aadc35449e | ||
|
|
cdc8139e29 | ||
|
|
e833bd47c9 | ||
|
|
b7ffb128cb | ||
|
|
64eecc0b82 | ||
|
|
8b88ab07f7 | ||
|
|
2779ceeb12 | ||
|
|
e93c4547fe | ||
|
|
0ffc676ce7 | ||
|
|
18fb3ddb89 | ||
|
|
2bddbe6603 | ||
|
|
e323338fd0 | ||
|
|
46b543ebef | ||
|
|
79909a92e1 | ||
|
|
d6232a0b2e | ||
|
|
ae63a0e692 | ||
|
|
cd3d446957 | ||
|
|
f01e6c6f1d | ||
|
|
ad3816d0d1 | ||
|
|
046a3d6173 | ||
|
|
001f2bdd0e | ||
|
|
cd2ddaded9 | ||
|
|
d68209e822 | ||
|
|
1088cde03c | ||
|
|
6759f3ec5e | ||
|
|
f6834f4393 | ||
|
|
78bc8e553d | ||
|
|
dad808a73a | ||
|
|
ca7fd50a4e | ||
|
|
282ab366f4 | ||
|
|
444d6a106b | ||
|
|
13635f7de7 | ||
|
|
d8a442206d | ||
|
|
c9c6f9747c | ||
|
|
c2aa7bd72d | ||
|
|
f9dd50cefc |
37
bugs
37
bugs
@@ -198,3 +198,40 @@ Tue Aug 29 15:57:41 EST 2000
|
|||||||
>> gc tag method for nil could call line hook
|
>> gc tag method for nil could call line hook
|
||||||
(by ry; since ?)
|
(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
274
lapi.c
@@ -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
|
** Lua API
|
||||||
** See Copyright Notice in lua.h
|
** 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) {
|
void luaA_pushobject (lua_State *L, const TObject *o) {
|
||||||
*L->top = *o;
|
*L->top = *o;
|
||||||
incr_top;
|
incr_top;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lua_stackspace (lua_State *L) {
|
LUA_API int lua_stackspace (lua_State *L) {
|
||||||
return (L->stack_last - L->top);
|
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);
|
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)
|
if (index >= 0)
|
||||||
luaD_adjusttop(L, L->Cbase, index);
|
luaD_adjusttop(L, L->Cbase, index);
|
||||||
else
|
else
|
||||||
@@ -68,15 +78,15 @@ void lua_settop (lua_State *L, int index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_remove (lua_State *L, int index) {
|
LUA_API void lua_remove (lua_State *L, int index) {
|
||||||
StkId p = Index(L, index);
|
StkId p = luaA_index(L, index);
|
||||||
while (++p < L->top) *(p-1) = *p;
|
while (++p < L->top) *(p-1) = *p;
|
||||||
L->top--;
|
L->top--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_insert (lua_State *L, int index) {
|
LUA_API void lua_insert (lua_State *L, int index) {
|
||||||
StkId p = Index(L, index);
|
StkId p = luaA_index(L, index);
|
||||||
StkId q;
|
StkId q;
|
||||||
for (q = L->top; q>p; q--)
|
for (q = L->top; q>p; q--)
|
||||||
*q = *(q-1);
|
*q = *(q-1);
|
||||||
@@ -84,8 +94,8 @@ void lua_insert (lua_State *L, int index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_pushvalue (lua_State *L, int index) {
|
LUA_API void lua_pushvalue (lua_State *L, int index) {
|
||||||
*L->top = *Index(L, index);
|
*L->top = *luaA_index(L, index);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,87 +106,87 @@ void lua_pushvalue (lua_State *L, int index) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define btest(L,i,value,default) { \
|
LUA_API int lua_type (lua_State *L, int index) {
|
||||||
StkId o; \
|
StkId o = luaA_indexAcceptable(L, index);
|
||||||
if ((i) >= 0) { \
|
return (o == NULL) ? LUA_TNONE : ttype(o);
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int lua_iscfunction (lua_State *L, int index) {
|
LUA_API const char *lua_typename (lua_State *L, int t) {
|
||||||
btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
|
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) {
|
LUA_API int lua_isnumber (lua_State *L, int index) {
|
||||||
btest(L, index,
|
TObject *o = luaA_indexAcceptable(L, index);
|
||||||
((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
|
return (o == NULL) ? 0 : (tonumber(o) == 0);
|
||||||
luaT_effectivetag(L, o)), -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int lua_equal (lua_State *L, int index1, int index2) {
|
LUA_API int lua_isstring (lua_State *L, int index) {
|
||||||
StkId o1 = Index(L, index1);
|
int t = lua_type(L, index);
|
||||||
StkId o2 = Index(L, index2);
|
return (t == LUA_TSTRING || t == LUA_TNUMBER);
|
||||||
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
else return luaO_equalObj(o1, o2);
|
||||||
}
|
}
|
||||||
|
|
||||||
int lua_lessthan (lua_State *L, int index1, int index2) {
|
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
|
||||||
StkId o1 = Index(L, index1);
|
StkId o1 = luaA_indexAcceptable(L, index1);
|
||||||
StkId o2 = Index(L, index2);
|
StkId o2 = luaA_indexAcceptable(L, index2);
|
||||||
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
|
if (o1 == NULL || o2 == NULL) return 0; /* index out-of-range */
|
||||||
else return luaV_lessthan(L, o1, o2, L->top);
|
else return luaV_lessthan(L, o1, o2, L->top);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double lua_tonumber (lua_State *L, int index) {
|
LUA_API double lua_tonumber (lua_State *L, int index) {
|
||||||
access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
|
StkId o = luaA_indexAcceptable(L, index);
|
||||||
|
return (o == NULL || tonumber(o)) ? 0 : nvalue(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *lua_tostring (lua_State *L, int index) {
|
LUA_API const char *lua_tostring (lua_State *L, int index) {
|
||||||
luaC_checkGC(L); /* `tostring' may create a new string */
|
StkId o = luaA_indexAcceptable(L, index);
|
||||||
access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
|
return (o == NULL || tostring(L, o)) ? NULL : svalue(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t lua_strlen (lua_State *L, int index) {
|
LUA_API size_t lua_strlen (lua_State *L, int index) {
|
||||||
access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
|
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) {
|
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
|
||||||
access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
|
StkId o = luaA_indexAcceptable(L, index);
|
||||||
|
return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *lua_touserdata (lua_State *L, int index) {
|
LUA_API void *lua_touserdata (lua_State *L, int index) {
|
||||||
access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
|
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) {
|
LUA_API const void *lua_topointer (lua_State *L, int index) {
|
||||||
StkId o = Index(L, index);
|
StkId o = luaA_indexAcceptable(L, index);
|
||||||
|
if (o == NULL) return NULL;
|
||||||
switch (ttype(o)) {
|
switch (ttype(o)) {
|
||||||
case TAG_TABLE:
|
case LUA_TTABLE:
|
||||||
return hvalue(o);
|
return hvalue(o);
|
||||||
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
case LUA_TFUNCTION:
|
||||||
return clvalue(o);
|
return clvalue(o);
|
||||||
default: return NULL;
|
default: return NULL;
|
||||||
}
|
}
|
||||||
@@ -189,28 +199,27 @@ const void *lua_topointer (lua_State *L, int index) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
void lua_pushnil (lua_State *L) {
|
LUA_API void lua_pushnil (lua_State *L) {
|
||||||
ttype(L->top) = TAG_NIL;
|
ttype(L->top) = LUA_TNIL;
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_pushnumber (lua_State *L, double n) {
|
LUA_API void lua_pushnumber (lua_State *L, double n) {
|
||||||
ttype(L->top) = TAG_NUMBER;
|
|
||||||
nvalue(L->top) = n;
|
nvalue(L->top) = n;
|
||||||
|
ttype(L->top) = LUA_TNUMBER;
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
||||||
luaC_checkGC(L);
|
|
||||||
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
||||||
ttype(L->top) = TAG_STRING;
|
ttype(L->top) = LUA_TSTRING;
|
||||||
api_incr_top(L);
|
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)
|
if (s == NULL)
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
else
|
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) {
|
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||||
luaC_checkGC(L);
|
|
||||||
luaV_Cclosure(L, fn, n);
|
luaV_Cclosure(L, fn, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
|
LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
|
||||||
luaC_checkGC(L);
|
/* ORDER LUA_T */
|
||||||
if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
|
if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag)))
|
||||||
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
|
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
|
||||||
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
||||||
ttype(L->top) = TAG_USERDATA;
|
ttype(L->top) = LUA_TUSERDATA;
|
||||||
api_incr_top(L);
|
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;
|
StkId top = L->top;
|
||||||
*top = *luaV_getglobal(L, luaS_new(L, name));
|
*top = *luaV_getglobal(L, luaS_new(L, name));
|
||||||
L->top = top;
|
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 t = Index(L, index);
|
||||||
StkId top = L->top;
|
StkId top = L->top;
|
||||||
*(top-1) = *luaV_gettable(L, t);
|
*(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);
|
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);
|
*(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);
|
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);
|
*L->top = *luaH_getnum(hvalue(o), n);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_getglobals (lua_State *L) {
|
LUA_API void lua_getglobals (lua_State *L) {
|
||||||
hvalue(L->top) = L->gt;
|
hvalue(L->top) = L->gt;
|
||||||
ttype(L->top) = TAG_TABLE;
|
ttype(L->top) = LUA_TTABLE;
|
||||||
api_incr_top(L);
|
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)
|
if (ref == LUA_REFNIL)
|
||||||
ttype(L->top) = TAG_NIL;
|
ttype(L->top) = LUA_TNIL;
|
||||||
else if (0 <= ref && ref < L->refSize &&
|
else if (0 <= ref && ref < L->refSize &&
|
||||||
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
||||||
*L->top = L->refArray[ref].o;
|
*L->top = L->refArray[ref].o;
|
||||||
@@ -291,10 +299,9 @@ int lua_getref (lua_State *L, int ref) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_newtable (lua_State *L) {
|
LUA_API void lua_newtable (lua_State *L) {
|
||||||
luaC_checkGC(L);
|
|
||||||
hvalue(L->top) = luaH_new(L, 0);
|
hvalue(L->top) = luaH_new(L, 0);
|
||||||
ttype(L->top) = TAG_TABLE;
|
ttype(L->top) = LUA_TTABLE;
|
||||||
api_incr_top(L);
|
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;
|
StkId top = L->top;
|
||||||
luaV_setglobal(L, luaS_new(L, name));
|
luaV_setglobal(L, luaS_new(L, name));
|
||||||
L->top = top-1; /* remove element from the top */
|
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 t = Index(L, index);
|
||||||
StkId top = L->top;
|
StkId top = L->top;
|
||||||
luaV_settable(L, t, top-2);
|
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);
|
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);
|
*luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
|
||||||
L->top -= 2;
|
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);
|
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);
|
*luaH_setint(L, hvalue(o), n) = *(L->top-1);
|
||||||
L->top--;
|
L->top--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_setglobals (lua_State *L) {
|
LUA_API void lua_setglobals (lua_State *L) {
|
||||||
StkId newtable = --L->top;
|
StkId newtable = --L->top;
|
||||||
LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
|
LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected");
|
||||||
L->gt = hvalue(newtable);
|
L->gt = hvalue(newtable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int lua_ref (lua_State *L, int lock) {
|
LUA_API int lua_ref (lua_State *L, int lock) {
|
||||||
int ref;
|
int ref;
|
||||||
if (ttype(L->top-1) == TAG_NIL)
|
if (ttype(L->top-1) == LUA_TNIL)
|
||||||
ref = LUA_REFNIL;
|
ref = LUA_REFNIL;
|
||||||
else {
|
else {
|
||||||
if (L->refFree != NONEXT) { /* is there a free place? */
|
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 */
|
else { /* no more free places */
|
||||||
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
|
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
|
||||||
"reference table overflow", MAX_INT);
|
"reference table overflow", MAX_INT);
|
||||||
|
L->nblocks += sizeof(struct Ref);
|
||||||
ref = L->refSize++;
|
ref = L->refSize++;
|
||||||
}
|
}
|
||||||
L->refArray[ref].o = *(L->top-1);
|
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)
|
** (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);
|
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
|
** miscellaneous functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void lua_settag (lua_State *L, int tag) {
|
LUA_API void lua_settag (lua_State *L, int tag) {
|
||||||
luaT_realtag(L, tag);
|
luaT_realtag(L, tag);
|
||||||
switch (ttype(L->top-1)) {
|
switch (ttype(L->top-1)) {
|
||||||
case TAG_TABLE:
|
case LUA_TTABLE:
|
||||||
hvalue(L->top-1)->htag = tag;
|
hvalue(L->top-1)->htag = tag;
|
||||||
break;
|
break;
|
||||||
case TAG_USERDATA:
|
case LUA_TUSERDATA:
|
||||||
tsvalue(L->top-1)->u.d.tag = tag;
|
tsvalue(L->top-1)->u.d.tag = tag;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
luaO_verror(L, "cannot change the tag of a %.20s",
|
luaO_verror(L, "cannot change the tag of a %.20s",
|
||||||
luaO_typename(L->top-1));
|
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) {
|
if (ref >= 0) {
|
||||||
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
|
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
|
||||||
L->refArray[ref].st = L->refFree;
|
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) {
|
LUA_API int lua_next (lua_State *L, int index) {
|
||||||
StkId t = Index(L, index);
|
StkId t = luaA_index(L, index);
|
||||||
Node *n;
|
Node *n;
|
||||||
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
|
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
|
||||||
n = luaH_next(L, hvalue(t), Index(L, -1));
|
n = luaH_next(L, hvalue(t), luaA_index(L, -1));
|
||||||
if (n) {
|
if (n) {
|
||||||
*(L->top-1) = *key(n);
|
*(L->top-1) = *key(n);
|
||||||
*L->top = *val(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) {
|
LUA_API int lua_getn (lua_State *L, int index) {
|
||||||
Hash *h = hvalue(Index(L, index));
|
Hash *h = hvalue(luaA_index(L, index));
|
||||||
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
|
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);
|
return (int)nvalue(value);
|
||||||
else {
|
else {
|
||||||
Number max = 0;
|
Number max = 0;
|
||||||
int i = h->size;
|
int i = h->size;
|
||||||
Node *n = h->node;
|
Node *n = h->node;
|
||||||
while (i--) {
|
while (i--) {
|
||||||
if (ttype(key(n)) == TAG_NUMBER &&
|
if (ttype(key(n)) == LUA_TNUMBER &&
|
||||||
ttype(val(n)) != TAG_NIL &&
|
ttype(val(n)) != LUA_TNIL &&
|
||||||
nvalue(key(n)) > max)
|
nvalue(key(n)) > max)
|
||||||
max = nvalue(key(n));
|
max = nvalue(key(n));
|
||||||
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;
|
StkId top = L->top;
|
||||||
luaV_strconc(L, n, top);
|
luaV_strconc(L, n, top);
|
||||||
L->top = top-(n-1);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
85
lauxlib.c
85
lauxlib.c
@@ -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
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** 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;
|
int i;
|
||||||
for (i=0; list[i]; i++)
|
for (i=0; list[i]; i++)
|
||||||
if (strcmp(list[i], name) == 0)
|
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 */
|
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_Debug ar;
|
||||||
lua_getstack(L, 0, &ar);
|
lua_getstack(L, 0, &ar);
|
||||||
lua_getinfo(L, "n", &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) {
|
static void type_error (lua_State *L, int narg, int t) {
|
||||||
char buff[100];
|
char buff[50];
|
||||||
const char *rt = lua_type(L, narg);
|
sprintf(buff, "%.8s expected, got %.8s", lua_typename(L, t),
|
||||||
if (*rt == 'N') rt = "no value";
|
lua_typename(L, lua_type(L, narg)));
|
||||||
sprintf(buff, "%.10s expected, got %.10s", type_name, rt);
|
|
||||||
luaL_argerror(L, narg, buff);
|
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))
|
if (space > lua_stackspace(L))
|
||||||
luaL_verror(L, "stack overflow (%.30s)", mes);
|
luaL_verror(L, "stack overflow (%.30s)", mes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
|
||||||
** use the 3rd letter of type names for testing:
|
if (lua_type(L, narg) != t)
|
||||||
** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
|
type_error(L, narg, t);
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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);
|
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);
|
if (len) *len = lua_strlen(L, narg);
|
||||||
return s;
|
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,
|
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
|
||||||
size_t *len) {
|
|
||||||
if (lua_isnull(L, narg)) {
|
if (lua_isnull(L, narg)) {
|
||||||
if (len) *len = def ? strlen(def) : 0;
|
if (len)
|
||||||
|
*len = (def ? strlen(def) : 0);
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
else return checkstr(L, narg, len);
|
else return luaL_check_lstr(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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;
|
if (lua_isnull(L, narg)) return def;
|
||||||
else {
|
else return luaL_check_number(L, narg);
|
||||||
if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
|
|
||||||
return lua_tonumber(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;
|
int i;
|
||||||
for (i=0; i<n; i++)
|
for (i=0; i<n; i++)
|
||||||
lua_register(L, l[i].name, l[i].func);
|
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];
|
char buff[500];
|
||||||
va_list argp;
|
va_list argp;
|
||||||
va_start(argp, fmt);
|
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))
|
if (emptybuffer(B))
|
||||||
adjuststack(B);
|
adjuststack(B);
|
||||||
return B->buffer;
|
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--)
|
while (l--)
|
||||||
luaL_putchar(B, *s++);
|
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));
|
luaL_addlstring(B, s, strlen(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaL_pushresult (luaL_Buffer *B) {
|
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||||
emptybuffer(B);
|
emptybuffer(B);
|
||||||
if (B->level == 0)
|
if (B->level == 0)
|
||||||
lua_pushlstring(B->L, NULL, 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;
|
lua_State *L = B->L;
|
||||||
size_t vl = lua_strlen(L, -1);
|
size_t vl = lua_strlen(L, -1);
|
||||||
if (vl <= bufffree(B)) { /* fit into buffer? */
|
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->L = L;
|
||||||
B->p = B->buffer;
|
B->p = B->buffer;
|
||||||
B->level = 0;
|
B->level = 0;
|
||||||
|
|||||||
43
lauxlib.h
43
lauxlib.h
@@ -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
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -15,25 +15,30 @@
|
|||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef LUALIB_API
|
||||||
|
#define LUALIB_API extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
struct luaL_reg {
|
struct luaL_reg {
|
||||||
const char *name;
|
const char *name;
|
||||||
lua_CFunction func;
|
lua_CFunction func;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
LUALIB_API void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
||||||
const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len);
|
LUALIB_API 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,
|
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, size_t *len);
|
||||||
size_t *len);
|
LUALIB_API double luaL_check_number (lua_State *L, int numArg);
|
||||||
double luaL_check_number (lua_State *L, int numArg);
|
LUALIB_API double luaL_opt_number (lua_State *L, int numArg, double def);
|
||||||
double luaL_opt_number (lua_State *L, int numArg, double def);
|
|
||||||
|
|
||||||
void luaL_checkstack (lua_State *L, int space, const char *msg);
|
LUALIB_API 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_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, ...);
|
LUALIB_API void luaL_verror (lua_State *L, const char *fmt, ...);
|
||||||
int luaL_findstring (const char *name, const char *const list[]);
|
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
|
#define LUAL_BUFFERSIZE BUFSIZ
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
typedef struct luaL_Buffer {
|
typedef struct luaL_Buffer {
|
||||||
@@ -77,12 +84,12 @@ typedef struct luaL_Buffer {
|
|||||||
|
|
||||||
#define luaL_addsize(B,n) ((B)->p += (n))
|
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||||
|
|
||||||
void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||||
char *luaL_prepbuffer (luaL_Buffer *B);
|
LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B);
|
||||||
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);
|
||||||
void luaL_addstring (luaL_Buffer *B, const char *s);
|
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s);
|
||||||
void luaL_addvalue (luaL_Buffer *B);
|
LUALIB_API void luaL_addvalue (luaL_Buffer *B);
|
||||||
void luaL_pushresult (luaL_Buffer *B);
|
LUALIB_API void luaL_pushresult (luaL_Buffer *B);
|
||||||
|
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|||||||
189
lbaselib.c
189
lbaselib.c
@@ -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
|
** Basic library
|
||||||
** See Copyright Notice in lua.h
|
** 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.
|
** The library `liolib' redefines _ERRORMESSAGE for better error information.
|
||||||
*/
|
*/
|
||||||
static int luaB__ERRORMESSAGE (lua_State *L) {
|
static int luaB__ERRORMESSAGE (lua_State *L) {
|
||||||
luaL_checktype(L, 1, "string");
|
luaL_checktype(L, 1, LUA_TSTRING);
|
||||||
lua_getglobal(L, LUA_ALERT);
|
lua_getglobal(L, LUA_ALERT);
|
||||||
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
|
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
@@ -87,7 +87,7 @@ static int luaB_print (lua_State *L) {
|
|||||||
static int luaB_tonumber (lua_State *L) {
|
static int luaB_tonumber (lua_State *L) {
|
||||||
int base = luaL_opt_int(L, 2, 10);
|
int base = luaL_opt_int(L, 2, 10);
|
||||||
if (base == 10) { /* standard conversion */
|
if (base == 10) { /* standard conversion */
|
||||||
luaL_checktype(L, 1, "any");
|
luaL_checkany(L, 1);
|
||||||
if (lua_isnumber(L, 1)) {
|
if (lua_isnumber(L, 1)) {
|
||||||
lua_pushnumber(L, lua_tonumber(L, 1));
|
lua_pushnumber(L, lua_tonumber(L, 1));
|
||||||
return 1;
|
return 1;
|
||||||
@@ -118,7 +118,7 @@ static int luaB_error (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_setglobal (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));
|
lua_setglobal(L, luaL_check_string(L, 1));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -129,17 +129,16 @@ static int luaB_getglobal (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_tag (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));
|
lua_pushnumber(L, lua_tag(L, 1));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_settag (lua_State *L) {
|
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_pushvalue(L, 1); /* push table */
|
||||||
lua_settag(L, luaL_check_int(L, 2));
|
lua_settag(L, luaL_check_int(L, 2));
|
||||||
lua_pop(L, 1); /* remove second argument */
|
return 1; /* return table */
|
||||||
return 1; /* return first argument */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_newtag (lua_State *L) {
|
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) {
|
static int luaB_globals (lua_State *L) {
|
||||||
lua_getglobals(L); /* value to be returned */
|
lua_getglobals(L); /* value to be returned */
|
||||||
if (!lua_isnull(L, 1)) {
|
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_pushvalue(L, 1); /* new table of globals */
|
||||||
lua_setglobals(L);
|
lua_setglobals(L);
|
||||||
}
|
}
|
||||||
@@ -164,54 +163,66 @@ static int luaB_globals (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_rawget (lua_State *L) {
|
static int luaB_rawget (lua_State *L) {
|
||||||
luaL_checktype(L, 1, "table");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_checktype(L, 2, "any");
|
luaL_checkany(L, 2);
|
||||||
lua_rawget(L, -2);
|
lua_rawget(L, -2);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_rawset (lua_State *L) {
|
static int luaB_rawset (lua_State *L) {
|
||||||
luaL_checktype(L, 1, "table");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_checktype(L, 2, "any");
|
luaL_checkany(L, 2);
|
||||||
luaL_checktype(L, 3, "any");
|
luaL_checkany(L, 3);
|
||||||
lua_rawset(L, -3);
|
lua_rawset(L, -3);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_settagmethod (lua_State *L) {
|
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);
|
const char *event = luaL_check_string(L, 2);
|
||||||
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
|
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
|
||||||
"function or nil expected");
|
"function or nil expected");
|
||||||
lua_pushnil(L); /* to get its tag */
|
if (strcmp(event, "gc") == 0)
|
||||||
if (strcmp(event, "gc") == 0 && tag != lua_tag(L, -1))
|
|
||||||
lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
|
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);
|
lua_settagmethod(L, tag, event);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int luaB_gettagmethod (lua_State *L) {
|
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;
|
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) {
|
static int luaB_collectgarbage (lua_State *L) {
|
||||||
lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
|
lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int luaB_type (lua_State *L) {
|
static int luaB_type (lua_State *L) {
|
||||||
luaL_checktype(L, 1, "any");
|
luaL_checkany(L, 1);
|
||||||
lua_pushstring(L, lua_type(L, 1));
|
lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int luaB_next (lua_State *L) {
|
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 */
|
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
||||||
if (lua_next(L, 1))
|
if (lua_next(L, 1))
|
||||||
return 2;
|
return 2;
|
||||||
@@ -224,7 +235,8 @@ static int luaB_next (lua_State *L) {
|
|||||||
|
|
||||||
static int passresults (lua_State *L, int status, int oldtop) {
|
static int passresults (lua_State *L, int status, int oldtop) {
|
||||||
static const char *const errornames[] =
|
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) {
|
if (status == 0) {
|
||||||
int nresults = lua_gettop(L) - oldtop;
|
int nresults = lua_gettop(L) - oldtop;
|
||||||
if (nresults > 0)
|
if (nresults > 0)
|
||||||
@@ -264,7 +276,7 @@ static int luaB_call (lua_State *L) {
|
|||||||
int err = 0; /* index of old error method */
|
int err = 0; /* index of old error method */
|
||||||
int i, status;
|
int i, status;
|
||||||
int n;
|
int n;
|
||||||
luaL_checktype(L, 2, "table");
|
luaL_checktype(L, 2, LUA_TTABLE);
|
||||||
n = lua_getn(L, 2);
|
n = lua_getn(L, 2);
|
||||||
if (!lua_isnull(L, 4)) { /* set new error method */
|
if (!lua_isnull(L, 4)) { /* set new error method */
|
||||||
lua_getglobal(L, LUA_ERRORMESSAGE);
|
lua_getglobal(L, LUA_ERRORMESSAGE);
|
||||||
@@ -298,23 +310,23 @@ static int luaB_call (lua_State *L) {
|
|||||||
|
|
||||||
static int luaB_tostring (lua_State *L) {
|
static int luaB_tostring (lua_State *L) {
|
||||||
char buff[64];
|
char buff[64];
|
||||||
switch (lua_type(L, 1)[2]) {
|
switch (lua_type(L, 1)) {
|
||||||
case 'm': /* nuMber */
|
case LUA_TNUMBER:
|
||||||
lua_pushstring(L, lua_tostring(L, 1));
|
lua_pushstring(L, lua_tostring(L, 1));
|
||||||
return 1;
|
return 1;
|
||||||
case 'r': /* stRing */
|
case LUA_TSTRING:
|
||||||
lua_pushvalue(L, 1);
|
lua_pushvalue(L, 1);
|
||||||
return 1;
|
return 1;
|
||||||
case 'b': /* taBle */
|
case LUA_TTABLE:
|
||||||
sprintf(buff, "table: %p", lua_topointer(L, 1));
|
sprintf(buff, "table: %p", lua_topointer(L, 1));
|
||||||
break;
|
break;
|
||||||
case 'n': /* fuNction */
|
case LUA_TFUNCTION:
|
||||||
sprintf(buff, "function: %p", lua_topointer(L, 1));
|
sprintf(buff, "function: %p", lua_topointer(L, 1));
|
||||||
break;
|
break;
|
||||||
case 'e': /* usErdata */
|
case LUA_TUSERDATA:
|
||||||
sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1));
|
sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1));
|
||||||
break;
|
break;
|
||||||
case 'l': /* niL */
|
case LUA_TNIL:
|
||||||
lua_pushstring(L, "nil");
|
lua_pushstring(L, "nil");
|
||||||
return 1;
|
return 1;
|
||||||
default:
|
default:
|
||||||
@@ -327,8 +339,8 @@ static int luaB_tostring (lua_State *L) {
|
|||||||
|
|
||||||
static int luaB_foreachi (lua_State *L) {
|
static int luaB_foreachi (lua_State *L) {
|
||||||
int n, i;
|
int n, i;
|
||||||
luaL_checktype(L, 1, "table");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_checktype(L, 2, "function");
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||||
n = lua_getn(L, 1);
|
n = lua_getn(L, 1);
|
||||||
for (i=1; i<=n; i++) {
|
for (i=1; i<=n; i++) {
|
||||||
lua_pushvalue(L, 2); /* function */
|
lua_pushvalue(L, 2); /* function */
|
||||||
@@ -344,8 +356,8 @@ static int luaB_foreachi (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int luaB_foreach (lua_State *L) {
|
static int luaB_foreach (lua_State *L) {
|
||||||
luaL_checktype(L, 1, "table");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_checktype(L, 2, "function");
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||||
lua_pushnil(L); /* first index */
|
lua_pushnil(L); /* first index */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (lua_next(L, 1) == 0)
|
if (lua_next(L, 1) == 0)
|
||||||
@@ -362,7 +374,7 @@ static int luaB_foreach (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int luaB_assert (lua_State *L) {
|
static int luaB_assert (lua_State *L) {
|
||||||
luaL_checktype(L, 1, "any");
|
luaL_checkany(L, 1);
|
||||||
if (lua_isnil(L, 1))
|
if (lua_isnil(L, 1))
|
||||||
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
|
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
|
||||||
return 0;
|
return 0;
|
||||||
@@ -370,7 +382,7 @@ static int luaB_assert (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int luaB_getn (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));
|
lua_pushnumber(L, lua_getn(L, 1));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -379,7 +391,7 @@ static int luaB_getn (lua_State *L) {
|
|||||||
static int luaB_tinsert (lua_State *L) {
|
static int luaB_tinsert (lua_State *L) {
|
||||||
int v = lua_gettop(L); /* last argument: to be inserted */
|
int v = lua_gettop(L); /* last argument: to be inserted */
|
||||||
int n, pos;
|
int n, pos;
|
||||||
luaL_checktype(L, 1, "table");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
n = lua_getn(L, 1);
|
n = lua_getn(L, 1);
|
||||||
if (v == 2) /* called with only 2 arguments */
|
if (v == 2) /* called with only 2 arguments */
|
||||||
pos = n+1;
|
pos = n+1;
|
||||||
@@ -400,7 +412,7 @@ static int luaB_tinsert (lua_State *L) {
|
|||||||
|
|
||||||
static int luaB_tremove (lua_State *L) {
|
static int luaB_tremove (lua_State *L) {
|
||||||
int pos, n;
|
int pos, n;
|
||||||
luaL_checktype(L, 1, "table");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
n = lua_getn(L, 1);
|
n = lua_getn(L, 1);
|
||||||
pos = luaL_opt_int(L, 2, n);
|
pos = luaL_opt_int(L, 2, n);
|
||||||
if (n <= 0) return 0; /* table is "empty" */
|
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) {
|
static void set2 (lua_State *L, int i, int j) {
|
||||||
lua_rawgeti(L, 1, i);
|
|
||||||
lua_rawgeti(L, 1, j);
|
|
||||||
lua_rawseti(L, 1, i);
|
lua_rawseti(L, 1, i);
|
||||||
lua_rawseti(L, 1, j);
|
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 */
|
/* WARNING: the caller (auxsort) must ensure stack space */
|
||||||
int res;
|
|
||||||
if (!lua_isnil(L, 2)) { /* function? */
|
if (!lua_isnil(L, 2)) { /* function? */
|
||||||
|
int res;
|
||||||
lua_pushvalue(L, 2);
|
lua_pushvalue(L, 2);
|
||||||
if (r) {
|
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
||||||
lua_rawgeti(L, 1, n); /* a[n] */
|
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
|
||||||
lua_pushvalue(L, -3); /* pivot */
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
lua_pushvalue(L, -2); /* pivot */
|
|
||||||
lua_rawgeti(L, 1, n); /* a[n] */
|
|
||||||
}
|
|
||||||
lua_rawcall(L, 2, 1);
|
lua_rawcall(L, 2, 1);
|
||||||
res = !lua_isnil(L, -1);
|
res = !lua_isnil(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
else { /* a < b? */
|
else /* a < b? */
|
||||||
lua_rawgeti(L, 1, n); /* a[n] */
|
return lua_lessthan(L, a, b);
|
||||||
if (r)
|
|
||||||
res = lua_lessthan(L, -1, -2);
|
|
||||||
else
|
|
||||||
res = lua_lessthan(L, -2, -1);
|
|
||||||
}
|
|
||||||
lua_pop(L, 1);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void auxsort (lua_State *L, int l, int u) {
|
static void auxsort (lua_State *L, int l, int u) {
|
||||||
while (l < u) { /* for tail recursion */
|
while (l < u) { /* for tail recursion */
|
||||||
int i, j;
|
int i, j;
|
||||||
luaL_checkstack(L, 4, "array too large");
|
|
||||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||||
|
lua_rawgeti(L, 1, l);
|
||||||
lua_rawgeti(L, 1, u);
|
lua_rawgeti(L, 1, u);
|
||||||
if (sort_comp(L, l, 0)) /* a[u] < a[l]? */
|
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
|
||||||
swap(L, l, u);
|
set2(L, l, u); /* swap a[l] - a[u] */
|
||||||
lua_pop(L, 1);
|
else
|
||||||
|
lua_pop(L, 2);
|
||||||
if (u-l == 1) break; /* only 2 elements */
|
if (u-l == 1) break; /* only 2 elements */
|
||||||
i = (l+u)/2;
|
i = (l+u)/2;
|
||||||
lua_rawgeti(L, 1, i); /* Pivot = a[i] */
|
lua_rawgeti(L, 1, i);
|
||||||
if (sort_comp(L, l, 0)) /* a[i]<a[l]? */
|
lua_rawgeti(L, 1, l);
|
||||||
swap(L, l, i);
|
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
|
||||||
|
set2(L, i, l);
|
||||||
else {
|
else {
|
||||||
if (sort_comp(L, u, 1)) /* a[u]<a[i]? */
|
lua_pop(L, 1); /* remove a[l] */
|
||||||
swap(L, i, u);
|
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 */
|
if (u-l == 2) break; /* only 3 elements */
|
||||||
lua_rawgeti(L, 1, i); /* Pivot */
|
lua_rawgeti(L, 1, i); /* Pivot */
|
||||||
swap(L, i, u-1); /* put median element as pivot (a[u-1]) */
|
lua_pushvalue(L, -1);
|
||||||
/* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
|
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;
|
i = l; j = u-1;
|
||||||
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
||||||
/* repeat i++ until a[i] >= P */
|
/* repeat ++i until a[i] >= P */
|
||||||
while (sort_comp(L, ++i, 1))
|
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||||
if (i>u) lua_error(L, "invalid order function for sorting");
|
if (i>u) lua_error(L, "invalid order function for sorting");
|
||||||
/* repeat j-- until a[j] <= P */
|
lua_pop(L, 1); /* remove a[i] */
|
||||||
while (sort_comp(L, --j, 0))
|
}
|
||||||
|
/* 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<l) lua_error(L, "invalid order function for sorting");
|
||||||
if (j<i) break;
|
lua_pop(L, 1); /* remove a[j] */
|
||||||
swap(L, i, 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] */
|
/* 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] */
|
/* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
|
||||||
if (i-l < u-i) {
|
if (i-l < u-i) {
|
||||||
@@ -505,17 +520,16 @@ static void auxsort (lua_State *L, int l, int u) {
|
|||||||
else {
|
else {
|
||||||
j=i+1; i=u; u=j-2;
|
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 */
|
auxsort(L, j, i); /* call recursively the smaller one */
|
||||||
} /* repeat the routine for the larger one */
|
} /* repeat the routine for the larger one */
|
||||||
}
|
}
|
||||||
|
|
||||||
static int luaB_sort (lua_State *L) {
|
static int luaB_sort (lua_State *L) {
|
||||||
int n;
|
int n;
|
||||||
luaL_checktype(L, 1, "table");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
n = lua_getn(L, 1);
|
n = lua_getn(L, 1);
|
||||||
if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
|
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 */
|
lua_settop(L, 2); /* make sure there is two arguments */
|
||||||
auxsort(L, 1, n);
|
auxsort(L, 1, n);
|
||||||
return 0;
|
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
|
** call corresponding function inserting `globals' as first argument
|
||||||
@@ -601,6 +615,7 @@ static const struct luaL_reg base_funcs[] = {
|
|||||||
{"error", luaB_error},
|
{"error", luaB_error},
|
||||||
{"foreach", luaB_foreach},
|
{"foreach", luaB_foreach},
|
||||||
{"foreachi", luaB_foreachi},
|
{"foreachi", luaB_foreachi},
|
||||||
|
{"gcinfo", luaB_gcinfo},
|
||||||
{"getglobal", luaB_getglobal},
|
{"getglobal", luaB_getglobal},
|
||||||
{"gettagmethod", luaB_gettagmethod},
|
{"gettagmethod", luaB_gettagmethod},
|
||||||
{"globals", luaB_globals},
|
{"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);
|
luaL_openl(L, base_funcs);
|
||||||
lua_pushstring(L, LUA_VERSION);
|
lua_pushstring(L, LUA_VERSION);
|
||||||
lua_setglobal(L, "_VERSION");
|
lua_setglobal(L, "_VERSION");
|
||||||
|
|||||||
9
lcode.c
9
lcode.c
@@ -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
|
** Code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** 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) {
|
static void codelineinfo (FuncState *fs) {
|
||||||
|
Proto *f = fs->f;
|
||||||
LexState *ls = fs->ls;
|
LexState *ls = fs->ls;
|
||||||
if (ls->lastline > fs->lastline) {
|
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);
|
"line info overflow", MAX_INT);
|
||||||
if (ls->lastline > fs->lastline+1)
|
if (ls->lastline > fs->lastline+1)
|
||||||
fs->f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
|
f->lineinfo[f->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
|
||||||
fs->f->lineinfo[fs->nlineinfo++] = fs->pc;
|
f->lineinfo[f->nlineinfo++] = fs->pc;
|
||||||
fs->lastline = ls->lastline;
|
fs->lastline = ls->lastline;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
88
ldblib.c
88
ldblib.c
@@ -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
|
** Interface from Lua to its debug API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -103,65 +103,73 @@ static int setlocal (lua_State *L) {
|
|||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||||
luaL_argerror(L, 1, "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)));
|
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
|
||||||
return 1;
|
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 */
|
/* dummy variables (to define unique addresses) */
|
||||||
static int callhook = LUA_NOREF; /* Lua reference to call hook function */
|
static char key1, key2;
|
||||||
|
#define KEY_CALLHOOK (&key1)
|
||||||
|
#define KEY_LINEHOOK (&key2)
|
||||||
|
|
||||||
|
|
||||||
|
static void hookf (lua_State *L, void *key) {
|
||||||
static void linef (lua_State *L, lua_Debug *ar) {
|
lua_getregistry(L);
|
||||||
if (linehook != LUA_NOREF) {
|
lua_pushuserdata(L, key);
|
||||||
lua_getref(L, linehook);
|
lua_gettable(L, -2);
|
||||||
lua_pushnumber(L, ar->currentline);
|
if (lua_isfunction(L, -1)) {
|
||||||
lua_call(L, 1, 0);
|
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) {
|
static void callf (lua_State *L, lua_Debug *ar) {
|
||||||
if (callhook != LUA_NOREF) {
|
lua_pushstring(L, ar->event);
|
||||||
lua_getref(L, callhook);
|
hookf(L, KEY_CALLHOOK);
|
||||||
lua_pushstring(L, ar->event);
|
}
|
||||||
lua_call(L, 1, 0);
|
|
||||||
}
|
|
||||||
|
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) {
|
static int setcallhook (lua_State *L) {
|
||||||
lua_unref(L, callhook);
|
sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
|
||||||
if (lua_isnull(L, 1)) {
|
return 1;
|
||||||
callhook = LUA_NOREF;
|
|
||||||
lua_setcallhook(L, NULL);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
callhook = lua_ref(L, 1);
|
|
||||||
lua_setcallhook(L, callf);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int setlinehook (lua_State *L) {
|
static int setlinehook (lua_State *L) {
|
||||||
lua_unref(L, linehook);
|
sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
|
||||||
if (lua_isnull(L, 1)) {
|
return 1;
|
||||||
linehook = LUA_NOREF;
|
|
||||||
lua_setlinehook(L, NULL);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
linehook = lua_ref(L, 1);
|
|
||||||
lua_setlinehook(L, linef);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -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);
|
luaL_openl(L, dblib);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
138
ldebug.c
138
ldebug.c
@@ -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
|
** Debug Interface
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -23,34 +23,32 @@
|
|||||||
#include "luadebug.h"
|
#include "luadebug.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const char *getfuncname (lua_State *L, StkId f, const char **name);
|
static const char *getfuncname (lua_State *L, StkId f, const char **name);
|
||||||
|
|
||||||
|
|
||||||
static void setnormalized (TObject *d, const TObject *s) {
|
static void setnormalized (TObject *d, const TObject *s) {
|
||||||
switch (s->ttype) {
|
if (ttype(s) == LUA_TMARK) {
|
||||||
case TAG_CMARK: {
|
clvalue(d) = infovalue(s)->func;
|
||||||
clvalue(d) = clvalue(s);
|
ttype(d) = LUA_TFUNCTION;
|
||||||
ttype(d) = TAG_CCLOSURE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TAG_LMARK: {
|
|
||||||
clvalue(d) = infovalue(s)->func;
|
|
||||||
ttype(d) = TAG_LCLOSURE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: *d = *s;
|
|
||||||
}
|
}
|
||||||
|
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;
|
lua_Hook oldhook = L->callhook;
|
||||||
L->callhook = func;
|
L->callhook = func;
|
||||||
return oldhook;
|
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;
|
lua_Hook oldhook = L->linehook;
|
||||||
L->linehook = func;
|
L->linehook = func;
|
||||||
return oldhook;
|
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);
|
StkId f = aux_stackedfunction(L, level, L->top);
|
||||||
if (f == NULL) return 0; /* there is no such level */
|
if (f == NULL) return 0; /* there is no such level */
|
||||||
else {
|
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)) {
|
switch (ttype(f)) {
|
||||||
case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK:
|
case LUA_TFUNCTION:
|
||||||
return clvalue(f)->nupvalues;
|
return clvalue(f)->nupvalues;
|
||||||
case TAG_LMARK:
|
case LUA_TMARK:
|
||||||
return infovalue(f)->func->nupvalues;
|
return infovalue(f)->func->nupvalues;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
@@ -94,8 +92,8 @@ static int lua_nups (StkId f) {
|
|||||||
|
|
||||||
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
||||||
int refi;
|
int refi;
|
||||||
if (lineinfo == NULL) return -1; /* no line info */
|
if (lineinfo == NULL || pc == -1)
|
||||||
else if (pc == -1) return refline; /* function preamble */
|
return -1; /* no line info or function is not active */
|
||||||
refi = prefi ? *prefi : 0;
|
refi = prefi ? *prefi : 0;
|
||||||
if (lineinfo[refi] < 0)
|
if (lineinfo[refi] < 0)
|
||||||
refline += -lineinfo[refi++];
|
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);
|
CallInfo *ci = infovalue(f);
|
||||||
LUA_ASSERT(ttype(f) == TAG_LMARK, "function has no pc");
|
LUA_ASSERT(isLmark(f), "function has no pc");
|
||||||
return (*ci->pc - ci->func->f.l->code) - 1;
|
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) {
|
static int currentline (StkId f) {
|
||||||
if (ttype(f) != TAG_LMARK)
|
if (!isLmark(f))
|
||||||
return -1; /* only active lua functions have current-line information */
|
return -1; /* only active lua functions have current-line information */
|
||||||
else {
|
else {
|
||||||
CallInfo *ci = infovalue(f);
|
CallInfo *ci = infovalue(f);
|
||||||
int *lineinfo = ci->func->f.l->lineinfo;
|
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) {
|
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;
|
const char *name;
|
||||||
StkId f = ar->_func;
|
StkId f = ar->_func;
|
||||||
Proto *fp = getluaproto(f);
|
Proto *fp = getluaproto(f);
|
||||||
if (!fp) return NULL; /* `f' is not a Lua function? */
|
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;
|
if (!name) return NULL;
|
||||||
luaA_pushobject(L, (f+1)+(localnum-1)); /* push value */
|
luaA_pushobject(L, (f+1)+(n-1)); /* push value */
|
||||||
return name;
|
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;
|
const char *name;
|
||||||
StkId f = ar->_func;
|
StkId f = ar->_func;
|
||||||
Proto *fp = getluaproto(f);
|
Proto *fp = getluaproto(f);
|
||||||
L->top--; /* pop new value */
|
L->top--; /* pop new value */
|
||||||
if (!fp) return NULL; /* `f' is not a Lua function? */
|
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 || name[0] == '*') return NULL; /* `*' starts private locals */
|
if (!name || name[0] == '(') return NULL; /* `(' starts private locals */
|
||||||
*((f+1)+(localnum-1)) = *L->top;
|
*((f+1)+(n-1)) = *L->top;
|
||||||
return name;
|
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)) {
|
switch (ttype(func)) {
|
||||||
case TAG_LCLOSURE:
|
case LUA_TFUNCTION:
|
||||||
infoLproto(ar, clvalue(func)->f.l);
|
cl = clvalue(func);
|
||||||
break;
|
break;
|
||||||
case TAG_LMARK:
|
case LUA_TMARK:
|
||||||
infoLproto(ar, infovalue(func)->func->f.l);
|
cl = infovalue(func)->func;
|
||||||
break;
|
|
||||||
case TAG_CCLOSURE: case TAG_CMARK:
|
|
||||||
ar->source = "(C)";
|
|
||||||
ar->linedefined = -1;
|
|
||||||
ar->what = "C";
|
|
||||||
break;
|
break;
|
||||||
default:
|
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));
|
luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
|
||||||
if (ar->linedefined == 0)
|
if (ar->linedefined == 0)
|
||||||
ar->what = "main";
|
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) {
|
static const char *travtagmethods (lua_State *L, const TObject *o) {
|
||||||
int e;
|
if (ttype(o) == LUA_TFUNCTION) {
|
||||||
for (e=0; e<IM_N; e++) {
|
int e;
|
||||||
int t;
|
for (e=0; e<TM_N; e++) {
|
||||||
for (t=0; t<=L->last_tag; t++)
|
int t;
|
||||||
if (luaO_equalObj(o, luaT_getim(L, t,e)))
|
for (t=0; t<=L->last_tag; t++)
|
||||||
return luaT_eventname[e];
|
if (clvalue(o) == luaT_gettm(L, t, e))
|
||||||
|
return luaT_eventname[e];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -218,14 +224,14 @@ static const char *travglobals (lua_State *L, const TObject *o) {
|
|||||||
int i;
|
int i;
|
||||||
for (i=0; i<g->size; i++) {
|
for (i=0; i<g->size; i++) {
|
||||||
if (luaO_equalObj(o, val(node(g, 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 tsvalue(key(node(g, i)))->str;
|
||||||
}
|
}
|
||||||
return NULL;
|
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;
|
TObject o;
|
||||||
setnormalized(&o, f);
|
setnormalized(&o, f);
|
||||||
/* try to find a name for given function */
|
/* 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;
|
StkId func;
|
||||||
int isactive = (*what != '>');
|
int isactive = (*what != '>');
|
||||||
if (isactive)
|
if (isactive)
|
||||||
@@ -250,21 +256,21 @@ int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
|||||||
for (; *what; what++) {
|
for (; *what; what++) {
|
||||||
switch (*what) {
|
switch (*what) {
|
||||||
case 'S': {
|
case 'S': {
|
||||||
lua_funcinfo(ar, func);
|
funcinfo(L, ar, func);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'l': {
|
case 'l': {
|
||||||
ar->currentline = lua_currentline(func);
|
ar->currentline = currentline(func);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'u': {
|
case 'u': {
|
||||||
ar->nups = lua_nups(func);
|
ar->nups = nups(func);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'n': {
|
case 'n': {
|
||||||
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
|
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
|
||||||
if (ar->namewhat == NULL)
|
if (ar->namewhat == NULL)
|
||||||
lua_getname(L, func, ar);
|
getname(L, func, ar);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'f': {
|
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) {
|
static const char *getobjname (lua_State *L, StkId obj, const char **name) {
|
||||||
StkId func = aux_stackedfunction(L, 0, obj);
|
StkId func = aux_stackedfunction(L, 0, obj);
|
||||||
if (func == NULL || ttype(func) != TAG_LMARK)
|
if (!isLmark(func))
|
||||||
return NULL; /* not a Lua function */
|
return NULL; /* not an active Lua function */
|
||||||
else {
|
else {
|
||||||
Proto *p = infovalue(func)->func->f.l;
|
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 */
|
int stackpos = obj - (func+1); /* func+1 == function base */
|
||||||
Instruction i = luaG_symbexec(p, pc, stackpos);
|
Instruction i = luaG_symbexec(p, pc, stackpos);
|
||||||
LUA_ASSERT(pc != -1, "function must be active");
|
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) {
|
static const char *getfuncname (lua_State *L, StkId f, const char **name) {
|
||||||
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
|
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
|
||||||
if (func == NULL || ttype(func) != TAG_LMARK)
|
if (!isLmark(func))
|
||||||
return NULL; /* not a Lua function */
|
return NULL; /* not an active Lua function */
|
||||||
else {
|
else {
|
||||||
Proto *p = infovalue(func)->func->f.l;
|
Proto *p = infovalue(func)->func->f.l;
|
||||||
int pc = lua_currentpc(func);
|
int pc = currentpc(func);
|
||||||
Instruction i;
|
Instruction i;
|
||||||
if (pc == -1) return NULL; /* function is not activated */
|
if (pc == -1) return NULL; /* function is not activated */
|
||||||
i = p->code[pc];
|
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++;
|
if (ttype(p1) == t) p1++;
|
||||||
LUA_ASSERT(ttype(p1) != t, "must be an error");
|
LUA_ASSERT(ttype(p1) != t, "must be an error");
|
||||||
luaG_typeerror(L, p1, op);
|
luaG_typeerror(L, p1, op);
|
||||||
|
|||||||
4
ldebug.h
4
ldebug.h
@@ -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
|
** Auxiliary functions from Debug Interface module
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
|
|
||||||
void luaG_typeerror (lua_State *L, StkId o, const char *op);
|
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);
|
int luaG_getline (int *lineinfo, int pc, int refline, int *refi);
|
||||||
void luaG_ordererror (lua_State *L, StkId top);
|
void luaG_ordererror (lua_State *L, StkId top);
|
||||||
|
|
||||||
|
|||||||
294
ldo.c
294
ldo.c
@@ -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
|
** Stack and Call structure of Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
|
|
||||||
void luaD_init (lua_State *L, int stacksize) {
|
void luaD_init (lua_State *L, int stacksize) {
|
||||||
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
|
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
|
||||||
|
L->nblocks += stacksize*sizeof(TObject);
|
||||||
L->stack_last = L->stack+(stacksize-1);
|
L->stack_last = L->stack+(stacksize-1);
|
||||||
L->stacksize = stacksize;
|
L->stacksize = stacksize;
|
||||||
L->Cbase = L->top = L->stack;
|
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) {
|
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)) {
|
if (L->stack_last-L->stack > (L->stacksize-1)) {
|
||||||
/* overflow while handling overflow: do what?? */
|
/* overflow while handling overflow */
|
||||||
L->top -= EXTRA_STACK;
|
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
|
||||||
lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
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 {
|
else {
|
||||||
luaD_checkstack(L, diff);
|
luaD_checkstack(L, diff);
|
||||||
while (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) {
|
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
|
||||||
StkId old_Cbase = L->Cbase;
|
StkId old_Cbase = L->Cbase;
|
||||||
StkId old_top = L->Cbase = L->top;
|
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 */
|
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||||
(*hook)(L, ar);
|
(*hook)(L, ar);
|
||||||
LUA_ASSERT(L->allowhooks == 0, "invalid allow");
|
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;
|
lua_Debug ar;
|
||||||
ar._func = func;
|
ar._func = func;
|
||||||
ar.event = event;
|
ar.event = event;
|
||||||
|
infovalue(func)->pc = NULL; /* function is not active */
|
||||||
dohook(L, &ar, callhook);
|
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;
|
StkId old_Cbase = L->Cbase;
|
||||||
int n;
|
int n;
|
||||||
L->Cbase = base; /* new base for C function */
|
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 */
|
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
|
||||||
*(L->top++) = cl->upvalue[n];
|
*(L->top++) = cl->upvalue[n];
|
||||||
n = (*cl->f.c)(L); /* do the actual call */
|
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;
|
StkId base = L->top - nParams;
|
||||||
luaD_openstack(L, base);
|
luaD_openstack(L, base);
|
||||||
*base = *f;
|
clvalue(base) = f;
|
||||||
|
ttype(base) = LUA_TFUNCTION;
|
||||||
luaD_call(L, base, nResults);
|
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.
|
** The number of results is nResults, unless nResults=LUA_MULTRET.
|
||||||
*/
|
*/
|
||||||
void luaD_call (lua_State *L, StkId func, int nResults) {
|
void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||||
|
lua_Hook callhook;
|
||||||
StkId firstResult;
|
StkId firstResult;
|
||||||
lua_Hook callhook = L->callhook;
|
CallInfo ci;
|
||||||
retry: /* for `function' tag method */
|
Closure *cl;
|
||||||
switch (ttype(func)) {
|
if (ttype(func) != LUA_TFUNCTION) {
|
||||||
case TAG_LCLOSURE: {
|
/* `func' is not a function; check the `function' tag method */
|
||||||
CallInfo ci;
|
Closure *tm = luaT_gettmbyObj(L, func, TM_FUNCTION);
|
||||||
ci.func = clvalue(func);
|
if (tm == NULL)
|
||||||
ci.line = 0;
|
luaG_typeerror(L, func, "call");
|
||||||
ttype(func) = TAG_LMARK;
|
luaD_openstack(L, func);
|
||||||
infovalue(func) = &ci;
|
clvalue(func) = tm; /* tag method is the new function to be called */
|
||||||
if (callhook)
|
ttype(func) = LUA_TFUNCTION;
|
||||||
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 */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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 */
|
if (callhook) /* same hook that was active at entry */
|
||||||
luaD_callHook(L, func, callhook, "return");
|
luaD_callHook(L, func, callhook, "return");
|
||||||
/* adjust the number of results */
|
LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag");
|
||||||
if (nResults == LUA_MULTRET)
|
|
||||||
nResults = L->top - firstResult;
|
|
||||||
else
|
|
||||||
luaD_adjusttop(L, firstResult, nResults);
|
|
||||||
/* move results to `func' (to erase parameters and function) */
|
/* move results to `func' (to erase parameters and function) */
|
||||||
while (nResults) {
|
if (nResults == LUA_MULTRET) {
|
||||||
*func++ = *(L->top - nResults);
|
while (firstResult < L->top) /* copy all results */
|
||||||
nResults--;
|
*func++ = *firstResult++;
|
||||||
|
L->top = func;
|
||||||
}
|
}
|
||||||
L->top = func;
|
else { /* copy at most `nResults' */
|
||||||
}
|
for (; nResults > 0 && firstResult < L->top; nResults--)
|
||||||
|
*func++ = *firstResult++;
|
||||||
|
L->top = func;
|
||||||
static void message (lua_State *L, const char *s) {
|
for (; nResults > 0; nResults--) { /* if there are not enough results */
|
||||||
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
|
ttype(L->top) = LUA_TNIL; /* adjust the stack */
|
||||||
if (*luaO_typename(em) == 'f') {
|
incr_top; /* must check stack space */
|
||||||
*L->top = *em;
|
}
|
||||||
incr_top;
|
|
||||||
lua_pushstring(L, s);
|
|
||||||
luaD_call(L, L->top-2, 0);
|
|
||||||
}
|
}
|
||||||
}
|
luaC_checkGC(L);
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Execute a protected call.
|
** 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 */
|
StkId func = L->top - (nargs+1); /* function to be called */
|
||||||
struct lua_longjmp myErrorJmp;
|
struct CallS c;
|
||||||
chain_longjmp(L, &myErrorJmp);
|
int status;
|
||||||
if (setjmp(myErrorJmp.b) == 0) {
|
c.func = func; c.nresults = nresults;
|
||||||
luaD_call(L, func, nresults);
|
status = luaD_runprotected(L, f_call, &c);
|
||||||
}
|
if (status != 0) /* an error occurred? */
|
||||||
else { /* an error occurred: restore the state */
|
L->top = func; /* remove parameters from the stack */
|
||||||
L->top = func; /* remove garbage from the stack */
|
return status;
|
||||||
restore_stack_limit(L);
|
}
|
||||||
}
|
|
||||||
return restore_longjmp(L, &myErrorJmp);
|
|
||||||
|
/*
|
||||||
|
** 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) {
|
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||||
struct lua_longjmp myErrorJmp;
|
struct ParserS p;
|
||||||
unsigned long old_blocks;
|
unsigned long old_blocks;
|
||||||
|
int status;
|
||||||
|
p.z = z; p.bin = bin;
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
old_blocks = L->nblocks;
|
old_blocks = L->nblocks;
|
||||||
chain_longjmp(L, &myErrorJmp);
|
status = luaD_runprotected(L, f_parser, &p);
|
||||||
if (setjmp(myErrorJmp.b) == 0) {
|
if (status == 0) {
|
||||||
Proto *tf = bin ? luaU_undump(L, z) : luaY_parser(L, z);
|
/* add new memory to threshold (as it probably will stay) */
|
||||||
luaV_Lclosure(L, tf, 0);
|
L->GCthreshold += (L->nblocks - old_blocks);
|
||||||
}
|
}
|
||||||
else { /* an error occurred: correct error code */
|
else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
|
||||||
if (myErrorJmp.status == LUA_ERRRUN)
|
status = LUA_ERRSYNTAX;
|
||||||
myErrorJmp.status = LUA_ERRSYNTAX;
|
return status;
|
||||||
}
|
|
||||||
/* add new memory to threshould (as it probably will stay) */
|
|
||||||
L->GCthreshold += (L->nblocks - old_blocks);
|
|
||||||
return restore_longjmp(L, &myErrorJmp); /* error code */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int parse_file (lua_State *L, const char *filename) {
|
static int parse_file (lua_State *L, const char *filename) {
|
||||||
ZIO z;
|
ZIO z;
|
||||||
char source[MAXFILENAME];
|
|
||||||
int status;
|
int status;
|
||||||
int bin; /* flag for file mode */
|
int bin; /* flag for file mode */
|
||||||
int c; /* look ahead char */
|
int c; /* look ahead char */
|
||||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||||
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
|
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);
|
c = fgetc(f);
|
||||||
ungetc(c, f);
|
ungetc(c, f);
|
||||||
bin = (c == ID_CHUNK);
|
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 */
|
f = freopen(filename, "rb", f); /* set binary mode */
|
||||||
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
|
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);
|
status = protectedparser(L, &z, bin);
|
||||||
if (f != stdin)
|
if (f != stdin)
|
||||||
fclose(f);
|
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);
|
int status = parse_file(L, filename);
|
||||||
if (status == 0) /* parse OK? */
|
if (status == 0) /* parse OK? */
|
||||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
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,
|
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name) {
|
||||||
const char *name) {
|
|
||||||
int status = parse_buffer(L, buff, size, name);
|
int status = parse_buffer(L, buff, size, name);
|
||||||
if (status == 0) /* parse OK? */
|
if (status == 0) /* parse OK? */
|
||||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
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);
|
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
8
ldo.h
@@ -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
|
** Stack and Call structure of Lua
|
||||||
** See Copyright Notice in lua.h
|
** 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_adjusttop (lua_State *L, StkId base, int extra);
|
||||||
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
|
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_call (lua_State *L, StkId func, int nResults);
|
||||||
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);
|
||||||
void luaD_breakrun (lua_State *L, int errcode);
|
|
||||||
void luaD_checkstack (lua_State *L, int n);
|
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
|
#endif
|
||||||
|
|||||||
59
lfunc.c
59
lfunc.c
@@ -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
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,47 +13,68 @@
|
|||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
#include "lstate.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 *luaF_newclosure (lua_State *L, int nelems) {
|
||||||
Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
|
int size = sizeclosure(nelems);
|
||||||
(lint32)sizeof(TObject)*(nelems-1));
|
Closure *c = (Closure *)luaM_malloc(L, size);
|
||||||
c->next = L->rootcl;
|
c->next = L->rootcl;
|
||||||
L->rootcl = c;
|
L->rootcl = c;
|
||||||
c->mark = c;
|
c->mark = c;
|
||||||
c->nupvalues = nelems;
|
c->nupvalues = nelems;
|
||||||
L->nblocks += gcsizeclosure(L, c);
|
L->nblocks += size;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Proto *luaF_newproto (lua_State *L) {
|
Proto *luaF_newproto (lua_State *L) {
|
||||||
Proto *f = luaM_new(L, Proto);
|
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->knum = NULL;
|
||||||
f->nknum = 0;
|
f->nknum = 0;
|
||||||
|
f->kstr = NULL;
|
||||||
|
f->nkstr = 0;
|
||||||
f->kproto = NULL;
|
f->kproto = NULL;
|
||||||
f->nkproto = 0;
|
f->nkproto = 0;
|
||||||
f->locvars = NULL;
|
f->code = NULL;
|
||||||
f->nlocvars = 0;
|
f->ncode = 0;
|
||||||
f->next = L->rootproto;
|
f->numparams = 0;
|
||||||
L->rootproto = f;
|
f->is_vararg = 0;
|
||||||
|
f->maxstacksize = 0;
|
||||||
f->marked = 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;
|
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) {
|
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->code);
|
||||||
luaM_free(L, f->locvars);
|
luaM_free(L, f->locvars);
|
||||||
luaM_free(L, f->kstr);
|
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) {
|
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||||
L->nblocks -= gcsizeclosure(L, c);
|
L->nblocks -= sizeclosure(c->nupvalues);
|
||||||
luaM_free(L, c);
|
luaM_free(L, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
lfunc.h
3
lfunc.h
@@ -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
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
|
|
||||||
Proto *luaF_newproto (lua_State *L);
|
Proto *luaF_newproto (lua_State *L);
|
||||||
|
void luaF_protook (lua_State *L, Proto *f, int pc);
|
||||||
Closure *luaF_newclosure (lua_State *L, int nelems);
|
Closure *luaF_newclosure (lua_State *L, int nelems);
|
||||||
void luaF_freeproto (lua_State *L, Proto *f);
|
void luaF_freeproto (lua_State *L, Proto *f);
|
||||||
void luaF_freeclosure (lua_State *L, Closure *c);
|
void luaF_freeclosure (lua_State *L, Closure *c);
|
||||||
|
|||||||
153
lgc.c
153
lgc.c
@@ -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
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** 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 */
|
/* 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) {
|
static void markclosure (GCState *st, Closure *cl) {
|
||||||
int e;
|
if (!ismarked(cl)) {
|
||||||
for (e=0; e<IM_N; e++) {
|
if (!cl->isC)
|
||||||
int t;
|
protomark(cl->f.l);
|
||||||
for (t=0; t<=L->last_tag; t++)
|
cl->mark = st->cmark; /* chain it for later traversal */
|
||||||
markobject(st, luaT_getim(L, t,e));
|
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)) {
|
switch (ttype(o)) {
|
||||||
case TAG_USERDATA: case TAG_STRING:
|
case LUA_TUSERDATA: case LUA_TSTRING:
|
||||||
strmark(tsvalue(o));
|
strmark(tsvalue(o));
|
||||||
break;
|
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))) {
|
if (!ismarked(hvalue(o))) {
|
||||||
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
|
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
|
||||||
st->tmark = hvalue(o);
|
st->tmark = hvalue(o);
|
||||||
}
|
}
|
||||||
break;
|
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 */
|
default: break; /* numbers, etc */
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -131,8 +130,8 @@ static void markall (lua_State *L) {
|
|||||||
st.tmark = h->mark; /* remove it from list */
|
st.tmark = h->mark; /* remove it from list */
|
||||||
for (i=0; i<h->size; i++) {
|
for (i=0; i<h->size; i++) {
|
||||||
Node *n = node(h, i);
|
Node *n = node(h, i);
|
||||||
if (ttype(key(n)) != TAG_NIL) {
|
if (ttype(key(n)) != LUA_TNIL) {
|
||||||
if (ttype(val(n)) == TAG_NIL)
|
if (ttype(val(n)) == LUA_TNIL)
|
||||||
luaH_remove(h, key(n)); /* dead element; try to remove it */
|
luaH_remove(h, key(n)); /* dead element; try to remove it */
|
||||||
markobject(&st, &n->key);
|
markobject(&st, &n->key);
|
||||||
markobject(&st, &n->val);
|
markobject(&st, &n->val);
|
||||||
@@ -147,12 +146,12 @@ static void markall (lua_State *L) {
|
|||||||
static int hasmark (const TObject *o) {
|
static int hasmark (const TObject *o) {
|
||||||
/* valid only for locked objects */
|
/* valid only for locked objects */
|
||||||
switch (o->ttype) {
|
switch (o->ttype) {
|
||||||
case TAG_STRING: case TAG_USERDATA:
|
case LUA_TSTRING: case LUA_TUSERDATA:
|
||||||
return tsvalue(o)->marked;
|
return tsvalue(o)->marked;
|
||||||
case TAG_TABLE:
|
case LUA_TTABLE:
|
||||||
return ismarked(hvalue(o));
|
return ismarked(hvalue(o));
|
||||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
case LUA_TFUNCTION:
|
||||||
return ismarked(clvalue(o)->mark);
|
return ismarked(clvalue(o));
|
||||||
default: /* number */
|
default: /* number */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -235,18 +234,13 @@ static void checktab (lua_State *L, stringtable *tb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
static void collectstrings (lua_State *L, int all) {
|
||||||
** 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) {
|
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<L->strt.size; i++) { /* for each list */
|
for (i=0; i<L->strt.size; i++) { /* for each list */
|
||||||
TString **p = &L->strt.hash[i];
|
TString **p = &L->strt.hash[i];
|
||||||
TString *next;
|
TString *next;
|
||||||
while ((next = *p) != NULL) {
|
while ((next = *p) != NULL) {
|
||||||
if ((int)next->marked > limit) { /* preserve? */
|
if (next->marked && !all) { /* preserve? */
|
||||||
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
||||||
next->marked = 0;
|
next->marked = 0;
|
||||||
p = &next->nexthash;
|
p = &next->nexthash;
|
||||||
@@ -254,7 +248,7 @@ static void collectstringtab (lua_State *L, int limit) {
|
|||||||
else { /* collect */
|
else { /* collect */
|
||||||
*p = next->nexthash;
|
*p = next->nexthash;
|
||||||
L->strt.nuse--;
|
L->strt.nuse--;
|
||||||
L->nblocks -= gcsizestring(L, next->u.s.len);
|
L->nblocks -= sizestring(next->len);
|
||||||
luaM_free(L, next);
|
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;
|
int i;
|
||||||
for (i=0; i<L->udt.size; i++) { /* for each list */
|
for (i=0; i<L->udt.size; i++) { /* for each list */
|
||||||
TString **p = &L->udt.hash[i];
|
TString **p = &L->udt.hash[i];
|
||||||
TString *next;
|
TString *next;
|
||||||
while ((next = *p) != NULL) {
|
while ((next = *p) != NULL) {
|
||||||
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
|
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
|
||||||
if ((int)next->marked > all) { /* preserve? */
|
if (next->marked && !all) { /* preserve? */
|
||||||
next->marked = 0;
|
next->marked = 0;
|
||||||
p = &next->nexthash;
|
p = &next->nexthash;
|
||||||
}
|
}
|
||||||
else { /* collect */
|
else { /* collect */
|
||||||
int tag = next->u.d.tag;
|
int tag = next->u.d.tag;
|
||||||
if (tag > L->last_tag) tag = TAG_USERDATA;
|
|
||||||
*p = next->nexthash;
|
*p = next->nexthash;
|
||||||
next->nexthash = L->IMtable[tag].collected; /* chain udata */
|
next->nexthash = L->TMtable[tag].collected; /* chain udata */
|
||||||
L->IMtable[tag].collected = next;
|
L->TMtable[tag].collected = next;
|
||||||
L->nblocks -= gcsizeudata;
|
L->nblocks -= sizestring(next->len);
|
||||||
L->udt.nuse--;
|
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) {
|
static void callgcTM (lua_State *L, const TObject *o) {
|
||||||
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
|
Closure *tm = luaT_gettmbyObj(L, o, TM_GC);
|
||||||
if (ttype(im) != TAG_NIL) {
|
if (tm != NULL) {
|
||||||
|
int oldah = L->allowhooks;
|
||||||
|
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
||||||
luaD_checkstack(L, 2);
|
luaD_checkstack(L, 2);
|
||||||
*(L->top) = *im;
|
clvalue(L->top) = tm;
|
||||||
|
ttype(L->top) = LUA_TFUNCTION;
|
||||||
*(L->top+1) = *o;
|
*(L->top+1) = *o;
|
||||||
L->top += 2;
|
L->top += 2;
|
||||||
luaD_call(L, L->top-2, 0);
|
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) {
|
static void callgcTMudata (lua_State *L) {
|
||||||
int tag;
|
int tag;
|
||||||
TObject o;
|
TObject o;
|
||||||
ttype(&o) = TAG_USERDATA;
|
ttype(&o) = LUA_TUSERDATA;
|
||||||
for (tag=L->last_tag; tag>=0; tag--) {
|
L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */
|
||||||
TString *udata = L->IMtable[tag].collected;
|
for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */
|
||||||
L->IMtable[tag].collected = NULL;
|
TString *udata;
|
||||||
while (udata) {
|
while ((udata = L->TMtable[tag].collected) != NULL) {
|
||||||
TString *next = udata->nexthash;
|
L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
|
||||||
tsvalue(&o) = udata;
|
tsvalue(&o) = udata;
|
||||||
callgcTM(L, &o);
|
callgcTM(L, &o);
|
||||||
luaM_free(L, udata);
|
luaM_free(L, udata);
|
||||||
udata = next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaC_collect (lua_State *L, int all) {
|
void luaC_collect (lua_State *L, int all) {
|
||||||
int oldah = L->allowhooks;
|
collectudata(L, all);
|
||||||
L->allowhooks = 0; /* stop debug hooks during GC */
|
|
||||||
L->GCthreshold *= 4; /* to avoid GC during GC */
|
|
||||||
collectudatatab(L, all);
|
|
||||||
callgcTMudata(L);
|
callgcTMudata(L);
|
||||||
collectstringtab(L, all?MAX_INT:0);
|
collectstrings(L, all);
|
||||||
collecttable(L);
|
collecttable(L);
|
||||||
collectproto(L);
|
collectproto(L);
|
||||||
collectclosure(L);
|
collectclosure(L);
|
||||||
L->allowhooks = oldah; /* restore hooks */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define MINBUFFER 256
|
static void luaC_collectgarbage (lua_State *L) {
|
||||||
|
|
||||||
long lua_collectgarbage (lua_State *L, long limit) {
|
|
||||||
unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
|
|
||||||
markall(L);
|
markall(L);
|
||||||
invalidaterefs(L);
|
invalidaterefs(L); /* check unlocked references */
|
||||||
luaC_collect(L, 0);
|
luaC_collect(L, 0);
|
||||||
recovered = recovered - L->nblocks;
|
checkMbuffer(L);
|
||||||
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
L->GCthreshold = 2*L->nblocks; /* set new threshold */
|
||||||
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
|
||||||
L->Mbuffsize /= 2; /* still larger than MINBUFFER */
|
|
||||||
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
|
||||||
}
|
|
||||||
callgcTM(L, &luaO_nilobject);
|
callgcTM(L, &luaO_nilobject);
|
||||||
return recovered;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaC_checkGC (lua_State *L) {
|
void luaC_checkGC (lua_State *L) {
|
||||||
if (L->nblocks >= L->GCthreshold)
|
if (L->nblocks >= L->GCthreshold)
|
||||||
lua_collectgarbage(L, 0);
|
luaC_collectgarbage(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
lgc.h
4
lgc.h
@@ -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
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -11,8 +11,8 @@
|
|||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
|
|
||||||
|
|
||||||
void luaC_checkGC (lua_State *L);
|
|
||||||
void luaC_collect (lua_State *L, int all);
|
void luaC_collect (lua_State *L, int all);
|
||||||
|
void luaC_checkGC (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
42
liolib.c
42
liolib.c
@@ -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
|
** Standard I/O (and system) library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -159,18 +159,9 @@ static int io_close (lua_State *L) {
|
|||||||
|
|
||||||
static int file_collect (lua_State *L) {
|
static int file_collect (lua_State *L) {
|
||||||
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
lua_pop(L, 1); /* remove upvalue */
|
FILE *f = getnonullfile(L, ctrl, 1);
|
||||||
if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) {
|
if (f != stdin && f != stdout && f != stderr)
|
||||||
/* collecting `ctrl' itself */
|
CLOSEFILE(L, f);
|
||||||
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);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,7 +315,7 @@ static int read_word (lua_State *L, FILE *f) {
|
|||||||
}
|
}
|
||||||
ungetc(c, f);
|
ungetc(c, f);
|
||||||
luaL_pushresult(&b); /* close buffer */
|
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;
|
size_t size = BUFSIZ;
|
||||||
char *buffer = NULL;
|
char *buffer = NULL;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
buffer = (char *)realloc(buffer, size);
|
char *newbuffer = (char *)realloc(buffer, size);
|
||||||
if (buffer == NULL)
|
if (newbuffer == NULL) {
|
||||||
|
free(buffer);
|
||||||
lua_error(L, "not enough memory to read a file");
|
lua_error(L, "not enough memory to read a file");
|
||||||
|
}
|
||||||
|
buffer = newbuffer;
|
||||||
len += fread(buffer+len, sizeof(char), size-len, f);
|
len += fread(buffer+len, sizeof(char), size-len, f);
|
||||||
if (len < size) break; /* did not read all it could */
|
if (len < size) break; /* did not read all it could */
|
||||||
size *= 2;
|
size *= 2;
|
||||||
@@ -449,7 +443,7 @@ static int io_write (lua_State *L) {
|
|||||||
if (f) arg++;
|
if (f) arg++;
|
||||||
else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
|
else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
|
||||||
for (; arg <= lastarg; arg++) {
|
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 */
|
/* optimization: could be done exactly as for strings */
|
||||||
status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
|
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) {
|
static void openwithcontrol (lua_State *L) {
|
||||||
IOCtrl *ctrl = (IOCtrl *)malloc(sizeof(IOCtrl));
|
IOCtrl *ctrl = (IOCtrl *)lua_newuserdata(L, sizeof(IOCtrl));
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
int ctrltag = lua_newtag(L);
|
|
||||||
ctrl->iotag = lua_newtag(L);
|
ctrl->iotag = lua_newtag(L);
|
||||||
ctrl->closedtag = lua_newtag(L);
|
ctrl->closedtag = lua_newtag(L);
|
||||||
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
|
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
|
||||||
/* put `ctrl' as upvalue for these functions */
|
/* put `ctrl' as upvalue for these functions */
|
||||||
lua_pushusertag(L, ctrl, ctrltag);
|
lua_pushvalue(L, -1);
|
||||||
lua_pushcclosure(L, iolibtag[i].func, 1);
|
lua_pushcclosure(L, iolibtag[i].func, 1);
|
||||||
lua_setglobal(L, iolibtag[i].name);
|
lua_setglobal(L, iolibtag[i].name);
|
||||||
}
|
}
|
||||||
@@ -712,16 +705,13 @@ static void openwithcontrol (lua_State *L) {
|
|||||||
setfilebyname(L, ctrl, stdin, "_STDIN");
|
setfilebyname(L, ctrl, stdin, "_STDIN");
|
||||||
setfilebyname(L, ctrl, stdout, "_STDOUT");
|
setfilebyname(L, ctrl, stdout, "_STDOUT");
|
||||||
setfilebyname(L, ctrl, stderr, "_STDERR");
|
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 */
|
/* 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);
|
luaL_openl(L, iolib);
|
||||||
openwithcontrol(L);
|
openwithcontrol(L);
|
||||||
}
|
}
|
||||||
|
|||||||
28
llex.c
28
llex.c
@@ -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
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** 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) {
|
void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
|
||||||
char buff[MAXSRC];
|
char buff[MAXSRC];
|
||||||
luaO_chunkid(buff, ls->source->str, sizeof(buff));
|
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);
|
s, token, ls->linenumber, buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ static const char *readname (LexState *LS) {
|
|||||||
|
|
||||||
|
|
||||||
/* LUA_NUMBER */
|
/* 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;
|
lua_State *L = LS->L;
|
||||||
size_t l = 0;
|
size_t l = 0;
|
||||||
checkbuffer(L, 10, l);
|
checkbuffer(L, 10, l);
|
||||||
@@ -178,12 +178,12 @@ static void read_number (LexState *LS, int comma) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
save(L, '\0', l);
|
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);
|
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;
|
lua_State *L = LS->L;
|
||||||
int cont = 0;
|
int cont = 0;
|
||||||
size_t l = 0;
|
size_t l = 0;
|
||||||
@@ -222,11 +222,11 @@ static void read_long_string (LexState *LS) {
|
|||||||
} endloop:
|
} endloop:
|
||||||
save_and_next(L, LS, l); /* skip the second ']' */
|
save_and_next(L, LS, l); /* skip the second ']' */
|
||||||
save(L, '\0', l);
|
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;
|
lua_State *L = LS->L;
|
||||||
size_t l = 0;
|
size_t l = 0;
|
||||||
checkbuffer(L, 10, l);
|
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_and_next(L, LS, l); /* skip delimiter */
|
||||||
save(L, '\0', l);
|
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 (;;) {
|
for (;;) {
|
||||||
switch (LS->current) {
|
switch (LS->current) {
|
||||||
|
|
||||||
@@ -304,7 +304,7 @@ int luaX_lex (LexState *LS) {
|
|||||||
next(LS);
|
next(LS);
|
||||||
if (LS->current != '[') return '[';
|
if (LS->current != '[') return '[';
|
||||||
else {
|
else {
|
||||||
read_long_string(LS);
|
read_long_string(LS, seminfo);
|
||||||
return TK_STRING;
|
return TK_STRING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,7 +330,7 @@ int luaX_lex (LexState *LS) {
|
|||||||
|
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
read_string(LS, LS->current);
|
read_string(LS, LS->current, seminfo);
|
||||||
return TK_STRING;
|
return TK_STRING;
|
||||||
|
|
||||||
case '.':
|
case '.':
|
||||||
@@ -345,13 +345,13 @@ int luaX_lex (LexState *LS) {
|
|||||||
}
|
}
|
||||||
else if (!isdigit(LS->current)) return '.';
|
else if (!isdigit(LS->current)) return '.';
|
||||||
else {
|
else {
|
||||||
read_number(LS, 1);
|
read_number(LS, 1, seminfo);
|
||||||
return TK_NUMBER;
|
return TK_NUMBER;
|
||||||
}
|
}
|
||||||
|
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
case '5': case '6': case '7': case '8': case '9':
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
read_number(LS, 0);
|
read_number(LS, 0, seminfo);
|
||||||
return TK_NUMBER;
|
return TK_NUMBER;
|
||||||
|
|
||||||
case EOZ:
|
case EOZ:
|
||||||
@@ -371,7 +371,7 @@ int luaX_lex (LexState *LS) {
|
|||||||
TString *ts = luaS_new(LS->L, readname(LS));
|
TString *ts = luaS_new(LS->L, readname(LS));
|
||||||
if (ts->marked >= RESERVEDMARK) /* reserved word? */
|
if (ts->marked >= RESERVEDMARK) /* reserved word? */
|
||||||
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
|
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
|
||||||
LS->t.seminfo.ts = ts;
|
seminfo->ts = ts;
|
||||||
return TK_NAME;
|
return TK_NAME;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
llex.h
16
llex.h
@@ -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
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -35,14 +35,18 @@ enum RESERVED {
|
|||||||
#define NUM_RESERVED ((int)(TK_WHILE-FIRST_RESERVED+1))
|
#define NUM_RESERVED ((int)(TK_WHILE-FIRST_RESERVED+1))
|
||||||
|
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
Number r;
|
||||||
|
TString *ts;
|
||||||
|
} SemInfo; /* semantics information */
|
||||||
|
|
||||||
|
|
||||||
typedef struct Token {
|
typedef struct Token {
|
||||||
int token;
|
int token;
|
||||||
union {
|
SemInfo seminfo;
|
||||||
Number r;
|
|
||||||
TString *ts;
|
|
||||||
} seminfo; /* semantics information */
|
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
|
|
||||||
typedef struct LexState {
|
typedef struct LexState {
|
||||||
int current; /* current character */
|
int current; /* current character */
|
||||||
Token t; /* current token */
|
Token t; /* current token */
|
||||||
@@ -58,7 +62,7 @@ typedef struct LexState {
|
|||||||
|
|
||||||
void luaX_init (lua_State *L);
|
void luaX_init (lua_State *L);
|
||||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source);
|
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_checklimit (LexState *ls, int val, int limit, const char *msg);
|
||||||
void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
|
void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
|
||||||
void luaX_error (LexState *ls, const char *s, int token);
|
void luaX_error (LexState *ls, const char *s, int token);
|
||||||
|
|||||||
32
llimits.h
32
llimits.h
@@ -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
|
** Limits, basic types, and some other "installation-dependent" definitions
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -41,6 +41,14 @@
|
|||||||
|
|
||||||
typedef LUA_NUM_TYPE Number;
|
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 */
|
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)
|
#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 */
|
#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
|
** 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
|
** For a very small machine, you may change that to 2 bytes (and adjust
|
||||||
** the following limits accordingly)
|
** the following limits accordingly)
|
||||||
*/
|
*/
|
||||||
@@ -103,7 +109,7 @@ typedef unsigned long Instruction;
|
|||||||
/*
|
/*
|
||||||
** limits for opcode arguments.
|
** limits for opcode arguments.
|
||||||
** we use (signed) int to manipulate most 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
|
#if SIZE_U < BITS_INT-1
|
||||||
#define MAXARG_U ((1<<SIZE_U)-1)
|
#define MAXARG_U ((1<<SIZE_U)-1)
|
||||||
@@ -195,10 +201,4 @@ typedef unsigned long Instruction;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* maximum part of a file name kept for error messages */
|
|
||||||
#ifndef MAXFILENAME
|
|
||||||
#define MAXFILENAME 260
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
11
lmathlib.c
11
lmathlib.c
@@ -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
|
** Standard mathematical library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -228,12 +228,11 @@ static const struct luaL_reg mathlib[] = {
|
|||||||
/*
|
/*
|
||||||
** Open math library
|
** Open math library
|
||||||
*/
|
*/
|
||||||
void lua_mathlibopen (lua_State *L) {
|
LUALIB_API void lua_mathlibopen (lua_State *L) {
|
||||||
luaL_openl(L, mathlib);
|
luaL_openl(L, mathlib);
|
||||||
lua_pushnumber(L, 0); /* to get its tag */
|
|
||||||
lua_pushcfunction(L, math_pow);
|
lua_pushcfunction(L, math_pow);
|
||||||
lua_settagmethod(L, lua_tag(L, -2), "pow");
|
lua_settagmethod(L, LUA_TNUMBER, "pow");
|
||||||
lua_pop(L, 1); /* remove number */
|
lua_pushnumber(L, PI);
|
||||||
lua_pushnumber(L, PI); lua_setglobal(L, "PI");
|
lua_setglobal(L, "PI");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
lmem.c
31
lmem.c
@@ -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
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -15,18 +15,9 @@
|
|||||||
#include "lstate.h"
|
#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 LUA_DEBUG
|
||||||
#ifdef DEBUG
|
|
||||||
/*
|
/*
|
||||||
** {======================================================================
|
** {======================================================================
|
||||||
** Controlled version for realloc.
|
** Controlled version for realloc.
|
||||||
@@ -38,17 +29,13 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#undef realloc
|
|
||||||
#undef malloc
|
|
||||||
#undef free
|
|
||||||
#define realloc(b, s) debug_realloc(b, s)
|
#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)
|
#define free(b) debug_realloc(b, 0)
|
||||||
|
|
||||||
|
|
||||||
/* ensures maximum alignment for HEADER */
|
/* ensures maximum alignment for HEADER */
|
||||||
union L_U { double d; char *s; long l; };
|
#define HEADER (sizeof(union L_Umaxalign))
|
||||||
#define HEADER (sizeof(union L_U))
|
|
||||||
|
|
||||||
#define MARKSIZE 16
|
#define MARKSIZE 16
|
||||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
#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,
|
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||||
int inc, size_t size, const char *errormsg, size_t limit) {
|
int inc, size_t size, const char *errormsg, size_t limit) {
|
||||||
size_t newn = nelems+inc;
|
size_t newn = nelems+inc;
|
||||||
|
|||||||
4
lmem.h
4
lmem.h
@@ -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
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** 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)))
|
((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t)))
|
||||||
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef LUA_DEBUG
|
||||||
extern unsigned long memdebug_numblocks;
|
extern unsigned long memdebug_numblocks;
|
||||||
extern unsigned long memdebug_total;
|
extern unsigned long memdebug_total;
|
||||||
extern unsigned long memdebug_maxmem;
|
extern unsigned long memdebug_maxmem;
|
||||||
|
|||||||
139
lobject.c
139
lobject.c
@@ -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
|
** Some generic functions over Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -17,18 +17,15 @@
|
|||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** you can use the fact that the 3rd letter or each name is always different
|
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
|
||||||
** (e-m-r-b-n-l) to compare and switch these strings
|
|
||||||
*/
|
|
||||||
const char *const luaO_typenames[] = { /* ORDER LUA_T */
|
const char *const luaO_typenames[] = {
|
||||||
"userdata", "number", "string", "table", "function", "function", "nil",
|
"userdata", "nil", "number", "string", "table", "function"
|
||||||
"function", "function"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const TObject luaO_nilobject = {TAG_NIL, {NULL}};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
** 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) {
|
int luaO_equalObj (const TObject *t1, const TObject *t2) {
|
||||||
if (ttype(t1) != ttype(t2)) return 0;
|
if (ttype(t1) != ttype(t2)) return 0;
|
||||||
switch (ttype(t1)) {
|
switch (ttype(t1)) {
|
||||||
case TAG_NUMBER:
|
case LUA_TNUMBER:
|
||||||
return nvalue(t1) == nvalue(t2);
|
return nvalue(t1) == nvalue(t2);
|
||||||
case TAG_STRING: case TAG_USERDATA:
|
case LUA_TSTRING: case LUA_TUSERDATA:
|
||||||
return tsvalue(t1) == tsvalue(t2);
|
return tsvalue(t1) == tsvalue(t2);
|
||||||
case TAG_TABLE:
|
case LUA_TTABLE:
|
||||||
return hvalue(t1) == hvalue(t2);
|
return hvalue(t1) == hvalue(t2);
|
||||||
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
case LUA_TFUNCTION:
|
||||||
return clvalue(t1) == clvalue(t2);
|
return clvalue(t1) == clvalue(t2);
|
||||||
default:
|
default:
|
||||||
LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
|
LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
|
||||||
return 1; /* TAG_NIL */
|
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) {
|
char *luaO_openspace (lua_State *L, size_t n) {
|
||||||
if (n > L->Mbuffsize) {
|
if (n > L->Mbuffsize) {
|
||||||
luaM_reallocvector(L, L->Mbuffer, n, char);
|
luaM_reallocvector(L, L->Mbuffer, n, char);
|
||||||
|
L->nblocks += (n - L->Mbuffsize)*sizeof(char);
|
||||||
L->Mbuffsize = n;
|
L->Mbuffsize = n;
|
||||||
}
|
}
|
||||||
return L->Mbuffer;
|
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 */
|
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
|
||||||
double a = 0.0;
|
char *endptr;
|
||||||
int point = 0; /* number of decimal digits */
|
Number res = lua_str2number(s, &endptr);
|
||||||
int sig;
|
if (endptr == s) return 0; /* no conversion */
|
||||||
while (isspace((unsigned char)*s)) s++;
|
while (isspace((unsigned char)*endptr)) endptr++;
|
||||||
sig = 0;
|
if (*endptr != '\0') return 0; /* invalid trailing characters? */
|
||||||
switch (*s) {
|
*result = res;
|
||||||
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;
|
|
||||||
return 1;
|
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, ...) {
|
void luaO_verror (lua_State *L, const char *fmt, ...) {
|
||||||
char buff[500];
|
|
||||||
va_list argp;
|
va_list argp;
|
||||||
|
char buff[MAX_VERROR]; /* to hold formatted message */
|
||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
vsprintf(buff, fmt, argp);
|
vsprintf(buff, fmt, argp);
|
||||||
va_end(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 bufflen) {
|
||||||
|
if (*source == '=') {
|
||||||
void luaO_chunkid (char *out, const char *source, int len) {
|
strncpy(out, source+1, bufflen); /* remove first char */
|
||||||
if (*source == '(') {
|
out[bufflen-1] = '\0'; /* ensures null termination */
|
||||||
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 */
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
len -= EXTRALEN;
|
if (*source == '@') {
|
||||||
if (*source == '@')
|
int l;
|
||||||
sprintf(out, "file `%.*s'", len, source+1);
|
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 {
|
else {
|
||||||
const char *b = strchr(source , '\n'); /* stop at first new line */
|
int len = strcspn(source, "\n"); /* stop at first newline */
|
||||||
int lim = (b && (b-source)<len) ? b-source : len;
|
bufflen -= sizeof("string \"%.*s...\"");
|
||||||
sprintf(out, "string \"%.*s\"", lim, source);
|
if (len > bufflen) len = bufflen;
|
||||||
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
83
lobject.h
83
lobject.h
@@ -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
|
** Type definitions for Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef LUA_DEBUG
|
||||||
#undef NDEBUG
|
#undef NDEBUG
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#define LUA_INTERNALERROR(s) assert(((void)s,0))
|
#define LUA_INTERNALERROR(s) assert(((void)s,0))
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef LUA_DEBUG
|
||||||
/* to avoid warnings, and make sure value is really unused */
|
/* to avoid warnings, and make sure value is really unused */
|
||||||
#define UNUSED(x) (x=0, (void)(x))
|
#define UNUSED(x) (x=0, (void)(x))
|
||||||
#else
|
#else
|
||||||
@@ -31,41 +31,24 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* mark for closures active in the stack */
|
||||||
** Lua TYPES
|
#define LUA_TMARK 6
|
||||||
** 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 */
|
|
||||||
|
|
||||||
TAG_LMARK, /* mark for Lua closures */
|
|
||||||
TAG_CMARK /* mark for C closures */
|
|
||||||
|
|
||||||
} lua_Type;
|
|
||||||
|
|
||||||
/* tags for values visible from Lua == first user-created tag */
|
/* tags for values visible from Lua == first user-created tag */
|
||||||
#define NUM_TAGS 7
|
#define NUM_TAGS 6
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* check whether `t' is a mark */
|
||||||
** check whether `t' is a mark
|
#define is_T_MARK(t) ((t) == LUA_TMARK)
|
||||||
*/
|
|
||||||
#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
|
|
||||||
|
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
struct TString *ts; /* TAG_STRING, TAG_USERDATA */
|
struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */
|
||||||
struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_CMARK */
|
struct Closure *cl; /* LUA_TFUNCTION */
|
||||||
struct Hash *a; /* TAG_TABLE */
|
struct Hash *a; /* LUA_TTABLE */
|
||||||
struct CallInfo *i; /* TAG_LMARK */
|
struct CallInfo *i; /* LUA_TLMARK */
|
||||||
Number n; /* TAG_NUMBER */
|
Number n; /* LUA_TNUMBER */
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
|
|
||||||
@@ -80,7 +63,7 @@ typedef union {
|
|||||||
|
|
||||||
|
|
||||||
typedef struct lua_TObject {
|
typedef struct lua_TObject {
|
||||||
lua_Type ttype;
|
int ttype;
|
||||||
Value value;
|
Value value;
|
||||||
} TObject;
|
} TObject;
|
||||||
|
|
||||||
@@ -88,11 +71,18 @@ typedef struct lua_TObject {
|
|||||||
/*
|
/*
|
||||||
** String headers for string table
|
** 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 {
|
typedef struct TString {
|
||||||
union {
|
union {
|
||||||
struct { /* for strings */
|
struct { /* for strings */
|
||||||
unsigned long hash;
|
unsigned long hash;
|
||||||
size_t len;
|
|
||||||
int constindex; /* hint to reuse constants */
|
int constindex; /* hint to reuse constants */
|
||||||
} s;
|
} s;
|
||||||
struct { /* for userdata */
|
struct { /* for userdata */
|
||||||
@@ -100,9 +90,10 @@ typedef struct TString {
|
|||||||
void *value;
|
void *value;
|
||||||
} d;
|
} d;
|
||||||
} u;
|
} u;
|
||||||
|
size_t len;
|
||||||
struct TString *nexthash; /* chain for hash table */
|
struct TString *nexthash; /* chain for hash table */
|
||||||
unsigned char marked;
|
int marked;
|
||||||
char str[1]; /* variable length string!! must be the last field! */
|
char str[TSPACK]; /* variable length string!! must be the last field! */
|
||||||
} TString;
|
} TString;
|
||||||
|
|
||||||
|
|
||||||
@@ -116,14 +107,16 @@ typedef struct Proto {
|
|||||||
int nkstr; /* size of `kstr' */
|
int nkstr; /* size of `kstr' */
|
||||||
struct Proto **kproto; /* functions defined inside the function */
|
struct Proto **kproto; /* functions defined inside the function */
|
||||||
int nkproto; /* size of `kproto' */
|
int nkproto; /* size of `kproto' */
|
||||||
Instruction *code; /* ends with opcode ENDCODE */
|
Instruction *code;
|
||||||
int numparams;
|
int ncode; /* size of `code'; when 0 means an incomplete `Proto' */
|
||||||
int is_vararg;
|
short numparams;
|
||||||
int maxstacksize;
|
short is_vararg;
|
||||||
|
short maxstacksize;
|
||||||
|
short marked;
|
||||||
struct Proto *next;
|
struct Proto *next;
|
||||||
int marked;
|
|
||||||
/* debug information */
|
/* debug information */
|
||||||
int *lineinfo; /* map from opcodes to source lines */
|
int *lineinfo; /* map from opcodes to source lines */
|
||||||
|
int nlineinfo; /* size of `lineinfo' */
|
||||||
int nlocvars;
|
int nlocvars;
|
||||||
struct LocVar *locvars; /* information about local variables */
|
struct LocVar *locvars; /* information about local variables */
|
||||||
int lineDefined;
|
int lineDefined;
|
||||||
@@ -148,11 +141,15 @@ typedef struct Closure {
|
|||||||
} f;
|
} f;
|
||||||
struct Closure *next;
|
struct Closure *next;
|
||||||
struct Closure *mark; /* marked closures (point to itself when not marked) */
|
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];
|
TObject upvalue[1];
|
||||||
} Closure;
|
} Closure;
|
||||||
|
|
||||||
|
|
||||||
|
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
|
||||||
|
|
||||||
|
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
TObject key;
|
TObject key;
|
||||||
TObject val;
|
TObject val;
|
||||||
@@ -187,10 +184,12 @@ typedef struct CallInfo {
|
|||||||
} CallInfo;
|
} CallInfo;
|
||||||
|
|
||||||
|
|
||||||
extern const char *const luaO_typenames[];
|
|
||||||
extern const TObject luaO_nilobject;
|
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);
|
lint32 luaO_power2 (lint32 n);
|
||||||
char *luaO_openspace (lua_State *L, size_t n);
|
char *luaO_openspace (lua_State *L, size_t n);
|
||||||
|
|||||||
@@ -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
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** 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_PUSHNIL,/* U - nil_1-nil_u */
|
||||||
OP_POP,/* U a_u-a_1 - */
|
OP_POP,/* U a_u-a_1 - */
|
||||||
|
|
||||||
OP_PUSHINT,/* S - (Number)s */
|
OP_PUSHINT,/* S - (Number)s */
|
||||||
OP_PUSHSTRING,/* K - KSTR[k] */
|
OP_PUSHSTRING,/* K - KSTR[k] */
|
||||||
OP_PUSHNUM,/* N - KNUM[n] */
|
OP_PUSHNUM,/* N - KNUM[n] */
|
||||||
OP_PUSHNEGNUM,/* N - -KNUM[n] */
|
OP_PUSHNEGNUM,/* N - -KNUM[n] */
|
||||||
|
|||||||
18
lparser.c
18
lparser.c
@@ -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
|
** LL(1) Parser and code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -60,13 +60,13 @@ static void next (LexState *ls) {
|
|||||||
ls->lookahead.token = TK_EOS; /* and discharge it */
|
ls->lookahead.token = TK_EOS; /* and discharge it */
|
||||||
}
|
}
|
||||||
else
|
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) {
|
static void lookahead (LexState *ls) {
|
||||||
LUA_ASSERT(ls->lookahead.token == TK_EOS, "two look-aheads");
|
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;
|
f->source = ls->source;
|
||||||
fs->pc = 0;
|
fs->pc = 0;
|
||||||
fs->lasttarget = 0;
|
fs->lasttarget = 0;
|
||||||
fs->nlineinfo = 0;
|
|
||||||
fs->lastline = 0;
|
fs->lastline = 0;
|
||||||
fs->jlt = NO_JUMP;
|
fs->jlt = NO_JUMP;
|
||||||
f->code = NULL;
|
f->code = NULL;
|
||||||
@@ -337,8 +336,9 @@ static void close_func (LexState *ls) {
|
|||||||
luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
|
luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
|
||||||
removelocalvars(ls, fs->nactloc);
|
removelocalvars(ls, fs->nactloc);
|
||||||
luaM_reallocvector(L, f->locvars, f->nlocvars, LocVar);
|
luaM_reallocvector(L, f->locvars, f->nlocvars, LocVar);
|
||||||
luaM_reallocvector(L, f->lineinfo, fs->nlineinfo+1, int);
|
luaM_reallocvector(L, f->lineinfo, f->nlineinfo+1, int);
|
||||||
f->lineinfo[fs->nlineinfo] = MAX_INT; /* end flag */
|
f->lineinfo[f->nlineinfo++] = MAX_INT; /* end flag */
|
||||||
|
luaF_protook(L, f, fs->pc); /* proto is ok now */
|
||||||
ls->fs = fs->prev;
|
ls->fs = fs->prev;
|
||||||
LUA_ASSERT(fs->bl == NULL, "wrong list end");
|
LUA_ASSERT(fs->bl == NULL, "wrong list end");
|
||||||
}
|
}
|
||||||
@@ -859,8 +859,8 @@ static void fornum (LexState *ls, TString *varname) {
|
|||||||
else
|
else
|
||||||
luaK_code1(fs, OP_PUSHINT, 1); /* default step */
|
luaK_code1(fs, OP_PUSHINT, 1); /* default step */
|
||||||
new_localvar(ls, varname, 0);
|
new_localvar(ls, varname, 0);
|
||||||
new_localvarstr(ls, "*limit*", 1);
|
new_localvarstr(ls, "(limit)", 1);
|
||||||
new_localvarstr(ls, "*step*", 2);
|
new_localvarstr(ls, "(step)", 2);
|
||||||
forbody(ls, 3, OP_FORPREP, OP_FORLOOP);
|
forbody(ls, 3, OP_FORPREP, OP_FORLOOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -876,7 +876,7 @@ static void forlist (LexState *ls, TString *indexname) {
|
|||||||
"`in' expected");
|
"`in' expected");
|
||||||
next(ls); /* skip `in' */
|
next(ls); /* skip `in' */
|
||||||
exp1(ls); /* table */
|
exp1(ls); /* table */
|
||||||
new_localvarstr(ls, "*table*", 0);
|
new_localvarstr(ls, "(table)", 0);
|
||||||
new_localvar(ls, indexname, 1);
|
new_localvar(ls, indexname, 1);
|
||||||
new_localvar(ls, valname, 2);
|
new_localvar(ls, valname, 2);
|
||||||
forbody(ls, 3, OP_LFORPREP, OP_LFORLOOP);
|
forbody(ls, 3, OP_LFORPREP, OP_LFORLOOP);
|
||||||
|
|||||||
@@ -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
|
** LL(1) Parser and code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -44,11 +44,10 @@ typedef struct FuncState {
|
|||||||
int pc; /* next position to code */
|
int pc; /* next position to code */
|
||||||
int lasttarget; /* `pc' of last `jump target' */
|
int lasttarget; /* `pc' of last `jump target' */
|
||||||
int jlt; /* list of jumps to `lasttarget' */
|
int jlt; /* list of jumps to `lasttarget' */
|
||||||
int stacklevel; /* number of values on activation register */
|
short stacklevel; /* number of values on activation register */
|
||||||
int nactloc; /* number of active local variables */
|
short nactloc; /* number of active local variables */
|
||||||
int nupvalues; /* number of upvalues */
|
short nupvalues; /* number of upvalues */
|
||||||
int lastline; /* line where last `lineinfo' was generated */
|
int lastline; /* line where last `lineinfo' was generated */
|
||||||
int nlineinfo; /* index of next `lineinfo' to be generated */
|
|
||||||
struct Breaklabel *bl; /* chain of breakable blocks */
|
struct Breaklabel *bl; /* chain of breakable blocks */
|
||||||
expdesc upvalues[MAXUPVALUES]; /* upvalues */
|
expdesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||||
int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
|
int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
|
||||||
|
|||||||
72
lstate.c
72
lstate.c
@@ -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
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -19,8 +19,8 @@
|
|||||||
#include "ltm.h"
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef LUA_DEBUG
|
||||||
extern lua_State *lua_state;
|
static lua_State *lua_state = NULL;
|
||||||
void luaB_opentests (lua_State *L);
|
void luaB_opentests (lua_State *L);
|
||||||
#endif
|
#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);
|
lua_State *L = luaM_new(NULL, lua_State);
|
||||||
if (L == NULL) return NULL; /* memory allocation error */
|
if (L == NULL) return NULL; /* memory allocation error */
|
||||||
L->stack = NULL;
|
L->stack = NULL;
|
||||||
@@ -52,50 +76,44 @@ lua_State *lua_open (int stacksize) {
|
|||||||
L->rootproto = NULL;
|
L->rootproto = NULL;
|
||||||
L->rootcl = NULL;
|
L->rootcl = NULL;
|
||||||
L->roottable = NULL;
|
L->roottable = NULL;
|
||||||
L->IMtable = NULL;
|
L->TMtable = NULL;
|
||||||
L->last_tag = -1;
|
L->last_tag = -1;
|
||||||
L->refArray = NULL;
|
L->refArray = NULL;
|
||||||
L->refSize = 0;
|
L->refSize = 0;
|
||||||
L->refFree = NONEXT;
|
L->refFree = NONEXT;
|
||||||
L->nblocks = 0;
|
L->nblocks = sizeof(lua_State);
|
||||||
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
||||||
L->callhook = NULL;
|
L->callhook = NULL;
|
||||||
L->linehook = NULL;
|
L->linehook = NULL;
|
||||||
L->allowhooks = 1;
|
L->allowhooks = 1;
|
||||||
L->errorJmp = &myErrorJmp;
|
L->errorJmp = NULL;
|
||||||
if (setjmp(myErrorJmp.b) == 0) { /* to catch memory allocation errors */
|
if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
|
||||||
L->gt = luaH_new(L, 10);
|
/* memory allocation error: free partial state */
|
||||||
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 */
|
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
return NULL;
|
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 */
|
luaC_collect(L, 1); /* collect all elements */
|
||||||
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
|
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
|
||||||
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
|
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
|
||||||
LUA_ASSERT(L->roottable == NULL, "list should be empty");
|
LUA_ASSERT(L->roottable == NULL, "list should be empty");
|
||||||
luaS_freeall(L);
|
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->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);
|
luaM_free(L, L->refArray);
|
||||||
|
L->nblocks -= (L->Mbuffsize)*sizeof(char);
|
||||||
luaM_free(L, L->Mbuffer);
|
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);
|
luaM_free(L, L);
|
||||||
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
||||||
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
||||||
|
|||||||
21
lstate.h
21
lstate.h
@@ -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
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,8 +7,6 @@
|
|||||||
#ifndef lstate_h
|
#ifndef lstate_h
|
||||||
#define lstate_h
|
#define lstate_h
|
||||||
|
|
||||||
#include <setjmp.h>
|
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "luadebug.h"
|
#include "luadebug.h"
|
||||||
@@ -33,15 +31,8 @@ struct Ref {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
struct lua_longjmp; /* defined in ldo.c */
|
||||||
** chain list of long jumps
|
struct TM; /* defined in ltm.h */
|
||||||
*/
|
|
||||||
struct lua_longjmp {
|
|
||||||
jmp_buf b;
|
|
||||||
struct lua_longjmp *previous;
|
|
||||||
volatile int status; /* error code */
|
|
||||||
StkId base;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct stringtable {
|
typedef struct stringtable {
|
||||||
@@ -69,13 +60,13 @@ struct lua_State {
|
|||||||
stringtable strt; /* hash table for strings */
|
stringtable strt; /* hash table for strings */
|
||||||
stringtable udt; /* hash table for udata */
|
stringtable udt; /* hash table for udata */
|
||||||
Hash *gt; /* table for globals */
|
Hash *gt; /* table for globals */
|
||||||
struct IM *IMtable; /* table for tag methods */
|
struct TM *TMtable; /* table for tag methods */
|
||||||
int last_tag; /* last used tag in IMtable */
|
int last_tag; /* last used tag in TMtable */
|
||||||
struct Ref *refArray; /* locked objects */
|
struct Ref *refArray; /* locked objects */
|
||||||
int refSize; /* size of refArray */
|
int refSize; /* size of refArray */
|
||||||
int refFree; /* list of free positions in refArray */
|
int refFree; /* list of free positions in refArray */
|
||||||
unsigned long GCthreshold;
|
unsigned long GCthreshold;
|
||||||
unsigned long nblocks; /* number of `blocks' currently allocated */
|
unsigned long nblocks; /* number of `bytes' currently allocated */
|
||||||
lua_Hook callhook;
|
lua_Hook callhook;
|
||||||
lua_Hook linehook;
|
lua_Hook linehook;
|
||||||
int allowhooks;
|
int allowhooks;
|
||||||
|
|||||||
53
lstring.c
53
lstring.c
@@ -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)
|
** String table (keeps all strings handled by Lua)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -15,10 +15,20 @@
|
|||||||
#include "lstring.h"
|
#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) {
|
void luaS_init (lua_State *L) {
|
||||||
L->strt.hash = luaM_newvector(L, 1, TString *);
|
L->strt.hash = luaM_newvector(L, 1, TString *);
|
||||||
L->udt.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.size = L->udt.size = 1;
|
||||||
L->strt.nuse = L->udt.nuse = 0;
|
L->strt.nuse = L->udt.nuse = 0;
|
||||||
L->strt.hash[0] = L->udt.hash[0] = NULL;
|
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) {
|
void luaS_freeall (lua_State *L) {
|
||||||
LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
|
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);
|
luaM_free(L, L->strt.hash);
|
||||||
LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
|
LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
|
||||||
luaM_free(L, L->udt.hash);
|
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);
|
luaM_free(L, tb->hash);
|
||||||
|
L->nblocks += (newsize - tb->size)*sizeof(TString *);
|
||||||
tb->size = newsize;
|
tb->size = newsize;
|
||||||
tb->hash = newhash;
|
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) {
|
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||||
unsigned long h = hash_s(str, l);
|
unsigned long h = hash_s(str, l);
|
||||||
int h1 = h&(L->strt.size-1);
|
int h1 = h & (L->strt.size-1);
|
||||||
TString *ts;
|
TString *ts;
|
||||||
for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
|
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;
|
return ts;
|
||||||
}
|
}
|
||||||
/* not found */
|
/* not found */
|
||||||
ts = (TString *)luaM_malloc(L, sizeof(TString)+(lint32)l*sizeof(char));
|
ts = (TString *)luaM_malloc(L, sizestring(l));
|
||||||
ts->marked = 0;
|
ts->marked = 0;
|
||||||
ts->nexthash = NULL;
|
ts->nexthash = NULL;
|
||||||
ts->u.s.len = l;
|
ts->len = l;
|
||||||
ts->u.s.hash = h;
|
ts->u.s.hash = h;
|
||||||
ts->u.s.constindex = 0;
|
ts->u.s.constindex = 0;
|
||||||
memcpy(ts->str, str, l);
|
memcpy(ts->str, str, l);
|
||||||
ts->str[l] = 0; /* ending 0 */
|
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 */
|
newentry(L, &L->strt, ts, h1); /* insert it on table */
|
||||||
return ts;
|
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) {
|
TString *luaS_createudata (lua_State *L, void *udata, int tag) {
|
||||||
unsigned long h = IntPoint(udata);
|
int h1 = IntPoint(udata) & (L->udt.size-1);
|
||||||
int h1 = h&(L->udt.size-1);
|
|
||||||
TString *ts;
|
TString *ts;
|
||||||
for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
|
for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
|
||||||
if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
|
if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
/* not found */
|
/* not found */
|
||||||
ts = luaM_new(L, TString);
|
ts = luaS_newudata(L, 0, udata);
|
||||||
ts->marked = 0;
|
if (tag != LUA_ANYTAG)
|
||||||
ts->nexthash = NULL;
|
ts->u.d.tag = tag;
|
||||||
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 */
|
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
** String table (keep all strings handled by Lua)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -20,12 +20,13 @@
|
|||||||
#define RESERVEDMARK 3
|
#define RESERVEDMARK 3
|
||||||
|
|
||||||
|
|
||||||
#define gcsizestring(L, l) numblocks(L, 0, sizeof(TString)+l)
|
#define sizestring(l) ((long)sizeof(TString) + \
|
||||||
#define gcsizeudata gcsizestring(L, 0)
|
((long)(l+1)-TSPACK)*(long)sizeof(char))
|
||||||
|
|
||||||
|
|
||||||
void luaS_init (lua_State *L);
|
void luaS_init (lua_State *L);
|
||||||
void luaS_resize (lua_State *L, stringtable *tb, int newsize);
|
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);
|
TString *luaS_createudata (lua_State *L, void *udata, int tag);
|
||||||
void luaS_freeall (lua_State *L);
|
void luaS_freeall (lua_State *L);
|
||||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||||
|
|||||||
@@ -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
|
** Standard library for string operations and pattern-matching
|
||||||
** See Copyright Notice in lua.h
|
** 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, '"');
|
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
|
#define MAX_ITEM 512
|
||||||
/* maximum size of each format specification (such as '%-099.99d') */
|
/* maximum size of each format specification (such as '%-099.99d') */
|
||||||
#define MAX_FORMAT 20
|
#define MAX_FORMAT 20
|
||||||
@@ -616,6 +616,6 @@ static const struct luaL_reg strlib[] = {
|
|||||||
/*
|
/*
|
||||||
** Open string library
|
** Open string library
|
||||||
*/
|
*/
|
||||||
void lua_strlibopen (lua_State *L) {
|
LUALIB_API void lua_strlibopen (lua_State *L) {
|
||||||
luaL_openl(L, strlib);
|
luaL_openl(L, strlib);
|
||||||
}
|
}
|
||||||
|
|||||||
50
ltable.c
50
ltable.c
@@ -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)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -27,11 +27,11 @@
|
|||||||
#include "ltable.h"
|
#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) {
|
Node *luaH_mainposition (const Hash *t, const TObject *key) {
|
||||||
unsigned long h;
|
unsigned long h;
|
||||||
switch (ttype(key)) {
|
switch (ttype(key)) {
|
||||||
case TAG_NUMBER:
|
case LUA_TNUMBER:
|
||||||
h = (unsigned long)(long)nvalue(key);
|
h = (unsigned long)(long)nvalue(key);
|
||||||
break;
|
break;
|
||||||
case TAG_STRING:
|
case LUA_TSTRING:
|
||||||
h = tsvalue(key)->u.s.hash;
|
h = tsvalue(key)->u.s.hash;
|
||||||
break;
|
break;
|
||||||
case TAG_USERDATA:
|
case LUA_TUSERDATA:
|
||||||
h = IntPoint(tsvalue(key));
|
h = IntPoint(tsvalue(key));
|
||||||
break;
|
break;
|
||||||
case TAG_TABLE:
|
case LUA_TTABLE:
|
||||||
h = IntPoint(hvalue(key));
|
h = IntPoint(hvalue(key));
|
||||||
break;
|
break;
|
||||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
case LUA_TFUNCTION:
|
||||||
h = IntPoint(clvalue(key));
|
h = IntPoint(clvalue(key));
|
||||||
break;
|
break;
|
||||||
default:
|
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) {
|
const TObject *luaH_getnum (const Hash *t, Number key) {
|
||||||
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
|
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
|
||||||
do {
|
do {
|
||||||
if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key)
|
if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key)
|
||||||
return &n->val;
|
return &n->val;
|
||||||
n = n->next;
|
n = n->next;
|
||||||
} while (n);
|
} while (n);
|
||||||
@@ -96,7 +96,7 @@ const TObject *luaH_getnum (const Hash *t, Number key) {
|
|||||||
const TObject *luaH_getstr (const Hash *t, TString *key) {
|
const TObject *luaH_getstr (const Hash *t, TString *key) {
|
||||||
Node *n = &t->node[key->u.s.hash&(t->size-1)];
|
Node *n = &t->node[key->u.s.hash&(t->size-1)];
|
||||||
do {
|
do {
|
||||||
if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key)
|
if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key)
|
||||||
return &n->val;
|
return &n->val;
|
||||||
n = n->next;
|
n = n->next;
|
||||||
} while (n);
|
} 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) {
|
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||||
switch (ttype(key)) {
|
switch (ttype(key)) {
|
||||||
case TAG_NUMBER: return luaH_getnum(t, nvalue(key));
|
case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
|
||||||
case TAG_STRING: return luaH_getstr(t, tsvalue(key));
|
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
|
||||||
default: return luaH_getany(L, t, 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) {
|
Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
|
||||||
int i;
|
int i;
|
||||||
if (ttype(key) == TAG_NIL)
|
if (ttype(key) == LUA_TNIL)
|
||||||
i = 0; /* first iteration */
|
i = 0; /* first iteration */
|
||||||
else {
|
else {
|
||||||
const TObject *v = luaH_get(L, t, key);
|
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++) {
|
for (; i<t->size; i++) {
|
||||||
Node *n = node(t, i);
|
Node *n = node(t, i);
|
||||||
if (ttype(val(n)) != TAG_NIL)
|
if (ttype(val(n)) != LUA_TNIL)
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
return NULL; /* no more elements */
|
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.
|
** hash, change `key' for a number with the same hash.
|
||||||
*/
|
*/
|
||||||
void luaH_remove (Hash *t, TObject *key) {
|
void luaH_remove (Hash *t, TObject *key) {
|
||||||
if (ttype(key) == TAG_NUMBER ||
|
if (ttype(key) == LUA_TNUMBER ||
|
||||||
(ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30))
|
(ttype(key) == LUA_TSTRING && tsvalue(key)->len <= 30))
|
||||||
return; /* do not remove numbers nor small strings */
|
return; /* do not remove numbers nor small strings */
|
||||||
else {
|
else {
|
||||||
/* try to find a number `n' with the same hash as `key' */
|
/* 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) */
|
return; /* give up; (to avoid overflow) */
|
||||||
n += t->size;
|
n += t->size;
|
||||||
}
|
}
|
||||||
ttype(key) = TAG_NUMBER;
|
ttype(key) = LUA_TNUMBER;
|
||||||
nvalue(key) = n;
|
nvalue(key) = n;
|
||||||
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
|
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");
|
lua_error(L, "table overflow");
|
||||||
t->node = luaM_newvector(L, size, Node);
|
t->node = luaM_newvector(L, size, Node);
|
||||||
for (i=0; i<(int)size; i++) {
|
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;
|
t->node[i].next = NULL;
|
||||||
}
|
}
|
||||||
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
|
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
|
||||||
@@ -200,7 +200,7 @@ static int numuse (const Hash *t) {
|
|||||||
int realuse = 0;
|
int realuse = 0;
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<size; i++) {
|
for (i=0; i<size; i++) {
|
||||||
if (ttype(&v[i].val) != TAG_NIL)
|
if (ttype(&v[i].val) != LUA_TNIL)
|
||||||
realuse++;
|
realuse++;
|
||||||
}
|
}
|
||||||
return realuse;
|
return realuse;
|
||||||
@@ -222,7 +222,7 @@ static void rehash (lua_State *L, Hash *t) {
|
|||||||
setnodevector(L, t, oldsize);
|
setnodevector(L, t, oldsize);
|
||||||
for (i=0; i<oldsize; i++) {
|
for (i=0; i<oldsize; i++) {
|
||||||
Node *old = nold+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;
|
*luaH_set(L, t, &old->key) = old->val;
|
||||||
}
|
}
|
||||||
luaM_free(L, nold); /* free old array */
|
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;
|
else n = n->next;
|
||||||
} while (n);
|
} while (n);
|
||||||
/* `key' not found; must insert it */
|
/* `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 */
|
Node *othern; /* main position of colliding node */
|
||||||
n = t->firstfree; /* get a free place */
|
n = t->firstfree; /* get a free place */
|
||||||
/* is colliding node out of its main position? (can only happens if
|
/* 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;
|
mp->key = *key;
|
||||||
for (;;) { /* correct `firstfree' */
|
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 */
|
return &mp->val; /* OK; table still has a free place */
|
||||||
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
||||||
else (t->firstfree)--;
|
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 *luaH_setint (lua_State *L, Hash *t, int key) {
|
||||||
TObject index;
|
TObject index;
|
||||||
ttype(&index) = TAG_NUMBER;
|
ttype(&index) = LUA_TNUMBER;
|
||||||
nvalue(&index) = key;
|
nvalue(&index) = key;
|
||||||
return luaH_set(L, t, &index);
|
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) {
|
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
|
||||||
TObject *value, index;
|
TObject *value, index;
|
||||||
ttype(&index) = TAG_STRING;
|
ttype(&index) = LUA_TSTRING;
|
||||||
tsvalue(&index) = key;
|
tsvalue(&index) = key;
|
||||||
value = luaH_set(L, t, &index);
|
value = luaH_set(L, t, &index);
|
||||||
ttype(value) = TAG_NUMBER;
|
ttype(value) = LUA_TNUMBER;
|
||||||
nvalue(value) = val;
|
nvalue(value) = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
113
ltests.c
113
ltests.c
@@ -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
|
** Internal Module for Debugging of the Lua Implementation
|
||||||
** See Copyright Notice in lua.h
|
** 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));
|
sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
|
||||||
switch ((enum Mode)luaK_opproperties[o].mode) {
|
switch ((enum Mode)luaK_opproperties[o].mode) {
|
||||||
case iO:
|
case iO:
|
||||||
sprintf(buff+8, "%s", name);
|
sprintf(buff+8, "%-12s", name);
|
||||||
break;
|
break;
|
||||||
case iU:
|
case iU:
|
||||||
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
|
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
|
||||||
@@ -93,7 +93,8 @@ static int listcode (lua_State *L) {
|
|||||||
int pc;
|
int pc;
|
||||||
Proto *p;
|
Proto *p;
|
||||||
int res;
|
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;
|
p = clvalue(luaA_index(L, 1))->f.l;
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
setnameval(L, "maxstack", p->maxstacksize);
|
setnameval(L, "maxstack", p->maxstacksize);
|
||||||
@@ -111,7 +112,8 @@ static int listcode (lua_State *L) {
|
|||||||
static int liststrings (lua_State *L) {
|
static int liststrings (lua_State *L) {
|
||||||
Proto *p;
|
Proto *p;
|
||||||
int i;
|
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;
|
p = clvalue(luaA_index(L, 1))->f.l;
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
for (i=0; i<p->nkstr; i++) {
|
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 pc = luaL_check_int(L, 2) - 1;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
const char *name;
|
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;
|
p = clvalue(luaA_index(L, 1))->f.l;
|
||||||
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
@@ -177,12 +180,12 @@ static int mem_query (lua_State *L) {
|
|||||||
|
|
||||||
static int hash_query (lua_State *L) {
|
static int hash_query (lua_State *L) {
|
||||||
if (lua_isnull(L, 2)) {
|
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);
|
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Hash *t;
|
Hash *t;
|
||||||
luaL_checktype(L, 2, "table");
|
luaL_checktype(L, 2, LUA_TTABLE);
|
||||||
t = hvalue(luaA_index(L, 2));
|
t = hvalue(luaA_index(L, 2));
|
||||||
lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
|
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) {
|
static int table_query (lua_State *L) {
|
||||||
const Hash *t;
|
const Hash *t;
|
||||||
int i = luaL_opt_int(L, 2, -1);
|
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));
|
t = hvalue(luaA_index(L, 1));
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
lua_pushnumber(L, t->size);
|
lua_pushnumber(L, t->size);
|
||||||
@@ -226,7 +229,7 @@ static int string_query (lua_State *L) {
|
|||||||
TString *ts;
|
TString *ts;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
|
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
|
||||||
ttype(L->top) = TAG_STRING;
|
ttype(L->top) = LUA_TSTRING;
|
||||||
tsvalue(L->top) = ts;
|
tsvalue(L->top) = ts;
|
||||||
incr_top;
|
incr_top;
|
||||||
n++;
|
n++;
|
||||||
@@ -238,7 +241,7 @@ static int string_query (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int tref (lua_State *L) {
|
static int tref (lua_State *L) {
|
||||||
luaL_checktype(L, 1, "any");
|
luaL_checkany(L, 1);
|
||||||
lua_pushvalue(L, 1);
|
lua_pushvalue(L, 1);
|
||||||
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
|
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
|
||||||
return 1;
|
return 1;
|
||||||
@@ -257,12 +260,15 @@ static int unref (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int newuserdata (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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int udataval (lua_State *L) {
|
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));
|
lua_pushnumber(L, (int)lua_touserdata(L, 1));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -290,7 +296,7 @@ static int loadlib (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int closestate (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));
|
lua_close((lua_State *)lua_touserdata(L, 1));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -299,7 +305,7 @@ static int doremote (lua_State *L) {
|
|||||||
lua_State *L1;
|
lua_State *L1;
|
||||||
const char *code = luaL_check_string(L, 2);
|
const char *code = luaL_check_string(L, 2);
|
||||||
int status;
|
int status;
|
||||||
luaL_checktype(L, 1, "userdata");
|
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||||
L1 = (lua_State *)lua_touserdata(L, 1);
|
L1 = (lua_State *)lua_touserdata(L, 1);
|
||||||
status = lua_dostring(L1, code);
|
status = lua_dostring(L1, code);
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
@@ -316,8 +322,12 @@ static int doremote (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int settagmethod (lua_State *L) {
|
static int settagmethod (lua_State *L) {
|
||||||
luaL_checktype(L, 3, "any");
|
int tag = luaL_check_int(L, 1);
|
||||||
lua_settagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,6 +396,45 @@ static int testC (lua_State *L) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
const char *inst = getname;
|
const char *inst = getname;
|
||||||
if EQ("") return 0;
|
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") {
|
else if EQ("return") {
|
||||||
return getnum;
|
return getnum;
|
||||||
}
|
}
|
||||||
@@ -410,19 +459,43 @@ static int testC (lua_State *L) {
|
|||||||
else if EQ("insert") {
|
else if EQ("insert") {
|
||||||
lua_insert(L, getnum);
|
lua_insert(L, getnum);
|
||||||
}
|
}
|
||||||
|
else if EQ("gettable") {
|
||||||
|
lua_gettable(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("settable") {
|
||||||
|
lua_settable(L, getnum);
|
||||||
|
}
|
||||||
else if EQ("next") {
|
else if EQ("next") {
|
||||||
lua_next(L, -2);
|
lua_next(L, -2);
|
||||||
}
|
}
|
||||||
else if EQ("concat") {
|
else if EQ("concat") {
|
||||||
lua_concat(L, getnum);
|
lua_concat(L, getnum);
|
||||||
}
|
}
|
||||||
else if EQ("call") {
|
else if EQ("rawcall") {
|
||||||
int narg = getnum;
|
int narg = getnum;
|
||||||
int nres = getnum;
|
int nres = getnum;
|
||||||
lua_rawcall(L, narg, nres);
|
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") {
|
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);
|
else luaL_verror(L, "unknown instruction %.30s", buff);
|
||||||
}
|
}
|
||||||
|
|||||||
100
ltm.c
100
ltm.c
@@ -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
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
#include "ltm.h"
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
const char *const luaT_eventname[] = { /* ORDER IM */
|
const char *const luaT_eventname[] = { /* ORDER TM */
|
||||||
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
|
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
|
||||||
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
|
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
|
||||||
"le", "gt", "ge", /* deprecated options!! */
|
"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) {
|
static int luaI_checkevent (lua_State *L, const char *name, int t) {
|
||||||
int e = findevent(name);
|
int e = findevent(name);
|
||||||
if (e >= IM_N)
|
if (e >= TM_N)
|
||||||
luaO_verror(L, "event `%.50s' is deprecated", name);
|
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");
|
luaO_verror(L, "event `gc' for tables is deprecated");
|
||||||
if (e < 0)
|
if (e < 0)
|
||||||
luaO_verror(L, "`%.50s' is not a valid event name", name);
|
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
|
* 'placeholder' for "default" fallbacks
|
||||||
*/
|
*/
|
||||||
/* ORDER LUA_T, ORDER IM */
|
/* ORDER LUA_T, ORDER TM */
|
||||||
static const char luaT_validevents[NUM_TAGS][IM_N] = {
|
static const char luaT_validevents[NUM_TAGS][TM_N] = {
|
||||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
|
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
|
||||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
|
{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, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
|
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
|
||||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
|
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
|
||||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
|
{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}, /* TAG_CCLOSURE */
|
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
|
||||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
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) {
|
static void init_entry (lua_State *L, int tag) {
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<IM_N; i++)
|
for (i=0; i<TM_N; i++)
|
||||||
ttype(luaT_getim(L, tag, i)) = TAG_NIL;
|
luaT_gettm(L, tag, i) = NULL;
|
||||||
L->IMtable[tag].collected = NULL;
|
L->TMtable[tag].collected = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaT_init (lua_State *L) {
|
void luaT_init (lua_State *L) {
|
||||||
int t;
|
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;
|
L->last_tag = NUM_TAGS-1;
|
||||||
for (t=0; t<=L->last_tag; t++)
|
for (t=0; t<=L->last_tag; t++)
|
||||||
init_entry(L, t);
|
init_entry(L, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int lua_newtag (lua_State *L) {
|
LUA_API int lua_newtag (lua_State *L) {
|
||||||
luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
|
luaM_growvector(L, L->TMtable, L->last_tag, 1, struct TM,
|
||||||
"tag table overflow", MAX_INT);
|
"tag table overflow", MAX_INT);
|
||||||
|
L->nblocks += sizeof(struct TM);
|
||||||
L->last_tag++;
|
L->last_tag++;
|
||||||
init_entry(L, L->last_tag);
|
init_entry(L, L->last_tag);
|
||||||
return 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) {
|
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);
|
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;
|
int e;
|
||||||
checktag(L, tagto);
|
checktag(L, tagto);
|
||||||
checktag(L, tagfrom);
|
checktag(L, tagfrom);
|
||||||
for (e=0; e<IM_N; e++) {
|
for (e=0; e<TM_N; e++) {
|
||||||
if (luaT_validevent(tagto, 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;
|
return tagto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaT_effectivetag (lua_State *L, const TObject *o) {
|
int luaT_tag (const TObject *o) {
|
||||||
lua_Type t = ttype(o);
|
int t = ttype(o);
|
||||||
switch (t) {
|
switch (t) {
|
||||||
case TAG_USERDATA: {
|
case LUA_TUSERDATA: return tsvalue(o)->u.d.tag;
|
||||||
int tag = tsvalue(o)->u.d.tag;
|
case LUA_TTABLE: return hvalue(o)->htag;
|
||||||
return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
|
default: return t;
|
||||||
}
|
|
||||||
case TAG_TABLE: 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;
|
int e;
|
||||||
e = luaI_checkevent(L, event, t);
|
e = luaI_checkevent(L, event, t);
|
||||||
checktag(L, t);
|
checktag(L, t);
|
||||||
if (luaT_validevent(t, e))
|
if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) {
|
||||||
*L->top = *luaT_getim(L, t,e);
|
clvalue(L->top) = luaT_gettm(L, t, e);
|
||||||
|
ttype(L->top) = LUA_TFUNCTION;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
ttype(L->top) = TAG_NIL;
|
ttype(L->top) = LUA_TNIL;
|
||||||
incr_top;
|
incr_top;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void lua_settagmethod (lua_State *L, int t, const char *event) {
|
LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
|
||||||
TObject temp;
|
int e = luaI_checkevent(L, event, t);
|
||||||
int e;
|
|
||||||
LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
|
|
||||||
"function or nil expected");
|
|
||||||
e = luaI_checkevent(L, event, t);
|
|
||||||
checktag(L, t);
|
checktag(L, t);
|
||||||
if (!luaT_validevent(t, e))
|
if (!luaT_validevent(t, e))
|
||||||
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||||
luaT_eventname[e], luaO_typenames[t],
|
luaT_eventname[e], luaO_typenames[t],
|
||||||
(t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
|
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
|
||||||
: "");
|
" with default tag" : "");
|
||||||
temp = *(L->top - 1);
|
switch (ttype(L->top - 1)) {
|
||||||
*(L->top - 1) = *luaT_getim(L, t,e);
|
case LUA_TNIL:
|
||||||
*luaT_getim(L, t, e) = temp;
|
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
53
ltm.h
@@ -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
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,43 +13,46 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* WARNING: if you change the order of this enumeration,
|
* WARNING: if you change the order of this enumeration,
|
||||||
* grep "ORDER IM"
|
* grep "ORDER TM"
|
||||||
*/
|
*/
|
||||||
typedef enum {
|
typedef enum {
|
||||||
IM_GETTABLE = 0,
|
TM_GETTABLE = 0,
|
||||||
IM_SETTABLE,
|
TM_SETTABLE,
|
||||||
IM_INDEX,
|
TM_INDEX,
|
||||||
IM_GETGLOBAL,
|
TM_GETGLOBAL,
|
||||||
IM_SETGLOBAL,
|
TM_SETGLOBAL,
|
||||||
IM_ADD,
|
TM_ADD,
|
||||||
IM_SUB,
|
TM_SUB,
|
||||||
IM_MUL,
|
TM_MUL,
|
||||||
IM_DIV,
|
TM_DIV,
|
||||||
IM_POW,
|
TM_POW,
|
||||||
IM_UNM,
|
TM_UNM,
|
||||||
IM_LT,
|
TM_LT,
|
||||||
IM_CONCAT,
|
TM_CONCAT,
|
||||||
IM_GC,
|
TM_GC,
|
||||||
IM_FUNCTION,
|
TM_FUNCTION,
|
||||||
IM_N /* number of elements in the enum */
|
TM_N /* number of elements in the enum */
|
||||||
} IMS;
|
} TMS;
|
||||||
|
|
||||||
|
|
||||||
struct IM {
|
struct TM {
|
||||||
TObject int_method[IM_N];
|
Closure *method[TM_N];
|
||||||
TString *collected; /* list of G. collected udata with this tag */
|
TString *collected; /* list of garbage-collected udata with this tag */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
|
#define luaT_gettm(L,tag,event) (L->TMtable[tag].method[event])
|
||||||
#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(L, o),(e)))
|
#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[];
|
extern const char *const luaT_eventname[];
|
||||||
|
|
||||||
|
|
||||||
void luaT_init (lua_State *L);
|
void luaT_init (lua_State *L);
|
||||||
void luaT_realtag (lua_State *L, int tag);
|
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 */
|
int luaT_validevent (int t, int e); /* used by compatibility module */
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
23
lua.c
23
lua.c
@@ -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
|
** Lua stand-alone interpreter
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -16,8 +16,7 @@
|
|||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
lua_State *lua_state = NULL;
|
static lua_State *L = NULL;
|
||||||
#define L lua_state
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef PROMPT
|
#ifndef PROMPT
|
||||||
@@ -49,18 +48,14 @@ static lua_Hook old_linehook = NULL;
|
|||||||
static lua_Hook old_callhook = NULL;
|
static lua_Hook old_callhook = NULL;
|
||||||
|
|
||||||
|
|
||||||
#ifdef USERINIT
|
|
||||||
extern void USERINIT (void);
|
|
||||||
#else
|
|
||||||
#define USERINIT userinit
|
|
||||||
static void userinit (void) {
|
static void userinit (void) {
|
||||||
lua_baselibopen(L);
|
lua_baselibopen(L);
|
||||||
lua_iolibopen(L);
|
lua_iolibopen(L);
|
||||||
lua_strlibopen(L);
|
lua_strlibopen(L);
|
||||||
lua_mathlibopen(L);
|
lua_mathlibopen(L);
|
||||||
lua_dblibopen(L);
|
lua_dblibopen(L);
|
||||||
|
/* add your libraries here */
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
static handler lreset (void) {
|
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 */
|
res = f(L, name); /* dostring | dofile */
|
||||||
lua_settop(L, top); /* remove eventual results */
|
lua_settop(L, top); /* remove eventual results */
|
||||||
signal(SIGINT, h); /* restore old action */
|
signal(SIGINT, h); /* restore old action */
|
||||||
|
/* Lua gives no message in such cases, so lua.c provides one */
|
||||||
if (res == LUA_ERRMEM) {
|
if (res == LUA_ERRMEM) {
|
||||||
/* Lua gives no message in such case, so lua.c provides one */
|
|
||||||
fprintf(stderr, "lua: memory allocation error\n");
|
fprintf(stderr, "lua: memory allocation error\n");
|
||||||
}
|
}
|
||||||
|
else if (res == LUA_ERRERR)
|
||||||
|
fprintf(stderr, "lua: error in error message\n");
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +115,7 @@ static void print_message (void) {
|
|||||||
|
|
||||||
|
|
||||||
static void print_version (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;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
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;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
break;
|
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') {
|
if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
|
||||||
int stacksize = atoi(&argv[1][2]);
|
int stacksize = atoi(&argv[1][2]);
|
||||||
if (stacksize <= 0) {
|
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);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
opt->stacksize = stacksize;
|
opt->stacksize = stacksize;
|
||||||
@@ -315,7 +312,7 @@ int main (int argc, char *argv[]) {
|
|||||||
opt.toclose = 0;
|
opt.toclose = 0;
|
||||||
getstacksize(argc, argv, &opt); /* handle option `-s' */
|
getstacksize(argc, argv, &opt); /* handle option `-s' */
|
||||||
L = lua_open(opt.stacksize); /* create state */
|
L = lua_open(opt.stacksize); /* create state */
|
||||||
USERINIT(); /* open libraries */
|
userinit(); /* open libraries */
|
||||||
register_getargs(argv); /* create `getargs' function */
|
register_getargs(argv); /* create `getargs' function */
|
||||||
status = handle_argv(argv+1, &opt);
|
status = handle_argv(argv+1, &opt);
|
||||||
if (opt.toclose)
|
if (opt.toclose)
|
||||||
|
|||||||
166
lua.h
166
lua.h
@@ -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
|
** Lua - An Extensible Extension Language
|
||||||
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||||
** e-mail: lua@tecgraf.puc-rio.br
|
** e-mail: lua@tecgraf.puc-rio.br
|
||||||
@@ -16,23 +16,36 @@
|
|||||||
#include <stddef.h>
|
#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_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
|
||||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||||
|
|
||||||
|
|
||||||
|
/* name of global variable with error handler */
|
||||||
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
|
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
|
||||||
|
|
||||||
|
|
||||||
|
/* pre-defined references */
|
||||||
#define LUA_NOREF (-2)
|
#define LUA_NOREF (-2)
|
||||||
#define LUA_REFNIL (-1)
|
#define LUA_REFNIL (-1)
|
||||||
|
#define LUA_REFREGISTRY 0
|
||||||
|
|
||||||
|
/* pre-defined tags */
|
||||||
#define LUA_ANYTAG (-1)
|
#define LUA_ANYTAG (-1)
|
||||||
#define LUA_NOTAG (-2)
|
#define LUA_NOTAG (-2)
|
||||||
|
|
||||||
|
|
||||||
|
/* option for multiple returns in lua_call */
|
||||||
#define LUA_MULTRET (-1)
|
#define LUA_MULTRET (-1)
|
||||||
|
|
||||||
|
|
||||||
|
/* minimum stack available for a C function */
|
||||||
#define LUA_MINSTACK 20
|
#define LUA_MINSTACK 20
|
||||||
|
|
||||||
|
|
||||||
@@ -41,117 +54,136 @@
|
|||||||
#define LUA_ERRFILE 2
|
#define LUA_ERRFILE 2
|
||||||
#define LUA_ERRSYNTAX 3
|
#define LUA_ERRSYNTAX 3
|
||||||
#define LUA_ERRMEM 4
|
#define LUA_ERRMEM 4
|
||||||
|
#define LUA_ERRERR 5
|
||||||
|
|
||||||
|
|
||||||
typedef struct lua_State lua_State;
|
typedef struct lua_State lua_State;
|
||||||
|
|
||||||
typedef int (*lua_CFunction) (lua_State *L);
|
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
|
** state manipulation
|
||||||
*/
|
*/
|
||||||
lua_State *lua_open (int stacksize);
|
LUA_API lua_State *lua_open (int stacksize);
|
||||||
void lua_close (lua_State *L);
|
LUA_API void lua_close (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** basic stack manipulation
|
** basic stack manipulation
|
||||||
*/
|
*/
|
||||||
int lua_gettop (lua_State *L);
|
LUA_API int lua_gettop (lua_State *L);
|
||||||
void lua_settop (lua_State *L, int index);
|
LUA_API void lua_settop (lua_State *L, int index);
|
||||||
void lua_pushvalue (lua_State *L, int index);
|
LUA_API void lua_pushvalue (lua_State *L, int index);
|
||||||
void lua_remove (lua_State *L, int index);
|
LUA_API void lua_remove (lua_State *L, int index);
|
||||||
void lua_insert (lua_State *L, int index);
|
LUA_API void lua_insert (lua_State *L, int index);
|
||||||
int lua_stackspace (lua_State *L);
|
LUA_API int lua_stackspace (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** access functions (stack -> C)
|
** access functions (stack -> C)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const char *lua_type (lua_State *L, int index);
|
LUA_API int lua_type (lua_State *L, int index);
|
||||||
int lua_isnumber (lua_State *L, int index);
|
LUA_API const char *lua_typename (lua_State *L, int t);
|
||||||
int lua_iscfunction (lua_State *L, int index);
|
LUA_API int lua_isnumber (lua_State *L, int index);
|
||||||
int lua_tag (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);
|
LUA_API int lua_equal (lua_State *L, int index1, int index2);
|
||||||
int lua_lessthan (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);
|
LUA_API double lua_tonumber (lua_State *L, int index);
|
||||||
const char *lua_tostring (lua_State *L, int index);
|
LUA_API const char *lua_tostring (lua_State *L, int index);
|
||||||
size_t lua_strlen (lua_State *L, int index);
|
LUA_API size_t lua_strlen (lua_State *L, int index);
|
||||||
lua_CFunction lua_tocfunction (lua_State *L, int index);
|
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index);
|
||||||
void *lua_touserdata (lua_State *L, int index);
|
LUA_API void *lua_touserdata (lua_State *L, int index);
|
||||||
const void *lua_topointer (lua_State *L, int index);
|
LUA_API const void *lua_topointer (lua_State *L, int index);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** push functions (C -> stack)
|
** push functions (C -> stack)
|
||||||
*/
|
*/
|
||||||
void lua_pushnil (lua_State *L);
|
LUA_API void lua_pushnil (lua_State *L);
|
||||||
void lua_pushnumber (lua_State *L, double n);
|
LUA_API void lua_pushnumber (lua_State *L, double n);
|
||||||
void lua_pushlstring (lua_State *L, const char *s, size_t len);
|
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len);
|
||||||
void lua_pushstring (lua_State *L, const char *s);
|
LUA_API void lua_pushstring (lua_State *L, const char *s);
|
||||||
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
LUA_API 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_pushusertag (lua_State *L, void *u, int tag);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** get functions (Lua -> stack)
|
** get functions (Lua -> stack)
|
||||||
*/
|
*/
|
||||||
void lua_getglobal (lua_State *L, const char *name);
|
LUA_API 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);
|
||||||
void lua_rawget (lua_State *L, int index);
|
LUA_API void lua_rawget (lua_State *L, int index);
|
||||||
void lua_rawgeti (lua_State *L, int index, int n);
|
LUA_API void lua_rawgeti (lua_State *L, int index, int n);
|
||||||
void lua_getglobals (lua_State *L);
|
LUA_API void lua_getglobals (lua_State *L);
|
||||||
void lua_gettagmethod (lua_State *L, int tag, const char *event);
|
LUA_API void lua_gettagmethod (lua_State *L, int tag, const char *event);
|
||||||
|
LUA_API int lua_getref (lua_State *L, int ref);
|
||||||
int lua_getref (lua_State *L, int ref);
|
LUA_API void lua_newtable (lua_State *L);
|
||||||
|
|
||||||
void lua_newtable (lua_State *L);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** set functions (stack -> Lua)
|
** set functions (stack -> Lua)
|
||||||
*/
|
*/
|
||||||
void lua_setglobal (lua_State *L, const char *name);
|
LUA_API void lua_setglobal (lua_State *L, const char *name);
|
||||||
void lua_settable (lua_State *L, int index);
|
LUA_API 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);
|
||||||
void lua_rawseti (lua_State *L, int index, int n);
|
LUA_API void lua_rawseti (lua_State *L, int index, int n);
|
||||||
void lua_setglobals (lua_State *L);
|
LUA_API void lua_setglobals (lua_State *L);
|
||||||
void lua_settagmethod (lua_State *L, int tag, const char *event);
|
LUA_API void lua_settagmethod (lua_State *L, int tag, const char *event);
|
||||||
int lua_ref (lua_State *L, int lock);
|
LUA_API int lua_ref (lua_State *L, int lock);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** "do" functions (run Lua code)
|
** "do" functions (run Lua code)
|
||||||
*/
|
*/
|
||||||
int lua_call (lua_State *L, int nargs, int nresults);
|
LUA_API int lua_call (lua_State *L, int nargs, int nresults);
|
||||||
void lua_rawcall (lua_State *L, int nargs, int nresults);
|
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
|
||||||
int lua_dofile (lua_State *L, const char *filename);
|
LUA_API int lua_dofile (lua_State *L, const char *filename);
|
||||||
int lua_dostring (lua_State *L, const char *str);
|
LUA_API int lua_dostring (lua_State *L, const char *str);
|
||||||
int lua_dobuffer (lua_State *L, const char *buff, size_t size,
|
LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name);
|
||||||
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
|
** miscellaneous functions
|
||||||
*/
|
*/
|
||||||
int lua_newtag (lua_State *L);
|
LUA_API int lua_newtag (lua_State *L);
|
||||||
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||||
void lua_settag (lua_State *L, int tag);
|
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);
|
LUA_API void lua_concat (lua_State *L, int n);
|
||||||
int lua_getn (lua_State *L, int index);
|
|
||||||
|
|
||||||
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_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
|
||||||
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
|
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
|
||||||
|
|
||||||
#define lua_isfunction(L,n) (*lua_type(L,n) == 'f')
|
#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
|
||||||
#define lua_isstring(L,n) (lua_tostring(L,n) != 0)
|
#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
|
||||||
#define lua_istable(L,n) (*lua_type(L,n) == 't')
|
#define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA)
|
||||||
#define lua_isuserdata(L,n) (*lua_type(L,n) == 'u')
|
#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
|
||||||
#define lua_isnil(L,n) (lua_type(L,n)[2] == 'l')
|
#define lua_isnull(L,n) (lua_type(L,n) == LUA_TNONE)
|
||||||
#define lua_isnull(L,n) (*lua_type(L,n) == 'N')
|
|
||||||
|
#define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -212,3 +245,4 @@ void lua_concat (lua_State *L, int n);
|
|||||||
*
|
*
|
||||||
* This implementation contains no third-party code.
|
* This implementation contains no third-party code.
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
|
|||||||
14
luadebug.h
14
luadebug.h
@@ -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
|
** Debugging API
|
||||||
** See Copyright Notice in lua.h
|
** 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);
|
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||||
|
|
||||||
|
|
||||||
int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
LUA_API int lua_getstack (lua_State *L, int level, 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);
|
||||||
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 *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);
|
||||||
|
|
||||||
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
LUA_API 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_setlinehook (lua_State *L, lua_Hook func);
|
||||||
|
|
||||||
|
|
||||||
#define LUA_IDSIZE 60
|
#define LUA_IDSIZE 60
|
||||||
|
|||||||
17
lualib.h
17
lualib.h
@@ -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
|
** Lua standard libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -11,13 +11,18 @@
|
|||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef LUALIB_API
|
||||||
|
#define LUALIB_API extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define LUA_ALERT "_ALERT"
|
#define LUA_ALERT "_ALERT"
|
||||||
|
|
||||||
void lua_baselibopen (lua_State *L);
|
LUALIB_API void lua_baselibopen (lua_State *L);
|
||||||
void lua_iolibopen (lua_State *L);
|
LUALIB_API void lua_iolibopen (lua_State *L);
|
||||||
void lua_strlibopen (lua_State *L);
|
LUALIB_API void lua_strlibopen (lua_State *L);
|
||||||
void lua_mathlibopen (lua_State *L);
|
LUALIB_API void lua_mathlibopen (lua_State *L);
|
||||||
void lua_dblibopen (lua_State *L);
|
LUALIB_API void lua_dblibopen (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
30
lundump.c
30
lundump.c
@@ -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
|
** load bytecodes from files
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -23,7 +23,7 @@ static const char* ZNAME (ZIO* Z)
|
|||||||
|
|
||||||
static void unexpectedEOZ (lua_State* L, 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)
|
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);
|
int size=LoadInt(L,Z,swap);
|
||||||
tf->code=luaM_newvector(L,size,Instruction);
|
tf->code=luaM_newvector(L,size,Instruction);
|
||||||
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
|
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)
|
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)
|
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
{
|
{
|
||||||
int n=LoadInt(L,Z,swap);
|
int n;
|
||||||
if (n==0) return;
|
tf->nlineinfo=n=LoadInt(L,Z,swap);
|
||||||
tf->lineinfo=luaM_newvector(L,n,int);
|
tf->lineinfo=luaM_newvector(L,n,int);
|
||||||
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
|
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->numparams=LoadInt(L,Z,swap);
|
||||||
tf->is_vararg=LoadByte(L,Z);
|
tf->is_vararg=LoadByte(L,Z);
|
||||||
tf->maxstacksize=LoadInt(L,Z,swap);
|
tf->maxstacksize=LoadInt(L,Z,swap);
|
||||||
LoadCode(L,tf,Z,swap);
|
|
||||||
LoadLocals(L,tf,Z,swap);
|
LoadLocals(L,tf,Z,swap);
|
||||||
LoadLines(L,tf,Z,swap);
|
LoadLines(L,tf,Z,swap);
|
||||||
LoadConstants(L,tf,Z,swap);
|
LoadConstants(L,tf,Z,swap);
|
||||||
|
LoadCode(L,tf,Z,swap);
|
||||||
return tf;
|
return tf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,15 +170,15 @@ static void LoadSignature (lua_State* L, ZIO* Z)
|
|||||||
const char* s=SIGNATURE;
|
const char* s=SIGNATURE;
|
||||||
while (*s!=0 && ezgetc(L,Z)==*s)
|
while (*s!=0 && ezgetc(L,Z)==*s)
|
||||||
++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)
|
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
|
||||||
{
|
{
|
||||||
int r=ezgetc(L,Z);
|
int r=ezgetc(L,Z);
|
||||||
if (r!=s)
|
if (r!=s)
|
||||||
luaO_verror(L,"virtual machine mismatch in `%.255s':\n"
|
luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
|
||||||
" %s is %d but read %d",ZNAME(Z),what,s,r);
|
" %.20s is %d but read %d",ZNAME(Z),what,s,r);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define TESTSIZE(s) TestSize(L,s,#s,Z)
|
#define TESTSIZE(s) TestSize(L,s,#s,Z)
|
||||||
@@ -190,11 +191,11 @@ static int LoadHeader (lua_State* L, ZIO* Z)
|
|||||||
LoadSignature(L,Z);
|
LoadSignature(L,Z);
|
||||||
version=ezgetc(L,Z);
|
version=ezgetc(L,Z);
|
||||||
if (version>VERSION)
|
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",
|
" read version %d.%d; expected at most %d.%d",
|
||||||
ZNAME(Z),V(version),V(VERSION));
|
ZNAME(Z),V(version),V(VERSION));
|
||||||
if (version<VERSION0) /* check last major change */
|
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",
|
" read version %d.%d; expected at least %d.%d",
|
||||||
ZNAME(Z),V(version),V(VERSION));
|
ZNAME(Z),V(version),V(VERSION));
|
||||||
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
|
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));
|
TESTSIZE(sizeof(Number));
|
||||||
f=LoadNumber(L,Z,swap);
|
f=LoadNumber(L,Z,swap);
|
||||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||||
luaO_verror(L,"unknown number format in `%.255s':\n"
|
luaO_verror(L,"unknown number format in `%.99s':\n"
|
||||||
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
" read " NUMBER_FMT "; expected " NUMBER_FMT, ZNAME(Z),f,tf);
|
||||||
ZNAME(Z),f,tf);
|
|
||||||
return swap;
|
return swap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,7 +230,7 @@ Proto* luaU_undump (lua_State* L, ZIO* Z)
|
|||||||
tf=LoadChunk(L,Z);
|
tf=LoadChunk(L,Z);
|
||||||
c=zgetc(Z);
|
c=zgetc(Z);
|
||||||
if (c!=EOZ)
|
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;
|
return tf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
lundump.h
16
lundump.h
@@ -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
|
** load pre-compiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -12,7 +12,6 @@
|
|||||||
|
|
||||||
/* load one chunk */
|
/* load one chunk */
|
||||||
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||||
#define luaU_undump1 luaU_undump
|
|
||||||
|
|
||||||
/* find byte order */
|
/* find byte order */
|
||||||
int luaU_endianess (void);
|
int luaU_endianess (void);
|
||||||
@@ -24,15 +23,10 @@ int luaU_endianess (void);
|
|||||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||||
|
|
||||||
/* formats for error messages */
|
/* formats for error messages */
|
||||||
#define xSOURCE "<%d:%.255s>"
|
#define SOURCE_FMT "<%d:%.99s>"
|
||||||
#define SOURCE "<%.255s:%d>"
|
#define SOURCE tf->lineDefined,tf->source->str
|
||||||
#define IN " in %p " SOURCE
|
#define IN_FMT " in %p " SOURCE_FMT
|
||||||
#define INLOC tf,tf->source->str,tf->lineDefined
|
#define IN tf,SOURCE
|
||||||
|
|
||||||
/* format for numbers in listings and error messages */
|
|
||||||
#ifndef NUMBER_FMT
|
|
||||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* a multiple of PI for testing native format */
|
/* a multiple of PI for testing native format */
|
||||||
/* multiplying by 1E8 gives non-trivial integer values */
|
/* multiplying by 1E8 gives non-trivial integer values */
|
||||||
|
|||||||
200
lvm.c
200
lvm.c
@@ -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
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -39,26 +39,26 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
|
int luaV_tonumber (TObject *obj) {
|
||||||
if (ttype(obj) != TAG_STRING)
|
if (ttype(obj) != LUA_TSTRING)
|
||||||
return 1;
|
return 1;
|
||||||
else {
|
else {
|
||||||
if (!luaO_str2d(svalue(obj), &nvalue(obj)))
|
if (!luaO_str2d(svalue(obj), &nvalue(obj)))
|
||||||
return 2;
|
return 2;
|
||||||
ttype(obj) = TAG_NUMBER;
|
ttype(obj) = LUA_TNUMBER;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
|
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
|
||||||
if (ttype(obj) != TAG_NUMBER)
|
if (ttype(obj) != LUA_TNUMBER)
|
||||||
return 1;
|
return 1;
|
||||||
else {
|
else {
|
||||||
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
|
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);
|
tsvalue(obj) = luaS_new(L, s);
|
||||||
ttype(obj) = TAG_STRING;
|
ttype(obj) = LUA_TSTRING;
|
||||||
return 0;
|
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) {
|
static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) {
|
||||||
CallInfo *ci = infovalue(base-1);
|
CallInfo *ci = infovalue(base-1);
|
||||||
int *lineinfo = ci->func->f.l->lineinfo;
|
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;
|
int newline;
|
||||||
if (ci->line == 0) { /* first time? */
|
if (pc == 0) { /* may be first time? */
|
||||||
ci->line = 1;
|
ci->line = 1;
|
||||||
ci->refi = 0;
|
ci->refi = 0;
|
||||||
ci->lastpc = pc+1; /* make sure it will call linehook */
|
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);
|
Closure *c = luaF_newclosure(L, nelems);
|
||||||
L->top -= nelems;
|
L->top -= nelems;
|
||||||
while (nelems--)
|
while (nelems--)
|
||||||
c->upvalue[nelems] = *(L->top+nelems);
|
c->upvalue[nelems] = *(L->top+nelems);
|
||||||
ttype(L->top) = t;
|
|
||||||
clvalue(L->top) = c;
|
clvalue(L->top) = c;
|
||||||
|
ttype(L->top) = LUA_TFUNCTION;
|
||||||
incr_top;
|
incr_top;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
|
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->f.c = c;
|
||||||
|
cl->isC = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
|
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->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.
|
** Receives the table at `t' and the key at top.
|
||||||
*/
|
*/
|
||||||
const TObject *luaV_gettable (lua_State *L, StkId t) {
|
const TObject *luaV_gettable (lua_State *L, StkId t) {
|
||||||
const TObject *im;
|
Closure *tm;
|
||||||
int tg;
|
int tg;
|
||||||
if (ttype(t) == TAG_TABLE && /* `t' is a table? */
|
if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
|
||||||
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
|
((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
|
||||||
ttype(luaT_getim(L, tg, IM_GETTABLE)) == TAG_NIL)) { /* or no TM? */
|
luaT_gettm(L, tg, TM_GETTABLE) == NULL)) { /* or no TM? */
|
||||||
/* do a primitive get */
|
/* 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? */
|
/* result is no nil or there is no `index' tag method? */
|
||||||
if (ttype(h) != TAG_NIL ||
|
if (ttype(h) != LUA_TNIL || ((tm=luaT_gettm(L, tg, TM_INDEX)) == NULL))
|
||||||
(ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL))
|
|
||||||
return h; /* return result */
|
return h; /* return result */
|
||||||
/* else call `index' tag method */
|
/* else call `index' tag method */
|
||||||
}
|
}
|
||||||
else { /* try a 'gettable' TM */
|
else { /* try a `gettable' tag method */
|
||||||
im = luaT_getimbyObj(L, t, IM_GETTABLE);
|
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);
|
luaD_checkstack(L, 2);
|
||||||
*(L->top+1) = *(L->top-1); /* key */
|
*(L->top+1) = *(L->top-1); /* key */
|
||||||
*L->top = *t; /* table */
|
*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;
|
L->top += 2;
|
||||||
luaD_call(L, L->top - 3, 1);
|
luaD_call(L, L->top - 3, 1);
|
||||||
return L->top - 1; /* call result */
|
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) {
|
void luaV_settable (lua_State *L, StkId t, StkId key) {
|
||||||
int tg;
|
int tg;
|
||||||
if (ttype(t) == TAG_TABLE && /* `t' is a table? */
|
if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
|
||||||
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
|
((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
|
||||||
ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */
|
luaT_gettm(L, tg, TM_SETTABLE) == NULL)) /* or no TM? */
|
||||||
*luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */
|
*luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */
|
||||||
else { /* try a `settable' tag method */
|
else { /* try a `settable' tag method */
|
||||||
const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE);
|
Closure *tm = luaT_gettmbyObj(L, t, TM_SETTABLE);
|
||||||
if (ttype(im) != TAG_NIL) {
|
if (tm != NULL) {
|
||||||
luaD_checkstack(L, 3);
|
luaD_checkstack(L, 3);
|
||||||
*(L->top+2) = *(L->top-1);
|
*(L->top+2) = *(L->top-1);
|
||||||
*(L->top+1) = *key;
|
*(L->top+1) = *key;
|
||||||
*(L->top) = *t;
|
*(L->top) = *t;
|
||||||
*(L->top-1) = *im;
|
clvalue(L->top-1) = tm;
|
||||||
|
ttype(L->top-1) = LUA_TFUNCTION;
|
||||||
L->top += 3;
|
L->top += 3;
|
||||||
luaD_call(L, L->top - 4, 0); /* call `settable' tag method */
|
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 *luaV_getglobal (lua_State *L, TString *s) {
|
||||||
const TObject *value = luaH_getstr(L->gt, s);
|
const TObject *value = luaH_getstr(L->gt, s);
|
||||||
TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
|
Closure *tm = luaT_gettmbyObj(L, value, TM_GETGLOBAL);
|
||||||
if (ttype(im) == TAG_NIL) /* is there a tag method? */
|
if (tm == NULL) /* is there a tag method? */
|
||||||
return value; /* default behavior */
|
return value; /* default behavior */
|
||||||
else { /* tag method */
|
else { /* tag method */
|
||||||
luaD_checkstack(L, 3);
|
luaD_checkstack(L, 3);
|
||||||
*L->top = *im;
|
clvalue(L->top) = tm;
|
||||||
ttype(L->top+1) = TAG_STRING;
|
ttype(L->top) = LUA_TFUNCTION;
|
||||||
tsvalue(L->top+1) = s; /* global name */
|
tsvalue(L->top+1) = s; /* global name */
|
||||||
|
ttype(L->top+1) = LUA_TSTRING;
|
||||||
*(L->top+2) = *value;
|
*(L->top+2) = *value;
|
||||||
L->top += 3;
|
L->top += 3;
|
||||||
luaD_call(L, L->top - 3, 1);
|
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) {
|
void luaV_setglobal (lua_State *L, TString *s) {
|
||||||
const TObject *oldvalue = luaH_getstr(L->gt, s);
|
const TObject *oldvalue = luaH_getstr(L->gt, s);
|
||||||
const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
|
Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL);
|
||||||
if (ttype(im) == TAG_NIL) { /* is there a tag method? */
|
if (tm == NULL) { /* is there a tag method? */
|
||||||
if (oldvalue != &luaO_nilobject) {
|
if (oldvalue != &luaO_nilobject) {
|
||||||
/* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
|
/* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
|
||||||
*(TObject *)oldvalue = *(L->top - 1);
|
*(TObject *)oldvalue = *(L->top - 1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
TObject key;
|
TObject key;
|
||||||
ttype(&key) = TAG_STRING;
|
ttype(&key) = LUA_TSTRING;
|
||||||
tsvalue(&key) = s;
|
tsvalue(&key) = s;
|
||||||
*luaH_set(L, L->gt, &key) = *(L->top - 1);
|
*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);
|
luaD_checkstack(L, 3);
|
||||||
*(L->top+2) = *(L->top-1); /* new value */
|
*(L->top+2) = *(L->top-1); /* new value */
|
||||||
*(L->top+1) = *oldvalue;
|
*(L->top+1) = *oldvalue;
|
||||||
ttype(L->top) = TAG_STRING;
|
ttype(L->top) = LUA_TSTRING;
|
||||||
tsvalue(L->top) = s;
|
tsvalue(L->top) = s;
|
||||||
*(L->top-1) = *im;
|
clvalue(L->top-1) = tm;
|
||||||
|
ttype(L->top-1) = LUA_TFUNCTION;
|
||||||
L->top += 3;
|
L->top += 3;
|
||||||
luaD_call(L, L->top - 4, 0);
|
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 */
|
/* try first operand */
|
||||||
const TObject *im = luaT_getimbyObj(L, top-2, event);
|
Closure *tm = luaT_gettmbyObj(L, top-2, event);
|
||||||
L->top = top;
|
L->top = top;
|
||||||
if (ttype(im) == TAG_NIL) {
|
if (tm == NULL) {
|
||||||
im = luaT_getimbyObj(L, top-1, event); /* try second operand */
|
tm = luaT_gettmbyObj(L, top-1, event); /* try second operand */
|
||||||
if (ttype(im) == TAG_NIL) {
|
if (tm == NULL) {
|
||||||
im = luaT_getim(L, 0, event); /* try a `global' method */
|
tm = luaT_gettm(L, 0, event); /* try a `global' method */
|
||||||
if (ttype(im) == TAG_NIL)
|
if (tm == NULL)
|
||||||
return 0; /* error */
|
return 0; /* error */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_pushstring(L, luaT_eventname[event]);
|
lua_pushstring(L, luaT_eventname[event]);
|
||||||
luaD_callTM(L, im, 3, 1);
|
luaD_callTM(L, tm, 3, 1);
|
||||||
return 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))
|
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) {
|
static int luaV_strcomp (const TString *ls, const TString *rs) {
|
||||||
const char *l = ls->str;
|
const char *l = ls->str;
|
||||||
size_t ll = ls->u.s.len;
|
size_t ll = ls->len;
|
||||||
const char *r = rs->str;
|
const char *r = rs->str;
|
||||||
size_t lr = rs->u.s.len;
|
size_t lr = rs->len;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int temp = strcoll(l, r);
|
int temp = strcoll(l, r);
|
||||||
if (temp != 0) return temp;
|
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) {
|
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));
|
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);
|
return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
|
||||||
else { /* call TM */
|
else { /* call TM */
|
||||||
luaD_checkstack(L, 2);
|
luaD_checkstack(L, 2);
|
||||||
*top++ = *l;
|
*top++ = *l;
|
||||||
*top++ = *r;
|
*top++ = *r;
|
||||||
if (!call_binTM(L, top, IM_LT))
|
if (!call_binTM(L, top, TM_LT))
|
||||||
luaG_ordererror(L, top-2);
|
luaG_ordererror(L, top-2);
|
||||||
L->top--;
|
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 {
|
do {
|
||||||
int n = 2; /* number of elements handled in this pass (at least 2) */
|
int n = 2; /* number of elements handled in this pass (at least 2) */
|
||||||
if (tostring(L, top-2) || tostring(L, top-1)) {
|
if (tostring(L, top-2) || tostring(L, top-1)) {
|
||||||
if (!call_binTM(L, top, IM_CONCAT))
|
if (!call_binTM(L, top, TM_CONCAT))
|
||||||
luaG_binerror(L, top-2, TAG_STRING, "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 */
|
/* at least two string values; get as many as possible */
|
||||||
lint32 tl = (lint32)tsvalue(top-1)->u.s.len +
|
lint32 tl = (lint32)tsvalue(top-1)->len +
|
||||||
(lint32)tsvalue(top-2)->u.s.len;
|
(lint32)tsvalue(top-2)->len;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
int i;
|
int i;
|
||||||
while (n < total && !tostring(L, top-n-1)) { /* collect total length */
|
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++;
|
n++;
|
||||||
}
|
}
|
||||||
if (tl > MAX_SIZET) lua_error(L, "string size overflow");
|
if (tl > MAX_SIZET) lua_error(L, "string size overflow");
|
||||||
buffer = luaO_openspace(L, tl);
|
buffer = luaO_openspace(L, tl);
|
||||||
tl = 0;
|
tl = 0;
|
||||||
for (i=n; i>0; i--) { /* concat all strings */
|
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);
|
memcpy(buffer+tl, tsvalue(top-i)->str, l);
|
||||||
tl += l;
|
tl += l;
|
||||||
}
|
}
|
||||||
@@ -322,7 +327,7 @@ static void luaV_pack (lua_State *L, StkId firstelem) {
|
|||||||
/* store counter in field `n' */
|
/* store counter in field `n' */
|
||||||
luaH_setstrnum(L, htab, luaS_new(L, "n"), i);
|
luaH_setstrnum(L, htab, luaS_new(L, "n"), i);
|
||||||
L->top = firstelem; /* remove elements from the stack */
|
L->top = firstelem; /* remove elements from the stack */
|
||||||
ttype(L->top) = TAG_TABLE;
|
ttype(L->top) = LUA_TTABLE;
|
||||||
hvalue(L->top) = htab;
|
hvalue(L->top) = htab;
|
||||||
incr_top;
|
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).
|
** Returns n such that the the results are between [n,top).
|
||||||
*/
|
*/
|
||||||
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
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 */
|
StkId top; /* keep top local, for performance */
|
||||||
const Instruction *pc = tf->code;
|
const Instruction *pc = tf->code;
|
||||||
TString **kstr = tf->kstr;
|
TString **const kstr = tf->kstr;
|
||||||
lua_Hook linehook = L->linehook;
|
const lua_Hook linehook = L->linehook;
|
||||||
infovalue(base-1)->pc = &pc;
|
infovalue(base-1)->pc = &pc;
|
||||||
luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
|
luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
|
||||||
if (tf->is_vararg) { /* varargs? */
|
if (tf->is_vararg) /* varargs? */
|
||||||
adjust_varargs(L, base, tf->numparams);
|
adjust_varargs(L, base, tf->numparams);
|
||||||
luaC_checkGC(L);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
luaD_adjusttop(L, base, tf->numparams);
|
luaD_adjusttop(L, base, tf->numparams);
|
||||||
top = L->top;
|
top = L->top;
|
||||||
@@ -365,7 +368,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
traceexec(L, base, top, linehook);
|
traceexec(L, base, top, linehook);
|
||||||
switch (GET_OPCODE(i)) {
|
switch (GET_OPCODE(i)) {
|
||||||
case OP_END: {
|
case OP_END: {
|
||||||
return L->top; /* no results */
|
L->top = top;
|
||||||
|
return top;
|
||||||
}
|
}
|
||||||
case OP_RETURN: {
|
case OP_RETURN: {
|
||||||
L->top = top;
|
L->top = top;
|
||||||
@@ -388,7 +392,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
int n = GETARG_U(i);
|
int n = GETARG_U(i);
|
||||||
LUA_ASSERT(n>0, "invalid argument");
|
LUA_ASSERT(n>0, "invalid argument");
|
||||||
do {
|
do {
|
||||||
ttype(top++) = TAG_NIL;
|
ttype(top++) = LUA_TNIL;
|
||||||
} while (--n > 0);
|
} while (--n > 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -397,25 +401,25 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_PUSHINT: {
|
case OP_PUSHINT: {
|
||||||
ttype(top) = TAG_NUMBER;
|
ttype(top) = LUA_TNUMBER;
|
||||||
nvalue(top) = (Number)GETARG_S(i);
|
nvalue(top) = (Number)GETARG_S(i);
|
||||||
top++;
|
top++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_PUSHSTRING: {
|
case OP_PUSHSTRING: {
|
||||||
ttype(top) = TAG_STRING;
|
ttype(top) = LUA_TSTRING;
|
||||||
tsvalue(top) = kstr[GETARG_U(i)];
|
tsvalue(top) = kstr[GETARG_U(i)];
|
||||||
top++;
|
top++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_PUSHNUM: {
|
case OP_PUSHNUM: {
|
||||||
ttype(top) = TAG_NUMBER;
|
ttype(top) = LUA_TNUMBER;
|
||||||
nvalue(top) = tf->knum[GETARG_U(i)];
|
nvalue(top) = tf->knum[GETARG_U(i)];
|
||||||
top++;
|
top++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_PUSHNEGNUM: {
|
case OP_PUSHNEGNUM: {
|
||||||
ttype(top) = TAG_NUMBER;
|
ttype(top) = LUA_TNUMBER;
|
||||||
nvalue(top) = -tf->knum[GETARG_U(i)];
|
nvalue(top) = -tf->knum[GETARG_U(i)];
|
||||||
top++;
|
top++;
|
||||||
break;
|
break;
|
||||||
@@ -441,7 +445,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_GETDOTTED: {
|
case OP_GETDOTTED: {
|
||||||
ttype(top) = TAG_STRING;
|
ttype(top) = LUA_TSTRING;
|
||||||
tsvalue(top) = kstr[GETARG_U(i)];
|
tsvalue(top) = kstr[GETARG_U(i)];
|
||||||
L->top = top+1;
|
L->top = top+1;
|
||||||
*(top-1) = *luaV_gettable(L, 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: {
|
case OP_PUSHSELF: {
|
||||||
TObject receiver;
|
TObject receiver;
|
||||||
receiver = *(top-1);
|
receiver = *(top-1);
|
||||||
ttype(top) = TAG_STRING;
|
ttype(top) = LUA_TSTRING;
|
||||||
tsvalue(top++) = kstr[GETARG_U(i)];
|
tsvalue(top++) = kstr[GETARG_U(i)];
|
||||||
L->top = top;
|
L->top = top;
|
||||||
*(top-2) = *luaV_gettable(L, top-2);
|
*(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;
|
L->top = top;
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
hvalue(top) = luaH_new(L, GETARG_U(i));
|
hvalue(top) = luaH_new(L, GETARG_U(i));
|
||||||
ttype(top) = TAG_TABLE;
|
ttype(top) = LUA_TTABLE;
|
||||||
top++;
|
top++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -510,7 +514,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_ADD: {
|
case OP_ADD: {
|
||||||
if (tonumber(top-2) || tonumber(top-1))
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
call_arith(L, top, IM_ADD);
|
call_arith(L, top, TM_ADD);
|
||||||
else
|
else
|
||||||
nvalue(top-2) += nvalue(top-1);
|
nvalue(top-2) += nvalue(top-1);
|
||||||
top--;
|
top--;
|
||||||
@@ -518,9 +522,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_ADDI: {
|
case OP_ADDI: {
|
||||||
if (tonumber(top-1)) {
|
if (tonumber(top-1)) {
|
||||||
ttype(top) = TAG_NUMBER;
|
ttype(top) = LUA_TNUMBER;
|
||||||
nvalue(top) = (Number)GETARG_S(i);
|
nvalue(top) = (Number)GETARG_S(i);
|
||||||
call_arith(L, top+1, IM_ADD);
|
call_arith(L, top+1, TM_ADD);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nvalue(top-1) += (Number)GETARG_S(i);
|
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: {
|
case OP_SUB: {
|
||||||
if (tonumber(top-2) || tonumber(top-1))
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
call_arith(L, top, IM_SUB);
|
call_arith(L, top, TM_SUB);
|
||||||
else
|
else
|
||||||
nvalue(top-2) -= nvalue(top-1);
|
nvalue(top-2) -= nvalue(top-1);
|
||||||
top--;
|
top--;
|
||||||
@@ -536,7 +540,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_MULT: {
|
case OP_MULT: {
|
||||||
if (tonumber(top-2) || tonumber(top-1))
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
call_arith(L, top, IM_MUL);
|
call_arith(L, top, TM_MUL);
|
||||||
else
|
else
|
||||||
nvalue(top-2) *= nvalue(top-1);
|
nvalue(top-2) *= nvalue(top-1);
|
||||||
top--;
|
top--;
|
||||||
@@ -544,14 +548,14 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_DIV: {
|
case OP_DIV: {
|
||||||
if (tonumber(top-2) || tonumber(top-1))
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
call_arith(L, top, IM_DIV);
|
call_arith(L, top, TM_DIV);
|
||||||
else
|
else
|
||||||
nvalue(top-2) /= nvalue(top-1);
|
nvalue(top-2) /= nvalue(top-1);
|
||||||
top--;
|
top--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_POW: {
|
case OP_POW: {
|
||||||
if (!call_binTM(L, top, IM_POW))
|
if (!call_binTM(L, top, TM_POW))
|
||||||
lua_error(L, "undefined operation");
|
lua_error(L, "undefined operation");
|
||||||
top--;
|
top--;
|
||||||
break;
|
break;
|
||||||
@@ -566,8 +570,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_MINUS: {
|
case OP_MINUS: {
|
||||||
if (tonumber(top-1)) {
|
if (tonumber(top-1)) {
|
||||||
ttype(top) = TAG_NIL;
|
ttype(top) = LUA_TNIL;
|
||||||
call_arith(L, top+1, IM_UNM);
|
call_arith(L, top+1, TM_UNM);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nvalue(top-1) = -nvalue(top-1);
|
nvalue(top-1) = -nvalue(top-1);
|
||||||
@@ -575,7 +579,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_NOT: {
|
case OP_NOT: {
|
||||||
ttype(top-1) =
|
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;
|
nvalue(top-1) = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -610,20 +614,20 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_JMPT: {
|
case OP_JMPT: {
|
||||||
if (ttype(--top) != TAG_NIL) dojump(pc, i);
|
if (ttype(--top) != LUA_TNIL) dojump(pc, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_JMPF: {
|
case OP_JMPF: {
|
||||||
if (ttype(--top) == TAG_NIL) dojump(pc, i);
|
if (ttype(--top) == LUA_TNIL) dojump(pc, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_JMPONT: {
|
case OP_JMPONT: {
|
||||||
if (ttype(top-1) == TAG_NIL) top--;
|
if (ttype(top-1) == LUA_TNIL) top--;
|
||||||
else dojump(pc, i);
|
else dojump(pc, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_JMPONF: {
|
case OP_JMPONF: {
|
||||||
if (ttype(top-1) != TAG_NIL) top--;
|
if (ttype(top-1) != LUA_TNIL) top--;
|
||||||
else dojump(pc, i);
|
else dojump(pc, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -632,7 +636,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_PUSHNILJMP: {
|
case OP_PUSHNILJMP: {
|
||||||
ttype(top++) = TAG_NIL;
|
ttype(top++) = LUA_TNIL;
|
||||||
pc++;
|
pc++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -652,9 +656,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_FORLOOP: {
|
case OP_FORLOOP: {
|
||||||
LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step");
|
LUA_ASSERT(ttype(top-1) == LUA_TNUMBER, "invalid step");
|
||||||
LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit");
|
LUA_ASSERT(ttype(top-2) == LUA_TNUMBER, "invalid limit");
|
||||||
if (ttype(top-3) != TAG_NUMBER)
|
if (ttype(top-3) != LUA_TNUMBER)
|
||||||
lua_error(L, "`for' index must be a number");
|
lua_error(L, "`for' index must be a number");
|
||||||
nvalue(top-3) += nvalue(top-1); /* increment index */
|
nvalue(top-3) += nvalue(top-1); /* increment index */
|
||||||
if (nvalue(top-1) > 0 ?
|
if (nvalue(top-1) > 0 ?
|
||||||
@@ -667,7 +671,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_LFORPREP: {
|
case OP_LFORPREP: {
|
||||||
Node *node;
|
Node *node;
|
||||||
if (ttype(top-1) != TAG_TABLE)
|
if (ttype(top-1) != LUA_TTABLE)
|
||||||
lua_error(L, "`for' table must be a table");
|
lua_error(L, "`for' table must be a table");
|
||||||
node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
|
node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
|
||||||
if (node == NULL) { /* `empty' loop? */
|
if (node == NULL) { /* `empty' loop? */
|
||||||
@@ -683,7 +687,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
|||||||
}
|
}
|
||||||
case OP_LFORLOOP: {
|
case OP_LFORLOOP: {
|
||||||
Node *node;
|
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);
|
node = luaH_next(L, hvalue(top-3), top-2);
|
||||||
if (node == NULL) /* end loop? */
|
if (node == NULL) /* end loop? */
|
||||||
top -= 3; /* remove table, key, and value */
|
top -= 3; /* remove table, key, and value */
|
||||||
|
|||||||
6
lvm.h
6
lvm.h
@@ -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
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,8 +13,8 @@
|
|||||||
#include "ltm.h"
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0))
|
#define tonumber(o) ((ttype(o) != LUA_TNUMBER) && (luaV_tonumber(o) != 0))
|
||||||
#define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0))
|
#define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0))
|
||||||
|
|
||||||
|
|
||||||
int luaV_tonumber (TObject *obj);
|
int luaV_tonumber (TObject *obj);
|
||||||
|
|||||||
5
lzio.h
5
lzio.h
@@ -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
|
** Buffered streams
|
||||||
** See Copyright Notice in lua.h
|
** 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)
|
#define zname(z) ((z)->name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* --------- Private Part ------------------ */
|
/* --------- Private Part ------------------ */
|
||||||
|
|
||||||
|
#ifndef ZBSIZE
|
||||||
#define ZBSIZE 256 /* buffer size */
|
#define ZBSIZE 256 /* buffer size */
|
||||||
|
#endif
|
||||||
|
|
||||||
struct zio {
|
struct zio {
|
||||||
size_t n; /* bytes still unread */
|
size_t n; /* bytes still unread */
|
||||||
|
|||||||
8
makefile
8
makefile
@@ -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
|
## Makefile
|
||||||
## See Copyright Notice in lua.h
|
## See Copyright Notice in lua.h
|
||||||
#
|
#
|
||||||
@@ -23,11 +23,11 @@
|
|||||||
# (only for compatibility with previous versions)
|
# (only for compatibility with previous versions)
|
||||||
# define LUA_COMPAT_ARGRET for compatibility in the way function results
|
# define LUA_COMPAT_ARGRET for compatibility in the way function results
|
||||||
# are passed as arguments
|
# are passed as arguments
|
||||||
# define LUA_DEPRECATETFUNCS to include obsolete functions
|
# define LUA_DEPRECATEDFUNCS to include obsolete functions
|
||||||
|
|
||||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||||
#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
|
#CONFIG = -DOLD_ANSI -DLUA_DEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
|
||||||
# -DLUA_DEPRECATETFUNCS
|
# -DLUA_DEPRECATEDFUNCS
|
||||||
|
|
||||||
|
|
||||||
# Compilation parameters
|
# Compilation parameters
|
||||||
|
|||||||
1076
manual.tex
1076
manual.tex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user