mirror of
https://github.com/lua/lua.git
synced 2026-07-27 08:29:08 +00:00
Compare commits
233 Commits
v4.0-beta
...
v4.1-alpha
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0171543704 | ||
|
|
ebd1d1f82c | ||
|
|
04320e04bf | ||
|
|
5d2d2b1752 | ||
|
|
f34001faa9 | ||
|
|
95988676d8 | ||
|
|
767abdfdc0 | ||
|
|
45b173cbf8 | ||
|
|
a94cba4b88 | ||
|
|
f9f355221f | ||
|
|
b3dd9b1bb1 | ||
|
|
d8f37bf42a | ||
|
|
a598804a04 | ||
|
|
6c8e652aa6 | ||
|
|
9f25df02d5 | ||
|
|
ae1cf64348 | ||
|
|
a264fd089e | ||
|
|
a3d03ff6b6 | ||
|
|
654b16e83a | ||
|
|
dc4e0ecdaf | ||
|
|
9924668931 | ||
|
|
89ef91a7fe | ||
|
|
00aaee0ae7 | ||
|
|
18d3e82cfe | ||
|
|
2a4afb97c8 | ||
|
|
87a5fae453 | ||
|
|
afed6c6864 | ||
|
|
770954510f | ||
|
|
b346834a09 | ||
|
|
61a036eaa5 | ||
|
|
37f3a1c045 | ||
|
|
9559c111a3 | ||
|
|
777061e441 | ||
|
|
fccadba4b5 | ||
|
|
b940f09984 | ||
|
|
878979cef7 | ||
|
|
8e586c13fc | ||
|
|
eadf2aaaff | ||
|
|
ae19b2f51e | ||
|
|
a3fbf5f5fd | ||
|
|
7fae1c17b1 | ||
|
|
89e8303f4e | ||
|
|
89c301d180 | ||
|
|
186111dc01 | ||
|
|
0a1b1acdd3 | ||
|
|
79acf5ea60 | ||
|
|
cbc59592ff | ||
|
|
4905fdd135 | ||
|
|
0267168675 | ||
|
|
ba11831d35 | ||
|
|
190ddd431d | ||
|
|
65c289b046 | ||
|
|
d5b83ead90 | ||
|
|
da673d31aa | ||
|
|
323da72577 | ||
|
|
943b8f5b18 | ||
|
|
762d059a13 | ||
|
|
572a69b6af | ||
|
|
beee01b170 | ||
|
|
6473f965ca | ||
|
|
0e0e4a480e | ||
|
|
2a50188269 | ||
|
|
9aedea6ec8 | ||
|
|
a68635a919 | ||
|
|
2112142680 | ||
|
|
dd3a63c205 | ||
|
|
cb49b088b6 | ||
|
|
c5a23cf01a | ||
|
|
6048c4f74d | ||
|
|
5e870f86a2 | ||
|
|
f81b8adb3f | ||
|
|
2b2267069b | ||
|
|
72d3d155b0 | ||
|
|
6d4db86888 | ||
|
|
e9a3820370 | ||
|
|
9e8a46daa2 | ||
|
|
7b84f9e65c | ||
|
|
1e40b4dc61 | ||
|
|
dfd7ce74cf | ||
|
|
8399df5dcf | ||
|
|
d2a64bb6d4 | ||
|
|
a90f06dcd8 | ||
|
|
35023355f2 | ||
|
|
39b7978329 | ||
|
|
d164e2294f | ||
|
|
2cc94ac81b | ||
|
|
5f37134e64 | ||
|
|
52ee91dd73 | ||
|
|
191fd35f0a | ||
|
|
d6978d03e1 | ||
|
|
68b08848d4 | ||
|
|
888f91fa24 | ||
|
|
c1db0b2bf1 | ||
|
|
099442c41f | ||
|
|
27600fe87a | ||
|
|
f8509668dc | ||
|
|
40f4e931f3 | ||
|
|
29371ecfe8 | ||
|
|
5d9b8b7cdc | ||
|
|
1c2501fad4 | ||
|
|
a4ae1475e3 | ||
|
|
b3da4ee907 | ||
|
|
3c649ce123 | ||
|
|
95da574dbc | ||
|
|
255052b6c6 | ||
|
|
1bdc156b52 | ||
|
|
03d8a9bf0d | ||
|
|
d2e340f467 | ||
|
|
6875fdc8be | ||
|
|
dc17a9cc24 | ||
|
|
81e63f75c0 | ||
|
|
c81404cae5 | ||
|
|
b6ce590433 | ||
|
|
e70e6a3b7a | ||
|
|
6548bf7462 | ||
|
|
7178a5e34a | ||
|
|
322b7b5fc5 | ||
|
|
1f917e709c | ||
|
|
d444153dbe | ||
|
|
a894499566 | ||
|
|
d1fc6244f0 | ||
|
|
7e3d3e1f70 | ||
|
|
1f9e3731d1 | ||
|
|
42224ca553 | ||
|
|
426d3e43bd | ||
|
|
8823f371a2 | ||
|
|
e506b864cd | ||
|
|
9a231afa97 | ||
|
|
4ab6acacdf | ||
|
|
6858763994 | ||
|
|
b68fb7f62e | ||
|
|
60ff79451c | ||
|
|
44a53df688 | ||
|
|
63a822c8e1 | ||
|
|
09def5da44 | ||
|
|
6b71a9cfe5 | ||
|
|
fa8c44b510 | ||
|
|
3b6f8bfbe6 | ||
|
|
caf01b5bfa | ||
|
|
ca1f28b829 | ||
|
|
b82242d4c4 | ||
|
|
ac390020e9 | ||
|
|
9b45439860 | ||
|
|
7959f3aebb | ||
|
|
bce6572579 | ||
|
|
a53d9b66ca | ||
|
|
c8559e3c8d | ||
|
|
71ae4801d6 | ||
|
|
6fda6a5302 | ||
|
|
4ac58853dc | ||
|
|
f2c451d745 | ||
|
|
619edfd9e4 | ||
|
|
a04de4f0ad | ||
|
|
a653d93a43 | ||
|
|
321c7fb6f8 | ||
|
|
dabb19fc17 | ||
|
|
08496eea8b | ||
|
|
4ff5545709 | ||
|
|
595e449537 | ||
|
|
a907aeeb1e | ||
|
|
76f62fc5a1 | ||
|
|
0183b8030c | ||
|
|
8c49e19865 | ||
|
|
6af005ec20 | ||
|
|
f8a0fd268e | ||
|
|
0066bbbb0b | ||
|
|
1db05793a0 | ||
|
|
af97be026b | ||
|
|
cce8ebd306 | ||
|
|
4894c27962 | ||
|
|
10ac68c648 | ||
|
|
01b00cc292 | ||
|
|
fc7b167ae0 | ||
|
|
96253ed8ce | ||
|
|
35d6b15057 | ||
|
|
22914afab3 | ||
|
|
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 |
72
bugs
72
bugs
@@ -198,3 +198,75 @@ Tue Aug 29 15:57:41 EST 2000
|
||||
>> gc tag method for nil could call line hook
|
||||
(by ry; since ?)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0 Beta
|
||||
|
||||
** liolib.c
|
||||
Fri Sep 22 15:12:37 EST 2000
|
||||
>> `read("*w")' should return nil at EOF
|
||||
(by roberto; since 4.0b)
|
||||
|
||||
** lvm.c
|
||||
Mon Sep 25 11:47:48 EST 2000
|
||||
>> lua_gettable does not get key from stack top
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lgc.c
|
||||
Mon Sep 25 11:50:48 EST 2000
|
||||
>> GC may crash when checking locked C closures
|
||||
(by Philip Yi; since 4.0b)
|
||||
|
||||
** lapi.c
|
||||
Wed Sep 27 09:50:19 EST 2000
|
||||
>> lua_tag should return LUA_NOTAG for non-valid indices
|
||||
(by Paul Hankin; since 4.0b)
|
||||
|
||||
** llex.h / llex.c / lparser.c
|
||||
Wed Sep 27 13:39:45 EST 2000
|
||||
>> parser overwrites semantic information when looking ahead
|
||||
>> (e.g. «a = {print'foo'}»)
|
||||
(by Edgar Toernig; since 4.0b, deriving from previous bug)
|
||||
|
||||
** liolib.c
|
||||
Thu Oct 26 10:50:46 EDT 2000
|
||||
>> in function `read_file', realloc() doesn't free the buffer if it can't
|
||||
>> allocate new memory
|
||||
(by Mauro Vezzosi; since 4.0b)
|
||||
|
||||
|
||||
|
||||
=================================================================
|
||||
--- Version 4.0
|
||||
|
||||
** lparser.c
|
||||
Wed Nov 29 09:51:44 EDT 2000
|
||||
>> parser does not accept a `;' after a `return'
|
||||
(by lhf; since 4.0b)
|
||||
|
||||
** liolib.c
|
||||
Fri Dec 22 15:30:42 EDT 2000
|
||||
>> when `read' fails it must return nil (and not no value)
|
||||
(by cassino; since at least 3.1)
|
||||
|
||||
** lstring.c/lapi.c
|
||||
Thu Feb 1 11:55:45 EDT 2001
|
||||
>> lua_pushuserdata(L, NULL) is buggy
|
||||
(by Edgar Toernig; since 4.0)
|
||||
|
||||
** ldo.c
|
||||
Fri Feb 2 14:06:40 EDT 2001
|
||||
>> «while 1 dostring[[print('hello\n')]] end» never reclaims memory
|
||||
(by Andrew Paton; since 4.0b)
|
||||
|
||||
** lbaselib.c
|
||||
Tue Feb 6 11:57:13 EDT 2001
|
||||
>> ESC (which starts precompiled code) in C is \33, not \27
|
||||
(by Edgar Toernig and lhf; since 4.0b)
|
||||
|
||||
** lparser.c
|
||||
Tue Jul 10 16:59:18 EST 2001
|
||||
>> error message for `%a' gave wrong line number
|
||||
(by Leonardo Constantino; since 4.0)
|
||||
|
||||
|
||||
762
lapi.c
762
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.148 2001/07/12 18:11:58 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -22,29 +23,53 @@
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
||||
"$Authors: " LUA_AUTHORS " $";
|
||||
const l_char lua_ident[] = l_s("$Lua: ") l_s(LUA_VERSION) l_s(" ")
|
||||
l_s(LUA_COPYRIGHT) l_s(" $\n") l_s("$Authors: ") l_s(LUA_AUTHORS) l_s(" $");
|
||||
|
||||
|
||||
|
||||
#define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
|
||||
#ifndef api_check
|
||||
#define api_check(L, o) /* nothing */
|
||||
#endif
|
||||
|
||||
#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->ci->base))
|
||||
|
||||
#define api_incr_top(L) incr_top
|
||||
|
||||
|
||||
|
||||
|
||||
TObject *luaA_index (lua_State *L, int index) {
|
||||
return Index(L, index);
|
||||
if (index > 0) {
|
||||
api_check(L, index <= L->top - L->ci->base);
|
||||
return L->ci->base+index-1;
|
||||
}
|
||||
else {
|
||||
api_check(L, index != 0 && -index <= L->top - L->ci->base);
|
||||
return L->top+index;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static TObject *luaA_indexAcceptable (lua_State *L, int index) {
|
||||
if (index > 0) {
|
||||
TObject *o = L->ci->base+(index-1);
|
||||
api_check(L, index <= L->stack_last - L->ci->base);
|
||||
if (o >= L->top) return NULL;
|
||||
else return o;
|
||||
}
|
||||
else {
|
||||
api_check(L, index != 0 && -index <= L->top - L->ci->base);
|
||||
return L->top+index;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaA_pushobject (lua_State *L, const TObject *o) {
|
||||
*L->top = *o;
|
||||
setobj(L->top, o);
|
||||
incr_top;
|
||||
}
|
||||
|
||||
int lua_stackspace (lua_State *L) {
|
||||
LUA_API int lua_stackspace (lua_State *L) {
|
||||
return (L->stack_last - L->top);
|
||||
}
|
||||
|
||||
@@ -55,38 +80,51 @@ int lua_stackspace (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
int lua_gettop (lua_State *L) {
|
||||
return (L->top - L->Cbase);
|
||||
LUA_API int lua_gettop (lua_State *L) {
|
||||
return (L->top - L->ci->base);
|
||||
}
|
||||
|
||||
|
||||
void lua_settop (lua_State *L, int index) {
|
||||
if (index >= 0)
|
||||
luaD_adjusttop(L, L->Cbase, index);
|
||||
else
|
||||
L->top = L->top+index+1; /* index is negative */
|
||||
LUA_API void lua_settop (lua_State *L, int index) {
|
||||
lua_lock(L);
|
||||
if (index >= 0) {
|
||||
api_check(L, index <= L->stack_last - L->ci->base);
|
||||
luaD_adjusttop(L, L->ci->base+index);
|
||||
}
|
||||
else {
|
||||
api_check(L, -(index+1) <= (L->top - L->ci->base));
|
||||
L->top += index+1; /* `subtract' index (index is negative) */
|
||||
}
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_remove (lua_State *L, int index) {
|
||||
StkId p = Index(L, index);
|
||||
while (++p < L->top) *(p-1) = *p;
|
||||
LUA_API void lua_remove (lua_State *L, int index) {
|
||||
StkId p;
|
||||
lua_lock(L);
|
||||
p = luaA_index(L, index);
|
||||
while (++p < L->top) setobj(p-1, p);
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_insert (lua_State *L, int index) {
|
||||
StkId p = Index(L, index);
|
||||
LUA_API void lua_insert (lua_State *L, int index) {
|
||||
StkId p;
|
||||
StkId q;
|
||||
for (q = L->top; q>p; q--)
|
||||
*q = *(q-1);
|
||||
*p = *L->top;
|
||||
lua_lock(L);
|
||||
p = luaA_index(L, index);
|
||||
for (q = L->top; q>p; q--) setobj(q, q-1);
|
||||
setobj(p, L->top);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushvalue (lua_State *L, int index) {
|
||||
*L->top = *Index(L, index);
|
||||
LUA_API void lua_pushvalue (lua_State *L, int index) {
|
||||
lua_lock(L);
|
||||
setobj(L->top, luaA_index(L, index));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,89 +134,139 @@ void lua_pushvalue (lua_State *L, int index) {
|
||||
*/
|
||||
|
||||
|
||||
#define btest(L,i,value,default) { \
|
||||
StkId o; \
|
||||
if ((i) >= 0) { \
|
||||
o = L->Cbase+((i)-1); \
|
||||
if (o >= L->top) return (default); \
|
||||
} \
|
||||
else o = L->top+(i); \
|
||||
return (value); }
|
||||
|
||||
|
||||
#define access(L,i,test,default,value) { \
|
||||
StkId o; \
|
||||
if ((i) >= 0) { \
|
||||
o = L->Cbase+((i)-1); \
|
||||
if (o >= L->top) return (default); \
|
||||
} \
|
||||
else o = L->top+(i); \
|
||||
return ((test) ? (value) : (default)); }
|
||||
|
||||
|
||||
const char *lua_type (lua_State *L, int index) {
|
||||
btest(L, index, luaO_typename(o), "NO VALUE");
|
||||
LUA_API int lua_rawtag (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? LUA_TNONE : ttype(o);
|
||||
}
|
||||
|
||||
int lua_iscfunction (lua_State *L, int index) {
|
||||
btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
|
||||
|
||||
LUA_API const l_char *lua_type (lua_State *L, int index) {
|
||||
StkId o;
|
||||
const l_char *type;
|
||||
lua_lock(L);
|
||||
o = luaA_indexAcceptable(L, index);
|
||||
type = (o == NULL) ? l_s("no value") : luaT_typename(G(L), o);
|
||||
lua_unlock(L);
|
||||
return type;
|
||||
}
|
||||
|
||||
int lua_isnumber (lua_State *L, int index) {
|
||||
btest(L, index, (tonumber(Index(L, index)) == 0), 0);
|
||||
|
||||
LUA_API int lua_iscfunction (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL) ? 0 : iscfunction(o);
|
||||
}
|
||||
|
||||
int lua_tag (lua_State *L, int index) {
|
||||
btest(L, index,
|
||||
((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
|
||||
luaT_effectivetag(L, o)), -1);
|
||||
|
||||
LUA_API int lua_isnumber (lua_State *L, int index) {
|
||||
TObject n;
|
||||
TObject *o = luaA_indexAcceptable(L, index);
|
||||
return (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n)));
|
||||
}
|
||||
|
||||
int lua_equal (lua_State *L, int index1, int index2) {
|
||||
StkId o1 = Index(L, index1);
|
||||
StkId o2 = Index(L, index2);
|
||||
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
|
||||
else return luaO_equalObj(o1, o2);
|
||||
|
||||
LUA_API int lua_isstring (lua_State *L, int index) {
|
||||
int t = lua_rawtag(L, index);
|
||||
return (t == LUA_TSTRING || t == LUA_TNUMBER);
|
||||
}
|
||||
|
||||
int lua_lessthan (lua_State *L, int index1, int index2) {
|
||||
StkId o1 = Index(L, index1);
|
||||
StkId o2 = Index(L, index2);
|
||||
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
|
||||
else return luaV_lessthan(L, o1, o2, L->top);
|
||||
|
||||
LUA_API int lua_tag (lua_State *L, int index) {
|
||||
StkId o;
|
||||
int i;
|
||||
lua_lock(L); /* other thread could be changing the tag */
|
||||
o = luaA_indexAcceptable(L, index);
|
||||
i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
|
||||
lua_unlock(L);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2) {
|
||||
StkId o1 = luaA_indexAcceptable(L, index1);
|
||||
StkId o2 = luaA_indexAcceptable(L, index2);
|
||||
return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
|
||||
: luaO_equalObj(o1, o2);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
|
||||
StkId o1, o2;
|
||||
int i;
|
||||
lua_lock(L); /* may call tag method */
|
||||
o1 = luaA_indexAcceptable(L, index1);
|
||||
o2 = luaA_indexAcceptable(L, index2);
|
||||
i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
|
||||
: luaV_lessthan(L, o1, o2);
|
||||
lua_unlock(L);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
double lua_tonumber (lua_State *L, int index) {
|
||||
access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
|
||||
LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
|
||||
TObject n;
|
||||
const TObject *o = luaA_indexAcceptable(L, index);
|
||||
if (o != NULL &&
|
||||
(ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL))
|
||||
return nvalue(o);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *lua_tostring (lua_State *L, int index) {
|
||||
luaC_checkGC(L); /* `tostring' may create a new string */
|
||||
access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
|
||||
|
||||
LUA_API const l_char *lua_tostring (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL)
|
||||
return NULL;
|
||||
else if (ttype(o) == LUA_TSTRING)
|
||||
return svalue(o);
|
||||
else {
|
||||
const l_char *s;
|
||||
lua_lock(L); /* `luaV_tostring' may create a new string */
|
||||
s = (luaV_tostring(L, o) == 0) ? svalue(o) : NULL;
|
||||
lua_unlock(L);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
size_t lua_strlen (lua_State *L, int index) {
|
||||
access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
|
||||
|
||||
LUA_API size_t lua_strlen (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL)
|
||||
return 0;
|
||||
else if (ttype(o) == LUA_TSTRING)
|
||||
return tsvalue(o)->tsv.len;
|
||||
else {
|
||||
size_t l;
|
||||
lua_lock(L); /* `luaV_tostring' may create a new string */
|
||||
l = (luaV_tostring(L, o) == 0) ? tsvalue(o)->tsv.len : 0;
|
||||
lua_unlock(L);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
|
||||
lua_CFunction lua_tocfunction (lua_State *L, int index) {
|
||||
access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
|
||||
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
|
||||
}
|
||||
|
||||
void *lua_touserdata (lua_State *L, int index) {
|
||||
access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
|
||||
|
||||
LUA_API void *lua_touserdata (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : uvalue(o)->uv.value;
|
||||
}
|
||||
|
||||
const void *lua_topointer (lua_State *L, int index) {
|
||||
StkId o = Index(L, index);
|
||||
switch (ttype(o)) {
|
||||
case TAG_TABLE:
|
||||
return hvalue(o);
|
||||
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
||||
return clvalue(o);
|
||||
default: return NULL;
|
||||
|
||||
LUA_API const void *lua_topointer (lua_State *L, int index) {
|
||||
StkId o = luaA_indexAcceptable(L, index);
|
||||
if (o == NULL) return NULL;
|
||||
else {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE: return hvalue(o);
|
||||
case LUA_TFUNCTION: return clvalue(o);
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,28 +277,31 @@ const void *lua_topointer (lua_State *L, int index) {
|
||||
*/
|
||||
|
||||
|
||||
void lua_pushnil (lua_State *L) {
|
||||
ttype(L->top) = TAG_NIL;
|
||||
LUA_API void lua_pushnil (lua_State *L) {
|
||||
lua_lock(L);
|
||||
setnilvalue(L->top);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushnumber (lua_State *L, double n) {
|
||||
ttype(L->top) = TAG_NUMBER;
|
||||
nvalue(L->top) = n;
|
||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
||||
lua_lock(L);
|
||||
setnvalue(L->top, n);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
||||
luaC_checkGC(L);
|
||||
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
||||
ttype(L->top) = TAG_STRING;
|
||||
LUA_API void lua_pushlstring (lua_State *L, const l_char *s, size_t len) {
|
||||
lua_lock(L);
|
||||
setsvalue(L->top, luaS_newlstr(L, s, len));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushstring (lua_State *L, const char *s) {
|
||||
LUA_API void lua_pushstring (lua_State *L, const l_char *s) {
|
||||
if (s == NULL)
|
||||
lua_pushnil(L);
|
||||
else
|
||||
@@ -218,19 +309,11 @@ void lua_pushstring (lua_State *L, const char *s) {
|
||||
}
|
||||
|
||||
|
||||
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
luaC_checkGC(L);
|
||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
luaV_Cclosure(L, fn, n);
|
||||
}
|
||||
|
||||
|
||||
void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
|
||||
luaC_checkGC(L);
|
||||
if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
|
||||
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
|
||||
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
||||
ttype(L->top) = TAG_USERDATA;
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
@@ -240,62 +323,91 @@ void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
|
||||
*/
|
||||
|
||||
|
||||
void lua_getglobal (lua_State *L, const char *name) {
|
||||
StkId top = L->top;
|
||||
*top = *luaV_getglobal(L, luaS_new(L, name));
|
||||
L->top = top;
|
||||
LUA_API void lua_getglobal (lua_State *L, const l_char *name) {
|
||||
lua_lock(L);
|
||||
luaV_getglobal(L, luaS_new(L, name), L->top);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_gettable (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
StkId top = L->top;
|
||||
*(top-1) = *luaV_gettable(L, t);
|
||||
L->top = top; /* tag method may change top */
|
||||
LUA_API void lua_gettable (lua_State *L, int index) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
luaV_gettable(L, t, L->top-1, L->top-1);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawget (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
|
||||
*(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
|
||||
LUA_API void lua_rawget (lua_State *L, int index) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttype(t) == LUA_TTABLE);
|
||||
setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawgeti (lua_State *L, int index, int n) {
|
||||
StkId o = Index(L, index);
|
||||
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
|
||||
*L->top = *luaH_getnum(hvalue(o), n);
|
||||
LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
o = luaA_index(L, index);
|
||||
api_check(L, ttype(o) == LUA_TTABLE);
|
||||
setobj(L->top, luaH_getnum(hvalue(o), n));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_getglobals (lua_State *L) {
|
||||
hvalue(L->top) = L->gt;
|
||||
ttype(L->top) = TAG_TABLE;
|
||||
LUA_API void lua_getglobals (lua_State *L) {
|
||||
lua_lock(L);
|
||||
sethvalue(L->top, L->gt);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
int lua_getref (lua_State *L, int ref) {
|
||||
if (ref == LUA_REFNIL)
|
||||
ttype(L->top) = TAG_NIL;
|
||||
else if (0 <= ref && ref < L->refSize &&
|
||||
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
||||
*L->top = L->refArray[ref].o;
|
||||
else
|
||||
return 0;
|
||||
api_incr_top(L);
|
||||
return 1;
|
||||
LUA_API int lua_getref (lua_State *L, int ref) {
|
||||
int status;
|
||||
lua_lock(L);
|
||||
if (ref == LUA_REFNIL) {
|
||||
setnilvalue(L->top);
|
||||
status = 1;
|
||||
}
|
||||
else {
|
||||
setobj(L->top, luaH_getnum(G(L)->weakregistry, ref));
|
||||
status = (ttype(L->top) != LUA_TNIL);
|
||||
}
|
||||
if (status)
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void lua_newtable (lua_State *L) {
|
||||
luaC_checkGC(L);
|
||||
hvalue(L->top) = luaH_new(L, 0);
|
||||
ttype(L->top) = TAG_TABLE;
|
||||
LUA_API void lua_newtable (lua_State *L) {
|
||||
lua_lock(L);
|
||||
sethvalue(L->top, luaH_new(L, 0));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_getregistry (lua_State *L) {
|
||||
lua_lock(L);
|
||||
sethvalue(L->top, G(L)->registry);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_getweakregistry (lua_State *L) {
|
||||
lua_lock(L);
|
||||
sethvalue(L->top, G(L)->weakregistry);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
@@ -305,73 +417,160 @@ void lua_newtable (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
void lua_setglobal (lua_State *L, const char *name) {
|
||||
StkId top = L->top;
|
||||
luaV_setglobal(L, luaS_new(L, name));
|
||||
L->top = top-1; /* remove element from the top */
|
||||
LUA_API void lua_setglobal (lua_State *L, const l_char *name) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
luaV_setglobal(L, luaS_new(L, name), L->top - 1);
|
||||
L->top--; /* remove element from the top */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_settable (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
StkId top = L->top;
|
||||
luaV_settable(L, t, top-2);
|
||||
L->top = top-2; /* pop index and value */
|
||||
LUA_API void lua_settable (lua_State *L, int index) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
t = luaA_index(L, index);
|
||||
luaV_settable(L, t, L->top - 2, L->top - 1);
|
||||
L->top -= 2; /* pop index and value */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawset (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
|
||||
*luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
|
||||
LUA_API void lua_rawset (lua_State *L, int index) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttype(t) == LUA_TTABLE);
|
||||
setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
|
||||
L->top -= 2;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawseti (lua_State *L, int index, int n) {
|
||||
StkId o = Index(L, index);
|
||||
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
|
||||
*luaH_setint(L, hvalue(o), n) = *(L->top-1);
|
||||
LUA_API void lua_rawseti (lua_State *L, int index, int n) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
o = luaA_index(L, index);
|
||||
api_check(L, ttype(o) == LUA_TTABLE);
|
||||
setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_setglobals (lua_State *L) {
|
||||
StkId newtable = --L->top;
|
||||
LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
|
||||
LUA_API void lua_setglobals (lua_State *L) {
|
||||
StkId newtable;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
newtable = --L->top;
|
||||
api_check(L, ttype(newtable) == LUA_TTABLE);
|
||||
L->gt = hvalue(newtable);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
int lua_ref (lua_State *L, int lock) {
|
||||
LUA_API int lua_ref (lua_State *L, int lock) {
|
||||
int ref;
|
||||
if (ttype(L->top-1) == TAG_NIL)
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
ref = LUA_REFNIL;
|
||||
else {
|
||||
if (L->refFree != NONEXT) { /* is there a free place? */
|
||||
ref = L->refFree;
|
||||
L->refFree = L->refArray[ref].st;
|
||||
}
|
||||
else { /* no more free places */
|
||||
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
|
||||
"reference table overflow", MAX_INT);
|
||||
ref = L->refSize++;
|
||||
}
|
||||
L->refArray[ref].o = *(L->top-1);
|
||||
L->refArray[ref].st = lock ? LOCK : HOLD;
|
||||
}
|
||||
L->top--;
|
||||
else {
|
||||
lua_getweakregistry(L);
|
||||
ref = lua_getn(L, -1) + 1;
|
||||
lua_pushvalue(L, -2);
|
||||
lua_rawseti(L, -2, ref);
|
||||
if (lock) {
|
||||
lua_getregistry(L);
|
||||
lua_pushvalue(L, -3);
|
||||
lua_rawseti(L, -2, ref);
|
||||
lua_pop(L, 1); /* remove registry */
|
||||
}
|
||||
lua_pushliteral(L, l_s("n"));
|
||||
lua_pushnumber(L, ref);
|
||||
lua_settable(L, -3);
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** "do" functions (run Lua code)
|
||||
** (most of them are in ldo.c)
|
||||
** `do' functions (run Lua code)
|
||||
*/
|
||||
|
||||
void lua_rawcall (lua_State *L, int nargs, int nresults) {
|
||||
luaD_call(L, L->top-(nargs+1), nresults);
|
||||
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
|
||||
StkId func;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, nargs+1);
|
||||
func = L->top - (nargs+1);
|
||||
luaD_call(L, func);
|
||||
if (nresults != LUA_MULTRET)
|
||||
luaD_adjusttop(L, func + nresults);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dofile (lua_State *L, const l_char *filename) {
|
||||
int status;
|
||||
status = lua_loadfile(L, filename);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size,
|
||||
const l_char *name) {
|
||||
int status;
|
||||
status = lua_loadbuffer(L, buff, size, name);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_dostring (lua_State *L, const l_char *str) {
|
||||
return lua_dobuffer(L, str, strlen(str), str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Garbage-collection functions
|
||||
*/
|
||||
|
||||
/* GC values are expressed in Kbytes: #bytes/2^10 */
|
||||
#define GCscale(x) ((int)((x)>>10))
|
||||
#define GCunscale(x) ((lu_mem)(x)<<10)
|
||||
|
||||
LUA_API int lua_getgcthreshold (lua_State *L) {
|
||||
int threshold;
|
||||
lua_lock(L);
|
||||
threshold = GCscale(G(L)->GCthreshold);
|
||||
lua_unlock(L);
|
||||
return threshold;
|
||||
}
|
||||
|
||||
LUA_API int lua_getgccount (lua_State *L) {
|
||||
int count;
|
||||
lua_lock(L);
|
||||
count = GCscale(G(L)->nblocks);
|
||||
lua_unlock(L);
|
||||
return count;
|
||||
}
|
||||
|
||||
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
|
||||
lua_lock(L);
|
||||
if (newthreshold > GCscale(ULONG_MAX))
|
||||
G(L)->GCthreshold = ULONG_MAX;
|
||||
else
|
||||
G(L)->GCthreshold = GCunscale(newthreshold);
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
@@ -379,74 +578,203 @@ void lua_rawcall (lua_State *L, int nargs, int nresults) {
|
||||
** miscellaneous functions
|
||||
*/
|
||||
|
||||
void lua_settag (lua_State *L, int tag) {
|
||||
luaT_realtag(L, tag);
|
||||
LUA_API int lua_newtype (lua_State *L, const l_char *name, int basictype) {
|
||||
int tag;
|
||||
lua_lock(L);
|
||||
if (basictype != LUA_TNONE &&
|
||||
basictype != LUA_TTABLE &&
|
||||
basictype != LUA_TUSERDATA)
|
||||
luaO_verror(L, l_s("invalid basic type (%d) for new type"), basictype);
|
||||
tag = luaT_newtag(L, name, basictype);
|
||||
if (tag == LUA_TNONE)
|
||||
luaO_verror(L, l_s("type name '%.30s' already exists"), name);
|
||||
lua_unlock(L);
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_name2tag (lua_State *L, const l_char *name) {
|
||||
int tag;
|
||||
const TObject *v;
|
||||
lua_lock(L);
|
||||
v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
|
||||
if (ttype(v) == LUA_TNIL)
|
||||
tag = LUA_TNONE;
|
||||
else {
|
||||
lua_assert(ttype(v) == LUA_TNUMBER);
|
||||
tag = (int)nvalue(v);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
LUA_API const l_char *lua_tag2name (lua_State *L, int tag) {
|
||||
const l_char *s;
|
||||
lua_lock(L);
|
||||
s = (tag == LUA_TNONE) ? l_s("no value") : typenamebytag(G(L), tag);
|
||||
lua_unlock(L);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settag (lua_State *L, int tag) {
|
||||
int basictype;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
if (tag < 0 || tag >= G(L)->ntag)
|
||||
luaO_verror(L, l_s("%d is not a valid tag"), tag);
|
||||
basictype = G(L)->TMtable[tag].basictype;
|
||||
if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
|
||||
luaO_verror(L, l_s("tag %d can only be used for type '%.20s'"), tag,
|
||||
typenamebytag(G(L), basictype));
|
||||
switch (ttype(L->top-1)) {
|
||||
case TAG_TABLE:
|
||||
case LUA_TTABLE:
|
||||
hvalue(L->top-1)->htag = tag;
|
||||
break;
|
||||
case TAG_USERDATA:
|
||||
tsvalue(L->top-1)->u.d.tag = tag;
|
||||
case LUA_TUSERDATA:
|
||||
uvalue(L->top-1)->uv.tag = tag;
|
||||
break;
|
||||
default:
|
||||
luaO_verror(L, "cannot change the tag of a %.20s",
|
||||
luaO_typename(L->top-1));
|
||||
luaO_verror(L, l_s("cannot change the tag of a %.20s"),
|
||||
luaT_typename(G(L), L->top-1));
|
||||
}
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (lua_State *L, int ref) {
|
||||
LUA_API void lua_error (lua_State *L, const l_char *s) {
|
||||
lua_lock(L);
|
||||
luaD_error(L, s);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_unref (lua_State *L, int ref) {
|
||||
if (ref >= 0) {
|
||||
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
|
||||
L->refArray[ref].st = L->refFree;
|
||||
L->refFree = ref;
|
||||
lua_getregistry(L);
|
||||
lua_pushnil(L);
|
||||
lua_rawseti(L, -2, ref);
|
||||
lua_getweakregistry(L);
|
||||
lua_pushnil(L);
|
||||
lua_rawseti(L, -2, ref);
|
||||
lua_pop(L, 2); /* remove both registries */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int lua_next (lua_State *L, int index) {
|
||||
StkId t = Index(L, index);
|
||||
LUA_API int lua_next (lua_State *L, int index) {
|
||||
StkId t;
|
||||
Node *n;
|
||||
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
|
||||
n = luaH_next(L, hvalue(t), Index(L, -1));
|
||||
int more;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttype(t) == LUA_TTABLE);
|
||||
n = luaH_next(L, hvalue(t), luaA_index(L, -1));
|
||||
if (n) {
|
||||
*(L->top-1) = *key(n);
|
||||
*L->top = *val(n);
|
||||
setobj(L->top-1, key(n));
|
||||
setobj(L->top, val(n));
|
||||
api_incr_top(L);
|
||||
return 1;
|
||||
more = 1;
|
||||
}
|
||||
else { /* no more elements */
|
||||
L->top -= 1; /* remove key */
|
||||
return 0;
|
||||
more = 0;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return more;
|
||||
}
|
||||
|
||||
|
||||
int lua_getn (lua_State *L, int index) {
|
||||
Hash *h = hvalue(Index(L, index));
|
||||
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
|
||||
if (ttype(value) == TAG_NUMBER)
|
||||
return (int)nvalue(value);
|
||||
LUA_API int lua_getn (lua_State *L, int index) {
|
||||
StkId t;
|
||||
const TObject *value;
|
||||
int n;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttype(t) == LUA_TTABLE);
|
||||
value = luaH_getstr(hvalue(t), luaS_newliteral(L, l_s("n"))); /* = t.n */
|
||||
if (ttype(value) == LUA_TNUMBER)
|
||||
n = (int)nvalue(value);
|
||||
else {
|
||||
Number max = 0;
|
||||
int i = h->size;
|
||||
Node *n = h->node;
|
||||
lua_Number max = 0;
|
||||
int i = hvalue(t)->size;
|
||||
Node *nd = hvalue(t)->node;
|
||||
while (i--) {
|
||||
if (ttype(key(n)) == TAG_NUMBER &&
|
||||
ttype(val(n)) != TAG_NIL &&
|
||||
nvalue(key(n)) > max)
|
||||
max = nvalue(key(n));
|
||||
n++;
|
||||
if (ttype(key(nd)) == LUA_TNUMBER &&
|
||||
ttype(val(nd)) != LUA_TNIL &&
|
||||
nvalue(key(nd)) > max)
|
||||
max = nvalue(key(nd));
|
||||
nd++;
|
||||
}
|
||||
return (int)max;
|
||||
n = (int)max;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
void lua_concat (lua_State *L, int n) {
|
||||
StkId top = L->top;
|
||||
luaV_strconc(L, n, top);
|
||||
L->top = top-(n-1);
|
||||
LUA_API void lua_concat (lua_State *L, int n) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
if (n >= 2) {
|
||||
luaV_strconc(L, n, L->top);
|
||||
L->top -= (n-1);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
else if (n == 0) { /* push null string */
|
||||
setsvalue(L->top, luaS_newlstr(L, NULL, 0));
|
||||
api_incr_top(L);
|
||||
}
|
||||
/* else n == 1; nothing to do */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
static Udata *pushnewudata (lua_State *L, size_t size) {
|
||||
Udata *u = luaS_newudata(L, size);
|
||||
setuvalue(L->top, u);
|
||||
api_incr_top(L);
|
||||
return uvalue(L->top-1);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
||||
Udata *u;
|
||||
void *p;
|
||||
lua_lock(L);
|
||||
u = pushnewudata(L, size);
|
||||
p = u->uv.value;
|
||||
lua_unlock(L);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_newuserdatabox (lua_State *L, void *p) {
|
||||
Udata *u;
|
||||
lua_lock(L);
|
||||
u = pushnewudata(L, 0);
|
||||
u->uv.value = p;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getweakmode (lua_State *L, int index) {
|
||||
StkId t;
|
||||
int mode;
|
||||
lua_lock(L);
|
||||
t = luaA_index(L, index);
|
||||
api_check(L, ttype(t) == LUA_TTABLE);
|
||||
mode = hvalue(t)->weakmode;
|
||||
lua_unlock(L);
|
||||
return mode;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_setweakmode (lua_State *L, int mode) {
|
||||
lua_lock(L);
|
||||
api_check(L, ttype(L->top-1) == LUA_TTABLE);
|
||||
hvalue(L->top-1)->weakmode = mode;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
122
lauxlib.c
122
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.50 2001/04/23 16:35:45 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -14,14 +14,16 @@
|
||||
** With care, these functions can be used by other libraries.
|
||||
*/
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
int luaL_findstring (const char *name, const char *const list[]) {
|
||||
LUALIB_API int luaL_findstring (const l_char *name, const l_char *const list[]) {
|
||||
int i;
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
@@ -29,87 +31,96 @@ int luaL_findstring (const char *name, const char *const list[]) {
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||
LUALIB_API void luaL_argerror (lua_State *L, int narg, const l_char *extramsg) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 0, &ar);
|
||||
lua_getinfo(L, "n", &ar);
|
||||
lua_getinfo(L, l_s("n"), &ar);
|
||||
if (ar.name == NULL)
|
||||
ar.name = "?";
|
||||
luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
|
||||
ar.name = l_s("?");
|
||||
luaL_verror(L, l_s("bad argument #%d to `%.50s' (%.100s)"),
|
||||
narg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
static void type_error (lua_State *L, int narg, const char *type_name) {
|
||||
char buff[100];
|
||||
const char *rt = lua_type(L, narg);
|
||||
if (*rt == 'N') rt = "no value";
|
||||
sprintf(buff, "%.10s expected, got %.10s", type_name, rt);
|
||||
static void type_error (lua_State *L, int narg, const l_char *tname) {
|
||||
l_char buff[80];
|
||||
sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
|
||||
luaL_argerror(L, narg, buff);
|
||||
}
|
||||
|
||||
|
||||
void luaL_checkstack (lua_State *L, int space, const char *mes) {
|
||||
static void tag_error (lua_State *L, int narg, int tag) {
|
||||
type_error(L, narg, lua_typename(L, tag));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const l_char *mes) {
|
||||
if (space > lua_stackspace(L))
|
||||
luaL_verror(L, "stack overflow (%.30s)", mes);
|
||||
luaL_verror(L, l_s("stack overflow (%.30s)"), mes);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** use the 3rd letter of type names for testing:
|
||||
** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
|
||||
*/
|
||||
void luaL_checktype(lua_State *L, int narg, const char *tname) {
|
||||
const char *rt = lua_type(L, narg);
|
||||
if (!(*rt != 'N' && (tname[2] == 'y' || tname[2] == rt[2])))
|
||||
type_error(L, narg, tname);
|
||||
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
|
||||
if (lua_rawtag(L, narg) != t)
|
||||
tag_error(L, narg, t);
|
||||
}
|
||||
|
||||
|
||||
static const char *checkstr (lua_State *L, int narg, size_t *len) {
|
||||
const char *s = lua_tostring(L, narg);
|
||||
if (!s) type_error(L, narg, "string");
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
|
||||
if (lua_rawtag(L, narg) == LUA_TNONE)
|
||||
luaL_argerror(L, narg, l_s("value expected"));
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
|
||||
const l_char *name) {
|
||||
if (strcmp(lua_type(L, narg), name) != 0)
|
||||
type_error(L, narg, name);
|
||||
return lua_touserdata(L, narg);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const l_char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
|
||||
const l_char *s = lua_tostring(L, narg);
|
||||
if (!s) tag_error(L, narg, LUA_TSTRING);
|
||||
if (len) *len = lua_strlen(L, narg);
|
||||
return s;
|
||||
}
|
||||
|
||||
const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
|
||||
return checkstr(L, narg, len);
|
||||
}
|
||||
|
||||
const char *luaL_opt_lstr (lua_State *L, int narg, const char *def,
|
||||
size_t *len) {
|
||||
LUALIB_API const l_char *luaL_opt_lstr (lua_State *L, int narg, const l_char *def, size_t *len) {
|
||||
if (lua_isnull(L, narg)) {
|
||||
if (len) *len = def ? strlen(def) : 0;
|
||||
if (len)
|
||||
*len = (def ? strlen(def) : 0);
|
||||
return def;
|
||||
}
|
||||
else return checkstr(L, narg, len);
|
||||
}
|
||||
|
||||
double luaL_check_number (lua_State *L, int narg) {
|
||||
if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
|
||||
return lua_tonumber(L, narg);
|
||||
else return luaL_check_lstr(L, narg, len);
|
||||
}
|
||||
|
||||
|
||||
double luaL_opt_number (lua_State *L, int narg, double def) {
|
||||
LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
|
||||
lua_Number d = lua_tonumber(L, narg);
|
||||
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
|
||||
tag_error(L, narg, LUA_TNUMBER);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
|
||||
if (lua_isnull(L, narg)) return def;
|
||||
else {
|
||||
if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
|
||||
return lua_tonumber(L, narg);
|
||||
}
|
||||
else return luaL_check_number(L, narg);
|
||||
}
|
||||
|
||||
|
||||
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n) {
|
||||
int i;
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(L, l[i].name, l[i].func);
|
||||
}
|
||||
|
||||
|
||||
void luaL_verror (lua_State *L, const char *fmt, ...) {
|
||||
char buff[500];
|
||||
LUALIB_API void luaL_verror (lua_State *L, const l_char *fmt, ...) {
|
||||
l_char buff[500];
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
@@ -157,43 +168,38 @@ static void adjuststack (luaL_Buffer *B) {
|
||||
}
|
||||
else break;
|
||||
} while (toget < B->level);
|
||||
if (toget >= 2) {
|
||||
lua_concat(L, toget);
|
||||
B->level = B->level - toget + 1;
|
||||
}
|
||||
lua_concat(L, toget);
|
||||
B->level = B->level - toget + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char *luaL_prepbuffer (luaL_Buffer *B) {
|
||||
LUALIB_API l_char *luaL_prepbuffer (luaL_Buffer *B) {
|
||||
if (emptybuffer(B))
|
||||
adjuststack(B);
|
||||
return B->buffer;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const l_char *s, size_t l) {
|
||||
while (l--)
|
||||
luaL_putchar(B, *s++);
|
||||
}
|
||||
|
||||
|
||||
void luaL_addstring (luaL_Buffer *B, const char *s) {
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const l_char *s) {
|
||||
luaL_addlstring(B, s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
void luaL_pushresult (luaL_Buffer *B) {
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||
emptybuffer(B);
|
||||
if (B->level == 0)
|
||||
lua_pushlstring(B->L, NULL, 0);
|
||||
else if (B->level > 1)
|
||||
lua_concat(B->L, B->level);
|
||||
lua_concat(B->L, B->level);
|
||||
B->level = 1;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addvalue (luaL_Buffer *B) {
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
|
||||
lua_State *L = B->L;
|
||||
size_t vl = lua_strlen(L, -1);
|
||||
if (vl <= bufffree(B)) { /* fit into buffer? */
|
||||
@@ -210,7 +216,7 @@ void luaL_addvalue (luaL_Buffer *B) {
|
||||
}
|
||||
|
||||
|
||||
void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
||||
B->L = L;
|
||||
B->p = B->buffer;
|
||||
B->level = 0;
|
||||
|
||||
63
lauxlib.h
63
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.35 2001/04/06 21:17:37 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -15,25 +15,36 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
struct luaL_reg {
|
||||
const char *name;
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct luaL_reg {
|
||||
const lua_char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
} luaL_reg;
|
||||
|
||||
|
||||
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
|
||||
void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
||||
const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len);
|
||||
const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def,
|
||||
size_t *len);
|
||||
double luaL_check_number (lua_State *L, int numArg);
|
||||
double luaL_opt_number (lua_State *L, int numArg, double def);
|
||||
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n);
|
||||
LUALIB_API void luaL_argerror (lua_State *L, int numarg,
|
||||
const lua_char *extramsg);
|
||||
LUALIB_API const lua_char *luaL_check_lstr (lua_State *L, int numArg,
|
||||
size_t *len);
|
||||
LUALIB_API const lua_char *luaL_opt_lstr (lua_State *L, int numArg,
|
||||
const lua_char *def, size_t *len);
|
||||
LUALIB_API lua_Number luaL_check_number (lua_State *L, int numArg);
|
||||
LUALIB_API lua_Number luaL_opt_number (lua_State *L, int nArg, lua_Number def);
|
||||
|
||||
void luaL_checkstack (lua_State *L, int space, const char *msg);
|
||||
void luaL_checktype (lua_State *L, int narg, const char *tname);
|
||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const lua_char *msg);
|
||||
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t);
|
||||
LUALIB_API void luaL_checkany (lua_State *L, int narg);
|
||||
LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
|
||||
const lua_char *name);
|
||||
|
||||
void luaL_verror (lua_State *L, const char *fmt, ...);
|
||||
int luaL_findstring (const char *name, const char *const list[]);
|
||||
LUALIB_API void luaL_verror (lua_State *L, const lua_char *fmt, ...);
|
||||
LUALIB_API int luaL_findstring (const lua_char *name,
|
||||
const lua_char *const list[]);
|
||||
|
||||
|
||||
|
||||
@@ -61,28 +72,30 @@ int luaL_findstring (const char *name, const char *const list[]);
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LUAL_BUFFERSIZE
|
||||
#define LUAL_BUFFERSIZE BUFSIZ
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct luaL_Buffer {
|
||||
char *p; /* current position in buffer */
|
||||
lua_char *p; /* current position in buffer */
|
||||
int level;
|
||||
lua_State *L;
|
||||
char buffer[LUAL_BUFFERSIZE];
|
||||
lua_char buffer[LUAL_BUFFERSIZE];
|
||||
} luaL_Buffer;
|
||||
|
||||
#define luaL_putchar(B,c) \
|
||||
((void)((B)->p < &(B)->buffer[LUAL_BUFFERSIZE] || luaL_prepbuffer(B)), \
|
||||
(*(B)->p++ = (char)(c)))
|
||||
((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
|
||||
(*(B)->p++ = (lua_char)(c)))
|
||||
|
||||
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||
|
||||
void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||
char *luaL_prepbuffer (luaL_Buffer *B);
|
||||
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
|
||||
void luaL_addstring (luaL_Buffer *B, const char *s);
|
||||
void luaL_addvalue (luaL_Buffer *B);
|
||||
void luaL_pushresult (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||
LUALIB_API lua_char *luaL_prepbuffer (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const lua_char *s, size_t l);
|
||||
LUALIB_API void luaL_addstring (luaL_Buffer *B, const lua_char *s);
|
||||
LUALIB_API void luaL_addvalue (luaL_Buffer *B);
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B);
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
605
lbaselib.c
605
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.38 2001/06/20 17:25:30 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -19,6 +20,13 @@
|
||||
|
||||
|
||||
|
||||
static void aux_setn (lua_State *L, int t, int n) {
|
||||
lua_pushliteral(L, l_s("n"));
|
||||
lua_pushnumber(L, n);
|
||||
lua_settable(L, t);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support `stderr', redefine this function, or
|
||||
** redefine _ERRORMESSAGE so that it won't need _ALERT.
|
||||
@@ -34,22 +42,22 @@ static int luaB__ALERT (lua_State *L) {
|
||||
** The library `liolib' redefines _ERRORMESSAGE for better error information.
|
||||
*/
|
||||
static int luaB__ERRORMESSAGE (lua_State *L) {
|
||||
luaL_checktype(L, 1, "string");
|
||||
lua_getglobal(L, LUA_ALERT);
|
||||
luaL_checktype(L, 1, LUA_TSTRING);
|
||||
lua_getglobal(L, l_s(LUA_ALERT));
|
||||
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
|
||||
lua_Debug ar;
|
||||
lua_pushstring(L, "error: ");
|
||||
lua_pushliteral(L, l_s("error: "));
|
||||
lua_pushvalue(L, 1);
|
||||
if (lua_getstack(L, 1, &ar)) {
|
||||
lua_getinfo(L, "Sl", &ar);
|
||||
lua_getinfo(L, l_s("Sl"), &ar);
|
||||
if (ar.source && ar.currentline > 0) {
|
||||
char buff[100];
|
||||
sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline);
|
||||
l_char buff[100];
|
||||
sprintf(buff, l_s("\n <%.70s: line %d>"), ar.short_src, ar.currentline);
|
||||
lua_pushstring(L, buff);
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
}
|
||||
lua_pushstring(L, "\n");
|
||||
lua_pushliteral(L, l_s("\n"));
|
||||
lua_concat(L, 3);
|
||||
lua_rawcall(L, 1, 0);
|
||||
}
|
||||
@@ -66,20 +74,20 @@ static int luaB__ERRORMESSAGE (lua_State *L) {
|
||||
static int luaB_print (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
lua_getglobal(L, "tostring");
|
||||
lua_getglobal(L, l_s("tostring"));
|
||||
for (i=1; i<=n; i++) {
|
||||
const char *s;
|
||||
const l_char *s;
|
||||
lua_pushvalue(L, -1); /* function to be called */
|
||||
lua_pushvalue(L, i); /* value to print */
|
||||
lua_rawcall(L, 1, 1);
|
||||
s = lua_tostring(L, -1); /* get result */
|
||||
if (s == NULL)
|
||||
lua_error(L, "`tostring' must return a string to `print'");
|
||||
if (i>1) fputs("\t", stdout);
|
||||
lua_error(L, l_s("`tostring' must return a string to `print'"));
|
||||
if (i>1) fputs(l_s("\t"), stdout);
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
fputs(l_s("\n"), stdout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -87,21 +95,21 @@ static int luaB_print (lua_State *L) {
|
||||
static int luaB_tonumber (lua_State *L) {
|
||||
int base = luaL_opt_int(L, 2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
luaL_checktype(L, 1, "any");
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnumber(L, 1)) {
|
||||
lua_pushnumber(L, lua_tonumber(L, 1));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const char *s1 = luaL_check_string(L, 1);
|
||||
char *s2;
|
||||
const l_char *s1 = luaL_check_string(L, 1);
|
||||
l_char *s2;
|
||||
unsigned long n;
|
||||
luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
luaL_arg_check(L, 2 <= base && base <= 36, 2, l_s("base out of range"));
|
||||
n = strtoul(s1, &s2, base);
|
||||
if (s1 != s2) { /* at least one valid digit? */
|
||||
while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
|
||||
if (*s2 == '\0') { /* no invalid trailing characters? */
|
||||
while (isspace(uchar(*s2))) s2++; /* skip trailing spaces */
|
||||
if (*s2 == l_c('\0')) { /* no invalid trailing characters? */
|
||||
lua_pushnumber(L, n);
|
||||
return 1;
|
||||
}
|
||||
@@ -118,7 +126,7 @@ static int luaB_error (lua_State *L) {
|
||||
}
|
||||
|
||||
static int luaB_setglobal (lua_State *L) {
|
||||
luaL_checktype(L, 2, "any");
|
||||
luaL_checkany(L, 2);
|
||||
lua_setglobal(L, luaL_check_string(L, 1));
|
||||
return 0;
|
||||
}
|
||||
@@ -128,35 +136,77 @@ static int luaB_getglobal (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* auxiliary function to get `tags' */
|
||||
static int gettag (lua_State *L, int narg) {
|
||||
switch (lua_rawtag(L, narg)) {
|
||||
case LUA_TNUMBER:
|
||||
return (int)lua_tonumber(L, narg);
|
||||
case LUA_TSTRING: {
|
||||
const l_char *name = lua_tostring(L, narg);
|
||||
int tag = lua_name2tag(L, name);
|
||||
if (tag == LUA_TNONE)
|
||||
luaL_verror(L, l_s("'%.30s' is not a valid type name"), name);
|
||||
return tag;
|
||||
}
|
||||
default:
|
||||
luaL_argerror(L, narg, l_s("tag or type name expected"));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tag (lua_State *L) {
|
||||
luaL_checktype(L, 1, "any");
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnumber(L, lua_tag(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_settag (lua_State *L) {
|
||||
luaL_checktype(L, 1, "table");
|
||||
static int luaB_settype (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushvalue(L, 1); /* push table */
|
||||
lua_settag(L, luaL_check_int(L, 2));
|
||||
lua_pop(L, 1); /* remove second argument */
|
||||
return 1; /* return first argument */
|
||||
lua_settag(L, gettag(L, 2));
|
||||
return 1; /* return table */
|
||||
}
|
||||
|
||||
static int luaB_newtag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_newtag(L));
|
||||
static int luaB_weakmode (lua_State *L) {
|
||||
const l_char *mode = luaL_check_string(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
if (*mode == l_c('?')) {
|
||||
l_char buff[3];
|
||||
l_char *s = buff;
|
||||
int imode = lua_getweakmode(L, 1);
|
||||
if (imode & LUA_WEAK_KEY) *s++ = 'k';
|
||||
if (imode & LUA_WEAK_VALUE) *s++ = 'v';
|
||||
*s = '\0';
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int imode = 0;
|
||||
if (strchr(mode, l_c('k'))) imode |= LUA_WEAK_KEY;
|
||||
if (strchr(mode, l_c('v'))) imode |= LUA_WEAK_VALUE;
|
||||
lua_pushvalue(L, 1); /* push table */
|
||||
lua_setweakmode(L, imode);
|
||||
return 1; /* return the table */
|
||||
}
|
||||
}
|
||||
|
||||
static int luaB_newtype (lua_State *L) {
|
||||
const l_char *name = luaL_opt_string(L, 1, NULL);
|
||||
lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_copytagmethods (lua_State *L) {
|
||||
lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
|
||||
luaL_check_int(L, 2)));
|
||||
lua_pushnumber(L, lua_copytagmethods(L, gettag(L, 1), gettag(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_globals (lua_State *L) {
|
||||
lua_getglobals(L); /* value to be returned */
|
||||
if (!lua_isnull(L, 1)) {
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushvalue(L, 1); /* new table of globals */
|
||||
lua_setglobals(L);
|
||||
}
|
||||
@@ -164,54 +214,73 @@ static int luaB_globals (lua_State *L) {
|
||||
}
|
||||
|
||||
static int luaB_rawget (lua_State *L) {
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 2, "any");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
lua_rawget(L, -2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_rawset (lua_State *L) {
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 2, "any");
|
||||
luaL_checktype(L, 3, "any");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checkany(L, 2);
|
||||
luaL_checkany(L, 3);
|
||||
lua_rawset(L, -3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_settagmethod (lua_State *L) {
|
||||
int tag = (int)luaL_check_int(L, 1);
|
||||
const char *event = luaL_check_string(L, 2);
|
||||
int tag = gettag(L, 1);
|
||||
const l_char *event = luaL_check_string(L, 2);
|
||||
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
|
||||
"function or nil expected");
|
||||
lua_pushnil(L); /* to get its tag */
|
||||
if (strcmp(event, "gc") == 0 && tag != lua_tag(L, -1))
|
||||
lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
|
||||
lua_pop(L, 1); /* remove the nil */
|
||||
l_s("function or nil expected"));
|
||||
if (strcmp(event, l_s("gc")) == 0)
|
||||
lua_error(L, l_s("deprecated use: cannot set the `gc' tag method from Lua"));
|
||||
lua_gettagmethod(L, tag, event);
|
||||
lua_pushvalue(L, 3);
|
||||
lua_settagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gettagmethod (lua_State *L) {
|
||||
lua_gettagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
|
||||
int tag = gettag(L, 1);
|
||||
const l_char *event = luaL_check_string(L, 2);
|
||||
if (strcmp(event, l_s("gc")) == 0)
|
||||
lua_error(L, l_s("deprecated use: cannot get the `gc' tag method from Lua"));
|
||||
lua_gettagmethod(L, tag, event);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_gcinfo (lua_State *L) {
|
||||
lua_pushnumber(L, lua_getgccount(L));
|
||||
lua_pushnumber(L, lua_getgcthreshold(L));
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_collectgarbage (lua_State *L) {
|
||||
lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
|
||||
return 1;
|
||||
lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_type (lua_State *L) {
|
||||
luaL_checktype(L, 1, "any");
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, lua_type(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_rawtype (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, lua_tag2name(L, lua_rawtag(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_next (lua_State *L) {
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
||||
if (lua_next(L, 1))
|
||||
return 2;
|
||||
@@ -223,14 +292,15 @@ static int luaB_next (lua_State *L) {
|
||||
|
||||
|
||||
static int passresults (lua_State *L, int status, int oldtop) {
|
||||
static const char *const errornames[] =
|
||||
{"ok", "run-time error", "file error", "syntax error", "memory error"};
|
||||
static const l_char *const errornames[] =
|
||||
{l_s("ok"), l_s("run-time error"), l_s("file error"), l_s("syntax error"),
|
||||
l_s("memory error"), l_s("error in error handling")};
|
||||
if (status == 0) {
|
||||
int nresults = lua_gettop(L) - oldtop;
|
||||
if (nresults > 0)
|
||||
return nresults; /* results are already on the stack */
|
||||
else {
|
||||
lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
|
||||
lua_newuserdatabox(L, NULL); /* at least one result to signal no errors */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -241,84 +311,180 @@ static int passresults (lua_State *L, int status, int oldtop) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int luaB_dostring (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
if (*s == '\27') /* binary files start with ESC... */
|
||||
lua_error(L, "`dostring' cannot run pre-compiled code");
|
||||
return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop);
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
const l_char *chunkname = luaL_opt_string(L, 2, s);
|
||||
return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadstring (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
size_t l;
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
const l_char *chunkname = luaL_opt_string(L, 2, s);
|
||||
return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
|
||||
}
|
||||
|
||||
static int luaB_dofile (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
const char *fname = luaL_opt_string(L, 1, NULL);
|
||||
const l_char *fname = luaL_opt_string(L, 1, NULL);
|
||||
return passresults(L, lua_dofile(L, fname), oldtop);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadfile (lua_State *L) {
|
||||
int oldtop = lua_gettop(L);
|
||||
const l_char *fname = luaL_opt_string(L, 1, NULL);
|
||||
return passresults(L, lua_loadfile(L, fname), oldtop);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define LUA_PATH l_s("LUA_PATH")
|
||||
|
||||
#define LUA_PATH_SEP l_s(";")
|
||||
|
||||
#ifndef LUA_PATH_DEFAULT
|
||||
#define LUA_PATH_DEFAULT l_s("./")
|
||||
#endif
|
||||
|
||||
static int luaB_require (lua_State *L) {
|
||||
const l_char *path;
|
||||
luaL_check_string(L, 1);
|
||||
lua_settop(L, 1);
|
||||
lua_getglobal(L, LUA_PATH); /* get path */
|
||||
if (lua_isstring(L, 2)) /* is LUA_PATH defined? */
|
||||
path = lua_tostring(L, 2);
|
||||
else { /* LUA_PATH not defined */
|
||||
lua_pop(L, 1); /* pop old global value */
|
||||
path = getenv(LUA_PATH); /* try environment variable */
|
||||
if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */
|
||||
lua_pushstring(L, path);
|
||||
lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */
|
||||
lua_setglobal(L, LUA_PATH);
|
||||
}
|
||||
lua_getregistry(L);
|
||||
lua_pushliteral(L, LUA_PATH);
|
||||
lua_gettable(L, 3); /* get book-keeping table */
|
||||
if (lua_isnil(L, 4)) { /* no book-keeping table? */
|
||||
lua_pop(L, 1); /* pop the `nil' */
|
||||
lua_newtable(L); /* create book-keeping table */
|
||||
lua_pushliteral(L, LUA_PATH);
|
||||
lua_pushvalue(L, -2); /* duplicate table to leave a copy on stack */
|
||||
lua_settable(L, 3); /* store book-keeping table in registry */
|
||||
}
|
||||
lua_pushvalue(L, 1);
|
||||
lua_gettable(L, 4); /* check package's name in book-keeping table */
|
||||
if (!lua_isnil(L, -1)) /* is it there? */
|
||||
return 0; /* package is already loaded */
|
||||
else { /* must load it */
|
||||
for (;;) { /* traverse path */
|
||||
int res;
|
||||
int l = strcspn(path, LUA_PATH_SEP); /* find separator */
|
||||
lua_pushlstring(L, path, l); /* directory name */
|
||||
lua_pushvalue(L, 1); /* package name */
|
||||
lua_concat(L, 2); /* concat directory with package name */
|
||||
res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */
|
||||
lua_settop(L, 4); /* pop string and eventual results from dofile */
|
||||
if (res == 0) break; /* ok; file done */
|
||||
else if (res != LUA_ERRFILE)
|
||||
lua_error(L, NULL); /* error running package; propagate it */
|
||||
if (*(path+l) == l_c('\0')) /* no more directories? */
|
||||
luaL_verror(L, l_s("could not load package `%.20s' from path `%.200s'"),
|
||||
lua_tostring(L, 1), lua_tostring(L, 2));
|
||||
path += l+1; /* try next directory */
|
||||
}
|
||||
}
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushnumber(L, 1);
|
||||
lua_settable(L, 4); /* mark it as loaded */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int aux_unpack (lua_State *L, int arg) {
|
||||
int n, i;
|
||||
luaL_checktype(L, arg, LUA_TTABLE);
|
||||
n = lua_getn(L, arg);
|
||||
luaL_checkstack(L, n, l_s("table too big to unpack"));
|
||||
for (i=1; i<=n; i++) /* push arg[1...n] */
|
||||
lua_rawgeti(L, arg, i);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_unpack (lua_State *L) {
|
||||
return aux_unpack(L, 1);
|
||||
}
|
||||
|
||||
|
||||
static int luaB_call (lua_State *L) {
|
||||
int oldtop;
|
||||
const char *options = luaL_opt_string(L, 3, "");
|
||||
const l_char *options = luaL_opt_string(L, 3, l_s(""));
|
||||
int err = 0; /* index of old error method */
|
||||
int i, status;
|
||||
int status;
|
||||
int n;
|
||||
luaL_checktype(L, 2, "table");
|
||||
n = lua_getn(L, 2);
|
||||
if (!lua_isnull(L, 4)) { /* set new error method */
|
||||
lua_getglobal(L, LUA_ERRORMESSAGE);
|
||||
lua_getglobal(L, l_s(LUA_ERRORMESSAGE));
|
||||
err = lua_gettop(L); /* get index */
|
||||
lua_pushvalue(L, 4);
|
||||
lua_setglobal(L, LUA_ERRORMESSAGE);
|
||||
lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
|
||||
}
|
||||
oldtop = lua_gettop(L); /* top before function-call preparation */
|
||||
/* push function */
|
||||
lua_pushvalue(L, 1);
|
||||
luaL_checkstack(L, n, "too many arguments");
|
||||
for (i=0; i<n; i++) /* push arg[1...n] */
|
||||
lua_rawgeti(L, 2, i+1);
|
||||
n = aux_unpack(L, 2); /* push arg[1...n] */
|
||||
status = lua_call(L, n, LUA_MULTRET);
|
||||
if (err != 0) { /* restore old error method */
|
||||
lua_pushvalue(L, err);
|
||||
lua_setglobal(L, LUA_ERRORMESSAGE);
|
||||
lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, 'x'))
|
||||
if (strchr(options, l_c('x')))
|
||||
lua_pushnil(L); /* return nil to signal the error */
|
||||
else
|
||||
lua_error(L, NULL); /* propagate error without additional messages */
|
||||
return 1;
|
||||
}
|
||||
if (strchr(options, 'p')) /* pack results? */
|
||||
lua_error(L, "deprecated option `p' in `call'");
|
||||
if (strchr(options, l_c('p'))) /* pack results? */
|
||||
lua_error(L, l_s("deprecated option `p' in `call'"));
|
||||
return lua_gettop(L) - oldtop; /* results are already on the stack */
|
||||
}
|
||||
|
||||
|
||||
static int luaB_tostring (lua_State *L) {
|
||||
char buff[64];
|
||||
switch (lua_type(L, 1)[2]) {
|
||||
case 'm': /* nuMber */
|
||||
l_char buff[64];
|
||||
switch (lua_rawtag(L, 1)) {
|
||||
case LUA_TNUMBER:
|
||||
lua_pushstring(L, lua_tostring(L, 1));
|
||||
return 1;
|
||||
case 'r': /* stRing */
|
||||
case LUA_TSTRING:
|
||||
lua_pushvalue(L, 1);
|
||||
return 1;
|
||||
case 'b': /* taBle */
|
||||
sprintf(buff, "table: %p", lua_topointer(L, 1));
|
||||
case LUA_TTABLE:
|
||||
sprintf(buff, l_s("%.40s: %p"), lua_type(L, 1), lua_topointer(L, 1));
|
||||
break;
|
||||
case 'n': /* fuNction */
|
||||
sprintf(buff, "function: %p", lua_topointer(L, 1));
|
||||
case LUA_TFUNCTION:
|
||||
sprintf(buff, l_s("function: %p"), lua_topointer(L, 1));
|
||||
break;
|
||||
case 'e': /* usErdata */
|
||||
sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1));
|
||||
case LUA_TUSERDATA: {
|
||||
const l_char *t = lua_type(L, 1);
|
||||
if (strcmp(t, l_s("userdata")) == 0)
|
||||
sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1),
|
||||
lua_touserdata(L, 1));
|
||||
else
|
||||
sprintf(buff, l_s("%.40s: %p"), t, lua_touserdata(L, 1));
|
||||
break;
|
||||
case 'l': /* niL */
|
||||
lua_pushstring(L, "nil");
|
||||
}
|
||||
case LUA_TNIL:
|
||||
lua_pushliteral(L, l_s("nil"));
|
||||
return 1;
|
||||
default:
|
||||
luaL_argerror(L, 1, "value expected");
|
||||
luaL_argerror(L, 1, l_s("value expected"));
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return 1;
|
||||
@@ -327,8 +493,8 @@ static int luaB_tostring (lua_State *L) {
|
||||
|
||||
static int luaB_foreachi (lua_State *L) {
|
||||
int n, i;
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 2, "function");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
n = lua_getn(L, 1);
|
||||
for (i=1; i<=n; i++) {
|
||||
lua_pushvalue(L, 2); /* function */
|
||||
@@ -344,8 +510,8 @@ static int luaB_foreachi (lua_State *L) {
|
||||
|
||||
|
||||
static int luaB_foreach (lua_State *L) {
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 2, "function");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_pushnil(L); /* first index */
|
||||
for (;;) {
|
||||
if (lua_next(L, 1) == 0)
|
||||
@@ -362,15 +528,16 @@ static int luaB_foreach (lua_State *L) {
|
||||
|
||||
|
||||
static int luaB_assert (lua_State *L) {
|
||||
luaL_checktype(L, 1, "any");
|
||||
luaL_checkany(L, 1);
|
||||
if (lua_isnil(L, 1))
|
||||
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
|
||||
return 0;
|
||||
luaL_verror(L, l_s("assertion failed! %.90s"), luaL_opt_string(L, 2, l_s("")));
|
||||
lua_settop(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_getn (lua_State *L) {
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushnumber(L, lua_getn(L, 1));
|
||||
return 1;
|
||||
}
|
||||
@@ -379,15 +546,13 @@ static int luaB_getn (lua_State *L) {
|
||||
static int luaB_tinsert (lua_State *L) {
|
||||
int v = lua_gettop(L); /* last argument: to be inserted */
|
||||
int n, pos;
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
if (v == 2) /* called with only 2 arguments */
|
||||
pos = n+1;
|
||||
else
|
||||
pos = luaL_check_int(L, 2); /* 2nd argument is the position */
|
||||
lua_pushstring(L, "n");
|
||||
lua_pushnumber(L, n+1);
|
||||
lua_rawset(L, 1); /* t.n = n+1 */
|
||||
aux_setn(L, 1, n+1); /* t.n = n+1 */
|
||||
for (; n>=pos; n--) {
|
||||
lua_rawgeti(L, 1, n);
|
||||
lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
|
||||
@@ -400,18 +565,16 @@ static int luaB_tinsert (lua_State *L) {
|
||||
|
||||
static int luaB_tremove (lua_State *L) {
|
||||
int pos, n;
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
pos = luaL_opt_int(L, 2, n);
|
||||
if (n <= 0) return 0; /* table is "empty" */
|
||||
if (n <= 0) return 0; /* table is `empty' */
|
||||
aux_setn(L, 1, n-1); /* t.n = n-1 */
|
||||
lua_rawgeti(L, 1, pos); /* result = t[pos] */
|
||||
for ( ;pos<n; pos++) {
|
||||
lua_rawgeti(L, 1, pos+1);
|
||||
lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
|
||||
}
|
||||
lua_pushstring(L, "n");
|
||||
lua_pushnumber(L, n-1);
|
||||
lua_rawset(L, 1); /* t.n = n-1 */
|
||||
lua_pushnil(L);
|
||||
lua_rawseti(L, 1, n); /* t[n] = nil */
|
||||
return 1;
|
||||
@@ -428,94 +591,96 @@ static int luaB_tremove (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
static void swap (lua_State *L, int i, int j) {
|
||||
lua_rawgeti(L, 1, i);
|
||||
lua_rawgeti(L, 1, j);
|
||||
static void set2 (lua_State *L, int i, int j) {
|
||||
lua_rawseti(L, 1, i);
|
||||
lua_rawseti(L, 1, j);
|
||||
}
|
||||
|
||||
static int sort_comp (lua_State *L, int n, int r) {
|
||||
static int sort_comp (lua_State *L, int a, int b) {
|
||||
/* WARNING: the caller (auxsort) must ensure stack space */
|
||||
int res;
|
||||
if (!lua_isnil(L, 2)) { /* function? */
|
||||
int res;
|
||||
lua_pushvalue(L, 2);
|
||||
if (r) {
|
||||
lua_rawgeti(L, 1, n); /* a[n] */
|
||||
lua_pushvalue(L, -3); /* pivot */
|
||||
}
|
||||
else {
|
||||
lua_pushvalue(L, -2); /* pivot */
|
||||
lua_rawgeti(L, 1, n); /* a[n] */
|
||||
}
|
||||
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
||||
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
|
||||
lua_rawcall(L, 2, 1);
|
||||
res = !lua_isnil(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
}
|
||||
else { /* a < b? */
|
||||
lua_rawgeti(L, 1, n); /* a[n] */
|
||||
if (r)
|
||||
res = lua_lessthan(L, -1, -2);
|
||||
else
|
||||
res = lua_lessthan(L, -2, -1);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
return res;
|
||||
else /* a < b? */
|
||||
return lua_lessthan(L, a, b);
|
||||
}
|
||||
|
||||
static void auxsort (lua_State *L, int l, int u) {
|
||||
while (l < u) { /* for tail recursion */
|
||||
int i, j;
|
||||
luaL_checkstack(L, 4, "array too large");
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
lua_rawgeti(L, 1, l);
|
||||
lua_rawgeti(L, 1, u);
|
||||
if (sort_comp(L, l, 0)) /* a[u] < a[l]? */
|
||||
swap(L, l, u);
|
||||
lua_pop(L, 1);
|
||||
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
|
||||
set2(L, l, u); /* swap a[l] - a[u] */
|
||||
else
|
||||
lua_pop(L, 2);
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
lua_rawgeti(L, 1, i); /* Pivot = a[i] */
|
||||
if (sort_comp(L, l, 0)) /* a[i]<a[l]? */
|
||||
swap(L, l, i);
|
||||
lua_rawgeti(L, 1, i);
|
||||
lua_rawgeti(L, 1, l);
|
||||
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
|
||||
set2(L, i, l);
|
||||
else {
|
||||
if (sort_comp(L, u, 1)) /* a[u]<a[i]? */
|
||||
swap(L, i, u);
|
||||
lua_pop(L, 1); /* remove a[l] */
|
||||
lua_rawgeti(L, 1, u);
|
||||
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
|
||||
set2(L, i, u);
|
||||
else
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
lua_pop(L, 1); /* pop old a[i] */
|
||||
if (u-l == 2) break; /* only 3 elements */
|
||||
lua_rawgeti(L, 1, i); /* Pivot */
|
||||
swap(L, i, u-1); /* put median element as pivot (a[u-1]) */
|
||||
/* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_rawgeti(L, 1, u-1);
|
||||
set2(L, i, u-1);
|
||||
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
|
||||
i = l; j = u-1;
|
||||
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
||||
/* repeat i++ until a[i] >= P */
|
||||
while (sort_comp(L, ++i, 1))
|
||||
if (i>u) lua_error(L, "invalid order function for sorting");
|
||||
/* repeat j-- until a[j] <= P */
|
||||
while (sort_comp(L, --j, 0))
|
||||
if (j<l) lua_error(L, "invalid order function for sorting");
|
||||
if (j<i) break;
|
||||
swap(L, i, j);
|
||||
/* repeat ++i until a[i] >= P */
|
||||
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||
if (i>u) lua_error(L, l_s("invalid order function for sorting"));
|
||||
lua_pop(L, 1); /* remove a[i] */
|
||||
}
|
||||
/* repeat --j until a[j] <= P */
|
||||
while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||
if (j<l) lua_error(L, l_s("invalid order function for sorting"));
|
||||
lua_pop(L, 1); /* remove a[j] */
|
||||
}
|
||||
if (j<i) {
|
||||
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
|
||||
break;
|
||||
}
|
||||
set2(L, i, j);
|
||||
}
|
||||
swap(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
||||
lua_rawgeti(L, 1, u-1);
|
||||
lua_rawgeti(L, 1, i);
|
||||
set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
||||
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
|
||||
/* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
|
||||
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
|
||||
if (i-l < u-i) {
|
||||
j=l; i=i-1; l=i+2;
|
||||
}
|
||||
else {
|
||||
j=i+1; i=u; u=j-2;
|
||||
}
|
||||
lua_pop(L, 1); /* remove pivot from stack */
|
||||
auxsort(L, j, i); /* call recursively the smaller one */
|
||||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
static int luaB_sort (lua_State *L) {
|
||||
int n;
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
n = lua_getn(L, 1);
|
||||
if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
|
||||
luaL_checktype(L, 2, "function");
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
lua_settop(L, 2); /* make sure there is two arguments */
|
||||
auxsort(L, 1, n);
|
||||
return 0;
|
||||
@@ -525,112 +690,56 @@ static int luaB_sort (lua_State *L) {
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Deprecated functions to manipulate global environment.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
#define num_deprecated 4
|
||||
|
||||
static const struct luaL_reg deprecated_names [num_deprecated] = {
|
||||
{"foreachvar", luaB_foreach},
|
||||
{"nextvar", luaB_next},
|
||||
{"rawgetglobal", luaB_rawget},
|
||||
{"rawsetglobal", luaB_rawset}
|
||||
};
|
||||
|
||||
|
||||
#ifdef LUA_DEPRECATETFUNCS
|
||||
|
||||
/*
|
||||
** call corresponding function inserting `globals' as first argument
|
||||
*/
|
||||
static int deprecated_func (lua_State *L) {
|
||||
lua_insert(L, 1); /* upvalue is the function to be called */
|
||||
lua_getglobals(L);
|
||||
lua_insert(L, 2); /* table of globals is 1o argument */
|
||||
lua_rawcall(L, lua_gettop(L)-1, LUA_MULTRET);
|
||||
return lua_gettop(L); /* return all results */
|
||||
}
|
||||
|
||||
|
||||
static void deprecated_funcs (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<num_deprecated; i++) {
|
||||
lua_pushcfunction(L, deprecated_names[i].func);
|
||||
lua_pushcclosure(L, deprecated_func, 1);
|
||||
lua_setglobal(L, deprecated_names[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
** gives an explicit error in any attempt to call a deprecated function
|
||||
*/
|
||||
static int deprecated_func (lua_State *L) {
|
||||
luaL_verror(L, "function `%.20s' is deprecated", lua_tostring(L, -1));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
static void deprecated_funcs (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<num_deprecated; i++) {
|
||||
lua_pushstring(L, deprecated_names[i].name);
|
||||
lua_pushcclosure(L, deprecated_func, 1);
|
||||
lua_setglobal(L, deprecated_names[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
static const struct luaL_reg base_funcs[] = {
|
||||
{LUA_ALERT, luaB__ALERT},
|
||||
{LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
|
||||
{"call", luaB_call},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"copytagmethods", luaB_copytagmethods},
|
||||
{"dofile", luaB_dofile},
|
||||
{"dostring", luaB_dostring},
|
||||
{"error", luaB_error},
|
||||
{"foreach", luaB_foreach},
|
||||
{"foreachi", luaB_foreachi},
|
||||
{"getglobal", luaB_getglobal},
|
||||
{"gettagmethod", luaB_gettagmethod},
|
||||
{"globals", luaB_globals},
|
||||
{"newtag", luaB_newtag},
|
||||
{"next", luaB_next},
|
||||
{"print", luaB_print},
|
||||
{"rawget", luaB_rawget},
|
||||
{"rawset", luaB_rawset},
|
||||
{"rawgettable", luaB_rawget}, /* for compatibility */
|
||||
{"rawsettable", luaB_rawset}, /* for compatibility */
|
||||
{"setglobal", luaB_setglobal},
|
||||
{"settag", luaB_settag},
|
||||
{"settagmethod", luaB_settagmethod},
|
||||
{"tag", luaB_tag},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
{"assert", luaB_assert},
|
||||
{"getn", luaB_getn},
|
||||
{"sort", luaB_sort},
|
||||
{"tinsert", luaB_tinsert},
|
||||
{"tremove", luaB_tremove}
|
||||
static const luaL_reg base_funcs[] = {
|
||||
{l_s(LUA_ALERT), luaB__ALERT},
|
||||
{l_s(LUA_ERRORMESSAGE), luaB__ERRORMESSAGE},
|
||||
{l_s("call"), luaB_call},
|
||||
{l_s("collectgarbage"), luaB_collectgarbage},
|
||||
{l_s("copytagmethods"), luaB_copytagmethods},
|
||||
{l_s("dofile"), luaB_dofile},
|
||||
{l_s("dostring"), luaB_dostring},
|
||||
{l_s("error"), luaB_error},
|
||||
{l_s("foreach"), luaB_foreach},
|
||||
{l_s("foreachi"), luaB_foreachi},
|
||||
{l_s("gcinfo"), luaB_gcinfo},
|
||||
{l_s("getglobal"), luaB_getglobal},
|
||||
{l_s("gettagmethod"), luaB_gettagmethod},
|
||||
{l_s("globals"), luaB_globals},
|
||||
{l_s("loadfile"), luaB_loadfile},
|
||||
{l_s("loadstring"), luaB_loadstring},
|
||||
{l_s("newtype"), luaB_newtype},
|
||||
{l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */
|
||||
{l_s("next"), luaB_next},
|
||||
{l_s("print"), luaB_print},
|
||||
{l_s("rawget"), luaB_rawget},
|
||||
{l_s("rawset"), luaB_rawset},
|
||||
{l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */
|
||||
{l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */
|
||||
{l_s("rawtype"), luaB_rawtype},
|
||||
{l_s("require"), luaB_require},
|
||||
{l_s("setglobal"), luaB_setglobal},
|
||||
{l_s("settag"), luaB_settype}, /* for compatibility 4.0 */
|
||||
{l_s("settype"), luaB_settype},
|
||||
{l_s("settagmethod"), luaB_settagmethod},
|
||||
{l_s("tag"), luaB_tag},
|
||||
{l_s("tonumber"), luaB_tonumber},
|
||||
{l_s("tostring"), luaB_tostring},
|
||||
{l_s("type"), luaB_type},
|
||||
{l_s("assert"), luaB_assert},
|
||||
{l_s("getn"), luaB_getn},
|
||||
{l_s("sort"), luaB_sort},
|
||||
{l_s("tinsert"), luaB_tinsert},
|
||||
{l_s("tremove"), luaB_tremove},
|
||||
{l_s("unpack"), luaB_unpack},
|
||||
{l_s("weakmode"), luaB_weakmode}
|
||||
};
|
||||
|
||||
|
||||
|
||||
void lua_baselibopen (lua_State *L) {
|
||||
LUALIB_API int lua_baselibopen (lua_State *L) {
|
||||
luaL_openl(L, base_funcs);
|
||||
lua_pushstring(L, LUA_VERSION);
|
||||
lua_setglobal(L, "_VERSION");
|
||||
deprecated_funcs(L);
|
||||
lua_pushliteral(L, l_s(LUA_VERSION));
|
||||
lua_setglobal(L, l_s("_VERSION"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
51
lcode.h
51
lcode.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lcode.h,v 1.15 2000/06/28 20:20:36 roberto Exp roberto $
|
||||
** $Id: lcode.h,v 1.23 2001/06/12 14:36:48 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -31,40 +31,37 @@ typedef enum BinOpr {
|
||||
OPR_NOBINOPR
|
||||
} BinOpr;
|
||||
|
||||
#define binopistest(op) ((op) >= OPR_NE)
|
||||
|
||||
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;
|
||||
|
||||
|
||||
enum Mode {iO, iU, iS, iAB}; /* instruction format */
|
||||
#define luaK_codeAsBc(fs,o,A,sBc) luaK_codeABc(fs,o,A,(sBc)+MAXARG_sBc)
|
||||
|
||||
#define VD 100 /* flag for variable delta */
|
||||
|
||||
extern const struct OpProperties {
|
||||
char mode;
|
||||
unsigned char push;
|
||||
unsigned char pop;
|
||||
} luaK_opproperties[];
|
||||
|
||||
|
||||
void luaK_error (LexState *ls, const char *msg);
|
||||
int luaK_code0 (FuncState *fs, OpCode o);
|
||||
int luaK_code1 (FuncState *fs, OpCode o, int arg1);
|
||||
int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2);
|
||||
void luaK_error (LexState *ls, const l_char *msg);
|
||||
int luaK_codeABc (FuncState *fs, OpCode o, int A, unsigned int Bc);
|
||||
int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
|
||||
void luaK_nil (FuncState *fs, int from, int n);
|
||||
void luaK_reserveregs (FuncState *fs, int n);
|
||||
int luaK_stringk (FuncState *fs, TString *s);
|
||||
void luaK_dischargevars (FuncState *fs, expdesc *e);
|
||||
int luaK_exp2anyreg (FuncState *fs, expdesc *e);
|
||||
void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
||||
void luaK_exp2val (FuncState *fs, expdesc *e);
|
||||
int luaK_exp2RK (FuncState *fs, expdesc *e);
|
||||
void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
|
||||
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *e);
|
||||
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
|
||||
void luaK_setcallreturns (FuncState *fs, expdesc *var, int nresults);
|
||||
int luaK_jump (FuncState *fs);
|
||||
void luaK_patchlist (FuncState *fs, int list, int target);
|
||||
void luaK_fixfor (FuncState *fs, int pc, int dest);
|
||||
void luaK_concat (FuncState *fs, int *l1, int l2);
|
||||
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue);
|
||||
int luaK_getlabel (FuncState *fs);
|
||||
void luaK_deltastack (FuncState *fs, int delta);
|
||||
void luaK_kstr (LexState *ls, int c);
|
||||
void luaK_number (FuncState *fs, Number f);
|
||||
void luaK_adjuststack (FuncState *fs, int n);
|
||||
int luaK_lastisopen (FuncState *fs);
|
||||
void luaK_setcallreturns (FuncState *fs, int nresults);
|
||||
void luaK_tostack (LexState *ls, expdesc *v, int onlyone);
|
||||
void luaK_storevar (LexState *ls, const expdesc *var);
|
||||
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v);
|
||||
void luaK_infix (LexState *ls, BinOpr op, expdesc *v);
|
||||
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2);
|
||||
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v);
|
||||
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
||||
void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
148
ldblib.c
148
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.36 2001/03/26 14:31:49 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -17,14 +18,14 @@
|
||||
|
||||
|
||||
|
||||
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||
static void settabss (lua_State *L, const l_char *i, const l_char *v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushstring(L, v);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_State *L, const char *i, int v) {
|
||||
static void settabsi (lua_State *L, const l_char *i, int v) {
|
||||
lua_pushstring(L, i);
|
||||
lua_pushnumber(L, v);
|
||||
lua_settable(L, -3);
|
||||
@@ -33,8 +34,8 @@ static void settabsi (lua_State *L, const char *i, int v) {
|
||||
|
||||
static int getinfo (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
const char *options = luaL_opt_string(L, 2, "flnSu");
|
||||
char buff[20];
|
||||
const l_char *options = luaL_opt_string(L, 2, l_s("flnSu"));
|
||||
l_char buff[20];
|
||||
if (lua_isnumber(L, 1)) {
|
||||
if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
|
||||
lua_pushnil(L); /* level out of range */
|
||||
@@ -43,35 +44,35 @@ static int getinfo (lua_State *L) {
|
||||
}
|
||||
else if (lua_isfunction(L, 1)) {
|
||||
lua_pushvalue(L, 1);
|
||||
sprintf(buff, ">%.10s", options);
|
||||
sprintf(buff, l_s(">%.10s"), options);
|
||||
options = buff;
|
||||
}
|
||||
else
|
||||
luaL_argerror(L, 1, "function or level expected");
|
||||
luaL_argerror(L, 1, l_s("function or level expected"));
|
||||
if (!lua_getinfo(L, options, &ar))
|
||||
luaL_argerror(L, 2, "invalid option");
|
||||
luaL_argerror(L, 2, l_s("invalid option"));
|
||||
lua_newtable(L);
|
||||
for (; *options; options++) {
|
||||
switch (*options) {
|
||||
case 'S':
|
||||
settabss(L, "source", ar.source);
|
||||
case l_c('S'):
|
||||
settabss(L, l_s("source"), ar.source);
|
||||
if (ar.source)
|
||||
settabss(L, "short_src", ar.short_src);
|
||||
settabsi(L, "linedefined", ar.linedefined);
|
||||
settabss(L, "what", ar.what);
|
||||
settabss(L, l_s("short_src"), ar.short_src);
|
||||
settabsi(L, l_s("linedefined"), ar.linedefined);
|
||||
settabss(L, l_s("what"), ar.what);
|
||||
break;
|
||||
case 'l':
|
||||
settabsi(L, "currentline", ar.currentline);
|
||||
case l_c('l'):
|
||||
settabsi(L, l_s("currentline"), ar.currentline);
|
||||
break;
|
||||
case 'u':
|
||||
settabsi(L, "nups", ar.nups);
|
||||
case l_c('u'):
|
||||
settabsi(L, l_s("nups"), ar.nups);
|
||||
break;
|
||||
case 'n':
|
||||
settabss(L, "name", ar.name);
|
||||
settabss(L, "namewhat", ar.namewhat);
|
||||
case l_c('n'):
|
||||
settabss(L, l_s("name"), ar.name);
|
||||
settabss(L, l_s("namewhat"), ar.namewhat);
|
||||
break;
|
||||
case 'f':
|
||||
lua_pushstring(L, "func");
|
||||
case l_c('f'):
|
||||
lua_pushliteral(L, l_s("func"));
|
||||
lua_pushvalue(L, -3);
|
||||
lua_settable(L, -3);
|
||||
break;
|
||||
@@ -83,9 +84,9 @@ static int getinfo (lua_State *L) {
|
||||
|
||||
static int getlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
const char *name;
|
||||
const l_char *name;
|
||||
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, l_s("level out of range"));
|
||||
name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
|
||||
if (name) {
|
||||
lua_pushstring(L, name);
|
||||
@@ -102,79 +103,86 @@ static int getlocal (lua_State *L) {
|
||||
static int setlocal (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||
luaL_argerror(L, 1, "level out of range");
|
||||
luaL_checktype(L, 3, "any");
|
||||
luaL_argerror(L, 1, l_s("level out of range"));
|
||||
luaL_checkany(L, 3);
|
||||
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** because of these variables, this module is not reentrant, and should
|
||||
** not be used in multiple states
|
||||
*/
|
||||
|
||||
static int linehook = LUA_NOREF; /* Lua reference to line hook function */
|
||||
static int callhook = LUA_NOREF; /* Lua reference to call hook function */
|
||||
#define KEY_CALLHOOK l_s("luadblibCallhook")
|
||||
#define KEY_LINEHOOK l_s("luadblibLinehook")
|
||||
|
||||
|
||||
|
||||
static void linef (lua_State *L, lua_Debug *ar) {
|
||||
if (linehook != LUA_NOREF) {
|
||||
lua_getref(L, linehook);
|
||||
lua_pushnumber(L, ar->currentline);
|
||||
lua_call(L, 1, 0);
|
||||
static void hookf (lua_State *L, const l_char *key) {
|
||||
lua_getregistry(L);
|
||||
lua_pushstring(L, key);
|
||||
lua_gettable(L, -2);
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushvalue(L, -3); /* original argument (below table and function) */
|
||||
lua_rawcall(L, 1, 0);
|
||||
}
|
||||
else
|
||||
lua_pop(L, 1); /* pop result from gettable */
|
||||
lua_pop(L, 1); /* pop table */
|
||||
}
|
||||
|
||||
|
||||
static void callf (lua_State *L, lua_Debug *ar) {
|
||||
if (callhook != LUA_NOREF) {
|
||||
lua_getref(L, callhook);
|
||||
lua_pushstring(L, ar->event);
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
lua_pushstring(L, ar->event);
|
||||
hookf(L, KEY_CALLHOOK);
|
||||
}
|
||||
|
||||
|
||||
static void linef (lua_State *L, lua_Debug *ar) {
|
||||
lua_pushnumber(L, ar->currentline);
|
||||
hookf(L, KEY_LINEHOOK);
|
||||
}
|
||||
|
||||
|
||||
static void sethook (lua_State *L, const l_char *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, l_s("function expected"));
|
||||
lua_getregistry(L);
|
||||
lua_pushstring(L, key);
|
||||
lua_pushvalue(L, -1); /* dup key */
|
||||
lua_gettable(L, -3); /* get old value */
|
||||
lua_pushvalue(L, -2); /* key (again) */
|
||||
lua_pushvalue(L, 1);
|
||||
lua_settable(L, -5); /* set new value */
|
||||
}
|
||||
|
||||
|
||||
static int setcallhook (lua_State *L) {
|
||||
lua_unref(L, callhook);
|
||||
if (lua_isnull(L, 1)) {
|
||||
callhook = LUA_NOREF;
|
||||
lua_setcallhook(L, NULL);
|
||||
}
|
||||
else {
|
||||
callhook = lua_ref(L, 1);
|
||||
lua_setcallhook(L, callf);
|
||||
}
|
||||
return 0;
|
||||
sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int setlinehook (lua_State *L) {
|
||||
lua_unref(L, linehook);
|
||||
if (lua_isnull(L, 1)) {
|
||||
linehook = LUA_NOREF;
|
||||
lua_setlinehook(L, NULL);
|
||||
}
|
||||
else {
|
||||
linehook = lua_ref(L, 1);
|
||||
lua_setlinehook(L, linef);
|
||||
}
|
||||
return 0;
|
||||
sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg dblib[] = {
|
||||
{"getlocal", getlocal},
|
||||
{"getinfo", getinfo},
|
||||
{"setcallhook", setcallhook},
|
||||
{"setlinehook", setlinehook},
|
||||
{"setlocal", setlocal}
|
||||
static const luaL_reg dblib[] = {
|
||||
{l_s("getlocal"), getlocal},
|
||||
{l_s("getinfo"), getinfo},
|
||||
{l_s("setcallhook"), setcallhook},
|
||||
{l_s("setlinehook"), setlinehook},
|
||||
{l_s("setlocal"), setlocal}
|
||||
};
|
||||
|
||||
|
||||
void lua_dblibopen (lua_State *L) {
|
||||
LUALIB_API int lua_dblibopen (lua_State *L) {
|
||||
luaL_openl(L, dblib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
644
ldebug.c
644
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.86 2001/06/28 19:58:57 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -21,98 +22,88 @@
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "luadebug.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, StkId f, const char **name);
|
||||
|
||||
static const l_char *getfuncname (lua_State *L, CallInfo *ci,
|
||||
const l_char **name);
|
||||
|
||||
|
||||
static void setnormalized (TObject *d, const TObject *s) {
|
||||
switch (s->ttype) {
|
||||
case TAG_CMARK: {
|
||||
clvalue(d) = clvalue(s);
|
||||
ttype(d) = TAG_CCLOSURE;
|
||||
break;
|
||||
}
|
||||
case TAG_LMARK: {
|
||||
clvalue(d) = infovalue(s)->func;
|
||||
ttype(d) = TAG_LCLOSURE;
|
||||
break;
|
||||
}
|
||||
default: *d = *s;
|
||||
}
|
||||
|
||||
static int isLmark (CallInfo *ci) {
|
||||
lua_assert(ci == NULL || ttype(ci->base - 1) == LUA_TFUNCTION);
|
||||
return (ci && ci->prev && !ci_func(ci)->isC);
|
||||
}
|
||||
|
||||
|
||||
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->callhook;
|
||||
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook;
|
||||
lua_lock(L);
|
||||
oldhook = L->callhook;
|
||||
L->callhook = func;
|
||||
lua_unlock(L);
|
||||
return oldhook;
|
||||
}
|
||||
|
||||
|
||||
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook = L->linehook;
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||
lua_Hook oldhook;
|
||||
lua_lock(L);
|
||||
oldhook = L->linehook;
|
||||
L->linehook = func;
|
||||
lua_unlock(L);
|
||||
return oldhook;
|
||||
}
|
||||
|
||||
|
||||
static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
|
||||
int i;
|
||||
for (i = (top-1) - L->stack; i>=0; i--) {
|
||||
if (is_T_MARK(L->stack[i].ttype)) {
|
||||
if (level == 0)
|
||||
return L->stack+i;
|
||||
level--;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
static CallInfo *ci_stack (lua_State *L, StkId obj) {
|
||||
CallInfo *ci = L->ci;
|
||||
while (ci->base > obj) ci = ci->prev;
|
||||
return (ci != &L->basefunc) ? ci : NULL;
|
||||
}
|
||||
|
||||
|
||||
int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
StkId f = aux_stackedfunction(L, level, L->top);
|
||||
if (f == NULL) return 0; /* there is no such level */
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||
CallInfo *ci;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
ci = L->ci;
|
||||
while (level-- && ci != &L->basefunc) {
|
||||
lua_assert(ci->base > ci->prev->base);
|
||||
ci = ci->prev;
|
||||
}
|
||||
if (ci == &L->basefunc) status = 0; /* there is no such level */
|
||||
else {
|
||||
ar->_func = f;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int lua_nups (StkId f) {
|
||||
switch (ttype(f)) {
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK:
|
||||
return clvalue(f)->nupvalues;
|
||||
case TAG_LMARK:
|
||||
return infovalue(f)->func->nupvalues;
|
||||
default:
|
||||
return 0;
|
||||
ar->_ci = ci;
|
||||
status = 1;
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
||||
int refi;
|
||||
if (lineinfo == NULL) return -1; /* no line info */
|
||||
else if (pc == -1) return refline; /* function preamble */
|
||||
if (lineinfo == NULL || pc == -1)
|
||||
return -1; /* no line info or function is not active */
|
||||
refi = prefi ? *prefi : 0;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline += -lineinfo[refi++];
|
||||
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||
refline += -lineinfo[refi++];
|
||||
lua_assert(lineinfo[refi] >= 0);
|
||||
while (lineinfo[refi] > pc) {
|
||||
refline--;
|
||||
refi--;
|
||||
if (lineinfo[refi] < 0)
|
||||
refline -= -lineinfo[refi--];
|
||||
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||
refline -= -lineinfo[refi--];
|
||||
lua_assert(lineinfo[refi] >= 0);
|
||||
}
|
||||
for (;;) {
|
||||
int nextline = refline + 1;
|
||||
int nextref = refi + 1;
|
||||
if (lineinfo[nextref] < 0)
|
||||
nextline += -lineinfo[nextref++];
|
||||
LUA_ASSERT(lineinfo[nextref] >= 0, "invalid line info");
|
||||
nextline += -lineinfo[nextref++];
|
||||
lua_assert(lineinfo[nextref] >= 0);
|
||||
if (lineinfo[nextref] > pc)
|
||||
break;
|
||||
refline = nextline;
|
||||
@@ -123,338 +114,453 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
||||
}
|
||||
|
||||
|
||||
static int lua_currentpc (StkId f) {
|
||||
CallInfo *ci = infovalue(f);
|
||||
LUA_ASSERT(ttype(f) == TAG_LMARK, "function has no pc");
|
||||
return (*ci->pc - ci->func->f.l->code) - 1;
|
||||
static int currentpc (CallInfo *ci) {
|
||||
lua_assert(isLmark(ci));
|
||||
if (ci->pc)
|
||||
return (*ci->pc - ci_func(ci)->f.l->code) - 1;
|
||||
else
|
||||
return -1; /* function is not active */
|
||||
}
|
||||
|
||||
|
||||
static int lua_currentline (StkId f) {
|
||||
if (ttype(f) != TAG_LMARK)
|
||||
static int currentline (CallInfo *ci) {
|
||||
if (!isLmark(ci))
|
||||
return -1; /* only active lua functions have current-line information */
|
||||
else {
|
||||
CallInfo *ci = infovalue(f);
|
||||
int *lineinfo = ci->func->f.l->lineinfo;
|
||||
return luaG_getline(lineinfo, lua_currentpc(f), 1, NULL);
|
||||
int *lineinfo = ci_func(ci)->f.l->lineinfo;
|
||||
return luaG_getline(lineinfo, currentpc(ci), 1, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static Proto *getluaproto (StkId f) {
|
||||
return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL;
|
||||
static Proto *getluaproto (CallInfo *ci) {
|
||||
return (isLmark(ci) ? ci_func(ci)->f.l : NULL);
|
||||
}
|
||||
|
||||
|
||||
const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int localnum) {
|
||||
const char *name;
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
if (!fp) return NULL; /* `f' is not a Lua function? */
|
||||
name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
|
||||
if (!name) return NULL;
|
||||
luaA_pushobject(L, (f+1)+(localnum-1)); /* push value */
|
||||
LUA_API const l_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const l_char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = ar->_ci;
|
||||
fp = getluaproto(ci);
|
||||
if (fp) { /* is a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(ci));
|
||||
if (name)
|
||||
luaA_pushobject(L, ci->base+(n-1)); /* push value */
|
||||
}
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int localnum) {
|
||||
const char *name;
|
||||
StkId f = ar->_func;
|
||||
Proto *fp = getluaproto(f);
|
||||
LUA_API const l_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
const l_char *name;
|
||||
CallInfo *ci;
|
||||
Proto *fp;
|
||||
lua_lock(L);
|
||||
name = NULL;
|
||||
ci = ar->_ci;
|
||||
fp = getluaproto(ci);
|
||||
L->top--; /* pop new value */
|
||||
if (!fp) return NULL; /* `f' is not a Lua function? */
|
||||
name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
|
||||
if (!name || name[0] == '*') return NULL; /* `*' starts private locals */
|
||||
*((f+1)+(localnum-1)) = *L->top;
|
||||
if (fp) { /* is a Lua function? */
|
||||
name = luaF_getlocalname(fp, n, currentpc(ci));
|
||||
if (!name || name[0] == l_c('(')) /* `(' starts private locals */
|
||||
name = NULL;
|
||||
else
|
||||
setobj(ci->base+(n-1), L->top);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
static void infoLproto (lua_Debug *ar, Proto *f) {
|
||||
ar->source = f->source->str;
|
||||
ar->source = getstr(f->source);
|
||||
ar->linedefined = f->lineDefined;
|
||||
ar->what = "Lua";
|
||||
ar->what = l_s("Lua");
|
||||
}
|
||||
|
||||
|
||||
static void lua_funcinfo (lua_Debug *ar, StkId func) {
|
||||
switch (ttype(func)) {
|
||||
case TAG_LCLOSURE:
|
||||
infoLproto(ar, clvalue(func)->f.l);
|
||||
break;
|
||||
case TAG_LMARK:
|
||||
infoLproto(ar, infovalue(func)->func->f.l);
|
||||
break;
|
||||
case TAG_CCLOSURE: case TAG_CMARK:
|
||||
ar->source = "(C)";
|
||||
ar->linedefined = -1;
|
||||
ar->what = "C";
|
||||
break;
|
||||
default:
|
||||
LUA_INTERNALERROR("invalid `func' value");
|
||||
static void funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
|
||||
Closure *cl;
|
||||
if (ttype(func) == LUA_TFUNCTION)
|
||||
cl = clvalue(func);
|
||||
else {
|
||||
luaD_error(L, l_s("value for `lua_getinfo' is not a function"));
|
||||
cl = NULL; /* to avoid warnings */
|
||||
}
|
||||
luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
|
||||
if (cl->isC) {
|
||||
ar->source = l_s("=C");
|
||||
ar->linedefined = -1;
|
||||
ar->what = l_s("C");
|
||||
}
|
||||
else
|
||||
infoLproto(ar, cl->f.l);
|
||||
luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
|
||||
if (ar->linedefined == 0)
|
||||
ar->what = "main";
|
||||
ar->what = l_s("main");
|
||||
}
|
||||
|
||||
|
||||
static const char *travtagmethods (lua_State *L, const TObject *o) {
|
||||
int e;
|
||||
for (e=0; e<IM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
if (luaO_equalObj(o, luaT_getim(L, t,e)))
|
||||
return luaT_eventname[e];
|
||||
static const l_char *travtagmethods (global_State *G, const TObject *o) {
|
||||
if (ttype(o) == LUA_TFUNCTION) {
|
||||
int e;
|
||||
for (e=0; e<TM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<G->ntag; t++)
|
||||
if (clvalue(o) == luaT_gettm(G, t, e))
|
||||
return luaT_eventname[e];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *travglobals (lua_State *L, const TObject *o) {
|
||||
static const l_char *travglobals (lua_State *L, const TObject *o) {
|
||||
Hash *g = L->gt;
|
||||
int i;
|
||||
for (i=0; i<g->size; i++) {
|
||||
if (luaO_equalObj(o, val(node(g, i))) &&
|
||||
ttype(key(node(g, i))) == TAG_STRING)
|
||||
return tsvalue(key(node(g, i)))->str;
|
||||
ttype(key(node(g, i))) == LUA_TSTRING)
|
||||
return getstr(tsvalue(key(node(g, i))));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void lua_getname (lua_State *L, StkId f, lua_Debug *ar) {
|
||||
TObject o;
|
||||
setnormalized(&o, f);
|
||||
static void getname (lua_State *L, const TObject *f, lua_Debug *ar) {
|
||||
/* try to find a name for given function */
|
||||
if ((ar->name = travglobals(L, &o)) != NULL)
|
||||
ar->namewhat = "global";
|
||||
if ((ar->name = travglobals(L, f)) != NULL)
|
||||
ar->namewhat = l_s("global");
|
||||
/* not found: try tag methods */
|
||||
else if ((ar->name = travtagmethods(L, &o)) != NULL)
|
||||
ar->namewhat = "tag-method";
|
||||
else ar->namewhat = ""; /* not found at all */
|
||||
else if ((ar->name = travtagmethods(G(L), f)) != NULL)
|
||||
ar->namewhat = l_s("tag-method");
|
||||
else ar->namewhat = l_s(""); /* not found at all */
|
||||
}
|
||||
|
||||
|
||||
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
StkId func;
|
||||
int isactive = (*what != '>');
|
||||
if (isactive)
|
||||
func = ar->_func;
|
||||
LUA_API int lua_getinfo (lua_State *L, const l_char *what, lua_Debug *ar) {
|
||||
StkId f;
|
||||
CallInfo *ci;
|
||||
int status = 1;
|
||||
lua_lock(L);
|
||||
if (*what != l_c('>')) { /* function is active? */
|
||||
ci = ar->_ci;
|
||||
f = ci->base - 1;
|
||||
}
|
||||
else {
|
||||
what++; /* skip the '>' */
|
||||
func = L->top - 1;
|
||||
what++; /* skip the `>' */
|
||||
ci = NULL;
|
||||
f = L->top - 1;
|
||||
}
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
case 'S': {
|
||||
lua_funcinfo(ar, func);
|
||||
case l_c('S'): {
|
||||
funcinfo(L, ar, f);
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
ar->currentline = lua_currentline(func);
|
||||
case l_c('l'): {
|
||||
ar->currentline = currentline(ci);
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = lua_nups(func);
|
||||
case l_c('u'): {
|
||||
ar->nups = (ttype(f) == LUA_TFUNCTION) ? clvalue(f)->nupvalues : 0;
|
||||
break;
|
||||
}
|
||||
case 'n': {
|
||||
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
|
||||
case l_c('n'): {
|
||||
ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
|
||||
if (ar->namewhat == NULL)
|
||||
lua_getname(L, func, ar);
|
||||
getname(L, f, ar);
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
setnormalized(L->top, func);
|
||||
case l_c('f'): {
|
||||
setobj(L->top, f);
|
||||
incr_top; /* push function */
|
||||
break;
|
||||
}
|
||||
default: return 0; /* invalid option */
|
||||
default: status = 0; /* invalid option */
|
||||
}
|
||||
}
|
||||
if (!isactive) L->top--; /* pop function */
|
||||
return 1;
|
||||
if (!ci) L->top--; /* pop function */
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Symbolic Execution
|
||||
** Symbolic Execution and code checker
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define check(x) if (!(x)) return 0;
|
||||
|
||||
static int pushpc (int *stack, int pc, int top, int n) {
|
||||
while (n--)
|
||||
stack[top++] = pc-1;
|
||||
return top;
|
||||
#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
|
||||
|
||||
#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
|
||||
|
||||
|
||||
static int checklineinfo (const Proto *pt) {
|
||||
int *lineinfo = pt->lineinfo;
|
||||
if (lineinfo == NULL) return 1;
|
||||
check(pt->sizelineinfo >= 2 && lineinfo[pt->sizelineinfo-1] == MAX_INT);
|
||||
if (*lineinfo < 0) lineinfo++;
|
||||
check(*lineinfo == 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
|
||||
int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
|
||||
const Instruction *code = pt->code;
|
||||
int top = pt->numparams;
|
||||
int pc = 0;
|
||||
if (pt->is_vararg) /* varargs? */
|
||||
top++; /* `arg' */
|
||||
while (pc < lastpc) {
|
||||
const Instruction i = code[pc++];
|
||||
LUA_ASSERT(0 <= top && top <= pt->maxstacksize, "wrong stack");
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_RETURN: {
|
||||
LUA_ASSERT(top >= GETARG_U(i), "wrong stack");
|
||||
top = GETARG_U(i);
|
||||
static int precheck (const Proto *pt) {
|
||||
check(checklineinfo(pt));
|
||||
check(pt->maxstacksize <= MAXSTACK);
|
||||
check(pt->numparams+pt->is_vararg <= pt->maxstacksize);
|
||||
check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int checkopenop (Instruction i) {
|
||||
OpCode op = GET_OPCODE(i);
|
||||
switch (op) {
|
||||
case OP_CALL:
|
||||
case OP_RETURN: {
|
||||
check(GETARG_B(i) == NO_REG);
|
||||
return 1;
|
||||
}
|
||||
case OP_SETLISTO: return 1;
|
||||
default: return 0; /* invalid instruction after an open call */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
|
||||
int pc;
|
||||
int last; /* stores position of last instruction that changed `reg' */
|
||||
last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
|
||||
if (reg == NO_REG) /* full check? */
|
||||
check(precheck(pt));
|
||||
for (pc = 0; pc < lastpc; pc++) {
|
||||
const Instruction i = pt->code[pc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
int a = GETARG_A(i);
|
||||
int b = 0;
|
||||
int c = 0;
|
||||
checkreg(pt, a);
|
||||
switch (getOpMode(op)) {
|
||||
case iABC: {
|
||||
b = GETARG_B(i);
|
||||
c = GETARG_C(i);
|
||||
if (testOpMode(op, OpModeBreg))
|
||||
checkreg(pt, b);
|
||||
if (testOpMode(op, OpModeCreg))
|
||||
check(c < pt->maxstacksize ||
|
||||
(c >= MAXSTACK && c-MAXSTACK < pt->sizek));
|
||||
break;
|
||||
}
|
||||
case OP_TAILCALL: {
|
||||
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||
top = GETARG_B(i);
|
||||
case iABc: {
|
||||
b = GETARG_Bc(i);
|
||||
if (testOpMode(op, OpModeK)) check(b < pt->sizek);
|
||||
break;
|
||||
}
|
||||
case OP_CALL: {
|
||||
int nresults = GETARG_B(i);
|
||||
if (nresults == MULT_RET) nresults = 1;
|
||||
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||
top = pushpc(stack, pc, GETARG_A(i), nresults);
|
||||
case iAsBc: {
|
||||
b = GETARG_sBc(i);
|
||||
break;
|
||||
}
|
||||
case OP_PUSHNIL: {
|
||||
top = pushpc(stack, pc, top, GETARG_U(i));
|
||||
}
|
||||
if (testOpMode(op, OpModesetA)) {
|
||||
if (a == reg) last = pc; /* change register `a' */
|
||||
}
|
||||
if (testOpMode(op, OpModeT))
|
||||
check(GET_OPCODE(pt->code[pc+1]) == OP_CJMP);
|
||||
switch (op) {
|
||||
case OP_LOADNIL: {
|
||||
if (a <= reg && reg <= b)
|
||||
last = pc; /* set registers from `a' to `b' */
|
||||
break;
|
||||
}
|
||||
case OP_POP: {
|
||||
top -= GETARG_U(i);
|
||||
case OP_LOADUPVAL: {
|
||||
check(b < pt->nupvalues);
|
||||
break;
|
||||
}
|
||||
case OP_SETTABLE:
|
||||
case OP_SETLIST: {
|
||||
top -= GETARG_B(i);
|
||||
case OP_GETGLOBAL:
|
||||
case OP_SETGLOBAL: {
|
||||
check(ttype(&pt->k[b]) == LUA_TSTRING);
|
||||
break;
|
||||
}
|
||||
case OP_SETMAP: {
|
||||
top -= 2*GETARG_U(i);
|
||||
case OP_SELF: {
|
||||
checkreg(pt, a+1);
|
||||
if (reg == a+1) last = pc;
|
||||
break;
|
||||
}
|
||||
case OP_CONCAT: {
|
||||
top -= GETARG_U(i);
|
||||
stack[top++] = pc-1;
|
||||
/* `c' is a register, and at least two operands */
|
||||
check(c < MAXSTACK && b < c);
|
||||
break;
|
||||
}
|
||||
case OP_JMP:
|
||||
case OP_CJMP: {
|
||||
int dest = pc+1+b;
|
||||
check(0 <= dest && dest < pt->sizecode);
|
||||
/* not full check and jump is forward and do not skip `lastpc'? */
|
||||
if (reg != NO_REG && pc < dest && dest <= lastpc)
|
||||
pc += b; /* do the jump */
|
||||
break;
|
||||
}
|
||||
case OP_NILJMP: {
|
||||
check(pc+2 < pt->sizecode); /* check its jump */
|
||||
break;
|
||||
}
|
||||
case OP_CALL: {
|
||||
if (b != NO_REG) {
|
||||
checkreg(pt, a+b);
|
||||
}
|
||||
if (c == NO_REG) {
|
||||
check(checkopenop(pt->code[pc+1]));
|
||||
}
|
||||
else if (c != 0)
|
||||
checkreg(pt, a+c-1);
|
||||
if (reg >= a) last = pc; /* affect all registers above base */
|
||||
break;
|
||||
}
|
||||
case OP_RETURN: {
|
||||
if (b != NO_REG && b != 0)
|
||||
checkreg(pt, a+b-1);
|
||||
break;
|
||||
}
|
||||
case OP_FORPREP:
|
||||
case OP_TFORPREP: {
|
||||
int dest = pc-b; /* jump is negated here */
|
||||
check(0 <= dest && dest < pt->sizecode &&
|
||||
GET_OPCODE(pt->code[dest]) == op+1);
|
||||
break;
|
||||
}
|
||||
case OP_FORLOOP:
|
||||
case OP_TFORLOOP: {
|
||||
int dest = pc+b;
|
||||
check(0 <= dest && dest < pt->sizecode &&
|
||||
pt->code[dest] == SET_OPCODE(i, op-1));
|
||||
checkreg(pt, a + ((op == OP_FORLOOP) ? 2 : 3));
|
||||
break;
|
||||
}
|
||||
case OP_SETLIST: {
|
||||
checkreg(pt, a + (b&(LFIELDS_PER_FLUSH-1)) + 1);
|
||||
break;
|
||||
}
|
||||
case OP_CLOSURE: {
|
||||
top -= GETARG_B(i);
|
||||
stack[top++] = pc-1;
|
||||
check(b < pt->sizep);
|
||||
checkreg(pt, a + pt->p[b]->nupvalues - 1);
|
||||
break;
|
||||
}
|
||||
case OP_JMPONT:
|
||||
case OP_JMPONF: {
|
||||
int newpc = pc + GETARG_S(i);
|
||||
/* jump is forward and do not skip `lastpc'? */
|
||||
if (pc < newpc && newpc <= lastpc) {
|
||||
stack[top-1] = pc-1; /* value comes from `and'/`or' */
|
||||
pc = newpc; /* do the jump */
|
||||
}
|
||||
else
|
||||
top--; /* do not jump; pop value */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
OpCode op = GET_OPCODE(i);
|
||||
LUA_ASSERT(luaK_opproperties[op].push != VD,
|
||||
"invalid opcode for default");
|
||||
top -= luaK_opproperties[op].pop;
|
||||
LUA_ASSERT(top >= 0, "wrong stack");
|
||||
top = pushpc(stack, pc, top, luaK_opproperties[op].push);
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return code[stack[stackpos]];
|
||||
return pt->code[last];
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (lua_State *L, StkId obj, const char **name) {
|
||||
StkId func = aux_stackedfunction(L, 0, obj);
|
||||
if (func == NULL || ttype(func) != TAG_LMARK)
|
||||
return NULL; /* not a Lua function */
|
||||
else {
|
||||
Proto *p = infovalue(func)->func->f.l;
|
||||
int pc = lua_currentpc(func);
|
||||
int stackpos = obj - (func+1); /* func+1 == function base */
|
||||
Instruction i = luaG_symbexec(p, pc, stackpos);
|
||||
LUA_ASSERT(pc != -1, "function must be active");
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_GETGLOBAL: {
|
||||
*name = p->kstr[GETARG_U(i)]->str;
|
||||
return "global";
|
||||
}
|
||||
case OP_GETLOCAL: {
|
||||
*name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
|
||||
LUA_ASSERT(*name, "local must exist");
|
||||
return "local";
|
||||
}
|
||||
case OP_PUSHSELF:
|
||||
case OP_GETDOTTED: {
|
||||
*name = p->kstr[GETARG_U(i)]->str;
|
||||
return "field";
|
||||
}
|
||||
default:
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *getfuncname (lua_State *L, StkId f, const char **name) {
|
||||
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
|
||||
if (func == NULL || ttype(func) != TAG_LMARK)
|
||||
return NULL; /* not a Lua function */
|
||||
else {
|
||||
Proto *p = infovalue(func)->func->f.l;
|
||||
int pc = lua_currentpc(func);
|
||||
Instruction i;
|
||||
if (pc == -1) return NULL; /* function is not activated */
|
||||
i = p->code[pc];
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_CALL: case OP_TAILCALL:
|
||||
return getobjname(L, (func+1)+GETARG_A(i), name);
|
||||
default:
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, StkId o, const char *op) {
|
||||
const char *name;
|
||||
const char *kind = getobjname(L, o, &name);
|
||||
const char *t = luaO_typename(o);
|
||||
int luaG_checkcode (const Proto *pt) {
|
||||
return luaG_symbexec(pt, pt->sizecode, NO_REG);
|
||||
}
|
||||
|
||||
|
||||
static const l_char *getobjname (lua_State *L, StkId obj, const l_char **name) {
|
||||
CallInfo *ci = ci_stack(L, obj);
|
||||
if (isLmark(ci)) { /* an active Lua function? */
|
||||
Proto *p = ci_func(ci)->f.l;
|
||||
int pc = currentpc(ci);
|
||||
int stackpos = obj - ci->base;
|
||||
Instruction i;
|
||||
*name = luaF_getlocalname(p, stackpos+1, pc);
|
||||
if (*name) /* is a local? */
|
||||
return l_s("local");
|
||||
i = luaG_symbexec(p, pc, stackpos); /* try symbolic execution */
|
||||
lua_assert(pc != -1);
|
||||
switch (GET_OPCODE(i)) {
|
||||
case OP_GETGLOBAL: {
|
||||
lua_assert(ttype(&p->k[GETARG_Bc(i)]) == LUA_TSTRING);
|
||||
*name = svalue(&p->k[GETARG_Bc(i)]);
|
||||
return l_s("global");
|
||||
}
|
||||
case OP_MOVE: {
|
||||
int a = GETARG_A(i);
|
||||
int b = GETARG_B(i); /* move from `b' to `a' */
|
||||
if (b < a)
|
||||
return getobjname(L, ci->base+b, name); /* get name for `b' */
|
||||
break;
|
||||
}
|
||||
case OP_GETTABLE:
|
||||
case OP_SELF: {
|
||||
int c = GETARG_C(i) - MAXSTACK;
|
||||
if (c >= 0 && ttype(&p->k[c]) == LUA_TSTRING) {
|
||||
*name = svalue(&p->k[c]);
|
||||
return l_s("field");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return NULL; /* no useful name found */
|
||||
}
|
||||
|
||||
|
||||
static const l_char *getfuncname (lua_State *L, CallInfo *ci,
|
||||
const l_char **name) {
|
||||
ci = ci->prev; /* calling function */
|
||||
if (ci == &L->basefunc || !isLmark(ci))
|
||||
return NULL; /* not an active Lua function */
|
||||
else {
|
||||
Proto *p = ci_func(ci)->f.l;
|
||||
int pc = currentpc(ci);
|
||||
Instruction i;
|
||||
if (pc == -1) return NULL; /* function is not activated */
|
||||
i = p->code[pc];
|
||||
return (GET_OPCODE(i) == OP_CALL
|
||||
? getobjname(L, ci->base+GETARG_A(i), name)
|
||||
: NULL); /* no useful name found */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaG_typeerror (lua_State *L, StkId o, const l_char *op) {
|
||||
const l_char *name;
|
||||
const l_char *kind = getobjname(L, o, &name);
|
||||
const l_char *t = luaT_typename(G(L), o);
|
||||
if (kind)
|
||||
luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
|
||||
luaO_verror(L, l_s("attempt to %.30s %.20s `%.40s' (a %.10s value)"),
|
||||
op, kind, name, t);
|
||||
else
|
||||
luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
|
||||
luaO_verror(L, l_s("attempt to %.30s a %.10s value"), op, t);
|
||||
}
|
||||
|
||||
|
||||
void luaG_binerror (lua_State *L, StkId p1, lua_Type t, const char *op) {
|
||||
if (ttype(p1) == t) p1++;
|
||||
LUA_ASSERT(ttype(p1) != t, "must be an error");
|
||||
luaG_typeerror(L, p1, op);
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
|
||||
if (ttype(p1) == LUA_TSTRING) p1 = p2;
|
||||
lua_assert(ttype(p1) != LUA_TSTRING);
|
||||
luaG_typeerror(L, p1, l_s("concat"));
|
||||
}
|
||||
|
||||
|
||||
void luaG_ordererror (lua_State *L, StkId top) {
|
||||
const char *t1 = luaO_typename(top-2);
|
||||
const char *t2 = luaO_typename(top-1);
|
||||
void luaG_aritherror (lua_State *L, StkId p1, TObject *p2) {
|
||||
TObject temp;
|
||||
if (luaV_tonumber(p1, &temp) != NULL)
|
||||
p1 = p2; /* first operand is OK; error is in the second */
|
||||
luaG_typeerror(L, p1, l_s("perform arithmetic on"));
|
||||
}
|
||||
|
||||
|
||||
void luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2) {
|
||||
const l_char *t1 = luaT_typename(G(L), p1);
|
||||
const l_char *t2 = luaT_typename(G(L), p2);
|
||||
if (t1[2] == t2[2])
|
||||
luaO_verror(L, "attempt to compare two %.10s values", t1);
|
||||
luaO_verror(L, l_s("attempt to compare two %.10s values"), t1);
|
||||
else
|
||||
luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
|
||||
luaO_verror(L, l_s("attempt to compare %.10s with %.10s"), t1, t2);
|
||||
}
|
||||
|
||||
|
||||
10
ldebug.h
10
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.14 2001/06/11 14:56:42 roberto Exp roberto $
|
||||
** Auxiliary functions from Debug Interface module
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,10 +12,12 @@
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
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_typeerror (lua_State *L, StkId o, const l_char *op);
|
||||
void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
|
||||
void luaG_aritherror (lua_State *L, StkId p1, TObject *p2);
|
||||
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, const TObject *p1, const TObject *p2);
|
||||
int luaG_checkcode (const Proto *pt);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
457
ldo.c
457
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.137 2001/07/12 19:34:03 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
@@ -28,52 +29,45 @@
|
||||
|
||||
|
||||
/* space to handle stack overflow errors */
|
||||
#define EXTRA_STACK (2*LUA_MINSTACK)
|
||||
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize) {
|
||||
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
|
||||
L->stack_last = L->stack+(stacksize-1);
|
||||
L->stacksize = stacksize;
|
||||
L->Cbase = L->top = L->stack;
|
||||
}
|
||||
|
||||
|
||||
void luaD_checkstack (lua_State *L, int n) {
|
||||
if (L->stack_last-L->top <= n) { /* stack overflow? */
|
||||
if (L->stack_last-L->stack > (L->stacksize-1)) {
|
||||
/* overflow while handling overflow: do what?? */
|
||||
L->top -= EXTRA_STACK;
|
||||
lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
|
||||
}
|
||||
else {
|
||||
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
||||
lua_error(L, "stack overflow");
|
||||
}
|
||||
}
|
||||
}
|
||||
#define EXTRA_STACK (2*LUA_MINSTACK)
|
||||
|
||||
|
||||
static void restore_stack_limit (lua_State *L) {
|
||||
if (L->top - L->stack < L->stacksize - 1)
|
||||
L->stack_last = L->stack + (L->stacksize-1);
|
||||
StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1;
|
||||
if (L->top < limit)
|
||||
L->stack_last = limit;
|
||||
}
|
||||
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize) {
|
||||
stacksize += EXTRA_STACK;
|
||||
L->stack = luaM_newvector(L, stacksize, TObject);
|
||||
L->stacksize = stacksize;
|
||||
L->basefunc.base = L->top = L->stack;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
|
||||
|
||||
void luaD_stackerror (lua_State *L) {
|
||||
if (L->stack_last == L->stack+L->stacksize-1) {
|
||||
/* overflow while handling overflow */
|
||||
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
|
||||
}
|
||||
else {
|
||||
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
||||
lua_assert(L->stack_last == L->stack+L->stacksize-1);
|
||||
luaD_error(L, l_s("stack overflow"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Adjust stack. Set top to base+extra, pushing NILs if needed.
|
||||
** (we cannot add base+extra unless we are sure it fits in the stack;
|
||||
** otherwise the result of such operation on pointers is undefined)
|
||||
** adjust top to new value; assume that new top is valid
|
||||
*/
|
||||
void luaD_adjusttop (lua_State *L, StkId base, int extra) {
|
||||
int diff = extra-(L->top-base);
|
||||
if (diff <= 0)
|
||||
L->top = base+extra;
|
||||
else {
|
||||
luaD_checkstack(L, diff);
|
||||
while (diff--)
|
||||
ttype(L->top++) = TAG_NIL;
|
||||
}
|
||||
void luaD_adjusttop (lua_State *L, StkId newtop) {
|
||||
while (L->top < newtop)
|
||||
setnilvalue(L->top++);
|
||||
L->top = newtop; /* `newtop' could be lower than `top' */
|
||||
}
|
||||
|
||||
|
||||
@@ -82,131 +76,238 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) {
|
||||
*/
|
||||
static void luaD_openstack (lua_State *L, StkId pos) {
|
||||
int i = L->top-pos;
|
||||
while (i--) pos[i+1] = pos[i];
|
||||
while (i--) setobj(pos+i+1, pos+i);
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
|
||||
StkId old_Cbase = L->Cbase;
|
||||
StkId old_top = L->Cbase = L->top;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* assures minimum stack size */
|
||||
StkId old_top = L->top;
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||
lua_unlock(L);
|
||||
(*hook)(L, ar);
|
||||
LUA_ASSERT(L->allowhooks == 0, "invalid allow");
|
||||
lua_lock(L);
|
||||
lua_assert(L->allowhooks == 0);
|
||||
L->allowhooks = 1;
|
||||
L->top = old_top;
|
||||
L->Cbase = old_Cbase;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
|
||||
void luaD_lineHook (lua_State *L, int line, lua_Hook linehook) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
ar._func = func;
|
||||
ar.event = "line";
|
||||
ar.event = l_s("line");
|
||||
ar._ci = L->ci;
|
||||
ar.currentline = line;
|
||||
dohook(L, &ar, linehook);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
|
||||
const char *event) {
|
||||
static void luaD_callHook (lua_State *L, lua_Hook callhook,
|
||||
const l_char *event) {
|
||||
if (L->allowhooks) {
|
||||
lua_Debug ar;
|
||||
ar._func = func;
|
||||
ar.event = event;
|
||||
ar._ci = L->ci;
|
||||
L->ci->pc = NULL; /* function is not active */
|
||||
dohook(L, &ar, callhook);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
|
||||
static StkId callCclosure (lua_State *L, const struct Closure *cl) {
|
||||
int nup = cl->nupvalues; /* number of upvalues */
|
||||
StkId old_Cbase = L->Cbase;
|
||||
int n;
|
||||
L->Cbase = base; /* new base for C function */
|
||||
luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */
|
||||
luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
|
||||
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
|
||||
*(L->top++) = cl->upvalue[n];
|
||||
setobj(L->top++, &cl->upvalue[n]);
|
||||
lua_unlock(L);
|
||||
n = (*cl->f.c)(L); /* do the actual call */
|
||||
L->Cbase = old_Cbase; /* restore old C base */
|
||||
lua_lock(L);
|
||||
return L->top - n; /* return index of first result */
|
||||
}
|
||||
|
||||
|
||||
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
|
||||
StkId base = L->top - nParams;
|
||||
luaD_openstack(L, base);
|
||||
*base = *f;
|
||||
luaD_call(L, base, nResults);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a function (C or Lua). The function to be called is at *func.
|
||||
** The arguments are on the stack, right after the function.
|
||||
** When returns, the results are on the stack, starting at the original
|
||||
** When returns, all the results are on the stack, starting at the original
|
||||
** function position.
|
||||
** 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) {
|
||||
lua_Hook callhook;
|
||||
StkId firstResult;
|
||||
lua_Hook callhook = L->callhook;
|
||||
retry: /* for `function' tag method */
|
||||
switch (ttype(func)) {
|
||||
case TAG_LCLOSURE: {
|
||||
CallInfo ci;
|
||||
ci.func = clvalue(func);
|
||||
ci.line = 0;
|
||||
ttype(func) = TAG_LMARK;
|
||||
infovalue(func) = &ci;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = luaV_execute(L, ci.func, func+1);
|
||||
break;
|
||||
}
|
||||
case TAG_CCLOSURE: {
|
||||
ttype(func) = TAG_CMARK;
|
||||
if (callhook)
|
||||
luaD_callHook(L, func, callhook, "call");
|
||||
firstResult = callCclosure(L, clvalue(func), func+1);
|
||||
break;
|
||||
}
|
||||
default: { /* `func' is not a function; check the `function' tag method */
|
||||
const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
|
||||
if (ttype(im) == TAG_NIL)
|
||||
luaG_typeerror(L, func, "call");
|
||||
luaD_openstack(L, func);
|
||||
*func = *im; /* tag method is the new function to be called */
|
||||
goto retry; /* retry the call */
|
||||
}
|
||||
CallInfo ci;
|
||||
if (ttype(func) != LUA_TFUNCTION) {
|
||||
/* `func' is not a function; check the `function' tag method */
|
||||
Closure *tm = luaT_gettmbyObj(G(L), func, TM_FUNCTION);
|
||||
if (tm == NULL)
|
||||
luaG_typeerror(L, func, l_s("call"));
|
||||
luaD_openstack(L, func);
|
||||
setclvalue(func, tm); /* tag method is the new function to be called */
|
||||
}
|
||||
ci.prev = L->ci; /* chain new callinfo */
|
||||
L->ci = &ci;
|
||||
ci.base = func+1;
|
||||
callhook = L->callhook;
|
||||
if (callhook)
|
||||
luaD_callHook(L, callhook, l_s("call"));
|
||||
firstResult = (clvalue(func)->isC ? callCclosure(L, clvalue(func)) :
|
||||
luaV_execute(L, clvalue(func), func+1));
|
||||
if (callhook) /* same hook that was active at entry */
|
||||
luaD_callHook(L, func, callhook, "return");
|
||||
/* adjust the number of results */
|
||||
if (nResults == LUA_MULTRET)
|
||||
nResults = L->top - firstResult;
|
||||
else
|
||||
luaD_adjusttop(L, firstResult, nResults);
|
||||
luaD_callHook(L, callhook, l_s("return"));
|
||||
L->ci = ci.prev; /* unchain callinfo */
|
||||
/* move results to `func' (to erase parameters and function) */
|
||||
while (nResults) {
|
||||
*func++ = *(L->top - nResults);
|
||||
nResults--;
|
||||
}
|
||||
while (firstResult < L->top)
|
||||
setobj(func++, firstResult++);
|
||||
L->top = func;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
static void message (lua_State *L, const char *s) {
|
||||
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
|
||||
if (*luaO_typename(em) == 'f') {
|
||||
*L->top = *em;
|
||||
incr_top;
|
||||
lua_pushstring(L, s);
|
||||
luaD_call(L, L->top-2, 0);
|
||||
/*
|
||||
** Execute a protected call.
|
||||
*/
|
||||
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);
|
||||
if (c->nresults != LUA_MULTRET)
|
||||
luaD_adjusttop(L, c->func + c->nresults);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
|
||||
StkId func;
|
||||
struct CallS c;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
func = L->top - (nargs+1); /* function to be called */
|
||||
c.func = func; c.nresults = nresults;
|
||||
status = luaD_runprotected(L, f_call, &c);
|
||||
if (status != 0) /* an error occurred? */
|
||||
L->top = func; /* remove parameters from the stack */
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
struct SParser { /* data to `f_parser' */
|
||||
ZIO *z;
|
||||
int bin;
|
||||
};
|
||||
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
struct SParser *p = (struct SParser *)ud;
|
||||
Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
|
||||
luaV_Lclosure(L, tf, 0);
|
||||
}
|
||||
|
||||
|
||||
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct SParser p;
|
||||
lu_mem old_blocks;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
p.z = z; p.bin = bin;
|
||||
/* before parsing, give a (good) chance to GC */
|
||||
if (G(L)->nblocks/8 >= G(L)->GCthreshold/10)
|
||||
luaC_collectgarbage(L);
|
||||
old_blocks = G(L)->nblocks;
|
||||
status = luaD_runprotected(L, f_parser, &p);
|
||||
if (status == 0) {
|
||||
/* add new memory to threshold (as it probably will stay) */
|
||||
lua_assert(G(L)->nblocks >= old_blocks);
|
||||
G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
|
||||
}
|
||||
else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
|
||||
status = LUA_ERRSYNTAX;
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_loadfile (lua_State *L, const l_char *filename) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int bin; /* flag for file mode */
|
||||
int nlevel; /* level on the stack of filename */
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, l_s("r"));
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
|
||||
bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
|
||||
if (bin && f != stdin) {
|
||||
fclose(f);
|
||||
f = fopen(filename, l_s("rb")); /* reopen in binary mode */
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
|
||||
}
|
||||
lua_pushliteral(L, l_s("@"));
|
||||
lua_pushstring(L, (filename == NULL) ? l_s("(stdin)") : filename);
|
||||
lua_concat(L, 2);
|
||||
nlevel = lua_gettop(L);
|
||||
filename = lua_tostring(L, -1); /* filename = `@'..filename */
|
||||
luaZ_Fopen(&z, f, filename);
|
||||
status = protectedparser(L, &z, bin);
|
||||
lua_remove(L, nlevel); /* remove filename */
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_loadbuffer (lua_State *L, const l_char *buff, size_t size,
|
||||
const l_char *name) {
|
||||
ZIO z;
|
||||
int status;
|
||||
if (!name) name = l_s("?");
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
status = protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** 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 l_char *s) {
|
||||
StkId top = L->top;
|
||||
luaV_getglobal(L, luaS_newliteral(L, l_s(LUA_ERRORMESSAGE)), top);
|
||||
if (ttype(top) == LUA_TFUNCTION) {
|
||||
incr_top;
|
||||
setsvalue(top+1, luaS_new(L, s));
|
||||
incr_top;
|
||||
luaD_call(L, top);
|
||||
L->top = top;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Reports an error, and jumps up to the available recovery label
|
||||
*/
|
||||
void luaD_error (lua_State *L, const l_char *s) {
|
||||
if (s) message(L, s);
|
||||
luaD_breakrun(L, LUA_ERRRUN);
|
||||
}
|
||||
|
||||
|
||||
@@ -217,125 +318,31 @@ void luaD_breakrun (lua_State *L, int errcode) {
|
||||
}
|
||||
else {
|
||||
if (errcode != LUA_ERRMEM)
|
||||
message(L, "unable to recover; exiting\n");
|
||||
message(L, l_s("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.
|
||||
*/
|
||||
int lua_call (lua_State *L, int nargs, int nresults) {
|
||||
StkId func = L->top - (nargs+1); /* function to be called */
|
||||
struct lua_longjmp myErrorJmp;
|
||||
chain_longjmp(L, &myErrorJmp);
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
luaD_call(L, func, nresults);
|
||||
}
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
|
||||
CallInfo *oldci = L->ci;
|
||||
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->top = func; /* remove garbage from the stack */
|
||||
L->allowhooks = allowhooks;
|
||||
L->ci = oldci;
|
||||
L->top = oldtop;
|
||||
restore_stack_limit(L);
|
||||
}
|
||||
return restore_longjmp(L, &myErrorJmp);
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
|
||||
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||
struct lua_longjmp myErrorJmp;
|
||||
unsigned long old_blocks;
|
||||
luaC_checkGC(L);
|
||||
old_blocks = L->nblocks;
|
||||
chain_longjmp(L, &myErrorJmp);
|
||||
if (setjmp(myErrorJmp.b) == 0) {
|
||||
Proto *tf = bin ? luaU_undump(L, z) : luaY_parser(L, z);
|
||||
luaV_Lclosure(L, tf, 0);
|
||||
}
|
||||
else { /* an error occurred: correct error code */
|
||||
if (myErrorJmp.status == LUA_ERRRUN)
|
||||
myErrorJmp.status = LUA_ERRSYNTAX;
|
||||
}
|
||||
/* add new memory to threshould (as it probably will stay) */
|
||||
L->GCthreshold += (L->nblocks - old_blocks);
|
||||
return restore_longjmp(L, &myErrorJmp); /* error code */
|
||||
}
|
||||
|
||||
|
||||
static int parse_file (lua_State *L, const char *filename) {
|
||||
ZIO z;
|
||||
char source[MAXFILENAME];
|
||||
int status;
|
||||
int bin; /* flag for file mode */
|
||||
int c; /* look ahead char */
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
|
||||
if (filename == NULL) filename = "(stdin)";
|
||||
sprintf(source, "@%.*s", (int)sizeof(source)-2, filename);
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin && f != stdin) {
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
|
||||
}
|
||||
luaZ_Fopen(&z, f, source);
|
||||
status = protectedparser(L, &z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int lua_dofile (lua_State *L, const char *filename) {
|
||||
int status = parse_file(L, filename);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int parse_buffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name) {
|
||||
ZIO z;
|
||||
if (!name) name = "?";
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
return protectedparser(L, &z, buff[0]==ID_CHUNK);
|
||||
}
|
||||
|
||||
|
||||
int lua_dobuffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name) {
|
||||
int status = parse_buffer(L, buff, size, name);
|
||||
if (status == 0) /* parse OK? */
|
||||
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int lua_dostring (lua_State *L, const char *str) {
|
||||
return lua_dobuffer(L, str, strlen(str), str);
|
||||
}
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
17
ldo.h
17
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.33 2001/06/05 19:41:24 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -19,13 +19,18 @@
|
||||
#define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
|
||||
|
||||
|
||||
#define luaD_checkstack(L,n) if (L->stack_last-(n)<=L->top) luaD_stackerror(L)
|
||||
|
||||
|
||||
void luaD_init (lua_State *L, int stacksize);
|
||||
void luaD_adjusttop (lua_State *L, StkId base, int extra);
|
||||
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
|
||||
void luaD_call (lua_State *L, StkId func, int nResults);
|
||||
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);
|
||||
void luaD_adjusttop (lua_State *L, StkId newtop);
|
||||
void luaD_lineHook (lua_State *L, int line, lua_Hook linehook);
|
||||
void luaD_call (lua_State *L, StkId func);
|
||||
void luaD_stackerror (lua_State *L);
|
||||
|
||||
void luaD_error (lua_State *L, const l_char *s);
|
||||
void luaD_breakrun (lua_State *L, int errcode);
|
||||
void luaD_checkstack (lua_State *L, int n);
|
||||
int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
64
lfunc.c
64
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.44 2001/06/05 18:17:01 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,66 +7,64 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
#define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
|
||||
#define gcsizeclosure(L, c) numblocks(L, c->nupvalues, sizeof(Closure))
|
||||
|
||||
#define sizeclosure(n) ((int)sizeof(Closure) + (int)sizeof(TObject)*((n)-1))
|
||||
|
||||
|
||||
Closure *luaF_newclosure (lua_State *L, int nelems) {
|
||||
Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
|
||||
(lint32)sizeof(TObject)*(nelems-1));
|
||||
c->next = L->rootcl;
|
||||
L->rootcl = c;
|
||||
Closure *c = (Closure *)luaM_malloc(L, sizeclosure(nelems));
|
||||
c->next = G(L)->rootcl;
|
||||
G(L)->rootcl = c;
|
||||
c->mark = c;
|
||||
c->nupvalues = nelems;
|
||||
L->nblocks += gcsizeclosure(L, c);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L) {
|
||||
Proto *f = luaM_new(L, Proto);
|
||||
f->k = NULL;
|
||||
f->sizek = 0;
|
||||
f->p = NULL;
|
||||
f->sizep = 0;
|
||||
f->code = NULL;
|
||||
f->sizecode = 0;
|
||||
f->nupvalues = 0;
|
||||
f->numparams = 0;
|
||||
f->is_vararg = 0;
|
||||
f->maxstacksize = 0;
|
||||
f->marked = 0;
|
||||
f->lineinfo = NULL;
|
||||
f->sizelocvars = 0;
|
||||
f->sizelineinfo = 0;
|
||||
f->locvars = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->source = NULL;
|
||||
f->kstr = NULL;
|
||||
f->nkstr = 0;
|
||||
f->knum = NULL;
|
||||
f->nknum = 0;
|
||||
f->kproto = NULL;
|
||||
f->nkproto = 0;
|
||||
f->locvars = NULL;
|
||||
f->nlocvars = 0;
|
||||
f->next = L->rootproto;
|
||||
L->rootproto = f;
|
||||
f->marked = 0;
|
||||
L->nblocks += gcsizeproto(L, f);
|
||||
f->next = G(L)->rootproto; /* chain in list of protos */
|
||||
G(L)->rootproto = f;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
L->nblocks -= gcsizeproto(L, f);
|
||||
luaM_free(L, f->code);
|
||||
luaM_free(L, f->locvars);
|
||||
luaM_free(L, f->kstr);
|
||||
luaM_free(L, f->knum);
|
||||
luaM_free(L, f->kproto);
|
||||
luaM_free(L, f->lineinfo);
|
||||
luaM_free(L, f);
|
||||
luaM_freearray(L, f->code, f->sizecode, Instruction);
|
||||
luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
|
||||
luaM_freearray(L, f->k, f->sizek, TObject);
|
||||
luaM_freearray(L, f->p, f->sizep, Proto *);
|
||||
luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
|
||||
luaM_freelem(L, f, Proto);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
L->nblocks -= gcsizeclosure(L, c);
|
||||
luaM_free(L, c);
|
||||
luaM_free(L, c, sizeclosure(c->nupvalues));
|
||||
}
|
||||
|
||||
|
||||
@@ -74,13 +72,13 @@ void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||
** Look for n-th local variable at line `line' in function `func'.
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||
const l_char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||
int i;
|
||||
for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; i++) {
|
||||
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
|
||||
if (pc < f->locvars[i].endpc) { /* is variable active? */
|
||||
local_number--;
|
||||
if (local_number == 0)
|
||||
return f->locvars[i].varname->str;
|
||||
return getstr(f->locvars[i].varname);
|
||||
}
|
||||
}
|
||||
return NULL; /* not found */
|
||||
|
||||
4
lfunc.h
4
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.14 2000/12/28 12:55:41 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,7 +17,7 @@ Closure *luaF_newclosure (lua_State *L, int nelems);
|
||||
void luaF_freeproto (lua_State *L, Proto *f);
|
||||
void luaF_freeclosure (lua_State *L, Closure *c);
|
||||
|
||||
const char *luaF_getlocalname (const Proto *func, int local_number, int pc);
|
||||
const l_char *luaF_getlocalname (const Proto *func, int local_number, int pc);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
442
lgc.c
442
lgc.c
@@ -1,9 +1,10 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.65 2000/09/11 17:38:42 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 1.108 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
@@ -17,6 +18,7 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
|
||||
typedef struct GCState {
|
||||
Hash *tmark; /* list of marked tables to be visited */
|
||||
Closure *cmark; /* list of marked closures to be visited */
|
||||
@@ -24,11 +26,8 @@ typedef struct GCState {
|
||||
|
||||
|
||||
|
||||
static int markobject (GCState *st, TObject *o);
|
||||
|
||||
|
||||
/* mark a string; marks larger than 1 cannot be changed */
|
||||
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
|
||||
#define strmark(s) {if ((s)->tsv.marked == 0) (s)->tsv.marked = 1;}
|
||||
|
||||
|
||||
|
||||
@@ -37,107 +36,142 @@ static void protomark (Proto *f) {
|
||||
int i;
|
||||
f->marked = 1;
|
||||
strmark(f->source);
|
||||
for (i=0; i<f->nkstr; i++)
|
||||
strmark(f->kstr[i]);
|
||||
for (i=0; i<f->nkproto; i++)
|
||||
protomark(f->kproto[i]);
|
||||
for (i=0; i<f->nlocvars; i++) /* mark local-variable names */
|
||||
for (i=0; i<f->sizek; i++) {
|
||||
if (ttype(f->k+i) == LUA_TSTRING)
|
||||
strmark(tsvalue(f->k+i));
|
||||
}
|
||||
for (i=0; i<f->sizep; i++)
|
||||
protomark(f->p[i]);
|
||||
for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
|
||||
strmark(f->locvars[i].varname);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markstack (lua_State *L, GCState *st) {
|
||||
StkId o;
|
||||
for (o=L->stack; o<L->top; o++)
|
||||
markobject(st, o);
|
||||
}
|
||||
|
||||
|
||||
static void marklock (lua_State *L, GCState *st) {
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++) {
|
||||
if (L->refArray[i].st == LOCK)
|
||||
markobject(st, &L->refArray[i].o);
|
||||
static void markclosure (GCState *st, Closure *cl) {
|
||||
if (!ismarked(cl)) {
|
||||
if (!cl->isC) {
|
||||
lua_assert(cl->nupvalues == cl->f.l->nupvalues);
|
||||
protomark(cl->f.l);
|
||||
}
|
||||
cl->mark = st->cmark; /* chain it for later traversal */
|
||||
st->cmark = cl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void marktagmethods (lua_State *L, GCState *st) {
|
||||
int e;
|
||||
for (e=0; e<IM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
markobject(st, luaT_getim(L, t,e));
|
||||
static void marktable (GCState *st, Hash *h) {
|
||||
if (!ismarked(h)) {
|
||||
h->mark = st->tmark; /* chain it for later traversal */
|
||||
st->tmark = h;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (GCState *st, TObject *o) {
|
||||
static void markobject (GCState *st, TObject *o) {
|
||||
switch (ttype(o)) {
|
||||
case TAG_USERDATA: case TAG_STRING:
|
||||
case LUA_TSTRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case TAG_TABLE: {
|
||||
if (!ismarked(hvalue(o))) {
|
||||
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
|
||||
st->tmark = hvalue(o);
|
||||
}
|
||||
case LUA_TUSERDATA:
|
||||
if (!ismarkedudata(uvalue(o)))
|
||||
switchudatamark(uvalue(o));
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
markclosure(st, clvalue(o));
|
||||
break;
|
||||
case LUA_TTABLE: {
|
||||
marktable(st, hvalue(o));
|
||||
break;
|
||||
}
|
||||
case TAG_LMARK: {
|
||||
Closure *cl = infovalue(o)->func;
|
||||
if (!ismarked(cl)) {
|
||||
protomark(cl->f.l);
|
||||
cl->mark = st->cmark; /* chain it for later traversal */
|
||||
st->cmark = cl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TAG_LCLOSURE:
|
||||
protomark(clvalue(o)->f.l);
|
||||
/* go through */
|
||||
case TAG_CCLOSURE: case TAG_CMARK:
|
||||
if (!ismarked(clvalue(o))) {
|
||||
clvalue(o)->mark = st->cmark; /* chain it for later traversal */
|
||||
st->cmark = clvalue(o);
|
||||
}
|
||||
break;
|
||||
default: break; /* numbers, etc */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void markstacks (lua_State *L, GCState *st) {
|
||||
lua_State *L1 = L;
|
||||
do { /* for each thread */
|
||||
StkId o, lim;
|
||||
marktable(st, L1->gt); /* mark table of globals */
|
||||
for (o=L1->stack; o<L1->top; o++)
|
||||
markobject(st, o);
|
||||
lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK
|
||||
: L1->stack_last;
|
||||
for (; o<=lim; o++) setnilvalue(o);
|
||||
lua_assert(L1->previous->next == L1 && L1->next->previous == L1);
|
||||
L1 = L1->next;
|
||||
} while (L1 != L);
|
||||
}
|
||||
|
||||
|
||||
static void marktagmethods (global_State *G, GCState *st) {
|
||||
int t;
|
||||
for (t=0; t<G->ntag; t++) {
|
||||
struct TM *tm = &G->TMtable[t];
|
||||
int e;
|
||||
if (tm->name) strmark(tm->name);
|
||||
for (e=0; e<TM_N; e++) {
|
||||
Closure *cl = tm->method[e];
|
||||
if (cl) markclosure(st, cl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void traverseclosure (GCState *st, Closure *f) {
|
||||
int i;
|
||||
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
|
||||
markobject(st, &f->upvalue[i]);
|
||||
}
|
||||
|
||||
|
||||
static void removekey (Node *n) {
|
||||
lua_assert(ttype(val(n)) == LUA_TNIL);
|
||||
if (ttype(key(n)) != LUA_TNIL && ttype(key(n)) != LUA_TNUMBER)
|
||||
setttype(key(n), LUA_TNONE); /* dead key; remove it */
|
||||
}
|
||||
|
||||
|
||||
static void traversetable (GCState *st, Hash *h) {
|
||||
int i;
|
||||
int mode = h->weakmode;
|
||||
if (mode == (LUA_WEAK_KEY | LUA_WEAK_VALUE))
|
||||
return; /* avoid traversing if both keys and values are weak */
|
||||
for (i=0; i<h->size; i++) {
|
||||
Node *n = node(h, i);
|
||||
if (ttype(val(n)) == LUA_TNIL)
|
||||
removekey(n);
|
||||
else {
|
||||
lua_assert(ttype(key(n)) != LUA_TNIL);
|
||||
if (ttype(key(n)) != LUA_TNUMBER && !(mode & LUA_WEAK_KEY))
|
||||
markobject(st, key(n));
|
||||
if (!(mode & LUA_WEAK_VALUE))
|
||||
markobject(st, val(n));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void markall (lua_State *L) {
|
||||
GCState st;
|
||||
st.cmark = NULL;
|
||||
st.tmark = L->gt; /* put table of globals in mark list */
|
||||
L->gt->mark = NULL;
|
||||
marktagmethods(L, &st); /* mark tag methods */
|
||||
markstack(L, &st); /* mark stack objects */
|
||||
marklock(L, &st); /* mark locked objects */
|
||||
st.tmark = NULL;
|
||||
marktagmethods(G(L), &st); /* mark tag methods */
|
||||
markstacks(L, &st); /* mark all stacks */
|
||||
marktable(&st, G(L)->type2tag);
|
||||
marktable(&st, G(L)->registry);
|
||||
marktable(&st, G(L)->weakregistry);
|
||||
for (;;) { /* mark tables and closures */
|
||||
if (st.cmark) {
|
||||
int i;
|
||||
Closure *f = st.cmark; /* get first closure from list */
|
||||
st.cmark = f->mark; /* remove it from list */
|
||||
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
|
||||
markobject(&st, &f->upvalue[i]);
|
||||
traverseclosure(&st, f);
|
||||
}
|
||||
else if (st.tmark) {
|
||||
int i;
|
||||
Hash *h = st.tmark; /* get first table from list */
|
||||
st.tmark = h->mark; /* remove it from list */
|
||||
for (i=0; i<h->size; i++) {
|
||||
Node *n = node(h, i);
|
||||
if (ttype(key(n)) != TAG_NIL) {
|
||||
if (ttype(val(n)) == TAG_NIL)
|
||||
luaH_remove(h, key(n)); /* dead element; try to remove it */
|
||||
markobject(&st, &n->key);
|
||||
markobject(&st, &n->val);
|
||||
}
|
||||
}
|
||||
traversetable(&st, h);
|
||||
}
|
||||
else break; /* nothing else to mark */
|
||||
}
|
||||
@@ -145,214 +179,208 @@ static void markall (lua_State *L) {
|
||||
|
||||
|
||||
static int hasmark (const TObject *o) {
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
return tsvalue(o)->marked;
|
||||
case TAG_TABLE:
|
||||
switch (ttype(o)) {
|
||||
case LUA_TSTRING:
|
||||
return tsvalue(o)->tsv.marked;
|
||||
case LUA_TUSERDATA:
|
||||
return ismarkedudata(uvalue(o));
|
||||
case LUA_TTABLE:
|
||||
return ismarked(hvalue(o));
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
return ismarked(clvalue(o)->mark);
|
||||
default: /* number */
|
||||
case LUA_TFUNCTION:
|
||||
return ismarked(clvalue(o));
|
||||
default: /* number, nil */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* macro for internal debugging; check if a link of free refs is valid */
|
||||
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
|
||||
|
||||
static void invalidaterefs (lua_State *L) {
|
||||
int n = L->refSize;
|
||||
static void cleardeadnodes (Hash *h) {
|
||||
int i;
|
||||
for (i=0; i<n; i++) {
|
||||
struct Ref *r = &L->refArray[i];
|
||||
if (r->st == HOLD && !hasmark(&r->o))
|
||||
r->st = COLLECTED;
|
||||
LUA_ASSERT((r->st == LOCK && hasmark(&r->o)) ||
|
||||
(r->st == HOLD && hasmark(&r->o)) ||
|
||||
r->st == COLLECTED ||
|
||||
r->st == NONEXT ||
|
||||
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
|
||||
"inconsistent ref table");
|
||||
for (i=0; i<h->size; i++) {
|
||||
Node *n = node(h, i);
|
||||
if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */
|
||||
if (!hasmark(val(n)) || !hasmark(key(n))) {
|
||||
setnilvalue(val(n)); /* remove value */
|
||||
removekey(n);
|
||||
}
|
||||
}
|
||||
LUA_ASSERT(VALIDLINK(L, L->refFree, n), "inconsistent ref table");
|
||||
}
|
||||
|
||||
|
||||
static void cleartables (global_State *G) {
|
||||
Hash *h;
|
||||
for (h = G->roottable; h; h = h->next) {
|
||||
if (h->weakmode && ismarked(h))
|
||||
cleardeadnodes(h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectproto (lua_State *L) {
|
||||
Proto **p = &L->rootproto;
|
||||
Proto *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (next->marked) {
|
||||
next->marked = 0;
|
||||
p = &next->next;
|
||||
Proto **p = &G(L)->rootproto;
|
||||
Proto *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (curr->marked) {
|
||||
curr->marked = 0;
|
||||
p = &curr->next;
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaF_freeproto(L, next);
|
||||
*p = curr->next;
|
||||
luaF_freeproto(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collectclosure (lua_State *L) {
|
||||
Closure **p = &L->rootcl;
|
||||
Closure *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (ismarked(next)) {
|
||||
next->mark = next; /* unmark */
|
||||
p = &next->next;
|
||||
Closure **p = &G(L)->rootcl;
|
||||
Closure *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (ismarked(curr)) {
|
||||
curr->mark = curr; /* unmark */
|
||||
p = &curr->next;
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaF_freeclosure(L, next);
|
||||
*p = curr->next;
|
||||
luaF_freeclosure(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void collecttable (lua_State *L) {
|
||||
Hash **p = &L->roottable;
|
||||
Hash *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if (ismarked(next)) {
|
||||
next->mark = next; /* unmark */
|
||||
p = &next->next;
|
||||
Hash **p = &G(L)->roottable;
|
||||
Hash *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (ismarked(curr)) {
|
||||
curr->mark = curr; /* unmark */
|
||||
p = &curr->next;
|
||||
}
|
||||
else {
|
||||
*p = next->next;
|
||||
luaH_free(L, next);
|
||||
*p = curr->next;
|
||||
luaH_free(L, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void checktab (lua_State *L, stringtable *tb) {
|
||||
if (tb->nuse < (lint32)(tb->size/4) && tb->size > 10)
|
||||
luaS_resize(L, tb, tb->size/2); /* table is too big */
|
||||
static void collectudata (lua_State *L, int keep) {
|
||||
Udata **p = &G(L)->rootudata;
|
||||
Udata *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (ismarkedudata(curr)) {
|
||||
switchudatamark(curr); /* unmark */
|
||||
p = &curr->uv.next;
|
||||
}
|
||||
else { /* collect */
|
||||
int tag = curr->uv.tag;
|
||||
*p = curr->uv.next;
|
||||
if (keep || /* must keep all of them (to close state)? */
|
||||
luaT_gettm(G(L), tag, TM_GC)) { /* or is there a GC tag method? */
|
||||
curr->uv.next = G(L)->TMtable[tag].collected; /* chain udata ... */
|
||||
G(L)->TMtable[tag].collected = curr; /* ... to call its TM later */
|
||||
}
|
||||
else /* no tag method; delete udata */
|
||||
luaM_free(L, curr, sizeudata(curr->uv.len));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** collect all elements with `marked' <= `limit'.
|
||||
** with limit=0, that means all unmarked elements;
|
||||
** with limit=MAX_INT, that means all elements.
|
||||
*/
|
||||
static void collectstringtab (lua_State *L, int limit) {
|
||||
static void collectstrings (lua_State *L, int all) {
|
||||
int i;
|
||||
for (i=0; i<L->strt.size; i++) { /* for each list */
|
||||
TString **p = &L->strt.hash[i];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
if ((int)next->marked > limit) { /* preserve? */
|
||||
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
for (i=0; i<G(L)->strt.size; i++) { /* for each list */
|
||||
TString **p = &G(L)->strt.hash[i];
|
||||
TString *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
if (curr->tsv.marked && !all) { /* preserve? */
|
||||
if (curr->tsv.marked < FIXMARK) /* does not change FIXMARKs */
|
||||
curr->tsv.marked = 0;
|
||||
p = &curr->tsv.nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
*p = next->nexthash;
|
||||
L->strt.nuse--;
|
||||
L->nblocks -= gcsizestring(L, next->u.s.len);
|
||||
luaM_free(L, next);
|
||||
*p = curr->tsv.nexthash;
|
||||
G(L)->strt.nuse--;
|
||||
luaM_free(L, curr, sizestring(curr->tsv.len));
|
||||
}
|
||||
}
|
||||
}
|
||||
checktab(L, &L->strt);
|
||||
if (G(L)->strt.nuse < (ls_nstr)(G(L)->strt.size/4) &&
|
||||
G(L)->strt.size > MINPOWER2)
|
||||
luaS_resize(L, G(L)->strt.size/2); /* table is too big */
|
||||
}
|
||||
|
||||
|
||||
static void collectudatatab (lua_State *L, int all) {
|
||||
int i;
|
||||
for (i=0; i<L->udt.size; i++) { /* for each list */
|
||||
TString **p = &L->udt.hash[i];
|
||||
TString *next;
|
||||
while ((next = *p) != NULL) {
|
||||
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
|
||||
if ((int)next->marked > all) { /* preserve? */
|
||||
next->marked = 0;
|
||||
p = &next->nexthash;
|
||||
}
|
||||
else { /* collect */
|
||||
int tag = next->u.d.tag;
|
||||
if (tag > L->last_tag) tag = TAG_USERDATA;
|
||||
*p = next->nexthash;
|
||||
next->nexthash = L->IMtable[tag].collected; /* chain udata */
|
||||
L->IMtable[tag].collected = next;
|
||||
L->nblocks -= gcsizeudata;
|
||||
L->udt.nuse--;
|
||||
}
|
||||
}
|
||||
|
||||
#define MINBUFFER 256
|
||||
static void checkMbuffer (lua_State *L) {
|
||||
if (G(L)->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
||||
size_t newsize = G(L)->Mbuffsize/2; /* still larger than MINBUFFER */
|
||||
luaM_reallocvector(L, G(L)->Mbuffer, G(L)->Mbuffsize, newsize, l_char);
|
||||
G(L)->Mbuffsize = newsize;
|
||||
}
|
||||
checktab(L, &L->udt);
|
||||
}
|
||||
|
||||
|
||||
static void callgcTM (lua_State *L, const TObject *o) {
|
||||
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
|
||||
if (ttype(im) != TAG_NIL) {
|
||||
luaD_checkstack(L, 2);
|
||||
*(L->top) = *im;
|
||||
*(L->top+1) = *o;
|
||||
static void callgcTM (lua_State *L, const TObject *obj) {
|
||||
Closure *tm = luaT_gettmbyObj(G(L), obj, TM_GC);
|
||||
if (tm != NULL) {
|
||||
int oldah = L->allowhooks;
|
||||
StkId top = L->top;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
||||
setclvalue(top, tm);
|
||||
setobj(top+1, obj);
|
||||
L->top += 2;
|
||||
luaD_call(L, L->top-2, 0);
|
||||
luaD_call(L, top);
|
||||
L->top = top; /* restore top */
|
||||
L->allowhooks = oldah; /* restore hooks */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void callgcTMudata (lua_State *L) {
|
||||
int tag;
|
||||
TObject o;
|
||||
ttype(&o) = TAG_USERDATA;
|
||||
for (tag=L->last_tag; tag>=0; tag--) {
|
||||
TString *udata = L->IMtable[tag].collected;
|
||||
L->IMtable[tag].collected = NULL;
|
||||
while (udata) {
|
||||
TString *next = udata->nexthash;
|
||||
tsvalue(&o) = udata;
|
||||
callgcTM(L, &o);
|
||||
luaM_free(L, udata);
|
||||
udata = next;
|
||||
luaD_checkstack(L, 3);
|
||||
for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
|
||||
Udata *udata;
|
||||
while ((udata = G(L)->TMtable[tag].collected) != NULL) {
|
||||
G(L)->TMtable[tag].collected = udata->uv.next; /* remove it from list */
|
||||
udata->uv.next = G(L)->rootudata; /* resurect it */
|
||||
G(L)->rootudata = udata;
|
||||
setuvalue(L->top, udata);
|
||||
L->top++; /* keep it in stack to avoid being (recursively) collected */
|
||||
callgcTM(L, L->top-1);
|
||||
uvalue(L->top-1)->uv.tag = 0; /* default tag (udata is `finalized') */
|
||||
L->top--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_callallgcTM (lua_State *L) {
|
||||
if (G(L)->rootudata) { /* avoid problems with incomplete states */
|
||||
collectudata(L, 1); /* collect all udata into tag lists */
|
||||
callgcTMudata(L); /* call their GC tag methods */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_collect (lua_State *L, int all) {
|
||||
int oldah = L->allowhooks;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC */
|
||||
L->GCthreshold *= 4; /* to avoid GC during GC */
|
||||
collectudatatab(L, all);
|
||||
callgcTMudata(L);
|
||||
collectstringtab(L, all?MAX_INT:0);
|
||||
collectudata(L, 0);
|
||||
collectstrings(L, all);
|
||||
collecttable(L);
|
||||
collectproto(L);
|
||||
collectclosure(L);
|
||||
L->allowhooks = oldah; /* restore hooks */
|
||||
}
|
||||
|
||||
|
||||
#define MINBUFFER 256
|
||||
|
||||
long lua_collectgarbage (lua_State *L, long limit) {
|
||||
unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
|
||||
void luaC_collectgarbage (lua_State *L) {
|
||||
markall(L);
|
||||
invalidaterefs(L);
|
||||
cleartables(G(L));
|
||||
luaC_collect(L, 0);
|
||||
recovered = recovered - L->nblocks;
|
||||
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
||||
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
||||
L->Mbuffsize /= 2; /* still larger than MINBUFFER */
|
||||
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
||||
}
|
||||
checkMbuffer(L);
|
||||
G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */
|
||||
callgcTMudata(L);
|
||||
callgcTM(L, &luaO_nilobject);
|
||||
return recovered;
|
||||
}
|
||||
|
||||
|
||||
void luaC_checkGC (lua_State *L) {
|
||||
if (L->nblocks >= L->GCthreshold)
|
||||
lua_collectgarbage(L, 0);
|
||||
}
|
||||
|
||||
|
||||
9
lgc.h
9
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.11 2001/06/12 18:43:13 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,8 +11,13 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaC_checkGC (lua_State *L);
|
||||
#define luaC_checkGC(L) if (G(L)->nblocks >= G(L)->GCthreshold) \
|
||||
luaC_collectgarbage(L)
|
||||
|
||||
|
||||
void luaC_callallgcTM (lua_State *L);
|
||||
void luaC_collect (lua_State *L, int all);
|
||||
void luaC_collectgarbage (lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
290
llex.c
290
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.88 2001/06/20 21:07:57 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,16 +9,14 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
@@ -28,17 +26,22 @@
|
||||
|
||||
|
||||
/* ORDER RESERVED */
|
||||
static const char *const token2string [] = {
|
||||
"and", "break", "do", "else", "elseif", "end", "for",
|
||||
"function", "if", "local", "nil", "not", "or", "repeat", "return", "then",
|
||||
"until", "while", "", "..", "...", "==", ">=", "<=", "~=", "", "", "<eof>"};
|
||||
static const l_char *const token2string [] = {
|
||||
l_s("and"), l_s("break"), l_s("do"), l_s("else"), l_s("elseif"),
|
||||
l_s("end"), l_s("for"), l_s("function"), l_s("global"), l_s("if"),
|
||||
l_s("in"), l_s("local"), l_s("nil"), l_s("not"), l_s("or"), l_s("repeat"),
|
||||
l_s("return"), l_s("then"), l_s("until"), l_s("while"), l_s(""),
|
||||
l_s(".."), l_s("..."), l_s("=="), l_s(">="), l_s("<="), l_s("~="),
|
||||
l_s(""), l_s(""), l_s("<eof>")
|
||||
};
|
||||
|
||||
|
||||
void luaX_init (lua_State *L) {
|
||||
int i;
|
||||
for (i=0; i<NUM_RESERVED; i++) {
|
||||
TString *ts = luaS_new(L, token2string[i]);
|
||||
ts->marked = (unsigned char)(RESERVEDMARK+i); /* reserved word */
|
||||
lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN);
|
||||
ts->tsv.marked = (unsigned short)(RESERVEDMARK+i); /* reserved word */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,37 +49,38 @@ void luaX_init (lua_State *L) {
|
||||
#define MAXSRC 80
|
||||
|
||||
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const l_char *msg) {
|
||||
if (val > limit) {
|
||||
char buff[100];
|
||||
sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
|
||||
l_char buff[90];
|
||||
sprintf(buff, l_s("too many %.40s (limit=%d)"), msg, limit);
|
||||
luaX_error(ls, buff, ls->t.token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
|
||||
char buff[MAXSRC];
|
||||
luaO_chunkid(buff, ls->source->str, sizeof(buff));
|
||||
luaO_verror(ls->L, "%.100s;\n last token read: `%.50s' at line %d in %.80s",
|
||||
void luaX_syntaxerror (LexState *ls, const l_char *s, const l_char *token) {
|
||||
l_char buff[MAXSRC];
|
||||
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
|
||||
luaO_verror(ls->L,
|
||||
l_s("%.99s;\n last token read: `%.30s' at line %d in %.80s"),
|
||||
s, token, ls->linenumber, buff);
|
||||
}
|
||||
|
||||
|
||||
void luaX_error (LexState *ls, const char *s, int token) {
|
||||
char buff[TOKEN_LEN];
|
||||
void luaX_error (LexState *ls, const l_char *s, int token) {
|
||||
l_char buff[TOKEN_LEN];
|
||||
luaX_token2str(token, buff);
|
||||
if (buff[0] == '\0')
|
||||
luaX_syntaxerror(ls, s, ls->L->Mbuffer);
|
||||
if (buff[0] == l_c('\0'))
|
||||
luaX_syntaxerror(ls, s, (l_char *)G(ls->L)->Mbuffer);
|
||||
else
|
||||
luaX_syntaxerror(ls, s, buff);
|
||||
}
|
||||
|
||||
|
||||
void luaX_token2str (int token, char *s) {
|
||||
void luaX_token2str (int token, l_char *s) {
|
||||
if (token < 256) {
|
||||
s[0] = (char)token;
|
||||
s[1] = '\0';
|
||||
s[0] = (l_char)token;
|
||||
s[1] = l_c('\0');
|
||||
}
|
||||
else
|
||||
strcpy(s, token2string[token-FIRST_RESERVED]);
|
||||
@@ -84,16 +88,16 @@ void luaX_token2str (int token, char *s) {
|
||||
|
||||
|
||||
static void luaX_invalidchar (LexState *ls, int c) {
|
||||
char buff[8];
|
||||
sprintf(buff, "0x%02X", c);
|
||||
luaX_syntaxerror(ls, "invalid control char", buff);
|
||||
l_char buff[8];
|
||||
sprintf(buff, l_s("0x%02X"), uchar(c));
|
||||
luaX_syntaxerror(ls, l_s("invalid control char"), buff);
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS) {
|
||||
next(LS); /* skip '\n' */
|
||||
next(LS); /* skip `\n' */
|
||||
++LS->linenumber;
|
||||
luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
|
||||
luaX_checklimit(LS, LS->linenumber, MAX_INT, l_s("lines in a chunk"));
|
||||
}
|
||||
|
||||
|
||||
@@ -106,10 +110,10 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
|
||||
LS->lastline = 1;
|
||||
LS->source = source;
|
||||
next(LS); /* read first char */
|
||||
if (LS->current == '#') {
|
||||
if (LS->current == l_c('#')) {
|
||||
do { /* skip first line */
|
||||
next(LS);
|
||||
} while (LS->current != '\n' && LS->current != EOZ);
|
||||
} while (LS->current != l_c('\n') && LS->current != EOZ);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,108 +129,112 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
|
||||
/* use Mbuffer to store names, literal strings and numbers */
|
||||
|
||||
#define EXTRABUFF 128
|
||||
#define checkbuffer(L, n, len) if ((len)+(n) > L->Mbuffsize) \
|
||||
luaO_openspace(L, (len)+(n)+EXTRABUFF)
|
||||
#define checkbuffer(L, n, len) \
|
||||
if (((len)+(n))*sizeof(l_char) > G(L)->Mbuffsize) \
|
||||
luaO_openspace(L, (len)+(n)+EXTRABUFF, l_char)
|
||||
|
||||
#define save(L, c, l) (L->Mbuffer[l++] = (char)c)
|
||||
#define save(L, c, l) (((l_char *)G(L)->Mbuffer)[l++] = (l_char)c)
|
||||
#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
|
||||
|
||||
|
||||
static const char *readname (LexState *LS) {
|
||||
static size_t readname (LexState *LS) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
do {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(L, '\0', l);
|
||||
return L->Mbuffer;
|
||||
} while (isalnum(LS->current) || LS->current == l_c('_'));
|
||||
save(L, l_c('\0'), l);
|
||||
return l-1;
|
||||
}
|
||||
|
||||
|
||||
/* LUA_NUMBER */
|
||||
static void read_number (LexState *LS, int comma) {
|
||||
static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
if (comma) save(L, '.', l);
|
||||
if (comma) save(L, l_c('.'), l);
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == '.') {
|
||||
if (LS->current == l_c('.')) {
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == '.') {
|
||||
if (LS->current == l_c('.')) {
|
||||
save_and_next(L, LS, l);
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "ambiguous syntax"
|
||||
" (decimal point x string concatenation)", TK_NUMBER);
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS,
|
||||
l_s("ambiguous syntax (decimal point x string concatenation)"),
|
||||
TK_NUMBER);
|
||||
}
|
||||
}
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
if (LS->current == 'e' || LS->current == 'E') {
|
||||
save_and_next(L, LS, l); /* read 'E' */
|
||||
if (LS->current == '+' || LS->current == '-')
|
||||
if (LS->current == l_c('e') || LS->current == l_c('E')) {
|
||||
save_and_next(L, LS, l); /* read `E' */
|
||||
if (LS->current == l_c('+') || LS->current == l_c('-'))
|
||||
save_and_next(L, LS, l); /* optional exponent sign */
|
||||
while (isdigit(LS->current)) {
|
||||
checkbuffer(L, 10, l);
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
}
|
||||
save(L, '\0', l);
|
||||
if (!luaO_str2d(L->Mbuffer, &LS->t.seminfo.r))
|
||||
luaX_error(LS, "malformed number", TK_NUMBER);
|
||||
save(L, l_c('\0'), l);
|
||||
if (!luaO_str2d((l_char *)G(L)->Mbuffer, &seminfo->r))
|
||||
luaX_error(LS, l_s("malformed number"), TK_NUMBER);
|
||||
}
|
||||
|
||||
|
||||
static void read_long_string (LexState *LS) {
|
||||
static void read_long_string (LexState *LS, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
int cont = 0;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
save(L, '[', l); /* save first '[' */
|
||||
save_and_next(L, LS, l); /* pass the second '[' */
|
||||
save(L, l_c('['), l); /* save first `[' */
|
||||
save_and_next(L, LS, l); /* pass the second `[' */
|
||||
if (LS->current == l_c('\n')) /* string starts with a newline? */
|
||||
inclinenumber(LS); /* skip it */
|
||||
for (;;) {
|
||||
checkbuffer(L, 10, l);
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "unfinished long string", TK_STRING);
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS, l_s("unfinished long string"), TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case '[':
|
||||
case l_c('['):
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == '[') {
|
||||
if (LS->current == l_c('[')) {
|
||||
cont++;
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
case l_c(']'):
|
||||
save_and_next(L, LS, l);
|
||||
if (LS->current == ']') {
|
||||
if (LS->current == l_c(']')) {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save(L, '\n', l);
|
||||
case l_c('\n'):
|
||||
save(L, l_c('\n'), l);
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
default:
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(L, LS, l); /* skip the second ']' */
|
||||
save(L, '\0', l);
|
||||
LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+2, l-5);
|
||||
save_and_next(L, LS, l); /* skip the second `]' */
|
||||
save(L, l_c('\0'), l);
|
||||
seminfo->ts = luaS_newlstr(L, (l_char *)G(L)->Mbuffer+2, l-5);
|
||||
}
|
||||
|
||||
|
||||
static void read_string (LexState *LS, int del) {
|
||||
static void read_string (LexState *LS, int del, SemInfo *seminfo) {
|
||||
lua_State *L = LS->L;
|
||||
size_t l = 0;
|
||||
checkbuffer(L, 10, l);
|
||||
@@ -234,38 +242,38 @@ static void read_string (LexState *LS, int del) {
|
||||
while (LS->current != del) {
|
||||
checkbuffer(L, 10, l);
|
||||
switch (LS->current) {
|
||||
case EOZ: case '\n':
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "unfinished string", TK_STRING);
|
||||
case EOZ: case l_c('\n'):
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS, l_s("unfinished string"), TK_STRING);
|
||||
break; /* to avoid warnings */
|
||||
case '\\':
|
||||
next(LS); /* do not save the '\' */
|
||||
case l_c('\\'):
|
||||
next(LS); /* do not save the `\' */
|
||||
switch (LS->current) {
|
||||
case 'a': save(L, '\a', l); next(LS); break;
|
||||
case 'b': save(L, '\b', l); next(LS); break;
|
||||
case 'f': save(L, '\f', l); next(LS); break;
|
||||
case 'n': save(L, '\n', l); next(LS); break;
|
||||
case 'r': save(L, '\r', l); next(LS); break;
|
||||
case 't': save(L, '\t', l); next(LS); break;
|
||||
case 'v': save(L, '\v', l); next(LS); break;
|
||||
case '\n': save(L, '\n', l); inclinenumber(LS); break;
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9': {
|
||||
int c = 0;
|
||||
int i = 0;
|
||||
do {
|
||||
c = 10*c + (LS->current-'0');
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c != (unsigned char)c) {
|
||||
save(L, '\0', l);
|
||||
luaX_error(LS, "escape sequence too large", TK_STRING);
|
||||
case l_c('a'): save(L, l_c('\a'), l); next(LS); break;
|
||||
case l_c('b'): save(L, l_c('\b'), l); next(LS); break;
|
||||
case l_c('f'): save(L, l_c('\f'), l); next(LS); break;
|
||||
case l_c('n'): save(L, l_c('\n'), l); next(LS); break;
|
||||
case l_c('r'): save(L, l_c('\r'), l); next(LS); break;
|
||||
case l_c('t'): save(L, l_c('\t'), l); next(LS); break;
|
||||
case l_c('v'): save(L, l_c('\v'), l); next(LS); break;
|
||||
case l_c('\n'): save(L, l_c('\n'), l); inclinenumber(LS); break;
|
||||
default: {
|
||||
if (!isdigit(LS->current))
|
||||
save_and_next(L, LS, l); /* handles \\, \", \', and \? */
|
||||
else { /* \xxx */
|
||||
int c = 0;
|
||||
int i = 0;
|
||||
do {
|
||||
c = 10*c + (LS->current-l_c('0'));
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c > UCHAR_MAX) {
|
||||
save(L, l_c('\0'), l);
|
||||
luaX_error(LS, l_s("escape sequence too large"), TK_STRING);
|
||||
}
|
||||
save(L, c, l);
|
||||
}
|
||||
save(L, c, l);
|
||||
break;
|
||||
}
|
||||
default: /* handles \\, \", \', and \? */
|
||||
save_and_next(L, LS, l);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -273,107 +281,109 @@ static void read_string (LexState *LS, int del) {
|
||||
}
|
||||
}
|
||||
save_and_next(L, LS, l); /* skip delimiter */
|
||||
save(L, '\0', l);
|
||||
LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+1, l-3);
|
||||
save(L, l_c('\0'), l);
|
||||
seminfo->ts = luaS_newlstr(L, (l_char *)G(L)->Mbuffer+1, l-3);
|
||||
}
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS) {
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo) {
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
|
||||
case l_c(' '): case l_c('\t'): case l_c('\r'): /* `\r' to avoid problems with DOS */
|
||||
next(LS);
|
||||
continue;
|
||||
|
||||
case '\n':
|
||||
case l_c('\n'):
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
|
||||
case '$':
|
||||
luaX_error(LS, "unexpected `$' (pragmas are no longer supported)", '$');
|
||||
case l_c('$'):
|
||||
luaX_error(LS,
|
||||
l_s("unexpected `$' (pragmas are no longer supported)"),
|
||||
LS->current);
|
||||
break;
|
||||
|
||||
case '-':
|
||||
case l_c('-'):
|
||||
next(LS);
|
||||
if (LS->current != '-') return '-';
|
||||
do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
|
||||
if (LS->current != l_c('-')) return l_c('-');
|
||||
do { next(LS); } while (LS->current != l_c('\n') && LS->current != EOZ);
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
case l_c('['):
|
||||
next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
if (LS->current != l_c('[')) return l_c('[');
|
||||
else {
|
||||
read_long_string(LS);
|
||||
read_long_string(LS, seminfo);
|
||||
return TK_STRING;
|
||||
}
|
||||
|
||||
case '=':
|
||||
case l_c('='):
|
||||
next(LS);
|
||||
if (LS->current != '=') return '=';
|
||||
if (LS->current != l_c('=')) return l_c('=');
|
||||
else { next(LS); return TK_EQ; }
|
||||
|
||||
case '<':
|
||||
case l_c('<'):
|
||||
next(LS);
|
||||
if (LS->current != '=') return '<';
|
||||
if (LS->current != l_c('=')) return l_c('<');
|
||||
else { next(LS); return TK_LE; }
|
||||
|
||||
case '>':
|
||||
case l_c('>'):
|
||||
next(LS);
|
||||
if (LS->current != '=') return '>';
|
||||
if (LS->current != l_c('=')) return l_c('>');
|
||||
else { next(LS); return TK_GE; }
|
||||
|
||||
case '~':
|
||||
case l_c('~'):
|
||||
next(LS);
|
||||
if (LS->current != '=') return '~';
|
||||
if (LS->current != l_c('=')) return l_c('~');
|
||||
else { next(LS); return TK_NE; }
|
||||
|
||||
case '"':
|
||||
case '\'':
|
||||
read_string(LS, LS->current);
|
||||
case l_c('"'):
|
||||
case l_c('\''):
|
||||
read_string(LS, LS->current, seminfo);
|
||||
return TK_STRING;
|
||||
|
||||
case '.':
|
||||
case l_c('.'):
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
if (LS->current == l_c('.')) {
|
||||
next(LS);
|
||||
if (LS->current == '.') {
|
||||
if (LS->current == l_c('.')) {
|
||||
next(LS);
|
||||
return TK_DOTS; /* ... */
|
||||
}
|
||||
else return TK_CONCAT; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
else if (!isdigit(LS->current)) return l_c('.');
|
||||
else {
|
||||
read_number(LS, 1);
|
||||
read_number(LS, 1, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
read_number(LS, 0);
|
||||
return TK_NUMBER;
|
||||
|
||||
case EOZ:
|
||||
return TK_EOS;
|
||||
|
||||
case '_': goto tname;
|
||||
|
||||
default:
|
||||
if (!isalpha(LS->current)) {
|
||||
int c = LS->current;
|
||||
default: {
|
||||
if (isdigit(LS->current)) {
|
||||
read_number(LS, 0, seminfo);
|
||||
return TK_NUMBER;
|
||||
}
|
||||
else if (isalpha(LS->current) || LS->current == l_c('_')) {
|
||||
/* identifier or reserved word */
|
||||
size_t l = readname(LS);
|
||||
TString *ts = luaS_newlstr(LS->L, (l_char *)G(LS->L)->Mbuffer, l);
|
||||
if (ts->tsv.marked >= RESERVEDMARK) /* reserved word? */
|
||||
return ts->tsv.marked-RESERVEDMARK+FIRST_RESERVED;
|
||||
seminfo->ts = ts;
|
||||
return TK_NAME;
|
||||
}
|
||||
else {
|
||||
l_charint c = LS->current;
|
||||
if (iscntrl(c))
|
||||
luaX_invalidchar(LS, c);
|
||||
next(LS);
|
||||
return c;
|
||||
}
|
||||
tname: { /* identifier or reserved word */
|
||||
TString *ts = luaS_new(LS->L, readname(LS));
|
||||
if (ts->marked >= RESERVEDMARK) /* reserved word? */
|
||||
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
|
||||
LS->t.seminfo.ts = ts;
|
||||
return TK_NAME;
|
||||
return c; /* single-char tokens (+ - / ...) */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
35
llex.h
35
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.36 2001/06/20 21:07:57 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
#define FIRST_RESERVED 257
|
||||
|
||||
/* maximum length of a reserved word (+1 for final 0) */
|
||||
#define TOKEN_LEN 15
|
||||
/* maximum length of a reserved word */
|
||||
#define TOKEN_LEN (sizeof(l_s("function"))/sizeof(l_char))
|
||||
|
||||
|
||||
/*
|
||||
@@ -24,8 +24,9 @@
|
||||
enum RESERVED {
|
||||
/* terminal symbols denoted by reserved words */
|
||||
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FOR, TK_FUNCTION, TK_IF, TK_LOCAL,
|
||||
TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN, TK_UNTIL, TK_WHILE,
|
||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FOR, TK_FUNCTION, TK_GLOBAL, TK_IF,
|
||||
TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN,
|
||||
TK_UNTIL, TK_WHILE,
|
||||
/* other terminal symbols */
|
||||
TK_NAME, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
|
||||
TK_STRING, TK_EOS
|
||||
@@ -35,16 +36,20 @@ enum RESERVED {
|
||||
#define NUM_RESERVED ((int)(TK_WHILE-FIRST_RESERVED+1))
|
||||
|
||||
|
||||
typedef union {
|
||||
lua_Number r;
|
||||
TString *ts;
|
||||
} SemInfo; /* semantics information */
|
||||
|
||||
|
||||
typedef struct Token {
|
||||
int token;
|
||||
union {
|
||||
Number r;
|
||||
TString *ts;
|
||||
} seminfo; /* semantics information */
|
||||
SemInfo seminfo;
|
||||
} Token;
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* current character */
|
||||
l_charint current; /* current character */
|
||||
Token t; /* current token */
|
||||
Token lookahead; /* look ahead token */
|
||||
struct FuncState *fs; /* `FuncState' is private to the parser */
|
||||
@@ -58,11 +63,11 @@ typedef struct LexState {
|
||||
|
||||
void luaX_init (lua_State *L);
|
||||
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source);
|
||||
int luaX_lex (LexState *LS);
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg);
|
||||
void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
|
||||
void luaX_error (LexState *ls, const char *s, int token);
|
||||
void luaX_token2str (int token, char *s);
|
||||
int luaX_lex (LexState *LS, SemInfo *seminfo);
|
||||
void luaX_checklimit (LexState *ls, int val, int limit, const l_char *msg);
|
||||
void luaX_syntaxerror (LexState *ls, const l_char *s, const l_char *token);
|
||||
void luaX_error (LexState *ls, const l_char *s, int token);
|
||||
void luaX_token2str (int token, l_char *s);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
147
llimits.h
147
llimits.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.13 2000/08/28 17:57:04 roberto Exp roberto $
|
||||
** Limits, basic types, and some other "installation-dependent" definitions
|
||||
** $Id: llimits.h,v 1.29 2001/06/05 18:17:01 roberto Exp roberto $
|
||||
** Limits, basic types, and some other `installation-dependent' definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** try to find number of bits in an integer
|
||||
@@ -32,17 +34,26 @@
|
||||
|
||||
|
||||
/*
|
||||
** Define the type `number' of Lua
|
||||
** GREP LUA_NUMBER to change that
|
||||
** the following types define integer types for values that may not
|
||||
** fit in a `small int' (16 bits), but may waste space in a
|
||||
** `large long' (64 bits). The current definitions should work in
|
||||
** any machine, but may not be optimal.
|
||||
*/
|
||||
#ifndef LUA_NUM_TYPE
|
||||
#define LUA_NUM_TYPE double
|
||||
#endif
|
||||
|
||||
typedef LUA_NUM_TYPE Number;
|
||||
/* an unsigned integer to hold hash values */
|
||||
typedef unsigned int lu_hash;
|
||||
/* its signed equivalent */
|
||||
typedef int ls_hash;
|
||||
|
||||
/* an unsigned integer big enough to count the total memory used by Lua */
|
||||
typedef unsigned long lu_mem;
|
||||
|
||||
/* an integer big enough to count the number of strings in use */
|
||||
typedef long ls_nstr;
|
||||
|
||||
|
||||
typedef unsigned long lint32; /* unsigned int with at least 32 bits */
|
||||
/* chars used as small naturals (so that `char' is reserved for characteres) */
|
||||
typedef unsigned char lu_byte;
|
||||
|
||||
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0)-2)
|
||||
@@ -51,21 +62,16 @@ typedef unsigned long lint32; /* unsigned int with at least 32 bits */
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||
|
||||
/*
|
||||
** conversion of pointer to int (for hashing only)
|
||||
** conversion of pointer to integer
|
||||
** this is for hashing only; there is no problem if the integer
|
||||
** cannot hold the whole pointer value
|
||||
** (the shift removes bits that are usually 0 because of alignment)
|
||||
*/
|
||||
#define IntPoint(p) (((unsigned long)(p)) >> 3)
|
||||
#define IntPoint(p) ((((lu_hash)(p)) >> 4) ^ (lu_hash)(p))
|
||||
|
||||
|
||||
/*
|
||||
** 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,95 +81,34 @@ typedef unsigned long lint32; /* unsigned int with at least 32 bits */
|
||||
|
||||
|
||||
|
||||
/* type to ensure maximum alignment */
|
||||
#ifndef LUSER_ALIGNMENT_T
|
||||
#define LUSER_ALIGNMENT_T double
|
||||
#endif
|
||||
union L_Umaxalign { LUSER_ALIGNMENT_T u; void *s; long l; };
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** type for virtual-machine instructions
|
||||
** must be an unsigned with 4 bytes (see details in lopcodes.h)
|
||||
** For a very small machine, you may change that to 2 bytes (and adjust
|
||||
** the following limits accordingly)
|
||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||
*/
|
||||
typedef unsigned long Instruction;
|
||||
|
||||
|
||||
/*
|
||||
** size and position of opcode arguments.
|
||||
** For an instruction with 2 bytes, size is 16, and size_b can be 5
|
||||
** (accordingly, size_u will be 10, and size_a will be 5)
|
||||
*/
|
||||
#define SIZE_INSTRUCTION 32
|
||||
#define SIZE_B 9
|
||||
|
||||
#define SIZE_OP 6
|
||||
#define SIZE_U (SIZE_INSTRUCTION-SIZE_OP)
|
||||
#define POS_U SIZE_OP
|
||||
#define POS_B SIZE_OP
|
||||
#define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B))
|
||||
#define POS_A (SIZE_OP+SIZE_B)
|
||||
|
||||
|
||||
/*
|
||||
** limits for opcode arguments.
|
||||
** we use (signed) int to manipulate most arguments,
|
||||
** so they must fit in BITS_INT-1 bits (-1 for signal)
|
||||
*/
|
||||
#if SIZE_U < BITS_INT-1
|
||||
#define MAXARG_U ((1<<SIZE_U)-1)
|
||||
#define MAXARG_S (MAXARG_U>>1) /* `S' is signed */
|
||||
#else
|
||||
#define MAXARG_U MAX_INT
|
||||
#define MAXARG_S MAX_INT
|
||||
#endif
|
||||
|
||||
#if SIZE_A < BITS_INT-1
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#else
|
||||
#define MAXARG_A MAX_INT
|
||||
#endif
|
||||
|
||||
#if SIZE_B < BITS_INT-1
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
#else
|
||||
#define MAXARG_B MAX_INT
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum stack size in a function */
|
||||
#ifndef MAXSTACK
|
||||
/* maximum stack for a Lua function */
|
||||
#define MAXSTACK 250
|
||||
#endif
|
||||
|
||||
#if MAXSTACK > MAXARG_B
|
||||
#undef MAXSTACK
|
||||
#define MAXSTACK MAXARG_B
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of local variables */
|
||||
#ifndef MAXLOCALS
|
||||
#define MAXLOCALS 200 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
#if MAXLOCALS>=MAXSTACK
|
||||
#undef MAXLOCALS
|
||||
#define MAXLOCALS (MAXSTACK-1)
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of upvalues */
|
||||
#ifndef MAXUPVALUES
|
||||
#define MAXUPVALUES 32 /* arbitrary limit (<=MAXARG_B) */
|
||||
#endif
|
||||
#if MAXUPVALUES>MAXARG_B
|
||||
#undef MAXUPVALUES
|
||||
#define MAXUPVALUES MAXARG_B
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum number of variables in the left side of an assignment */
|
||||
#ifndef MAXVARSLH
|
||||
#define MAXVARSLH 100 /* arbitrary limit (<MULT_RET) */
|
||||
#endif
|
||||
#if MAXVARSLH>=MULT_RET
|
||||
#undef MAXVARSLH
|
||||
#define MAXVARSLH (MULT_RET-1)
|
||||
#define MAXUPVALUES 32 /* arbitrary limit (<MAXSTACK) */
|
||||
#endif
|
||||
|
||||
|
||||
@@ -171,33 +116,17 @@ typedef unsigned long Instruction;
|
||||
#ifndef MAXPARAMS
|
||||
#define MAXPARAMS 100 /* arbitrary limit (<MAXLOCALS) */
|
||||
#endif
|
||||
#if MAXPARAMS>=MAXLOCALS
|
||||
#undef MAXPARAMS
|
||||
#define MAXPARAMS (MAXLOCALS-1)
|
||||
#endif
|
||||
|
||||
|
||||
/* number of list items to accumulate before a SETLIST instruction */
|
||||
/* (must be a power of 2) */
|
||||
#define LFIELDS_PER_FLUSH 64
|
||||
#if LFIELDS_PER_FLUSH>(MAXSTACK/4)
|
||||
#undef LFIELDS_PER_FLUSH
|
||||
#define LFIELDS_PER_FLUSH (MAXSTACK/4)
|
||||
#endif
|
||||
|
||||
/* number of record items to accumulate before a SETMAP instruction */
|
||||
/* (each item counts 2 elements on the stack: an index and a value) */
|
||||
#define RFIELDS_PER_FLUSH (LFIELDS_PER_FLUSH/2)
|
||||
|
||||
|
||||
/* maximum lookback to find a real constant (for code generation) */
|
||||
#ifndef LOOKBACKNUMS
|
||||
#define LOOKBACKNUMS 20 /* arbitrary constant */
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum part of a file name kept for error messages */
|
||||
#ifndef MAXFILENAME
|
||||
#define MAXFILENAME 260
|
||||
#define LOOKBACKNUMS 40 /* arbitrary constant */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
81
lmathlib.c
81
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.37 2001/03/06 20:09:38 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -139,10 +140,10 @@ static int math_ldexp (lua_State *L) {
|
||||
|
||||
static int math_min (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
double dmin = luaL_check_number(L, 1);
|
||||
lua_Number dmin = luaL_check_number(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
double d = luaL_check_number(L, i);
|
||||
lua_Number d = luaL_check_number(L, i);
|
||||
if (d < dmin)
|
||||
dmin = d;
|
||||
}
|
||||
@@ -153,10 +154,10 @@ static int math_min (lua_State *L) {
|
||||
|
||||
static int math_max (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
double dmax = luaL_check_number(L, 1);
|
||||
lua_Number dmax = luaL_check_number(L, 1);
|
||||
int i;
|
||||
for (i=2; i<=n; i++) {
|
||||
double d = luaL_check_number(L, i);
|
||||
lua_Number d = luaL_check_number(L, i);
|
||||
if (d > dmax)
|
||||
dmax = d;
|
||||
}
|
||||
@@ -166,9 +167,9 @@ static int math_max (lua_State *L) {
|
||||
|
||||
|
||||
static int math_random (lua_State *L) {
|
||||
/* the '%' avoids the (rare) case of r==1, and is needed also because on
|
||||
some systems (SunOS!) "rand()" may return a value larger than RAND_MAX */
|
||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||
/* the `%' avoids the (rare) case of r==1, and is needed also because on
|
||||
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
|
||||
lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
|
||||
switch (lua_gettop(L)) { /* check number of arguments */
|
||||
case 0: { /* no arguments */
|
||||
lua_pushnumber(L, r); /* Number between 0 and 1 */
|
||||
@@ -176,18 +177,18 @@ static int math_random (lua_State *L) {
|
||||
}
|
||||
case 1: { /* only upper limit */
|
||||
int u = luaL_check_int(L, 1);
|
||||
luaL_arg_check(L, 1<=u, 1, "interval is empty");
|
||||
luaL_arg_check(L, 1<=u, 1, l_s("interval is empty"));
|
||||
lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
|
||||
break;
|
||||
}
|
||||
case 2: { /* lower and upper limits */
|
||||
int l = luaL_check_int(L, 1);
|
||||
int u = luaL_check_int(L, 2);
|
||||
luaL_arg_check(L, l<=u, 2, "interval is empty");
|
||||
luaL_arg_check(L, l<=u, 2, l_s("interval is empty"));
|
||||
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
|
||||
break;
|
||||
}
|
||||
default: lua_error(L, "wrong number of arguments");
|
||||
default: lua_error(L, l_s("wrong number of arguments"));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -199,41 +200,41 @@ static int math_randomseed (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg mathlib[] = {
|
||||
{"abs", math_abs},
|
||||
{"sin", math_sin},
|
||||
{"cos", math_cos},
|
||||
{"tan", math_tan},
|
||||
{"asin", math_asin},
|
||||
{"acos", math_acos},
|
||||
{"atan", math_atan},
|
||||
{"atan2", math_atan2},
|
||||
{"ceil", math_ceil},
|
||||
{"floor", math_floor},
|
||||
{"mod", math_mod},
|
||||
{"frexp", math_frexp},
|
||||
{"ldexp", math_ldexp},
|
||||
{"sqrt", math_sqrt},
|
||||
{"min", math_min},
|
||||
{"max", math_max},
|
||||
{"log", math_log},
|
||||
{"log10", math_log10},
|
||||
{"exp", math_exp},
|
||||
{"deg", math_deg},
|
||||
{"rad", math_rad},
|
||||
{"random", math_random},
|
||||
{"randomseed", math_randomseed}
|
||||
static const luaL_reg mathlib[] = {
|
||||
{l_s("abs"), math_abs},
|
||||
{l_s("sin"), math_sin},
|
||||
{l_s("cos"), math_cos},
|
||||
{l_s("tan"), math_tan},
|
||||
{l_s("asin"), math_asin},
|
||||
{l_s("acos"), math_acos},
|
||||
{l_s("atan"), math_atan},
|
||||
{l_s("atan2"), math_atan2},
|
||||
{l_s("ceil"), math_ceil},
|
||||
{l_s("floor"), math_floor},
|
||||
{l_s("mod"), math_mod},
|
||||
{l_s("frexp"), math_frexp},
|
||||
{l_s("ldexp"), math_ldexp},
|
||||
{l_s("sqrt"), math_sqrt},
|
||||
{l_s("min"), math_min},
|
||||
{l_s("max"), math_max},
|
||||
{l_s("log"), math_log},
|
||||
{l_s("log10"), math_log10},
|
||||
{l_s("exp"), math_exp},
|
||||
{l_s("deg"), math_deg},
|
||||
{l_s("rad"), math_rad},
|
||||
{l_s("random"), math_random},
|
||||
{l_s("randomseed"), math_randomseed}
|
||||
};
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
void lua_mathlibopen (lua_State *L) {
|
||||
LUALIB_API int lua_mathlibopen (lua_State *L) {
|
||||
luaL_openl(L, mathlib);
|
||||
lua_pushnumber(L, 0); /* to get its tag */
|
||||
lua_pushcfunction(L, math_pow);
|
||||
lua_settagmethod(L, lua_tag(L, -2), "pow");
|
||||
lua_pop(L, 1); /* remove number */
|
||||
lua_pushnumber(L, PI); lua_setglobal(L, "PI");
|
||||
lua_settagmethod(L, LUA_TNUMBER, l_s("pow"));
|
||||
lua_pushnumber(L, PI);
|
||||
lua_setglobal(L, l_s("PI"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
155
lmem.c
155
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.48 2001/02/23 17:17:25 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
@@ -15,139 +16,53 @@
|
||||
#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)
|
||||
|
||||
#ifndef l_realloc
|
||||
#define l_realloc(b,os,s) realloc(b,s)
|
||||
#define l_free(b,s) free(b)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
/*
|
||||
** {======================================================================
|
||||
** Controlled version for realloc.
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#undef realloc
|
||||
#undef malloc
|
||||
#undef free
|
||||
#define realloc(b, s) debug_realloc(b, s)
|
||||
#define malloc(b) debug_realloc(NULL, 0)
|
||||
#define free(b) debug_realloc(b, 0)
|
||||
|
||||
|
||||
/* ensures maximum alignment for HEADER */
|
||||
union L_U { double d; char *s; long l; };
|
||||
#define HEADER (sizeof(union L_U))
|
||||
|
||||
#define MARKSIZE 16
|
||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||
|
||||
|
||||
#define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
|
||||
|
||||
unsigned long memdebug_numblocks = 0;
|
||||
unsigned long memdebug_total = 0;
|
||||
unsigned long memdebug_maxmem = 0;
|
||||
unsigned long memdebug_memlimit = LONG_MAX;
|
||||
|
||||
|
||||
static void *checkblock (void *block) {
|
||||
unsigned long *b = blocksize(block);
|
||||
unsigned long size = *b;
|
||||
int i;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
||||
memdebug_numblocks--;
|
||||
memdebug_total -= size;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
static void freeblock (void *block) {
|
||||
if (block) {
|
||||
size_t size = *blocksize(block);
|
||||
block = checkblock(block);
|
||||
memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
|
||||
(free)(block); /* free original block */
|
||||
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
||||
int limit, const l_char *errormsg) {
|
||||
void *newblock;
|
||||
int newsize = (*size)*2;
|
||||
if (newsize < MINPOWER2)
|
||||
newsize = MINPOWER2; /* minimum size */
|
||||
else if (*size >= limit/2) { /* cannot double it? */
|
||||
if (*size < limit - MINPOWER2) /* try something smaller... */
|
||||
newsize = limit; /* still have at least MINPOWER2 free places */
|
||||
else luaD_error(L, errormsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void *debug_realloc (void *block, size_t size) {
|
||||
if (size == 0) {
|
||||
freeblock(block);
|
||||
return NULL;
|
||||
}
|
||||
else if (memdebug_total+size > memdebug_memlimit)
|
||||
return NULL; /* to test memory allocation errors */
|
||||
else {
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
|
||||
int i;
|
||||
if (realsize < size) return NULL; /* overflow! */
|
||||
if (newblock == NULL) return NULL;
|
||||
if (block) {
|
||||
size_t oldsize = *blocksize(block);
|
||||
if (oldsize > size) oldsize = size;
|
||||
memcpy(newblock+HEADER, block, oldsize);
|
||||
freeblock(block); /* erase (and check) old copy */
|
||||
}
|
||||
memdebug_total += size;
|
||||
if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total;
|
||||
memdebug_numblocks++;
|
||||
*(unsigned long *)newblock = size;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
*(newblock+HEADER+size+i) = (char)(MARK+i);
|
||||
return newblock+HEADER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================================== */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||
int inc, size_t size, const char *errormsg, size_t limit) {
|
||||
size_t newn = nelems+inc;
|
||||
if (nelems >= limit-inc) lua_error(L, errormsg);
|
||||
if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
|
||||
(nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
|
||||
return block; /* do not need to reallocate */
|
||||
else /* it crossed a power-of-2 boundary; grow to next power */
|
||||
return luaM_realloc(L, block, luaO_power2(newn)*size);
|
||||
newblock = luaM_realloc(L, block, (lu_mem)(*size)*(lu_mem)size_elems,
|
||||
(lu_mem)newsize*(lu_mem)size_elems);
|
||||
*size = newsize; /* update only when everything else is OK */
|
||||
return newblock;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** generic allocation routine.
|
||||
*/
|
||||
void *luaM_realloc (lua_State *L, void *block, lint32 size) {
|
||||
void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
|
||||
if (size == 0) {
|
||||
free(block); /* block may be NULL; that is OK for free */
|
||||
return NULL;
|
||||
l_free(block, oldsize); /* block may be NULL; that is OK for free */
|
||||
block = NULL;
|
||||
}
|
||||
else if (size >= MAX_SIZET)
|
||||
lua_error(L, "memory allocation error: block too big");
|
||||
block = realloc(block, size);
|
||||
if (block == NULL) {
|
||||
if (L)
|
||||
luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
|
||||
else return NULL; /* error before creating state! */
|
||||
luaD_error(L, l_s("memory allocation error: block too big"));
|
||||
else {
|
||||
block = l_realloc(block, oldsize, size);
|
||||
if (block == NULL) {
|
||||
if (L)
|
||||
luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
|
||||
else return NULL; /* error before creating state! */
|
||||
}
|
||||
}
|
||||
if (L && G(L)) {
|
||||
G(L)->nblocks -= oldsize;
|
||||
G(L)->nblocks += size;
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
39
lmem.h
39
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.21 2001/02/20 18:15:33 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,29 +13,28 @@
|
||||
#include "llimits.h"
|
||||
#include "lua.h"
|
||||
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, lint32 size);
|
||||
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||
int inc, size_t size, const char *errormsg,
|
||||
size_t limit);
|
||||
void *luaM_realloc (lua_State *L, void *oldblock, lu_mem oldsize, lu_mem size);
|
||||
|
||||
#define luaM_free(L, b) luaM_realloc(L, (b), 0)
|
||||
#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t))
|
||||
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem,
|
||||
int limit, const l_char *errormsg);
|
||||
|
||||
#define luaM_free(L, b, s) luaM_realloc(L, (b), (s), 0)
|
||||
#define luaM_freelem(L, b, t) luaM_realloc(L, (b), sizeof(t), 0)
|
||||
#define luaM_freearray(L, b, n, t) luaM_realloc(L, (b), \
|
||||
((lu_mem)(n)*(lu_mem)sizeof(t)), 0)
|
||||
|
||||
#define luaM_malloc(L, t) luaM_realloc(L, NULL, 0, (t))
|
||||
#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
|
||||
#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(lint32)sizeof(t)))
|
||||
#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, \
|
||||
(lu_mem)(n)*(lu_mem)sizeof(t)))
|
||||
|
||||
#define luaM_growvector(L, v,nelems,inc,t,e,l) \
|
||||
((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l))
|
||||
#define luaM_growvector(L,v,nelems,size,t,limit,e) \
|
||||
if (((nelems)+1) > (size)) \
|
||||
((v)=(t *)luaM_growaux(L,v,&(size),sizeof(t),limit,e))
|
||||
|
||||
#define luaM_reallocvector(L, v,n,t) \
|
||||
((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t)))
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
extern unsigned long memdebug_numblocks;
|
||||
extern unsigned long memdebug_total;
|
||||
extern unsigned long memdebug_maxmem;
|
||||
extern unsigned long memdebug_memlimit;
|
||||
#endif
|
||||
#define luaM_reallocvector(L, v,oldn,n,t) \
|
||||
((v)=(t *)luaM_realloc(L, v,(lu_mem)(oldn)*(lu_mem)sizeof(t), \
|
||||
(lu_mem)(n)*(lu_mem)sizeof(t)))
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
163
lobject.c
163
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.69 2001/03/07 12:27:06 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,151 +10,96 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*
|
||||
** you can use the fact that the 3rd letter or each name is always different
|
||||
** (e-m-r-b-n-l) to compare and switch these strings
|
||||
*/
|
||||
const char *const luaO_typenames[] = { /* ORDER LUA_T */
|
||||
"userdata", "number", "string", "table", "function", "function", "nil",
|
||||
"function", "function"
|
||||
};
|
||||
|
||||
|
||||
const TObject luaO_nilobject = {TAG_NIL, {NULL}};
|
||||
|
||||
|
||||
/*
|
||||
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
||||
*/
|
||||
lint32 luaO_power2 (lint32 n) {
|
||||
lint32 p = MINPOWER2;
|
||||
while (p<=n) p<<=1;
|
||||
return p;
|
||||
}
|
||||
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
|
||||
|
||||
|
||||
int luaO_equalObj (const TObject *t1, const TObject *t2) {
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
switch (ttype(t1)) {
|
||||
case TAG_NUMBER:
|
||||
case LUA_TNUMBER:
|
||||
return nvalue(t1) == nvalue(t2);
|
||||
case TAG_STRING: case TAG_USERDATA:
|
||||
case LUA_TNIL:
|
||||
return 1;
|
||||
default: /* all other types are equal if pointers are equal */
|
||||
return tsvalue(t1) == tsvalue(t2);
|
||||
case TAG_TABLE:
|
||||
return hvalue(t1) == hvalue(t2);
|
||||
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
||||
return clvalue(t1) == clvalue(t2);
|
||||
default:
|
||||
LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
|
||||
return 1; /* TAG_NIL */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char *luaO_openspace (lua_State *L, size_t n) {
|
||||
if (n > L->Mbuffsize) {
|
||||
luaM_reallocvector(L, L->Mbuffer, n, char);
|
||||
L->Mbuffsize = n;
|
||||
void *luaO_openspaceaux (lua_State *L, size_t n) {
|
||||
if (n > G(L)->Mbuffsize) {
|
||||
G(L)->Mbuffer = luaM_realloc(L, G(L)->Mbuffer, G(L)->Mbuffsize, n);
|
||||
G(L)->Mbuffsize = n;
|
||||
}
|
||||
return L->Mbuffer;
|
||||
return G(L)->Mbuffer;
|
||||
}
|
||||
|
||||
|
||||
static double expten (unsigned int e) {
|
||||
double exp = 10.0;
|
||||
double res = 1.0;
|
||||
for (; e; e>>=1) {
|
||||
if (e & 1) res *= exp;
|
||||
exp *= exp;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
|
||||
double a = 0.0;
|
||||
int point = 0; /* number of decimal digits */
|
||||
int sig;
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
sig = 0;
|
||||
switch (*s) {
|
||||
case '-': sig = 1; /* go through */
|
||||
case '+': s++;
|
||||
}
|
||||
if (! (isdigit((unsigned char)*s) ||
|
||||
(*s == '.' && isdigit((unsigned char)*(s+1)))))
|
||||
return 0; /* not (at least one digit before or after the point) */
|
||||
while (isdigit((unsigned char)*s))
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit((unsigned char)*s)) {
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
point++;
|
||||
}
|
||||
}
|
||||
if (sig) a = -a;
|
||||
if (*s == 'e' || *s == 'E') {
|
||||
int e = 0;
|
||||
s++;
|
||||
sig = 0;
|
||||
switch (*s) {
|
||||
case '-': sig = 1; /* go through */
|
||||
case '+': s++;
|
||||
}
|
||||
if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
|
||||
do {
|
||||
e = 10*e + (*(s++)-'0');
|
||||
} while (isdigit((unsigned char)*s));
|
||||
if (sig) e = -e;
|
||||
point -= e;
|
||||
}
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
if (*s != '\0') return 0; /* invalid trailing characters? */
|
||||
if (point != 0) {
|
||||
if (point > 0) a /= expten(point);
|
||||
else a *= expten(-point);
|
||||
}
|
||||
*result = a;
|
||||
int luaO_str2d (const l_char *s, lua_Number *result) {
|
||||
l_char *endptr;
|
||||
lua_Number res = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* no conversion */
|
||||
while (isspace(uchar(*endptr))) endptr++;
|
||||
if (*endptr != l_c('\0')) return 0; /* invalid trailing characters? */
|
||||
*result = res;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* this function needs to handle only '%d' and '%.XXXs' formats */
|
||||
void luaO_verror (lua_State *L, const char *fmt, ...) {
|
||||
char buff[500];
|
||||
/* 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 l_char *fmt, ...) {
|
||||
va_list argp;
|
||||
l_char buff[MAX_VERROR]; /* to hold formatted message */
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(L, buff);
|
||||
luaD_error(L, buff);
|
||||
}
|
||||
|
||||
|
||||
#define EXTRALEN sizeof("string \"...\"0")
|
||||
|
||||
void luaO_chunkid (char *out, const char *source, int len) {
|
||||
if (*source == '(') {
|
||||
strncpy(out, source+1, len-1); /* remove first char */
|
||||
out[len-1] = '\0'; /* make sure `out' has an end */
|
||||
out[strlen(out)-1] = '\0'; /* remove last char */
|
||||
void luaO_chunkid (l_char *out, const l_char *source, int bufflen) {
|
||||
if (*source == l_c('=')) {
|
||||
strncpy(out, source+1, bufflen); /* remove first char */
|
||||
out[bufflen-1] = l_c('\0'); /* ensures null termination */
|
||||
}
|
||||
else {
|
||||
len -= EXTRALEN;
|
||||
if (*source == '@')
|
||||
sprintf(out, "file `%.*s'", len, source+1);
|
||||
if (*source == l_c('@')) {
|
||||
int l;
|
||||
source++; /* skip the `@' */
|
||||
bufflen -= sizeof(l_s("file `...%s'"));
|
||||
l = strlen(source);
|
||||
if (l>bufflen) {
|
||||
source += (l-bufflen); /* get last part of file name */
|
||||
sprintf(out, l_s("file `...%.99s'"), source);
|
||||
}
|
||||
else
|
||||
sprintf(out, l_s("file `%.99s'"), source);
|
||||
}
|
||||
else {
|
||||
const char *b = strchr(source , '\n'); /* stop at first new line */
|
||||
int lim = (b && (b-source)<len) ? b-source : len;
|
||||
sprintf(out, "string \"%.*s\"", lim, source);
|
||||
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
|
||||
int len = strcspn(source, l_s("\n")); /* stop at first newline */
|
||||
bufflen -= sizeof(l_s("string \"%.*s...\""));
|
||||
if (len > bufflen) len = bufflen;
|
||||
if (source[len] != l_c('\0')) { /* must truncate? */
|
||||
strcpy(out, l_s("string \""));
|
||||
out += strlen(out);
|
||||
strncpy(out, source, len);
|
||||
strcpy(out+len, l_s("...\""));
|
||||
}
|
||||
else
|
||||
sprintf(out, l_s("string \"%.99s\""), source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
208
lobject.h
208
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.108 2001/06/28 14:48:44 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,120 +12,133 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define LUA_INTERNALERROR(s) assert(((void)s,0))
|
||||
#define LUA_ASSERT(c,s) assert(((void)s,(c)))
|
||||
#else
|
||||
#define LUA_INTERNALERROR(s) /* empty */
|
||||
#define LUA_ASSERT(c,s) /* empty */
|
||||
#ifndef lua_assert
|
||||
#define lua_assert(c) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
/* to avoid warnings, and make sure value is really unused */
|
||||
#define UNUSED(x) (x=0, (void)(x))
|
||||
#else
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Lua TYPES
|
||||
** WARNING: if you change the order of this enumeration,
|
||||
** grep "ORDER LUA_T"
|
||||
*/
|
||||
typedef enum {
|
||||
TAG_USERDATA = 0, /* default tag for userdata */
|
||||
TAG_NUMBER, /* fixed tag for numbers */
|
||||
TAG_STRING, /* fixed tag for strings */
|
||||
TAG_TABLE, /* default tag for tables */
|
||||
TAG_LCLOSURE, /* fixed tag for Lua closures */
|
||||
TAG_CCLOSURE, /* fixed tag for C closures */
|
||||
TAG_NIL, /* last "pre-defined" tag */
|
||||
|
||||
TAG_LMARK, /* mark for Lua closures */
|
||||
TAG_CMARK /* mark for C closures */
|
||||
|
||||
} lua_Type;
|
||||
|
||||
/* tags for values visible from Lua == first user-created tag */
|
||||
#define NUM_TAGS 7
|
||||
|
||||
|
||||
/*
|
||||
** check whether `t' is a mark
|
||||
*/
|
||||
#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
|
||||
#define NUM_TAGS 6
|
||||
|
||||
|
||||
typedef union {
|
||||
struct TString *ts; /* TAG_STRING, TAG_USERDATA */
|
||||
struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_CMARK */
|
||||
struct Hash *a; /* TAG_TABLE */
|
||||
struct CallInfo *i; /* TAG_LMARK */
|
||||
Number n; /* TAG_NUMBER */
|
||||
union TString *ts;
|
||||
union Udata *u;
|
||||
struct Closure *cl;
|
||||
struct Hash *h;
|
||||
lua_Number n; /* LUA_TNUMBER */
|
||||
} Value;
|
||||
|
||||
|
||||
/* Macros to access values */
|
||||
#define ttype(o) ((o)->ttype)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define hvalue(o) ((o)->value.a)
|
||||
#define infovalue(o) ((o)->value.i)
|
||||
#define svalue(o) (tsvalue(o)->str)
|
||||
|
||||
|
||||
typedef struct lua_TObject {
|
||||
lua_Type ttype;
|
||||
int tt;
|
||||
Value value;
|
||||
} TObject;
|
||||
|
||||
|
||||
/* Macros to access values */
|
||||
#define ttype(o) ((o)->tt)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define uvalue(o) ((o)->value.u)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define hvalue(o) ((o)->value.h)
|
||||
|
||||
|
||||
/* Macros to set values */
|
||||
#define setnvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TNUMBER; _o->value.n=(x); }
|
||||
|
||||
#define chgnvalue(obj,x) ((obj)->value.n=(x))
|
||||
|
||||
#define setsvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TSTRING; _o->value.ts=(x); }
|
||||
|
||||
#define setuvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TUSERDATA; _o->value.u=(x); }
|
||||
|
||||
#define setclvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TFUNCTION; _o->value.cl=(x); }
|
||||
|
||||
#define sethvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TTABLE; _o->value.h=(x); }
|
||||
|
||||
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
|
||||
|
||||
#define setobj(obj1,obj2) \
|
||||
{ TObject *o1=(obj1); const TObject *o2=(obj2); \
|
||||
o1->tt=o2->tt; o1->value = o2->value; }
|
||||
|
||||
#define setttype(obj, tt) (ttype(obj) = (tt))
|
||||
|
||||
|
||||
|
||||
typedef TObject *StkId; /* index to stack elements */
|
||||
|
||||
|
||||
/*
|
||||
** String headers for string table
|
||||
*/
|
||||
typedef struct TString {
|
||||
union {
|
||||
struct { /* for strings */
|
||||
unsigned long hash;
|
||||
size_t len;
|
||||
int constindex; /* hint to reuse constants */
|
||||
} s;
|
||||
struct { /* for userdata */
|
||||
int tag;
|
||||
void *value;
|
||||
} d;
|
||||
} u;
|
||||
struct TString *nexthash; /* chain for hash table */
|
||||
unsigned char marked;
|
||||
char str[1]; /* variable length string!! must be the last field! */
|
||||
typedef union TString {
|
||||
union L_Umaxalign dummy; /* ensures maximum alignment for strings */
|
||||
struct {
|
||||
lu_hash hash;
|
||||
size_t len;
|
||||
unsigned short constindex; /* hint to reuse constants */
|
||||
short marked;
|
||||
union TString *nexthash; /* chain for hash table */
|
||||
} tsv;
|
||||
} TString;
|
||||
|
||||
|
||||
#define getstr(ts) ((l_char *)((ts) + 1))
|
||||
#define svalue(o) getstr(tsvalue(o))
|
||||
|
||||
|
||||
|
||||
typedef union Udata {
|
||||
union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
||||
struct {
|
||||
int tag; /* negative means `marked' (only during GC) */
|
||||
void *value;
|
||||
size_t len;
|
||||
union Udata *next; /* chain for list of all udata */
|
||||
} uv;
|
||||
} Udata;
|
||||
|
||||
|
||||
#define switchudatamark(u) ((u)->uv.tag = (-((u)->uv.tag+1)))
|
||||
#define ismarkedudata(u) ((u)->uv.tag < 0)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct Proto {
|
||||
Number *knum; /* Number numbers used by the function */
|
||||
int nknum; /* size of `knum' */
|
||||
struct TString **kstr; /* strings used by the function */
|
||||
int nkstr; /* size of `kstr' */
|
||||
struct Proto **kproto; /* functions defined inside the function */
|
||||
int nkproto; /* size of `kproto' */
|
||||
Instruction *code; /* ends with opcode ENDCODE */
|
||||
int numparams;
|
||||
int is_vararg;
|
||||
int maxstacksize;
|
||||
TObject *k; /* constants used by the function */
|
||||
int sizek; /* size of `k' */
|
||||
struct Proto **p; /* functions defined inside the function */
|
||||
int sizep; /* size of `p' */
|
||||
Instruction *code;
|
||||
int sizecode;
|
||||
short nupvalues;
|
||||
short numparams;
|
||||
short is_vararg;
|
||||
short maxstacksize;
|
||||
short marked;
|
||||
struct Proto *next;
|
||||
int marked;
|
||||
/* debug information */
|
||||
int *lineinfo; /* map from opcodes to source lines */
|
||||
int nlocvars;
|
||||
int sizelineinfo; /* size of `lineinfo' */
|
||||
struct LocVar *locvars; /* information about local variables */
|
||||
int sizelocvars;
|
||||
int lineDefined;
|
||||
TString *source;
|
||||
} Proto;
|
||||
@@ -142,23 +155,28 @@ typedef struct LocVar {
|
||||
** Closures
|
||||
*/
|
||||
typedef struct Closure {
|
||||
int isC; /* 0 for Lua functions, 1 for C functions */
|
||||
int nupvalues;
|
||||
union {
|
||||
lua_CFunction c; /* C functions */
|
||||
struct Proto *l; /* Lua functions */
|
||||
} f;
|
||||
struct Closure *next;
|
||||
struct Closure *mark; /* marked closures (point to itself when not marked) */
|
||||
int nupvalues;
|
||||
TObject upvalue[1];
|
||||
} Closure;
|
||||
|
||||
|
||||
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
|
||||
|
||||
|
||||
typedef struct Node {
|
||||
struct Node *next; /* for chaining */
|
||||
TObject key;
|
||||
TObject val;
|
||||
struct Node *next; /* for chaining */
|
||||
} Node;
|
||||
|
||||
|
||||
typedef struct Hash {
|
||||
Node *node;
|
||||
int htag;
|
||||
@@ -166,6 +184,7 @@ typedef struct Hash {
|
||||
Node *firstfree; /* this position is free; all positions after it are full */
|
||||
struct Hash *next;
|
||||
struct Hash *mark; /* marked tables (point to itself when not marked) */
|
||||
int weakmode;
|
||||
} Hash;
|
||||
|
||||
|
||||
@@ -175,31 +194,38 @@ typedef struct Hash {
|
||||
#define ismarked(x) ((x)->mark != (x))
|
||||
|
||||
|
||||
/*
|
||||
** `module' operation for hashing (size is always a power of 2)
|
||||
*/
|
||||
#define lmod(s,size) ((int)((s) & ((size)-1)))
|
||||
|
||||
|
||||
/*
|
||||
** informations about a call (for debugging)
|
||||
*/
|
||||
typedef struct CallInfo {
|
||||
struct Closure *func; /* function being called */
|
||||
struct CallInfo *prev; /* linked list */
|
||||
StkId base; /* base for called function */
|
||||
const Instruction **pc; /* current pc of called function */
|
||||
int lastpc; /* last pc traced */
|
||||
int line; /* current line */
|
||||
int refi; /* current index in `lineinfo' */
|
||||
} CallInfo;
|
||||
|
||||
#define ci_func(ci) (clvalue((ci)->base - 1))
|
||||
|
||||
|
||||
extern const char *const luaO_typenames[];
|
||||
extern const TObject luaO_nilobject;
|
||||
|
||||
#define luaO_typename(o) luaO_typenames[ttype(o)]
|
||||
|
||||
lint32 luaO_power2 (lint32 n);
|
||||
char *luaO_openspace (lua_State *L, size_t n);
|
||||
#define luaO_openspace(L,n,t) ((t *)luaO_openspaceaux(L,(n)*sizeof(t)))
|
||||
void *luaO_openspaceaux (lua_State *L, size_t n);
|
||||
|
||||
int luaO_equalObj (const TObject *t1, const TObject *t2);
|
||||
int luaO_str2d (const char *s, Number *result);
|
||||
int luaO_str2d (const l_char *s, lua_Number *result);
|
||||
|
||||
void luaO_verror (lua_State *L, const char *fmt, ...);
|
||||
void luaO_chunkid (char *out, const char *source, int len);
|
||||
void luaO_verror (lua_State *L, const l_char *fmt, ...);
|
||||
void luaO_chunkid (l_char *out, const l_char *source, int len);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
107
lopcodes.c
Normal file
107
lopcodes.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
** $Id:$
|
||||
** extracted automatically from lopcodes.h by mkprint.lua
|
||||
** DO NOT EDIT
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
#ifdef LUA_OPNAMES
|
||||
|
||||
const l_char *const luaP_opnames[] = {
|
||||
l_s("MOVE"),
|
||||
l_s("LOADK"),
|
||||
l_s("LOADINT"),
|
||||
l_s("LOADNIL"),
|
||||
l_s("LOADUPVAL"),
|
||||
l_s("GETGLOBAL"),
|
||||
l_s("GETTABLE"),
|
||||
l_s("SETGLOBAL"),
|
||||
l_s("SETTABLE"),
|
||||
l_s("NEWTABLE"),
|
||||
l_s("SELF"),
|
||||
l_s("ADD"),
|
||||
l_s("SUB"),
|
||||
l_s("MUL"),
|
||||
l_s("DIV"),
|
||||
l_s("POW"),
|
||||
l_s("UNM"),
|
||||
l_s("NOT"),
|
||||
l_s("CONCAT"),
|
||||
l_s("JMP"),
|
||||
l_s("CJMP"),
|
||||
l_s("TESTEQ"),
|
||||
l_s("TESTNE"),
|
||||
l_s("TESTLT"),
|
||||
l_s("TESTLE"),
|
||||
l_s("TESTGT"),
|
||||
l_s("TESTGE"),
|
||||
l_s("TESTT"),
|
||||
l_s("TESTF"),
|
||||
l_s("NILJMP"),
|
||||
l_s("CALL"),
|
||||
l_s("RETURN"),
|
||||
l_s("FORPREP"),
|
||||
l_s("FORLOOP"),
|
||||
l_s("TFORPREP"),
|
||||
l_s("TFORLOOP"),
|
||||
l_s("SETLIST"),
|
||||
l_s("SETLISTO"),
|
||||
l_s("CLOSURE")
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#define opmode(t,x,b,c,sa,k,m) (((t)<<OpModeT) | \
|
||||
((b)<<OpModeBreg) | ((c)<<OpModeCreg) | \
|
||||
((sa)<<OpModesetA) | ((k)<<OpModeK) | (m))
|
||||
|
||||
const lu_byte luaP_opmodes[] = {
|
||||
/* T _ B C sA K mode opcode */
|
||||
opmode(0,0,1,0, 1,0,iABC) /* OP_MOVE */
|
||||
,opmode(0,0,0,0, 1,1,iABc) /* OP_LOADK */
|
||||
,opmode(0,0,0,0, 1,0,iAsBc) /* OP_LOADINT */
|
||||
,opmode(0,0,1,0, 1,0,iABC) /* OP_LOADNIL */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_LOADUPVAL */
|
||||
,opmode(0,0,0,0, 1,1,iABc) /* OP_GETGLOBAL */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_GETTABLE */
|
||||
,opmode(0,0,0,0, 0,1,iABc) /* OP_SETGLOBAL */
|
||||
,opmode(0,0,1,1, 0,0,iABC) /* OP_SETTABLE */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_NEWTABLE */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_SELF */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_ADD */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_SUB */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_MUL */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_DIV */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_POW */
|
||||
,opmode(0,0,1,0, 1,0,iABC) /* OP_UNM */
|
||||
,opmode(0,0,1,0, 1,0,iABC) /* OP_NOT */
|
||||
,opmode(0,0,1,1, 1,0,iABC) /* OP_CONCAT */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_JMP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_CJMP */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTEQ */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTNE */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTLT */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTLE */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTGT */
|
||||
,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTGE */
|
||||
,opmode(1,0,1,0, 1,0,iABC) /* OP_TESTT */
|
||||
,opmode(1,0,1,0, 1,0,iABC) /* OP_TESTF */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_NILJMP */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_CALL */
|
||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_RETURN */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_FORPREP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_FORLOOP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_TFORPREP */
|
||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_TFORLOOP */
|
||||
,opmode(0,0,0,0, 0,0,iABc) /* OP_SETLIST */
|
||||
,opmode(0,0,0,0, 0,0,iABc) /* OP_SETLISTO */
|
||||
,opmode(0,0,0,0, 1,0,iABc) /* OP_CLOSURE */
|
||||
};
|
||||
|
||||
248
lopcodes.h
248
lopcodes.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.66 2000/08/15 18:28:48 roberto Exp roberto $
|
||||
** $Id: lopcodes.h,v 1.77 2001/07/03 17:01:34 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,29 +12,58 @@
|
||||
|
||||
/*===========================================================================
|
||||
We assume that instructions are unsigned numbers.
|
||||
All instructions have an opcode in the first 6 bits. Moreover,
|
||||
an instruction can have 0, 1, or 2 arguments. Instructions can
|
||||
have the following types:
|
||||
type 0: no arguments
|
||||
type 1: 1 unsigned argument in the higher bits (called `U')
|
||||
type 2: 1 signed argument in the higher bits (`S')
|
||||
type 3: 1st unsigned argument in the higher bits (`A')
|
||||
2nd unsigned argument in the middle bits (`B')
|
||||
All instructions have an opcode in the first 6 bits.
|
||||
Instructions can have the following fields:
|
||||
`A' : 8 bits (25-32)
|
||||
`B' : 8 bits (17-24)
|
||||
`C' : 10 bits (7-16)
|
||||
`Bc' : 18 bits (`B' and `C' together)
|
||||
`sBc' : signed Bc
|
||||
|
||||
A signed argument is represented in excess K; that is, the number
|
||||
value is the unsigned value minus K. K is exactly the maximum value
|
||||
for that argument (so that -max is represented by 0, and +max is
|
||||
represented by 2*max), which is half the maximum for the corresponding
|
||||
unsigned argument.
|
||||
|
||||
The size of each argument is defined in `llimits.h'. The usual is an
|
||||
instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
|
||||
with 9 bits, and A arguments with 17 bits (32-6-9). For small
|
||||
installations, the instruction size can be 16, so U has 10 bits,
|
||||
and A and B have 5 bits each.
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
enum OpMode {iABC, iABc, iAsBc}; /* basic instruction format */
|
||||
|
||||
|
||||
/*
|
||||
** size and position of opcode arguments.
|
||||
*/
|
||||
#define SIZE_C 10
|
||||
#define SIZE_B 8
|
||||
#define SIZE_Bc (SIZE_C + SIZE_B)
|
||||
#define SIZE_A 8
|
||||
|
||||
#define SIZE_OP 6
|
||||
|
||||
#define POS_C SIZE_OP
|
||||
#define POS_B (POS_C + SIZE_C)
|
||||
#define POS_Bc POS_C
|
||||
#define POS_A (POS_B + SIZE_B)
|
||||
|
||||
|
||||
/*
|
||||
** limits for opcode arguments.
|
||||
** we use (signed) int to manipulate most arguments,
|
||||
** so they must fit in BITS_INT-1 bits (-1 for sign)
|
||||
*/
|
||||
#if SIZE_Bc < BITS_INT-1
|
||||
#define MAXARG_Bc ((1<<SIZE_Bc)-1)
|
||||
#define MAXARG_sBc (MAXARG_Bc>>1) /* `sBc' is signed */
|
||||
#else
|
||||
#define MAXARG_Bc MAX_INT
|
||||
#define MAXARG_sBc MAX_INT
|
||||
#endif
|
||||
|
||||
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||
|
||||
|
||||
/* creates a mask with `n' 1 bits at position `p' */
|
||||
@@ -47,122 +76,153 @@
|
||||
** the following macros help to manipulate instructions
|
||||
*/
|
||||
|
||||
#define CREATE_0(o) ((Instruction)(o))
|
||||
#define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
|
||||
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
|
||||
#define SET_OPCODE(i,o) (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))
|
||||
|
||||
#define CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
|
||||
#define GETARG_U(i) ((int)((i)>>POS_U))
|
||||
#define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
|
||||
((Instruction)(u)<<POS_U)))
|
||||
|
||||
#define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S)
|
||||
#define GETARG_S(i) (GETARG_U(i)-MAXARG_S)
|
||||
#define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S)
|
||||
|
||||
|
||||
#define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
|
||||
| ((Instruction)(b)<<POS_B))
|
||||
#define GETARG_A(i) ((int)((i)>>POS_A))
|
||||
#define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
||||
((Instruction)(a)<<POS_A)))
|
||||
#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
||||
((Instruction)(u)<<POS_A)))
|
||||
|
||||
#define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
|
||||
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
|
||||
((Instruction)(b)<<POS_B)))
|
||||
|
||||
#define GETARG_C(i) ((int)(((i)>>POS_C) & MASK1(SIZE_C,0)))
|
||||
#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
|
||||
((Instruction)(b)<<POS_C)))
|
||||
|
||||
#define GETARG_Bc(i) ((int)(((i)>>POS_Bc) & MASK1(SIZE_Bc,0)))
|
||||
#define SETARG_Bc(i,b) ((i) = (((i)&MASK0(SIZE_Bc,POS_Bc)) | \
|
||||
((Instruction)(b)<<POS_Bc)))
|
||||
|
||||
#define GETARG_sBc(i) (GETARG_Bc(i)-MAXARG_sBc)
|
||||
#define SETARG_sBc(i,b) SETARG_Bc((i),(unsigned int)((b)+MAXARG_sBc))
|
||||
|
||||
|
||||
#define CREATE_ABC(o,a,b,c) ((Instruction)(o) \
|
||||
| ((Instruction)(a)<<POS_A) \
|
||||
| ((Instruction)(b)<<POS_B) \
|
||||
| ((Instruction)(c)<<POS_C))
|
||||
|
||||
#define CREATE_ABc(o,a,bc) ((Instruction)(o) \
|
||||
| ((Instruction)(a)<<POS_A) \
|
||||
| ((Instruction)(bc)<<POS_Bc))
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** K = U argument used as index to `kstr'
|
||||
** J = S argument used as jump offset (relative to pc of next instruction)
|
||||
** L = unsigned argument used as index of local variable
|
||||
** N = U argument used as index to `knum'
|
||||
** an invalid register that fits in 8 bits
|
||||
*/
|
||||
#define NO_REG MAXARG_A
|
||||
|
||||
|
||||
/*
|
||||
** R(x) - register
|
||||
** Kst(x) - constant (in constant table)
|
||||
** R/K(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
/*----------------------------------------------------------------------
|
||||
name args stack before stack after side effects
|
||||
name args description
|
||||
------------------------------------------------------------------------*/
|
||||
OP_END,/* - - (return) no results */
|
||||
OP_RETURN,/* U v_n-v_x(at u) (return) returns v_x-v_n */
|
||||
OP_MOVE,/* A B R(A) := R(B) */
|
||||
OP_LOADK,/* A Bc R(A) := Kst(Bc) */
|
||||
OP_LOADINT,/* A sBc R(A) := (Number)sBc */
|
||||
OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
|
||||
OP_LOADUPVAL,/* A Bc R(A) := UpValue[Bc] */
|
||||
|
||||
OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
|
||||
OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
|
||||
OP_GETGLOBAL,/* A Bc R(A) := Gbl[Kst(Bc)] */
|
||||
OP_GETTABLE,/* A B C R(A) := R(B)[R/K(C)] */
|
||||
|
||||
OP_PUSHNIL,/* U - nil_1-nil_u */
|
||||
OP_POP,/* U a_u-a_1 - */
|
||||
OP_SETGLOBAL,/* A Bc Gbl[Kst(Bc)] := R(A) */
|
||||
OP_SETTABLE,/* A B C R(B)[R/K(C)] := R(A) */
|
||||
|
||||
OP_PUSHINT,/* S - (Number)s */
|
||||
OP_PUSHSTRING,/* K - KSTR[k] */
|
||||
OP_PUSHNUM,/* N - KNUM[n] */
|
||||
OP_PUSHNEGNUM,/* N - -KNUM[n] */
|
||||
OP_NEWTABLE,/* A Bc R(A) := {} (size = Bc) */
|
||||
|
||||
OP_PUSHUPVALUE,/* U - Closure[u] */
|
||||
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[R/K(C)] */
|
||||
|
||||
OP_GETLOCAL,/* L - LOC[l] */
|
||||
OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
|
||||
OP_ADD,/* A B C R(A) := R(B) + R/K(C) */
|
||||
OP_SUB,/* A B C R(A) := R(B) - R/K(C) */
|
||||
OP_MUL,/* A B C R(A) := R(B) * R/K(C) */
|
||||
OP_DIV,/* A B C R(A) := R(B) / R/K(C) */
|
||||
OP_POW,/* A B C R(A) := R(B) ^ R/K(C) */
|
||||
OP_UNM,/* A B R(A) := -R(B) */
|
||||
OP_NOT,/* A B R(A) := not R(B) */
|
||||
|
||||
OP_GETTABLE,/* - i t t[i] */
|
||||
OP_GETDOTTED,/* K t t[KSTR[k]] */
|
||||
OP_GETINDEXED,/* L t t[LOC[l]] */
|
||||
OP_PUSHSELF,/* K t t t[KSTR[k]] */
|
||||
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||
|
||||
OP_CREATETABLE,/* U - newarray(size = u) */
|
||||
OP_JMP,/* sBc PC += sBc */
|
||||
OP_CJMP,/* sBc if test then PC += sBc (see (1)) */
|
||||
|
||||
OP_SETLOCAL,/* L x - LOC[l]=x */
|
||||
OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
|
||||
OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
|
||||
OP_TESTEQ,/* A C test := (R(A) == R/K(C)) */
|
||||
OP_TESTNE,/* A C test := (R(A) ~= R/K(C)) */
|
||||
OP_TESTLT,/* A C test := (R(A) < R/K(C)) */
|
||||
OP_TESTLE,/* A C test := (R(A) <= R/K(C)) */
|
||||
OP_TESTGT,/* A C test := (R(A) > R/K(C)) */
|
||||
OP_TESTGE,/* A C test := (R(A) >= R/K(C)) */
|
||||
|
||||
OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */
|
||||
OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */
|
||||
OP_TESTT,/* A B test := R(B); if (test) R(A) := R(B) */
|
||||
OP_TESTF,/* A B test := not R(B); if (test) R(A) := nil */
|
||||
|
||||
OP_ADD,/* - y x x+y */
|
||||
OP_ADDI,/* S x x+s */
|
||||
OP_SUB,/* - y x x-y */
|
||||
OP_MULT,/* - y x x*y */
|
||||
OP_DIV,/* - y x x/y */
|
||||
OP_POW,/* - y x x^y */
|
||||
OP_CONCAT,/* U v_u-v_1 v1..-..v_u */
|
||||
OP_MINUS,/* - x -x */
|
||||
OP_NOT,/* - x (x==nil)? 1 : nil */
|
||||
OP_NILJMP,/* A Bc R(A) := nil; PC++; */
|
||||
|
||||
OP_JMPNE,/* J y x - (x~=y)? PC+=s */
|
||||
OP_JMPEQ,/* J y x - (x==y)? PC+=s */
|
||||
OP_JMPLT,/* J y x - (x<y)? PC+=s */
|
||||
OP_JMPLE,/* J y x - (x<y)? PC+=s */
|
||||
OP_JMPGT,/* J y x - (x>y)? PC+=s */
|
||||
OP_JMPGE,/* J y x - (x>=y)? PC+=s */
|
||||
OP_CALL,/* A B C R(A), ... ,R(A+C-1) := R(A)(R(A+1), ... ,R(A+B))*/
|
||||
OP_RETURN,/* A B return R(A), ... ,R(A+B-1) (see (3)) */
|
||||
|
||||
OP_JMPT,/* J x - (x~=nil)? PC+=s */
|
||||
OP_JMPF,/* J x - (x==nil)? PC+=s */
|
||||
OP_JMPONT,/* J x (x~=nil)? x : - (x~=nil)? PC+=s */
|
||||
OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */
|
||||
OP_JMP,/* J - - PC+=s */
|
||||
OP_FORPREP,/* A sBc */
|
||||
OP_FORLOOP,/* A sBc */
|
||||
|
||||
OP_PUSHNILJMP,/* - - nil PC++; */
|
||||
OP_TFORPREP,/* A sBc */
|
||||
OP_TFORLOOP,/* A sBc */
|
||||
|
||||
OP_FORPREP,/* J */
|
||||
OP_FORLOOP,/* J */
|
||||
|
||||
OP_LFORPREP,/* J */
|
||||
OP_LFORLOOP,/* J */
|
||||
|
||||
OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
|
||||
OP_SETLIST,/* A Bc R(A)[Bc-Bc%FPF+i] := R(A+i), 1 <= i <= Bc%FPF+1 */
|
||||
OP_SETLISTO,/* A Bc */
|
||||
|
||||
OP_CLOSURE /* A Bc R(A) := closure(KPROTO[Bc], R(A), ... ,R(A+n)) */
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define NUM_OPCODES ((int)OP_CLOSURE+1)
|
||||
|
||||
|
||||
#define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
|
||||
|
||||
/*===========================================================================
|
||||
Notes:
|
||||
(1) In the current implementation there is no `test' variable;
|
||||
instructions OP_TEST* and OP_CJMP must always occur together.
|
||||
|
||||
(2) In OP_CALL, if (B == NO_REG) then B = top. C is the number of returns,
|
||||
and can be NO_REG. OP_CALL can set `top' to last_result+1, so
|
||||
next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
|
||||
|
||||
(3) In OP_RETURN, if (B == NO_REG) then return up to `top'
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
|
||||
/* special code to fit a LUA_MULTRET inside an argB */
|
||||
#define MULT_RET 255 /* (<=MAXARG_B) */
|
||||
#if MULT_RET>MAXARG_B
|
||||
#undef MULT_RET
|
||||
#define MULT_RET MAXARG_B
|
||||
#endif
|
||||
/*
|
||||
** masks for instruction properties
|
||||
*/
|
||||
enum OpModeMask {
|
||||
OpModeBreg = 2, /* B is a register */
|
||||
OpModeCreg, /* C is a register/constant */
|
||||
OpModesetA, /* instruction set register A */
|
||||
OpModeK, /* Bc is a constant */
|
||||
OpModeT /* operator is a test */
|
||||
};
|
||||
|
||||
extern const lu_byte luaP_opmodes[];
|
||||
|
||||
#define getOpMode(m) ((enum OpMode)(luaP_opmodes[m] & 3))
|
||||
#define testOpMode(m, b) (luaP_opmodes[m] & (1 << (b)))
|
||||
|
||||
|
||||
/*
|
||||
** opcode names (only included when compiled with LUA_OPNAMES)
|
||||
*/
|
||||
extern const l_char *const luaP_opnames[];
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
37
lparser.h
37
lparser.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.23 2000/08/22 17:44:17 roberto Exp roberto $
|
||||
** $Id: lparser.h,v 1.31 2001/06/05 18:17:01 roberto Exp roberto $
|
||||
** LL(1) Parser and code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -16,39 +16,48 @@
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
VGLOBAL,
|
||||
VLOCAL,
|
||||
VINDEXED,
|
||||
VEXP
|
||||
VVOID, /* no value */
|
||||
VNIL,
|
||||
VNUMBER, /* n = value */
|
||||
VK, /* info = index of constant in `k' */
|
||||
VGLOBAL, /* info = index of global name in `k' */
|
||||
VLOCAL, /* info = local register */
|
||||
VINDEXED, /* info = table register; aux = index register (or `k') */
|
||||
VRELOCABLE, /* info = instruction pc */
|
||||
VNONRELOC, /* info = result register */
|
||||
VJMP, /* info = result register */
|
||||
VCALL /* info = result register */
|
||||
} expkind;
|
||||
|
||||
typedef struct expdesc {
|
||||
expkind k;
|
||||
union {
|
||||
int index; /* VGLOBAL: `kstr' index of global name; VLOCAL: stack index */
|
||||
struct {
|
||||
int t; /* patch list of `exit when true' */
|
||||
int f; /* patch list of `exit when false' */
|
||||
} l;
|
||||
int info, aux;
|
||||
} i;
|
||||
lua_Number n;
|
||||
} u;
|
||||
int t; /* patch list of `exit when true' */
|
||||
int f; /* patch list of `exit when false' */
|
||||
} expdesc;
|
||||
|
||||
|
||||
|
||||
/* state needed to generate code for a given function */
|
||||
typedef struct FuncState {
|
||||
Proto *f; /* current function header */
|
||||
struct FuncState *prev; /* enclosing function */
|
||||
struct LexState *ls; /* lexical state */
|
||||
struct lua_State *L; /* copy of the Lua state */
|
||||
int pc; /* next position to code */
|
||||
int pc; /* next position to code (equivalent to `ncode') */
|
||||
int lasttarget; /* `pc' of last `jump target' */
|
||||
int jlt; /* list of jumps to `lasttarget' */
|
||||
int stacklevel; /* number of values on activation register */
|
||||
int freereg; /* first free register */
|
||||
int nk; /* number of elements in `k' */
|
||||
int np; /* number of elements in `p' */
|
||||
int nlineinfo; /* number of elements in `lineinfo' */
|
||||
int nlocvars; /* number of elements in `locvars' */
|
||||
int nactloc; /* number of active local variables */
|
||||
int nupvalues; /* number of upvalues */
|
||||
int lastline; /* line where last `lineinfo' was generated */
|
||||
int nlineinfo; /* index of next `lineinfo' to be generated */
|
||||
struct Breaklabel *bl; /* chain of breakable blocks */
|
||||
expdesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||
int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
|
||||
|
||||
179
lstate.c
179
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.65 2001/06/21 16:41:34 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
@@ -19,85 +20,123 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
extern lua_State *lua_state;
|
||||
void luaB_opentests (lua_State *L);
|
||||
#endif
|
||||
struct Sopen {
|
||||
int stacksize;
|
||||
lua_State *L;
|
||||
};
|
||||
|
||||
|
||||
static void close_state (lua_State *L, lua_State *OL);
|
||||
|
||||
|
||||
/*
|
||||
** built-in implementation for ERRORMESSAGE. In a "correct" environment
|
||||
** ERRORMESSAGE should have an external definition, and so this function
|
||||
** would not be used.
|
||||
** open parts that may cause memory-allocation errors
|
||||
*/
|
||||
static int errormessage (lua_State *L) {
|
||||
const char *s = lua_tostring(L, 1);
|
||||
if (s == NULL) s = "(no message)";
|
||||
fprintf(stderr, "error: %s\n", s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
lua_State *lua_open (int stacksize) {
|
||||
struct lua_longjmp myErrorJmp;
|
||||
lua_State *L = luaM_new(NULL, lua_State);
|
||||
if (L == NULL) return NULL; /* memory allocation error */
|
||||
L->stack = NULL;
|
||||
L->strt.size = L->udt.size = 0;
|
||||
L->strt.nuse = L->udt.nuse = 0;
|
||||
L->strt.hash = NULL;
|
||||
L->udt.hash = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->Mbuffsize = 0;
|
||||
L->rootproto = NULL;
|
||||
L->rootcl = NULL;
|
||||
L->roottable = NULL;
|
||||
L->IMtable = NULL;
|
||||
L->last_tag = -1;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->refFree = NONEXT;
|
||||
L->nblocks = 0;
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->allowhooks = 1;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp.b) == 0) { /* to catch memory allocation errors */
|
||||
L->gt = luaH_new(L, 10);
|
||||
luaD_init(L, (stacksize == 0) ? DEFAULT_STACK_SIZE :
|
||||
stacksize+LUA_MINSTACK);
|
||||
luaS_init(L);
|
||||
static void f_luaopen (lua_State *L, void *ud) {
|
||||
struct Sopen *so = (struct Sopen *)ud;
|
||||
if (so->stacksize == 0)
|
||||
so->stacksize = DEFAULT_STACK_SIZE;
|
||||
else
|
||||
so->stacksize += LUA_MINSTACK;
|
||||
if (so->L != NULL) { /* shared global state? */
|
||||
L->G = G(so->L);
|
||||
L->gt = so->L->gt; /* share table of globals */
|
||||
so->L->next->previous = L; /* insert L into linked list */
|
||||
L->next = so->L->next;
|
||||
so->L->next = L;
|
||||
L->previous = so->L;
|
||||
luaD_init(L, so->stacksize); /* init stack */
|
||||
}
|
||||
else { /* create a new global state */
|
||||
L->G = luaM_new(L, global_State);
|
||||
G(L)->strt.size = 0;
|
||||
G(L)->strt.nuse = 0;
|
||||
G(L)->strt.hash = NULL;
|
||||
G(L)->Mbuffer = NULL;
|
||||
G(L)->Mbuffsize = 0;
|
||||
G(L)->rootproto = NULL;
|
||||
G(L)->rootcl = NULL;
|
||||
G(L)->roottable = NULL;
|
||||
G(L)->rootudata = NULL;
|
||||
G(L)->TMtable = NULL;
|
||||
G(L)->sizeTM = 0;
|
||||
G(L)->ntag = 0;
|
||||
G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
|
||||
luaD_init(L, so->stacksize); /* init stack */
|
||||
L->gt = luaH_new(L, 10); /* table of globals */
|
||||
G(L)->type2tag = luaH_new(L, 10);
|
||||
G(L)->registry = luaH_new(L, 0);
|
||||
G(L)->weakregistry = luaH_new(L, 0);
|
||||
/* make weakregistry weak */
|
||||
G(L)->weakregistry->weakmode = LUA_WEAK_KEY | LUA_WEAK_VALUE;
|
||||
luaS_resize(L, MINPOWER2);
|
||||
luaX_init(L);
|
||||
luaT_init(L);
|
||||
lua_register(L, LUA_ERRORMESSAGE, errormessage);
|
||||
#ifdef DEBUG
|
||||
luaB_opentests(L);
|
||||
#endif
|
||||
L->GCthreshold = L->nblocks*4;
|
||||
G(L)->GCthreshold = 4*G(L)->nblocks;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
|
||||
struct Sopen so;
|
||||
lua_State *L;
|
||||
if (OL) lua_lock(OL);
|
||||
L = luaM_new(OL, lua_State);
|
||||
if (L) { /* allocation OK? */
|
||||
L->G = NULL;
|
||||
L->stack = NULL;
|
||||
L->stacksize = 0;
|
||||
L->ci = &L->basefunc;
|
||||
L->basefunc.prev = NULL;
|
||||
L->errorJmp = NULL;
|
||||
return L;
|
||||
}
|
||||
else { /* memory allocation error: free partial state */
|
||||
lua_close(L);
|
||||
return NULL;
|
||||
L->callhook = NULL;
|
||||
L->linehook = NULL;
|
||||
L->allowhooks = 1;
|
||||
L->next = L->previous = L;
|
||||
so.stacksize = stacksize;
|
||||
so.L = OL;
|
||||
if (luaD_runprotected(L, f_luaopen, &so) != 0) {
|
||||
/* memory allocation error: free partial state */
|
||||
close_state(L, OL);
|
||||
L = NULL;
|
||||
}
|
||||
}
|
||||
if (OL) lua_unlock(OL);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
void lua_close (lua_State *L) {
|
||||
luaC_collect(L, 1); /* collect all elements */
|
||||
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
|
||||
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
|
||||
LUA_ASSERT(L->roottable == NULL, "list should be empty");
|
||||
luaS_freeall(L);
|
||||
luaM_free(L, L->stack);
|
||||
luaM_free(L, L->IMtable);
|
||||
luaM_free(L, L->refArray);
|
||||
luaM_free(L, L->Mbuffer);
|
||||
LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
|
||||
luaM_free(L, L);
|
||||
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
||||
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
||||
static void close_state (lua_State *L, lua_State *OL) {
|
||||
if (OL != NULL) { /* are there other threads? */
|
||||
lua_assert(L->previous != L);
|
||||
L->previous->next = L->next;
|
||||
L->next->previous = L->previous;
|
||||
}
|
||||
else if (G(L)) { /* last thread; close global state */
|
||||
luaC_callallgcTM(L); /* call GC tag methods for all udata */
|
||||
luaC_collect(L, 1); /* collect all elements */
|
||||
lua_assert(G(L)->rootproto == NULL);
|
||||
lua_assert(G(L)->rootudata == NULL);
|
||||
lua_assert(G(L)->rootcl == NULL);
|
||||
lua_assert(G(L)->roottable == NULL);
|
||||
luaS_freeall(L);
|
||||
luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
|
||||
luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, l_char);
|
||||
luaM_freelem(NULL, L->G, global_State);
|
||||
}
|
||||
luaM_freearray(OL, L->stack, L->stacksize, TObject);
|
||||
luaM_freelem(OL, L, lua_State);
|
||||
}
|
||||
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
lua_State *OL;
|
||||
lua_assert(L != lua_state || lua_gettop(L) == 0);
|
||||
lua_lock(L);
|
||||
OL = L->next; /* any surviving thread (if there is one) */
|
||||
if (OL == L) OL = NULL; /* no surviving threads */
|
||||
close_state(L, OL);
|
||||
if (OL) lua_unlock(OL); /* cannot unlock over a freed state */
|
||||
lua_assert(L != lua_state || memdebug_numblocks == 0);
|
||||
lua_assert(L != lua_state || memdebug_total == 0);
|
||||
}
|
||||
|
||||
|
||||
103
lstate.h
103
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.57 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,80 +7,95 @@
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include <setjmp.h>
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
/*
|
||||
** macros for thread syncronization inside Lua core machine:
|
||||
** all accesses to the global state and to global objects are syncronized.
|
||||
** Because threads can read the stack of other threads
|
||||
** (when running garbage collection),
|
||||
** a thread must also syncronize any write-access to its own stack.
|
||||
** Unsyncronized accesses are allowed only when reading its own stack,
|
||||
** or when reading immutable fields from global objects
|
||||
** (such as string values and udata values).
|
||||
*/
|
||||
#ifndef lua_lock
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
typedef TObject *StkId; /* index to stack elements */
|
||||
|
||||
#ifndef lua_unlock
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** marks for Reference array
|
||||
** macro to allow the inclusion of user information in Lua state
|
||||
*/
|
||||
#define NONEXT -1 /* to end the free list */
|
||||
#define HOLD -2
|
||||
#define COLLECTED -3
|
||||
#define LOCK -4
|
||||
#ifndef LUA_USERSTATE
|
||||
#define LUA_USERSTATE
|
||||
#endif
|
||||
|
||||
|
||||
struct Ref {
|
||||
TObject o;
|
||||
int st; /* can be LOCK, HOLD, COLLECTED, or next (for free list) */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** chain list of long jumps
|
||||
*/
|
||||
struct lua_longjmp {
|
||||
jmp_buf b;
|
||||
struct lua_longjmp *previous;
|
||||
volatile int status; /* error code */
|
||||
StkId base;
|
||||
};
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
struct TM; /* defined in ltm.h */
|
||||
|
||||
|
||||
typedef struct stringtable {
|
||||
int size;
|
||||
lint32 nuse; /* number of elements */
|
||||
ls_nstr nuse; /* number of elements */
|
||||
TString **hash;
|
||||
} stringtable;
|
||||
|
||||
|
||||
|
||||
struct lua_State {
|
||||
/* thread-specific state */
|
||||
StkId top; /* first free slot in the stack */
|
||||
StkId stack; /* stack base */
|
||||
StkId stack_last; /* last free slot in the stack */
|
||||
int stacksize;
|
||||
StkId Cbase; /* base for current C function */
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
char *Mbuffer; /* global buffer */
|
||||
/*
|
||||
** `global state', shared by all threads of this state
|
||||
*/
|
||||
typedef struct global_State {
|
||||
void *Mbuffer; /* global buffer */
|
||||
size_t Mbuffsize; /* size of Mbuffer */
|
||||
/* global state */
|
||||
Proto *rootproto; /* list of all prototypes */
|
||||
Closure *rootcl; /* list of all closures */
|
||||
Hash *roottable; /* list of all tables */
|
||||
Udata *rootudata; /* list of all userdata */
|
||||
stringtable strt; /* hash table for strings */
|
||||
stringtable udt; /* hash table for udata */
|
||||
Hash *type2tag; /* hash table from type names to tags */
|
||||
Hash *registry; /* (strong) registry table */
|
||||
Hash *weakregistry; /* weakregistry table */
|
||||
struct TM *TMtable; /* table for tag methods */
|
||||
int sizeTM; /* size of TMtable */
|
||||
int ntag; /* number of tags in TMtable */
|
||||
lu_mem GCthreshold;
|
||||
lu_mem nblocks; /* number of `bytes' currently allocated */
|
||||
} global_State;
|
||||
|
||||
|
||||
/*
|
||||
** `per thread' state
|
||||
*/
|
||||
struct lua_State {
|
||||
LUA_USERSTATE
|
||||
StkId top; /* first free slot in the stack */
|
||||
CallInfo *ci; /* call info for current function */
|
||||
StkId stack_last; /* last free slot in the stack */
|
||||
Hash *gt; /* table for globals */
|
||||
struct IM *IMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in IMtable */
|
||||
struct Ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
int refFree; /* list of free positions in refArray */
|
||||
unsigned long GCthreshold;
|
||||
unsigned long nblocks; /* number of `blocks' currently allocated */
|
||||
global_State *G;
|
||||
StkId stack; /* stack base */
|
||||
int stacksize;
|
||||
lua_Hook callhook;
|
||||
lua_Hook linehook;
|
||||
int allowhooks;
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
lua_State *next; /* circular double linked list of states */
|
||||
lua_State *previous;
|
||||
CallInfo basefunc;
|
||||
};
|
||||
|
||||
|
||||
#define G(L) (L->G)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
130
lstring.c
130
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.64 2001/06/07 15:01:21 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lmem.h"
|
||||
@@ -16,117 +17,82 @@
|
||||
|
||||
|
||||
|
||||
void luaS_init (lua_State *L) {
|
||||
L->strt.hash = luaM_newvector(L, 1, TString *);
|
||||
L->udt.hash = luaM_newvector(L, 1, TString *);
|
||||
L->strt.size = L->udt.size = 1;
|
||||
L->strt.nuse = L->udt.nuse = 0;
|
||||
L->strt.hash[0] = L->udt.hash[0] = NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaS_freeall (lua_State *L) {
|
||||
LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
|
||||
luaM_free(L, L->strt.hash);
|
||||
LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
|
||||
luaM_free(L, L->udt.hash);
|
||||
lua_assert(G(L)->strt.nuse==0);
|
||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
|
||||
}
|
||||
|
||||
|
||||
static unsigned long hash_s (const char *s, size_t l) {
|
||||
unsigned long h = l; /* seed */
|
||||
size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */
|
||||
for (; l>=step; l-=step)
|
||||
h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
|
||||
void luaS_resize (lua_State *L, int newsize) {
|
||||
TString **newhash = luaM_newvector(L, newsize, TString *);
|
||||
stringtable *tb = &G(L)->strt;
|
||||
int i;
|
||||
for (i=0; i<newsize; i++) newhash[i] = NULL;
|
||||
/* rehash */
|
||||
for (i=0; i<tb->size; i++) {
|
||||
TString *p = tb->hash[i];
|
||||
while (p) { /* for each node in the list */
|
||||
TString *next = p->nexthash; /* save next */
|
||||
unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
|
||||
int h1 = h&(newsize-1); /* new position */
|
||||
LUA_ASSERT(h%newsize == (h&(newsize-1)),
|
||||
"a&(x-1) == a%x, for x power of 2");
|
||||
p->nexthash = newhash[h1]; /* chain it in new position */
|
||||
TString *next = p->tsv.nexthash; /* save next */
|
||||
lu_hash h = p->tsv.hash;
|
||||
int h1 = lmod(h, newsize); /* new position */
|
||||
lua_assert((int)(h%newsize) == lmod(h, newsize));
|
||||
p->tsv.nexthash = newhash[h1]; /* chain it in new position */
|
||||
newhash[h1] = p;
|
||||
p = next;
|
||||
}
|
||||
}
|
||||
luaM_free(L, tb->hash);
|
||||
luaM_freearray(L, tb->hash, tb->size, TString *);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
|
||||
ts->nexthash = tb->hash[h]; /* chain new entry */
|
||||
static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) {
|
||||
TString *ts = (TString *)luaM_malloc(L, sizestring(l));
|
||||
stringtable *tb;
|
||||
ts->tsv.nexthash = NULL;
|
||||
ts->tsv.len = l;
|
||||
ts->tsv.hash = h;
|
||||
ts->tsv.marked = 0;
|
||||
ts->tsv.constindex = 0;
|
||||
memcpy(getstr(ts), str, l*sizeof(l_char));
|
||||
getstr(ts)[l] = l_c('\0'); /* ending 0 */
|
||||
tb = &G(L)->strt;
|
||||
h = lmod(h, tb->size);
|
||||
ts->tsv.nexthash = tb->hash[h]; /* chain new entry */
|
||||
tb->hash[h] = ts;
|
||||
tb->nuse++;
|
||||
if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */
|
||||
luaS_resize(L, tb, tb->size*2);
|
||||
if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2)
|
||||
luaS_resize(L, tb->size*2); /* too crowded */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
unsigned long h = hash_s(str, l);
|
||||
int h1 = h&(L->strt.size-1);
|
||||
TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) {
|
||||
TString *ts;
|
||||
for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
|
||||
lu_hash h = l; /* seed */
|
||||
size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
|
||||
size_t l1;
|
||||
for (l1=l; l1>=step; l1-=step) /* compute hash */
|
||||
h = h ^ ((h<<5)+(h>>2)+uchar(str[l1-1]));
|
||||
for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
|
||||
ts != NULL;
|
||||
ts = ts->tsv.nexthash) {
|
||||
if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
|
||||
return ts;
|
||||
}
|
||||
/* not found */
|
||||
ts = (TString *)luaM_malloc(L, sizeof(TString)+(lint32)l*sizeof(char));
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->u.s.len = l;
|
||||
ts->u.s.hash = h;
|
||||
ts->u.s.constindex = 0;
|
||||
memcpy(ts->str, str, l);
|
||||
ts->str[l] = 0; /* ending 0 */
|
||||
L->nblocks += gcsizestring(L, l);
|
||||
newentry(L, &L->strt, ts, h1); /* insert it on table */
|
||||
return ts;
|
||||
return newlstr(L, str, l, h); /* not found */
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_createudata (lua_State *L, void *udata, int tag) {
|
||||
unsigned long h = IntPoint(udata);
|
||||
int h1 = h&(L->udt.size-1);
|
||||
TString *ts;
|
||||
for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
|
||||
if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
|
||||
return ts;
|
||||
}
|
||||
/* not found */
|
||||
ts = luaM_new(L, TString);
|
||||
ts->marked = 0;
|
||||
ts->nexthash = NULL;
|
||||
ts->u.d.value = udata;
|
||||
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
|
||||
L->nblocks += gcsizeudata;
|
||||
newentry(L, &L->udt, ts, h1); /* insert it on table */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_new (lua_State *L, const char *str) {
|
||||
return luaS_newlstr(L, str, strlen(str));
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newfixed (lua_State *L, const char *str) {
|
||||
TString *ts = luaS_new(L, str);
|
||||
if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
|
||||
return ts;
|
||||
Udata *luaS_newudata (lua_State *L, size_t s) {
|
||||
Udata *u = (Udata *)luaM_malloc(L, sizeudata(s));
|
||||
u->uv.len = s;
|
||||
u->uv.tag = 0;
|
||||
u->uv.value = u + 1;
|
||||
/* chain it on udata list */
|
||||
u->uv.next = G(L)->rootudata;
|
||||
G(L)->rootudata = u;
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
21
lstring.h
21
lstring.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.20 2000/05/10 16:33:20 roberto Exp roberto $
|
||||
** $Id: lstring.h,v 1.32 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** any TString with mark>=FIXMARK is never collected.
|
||||
** Marks>=RESERVEDMARK are used to identify reserved words.
|
||||
@@ -20,17 +21,19 @@
|
||||
#define RESERVEDMARK 3
|
||||
|
||||
|
||||
#define gcsizestring(L, l) numblocks(L, 0, sizeof(TString)+l)
|
||||
#define gcsizeudata gcsizestring(L, 0)
|
||||
#define sizestring(l) ((lu_mem)sizeof(union TString)+ \
|
||||
((lu_mem)(l)+1)*sizeof(l_char))
|
||||
|
||||
#define sizeudata(l) ((lu_mem)sizeof(union Udata)+(l))
|
||||
|
||||
void luaS_init (lua_State *L);
|
||||
void luaS_resize (lua_State *L, stringtable *tb, int newsize);
|
||||
TString *luaS_createudata (lua_State *L, void *udata, int tag);
|
||||
#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s)))
|
||||
#define luaS_newliteral(L, s) (luaS_newlstr(L, l_s("") s, \
|
||||
(sizeof(s)/sizeof(l_char))-1))
|
||||
|
||||
void luaS_resize (lua_State *L, int newsize);
|
||||
Udata *luaS_newudata (lua_State *L, size_t s);
|
||||
void luaS_freeall (lua_State *L);
|
||||
TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||
TString *luaS_new (lua_State *L, const char *str);
|
||||
TString *luaS_newfixed (lua_State *L, const char *str);
|
||||
TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
530
lstrlib.c
530
lstrlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.52 2000/09/11 17:38:42 roberto Exp roberto $
|
||||
** $Id: lstrlib.c,v 1.68 2001/03/26 14:31:49 roberto Exp roberto $
|
||||
** Standard library for string operations and pattern-matching
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,12 +11,15 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
typedef long sint32; /* a signed version for size_t */
|
||||
|
||||
|
||||
static int str_len (lua_State *L) {
|
||||
size_t l;
|
||||
@@ -26,22 +29,22 @@ static int str_len (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static long posrelat (long pos, size_t len) {
|
||||
static sint32 posrelat (sint32 pos, size_t len) {
|
||||
/* relative string position: negative means back from end */
|
||||
return (pos>=0) ? pos : (long)len+pos+1;
|
||||
return (pos>=0) ? pos : (sint32)len+pos+1;
|
||||
}
|
||||
|
||||
|
||||
static int str_sub (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
long start = posrelat(luaL_check_long(L, 2), l);
|
||||
long end = posrelat(luaL_opt_long(L, 3, -1), l);
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
sint32 start = posrelat(luaL_check_long(L, 2), l);
|
||||
sint32 end = posrelat(luaL_opt_long(L, 3, -1), l);
|
||||
if (start < 1) start = 1;
|
||||
if (end > (long)l) end = l;
|
||||
if (end > (sint32)l) end = l;
|
||||
if (start <= end)
|
||||
lua_pushlstring(L, s+start-1, end-start+1);
|
||||
else lua_pushstring(L, "");
|
||||
else lua_pushliteral(L, l_s(""));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -50,10 +53,10 @@ static int str_lower (lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_putchar(&b, tolower((unsigned char)(s[i])));
|
||||
luaL_putchar(&b, tolower(uchar(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
@@ -63,10 +66,10 @@ static int str_upper (lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=0; i<l; i++)
|
||||
luaL_putchar(&b, toupper((unsigned char)(s[i])));
|
||||
luaL_putchar(&b, toupper(uchar(s[i])));
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
@@ -74,7 +77,7 @@ static int str_upper (lua_State *L) {
|
||||
static int str_rep (lua_State *L) {
|
||||
size_t l;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
int n = luaL_check_int(L, 2);
|
||||
luaL_buffinit(L, &b);
|
||||
while (n-- > 0)
|
||||
@@ -86,10 +89,10 @@ static int str_rep (lua_State *L) {
|
||||
|
||||
static int str_byte (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, 1, &l);
|
||||
long pos = posrelat(luaL_opt_long(L, 2, 1), l);
|
||||
luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range");
|
||||
lua_pushnumber(L, (unsigned char)s[pos-1]);
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l);
|
||||
sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
|
||||
luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, l_s("out of range"));
|
||||
lua_pushnumber(L, uchar(s[pos-1]));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -101,8 +104,8 @@ static int str_char (lua_State *L) {
|
||||
luaL_buffinit(L, &b);
|
||||
for (i=1; i<=n; i++) {
|
||||
int c = luaL_check_int(L, i);
|
||||
luaL_arg_check(L, (unsigned char)c == c, i, "invalid value");
|
||||
luaL_putchar(&b, (unsigned char)c);
|
||||
luaL_arg_check(L, uchar(c) == c, i, l_s("invalid value"));
|
||||
luaL_putchar(&b, uchar(c));
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
@@ -121,48 +124,56 @@ static int str_char (lua_State *L) {
|
||||
#endif
|
||||
|
||||
|
||||
struct Capture {
|
||||
const char *src_end; /* end ('\0') of source string */
|
||||
#define CAP_UNFINISHED (-1)
|
||||
#define CAP_POSITION (-2)
|
||||
|
||||
typedef struct MatchState {
|
||||
const l_char *src_init; /* init of source string */
|
||||
const l_char *src_end; /* end (`\0') of source string */
|
||||
int level; /* total number of captures (finished or unfinished) */
|
||||
struct {
|
||||
const char *init;
|
||||
long len; /* -1 signals unfinished capture */
|
||||
const l_char *init;
|
||||
sint32 len;
|
||||
} capture[MAX_CAPTURES];
|
||||
};
|
||||
lua_State *L;
|
||||
} MatchState;
|
||||
|
||||
|
||||
#define ESC '%'
|
||||
#define SPECIALS "^$*+?.([%-"
|
||||
#define ESC l_c('%')
|
||||
#define SPECIALS l_s("^$*+?.([%-")
|
||||
|
||||
|
||||
static int check_capture (lua_State *L, int l, struct Capture *cap) {
|
||||
l -= '1';
|
||||
if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
|
||||
lua_error(L, "invalid capture index");
|
||||
static int check_capture (MatchState *ms, int l) {
|
||||
l -= l_c('1');
|
||||
if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
|
||||
lua_error(ms->L, l_s("invalid capture index"));
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
static int capture_to_close (lua_State *L, struct Capture *cap) {
|
||||
int level = cap->level;
|
||||
static int capture_to_close (MatchState *ms) {
|
||||
int level = ms->level;
|
||||
for (level--; level>=0; level--)
|
||||
if (cap->capture[level].len == -1) return level;
|
||||
lua_error(L, "invalid pattern capture");
|
||||
if (ms->capture[level].len == CAP_UNFINISHED) return level;
|
||||
lua_error(ms->L, l_s("invalid pattern capture"));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
const char *luaI_classend (lua_State *L, const char *p) {
|
||||
static const l_char *luaI_classend (MatchState *ms, const l_char *p) {
|
||||
switch (*p++) {
|
||||
case ESC:
|
||||
if (*p == '\0') lua_error(L, "malformed pattern (ends with `%')");
|
||||
if (*p == l_c('\0'))
|
||||
lua_error(ms->L, l_s("malformed pattern (ends with `%')"));
|
||||
return p+1;
|
||||
case '[':
|
||||
if (*p == '^') p++;
|
||||
do { /* look for a ']' */
|
||||
if (*p == '\0') lua_error(L, "malformed pattern (missing `]')");
|
||||
if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */
|
||||
} while (*p != ']');
|
||||
case l_c('['):
|
||||
if (*p == l_c('^')) p++;
|
||||
do { /* look for a `]' */
|
||||
if (*p == l_c('\0'))
|
||||
lua_error(ms->L, l_s("malformed pattern (missing `]')"));
|
||||
if (*(p++) == ESC && *p != l_c('\0'))
|
||||
p++; /* skip escapes (e.g. `%]') */
|
||||
} while (*p != l_c(']'));
|
||||
return p+1;
|
||||
default:
|
||||
return p;
|
||||
@@ -170,78 +181,75 @@ const char *luaI_classend (lua_State *L, const char *p) {
|
||||
}
|
||||
|
||||
|
||||
static int match_class (int c, int cl) {
|
||||
static int match_class (l_char c, l_char cl) {
|
||||
int res;
|
||||
switch (tolower(cl)) {
|
||||
case 'a' : res = isalpha(c); break;
|
||||
case 'c' : res = iscntrl(c); break;
|
||||
case 'd' : res = isdigit(c); break;
|
||||
case 'l' : res = islower(c); break;
|
||||
case 'p' : res = ispunct(c); break;
|
||||
case 's' : res = isspace(c); break;
|
||||
case 'u' : res = isupper(c); break;
|
||||
case 'w' : res = isalnum(c); break;
|
||||
case 'x' : res = isxdigit(c); break;
|
||||
case 'z' : res = (c == '\0'); break;
|
||||
switch (tolower(uchar(cl))) {
|
||||
case l_c('a') : res = isalpha(uchar(c)); break;
|
||||
case l_c('c') : res = iscntrl(uchar(c)); break;
|
||||
case l_c('d') : res = isdigit(uchar(c)); break;
|
||||
case l_c('l') : res = islower(uchar(c)); break;
|
||||
case l_c('p') : res = ispunct(uchar(c)); break;
|
||||
case l_c('s') : res = isspace(uchar(c)); break;
|
||||
case l_c('u') : res = isupper(uchar(c)); break;
|
||||
case l_c('w') : res = isalnum(uchar(c)); break;
|
||||
case l_c('x') : res = isxdigit(uchar(c)); break;
|
||||
case l_c('z') : res = (c == l_c('\0')); break;
|
||||
default: return (cl == c);
|
||||
}
|
||||
return (islower(cl) ? res : !res);
|
||||
return (islower(uchar(cl)) ? res : !res);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int matchbracketclass (int c, const char *p, const char *endclass) {
|
||||
static int matchbracketclass (l_char c, const l_char *p, const l_char *ec) {
|
||||
int sig = 1;
|
||||
if (*(p+1) == '^') {
|
||||
if (*(p+1) == l_c('^')) {
|
||||
sig = 0;
|
||||
p++; /* skip the '^' */
|
||||
p++; /* skip the `^' */
|
||||
}
|
||||
while (++p < endclass) {
|
||||
while (++p < ec) {
|
||||
if (*p == ESC) {
|
||||
p++;
|
||||
if (match_class(c, (unsigned char)*p))
|
||||
if (match_class(c, *p))
|
||||
return sig;
|
||||
}
|
||||
else if ((*(p+1) == '-') && (p+2 < endclass)) {
|
||||
else if ((*(p+1) == l_c('-')) && (p+2 < ec)) {
|
||||
p+=2;
|
||||
if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p)
|
||||
if (uchar(*(p-2)) <= uchar(c) && uchar(c) <= uchar(*p))
|
||||
return sig;
|
||||
}
|
||||
else if ((int)(unsigned char)*p == c) return sig;
|
||||
else if (*p == c) return sig;
|
||||
}
|
||||
return !sig;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int luaI_singlematch (int c, const char *p, const char *ep) {
|
||||
static int luaI_singlematch (l_char c, const l_char *p, const l_char *ep) {
|
||||
switch (*p) {
|
||||
case '.': /* matches any char */
|
||||
case l_c('.'): /* matches any char */
|
||||
return 1;
|
||||
case ESC:
|
||||
return match_class(c, (unsigned char)*(p+1));
|
||||
case '[':
|
||||
return match_class(c, *(p+1));
|
||||
case l_c('['):
|
||||
return matchbracketclass(c, p, ep-1);
|
||||
default:
|
||||
return ((unsigned char)*p == c);
|
||||
return (*p == c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *match (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap);
|
||||
static const l_char *match (MatchState *ms, const l_char *s, const l_char *p);
|
||||
|
||||
|
||||
static const char *matchbalance (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
static const l_char *matchbalance (MatchState *ms, const l_char *s,
|
||||
const l_char *p) {
|
||||
if (*p == 0 || *(p+1) == 0)
|
||||
lua_error(L, "unbalanced pattern");
|
||||
lua_error(ms->L, l_s("unbalanced pattern"));
|
||||
if (*s != *p) return NULL;
|
||||
else {
|
||||
int b = *p;
|
||||
int e = *(p+1);
|
||||
int cont = 1;
|
||||
while (++s < cap->src_end) {
|
||||
while (++s < ms->src_end) {
|
||||
if (*s == e) {
|
||||
if (--cont == 0) return s+1;
|
||||
}
|
||||
@@ -252,14 +260,14 @@ static const char *matchbalance (lua_State *L, const char *s, const char *p,
|
||||
}
|
||||
|
||||
|
||||
static const char *max_expand (lua_State *L, const char *s, const char *p,
|
||||
const char *ep, struct Capture *cap) {
|
||||
long i = 0; /* counts maximum expand for item */
|
||||
while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
|
||||
static const l_char *max_expand (MatchState *ms, const l_char *s,
|
||||
const l_char *p, const l_char *ep) {
|
||||
sint32 i = 0; /* counts maximum expand for item */
|
||||
while ((s+i)<ms->src_end && luaI_singlematch(*(s+i), p, ep))
|
||||
i++;
|
||||
/* keeps trying to match with the maximum repetitions */
|
||||
while (i>=0) {
|
||||
const char *res = match(L, (s+i), ep+1, cap);
|
||||
const l_char *res = match(ms, (s+i), ep+1);
|
||||
if (res) return res;
|
||||
i--; /* else didn't match; reduce 1 repetition to try again */
|
||||
}
|
||||
@@ -267,100 +275,102 @@ static const char *max_expand (lua_State *L, const char *s, const char *p,
|
||||
}
|
||||
|
||||
|
||||
static const char *min_expand (lua_State *L, const char *s, const char *p,
|
||||
const char *ep, struct Capture *cap) {
|
||||
static const l_char *min_expand (MatchState *ms, const l_char *s,
|
||||
const l_char *p, const l_char *ep) {
|
||||
for (;;) {
|
||||
const char *res = match(L, s, ep+1, cap);
|
||||
const l_char *res = match(ms, s, ep+1);
|
||||
if (res != NULL)
|
||||
return res;
|
||||
else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep))
|
||||
else if (s<ms->src_end && luaI_singlematch(*s, p, ep))
|
||||
s++; /* try with one more repetition */
|
||||
else return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *start_capture (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
const char *res;
|
||||
int level = cap->level;
|
||||
if (level >= MAX_CAPTURES) lua_error(L, "too many captures");
|
||||
cap->capture[level].init = s;
|
||||
cap->capture[level].len = -1;
|
||||
cap->level = level+1;
|
||||
if ((res=match(L, s, p+1, cap)) == NULL) /* match failed? */
|
||||
cap->level--; /* undo capture */
|
||||
static const l_char *start_capture (MatchState *ms, const l_char *s,
|
||||
const l_char *p, int what) {
|
||||
const l_char *res;
|
||||
int level = ms->level;
|
||||
if (level >= MAX_CAPTURES) lua_error(ms->L, l_s("too many captures"));
|
||||
ms->capture[level].init = s;
|
||||
ms->capture[level].len = what;
|
||||
ms->level = level+1;
|
||||
if ((res=match(ms, s, p)) == NULL) /* match failed? */
|
||||
ms->level--; /* undo capture */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static const char *end_capture (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
int l = capture_to_close(L, cap);
|
||||
const char *res;
|
||||
cap->capture[l].len = s - cap->capture[l].init; /* close capture */
|
||||
if ((res = match(L, s, p+1, cap)) == NULL) /* match failed? */
|
||||
cap->capture[l].len = -1; /* undo capture */
|
||||
static const l_char *end_capture (MatchState *ms, const l_char *s,
|
||||
const l_char *p) {
|
||||
int l = capture_to_close(ms);
|
||||
const l_char *res;
|
||||
ms->capture[l].len = s - ms->capture[l].init; /* close capture */
|
||||
if ((res = match(ms, s, p)) == NULL) /* match failed? */
|
||||
ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static const char *match_capture (lua_State *L, const char *s, int level,
|
||||
struct Capture *cap) {
|
||||
int l = check_capture(L, level, cap);
|
||||
size_t len = cap->capture[l].len;
|
||||
if ((size_t)(cap->src_end-s) >= len &&
|
||||
memcmp(cap->capture[l].init, s, len) == 0)
|
||||
static const l_char *match_capture (MatchState *ms, const l_char *s, int l) {
|
||||
size_t len;
|
||||
l = check_capture(ms, l);
|
||||
len = ms->capture[l].len;
|
||||
if ((size_t)(ms->src_end-s) >= len &&
|
||||
memcmp(ms->capture[l].init, s, len) == 0)
|
||||
return s+len;
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *match (lua_State *L, const char *s, const char *p,
|
||||
struct Capture *cap) {
|
||||
static const l_char *match (MatchState *ms, const l_char *s, const l_char *p) {
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
switch (*p) {
|
||||
case '(': /* start capture */
|
||||
return start_capture(L, s, p, cap);
|
||||
case ')': /* end capture */
|
||||
return end_capture(L, s, p, cap);
|
||||
case l_c('('): /* start capture */
|
||||
if (*(p+1) == l_c(')')) /* position capture? */
|
||||
return start_capture(ms, s, p+2, CAP_POSITION);
|
||||
else
|
||||
return start_capture(ms, s, p+1, CAP_UNFINISHED);
|
||||
case l_c(')'): /* end capture */
|
||||
return end_capture(ms, s, p+1);
|
||||
case ESC: /* may be %[0-9] or %b */
|
||||
if (isdigit((unsigned char)(*(p+1)))) { /* capture? */
|
||||
s = match_capture(L, s, *(p+1), cap);
|
||||
if (isdigit(uchar(*(p+1)))) { /* capture? */
|
||||
s = match_capture(ms, s, *(p+1));
|
||||
if (s == NULL) return NULL;
|
||||
p+=2; goto init; /* else return match(L, s, p+2, cap) */
|
||||
p+=2; goto init; /* else return match(ms, s, p+2) */
|
||||
}
|
||||
else if (*(p+1) == 'b') { /* balanced string? */
|
||||
s = matchbalance(L, s, p+2, cap);
|
||||
else if (*(p+1) == l_c('b')) { /* balanced string? */
|
||||
s = matchbalance(ms, s, p+2);
|
||||
if (s == NULL) return NULL;
|
||||
p+=4; goto init; /* else return match(L, s, p+4, cap); */
|
||||
p+=4; goto init; /* else return match(ms, s, p+4); */
|
||||
}
|
||||
else goto dflt; /* case default */
|
||||
case '\0': /* end of pattern */
|
||||
case l_c('\0'): /* end of pattern */
|
||||
return s; /* match succeeded */
|
||||
case '$':
|
||||
if (*(p+1) == '\0') /* is the '$' the last char in pattern? */
|
||||
return (s == cap->src_end) ? s : NULL; /* check end of string */
|
||||
case l_c('$'):
|
||||
if (*(p+1) == l_c('\0')) /* is the `$' the last char in pattern? */
|
||||
return (s == ms->src_end) ? s : NULL; /* check end of string */
|
||||
else goto dflt;
|
||||
default: dflt: { /* it is a pattern item */
|
||||
const char *ep = luaI_classend(L, p); /* points to what is next */
|
||||
int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep);
|
||||
const l_char *ep = luaI_classend(ms, p); /* points to what is next */
|
||||
int m = s<ms->src_end && luaI_singlematch(*s, p, ep);
|
||||
switch (*ep) {
|
||||
case '?': { /* optional */
|
||||
const char *res;
|
||||
if (m && ((res=match(L, s+1, ep+1, cap)) != NULL))
|
||||
case l_c('?'): { /* optional */
|
||||
const l_char *res;
|
||||
if (m && ((res=match(ms, s+1, ep+1)) != NULL))
|
||||
return res;
|
||||
p=ep+1; goto init; /* else return match(L, s, ep+1, cap); */
|
||||
p=ep+1; goto init; /* else return match(ms, s, ep+1); */
|
||||
}
|
||||
case '*': /* 0 or more repetitions */
|
||||
return max_expand(L, s, p, ep, cap);
|
||||
case '+': /* 1 or more repetitions */
|
||||
return (m ? max_expand(L, s+1, p, ep, cap) : NULL);
|
||||
case '-': /* 0 or more repetitions (minimum) */
|
||||
return min_expand(L, s, p, ep, cap);
|
||||
case l_c('*'): /* 0 or more repetitions */
|
||||
return max_expand(ms, s, p, ep);
|
||||
case l_c('+'): /* 1 or more repetitions */
|
||||
return (m ? max_expand(ms, s+1, p, ep) : NULL);
|
||||
case l_c('-'): /* 0 or more repetitions (minimum) */
|
||||
return min_expand(ms, s, p, ep);
|
||||
default:
|
||||
if (!m) return NULL;
|
||||
s++; p=ep; goto init; /* else return match(L, s+1, ep, cap); */
|
||||
s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -368,15 +378,15 @@ static const char *match (lua_State *L, const char *s, const char *p,
|
||||
|
||||
|
||||
|
||||
static const char *lmemfind (const char *s1, size_t l1,
|
||||
const char *s2, size_t l2) {
|
||||
static const l_char *lmemfind (const l_char *s1, size_t l1,
|
||||
const l_char *s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
||||
else {
|
||||
const char *init; /* to search for a `*s2' inside `s1' */
|
||||
const l_char *init; /* to search for a `*s2' inside `s1' */
|
||||
l2--; /* 1st char will be checked by `memchr' */
|
||||
l1 = l1-l2; /* `s2' cannot be found after that */
|
||||
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
||||
while (l1 > 0 && (init = (const l_char *)memchr(s1, *s2, l1)) != NULL) {
|
||||
init++; /* 1st char is already checked */
|
||||
if (memcmp(init, s2+1, l2) == 0)
|
||||
return init-1;
|
||||
@@ -390,28 +400,34 @@ static const char *lmemfind (const char *s1, size_t l1,
|
||||
}
|
||||
|
||||
|
||||
static int push_captures (lua_State *L, struct Capture *cap) {
|
||||
static void push_onecapture (MatchState *ms, int i) {
|
||||
int l = ms->capture[i].len;
|
||||
if (l == CAP_UNFINISHED) lua_error(ms->L, l_s("unfinished capture"));
|
||||
if (l == CAP_POSITION)
|
||||
lua_pushnumber(ms->L, ms->capture[i].init - ms->src_init + 1);
|
||||
else
|
||||
lua_pushlstring(ms->L, ms->capture[i].init, l);
|
||||
}
|
||||
|
||||
|
||||
static int push_captures (MatchState *ms) {
|
||||
int i;
|
||||
luaL_checkstack(L, cap->level, "too many captures");
|
||||
for (i=0; i<cap->level; i++) {
|
||||
int l = cap->capture[i].len;
|
||||
if (l == -1) lua_error(L, "unfinished capture");
|
||||
lua_pushlstring(L, cap->capture[i].init, l);
|
||||
}
|
||||
return cap->level; /* number of strings pushed */
|
||||
luaL_checkstack(ms->L, ms->level, l_s("too many captures"));
|
||||
for (i=0; i<ms->level; i++)
|
||||
push_onecapture(ms, i);
|
||||
return ms->level; /* number of strings pushed */
|
||||
}
|
||||
|
||||
|
||||
static int str_find (lua_State *L) {
|
||||
size_t l1, l2;
|
||||
const char *s = luaL_check_lstr(L, 1, &l1);
|
||||
const char *p = luaL_check_lstr(L, 2, &l2);
|
||||
long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
|
||||
struct Capture cap;
|
||||
luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
|
||||
const l_char *s = luaL_check_lstr(L, 1, &l1);
|
||||
const l_char *p = luaL_check_lstr(L, 2, &l2);
|
||||
sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
|
||||
luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, l_s("out of range"));
|
||||
if (lua_gettop(L) > 3 || /* extra argument? */
|
||||
strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
|
||||
const char *s2 = lmemfind(s+init, l1-init, p, l2);
|
||||
const l_char *s2 = lmemfind(s+init, l1-init, p, l2);
|
||||
if (s2) {
|
||||
lua_pushnumber(L, s2-s+1);
|
||||
lua_pushnumber(L, s2-s+l2);
|
||||
@@ -419,27 +435,31 @@ static int str_find (lua_State *L) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
const char *s1=s+init;
|
||||
cap.src_end = s+l1;
|
||||
MatchState ms;
|
||||
int anchor = (*p == l_c('^')) ? (p++, 1) : 0;
|
||||
const l_char *s1=s+init;
|
||||
ms.L = L;
|
||||
ms.src_init = s;
|
||||
ms.src_end = s+l1;
|
||||
do {
|
||||
const char *res;
|
||||
cap.level = 0;
|
||||
if ((res=match(L, s1, p, &cap)) != NULL) {
|
||||
const l_char *res;
|
||||
ms.level = 0;
|
||||
if ((res=match(&ms, s1, p)) != NULL) {
|
||||
lua_pushnumber(L, s1-s+1); /* start */
|
||||
lua_pushnumber(L, res-s); /* end */
|
||||
return push_captures(L, &cap) + 2;
|
||||
return push_captures(&ms) + 2;
|
||||
}
|
||||
} while (s1++<cap.src_end && !anchor);
|
||||
} while (s1++<ms.src_end && !anchor);
|
||||
}
|
||||
lua_pushnil(L); /* not found */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
static void add_s (MatchState *ms, luaL_Buffer *b) {
|
||||
lua_State *L = ms->L;
|
||||
if (lua_isstring(L, 3)) {
|
||||
const char *news = lua_tostring(L, 3);
|
||||
const l_char *news = lua_tostring(L, 3);
|
||||
size_t l = lua_strlen(L, 3);
|
||||
size_t i;
|
||||
for (i=0; i<l; i++) {
|
||||
@@ -447,11 +467,12 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit((unsigned char)news[i]))
|
||||
if (!isdigit(uchar(news[i])))
|
||||
luaL_putchar(b, news[i]);
|
||||
else {
|
||||
int level = check_capture(L, news[i], cap);
|
||||
luaL_addlstring(b, cap->capture[level].init, cap->capture[level].len);
|
||||
int level = check_capture(ms, news[i]);
|
||||
push_onecapture(ms, level);
|
||||
luaL_addvalue(b); /* add capture to accumulated result */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -459,7 +480,7 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
else { /* is a function */
|
||||
int n;
|
||||
lua_pushvalue(L, 3);
|
||||
n = push_captures(L, cap);
|
||||
n = push_captures(ms);
|
||||
lua_rawcall(L, n, 1);
|
||||
if (lua_isstring(L, -1))
|
||||
luaL_addvalue(b); /* add return to accumulated result */
|
||||
@@ -471,34 +492,36 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||
|
||||
static int str_gsub (lua_State *L) {
|
||||
size_t srcl;
|
||||
const char *src = luaL_check_lstr(L, 1, &srcl);
|
||||
const char *p = luaL_check_string(L, 2);
|
||||
const l_char *src = luaL_check_lstr(L, 1, &srcl);
|
||||
const l_char *p = luaL_check_string(L, 2);
|
||||
int max_s = luaL_opt_int(L, 4, srcl+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int anchor = (*p == l_c('^')) ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
struct Capture cap;
|
||||
MatchState ms;
|
||||
luaL_Buffer b;
|
||||
luaL_arg_check(L,
|
||||
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
|
||||
3, "string or function expected");
|
||||
3, l_s("string or function expected"));
|
||||
luaL_buffinit(L, &b);
|
||||
cap.src_end = src+srcl;
|
||||
ms.L = L;
|
||||
ms.src_init = src;
|
||||
ms.src_end = src+srcl;
|
||||
while (n < max_s) {
|
||||
const char *e;
|
||||
cap.level = 0;
|
||||
e = match(L, src, p, &cap);
|
||||
const l_char *e;
|
||||
ms.level = 0;
|
||||
e = match(&ms, src, p);
|
||||
if (e) {
|
||||
n++;
|
||||
add_s(L, &b, &cap);
|
||||
add_s(&ms, &b);
|
||||
}
|
||||
if (e && e>src) /* non empty match? */
|
||||
src = e; /* skip it */
|
||||
else if (src < cap.src_end)
|
||||
else if (src < ms.src_end)
|
||||
luaL_putchar(&b, *src++);
|
||||
else break;
|
||||
if (anchor) break;
|
||||
}
|
||||
luaL_addlstring(&b, src, cap.src_end-src);
|
||||
luaL_addlstring(&b, src, ms.src_end-src);
|
||||
luaL_pushresult(&b);
|
||||
lua_pushnumber(L, n); /* number of substitutions */
|
||||
return 2;
|
||||
@@ -507,88 +530,105 @@ static int str_gsub (lua_State *L) {
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
luaL_putchar(b, '"');
|
||||
while (l--) {
|
||||
switch (*s) {
|
||||
case '"': case '\\': case '\n':
|
||||
luaL_putchar(b, '\\');
|
||||
luaL_putchar(b, *s);
|
||||
break;
|
||||
case '\0': luaL_addlstring(b, "\\000", 4); break;
|
||||
default: luaL_putchar(b, *s);
|
||||
}
|
||||
s++;
|
||||
}
|
||||
luaL_putchar(b, '"');
|
||||
}
|
||||
|
||||
/* maximum size of each formated item (> len(format('%99.99f', -1e308))) */
|
||||
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
||||
#define MAX_ITEM 512
|
||||
/* maximum size of each format specification (such as '%-099.99d') */
|
||||
#define MAX_FORMAT 20
|
||||
|
||||
|
||||
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
size_t l;
|
||||
const l_char *s = luaL_check_lstr(L, arg, &l);
|
||||
luaL_putchar(b, l_c('"'));
|
||||
while (l--) {
|
||||
switch (*s) {
|
||||
case l_c('"'): case l_c('\\'): case l_c('\n'):
|
||||
luaL_putchar(b, l_c('\\'));
|
||||
luaL_putchar(b, *s);
|
||||
break;
|
||||
case l_c('\0'): luaL_addlstring(b, l_s("\\000"), 4); break;
|
||||
default: luaL_putchar(b, *s);
|
||||
}
|
||||
s++;
|
||||
}
|
||||
luaL_putchar(b, l_c('"'));
|
||||
}
|
||||
|
||||
|
||||
static const l_char *scanformat (lua_State *L, const l_char *strfrmt,
|
||||
l_char *form, int *hasprecision) {
|
||||
const l_char *p = strfrmt;
|
||||
while (strchr(l_s("-+ #0"), *p)) p++; /* skip flags */
|
||||
if (isdigit(uchar(*p))) p++; /* skip width */
|
||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
||||
if (*p == l_c('.')) {
|
||||
p++;
|
||||
*hasprecision = 1;
|
||||
if (isdigit(uchar(*p))) p++; /* skip precision */
|
||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
||||
}
|
||||
if (isdigit(uchar(*p)))
|
||||
lua_error(L, l_s("invalid format (width or precision too long)"));
|
||||
if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
|
||||
lua_error(L, l_s("invalid format (too long)"));
|
||||
form[0] = l_c('%');
|
||||
strncpy(form+1, strfrmt, p-strfrmt+1);
|
||||
form[p-strfrmt+2] = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static int str_format (lua_State *L) {
|
||||
int arg = 1;
|
||||
const char *strfrmt = luaL_check_string(L, arg);
|
||||
size_t sfl;
|
||||
const l_char *strfrmt = luaL_check_lstr(L, arg, &sfl);
|
||||
const l_char *strfrmt_end = strfrmt+sfl;
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
while (*strfrmt) {
|
||||
if (*strfrmt != '%')
|
||||
while (strfrmt < strfrmt_end) {
|
||||
if (*strfrmt != l_c('%'))
|
||||
luaL_putchar(&b, *strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
else if (*++strfrmt == l_c('%'))
|
||||
luaL_putchar(&b, *strfrmt++); /* %% */
|
||||
else { /* format item */
|
||||
struct Capture cap;
|
||||
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||
char buff[MAX_ITEM]; /* to store the formatted item */
|
||||
const char *initf = strfrmt;
|
||||
form[0] = '%';
|
||||
if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
|
||||
arg = *initf - '0';
|
||||
initf += 2; /* skip the 'n$' */
|
||||
}
|
||||
l_char form[MAX_FORMAT]; /* to store the format (`%...') */
|
||||
l_char buff[MAX_ITEM]; /* to store the formatted item */
|
||||
int hasprecision = 0;
|
||||
if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == l_c('$'))
|
||||
lua_error(L, l_s("obsolete `format' option (d$)"));
|
||||
arg++;
|
||||
cap.src_end = strfrmt+strlen(strfrmt)+1;
|
||||
cap.level = 0;
|
||||
strfrmt = match(L, initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
|
||||
if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */
|
||||
strfrmt-initf > MAX_FORMAT-2)
|
||||
lua_error(L, "invalid format (width or precision too long)");
|
||||
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
|
||||
form[strfrmt-initf+2] = 0;
|
||||
strfrmt = scanformat(L, strfrmt, form, &hasprecision);
|
||||
switch (*strfrmt++) {
|
||||
case 'c': case 'd': case 'i':
|
||||
case l_c('c'): case l_c('d'): case l_c('i'):
|
||||
sprintf(buff, form, luaL_check_int(L, arg));
|
||||
break;
|
||||
case 'o': case 'u': case 'x': case 'X':
|
||||
case l_c('o'): case l_c('u'): case l_c('x'): case l_c('X'):
|
||||
sprintf(buff, form, (unsigned int)luaL_check_number(L, arg));
|
||||
break;
|
||||
case 'e': case 'E': case 'f': case 'g': case 'G':
|
||||
case l_c('e'): case l_c('E'): case l_c('f'):
|
||||
case l_c('g'): case l_c('G'):
|
||||
sprintf(buff, form, luaL_check_number(L, arg));
|
||||
break;
|
||||
case 'q':
|
||||
case l_c('q'):
|
||||
luaI_addquoted(L, &b, arg);
|
||||
continue; /* skip the "addsize" at the end */
|
||||
case 's': {
|
||||
continue; /* skip the `addsize' at the end */
|
||||
case l_c('s'): {
|
||||
size_t l;
|
||||
const char *s = luaL_check_lstr(L, arg, &l);
|
||||
if (cap.capture[1].len == 0 && l >= 100) {
|
||||
const l_char *s = luaL_check_lstr(L, arg, &l);
|
||||
if (!hasprecision && l >= 100) {
|
||||
/* no precision and string is too long to be formatted;
|
||||
keep original string */
|
||||
lua_pushvalue(L, arg);
|
||||
luaL_addvalue(&b);
|
||||
continue; /* skip the "addsize" at the end */
|
||||
continue; /* skip the `addsize' at the end */
|
||||
}
|
||||
else {
|
||||
sprintf(buff, form, s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
default: /* also treat cases 'pnLlh' */
|
||||
lua_error(L, "invalid option in `format'");
|
||||
default: /* also treat cases `pnLlh' */
|
||||
lua_error(L, l_s("invalid option in `format'"));
|
||||
}
|
||||
luaL_addlstring(&b, buff, strlen(buff));
|
||||
}
|
||||
@@ -598,24 +638,24 @@ static int str_format (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_reg strlib[] = {
|
||||
{"strlen", str_len},
|
||||
{"strsub", str_sub},
|
||||
{"strlower", str_lower},
|
||||
{"strupper", str_upper},
|
||||
{"strchar", str_char},
|
||||
{"strrep", str_rep},
|
||||
{"ascii", str_byte}, /* for compatibility with 3.0 and earlier */
|
||||
{"strbyte", str_byte},
|
||||
{"format", str_format},
|
||||
{"strfind", str_find},
|
||||
{"gsub", str_gsub}
|
||||
static const luaL_reg strlib[] = {
|
||||
{l_s("strlen"), str_len},
|
||||
{l_s("strsub"), str_sub},
|
||||
{l_s("strlower"), str_lower},
|
||||
{l_s("strupper"), str_upper},
|
||||
{l_s("strchar"), str_char},
|
||||
{l_s("strrep"), str_rep},
|
||||
{l_s("strbyte"), str_byte},
|
||||
{l_s("format"), str_format},
|
||||
{l_s("strfind"), str_find},
|
||||
{l_s("gsub"), str_gsub}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
void lua_strlibopen (lua_State *L) {
|
||||
LUALIB_API int lua_strlibopen (lua_State *L) {
|
||||
luaL_openl(L, strlib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
333
ltable.c
333
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.82 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -18,21 +18,23 @@
|
||||
*/
|
||||
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
|
||||
|
||||
#define gcsize(L, n) numblocks(L, n*2, sizeof(Hash))
|
||||
|
||||
#define TagDefault LUA_TTABLE
|
||||
|
||||
|
||||
|
||||
#define TagDefault TAG_TABLE
|
||||
|
||||
#define hashnum(t,n) (node(t, lmod((lu_hash)(ls_hash)(n), t->size)))
|
||||
#define hashstr(t,str) (node(t, lmod((str)->tsv.hash, t->size)))
|
||||
#define hashpointer(t,p) (node(t, lmod(IntPoint(p), t->size)))
|
||||
|
||||
|
||||
/*
|
||||
@@ -40,157 +42,92 @@
|
||||
** of its hash value)
|
||||
*/
|
||||
Node *luaH_mainposition (const Hash *t, const TObject *key) {
|
||||
unsigned long h;
|
||||
switch (ttype(key)) {
|
||||
case TAG_NUMBER:
|
||||
h = (unsigned long)(long)nvalue(key);
|
||||
break;
|
||||
case TAG_STRING:
|
||||
h = tsvalue(key)->u.s.hash;
|
||||
break;
|
||||
case TAG_USERDATA:
|
||||
h = IntPoint(tsvalue(key));
|
||||
break;
|
||||
case TAG_TABLE:
|
||||
h = IntPoint(hvalue(key));
|
||||
break;
|
||||
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||
h = IntPoint(clvalue(key));
|
||||
break;
|
||||
default:
|
||||
return NULL; /* invalid key */
|
||||
}
|
||||
LUA_ASSERT(h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
|
||||
"a&(x-1) == a%x, for x power of 2");
|
||||
return &t->node[h&(t->size-1)];
|
||||
}
|
||||
|
||||
|
||||
static const TObject *luaH_getany (lua_State *L, const Hash *t,
|
||||
const TObject *key) {
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
if (!n)
|
||||
lua_error(L, "table index is nil");
|
||||
else do {
|
||||
if (luaO_equalObj(key, &n->key))
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
/* specialized version for numbers */
|
||||
const TObject *luaH_getnum (const Hash *t, Number key) {
|
||||
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
|
||||
do {
|
||||
if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key)
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
/* specialized version for strings */
|
||||
const TObject *luaH_getstr (const Hash *t, TString *key) {
|
||||
Node *n = &t->node[key->u.s.hash&(t->size-1)];
|
||||
do {
|
||||
if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key)
|
||||
return &n->val;
|
||||
n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject; /* key not found */
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case TAG_NUMBER: return luaH_getnum(t, nvalue(key));
|
||||
case TAG_STRING: return luaH_getstr(t, tsvalue(key));
|
||||
default: return luaH_getany(L, t, key);
|
||||
case LUA_TNUMBER:
|
||||
return hashnum(t, nvalue(key));
|
||||
case LUA_TSTRING:
|
||||
return hashstr(t, tsvalue(key));
|
||||
default: /* all other types are hashed as (void *) */
|
||||
return hashpointer(t, tsvalue(key));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
|
||||
Node *luaH_next (lua_State *L, Hash *t, const TObject *key) {
|
||||
int i;
|
||||
if (ttype(key) == TAG_NIL)
|
||||
if (ttype(key) == LUA_TNIL)
|
||||
i = 0; /* first iteration */
|
||||
else {
|
||||
const TObject *v = luaH_get(L, t, key);
|
||||
const TObject *v = luaH_get(t, key);
|
||||
if (v == &luaO_nilobject)
|
||||
lua_error(L, "invalid key for `next'");
|
||||
i = (int)(((const char *)v -
|
||||
(const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
|
||||
luaD_error(L, l_s("invalid key for `next'"));
|
||||
i = (int)(((const lu_byte *)v -
|
||||
(const lu_byte *)(val(node(t, 0)))) / sizeof(Node)) + 1;
|
||||
}
|
||||
for (; i<t->size; i++) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(val(n)) != TAG_NIL)
|
||||
if (ttype(val(n)) != LUA_TNIL)
|
||||
return n;
|
||||
}
|
||||
return NULL; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** try to remove a key without value from a table. To avoid problems with
|
||||
** hash, change `key' for a number with the same hash.
|
||||
*/
|
||||
void luaH_remove (Hash *t, TObject *key) {
|
||||
if (ttype(key) == TAG_NUMBER ||
|
||||
(ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30))
|
||||
return; /* do not remove numbers nor small strings */
|
||||
else {
|
||||
/* try to find a number `n' with the same hash as `key' */
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
int n = mp - &t->node[0];
|
||||
/* make sure `n' is not in `t' */
|
||||
while (luaH_getnum(t, n) != &luaO_nilobject) {
|
||||
if (n >= MAX_INT - t->size)
|
||||
return; /* give up; (to avoid overflow) */
|
||||
n += t->size;
|
||||
}
|
||||
ttype(key) = TAG_NUMBER;
|
||||
nvalue(key) = n;
|
||||
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
|
||||
int luaH_nexti (Hash *t, int i) {
|
||||
while ((++i)<t->size) {
|
||||
if (ttype(val(node(t, i))) != LUA_TNIL) /* a non-nil value? */
|
||||
return i;
|
||||
}
|
||||
return -1; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Hash *t, lint32 size) {
|
||||
int i;
|
||||
if (size > MAX_INT)
|
||||
lua_error(L, "table overflow");
|
||||
t->node = luaM_newvector(L, size, Node);
|
||||
for (i=0; i<(int)size; i++) {
|
||||
ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL;
|
||||
t->node[i].next = NULL;
|
||||
#define check_grow(L, p, n) \
|
||||
if ((p) >= MAX_INT/(n)) luaD_error(L, l_s("table overflow"));
|
||||
|
||||
/*
|
||||
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
||||
*/
|
||||
static int power2 (lua_State *L, int n) {
|
||||
int p = MINPOWER2;
|
||||
while (p <= n) {
|
||||
check_grow(L, p, 2);
|
||||
p *= 2;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Hash *t, int size) {
|
||||
int i;
|
||||
t->node = luaM_newvector(L, size, Node);
|
||||
for (i=0; i<size; i++) {
|
||||
t->node[i].next = NULL;
|
||||
setnilvalue(key(node(t, i)));
|
||||
setnilvalue(val(node(t, i)));
|
||||
}
|
||||
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
|
||||
t->size = size;
|
||||
t->firstfree = &t->node[size-1]; /* first free position to be used */
|
||||
t->firstfree = node(t, size-1); /* first free position to be used */
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (lua_State *L, int size) {
|
||||
Hash *t = luaM_new(L, Hash);
|
||||
t->htag = TagDefault;
|
||||
t->next = L->roottable;
|
||||
L->roottable = t;
|
||||
t->next = G(L)->roottable;
|
||||
G(L)->roottable = t;
|
||||
t->mark = t;
|
||||
t->size = 0;
|
||||
L->nblocks += gcsize(L, 0);
|
||||
t->weakmode = 0;
|
||||
t->node = NULL;
|
||||
setnodevector(L, t, luaO_power2(size));
|
||||
setnodevector(L, t, power2(L, size));
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (lua_State *L, Hash *t) {
|
||||
L->nblocks -= gcsize(L, t->size);
|
||||
luaM_free(L, t->node);
|
||||
luaM_free(L, t);
|
||||
luaM_freearray(L, t->node, t->size, Node);
|
||||
luaM_freelem(L, t, Hash);
|
||||
}
|
||||
|
||||
|
||||
@@ -200,7 +137,7 @@ static int numuse (const Hash *t) {
|
||||
int realuse = 0;
|
||||
int i;
|
||||
for (i=0; i<size; i++) {
|
||||
if (ttype(&v[i].val) != TAG_NIL)
|
||||
if (ttype(&v[i].val) != LUA_TNIL)
|
||||
realuse++;
|
||||
}
|
||||
return realuse;
|
||||
@@ -212,53 +149,46 @@ static void rehash (lua_State *L, Hash *t) {
|
||||
Node *nold = t->node;
|
||||
int nelems = numuse(t);
|
||||
int i;
|
||||
LUA_ASSERT(nelems<=oldsize, "wrong count");
|
||||
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
|
||||
setnodevector(L, t, (lint32)oldsize*2);
|
||||
lua_assert(nelems<=oldsize);
|
||||
if (nelems >= oldsize-oldsize/4) { /* using more than 3/4? */
|
||||
check_grow(L, oldsize, 2);
|
||||
setnodevector(L, t, oldsize*2); /* grow array */
|
||||
}
|
||||
else if (nelems <= oldsize/4 && /* less than 1/4? */
|
||||
oldsize > MINPOWER2)
|
||||
setnodevector(L, t, oldsize/2);
|
||||
setnodevector(L, t, oldsize/2); /* shrink array */
|
||||
else
|
||||
setnodevector(L, t, oldsize);
|
||||
setnodevector(L, t, oldsize); /* just rehash; keep the same size */
|
||||
for (i=0; i<oldsize; i++) {
|
||||
Node *old = nold+i;
|
||||
if (ttype(&old->val) != TAG_NIL)
|
||||
*luaH_set(L, t, &old->key) = old->val;
|
||||
if (ttype(val(old)) != LUA_TNIL) {
|
||||
TObject *v = luaH_set(L, t, key(old));
|
||||
setobj(v, val(old));
|
||||
}
|
||||
}
|
||||
luaM_free(L, nold); /* free old array */
|
||||
luaM_freearray(L, nold, oldsize, Node); /* free old array */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** inserts a key into a hash table; first, check whether key is
|
||||
** already present; if not, check whether key's main position is free;
|
||||
** if not, check whether colliding node is in its main position or not;
|
||||
** if it is not, move colliding node to an empty place and put new key
|
||||
** in its main position; otherwise (colliding node is in its main position),
|
||||
** new key goes to an empty position.
|
||||
** inserts a new key into a hash table; first, check whether key's main
|
||||
** position is free. If not, check whether colliding node is in its main
|
||||
** position or not: if it is not, move colliding node to an empty place and
|
||||
** put new key in its main position; otherwise (colliding node is in its main
|
||||
** position), new key goes to an empty position.
|
||||
*/
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
static TObject *newkey (lua_State *L, Hash *t, const TObject *key) {
|
||||
Node *mp = luaH_mainposition(t, key);
|
||||
Node *n = mp;
|
||||
if (!mp)
|
||||
lua_error(L, "table index is nil");
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_equalObj(key, &n->key))
|
||||
return &n->val; /* that's all */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
/* `key' not found; must insert it */
|
||||
if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
|
||||
Node *othern; /* main position of colliding node */
|
||||
n = t->firstfree; /* get a free place */
|
||||
/* is colliding node out of its main position? (can only happens if
|
||||
its position is after "firstfree") */
|
||||
if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
|
||||
if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */
|
||||
Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */
|
||||
Node *n = t->firstfree; /* get a free place */
|
||||
if (othern != mp) { /* is colliding node out of its main position? */
|
||||
/* yes; move colliding node into free position */
|
||||
while (othern->next != mp) othern = othern->next; /* find previous */
|
||||
othern->next = n; /* redo the chain with `n' in place of `mp' */
|
||||
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
|
||||
mp->next = NULL; /* now `mp' is free */
|
||||
setnilvalue(val(mp));
|
||||
}
|
||||
else { /* colliding node is in its own main position */
|
||||
/* new node will go into free position */
|
||||
@@ -267,10 +197,11 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
mp = n;
|
||||
}
|
||||
}
|
||||
mp->key = *key;
|
||||
setobj(key(mp), key);
|
||||
lua_assert(ttype(val(mp)) == LUA_TNIL);
|
||||
for (;;) { /* correct `firstfree' */
|
||||
if (ttype(&t->firstfree->key) == TAG_NIL)
|
||||
return &mp->val; /* OK; table still has a free place */
|
||||
if (ttype(key(t->firstfree)) == LUA_TNIL)
|
||||
return val(mp); /* OK; table still has a free place */
|
||||
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
||||
else (t->firstfree)--;
|
||||
}
|
||||
@@ -279,25 +210,93 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_setint (lua_State *L, Hash *t, int key) {
|
||||
TObject index;
|
||||
ttype(&index) = TAG_NUMBER;
|
||||
nvalue(&index) = key;
|
||||
return luaH_set(L, t, &index);
|
||||
/*
|
||||
** generic search function
|
||||
*/
|
||||
static const TObject *luaH_getany (Hash *t, const TObject *key) {
|
||||
if (ttype(key) == LUA_TNIL) return &luaO_nilobject;
|
||||
else {
|
||||
Node *n = luaH_mainposition(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (luaO_equalObj(key(n), key)) return val(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
|
||||
TObject *value, index;
|
||||
ttype(&index) = TAG_STRING;
|
||||
tsvalue(&index) = key;
|
||||
value = luaH_set(L, t, &index);
|
||||
ttype(value) = TAG_NUMBER;
|
||||
nvalue(value) = val;
|
||||
/*
|
||||
** search function for integers
|
||||
*/
|
||||
const TObject *luaH_getnum (Hash *t, int key) {
|
||||
Node *n = hashnum(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == (lua_Number)key)
|
||||
return val(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
const TObject *luaH_getglobal (lua_State *L, const char *name) {
|
||||
return luaH_getstr(L->gt, luaS_new(L, name));
|
||||
/*
|
||||
** search function for strings
|
||||
*/
|
||||
const TObject *luaH_getstr (Hash *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key)
|
||||
return val(n); /* that's it */
|
||||
else n = n->next;
|
||||
} while (n);
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** main search function
|
||||
*/
|
||||
const TObject *luaH_get (Hash *t, const TObject *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
|
||||
case LUA_TNUMBER: {
|
||||
int k = (int)nvalue(key);
|
||||
if ((lua_Number)k == nvalue(key)) /* is an integer index? */
|
||||
return luaH_getnum(t, k); /* use specialized version */
|
||||
/* else go through */
|
||||
}
|
||||
default: return luaH_getany(t, key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||
const TObject *p = luaH_get(t, key);
|
||||
if (p != &luaO_nilobject) return (TObject *)p;
|
||||
else if (ttype(key) == LUA_TNIL) luaD_error(L, l_s("table index is nil"));
|
||||
return newkey(L, t, key);
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
|
||||
const TObject *p = luaH_getstr(t, key);
|
||||
if (p != &luaO_nilobject) return (TObject *)p;
|
||||
else {
|
||||
TObject k;
|
||||
setsvalue(&k, key);
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_setnum (lua_State *L, Hash *t, int key) {
|
||||
const TObject *p = luaH_getnum(t, key);
|
||||
if (p != &luaO_nilobject) return (TObject *)p;
|
||||
else {
|
||||
TObject k;
|
||||
setnvalue(&k, key);
|
||||
return newkey(L, t, &k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
ltable.h
26
ltable.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.23 2000/06/06 16:31:41 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 1.33 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,22 +10,20 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define key(n) (&(n)->key)
|
||||
#define val(n) (&(n)->val)
|
||||
#define node(_t,_i) (&(_t)->node[_i])
|
||||
#define key(_n) (&(_n)->key)
|
||||
#define val(_n) (&(_n)->val)
|
||||
|
||||
const TObject *luaH_getnum (Hash *t, int key);
|
||||
TObject *luaH_setnum (lua_State *L, Hash *t, int key);
|
||||
const TObject *luaH_getstr (Hash *t, TString *key);
|
||||
TObject *luaH_setstr (lua_State *L, Hash *t, TString *key);
|
||||
const TObject *luaH_get (Hash *t, const TObject *key);
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key);
|
||||
Hash *luaH_new (lua_State *L, int nhash);
|
||||
void luaH_free (lua_State *L, Hash *t);
|
||||
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key);
|
||||
const TObject *luaH_getnum (const Hash *t, Number key);
|
||||
const TObject *luaH_getstr (const Hash *t, TString *key);
|
||||
void luaH_remove (Hash *t, TObject *key);
|
||||
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key);
|
||||
Node * luaH_next (lua_State *L, const Hash *t, const TObject *r);
|
||||
TObject *luaH_setint (lua_State *L, Hash *t, int key);
|
||||
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val);
|
||||
unsigned long luaH_hash (lua_State *L, const TObject *key);
|
||||
const TObject *luaH_getglobal (lua_State *L, const char *name);
|
||||
Node *luaH_next (lua_State *L, Hash *t, const TObject *r);
|
||||
int luaH_nexti (Hash *t, int i);
|
||||
|
||||
/* exported only for debugging */
|
||||
Node *luaH_mainposition (const Hash *t, const TObject *key);
|
||||
|
||||
510
ltests.c
510
ltests.c
@@ -1,16 +1,18 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 1.41 2000/09/11 19:42:57 roberto Exp roberto $
|
||||
** $Id: ltests.c,v 1.87 2001/07/05 20:31:14 roberto Exp roberto $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -28,23 +30,108 @@
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
lua_State *lua_state = NULL;
|
||||
|
||||
int islocked = 0;
|
||||
|
||||
|
||||
|
||||
static void setnameval (lua_State *L, const char *name, int val) {
|
||||
static void setnameval (lua_State *L, const l_char *name, int val) {
|
||||
lua_pushstring(L, name);
|
||||
lua_pushnumber(L, val);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** Controlled version for realloc.
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
|
||||
/* ensures maximum alignment for HEADER */
|
||||
#define HEADER (sizeof(union L_Umaxalign))
|
||||
|
||||
#define MARKSIZE 32
|
||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||
|
||||
|
||||
#define blocksize(b) ((size_t *)((l_char *)(b) - HEADER))
|
||||
|
||||
unsigned long memdebug_numblocks = 0;
|
||||
unsigned long memdebug_total = 0;
|
||||
unsigned long memdebug_maxmem = 0;
|
||||
unsigned long memdebug_memlimit = ULONG_MAX;
|
||||
|
||||
|
||||
static void *checkblock (void *block) {
|
||||
size_t *b = blocksize(block);
|
||||
size_t size = *b;
|
||||
int i;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
lua_assert(*(((l_char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
static void freeblock (void *block) {
|
||||
if (block) {
|
||||
size_t size = *blocksize(block);
|
||||
block = checkblock(block);
|
||||
memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
|
||||
free(block); /* free original block */
|
||||
memdebug_numblocks--;
|
||||
memdebug_total -= size;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *debug_realloc (void *block, size_t oldsize, size_t size) {
|
||||
lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
|
||||
if (size == 0) {
|
||||
freeblock(block);
|
||||
return NULL;
|
||||
}
|
||||
else if (memdebug_total+size-oldsize > memdebug_memlimit)
|
||||
return NULL; /* to test memory allocation errors */
|
||||
else {
|
||||
l_char *newblock;
|
||||
int i;
|
||||
size_t realsize = HEADER+size+MARKSIZE;
|
||||
if (realsize < size) return NULL; /* overflow! */
|
||||
newblock = (l_char *)malloc(realsize); /* alloc a new block */
|
||||
if (newblock == NULL) return NULL;
|
||||
if (oldsize > size) oldsize = size;
|
||||
if (block) {
|
||||
memcpy(newblock+HEADER, block, oldsize);
|
||||
freeblock(block); /* erase (and check) old copy */
|
||||
}
|
||||
/* initialize new part of the block with something `weird' */
|
||||
memset(newblock+HEADER+oldsize, -MARK, size-oldsize);
|
||||
memdebug_total += size;
|
||||
if (memdebug_total > memdebug_maxmem)
|
||||
memdebug_maxmem = memdebug_total;
|
||||
memdebug_numblocks++;
|
||||
*(size_t *)newblock = size;
|
||||
for (i=0;i<MARKSIZE;i++)
|
||||
*(newblock+HEADER+size+i) = (l_char)(MARK+i);
|
||||
return newblock+HEADER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Disassembler
|
||||
@@ -52,71 +139,56 @@ static void setnameval (lua_State *L, const char *name, int val) {
|
||||
*/
|
||||
|
||||
|
||||
static const char *const instrname[NUM_OPCODES] = {
|
||||
"END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
|
||||
"PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
|
||||
"GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
|
||||
"CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
|
||||
"ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
|
||||
"JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
|
||||
"JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
|
||||
"LFORLOOP", "CLOSURE"
|
||||
};
|
||||
|
||||
|
||||
static int pushop (lua_State *L, Proto *p, int pc) {
|
||||
char buff[100];
|
||||
static l_char *buildop (Proto *p, int pc, l_char *buff) {
|
||||
Instruction i = p->code[pc];
|
||||
OpCode o = GET_OPCODE(i);
|
||||
const char *name = instrname[o];
|
||||
sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
|
||||
switch ((enum Mode)luaK_opproperties[o].mode) {
|
||||
case iO:
|
||||
sprintf(buff+8, "%s", name);
|
||||
const l_char *name = luaP_opnames[o];
|
||||
sprintf(buff, l_s("%4d - "), pc);
|
||||
switch (getOpMode(o)) {
|
||||
case iABC:
|
||||
sprintf(buff+strlen(buff), l_s("%-12s%4d %4d %4d"), name,
|
||||
GETARG_A(i), GETARG_B(i), GETARG_C(i));
|
||||
break;
|
||||
case iU:
|
||||
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
|
||||
case iABc:
|
||||
sprintf(buff+strlen(buff), l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_Bc(i));
|
||||
break;
|
||||
case iS:
|
||||
sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
|
||||
break;
|
||||
case iAB:
|
||||
sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
|
||||
case iAsBc:
|
||||
sprintf(buff+strlen(buff), l_s("%-12s%4d %4d"), name, GETARG_A(i), GETARG_sBc(i));
|
||||
break;
|
||||
}
|
||||
lua_pushstring(L, buff);
|
||||
return (o != OP_END);
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
static int listcode (lua_State *L) {
|
||||
int pc;
|
||||
Proto *p;
|
||||
int res;
|
||||
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, l_s("Lua function expected"));
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
lua_newtable(L);
|
||||
setnameval(L, "maxstack", p->maxstacksize);
|
||||
setnameval(L, "numparams", p->numparams);
|
||||
pc = 0;
|
||||
do {
|
||||
setnameval(L, l_s("maxstack"), p->maxstacksize);
|
||||
setnameval(L, l_s("numparams"), p->numparams);
|
||||
for (pc=0; pc<p->sizecode; pc++) {
|
||||
l_char buff[100];
|
||||
lua_pushnumber(L, pc+1);
|
||||
res = pushop(L, p, pc++);
|
||||
lua_pushstring(L, buildop(p, pc, buff));
|
||||
lua_settable(L, -3);
|
||||
} while (res);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int liststrings (lua_State *L) {
|
||||
static int listk (lua_State *L) {
|
||||
Proto *p;
|
||||
int i;
|
||||
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, l_s("Lua function expected"));
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
lua_newtable(L);
|
||||
for (i=0; i<p->nkstr; i++) {
|
||||
for (i=0; i<p->sizek; i++) {
|
||||
lua_pushnumber(L, i+1);
|
||||
lua_pushstring(L, p->kstr[i]->str);
|
||||
luaA_pushobject(L, p->k+i);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
@@ -127,8 +199,9 @@ static int listlocals (lua_State *L) {
|
||||
Proto *p;
|
||||
int pc = luaL_check_int(L, 2) - 1;
|
||||
int i = 0;
|
||||
const char *name;
|
||||
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
|
||||
const l_char *name;
|
||||
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
1, l_s("Lua function expected"));
|
||||
p = clvalue(luaA_index(L, 1))->f.l;
|
||||
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
||||
lua_pushstring(L, name);
|
||||
@@ -138,25 +211,22 @@ static int listlocals (lua_State *L) {
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static int pushbool (lua_State *L, int b) {
|
||||
if (b) lua_pushnumber(L, 1);
|
||||
else lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int get_limits (lua_State *L) {
|
||||
lua_newtable(L);
|
||||
setnameval(L, "BITS_INT", BITS_INT);
|
||||
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||
setnameval(L, "MAXARG_A", MAXARG_A);
|
||||
setnameval(L, "MAXARG_B", MAXARG_B);
|
||||
setnameval(L, "MAXARG_S", MAXARG_S);
|
||||
setnameval(L, "MAXARG_U", MAXARG_U);
|
||||
setnameval(L, "MAXLOCALS", MAXLOCALS);
|
||||
setnameval(L, "MAXPARAMS", MAXPARAMS);
|
||||
setnameval(L, "MAXSTACK", MAXSTACK);
|
||||
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
||||
setnameval(L, "MAXVARSLH", MAXVARSLH);
|
||||
setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
|
||||
setnameval(L, "SIZE_A", SIZE_A);
|
||||
setnameval(L, "SIZE_B", SIZE_B);
|
||||
setnameval(L, "SIZE_OP", SIZE_OP);
|
||||
setnameval(L, "SIZE_U", SIZE_U);
|
||||
setnameval(L, l_s("BITS_INT"), BITS_INT);
|
||||
setnameval(L, l_s("LFPF"), LFIELDS_PER_FLUSH);
|
||||
setnameval(L, l_s("MAXLOCALS"), MAXLOCALS);
|
||||
setnameval(L, l_s("MAXPARAMS"), MAXPARAMS);
|
||||
setnameval(L, l_s("MAXSTACK"), MAXSTACK);
|
||||
setnameval(L, l_s("MAXUPVALUES"), MAXUPVALUES);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -177,14 +247,15 @@ static int mem_query (lua_State *L) {
|
||||
|
||||
static int hash_query (lua_State *L) {
|
||||
if (lua_isnull(L, 2)) {
|
||||
luaL_arg_check(L, lua_tag(L, 1) == TAG_STRING, 1, "string expected");
|
||||
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
|
||||
luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, l_s("string expected"));
|
||||
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->tsv.hash);
|
||||
}
|
||||
else {
|
||||
TObject *o = luaA_index(L, 1);
|
||||
Hash *t;
|
||||
luaL_checktype(L, 2, "table");
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 2));
|
||||
lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
|
||||
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -193,7 +264,7 @@ static int hash_query (lua_State *L) {
|
||||
static int table_query (lua_State *L) {
|
||||
const Hash *t;
|
||||
int i = luaL_opt_int(L, 2, -1);
|
||||
luaL_checktype(L, 1, "table");
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
t = hvalue(luaA_index(L, 1));
|
||||
if (i == -1) {
|
||||
lua_pushnumber(L, t->size);
|
||||
@@ -201,7 +272,13 @@ static int table_query (lua_State *L) {
|
||||
return 2;
|
||||
}
|
||||
else if (i < t->size) {
|
||||
luaA_pushobject(L, &t->node[i].key);
|
||||
if (ttype(val(node(t, i))) != LUA_TNIL ||
|
||||
ttype(key(node(t, i))) == LUA_TNIL ||
|
||||
ttype(key(node(t, i))) == LUA_TNUMBER) {
|
||||
luaA_pushobject(L, key(node(t, i)));
|
||||
}
|
||||
else
|
||||
lua_pushstring(L, "<undef>");
|
||||
luaA_pushobject(L, &t->node[i].val);
|
||||
if (t->node[i].next) {
|
||||
lua_pushnumber(L, t->node[i].next - t->node);
|
||||
@@ -215,7 +292,7 @@ static int table_query (lua_State *L) {
|
||||
|
||||
|
||||
static int string_query (lua_State *L) {
|
||||
stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
|
||||
stringtable *tb = &G(L)->strt;
|
||||
int s = luaL_opt_int(L, 2, 0) - 1;
|
||||
if (s==-1) {
|
||||
lua_pushnumber(L ,tb->nuse);
|
||||
@@ -225,9 +302,8 @@ static int string_query (lua_State *L) {
|
||||
else if (s < tb->size) {
|
||||
TString *ts;
|
||||
int n = 0;
|
||||
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
|
||||
ttype(L->top) = TAG_STRING;
|
||||
tsvalue(L->top) = ts;
|
||||
for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
|
||||
setsvalue(L->top, ts);
|
||||
incr_top;
|
||||
n++;
|
||||
}
|
||||
@@ -238,69 +314,120 @@ static int string_query (lua_State *L) {
|
||||
|
||||
|
||||
static int tref (lua_State *L) {
|
||||
luaL_checktype(L, 1, "any");
|
||||
int level = lua_gettop(L);
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
|
||||
assert(lua_gettop(L) == level+1); /* +1 for result */
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getref (lua_State *L) {
|
||||
if (lua_getref(L, luaL_check_int(L, 1)))
|
||||
int level = lua_gettop(L);
|
||||
if (lua_getref(L, luaL_check_int(L, 1))) {
|
||||
assert(lua_gettop(L) == level+1);
|
||||
return 1;
|
||||
else
|
||||
}
|
||||
else {
|
||||
assert(lua_gettop(L) == level);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int unref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
lua_unref(L, luaL_check_int(L, 1));
|
||||
assert(lua_gettop(L) == level);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int newuserdata (lua_State *L) {
|
||||
lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
|
||||
size_t size = luaL_check_int(L, 1);
|
||||
l_char *p = (l_char *)lua_newuserdata(L, size);
|
||||
while (size--) *p++ = l_c('\0');
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newuserdatabox (lua_State *L) {
|
||||
lua_newuserdatabox(L, (void *)luaL_check_int(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int settag (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushvalue(L, 1); /* push value */
|
||||
lua_settag(L, luaL_check_int(L, 2));
|
||||
return 1; /* return value */
|
||||
}
|
||||
|
||||
static int udataval (lua_State *L) {
|
||||
luaL_checktype(L, 1, "userdata");
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
lua_pushnumber(L, (int)lua_touserdata(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int newtag (lua_State *L) {
|
||||
lua_pushnumber(L, lua_newtype(L, lua_tostring(L, 1),
|
||||
(int)lua_tonumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int doonnewstack (lua_State *L) {
|
||||
lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
|
||||
if (L1 == NULL) return 0;
|
||||
*((int **)L1) = &islocked; /* initialize the lock */
|
||||
lua_dostring(L1, luaL_check_string(L, 2));
|
||||
lua_pushnumber(L, 1);
|
||||
lua_close(L1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int s2d (lua_State *L) {
|
||||
lua_pushnumber(L, *(double *)luaL_check_string(L, 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int d2s (lua_State *L) {
|
||||
double d = luaL_check_number(L, 1);
|
||||
lua_pushlstring(L, (l_char *)&d, sizeof(d));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int newstate (lua_State *L) {
|
||||
lua_State *L1 = lua_open(luaL_check_int(L, 1));
|
||||
if (L1)
|
||||
lua_pushuserdata(L, L1);
|
||||
if (L1) {
|
||||
*((int **)L1) = &islocked; /* initialize the lock */
|
||||
lua_pushnumber(L, (unsigned long)L1);
|
||||
}
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int loadlib (lua_State *L) {
|
||||
lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
|
||||
switch (*luaL_check_string(L, 2)) {
|
||||
case 'm': lua_mathlibopen(L1); break;
|
||||
case 's': lua_strlibopen(L1); break;
|
||||
case 'i': lua_iolibopen(L1); break;
|
||||
case 'd': lua_dblibopen(L1); break;
|
||||
case 'b': lua_baselibopen(L1); break;
|
||||
default: luaL_argerror(L, 2, "invalid option");
|
||||
}
|
||||
lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
|
||||
lua_register(L1, "mathlibopen", lua_mathlibopen);
|
||||
lua_register(L1, "strlibopen", lua_strlibopen);
|
||||
lua_register(L1, "iolibopen", lua_iolibopen);
|
||||
lua_register(L1, "dblibopen", lua_dblibopen);
|
||||
lua_register(L1, "baselibopen", lua_baselibopen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int closestate (lua_State *L) {
|
||||
luaL_checktype(L, 1, "userdata");
|
||||
lua_close((lua_State *)lua_touserdata(L, 1));
|
||||
lua_State *L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
|
||||
lua_close(L1);
|
||||
lua_unlock(L); /* close cannot unlock that */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int doremote (lua_State *L) {
|
||||
lua_State *L1;
|
||||
const char *code = luaL_check_string(L, 2);
|
||||
const l_char *code = luaL_check_string(L, 2);
|
||||
int status;
|
||||
luaL_checktype(L, 1, "userdata");
|
||||
L1 = (lua_State *)lua_touserdata(L, 1);
|
||||
L1 = (lua_State *)(unsigned long)luaL_check_number(L, 1);
|
||||
status = lua_dostring(L1, code);
|
||||
if (status != 0) {
|
||||
lua_pushnil(L);
|
||||
@@ -311,19 +438,18 @@ static int doremote (lua_State *L) {
|
||||
int i = 0;
|
||||
while (!lua_isnull(L1, ++i))
|
||||
lua_pushstring(L, lua_tostring(L1, i));
|
||||
lua_pop(L1, i-1);
|
||||
return i-1;
|
||||
}
|
||||
}
|
||||
|
||||
static int settagmethod (lua_State *L) {
|
||||
luaL_checktype(L, 3, "any");
|
||||
lua_settagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int pushbool (lua_State *L, int b) {
|
||||
if (b) lua_pushnumber(L, 1);
|
||||
else lua_pushnil(L);
|
||||
int tag = luaL_check_int(L, 1);
|
||||
const l_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;
|
||||
}
|
||||
|
||||
@@ -335,41 +461,41 @@ static int equal (lua_State *L) {
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** function to test the API with C. It interprets a kind of "assembler"
|
||||
** function to test the API with C. It interprets a kind of assembler
|
||||
** language with calls to the API, so the test can be driven by Lua code
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static const char *const delimits = " \t\n,;";
|
||||
static const l_char *const delimits = l_s(" \t\n,;");
|
||||
|
||||
static void skip (const char **pc) {
|
||||
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
||||
static void skip (const l_char **pc) {
|
||||
while (**pc != l_c('\0') && strchr(delimits, **pc)) (*pc)++;
|
||||
}
|
||||
|
||||
static int getnum (lua_State *L, const char **pc) {
|
||||
static int getnum (lua_State *L, const l_char **pc) {
|
||||
int res = 0;
|
||||
int sig = 1;
|
||||
skip(pc);
|
||||
if (**pc == '.') {
|
||||
if (**pc == l_c('.')) {
|
||||
res = (int)lua_tonumber(L, -1);
|
||||
lua_pop(L, 1);
|
||||
(*pc)++;
|
||||
return res;
|
||||
}
|
||||
else if (**pc == '-') {
|
||||
else if (**pc == l_c('-')) {
|
||||
sig = -1;
|
||||
(*pc)++;
|
||||
}
|
||||
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
|
||||
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - l_c('0');
|
||||
return sig*res;
|
||||
}
|
||||
|
||||
static const char *getname (char *buff, const char **pc) {
|
||||
static const l_char *getname (l_char *buff, const l_char **pc) {
|
||||
int i = 0;
|
||||
skip(pc);
|
||||
while (**pc != '\0' && !strchr(delimits, **pc))
|
||||
while (**pc != l_c('\0') && !strchr(delimits, **pc))
|
||||
buff[i++] = *(*pc)++;
|
||||
buff[i] = '\0';
|
||||
buff[i] = l_c('\0');
|
||||
return buff;
|
||||
}
|
||||
|
||||
@@ -381,50 +507,121 @@ static const char *getname (char *buff, const char **pc) {
|
||||
|
||||
|
||||
static int testC (lua_State *L) {
|
||||
char buff[30];
|
||||
const char *pc = luaL_check_string(L, 1);
|
||||
l_char buff[30];
|
||||
const l_char *pc = luaL_check_string(L, 1);
|
||||
for (;;) {
|
||||
const char *inst = getname;
|
||||
if EQ("") return 0;
|
||||
else if EQ("return") {
|
||||
const l_char *inst = getname;
|
||||
if EQ(l_s("")) return 0;
|
||||
else if EQ(l_s("isnumber")) {
|
||||
lua_pushnumber(L, lua_isnumber(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isstring")) {
|
||||
lua_pushnumber(L, lua_isstring(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("istable")) {
|
||||
lua_pushnumber(L, lua_istable(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("iscfunction")) {
|
||||
lua_pushnumber(L, lua_iscfunction(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isfunction")) {
|
||||
lua_pushnumber(L, lua_isfunction(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isuserdata")) {
|
||||
lua_pushnumber(L, lua_isuserdata(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isnil")) {
|
||||
lua_pushnumber(L, lua_isnil(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("isnull")) {
|
||||
lua_pushnumber(L, lua_isnull(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("tonumber")) {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("tostring")) {
|
||||
const l_char *s = lua_tostring(L, getnum);
|
||||
lua_pushstring(L, s);
|
||||
}
|
||||
else if EQ(l_s("tonumber")) {
|
||||
lua_pushnumber(L, lua_tonumber(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("strlen")) {
|
||||
lua_pushnumber(L, lua_strlen(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("tocfunction")) {
|
||||
lua_pushcfunction(L, lua_tocfunction(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("return")) {
|
||||
return getnum;
|
||||
}
|
||||
else if EQ("gettop") {
|
||||
else if EQ(l_s("gettop")) {
|
||||
lua_pushnumber(L, lua_gettop(L));
|
||||
}
|
||||
else if EQ("settop") {
|
||||
else if EQ(l_s("settop")) {
|
||||
lua_settop(L, getnum);
|
||||
}
|
||||
else if EQ("pop") {
|
||||
else if EQ(l_s("pop")) {
|
||||
lua_pop(L, getnum);
|
||||
}
|
||||
else if EQ("pushnum") {
|
||||
else if EQ(l_s("pushnum")) {
|
||||
lua_pushnumber(L, getnum);
|
||||
}
|
||||
else if EQ("pushvalue") {
|
||||
else if EQ(l_s("pushvalue")) {
|
||||
lua_pushvalue(L, getnum);
|
||||
}
|
||||
else if EQ("remove") {
|
||||
else if EQ(l_s("remove")) {
|
||||
lua_remove(L, getnum);
|
||||
}
|
||||
else if EQ("insert") {
|
||||
else if EQ(l_s("insert")) {
|
||||
lua_insert(L, getnum);
|
||||
}
|
||||
else if EQ("next") {
|
||||
else if EQ(l_s("gettable")) {
|
||||
lua_gettable(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("settable")) {
|
||||
lua_settable(L, getnum);
|
||||
}
|
||||
else if EQ(l_s("next")) {
|
||||
lua_next(L, -2);
|
||||
}
|
||||
else if EQ("concat") {
|
||||
else if EQ(l_s("concat")) {
|
||||
lua_concat(L, getnum);
|
||||
}
|
||||
else if EQ("call") {
|
||||
else if EQ(l_s("lessthan")) {
|
||||
int a = getnum;
|
||||
if (lua_lessthan(L, a, getnum))
|
||||
lua_pushnumber(L, 1);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else if EQ(l_s("rawcall")) {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_rawcall(L, narg, nres);
|
||||
}
|
||||
else if EQ("type") {
|
||||
else if EQ(l_s("call")) {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
lua_call(L, narg, nres);
|
||||
}
|
||||
else if EQ(l_s("dostring")) {
|
||||
lua_dostring(L, luaL_check_string(L, getnum));
|
||||
}
|
||||
else if EQ(l_s("settagmethod")) {
|
||||
int tag = getnum;
|
||||
const l_char *event = getname;
|
||||
lua_settagmethod(L, tag, event);
|
||||
}
|
||||
else if EQ(l_s("gettagmethod")) {
|
||||
int tag = getnum;
|
||||
const l_char *event = getname;
|
||||
lua_gettagmethod(L, tag, event);
|
||||
}
|
||||
else if EQ(l_s("type")) {
|
||||
lua_pushstring(L, lua_type(L, getnum));
|
||||
}
|
||||
else luaL_verror(L, "unknown instruction %.30s", buff);
|
||||
else luaL_verror(L, l_s("unknown instruction %.30s"), buff);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -434,37 +631,46 @@ static int testC (lua_State *L) {
|
||||
|
||||
|
||||
static const struct luaL_reg tests_funcs[] = {
|
||||
{"hash", hash_query},
|
||||
{"limits", get_limits},
|
||||
{"listcode", listcode},
|
||||
{"liststrings", liststrings},
|
||||
{"listlocals", listlocals},
|
||||
{"loadlib", loadlib},
|
||||
{"querystr", string_query},
|
||||
{"querytab", table_query},
|
||||
{"testC", testC},
|
||||
{"ref", tref},
|
||||
{"getref", getref},
|
||||
{"unref", unref},
|
||||
{"newuserdata", newuserdata},
|
||||
{"udataval", udataval},
|
||||
{"newstate", newstate},
|
||||
{"closestate", closestate},
|
||||
{"doremote", doremote},
|
||||
{"settagmethod", settagmethod},
|
||||
{"equal", equal},
|
||||
{"totalmem", mem_query}
|
||||
{l_s("hash"), hash_query},
|
||||
{l_s("limits"), get_limits},
|
||||
{l_s("listcode"), listcode},
|
||||
{l_s("listk"), listk},
|
||||
{l_s("listlocals"), listlocals},
|
||||
{l_s("loadlib"), loadlib},
|
||||
{l_s("querystr"), string_query},
|
||||
{l_s("querytab"), table_query},
|
||||
{l_s("testC"), testC},
|
||||
{l_s("ref"), tref},
|
||||
{l_s("getref"), getref},
|
||||
{l_s("unref"), unref},
|
||||
{l_s("d2s"), d2s},
|
||||
{l_s("s2d"), s2d},
|
||||
{l_s("newuserdata"), newuserdata},
|
||||
{l_s("newuserdatabox"), newuserdatabox},
|
||||
{l_s("settag"), settag},
|
||||
{l_s("udataval"), udataval},
|
||||
{l_s("newtag"), newtag},
|
||||
{l_s("doonnewstack"), doonnewstack},
|
||||
{l_s("newstate"), newstate},
|
||||
{l_s("closestate"), closestate},
|
||||
{l_s("doremote"), doremote},
|
||||
{l_s("settagmethod"), settagmethod},
|
||||
{l_s("equal"), equal},
|
||||
{l_s("totalmem"), mem_query}
|
||||
};
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L) {
|
||||
*((int **)L) = &islocked; /* init lock */
|
||||
lua_state = L; /* keep first state to be opened */
|
||||
/* open lib in a new table */
|
||||
lua_newtable(L);
|
||||
lua_getglobals(L);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_setglobals(L);
|
||||
luaL_openl(L, tests_funcs); /* open functions inside new table */
|
||||
lua_setglobals(L); /* restore old table of globals */
|
||||
lua_setglobal(L, "T"); /* set new table as global T */
|
||||
lua_setglobal(L, l_s("T")); /* set new table as global T */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
58
ltests.h
Normal file
58
ltests.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
** $Id: ltests.h,v 1.6 2001/03/02 17:27:50 roberto Exp roberto $
|
||||
** Internal Header for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ltests_h
|
||||
#define ltests_h
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define LUA_DEBUG
|
||||
|
||||
#define LUA_OPNAMES
|
||||
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define lua_assert(c) assert(c)
|
||||
#define api_check(L, o) lua_assert(o)
|
||||
|
||||
|
||||
/* to avoid warnings, and to make sure value is really unused */
|
||||
#define UNUSED(x) (x=0, (void)(x))
|
||||
|
||||
|
||||
/* memory allocator control variables */
|
||||
extern unsigned long memdebug_numblocks;
|
||||
extern unsigned long memdebug_total;
|
||||
extern unsigned long memdebug_maxmem;
|
||||
extern unsigned long memdebug_memlimit;
|
||||
|
||||
|
||||
#define l_malloc(s) debug_realloc(NULL, 0, s)
|
||||
#define l_realloc(b, os, s) debug_realloc(b, os, s)
|
||||
#define l_free(b, s) debug_realloc(b, s, 0)
|
||||
|
||||
void *debug_realloc (void *block, size_t oldsize, size_t size);
|
||||
|
||||
|
||||
|
||||
/* test for lock/unlock */
|
||||
#define LUA_USERSTATE int *lock;
|
||||
extern int islocked;
|
||||
#define lua_lock(L) lua_assert((**((int **)L))++ == 0)
|
||||
#define lua_unlock(L) lua_assert(--(**((int **)L)) == 0)
|
||||
|
||||
|
||||
extern lua_State *lua_state;
|
||||
|
||||
|
||||
void luaB_opentests (lua_State *L);
|
||||
|
||||
#define LUA_USERINIT(L) (luaB_opentests(L), openstdlibs(L))
|
||||
|
||||
|
||||
#endif
|
||||
197
ltm.c
197
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.75 2001/07/23 19:56:00 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -8,24 +8,28 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
const char *const luaT_eventname[] = { /* ORDER IM */
|
||||
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
|
||||
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
|
||||
"le", "gt", "ge", /* deprecated options!! */
|
||||
const l_char *const luaT_eventname[] = { /* ORDER TM */
|
||||
l_s("gettable"), l_s("settable"), l_s("index"), l_s("getglobal"),
|
||||
l_s("setglobal"), l_s("add"), l_s("sub"), l_s("mul"), l_s("div"),
|
||||
l_s("pow"), l_s("unm"), l_s("lt"), l_s("concat"), l_s("gc"),
|
||||
l_s("function"),
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
static int findevent (const char *name) {
|
||||
static int findevent (const l_char *name) {
|
||||
int i;
|
||||
for (i=0; luaT_eventname[i]; i++)
|
||||
if (strcmp(luaT_eventname[i], name) == 0)
|
||||
@@ -34,126 +38,155 @@ 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 l_char *name) {
|
||||
int e = findevent(name);
|
||||
if (e >= IM_N)
|
||||
luaO_verror(L, "event `%.50s' is deprecated", name);
|
||||
if (e == IM_GC && t == TAG_TABLE)
|
||||
luaO_verror(L, "event `gc' for tables is deprecated");
|
||||
if (e < 0)
|
||||
luaO_verror(L, "`%.50s' is not a valid event name", name);
|
||||
luaO_verror(L, l_s("`%.50s' is not a valid event name"), name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* events in TAG_NIL are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
/* events in LUA_TNIL are all allowed, since this is used as a
|
||||
* `placeholder' for default fallbacks
|
||||
*/
|
||||
/* ORDER LUA_T, ORDER IM */
|
||||
static const char luaT_validevents[NUM_TAGS][IM_N] = {
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
|
||||
/* ORDER LUA_T, ORDER TM */
|
||||
static const lu_byte luaT_validevents[NUM_TAGS][TM_N] = {
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TTABLE */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
|
||||
};
|
||||
|
||||
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
|
||||
}
|
||||
|
||||
|
||||
static void init_entry (lua_State *L, int tag) {
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
ttype(luaT_getim(L, tag, i)) = TAG_NIL;
|
||||
L->IMtable[tag].collected = NULL;
|
||||
return (t >= NUM_TAGS) ? 1 : (int)luaT_validevents[t][e];
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (lua_State *L) {
|
||||
int t;
|
||||
luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
|
||||
L->last_tag = NUM_TAGS-1;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
init_entry(L, t);
|
||||
static const l_char *const typenames[NUM_TAGS] = {
|
||||
l_s("userdata"), l_s("nil"), l_s("number"), l_s("string"),
|
||||
l_s("table"), l_s("function")
|
||||
};
|
||||
int i;
|
||||
for (i=0; i<NUM_TAGS; i++)
|
||||
luaT_newtag(L, typenames[i], i);
|
||||
}
|
||||
|
||||
|
||||
int lua_newtag (lua_State *L) {
|
||||
luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
|
||||
"tag table overflow", MAX_INT);
|
||||
L->last_tag++;
|
||||
init_entry(L, L->last_tag);
|
||||
return L->last_tag;
|
||||
int luaT_newtag (lua_State *L, const l_char *name, int basictype) {
|
||||
int tag;
|
||||
int i;
|
||||
TString *ts = NULL;
|
||||
luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM,
|
||||
MAX_INT, l_s("tag table overflow"));
|
||||
tag = G(L)->ntag;
|
||||
if (name) {
|
||||
TObject *v;
|
||||
ts = luaS_new(L, name);
|
||||
v = luaH_setstr(L, G(L)->type2tag, ts);
|
||||
if (ttype(v) == LUA_TNUMBER) return (int)nvalue(v);
|
||||
setnvalue(v, tag);
|
||||
}
|
||||
for (i=0; i<TM_N; i++)
|
||||
luaT_gettm(G(L), tag, i) = NULL;
|
||||
G(L)->TMtable[tag].collected = NULL;
|
||||
G(L)->TMtable[tag].name = ts;
|
||||
G(L)->TMtable[tag].basictype = basictype;
|
||||
G(L)->ntag++;
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
static void checktag (lua_State *L, int tag) {
|
||||
if (!(0 <= tag && tag <= L->last_tag))
|
||||
luaO_verror(L, "%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaT_realtag (lua_State *L, int tag) {
|
||||
if (!(NUM_TAGS <= tag && tag <= L->last_tag))
|
||||
luaO_verror(L, "tag %d was not created by `newtag'", tag);
|
||||
if (!(0 <= tag && tag < G(L)->ntag))
|
||||
luaO_verror(L, l_s("%d is not a valid tag"), 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;
|
||||
lua_lock(L);
|
||||
checktag(L, tagto);
|
||||
checktag(L, tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
for (e=0; e<TM_N; e++) {
|
||||
if (luaT_validevent(tagto, e))
|
||||
*luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
|
||||
luaT_gettm(G(L), tagto, e) = luaT_gettm(G(L), tagfrom, e);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_effectivetag (lua_State *L, const TObject *o) {
|
||||
lua_Type t = ttype(o);
|
||||
int luaT_tag (const TObject *o) {
|
||||
int t = ttype(o);
|
||||
switch (t) {
|
||||
case TAG_USERDATA: {
|
||||
int tag = tsvalue(o)->u.d.tag;
|
||||
return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
|
||||
}
|
||||
case TAG_TABLE: return hvalue(o)->htag;
|
||||
default: return t;
|
||||
case LUA_TUSERDATA: return uvalue(o)->uv.tag;
|
||||
case LUA_TTABLE: return hvalue(o)->htag;
|
||||
default: return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lua_gettagmethod (lua_State *L, int t, const char *event) {
|
||||
int e;
|
||||
e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (luaT_validevent(t, e))
|
||||
*L->top = *luaT_getim(L, t,e);
|
||||
else
|
||||
ttype(L->top) = TAG_NIL;
|
||||
incr_top;
|
||||
const l_char *luaT_typename (global_State *G, const TObject *o) {
|
||||
int t = ttype(o);
|
||||
int tag;
|
||||
TString *ts;
|
||||
switch (t) {
|
||||
case LUA_TUSERDATA:
|
||||
tag = uvalue(o)->uv.tag;
|
||||
break;
|
||||
case LUA_TTABLE:
|
||||
tag = hvalue(o)->htag;
|
||||
break;
|
||||
default:
|
||||
tag = t;
|
||||
}
|
||||
ts = G->TMtable[tag].name;
|
||||
if (ts == NULL)
|
||||
ts = G->TMtable[t].name;
|
||||
return getstr(ts);
|
||||
}
|
||||
|
||||
|
||||
void lua_settagmethod (lua_State *L, int t, const char *event) {
|
||||
TObject temp;
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int t, const l_char *event) {
|
||||
int e;
|
||||
LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
|
||||
"function or nil expected");
|
||||
e = luaI_checkevent(L, event, t);
|
||||
lua_lock(L);
|
||||
e = luaI_checkevent(L, event);
|
||||
checktag(L, t);
|
||||
if (luaT_validevent(t, e) && luaT_gettm(G(L), t, e)) {
|
||||
setclvalue(L->top, luaT_gettm(G(L), t, e));
|
||||
}
|
||||
else
|
||||
setnilvalue(L->top);
|
||||
incr_top;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settagmethod (lua_State *L, int t, const l_char *event) {
|
||||
int e;
|
||||
lua_lock(L);
|
||||
e = luaI_checkevent(L, event);
|
||||
checktag(L, t);
|
||||
if (!luaT_validevent(t, e))
|
||||
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||
luaT_eventname[e], luaO_typenames[t],
|
||||
(t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
|
||||
: "");
|
||||
temp = *(L->top - 1);
|
||||
*(L->top - 1) = *luaT_getim(L, t,e);
|
||||
*luaT_getim(L, t, e) = temp;
|
||||
luaO_verror(L, l_s("cannot change `%.20s' tag method for type `%.20s'%.20s"),
|
||||
luaT_eventname[e], typenamebytag(G(L), t),
|
||||
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
|
||||
l_s(" with default tag") : l_s(""));
|
||||
switch (ttype(L->top - 1)) {
|
||||
case LUA_TNIL:
|
||||
luaT_gettm(G(L), t, e) = NULL;
|
||||
break;
|
||||
case LUA_TFUNCTION:
|
||||
luaT_gettm(G(L), t, e) = clvalue(L->top - 1);
|
||||
break;
|
||||
default:
|
||||
luaD_error(L, l_s("tag method must be a function (or nil)"));
|
||||
}
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
78
ltm.h
78
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.25 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,43 +13,67 @@
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER IM"
|
||||
* grep "ORDER TM"
|
||||
*/
|
||||
typedef enum {
|
||||
IM_GETTABLE = 0,
|
||||
IM_SETTABLE,
|
||||
IM_INDEX,
|
||||
IM_GETGLOBAL,
|
||||
IM_SETGLOBAL,
|
||||
IM_ADD,
|
||||
IM_SUB,
|
||||
IM_MUL,
|
||||
IM_DIV,
|
||||
IM_POW,
|
||||
IM_UNM,
|
||||
IM_LT,
|
||||
IM_CONCAT,
|
||||
IM_GC,
|
||||
IM_FUNCTION,
|
||||
IM_N /* number of elements in the enum */
|
||||
} IMS;
|
||||
TM_GETTABLE = 0,
|
||||
TM_SETTABLE,
|
||||
TM_INDEX,
|
||||
TM_GETGLOBAL,
|
||||
TM_SETGLOBAL,
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
TM_DIV,
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LT,
|
||||
TM_CONCAT,
|
||||
TM_GC,
|
||||
TM_FUNCTION,
|
||||
TM_N /* number of elements in the enum */
|
||||
} TMS;
|
||||
|
||||
|
||||
struct IM {
|
||||
TObject int_method[IM_N];
|
||||
TString *collected; /* list of G. collected udata with this tag */
|
||||
|
||||
/*
|
||||
** masks for allowable tag methods
|
||||
** (see `luaT_validevents')
|
||||
*/
|
||||
#define HAS_TM_GETGLOBAL(L,t) (1<<(t) & ((1<<LUA_TUSERDATA) | \
|
||||
(1<<LUA_TTABLE) | \
|
||||
(1<<LUA_TNIL)))
|
||||
|
||||
#define HAS_TM_SETGLOBAL(L,t) (1<<(t) & ((1<<LUA_TUSERDATA) | \
|
||||
(1<<LUA_TTABLE) | \
|
||||
(1<<LUA_TNIL) | \
|
||||
(1<<LUA_TFUNCTION)))
|
||||
|
||||
|
||||
|
||||
struct TM {
|
||||
Closure *method[TM_N];
|
||||
Udata *collected; /* list of garbage-collected udata with this tag */
|
||||
TString *name; /* type name */
|
||||
int basictype;
|
||||
};
|
||||
|
||||
|
||||
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
|
||||
#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(L, o),(e)))
|
||||
#define luaT_gettm(G,tag,event) (G->TMtable[tag].method[event])
|
||||
#define luaT_gettmbyObj(G,o,e) (luaT_gettm((G),luaT_tag(o),(e)))
|
||||
|
||||
extern const char *const luaT_eventname[];
|
||||
#define typenamebytag(G, t) getstr(G->TMtable[t].name)
|
||||
|
||||
|
||||
#define validtag(G,t) (NUM_TAGS <= (t) && (t) < G->ntag)
|
||||
|
||||
extern const l_char *const luaT_eventname[];
|
||||
|
||||
|
||||
void luaT_init (lua_State *L);
|
||||
void luaT_realtag (lua_State *L, int tag);
|
||||
int luaT_effectivetag (lua_State *L, const TObject *o);
|
||||
int luaT_newtag (lua_State *L, const l_char *name, int basictype);
|
||||
const l_char *luaT_typename (global_State *G, const TObject *o);
|
||||
int luaT_tag (const TObject *o);
|
||||
int luaT_validevent (int t, int e); /* used by compatibility module */
|
||||
|
||||
|
||||
|
||||
220
lua.c
220
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.67 2001/06/06 18:00:19 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -10,20 +10,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
lua_State *lua_state = NULL;
|
||||
#define L lua_state
|
||||
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT "> "
|
||||
#endif
|
||||
|
||||
#ifdef _POSIX_SOURCE
|
||||
#include <unistd.h>
|
||||
#else
|
||||
@@ -31,6 +24,18 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef PROMPT
|
||||
#define PROMPT l_s("> ")
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef LUA_USERINIT
|
||||
#define LUA_USERINIT(L) openstdlibs(L)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** global options
|
||||
*/
|
||||
@@ -40,6 +45,9 @@ struct Options {
|
||||
};
|
||||
|
||||
|
||||
static lua_State *L = NULL;
|
||||
|
||||
|
||||
typedef void (*handler)(int); /* type for signal actions */
|
||||
|
||||
static void laction (int i);
|
||||
@@ -49,19 +57,6 @@ static lua_Hook old_linehook = NULL;
|
||||
static lua_Hook old_callhook = NULL;
|
||||
|
||||
|
||||
#ifdef USERINIT
|
||||
extern void USERINIT (void);
|
||||
#else
|
||||
#define USERINIT userinit
|
||||
static void userinit (void) {
|
||||
lua_baselibopen(L);
|
||||
lua_iolibopen(L);
|
||||
lua_strlibopen(L);
|
||||
lua_mathlibopen(L);
|
||||
lua_dblibopen(L);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static handler lreset (void) {
|
||||
return signal(SIGINT, laction);
|
||||
@@ -72,7 +67,7 @@ static void lstop (void) {
|
||||
lua_setlinehook(L, old_linehook);
|
||||
lua_setcallhook(L, old_callhook);
|
||||
lreset();
|
||||
lua_error(L, "interrupted!");
|
||||
lua_error(L, l_s("interrupted!"));
|
||||
}
|
||||
|
||||
|
||||
@@ -85,52 +80,54 @@ static void laction (int i) {
|
||||
}
|
||||
|
||||
|
||||
static int ldo (int (*f)(lua_State *l, const char *), const char *name) {
|
||||
static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name) {
|
||||
int res;
|
||||
handler h = lreset();
|
||||
int top = lua_gettop(L);
|
||||
res = f(L, name); /* dostring | dofile */
|
||||
lua_settop(L, top); /* remove eventual results */
|
||||
signal(SIGINT, h); /* restore old action */
|
||||
/* Lua gives no message in such cases, so lua.c provides one */
|
||||
if (res == LUA_ERRMEM) {
|
||||
/* Lua gives no message in such case, so lua.c provides one */
|
||||
fprintf(stderr, "lua: memory allocation error\n");
|
||||
fprintf(stderr, l_s("lua: memory allocation error\n"));
|
||||
}
|
||||
else if (res == LUA_ERRERR)
|
||||
fprintf(stderr, l_s("lua: error in error message\n"));
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static void print_message (void) {
|
||||
fprintf(stderr,
|
||||
"usage: lua [options]. Available options are:\n"
|
||||
" - execute stdin as a file\n"
|
||||
" -c close Lua when exiting\n"
|
||||
" -e stat execute string `stat'\n"
|
||||
" -f name execute file `name' with remaining arguments in table `arg'\n"
|
||||
" -i enter interactive mode with prompt\n"
|
||||
" -q enter interactive mode without prompt\n"
|
||||
" -sNUM set stack size to NUM (must be the first option)\n"
|
||||
" -v print version information\n"
|
||||
" a=b set global `a' to string `b'\n"
|
||||
" name execute file `name'\n"
|
||||
l_s("usage: lua [options]. Available options are:\n")
|
||||
l_s(" - execute stdin as a file\n")
|
||||
l_s(" -c close Lua when exiting\n")
|
||||
l_s(" -e stat execute string `stat'\n")
|
||||
l_s(" -f name execute file `name' with remaining arguments in table `arg'\n")
|
||||
l_s(" -i enter interactive mode with prompt\n")
|
||||
l_s(" -q enter interactive mode without prompt\n")
|
||||
l_s(" -sNUM set stack size to NUM (must be the first option)\n")
|
||||
l_s(" -v print version information\n")
|
||||
l_s(" a=b set global `a' to string `b'\n")
|
||||
l_s(" name execute file `name'\n")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
static void print_version (void) {
|
||||
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
printf(l_s("%.80s %.80s\n"), l_s(LUA_VERSION), l_s(LUA_COPYRIGHT));
|
||||
}
|
||||
|
||||
|
||||
static void assign (char *arg) {
|
||||
char *eq = strchr(arg, '=');
|
||||
*eq = '\0'; /* spilt `arg' in two strings (name & value) */
|
||||
static void assign (l_char *arg) {
|
||||
l_char *eq = strchr(arg, l_c('='));
|
||||
*eq = l_c('\0'); /* spilt `arg' in two strings (name & value) */
|
||||
lua_pushstring(L, eq+1);
|
||||
lua_setglobal(L, arg);
|
||||
}
|
||||
|
||||
|
||||
static void getargs (char *argv[]) {
|
||||
static void getargs (l_char *argv[]) {
|
||||
int i;
|
||||
lua_newtable(L);
|
||||
for (i=0; argv[i]; i++) {
|
||||
@@ -140,24 +137,24 @@ static void getargs (char *argv[]) {
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
/* arg.n = maximum index in table `arg' */
|
||||
lua_pushstring(L, "n");
|
||||
lua_pushliteral(L, l_s("n"));
|
||||
lua_pushnumber(L, i-1);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
|
||||
static int l_getargs (lua_State *l) {
|
||||
char **argv = (char **)lua_touserdata(l, -1);
|
||||
l_char **argv = (l_char **)lua_touserdata(l, -1);
|
||||
getargs(argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int file_input (const char *argv) {
|
||||
static int file_input (const l_char *argv) {
|
||||
int result = ldo(lua_dofile, argv);
|
||||
if (result) {
|
||||
if (result == LUA_ERRFILE) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
fprintf(stderr, l_s("lua: cannot execute file "));
|
||||
perror(argv);
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
@@ -167,51 +164,55 @@ static int file_input (const char *argv) {
|
||||
}
|
||||
|
||||
|
||||
/* maximum length of an input string */
|
||||
/* maximum length of an input line */
|
||||
#ifndef MAXINPUT
|
||||
#define MAXINPUT BUFSIZ
|
||||
#define MAXINPUT 512
|
||||
#endif
|
||||
|
||||
static void manual_input (int version, int prompt) {
|
||||
int cont = 1;
|
||||
if (version) print_version();
|
||||
while (cont) {
|
||||
char buffer[MAXINPUT];
|
||||
int i = 0;
|
||||
if (prompt) {
|
||||
const char *s;
|
||||
lua_getglobal(L, "_PROMPT");
|
||||
s = lua_tostring(L, -1);
|
||||
if (!s) s = PROMPT;
|
||||
fputs(s, stdout);
|
||||
lua_pop(L, 1); /* remove global */
|
||||
}
|
||||
for(;;) {
|
||||
int c = getchar();
|
||||
if (c == EOF) {
|
||||
cont = 0;
|
||||
break;
|
||||
}
|
||||
else if (c == '\n') {
|
||||
if (i>0 && buffer[i-1] == '\\')
|
||||
buffer[i-1] = '\n';
|
||||
else break;
|
||||
}
|
||||
else if (i >= MAXINPUT-1) {
|
||||
fprintf(stderr, "lua: input line too long\n");
|
||||
break;
|
||||
}
|
||||
else buffer[i++] = (char)c;
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
ldo(lua_dostring, buffer);
|
||||
lua_settop(L, 0); /* remove eventual results */
|
||||
|
||||
static const l_char *get_prompt (int prompt) {
|
||||
if (!prompt)
|
||||
return l_s("");
|
||||
else {
|
||||
const l_char *s;
|
||||
lua_getglobal(L, l_s("_PROMPT"));
|
||||
s = lua_tostring(L, -1);
|
||||
if (!s) s = PROMPT;
|
||||
lua_pop(L, 1); /* remove global */
|
||||
return s;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
static int handle_argv (char *argv[], struct Options *opt) {
|
||||
static void manual_input (int version, int prompt) {
|
||||
if (version) print_version();
|
||||
for (;;) {
|
||||
fputs(get_prompt(prompt), stdout); /* show prompt */
|
||||
for(;;) {
|
||||
l_char buffer[MAXINPUT];
|
||||
size_t l;
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
||||
printf(l_s("\n"));
|
||||
return;
|
||||
}
|
||||
l = strlen(buffer);
|
||||
if (buffer[l-1] == l_c('\n') && buffer[l-2] == l_c('\\')) {
|
||||
buffer[l-2] = l_c('\n');
|
||||
lua_pushlstring(L, buffer, l-1);
|
||||
}
|
||||
else {
|
||||
lua_pushlstring(L, buffer, l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
lua_concat(L, lua_gettop(L));
|
||||
ldo(lua_dostring, lua_tostring(L, -1));
|
||||
lua_settop(L, 0); /* remove eventual results */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int handle_argv (l_char *argv[], struct Options *opt) {
|
||||
if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */
|
||||
if (*argv == NULL) { /* no more arguments? */
|
||||
if (isatty(0)) {
|
||||
@@ -223,8 +224,8 @@ static int handle_argv (char *argv[], struct Options *opt) {
|
||||
else { /* other arguments; loop over them */
|
||||
int i;
|
||||
for (i = 0; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') { /* not an option? */
|
||||
if (strchr(argv[i], '='))
|
||||
if (argv[i][0] != l_c('-')) { /* not an option? */
|
||||
if (strchr(argv[i], l_c('=')))
|
||||
assign(argv[i]);
|
||||
else
|
||||
if (file_input(argv[i]) != EXIT_SUCCESS)
|
||||
@@ -235,46 +236,46 @@ static int handle_argv (char *argv[], struct Options *opt) {
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
break;
|
||||
}
|
||||
case 'i': {
|
||||
case l_c('i'): {
|
||||
manual_input(0, 1);
|
||||
break;
|
||||
}
|
||||
case 'q': {
|
||||
case l_c('q'): {
|
||||
manual_input(0, 0);
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
case l_c('c'): {
|
||||
opt->toclose = 1;
|
||||
break;
|
||||
}
|
||||
case 'v': {
|
||||
case l_c('v'): {
|
||||
print_version();
|
||||
break;
|
||||
}
|
||||
case 'e': {
|
||||
case l_c('e'): {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||
fprintf(stderr, l_s("lua: error running argument `%.99s'\n"), argv[i]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
case l_c('f'): {
|
||||
i++;
|
||||
if (argv[i] == NULL) {
|
||||
print_message();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
getargs(argv+i); /* collect remaining arguments */
|
||||
lua_setglobal(L, "arg");
|
||||
lua_setglobal(L, l_s("arg"));
|
||||
return file_input(argv[i]); /* stop scanning arguments */
|
||||
}
|
||||
case 's': {
|
||||
fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
|
||||
case l_c('s'): {
|
||||
fprintf(stderr, l_s("lua: stack size (`-s') must be the first option\n"));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
default: {
|
||||
@@ -288,11 +289,11 @@ static int handle_argv (char *argv[], struct Options *opt) {
|
||||
}
|
||||
|
||||
|
||||
static void getstacksize (int argc, char *argv[], struct Options *opt) {
|
||||
if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
|
||||
int stacksize = atoi(&argv[1][2]);
|
||||
static void getstacksize (int argc, l_char *argv[], struct Options *opt) {
|
||||
if (argc >= 2 && argv[1][0] == l_c('-') && argv[1][1] == l_c('s')) {
|
||||
int stacksize = strtol(&argv[1][2], NULL, 10);
|
||||
if (stacksize <= 0) {
|
||||
fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
|
||||
fprintf(stderr, l_s("lua: invalid stack size ('%.20s')\n"), &argv[1][2]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
opt->stacksize = stacksize;
|
||||
@@ -302,20 +303,29 @@ static void getstacksize (int argc, char *argv[], struct Options *opt) {
|
||||
}
|
||||
|
||||
|
||||
static void register_getargs (char *argv[]) {
|
||||
lua_pushuserdata(L, argv);
|
||||
static void register_getargs (l_char *argv[]) {
|
||||
lua_newuserdatabox(L, argv);
|
||||
lua_pushcclosure(L, l_getargs, 1);
|
||||
lua_setglobal(L, "getargs");
|
||||
lua_setglobal(L, l_s("getargs"));
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
static void openstdlibs (lua_State *l) {
|
||||
lua_baselibopen(l);
|
||||
lua_iolibopen(l);
|
||||
lua_strlibopen(l);
|
||||
lua_mathlibopen(l);
|
||||
lua_dblibopen(l);
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, l_char *argv[]) {
|
||||
struct Options opt;
|
||||
int status;
|
||||
opt.toclose = 0;
|
||||
getstacksize(argc, argv, &opt); /* handle option `-s' */
|
||||
L = lua_open(opt.stacksize); /* create state */
|
||||
USERINIT(); /* open libraries */
|
||||
LUA_USERINIT(L); /* open libraries */
|
||||
register_getargs(argv); /* create `getargs' function */
|
||||
status = handle_argv(argv+1, &opt);
|
||||
if (opt.toclose)
|
||||
|
||||
291
lua.h
291
lua.h
@@ -1,9 +1,9 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.69 2000/09/14 14:09:31 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.101 2001/07/12 18:11:58 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||
** e-mail: lua@tecgraf.puc-rio.br
|
||||
** www: http://www.tecgraf.puc-rio.br/lua/
|
||||
** e-mail: info@lua.org
|
||||
** www: http://www.lua.org
|
||||
** See Copyright Notice at the end of this file
|
||||
*/
|
||||
|
||||
@@ -16,31 +16,35 @@
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#define LUA_VERSION "Lua 4.0 (beta)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
|
||||
|
||||
#define LUA_VERSION "Lua 4.1 (alpha)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-2001 TeCGraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
/* name of global variable with error handler */
|
||||
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
|
||||
|
||||
|
||||
/* pre-defined references */
|
||||
#define LUA_NOREF (-2)
|
||||
#define LUA_REFNIL (-1)
|
||||
|
||||
#define LUA_ANYTAG (-1)
|
||||
#define LUA_NOTAG (-2)
|
||||
|
||||
/* option for multiple returns in `lua_call' */
|
||||
#define LUA_MULTRET (-1)
|
||||
|
||||
|
||||
#define LUA_MINSTACK 20
|
||||
|
||||
|
||||
/* error codes for lua_do* */
|
||||
/* error codes for `lua_do*' and the like */
|
||||
#define LUA_ERRRUN 1
|
||||
#define LUA_ERRFILE 2
|
||||
#define LUA_ERRSYNTAX 3
|
||||
#define LUA_ERRMEM 4
|
||||
#define LUA_ERRERR 5
|
||||
|
||||
/* weak-table modes */
|
||||
#define LUA_WEAK_KEY 1
|
||||
#define LUA_WEAK_VALUE 2
|
||||
|
||||
|
||||
typedef struct lua_State lua_State;
|
||||
@@ -48,110 +52,177 @@ typedef struct lua_State lua_State;
|
||||
typedef int (*lua_CFunction) (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** an invalid `tag'
|
||||
*/
|
||||
#define LUA_NOTAG (-1)
|
||||
|
||||
/*
|
||||
** tags for basic types
|
||||
*/
|
||||
#define LUA_TNONE LUA_NOTAG
|
||||
|
||||
#define LUA_TUSERDATA 0
|
||||
#define LUA_TNIL 1
|
||||
#define LUA_TNUMBER 2
|
||||
#define LUA_TSTRING 3
|
||||
#define LUA_TTABLE 4
|
||||
#define LUA_TFUNCTION 5
|
||||
|
||||
|
||||
/* minimum Lua stack available to a C function */
|
||||
#define LUA_MINSTACK 20
|
||||
|
||||
|
||||
/*
|
||||
** generic extra include file
|
||||
*/
|
||||
#ifdef LUA_USER_H
|
||||
#include LUA_USER_H
|
||||
#endif
|
||||
|
||||
|
||||
/* Lua numerical type */
|
||||
#ifndef LUA_NUMBER
|
||||
#define LUA_NUMBER double
|
||||
#endif
|
||||
typedef LUA_NUMBER lua_Number;
|
||||
|
||||
/* Lua character type */
|
||||
#ifndef L_CHAR
|
||||
#define L_CHAR char
|
||||
#endif
|
||||
typedef L_CHAR lua_char;
|
||||
|
||||
|
||||
/* mark for all API functions */
|
||||
#ifndef LUA_API
|
||||
#define LUA_API extern
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
lua_State *lua_open (int stacksize);
|
||||
void lua_close (lua_State *L);
|
||||
LUA_API lua_State *lua_newthread (lua_State *L, int stacksize);
|
||||
LUA_API void lua_close (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** basic stack manipulation
|
||||
*/
|
||||
int lua_gettop (lua_State *L);
|
||||
void lua_settop (lua_State *L, int index);
|
||||
void lua_pushvalue (lua_State *L, int index);
|
||||
void lua_remove (lua_State *L, int index);
|
||||
void lua_insert (lua_State *L, int index);
|
||||
int lua_stackspace (lua_State *L);
|
||||
LUA_API int lua_gettop (lua_State *L);
|
||||
LUA_API void lua_settop (lua_State *L, int index);
|
||||
LUA_API void lua_pushvalue (lua_State *L, int index);
|
||||
LUA_API void lua_remove (lua_State *L, int index);
|
||||
LUA_API void lua_insert (lua_State *L, int index);
|
||||
LUA_API int lua_stackspace (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** access functions (stack -> C)
|
||||
*/
|
||||
|
||||
const char *lua_type (lua_State *L, int index);
|
||||
int lua_isnumber (lua_State *L, int index);
|
||||
int lua_iscfunction (lua_State *L, int index);
|
||||
int lua_tag (lua_State *L, int index);
|
||||
LUA_API const lua_char *lua_type (lua_State *L, int index);
|
||||
LUA_API int lua_isnumber (lua_State *L, int index);
|
||||
LUA_API int lua_isstring (lua_State *L, int index);
|
||||
LUA_API int lua_iscfunction (lua_State *L, int index);
|
||||
LUA_API int lua_tag (lua_State *L, int index);
|
||||
LUA_API int lua_rawtag (lua_State *L, int index);
|
||||
|
||||
int lua_equal (lua_State *L, int index1, int index2);
|
||||
int lua_lessthan (lua_State *L, int index1, int index2);
|
||||
LUA_API int lua_equal (lua_State *L, int index1, int index2);
|
||||
LUA_API int lua_lessthan (lua_State *L, int index1, int index2);
|
||||
|
||||
double lua_tonumber (lua_State *L, int index);
|
||||
const char *lua_tostring (lua_State *L, int index);
|
||||
size_t lua_strlen (lua_State *L, int index);
|
||||
lua_CFunction lua_tocfunction (lua_State *L, int index);
|
||||
void *lua_touserdata (lua_State *L, int index);
|
||||
const void *lua_topointer (lua_State *L, int index);
|
||||
LUA_API lua_Number lua_tonumber (lua_State *L, int index);
|
||||
LUA_API const lua_char *lua_tostring (lua_State *L, int index);
|
||||
LUA_API size_t lua_strlen (lua_State *L, int index);
|
||||
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index);
|
||||
LUA_API void *lua_touserdata (lua_State *L, int index);
|
||||
LUA_API const void *lua_topointer (lua_State *L, int index);
|
||||
|
||||
|
||||
/*
|
||||
** push functions (C -> stack)
|
||||
*/
|
||||
void lua_pushnil (lua_State *L);
|
||||
void lua_pushnumber (lua_State *L, double n);
|
||||
void lua_pushlstring (lua_State *L, const char *s, size_t len);
|
||||
void lua_pushstring (lua_State *L, const char *s);
|
||||
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||
void lua_pushusertag (lua_State *L, void *u, int tag);
|
||||
LUA_API void lua_pushnil (lua_State *L);
|
||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n);
|
||||
LUA_API void lua_pushlstring (lua_State *L, const lua_char *s, size_t len);
|
||||
LUA_API void lua_pushstring (lua_State *L, const lua_char *s);
|
||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||
|
||||
|
||||
/*
|
||||
** get functions (Lua -> stack)
|
||||
*/
|
||||
void lua_getglobal (lua_State *L, const char *name);
|
||||
void lua_gettable (lua_State *L, int index);
|
||||
void lua_rawget (lua_State *L, int index);
|
||||
void lua_rawgeti (lua_State *L, int index, int n);
|
||||
void lua_getglobals (lua_State *L);
|
||||
void lua_gettagmethod (lua_State *L, int tag, const char *event);
|
||||
|
||||
int lua_getref (lua_State *L, int ref);
|
||||
|
||||
void lua_newtable (lua_State *L);
|
||||
LUA_API void lua_getglobal (lua_State *L, const lua_char *name);
|
||||
LUA_API void lua_gettable (lua_State *L, int index);
|
||||
LUA_API void lua_rawget (lua_State *L, int index);
|
||||
LUA_API void lua_rawgeti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_getglobals (lua_State *L);
|
||||
LUA_API void lua_gettagmethod (lua_State *L, int tag, const lua_char *event);
|
||||
LUA_API int lua_getref (lua_State *L, int ref);
|
||||
LUA_API void lua_newtable (lua_State *L);
|
||||
LUA_API void lua_getregistry (lua_State *L);
|
||||
LUA_API void lua_getweakregistry (lua_State *L);
|
||||
|
||||
|
||||
/*
|
||||
** set functions (stack -> Lua)
|
||||
*/
|
||||
void lua_setglobal (lua_State *L, const char *name);
|
||||
void lua_settable (lua_State *L, int index);
|
||||
void lua_rawset (lua_State *L, int index);
|
||||
void lua_rawseti (lua_State *L, int index, int n);
|
||||
void lua_setglobals (lua_State *L);
|
||||
void lua_settagmethod (lua_State *L, int tag, const char *event);
|
||||
int lua_ref (lua_State *L, int lock);
|
||||
LUA_API void lua_setglobal (lua_State *L, const lua_char *name);
|
||||
LUA_API void lua_settable (lua_State *L, int index);
|
||||
LUA_API void lua_rawset (lua_State *L, int index);
|
||||
LUA_API void lua_rawseti (lua_State *L, int index, int n);
|
||||
LUA_API void lua_setglobals (lua_State *L);
|
||||
LUA_API void lua_settagmethod (lua_State *L, int tag, const lua_char *event);
|
||||
LUA_API int lua_ref (lua_State *L, int lock);
|
||||
|
||||
|
||||
/*
|
||||
** "do" functions (run Lua code)
|
||||
** `load' and `do' functions (load and run Lua code)
|
||||
*/
|
||||
int lua_call (lua_State *L, int nargs, int nresults);
|
||||
void lua_rawcall (lua_State *L, int nargs, int nresults);
|
||||
int lua_dofile (lua_State *L, const char *filename);
|
||||
int lua_dostring (lua_State *L, const char *str);
|
||||
int lua_dobuffer (lua_State *L, const char *buff, size_t size,
|
||||
const char *name);
|
||||
LUA_API int lua_call (lua_State *L, int nargs, int nresults);
|
||||
LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults);
|
||||
LUA_API int lua_loadfile (lua_State *L, const lua_char *filename);
|
||||
LUA_API int lua_dofile (lua_State *L, const lua_char *filename);
|
||||
LUA_API int lua_dostring (lua_State *L, const lua_char *str);
|
||||
LUA_API int lua_loadbuffer (lua_State *L, const lua_char *buff, size_t size,
|
||||
const lua_char *name);
|
||||
LUA_API int lua_dobuffer (lua_State *L, const lua_char *buff, size_t size,
|
||||
const lua_char *name);
|
||||
|
||||
/*
|
||||
** Garbage-collection functions
|
||||
*/
|
||||
LUA_API int lua_getgcthreshold (lua_State *L);
|
||||
LUA_API int lua_getgccount (lua_State *L);
|
||||
LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
|
||||
|
||||
/*
|
||||
** miscellaneous functions
|
||||
*/
|
||||
int lua_newtag (lua_State *L);
|
||||
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||
void lua_settag (lua_State *L, int tag);
|
||||
LUA_API int lua_newtype (lua_State *L, const lua_char *name, int basictype);
|
||||
LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||
LUA_API void lua_settag (lua_State *L, int tag);
|
||||
|
||||
void lua_error (lua_State *L, const char *s);
|
||||
LUA_API int lua_name2tag (lua_State *L, const lua_char *name);
|
||||
LUA_API const lua_char *lua_tag2name (lua_State *L, int tag);
|
||||
|
||||
void lua_unref (lua_State *L, int ref);
|
||||
LUA_API void lua_error (lua_State *L, const lua_char *s);
|
||||
|
||||
long lua_collectgarbage (lua_State *L, long limit);
|
||||
LUA_API void lua_unref (lua_State *L, int ref);
|
||||
|
||||
int lua_next (lua_State *L, int index);
|
||||
int lua_getn (lua_State *L, int index);
|
||||
LUA_API int lua_next (lua_State *L, int index);
|
||||
LUA_API int lua_getn (lua_State *L, int index);
|
||||
|
||||
LUA_API void lua_concat (lua_State *L, int n);
|
||||
|
||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size);
|
||||
LUA_API void lua_newuserdatabox (lua_State *L, void *u);
|
||||
|
||||
LUA_API void lua_setweakmode (lua_State *L, int mode);
|
||||
LUA_API int lua_getweakmode (lua_State *L, int index);
|
||||
|
||||
void lua_concat (lua_State *L, int n);
|
||||
|
||||
|
||||
/*
|
||||
@@ -160,26 +231,89 @@ void lua_concat (lua_State *L, int n);
|
||||
** ===============================================================
|
||||
*/
|
||||
|
||||
#define lua_open(n) lua_newthread(NULL, (n))
|
||||
|
||||
#define lua_pop(L,n) lua_settop(L, -(n)-1)
|
||||
|
||||
#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
|
||||
#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 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_isfunction(L,n) (*lua_type(L,n) == 'f')
|
||||
#define lua_isstring(L,n) (lua_tostring(L,n) != 0)
|
||||
#define lua_istable(L,n) (*lua_type(L,n) == 't')
|
||||
#define lua_isuserdata(L,n) (*lua_type(L,n) == 'u')
|
||||
#define lua_isnil(L,n) (lua_type(L,n)[2] == 'l')
|
||||
#define lua_isnull(L,n) (*lua_type(L,n) == 'N')
|
||||
#define lua_isfunction(L,n) (lua_rawtag(L,n) == LUA_TFUNCTION)
|
||||
#define lua_istable(L,n) (lua_rawtag(L,n) == LUA_TTABLE)
|
||||
#define lua_isuserdata(L,n) (lua_rawtag(L,n) == LUA_TUSERDATA)
|
||||
#define lua_isnil(L,n) (lua_rawtag(L,n) == LUA_TNIL)
|
||||
#define lua_isnull(L,n) (lua_rawtag(L,n) == LUA_TNONE)
|
||||
|
||||
#define lua_pushliteral(L, s) lua_pushlstring(L, s, \
|
||||
(sizeof(s)/sizeof(lua_char))-1)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** compatibility macros
|
||||
*/
|
||||
#define lua_newtag(L) lua_newtype(L, NULL, LUA_TNONE)
|
||||
#define lua_typename lua_tag2name
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================================
|
||||
** useful definitions for Lua kernel and libraries
|
||||
*/
|
||||
#ifdef LUA_PRIVATE
|
||||
|
||||
#define l_char lua_char
|
||||
|
||||
/* macro to control type of literal strings */
|
||||
#ifndef l_s
|
||||
#define l_s(x) x
|
||||
#endif
|
||||
|
||||
/* macro to control type of literal chars */
|
||||
#ifndef l_c
|
||||
#define l_c(x) x
|
||||
#endif
|
||||
|
||||
/* macro to `unsign' a character */
|
||||
#ifndef uchar
|
||||
#define uchar(c) ((unsigned char)(c))
|
||||
#endif
|
||||
|
||||
/* integer type to hold the result of fgetc */
|
||||
#ifndef l_charint
|
||||
#define l_charint int
|
||||
#endif
|
||||
|
||||
/*
|
||||
** formats for Lua numbers
|
||||
*/
|
||||
#ifndef LUA_NUMBER_SCAN
|
||||
#define LUA_NUMBER_SCAN "%lf"
|
||||
#endif
|
||||
#ifndef LUA_NUMBER_FMT
|
||||
#define LUA_NUMBER_FMT "%.16g"
|
||||
#endif
|
||||
/* function to convert a lua_Number to a string */
|
||||
#ifndef lua_number2str
|
||||
#define lua_number2str(s,n) sprintf((s), l_s(LUA_NUMBER_FMT), (n))
|
||||
#endif
|
||||
|
||||
/* function to convert a string to a lua_Number */
|
||||
#ifndef lua_str2number
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
#endif
|
||||
|
||||
#endif
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 1994-2000 TeCGraf, PUC-Rio. All rights reserved.
|
||||
* Copyright (C) 1994-2001 TeCGraf, PUC-Rio. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and without license
|
||||
* or royalty fees, to use, copy, modify, and distribute this software and its
|
||||
@@ -212,3 +346,4 @@ void lua_concat (lua_State *L, int n);
|
||||
*
|
||||
* This implementation contains no third-party code.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
28
luadebug.h
28
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.19 2001/03/07 18:09:25 roberto Exp roberto $
|
||||
** Debugging API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,29 +17,29 @@ typedef struct lua_Localvar lua_Localvar;
|
||||
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||
|
||||
|
||||
int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
|
||||
const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int localnum);
|
||||
const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int localnum);
|
||||
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||
LUA_API int lua_getinfo (lua_State *L, const lua_char *what, lua_Debug *ar);
|
||||
LUA_API const lua_char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
|
||||
LUA_API const lua_char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
|
||||
|
||||
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||
LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||
LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||
|
||||
|
||||
#define LUA_IDSIZE 60
|
||||
|
||||
struct lua_Debug {
|
||||
const char *event; /* `call', `return' */
|
||||
const lua_char *event; /* `call', `return' */
|
||||
int currentline; /* (l) */
|
||||
const char *name; /* (n) */
|
||||
const char *namewhat; /* (n) `global', `tag method', `local', `field' */
|
||||
const lua_char *name; /* (n) */
|
||||
const lua_char *namewhat; /* (n) `global', `tag method', `local', `field' */
|
||||
int nups; /* (u) number of upvalues */
|
||||
int linedefined; /* (S) */
|
||||
const char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
const char *source; /* (S) */
|
||||
char short_src[LUA_IDSIZE]; /* (S) */
|
||||
const lua_char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||
const lua_char *source; /* (S) */
|
||||
lua_char short_src[LUA_IDSIZE]; /* (S) */
|
||||
/* private part */
|
||||
struct lua_TObject *_func; /* active function */
|
||||
struct CallInfo *_ci; /* active function */
|
||||
};
|
||||
|
||||
|
||||
|
||||
23
lualib.h
23
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.20 2001/03/06 20:09:38 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,19 +11,18 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifndef LUALIB_API
|
||||
#define LUALIB_API extern
|
||||
#endif
|
||||
|
||||
|
||||
#define LUA_ALERT "_ALERT"
|
||||
|
||||
void lua_baselibopen (lua_State *L);
|
||||
void lua_iolibopen (lua_State *L);
|
||||
void lua_strlibopen (lua_State *L);
|
||||
void lua_mathlibopen (lua_State *L);
|
||||
void lua_dblibopen (lua_State *L);
|
||||
LUALIB_API int lua_baselibopen (lua_State *L);
|
||||
LUALIB_API int lua_iolibopen (lua_State *L);
|
||||
LUALIB_API int lua_strlibopen (lua_State *L);
|
||||
LUALIB_API int lua_mathlibopen (lua_State *L);
|
||||
LUALIB_API int lua_dblibopen (lua_State *L);
|
||||
|
||||
|
||||
|
||||
/* Auxiliary functions (private) */
|
||||
|
||||
const char *luaI_classend (lua_State *L, const char *p);
|
||||
int luaI_singlematch (int c, const char *p, const char *ep);
|
||||
|
||||
#endif
|
||||
|
||||
204
lundump.c
204
lundump.c
@@ -1,12 +1,16 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.32 2000/09/21 03:15:36 lhf Exp $
|
||||
** load bytecodes from files
|
||||
** $Id: lundump.c,v 1.36 2001/07/19 14:34:06 lhf Exp lhf $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
@@ -14,16 +18,17 @@
|
||||
#include "lundump.h"
|
||||
|
||||
#define LoadByte ezgetc
|
||||
#define LoadShort (short) LoadInt
|
||||
|
||||
static const char* ZNAME (ZIO* Z)
|
||||
static const l_char* ZNAME (ZIO* Z)
|
||||
{
|
||||
const char* s=zname(Z);
|
||||
return (*s=='@') ? s+1 : s;
|
||||
const l_char* s=zname(Z);
|
||||
return (*s==l_c('@')) ? s+1 : s;
|
||||
}
|
||||
|
||||
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
||||
{
|
||||
luaO_verror(L,"unexpected end of file in `%.255s'",ZNAME(Z));
|
||||
luaO_verror(L,l_s("unexpected end of file in `%.99s'"),ZNAME(Z));
|
||||
}
|
||||
|
||||
static int ezgetc (lua_State* L, ZIO* Z)
|
||||
@@ -43,9 +48,9 @@ static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
|
||||
{
|
||||
if (swap)
|
||||
{
|
||||
char *p=(char *) b+size-1;
|
||||
l_char *p=(l_char*) b+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(L,Z);
|
||||
while (n--) *p--=(l_char)ezgetc(L,Z);
|
||||
}
|
||||
else
|
||||
ezread(L,Z,b,size);
|
||||
@@ -55,12 +60,12 @@ static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int s
|
||||
{
|
||||
if (swap)
|
||||
{
|
||||
char *q=(char *) b;
|
||||
l_char *q=(l_char*) b;
|
||||
while (m--)
|
||||
{
|
||||
char *p=q+size-1;
|
||||
l_char *p=q+size-1;
|
||||
int n=size;
|
||||
while (n--) *p--=(char)ezgetc(L,Z);
|
||||
while (n--) *p--=(l_char)ezgetc(L,Z);
|
||||
q+=size;
|
||||
}
|
||||
}
|
||||
@@ -82,9 +87,9 @@ static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
|
||||
return x;
|
||||
}
|
||||
|
||||
static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
|
||||
static lua_Number LoadNumber (lua_State* L, ZIO* Z, int swap)
|
||||
{
|
||||
Number x;
|
||||
lua_Number x;
|
||||
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||
return x;
|
||||
}
|
||||
@@ -96,149 +101,164 @@ static TString* LoadString (lua_State* L, ZIO* Z, int swap)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* s=luaO_openspace(L,size);
|
||||
l_char* s=luaO_openspace(L,size,l_char);
|
||||
LoadBlock(L,s,size,Z,0);
|
||||
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadCode (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
int size=LoadInt(L,Z,swap);
|
||||
tf->code=luaM_newvector(L,size,Instruction);
|
||||
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
|
||||
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.255s'",ZNAME(Z));
|
||||
f->code=luaM_newvector(L,size,Instruction);
|
||||
f->sizecode=size;
|
||||
LoadVector(L,f->code,size,sizeof(*f->code),Z,swap);
|
||||
}
|
||||
|
||||
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadLocals (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
int i,n;
|
||||
tf->nlocvars=n=LoadInt(L,Z,swap);
|
||||
tf->locvars=luaM_newvector(L,n,LocVar);
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->locvars=luaM_newvector(L,n,LocVar);
|
||||
f->sizelocvars=n;
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].varname=LoadString(L,Z,swap);
|
||||
tf->locvars[i].startpc=LoadInt(L,Z,swap);
|
||||
tf->locvars[i].endpc=LoadInt(L,Z,swap);
|
||||
f->locvars[i].varname=LoadString(L,Z,swap);
|
||||
f->locvars[i].startpc=LoadInt(L,Z,swap);
|
||||
f->locvars[i].endpc=LoadInt(L,Z,swap);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadLines (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
int n=LoadInt(L,Z,swap);
|
||||
if (n==0) return;
|
||||
tf->lineinfo=luaM_newvector(L,n,int);
|
||||
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
|
||||
int n;
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->lineinfo=luaM_newvector(L,n,int);
|
||||
f->sizelineinfo=n;
|
||||
LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
|
||||
static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap);
|
||||
|
||||
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||
static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
|
||||
{
|
||||
int i,n;
|
||||
tf->nkstr=n=LoadInt(L,Z,swap);
|
||||
tf->kstr=luaM_newvector(L,n,TString*);
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->k=luaM_newvector(L,n,TObject);
|
||||
f->sizek=n;
|
||||
for (i=0; i<n; i++)
|
||||
tf->kstr[i]=LoadString(L,Z,swap);
|
||||
tf->nknum=n=LoadInt(L,Z,swap);
|
||||
tf->knum=luaM_newvector(L,n,Number);
|
||||
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
|
||||
tf->nkproto=n=LoadInt(L,Z,swap);
|
||||
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||
for (i=0; i<n; i++)
|
||||
tf->kproto[i]=LoadFunction(L,Z,swap);
|
||||
{
|
||||
TObject* o=&f->k[i];
|
||||
ttype(o)=LoadByte(L,Z);
|
||||
switch (ttype(o))
|
||||
{
|
||||
case LUA_TNUMBER:
|
||||
nvalue(o)=LoadNumber(L,Z,swap);
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
tsvalue(o)=LoadString(L,Z,swap);
|
||||
break;
|
||||
default:
|
||||
luaO_verror(L,l_s("bad constant type (%d) in `%.99s'"),ttype(o),ZNAME(Z));
|
||||
break;
|
||||
}
|
||||
}
|
||||
n=LoadInt(L,Z,swap);
|
||||
f->p=luaM_newvector(L,n,Proto*);
|
||||
f->sizep=n;
|
||||
for (i=0; i<n; i++) f->p[i]=LoadFunction(L,f->source,Z,swap);
|
||||
}
|
||||
|
||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
|
||||
static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap)
|
||||
{
|
||||
Proto* tf=luaF_newproto(L);
|
||||
tf->source=LoadString(L,Z,swap);
|
||||
tf->lineDefined=LoadInt(L,Z,swap);
|
||||
tf->numparams=LoadInt(L,Z,swap);
|
||||
tf->is_vararg=LoadByte(L,Z);
|
||||
tf->maxstacksize=LoadInt(L,Z,swap);
|
||||
LoadCode(L,tf,Z,swap);
|
||||
LoadLocals(L,tf,Z,swap);
|
||||
LoadLines(L,tf,Z,swap);
|
||||
LoadConstants(L,tf,Z,swap);
|
||||
return tf;
|
||||
Proto* f=luaF_newproto(L);
|
||||
f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p;
|
||||
f->lineDefined=LoadInt(L,Z,swap);
|
||||
f->nupvalues=LoadShort(L,Z,swap);
|
||||
f->numparams=LoadShort(L,Z,swap);
|
||||
f->is_vararg=LoadShort(L,Z,swap);
|
||||
f->maxstacksize=LoadShort(L,Z,swap);
|
||||
LoadLocals(L,f,Z,swap);
|
||||
LoadLines(L,f,Z,swap);
|
||||
LoadConstants(L,f,Z,swap);
|
||||
LoadCode(L,f,Z,swap);
|
||||
#ifndef TRUST_BINARIES
|
||||
if (!luaG_checkcode(f)) luaO_verror(L,l_s("bad code in `%.99s'"),ZNAME(Z));
|
||||
#endif
|
||||
return f;
|
||||
}
|
||||
|
||||
static void LoadSignature (lua_State* L, ZIO* Z)
|
||||
{
|
||||
const char* s=SIGNATURE;
|
||||
const l_char* s=l_s(LUA_SIGNATURE);
|
||||
while (*s!=0 && ezgetc(L,Z)==*s)
|
||||
++s;
|
||||
if (*s!=0) luaO_verror(L,"bad signature in `%.255s'",ZNAME(Z));
|
||||
if (*s!=0) luaO_verror(L,l_s("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 l_char* what, ZIO* Z)
|
||||
{
|
||||
int r=ezgetc(L,Z);
|
||||
int r=LoadByte(L,Z);
|
||||
if (r!=s)
|
||||
luaO_verror(L,"virtual machine mismatch in `%.255s':\n"
|
||||
" %s is %d but read %d",ZNAME(Z),what,s,r);
|
||||
luaO_verror(L,l_s("virtual machine mismatch in `%.99s':\n")
|
||||
l_s(" size of %.20s is %d but read %d"),ZNAME(Z),what,s,r);
|
||||
}
|
||||
|
||||
#define TESTSIZE(s) TestSize(L,s,#s,Z)
|
||||
#define V(v) v/16,v%16
|
||||
#define TESTSIZE(s,w) TestSize(L,s,w,Z)
|
||||
#define V(v) v/16,v%16
|
||||
|
||||
static int LoadHeader (lua_State* L, ZIO* Z)
|
||||
{
|
||||
int version,swap;
|
||||
Number f=0,tf=TEST_NUMBER;
|
||||
lua_Number x=0,tx=TEST_NUMBER;
|
||||
LoadSignature(L,Z);
|
||||
version=ezgetc(L,Z);
|
||||
version=LoadByte(L,Z);
|
||||
if (version>VERSION)
|
||||
luaO_verror(L,"`%.255s' too new:\n"
|
||||
" read version %d.%d; expected at most %d.%d",
|
||||
luaO_verror(L,l_s("`%.99s' too new:\n")
|
||||
l_s(" read version %d.%d; expected at most %d.%d"),
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaO_verror(L,"`%.255s' too old:\n"
|
||||
" read version %d.%d; expected at least %d.%d",
|
||||
luaO_verror(L,l_s("`%.99s' too old:\n")
|
||||
l_s(" read version %d.%d; expected at least %d.%d"),
|
||||
ZNAME(Z),V(version),V(VERSION));
|
||||
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
|
||||
TESTSIZE(sizeof(int));
|
||||
TESTSIZE(sizeof(size_t));
|
||||
TESTSIZE(sizeof(Instruction));
|
||||
TESTSIZE(SIZE_INSTRUCTION);
|
||||
TESTSIZE(SIZE_OP);
|
||||
TESTSIZE(SIZE_B);
|
||||
TESTSIZE(sizeof(Number));
|
||||
f=LoadNumber(L,Z,swap);
|
||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||
luaO_verror(L,"unknown number format in `%.255s':\n"
|
||||
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||
ZNAME(Z),f,tf);
|
||||
swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */
|
||||
TESTSIZE(sizeof(int),l_s("int"));
|
||||
TESTSIZE(sizeof(size_t), l_s("size_t"));
|
||||
TESTSIZE(sizeof(Instruction), l_s("size_t"));
|
||||
TESTSIZE(SIZE_OP, l_s("OP"));
|
||||
TESTSIZE(SIZE_A, l_s("A"));
|
||||
TESTSIZE(SIZE_B, l_s("B"));
|
||||
TESTSIZE(SIZE_C, l_s("C"));
|
||||
TESTSIZE(sizeof(lua_Number), l_s("number"));
|
||||
x=LoadNumber(L,Z,swap);
|
||||
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
|
||||
luaO_verror(L,l_s("unknown number format in `%.99s':\n")
|
||||
l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT),
|
||||
ZNAME(Z),x,tx);
|
||||
return swap;
|
||||
}
|
||||
|
||||
static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
||||
{
|
||||
return LoadFunction(L,Z,LoadHeader(L,Z));
|
||||
return LoadFunction(L,NULL,Z,LoadHeader(L,Z));
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file or buffer
|
||||
** return main if ok and NULL at EOF
|
||||
*/
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z)
|
||||
{
|
||||
Proto* tf=NULL;
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_CHUNK)
|
||||
tf=LoadChunk(L,Z);
|
||||
c=zgetc(Z);
|
||||
if (c!=EOZ)
|
||||
luaO_verror(L,"`%.255s' apparently contains more than one chunk",ZNAME(Z));
|
||||
return tf;
|
||||
Proto* f=LoadChunk(L,Z);
|
||||
if (zgetc(Z)!=EOZ)
|
||||
luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z));
|
||||
return f;
|
||||
}
|
||||
|
||||
/*
|
||||
** find byte order
|
||||
*/
|
||||
int luaU_endianess (void)
|
||||
int luaU_endianness (void)
|
||||
{
|
||||
int x=1;
|
||||
return *(char*)&x;
|
||||
return *(l_char*)&x;
|
||||
}
|
||||
|
||||
23
lundump.h
23
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.23 2001/06/28 13:55:17 lhf Exp lhf $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -12,27 +12,14 @@
|
||||
|
||||
/* load one chunk */
|
||||
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||
#define luaU_undump1 luaU_undump
|
||||
|
||||
/* find byte order */
|
||||
int luaU_endianess (void);
|
||||
int luaU_endianness (void);
|
||||
|
||||
/* definitions for headers of binary files */
|
||||
#define VERSION 0x40 /* last format change was in 4.0 */
|
||||
#define VERSION0 0x40 /* last major change was in 4.0 */
|
||||
#define ID_CHUNK 27 /* binary files start with ESC... */
|
||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||
|
||||
/* formats for error messages */
|
||||
#define xSOURCE "<%d:%.255s>"
|
||||
#define SOURCE "<%.255s:%d>"
|
||||
#define IN " in %p " SOURCE
|
||||
#define INLOC tf,tf->source->str,tf->lineDefined
|
||||
|
||||
/* format for numbers in listings and error messages */
|
||||
#ifndef NUMBER_FMT
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#endif
|
||||
#define VERSION 0x41 /* last format change was in 4.1 */
|
||||
#define VERSION0 0x41 /* last major change was in 4.1 */
|
||||
#define LUA_SIGNATURE "\033Lua" /* binary files start with <esc>Lua */
|
||||
|
||||
/* a multiple of PI for testing native format */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
|
||||
17
lvm.h
17
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.29 2001/02/07 18:13:49 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,20 +13,19 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0))
|
||||
#define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0))
|
||||
|
||||
|
||||
int luaV_tonumber (TObject *obj);
|
||||
const TObject *luaV_tonumber (const TObject *obj, TObject *n);
|
||||
int luaV_tostring (lua_State *L, TObject *obj);
|
||||
const TObject *luaV_gettable (lua_State *L, StkId t);
|
||||
void luaV_settable (lua_State *L, StkId t, StkId key);
|
||||
const TObject *luaV_getglobal (lua_State *L, TString *s);
|
||||
void luaV_setglobal (lua_State *L, TString *s);
|
||||
void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res);
|
||||
void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val);
|
||||
void luaV_getglobal (lua_State *L, TString *s, StkId res);
|
||||
void luaV_setglobal (lua_State *L, TString *s, StkId val);
|
||||
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base);
|
||||
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems);
|
||||
void luaV_Lclosure (lua_State *L, Proto *l, int nelems);
|
||||
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);
|
||||
void luaV_strconc (lua_State *L, int total, StkId top);
|
||||
|
||||
#endif
|
||||
|
||||
8
lzio.c
8
lzio.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.12 2000/05/24 13:54:49 roberto Exp roberto $
|
||||
** $Id: lzio.c,v 1.13 2000/06/12 13:52:05 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LUA_PRIVATE
|
||||
#include "lua.h"
|
||||
|
||||
#include "lzio.h"
|
||||
@@ -71,7 +72,10 @@ size_t zread (ZIO *z, void *b, size_t n) {
|
||||
if (z->n == 0) {
|
||||
if (z->filbuf(z) == EOZ)
|
||||
return n; /* return number of missing bytes */
|
||||
zungetc(z); /* put result from `filbuf' in the buffer */
|
||||
else {
|
||||
++z->n; /* filbuf removed first byte; put back it */
|
||||
--z->p;
|
||||
}
|
||||
}
|
||||
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||
memcpy(b, z->p, m);
|
||||
|
||||
6
lzio.h
6
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.7 2000/10/20 16:36:32 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -29,13 +29,15 @@ ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */
|
||||
size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
|
||||
|
||||
#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||
#define zname(z) ((z)->name)
|
||||
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
#ifndef ZBSIZE
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
#endif
|
||||
|
||||
struct zio {
|
||||
size_t n; /* bytes still unread */
|
||||
|
||||
52
makefile
52
makefile
@@ -1,5 +1,5 @@
|
||||
#
|
||||
## $Id: makefile,v 1.27 2000/09/11 20:29:27 roberto Exp roberto $
|
||||
## $Id: makefile,v 1.31 2001/07/24 17:25:30 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
@@ -19,15 +19,11 @@
|
||||
# (for instance, -DLUA_NUM_TYPE=float)
|
||||
# you may need to adapat the code, too.
|
||||
#
|
||||
# define LUA_COMPAT_READPATTERN if you need read patterns
|
||||
# (only for compatibility with previous versions)
|
||||
# define LUA_COMPAT_ARGRET for compatibility in the way function results
|
||||
# are passed as arguments
|
||||
# define LUA_DEPRECATETFUNCS to include obsolete functions
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
|
||||
# -DLUA_DEPRECATETFUNCS
|
||||
#EXTRA_H=-DLUA_USER_H='"ltests.h"'
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE $(EXTRA_H)
|
||||
#CONFIG = -DOLD_ANSI
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
@@ -39,11 +35,11 @@ CWARNS = -Wall -W -pedantic \
|
||||
-Wnested-externs \
|
||||
-Wpointer-arith \
|
||||
-Wshadow \
|
||||
-Wwrite-strings
|
||||
-Wwrite-strings \
|
||||
# -Wcast-qual
|
||||
# -Wtraditional
|
||||
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O3 -fomit-frame-pointer
|
||||
|
||||
|
||||
# To make early versions
|
||||
@@ -70,10 +66,11 @@ LUAOBJS = \
|
||||
lcode.o \
|
||||
lparser.o \
|
||||
llex.o \
|
||||
lopcodes.o \
|
||||
lundump.o \
|
||||
lzio.o \
|
||||
ldebug.o \
|
||||
ltests.o
|
||||
ltests.o
|
||||
|
||||
LIBOBJS = \
|
||||
lauxlib.o \
|
||||
@@ -113,15 +110,14 @@ clear :
|
||||
|
||||
lapi.o: lapi.c lua.h lapi.h lobject.h llimits.h ldo.h lstate.h \
|
||||
luadebug.h lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h lvm.h
|
||||
lauxlib.o: lauxlib.c lua.h lauxlib.h luadebug.h
|
||||
lauxlib.o: lauxlib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
lbaselib.o: lbaselib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
lcode.o: lcode.c /usr/include/stdlib.h lua.h lcode.h llex.h lobject.h \
|
||||
llimits.h lzio.h lopcodes.h lparser.h ldo.h lstate.h luadebug.h \
|
||||
lmem.h
|
||||
lcode.o: lcode.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h lmem.h
|
||||
ldblib.o: ldblib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
ldebug.o: ldebug.c lua.h lapi.h lobject.h llimits.h lcode.h llex.h \
|
||||
lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lstring.h ltable.h ltm.h
|
||||
lfunc.h lstring.h ltable.h ltm.h lvm.h
|
||||
ldo.o: ldo.c lua.h ldebug.h lstate.h lobject.h llimits.h luadebug.h \
|
||||
ldo.h lgc.h lmem.h lparser.h lzio.h lstring.h ltable.h ltm.h \
|
||||
lundump.h lvm.h
|
||||
@@ -130,30 +126,32 @@ lfunc.o: lfunc.c lua.h lfunc.h lobject.h llimits.h lmem.h lstate.h \
|
||||
lgc.o: lgc.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h
|
||||
liolib.o: liolib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||
llex.o: llex.c lua.h llex.h lobject.h llimits.h lzio.h lmem.h \
|
||||
lparser.h lstate.h luadebug.h lstring.h ltable.h
|
||||
llex.o: llex.c lua.h llex.h lobject.h llimits.h lzio.h lparser.h \
|
||||
lstate.h luadebug.h lstring.h
|
||||
lmathlib.o: lmathlib.c lua.h lauxlib.h lualib.h
|
||||
lmem.o: lmem.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h
|
||||
lobject.o: lobject.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h
|
||||
lobject.o: lobject.c lua.h ldo.h lobject.h llimits.h lstate.h \
|
||||
luadebug.h lmem.h
|
||||
lopcodes.o: lopcodes.c lua.h lopcodes.h llimits.h
|
||||
lparser.o: lparser.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||
lopcodes.h lparser.h lfunc.h lmem.h lstate.h luadebug.h lstring.h
|
||||
lopcodes.h lparser.h ldebug.h lstate.h luadebug.h lfunc.h lmem.h \
|
||||
lstring.h
|
||||
lstate.o: lstate.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h
|
||||
lstring.o: lstring.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h lstring.h
|
||||
lstrlib.o: lstrlib.c lua.h lauxlib.h lualib.h
|
||||
ltable.o: ltable.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||
luadebug.h lstring.h ltable.h
|
||||
ltable.o: ltable.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h ltable.h
|
||||
ltests.o: ltests.c lua.h lapi.h lobject.h llimits.h lauxlib.h lcode.h \
|
||||
llex.h lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||
lfunc.h lmem.h lstring.h ltable.h lualib.h
|
||||
ltm.o: ltm.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||
lmem.h ltm.h
|
||||
lmem.h lstring.h ltable.h ltm.h
|
||||
lua.o: lua.c lua.h luadebug.h lualib.h
|
||||
lundump.o: lundump.c lfunc.h lobject.h llimits.h lua.h lmem.h \
|
||||
lopcodes.h lstring.h lstate.h luadebug.h lundump.h lzio.h
|
||||
lundump.o: lundump.c lua.h ldebug.h lstate.h lobject.h llimits.h \
|
||||
luadebug.h lfunc.h lmem.h lopcodes.h lstring.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h \
|
||||
luadebug.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h ltm.h \
|
||||
lvm.h
|
||||
|
||||
1786
manual.tex
1786
manual.tex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user