mirror of
https://github.com/lua/lua.git
synced 2026-01-27 18:04:56 +00:00
Compare commits
350 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a7cf4f319 | ||
|
|
5cfc725a8b | ||
|
|
45c7ae5b1b | ||
|
|
962f444a75 | ||
|
|
c4e2c91973 | ||
|
|
632a71b24d | ||
|
|
578ae5745c | ||
|
|
a5522f06d2 | ||
|
|
3d03ae5bd6 | ||
|
|
82d721a855 | ||
|
|
104b0fc700 | ||
|
|
8164d09338 | ||
|
|
985ef32248 | ||
|
|
a07f6a8241 | ||
|
|
f33cc4ddec | ||
|
|
d94f7ba304 | ||
|
|
4cf498210e | ||
|
|
5b7d998764 | ||
|
|
81f4def54f | ||
|
|
e44f3a2ffc | ||
|
|
f791bb6906 | ||
|
|
d342328e5b | ||
|
|
0149b781d4 | ||
|
|
d4eff00234 | ||
|
|
fca974486d | ||
|
|
26755cad99 | ||
|
|
b352217b84 | ||
|
|
9c66903cc5 | ||
|
|
30a7b93439 | ||
|
|
7a92f3f99a | ||
|
|
3347c9d32d | ||
|
|
25c54fe60e | ||
|
|
0cc3c9447c | ||
|
|
8fb1af0e33 | ||
|
|
140b672e2e | ||
|
|
9ea06e61f2 | ||
|
|
ffbcadfb41 | ||
|
|
0b73ed8f08 | ||
|
|
f87416f1a3 | ||
|
|
03a3473687 | ||
|
|
9a3940380a | ||
|
|
711890624f | ||
|
|
13d2326a23 | ||
|
|
06c5d3825f | ||
|
|
c345877e4c | ||
|
|
907d172c11 | ||
|
|
88aa4049ad | ||
|
|
c688b00f73 | ||
|
|
dd095677e3 | ||
|
|
53dc5a3bba | ||
|
|
5b179eaf6a | ||
|
|
8fddca81e7 | ||
|
|
c33bb08ffe | ||
|
|
e3716ee161 | ||
|
|
303f415559 | ||
|
|
ccb8b307f1 | ||
|
|
60b6599e83 | ||
|
|
c612685d4b | ||
|
|
85a3c1699c | ||
|
|
f65d1f9e02 | ||
|
|
942c10a5e3 | ||
|
|
8485687908 | ||
|
|
03d672a95c | ||
|
|
03bf7fdd4f | ||
|
|
59a1adf194 | ||
|
|
cfce6f4b20 | ||
|
|
1da89da62f | ||
|
|
f6c627af20 | ||
|
|
270a58c062 | ||
|
|
30531c291b | ||
|
|
07b009c371 | ||
|
|
f711567448 | ||
|
|
9386e49a31 | ||
|
|
8cd7ae7da0 | ||
|
|
0cecf1ab6d | ||
|
|
e657a48ea5 | ||
|
|
fd897027f1 | ||
|
|
d05fe48bfd | ||
|
|
519c57d597 | ||
|
|
c15543b9af | ||
|
|
be05c44481 | ||
|
|
6d53701c7a | ||
|
|
abbae57c78 | ||
|
|
f2c1531e6c | ||
|
|
ded2ad2d86 | ||
|
|
3fb7a77731 | ||
|
|
fded0b4a84 | ||
|
|
3b9dd52be0 | ||
|
|
7dc6aae290 | ||
|
|
5b1ab8efdc | ||
|
|
7ade155762 | ||
|
|
d827e96f33 | ||
|
|
3f0ea90aa8 | ||
|
|
4365a45d68 | ||
|
|
be81209063 | ||
|
|
e055905914 | ||
|
|
9b014d4bcd | ||
|
|
50fd8d03c3 | ||
|
|
3dbb1a4b89 | ||
|
|
3dd8ea54da | ||
|
|
620f49a7aa | ||
|
|
3f4f28010a | ||
|
|
93e347b519 | ||
|
|
f4123b2fc2 | ||
|
|
37a1b72706 | ||
|
|
ef5d171cc8 | ||
|
|
b0f3df16a4 | ||
|
|
cad5a4fdbb | ||
|
|
fdbb4c2341 | ||
|
|
921832be8d | ||
|
|
205f9aa67b | ||
|
|
94d38560c3 | ||
|
|
c2dc6e8e94 | ||
|
|
22974326ca | ||
|
|
c931d86e98 | ||
|
|
d9e0f64a5d | ||
|
|
ab66652b32 | ||
|
|
4398e488e6 | ||
|
|
808976bb59 | ||
|
|
b5b1995f29 | ||
|
|
cb88c1cd5d | ||
|
|
ee99452158 | ||
|
|
127a8e80fe | ||
|
|
f9e35627ed | ||
|
|
ceac82f78b | ||
|
|
e5f4927a0b | ||
|
|
cd38fe8cf3 | ||
|
|
fa1382b5cd | ||
|
|
d1e677c52b | ||
|
|
f7439112a5 | ||
|
|
39a14ea7d7 | ||
|
|
c4e7cdb541 | ||
|
|
7d7ae8781e | ||
|
|
724012d3d0 | ||
|
|
664bda02ba | ||
|
|
2d8d5c74b5 | ||
|
|
3cdd49c94a | ||
|
|
10e931da82 | ||
|
|
4b107a8776 | ||
|
|
429761d7d2 | ||
|
|
915c29f8bd | ||
|
|
7ca3c40b50 | ||
|
|
8a3a49250c | ||
|
|
1ec251e091 | ||
|
|
5894ca7b95 | ||
|
|
abf8b1cd4a | ||
|
|
2a307f898b | ||
|
|
f81d0bbd4f | ||
|
|
1c40ff9faa | ||
|
|
7538f3886d | ||
|
|
412e9a4d95 | ||
|
|
25a491fe34 | ||
|
|
b4b616bdf2 | ||
|
|
bb93f04d87 | ||
|
|
975d4e0592 | ||
|
|
04e495403b | ||
|
|
62afbc6bfc | ||
|
|
002beeebe7 | ||
|
|
9329eeac3b | ||
|
|
682efe2678 | ||
|
|
50c7c915ee | ||
|
|
b117bdb344 | ||
|
|
f12ce4029d | ||
|
|
a4762b6ffe | ||
|
|
d4247befa1 | ||
|
|
ee6a4cd1ec | ||
|
|
8a4419b119 | ||
|
|
9a91fe1640 | ||
|
|
2491b87c10 | ||
|
|
0de8191152 | ||
|
|
853311e5b1 | ||
|
|
25a2dac2bc | ||
|
|
e3ce88c9e8 | ||
|
|
5ffcd458f0 | ||
|
|
9b01da97e3 | ||
|
|
258355734d | ||
|
|
d0815046d0 | ||
|
|
3d54b42d59 | ||
|
|
e4f418f07c | ||
|
|
20d42ccaae | ||
|
|
70d6975018 | ||
|
|
00e34375ec | ||
|
|
45a8f1b593 | ||
|
|
8fac494509 | ||
|
|
9b72355f99 | ||
|
|
ddfa1fbccf | ||
|
|
b443145ff3 | ||
|
|
4901853c11 | ||
|
|
a04e0ffdb9 | ||
|
|
007b8c7a01 | ||
|
|
fd0e1f530d | ||
|
|
75620b45ae | ||
|
|
3e88b72b8e | ||
|
|
8b752ddf14 | ||
|
|
1bf4b80f1a | ||
|
|
4c6afbcb01 | ||
|
|
f2206b2abe | ||
|
|
0acd55898d | ||
|
|
15231d4fb2 | ||
|
|
f407b3c4a1 | ||
|
|
a546138d15 | ||
|
|
cd4de92762 | ||
|
|
6b45ccf4ed | ||
|
|
93fd6892f8 | ||
|
|
193bf7919e | ||
|
|
366c855648 | ||
|
|
781219dbe1 | ||
|
|
d71fbc3175 | ||
|
|
c403e456b6 | ||
|
|
6ac7219da3 | ||
|
|
9904c253da | ||
|
|
fb7e5b76c9 | ||
|
|
c1dc08e8e8 | ||
|
|
0f7025dcae | ||
|
|
ef28e5f789 | ||
|
|
ec65ab878e | ||
|
|
e24ce8c2b3 | ||
|
|
a08d82eb13 | ||
|
|
55ac40f859 | ||
|
|
97ef8e7bd4 | ||
|
|
aaf3533653 | ||
|
|
b529aefc53 | ||
|
|
d51022bf9e | ||
|
|
bb7bb5944c | ||
|
|
94b503d95e | ||
|
|
bdc85357aa | ||
|
|
b291008cc2 | ||
|
|
814213b65f | ||
|
|
cbdf4969ec | ||
|
|
262dc5729a | ||
|
|
9d985db7bb | ||
|
|
0897c0a428 | ||
|
|
5edacafcfa | ||
|
|
3507c3380f | ||
|
|
88a50ffa71 | ||
|
|
86a8e74824 | ||
|
|
9fa63a6268 | ||
|
|
0593256707 | ||
|
|
ce6f5502c9 | ||
|
|
ba71060381 | ||
|
|
3823fc6c81 | ||
|
|
52aa2b5d24 | ||
|
|
cc2b66c856 | ||
|
|
65b07dd53d | ||
|
|
7237eb3f1c | ||
|
|
165389b27b | ||
|
|
c8121ce34b | ||
|
|
7360f8d0fd | ||
|
|
6063c47031 | ||
|
|
0c9bec0d38 | ||
|
|
c31d6774ac | ||
|
|
108e0bdc84 | ||
|
|
b34a97a4af | ||
|
|
3e9dbe143d | ||
|
|
4a8e480864 | ||
|
|
17e0c29d9b | ||
|
|
8eb0abc9db | ||
|
|
d862da6d04 | ||
|
|
e288c5a918 | ||
|
|
7827c40c49 | ||
|
|
e7af9cdf0b | ||
|
|
12b6f610b0 | ||
|
|
e81f586001 | ||
|
|
e2cc179454 | ||
|
|
5853c37a83 | ||
|
|
ad0ea7813b | ||
|
|
666e95a66d | ||
|
|
4eda1acafa | ||
|
|
ad73b33224 | ||
|
|
925fe8a0f2 | ||
|
|
789e7acdea | ||
|
|
74b4013538 | ||
|
|
b719ff9399 | ||
|
|
35a2fed2d1 | ||
|
|
63d68bd657 | ||
|
|
842a83f09c | ||
|
|
011850a8f8 | ||
|
|
52b899d60d | ||
|
|
25cd3d377e | ||
|
|
1028f296a8 | ||
|
|
6d042a178f | ||
|
|
eabf425c76 | ||
|
|
3b57e37e48 | ||
|
|
024f9064f1 | ||
|
|
7f4906f565 | ||
|
|
b8a9d14032 | ||
|
|
19afd91687 | ||
|
|
37c215b43f | ||
|
|
9e99f3071d | ||
|
|
fa075b7953 | ||
|
|
08a077d673 | ||
|
|
7923dbbf72 | ||
|
|
43c8e5bded | ||
|
|
b8b709b6d4 | ||
|
|
81e4fce530 | ||
|
|
6baee9ef9d | ||
|
|
edd8589f47 | ||
|
|
14e416355f | ||
|
|
f33cda8d6e | ||
|
|
96f7714237 | ||
|
|
0554581605 | ||
|
|
07a9eab23a | ||
|
|
c815c2f0eb | ||
|
|
9363a8b990 | ||
|
|
5ab6a5756b | ||
|
|
9b4f39ab14 | ||
|
|
f4211a5ea4 | ||
|
|
1b3f507f62 | ||
|
|
6b51133a98 | ||
|
|
cbae016202 | ||
|
|
ab6a949522 | ||
|
|
ea39042e13 | ||
|
|
05ec55f16b | ||
|
|
f623b96932 | ||
|
|
9be74ccc21 | ||
|
|
819bd51d87 | ||
|
|
f8d30826dd | ||
|
|
351ccd7332 | ||
|
|
09f3c2372f | ||
|
|
c197885cb0 | ||
|
|
934e77a286 | ||
|
|
6443185167 | ||
|
|
86e8039a72 | ||
|
|
5a04f1851e | ||
|
|
8c064fdc23 | ||
|
|
8dea54877a | ||
|
|
b5ab31a475 | ||
|
|
140cdcced5 | ||
|
|
7d4c7ae2af | ||
|
|
873588dc5f | ||
|
|
b2f7b3b79f | ||
|
|
540d805226 | ||
|
|
7d6a97e42b | ||
|
|
d70a0c91ad | ||
|
|
3e6818ca87 | ||
|
|
88e5c6b80b | ||
|
|
e33e1bda97 | ||
|
|
5d8b5b9290 | ||
|
|
40565b4a08 | ||
|
|
ff106c028c | ||
|
|
d738c8d18b | ||
|
|
0270c204c2 | ||
|
|
efc7c5d503 | ||
|
|
d324a0ccf9 | ||
|
|
152b51955a | ||
|
|
ec61be9a7e | ||
|
|
f356d5acdd | ||
|
|
76953316d1 | ||
|
|
3d2bd1359d | ||
|
|
8047b2d03e |
375
lapi.c
375
lapi.c
@ -40,10 +40,8 @@ const char lua_ident[] =
|
||||
|
||||
/*
|
||||
** Test for a valid index (one that is not the 'nilvalue').
|
||||
** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
|
||||
** However, it covers the most common cases in a faster way.
|
||||
*/
|
||||
#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
|
||||
#define isvalid(L, o) ((o) != &G(L)->nilvalue)
|
||||
|
||||
|
||||
/* test for pseudo index */
|
||||
@ -92,7 +90,7 @@ static TValue *index2value (lua_State *L, int idx) {
|
||||
/*
|
||||
** Convert a valid actual index (not a pseudo-index) to its address.
|
||||
*/
|
||||
l_sinline StkId index2stack (lua_State *L, int idx) {
|
||||
static StkId index2stack (lua_State *L, int idx) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (idx > 0) {
|
||||
StkId o = ci->func.p + idx;
|
||||
@ -129,7 +127,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
|
||||
int i;
|
||||
if (from == to) return;
|
||||
lua_lock(to);
|
||||
api_checknelems(from, n);
|
||||
api_checkpop(from, n);
|
||||
api_check(from, G(from) == G(to), "moving among independent states");
|
||||
api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
|
||||
from->top.p -= n;
|
||||
@ -195,10 +193,9 @@ LUA_API void lua_settop (lua_State *L, int idx) {
|
||||
api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
|
||||
diff = idx + 1; /* will "subtract" index (as it is negative) */
|
||||
}
|
||||
api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot");
|
||||
newtop = L->top.p + diff;
|
||||
if (diff < 0 && L->tbclist.p >= newtop) {
|
||||
lua_assert(hastocloseCfunc(ci->nresults));
|
||||
lua_assert(ci->callstatus & CIST_TBC);
|
||||
newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
|
||||
}
|
||||
L->top.p = newtop; /* correct top only after closing any upvalue */
|
||||
@ -210,7 +207,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) {
|
||||
StkId level;
|
||||
lua_lock(L);
|
||||
level = index2stack(L, idx);
|
||||
api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level,
|
||||
api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level),
|
||||
"no variable to close at given level");
|
||||
level = luaF_close(L, level, CLOSEKTOP, 0);
|
||||
setnilvalue(s2v(level));
|
||||
@ -224,7 +221,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) {
|
||||
** Note that we move(copy) only the value inside the stack.
|
||||
** (We do not move additional fields that may exist.)
|
||||
*/
|
||||
l_sinline void reverse (lua_State *L, StkId from, StkId to) {
|
||||
static void reverse (lua_State *L, StkId from, StkId to) {
|
||||
for (; from < to; from++, to--) {
|
||||
TValue temp;
|
||||
setobj(L, &temp, s2v(from));
|
||||
@ -243,6 +240,7 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
||||
lua_lock(L);
|
||||
t = L->top.p - 1; /* end of stack segment being rotated */
|
||||
p = index2stack(L, idx); /* start of segment */
|
||||
api_check(L, L->tbclist.p < p, "moving a to-be-closed slot");
|
||||
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
||||
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
||||
reverse(L, p, m); /* reverse the prefix with length 'n' */
|
||||
@ -335,15 +333,15 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
|
||||
LUA_API void lua_arith (lua_State *L, int op) {
|
||||
lua_lock(L);
|
||||
if (op != LUA_OPUNM && op != LUA_OPBNOT)
|
||||
api_checknelems(L, 2); /* all other operations expect two operands */
|
||||
api_checkpop(L, 2); /* all other operations expect two operands */
|
||||
else { /* for unary operations, add fake 2nd operand */
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
setobjs2s(L, L->top.p, L->top.p - 1);
|
||||
api_incr_top(L);
|
||||
}
|
||||
/* first operand at top - 2, second at top - 1; result go to top - 2 */
|
||||
luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
|
||||
L->top.p--; /* remove second operand */
|
||||
L->top.p--; /* pop second operand */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
@ -368,6 +366,18 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API unsigned lua_numbertocstring (lua_State *L, int idx, char *buff) {
|
||||
const TValue *o = index2value(L, idx);
|
||||
if (ttisnumber(o)) {
|
||||
unsigned len = luaO_tostringbuff(o, buff);
|
||||
buff[len++] = '\0'; /* add final zero */
|
||||
return len;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
||||
size_t sz = luaO_str2num(s, s2v(L->top.p));
|
||||
if (sz != 0)
|
||||
@ -416,20 +426,27 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
luaC_checkGC(L);
|
||||
o = index2value(L, idx); /* previous call may reallocate the stack */
|
||||
}
|
||||
if (len != NULL)
|
||||
*len = vslen(o);
|
||||
lua_unlock(L);
|
||||
return svalue(o);
|
||||
if (len != NULL)
|
||||
return getlstr(tsvalue(o), *len);
|
||||
else
|
||||
return getstr(tsvalue(o));
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
|
||||
const TValue *o = index2value(L, idx);
|
||||
switch (ttypetag(o)) {
|
||||
case LUA_VSHRSTR: return tsvalue(o)->shrlen;
|
||||
case LUA_VLNGSTR: return tsvalue(o)->u.lnglen;
|
||||
case LUA_VUSERDATA: return uvalue(o)->len;
|
||||
case LUA_VTABLE: return luaH_getn(hvalue(o));
|
||||
case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen);
|
||||
case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen);
|
||||
case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len);
|
||||
case LUA_VTABLE: {
|
||||
lua_Unsigned res;
|
||||
lua_lock(L);
|
||||
res = luaH_getn(L, hvalue(o));
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
@ -467,7 +484,7 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
|
||||
|
||||
/*
|
||||
** Returns a pointer to the internal representation of an object.
|
||||
** Note that ANSI C does not allow the conversion of a pointer to
|
||||
** Note that ISO C does not allow the conversion of a pointer to
|
||||
** function to a 'void*', so the conversion here goes through
|
||||
** a 'size_t'. (As the returned pointer is only informative, this
|
||||
** conversion should not be a problem.)
|
||||
@ -535,6 +552,21 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_pushexternalstring (lua_State *L,
|
||||
const char *s, size_t len, lua_Alloc falloc, void *ud) {
|
||||
TString *ts;
|
||||
lua_lock(L);
|
||||
api_check(L, len <= MAX_SIZE, "string too large");
|
||||
api_check(L, s[len] == '\0', "string not ending with zero");
|
||||
ts = luaS_newextlstr (L, s, len, falloc, ud);
|
||||
setsvalue2s(L, L->top.p, ts);
|
||||
api_incr_top(L);
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
return getstr(ts);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
||||
lua_lock(L);
|
||||
if (s == NULL)
|
||||
@ -567,9 +599,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
|
||||
const char *ret;
|
||||
va_list argp;
|
||||
lua_lock(L);
|
||||
va_start(argp, fmt);
|
||||
ret = luaO_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
pushvfstring(L, argp, fmt, ret);
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
return ret;
|
||||
@ -583,17 +613,18 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
api_incr_top(L);
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
CClosure *cl;
|
||||
api_checknelems(L, n);
|
||||
api_checkpop(L, n);
|
||||
api_check(L, n <= MAXUPVAL, "upvalue index too large");
|
||||
cl = luaF_newCclosure(L, n);
|
||||
cl->f = fn;
|
||||
L->top.p -= n;
|
||||
while (n--) {
|
||||
setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n));
|
||||
for (i = 0; i < n; i++) {
|
||||
setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i));
|
||||
/* does not need barrier because closure is white */
|
||||
lua_assert(iswhite(cl));
|
||||
}
|
||||
L->top.p -= n;
|
||||
setclCvalue(L, s2v(L->top.p), cl);
|
||||
api_incr_top(L);
|
||||
luaC_checkGC(L);
|
||||
@ -626,7 +657,7 @@ LUA_API int lua_pushthread (lua_State *L) {
|
||||
setthvalue(L, s2v(L->top.p), L);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return (G(L)->mainthread == L);
|
||||
return (mainthread(G(L)) == L);
|
||||
}
|
||||
|
||||
|
||||
@ -636,53 +667,54 @@ LUA_API int lua_pushthread (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
||||
const TValue *slot;
|
||||
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
||||
lu_byte tag;
|
||||
TString *str = luaS_new(L, k);
|
||||
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
||||
setobj2s(L, L->top.p, slot);
|
||||
luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
|
||||
if (!tagisempty(tag))
|
||||
api_incr_top(L);
|
||||
}
|
||||
else {
|
||||
setsvalue2s(L, L->top.p, str);
|
||||
api_incr_top(L);
|
||||
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
|
||||
tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
return novariant(tag);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get the global table in the registry. Since all predefined
|
||||
** indices in the registry were inserted right when the registry
|
||||
** was created and never removed, they must always be in the array
|
||||
** part of the registry.
|
||||
** The following function assumes that the registry cannot be a weak
|
||||
** table; so, an emergency collection while using the global table
|
||||
** cannot collect it.
|
||||
*/
|
||||
#define getGtable(L) \
|
||||
(&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
|
||||
static void getGlobalTable (lua_State *L, TValue *gt) {
|
||||
Table *registry = hvalue(&G(L)->l_registry);
|
||||
lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
|
||||
(void)tag; /* avoid not-used warnings when checks are off */
|
||||
api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
||||
const TValue *G;
|
||||
TValue gt;
|
||||
lua_lock(L);
|
||||
G = getGtable(L);
|
||||
return auxgetstr(L, G, name);
|
||||
getGlobalTable(L, >);
|
||||
return auxgetstr(L, >, name);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_gettable (lua_State *L, int idx) {
|
||||
const TValue *slot;
|
||||
lu_byte tag;
|
||||
TValue *t;
|
||||
lua_lock(L);
|
||||
api_checkpop(L, 1);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
|
||||
setobj2s(L, L->top.p - 1, slot);
|
||||
}
|
||||
else
|
||||
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
|
||||
luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
|
||||
if (tagisempty(tag))
|
||||
tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
return novariant(tag);
|
||||
}
|
||||
|
||||
|
||||
@ -694,35 +726,31 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
|
||||
|
||||
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
|
||||
TValue *t;
|
||||
const TValue *slot;
|
||||
lu_byte tag;
|
||||
lua_lock(L);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastgeti(L, t, n, slot)) {
|
||||
setobj2s(L, L->top.p, slot);
|
||||
}
|
||||
else {
|
||||
TValue aux;
|
||||
setivalue(&aux, n);
|
||||
luaV_finishget(L, t, &aux, L->top.p, slot);
|
||||
luaV_fastgeti(t, n, s2v(L->top.p), tag);
|
||||
if (tagisempty(tag)) {
|
||||
TValue key;
|
||||
setivalue(&key, n);
|
||||
tag = luaV_finishget(L, t, &key, L->top.p, tag);
|
||||
}
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
return novariant(tag);
|
||||
}
|
||||
|
||||
|
||||
l_sinline int finishrawget (lua_State *L, const TValue *val) {
|
||||
if (isempty(val)) /* avoid copying empty items to the stack */
|
||||
static int finishrawget (lua_State *L, lu_byte tag) {
|
||||
if (tagisempty(tag)) /* avoid copying empty items to the stack */
|
||||
setnilvalue(s2v(L->top.p));
|
||||
else
|
||||
setobj2s(L, L->top.p, val);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
return novariant(tag);
|
||||
}
|
||||
|
||||
|
||||
static Table *gettable (lua_State *L, int idx) {
|
||||
l_sinline Table *gettable (lua_State *L, int idx) {
|
||||
TValue *t = index2value(L, idx);
|
||||
api_check(L, ttistable(t), "table expected");
|
||||
return hvalue(t);
|
||||
@ -731,21 +759,23 @@ static Table *gettable (lua_State *L, int idx) {
|
||||
|
||||
LUA_API int lua_rawget (lua_State *L, int idx) {
|
||||
Table *t;
|
||||
const TValue *val;
|
||||
lu_byte tag;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
t = gettable(L, idx);
|
||||
val = luaH_get(t, s2v(L->top.p - 1));
|
||||
L->top.p--; /* remove key */
|
||||
return finishrawget(L, val);
|
||||
tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
|
||||
L->top.p--; /* pop key */
|
||||
return finishrawget(L, tag);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
|
||||
Table *t;
|
||||
lu_byte tag;
|
||||
lua_lock(L);
|
||||
t = gettable(L, idx);
|
||||
return finishrawget(L, luaH_getint(t, n));
|
||||
luaH_fastgeti(t, n, s2v(L->top.p), tag);
|
||||
return finishrawget(L, tag);
|
||||
}
|
||||
|
||||
|
||||
@ -755,7 +785,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
|
||||
lua_lock(L);
|
||||
t = gettable(L, idx);
|
||||
setpvalue(&k, cast_voidp(p));
|
||||
return finishrawget(L, luaH_get(t, &k));
|
||||
return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
|
||||
}
|
||||
|
||||
|
||||
@ -766,7 +796,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
||||
sethvalue2s(L, L->top.p, t);
|
||||
api_incr_top(L);
|
||||
if (narray > 0 || nrec > 0)
|
||||
luaH_resize(L, t, narray, nrec);
|
||||
luaH_resize(L, t, cast_uint(narray), cast_uint(nrec));
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
@ -827,17 +857,18 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
|
||||
** t[k] = value at the top of the stack (where 'k' is a string)
|
||||
*/
|
||||
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
|
||||
const TValue *slot;
|
||||
int hres;
|
||||
TString *str = luaS_new(L, k);
|
||||
api_checknelems(L, 1);
|
||||
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||
api_checkpop(L, 1);
|
||||
luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
|
||||
if (hres == HOK) {
|
||||
luaV_finishfastset(L, t, s2v(L->top.p - 1));
|
||||
L->top.p--; /* pop value */
|
||||
}
|
||||
else {
|
||||
setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
|
||||
api_incr_top(L);
|
||||
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
|
||||
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
|
||||
L->top.p -= 2; /* pop value and key */
|
||||
}
|
||||
lua_unlock(L); /* lock done by caller */
|
||||
@ -845,24 +876,24 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
|
||||
|
||||
|
||||
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
||||
const TValue *G;
|
||||
TValue gt;
|
||||
lua_lock(L); /* unlock done in 'auxsetstr' */
|
||||
G = getGtable(L);
|
||||
auxsetstr(L, G, name);
|
||||
getGlobalTable(L, >);
|
||||
auxsetstr(L, >, name);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settable (lua_State *L, int idx) {
|
||||
TValue *t;
|
||||
const TValue *slot;
|
||||
int hres;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
api_checkpop(L, 2);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||
}
|
||||
luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
|
||||
if (hres == HOK)
|
||||
luaV_finishfastset(L, t, s2v(L->top.p - 1));
|
||||
else
|
||||
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
|
||||
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
|
||||
L->top.p -= 2; /* pop index and value */
|
||||
lua_unlock(L);
|
||||
}
|
||||
@ -876,17 +907,17 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
|
||||
|
||||
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
||||
TValue *t;
|
||||
const TValue *slot;
|
||||
int hres;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastgeti(L, t, n, slot)) {
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||
}
|
||||
luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
|
||||
if (hres == HOK)
|
||||
luaV_finishfastset(L, t, s2v(L->top.p - 1));
|
||||
else {
|
||||
TValue aux;
|
||||
setivalue(&aux, n);
|
||||
luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
|
||||
TValue temp;
|
||||
setivalue(&temp, n);
|
||||
luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
|
||||
}
|
||||
L->top.p--; /* pop value */
|
||||
lua_unlock(L);
|
||||
@ -896,7 +927,7 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
||||
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
|
||||
Table *t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
api_checkpop(L, n);
|
||||
t = gettable(L, idx);
|
||||
luaH_set(L, t, key, s2v(L->top.p - 1));
|
||||
invalidateTMcache(t);
|
||||
@ -921,7 +952,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
||||
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
||||
Table *t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
t = gettable(L, idx);
|
||||
luaH_setint(L, t, n, s2v(L->top.p - 1));
|
||||
luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
|
||||
@ -934,7 +965,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
||||
TValue *obj;
|
||||
Table *mt;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
obj = index2value(L, objindex);
|
||||
if (ttisnil(s2v(L->top.p - 1)))
|
||||
mt = NULL;
|
||||
@ -974,7 +1005,7 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
|
||||
TValue *o;
|
||||
int res;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
o = index2value(L, idx);
|
||||
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
||||
if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
|
||||
@ -996,9 +1027,11 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
|
||||
|
||||
|
||||
#define checkresults(L,na,nr) \
|
||||
api_check(L, (nr) == LUA_MULTRET \
|
||||
(api_check(L, (nr) == LUA_MULTRET \
|
||||
|| (L->ci->top.p - L->top.p >= (nr) - (na)), \
|
||||
"results from function overflow current stack size")
|
||||
"results from function overflow current stack size"), \
|
||||
api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \
|
||||
"invalid number of results"))
|
||||
|
||||
|
||||
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
||||
@ -1007,7 +1040,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
||||
lua_lock(L);
|
||||
api_check(L, k == NULL || !isLua(L->ci),
|
||||
"cannot use continuations inside hooks");
|
||||
api_checknelems(L, nargs+1);
|
||||
api_checkpop(L, nargs + 1);
|
||||
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
||||
checkresults(L, nargs, nresults);
|
||||
func = L->top.p - (nargs+1);
|
||||
@ -1043,12 +1076,12 @@ static void f_call (lua_State *L, void *ud) {
|
||||
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
lua_KContext ctx, lua_KFunction k) {
|
||||
struct CallS c;
|
||||
int status;
|
||||
TStatus status;
|
||||
ptrdiff_t func;
|
||||
lua_lock(L);
|
||||
api_check(L, k == NULL || !isLua(L->ci),
|
||||
"cannot use continuations inside hooks");
|
||||
api_checknelems(L, nargs+1);
|
||||
api_checkpop(L, nargs + 1);
|
||||
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
||||
checkresults(L, nargs, nresults);
|
||||
if (errfunc == 0)
|
||||
@ -1071,7 +1104,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
ci->u2.funcidx = cast_int(savestack(L, c.func));
|
||||
ci->u.c.old_errfunc = L->errfunc;
|
||||
L->errfunc = func;
|
||||
setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
|
||||
setoah(ci, L->allowhook); /* save value of 'allowhook' */
|
||||
ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
|
||||
luaD_call(L, c.func, nresults); /* do the call */
|
||||
ci->callstatus &= ~CIST_YPCALL;
|
||||
@ -1080,14 +1113,14 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
}
|
||||
adjustresults(L, nresults);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
return APIstatus(status);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
||||
const char *chunkname, const char *mode) {
|
||||
ZIO z;
|
||||
int status;
|
||||
TStatus status;
|
||||
lua_lock(L);
|
||||
if (!chunkname) chunkname = "?";
|
||||
luaZ_init(L, &z, reader, data);
|
||||
@ -1096,34 +1129,38 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
||||
LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
|
||||
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
||||
/* get global table from registry */
|
||||
const TValue *gt = getGtable(L);
|
||||
TValue gt;
|
||||
getGlobalTable(L, >);
|
||||
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
||||
setobj(L, f->upvals[0]->v.p, gt);
|
||||
luaC_barrier(L, f->upvals[0], gt);
|
||||
setobj(L, f->upvals[0]->v.p, >);
|
||||
luaC_barrier(L, f->upvals[0], >);
|
||||
}
|
||||
}
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
return APIstatus(status);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Dump a Lua function, calling 'writer' to write its parts. Ensure
|
||||
** the stack returns with its original size.
|
||||
*/
|
||||
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
|
||||
int status;
|
||||
TValue *o;
|
||||
ptrdiff_t otop = savestack(L, L->top.p); /* original top */
|
||||
TValue *f = s2v(L->top.p - 1); /* function to be dumped */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
o = s2v(L->top.p - 1);
|
||||
if (isLfunction(o))
|
||||
status = luaU_dump(L, getproto(o), writer, data, strip);
|
||||
else
|
||||
status = 1;
|
||||
api_checkpop(L, 1);
|
||||
api_check(L, isLfunction(f), "Lua function expected");
|
||||
status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
|
||||
L->top.p = restorestack(L, otop); /* restore top */
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_status (lua_State *L) {
|
||||
return L->status;
|
||||
return APIstatus(L->status);
|
||||
}
|
||||
|
||||
|
||||
@ -1134,7 +1171,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
||||
va_list argp;
|
||||
int res = 0;
|
||||
global_State *g = G(L);
|
||||
if (g->gcstp & GCSTPGC) /* internal stop? */
|
||||
if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */
|
||||
return -1; /* all options are invalid when stopped */
|
||||
lua_lock(L);
|
||||
va_start(argp, what);
|
||||
@ -1145,7 +1182,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
||||
}
|
||||
case LUA_GCRESTART: {
|
||||
luaE_setdebt(g, 0);
|
||||
g->gcstp = 0; /* (GCSTPGC must be already zero here) */
|
||||
g->gcstp = 0; /* (other bits must be zero here) */
|
||||
break;
|
||||
}
|
||||
case LUA_GCCOLLECT: {
|
||||
@ -1162,34 +1199,22 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
||||
break;
|
||||
}
|
||||
case LUA_GCSTEP: {
|
||||
int data = va_arg(argp, int);
|
||||
l_mem debt = 1; /* =1 to signal that it did an actual step */
|
||||
lu_byte oldstp = g->gcstp;
|
||||
g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */
|
||||
if (data == 0) {
|
||||
luaE_setdebt(g, 0); /* do a basic step */
|
||||
luaC_step(L);
|
||||
}
|
||||
else { /* add 'data' to total debt */
|
||||
debt = cast(l_mem, data) * 1024 + g->GCdebt;
|
||||
luaE_setdebt(g, debt);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
g->gcstp = oldstp; /* restore previous state */
|
||||
if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
|
||||
l_mem n = cast(l_mem, va_arg(argp, size_t));
|
||||
l_mem newdebt;
|
||||
int work = 0; /* true if GC did some work */
|
||||
g->gcstp = 0; /* allow GC to run (other bits must be zero here) */
|
||||
if (n <= 0)
|
||||
newdebt = 0; /* force to run one basic step */
|
||||
else if (g->GCdebt >= n - MAX_LMEM) /* no overflow? */
|
||||
newdebt = g->GCdebt - n;
|
||||
else /* overflow */
|
||||
newdebt = -MAX_LMEM; /* set debt to miminum value */
|
||||
luaE_setdebt(g, newdebt);
|
||||
luaC_condGC(L, (void)0, work = 1);
|
||||
if (work && g->gcstate == GCSpause) /* end of cycle? */
|
||||
res = 1; /* signal it */
|
||||
break;
|
||||
}
|
||||
case LUA_GCSETPAUSE: {
|
||||
int data = va_arg(argp, int);
|
||||
res = getgcparam(g->gcpause);
|
||||
setgcparam(g->gcpause, data);
|
||||
break;
|
||||
}
|
||||
case LUA_GCSETSTEPMUL: {
|
||||
int data = va_arg(argp, int);
|
||||
res = getgcparam(g->gcstepmul);
|
||||
setgcparam(g->gcstepmul, data);
|
||||
g->gcstp = oldstp; /* restore previous state */
|
||||
break;
|
||||
}
|
||||
case LUA_GCISRUNNING: {
|
||||
@ -1197,30 +1222,24 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
||||
break;
|
||||
}
|
||||
case LUA_GCGEN: {
|
||||
int minormul = va_arg(argp, int);
|
||||
int majormul = va_arg(argp, int);
|
||||
res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
|
||||
if (minormul != 0)
|
||||
g->genminormul = minormul;
|
||||
if (majormul != 0)
|
||||
setgcparam(g->genmajormul, majormul);
|
||||
luaC_changemode(L, KGC_GEN);
|
||||
res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
|
||||
luaC_changemode(L, KGC_GENMINOR);
|
||||
break;
|
||||
}
|
||||
case LUA_GCINC: {
|
||||
int pause = va_arg(argp, int);
|
||||
int stepmul = va_arg(argp, int);
|
||||
int stepsize = va_arg(argp, int);
|
||||
res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
|
||||
if (pause != 0)
|
||||
setgcparam(g->gcpause, pause);
|
||||
if (stepmul != 0)
|
||||
setgcparam(g->gcstepmul, stepmul);
|
||||
if (stepsize != 0)
|
||||
g->gcstepsize = stepsize;
|
||||
res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
|
||||
luaC_changemode(L, KGC_INC);
|
||||
break;
|
||||
}
|
||||
case LUA_GCPARAM: {
|
||||
int param = va_arg(argp, int);
|
||||
int value = va_arg(argp, int);
|
||||
api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
|
||||
res = cast_int(luaO_applyparam(g->gcparams[param], 100));
|
||||
if (value >= 0)
|
||||
g->gcparams[param] = luaO_codeparam(cast_uint(value));
|
||||
break;
|
||||
}
|
||||
default: res = -1; /* invalid option */
|
||||
}
|
||||
va_end(argp);
|
||||
@ -1239,7 +1258,7 @@ LUA_API int lua_error (lua_State *L) {
|
||||
TValue *errobj;
|
||||
lua_lock(L);
|
||||
errobj = s2v(L->top.p - 1);
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
/* error object is the memory error message? */
|
||||
if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
|
||||
luaM_error(L); /* raise a memory error */
|
||||
@ -1254,30 +1273,25 @@ LUA_API int lua_next (lua_State *L, int idx) {
|
||||
Table *t;
|
||||
int more;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
api_checkpop(L, 1);
|
||||
t = gettable(L, idx);
|
||||
more = luaH_next(L, t, L->top.p - 1);
|
||||
if (more) {
|
||||
if (more)
|
||||
api_incr_top(L);
|
||||
}
|
||||
else /* no more elements */
|
||||
L->top.p -= 1; /* remove key */
|
||||
L->top.p--; /* pop key */
|
||||
lua_unlock(L);
|
||||
return more;
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_toclose (lua_State *L, int idx) {
|
||||
int nresults;
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
o = index2stack(L, idx);
|
||||
nresults = L->ci->nresults;
|
||||
api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
|
||||
luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
|
||||
if (!hastocloseCfunc(nresults)) /* function not marked yet? */
|
||||
L->ci->nresults = codeNresults(nresults); /* mark it */
|
||||
lua_assert(hastocloseCfunc(L->ci->nresults));
|
||||
L->ci->callstatus |= CIST_TBC; /* mark that function has TBC slots */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
@ -1285,13 +1299,14 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
|
||||
LUA_API void lua_concat (lua_State *L, int n) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
if (n > 0)
|
||||
if (n > 0) {
|
||||
luaV_concat(L, n);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
else { /* nothing to concatenate */
|
||||
setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
|
||||
api_incr_top(L);
|
||||
}
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
@ -1343,8 +1358,8 @@ void lua_warning (lua_State *L, const char *msg, int tocont) {
|
||||
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
||||
Udata *u;
|
||||
lua_lock(L);
|
||||
api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
|
||||
u = luaS_newudata(L, size, nuvalue);
|
||||
api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
|
||||
u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
|
||||
setuvalue(L, s2v(L->top.p), u);
|
||||
api_incr_top(L);
|
||||
luaC_checkGC(L);
|
||||
|
||||
49
lapi.h
49
lapi.h
@ -12,10 +12,29 @@
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#if defined(LUA_USE_APICHECK)
|
||||
#include <assert.h>
|
||||
#define api_check(l,e,msg) assert(e)
|
||||
#else /* for testing */
|
||||
#define api_check(l,e,msg) ((void)(l), lua_assert((e) && msg))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Increments 'L->top.p', checking for stack overflows */
|
||||
#define api_incr_top(L) {L->top.p++; \
|
||||
api_check(L, L->top.p <= L->ci->top.p, \
|
||||
"stack overflow");}
|
||||
#define api_incr_top(L) \
|
||||
(L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow"))
|
||||
|
||||
|
||||
/*
|
||||
** macros that are executed whenever program enters the Lua core
|
||||
** ('lua_lock') and leaves the core ('lua_unlock')
|
||||
*/
|
||||
#if !defined(lua_lock)
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@ -30,23 +49,17 @@
|
||||
|
||||
/* Ensure the stack has at least 'n' elements */
|
||||
#define api_checknelems(L,n) \
|
||||
api_check(L, (n) < (L->top.p - L->ci->func.p), \
|
||||
"not enough elements in the stack")
|
||||
api_check(L, (n) < (L->top.p - L->ci->func.p), \
|
||||
"not enough elements in the stack")
|
||||
|
||||
|
||||
/*
|
||||
** To reduce the overhead of returning from C functions, the presence of
|
||||
** to-be-closed variables in these functions is coded in the CallInfo's
|
||||
** field 'nresults', in a way that functions with no to-be-closed variables
|
||||
** with zero, one, or "all" wanted results have no overhead. Functions
|
||||
** with other number of wanted results, as well as functions with
|
||||
** variables to be closed, have an extra check.
|
||||
/* Ensure the stack has at least 'n' elements to be popped. (Some
|
||||
** functions only update a slot after checking it for popping, but that
|
||||
** is only an optimization for a pop followed by a push.)
|
||||
*/
|
||||
|
||||
#define hastocloseCfunc(n) ((n) < LUA_MULTRET)
|
||||
|
||||
/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
|
||||
#define codeNresults(n) (-(n) - 3)
|
||||
#define decodeNresults(n) (-(n) - 3)
|
||||
#define api_checkpop(L,n) \
|
||||
api_check(L, (n) < L->top.p - L->ci->func.p && \
|
||||
L->tbclist.p < L->top.p - (n), \
|
||||
"not enough free elements in the stack")
|
||||
|
||||
#endif
|
||||
|
||||
240
lauxlib.c
240
lauxlib.c
@ -25,12 +25,7 @@
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
|
||||
|
||||
#if !defined(MAX_SIZET)
|
||||
/* maximum value for size_t */
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0))
|
||||
#endif
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -80,6 +75,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
||||
int top = lua_gettop(L);
|
||||
lua_getinfo(L, "f", ar); /* push function */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
|
||||
luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */
|
||||
if (findfield(L, top + 1, 2)) {
|
||||
const char *name = lua_tostring(L, -1);
|
||||
if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */
|
||||
@ -98,14 +94,14 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
||||
|
||||
|
||||
static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
||||
if (pushglobalfuncname(L, ar)) { /* try first a global name */
|
||||
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
||||
lua_remove(L, -2); /* remove name */
|
||||
}
|
||||
else if (*ar->namewhat != '\0') /* is there a name from code? */
|
||||
if (*ar->namewhat != '\0') /* is there a name from code? */
|
||||
lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */
|
||||
else if (*ar->what == 'm') /* main? */
|
||||
lua_pushliteral(L, "main chunk");
|
||||
else if (pushglobalfuncname(L, ar)) { /* try a global name */
|
||||
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
||||
lua_remove(L, -2); /* remove name */
|
||||
}
|
||||
else if (*ar->what != 'C') /* for Lua functions, use <file:line> */
|
||||
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
|
||||
else /* nothing left... */
|
||||
@ -174,19 +170,27 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
|
||||
|
||||
LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
const char *argword;
|
||||
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
|
||||
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
|
||||
lua_getinfo(L, "n", &ar);
|
||||
if (strcmp(ar.namewhat, "method") == 0) {
|
||||
arg--; /* do not count 'self' */
|
||||
if (arg == 0) /* error is in the self argument itself? */
|
||||
return luaL_error(L, "calling '%s' on bad self (%s)",
|
||||
ar.name, extramsg);
|
||||
lua_getinfo(L, "nt", &ar);
|
||||
if (arg <= ar.extraargs) /* error in an extra argument? */
|
||||
argword = "extra argument";
|
||||
else {
|
||||
arg -= ar.extraargs; /* do not count extra arguments */
|
||||
if (strcmp(ar.namewhat, "method") == 0) { /* colon syntax? */
|
||||
arg--; /* do not count (extra) self argument */
|
||||
if (arg == 0) /* error in self argument? */
|
||||
return luaL_error(L, "calling '%s' on bad self (%s)",
|
||||
ar.name, extramsg);
|
||||
/* else go through; error in a regular argument */
|
||||
}
|
||||
argword = "argument";
|
||||
}
|
||||
if (ar.name == NULL)
|
||||
ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
|
||||
return luaL_error(L, "bad argument #%d to '%s' (%s)",
|
||||
arg, ar.name, extramsg);
|
||||
return luaL_error(L, "bad %s #%d to '%s' (%s)",
|
||||
argword, arg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
@ -229,7 +233,7 @@ LUALIB_API void luaL_where (lua_State *L, int level) {
|
||||
/*
|
||||
** Again, the use of 'lua_pushvfstring' ensures this function does
|
||||
** not need reserved stack space when called. (At worst, it generates
|
||||
** an error with "stack overflow" instead of the given message.)
|
||||
** a memory error instead of the given message.)
|
||||
*/
|
||||
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
|
||||
va_list argp;
|
||||
@ -249,11 +253,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
const char *msg;
|
||||
luaL_pushfail(L);
|
||||
msg = (en != 0) ? strerror(en) : "(no extra info)";
|
||||
if (fname)
|
||||
lua_pushfstring(L, "%s: %s", fname, strerror(en));
|
||||
lua_pushfstring(L, "%s: %s", fname, msg);
|
||||
else
|
||||
lua_pushstring(L, strerror(en));
|
||||
lua_pushstring(L, msg);
|
||||
lua_pushinteger(L, en);
|
||||
return 3;
|
||||
}
|
||||
@ -470,18 +476,27 @@ typedef struct UBox {
|
||||
} UBox;
|
||||
|
||||
|
||||
/* Resize the buffer used by a box. Optimize for the common case of
|
||||
** resizing to the old size. (For instance, __gc will resize the box
|
||||
** to 0 even after it was closed. 'pushresult' may also resize it to a
|
||||
** final size that is equal to the one set when the buffer was created.)
|
||||
*/
|
||||
static void *resizebox (lua_State *L, int idx, size_t newsize) {
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud);
|
||||
UBox *box = (UBox *)lua_touserdata(L, idx);
|
||||
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
||||
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
|
||||
lua_pushliteral(L, "not enough memory");
|
||||
lua_error(L); /* raise a memory error */
|
||||
if (box->bsize == newsize) /* not changing size? */
|
||||
return box->box; /* keep the buffer */
|
||||
else {
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud);
|
||||
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
||||
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
|
||||
lua_pushliteral(L, "not enough memory");
|
||||
lua_error(L); /* raise a memory error */
|
||||
}
|
||||
box->box = temp;
|
||||
box->bsize = newsize;
|
||||
return temp;
|
||||
}
|
||||
box->box = temp;
|
||||
box->bsize = newsize;
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
@ -526,15 +541,17 @@ static void newbox (lua_State *L) {
|
||||
|
||||
/*
|
||||
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
|
||||
** bytes. (The test for "not big enough" also gets the case when the
|
||||
** computation of 'newsize' overflows.)
|
||||
** bytes plus one for a terminating zero.
|
||||
*/
|
||||
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
|
||||
size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */
|
||||
if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
|
||||
return luaL_error(B->L, "buffer too large");
|
||||
if (newsize < B->n + sz) /* not big enough? */
|
||||
newsize = B->n + sz;
|
||||
size_t newsize = B->size;
|
||||
if (l_unlikely(sz >= MAX_SIZE - B->n))
|
||||
return cast_sizet(luaL_error(B->L, "resulting string too large"));
|
||||
/* else B->n + sz + 1 <= MAX_SIZE */
|
||||
if (newsize <= MAX_SIZE/3 * 2) /* no overflow? */
|
||||
newsize += (newsize >> 1); /* new size *= 1.5 */
|
||||
if (newsize < B->n + sz + 1) /* not big enough? */
|
||||
newsize = B->n + sz + 1;
|
||||
return newsize;
|
||||
}
|
||||
|
||||
@ -594,9 +611,23 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||
lua_State *L = B->L;
|
||||
checkbufferlevel(B, -1);
|
||||
lua_pushlstring(L, B->b, B->n);
|
||||
if (buffonstack(B))
|
||||
if (!buffonstack(B)) /* using static buffer? */
|
||||
lua_pushlstring(L, B->b, B->n); /* save result as regular string */
|
||||
else { /* reuse buffer already allocated */
|
||||
UBox *box = (UBox *)lua_touserdata(L, -1);
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud); /* function to free buffer */
|
||||
size_t len = B->n; /* final string length */
|
||||
char *s;
|
||||
resizebox(L, -1, len + 1); /* adjust box size to content size */
|
||||
s = (char*)box->box; /* final buffer address */
|
||||
s[len] = '\0'; /* add ending zero */
|
||||
/* clear box, as Lua will take control of the buffer */
|
||||
box->bsize = 0; box->box = NULL;
|
||||
lua_pushexternalstring(L, s, len, allocf, ud);
|
||||
lua_closeslot(L, -2); /* close the box */
|
||||
lua_gc(L, LUA_GCSTEP, len);
|
||||
}
|
||||
lua_remove(L, -2); /* remove box or placeholder from the stack */
|
||||
}
|
||||
|
||||
@ -650,13 +681,10 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/* index of free-list header (after the predefined values) */
|
||||
#define freelist (LUA_RIDX_LAST + 1)
|
||||
|
||||
/*
|
||||
** The previously freed references form a linked list:
|
||||
** t[freelist] is the index of a first free index, or zero if list is
|
||||
** empty; t[t[freelist]] is the index of the second element; etc.
|
||||
** The previously freed references form a linked list: t[1] is the index
|
||||
** of a first free index, t[t[1]] is the index of the second element,
|
||||
** etc. A zero signals the end of the list.
|
||||
*/
|
||||
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
int ref;
|
||||
@ -665,19 +693,18 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
|
||||
}
|
||||
t = lua_absindex(L, t);
|
||||
if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */
|
||||
if (lua_rawgeti(L, t, 1) == LUA_TNUMBER) /* already initialized? */
|
||||
ref = (int)lua_tointeger(L, -1); /* ref = t[1] */
|
||||
else { /* first access */
|
||||
lua_assert(!lua_toboolean(L, -1)); /* must be nil or false */
|
||||
ref = 0; /* list is empty */
|
||||
lua_pushinteger(L, 0); /* initialize as an empty list */
|
||||
lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */
|
||||
}
|
||||
else { /* already initialized */
|
||||
lua_assert(lua_isinteger(L, -1));
|
||||
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
|
||||
lua_rawseti(L, t, 1); /* ref = t[1] = 0 */
|
||||
}
|
||||
lua_pop(L, 1); /* remove element from stack */
|
||||
if (ref != 0) { /* any free element? */
|
||||
lua_rawgeti(L, t, ref); /* remove it from list */
|
||||
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
|
||||
lua_rawseti(L, t, 1); /* (t[1] = t[ref]) */
|
||||
}
|
||||
else /* no free elements */
|
||||
ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
|
||||
@ -689,11 +716,11 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
||||
if (ref >= 0) {
|
||||
t = lua_absindex(L, t);
|
||||
lua_rawgeti(L, t, freelist);
|
||||
lua_rawgeti(L, t, 1);
|
||||
lua_assert(lua_isinteger(L, -1));
|
||||
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
|
||||
lua_rawseti(L, t, ref); /* t[ref] = t[1] */
|
||||
lua_pushinteger(L, ref);
|
||||
lua_rawseti(L, t, freelist); /* t[freelist] = ref */
|
||||
lua_rawseti(L, t, 1); /* t[1] = ref */
|
||||
}
|
||||
}
|
||||
|
||||
@ -707,7 +734,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
||||
*/
|
||||
|
||||
typedef struct LoadF {
|
||||
int n; /* number of pre-read characters */
|
||||
unsigned n; /* number of pre-read characters */
|
||||
FILE *f; /* file being read */
|
||||
char buff[BUFSIZ]; /* area for reading file */
|
||||
} LoadF;
|
||||
@ -715,7 +742,7 @@ typedef struct LoadF {
|
||||
|
||||
static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||
LoadF *lf = (LoadF *)ud;
|
||||
(void)L; /* not used */
|
||||
UNUSED(L);
|
||||
if (lf->n > 0) { /* are there pre-read characters to be read? */
|
||||
*size = lf->n; /* return them (chars already in buffer) */
|
||||
lf->n = 0; /* no more pre-read characters */
|
||||
@ -732,9 +759,12 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||
|
||||
|
||||
static int errfile (lua_State *L, const char *what, int fnameindex) {
|
||||
const char *serr = strerror(errno);
|
||||
int err = errno;
|
||||
const char *filename = lua_tostring(L, fnameindex) + 1;
|
||||
lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
|
||||
if (err != 0)
|
||||
lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err));
|
||||
else
|
||||
lua_pushfstring(L, "cannot %s %s", what, filename);
|
||||
lua_remove(L, fnameindex);
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
@ -787,6 +817,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
||||
}
|
||||
else {
|
||||
lua_pushfstring(L, "@%s", filename);
|
||||
errno = 0;
|
||||
lf.f = fopen(filename, "r");
|
||||
if (lf.f == NULL) return errfile(L, "open", fnameindex);
|
||||
}
|
||||
@ -796,15 +827,17 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
||||
if (c == LUA_SIGNATURE[0]) { /* binary file? */
|
||||
lf.n = 0; /* remove possible newline */
|
||||
if (filename) { /* "real" file? */
|
||||
errno = 0;
|
||||
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
|
||||
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
|
||||
skipcomment(lf.f, &c); /* re-read initial portion */
|
||||
}
|
||||
}
|
||||
if (c != EOF)
|
||||
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
|
||||
lf.buff[lf.n++] = cast_char(c); /* 'c' is the first character */
|
||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
|
||||
readstatus = ferror(lf.f);
|
||||
errno = 0; /* no useful error number until here */
|
||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||
if (readstatus) {
|
||||
lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
|
||||
@ -823,7 +856,7 @@ typedef struct LoadS {
|
||||
|
||||
static const char *getS (lua_State *L, void *ud, size_t *size) {
|
||||
LoadS *ls = (LoadS *)ud;
|
||||
(void)L; /* not used */
|
||||
UNUSED(L);
|
||||
if (ls->size == 0) return NULL;
|
||||
*size = ls->size;
|
||||
ls->size = 0;
|
||||
@ -895,10 +928,9 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
else {
|
||||
switch (lua_type(L, idx)) {
|
||||
case LUA_TNUMBER: {
|
||||
if (lua_isinteger(L, idx))
|
||||
lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx));
|
||||
else
|
||||
lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
|
||||
char buff[LUA_N2SBUFFSZ];
|
||||
lua_numbertocstring(L, idx, buff);
|
||||
lua_pushstring(L, buff);
|
||||
break;
|
||||
}
|
||||
case LUA_TSTRING:
|
||||
@ -933,7 +965,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||
luaL_checkstack(L, nup, "too many upvalues");
|
||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||
if (l->func == NULL) /* place holder? */
|
||||
if (l->func == NULL) /* placeholder? */
|
||||
lua_pushboolean(L, 0);
|
||||
else {
|
||||
int i;
|
||||
@ -996,7 +1028,7 @@ LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
|
||||
const char *wild;
|
||||
size_t l = strlen(p);
|
||||
while ((wild = strstr(s, p)) != NULL) {
|
||||
luaL_addlstring(b, s, wild - s); /* push prefix */
|
||||
luaL_addlstring(b, s, ct_diff2sz(wild - s)); /* push prefix */
|
||||
luaL_addstring(b, r); /* push replacement in place of pattern */
|
||||
s = wild + l; /* continue after 'p' */
|
||||
}
|
||||
@ -1014,8 +1046,8 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
|
||||
}
|
||||
|
||||
|
||||
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
(void)ud; (void)osize; /* not used */
|
||||
void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
UNUSED(ud); UNUSED(osize);
|
||||
if (nsize == 0) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
@ -1025,9 +1057,14 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Standard panic function just prints an error message. The test
|
||||
** with 'lua_type' avoids possible memory errors in 'lua_tostring'.
|
||||
*/
|
||||
static int panic (lua_State *L) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "error object is not a string";
|
||||
const char *msg = (lua_type(L, -1) == LUA_TSTRING)
|
||||
? lua_tostring(L, -1)
|
||||
: "error object is not a string";
|
||||
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
msg);
|
||||
return 0; /* return to Lua to abort */
|
||||
@ -1091,11 +1128,64 @@ static void warnfon (void *ud, const char *message, int tocont) {
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_State *luaL_newstate (void) {
|
||||
lua_State *L = lua_newstate(l_alloc, NULL);
|
||||
|
||||
/*
|
||||
** A function to compute an unsigned int with some level of
|
||||
** randomness. Rely on Address Space Layout Randomization (if present)
|
||||
** and the current time.
|
||||
*/
|
||||
#if !defined(luai_makeseed)
|
||||
|
||||
#include <time.h>
|
||||
|
||||
|
||||
/* Size for the buffer, in bytes */
|
||||
#define BUFSEEDB (sizeof(void*) + sizeof(time_t))
|
||||
|
||||
/* Size for the buffer in int's, rounded up */
|
||||
#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int))
|
||||
|
||||
/*
|
||||
** Copy the contents of variable 'v' into the buffer pointed by 'b'.
|
||||
** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.)
|
||||
*/
|
||||
#define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v))
|
||||
|
||||
|
||||
static unsigned int luai_makeseed (void) {
|
||||
unsigned int buff[BUFSEED];
|
||||
unsigned int res;
|
||||
unsigned int i;
|
||||
time_t t = time(NULL);
|
||||
char *b = (char*)buff;
|
||||
addbuff(b, b); /* local variable's address */
|
||||
addbuff(b, t); /* time */
|
||||
/* fill (rare but possible) remain of the buffer with zeros */
|
||||
memset(b, 0, sizeof(buff) - BUFSEEDB);
|
||||
res = buff[0];
|
||||
for (i = 1; i < BUFSEED; i++)
|
||||
res ^= (res >> 3) + (res << 7) + buff[i];
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
|
||||
UNUSED(L);
|
||||
return luai_makeseed();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Use the name with parentheses so that headers can redefine it
|
||||
** as a macro.
|
||||
*/
|
||||
LUALIB_API lua_State *(luaL_newstate) (void) {
|
||||
lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL));
|
||||
if (l_likely(L)) {
|
||||
lua_atpanic(L, &panic);
|
||||
lua_setwarnf(L, warnfoff, L); /* default is warnings off */
|
||||
lua_setwarnf(L, warnfon, L);
|
||||
}
|
||||
return L;
|
||||
}
|
||||
|
||||
46
lauxlib.h
46
lauxlib.h
@ -81,6 +81,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
|
||||
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
|
||||
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
|
||||
|
||||
LUALIB_API void *(luaL_alloc) (void *ud, void *ptr, size_t osize,
|
||||
size_t nsize);
|
||||
|
||||
|
||||
/* predefined references */
|
||||
#define LUA_NOREF (-2)
|
||||
@ -100,6 +103,8 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
|
||||
|
||||
LUALIB_API lua_State *(luaL_newstate) (void);
|
||||
|
||||
LUALIB_API unsigned (luaL_makeseed) (lua_State *L);
|
||||
|
||||
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
|
||||
|
||||
LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
|
||||
@ -163,21 +168,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
||||
|
||||
|
||||
/* push the value used to represent failure/error */
|
||||
#define luaL_pushfail(L) lua_pushnil(L)
|
||||
|
||||
|
||||
/*
|
||||
** Internal assertions for in-house debugging
|
||||
*/
|
||||
#if !defined(lua_assert)
|
||||
|
||||
#if defined LUAI_ASSERT
|
||||
#include <assert.h>
|
||||
#define lua_assert(c) assert(c)
|
||||
#if defined(LUA_FAILISFALSE)
|
||||
#define luaL_pushfail(L) lua_pushboolean(L, 0)
|
||||
#else
|
||||
#define lua_assert(c) ((void)0)
|
||||
#endif
|
||||
|
||||
#define luaL_pushfail(L) lua_pushnil(L)
|
||||
#endif
|
||||
|
||||
|
||||
@ -249,30 +243,6 @@ typedef struct luaL_Stream {
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** "Abstraction Layer" for basic report of messages and errors
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/* print a string */
|
||||
#if !defined(lua_writestring)
|
||||
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
||||
#endif
|
||||
|
||||
/* print a newline and flush the output */
|
||||
#if !defined(lua_writeline)
|
||||
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
||||
#endif
|
||||
|
||||
/* print an error message */
|
||||
#if !defined(lua_writestringerror)
|
||||
#define lua_writestringerror(s,p) \
|
||||
(fprintf(stderr, (s), (p)), fflush(stderr))
|
||||
#endif
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {============================================================
|
||||
|
||||
84
lbaselib.c
84
lbaselib.c
@ -19,6 +19,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
static int luaB_print (lua_State *L) {
|
||||
@ -57,21 +58,22 @@ static int luaB_warn (lua_State *L) {
|
||||
|
||||
#define SPACECHARS " \f\n\r\t\v"
|
||||
|
||||
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
|
||||
static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) {
|
||||
lua_Unsigned n = 0;
|
||||
int neg = 0;
|
||||
s += strspn(s, SPACECHARS); /* skip initial spaces */
|
||||
if (*s == '-') { s++; neg = 1; } /* handle sign */
|
||||
else if (*s == '+') s++;
|
||||
if (!isalnum((unsigned char)*s)) /* no digit? */
|
||||
if (!isalnum(cast_uchar(*s))) /* no digit? */
|
||||
return NULL;
|
||||
do {
|
||||
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
||||
: (toupper((unsigned char)*s) - 'A') + 10;
|
||||
unsigned digit = cast_uint(isdigit(cast_uchar(*s))
|
||||
? *s - '0'
|
||||
: (toupper(cast_uchar(*s)) - 'A') + 10);
|
||||
if (digit >= base) return NULL; /* invalid numeral */
|
||||
n = n * base + digit;
|
||||
s++;
|
||||
} while (isalnum((unsigned char)*s));
|
||||
} while (isalnum(cast_uchar(*s)));
|
||||
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
||||
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
||||
return s;
|
||||
@ -101,7 +103,7 @@ static int luaB_tonumber (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
|
||||
s = lua_tolstring(L, 1, &l);
|
||||
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
if (b_str2int(s, (int)base, &n) == s + l) {
|
||||
if (b_str2int(s, cast_uint(base), &n) == s + l) {
|
||||
lua_pushinteger(L, n);
|
||||
return 1;
|
||||
} /* else not a number */
|
||||
@ -158,7 +160,7 @@ static int luaB_rawlen (lua_State *L) {
|
||||
int t = lua_type(L, 1);
|
||||
luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
|
||||
"table or string");
|
||||
lua_pushinteger(L, lua_rawlen(L, 1));
|
||||
lua_pushinteger(L, l_castU2S(lua_rawlen(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -198,11 +200,11 @@ static int pushmode (lua_State *L, int oldmode) {
|
||||
|
||||
static int luaB_collectgarbage (lua_State *L) {
|
||||
static const char *const opts[] = {"stop", "restart", "collect",
|
||||
"count", "step", "setpause", "setstepmul",
|
||||
"isrunning", "generational", "incremental", NULL};
|
||||
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
|
||||
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
|
||||
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
|
||||
"count", "step", "isrunning", "generational", "incremental",
|
||||
"param", NULL};
|
||||
static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
|
||||
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC,
|
||||
LUA_GCPARAM};
|
||||
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
|
||||
switch (o) {
|
||||
case LUA_GCCOUNT: {
|
||||
@ -213,20 +215,12 @@ static int luaB_collectgarbage (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCSTEP: {
|
||||
int step = (int)luaL_optinteger(L, 2, 0);
|
||||
int res = lua_gc(L, o, step);
|
||||
lua_Integer n = luaL_optinteger(L, 2, 0);
|
||||
int res = lua_gc(L, o, cast_sizet(n));
|
||||
checkvalres(res);
|
||||
lua_pushboolean(L, res);
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCSETPAUSE:
|
||||
case LUA_GCSETSTEPMUL: {
|
||||
int p = (int)luaL_optinteger(L, 2, 0);
|
||||
int previous = lua_gc(L, o, p);
|
||||
checkvalres(previous);
|
||||
lua_pushinteger(L, previous);
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCISRUNNING: {
|
||||
int res = lua_gc(L, o);
|
||||
checkvalres(res);
|
||||
@ -234,15 +228,22 @@ static int luaB_collectgarbage (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
case LUA_GCGEN: {
|
||||
int minormul = (int)luaL_optinteger(L, 2, 0);
|
||||
int majormul = (int)luaL_optinteger(L, 3, 0);
|
||||
return pushmode(L, lua_gc(L, o, minormul, majormul));
|
||||
return pushmode(L, lua_gc(L, o));
|
||||
}
|
||||
case LUA_GCINC: {
|
||||
int pause = (int)luaL_optinteger(L, 2, 0);
|
||||
int stepmul = (int)luaL_optinteger(L, 3, 0);
|
||||
int stepsize = (int)luaL_optinteger(L, 4, 0);
|
||||
return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
|
||||
return pushmode(L, lua_gc(L, o));
|
||||
}
|
||||
case LUA_GCPARAM: {
|
||||
static const char *const params[] = {
|
||||
"minormul", "majorminor", "minormajor",
|
||||
"pause", "stepmul", "stepsize", NULL};
|
||||
static const char pnum[] = {
|
||||
LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR,
|
||||
LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE};
|
||||
int p = pnum[luaL_checkoption(L, 2, NULL, params)];
|
||||
lua_Integer value = luaL_optinteger(L, 3, -1);
|
||||
lua_pushinteger(L, lua_gc(L, o, p, (int)value));
|
||||
return 1;
|
||||
}
|
||||
default: {
|
||||
int res = lua_gc(L, o);
|
||||
@ -278,21 +279,22 @@ static int luaB_next (lua_State *L) {
|
||||
|
||||
static int pairscont (lua_State *L, int status, lua_KContext k) {
|
||||
(void)L; (void)status; (void)k; /* unused */
|
||||
return 3;
|
||||
return 4; /* __pairs did all the work, just return its results */
|
||||
}
|
||||
|
||||
static int luaB_pairs (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
|
||||
lua_pushcfunction(L, luaB_next); /* will return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
lua_pushnil(L); /* and initial value */
|
||||
lua_pushcfunction(L, luaB_next); /* will return generator and */
|
||||
lua_pushvalue(L, 1); /* state */
|
||||
lua_pushnil(L); /* initial value */
|
||||
lua_pushnil(L); /* to-be-closed object */
|
||||
}
|
||||
else {
|
||||
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
|
||||
lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */
|
||||
lua_callk(L, 1, 4, 0, pairscont); /* get 4 values from metamethod */
|
||||
}
|
||||
return 3;
|
||||
return 4;
|
||||
}
|
||||
|
||||
|
||||
@ -337,9 +339,17 @@ static int load_aux (lua_State *L, int status, int envidx) {
|
||||
}
|
||||
|
||||
|
||||
static const char *getMode (lua_State *L, int idx) {
|
||||
const char *mode = luaL_optstring(L, idx, "bt");
|
||||
if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */
|
||||
luaL_argerror(L, idx, "invalid mode");
|
||||
return mode;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_loadfile (lua_State *L) {
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
const char *mode = luaL_optstring(L, 2, NULL);
|
||||
const char *mode = getMode(L, 2);
|
||||
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
|
||||
int status = luaL_loadfilex(L, fname, mode);
|
||||
return load_aux(L, status, env);
|
||||
@ -388,7 +398,7 @@ static int luaB_load (lua_State *L) {
|
||||
int status;
|
||||
size_t l;
|
||||
const char *s = lua_tolstring(L, 1, &l);
|
||||
const char *mode = luaL_optstring(L, 3, "bt");
|
||||
const char *mode = getMode(L, 3);
|
||||
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
|
||||
if (s != NULL) { /* loading a string? */
|
||||
const char *chunkname = luaL_optstring(L, 2, s);
|
||||
|
||||
414
lcode.c
414
lcode.c
@ -31,10 +31,7 @@
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
/* Maximum number of registers in a Lua function (must fit in 8 bits) */
|
||||
#define MAXREGS 255
|
||||
|
||||
|
||||
/* (note that expressions VJMP also have jumps.) */
|
||||
#define hasjumps(e) ((e)->t != (e)->f)
|
||||
|
||||
|
||||
@ -43,8 +40,12 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k);
|
||||
|
||||
|
||||
/* semantic error */
|
||||
l_noret luaK_semerror (LexState *ls, const char *msg) {
|
||||
l_noret luaK_semerror (LexState *ls, const char *fmt, ...) {
|
||||
const char *msg;
|
||||
va_list argp;
|
||||
pushvfstring(ls->L, argp, fmt, msg);
|
||||
ls->t.token = 0; /* remove "near <token>" from final message */
|
||||
ls->linenumber = ls->lastline; /* back to line of last used token */
|
||||
luaX_syntaxerror(ls, msg);
|
||||
}
|
||||
|
||||
@ -211,6 +212,7 @@ void luaK_ret (FuncState *fs, int first, int nret) {
|
||||
case 1: op = OP_RETURN1; break;
|
||||
default: op = OP_RETURN; break;
|
||||
}
|
||||
luaY_checklimit(fs, nret + 1, MAXARG_B, "returns");
|
||||
luaK_codeABC(fs, op, first, nret + 1, 0);
|
||||
}
|
||||
|
||||
@ -331,15 +333,15 @@ static void savelineinfo (FuncState *fs, Proto *f, int line) {
|
||||
int pc = fs->pc - 1; /* last instruction coded */
|
||||
if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
|
||||
luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
|
||||
f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines");
|
||||
f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines");
|
||||
f->abslineinfo[fs->nabslineinfo].pc = pc;
|
||||
f->abslineinfo[fs->nabslineinfo++].line = line;
|
||||
linedif = ABSLINEINFO; /* signal that there is absolute information */
|
||||
fs->iwthabs = 1; /* restart counter */
|
||||
}
|
||||
luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
|
||||
MAX_INT, "opcodes");
|
||||
f->lineinfo[pc] = linedif;
|
||||
INT_MAX, "opcodes");
|
||||
f->lineinfo[pc] = cast(ls_byte, linedif);
|
||||
fs->previousline = line; /* last line saved */
|
||||
}
|
||||
|
||||
@ -383,7 +385,7 @@ int luaK_code (FuncState *fs, Instruction i) {
|
||||
Proto *f = fs->f;
|
||||
/* put new instruction in code array */
|
||||
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||
MAX_INT, "opcodes");
|
||||
INT_MAX, "opcodes");
|
||||
f->code[fs->pc++] = i;
|
||||
savelineinfo(fs, f, fs->ls->lastline);
|
||||
return fs->pc - 1; /* index of new instruction */
|
||||
@ -394,32 +396,40 @@ int luaK_code (FuncState *fs, Instruction i) {
|
||||
** Format and emit an 'iABC' instruction. (Assertions check consistency
|
||||
** of parameters versus opcode.)
|
||||
*/
|
||||
int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) {
|
||||
int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
|
||||
lua_assert(getOpMode(o) == iABC);
|
||||
lua_assert(a <= MAXARG_A && b <= MAXARG_B &&
|
||||
c <= MAXARG_C && (k & ~1) == 0);
|
||||
return luaK_code(fs, CREATE_ABCk(o, a, b, c, k));
|
||||
lua_assert(A <= MAXARG_A && B <= MAXARG_B &&
|
||||
C <= MAXARG_C && (k & ~1) == 0);
|
||||
return luaK_code(fs, CREATE_ABCk(o, A, B, C, k));
|
||||
}
|
||||
|
||||
|
||||
int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
|
||||
lua_assert(getOpMode(o) == ivABC);
|
||||
lua_assert(A <= MAXARG_A && B <= MAXARG_vB &&
|
||||
C <= MAXARG_vC && (k & ~1) == 0);
|
||||
return luaK_code(fs, CREATE_vABCk(o, A, B, C, k));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Format and emit an 'iABx' instruction.
|
||||
*/
|
||||
int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
||||
int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) {
|
||||
lua_assert(getOpMode(o) == iABx);
|
||||
lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
|
||||
return luaK_code(fs, CREATE_ABx(o, a, bc));
|
||||
lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
|
||||
return luaK_code(fs, CREATE_ABx(o, A, Bc));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Format and emit an 'iAsBx' instruction.
|
||||
*/
|
||||
int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
|
||||
unsigned int b = bc + OFFSET_sBx;
|
||||
static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
|
||||
int b = Bc + OFFSET_sBx;
|
||||
lua_assert(getOpMode(o) == iAsBx);
|
||||
lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
|
||||
return luaK_code(fs, CREATE_ABx(o, a, b));
|
||||
lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
|
||||
return luaK_code(fs, CREATE_ABx(o, A, b));
|
||||
}
|
||||
|
||||
|
||||
@ -427,7 +437,7 @@ int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
|
||||
** Format and emit an 'isJ' instruction.
|
||||
*/
|
||||
static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
|
||||
unsigned int j = sj + OFFSET_sJ;
|
||||
int j = sj + OFFSET_sJ;
|
||||
lua_assert(getOpMode(o) == isJ);
|
||||
lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
|
||||
return luaK_code(fs, CREATE_sJ(o, j, k));
|
||||
@ -437,9 +447,9 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
|
||||
/*
|
||||
** Emit an "extra argument" instruction (format 'iAx')
|
||||
*/
|
||||
static int codeextraarg (FuncState *fs, int a) {
|
||||
lua_assert(a <= MAXARG_Ax);
|
||||
return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
|
||||
static int codeextraarg (FuncState *fs, int A) {
|
||||
lua_assert(A <= MAXARG_Ax);
|
||||
return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A));
|
||||
}
|
||||
|
||||
|
||||
@ -466,9 +476,7 @@ static int luaK_codek (FuncState *fs, int reg, int k) {
|
||||
void luaK_checkstack (FuncState *fs, int n) {
|
||||
int newstack = fs->freereg + n;
|
||||
if (newstack > fs->f->maxstacksize) {
|
||||
if (newstack >= MAXREGS)
|
||||
luaX_syntaxerror(fs->ls,
|
||||
"function or expression needs too many registers");
|
||||
luaY_checklimit(fs, newstack, MAX_FSTACK, "registers");
|
||||
fs->f->maxstacksize = cast_byte(newstack);
|
||||
}
|
||||
}
|
||||
@ -479,7 +487,7 @@ void luaK_checkstack (FuncState *fs, int n) {
|
||||
*/
|
||||
void luaK_reserveregs (FuncState *fs, int n) {
|
||||
luaK_checkstack(fs, n);
|
||||
fs->freereg += n;
|
||||
fs->freereg = cast_byte(fs->freereg + n);
|
||||
}
|
||||
|
||||
|
||||
@ -533,35 +541,14 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
|
||||
|
||||
/*
|
||||
** Add constant 'v' to prototype's list of constants (field 'k').
|
||||
** Use scanner's table to cache position of constants in constant list
|
||||
** and try to reuse constants. Because some values should not be used
|
||||
** as keys (nil cannot be a key, integer keys can collapse with float
|
||||
** keys), the caller must provide a useful 'key' for indexing the cache.
|
||||
** Note that all functions share the same table, so entering or exiting
|
||||
** a function can make some indices wrong.
|
||||
*/
|
||||
static int addk (FuncState *fs, TValue *key, TValue *v) {
|
||||
TValue val;
|
||||
static int addk (FuncState *fs, Proto *f, TValue *v) {
|
||||
lua_State *L = fs->ls->L;
|
||||
Proto *f = fs->f;
|
||||
const TValue *idx = luaH_get(fs->ls->h, key); /* query scanner table */
|
||||
int k, oldsize;
|
||||
if (ttisinteger(idx)) { /* is there an index there? */
|
||||
k = cast_int(ivalue(idx));
|
||||
/* correct value? (warning: must distinguish floats from integers!) */
|
||||
if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) &&
|
||||
luaV_rawequalobj(&f->k[k], v))
|
||||
return k; /* reuse index */
|
||||
}
|
||||
/* constant not found; create a new entry */
|
||||
oldsize = f->sizek;
|
||||
k = fs->nk;
|
||||
/* numerical value does not need GC barrier;
|
||||
table has no metatable, so it does not need to invalidate cache */
|
||||
setivalue(&val, k);
|
||||
luaH_finishset(L, fs->ls->h, key, idx, &val);
|
||||
int oldsize = f->sizek;
|
||||
int k = fs->nk;
|
||||
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
|
||||
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
||||
while (oldsize < f->sizek)
|
||||
setnilvalue(&f->k[oldsize++]);
|
||||
setobj(L, &f->k[k], v);
|
||||
fs->nk++;
|
||||
luaC_barrier(L, f, v);
|
||||
@ -569,13 +556,40 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Use scanner's table to cache position of constants in constant list
|
||||
** and try to reuse constants. Because some values should not be used
|
||||
** as keys (nil cannot be a key, integer keys can collapse with float
|
||||
** keys), the caller must provide a useful 'key' for indexing the cache.
|
||||
*/
|
||||
static int k2proto (FuncState *fs, TValue *key, TValue *v) {
|
||||
TValue val;
|
||||
Proto *f = fs->f;
|
||||
int tag = luaH_get(fs->kcache, key, &val); /* query scanner table */
|
||||
if (!tagisempty(tag)) { /* is there an index there? */
|
||||
int k = cast_int(ivalue(&val));
|
||||
/* collisions can happen only for float keys */
|
||||
lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v));
|
||||
return k; /* reuse index */
|
||||
}
|
||||
else { /* constant not found; create a new entry */
|
||||
int k = addk(fs, f, v);
|
||||
/* cache it for reuse; numerical value does not need GC barrier;
|
||||
table is not a metatable, so it does not need to invalidate cache */
|
||||
setivalue(&val, k);
|
||||
luaH_set(fs->ls->L, fs->kcache, key, &val);
|
||||
return k;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Add a string to list of constants and return its index.
|
||||
*/
|
||||
static int stringK (FuncState *fs, TString *s) {
|
||||
TValue o;
|
||||
setsvalue(fs->ls->L, &o, s);
|
||||
return addk(fs, &o, &o); /* use string itself as key */
|
||||
return k2proto(fs, &o, &o); /* use string itself as key */
|
||||
}
|
||||
|
||||
|
||||
@ -585,36 +599,42 @@ static int stringK (FuncState *fs, TString *s) {
|
||||
static int luaK_intK (FuncState *fs, lua_Integer n) {
|
||||
TValue o;
|
||||
setivalue(&o, n);
|
||||
return addk(fs, &o, &o); /* use integer itself as key */
|
||||
return k2proto(fs, &o, &o); /* use integer itself as key */
|
||||
}
|
||||
|
||||
/*
|
||||
** Add a float to list of constants and return its index. Floats
|
||||
** with integral values need a different key, to avoid collision
|
||||
** with actual integers. To that, we add to the number its smaller
|
||||
** with actual integers. To that end, we add to the number its smaller
|
||||
** power-of-two fraction that is still significant in its scale.
|
||||
** For doubles, that would be 1/2^52.
|
||||
** (This method is not bulletproof: there may be another float
|
||||
** with that value, and for floats larger than 2^53 the result is
|
||||
** still an integer. At worst, this only wastes an entry with
|
||||
** a duplicate.)
|
||||
** (For doubles, the fraction would be 2^-52).
|
||||
** This method is not bulletproof: different numbers may generate the
|
||||
** same key (e.g., very large numbers will overflow to 'inf') and for
|
||||
** floats larger than 2^53 the result is still an integer. For those
|
||||
** cases, just generate a new entry. At worst, this only wastes an entry
|
||||
** with a duplicate.
|
||||
*/
|
||||
static int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||
TValue o;
|
||||
lua_Integer ik;
|
||||
setfltvalue(&o, r);
|
||||
if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */
|
||||
return addk(fs, &o, &o); /* use number itself as key */
|
||||
else { /* must build an alternative key */
|
||||
TValue o, kv;
|
||||
setfltvalue(&o, r); /* value as a TValue */
|
||||
if (r == 0) { /* handle zero as a special case */
|
||||
setpvalue(&kv, fs); /* use FuncState as index */
|
||||
return k2proto(fs, &kv, &o); /* cannot collide */
|
||||
}
|
||||
else {
|
||||
const int nbm = l_floatatt(MANT_DIG);
|
||||
const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
|
||||
const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */
|
||||
TValue kv;
|
||||
setfltvalue(&kv, k);
|
||||
/* result is not an integral value, unless value is too large */
|
||||
lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) ||
|
||||
l_mathop(fabs)(r) >= l_mathop(1e6));
|
||||
return addk(fs, &kv, &o);
|
||||
const lua_Number k = r * (1 + q); /* key */
|
||||
lua_Integer ik;
|
||||
setfltvalue(&kv, k); /* key as a TValue */
|
||||
if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integer value? */
|
||||
int n = k2proto(fs, &kv, &o); /* use key */
|
||||
if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */
|
||||
return n;
|
||||
}
|
||||
/* else, either key is still an integer or there was a collision;
|
||||
anyway, do not try to reuse constant; instead, create a new one */
|
||||
return addk(fs, fs->f, &o);
|
||||
}
|
||||
}
|
||||
|
||||
@ -625,7 +645,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||
static int boolF (FuncState *fs) {
|
||||
TValue o;
|
||||
setbfvalue(&o);
|
||||
return addk(fs, &o, &o); /* use boolean itself as key */
|
||||
return k2proto(fs, &o, &o); /* use boolean itself as key */
|
||||
}
|
||||
|
||||
|
||||
@ -635,7 +655,7 @@ static int boolF (FuncState *fs) {
|
||||
static int boolT (FuncState *fs) {
|
||||
TValue o;
|
||||
setbtvalue(&o);
|
||||
return addk(fs, &o, &o); /* use boolean itself as key */
|
||||
return k2proto(fs, &o, &o); /* use boolean itself as key */
|
||||
}
|
||||
|
||||
|
||||
@ -645,9 +665,9 @@ static int boolT (FuncState *fs) {
|
||||
static int nilK (FuncState *fs) {
|
||||
TValue k, v;
|
||||
setnilvalue(&v);
|
||||
/* cannot use nil as key; instead use table itself to represent nil */
|
||||
sethvalue(fs->ls->L, &k, fs->ls->h);
|
||||
return addk(fs, &k, &v);
|
||||
/* cannot use nil as key; instead use table itself */
|
||||
sethvalue(fs->ls->L, &k, fs->kcache);
|
||||
return k2proto(fs, &k, &v);
|
||||
}
|
||||
|
||||
|
||||
@ -671,7 +691,7 @@ static int fitsBx (lua_Integer i) {
|
||||
|
||||
void luaK_int (FuncState *fs, int reg, lua_Integer i) {
|
||||
if (fitsBx(i))
|
||||
luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
|
||||
codeAsBx(fs, OP_LOADI, reg, cast_int(i));
|
||||
else
|
||||
luaK_codek(fs, reg, luaK_intK(fs, i));
|
||||
}
|
||||
@ -680,12 +700,28 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
|
||||
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
|
||||
lua_Integer fi;
|
||||
if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
|
||||
luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
|
||||
codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
|
||||
else
|
||||
luaK_codek(fs, reg, luaK_numberK(fs, f));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get the value of 'var' in a register and generate an opcode to check
|
||||
** whether that register is nil. 'k' is the index of the variable name
|
||||
** in the list of constants. If its value cannot be encoded in Bx, a 0
|
||||
** will use '?' for the name.
|
||||
*/
|
||||
void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line) {
|
||||
luaK_exp2anyreg(fs, var);
|
||||
luaK_fixline(fs, line);
|
||||
k = (k >= MAXARG_Bx) ? 0 : k + 1;
|
||||
luaK_codeABx(fs, OP_ERRNNIL, var->u.info, k);
|
||||
luaK_fixline(fs, line);
|
||||
freeexp(fs, var);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Convert a constant in 'v' into an expression description 'e'
|
||||
*/
|
||||
@ -720,6 +756,7 @@ static void const2exp (TValue *v, expdesc *e) {
|
||||
*/
|
||||
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
||||
Instruction *pc = &getinstruction(fs, e);
|
||||
luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results");
|
||||
if (e->k == VCALL) /* expression is an open function call? */
|
||||
SETARG_C(*pc, nresults + 1);
|
||||
else {
|
||||
@ -734,10 +771,11 @@ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
||||
/*
|
||||
** Convert a VKSTR to a VK
|
||||
*/
|
||||
static void str2K (FuncState *fs, expdesc *e) {
|
||||
static int str2K (FuncState *fs, expdesc *e) {
|
||||
lua_assert(e->k == VKSTR);
|
||||
e->u.info = stringK(fs, e->u.strval);
|
||||
e->k = VK;
|
||||
return e->u.info;
|
||||
}
|
||||
|
||||
|
||||
@ -764,6 +802,15 @@ void luaK_setoneret (FuncState *fs, expdesc *e) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Change a vararg parameter into a regular local variable
|
||||
*/
|
||||
void luaK_vapar2local (FuncState *fs, expdesc *var) {
|
||||
needvatab(fs->f); /* function will need a vararg table */
|
||||
/* now a vararg parameter is equivalent to a regular local variable */
|
||||
var->k = VLOCAL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Ensure that expression 'e' is not a variable (nor a <const>).
|
||||
@ -775,8 +822,12 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
||||
const2exp(const2val(fs, e), e);
|
||||
break;
|
||||
}
|
||||
case VVARGVAR: {
|
||||
luaK_vapar2local(fs, e); /* turn it into a local variable */
|
||||
} /* FALLTHROUGH */
|
||||
case VLOCAL: { /* already in a register */
|
||||
e->u.info = e->u.var.ridx;
|
||||
int temp = e->u.var.ridx;
|
||||
e->u.info = temp; /* (can't do a direct assignment; values overlap) */
|
||||
e->k = VNONRELOC; /* becomes a non-relocatable value */
|
||||
break;
|
||||
}
|
||||
@ -808,6 +859,12 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
||||
e->k = VRELOC;
|
||||
break;
|
||||
}
|
||||
case VVARGIND: {
|
||||
freeregs(fs, e->u.ind.t, e->u.ind.idx);
|
||||
e->u.info = luaK_codeABC(fs, OP_GETVARG, 0, e->u.ind.t, e->u.ind.idx);
|
||||
e->k = VRELOC;
|
||||
break;
|
||||
}
|
||||
case VVARARG: case VCALL: {
|
||||
luaK_setoneret(fs, e);
|
||||
break;
|
||||
@ -970,11 +1027,11 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
||||
|
||||
|
||||
/*
|
||||
** Ensures final expression result is either in a register
|
||||
** or in an upvalue.
|
||||
** Ensures final expression result is either in a register,
|
||||
** in an upvalue, or it is the vararg parameter.
|
||||
*/
|
||||
void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
|
||||
if (e->k != VUPVAL || hasjumps(e))
|
||||
if ((e->k != VUPVAL && e->k != VVARGVAR) || hasjumps(e))
|
||||
luaK_exp2anyreg(fs, e);
|
||||
}
|
||||
|
||||
@ -984,7 +1041,7 @@ void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
|
||||
** or it is a constant.
|
||||
*/
|
||||
void luaK_exp2val (FuncState *fs, expdesc *e) {
|
||||
if (hasjumps(e))
|
||||
if (e->k == VJMP || hasjumps(e))
|
||||
luaK_exp2anyreg(fs, e);
|
||||
else
|
||||
luaK_dischargevars(fs, e);
|
||||
@ -1025,7 +1082,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) {
|
||||
** in the range of R/K indices).
|
||||
** Returns 1 iff expression is K.
|
||||
*/
|
||||
int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
||||
static int exp2RK (FuncState *fs, expdesc *e) {
|
||||
if (luaK_exp2K(fs, e))
|
||||
return 1;
|
||||
else { /* not a constant in the right range: put it in a register */
|
||||
@ -1035,10 +1092,10 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
||||
}
|
||||
|
||||
|
||||
static void codeABRK (FuncState *fs, OpCode o, int a, int b,
|
||||
static void codeABRK (FuncState *fs, OpCode o, int A, int B,
|
||||
expdesc *ec) {
|
||||
int k = luaK_exp2RK(fs, ec);
|
||||
luaK_codeABCk(fs, o, a, b, ec->u.info, k);
|
||||
int k = exp2RK(fs, ec);
|
||||
luaK_codeABCk(fs, o, A, B, ec->u.info, k);
|
||||
}
|
||||
|
||||
|
||||
@ -1069,6 +1126,10 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
||||
codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
|
||||
break;
|
||||
}
|
||||
case VVARGIND: {
|
||||
needvatab(fs->f); /* function will need a vararg table */
|
||||
/* now, assignment is to a regular table */
|
||||
} /* FALLTHROUGH */
|
||||
case VINDEXED: {
|
||||
codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
|
||||
break;
|
||||
@ -1079,22 +1140,6 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
|
||||
*/
|
||||
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
||||
int ereg;
|
||||
luaK_exp2anyreg(fs, e);
|
||||
ereg = e->u.info; /* register where 'e' was placed */
|
||||
freeexp(fs, e);
|
||||
e->u.info = fs->freereg; /* base register for op_self */
|
||||
e->k = VNONRELOC; /* self expression has a fixed register */
|
||||
luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
|
||||
codeABRK(fs, OP_SELF, e->u.info, ereg, key);
|
||||
freeexp(fs, key);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Negate condition 'e' (where 'e' is a comparison).
|
||||
*/
|
||||
@ -1157,7 +1202,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
||||
/*
|
||||
** Emit code to go through if 'e' is false, jump otherwise.
|
||||
*/
|
||||
void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of new jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
@ -1215,17 +1260,17 @@ static void codenot (FuncState *fs, expdesc *e) {
|
||||
|
||||
|
||||
/*
|
||||
** Check whether expression 'e' is a small literal string
|
||||
** Check whether expression 'e' is a short literal string
|
||||
*/
|
||||
static int isKstr (FuncState *fs, expdesc *e) {
|
||||
return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
|
||||
return (e->k == VK && !hasjumps(e) && e->u.info <= MAXINDEXRK &&
|
||||
ttisshrstring(&fs->f->k[e->u.info]));
|
||||
}
|
||||
|
||||
/*
|
||||
** Check whether expression 'e' is a literal integer.
|
||||
*/
|
||||
int luaK_isKint (expdesc *e) {
|
||||
static int isKint (expdesc *e) {
|
||||
return (e->k == VKINT && !hasjumps(e));
|
||||
}
|
||||
|
||||
@ -1235,7 +1280,7 @@ int luaK_isKint (expdesc *e) {
|
||||
** proper range to fit in register C
|
||||
*/
|
||||
static int isCint (expdesc *e) {
|
||||
return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
|
||||
return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
|
||||
}
|
||||
|
||||
|
||||
@ -1244,7 +1289,7 @@ static int isCint (expdesc *e) {
|
||||
** proper range to fit in register sC
|
||||
*/
|
||||
static int isSCint (expdesc *e) {
|
||||
return luaK_isKint(e) && fitsC(e->u.ival);
|
||||
return isKint(e) && fitsC(e->u.ival);
|
||||
}
|
||||
|
||||
|
||||
@ -1269,6 +1314,40 @@ static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Emit SELF instruction or equivalent: the code will convert
|
||||
** expression 'e' into 'e.key(e,'.
|
||||
*/
|
||||
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
||||
int ereg, base;
|
||||
luaK_exp2anyreg(fs, e);
|
||||
ereg = e->u.info; /* register where 'e' (the receiver) was placed */
|
||||
freeexp(fs, e);
|
||||
base = e->u.info = fs->freereg; /* base register for op_self */
|
||||
e->k = VNONRELOC; /* self expression has a fixed register */
|
||||
luaK_reserveregs(fs, 2); /* method and 'self' produced by op_self */
|
||||
lua_assert(key->k == VKSTR);
|
||||
/* is method name a short string in a valid K index? */
|
||||
if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) {
|
||||
/* can use 'self' opcode */
|
||||
luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0);
|
||||
}
|
||||
else { /* cannot use 'self' opcode; use move+gettable */
|
||||
luaK_exp2anyreg(fs, key); /* put method name in a register */
|
||||
luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0); /* copy self to base+1 */
|
||||
luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info); /* get method */
|
||||
}
|
||||
freeexp(fs, key);
|
||||
}
|
||||
|
||||
|
||||
/* auxiliary function to define indexing expressions */
|
||||
static void fillidxk (expdesc *t, int idx, expkind k) {
|
||||
t->u.ind.idx = cast_byte(idx);
|
||||
t->k = k;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Create expression 't[k]'. 't' must have its final result already in a
|
||||
** register or upvalue. Upvalues can only be indexed by literal strings.
|
||||
@ -1276,33 +1355,39 @@ static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
|
||||
** values in registers.
|
||||
*/
|
||||
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
||||
int keystr = -1;
|
||||
if (k->k == VKSTR)
|
||||
str2K(fs, k);
|
||||
keystr = str2K(fs, k);
|
||||
lua_assert(!hasjumps(t) &&
|
||||
(t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
|
||||
(t->k == VLOCAL || t->k == VVARGVAR ||
|
||||
t->k == VNONRELOC || t->k == VUPVAL));
|
||||
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
|
||||
luaK_exp2anyreg(fs, t); /* put it in a register */
|
||||
if (t->k == VUPVAL) {
|
||||
t->u.ind.t = t->u.info; /* upvalue index */
|
||||
t->u.ind.idx = k->u.info; /* literal string */
|
||||
t->k = VINDEXUP;
|
||||
lu_byte temp = cast_byte(t->u.info); /* upvalue index */
|
||||
t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */
|
||||
lua_assert(isKstr(fs, k));
|
||||
fillidxk(t, k->u.info, VINDEXUP); /* literal short string */
|
||||
}
|
||||
else if (t->k == VVARGVAR) { /* indexing the vararg parameter? */
|
||||
int kreg = luaK_exp2anyreg(fs, k); /* put key in some register */
|
||||
lu_byte vreg = cast_byte(t->u.var.ridx); /* register with vararg param. */
|
||||
lua_assert(vreg == fs->f->numparams);
|
||||
t->u.ind.t = vreg; /* (avoid a direct assignment; values may overlap) */
|
||||
fillidxk(t, kreg, VVARGIND); /* 't' represents 'vararg[k]' */
|
||||
}
|
||||
else {
|
||||
/* register index of the table */
|
||||
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
|
||||
if (isKstr(fs, k)) {
|
||||
t->u.ind.idx = k->u.info; /* literal string */
|
||||
t->k = VINDEXSTR;
|
||||
}
|
||||
else if (isCint(k)) {
|
||||
t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */
|
||||
t->k = VINDEXI;
|
||||
}
|
||||
else {
|
||||
t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */
|
||||
t->k = VINDEXED;
|
||||
}
|
||||
t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
|
||||
if (isKstr(fs, k))
|
||||
fillidxk(t, k->u.info, VINDEXSTR); /* literal short string */
|
||||
else if (isCint(k)) /* int. constant in proper range? */
|
||||
fillidxk(t, cast_int(k->u.ival), VINDEXI);
|
||||
else
|
||||
fillidxk(t, luaK_exp2anyreg(fs, k), VINDEXED); /* register */
|
||||
}
|
||||
t->u.ind.keystr = keystr; /* string index in 'k' */
|
||||
t->u.ind.ro = 0; /* by default, not read-only */
|
||||
}
|
||||
|
||||
|
||||
@ -1409,7 +1494,7 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
|
||||
e1->u.info = pc;
|
||||
e1->k = VRELOC; /* all those operations are relocatable */
|
||||
luaK_fixline(fs, line);
|
||||
luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */
|
||||
luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip); /* metamethod */
|
||||
luaK_fixline(fs, line);
|
||||
}
|
||||
|
||||
@ -1459,7 +1544,7 @@ static void codebinK (FuncState *fs, BinOpr opr,
|
||||
*/
|
||||
static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
|
||||
OpCode op, int line, TMS event) {
|
||||
if (!luaK_isKint(e2))
|
||||
if (!isKint(e2))
|
||||
return 0; /* not an integer constant */
|
||||
else {
|
||||
lua_Integer i2 = e2->u.ival;
|
||||
@ -1592,7 +1677,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
||||
op = OP_EQI;
|
||||
r2 = im; /* immediate operand */
|
||||
}
|
||||
else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */
|
||||
else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */
|
||||
op = OP_EQK;
|
||||
r2 = e2->u.info; /* constant index */
|
||||
}
|
||||
@ -1614,7 +1699,7 @@ void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (opr) {
|
||||
case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
|
||||
if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
|
||||
if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef))
|
||||
break;
|
||||
/* else */ /* FALLTHROUGH */
|
||||
case OPR_LEN:
|
||||
@ -1658,7 +1743,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
||||
}
|
||||
case OPR_EQ: case OPR_NE: {
|
||||
if (!tonumeral(v, NULL))
|
||||
luaK_exp2RK(fs, v);
|
||||
exp2RK(fs, v);
|
||||
/* else keep numeral, which may be an immediate operand */
|
||||
break;
|
||||
}
|
||||
@ -1702,7 +1787,7 @@ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
|
||||
void luaK_posfix (FuncState *fs, BinOpr opr,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
luaK_dischargevars(fs, e2);
|
||||
if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
|
||||
if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2))
|
||||
return; /* done by folding */
|
||||
switch (opr) {
|
||||
case OPR_AND: {
|
||||
@ -1788,11 +1873,11 @@ void luaK_fixline (FuncState *fs, int line) {
|
||||
|
||||
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
|
||||
Instruction *inst = &fs->f->code[pc];
|
||||
int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */
|
||||
int extra = asize / (MAXARG_C + 1); /* higher bits of array size */
|
||||
int rc = asize % (MAXARG_C + 1); /* lower bits of array size */
|
||||
int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */
|
||||
int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */
|
||||
int k = (extra > 0); /* true iff needs extra argument */
|
||||
*inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k);
|
||||
hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0;
|
||||
*inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k);
|
||||
*(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
|
||||
}
|
||||
|
||||
@ -1805,18 +1890,18 @@ void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
|
||||
** table (or LUA_MULTRET to add up to stack top).
|
||||
*/
|
||||
void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
|
||||
lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
|
||||
lua_assert(tostore != 0);
|
||||
if (tostore == LUA_MULTRET)
|
||||
tostore = 0;
|
||||
if (nelems <= MAXARG_C)
|
||||
luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems);
|
||||
if (nelems <= MAXARG_vC)
|
||||
luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0);
|
||||
else {
|
||||
int extra = nelems / (MAXARG_C + 1);
|
||||
nelems %= (MAXARG_C + 1);
|
||||
luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
|
||||
int extra = nelems / (MAXARG_vC + 1);
|
||||
nelems %= (MAXARG_vC + 1);
|
||||
luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
|
||||
codeextraarg(fs, extra);
|
||||
}
|
||||
fs->freereg = base + 1; /* free registers with list values */
|
||||
fs->freereg = cast_byte(base + 1); /* free registers with list values */
|
||||
}
|
||||
|
||||
|
||||
@ -1829,8 +1914,8 @@ static int finaltarget (Instruction *code, int i) {
|
||||
Instruction pc = code[i];
|
||||
if (GET_OPCODE(pc) != OP_JMP)
|
||||
break;
|
||||
else
|
||||
i += GETARG_sJ(pc) + 1;
|
||||
else
|
||||
i += GETARG_sJ(pc) + 1;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
@ -1840,15 +1925,20 @@ static int finaltarget (Instruction *code, int i) {
|
||||
** Do a final pass over the code of a function, doing small peephole
|
||||
** optimizations and adjustments.
|
||||
*/
|
||||
#include "lopnames.h"
|
||||
void luaK_finish (FuncState *fs) {
|
||||
int i;
|
||||
Proto *p = fs->f;
|
||||
if (p->flag & PF_VATAB) /* will it use a vararg table? */
|
||||
p->flag &= cast_byte(~PF_VAHID); /* then it will not use hidden args. */
|
||||
for (i = 0; i < fs->pc; i++) {
|
||||
Instruction *pc = &p->code[i];
|
||||
lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc));
|
||||
/* avoid "not used" warnings when assert is off (for 'onelua.c') */
|
||||
(void)luaP_isOT; (void)luaP_isIT;
|
||||
lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc));
|
||||
switch (GET_OPCODE(*pc)) {
|
||||
case OP_RETURN0: case OP_RETURN1: {
|
||||
if (!(fs->needclose || p->is_vararg))
|
||||
if (!(fs->needclose || (p->flag & PF_VAHID)))
|
||||
break; /* no extra work */
|
||||
/* else use OP_RETURN to do the extra work */
|
||||
SET_OPCODE(*pc, OP_RETURN);
|
||||
@ -1856,13 +1946,23 @@ void luaK_finish (FuncState *fs) {
|
||||
case OP_RETURN: case OP_TAILCALL: {
|
||||
if (fs->needclose)
|
||||
SETARG_k(*pc, 1); /* signal that it needs to close */
|
||||
if (p->is_vararg)
|
||||
SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */
|
||||
if (p->flag & PF_VAHID) /* does it use hidden arguments? */
|
||||
SETARG_C(*pc, p->numparams + 1); /* signal that */
|
||||
break;
|
||||
}
|
||||
case OP_JMP: {
|
||||
case OP_GETVARG: {
|
||||
if (p->flag & PF_VATAB) /* function has a vararg table? */
|
||||
SET_OPCODE(*pc, OP_GETTABLE); /* must get vararg there */
|
||||
break;
|
||||
}
|
||||
case OP_VARARG: {
|
||||
if (p->flag & PF_VATAB) /* function has a vararg table? */
|
||||
SETARG_k(*pc, 1); /* must get vararg there */
|
||||
break;
|
||||
}
|
||||
case OP_JMP: { /* to optimize jumps to jumps */
|
||||
int target = finaltarget(p->code, i);
|
||||
fixjump(fs, i, target);
|
||||
fixjump(fs, i, target); /* jump directly to final target */
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
|
||||
17
lcode.h
17
lcode.h
@ -60,27 +60,28 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
||||
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
|
||||
|
||||
LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
|
||||
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
|
||||
LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx);
|
||||
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
|
||||
int B, int C, int k);
|
||||
LUAI_FUNC int luaK_isKint (expdesc *e);
|
||||
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bx);
|
||||
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C,
|
||||
int k);
|
||||
LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C,
|
||||
int k);
|
||||
LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v);
|
||||
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
|
||||
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
|
||||
LUAI_FUNC void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k,
|
||||
int line);
|
||||
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
|
||||
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
|
||||
LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
|
||||
LUAI_FUNC void luaK_vapar2local (FuncState *fs, expdesc *var);
|
||||
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
|
||||
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
||||
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
|
||||
LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
|
||||
LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
|
||||
@ -98,7 +99,7 @@ LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc,
|
||||
int ra, int asize, int hsize);
|
||||
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
|
||||
LUAI_FUNC void luaK_finish (FuncState *fs);
|
||||
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg);
|
||||
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *fmt, ...);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
25
lcorolib.c
25
lcorolib.c
@ -16,6 +16,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
static lua_State *getco (lua_State *L) {
|
||||
@ -76,7 +77,7 @@ static int luaB_auxwrap (lua_State *L) {
|
||||
if (l_unlikely(r < 0)) { /* error? */
|
||||
int stat = lua_status(co);
|
||||
if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
|
||||
stat = lua_resetthread(co, L); /* close its tbc variables */
|
||||
stat = lua_closethread(co, L); /* close its tbc variables */
|
||||
lua_assert(stat != LUA_OK);
|
||||
lua_xmove(co, L, 1); /* move error message to the caller */
|
||||
}
|
||||
@ -153,8 +154,13 @@ static int luaB_costatus (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static lua_State *getoptco (lua_State *L) {
|
||||
return (lua_isnone(L, 1) ? L : getco(L));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_yieldable (lua_State *L) {
|
||||
lua_State *co = lua_isnone(L, 1) ? L : getco(L);
|
||||
lua_State *co = getoptco(L);
|
||||
lua_pushboolean(L, lua_isyieldable(co));
|
||||
return 1;
|
||||
}
|
||||
@ -168,11 +174,11 @@ static int luaB_corunning (lua_State *L) {
|
||||
|
||||
|
||||
static int luaB_close (lua_State *L) {
|
||||
lua_State *co = getco(L);
|
||||
lua_State *co = getoptco(L);
|
||||
int status = auxstatus(L, co);
|
||||
switch (status) {
|
||||
case COS_DEAD: case COS_YIELD: {
|
||||
status = lua_resetthread(co, L);
|
||||
status = lua_closethread(co, L);
|
||||
if (status == LUA_OK) {
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
@ -183,8 +189,17 @@ static int luaB_close (lua_State *L) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
default: /* normal or running coroutine */
|
||||
case COS_NORM:
|
||||
return luaL_error(L, "cannot close a %s coroutine", statname[status]);
|
||||
case COS_RUN:
|
||||
lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */
|
||||
if (lua_tothread(L, -1) == co)
|
||||
return luaL_error(L, "cannot close main thread");
|
||||
lua_closethread(co, L); /* close itself */
|
||||
/* previous call does not return *//* FALLTHROUGH */
|
||||
default:
|
||||
lua_assert(0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
2
lctype.c
2
lctype.c
@ -18,7 +18,7 @@
|
||||
|
||||
|
||||
#if defined (LUA_UCID) /* accept UniCode IDentifiers? */
|
||||
/* consider all non-ascii codepoints to be alphabetic */
|
||||
/* consider all non-ASCII codepoints to be alphabetic */
|
||||
#define NONA 0x01
|
||||
#else
|
||||
#define NONA 0x00 /* default */
|
||||
|
||||
14
ldblib.c
14
ldblib.c
@ -18,6 +18,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -190,8 +191,10 @@ static int db_getinfo (lua_State *L) {
|
||||
settabsi(L, "ftransfer", ar.ftransfer);
|
||||
settabsi(L, "ntransfer", ar.ntransfer);
|
||||
}
|
||||
if (strchr(options, 't'))
|
||||
if (strchr(options, 't')) {
|
||||
settabsb(L, "istailcall", ar.istailcall);
|
||||
settabsi(L, "extraargs", ar.extraargs);
|
||||
}
|
||||
if (strchr(options, 'L'))
|
||||
treatstackoption(L, L1, "activelines");
|
||||
if (strchr(options, 'f'))
|
||||
@ -446,14 +449,6 @@ static int db_traceback (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int db_setcstacklimit (lua_State *L) {
|
||||
int limit = (int)luaL_checkinteger(L, 1);
|
||||
int res = lua_setcstacklimit(L, limit);
|
||||
lua_pushinteger(L, res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_Reg dblib[] = {
|
||||
{"debug", db_debug},
|
||||
{"getuservalue", db_getuservalue},
|
||||
@ -471,7 +466,6 @@ static const luaL_Reg dblib[] = {
|
||||
{"setmetatable", db_setmetatable},
|
||||
{"setupvalue", db_setupvalue},
|
||||
{"traceback", db_traceback},
|
||||
{"setcstacklimit", db_setcstacklimit},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
281
ldebug.c
281
ldebug.c
@ -31,8 +31,10 @@
|
||||
|
||||
|
||||
|
||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL)
|
||||
#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL)
|
||||
|
||||
static const char strlocal[] = "local";
|
||||
static const char strupval[] = "upvalue";
|
||||
|
||||
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
|
||||
const char **name);
|
||||
@ -63,7 +65,7 @@ static int getbaseline (const Proto *f, int pc, int *basepc) {
|
||||
return f->linedefined;
|
||||
}
|
||||
else {
|
||||
int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */
|
||||
int i = pc / MAXIWTHABS - 1; /* get an estimate */
|
||||
/* estimate must be a lower bound of the correct base */
|
||||
lua_assert(i < 0 ||
|
||||
(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
|
||||
@ -182,7 +184,7 @@ static const char *upvalname (const Proto *p, int uv) {
|
||||
|
||||
|
||||
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
||||
if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
|
||||
if (clLvalue(s2v(ci->func.p))->p->flag & PF_VAHID) {
|
||||
int nextra = ci->u.l.nextraargs;
|
||||
if (n >= -nextra) { /* 'n' is negative */
|
||||
*pos = ci->func.p - nextra - (n + 1);
|
||||
@ -245,6 +247,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
lua_lock(L);
|
||||
name = luaG_findlocal(L, ar->i_ci, n, &pos);
|
||||
if (name) {
|
||||
api_checkpop(L, 1);
|
||||
setobjs2s(L, pos, L->top.p - 1);
|
||||
L->top.p--; /* pop value */
|
||||
}
|
||||
@ -254,7 +257,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||
|
||||
|
||||
static void funcinfo (lua_Debug *ar, Closure *cl) {
|
||||
if (noLuaClosure(cl)) {
|
||||
if (!LuaClosure(cl)) {
|
||||
ar->source = "=[C]";
|
||||
ar->srclen = LL("=[C]");
|
||||
ar->linedefined = -1;
|
||||
@ -264,8 +267,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) {
|
||||
else {
|
||||
const Proto *p = cl->l.p;
|
||||
if (p->source) {
|
||||
ar->source = getstr(p->source);
|
||||
ar->srclen = tsslen(p->source);
|
||||
ar->source = getlstr(p->source, ar->srclen);
|
||||
}
|
||||
else {
|
||||
ar->source = "=?";
|
||||
@ -288,29 +290,31 @@ static int nextline (const Proto *p, int currentline, int pc) {
|
||||
|
||||
|
||||
static void collectvalidlines (lua_State *L, Closure *f) {
|
||||
if (noLuaClosure(f)) {
|
||||
if (!LuaClosure(f)) {
|
||||
setnilvalue(s2v(L->top.p));
|
||||
api_incr_top(L);
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
TValue v;
|
||||
const Proto *p = f->l.p;
|
||||
int currentline = p->linedefined;
|
||||
Table *t = luaH_new(L); /* new table to store active lines */
|
||||
sethvalue2s(L, L->top.p, t); /* push it on stack */
|
||||
api_incr_top(L);
|
||||
setbtvalue(&v); /* boolean 'true' to be the value of all indices */
|
||||
if (!p->is_vararg) /* regular function? */
|
||||
i = 0; /* consider all instructions */
|
||||
else { /* vararg function */
|
||||
lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
|
||||
currentline = nextline(p, currentline, 0);
|
||||
i = 1; /* skip first instruction (OP_VARARGPREP) */
|
||||
}
|
||||
for (; i < p->sizelineinfo; i++) { /* for each instruction */
|
||||
currentline = nextline(p, currentline, i); /* get its line */
|
||||
luaH_setint(L, t, currentline, &v); /* table[line] = true */
|
||||
if (p->lineinfo != NULL) { /* proto with debug information? */
|
||||
int i;
|
||||
TValue v;
|
||||
setbtvalue(&v); /* boolean 'true' to be the value of all indices */
|
||||
if (!(isvararg(p))) /* regular function? */
|
||||
i = 0; /* consider all instructions */
|
||||
else { /* vararg function */
|
||||
lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
|
||||
currentline = nextline(p, currentline, 0);
|
||||
i = 1; /* skip first instruction (OP_VARARGPREP) */
|
||||
}
|
||||
for (; i < p->sizelineinfo; i++) { /* for each instruction */
|
||||
currentline = nextline(p, currentline, i); /* get its line */
|
||||
luaH_setint(L, t, currentline, &v); /* table[line] = true */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -339,18 +343,26 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
}
|
||||
case 'u': {
|
||||
ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
|
||||
if (noLuaClosure(f)) {
|
||||
if (!LuaClosure(f)) {
|
||||
ar->isvararg = 1;
|
||||
ar->nparams = 0;
|
||||
}
|
||||
else {
|
||||
ar->isvararg = f->l.p->is_vararg;
|
||||
ar->isvararg = (isvararg(f->l.p)) ? 1 : 0;
|
||||
ar->nparams = f->l.p->numparams;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 't': {
|
||||
ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
|
||||
if (ci != NULL) {
|
||||
ar->istailcall = !!(ci->callstatus & CIST_TAIL);
|
||||
ar->extraargs =
|
||||
cast_uchar((ci->callstatus & MAX_CCMT) >> CIST_CCMT);
|
||||
}
|
||||
else {
|
||||
ar->istailcall = 0;
|
||||
ar->extraargs = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'n': {
|
||||
@ -362,11 +374,11 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
break;
|
||||
}
|
||||
case 'r': {
|
||||
if (ci == NULL || !(ci->callstatus & CIST_TRAN))
|
||||
if (ci == NULL || !(ci->callstatus & CIST_HOOKED))
|
||||
ar->ftransfer = ar->ntransfer = 0;
|
||||
else {
|
||||
ar->ftransfer = ci->u2.transferinfo.ftransfer;
|
||||
ar->ntransfer = ci->u2.transferinfo.ntransfer;
|
||||
ar->ftransfer = L->transferinfo.ftransfer;
|
||||
ar->ntransfer = L->transferinfo.ntransfer;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -417,40 +429,6 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static const char *getobjname (const Proto *p, int lastpc, int reg,
|
||||
const char **name);
|
||||
|
||||
|
||||
/*
|
||||
** Find a "name" for the constant 'c'.
|
||||
*/
|
||||
static void kname (const Proto *p, int c, const char **name) {
|
||||
TValue *kvalue = &p->k[c];
|
||||
*name = (ttisstring(kvalue)) ? svalue(kvalue) : "?";
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Find a "name" for the register 'c'.
|
||||
*/
|
||||
static void rname (const Proto *p, int pc, int c, const char **name) {
|
||||
const char *what = getobjname(p, pc, c, name); /* search for 'c' */
|
||||
if (!(what && *what == 'c')) /* did not find a constant name? */
|
||||
*name = "?";
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Find a "name" for a 'C' value in an RK instruction.
|
||||
*/
|
||||
static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
|
||||
int c = GETARG_C(i); /* key index */
|
||||
if (GETARG_k(i)) /* is 'c' a constant? */
|
||||
kname(p, c, name);
|
||||
else /* 'c' is a register */
|
||||
rname(p, pc, c, name);
|
||||
}
|
||||
|
||||
|
||||
static int filterpc (int pc, int jmptarget) {
|
||||
if (pc < jmptarget) /* is code conditional (inside a jump)? */
|
||||
@ -509,28 +487,29 @@ static int findsetreg (const Proto *p, int lastpc, int reg) {
|
||||
|
||||
|
||||
/*
|
||||
** Check whether table being indexed by instruction 'i' is the
|
||||
** environment '_ENV'
|
||||
** Find a "name" for the constant 'c'.
|
||||
*/
|
||||
static const char *gxf (const Proto *p, int pc, Instruction i, int isup) {
|
||||
int t = GETARG_B(i); /* table index */
|
||||
const char *name; /* name of indexed variable */
|
||||
if (isup) /* is an upvalue? */
|
||||
name = upvalname(p, t);
|
||||
else
|
||||
getobjname(p, pc, t, &name);
|
||||
return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
|
||||
static const char *kname (const Proto *p, int index, const char **name) {
|
||||
TValue *kvalue = &p->k[index];
|
||||
if (ttisstring(kvalue)) {
|
||||
*name = getstr(tsvalue(kvalue));
|
||||
return "constant";
|
||||
}
|
||||
else {
|
||||
*name = "?";
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *getobjname (const Proto *p, int lastpc, int reg,
|
||||
const char **name) {
|
||||
int pc;
|
||||
*name = luaF_getlocalname(p, reg + 1, lastpc);
|
||||
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
|
||||
const char **name) {
|
||||
int pc = *ppc;
|
||||
*name = luaF_getlocalname(p, reg + 1, pc);
|
||||
if (*name) /* is a local? */
|
||||
return "local";
|
||||
return strlocal;
|
||||
/* else try symbolic execution */
|
||||
pc = findsetreg(p, lastpc, reg);
|
||||
*ppc = pc = findsetreg(p, pc, reg);
|
||||
if (pc != -1) { /* could find instruction? */
|
||||
Instruction i = p->code[pc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
@ -538,18 +517,73 @@ static const char *getobjname (const Proto *p, int lastpc, int reg,
|
||||
case OP_MOVE: {
|
||||
int b = GETARG_B(i); /* move from 'b' to 'a' */
|
||||
if (b < GETARG_A(i))
|
||||
return getobjname(p, pc, b, name); /* get name for 'b' */
|
||||
return basicgetobjname(p, ppc, b, name); /* get name for 'b' */
|
||||
break;
|
||||
}
|
||||
case OP_GETUPVAL: {
|
||||
*name = upvalname(p, GETARG_B(i));
|
||||
return strupval;
|
||||
}
|
||||
case OP_LOADK: return kname(p, GETARG_Bx(i), name);
|
||||
case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name);
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return NULL; /* could not find reasonable name */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Find a "name" for the register 'c'.
|
||||
*/
|
||||
static void rname (const Proto *p, int pc, int c, const char **name) {
|
||||
const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
|
||||
if (!(what && *what == 'c')) /* did not find a constant name? */
|
||||
*name = "?";
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Check whether table being indexed by instruction 'i' is the
|
||||
** environment '_ENV'
|
||||
*/
|
||||
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
|
||||
int t = GETARG_B(i); /* table index */
|
||||
const char *name; /* name of indexed variable */
|
||||
if (isup) /* is 't' an upvalue? */
|
||||
name = upvalname(p, t);
|
||||
else { /* 't' is a register */
|
||||
const char *what = basicgetobjname(p, &pc, t, &name);
|
||||
/* 'name' must be the name of a local variable (at the current
|
||||
level or an upvalue) */
|
||||
if (what != strlocal && what != strupval)
|
||||
name = NULL; /* cannot be the variable _ENV */
|
||||
}
|
||||
return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Extend 'basicgetobjname' to handle table accesses
|
||||
*/
|
||||
static const char *getobjname (const Proto *p, int lastpc, int reg,
|
||||
const char **name) {
|
||||
const char *kind = basicgetobjname(p, &lastpc, reg, name);
|
||||
if (kind != NULL)
|
||||
return kind;
|
||||
else if (lastpc != -1) { /* could find instruction? */
|
||||
Instruction i = p->code[lastpc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
switch (op) {
|
||||
case OP_GETTABUP: {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
kname(p, k, name);
|
||||
return gxf(p, pc, i, 1);
|
||||
return isEnv(p, lastpc, i, 1);
|
||||
}
|
||||
case OP_GETTABLE: {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
rname(p, pc, k, name);
|
||||
return gxf(p, pc, i, 0);
|
||||
rname(p, lastpc, k, name);
|
||||
return isEnv(p, lastpc, i, 0);
|
||||
}
|
||||
case OP_GETI: {
|
||||
*name = "integer index";
|
||||
@ -558,24 +592,11 @@ static const char *getobjname (const Proto *p, int lastpc, int reg,
|
||||
case OP_GETFIELD: {
|
||||
int k = GETARG_C(i); /* key index */
|
||||
kname(p, k, name);
|
||||
return gxf(p, pc, i, 0);
|
||||
}
|
||||
case OP_GETUPVAL: {
|
||||
*name = upvalname(p, GETARG_B(i));
|
||||
return "upvalue";
|
||||
}
|
||||
case OP_LOADK:
|
||||
case OP_LOADKX: {
|
||||
int b = (op == OP_LOADK) ? GETARG_Bx(i)
|
||||
: GETARG_Ax(p->code[pc + 1]);
|
||||
if (ttisstring(&p->k[b])) {
|
||||
*name = svalue(&p->k[b]);
|
||||
return "constant";
|
||||
}
|
||||
break;
|
||||
return isEnv(p, lastpc, i, 0);
|
||||
}
|
||||
case OP_SELF: {
|
||||
rkname(p, pc, i, name);
|
||||
int k = GETARG_C(i); /* key index */
|
||||
kname(p, k, name);
|
||||
return "method";
|
||||
}
|
||||
default: break; /* go through to return NULL */
|
||||
@ -627,7 +648,7 @@ static const char *funcnamefromcode (lua_State *L, const Proto *p,
|
||||
default:
|
||||
return NULL; /* cannot find a reasonable name */
|
||||
}
|
||||
*name = getstr(G(L)->tmname[tm]) + 2;
|
||||
*name = getshrstr(G(L)->tmname[tm]) + 2;
|
||||
return "metamethod";
|
||||
}
|
||||
|
||||
@ -684,7 +705,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
|
||||
for (i = 0; i < c->nupvalues; i++) {
|
||||
if (c->upvals[i]->v.p == o) {
|
||||
*name = upvalname(c->p, i);
|
||||
return "upvalue";
|
||||
return strupval;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
@ -793,16 +814,26 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
}
|
||||
|
||||
|
||||
l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k) {
|
||||
const char *globalname = "?"; /* default name if k == 0 */
|
||||
if (k > 0)
|
||||
kname(cl->p, k - 1, &globalname);
|
||||
luaG_runerror(L, "global '%s' already defined", globalname);
|
||||
}
|
||||
|
||||
|
||||
/* add src:line information to 'msg' */
|
||||
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
|
||||
int line) {
|
||||
char buff[LUA_IDSIZE];
|
||||
if (src)
|
||||
luaO_chunkid(buff, getstr(src), tsslen(src));
|
||||
else { /* no source available; use "?" instead */
|
||||
buff[0] = '?'; buff[1] = '\0';
|
||||
if (src == NULL) /* no debug information? */
|
||||
return luaO_pushfstring(L, "?:?: %s", msg);
|
||||
else {
|
||||
char buff[LUA_IDSIZE];
|
||||
size_t idlen;
|
||||
const char *id = getlstr(src, idlen);
|
||||
luaO_chunkid(buff, id, idlen);
|
||||
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
||||
}
|
||||
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
||||
}
|
||||
|
||||
|
||||
@ -815,6 +846,10 @@ l_noret luaG_errormsg (lua_State *L) {
|
||||
L->top.p++; /* assume EXTRA_STACK */
|
||||
luaD_callnoyield(L, L->top.p - 2, 1); /* call it */
|
||||
}
|
||||
if (ttisnil(s2v(L->top.p - 1))) { /* error object is nil? */
|
||||
/* change it to a proper message */
|
||||
setsvalue2s(L, L->top.p - 1, luaS_newliteral(L, "<no error object>"));
|
||||
}
|
||||
luaD_throw(L, LUA_ERRRUN);
|
||||
}
|
||||
|
||||
@ -824,10 +859,9 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||
const char *msg;
|
||||
va_list argp;
|
||||
luaC_checkGC(L); /* error message uses memory */
|
||||
va_start(argp, fmt);
|
||||
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
|
||||
va_end(argp);
|
||||
if (isLua(ci)) { /* if Lua function, add source:line information */
|
||||
pushvfstring(L, argp, fmt, msg);
|
||||
if (isLua(ci)) { /* Lua function? */
|
||||
/* add source:line information */
|
||||
luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
|
||||
setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */
|
||||
L->top.p--;
|
||||
@ -865,6 +899,28 @@ static int changedline (const Proto *p, int oldpc, int newpc) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traces Lua calls. If code is running the first instruction of a function,
|
||||
** and function is not vararg, and it is not coming from an yield,
|
||||
** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall'
|
||||
** after adjusting its variable arguments; otherwise, they could call
|
||||
** a line/count hook before the call hook. Functions coming from
|
||||
** an yield already called 'luaD_hookcall' before yielding.)
|
||||
*/
|
||||
int luaG_tracecall (lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
Proto *p = ci_func(ci)->p;
|
||||
ci->u.l.trap = 1; /* ensure hooks will be checked */
|
||||
if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */
|
||||
if (isvararg(p))
|
||||
return 0; /* hooks will start at VARARGPREP instruction */
|
||||
else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yielded? */
|
||||
luaD_hookcall(L, ci); /* check 'call' hook */
|
||||
}
|
||||
return 1; /* keep 'trap' on */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traces the execution of a Lua function. Called before the execution
|
||||
** of each opcode, when debug is on. 'L->oldpc' stores the last
|
||||
@ -879,7 +935,7 @@ static int changedline (const Proto *p, int oldpc, int newpc) {
|
||||
*/
|
||||
int luaG_traceexec (lua_State *L, const Instruction *pc) {
|
||||
CallInfo *ci = L->ci;
|
||||
lu_byte mask = L->hookmask;
|
||||
lu_byte mask = cast_byte(L->hookmask);
|
||||
const Proto *p = ci_func(ci)->p;
|
||||
int counthook;
|
||||
if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
|
||||
@ -888,16 +944,16 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) {
|
||||
}
|
||||
pc++; /* reference is always next instruction */
|
||||
ci->u.l.savedpc = pc; /* save 'pc' */
|
||||
counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
|
||||
counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
|
||||
if (counthook)
|
||||
resethookcount(L); /* reset count */
|
||||
else if (!(mask & LUA_MASKLINE))
|
||||
return 1; /* no line hook and count != 0; nothing to be done now */
|
||||
if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
|
||||
if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */
|
||||
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
|
||||
return 1; /* do not call hook again (VM yielded, so it did not move) */
|
||||
}
|
||||
if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
|
||||
if (!luaP_isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
|
||||
L->top.p = ci->top.p; /* correct top */
|
||||
if (counthook)
|
||||
luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
|
||||
@ -915,7 +971,6 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) {
|
||||
if (L->status == LUA_YIELD) { /* did hook yield? */
|
||||
if (counthook)
|
||||
L->hookcount = 1; /* undo decrement to zero */
|
||||
ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
|
||||
ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
|
||||
luaD_throw(L, LUA_YIELD);
|
||||
}
|
||||
|
||||
2
ldebug.h
2
ldebug.h
@ -53,11 +53,13 @@ LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k);
|
||||
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
||||
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
|
||||
TString *src, int line);
|
||||
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
||||
LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc);
|
||||
LUAI_FUNC int luaG_tracecall (lua_State *L);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
522
ldo.c
522
ldo.c
@ -38,16 +38,37 @@
|
||||
#define errorstatus(s) ((s) > LUA_YIELD)
|
||||
|
||||
|
||||
/*
|
||||
** these macros allow user-specific actions when a thread is
|
||||
** resumed/yielded.
|
||||
*/
|
||||
#if !defined(luai_userstateresume)
|
||||
#define luai_userstateresume(L,n) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstateyield)
|
||||
#define luai_userstateyield(L,n) ((void)L)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Error-recovery functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/* chained list of long jump buffers */
|
||||
typedef struct lua_longjmp {
|
||||
struct lua_longjmp *previous;
|
||||
jmp_buf b;
|
||||
volatile TStatus status; /* error code */
|
||||
} lua_longjmp;
|
||||
|
||||
|
||||
/*
|
||||
** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
|
||||
** default, Lua handles errors with exceptions when compiling as
|
||||
** C++ code, with _longjmp/_setjmp when asked to use them, and with
|
||||
** C++ code, with _longjmp/_setjmp when available (POSIX), and with
|
||||
** longjmp/setjmp otherwise.
|
||||
*/
|
||||
#if !defined(LUAI_THROW) /* { */
|
||||
@ -56,73 +77,64 @@
|
||||
|
||||
/* C++ exceptions */
|
||||
#define LUAI_THROW(L,c) throw(c)
|
||||
#define LUAI_TRY(L,c,a) \
|
||||
try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
|
||||
#define luai_jmpbuf int /* dummy variable */
|
||||
|
||||
static void LUAI_TRY (lua_State *L, lua_longjmp *c, Pfunc f, void *ud) {
|
||||
try {
|
||||
f(L, ud); /* call function protected */
|
||||
}
|
||||
catch (lua_longjmp *c1) { /* Lua error */
|
||||
if (c1 != c) /* not the correct level? */
|
||||
throw; /* rethrow to upper level */
|
||||
}
|
||||
catch (...) { /* non-Lua exception */
|
||||
c->status = -1; /* create some error code */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined(LUA_USE_POSIX) /* }{ */
|
||||
|
||||
/* in POSIX, try _longjmp/_setjmp (more efficient) */
|
||||
/* in POSIX, use _longjmp/_setjmp (more efficient) */
|
||||
#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
|
||||
#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
|
||||
#define luai_jmpbuf jmp_buf
|
||||
#define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud))
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* ISO C handling with long jumps */
|
||||
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
|
||||
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
|
||||
#define luai_jmpbuf jmp_buf
|
||||
#define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud))
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
|
||||
|
||||
/* chain list of long jump buffers */
|
||||
struct lua_longjmp {
|
||||
struct lua_longjmp *previous;
|
||||
luai_jmpbuf b;
|
||||
volatile int status; /* error code */
|
||||
};
|
||||
|
||||
|
||||
void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
|
||||
switch (errcode) {
|
||||
case LUA_ERRMEM: { /* memory error? */
|
||||
setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
|
||||
break;
|
||||
}
|
||||
case LUA_ERRERR: {
|
||||
setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
|
||||
break;
|
||||
}
|
||||
case LUA_OK: { /* special case only for closing upvalues */
|
||||
setnilvalue(s2v(oldtop)); /* no error message */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
lua_assert(errorstatus(errcode)); /* real error */
|
||||
setobjs2s(L, oldtop, L->top.p - 1); /* error message on current top */
|
||||
break;
|
||||
}
|
||||
void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) {
|
||||
if (errcode == LUA_ERRMEM) { /* memory error? */
|
||||
setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
|
||||
}
|
||||
L->top.p = oldtop + 1;
|
||||
else {
|
||||
lua_assert(errorstatus(errcode)); /* must be a real error */
|
||||
lua_assert(!ttisnil(s2v(L->top.p - 1))); /* with a non-nil object */
|
||||
setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */
|
||||
}
|
||||
L->top.p = oldtop + 1; /* top goes back to old top plus error object */
|
||||
}
|
||||
|
||||
|
||||
l_noret luaD_throw (lua_State *L, int errcode) {
|
||||
l_noret luaD_throw (lua_State *L, TStatus errcode) {
|
||||
if (L->errorJmp) { /* thread has an error handler? */
|
||||
L->errorJmp->status = errcode; /* set status */
|
||||
LUAI_THROW(L, L->errorJmp); /* jump to it */
|
||||
}
|
||||
else { /* thread has no error handler */
|
||||
global_State *g = G(L);
|
||||
lua_State *mainth = mainthread(g);
|
||||
errcode = luaE_resetthread(L, errcode); /* close all upvalues */
|
||||
if (g->mainthread->errorJmp) { /* main thread has a handler? */
|
||||
setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */
|
||||
luaD_throw(g->mainthread, errcode); /* re-throw in main thread */
|
||||
L->status = errcode;
|
||||
if (mainth->errorJmp) { /* main thread has a handler? */
|
||||
setobjs2s(L, mainth->top.p++, L->top.p - 1); /* copy error obj. */
|
||||
luaD_throw(mainth, errcode); /* re-throw in main thread */
|
||||
}
|
||||
else { /* no handler at all; abort */
|
||||
if (g->panic) { /* panic function? */
|
||||
@ -135,15 +147,23 @@ l_noret luaD_throw (lua_State *L, int errcode) {
|
||||
}
|
||||
|
||||
|
||||
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) {
|
||||
if (L->errorJmp) {
|
||||
/* unroll error entries up to the first level */
|
||||
while (L->errorJmp->previous != NULL)
|
||||
L->errorJmp = L->errorJmp->previous;
|
||||
}
|
||||
luaD_throw(L, errcode);
|
||||
}
|
||||
|
||||
|
||||
TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
l_uint32 oldnCcalls = L->nCcalls;
|
||||
struct lua_longjmp lj;
|
||||
lua_longjmp lj;
|
||||
lj.status = LUA_OK;
|
||||
lj.previous = L->errorJmp; /* chain new error handler */
|
||||
L->errorJmp = &lj;
|
||||
LUAI_TRY(L, &lj,
|
||||
(*f)(L, ud);
|
||||
);
|
||||
LUAI_TRY(L, &lj, f, ud); /* call 'f' catching errors */
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
L->nCcalls = oldnCcalls;
|
||||
return lj.status;
|
||||
@ -158,7 +178,82 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/* some stack space for error handling */
|
||||
#define STACKERRSPACE 200
|
||||
|
||||
|
||||
/*
|
||||
** LUAI_MAXSTACK limits the size of the Lua stack.
|
||||
** It must fit into INT_MAX/2.
|
||||
*/
|
||||
|
||||
#if !defined(LUAI_MAXSTACK)
|
||||
#if 1000000 < (INT_MAX / 2)
|
||||
#define LUAI_MAXSTACK 1000000
|
||||
#else
|
||||
#define LUAI_MAXSTACK (INT_MAX / 2u)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum stack size that respects size_t */
|
||||
#define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE)
|
||||
|
||||
/*
|
||||
** Minimum between LUAI_MAXSTACK and MAXSTACK_BYSIZET
|
||||
** (Maximum size for the stack must respect size_t.)
|
||||
*/
|
||||
#define MAXSTACK cast_int(LUAI_MAXSTACK < MAXSTACK_BYSIZET \
|
||||
? LUAI_MAXSTACK : MAXSTACK_BYSIZET)
|
||||
|
||||
|
||||
/* stack size with extra space for error handling */
|
||||
#define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE)
|
||||
|
||||
|
||||
/* raise a stack error while running the message handler */
|
||||
l_noret luaD_errerr (lua_State *L) {
|
||||
TString *msg = luaS_newliteral(L, "error in error handling");
|
||||
setsvalue2s(L, L->top.p, msg);
|
||||
L->top.p++; /* assume EXTRA_STACK */
|
||||
luaD_throw(L, LUA_ERRERR);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Check whether stacks have enough space to run a simple function (such
|
||||
** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack, two
|
||||
** available CallInfos, and two "slots" in the C stack.
|
||||
*/
|
||||
int luaD_checkminstack (lua_State *L) {
|
||||
if (getCcalls(L) >= LUAI_MAXCCALLS - 2)
|
||||
return 0; /* not enough C-stack slots */
|
||||
if (L->ci->next == NULL && luaE_extendCI(L, 0) == NULL)
|
||||
return 0; /* unable to allocate first ci */
|
||||
if (L->ci->next->next == NULL && luaE_extendCI(L, 0) == NULL)
|
||||
return 0; /* unable to allocate second ci */
|
||||
if (L->stack_last.p - L->top.p >= BASIC_STACK_SIZE)
|
||||
return 1; /* enough (BASIC_STACK_SIZE) free slots in the Lua stack */
|
||||
else /* try to grow stack to a size with enough free slots */
|
||||
return luaD_growstack(L, BASIC_STACK_SIZE, 0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** In ISO C, any pointer use after the pointer has been deallocated is
|
||||
** undefined behavior. So, before a stack reallocation, all pointers
|
||||
** should be changed to offsets, and after the reallocation they should
|
||||
** be changed back to pointers. As during the reallocation the pointers
|
||||
** are invalid, the reallocation cannot run emergency collections.
|
||||
** Alternatively, we can use the old address after the deallocation.
|
||||
** That is not strict ISO C, but seems to work fine everywhere.
|
||||
** The following macro chooses how strict is the code.
|
||||
*/
|
||||
#if !defined(LUAI_STRICT_ADDRESS)
|
||||
#define LUAI_STRICT_ADDRESS 1
|
||||
#endif
|
||||
|
||||
#if LUAI_STRICT_ADDRESS
|
||||
/*
|
||||
** Change all pointers to the stack into offsets.
|
||||
*/
|
||||
@ -179,9 +274,10 @@ static void relstack (lua_State *L) {
|
||||
/*
|
||||
** Change back all offsets into pointers.
|
||||
*/
|
||||
static void correctstack (lua_State *L) {
|
||||
static void correctstack (lua_State *L, StkId oldstack) {
|
||||
CallInfo *ci;
|
||||
UpVal *up;
|
||||
UNUSED(oldstack);
|
||||
L->top.p = restorestack(L, L->top.offset);
|
||||
L->tbclist.p = restorestack(L, L->tbclist.offset);
|
||||
for (up = L->openupval; up != NULL; up = up->u.open.next)
|
||||
@ -194,18 +290,40 @@ static void correctstack (lua_State *L) {
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
/*
|
||||
** Assume that it is fine to use an address after its deallocation,
|
||||
** as long as we do not dereference it.
|
||||
*/
|
||||
|
||||
static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */
|
||||
|
||||
|
||||
/*
|
||||
** Correct pointers into 'oldstack' to point into 'L->stack'.
|
||||
*/
|
||||
static void correctstack (lua_State *L, StkId oldstack) {
|
||||
CallInfo *ci;
|
||||
UpVal *up;
|
||||
StkId newstack = L->stack.p;
|
||||
if (oldstack == newstack)
|
||||
return;
|
||||
L->top.p = L->top.p - oldstack + newstack;
|
||||
L->tbclist.p = L->tbclist.p - oldstack + newstack;
|
||||
for (up = L->openupval; up != NULL; up = up->u.open.next)
|
||||
up->v.p = s2v(uplevel(up) - oldstack + newstack);
|
||||
for (ci = L->ci; ci != NULL; ci = ci->previous) {
|
||||
ci->top.p = ci->top.p - oldstack + newstack;
|
||||
ci->func.p = ci->func.p - oldstack + newstack;
|
||||
if (isLua(ci))
|
||||
ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* some space for error handling */
|
||||
#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
|
||||
|
||||
/*
|
||||
** Reallocate the stack to a new size, correcting all pointers into it.
|
||||
** In ISO C, any pointer use after the pointer has been deallocated is
|
||||
** undefined behavior. So, before the reallocation, all pointers are
|
||||
** changed to offsets, and after the reallocation they are changed back
|
||||
** to pointers. As during the reallocation the pointers are invalid, the
|
||||
** reallocation cannot run emergency collections.
|
||||
**
|
||||
** In case of allocation error, raise an error or return false according
|
||||
** to 'raiseerror'.
|
||||
*/
|
||||
@ -213,21 +331,22 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
|
||||
int oldsize = stacksize(L);
|
||||
int i;
|
||||
StkId newstack;
|
||||
int oldgcstop = G(L)->gcstopem;
|
||||
lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
|
||||
StkId oldstack = L->stack.p;
|
||||
lu_byte oldgcstop = G(L)->gcstopem;
|
||||
lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE);
|
||||
relstack(L); /* change pointers to offsets */
|
||||
G(L)->gcstopem = 1; /* stop emergency collection */
|
||||
newstack = luaM_reallocvector(L, L->stack.p, oldsize + EXTRA_STACK,
|
||||
newstack = luaM_reallocvector(L, oldstack, oldsize + EXTRA_STACK,
|
||||
newsize + EXTRA_STACK, StackValue);
|
||||
G(L)->gcstopem = oldgcstop; /* restore emergency collection */
|
||||
if (l_unlikely(newstack == NULL)) { /* reallocation failed? */
|
||||
correctstack(L); /* change offsets back to pointers */
|
||||
correctstack(L, oldstack); /* change offsets back to pointers */
|
||||
if (raiseerror)
|
||||
luaM_error(L);
|
||||
else return 0; /* do not raise an error */
|
||||
}
|
||||
L->stack.p = newstack;
|
||||
correctstack(L); /* change offsets back to pointers */
|
||||
correctstack(L, oldstack); /* change offsets back to pointers */
|
||||
L->stack_last.p = L->stack.p + newsize;
|
||||
for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++)
|
||||
setnilvalue(s2v(newstack + i)); /* erase new segment */
|
||||
@ -241,23 +360,23 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
|
||||
*/
|
||||
int luaD_growstack (lua_State *L, int n, int raiseerror) {
|
||||
int size = stacksize(L);
|
||||
if (l_unlikely(size > LUAI_MAXSTACK)) {
|
||||
if (l_unlikely(size > MAXSTACK)) {
|
||||
/* if stack is larger than maximum, thread is already using the
|
||||
extra space reserved for errors, that is, thread is handling
|
||||
a stack error; cannot grow further than that. */
|
||||
lua_assert(stacksize(L) == ERRORSTACKSIZE);
|
||||
if (raiseerror)
|
||||
luaD_throw(L, LUA_ERRERR); /* error inside message handler */
|
||||
luaD_errerr(L); /* stack error inside message handler */
|
||||
return 0; /* if not 'raiseerror', just signal it */
|
||||
}
|
||||
else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */
|
||||
int newsize = 2 * size; /* tentative new size */
|
||||
else if (n < MAXSTACK) { /* avoids arithmetic overflows */
|
||||
int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */
|
||||
int needed = cast_int(L->top.p - L->stack.p) + n;
|
||||
if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */
|
||||
newsize = LUAI_MAXSTACK;
|
||||
if (newsize > MAXSTACK) /* cannot cross the limit */
|
||||
newsize = MAXSTACK;
|
||||
if (newsize < needed) /* but must respect what was asked for */
|
||||
newsize = needed;
|
||||
if (l_likely(newsize <= LUAI_MAXSTACK))
|
||||
if (l_likely(newsize <= MAXSTACK))
|
||||
return luaD_reallocstack(L, newsize, raiseerror);
|
||||
}
|
||||
/* else stack overflow */
|
||||
@ -293,28 +412,28 @@ static int stackinuse (lua_State *L) {
|
||||
** to twice the current use. (So, the final stack size is at most 2/3 the
|
||||
** previous size, and half of its entries are empty.)
|
||||
** As a particular case, if stack was handling a stack overflow and now
|
||||
** it is not, 'max' (limited by LUAI_MAXSTACK) will be smaller than
|
||||
** it is not, 'max' (limited by MAXSTACK) will be smaller than
|
||||
** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack
|
||||
** will be reduced to a "regular" size.
|
||||
*/
|
||||
void luaD_shrinkstack (lua_State *L) {
|
||||
int inuse = stackinuse(L);
|
||||
int max = (inuse > LUAI_MAXSTACK / 3) ? LUAI_MAXSTACK : inuse * 3;
|
||||
int max = (inuse > MAXSTACK / 3) ? MAXSTACK : inuse * 3;
|
||||
/* if thread is currently not handling a stack overflow and its
|
||||
size is larger than maximum "reasonable" size, shrink it */
|
||||
if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) {
|
||||
int nsize = (inuse > LUAI_MAXSTACK / 2) ? LUAI_MAXSTACK : inuse * 2;
|
||||
if (inuse <= MAXSTACK && stacksize(L) > max) {
|
||||
int nsize = (inuse > MAXSTACK / 2) ? MAXSTACK : inuse * 2;
|
||||
luaD_reallocstack(L, nsize, 0); /* ok if that fails */
|
||||
}
|
||||
else /* don't change stack */
|
||||
condmovestack(L,{},{}); /* (change only for debugging) */
|
||||
condmovestack(L,(void)0,(void)0); /* (change only for debugging) */
|
||||
luaE_shrinkCI(L); /* shrink CI list */
|
||||
}
|
||||
|
||||
|
||||
void luaD_inctop (lua_State *L) {
|
||||
luaD_checkstack(L, 1);
|
||||
L->top.p++;
|
||||
luaD_checkstack(L, 1);
|
||||
}
|
||||
|
||||
/* }================================================================== */
|
||||
@ -329,7 +448,6 @@ void luaD_hook (lua_State *L, int event, int line,
|
||||
int ftransfer, int ntransfer) {
|
||||
lua_Hook hook = L->hook;
|
||||
if (hook && L->allowhook) { /* make sure there is a hook */
|
||||
int mask = CIST_HOOKED;
|
||||
CallInfo *ci = L->ci;
|
||||
ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */
|
||||
ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */
|
||||
@ -337,18 +455,15 @@ void luaD_hook (lua_State *L, int event, int line,
|
||||
ar.event = event;
|
||||
ar.currentline = line;
|
||||
ar.i_ci = ci;
|
||||
if (ntransfer != 0) {
|
||||
mask |= CIST_TRAN; /* 'ci' has transfer information */
|
||||
ci->u2.transferinfo.ftransfer = ftransfer;
|
||||
ci->u2.transferinfo.ntransfer = ntransfer;
|
||||
}
|
||||
L->transferinfo.ftransfer = ftransfer;
|
||||
L->transferinfo.ntransfer = ntransfer;
|
||||
if (isLua(ci) && L->top.p < ci->top.p)
|
||||
L->top.p = ci->top.p; /* protect entire activation register */
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
if (ci->top.p < L->top.p + LUA_MINSTACK)
|
||||
ci->top.p = L->top.p + LUA_MINSTACK;
|
||||
L->allowhook = 0; /* cannot call hooks inside a hook */
|
||||
ci->callstatus |= mask;
|
||||
ci->callstatus |= CIST_HOOKED;
|
||||
lua_unlock(L);
|
||||
(*hook)(L, &ar);
|
||||
lua_lock(L);
|
||||
@ -356,7 +471,7 @@ void luaD_hook (lua_State *L, int event, int line,
|
||||
L->allowhook = 1;
|
||||
ci->top.p = restorestack(L, ci_top);
|
||||
L->top.p = restorestack(L, top);
|
||||
ci->callstatus &= ~mask;
|
||||
ci->callstatus &= ~CIST_HOOKED;
|
||||
}
|
||||
}
|
||||
|
||||
@ -391,11 +506,11 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) {
|
||||
int ftransfer;
|
||||
if (isLua(ci)) {
|
||||
Proto *p = ci_func(ci)->p;
|
||||
if (p->is_vararg)
|
||||
if (p->flag & PF_VAHID)
|
||||
delta = ci->u.l.nextraargs + p->numparams + 1;
|
||||
}
|
||||
ci->func.p += delta; /* if vararg, back to virtual 'func' */
|
||||
ftransfer = cast(unsigned short, firstres - ci->func.p);
|
||||
ftransfer = cast_int(firstres - ci->func.p);
|
||||
luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */
|
||||
ci->func.p -= delta;
|
||||
}
|
||||
@ -406,66 +521,34 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) {
|
||||
|
||||
/*
|
||||
** Check whether 'func' has a '__call' metafield. If so, put it in the
|
||||
** stack, below original 'func', so that 'luaD_precall' can call it. Raise
|
||||
** an error if there is no '__call' metafield.
|
||||
** stack, below original 'func', so that 'luaD_precall' can call it.
|
||||
** Raise an error if there is no '__call' metafield.
|
||||
** Bits CIST_CCMT in status count how many _call metamethods were
|
||||
** invoked and how many corresponding extra arguments were pushed.
|
||||
** (This count will be saved in the 'callstatus' of the call).
|
||||
** Raise an error if this counter overflows.
|
||||
*/
|
||||
StkId luaD_tryfuncTM (lua_State *L, StkId func) {
|
||||
static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) {
|
||||
const TValue *tm;
|
||||
StkId p;
|
||||
checkstackGCp(L, 1, func); /* space for metamethod */
|
||||
tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */
|
||||
if (l_unlikely(ttisnil(tm)))
|
||||
luaG_callerror(L, s2v(func)); /* nothing to call */
|
||||
tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);
|
||||
if (l_unlikely(ttisnil(tm))) /* no metamethod? */
|
||||
luaG_callerror(L, s2v(func));
|
||||
for (p = L->top.p; p > func; p--) /* open space for metamethod */
|
||||
setobjs2s(L, p, p-1);
|
||||
L->top.p++; /* stack space pre-allocated by the caller */
|
||||
setobj2s(L, func, tm); /* metamethod is the new function to be called */
|
||||
return func;
|
||||
if ((status & MAX_CCMT) == MAX_CCMT) /* is counter full? */
|
||||
luaG_runerror(L, "'__call' chain too long");
|
||||
return status + (1u << CIST_CCMT); /* increment counter */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'.
|
||||
** Handle most typical cases (zero results for commands, one result for
|
||||
** expressions, multiple results for tail calls/single parameters)
|
||||
** separated.
|
||||
*/
|
||||
l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
|
||||
StkId firstresult;
|
||||
/* Generic case for 'moveresult' */
|
||||
l_sinline void genmoveresults (lua_State *L, StkId res, int nres,
|
||||
int wanted) {
|
||||
StkId firstresult = L->top.p - nres; /* index of first result */
|
||||
int i;
|
||||
switch (wanted) { /* handle typical cases separately */
|
||||
case 0: /* no values needed */
|
||||
L->top.p = res;
|
||||
return;
|
||||
case 1: /* one value needed */
|
||||
if (nres == 0) /* no results? */
|
||||
setnilvalue(s2v(res)); /* adjust with nil */
|
||||
else /* at least one result */
|
||||
setobjs2s(L, res, L->top.p - nres); /* move it to proper place */
|
||||
L->top.p = res + 1;
|
||||
return;
|
||||
case LUA_MULTRET:
|
||||
wanted = nres; /* we want all results */
|
||||
break;
|
||||
default: /* two/more results and/or to-be-closed variables */
|
||||
if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */
|
||||
L->ci->callstatus |= CIST_CLSRET; /* in case of yields */
|
||||
L->ci->u2.nres = nres;
|
||||
res = luaF_close(L, res, CLOSEKTOP, 1);
|
||||
L->ci->callstatus &= ~CIST_CLSRET;
|
||||
if (L->hookmask) { /* if needed, call hook after '__close's */
|
||||
ptrdiff_t savedres = savestack(L, res);
|
||||
rethook(L, L->ci, nres);
|
||||
res = restorestack(L, savedres); /* hook can move stack */
|
||||
}
|
||||
wanted = decodeNresults(wanted);
|
||||
if (wanted == LUA_MULTRET)
|
||||
wanted = nres; /* we want all results */
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* generic case */
|
||||
firstresult = L->top.p - nres; /* index of first result */
|
||||
if (nres > wanted) /* extra results? */
|
||||
nres = wanted; /* don't need them */
|
||||
for (i = 0; i < nres; i++) /* move all results to correct place */
|
||||
@ -476,6 +559,51 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given 'nres' results at 'firstResult', move 'fwanted-1' of them
|
||||
** to 'res'. Handle most typical cases (zero results for commands,
|
||||
** one result for expressions, multiple results for tail calls/single
|
||||
** parameters) separated. The flag CIST_TBC in 'fwanted', if set,
|
||||
** forces the switch to go to the default case.
|
||||
*/
|
||||
l_sinline void moveresults (lua_State *L, StkId res, int nres,
|
||||
l_uint32 fwanted) {
|
||||
switch (fwanted) { /* handle typical cases separately */
|
||||
case 0 + 1: /* no values needed */
|
||||
L->top.p = res;
|
||||
return;
|
||||
case 1 + 1: /* one value needed */
|
||||
if (nres == 0) /* no results? */
|
||||
setnilvalue(s2v(res)); /* adjust with nil */
|
||||
else /* at least one result */
|
||||
setobjs2s(L, res, L->top.p - nres); /* move it to proper place */
|
||||
L->top.p = res + 1;
|
||||
return;
|
||||
case LUA_MULTRET + 1:
|
||||
genmoveresults(L, res, nres, nres); /* we want all results */
|
||||
break;
|
||||
default: { /* two/more results and/or to-be-closed variables */
|
||||
int wanted = get_nresults(fwanted);
|
||||
if (fwanted & CIST_TBC) { /* to-be-closed variables? */
|
||||
L->ci->u2.nres = nres;
|
||||
L->ci->callstatus |= CIST_CLSRET; /* in case of yields */
|
||||
res = luaF_close(L, res, CLOSEKTOP, 1);
|
||||
L->ci->callstatus &= ~CIST_CLSRET;
|
||||
if (L->hookmask) { /* if needed, call hook after '__close's */
|
||||
ptrdiff_t savedres = savestack(L, res);
|
||||
rethook(L, L->ci, nres);
|
||||
res = restorestack(L, savedres); /* hook can move stack */
|
||||
}
|
||||
if (wanted == LUA_MULTRET)
|
||||
wanted = nres; /* we want all results */
|
||||
}
|
||||
genmoveresults(L, res, nres, wanted);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Finishes a function call: calls hook if necessary, moves current
|
||||
** number of results to proper place, and returns to previous call
|
||||
@ -483,28 +611,34 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
|
||||
** that.
|
||||
*/
|
||||
void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
|
||||
int wanted = ci->nresults;
|
||||
if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted)))
|
||||
l_uint32 fwanted = ci->callstatus & (CIST_TBC | CIST_NRESULTS);
|
||||
if (l_unlikely(L->hookmask) && !(fwanted & CIST_TBC))
|
||||
rethook(L, ci, nres);
|
||||
/* move results to proper place */
|
||||
moveresults(L, ci->func.p, nres, wanted);
|
||||
moveresults(L, ci->func.p, nres, fwanted);
|
||||
/* function cannot be in any of these cases when returning */
|
||||
lua_assert(!(ci->callstatus &
|
||||
(CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET)));
|
||||
(CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_CLSRET)));
|
||||
L->ci = ci->previous; /* back to caller (after closing variables) */
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L))
|
||||
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L, 1))
|
||||
|
||||
|
||||
l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret,
|
||||
int mask, StkId top) {
|
||||
/*
|
||||
** Allocate and initialize CallInfo structure. At this point, the
|
||||
** only valid fields in the call status are number of results,
|
||||
** CIST_C (if it's a C function), and number of extra arguments.
|
||||
** (All these bit-fields fit in 16-bit values.)
|
||||
*/
|
||||
l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status,
|
||||
StkId top) {
|
||||
CallInfo *ci = L->ci = next_ci(L); /* new frame */
|
||||
ci->func.p = func;
|
||||
ci->nresults = nret;
|
||||
ci->callstatus = mask;
|
||||
lua_assert((status & ~(CIST_NRESULTS | CIST_C | MAX_CCMT)) == 0);
|
||||
ci->callstatus = status;
|
||||
ci->top.p = top;
|
||||
return ci;
|
||||
}
|
||||
@ -513,12 +647,12 @@ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret,
|
||||
/*
|
||||
** precall for C functions
|
||||
*/
|
||||
l_sinline int precallC (lua_State *L, StkId func, int nresults,
|
||||
l_sinline int precallC (lua_State *L, StkId func, unsigned status,
|
||||
lua_CFunction f) {
|
||||
int n; /* number of returns */
|
||||
CallInfo *ci;
|
||||
checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
|
||||
L->ci = ci = prepCallInfo(L, func, nresults, CIST_C,
|
||||
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
|
||||
L->ci = ci = prepCallInfo(L, func, status | CIST_C,
|
||||
L->top.p + LUA_MINSTACK);
|
||||
lua_assert(ci->top.p <= L->stack_last.p);
|
||||
if (l_unlikely(L->hookmask & LUA_MASKCALL)) {
|
||||
@ -542,18 +676,19 @@ l_sinline int precallC (lua_State *L, StkId func, int nresults,
|
||||
*/
|
||||
int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
|
||||
int narg1, int delta) {
|
||||
unsigned status = LUA_MULTRET + 1;
|
||||
retry:
|
||||
switch (ttypetag(s2v(func))) {
|
||||
case LUA_VCCL: /* C closure */
|
||||
return precallC(L, func, LUA_MULTRET, clCvalue(s2v(func))->f);
|
||||
return precallC(L, func, status, clCvalue(s2v(func))->f);
|
||||
case LUA_VLCF: /* light C function */
|
||||
return precallC(L, func, LUA_MULTRET, fvalue(s2v(func)));
|
||||
return precallC(L, func, status, fvalue(s2v(func)));
|
||||
case LUA_VLCL: { /* Lua function */
|
||||
Proto *p = clLvalue(s2v(func))->p;
|
||||
int fsize = p->maxstacksize; /* frame size */
|
||||
int nfixparams = p->numparams;
|
||||
int i;
|
||||
checkstackGCp(L, fsize - delta, func);
|
||||
checkstackp(L, fsize - delta, func);
|
||||
ci->func.p -= delta; /* restore 'func' (if vararg) */
|
||||
for (i = 0; i < narg1; i++) /* move down function and arguments */
|
||||
setobjs2s(L, ci->func.p + i, func + i);
|
||||
@ -568,8 +703,8 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
|
||||
return -1;
|
||||
}
|
||||
default: { /* not a function */
|
||||
func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */
|
||||
/* return luaD_pretailcall(L, ci, func, narg1 + 1, delta); */
|
||||
checkstackp(L, 1, func); /* space for metamethod */
|
||||
status = tryfuncTM(L, func, status); /* try '__call' metamethod */
|
||||
narg1++;
|
||||
goto retry; /* try again */
|
||||
}
|
||||
@ -586,13 +721,15 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
|
||||
** original function position.
|
||||
*/
|
||||
CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
unsigned status = cast_uint(nresults + 1);
|
||||
lua_assert(status <= MAXRESULTS + 1);
|
||||
retry:
|
||||
switch (ttypetag(s2v(func))) {
|
||||
case LUA_VCCL: /* C closure */
|
||||
precallC(L, func, nresults, clCvalue(s2v(func))->f);
|
||||
precallC(L, func, status, clCvalue(s2v(func))->f);
|
||||
return NULL;
|
||||
case LUA_VLCF: /* light C function */
|
||||
precallC(L, func, nresults, fvalue(s2v(func)));
|
||||
precallC(L, func, status, fvalue(s2v(func)));
|
||||
return NULL;
|
||||
case LUA_VLCL: { /* Lua function */
|
||||
CallInfo *ci;
|
||||
@ -600,8 +737,8 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */
|
||||
int nfixparams = p->numparams;
|
||||
int fsize = p->maxstacksize; /* frame size */
|
||||
checkstackGCp(L, fsize, func);
|
||||
L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize);
|
||||
checkstackp(L, fsize, func);
|
||||
L->ci = ci = prepCallInfo(L, func, status, func + 1 + fsize);
|
||||
ci->u.l.savedpc = p->code; /* starting point */
|
||||
for (; narg < nfixparams; narg++)
|
||||
setnilvalue(s2v(L->top.p++)); /* complete missing arguments */
|
||||
@ -609,8 +746,8 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
return ci;
|
||||
}
|
||||
default: { /* not a function */
|
||||
func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */
|
||||
/* return luaD_precall(L, func, nresults); */
|
||||
checkstackp(L, 1, func); /* space for metamethod */
|
||||
status = tryfuncTM(L, func, status); /* try '__call' metamethod */
|
||||
goto retry; /* try again with metamethod */
|
||||
}
|
||||
}
|
||||
@ -633,7 +770,7 @@ l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) {
|
||||
luaE_checkcstack(L);
|
||||
}
|
||||
if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */
|
||||
ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */
|
||||
ci->callstatus |= CIST_FRESH; /* mark that it is a "fresh" execute */
|
||||
luaV_execute(L, ci); /* call it */
|
||||
}
|
||||
L->nCcalls -= inc;
|
||||
@ -672,13 +809,13 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
|
||||
** particular, field CIST_RECST preserves the error status across these
|
||||
** multiple runs, changing only if there is a new error.
|
||||
*/
|
||||
static int finishpcallk (lua_State *L, CallInfo *ci) {
|
||||
int status = getcistrecst(ci); /* get original status */
|
||||
static TStatus finishpcallk (lua_State *L, CallInfo *ci) {
|
||||
TStatus status = getcistrecst(ci); /* get original status */
|
||||
if (l_likely(status == LUA_OK)) /* no error? */
|
||||
status = LUA_YIELD; /* was interrupted by an yield */
|
||||
else { /* error */
|
||||
StkId func = restorestack(L, ci->u2.funcidx);
|
||||
L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */
|
||||
L->allowhook = getoah(ci); /* restore 'allowhook' */
|
||||
func = luaF_close(L, func, status, 1); /* can yield or raise an error */
|
||||
luaD_seterrorobj(L, status, func);
|
||||
luaD_shrinkstack(L); /* restore stack size in case of overflow */
|
||||
@ -707,20 +844,21 @@ static int finishpcallk (lua_State *L, CallInfo *ci) {
|
||||
*/
|
||||
static void finishCcall (lua_State *L, CallInfo *ci) {
|
||||
int n; /* actual number of results from C function */
|
||||
if (ci->callstatus & CIST_CLSRET) { /* was returning? */
|
||||
lua_assert(hastocloseCfunc(ci->nresults));
|
||||
if (ci->callstatus & CIST_CLSRET) { /* was closing TBC variable? */
|
||||
lua_assert(ci->callstatus & CIST_TBC);
|
||||
n = ci->u2.nres; /* just redo 'luaD_poscall' */
|
||||
/* don't need to reset CIST_CLSRET, as it will be set again anyway */
|
||||
}
|
||||
else {
|
||||
int status = LUA_YIELD; /* default if there were no errors */
|
||||
TStatus status = LUA_YIELD; /* default if there were no errors */
|
||||
lua_KFunction kf = ci->u.c.k; /* continuation function */
|
||||
/* must have a continuation and must be able to call it */
|
||||
lua_assert(ci->u.c.k != NULL && yieldable(L));
|
||||
lua_assert(kf != NULL && yieldable(L));
|
||||
if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */
|
||||
status = finishpcallk(L, ci); /* finish it */
|
||||
adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */
|
||||
lua_unlock(L);
|
||||
n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */
|
||||
n = (*kf)(L, APIstatus(status), ci->u.c.ctx); /* call continuation */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
}
|
||||
@ -767,6 +905,7 @@ static CallInfo *findpcall (lua_State *L) {
|
||||
** coroutine error handler and should not kill the coroutine.)
|
||||
*/
|
||||
static int resume_error (lua_State *L, const char *msg, int narg) {
|
||||
api_checkpop(L, narg);
|
||||
L->top.p -= narg; /* remove args from the stack */
|
||||
setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */
|
||||
api_incr_top(L);
|
||||
@ -792,6 +931,10 @@ static void resume (lua_State *L, void *ud) {
|
||||
lua_assert(L->status == LUA_YIELD);
|
||||
L->status = LUA_OK; /* mark that it is running (again) */
|
||||
if (isLua(ci)) { /* yielded inside a hook? */
|
||||
/* undo increment made by 'luaG_traceexec': instruction was not
|
||||
executed yet */
|
||||
lua_assert(ci->callstatus & CIST_HOOKYIELD);
|
||||
ci->u.l.savedpc--;
|
||||
L->top.p = firstArg; /* discard arguments */
|
||||
luaV_execute(L, ci); /* just continue running Lua code */
|
||||
}
|
||||
@ -817,7 +960,7 @@ static void resume (lua_State *L, void *ud) {
|
||||
** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't
|
||||
** find a recover point).
|
||||
*/
|
||||
static int precover (lua_State *L, int status) {
|
||||
static TStatus precover (lua_State *L, TStatus status) {
|
||||
CallInfo *ci;
|
||||
while (errorstatus(status) && (ci = findpcall(L)) != NULL) {
|
||||
L->ci = ci; /* go down to recovery functions */
|
||||
@ -830,7 +973,7 @@ static int precover (lua_State *L, int status) {
|
||||
|
||||
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
|
||||
int *nresults) {
|
||||
int status;
|
||||
TStatus status;
|
||||
lua_lock(L);
|
||||
if (L->status == LUA_OK) { /* may be starting a coroutine */
|
||||
if (L->ci != &L->base_ci) /* not in base level? */
|
||||
@ -845,21 +988,21 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
|
||||
return resume_error(L, "C stack overflow", nargs);
|
||||
L->nCcalls++;
|
||||
luai_userstateresume(L, nargs);
|
||||
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
||||
api_checkpop(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
||||
status = luaD_rawrunprotected(L, resume, &nargs);
|
||||
/* continue running after recoverable errors */
|
||||
status = precover(L, status);
|
||||
if (l_likely(!errorstatus(status)))
|
||||
lua_assert(status == L->status); /* normal end or yield */
|
||||
else { /* unrecoverable error */
|
||||
L->status = cast_byte(status); /* mark thread as 'dead' */
|
||||
L->status = status; /* mark thread as 'dead' */
|
||||
luaD_seterrorobj(L, status, L->top.p); /* push error message */
|
||||
L->ci->top.p = L->top.p;
|
||||
}
|
||||
*nresults = (status == LUA_YIELD) ? L->ci->u2.nyield
|
||||
: cast_int(L->top.p - (L->ci->func.p + 1));
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
return APIstatus(status);
|
||||
}
|
||||
|
||||
|
||||
@ -874,9 +1017,9 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
||||
luai_userstateyield(L, nresults);
|
||||
lua_lock(L);
|
||||
ci = L->ci;
|
||||
api_checknelems(L, nresults);
|
||||
api_checkpop(L, nresults);
|
||||
if (l_unlikely(!yieldable(L))) {
|
||||
if (L != G(L)->mainthread)
|
||||
if (L != mainthread(G(L)))
|
||||
luaG_runerror(L, "attempt to yield across a C-call boundary");
|
||||
else
|
||||
luaG_runerror(L, "attempt to yield from outside a coroutine");
|
||||
@ -904,7 +1047,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
||||
*/
|
||||
struct CloseP {
|
||||
StkId level;
|
||||
int status;
|
||||
TStatus status;
|
||||
};
|
||||
|
||||
|
||||
@ -921,7 +1064,7 @@ static void closepaux (lua_State *L, void *ud) {
|
||||
** Calls 'luaF_close' in protected mode. Return the original status
|
||||
** or, in case of errors, the new status.
|
||||
*/
|
||||
int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
|
||||
TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) {
|
||||
CallInfo *old_ci = L->ci;
|
||||
lu_byte old_allowhooks = L->allowhook;
|
||||
for (;;) { /* keep closing upvalues until no more errors */
|
||||
@ -943,9 +1086,9 @@ int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
|
||||
** thread information ('allowhook', etc.) and in particular
|
||||
** its stack level in case of errors.
|
||||
*/
|
||||
int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
ptrdiff_t old_top, ptrdiff_t ef) {
|
||||
int status;
|
||||
TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top,
|
||||
ptrdiff_t ef) {
|
||||
TStatus status;
|
||||
CallInfo *old_ci = L->ci;
|
||||
lu_byte old_allowhooks = L->allowhook;
|
||||
ptrdiff_t old_errfunc = L->errfunc;
|
||||
@ -977,7 +1120,7 @@ struct SParser { /* data to 'f_parser' */
|
||||
|
||||
|
||||
static void checkmode (lua_State *L, const char *mode, const char *x) {
|
||||
if (mode && strchr(mode, x[0]) == NULL) {
|
||||
if (strchr(mode, x[0]) == NULL) {
|
||||
luaO_pushfstring(L,
|
||||
"attempt to load a %s chunk (mode is '%s')", x, mode);
|
||||
luaD_throw(L, LUA_ERRSYNTAX);
|
||||
@ -988,13 +1131,18 @@ static void checkmode (lua_State *L, const char *mode, const char *x) {
|
||||
static void f_parser (lua_State *L, void *ud) {
|
||||
LClosure *cl;
|
||||
struct SParser *p = cast(struct SParser *, ud);
|
||||
const char *mode = p->mode ? p->mode : "bt";
|
||||
int c = zgetc(p->z); /* read first character */
|
||||
if (c == LUA_SIGNATURE[0]) {
|
||||
checkmode(L, p->mode, "binary");
|
||||
cl = luaU_undump(L, p->z, p->name);
|
||||
int fixed = 0;
|
||||
if (strchr(mode, 'B') != NULL)
|
||||
fixed = 1;
|
||||
else
|
||||
checkmode(L, mode, "binary");
|
||||
cl = luaU_undump(L, p->z, p->name, fixed);
|
||||
}
|
||||
else {
|
||||
checkmode(L, p->mode, "text");
|
||||
checkmode(L, mode, "text");
|
||||
cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
|
||||
}
|
||||
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
||||
@ -1002,10 +1150,10 @@ static void f_parser (lua_State *L, void *ud) {
|
||||
}
|
||||
|
||||
|
||||
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||
const char *mode) {
|
||||
TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||
const char *mode) {
|
||||
struct SParser p;
|
||||
int status;
|
||||
TStatus status;
|
||||
incnny(L); /* cannot yield during parsing */
|
||||
p.z = z; p.name = name; p.mode = mode;
|
||||
p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
|
||||
@ -1014,9 +1162,9 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||
luaZ_initbuffer(L, &p.buff);
|
||||
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc);
|
||||
luaZ_freebuffer(L, &p.buff);
|
||||
luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
|
||||
luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
|
||||
luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
|
||||
luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size));
|
||||
luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size));
|
||||
luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size));
|
||||
decnny(L);
|
||||
return status;
|
||||
}
|
||||
|
||||
49
ldo.h
49
ldo.h
@ -23,10 +23,19 @@
|
||||
** 'condmovestack' is used in heavy tests to force a stack reallocation
|
||||
** at every check.
|
||||
*/
|
||||
|
||||
#if !defined(HARDSTACKTESTS)
|
||||
#define condmovestack(L,pre,pos) ((void)0)
|
||||
#else
|
||||
/* realloc stack keeping its size */
|
||||
#define condmovestack(L,pre,pos) \
|
||||
{ int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
|
||||
#endif
|
||||
|
||||
#define luaD_checkstackaux(L,n,pre,pos) \
|
||||
if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
|
||||
{ pre; luaD_growstack(L, n, 1); pos; } \
|
||||
else { condmovestack(L,pre,pos); }
|
||||
else { condmovestack(L,pre,pos); }
|
||||
|
||||
/* In general, 'pre'/'pos' are empty (nothing to save) */
|
||||
#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
|
||||
@ -44,24 +53,24 @@
|
||||
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
|
||||
|
||||
|
||||
/* macro to check stack size and GC, preserving 'p' */
|
||||
#define checkstackGCp(L,n,p) \
|
||||
luaD_checkstackaux(L, n, \
|
||||
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
|
||||
luaC_checkGC(L), /* stack grow uses memory */ \
|
||||
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
|
||||
|
||||
|
||||
/* macro to check stack size and GC */
|
||||
#define checkstackGC(L,fsize) \
|
||||
luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
|
||||
/*
|
||||
** Maximum depth for nested C calls, syntactical nested non-terminals,
|
||||
** and other features implemented through recursion in C. (Value must
|
||||
** fit in a 16-bit unsigned integer. It must also be compatible with
|
||||
** the size of the C stack.)
|
||||
*/
|
||||
#if !defined(LUAI_MAXCCALLS)
|
||||
#define LUAI_MAXCCALLS 200
|
||||
#endif
|
||||
|
||||
|
||||
/* type of protected functions, to be ran by 'runprotected' */
|
||||
typedef void (*Pfunc) (lua_State *L, void *ud);
|
||||
|
||||
LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
|
||||
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||
LUAI_FUNC l_noret luaD_errerr (lua_State *L);
|
||||
LUAI_FUNC void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop);
|
||||
LUAI_FUNC TStatus luaD_protectedparser (lua_State *L, ZIO *z,
|
||||
const char *name,
|
||||
const char *mode);
|
||||
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
|
||||
int fTransfer, int nTransfer);
|
||||
@ -71,18 +80,20 @@ LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
|
||||
LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
|
||||
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
|
||||
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
||||
LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func);
|
||||
LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status);
|
||||
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
LUAI_FUNC TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level,
|
||||
TStatus status);
|
||||
LUAI_FUNC TStatus luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
ptrdiff_t oldtop, ptrdiff_t ef);
|
||||
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
|
||||
LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
|
||||
LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
|
||||
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
||||
LUAI_FUNC void luaD_inctop (lua_State *L);
|
||||
LUAI_FUNC int luaD_checkminstack (lua_State *L);
|
||||
|
||||
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
|
||||
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||
LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode);
|
||||
LUAI_FUNC l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode);
|
||||
LUAI_FUNC TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
159
ldump.c
159
ldump.c
@ -15,8 +15,11 @@
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lgc.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "ltable.h"
|
||||
#include "lundump.h"
|
||||
|
||||
|
||||
@ -24,8 +27,11 @@ typedef struct {
|
||||
lua_State *L;
|
||||
lua_Writer writer;
|
||||
void *data;
|
||||
size_t offset; /* current position relative to beginning of dump */
|
||||
int strip;
|
||||
int status;
|
||||
Table *h; /* table to track saved strings */
|
||||
lua_Unsigned nstr; /* counter for counting saved strings */
|
||||
} DumpState;
|
||||
|
||||
|
||||
@ -38,15 +44,36 @@ typedef struct {
|
||||
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
|
||||
|
||||
|
||||
/*
|
||||
** Dump the block of memory pointed by 'b' with given 'size'.
|
||||
** 'b' should not be NULL, except for the last call signaling the end
|
||||
** of the dump.
|
||||
*/
|
||||
static void dumpBlock (DumpState *D, const void *b, size_t size) {
|
||||
if (D->status == 0 && size > 0) {
|
||||
if (D->status == 0) { /* do not write anything after an error */
|
||||
lua_unlock(D->L);
|
||||
D->status = (*D->writer)(D->L, b, size, D->data);
|
||||
lua_lock(D->L);
|
||||
D->offset += size;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Dump enough zeros to ensure that current position is a multiple of
|
||||
** 'align'.
|
||||
*/
|
||||
static void dumpAlign (DumpState *D, unsigned align) {
|
||||
unsigned padding = align - cast_uint(D->offset % align);
|
||||
if (padding < align) { /* padding == align means no padding */
|
||||
static lua_Integer paddingContent = 0;
|
||||
lua_assert(align <= sizeof(lua_Integer));
|
||||
dumpBlock(D, &paddingContent, padding);
|
||||
}
|
||||
lua_assert(D->offset % align == 0);
|
||||
}
|
||||
|
||||
|
||||
#define dumpVar(D,x) dumpVector(D,&x,1)
|
||||
|
||||
|
||||
@ -57,25 +84,32 @@ static void dumpByte (DumpState *D, int y) {
|
||||
|
||||
|
||||
/*
|
||||
** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
|
||||
** rounds up the division.)
|
||||
** size for 'dumpVarint' buffer: each byte can store up to 7 bits.
|
||||
** (The "+6" rounds up the division.)
|
||||
*/
|
||||
#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
|
||||
#define DIBS ((l_numbits(lua_Unsigned) + 6) / 7)
|
||||
|
||||
static void dumpSize (DumpState *D, size_t x) {
|
||||
/*
|
||||
** Dumps an unsigned integer using the MSB Varint encoding
|
||||
*/
|
||||
static void dumpVarint (DumpState *D, lua_Unsigned x) {
|
||||
lu_byte buff[DIBS];
|
||||
int n = 0;
|
||||
do {
|
||||
buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */
|
||||
x >>= 7;
|
||||
} while (x != 0);
|
||||
buff[DIBS - 1] |= 0x80; /* mark last byte */
|
||||
unsigned n = 1;
|
||||
buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */
|
||||
while ((x >>= 7) != 0) /* fill other bytes in reverse order */
|
||||
buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80);
|
||||
dumpVector(D, buff + DIBS - n, n);
|
||||
}
|
||||
|
||||
|
||||
static void dumpSize (DumpState *D, size_t sz) {
|
||||
dumpVarint(D, cast(lua_Unsigned, sz));
|
||||
}
|
||||
|
||||
|
||||
static void dumpInt (DumpState *D, int x) {
|
||||
dumpSize(D, x);
|
||||
lua_assert(x >= 0);
|
||||
dumpVarint(D, cast_uint(x));
|
||||
}
|
||||
|
||||
|
||||
@ -84,30 +118,65 @@ static void dumpNumber (DumpState *D, lua_Number x) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Signed integers are coded to keep small values small. (Coding -1 as
|
||||
** 0xfff...fff would use too many bytes to save a quite common value.)
|
||||
** A non-negative x is coded as 2x; a negative x is coded as -2x - 1.
|
||||
** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...)
|
||||
*/
|
||||
static void dumpInteger (DumpState *D, lua_Integer x) {
|
||||
dumpVar(D, x);
|
||||
lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x)
|
||||
: (2u * ~l_castS2U(x)) + 1;
|
||||
dumpVarint(D, cx);
|
||||
}
|
||||
|
||||
|
||||
static void dumpString (DumpState *D, const TString *s) {
|
||||
if (s == NULL)
|
||||
dumpSize(D, 0);
|
||||
/*
|
||||
** Dump a String. First dump its "size":
|
||||
** size==0 is followed by an index and means "reuse saved string with
|
||||
** that index"; index==0 means NULL.
|
||||
** size>=1 is followed by the string contents with real size==size-1 and
|
||||
** means that string, which will be saved with the next available index.
|
||||
** The real size does not include the ending '\0' (which is not dumped),
|
||||
** so adding 1 to it cannot overflow a size_t.
|
||||
*/
|
||||
static void dumpString (DumpState *D, TString *ts) {
|
||||
if (ts == NULL) {
|
||||
dumpVarint(D, 0); /* will "reuse" NULL */
|
||||
dumpVarint(D, 0); /* special index for NULL */
|
||||
}
|
||||
else {
|
||||
size_t size = tsslen(s);
|
||||
const char *str = getstr(s);
|
||||
dumpSize(D, size + 1);
|
||||
dumpVector(D, str, size);
|
||||
TValue idx;
|
||||
int tag = luaH_getstr(D->h, ts, &idx);
|
||||
if (!tagisempty(tag)) { /* string already saved? */
|
||||
dumpVarint(D, 0); /* reuse a saved string */
|
||||
dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */
|
||||
}
|
||||
else { /* must write and save the string */
|
||||
TValue key, value; /* to save the string in the hash */
|
||||
size_t size;
|
||||
const char *s = getlstr(ts, size);
|
||||
dumpSize(D, size + 1);
|
||||
dumpVector(D, s, size + 1); /* include ending '\0' */
|
||||
D->nstr++; /* one more saved string */
|
||||
setsvalue(D->L, &key, ts); /* the string is the key */
|
||||
setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */
|
||||
luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */
|
||||
/* integer value does not need barrier */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void dumpCode (DumpState *D, const Proto *f) {
|
||||
dumpInt(D, f->sizecode);
|
||||
dumpVector(D, f->code, f->sizecode);
|
||||
dumpAlign(D, sizeof(f->code[0]));
|
||||
lua_assert(f->code != NULL);
|
||||
dumpVector(D, f->code, cast_uint(f->sizecode));
|
||||
}
|
||||
|
||||
|
||||
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
|
||||
static void dumpFunction (DumpState *D, const Proto *f);
|
||||
|
||||
static void dumpConstants (DumpState *D, const Proto *f) {
|
||||
int i;
|
||||
@ -140,7 +209,7 @@ static void dumpProtos (DumpState *D, const Proto *f) {
|
||||
int n = f->sizep;
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++)
|
||||
dumpFunction(D, f->p[i], f->source);
|
||||
dumpFunction(D, f->p[i]);
|
||||
}
|
||||
|
||||
|
||||
@ -159,12 +228,14 @@ static void dumpDebug (DumpState *D, const Proto *f) {
|
||||
int i, n;
|
||||
n = (D->strip) ? 0 : f->sizelineinfo;
|
||||
dumpInt(D, n);
|
||||
dumpVector(D, f->lineinfo, n);
|
||||
if (f->lineinfo != NULL)
|
||||
dumpVector(D, f->lineinfo, cast_uint(n));
|
||||
n = (D->strip) ? 0 : f->sizeabslineinfo;
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
dumpInt(D, f->abslineinfo[i].pc);
|
||||
dumpInt(D, f->abslineinfo[i].line);
|
||||
if (n > 0) {
|
||||
/* 'abslineinfo' is an array of structures of int's */
|
||||
dumpAlign(D, sizeof(int));
|
||||
dumpVector(D, f->abslineinfo, cast_uint(n));
|
||||
}
|
||||
n = (D->strip) ? 0 : f->sizelocvars;
|
||||
dumpInt(D, n);
|
||||
@ -180,51 +251,57 @@ static void dumpDebug (DumpState *D, const Proto *f) {
|
||||
}
|
||||
|
||||
|
||||
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
|
||||
if (D->strip || f->source == psource)
|
||||
dumpString(D, NULL); /* no debug info or same source as its parent */
|
||||
else
|
||||
dumpString(D, f->source);
|
||||
static void dumpFunction (DumpState *D, const Proto *f) {
|
||||
dumpInt(D, f->linedefined);
|
||||
dumpInt(D, f->lastlinedefined);
|
||||
dumpByte(D, f->numparams);
|
||||
dumpByte(D, f->is_vararg);
|
||||
dumpByte(D, f->flag);
|
||||
dumpByte(D, f->maxstacksize);
|
||||
dumpCode(D, f);
|
||||
dumpConstants(D, f);
|
||||
dumpUpvalues(D, f);
|
||||
dumpProtos(D, f);
|
||||
dumpString(D, D->strip ? NULL : f->source);
|
||||
dumpDebug(D, f);
|
||||
}
|
||||
|
||||
|
||||
#define dumpNumInfo(D, tvar, value) \
|
||||
{ tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); }
|
||||
|
||||
|
||||
static void dumpHeader (DumpState *D) {
|
||||
dumpLiteral(D, LUA_SIGNATURE);
|
||||
dumpByte(D, LUAC_VERSION);
|
||||
dumpByte(D, LUAC_FORMAT);
|
||||
dumpLiteral(D, LUAC_DATA);
|
||||
dumpByte(D, sizeof(Instruction));
|
||||
dumpByte(D, sizeof(lua_Integer));
|
||||
dumpByte(D, sizeof(lua_Number));
|
||||
dumpInteger(D, LUAC_INT);
|
||||
dumpNumber(D, LUAC_NUM);
|
||||
dumpNumInfo(D, int, LUAC_INT);
|
||||
dumpNumInfo(D, Instruction, LUAC_INST);
|
||||
dumpNumInfo(D, lua_Integer, LUAC_INT);
|
||||
dumpNumInfo(D, lua_Number, LUAC_NUM);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** dump Lua function as precompiled chunk
|
||||
*/
|
||||
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
|
||||
int strip) {
|
||||
int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
|
||||
int strip) {
|
||||
DumpState D;
|
||||
D.h = luaH_new(L); /* aux. table to keep strings already dumped */
|
||||
sethvalue2s(L, L->top.p, D.h); /* anchor it */
|
||||
L->top.p++;
|
||||
D.L = L;
|
||||
D.writer = w;
|
||||
D.offset = 0;
|
||||
D.data = data;
|
||||
D.strip = strip;
|
||||
D.status = 0;
|
||||
D.nstr = 0;
|
||||
dumpHeader(&D);
|
||||
dumpByte(&D, f->sizeupvalues);
|
||||
dumpFunction(&D, f, NULL);
|
||||
dumpFunction(&D, f);
|
||||
dumpBlock(&D, NULL, 0); /* signal end of dump */
|
||||
return D.status;
|
||||
}
|
||||
|
||||
|
||||
82
lfunc.c
82
lfunc.c
@ -100,21 +100,23 @@ UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||
|
||||
|
||||
/*
|
||||
** Call closing method for object 'obj' with error message 'err'. The
|
||||
** Call closing method for object 'obj' with error object 'err'. The
|
||||
** boolean 'yy' controls whether the call is yieldable.
|
||||
** (This function assumes EXTRA_STACK.)
|
||||
*/
|
||||
static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
|
||||
StkId top = L->top.p;
|
||||
StkId func = top;
|
||||
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
|
||||
setobj2s(L, top, tm); /* will call metamethod... */
|
||||
setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
|
||||
setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
|
||||
L->top.p = top + 3; /* add function and arguments */
|
||||
setobj2s(L, top++, tm); /* will call metamethod... */
|
||||
setobj2s(L, top++, obj); /* with 'self' as the 1st argument */
|
||||
if (err != NULL) /* if there was an error... */
|
||||
setobj2s(L, top++, err); /* then error object will be 2nd argument */
|
||||
L->top.p = top; /* add function and arguments */
|
||||
if (yy)
|
||||
luaD_call(L, top, 0);
|
||||
luaD_call(L, func, 0);
|
||||
else
|
||||
luaD_callnoyield(L, top, 0);
|
||||
luaD_callnoyield(L, func, 0);
|
||||
}
|
||||
|
||||
|
||||
@ -140,26 +142,28 @@ static void checkclosemth (lua_State *L, StkId level) {
|
||||
** the 'level' of the upvalue being closed, as everything after that
|
||||
** won't be used again.
|
||||
*/
|
||||
static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
|
||||
static void prepcallclosemth (lua_State *L, StkId level, TStatus status,
|
||||
int yy) {
|
||||
TValue *uv = s2v(level); /* value being closed */
|
||||
TValue *errobj;
|
||||
if (status == CLOSEKTOP)
|
||||
errobj = &G(L)->nilvalue; /* error object is nil */
|
||||
else { /* 'luaD_seterrorobj' will set top to level + 2 */
|
||||
errobj = s2v(level + 1); /* error object goes after 'uv' */
|
||||
luaD_seterrorobj(L, status, level + 1); /* set error object */
|
||||
switch (status) {
|
||||
case LUA_OK:
|
||||
L->top.p = level + 1; /* call will be at this level */
|
||||
/* FALLTHROUGH */
|
||||
case CLOSEKTOP: /* don't need to change top */
|
||||
errobj = NULL; /* no error object */
|
||||
break;
|
||||
default: /* 'luaD_seterrorobj' will set top to level + 2 */
|
||||
errobj = s2v(level + 1); /* error object goes after 'uv' */
|
||||
luaD_seterrorobj(L, status, level + 1); /* set error object */
|
||||
break;
|
||||
}
|
||||
callclosemethod(L, uv, errobj, yy);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Maximum value for deltas in 'tbclist', dependent on the type
|
||||
** of delta. (This macro assumes that an 'L' is in scope where it
|
||||
** is used.)
|
||||
*/
|
||||
#define MAXDELTA \
|
||||
((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1)
|
||||
/* Maximum value for deltas in 'tbclist' */
|
||||
#define MAXDELTA USHRT_MAX
|
||||
|
||||
|
||||
/*
|
||||
@ -192,8 +196,7 @@ void luaF_unlinkupval (UpVal *uv) {
|
||||
*/
|
||||
void luaF_closeupval (lua_State *L, StkId level) {
|
||||
UpVal *uv;
|
||||
StkId upl; /* stack index pointed by 'uv' */
|
||||
while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
|
||||
while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
|
||||
TValue *slot = &uv->u.value; /* new position for value */
|
||||
lua_assert(uplevel(uv) < L->top.p);
|
||||
luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
|
||||
@ -224,7 +227,7 @@ static void poptbclist (lua_State *L) {
|
||||
** Close all upvalues and to-be-closed variables up to the given stack
|
||||
** level. Return restored 'level'.
|
||||
*/
|
||||
StkId luaF_close (lua_State *L, StkId level, int status, int yy) {
|
||||
StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
|
||||
ptrdiff_t levelrel = savestack(L, level);
|
||||
luaF_closeupval(L, level); /* first, close the upvalues */
|
||||
while (L->tbclist.p >= level) { /* traverse tbc's down to that level */
|
||||
@ -253,7 +256,7 @@ Proto *luaF_newproto (lua_State *L) {
|
||||
f->upvalues = NULL;
|
||||
f->sizeupvalues = 0;
|
||||
f->numparams = 0;
|
||||
f->is_vararg = 0;
|
||||
f->flag = 0;
|
||||
f->maxstacksize = 0;
|
||||
f->locvars = NULL;
|
||||
f->sizelocvars = 0;
|
||||
@ -264,14 +267,31 @@ Proto *luaF_newproto (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
lu_mem luaF_protosize (Proto *p) {
|
||||
lu_mem sz = cast(lu_mem, sizeof(Proto))
|
||||
+ cast_uint(p->sizep) * sizeof(Proto*)
|
||||
+ cast_uint(p->sizek) * sizeof(TValue)
|
||||
+ cast_uint(p->sizelocvars) * sizeof(LocVar)
|
||||
+ cast_uint(p->sizeupvalues) * sizeof(Upvaldesc);
|
||||
if (!(p->flag & PF_FIXED)) {
|
||||
sz += cast_uint(p->sizecode) * sizeof(Instruction);
|
||||
sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte);
|
||||
sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo);
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||
luaM_freearray(L, f->code, f->sizecode);
|
||||
luaM_freearray(L, f->p, f->sizep);
|
||||
luaM_freearray(L, f->k, f->sizek);
|
||||
luaM_freearray(L, f->lineinfo, f->sizelineinfo);
|
||||
luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
|
||||
luaM_freearray(L, f->locvars, f->sizelocvars);
|
||||
luaM_freearray(L, f->upvalues, f->sizeupvalues);
|
||||
if (!(f->flag & PF_FIXED)) {
|
||||
luaM_freearray(L, f->code, cast_sizet(f->sizecode));
|
||||
luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo));
|
||||
luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo));
|
||||
}
|
||||
luaM_freearray(L, f->p, cast_sizet(f->sizep));
|
||||
luaM_freearray(L, f->k, cast_sizet(f->sizek));
|
||||
luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars));
|
||||
luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues));
|
||||
luaM_free(L, f);
|
||||
}
|
||||
|
||||
|
||||
13
lfunc.h
13
lfunc.h
@ -11,11 +11,11 @@
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \
|
||||
cast_int(sizeof(TValue)) * (n))
|
||||
#define sizeCclosure(n) \
|
||||
(offsetof(CClosure, upvalue) + sizeof(TValue) * cast_uint(n))
|
||||
|
||||
#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \
|
||||
cast_int(sizeof(TValue *)) * (n))
|
||||
#define sizeLclosure(n) \
|
||||
(offsetof(LClosure, upvals) + sizeof(UpVal *) * cast_uint(n))
|
||||
|
||||
|
||||
/* test whether thread is in 'twups' list */
|
||||
@ -44,7 +44,7 @@
|
||||
|
||||
|
||||
/* special status to close upvalues preserving the top of the stack */
|
||||
#define CLOSEKTOP (-1)
|
||||
#define CLOSEKTOP (LUA_ERRERR + 1)
|
||||
|
||||
|
||||
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
||||
@ -54,8 +54,9 @@ LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
|
||||
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
|
||||
LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
|
||||
LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level);
|
||||
LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy);
|
||||
LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy);
|
||||
LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
|
||||
LUAI_FUNC lu_mem luaF_protosize (Proto *p);
|
||||
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
|
||||
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
|
||||
int pc);
|
||||
|
||||
130
lgc.h
130
lgc.h
@ -8,6 +8,9 @@
|
||||
#define lgc_h
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
@ -20,8 +23,9 @@
|
||||
** never point to a white one. Moreover, any gray object must be in a
|
||||
** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
|
||||
** can be visited again before finishing the collection cycle. (Open
|
||||
** upvalues are an exception to this rule.) These lists have no meaning
|
||||
** when the invariant is not being enforced (e.g., sweep phase).
|
||||
** upvalues are an exception to this rule, as they are attached to
|
||||
** a corresponding thread.) These lists have no meaning when the
|
||||
** invariant is not being enforced (e.g., sweep phase).
|
||||
*/
|
||||
|
||||
|
||||
@ -45,10 +49,10 @@
|
||||
|
||||
/*
|
||||
** macro to tell when main invariant (white objects cannot point to black
|
||||
** ones) must be kept. During a collection, the sweep
|
||||
** phase may break the invariant, as objects turned white may point to
|
||||
** still-black objects. The invariant is restored when sweep ends and
|
||||
** all objects are white again.
|
||||
** ones) must be kept. During a collection, the sweep phase may break
|
||||
** the invariant, as objects turned white may point to still-black
|
||||
** objects. The invariant is restored when sweep ends and all objects
|
||||
** are white again.
|
||||
*/
|
||||
|
||||
#define keepinvariant(g) ((g)->gcstate <= GCSatomic)
|
||||
@ -117,36 +121,90 @@
|
||||
#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
|
||||
#define isold(o) (getage(o) > G_SURVIVAL)
|
||||
|
||||
#define changeage(o,f,t) \
|
||||
check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t)))
|
||||
|
||||
/*
|
||||
** In generational mode, objects are created 'new'. After surviving one
|
||||
** cycle, they become 'survival'. Both 'new' and 'survival' can point
|
||||
** to any other object, as they are traversed at the end of the cycle.
|
||||
** We call them both 'young' objects.
|
||||
** If a survival object survives another cycle, it becomes 'old1'.
|
||||
** 'old1' objects can still point to survival objects (but not to
|
||||
** new objects), so they still must be traversed. After another cycle
|
||||
** (that, being old, 'old1' objects will "survive" no matter what)
|
||||
** finally the 'old1' object becomes really 'old', and then they
|
||||
** are no more traversed.
|
||||
**
|
||||
** To keep its invariants, the generational mode uses the same barriers
|
||||
** also used by the incremental mode. If a young object is caught in a
|
||||
** forward barrier, it cannot become old immediately, because it can
|
||||
** still point to other young objects. Instead, it becomes 'old0',
|
||||
** which in the next cycle becomes 'old1'. So, 'old0' objects is
|
||||
** old but can point to new and survival objects; 'old1' is old
|
||||
** but cannot point to new objects; and 'old' cannot point to any
|
||||
** young object.
|
||||
**
|
||||
** If any old object ('old0', 'old1', 'old') is caught in a back
|
||||
** barrier, it becomes 'touched1' and goes into a gray list, to be
|
||||
** visited at the end of the cycle. There it evolves to 'touched2',
|
||||
** which can point to survivals but not to new objects. In yet another
|
||||
** cycle then it becomes 'old' again.
|
||||
**
|
||||
** The generational mode must also control the colors of objects,
|
||||
** because of the barriers. While the mutator is running, young objects
|
||||
** are kept white. 'old', 'old1', and 'touched2' objects are kept black,
|
||||
** as they cannot point to new objects; exceptions are threads and open
|
||||
** upvalues, which age to 'old1' and 'old' but are kept gray. 'old0'
|
||||
** objects may be gray or black, as in the incremental mode. 'touched1'
|
||||
** objects are kept gray, as they must be visited again at the end of
|
||||
** the cycle.
|
||||
*/
|
||||
|
||||
|
||||
/* Default Values for GC parameters */
|
||||
#define LUAI_GENMAJORMUL 100
|
||||
/*
|
||||
** {======================================================
|
||||
** Default Values for GC parameters
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
** Minor collections will shift to major ones after LUAI_MINORMAJOR%
|
||||
** bytes become old.
|
||||
*/
|
||||
#define LUAI_MINORMAJOR 70
|
||||
|
||||
/*
|
||||
** Major collections will shift to minor ones after a collection
|
||||
** collects at least LUAI_MAJORMINOR% of the new bytes.
|
||||
*/
|
||||
#define LUAI_MAJORMINOR 50
|
||||
|
||||
/*
|
||||
** A young (minor) collection will run after creating LUAI_GENMINORMUL%
|
||||
** new bytes.
|
||||
*/
|
||||
#define LUAI_GENMINORMUL 20
|
||||
|
||||
/* wait memory to double before starting new cycle */
|
||||
#define LUAI_GCPAUSE 200
|
||||
|
||||
/* incremental */
|
||||
|
||||
/* Number of bytes must be LUAI_GCPAUSE% before starting new cycle */
|
||||
#define LUAI_GCPAUSE 250
|
||||
|
||||
/*
|
||||
** some gc parameters are stored divided by 4 to allow a maximum value
|
||||
** up to 1023 in a 'lu_byte'.
|
||||
** Step multiplier: The collector handles LUAI_GCMUL% work units for
|
||||
** each new allocated word. (Each "work unit" corresponds roughly to
|
||||
** sweeping one object or traversing one slot.)
|
||||
*/
|
||||
#define getgcparam(p) ((p) * 4)
|
||||
#define setgcparam(p,v) ((p) = (v) / 4)
|
||||
#define LUAI_GCMUL 200
|
||||
|
||||
#define LUAI_GCMUL 100
|
||||
|
||||
/* how much to allocate before next GC step (log2) */
|
||||
#define LUAI_GCSTEPSIZE 13 /* 8 KB */
|
||||
/* How many bytes to allocate before next GC step */
|
||||
#define LUAI_GCSTEPSIZE (200 * sizeof(Table))
|
||||
|
||||
|
||||
/*
|
||||
** Check whether the declared GC mode is generational. While in
|
||||
** generational mode, the collector can go temporarily to incremental
|
||||
** mode to improve performance. This is signaled by 'g->lastatomic != 0'.
|
||||
*/
|
||||
#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
|
||||
#define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v))
|
||||
#define applygcparam(g,p,x) luaO_applyparam(g->gcparams[LUA_GCP##p], x)
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
@ -159,14 +217,22 @@
|
||||
|
||||
|
||||
/*
|
||||
** Does one step of collection when debt becomes positive. 'pre'/'pos'
|
||||
** Does one step of collection when debt becomes zero. 'pre'/'pos'
|
||||
** allows some adjustments to be done only when needed. macro
|
||||
** 'condchangemem' is used only for heavy tests (forcing a full
|
||||
** GC cycle on every opportunity)
|
||||
*/
|
||||
|
||||
#if !defined(HARDMEMTESTS)
|
||||
#define condchangemem(L,pre,pos,emg) ((void)0)
|
||||
#else
|
||||
#define condchangemem(L,pre,pos,emg) \
|
||||
{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, emg); pos; } }
|
||||
#endif
|
||||
|
||||
#define luaC_condGC(L,pre,pos) \
|
||||
{ if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
|
||||
condchangemem(L,pre,pos); }
|
||||
{ if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \
|
||||
condchangemem(L,pre,pos,0); }
|
||||
|
||||
/* more often than not, 'pre'/'pos' are empty */
|
||||
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
|
||||
@ -188,10 +254,10 @@
|
||||
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
|
||||
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
||||
LUAI_FUNC void luaC_step (lua_State *L);
|
||||
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
|
||||
LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast);
|
||||
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
|
||||
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
|
||||
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
|
||||
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz);
|
||||
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz,
|
||||
size_t offset);
|
||||
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
||||
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
|
||||
|
||||
50
linit.c
50
linit.c
@ -8,21 +8,6 @@
|
||||
#define linit_c
|
||||
#define LUA_LIB
|
||||
|
||||
/*
|
||||
** If you embed Lua in your program and need to open the standard
|
||||
** libraries, call luaL_openlibs in your program. If you need a
|
||||
** different set of libraries, copy this file to your project and edit
|
||||
** it to suit your needs.
|
||||
**
|
||||
** You can also *preload* libraries, so that a later 'require' can
|
||||
** open the library, which is already linked to the application.
|
||||
** For that, do the following code:
|
||||
**
|
||||
** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||
** lua_pushcfunction(L, luaopen_modname);
|
||||
** lua_setfield(L, -2, modname);
|
||||
** lua_pop(L, 1); // remove PRELOAD table
|
||||
*/
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
@ -33,33 +18,46 @@
|
||||
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
** these libs are loaded by lua.c and are readily available to any Lua
|
||||
** program
|
||||
** Standard Libraries. (Must be listed in the same ORDER of their
|
||||
** respective constants LUA_<libname>K.)
|
||||
*/
|
||||
static const luaL_Reg loadedlibs[] = {
|
||||
static const luaL_Reg stdlibs[] = {
|
||||
{LUA_GNAME, luaopen_base},
|
||||
{LUA_LOADLIBNAME, luaopen_package},
|
||||
{LUA_COLIBNAME, luaopen_coroutine},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
{LUA_IOLIBNAME, luaopen_io},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_OSLIBNAME, luaopen_os},
|
||||
{LUA_STRLIBNAME, luaopen_string},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
{LUA_UTF8LIBNAME, luaopen_utf8},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||
/*
|
||||
** require and preload selected standard libraries
|
||||
*/
|
||||
LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
|
||||
int mask;
|
||||
const luaL_Reg *lib;
|
||||
/* "require" functions from 'loadedlibs' and set results to global table */
|
||||
for (lib = loadedlibs; lib->func; lib++) {
|
||||
luaL_requiref(L, lib->name, lib->func, 1);
|
||||
lua_pop(L, 1); /* remove lib */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||
for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
|
||||
if (load & mask) { /* selected? */
|
||||
luaL_requiref(L, lib->name, lib->func, 1); /* require library */
|
||||
lua_pop(L, 1); /* remove result from the stack */
|
||||
}
|
||||
else if (preload & mask) { /* selected? */
|
||||
lua_pushcfunction(L, lib->func);
|
||||
lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
|
||||
}
|
||||
}
|
||||
lua_assert((mask >> 1) == LUA_UTF8LIBK);
|
||||
lua_pop(L, 1); /* remove PRELOAD table */
|
||||
}
|
||||
|
||||
|
||||
77
liolib.c
77
liolib.c
@ -21,8 +21,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -115,7 +114,7 @@ static int l_checkmode (const char *mode) {
|
||||
|
||||
#if !defined(l_fseek) /* { */
|
||||
|
||||
#if defined(LUA_USE_POSIX) /* { */
|
||||
#if defined(LUA_USE_POSIX) || defined(LUA_USE_OFF_T) /* { */
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
@ -245,8 +244,8 @@ static int f_gc (lua_State *L) {
|
||||
*/
|
||||
static int io_fclose (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
int res = fclose(p->f);
|
||||
return luaL_fileresult(L, (res == 0), NULL);
|
||||
errno = 0;
|
||||
return luaL_fileresult(L, (fclose(p->f) == 0), NULL);
|
||||
}
|
||||
|
||||
|
||||
@ -272,6 +271,7 @@ static int io_open (lua_State *L) {
|
||||
LStream *p = newfile(L);
|
||||
const char *md = mode; /* to traverse/check mode */
|
||||
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
|
||||
errno = 0;
|
||||
p->f = fopen(filename, mode);
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||
}
|
||||
@ -292,6 +292,7 @@ static int io_popen (lua_State *L) {
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
LStream *p = newprefile(L);
|
||||
luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode");
|
||||
errno = 0;
|
||||
p->f = l_popen(L, filename, mode);
|
||||
p->closef = &io_pclose;
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||
@ -300,6 +301,7 @@ static int io_popen (lua_State *L) {
|
||||
|
||||
static int io_tmpfile (lua_State *L) {
|
||||
LStream *p = newfile(L);
|
||||
errno = 0;
|
||||
p->f = tmpfile();
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
||||
}
|
||||
@ -441,7 +443,7 @@ static int nextc (RN *rn) {
|
||||
return 0; /* fail */
|
||||
}
|
||||
else {
|
||||
rn->buff[rn->n++] = rn->c; /* save current char */
|
||||
rn->buff[rn->n++] = cast_char(rn->c); /* save current char */
|
||||
rn->c = l_getc(rn->f); /* read next one */
|
||||
return 1;
|
||||
}
|
||||
@ -522,15 +524,15 @@ static int read_line (lua_State *L, FILE *f, int chop) {
|
||||
luaL_buffinit(L, &b);
|
||||
do { /* may need to read several chunks to get whole line */
|
||||
char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */
|
||||
int i = 0;
|
||||
unsigned i = 0;
|
||||
l_lockfile(f); /* no memory errors can happen inside the lock */
|
||||
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
|
||||
buff[i++] = c; /* read up to end of line or buffer limit */
|
||||
buff[i++] = cast_char(c); /* read up to end of line or buffer limit */
|
||||
l_unlockfile(f);
|
||||
luaL_addsize(&b, i);
|
||||
} while (c != EOF && c != '\n'); /* repeat until end of line */
|
||||
if (!chop && c == '\n') /* want a newline and have one? */
|
||||
luaL_addchar(&b, c); /* add ending newline to result */
|
||||
luaL_addchar(&b, '\n'); /* add ending newline to result */
|
||||
luaL_pushresult(&b); /* close buffer */
|
||||
/* return ok if read something (either a newline or something else) */
|
||||
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
||||
@ -567,6 +569,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
||||
int nargs = lua_gettop(L) - 1;
|
||||
int n, success;
|
||||
clearerr(f);
|
||||
errno = 0;
|
||||
if (nargs == 0) { /* no arguments? */
|
||||
success = read_line(L, f, 1);
|
||||
n = first + 1; /* to return 1 result */
|
||||
@ -659,26 +662,28 @@ static int io_readline (lua_State *L) {
|
||||
|
||||
static int g_write (lua_State *L, FILE *f, int arg) {
|
||||
int nargs = lua_gettop(L) - arg;
|
||||
int status = 1;
|
||||
for (; nargs--; arg++) {
|
||||
if (lua_type(L, arg) == LUA_TNUMBER) {
|
||||
/* optimization: could be done exactly as for strings */
|
||||
int len = lua_isinteger(L, arg)
|
||||
? fprintf(f, LUA_INTEGER_FMT,
|
||||
(LUAI_UACINT)lua_tointeger(L, arg))
|
||||
: fprintf(f, LUA_NUMBER_FMT,
|
||||
(LUAI_UACNUMBER)lua_tonumber(L, arg));
|
||||
status = status && (len > 0);
|
||||
size_t totalbytes = 0; /* total number of bytes written */
|
||||
errno = 0;
|
||||
for (; nargs--; arg++) { /* for each argument */
|
||||
char buff[LUA_N2SBUFFSZ];
|
||||
const char *s;
|
||||
size_t numbytes; /* bytes written in one call to 'fwrite' */
|
||||
size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */
|
||||
if (len > 0) { /* did conversion work (value was a number)? */
|
||||
s = buff;
|
||||
len--;
|
||||
}
|
||||
else {
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, arg, &l);
|
||||
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||
else /* must be a string */
|
||||
s = luaL_checklstring(L, arg, &len);
|
||||
numbytes = fwrite(s, sizeof(char), len, f);
|
||||
totalbytes += numbytes;
|
||||
if (numbytes < len) { /* write error? */
|
||||
int n = luaL_fileresult(L, 0, NULL);
|
||||
lua_pushinteger(L, cast_st2S(totalbytes));
|
||||
return n + 1; /* return fail, error msg., error code, and counter */
|
||||
}
|
||||
}
|
||||
if (l_likely(status))
|
||||
return 1; /* file handle already on stack top */
|
||||
else return luaL_fileresult(L, status, NULL);
|
||||
return 1; /* no errors; file handle already on stack top */
|
||||
}
|
||||
|
||||
|
||||
@ -703,6 +708,7 @@ static int f_seek (lua_State *L) {
|
||||
l_seeknum offset = (l_seeknum)p3;
|
||||
luaL_argcheck(L, (lua_Integer)offset == p3, 3,
|
||||
"not an integer in proper range");
|
||||
errno = 0;
|
||||
op = l_fseek(f, offset, mode[op]);
|
||||
if (l_unlikely(op))
|
||||
return luaL_fileresult(L, 0, NULL); /* error */
|
||||
@ -719,19 +725,26 @@ static int f_setvbuf (lua_State *L) {
|
||||
FILE *f = tofile(L);
|
||||
int op = luaL_checkoption(L, 2, NULL, modenames);
|
||||
lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
|
||||
int res = setvbuf(f, NULL, mode[op], (size_t)sz);
|
||||
int res;
|
||||
errno = 0;
|
||||
res = setvbuf(f, NULL, mode[op], (size_t)sz);
|
||||
return luaL_fileresult(L, res == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int io_flush (lua_State *L) {
|
||||
return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
||||
static int aux_flush (lua_State *L, FILE *f) {
|
||||
errno = 0;
|
||||
return luaL_fileresult(L, fflush(f) == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
static int f_flush (lua_State *L) {
|
||||
return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
|
||||
return aux_flush(L, tofile(L));
|
||||
}
|
||||
|
||||
|
||||
static int io_flush (lua_State *L) {
|
||||
return aux_flush(L, getiofile(L, IO_OUTPUT));
|
||||
}
|
||||
|
||||
|
||||
@ -773,7 +786,7 @@ static const luaL_Reg meth[] = {
|
||||
** metamethods for file handles
|
||||
*/
|
||||
static const luaL_Reg metameth[] = {
|
||||
{"__index", NULL}, /* place holder */
|
||||
{"__index", NULL}, /* placeholder */
|
||||
{"__gc", f_gc},
|
||||
{"__close", f_gc},
|
||||
{"__tostring", f_tostring},
|
||||
|
||||
@ -21,7 +21,7 @@ static const void *const disptab[NUM_OPCODES] = {
|
||||
#if 0
|
||||
** you can update the following list with this command:
|
||||
**
|
||||
** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h
|
||||
** sed -n '/^OP_/!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h
|
||||
**
|
||||
#endif
|
||||
|
||||
@ -57,8 +57,8 @@ static const void *const disptab[NUM_OPCODES] = {
|
||||
&&L_OP_BANDK,
|
||||
&&L_OP_BORK,
|
||||
&&L_OP_BXORK,
|
||||
&&L_OP_SHRI,
|
||||
&&L_OP_SHLI,
|
||||
&&L_OP_SHRI,
|
||||
&&L_OP_ADD,
|
||||
&&L_OP_SUB,
|
||||
&&L_OP_MUL,
|
||||
@ -106,6 +106,8 @@ static const void *const disptab[NUM_OPCODES] = {
|
||||
&&L_OP_SETLIST,
|
||||
&&L_OP_CLOSURE,
|
||||
&&L_OP_VARARG,
|
||||
&&L_OP_GETVARG,
|
||||
&&L_OP_ERRNNIL,
|
||||
&&L_OP_VARARGPREP,
|
||||
&&L_OP_EXTRAARG
|
||||
|
||||
|
||||
85
llex.c
85
llex.c
@ -32,6 +32,11 @@
|
||||
#define next(ls) (ls->current = zgetc(ls->z))
|
||||
|
||||
|
||||
/* minimum size for string buffer */
|
||||
#if !defined(LUA_MINBUFFER)
|
||||
#define LUA_MINBUFFER 32
|
||||
#endif
|
||||
|
||||
|
||||
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
|
||||
|
||||
@ -39,7 +44,7 @@
|
||||
/* ORDER RESERVED */
|
||||
static const char *const luaX_tokens [] = {
|
||||
"and", "break", "do", "else", "elseif",
|
||||
"end", "false", "for", "function", "goto", "if",
|
||||
"end", "false", "for", "function", "global", "goto", "if",
|
||||
"in", "local", "nil", "not", "or", "repeat",
|
||||
"return", "then", "true", "until", "while",
|
||||
"//", "..", "...", "==", ">=", "<=", "~=",
|
||||
@ -57,10 +62,10 @@ static l_noret lexerror (LexState *ls, const char *msg, int token);
|
||||
static void save (LexState *ls, int c) {
|
||||
Mbuffer *b = ls->buff;
|
||||
if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
|
||||
size_t newsize;
|
||||
if (luaZ_sizebuffer(b) >= MAX_SIZE/2)
|
||||
size_t newsize = luaZ_sizebuffer(b); /* get old size */;
|
||||
if (newsize >= (MAX_SIZE/3 * 2)) /* larger than MAX_SIZE/1.5 ? */
|
||||
lexerror(ls, "lexical element too long", 0);
|
||||
newsize = luaZ_sizebuffer(b) * 2;
|
||||
newsize += (newsize >> 1); /* new size is 1.5 times the old one */
|
||||
luaZ_resizebuffer(ls->L, b, newsize);
|
||||
}
|
||||
b->buffer[luaZ_bufflen(b)++] = cast_char(c);
|
||||
@ -122,30 +127,34 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
||||
|
||||
|
||||
/*
|
||||
** Creates a new string and anchors it in scanner's table so that it
|
||||
** will not be collected until the end of the compilation; by that time
|
||||
** it should be anchored somewhere. It also internalizes long strings,
|
||||
** ensuring there is only one copy of each unique string. The table
|
||||
** here is used as a set: the string enters as the key, while its value
|
||||
** is irrelevant. We use the string itself as the value only because it
|
||||
** is a TValue readily available. Later, the code generation can change
|
||||
** this value.
|
||||
** Anchors a string in scanner's table so that it will not be collected
|
||||
** until the end of the compilation; by that time it should be anchored
|
||||
** somewhere. It also internalizes long strings, ensuring there is only
|
||||
** one copy of each unique string.
|
||||
*/
|
||||
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||
static TString *anchorstr (LexState *ls, TString *ts) {
|
||||
lua_State *L = ls->L;
|
||||
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
||||
const TValue *o = luaH_getstr(ls->h, ts);
|
||||
if (!ttisnil(o)) /* string already present? */
|
||||
ts = keystrval(nodefromval(o)); /* get saved copy */
|
||||
else { /* not in use yet */
|
||||
TValue oldts;
|
||||
int tag = luaH_getstr(ls->h, ts, &oldts);
|
||||
if (!tagisempty(tag)) /* string already present? */
|
||||
return tsvalue(&oldts); /* use stored value */
|
||||
else { /* create a new entry */
|
||||
TValue *stv = s2v(L->top.p++); /* reserve stack space for string */
|
||||
setsvalue(L, stv, ts); /* temporarily anchor the string */
|
||||
luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */
|
||||
setsvalue(L, stv, ts); /* push (anchor) the string on the stack */
|
||||
luaH_set(L, ls->h, stv, stv); /* t[string] = string */
|
||||
/* table is not a metatable, so it does not need to invalidate cache */
|
||||
luaC_checkGC(L);
|
||||
L->top.p--; /* remove string from stack */
|
||||
return ts;
|
||||
}
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Creates a new string and anchors it in scanner's table.
|
||||
*/
|
||||
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||
return anchorstr(ls, luaS_newlstr(ls->L, str, l));
|
||||
}
|
||||
|
||||
|
||||
@ -159,7 +168,7 @@ static void inclinenumber (LexState *ls) {
|
||||
next(ls); /* skip '\n' or '\r' */
|
||||
if (currIsNewline(ls) && ls->current != old)
|
||||
next(ls); /* skip '\n\r' or '\r\n' */
|
||||
if (++ls->linenumber >= MAX_INT)
|
||||
if (++ls->linenumber >= INT_MAX)
|
||||
lexerror(ls, "chunk has too many lines", 0);
|
||||
}
|
||||
|
||||
@ -175,7 +184,15 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
||||
ls->linenumber = 1;
|
||||
ls->lastline = 1;
|
||||
ls->source = source;
|
||||
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */
|
||||
/* all three strings here ("_ENV", "break", "global") were fixed,
|
||||
so they cannot be collected */
|
||||
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */
|
||||
ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */
|
||||
#if defined(LUA_COMPAT_GLOBAL)
|
||||
/* compatibility mode: "global" is not a reserved word */
|
||||
ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */
|
||||
ls->glbn->extra = 0; /* mark it as not reserved */
|
||||
#endif
|
||||
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
||||
}
|
||||
|
||||
@ -340,12 +357,17 @@ static int readhexaesc (LexState *ls) {
|
||||
}
|
||||
|
||||
|
||||
static unsigned long readutf8esc (LexState *ls) {
|
||||
unsigned long r;
|
||||
int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */
|
||||
/*
|
||||
** When reading a UTF-8 escape sequence, save everything to the buffer
|
||||
** for error reporting in case of errors; 'i' counts the number of
|
||||
** saved characters, so that they can be removed if case of success.
|
||||
*/
|
||||
static l_uint32 readutf8esc (LexState *ls) {
|
||||
l_uint32 r;
|
||||
int i = 4; /* number of chars to be removed: start with #"\u{X" */
|
||||
save_and_next(ls); /* skip 'u' */
|
||||
esccheck(ls, ls->current == '{', "missing '{'");
|
||||
r = gethexa(ls); /* must have at least one digit */
|
||||
r = cast_uint(gethexa(ls)); /* must have at least one digit */
|
||||
while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) {
|
||||
i++;
|
||||
esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large");
|
||||
@ -542,12 +564,13 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
||||
do {
|
||||
save_and_next(ls);
|
||||
} while (lislalnum(ls->current));
|
||||
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
|
||||
luaZ_bufflen(ls->buff));
|
||||
seminfo->ts = ts;
|
||||
if (isreserved(ts)) /* reserved word? */
|
||||
/* find or create string */
|
||||
ts = luaS_newlstr(ls->L, luaZ_buffer(ls->buff),
|
||||
luaZ_bufflen(ls->buff));
|
||||
if (isreserved(ts)) /* reserved word? */
|
||||
return ts->extra - 1 + FIRST_RESERVED;
|
||||
else {
|
||||
seminfo->ts = anchorstr(ls, ts);
|
||||
return TK_NAME;
|
||||
}
|
||||
}
|
||||
|
||||
8
llex.h
8
llex.h
@ -33,8 +33,8 @@ enum RESERVED {
|
||||
/* terminal symbols denoted by reserved words */
|
||||
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
|
||||
TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
|
||||
TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
|
||||
TK_GLOBAL, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR,
|
||||
TK_REPEAT, TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
|
||||
/* other terminal symbols */
|
||||
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
|
||||
TK_SHL, TK_SHR,
|
||||
@ -59,7 +59,7 @@ typedef struct Token {
|
||||
} Token;
|
||||
|
||||
|
||||
/* state of the lexer plus state of the parser when shared by all
|
||||
/* state of the scanner plus state of the parser when shared by all
|
||||
functions */
|
||||
typedef struct LexState {
|
||||
int current; /* current character (charint) */
|
||||
@ -75,6 +75,8 @@ typedef struct LexState {
|
||||
struct Dyndata *dyd; /* dynamic structures used by the parser */
|
||||
TString *source; /* current source name */
|
||||
TString *envn; /* environment variable name */
|
||||
TString *brkn; /* "break" name (used as a label) */
|
||||
TString *glbn; /* "global" name (when not a reserved word) */
|
||||
} LexState;
|
||||
|
||||
|
||||
|
||||
279
llimits.h
279
llimits.h
@ -15,50 +15,49 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define l_numbits(t) cast_int(sizeof(t) * CHAR_BIT)
|
||||
|
||||
/*
|
||||
** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
|
||||
** the total memory used by Lua (in bytes). Usually, 'size_t' and
|
||||
** 'l_mem' is a signed integer big enough to count the total memory
|
||||
** used by Lua. (It is signed due to the use of debt in several
|
||||
** computations.) 'lu_mem' is a corresponding unsigned type. Usually,
|
||||
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
|
||||
*/
|
||||
#if defined(LUAI_MEM) /* { external definitions? */
|
||||
typedef LUAI_UMEM lu_mem;
|
||||
typedef LUAI_MEM l_mem;
|
||||
typedef LUAI_UMEM lu_mem;
|
||||
#elif LUAI_IS32INT /* }{ */
|
||||
typedef size_t lu_mem;
|
||||
typedef ptrdiff_t l_mem;
|
||||
typedef size_t lu_mem;
|
||||
#else /* 16-bit ints */ /* }{ */
|
||||
typedef unsigned long lu_mem;
|
||||
typedef long l_mem;
|
||||
typedef unsigned long lu_mem;
|
||||
#endif /* } */
|
||||
|
||||
#define MAX_LMEM \
|
||||
cast(l_mem, (cast(lu_mem, 1) << (l_numbits(l_mem) - 1)) - 1)
|
||||
|
||||
|
||||
/* chars used as small naturals (so that 'char' is reserved for characters) */
|
||||
typedef unsigned char lu_byte;
|
||||
typedef signed char ls_byte;
|
||||
|
||||
|
||||
/* Type for thread status/error codes */
|
||||
typedef lu_byte TStatus;
|
||||
|
||||
/* The C API still uses 'int' for status/error codes */
|
||||
#define APIstatus(st) cast_int(st)
|
||||
|
||||
/* maximum value for size_t */
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0))
|
||||
|
||||
/* maximum size visible for Lua (must be representable in a lua_Integer) */
|
||||
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
|
||||
: (size_t)(LUA_MAXINTEGER))
|
||||
|
||||
|
||||
#define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
|
||||
|
||||
#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
|
||||
|
||||
|
||||
#define MAX_INT INT_MAX /* maximum value of an int */
|
||||
|
||||
|
||||
/*
|
||||
** floor of the log2 of the maximum signed value for integral type 't'.
|
||||
** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
|
||||
** Maximum size for strings and userdata visible for Lua; should be
|
||||
** representable as a lua_Integer and as a size_t.
|
||||
*/
|
||||
#define log2maxs(t) (sizeof(t) * 8 - 2)
|
||||
|
||||
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
|
||||
: cast_sizet(LUA_MAXINTEGER))
|
||||
|
||||
/*
|
||||
** test whether an unsigned value is a power of 2 (or zero)
|
||||
@ -88,7 +87,7 @@ typedef signed char ls_byte;
|
||||
#define L_P2I size_t
|
||||
#endif
|
||||
|
||||
#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX))
|
||||
#define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX)
|
||||
|
||||
|
||||
|
||||
@ -104,26 +103,18 @@ typedef LUAI_UACINT l_uacInt;
|
||||
#undef NDEBUG
|
||||
#include <assert.h>
|
||||
#define lua_assert(c) assert(c)
|
||||
#define assert_code(c) c
|
||||
#endif
|
||||
|
||||
#if defined(lua_assert)
|
||||
#define check_exp(c,e) (lua_assert(c), (e))
|
||||
/* to avoid problems with conditions too long */
|
||||
#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
|
||||
#else
|
||||
#define lua_assert(c) ((void)0)
|
||||
#define check_exp(c,e) (e)
|
||||
#define lua_longassert(c) ((void)0)
|
||||
#define assert_code(c) ((void)0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** assertion for checking API calls
|
||||
*/
|
||||
#if !defined(luai_apicheck)
|
||||
#define luai_apicheck(l,e) ((void)l, lua_assert(e))
|
||||
#endif
|
||||
|
||||
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||
#define check_exp(c,e) (lua_assert(c), (e))
|
||||
/* to avoid problems with conditions too long */
|
||||
#define lua_longassert(c) assert_code((c) ? (void)0 : lua_assert(0))
|
||||
|
||||
|
||||
/* macro to avoid warnings about unused variables */
|
||||
@ -139,12 +130,15 @@ typedef LUAI_UACINT l_uacInt;
|
||||
#define cast_voidp(i) cast(void *, (i))
|
||||
#define cast_num(i) cast(lua_Number, (i))
|
||||
#define cast_int(i) cast(int, (i))
|
||||
#define cast_short(i) cast(short, (i))
|
||||
#define cast_uint(i) cast(unsigned int, (i))
|
||||
#define cast_byte(i) cast(lu_byte, (i))
|
||||
#define cast_uchar(i) cast(unsigned char, (i))
|
||||
#define cast_char(i) cast(char, (i))
|
||||
#define cast_charp(i) cast(char *, (i))
|
||||
#define cast_sizet(i) cast(size_t, (i))
|
||||
#define cast_Integer(i) cast(lua_Integer, (i))
|
||||
#define cast_Inst(i) cast(Instruction, (i))
|
||||
|
||||
|
||||
/* cast a signed lua_Integer to lua_Unsigned */
|
||||
@ -161,6 +155,38 @@ typedef LUAI_UACINT l_uacInt;
|
||||
#define l_castU2S(i) ((lua_Integer)(i))
|
||||
#endif
|
||||
|
||||
/*
|
||||
** cast a size_t to lua_Integer: These casts are always valid for
|
||||
** sizes of Lua objects (see MAX_SIZE)
|
||||
*/
|
||||
#define cast_st2S(sz) ((lua_Integer)(sz))
|
||||
|
||||
/* Cast a ptrdiff_t to size_t, when it is known that the minuend
|
||||
** comes from the subtrahend (the base)
|
||||
*/
|
||||
#define ct_diff2sz(df) ((size_t)(df))
|
||||
|
||||
/* ptrdiff_t to lua_Integer */
|
||||
#define ct_diff2S(df) cast_st2S(ct_diff2sz(df))
|
||||
|
||||
/*
|
||||
** Special type equivalent to '(void*)' for functions (to suppress some
|
||||
** warnings when converting function pointers)
|
||||
*/
|
||||
typedef void (*voidf)(void);
|
||||
|
||||
/*
|
||||
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
||||
** is undefined according to ISO C, but POSIX assumes that it works.
|
||||
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
||||
*/
|
||||
#if defined(__GNUC__)
|
||||
#define cast_func(p) (__extension__ (voidf)(p))
|
||||
#else
|
||||
#define cast_func(p) ((voidf)(p))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** non-return type
|
||||
@ -193,8 +219,7 @@ typedef LUAI_UACINT l_uacInt;
|
||||
|
||||
|
||||
/*
|
||||
** type for virtual-machine instructions;
|
||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||
** An unsigned with (at least) 4 bytes
|
||||
*/
|
||||
#if LUAI_IS32INT
|
||||
typedef unsigned int l_uint32;
|
||||
@ -202,107 +227,6 @@ typedef unsigned int l_uint32;
|
||||
typedef unsigned long l_uint32;
|
||||
#endif
|
||||
|
||||
typedef l_uint32 Instruction;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Maximum length for short strings, that is, strings that are
|
||||
** internalized. (Cannot be smaller than reserved words or tags for
|
||||
** metamethods, as these strings must be internalized;
|
||||
** #("function") = 8, #("__newindex") = 10.)
|
||||
*/
|
||||
#if !defined(LUAI_MAXSHORTLEN)
|
||||
#define LUAI_MAXSHORTLEN 40
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Initial size for the string table (must be power of 2).
|
||||
** The Lua core alone registers ~50 strings (reserved words +
|
||||
** metaevent keys + a few others). Libraries would typically add
|
||||
** a few dozens more.
|
||||
*/
|
||||
#if !defined(MINSTRTABSIZE)
|
||||
#define MINSTRTABSIZE 128
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Size of cache for strings in the API. 'N' is the number of
|
||||
** sets (better be a prime) and "M" is the size of each set (M == 1
|
||||
** makes a direct cache.)
|
||||
*/
|
||||
#if !defined(STRCACHE_N)
|
||||
#define STRCACHE_N 53
|
||||
#define STRCACHE_M 2
|
||||
#endif
|
||||
|
||||
|
||||
/* minimum size for string buffer */
|
||||
#if !defined(LUA_MINBUFFER)
|
||||
#define LUA_MINBUFFER 32
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Maximum depth for nested C calls, syntactical nested non-terminals,
|
||||
** and other features implemented through recursion in C. (Value must
|
||||
** fit in a 16-bit unsigned integer. It must also be compatible with
|
||||
** the size of the C stack.)
|
||||
*/
|
||||
#if !defined(LUAI_MAXCCALLS)
|
||||
#define LUAI_MAXCCALLS 200
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** macros that are executed whenever program enters the Lua core
|
||||
** ('lua_lock') and leaves the core ('lua_unlock')
|
||||
*/
|
||||
#if !defined(lua_lock)
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** macro executed during Lua functions at points where the
|
||||
** function can yield.
|
||||
*/
|
||||
#if !defined(luai_threadyield)
|
||||
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** these macros allow user-specific actions when a thread is
|
||||
** created/deleted/resumed/yielded.
|
||||
*/
|
||||
#if !defined(luai_userstateopen)
|
||||
#define luai_userstateopen(L) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstateclose)
|
||||
#define luai_userstateclose(L) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstatethread)
|
||||
#define luai_userstatethread(L,L1) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstatefree)
|
||||
#define luai_userstatefree(L,L1) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstateresume)
|
||||
#define luai_userstateresume(L,n) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstateyield)
|
||||
#define luai_userstateyield(L,n) ((void)L)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** The luai_num* macros define the primitive operations over numbers.
|
||||
@ -357,24 +281,77 @@ typedef l_uint32 Instruction;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** lua_numbertointeger converts a float number with an integral value
|
||||
** to an integer, or returns 0 if the float is not within the range of
|
||||
** a lua_Integer. (The range comparisons are tricky because of
|
||||
** rounding. The tests here assume a two-complement representation,
|
||||
** where MININTEGER always has an exact representation as a float;
|
||||
** MAXINTEGER may not have one, and therefore its conversion to float
|
||||
** may have an ill-defined value.)
|
||||
*/
|
||||
#define lua_numbertointeger(n,p) \
|
||||
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
|
||||
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
||||
(*(p) = (LUA_INTEGER)(n), 1))
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** macro to control inclusion of some hard tests on stack reallocation
|
||||
** LUAI_FUNC is a mark for all extern functions that are not to be
|
||||
** exported to outside modules.
|
||||
** LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables,
|
||||
** none of which to be exported to outside modules (LUAI_DDEF for
|
||||
** definitions and LUAI_DDEC for declarations).
|
||||
** Elf and MACH/gcc (versions 3.2 and later) mark them as "hidden" to
|
||||
** optimize access when Lua is compiled as a shared library. Not all elf
|
||||
** targets support this attribute. Unfortunately, gcc does not offer
|
||||
** a way to check whether the target offers that support, and those
|
||||
** without support give a warning about it. To avoid these warnings,
|
||||
** change to the default definition.
|
||||
*/
|
||||
#if !defined(HARDSTACKTESTS)
|
||||
#define condmovestack(L,pre,pos) ((void)0)
|
||||
#if !defined(LUAI_FUNC)
|
||||
|
||||
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||
(defined(__ELF__) || defined(__MACH__))
|
||||
#define LUAI_FUNC __attribute__((visibility("internal"))) extern
|
||||
#else
|
||||
/* realloc stack keeping its size */
|
||||
#define condmovestack(L,pre,pos) \
|
||||
{ int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
|
||||
#define LUAI_FUNC extern
|
||||
#endif
|
||||
|
||||
#if !defined(HARDMEMTESTS)
|
||||
#define condchangemem(L,pre,pos) ((void)0)
|
||||
#else
|
||||
#define condchangemem(L,pre,pos) \
|
||||
{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
|
||||
#endif
|
||||
#define LUAI_DDEC(dec) LUAI_FUNC dec
|
||||
#define LUAI_DDEF /* empty */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* Give these macros simpler names for internal use */
|
||||
#define l_likely(x) luai_likely(x)
|
||||
#define l_unlikely(x) luai_unlikely(x)
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** "Abstraction Layer" for basic report of messages and errors
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/* print a string */
|
||||
#if !defined(lua_writestring)
|
||||
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
||||
#endif
|
||||
|
||||
/* print a newline and flush the output */
|
||||
#if !defined(lua_writeline)
|
||||
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
||||
#endif
|
||||
|
||||
/* print an error message */
|
||||
#if !defined(lua_writestringerror)
|
||||
#define lua_writestringerror(s,p) \
|
||||
(fprintf(stderr, (s), (p)), fflush(stderr))
|
||||
#endif
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
163
lmathlib.c
163
lmathlib.c
@ -20,6 +20,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
#undef PI
|
||||
@ -37,31 +38,37 @@ static int math_abs (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_sin (lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_cos (lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_tan (lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_asin (lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_acos (lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_atan (lua_State *L) {
|
||||
lua_Number y = luaL_checknumber(L, 1);
|
||||
lua_Number x = luaL_optnumber(L, 2, 1);
|
||||
@ -105,7 +112,7 @@ static int math_floor (lua_State *L) {
|
||||
|
||||
static int math_ceil (lua_State *L) {
|
||||
if (lua_isinteger(L, 1))
|
||||
lua_settop(L, 1); /* integer is its own ceil */
|
||||
lua_settop(L, 1); /* integer is its own ceiling */
|
||||
else {
|
||||
lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
|
||||
pushnumint(L, d);
|
||||
@ -166,6 +173,7 @@ static int math_ult (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_log (lua_State *L) {
|
||||
lua_Number x = luaL_checknumber(L, 1);
|
||||
lua_Number res;
|
||||
@ -187,22 +195,42 @@ static int math_log (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_exp (lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_rad (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_frexp (lua_State *L) {
|
||||
lua_Number x = luaL_checknumber(L, 1);
|
||||
int ep;
|
||||
lua_pushnumber(L, l_mathop(frexp)(x, &ep));
|
||||
lua_pushinteger(L, ep);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
static int math_ldexp (lua_State *L) {
|
||||
lua_Number x = luaL_checknumber(L, 1);
|
||||
int ep = (int)luaL_checkinteger(L, 2);
|
||||
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int math_min (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int imin = 1; /* index of current minimum value */
|
||||
@ -249,6 +277,15 @@ static int math_type (lua_State *L) {
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
** This code uses lots of shifts. ISO C does not allow shifts greater
|
||||
** than or equal to the width of the type being shifted, so some shifts
|
||||
** are written in convoluted ways to match that restriction. For
|
||||
** preprocessor tests, it assumes a width of 32 bits, so the maximum
|
||||
** shift there is 31 bits.
|
||||
*/
|
||||
|
||||
|
||||
/* number of binary digits in the mantissa of a float */
|
||||
#define FIGS l_floatatt(MANT_DIG)
|
||||
|
||||
@ -271,16 +308,19 @@ static int math_type (lua_State *L) {
|
||||
|
||||
/* 'long' has at least 64 bits */
|
||||
#define Rand64 unsigned long
|
||||
#define SRand64 long
|
||||
|
||||
#elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
|
||||
|
||||
/* there is a 'long long' type (which must have at least 64 bits) */
|
||||
#define Rand64 unsigned long long
|
||||
#define SRand64 long long
|
||||
|
||||
#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3
|
||||
|
||||
/* 'lua_Unsigned' has at least 64 bits */
|
||||
#define Rand64 lua_Unsigned
|
||||
#define SRand64 lua_Integer
|
||||
|
||||
#endif
|
||||
|
||||
@ -319,23 +359,30 @@ static Rand64 nextrand (Rand64 *state) {
|
||||
}
|
||||
|
||||
|
||||
/* must take care to not shift stuff by more than 63 slots */
|
||||
|
||||
|
||||
/*
|
||||
** Convert bits from a random integer into a float in the
|
||||
** interval [0,1), getting the higher FIG bits from the
|
||||
** random unsigned integer and converting that to a float.
|
||||
** Some old Microsoft compilers cannot cast an unsigned long
|
||||
** to a floating-point number, so we use a signed long as an
|
||||
** intermediary. When lua_Number is float or double, the shift ensures
|
||||
** that 'sx' is non negative; in that case, a good compiler will remove
|
||||
** the correction.
|
||||
*/
|
||||
|
||||
/* must throw out the extra (64 - FIGS) bits */
|
||||
#define shift64_FIG (64 - FIGS)
|
||||
|
||||
/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */
|
||||
/* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */
|
||||
#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
|
||||
|
||||
static lua_Number I2d (Rand64 x) {
|
||||
return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG;
|
||||
SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG);
|
||||
lua_Number res = (lua_Number)(sx) * scaleFIG;
|
||||
if (sx < 0)
|
||||
res += l_mathop(1.0); /* correct the two's complement if negative */
|
||||
lua_assert(0 <= res && res < 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* convert a 'Rand64' to a 'lua_Unsigned' */
|
||||
@ -347,25 +394,17 @@ static lua_Number I2d (Rand64 x) {
|
||||
|
||||
#else /* no 'Rand64' }{ */
|
||||
|
||||
/* get an integer with at least 32 bits */
|
||||
#if LUAI_IS32INT
|
||||
typedef unsigned int lu_int32;
|
||||
#else
|
||||
typedef unsigned long lu_int32;
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Use two 32-bit integers to represent a 64-bit quantity.
|
||||
*/
|
||||
typedef struct Rand64 {
|
||||
lu_int32 h; /* higher half */
|
||||
lu_int32 l; /* lower half */
|
||||
l_uint32 h; /* higher half */
|
||||
l_uint32 l; /* lower half */
|
||||
} Rand64;
|
||||
|
||||
|
||||
/*
|
||||
** If 'lu_int32' has more than 32 bits, the extra bits do not interfere
|
||||
** If 'l_uint32' has more than 32 bits, the extra bits do not interfere
|
||||
** with the 32 initial bits, except in a right shift and comparisons.
|
||||
** Moreover, the final result has to discard the extra bits.
|
||||
*/
|
||||
@ -379,7 +418,7 @@ typedef struct Rand64 {
|
||||
*/
|
||||
|
||||
/* build a new Rand64 value */
|
||||
static Rand64 packI (lu_int32 h, lu_int32 l) {
|
||||
static Rand64 packI (l_uint32 h, l_uint32 l) {
|
||||
Rand64 result;
|
||||
result.h = h;
|
||||
result.l = l;
|
||||
@ -452,7 +491,7 @@ static Rand64 nextrand (Rand64 *state) {
|
||||
*/
|
||||
|
||||
/* an unsigned 1 with proper type */
|
||||
#define UONE ((lu_int32)1)
|
||||
#define UONE ((l_uint32)1)
|
||||
|
||||
|
||||
#if FIGS <= 32
|
||||
@ -471,8 +510,6 @@ static lua_Number I2d (Rand64 x) {
|
||||
|
||||
#else /* 32 < FIGS <= 64 */
|
||||
|
||||
/* must take care to not shift stuff by more than 31 slots */
|
||||
|
||||
/* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */
|
||||
#define scaleFIG \
|
||||
(l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33)))
|
||||
@ -505,7 +542,7 @@ static lua_Unsigned I2UInt (Rand64 x) {
|
||||
|
||||
/* convert a 'lua_Unsigned' to a 'Rand64' */
|
||||
static Rand64 Int2I (lua_Unsigned n) {
|
||||
return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n);
|
||||
return packI((l_uint32)((n >> 31) >> 1), (l_uint32)n);
|
||||
}
|
||||
|
||||
#endif /* } */
|
||||
@ -523,7 +560,7 @@ typedef struct {
|
||||
** Project the random integer 'ran' into the interval [0, n].
|
||||
** Because 'ran' has 2^B possible values, the projection can only be
|
||||
** uniform when the size of the interval is a power of 2 (exact
|
||||
** division). Otherwise, to get a uniform projection into [0, n], we
|
||||
** division). So, to get a uniform projection into [0, n], we
|
||||
** first compute 'lim', the smallest Mersenne number not smaller than
|
||||
** 'n'. We then project 'ran' into the interval [0, lim]. If the result
|
||||
** is inside [0, n], we are done. Otherwise, we try with another 'ran',
|
||||
@ -531,26 +568,14 @@ typedef struct {
|
||||
*/
|
||||
static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
|
||||
RanState *state) {
|
||||
if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
|
||||
return ran & n; /* no bias */
|
||||
else {
|
||||
lua_Unsigned lim = n;
|
||||
/* compute the smallest (2^b - 1) not smaller than 'n' */
|
||||
lim |= (lim >> 1);
|
||||
lim |= (lim >> 2);
|
||||
lim |= (lim >> 4);
|
||||
lim |= (lim >> 8);
|
||||
lim |= (lim >> 16);
|
||||
#if (LUA_MAXUNSIGNED >> 31) >= 3
|
||||
lim |= (lim >> 32); /* integer type has more than 32 bits */
|
||||
#endif
|
||||
lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
|
||||
&& lim >= n /* not smaller than 'n', */
|
||||
&& (lim >> 1) < n); /* and it is the smallest one */
|
||||
while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
|
||||
ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
|
||||
return ran;
|
||||
}
|
||||
lua_Unsigned lim = n; /* to compute the Mersenne number */
|
||||
int sh; /* how much to spread bits to the right in 'lim' */
|
||||
/* spread '1' bits in 'lim' until it becomes a Mersenne number */
|
||||
for (sh = 1; (lim & (lim + 1)) != 0; sh *= 2)
|
||||
lim |= (lim >> sh); /* spread '1's to the right */
|
||||
while ((ran &= lim) > n) /* project 'ran' into [0..lim] and test */
|
||||
ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
|
||||
return ran;
|
||||
}
|
||||
|
||||
|
||||
@ -568,7 +593,7 @@ static int math_random (lua_State *L) {
|
||||
low = 1;
|
||||
up = luaL_checkinteger(L, 1);
|
||||
if (up == 0) { /* single 0 as argument? */
|
||||
lua_pushinteger(L, I2UInt(rv)); /* full random integer */
|
||||
lua_pushinteger(L, l_castU2S(I2UInt(rv))); /* full random integer */
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
@ -583,8 +608,8 @@ static int math_random (lua_State *L) {
|
||||
/* random integer in the interval [low, up] */
|
||||
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
||||
/* project random integer into the interval [0, up - low] */
|
||||
p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
|
||||
lua_pushinteger(L, p + (lua_Unsigned)low);
|
||||
p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state);
|
||||
lua_pushinteger(L, l_castU2S(p + l_castS2U(low)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -598,33 +623,23 @@ static void setseed (lua_State *L, Rand64 *state,
|
||||
state[3] = Int2I(0);
|
||||
for (i = 0; i < 16; i++)
|
||||
nextrand(state); /* discard initial values to "spread" seed */
|
||||
lua_pushinteger(L, n1);
|
||||
lua_pushinteger(L, n2);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Set a "random" seed. To get some randomness, use the current time
|
||||
** and the address of 'L' (in case the machine does address space layout
|
||||
** randomization).
|
||||
*/
|
||||
static void randseed (lua_State *L, RanState *state) {
|
||||
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
|
||||
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
|
||||
setseed(L, state->s, seed1, seed2);
|
||||
lua_pushinteger(L, l_castU2S(n1));
|
||||
lua_pushinteger(L, l_castU2S(n2));
|
||||
}
|
||||
|
||||
|
||||
static int math_randomseed (lua_State *L) {
|
||||
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
||||
lua_Unsigned n1, n2;
|
||||
if (lua_isnone(L, 1)) {
|
||||
randseed(L, state);
|
||||
n1 = luaL_makeseed(L); /* "random" seed */
|
||||
n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */
|
||||
}
|
||||
else {
|
||||
lua_Integer n1 = luaL_checkinteger(L, 1);
|
||||
lua_Integer n2 = luaL_optinteger(L, 2, 0);
|
||||
setseed(L, state->s, n1, n2);
|
||||
n1 = l_castS2U(luaL_checkinteger(L, 1));
|
||||
n2 = l_castS2U(luaL_optinteger(L, 2, 0));
|
||||
}
|
||||
setseed(L, state->s, n1, n2);
|
||||
return 2; /* return seeds */
|
||||
}
|
||||
|
||||
@ -641,7 +656,7 @@ static const luaL_Reg randfuncs[] = {
|
||||
*/
|
||||
static void setrandfunc (lua_State *L) {
|
||||
RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
|
||||
randseed(L, state); /* initialize with a "random" seed */
|
||||
setseed(L, state->s, luaL_makeseed(L), 0); /* initialize with random seed */
|
||||
lua_pop(L, 2); /* remove pushed seeds */
|
||||
luaL_setfuncs(L, randfuncs, 1);
|
||||
}
|
||||
@ -678,20 +693,6 @@ static int math_pow (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_frexp (lua_State *L) {
|
||||
int e;
|
||||
lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
|
||||
lua_pushinteger(L, e);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int math_ldexp (lua_State *L) {
|
||||
lua_Number x = luaL_checknumber(L, 1);
|
||||
int ep = (int)luaL_checkinteger(L, 2);
|
||||
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_log10 (lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
@ -714,7 +715,9 @@ static const luaL_Reg mathlib[] = {
|
||||
{"tointeger", math_toint},
|
||||
{"floor", math_floor},
|
||||
{"fmod", math_fmod},
|
||||
{"frexp", math_frexp},
|
||||
{"ult", math_ult},
|
||||
{"ldexp", math_ldexp},
|
||||
{"log", math_log},
|
||||
{"max", math_max},
|
||||
{"min", math_min},
|
||||
@ -730,8 +733,6 @@ static const luaL_Reg mathlib[] = {
|
||||
{"sinh", math_sinh},
|
||||
{"tanh", math_tanh},
|
||||
{"pow", math_pow},
|
||||
{"frexp", math_frexp},
|
||||
{"ldexp", math_ldexp},
|
||||
{"log10", math_log10},
|
||||
#endif
|
||||
/* placeholders */
|
||||
|
||||
18
lmem.c
18
lmem.c
@ -95,7 +95,7 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
|
||||
|
||||
|
||||
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
||||
int size_elems, int limit, const char *what) {
|
||||
unsigned size_elems, int limit, const char *what) {
|
||||
void *newblock;
|
||||
int size = *psize;
|
||||
if (nelems + 1 <= size) /* does one extra element still fit? */
|
||||
@ -126,10 +126,10 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
||||
** error.
|
||||
*/
|
||||
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
||||
int final_n, int size_elem) {
|
||||
int final_n, unsigned size_elem) {
|
||||
void *newblock;
|
||||
size_t oldsize = cast_sizet((*size) * size_elem);
|
||||
size_t newsize = cast_sizet(final_n * size_elem);
|
||||
size_t oldsize = cast_sizet(*size) * size_elem;
|
||||
size_t newsize = cast_sizet(final_n) * size_elem;
|
||||
lua_assert(newsize <= oldsize);
|
||||
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
|
||||
*size = final_n;
|
||||
@ -151,7 +151,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) {
|
||||
global_State *g = G(L);
|
||||
lua_assert((osize == 0) == (block == NULL));
|
||||
callfrealloc(g, block, osize, 0);
|
||||
g->GCdebt -= osize;
|
||||
g->GCdebt += cast(l_mem, osize);
|
||||
}
|
||||
|
||||
|
||||
@ -184,7 +184,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||
return NULL; /* do not update 'GCdebt' */
|
||||
}
|
||||
lua_assert((nsize == 0) == (newblock == NULL));
|
||||
g->GCdebt = (g->GCdebt + nsize) - osize;
|
||||
g->GCdebt -= cast(l_mem, nsize) - cast(l_mem, osize);
|
||||
return newblock;
|
||||
}
|
||||
|
||||
@ -203,13 +203,13 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
||||
return NULL; /* that's all */
|
||||
else {
|
||||
global_State *g = G(L);
|
||||
void *newblock = firsttry(g, NULL, tag, size);
|
||||
void *newblock = firsttry(g, NULL, cast_sizet(tag), size);
|
||||
if (l_unlikely(newblock == NULL)) {
|
||||
newblock = tryagain(L, NULL, tag, size);
|
||||
newblock = tryagain(L, NULL, cast_sizet(tag), size);
|
||||
if (newblock == NULL)
|
||||
luaM_error(L);
|
||||
}
|
||||
g->GCdebt += size;
|
||||
g->GCdebt -= cast(l_mem, size);
|
||||
return newblock;
|
||||
}
|
||||
}
|
||||
|
||||
13
lmem.h
13
lmem.h
@ -39,11 +39,11 @@
|
||||
** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
|
||||
** the result is not larger than 'n' and cannot overflow a 'size_t'
|
||||
** when multiplied by the size of type 't'. (Assumes that 'n' is an
|
||||
** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
|
||||
** 'int' and that 'int' is not larger than 'size_t'.)
|
||||
*/
|
||||
#define luaM_limitN(n,t) \
|
||||
((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
|
||||
cast_uint((MAX_SIZET/sizeof(t))))
|
||||
cast_int((MAX_SIZET/sizeof(t))))
|
||||
|
||||
|
||||
/*
|
||||
@ -57,12 +57,15 @@
|
||||
#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b)))
|
||||
|
||||
#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0))
|
||||
#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0))
|
||||
#define luaM_newvector(L,n,t) \
|
||||
cast(t*, luaM_malloc_(L, cast_sizet(n)*sizeof(t), 0))
|
||||
#define luaM_newvectorchecked(L,n,t) \
|
||||
(luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
|
||||
|
||||
#define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag)
|
||||
|
||||
#define luaM_newblock(L, size) luaM_newvector(L, size, char)
|
||||
|
||||
#define luaM_growvector(L,v,nelems,size,t,limit,e) \
|
||||
((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
|
||||
luaM_limitN(limit,t),e)))
|
||||
@ -83,10 +86,10 @@ LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
|
||||
size_t size);
|
||||
LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
|
||||
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
|
||||
int *size, int size_elem, int limit,
|
||||
int *size, unsigned size_elem, int limit,
|
||||
const char *what);
|
||||
LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
|
||||
int final_n, int size_elem);
|
||||
int final_n, unsigned size_elem);
|
||||
LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
|
||||
|
||||
#endif
|
||||
|
||||
135
loadlib.c
135
loadlib.c
@ -22,15 +22,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
/*
|
||||
** LUA_IGMARK is a mark to ignore all before it when building the
|
||||
** luaopen_ function name.
|
||||
*/
|
||||
#if !defined (LUA_IGMARK)
|
||||
#define LUA_IGMARK "-"
|
||||
#endif
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -67,11 +59,8 @@ static const char *const CLIBS = "_CLIBS";
|
||||
#define setprogdir(L) ((void)0)
|
||||
|
||||
|
||||
/*
|
||||
** Special type equivalent to '(void*)' for functions in gcc
|
||||
** (to suppress warnings when converting function pointers)
|
||||
*/
|
||||
typedef void (*voidf)(void);
|
||||
/* cast void* to a Lua function */
|
||||
#define cast_Lfunc(p) cast(lua_CFunction, cast_func(p))
|
||||
|
||||
|
||||
/*
|
||||
@ -104,26 +93,13 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
||||
#if defined(LUA_USE_DLOPEN) /* { */
|
||||
/*
|
||||
** {========================================================================
|
||||
** This is an implementation of loadlib based on the dlfcn interface.
|
||||
** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
|
||||
** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
|
||||
** as an emulation layer on top of native functions.
|
||||
** This is an implementation of loadlib based on the dlfcn interface,
|
||||
** which is available in all POSIX systems.
|
||||
** =========================================================================
|
||||
*/
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
/*
|
||||
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
||||
** is undefined according to ISO C, but POSIX assumes that it works.
|
||||
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
||||
*/
|
||||
#if defined(__GNUC__)
|
||||
#define cast_func(p) (__extension__ (lua_CFunction)(p))
|
||||
#else
|
||||
#define cast_func(p) ((lua_CFunction)(p))
|
||||
#endif
|
||||
|
||||
|
||||
static void lsys_unloadlib (void *lib) {
|
||||
dlclose(lib);
|
||||
@ -139,7 +115,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
||||
|
||||
|
||||
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||
lua_CFunction f = cast_func(dlsym(lib, sym));
|
||||
lua_CFunction f = cast_Lfunc(dlsym(lib, sym));
|
||||
if (l_unlikely(f == NULL))
|
||||
lua_pushstring(L, dlerror());
|
||||
return f;
|
||||
@ -215,7 +191,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
||||
|
||||
|
||||
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||
lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
|
||||
lua_CFunction f = cast_Lfunc(GetProcAddress((HMODULE)lib, sym));
|
||||
if (f == NULL) pusherror(L);
|
||||
return f;
|
||||
}
|
||||
@ -292,7 +268,8 @@ static int noenv (lua_State *L) {
|
||||
|
||||
|
||||
/*
|
||||
** Set a path
|
||||
** Set a path. (If using the default path, assume it is a string
|
||||
** literal in C and create it as an external string.)
|
||||
*/
|
||||
static void setpath (lua_State *L, const char *fieldname,
|
||||
const char *envname,
|
||||
@ -303,7 +280,7 @@ static void setpath (lua_State *L, const char *fieldname,
|
||||
if (path == NULL) /* no versioned environment variable? */
|
||||
path = getenv(envname); /* try unversioned name */
|
||||
if (path == NULL || noenv(L)) /* no environment variable? */
|
||||
lua_pushstring(L, dft); /* use default */
|
||||
lua_pushexternalstring(L, dft, strlen(dft), NULL, NULL); /* use default */
|
||||
else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
|
||||
lua_pushstring(L, path); /* nothing to change */
|
||||
else { /* path contains a ";;": insert default path in its place */
|
||||
@ -311,13 +288,13 @@ static void setpath (lua_State *L, const char *fieldname,
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
if (path < dftmark) { /* is there a prefix before ';;'? */
|
||||
luaL_addlstring(&b, path, dftmark - path); /* add it */
|
||||
luaL_addlstring(&b, path, ct_diff2sz(dftmark - path)); /* add it */
|
||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
||||
}
|
||||
luaL_addstring(&b, dft); /* add default */
|
||||
if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
|
||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
||||
luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
|
||||
luaL_addlstring(&b, dftmark + 2, ct_diff2sz((path + len - 2) - dftmark));
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
}
|
||||
@ -329,6 +306,16 @@ static void setpath (lua_State *L, const char *fieldname,
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** External strings created by DLLs may need the DLL code to be
|
||||
** deallocated. This implies that a DLL can only be unloaded after all
|
||||
** its strings were deallocated. To ensure that, we create a 'library
|
||||
** string' to represent each DLL, and when this string is deallocated
|
||||
** it closes its corresponding DLL.
|
||||
** (The string itself is irrelevant; its userdata is the DLL pointer.)
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** return registry.CLIBS[path]
|
||||
*/
|
||||
@ -343,34 +330,41 @@ static void *checkclib (lua_State *L, const char *path) {
|
||||
|
||||
|
||||
/*
|
||||
** registry.CLIBS[path] = plib -- for queries
|
||||
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
||||
** Deallocate function for library strings.
|
||||
** Unload the DLL associated with the string being deallocated.
|
||||
*/
|
||||
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_pushlightuserdata(L, plib);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||
lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
|
||||
lua_pop(L, 1); /* pop CLIBS table */
|
||||
static void *freelib (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
/* string itself is irrelevant and static */
|
||||
(void)ptr; (void)osize; (void)nsize;
|
||||
lsys_unloadlib(ud); /* unload library represented by the string */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
|
||||
** handles in list CLIBS
|
||||
** Create a library string that, when deallocated, will unload 'plib'
|
||||
*/
|
||||
static int gctm (lua_State *L) {
|
||||
lua_Integer n = luaL_len(L, 1);
|
||||
for (; n >= 1; n--) { /* for each handle, in reverse order */
|
||||
lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
|
||||
lsys_unloadlib(lua_touserdata(L, -1));
|
||||
lua_pop(L, 1); /* pop handle */
|
||||
}
|
||||
return 0;
|
||||
static void createlibstr (lua_State *L, void *plib) {
|
||||
/* common content for all library strings */
|
||||
static const char dummy[] = "01234567890";
|
||||
lua_pushexternalstring(L, dummy, sizeof(dummy) - 1, freelib, plib);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** registry.CLIBS[path] = plib -- for queries.
|
||||
** Also create a reference to strlib, so that the library string will
|
||||
** only be collected when registry.CLIBS is collected.
|
||||
*/
|
||||
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_pushlightuserdata(L, plib);
|
||||
lua_setfield(L, -2, path); /* CLIBS[path] = plib */
|
||||
createlibstr(L, plib);
|
||||
luaL_ref(L, -2); /* keep library string in CLIBS */
|
||||
lua_pop(L, 1); /* pop CLIBS table */
|
||||
}
|
||||
|
||||
|
||||
/* error codes for 'lookforfunc' */
|
||||
#define ERRLIB 1
|
||||
@ -384,8 +378,8 @@ static int gctm (lua_State *L) {
|
||||
** Then, if 'sym' is '*', return true (as library has been loaded).
|
||||
** Otherwise, look for symbol 'sym' in the library and push a
|
||||
** C function with that symbol.
|
||||
** Return 0 and 'true' or a function in the stack; in case of
|
||||
** errors, return an error code and an error message in the stack.
|
||||
** Return 0 with 'true' or a function in the stack; in case of
|
||||
** errors, return an error code with an error message in the stack.
|
||||
*/
|
||||
static int lookforfunc (lua_State *L, const char *path, const char *sym) {
|
||||
void *reg = checkclib(L, path); /* check loaded C libraries */
|
||||
@ -566,7 +560,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
|
||||
mark = strchr(modname, *LUA_IGMARK);
|
||||
if (mark) {
|
||||
int stat;
|
||||
openfunc = lua_pushlstring(L, modname, mark - modname);
|
||||
openfunc = lua_pushlstring(L, modname, ct_diff2sz(mark - modname));
|
||||
openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
|
||||
stat = lookforfunc(L, filename, openfunc);
|
||||
if (stat != ERRFUNC) return stat;
|
||||
@ -591,7 +585,7 @@ static int searcher_Croot (lua_State *L) {
|
||||
const char *p = strchr(name, '.');
|
||||
int stat;
|
||||
if (p == NULL) return 0; /* is root */
|
||||
lua_pushlstring(L, name, p - name);
|
||||
lua_pushlstring(L, name, ct_diff2sz(p - name));
|
||||
filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
|
||||
if (filename == NULL) return 1; /* root not found */
|
||||
if ((stat = loadfunc(L, filename, name)) != 0) {
|
||||
@ -629,12 +623,12 @@ static void findloader (lua_State *L, const char *name) {
|
||||
!= LUA_TTABLE))
|
||||
luaL_error(L, "'package.searchers' must be a table");
|
||||
luaL_buffinit(L, &msg);
|
||||
luaL_addstring(&msg, "\n\t"); /* error-message prefix for first message */
|
||||
/* iterate over available searchers to find a loader */
|
||||
for (i = 1; ; i++) {
|
||||
luaL_addstring(&msg, "\n\t"); /* error-message prefix */
|
||||
if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */
|
||||
lua_pop(L, 1); /* remove nil */
|
||||
luaL_buffsub(&msg, 2); /* remove prefix */
|
||||
luaL_buffsub(&msg, 2); /* remove last prefix */
|
||||
luaL_pushresult(&msg); /* create error message */
|
||||
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
||||
}
|
||||
@ -645,11 +639,10 @@ static void findloader (lua_State *L, const char *name) {
|
||||
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
|
||||
lua_pop(L, 1); /* remove extra return */
|
||||
luaL_addvalue(&msg); /* concatenate error message */
|
||||
luaL_addstring(&msg, "\n\t"); /* prefix for next message */
|
||||
}
|
||||
else { /* no error message */
|
||||
else /* no error message */
|
||||
lua_pop(L, 2); /* remove both returns */
|
||||
luaL_buffsub(&msg, 2); /* remove prefix */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -728,21 +721,9 @@ static void createsearcherstable (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** create table CLIBS to keep track of loaded C libraries,
|
||||
** setting a finalizer to close all libraries when closing state.
|
||||
*/
|
||||
static void createclibstable (lua_State *L) {
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
||||
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
||||
lua_pushcfunction(L, gctm);
|
||||
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
createclibstable(L);
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
||||
lua_pop(L, 1); /* will not use it now */
|
||||
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
||||
createsearcherstable(L);
|
||||
/* set paths */
|
||||
|
||||
336
lobject.c
336
lobject.c
@ -10,6 +10,7 @@
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <float.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
@ -30,10 +31,11 @@
|
||||
|
||||
|
||||
/*
|
||||
** Computes ceil(log2(x))
|
||||
** Computes ceil(log2(x)), which is the smallest integer n such that
|
||||
** x <= (1 << n).
|
||||
*/
|
||||
int luaO_ceillog2 (unsigned int x) {
|
||||
static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
|
||||
lu_byte luaO_ceillog2 (unsigned int x) {
|
||||
static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */
|
||||
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
@ -46,7 +48,67 @@ int luaO_ceillog2 (unsigned int x) {
|
||||
int l = 0;
|
||||
x--;
|
||||
while (x >= 256) { l += 8; x >>= 8; }
|
||||
return l + log_2[x];
|
||||
return cast_byte(l + log_2[x]);
|
||||
}
|
||||
|
||||
/*
|
||||
** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx).
|
||||
** The exponent is represented using excess-7. Mimicking IEEE 754, the
|
||||
** representation normalizes the number when possible, assuming an extra
|
||||
** 1 before the mantissa (xxxx) and adding one to the exponent (eeee)
|
||||
** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if
|
||||
** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers).
|
||||
*/
|
||||
lu_byte luaO_codeparam (unsigned int p) {
|
||||
if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */
|
||||
return 0xFF; /* return maximum value */
|
||||
else {
|
||||
p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */
|
||||
if (p < 0x10) { /* subnormal number? */
|
||||
/* exponent bits are already zero; nothing else to do */
|
||||
return cast_byte(p);
|
||||
}
|
||||
else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */
|
||||
/* preserve 5 bits in 'p' */
|
||||
unsigned log = luaO_ceillog2(p + 1) - 5u;
|
||||
return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Computes 'p' times 'x', where 'p' is a floating-point byte. Roughly,
|
||||
** we have to multiply 'x' by the mantissa and then shift accordingly to
|
||||
** the exponent. If the exponent is positive, both the multiplication
|
||||
** and the shift increase 'x', so we have to care only about overflows.
|
||||
** For negative exponents, however, multiplying before the shift keeps
|
||||
** more significant bits, as long as the multiplication does not
|
||||
** overflow, so we check which order is best.
|
||||
*/
|
||||
l_mem luaO_applyparam (lu_byte p, l_mem x) {
|
||||
int m = p & 0xF; /* mantissa */
|
||||
int e = (p >> 4); /* exponent */
|
||||
if (e > 0) { /* normalized? */
|
||||
e--; /* correct exponent */
|
||||
m += 0x10; /* correct mantissa; maximum value is 0x1F */
|
||||
}
|
||||
e -= 7; /* correct excess-7 */
|
||||
if (e >= 0) {
|
||||
if (x < (MAX_LMEM / 0x1F) >> e) /* no overflow? */
|
||||
return (x * m) << e; /* order doesn't matter here */
|
||||
else /* real overflow */
|
||||
return MAX_LMEM;
|
||||
}
|
||||
else { /* negative exponent */
|
||||
e = -e;
|
||||
if (x < MAX_LMEM / 0x1F) /* multiplication cannot overflow? */
|
||||
return (x * m) >> e; /* multiplying first gives more precision */
|
||||
else if ((x >> e) < MAX_LMEM / 0x1F) /* cannot overflow after shift? */
|
||||
return (x >> e) * m;
|
||||
else /* real overflow */
|
||||
return MAX_LMEM;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -132,9 +194,10 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
||||
}
|
||||
|
||||
|
||||
int luaO_hexavalue (int c) {
|
||||
if (lisdigit(c)) return c - '0';
|
||||
else return (ltolower(c) - 'a') + 10;
|
||||
lu_byte luaO_hexavalue (int c) {
|
||||
lua_assert(lisxdigit(c));
|
||||
if (lisdigit(c)) return cast_byte(c - '0');
|
||||
else return cast_byte((ltolower(c) - 'a') + 10);
|
||||
}
|
||||
|
||||
|
||||
@ -185,7 +248,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||
nosigdig++;
|
||||
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
||||
r = (r * l_mathop(16.0)) + luaO_hexavalue(*s);
|
||||
else e++; /* too many digits; ignore, but still count for exponent */
|
||||
else e++; /* too many digits; ignore, but still count for exponent */
|
||||
if (hasdot) e--; /* decimal digit? correct exponent */
|
||||
}
|
||||
else break; /* neither a dot nor a digit */
|
||||
@ -292,7 +355,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
|
||||
int d = *s - '0';
|
||||
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
|
||||
return NULL; /* do not accept it (as integer) */
|
||||
a = a * 10 + d;
|
||||
a = a * 10 + cast_uint(d);
|
||||
empty = 0;
|
||||
}
|
||||
}
|
||||
@ -316,14 +379,14 @@ size_t luaO_str2num (const char *s, TValue *o) {
|
||||
}
|
||||
else
|
||||
return 0; /* conversion failed */
|
||||
return (e - s) + 1; /* success; return string size */
|
||||
return ct_diff2sz(e - s) + 1; /* success; return string size */
|
||||
}
|
||||
|
||||
|
||||
int luaO_utf8esc (char *buff, unsigned long x) {
|
||||
int luaO_utf8esc (char *buff, l_uint32 x) {
|
||||
int n = 1; /* number of bytes put in buffer (backwards) */
|
||||
lua_assert(x <= 0x7FFFFFFFu);
|
||||
if (x < 0x80) /* ascii? */
|
||||
if (x < 0x80) /* ASCII? */
|
||||
buff[UTF8BUFFSZ - 1] = cast_char(x);
|
||||
else { /* need continuation bytes */
|
||||
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
||||
@ -339,32 +402,59 @@ int luaO_utf8esc (char *buff, unsigned long x) {
|
||||
|
||||
|
||||
/*
|
||||
** Maximum length of the conversion of a number to a string. Must be
|
||||
** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT.
|
||||
** (For a long long int, this is 19 digits plus a sign and a final '\0',
|
||||
** adding to 21. For a long double, it can go to a sign, 33 digits,
|
||||
** the dot, an exponent letter, an exponent sign, 5 exponent digits,
|
||||
** and a final '\0', adding to 43.)
|
||||
** The size of the buffer for the conversion of a number to a string
|
||||
** 'LUA_N2SBUFFSZ' must be enough to accommodate both LUA_INTEGER_FMT
|
||||
** and LUA_NUMBER_FMT. For a long long int, this is 19 digits plus a
|
||||
** sign and a final '\0', adding to 21. For a long double, it can go to
|
||||
** a sign, the dot, an exponent letter, an exponent sign, 4 exponent
|
||||
** digits, the final '\0', plus the significant digits, which are
|
||||
** approximately the *_DIG attribute.
|
||||
*/
|
||||
#define MAXNUMBER2STR 44
|
||||
#if LUA_N2SBUFFSZ < (20 + l_floatatt(DIG))
|
||||
#error "invalid value for LUA_N2SBUFFSZ"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Convert a number object to a string, adding it to a buffer
|
||||
** Convert a float to a string, adding it to a buffer. First try with
|
||||
** a not too large number of digits, to avoid noise (for instance,
|
||||
** 1.1 going to "1.1000000000000001"). If that lose precision, so
|
||||
** that reading the result back gives a different number, then do the
|
||||
** conversion again with extra precision. Moreover, if the numeral looks
|
||||
** like an integer (without a decimal point or an exponent), add ".0" to
|
||||
** its end.
|
||||
*/
|
||||
static int tostringbuff (TValue *obj, char *buff) {
|
||||
static int tostringbuffFloat (lua_Number n, char *buff) {
|
||||
/* first conversion */
|
||||
int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT,
|
||||
(LUAI_UACNUMBER)n);
|
||||
lua_Number check = lua_str2number(buff, NULL); /* read it back */
|
||||
if (check != n) { /* not enough precision? */
|
||||
/* convert again with more precision */
|
||||
len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N,
|
||||
(LUAI_UACNUMBER)n);
|
||||
}
|
||||
/* looks like an integer? */
|
||||
if (buff[strspn(buff, "-0123456789")] == '\0') {
|
||||
buff[len++] = lua_getlocaledecpoint();
|
||||
buff[len++] = '0'; /* adds '.0' to result */
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Convert a number object to a string, adding it to a buffer.
|
||||
*/
|
||||
unsigned luaO_tostringbuff (const TValue *obj, char *buff) {
|
||||
int len;
|
||||
lua_assert(ttisnumber(obj));
|
||||
if (ttisinteger(obj))
|
||||
len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj));
|
||||
else {
|
||||
len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj));
|
||||
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
||||
buff[len++] = lua_getlocaledecpoint();
|
||||
buff[len++] = '0'; /* adds '.0' to result */
|
||||
}
|
||||
}
|
||||
return len;
|
||||
len = lua_integer2str(buff, LUA_N2SBUFFSZ, ivalue(obj));
|
||||
else
|
||||
len = tostringbuffFloat(fltvalue(obj), buff);
|
||||
lua_assert(len < LUA_N2SBUFFSZ);
|
||||
return cast_uint(len);
|
||||
}
|
||||
|
||||
|
||||
@ -372,8 +462,8 @@ static int tostringbuff (TValue *obj, char *buff) {
|
||||
** Convert a number object to a Lua string, replacing the value at 'obj'
|
||||
*/
|
||||
void luaO_tostring (lua_State *L, TValue *obj) {
|
||||
char buff[MAXNUMBER2STR];
|
||||
int len = tostringbuff(obj, buff);
|
||||
char buff[LUA_N2SBUFFSZ];
|
||||
unsigned len = luaO_tostringbuff(obj, buff);
|
||||
setsvalue(L, obj, luaS_newlstr(L, buff, len));
|
||||
}
|
||||
|
||||
@ -388,78 +478,104 @@ void luaO_tostring (lua_State *L, TValue *obj) {
|
||||
|
||||
/*
|
||||
** Size for buffer space used by 'luaO_pushvfstring'. It should be
|
||||
** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages,
|
||||
** so that 'luaG_addinfo' can work directly on the buffer.
|
||||
** (LUA_IDSIZE + LUA_N2SBUFFSZ) + a minimal space for basic messages,
|
||||
** so that 'luaG_addinfo' can work directly on the static buffer.
|
||||
*/
|
||||
#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95)
|
||||
#define BUFVFS cast_uint(LUA_IDSIZE + LUA_N2SBUFFSZ + 95)
|
||||
|
||||
/* buffer used by 'luaO_pushvfstring' */
|
||||
/*
|
||||
** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while
|
||||
** building result (memory error [1] or buffer overflow [2]).
|
||||
*/
|
||||
typedef struct BuffFS {
|
||||
lua_State *L;
|
||||
int pushed; /* true if there is a part of the result on the stack */
|
||||
int blen; /* length of partial string in 'space' */
|
||||
char space[BUFVFS]; /* holds last part of the result */
|
||||
char *b;
|
||||
size_t buffsize;
|
||||
size_t blen; /* length of string in 'buff' */
|
||||
int err;
|
||||
char space[BUFVFS]; /* initial buffer */
|
||||
} BuffFS;
|
||||
|
||||
|
||||
static void initbuff (lua_State *L, BuffFS *buff) {
|
||||
buff->L = L;
|
||||
buff->b = buff->space;
|
||||
buff->buffsize = sizeof(buff->space);
|
||||
buff->blen = 0;
|
||||
buff->err = 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Push given string to the stack, as part of the result, and
|
||||
** join it to previous partial result if there is one.
|
||||
** It may call 'luaV_concat' while using one slot from EXTRA_STACK.
|
||||
** This call cannot invoke metamethods, as both operands must be
|
||||
** strings. It can, however, raise an error if the result is too
|
||||
** long. In that case, 'luaV_concat' frees the extra slot before
|
||||
** raising the error.
|
||||
** Push final result from 'luaO_pushvfstring'. This function may raise
|
||||
** errors explicitly or through memory errors, so it must run protected.
|
||||
*/
|
||||
static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
|
||||
static void pushbuff (lua_State *L, void *ud) {
|
||||
BuffFS *buff = cast(BuffFS*, ud);
|
||||
switch (buff->err) {
|
||||
case 1: /* memory error */
|
||||
luaD_throw(L, LUA_ERRMEM);
|
||||
break;
|
||||
case 2: /* length overflow: Add "..." at the end of result */
|
||||
if (buff->buffsize - buff->blen < 3)
|
||||
strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */
|
||||
else { /* there is enough space left for the "..." */
|
||||
strcpy(buff->b + buff->blen, "...");
|
||||
buff->blen += 3;
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
default: { /* no errors, but it can raise one creating the new string */
|
||||
TString *ts = luaS_newlstr(L, buff->b, buff->blen);
|
||||
setsvalue2s(L, L->top.p, ts);
|
||||
L->top.p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *clearbuff (BuffFS *buff) {
|
||||
lua_State *L = buff->L;
|
||||
setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr));
|
||||
L->top.p++; /* may use one slot from EXTRA_STACK */
|
||||
if (!buff->pushed) /* no previous string on the stack? */
|
||||
buff->pushed = 1; /* now there is one */
|
||||
else /* join previous string with new one */
|
||||
luaV_concat(L, 2);
|
||||
const char *res;
|
||||
if (luaD_rawrunprotected(L, pushbuff, buff) != LUA_OK) /* errors? */
|
||||
res = NULL; /* error message is on the top of the stack */
|
||||
else
|
||||
res = getstr(tsvalue(s2v(L->top.p - 1)));
|
||||
if (buff->b != buff->space) /* using dynamic buffer? */
|
||||
luaM_freearray(L, buff->b, buff->buffsize); /* free it */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** empty the buffer space into the stack
|
||||
*/
|
||||
static void clearbuff (BuffFS *buff) {
|
||||
pushstr(buff, buff->space, buff->blen); /* push buffer contents */
|
||||
buff->blen = 0; /* space now is empty */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get a space of size 'sz' in the buffer. If buffer has not enough
|
||||
** space, empty it. 'sz' must fit in an empty buffer.
|
||||
*/
|
||||
static char *getbuff (BuffFS *buff, int sz) {
|
||||
lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
|
||||
if (sz > BUFVFS - buff->blen) /* not enough space? */
|
||||
clearbuff(buff);
|
||||
return buff->space + buff->blen;
|
||||
}
|
||||
|
||||
|
||||
#define addsize(b,sz) ((b)->blen += (sz))
|
||||
|
||||
|
||||
/*
|
||||
** Add 'str' to the buffer. If string is larger than the buffer space,
|
||||
** push the string directly to the stack.
|
||||
*/
|
||||
static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
|
||||
if (slen <= BUFVFS) { /* does string fit into buffer? */
|
||||
char *bf = getbuff(buff, cast_int(slen));
|
||||
memcpy(bf, str, slen); /* add string to buffer */
|
||||
addsize(buff, cast_int(slen));
|
||||
}
|
||||
else { /* string larger than buffer */
|
||||
clearbuff(buff); /* string comes after buffer's content */
|
||||
pushstr(buff, str, slen); /* push string */
|
||||
size_t left = buff->buffsize - buff->blen; /* space left in the buffer */
|
||||
if (buff->err) /* do nothing else after an error */
|
||||
return;
|
||||
if (slen > left) { /* new string doesn't fit into current buffer? */
|
||||
if (slen > ((MAX_SIZE/2) - buff->blen)) { /* overflow? */
|
||||
memcpy(buff->b + buff->blen, str, left); /* copy what it can */
|
||||
buff->blen = buff->buffsize;
|
||||
buff->err = 2; /* doesn't add anything else */
|
||||
return;
|
||||
}
|
||||
else {
|
||||
size_t newsize = buff->buffsize + slen; /* limited to MAX_SIZE/2 */
|
||||
char *newb =
|
||||
(buff->b == buff->space) /* still using static space? */
|
||||
? luaM_reallocvector(buff->L, NULL, 0, newsize, char)
|
||||
: luaM_reallocvector(buff->L, buff->b, buff->buffsize, newsize,
|
||||
char);
|
||||
if (newb == NULL) { /* allocation error? */
|
||||
buff->err = 1; /* signal a memory error */
|
||||
return;
|
||||
}
|
||||
if (buff->b == buff->space) /* new buffer (not reallocated)? */
|
||||
memcpy(newb, buff->b, buff->blen); /* copy previous content */
|
||||
buff->b = newb; /* set new (larger) buffer... */
|
||||
buff->buffsize = newsize; /* ...and its new size */
|
||||
}
|
||||
}
|
||||
memcpy(buff->b + buff->blen, str, slen); /* copy new content */
|
||||
buff->blen += slen;
|
||||
}
|
||||
|
||||
|
||||
@ -467,9 +583,9 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
|
||||
** Add a numeral to the buffer.
|
||||
*/
|
||||
static void addnum2buff (BuffFS *buff, TValue *num) {
|
||||
char *numbuff = getbuff(buff, MAXNUMBER2STR);
|
||||
int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
|
||||
addsize(buff, len);
|
||||
char numbuff[LUA_N2SBUFFSZ];
|
||||
unsigned len = luaO_tostringbuff(num, numbuff);
|
||||
addstr2buff(buff, numbuff, len);
|
||||
}
|
||||
|
||||
|
||||
@ -480,10 +596,9 @@ static void addnum2buff (BuffFS *buff, TValue *num) {
|
||||
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
BuffFS buff; /* holds last part of the result */
|
||||
const char *e; /* points to next '%' */
|
||||
buff.pushed = buff.blen = 0;
|
||||
buff.L = L;
|
||||
initbuff(L, &buff);
|
||||
while ((e = strchr(fmt, '%')) != NULL) {
|
||||
addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */
|
||||
addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */
|
||||
switch (*(e + 1)) { /* conversion specifier */
|
||||
case 's': { /* zero-terminated string */
|
||||
const char *s = va_arg(argp, char *);
|
||||
@ -492,7 +607,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
break;
|
||||
}
|
||||
case 'c': { /* an 'int' as a character */
|
||||
char c = cast_uchar(va_arg(argp, int));
|
||||
char c = cast_char(va_arg(argp, int));
|
||||
addstr2buff(&buff, &c, sizeof(char));
|
||||
break;
|
||||
}
|
||||
@ -504,7 +619,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
}
|
||||
case 'I': { /* a 'lua_Integer' */
|
||||
TValue num;
|
||||
setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt)));
|
||||
setivalue(&num, cast_Integer(va_arg(argp, l_uacInt)));
|
||||
addnum2buff(&buff, &num);
|
||||
break;
|
||||
}
|
||||
@ -515,17 +630,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
break;
|
||||
}
|
||||
case 'p': { /* a pointer */
|
||||
const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
|
||||
char *bf = getbuff(&buff, sz);
|
||||
char bf[LUA_N2SBUFFSZ]; /* enough space for '%p' */
|
||||
void *p = va_arg(argp, void *);
|
||||
int len = lua_pointer2str(bf, sz, p);
|
||||
addsize(&buff, len);
|
||||
int len = lua_pointer2str(bf, LUA_N2SBUFFSZ, p);
|
||||
addstr2buff(&buff, bf, cast_uint(len));
|
||||
break;
|
||||
}
|
||||
case 'U': { /* a 'long' as a UTF-8 sequence */
|
||||
case 'U': { /* an 'unsigned long' as a UTF-8 sequence */
|
||||
char bf[UTF8BUFFSZ];
|
||||
int len = luaO_utf8esc(bf, va_arg(argp, long));
|
||||
addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
|
||||
unsigned long arg = va_arg(argp, unsigned long);
|
||||
int len = luaO_utf8esc(bf, cast(l_uint32, arg));
|
||||
addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len));
|
||||
break;
|
||||
}
|
||||
case '%': {
|
||||
@ -533,16 +648,14 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
|
||||
*(e + 1));
|
||||
addstr2buff(&buff, e, 2); /* keep unknown format in the result */
|
||||
break;
|
||||
}
|
||||
}
|
||||
fmt = e + 2; /* skip '%' and the specifier */
|
||||
}
|
||||
addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
|
||||
clearbuff(&buff); /* empty buffer into the stack */
|
||||
lua_assert(buff.pushed == 1);
|
||||
return svalue(s2v(L->top.p - 1));
|
||||
return clearbuff(&buff); /* empty buffer into a new string */
|
||||
}
|
||||
|
||||
|
||||
@ -552,6 +665,8 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
||||
va_start(argp, fmt);
|
||||
msg = luaO_pushvfstring(L, fmt, argp);
|
||||
va_end(argp);
|
||||
if (msg == NULL) /* error? */
|
||||
luaD_throw(L, LUA_ERRMEM);
|
||||
return msg;
|
||||
}
|
||||
|
||||
@ -591,7 +706,8 @@ void luaO_chunkid (char *out, const char *source, size_t srclen) {
|
||||
addstr(out, source, srclen); /* keep it */
|
||||
}
|
||||
else {
|
||||
if (nl != NULL) srclen = nl - source; /* stop at first newline */
|
||||
if (nl != NULL)
|
||||
srclen = ct_diff2sz(nl - source); /* stop at first newline */
|
||||
if (srclen > bufflen) srclen = bufflen;
|
||||
addstr(out, source, srclen);
|
||||
addstr(out, RETS, LL(RETS));
|
||||
|
||||
125
lobject.h
125
lobject.h
@ -188,10 +188,21 @@ typedef union {
|
||||
/* Value returned for a key not found in a table (absent key) */
|
||||
#define LUA_VABSTKEY makevariant(LUA_TNIL, 2)
|
||||
|
||||
/* Special variant to signal that a fast get is accessing a non-table */
|
||||
#define LUA_VNOTABLE makevariant(LUA_TNIL, 3)
|
||||
|
||||
|
||||
/* macro to test for (any kind of) nil */
|
||||
#define ttisnil(v) checktype((v), LUA_TNIL)
|
||||
|
||||
/*
|
||||
** Macro to test the result of a table access. Formally, it should
|
||||
** distinguish between LUA_VEMPTY/LUA_VABSTKEY/LUA_VNOTABLE and
|
||||
** other tags. As currently nil is equivalent to LUA_VEMPTY, it is
|
||||
** simpler to just test whether the value is nil.
|
||||
*/
|
||||
#define tagisempty(tag) (novariant(tag) == LUA_TNIL)
|
||||
|
||||
|
||||
/* macro to test for a standard nil */
|
||||
#define ttisstrictnil(o) checktag((o), LUA_VNIL)
|
||||
@ -245,6 +256,8 @@ typedef union {
|
||||
|
||||
|
||||
#define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
|
||||
#define tagisfalse(t) ((t) == LUA_VFALSE || novariant(t) == LUA_TNIL)
|
||||
|
||||
|
||||
|
||||
#define setbfvalue(obj) settt_(obj, LUA_VFALSE)
|
||||
@ -380,37 +393,54 @@ typedef struct GCObject {
|
||||
#define setsvalue2n setsvalue
|
||||
|
||||
|
||||
/* Kinds of long strings (stored in 'shrlen') */
|
||||
#define LSTRREG -1 /* regular long string */
|
||||
#define LSTRFIX -2 /* fixed external long string */
|
||||
#define LSTRMEM -3 /* external long string with deallocation */
|
||||
|
||||
|
||||
/*
|
||||
** Header for a string value.
|
||||
*/
|
||||
typedef struct TString {
|
||||
CommonHeader;
|
||||
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
|
||||
lu_byte shrlen; /* length for short strings */
|
||||
ls_byte shrlen; /* length for short strings, negative for long strings */
|
||||
unsigned int hash;
|
||||
union {
|
||||
size_t lnglen; /* length for long strings */
|
||||
struct TString *hnext; /* linked list for hash table */
|
||||
} u;
|
||||
char contents[1];
|
||||
char *contents; /* pointer to content in long strings */
|
||||
lua_Alloc falloc; /* deallocation function for external strings */
|
||||
void *ud; /* user data for external strings */
|
||||
} TString;
|
||||
|
||||
|
||||
#define strisshr(ts) ((ts)->shrlen >= 0)
|
||||
#define isextstr(ts) (ttislngstring(ts) && tsvalue(ts)->shrlen != LSTRREG)
|
||||
|
||||
|
||||
/*
|
||||
** Get the actual string (array of bytes) from a 'TString'.
|
||||
** Get the actual string (array of bytes) from a 'TString'. (Generic
|
||||
** version and specialized versions for long and short strings.)
|
||||
*/
|
||||
#define getstr(ts) ((ts)->contents)
|
||||
#define rawgetshrstr(ts) (cast_charp(&(ts)->contents))
|
||||
#define getshrstr(ts) check_exp(strisshr(ts), rawgetshrstr(ts))
|
||||
#define getlngstr(ts) check_exp(!strisshr(ts), (ts)->contents)
|
||||
#define getstr(ts) (strisshr(ts) ? rawgetshrstr(ts) : (ts)->contents)
|
||||
|
||||
|
||||
/* get the actual string (array of bytes) from a Lua value */
|
||||
#define svalue(o) getstr(tsvalue(o))
|
||||
/* get string length from 'TString *ts' */
|
||||
#define tsslen(ts) \
|
||||
(strisshr(ts) ? cast_sizet((ts)->shrlen) : (ts)->u.lnglen)
|
||||
|
||||
/* get string length from 'TString *s' */
|
||||
#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen)
|
||||
|
||||
/* get string length from 'TValue *o' */
|
||||
#define vslen(o) tsslen(tsvalue(o))
|
||||
/*
|
||||
** Get string and length */
|
||||
#define getlstr(ts, len) \
|
||||
(strisshr(ts) \
|
||||
? (cast_void((len) = cast_sizet((ts)->shrlen)), rawgetshrstr(ts)) \
|
||||
: (cast_void((len) = (ts)->u.lnglen), (ts)->contents))
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
@ -488,8 +518,8 @@ typedef struct Udata0 {
|
||||
|
||||
/* compute the offset of the memory area of a userdata */
|
||||
#define udatamemoffset(nuv) \
|
||||
((nuv) == 0 ? offsetof(Udata0, bindata) \
|
||||
: offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
|
||||
((nuv) == 0 ? offsetof(Udata0, bindata) \
|
||||
: offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
|
||||
|
||||
/* get the address of the memory block inside 'Udata' */
|
||||
#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))
|
||||
@ -509,6 +539,9 @@ typedef struct Udata0 {
|
||||
#define LUA_VPROTO makevariant(LUA_TPROTO, 0)
|
||||
|
||||
|
||||
typedef l_uint32 Instruction;
|
||||
|
||||
|
||||
/*
|
||||
** Description of an upvalue for function prototypes
|
||||
*/
|
||||
@ -546,13 +579,30 @@ typedef struct AbsLineInfo {
|
||||
int line;
|
||||
} AbsLineInfo;
|
||||
|
||||
|
||||
/*
|
||||
** Flags in Prototypes
|
||||
*/
|
||||
#define PF_VAHID 1 /* function has hidden vararg arguments */
|
||||
#define PF_VATAB 2 /* function has vararg table */
|
||||
#define PF_FIXED 4 /* prototype has parts in fixed memory */
|
||||
|
||||
/* a vararg function either has hidden args. or a vararg table */
|
||||
#define isvararg(p) ((p)->flag & (PF_VAHID | PF_VATAB))
|
||||
|
||||
/*
|
||||
** mark that a function needs a vararg table. (The flag PF_VAHID will
|
||||
** be cleared later.)
|
||||
*/
|
||||
#define needvatab(p) ((p)->flag |= PF_VATAB)
|
||||
|
||||
/*
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct Proto {
|
||||
CommonHeader;
|
||||
lu_byte numparams; /* number of fixed (named) parameters */
|
||||
lu_byte is_vararg;
|
||||
lu_byte flag;
|
||||
lu_byte maxstacksize; /* number of registers needed by this function */
|
||||
int sizeupvalues; /* size of 'upvalues' */
|
||||
int sizek; /* size of 'k' */
|
||||
@ -710,10 +760,9 @@ typedef union Node {
|
||||
|
||||
|
||||
/* copy a value into a key */
|
||||
#define setnodekey(L,node,obj) \
|
||||
#define setnodekey(node,obj) \
|
||||
{ Node *n_=(node); const TValue *io_=(obj); \
|
||||
n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
|
||||
checkliveness(L,io_); }
|
||||
n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; }
|
||||
|
||||
|
||||
/* copy a value from a key */
|
||||
@ -723,27 +772,14 @@ typedef union Node {
|
||||
checkliveness(L,io_); }
|
||||
|
||||
|
||||
/*
|
||||
** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the
|
||||
** real size of 'array'. Otherwise, the real size of 'array' is the
|
||||
** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'
|
||||
** is zero); 'alimit' is then used as a hint for #t.
|
||||
*/
|
||||
|
||||
#define BITRAS (1 << 7)
|
||||
#define isrealasize(t) (!((t)->flags & BITRAS))
|
||||
#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS))
|
||||
#define setnorealasize(t) ((t)->flags |= BITRAS)
|
||||
|
||||
|
||||
typedef struct Table {
|
||||
CommonHeader;
|
||||
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
||||
lu_byte lsizenode; /* log2 of size of 'node' array */
|
||||
unsigned int alimit; /* "limit" of 'array' array */
|
||||
TValue *array; /* array part */
|
||||
lu_byte lsizenode; /* log2 of number of slots of 'node' array */
|
||||
unsigned int asize; /* number of slots in 'array' array */
|
||||
Value *array; /* array part */
|
||||
Node *node;
|
||||
Node *lastfree; /* any free position is before this position */
|
||||
struct Table *metatable;
|
||||
GCObject *gclist;
|
||||
} Table;
|
||||
@ -786,24 +822,37 @@ typedef struct Table {
|
||||
** 'module' operation for hashing (size is always a power of 2)
|
||||
*/
|
||||
#define lmod(s,size) \
|
||||
(check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
|
||||
(check_exp((size&(size-1))==0, (cast_uint(s) & cast_uint((size)-1))))
|
||||
|
||||
|
||||
#define twoto(x) (1<<(x))
|
||||
#define twoto(x) (1u<<(x))
|
||||
#define sizenode(t) (twoto((t)->lsizenode))
|
||||
|
||||
|
||||
/* size of buffer for 'luaO_utf8esc' function */
|
||||
#define UTF8BUFFSZ 8
|
||||
|
||||
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
|
||||
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
|
||||
|
||||
/* macro to call 'luaO_pushvfstring' correctly */
|
||||
#define pushvfstring(L, argp, fmt, msg) \
|
||||
{ va_start(argp, fmt); \
|
||||
msg = luaO_pushvfstring(L, fmt, argp); \
|
||||
va_end(argp); \
|
||||
if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ }
|
||||
|
||||
|
||||
LUAI_FUNC int luaO_utf8esc (char *buff, l_uint32 x);
|
||||
LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x);
|
||||
LUAI_FUNC lu_byte luaO_codeparam (unsigned int p);
|
||||
LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x);
|
||||
|
||||
LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
|
||||
const TValue *p2, TValue *res);
|
||||
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
|
||||
const TValue *p2, StkId res);
|
||||
LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
|
||||
LUAI_FUNC int luaO_hexavalue (int c);
|
||||
LUAI_FUNC unsigned luaO_tostringbuff (const TValue *obj, char *buff);
|
||||
LUAI_FUNC lu_byte luaO_hexavalue (int c);
|
||||
LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
|
||||
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
|
||||
va_list argp);
|
||||
|
||||
46
lopcodes.c
46
lopcodes.c
@ -13,6 +13,10 @@
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
#define opmode(mm,ot,it,t,a,m) \
|
||||
(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
|
||||
|
||||
|
||||
/* ORDER OP */
|
||||
|
||||
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
@ -36,7 +40,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */
|
||||
,opmode(0, 0, 0, 0, 1, ivABC) /* OP_NEWTABLE */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
|
||||
@ -49,8 +53,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
|
||||
@ -64,8 +68,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI */
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */
|
||||
@ -95,10 +99,42 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */
|
||||
,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */
|
||||
,opmode(0, 0, 1, 0, 0, ivABC) /* OP_SETLIST */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
|
||||
,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETVARG */
|
||||
,opmode(0, 0, 0, 0, 0, iABx) /* OP_ERRNNIL */
|
||||
,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
|
||||
,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Check whether instruction sets top for next instruction, that is,
|
||||
** it results in multiple values.
|
||||
*/
|
||||
int luaP_isOT (Instruction i) {
|
||||
OpCode op = GET_OPCODE(i);
|
||||
switch (op) {
|
||||
case OP_TAILCALL: return 1;
|
||||
default:
|
||||
return testOTMode(op) && GETARG_C(i) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Check whether instruction uses top from previous instruction, that is,
|
||||
** it accepts multiple results.
|
||||
*/
|
||||
int luaP_isIT (Instruction i) {
|
||||
OpCode op = GET_OPCODE(i);
|
||||
switch (op) {
|
||||
case OP_SETLIST:
|
||||
return testITMode(GET_OPCODE(i)) && GETARG_vB(i) == 0;
|
||||
default:
|
||||
return testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
164
lopcodes.h
164
lopcodes.h
@ -8,6 +8,7 @@
|
||||
#define lopcodes_h
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
/*===========================================================================
|
||||
@ -18,25 +19,30 @@
|
||||
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
|
||||
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
|
||||
iABC C(8) | B(8) |k| A(8) | Op(7) |
|
||||
ivABC vC(10) | vB(6) |k| A(8) | Op(7) |
|
||||
iABx Bx(17) | A(8) | Op(7) |
|
||||
iAsBx sBx (signed)(17) | A(8) | Op(7) |
|
||||
iAx Ax(25) | Op(7) |
|
||||
isJ sJ (signed)(25) | Op(7) |
|
||||
|
||||
A signed argument is represented in excess K: the represented value is
|
||||
the written unsigned value minus K, where K is half the maximum for the
|
||||
corresponding unsigned argument.
|
||||
('v' stands for "variant", 's' for "signed", 'x' for "extended".)
|
||||
A signed argument is represented in excess K: The represented value is
|
||||
the written unsigned value minus K, where K is half (rounded down) the
|
||||
maximum value for the corresponding unsigned argument.
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
/* basic instruction formats */
|
||||
enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ};
|
||||
|
||||
|
||||
/*
|
||||
** size and position of opcode arguments.
|
||||
*/
|
||||
#define SIZE_C 8
|
||||
#define SIZE_vC 10
|
||||
#define SIZE_B 8
|
||||
#define SIZE_vB 6
|
||||
#define SIZE_Bx (SIZE_C + SIZE_B + 1)
|
||||
#define SIZE_A 8
|
||||
#define SIZE_Ax (SIZE_Bx + SIZE_A)
|
||||
@ -49,7 +55,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
#define POS_A (POS_OP + SIZE_OP)
|
||||
#define POS_k (POS_A + SIZE_A)
|
||||
#define POS_B (POS_k + 1)
|
||||
#define POS_vB (POS_k + 1)
|
||||
#define POS_C (POS_B + SIZE_B)
|
||||
#define POS_vC (POS_vB + SIZE_vB)
|
||||
|
||||
#define POS_Bx POS_k
|
||||
|
||||
@ -64,14 +72,17 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
** so they must fit in ints.
|
||||
*/
|
||||
|
||||
/* Check whether type 'int' has at least 'b' bits ('b' < 32) */
|
||||
#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1)
|
||||
/*
|
||||
** Check whether type 'int' has at least 'b' + 1 bits.
|
||||
** 'b' < 32; +1 for the sign bit.
|
||||
*/
|
||||
#define L_INTHASBITS(b) ((UINT_MAX >> (b)) >= 1)
|
||||
|
||||
|
||||
#if L_INTHASBITS(SIZE_Bx)
|
||||
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
||||
#else
|
||||
#define MAXARG_Bx MAX_INT
|
||||
#define MAXARG_Bx INT_MAX
|
||||
#endif
|
||||
|
||||
#define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
|
||||
@ -80,13 +91,13 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
#if L_INTHASBITS(SIZE_Ax)
|
||||
#define MAXARG_Ax ((1<<SIZE_Ax)-1)
|
||||
#else
|
||||
#define MAXARG_Ax MAX_INT
|
||||
#define MAXARG_Ax INT_MAX
|
||||
#endif
|
||||
|
||||
#if L_INTHASBITS(SIZE_sJ)
|
||||
#define MAXARG_sJ ((1 << SIZE_sJ) - 1)
|
||||
#else
|
||||
#define MAXARG_sJ MAX_INT
|
||||
#define MAXARG_sJ INT_MAX
|
||||
#endif
|
||||
|
||||
#define OFFSET_sJ (MAXARG_sJ >> 1)
|
||||
@ -94,7 +105,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
|
||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||
#define MAXARG_vB ((1<<SIZE_vB)-1)
|
||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||
#define MAXARG_vC ((1<<SIZE_vC)-1)
|
||||
#define OFFSET_sC (MAXARG_C >> 1)
|
||||
|
||||
#define int2sC(i) ((i) + OFFSET_sC)
|
||||
@ -113,28 +126,36 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
|
||||
#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
|
||||
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
|
||||
((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
|
||||
((cast_Inst(o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
|
||||
|
||||
#define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
|
||||
|
||||
|
||||
#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))
|
||||
#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
|
||||
((cast(Instruction, v)<<pos)&MASK1(size,pos))))
|
||||
((cast_Inst(v)<<pos)&MASK1(size,pos))))
|
||||
|
||||
#define GETARG_A(i) getarg(i, POS_A, SIZE_A)
|
||||
#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
|
||||
|
||||
#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
|
||||
#define GETARG_B(i) \
|
||||
check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
|
||||
#define GETARG_vB(i) \
|
||||
check_exp(checkopm(i, ivABC), getarg(i, POS_vB, SIZE_vB))
|
||||
#define GETARG_sB(i) sC2int(GETARG_B(i))
|
||||
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
|
||||
#define SETARG_vB(i,v) setarg(i, v, POS_vB, SIZE_vB)
|
||||
|
||||
#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
|
||||
#define GETARG_C(i) \
|
||||
check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
|
||||
#define GETARG_vC(i) \
|
||||
check_exp(checkopm(i, ivABC), getarg(i, POS_vC, SIZE_vC))
|
||||
#define GETARG_sC(i) sC2int(GETARG_C(i))
|
||||
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
|
||||
#define SETARG_vC(i,v) setarg(i, v, POS_vC, SIZE_vC)
|
||||
|
||||
#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))
|
||||
#define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
|
||||
#define TESTARG_k(i) (cast_int(((i) & (1u << POS_k))))
|
||||
#define GETARG_k(i) getarg(i, POS_k, 1)
|
||||
#define SETARG_k(i,v) setarg(i, v, POS_k, 1)
|
||||
|
||||
#define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
|
||||
@ -153,22 +174,28 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
|
||||
|
||||
|
||||
#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \
|
||||
| (cast(Instruction, a)<<POS_A) \
|
||||
| (cast(Instruction, b)<<POS_B) \
|
||||
| (cast(Instruction, c)<<POS_C) \
|
||||
| (cast(Instruction, k)<<POS_k))
|
||||
#define CREATE_ABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
|
||||
| (cast_Inst(a)<<POS_A) \
|
||||
| (cast_Inst(b)<<POS_B) \
|
||||
| (cast_Inst(c)<<POS_C) \
|
||||
| (cast_Inst(k)<<POS_k))
|
||||
|
||||
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
|
||||
| (cast(Instruction, a)<<POS_A) \
|
||||
| (cast(Instruction, bc)<<POS_Bx))
|
||||
#define CREATE_vABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
|
||||
| (cast_Inst(a)<<POS_A) \
|
||||
| (cast_Inst(b)<<POS_vB) \
|
||||
| (cast_Inst(c)<<POS_vC) \
|
||||
| (cast_Inst(k)<<POS_k))
|
||||
|
||||
#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
|
||||
| (cast(Instruction, a)<<POS_Ax))
|
||||
#define CREATE_ABx(o,a,bc) ((cast_Inst(o)<<POS_OP) \
|
||||
| (cast_Inst(a)<<POS_A) \
|
||||
| (cast_Inst(bc)<<POS_Bx))
|
||||
|
||||
#define CREATE_sJ(o,j,k) ((cast(Instruction, o) << POS_OP) \
|
||||
| (cast(Instruction, j) << POS_sJ) \
|
||||
| (cast(Instruction, k) << POS_k))
|
||||
#define CREATE_Ax(o,a) ((cast_Inst(o)<<POS_OP) \
|
||||
| (cast_Inst(a)<<POS_Ax))
|
||||
|
||||
#define CREATE_sJ(o,j,k) ((cast_Inst(o) << POS_OP) \
|
||||
| (cast_Inst(j) << POS_sJ) \
|
||||
| (cast_Inst(k) << POS_k))
|
||||
|
||||
|
||||
#if !defined(MAXINDEXRK) /* (for debugging only) */
|
||||
@ -177,9 +204,16 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
|
||||
|
||||
/*
|
||||
** invalid register that fits in 8 bits
|
||||
** Maximum size for the stack of a Lua function. It must fit in 8 bits.
|
||||
** The highest valid register is one less than this value.
|
||||
*/
|
||||
#define NO_REG MAXARG_A
|
||||
#define MAX_FSTACK MAXARG_A
|
||||
|
||||
/*
|
||||
** Invalid register (one more than last valid register).
|
||||
*/
|
||||
#define NO_REG MAX_FSTACK
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@ -190,8 +224,8 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
|
||||
|
||||
/*
|
||||
** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
|
||||
** has extra descriptions in the notes after the enumeration.
|
||||
** Grep "ORDER OP" if you change this enum.
|
||||
** See "Notes" below for more information about some instructions.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
@ -204,25 +238,25 @@ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
|
||||
OP_LOADK,/* A Bx R[A] := K[Bx] */
|
||||
OP_LOADKX,/* A R[A] := K[extra arg] */
|
||||
OP_LOADFALSE,/* A R[A] := false */
|
||||
OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */
|
||||
OP_LFALSESKIP,/*A R[A] := false; pc++ */
|
||||
OP_LOADTRUE,/* A R[A] := true */
|
||||
OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */
|
||||
OP_GETUPVAL,/* A B R[A] := UpValue[B] */
|
||||
OP_SETUPVAL,/* A B UpValue[B] := R[A] */
|
||||
|
||||
OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:string] */
|
||||
OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */
|
||||
OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */
|
||||
OP_GETI,/* A B C R[A] := R[B][C] */
|
||||
OP_GETFIELD,/* A B C R[A] := R[B][K[C]:string] */
|
||||
OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */
|
||||
|
||||
OP_SETTABUP,/* A B C UpValue[A][K[B]:string] := RK(C) */
|
||||
OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */
|
||||
OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
|
||||
OP_SETI,/* A B C R[A][B] := RK(C) */
|
||||
OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */
|
||||
OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */
|
||||
|
||||
OP_NEWTABLE,/* A B C k R[A] := {} */
|
||||
OP_NEWTABLE,/* A vB vC k R[A] := {} */
|
||||
|
||||
OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
|
||||
OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][K[C]:shortstring] */
|
||||
|
||||
OP_ADDI,/* A B sC R[A] := R[B] + sC */
|
||||
|
||||
@ -238,8 +272,8 @@ OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */
|
||||
OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */
|
||||
OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */
|
||||
|
||||
OP_SHRI,/* A B sC R[A] := R[B] >> sC */
|
||||
OP_SHLI,/* A B sC R[A] := sC << R[B] */
|
||||
OP_SHRI,/* A B sC R[A] := R[B] >> sC */
|
||||
|
||||
OP_ADD,/* A B C R[A] := R[B] + R[C] */
|
||||
OP_SUB,/* A B C R[A] := R[B] - R[C] */
|
||||
@ -255,7 +289,7 @@ OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
|
||||
OP_SHL,/* A B C R[A] := R[B] << R[C] */
|
||||
OP_SHR,/* A B C R[A] := R[B] >> R[C] */
|
||||
|
||||
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */
|
||||
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */
|
||||
OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
|
||||
OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
|
||||
|
||||
@ -281,12 +315,12 @@ OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
|
||||
OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
|
||||
|
||||
OP_TEST,/* A k if (not R[A] == k) then pc++ */
|
||||
OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */
|
||||
OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
|
||||
|
||||
OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
|
||||
OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
|
||||
|
||||
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */
|
||||
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] */
|
||||
OP_RETURN0,/* return */
|
||||
OP_RETURN1,/* A return R[A] */
|
||||
|
||||
@ -298,13 +332,17 @@ OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */
|
||||
OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
|
||||
OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
|
||||
|
||||
OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */
|
||||
OP_SETLIST,/* A vB vC k R[A][vC+i] := R[A+i], 1 <= i <= vB */
|
||||
|
||||
OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
|
||||
|
||||
OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
|
||||
OP_VARARG,/* A B C k R[A], ..., R[A+C-2] = varargs */
|
||||
|
||||
OP_VARARGPREP,/*A (adjust vararg parameters) */
|
||||
OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */
|
||||
|
||||
OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx - 1] is global name)*/
|
||||
|
||||
OP_VARARGPREP,/* (adjust varargs) */
|
||||
|
||||
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
} OpCode;
|
||||
@ -333,7 +371,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
OP_RETURN*, OP_SETLIST) may use 'top'.
|
||||
|
||||
(*) In OP_VARARG, if (C == 0) then use actual number of varargs and
|
||||
set top (like in OP_CALL with C == 0).
|
||||
set top (like in OP_CALL with C == 0). 'k' means function has a
|
||||
vararg table, which is in R[B].
|
||||
|
||||
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
||||
|
||||
@ -344,22 +383,27 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
|
||||
bits of C).
|
||||
|
||||
(*) In OP_NEWTABLE, B is log2 of the hash size (which is always a
|
||||
(*) In OP_NEWTABLE, vB is log2 of the hash size (which is always a
|
||||
power of 2) plus 1, or zero for size zero. If not k, the array size
|
||||
is C. Otherwise, the array size is EXTRAARG _ C.
|
||||
is vC. Otherwise, the array size is EXTRAARG _ vC.
|
||||
|
||||
(*) In OP_ERRNNIL, (Bx == 0) means index of global name doesn't
|
||||
fit in Bx. (So, that name is not available for the error message.)
|
||||
|
||||
(*) For comparisons, k specifies what condition the test should accept
|
||||
(true or false).
|
||||
|
||||
(*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
|
||||
(the constant is the first operand).
|
||||
(the constant is the first operand).
|
||||
|
||||
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
||||
(*) All comparison and test instructions assume that the instruction
|
||||
being skipped (pc++) is a jump.
|
||||
|
||||
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
|
||||
function builds upvalues, which may need to be closed. C > 0 means
|
||||
the function is vararg, so that its 'func' must be corrected before
|
||||
returning; in this case, (C - 1) is its number of fixed parameters.
|
||||
the function has hidden vararg arguments, so that its 'func' must be
|
||||
corrected before returning; in this case, (C - 1) is its number of
|
||||
fixed parameters.
|
||||
|
||||
(*) In comparisons with an immediate operand, C signals whether the
|
||||
original operand was a float. (It must be corrected in case of
|
||||
@ -387,19 +431,9 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
||||
#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
|
||||
#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
|
||||
|
||||
/* "out top" (set top for next instruction) */
|
||||
#define isOT(i) \
|
||||
((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \
|
||||
GET_OPCODE(i) == OP_TAILCALL)
|
||||
|
||||
/* "in top" (uses top from previous instruction) */
|
||||
#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
|
||||
LUAI_FUNC int luaP_isOT (Instruction i);
|
||||
LUAI_FUNC int luaP_isIT (Instruction i);
|
||||
|
||||
#define opmode(mm,ot,it,t,a,m) \
|
||||
(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
|
||||
|
||||
|
||||
/* number of list items to accumulate before a SETLIST instruction */
|
||||
#define LFIELDS_PER_FLUSH 50
|
||||
|
||||
#endif
|
||||
|
||||
@ -45,8 +45,8 @@ static const char *const opnames[] = {
|
||||
"BANDK",
|
||||
"BORK",
|
||||
"BXORK",
|
||||
"SHRI",
|
||||
"SHLI",
|
||||
"SHRI",
|
||||
"ADD",
|
||||
"SUB",
|
||||
"MUL",
|
||||
@ -94,6 +94,8 @@ static const char *const opnames[] = {
|
||||
"SETLIST",
|
||||
"CLOSURE",
|
||||
"VARARG",
|
||||
"GETVARG",
|
||||
"ERRNNIL",
|
||||
"VARARGPREP",
|
||||
"EXTRAARG",
|
||||
NULL
|
||||
|
||||
12
loslib.c
12
loslib.c
@ -20,6 +20,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -33,7 +34,7 @@
|
||||
#if defined(LUA_USE_WINDOWS)
|
||||
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \
|
||||
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
|
||||
#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */
|
||||
#elif defined(LUA_USE_C89) /* C89 (only 1-char options) */
|
||||
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%"
|
||||
#else /* C99 specification */
|
||||
#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
|
||||
@ -155,6 +156,7 @@ static int os_execute (lua_State *L) {
|
||||
|
||||
static int os_remove (lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
errno = 0;
|
||||
return luaL_fileresult(L, remove(filename) == 0, filename);
|
||||
}
|
||||
|
||||
@ -162,6 +164,7 @@ static int os_remove (lua_State *L) {
|
||||
static int os_rename (lua_State *L) {
|
||||
const char *fromname = luaL_checkstring(L, 1);
|
||||
const char *toname = luaL_checkstring(L, 2);
|
||||
errno = 0;
|
||||
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
||||
}
|
||||
|
||||
@ -270,9 +273,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
|
||||
|
||||
|
||||
static const char *checkoption (lua_State *L, const char *conv,
|
||||
ptrdiff_t convlen, char *buff) {
|
||||
size_t convlen, char *buff) {
|
||||
const char *option = LUA_STRFTIMEOPTIONS;
|
||||
int oplen = 1; /* length of options being checked */
|
||||
unsigned oplen = 1; /* length of options being checked */
|
||||
for (; *option != '\0' && oplen <= convlen; option += oplen) {
|
||||
if (*option == '|') /* next block? */
|
||||
oplen++; /* will check options with next length (+1) */
|
||||
@ -330,7 +333,8 @@ static int os_date (lua_State *L) {
|
||||
size_t reslen;
|
||||
char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
|
||||
s++; /* skip '%' */
|
||||
s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
|
||||
/* copy specifier to 'cc' */
|
||||
s = checkoption(L, s, ct_diff2sz(se - s), cc + 1);
|
||||
reslen = strftime(buff, SIZETIMEFMT, cc, stm);
|
||||
luaL_addsize(&b, reslen);
|
||||
}
|
||||
|
||||
57
lparser.h
57
lparser.h
@ -32,26 +32,36 @@ typedef enum {
|
||||
VKFLT, /* floating constant; nval = numerical float value */
|
||||
VKINT, /* integer constant; ival = numerical integer value */
|
||||
VKSTR, /* string constant; strval = TString address;
|
||||
(string is fixed by the lexer) */
|
||||
(string is fixed by the scanner) */
|
||||
VNONRELOC, /* expression has its value in a fixed register;
|
||||
info = result register */
|
||||
VLOCAL, /* local variable; var.ridx = register index;
|
||||
var.vidx = relative index in 'actvar.arr' */
|
||||
VVARGVAR, /* vararg parameter; var.ridx = register index;
|
||||
var.vidx = relative index in 'actvar.arr' */
|
||||
VGLOBAL, /* global variable;
|
||||
info = relative index in 'actvar.arr' (or -1 for
|
||||
implicit declaration) */
|
||||
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
|
||||
VCONST, /* compile-time <const> variable;
|
||||
info = absolute index in 'actvar.arr' */
|
||||
VINDEXED, /* indexed variable;
|
||||
ind.t = table register;
|
||||
ind.idx = key's R index */
|
||||
ind.idx = key's R index;
|
||||
ind.ro = true if it represents a read-only global;
|
||||
ind.keystr = if key is a string, index in 'k' of that string;
|
||||
-1 if key is not a string */
|
||||
VVARGIND, /* indexed vararg parameter;
|
||||
ind.* as in VINDEXED */
|
||||
VINDEXUP, /* indexed upvalue;
|
||||
ind.t = table upvalue;
|
||||
ind.idx = key's K index */
|
||||
ind.idx = key's K index;
|
||||
ind.* as in VINDEXED */
|
||||
VINDEXI, /* indexed variable with constant integer;
|
||||
ind.t = table register;
|
||||
ind.idx = key's value */
|
||||
VINDEXSTR, /* indexed variable with literal string;
|
||||
ind.t = table register;
|
||||
ind.idx = key's K index */
|
||||
ind.idx = key's K index;
|
||||
ind.* as in VINDEXED */
|
||||
VJMP, /* expression is a test/comparison;
|
||||
info = pc of corresponding jump instruction */
|
||||
VRELOC, /* expression can put result in any register;
|
||||
@ -75,10 +85,12 @@ typedef struct expdesc {
|
||||
struct { /* for indexed variables */
|
||||
short idx; /* index (R or "long" K) */
|
||||
lu_byte t; /* table (register or upvalue) */
|
||||
lu_byte ro; /* true if variable is read-only */
|
||||
int keystr; /* index in 'k' of string key, or -1 if not a string */
|
||||
} ind;
|
||||
struct { /* for local variables */
|
||||
lu_byte ridx; /* register holding the variable */
|
||||
unsigned short vidx; /* compiler index (in 'actvar.arr') */
|
||||
short vidx; /* index in 'actvar.arr' */
|
||||
} var;
|
||||
} u;
|
||||
int t; /* patch list of 'exit when true' */
|
||||
@ -87,12 +99,22 @@ typedef struct expdesc {
|
||||
|
||||
|
||||
/* kinds of variables */
|
||||
#define VDKREG 0 /* regular */
|
||||
#define RDKCONST 1 /* constant */
|
||||
#define RDKTOCLOSE 2 /* to-be-closed */
|
||||
#define RDKCTC 3 /* compile-time constant */
|
||||
#define VDKREG 0 /* regular local */
|
||||
#define RDKCONST 1 /* local constant */
|
||||
#define RDKVAVAR 2 /* vararg parameter */
|
||||
#define RDKTOCLOSE 3 /* to-be-closed */
|
||||
#define RDKCTC 4 /* local compile-time constant */
|
||||
#define GDKREG 5 /* regular global */
|
||||
#define GDKCONST 6 /* global constant */
|
||||
|
||||
/* description of an active local variable */
|
||||
/* variables that live in registers */
|
||||
#define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE)
|
||||
|
||||
/* test for global variables */
|
||||
#define varglobal(v) ((v)->vd.kind >= GDKREG)
|
||||
|
||||
|
||||
/* description of an active variable */
|
||||
typedef union Vardesc {
|
||||
struct {
|
||||
TValuefields; /* constant value (if it is a compile-time constant) */
|
||||
@ -111,8 +133,8 @@ typedef struct Labeldesc {
|
||||
TString *name; /* label identifier */
|
||||
int pc; /* position in code */
|
||||
int line; /* line where it appeared */
|
||||
lu_byte nactvar; /* number of active variables in that position */
|
||||
lu_byte close; /* goto that escapes upvalues */
|
||||
short nactvar; /* number of active variables in that position */
|
||||
lu_byte close; /* true for goto that escapes upvalues */
|
||||
} Labeldesc;
|
||||
|
||||
|
||||
@ -146,6 +168,7 @@ typedef struct FuncState {
|
||||
struct FuncState *prev; /* enclosing function */
|
||||
struct LexState *ls; /* lexical state */
|
||||
struct BlockCnt *bl; /* chain of current blocks */
|
||||
Table *kcache; /* cache for reusing constants */
|
||||
int pc; /* next position to code (equivalent to 'ncode') */
|
||||
int lasttarget; /* 'label' of last 'jump label' */
|
||||
int previousline; /* last line that was saved in 'lineinfo' */
|
||||
@ -155,7 +178,7 @@ typedef struct FuncState {
|
||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||
int firstlabel; /* index of first label (in 'dyd->label->arr') */
|
||||
short ndebugvars; /* number of elements in 'f->locvars' */
|
||||
lu_byte nactvar; /* number of active local variables */
|
||||
short nactvar; /* number of active variable declarations */
|
||||
lu_byte nups; /* number of upvalues */
|
||||
lu_byte freereg; /* first free register */
|
||||
lu_byte iwthabs; /* instructions issued since last absolute line info */
|
||||
@ -163,7 +186,9 @@ typedef struct FuncState {
|
||||
} FuncState;
|
||||
|
||||
|
||||
LUAI_FUNC int luaY_nvarstack (FuncState *fs);
|
||||
LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs);
|
||||
LUAI_FUNC void luaY_checklimit (FuncState *fs, int v, int l,
|
||||
const char *what);
|
||||
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Dyndata *dyd, const char *name, int firstchar);
|
||||
|
||||
|
||||
210
lstate.c
210
lstate.c
@ -29,87 +29,58 @@
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** thread state + extra space
|
||||
*/
|
||||
typedef struct LX {
|
||||
lu_byte extra_[LUA_EXTRASPACE];
|
||||
lua_State l;
|
||||
} LX;
|
||||
|
||||
|
||||
/*
|
||||
** Main thread combines a thread state and the global state
|
||||
*/
|
||||
typedef struct LG {
|
||||
LX l;
|
||||
global_State g;
|
||||
} LG;
|
||||
|
||||
|
||||
|
||||
#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
|
||||
|
||||
|
||||
/*
|
||||
** A macro to create a "random" seed when a state is created;
|
||||
** the seed is used to randomize string hashes.
|
||||
** these macros allow user-specific actions when a thread is
|
||||
** created/deleted
|
||||
*/
|
||||
#if !defined(luai_makeseed)
|
||||
#if !defined(luai_userstateopen)
|
||||
#define luai_userstateopen(L) ((void)L)
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
#if !defined(luai_userstateclose)
|
||||
#define luai_userstateclose(L) ((void)L)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** Compute an initial seed with some level of randomness.
|
||||
** Rely on Address Space Layout Randomization (if present) and
|
||||
** current time.
|
||||
*/
|
||||
#define addbuff(b,p,e) \
|
||||
{ size_t t = cast_sizet(e); \
|
||||
memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
|
||||
|
||||
static unsigned int luai_makeseed (lua_State *L) {
|
||||
char buff[3 * sizeof(size_t)];
|
||||
unsigned int h = cast_uint(time(NULL));
|
||||
int p = 0;
|
||||
addbuff(buff, p, L); /* heap variable */
|
||||
addbuff(buff, p, &h); /* local variable */
|
||||
addbuff(buff, p, &lua_newstate); /* public function */
|
||||
lua_assert(p == sizeof(buff));
|
||||
return luaS_hash(buff, p, h);
|
||||
}
|
||||
#if !defined(luai_userstatethread)
|
||||
#define luai_userstatethread(L,L1) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstatefree)
|
||||
#define luai_userstatefree(L,L1) ((void)L)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
|
||||
** invariant (and avoiding underflows in 'totalbytes')
|
||||
** set GCdebt to a new value keeping the real number of allocated
|
||||
** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in
|
||||
** 'GCtotalobjs'.
|
||||
*/
|
||||
void luaE_setdebt (global_State *g, l_mem debt) {
|
||||
l_mem tb = gettotalbytes(g);
|
||||
lua_assert(tb > 0);
|
||||
if (debt < tb - MAX_LMEM)
|
||||
debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
|
||||
g->totalbytes = tb - debt;
|
||||
if (debt > MAX_LMEM - tb)
|
||||
debt = MAX_LMEM - tb; /* will make GCtotalbytes == MAX_LMEM */
|
||||
g->GCtotalbytes = tb + debt;
|
||||
g->GCdebt = debt;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
|
||||
UNUSED(L); UNUSED(limit);
|
||||
return LUAI_MAXCCALLS; /* warning?? */
|
||||
}
|
||||
|
||||
|
||||
CallInfo *luaE_extendCI (lua_State *L) {
|
||||
CallInfo *luaE_extendCI (lua_State *L, int err) {
|
||||
CallInfo *ci;
|
||||
lua_assert(L->ci->next == NULL);
|
||||
ci = luaM_new(L, CallInfo);
|
||||
lua_assert(L->ci->next == NULL);
|
||||
L->ci->next = ci;
|
||||
ci = luaM_reallocvector(L, NULL, 0, 1, CallInfo);
|
||||
if (l_unlikely(ci == NULL)) { /* allocation failed? */
|
||||
if (err)
|
||||
luaM_error(L); /* raise the error */
|
||||
return NULL; /* else only report it */
|
||||
}
|
||||
ci->next = L->ci->next;
|
||||
ci->previous = L->ci;
|
||||
ci->next = NULL;
|
||||
L->ci->next = ci;
|
||||
if (ci->next)
|
||||
ci->next->previous = ci;
|
||||
ci->u.l.trap = 0;
|
||||
L->nci++;
|
||||
return ci;
|
||||
@ -119,7 +90,7 @@ CallInfo *luaE_extendCI (lua_State *L) {
|
||||
/*
|
||||
** free all CallInfo structures not in use by a thread
|
||||
*/
|
||||
void luaE_freeCI (lua_State *L) {
|
||||
static void freeCI (lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
CallInfo *next = ci->next;
|
||||
ci->next = NULL;
|
||||
@ -166,7 +137,7 @@ void luaE_checkcstack (lua_State *L) {
|
||||
if (getCcalls(L) == LUAI_MAXCCALLS)
|
||||
luaG_runerror(L, "C stack overflow");
|
||||
else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
|
||||
luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
|
||||
luaD_errerr(L); /* error while handling stack error */
|
||||
}
|
||||
|
||||
|
||||
@ -177,26 +148,29 @@ LUAI_FUNC void luaE_incCstack (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static void resetCI (lua_State *L) {
|
||||
CallInfo *ci = L->ci = &L->base_ci;
|
||||
ci->func.p = L->stack.p;
|
||||
setnilvalue(s2v(ci->func.p)); /* 'function' entry for basic 'ci' */
|
||||
ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */
|
||||
ci->u.c.k = NULL;
|
||||
ci->callstatus = CIST_C;
|
||||
L->status = LUA_OK;
|
||||
L->errfunc = 0; /* stack unwind can "throw away" the error function */
|
||||
}
|
||||
|
||||
|
||||
static void stack_init (lua_State *L1, lua_State *L) {
|
||||
int i; CallInfo *ci;
|
||||
int i;
|
||||
/* initialize stack array */
|
||||
L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
|
||||
L1->tbclist.p = L1->stack.p;
|
||||
for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
|
||||
setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */
|
||||
L1->top.p = L1->stack.p;
|
||||
L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
|
||||
/* initialize first ci */
|
||||
ci = &L1->base_ci;
|
||||
ci->next = ci->previous = NULL;
|
||||
ci->callstatus = CIST_C;
|
||||
ci->func.p = L1->top.p;
|
||||
ci->u.c.k = NULL;
|
||||
ci->nresults = 0;
|
||||
setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */
|
||||
L1->top.p++;
|
||||
ci->top.p = L1->top.p + LUA_MINSTACK;
|
||||
L1->ci = ci;
|
||||
resetCI(L1);
|
||||
L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */
|
||||
}
|
||||
|
||||
|
||||
@ -204,9 +178,10 @@ static void freestack (lua_State *L) {
|
||||
if (L->stack.p == NULL)
|
||||
return; /* stack not completely built yet */
|
||||
L->ci = &L->base_ci; /* free the entire 'ci' list */
|
||||
luaE_freeCI(L);
|
||||
freeCI(L);
|
||||
lua_assert(L->nci == 0);
|
||||
luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */
|
||||
/* free stack */
|
||||
luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK));
|
||||
}
|
||||
|
||||
|
||||
@ -215,13 +190,19 @@ static void freestack (lua_State *L) {
|
||||
*/
|
||||
static void init_registry (lua_State *L, global_State *g) {
|
||||
/* create registry */
|
||||
TValue aux;
|
||||
Table *registry = luaH_new(L);
|
||||
sethvalue(L, &g->l_registry, registry);
|
||||
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
||||
/* registry[1] = false */
|
||||
setbfvalue(&aux);
|
||||
luaH_setint(L, registry, 1, &aux);
|
||||
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
||||
setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L);
|
||||
setthvalue(L, &aux, L);
|
||||
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux);
|
||||
/* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
|
||||
sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
|
||||
sethvalue(L, &aux, luaH_new(L));
|
||||
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux);
|
||||
}
|
||||
|
||||
|
||||
@ -263,6 +244,16 @@ static void preinit_thread (lua_State *L, global_State *g) {
|
||||
L->status = LUA_OK;
|
||||
L->errfunc = 0;
|
||||
L->oldpc = 0;
|
||||
L->base_ci.previous = L->base_ci.next = NULL;
|
||||
}
|
||||
|
||||
|
||||
lu_mem luaE_threadsize (lua_State *L) {
|
||||
lu_mem sz = cast(lu_mem, sizeof(LX))
|
||||
+ cast_uint(L->nci) * sizeof(CallInfo);
|
||||
if (L->stack.p != NULL)
|
||||
sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue);
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
@ -271,15 +262,16 @@ static void close_state (lua_State *L) {
|
||||
if (!completestate(g)) /* closing a partially built state? */
|
||||
luaC_freeallobjects(L); /* just collect its objects */
|
||||
else { /* closing a fully built state */
|
||||
L->ci = &L->base_ci; /* unwind CallInfo list */
|
||||
resetCI(L);
|
||||
luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
|
||||
L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */
|
||||
luaC_freeallobjects(L); /* collect all objects */
|
||||
luai_userstateclose(L);
|
||||
}
|
||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
|
||||
luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
|
||||
freestack(L);
|
||||
lua_assert(gettotalbytes(g) == sizeof(LG));
|
||||
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
||||
lua_assert(gettotalbytes(g) == sizeof(global_State));
|
||||
(*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */
|
||||
}
|
||||
|
||||
|
||||
@ -301,7 +293,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
|
||||
L1->hook = L->hook;
|
||||
resethookcount(L1);
|
||||
/* initialize L1 extra space */
|
||||
memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
|
||||
memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)),
|
||||
LUA_EXTRASPACE);
|
||||
luai_userstatethread(L, L1);
|
||||
stack_init(L1, L); /* init stack */
|
||||
@ -320,43 +312,39 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
|
||||
}
|
||||
|
||||
|
||||
int luaE_resetthread (lua_State *L, int status) {
|
||||
CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */
|
||||
setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */
|
||||
ci->func.p = L->stack.p;
|
||||
ci->callstatus = CIST_C;
|
||||
TStatus luaE_resetthread (lua_State *L, TStatus status) {
|
||||
resetCI(L);
|
||||
if (status == LUA_YIELD)
|
||||
status = LUA_OK;
|
||||
L->status = LUA_OK; /* so it can run __close metamethods */
|
||||
status = luaD_closeprotected(L, 1, status);
|
||||
if (status != LUA_OK) /* errors? */
|
||||
luaD_seterrorobj(L, status, L->stack.p + 1);
|
||||
else
|
||||
L->top.p = L->stack.p + 1;
|
||||
ci->top.p = L->top.p + LUA_MINSTACK;
|
||||
luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0);
|
||||
luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_resetthread (lua_State *L, lua_State *from) {
|
||||
int status;
|
||||
LUA_API int lua_closethread (lua_State *L, lua_State *from) {
|
||||
TStatus status;
|
||||
lua_lock(L);
|
||||
L->nCcalls = (from) ? getCcalls(from) : 0;
|
||||
status = luaE_resetthread(L, L->status);
|
||||
if (L == from) /* closing itself? */
|
||||
luaD_throwbaselevel(L, status);
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
return APIstatus(status);
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
|
||||
int i;
|
||||
lua_State *L;
|
||||
global_State *g;
|
||||
LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
|
||||
if (l == NULL) return NULL;
|
||||
L = &l->l.l;
|
||||
g = &l->g;
|
||||
global_State *g = cast(global_State*,
|
||||
(*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State)));
|
||||
if (g == NULL) return NULL;
|
||||
L = &g->mainth.l;
|
||||
L->tt = LUA_VTHREAD;
|
||||
g->currentwhite = bitmask(WHITE0BIT);
|
||||
L->marked = luaC_white(g);
|
||||
@ -368,8 +356,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
g->ud = ud;
|
||||
g->warnf = NULL;
|
||||
g->ud_warn = NULL;
|
||||
g->mainthread = L;
|
||||
g->seed = luai_makeseed(L);
|
||||
g->seed = seed;
|
||||
g->gcstp = GCSTPGC; /* no GC while building state */
|
||||
g->strt.size = g->strt.nuse = 0;
|
||||
g->strt.hash = NULL;
|
||||
@ -386,16 +373,17 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
g->gray = g->grayagain = NULL;
|
||||
g->weak = g->ephemeron = g->allweak = NULL;
|
||||
g->twups = NULL;
|
||||
g->totalbytes = sizeof(LG);
|
||||
g->GCtotalbytes = sizeof(global_State);
|
||||
g->GCmarked = 0;
|
||||
g->GCdebt = 0;
|
||||
g->lastatomic = 0;
|
||||
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
|
||||
setgcparam(g->gcpause, LUAI_GCPAUSE);
|
||||
setgcparam(g->gcstepmul, LUAI_GCMUL);
|
||||
g->gcstepsize = LUAI_GCSTEPSIZE;
|
||||
setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
|
||||
g->genminormul = LUAI_GENMINORMUL;
|
||||
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
|
||||
setgcparam(g, PAUSE, LUAI_GCPAUSE);
|
||||
setgcparam(g, STEPMUL, LUAI_GCMUL);
|
||||
setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE);
|
||||
setgcparam(g, MINORMUL, LUAI_GENMINORMUL);
|
||||
setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR);
|
||||
setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR);
|
||||
for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL;
|
||||
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
|
||||
/* memory allocation error: free partial state */
|
||||
close_state(L);
|
||||
@ -407,7 +395,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
|
||||
LUA_API void lua_close (lua_State *L) {
|
||||
lua_lock(L);
|
||||
L = G(L)->mainthread; /* only the main thread can be closed */
|
||||
L = mainthread(G(L)); /* only the main thread can be closed */
|
||||
close_state(L);
|
||||
}
|
||||
|
||||
@ -425,7 +413,7 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) {
|
||||
void luaE_warnerror (lua_State *L, const char *where) {
|
||||
TValue *errobj = s2v(L->top.p - 1); /* error object */
|
||||
const char *msg = (ttisstring(errobj))
|
||||
? svalue(errobj)
|
||||
? getstr(tsvalue(errobj))
|
||||
: "error object is not a string";
|
||||
/* produce warning "error in %s (%s)" (where, msg) */
|
||||
luaE_warning(L, "error in ", 1);
|
||||
|
||||
202
lstate.h
202
lstate.h
@ -85,7 +85,7 @@ typedef struct CallInfo CallInfo;
|
||||
** they must be visited again at the end of the cycle), but they are
|
||||
** marked black because assignments to them must activate barriers (to
|
||||
** move them back to TOUCHED1).
|
||||
** - Open upvales are kept gray to avoid barriers, but they stay out
|
||||
** - Open upvalues are kept gray to avoid barriers, but they stay out
|
||||
** of gray lists. (They don't even have a 'gclist' field.)
|
||||
*/
|
||||
|
||||
@ -142,6 +142,17 @@ struct lua_longjmp; /* defined in ldo.c */
|
||||
#define EXTRA_STACK 5
|
||||
|
||||
|
||||
/*
|
||||
** Size of cache for strings in the API. 'N' is the number of
|
||||
** sets (better be a prime) and "M" is the size of each set.
|
||||
** (M == 1 makes a direct cache.)
|
||||
*/
|
||||
#if !defined(STRCACHE_N)
|
||||
#define STRCACHE_N 53
|
||||
#define STRCACHE_M 2
|
||||
#endif
|
||||
|
||||
|
||||
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
||||
|
||||
#define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p)
|
||||
@ -149,13 +160,14 @@ struct lua_longjmp; /* defined in ldo.c */
|
||||
|
||||
/* kinds of Garbage Collection */
|
||||
#define KGC_INC 0 /* incremental gc */
|
||||
#define KGC_GEN 1 /* generational gc */
|
||||
#define KGC_GENMINOR 1 /* generational gc in minor (regular) mode */
|
||||
#define KGC_GENMAJOR 2 /* generational in major mode */
|
||||
|
||||
|
||||
typedef struct stringtable {
|
||||
TString **hash;
|
||||
TString **hash; /* array of buckets (linked lists of strings) */
|
||||
int nuse; /* number of elements */
|
||||
int size;
|
||||
int size; /* number of buckets */
|
||||
} stringtable;
|
||||
|
||||
|
||||
@ -171,17 +183,15 @@ typedef struct stringtable {
|
||||
** yield (from the yield until the next resume);
|
||||
** - field 'nres' is used only while closing tbc variables when
|
||||
** returning from a function;
|
||||
** - field 'transferinfo' is used only during call/returnhooks,
|
||||
** before the function starts or after it ends.
|
||||
*/
|
||||
struct CallInfo {
|
||||
StkIdRel func; /* function index in the stack */
|
||||
StkIdRel top; /* top for this function */
|
||||
StkIdRel top; /* top for this function */
|
||||
struct CallInfo *previous, *next; /* dynamic call link */
|
||||
union {
|
||||
struct { /* only for Lua functions */
|
||||
const Instruction *savedpc;
|
||||
volatile l_signalT trap;
|
||||
volatile l_signalT trap; /* function is tracing lines/counts */
|
||||
int nextraargs; /* # of extra arguments in vararg functions */
|
||||
} l;
|
||||
struct { /* only for C functions */
|
||||
@ -194,35 +204,54 @@ struct CallInfo {
|
||||
int funcidx; /* called-function index */
|
||||
int nyield; /* number of values yielded */
|
||||
int nres; /* number of values returned */
|
||||
struct { /* info about transferred values (for call/return hooks) */
|
||||
unsigned short ftransfer; /* offset of first value transferred */
|
||||
unsigned short ntransfer; /* number of values transferred */
|
||||
} transferinfo;
|
||||
} u2;
|
||||
short nresults; /* expected number of results from this function */
|
||||
unsigned short callstatus;
|
||||
l_uint32 callstatus;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Maximum expected number of results from a function
|
||||
** (must fit in CIST_NRESULTS).
|
||||
*/
|
||||
#define MAXRESULTS 250
|
||||
|
||||
|
||||
/*
|
||||
** Bits in CallInfo status
|
||||
*/
|
||||
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
||||
#define CIST_C (1<<1) /* call is running a C function */
|
||||
#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */
|
||||
#define CIST_HOOKED (1<<3) /* call is running a debug hook */
|
||||
#define CIST_YPCALL (1<<4) /* doing a yieldable protected call */
|
||||
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||
#define CIST_FIN (1<<7) /* function "called" a finalizer */
|
||||
#define CIST_TRAN (1<<8) /* 'ci' has transfer information */
|
||||
#define CIST_CLSRET (1<<9) /* function is closing tbc variables */
|
||||
/* Bits 10-12 are used for CIST_RECST (see below) */
|
||||
#define CIST_RECST 10
|
||||
#if defined(LUA_COMPAT_LT_LE)
|
||||
#define CIST_LEQ (1<<13) /* using __lt for __le */
|
||||
#endif
|
||||
/* bits 0-7 are the expected number of results from this function + 1 */
|
||||
#define CIST_NRESULTS 0xffu
|
||||
|
||||
/* bits 8-11 count call metamethods (and their extra arguments) */
|
||||
#define CIST_CCMT 8 /* the offset, not the mask */
|
||||
#define MAX_CCMT (0xfu << CIST_CCMT)
|
||||
|
||||
/* Bits 12-14 are used for CIST_RECST (see below) */
|
||||
#define CIST_RECST 12 /* the offset, not the mask */
|
||||
|
||||
/* call is running a C function (still in first 16 bits) */
|
||||
#define CIST_C (1u << (CIST_RECST + 3))
|
||||
/* call is on a fresh "luaV_execute" frame */
|
||||
#define CIST_FRESH (cast(l_uint32, CIST_C) << 1)
|
||||
/* function is closing tbc variables */
|
||||
#define CIST_CLSRET (CIST_FRESH << 1)
|
||||
/* function has tbc variables to close */
|
||||
#define CIST_TBC (CIST_CLSRET << 1)
|
||||
/* original value of 'allowhook' */
|
||||
#define CIST_OAH (CIST_TBC << 1)
|
||||
/* call is running a debug hook */
|
||||
#define CIST_HOOKED (CIST_OAH << 1)
|
||||
/* doing a yieldable protected call */
|
||||
#define CIST_YPCALL (CIST_HOOKED << 1)
|
||||
/* call was tail called */
|
||||
#define CIST_TAIL (CIST_YPCALL << 1)
|
||||
/* last hook called yielded */
|
||||
#define CIST_HOOKYIELD (CIST_TAIL << 1)
|
||||
/* function "called" a finalizer */
|
||||
#define CIST_FIN (CIST_HOOKYIELD << 1)
|
||||
|
||||
|
||||
#define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1)
|
||||
|
||||
/*
|
||||
** Field CIST_RECST stores the "recover status", used to keep the error
|
||||
@ -233,8 +262,8 @@ struct CallInfo {
|
||||
#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7)
|
||||
#define setcistrecst(ci,st) \
|
||||
check_exp(((st) & 7) == (st), /* status must fit in three bits */ \
|
||||
((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \
|
||||
| ((st) << CIST_RECST)))
|
||||
((ci)->callstatus = ((ci)->callstatus & ~(7u << CIST_RECST)) \
|
||||
| (cast(l_uint32, st) << CIST_RECST)))
|
||||
|
||||
|
||||
/* active function is a Lua function */
|
||||
@ -243,9 +272,53 @@ struct CallInfo {
|
||||
/* call is running Lua code (not a hook) */
|
||||
#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED)))
|
||||
|
||||
/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
|
||||
#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
|
||||
#define getoah(st) ((st) & CIST_OAH)
|
||||
|
||||
#define setoah(ci,v) \
|
||||
((ci)->callstatus = ((v) ? (ci)->callstatus | CIST_OAH \
|
||||
: (ci)->callstatus & ~CIST_OAH))
|
||||
#define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0)
|
||||
|
||||
|
||||
/*
|
||||
** 'per thread' state
|
||||
*/
|
||||
struct lua_State {
|
||||
CommonHeader;
|
||||
lu_byte allowhook;
|
||||
TStatus status;
|
||||
StkIdRel top; /* first free slot in the stack */
|
||||
struct global_State *l_G;
|
||||
CallInfo *ci; /* call info for current function */
|
||||
StkIdRel stack_last; /* end of stack (last element + 1) */
|
||||
StkIdRel stack; /* stack base */
|
||||
UpVal *openupval; /* list of open upvalues in this stack */
|
||||
StkIdRel tbclist; /* list of to-be-closed variables */
|
||||
GCObject *gclist;
|
||||
struct lua_State *twups; /* list of threads with open upvalues */
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
CallInfo base_ci; /* CallInfo for first level (C host) */
|
||||
volatile lua_Hook hook;
|
||||
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
||||
l_uint32 nCcalls; /* number of nested non-yieldable or C calls */
|
||||
int oldpc; /* last pc traced */
|
||||
int nci; /* number of items in 'ci' list */
|
||||
int basehookcount;
|
||||
int hookcount;
|
||||
volatile l_signalT hookmask;
|
||||
struct { /* info about transferred values (for call/return hooks) */
|
||||
int ftransfer; /* offset of first value transferred */
|
||||
int ntransfer; /* number of values transferred */
|
||||
} transferinfo;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** thread state + extra space
|
||||
*/
|
||||
typedef struct LX {
|
||||
lu_byte extra_[LUA_EXTRASPACE];
|
||||
lua_State l;
|
||||
} LX;
|
||||
|
||||
|
||||
/*
|
||||
@ -254,25 +327,21 @@ struct CallInfo {
|
||||
typedef struct global_State {
|
||||
lua_Alloc frealloc; /* function to reallocate memory */
|
||||
void *ud; /* auxiliary data to 'frealloc' */
|
||||
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
||||
lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */
|
||||
l_mem GCtotalbytes; /* number of bytes currently allocated + debt */
|
||||
l_mem GCdebt; /* bytes counted but not yet allocated */
|
||||
l_mem GCmarked; /* number of objects marked in a GC cycle */
|
||||
l_mem GCmajorminor; /* auxiliary counter to control major-minor shifts */
|
||||
stringtable strt; /* hash table for strings */
|
||||
TValue l_registry;
|
||||
TValue nilvalue; /* a nil value */
|
||||
unsigned int seed; /* randomized seed for hashes */
|
||||
lu_byte gcparams[LUA_GCPN];
|
||||
lu_byte currentwhite;
|
||||
lu_byte gcstate; /* state of garbage collector */
|
||||
lu_byte gckind; /* kind of GC running */
|
||||
lu_byte gcstopem; /* stops emergency collections */
|
||||
lu_byte genminormul; /* control for minor generational collections */
|
||||
lu_byte genmajormul; /* control for major generational collections */
|
||||
lu_byte gcstp; /* control whether GC is running */
|
||||
lu_byte gcemergency; /* true if this is an emergency collection */
|
||||
lu_byte gcpause; /* size of pause between successive GCs */
|
||||
lu_byte gcstepmul; /* GC "speed" */
|
||||
lu_byte gcstepsize; /* (log2 of) GC granularity */
|
||||
GCObject *allgc; /* list of all collectable objects */
|
||||
GCObject **sweepgc; /* current position of sweep in list */
|
||||
GCObject *finobj; /* list of collectable objects with finalizers */
|
||||
@ -293,46 +362,18 @@ typedef struct global_State {
|
||||
GCObject *finobjrold; /* list of really old objects with finalizers */
|
||||
struct lua_State *twups; /* list of threads with open upvalues */
|
||||
lua_CFunction panic; /* to be called in unprotected errors */
|
||||
struct lua_State *mainthread;
|
||||
TString *memerrmsg; /* message for memory-allocation errors */
|
||||
TString *tmname[TM_N]; /* array with tag-method names */
|
||||
struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */
|
||||
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
||||
lua_WarnFunction warnf; /* warning function */
|
||||
void *ud_warn; /* auxiliary data to 'warnf' */
|
||||
LX mainth; /* main thread of this state */
|
||||
} global_State;
|
||||
|
||||
|
||||
/*
|
||||
** 'per thread' state
|
||||
*/
|
||||
struct lua_State {
|
||||
CommonHeader;
|
||||
lu_byte status;
|
||||
lu_byte allowhook;
|
||||
unsigned short nci; /* number of items in 'ci' list */
|
||||
StkIdRel top; /* first free slot in the stack */
|
||||
global_State *l_G;
|
||||
CallInfo *ci; /* call info for current function */
|
||||
StkIdRel stack_last; /* end of stack (last element + 1) */
|
||||
StkIdRel stack; /* stack base */
|
||||
UpVal *openupval; /* list of open upvalues in this stack */
|
||||
StkIdRel tbclist; /* list of to-be-closed variables */
|
||||
GCObject *gclist;
|
||||
struct lua_State *twups; /* list of threads with open upvalues */
|
||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
|
||||
volatile lua_Hook hook;
|
||||
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
||||
l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */
|
||||
int oldpc; /* last pc traced */
|
||||
int basehookcount;
|
||||
int hookcount;
|
||||
volatile l_signalT hookmask;
|
||||
};
|
||||
|
||||
|
||||
#define G(L) (L->l_G)
|
||||
#define mainthread(G) (&(G)->mainth.l)
|
||||
|
||||
/*
|
||||
** 'g->nilvalue' being a nil value flags that the state was completely
|
||||
@ -385,24 +426,25 @@ union GCUnion {
|
||||
|
||||
/*
|
||||
** macro to convert a Lua object into a GCObject
|
||||
** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.)
|
||||
*/
|
||||
#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc))
|
||||
#define obj2gco(v) \
|
||||
check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc))
|
||||
|
||||
|
||||
/* actual number of total bytes allocated */
|
||||
#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
|
||||
/* actual number of total memory allocated */
|
||||
#define gettotalbytes(g) ((g)->GCtotalbytes - (g)->GCdebt)
|
||||
|
||||
|
||||
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
|
||||
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
||||
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
|
||||
LUAI_FUNC void luaE_freeCI (lua_State *L);
|
||||
LUAI_FUNC lu_mem luaE_threadsize (lua_State *L);
|
||||
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L, int err);
|
||||
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
|
||||
LUAI_FUNC void luaE_checkcstack (lua_State *L);
|
||||
LUAI_FUNC void luaE_incCstack (lua_State *L);
|
||||
LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
|
||||
LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
|
||||
LUAI_FUNC int luaE_resetthread (lua_State *L, int status);
|
||||
LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
130
lstring.c
130
lstring.c
@ -25,22 +25,32 @@
|
||||
/*
|
||||
** Maximum size for string table.
|
||||
*/
|
||||
#define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*))
|
||||
#define MAXSTRTB cast_int(luaM_limitN(INT_MAX, TString*))
|
||||
|
||||
/*
|
||||
** Initial size for the string table (must be power of 2).
|
||||
** The Lua core alone registers ~50 strings (reserved words +
|
||||
** metaevent keys + a few others). Libraries would typically add
|
||||
** a few dozens more.
|
||||
*/
|
||||
#if !defined(MINSTRTABSIZE)
|
||||
#define MINSTRTABSIZE 128
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** equality for long strings
|
||||
** generic equality for strings
|
||||
*/
|
||||
int luaS_eqlngstr (TString *a, TString *b) {
|
||||
size_t len = a->u.lnglen;
|
||||
lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
|
||||
return (a == b) || /* same instance or... */
|
||||
((len == b->u.lnglen) && /* equal length and ... */
|
||||
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
||||
int luaS_eqstr (TString *a, TString *b) {
|
||||
size_t len1, len2;
|
||||
const char *s1 = getlstr(a, len1);
|
||||
const char *s2 = getlstr(b, len2);
|
||||
return ((len1 == len2) && /* equal length and ... */
|
||||
(memcmp(s1, s2, len1) == 0)); /* equal contents */
|
||||
}
|
||||
|
||||
|
||||
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
||||
static unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
|
||||
unsigned int h = seed ^ cast_uint(l);
|
||||
for (; l > 0; l--)
|
||||
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
|
||||
@ -48,11 +58,11 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
||||
}
|
||||
|
||||
|
||||
unsigned int luaS_hashlongstr (TString *ts) {
|
||||
unsigned luaS_hashlongstr (TString *ts) {
|
||||
lua_assert(ts->tt == LUA_VLNGSTR);
|
||||
if (ts->extra == 0) { /* no hash? */
|
||||
size_t len = ts->u.lnglen;
|
||||
ts->hash = luaS_hash(getstr(ts), len, ts->hash);
|
||||
ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
|
||||
ts->extra = 1; /* now it has its hash */
|
||||
}
|
||||
return ts->hash;
|
||||
@ -136,27 +146,43 @@ void luaS_init (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
size_t luaS_sizelngstr (size_t len, int kind) {
|
||||
switch (kind) {
|
||||
case LSTRREG: /* regular long string */
|
||||
/* don't need 'falloc'/'ud', but need space for content */
|
||||
return offsetof(TString, falloc) + (len + 1) * sizeof(char);
|
||||
case LSTRFIX: /* fixed external long string */
|
||||
/* don't need 'falloc'/'ud' */
|
||||
return offsetof(TString, falloc);
|
||||
default: /* external long string with deallocation */
|
||||
lua_assert(kind == LSTRMEM);
|
||||
return sizeof(TString);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** creates a new string object
|
||||
*/
|
||||
static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
|
||||
static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
|
||||
unsigned h) {
|
||||
TString *ts;
|
||||
GCObject *o;
|
||||
size_t totalsize; /* total size of TString object */
|
||||
totalsize = sizelstring(l);
|
||||
o = luaC_newobj(L, tag, totalsize);
|
||||
ts = gco2ts(o);
|
||||
ts->hash = h;
|
||||
ts->extra = 0;
|
||||
getstr(ts)[l] = '\0'; /* ending 0 */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
|
||||
TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
|
||||
size_t totalsize = luaS_sizelngstr(l, LSTRREG);
|
||||
TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed);
|
||||
ts->u.lnglen = l;
|
||||
ts->shrlen = LSTRREG; /* signals that it is a regular long string */
|
||||
ts->contents = cast_charp(ts) + offsetof(TString, falloc);
|
||||
ts->contents[l] = '\0'; /* ending 0 */
|
||||
return ts;
|
||||
}
|
||||
|
||||
@ -172,9 +198,9 @@ void luaS_remove (lua_State *L, TString *ts) {
|
||||
|
||||
|
||||
static void growstrtab (lua_State *L, stringtable *tb) {
|
||||
if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
|
||||
if (l_unlikely(tb->nuse == INT_MAX)) { /* too many strings? */
|
||||
luaC_fullgc(L, 1); /* try to free some... */
|
||||
if (tb->nuse == MAX_INT) /* still too many? */
|
||||
if (tb->nuse == INT_MAX) /* still too many? */
|
||||
luaM_error(L); /* cannot even create a message... */
|
||||
}
|
||||
if (tb->size <= MAXSTRTB / 2) /* can grow string table? */
|
||||
@ -193,7 +219,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
||||
TString **list = &tb->hash[lmod(h, tb->size)];
|
||||
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
|
||||
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
||||
if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
||||
if (l == cast_uint(ts->shrlen) &&
|
||||
(memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
|
||||
/* found! */
|
||||
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
||||
changewhite(ts); /* resurrect it */
|
||||
@ -205,9 +232,10 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
||||
growstrtab(L, tb);
|
||||
list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
|
||||
}
|
||||
ts = createstrobj(L, l, LUA_VSHRSTR, h);
|
||||
memcpy(getstr(ts), str, l * sizeof(char));
|
||||
ts->shrlen = cast_byte(l);
|
||||
ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
|
||||
ts->shrlen = cast(ls_byte, l);
|
||||
getshrstr(ts)[l] = '\0'; /* ending 0 */
|
||||
memcpy(getshrstr(ts), str, l * sizeof(char));
|
||||
ts->u.hnext = *list;
|
||||
*list = ts;
|
||||
tb->nuse++;
|
||||
@ -223,10 +251,10 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
return internshrstr(L, str, l);
|
||||
else {
|
||||
TString *ts;
|
||||
if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
|
||||
if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
|
||||
luaM_toobig(L);
|
||||
ts = luaS_createlngstrobj(L, l);
|
||||
memcpy(getstr(ts), str, l * sizeof(char));
|
||||
memcpy(getlngstr(ts), str, l * sizeof(char));
|
||||
return ts;
|
||||
}
|
||||
}
|
||||
@ -255,7 +283,7 @@ TString *luaS_new (lua_State *L, const char *str) {
|
||||
}
|
||||
|
||||
|
||||
Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
|
||||
Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
|
||||
Udata *u;
|
||||
int i;
|
||||
GCObject *o;
|
||||
@ -271,3 +299,55 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
struct NewExt {
|
||||
ls_byte kind;
|
||||
const char *s;
|
||||
size_t len;
|
||||
TString *ts; /* output */
|
||||
};
|
||||
|
||||
|
||||
static void f_newext (lua_State *L, void *ud) {
|
||||
struct NewExt *ne = cast(struct NewExt *, ud);
|
||||
size_t size = luaS_sizelngstr(0, ne->kind);
|
||||
ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed);
|
||||
}
|
||||
|
||||
|
||||
TString *luaS_newextlstr (lua_State *L,
|
||||
const char *s, size_t len, lua_Alloc falloc, void *ud) {
|
||||
struct NewExt ne;
|
||||
if (!falloc) {
|
||||
ne.kind = LSTRFIX;
|
||||
f_newext(L, &ne); /* just create header */
|
||||
}
|
||||
else {
|
||||
ne.kind = LSTRMEM;
|
||||
if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) { /* mem. error? */
|
||||
(*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */
|
||||
luaM_error(L); /* re-raise memory error */
|
||||
}
|
||||
ne.ts->falloc = falloc;
|
||||
ne.ts->ud = ud;
|
||||
}
|
||||
ne.ts->shrlen = ne.kind;
|
||||
ne.ts->u.lnglen = len;
|
||||
ne.ts->contents = cast_charp(s);
|
||||
return ne.ts;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Normalize an external string: If it is short, internalize it.
|
||||
*/
|
||||
TString *luaS_normstr (lua_State *L, TString *ts) {
|
||||
size_t len = ts->u.lnglen;
|
||||
if (len > LUAI_MAXSHORTLEN)
|
||||
return ts; /* long string; keep the original */
|
||||
else {
|
||||
const char *str = getlngstr(ts);
|
||||
return internshrstr(L, str, len);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
32
lstring.h
32
lstring.h
@ -20,10 +20,23 @@
|
||||
|
||||
|
||||
/*
|
||||
** Size of a TString: Size of the header plus space for the string
|
||||
** Maximum length for short strings, that is, strings that are
|
||||
** internalized. (Cannot be smaller than reserved words or tags for
|
||||
** metamethods, as these strings must be internalized;
|
||||
** #("function") = 8, #("__newindex") = 10.)
|
||||
*/
|
||||
#if !defined(LUAI_MAXSHORTLEN)
|
||||
#define LUAI_MAXSHORTLEN 40
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Size of a short TString: Size of the header plus space for the string
|
||||
** itself (including final '\0').
|
||||
*/
|
||||
#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char))
|
||||
#define sizestrshr(l) \
|
||||
(offsetof(TString, contents) + ((l) + 1) * sizeof(char))
|
||||
|
||||
|
||||
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
|
||||
(sizeof(s)/sizeof(char))-1))
|
||||
@ -32,7 +45,7 @@
|
||||
/*
|
||||
** test whether a string is a reserved word
|
||||
*/
|
||||
#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0)
|
||||
#define isreserved(s) (strisshr(s) && (s)->extra > 0)
|
||||
|
||||
|
||||
/*
|
||||
@ -41,17 +54,20 @@
|
||||
#define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b))
|
||||
|
||||
|
||||
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
|
||||
LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
|
||||
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
||||
LUAI_FUNC unsigned luaS_hashlongstr (TString *ts);
|
||||
LUAI_FUNC int luaS_eqstr (TString *a, TString *b);
|
||||
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
||||
LUAI_FUNC void luaS_clearcache (global_State *g);
|
||||
LUAI_FUNC void luaS_init (lua_State *L);
|
||||
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
||||
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue);
|
||||
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s,
|
||||
unsigned short nuvalue);
|
||||
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
|
||||
LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
|
||||
|
||||
LUAI_FUNC TString *luaS_newextlstr (lua_State *L,
|
||||
const char *s, size_t len, lua_Alloc falloc, void *ud);
|
||||
LUAI_FUNC size_t luaS_sizelngstr (size_t len, int kind);
|
||||
LUAI_FUNC TString *luaS_normstr (lua_State *L, TString *ts);
|
||||
|
||||
#endif
|
||||
|
||||
298
lstrlib.c
298
lstrlib.c
@ -24,6 +24,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -36,22 +37,6 @@
|
||||
#endif
|
||||
|
||||
|
||||
/* macro to 'unsign' a character */
|
||||
#define uchar(c) ((unsigned char)(c))
|
||||
|
||||
|
||||
/*
|
||||
** Some sizes are better limited to fit in 'int', but must also fit in
|
||||
** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
|
||||
*/
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0))
|
||||
|
||||
#define MAXSIZE \
|
||||
(sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
|
||||
|
||||
|
||||
|
||||
|
||||
static int str_len (lua_State *L) {
|
||||
size_t l;
|
||||
luaL_checklstring(L, 1, &l);
|
||||
@ -128,7 +113,7 @@ static int str_lower (lua_State *L) {
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
char *p = luaL_buffinitsize(L, &b, l);
|
||||
for (i=0; i<l; i++)
|
||||
p[i] = tolower(uchar(s[i]));
|
||||
p[i] = cast_char(tolower(cast_uchar(s[i])));
|
||||
luaL_pushresultsize(&b, l);
|
||||
return 1;
|
||||
}
|
||||
@ -141,33 +126,37 @@ static int str_upper (lua_State *L) {
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
char *p = luaL_buffinitsize(L, &b, l);
|
||||
for (i=0; i<l; i++)
|
||||
p[i] = toupper(uchar(s[i]));
|
||||
p[i] = cast_char(toupper(cast_uchar(s[i])));
|
||||
luaL_pushresultsize(&b, l);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** MAX_SIZE is limited both by size_t and lua_Integer.
|
||||
** When x <= MAX_SIZE, x can be safely cast to size_t or lua_Integer.
|
||||
*/
|
||||
static int str_rep (lua_State *L) {
|
||||
size_t l, lsep;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
size_t len, lsep;
|
||||
const char *s = luaL_checklstring(L, 1, &len);
|
||||
lua_Integer n = luaL_checkinteger(L, 2);
|
||||
const char *sep = luaL_optlstring(L, 3, "", &lsep);
|
||||
if (n <= 0)
|
||||
lua_pushliteral(L, "");
|
||||
else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n))
|
||||
if (n <= 0 || (len | lsep) == 0)
|
||||
lua_pushliteral(L, ""); /* no repetitions or both strings empty */
|
||||
else if (l_unlikely(len > MAX_SIZE - lsep ||
|
||||
cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n))
|
||||
return luaL_error(L, "resulting string too large");
|
||||
else {
|
||||
size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
|
||||
size_t totallen = (cast_sizet(n) * (len + lsep)) - lsep;
|
||||
luaL_Buffer b;
|
||||
char *p = luaL_buffinitsize(L, &b, totallen);
|
||||
while (n-- > 1) { /* first n-1 copies (followed by separator) */
|
||||
memcpy(p, s, l * sizeof(char)); p += l;
|
||||
memcpy(p, s, len * sizeof(char)); p += len;
|
||||
if (lsep > 0) { /* empty 'memcpy' is not that cheap */
|
||||
memcpy(p, sep, lsep * sizeof(char));
|
||||
p += lsep;
|
||||
memcpy(p, sep, lsep * sizeof(char)); p += lsep;
|
||||
}
|
||||
}
|
||||
memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
|
||||
memcpy(p, s, len * sizeof(char)); /* last copy without separator */
|
||||
luaL_pushresultsize(&b, totallen);
|
||||
}
|
||||
return 1;
|
||||
@ -187,7 +176,7 @@ static int str_byte (lua_State *L) {
|
||||
n = (int)(pose - posi) + 1;
|
||||
luaL_checkstack(L, n, "string slice too long");
|
||||
for (i=0; i<n; i++)
|
||||
lua_pushinteger(L, uchar(s[posi+i-1]));
|
||||
lua_pushinteger(L, cast_uchar(s[posi + cast_uint(i) - 1]));
|
||||
return n;
|
||||
}
|
||||
|
||||
@ -196,13 +185,13 @@ static int str_char (lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
luaL_Buffer b;
|
||||
char *p = luaL_buffinitsize(L, &b, n);
|
||||
char *p = luaL_buffinitsize(L, &b, cast_uint(n));
|
||||
for (i=1; i<=n; i++) {
|
||||
lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
|
||||
luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
|
||||
p[i - 1] = uchar(c);
|
||||
p[i - 1] = cast_char(cast_uchar(c));
|
||||
}
|
||||
luaL_pushresultsize(&b, n);
|
||||
luaL_pushresultsize(&b, cast_uint(n));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -225,7 +214,12 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) {
|
||||
state->init = 1;
|
||||
luaL_buffinit(L, &state->B);
|
||||
}
|
||||
luaL_addlstring(&state->B, (const char *)b, size);
|
||||
if (b == NULL) { /* finishing dump? */
|
||||
luaL_pushresult(&state->B); /* push result */
|
||||
lua_replace(L, 1); /* move it to reserved slot */
|
||||
}
|
||||
else
|
||||
luaL_addlstring(&state->B, (const char *)b, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -233,12 +227,13 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) {
|
||||
static int str_dump (lua_State *L) {
|
||||
struct str_Writer state;
|
||||
int strip = lua_toboolean(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_settop(L, 1); /* ensure function is on the top of the stack */
|
||||
luaL_argcheck(L, lua_type(L, 1) == LUA_TFUNCTION && !lua_iscfunction(L, 1),
|
||||
1, "Lua function expected");
|
||||
/* ensure function is on the top of the stack and vacate slot 1 */
|
||||
lua_pushvalue(L, 1);
|
||||
state.init = 0;
|
||||
if (l_unlikely(lua_dump(L, writer, &state, strip) != 0))
|
||||
return luaL_error(L, "unable to dump given function");
|
||||
luaL_pushresult(&state.B);
|
||||
lua_dump(L, writer, &state, strip);
|
||||
lua_settop(L, 1); /* leave final result on top */
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -274,11 +269,18 @@ static int tonum (lua_State *L, int arg) {
|
||||
}
|
||||
|
||||
|
||||
static void trymt (lua_State *L, const char *mtname) {
|
||||
/*
|
||||
** To be here, either the first operand was a string or the first
|
||||
** operand didn't have a corresponding metamethod. (Otherwise, that
|
||||
** other metamethod would have been called.) So, if this metamethod
|
||||
** doesn't work, the only other option would be for the second
|
||||
** operand to have a different metamethod.
|
||||
*/
|
||||
static void trymt (lua_State *L, const char *mtkey, const char *opname) {
|
||||
lua_settop(L, 2); /* back to the original arguments */
|
||||
if (l_unlikely(lua_type(L, 2) == LUA_TSTRING ||
|
||||
!luaL_getmetafield(L, 2, mtname)))
|
||||
luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2,
|
||||
!luaL_getmetafield(L, 2, mtkey)))
|
||||
luaL_error(L, "attempt to %s a '%s' with a '%s'", opname,
|
||||
luaL_typename(L, -2), luaL_typename(L, -1));
|
||||
lua_insert(L, -3); /* put metamethod before arguments */
|
||||
lua_call(L, 2, 1); /* call metamethod */
|
||||
@ -289,7 +291,7 @@ static int arith (lua_State *L, int op, const char *mtname) {
|
||||
if (tonum(L, 1) && tonum(L, 2))
|
||||
lua_arith(L, op); /* result will be on the top */
|
||||
else
|
||||
trymt(L, mtname);
|
||||
trymt(L, mtname, mtname + 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -361,10 +363,10 @@ typedef struct MatchState {
|
||||
const char *p_end; /* end ('\0') of pattern */
|
||||
lua_State *L;
|
||||
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
|
||||
unsigned char level; /* total number of captures (finished or unfinished) */
|
||||
int level; /* total number of captures (finished or unfinished) */
|
||||
struct {
|
||||
const char *init;
|
||||
ptrdiff_t len;
|
||||
ptrdiff_t len; /* length or special value (CAP_*) */
|
||||
} capture[LUA_MAXCAPTURES];
|
||||
} MatchState;
|
||||
|
||||
@ -453,15 +455,15 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
|
||||
while (++p < ec) {
|
||||
if (*p == L_ESC) {
|
||||
p++;
|
||||
if (match_class(c, uchar(*p)))
|
||||
if (match_class(c, cast_uchar(*p)))
|
||||
return sig;
|
||||
}
|
||||
else if ((*(p+1) == '-') && (p+2 < ec)) {
|
||||
p+=2;
|
||||
if (uchar(*(p-2)) <= c && c <= uchar(*p))
|
||||
if (cast_uchar(*(p-2)) <= c && c <= cast_uchar(*p))
|
||||
return sig;
|
||||
}
|
||||
else if (uchar(*p) == c) return sig;
|
||||
else if (cast_uchar(*p) == c) return sig;
|
||||
}
|
||||
return !sig;
|
||||
}
|
||||
@ -472,12 +474,12 @@ static int singlematch (MatchState *ms, const char *s, const char *p,
|
||||
if (s >= ms->src_end)
|
||||
return 0;
|
||||
else {
|
||||
int c = uchar(*s);
|
||||
int c = cast_uchar(*s);
|
||||
switch (*p) {
|
||||
case '.': return 1; /* matches any char */
|
||||
case L_ESC: return match_class(c, uchar(*(p+1)));
|
||||
case L_ESC: return match_class(c, cast_uchar(*(p+1)));
|
||||
case '[': return matchbracketclass(c, p, ep-1);
|
||||
default: return (uchar(*p) == c);
|
||||
default: return (cast_uchar(*p) == c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -559,7 +561,7 @@ static const char *end_capture (MatchState *ms, const char *s,
|
||||
static const char *match_capture (MatchState *ms, const char *s, int l) {
|
||||
size_t len;
|
||||
l = check_capture(ms, l);
|
||||
len = ms->capture[l].len;
|
||||
len = cast_sizet(ms->capture[l].len);
|
||||
if ((size_t)(ms->src_end-s) >= len &&
|
||||
memcmp(ms->capture[l].init, s, len) == 0)
|
||||
return s+len;
|
||||
@ -606,8 +608,8 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||
luaL_error(ms->L, "missing '[' after '%%f' in pattern");
|
||||
ep = classend(ms, p); /* points to what is next */
|
||||
previous = (s == ms->src_init) ? '\0' : *(s - 1);
|
||||
if (!matchbracketclass(uchar(previous), p, ep - 1) &&
|
||||
matchbracketclass(uchar(*s), p, ep - 1)) {
|
||||
if (!matchbracketclass(cast_uchar(previous), p, ep - 1) &&
|
||||
matchbracketclass(cast_uchar(*s), p, ep - 1)) {
|
||||
p = ep; goto init; /* return match(ms, s, ep); */
|
||||
}
|
||||
s = NULL; /* match failed */
|
||||
@ -616,7 +618,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||
case '0': case '1': case '2': case '3':
|
||||
case '4': case '5': case '6': case '7':
|
||||
case '8': case '9': { /* capture results (%0-%9)? */
|
||||
s = match_capture(ms, s, uchar(*(p + 1)));
|
||||
s = match_capture(ms, s, cast_uchar(*(p + 1)));
|
||||
if (s != NULL) {
|
||||
p += 2; goto init; /* return match(ms, s, p + 2) */
|
||||
}
|
||||
@ -683,7 +685,7 @@ static const char *lmemfind (const char *s1, size_t l1,
|
||||
if (memcmp(init, s2+1, l2) == 0)
|
||||
return init-1;
|
||||
else { /* correct 'l1' and 's1' to try again */
|
||||
l1 -= init-s1;
|
||||
l1 -= ct_diff2sz(init - s1);
|
||||
s1 = init;
|
||||
}
|
||||
}
|
||||
@ -699,13 +701,13 @@ static const char *lmemfind (const char *s1, size_t l1,
|
||||
** its length and put its address in '*cap'. If it is an integer
|
||||
** (a position), push it on the stack and return CAP_POSITION.
|
||||
*/
|
||||
static size_t get_onecapture (MatchState *ms, int i, const char *s,
|
||||
static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s,
|
||||
const char *e, const char **cap) {
|
||||
if (i >= ms->level) {
|
||||
if (l_unlikely(i != 0))
|
||||
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
|
||||
*cap = s;
|
||||
return e - s;
|
||||
return (e - s);
|
||||
}
|
||||
else {
|
||||
ptrdiff_t capl = ms->capture[i].len;
|
||||
@ -713,7 +715,8 @@ static size_t get_onecapture (MatchState *ms, int i, const char *s,
|
||||
if (l_unlikely(capl == CAP_UNFINISHED))
|
||||
luaL_error(ms->L, "unfinished capture");
|
||||
else if (capl == CAP_POSITION)
|
||||
lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
|
||||
lua_pushinteger(ms->L,
|
||||
ct_diff2S(ms->capture[i].init - ms->src_init) + 1);
|
||||
return capl;
|
||||
}
|
||||
}
|
||||
@ -727,7 +730,7 @@ static void push_onecapture (MatchState *ms, int i, const char *s,
|
||||
const char *cap;
|
||||
ptrdiff_t l = get_onecapture(ms, i, s, e, &cap);
|
||||
if (l != CAP_POSITION)
|
||||
lua_pushlstring(ms->L, cap, l);
|
||||
lua_pushlstring(ms->L, cap, cast_sizet(l));
|
||||
/* else position was already pushed */
|
||||
}
|
||||
|
||||
@ -784,8 +787,8 @@ static int str_find_aux (lua_State *L, int find) {
|
||||
/* do a plain search */
|
||||
const char *s2 = lmemfind(s + init, ls - init, p, lp);
|
||||
if (s2) {
|
||||
lua_pushinteger(L, (s2 - s) + 1);
|
||||
lua_pushinteger(L, (s2 - s) + lp);
|
||||
lua_pushinteger(L, ct_diff2S(s2 - s) + 1);
|
||||
lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@ -802,8 +805,8 @@ static int str_find_aux (lua_State *L, int find) {
|
||||
reprepstate(&ms);
|
||||
if ((res=match(&ms, s1, p)) != NULL) {
|
||||
if (find) {
|
||||
lua_pushinteger(L, (s1 - s) + 1); /* start */
|
||||
lua_pushinteger(L, res - s); /* end */
|
||||
lua_pushinteger(L, ct_diff2S(s1 - s) + 1); /* start */
|
||||
lua_pushinteger(L, ct_diff2S(res - s)); /* end */
|
||||
return push_captures(&ms, NULL, 0) + 2;
|
||||
}
|
||||
else
|
||||
@ -875,23 +878,23 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
|
||||
const char *news = lua_tolstring(L, 3, &l);
|
||||
const char *p;
|
||||
while ((p = (char *)memchr(news, L_ESC, l)) != NULL) {
|
||||
luaL_addlstring(b, news, p - news);
|
||||
luaL_addlstring(b, news, ct_diff2sz(p - news));
|
||||
p++; /* skip ESC */
|
||||
if (*p == L_ESC) /* '%%' */
|
||||
luaL_addchar(b, *p);
|
||||
else if (*p == '0') /* '%0' */
|
||||
luaL_addlstring(b, s, e - s);
|
||||
else if (isdigit(uchar(*p))) { /* '%n' */
|
||||
luaL_addlstring(b, s, ct_diff2sz(e - s));
|
||||
else if (isdigit(cast_uchar(*p))) { /* '%n' */
|
||||
const char *cap;
|
||||
ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
|
||||
if (resl == CAP_POSITION)
|
||||
luaL_addvalue(b); /* add position to accumulated result */
|
||||
else
|
||||
luaL_addlstring(b, cap, resl);
|
||||
luaL_addlstring(b, cap, cast_sizet(resl));
|
||||
}
|
||||
else
|
||||
luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
|
||||
l -= p + 1 - news;
|
||||
l -= ct_diff2sz(p + 1 - news);
|
||||
news = p + 1;
|
||||
}
|
||||
luaL_addlstring(b, news, l);
|
||||
@ -926,7 +929,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
|
||||
}
|
||||
if (!lua_toboolean(L, -1)) { /* nil or false? */
|
||||
lua_pop(L, 1); /* remove value */
|
||||
luaL_addlstring(b, s, e - s); /* keep original text */
|
||||
luaL_addlstring(b, s, ct_diff2sz(e - s)); /* keep original text */
|
||||
return 0; /* no changes */
|
||||
}
|
||||
else if (l_unlikely(!lua_isstring(L, -1)))
|
||||
@ -945,7 +948,8 @@ static int str_gsub (lua_State *L) {
|
||||
const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
|
||||
const char *lastmatch = NULL; /* end of last match */
|
||||
int tr = lua_type(L, 3); /* replacement type */
|
||||
lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
|
||||
/* max replacements */
|
||||
lua_Integer max_s = luaL_optinteger(L, 4, cast_st2S(srcl) + 1);
|
||||
int anchor = (*p == '^');
|
||||
lua_Integer n = 0; /* replacement count */
|
||||
int changed = 0; /* change flag */
|
||||
@ -964,7 +968,7 @@ static int str_gsub (lua_State *L) {
|
||||
reprepstate(&ms); /* (re)prepare state for new match */
|
||||
if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */
|
||||
n++;
|
||||
changed = add_value(&ms, &b, src, e, tr) | changed;
|
||||
changed = add_value(&ms, &b, src, e, tr) || changed;
|
||||
src = lastmatch = e;
|
||||
}
|
||||
else if (src < ms.src_end) /* otherwise, skip one character */
|
||||
@ -975,7 +979,7 @@ static int str_gsub (lua_State *L) {
|
||||
if (!changed) /* no changes? */
|
||||
lua_pushvalue(L, 1); /* return original string */
|
||||
else { /* something changed */
|
||||
luaL_addlstring(&b, src, ms.src_end-src);
|
||||
luaL_addlstring(&b, src, ct_diff2sz(ms.src_end - src));
|
||||
luaL_pushresult(&b); /* create and return new string */
|
||||
}
|
||||
lua_pushinteger(L, n); /* number of substitutions */
|
||||
@ -1013,15 +1017,15 @@ static int str_gsub (lua_State *L) {
|
||||
/*
|
||||
** Add integer part of 'x' to buffer and return new 'x'
|
||||
*/
|
||||
static lua_Number adddigit (char *buff, int n, lua_Number x) {
|
||||
static lua_Number adddigit (char *buff, unsigned n, lua_Number x) {
|
||||
lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
|
||||
int d = (int)dd;
|
||||
buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
|
||||
buff[n] = cast_char(d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
|
||||
return x - dd; /* return what is left */
|
||||
}
|
||||
|
||||
|
||||
static int num2straux (char *buff, int sz, lua_Number x) {
|
||||
static int num2straux (char *buff, unsigned sz, lua_Number x) {
|
||||
/* if 'inf' or 'NaN', format it like '%g' */
|
||||
if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
|
||||
return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
|
||||
@ -1032,7 +1036,7 @@ static int num2straux (char *buff, int sz, lua_Number x) {
|
||||
else {
|
||||
int e;
|
||||
lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */
|
||||
int n = 0; /* character count */
|
||||
unsigned n = 0; /* character count */
|
||||
if (m < 0) { /* is number negative? */
|
||||
buff[n++] = '-'; /* add sign */
|
||||
m = -m; /* make it positive */
|
||||
@ -1046,20 +1050,20 @@ static int num2straux (char *buff, int sz, lua_Number x) {
|
||||
m = adddigit(buff, n++, m * 16);
|
||||
} while (m > 0);
|
||||
}
|
||||
n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
|
||||
n += cast_uint(l_sprintf(buff + n, sz - n, "p%+d", e)); /* add exponent */
|
||||
lua_assert(n < sz);
|
||||
return n;
|
||||
return cast_int(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int lua_number2strx (lua_State *L, char *buff, int sz,
|
||||
static int lua_number2strx (lua_State *L, char *buff, unsigned sz,
|
||||
const char *fmt, lua_Number x) {
|
||||
int n = num2straux(buff, sz, x);
|
||||
if (fmt[SIZELENMOD] == 'A') {
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
buff[i] = toupper(uchar(buff[i]));
|
||||
buff[i] = cast_char(toupper(cast_uchar(buff[i])));
|
||||
}
|
||||
else if (l_unlikely(fmt[SIZELENMOD] != 'a'))
|
||||
return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
|
||||
@ -1126,12 +1130,12 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
|
||||
luaL_addchar(b, '\\');
|
||||
luaL_addchar(b, *s);
|
||||
}
|
||||
else if (iscntrl(uchar(*s))) {
|
||||
else if (iscntrl(cast_uchar(*s))) {
|
||||
char buff[10];
|
||||
if (!isdigit(uchar(*(s+1))))
|
||||
l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
|
||||
if (!isdigit(cast_uchar(*(s+1))))
|
||||
l_sprintf(buff, sizeof(buff), "\\%d", (int)cast_uchar(*s));
|
||||
else
|
||||
l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
|
||||
l_sprintf(buff, sizeof(buff), "\\%03d", (int)cast_uchar(*s));
|
||||
luaL_addstring(b, buff);
|
||||
}
|
||||
else
|
||||
@ -1160,9 +1164,9 @@ static int quotefloat (lua_State *L, char *buff, lua_Number n) {
|
||||
int nb = lua_number2strx(L, buff, MAX_ITEM,
|
||||
"%" LUA_NUMBER_FRMLEN "a", n);
|
||||
/* ensures that 'buff' string uses a dot as the radix character */
|
||||
if (memchr(buff, '.', nb) == NULL) { /* no dot? */
|
||||
if (memchr(buff, '.', cast_uint(nb)) == NULL) { /* no dot? */
|
||||
char point = lua_getlocaledecpoint(); /* try locale point */
|
||||
char *ppoint = (char *)memchr(buff, point, nb);
|
||||
char *ppoint = (char *)memchr(buff, point, cast_uint(nb));
|
||||
if (ppoint) *ppoint = '.'; /* change it to a dot */
|
||||
}
|
||||
return nb;
|
||||
@ -1192,7 +1196,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
: LUA_INTEGER_FMT; /* else use default format */
|
||||
nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
|
||||
}
|
||||
luaL_addsize(b, nb);
|
||||
luaL_addsize(b, cast_uint(nb));
|
||||
break;
|
||||
}
|
||||
case LUA_TNIL: case LUA_TBOOLEAN: {
|
||||
@ -1208,9 +1212,9 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
|
||||
|
||||
static const char *get2digits (const char *s) {
|
||||
if (isdigit(uchar(*s))) {
|
||||
if (isdigit(cast_uchar(*s))) {
|
||||
s++;
|
||||
if (isdigit(uchar(*s))) s++; /* (2 digits at most) */
|
||||
if (isdigit(cast_uchar(*s))) s++; /* (2 digits at most) */
|
||||
}
|
||||
return s;
|
||||
}
|
||||
@ -1233,7 +1237,7 @@ static void checkformat (lua_State *L, const char *form, const char *flags,
|
||||
spec = get2digits(spec); /* skip precision */
|
||||
}
|
||||
}
|
||||
if (!isalpha(uchar(*spec))) /* did not go to the end? */
|
||||
if (!isalpha(cast_uchar(*spec))) /* did not go to the end? */
|
||||
luaL_error(L, "invalid conversion specification: '%s'", form);
|
||||
}
|
||||
|
||||
@ -1286,7 +1290,7 @@ static int str_format (lua_State *L) {
|
||||
luaL_addchar(&b, *strfrmt++); /* %% */
|
||||
else { /* format item */
|
||||
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||
int maxitem = MAX_ITEM; /* maximum length for the result */
|
||||
unsigned maxitem = MAX_ITEM; /* maximum length for the result */
|
||||
char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */
|
||||
int nb = 0; /* number of bytes in result */
|
||||
if (++arg > top)
|
||||
@ -1369,8 +1373,8 @@ static int str_format (lua_State *L) {
|
||||
return luaL_error(L, "invalid conversion '%s' to 'format'", form);
|
||||
}
|
||||
}
|
||||
lua_assert(nb < maxitem);
|
||||
luaL_addsize(&b, nb);
|
||||
lua_assert(cast_uint(nb) < maxitem);
|
||||
luaL_addsize(&b, cast_uint(nb));
|
||||
}
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
@ -1418,7 +1422,7 @@ static const union {
|
||||
typedef struct Header {
|
||||
lua_State *L;
|
||||
int islittle;
|
||||
int maxalign;
|
||||
unsigned maxalign;
|
||||
} Header;
|
||||
|
||||
|
||||
@ -1446,14 +1450,14 @@ typedef enum KOption {
|
||||
*/
|
||||
static int digit (int c) { return '0' <= c && c <= '9'; }
|
||||
|
||||
static int getnum (const char **fmt, int df) {
|
||||
static size_t getnum (const char **fmt, size_t df) {
|
||||
if (!digit(**fmt)) /* no number? */
|
||||
return df; /* return default value */
|
||||
else {
|
||||
int a = 0;
|
||||
size_t a = 0;
|
||||
do {
|
||||
a = a*10 + (*((*fmt)++) - '0');
|
||||
} while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10);
|
||||
a = a*10 + cast_uint(*((*fmt)++) - '0');
|
||||
} while (digit(**fmt) && a <= (MAX_SIZE - 9)/10);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
@ -1461,14 +1465,14 @@ static int getnum (const char **fmt, int df) {
|
||||
|
||||
/*
|
||||
** Read an integer numeral and raises an error if it is larger
|
||||
** than the maximum size for integers.
|
||||
** than the maximum size of integers.
|
||||
*/
|
||||
static int getnumlimit (Header *h, const char **fmt, int df) {
|
||||
int sz = getnum(fmt, df);
|
||||
if (l_unlikely(sz > MAXINTSIZE || sz <= 0))
|
||||
return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
|
||||
sz, MAXINTSIZE);
|
||||
return sz;
|
||||
static unsigned getnumlimit (Header *h, const char **fmt, size_t df) {
|
||||
size_t sz = getnum(fmt, df);
|
||||
if (l_unlikely((sz - 1u) >= MAXINTSIZE))
|
||||
return cast_uint(luaL_error(h->L,
|
||||
"integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE));
|
||||
return cast_uint(sz);
|
||||
}
|
||||
|
||||
|
||||
@ -1485,7 +1489,7 @@ static void initheader (lua_State *L, Header *h) {
|
||||
/*
|
||||
** Read and classify next option. 'size' is filled with option's size.
|
||||
*/
|
||||
static KOption getoption (Header *h, const char **fmt, int *size) {
|
||||
static KOption getoption (Header *h, const char **fmt, size_t *size) {
|
||||
/* dummy structure to get native alignment requirements */
|
||||
struct cD { char c; union { LUAI_MAXALIGN; } u; };
|
||||
int opt = *((*fmt)++);
|
||||
@ -1507,8 +1511,8 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
||||
case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
|
||||
case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
|
||||
case 'c':
|
||||
*size = getnum(fmt, -1);
|
||||
if (l_unlikely(*size == -1))
|
||||
*size = getnum(fmt, cast_sizet(-1));
|
||||
if (l_unlikely(*size == cast_sizet(-1)))
|
||||
luaL_error(h->L, "missing size for format option 'c'");
|
||||
return Kchar;
|
||||
case 'z': return Kzstr;
|
||||
@ -1519,7 +1523,7 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
||||
case '>': h->islittle = 0; break;
|
||||
case '=': h->islittle = nativeendian.little; break;
|
||||
case '!': {
|
||||
const int maxalign = offsetof(struct cD, u);
|
||||
const size_t maxalign = offsetof(struct cD, u);
|
||||
h->maxalign = getnumlimit(h, fmt, maxalign);
|
||||
break;
|
||||
}
|
||||
@ -1538,10 +1542,10 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
||||
** the maximum alignment ('maxalign'). Kchar option needs no alignment
|
||||
** despite its size.
|
||||
*/
|
||||
static KOption getdetails (Header *h, size_t totalsize,
|
||||
const char **fmt, int *psize, int *ntoalign) {
|
||||
static KOption getdetails (Header *h, size_t totalsize, const char **fmt,
|
||||
size_t *psize, unsigned *ntoalign) {
|
||||
KOption opt = getoption(h, fmt, psize);
|
||||
int align = *psize; /* usually, alignment follows size */
|
||||
size_t align = *psize; /* usually, alignment follows size */
|
||||
if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
|
||||
if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
|
||||
luaL_argerror(h->L, 1, "invalid next option for option 'X'");
|
||||
@ -1551,9 +1555,15 @@ static KOption getdetails (Header *h, size_t totalsize,
|
||||
else {
|
||||
if (align > h->maxalign) /* enforce maximum alignment */
|
||||
align = h->maxalign;
|
||||
if (l_unlikely((align & (align - 1)) != 0)) /* not a power of 2? */
|
||||
if (l_unlikely(!ispow2(align))) { /* not a power of 2? */
|
||||
*ntoalign = 0; /* to avoid warnings */
|
||||
luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
|
||||
*ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
|
||||
}
|
||||
else {
|
||||
/* 'szmoda' = totalsize % align */
|
||||
unsigned szmoda = cast_uint(totalsize & (align - 1));
|
||||
*ntoalign = cast_uint((align - szmoda) & (align - 1));
|
||||
}
|
||||
}
|
||||
return opt;
|
||||
}
|
||||
@ -1566,9 +1576,9 @@ static KOption getdetails (Header *h, size_t totalsize,
|
||||
** bytes if necessary (by default they would be zeros).
|
||||
*/
|
||||
static void packint (luaL_Buffer *b, lua_Unsigned n,
|
||||
int islittle, int size, int neg) {
|
||||
int islittle, unsigned size, int neg) {
|
||||
char *buff = luaL_prepbuffsize(b, size);
|
||||
int i;
|
||||
unsigned i;
|
||||
buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
|
||||
for (i = 1; i < size; i++) {
|
||||
n >>= NB;
|
||||
@ -1587,7 +1597,7 @@ static void packint (luaL_Buffer *b, lua_Unsigned n,
|
||||
** given 'islittle' is different from native endianness.
|
||||
*/
|
||||
static void copywithendian (char *dest, const char *src,
|
||||
int size, int islittle) {
|
||||
unsigned size, int islittle) {
|
||||
if (islittle == nativeendian.little)
|
||||
memcpy(dest, src, size);
|
||||
else {
|
||||
@ -1608,8 +1618,11 @@ static int str_pack (lua_State *L) {
|
||||
lua_pushnil(L); /* mark to separate arguments from string buffer */
|
||||
luaL_buffinit(L, &b);
|
||||
while (*fmt != '\0') {
|
||||
int size, ntoalign;
|
||||
unsigned ntoalign;
|
||||
size_t size;
|
||||
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||
luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg,
|
||||
"result too long");
|
||||
totalsize += ntoalign + size;
|
||||
while (ntoalign-- > 0)
|
||||
luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */
|
||||
@ -1621,7 +1634,7 @@ static int str_pack (lua_State *L) {
|
||||
lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
|
||||
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
|
||||
}
|
||||
packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
|
||||
packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0));
|
||||
break;
|
||||
}
|
||||
case Kuint: { /* unsigned integers */
|
||||
@ -1629,7 +1642,7 @@ static int str_pack (lua_State *L) {
|
||||
if (size < SZINT) /* need overflow check? */
|
||||
luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
|
||||
arg, "unsigned overflow");
|
||||
packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
|
||||
packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0);
|
||||
break;
|
||||
}
|
||||
case Kfloat: { /* C float */
|
||||
@ -1659,20 +1672,24 @@ static int str_pack (lua_State *L) {
|
||||
case Kchar: { /* fixed-size string */
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, arg, &len);
|
||||
luaL_argcheck(L, len <= (size_t)size, arg,
|
||||
"string longer than given size");
|
||||
luaL_argcheck(L, len <= size, arg, "string longer than given size");
|
||||
luaL_addlstring(&b, s, len); /* add string */
|
||||
while (len++ < (size_t)size) /* pad extra space */
|
||||
luaL_addchar(&b, LUAL_PACKPADBYTE);
|
||||
if (len < size) { /* does it need padding? */
|
||||
size_t psize = size - len; /* pad size */
|
||||
char *buff = luaL_prepbuffsize(&b, psize);
|
||||
memset(buff, LUAL_PACKPADBYTE, psize);
|
||||
luaL_addsize(&b, psize);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Kstring: { /* strings with length count */
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, arg, &len);
|
||||
luaL_argcheck(L, size >= (int)sizeof(size_t) ||
|
||||
len < ((size_t)1 << (size * NB)),
|
||||
luaL_argcheck(L, size >= sizeof(lua_Unsigned) ||
|
||||
len < ((lua_Unsigned)1 << (size * NB)),
|
||||
arg, "string length does not fit in given size");
|
||||
packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */
|
||||
/* pack length */
|
||||
packint(&b, (lua_Unsigned)len, h.islittle, cast_uint(size), 0);
|
||||
luaL_addlstring(&b, s, len);
|
||||
totalsize += len;
|
||||
break;
|
||||
@ -1703,16 +1720,17 @@ static int str_packsize (lua_State *L) {
|
||||
size_t totalsize = 0; /* accumulate total size of result */
|
||||
initheader(L, &h);
|
||||
while (*fmt != '\0') {
|
||||
int size, ntoalign;
|
||||
unsigned ntoalign;
|
||||
size_t size;
|
||||
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||
luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
|
||||
"variable-length format");
|
||||
size += ntoalign; /* total space used by option */
|
||||
luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
|
||||
"format result too large");
|
||||
luaL_argcheck(L, totalsize <= MAX_SIZE - size,
|
||||
1, "format result too large");
|
||||
totalsize += size;
|
||||
}
|
||||
lua_pushinteger(L, (lua_Integer)totalsize);
|
||||
lua_pushinteger(L, cast_st2S(totalsize));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1761,9 +1779,10 @@ static int str_unpack (lua_State *L) {
|
||||
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
|
||||
initheader(L, &h);
|
||||
while (*fmt != '\0') {
|
||||
int size, ntoalign;
|
||||
unsigned ntoalign;
|
||||
size_t size;
|
||||
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
||||
luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2,
|
||||
luaL_argcheck(L, ntoalign + size <= ld - pos, 2,
|
||||
"data string too short");
|
||||
pos += ntoalign; /* skip alignment */
|
||||
/* stack space for item + next position */
|
||||
@ -1772,8 +1791,8 @@ static int str_unpack (lua_State *L) {
|
||||
switch (opt) {
|
||||
case Kint:
|
||||
case Kuint: {
|
||||
lua_Integer res = unpackint(L, data + pos, h.islittle, size,
|
||||
(opt == Kint));
|
||||
lua_Integer res = unpackint(L, data + pos, h.islittle,
|
||||
cast_int(size), (opt == Kint));
|
||||
lua_pushinteger(L, res);
|
||||
break;
|
||||
}
|
||||
@ -1800,10 +1819,11 @@ static int str_unpack (lua_State *L) {
|
||||
break;
|
||||
}
|
||||
case Kstring: {
|
||||
size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
|
||||
lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos,
|
||||
h.islittle, cast_int(size), 0);
|
||||
luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
|
||||
lua_pushlstring(L, data + pos + size, len);
|
||||
pos += len; /* skip string */
|
||||
lua_pushlstring(L, data + pos + size, cast_sizet(len));
|
||||
pos += cast_sizet(len); /* skip string */
|
||||
break;
|
||||
}
|
||||
case Kzstr: {
|
||||
@ -1820,7 +1840,7 @@ static int str_unpack (lua_State *L) {
|
||||
}
|
||||
pos += size;
|
||||
}
|
||||
lua_pushinteger(L, pos + 1); /* next position */
|
||||
lua_pushinteger(L, cast_st2S(pos) + 1); /* next position */
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
|
||||
149
ltable.h
149
ltable.h
@ -20,11 +20,21 @@
|
||||
** may have any of these metamethods. (First access that fails after the
|
||||
** clearing will set the bit again.)
|
||||
*/
|
||||
#define invalidateTMcache(t) ((t)->flags &= ~maskflags)
|
||||
#define invalidateTMcache(t) ((t)->flags &= cast_byte(~maskflags))
|
||||
|
||||
|
||||
/* true when 't' is using 'dummynode' as its hash part */
|
||||
#define isdummy(t) ((t)->lastfree == NULL)
|
||||
/*
|
||||
** Bit BITDUMMY set in 'flags' means the table is using the dummy node
|
||||
** for its hash part.
|
||||
*/
|
||||
|
||||
#define BITDUMMY (1 << 6)
|
||||
#define NOTBITDUMMY cast_byte(~BITDUMMY)
|
||||
#define isdummy(t) ((t)->flags & BITDUMMY)
|
||||
|
||||
#define setnodummy(t) ((t)->flags &= NOTBITDUMMY)
|
||||
#define setdummy(t) ((t)->flags |= BITDUMMY)
|
||||
|
||||
|
||||
|
||||
/* allocated size for hash nodes */
|
||||
@ -35,26 +45,135 @@
|
||||
#define nodefromval(v) cast(Node *, (v))
|
||||
|
||||
|
||||
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
|
||||
|
||||
#define luaH_fastgeti(t,k,res,tag) \
|
||||
{ Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \
|
||||
if ((u < h->asize)) { \
|
||||
tag = *getArrTag(h, u); \
|
||||
if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \
|
||||
else { tag = luaH_getint(h, (k), res); }}
|
||||
|
||||
|
||||
#define luaH_fastseti(t,k,val,hres) \
|
||||
{ Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \
|
||||
if ((u < h->asize)) { \
|
||||
lu_byte *tag = getArrTag(h, u); \
|
||||
if (checknoTM(h->metatable, TM_NEWINDEX) || !tagisempty(*tag)) \
|
||||
{ fval2arr(h, u, tag, val); hres = HOK; } \
|
||||
else hres = ~cast_int(u); } \
|
||||
else { hres = luaH_psetint(h, k, val); }}
|
||||
|
||||
|
||||
/* results from pset */
|
||||
#define HOK 0
|
||||
#define HNOTFOUND 1
|
||||
#define HNOTATABLE 2
|
||||
#define HFIRSTNODE 3
|
||||
|
||||
/*
|
||||
** 'luaH_get*' operations set 'res', unless the value is absent, and
|
||||
** return the tag of the result.
|
||||
** The 'luaH_pset*' (pre-set) operations set the given value and return
|
||||
** HOK, unless the original value is absent. In that case, if the key
|
||||
** is really absent, they return HNOTFOUND. Otherwise, if there is a
|
||||
** slot with that key but with no value, 'luaH_pset*' return an encoding
|
||||
** of where the key is (usually called 'hres'). (pset cannot set that
|
||||
** value because there might be a metamethod.) If the slot is in the
|
||||
** hash part, the encoding is (HFIRSTNODE + hash index); if the slot is
|
||||
** in the array part, the encoding is (~array index), a negative value.
|
||||
** The value HNOTATABLE is used by the fast macros to signal that the
|
||||
** value being indexed is not a table.
|
||||
** (The size for the array part is limited by the maximum power of two
|
||||
** that fits in an unsigned integer; that is INT_MAX+1. So, the C-index
|
||||
** ranges from 0, which encodes to -1, to INT_MAX, which encodes to
|
||||
** INT_MIN. The size of the hash part is limited by the maximum power of
|
||||
** two that fits in a signed integer; that is (INT_MAX+1)/2. So, it is
|
||||
** safe to add HFIRSTNODE to any index there.)
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** The array part of a table is represented by an inverted array of
|
||||
** values followed by an array of tags, to avoid wasting space with
|
||||
** padding. In between them there is an unsigned int, explained later.
|
||||
** The 'array' pointer points between the two arrays, so that values are
|
||||
** indexed with negative indices and tags with non-negative indices.
|
||||
|
||||
Values Tags
|
||||
--------------------------------------------------------
|
||||
... | Value 1 | Value 0 |unsigned|0|1|...
|
||||
--------------------------------------------------------
|
||||
^ t->array
|
||||
|
||||
** All accesses to 't->array' should be through the macros 'getArrTag'
|
||||
** and 'getArrVal'.
|
||||
*/
|
||||
|
||||
/* Computes the address of the tag for the abstract C-index 'k' */
|
||||
#define getArrTag(t,k) (cast(lu_byte*, (t)->array) + sizeof(unsigned) + (k))
|
||||
|
||||
/* Computes the address of the value for the abstract C-index 'k' */
|
||||
#define getArrVal(t,k) ((t)->array - 1 - (k))
|
||||
|
||||
|
||||
/*
|
||||
** The unsigned between the two arrays is used as a hint for #t;
|
||||
** see luaH_getn. It is stored there to avoid wasting space in
|
||||
** the structure Table for tables with no array part.
|
||||
*/
|
||||
#define lenhint(t) cast(unsigned*, (t)->array)
|
||||
|
||||
|
||||
/*
|
||||
** Move TValues to/from arrays, using C indices
|
||||
*/
|
||||
#define arr2obj(h,k,val) \
|
||||
((val)->tt_ = *getArrTag(h,(k)), (val)->value_ = *getArrVal(h,(k)))
|
||||
|
||||
#define obj2arr(h,k,val) \
|
||||
(*getArrTag(h,(k)) = (val)->tt_, *getArrVal(h,(k)) = (val)->value_)
|
||||
|
||||
|
||||
/*
|
||||
** Often, we need to check the tag of a value before moving it. The
|
||||
** following macros also move TValues to/from arrays, but receive the
|
||||
** precomputed tag value or address as an extra argument.
|
||||
*/
|
||||
#define farr2val(h,k,tag,res) \
|
||||
((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)))
|
||||
|
||||
#define fval2arr(h,k,tag,val) \
|
||||
(*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_)
|
||||
|
||||
|
||||
LUAI_FUNC lu_byte luaH_get (Table *t, const TValue *key, TValue *res);
|
||||
LUAI_FUNC lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res);
|
||||
LUAI_FUNC lu_byte luaH_getstr (Table *t, TString *key, TValue *res);
|
||||
LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res);
|
||||
|
||||
/* Special get for metamethods */
|
||||
LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key);
|
||||
|
||||
LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val);
|
||||
LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val);
|
||||
LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val);
|
||||
LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val);
|
||||
|
||||
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
|
||||
TValue *value);
|
||||
LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
|
||||
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
||||
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
|
||||
LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key,
|
||||
TValue *value);
|
||||
LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key,
|
||||
TValue *value);
|
||||
|
||||
LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key,
|
||||
const TValue *slot, TValue *value);
|
||||
TValue *value, int hres);
|
||||
LUAI_FUNC Table *luaH_new (lua_State *L);
|
||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
||||
unsigned int nhsize);
|
||||
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
|
||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize,
|
||||
unsigned nhsize);
|
||||
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize);
|
||||
LUAI_FUNC lu_mem luaH_size (Table *t);
|
||||
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
|
||||
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
|
||||
LUAI_FUNC lua_Unsigned luaH_getn (Table *t);
|
||||
LUAI_FUNC unsigned int luaH_realasize (const Table *t);
|
||||
LUAI_FUNC lua_Unsigned luaH_getn (lua_State *L, Table *t);
|
||||
|
||||
|
||||
#if defined(LUA_DEBUG)
|
||||
|
||||
86
ltablib.c
86
ltablib.c
@ -18,6 +18,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -58,6 +59,16 @@ static void checktab (lua_State *L, int arg, int what) {
|
||||
}
|
||||
|
||||
|
||||
static int tcreate (lua_State *L) {
|
||||
lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1);
|
||||
lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0);
|
||||
luaL_argcheck(L, sizeseq <= cast_uint(INT_MAX), 1, "out of range");
|
||||
luaL_argcheck(L, sizerest <= cast_uint(INT_MAX), 2, "out of range");
|
||||
lua_createtable(L, cast_int(sizeseq), cast_int(sizerest));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int tinsert (lua_State *L) {
|
||||
lua_Integer pos; /* where to insert new element */
|
||||
lua_Integer e = aux_getn(L, 1, TAB_RW);
|
||||
@ -196,7 +207,7 @@ static int tunpack (lua_State *L) {
|
||||
lua_Integer i = luaL_optinteger(L, 2, 1);
|
||||
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
|
||||
if (i > e) return 0; /* empty range */
|
||||
n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
|
||||
n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */
|
||||
if (l_unlikely(n >= (unsigned int)INT_MAX ||
|
||||
!lua_checkstack(L, (int)(++n))))
|
||||
return luaL_error(L, "too many results to unpack");
|
||||
@ -220,41 +231,26 @@ static int tunpack (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
/* type for array indices */
|
||||
/*
|
||||
** Type for array indices. These indices are always limited by INT_MAX,
|
||||
** so it is safe to cast them to lua_Integer even for Lua 32 bits.
|
||||
*/
|
||||
typedef unsigned int IdxT;
|
||||
|
||||
|
||||
/* Versions of lua_seti/lua_geti specialized for IdxT */
|
||||
#define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx))
|
||||
#define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx))
|
||||
|
||||
|
||||
/*
|
||||
** Produce a "random" 'unsigned int' to randomize pivot choice. This
|
||||
** macro is used only when 'sort' detects a big imbalance in the result
|
||||
** of a partition. (If you don't want/need this "randomness", ~0 is a
|
||||
** good choice.)
|
||||
*/
|
||||
#if !defined(l_randomizePivot) /* { */
|
||||
|
||||
#include <time.h>
|
||||
|
||||
/* size of 'e' measured in number of 'unsigned int's */
|
||||
#define sof(e) (sizeof(e) / sizeof(unsigned int))
|
||||
|
||||
/*
|
||||
** Use 'time' and 'clock' as sources of "randomness". Because we don't
|
||||
** know the types 'clock_t' and 'time_t', we cannot cast them to
|
||||
** anything without risking overflows. A safe way to use their values
|
||||
** is to copy them to an array of a known type and use the array values.
|
||||
*/
|
||||
static unsigned int l_randomizePivot (void) {
|
||||
clock_t c = clock();
|
||||
time_t t = time(NULL);
|
||||
unsigned int buff[sof(c) + sof(t)];
|
||||
unsigned int i, rnd = 0;
|
||||
memcpy(buff, &c, sof(c) * sizeof(unsigned int));
|
||||
memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
|
||||
for (i = 0; i < sof(buff); i++)
|
||||
rnd += buff[i];
|
||||
return rnd;
|
||||
}
|
||||
|
||||
#if !defined(l_randomizePivot)
|
||||
#define l_randomizePivot(L) luaL_makeseed(L)
|
||||
#endif /* } */
|
||||
|
||||
|
||||
@ -263,8 +259,8 @@ static unsigned int l_randomizePivot (void) {
|
||||
|
||||
|
||||
static void set2 (lua_State *L, IdxT i, IdxT j) {
|
||||
lua_seti(L, 1, i);
|
||||
lua_seti(L, 1, j);
|
||||
seti(L, 1, i);
|
||||
seti(L, 1, j);
|
||||
}
|
||||
|
||||
|
||||
@ -301,15 +297,15 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
||||
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
|
||||
for (;;) {
|
||||
/* next loop: repeat ++i while a[i] < P */
|
||||
while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||
if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */
|
||||
while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||
if (l_unlikely(i == up - 1)) /* a[up - 1] < P == a[up - 1] */
|
||||
luaL_error(L, "invalid order function for sorting");
|
||||
lua_pop(L, 1); /* remove a[i] */
|
||||
}
|
||||
/* after the loop, a[i] >= P and a[lo .. i - 1] < P */
|
||||
/* after the loop, a[i] >= P and a[lo .. i - 1] < P (a) */
|
||||
/* next loop: repeat --j while P < a[j] */
|
||||
while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||
if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */
|
||||
while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||
if (l_unlikely(j < i)) /* j <= i - 1 and a[j] > P, contradicts (a) */
|
||||
luaL_error(L, "invalid order function for sorting");
|
||||
lua_pop(L, 1); /* remove a[j] */
|
||||
}
|
||||
@ -333,7 +329,7 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
||||
*/
|
||||
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
||||
IdxT r4 = (up - lo) / 4; /* range/4 */
|
||||
IdxT p = rnd % (r4 * 2) + (lo + r4);
|
||||
IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4);
|
||||
lua_assert(lo + r4 <= p && p <= up - r4);
|
||||
return p;
|
||||
}
|
||||
@ -342,14 +338,13 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
||||
/*
|
||||
** Quicksort algorithm (recursive function)
|
||||
*/
|
||||
static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||
unsigned int rnd) {
|
||||
static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
|
||||
while (lo < up) { /* loop for tail recursion */
|
||||
IdxT p; /* Pivot index */
|
||||
IdxT n; /* to be used later */
|
||||
/* sort elements 'lo', 'p', and 'up' */
|
||||
lua_geti(L, 1, lo);
|
||||
lua_geti(L, 1, up);
|
||||
geti(L, 1, lo);
|
||||
geti(L, 1, up);
|
||||
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
|
||||
set2(L, lo, up); /* swap a[lo] - a[up] */
|
||||
else
|
||||
@ -360,13 +355,13 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||
p = (lo + up)/2; /* middle element is a good pivot */
|
||||
else /* for larger intervals, it is worth a random pivot */
|
||||
p = choosePivot(lo, up, rnd);
|
||||
lua_geti(L, 1, p);
|
||||
lua_geti(L, 1, lo);
|
||||
geti(L, 1, p);
|
||||
geti(L, 1, lo);
|
||||
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
|
||||
set2(L, p, lo); /* swap a[p] - a[lo] */
|
||||
else {
|
||||
lua_pop(L, 1); /* remove a[lo] */
|
||||
lua_geti(L, 1, up);
|
||||
geti(L, 1, up);
|
||||
if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
|
||||
set2(L, p, up); /* swap a[up] - a[p] */
|
||||
else
|
||||
@ -374,9 +369,9 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||
}
|
||||
if (up - lo == 2) /* only 3 elements? */
|
||||
return; /* already sorted */
|
||||
lua_geti(L, 1, p); /* get middle element (Pivot) */
|
||||
geti(L, 1, p); /* get middle element (Pivot) */
|
||||
lua_pushvalue(L, -1); /* push Pivot */
|
||||
lua_geti(L, 1, up - 1); /* push a[up - 1] */
|
||||
geti(L, 1, up - 1); /* push a[up - 1] */
|
||||
set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
|
||||
p = partition(L, lo, up);
|
||||
/* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
|
||||
@ -391,7 +386,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
|
||||
}
|
||||
if ((up - lo) / 128 > n) /* partition too imbalanced? */
|
||||
rnd = l_randomizePivot(); /* try a new randomization */
|
||||
rnd = l_randomizePivot(L); /* try a new randomization */
|
||||
} /* tail call auxsort(L, lo, up, rnd) */
|
||||
}
|
||||
|
||||
@ -413,6 +408,7 @@ static int sort (lua_State *L) {
|
||||
|
||||
static const luaL_Reg tab_funcs[] = {
|
||||
{"concat", tconcat},
|
||||
{"create", tcreate},
|
||||
{"insert", tinsert},
|
||||
{"pack", tpack},
|
||||
{"unpack", tunpack},
|
||||
|
||||
506
ltests.c
506
ltests.c
@ -73,8 +73,9 @@ static void badexit (const char *fmt, const char *s1, const char *s2) {
|
||||
|
||||
|
||||
static int tpanic (lua_State *L) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "error object is not a string";
|
||||
const char *msg = (lua_type(L, -1) == LUA_TSTRING)
|
||||
? lua_tostring(L, -1)
|
||||
: "error object is not a string";
|
||||
return (badexit("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
msg, NULL),
|
||||
0); /* do not return to Lua */
|
||||
@ -163,13 +164,13 @@ static void warnf (void *ud, const char *msg, int tocont) {
|
||||
|
||||
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||
|
||||
typedef union Header {
|
||||
typedef union memHeader {
|
||||
LUAI_MAXALIGN;
|
||||
struct {
|
||||
size_t size;
|
||||
int type;
|
||||
} d;
|
||||
} Header;
|
||||
} memHeader;
|
||||
|
||||
|
||||
#if !defined(EXTERNMEMCHECK)
|
||||
@ -192,14 +193,14 @@ Memcontrol l_memcontrol =
|
||||
{0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
|
||||
|
||||
|
||||
static void freeblock (Memcontrol *mc, Header *block) {
|
||||
static void freeblock (Memcontrol *mc, memHeader *block) {
|
||||
if (block) {
|
||||
size_t size = block->d.size;
|
||||
int i;
|
||||
for (i = 0; i < MARKSIZE; i++) /* check marks after block */
|
||||
lua_assert(*(cast_charp(block + 1) + size + i) == MARK);
|
||||
mc->objcount[block->d.type]--;
|
||||
fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */
|
||||
fillmem(block, sizeof(memHeader) + size + MARKSIZE); /* erase block */
|
||||
free(block); /* actually free block */
|
||||
mc->numblocks--; /* update counts */
|
||||
mc->total -= size;
|
||||
@ -209,14 +210,14 @@ static void freeblock (Memcontrol *mc, Header *block) {
|
||||
|
||||
void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
||||
Memcontrol *mc = cast(Memcontrol *, ud);
|
||||
Header *block = cast(Header *, b);
|
||||
memHeader *block = cast(memHeader *, b);
|
||||
int type;
|
||||
if (mc->memlimit == 0) { /* first time? */
|
||||
char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
|
||||
mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
|
||||
}
|
||||
if (block == NULL) {
|
||||
type = (oldsize < LUA_NUMTAGS) ? oldsize : 0;
|
||||
type = (oldsize < LUA_NUMTYPES) ? cast_int(oldsize) : 0;
|
||||
oldsize = 0;
|
||||
}
|
||||
else {
|
||||
@ -240,12 +241,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
||||
if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
|
||||
return NULL; /* fake a memory allocation error */
|
||||
else {
|
||||
Header *newblock;
|
||||
memHeader *newblock;
|
||||
int i;
|
||||
size_t commonsize = (oldsize < size) ? oldsize : size;
|
||||
size_t realsize = sizeof(Header) + size + MARKSIZE;
|
||||
size_t realsize = sizeof(memHeader) + size + MARKSIZE;
|
||||
if (realsize < size) return NULL; /* arithmetic overflow! */
|
||||
newblock = cast(Header *, malloc(realsize)); /* alloc a new block */
|
||||
newblock = cast(memHeader *, malloc(realsize)); /* alloc a new block */
|
||||
if (newblock == NULL)
|
||||
return NULL; /* really out of memory? */
|
||||
if (block) {
|
||||
@ -297,13 +298,13 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
|
||||
if (isdead(g,t)) return 0;
|
||||
if (issweepphase(g))
|
||||
return 1; /* no invariants */
|
||||
else if (g->gckind == KGC_INC)
|
||||
else if (g->gckind != KGC_GENMINOR)
|
||||
return !(isblack(f) && iswhite(t)); /* basic incremental invariant */
|
||||
else { /* generational mode */
|
||||
if ((getage(f) == G_OLD && isblack(f)) && !isold(t))
|
||||
return 0;
|
||||
if (((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && isblack(f)) &&
|
||||
getage(t) == G_NEW)
|
||||
if ((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) &&
|
||||
getage(t) == G_NEW)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
@ -324,6 +325,46 @@ void lua_printobj (lua_State *L, struct GCObject *o) {
|
||||
printobj(G(L), o);
|
||||
}
|
||||
|
||||
|
||||
void lua_printvalue (TValue *v) {
|
||||
switch (ttypetag(v)) {
|
||||
case LUA_VNUMINT: case LUA_VNUMFLT: {
|
||||
char buff[LUA_N2SBUFFSZ];
|
||||
unsigned len = luaO_tostringbuff(v, buff);
|
||||
buff[len] = '\0';
|
||||
printf("%s", buff);
|
||||
break;
|
||||
}
|
||||
case LUA_VSHRSTR:
|
||||
printf("'%s'", getstr(tsvalue(v))); break;
|
||||
case LUA_VLNGSTR:
|
||||
printf("'%.30s...'", getstr(tsvalue(v))); break;
|
||||
case LUA_VFALSE:
|
||||
printf("%s", "false"); break;
|
||||
case LUA_VTRUE:
|
||||
printf("%s", "true"); break;
|
||||
case LUA_VLIGHTUSERDATA:
|
||||
printf("light udata: %p", pvalue(v)); break;
|
||||
case LUA_VUSERDATA:
|
||||
printf("full udata: %p", uvalue(v)); break;
|
||||
case LUA_VNIL:
|
||||
printf("nil"); break;
|
||||
case LUA_VLCF:
|
||||
printf("light C function: %p", fvalue(v)); break;
|
||||
case LUA_VCCL:
|
||||
printf("C closure: %p", clCvalue(v)); break;
|
||||
case LUA_VLCL:
|
||||
printf("Lua function: %p", clLvalue(v)); break;
|
||||
case LUA_VTHREAD:
|
||||
printf("thread: %p", thvalue(v)); break;
|
||||
case LUA_VTABLE:
|
||||
printf("table: %p", hvalue(v)); break;
|
||||
default:
|
||||
lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
||||
int r1 = testobjref1(g, f, t);
|
||||
if (!r1) {
|
||||
@ -358,16 +399,19 @@ static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
|
||||
|
||||
static void checktable (global_State *g, Table *h) {
|
||||
unsigned int i;
|
||||
unsigned int asize = luaH_realasize(h);
|
||||
unsigned int asize = h->asize;
|
||||
Node *n, *limit = gnode(h, sizenode(h));
|
||||
GCObject *hgc = obj2gco(h);
|
||||
checkobjrefN(g, hgc, h->metatable);
|
||||
for (i = 0; i < asize; i++)
|
||||
checkvalref(g, hgc, &h->array[i]);
|
||||
for (i = 0; i < asize; i++) {
|
||||
TValue aux;
|
||||
arr2obj(h, i, &aux);
|
||||
checkvalref(g, hgc, &aux);
|
||||
}
|
||||
for (n = gnode(h, 0); n < limit; n++) {
|
||||
if (!isempty(gval(n))) {
|
||||
TValue k;
|
||||
getnodekey(g->mainthread, &k, n);
|
||||
getnodekey(mainthread(g), &k, n);
|
||||
assert(!keyisnil(n));
|
||||
checkvalref(g, hgc, &k);
|
||||
checkvalref(g, hgc, gval(n));
|
||||
@ -436,7 +480,7 @@ static int lua_checkpc (CallInfo *ci) {
|
||||
}
|
||||
|
||||
|
||||
static void checkstack (global_State *g, lua_State *L1) {
|
||||
static void check_stack (global_State *g, lua_State *L1) {
|
||||
StkId o;
|
||||
CallInfo *ci;
|
||||
UpVal *uv;
|
||||
@ -473,7 +517,7 @@ static void checkrefs (global_State *g, GCObject *o) {
|
||||
break;
|
||||
}
|
||||
case LUA_VTHREAD: {
|
||||
checkstack(g, gco2th(o));
|
||||
check_stack(g, gco2th(o));
|
||||
break;
|
||||
}
|
||||
case LUA_VLCL: {
|
||||
@ -507,7 +551,8 @@ static void checkrefs (global_State *g, GCObject *o) {
|
||||
** * objects must be old enough for their lists ('listage').
|
||||
** * old objects cannot be white.
|
||||
** * old objects must be black, except for 'touched1', 'old0',
|
||||
** threads, and open upvalues.
|
||||
** threads, and open upvalues.
|
||||
** * 'touched1' objects must be gray.
|
||||
*/
|
||||
static void checkobject (global_State *g, GCObject *o, int maybedead,
|
||||
int listage) {
|
||||
@ -515,23 +560,24 @@ static void checkobject (global_State *g, GCObject *o, int maybedead,
|
||||
assert(maybedead);
|
||||
else {
|
||||
assert(g->gcstate != GCSpause || iswhite(o));
|
||||
if (g->gckind == KGC_GEN) { /* generational mode? */
|
||||
if (g->gckind == KGC_GENMINOR) { /* generational mode? */
|
||||
assert(getage(o) >= listage);
|
||||
assert(!iswhite(o) || !isold(o));
|
||||
if (isold(o)) {
|
||||
assert(!iswhite(o));
|
||||
assert(isblack(o) ||
|
||||
getage(o) == G_TOUCHED1 ||
|
||||
getage(o) == G_OLD0 ||
|
||||
o->tt == LUA_VTHREAD ||
|
||||
(o->tt == LUA_VUPVAL && upisopen(gco2upv(o))));
|
||||
}
|
||||
assert(getage(o) != G_TOUCHED1 || isgray(o));
|
||||
}
|
||||
checkrefs(g, o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static lu_mem checkgraylist (global_State *g, GCObject *o) {
|
||||
static l_mem checkgraylist (global_State *g, GCObject *o) {
|
||||
int total = 0; /* count number of elements in the list */
|
||||
cast_void(g); /* better to keep it if we need to print an object */
|
||||
while (o) {
|
||||
@ -560,8 +606,8 @@ static lu_mem checkgraylist (global_State *g, GCObject *o) {
|
||||
/*
|
||||
** Check objects in gray lists.
|
||||
*/
|
||||
static lu_mem checkgrays (global_State *g) {
|
||||
int total = 0; /* count number of elements in all lists */
|
||||
static l_mem checkgrays (global_State *g) {
|
||||
l_mem total = 0; /* count number of elements in all lists */
|
||||
if (!keepinvariant(g)) return total;
|
||||
total += checkgraylist(g, g->gray);
|
||||
total += checkgraylist(g, g->grayagain);
|
||||
@ -577,7 +623,7 @@ static lu_mem checkgrays (global_State *g) {
|
||||
** 'count' and check its TESTBIT. (It must have been previously set by
|
||||
** 'checkgraylist'.)
|
||||
*/
|
||||
static void incifingray (global_State *g, GCObject *o, lu_mem *count) {
|
||||
static void incifingray (global_State *g, GCObject *o, l_mem *count) {
|
||||
if (!keepinvariant(g))
|
||||
return; /* gray lists not being kept in these phases */
|
||||
if (o->tt == LUA_VUPVAL) {
|
||||
@ -594,10 +640,10 @@ static void incifingray (global_State *g, GCObject *o, lu_mem *count) {
|
||||
}
|
||||
|
||||
|
||||
static lu_mem checklist (global_State *g, int maybedead, int tof,
|
||||
static l_mem checklist (global_State *g, int maybedead, int tof,
|
||||
GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) {
|
||||
GCObject *o;
|
||||
lu_mem total = 0; /* number of object that should be in gray lists */
|
||||
l_mem total = 0; /* number of object that should be in gray lists */
|
||||
for (o = newl; o != survival; o = o->next) {
|
||||
checkobject(g, o, maybedead, G_NEW);
|
||||
incifingray(g, o, &total);
|
||||
@ -626,10 +672,10 @@ int lua_checkmemory (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
GCObject *o;
|
||||
int maybedead;
|
||||
lu_mem totalin; /* total of objects that are in gray lists */
|
||||
lu_mem totalshould; /* total of objects that should be in gray lists */
|
||||
l_mem totalin; /* total of objects that are in gray lists */
|
||||
l_mem totalshould; /* total of objects that should be in gray lists */
|
||||
if (keepinvariant(g)) {
|
||||
assert(!iswhite(g->mainthread));
|
||||
assert(!iswhite(mainthread(g)));
|
||||
assert(!iswhite(gcvalue(&g->l_registry)));
|
||||
}
|
||||
assert(!isdead(g, gcvalue(&g->l_registry)));
|
||||
@ -691,6 +737,11 @@ static char *buildop (Proto *p, int pc, char *buff) {
|
||||
GETARG_A(i), GETARG_B(i), GETARG_C(i),
|
||||
GETARG_k(i) ? " (k)" : "");
|
||||
break;
|
||||
case ivABC:
|
||||
sprintf(buff, "%-12s%4d %4d %4d%s", name,
|
||||
GETARG_A(i), GETARG_vB(i), GETARG_vC(i),
|
||||
GETARG_k(i) ? " (k)" : "");
|
||||
break;
|
||||
case iABx:
|
||||
sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
|
||||
break;
|
||||
@ -811,35 +862,68 @@ static int listlocals (lua_State *L) {
|
||||
|
||||
|
||||
|
||||
static void printstack (lua_State *L) {
|
||||
void lua_printstack (lua_State *L) {
|
||||
int i;
|
||||
int n = lua_gettop(L);
|
||||
printf("stack: >>\n");
|
||||
for (i = 1; i <= n; i++) {
|
||||
printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
|
||||
lua_pop(L, 1);
|
||||
printf("%3d: ", i);
|
||||
lua_printvalue(s2v(L->ci->func.p + i));
|
||||
printf("\n");
|
||||
}
|
||||
printf("<<\n");
|
||||
}
|
||||
|
||||
|
||||
int lua_printallstack (lua_State *L) {
|
||||
StkId p;
|
||||
int i = 1;
|
||||
CallInfo *ci = &L->base_ci;
|
||||
printf("stack: >>\n");
|
||||
for (p = L->stack.p; p < L->top.p; p++) {
|
||||
if (ci != NULL && p == ci->func.p) {
|
||||
printf(" ---\n");
|
||||
if (ci == L->ci)
|
||||
ci = NULL; /* printed last frame */
|
||||
else
|
||||
ci = ci->next;
|
||||
}
|
||||
printf("%3d: ", i++);
|
||||
lua_printvalue(s2v(p));
|
||||
printf("\n");
|
||||
}
|
||||
printf("<<\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int get_limits (lua_State *L) {
|
||||
lua_createtable(L, 0, 6);
|
||||
lua_createtable(L, 0, 5);
|
||||
setnameval(L, "IS32INT", LUAI_IS32INT);
|
||||
setnameval(L, "MAXARG_Ax", MAXARG_Ax);
|
||||
setnameval(L, "MAXARG_Bx", MAXARG_Bx);
|
||||
setnameval(L, "OFFSET_sBx", OFFSET_sBx);
|
||||
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int get_sizes (lua_State *L) {
|
||||
lua_newtable(L);
|
||||
setnameval(L, "Lua state", sizeof(lua_State));
|
||||
setnameval(L, "global state", sizeof(global_State));
|
||||
setnameval(L, "TValue", sizeof(TValue));
|
||||
setnameval(L, "Node", sizeof(Node));
|
||||
setnameval(L, "stack Value", sizeof(StackValue));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int mem_query (lua_State *L) {
|
||||
if (lua_isnone(L, 1)) {
|
||||
lua_pushinteger(L, l_memcontrol.total);
|
||||
lua_pushinteger(L, l_memcontrol.numblocks);
|
||||
lua_pushinteger(L, l_memcontrol.maxmem);
|
||||
lua_pushinteger(L, cast_Integer(l_memcontrol.total));
|
||||
lua_pushinteger(L, cast_Integer(l_memcontrol.numblocks));
|
||||
lua_pushinteger(L, cast_Integer(l_memcontrol.maxmem));
|
||||
return 3;
|
||||
}
|
||||
else if (lua_isnumber(L, 1)) {
|
||||
@ -851,9 +935,9 @@ static int mem_query (lua_State *L) {
|
||||
else {
|
||||
const char *t = luaL_checkstring(L, 1);
|
||||
int i;
|
||||
for (i = LUA_NUMTAGS - 1; i >= 0; i--) {
|
||||
for (i = LUA_NUMTYPES - 1; i >= 0; i--) {
|
||||
if (strcmp(t, ttypename(i)) == 0) {
|
||||
lua_pushinteger(L, l_memcontrol.objcount[i]);
|
||||
lua_pushinteger(L, cast_Integer(l_memcontrol.objcount[i]));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -864,9 +948,9 @@ static int mem_query (lua_State *L) {
|
||||
|
||||
static int alloc_count (lua_State *L) {
|
||||
if (lua_isnone(L, 1))
|
||||
l_memcontrol.countlimit = ~0L;
|
||||
l_memcontrol.countlimit = cast(unsigned long, ~0L);
|
||||
else
|
||||
l_memcontrol.countlimit = luaL_checkinteger(L, 1);
|
||||
l_memcontrol.countlimit = cast(unsigned long, luaL_checkinteger(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -934,81 +1018,138 @@ static int gc_printobj (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static const char *const statenames[] = {
|
||||
"propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj",
|
||||
"sweeptobefnz", "sweepend", "callfin", "pause", ""};
|
||||
|
||||
static int gc_state (lua_State *L) {
|
||||
static const char *statenames[] = {
|
||||
"propagate", "atomic", "enteratomic", "sweepallgc", "sweepfinobj",
|
||||
"sweeptobefnz", "sweepend", "callfin", "pause", ""};
|
||||
static const int states[] = {
|
||||
GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj,
|
||||
GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1};
|
||||
int option = states[luaL_checkoption(L, 1, "", statenames)];
|
||||
global_State *g = G(L);
|
||||
if (option == -1) {
|
||||
lua_pushstring(L, statenames[G(L)->gcstate]);
|
||||
lua_pushstring(L, statenames[g->gcstate]);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
global_State *g = G(L);
|
||||
if (G(L)->gckind == KGC_GEN)
|
||||
if (g->gckind != KGC_INC)
|
||||
luaL_error(L, "cannot change states in generational mode");
|
||||
lua_lock(L);
|
||||
if (option < g->gcstate) { /* must cross 'pause'? */
|
||||
luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */
|
||||
luaC_runtilstate(L, GCSpause, 1); /* run until pause */
|
||||
}
|
||||
luaC_runtilstate(L, bitmask(option));
|
||||
lua_assert(G(L)->gcstate == option);
|
||||
luaC_runtilstate(L, option, 0); /* do not skip propagation state */
|
||||
lua_assert(g->gcstate == option);
|
||||
lua_unlock(L);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int tracinggc = 0;
|
||||
void luai_tracegctest (lua_State *L, int first) {
|
||||
if (!tracinggc) return;
|
||||
else {
|
||||
global_State *g = G(L);
|
||||
lua_unlock(L);
|
||||
g->gcstp = GCSTPGC;
|
||||
lua_checkstack(L, 10);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "tracegc");
|
||||
lua_pushboolean(L, first);
|
||||
lua_call(L, 1, 0);
|
||||
g->gcstp = 0;
|
||||
lua_lock(L);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int tracegc (lua_State *L) {
|
||||
if (lua_isnil(L, 1))
|
||||
tracinggc = 0;
|
||||
else {
|
||||
tracinggc = 1;
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, "tracegc");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int hash_query (lua_State *L) {
|
||||
if (lua_isnone(L, 2)) {
|
||||
TString *ts;
|
||||
luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
|
||||
lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
|
||||
ts = tsvalue(obj_at(L, 1));
|
||||
if (ts->tt == LUA_VLNGSTR)
|
||||
luaS_hashlongstr(ts); /* make sure long string has a hash */
|
||||
lua_pushinteger(L, cast_int(ts->hash));
|
||||
}
|
||||
else {
|
||||
TValue *o = obj_at(L, 1);
|
||||
Table *t;
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
t = hvalue(obj_at(L, 2));
|
||||
lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
|
||||
lua_pushinteger(L, cast_Integer(luaH_mainposition(t, o) - t->node));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int stacklevel (lua_State *L) {
|
||||
unsigned long a = 0;
|
||||
lua_pushinteger(L, (L->top.p - L->stack.p));
|
||||
int a = 0;
|
||||
lua_pushinteger(L, cast_Integer(L->top.p - L->stack.p));
|
||||
lua_pushinteger(L, stacksize(L));
|
||||
lua_pushinteger(L, L->nCcalls);
|
||||
lua_pushinteger(L, cast_Integer(L->nCcalls));
|
||||
lua_pushinteger(L, L->nci);
|
||||
lua_pushinteger(L, (unsigned long)&a);
|
||||
lua_pushinteger(L, (lua_Integer)(size_t)&a);
|
||||
return 5;
|
||||
}
|
||||
|
||||
|
||||
static int resetCI (lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
while (ci->next != NULL) {
|
||||
CallInfo *tofree = ci->next;
|
||||
ci->next = ci->next->next;
|
||||
luaM_free(L, tofree);
|
||||
L->nci--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int reallocstack (lua_State *L) {
|
||||
int n = cast_int(luaL_checkinteger(L, 1));
|
||||
lua_lock(L);
|
||||
luaD_reallocstack(L, cast_int(L->top.p - L->stack.p) + n, 1);
|
||||
lua_unlock(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int table_query (lua_State *L) {
|
||||
const Table *t;
|
||||
int i = cast_int(luaL_optinteger(L, 2, -1));
|
||||
unsigned int asize;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
t = hvalue(obj_at(L, 1));
|
||||
asize = luaH_realasize(t);
|
||||
asize = t->asize;
|
||||
if (i == -1) {
|
||||
lua_pushinteger(L, asize);
|
||||
lua_pushinteger(L, allocsizenode(t));
|
||||
lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node);
|
||||
lua_pushinteger(L, t->alimit);
|
||||
return 4;
|
||||
lua_pushinteger(L, cast_Integer(asize));
|
||||
lua_pushinteger(L, cast_Integer(allocsizenode(t)));
|
||||
lua_pushinteger(L, cast_Integer(asize > 0 ? *lenhint(t) : 0));
|
||||
return 3;
|
||||
}
|
||||
else if ((unsigned int)i < asize) {
|
||||
else if (cast_uint(i) < asize) {
|
||||
lua_pushinteger(L, i);
|
||||
pushobject(L, &t->array[i]);
|
||||
if (!tagisempty(*getArrTag(t, i)))
|
||||
arr2obj(t, cast_uint(i), s2v(L->top.p));
|
||||
else
|
||||
setnilvalue(s2v(L->top.p));
|
||||
api_incr_top(L);
|
||||
lua_pushnil(L);
|
||||
}
|
||||
else if ((i -= asize) < sizenode(t)) {
|
||||
else if (cast_uint(i -= cast_int(asize)) < sizenode(t)) {
|
||||
TValue k;
|
||||
getnodekey(L, &k, gnode(t, i));
|
||||
if (!isempty(gval(gnode(t, i))) ||
|
||||
@ -1018,16 +1159,45 @@ static int table_query (lua_State *L) {
|
||||
}
|
||||
else
|
||||
lua_pushliteral(L, "<undef>");
|
||||
pushobject(L, gval(gnode(t, i)));
|
||||
if (gnext(&t->node[i]) != 0)
|
||||
lua_pushinteger(L, gnext(&t->node[i]));
|
||||
if (!isempty(gval(gnode(t, i))))
|
||||
pushobject(L, gval(gnode(t, i)));
|
||||
else
|
||||
lua_pushnil(L);
|
||||
lua_pushinteger(L, gnext(&t->node[i]));
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int gc_query (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
lua_pushstring(L, g->gckind == KGC_INC ? "inc"
|
||||
: g->gckind == KGC_GENMAJOR ? "genmajor"
|
||||
: "genminor");
|
||||
lua_pushstring(L, statenames[g->gcstate]);
|
||||
lua_pushinteger(L, cast_st2S(gettotalbytes(g)));
|
||||
lua_pushinteger(L, cast_st2S(g->GCdebt));
|
||||
lua_pushinteger(L, cast_st2S(g->GCmarked));
|
||||
lua_pushinteger(L, cast_st2S(g->GCmajorminor));
|
||||
return 6;
|
||||
}
|
||||
|
||||
|
||||
static int test_codeparam (lua_State *L) {
|
||||
lua_Integer p = luaL_checkinteger(L, 1);
|
||||
lua_pushinteger(L, luaO_codeparam(cast_uint(p)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_applyparam (lua_State *L) {
|
||||
lua_Integer p = luaL_checkinteger(L, 1);
|
||||
lua_Integer x = luaL_checkinteger(L, 2);
|
||||
lua_pushinteger(L, cast_Integer(luaO_applyparam(cast_byte(p), x)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int string_query (lua_State *L) {
|
||||
stringtable *tb = &G(L)->strt;
|
||||
int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
|
||||
@ -1050,27 +1220,39 @@ static int string_query (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int getreftable (lua_State *L) {
|
||||
if (lua_istable(L, 2)) /* is there a table as second argument? */
|
||||
return 2; /* use it as the table */
|
||||
else
|
||||
return LUA_REGISTRYINDEX; /* default is to use the register */
|
||||
}
|
||||
|
||||
|
||||
static int tref (lua_State *L) {
|
||||
int t = getreftable(L);
|
||||
int level = lua_gettop(L);
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
|
||||
lua_pushinteger(L, luaL_ref(L, t));
|
||||
cast_void(level); /* to avoid warnings */
|
||||
lua_assert(lua_gettop(L) == level+1); /* +1 for result */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int getref (lua_State *L) {
|
||||
int t = getreftable(L);
|
||||
int level = lua_gettop(L);
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
|
||||
lua_rawgeti(L, t, luaL_checkinteger(L, 1));
|
||||
cast_void(level); /* to avoid warnings */
|
||||
lua_assert(lua_gettop(L) == level+1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int unref (lua_State *L) {
|
||||
int t = getreftable(L);
|
||||
int level = lua_gettop(L);
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
|
||||
luaL_unref(L, t, cast_int(luaL_checkinteger(L, 1)));
|
||||
cast_void(level); /* to avoid warnings */
|
||||
lua_assert(lua_gettop(L) == level);
|
||||
return 0;
|
||||
@ -1096,7 +1278,7 @@ static int upvalue (lua_State *L) {
|
||||
|
||||
static int newuserdata (lua_State *L) {
|
||||
size_t size = cast_sizet(luaL_optinteger(L, 1, 0));
|
||||
int nuv = luaL_optinteger(L, 2, 0);
|
||||
int nuv = cast_int(luaL_optinteger(L, 2, 0));
|
||||
char *p = cast_charp(lua_newuserdatauv(L, size, nuv));
|
||||
while (size--) *p++ = '\0';
|
||||
return 1;
|
||||
@ -1111,7 +1293,7 @@ static int pushuserdata (lua_State *L) {
|
||||
|
||||
|
||||
static int udataval (lua_State *L) {
|
||||
lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
|
||||
lua_pushinteger(L, cast_st2S(cast_sizet(lua_touserdata(L, 1))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -1147,10 +1329,16 @@ static int num2int (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int makeseed (lua_State *L) {
|
||||
lua_pushinteger(L, cast_Integer(luaL_makeseed(L)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int newstate (lua_State *L) {
|
||||
void *ud;
|
||||
lua_Alloc f = lua_getallocf(L, &ud);
|
||||
lua_State *L1 = lua_newstate(f, ud);
|
||||
lua_State *L1 = lua_newstate(f, ud, 0);
|
||||
if (L1) {
|
||||
lua_atpanic(L1, tpanic);
|
||||
lua_pushlightuserdata(L, L1);
|
||||
@ -1169,31 +1357,16 @@ static lua_State *getstate (lua_State *L) {
|
||||
|
||||
|
||||
static int loadlib (lua_State *L) {
|
||||
static const luaL_Reg libs[] = {
|
||||
{LUA_GNAME, luaopen_base},
|
||||
{"coroutine", luaopen_coroutine},
|
||||
{"debug", luaopen_debug},
|
||||
{"io", luaopen_io},
|
||||
{"os", luaopen_os},
|
||||
{"math", luaopen_math},
|
||||
{"string", luaopen_string},
|
||||
{"table", luaopen_table},
|
||||
{"T", luaB_opentests},
|
||||
{NULL, NULL}
|
||||
};
|
||||
lua_State *L1 = getstate(L);
|
||||
int i;
|
||||
luaL_requiref(L1, "package", luaopen_package, 0);
|
||||
int load = cast_int(luaL_checkinteger(L, 2));
|
||||
int preload = cast_int(luaL_checkinteger(L, 3));
|
||||
luaL_openselectedlibs(L1, load, preload);
|
||||
luaL_requiref(L1, "T", luaB_opentests, 0);
|
||||
lua_assert(lua_type(L1, -1) == LUA_TTABLE);
|
||||
/* 'requiref' should not reload module already loaded... */
|
||||
luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */
|
||||
luaL_requiref(L1, "T", NULL, 1); /* seg. fault if it reloads */
|
||||
/* ...but should return the same module */
|
||||
lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ));
|
||||
luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||
for (i = 0; libs[i].name; i++) {
|
||||
lua_pushcfunction(L1, libs[i].func);
|
||||
lua_setfield(L1, -2, libs[i].name);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1259,9 +1432,9 @@ static int checkpanic (lua_State *L) {
|
||||
lua_Alloc f = lua_getallocf(L, &ud);
|
||||
b.paniccode = luaL_optstring(L, 2, "");
|
||||
b.L = L;
|
||||
L1 = lua_newstate(f, ud); /* create new state */
|
||||
L1 = lua_newstate(f, ud, 0); /* create new state */
|
||||
if (L1 == NULL) { /* error? */
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, MEMERRMSG);
|
||||
return 1;
|
||||
}
|
||||
lua_atpanic(L1, panicback); /* set its panic function */
|
||||
@ -1280,6 +1453,37 @@ static int checkpanic (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int externKstr (lua_State *L) {
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, 1, &len);
|
||||
lua_pushexternalstring(L, s, len, NULL, NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Create a buffer with the content of a given string and then
|
||||
** create an external string using that buffer. Use the allocation
|
||||
** function from Lua to create and free the buffer.
|
||||
*/
|
||||
static int externstr (lua_State *L) {
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, 1, &len);
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud); /* get allocation function */
|
||||
/* create the buffer */
|
||||
char *buff = cast_charp((*allocf)(ud, NULL, 0, len + 1));
|
||||
if (buff == NULL) { /* memory error? */
|
||||
lua_pushliteral(L, "not enough memory");
|
||||
lua_error(L); /* raise a memory error */
|
||||
}
|
||||
/* copy string content to buffer, including ending 0 */
|
||||
memcpy(buff, s, (len + 1) * sizeof(char));
|
||||
/* create external string */
|
||||
lua_pushexternalstring(L, buff, len, allocf, ud);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {====================================================================
|
||||
@ -1318,6 +1522,16 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
|
||||
(*pc)++;
|
||||
return res;
|
||||
}
|
||||
else if (**pc == '!') {
|
||||
(*pc)++;
|
||||
if (**pc == 'G')
|
||||
res = LUA_RIDX_GLOBALS;
|
||||
else if (**pc == 'M')
|
||||
res = LUA_RIDX_MAINTHREAD;
|
||||
else lua_assert(0);
|
||||
(*pc)++;
|
||||
return res;
|
||||
}
|
||||
else if (**pc == '-') {
|
||||
sig = -1;
|
||||
(*pc)++;
|
||||
@ -1352,15 +1566,20 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
|
||||
skip(pc);
|
||||
switch (*(*pc)++) {
|
||||
case 'R': return LUA_REGISTRYINDEX;
|
||||
case 'G': return luaL_error(L, "deprecated index 'G'");
|
||||
case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
|
||||
default: (*pc)--; return getnum_aux(L, L1, pc);
|
||||
default: {
|
||||
int n;
|
||||
(*pc)--; /* to read again */
|
||||
n = getnum_aux(L, L1, pc);
|
||||
if (n == 0) return 0;
|
||||
else return lua_absindex(L1, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN",
|
||||
"ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"};
|
||||
"ERRSYNTAX", MEMERRMSG, "ERRERR"};
|
||||
|
||||
/*
|
||||
** Avoid these stat codes from being collected, to avoid possible
|
||||
@ -1403,17 +1622,17 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
const char *inst = getstring;
|
||||
if EQ("") return 0;
|
||||
else if EQ("absindex") {
|
||||
lua_pushnumber(L1, lua_absindex(L1, getindex));
|
||||
lua_pushinteger(L1, getindex);
|
||||
}
|
||||
else if EQ("append") {
|
||||
int t = getindex;
|
||||
int i = lua_rawlen(L1, t);
|
||||
int i = cast_int(lua_rawlen(L1, t));
|
||||
lua_rawseti(L1, t, i + 1);
|
||||
}
|
||||
else if EQ("arith") {
|
||||
int op;
|
||||
skip(&pc);
|
||||
op = strchr(ops, *pc++) - ops;
|
||||
op = cast_int(strchr(ops, *pc++) - ops);
|
||||
lua_arith(L1, op);
|
||||
}
|
||||
else if EQ("call") {
|
||||
@ -1455,11 +1674,12 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
}
|
||||
else if EQ("func2num") {
|
||||
lua_CFunction func = lua_tocfunction(L1, getindex);
|
||||
lua_pushnumber(L1, cast_sizet(func));
|
||||
lua_pushinteger(L1, cast_st2S(cast_sizet(func)));
|
||||
}
|
||||
else if EQ("getfield") {
|
||||
int t = getindex;
|
||||
lua_getfield(L1, t, getstring);
|
||||
int tp = lua_getfield(L1, t, getstring);
|
||||
lua_assert(tp == lua_type(L1, -1));
|
||||
}
|
||||
else if EQ("getglobal") {
|
||||
lua_getglobal(L1, getstring);
|
||||
@ -1469,7 +1689,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
lua_pushnil(L1);
|
||||
}
|
||||
else if EQ("gettable") {
|
||||
lua_gettable(L1, getindex);
|
||||
int tp = lua_gettable(L1, getindex);
|
||||
lua_assert(tp == lua_type(L1, -1));
|
||||
}
|
||||
else if EQ("gettop") {
|
||||
lua_pushinteger(L1, lua_gettop(L1));
|
||||
@ -1520,8 +1741,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
||||
}
|
||||
else if EQ("loadstring") {
|
||||
const char *s = luaL_checkstring(L1, getnum);
|
||||
luaL_loadstring(L1, s);
|
||||
size_t slen;
|
||||
const char *s = luaL_checklstring(L1, getnum, &slen);
|
||||
const char *name = getstring;
|
||||
const char *mode = getstring;
|
||||
luaL_loadbufferx(L1, s, slen, name, mode);
|
||||
}
|
||||
else if EQ("newmetatable") {
|
||||
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
||||
@ -1533,16 +1757,16 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
lua_newthread(L1);
|
||||
}
|
||||
else if EQ("resetthread") {
|
||||
lua_pushinteger(L1, lua_resetthread(L1, L));
|
||||
lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */
|
||||
}
|
||||
else if EQ("newuserdata") {
|
||||
lua_newuserdata(L1, getnum);
|
||||
lua_newuserdata(L1, cast_sizet(getnum));
|
||||
}
|
||||
else if EQ("next") {
|
||||
lua_next(L1, -2);
|
||||
}
|
||||
else if EQ("objsize") {
|
||||
lua_pushinteger(L1, lua_rawlen(L1, getindex));
|
||||
lua_pushinteger(L1, l_castU2S(lua_rawlen(L1, getindex)));
|
||||
}
|
||||
else if EQ("pcall") {
|
||||
int narg = getnum;
|
||||
@ -1561,10 +1785,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
else if EQ("printstack") {
|
||||
int n = getnum;
|
||||
if (n != 0) {
|
||||
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
||||
lua_pop(L1, 1);
|
||||
lua_printvalue(s2v(L->ci->func.p + n));
|
||||
printf("\n");
|
||||
}
|
||||
else printstack(L1);
|
||||
else lua_printstack(L1);
|
||||
}
|
||||
else if EQ("print") {
|
||||
const char *msg = getstring;
|
||||
@ -1649,6 +1873,17 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
int nres;
|
||||
status = lua_resume(lua_tothread(L1, i), L, getnum, &nres);
|
||||
}
|
||||
else if EQ("traceback") {
|
||||
const char *msg = getstring;
|
||||
int level = getnum;
|
||||
luaL_traceback(L1, L1, msg, level);
|
||||
}
|
||||
else if EQ("threadstatus") {
|
||||
lua_pushstring(L1, statcodes[lua_status(L1)]);
|
||||
}
|
||||
else if EQ("alloccount") {
|
||||
l_memcontrol.countlimit = cast_uint(getnum);
|
||||
}
|
||||
else if EQ("return") {
|
||||
int n = getnum;
|
||||
if (L1 != L) {
|
||||
@ -1775,6 +2010,10 @@ static struct X { int x; } x;
|
||||
else if EQ("closeslot") {
|
||||
lua_closeslot(L1, getnum);
|
||||
}
|
||||
else if EQ("argerror") {
|
||||
int arg = getnum;
|
||||
luaL_argerror(L1, arg, getstring);
|
||||
}
|
||||
else luaL_error(L, "unknown instruction %s", buff);
|
||||
}
|
||||
return 0;
|
||||
@ -1808,9 +2047,9 @@ static int Cfunc (lua_State *L) {
|
||||
static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
|
||||
lua_pushstring(L, statcodes[status]);
|
||||
lua_setglobal(L, "status");
|
||||
lua_pushinteger(L, ctx);
|
||||
lua_pushinteger(L, cast_Integer(ctx));
|
||||
lua_setglobal(L, "ctx");
|
||||
return runC(L, L, lua_tostring(L, ctx));
|
||||
return runC(L, L, lua_tostring(L, cast_int(ctx)));
|
||||
}
|
||||
|
||||
|
||||
@ -1903,6 +2142,25 @@ static int coresume (lua_State *L) {
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(LUA_USE_POSIX)
|
||||
|
||||
#define nonblock NULL
|
||||
|
||||
#else
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static int nonblock (lua_State *L) {
|
||||
FILE *f = cast(luaL_Stream*, luaL_checkudata(L, 1, LUA_FILEHANDLE))->f;
|
||||
int fd = fileno(f);
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(fd, F_SETFL, flags);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
@ -1916,6 +2174,7 @@ static const struct luaL_Reg tests_funcs[] = {
|
||||
{"gccolor", gc_color},
|
||||
{"gcage", gc_age},
|
||||
{"gcstate", gc_state},
|
||||
{"tracegc", tracegc},
|
||||
{"pobj", gc_printobj},
|
||||
{"getref", getref},
|
||||
{"hash", hash_query},
|
||||
@ -1923,6 +2182,7 @@ static const struct luaL_Reg tests_funcs[] = {
|
||||
{"limits", get_limits},
|
||||
{"listcode", listcode},
|
||||
{"printcode", printcode},
|
||||
{"printallstack", lua_printallstack},
|
||||
{"listk", listk},
|
||||
{"listabslineinfo", listabslineinfo},
|
||||
{"listlocals", listlocals},
|
||||
@ -1931,14 +2191,21 @@ static const struct luaL_Reg tests_funcs[] = {
|
||||
{"newstate", newstate},
|
||||
{"newuserdata", newuserdata},
|
||||
{"num2int", num2int},
|
||||
{"makeseed", makeseed},
|
||||
{"pushuserdata", pushuserdata},
|
||||
{"gcquery", gc_query},
|
||||
{"querystr", string_query},
|
||||
{"querytab", table_query},
|
||||
{"codeparam", test_codeparam},
|
||||
{"applyparam", test_applyparam},
|
||||
{"ref", tref},
|
||||
{"resume", coresume},
|
||||
{"s2d", s2d},
|
||||
{"sethook", sethook},
|
||||
{"stacklevel", stacklevel},
|
||||
{"resetCI", resetCI},
|
||||
{"reallocstack", reallocstack},
|
||||
{"sizes", get_sizes},
|
||||
{"testC", testC},
|
||||
{"makeCfunc", makeCfunc},
|
||||
{"totalmem", mem_query},
|
||||
@ -1948,6 +2215,9 @@ static const struct luaL_Reg tests_funcs[] = {
|
||||
{"udataval", udataval},
|
||||
{"unref", unref},
|
||||
{"upvalue", upvalue},
|
||||
{"externKstr", externKstr},
|
||||
{"externstr", externstr},
|
||||
{"nonblock", nonblock},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
53
ltests.h
53
ltests.h
@ -13,7 +13,7 @@
|
||||
|
||||
/* test Lua with compatibility code */
|
||||
#define LUA_COMPAT_MATHLIB
|
||||
#define LUA_COMPAT_LT_LE
|
||||
#undef LUA_COMPAT_GLOBAL
|
||||
|
||||
|
||||
#define LUA_DEBUG
|
||||
@ -44,6 +44,10 @@
|
||||
#define LUA_RAND32
|
||||
|
||||
|
||||
/* test stack reallocation without strict address use */
|
||||
#define LUAI_STRICT_ADDRESS 0
|
||||
|
||||
|
||||
/* memory-allocator control variables */
|
||||
typedef struct Memcontrol {
|
||||
int failnext;
|
||||
@ -58,23 +62,39 @@ typedef struct Memcontrol {
|
||||
LUA_API Memcontrol l_memcontrol;
|
||||
|
||||
|
||||
#define luai_tracegc(L,f) luai_tracegctest(L, f)
|
||||
extern void luai_tracegctest (lua_State *L, int first);
|
||||
|
||||
|
||||
/*
|
||||
** generic variable for debug tricks
|
||||
*/
|
||||
extern void *l_Trick;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Function to traverse and check all memory used by Lua
|
||||
*/
|
||||
LUAI_FUNC int lua_checkmemory (lua_State *L);
|
||||
extern int lua_checkmemory (lua_State *L);
|
||||
|
||||
/*
|
||||
** Function to print an object GC-friendly
|
||||
*/
|
||||
struct GCObject;
|
||||
LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o);
|
||||
extern void lua_printobj (lua_State *L, struct GCObject *o);
|
||||
|
||||
|
||||
/*
|
||||
** Function to print a value
|
||||
*/
|
||||
struct TValue;
|
||||
extern void lua_printvalue (struct TValue *v);
|
||||
|
||||
/*
|
||||
** Function to print the stack
|
||||
*/
|
||||
extern void lua_printstack (lua_State *L);
|
||||
extern int lua_printallstack (lua_State *L);
|
||||
|
||||
|
||||
/* test for lock/unlock */
|
||||
@ -101,13 +121,14 @@ LUA_API int luaB_opentests (lua_State *L);
|
||||
LUA_API void *debug_realloc (void *ud, void *block,
|
||||
size_t osize, size_t nsize);
|
||||
|
||||
#if defined(lua_c)
|
||||
#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol)
|
||||
#define luaL_openlibs(L) \
|
||||
{ (luaL_openlibs)(L); \
|
||||
|
||||
#define luaL_newstate() \
|
||||
lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL))
|
||||
#define luai_openlibs(L) \
|
||||
{ luaL_openlibs(L); \
|
||||
luaL_requiref(L, "T", luaB_opentests, 1); \
|
||||
lua_pop(L, 1); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
@ -121,20 +142,14 @@ LUA_API void *debug_realloc (void *ud, void *block,
|
||||
#define STRCACHE_N 23
|
||||
#define STRCACHE_M 5
|
||||
|
||||
#undef LUAI_USER_ALIGNMENT_T
|
||||
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||
#define MAXINDEXRK 1
|
||||
|
||||
|
||||
/*
|
||||
** This one is not compatible with tests for opcode optimizations,
|
||||
** as it blocks some optimizations
|
||||
#define MAXINDEXRK 0
|
||||
** Reduce maximum stack size to make stack-overflow tests run faster.
|
||||
** (But value is still large enough to overflow smaller integers.)
|
||||
*/
|
||||
|
||||
|
||||
/* make stack-overflow tests run faster */
|
||||
#undef LUAI_MAXSTACK
|
||||
#define LUAI_MAXSTACK 50000
|
||||
#define LUAI_MAXSTACK 68000
|
||||
|
||||
|
||||
/* test mode uses more stack space */
|
||||
|
||||
193
ltm.c
193
ltm.c
@ -58,7 +58,7 @@ void luaT_init (lua_State *L) {
|
||||
** tag methods
|
||||
*/
|
||||
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
||||
const TValue *tm = luaH_getshortstr(events, ename);
|
||||
const TValue *tm = luaH_Hgetshortstr(events, ename);
|
||||
lua_assert(event <= TM_EQ);
|
||||
if (notm(tm)) { /* no tag method? */
|
||||
events->flags |= cast_byte(1u<<event); /* cache this fact */
|
||||
@ -80,7 +80,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
||||
default:
|
||||
mt = G(L)->mt[ttype(o)];
|
||||
}
|
||||
return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
|
||||
return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
|
||||
}
|
||||
|
||||
|
||||
@ -92,7 +92,7 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
|
||||
Table *mt;
|
||||
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
|
||||
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
|
||||
const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
|
||||
const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name"));
|
||||
if (ttisstring(name)) /* is '__name' a string? */
|
||||
return getstr(tsvalue(name)); /* use it as type name */
|
||||
}
|
||||
@ -116,8 +116,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
||||
}
|
||||
|
||||
|
||||
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
|
||||
const TValue *p2, StkId res) {
|
||||
lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
|
||||
const TValue *p2, StkId res) {
|
||||
ptrdiff_t result = savestack(L, res);
|
||||
StkId func = L->top.p;
|
||||
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
||||
@ -131,6 +131,7 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
|
||||
luaD_callnoyield(L, func, 1);
|
||||
res = restorestack(L, result);
|
||||
setobjs2s(L, res, --L->top.p); /* move result to its place */
|
||||
return ttypetag(s2v(res)); /* return tag of the result */
|
||||
}
|
||||
|
||||
|
||||
@ -139,15 +140,16 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
|
||||
if (notm(tm))
|
||||
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
|
||||
if (notm(tm)) return 0;
|
||||
luaT_callTMres(L, tm, p1, p2, res);
|
||||
return 1;
|
||||
if (notm(tm))
|
||||
return -1; /* tag method not found */
|
||||
else /* call tag method and return the tag of the result */
|
||||
return luaT_callTMres(L, tm, p1, p2, res);
|
||||
}
|
||||
|
||||
|
||||
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
StkId res, TMS event) {
|
||||
if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
|
||||
if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) {
|
||||
switch (event) {
|
||||
case TM_BAND: case TM_BOR: case TM_BXOR:
|
||||
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
||||
@ -164,11 +166,14 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** The use of 'p1' after 'callbinTM' is safe because, when a tag
|
||||
** method is not found, 'callbinTM' cannot change the stack.
|
||||
*/
|
||||
void luaT_tryconcatTM (lua_State *L) {
|
||||
StkId top = L->top.p;
|
||||
if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2,
|
||||
TM_CONCAT)))
|
||||
luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
|
||||
StkId p1 = L->top.p - 2; /* first argument */
|
||||
if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0))
|
||||
luaG_concaterror(L, s2v(p1), s2v(p1 + 1));
|
||||
}
|
||||
|
||||
|
||||
@ -191,28 +196,12 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
||||
|
||||
/*
|
||||
** Calls an order tag method.
|
||||
** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
|
||||
** behavior: if there is no '__le', try '__lt', based on l <= r iff
|
||||
** !(r < l) (assuming a total order). If the metamethod yields during
|
||||
** this substitution, the continuation has to know about it (to negate
|
||||
** the result of r<l); bit CIST_LEQ in the call status keeps that
|
||||
** information.
|
||||
*/
|
||||
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
TMS event) {
|
||||
if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */
|
||||
return !l_isfalse(s2v(L->top.p));
|
||||
#if defined(LUA_COMPAT_LT_LE)
|
||||
else if (event == TM_LE) {
|
||||
/* try '!(p2 < p1)' for '(p1 <= p2)' */
|
||||
L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
|
||||
if (callbinTM(L, p2, p1, L->top.p, TM_LT)) {
|
||||
L->ci->callstatus ^= CIST_LEQ; /* clear mark */
|
||||
return l_isfalse(s2v(L->top.p));
|
||||
}
|
||||
/* else error will remove this 'ci'; no need to clear mark */
|
||||
}
|
||||
#endif
|
||||
int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */
|
||||
if (tag >= 0) /* found tag method? */
|
||||
return !tagisfalse(tag);
|
||||
luaG_ordererror(L, p1, p2); /* no metamethod found */
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
@ -235,36 +224,140 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
||||
}
|
||||
|
||||
|
||||
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
|
||||
const Proto *p) {
|
||||
/*
|
||||
** Create a vararg table at the top of the stack, with 'n' elements
|
||||
** starting at 'f'.
|
||||
*/
|
||||
static void createvarargtab (lua_State *L, StkId f, int n) {
|
||||
int i;
|
||||
TValue key, value;
|
||||
Table *t = luaH_new(L);
|
||||
sethvalue(L, s2v(L->top.p), t);
|
||||
L->top.p++;
|
||||
luaH_resize(L, t, cast_uint(n), 1);
|
||||
setsvalue(L, &key, luaS_new(L, "n")); /* key is "n" */
|
||||
setivalue(&value, n); /* value is n */
|
||||
/* No need to anchor the key: Due to the resize, the next operation
|
||||
cannot trigger a garbage collection */
|
||||
luaH_set(L, t, &key, &value); /* t.n = n */
|
||||
for (i = 0; i < n; i++)
|
||||
luaH_setint(L, t, i + 1, s2v(f + i));
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** initial stack: func arg1 ... argn extra1 ...
|
||||
** ^ ci->func ^ L->top
|
||||
** final stack: func nil ... nil extra1 ... func arg1 ... argn
|
||||
** ^ ci->func
|
||||
*/
|
||||
static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p,
|
||||
int totalargs, int nfixparams, int nextra) {
|
||||
int i;
|
||||
int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
|
||||
int nextra = actual - nfixparams; /* number of extra arguments */
|
||||
ci->u.l.nextraargs = nextra;
|
||||
luaD_checkstack(L, p->maxstacksize + 1);
|
||||
/* copy function to the top of the stack */
|
||||
/* copy function to the top of the stack, after extra arguments */
|
||||
setobjs2s(L, L->top.p++, ci->func.p);
|
||||
/* move fixed parameters to the top of the stack */
|
||||
/* move fixed parameters to after the copied function */
|
||||
for (i = 1; i <= nfixparams; i++) {
|
||||
setobjs2s(L, L->top.p++, ci->func.p + i);
|
||||
setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
|
||||
}
|
||||
ci->func.p += actual + 1;
|
||||
ci->top.p += actual + 1;
|
||||
lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
|
||||
ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */
|
||||
ci->top.p += totalargs + 1;
|
||||
}
|
||||
|
||||
|
||||
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
|
||||
int i;
|
||||
int nextra = ci->u.l.nextraargs;
|
||||
if (wanted < 0) {
|
||||
wanted = nextra; /* get all extra arguments available */
|
||||
checkstackGCp(L, nextra, where); /* ensure stack space */
|
||||
L->top.p = where + nextra; /* next instruction will need top */
|
||||
void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) {
|
||||
int totalargs = cast_int(L->top.p - ci->func.p) - 1;
|
||||
int nfixparams = p->numparams;
|
||||
int nextra = totalargs - nfixparams; /* number of extra arguments */
|
||||
if (p->flag & PF_VATAB) { /* does it need a vararg table? */
|
||||
lua_assert(!(p->flag & PF_VAHID));
|
||||
createvarargtab(L, ci->func.p + nfixparams + 1, nextra);
|
||||
/* move table to proper place (last parameter) */
|
||||
setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1);
|
||||
}
|
||||
else { /* no table */
|
||||
lua_assert(p->flag & PF_VAHID);
|
||||
buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra);
|
||||
/* set vararg parameter to nil */
|
||||
setnilvalue(s2v(ci->func.p + nfixparams + 1));
|
||||
lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) {
|
||||
int nextra = ci->u.l.nextraargs;
|
||||
lua_Integer n;
|
||||
if (tointegerns(rc, &n)) { /* integral value? */
|
||||
if (l_castS2U(n) - 1 < cast_uint(nextra)) {
|
||||
StkId slot = ci->func.p - nextra + cast_int(n) - 1;
|
||||
setobjs2s(((lua_State*)NULL), ra, slot);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (ttisstring(rc)) { /* string value? */
|
||||
size_t len;
|
||||
const char *s = getlstr(tsvalue(rc), len);
|
||||
if (len == 1 && s[0] == 'n') { /* key is "n"? */
|
||||
setivalue(s2v(ra), nextra);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setnilvalue(s2v(ra)); /* else produce nil */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get the number of extra arguments in a vararg function. If vararg
|
||||
** table has been optimized away, that number is in the call info.
|
||||
** Otherwise, get the field 'n' from the vararg table and check that it
|
||||
** has a proper value (non-negative integer not larger than the stack
|
||||
** limit).
|
||||
*/
|
||||
static int getnumargs (lua_State *L, CallInfo *ci, Table *h) {
|
||||
if (h == NULL) /* no vararg table? */
|
||||
return ci->u.l.nextraargs;
|
||||
else {
|
||||
TValue res;
|
||||
if (luaH_getshortstr(h, luaS_new(L, "n"), &res) != LUA_VNUMINT ||
|
||||
l_castS2U(ivalue(&res)) > cast_uint(INT_MAX/2))
|
||||
luaG_runerror(L, "vararg table has no proper 'n'");
|
||||
return cast_int(ivalue(&res));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get 'wanted' vararg arguments and put them in 'where'. 'vatab' is
|
||||
** the register of the vararg table or -1 if there is no vararg table.
|
||||
*/
|
||||
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted,
|
||||
int vatab) {
|
||||
Table *h = (vatab < 0) ? NULL : hvalue(s2v(ci->func.p + vatab + 1));
|
||||
int nargs = getnumargs(L, ci, h); /* number of available vararg args. */
|
||||
int i, touse; /* 'touse' is minimum between 'wanted' and 'nargs' */
|
||||
if (wanted < 0) {
|
||||
touse = wanted = nargs; /* get all extra arguments available */
|
||||
checkstackp(L, nargs, where); /* ensure stack space */
|
||||
L->top.p = where + nargs; /* next instruction will need top */
|
||||
}
|
||||
else
|
||||
touse = (nargs > wanted) ? wanted : nargs;
|
||||
if (h == NULL) { /* no vararg table? */
|
||||
for (i = 0; i < touse; i++) /* get vararg values from the stack */
|
||||
setobjs2s(L, where + i, ci->func.p - nargs + i);
|
||||
}
|
||||
else { /* get vararg values from vararg table */
|
||||
for (i = 0; i < touse; i++) {
|
||||
lu_byte tag = luaH_getint(h, i + 1, s2v(where + i));
|
||||
if (tagisempty(tag))
|
||||
setnilvalue(s2v(where + i));
|
||||
}
|
||||
}
|
||||
for (i = 0; i < wanted && i < nextra; i++)
|
||||
setobjs2s(L, where + i, ci->func.p - nextra + i);
|
||||
for (; i < wanted; i++) /* complete required results with nil */
|
||||
setnilvalue(s2v(where + i));
|
||||
}
|
||||
|
||||
27
ltm.h
27
ltm.h
@ -9,7 +9,6 @@
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*
|
||||
@ -49,10 +48,10 @@ typedef enum {
|
||||
/*
|
||||
** Mask with 1 in all fast-access methods. A 1 in any of these bits
|
||||
** in the flag of a (meta)table means the metatable does not have the
|
||||
** corresponding metamethod field. (Bit 7 of the flag is used for
|
||||
** 'isrealasize'.)
|
||||
** corresponding metamethod field. (Bit 6 of the flag indicates that
|
||||
** the table is using the dummy node.)
|
||||
*/
|
||||
#define maskflags (~(~0u << (TM_EQ + 1)))
|
||||
#define maskflags cast_byte(~(~0u << (TM_EQ + 1)))
|
||||
|
||||
|
||||
/*
|
||||
@ -61,11 +60,12 @@ typedef enum {
|
||||
*/
|
||||
#define notm(tm) ttisnil(tm)
|
||||
|
||||
#define checknoTM(mt,e) ((mt) == NULL || (mt)->flags & (1u<<(e)))
|
||||
|
||||
#define gfasttm(g,et,e) ((et) == NULL ? NULL : \
|
||||
((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
|
||||
#define gfasttm(g,mt,e) \
|
||||
(checknoTM(mt, e) ? NULL : luaT_gettm(mt, e, (g)->tmname[e]))
|
||||
|
||||
#define fasttm(l,et,e) gfasttm(G(l), et, e)
|
||||
#define fasttm(l,mt,e) gfasttm(G(l), mt, e)
|
||||
|
||||
#define ttypename(x) luaT_typenames_[(x) + 1]
|
||||
|
||||
@ -81,8 +81,8 @@ LUAI_FUNC void luaT_init (lua_State *L);
|
||||
|
||||
LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
||||
const TValue *p2, const TValue *p3);
|
||||
LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f,
|
||||
const TValue *p1, const TValue *p2, StkId p3);
|
||||
LUAI_FUNC lu_byte luaT_callTMres (lua_State *L, const TValue *f,
|
||||
const TValue *p1, const TValue *p2, StkId p3);
|
||||
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
StkId res, TMS event);
|
||||
LUAI_FUNC void luaT_tryconcatTM (lua_State *L);
|
||||
@ -95,10 +95,11 @@ LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
|
||||
LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
||||
int inv, int isfloat, TMS event);
|
||||
|
||||
LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams,
|
||||
CallInfo *ci, const Proto *p);
|
||||
LUAI_FUNC void luaT_getvarargs (lua_State *L, CallInfo *ci,
|
||||
StkId where, int wanted);
|
||||
LUAI_FUNC void luaT_adjustvarargs (lua_State *L, struct CallInfo *ci,
|
||||
const Proto *p);
|
||||
LUAI_FUNC void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc);
|
||||
LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where,
|
||||
int wanted, int vatab);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
177
lua.c
177
lua.c
@ -19,6 +19,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
#if !defined(LUA_PROGNAME)
|
||||
@ -115,12 +116,13 @@ static void l_message (const char *pname, const char *msg) {
|
||||
|
||||
/*
|
||||
** Check whether 'status' is not OK and, if so, prints the error
|
||||
** message on the top of the stack. It assumes that the error object
|
||||
** is a string, as it was either generated by Lua or by 'msghandler'.
|
||||
** message on the top of the stack.
|
||||
*/
|
||||
static int report (lua_State *L, int status) {
|
||||
if (status != LUA_OK) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL)
|
||||
msg = "(error message not a string)";
|
||||
l_message(progname, msg);
|
||||
lua_pop(L, 1); /* remove message */
|
||||
}
|
||||
@ -210,12 +212,17 @@ static int dostring (lua_State *L, const char *s, const char *name) {
|
||||
|
||||
/*
|
||||
** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
|
||||
** If there is no explicit modname and globname contains a '-', cut
|
||||
** the suffix after '-' (the "version") to make the global name.
|
||||
*/
|
||||
static int dolibrary (lua_State *L, char *globname) {
|
||||
int status;
|
||||
char *suffix = NULL;
|
||||
char *modname = strchr(globname, '=');
|
||||
if (modname == NULL) /* no explicit name? */
|
||||
if (modname == NULL) { /* no explicit name? */
|
||||
modname = globname; /* module name is equal to global name */
|
||||
suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */
|
||||
}
|
||||
else {
|
||||
*modname = '\0'; /* global name ends here */
|
||||
modname++; /* module name starts after the '=' */
|
||||
@ -223,8 +230,11 @@ static int dolibrary (lua_State *L, char *globname) {
|
||||
lua_getglobal(L, "require");
|
||||
lua_pushstring(L, modname);
|
||||
status = docall(L, 1, 1); /* call 'require(modname)' */
|
||||
if (status == LUA_OK)
|
||||
if (status == LUA_OK) {
|
||||
if (suffix != NULL) /* is there a suffix mark? */
|
||||
*suffix = '\0'; /* remove suffix from global name */
|
||||
lua_setglobal(L, globname); /* globname = require(modname) */
|
||||
}
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
@ -293,7 +303,8 @@ static int collectargs (char **argv, int *first) {
|
||||
case '-': /* '--' */
|
||||
if (argv[i][2] != '\0') /* extra characters after '--'? */
|
||||
return has_error; /* invalid option */
|
||||
*first = i + 1;
|
||||
/* if there is a script name, it comes after '--' */
|
||||
*first = (argv[i + 1] != NULL) ? i + 1 : 0;
|
||||
return args;
|
||||
case '\0': /* '-' */
|
||||
return args; /* script "name" is '-' */
|
||||
@ -338,6 +349,7 @@ static int collectargs (char **argv, int *first) {
|
||||
*/
|
||||
static int runargs (lua_State *L, char **argv, int n) {
|
||||
int i;
|
||||
lua_warning(L, "@off", 0); /* by default, Lua stand-alone has warnings off */
|
||||
for (i = 1; i < n; i++) {
|
||||
int option = argv[i][1];
|
||||
lua_assert(argv[i][0] == '-'); /* already checked */
|
||||
@ -422,30 +434,91 @@ static int handle_luainit (lua_State *L) {
|
||||
|
||||
|
||||
/*
|
||||
** lua_readline defines how to show a prompt and then read a line from
|
||||
** the standard input.
|
||||
** lua_saveline defines how to "save" a read line in a "history".
|
||||
** lua_freeline defines how to free a line read by lua_readline.
|
||||
** * lua_initreadline initializes the readline system.
|
||||
** * lua_readline defines how to show a prompt and then read a line from
|
||||
** the standard input.
|
||||
** * lua_saveline defines how to "save" a read line in a "history".
|
||||
** * lua_freeline defines how to free a line read by lua_readline.
|
||||
*/
|
||||
|
||||
#if !defined(lua_readline) /* { */
|
||||
/* Otherwise, all previously listed functions should be defined. */
|
||||
|
||||
#if defined(LUA_USE_READLINE) /* { */
|
||||
/* Lua will be linked with '-lreadline' */
|
||||
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
|
||||
#define lua_initreadline(L) ((void)L, rl_readline_name="lua")
|
||||
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
||||
#define lua_saveline(L,line) ((void)L, add_history(line))
|
||||
#define lua_freeline(L,b) ((void)L, free(b))
|
||||
#define lua_readline(buff,prompt) ((void)buff, readline(prompt))
|
||||
#define lua_saveline(line) add_history(line)
|
||||
#define lua_freeline(line) free(line)
|
||||
|
||||
#else /* }{ */
|
||||
#else /* }{ */
|
||||
/* use dynamically loaded readline (or nothing) */
|
||||
|
||||
#define lua_initreadline(L) ((void)L)
|
||||
#define lua_readline(L,b,p) \
|
||||
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
||||
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
||||
#define lua_saveline(L,line) { (void)L; (void)line; }
|
||||
#define lua_freeline(L,b) { (void)L; (void)b; }
|
||||
/* pointer to 'readline' function (if any) */
|
||||
typedef char *(*l_readlineT) (const char *prompt);
|
||||
static l_readlineT l_readline = NULL;
|
||||
|
||||
/* pointer to 'add_history' function (if any) */
|
||||
typedef void (*l_addhistT) (const char *string);
|
||||
static l_addhistT l_addhist = NULL;
|
||||
|
||||
|
||||
static char *lua_readline (char *buff, const char *prompt) {
|
||||
if (l_readline != NULL) /* is there a 'readline'? */
|
||||
return (*l_readline)(prompt); /* use it */
|
||||
else { /* emulate 'readline' over 'buff' */
|
||||
fputs(prompt, stdout);
|
||||
fflush(stdout); /* show prompt */
|
||||
return fgets(buff, LUA_MAXINPUT, stdin); /* read line */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void lua_saveline (const char *line) {
|
||||
if (l_addhist != NULL) /* is there an 'add_history'? */
|
||||
(*l_addhist)(line); /* use it */
|
||||
/* else nothing to be done */
|
||||
}
|
||||
|
||||
|
||||
static void lua_freeline (char *line) {
|
||||
if (l_readline != NULL) /* is there a 'readline'? */
|
||||
free(line); /* free line created by it */
|
||||
/* else 'lua_readline' used an automatic buffer; nothing to free */
|
||||
}
|
||||
|
||||
|
||||
#if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */
|
||||
/* try to load 'readline' dynamically */
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
static void lua_initreadline (lua_State *L) {
|
||||
void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL);
|
||||
if (lib == NULL)
|
||||
lua_warning(L, "library '" LUA_READLINELIB "' not found", 0);
|
||||
else {
|
||||
const char **name = cast(const char**, dlsym(lib, "rl_readline_name"));
|
||||
if (name != NULL)
|
||||
*name = "lua";
|
||||
l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline")));
|
||||
l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history")));
|
||||
if (l_readline == NULL)
|
||||
lua_warning(L, "unable to load 'readline'", 0);
|
||||
}
|
||||
}
|
||||
|
||||
#else /* }{ */
|
||||
/* no dlopen or LUA_READLINELIB undefined */
|
||||
|
||||
/* Leave pointers with NULL */
|
||||
#define lua_initreadline(L) ((void)L)
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
@ -481,10 +554,8 @@ static int incomplete (lua_State *L, int status) {
|
||||
if (status == LUA_ERRSYNTAX) {
|
||||
size_t lmsg;
|
||||
const char *msg = lua_tolstring(L, -1, &lmsg);
|
||||
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
|
||||
lua_pop(L, 1);
|
||||
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0; /* else... */
|
||||
}
|
||||
@ -495,21 +566,17 @@ static int incomplete (lua_State *L, int status) {
|
||||
*/
|
||||
static int pushline (lua_State *L, int firstline) {
|
||||
char buffer[LUA_MAXINPUT];
|
||||
char *b = buffer;
|
||||
size_t l;
|
||||
const char *prmt = get_prompt(L, firstline);
|
||||
int readstatus = lua_readline(L, b, prmt);
|
||||
if (readstatus == 0)
|
||||
return 0; /* no input (prompt will be popped by caller) */
|
||||
char *b = lua_readline(buffer, prmt);
|
||||
lua_pop(L, 1); /* remove prompt */
|
||||
if (b == NULL)
|
||||
return 0; /* no input */
|
||||
l = strlen(b);
|
||||
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
||||
b[--l] = '\0'; /* remove it */
|
||||
if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
|
||||
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
||||
else
|
||||
lua_pushlstring(L, b, l);
|
||||
lua_freeline(L, b);
|
||||
lua_pushlstring(L, b, l);
|
||||
lua_freeline(b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -522,32 +589,44 @@ static int addreturn (lua_State *L) {
|
||||
const char *line = lua_tostring(L, -1); /* original line */
|
||||
const char *retline = lua_pushfstring(L, "return %s;", line);
|
||||
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
|
||||
if (status == LUA_OK) {
|
||||
if (status == LUA_OK)
|
||||
lua_remove(L, -2); /* remove modified line */
|
||||
if (line[0] != '\0') /* non empty? */
|
||||
lua_saveline(L, line); /* keep history */
|
||||
}
|
||||
else
|
||||
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static void checklocal (const char *line) {
|
||||
static const size_t szloc = sizeof("local") - 1;
|
||||
static const char space[] = " \t";
|
||||
line += strspn(line, space); /* skip spaces */
|
||||
if (strncmp(line, "local", szloc) == 0 && /* "local"? */
|
||||
strchr(space, *(line + szloc)) != NULL) { /* followed by a space? */
|
||||
lua_writestringerror("%s\n",
|
||||
"warning: locals do not survive across lines in interactive mode");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Read multiple lines until a complete Lua statement
|
||||
** Read multiple lines until a complete Lua statement or an error not
|
||||
** for an incomplete statement. Start with first line already read in
|
||||
** the stack.
|
||||
*/
|
||||
static int multiline (lua_State *L) {
|
||||
size_t len;
|
||||
const char *line = lua_tolstring(L, 1, &len); /* get first line */
|
||||
checklocal(line);
|
||||
for (;;) { /* repeat until gets a complete statement */
|
||||
size_t len;
|
||||
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
||||
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
||||
if (!incomplete(L, status) || !pushline(L, 0)) {
|
||||
lua_saveline(L, line); /* keep history */
|
||||
return status; /* cannot or should not try to add continuation line */
|
||||
}
|
||||
if (!incomplete(L, status) || !pushline(L, 0))
|
||||
return status; /* should not or cannot try to add continuation line */
|
||||
lua_remove(L, -2); /* remove error message (from incomplete line) */
|
||||
lua_pushliteral(L, "\n"); /* add newline... */
|
||||
lua_insert(L, -2); /* ...between the two lines */
|
||||
lua_concat(L, 3); /* join them */
|
||||
line = lua_tolstring(L, 1, &len); /* get what is has */
|
||||
}
|
||||
}
|
||||
|
||||
@ -559,12 +638,16 @@ static int multiline (lua_State *L) {
|
||||
** in the top of the stack.
|
||||
*/
|
||||
static int loadline (lua_State *L) {
|
||||
const char *line;
|
||||
int status;
|
||||
lua_settop(L, 0);
|
||||
if (!pushline(L, 1))
|
||||
return -1; /* no input */
|
||||
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
||||
status = multiline(L); /* try as command, maybe with continuation lines */
|
||||
line = lua_tostring(L, 1);
|
||||
if (line[0] != '\0') /* non empty? */
|
||||
lua_saveline(line); /* keep history */
|
||||
lua_remove(L, 1); /* remove line from the stack */
|
||||
lua_assert(lua_gettop(L) == 1);
|
||||
return status;
|
||||
@ -609,6 +692,10 @@ static void doREPL (lua_State *L) {
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
#if !defined(luai_openlibs)
|
||||
#define luai_openlibs(L) luaL_openselectedlibs(L, ~0, 0)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Main body of stand-alone interpreter (to be called in protected mode).
|
||||
@ -631,15 +718,15 @@ static int pmain (lua_State *L) {
|
||||
lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
|
||||
}
|
||||
luaL_openlibs(L); /* open standard libraries */
|
||||
luai_openlibs(L); /* open standard libraries */
|
||||
createargtable(L, argv, argc, script); /* create table 'arg' */
|
||||
lua_gc(L, LUA_GCRESTART); /* start GC... */
|
||||
lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */
|
||||
lua_gc(L, LUA_GCGEN); /* ...in generational mode */
|
||||
if (!(args & has_E)) { /* no option '-E'? */
|
||||
if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
|
||||
return 0; /* error running LUA_INIT */
|
||||
}
|
||||
if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */
|
||||
if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */
|
||||
return 0; /* something failed */
|
||||
if (script > 0) { /* execute main script (if there is one) */
|
||||
if (handle_script(L, argv + script) != LUA_OK)
|
||||
|
||||
105
lua.h
105
lua.h
@ -1,7 +1,7 @@
|
||||
/*
|
||||
** $Id: lua.h $
|
||||
** Lua - A Scripting Language
|
||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||
** Lua.org, PUC-Rio, Brazil (www.lua.org)
|
||||
** See Copyright Notice at the end of this file
|
||||
*/
|
||||
|
||||
@ -13,22 +13,21 @@
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#include "luaconf.h"
|
||||
|
||||
|
||||
#define LUA_VERSION_MAJOR "5"
|
||||
#define LUA_VERSION_MINOR "4"
|
||||
#define LUA_VERSION_RELEASE "5"
|
||||
|
||||
#define LUA_VERSION_NUM 504
|
||||
#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 5)
|
||||
|
||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2023 Lua.org, PUC-Rio"
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2025 Lua.org, PUC-Rio"
|
||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
||||
|
||||
|
||||
#define LUA_VERSION_MAJOR_N 5
|
||||
#define LUA_VERSION_MINOR_N 5
|
||||
#define LUA_VERSION_RELEASE_N 0
|
||||
|
||||
#define LUA_VERSION_NUM (LUA_VERSION_MAJOR_N * 100 + LUA_VERSION_MINOR_N)
|
||||
#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + LUA_VERSION_RELEASE_N)
|
||||
|
||||
|
||||
#include "luaconf.h"
|
||||
|
||||
|
||||
/* mark for precompiled code ('<esc>Lua') */
|
||||
#define LUA_SIGNATURE "\x1bLua"
|
||||
|
||||
@ -38,10 +37,10 @@
|
||||
|
||||
/*
|
||||
** Pseudo-indices
|
||||
** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty
|
||||
** space after that to help overflow detection)
|
||||
** (The stack size is limited to INT_MAX/2; we keep some free empty
|
||||
** space after that to help overflow detection.)
|
||||
*/
|
||||
#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000)
|
||||
#define LUA_REGISTRYINDEX (-(INT_MAX/2 + 1000))
|
||||
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
|
||||
|
||||
|
||||
@ -81,9 +80,10 @@ typedef struct lua_State lua_State;
|
||||
|
||||
|
||||
/* predefined values in the registry */
|
||||
#define LUA_RIDX_MAINTHREAD 1
|
||||
/* index 1 is reserved for the reference mechanism */
|
||||
#define LUA_RIDX_GLOBALS 2
|
||||
#define LUA_RIDX_LAST LUA_RIDX_GLOBALS
|
||||
#define LUA_RIDX_MAINTHREAD 3
|
||||
#define LUA_RIDX_LAST 3
|
||||
|
||||
|
||||
/* type of numbers in Lua */
|
||||
@ -160,10 +160,10 @@ extern const char lua_ident[];
|
||||
/*
|
||||
** state manipulation
|
||||
*/
|
||||
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
|
||||
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, unsigned seed);
|
||||
LUA_API void (lua_close) (lua_State *L);
|
||||
LUA_API lua_State *(lua_newthread) (lua_State *L);
|
||||
LUA_API int (lua_resetthread) (lua_State *L, lua_State *from);
|
||||
LUA_API int (lua_closethread) (lua_State *L, lua_State *from);
|
||||
|
||||
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
|
||||
|
||||
@ -244,6 +244,8 @@ LUA_API void (lua_pushnil) (lua_State *L);
|
||||
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
|
||||
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
||||
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len);
|
||||
LUA_API const char *(lua_pushexternalstring) (lua_State *L,
|
||||
const char *s, size_t len, lua_Alloc falloc, void *ud);
|
||||
LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
|
||||
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
|
||||
va_list argp);
|
||||
@ -323,7 +325,7 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
|
||||
|
||||
|
||||
/*
|
||||
** garbage-collection function and options
|
||||
** garbage-collection options
|
||||
*/
|
||||
|
||||
#define LUA_GCSTOP 0
|
||||
@ -332,11 +334,28 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
|
||||
#define LUA_GCCOUNT 3
|
||||
#define LUA_GCCOUNTB 4
|
||||
#define LUA_GCSTEP 5
|
||||
#define LUA_GCSETPAUSE 6
|
||||
#define LUA_GCSETSTEPMUL 7
|
||||
#define LUA_GCISRUNNING 9
|
||||
#define LUA_GCGEN 10
|
||||
#define LUA_GCINC 11
|
||||
#define LUA_GCISRUNNING 6
|
||||
#define LUA_GCGEN 7
|
||||
#define LUA_GCINC 8
|
||||
#define LUA_GCPARAM 9
|
||||
|
||||
|
||||
/*
|
||||
** garbage-collection parameters
|
||||
*/
|
||||
/* parameters for generational mode */
|
||||
#define LUA_GCPMINORMUL 0 /* control minor collections */
|
||||
#define LUA_GCPMAJORMINOR 1 /* control shift major->minor */
|
||||
#define LUA_GCPMINORMAJOR 2 /* control shift minor->major */
|
||||
|
||||
/* parameters for incremental mode */
|
||||
#define LUA_GCPPAUSE 3 /* size of pause between successive GCs */
|
||||
#define LUA_GCPSTEPMUL 4 /* GC "speed" */
|
||||
#define LUA_GCPSTEPSIZE 5 /* GC granularity */
|
||||
|
||||
/* number of parameters */
|
||||
#define LUA_GCPN 6
|
||||
|
||||
|
||||
LUA_API int (lua_gc) (lua_State *L, int what, ...);
|
||||
|
||||
@ -352,7 +371,9 @@ LUA_API int (lua_next) (lua_State *L, int idx);
|
||||
LUA_API void (lua_concat) (lua_State *L, int n);
|
||||
LUA_API void (lua_len) (lua_State *L, int idx);
|
||||
|
||||
LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
|
||||
#define LUA_N2SBUFFSZ 64
|
||||
LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff);
|
||||
LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
|
||||
|
||||
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
||||
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
||||
@ -411,19 +432,12 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx);
|
||||
** compatibility macros
|
||||
** ===============================================================
|
||||
*/
|
||||
#if defined(LUA_COMPAT_APIINTCASTS)
|
||||
|
||||
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
||||
#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is))
|
||||
#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
|
||||
|
||||
#endif
|
||||
|
||||
#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1)
|
||||
#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
|
||||
#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
|
||||
|
||||
#define LUA_NUMTAGS LUA_NUMTYPES
|
||||
#define lua_resetthread(L) lua_closethread(L,NULL)
|
||||
|
||||
/* }============================================================== */
|
||||
|
||||
@ -469,7 +483,6 @@ LUA_API lua_Hook (lua_gethook) (lua_State *L);
|
||||
LUA_API int (lua_gethookmask) (lua_State *L);
|
||||
LUA_API int (lua_gethookcount) (lua_State *L);
|
||||
|
||||
LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit);
|
||||
|
||||
struct lua_Debug {
|
||||
int event;
|
||||
@ -484,9 +497,10 @@ struct lua_Debug {
|
||||
unsigned char nups; /* (u) number of upvalues */
|
||||
unsigned char nparams;/* (u) number of parameters */
|
||||
char isvararg; /* (u) */
|
||||
unsigned char extraargs; /* (t) number of extra arguments */
|
||||
char istailcall; /* (t) */
|
||||
unsigned short ftransfer; /* (r) index of first value transferred */
|
||||
unsigned short ntransfer; /* (r) number of transferred values */
|
||||
int ftransfer; /* (r) index of first value transferred */
|
||||
int ntransfer; /* (r) number of transferred values */
|
||||
char short_src[LUA_IDSIZE]; /* (S) */
|
||||
/* private part */
|
||||
struct CallInfo *i_ci; /* active function */
|
||||
@ -495,8 +509,19 @@ struct lua_Debug {
|
||||
/* }====================================================================== */
|
||||
|
||||
|
||||
#define LUAI_TOSTRAUX(x) #x
|
||||
#define LUAI_TOSTR(x) LUAI_TOSTRAUX(x)
|
||||
|
||||
#define LUA_VERSION_MAJOR LUAI_TOSTR(LUA_VERSION_MAJOR_N)
|
||||
#define LUA_VERSION_MINOR LUAI_TOSTR(LUA_VERSION_MINOR_N)
|
||||
#define LUA_VERSION_RELEASE LUAI_TOSTR(LUA_VERSION_RELEASE_N)
|
||||
|
||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 1994-2023 Lua.org, PUC-Rio.
|
||||
* Copyright (C) 1994-2025 Lua.org, PUC-Rio.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
|
||||
150
luaconf.h
150
luaconf.h
@ -58,15 +58,26 @@
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** When POSIX DLL ('LUA_USE_DLOPEN') is enabled, the Lua stand-alone
|
||||
** application will try to dynamically link a 'readline' facility
|
||||
** for its REPL. In that case, LUA_READLINELIB is the name of the
|
||||
** library it will look for those facilities. If lua.c cannot open
|
||||
** the specified library, it will generate a warning and then run
|
||||
** without 'readline'. If that macro is not defined, lua.c will not
|
||||
** use 'readline'.
|
||||
*/
|
||||
#if defined(LUA_USE_LINUX)
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
||||
#define LUA_READLINELIB "libreadline.so"
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(LUA_USE_MACOSX)
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_USE_DLOPEN /* MacOS does not need -ldl */
|
||||
#define LUA_USE_DLOPEN /* macOS does not need -ldl */
|
||||
#define LUA_READLINELIB "libedit.dylib"
|
||||
#endif
|
||||
|
||||
|
||||
@ -76,6 +87,11 @@
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(LUA_USE_C89) && defined(LUA_USE_POSIX)
|
||||
#error "POSIX is not compatible with C89"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits.
|
||||
*/
|
||||
@ -122,7 +138,7 @@
|
||||
/*
|
||||
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats.
|
||||
*/
|
||||
#define LUA_32BITS 0
|
||||
/* #define LUA_32BITS */
|
||||
|
||||
|
||||
/*
|
||||
@ -137,7 +153,7 @@
|
||||
#endif
|
||||
|
||||
|
||||
#if LUA_32BITS /* { */
|
||||
#if defined(LUA_32BITS) /* { */
|
||||
/*
|
||||
** 32-bit integers and 'float'
|
||||
*/
|
||||
@ -257,6 +273,15 @@
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** LUA_IGMARK is a mark to ignore all after it when building the
|
||||
** module name (e.g., used to build the luaopen_ function name).
|
||||
** Typically, the suffix after the mark is the module version,
|
||||
** as in "mod-v1.2.so".
|
||||
*/
|
||||
#define LUA_IGMARK "-"
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
@ -294,32 +319,13 @@
|
||||
** More often than not the libs go together with the core.
|
||||
*/
|
||||
#define LUALIB_API LUA_API
|
||||
|
||||
#if defined(__cplusplus)
|
||||
/* Lua uses the "C name" when calling open functions */
|
||||
#define LUAMOD_API extern "C"
|
||||
#else
|
||||
#define LUAMOD_API LUA_API
|
||||
|
||||
|
||||
/*
|
||||
@@ LUAI_FUNC is a mark for all extern functions that are not to be
|
||||
** exported to outside modules.
|
||||
@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables,
|
||||
** none of which to be exported to outside modules (LUAI_DDEF for
|
||||
** definitions and LUAI_DDEC for declarations).
|
||||
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
||||
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
||||
** when Lua is compiled as a shared library. Not all elf targets support
|
||||
** this attribute. Unfortunately, gcc does not offer a way to check
|
||||
** whether the target offers that support, and those without support
|
||||
** give a warning about it. To avoid these warnings, change to the
|
||||
** default definition.
|
||||
*/
|
||||
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||
defined(__ELF__) /* { */
|
||||
#define LUAI_FUNC __attribute__((visibility("internal"))) extern
|
||||
#else /* }{ */
|
||||
#define LUAI_FUNC extern
|
||||
#endif /* } */
|
||||
|
||||
#define LUAI_DDEC(dec) LUAI_FUNC dec
|
||||
#define LUAI_DDEF /* empty */
|
||||
#endif
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
@ -331,11 +337,10 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3.
|
||||
** You can define it to get all options, or change specific options
|
||||
** to fit your specific needs.
|
||||
@@ LUA_COMPAT_GLOBAL avoids 'global' being a reserved word
|
||||
*/
|
||||
#if defined(LUA_COMPAT_5_3) /* { */
|
||||
#define LUA_COMPAT_GLOBAL
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated
|
||||
@ -343,23 +348,7 @@
|
||||
** (These functions were already officially removed in 5.3;
|
||||
** nevertheless they are still available here.)
|
||||
*/
|
||||
#define LUA_COMPAT_MATHLIB
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for
|
||||
** manipulating other integer types (lua_pushunsigned, lua_tounsigned,
|
||||
** luaL_checkint, luaL_checklong, etc.)
|
||||
** (These macros were also officially removed in 5.3, but they are still
|
||||
** available here.)
|
||||
*/
|
||||
#define LUA_COMPAT_APIINTCASTS
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod
|
||||
** using '__lt'.
|
||||
*/
|
||||
#define LUA_COMPAT_LT_LE
|
||||
/* #define LUA_COMPAT_MATHLIB */
|
||||
|
||||
|
||||
/*
|
||||
@ -376,8 +365,6 @@
|
||||
#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
|
||||
#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT)
|
||||
|
||||
#endif /* } */
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
@ -396,35 +383,23 @@
|
||||
@@ l_floatatt(x) corrects float attribute 'x' to the proper float type
|
||||
** by prefixing it with one of FLT/DBL/LDBL.
|
||||
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
||||
@@ LUA_NUMBER_FMT is the format for writing floats.
|
||||
@@ lua_number2str converts a float to a string.
|
||||
@@ LUA_NUMBER_FMT is the format for writing floats with the maximum
|
||||
** number of digits that respects tostring(tonumber(numeral)) == numeral.
|
||||
** (That would be floor(log10(2^n)), where n is the number of bits in
|
||||
** the float mantissa.)
|
||||
@@ LUA_NUMBER_FMT_N is the format for writing floats with the minimum
|
||||
** number of digits that ensures tonumber(tostring(number)) == number.
|
||||
** (That would be LUA_NUMBER_FMT+2.)
|
||||
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
|
||||
@@ l_floor takes the floor of a float.
|
||||
@@ lua_str2number converts a decimal numeral to a number.
|
||||
*/
|
||||
|
||||
|
||||
/* The following definitions are good for most cases here */
|
||||
/* The following definition is good for most cases here */
|
||||
|
||||
#define l_floor(x) (l_mathop(floor)(x))
|
||||
|
||||
#define lua_number2str(s,sz,n) \
|
||||
l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n))
|
||||
|
||||
/*
|
||||
@@ lua_numbertointeger converts a float number with an integral value
|
||||
** to an integer, or returns 0 if float is not within the range of
|
||||
** a lua_Integer. (The range comparisons are tricky because of
|
||||
** rounding. The tests here assume a two-complement representation,
|
||||
** where MININTEGER always has an exact representation as a float;
|
||||
** MAXINTEGER may not have one, and therefore its conversion to float
|
||||
** may have an ill-defined value.)
|
||||
*/
|
||||
#define lua_numbertointeger(n,p) \
|
||||
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
|
||||
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
||||
(*(p) = (LUA_INTEGER)(n), 1))
|
||||
|
||||
|
||||
/* now the variable definitions */
|
||||
|
||||
@ -438,6 +413,7 @@
|
||||
|
||||
#define LUA_NUMBER_FRMLEN ""
|
||||
#define LUA_NUMBER_FMT "%.7g"
|
||||
#define LUA_NUMBER_FMT_N "%.9g"
|
||||
|
||||
#define l_mathop(op) op##f
|
||||
|
||||
@ -454,6 +430,7 @@
|
||||
|
||||
#define LUA_NUMBER_FRMLEN "L"
|
||||
#define LUA_NUMBER_FMT "%.19Lg"
|
||||
#define LUA_NUMBER_FMT_N "%.21Lg"
|
||||
|
||||
#define l_mathop(op) op##l
|
||||
|
||||
@ -468,7 +445,8 @@
|
||||
#define LUAI_UACNUMBER double
|
||||
|
||||
#define LUA_NUMBER_FRMLEN ""
|
||||
#define LUA_NUMBER_FMT "%.14g"
|
||||
#define LUA_NUMBER_FMT "%.15g"
|
||||
#define LUA_NUMBER_FMT_N "%.17g"
|
||||
|
||||
#define l_mathop(op) op
|
||||
|
||||
@ -682,13 +660,6 @@
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(LUA_CORE) || defined(LUA_LIB)
|
||||
/* shorter names for Lua's own use */
|
||||
#define l_likely(x) luai_likely(x)
|
||||
#define l_unlikely(x) luai_unlikely(x)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
@ -713,10 +684,7 @@
|
||||
@@ LUA_USE_APICHECK turns on several consistency checks on the C API.
|
||||
** Define it as a help when debugging C code.
|
||||
*/
|
||||
#if defined(LUA_USE_APICHECK)
|
||||
#include <assert.h>
|
||||
#define luai_apicheck(l,e) assert(e)
|
||||
#endif
|
||||
/* #define LUA_USE_APICHECK */
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
@ -729,20 +697,6 @@
|
||||
** =====================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ LUAI_MAXSTACK limits the size of the Lua stack.
|
||||
** CHANGE it if you need a different limit. This limit is arbitrary;
|
||||
** its only purpose is to stop Lua from consuming unlimited stack
|
||||
** space (and to reserve some numbers for pseudo-indices).
|
||||
** (It must fit into max(size_t)/32 and max(int)/2.)
|
||||
*/
|
||||
#if LUAI_IS32INT
|
||||
#define LUAI_MAXSTACK 1000000
|
||||
#else
|
||||
#define LUAI_MAXSTACK 15000
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
|
||||
** a Lua state with very fast access.
|
||||
@ -787,7 +741,5 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
67
lualib.h
67
lualib.h
@ -14,39 +14,52 @@
|
||||
/* version suffix for environment variable names */
|
||||
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
|
||||
|
||||
|
||||
#define LUA_GLIBK 1
|
||||
LUAMOD_API int (luaopen_base) (lua_State *L);
|
||||
|
||||
#define LUA_COLIBNAME "coroutine"
|
||||
LUAMOD_API int (luaopen_coroutine) (lua_State *L);
|
||||
|
||||
#define LUA_TABLIBNAME "table"
|
||||
LUAMOD_API int (luaopen_table) (lua_State *L);
|
||||
|
||||
#define LUA_IOLIBNAME "io"
|
||||
LUAMOD_API int (luaopen_io) (lua_State *L);
|
||||
|
||||
#define LUA_OSLIBNAME "os"
|
||||
LUAMOD_API int (luaopen_os) (lua_State *L);
|
||||
|
||||
#define LUA_STRLIBNAME "string"
|
||||
LUAMOD_API int (luaopen_string) (lua_State *L);
|
||||
|
||||
#define LUA_UTF8LIBNAME "utf8"
|
||||
LUAMOD_API int (luaopen_utf8) (lua_State *L);
|
||||
|
||||
#define LUA_MATHLIBNAME "math"
|
||||
LUAMOD_API int (luaopen_math) (lua_State *L);
|
||||
|
||||
#define LUA_DBLIBNAME "debug"
|
||||
LUAMOD_API int (luaopen_debug) (lua_State *L);
|
||||
|
||||
#define LUA_LOADLIBNAME "package"
|
||||
#define LUA_LOADLIBK (LUA_GLIBK << 1)
|
||||
LUAMOD_API int (luaopen_package) (lua_State *L);
|
||||
|
||||
|
||||
/* open all previous libraries */
|
||||
LUALIB_API void (luaL_openlibs) (lua_State *L);
|
||||
#define LUA_COLIBNAME "coroutine"
|
||||
#define LUA_COLIBK (LUA_LOADLIBK << 1)
|
||||
LUAMOD_API int (luaopen_coroutine) (lua_State *L);
|
||||
|
||||
#define LUA_DBLIBNAME "debug"
|
||||
#define LUA_DBLIBK (LUA_COLIBK << 1)
|
||||
LUAMOD_API int (luaopen_debug) (lua_State *L);
|
||||
|
||||
#define LUA_IOLIBNAME "io"
|
||||
#define LUA_IOLIBK (LUA_DBLIBK << 1)
|
||||
LUAMOD_API int (luaopen_io) (lua_State *L);
|
||||
|
||||
#define LUA_MATHLIBNAME "math"
|
||||
#define LUA_MATHLIBK (LUA_IOLIBK << 1)
|
||||
LUAMOD_API int (luaopen_math) (lua_State *L);
|
||||
|
||||
#define LUA_OSLIBNAME "os"
|
||||
#define LUA_OSLIBK (LUA_MATHLIBK << 1)
|
||||
LUAMOD_API int (luaopen_os) (lua_State *L);
|
||||
|
||||
#define LUA_STRLIBNAME "string"
|
||||
#define LUA_STRLIBK (LUA_OSLIBK << 1)
|
||||
LUAMOD_API int (luaopen_string) (lua_State *L);
|
||||
|
||||
#define LUA_TABLIBNAME "table"
|
||||
#define LUA_TABLIBK (LUA_STRLIBK << 1)
|
||||
LUAMOD_API int (luaopen_table) (lua_State *L);
|
||||
|
||||
#define LUA_UTF8LIBNAME "utf8"
|
||||
#define LUA_UTF8LIBK (LUA_TABLIBK << 1)
|
||||
LUAMOD_API int (luaopen_utf8) (lua_State *L);
|
||||
|
||||
|
||||
/* open selected libraries */
|
||||
LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int load, int preload);
|
||||
|
||||
/* open all libraries */
|
||||
#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0, 0)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
253
lundump.c
253
lundump.c
@ -21,6 +21,7 @@
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "lundump.h"
|
||||
#include "lzio.h"
|
||||
|
||||
@ -34,6 +35,10 @@ typedef struct {
|
||||
lua_State *L;
|
||||
ZIO *Z;
|
||||
const char *name;
|
||||
Table *h; /* list for string reuse */
|
||||
size_t offset; /* current position relative to beginning of dump */
|
||||
lua_Unsigned nstr; /* number of strings in the list */
|
||||
lu_byte fixed; /* dump is fixed in memory */
|
||||
} LoadState;
|
||||
|
||||
|
||||
@ -47,11 +52,33 @@ static l_noret error (LoadState *S, const char *why) {
|
||||
** All high-level loads go through loadVector; you can change it to
|
||||
** adapt to the endianness of the input
|
||||
*/
|
||||
#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0]))
|
||||
#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0]))
|
||||
|
||||
static void loadBlock (LoadState *S, void *b, size_t size) {
|
||||
if (luaZ_read(S->Z, b, size) != 0)
|
||||
error(S, "truncated chunk");
|
||||
S->offset += size;
|
||||
}
|
||||
|
||||
|
||||
static void loadAlign (LoadState *S, unsigned align) {
|
||||
unsigned padding = align - cast_uint(S->offset % align);
|
||||
if (padding < align) { /* (padding == align) means no padding */
|
||||
lua_Integer paddingContent;
|
||||
loadBlock(S, &paddingContent, padding);
|
||||
lua_assert(S->offset % align == 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define getaddr(S,n,t) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t)))
|
||||
|
||||
static const void *getaddr_ (LoadState *S, size_t size) {
|
||||
const void *block = luaZ_getaddr(S->Z, size);
|
||||
S->offset += size;
|
||||
if (block == NULL)
|
||||
error(S, "truncated fixed buffer");
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
@ -62,34 +89,36 @@ static lu_byte loadByte (LoadState *S) {
|
||||
int b = zgetc(S->Z);
|
||||
if (b == EOZ)
|
||||
error(S, "truncated chunk");
|
||||
S->offset++;
|
||||
return cast_byte(b);
|
||||
}
|
||||
|
||||
|
||||
static size_t loadUnsigned (LoadState *S, size_t limit) {
|
||||
size_t x = 0;
|
||||
static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) {
|
||||
lua_Unsigned x = 0;
|
||||
int b;
|
||||
limit >>= 7;
|
||||
do {
|
||||
b = loadByte(S);
|
||||
if (x >= limit)
|
||||
if (x > limit)
|
||||
error(S, "integer overflow");
|
||||
x = (x << 7) | (b & 0x7f);
|
||||
} while ((b & 0x80) == 0);
|
||||
} while ((b & 0x80) != 0);
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
static size_t loadSize (LoadState *S) {
|
||||
return loadUnsigned(S, ~(size_t)0);
|
||||
return cast_sizet(loadVarint(S, MAX_SIZE));
|
||||
}
|
||||
|
||||
|
||||
static int loadInt (LoadState *S) {
|
||||
return cast_int(loadUnsigned(S, INT_MAX));
|
||||
return cast_int(loadVarint(S, cast_sizet(INT_MAX)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static lua_Number loadNumber (LoadState *S) {
|
||||
lua_Number x;
|
||||
loadVar(S, x);
|
||||
@ -98,58 +127,79 @@ static lua_Number loadNumber (LoadState *S) {
|
||||
|
||||
|
||||
static lua_Integer loadInteger (LoadState *S) {
|
||||
lua_Integer x;
|
||||
loadVar(S, x);
|
||||
return x;
|
||||
lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED);
|
||||
/* decode unsigned to signed */
|
||||
if ((cx & 1) != 0)
|
||||
return l_castU2S(~(cx >> 1));
|
||||
else
|
||||
return l_castU2S(cx >> 1);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Load a nullable string into prototype 'p'.
|
||||
** Load a nullable string into slot 'sl' from prototype 'p'. The
|
||||
** assignment to the slot and the barrier must be performed before any
|
||||
** possible GC activity, to anchor the string. (Both 'loadVector' and
|
||||
** 'luaH_setint' can call the GC.)
|
||||
*/
|
||||
static TString *loadStringN (LoadState *S, Proto *p) {
|
||||
static void loadString (LoadState *S, Proto *p, TString **sl) {
|
||||
lua_State *L = S->L;
|
||||
TString *ts;
|
||||
TValue sv;
|
||||
size_t size = loadSize(S);
|
||||
if (size == 0) /* no string? */
|
||||
return NULL;
|
||||
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
|
||||
char buff[LUAI_MAXSHORTLEN];
|
||||
loadVector(S, buff, size); /* load string into buffer */
|
||||
ts = luaS_newlstr(L, buff, size); /* create string */
|
||||
if (size == 0) { /* previously saved string? */
|
||||
lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */
|
||||
TValue stv;
|
||||
if (idx == 0) { /* no string? */
|
||||
lua_assert(*sl == NULL); /* must be prefilled */
|
||||
return;
|
||||
}
|
||||
if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING)
|
||||
error(S, "invalid string index");
|
||||
*sl = ts = tsvalue(&stv); /* get its value */
|
||||
luaC_objbarrier(L, p, ts);
|
||||
return; /* do not save it again */
|
||||
}
|
||||
else { /* long string */
|
||||
ts = luaS_createlngstrobj(L, size); /* create string */
|
||||
setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */
|
||||
luaD_inctop(L);
|
||||
loadVector(S, getstr(ts), size); /* load directly in final place */
|
||||
L->top.p--; /* pop string */
|
||||
else if ((size -= 1) <= LUAI_MAXSHORTLEN) { /* short string? */
|
||||
char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */
|
||||
loadVector(S, buff, size + 1); /* load string into buffer */
|
||||
*sl = ts = luaS_newlstr(L, buff, size); /* create string */
|
||||
luaC_objbarrier(L, p, ts);
|
||||
}
|
||||
luaC_objbarrier(L, p, ts);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Load a non-nullable string into prototype 'p'.
|
||||
*/
|
||||
static TString *loadString (LoadState *S, Proto *p) {
|
||||
TString *st = loadStringN(S, p);
|
||||
if (st == NULL)
|
||||
error(S, "bad format for constant string");
|
||||
return st;
|
||||
else if (S->fixed) { /* for a fixed buffer, use a fixed string */
|
||||
const char *s = getaddr(S, size + 1, char); /* get content address */
|
||||
*sl = ts = luaS_newextlstr(L, s, size, NULL, NULL);
|
||||
luaC_objbarrier(L, p, ts);
|
||||
}
|
||||
else { /* create internal copy */
|
||||
*sl = ts = luaS_createlngstrobj(L, size); /* create string */
|
||||
luaC_objbarrier(L, p, ts);
|
||||
loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */
|
||||
}
|
||||
/* add string to list of saved strings */
|
||||
S->nstr++;
|
||||
setsvalue(L, &sv, ts);
|
||||
luaH_setint(L, S->h, l_castU2S(S->nstr), &sv);
|
||||
luaC_objbarrierback(L, obj2gco(S->h), ts);
|
||||
}
|
||||
|
||||
|
||||
static void loadCode (LoadState *S, Proto *f) {
|
||||
int n = loadInt(S);
|
||||
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
||||
f->sizecode = n;
|
||||
loadVector(S, f->code, n);
|
||||
loadAlign(S, sizeof(f->code[0]));
|
||||
if (S->fixed) {
|
||||
f->code = getaddr(S, n, Instruction);
|
||||
f->sizecode = n;
|
||||
}
|
||||
else {
|
||||
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
||||
f->sizecode = n;
|
||||
loadVector(S, f->code, n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void loadFunction(LoadState *S, Proto *f, TString *psource);
|
||||
static void loadFunction(LoadState *S, Proto *f);
|
||||
|
||||
|
||||
static void loadConstants (LoadState *S, Proto *f) {
|
||||
@ -179,10 +229,16 @@ static void loadConstants (LoadState *S, Proto *f) {
|
||||
setivalue(o, loadInteger(S));
|
||||
break;
|
||||
case LUA_VSHRSTR:
|
||||
case LUA_VLNGSTR:
|
||||
setsvalue2n(S->L, o, loadString(S, f));
|
||||
case LUA_VLNGSTR: {
|
||||
lua_assert(f->source == NULL);
|
||||
loadString(S, f, &f->source); /* use 'source' to anchor string */
|
||||
if (f->source == NULL)
|
||||
error(S, "bad format for constant string");
|
||||
setsvalue2n(S->L, o, f->source); /* save it in the right place */
|
||||
f->source = NULL;
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
default: error(S, "invalid constant");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -198,7 +254,7 @@ static void loadProtos (LoadState *S, Proto *f) {
|
||||
for (i = 0; i < n; i++) {
|
||||
f->p[i] = luaF_newproto(S->L);
|
||||
luaC_objbarrier(S->L, f, f->p[i]);
|
||||
loadFunction(S, f->p[i], f->source);
|
||||
loadFunction(S, f->p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -210,8 +266,8 @@ static void loadProtos (LoadState *S, Proto *f) {
|
||||
** in that case all prototypes must be consistent for the GC.
|
||||
*/
|
||||
static void loadUpvalues (LoadState *S, Proto *f) {
|
||||
int i, n;
|
||||
n = loadInt(S);
|
||||
int i;
|
||||
int n = loadInt(S);
|
||||
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
|
||||
f->sizeupvalues = n;
|
||||
for (i = 0; i < n; i++) /* make array valid for GC */
|
||||
@ -225,17 +281,29 @@ static void loadUpvalues (LoadState *S, Proto *f) {
|
||||
|
||||
|
||||
static void loadDebug (LoadState *S, Proto *f) {
|
||||
int i, n;
|
||||
int i;
|
||||
int n = loadInt(S);
|
||||
if (S->fixed) {
|
||||
f->lineinfo = getaddr(S, n, ls_byte);
|
||||
f->sizelineinfo = n;
|
||||
}
|
||||
else {
|
||||
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
||||
f->sizelineinfo = n;
|
||||
loadVector(S, f->lineinfo, n);
|
||||
}
|
||||
n = loadInt(S);
|
||||
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
||||
f->sizelineinfo = n;
|
||||
loadVector(S, f->lineinfo, n);
|
||||
n = loadInt(S);
|
||||
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
||||
f->sizeabslineinfo = n;
|
||||
for (i = 0; i < n; i++) {
|
||||
f->abslineinfo[i].pc = loadInt(S);
|
||||
f->abslineinfo[i].line = loadInt(S);
|
||||
if (n > 0) {
|
||||
loadAlign(S, sizeof(int));
|
||||
if (S->fixed) {
|
||||
f->abslineinfo = getaddr(S, n, AbsLineInfo);
|
||||
f->sizeabslineinfo = n;
|
||||
}
|
||||
else {
|
||||
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
||||
f->sizeabslineinfo = n;
|
||||
loadVector(S, f->abslineinfo, n);
|
||||
}
|
||||
}
|
||||
n = loadInt(S);
|
||||
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
|
||||
@ -243,7 +311,7 @@ static void loadDebug (LoadState *S, Proto *f) {
|
||||
for (i = 0; i < n; i++)
|
||||
f->locvars[i].varname = NULL;
|
||||
for (i = 0; i < n; i++) {
|
||||
f->locvars[i].varname = loadStringN(S, f);
|
||||
loadString(S, f, &f->locvars[i].varname);
|
||||
f->locvars[i].startpc = loadInt(S);
|
||||
f->locvars[i].endpc = loadInt(S);
|
||||
}
|
||||
@ -251,23 +319,24 @@ static void loadDebug (LoadState *S, Proto *f) {
|
||||
if (n != 0) /* does it have debug information? */
|
||||
n = f->sizeupvalues; /* must be this many */
|
||||
for (i = 0; i < n; i++)
|
||||
f->upvalues[i].name = loadStringN(S, f);
|
||||
loadString(S, f, &f->upvalues[i].name);
|
||||
}
|
||||
|
||||
|
||||
static void loadFunction (LoadState *S, Proto *f, TString *psource) {
|
||||
f->source = loadStringN(S, f);
|
||||
if (f->source == NULL) /* no source in dump? */
|
||||
f->source = psource; /* reuse parent's source */
|
||||
static void loadFunction (LoadState *S, Proto *f) {
|
||||
f->linedefined = loadInt(S);
|
||||
f->lastlinedefined = loadInt(S);
|
||||
f->numparams = loadByte(S);
|
||||
f->is_vararg = loadByte(S);
|
||||
/* get only the meaningful flags */
|
||||
f->flag = cast_byte(loadByte(S) & ~PF_FIXED);
|
||||
if (S->fixed)
|
||||
f->flag |= PF_FIXED; /* signal that code is fixed */
|
||||
f->maxstacksize = loadByte(S);
|
||||
loadCode(S, f);
|
||||
loadConstants(S, f);
|
||||
loadUpvalues(S, f);
|
||||
loadProtos(S, f);
|
||||
loadString(S, f, &f->source);
|
||||
loadDebug(S, f);
|
||||
}
|
||||
|
||||
@ -281,13 +350,29 @@ static void checkliteral (LoadState *S, const char *s, const char *msg) {
|
||||
}
|
||||
|
||||
|
||||
static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
||||
if (loadByte(S) != size)
|
||||
error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
|
||||
static l_noret numerror (LoadState *S, const char *what, const char *tname) {
|
||||
const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what);
|
||||
error(S, msg);
|
||||
}
|
||||
|
||||
|
||||
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
|
||||
static void checknumsize (LoadState *S, int size, const char *tname) {
|
||||
if (size != loadByte(S))
|
||||
numerror(S, "size", tname);
|
||||
}
|
||||
|
||||
|
||||
static void checknumformat (LoadState *S, int eq, const char *tname) {
|
||||
if (!eq)
|
||||
numerror(S, "format", tname);
|
||||
}
|
||||
|
||||
|
||||
#define checknum(S,tvar,value,tname) \
|
||||
{ tvar i; checknumsize(S, sizeof(i), tname); \
|
||||
loadVar(S, i); \
|
||||
checknumformat(S, i == value, tname); }
|
||||
|
||||
|
||||
static void checkHeader (LoadState *S) {
|
||||
/* skip 1st char (already read and checked) */
|
||||
@ -297,39 +382,43 @@ static void checkHeader (LoadState *S) {
|
||||
if (loadByte(S) != LUAC_FORMAT)
|
||||
error(S, "format mismatch");
|
||||
checkliteral(S, LUAC_DATA, "corrupted chunk");
|
||||
checksize(S, Instruction);
|
||||
checksize(S, lua_Integer);
|
||||
checksize(S, lua_Number);
|
||||
if (loadInteger(S) != LUAC_INT)
|
||||
error(S, "integer format mismatch");
|
||||
if (loadNumber(S) != LUAC_NUM)
|
||||
error(S, "float format mismatch");
|
||||
checknum(S, int, LUAC_INT, "int");
|
||||
checknum(S, Instruction, LUAC_INST, "instruction");
|
||||
checknum(S, lua_Integer, LUAC_INT, "Lua integer");
|
||||
checknum(S, lua_Number, LUAC_NUM, "Lua number");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Load precompiled chunk.
|
||||
*/
|
||||
LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
||||
LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
|
||||
LoadState S;
|
||||
LClosure *cl;
|
||||
if (*name == '@' || *name == '=')
|
||||
S.name = name + 1;
|
||||
name = name + 1;
|
||||
else if (*name == LUA_SIGNATURE[0])
|
||||
S.name = "binary string";
|
||||
else
|
||||
S.name = name;
|
||||
name = "binary string";
|
||||
S.name = name;
|
||||
S.L = L;
|
||||
S.Z = Z;
|
||||
S.fixed = cast_byte(fixed);
|
||||
S.offset = 1; /* fist byte was already read */
|
||||
checkHeader(&S);
|
||||
cl = luaF_newLclosure(L, loadByte(&S));
|
||||
setclLvalue2s(L, L->top.p, cl);
|
||||
luaD_inctop(L);
|
||||
S.h = luaH_new(L); /* create list of saved strings */
|
||||
S.nstr = 0;
|
||||
sethvalue2s(L, L->top.p, S.h); /* anchor it */
|
||||
luaD_inctop(L);
|
||||
cl->p = luaF_newproto(L);
|
||||
luaC_objbarrier(L, cl, cl->p);
|
||||
loadFunction(&S, cl->p, NULL);
|
||||
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
||||
loadFunction(&S, cl->p);
|
||||
if (cl->nupvalues != cl->p->sizeupvalues)
|
||||
error(&S, "corrupted chunk");
|
||||
luai_verifycode(L, cl->p);
|
||||
L->top.p--; /* pop table */
|
||||
return cl;
|
||||
}
|
||||
|
||||
|
||||
14
lundump.h
14
lundump.h
@ -7,6 +7,8 @@
|
||||
#ifndef lundump_h
|
||||
#define lundump_h
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "llimits.h"
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
@ -15,19 +17,21 @@
|
||||
/* data to catch conversion errors */
|
||||
#define LUAC_DATA "\x19\x93\r\n\x1a\n"
|
||||
|
||||
#define LUAC_INT 0x5678
|
||||
#define LUAC_NUM cast_num(370.5)
|
||||
#define LUAC_INT -0x5678
|
||||
#define LUAC_INST 0x12345678
|
||||
#define LUAC_NUM cast_num(-370.5)
|
||||
|
||||
/*
|
||||
** Encode major-minor version in one byte, one nibble for each
|
||||
*/
|
||||
#define MYINT(s) (s[0]-'0') /* assume one-digit numerals */
|
||||
#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR))
|
||||
#define LUAC_VERSION (LUA_VERSION_MAJOR_N*16+LUA_VERSION_MINOR_N)
|
||||
|
||||
#define LUAC_FORMAT 0 /* this is the official format */
|
||||
|
||||
|
||||
/* load one chunk; from lundump.c */
|
||||
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
|
||||
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name,
|
||||
int fixed);
|
||||
|
||||
/* dump one chunk; from ldump.c */
|
||||
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
|
||||
|
||||
92
lutf8lib.c
92
lutf8lib.c
@ -10,7 +10,6 @@
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -19,6 +18,7 @@
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
#define MAXUNICODE 0x10FFFFu
|
||||
@ -28,15 +28,6 @@
|
||||
|
||||
#define MSGInvalid "invalid UTF-8 code"
|
||||
|
||||
/*
|
||||
** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
|
||||
*/
|
||||
#if (UINT_MAX >> 30) >= 1
|
||||
typedef unsigned int utfint;
|
||||
#else
|
||||
typedef unsigned long utfint;
|
||||
#endif
|
||||
|
||||
|
||||
#define iscont(c) (((c) & 0xC0) == 0x80)
|
||||
#define iscontp(p) iscont(*(p))
|
||||
@ -55,15 +46,15 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
|
||||
** Decode one UTF-8 sequence, returning NULL if byte sequence is
|
||||
** invalid. The array 'limits' stores the minimum value for each
|
||||
** sequence length, to check for overlong representations. Its first
|
||||
** entry forces an error for non-ascii bytes with no continuation
|
||||
** entry forces an error for non-ASCII bytes with no continuation
|
||||
** bytes (count == 0).
|
||||
*/
|
||||
static const char *utf8_decode (const char *s, utfint *val, int strict) {
|
||||
static const utfint limits[] =
|
||||
{~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
|
||||
static const char *utf8_decode (const char *s, l_uint32 *val, int strict) {
|
||||
static const l_uint32 limits[] =
|
||||
{~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
|
||||
unsigned int c = (unsigned char)s[0];
|
||||
utfint res = 0; /* final result */
|
||||
if (c < 0x80) /* ascii? */
|
||||
l_uint32 res = 0; /* final result */
|
||||
if (c < 0x80) /* ASCII? */
|
||||
res = c;
|
||||
else {
|
||||
int count = 0; /* to count number of continuation bytes */
|
||||
@ -73,7 +64,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) {
|
||||
return NULL; /* invalid byte sequence */
|
||||
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
|
||||
}
|
||||
res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */
|
||||
res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */
|
||||
if (count > 5 || res > MAXUTF || res < limits[count])
|
||||
return NULL; /* invalid byte sequence */
|
||||
s += count; /* skip continuation bytes read */
|
||||
@ -111,7 +102,7 @@ static int utflen (lua_State *L) {
|
||||
lua_pushinteger(L, posi + 1); /* ... and current position */
|
||||
return 2;
|
||||
}
|
||||
posi = s1 - s;
|
||||
posi = ct_diff2S(s1 - s);
|
||||
n++;
|
||||
}
|
||||
lua_pushinteger(L, n);
|
||||
@ -141,11 +132,11 @@ static int codepoint (lua_State *L) {
|
||||
n = 0; /* count the number of returns */
|
||||
se = s + pose; /* string end */
|
||||
for (s += posi - 1; s < se;) {
|
||||
utfint code;
|
||||
l_uint32 code;
|
||||
s = utf8_decode(s, &code, !lax);
|
||||
if (s == NULL)
|
||||
return luaL_error(L, MSGInvalid);
|
||||
lua_pushinteger(L, code);
|
||||
lua_pushinteger(L, l_castU2S(code));
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
@ -181,14 +172,14 @@ static int utfchar (lua_State *L) {
|
||||
|
||||
|
||||
/*
|
||||
** offset(s, n, [i]) -> index where n-th character counting from
|
||||
** position 'i' starts; 0 means character at 'i'.
|
||||
** offset(s, n, [i]) -> indices where n-th character counting from
|
||||
** position 'i' starts and ends; 0 means character at 'i'.
|
||||
*/
|
||||
static int byteoffset (lua_State *L) {
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, 1, &len);
|
||||
lua_Integer n = luaL_checkinteger(L, 2);
|
||||
lua_Integer posi = (n >= 0) ? 1 : len + 1;
|
||||
lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
|
||||
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
||||
"position out of bounds");
|
||||
@ -200,28 +191,37 @@ static int byteoffset (lua_State *L) {
|
||||
if (iscontp(s + posi))
|
||||
return luaL_error(L, "initial position is a continuation byte");
|
||||
if (n < 0) {
|
||||
while (n < 0 && posi > 0) { /* move back */
|
||||
do { /* find beginning of previous character */
|
||||
posi--;
|
||||
} while (posi > 0 && iscontp(s + posi));
|
||||
n++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
n--; /* do not move for 1st character */
|
||||
while (n > 0 && posi < (lua_Integer)len) {
|
||||
do { /* find beginning of next character */
|
||||
posi++;
|
||||
} while (iscontp(s + posi)); /* (cannot pass final '\0') */
|
||||
n--;
|
||||
}
|
||||
}
|
||||
while (n < 0 && posi > 0) { /* move back */
|
||||
do { /* find beginning of previous character */
|
||||
posi--;
|
||||
} while (posi > 0 && iscontp(s + posi));
|
||||
n++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
n--; /* do not move for 1st character */
|
||||
while (n > 0 && posi < (lua_Integer)len) {
|
||||
do { /* find beginning of next character */
|
||||
posi++;
|
||||
} while (iscontp(s + posi)); /* (cannot pass final '\0') */
|
||||
n--;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (n == 0) /* did it find given character? */
|
||||
lua_pushinteger(L, posi + 1);
|
||||
else /* no such character */
|
||||
if (n != 0) { /* did not find given character? */
|
||||
luaL_pushfail(L);
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
lua_pushinteger(L, posi + 1); /* initial position */
|
||||
if ((s[posi] & 0x80) != 0) { /* multi-byte character? */
|
||||
if (iscont(s[posi]))
|
||||
return luaL_error(L, "initial position is a continuation byte");
|
||||
while (iscontp(s + posi + 1))
|
||||
posi++; /* skip to last continuation byte */
|
||||
}
|
||||
/* else one-byte character: final position is the initial one */
|
||||
lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
@ -235,12 +235,12 @@ static int iter_aux (lua_State *L, int strict) {
|
||||
if (n >= len) /* (also handles original 'n' being negative) */
|
||||
return 0; /* no more codepoints */
|
||||
else {
|
||||
utfint code;
|
||||
l_uint32 code;
|
||||
const char *next = utf8_decode(s + n, &code, strict);
|
||||
if (next == NULL || iscontp(next))
|
||||
return luaL_error(L, MSGInvalid);
|
||||
lua_pushinteger(L, n + 1);
|
||||
lua_pushinteger(L, code);
|
||||
lua_pushinteger(L, l_castU2S(n + 1));
|
||||
lua_pushinteger(L, l_castU2S(code));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
45
lvm.h
45
lvm.h
@ -43,7 +43,7 @@
|
||||
typedef enum {
|
||||
F2Ieq, /* no rounding; accepts only integral values */
|
||||
F2Ifloor, /* takes the floor of the number */
|
||||
F2Iceil /* takes the ceil of the number */
|
||||
F2Iceil /* takes the ceiling of the number */
|
||||
} F2Imod;
|
||||
|
||||
|
||||
@ -76,38 +76,33 @@ typedef enum {
|
||||
|
||||
|
||||
/*
|
||||
** fast track for 'gettable': if 't' is a table and 't[k]' is present,
|
||||
** return 1 with 'slot' pointing to 't[k]' (position of final result).
|
||||
** Otherwise, return 0 (meaning it will have to check metamethod)
|
||||
** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL
|
||||
** (otherwise). 'f' is the raw get function to use.
|
||||
** fast track for 'gettable'
|
||||
*/
|
||||
#define luaV_fastget(L,t,k,slot,f) \
|
||||
(!ttistable(t) \
|
||||
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
|
||||
: (slot = f(hvalue(t), k), /* else, do raw access */ \
|
||||
!isempty(slot))) /* result not empty? */
|
||||
#define luaV_fastget(t,k,res,f, tag) \
|
||||
(tag = (!ttistable(t) ? LUA_VNOTABLE : f(hvalue(t), k, res)))
|
||||
|
||||
|
||||
/*
|
||||
** Special case of 'luaV_fastget' for integers, inlining the fast case
|
||||
** of 'luaH_getint'.
|
||||
*/
|
||||
#define luaV_fastgeti(L,t,k,slot) \
|
||||
(!ttistable(t) \
|
||||
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
|
||||
: (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \
|
||||
? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \
|
||||
!isempty(slot))) /* result not empty? */
|
||||
#define luaV_fastgeti(t,k,res,tag) \
|
||||
if (!ttistable(t)) tag = LUA_VNOTABLE; \
|
||||
else { luaH_fastgeti(hvalue(t), k, res, tag); }
|
||||
|
||||
|
||||
#define luaV_fastset(t,k,val,hres,f) \
|
||||
(hres = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, val)))
|
||||
|
||||
#define luaV_fastseti(t,k,val,hres) \
|
||||
if (!ttistable(t)) hres = HNOTATABLE; \
|
||||
else { luaH_fastseti(hvalue(t), k, val, hres); }
|
||||
|
||||
|
||||
/*
|
||||
** Finish a fast set operation (when fast get succeeds). In that case,
|
||||
** 'slot' points to the place to put the value.
|
||||
** Finish a fast set operation (when fast set succeeds).
|
||||
*/
|
||||
#define luaV_finishfastset(L,t,slot,v) \
|
||||
{ setobj2t(L, cast(TValue *,slot), v); \
|
||||
luaC_barrierback(L, gcvalue(t), v); }
|
||||
#define luaV_finishfastset(L,t,v) luaC_barrierback(L, gcvalue(t), v)
|
||||
|
||||
|
||||
/*
|
||||
@ -125,10 +120,10 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
|
||||
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
|
||||
F2Imod mode);
|
||||
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
|
||||
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
|
||||
StkId val, const TValue *slot);
|
||||
LUAI_FUNC lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key,
|
||||
StkId val, lu_byte tag);
|
||||
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
|
||||
TValue *val, const TValue *slot);
|
||||
TValue *val, int aux);
|
||||
LUAI_FUNC void luaV_finishOp (lua_State *L);
|
||||
LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci);
|
||||
LUAI_FUNC void luaV_concat (lua_State *L, int total);
|
||||
|
||||
37
lzio.c
37
lzio.c
@ -14,6 +14,7 @@
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
#include "llimits.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
@ -45,17 +46,25 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
|
||||
static int checkbuffer (ZIO *z) {
|
||||
if (z->n == 0) { /* no bytes in buffer? */
|
||||
if (luaZ_fill(z) == EOZ) /* try to read more */
|
||||
return 0; /* no more input */
|
||||
else {
|
||||
z->n++; /* luaZ_fill consumed first byte; put it back */
|
||||
z->p--;
|
||||
}
|
||||
}
|
||||
return 1; /* now buffer has something */
|
||||
}
|
||||
|
||||
|
||||
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
||||
while (n) {
|
||||
size_t m;
|
||||
if (z->n == 0) { /* no bytes in buffer? */
|
||||
if (luaZ_fill(z) == EOZ) /* try to read more */
|
||||
return n; /* no more input; return number of missing bytes */
|
||||
else {
|
||||
z->n++; /* luaZ_fill consumed first byte; put it back */
|
||||
z->p--;
|
||||
}
|
||||
}
|
||||
if (!checkbuffer(z))
|
||||
return n; /* no more input; return number of missing bytes */
|
||||
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
@ -66,3 +75,15 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
const void *luaZ_getaddr (ZIO* z, size_t n) {
|
||||
const void *res;
|
||||
if (!checkbuffer(z))
|
||||
return NULL; /* no more input */
|
||||
if (z->n < n) /* not enough bytes? */
|
||||
return NULL; /* block not whole; cannot give an address */
|
||||
res = z->p; /* get block address */
|
||||
z->n -= n; /* consume these bytes */
|
||||
z->p += n;
|
||||
return res;
|
||||
}
|
||||
|
||||
3
lzio.h
3
lzio.h
@ -32,7 +32,7 @@ typedef struct Mbuffer {
|
||||
#define luaZ_sizebuffer(buff) ((buff)->buffsize)
|
||||
#define luaZ_bufflen(buff) ((buff)->n)
|
||||
|
||||
#define luaZ_buffremove(buff,i) ((buff)->n -= (i))
|
||||
#define luaZ_buffremove(buff,i) ((buff)->n -= cast_sizet(i))
|
||||
#define luaZ_resetbuffer(buff) ((buff)->n = 0)
|
||||
|
||||
|
||||
@ -48,6 +48,7 @@ LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
|
||||
void *data);
|
||||
LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
|
||||
|
||||
LUAI_FUNC const void *luaZ_getaddr (ZIO* z, size_t n);
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
71
makefile
71
makefile
@ -14,13 +14,12 @@ CWARNSCPP= \
|
||||
-Wdisabled-optimization \
|
||||
-Wdouble-promotion \
|
||||
-Wmissing-declarations \
|
||||
-Wconversion \
|
||||
# the next warnings might be useful sometimes,
|
||||
# but usually they generate too much noise
|
||||
# -Wstrict-overflow=2 \
|
||||
# -Werror \
|
||||
# -pedantic # warns if we use jump tables \
|
||||
# -Wconversion \
|
||||
# -Wsign-conversion \
|
||||
# -Wstrict-overflow=2 \
|
||||
# -Wformat=2 \
|
||||
# -Wcast-qual \
|
||||
|
||||
@ -63,16 +62,18 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC)
|
||||
# ASAN_OPTIONS="detect_invalid_pointer_pairs=2".
|
||||
# -fsanitize=undefined
|
||||
# -fsanitize=pointer-subtract -fsanitize=address -fsanitize=pointer-compare
|
||||
# TESTS= -DLUA_USER_H='"ltests.h"' -O0 -g
|
||||
# TESTS= -DLUA_USER_H='"ltests.h"' -Og -g
|
||||
|
||||
|
||||
LOCAL = $(TESTS) $(CWARNS)
|
||||
|
||||
|
||||
# enable Linux goodies
|
||||
MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_USE_READLINE
|
||||
MYLDFLAGS= $(LOCAL) -Wl,-E
|
||||
MYLIBS= -ldl -lreadline
|
||||
# To enable Linux goodies, -DLUA_USE_LINUX
|
||||
# For C89, "-std=c89 -DLUA_USE_C89"
|
||||
# Note that Linux/Posix options are not compatible with C89
|
||||
MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX
|
||||
MYLDFLAGS= -Wl,-E
|
||||
MYLIBS= -ldl
|
||||
|
||||
|
||||
CC= gcc
|
||||
@ -144,40 +145,45 @@ $(ALL_O): makefile ltests.h
|
||||
lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \
|
||||
ltable.h lundump.h lvm.h
|
||||
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h
|
||||
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h llimits.h
|
||||
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
||||
llimits.h
|
||||
lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||
ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||
lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
ldo.h lgc.h lstring.h ltable.h lvm.h lopnames.h
|
||||
lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
||||
llimits.h
|
||||
lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h
|
||||
ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
||||
ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \
|
||||
ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h
|
||||
ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
|
||||
lparser.h lstring.h ltable.h lundump.h lvm.h
|
||||
ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \
|
||||
ltm.h lzio.h lmem.h lundump.h
|
||||
ldump.o: ldump.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h lgc.h ltable.h lundump.h
|
||||
lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h
|
||||
lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
|
||||
linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h
|
||||
liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h llimits.h
|
||||
liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
||||
llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \
|
||||
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \
|
||||
lstring.h ltable.h
|
||||
lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
||||
llimits.h
|
||||
lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h
|
||||
loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
||||
llimits.h
|
||||
lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \
|
||||
ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \
|
||||
lvm.h
|
||||
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h
|
||||
loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h \
|
||||
lobject.h
|
||||
loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
||||
lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||
ldo.h lfunc.h lstring.h lgc.h ltable.h
|
||||
@ -186,25 +192,28 @@ lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lstring.h ltable.h
|
||||
lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
||||
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
||||
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
||||
llimits.h
|
||||
ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
||||
llimits.h
|
||||
ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \
|
||||
lparser.h lctype.h ldebug.h ldo.h lfunc.h lopnames.h lstring.h lgc.h \
|
||||
ltable.h lualib.h
|
||||
ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
||||
lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
||||
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
|
||||
lundump.h
|
||||
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \
|
||||
ltable.h lvm.h ljumptab.h
|
||||
lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
|
||||
lobject.h ltm.h lzio.h
|
||||
ltable.h lundump.h
|
||||
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
||||
llimits.h
|
||||
lvm.o: lvm.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
|
||||
lstring.h ltable.h lvm.h ljumptab.h
|
||||
lzio.o: lzio.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h
|
||||
|
||||
# (end of Makefile)
|
||||
|
||||
10
manual/2html
10
manual/2html
@ -8,11 +8,11 @@
|
||||
|
||||
---------------------------------------------------------------
|
||||
header = [[
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lua 5.4 Reference Manual</title>
|
||||
<title>Lua 5.5 Reference Manual</title>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
|
||||
<link rel="stylesheet" href="lua.css">
|
||||
<link rel="stylesheet" href="manual.css">
|
||||
@ -23,14 +23,14 @@ header = [[
|
||||
<hr>
|
||||
<h1>
|
||||
<a href="http://www.lua.org/home.html"><img src="logo.gif" alt="[Lua logo]" border="0"></a>
|
||||
Lua 5.4 Reference Manual
|
||||
Lua 5.5 Reference Manual
|
||||
</h1>
|
||||
|
||||
by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
|
||||
<p>
|
||||
<small>
|
||||
<a href="http://www.lua.org/copyright.html">Copyright</a>
|
||||
© 2023 Lua.org, PUC-Rio. All rights reserved.
|
||||
© 2025 Lua.org, PUC-Rio. All rights reserved.
|
||||
</small>
|
||||
<hr>
|
||||
|
||||
@ -358,7 +358,7 @@ item = function (s)
|
||||
local t, p = string.match(s, "^([^\n|]+)|()")
|
||||
if t then
|
||||
s = string.sub(s, p)
|
||||
s = Tag.b(t..": ") .. s
|
||||
s = Tag.b(t) ..": " .. s
|
||||
end
|
||||
return Tag.li(fixpara(s))
|
||||
end,
|
||||
|
||||
1500
manual/manual.of
1500
manual/manual.of
File diff suppressed because it is too large
Load Diff
25
onelua.c
25
onelua.c
@ -5,10 +5,14 @@
|
||||
**
|
||||
** $ gcc -O2 -std=c99 -o lua onelua.c -lm
|
||||
**
|
||||
** or
|
||||
** or (for C89)
|
||||
**
|
||||
** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm
|
||||
**
|
||||
** or (for Linux)
|
||||
**
|
||||
** gcc -O2 -o lua -DLUA_USE_LINUX -Wl,-E onelua.c -lm -ldl
|
||||
**
|
||||
*/
|
||||
|
||||
/* default is to build the full interpreter */
|
||||
@ -30,7 +34,15 @@
|
||||
#define LUA_USE_LINUX
|
||||
#define LUA_USE_MACOSX
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_ANSI
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Other specific features
|
||||
*/
|
||||
#if 0
|
||||
#define LUA_32BITS
|
||||
#define LUA_USE_C89
|
||||
#endif
|
||||
|
||||
|
||||
@ -54,12 +66,10 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
/* setup for luaconf.h */
|
||||
#define LUA_CORE
|
||||
#define LUA_LIB
|
||||
#define ltable_c
|
||||
#define lvm_c
|
||||
|
||||
#include "luaconf.h"
|
||||
|
||||
/* do not export internal symbols */
|
||||
@ -110,6 +120,11 @@
|
||||
#include "linit.c"
|
||||
#endif
|
||||
|
||||
/* test library -- used only for internal development */
|
||||
#if defined(LUA_DEBUG)
|
||||
#include "ltests.c"
|
||||
#endif
|
||||
|
||||
/* lua */
|
||||
#ifdef MAKE_LUA
|
||||
#include "lua.c"
|
||||
|
||||
47
testes/all.lua
Normal file → Executable file
47
testes/all.lua
Normal file → Executable file
@ -1,9 +1,13 @@
|
||||
#!../lua
|
||||
-- $Id: testes/all.lua $
|
||||
-- See Copyright Notice at the end of this file
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
local version = "Lua 5.4"
|
||||
global _soft, _port, _nomsg
|
||||
global T
|
||||
|
||||
local version = "Lua 5.5"
|
||||
if _VERSION ~= version then
|
||||
io.stderr:write("This test suite is for ", version,
|
||||
", not for ", _VERSION, "\nExiting tests")
|
||||
@ -28,14 +32,13 @@ _nomsg = rawget(_G, "_nomsg") or false
|
||||
local usertests = rawget(_G, "_U")
|
||||
|
||||
if usertests then
|
||||
-- tests for sissies ;) Avoid problems
|
||||
_soft = true
|
||||
_port = true
|
||||
_nomsg = true
|
||||
_soft = true -- avoid tests that take too long
|
||||
_port = true -- avoid non-portable tests
|
||||
_nomsg = true -- avoid messages about tests not performed
|
||||
end
|
||||
|
||||
-- tests should require debug when needed
|
||||
debug = nil
|
||||
global debug; debug = nil
|
||||
|
||||
|
||||
if usertests then
|
||||
@ -72,7 +75,7 @@ do -- (
|
||||
|
||||
-- track messages for tests not performed
|
||||
local msgs = {}
|
||||
function Message (m)
|
||||
global function Message (m)
|
||||
if not _nomsg then
|
||||
print(m)
|
||||
msgs[#msgs+1] = string.sub(m, 3, -3)
|
||||
@ -183,6 +186,7 @@ dofile('nextvar.lua')
|
||||
dofile('pm.lua')
|
||||
dofile('utf8.lua')
|
||||
dofile('api.lua')
|
||||
dofile('memerr.lua')
|
||||
assert(dofile('events.lua') == 12)
|
||||
dofile('vararg.lua')
|
||||
dofile('closure.lua')
|
||||
@ -283,30 +287,3 @@ end
|
||||
|
||||
print("final OK !!!")
|
||||
|
||||
|
||||
|
||||
--[[
|
||||
*****************************************************************************
|
||||
* Copyright (C) 1994-2016 Lua.org, PUC-Rio.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*****************************************************************************
|
||||
]]
|
||||
|
||||
|
||||
407
testes/api.lua
407
testes/api.lua
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/api.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
if T==nil then
|
||||
(Message or print)('\n >>> testC not active: skipping API tests <<<\n')
|
||||
@ -11,9 +11,6 @@ local debug = require "debug"
|
||||
local pack = table.pack
|
||||
|
||||
|
||||
-- standard error message for memory errors
|
||||
local MEMERRMSG = "not enough memory"
|
||||
|
||||
local function tcheck (t1, t2)
|
||||
assert(t1.n == (t2.n or #t2) + 1)
|
||||
for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
|
||||
@ -117,7 +114,7 @@ end
|
||||
|
||||
-- testing warnings
|
||||
T.testC([[
|
||||
warningC "#This shold be a"
|
||||
warningC "#This should be a"
|
||||
warningC " single "
|
||||
warning "warning"
|
||||
warningC "#This should be "
|
||||
@ -165,6 +162,23 @@ do -- test returning more results than fit in the caller stack
|
||||
end
|
||||
|
||||
|
||||
do -- testing multiple returns
|
||||
local function foo (n)
|
||||
if n > 0 then return n, foo(n - 1) end
|
||||
end
|
||||
|
||||
local t = {T.testC("call 1 10; return 10", foo, 20)}
|
||||
assert(t[1] == 20 and t[10] == 11 and t[11] == nil)
|
||||
|
||||
local t = table.pack(T.testC("call 1 10; return 10", foo, 2))
|
||||
assert(t[1] == 2 and t[2] == 1 and t[3] == nil and t.n == 10)
|
||||
|
||||
local t = {T.testC([[
|
||||
checkstack 300 "error"; call 1 250; return 250]], foo, 250)}
|
||||
assert(t[1] == 250 and t[250] == 1 and t[251] == nil)
|
||||
end
|
||||
|
||||
|
||||
-- testing globals
|
||||
_G.AA = 14; _G.BB = "a31"
|
||||
local a = {T.testC[[
|
||||
@ -232,7 +246,8 @@ assert(not T.testC("compare LT 1 4, return 1"))
|
||||
assert(not T.testC("compare LE 9 1, return 1"))
|
||||
assert(not T.testC("compare EQ 9 9, return 1"))
|
||||
|
||||
local b = {__lt = function (a,b) return a[1] < b[1] end}
|
||||
local b = {__lt = function (a,b) return a[1] < b[1] end,
|
||||
__le = function (a,b) return a[1] <= b[1] end}
|
||||
local a1,a3,a4 = setmetatable({1}, b),
|
||||
setmetatable({3}, b),
|
||||
setmetatable({4}, b)
|
||||
@ -399,6 +414,10 @@ do
|
||||
-- trivial error
|
||||
assert(T.checkpanic("pushstring hi; error") == "hi")
|
||||
|
||||
-- thread status inside panic (bug in 5.4.4)
|
||||
assert(T.checkpanic("pushstring hi; error", "threadstatus; return 2") ==
|
||||
"ERRRUN")
|
||||
|
||||
-- using the stack inside panic
|
||||
assert(T.checkpanic("pushstring hi; error;",
|
||||
[[checkstack 5 XX
|
||||
@ -407,20 +426,30 @@ do
|
||||
concat 3]]) == "hi alo mundo")
|
||||
|
||||
-- "argerror" without frames
|
||||
assert(T.checkpanic("loadstring 4") ==
|
||||
assert(T.checkpanic("loadstring 4 name bt") ==
|
||||
"bad argument #4 (string expected, got no value)")
|
||||
|
||||
|
||||
-- memory error
|
||||
T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
|
||||
assert(T.checkpanic("newuserdata 20000") == MEMERRMSG)
|
||||
T.totalmem(0) -- restore high limit
|
||||
-- memory error + thread status
|
||||
local x = T.checkpanic(
|
||||
[[ alloccount 0 # force a memory error in next line
|
||||
newtable
|
||||
]],
|
||||
[[
|
||||
alloccount -1 # allow free allocations again
|
||||
pushstring XX
|
||||
threadstatus
|
||||
concat 2 # to make sure message came from here
|
||||
return 1
|
||||
]])
|
||||
T.alloccount()
|
||||
assert(x == "XX" .. "not enough memory")
|
||||
|
||||
-- stack error
|
||||
if not _soft then
|
||||
local msg = T.checkpanic[[
|
||||
pushstring "function f() f() end"
|
||||
loadstring -1; call 0 0
|
||||
loadstring -1 name t; call 0 0
|
||||
getglobal f; call 0 0
|
||||
]]
|
||||
assert(string.find(msg, "stack overflow"))
|
||||
@ -430,7 +459,7 @@ do
|
||||
assert(T.checkpanic([[
|
||||
pushstring "return {__close = function () Y = 'ho'; end}"
|
||||
newtable
|
||||
loadstring -2
|
||||
loadstring -2 name t
|
||||
call 0 1
|
||||
setmetatable -2
|
||||
toclose -1
|
||||
@ -458,6 +487,8 @@ if not _soft then
|
||||
print'+'
|
||||
end
|
||||
|
||||
|
||||
|
||||
local lim = _soft and 500 or 12000
|
||||
local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
|
||||
for i = 1,lim do
|
||||
@ -465,7 +496,7 @@ for i = 1,lim do
|
||||
prog[#prog + 1] = "pushnum " .. i * 10
|
||||
end
|
||||
|
||||
prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
|
||||
prog[#prog + 1] = "rawgeti R !G" -- get global table in registry
|
||||
prog[#prog + 1] = "insert " .. -(2*lim + 2)
|
||||
|
||||
for i = 1,lim do
|
||||
@ -481,10 +512,20 @@ for i = 1,lim do assert(t[i] == i*10); t[i] = undef end
|
||||
assert(next(t) == nil)
|
||||
prog, g, t = nil
|
||||
|
||||
do -- shrink stack
|
||||
local m1, m2 = 0, collectgarbage"count" * 1024
|
||||
while m1 ~= m2 do -- repeat until stable
|
||||
collectgarbage()
|
||||
m1 = m2
|
||||
m2 = collectgarbage"count" * 1024
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- testing errors
|
||||
|
||||
a = T.testC([[
|
||||
loadstring 2; pcall 0 1 0;
|
||||
loadstring 2 name t; pcall 0 1 0;
|
||||
pushvalue 3; insert -2; pcall 1 1 0;
|
||||
pcall 0 0 0;
|
||||
return 1
|
||||
@ -498,7 +539,7 @@ local function check3(p, ...)
|
||||
assert(#arg == 3)
|
||||
assert(string.find(arg[3], p))
|
||||
end
|
||||
check3(":1:", T.testC("loadstring 2; return *", "x="))
|
||||
check3(":1:", T.testC("loadstring 2 name t; return *", "x="))
|
||||
check3("%.", T.testC("loadfile 2; return *", "."))
|
||||
check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
|
||||
|
||||
@ -509,6 +550,53 @@ local function checkerrnopro (code, msg)
|
||||
assert(not stt and string.find(err, msg))
|
||||
end
|
||||
|
||||
|
||||
do
|
||||
print("testing load of binaries in fixed buffers")
|
||||
local source = {}
|
||||
local N = 1000
|
||||
-- create a somewhat "large" source
|
||||
for i = 1, N do source[i] = "X = X + 1; " end
|
||||
-- add a long string to the source
|
||||
source[#source + 1] = string.format("Y = '%s'", string.rep("a", N));
|
||||
source = table.concat(source)
|
||||
-- give chunk an explicit name to avoid using source as name
|
||||
source = load(source, "name1")
|
||||
-- dump without debug information
|
||||
source = string.dump(source, true)
|
||||
-- each "X=X+1" generates 4 opcodes with 4 bytes each, plus the string
|
||||
assert(#source > N * 4 * 4 + N)
|
||||
collectgarbage(); collectgarbage()
|
||||
local m1 = collectgarbage"count" * 1024
|
||||
-- load dump using fixed buffer
|
||||
local code = T.testC([[
|
||||
loadstring 2 name B;
|
||||
return 1
|
||||
]], source)
|
||||
collectgarbage()
|
||||
local m2 = collectgarbage"count" * 1024
|
||||
-- load used fewer than 400 bytes. Code alone has more than 3*N bytes,
|
||||
-- and string literal has N bytes. Both were not loaded.
|
||||
assert(m2 > m1 and m2 - m1 < 400)
|
||||
X = 0; code(); assert(X == N and Y == string.rep("a", N))
|
||||
X = nil; Y = nil
|
||||
|
||||
-- testing debug info in fixed buffers
|
||||
source = {"X = 0"}
|
||||
for i = 2, 300 do source[i] = "X = X + 1" end
|
||||
source[#source + 1] = "X = X + {}" -- error in last line
|
||||
source = table.concat(source, "\n")
|
||||
source = load(source, "name1")
|
||||
source = string.dump(source)
|
||||
-- load dump using fixed buffer
|
||||
local code = T.testC([[
|
||||
loadstring 2 name B;
|
||||
return 1
|
||||
]], source)
|
||||
checkerr(":301:", code) -- correct line information
|
||||
end
|
||||
|
||||
|
||||
if not _soft then
|
||||
collectgarbage("stop") -- avoid __gc with full stack
|
||||
checkerrnopro("pushnum 3; call 0 0", "attempt to call")
|
||||
@ -723,7 +811,7 @@ assert(debug.getuservalue(b) == 134)
|
||||
-- test barrier for uservalues
|
||||
do
|
||||
local oldmode = collectgarbage("incremental")
|
||||
T.gcstate("atomic")
|
||||
T.gcstate("enteratomic")
|
||||
assert(T.gccolor(b) == "black")
|
||||
debug.setuservalue(b, {x = 100})
|
||||
T.gcstate("pause") -- complete collection
|
||||
@ -815,7 +903,7 @@ F = function (x)
|
||||
assert(T.udataval(A) == B)
|
||||
debug.getmetatable(A) -- just access it
|
||||
end
|
||||
A = x -- ressurect userdata
|
||||
A = x -- resurrect userdata
|
||||
B = udval
|
||||
return 1,2,3
|
||||
end
|
||||
@ -871,28 +959,30 @@ checkerr("FILE%* expected, got userdata", io.input, x)
|
||||
|
||||
assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
|
||||
|
||||
local d = T.ref(a);
|
||||
local e = T.ref(b);
|
||||
local f = T.ref(c);
|
||||
t = {T.getref(d), T.getref(e), T.getref(f)}
|
||||
-- Test references in an arbitrary table
|
||||
local reftable = {}
|
||||
local d = T.ref(a, reftable);
|
||||
local e = T.ref(b, reftable);
|
||||
local f = T.ref(c, reftable);
|
||||
t = {T.getref(d, reftable), T.getref(e, reftable), T.getref(f, reftable)}
|
||||
assert(t[1] == a and t[2] == b and t[3] == c)
|
||||
|
||||
t=nil; a=nil; c=nil;
|
||||
T.unref(e); T.unref(f)
|
||||
T.unref(e, reftable); T.unref(f, reftable)
|
||||
|
||||
collectgarbage()
|
||||
|
||||
-- check that unref objects have been collected
|
||||
assert(#cl == 1 and cl[1] == nc)
|
||||
|
||||
x = T.getref(d)
|
||||
x = T.getref(d, reftable)
|
||||
assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
|
||||
x =nil
|
||||
tt.b = b -- create cycle
|
||||
tt=nil -- frees tt for GC
|
||||
A = nil
|
||||
b = nil
|
||||
T.unref(d);
|
||||
T.unref(d, reftable);
|
||||
local n5 = T.newuserdata(0)
|
||||
debug.setmetatable(n5, {__gc=F})
|
||||
n5 = T.udataval(n5)
|
||||
@ -901,6 +991,21 @@ assert(#cl == 4)
|
||||
-- check order of collection
|
||||
assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
|
||||
|
||||
-- reuse a reference in 'reftable'
|
||||
T.unref(T.ref(23, reftable), reftable)
|
||||
|
||||
do -- check reftable
|
||||
local count = 0
|
||||
local i = 1
|
||||
while reftable[i] ~= 0 do
|
||||
i = reftable[i] -- traverse linked list of free references
|
||||
count = count + 1
|
||||
end
|
||||
-- maximum number of simultaneously locked objects was 3
|
||||
assert(count == 3 and #reftable == 3 + 1) -- +1 for reserved [1]
|
||||
end
|
||||
|
||||
|
||||
collectgarbage"restart"
|
||||
|
||||
|
||||
@ -1046,10 +1151,12 @@ assert(a == nil and c == 2) -- 2 == run-time error
|
||||
a, b, c = T.doremote(L1, "return a+")
|
||||
assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
|
||||
|
||||
T.loadlib(L1)
|
||||
T.loadlib(L1, 2, ~2) -- load only 'package', preload all others
|
||||
a, b, c = T.doremote(L1, [[
|
||||
string = require'string'
|
||||
a = require'_G'; assert(a == _G and require("_G") == a)
|
||||
local initialG = _G -- not loaded yet
|
||||
local a = require'_G'; assert(a == _G and require("_G") == a)
|
||||
assert(initialG == nil and io == nil) -- now we have 'assert'
|
||||
io = require'io'; assert(type(io.read) == "function")
|
||||
assert(require("io") == io)
|
||||
a = require'table'; assert(type(a.insert) == "function")
|
||||
@ -1063,7 +1170,7 @@ T.closestate(L1);
|
||||
|
||||
|
||||
L1 = T.newstate()
|
||||
T.loadlib(L1)
|
||||
T.loadlib(L1, 0, 0)
|
||||
T.doremote(L1, "a = {}")
|
||||
T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
|
||||
settable -3]])
|
||||
@ -1115,7 +1222,8 @@ do
|
||||
local a, b = pcall(T.makeCfunc[[
|
||||
call 0 1 # create resource
|
||||
toclose -1 # mark it to be closed
|
||||
error # resource is the error object
|
||||
pushvalue -1 # replicate it as error object
|
||||
error # resource right after error object
|
||||
]], newresource)
|
||||
assert(a == false and b[1] == 11)
|
||||
assert(#openresource == 0) -- was closed
|
||||
@ -1191,241 +1299,6 @@ do
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
** {==================================================================
|
||||
** Testing memory limits
|
||||
** ===================================================================
|
||||
--]]
|
||||
|
||||
print("memory-allocation errors")
|
||||
|
||||
checkerr("block too big", T.newuserdata, math.maxinteger)
|
||||
collectgarbage()
|
||||
local f = load"local a={}; for i=1,100000 do a[i]=i end"
|
||||
T.alloccount(10)
|
||||
checkerr(MEMERRMSG, f)
|
||||
T.alloccount() -- remove limit
|
||||
|
||||
|
||||
-- test memory errors; increase limit for maximum memory by steps,
|
||||
-- o that we get memory errors in all allocations of a given
|
||||
-- task, until there is enough memory to complete the task without
|
||||
-- errors.
|
||||
local function testbytes (s, f)
|
||||
collectgarbage()
|
||||
local M = T.totalmem()
|
||||
local oldM = M
|
||||
local a,b = nil
|
||||
while true do
|
||||
collectgarbage(); collectgarbage()
|
||||
T.totalmem(M)
|
||||
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
|
||||
T.totalmem(0) -- remove limit
|
||||
if a and b == "OK" then break end -- stop when no more errors
|
||||
if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
|
||||
error(a, 0) -- propagate it
|
||||
end
|
||||
M = M + 7 -- increase memory limit
|
||||
end
|
||||
print(string.format("minimum memory for %s: %d bytes", s, M - oldM))
|
||||
return a
|
||||
end
|
||||
|
||||
-- test memory errors; increase limit for number of allocations one
|
||||
-- by one, so that we get memory errors in all allocations of a given
|
||||
-- task, until there is enough allocations to complete the task without
|
||||
-- errors.
|
||||
|
||||
local function testalloc (s, f)
|
||||
collectgarbage()
|
||||
local M = 0
|
||||
local a,b = nil
|
||||
while true do
|
||||
collectgarbage(); collectgarbage()
|
||||
T.alloccount(M)
|
||||
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
|
||||
T.alloccount() -- remove limit
|
||||
if a and b == "OK" then break end -- stop when no more errors
|
||||
if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
|
||||
error(a, 0) -- propagate it
|
||||
end
|
||||
M = M + 1 -- increase allocation limit
|
||||
end
|
||||
print(string.format("minimum allocations for %s: %d allocations", s, M))
|
||||
return a
|
||||
end
|
||||
|
||||
|
||||
local function testamem (s, f)
|
||||
testalloc(s, f)
|
||||
return testbytes(s, f)
|
||||
end
|
||||
|
||||
|
||||
-- doing nothing
|
||||
b = testamem("doing nothing", function () return 10 end)
|
||||
assert(b == 10)
|
||||
|
||||
-- testing memory errors when creating a new state
|
||||
|
||||
testamem("state creation", function ()
|
||||
local st = T.newstate()
|
||||
if st then T.closestate(st) end -- close new state
|
||||
return st
|
||||
end)
|
||||
|
||||
testamem("empty-table creation", function ()
|
||||
return {}
|
||||
end)
|
||||
|
||||
testamem("string creation", function ()
|
||||
return "XXX" .. "YYY"
|
||||
end)
|
||||
|
||||
testamem("coroutine creation", function()
|
||||
return coroutine.create(print)
|
||||
end)
|
||||
|
||||
|
||||
-- testing to-be-closed variables
|
||||
testamem("to-be-closed variables", function()
|
||||
local flag
|
||||
do
|
||||
local x <close> =
|
||||
setmetatable({}, {__close = function () flag = true end})
|
||||
flag = false
|
||||
local x = {}
|
||||
end
|
||||
return flag
|
||||
end)
|
||||
|
||||
|
||||
-- testing threads
|
||||
|
||||
-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
|
||||
local mt = T.testC("rawgeti R 1; return 1")
|
||||
assert(type(mt) == "thread" and coroutine.running() == mt)
|
||||
|
||||
|
||||
|
||||
local function expand (n,s)
|
||||
if n==0 then return "" end
|
||||
local e = string.rep("=", n)
|
||||
return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
|
||||
e, s, expand(n-1,s), e)
|
||||
end
|
||||
|
||||
G=0; collectgarbage(); a =collectgarbage("count")
|
||||
load(expand(20,"G=G+1"))()
|
||||
assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
|
||||
G = nil
|
||||
|
||||
testamem("running code on new thread", function ()
|
||||
return T.doonnewstack("local x=1") == 0 -- try to create thread
|
||||
end)
|
||||
|
||||
|
||||
-- testing memory x compiler
|
||||
|
||||
testamem("loadstring", function ()
|
||||
return load("x=1") -- try to do load a string
|
||||
end)
|
||||
|
||||
|
||||
local testprog = [[
|
||||
local function foo () return end
|
||||
local t = {"x"}
|
||||
AA = "aaa"
|
||||
for i = 1, #t do AA = AA .. t[i] end
|
||||
return true
|
||||
]]
|
||||
|
||||
-- testing memory x dofile
|
||||
_G.AA = nil
|
||||
local t =os.tmpname()
|
||||
local f = assert(io.open(t, "w"))
|
||||
f:write(testprog)
|
||||
f:close()
|
||||
testamem("dofile", function ()
|
||||
local a = loadfile(t)
|
||||
return a and a()
|
||||
end)
|
||||
assert(os.remove(t))
|
||||
assert(_G.AA == "aaax")
|
||||
|
||||
|
||||
-- other generic tests
|
||||
|
||||
testamem("gsub", function ()
|
||||
local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
|
||||
return (a == 'ablo ablo')
|
||||
end)
|
||||
|
||||
testamem("dump/undump", function ()
|
||||
local a = load(testprog)
|
||||
local b = a and string.dump(a)
|
||||
a = b and load(b)
|
||||
return a and a()
|
||||
end)
|
||||
|
||||
_G.AA = nil
|
||||
|
||||
local t = os.tmpname()
|
||||
testamem("file creation", function ()
|
||||
local f = assert(io.open(t, 'w'))
|
||||
assert (not io.open"nomenaoexistente")
|
||||
io.close(f);
|
||||
return not loadfile'nomenaoexistente'
|
||||
end)
|
||||
assert(os.remove(t))
|
||||
|
||||
testamem("table creation", function ()
|
||||
local a, lim = {}, 10
|
||||
for i=1,lim do a[i] = i; a[i..'a'] = {} end
|
||||
return (type(a[lim..'a']) == 'table' and a[lim] == lim)
|
||||
end)
|
||||
|
||||
testamem("constructors", function ()
|
||||
local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
|
||||
return (type(a) == 'table' and a.e == 5)
|
||||
end)
|
||||
|
||||
local a = 1
|
||||
local close = nil
|
||||
testamem("closure creation", function ()
|
||||
function close (b)
|
||||
return function (x) return b + x end
|
||||
end
|
||||
return (close(2)(4) == 6)
|
||||
end)
|
||||
|
||||
testamem("using coroutines", function ()
|
||||
local a = coroutine.wrap(function ()
|
||||
coroutine.yield(string.rep("a", 10))
|
||||
return {}
|
||||
end)
|
||||
assert(string.len(a()) == 10)
|
||||
return a()
|
||||
end)
|
||||
|
||||
do -- auxiliary buffer
|
||||
local lim = 100
|
||||
local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
|
||||
testamem("auxiliary buffer", function ()
|
||||
return (#table.concat(a, ",") == 20*lim + lim - 1)
|
||||
end)
|
||||
end
|
||||
|
||||
testamem("growing stack", function ()
|
||||
local function foo (n)
|
||||
if n == 0 then return 1 else return 1 + foo(n - 1) end
|
||||
end
|
||||
return foo(100)
|
||||
end)
|
||||
|
||||
-- }==================================================================
|
||||
|
||||
|
||||
do -- testing failing in 'lua_checkstack'
|
||||
local res = T.testC([[rawcheckstack 500000; return 1]])
|
||||
assert(res == false)
|
||||
@ -1446,10 +1319,10 @@ end
|
||||
|
||||
do -- garbage collection with no extra memory
|
||||
local L = T.newstate()
|
||||
T.loadlib(L)
|
||||
T.loadlib(L, 1 | 2, 0) -- load _G and 'package'
|
||||
local res = (T.doremote(L, [[
|
||||
_ENV = require"_G"
|
||||
local T = require"T"
|
||||
_ENV = _G
|
||||
assert(string == nil)
|
||||
local a = {}
|
||||
for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table
|
||||
local stsize, stuse = T.querystr()
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/attrib.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print "testing require"
|
||||
|
||||
@ -236,7 +236,7 @@ package.path = oldpath
|
||||
local fname = "file_does_not_exist2"
|
||||
local m, err = pcall(require, fname)
|
||||
for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do
|
||||
t = string.gsub(t, "?", fname)
|
||||
local t = string.gsub(t, "?", fname)
|
||||
assert(string.find(err, t, 1, true))
|
||||
end
|
||||
|
||||
@ -308,11 +308,11 @@ else
|
||||
_ENV.x, _ENV.y = nil
|
||||
end
|
||||
|
||||
|
||||
_ENV = _G
|
||||
|
||||
|
||||
-- testing preload
|
||||
|
||||
do
|
||||
local p = package
|
||||
package = {}
|
||||
@ -331,6 +331,26 @@ do
|
||||
assert(type(package.path) == "string")
|
||||
end
|
||||
|
||||
|
||||
do print("testing external strings")
|
||||
package.cpath = DC"?"
|
||||
local lib2 = require"lib2-v2"
|
||||
local t = {}
|
||||
for _, len in ipairs{0, 10, 39, 40, 41, 1000} do
|
||||
local str = string.rep("a", len)
|
||||
local str1 = lib2.newstr(str)
|
||||
assert(str == str1)
|
||||
assert(not T or T.hash(str) == T.hash(str1))
|
||||
t[str1] = 20; assert(t[str] == 20 and t[str1] == 20)
|
||||
t[str] = 10; assert(t[str1] == 10)
|
||||
local tt = {[str1] = str1}
|
||||
assert(next(tt) == str1 and next(tt, str1) == nil)
|
||||
assert(tt[str] == str)
|
||||
local str2 = lib2.newstr(str1)
|
||||
assert(str == str2 and t[str2] == 10 and tt[str2] == str)
|
||||
end
|
||||
end
|
||||
|
||||
print('+')
|
||||
|
||||
end --]
|
||||
@ -447,7 +467,7 @@ do
|
||||
end
|
||||
|
||||
|
||||
-- test of large float/integer indices
|
||||
-- test of large float/integer indices
|
||||
|
||||
-- compute maximum integer where all bits fit in a float
|
||||
local maxint = math.maxinteger
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/big.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
if _soft then
|
||||
return 'a'
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/bitwise.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print("testing bitwise operations")
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ local strsub = string.sub
|
||||
|
||||
local print = print
|
||||
|
||||
_ENV = nil
|
||||
global none
|
||||
|
||||
-- Try to convert a value to an integer, without assuming any coercion.
|
||||
local function toint (x)
|
||||
|
||||
148
testes/calls.lua
148
testes/calls.lua
@ -1,5 +1,7 @@
|
||||
-- $Id: testes/calls.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
print("testing functions and calls")
|
||||
|
||||
@ -22,7 +24,7 @@ assert(not pcall(type))
|
||||
|
||||
|
||||
-- testing local-function recursion
|
||||
fact = false
|
||||
global fact = false
|
||||
do
|
||||
local res = 1
|
||||
local function fact (n)
|
||||
@ -63,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12)
|
||||
|
||||
print('+')
|
||||
|
||||
t = nil -- 'declare' t
|
||||
global t = nil -- 'declare' t
|
||||
function f(a,b,c) local d = 'a'; t={a,b,c,d} end
|
||||
|
||||
f( -- this line change must be valid
|
||||
@ -75,7 +77,7 @@ assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
|
||||
|
||||
t = nil -- delete 't'
|
||||
|
||||
function fat(x)
|
||||
global function fat(x)
|
||||
if x <= 1 then return 1
|
||||
else return x*load("return fat(" .. x-1 .. ")", "")()
|
||||
end
|
||||
@ -107,7 +109,7 @@ end
|
||||
|
||||
_G.deep = nil -- "declaration" (used by 'all.lua')
|
||||
|
||||
function deep (n)
|
||||
global function deep (n)
|
||||
if n>0 then deep(n-1) end
|
||||
end
|
||||
deep(10)
|
||||
@ -178,7 +180,7 @@ do -- tail calls x chain of __call
|
||||
end
|
||||
|
||||
-- build a chain of __call metamethods ending in function 'foo'
|
||||
for i = 1, 100 do
|
||||
for i = 1, 15 do
|
||||
foo = setmetatable({}, {__call = foo})
|
||||
end
|
||||
|
||||
@ -190,8 +192,8 @@ end
|
||||
print('+')
|
||||
|
||||
|
||||
do -- testing chains of '__call'
|
||||
local N = 20
|
||||
do print"testing chains of '__call'"
|
||||
local N = 15
|
||||
local u = table.pack
|
||||
for i = 1, N do
|
||||
u = setmetatable({i}, {__call = u})
|
||||
@ -204,9 +206,37 @@ do -- testing chains of '__call'
|
||||
assert(Res[i][1] == i)
|
||||
end
|
||||
assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
|
||||
|
||||
local function u (...)
|
||||
local n = debug.getinfo(1, 't').extraargs
|
||||
assert(select("#", ...) == n)
|
||||
return n
|
||||
end
|
||||
|
||||
for i = 0, N do
|
||||
assert(u() == i)
|
||||
u = setmetatable({}, {__call = u})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
do -- testing chains too long
|
||||
local a = {}
|
||||
for i = 1, 16 do -- one too many
|
||||
a = setmetatable({}, {__call = a})
|
||||
end
|
||||
local status, msg = pcall(a)
|
||||
assert(not status and string.find(msg, "too long"))
|
||||
|
||||
setmetatable(a, {__call = a}) -- infinite chain
|
||||
local status, msg = pcall(a)
|
||||
assert(not status and string.find(msg, "too long"))
|
||||
|
||||
-- again, with a tail call
|
||||
local status, msg = pcall(function () return a() end)
|
||||
assert(not status and string.find(msg, "too long"))
|
||||
end
|
||||
|
||||
a = nil
|
||||
(function (x) a=x end)(23)
|
||||
assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
||||
@ -324,7 +354,7 @@ assert(not load(function () return true end))
|
||||
|
||||
-- small bug
|
||||
local t = {nil, "return ", "3"}
|
||||
f, msg = load(function () return table.remove(t, 1) end)
|
||||
local f, msg = load(function () return table.remove(t, 1) end)
|
||||
assert(f() == nil) -- should read the empty chunk
|
||||
|
||||
-- another small bug (in 5.2.1)
|
||||
@ -342,20 +372,6 @@ do -- another bug (in 5.4.0)
|
||||
end
|
||||
|
||||
|
||||
do -- another bug (since 5.2)
|
||||
-- corrupted binary dump: list of upvalue names is larger than number
|
||||
-- of upvalues, overflowing the array of upvalues.
|
||||
local code =
|
||||
"\x1b\x4c\x75\x61\x54\x00\x19\x93\x0d\x0a\x1a\x0a\x04\x08\x08\x78\x56\z
|
||||
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x28\x77\x40\x00\x86\x40\z
|
||||
\x74\x65\x6d\x70\x81\x81\x01\x00\x02\x82\x48\x00\x02\x00\xc7\x00\x01\z
|
||||
\x00\x80\x80\x80\x82\x00\x00\x80\x81\x82\x78\x80\x82\x81\x86\x40\x74\z
|
||||
\x65\x6d\x70"
|
||||
|
||||
assert(load(code)) -- segfaults in previous versions
|
||||
end
|
||||
|
||||
|
||||
x = string.dump(load("x = 1; return x"))
|
||||
a = assert(load(read1(x), nil, "b"))
|
||||
assert(a() == 1 and _G.x == 1)
|
||||
@ -374,7 +390,8 @@ assert(load("return _ENV", nil, nil, 123)() == 123)
|
||||
|
||||
|
||||
-- load when _ENV is not first upvalue
|
||||
local x; XX = 123
|
||||
global XX; local x
|
||||
XX = 123
|
||||
local function h ()
|
||||
local y=x -- use 'x', so that it becomes 1st upvalue
|
||||
return XX -- global name
|
||||
@ -466,15 +483,22 @@ assert((function (a) return a end)() == nil)
|
||||
|
||||
print("testing binary chunks")
|
||||
do
|
||||
local header = string.pack("c4BBc6BBB",
|
||||
"\27Lua", -- signature
|
||||
0x54, -- version 5.4 (0x54)
|
||||
0, -- format
|
||||
"\x19\x93\r\n\x1a\n", -- data
|
||||
4, -- size of instruction
|
||||
string.packsize("j"), -- sizeof(lua integer)
|
||||
string.packsize("n") -- sizeof(lua number)
|
||||
)
|
||||
local headformat = "c4BBc6BiBI4BjBn"
|
||||
local header = { -- header components
|
||||
"\27Lua", -- signature
|
||||
0x55, -- version 5.5 (0x55)
|
||||
0, -- format
|
||||
"\x19\x93\r\n\x1a\n", -- a binary string
|
||||
string.packsize("i"), -- size of an int
|
||||
-0x5678, -- an int
|
||||
4, -- size of an instruction
|
||||
0x12345678, -- an instruction (4 bytes)
|
||||
string.packsize("j"), -- size of a Lua integer
|
||||
-0x5678, -- a Lua integer
|
||||
string.packsize("n"), -- size of a Lua float
|
||||
-370.5, -- a Lua float
|
||||
}
|
||||
|
||||
local c = string.dump(function ()
|
||||
local a = 1; local b = 3;
|
||||
local f = function () return a + b + _ENV.c; end -- upvalues
|
||||
@ -486,17 +510,23 @@ do
|
||||
assert(assert(load(c))() == 10)
|
||||
|
||||
-- check header
|
||||
assert(string.sub(c, 1, #header) == header)
|
||||
-- check LUAC_INT and LUAC_NUM
|
||||
local ci, cn = string.unpack("jn", c, #header + 1)
|
||||
assert(ci == 0x5678 and cn == 370.5)
|
||||
|
||||
-- corrupted header
|
||||
local t = {string.unpack(headformat, c)}
|
||||
for i = 1, #header do
|
||||
assert(t[i] == header[i])
|
||||
end
|
||||
|
||||
-- Testing corrupted header.
|
||||
-- A single wrong byte in the head invalidates the chunk,
|
||||
-- except for the Lua float check. (If numbers are long double,
|
||||
-- the representation may need padding, and changing that padding
|
||||
-- will not invalidate the chunk.)
|
||||
local headlen = string.packsize(headformat)
|
||||
headlen = headlen - string.packsize("n") -- remove float check
|
||||
for i = 1, headlen do
|
||||
local s = string.sub(c, 1, i - 1) ..
|
||||
string.char(string.byte(string.sub(c, i, i)) + 1) ..
|
||||
string.char((string.byte(string.sub(c, i, i)) + 1) & 0xFF) ..
|
||||
string.sub(c, i + 1, -1)
|
||||
assert(#s == #c)
|
||||
assert(#s == #c and s ~= c)
|
||||
assert(not load(s))
|
||||
end
|
||||
|
||||
@ -507,5 +537,41 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
do -- check reuse of strings in dumps
|
||||
local str = "|" .. string.rep("X", 50) .. "|"
|
||||
local foo = load(string.format([[
|
||||
local str <const> = "%s"
|
||||
return {
|
||||
function () return str end,
|
||||
function () return str end,
|
||||
function () return str end
|
||||
}
|
||||
]], str))
|
||||
-- count occurrences of 'str' inside the dump
|
||||
local dump = string.dump(foo)
|
||||
local _, count = string.gsub(dump, str, {})
|
||||
-- there should be only two occurrences:
|
||||
-- one inside the source, other the string itself.
|
||||
assert(count == 2)
|
||||
|
||||
if T then -- check reuse of strings in undump
|
||||
local funcs = load(dump)()
|
||||
assert(string.format("%p", T.listk(funcs[1])[1]) ==
|
||||
string.format("%p", T.listk(funcs[3])[1]))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
do -- test limit of multiple returns (254 values)
|
||||
local code = "return 10" .. string.rep(",10", 253)
|
||||
local res = {assert(load(code))()}
|
||||
assert(#res == 254 and res[254] == 10)
|
||||
|
||||
code = code .. ",10"
|
||||
local status, msg = load(code)
|
||||
assert(not status and string.find(msg, "too many returns"))
|
||||
end
|
||||
|
||||
print('OK')
|
||||
return deep
|
||||
|
||||
@ -1,8 +1,18 @@
|
||||
-- $Id: testes/closure.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
print "testing closures"
|
||||
|
||||
do -- bug in 5.4.7
|
||||
_ENV[true] = 10
|
||||
local function aux () return _ENV[1 < 2] end
|
||||
assert(aux() == 10)
|
||||
_ENV[true] = nil
|
||||
end
|
||||
|
||||
|
||||
local A,B = 0,{g=10}
|
||||
local function f(x)
|
||||
local a = {}
|
||||
@ -60,32 +70,29 @@ end
|
||||
-- testing closures with 'for' control variable
|
||||
a = {}
|
||||
for i=1,10 do
|
||||
a[i] = {set = function(x) i=x end, get = function () return i end}
|
||||
a[i] = function () return i end
|
||||
if i == 3 then break end
|
||||
end
|
||||
assert(a[4] == undef)
|
||||
a[1].set(10)
|
||||
assert(a[2].get() == 2)
|
||||
a[2].set('a')
|
||||
assert(a[3].get() == 3)
|
||||
assert(a[2].get() == 'a')
|
||||
assert(a[2]() == 2)
|
||||
assert(a[3]() == 3)
|
||||
|
||||
a = {}
|
||||
local t = {"a", "b"}
|
||||
for i = 1, #t do
|
||||
local k = t[i]
|
||||
a[i] = {set = function(x, y) i=x; k=y end,
|
||||
a[i] = {set = function(x) k=x end,
|
||||
get = function () return i, k end}
|
||||
if i == 2 then break end
|
||||
end
|
||||
a[1].set(10, 20)
|
||||
a[1].set(10)
|
||||
local r,s = a[2].get()
|
||||
assert(r == 2 and s == 'b')
|
||||
r,s = a[1].get()
|
||||
assert(r == 10 and s == 20)
|
||||
a[2].set('a', 'b')
|
||||
assert(r == 1 and s == 10)
|
||||
a[2].set('a')
|
||||
r,s = a[2].get()
|
||||
assert(r == "a" and s == "b")
|
||||
assert(r == 2 and s == "a")
|
||||
|
||||
|
||||
-- testing closures with 'for' control variable x break
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
-- $Id: testes/code.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
if T==nil then
|
||||
(Message or print)('\n >>> testC not active: skipping opcode tests <<<\n')
|
||||
@ -405,20 +407,29 @@ do -- tests for table access in upvalues
|
||||
end
|
||||
|
||||
-- de morgan
|
||||
checkequal(function () local a; if not (a or b) then b=a end end,
|
||||
function () local a; if (not a and not b) then b=a end end)
|
||||
checkequal(function () local a, b; if not (a or b) then b=a end end,
|
||||
function () local a, b; if (not a and not b) then b=a end end)
|
||||
|
||||
checkequal(function (l) local a; return 0 <= a and a <= l end,
|
||||
function (l) local a; return not (not(a >= 0) or not(a <= l)) end)
|
||||
|
||||
|
||||
-- if-break optimizations
|
||||
check(function (a, b)
|
||||
while a do
|
||||
if b then break else a = a + 1 end
|
||||
end
|
||||
end,
|
||||
'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0')
|
||||
'TEST', 'JMP', 'TEST', 'JMP', 'JMP', 'CLOSE', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0')
|
||||
|
||||
check(function ()
|
||||
do
|
||||
goto exit -- don't need to close
|
||||
local x <close> = nil
|
||||
goto exit -- must close
|
||||
end
|
||||
::exit::
|
||||
end, 'JMP', 'CLOSE', 'LOADNIL', 'TBC',
|
||||
'CLOSE', 'JMP', 'CLOSE', 'RETURN')
|
||||
|
||||
checkequal(function () return 6 or true or nil end,
|
||||
function () return k6 or kTrue or kNil end)
|
||||
@ -445,5 +456,49 @@ do -- string constants
|
||||
assert(T.listk(f2)[1] == nil)
|
||||
end
|
||||
|
||||
|
||||
do -- check number of available registers
|
||||
-- 1 register for local + 1 for function + 252 arguments
|
||||
local source = "local a; return a(" .. string.rep("a, ", 252) .. "a)"
|
||||
local prog = T.listcode(assert(load(source)))
|
||||
-- maximum valid register is 254
|
||||
for i = 1, 254 do
|
||||
assert(string.find(prog[2 + i], "MOVE%s*" .. i))
|
||||
end
|
||||
-- one more argument would need register #255 (but that is reserved)
|
||||
source = "local a; return a(" .. string.rep("a, ", 253) .. "a)"
|
||||
local _, msg = load(source)
|
||||
assert(string.find(msg, "too many registers"))
|
||||
end
|
||||
|
||||
|
||||
do -- basic check for SETLIST
|
||||
-- create a list constructor with 50 elements
|
||||
local source = "local a; return {" .. string.rep("a, ", 50) .. "}"
|
||||
local func = assert(load(source))
|
||||
local code = table.concat(T.listcode(func), "\n")
|
||||
local _, count = string.gsub(code, "SETLIST", "")
|
||||
-- code uses only 1 SETLIST for the constructor
|
||||
assert(count == 1)
|
||||
end
|
||||
|
||||
|
||||
do print("testing code for integer limits")
|
||||
local function checkints (n)
|
||||
local source = string.format(
|
||||
"local a = {[true] = 0X%x}; return a[true]", n)
|
||||
local f = assert(load(source))
|
||||
checkKlist(f, {n})
|
||||
assert(f() == n)
|
||||
f = load(string.dump(f))
|
||||
assert(f() == n)
|
||||
end
|
||||
|
||||
checkints(math.maxinteger)
|
||||
checkints(math.mininteger)
|
||||
checkints(-1)
|
||||
|
||||
end
|
||||
|
||||
print 'OK'
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/constructs.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
;;print "testing syntax";;
|
||||
|
||||
@ -60,7 +60,7 @@ assert((x>y) and x or y == 2);
|
||||
|
||||
assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
|
||||
|
||||
do -- testing operators with diffent kinds of constants
|
||||
do -- testing operators with different kinds of constants
|
||||
-- operands to consider:
|
||||
-- * fit in register
|
||||
-- * constant doesn't fit in register
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/coroutine.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print "testing coroutines"
|
||||
|
||||
@ -127,6 +127,18 @@ assert(#a == 22 and a[#a] == 79)
|
||||
x, a = nil
|
||||
|
||||
|
||||
do -- "bug" in 5.4.2
|
||||
local function foo () foo () end -- just create a stack overflow
|
||||
local co = coroutine.create(foo)
|
||||
-- running this coroutine would overflow the unsigned short 'nci', the
|
||||
-- counter of CallInfo structures available to the thread.
|
||||
-- (The issue only manifests in an 'assert'.)
|
||||
local st, msg = coroutine.resume(co)
|
||||
assert(string.find(msg, "stack overflow"))
|
||||
assert(coroutine.status(co) == "dead")
|
||||
end
|
||||
|
||||
|
||||
print("to-be-closed variables in coroutines")
|
||||
|
||||
local function func2close (f)
|
||||
@ -144,33 +156,32 @@ do
|
||||
st, msg = coroutine.close(co)
|
||||
assert(st and msg == nil)
|
||||
|
||||
|
||||
-- cannot close the running coroutine
|
||||
local st, msg = pcall(coroutine.close, coroutine.running())
|
||||
assert(not st and string.find(msg, "running"))
|
||||
|
||||
local main = coroutine.running()
|
||||
|
||||
-- cannot close 'main'
|
||||
local st, msg = pcall(coroutine.close, main);
|
||||
assert(not st and string.find(msg, "main"))
|
||||
|
||||
|
||||
-- cannot close a "normal" coroutine
|
||||
;(coroutine.wrap(function ()
|
||||
local st, msg = pcall(coroutine.close, main)
|
||||
assert(not st and string.find(msg, "normal"))
|
||||
end))()
|
||||
|
||||
-- cannot close a coroutine while closing it
|
||||
do
|
||||
do -- close a coroutine while closing it
|
||||
local co
|
||||
co = coroutine.create(
|
||||
function()
|
||||
local x <close> = func2close(function()
|
||||
coroutine.close(co) -- try to close it again
|
||||
coroutine.close(co) -- close it again
|
||||
end)
|
||||
coroutine.yield(20)
|
||||
end)
|
||||
local st, msg = coroutine.resume(co)
|
||||
assert(st and msg == 20)
|
||||
st, msg = coroutine.close(co)
|
||||
assert(not st and string.find(msg, "running coroutine"))
|
||||
assert(st and msg == nil)
|
||||
end
|
||||
|
||||
-- to-be-closed variables in coroutines
|
||||
@ -277,6 +288,56 @@ do
|
||||
end
|
||||
|
||||
|
||||
do print("coroutines closing itself")
|
||||
global <const> coroutine, string, os
|
||||
global <const> assert, error, pcall
|
||||
|
||||
local X = nil
|
||||
|
||||
local function new ()
|
||||
return coroutine.create(function (what)
|
||||
|
||||
local <close>var = func2close(function (t, err)
|
||||
if what == "yield" then
|
||||
coroutine.yield()
|
||||
elseif what == "error" then
|
||||
error(200)
|
||||
else
|
||||
X = "Ok"
|
||||
return X
|
||||
end
|
||||
end)
|
||||
|
||||
-- do an unprotected call so that coroutine becomes non-yieldable
|
||||
string.gsub("a", "a", function ()
|
||||
assert(not coroutine.isyieldable())
|
||||
-- do protected calls while non-yieldable, to add recovery
|
||||
-- entries (setjmp) to the stack
|
||||
assert(pcall(pcall, function ()
|
||||
-- 'close' works even while non-yieldable
|
||||
coroutine.close() -- close itself
|
||||
os.exit(false) -- not reacheable
|
||||
end))
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
local co = new()
|
||||
local st, msg = coroutine.resume(co, "ret")
|
||||
assert(st and msg == nil)
|
||||
assert(X == "Ok")
|
||||
|
||||
local co = new()
|
||||
local st, msg = coroutine.resume(co, "error")
|
||||
assert(not st and msg == 200)
|
||||
|
||||
local co = new()
|
||||
local st, msg = coroutine.resume(co, "yield")
|
||||
assert(not st and string.find(msg, "attempt to yield"))
|
||||
|
||||
end
|
||||
|
||||
|
||||
-- yielding across C boundaries
|
||||
|
||||
local co = coroutine.wrap(function()
|
||||
@ -493,6 +554,25 @@ assert(not pcall(a, a))
|
||||
a = nil
|
||||
|
||||
|
||||
do
|
||||
-- bug in 5.4: thread can use message handler higher in the stack
|
||||
-- than the variable being closed
|
||||
local c = coroutine.create(function()
|
||||
local clo <close> = setmetatable({}, {__close=function()
|
||||
local x = 134 -- will overwrite message handler
|
||||
error(x)
|
||||
end})
|
||||
-- yields coroutine but leaves a new message handler for it,
|
||||
-- that would be used when closing the coroutine (except that it
|
||||
-- will be overwritten)
|
||||
xpcall(coroutine.yield, function() return "XXX" end)
|
||||
end)
|
||||
|
||||
assert(coroutine.resume(c)) -- start coroutine
|
||||
local st, msg = coroutine.close(c)
|
||||
assert(not st and msg == 134)
|
||||
end
|
||||
|
||||
-- access to locals of erroneous coroutines
|
||||
local x = coroutine.create (function ()
|
||||
local a = 10
|
||||
@ -515,7 +595,7 @@ else
|
||||
print "testing yields inside hooks"
|
||||
|
||||
local turn
|
||||
|
||||
|
||||
local function fact (t, x)
|
||||
assert(turn == t)
|
||||
if x == 0 then return 1
|
||||
@ -610,18 +690,22 @@ else
|
||||
-- (bug in 5.2/5.3)
|
||||
c = coroutine.create(function (a, ...)
|
||||
T.sethook("yield 0", "l") -- will yield on next two lines
|
||||
assert(a == 10)
|
||||
local b = a
|
||||
return ...
|
||||
end)
|
||||
|
||||
assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine
|
||||
local n,v = debug.getlocal(c, 0, 1) -- check its local
|
||||
assert(n == "a" and v == 1)
|
||||
assert(n == "a" and v == 1 and debug.getlocal(c, 0, 2) ~= "b")
|
||||
assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal'
|
||||
local t = debug.getinfo(c, 0) -- test 'getinfo'
|
||||
assert(t.currentline == t.linedefined + 1)
|
||||
assert(t.currentline == t.linedefined + 2)
|
||||
assert(not debug.getinfo(c, 1)) -- no other level
|
||||
assert(coroutine.resume(c)) -- run next line
|
||||
local n,v = debug.getlocal(c, 0, 2) -- check vararg table
|
||||
assert(n == "(vararg table)" and v == nil)
|
||||
local n,v = debug.getlocal(c, 0, 3) -- check next local
|
||||
assert(n == "b" and v == 10)
|
||||
v = {coroutine.resume(c)} -- finish coroutine
|
||||
assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef)
|
||||
assert(not coroutine.resume(c))
|
||||
@ -640,7 +724,7 @@ else
|
||||
|
||||
|
||||
print "testing coroutine API"
|
||||
|
||||
|
||||
-- reusing a thread
|
||||
assert(T.testC([[
|
||||
newthread # create thread
|
||||
@ -681,7 +765,7 @@ else
|
||||
c == "ERRRUN" and d == 4)
|
||||
|
||||
a, b, c, d = T.testC([[
|
||||
rawgeti R 1 # get main thread
|
||||
rawgeti R !M # get main thread
|
||||
pushnum 10;
|
||||
pushnum 20;
|
||||
resume -3 2;
|
||||
@ -699,11 +783,11 @@ else
|
||||
assert(T.testC(state, "newthread; isyieldable -1; remove 1; return 1"))
|
||||
|
||||
-- main thread is not yieldable
|
||||
assert(not T.testC(state, "rawgeti R 1; isyieldable -1; remove 1; return 1"))
|
||||
assert(not T.testC(state, "rawgeti R !M; isyieldable -1; remove 1; return 1"))
|
||||
|
||||
T.testC(state, "settop 0")
|
||||
|
||||
T.loadlib(state)
|
||||
T.loadlib(state, 1 | 2, 4) -- load _G and 'package', preload 'coroutine'
|
||||
|
||||
assert(T.doremote(state, [[
|
||||
coroutine = require'coroutine';
|
||||
@ -711,7 +795,7 @@ else
|
||||
return 'ok']]))
|
||||
|
||||
local t = table.pack(T.testC(state, [[
|
||||
rawgeti R 1 # get main thread
|
||||
rawgeti R !M # get main thread
|
||||
pushstring 'XX'
|
||||
getglobal X # get function for body
|
||||
pushstring AA # arg
|
||||
@ -720,7 +804,7 @@ else
|
||||
setglobal T # top
|
||||
setglobal B # second yielded value
|
||||
setglobal A # fist yielded value
|
||||
rawgeti R 1 # get main thread
|
||||
rawgeti R !M # get main thread
|
||||
pushnum 5 # arg (noise)
|
||||
resume 1 1 # after coroutine ends, previous stack is back
|
||||
pushstatus
|
||||
@ -918,7 +1002,7 @@ do -- a few more tests for comparison operators
|
||||
until res ~= 10
|
||||
return res
|
||||
end
|
||||
|
||||
|
||||
local function test ()
|
||||
local a1 = setmetatable({x=1}, mt1)
|
||||
local a2 = setmetatable({x=2}, mt2)
|
||||
@ -930,7 +1014,7 @@ do -- a few more tests for comparison operators
|
||||
assert(2 >= a2)
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
run(test)
|
||||
|
||||
end
|
||||
@ -1035,6 +1119,31 @@ f = T.makeCfunc([[
|
||||
return *
|
||||
]], 23, "huu")
|
||||
|
||||
|
||||
do -- testing bug introduced in commit f407b3c4a
|
||||
local X = false -- flag "to be closed"
|
||||
local coro = coroutine.wrap(T.testC)
|
||||
-- runs it until 'pcallk' (that yields)
|
||||
-- 4th argument (at index 4): object to be closed
|
||||
local res1, res2 = coro(
|
||||
[[
|
||||
toclose 3 # this could break the next 'pcallk'
|
||||
pushvalue 2 # push function 'yield' to call it
|
||||
pushint 22; pushint 33 # arguments to yield
|
||||
# calls 'yield' (2 args; 2 results; continuation function at index 4)
|
||||
pcallk 2 2 4
|
||||
invalid command (should not arrive here)
|
||||
]], -- 1st argument (at index 1): code;
|
||||
coroutine.yield, -- (at index 2): function to be called
|
||||
func2close(function () X = true end), -- (index 3): TBC slot
|
||||
"pushint 43; return 3" -- (index 4): code for continuation function
|
||||
)
|
||||
|
||||
assert(res1 == 22 and res2 == 33 and not X)
|
||||
local res1, res2, res3 = coro(34, "hi") -- runs continuation function
|
||||
assert(res1 == 34 and res2 == "hi" and res3 == 43 and X)
|
||||
end
|
||||
|
||||
x = coroutine.wrap(f)
|
||||
assert(x() == 102)
|
||||
eqtab({x()}, {23, "huu"})
|
||||
@ -1092,11 +1201,11 @@ co = coroutine.wrap(function (...) return
|
||||
cannot be here!
|
||||
]],
|
||||
[[ # 1st continuation
|
||||
yieldk 0 3
|
||||
yieldk 0 3
|
||||
cannot be here!
|
||||
]],
|
||||
[[ # 2nd continuation
|
||||
yieldk 0 4
|
||||
yieldk 0 4
|
||||
cannot be here!
|
||||
]],
|
||||
[[ # 3th continuation
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/cstack.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
|
||||
local tracegc = require"tracegc"
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/db.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
-- testing debug library
|
||||
|
||||
@ -49,6 +49,15 @@ do
|
||||
end
|
||||
|
||||
|
||||
-- bug in 5.4.4-5.4.6: activelines in vararg functions
|
||||
-- without debug information
|
||||
do
|
||||
local func = load(string.dump(load("print(10)"), true))
|
||||
local actl = debug.getinfo(func, "L").activelines
|
||||
assert(#actl == 0) -- no line info
|
||||
end
|
||||
|
||||
|
||||
-- test file and string names truncation
|
||||
local a = "function f () end"
|
||||
local function dostring (s, x) return load(s, x)() end
|
||||
@ -119,7 +128,7 @@ then
|
||||
else
|
||||
a=2
|
||||
end
|
||||
]], {2,3,4,7})
|
||||
]], {2,4,7})
|
||||
|
||||
|
||||
test([[
|
||||
@ -340,12 +349,15 @@ end, "crl")
|
||||
|
||||
|
||||
function f(a,b)
|
||||
-- declare some globals to check that they don't interfere with 'getlocal'
|
||||
global collectgarbage
|
||||
collectgarbage()
|
||||
local _, x = debug.getlocal(1, 1)
|
||||
global assert, g, string
|
||||
local _, y = debug.getlocal(1, 2)
|
||||
assert(x == a and y == b)
|
||||
assert(debug.setlocal(2, 3, "pera") == "AA".."AA")
|
||||
assert(debug.setlocal(2, 4, "maçã") == "B")
|
||||
assert(debug.setlocal(2, 4, "pera") == "AA".."AA")
|
||||
assert(debug.setlocal(2, 5, "manga") == "B")
|
||||
x = debug.getinfo(2)
|
||||
assert(x.func == g and x.what == "Lua" and x.name == 'g' and
|
||||
x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
|
||||
@ -373,12 +385,14 @@ function g (...)
|
||||
local arg = {...}
|
||||
do local a,b,c; a=math.sin(40); end
|
||||
local feijao
|
||||
local AAAA,B = "xuxu", "mamão"
|
||||
local AAAA,B = "xuxu", "abacate"
|
||||
f(AAAA,B)
|
||||
assert(AAAA == "pera" and B == "maçã")
|
||||
assert(AAAA == "pera" and B == "manga")
|
||||
do
|
||||
global *
|
||||
local B = 13
|
||||
local x,y = debug.getlocal(1,5)
|
||||
global<const> assert
|
||||
local x,y = debug.getlocal(1,6)
|
||||
assert(x == 'B' and y == 13)
|
||||
end
|
||||
end
|
||||
@ -422,7 +436,7 @@ do
|
||||
assert(a == nil and not b)
|
||||
end
|
||||
|
||||
-- testing iteraction between multiple values x hooks
|
||||
-- testing interaction between multiple values x hooks
|
||||
do
|
||||
local function f(...) return 3, ... end
|
||||
local count = 0
|
||||
@ -444,7 +458,8 @@ local function collectlocals (level)
|
||||
local tab = {}
|
||||
for i = 1, math.huge do
|
||||
local n, v = debug.getlocal(level + 1, i)
|
||||
if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then
|
||||
if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or
|
||||
n == "(vararg table)") then
|
||||
break -- consider only real variables
|
||||
end
|
||||
tab[n] = v
|
||||
@ -578,7 +593,7 @@ t = getupvalues(foo2)
|
||||
assert(t.a == 1 and t.b == 2 and t.c == 3)
|
||||
assert(debug.setupvalue(foo1, 1, "xuxu") == "b")
|
||||
assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu")
|
||||
-- upvalues of C functions are allways "called" "" (the empty string)
|
||||
-- upvalues of C functions are always named "" (the empty string)
|
||||
assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "")
|
||||
|
||||
|
||||
@ -615,6 +630,9 @@ local function f (x)
|
||||
end
|
||||
end
|
||||
|
||||
assert(debug.getinfo(print, 't').istailcall == false)
|
||||
assert(debug.getinfo(print, 't').extraargs == 0)
|
||||
|
||||
function g(x) return f(x) end
|
||||
|
||||
function g1(x) g(x) end
|
||||
@ -689,7 +707,7 @@ assert(debug.traceback(print, 4) == print)
|
||||
assert(string.find(debug.traceback("hi", 4), "^hi\n"))
|
||||
assert(string.find(debug.traceback("hi"), "^hi\n"))
|
||||
assert(not string.find(debug.traceback("hi"), "'debug.traceback'"))
|
||||
assert(string.find(debug.traceback("hi", 0), "'debug.traceback'"))
|
||||
assert(string.find(debug.traceback("hi", 0), "'traceback'"))
|
||||
assert(string.find(debug.traceback(), "^stack traceback:\n"))
|
||||
|
||||
do -- C-function names in traceback
|
||||
@ -708,6 +726,9 @@ assert(t.isvararg == false and t.nparams == 3 and t.nups == 0)
|
||||
t = debug.getinfo(function (a,b,...) return t[a] end, "u")
|
||||
assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
|
||||
|
||||
t = debug.getinfo(function (a,b,...t) t.n = 2; return t[a] end, "u")
|
||||
assert(t.isvararg == true and t.nparams == 2 and t.nups == 0)
|
||||
|
||||
t = debug.getinfo(1) -- main
|
||||
assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
|
||||
debug.getupvalue(t.func, 1) == "_ENV")
|
||||
@ -817,7 +838,7 @@ end
|
||||
|
||||
co = coroutine.create(function (x) f(x) end)
|
||||
a, b = coroutine.resume(co, 3)
|
||||
t = {"'coroutine.yield'", "'f'", "in function <"}
|
||||
t = {"'yield'", "'f'", "in function <"}
|
||||
while coroutine.status(co) == "suspended" do
|
||||
checktraceback(co, t)
|
||||
a, b = coroutine.resume(co)
|
||||
@ -827,7 +848,7 @@ t[1] = "'error'"
|
||||
checktraceback(co, t)
|
||||
|
||||
|
||||
-- test acessing line numbers of a coroutine from a resume inside
|
||||
-- test accessing line numbers of a coroutine from a resume inside
|
||||
-- a C function (this is a known bug in Lua 5.0)
|
||||
|
||||
local function g(x)
|
||||
@ -928,7 +949,7 @@ do
|
||||
local cl = countlines(rest)
|
||||
-- at most 10 lines in first part, 11 in second, plus '...'
|
||||
assert(cl <= 10 + 11 + 1)
|
||||
local brk = string.find(rest, "%.%.%.")
|
||||
local brk = string.find(rest, "%.%.%.\t%(skip")
|
||||
if brk then -- does message have '...'?
|
||||
local rest1 = string.sub(rest, 1, brk)
|
||||
local rest2 = string.sub(rest, brk, #rest)
|
||||
@ -954,9 +975,9 @@ local debug = require'debug'
|
||||
local a = 12 -- a local variable
|
||||
|
||||
local n, v = debug.getlocal(1, 1)
|
||||
assert(n == "(temporary)" and v == debug) -- unkown name but known value
|
||||
assert(n == "(temporary)" and v == debug) -- unknown name but known value
|
||||
n, v = debug.getlocal(1, 2)
|
||||
assert(n == "(temporary)" and v == 12) -- unkown name but known value
|
||||
assert(n == "(temporary)" and v == 12) -- unknown name but known value
|
||||
|
||||
-- a function with an upvalue
|
||||
local f = function () local x; return a end
|
||||
@ -1006,7 +1027,7 @@ do -- bug in 5.4.0: line hooks in stripped code
|
||||
line = l
|
||||
end, "l")
|
||||
assert(s() == 2); debug.sethook(nil)
|
||||
assert(line == nil) -- hook called withoug debug info for 1st instruction
|
||||
assert(line == nil) -- hook called without debug info for 1st instruction
|
||||
end
|
||||
|
||||
do -- tests for 'source' in binary dumps
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/errors.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print("testing errors")
|
||||
|
||||
@ -45,8 +45,8 @@ end
|
||||
-- test error message with no extra info
|
||||
assert(doit("error('hi', 0)") == 'hi')
|
||||
|
||||
-- test error message with no info
|
||||
assert(doit("error()") == nil)
|
||||
-- test nil error message
|
||||
assert(doit("error()") == "<no error object>")
|
||||
|
||||
|
||||
-- test common errors/errors that crashed in the past
|
||||
@ -91,7 +91,7 @@ end
|
||||
|
||||
if not T then
|
||||
(Message or print)
|
||||
('\n >>> testC not active: skipping memory message test <<<\n')
|
||||
('\n >>> testC not active: skipping tests for messages in C <<<\n')
|
||||
else
|
||||
print "testing memory error message"
|
||||
local a = {}
|
||||
@ -104,6 +104,44 @@ else
|
||||
end)
|
||||
T.totalmem(0)
|
||||
assert(not st and msg == "not enough" .. " memory")
|
||||
|
||||
-- stack space for luaL_traceback (bug in 5.4.6)
|
||||
local res = T.testC[[
|
||||
# push 16 elements on the stack
|
||||
pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
|
||||
pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
|
||||
pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
|
||||
pushnum 1;
|
||||
# traceback should work with 4 remaining slots
|
||||
traceback xuxu 1;
|
||||
return 1
|
||||
]]
|
||||
assert(string.find(res, "xuxu.-main chunk"))
|
||||
|
||||
do -- tests for error messages about extra arguments from __call
|
||||
local function createobj (n)
|
||||
-- function that raises an error on its n-th argument
|
||||
local code = string.format("argerror %d 'msg'", n)
|
||||
local func = T.makeCfunc(code)
|
||||
-- create a chain of 2 __call objects
|
||||
local M = setmetatable({}, {__call = func})
|
||||
M = setmetatable({}, {__call = M})
|
||||
-- put it as a method for a new object
|
||||
return {foo = M}
|
||||
end
|
||||
|
||||
_G.a = createobj(1) -- error in first (extra) argument
|
||||
checkmessage("a:foo()", "bad extra argument #1")
|
||||
|
||||
_G.a = createobj(2) -- error in second (extra) argument
|
||||
checkmessage("a:foo()", "bad extra argument #2")
|
||||
|
||||
_G.a = createobj(3) -- error in self (after two extra arguments)
|
||||
checkmessage("a:foo()", "bad self")
|
||||
|
||||
_G.a = createobj(4) -- error in first regular argument (after self)
|
||||
checkmessage("a:foo()", "bad argument #1")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -121,6 +159,12 @@ assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
|
||||
checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
|
||||
checkmessage("aaa=(1)..{}", "a table value")
|
||||
|
||||
-- bug in 5.4.6
|
||||
checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'")
|
||||
|
||||
-- a similar bug, since 5.4.0
|
||||
checkmessage("print(('_ENV').x + 1)", "field 'x'")
|
||||
|
||||
_G.aaa, _G.bbbb = nil
|
||||
|
||||
-- calls
|
||||
@ -259,14 +303,14 @@ do
|
||||
local f = function (a) return a + 1 end
|
||||
f = assert(load(string.dump(f, true)))
|
||||
assert(f(3) == 4)
|
||||
checkerr("^%?:%-1:", f, {})
|
||||
checkerr("^%?:%?:", f, {})
|
||||
|
||||
-- code with a move to a local var ('OP_MOV A B' with A<B)
|
||||
f = function () local a; a = {}; return a + 2 end
|
||||
-- no debug info (so that 'a' is unknown)
|
||||
f = assert(load(string.dump(f, true)))
|
||||
-- symbolic execution should not get lost
|
||||
checkerr("^%?:%-1:.*table value", f)
|
||||
checkerr("^%?:%?:.*table value", f)
|
||||
end
|
||||
|
||||
|
||||
@ -280,7 +324,8 @@ t = nil
|
||||
checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
|
||||
checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
|
||||
checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
|
||||
checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
|
||||
-- cannot use 'self' opcode
|
||||
checkmessage(s.."; local t = {}; t:bbb()", "field 'bbb'")
|
||||
|
||||
checkmessage([[aaa=9
|
||||
repeat until 3==3
|
||||
@ -373,38 +418,38 @@ end
|
||||
|
||||
-- testing line error
|
||||
|
||||
local function lineerror (s, l)
|
||||
local function lineerror (s, l, w)
|
||||
local err,msg = pcall(load(s))
|
||||
local line = tonumber(string.match(msg, ":(%d+):"))
|
||||
assert(line == l or (not line and not l))
|
||||
assert((line == l or (not line and not l)) and string.find(msg, w))
|
||||
end
|
||||
|
||||
lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
|
||||
lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3)
|
||||
lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4)
|
||||
lineerror("function a.x.y ()\na=a+1\nend", 1)
|
||||
lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2, "limit")
|
||||
lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3, "to call")
|
||||
lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4, "to call")
|
||||
lineerror("function a.x.y ()\na=a+1\nend", 1, "index")
|
||||
|
||||
lineerror("a = \na\n+\n{}", 3)
|
||||
lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6)
|
||||
lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3)
|
||||
lineerror("a = \na\n+\n{}", 3, "arithmetic")
|
||||
lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6, "arithmetic")
|
||||
lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3, "arithmetic")
|
||||
|
||||
lineerror("a\n=\n-\n\nprint\n;", 3)
|
||||
lineerror("a\n=\n-\n\nprint\n;", 3, "arithmetic")
|
||||
|
||||
lineerror([[
|
||||
a
|
||||
(
|
||||
( -- <<
|
||||
23)
|
||||
]], 1)
|
||||
]], 2, "call")
|
||||
|
||||
lineerror([[
|
||||
local a = {x = 13}
|
||||
a
|
||||
.
|
||||
x
|
||||
(
|
||||
( -- <<
|
||||
23
|
||||
)
|
||||
]], 2)
|
||||
]], 5, "call")
|
||||
|
||||
lineerror([[
|
||||
local a = {x = 13}
|
||||
@ -414,17 +459,17 @@ x
|
||||
(
|
||||
23 + a
|
||||
)
|
||||
]], 6)
|
||||
]], 6, "arithmetic")
|
||||
|
||||
local p = [[
|
||||
function g() f() end
|
||||
function f(x) error('a', XX) end
|
||||
g()
|
||||
]]
|
||||
XX=3;lineerror((p), 3)
|
||||
XX=0;lineerror((p), false)
|
||||
XX=1;lineerror((p), 2)
|
||||
XX=2;lineerror((p), 1)
|
||||
XX=3;lineerror((p), 3, "a")
|
||||
XX=0;lineerror((p), false, "a")
|
||||
XX=1;lineerror((p), 2, "a")
|
||||
XX=2;lineerror((p), 1, "a")
|
||||
_G.XX, _G.g, _G.f = nil
|
||||
|
||||
|
||||
@ -432,7 +477,7 @@ lineerror([[
|
||||
local b = false
|
||||
if not b then
|
||||
error 'test'
|
||||
end]], 3)
|
||||
end]], 3, "test")
|
||||
|
||||
lineerror([[
|
||||
local b = false
|
||||
@ -442,7 +487,15 @@ if not b then
|
||||
error 'test'
|
||||
end
|
||||
end
|
||||
end]], 5)
|
||||
end]], 5, "test")
|
||||
|
||||
lineerror([[
|
||||
_ENV = 1
|
||||
global function foo ()
|
||||
local a = 10
|
||||
return a
|
||||
end
|
||||
]], 2, "index")
|
||||
|
||||
|
||||
-- bug in 5.4.0
|
||||
@ -450,19 +503,39 @@ lineerror([[
|
||||
local a = 0
|
||||
local b = 1
|
||||
local c = b % a
|
||||
]], 3)
|
||||
]], 3, "perform")
|
||||
|
||||
do
|
||||
-- Force a negative estimate for base line. Error in instruction 2
|
||||
-- (after VARARGPREP, GETGLOBAL), with first absolute line information
|
||||
-- (forced by too many lines) in instruction 0.
|
||||
local s = string.format("%s return __A.x", string.rep("\n", 300))
|
||||
lineerror(s, 301)
|
||||
lineerror(s, 301, "index")
|
||||
end
|
||||
|
||||
|
||||
local function stxlineerror (s, l, w)
|
||||
local err,msg = load(s)
|
||||
local line = tonumber(string.match(msg, ":(%d+):"))
|
||||
assert((line == l or (not line and not l)) and string.find(msg, w, 1, true))
|
||||
end
|
||||
|
||||
stxlineerror([[
|
||||
::L1::
|
||||
::L1::
|
||||
]], 2, "already defined")
|
||||
|
||||
stxlineerror([[
|
||||
global none
|
||||
local x = b
|
||||
]], 2, "not declared")
|
||||
|
||||
stxlineerror([[
|
||||
local <close> a, b
|
||||
]], 1, "multiple")
|
||||
|
||||
if not _soft then
|
||||
-- several tests that exaust the Lua stack
|
||||
-- several tests that exhaust the Lua stack
|
||||
collectgarbage()
|
||||
print"testing stack overflow"
|
||||
local C = 0
|
||||
@ -513,7 +586,7 @@ if not _soft then
|
||||
|
||||
-- error in error handling
|
||||
local res, msg = xpcall(error, error)
|
||||
assert(not res and type(msg) == 'string')
|
||||
assert(not res and msg == 'error in error handling')
|
||||
print('+')
|
||||
|
||||
local function f (x)
|
||||
@ -544,6 +617,27 @@ if not _soft then
|
||||
end
|
||||
|
||||
|
||||
do -- errors in error handle that not necessarily go forever
|
||||
local function err (n) -- function to be used as message handler
|
||||
-- generate an error unless n is zero, so that there is a limited
|
||||
-- loop of errors
|
||||
if type(n) ~= "number" then -- some other error?
|
||||
return n -- report it
|
||||
elseif n == 0 then
|
||||
return "END" -- that will be the final message
|
||||
else error(n - 1) -- does the loop
|
||||
end
|
||||
end
|
||||
|
||||
local res, msg = xpcall(error, err, 170)
|
||||
assert(not res and msg == "END")
|
||||
|
||||
-- too many levels
|
||||
local res, msg = xpcall(error, err, 300)
|
||||
assert(not res and msg == "C stack overflow")
|
||||
end
|
||||
|
||||
|
||||
do
|
||||
-- non string messages
|
||||
local t = {}
|
||||
@ -551,7 +645,7 @@ do
|
||||
assert(not res and msg == t)
|
||||
|
||||
res, msg = pcall(function () error(nil) end)
|
||||
assert(not res and msg == nil)
|
||||
assert(not res and msg == "<no error object>")
|
||||
|
||||
local function f() error{msg='x'} end
|
||||
res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
|
||||
@ -571,7 +665,7 @@ do
|
||||
assert(not res and msg == t)
|
||||
|
||||
res, msg = pcall(assert, nil, nil)
|
||||
assert(not res and msg == nil)
|
||||
assert(not res and type(msg) == "string")
|
||||
|
||||
-- 'assert' without arguments
|
||||
res, msg = pcall(assert)
|
||||
@ -615,21 +709,26 @@ end
|
||||
-- testing syntax limits
|
||||
|
||||
local function testrep (init, rep, close, repc, finalresult)
|
||||
local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100)
|
||||
local res, msg = load(s)
|
||||
assert(res) -- 100 levels is OK
|
||||
local function gencode (n)
|
||||
return init .. string.rep(rep, n) .. close .. string.rep(repc, n)
|
||||
end
|
||||
local res, msg = load(gencode(100)) -- 100 levels is OK
|
||||
assert(res)
|
||||
if (finalresult) then
|
||||
assert(res() == finalresult)
|
||||
end
|
||||
s = init .. string.rep(rep, 500)
|
||||
local res, msg = load(s) -- 500 levels not ok
|
||||
local res, msg = load(gencode(500)) -- 500 levels not ok
|
||||
assert(not res and (string.find(msg, "too many") or
|
||||
string.find(msg, "overflow")))
|
||||
end
|
||||
|
||||
testrep("local a", ",a", ";", "") -- local variables
|
||||
testrep("local a", ",a", "= 1", ",1") -- local variables initialized
|
||||
testrep("local a", ",a", "= f()", "") -- local variables initialized
|
||||
testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
|
||||
testrep("local a; a=", "{", "0", "}")
|
||||
testrep("return ", "(", "2", ")", 2)
|
||||
testrep("local a; a=", "{", "0", "}") -- constructors
|
||||
testrep("return ", "(", "2", ")", 2) -- parentheses
|
||||
-- nested calls (a(a(a(a(...)))))
|
||||
testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2)
|
||||
testrep("", "do ", "", " end")
|
||||
testrep("", "while a do ", "", " end")
|
||||
@ -668,7 +767,7 @@ assert(c > 255 and string.find(b, "too many upvalues") and
|
||||
|
||||
-- local variables
|
||||
s = "\nfunction foo ()\n local "
|
||||
for j = 1,300 do
|
||||
for j = 1,200 do
|
||||
s = s.."a"..j..", "
|
||||
end
|
||||
s = s.."b\n"
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/events.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print('testing metatables')
|
||||
|
||||
@ -248,6 +248,15 @@ end
|
||||
test(Op(1), Op(2), Op(3))
|
||||
|
||||
|
||||
do -- test nil as false
|
||||
local x = setmetatable({12}, {__eq= function (a,b)
|
||||
return a[1] == b[1] or nil
|
||||
end})
|
||||
assert(not (x == {20}))
|
||||
assert(x == {12})
|
||||
end
|
||||
|
||||
|
||||
-- test `partial order'
|
||||
|
||||
local function rawSet(x)
|
||||
@ -370,6 +379,17 @@ x = 0 .."a".."b"..c..d.."e".."f".."g"
|
||||
assert(x.val == "0abcdefg")
|
||||
|
||||
|
||||
do
|
||||
-- bug since 5.4.1 (test needs T)
|
||||
local mt = setmetatable({__newindex={}}, {__mode='v'})
|
||||
local t = setmetatable({}, mt)
|
||||
|
||||
if T then T.allocfailnext() end
|
||||
|
||||
-- seg. fault
|
||||
for i=1, 10 do t[i] = 1 end
|
||||
end
|
||||
|
||||
-- concat metamethod x numbers (bug in 5.1.1)
|
||||
c = {}
|
||||
local x
|
||||
@ -472,7 +492,7 @@ assert(not pcall(function (a,b) return a[b] end, a, 10))
|
||||
assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
|
||||
|
||||
-- bug in 5.1
|
||||
T, K, V = nil
|
||||
local T, K, V = nil
|
||||
grandparent = {}
|
||||
grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
-- $Id: testes/files.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
local debug = require "debug"
|
||||
|
||||
@ -74,6 +76,8 @@ io.input(io.stdin); io.output(io.stdout);
|
||||
|
||||
os.remove(file)
|
||||
assert(not loadfile(file))
|
||||
-- Lua code cannot use chunks with fixed buffers
|
||||
checkerr("invalid mode", load, "", "", "B")
|
||||
checkerr("", dofile, file)
|
||||
assert(not io.open(file))
|
||||
io.output(file)
|
||||
@ -92,8 +96,8 @@ assert(io.output():seek("end") == string.len("alo joao"))
|
||||
|
||||
assert(io.output():seek("set") == 0)
|
||||
|
||||
assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
|
||||
assert(io.write('çfourth_line'))
|
||||
assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n"))
|
||||
assert(io.write('Xfourth_line'))
|
||||
io.output(io.stdout)
|
||||
collectgarbage() -- file should be closed by GC
|
||||
assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
|
||||
@ -300,14 +304,14 @@ do -- test error returns
|
||||
end
|
||||
checkerr("invalid format", io.read, "x")
|
||||
assert(io.read(0) == "") -- not eof
|
||||
assert(io.read(5, 'l') == '"álo"')
|
||||
assert(io.read(5, 'l') == '"alo"')
|
||||
assert(io.read(0) == "")
|
||||
assert(io.read() == "second line")
|
||||
local x = io.input():seek()
|
||||
assert(io.read() == "third line ")
|
||||
assert(io.input():seek("set", x))
|
||||
assert(io.read('L') == "third line \n")
|
||||
assert(io.read(1) == "ç")
|
||||
assert(io.read(1) == "X")
|
||||
assert(io.read(string.len"fourth_line") == "fourth_line")
|
||||
assert(io.input():seek("cur", -string.len"fourth_line"))
|
||||
assert(io.read() == "fourth_line")
|
||||
@ -345,7 +349,7 @@ collectgarbage()
|
||||
|
||||
assert(io.write(' ' .. t .. ' '))
|
||||
assert(io.write(';', 'end of file\n'))
|
||||
f:flush(); io.flush()
|
||||
assert(f:flush()); assert(io.flush())
|
||||
f:close()
|
||||
print('+')
|
||||
|
||||
@ -427,12 +431,12 @@ do -- testing closing file in line iteration
|
||||
-- get the to-be-closed variable from a loop
|
||||
local function gettoclose (lv)
|
||||
lv = lv + 1
|
||||
local stvar = 0 -- to-be-closed is 4th state variable in the loop
|
||||
local stvar = 0 -- to-be-closed is 3th state variable in the loop
|
||||
for i = 1, 1000 do
|
||||
local n, v = debug.getlocal(lv, i)
|
||||
if n == "(for state)" then
|
||||
stvar = stvar + 1
|
||||
if stvar == 4 then return v end
|
||||
if stvar == 3 then return v end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -459,7 +463,24 @@ do -- testing closing file in line iteration
|
||||
end
|
||||
|
||||
|
||||
-- test for multipe arguments in 'lines'
|
||||
do print("testing flush")
|
||||
local f = io.output("/dev/null")
|
||||
assert(f:write("abcd")) -- write to buffer
|
||||
assert(f:flush()) -- write to device
|
||||
assert(f:write("abcd")) -- write to buffer
|
||||
assert(io.flush()) -- write to device
|
||||
assert(f:close())
|
||||
|
||||
local f = io.output("/dev/full")
|
||||
assert(f:write("abcd")) -- write to buffer
|
||||
assert(not f:flush()) -- cannot write to device
|
||||
assert(f:write("abcd")) -- write to buffer
|
||||
assert(not io.flush()) -- cannot write to device
|
||||
assert(f:close())
|
||||
end
|
||||
|
||||
|
||||
-- test for multiple arguments in 'lines'
|
||||
io.output(file); io.write"0123456789\n":close()
|
||||
for a,b in io.lines(file, 1, 1) do
|
||||
if a == "\n" then assert(not b)
|
||||
@ -694,6 +715,37 @@ do
|
||||
end
|
||||
|
||||
|
||||
if T and T.nonblock and not _port then
|
||||
print("testing failed write")
|
||||
|
||||
-- unable to write anything to /dev/full
|
||||
local f = io.open("/dev/full", "w")
|
||||
assert(f:setvbuf("no"))
|
||||
local _, _, err, count = f:write("abcd")
|
||||
assert(err > 0 and count == 0)
|
||||
assert(f:close())
|
||||
|
||||
-- receiver will read a "few" bytes (enough to empty a large buffer)
|
||||
local receiver = [[
|
||||
lua -e 'assert(io.stdin:setvbuf("no")); assert(#io.read(1e4) == 1e4)' ]]
|
||||
|
||||
local f = io.popen(receiver, "w")
|
||||
assert(f:setvbuf("no"))
|
||||
T.nonblock(f)
|
||||
|
||||
-- able to write a few bytes
|
||||
assert(f:write(string.rep("a", 1e2)))
|
||||
|
||||
-- Unable to write more bytes than the pipe buffer supports.
|
||||
-- (In Linux, the pipe buffer size is 64K (2^16). Posix requires at
|
||||
-- least 512 bytes.)
|
||||
local _, _, err, count = f:write("abcd", string.rep("a", 2^17))
|
||||
assert(err > 0 and count >= 512 and count < 2^17)
|
||||
|
||||
assert(f:close())
|
||||
end
|
||||
|
||||
|
||||
if not _soft then
|
||||
print("testing large files (> BUFSIZ)")
|
||||
io.output(file)
|
||||
@ -764,6 +816,7 @@ if not _port then
|
||||
assert((v[3] == nil and z > 0) or v[3] == z)
|
||||
end
|
||||
end
|
||||
print("(done)")
|
||||
end
|
||||
|
||||
|
||||
@ -787,13 +840,13 @@ assert(os.date("!\0\0") == "\0\0")
|
||||
local x = string.rep("a", 10000)
|
||||
assert(os.date(x) == x)
|
||||
local t = os.time()
|
||||
D = os.date("*t", t)
|
||||
global D = os.date("*t", t)
|
||||
assert(os.date(string.rep("%d", 1000), t) ==
|
||||
string.rep(os.date("%d", t), 1000))
|
||||
assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
|
||||
|
||||
local function checkDateTable (t)
|
||||
_G.D = os.date("*t", t)
|
||||
D = os.date("*t", t)
|
||||
assert(os.time(D) == t)
|
||||
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
|
||||
D.hour==%H and D.min==%M and D.sec==%S and
|
||||
|
||||
183
testes/gc.lua
183
testes/gc.lua
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/gc.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print('testing incremental garbage collection')
|
||||
|
||||
@ -27,27 +27,54 @@ end
|
||||
|
||||
-- test weird parameters to 'collectgarbage'
|
||||
do
|
||||
-- save original parameters
|
||||
local a = collectgarbage("setpause", 200)
|
||||
local b = collectgarbage("setstepmul", 200)
|
||||
collectgarbage("incremental")
|
||||
local opause = collectgarbage("param", "pause", 100)
|
||||
local ostepmul = collectgarbage("param", "stepmul", 100)
|
||||
assert(collectgarbage("param", "pause") == 100)
|
||||
assert(collectgarbage("param", "stepmul") == 100)
|
||||
local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe}
|
||||
for i = 1, #t do
|
||||
local p = t[i]
|
||||
collectgarbage("param", "pause", t[i])
|
||||
for j = 1, #t do
|
||||
local m = t[j]
|
||||
collectgarbage("setpause", p)
|
||||
collectgarbage("setstepmul", m)
|
||||
collectgarbage("step", 0)
|
||||
collectgarbage("step", 10000)
|
||||
collectgarbage("param", "stepmul", t[j])
|
||||
collectgarbage("step", t[j])
|
||||
end
|
||||
end
|
||||
-- restore original parameters
|
||||
collectgarbage("setpause", a)
|
||||
collectgarbage("setstepmul", b)
|
||||
collectgarbage("param", "pause", opause)
|
||||
collectgarbage("param", "stepmul", ostepmul)
|
||||
collectgarbage()
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- test the "size" of basic GC steps (whatever they mean...)
|
||||
--
|
||||
do print("steps")
|
||||
|
||||
local function dosteps (siz)
|
||||
collectgarbage()
|
||||
local a = {}
|
||||
for i=1,100 do a[i] = {{}}; local b = {} end
|
||||
local x = gcinfo()
|
||||
local i = 0
|
||||
repeat -- do steps until it completes a collection cycle
|
||||
i = i+1
|
||||
until collectgarbage("step", siz)
|
||||
assert(gcinfo() < x)
|
||||
return i -- number of steps
|
||||
end
|
||||
|
||||
|
||||
if not _port then
|
||||
collectgarbage"stop"
|
||||
assert(dosteps(10) < dosteps(2))
|
||||
collectgarbage"restart"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
_G["while"] = 234
|
||||
|
||||
|
||||
@ -174,45 +201,6 @@ do
|
||||
end
|
||||
|
||||
|
||||
--
|
||||
-- test the "size" of basic GC steps (whatever they mean...)
|
||||
--
|
||||
do
|
||||
print("steps")
|
||||
|
||||
print("steps (2)")
|
||||
|
||||
local function dosteps (siz)
|
||||
collectgarbage()
|
||||
local a = {}
|
||||
for i=1,100 do a[i] = {{}}; local b = {} end
|
||||
local x = gcinfo()
|
||||
local i = 0
|
||||
repeat -- do steps until it completes a collection cycle
|
||||
i = i+1
|
||||
until collectgarbage("step", siz)
|
||||
assert(gcinfo() < x)
|
||||
return i -- number of steps
|
||||
end
|
||||
|
||||
collectgarbage"stop"
|
||||
|
||||
if not _port then
|
||||
assert(dosteps(10) < dosteps(2))
|
||||
end
|
||||
|
||||
-- collector should do a full collection with so many steps
|
||||
assert(dosteps(20000) == 1)
|
||||
assert(collectgarbage("step", 20000) == true)
|
||||
assert(collectgarbage("step", 20000) == true)
|
||||
|
||||
assert(not collectgarbage("isrunning"))
|
||||
collectgarbage"restart"
|
||||
assert(collectgarbage("isrunning"))
|
||||
|
||||
end
|
||||
|
||||
|
||||
if not _port then
|
||||
-- test the pace of the collector
|
||||
collectgarbage(); collectgarbage()
|
||||
@ -300,6 +288,21 @@ x,y,z=nil
|
||||
collectgarbage()
|
||||
assert(next(a) == string.rep('$', 11))
|
||||
|
||||
do -- invalid mode
|
||||
local a = setmetatable({}, {__mode = 34})
|
||||
collectgarbage()
|
||||
end
|
||||
|
||||
|
||||
if T then -- bug since 5.3: all-weak tables are not being revisited
|
||||
T.gcstate("propagate")
|
||||
local t = setmetatable({}, {__mode = "kv"})
|
||||
T.gcstate("enteratomic") -- 't' was visited
|
||||
setmetatable(t, {__mode = "kv"})
|
||||
T.gcstate("pause") -- its new metatable is not being visited
|
||||
assert(getmetatable(t).__mode == "kv")
|
||||
end
|
||||
|
||||
|
||||
-- 'bug' in 5.1
|
||||
a = {}
|
||||
@ -458,12 +461,9 @@ do -- tests for string keys in weak tables
|
||||
local m = collectgarbage("count") -- current memory
|
||||
local a = setmetatable({}, {__mode = "kv"})
|
||||
a[string.rep("a", 2^22)] = 25 -- long string key -> number value
|
||||
a[string.rep("b", 2^22)] = {} -- long string key -> colectable value
|
||||
a[{}] = 14 -- colectable key
|
||||
assert(collectgarbage("count") > m + 2^13) -- 2^13 == 2 * 2^22 in KB
|
||||
a[string.rep("b", 2^22)] = {} -- long string key -> collectable value
|
||||
a[{}] = 14 -- collectable key
|
||||
collectgarbage()
|
||||
assert(collectgarbage("count") >= m + 2^12 and
|
||||
collectgarbage("count") < m + 2^13) -- one key was collected
|
||||
local k, v = next(a) -- string key with number value preserved
|
||||
assert(k == string.rep("a", 2^22) and v == 25)
|
||||
assert(next(a, k) == nil) -- everything else cleared
|
||||
@ -474,7 +474,7 @@ do -- tests for string keys in weak tables
|
||||
assert(next(a) == nil)
|
||||
-- make sure will not try to compare with dead key
|
||||
assert(a[string.rep("b", 100)] == undef)
|
||||
assert(collectgarbage("count") <= m + 1) -- eveything collected
|
||||
assert(collectgarbage("count") <= m + 1) -- everything collected
|
||||
end
|
||||
|
||||
|
||||
@ -539,7 +539,7 @@ do
|
||||
local co = coroutine.create(f)
|
||||
assert(coroutine.resume(co, co))
|
||||
end
|
||||
-- Now, thread and closure are not reacheable any more.
|
||||
-- Now, thread and closure are not reachable any more.
|
||||
collectgarbage()
|
||||
assert(collected)
|
||||
collectgarbage("restart")
|
||||
@ -549,7 +549,7 @@ end
|
||||
do
|
||||
collectgarbage()
|
||||
collectgarbage"stop"
|
||||
collectgarbage("step", 0) -- steps should not unblock the collector
|
||||
collectgarbage("step") -- steps should not unblock the collector
|
||||
local x = gcinfo()
|
||||
repeat
|
||||
for i=1,1000 do _ENV.a = {} end -- no collection during the loop
|
||||
@ -575,8 +575,8 @@ if T then -- tests for weird cases collecting upvalues
|
||||
-- create coroutine in a weak table, so it will never be marked
|
||||
t.co = coroutine.wrap(foo)
|
||||
local f = t.co() -- create function to access local 'a'
|
||||
T.gcstate("atomic") -- ensure all objects are traversed
|
||||
assert(T.gcstate() == "atomic")
|
||||
T.gcstate("enteratomic") -- ensure all objects are traversed
|
||||
assert(T.gcstate() == "enteratomic")
|
||||
assert(t.co() == 100) -- resume coroutine, creating new table for 'a'
|
||||
assert(T.gccolor(t.co) == "white") -- thread was not traversed
|
||||
T.gcstate("pause") -- collect thread, but should mark 'a' before that
|
||||
@ -589,7 +589,7 @@ if T then -- tests for weird cases collecting upvalues
|
||||
collectgarbage()
|
||||
collectgarbage"stop"
|
||||
local a = {} -- avoid 'u' as first element in 'allgc'
|
||||
T.gcstate"atomic"
|
||||
T.gcstate"enteratomic"
|
||||
T.gcstate"sweepallgc"
|
||||
local x = {}
|
||||
assert(T.gccolor(u) == "black") -- userdata is "old" (black)
|
||||
@ -615,6 +615,21 @@ if T then
|
||||
end
|
||||
|
||||
|
||||
if T then
|
||||
collectgarbage("stop")
|
||||
T.gcstate("pause")
|
||||
local sup = {x = 0}
|
||||
local a = setmetatable({}, {__newindex = sup})
|
||||
T.gcstate("enteratomic")
|
||||
assert(T.gccolor(sup) == "black")
|
||||
a.x = {} -- should not break the invariant
|
||||
assert(not (T.gccolor(sup) == "black" and T.gccolor(sup.x) == "white"))
|
||||
T.gcstate("pause") -- complete the GC cycle
|
||||
sup.x.y = 10
|
||||
collectgarbage("restart")
|
||||
end
|
||||
|
||||
|
||||
if T then
|
||||
print("emergency collections")
|
||||
collectgarbage()
|
||||
@ -644,7 +659,7 @@ do
|
||||
assert(getmetatable(o) == tt)
|
||||
-- create new objects during GC
|
||||
local a = 'xuxu'..(10+3)..'joao', {}
|
||||
___Glob = o -- ressurrect object!
|
||||
___Glob = o -- resurrect object!
|
||||
setmetatable({}, tt) -- creates a new one with same metatable
|
||||
print(">>> closing state " .. "<<<\n")
|
||||
end
|
||||
@ -692,4 +707,46 @@ end
|
||||
|
||||
collectgarbage(oldmode)
|
||||
|
||||
|
||||
if T then
|
||||
print("testing stack issues when calling finalizers")
|
||||
|
||||
local X
|
||||
local obj
|
||||
|
||||
local function initobj ()
|
||||
X = false
|
||||
obj = setmetatable({}, {__gc = function () X = true end})
|
||||
end
|
||||
|
||||
local function loop (n)
|
||||
if n > 0 then loop(n - 1) end
|
||||
end
|
||||
|
||||
-- should not try to call finalizer without a CallInfo available
|
||||
initobj()
|
||||
loop(20) -- ensure stack space
|
||||
T.resetCI() -- remove extra CallInfos
|
||||
T.alloccount(0) -- cannot allocate more CallInfos
|
||||
obj = nil
|
||||
collectgarbage() -- will not call finalizer
|
||||
T.alloccount()
|
||||
assert(X == false)
|
||||
collectgarbage() -- now will call finalizer (it was still pending)
|
||||
assert(X == true)
|
||||
|
||||
-- should not try to call finalizer without stack space available
|
||||
initobj()
|
||||
loop(5) -- ensure enough CallInfos
|
||||
T.reallocstack(0) -- remove extra stack slots
|
||||
T.alloccount(0) -- cannot reallocate stack
|
||||
obj = nil
|
||||
collectgarbage() -- will not call finalizer
|
||||
T.alloccount()
|
||||
assert(X == false)
|
||||
collectgarbage() -- now will call finalizer (it was still pending)
|
||||
assert(X == true)
|
||||
end
|
||||
|
||||
|
||||
print('OK')
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/gengc.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print('testing generational garbage collection')
|
||||
|
||||
@ -24,12 +24,12 @@ do
|
||||
assert(not T or (T.gcage(U) == "touched1" and T.gcage(U[1]) == "new"))
|
||||
|
||||
-- both U and the table survive one more collection
|
||||
collectgarbage("step", 0)
|
||||
collectgarbage("step")
|
||||
assert(not T or (T.gcage(U) == "touched2" and T.gcage(U[1]) == "survival"))
|
||||
|
||||
-- both U and the table survive yet another collection
|
||||
-- now everything is old
|
||||
collectgarbage("step", 0)
|
||||
collectgarbage("step")
|
||||
assert(not T or (T.gcage(U) == "old" and T.gcage(U[1]) == "old1"))
|
||||
|
||||
-- data was not corrupted
|
||||
@ -46,10 +46,10 @@ do
|
||||
assert(not T or T.gcage(old) == "old")
|
||||
setmetatable(old, {}) -- new table becomes OLD0 (barrier)
|
||||
assert(not T or T.gcage(getmetatable(old)) == "old0")
|
||||
collectgarbage("step", 0) -- new table becomes OLD1 and firstold1
|
||||
collectgarbage("step") -- new table becomes OLD1 and firstold1
|
||||
assert(not T or T.gcage(getmetatable(old)) == "old1")
|
||||
setmetatable(getmetatable(old), {__gc = foo}) -- get it out of allgc list
|
||||
collectgarbage("step", 0) -- should not seg. fault
|
||||
collectgarbage("step") -- should not seg. fault
|
||||
end
|
||||
|
||||
|
||||
@ -65,18 +65,18 @@ do -- bug in 5.4.0
|
||||
A[1] = obj -- anchor object
|
||||
assert(not T or T.gcage(obj) == "old1")
|
||||
obj = nil -- remove it from the stack
|
||||
collectgarbage("step", 0) -- do a young collection
|
||||
collectgarbage("step") -- do a young collection
|
||||
print(getmetatable(A[1]).x) -- metatable was collected
|
||||
end
|
||||
|
||||
collectgarbage() -- make A old
|
||||
local obj = {} -- create a new object
|
||||
collectgarbage("step", 0) -- make it a survival
|
||||
collectgarbage("step") -- make it a survival
|
||||
assert(not T or T.gcage(obj) == "survival")
|
||||
setmetatable(obj, {__gc = gcf, x = "+"}) -- create its metatable
|
||||
assert(not T or T.gcage(getmetatable(obj)) == "new")
|
||||
obj = nil -- clear object
|
||||
collectgarbage("step", 0) -- will call obj's finalizer
|
||||
collectgarbage("step") -- will call obj's finalizer
|
||||
end
|
||||
|
||||
|
||||
@ -94,13 +94,13 @@ do -- another bug in 5.4.0
|
||||
end
|
||||
)
|
||||
local _, f = coroutine.resume(co) -- create closure over 'x' in coroutine
|
||||
collectgarbage("step", 0) -- make upvalue a survival
|
||||
collectgarbage("step") -- make upvalue a survival
|
||||
old[1] = {"hello"} -- 'old' go to grayagain as 'touched1'
|
||||
coroutine.resume(co, {123}) -- its value will be new
|
||||
co = nil
|
||||
collectgarbage("step", 0) -- hit the barrier
|
||||
collectgarbage("step") -- hit the barrier
|
||||
assert(f() == 123 and old[1][1] == "hello")
|
||||
collectgarbage("step", 0) -- run the collector once more
|
||||
collectgarbage("step") -- run the collector once more
|
||||
-- make sure old[1] was not collected
|
||||
assert(f() == 123 and old[1][1] == "hello")
|
||||
end
|
||||
@ -112,12 +112,12 @@ do -- bug introduced in commit 9cf3299fa
|
||||
assert(not T or T.gcage(t) == "old")
|
||||
t[1] = {10}
|
||||
assert(not T or (T.gcage(t) == "touched1" and T.gccolor(t) == "gray"))
|
||||
collectgarbage("step", 0) -- minor collection
|
||||
collectgarbage("step") -- minor collection
|
||||
assert(not T or (T.gcage(t) == "touched2" and T.gccolor(t) == "black"))
|
||||
collectgarbage("step", 0) -- minor collection
|
||||
collectgarbage("step") -- minor collection
|
||||
assert(not T or T.gcage(t) == "old") -- t should be black, but it was gray
|
||||
t[1] = {10} -- no barrier here, so t was still old
|
||||
collectgarbage("step", 0) -- minor collection
|
||||
collectgarbage("step") -- minor collection
|
||||
-- t, being old, is ignored by the collection, so it is not cleared
|
||||
assert(t[1] == nil) -- fails with the bug
|
||||
end
|
||||
@ -144,13 +144,13 @@ do
|
||||
T.gcage(debug.getuservalue(U)) == "new")
|
||||
|
||||
-- both U and the table survive one more collection
|
||||
collectgarbage("step", 0)
|
||||
collectgarbage("step")
|
||||
assert(T.gcage(U) == "touched2" and
|
||||
T.gcage(debug.getuservalue(U)) == "survival")
|
||||
|
||||
-- both U and the table survive yet another collection
|
||||
-- now everything is old
|
||||
collectgarbage("step", 0)
|
||||
collectgarbage("step")
|
||||
assert(T.gcage(U) == "old" and
|
||||
T.gcage(debug.getuservalue(U)) == "old1")
|
||||
|
||||
@ -162,9 +162,33 @@ end
|
||||
assert(collectgarbage'isrunning')
|
||||
|
||||
|
||||
do print"testing stop-the-world collection"
|
||||
local step = collectgarbage("param", "stepsize", 0);
|
||||
collectgarbage("incremental")
|
||||
assert(collectgarbage("param", "stepsize") == 0)
|
||||
|
||||
-- just to make sure
|
||||
assert(collectgarbage'isrunning')
|
||||
-- each step does a complete cycle
|
||||
assert(collectgarbage("step"))
|
||||
assert(collectgarbage("step"))
|
||||
|
||||
-- back to default value
|
||||
collectgarbage("param", "stepsize", step);
|
||||
assert(collectgarbage("param", "stepsize") == step)
|
||||
end
|
||||
|
||||
|
||||
if T then -- test GC parameter codification
|
||||
for _, percentage in ipairs{5, 10, 12, 20, 50, 100, 200, 500} do
|
||||
local param = T.codeparam(percentage) -- codify percentage
|
||||
for _, value in ipairs{1, 2, 10, 100, 257, 1023, 6500, 100000} do
|
||||
local exact = value*percentage // 100
|
||||
local aprox = T.applyparam(param, value) -- apply percentage
|
||||
-- difference is at most 10% (+1 compensates difference due to
|
||||
-- rounding to integers)
|
||||
assert(math.abs(aprox - exact) <= exact/10 + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
collectgarbage(oldmode)
|
||||
|
||||
|
||||
242
testes/goto.lua
242
testes/goto.lua
@ -1,5 +1,11 @@
|
||||
-- $Id: testes/goto.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global<const> require
|
||||
global<const> print, load, assert, string, setmetatable
|
||||
global<const> collectgarbage, error
|
||||
|
||||
print("testing goto and global declarations")
|
||||
|
||||
collectgarbage()
|
||||
|
||||
@ -17,15 +23,18 @@ errmsg([[ ::l1:: ::l1:: ]], "label 'l1'")
|
||||
errmsg([[ ::l1:: do ::l1:: end]], "label 'l1'")
|
||||
|
||||
|
||||
-- undefined label
|
||||
errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'")
|
||||
|
||||
-- jumping over variable definition
|
||||
-- jumping over variable declaration
|
||||
errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "scope of 'aa'")
|
||||
|
||||
errmsg([[ goto l2; global *; ::l1:: ::l2:: print(3) ]], "scope of '*'")
|
||||
|
||||
errmsg([[
|
||||
do local bb, cc; goto l1; end
|
||||
local aa
|
||||
::l1:: print(3)
|
||||
]], "local 'aa'")
|
||||
]], "scope of 'aa'")
|
||||
|
||||
|
||||
-- jumping into a block
|
||||
errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'")
|
||||
@ -38,7 +47,7 @@ errmsg([[
|
||||
local xuxu = 10
|
||||
::cont::
|
||||
until xuxu < x
|
||||
]], "local 'xuxu'")
|
||||
]], "scope of 'xuxu'")
|
||||
|
||||
-- simple gotos
|
||||
local x
|
||||
@ -250,22 +259,219 @@ assert(testG(3) == "3")
|
||||
assert(testG(4) == 5)
|
||||
assert(testG(5) == 10)
|
||||
|
||||
do
|
||||
-- if x back goto out of scope of upvalue
|
||||
local X
|
||||
goto L1
|
||||
do -- test goto's around to-be-closed variable
|
||||
|
||||
::L2:: goto L3
|
||||
global *
|
||||
|
||||
::L1:: do
|
||||
local a <close> = setmetatable({}, {__close = function () X = true end})
|
||||
assert(X == nil)
|
||||
if a then goto L2 end -- jumping back out of scope of 'a'
|
||||
-- set 'var' and return an object that will reset 'var' when
|
||||
-- it goes out of scope
|
||||
local function newobj (var)
|
||||
_ENV[var] = true
|
||||
return setmetatable({}, {__close = function ()
|
||||
_ENV[var] = nil
|
||||
end})
|
||||
end
|
||||
|
||||
::L3:: assert(X == true) -- checks that 'a' was correctly closed
|
||||
end
|
||||
--------------------------------------------------------------------------------
|
||||
goto L1
|
||||
|
||||
::L4:: assert(not varX); goto L5 -- varX dead here
|
||||
|
||||
::L1::
|
||||
local varX <close> = newobj("X")
|
||||
assert(varX); goto L2 -- varX alive here
|
||||
|
||||
::L3::
|
||||
assert(varX); goto L4 -- varX alive here
|
||||
|
||||
::L2:: assert(varX); goto L3 -- varX alive here
|
||||
|
||||
::L5:: -- return
|
||||
end
|
||||
|
||||
|
||||
|
||||
foo()
|
||||
--------------------------------------------------------------------------
|
||||
|
||||
-- check for compilation errors
|
||||
local function checkerr (code, err)
|
||||
local st, msg = load(code)
|
||||
assert(not st and string.find(msg, err))
|
||||
end
|
||||
|
||||
do
|
||||
global T<const>
|
||||
|
||||
-- globals must be declared, after a global declaration
|
||||
checkerr("global none; X = 1", "variable 'X'")
|
||||
checkerr("global none; function XX() end", "variable 'XX'")
|
||||
|
||||
-- global variables cannot be to-be-closed
|
||||
checkerr("global X<close>", "cannot be")
|
||||
checkerr("global <close> *", "cannot be")
|
||||
|
||||
do
|
||||
local X = 10
|
||||
do global X; X = 20 end
|
||||
assert(X == 10) -- local X
|
||||
end
|
||||
assert(_ENV.X == 20) -- global X
|
||||
|
||||
-- '_ENV' cannot be global
|
||||
checkerr("global _ENV, a; a = 10", "variable 'a'")
|
||||
|
||||
-- global declarations inside functions
|
||||
checkerr([[
|
||||
global none
|
||||
local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'")
|
||||
|
||||
if not T then -- when not in "test mode", "global" isn't reserved
|
||||
assert(load("global = 1; return global")() == 1)
|
||||
print " ('global' is not a reserved word)"
|
||||
else
|
||||
-- "global" reserved, cannot be used as a variable
|
||||
assert(not load("global = 1; return global"))
|
||||
end
|
||||
|
||||
local foo = 20
|
||||
do
|
||||
global function foo (x)
|
||||
if x == 0 then return 1 else return 2 * foo(x - 1) end
|
||||
end
|
||||
assert(foo == _ENV.foo and foo(4) == 16)
|
||||
end
|
||||
assert(_ENV.foo(4) == 16)
|
||||
assert(foo == 20) -- local one is in context here
|
||||
|
||||
do
|
||||
global foo;
|
||||
function foo (x) return end -- Ok after declaration
|
||||
end
|
||||
|
||||
checkerr([[
|
||||
global<const> foo;
|
||||
function foo (x) return end -- ERROR: foo is read-only
|
||||
]], "assign to const variable 'foo'")
|
||||
|
||||
checkerr([[
|
||||
global foo <const>;
|
||||
function foo (x) -- ERROR: foo is read-only
|
||||
return
|
||||
end
|
||||
]], "%:2%:") -- correct line in error message
|
||||
|
||||
checkerr([[
|
||||
global<const> *;
|
||||
print(X) -- Ok to use
|
||||
Y = 1 -- ERROR
|
||||
]], "assign to const variable 'Y'")
|
||||
|
||||
checkerr([[
|
||||
global *;
|
||||
Y = X -- Ok to use
|
||||
global<const> *;
|
||||
Y = 1 -- ERROR
|
||||
]], "assign to const variable 'Y'")
|
||||
|
||||
global *
|
||||
Y = 10
|
||||
assert(_ENV.Y == 10)
|
||||
global<const> *
|
||||
local x = Y
|
||||
global *
|
||||
Y = x + Y
|
||||
assert(_ENV.Y == 20)
|
||||
Y = nil
|
||||
end
|
||||
|
||||
|
||||
do -- Ok to declare hundreds of globals
|
||||
global table
|
||||
local code = {}
|
||||
for i = 1, 1000 do
|
||||
code[#code + 1] = ";global x" .. i
|
||||
end
|
||||
code[#code + 1] = "; return x990"
|
||||
code = table.concat(code)
|
||||
_ENV.x990 = 11
|
||||
assert(load(code)() == 11)
|
||||
_ENV.x990 = nil
|
||||
end
|
||||
|
||||
do -- mixing lots of global/local declarations
|
||||
global table
|
||||
local code = {}
|
||||
for i = 1, 200 do
|
||||
code[#code + 1] = ";global x" .. i
|
||||
code[#code + 1] = ";local y" .. i .. "=" .. (2*i)
|
||||
end
|
||||
code[#code + 1] = "; return x200 + y200"
|
||||
code = table.concat(code)
|
||||
_ENV.x200 = 11
|
||||
assert(assert(load(code))() == 2*200 + 11)
|
||||
_ENV.x200 = nil
|
||||
end
|
||||
|
||||
do print "testing initialization in global declarations"
|
||||
global<const> a, b, c = 10, 20, 30
|
||||
assert(_ENV.a == 10 and b == 20 and c == 30)
|
||||
_ENV.a = nil; _ENV.b = nil; _ENV.c = nil;
|
||||
|
||||
global<const> a, b, c = 10
|
||||
assert(_ENV.a == 10 and b == nil and c == nil)
|
||||
_ENV.a = nil; _ENV.b = nil; _ENV.c = nil;
|
||||
|
||||
global table
|
||||
global a, b, c, d = table.unpack{1, 2, 3, 6, 5}
|
||||
assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6)
|
||||
a = nil; b = nil; c = nil; d = nil
|
||||
|
||||
local a, b = 100, 200
|
||||
do
|
||||
global a, b = a, b
|
||||
end
|
||||
assert(_ENV.a == 100 and _ENV.b == 200)
|
||||
_ENV.a = nil; _ENV.b = nil
|
||||
|
||||
|
||||
assert(_ENV.a == nil and _ENV.b == nil and _ENV.c == nil and _ENV.d == nil)
|
||||
end
|
||||
|
||||
do
|
||||
global table, string
|
||||
-- global initialization when names don't fit in K
|
||||
|
||||
-- to fill constant table
|
||||
local code = {}
|
||||
for i = 1, 300 do code[i] = "'" .. i .. "'" end
|
||||
code = table.concat(code, ",")
|
||||
code = string.format([[
|
||||
return function (_ENV)
|
||||
local dummy = {%s} -- fill initial positions in constant table,
|
||||
-- so that initialization must use registers for global names
|
||||
global a, b, c = 10, 20, 30
|
||||
end]], code)
|
||||
|
||||
local fun = assert(load(code))()
|
||||
|
||||
local env = {}
|
||||
fun(env)
|
||||
assert(env.a == 10 and env.b == 20 and env.c == 30)
|
||||
end
|
||||
|
||||
|
||||
do -- testing global redefinitions
|
||||
-- cannot use 'checkerr' as errors are not compile time
|
||||
global pcall
|
||||
local f = assert(load("global print = 10"))
|
||||
local st, msg = pcall(f)
|
||||
assert(string.find(msg, "global 'print' already defined"))
|
||||
|
||||
local f = assert(load("local _ENV = {AA = false}; global AA = 10"))
|
||||
local st, msg = pcall(f)
|
||||
assert(string.find(msg, "global 'AA' already defined"))
|
||||
|
||||
end
|
||||
|
||||
print'OK'
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
-- $Id: heavy.lua,v 1.7 2017/12/29 15:42:15 roberto Exp $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- $Id: testes/heavy.lua,v $
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
local function teststring ()
|
||||
print("creating a string too long")
|
||||
@ -47,9 +49,9 @@ local function loadrep (x, what)
|
||||
end
|
||||
|
||||
|
||||
function controlstruct ()
|
||||
local function controlstruct ()
|
||||
print("control structure too long")
|
||||
local lim = ((1 << 24) - 2) // 3
|
||||
local lim = ((1 << 24) - 2) // 4
|
||||
local s = string.rep("a = a + 1\n", lim)
|
||||
s = "while true do " .. s .. "end"
|
||||
assert(load(s))
|
||||
@ -63,7 +65,7 @@ function controlstruct ()
|
||||
end
|
||||
|
||||
|
||||
function manylines ()
|
||||
local function manylines ()
|
||||
print("loading chunk with too many lines")
|
||||
local st, msg = loadrep("\n", "lines")
|
||||
assert(not st and string.find(msg, "too many lines"))
|
||||
@ -71,7 +73,7 @@ function manylines ()
|
||||
end
|
||||
|
||||
|
||||
function hugeid ()
|
||||
local function hugeid ()
|
||||
print("loading chunk with huge identifier")
|
||||
local st, msg = loadrep("a", "chars")
|
||||
assert(not st and
|
||||
@ -80,7 +82,7 @@ function hugeid ()
|
||||
print('+')
|
||||
end
|
||||
|
||||
function toomanyinst ()
|
||||
local function toomanyinst ()
|
||||
print("loading chunk with too many instructions")
|
||||
local st, msg = loadrep("a = 10; ", "instructions")
|
||||
print('+')
|
||||
@ -107,7 +109,7 @@ local function loadrepfunc (prefix, f)
|
||||
end
|
||||
|
||||
|
||||
function toomanyconst ()
|
||||
local function toomanyconst ()
|
||||
print("loading function with too many constants")
|
||||
loadrepfunc("function foo () return {0,",
|
||||
function (n)
|
||||
@ -126,7 +128,7 @@ function toomanyconst ()
|
||||
end
|
||||
|
||||
|
||||
function toomanystr ()
|
||||
local function toomanystr ()
|
||||
local a = {}
|
||||
local st, msg = pcall(function ()
|
||||
for i = 1, math.huge do
|
||||
@ -144,7 +146,7 @@ function toomanystr ()
|
||||
end
|
||||
|
||||
|
||||
function toomanyidx ()
|
||||
local function toomanyidx ()
|
||||
local a = {}
|
||||
local st, msg = pcall(function ()
|
||||
for i = 1, math.huge do
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "lua.h"
|
||||
|
||||
/* function from lib1.c */
|
||||
int lib1_export (lua_State *L);
|
||||
LUAMOD_API int lib1_export (lua_State *L);
|
||||
|
||||
LUAMOD_API int luaopen_lib11 (lua_State *L) {
|
||||
return lib1_export(L);
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
/* implementation for lib2-v2 */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
@ -8,8 +12,54 @@ static int id (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
struct STR {
|
||||
void *ud;
|
||||
lua_Alloc allocf;
|
||||
};
|
||||
|
||||
|
||||
static void *t_freestr (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
struct STR *blk = (struct STR*)ptr - 1;
|
||||
blk->allocf(blk->ud, blk, sizeof(struct STR) + osize, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static int newstr (lua_State *L) {
|
||||
size_t len;
|
||||
const char *str = luaL_checklstring(L, 1, &len);
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud);
|
||||
struct STR *blk = (struct STR*)allocf(ud, NULL, 0,
|
||||
len + 1 + sizeof(struct STR));
|
||||
if (blk == NULL) { /* allocation error? */
|
||||
lua_pushliteral(L, "not enough memory");
|
||||
lua_error(L); /* raise a memory error */
|
||||
}
|
||||
blk->ud = ud; blk->allocf = allocf;
|
||||
memcpy(blk + 1, str, len + 1);
|
||||
lua_pushexternalstring(L, (char *)(blk + 1), len, t_freestr, L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Create an external string and keep it in the registry, so that it
|
||||
** will test that the library code is still available (to deallocate
|
||||
** this string) when closing the state.
|
||||
*/
|
||||
static void initstr (lua_State *L) {
|
||||
lua_pushcfunction(L, newstr);
|
||||
lua_pushstring(L,
|
||||
"012345678901234567890123456789012345678901234567890123456789");
|
||||
lua_call(L, 1, 1); /* call newstr("0123...") */
|
||||
luaL_ref(L, LUA_REGISTRYINDEX); /* keep string in the registry */
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_Reg funcs[] = {
|
||||
{"id", id},
|
||||
{"newstr", newstr},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -18,6 +68,7 @@ LUAMOD_API int luaopen_lib2 (lua_State *L) {
|
||||
lua_settop(L, 2);
|
||||
lua_setglobal(L, "y"); /* y gets 2nd parameter */
|
||||
lua_setglobal(L, "x"); /* x gets 1st parameter */
|
||||
initstr(L);
|
||||
luaL_newlib(L, funcs);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@ LUA_DIR = ../../
|
||||
CC = gcc
|
||||
|
||||
# compilation should generate Dynamic-Link Libraries
|
||||
CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared
|
||||
CFLAGS = -Wall -O2 -I$(LUA_DIR) -fPIC -shared
|
||||
|
||||
# libraries used by the tests
|
||||
all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
-- $Id: testes/literals.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print('testing scanner')
|
||||
|
||||
global <const> *
|
||||
|
||||
local debug = require "debug"
|
||||
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
-- $Id: testes/locals.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
print('testing local variables and environments')
|
||||
|
||||
@ -39,9 +41,11 @@ f = nil
|
||||
local f
|
||||
local x = 1
|
||||
|
||||
a = nil
|
||||
load('local a = {}')()
|
||||
assert(a == nil)
|
||||
do
|
||||
global a; a = nil
|
||||
load('local a = {}')()
|
||||
assert(a == nil)
|
||||
end
|
||||
|
||||
function f (a)
|
||||
local _1, _2, _3, _4, _5
|
||||
@ -154,7 +158,7 @@ local _ENV = (function (...) return ... end)(_G, dummy) -- {
|
||||
do local _ENV = {assert=assert}; assert(true) end
|
||||
local mt = {_G = _G}
|
||||
local foo,x
|
||||
A = false -- "declare" A
|
||||
global A; A = false -- "declare" A
|
||||
do local _ENV = mt
|
||||
function foo (x)
|
||||
A = x
|
||||
@ -177,20 +181,27 @@ assert(x==20)
|
||||
A = nil
|
||||
|
||||
|
||||
do -- constants
|
||||
do print("testing local constants")
|
||||
global assert<const>, load, string, X
|
||||
X = 1 -- not a constant
|
||||
local a<const>, b, c<const> = 10, 20, 30
|
||||
b = a + c + b -- 'b' is not constant
|
||||
assert(a == 10 and b == 60 and c == 30)
|
||||
|
||||
local function checkro (name, code)
|
||||
local st, msg = load(code)
|
||||
local gab = string.format("attempt to assign to const variable '%s'", name)
|
||||
assert(not st and string.find(msg, gab))
|
||||
end
|
||||
|
||||
checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12")
|
||||
checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11")
|
||||
checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11")
|
||||
checkro("foo", "local foo <const> = 10; function foo() end")
|
||||
checkro("foo", "local foo <const> = {}; function foo() end")
|
||||
checkro("foo", "local<const> foo = 10; function foo() end")
|
||||
checkro("foo", "local<const> foo <const> = {}; function foo() end")
|
||||
checkro("foo", "global<const> foo <const>; function foo() end")
|
||||
checkro("XX", "global XX <const>; XX = 10")
|
||||
checkro("XX", "local _ENV; global XX <const>; XX = 10")
|
||||
|
||||
checkro("z", [[
|
||||
local a, z <const>, b = 10;
|
||||
@ -201,11 +212,26 @@ do -- constants
|
||||
local a, var1 <const> = 10;
|
||||
function foo() a = 20; z = function () var1 = 12; end end
|
||||
]])
|
||||
|
||||
checkro("var1", [[
|
||||
global a, var1 <const>, z;
|
||||
local function foo() a = 20; z = function () var1 = 12; end end
|
||||
]])
|
||||
end
|
||||
|
||||
|
||||
|
||||
print"testing to-be-closed variables"
|
||||
|
||||
|
||||
do
|
||||
local st, msg = load("local <close> a, b")
|
||||
assert(not st and string.find(msg, "multiple"))
|
||||
|
||||
local st, msg = load("local a<close>, b<close>")
|
||||
assert(not st and string.find(msg, "multiple"))
|
||||
end
|
||||
|
||||
local function stack(n) n = ((n == 0) or stack(n - 1)) end
|
||||
|
||||
local function func2close (f, x, y)
|
||||
@ -280,6 +306,31 @@ do
|
||||
end
|
||||
|
||||
|
||||
do -- testing presence of second argument
|
||||
local function foo (howtoclose, obj, n)
|
||||
local ca -- copy of 'a' visible inside its close metamethod
|
||||
do
|
||||
local a <close> = func2close(function (...t)
|
||||
assert(select("#", ...) == n)
|
||||
assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj))
|
||||
ca = 15 -- final value to be returned if howtoclose=="scope"
|
||||
end)
|
||||
ca = a
|
||||
if howtoclose == "ret" then return obj -- 'a' closed by return
|
||||
elseif howtoclose == "err" then error(obj) -- 'a' closed by error
|
||||
end
|
||||
end -- 'a' closed by end of scope
|
||||
return ca -- ca now should be 15
|
||||
end
|
||||
-- with no errors, closing methods receive no extra argument
|
||||
assert(foo("scope", nil, 1) == 15) -- close by end of scope
|
||||
assert(foo("ret", 32, 1) == 32) -- close by return
|
||||
-- with errors, they do
|
||||
local st, msg = pcall(foo, "err", 23, 2) -- close by error
|
||||
assert(not st and msg == 23)
|
||||
end
|
||||
|
||||
|
||||
-- testing to-be-closed x compile-time constants
|
||||
-- (there were some bugs here in Lua 5.4-rc3, due to a confusion
|
||||
-- between compile levels and stack levels of variables)
|
||||
@ -728,14 +779,8 @@ if rawget(_G, "T") then
|
||||
-- first buffer was released by 'toclose'
|
||||
assert(T.totalmem() - m <= extra)
|
||||
|
||||
-- error in creation of final string
|
||||
T.totalmem(m + 2 * lim + extra)
|
||||
assert(not pcall(table.concat, a))
|
||||
-- second buffer was released by 'toclose'
|
||||
assert(T.totalmem() - m <= extra)
|
||||
|
||||
-- userdata, buffer, buffer, final string
|
||||
T.totalmem(m + 4*lim + extra)
|
||||
-- userdata, buffer, final string
|
||||
T.totalmem(m + 2*lim + extra)
|
||||
assert(#table.concat(a) == 2*lim)
|
||||
|
||||
T.totalmem(0) -- remove memory limit
|
||||
@ -865,14 +910,15 @@ do
|
||||
|
||||
local extrares -- result from extra yield (if any)
|
||||
|
||||
local function check (body, extra, ...)
|
||||
local t = table.pack(...) -- expected returns
|
||||
local function check (body, extra, ...t)
|
||||
local co = coroutine.wrap(body)
|
||||
if extra then
|
||||
extrares = co() -- runs until first (extra) yield
|
||||
end
|
||||
local res = table.pack(co()) -- runs until yield inside '__close'
|
||||
assert(res.n == 2 and res[2] == nil)
|
||||
local res = table.pack(co()) -- runs until "regular" yield
|
||||
-- regular yield will yield all values passed to the close function;
|
||||
-- without errors, that is only the object being closed.
|
||||
assert(res.n == 1 and type(res[1]) == "table")
|
||||
local res2 = table.pack(co()) -- runs until end of function
|
||||
assert(res2.n == t.n)
|
||||
for i = 1, #t do
|
||||
@ -885,10 +931,10 @@ do
|
||||
end
|
||||
|
||||
local function foo ()
|
||||
local x <close> = func2close(coroutine.yield)
|
||||
local x <close> = func2close(coroutine.yield) -- "regular" yield
|
||||
local extra <close> = func2close(function (self)
|
||||
assert(self == extrares)
|
||||
coroutine.yield(100)
|
||||
coroutine.yield(100) -- first (extra) yield
|
||||
end)
|
||||
extrares = extra
|
||||
return table.unpack{10, x, 30}
|
||||
@ -897,21 +943,21 @@ do
|
||||
assert(extrares == 100)
|
||||
|
||||
local function foo ()
|
||||
local x <close> = func2close(coroutine.yield)
|
||||
local x <close> = func2close(coroutine.yield) -- "regular" yield
|
||||
return
|
||||
end
|
||||
check(foo, false)
|
||||
|
||||
local function foo ()
|
||||
local x <close> = func2close(coroutine.yield)
|
||||
local x <close> = func2close(coroutine.yield) -- "regular" yield
|
||||
local y, z = 20, 30
|
||||
return x
|
||||
end
|
||||
check(foo, false, "x")
|
||||
|
||||
local function foo ()
|
||||
local x <close> = func2close(coroutine.yield)
|
||||
local extra <close> = func2close(coroutine.yield)
|
||||
local x <close> = func2close(coroutine.yield) -- "regular" yield
|
||||
local extra <close> = func2close(coroutine.yield) -- extra yield
|
||||
return table.unpack({}, 1, 100) -- 100 nils
|
||||
end
|
||||
check(foo, true, table.unpack({}, 1, 100))
|
||||
@ -1140,7 +1186,7 @@ do
|
||||
local function open (x)
|
||||
numopen = numopen + 1
|
||||
return
|
||||
function () -- iteraction function
|
||||
function () -- iteration function
|
||||
x = x - 1
|
||||
if x > 0 then return x end
|
||||
end,
|
||||
|
||||
110
testes/main.lua
110
testes/main.lua
@ -1,6 +1,6 @@
|
||||
# testing special comment on first line
|
||||
-- $Id: testes/main.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
-- most (all?) tests here assume a reasonable "Unix-like" shell
|
||||
if _port then return end
|
||||
@ -27,17 +27,19 @@ do
|
||||
end
|
||||
print("progname: "..progname)
|
||||
|
||||
local prepfile = function (s, p)
|
||||
p = p or prog
|
||||
io.output(p)
|
||||
io.write(s)
|
||||
assert(io.close())
|
||||
|
||||
local prepfile = function (s, mod, p)
|
||||
mod = mod and "wb" or "w" -- mod true means binary files
|
||||
p = p or prog -- file to write the program
|
||||
local f = io.open(p, mod)
|
||||
f:write(s)
|
||||
assert(f:close())
|
||||
end
|
||||
|
||||
local function getoutput ()
|
||||
io.input(out)
|
||||
local t = io.read("a")
|
||||
io.input():close()
|
||||
local f = io.open(out)
|
||||
local t = f:read("a")
|
||||
f:close()
|
||||
assert(os.remove(out))
|
||||
return t
|
||||
end
|
||||
@ -65,10 +67,11 @@ local function RUN (p, ...)
|
||||
assert(os.execute(s))
|
||||
end
|
||||
|
||||
|
||||
local function NoRun (msg, p, ...)
|
||||
p = string.gsub(p, "lua", '"'..progname..'"', 1)
|
||||
local s = string.format(p, ...)
|
||||
s = string.format("%s 2> %s", s, out) -- will send error to 'out'
|
||||
s = string.format("%s >%s 2>&1", s, out) -- send output and error to 'out'
|
||||
assert(not os.execute(s))
|
||||
assert(string.find(getoutput(), msg, 1, true)) -- check error message
|
||||
end
|
||||
@ -87,7 +90,7 @@ prepfile[[
|
||||
1, a
|
||||
)
|
||||
]]
|
||||
RUN('lua - < %s > %s', prog, out)
|
||||
RUN('lua - -- < %s > %s', prog, out)
|
||||
checkout("1\tnil\n")
|
||||
|
||||
RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
|
||||
@ -108,17 +111,17 @@ RUN('lua %s > %s', prog, out)
|
||||
checkout("3\n")
|
||||
|
||||
-- bad BOMs
|
||||
prepfile("\xEF")
|
||||
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
|
||||
prepfile("\xEF", true)
|
||||
NoRun("unexpected symbol", 'lua %s', prog)
|
||||
|
||||
prepfile("\xEF\xBB")
|
||||
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
|
||||
prepfile("\xEF\xBB", true)
|
||||
NoRun("unexpected symbol", 'lua %s', prog)
|
||||
|
||||
prepfile("\xEFprint(3)")
|
||||
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
|
||||
prepfile("\xEFprint(3)", true)
|
||||
NoRun("unexpected symbol", 'lua %s', prog)
|
||||
|
||||
prepfile("\xEF\xBBprint(3)")
|
||||
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
|
||||
prepfile("\xEF\xBBprint(3)", true)
|
||||
NoRun("unexpected symbol", 'lua %s', prog)
|
||||
|
||||
|
||||
-- test option '-'
|
||||
@ -130,11 +133,11 @@ checkout("-h\n")
|
||||
prepfile("print(package.path)")
|
||||
|
||||
-- test LUA_PATH
|
||||
RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out)
|
||||
RUN('env LUA_INIT= LUA_PATH=x lua -- %s > %s', prog, out)
|
||||
checkout("x\n")
|
||||
|
||||
-- test LUA_PATH_version
|
||||
RUN('env LUA_INIT= LUA_PATH_5_4=y LUA_PATH=x lua %s > %s', prog, out)
|
||||
RUN('env LUA_INIT= LUA_PATH_5_5=y LUA_PATH=x lua %s > %s', prog, out)
|
||||
checkout("y\n")
|
||||
|
||||
-- test LUA_CPATH
|
||||
@ -143,7 +146,7 @@ RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out)
|
||||
checkout("xuxu\n")
|
||||
|
||||
-- test LUA_CPATH_version
|
||||
RUN('env LUA_INIT= LUA_CPATH_5_4=yacc LUA_CPATH=x lua %s > %s', prog, out)
|
||||
RUN('env LUA_INIT= LUA_CPATH_5_5=yacc LUA_CPATH=x lua %s > %s', prog, out)
|
||||
checkout("yacc\n")
|
||||
|
||||
-- test LUA_INIT (and its access to 'arg' table)
|
||||
@ -153,7 +156,7 @@ checkout("3.2\n")
|
||||
|
||||
-- test LUA_INIT_version
|
||||
prepfile("print(X)")
|
||||
RUN('env LUA_INIT_5_4="X=10" LUA_INIT="X=3" lua %s > %s', prog, out)
|
||||
RUN('env LUA_INIT_5_5="X=10" LUA_INIT="X=3" lua %s > %s', prog, out)
|
||||
checkout("10\n")
|
||||
|
||||
-- test LUA_INIT for files
|
||||
@ -213,7 +216,7 @@ convert("a;b;;c")
|
||||
|
||||
-- test -l over multiple libraries
|
||||
prepfile("print(1); a=2; return {x=15}")
|
||||
prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
|
||||
prepfile(("print(a); print(_G['%s'].x)"):format(prog), false, otherprog)
|
||||
RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
|
||||
checkout("1\n2\n15\n2\n15\n")
|
||||
|
||||
@ -222,6 +225,13 @@ prepfile("print(str.upper'alo alo', m.max(10, 20))")
|
||||
RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
|
||||
checkout("0.0\nALO ALO\t20\n")
|
||||
|
||||
|
||||
-- test module names with version suffix ("libs/lib2-v2")
|
||||
RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s",
|
||||
out)
|
||||
checkout("true\n")
|
||||
|
||||
|
||||
-- test 'arg' table
|
||||
local a = [[
|
||||
assert(#arg == 3 and arg[1] == 'a' and
|
||||
@ -237,7 +247,7 @@ RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command
|
||||
|
||||
-- test 'arg' availability in libraries
|
||||
prepfile"assert(arg)"
|
||||
prepfile("assert(arg)", otherprog)
|
||||
prepfile("assert(arg)", false, otherprog)
|
||||
RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
|
||||
|
||||
-- test messing up the 'arg' table
|
||||
@ -253,6 +263,15 @@ assert(string.find(getoutput(), "error calling 'print'"))
|
||||
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
|
||||
checkout("lua_debug> 1000lua_debug> ")
|
||||
|
||||
do -- test warning for locals
|
||||
RUN('echo " local x" | lua -i > %s 2>&1', out)
|
||||
assert(string.find(getoutput(), "warning: "))
|
||||
|
||||
RUN('echo "local1 = 10\nlocal1 + 3" | lua -i > %s 2>&1', out)
|
||||
local t = getoutput()
|
||||
assert(not string.find(t, "warning"))
|
||||
assert(string.find(t, "13"))
|
||||
end
|
||||
|
||||
print("testing warnings")
|
||||
|
||||
@ -291,8 +310,11 @@ checkprogout("ZYX)\nXYZ)\n")
|
||||
-- bug since 5.2: finalizer called when closing a state could
|
||||
-- subvert finalization order
|
||||
prepfile[[
|
||||
-- should be called last
|
||||
-- ensure tables will be collected only at the end of the program
|
||||
collectgarbage"stop"
|
||||
|
||||
print("creating 1")
|
||||
-- this finalizer should be called last
|
||||
setmetatable({}, {__gc = function () print(1) end})
|
||||
|
||||
print("creating 2")
|
||||
@ -302,7 +324,7 @@ setmetatable({}, {__gc = function ()
|
||||
-- this finalizer should not be called, as object will be
|
||||
-- created after 'lua_close' has been called
|
||||
setmetatable({}, {__gc = function () print(3) end})
|
||||
print(collectgarbage()) -- cannot call collector here
|
||||
print(collectgarbage() or false) -- cannot call collector here
|
||||
os.exit(0, true)
|
||||
end})
|
||||
]]
|
||||
@ -312,7 +334,7 @@ creating 1
|
||||
creating 2
|
||||
2
|
||||
creating 3
|
||||
nil
|
||||
false
|
||||
1
|
||||
]]
|
||||
|
||||
@ -325,7 +347,7 @@ checkout("a\n")
|
||||
RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out)
|
||||
checkout("1\n3\n")
|
||||
|
||||
-- test iteractive mode
|
||||
-- test interactive mode
|
||||
prepfile[[
|
||||
(6*2-6) -- ===
|
||||
a =
|
||||
@ -335,10 +357,15 @@ a]]
|
||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
||||
checkprogout("6\n10\n10\n\n")
|
||||
|
||||
prepfile("a = [[b\nc\nd\ne]]\n=a")
|
||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
||||
prepfile("a = [[b\nc\nd\ne]]\na")
|
||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i -- < %s > %s]], prog, out)
|
||||
checkprogout("b\nc\nd\ne\n\n")
|
||||
|
||||
-- input interrupted in continuation line
|
||||
prepfile("a.\n")
|
||||
RUN([[lua -i < %s > /dev/null 2> %s]], prog, out)
|
||||
checkprogout("near <eof>\n")
|
||||
|
||||
local prompt = "alo"
|
||||
prepfile[[ --
|
||||
a = 2
|
||||
@ -358,20 +385,18 @@ assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
|
||||
|
||||
|
||||
-- non-string prompt
|
||||
prompt =
|
||||
"local C = 0;\z
|
||||
_PROMPT=setmetatable({},{__tostring = function () \z
|
||||
C = C + 1; return C end})"
|
||||
prompt = [[
|
||||
local C = 'X';
|
||||
_PROMPT=setmetatable({},{__tostring = function ()
|
||||
C = C .. 'X'; return C end})
|
||||
]]
|
||||
prepfile[[ --
|
||||
a = 2
|
||||
]]
|
||||
RUN([[lua -e "%s" -i < %s > %s]], prompt, prog, out)
|
||||
local t = getoutput()
|
||||
assert(string.find(t, [[
|
||||
1 --
|
||||
2a = 2
|
||||
3
|
||||
]], 1, true))
|
||||
-- skip version line and then check the presence of the three prompts
|
||||
assert(string.find(t, "^.-\nXX[^\nX]*\n?XXX[^\nX]*\n?XXXX\n?$"))
|
||||
|
||||
|
||||
-- test for error objects
|
||||
@ -413,7 +438,7 @@ prepfile[[#comment in 1st line without \n at the end]]
|
||||
RUN('lua %s', prog)
|
||||
|
||||
-- first-line comment with binary file
|
||||
prepfile("#comment\n" .. string.dump(load("print(3)")))
|
||||
prepfile("#comment\n" .. string.dump(load("print(3)")), true)
|
||||
RUN('lua %s > %s', prog, out)
|
||||
checkout('3\n')
|
||||
|
||||
@ -463,12 +488,13 @@ assert(not os.remove(out))
|
||||
-- invalid options
|
||||
NoRun("unrecognized option '-h'", "lua -h")
|
||||
NoRun("unrecognized option '---'", "lua ---")
|
||||
NoRun("unrecognized option '-Ex'", "lua -Ex")
|
||||
NoRun("unrecognized option '-Ex'", "lua -Ex --")
|
||||
NoRun("unrecognized option '-vv'", "lua -vv")
|
||||
NoRun("unrecognized option '-iv'", "lua -iv")
|
||||
NoRun("'-e' needs argument", "lua -e")
|
||||
NoRun("syntax error", "lua -e a")
|
||||
NoRun("'-l' needs argument", "lua -l")
|
||||
NoRun("-i", "lua -- -i") -- handles -i as a script name
|
||||
|
||||
|
||||
if T then -- test library?
|
||||
|
||||
139
testes/math.lua
139
testes/math.lua
@ -1,10 +1,17 @@
|
||||
-- $Id: testes/math.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print("testing numbers and math lib")
|
||||
|
||||
local minint <const> = math.mininteger
|
||||
local maxint <const> = math.maxinteger
|
||||
local math = require "math"
|
||||
local string = require "string"
|
||||
|
||||
global none
|
||||
|
||||
global<const> print, assert, pcall, type, pairs, load
|
||||
global<const> tonumber, tostring, select
|
||||
|
||||
local<const> minint, maxint = math.mininteger, math.maxinteger
|
||||
|
||||
local intbits <const> = math.floor(math.log(maxint, 2) + 0.5) + 1
|
||||
assert((1 << intbits) == 0)
|
||||
@ -22,6 +29,18 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- maximum exponent for a floating-point number
|
||||
local maxexp = 0
|
||||
do
|
||||
local p = 2.0
|
||||
while p < math.huge do
|
||||
maxexp = maxexp + 1
|
||||
p = p + p
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local function isNaN (x)
|
||||
return (x ~= x)
|
||||
end
|
||||
@ -34,8 +53,8 @@ do
|
||||
local x = 2.0^floatbits
|
||||
assert(x > x - 1.0 and x == x + 1.0)
|
||||
|
||||
print(string.format("%d-bit integers, %d-bit (mantissa) floats",
|
||||
intbits, floatbits))
|
||||
local msg = " %d-bit integers, %d-bit*2^%d floats"
|
||||
print(string.format(msg, intbits, floatbits, maxexp))
|
||||
end
|
||||
|
||||
assert(math.type(0) == "integer" and math.type(0.0) == "float"
|
||||
@ -172,7 +191,7 @@ do
|
||||
for i = -3, 3 do -- variables avoid constant folding
|
||||
for j = -3, 3 do
|
||||
-- domain errors (0^(-n)) are not portable
|
||||
if not _port or i ~= 0 or j > 0 then
|
||||
if not _ENV._port or i ~= 0 or j > 0 then
|
||||
assert(eq(i^j, 1 / i^(-j)))
|
||||
end
|
||||
end
|
||||
@ -418,7 +437,7 @@ for i = 2,36 do
|
||||
assert(tonumber('\t10000000000\t', i) == i10)
|
||||
end
|
||||
|
||||
if not _soft then
|
||||
if not _ENV._soft then
|
||||
-- tests with very long numerals
|
||||
assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1)
|
||||
assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1)
|
||||
@ -620,7 +639,7 @@ assert(maxint % -2 == -1)
|
||||
|
||||
-- non-portable tests because Windows C library cannot compute
|
||||
-- fmod(1, huge) correctly
|
||||
if not _port then
|
||||
if not _ENV._port then
|
||||
local function anan (x) assert(isNaN(x)) end -- assert Not a Number
|
||||
anan(0.0 % 0)
|
||||
anan(1.3 % 0)
|
||||
@ -666,6 +685,18 @@ assert(eq(math.exp(0), 1))
|
||||
assert(eq(math.sin(10), math.sin(10%(2*math.pi))))
|
||||
|
||||
|
||||
do print("testing ldexp/frexp")
|
||||
global ipairs
|
||||
for _, x in ipairs{0, 10, 32, -math.pi, 1e10, 1e-10, math.huge, -math.huge} do
|
||||
local m, p = math.frexp(x)
|
||||
assert(math.ldexp(m, p) == x)
|
||||
local am = math.abs(m)
|
||||
assert(m == x or (0.5 <= am and am < 1))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
assert(tonumber(' 1.3e-2 ') == 1.3e-2)
|
||||
assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
|
||||
|
||||
@ -767,6 +798,7 @@ assert(a == '10' and b == '20')
|
||||
|
||||
do
|
||||
print("testing -0 and NaN")
|
||||
global rawset, undef
|
||||
local mz <const> = -0.0
|
||||
local z <const> = 0.0
|
||||
assert(mz == z)
|
||||
@ -803,7 +835,11 @@ do
|
||||
end
|
||||
|
||||
|
||||
print("testing 'math.random'")
|
||||
--
|
||||
-- [[==================================================================
|
||||
print("testing 'math.random'")
|
||||
-- -===================================================================
|
||||
--
|
||||
|
||||
local random, max, min = math.random, math.max, math.min
|
||||
|
||||
@ -1019,6 +1055,91 @@ assert(not pcall(random, minint + 1, minint))
|
||||
assert(not pcall(random, maxint, maxint - 1))
|
||||
assert(not pcall(random, maxint, minint))
|
||||
|
||||
-- ]]==================================================================
|
||||
|
||||
|
||||
--
|
||||
-- [[==================================================================
|
||||
print("testing precision of 'tostring'")
|
||||
-- -===================================================================
|
||||
--
|
||||
|
||||
-- number of decimal digits supported by float precision
|
||||
local decdig = math.floor(floatbits * math.log(2, 10))
|
||||
print(string.format(" %d-digit float numbers with full precision",
|
||||
decdig))
|
||||
-- number of decimal digits supported by integer precision
|
||||
local Idecdig = math.floor(math.log(maxint, 10))
|
||||
print(string.format(" %d-digit integer numbers with full precision",
|
||||
Idecdig))
|
||||
|
||||
do
|
||||
-- Any number should print so that reading it back gives itself:
|
||||
-- tonumber(tostring(x)) == x
|
||||
|
||||
-- Mersenne fractions
|
||||
local p = 1.0
|
||||
for i = 1, maxexp do
|
||||
p = p + p
|
||||
local x = 1 / (p - 1)
|
||||
assert(x == tonumber(tostring(x)))
|
||||
end
|
||||
|
||||
-- some random numbers in [0,1)
|
||||
for i = 1, 100 do
|
||||
local x = math.random()
|
||||
assert(x == tonumber(tostring(x)))
|
||||
end
|
||||
|
||||
-- different numbers should print differently.
|
||||
-- check pairs of floats with minimum detectable difference
|
||||
local p = floatbits - 1
|
||||
global ipairs
|
||||
for i = 1, maxexp - 1 do
|
||||
for _, i in ipairs{-i, i} do
|
||||
local x = 2^i
|
||||
local diff = 2^(i - p) -- least significant bit for 'x'
|
||||
local y = x + diff
|
||||
local fy = tostring(y)
|
||||
assert(x ~= y and tostring(x) ~= fy)
|
||||
assert(tonumber(fy) == y)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- "reasonable" numerals should be printed like themselves
|
||||
|
||||
-- create random float numerals with 5 digits, with a decimal point
|
||||
-- inserted in all places. (With more than 5, things like "0.00001"
|
||||
-- reformats like "1e-5".)
|
||||
for i = 1, 1000 do
|
||||
-- random numeral with 5 digits
|
||||
local x = string.format("%.5d", math.random(0, 99999))
|
||||
for i = 2, #x do
|
||||
-- insert decimal point at position 'i'
|
||||
local y = string.sub(x, 1, i - 1) .. "." .. string.sub(x, i, -1)
|
||||
y = string.gsub(y, "^0*(%d.-%d)0*$", "%1") -- trim extra zeros
|
||||
assert(y == tostring(tonumber(y)))
|
||||
end
|
||||
end
|
||||
|
||||
-- all-random floats
|
||||
local Fsz = string.packsize("n") -- size of floats in bytes
|
||||
|
||||
for i = 1, 400 do
|
||||
local s = string.pack("j", math.random(0)) -- a random string of bits
|
||||
while #s < Fsz do -- make 's' long enough
|
||||
s = s .. string.pack("j", math.random(0))
|
||||
end
|
||||
local n = string.unpack("n", s) -- read 's' as a float
|
||||
s = tostring(n)
|
||||
if string.find(s, "^%-?%d") then -- avoid NaN, inf, -inf
|
||||
assert(tonumber(s) == n)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
-- ]]==================================================================
|
||||
|
||||
|
||||
print('OK')
|
||||
|
||||
309
testes/memerr.lua
Normal file
309
testes/memerr.lua
Normal file
@ -0,0 +1,309 @@
|
||||
-- $Id: testes/memerr.lua $
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
|
||||
local function checkerr (msg, f, ...)
|
||||
local stat, err = pcall(f, ...)
|
||||
assert(not stat and string.find(err, msg))
|
||||
end
|
||||
|
||||
if T==nil then
|
||||
(Message or print)
|
||||
('\n >>> testC not active: skipping memory error tests <<<\n')
|
||||
return
|
||||
end
|
||||
|
||||
print("testing memory-allocation errors")
|
||||
|
||||
local debug = require "debug"
|
||||
|
||||
local pack = table.pack
|
||||
|
||||
-- standard error message for memory errors
|
||||
local MEMERRMSG = "not enough memory"
|
||||
|
||||
|
||||
-- memory error in panic function
|
||||
T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
|
||||
assert(T.checkpanic("newuserdata 20000") == MEMERRMSG)
|
||||
T.totalmem(0) -- restore high limit
|
||||
|
||||
|
||||
|
||||
-- {==================================================================
|
||||
-- Testing memory limits
|
||||
-- ===================================================================
|
||||
|
||||
checkerr("block too big", T.newuserdata, math.maxinteger)
|
||||
collectgarbage()
|
||||
local f = load"local a={}; for i=1,100000 do a[i]=i end"
|
||||
T.alloccount(10)
|
||||
checkerr(MEMERRMSG, f)
|
||||
T.alloccount() -- remove limit
|
||||
|
||||
|
||||
-- preallocate stack space
|
||||
local function deep (n) if n > 0 then deep(n - 1) end end
|
||||
|
||||
|
||||
-- test memory errors; increase limit for maximum memory by steps,
|
||||
-- so that we get memory errors in all allocations of a given
|
||||
-- task, until there is enough memory to complete the task without
|
||||
-- errors.
|
||||
local function testbytes (s, f)
|
||||
collectgarbage()
|
||||
local M = T.totalmem()
|
||||
local oldM = M
|
||||
local a,b = nil
|
||||
while true do
|
||||
collectgarbage(); collectgarbage()
|
||||
deep(4)
|
||||
T.totalmem(M)
|
||||
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
|
||||
T.totalmem(0) -- remove limit
|
||||
if a and b == "OK" then break end -- stop when no more errors
|
||||
if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
|
||||
error(a, 0) -- propagate it
|
||||
end
|
||||
M = M + 7 -- increase memory limit
|
||||
end
|
||||
print(string.format("minimum memory for %s: %d bytes", s, M - oldM))
|
||||
return a
|
||||
end
|
||||
|
||||
-- test memory errors; increase limit for number of allocations one
|
||||
-- by one, so that we get memory errors in all allocations of a given
|
||||
-- task, until there is enough allocations to complete the task without
|
||||
-- errors.
|
||||
|
||||
local function testalloc (s, f)
|
||||
collectgarbage()
|
||||
local M = 0
|
||||
local a,b = nil
|
||||
while true do
|
||||
collectgarbage(); collectgarbage()
|
||||
deep(4)
|
||||
T.alloccount(M)
|
||||
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
|
||||
T.alloccount() -- remove limit
|
||||
if a and b == "OK" then break end -- stop when no more errors
|
||||
if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
|
||||
error(a, 0) -- propagate it
|
||||
end
|
||||
M = M + 1 -- increase allocation limit
|
||||
end
|
||||
print(string.format("minimum allocations for %s: %d allocations", s, M))
|
||||
return M
|
||||
end
|
||||
|
||||
|
||||
local function testamem (s, f)
|
||||
local aloc = testalloc(s, f)
|
||||
local res = testbytes(s, f)
|
||||
return {aloc = aloc, res = res}
|
||||
end
|
||||
|
||||
|
||||
local b = testamem("function call", function () return 10 end)
|
||||
assert(b.res == 10 and b.aloc == 0)
|
||||
|
||||
testamem("state creation", function ()
|
||||
local st = T.newstate()
|
||||
if st then T.closestate(st) end -- close new state
|
||||
return st
|
||||
end)
|
||||
|
||||
testamem("empty-table creation", function ()
|
||||
return {}
|
||||
end)
|
||||
|
||||
testamem("string creation", function ()
|
||||
return "XXX" .. "YYY"
|
||||
end)
|
||||
|
||||
testamem("coroutine creation", function()
|
||||
return coroutine.create(print)
|
||||
end)
|
||||
|
||||
do -- vararg tables
|
||||
local function pack (...t) return t end
|
||||
local b = testamem("vararg table", function ()
|
||||
return pack(10, 20, 30, 40, "hello")
|
||||
end)
|
||||
assert(b.aloc == 3) -- new table uses three memory blocks
|
||||
-- table optimized away
|
||||
local function sel (n, ...arg) return arg[n] + arg.n end
|
||||
local b = testamem("optimized vararg table",
|
||||
function () return sel(2.0, 20, 30) end)
|
||||
assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case
|
||||
end
|
||||
|
||||
-- testing to-be-closed variables
|
||||
testamem("to-be-closed variables", function()
|
||||
local flag
|
||||
do
|
||||
local x <close> =
|
||||
setmetatable({}, {__close = function () flag = true end})
|
||||
flag = false
|
||||
local x = {}
|
||||
end
|
||||
return flag
|
||||
end)
|
||||
|
||||
|
||||
-- testing threads
|
||||
|
||||
-- get main thread from registry
|
||||
local mt = T.testC("rawgeti R !M; return 1")
|
||||
assert(type(mt) == "thread" and coroutine.running() == mt)
|
||||
|
||||
|
||||
|
||||
local function expand (n,s)
|
||||
if n==0 then return "" end
|
||||
local e = string.rep("=", n)
|
||||
return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
|
||||
e, s, expand(n-1,s), e)
|
||||
end
|
||||
|
||||
G=0; collectgarbage()
|
||||
load(expand(20,"G=G+1"))()
|
||||
assert(G==20); collectgarbage()
|
||||
G = nil
|
||||
|
||||
testamem("running code on new thread", function ()
|
||||
return T.doonnewstack("local x=1") == 0 -- try to create thread
|
||||
end)
|
||||
|
||||
|
||||
do -- external strings
|
||||
local str = string.rep("a", 100)
|
||||
testamem("creating external strings", function ()
|
||||
return T.externstr(str)
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
-- testing memory x compiler
|
||||
|
||||
testamem("loadstring", function ()
|
||||
return load("x=1") -- try to do load a string
|
||||
end)
|
||||
|
||||
|
||||
local testprog = [[
|
||||
local function foo () return end
|
||||
local t = {"x"}
|
||||
AA = "aaa"
|
||||
for i = 1, #t do AA = AA .. t[i] end
|
||||
return true
|
||||
]]
|
||||
|
||||
-- testing memory x dofile
|
||||
_G.AA = nil
|
||||
local t =os.tmpname()
|
||||
local f = assert(io.open(t, "w"))
|
||||
f:write(testprog)
|
||||
f:close()
|
||||
testamem("dofile", function ()
|
||||
local a = loadfile(t)
|
||||
return a and a()
|
||||
end)
|
||||
assert(os.remove(t))
|
||||
assert(_G.AA == "aaax")
|
||||
|
||||
|
||||
-- other generic tests
|
||||
|
||||
testamem("gsub", function ()
|
||||
local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
|
||||
return (a == 'ablo ablo')
|
||||
end)
|
||||
|
||||
testamem("dump/undump", function ()
|
||||
local a = load(testprog)
|
||||
local b = a and string.dump(a)
|
||||
a = b and load(b)
|
||||
return a and a()
|
||||
end)
|
||||
|
||||
_G.AA = nil
|
||||
|
||||
local t = os.tmpname()
|
||||
testamem("file creation", function ()
|
||||
local f = assert(io.open(t, 'w'))
|
||||
assert (not io.open"nomenaoexistente")
|
||||
io.close(f);
|
||||
return not loadfile'nomenaoexistente'
|
||||
end)
|
||||
assert(os.remove(t))
|
||||
|
||||
testamem("table creation", function ()
|
||||
local a, lim = {}, 10
|
||||
for i=1,lim do a[i] = i; a[i..'a'] = {} end
|
||||
return (type(a[lim..'a']) == 'table' and a[lim] == lim)
|
||||
end)
|
||||
|
||||
testamem("constructors", function ()
|
||||
local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
|
||||
return (type(a) == 'table' and a.e == 5)
|
||||
end)
|
||||
|
||||
local a = 1
|
||||
local close = nil
|
||||
testamem("closure creation", function ()
|
||||
function close (b)
|
||||
return function (x) return b + x end
|
||||
end
|
||||
return (close(2)(4) == 6)
|
||||
end)
|
||||
|
||||
testamem("using coroutines", function ()
|
||||
local a = coroutine.wrap(function ()
|
||||
coroutine.yield(string.rep("a", 10))
|
||||
return {}
|
||||
end)
|
||||
assert(string.len(a()) == 10)
|
||||
return a()
|
||||
end)
|
||||
|
||||
do -- auxiliary buffer
|
||||
local lim = 100
|
||||
local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
|
||||
testamem("auxiliary buffer", function ()
|
||||
return (#table.concat(a, ",") == 20*lim + lim - 1)
|
||||
end)
|
||||
end
|
||||
|
||||
testamem("growing stack", function ()
|
||||
local function foo (n)
|
||||
if n == 0 then return 1 else return 1 + foo(n - 1) end
|
||||
end
|
||||
return foo(100)
|
||||
end)
|
||||
|
||||
|
||||
collectgarbage()
|
||||
collectgarbage()
|
||||
global io, T, setmetatable, collectgarbage, print
|
||||
|
||||
local Count = 0
|
||||
testamem("finalizers", function ()
|
||||
local X = false
|
||||
local obj = setmetatable({}, {__gc = function () X = true end})
|
||||
obj = nil
|
||||
T.resetCI() -- remove extra CallInfos
|
||||
T.reallocstack(18) -- remove extra stack slots
|
||||
Count = Count + 1
|
||||
io.stderr:write(Count, "\n")
|
||||
T.trick(io)
|
||||
collectgarbage()
|
||||
return X
|
||||
end)
|
||||
|
||||
-- }==================================================================
|
||||
|
||||
|
||||
print "Ok"
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
-- $Id: testes/nextvar.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
global <const> *
|
||||
|
||||
print('testing tables, next, and for')
|
||||
|
||||
@ -9,6 +11,28 @@ local function checkerror (msg, f, ...)
|
||||
end
|
||||
|
||||
|
||||
|
||||
----------------------------------------------------------------
|
||||
local function printTable (t)
|
||||
local a, h = T.querytab(t)
|
||||
print("array:")
|
||||
for i = 1, a do
|
||||
print("", T.querytab(t, i - 1))
|
||||
end
|
||||
print("hash:")
|
||||
for i = 1, h do
|
||||
print("", T.querytab(t, a + i - 1))
|
||||
end
|
||||
end
|
||||
----------------------------------------------------------------
|
||||
local function countentries (t)
|
||||
local e = 0
|
||||
for _ in pairs(t) do e = e + 1 end
|
||||
return e
|
||||
end
|
||||
----------------------------------------------------------------
|
||||
|
||||
|
||||
local function check (t, na, nh)
|
||||
if not T then return end
|
||||
local a, h = T.querytab(t)
|
||||
@ -39,13 +63,32 @@ do -- rehash moving elements from array to hash
|
||||
for i = 5, 95 do a[i] = nil end
|
||||
check(a, 128, 0)
|
||||
|
||||
a.x = 1 -- force a re-hash
|
||||
check(a, 4, 8)
|
||||
a[129] = 1 -- force a re-hash
|
||||
check(a, 4, 8) -- keys larger than 4 go to the hash part
|
||||
|
||||
for i = 1, 4 do assert(a[i] == i) end
|
||||
for i = 5, 95 do assert(a[i] == nil) end
|
||||
for i = 96, 100 do assert(a[i] == i) end
|
||||
assert(a.x == 1)
|
||||
assert(a[129] == 1)
|
||||
end
|
||||
|
||||
|
||||
do -- growing hash part keeping array part
|
||||
local a = table.create(1000)
|
||||
check(a, 1000, 0)
|
||||
a.x = 10
|
||||
check(a, 1000, 1) -- array part keeps its elements
|
||||
end
|
||||
|
||||
|
||||
do -- "growing" length of a prebuilt table
|
||||
local N = 100
|
||||
local a = table.create(N)
|
||||
for i = 1, N do
|
||||
a[#a + 1] = true
|
||||
assert(#a == i)
|
||||
end
|
||||
check(a, N, 0)
|
||||
end
|
||||
|
||||
|
||||
@ -80,6 +123,24 @@ do -- overflow (must wrap-around)
|
||||
assert(k == nil)
|
||||
end
|
||||
|
||||
|
||||
do
|
||||
-- alternate insertions and deletions in an almost full hash.
|
||||
-- In versions pre-5.5, that causes constant rehashings and
|
||||
-- takes a long time to complete.
|
||||
local a = {}
|
||||
for i = 1, 2^11 - 1 do
|
||||
a[i .. ""] = true
|
||||
end
|
||||
|
||||
for i = 1, 1e5 do
|
||||
local key = i .. "."
|
||||
a[key] = true
|
||||
a[key] = nil
|
||||
end
|
||||
assert(countentries(a) == 2^11 - 1)
|
||||
end
|
||||
|
||||
if not T then
|
||||
(Message or print)
|
||||
('\n >>> testC not active: skipping tests for table sizes <<<\n')
|
||||
@ -87,9 +148,10 @@ else --[
|
||||
-- testing table sizes
|
||||
|
||||
|
||||
local function mp2 (n) -- minimum power of 2 >= n
|
||||
-- minimum power of 2 (or zero) >= n
|
||||
local function mp2 (n)
|
||||
local mp = 2^math.ceil(math.log(n, 2))
|
||||
assert(n == 0 or (mp/2 < n and n <= mp))
|
||||
assert((mp == 0 or mp/2 < n) and n <= mp)
|
||||
return mp
|
||||
end
|
||||
|
||||
@ -104,7 +166,7 @@ end
|
||||
|
||||
-- testing constructor sizes
|
||||
local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17,
|
||||
30, 31, 32, 33, 34, 254, 255, 256, 500, 1000}
|
||||
30, 31, 32, 33, 34, 254, 255, 256, 257, 500, 1001}
|
||||
|
||||
for _, sa in ipairs(sizes) do -- 'sa' is size of the array part
|
||||
local arr = {"return {"}
|
||||
@ -148,8 +210,9 @@ end
|
||||
|
||||
-- testing tables dynamically built
|
||||
local lim = 130
|
||||
local a = {}; a[2] = 1; check(a, 0, 1)
|
||||
a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
|
||||
local a = {}; a[2] = 1; check(a, 2, 0)
|
||||
a = {}; a[0] = 1; check(a, 0, 1);
|
||||
a[2] = 1; check(a, 2, 1)
|
||||
a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
|
||||
a = {}
|
||||
for i = 1,lim do
|
||||
@ -165,28 +228,82 @@ for i = 1,lim do
|
||||
check(a, 0, mp2(i))
|
||||
end
|
||||
|
||||
a = {}
|
||||
for i=1,16 do a[i] = i end
|
||||
check(a, 16, 0)
|
||||
|
||||
-- insert and delete elements until a rehash occur. Caller must ensure
|
||||
-- that a rehash will change the shape of the table. Must repeat because
|
||||
-- the insertion may collide with the deleted element, and then there is
|
||||
-- no rehash.
|
||||
local function forcerehash (t)
|
||||
local na, nh = T.querytab(t)
|
||||
local i = 10000
|
||||
repeat
|
||||
i = i + 1
|
||||
t[i] = true
|
||||
t[i] = undef
|
||||
local nna, nnh = T.querytab(t)
|
||||
until nna ~= na or nnh ~= nh
|
||||
end
|
||||
|
||||
|
||||
do
|
||||
local a = {}
|
||||
for i=1,16 do a[i] = i end
|
||||
check(a, 16, 0)
|
||||
for i=1,11 do a[i] = undef end
|
||||
for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?)
|
||||
check(a, 0, 8) -- 5 elements in the table
|
||||
check(a, 16, 0)
|
||||
a[30] = true -- force a rehash
|
||||
a[30] = undef
|
||||
check(a, 0, 8) -- 5 elements in the hash part: [12]-[16]
|
||||
a[10] = 1
|
||||
for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?)
|
||||
check(a, 0, 8) -- only 6 elements in the table
|
||||
forcerehash(a)
|
||||
check(a, 16, 1)
|
||||
for i=1,14 do a[i] = true; a[i] = undef end
|
||||
for i=18,50 do a[i] = true; a[i] = undef end -- force a rehash (?)
|
||||
check(a, 0, 4) -- only 2 elements ([15] and [16])
|
||||
check(a, 16, 1) -- no rehash...
|
||||
a[31] = true; a[32] = true -- force a rehash
|
||||
check(a, 0, 4) -- [15], [16], [31], [32]
|
||||
end
|
||||
|
||||
-- reverse filling
|
||||
for i=1,lim do
|
||||
do
|
||||
local N = 2^10
|
||||
local a = {}
|
||||
for i=i,1,-1 do a[i] = i end -- fill in reverse
|
||||
check(a, mp2(i), 0)
|
||||
for i = N, 1, -1 do a[i] = i end -- fill in reverse
|
||||
check(a, mp2(N), 0)
|
||||
end
|
||||
|
||||
|
||||
do -- "almost sparse" arrays
|
||||
-- create table with holes in 1/3 of its entries; all its
|
||||
-- elements are always in the array part
|
||||
local a = {}
|
||||
for i = 1, 257 do
|
||||
if i % 3 ~= 1 then
|
||||
a[i] = true
|
||||
check(a, mp2(i), 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
do
|
||||
-- alternate insertions and deletions should give some extra
|
||||
-- space for the hash part. Otherwise, a mix of insertions/deletions
|
||||
-- could cause too many rehashes. (See the other test for "alternate
|
||||
-- insertions and deletions" in this file.)
|
||||
local a = {}
|
||||
for i = 1, 256 do
|
||||
a[i .. ""] = true
|
||||
end
|
||||
check(a, 0, 256) -- hash part is full
|
||||
a["256"] = nil -- delete a key
|
||||
forcerehash(a)
|
||||
-- table has only 255 elements, but it got some extra space;
|
||||
-- otherwise, almost each delete-insert would rehash the table again.
|
||||
assert(countentries(a) == 255)
|
||||
check(a, 0, 512)
|
||||
end
|
||||
|
||||
|
||||
-- size tests for vararg
|
||||
lim = 35
|
||||
local function foo (n, ...)
|
||||
@ -201,21 +318,6 @@ end
|
||||
local a = {}
|
||||
for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end
|
||||
|
||||
|
||||
-- Table length with limit smaller than maximum value at array
|
||||
local a = {}
|
||||
for i = 1,64 do a[i] = true end -- make its array size 64
|
||||
for i = 1,64 do a[i] = nil end -- erase all elements
|
||||
assert(T.querytab(a) == 64) -- array part has 64 elements
|
||||
a[32] = true; a[48] = true; -- binary search will find these ones
|
||||
a[51] = true -- binary search will miss this one
|
||||
assert(#a == 48) -- this will set the limit
|
||||
assert(select(4, T.querytab(a)) == 48) -- this is the limit now
|
||||
a[50] = true -- this will set a new limit
|
||||
assert(select(4, T.querytab(a)) == 50) -- this is the limit now
|
||||
-- but the size is larger (and still inside the array part)
|
||||
assert(#a == 51)
|
||||
|
||||
end --]
|
||||
|
||||
|
||||
@ -229,13 +331,36 @@ assert(#{1, 2, 3, nil, nil} == 3)
|
||||
print'+'
|
||||
|
||||
|
||||
do
|
||||
local s1, s2 = math.randomseed()
|
||||
print(string.format(
|
||||
"testing length for some random tables (seeds 0X%x:%x)", s1, s2))
|
||||
local N = 130
|
||||
for i = 1, 1e3 do -- create that many random tables
|
||||
local a = table.create(math.random(N)) -- initiate with random size
|
||||
for j = 1, math.random(N) do -- add random number of random entries
|
||||
a[math.random(N)] = true
|
||||
end
|
||||
assert(#a == 0 or a[#a] and not a[#a + 1])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
do print("testing attack on table length")
|
||||
local t = {}
|
||||
local lim = math.floor(math.log(math.maxinteger, 2)) - 1
|
||||
for i = lim, 0, -1 do
|
||||
t[2^i] = true
|
||||
end
|
||||
assert(t[1 << lim])
|
||||
-- next loop should not take forever
|
||||
for i = 1, #t do end
|
||||
end
|
||||
|
||||
local nofind = {}
|
||||
|
||||
a,b,c = 1,2,3
|
||||
a,b,c = nil
|
||||
|
||||
|
||||
-- next uses always the same iteraction function
|
||||
-- next uses always the same iteration function
|
||||
assert(next{} == next{})
|
||||
|
||||
local function find (name)
|
||||
@ -282,7 +407,7 @@ for i=0,10000 do
|
||||
end
|
||||
end
|
||||
|
||||
n = {n=0}
|
||||
local n = {n=0}
|
||||
for i,v in pairs(a) do
|
||||
n.n = n.n+1
|
||||
assert(i and v and a[i] == v)
|
||||
@ -609,10 +734,12 @@ do
|
||||
a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1)
|
||||
end
|
||||
|
||||
do -- changing the control variable
|
||||
local a
|
||||
a = 0; for i = 1, 10 do a = a + 1; i = "x" end; assert(a == 10)
|
||||
a = 0; for i = 10.0, 1, -1 do a = a + 1; i = "x" end; assert(a == 10)
|
||||
do -- attempt to change the control variable
|
||||
local st, msg = load "for i = 1, 10 do i = 10 end"
|
||||
assert(not st and string.find(msg, "assign to const variable 'i'"))
|
||||
|
||||
local st, msg = load "for v, k in pairs{} do v = 10 end"
|
||||
assert(not st and string.find(msg, "assign to const variable 'v'"))
|
||||
end
|
||||
|
||||
-- conversion
|
||||
@ -778,13 +905,18 @@ local function foo1 (e,i)
|
||||
if i <= e.n then return i,a[i] end
|
||||
end
|
||||
|
||||
setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
|
||||
local closed = false
|
||||
setmetatable(a, {__pairs = function (x)
|
||||
local tbc = setmetatable({}, {__close = function () closed = true end})
|
||||
return foo, x, 0, tbc
|
||||
end})
|
||||
|
||||
local i = 0
|
||||
for k,v in pairs(a) do
|
||||
i = i + 1
|
||||
assert(k == i and v == k+1)
|
||||
end
|
||||
assert(closed) -- 'tbc' has been closed
|
||||
|
||||
a.n = 5
|
||||
a[3] = 30
|
||||
@ -819,7 +951,7 @@ do
|
||||
co() -- start coroutine
|
||||
co(1) -- continue after yield
|
||||
assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3)
|
||||
|
||||
|
||||
end
|
||||
|
||||
print"OK"
|
||||
|
||||
@ -28,6 +28,7 @@ $NAME/literals.lua \
|
||||
$NAME/locals.lua \
|
||||
$NAME/main.lua \
|
||||
$NAME/math.lua \
|
||||
$NAME/memerr.lua \
|
||||
$NAME/nextvar.lua \
|
||||
$NAME/pm.lua \
|
||||
$NAME/sort.lua \
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
-- $Id: testes/pm.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
-- UTF-8 file
|
||||
|
||||
|
||||
print('testing pattern matching')
|
||||
|
||||
global <const> *
|
||||
|
||||
local function checkerror (msg, f, ...)
|
||||
local s, err = pcall(f, ...)
|
||||
assert(not s and string.find(err, msg))
|
||||
@ -20,9 +25,9 @@ a,b = string.find('alo', '')
|
||||
assert(a == 1 and b == 0)
|
||||
a,b = string.find('a\0o a\0o a\0o', 'a', 1) -- first position
|
||||
assert(a == 1 and b == 1)
|
||||
a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the midle
|
||||
a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the middle
|
||||
assert(a == 5 and b == 7)
|
||||
a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the midle
|
||||
a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the middle
|
||||
assert(a == 9 and b == 11)
|
||||
a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end
|
||||
assert(a == 9 and b == 11);
|
||||
@ -50,6 +55,20 @@ assert(f('aLo_ALO', '%a*') == 'aLo')
|
||||
|
||||
assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu")
|
||||
|
||||
|
||||
-- Adapt a pattern to UTF-8
|
||||
local function PU (p)
|
||||
-- distribute '?' into each individual byte of a character.
|
||||
-- (For instance, "á?" becomes "\195?\161?".)
|
||||
p = string.gsub(p, "(" .. utf8.charpattern .. ")%?", function (c)
|
||||
return string.gsub(c, ".", "%0?")
|
||||
end)
|
||||
-- change '.' to utf-8 character patterns
|
||||
p = string.gsub(p, "%.", utf8.charpattern)
|
||||
return p
|
||||
end
|
||||
|
||||
|
||||
assert(f('aaab', 'a*') == 'aaa');
|
||||
assert(f('aaa', '^.*$') == 'aaa');
|
||||
assert(f('aaa', 'b*') == '');
|
||||
@ -73,16 +92,16 @@ assert(f('aaa', '^.-$') == 'aaa')
|
||||
assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
|
||||
assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab')
|
||||
assert(f('alo xo', '.o$') == 'xo')
|
||||
assert(f(' \n isto é assim', '%S%S*') == 'isto')
|
||||
assert(f(' \n isto é assim', '%S*$') == 'assim')
|
||||
assert(f(' \n isto é assim', '[a-z]*$') == 'assim')
|
||||
assert(f(' \n isto é assim', '%S%S*') == 'isto')
|
||||
assert(f(' \n isto é assim', '%S*$') == 'assim')
|
||||
assert(f(' \n isto é assim', '[a-z]*$') == 'assim')
|
||||
assert(f('um caracter ? extra', '[^%sa-z]') == '?')
|
||||
assert(f('', 'a?') == '')
|
||||
assert(f('á', 'á?') == 'á')
|
||||
assert(f('ábl', 'á?b?l?') == 'ábl')
|
||||
assert(f(' ábl', 'á?b?l?') == '')
|
||||
assert(f('á', PU'á?') == 'á')
|
||||
assert(f('ábl', PU'á?b?l?') == 'ábl')
|
||||
assert(f(' ábl', PU'á?b?l?') == '')
|
||||
assert(f('aa', '^aa?a?a') == 'aa')
|
||||
assert(f(']]]áb', '[^]]') == 'á')
|
||||
assert(f(']]]áb', '[^]]+') == 'áb')
|
||||
assert(f("0alo alo", "%x*") == "0a")
|
||||
assert(f("alo alo", "%C+") == "alo alo")
|
||||
print('+')
|
||||
@ -136,28 +155,28 @@ assert(string.match("alo xyzK", "(%w+)K") == "xyz")
|
||||
assert(string.match("254 K", "(%d*)K") == "")
|
||||
assert(string.match("alo ", "(%w*)$") == "")
|
||||
assert(not string.match("alo ", "(%w+)$"))
|
||||
assert(string.find("(álo)", "%(á") == 1)
|
||||
local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$")
|
||||
assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil)
|
||||
assert(string.find("(álo)", "%(á") == 1)
|
||||
local a, b, c, d, e = string.match("âlo alo", PU"^(((.).). (%w*))$")
|
||||
assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil)
|
||||
a, b, c, d = string.match('0123456789', '(.+(.?)())')
|
||||
assert(a == '0123456789' and b == '' and c == 11 and d == nil)
|
||||
print('+')
|
||||
|
||||
assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
|
||||
assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
|
||||
assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
|
||||
assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
|
||||
assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim
|
||||
assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
|
||||
local t = "abç d"
|
||||
a, b = string.gsub(t, '(.)', '%1@')
|
||||
assert('@'..a == string.gsub(t, '', '@') and b == 5)
|
||||
a, b = string.gsub('abçd', '(.)', '%0@', 2)
|
||||
assert(a == 'a@b@çd' and b == 2)
|
||||
local t = "abç d"
|
||||
a, b = string.gsub(t, PU'(.)', '%1@')
|
||||
assert(a == "a@b@ç@ @d@" and b == 5)
|
||||
a, b = string.gsub('abçd', PU'(.)', '%0@', 2)
|
||||
assert(a == 'a@b@çd' and b == 2)
|
||||
assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o')
|
||||
assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") ==
|
||||
"xyz=abc-abc=xyz")
|
||||
assert(string.gsub("abc", "%w", "%1%0") == "aabbcc")
|
||||
assert(string.gsub("abc", "%w+", "%0%1") == "abcabc")
|
||||
assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú')
|
||||
assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú')
|
||||
assert(string.gsub('', '^', 'r') == 'r')
|
||||
assert(string.gsub('', '$', 'r') == 'r')
|
||||
print('+')
|
||||
@ -188,8 +207,8 @@ do
|
||||
end
|
||||
|
||||
function f(a,b) return string.gsub(a,'.',b) end
|
||||
assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
|
||||
"trocar tudo em bbbbb é alalalalalal")
|
||||
assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
|
||||
"trocar tudo em bbbbb é alalalalalal")
|
||||
|
||||
local function dostring (s) return load(s, "")() or "" end
|
||||
assert(string.gsub("alo $a='x'$ novamente $return a$",
|
||||
|
||||
@ -1,12 +1,8 @@
|
||||
-- $Id: testes/sort.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
print "testing (parts of) table library"
|
||||
|
||||
print "testing unpack"
|
||||
|
||||
local unpack = table.unpack
|
||||
|
||||
local maxI = math.maxinteger
|
||||
local minI = math.mininteger
|
||||
|
||||
@ -17,6 +13,40 @@ local function checkerror (msg, f, ...)
|
||||
end
|
||||
|
||||
|
||||
do print "testing 'table.create'"
|
||||
local N = 10000
|
||||
collectgarbage()
|
||||
local m = collectgarbage("count") * 1024
|
||||
local t = table.create(N)
|
||||
local memdiff = collectgarbage("count") * 1024 - m
|
||||
assert(memdiff > N * 4)
|
||||
for i = 1, 20 do
|
||||
assert(#t == i - 1)
|
||||
t[i] = 0
|
||||
end
|
||||
for i = 1, 20 do t[#t + 1] = i * 10 end
|
||||
assert(#t == 40 and t[39] == 190)
|
||||
assert(not T or T.querytab(t) == N)
|
||||
t = nil
|
||||
collectgarbage()
|
||||
m = collectgarbage("count") * 1024
|
||||
t = table.create(0, 1024)
|
||||
memdiff = collectgarbage("count") * 1024 - m
|
||||
assert(memdiff > 1024 * 12)
|
||||
assert(not T or select(2, T.querytab(t)) == 1024)
|
||||
|
||||
local maxint1 = 1 << (string.packsize("i") * 8 - 1)
|
||||
checkerror("out of range", table.create, maxint1)
|
||||
checkerror("out of range", table.create, 0, maxint1)
|
||||
checkerror("table overflow", table.create, 0, maxint1 - 1)
|
||||
end
|
||||
|
||||
|
||||
print "testing unpack"
|
||||
|
||||
local unpack = table.unpack
|
||||
|
||||
|
||||
checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
|
||||
|
||||
local x,y,z,a,n
|
||||
@ -169,7 +199,7 @@ do
|
||||
__index = function (_,k) pos1 = k end,
|
||||
__newindex = function (_,k) pos2 = k; error() end, })
|
||||
local st, msg = pcall(table.move, a, f, e, t)
|
||||
assert(not st and not msg and pos1 == x and pos2 == y)
|
||||
assert(not st and pos1 == x and pos2 == y)
|
||||
end
|
||||
checkmove(1, maxI, 0, 1, 0)
|
||||
checkmove(0, maxI - 1, 1, maxI - 1, maxI)
|
||||
@ -289,7 +319,7 @@ timesort(a, limit, function(x,y) return nil end, "equal")
|
||||
|
||||
for i,v in pairs(a) do assert(v == false) end
|
||||
|
||||
AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
|
||||
AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"}
|
||||
table.sort(AA)
|
||||
check(AA)
|
||||
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
-- $Id: testes/strings.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
-- ISO Latin encoding
|
||||
|
||||
global <const> *
|
||||
|
||||
print('testing strings and string library')
|
||||
|
||||
@ -106,10 +110,9 @@ assert(string.rep('teste', 0) == '')
|
||||
assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê')
|
||||
assert(string.rep('', 10) == '')
|
||||
|
||||
if string.packsize("i") == 4 then
|
||||
-- result length would be 2^31 (int overflow)
|
||||
checkerror("too large", string.rep, 'aa', (1 << 30))
|
||||
checkerror("too large", string.rep, 'a', (1 << 30), ',')
|
||||
do
|
||||
checkerror("too large", string.rep, 'aa', math.maxinteger);
|
||||
checkerror("too large", string.rep, 'a', math.maxinteger, ',')
|
||||
end
|
||||
|
||||
-- repetitions with separator
|
||||
@ -154,6 +157,12 @@ else -- compatible coercion
|
||||
assert(tostring(-1203 + 0.0) == "-1203")
|
||||
end
|
||||
|
||||
|
||||
local function topointer (s)
|
||||
return string.format("%p", s)
|
||||
end
|
||||
|
||||
|
||||
do -- tests for '%p' format
|
||||
-- not much to test, as C does not specify what '%p' does.
|
||||
-- ("The value of the pointer is converted to a sequence of printing
|
||||
@ -177,18 +186,18 @@ do -- tests for '%p' format
|
||||
|
||||
do
|
||||
local t1 = {}; local t2 = {}
|
||||
assert(string.format("%p", t1) ~= string.format("%p", t2))
|
||||
assert(topointer(t1) ~= topointer(t2))
|
||||
end
|
||||
|
||||
do -- short strings are internalized
|
||||
local s1 = string.rep("a", 10)
|
||||
local s2 = string.rep("aa", 5)
|
||||
assert(string.format("%p", s1) == string.format("%p", s2))
|
||||
assert(topointer(s1) == topointer(s2))
|
||||
end
|
||||
|
||||
do -- long strings aren't internalized
|
||||
local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
|
||||
assert(string.format("%p", s1) ~= string.format("%p", s2))
|
||||
assert(topointer(s1) ~= topointer(s2))
|
||||
end
|
||||
end
|
||||
|
||||
@ -518,6 +527,37 @@ else
|
||||
testpfs("P", str, {})
|
||||
end
|
||||
|
||||
if T == nil then
|
||||
(Message or print)('\n >>> testC not active: skipping external strings tests <<<\n')
|
||||
else
|
||||
print("testing external strings")
|
||||
local x = T.externKstr("hello") -- external fixed short string
|
||||
assert(x == "hello")
|
||||
local x = T.externstr("hello") -- external allocated short string
|
||||
assert(x == "hello")
|
||||
x = string.rep("a", 100) -- long string
|
||||
local y = T.externKstr(x) -- external fixed long string
|
||||
assert(y == x)
|
||||
local z = T.externstr(x) -- external allocated long string
|
||||
assert(z == y)
|
||||
|
||||
local e = T.externstr("") -- empty external string
|
||||
assert(e .. "x" == "x" and "x" .. e == "x")
|
||||
assert(e .. e == "" and #e == 0)
|
||||
|
||||
-- external string as the "n" key in vararg table
|
||||
local n = T.externstr("n")
|
||||
local n0 = T.externstr("n\0")
|
||||
local function aux (...t) assert(t[n0] == nil); return t[n] end
|
||||
assert(aux(10, 20, 30) == 3)
|
||||
|
||||
-- external string as mode in weak table
|
||||
local t = setmetatable({}, {__mode = T.externstr("kv")})
|
||||
t[{}] = {}
|
||||
assert(next(t))
|
||||
collectgarbage()
|
||||
assert(next(t) == nil)
|
||||
end
|
||||
|
||||
print('OK')
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
-- $Id: testes/tpack.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
local pack = string.pack
|
||||
local packsize = string.packsize
|
||||
@ -135,15 +135,15 @@ checkerror("variable%-length format", packsize, "z")
|
||||
-- overflow in option size (error will be in digit after limit)
|
||||
checkerror("invalid format", packsize, "c1" .. string.rep("0", 40))
|
||||
|
||||
if packsize("i") == 4 then
|
||||
-- result would be 2^31 (2^3 repetitions of 2^28 strings)
|
||||
local s = string.rep("c268435456", 2^3)
|
||||
checkerror("too large", packsize, s)
|
||||
-- one less is OK
|
||||
s = string.rep("c268435456", 2^3 - 1) .. "c268435455"
|
||||
assert(packsize(s) == 0x7fffffff)
|
||||
do
|
||||
local maxsize = (packsize("j") <= packsize("T")) and
|
||||
math.maxinteger or (1 << (packsize("T") * 8))
|
||||
assert (packsize(string.format("c%d", maxsize - 9)) == maxsize - 9)
|
||||
checkerror("too large", packsize, string.format("c%dc10", maxsize - 9))
|
||||
checkerror("too long", pack, string.format("xxxxxxxxxx c%d", maxsize - 9))
|
||||
end
|
||||
|
||||
|
||||
-- overflow in packing
|
||||
for i = 1, sizeLI - 1 do
|
||||
local umax = (1 << (i * 8)) - 1
|
||||
@ -229,8 +229,9 @@ do
|
||||
assert(pack("c3", "123") == "123")
|
||||
assert(pack("c0", "") == "")
|
||||
assert(pack("c8", "123456") == "123456\0\0")
|
||||
assert(pack("c88", "") == string.rep("\0", 88))
|
||||
assert(pack("c188", "ab") == "ab" .. string.rep("\0", 188 - 2))
|
||||
assert(pack("c88 c1", "", "X") == string.rep("\0", 88) .. "X")
|
||||
assert(pack("c188 c2", "ab", "X\1") ==
|
||||
"ab" .. string.rep("\0", 188 - 2) .. "X\1")
|
||||
local a, b, c = unpack("!4 z c3", "abcdefghi\0xyz")
|
||||
assert(a == "abcdefghi" and b == "xyz" and c == 14)
|
||||
checkerror("longer than", pack, "c3", "1234")
|
||||
|
||||
@ -1,12 +1,17 @@
|
||||
-- track collections
|
||||
|
||||
|
||||
local M = {}
|
||||
|
||||
-- import list
|
||||
local setmetatable, stderr, collectgarbage =
|
||||
setmetatable, io.stderr, collectgarbage
|
||||
local stderr, collectgarbage = io.stderr, collectgarbage
|
||||
|
||||
_ENV = nil
|
||||
-- the debug version of setmetatable does not create any object (such as
|
||||
-- a '__metatable' string), and so it is more appropriate to be used in
|
||||
-- a finalizer
|
||||
local setmetatable = require"debug".setmetatable
|
||||
|
||||
global none
|
||||
|
||||
local active = false
|
||||
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
-- $Id: testes/utf8.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
-- See Copyright Notice in file lua.h
|
||||
|
||||
-- UTF-8 file
|
||||
|
||||
global <const> *
|
||||
|
||||
print "testing UTF-8 library"
|
||||
|
||||
@ -50,25 +54,35 @@ local function check (s, t, nonstrict)
|
||||
for i = 1, #t do assert(t[i] == t1[i]) end -- 't' is equal to 't1'
|
||||
|
||||
for i = 1, l do -- for all codepoints
|
||||
local pi = utf8.offset(s, i) -- position of i-th char
|
||||
local pi, pie = utf8.offset(s, i) -- position of i-th char
|
||||
local pi1 = utf8.offset(s, 2, pi) -- position of next char
|
||||
assert(pi1 == pie + 1)
|
||||
assert(string.find(string.sub(s, pi, pi1 - 1), justone))
|
||||
assert(utf8.offset(s, -1, pi1) == pi)
|
||||
assert(utf8.offset(s, i - l - 1) == pi)
|
||||
assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi, pi, nonstrict)))
|
||||
for j = pi, pi1 - 1 do
|
||||
assert(utf8.offset(s, 0, j) == pi)
|
||||
local off1, off2 = utf8.offset(s, 0, j)
|
||||
assert(off1 == pi and off2 == pi1 - 1)
|
||||
end
|
||||
for j = pi + 1, pi1 - 1 do
|
||||
assert(not utf8.len(s, j))
|
||||
end
|
||||
assert(utf8.len(s, pi, pi, nonstrict) == 1)
|
||||
assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1)
|
||||
assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1)
|
||||
assert(utf8.len(s, pi1, -1, nonstrict) == l - i)
|
||||
assert(utf8.len(s, 1, pi, nonstrict) == i)
|
||||
assert(utf8.len(s, pi, pi, nonstrict) == 1)
|
||||
assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1)
|
||||
assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1)
|
||||
assert(utf8.len(s, pi1, -1, nonstrict) == l - i)
|
||||
assert(utf8.len(s, 1, pi, nonstrict) == i)
|
||||
end
|
||||
|
||||
local expected = 1 -- expected position of "current" character
|
||||
for i = 1, l + 1 do
|
||||
local p, e = utf8.offset(s, i)
|
||||
assert(p == expected)
|
||||
expected = e + 1
|
||||
end
|
||||
assert(expected - 1 == #s + 1)
|
||||
|
||||
local i = 0
|
||||
for p, c in utf8.codes(s, nonstrict) do
|
||||
i = i + 1
|
||||
@ -92,20 +106,20 @@ end
|
||||
|
||||
|
||||
do -- error indication in utf8.len
|
||||
local function check (s, p)
|
||||
local function checklen (s, p)
|
||||
local a, b = utf8.len(s)
|
||||
assert(not a and b == p)
|
||||
end
|
||||
check("abc\xE3def", 4)
|
||||
check("\xF4\x9F\xBF", 1)
|
||||
check("\xF4\x9F\xBF\xBF", 1)
|
||||
checklen("abc\xE3def", 4)
|
||||
checklen("\xF4\x9F\xBF", 1)
|
||||
checklen("\xF4\x9F\xBF\xBF", 1)
|
||||
-- spurious continuation bytes
|
||||
check("汉字\x80", #("汉字") + 1)
|
||||
check("\x80hello", 1)
|
||||
check("hel\x80lo", 4)
|
||||
check("汉字\xBF", #("汉字") + 1)
|
||||
check("\xBFhello", 1)
|
||||
check("hel\xBFlo", 4)
|
||||
checklen("汉字\x80", #("汉字") + 1)
|
||||
checklen("\x80hello", 1)
|
||||
checklen("hel\x80lo", 4)
|
||||
checklen("汉字\xBF", #("汉字") + 1)
|
||||
checklen("\xBFhello", 1)
|
||||
checklen("hel\xBFlo", 4)
|
||||
end
|
||||
|
||||
-- errors in utf8.codes
|
||||
@ -122,7 +136,7 @@ do
|
||||
errorcodes("\xbfinvalid")
|
||||
errorcodes("αλφ\xBFα")
|
||||
|
||||
-- calling interation function with invalid arguments
|
||||
-- calling iteration function with invalid arguments
|
||||
local f = utf8.codes("")
|
||||
assert(f("", 2) == nil)
|
||||
assert(f("", -1) == nil)
|
||||
@ -138,11 +152,20 @@ checkerror("position out of bounds", utf8.offset, "", 1, -1)
|
||||
checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
|
||||
checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
|
||||
checkerror("continuation byte", utf8.offset, "\x80", 1)
|
||||
checkerror("continuation byte", utf8.offset, "\x9c", -1)
|
||||
|
||||
-- error in indices for len
|
||||
checkerror("out of bounds", utf8.len, "abc", 0, 2)
|
||||
checkerror("out of bounds", utf8.len, "abc", 1, 4)
|
||||
|
||||
do -- missing continuation bytes
|
||||
-- get what is available
|
||||
local p, e = utf8.offset("\xE0", 1)
|
||||
assert(p == 1 and e == 1)
|
||||
local p, e = utf8.offset("\xE0\x9e", -1)
|
||||
assert(p == 1 and e == 2)
|
||||
end
|
||||
|
||||
|
||||
local s = "hello World"
|
||||
local t = {string.byte(s, 1, -1)}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user