mirror of
https://github.com/lua/lua
synced 2026-06-08 15:34:49 +00:00
Compare commits
415 Commits
v5.4-alpha
...
v5.4
| Author | SHA1 | Date | |
|---|---|---|---|
| 934fdd481c | |||
| 9ac9d23f41 | |||
| 1b0f943da7 | |||
| 6e22fedb74 | |||
| 267ef461d0 | |||
| d1ee2a4deb | |||
| 3fe7be956f | |||
| 983bc433e6 | |||
| 25da574fcb | |||
| f5e55be2a0 | |||
| 9f0c0fe0de | |||
| 782ef85b22 | |||
| 30982bec96 | |||
| 1ab3208a1f | |||
| 21ff8de33a | |||
| 7eb1ed21b7 | |||
| 2db966fcbf | |||
| ae9a0cbbb4 | |||
| d5212c13b0 | |||
| e0efebdbe4 | |||
| e84f7bf198 | |||
| dfbde4c7d5 | |||
| de794a6527 | |||
| 8b83417de9 | |||
| e288c5a918 | |||
| 5853c37a83 | |||
| 842a83f09c | |||
| 7923dbbf72 | |||
| 81e4fce530 | |||
| 6baee9ef9d | |||
| edd8589f47 | |||
| 07a9eab23a | |||
| 9363a8b990 | |||
| 5ab6a5756b | |||
| 9b4f39ab14 | |||
| f4211a5ea4 | |||
| 1b3f507f62 | |||
| 6b51133a98 | |||
| cbae016202 | |||
| ea39042e13 | |||
| 05ec55f16b | |||
| f623b96932 | |||
| 9be74ccc21 | |||
| 09f3c2372f | |||
| c197885cb0 | |||
| 934e77a286 | |||
| 6443185167 | |||
| e15f1f2bb7 | |||
| b5c65705ca | |||
| 7ca8105a2e | |||
| 94689ac3ad | |||
| ab859fe59b | |||
| c4b71b7ba0 | |||
| 1de2f31694 | |||
| 02bab9fc25 | |||
| 5e08b41567 | |||
| cf08915d62 | |||
| c888ae0aea | |||
| d69789da1c | |||
| 314745ed84 | |||
| 0825cf237d | |||
| f874d37fa2 | |||
| 35e01ed21d | |||
| 6aabf4b15e | |||
| fa2f294dd1 | |||
| 82fae58e25 | |||
| 9a77f57edc | |||
| be908a7d4d | |||
| 9ede317c70 | |||
| ee645472eb | |||
| 413a393e62 | |||
| ba089bcb08 | |||
| 1e64c1391f | |||
| b85816b9a8 | |||
| c954db3924 | |||
| 14d2803e55 | |||
| 7f12bf40c4 | |||
| 26be27459b | |||
| cfbe378f90 | |||
| a1089b415a | |||
| f8c4c4fcf2 | |||
| 71bc69c2af | |||
| cd56f222b7 | |||
| 69b77b6fde | |||
| 997f11f543 | |||
| 02060b7a37 | |||
| a1f77a234a | |||
| c6cea857a4 | |||
| d61b0c6028 | |||
| 196bb94d66 | |||
| 603b2c64ad | |||
| 4a00f61276 | |||
| 42d40581dd | |||
| e435aaabef | |||
| 315639d3bb | |||
| c764ca71a6 | |||
| 295cde9454 | |||
| f3cfd5bf2b | |||
| 8426d9b4d4 | |||
| 1f3c6f4534 | |||
| 25b143dd34 | |||
| 5d708c3f9c | |||
| 8dd2c912d2 | |||
| 05ac2409ee | |||
| 597a53bbc6 | |||
| 86ec152433 | |||
| cf613cdc6f | |||
| 066e0f93c4 | |||
| 0bfc572e51 | |||
| 1de95e97ef | |||
| ad3942adba | |||
| 48835c76c8 | |||
| 6b3e116d44 | |||
| e8deac5a41 | |||
| bfbff3703e | |||
| 74d99057a5 | |||
| 1fce5bea81 | |||
| 3699446aaf | |||
| 0e5071b5fb | |||
| 87a9573b2e | |||
| deac067ed3 | |||
| 2ff3471722 | |||
| 9db4bfed6b | |||
| 91673a8ec0 | |||
| 41871f1803 | |||
| a393ac2554 | |||
| 65434b4d1b | |||
| 59acd79c05 | |||
| e2c07dcbf7 | |||
| 439e45a2f6 | |||
| 62fb934427 | |||
| 8a32e0aa4a | |||
| dbdc74dc55 | |||
| 6a0dace25a | |||
| 04e19712a5 | |||
| 901d760093 | |||
| c0ed74c1e1 | |||
| fc6c74f100 | |||
| 1e6918d553 | |||
| 681297187e | |||
| 5148954eed | |||
| d205f3a484 | |||
| 47cffdc723 | |||
| 36de01d988 | |||
| 7fbe215808 | |||
| bf10593a3a | |||
| bef250eb8d | |||
| ba81adaad9 | |||
| eadd8c7178 | |||
| 014daf43cb | |||
| 05b13651f9 | |||
| 81c6021fb4 | |||
| a7b8b27dd3 | |||
| 511d53a826 | |||
| f5df7f91f7 | |||
| e7803f7dbc | |||
| b7eb21c1ef | |||
| cf23a93d82 | |||
| 9a2de786de | |||
| 5276973224 | |||
| f9d857a81b | |||
| 1537d6680b | |||
| e0260eb2d4 | |||
| 5205f073c5 | |||
| 31925e4cc2 | |||
| 59c88f846d | |||
| c03c527fd2 | |||
| 38cc7d40a4 | |||
| bc970005ce | |||
| f79ccdca9b | |||
| 4e47f81188 | |||
| c63e5d212b | |||
| dee6433a89 | |||
| 2bfa13e520 | |||
| e500892e18 | |||
| 949187b049 | |||
| 58aa09a0b9 | |||
| 1f81baffad | |||
| 0e9254dfa0 | |||
| 6ccd24eff5 | |||
| d0f34d9137 | |||
| 825ac8eca8 | |||
| b07fc10e91 | |||
| cc1692515e | |||
| ce101dcaf7 | |||
| 553b37ce4f | |||
| 4bd10b6fe8 | |||
| 59e565d955 | |||
| 6188f3a654 | |||
| 7af27ef59d | |||
| 0ceada8da9 | |||
| f9d29b0c44 | |||
| 409256b784 | |||
| b17178b27a | |||
| e1ceea5674 | |||
| 748d6d4e7a | |||
| e2ea3b31c9 | |||
| 23051e830a | |||
| f15589f3b0 | |||
| 754ca0060f | |||
| c36ced53c9 | |||
| d41c36bf67 | |||
| d9d2904f09 | |||
| 65d2294454 | |||
| 131e3fd814 | |||
| 9d067ab73b | |||
| 2f4162bc47 | |||
| ab1aca94e8 | |||
| d282652561 | |||
| 58216600eb | |||
| 94cbe46511 | |||
| 69b71a6919 | |||
| d742a193e5 | |||
| e4a38eb0e8 | |||
| f07de22576 | |||
| 52c8679760 | |||
| 849b2ecbd2 | |||
| 30528049f1 | |||
| 9a89fb1c9d | |||
| c23cc86c54 | |||
| 171dcd7d74 | |||
| 5aa36e894f | |||
| 9ecd446141 | |||
| fb172d0a92 | |||
| 0085db4596 | |||
| 490d42b5f8 | |||
| 287b302acb | |||
| 5d8ce05b3f | |||
| f83de8e34e | |||
| 9f791535cd | |||
| 51c29ec0c4 | |||
| fbaf040f5e | |||
| e51564d1be | |||
| 98ec799591 | |||
| b6888a158b | |||
| 613513d09f | |||
| 8496112a18 | |||
| 6bc0f13505 | |||
| 8a89da07ba | |||
| f849885a4b | |||
| f7ce7e5faa | |||
| 65141832d2 | |||
| f13dc59416 | |||
| 7c3cb71fa4 | |||
| 68109afcdb | |||
| 9cf3299faf | |||
| b9b554e0f6 | |||
| 0dc5deca1c | |||
| b4c353434f | |||
| 71f70df327 | |||
| 663f83f647 | |||
| ae5b5ba529 | |||
| a585eae6e7 | |||
| d2c2e32e8a | |||
| 8c7c9ea065 | |||
| a6da1472c0 | |||
| 34affe7a63 | |||
| a2195644d8 | |||
| 1ecfbfa1a1 | |||
| e1d8770f12 | |||
| 0f1cd0eba9 | |||
| 127e7a6c89 | |||
| 6f5bd5072d | |||
| 56ec432281 | |||
| 31b8c2d438 | |||
| eb41999461 | |||
| 314c6057b7 | |||
| d39ea8b3ce | |||
| 6298903e35 | |||
| b57574d6fb | |||
| bfcf06d91a | |||
| 0280407fc5 | |||
| ae809e9fd1 | |||
| e96385aded | |||
| 56a165bf0f | |||
| 422ce50d2e | |||
| c33b1728ae | |||
| a304199836 | |||
| 6d7cd31fee | |||
| 993c58fde3 | |||
| d49b288728 | |||
| 69e84805e4 | |||
| 364e569945 | |||
| 63295f1f7f | |||
| 50523b107d | |||
| aa8d4a782d | |||
| efcf24be0c | |||
| 17dbaa8639 | |||
| 9514abc2da | |||
| 0be57b9b6d | |||
| 61a4e64a66 | |||
| 9a6f47f0ed | |||
| 948fb628d9 | |||
| a901c505ab | |||
| 0ddc0f47bd | |||
| cac075a122 | |||
| 9e0a8475cd | |||
| 7ccc6d8290 | |||
| 7288528a1e | |||
| 513559cc47 | |||
| e460752323 | |||
| 92594f0939 | |||
| e8a52281d9 | |||
| 6eb53b7526 | |||
| 9b7987a9d1 | |||
| 28ef7061bb | |||
| 46c3587a6f | |||
| 69c7139ff8 | |||
| 5ff408d218 | |||
| e3c83835e7 | |||
| bd1b87c579 | |||
| d7bb8df841 | |||
| c646e57fd6 | |||
| e0ab13c62f | |||
| e0cbaa50fa | |||
| 3747487320 | |||
| 1e0ad018ce | |||
| 95735bda46 | |||
| d30569c064 | |||
| 2d92102dee | |||
| 490ecfcaa1 | |||
| e174f43807 | |||
| 81f2401c6d | |||
| 508a705c1c | |||
| 6f1c033d72 | |||
| 5f83fb6582 | |||
| 679dc72c08 | |||
| 1499680f9e | |||
| bdcfae2e1c | |||
| 7d526e75a7 | |||
| c12983cf8a | |||
| ba9cd0d25a | |||
| b93f3b00bb | |||
| 4c32d9300c | |||
| 6e285e5392 | |||
| b8cdea0190 | |||
| e592f94a64 | |||
| 6e1aec7a67 | |||
| 6055a039b5 | |||
| 6c0e44464b | |||
| 6a84c32900 | |||
| 6a10f03ff8 | |||
| 7bd1e53753 | |||
| b98d41db99 | |||
| b2a580bdb1 | |||
| 89f6a85f03 | |||
| 6ef366644f | |||
| 03cde80b58 | |||
| 6b2e202df5 | |||
| 40d8832ee0 | |||
| 91dad09f65 | |||
| 4518e5df24 | |||
| 72a094bda7 | |||
| 46b84580d6 | |||
| df13f25948 | |||
| 643188d6e5 | |||
| 3df5624ff4 | |||
| be78aeae4c | |||
| 5bc47fe830 | |||
| 9405472565 | |||
| 45948e7e55 | |||
| b96b0b5abb | |||
| ca13be9af7 | |||
| a1d8eb2743 | |||
| f64a1b175a | |||
| 09b4e527a0 | |||
| 35a28a58b3 | |||
| 223bb04090 | |||
| fe040633a1 | |||
| f645d31573 | |||
| 35b4efc270 | |||
| 0d52913804 | |||
| b80077b8f3 | |||
| e70f275f32 | |||
| 9a37dc0ce6 | |||
| 0eb6aa4013 | |||
| 7f5c31cdca | |||
| 9e6807c3c9 | |||
| 2f22c6bb79 | |||
| 3c0d3c6fbe | |||
| 440a5ee78c | |||
| dc07719b0d | |||
| 9cdf6b7082 | |||
| 3c1d415bd3 | |||
| d36a31e673 | |||
| 024a6071ca | |||
| 4eefef07ab | |||
| 9c28ed05c9 | |||
| 8082906c05 | |||
| d6af81084d | |||
| 4846f7e3bb | |||
| c220b0a5d0 | |||
| 298f383ffc | |||
| 758c1ef445 | |||
| dd6d8db49a | |||
| 1fb4d53925 | |||
| f6aab3ec1f | |||
| be8445d7e4 | |||
| 3d296304ef | |||
| 54f7b46c1e | |||
| e888976bc6 | |||
| 4d46289331 | |||
| 8eca21c2e8 | |||
| 924bed7297 | |||
| 8b7cfee26b | |||
| c1a63c45f8 | |||
| 4487c28ced | |||
| 05ba288049 | |||
| 6b9490bd72 | |||
| e4b02ca8e4 | |||
| 20a9853e02 | |||
| 1d70708a78 | |||
| be73f72fcc | |||
| 3cd9b56ae6 | |||
| d2a9b4ffb8 |
+15
@@ -0,0 +1,15 @@
|
|||||||
|
.gitattributes
|
||||||
|
|
||||||
|
*.so
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
|
||||||
|
manual/manual.html
|
||||||
|
|
||||||
|
testes/time.txt
|
||||||
|
testes/time-debug.txt
|
||||||
|
|
||||||
|
testes/libs/all
|
||||||
|
|
||||||
|
temp
|
||||||
|
lua
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# Lua
|
||||||
|
|
||||||
|
This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/).
|
||||||
|
|
||||||
|
Please **do not** send pull requests. To report issues, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html).
|
||||||
|
|
||||||
|
Download official Lua releases from [Lua.org](https://www.lua.org/download.html).
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
make -s -j
|
make -s -j
|
||||||
cd testes/libs; make -s
|
cd testes/libs; make -s
|
||||||
cd .. # back to directory 'testes'
|
cd .. # back to directory 'testes'
|
||||||
ulimit -S -s 2000
|
ulimit -S -s 1100
|
||||||
if { ../lua all.lua; } then
|
if { ../lua -W all.lua; } then
|
||||||
echo -e "\n\n final OK!!!!\n\n"
|
echo -e "\n\n final OK!!!!\n\n"
|
||||||
else
|
else
|
||||||
echo -e "\n\n >>>> BUG!!!!\n\n"
|
echo -e "\n\n >>>> BUG!!!!\n\n"
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ const char lua_ident[] =
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Test for a valid index.
|
** Test for a valid index (one that is not the 'nilvalue').
|
||||||
** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
|
** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
|
||||||
** However, it covers the most common cases in a faster way.
|
** However, it covers the most common cases in a faster way.
|
||||||
*/
|
*/
|
||||||
@@ -53,64 +53,73 @@ const char lua_ident[] =
|
|||||||
#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
|
#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Convert an acceptable index to a pointer to its respective value.
|
||||||
|
** Non-valid indices return the special nil value 'G(L)->nilvalue'.
|
||||||
|
*/
|
||||||
static TValue *index2value (lua_State *L, int idx) {
|
static TValue *index2value (lua_State *L, int idx) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
if (idx > 0) {
|
if (idx > 0) {
|
||||||
StkId o = ci->func + idx;
|
StkId o = ci->func.p + idx;
|
||||||
api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index");
|
api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
|
||||||
if (o >= L->top) return &G(L)->nilvalue;
|
if (o >= L->top.p) return &G(L)->nilvalue;
|
||||||
else return s2v(o);
|
else return s2v(o);
|
||||||
}
|
}
|
||||||
else if (!ispseudo(idx)) { /* negative index */
|
else if (!ispseudo(idx)) { /* negative index */
|
||||||
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
|
||||||
return s2v(L->top + idx);
|
"invalid index");
|
||||||
|
return s2v(L->top.p + idx);
|
||||||
}
|
}
|
||||||
else if (idx == LUA_REGISTRYINDEX)
|
else if (idx == LUA_REGISTRYINDEX)
|
||||||
return &G(L)->l_registry;
|
return &G(L)->l_registry;
|
||||||
else { /* upvalues */
|
else { /* upvalues */
|
||||||
idx = LUA_REGISTRYINDEX - idx;
|
idx = LUA_REGISTRYINDEX - idx;
|
||||||
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
|
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
|
||||||
if (ttislcf(s2v(ci->func))) /* light C function? */
|
if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */
|
||||||
return &G(L)->nilvalue; /* it has no upvalues */
|
CClosure *func = clCvalue(s2v(ci->func.p));
|
||||||
else {
|
return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
|
||||||
CClosure *func = clCvalue(s2v(ci->func));
|
: &G(L)->nilvalue;
|
||||||
return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue;
|
}
|
||||||
|
else { /* light C function or Lua function (through a hook)?) */
|
||||||
|
api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function");
|
||||||
|
return &G(L)->nilvalue; /* no upvalues */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static StkId index2stack (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) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
if (idx > 0) {
|
if (idx > 0) {
|
||||||
StkId o = ci->func + idx;
|
StkId o = ci->func.p + idx;
|
||||||
api_check(L, o < L->top, "unacceptable index");
|
api_check(L, o < L->top.p, "invalid index");
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
else { /* non-positive index */
|
else { /* non-positive index */
|
||||||
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
|
||||||
|
"invalid index");
|
||||||
api_check(L, !ispseudo(idx), "invalid index");
|
api_check(L, !ispseudo(idx), "invalid index");
|
||||||
return L->top + idx;
|
return L->top.p + idx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_checkstack (lua_State *L, int n) {
|
LUA_API int lua_checkstack (lua_State *L, int n) {
|
||||||
int res;
|
int res;
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
|
ci = L->ci;
|
||||||
api_check(L, n >= 0, "negative 'n'");
|
api_check(L, n >= 0, "negative 'n'");
|
||||||
if (L->stack_last - L->top > n) /* stack large enough? */
|
if (L->stack_last.p - L->top.p > n) /* stack large enough? */
|
||||||
res = 1; /* yes; check is OK */
|
res = 1; /* yes; check is OK */
|
||||||
else { /* no; need to grow stack */
|
else /* need to grow stack */
|
||||||
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
|
res = luaD_growstack(L, n, 0);
|
||||||
if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
|
if (res && ci->top.p < L->top.p + n)
|
||||||
res = 0; /* no */
|
ci->top.p = L->top.p + n; /* adjust frame top */
|
||||||
else /* try to grow stack */
|
|
||||||
res = luaD_growstack(L, n, 0);
|
|
||||||
}
|
|
||||||
if (res && ci->top < L->top + n)
|
|
||||||
ci->top = L->top + n; /* adjust frame top */
|
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -122,11 +131,11 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
|
|||||||
lua_lock(to);
|
lua_lock(to);
|
||||||
api_checknelems(from, n);
|
api_checknelems(from, n);
|
||||||
api_check(from, G(from) == G(to), "moving among independent states");
|
api_check(from, G(from) == G(to), "moving among independent states");
|
||||||
api_check(from, to->ci->top - to->top >= n, "stack overflow");
|
api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
|
||||||
from->top -= n;
|
from->top.p -= n;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
setobjs2s(to, to->top, from->top + i);
|
setobjs2s(to, to->top.p, from->top.p + i);
|
||||||
to->top++; /* stack already checked by previous 'api_check' */
|
to->top.p++; /* stack already checked by previous 'api_check' */
|
||||||
}
|
}
|
||||||
lua_unlock(to);
|
lua_unlock(to);
|
||||||
}
|
}
|
||||||
@@ -160,30 +169,51 @@ LUA_API lua_Number lua_version (lua_State *L) {
|
|||||||
LUA_API int lua_absindex (lua_State *L, int idx) {
|
LUA_API int lua_absindex (lua_State *L, int idx) {
|
||||||
return (idx > 0 || ispseudo(idx))
|
return (idx > 0 || ispseudo(idx))
|
||||||
? idx
|
? idx
|
||||||
: cast_int(L->top - L->ci->func) + idx;
|
: cast_int(L->top.p - L->ci->func.p) + idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_gettop (lua_State *L) {
|
LUA_API int lua_gettop (lua_State *L) {
|
||||||
return cast_int(L->top - (L->ci->func + 1));
|
return cast_int(L->top.p - (L->ci->func.p + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API void lua_settop (lua_State *L, int idx) {
|
LUA_API void lua_settop (lua_State *L, int idx) {
|
||||||
StkId func = L->ci->func;
|
CallInfo *ci;
|
||||||
|
StkId func, newtop;
|
||||||
|
ptrdiff_t diff; /* difference for new top */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
|
ci = L->ci;
|
||||||
|
func = ci->func.p;
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
StkId newtop = (func + 1) + idx;
|
api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
|
||||||
api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
|
diff = ((func + 1) + idx) - L->top.p;
|
||||||
while (L->top < newtop)
|
for (; diff > 0; diff--)
|
||||||
setnilvalue(s2v(L->top++));
|
setnilvalue(s2v(L->top.p++)); /* clear new slots */
|
||||||
L->top = newtop;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
|
||||||
L->top += idx+1; /* 'subtract' index (index is negative) */
|
diff = idx + 1; /* will "subtract" index (as it is negative) */
|
||||||
}
|
}
|
||||||
luaF_close(L, L->top, LUA_OK);
|
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));
|
||||||
|
newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
|
||||||
|
}
|
||||||
|
L->top.p = newtop; /* correct top only after closing any upvalue */
|
||||||
|
lua_unlock(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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,
|
||||||
|
"no variable to close at given level");
|
||||||
|
level = luaF_close(L, level, CLOSEKTOP, 0);
|
||||||
|
setnilvalue(s2v(level));
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +224,7 @@ LUA_API void lua_settop (lua_State *L, int idx) {
|
|||||||
** Note that we move(copy) only the value inside the stack.
|
** Note that we move(copy) only the value inside the stack.
|
||||||
** (We do not move additional fields that may exist.)
|
** (We do not move additional fields that may exist.)
|
||||||
*/
|
*/
|
||||||
static void reverse (lua_State *L, StkId from, StkId to) {
|
l_sinline void reverse (lua_State *L, StkId from, StkId to) {
|
||||||
for (; from < to; from++, to--) {
|
for (; from < to; from++, to--) {
|
||||||
TValue temp;
|
TValue temp;
|
||||||
setobj(L, &temp, s2v(from));
|
setobj(L, &temp, s2v(from));
|
||||||
@@ -211,7 +241,7 @@ static void reverse (lua_State *L, StkId from, StkId to) {
|
|||||||
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
||||||
StkId p, t, m;
|
StkId p, t, m;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = L->top - 1; /* end of stack segment being rotated */
|
t = L->top.p - 1; /* end of stack segment being rotated */
|
||||||
p = index2stack(L, idx); /* start of segment */
|
p = index2stack(L, idx); /* start of segment */
|
||||||
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
||||||
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
||||||
@@ -227,10 +257,10 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
fr = index2value(L, fromidx);
|
fr = index2value(L, fromidx);
|
||||||
to = index2value(L, toidx);
|
to = index2value(L, toidx);
|
||||||
api_check(l, isvalid(L, to), "invalid index");
|
api_check(L, isvalid(L, to), "invalid index");
|
||||||
setobj(L, to, fr);
|
setobj(L, to, fr);
|
||||||
if (isupvalue(toidx)) /* function upvalue? */
|
if (isupvalue(toidx)) /* function upvalue? */
|
||||||
luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
|
luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr);
|
||||||
/* LUA_REGISTRYINDEX does not need gc barrier
|
/* LUA_REGISTRYINDEX does not need gc barrier
|
||||||
(collector revisits it before finishing collection) */
|
(collector revisits it before finishing collection) */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -239,7 +269,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
|||||||
|
|
||||||
LUA_API void lua_pushvalue (lua_State *L, int idx) {
|
LUA_API void lua_pushvalue (lua_State *L, int idx) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
setobj2s(L, L->top, index2value(L, idx));
|
setobj2s(L, L->top.p, index2value(L, idx));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -259,7 +289,7 @@ LUA_API int lua_type (lua_State *L, int idx) {
|
|||||||
|
|
||||||
LUA_API const char *lua_typename (lua_State *L, int t) {
|
LUA_API const char *lua_typename (lua_State *L, int t) {
|
||||||
UNUSED(L);
|
UNUSED(L);
|
||||||
api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
|
api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
|
||||||
return ttypename(t);
|
return ttypename(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,12 +338,12 @@ LUA_API void lua_arith (lua_State *L, int op) {
|
|||||||
api_checknelems(L, 2); /* all other operations expect two operands */
|
api_checknelems(L, 2); /* all other operations expect two operands */
|
||||||
else { /* for unary operations, add fake 2nd operand */
|
else { /* for unary operations, add fake 2nd operand */
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
setobjs2s(L, L->top, L->top - 1);
|
setobjs2s(L, L->top.p, L->top.p - 1);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
/* first operand at top - 2, second at top - 1; result go to top - 2 */
|
/* first operand at top - 2, second at top - 1; result go to top - 2 */
|
||||||
luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2);
|
luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
|
||||||
L->top--; /* remove second operand */
|
L->top.p--; /* remove second operand */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,7 +369,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
||||||
size_t sz = luaO_str2num(s, s2v(L->top));
|
size_t sz = luaO_str2num(s, s2v(L->top.p));
|
||||||
if (sz != 0)
|
if (sz != 0)
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
return sz;
|
return sz;
|
||||||
@@ -347,23 +377,21 @@ LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
|
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
|
||||||
lua_Number n;
|
lua_Number n = 0;
|
||||||
const TValue *o = index2value(L, idx);
|
const TValue *o = index2value(L, idx);
|
||||||
int isnum = tonumber(o, &n);
|
int isnum = tonumber(o, &n);
|
||||||
if (!isnum)
|
if (pisnum)
|
||||||
n = 0; /* call to 'tonumber' may change 'n' even if it fails */
|
*pisnum = isnum;
|
||||||
if (pisnum) *pisnum = isnum;
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
|
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
|
||||||
lua_Integer res;
|
lua_Integer res = 0;
|
||||||
const TValue *o = index2value(L, idx);
|
const TValue *o = index2value(L, idx);
|
||||||
int isnum = tointeger(o, &res);
|
int isnum = tointeger(o, &res);
|
||||||
if (!isnum)
|
if (pisnum)
|
||||||
res = 0; /* call to 'tointeger' may change 'n' even if it fails */
|
*pisnum = isnum;
|
||||||
if (pisnum) *pisnum = isnum;
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,31 +403,33 @@ LUA_API int lua_toboolean (lua_State *L, int idx) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
||||||
TValue *o = index2value(L, idx);
|
TValue *o;
|
||||||
|
lua_lock(L);
|
||||||
|
o = index2value(L, idx);
|
||||||
if (!ttisstring(o)) {
|
if (!ttisstring(o)) {
|
||||||
if (!cvt2str(o)) { /* not convertible? */
|
if (!cvt2str(o)) { /* not convertible? */
|
||||||
if (len != NULL) *len = 0;
|
if (len != NULL) *len = 0;
|
||||||
|
lua_unlock(L);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
lua_lock(L); /* 'luaO_tostring' may create a new string */
|
|
||||||
luaO_tostring(L, o);
|
luaO_tostring(L, o);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
o = index2value(L, idx); /* previous call may reallocate the stack */
|
o = index2value(L, idx); /* previous call may reallocate the stack */
|
||||||
lua_unlock(L);
|
|
||||||
}
|
}
|
||||||
if (len != NULL)
|
if (len != NULL)
|
||||||
*len = vslen(o);
|
*len = tsslen(tsvalue(o));
|
||||||
return svalue(o);
|
lua_unlock(L);
|
||||||
|
return getstr(tsvalue(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
|
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
|
||||||
const TValue *o = index2value(L, idx);
|
const TValue *o = index2value(L, idx);
|
||||||
switch (ttypetag(o)) {
|
switch (ttypetag(o)) {
|
||||||
case LUA_TSHRSTR: return tsvalue(o)->shrlen;
|
case LUA_VSHRSTR: return tsvalue(o)->shrlen;
|
||||||
case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
|
case LUA_VLNGSTR: return tsvalue(o)->u.lnglen;
|
||||||
case LUA_TUSERDATA: return uvalue(o)->len;
|
case LUA_VUSERDATA: return uvalue(o)->len;
|
||||||
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
case LUA_VTABLE: return luaH_getn(hvalue(o));
|
||||||
default: return 0;
|
default: return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -414,7 +444,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void *touserdata (const TValue *o) {
|
l_sinline void *touserdata (const TValue *o) {
|
||||||
switch (ttype(o)) {
|
switch (ttype(o)) {
|
||||||
case LUA_TUSERDATA: return getudatamem(uvalue(o));
|
case LUA_TUSERDATA: return getudatamem(uvalue(o));
|
||||||
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
||||||
@@ -445,8 +475,8 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
|
|||||||
LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
||||||
const TValue *o = index2value(L, idx);
|
const TValue *o = index2value(L, idx);
|
||||||
switch (ttypetag(o)) {
|
switch (ttypetag(o)) {
|
||||||
case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o)));
|
case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
|
||||||
case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA:
|
case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
|
||||||
return touserdata(o);
|
return touserdata(o);
|
||||||
default: {
|
default: {
|
||||||
if (iscollectable(o))
|
if (iscollectable(o))
|
||||||
@@ -466,7 +496,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
|||||||
|
|
||||||
LUA_API void lua_pushnil (lua_State *L) {
|
LUA_API void lua_pushnil (lua_State *L) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
setnilvalue(s2v(L->top));
|
setnilvalue(s2v(L->top.p));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -474,7 +504,7 @@ LUA_API void lua_pushnil (lua_State *L) {
|
|||||||
|
|
||||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
setfltvalue(s2v(L->top), n);
|
setfltvalue(s2v(L->top.p), n);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -482,7 +512,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
|||||||
|
|
||||||
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
|
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
setivalue(s2v(L->top), n);
|
setivalue(s2v(L->top.p), n);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -497,7 +527,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
|||||||
TString *ts;
|
TString *ts;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
|
ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top.p, ts);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -508,11 +538,11 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
|||||||
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
setnilvalue(s2v(L->top));
|
setnilvalue(s2v(L->top.p));
|
||||||
else {
|
else {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
ts = luaS_new(L, s);
|
ts = luaS_new(L, s);
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top.p, ts);
|
||||||
s = getstr(ts); /* internal copy's address */
|
s = getstr(ts); /* internal copy's address */
|
||||||
}
|
}
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
@@ -549,7 +579,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
|
|||||||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
setfvalue(s2v(L->top), fn);
|
setfvalue(s2v(L->top.p), fn);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -558,12 +588,13 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
|||||||
api_check(L, n <= MAXUPVAL, "upvalue index too large");
|
api_check(L, n <= MAXUPVAL, "upvalue index too large");
|
||||||
cl = luaF_newCclosure(L, n);
|
cl = luaF_newCclosure(L, n);
|
||||||
cl->f = fn;
|
cl->f = fn;
|
||||||
L->top -= n;
|
L->top.p -= n;
|
||||||
while (n--) {
|
while (n--) {
|
||||||
setobj2n(L, &cl->upvalue[n], s2v(L->top + n));
|
setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n));
|
||||||
/* does not need barrier because closure is white */
|
/* does not need barrier because closure is white */
|
||||||
|
lua_assert(iswhite(cl));
|
||||||
}
|
}
|
||||||
setclCvalue(L, s2v(L->top), cl);
|
setclCvalue(L, s2v(L->top.p), cl);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
}
|
}
|
||||||
@@ -573,7 +604,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
|||||||
|
|
||||||
LUA_API void lua_pushboolean (lua_State *L, int b) {
|
LUA_API void lua_pushboolean (lua_State *L, int b) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */
|
if (b)
|
||||||
|
setbtvalue(s2v(L->top.p));
|
||||||
|
else
|
||||||
|
setbfvalue(s2v(L->top.p));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -581,7 +615,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) {
|
|||||||
|
|
||||||
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
|
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
setpvalue(s2v(L->top), p);
|
setpvalue(s2v(L->top.p), p);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -589,7 +623,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
|
|||||||
|
|
||||||
LUA_API int lua_pushthread (lua_State *L) {
|
LUA_API int lua_pushthread (lua_State *L) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
setthvalue(L, s2v(L->top), L);
|
setthvalue(L, s2v(L->top.p), L);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return (G(L)->mainthread == L);
|
return (G(L)->mainthread == L);
|
||||||
@@ -602,27 +636,38 @@ LUA_API int lua_pushthread (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
||||||
const TValue *slot;
|
const TValue *slot;
|
||||||
TString *str = luaS_new(L, k);
|
TString *str = luaS_new(L, k);
|
||||||
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
||||||
setobj2s(L, L->top, slot);
|
setobj2s(L, L->top.p, slot);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
setsvalue2s(L, L->top, str);
|
setsvalue2s(L, L->top.p, str);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
|
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttype(s2v(L->top - 1));
|
return ttype(s2v(L->top.p - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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.
|
||||||
|
*/
|
||||||
|
#define getGtable(L) \
|
||||||
|
(&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
||||||
Table *reg = hvalue(&G(L)->l_registry);
|
const TValue *G;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
|
G = getGtable(L);
|
||||||
|
return auxgetstr(L, G, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -631,13 +676,13 @@ LUA_API int lua_gettable (lua_State *L, int idx) {
|
|||||||
TValue *t;
|
TValue *t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2value(L, idx);
|
t = index2value(L, idx);
|
||||||
if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) {
|
if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
|
||||||
setobj2s(L, L->top - 1, slot);
|
setobj2s(L, L->top.p - 1, slot);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
|
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttype(s2v(L->top - 1));
|
return ttype(s2v(L->top.p - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -653,27 +698,27 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2value(L, idx);
|
t = index2value(L, idx);
|
||||||
if (luaV_fastgeti(L, t, n, slot)) {
|
if (luaV_fastgeti(L, t, n, slot)) {
|
||||||
setobj2s(L, L->top, slot);
|
setobj2s(L, L->top.p, slot);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
TValue aux;
|
TValue aux;
|
||||||
setivalue(&aux, n);
|
setivalue(&aux, n);
|
||||||
luaV_finishget(L, t, &aux, L->top, slot);
|
luaV_finishget(L, t, &aux, L->top.p, slot);
|
||||||
}
|
}
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttype(s2v(L->top - 1));
|
return ttype(s2v(L->top.p - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int finishrawget (lua_State *L, const TValue *val) {
|
l_sinline int finishrawget (lua_State *L, const TValue *val) {
|
||||||
if (isempty(val)) /* avoid copying empty items to the stack */
|
if (isempty(val)) /* avoid copying empty items to the stack */
|
||||||
setnilvalue(s2v(L->top));
|
setnilvalue(s2v(L->top.p));
|
||||||
else
|
else
|
||||||
setobj2s(L, L->top, val);
|
setobj2s(L, L->top.p, val);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttype(s2v(L->top - 1));
|
return ttype(s2v(L->top.p - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -690,8 +735,8 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
t = gettable(L, idx);
|
t = gettable(L, idx);
|
||||||
val = luaH_get(t, s2v(L->top - 1));
|
val = luaH_get(t, s2v(L->top.p - 1));
|
||||||
L->top--; /* remove key */
|
L->top.p--; /* remove key */
|
||||||
return finishrawget(L, val);
|
return finishrawget(L, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -718,7 +763,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
|||||||
Table *t;
|
Table *t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = luaH_new(L);
|
t = luaH_new(L);
|
||||||
sethvalue2s(L, L->top, t);
|
sethvalue2s(L, L->top.p, t);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
if (narray > 0 || nrec > 0)
|
if (narray > 0 || nrec > 0)
|
||||||
luaH_resize(L, t, narray, nrec);
|
luaH_resize(L, t, narray, nrec);
|
||||||
@@ -745,7 +790,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (mt != NULL) {
|
if (mt != NULL) {
|
||||||
sethvalue2s(L, L->top, mt);
|
sethvalue2s(L, L->top.p, mt);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
res = 1;
|
res = 1;
|
||||||
}
|
}
|
||||||
@@ -761,12 +806,12 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
|
|||||||
o = index2value(L, idx);
|
o = index2value(L, idx);
|
||||||
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
||||||
if (n <= 0 || n > uvalue(o)->nuvalue) {
|
if (n <= 0 || n > uvalue(o)->nuvalue) {
|
||||||
setnilvalue(s2v(L->top));
|
setnilvalue(s2v(L->top.p));
|
||||||
t = LUA_TNONE;
|
t = LUA_TNONE;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv);
|
setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
|
||||||
t = ttype(s2v(L->top));
|
t = ttype(s2v(L->top.p));
|
||||||
}
|
}
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -786,23 +831,24 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
|
|||||||
TString *str = luaS_new(L, k);
|
TString *str = luaS_new(L, k);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
||||||
luaV_finishfastset(L, t, slot, s2v(L->top - 1));
|
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||||
L->top--; /* pop value */
|
L->top.p--; /* pop value */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
|
setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
|
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
|
||||||
L->top -= 2; /* pop value and key */
|
L->top.p -= 2; /* pop value and key */
|
||||||
}
|
}
|
||||||
lua_unlock(L); /* lock done by caller */
|
lua_unlock(L); /* lock done by caller */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
||||||
Table *reg = hvalue(&G(L)->l_registry);
|
const TValue *G;
|
||||||
lua_lock(L); /* unlock done in 'auxsetstr' */
|
lua_lock(L); /* unlock done in 'auxsetstr' */
|
||||||
auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
|
G = getGtable(L);
|
||||||
|
auxsetstr(L, G, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -812,12 +858,12 @@ LUA_API void lua_settable (lua_State *L, int idx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 2);
|
api_checknelems(L, 2);
|
||||||
t = index2value(L, idx);
|
t = index2value(L, idx);
|
||||||
if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
|
if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
|
||||||
luaV_finishfastset(L, t, slot, s2v(L->top - 1));
|
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
|
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
|
||||||
L->top -= 2; /* pop index and value */
|
L->top.p -= 2; /* pop index and value */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -835,30 +881,40 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
t = index2value(L, idx);
|
t = index2value(L, idx);
|
||||||
if (luaV_fastgeti(L, t, n, slot)) {
|
if (luaV_fastgeti(L, t, n, slot)) {
|
||||||
luaV_finishfastset(L, t, slot, s2v(L->top - 1));
|
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
TValue aux;
|
TValue aux;
|
||||||
setivalue(&aux, n);
|
setivalue(&aux, n);
|
||||||
luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
|
luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
|
||||||
}
|
}
|
||||||
L->top--; /* pop value */
|
L->top.p--; /* pop value */
|
||||||
|
lua_unlock(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
|
||||||
|
Table *t;
|
||||||
|
lua_lock(L);
|
||||||
|
api_checknelems(L, n);
|
||||||
|
t = gettable(L, idx);
|
||||||
|
luaH_set(L, t, key, s2v(L->top.p - 1));
|
||||||
|
invalidateTMcache(t);
|
||||||
|
luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
|
||||||
|
L->top.p -= n;
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API void lua_rawset (lua_State *L, int idx) {
|
LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||||
Table *t;
|
aux_rawset(L, idx, s2v(L->top.p - 2), 2);
|
||||||
TValue *slot;
|
}
|
||||||
lua_lock(L);
|
|
||||||
api_checknelems(L, 2);
|
|
||||||
t = gettable(L, idx);
|
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
||||||
slot = luaH_set(L, t, s2v(L->top - 2));
|
TValue k;
|
||||||
setobj2t(L, slot, s2v(L->top - 1));
|
setpvalue(&k, cast_voidp(p));
|
||||||
invalidateTMcache(t);
|
aux_rawset(L, idx, &k, 1);
|
||||||
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
|
|
||||||
L->top -= 2;
|
|
||||||
lua_unlock(L);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -867,24 +923,9 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
t = gettable(L, idx);
|
t = gettable(L, idx);
|
||||||
luaH_setint(L, t, n, s2v(L->top - 1));
|
luaH_setint(L, t, n, s2v(L->top.p - 1));
|
||||||
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
|
luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
|
||||||
L->top--;
|
L->top.p--;
|
||||||
lua_unlock(L);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
|
||||||
Table *t;
|
|
||||||
TValue k, *slot;
|
|
||||||
lua_lock(L);
|
|
||||||
api_checknelems(L, 1);
|
|
||||||
t = gettable(L, idx);
|
|
||||||
setpvalue(&k, cast_voidp(p));
|
|
||||||
slot = luaH_set(L, t, &k);
|
|
||||||
setobj2t(L, slot, s2v(L->top - 1));
|
|
||||||
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
|
|
||||||
L->top--;
|
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -895,11 +936,11 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
obj = index2value(L, objindex);
|
obj = index2value(L, objindex);
|
||||||
if (ttisnil(s2v(L->top - 1)))
|
if (ttisnil(s2v(L->top.p - 1)))
|
||||||
mt = NULL;
|
mt = NULL;
|
||||||
else {
|
else {
|
||||||
api_check(L, ttistable(s2v(L->top - 1)), "table expected");
|
api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
|
||||||
mt = hvalue(s2v(L->top - 1));
|
mt = hvalue(s2v(L->top.p - 1));
|
||||||
}
|
}
|
||||||
switch (ttype(obj)) {
|
switch (ttype(obj)) {
|
||||||
case LUA_TTABLE: {
|
case LUA_TTABLE: {
|
||||||
@@ -923,7 +964,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
L->top--;
|
L->top.p--;
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -939,11 +980,11 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
|
|||||||
if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
|
if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
|
||||||
res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
|
res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
|
||||||
else {
|
else {
|
||||||
setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1));
|
setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
|
||||||
luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
|
luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
|
||||||
res = 1;
|
res = 1;
|
||||||
}
|
}
|
||||||
L->top--;
|
L->top.p--;
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -955,7 +996,8 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
|
|||||||
|
|
||||||
|
|
||||||
#define checkresults(L,na,nr) \
|
#define checkresults(L,na,nr) \
|
||||||
api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
|
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")
|
||||||
|
|
||||||
|
|
||||||
@@ -968,7 +1010,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
|||||||
api_checknelems(L, nargs+1);
|
api_checknelems(L, nargs+1);
|
||||||
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
||||||
checkresults(L, nargs, nresults);
|
checkresults(L, nargs, nresults);
|
||||||
func = L->top - (nargs+1);
|
func = L->top.p - (nargs+1);
|
||||||
if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
|
if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
|
||||||
L->ci->u.c.k = k; /* save continuation */
|
L->ci->u.c.k = k; /* save continuation */
|
||||||
L->ci->u.c.ctx = ctx; /* save context */
|
L->ci->u.c.ctx = ctx; /* save context */
|
||||||
@@ -1016,7 +1058,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
|||||||
api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
|
api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
|
||||||
func = savestack(L, o);
|
func = savestack(L, o);
|
||||||
}
|
}
|
||||||
c.func = L->top - (nargs+1); /* function to be called */
|
c.func = L->top.p - (nargs+1); /* function to be called */
|
||||||
if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
|
if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
|
||||||
c.nresults = nresults; /* do a 'conventional' protected call */
|
c.nresults = nresults; /* do a 'conventional' protected call */
|
||||||
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
|
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
|
||||||
@@ -1051,13 +1093,12 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
|||||||
luaZ_init(L, &z, reader, data);
|
luaZ_init(L, &z, reader, data);
|
||||||
status = luaD_protectedparser(L, &z, chunkname, mode);
|
status = luaD_protectedparser(L, &z, chunkname, mode);
|
||||||
if (status == LUA_OK) { /* no errors? */
|
if (status == LUA_OK) { /* no errors? */
|
||||||
LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
|
LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
|
||||||
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
||||||
/* get global table from registry */
|
/* get global table from registry */
|
||||||
Table *reg = hvalue(&G(L)->l_registry);
|
const TValue *gt = getGtable(L);
|
||||||
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
|
||||||
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
||||||
setobj(L, f->upvals[0]->v, gt);
|
setobj(L, f->upvals[0]->v.p, gt);
|
||||||
luaC_barrier(L, f->upvals[0], gt);
|
luaC_barrier(L, f->upvals[0], gt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1071,7 +1112,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
|
|||||||
TValue *o;
|
TValue *o;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
o = s2v(L->top - 1);
|
o = s2v(L->top.p - 1);
|
||||||
if (isLfunction(o))
|
if (isLfunction(o))
|
||||||
status = luaU_dump(L, getproto(o), writer, data, strip);
|
status = luaU_dump(L, getproto(o), writer, data, strip);
|
||||||
else
|
else
|
||||||
@@ -1093,16 +1134,18 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||||||
va_list argp;
|
va_list argp;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
|
if (g->gcstp & GCSTPGC) /* internal stop? */
|
||||||
|
return -1; /* all options are invalid when stopped */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
va_start(argp, what);
|
va_start(argp, what);
|
||||||
switch (what) {
|
switch (what) {
|
||||||
case LUA_GCSTOP: {
|
case LUA_GCSTOP: {
|
||||||
g->gcrunning = 0;
|
g->gcstp = GCSTPUSR; /* stopped by the user */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_GCRESTART: {
|
case LUA_GCRESTART: {
|
||||||
luaE_setdebt(g, 0);
|
luaE_setdebt(g, 0);
|
||||||
g->gcrunning = 1;
|
g->gcstp = 0; /* (GCSTPGC must be already zero here) */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_GCCOLLECT: {
|
case LUA_GCCOLLECT: {
|
||||||
@@ -1121,8 +1164,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||||||
case LUA_GCSTEP: {
|
case LUA_GCSTEP: {
|
||||||
int data = va_arg(argp, int);
|
int data = va_arg(argp, int);
|
||||||
l_mem debt = 1; /* =1 to signal that it did an actual step */
|
l_mem debt = 1; /* =1 to signal that it did an actual step */
|
||||||
lu_byte oldrunning = g->gcrunning;
|
lu_byte oldstp = g->gcstp;
|
||||||
g->gcrunning = 1; /* allow GC to run */
|
g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */
|
||||||
if (data == 0) {
|
if (data == 0) {
|
||||||
luaE_setdebt(g, 0); /* do a basic step */
|
luaE_setdebt(g, 0); /* do a basic step */
|
||||||
luaC_step(L);
|
luaC_step(L);
|
||||||
@@ -1132,7 +1175,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||||||
luaE_setdebt(g, debt);
|
luaE_setdebt(g, debt);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
}
|
}
|
||||||
g->gcrunning = oldrunning; /* restore previous state */
|
g->gcstp = oldstp; /* restore previous state */
|
||||||
if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
|
if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
|
||||||
res = 1; /* signal it */
|
res = 1; /* signal it */
|
||||||
break;
|
break;
|
||||||
@@ -1150,7 +1193,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_GCISRUNNING: {
|
case LUA_GCISRUNNING: {
|
||||||
res = g->gcrunning;
|
res = gcrunning(g);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_GCGEN: {
|
case LUA_GCGEN: {
|
||||||
@@ -1193,9 +1236,15 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API int lua_error (lua_State *L) {
|
LUA_API int lua_error (lua_State *L) {
|
||||||
|
TValue *errobj;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
|
errobj = s2v(L->top.p - 1);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
luaG_errormsg(L);
|
/* error object is the memory error message? */
|
||||||
|
if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
|
||||||
|
luaM_error(L); /* raise a memory error */
|
||||||
|
else
|
||||||
|
luaG_errormsg(L); /* raise a regular error */
|
||||||
/* code unreachable; will unlock when control actually leaves the kernel */
|
/* code unreachable; will unlock when control actually leaves the kernel */
|
||||||
return 0; /* to avoid warnings */
|
return 0; /* to avoid warnings */
|
||||||
}
|
}
|
||||||
@@ -1207,12 +1256,12 @@ LUA_API int lua_next (lua_State *L, int idx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
t = gettable(L, idx);
|
t = gettable(L, idx);
|
||||||
more = luaH_next(L, t, L->top - 1);
|
more = luaH_next(L, t, L->top.p - 1);
|
||||||
if (more) {
|
if (more) {
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
else /* no more elements */
|
else /* no more elements */
|
||||||
L->top -= 1; /* remove key */
|
L->top.p -= 1; /* remove key */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return more;
|
return more;
|
||||||
}
|
}
|
||||||
@@ -1224,8 +1273,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
o = index2stack(L, idx);
|
o = index2stack(L, idx);
|
||||||
nresults = L->ci->nresults;
|
nresults = L->ci->nresults;
|
||||||
api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o,
|
api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
|
||||||
"marked index below or equal new one");
|
|
||||||
luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
|
luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
|
||||||
if (!hastocloseCfunc(nresults)) /* function not marked yet? */
|
if (!hastocloseCfunc(nresults)) /* function not marked yet? */
|
||||||
L->ci->nresults = codeNresults(nresults); /* mark it */
|
L->ci->nresults = codeNresults(nresults); /* mark it */
|
||||||
@@ -1237,14 +1285,12 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
|
|||||||
LUA_API void lua_concat (lua_State *L, int n) {
|
LUA_API void lua_concat (lua_State *L, int n) {
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
if (n >= 2) {
|
if (n > 0)
|
||||||
luaV_concat(L, n);
|
luaV_concat(L, n);
|
||||||
}
|
else { /* nothing to concatenate */
|
||||||
else if (n == 0) { /* push empty string */
|
setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
|
||||||
setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
|
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
/* else n == 1; nothing to do */
|
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -1254,7 +1300,7 @@ LUA_API void lua_len (lua_State *L, int idx) {
|
|||||||
TValue *t;
|
TValue *t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2value(L, idx);
|
t = index2value(L, idx);
|
||||||
luaV_objlen(L, L->top, t);
|
luaV_objlen(L, L->top.p, t);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -1297,9 +1343,9 @@ void lua_warning (lua_State *L, const char *msg, int tocont) {
|
|||||||
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
||||||
Udata *u;
|
Udata *u;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
|
api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
|
||||||
u = luaS_newudata(L, size, nuvalue);
|
u = luaS_newudata(L, size, nuvalue);
|
||||||
setuvalue(L, s2v(L->top), u);
|
setuvalue(L, s2v(L->top.p), u);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -1311,7 +1357,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
|||||||
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
||||||
GCObject **owner) {
|
GCObject **owner) {
|
||||||
switch (ttypetag(fi)) {
|
switch (ttypetag(fi)) {
|
||||||
case LUA_TCCL: { /* C closure */
|
case LUA_VCCL: { /* C closure */
|
||||||
CClosure *f = clCvalue(fi);
|
CClosure *f = clCvalue(fi);
|
||||||
if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
|
if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
|
||||||
return NULL; /* 'n' not in [1, f->nupvalues] */
|
return NULL; /* 'n' not in [1, f->nupvalues] */
|
||||||
@@ -1319,13 +1365,13 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
|||||||
if (owner) *owner = obj2gco(f);
|
if (owner) *owner = obj2gco(f);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
case LUA_TLCL: { /* Lua closure */
|
case LUA_VLCL: { /* Lua closure */
|
||||||
LClosure *f = clLvalue(fi);
|
LClosure *f = clLvalue(fi);
|
||||||
TString *name;
|
TString *name;
|
||||||
Proto *p = f->p;
|
Proto *p = f->p;
|
||||||
if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
|
if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
|
||||||
return NULL; /* 'n' not in [1, p->sizeupvalues] */
|
return NULL; /* 'n' not in [1, p->sizeupvalues] */
|
||||||
*val = f->upvals[n-1]->v;
|
*val = f->upvals[n-1]->v.p;
|
||||||
if (owner) *owner = obj2gco(f->upvals[n - 1]);
|
if (owner) *owner = obj2gco(f->upvals[n - 1]);
|
||||||
name = p->upvalues[n-1].name;
|
name = p->upvalues[n-1].name;
|
||||||
return (name == NULL) ? "(no name)" : getstr(name);
|
return (name == NULL) ? "(no name)" : getstr(name);
|
||||||
@@ -1341,7 +1387,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
|
name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
|
||||||
if (name) {
|
if (name) {
|
||||||
setobj2s(L, L->top, val);
|
setobj2s(L, L->top.p, val);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -1359,8 +1405,8 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
|
|||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
name = aux_upvalue(fi, n, &val, &owner);
|
name = aux_upvalue(fi, n, &val, &owner);
|
||||||
if (name) {
|
if (name) {
|
||||||
L->top--;
|
L->top.p--;
|
||||||
setobj(L, val, s2v(L->top));
|
setobj(L, val, s2v(L->top.p));
|
||||||
luaC_barrier(L, owner, val);
|
luaC_barrier(L, owner, val);
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -1369,29 +1415,35 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
|
|||||||
|
|
||||||
|
|
||||||
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
|
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
|
||||||
|
static const UpVal *const nullup = NULL;
|
||||||
LClosure *f;
|
LClosure *f;
|
||||||
TValue *fi = index2value(L, fidx);
|
TValue *fi = index2value(L, fidx);
|
||||||
api_check(L, ttisLclosure(fi), "Lua function expected");
|
api_check(L, ttisLclosure(fi), "Lua function expected");
|
||||||
f = clLvalue(fi);
|
f = clLvalue(fi);
|
||||||
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
|
|
||||||
if (pf) *pf = f;
|
if (pf) *pf = f;
|
||||||
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
if (1 <= n && n <= f->p->sizeupvalues)
|
||||||
|
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
||||||
|
else
|
||||||
|
return (UpVal**)&nullup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
||||||
TValue *fi = index2value(L, fidx);
|
TValue *fi = index2value(L, fidx);
|
||||||
switch (ttypetag(fi)) {
|
switch (ttypetag(fi)) {
|
||||||
case LUA_TLCL: { /* lua closure */
|
case LUA_VLCL: { /* lua closure */
|
||||||
return *getupvalref(L, fidx, n, NULL);
|
return *getupvalref(L, fidx, n, NULL);
|
||||||
}
|
}
|
||||||
case LUA_TCCL: { /* C closure */
|
case LUA_VCCL: { /* C closure */
|
||||||
CClosure *f = clCvalue(fi);
|
CClosure *f = clCvalue(fi);
|
||||||
api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
|
if (1 <= n && n <= f->nupvalues)
|
||||||
return &f->upvalue[n - 1];
|
return &f->upvalue[n - 1];
|
||||||
}
|
/* else */
|
||||||
|
} /* FALLTHROUGH */
|
||||||
|
case LUA_VLCF:
|
||||||
|
return NULL; /* light C functions have no upvalues */
|
||||||
default: {
|
default: {
|
||||||
api_check(L, 0, "closure expected");
|
api_check(L, 0, "function expected");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1403,6 +1455,7 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
|
|||||||
LClosure *f1;
|
LClosure *f1;
|
||||||
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
|
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
|
||||||
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
|
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
|
||||||
|
api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
|
||||||
*up1 = *up2;
|
*up1 = *up2;
|
||||||
luaC_objbarrier(L, f1, *up1);
|
luaC_objbarrier(L, f1, *up1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,14 +11,27 @@
|
|||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
|
|
||||||
"stack overflow");}
|
|
||||||
|
|
||||||
|
/* 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");}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If a call returns too many multiple returns, the callee may not have
|
||||||
|
** stack space to accommodate all results. In this case, this macro
|
||||||
|
** increases its stack space ('L->ci->top.p').
|
||||||
|
*/
|
||||||
#define adjustresults(L,nres) \
|
#define adjustresults(L,nres) \
|
||||||
{ if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
|
{ if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
|
||||||
|
L->ci->top.p = L->top.p; }
|
||||||
|
|
||||||
#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
|
|
||||||
"not enough elements in the stack")
|
/* 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")
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -32,6 +45,8 @@
|
|||||||
|
|
||||||
#define hastocloseCfunc(n) ((n) < LUA_MULTRET)
|
#define hastocloseCfunc(n) ((n) < LUA_MULTRET)
|
||||||
|
|
||||||
|
/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
|
||||||
#define codeNresults(n) (-(n) - 3)
|
#define codeNresults(n) (-(n) - 3)
|
||||||
|
#define decodeNresults(n) (-(n) - 3)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ static int findfield (lua_State *L, int objidx, int level) {
|
|||||||
else if (findfield(L, objidx, level - 1)) { /* try recursively */
|
else if (findfield(L, objidx, level - 1)) { /* try recursively */
|
||||||
/* stack: lib_name, lib_table, field_name (top) */
|
/* stack: lib_name, lib_table, field_name (top) */
|
||||||
lua_pushliteral(L, "."); /* place '.' between the two names */
|
lua_pushliteral(L, "."); /* place '.' between the two names */
|
||||||
lua_replace(L, -3); /* (in the slot ocupied by table) */
|
lua_replace(L, -3); /* (in the slot occupied by table) */
|
||||||
lua_concat(L, 3); /* lib_name.field_name */
|
lua_concat(L, 3); /* lib_name.field_name */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -80,6 +80,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
|||||||
int top = lua_gettop(L);
|
int top = lua_gettop(L);
|
||||||
lua_getinfo(L, "f", ar); /* push function */
|
lua_getinfo(L, "f", ar); /* push function */
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
|
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
|
||||||
|
luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */
|
||||||
if (findfield(L, top + 1, 2)) {
|
if (findfield(L, top + 1, 2)) {
|
||||||
const char *name = lua_tostring(L, -1);
|
const char *name = lua_tostring(L, -1);
|
||||||
if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */
|
if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */
|
||||||
@@ -87,7 +88,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
|||||||
lua_remove(L, -2); /* remove original name */
|
lua_remove(L, -2); /* remove original name */
|
||||||
}
|
}
|
||||||
lua_copy(L, -1, top + 1); /* copy name to proper place */
|
lua_copy(L, -1, top + 1); /* copy name to proper place */
|
||||||
lua_settop(L, top + 1); /* remove table "loaded" an name copy */
|
lua_settop(L, top + 1); /* remove table "loaded" and name copy */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -190,7 +191,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaL_typeerror (lua_State *L, int arg, const char *tname) {
|
LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
|
||||||
const char *msg;
|
const char *msg;
|
||||||
const char *typearg; /* name for the type of the actual argument */
|
const char *typearg; /* name for the type of the actual argument */
|
||||||
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
|
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
|
||||||
@@ -249,11 +250,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lua_pushnil(L);
|
const char *msg;
|
||||||
|
luaL_pushfail(L);
|
||||||
|
msg = (en != 0) ? strerror(en) : "(no extra info)";
|
||||||
if (fname)
|
if (fname)
|
||||||
lua_pushfstring(L, "%s: %s", fname, strerror(en));
|
lua_pushfstring(L, "%s: %s", fname, msg);
|
||||||
else
|
else
|
||||||
lua_pushstring(L, strerror(en));
|
lua_pushstring(L, msg);
|
||||||
lua_pushinteger(L, en);
|
lua_pushinteger(L, en);
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
@@ -283,18 +286,18 @@ 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 int luaL_execresult (lua_State *L, int stat) {
|
||||||
const char *what = "exit"; /* type of termination */
|
if (stat != 0 && errno != 0) /* error with an 'errno'? */
|
||||||
if (stat == -1) /* error? */
|
|
||||||
return luaL_fileresult(L, 0, NULL);
|
return luaL_fileresult(L, 0, NULL);
|
||||||
else {
|
else {
|
||||||
|
const char *what = "exit"; /* type of termination */
|
||||||
l_inspectstat(stat, what); /* interpret result */
|
l_inspectstat(stat, what); /* interpret result */
|
||||||
if (*what == 'e' && stat == 0) /* successful termination? */
|
if (*what == 'e' && stat == 0) /* successful termination? */
|
||||||
lua_pushboolean(L, 1);
|
lua_pushboolean(L, 1);
|
||||||
else
|
else
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
lua_pushstring(L, what);
|
lua_pushstring(L, what);
|
||||||
lua_pushinteger(L, stat);
|
lua_pushinteger(L, stat);
|
||||||
return 3; /* return true/nil,what,code */
|
return 3; /* return true/fail,what,code */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +381,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
|
|||||||
** but without 'msg'.)
|
** but without 'msg'.)
|
||||||
*/
|
*/
|
||||||
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
|
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
|
||||||
if (!lua_checkstack(L, space)) {
|
if (l_unlikely(!lua_checkstack(L, space))) {
|
||||||
if (msg)
|
if (msg)
|
||||||
luaL_error(L, "stack overflow (%s)", msg);
|
luaL_error(L, "stack overflow (%s)", msg);
|
||||||
else
|
else
|
||||||
@@ -388,20 +391,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
|
|||||||
|
|
||||||
|
|
||||||
LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
|
LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
|
||||||
if (lua_type(L, arg) != t)
|
if (l_unlikely(lua_type(L, arg) != t))
|
||||||
tag_error(L, arg, t);
|
tag_error(L, arg, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUALIB_API void luaL_checkany (lua_State *L, int arg) {
|
LUALIB_API void luaL_checkany (lua_State *L, int arg) {
|
||||||
if (lua_type(L, arg) == LUA_TNONE)
|
if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
|
||||||
luaL_argerror(L, arg, "value expected");
|
luaL_argerror(L, arg, "value expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
|
LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
|
||||||
const char *s = lua_tolstring(L, arg, len);
|
const char *s = lua_tolstring(L, arg, len);
|
||||||
if (!s) tag_error(L, arg, LUA_TSTRING);
|
if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,7 +423,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
|
|||||||
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
|
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
|
||||||
int isnum;
|
int isnum;
|
||||||
lua_Number d = lua_tonumberx(L, arg, &isnum);
|
lua_Number d = lua_tonumberx(L, arg, &isnum);
|
||||||
if (!isnum)
|
if (l_unlikely(!isnum))
|
||||||
tag_error(L, arg, LUA_TNUMBER);
|
tag_error(L, arg, LUA_TNUMBER);
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
@@ -442,7 +445,7 @@ static void interror (lua_State *L, int arg) {
|
|||||||
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
|
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
|
||||||
int isnum;
|
int isnum;
|
||||||
lua_Integer d = lua_tointegerx(L, arg, &isnum);
|
lua_Integer d = lua_tointegerx(L, arg, &isnum);
|
||||||
if (!isnum) {
|
if (l_unlikely(!isnum)) {
|
||||||
interror(L, arg);
|
interror(L, arg);
|
||||||
}
|
}
|
||||||
return d;
|
return d;
|
||||||
@@ -475,8 +478,10 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
|
|||||||
lua_Alloc allocf = lua_getallocf(L, &ud);
|
lua_Alloc allocf = lua_getallocf(L, &ud);
|
||||||
UBox *box = (UBox *)lua_touserdata(L, idx);
|
UBox *box = (UBox *)lua_touserdata(L, idx);
|
||||||
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
||||||
if (temp == NULL && newsize > 0) /* allocation error? */
|
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
|
||||||
luaL_error(L, "not enough memory for buffer allocation");
|
lua_pushliteral(L, "not enough memory");
|
||||||
|
lua_error(L); /* raise a memory error */
|
||||||
|
}
|
||||||
box->box = temp;
|
box->box = temp;
|
||||||
box->bsize = newsize;
|
box->bsize = newsize;
|
||||||
return temp;
|
return temp;
|
||||||
@@ -513,15 +518,25 @@ static void newbox (lua_State *L) {
|
|||||||
#define buffonstack(B) ((B)->b != (B)->init.b)
|
#define buffonstack(B) ((B)->b != (B)->init.b)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Whenever buffer is accessed, slot 'idx' must either be a box (which
|
||||||
|
** cannot be NULL) or it is a placeholder for the buffer.
|
||||||
|
*/
|
||||||
|
#define checkbufferlevel(B,idx) \
|
||||||
|
lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \
|
||||||
|
: lua_touserdata(B->L, idx) == (void*)B)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
|
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
|
||||||
** bytes.
|
** bytes. (The test for "not big enough" also gets the case when the
|
||||||
|
** computation of 'newsize' overflows.)
|
||||||
*/
|
*/
|
||||||
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
|
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
|
||||||
size_t newsize = B->size * 2; /* double buffer size */
|
size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */
|
||||||
if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */
|
if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
|
||||||
return luaL_error(B->L, "buffer too large");
|
return luaL_error(B->L, "buffer too large");
|
||||||
if (newsize < B->n + sz) /* double is not big enough? */
|
if (newsize < B->n + sz) /* not big enough? */
|
||||||
newsize = B->n + sz;
|
newsize = B->n + sz;
|
||||||
return newsize;
|
return newsize;
|
||||||
}
|
}
|
||||||
@@ -529,10 +544,11 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Returns a pointer to a free area with at least 'sz' bytes in buffer
|
** Returns a pointer to a free area with at least 'sz' bytes in buffer
|
||||||
** 'B'. 'boxidx' is the relative position in the stack where the
|
** 'B'. 'boxidx' is the relative position in the stack where is the
|
||||||
** buffer's box is or should be.
|
** buffer's box or its placeholder.
|
||||||
*/
|
*/
|
||||||
static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
|
static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
|
||||||
|
checkbufferlevel(B, boxidx);
|
||||||
if (B->size - B->n >= sz) /* enough space? */
|
if (B->size - B->n >= sz) /* enough space? */
|
||||||
return B->b + B->n;
|
return B->b + B->n;
|
||||||
else {
|
else {
|
||||||
@@ -543,10 +559,9 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
|
|||||||
if (buffonstack(B)) /* buffer already has a box? */
|
if (buffonstack(B)) /* buffer already has a box? */
|
||||||
newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */
|
newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */
|
||||||
else { /* no box yet */
|
else { /* no box yet */
|
||||||
lua_pushnil(L); /* reserve slot for final result */
|
lua_remove(L, boxidx); /* remove placeholder */
|
||||||
newbox(L); /* create a new box */
|
newbox(L); /* create a new box */
|
||||||
/* move box (and slot) to its intended position */
|
lua_insert(L, boxidx); /* move box to its intended position */
|
||||||
lua_rotate(L, boxidx - 1, 2);
|
|
||||||
lua_toclose(L, boxidx);
|
lua_toclose(L, boxidx);
|
||||||
newbuff = (char *)resizebox(L, boxidx, newsize);
|
newbuff = (char *)resizebox(L, boxidx, newsize);
|
||||||
memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */
|
memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */
|
||||||
@@ -581,11 +596,11 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
|
|||||||
|
|
||||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||||
lua_State *L = B->L;
|
lua_State *L = B->L;
|
||||||
|
checkbufferlevel(B, -1);
|
||||||
lua_pushlstring(L, B->b, B->n);
|
lua_pushlstring(L, B->b, B->n);
|
||||||
if (buffonstack(B)) {
|
if (buffonstack(B))
|
||||||
lua_copy(L, -1, -3); /* move string to reserved slot */
|
lua_closeslot(L, -2); /* close the box */
|
||||||
lua_pop(L, 2); /* pop string and box (closing the box) */
|
lua_remove(L, -2); /* remove box or placeholder from the stack */
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -600,7 +615,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
|
|||||||
** box (if existent) is not on the top of the stack. So, instead of
|
** box (if existent) is not on the top of the stack. So, instead of
|
||||||
** calling 'luaL_addlstring', it replicates the code using -2 as the
|
** calling 'luaL_addlstring', it replicates the code using -2 as the
|
||||||
** last argument to 'prepbuffsize', signaling that the box is (or will
|
** last argument to 'prepbuffsize', signaling that the box is (or will
|
||||||
** be) bellow the string being added to the buffer. (Box creation can
|
** be) below the string being added to the buffer. (Box creation can
|
||||||
** trigger an emergency GC, so we should not remove the string from the
|
** trigger an emergency GC, so we should not remove the string from the
|
||||||
** stack before we have the space guaranteed.)
|
** stack before we have the space guaranteed.)
|
||||||
*/
|
*/
|
||||||
@@ -620,6 +635,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
|||||||
B->b = B->init.b;
|
B->b = B->init.b;
|
||||||
B->n = 0;
|
B->n = 0;
|
||||||
B->size = LUAL_BUFFERSIZE;
|
B->size = LUAL_BUFFERSIZE;
|
||||||
|
lua_pushlightuserdata(L, (void*)B); /* push placeholder */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -637,10 +653,14 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* index of free-list header */
|
/* index of free-list header (after the predefined values) */
|
||||||
#define freelist 0
|
#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.
|
||||||
|
*/
|
||||||
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||||
int ref;
|
int ref;
|
||||||
if (lua_isnil(L, -1)) {
|
if (lua_isnil(L, -1)) {
|
||||||
@@ -648,9 +668,16 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
|||||||
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
|
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
|
||||||
}
|
}
|
||||||
t = lua_absindex(L, t);
|
t = lua_absindex(L, t);
|
||||||
lua_rawgeti(L, t, freelist); /* get first free element */
|
if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */
|
||||||
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
|
ref = 0; /* list is empty */
|
||||||
lua_pop(L, 1); /* remove it from stack */
|
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_pop(L, 1); /* remove element from stack */
|
||||||
if (ref != 0) { /* any free element? */
|
if (ref != 0) { /* any free element? */
|
||||||
lua_rawgeti(L, t, ref); /* remove it from list */
|
lua_rawgeti(L, t, ref); /* remove it from list */
|
||||||
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
|
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
|
||||||
@@ -666,6 +693,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
|||||||
if (ref >= 0) {
|
if (ref >= 0) {
|
||||||
t = lua_absindex(L, t);
|
t = lua_absindex(L, t);
|
||||||
lua_rawgeti(L, t, freelist);
|
lua_rawgeti(L, t, freelist);
|
||||||
|
lua_assert(lua_isinteger(L, -1));
|
||||||
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
|
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
|
||||||
lua_pushinteger(L, ref);
|
lua_pushinteger(L, ref);
|
||||||
lua_rawseti(L, t, freelist); /* t[freelist] = ref */
|
lua_rawseti(L, t, freelist); /* t[freelist] = ref */
|
||||||
@@ -707,25 +735,29 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
|
|||||||
|
|
||||||
|
|
||||||
static int errfile (lua_State *L, const char *what, int fnameindex) {
|
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;
|
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);
|
lua_remove(L, fnameindex);
|
||||||
return LUA_ERRFILE;
|
return LUA_ERRFILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int skipBOM (LoadF *lf) {
|
/*
|
||||||
const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
|
** Skip an optional BOM at the start of a stream. If there is an
|
||||||
int c;
|
** incomplete BOM (the first character is correct but the rest is
|
||||||
lf->n = 0;
|
** not), returns the first character anyway to force an error
|
||||||
do {
|
** (as no chunk can start with 0xEF).
|
||||||
c = getc(lf->f);
|
*/
|
||||||
if (c == EOF || c != *(const unsigned char *)p++) return c;
|
static int skipBOM (FILE *f) {
|
||||||
lf->buff[lf->n++] = c; /* to be read by the parser */
|
int c = getc(f); /* read first character */
|
||||||
} while (*p != '\0');
|
if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */
|
||||||
lf->n = 0; /* prefix matched; discard it */
|
return getc(f); /* ignore BOM and return next char */
|
||||||
return getc(lf->f); /* return next character */
|
else /* no (valid) BOM */
|
||||||
|
return c; /* return first character */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -736,13 +768,13 @@ static int skipBOM (LoadF *lf) {
|
|||||||
** first "valid" character of the file (after the optional BOM and
|
** first "valid" character of the file (after the optional BOM and
|
||||||
** a first-line comment).
|
** a first-line comment).
|
||||||
*/
|
*/
|
||||||
static int skipcomment (LoadF *lf, int *cp) {
|
static int skipcomment (FILE *f, int *cp) {
|
||||||
int c = *cp = skipBOM(lf);
|
int c = *cp = skipBOM(f);
|
||||||
if (c == '#') { /* first line is a comment (Unix exec. file)? */
|
if (c == '#') { /* first line is a comment (Unix exec. file)? */
|
||||||
do { /* skip first line */
|
do { /* skip first line */
|
||||||
c = getc(lf->f);
|
c = getc(f);
|
||||||
} while (c != EOF && c != '\n');
|
} while (c != EOF && c != '\n');
|
||||||
*cp = getc(lf->f); /* skip end-of-line, if present */
|
*cp = getc(f); /* next character after comment, if present */
|
||||||
return 1; /* there was a comment */
|
return 1; /* there was a comment */
|
||||||
}
|
}
|
||||||
else return 0; /* no comment */
|
else return 0; /* no comment */
|
||||||
@@ -761,18 +793,25 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lua_pushfstring(L, "@%s", filename);
|
lua_pushfstring(L, "@%s", filename);
|
||||||
|
errno = 0;
|
||||||
lf.f = fopen(filename, "r");
|
lf.f = fopen(filename, "r");
|
||||||
if (lf.f == NULL) return errfile(L, "open", fnameindex);
|
if (lf.f == NULL) return errfile(L, "open", fnameindex);
|
||||||
}
|
}
|
||||||
if (skipcomment(&lf, &c)) /* read initial portion */
|
lf.n = 0;
|
||||||
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
|
if (skipcomment(lf.f, &c)) /* read initial portion */
|
||||||
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
|
lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */
|
||||||
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
|
if (c == LUA_SIGNATURE[0]) { /* binary file? */
|
||||||
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
|
lf.n = 0; /* remove possible newline */
|
||||||
skipcomment(&lf, &c); /* re-read initial portion */
|
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)
|
if (c != EOF)
|
||||||
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
|
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
|
||||||
|
errno = 0;
|
||||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
|
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
|
||||||
readstatus = ferror(lf.f);
|
readstatus = ferror(lf.f);
|
||||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||||
@@ -849,7 +888,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
|
|||||||
int isnum;
|
int isnum;
|
||||||
lua_len(L, idx);
|
lua_len(L, idx);
|
||||||
l = lua_tointegerx(L, -1, &isnum);
|
l = lua_tointegerx(L, -1, &isnum);
|
||||||
if (!isnum)
|
if (l_unlikely(!isnum))
|
||||||
luaL_error(L, "object length is not an integer");
|
luaL_error(L, "object length is not an integer");
|
||||||
lua_pop(L, 1); /* remove object */
|
lua_pop(L, 1); /* remove object */
|
||||||
return l;
|
return l;
|
||||||
@@ -857,6 +896,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
|
|||||||
|
|
||||||
|
|
||||||
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||||
|
idx = lua_absindex(L,idx);
|
||||||
if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
|
if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
|
||||||
if (!lua_isstring(L, -1))
|
if (!lua_isstring(L, -1))
|
||||||
luaL_error(L, "'__tostring' must return a string");
|
luaL_error(L, "'__tostring' must return a string");
|
||||||
@@ -902,10 +942,10 @@ 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) {
|
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||||
luaL_checkstack(L, nup, "too many upvalues");
|
luaL_checkstack(L, nup, "too many upvalues");
|
||||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||||
int i;
|
if (l->func == NULL) /* placeholder? */
|
||||||
if (l->func == NULL) /* place holder? */
|
|
||||||
lua_pushboolean(L, 0);
|
lua_pushboolean(L, 0);
|
||||||
else {
|
else {
|
||||||
|
int i;
|
||||||
for (i = 0; i < nup; i++) /* copy upvalues to the top */
|
for (i = 0; i < nup; i++) /* copy upvalues to the top */
|
||||||
lua_pushvalue(L, -nup);
|
lua_pushvalue(L, -nup);
|
||||||
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
|
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
|
||||||
@@ -994,37 +1034,82 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Standard panic funcion just prints an error message. The test
|
||||||
|
** with 'lua_type' avoids possible memory errors in 'lua_tostring'.
|
||||||
|
*/
|
||||||
static int panic (lua_State *L) {
|
static int panic (lua_State *L) {
|
||||||
|
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",
|
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||||
lua_tostring(L, -1));
|
msg);
|
||||||
return 0; /* return to Lua to abort */
|
return 0; /* return to Lua to abort */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Emit a warning. '*previoustocont' signals whether previous message
|
** Warning functions:
|
||||||
** was to be continued by the current one.
|
** warnfoff: warning system is off
|
||||||
|
** warnfon: ready to start a new message
|
||||||
|
** warnfcont: previous message is to be continued
|
||||||
*/
|
*/
|
||||||
static void warnf (void *ud, const char *message, int tocont) {
|
static void warnfoff (void *ud, const char *message, int tocont);
|
||||||
int *previoustocont = (int *)ud;
|
static void warnfon (void *ud, const char *message, int tocont);
|
||||||
if (!*previoustocont) /* previous message was the last? */
|
static void warnfcont (void *ud, const char *message, int tocont);
|
||||||
lua_writestringerror("%s", "Lua warning: "); /* start a new warning */
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Check whether message is a control message. If so, execute the
|
||||||
|
** control or ignore it if unknown.
|
||||||
|
*/
|
||||||
|
static int checkcontrol (lua_State *L, const char *message, int tocont) {
|
||||||
|
if (tocont || *(message++) != '@') /* not a control message? */
|
||||||
|
return 0;
|
||||||
|
else {
|
||||||
|
if (strcmp(message, "off") == 0)
|
||||||
|
lua_setwarnf(L, warnfoff, L); /* turn warnings off */
|
||||||
|
else if (strcmp(message, "on") == 0)
|
||||||
|
lua_setwarnf(L, warnfon, L); /* turn warnings on */
|
||||||
|
return 1; /* it was a control message */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void warnfoff (void *ud, const char *message, int tocont) {
|
||||||
|
checkcontrol((lua_State *)ud, message, tocont);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Writes the message and handle 'tocont', finishing the message
|
||||||
|
** if needed and setting the next warn function.
|
||||||
|
*/
|
||||||
|
static void warnfcont (void *ud, const char *message, int tocont) {
|
||||||
|
lua_State *L = (lua_State *)ud;
|
||||||
lua_writestringerror("%s", message); /* write message */
|
lua_writestringerror("%s", message); /* write message */
|
||||||
if (!tocont) /* is this the last part? */
|
if (tocont) /* not the last part? */
|
||||||
|
lua_setwarnf(L, warnfcont, L); /* to be continued */
|
||||||
|
else { /* last part */
|
||||||
lua_writestringerror("%s", "\n"); /* finish message with end-of-line */
|
lua_writestringerror("%s", "\n"); /* finish message with end-of-line */
|
||||||
*previoustocont = tocont;
|
lua_setwarnf(L, warnfon, L); /* next call is a new message */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void warnfon (void *ud, const char *message, int tocont) {
|
||||||
|
if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */
|
||||||
|
return; /* nothing else to be done */
|
||||||
|
lua_writestringerror("%s", "Lua warning: "); /* start a new warning */
|
||||||
|
warnfcont(ud, message, tocont); /* finish processing */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUALIB_API lua_State *luaL_newstate (void) {
|
LUALIB_API lua_State *luaL_newstate (void) {
|
||||||
lua_State *L = lua_newstate(l_alloc, NULL);
|
lua_State *L = lua_newstate(l_alloc, NULL);
|
||||||
if (L) {
|
if (l_likely(L)) {
|
||||||
int *previoustocont; /* space for warning state */
|
|
||||||
lua_atpanic(L, &panic);
|
lua_atpanic(L, &panic);
|
||||||
previoustocont = (int *)lua_newuserdatauv(L, sizeof(int), 0);
|
lua_setwarnf(L, warnfoff, L); /* default is warnings off */
|
||||||
luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */
|
|
||||||
*previoustocont = 0; /* next message starts a new warning */
|
|
||||||
lua_setwarnf(L, warnf, previoustocont);
|
|
||||||
}
|
}
|
||||||
return L;
|
return L;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,12 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "luaconf.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
/* global table */
|
/* global table */
|
||||||
#define LUA_GNAME "_G"
|
#define LUA_GNAME "_G"
|
||||||
|
|
||||||
|
|
||||||
typedef struct luaL_Buffer luaL_Buffer;
|
typedef struct luaL_Buffer luaL_Buffer;
|
||||||
@@ -101,7 +102,7 @@ LUALIB_API lua_State *(luaL_newstate) (void);
|
|||||||
|
|
||||||
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
|
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
|
||||||
|
|
||||||
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
|
LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
|
||||||
const char *p, const char *r);
|
const char *p, const char *r);
|
||||||
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
|
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
|
||||||
const char *p, const char *r);
|
const char *p, const char *r);
|
||||||
@@ -130,10 +131,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
|||||||
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
|
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
|
||||||
|
|
||||||
#define luaL_argcheck(L, cond,arg,extramsg) \
|
#define luaL_argcheck(L, cond,arg,extramsg) \
|
||||||
((void)((cond) || luaL_argerror(L, (arg), (extramsg))))
|
((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
|
||||||
|
|
||||||
#define luaL_argexpected(L,cond,arg,tname) \
|
#define luaL_argexpected(L,cond,arg,tname) \
|
||||||
((void)((cond) || luaL_typeerror(L, (arg), (tname))))
|
((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname))))
|
||||||
|
|
||||||
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
|
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
|
||||||
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
|
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
|
||||||
@@ -153,6 +154,34 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
|||||||
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
|
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Perform arithmetic operations on lua_Integer values with wrap-around
|
||||||
|
** semantics, as the Lua core does.
|
||||||
|
*/
|
||||||
|
#define luaL_intop(op,v1,v2) \
|
||||||
|
((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2)))
|
||||||
|
|
||||||
|
|
||||||
|
/* 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)
|
||||||
|
#else
|
||||||
|
#define lua_assert(c) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {======================================================
|
||||||
** Generic Buffer manipulation
|
** Generic Buffer manipulation
|
||||||
@@ -181,6 +210,8 @@ struct luaL_Buffer {
|
|||||||
|
|
||||||
#define luaL_addsize(B,s) ((B)->n += (s))
|
#define luaL_addsize(B,s) ((B)->n += (s))
|
||||||
|
|
||||||
|
#define luaL_buffsub(B,s) ((B)->n -= (s))
|
||||||
|
|
||||||
LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
|
LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
|
||||||
LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
|
LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
|
||||||
LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
|
LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
|
||||||
|
|||||||
+36
-14
@@ -48,9 +48,9 @@ static int luaB_warn (lua_State *L) {
|
|||||||
luaL_checkstring(L, 1); /* at least one argument */
|
luaL_checkstring(L, 1); /* at least one argument */
|
||||||
for (i = 2; i <= n; i++)
|
for (i = 2; i <= n; i++)
|
||||||
luaL_checkstring(L, i); /* make sure all arguments are strings */
|
luaL_checkstring(L, i); /* make sure all arguments are strings */
|
||||||
for (i = 1; i <= n; i++) /* compose warning */
|
for (i = 1; i < n; i++) /* compose warning */
|
||||||
lua_warning(L, lua_tostring(L, i), 1);
|
lua_warning(L, lua_tostring(L, i), 1);
|
||||||
lua_warning(L, "", 0); /* close warning */
|
lua_warning(L, lua_tostring(L, n), 0); /* close warning */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ static int luaB_tonumber (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
} /* else not a number */
|
} /* else not a number */
|
||||||
} /* else not a number */
|
} /* else not a number */
|
||||||
lua_pushnil(L); /* not a number */
|
luaL_pushfail(L); /* not a number */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +138,7 @@ static int luaB_setmetatable (lua_State *L) {
|
|||||||
int t = lua_type(L, 2);
|
int t = lua_type(L, 2);
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
|
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
|
||||||
if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
|
if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL))
|
||||||
return luaL_error(L, "cannot change a protected metatable");
|
return luaL_error(L, "cannot change a protected metatable");
|
||||||
lua_settop(L, 2);
|
lua_settop(L, 2);
|
||||||
lua_setmetatable(L, 1);
|
lua_setmetatable(L, 1);
|
||||||
@@ -182,11 +182,20 @@ static int luaB_rawset (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int pushmode (lua_State *L, int oldmode) {
|
static int pushmode (lua_State *L, int oldmode) {
|
||||||
lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational");
|
if (oldmode == -1)
|
||||||
|
luaL_pushfail(L); /* invalid call to 'lua_gc' */
|
||||||
|
else
|
||||||
|
lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
|
||||||
|
: "generational");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** check whether call to 'lua_gc' was valid (not inside a finalizer)
|
||||||
|
*/
|
||||||
|
#define checkvalres(res) { if (res == -1) break; }
|
||||||
|
|
||||||
static int luaB_collectgarbage (lua_State *L) {
|
static int luaB_collectgarbage (lua_State *L) {
|
||||||
static const char *const opts[] = {"stop", "restart", "collect",
|
static const char *const opts[] = {"stop", "restart", "collect",
|
||||||
"count", "step", "setpause", "setstepmul",
|
"count", "step", "setpause", "setstepmul",
|
||||||
@@ -199,12 +208,14 @@ static int luaB_collectgarbage (lua_State *L) {
|
|||||||
case LUA_GCCOUNT: {
|
case LUA_GCCOUNT: {
|
||||||
int k = lua_gc(L, o);
|
int k = lua_gc(L, o);
|
||||||
int b = lua_gc(L, LUA_GCCOUNTB);
|
int b = lua_gc(L, LUA_GCCOUNTB);
|
||||||
|
checkvalres(k);
|
||||||
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
|
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case LUA_GCSTEP: {
|
case LUA_GCSTEP: {
|
||||||
int step = (int)luaL_optinteger(L, 2, 0);
|
int step = (int)luaL_optinteger(L, 2, 0);
|
||||||
int res = lua_gc(L, o, step);
|
int res = lua_gc(L, o, step);
|
||||||
|
checkvalres(res);
|
||||||
lua_pushboolean(L, res);
|
lua_pushboolean(L, res);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -212,11 +223,13 @@ static int luaB_collectgarbage (lua_State *L) {
|
|||||||
case LUA_GCSETSTEPMUL: {
|
case LUA_GCSETSTEPMUL: {
|
||||||
int p = (int)luaL_optinteger(L, 2, 0);
|
int p = (int)luaL_optinteger(L, 2, 0);
|
||||||
int previous = lua_gc(L, o, p);
|
int previous = lua_gc(L, o, p);
|
||||||
|
checkvalres(previous);
|
||||||
lua_pushinteger(L, previous);
|
lua_pushinteger(L, previous);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case LUA_GCISRUNNING: {
|
case LUA_GCISRUNNING: {
|
||||||
int res = lua_gc(L, o);
|
int res = lua_gc(L, o);
|
||||||
|
checkvalres(res);
|
||||||
lua_pushboolean(L, res);
|
lua_pushboolean(L, res);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -233,10 +246,13 @@ static int luaB_collectgarbage (lua_State *L) {
|
|||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
int res = lua_gc(L, o);
|
int res = lua_gc(L, o);
|
||||||
|
checkvalres(res);
|
||||||
lua_pushinteger(L, res);
|
lua_pushinteger(L, res);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
luaL_pushfail(L); /* invalid call (inside a finalizer) */
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -260,6 +276,11 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
static int luaB_pairs (lua_State *L) {
|
static int luaB_pairs (lua_State *L) {
|
||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
|
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
|
||||||
@@ -269,7 +290,7 @@ static int luaB_pairs (lua_State *L) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
|
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
|
||||||
lua_call(L, 1, 3); /* get 3 values from metamethod */
|
lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */
|
||||||
}
|
}
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
@@ -279,7 +300,8 @@ static int luaB_pairs (lua_State *L) {
|
|||||||
** Traversal function for 'ipairs'
|
** Traversal function for 'ipairs'
|
||||||
*/
|
*/
|
||||||
static int ipairsaux (lua_State *L) {
|
static int ipairsaux (lua_State *L) {
|
||||||
lua_Integer i = luaL_checkinteger(L, 2) + 1;
|
lua_Integer i = luaL_checkinteger(L, 2);
|
||||||
|
i = luaL_intop(+, i, 1);
|
||||||
lua_pushinteger(L, i);
|
lua_pushinteger(L, i);
|
||||||
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
|
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
|
||||||
}
|
}
|
||||||
@@ -299,7 +321,7 @@ static int luaB_ipairs (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int load_aux (lua_State *L, int status, int envidx) {
|
static int load_aux (lua_State *L, int status, int envidx) {
|
||||||
if (status == LUA_OK) {
|
if (l_likely(status == LUA_OK)) {
|
||||||
if (envidx != 0) { /* 'env' parameter? */
|
if (envidx != 0) { /* 'env' parameter? */
|
||||||
lua_pushvalue(L, envidx); /* environment for loaded function */
|
lua_pushvalue(L, envidx); /* environment for loaded function */
|
||||||
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
|
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
|
||||||
@@ -308,9 +330,9 @@ static int load_aux (lua_State *L, int status, int envidx) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else { /* error (message is on top of the stack) */
|
else { /* error (message is on top of the stack) */
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
lua_insert(L, -2); /* put before error message */
|
lua_insert(L, -2); /* put before error message */
|
||||||
return 2; /* return nil plus error message */
|
return 2; /* return fail plus error message */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,7 +377,7 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
|
|||||||
*size = 0;
|
*size = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (!lua_isstring(L, -1))
|
else if (l_unlikely(!lua_isstring(L, -1)))
|
||||||
luaL_error(L, "reader function must return a string");
|
luaL_error(L, "reader function must return a string");
|
||||||
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
|
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
|
||||||
return lua_tolstring(L, RESERVEDSLOT, size);
|
return lua_tolstring(L, RESERVEDSLOT, size);
|
||||||
@@ -393,7 +415,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
|
|||||||
static int luaB_dofile (lua_State *L) {
|
static int luaB_dofile (lua_State *L) {
|
||||||
const char *fname = luaL_optstring(L, 1, NULL);
|
const char *fname = luaL_optstring(L, 1, NULL);
|
||||||
lua_settop(L, 1);
|
lua_settop(L, 1);
|
||||||
if (luaL_loadfile(L, fname) != LUA_OK)
|
if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK))
|
||||||
return lua_error(L);
|
return lua_error(L);
|
||||||
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
|
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
|
||||||
return dofilecont(L, 0, 0);
|
return dofilecont(L, 0, 0);
|
||||||
@@ -401,7 +423,7 @@ static int luaB_dofile (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int luaB_assert (lua_State *L) {
|
static int luaB_assert (lua_State *L) {
|
||||||
if (lua_toboolean(L, 1)) /* condition is true? */
|
if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
|
||||||
return lua_gettop(L); /* return all arguments */
|
return lua_gettop(L); /* return all arguments */
|
||||||
else { /* error */
|
else { /* error */
|
||||||
luaL_checkany(L, 1); /* there must be a condition */
|
luaL_checkany(L, 1); /* there must be a condition */
|
||||||
@@ -437,7 +459,7 @@ static int luaB_select (lua_State *L) {
|
|||||||
** ignored).
|
** ignored).
|
||||||
*/
|
*/
|
||||||
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
|
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
|
||||||
if (status != LUA_OK && status != LUA_YIELD) { /* error? */
|
if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */
|
||||||
lua_pushboolean(L, 0); /* first result (false) */
|
lua_pushboolean(L, 0); /* first result (false) */
|
||||||
lua_pushvalue(L, -2); /* error message */
|
lua_pushvalue(L, -2); /* error message */
|
||||||
return 2; /* return false, msg */
|
return 2; /* return false, msg */
|
||||||
|
|||||||
@@ -24,19 +24,27 @@
|
|||||||
** grep "ORDER OPR" if you change these enums (ORDER OP)
|
** grep "ORDER OPR" if you change these enums (ORDER OP)
|
||||||
*/
|
*/
|
||||||
typedef enum BinOpr {
|
typedef enum BinOpr {
|
||||||
|
/* arithmetic operators */
|
||||||
OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
|
OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
|
||||||
OPR_DIV,
|
OPR_DIV, OPR_IDIV,
|
||||||
OPR_IDIV,
|
/* bitwise operators */
|
||||||
OPR_BAND, OPR_BOR, OPR_BXOR,
|
OPR_BAND, OPR_BOR, OPR_BXOR,
|
||||||
OPR_SHL, OPR_SHR,
|
OPR_SHL, OPR_SHR,
|
||||||
|
/* string operator */
|
||||||
OPR_CONCAT,
|
OPR_CONCAT,
|
||||||
|
/* comparison operators */
|
||||||
OPR_EQ, OPR_LT, OPR_LE,
|
OPR_EQ, OPR_LT, OPR_LE,
|
||||||
OPR_NE, OPR_GT, OPR_GE,
|
OPR_NE, OPR_GT, OPR_GE,
|
||||||
|
/* logical operators */
|
||||||
OPR_AND, OPR_OR,
|
OPR_AND, OPR_OR,
|
||||||
OPR_NOBINOPR
|
OPR_NOBINOPR
|
||||||
} BinOpr;
|
} BinOpr;
|
||||||
|
|
||||||
|
|
||||||
|
/* true if operation is foldable (that is, it is arithmetic or bitwise) */
|
||||||
|
#define foldbinop(op) ((op) <= OPR_SHR)
|
||||||
|
|
||||||
|
|
||||||
#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0)
|
#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0)
|
||||||
|
|
||||||
|
|
||||||
@@ -51,23 +59,21 @@ 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)
|
#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_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,
|
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
|
||||||
int B, int C, int k);
|
int B, int C, int k);
|
||||||
LUAI_FUNC int luaK_isKint (expdesc *e);
|
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_fixline (FuncState *fs, int line);
|
||||||
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
|
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
|
||||||
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
|
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
|
||||||
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
|
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
|
||||||
LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
|
|
||||||
LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
|
LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
|
||||||
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC int luaK_exp2anyreg (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_exp2anyregup (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC void luaK_exp2val (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_self (FuncState *fs, expdesc *e, expdesc *key);
|
||||||
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
||||||
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
|
||||||
@@ -85,6 +91,8 @@ LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
|
|||||||
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
||||||
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
|
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
|
||||||
expdesc *v2, int line);
|
expdesc *v2, int line);
|
||||||
|
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_setlist (FuncState *fs, int base, int nelems, int tostore);
|
||||||
LUAI_FUNC void luaK_finish (FuncState *fs);
|
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 *msg);
|
||||||
|
|||||||
+16
-11
@@ -31,14 +31,14 @@ static lua_State *getco (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
||||||
int status, nres;
|
int status, nres;
|
||||||
if (!lua_checkstack(co, narg)) {
|
if (l_unlikely(!lua_checkstack(co, narg))) {
|
||||||
lua_pushliteral(L, "too many arguments to resume");
|
lua_pushliteral(L, "too many arguments to resume");
|
||||||
return -1; /* error flag */
|
return -1; /* error flag */
|
||||||
}
|
}
|
||||||
lua_xmove(L, co, narg);
|
lua_xmove(L, co, narg);
|
||||||
status = lua_resume(co, L, narg, &nres);
|
status = lua_resume(co, L, narg, &nres);
|
||||||
if (status == LUA_OK || status == LUA_YIELD) {
|
if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
|
||||||
if (!lua_checkstack(L, nres + 1)) {
|
if (l_unlikely(!lua_checkstack(L, nres + 1))) {
|
||||||
lua_pop(co, nres); /* remove results anyway */
|
lua_pop(co, nres); /* remove results anyway */
|
||||||
lua_pushliteral(L, "too many results to resume");
|
lua_pushliteral(L, "too many results to resume");
|
||||||
return -1; /* error flag */
|
return -1; /* error flag */
|
||||||
@@ -57,7 +57,7 @@ static int luaB_coresume (lua_State *L) {
|
|||||||
lua_State *co = getco(L);
|
lua_State *co = getco(L);
|
||||||
int r;
|
int r;
|
||||||
r = auxresume(L, co, lua_gettop(L) - 1);
|
r = auxresume(L, co, lua_gettop(L) - 1);
|
||||||
if (r < 0) {
|
if (l_unlikely(r < 0)) {
|
||||||
lua_pushboolean(L, 0);
|
lua_pushboolean(L, 0);
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
return 2; /* return false + error message */
|
return 2; /* return false + error message */
|
||||||
@@ -73,11 +73,15 @@ static int luaB_coresume (lua_State *L) {
|
|||||||
static int luaB_auxwrap (lua_State *L) {
|
static int luaB_auxwrap (lua_State *L) {
|
||||||
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
||||||
int r = auxresume(L, co, lua_gettop(L));
|
int r = auxresume(L, co, lua_gettop(L));
|
||||||
if (r < 0) {
|
if (l_unlikely(r < 0)) { /* error? */
|
||||||
int stat = lua_status(co);
|
int stat = lua_status(co);
|
||||||
if (stat != LUA_OK && stat != LUA_YIELD)
|
if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
|
||||||
lua_resetthread(co); /* close variables in case of errors */
|
stat = lua_closethread(co, L); /* close its tbc variables */
|
||||||
if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */
|
lua_assert(stat != LUA_OK);
|
||||||
|
lua_xmove(co, L, 1); /* move error message to the caller */
|
||||||
|
}
|
||||||
|
if (stat != LUA_ERRMEM && /* not a memory error and ... */
|
||||||
|
lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
|
||||||
luaL_where(L, 1); /* add extra info, if available */
|
luaL_where(L, 1); /* add extra info, if available */
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
lua_concat(L, 2);
|
lua_concat(L, 2);
|
||||||
@@ -116,7 +120,8 @@ static int luaB_yield (lua_State *L) {
|
|||||||
#define COS_NORM 3
|
#define COS_NORM 3
|
||||||
|
|
||||||
|
|
||||||
static const char *statname[] = {"running", "dead", "suspended", "normal"};
|
static const char *const statname[] =
|
||||||
|
{"running", "dead", "suspended", "normal"};
|
||||||
|
|
||||||
|
|
||||||
static int auxstatus (lua_State *L, lua_State *co) {
|
static int auxstatus (lua_State *L, lua_State *co) {
|
||||||
@@ -167,14 +172,14 @@ static int luaB_close (lua_State *L) {
|
|||||||
int status = auxstatus(L, co);
|
int status = auxstatus(L, co);
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case COS_DEAD: case COS_YIELD: {
|
case COS_DEAD: case COS_YIELD: {
|
||||||
status = lua_resetthread(co);
|
status = lua_closethread(co, L);
|
||||||
if (status == LUA_OK) {
|
if (status == LUA_OK) {
|
||||||
lua_pushboolean(L, 1);
|
lua_pushboolean(L, 1);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lua_pushboolean(L, 0);
|
lua_pushboolean(L, 0);
|
||||||
lua_xmove(co, L, 1); /* copy error message */
|
lua_xmove(co, L, 1); /* move error message */
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,15 @@
|
|||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
|
|
||||||
|
#if defined (LUA_UCID) /* accept UniCode IDentifiers? */
|
||||||
|
/* consider all non-ascii codepoints to be alphabetic */
|
||||||
|
#define NONA 0x01
|
||||||
|
#else
|
||||||
|
#define NONA 0x00 /* default */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
||||||
0x00, /* EOZ */
|
0x00, /* EOZ */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
|
||||||
@@ -34,22 +43,22 @@ LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
|||||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
|
||||||
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
|
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
|
0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
|
NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
/*
|
/*
|
||||||
** WARNING: the functions defined here do not necessarily correspond
|
** WARNING: the functions defined here do not necessarily correspond
|
||||||
** to the similar functions in the standard C ctype.h. They are
|
** to the similar functions in the standard C ctype.h. They are
|
||||||
** optimized for the specific needs of Lua
|
** optimized for the specific needs of Lua.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !defined(LUA_USE_CTYPE)
|
#if !defined(LUA_USE_CTYPE)
|
||||||
@@ -61,13 +61,19 @@
|
|||||||
#define lisprint(c) testprop(c, MASK(PRINTBIT))
|
#define lisprint(c) testprop(c, MASK(PRINTBIT))
|
||||||
#define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
|
#define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** this 'ltolower' only works for alphabetic characters
|
** In ASCII, this 'ltolower' is correct for alphabetic characters and
|
||||||
|
** for '.'. That is enough for Lua needs. ('check_exp' ensures that
|
||||||
|
** the character either is an upper-case letter or is unchanged by
|
||||||
|
** the transformation, which holds for lower-case letters and '.'.)
|
||||||
*/
|
*/
|
||||||
#define ltolower(c) ((c) | ('A' ^ 'a'))
|
#define ltolower(c) \
|
||||||
|
check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \
|
||||||
|
(c) | ('A' ^ 'a'))
|
||||||
|
|
||||||
|
|
||||||
/* two more entries for 0 and -1 (EOZ) */
|
/* one entry for each character and for -1 (EOZ) */
|
||||||
LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];)
|
LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,10 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** The hook table at registry[&HOOKKEY] maps threads to their current
|
** The hook table at registry[HOOKKEY] maps threads to their current
|
||||||
** hook function. (We only need the unique address of 'HOOKKEY'.)
|
** hook function.
|
||||||
*/
|
*/
|
||||||
static const int HOOKKEY = 0;
|
static const char *const HOOKKEY = "_HOOKKEY";
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -33,7 +33,7 @@ static const int HOOKKEY = 0;
|
|||||||
** checked.
|
** checked.
|
||||||
*/
|
*/
|
||||||
static void checkstack (lua_State *L, lua_State *L1, int n) {
|
static void checkstack (lua_State *L, lua_State *L1, int n) {
|
||||||
if (L != L1 && !lua_checkstack(L1, n))
|
if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
|
||||||
luaL_error(L, "stack overflow");
|
luaL_error(L, "stack overflow");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ static int db_setmetatable (lua_State *L) {
|
|||||||
static int db_getuservalue (lua_State *L) {
|
static int db_getuservalue (lua_State *L) {
|
||||||
int n = (int)luaL_optinteger(L, 2, 1);
|
int n = (int)luaL_optinteger(L, 2, 1);
|
||||||
if (lua_type(L, 1) != LUA_TUSERDATA)
|
if (lua_type(L, 1) != LUA_TUSERDATA)
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
|
else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
|
||||||
lua_pushboolean(L, 1);
|
lua_pushboolean(L, 1);
|
||||||
return 2;
|
return 2;
|
||||||
@@ -80,7 +80,7 @@ static int db_setuservalue (lua_State *L) {
|
|||||||
luaL_checkany(L, 2);
|
luaL_checkany(L, 2);
|
||||||
lua_settop(L, 2);
|
lua_settop(L, 2);
|
||||||
if (!lua_setiuservalue(L, 1, n))
|
if (!lua_setiuservalue(L, 1, n))
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,6 +152,7 @@ static int db_getinfo (lua_State *L) {
|
|||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
const char *options = luaL_optstring(L, arg+2, "flnSrtu");
|
const char *options = luaL_optstring(L, arg+2, "flnSrtu");
|
||||||
checkstack(L, L1, 3);
|
checkstack(L, L1, 3);
|
||||||
|
luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
|
||||||
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
|
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
|
||||||
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
|
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
|
||||||
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
|
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
|
||||||
@@ -159,7 +160,7 @@ static int db_getinfo (lua_State *L) {
|
|||||||
}
|
}
|
||||||
else { /* stack level */
|
else { /* stack level */
|
||||||
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
|
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
|
||||||
lua_pushnil(L); /* level out of range */
|
luaL_pushfail(L); /* level out of range */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -202,8 +203,6 @@ static int db_getinfo (lua_State *L) {
|
|||||||
static int db_getlocal (lua_State *L) {
|
static int db_getlocal (lua_State *L) {
|
||||||
int arg;
|
int arg;
|
||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
lua_Debug ar;
|
|
||||||
const char *name;
|
|
||||||
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
|
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
|
||||||
if (lua_isfunction(L, arg + 1)) { /* function argument? */
|
if (lua_isfunction(L, arg + 1)) { /* function argument? */
|
||||||
lua_pushvalue(L, arg + 1); /* push function */
|
lua_pushvalue(L, arg + 1); /* push function */
|
||||||
@@ -211,8 +210,10 @@ static int db_getlocal (lua_State *L) {
|
|||||||
return 1; /* return only name (there is no value) */
|
return 1; /* return only name (there is no value) */
|
||||||
}
|
}
|
||||||
else { /* stack-level argument */
|
else { /* stack-level argument */
|
||||||
|
lua_Debug ar;
|
||||||
|
const char *name;
|
||||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||||
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
|
||||||
return luaL_argerror(L, arg+1, "level out of range");
|
return luaL_argerror(L, arg+1, "level out of range");
|
||||||
checkstack(L, L1, 1);
|
checkstack(L, L1, 1);
|
||||||
name = lua_getlocal(L1, &ar, nvar);
|
name = lua_getlocal(L1, &ar, nvar);
|
||||||
@@ -223,7 +224,7 @@ static int db_getlocal (lua_State *L) {
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lua_pushnil(L); /* no name (nor value) */
|
luaL_pushfail(L); /* no name (nor value) */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -237,7 +238,7 @@ static int db_setlocal (lua_State *L) {
|
|||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||||
int nvar = (int)luaL_checkinteger(L, arg + 2);
|
int nvar = (int)luaL_checkinteger(L, arg + 2);
|
||||||
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
|
||||||
return luaL_argerror(L, arg+1, "level out of range");
|
return luaL_argerror(L, arg+1, "level out of range");
|
||||||
luaL_checkany(L, arg+3);
|
luaL_checkany(L, arg+3);
|
||||||
lua_settop(L, arg+3);
|
lua_settop(L, arg+3);
|
||||||
@@ -281,25 +282,33 @@ static int db_setupvalue (lua_State *L) {
|
|||||||
** Check whether a given upvalue from a given closure exists and
|
** Check whether a given upvalue from a given closure exists and
|
||||||
** returns its index
|
** returns its index
|
||||||
*/
|
*/
|
||||||
static int checkupval (lua_State *L, int argf, int argnup) {
|
static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
|
||||||
|
void *id;
|
||||||
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
|
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
|
||||||
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
|
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
|
||||||
luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
|
id = lua_upvalueid(L, argf, nup);
|
||||||
"invalid upvalue index");
|
if (pnup) {
|
||||||
return nup;
|
luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
|
||||||
|
*pnup = nup;
|
||||||
|
}
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int db_upvalueid (lua_State *L) {
|
static int db_upvalueid (lua_State *L) {
|
||||||
int n = checkupval(L, 1, 2);
|
void *id = checkupval(L, 1, 2, NULL);
|
||||||
lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
|
if (id != NULL)
|
||||||
|
lua_pushlightuserdata(L, id);
|
||||||
|
else
|
||||||
|
luaL_pushfail(L);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int db_upvaluejoin (lua_State *L) {
|
static int db_upvaluejoin (lua_State *L) {
|
||||||
int n1 = checkupval(L, 1, 2);
|
int n1, n2;
|
||||||
int n2 = checkupval(L, 3, 4);
|
checkupval(L, 1, 2, &n1);
|
||||||
|
checkupval(L, 3, 4, &n2);
|
||||||
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
|
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
|
||||||
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
|
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
|
||||||
lua_upvaluejoin(L, 1, n1, 3, n2);
|
lua_upvaluejoin(L, 1, n1, 3, n2);
|
||||||
@@ -314,7 +323,7 @@ static int db_upvaluejoin (lua_State *L) {
|
|||||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||||
static const char *const hooknames[] =
|
static const char *const hooknames[] =
|
||||||
{"call", "return", "line", "count", "tail call"};
|
{"call", "return", "line", "count", "tail call"};
|
||||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
|
||||||
lua_pushthread(L);
|
lua_pushthread(L);
|
||||||
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
||||||
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
||||||
@@ -367,14 +376,12 @@ static int db_sethook (lua_State *L) {
|
|||||||
count = (int)luaL_optinteger(L, arg + 3, 0);
|
count = (int)luaL_optinteger(L, arg + 3, 0);
|
||||||
func = hookf; mask = makemask(smask, count);
|
func = hookf; mask = makemask(smask, count);
|
||||||
}
|
}
|
||||||
if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
|
if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
|
||||||
lua_createtable(L, 0, 2); /* create a hook table */
|
/* table just created; initialize it */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushliteral(L, "k");
|
||||||
lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
|
|
||||||
lua_pushstring(L, "k");
|
|
||||||
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
|
||||||
}
|
}
|
||||||
checkstack(L, L1, 1);
|
checkstack(L, L1, 1);
|
||||||
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
|
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
|
||||||
@@ -391,12 +398,14 @@ static int db_gethook (lua_State *L) {
|
|||||||
char buff[5];
|
char buff[5];
|
||||||
int mask = lua_gethookmask(L1);
|
int mask = lua_gethookmask(L1);
|
||||||
lua_Hook hook = lua_gethook(L1);
|
lua_Hook hook = lua_gethook(L1);
|
||||||
if (hook == NULL) /* no hook? */
|
if (hook == NULL) { /* no hook? */
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
else if (hook != hookf) /* external hook? */
|
else if (hook != hookf) /* external hook? */
|
||||||
lua_pushliteral(L, "external hook");
|
lua_pushliteral(L, "external hook");
|
||||||
else { /* hook table must exist */
|
else { /* hook table must exist */
|
||||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
|
||||||
checkstack(L, L1, 1);
|
checkstack(L, L1, 1);
|
||||||
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||||
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
||||||
@@ -412,12 +421,12 @@ static int db_debug (lua_State *L) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
char buffer[250];
|
char buffer[250];
|
||||||
lua_writestringerror("%s", "lua_debug> ");
|
lua_writestringerror("%s", "lua_debug> ");
|
||||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
|
||||||
strcmp(buffer, "cont\n") == 0)
|
strcmp(buffer, "cont\n") == 0)
|
||||||
return 0;
|
return 0;
|
||||||
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
||||||
lua_pcall(L, 0, 0, 0))
|
lua_pcall(L, 0, 0, 0))
|
||||||
lua_writestringerror("%s\n", lua_tostring(L, -1));
|
lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
|
||||||
lua_settop(L, 0); /* remove eventual returns */
|
lua_settop(L, 0); /* remove eventual returns */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -437,6 +446,14 @@ 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[] = {
|
static const luaL_Reg dblib[] = {
|
||||||
{"debug", db_debug},
|
{"debug", db_debug},
|
||||||
{"getuservalue", db_getuservalue},
|
{"getuservalue", db_getuservalue},
|
||||||
@@ -454,6 +471,7 @@ static const luaL_Reg dblib[] = {
|
|||||||
{"setmetatable", db_setmetatable},
|
{"setmetatable", db_setmetatable},
|
||||||
{"setupvalue", db_setupvalue},
|
{"setupvalue", db_setupvalue},
|
||||||
{"traceback", db_traceback},
|
{"traceback", db_traceback},
|
||||||
|
{"setcstacklimit", db_setcstacklimit},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -31,15 +31,14 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL)
|
||||||
|
|
||||||
|
|
||||||
/* Active Lua function (given call info) */
|
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
|
||||||
#define ci_func(ci) (clLvalue(s2v((ci)->func)))
|
const char **name);
|
||||||
|
|
||||||
|
static const char strlocal[] = "local";
|
||||||
static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
|
static const char strupval[] = "upvalue";
|
||||||
const char **name);
|
|
||||||
|
|
||||||
|
|
||||||
static int currentpc (CallInfo *ci) {
|
static int currentpc (CallInfo *ci) {
|
||||||
@@ -50,10 +49,16 @@ static int currentpc (CallInfo *ci) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Get a "base line" to find the line corresponding to an instruction.
|
** Get a "base line" to find the line corresponding to an instruction.
|
||||||
** For that, search the array of absolute line info for the largest saved
|
** Base lines are regularly placed at MAXIWTHABS intervals, so usually
|
||||||
** instruction smaller or equal to the wanted instruction. A special
|
** an integer division gets the right place. When the source file has
|
||||||
** case is when there is no absolute info or the instruction is before
|
** large sequences of empty/comment lines, it may need extra entries,
|
||||||
** the first absolute one.
|
** so the original estimate needs a correction.
|
||||||
|
** If the original estimate is -1, the initial 'if' ensures that the
|
||||||
|
** 'while' will run at least once.
|
||||||
|
** The assertion that the estimate is a lower bound for the correct base
|
||||||
|
** is valid as long as the debug info has been generated with the same
|
||||||
|
** value for MAXIWTHABS or smaller. (Previous releases use a little
|
||||||
|
** smaller value.)
|
||||||
*/
|
*/
|
||||||
static int getbaseline (const Proto *f, int pc, int *basepc) {
|
static int getbaseline (const Proto *f, int pc, int *basepc) {
|
||||||
if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
|
if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
|
||||||
@@ -61,20 +66,12 @@ static int getbaseline (const Proto *f, int pc, int *basepc) {
|
|||||||
return f->linedefined;
|
return f->linedefined;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
unsigned int i;
|
int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */
|
||||||
if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc)
|
/* estimate must be a lower bound of the correct base */
|
||||||
i = f->sizeabslineinfo - 1; /* instruction is after last saved one */
|
lua_assert(i < 0 ||
|
||||||
else { /* binary search */
|
(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
|
||||||
unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */
|
while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc)
|
||||||
i = 0; /* abslineinfo[i] <= pc */
|
i++; /* low estimate; adjust it */
|
||||||
while (i < j - 1) {
|
|
||||||
unsigned int m = (j + i) / 2;
|
|
||||||
if (pc >= f->abslineinfo[m].pc)
|
|
||||||
i = m;
|
|
||||||
else
|
|
||||||
j = m;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*basepc = f->abslineinfo[i].pc;
|
*basepc = f->abslineinfo[i].pc;
|
||||||
return f->abslineinfo[i].line;
|
return f->abslineinfo[i].line;
|
||||||
}
|
}
|
||||||
@@ -101,19 +98,21 @@ int luaG_getfuncline (const Proto *f, int pc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int currentline (CallInfo *ci) {
|
static int getcurrentline (CallInfo *ci) {
|
||||||
return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
|
return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** This function can be called asynchronously (e.g. during a signal),
|
** Set 'trap' for all active Lua frames.
|
||||||
** under "reasonable" assumptions. A new 'ci' is completely linked
|
** This function can be called during a signal, under "reasonable"
|
||||||
** in the list before it becomes part of the "active" list, and
|
** assumptions. A new 'ci' is completely linked in the list before it
|
||||||
** we assume that pointers are atomic (see comment in next function).
|
** becomes part of the "active" list, and we assume that pointers are
|
||||||
** (If we traverse one more item, there is no problem. If we traverse
|
** atomic; see comment in next function.
|
||||||
** one less item, the worst that can happen is that the signal will
|
** (A compiler doing interprocedural optimizations could, theoretically,
|
||||||
** not interrupt the script.)
|
** reorder memory writes in such a way that the list could be
|
||||||
|
** temporarily broken while inserting a new element. We simply assume it
|
||||||
|
** has no good reasons to do that.)
|
||||||
*/
|
*/
|
||||||
static void settraps (CallInfo *ci) {
|
static void settraps (CallInfo *ci) {
|
||||||
for (; ci != NULL; ci = ci->previous)
|
for (; ci != NULL; ci = ci->previous)
|
||||||
@@ -123,22 +122,20 @@ static void settraps (CallInfo *ci) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** This function can be called asynchronously (e.g. during a signal),
|
** This function can be called during a signal, under "reasonable"
|
||||||
** under "reasonable" assumptions.
|
** assumptions.
|
||||||
** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
|
** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount')
|
||||||
** 'resethookcount') are for debug only, and it is no problem if they
|
** are for debug only, and it is no problem if they get arbitrary
|
||||||
** get arbitrary values (causes at most one wrong hook call). 'hookmask'
|
** values (causes at most one wrong hook call). 'hookmask' is an atomic
|
||||||
** is an atomic value. We assume that pointers are atomic too (e.g., gcc
|
** value. We assume that pointers are atomic too (e.g., gcc ensures that
|
||||||
** ensures that for all platforms where it runs). Moreover, 'hook' is
|
** for all platforms where it runs). Moreover, 'hook' is always checked
|
||||||
** always checked before being called (see 'luaD_hook').
|
** before being called (see 'luaD_hook').
|
||||||
*/
|
*/
|
||||||
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
|
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
|
||||||
if (func == NULL || mask == 0) { /* turn off hooks? */
|
if (func == NULL || mask == 0) { /* turn off hooks? */
|
||||||
mask = 0;
|
mask = 0;
|
||||||
func = NULL;
|
func = NULL;
|
||||||
}
|
}
|
||||||
if (isLua(L->ci))
|
|
||||||
L->oldpc = L->ci->u.l.savedpc;
|
|
||||||
L->hook = func;
|
L->hook = func;
|
||||||
L->basehookcount = count;
|
L->basehookcount = count;
|
||||||
resethookcount(L);
|
resethookcount(L);
|
||||||
@@ -188,10 +185,10 @@ static const char *upvalname (const Proto *p, int uv) {
|
|||||||
|
|
||||||
|
|
||||||
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
||||||
if (clLvalue(s2v(ci->func))->p->is_vararg) {
|
if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
|
||||||
int nextra = ci->u.l.nextraargs;
|
int nextra = ci->u.l.nextraargs;
|
||||||
if (n <= nextra) {
|
if (n >= -nextra) { /* 'n' is negative */
|
||||||
*pos = ci->func - nextra + (n - 1);
|
*pos = ci->func.p - nextra - (n + 1);
|
||||||
return "(vararg)"; /* generic name for any vararg */
|
return "(vararg)"; /* generic name for any vararg */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,16 +197,16 @@ static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
|||||||
|
|
||||||
|
|
||||||
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
|
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
|
||||||
StkId base = ci->func + 1;
|
StkId base = ci->func.p + 1;
|
||||||
const char *name = NULL;
|
const char *name = NULL;
|
||||||
if (isLua(ci)) {
|
if (isLua(ci)) {
|
||||||
if (n < 0) /* access to vararg values? */
|
if (n < 0) /* access to vararg values? */
|
||||||
return findvararg(ci, -n, pos);
|
return findvararg(ci, n, pos);
|
||||||
else
|
else
|
||||||
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
|
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
|
||||||
}
|
}
|
||||||
if (name == NULL) { /* no 'standard' name? */
|
if (name == NULL) { /* no 'standard' name? */
|
||||||
StkId limit = (ci == L->ci) ? L->top : ci->next->func;
|
StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
|
||||||
if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
|
if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
|
||||||
/* generic name for any valid slot */
|
/* generic name for any valid slot */
|
||||||
name = isLua(ci) ? "(temporary)" : "(C temporary)";
|
name = isLua(ci) ? "(temporary)" : "(C temporary)";
|
||||||
@@ -227,16 +224,16 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
|||||||
const char *name;
|
const char *name;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (ar == NULL) { /* information about non-active function? */
|
if (ar == NULL) { /* information about non-active function? */
|
||||||
if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */
|
if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */
|
||||||
name = NULL;
|
name = NULL;
|
||||||
else /* consider live variables at function start (parameters) */
|
else /* consider live variables at function start (parameters) */
|
||||||
name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0);
|
name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0);
|
||||||
}
|
}
|
||||||
else { /* active function; get information through 'ar' */
|
else { /* active function; get information through 'ar' */
|
||||||
StkId pos = NULL; /* to avoid warnings */
|
StkId pos = NULL; /* to avoid warnings */
|
||||||
name = luaG_findlocal(L, ar->i_ci, n, &pos);
|
name = luaG_findlocal(L, ar->i_ci, n, &pos);
|
||||||
if (name) {
|
if (name) {
|
||||||
setobjs2s(L, L->top, pos);
|
setobjs2s(L, L->top.p, pos);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -251,8 +248,8 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
name = luaG_findlocal(L, ar->i_ci, n, &pos);
|
name = luaG_findlocal(L, ar->i_ci, n, &pos);
|
||||||
if (name) {
|
if (name) {
|
||||||
setobjs2s(L, pos, L->top - 1);
|
setobjs2s(L, pos, L->top.p - 1);
|
||||||
L->top--; /* pop value */
|
L->top.p--; /* pop value */
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return name;
|
return name;
|
||||||
@@ -260,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) {
|
static void funcinfo (lua_Debug *ar, Closure *cl) {
|
||||||
if (noLuaClosure(cl)) {
|
if (!LuaClosure(cl)) {
|
||||||
ar->source = "=[C]";
|
ar->source = "=[C]";
|
||||||
ar->srclen = LL("=[C]");
|
ar->srclen = LL("=[C]");
|
||||||
ar->linedefined = -1;
|
ar->linedefined = -1;
|
||||||
@@ -294,37 +291,40 @@ static int nextline (const Proto *p, int currentline, int pc) {
|
|||||||
|
|
||||||
|
|
||||||
static void collectvalidlines (lua_State *L, Closure *f) {
|
static void collectvalidlines (lua_State *L, Closure *f) {
|
||||||
if (noLuaClosure(f)) {
|
if (!LuaClosure(f)) {
|
||||||
setnilvalue(s2v(L->top));
|
setnilvalue(s2v(L->top.p));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int i;
|
|
||||||
TValue v;
|
|
||||||
const Proto *p = f->l.p;
|
const Proto *p = f->l.p;
|
||||||
int currentline = p->linedefined;
|
int currentline = p->linedefined;
|
||||||
Table *t = luaH_new(L); /* new table to store active lines */
|
Table *t = luaH_new(L); /* new table to store active lines */
|
||||||
sethvalue2s(L, L->top, t); /* push it on stack */
|
sethvalue2s(L, L->top.p, t); /* push it on stack */
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
|
if (p->lineinfo != NULL) { /* proto with debug information? */
|
||||||
for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */
|
int i;
|
||||||
currentline = nextline(p, currentline, i);
|
TValue v;
|
||||||
luaH_setint(L, t, currentline, &v); /* table[line] = true */
|
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 */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
||||||
if (ci == NULL) /* no 'ci'? */
|
/* calling function is a known function? */
|
||||||
return NULL; /* no info */
|
if (ci != NULL && !(ci->callstatus & CIST_TAIL))
|
||||||
else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */
|
return funcnamefromcall(L, ci->previous, name);
|
||||||
*name = "__gc";
|
|
||||||
return "metamethod"; /* report it as such */
|
|
||||||
}
|
|
||||||
/* calling function is a known Lua function? */
|
|
||||||
else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
|
|
||||||
return funcnamefromcode(L, ci->previous, name);
|
|
||||||
else return NULL; /* no way to find a name */
|
else return NULL; /* no way to find a name */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,12 +339,12 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'l': {
|
case 'l': {
|
||||||
ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
|
ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'u': {
|
case 'u': {
|
||||||
ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
|
ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
|
||||||
if (noLuaClosure(f)) {
|
if (!LuaClosure(f)) {
|
||||||
ar->isvararg = 1;
|
ar->isvararg = 1;
|
||||||
ar->nparams = 0;
|
ar->nparams = 0;
|
||||||
}
|
}
|
||||||
@@ -393,20 +393,20 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (*what == '>') {
|
if (*what == '>') {
|
||||||
ci = NULL;
|
ci = NULL;
|
||||||
func = s2v(L->top - 1);
|
func = s2v(L->top.p - 1);
|
||||||
api_check(L, ttisfunction(func), "function expected");
|
api_check(L, ttisfunction(func), "function expected");
|
||||||
what++; /* skip the '>' */
|
what++; /* skip the '>' */
|
||||||
L->top--; /* pop function */
|
L->top.p--; /* pop function */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ci = ar->i_ci;
|
ci = ar->i_ci;
|
||||||
func = s2v(ci->func);
|
func = s2v(ci->func.p);
|
||||||
lua_assert(ttisfunction(func));
|
lua_assert(ttisfunction(func));
|
||||||
}
|
}
|
||||||
cl = ttisclosure(func) ? clvalue(func) : NULL;
|
cl = ttisclosure(func) ? clvalue(func) : NULL;
|
||||||
status = auxgetinfo(L, what, ar, cl, ci);
|
status = auxgetinfo(L, what, ar, cl, ci);
|
||||||
if (strchr(what, 'f')) {
|
if (strchr(what, 'f')) {
|
||||||
setobj2s(L, L->top, func);
|
setobj2s(L, L->top.p, func);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
if (strchr(what, 'L'))
|
if (strchr(what, 'L'))
|
||||||
@@ -422,40 +422,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) {
|
static int filterpc (int pc, int jmptarget) {
|
||||||
if (pc < jmptarget) /* is code conditional (inside a jump)? */
|
if (pc < jmptarget) /* is code conditional (inside a jump)? */
|
||||||
@@ -465,12 +431,14 @@ static int filterpc (int pc, int jmptarget) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** try to find last instruction before 'lastpc' that modified register 'reg'
|
** Try to find last instruction before 'lastpc' that modified register 'reg'.
|
||||||
*/
|
*/
|
||||||
static int findsetreg (const Proto *p, int lastpc, int reg) {
|
static int findsetreg (const Proto *p, int lastpc, int reg) {
|
||||||
int pc;
|
int pc;
|
||||||
int setreg = -1; /* keep last instruction that changed 'reg' */
|
int setreg = -1; /* keep last instruction that changed 'reg' */
|
||||||
int jmptarget = 0; /* any code before this address is conditional */
|
int jmptarget = 0; /* any code before this address is conditional */
|
||||||
|
if (testMMMode(GET_OPCODE(p->code[lastpc])))
|
||||||
|
lastpc--; /* previous instruction was not actually executed */
|
||||||
for (pc = 0; pc < lastpc; pc++) {
|
for (pc = 0; pc < lastpc; pc++) {
|
||||||
Instruction i = p->code[pc];
|
Instruction i = p->code[pc];
|
||||||
OpCode op = GET_OPCODE(i);
|
OpCode op = GET_OPCODE(i);
|
||||||
@@ -512,28 +480,29 @@ static int findsetreg (const Proto *p, int lastpc, int reg) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check whether table being indexed by instruction 'i' is the
|
** Find a "name" for the constant 'c'.
|
||||||
** environment '_ENV'
|
|
||||||
*/
|
*/
|
||||||
static const char *gxf (const Proto *p, int pc, Instruction i, int isup) {
|
static const char *kname (const Proto *p, int index, const char **name) {
|
||||||
int t = GETARG_B(i); /* table index */
|
TValue *kvalue = &p->k[index];
|
||||||
const char *name; /* name of indexed variable */
|
if (ttisstring(kvalue)) {
|
||||||
if (isup) /* is an upvalue? */
|
*name = getstr(tsvalue(kvalue));
|
||||||
name = upvalname(p, t);
|
return "constant";
|
||||||
else
|
}
|
||||||
getobjname(p, pc, t, &name);
|
else {
|
||||||
return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field";
|
*name = "?";
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *getobjname (const Proto *p, int lastpc, int reg,
|
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
|
||||||
const char **name) {
|
const char **name) {
|
||||||
int pc;
|
int pc = *ppc;
|
||||||
*name = luaF_getlocalname(p, reg + 1, lastpc);
|
*name = luaF_getlocalname(p, reg + 1, pc);
|
||||||
if (*name) /* is a local? */
|
if (*name) /* is a local? */
|
||||||
return "local";
|
return strlocal;
|
||||||
/* else try symbolic execution */
|
/* else try symbolic execution */
|
||||||
pc = findsetreg(p, lastpc, reg);
|
*ppc = pc = findsetreg(p, pc, reg);
|
||||||
if (pc != -1) { /* could find instruction? */
|
if (pc != -1) { /* could find instruction? */
|
||||||
Instruction i = p->code[pc];
|
Instruction i = p->code[pc];
|
||||||
OpCode op = GET_OPCODE(i);
|
OpCode op = GET_OPCODE(i);
|
||||||
@@ -541,18 +510,86 @@ static const char *getobjname (const Proto *p, int lastpc, int reg,
|
|||||||
case OP_MOVE: {
|
case OP_MOVE: {
|
||||||
int b = GETARG_B(i); /* move from 'b' to 'a' */
|
int b = GETARG_B(i); /* move from 'b' to 'a' */
|
||||||
if (b < GETARG_A(i))
|
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;
|
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 = "?";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Check whether table being indexed by instruction 'i' is the
|
||||||
|
** environment '_ENV'. If the table is an upvalue, get its name;
|
||||||
|
** otherwise, find some "name" for the table and check whether
|
||||||
|
** that name is the name of a local variable (and not, for instance,
|
||||||
|
** a string). Then check that, if there is a name, it is '_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);
|
||||||
|
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: {
|
case OP_GETTABUP: {
|
||||||
int k = GETARG_C(i); /* key index */
|
int k = GETARG_C(i); /* key index */
|
||||||
kname(p, k, name);
|
kname(p, k, name);
|
||||||
return gxf(p, pc, i, 1);
|
return isEnv(p, lastpc, i, 1);
|
||||||
}
|
}
|
||||||
case OP_GETTABLE: {
|
case OP_GETTABLE: {
|
||||||
int k = GETARG_C(i); /* key index */
|
int k = GETARG_C(i); /* key index */
|
||||||
rname(p, pc, k, name);
|
rname(p, lastpc, k, name);
|
||||||
return gxf(p, pc, i, 0);
|
return isEnv(p, lastpc, i, 0);
|
||||||
}
|
}
|
||||||
case OP_GETI: {
|
case OP_GETI: {
|
||||||
*name = "integer index";
|
*name = "integer index";
|
||||||
@@ -561,24 +598,10 @@ static const char *getobjname (const Proto *p, int lastpc, int reg,
|
|||||||
case OP_GETFIELD: {
|
case OP_GETFIELD: {
|
||||||
int k = GETARG_C(i); /* key index */
|
int k = GETARG_C(i); /* key index */
|
||||||
kname(p, k, name);
|
kname(p, k, name);
|
||||||
return gxf(p, pc, i, 0);
|
return isEnv(p, lastpc, 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;
|
|
||||||
}
|
}
|
||||||
case OP_SELF: {
|
case OP_SELF: {
|
||||||
rkname(p, pc, i, name);
|
rkname(p, lastpc, i, name);
|
||||||
return "method";
|
return "method";
|
||||||
}
|
}
|
||||||
default: break; /* go through to return NULL */
|
default: break; /* go through to return NULL */
|
||||||
@@ -594,16 +617,10 @@ static const char *getobjname (const Proto *p, int lastpc, int reg,
|
|||||||
** Returns what the name is (e.g., "for iterator", "method",
|
** Returns what the name is (e.g., "for iterator", "method",
|
||||||
** "metamethod") and sets '*name' to point to the name.
|
** "metamethod") and sets '*name' to point to the name.
|
||||||
*/
|
*/
|
||||||
static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
|
static const char *funcnamefromcode (lua_State *L, const Proto *p,
|
||||||
const char **name) {
|
int pc, const char **name) {
|
||||||
TMS tm = (TMS)0; /* (initial value avoids warnings) */
|
TMS tm = (TMS)0; /* (initial value avoids warnings) */
|
||||||
const Proto *p = ci_func(ci)->p; /* calling function */
|
|
||||||
int pc = currentpc(ci); /* calling instruction index */
|
|
||||||
Instruction i = p->code[pc]; /* calling instruction */
|
Instruction i = p->code[pc]; /* calling instruction */
|
||||||
if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
|
|
||||||
*name = "?";
|
|
||||||
return "hook";
|
|
||||||
}
|
|
||||||
switch (GET_OPCODE(i)) {
|
switch (GET_OPCODE(i)) {
|
||||||
case OP_CALL:
|
case OP_CALL:
|
||||||
case OP_TAILCALL:
|
case OP_TAILCALL:
|
||||||
@@ -620,24 +637,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
|
|||||||
case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
|
case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
|
||||||
tm = TM_NEWINDEX;
|
tm = TM_NEWINDEX;
|
||||||
break;
|
break;
|
||||||
case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI:
|
case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
|
||||||
case OP_POWI: case OP_DIVI: case OP_IDIVI: {
|
tm = cast(TMS, GETARG_C(i));
|
||||||
int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */
|
|
||||||
tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OP_ADDK: case OP_SUBK: case OP_MULK: case OP_MODK:
|
|
||||||
case OP_POWK: case OP_DIVK: case OP_IDIVK:
|
|
||||||
case OP_BANDK: case OP_BORK: case OP_BXORK: {
|
|
||||||
int offset = GET_OPCODE(i) - OP_ADDK; /* ORDER OP */
|
|
||||||
tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
|
|
||||||
case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
|
|
||||||
case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
|
|
||||||
int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */
|
|
||||||
tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_UNM: tm = TM_UNM; break;
|
case OP_UNM: tm = TM_UNM; break;
|
||||||
@@ -645,32 +646,55 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
|
|||||||
case OP_LEN: tm = TM_LEN; break;
|
case OP_LEN: tm = TM_LEN; break;
|
||||||
case OP_CONCAT: tm = TM_CONCAT; break;
|
case OP_CONCAT: tm = TM_CONCAT; break;
|
||||||
case OP_EQ: tm = TM_EQ; break;
|
case OP_EQ: tm = TM_EQ; break;
|
||||||
case OP_LT: case OP_LE: case OP_LTI: case OP_LEI:
|
/* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
|
||||||
*name = "order"; /* '<=' can call '__lt', etc. */
|
case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
|
||||||
return "metamethod";
|
case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
|
||||||
case OP_SHRI: case OP_SHLI:
|
case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
|
||||||
*name = "shift";
|
|
||||||
return "metamethod";
|
|
||||||
default:
|
default:
|
||||||
return NULL; /* cannot find a reasonable name */
|
return NULL; /* cannot find a reasonable name */
|
||||||
}
|
}
|
||||||
*name = getstr(G(L)->tmname[tm]) + 2;
|
*name = getshrstr(G(L)->tmname[tm]) + 2;
|
||||||
return "metamethod";
|
return "metamethod";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Try to find a name for a function based on how it was called.
|
||||||
|
*/
|
||||||
|
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
|
||||||
|
const char **name) {
|
||||||
|
if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
|
||||||
|
*name = "?";
|
||||||
|
return "hook";
|
||||||
|
}
|
||||||
|
else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */
|
||||||
|
*name = "__gc";
|
||||||
|
return "metamethod"; /* report it as such */
|
||||||
|
}
|
||||||
|
else if (isLua(ci))
|
||||||
|
return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** The subtraction of two potentially unrelated pointers is
|
** Check whether pointer 'o' points to some value in the stack frame of
|
||||||
** not ISO C, but it should not crash a program; the subsequent
|
** the current function and, if so, returns its index. Because 'o' may
|
||||||
** checks are ISO C and ensure a correct result.
|
** not point to a value in this stack, we cannot compare it with the
|
||||||
|
** region boundaries (undefined behavior in ISO C).
|
||||||
*/
|
*/
|
||||||
static int isinstack (CallInfo *ci, const TValue *o) {
|
static int instack (CallInfo *ci, const TValue *o) {
|
||||||
StkId base = ci->func + 1;
|
int pos;
|
||||||
ptrdiff_t i = cast(StkId, o) - base;
|
StkId base = ci->func.p + 1;
|
||||||
return (0 <= i && i < (ci->top - base) && s2v(base + i) == o);
|
for (pos = 0; base + pos < ci->top.p; pos++) {
|
||||||
|
if (o == s2v(base + pos))
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
return -1; /* not found */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -684,32 +708,73 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
|
|||||||
LClosure *c = ci_func(ci);
|
LClosure *c = ci_func(ci);
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < c->nupvalues; i++) {
|
for (i = 0; i < c->nupvalues; i++) {
|
||||||
if (c->upvals[i]->v == o) {
|
if (c->upvals[i]->v.p == o) {
|
||||||
*name = upvalname(c->p, i);
|
*name = upvalname(c->p, i);
|
||||||
return "upvalue";
|
return strupval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *formatvarinfo (lua_State *L, const char *kind,
|
||||||
|
const char *name) {
|
||||||
|
if (kind == NULL)
|
||||||
|
return ""; /* no information */
|
||||||
|
else
|
||||||
|
return luaO_pushfstring(L, " (%s '%s')", kind, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Build a string with a "description" for the value 'o', such as
|
||||||
|
** "variable 'x'" or "upvalue 'y'".
|
||||||
|
*/
|
||||||
static const char *varinfo (lua_State *L, const TValue *o) {
|
static const char *varinfo (lua_State *L, const TValue *o) {
|
||||||
const char *name = NULL; /* to avoid warnings */
|
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
|
const char *name = NULL; /* to avoid warnings */
|
||||||
const char *kind = NULL;
|
const char *kind = NULL;
|
||||||
if (isLua(ci)) {
|
if (isLua(ci)) {
|
||||||
kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
|
kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
|
||||||
if (!kind && isinstack(ci, o)) /* no? try a register */
|
if (!kind) { /* not an upvalue? */
|
||||||
kind = getobjname(ci_func(ci)->p, currentpc(ci),
|
int reg = instack(ci, o); /* try a register */
|
||||||
cast_int(cast(StkId, o) - (ci->func + 1)), &name);
|
if (reg >= 0) /* is 'o' a register? */
|
||||||
|
kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
|
return formatvarinfo(L, kind, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
/*
|
||||||
|
** Raise a type error
|
||||||
|
*/
|
||||||
|
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
|
||||||
|
const char *extra) {
|
||||||
const char *t = luaT_objtypename(L, o);
|
const char *t = luaT_objtypename(L, o);
|
||||||
luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
|
luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Raise a type error with "standard" information about the faulty
|
||||||
|
** object 'o' (using 'varinfo').
|
||||||
|
*/
|
||||||
|
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
||||||
|
typeerror(L, o, op, varinfo(L, o));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Raise an error for calling a non-callable object. Try to find a name
|
||||||
|
** for the object based on how it was called ('funcnamefromcall'); if it
|
||||||
|
** cannot get a name there, try 'varinfo'.
|
||||||
|
*/
|
||||||
|
l_noret luaG_callerror (lua_State *L, const TValue *o) {
|
||||||
|
CallInfo *ci = L->ci;
|
||||||
|
const char *name = NULL; /* to avoid warnings */
|
||||||
|
const char *kind = funcnamefromcall(L, ci, &name);
|
||||||
|
const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o);
|
||||||
|
typeerror(L, o, "call", extra);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -738,7 +803,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1,
|
|||||||
*/
|
*/
|
||||||
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
|
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||||
lua_Integer temp;
|
lua_Integer temp;
|
||||||
if (!tointegerns(p1, &temp))
|
if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
|
||||||
p2 = p1;
|
p2 = p1;
|
||||||
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
|
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
|
||||||
}
|
}
|
||||||
@@ -771,10 +836,10 @@ l_noret luaG_errormsg (lua_State *L) {
|
|||||||
if (L->errfunc != 0) { /* is there an error handling function? */
|
if (L->errfunc != 0) { /* is there an error handling function? */
|
||||||
StkId errfunc = restorestack(L, L->errfunc);
|
StkId errfunc = restorestack(L, L->errfunc);
|
||||||
lua_assert(ttisfunction(s2v(errfunc)));
|
lua_assert(ttisfunction(s2v(errfunc)));
|
||||||
setobjs2s(L, L->top, L->top - 1); /* move argument */
|
setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */
|
||||||
setobjs2s(L, L->top - 1, errfunc); /* push function */
|
setobjs2s(L, L->top.p - 1, errfunc); /* push function */
|
||||||
L->top++; /* assume EXTRA_STACK */
|
L->top.p++; /* assume EXTRA_STACK */
|
||||||
luaD_callnoyield(L, L->top - 2, 1); /* call it */
|
luaD_callnoyield(L, L->top.p - 2, 1); /* call it */
|
||||||
}
|
}
|
||||||
luaD_throw(L, LUA_ERRRUN);
|
luaD_throw(L, LUA_ERRRUN);
|
||||||
}
|
}
|
||||||
@@ -788,28 +853,82 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
|||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
|
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
|
||||||
va_end(argp);
|
va_end(argp);
|
||||||
if (isLua(ci)) /* if Lua function, add source:line information */
|
if (isLua(ci)) { /* if Lua function, add source:line information */
|
||||||
luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
|
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--;
|
||||||
|
}
|
||||||
luaG_errormsg(L);
|
luaG_errormsg(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check whether new instruction 'newpc' is in a different line from
|
** Check whether new instruction 'newpc' is in a different line from
|
||||||
** previous instruction 'oldpc'.
|
** previous instruction 'oldpc'. More often than not, 'newpc' is only
|
||||||
|
** one or a few instructions after 'oldpc' (it must be after, see
|
||||||
|
** caller), so try to avoid calling 'luaG_getfuncline'. If they are
|
||||||
|
** too far apart, there is a good chance of a ABSLINEINFO in the way,
|
||||||
|
** so it goes directly to 'luaG_getfuncline'.
|
||||||
*/
|
*/
|
||||||
static int changedline (const Proto *p, int oldpc, int newpc) {
|
static int changedline (const Proto *p, int oldpc, int newpc) {
|
||||||
while (oldpc++ < newpc) {
|
if (p->lineinfo == NULL) /* no debug information? */
|
||||||
if (p->lineinfo[oldpc] != 0)
|
return 0;
|
||||||
return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc));
|
if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */
|
||||||
|
int delta = 0; /* line difference */
|
||||||
|
int pc = oldpc;
|
||||||
|
for (;;) {
|
||||||
|
int lineinfo = p->lineinfo[++pc];
|
||||||
|
if (lineinfo == ABSLINEINFO)
|
||||||
|
break; /* cannot compute delta; fall through */
|
||||||
|
delta += lineinfo;
|
||||||
|
if (pc == newpc)
|
||||||
|
return (delta != 0); /* delta computed successfully */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0; /* no line changes in the way */
|
/* either instructions are too far apart or there is an absolute line
|
||||||
|
info in the way; compute line difference explicitly */
|
||||||
|
return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, 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 (p->is_vararg)
|
||||||
|
return 0; /* hooks will start at VARARGPREP instruction */
|
||||||
|
else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yieded? */
|
||||||
|
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
|
||||||
|
** instruction traced, to detect line changes. When entering a new
|
||||||
|
** function, 'npci' will be zero and will test as a new line whatever
|
||||||
|
** the value of 'oldpc'. Some exceptional conditions may return to
|
||||||
|
** a function without setting 'oldpc'. In that case, 'oldpc' may be
|
||||||
|
** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
|
||||||
|
** at most causes an extra call to a line hook.)
|
||||||
|
** This function is not "Protected" when called, so it should correct
|
||||||
|
** 'L->top.p' before calling anything that can run the GC.
|
||||||
|
*/
|
||||||
int luaG_traceexec (lua_State *L, const Instruction *pc) {
|
int luaG_traceexec (lua_State *L, const Instruction *pc) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
lu_byte mask = L->hookmask;
|
lu_byte mask = L->hookmask;
|
||||||
|
const Proto *p = ci_func(ci)->p;
|
||||||
int counthook;
|
int counthook;
|
||||||
if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
|
if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
|
||||||
ci->u.l.trap = 0; /* don't need to stop again */
|
ci->u.l.trap = 0; /* don't need to stop again */
|
||||||
@@ -817,34 +936,33 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) {
|
|||||||
}
|
}
|
||||||
pc++; /* reference is always next instruction */
|
pc++; /* reference is always next instruction */
|
||||||
ci->u.l.savedpc = pc; /* save 'pc' */
|
ci->u.l.savedpc = pc; /* save 'pc' */
|
||||||
counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
|
counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
|
||||||
if (counthook)
|
if (counthook)
|
||||||
resethookcount(L); /* reset count */
|
resethookcount(L); /* reset count */
|
||||||
else if (!(mask & LUA_MASKLINE))
|
else if (!(mask & LUA_MASKLINE))
|
||||||
return 1; /* no line hook and count != 0; nothing to be done now */
|
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 */
|
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
|
||||||
return 1; /* do not call hook again (VM yielded, so it did not move) */
|
return 1; /* do not call hook again (VM yielded, so it did not move) */
|
||||||
}
|
}
|
||||||
if (!isIT(*(ci->u.l.savedpc - 1)))
|
if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
|
||||||
L->top = ci->top; /* prepare top */
|
L->top.p = ci->top.p; /* correct top */
|
||||||
if (counthook)
|
if (counthook)
|
||||||
luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
|
luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
|
||||||
if (mask & LUA_MASKLINE) {
|
if (mask & LUA_MASKLINE) {
|
||||||
const Proto *p = ci_func(ci)->p;
|
/* 'L->oldpc' may be invalid; use zero in this case */
|
||||||
|
int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0;
|
||||||
int npci = pcRel(pc, p);
|
int npci = pcRel(pc, p);
|
||||||
if (npci == 0 || /* call linehook when enter a new function, */
|
if (npci <= oldpc || /* call hook when jump back (loop), */
|
||||||
pc <= L->oldpc || /* when jump back (loop), or when */
|
changedline(p, oldpc, npci)) { /* or when enter new line */
|
||||||
changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */
|
|
||||||
int newline = luaG_getfuncline(p, npci);
|
int newline = luaG_getfuncline(p, npci);
|
||||||
luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
|
luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */
|
||||||
}
|
}
|
||||||
L->oldpc = pc; /* 'pc' of last call to line hook */
|
L->oldpc = npci; /* 'pc' of last call to line hook */
|
||||||
}
|
}
|
||||||
if (L->status == LUA_YIELD) { /* did hook yield? */
|
if (L->status == LUA_YIELD) { /* did hook yield? */
|
||||||
if (counthook)
|
if (counthook)
|
||||||
L->hookcount = 1; /* undo decrement to zero */
|
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 */
|
ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
|
||||||
luaD_throw(L, LUA_YIELD);
|
luaD_throw(L, LUA_YIELD);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,11 @@
|
|||||||
|
|
||||||
#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1)
|
#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
/* Active Lua function (given call info) */
|
||||||
|
#define ci_func(ci) (clLvalue(s2v((ci)->func.p)))
|
||||||
|
|
||||||
|
|
||||||
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -21,11 +26,22 @@
|
|||||||
*/
|
*/
|
||||||
#define ABSLINEINFO (-0x80)
|
#define ABSLINEINFO (-0x80)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** MAXimum number of successive Instructions WiTHout ABSolute line
|
||||||
|
** information. (A power of two allows fast divisions.)
|
||||||
|
*/
|
||||||
|
#if !defined(MAXIWTHABS)
|
||||||
|
#define MAXIWTHABS 128
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc);
|
LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc);
|
||||||
LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n,
|
LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n,
|
||||||
StkId *pos);
|
StkId *pos);
|
||||||
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
|
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
|
||||||
const char *opname);
|
const char *opname);
|
||||||
|
LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o);
|
||||||
LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o,
|
LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o,
|
||||||
const char *what);
|
const char *what);
|
||||||
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
||||||
@@ -42,6 +58,7 @@ LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
|
|||||||
TString *src, int line);
|
TString *src, int line);
|
||||||
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
||||||
LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc);
|
LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc);
|
||||||
|
LUAI_FUNC int luaG_tracecall (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#define ldo_h
|
#define ldo_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "llimits.h"
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
@@ -17,11 +18,13 @@
|
|||||||
** Macro to check stack size and grow stack if needed. Parameters
|
** Macro to check stack size and grow stack if needed. Parameters
|
||||||
** 'pre'/'pos' allow the macro to preserve a pointer into the
|
** 'pre'/'pos' allow the macro to preserve a pointer into the
|
||||||
** stack across reallocations, doing the work only when needed.
|
** stack across reallocations, doing the work only when needed.
|
||||||
|
** It also allows the running of one GC step when the stack is
|
||||||
|
** reallocated.
|
||||||
** 'condmovestack' is used in heavy tests to force a stack reallocation
|
** 'condmovestack' is used in heavy tests to force a stack reallocation
|
||||||
** at every check.
|
** at every check.
|
||||||
*/
|
*/
|
||||||
#define luaD_checkstackaux(L,n,pre,pos) \
|
#define luaD_checkstackaux(L,n,pre,pos) \
|
||||||
if (L->stack_last - L->top <= (n)) \
|
if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
|
||||||
{ pre; luaD_growstack(L, n, 1); pos; } \
|
{ pre; luaD_growstack(L, n, 1); pos; } \
|
||||||
else { condmovestack(L,pre,pos); }
|
else { condmovestack(L,pre,pos); }
|
||||||
|
|
||||||
@@ -30,12 +33,19 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define savestack(L,p) ((char *)(p) - (char *)L->stack)
|
#define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p))
|
||||||
#define restorestack(L,n) ((StkId)((char *)L->stack + (n)))
|
#define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n))
|
||||||
|
|
||||||
|
|
||||||
/* macro to check stack size, preserving 'p' */
|
/* macro to check stack size, preserving 'p' */
|
||||||
#define checkstackp(L,n,p) \
|
#define checkstackp(L,n,p) \
|
||||||
|
luaD_checkstackaux(L, n, \
|
||||||
|
ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \
|
||||||
|
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, \
|
luaD_checkstackaux(L, n, \
|
||||||
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
|
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
|
||||||
luaC_checkGC(L), /* stack grow uses memory */ \
|
luaC_checkGC(L), /* stack grow uses memory */ \
|
||||||
@@ -44,22 +54,25 @@
|
|||||||
|
|
||||||
/* macro to check stack size and GC */
|
/* macro to check stack size and GC */
|
||||||
#define checkstackGC(L,fsize) \
|
#define checkstackGC(L,fsize) \
|
||||||
luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L))
|
luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
|
||||||
|
|
||||||
|
|
||||||
/* type of protected functions, to be ran by 'runprotected' */
|
/* type of protected functions, to be ran by 'runprotected' */
|
||||||
typedef void (*Pfunc) (lua_State *L, void *ud);
|
typedef void (*Pfunc) (lua_State *L, void *ud);
|
||||||
|
|
||||||
|
LUAI_FUNC l_noret luaD_errerr (lua_State *L);
|
||||||
LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
|
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 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||||
const char *mode);
|
const char *mode);
|
||||||
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
|
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
|
||||||
int fTransfer, int nTransfer);
|
int fTransfer, int nTransfer);
|
||||||
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
|
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
|
||||||
LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n);
|
LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
|
||||||
|
int narg1, int delta);
|
||||||
|
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_call (lua_State *L, StkId func, int nResults);
|
||||||
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
||||||
LUAI_FUNC void 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 int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||||
ptrdiff_t oldtop, ptrdiff_t ef);
|
ptrdiff_t oldtop, ptrdiff_t ef);
|
||||||
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
|
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
@@ -29,15 +30,15 @@ typedef struct {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** All high-level dumps go through DumpVector; you can change it to
|
** All high-level dumps go through dumpVector; you can change it to
|
||||||
** change the endianness of the result
|
** change the endianness of the result
|
||||||
*/
|
*/
|
||||||
#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
|
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
|
||||||
|
|
||||||
#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
|
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
|
||||||
|
|
||||||
|
|
||||||
static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
static void dumpBlock (DumpState *D, const void *b, size_t size) {
|
||||||
if (D->status == 0 && size > 0) {
|
if (D->status == 0 && size > 0) {
|
||||||
lua_unlock(D->L);
|
lua_unlock(D->L);
|
||||||
D->status = (*D->writer)(D->L, b, size, D->data);
|
D->status = (*D->writer)(D->L, b, size, D->data);
|
||||||
@@ -46,19 +47,22 @@ static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define DumpVar(x,D) DumpVector(&x,1,D)
|
#define dumpVar(D,x) dumpVector(D,&x,1)
|
||||||
|
|
||||||
|
|
||||||
static void DumpByte (int y, DumpState *D) {
|
static void dumpByte (DumpState *D, int y) {
|
||||||
lu_byte x = (lu_byte)y;
|
lu_byte x = (lu_byte)y;
|
||||||
DumpVar(x, D);
|
dumpVar(D, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* DumpInt Buff Size */
|
/*
|
||||||
#define DIBS ((sizeof(size_t) * 8 / 7) + 1)
|
** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6"
|
||||||
|
** rounds up the division.)
|
||||||
|
*/
|
||||||
|
#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
|
||||||
|
|
||||||
static void DumpSize (size_t x, DumpState *D) {
|
static void dumpSize (DumpState *D, size_t x) {
|
||||||
lu_byte buff[DIBS];
|
lu_byte buff[DIBS];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
do {
|
do {
|
||||||
@@ -66,146 +70,144 @@ static void DumpSize (size_t x, DumpState *D) {
|
|||||||
x >>= 7;
|
x >>= 7;
|
||||||
} while (x != 0);
|
} while (x != 0);
|
||||||
buff[DIBS - 1] |= 0x80; /* mark last byte */
|
buff[DIBS - 1] |= 0x80; /* mark last byte */
|
||||||
DumpVector(buff + DIBS - n, n, D);
|
dumpVector(D, buff + DIBS - n, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpInt (int x, DumpState *D) {
|
static void dumpInt (DumpState *D, int x) {
|
||||||
DumpSize(x, D);
|
dumpSize(D, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpNumber (lua_Number x, DumpState *D) {
|
static void dumpNumber (DumpState *D, lua_Number x) {
|
||||||
DumpVar(x, D);
|
dumpVar(D, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpInteger (lua_Integer x, DumpState *D) {
|
static void dumpInteger (DumpState *D, lua_Integer x) {
|
||||||
DumpVar(x, D);
|
dumpVar(D, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpString (const TString *s, DumpState *D) {
|
static void dumpString (DumpState *D, const TString *s) {
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
DumpSize(0, D);
|
dumpSize(D, 0);
|
||||||
else {
|
else {
|
||||||
size_t size = tsslen(s);
|
size_t size = tsslen(s);
|
||||||
const char *str = getstr(s);
|
const char *str = getstr(s);
|
||||||
DumpSize(size + 1, D);
|
dumpSize(D, size + 1);
|
||||||
DumpVector(str, size, D);
|
dumpVector(D, str, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpCode (const Proto *f, DumpState *D) {
|
static void dumpCode (DumpState *D, const Proto *f) {
|
||||||
DumpInt(f->sizecode, D);
|
dumpInt(D, f->sizecode);
|
||||||
DumpVector(f->code, f->sizecode, D);
|
dumpVector(D, f->code, f->sizecode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
|
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
|
||||||
|
|
||||||
static void DumpConstants (const Proto *f, DumpState *D) {
|
static void dumpConstants (DumpState *D, const Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
int n = f->sizek;
|
int n = f->sizek;
|
||||||
DumpInt(n, D);
|
dumpInt(D, n);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
const TValue *o = &f->k[i];
|
const TValue *o = &f->k[i];
|
||||||
DumpByte(ttypetag(o), D);
|
int tt = ttypetag(o);
|
||||||
switch (ttypetag(o)) {
|
dumpByte(D, tt);
|
||||||
case LUA_TNIL:
|
switch (tt) {
|
||||||
|
case LUA_VNUMFLT:
|
||||||
|
dumpNumber(D, fltvalue(o));
|
||||||
break;
|
break;
|
||||||
case LUA_TBOOLEAN:
|
case LUA_VNUMINT:
|
||||||
DumpByte(bvalue(o), D);
|
dumpInteger(D, ivalue(o));
|
||||||
break;
|
break;
|
||||||
case LUA_TNUMFLT:
|
case LUA_VSHRSTR:
|
||||||
DumpNumber(fltvalue(o), D);
|
case LUA_VLNGSTR:
|
||||||
|
dumpString(D, tsvalue(o));
|
||||||
break;
|
break;
|
||||||
case LUA_TNUMINT:
|
default:
|
||||||
DumpInteger(ivalue(o), D);
|
lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
|
||||||
break;
|
|
||||||
case LUA_TSHRSTR:
|
|
||||||
case LUA_TLNGSTR:
|
|
||||||
DumpString(tsvalue(o), D);
|
|
||||||
break;
|
|
||||||
default: lua_assert(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpProtos (const Proto *f, DumpState *D) {
|
static void dumpProtos (DumpState *D, const Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
int n = f->sizep;
|
int n = f->sizep;
|
||||||
DumpInt(n, D);
|
dumpInt(D, n);
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
DumpFunction(f->p[i], f->source, D);
|
dumpFunction(D, f->p[i], f->source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpUpvalues (const Proto *f, DumpState *D) {
|
static void dumpUpvalues (DumpState *D, const Proto *f) {
|
||||||
int i, n = f->sizeupvalues;
|
int i, n = f->sizeupvalues;
|
||||||
DumpInt(n, D);
|
dumpInt(D, n);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
DumpByte(f->upvalues[i].instack, D);
|
dumpByte(D, f->upvalues[i].instack);
|
||||||
DumpByte(f->upvalues[i].idx, D);
|
dumpByte(D, f->upvalues[i].idx);
|
||||||
|
dumpByte(D, f->upvalues[i].kind);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpDebug (const Proto *f, DumpState *D) {
|
static void dumpDebug (DumpState *D, const Proto *f) {
|
||||||
int i, n;
|
int i, n;
|
||||||
n = (D->strip) ? 0 : f->sizelineinfo;
|
n = (D->strip) ? 0 : f->sizelineinfo;
|
||||||
DumpInt(n, D);
|
dumpInt(D, n);
|
||||||
DumpVector(f->lineinfo, n, D);
|
dumpVector(D, f->lineinfo, n);
|
||||||
n = (D->strip) ? 0 : f->sizeabslineinfo;
|
n = (D->strip) ? 0 : f->sizeabslineinfo;
|
||||||
DumpInt(n, D);
|
dumpInt(D, n);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
DumpInt(f->abslineinfo[i].pc, D);
|
dumpInt(D, f->abslineinfo[i].pc);
|
||||||
DumpInt(f->abslineinfo[i].line, D);
|
dumpInt(D, f->abslineinfo[i].line);
|
||||||
}
|
}
|
||||||
n = (D->strip) ? 0 : f->sizelocvars;
|
n = (D->strip) ? 0 : f->sizelocvars;
|
||||||
DumpInt(n, D);
|
dumpInt(D, n);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
DumpString(f->locvars[i].varname, D);
|
dumpString(D, f->locvars[i].varname);
|
||||||
DumpInt(f->locvars[i].startpc, D);
|
dumpInt(D, f->locvars[i].startpc);
|
||||||
DumpInt(f->locvars[i].endpc, D);
|
dumpInt(D, f->locvars[i].endpc);
|
||||||
}
|
}
|
||||||
n = (D->strip) ? 0 : f->sizeupvalues;
|
n = (D->strip) ? 0 : f->sizeupvalues;
|
||||||
DumpInt(n, D);
|
dumpInt(D, n);
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
DumpString(f->upvalues[i].name, D);
|
dumpString(D, f->upvalues[i].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
|
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
|
||||||
if (D->strip || f->source == psource)
|
if (D->strip || f->source == psource)
|
||||||
DumpString(NULL, D); /* no debug info or same source as its parent */
|
dumpString(D, NULL); /* no debug info or same source as its parent */
|
||||||
else
|
else
|
||||||
DumpString(f->source, D);
|
dumpString(D, f->source);
|
||||||
DumpInt(f->linedefined, D);
|
dumpInt(D, f->linedefined);
|
||||||
DumpInt(f->lastlinedefined, D);
|
dumpInt(D, f->lastlinedefined);
|
||||||
DumpByte(f->numparams, D);
|
dumpByte(D, f->numparams);
|
||||||
DumpByte(f->is_vararg, D);
|
dumpByte(D, f->is_vararg);
|
||||||
DumpByte(f->maxstacksize, D);
|
dumpByte(D, f->maxstacksize);
|
||||||
DumpCode(f, D);
|
dumpCode(D, f);
|
||||||
DumpConstants(f, D);
|
dumpConstants(D, f);
|
||||||
DumpUpvalues(f, D);
|
dumpUpvalues(D, f);
|
||||||
DumpProtos(f, D);
|
dumpProtos(D, f);
|
||||||
DumpDebug(f, D);
|
dumpDebug(D, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DumpHeader (DumpState *D) {
|
static void dumpHeader (DumpState *D) {
|
||||||
DumpLiteral(LUA_SIGNATURE, D);
|
dumpLiteral(D, LUA_SIGNATURE);
|
||||||
DumpInt(LUAC_VERSION, D);
|
dumpByte(D, LUAC_VERSION);
|
||||||
DumpByte(LUAC_FORMAT, D);
|
dumpByte(D, LUAC_FORMAT);
|
||||||
DumpLiteral(LUAC_DATA, D);
|
dumpLiteral(D, LUAC_DATA);
|
||||||
DumpByte(sizeof(Instruction), D);
|
dumpByte(D, sizeof(Instruction));
|
||||||
DumpByte(sizeof(lua_Integer), D);
|
dumpByte(D, sizeof(lua_Integer));
|
||||||
DumpByte(sizeof(lua_Number), D);
|
dumpByte(D, sizeof(lua_Number));
|
||||||
DumpInteger(LUAC_INT, D);
|
dumpInteger(D, LUAC_INT);
|
||||||
DumpNumber(LUAC_NUM, D);
|
dumpNumber(D, LUAC_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -220,9 +222,9 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
|
|||||||
D.data = data;
|
D.data = data;
|
||||||
D.strip = strip;
|
D.strip = strip;
|
||||||
D.status = 0;
|
D.status = 0;
|
||||||
DumpHeader(&D);
|
dumpHeader(&D);
|
||||||
DumpByte(f->sizeupvalues, &D);
|
dumpByte(&D, f->sizeupvalues);
|
||||||
DumpFunction(f, NULL, &D);
|
dumpFunction(&D, f, NULL);
|
||||||
return D.status;
|
return D.status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,20 +24,20 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
CClosure *luaF_newCclosure (lua_State *L, int n) {
|
CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
|
GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
|
||||||
CClosure *c = gco2ccl(o);
|
CClosure *c = gco2ccl(o);
|
||||||
c->nupvalues = cast_byte(n);
|
c->nupvalues = cast_byte(nupvals);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LClosure *luaF_newLclosure (lua_State *L, int n) {
|
LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
|
GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
|
||||||
LClosure *c = gco2lcl(o);
|
LClosure *c = gco2lcl(o);
|
||||||
c->p = NULL;
|
c->p = NULL;
|
||||||
c->nupvalues = cast_byte(n);
|
c->nupvalues = cast_byte(nupvals);
|
||||||
while (n--) c->upvals[n] = NULL;
|
while (nupvals--) c->upvals[nupvals] = NULL;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,25 +48,25 @@ LClosure *luaF_newLclosure (lua_State *L, int n) {
|
|||||||
void luaF_initupvals (lua_State *L, LClosure *cl) {
|
void luaF_initupvals (lua_State *L, LClosure *cl) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < cl->nupvalues; i++) {
|
for (i = 0; i < cl->nupvalues; i++) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal));
|
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
|
||||||
UpVal *uv = gco2upv(o);
|
UpVal *uv = gco2upv(o);
|
||||||
uv->v = &uv->u.value; /* make it closed */
|
uv->v.p = &uv->u.value; /* make it closed */
|
||||||
setnilvalue(uv->v);
|
setnilvalue(uv->v.p);
|
||||||
cl->upvals[i] = uv;
|
cl->upvals[i] = uv;
|
||||||
luaC_objbarrier(L, cl, o);
|
luaC_objbarrier(L, cl, uv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Create a new upvalue with the given tag at the given level,
|
** Create a new upvalue at the given level, and link it to the list of
|
||||||
** and link it to the list of open upvalues of 'L' after entry 'prev'.
|
** open upvalues of 'L' after entry 'prev'.
|
||||||
**/
|
**/
|
||||||
static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) {
|
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
|
||||||
GCObject *o = luaC_newobj(L, tag, sizeof(UpVal));
|
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
|
||||||
UpVal *uv = gco2upv(o);
|
UpVal *uv = gco2upv(o);
|
||||||
UpVal *next = *prev;
|
UpVal *next = *prev;
|
||||||
uv->v = s2v(level); /* current value lives in the stack */
|
uv->v.p = s2v(level); /* current value lives in the stack */
|
||||||
uv->u.open.next = next; /* link it to list of open upvalues */
|
uv->u.open.next = next; /* link it to list of open upvalues */
|
||||||
uv->u.open.previous = prev;
|
uv->u.open.previous = prev;
|
||||||
if (next)
|
if (next)
|
||||||
@@ -81,7 +81,7 @@ static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Find and reuse, or create if it does not exist, a regular upvalue
|
** Find and reuse, or create if it does not exist, an upvalue
|
||||||
** at the given level.
|
** at the given level.
|
||||||
*/
|
*/
|
||||||
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||||
@@ -89,103 +89,93 @@ UpVal *luaF_findupval (lua_State *L, StkId level) {
|
|||||||
UpVal *p;
|
UpVal *p;
|
||||||
lua_assert(isintwups(L) || L->openupval == NULL);
|
lua_assert(isintwups(L) || L->openupval == NULL);
|
||||||
while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
|
while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
|
||||||
if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */
|
lua_assert(!isdead(G(L), p));
|
||||||
|
if (uplevel(p) == level) /* corresponding upvalue? */
|
||||||
return p; /* return it */
|
return p; /* return it */
|
||||||
pp = &p->u.open.next;
|
pp = &p->u.open.next;
|
||||||
}
|
}
|
||||||
/* not found: create a new upvalue after 'pp' */
|
/* not found: create a new upvalue after 'pp' */
|
||||||
return newupval(L, LUA_TUPVAL, level, pp);
|
return newupval(L, level, pp);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void callclose (lua_State *L, void *ud) {
|
|
||||||
UNUSED(ud);
|
|
||||||
luaD_callnoyield(L, L->top - 3, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Prepare closing method plus its arguments for object 'obj' with
|
** Call closing method for object 'obj' with error message 'err'. The
|
||||||
** error message 'err'. (This function assumes EXTRA_STACK.)
|
** boolean 'yy' controls whether the call is yieldable.
|
||||||
|
** (This function assumes EXTRA_STACK.)
|
||||||
*/
|
*/
|
||||||
static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) {
|
static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
|
||||||
StkId top = L->top;
|
StkId top = L->top.p;
|
||||||
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
|
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
|
||||||
if (ttisnil(tm)) /* no metamethod? */
|
|
||||||
return 0; /* nothing to call */
|
|
||||||
setobj2s(L, top, tm); /* will call metamethod... */
|
setobj2s(L, top, tm); /* will call metamethod... */
|
||||||
setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
|
setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
|
||||||
setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
|
setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
|
||||||
L->top = top + 3; /* add function and arguments */
|
L->top.p = top + 3; /* add function and arguments */
|
||||||
return 1;
|
if (yy)
|
||||||
|
luaD_call(L, top, 0);
|
||||||
|
else
|
||||||
|
luaD_callnoyield(L, top, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Prepare and call a closing method. If status is OK, code is still
|
** Check whether object at given level has a close metamethod and raise
|
||||||
** inside the original protected call, and so any error will be handled
|
** an error if not.
|
||||||
** there. Otherwise, a previous error already activated original
|
|
||||||
** protected call, and so the call to the closing method must be
|
|
||||||
** protected here. (A status = CLOSEPROTECT behaves like a previous
|
|
||||||
** error, to also run the closing method in protected mode).
|
|
||||||
** If status is OK, the call to the closing method will be pushed
|
|
||||||
** at the top of the stack. Otherwise, values are pushed after
|
|
||||||
** the 'level' of the upvalue being closed, as everything after
|
|
||||||
** that won't be used again.
|
|
||||||
*/
|
*/
|
||||||
static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) {
|
static void checkclosemth (lua_State *L, StkId level) {
|
||||||
if (likely(status == LUA_OK)) {
|
const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
|
||||||
if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */
|
if (ttisnil(tm)) { /* no metamethod? */
|
||||||
callclose(L, NULL); /* call closing method */
|
int idx = cast_int(level - L->ci->func.p); /* variable index */
|
||||||
else if (!ttisnil(uv)) { /* non-closable non-nil value? */
|
const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
|
||||||
int idx = cast_int(level - L->ci->func);
|
if (vname == NULL) vname = "?";
|
||||||
const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
|
luaG_runerror(L, "variable '%s' got a non-closable value", vname);
|
||||||
if (vname == NULL) vname = "?";
|
|
||||||
luaG_runerror(L, "attempt to close non-closable variable '%s'", vname);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else { /* must close the object in protected mode */
|
|
||||||
ptrdiff_t oldtop = savestack(L, level + 1);
|
|
||||||
/* save error message and set stack top to 'level + 1' */
|
|
||||||
luaD_seterrorobj(L, status, level);
|
|
||||||
if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */
|
|
||||||
int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0);
|
|
||||||
if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */
|
|
||||||
status = newstatus; /* this will be the new error */
|
|
||||||
else /* leave original error (or nil) on top */
|
|
||||||
L->top = restorestack(L, oldtop);
|
|
||||||
}
|
|
||||||
/* else no metamethod; ignore this case and keep original error */
|
|
||||||
}
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Try to create a to-be-closed upvalue
|
** Prepare and call a closing method.
|
||||||
** (can raise a memory-allocation error)
|
** If status is CLOSEKTOP, the call to the closing method will be pushed
|
||||||
|
** at the top of the stack. Otherwise, values can be pushed right after
|
||||||
|
** the 'level' of the upvalue being closed, as everything after that
|
||||||
|
** won't be used again.
|
||||||
*/
|
*/
|
||||||
static void trynewtbcupval (lua_State *L, void *ud) {
|
static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
|
||||||
StkId level = cast(StkId, ud);
|
TValue *uv = s2v(level); /* value being closed */
|
||||||
lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
|
TValue *errobj;
|
||||||
newupval(L, LUA_TUPVALTBC, level, &L->openupval);
|
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 */
|
||||||
|
}
|
||||||
|
callclosemethod(L, uv, errobj, yy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Create a to-be-closed upvalue. If there is a memory error
|
** Maximum value for deltas in 'tbclist', dependent on the type
|
||||||
** when creating the upvalue, the closing method must be called here,
|
** of delta. (This macro assumes that an 'L' is in scope where it
|
||||||
** as there is no upvalue to call it later.
|
** is used.)
|
||||||
|
*/
|
||||||
|
#define MAXDELTA \
|
||||||
|
((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Insert a variable in the list of to-be-closed variables.
|
||||||
*/
|
*/
|
||||||
void luaF_newtbcupval (lua_State *L, StkId level) {
|
void luaF_newtbcupval (lua_State *L, StkId level) {
|
||||||
int status = luaD_rawrunprotected(L, trynewtbcupval, level);
|
lua_assert(level > L->tbclist.p);
|
||||||
if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
|
if (l_isfalse(s2v(level)))
|
||||||
lua_assert(status == LUA_ERRMEM);
|
return; /* false doesn't need to be closed */
|
||||||
luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */
|
checkclosemth(L, level); /* value must have a close method */
|
||||||
if (prepclosingmethod(L, s2v(level), s2v(level + 1)))
|
while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
|
||||||
callclose(L, NULL); /* call closing method */
|
L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */
|
||||||
luaD_throw(L, LUA_ERRMEM); /* throw memory error */
|
L->tbclist.p->tbclist.delta = 0;
|
||||||
}
|
}
|
||||||
|
level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
|
||||||
|
L->tbclist.p = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -197,30 +187,58 @@ void luaF_unlinkupval (UpVal *uv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaF_close (lua_State *L, StkId level, int status) {
|
/*
|
||||||
|
** Close all upvalues up to the given stack level.
|
||||||
|
*/
|
||||||
|
void luaF_closeupval (lua_State *L, StkId level) {
|
||||||
UpVal *uv;
|
UpVal *uv;
|
||||||
while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
|
StkId upl; /* stack index pointed by 'uv' */
|
||||||
StkId upl = uplevel(uv);
|
while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
|
||||||
TValue *slot = &uv->u.value; /* new position for value */
|
TValue *slot = &uv->u.value; /* new position for value */
|
||||||
luaF_unlinkupval(uv);
|
lua_assert(uplevel(uv) < L->top.p);
|
||||||
setobj(L, slot, uv->v); /* move value to upvalue slot */
|
luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
|
||||||
uv->v = slot; /* now current value lives here */
|
setobj(L, slot, uv->v.p); /* move value to upvalue slot */
|
||||||
if (!iswhite(uv))
|
uv->v.p = slot; /* now current value lives here */
|
||||||
gray2black(uv); /* closed upvalues cannot be gray */
|
if (!iswhite(uv)) { /* neither white nor dead? */
|
||||||
luaC_barrier(L, uv, slot);
|
nw2black(uv); /* closed upvalues cannot be gray */
|
||||||
if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) {
|
luaC_barrier(L, uv, slot);
|
||||||
/* must run closing method */
|
|
||||||
ptrdiff_t levelrel = savestack(L, level);
|
|
||||||
status = callclosemth(L, uv->v, upl, status); /* may change the stack */
|
|
||||||
level = restorestack(L, levelrel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return status;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Remove first element from the tbclist plus its dummy nodes.
|
||||||
|
*/
|
||||||
|
static void poptbclist (lua_State *L) {
|
||||||
|
StkId tbc = L->tbclist.p;
|
||||||
|
lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */
|
||||||
|
tbc -= tbc->tbclist.delta;
|
||||||
|
while (tbc > L->stack.p && tbc->tbclist.delta == 0)
|
||||||
|
tbc -= MAXDELTA; /* remove dummy nodes */
|
||||||
|
L->tbclist.p = tbc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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) {
|
||||||
|
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 */
|
||||||
|
StkId tbc = L->tbclist.p; /* get variable index */
|
||||||
|
poptbclist(L); /* remove it from list */
|
||||||
|
prepcallclosemth(L, tbc, status, yy); /* close variable */
|
||||||
|
level = restorestack(L, levelrel);
|
||||||
|
}
|
||||||
|
return level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Proto *luaF_newproto (lua_State *L) {
|
Proto *luaF_newproto (lua_State *L) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
|
GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
|
||||||
Proto *f = gco2p(o);
|
Proto *f = gco2p(o);
|
||||||
f->k = NULL;
|
f->k = NULL;
|
||||||
f->sizek = 0;
|
f->sizek = 0;
|
||||||
|
|||||||
@@ -29,10 +29,10 @@
|
|||||||
#define MAXUPVAL 255
|
#define MAXUPVAL 255
|
||||||
|
|
||||||
|
|
||||||
#define upisopen(up) ((up)->v != &(up)->u.value)
|
#define upisopen(up) ((up)->v.p != &(up)->u.value)
|
||||||
|
|
||||||
|
|
||||||
#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v))
|
#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -42,24 +42,19 @@
|
|||||||
#define MAXMISS 10
|
#define MAXMISS 10
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Special "status" for 'luaF_close'
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* close upvalues without running their closing methods */
|
/* special status to close upvalues preserving the top of the stack */
|
||||||
#define NOCLOSINGMETH (-1)
|
#define CLOSEKTOP (-1)
|
||||||
|
|
||||||
/* close upvalues running all closing methods in protected mode */
|
|
||||||
#define CLOSEPROTECT (-2)
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
||||||
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems);
|
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals);
|
||||||
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems);
|
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals);
|
||||||
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
|
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
|
||||||
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
|
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
|
||||||
LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
|
LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
|
||||||
LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status);
|
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 void luaF_unlinkupval (UpVal *uv);
|
LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
|
||||||
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
|
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
|
||||||
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
|
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
|
||||||
|
|||||||
@@ -12,16 +12,16 @@
|
|||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Collectable objects may have one of three colors: white, which
|
** Collectable objects may have one of three colors: white, which means
|
||||||
** means the object is not marked; gray, which means the
|
** the object is not marked; gray, which means the object is marked, but
|
||||||
** object is marked, but its references may be not marked; and
|
** its references may be not marked; and black, which means that the
|
||||||
** black, which means that the object and all its references are marked.
|
** object and all its references are marked. The main invariant of the
|
||||||
** The main invariant of the garbage collector, while marking objects,
|
** garbage collector, while marking objects, is that a black object can
|
||||||
** is that a black object can never point to a white one. Moreover,
|
** never point to a white one. Moreover, any gray object must be in a
|
||||||
** any gray object must be in a "gray list" (gray, grayagain, weak,
|
** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
|
||||||
** allweak, ephemeron) so that it can be visited again before finishing
|
** can be visited again before finishing the collection cycle. (Open
|
||||||
** the collection cycle. These lists have no meaning when the invariant
|
** upvalues are an exception to this rule.) These lists have no meaning
|
||||||
** is not being enforced (e.g., sweep phase).
|
** when the invariant is not being enforced (e.g., sweep phase).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -69,14 +69,16 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Layout for bit use in 'marked' field. First three bits are
|
** Layout for bit use in 'marked' field. First three bits are
|
||||||
** used for object "age" in generational mode. Last bit is free
|
** used for object "age" in generational mode. Last bit is used
|
||||||
** to be used by respective objects.
|
** by tests.
|
||||||
*/
|
*/
|
||||||
#define WHITE0BIT 3 /* object is white (type 0) */
|
#define WHITE0BIT 3 /* object is white (type 0) */
|
||||||
#define WHITE1BIT 4 /* object is white (type 1) */
|
#define WHITE1BIT 4 /* object is white (type 1) */
|
||||||
#define BLACKBIT 5 /* object is black */
|
#define BLACKBIT 5 /* object is black */
|
||||||
#define FINALIZEDBIT 6 /* object has been marked for finalization */
|
#define FINALIZEDBIT 6 /* object has been marked for finalization */
|
||||||
|
|
||||||
|
#define TESTBIT 7
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
||||||
@@ -94,7 +96,8 @@
|
|||||||
#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
|
#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
|
||||||
|
|
||||||
#define changewhite(x) ((x)->marked ^= WHITEBITS)
|
#define changewhite(x) ((x)->marked ^= WHITEBITS)
|
||||||
#define gray2black(x) l_setbit((x)->marked, BLACKBIT)
|
#define nw2black(x) \
|
||||||
|
check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
|
||||||
|
|
||||||
#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
|
#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
|
||||||
|
|
||||||
@@ -145,6 +148,16 @@
|
|||||||
*/
|
*/
|
||||||
#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
|
#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Control when GC is running:
|
||||||
|
*/
|
||||||
|
#define GCSTPUSR 1 /* bit true when GC stopped by user */
|
||||||
|
#define GCSTPGC 2 /* bit true when GC stopped by itself */
|
||||||
|
#define GCSTPCLS 4 /* bit true when closing Lua state */
|
||||||
|
#define gcrunning(g) ((g)->gcstp == 0)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Does one step of collection when debt becomes positive. 'pre'/'pos'
|
** Does one step of collection when debt becomes positive. 'pre'/'pos'
|
||||||
** allows some adjustments to be done only when needed. macro
|
** allows some adjustments to be done only when needed. macro
|
||||||
@@ -159,24 +172,27 @@
|
|||||||
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
|
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
|
||||||
|
|
||||||
|
|
||||||
#define luaC_barrier(L,p,v) ( \
|
|
||||||
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
|
||||||
luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
|
|
||||||
|
|
||||||
#define luaC_barrierback(L,p,v) ( \
|
|
||||||
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
|
||||||
luaC_barrierback_(L,p) : cast_void(0))
|
|
||||||
|
|
||||||
#define luaC_objbarrier(L,p,o) ( \
|
#define luaC_objbarrier(L,p,o) ( \
|
||||||
(isblack(p) && iswhite(o)) ? \
|
(isblack(p) && iswhite(o)) ? \
|
||||||
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
|
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
|
||||||
|
|
||||||
|
#define luaC_barrier(L,p,v) ( \
|
||||||
|
iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
|
||||||
|
|
||||||
|
#define luaC_objbarrierback(L,p,o) ( \
|
||||||
|
(isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
|
||||||
|
|
||||||
|
#define luaC_barrierback(L,p,v) ( \
|
||||||
|
iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
|
||||||
|
|
||||||
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
|
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
|
||||||
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
||||||
LUAI_FUNC void luaC_step (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 statesmask);
|
||||||
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
|
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_newobj (lua_State *L, int tt, size_t sz);
|
||||||
|
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
|
||||||
|
size_t offset);
|
||||||
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
||||||
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
|
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
|
||||||
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
||||||
|
|||||||
@@ -64,6 +64,12 @@ static int l_checkmode (const char *mode) {
|
|||||||
#define l_popen(L,c,m) (_popen(c,m))
|
#define l_popen(L,c,m) (_popen(c,m))
|
||||||
#define l_pclose(L,file) (_pclose(file))
|
#define l_pclose(L,file) (_pclose(file))
|
||||||
|
|
||||||
|
#if !defined(l_checkmodep)
|
||||||
|
/* Windows accepts "[rw][bt]?" as valid modes */
|
||||||
|
#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \
|
||||||
|
(m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0')))
|
||||||
|
#endif
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ISO C definitions */
|
/* ISO C definitions */
|
||||||
@@ -77,6 +83,12 @@ static int l_checkmode (const char *mode) {
|
|||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(l_checkmodep)
|
||||||
|
/* By default, Lua accepts only "r" or "w" as valid modes */
|
||||||
|
#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0')
|
||||||
|
#endif
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -153,7 +165,7 @@ static int io_type (lua_State *L) {
|
|||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
|
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
lua_pushnil(L); /* not a file */
|
luaL_pushfail(L); /* not a file */
|
||||||
else if (isclosed(p))
|
else if (isclosed(p))
|
||||||
lua_pushliteral(L, "closed file");
|
lua_pushliteral(L, "closed file");
|
||||||
else
|
else
|
||||||
@@ -174,7 +186,7 @@ static int f_tostring (lua_State *L) {
|
|||||||
|
|
||||||
static FILE *tofile (lua_State *L) {
|
static FILE *tofile (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
if (isclosed(p))
|
if (l_unlikely(isclosed(p)))
|
||||||
luaL_error(L, "attempt to use a closed file");
|
luaL_error(L, "attempt to use a closed file");
|
||||||
lua_assert(p->f);
|
lua_assert(p->f);
|
||||||
return p->f;
|
return p->f;
|
||||||
@@ -215,7 +227,7 @@ static int f_close (lua_State *L) {
|
|||||||
|
|
||||||
static int io_close (lua_State *L) {
|
static int io_close (lua_State *L) {
|
||||||
if (lua_isnone(L, 1)) /* no argument? */
|
if (lua_isnone(L, 1)) /* no argument? */
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
|
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */
|
||||||
return f_close(L);
|
return f_close(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,8 +245,8 @@ static int f_gc (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
static int io_fclose (lua_State *L) {
|
static int io_fclose (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
int res = fclose(p->f);
|
errno = 0;
|
||||||
return luaL_fileresult(L, (res == 0), NULL);
|
return luaL_fileresult(L, (fclose(p->f) == 0), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -249,7 +261,7 @@ static LStream *newfile (lua_State *L) {
|
|||||||
static void opencheck (lua_State *L, const char *fname, const char *mode) {
|
static void opencheck (lua_State *L, const char *fname, const char *mode) {
|
||||||
LStream *p = newfile(L);
|
LStream *p = newfile(L);
|
||||||
p->f = fopen(fname, mode);
|
p->f = fopen(fname, mode);
|
||||||
if (p->f == NULL)
|
if (l_unlikely(p->f == NULL))
|
||||||
luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
|
luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,6 +272,7 @@ static int io_open (lua_State *L) {
|
|||||||
LStream *p = newfile(L);
|
LStream *p = newfile(L);
|
||||||
const char *md = mode; /* to traverse/check mode */
|
const char *md = mode; /* to traverse/check mode */
|
||||||
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
|
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
|
||||||
|
errno = 0;
|
||||||
p->f = fopen(filename, mode);
|
p->f = fopen(filename, mode);
|
||||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||||
}
|
}
|
||||||
@@ -270,6 +283,7 @@ static int io_open (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
static int io_pclose (lua_State *L) {
|
static int io_pclose (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
|
errno = 0;
|
||||||
return luaL_execresult(L, l_pclose(L, p->f));
|
return luaL_execresult(L, l_pclose(L, p->f));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,6 +292,8 @@ static int io_popen (lua_State *L) {
|
|||||||
const char *filename = luaL_checkstring(L, 1);
|
const char *filename = luaL_checkstring(L, 1);
|
||||||
const char *mode = luaL_optstring(L, 2, "r");
|
const char *mode = luaL_optstring(L, 2, "r");
|
||||||
LStream *p = newprefile(L);
|
LStream *p = newprefile(L);
|
||||||
|
luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode");
|
||||||
|
errno = 0;
|
||||||
p->f = l_popen(L, filename, mode);
|
p->f = l_popen(L, filename, mode);
|
||||||
p->closef = &io_pclose;
|
p->closef = &io_pclose;
|
||||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||||
@@ -286,6 +302,7 @@ static int io_popen (lua_State *L) {
|
|||||||
|
|
||||||
static int io_tmpfile (lua_State *L) {
|
static int io_tmpfile (lua_State *L) {
|
||||||
LStream *p = newfile(L);
|
LStream *p = newfile(L);
|
||||||
|
errno = 0;
|
||||||
p->f = tmpfile();
|
p->f = tmpfile();
|
||||||
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
||||||
}
|
}
|
||||||
@@ -295,8 +312,8 @@ static FILE *getiofile (lua_State *L, const char *findex) {
|
|||||||
LStream *p;
|
LStream *p;
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
||||||
p = (LStream *)lua_touserdata(L, -1);
|
p = (LStream *)lua_touserdata(L, -1);
|
||||||
if (isclosed(p))
|
if (l_unlikely(isclosed(p)))
|
||||||
luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
|
luaL_error(L, "default %s file is closed", findex + IOPREF_LEN);
|
||||||
return p->f;
|
return p->f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,7 +355,7 @@ static int io_readline (lua_State *L);
|
|||||||
#define MAXARGLINE 250
|
#define MAXARGLINE 250
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Auxiliar function to create the iteration function for 'lines'.
|
** Auxiliary function to create the iteration function for 'lines'.
|
||||||
** The iteration function is a closure over 'io_readline', with
|
** The iteration function is a closure over 'io_readline', with
|
||||||
** the following upvalues:
|
** the following upvalues:
|
||||||
** 1) The file being read (first value in the stack)
|
** 1) The file being read (first value in the stack)
|
||||||
@@ -422,7 +439,7 @@ typedef struct {
|
|||||||
** Add current char to buffer (if not out of space) and read next one
|
** Add current char to buffer (if not out of space) and read next one
|
||||||
*/
|
*/
|
||||||
static int nextc (RN *rn) {
|
static int nextc (RN *rn) {
|
||||||
if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */
|
if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */
|
||||||
rn->buff[0] = '\0'; /* invalidate result */
|
rn->buff[0] = '\0'; /* invalidate result */
|
||||||
return 0; /* fail */
|
return 0; /* fail */
|
||||||
}
|
}
|
||||||
@@ -485,8 +502,8 @@ static int read_number (lua_State *L, FILE *f) {
|
|||||||
ungetc(rn.c, rn.f); /* unread look-ahead char */
|
ungetc(rn.c, rn.f); /* unread look-ahead char */
|
||||||
l_unlockfile(rn.f);
|
l_unlockfile(rn.f);
|
||||||
rn.buff[rn.n] = '\0'; /* finish string */
|
rn.buff[rn.n] = '\0'; /* finish string */
|
||||||
if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */
|
if (l_likely(lua_stringtonumber(L, rn.buff)))
|
||||||
return 1; /* ok */
|
return 1; /* ok, it is a valid number */
|
||||||
else { /* invalid format */
|
else { /* invalid format */
|
||||||
lua_pushnil(L); /* "result" to be removed */
|
lua_pushnil(L); /* "result" to be removed */
|
||||||
return 0; /* read fails */
|
return 0; /* read fails */
|
||||||
@@ -553,6 +570,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
|||||||
int nargs = lua_gettop(L) - 1;
|
int nargs = lua_gettop(L) - 1;
|
||||||
int n, success;
|
int n, success;
|
||||||
clearerr(f);
|
clearerr(f);
|
||||||
|
errno = 0;
|
||||||
if (nargs == 0) { /* no arguments? */
|
if (nargs == 0) { /* no arguments? */
|
||||||
success = read_line(L, f, 1);
|
success = read_line(L, f, 1);
|
||||||
n = first + 1; /* to return 1 result */
|
n = first + 1; /* to return 1 result */
|
||||||
@@ -593,7 +611,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
|||||||
return luaL_fileresult(L, 0, NULL);
|
return luaL_fileresult(L, 0, NULL);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
lua_pop(L, 1); /* remove last result */
|
lua_pop(L, 1); /* remove last result */
|
||||||
lua_pushnil(L); /* push nil instead */
|
luaL_pushfail(L); /* push nil instead */
|
||||||
}
|
}
|
||||||
return n - first;
|
return n - first;
|
||||||
}
|
}
|
||||||
@@ -626,7 +644,7 @@ static int io_readline (lua_State *L) {
|
|||||||
lua_assert(n > 0); /* should return at least a nil */
|
lua_assert(n > 0); /* should return at least a nil */
|
||||||
if (lua_toboolean(L, -n)) /* read at least one value? */
|
if (lua_toboolean(L, -n)) /* read at least one value? */
|
||||||
return n; /* return them */
|
return n; /* return them */
|
||||||
else { /* first result is nil: EOF or error */
|
else { /* first result is false: EOF or error */
|
||||||
if (n > 1) { /* is there error information? */
|
if (n > 1) { /* is there error information? */
|
||||||
/* 2nd result is error message */
|
/* 2nd result is error message */
|
||||||
return luaL_error(L, "%s", lua_tostring(L, -n + 1));
|
return luaL_error(L, "%s", lua_tostring(L, -n + 1));
|
||||||
@@ -646,6 +664,7 @@ static int io_readline (lua_State *L) {
|
|||||||
static int g_write (lua_State *L, FILE *f, int arg) {
|
static int g_write (lua_State *L, FILE *f, int arg) {
|
||||||
int nargs = lua_gettop(L) - arg;
|
int nargs = lua_gettop(L) - arg;
|
||||||
int status = 1;
|
int status = 1;
|
||||||
|
errno = 0;
|
||||||
for (; nargs--; arg++) {
|
for (; nargs--; arg++) {
|
||||||
if (lua_type(L, arg) == LUA_TNUMBER) {
|
if (lua_type(L, arg) == LUA_TNUMBER) {
|
||||||
/* optimization: could be done exactly as for strings */
|
/* optimization: could be done exactly as for strings */
|
||||||
@@ -662,8 +681,10 @@ static int g_write (lua_State *L, FILE *f, int arg) {
|
|||||||
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (status) return 1; /* file handle already on stack top */
|
if (l_likely(status))
|
||||||
else return luaL_fileresult(L, status, NULL);
|
return 1; /* file handle already on stack top */
|
||||||
|
else
|
||||||
|
return luaL_fileresult(L, status, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -688,8 +709,9 @@ static int f_seek (lua_State *L) {
|
|||||||
l_seeknum offset = (l_seeknum)p3;
|
l_seeknum offset = (l_seeknum)p3;
|
||||||
luaL_argcheck(L, (lua_Integer)offset == p3, 3,
|
luaL_argcheck(L, (lua_Integer)offset == p3, 3,
|
||||||
"not an integer in proper range");
|
"not an integer in proper range");
|
||||||
|
errno = 0;
|
||||||
op = l_fseek(f, offset, mode[op]);
|
op = l_fseek(f, offset, mode[op]);
|
||||||
if (op)
|
if (l_unlikely(op))
|
||||||
return luaL_fileresult(L, 0, NULL); /* error */
|
return luaL_fileresult(L, 0, NULL); /* error */
|
||||||
else {
|
else {
|
||||||
lua_pushinteger(L, (lua_Integer)l_ftell(f));
|
lua_pushinteger(L, (lua_Integer)l_ftell(f));
|
||||||
@@ -704,19 +726,25 @@ static int f_setvbuf (lua_State *L) {
|
|||||||
FILE *f = tofile(L);
|
FILE *f = tofile(L);
|
||||||
int op = luaL_checkoption(L, 2, NULL, modenames);
|
int op = luaL_checkoption(L, 2, NULL, modenames);
|
||||||
lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
|
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);
|
return luaL_fileresult(L, res == 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int io_flush (lua_State *L) {
|
static int io_flush (lua_State *L) {
|
||||||
return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
FILE *f = getiofile(L, IO_OUTPUT);
|
||||||
|
errno = 0;
|
||||||
|
return luaL_fileresult(L, fflush(f) == 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int f_flush (lua_State *L) {
|
static int f_flush (lua_State *L) {
|
||||||
return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
|
FILE *f = tofile(L);
|
||||||
|
errno = 0;
|
||||||
|
return luaL_fileresult(L, fflush(f) == 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -742,14 +770,23 @@ static const luaL_Reg iolib[] = {
|
|||||||
/*
|
/*
|
||||||
** methods for file handles
|
** methods for file handles
|
||||||
*/
|
*/
|
||||||
static const luaL_Reg flib[] = {
|
static const luaL_Reg meth[] = {
|
||||||
{"close", f_close},
|
|
||||||
{"flush", f_flush},
|
|
||||||
{"lines", f_lines},
|
|
||||||
{"read", f_read},
|
{"read", f_read},
|
||||||
{"seek", f_seek},
|
|
||||||
{"setvbuf", f_setvbuf},
|
|
||||||
{"write", f_write},
|
{"write", f_write},
|
||||||
|
{"lines", f_lines},
|
||||||
|
{"flush", f_flush},
|
||||||
|
{"seek", f_seek},
|
||||||
|
{"close", f_close},
|
||||||
|
{"setvbuf", f_setvbuf},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** metamethods for file handles
|
||||||
|
*/
|
||||||
|
static const luaL_Reg metameth[] = {
|
||||||
|
{"__index", NULL}, /* placeholder */
|
||||||
{"__gc", f_gc},
|
{"__gc", f_gc},
|
||||||
{"__close", f_gc},
|
{"__close", f_gc},
|
||||||
{"__tostring", f_tostring},
|
{"__tostring", f_tostring},
|
||||||
@@ -758,11 +795,12 @@ static const luaL_Reg flib[] = {
|
|||||||
|
|
||||||
|
|
||||||
static void createmeta (lua_State *L) {
|
static void createmeta (lua_State *L) {
|
||||||
luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
|
luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */
|
||||||
lua_pushvalue(L, -1); /* push metatable */
|
luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
|
||||||
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
luaL_newlibtable(L, meth); /* create method table */
|
||||||
luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
|
luaL_setfuncs(L, meth, 0); /* add file methods to method table */
|
||||||
lua_pop(L, 1); /* pop new metatable */
|
lua_setfield(L, -2, "__index"); /* metatable.__index = method table */
|
||||||
|
lua_pop(L, 1); /* pop metatable */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -772,7 +810,7 @@ static void createmeta (lua_State *L) {
|
|||||||
static int io_noclose (lua_State *L) {
|
static int io_noclose (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
p->closef = &io_noclose; /* keep file opened */
|
p->closef = &io_noclose; /* keep file opened */
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
lua_pushliteral(L, "cannot close standard file");
|
lua_pushliteral(L, "cannot close standard file");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-8
@@ -16,7 +16,7 @@
|
|||||||
#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i));
|
#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i));
|
||||||
|
|
||||||
|
|
||||||
static void *disptab[NUM_OPCODES] = {
|
static const void *const disptab[NUM_OPCODES] = {
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
** you can update the following list with this command:
|
** you can update the following list with this command:
|
||||||
@@ -30,7 +30,9 @@ static void *disptab[NUM_OPCODES] = {
|
|||||||
&&L_OP_LOADF,
|
&&L_OP_LOADF,
|
||||||
&&L_OP_LOADK,
|
&&L_OP_LOADK,
|
||||||
&&L_OP_LOADKX,
|
&&L_OP_LOADKX,
|
||||||
&&L_OP_LOADBOOL,
|
&&L_OP_LOADFALSE,
|
||||||
|
&&L_OP_LFALSESKIP,
|
||||||
|
&&L_OP_LOADTRUE,
|
||||||
&&L_OP_LOADNIL,
|
&&L_OP_LOADNIL,
|
||||||
&&L_OP_GETUPVAL,
|
&&L_OP_GETUPVAL,
|
||||||
&&L_OP_SETUPVAL,
|
&&L_OP_SETUPVAL,
|
||||||
@@ -45,12 +47,6 @@ static void *disptab[NUM_OPCODES] = {
|
|||||||
&&L_OP_NEWTABLE,
|
&&L_OP_NEWTABLE,
|
||||||
&&L_OP_SELF,
|
&&L_OP_SELF,
|
||||||
&&L_OP_ADDI,
|
&&L_OP_ADDI,
|
||||||
&&L_OP_SUBI,
|
|
||||||
&&L_OP_MULI,
|
|
||||||
&&L_OP_MODI,
|
|
||||||
&&L_OP_POWI,
|
|
||||||
&&L_OP_DIVI,
|
|
||||||
&&L_OP_IDIVI,
|
|
||||||
&&L_OP_ADDK,
|
&&L_OP_ADDK,
|
||||||
&&L_OP_SUBK,
|
&&L_OP_SUBK,
|
||||||
&&L_OP_MULK,
|
&&L_OP_MULK,
|
||||||
@@ -75,6 +71,9 @@ static void *disptab[NUM_OPCODES] = {
|
|||||||
&&L_OP_BXOR,
|
&&L_OP_BXOR,
|
||||||
&&L_OP_SHL,
|
&&L_OP_SHL,
|
||||||
&&L_OP_SHR,
|
&&L_OP_SHR,
|
||||||
|
&&L_OP_MMBIN,
|
||||||
|
&&L_OP_MMBINI,
|
||||||
|
&&L_OP_MMBINK,
|
||||||
&&L_OP_UNM,
|
&&L_OP_UNM,
|
||||||
&&L_OP_BNOT,
|
&&L_OP_BNOT,
|
||||||
&&L_OP_NOT,
|
&&L_OP_NOT,
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ void luaX_init (lua_State *L) {
|
|||||||
|
|
||||||
const char *luaX_token2str (LexState *ls, int token) {
|
const char *luaX_token2str (LexState *ls, int token) {
|
||||||
if (token < FIRST_RESERVED) { /* single-byte symbols? */
|
if (token < FIRST_RESERVED) { /* single-byte symbols? */
|
||||||
lua_assert(token == cast_uchar(token));
|
|
||||||
if (lisprint(token))
|
if (lisprint(token))
|
||||||
return luaO_pushfstring(ls->L, "'%c'", token);
|
return luaO_pushfstring(ls->L, "'%c'", token);
|
||||||
else /* control character */
|
else /* control character */
|
||||||
@@ -123,26 +122,29 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** creates a new string and anchors it in scanner's table so that
|
** Creates a new string and anchors it in scanner's table so that it
|
||||||
** it will not be collected until the end of the compilation
|
** will not be collected until the end of the compilation; by that time
|
||||||
** (by that time it should be anchored somewhere)
|
** 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.
|
||||||
*/
|
*/
|
||||||
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||||
lua_State *L = ls->L;
|
lua_State *L = ls->L;
|
||||||
TValue *o; /* entry for 'str' */
|
|
||||||
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
||||||
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
const TValue *o = luaH_getstr(ls->h, ts);
|
||||||
o = luaH_set(L, ls->h, s2v(L->top - 1));
|
if (!ttisnil(o)) /* string already present? */
|
||||||
if (isempty(o)) { /* not in use yet? */
|
ts = keystrval(nodefromval(o)); /* get saved copy */
|
||||||
/* boolean value does not need GC barrier;
|
else { /* not in use yet */
|
||||||
table is not a metatable, so it does not need to invalidate cache */
|
TValue *stv = s2v(L->top.p++); /* reserve stack space for string */
|
||||||
setbvalue(o, 1); /* t[string] = true */
|
setsvalue(L, stv, ts); /* temporarily anchor the string */
|
||||||
|
luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */
|
||||||
|
/* table is not a metatable, so it does not need to invalidate cache */
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
|
L->top.p--; /* remove string from stack */
|
||||||
}
|
}
|
||||||
else { /* string already present */
|
|
||||||
ts = keystrval(nodefromval(o)); /* re-use value previously stored */
|
|
||||||
}
|
|
||||||
L->top--; /* remove string from stack */
|
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,8 +213,16 @@ static int check_next2 (LexState *ls, const char *set) {
|
|||||||
|
|
||||||
/* LUA_NUMBER */
|
/* LUA_NUMBER */
|
||||||
/*
|
/*
|
||||||
** this function is quite liberal in what it accepts, as 'luaO_str2num'
|
** This function is quite liberal in what it accepts, as 'luaO_str2num'
|
||||||
** will reject ill-formed numerals.
|
** will reject ill-formed numerals. Roughly, it accepts the following
|
||||||
|
** pattern:
|
||||||
|
**
|
||||||
|
** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))*
|
||||||
|
**
|
||||||
|
** The only tricky part is to accept [+-] only after a valid exponent
|
||||||
|
** mark, to avoid reading '3-4' or '0xe+1' as a single number.
|
||||||
|
**
|
||||||
|
** The caller might have already read an initial dot.
|
||||||
*/
|
*/
|
||||||
static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||||
TValue obj;
|
TValue obj;
|
||||||
@@ -223,15 +233,13 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
|||||||
if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
|
if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
|
||||||
expo = "Pp";
|
expo = "Pp";
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (check_next2(ls, expo)) /* exponent part? */
|
if (check_next2(ls, expo)) /* exponent mark? */
|
||||||
check_next2(ls, "-+"); /* optional exponent sign */
|
check_next2(ls, "-+"); /* optional exponent sign */
|
||||||
if (lisxdigit(ls->current))
|
else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */
|
||||||
save_and_next(ls);
|
|
||||||
else if (ls->current == '.')
|
|
||||||
save_and_next(ls);
|
save_and_next(ls);
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
if (lislalnum(ls->current)) /* is numeral touching an alpha num? */
|
if (lislalpha(ls->current)) /* is numeral touching a letter? */
|
||||||
save_and_next(ls); /* force an error */
|
save_and_next(ls); /* force an error */
|
||||||
save(ls, '\0');
|
save(ls, '\0');
|
||||||
if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
|
if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
|
||||||
@@ -249,9 +257,10 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** reads a sequence '[=*[' or ']=*]', leaving the last bracket.
|
** read a sequence '[=*[' or ']=*]', leaving the last bracket. If
|
||||||
** If sequence is well formed, return its number of '='s + 2; otherwise,
|
** sequence is well formed, return its number of '='s + 2; otherwise,
|
||||||
** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...').
|
** return 1 if it is a single bracket (no '='s and no 2nd bracket);
|
||||||
|
** otherwise (an unfinished '[==...') return 0.
|
||||||
*/
|
*/
|
||||||
static size_t skip_sep (LexState *ls) {
|
static size_t skip_sep (LexState *ls) {
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
@@ -476,34 +485,34 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
}
|
}
|
||||||
case '=': {
|
case '=': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_EQ;
|
if (check_next1(ls, '=')) return TK_EQ; /* '==' */
|
||||||
else return '=';
|
else return '=';
|
||||||
}
|
}
|
||||||
case '<': {
|
case '<': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_LE;
|
if (check_next1(ls, '=')) return TK_LE; /* '<=' */
|
||||||
else if (check_next1(ls, '<')) return TK_SHL;
|
else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */
|
||||||
else return '<';
|
else return '<';
|
||||||
}
|
}
|
||||||
case '>': {
|
case '>': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_GE;
|
if (check_next1(ls, '=')) return TK_GE; /* '>=' */
|
||||||
else if (check_next1(ls, '>')) return TK_SHR;
|
else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */
|
||||||
else return '>';
|
else return '>';
|
||||||
}
|
}
|
||||||
case '/': {
|
case '/': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '/')) return TK_IDIV;
|
if (check_next1(ls, '/')) return TK_IDIV; /* '//' */
|
||||||
else return '/';
|
else return '/';
|
||||||
}
|
}
|
||||||
case '~': {
|
case '~': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_NE;
|
if (check_next1(ls, '=')) return TK_NE; /* '~=' */
|
||||||
else return '~';
|
else return '~';
|
||||||
}
|
}
|
||||||
case ':': {
|
case ':': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, ':')) return TK_DBCOLON;
|
if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */
|
||||||
else return ':';
|
else return ':';
|
||||||
}
|
}
|
||||||
case '"': case '\'': { /* short literal strings */
|
case '"': case '\'': { /* short literal strings */
|
||||||
@@ -542,7 +551,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
return TK_NAME;
|
return TK_NAME;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* single-char tokens (+ - / ...) */
|
else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */
|
||||||
int c = ls->current;
|
int c = ls->current;
|
||||||
next(ls);
|
next(ls);
|
||||||
return c;
|
return c;
|
||||||
|
|||||||
@@ -7,11 +7,17 @@
|
|||||||
#ifndef llex_h
|
#ifndef llex_h
|
||||||
#define llex_h
|
#define llex_h
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
#define FIRST_RESERVED 257
|
/*
|
||||||
|
** Single-char tokens (terminal symbols) are represented by their own
|
||||||
|
** numeric code. Other tokens start at the following value.
|
||||||
|
*/
|
||||||
|
#define FIRST_RESERVED (UCHAR_MAX + 1)
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_ENV)
|
#if !defined(LUA_ENV)
|
||||||
|
|||||||
@@ -71,11 +71,24 @@ typedef signed char ls_byte;
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** conversion of pointer to unsigned integer:
|
** conversion of pointer to unsigned integer: this is for hashing only;
|
||||||
** this is for hashing only; there is no problem if the integer
|
** there is no problem if the integer cannot hold the whole pointer
|
||||||
** cannot hold the whole pointer value
|
** value. (In strict ISO C this may cause undefined behavior, but no
|
||||||
|
** actual machine seems to bother.)
|
||||||
*/
|
*/
|
||||||
#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
|
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
|
||||||
|
__STDC_VERSION__ >= 199901L
|
||||||
|
#include <stdint.h>
|
||||||
|
#if defined(UINTPTR_MAX) /* even in C99 this type is optional */
|
||||||
|
#define L_P2I uintptr_t
|
||||||
|
#else /* no 'intptr'? */
|
||||||
|
#define L_P2I uintmax_t /* use the largest available integer */
|
||||||
|
#endif
|
||||||
|
#else /* C89 option */
|
||||||
|
#define L_P2I size_t
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -84,7 +97,15 @@ typedef LUAI_UACNUMBER l_uacNumber;
|
|||||||
typedef LUAI_UACINT l_uacInt;
|
typedef LUAI_UACINT l_uacInt;
|
||||||
|
|
||||||
|
|
||||||
/* internal assertions for in-house debugging */
|
/*
|
||||||
|
** Internal assertions for in-house debugging
|
||||||
|
*/
|
||||||
|
#if defined LUAI_ASSERT
|
||||||
|
#undef NDEBUG
|
||||||
|
#include <assert.h>
|
||||||
|
#define lua_assert(c) assert(c)
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(lua_assert)
|
#if defined(lua_assert)
|
||||||
#define check_exp(c,e) (lua_assert(c), (e))
|
#define check_exp(c,e) (lua_assert(c), (e))
|
||||||
/* to avoid problems with conditions too long */
|
/* to avoid problems with conditions too long */
|
||||||
@@ -99,7 +120,7 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
** assertion for checking API calls
|
** assertion for checking API calls
|
||||||
*/
|
*/
|
||||||
#if !defined(luai_apicheck)
|
#if !defined(luai_apicheck)
|
||||||
#define luai_apicheck(l,e) lua_assert(e)
|
#define luai_apicheck(l,e) ((void)l, lua_assert(e))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||||
@@ -141,22 +162,6 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** macros to improve jump prediction (used mainly for error handling)
|
|
||||||
*/
|
|
||||||
#if !defined(likely)
|
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
|
||||||
#define likely(x) (__builtin_expect(((x) != 0), 1))
|
|
||||||
#define unlikely(x) (__builtin_expect(((x) != 0), 0))
|
|
||||||
#else
|
|
||||||
#define likely(x) (x)
|
|
||||||
#define unlikely(x) (x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** non-return type
|
** non-return type
|
||||||
*/
|
*/
|
||||||
@@ -173,6 +178,20 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Inline functions
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
#define l_inline inline
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
#define l_inline __inline__
|
||||||
|
#else
|
||||||
|
#define l_inline /* empty */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define l_sinline static l_inline
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** type for virtual-machine instructions;
|
** type for virtual-machine instructions;
|
||||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||||
@@ -226,6 +245,17 @@ typedef l_uint32 Instruction;
|
|||||||
#endif
|
#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
|
** macros that are executed whenever program enters the Lua core
|
||||||
** ('lua_lock') and leaves the core ('lua_unlock')
|
** ('lua_lock') and leaves the core ('lua_unlock')
|
||||||
@@ -307,7 +337,8 @@ typedef l_uint32 Instruction;
|
|||||||
|
|
||||||
/* exponentiation */
|
/* exponentiation */
|
||||||
#if !defined(luai_numpow)
|
#if !defined(luai_numpow)
|
||||||
#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
|
#define luai_numpow(L,a,b) \
|
||||||
|
((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* the others are quite standard operations */
|
/* the others are quite standard operations */
|
||||||
@@ -336,14 +367,14 @@ typedef l_uint32 Instruction;
|
|||||||
#else
|
#else
|
||||||
/* realloc stack keeping its size */
|
/* realloc stack keeping its size */
|
||||||
#define condmovestack(L,pre,pos) \
|
#define condmovestack(L,pre,pos) \
|
||||||
{ int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; }
|
{ int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(HARDMEMTESTS)
|
#if !defined(HARDMEMTESTS)
|
||||||
#define condchangemem(L,pre,pos) ((void)0)
|
#define condchangemem(L,pre,pos) ((void)0)
|
||||||
#else
|
#else
|
||||||
#define condchangemem(L,pre,pos) \
|
#define condchangemem(L,pre,pos) \
|
||||||
{ if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
|
{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+63
-38
@@ -73,11 +73,11 @@ static int math_atan (lua_State *L) {
|
|||||||
static int math_toint (lua_State *L) {
|
static int math_toint (lua_State *L) {
|
||||||
int valid;
|
int valid;
|
||||||
lua_Integer n = lua_tointegerx(L, 1, &valid);
|
lua_Integer n = lua_tointegerx(L, 1, &valid);
|
||||||
if (valid)
|
if (l_likely(valid))
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
else {
|
else {
|
||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
lua_pushnil(L); /* value is not convertible to integer */
|
luaL_pushfail(L); /* value is not convertible to integer */
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -175,7 +175,8 @@ static int math_log (lua_State *L) {
|
|||||||
lua_Number base = luaL_checknumber(L, 2);
|
lua_Number base = luaL_checknumber(L, 2);
|
||||||
#if !defined(LUA_USE_C89)
|
#if !defined(LUA_USE_C89)
|
||||||
if (base == l_mathop(2.0))
|
if (base == l_mathop(2.0))
|
||||||
res = l_mathop(log2)(x); else
|
res = l_mathop(log2)(x);
|
||||||
|
else
|
||||||
#endif
|
#endif
|
||||||
if (base == l_mathop(10.0))
|
if (base == l_mathop(10.0))
|
||||||
res = l_mathop(log10)(x);
|
res = l_mathop(log10)(x);
|
||||||
@@ -235,7 +236,7 @@ static int math_type (lua_State *L) {
|
|||||||
lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
|
lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
|
||||||
else {
|
else {
|
||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -248,8 +249,17 @@ static int math_type (lua_State *L) {
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
** This code uses lots of shifts. ANSI 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 */
|
/* number of binary digits in the mantissa of a float */
|
||||||
#define FIGS l_mathlim(MANT_DIG)
|
#define FIGS l_floatatt(MANT_DIG)
|
||||||
|
|
||||||
#if FIGS > 64
|
#if FIGS > 64
|
||||||
/* there are only 64 random bits; use them all */
|
/* there are only 64 random bits; use them all */
|
||||||
@@ -266,20 +276,23 @@ static int math_type (lua_State *L) {
|
|||||||
|
|
||||||
/* try to find an integer type with at least 64 bits */
|
/* try to find an integer type with at least 64 bits */
|
||||||
|
|
||||||
#if (ULONG_MAX >> 31 >> 31) >= 3
|
#if ((ULONG_MAX >> 31) >> 31) >= 3
|
||||||
|
|
||||||
/* 'long' has at least 64 bits */
|
/* 'long' has at least 64 bits */
|
||||||
#define Rand64 unsigned long
|
#define Rand64 unsigned long
|
||||||
|
#define SRand64 long
|
||||||
|
|
||||||
#elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
|
#elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
|
||||||
|
|
||||||
/* there is a 'long long' type (which must have at least 64 bits) */
|
/* there is a 'long long' type (which must have at least 64 bits) */
|
||||||
#define Rand64 unsigned long long
|
#define Rand64 unsigned long long
|
||||||
|
#define SRand64 long long
|
||||||
|
|
||||||
#elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3
|
#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3
|
||||||
|
|
||||||
/* 'lua_Integer' has at least 64 bits */
|
/* 'lua_Unsigned' has at least 64 bits */
|
||||||
#define Rand64 lua_Unsigned
|
#define Rand64 lua_Unsigned
|
||||||
|
#define SRand64 lua_Integer
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -318,23 +331,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
|
** Convert bits from a random integer into a float in the
|
||||||
** interval [0,1), getting the higher FIG bits from the
|
** interval [0,1), getting the higher FIG bits from the
|
||||||
** random unsigned integer and converting that to a float.
|
** 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 */
|
/* must throw out the extra (64 - FIGS) bits */
|
||||||
#define shift64_FIG (64 - FIGS)
|
#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)))
|
#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
|
||||||
|
|
||||||
static lua_Number I2d (Rand64 x) {
|
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' */
|
/* convert a 'Rand64' to a 'lua_Unsigned' */
|
||||||
@@ -470,11 +490,9 @@ static lua_Number I2d (Rand64 x) {
|
|||||||
|
|
||||||
#else /* 32 < FIGS <= 64 */
|
#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) */
|
/* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */
|
||||||
#define scaleFIG \
|
#define scaleFIG \
|
||||||
((lua_Number)1.0 / (UONE << 30) / 8.0 / (UONE << (FIGS - 33)))
|
(l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33)))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** use FIGS - 32 bits from lower half, throwing out the other
|
** use FIGS - 32 bits from lower half, throwing out the other
|
||||||
@@ -485,7 +503,7 @@ static lua_Number I2d (Rand64 x) {
|
|||||||
/*
|
/*
|
||||||
** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32)
|
** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32)
|
||||||
*/
|
*/
|
||||||
#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * 2.0)
|
#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0))
|
||||||
|
|
||||||
|
|
||||||
static lua_Number I2d (Rand64 x) {
|
static lua_Number I2d (Rand64 x) {
|
||||||
@@ -499,12 +517,12 @@ static lua_Number I2d (Rand64 x) {
|
|||||||
|
|
||||||
/* convert a 'Rand64' to a 'lua_Unsigned' */
|
/* convert a 'Rand64' to a 'lua_Unsigned' */
|
||||||
static lua_Unsigned I2UInt (Rand64 x) {
|
static lua_Unsigned I2UInt (Rand64 x) {
|
||||||
return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l);
|
return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* convert a 'lua_Unsigned' to a 'Rand64' */
|
/* convert a 'lua_Unsigned' to a 'Rand64' */
|
||||||
static Rand64 Int2I (lua_Unsigned n) {
|
static Rand64 Int2I (lua_Unsigned n) {
|
||||||
return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n);
|
return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
@@ -522,16 +540,18 @@ typedef struct {
|
|||||||
** Project the random integer 'ran' into the interval [0, n].
|
** Project the random integer 'ran' into the interval [0, n].
|
||||||
** Because 'ran' has 2^B possible values, the projection can only be
|
** 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
|
** uniform when the size of the interval is a power of 2 (exact
|
||||||
** division). To get a uniform projection into [0, n], we first compute
|
** division). Otherwise, to get a uniform projection into [0, n], we
|
||||||
** 'lim', the smallest Mersenne number not smaller than 'n'. We then
|
** first compute 'lim', the smallest Mersenne number not smaller than
|
||||||
** project 'ran' into the interval [0, lim]. If the result is inside
|
** 'n'. We then project 'ran' into the interval [0, lim]. If the result
|
||||||
** [0, n], we are done. Otherwise, we try with another 'ran', until we
|
** is inside [0, n], we are done. Otherwise, we try with another 'ran',
|
||||||
** have a result inside the interval.
|
** until we have a result inside the interval.
|
||||||
*/
|
*/
|
||||||
static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
|
static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
|
||||||
RanState *state) {
|
RanState *state) {
|
||||||
lua_Unsigned lim = n;
|
if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
|
||||||
if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */
|
return ran & n; /* no bias */
|
||||||
|
else {
|
||||||
|
lua_Unsigned lim = n;
|
||||||
/* compute the smallest (2^b - 1) not smaller than 'n' */
|
/* compute the smallest (2^b - 1) not smaller than 'n' */
|
||||||
lim |= (lim >> 1);
|
lim |= (lim >> 1);
|
||||||
lim |= (lim >> 2);
|
lim |= (lim >> 2);
|
||||||
@@ -541,13 +561,13 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
|
|||||||
#if (LUA_MAXUNSIGNED >> 31) >= 3
|
#if (LUA_MAXUNSIGNED >> 31) >= 3
|
||||||
lim |= (lim >> 32); /* integer type has more than 32 bits */
|
lim |= (lim >> 32); /* integer type has more than 32 bits */
|
||||||
#endif
|
#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_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
|
|
||||||
&& lim >= n /* not smaller than 'n', */
|
|
||||||
&& (lim == 0 || (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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -586,7 +606,8 @@ static int math_random (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
|
static void setseed (lua_State *L, Rand64 *state,
|
||||||
|
lua_Unsigned n1, lua_Unsigned n2) {
|
||||||
int i;
|
int i;
|
||||||
state[0] = Int2I(n1);
|
state[0] = Int2I(n1);
|
||||||
state[1] = Int2I(0xff); /* avoid a zero state */
|
state[1] = Int2I(0xff); /* avoid a zero state */
|
||||||
@@ -594,6 +615,8 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
|
|||||||
state[3] = Int2I(0);
|
state[3] = Int2I(0);
|
||||||
for (i = 0; i < 16; i++)
|
for (i = 0; i < 16; i++)
|
||||||
nextrand(state); /* discard initial values to "spread" seed */
|
nextrand(state); /* discard initial values to "spread" seed */
|
||||||
|
lua_pushinteger(L, n1);
|
||||||
|
lua_pushinteger(L, n2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -605,20 +628,21 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
|
|||||||
static void randseed (lua_State *L, RanState *state) {
|
static void randseed (lua_State *L, RanState *state) {
|
||||||
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
|
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
|
||||||
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
|
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
|
||||||
setseed(state->s, seed1, seed2);
|
setseed(L, state->s, seed1, seed2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_randomseed (lua_State *L) {
|
static int math_randomseed (lua_State *L) {
|
||||||
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
if (lua_isnone(L, 1))
|
if (lua_isnone(L, 1)) {
|
||||||
randseed(L, state);
|
randseed(L, state);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
lua_Integer n1 = luaL_checkinteger(L, 1);
|
lua_Integer n1 = luaL_checkinteger(L, 1);
|
||||||
lua_Integer n2 = luaL_optinteger(L, 2, 0);
|
lua_Integer n2 = luaL_optinteger(L, 2, 0);
|
||||||
setseed(state->s, n1, n2);
|
setseed(L, state->s, n1, n2);
|
||||||
}
|
}
|
||||||
return 0;
|
return 2; /* return seeds */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -635,6 +659,7 @@ static const luaL_Reg randfuncs[] = {
|
|||||||
static void setrandfunc (lua_State *L) {
|
static void setrandfunc (lua_State *L) {
|
||||||
RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
|
RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
|
||||||
randseed(L, state); /* initialize with a "random" seed */
|
randseed(L, state); /* initialize with a "random" seed */
|
||||||
|
lua_pop(L, 2); /* remove pushed seeds */
|
||||||
luaL_setfuncs(L, randfuncs, 1);
|
luaL_setfuncs(L, randfuncs, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,34 +22,75 @@
|
|||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
#if defined(HARDMEMTESTS)
|
|
||||||
#define hardtest(L,os,s) /* force a GC whenever possible */ \
|
/*
|
||||||
if ((s) > (os) && (G(L))->gcrunning) luaC_fullgc(L, 1);
|
** About the realloc function:
|
||||||
|
** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||||
|
** ('osize' is the old size, 'nsize' is the new size)
|
||||||
|
**
|
||||||
|
** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
|
||||||
|
** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
|
||||||
|
** which is equivalent to free(NULL) in ISO C.
|
||||||
|
**
|
||||||
|
** - frealloc(ud, NULL, x, s) creates a new block of size 's'
|
||||||
|
** (no matter 'x'). Returns NULL if it cannot create the new block.
|
||||||
|
**
|
||||||
|
** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
|
||||||
|
** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
|
||||||
|
** block to the new size.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Macro to call the allocation function.
|
||||||
|
*/
|
||||||
|
#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** When an allocation fails, it will try again after an emergency
|
||||||
|
** collection, except when it cannot run a collection. The GC should
|
||||||
|
** not be called while the state is not fully built, as the collector
|
||||||
|
** is not yet fully initialized. Also, it should not be called when
|
||||||
|
** 'gcstopem' is true, because then the interpreter is in the middle of
|
||||||
|
** a collection step.
|
||||||
|
*/
|
||||||
|
#define cantryagain(g) (completestate(g) && !g->gcstopem)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(EMERGENCYGCTESTS)
|
||||||
|
/*
|
||||||
|
** First allocation will fail except when freeing a block (frees never
|
||||||
|
** fail) and when it cannot try again; this fail will trigger 'tryagain'
|
||||||
|
** and a full GC cycle at every allocation.
|
||||||
|
*/
|
||||||
|
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
|
||||||
|
if (ns > 0 && cantryagain(g))
|
||||||
|
return NULL; /* fail */
|
||||||
|
else /* normal allocation */
|
||||||
|
return callfrealloc(g, block, os, ns);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
#define hardtest(L,os,s) ((void)0)
|
#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** About the realloc function:
|
** {==================================================================
|
||||||
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
** Functions to allocate/deallocate arrays for the Parser
|
||||||
** ('osize' is the old size, 'nsize' is the new size)
|
** ===================================================================
|
||||||
**
|
|
||||||
** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
|
|
||||||
** matter 'x').
|
|
||||||
**
|
|
||||||
** * frealloc(ud, p, x, 0) frees the block 'p'
|
|
||||||
** (in this specific case, frealloc must return NULL);
|
|
||||||
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
|
||||||
** (which is equivalent to free(NULL) in ISO C)
|
|
||||||
**
|
|
||||||
** frealloc returns NULL if it cannot create or reallocate the area
|
|
||||||
** (any reallocation to an equal or smaller size cannot fail!)
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Minimum size for arrays during parsing, to avoid overhead of
|
||||||
|
** reallocating to size 1, then 2, and then 4. All these arrays
|
||||||
|
** will be reallocated to exact sizes or erased when parsing ends.
|
||||||
|
*/
|
||||||
#define MINSIZEARRAY 4
|
#define MINSIZEARRAY 4
|
||||||
|
|
||||||
|
|
||||||
@@ -60,7 +101,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
|||||||
if (nelems + 1 <= size) /* does one extra element still fit? */
|
if (nelems + 1 <= size) /* does one extra element still fit? */
|
||||||
return block; /* nothing to be done */
|
return block; /* nothing to be done */
|
||||||
if (size >= limit / 2) { /* cannot double it? */
|
if (size >= limit / 2) { /* cannot double it? */
|
||||||
if (unlikely(size >= limit)) /* cannot grow even a little? */
|
if (l_unlikely(size >= limit)) /* cannot grow even a little? */
|
||||||
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
|
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
|
||||||
size = limit; /* still have at least one free place */
|
size = limit; /* still have at least one free place */
|
||||||
}
|
}
|
||||||
@@ -71,32 +112,32 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
|||||||
}
|
}
|
||||||
lua_assert(nelems + 1 <= size && size <= limit);
|
lua_assert(nelems + 1 <= size && size <= limit);
|
||||||
/* 'limit' ensures that multiplication will not overflow */
|
/* 'limit' ensures that multiplication will not overflow */
|
||||||
newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems,
|
newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
|
||||||
cast_sizet(size) * size_elems);
|
cast_sizet(size) * size_elems);
|
||||||
if (unlikely(newblock == NULL))
|
|
||||||
luaM_error(L);
|
|
||||||
*psize = size; /* update only when everything else is OK */
|
*psize = size; /* update only when everything else is OK */
|
||||||
return newblock;
|
return newblock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** In prototypes, the size of the array is also its number of
|
||||||
|
** elements (to save memory). So, if it cannot shrink an array
|
||||||
|
** to its number of elements, the only option is to raise an
|
||||||
|
** error.
|
||||||
|
*/
|
||||||
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
||||||
int final_n, int size_elem) {
|
int final_n, int size_elem) {
|
||||||
global_State *g = G(L);
|
|
||||||
void *newblock;
|
void *newblock;
|
||||||
size_t oldsize = cast_sizet((*size) * size_elem);
|
size_t oldsize = cast_sizet((*size) * size_elem);
|
||||||
size_t newsize = cast_sizet(final_n * size_elem);
|
size_t newsize = cast_sizet(final_n * size_elem);
|
||||||
lua_assert(newsize <= oldsize);
|
lua_assert(newsize <= oldsize);
|
||||||
newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
|
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
|
||||||
if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */
|
*size = final_n;
|
||||||
luaM_error(L);
|
return newblock;
|
||||||
else {
|
|
||||||
g->GCdebt += newsize - oldsize;
|
|
||||||
*size = final_n;
|
|
||||||
return newblock;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
l_noret luaM_toobig (lua_State *L) {
|
l_noret luaM_toobig (lua_State *L) {
|
||||||
luaG_runerror(L, "memory allocation error: block too big");
|
luaG_runerror(L, "memory allocation error: block too big");
|
||||||
@@ -109,42 +150,38 @@ l_noret luaM_toobig (lua_State *L) {
|
|||||||
void luaM_free_ (lua_State *L, void *block, size_t osize) {
|
void luaM_free_ (lua_State *L, void *block, size_t osize) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
lua_assert((osize == 0) == (block == NULL));
|
lua_assert((osize == 0) == (block == NULL));
|
||||||
(*g->frealloc)(g->ud, block, osize, 0);
|
callfrealloc(g, block, osize, 0);
|
||||||
g->GCdebt -= osize;
|
g->GCdebt -= osize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** In case of allocation fail, this function will call the GC to try
|
** In case of allocation fail, this function will do an emergency
|
||||||
** to free some memory and then try the allocation again.
|
** collection to free some memory and then try the allocation again.
|
||||||
** (It should not be called when shrinking a block, because then the
|
|
||||||
** interpreter may be in the middle of a collection step.)
|
|
||||||
*/
|
*/
|
||||||
static void *tryagain (lua_State *L, void *block,
|
static void *tryagain (lua_State *L, void *block,
|
||||||
size_t osize, size_t nsize) {
|
size_t osize, size_t nsize) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
if (ttisnil(&g->nilvalue)) { /* is state fully build? */
|
if (cantryagain(g)) {
|
||||||
luaC_fullgc(L, 1); /* try to free some memory... */
|
luaC_fullgc(L, 1); /* try to free some memory... */
|
||||||
return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
return callfrealloc(g, block, osize, nsize); /* try again */
|
||||||
}
|
}
|
||||||
else return NULL; /* cannot free any memory without a full state */
|
else return NULL; /* cannot run an emergency collection */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** generic allocation routine.
|
** Generic allocation routine.
|
||||||
*/
|
*/
|
||||||
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||||
void *newblock;
|
void *newblock;
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
lua_assert((osize == 0) == (block == NULL));
|
lua_assert((osize == 0) == (block == NULL));
|
||||||
hardtest(L, osize, nsize);
|
newblock = firsttry(g, block, osize, nsize);
|
||||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
if (l_unlikely(newblock == NULL && nsize > 0)) {
|
||||||
if (unlikely(newblock == NULL && nsize > 0)) {
|
newblock = tryagain(L, block, osize, nsize);
|
||||||
if (nsize > osize) /* not shrinking a block? */
|
|
||||||
newblock = tryagain(L, block, osize, nsize);
|
|
||||||
if (newblock == NULL) /* still no memory? */
|
if (newblock == NULL) /* still no memory? */
|
||||||
return NULL;
|
return NULL; /* do not update 'GCdebt' */
|
||||||
}
|
}
|
||||||
lua_assert((nsize == 0) == (newblock == NULL));
|
lua_assert((nsize == 0) == (newblock == NULL));
|
||||||
g->GCdebt = (g->GCdebt + nsize) - osize;
|
g->GCdebt = (g->GCdebt + nsize) - osize;
|
||||||
@@ -155,20 +192,19 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
|||||||
void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
|
void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
|
||||||
size_t nsize) {
|
size_t nsize) {
|
||||||
void *newblock = luaM_realloc_(L, block, osize, nsize);
|
void *newblock = luaM_realloc_(L, block, osize, nsize);
|
||||||
if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
|
if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
|
||||||
luaM_error(L);
|
luaM_error(L);
|
||||||
return newblock;
|
return newblock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
||||||
hardtest(L, 0, size);
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return NULL; /* that's all */
|
return NULL; /* that's all */
|
||||||
else {
|
else {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
void *newblock = (*g->frealloc)(g->ud, NULL, tag, size);
|
void *newblock = firsttry(g, NULL, tag, size);
|
||||||
if (unlikely(newblock == NULL)) {
|
if (l_unlikely(newblock == NULL)) {
|
||||||
newblock = tryagain(L, NULL, tag, size);
|
newblock = tryagain(L, NULL, tag, size);
|
||||||
if (newblock == NULL)
|
if (newblock == NULL)
|
||||||
luaM_error(L);
|
luaM_error(L);
|
||||||
|
|||||||
@@ -24,15 +24,6 @@
|
|||||||
#include "lualib.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
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** LUA_CSUBSEP is the character that replaces dots in submodule names
|
** LUA_CSUBSEP is the character that replaces dots in submodule names
|
||||||
** when searching for a C loader.
|
** when searching for a C loader.
|
||||||
@@ -56,10 +47,10 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** unique key for table in the registry that keeps handles
|
** key for table in the registry that keeps handles
|
||||||
** for all loaded C libraries
|
** for all loaded C libraries
|
||||||
*/
|
*/
|
||||||
static const int CLIBS = 0;
|
static const char *const CLIBS = "_CLIBS";
|
||||||
|
|
||||||
#define LIB_FAIL "open"
|
#define LIB_FAIL "open"
|
||||||
|
|
||||||
@@ -67,6 +58,13 @@ static const int CLIBS = 0;
|
|||||||
#define setprogdir(L) ((void)0)
|
#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);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** system-dependent functions
|
** system-dependent functions
|
||||||
*/
|
*/
|
||||||
@@ -125,14 +123,16 @@ static void lsys_unloadlib (void *lib) {
|
|||||||
|
|
||||||
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
||||||
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
|
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
|
||||||
if (lib == NULL) lua_pushstring(L, dlerror());
|
if (l_unlikely(lib == NULL))
|
||||||
|
lua_pushstring(L, dlerror());
|
||||||
return lib;
|
return lib;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
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_func(dlsym(lib, sym));
|
||||||
if (f == NULL) lua_pushstring(L, dlerror());
|
if (l_unlikely(f == NULL))
|
||||||
|
lua_pushstring(L, dlerror());
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +206,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) {
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||||
lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
|
lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
|
||||||
if (f == NULL) pusherror(L);
|
if (f == NULL) pusherror(L);
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@@ -269,8 +269,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define AUXMARK "\1" /* auxiliary mark */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** return registry.LUA_NOENV as a boolean
|
** return registry.LUA_NOENV as a boolean
|
||||||
@@ -308,7 +306,7 @@ static void setpath (lua_State *L, const char *fieldname,
|
|||||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
luaL_addchar(&b, *LUA_PATH_SEP);
|
||||||
}
|
}
|
||||||
luaL_addstring(&b, dft); /* add default */
|
luaL_addstring(&b, dft); /* add default */
|
||||||
if (dftmark < path + len - 2) { /* is there a sufix after ';;'? */
|
if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
|
||||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
luaL_addchar(&b, *LUA_PATH_SEP);
|
||||||
luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
|
luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
|
||||||
}
|
}
|
||||||
@@ -327,7 +325,7 @@ static void setpath (lua_State *L, const char *fieldname,
|
|||||||
*/
|
*/
|
||||||
static void *checkclib (lua_State *L, const char *path) {
|
static void *checkclib (lua_State *L, const char *path) {
|
||||||
void *plib;
|
void *plib;
|
||||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||||
lua_getfield(L, -1, path);
|
lua_getfield(L, -1, path);
|
||||||
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||||
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
||||||
@@ -340,7 +338,7 @@ static void *checkclib (lua_State *L, const char *path) {
|
|||||||
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
||||||
*/
|
*/
|
||||||
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
||||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||||
lua_pushlightuserdata(L, plib);
|
lua_pushlightuserdata(L, plib);
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||||
@@ -405,13 +403,13 @@ static int ll_loadlib (lua_State *L) {
|
|||||||
const char *path = luaL_checkstring(L, 1);
|
const char *path = luaL_checkstring(L, 1);
|
||||||
const char *init = luaL_checkstring(L, 2);
|
const char *init = luaL_checkstring(L, 2);
|
||||||
int stat = lookforfunc(L, path, init);
|
int stat = lookforfunc(L, path, init);
|
||||||
if (stat == 0) /* no errors? */
|
if (l_likely(stat == 0)) /* no errors? */
|
||||||
return 1; /* return the loaded function */
|
return 1; /* return the loaded function */
|
||||||
else { /* error; error message is on stack top */
|
else { /* error; error message is on stack top */
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
|
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
|
||||||
return 3; /* return nil, error message, and where */
|
return 3; /* return fail, error message, and where */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,13 +456,13 @@ static const char *getnextfilename (char **path, char *end) {
|
|||||||
/*
|
/*
|
||||||
** Given a path such as ";blabla.so;blublu.so", pushes the string
|
** Given a path such as ";blabla.so;blublu.so", pushes the string
|
||||||
**
|
**
|
||||||
** no file 'blabla.so'
|
** no file 'blabla.so'
|
||||||
** no file 'blublu.so'
|
** no file 'blublu.so'
|
||||||
*/
|
*/
|
||||||
static void pusherrornotfound (lua_State *L, const char *path) {
|
static void pusherrornotfound (lua_State *L, const char *path) {
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
luaL_addstring(&b, "\n\tno file '");
|
luaL_addstring(&b, "no file '");
|
||||||
luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
|
luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
|
||||||
luaL_addstring(&b, "'");
|
luaL_addstring(&b, "'");
|
||||||
luaL_pushresult(&b);
|
luaL_pushresult(&b);
|
||||||
@@ -505,9 +503,9 @@ static int ll_searchpath (lua_State *L) {
|
|||||||
luaL_optstring(L, 4, LUA_DIRSEP));
|
luaL_optstring(L, 4, LUA_DIRSEP));
|
||||||
if (f != NULL) return 1;
|
if (f != NULL) return 1;
|
||||||
else { /* error message is on top of the stack */
|
else { /* error message is on top of the stack */
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
return 2; /* return nil + error message */
|
return 2; /* return fail + error message */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -518,14 +516,14 @@ static const char *findfile (lua_State *L, const char *name,
|
|||||||
const char *path;
|
const char *path;
|
||||||
lua_getfield(L, lua_upvalueindex(1), pname);
|
lua_getfield(L, lua_upvalueindex(1), pname);
|
||||||
path = lua_tostring(L, -1);
|
path = lua_tostring(L, -1);
|
||||||
if (path == NULL)
|
if (l_unlikely(path == NULL))
|
||||||
luaL_error(L, "'package.%s' must be a string", pname);
|
luaL_error(L, "'package.%s' must be a string", pname);
|
||||||
return searchpath(L, name, path, ".", dirsep);
|
return searchpath(L, name, path, ".", dirsep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int checkload (lua_State *L, int stat, const char *filename) {
|
static int checkload (lua_State *L, int stat, const char *filename) {
|
||||||
if (stat) { /* module loaded successfully? */
|
if (l_likely(stat)) { /* module loaded successfully? */
|
||||||
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
||||||
return 2; /* return open function and file name */
|
return 2; /* return open function and file name */
|
||||||
}
|
}
|
||||||
@@ -591,7 +589,7 @@ static int searcher_Croot (lua_State *L) {
|
|||||||
if (stat != ERRFUNC)
|
if (stat != ERRFUNC)
|
||||||
return checkload(L, 0, filename); /* real error */
|
return checkload(L, 0, filename); /* real error */
|
||||||
else { /* open function not found */
|
else { /* open function not found */
|
||||||
lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
|
lua_pushfstring(L, "no module '%s' in file '%s'", name, filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -604,7 +602,7 @@ static int searcher_preload (lua_State *L) {
|
|||||||
const char *name = luaL_checkstring(L, 1);
|
const char *name = luaL_checkstring(L, 1);
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||||
if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
|
if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
|
||||||
lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
|
lua_pushfstring(L, "no field package.preload['%s']", name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -618,13 +616,16 @@ static void findloader (lua_State *L, const char *name) {
|
|||||||
int i;
|
int i;
|
||||||
luaL_Buffer msg; /* to build error message */
|
luaL_Buffer msg; /* to build error message */
|
||||||
/* push 'package.searchers' to index 3 in the stack */
|
/* push 'package.searchers' to index 3 in the stack */
|
||||||
if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
|
if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers")
|
||||||
|
!= LUA_TTABLE))
|
||||||
luaL_error(L, "'package.searchers' must be a table");
|
luaL_error(L, "'package.searchers' must be a table");
|
||||||
luaL_buffinit(L, &msg);
|
luaL_buffinit(L, &msg);
|
||||||
/* iterate over available searchers to find a loader */
|
/* iterate over available searchers to find a loader */
|
||||||
for (i = 1; ; i++) {
|
for (i = 1; ; i++) {
|
||||||
if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
|
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 */
|
lua_pop(L, 1); /* remove nil */
|
||||||
|
luaL_buffsub(&msg, 2); /* remove prefix */
|
||||||
luaL_pushresult(&msg); /* create error message */
|
luaL_pushresult(&msg); /* create error message */
|
||||||
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
||||||
}
|
}
|
||||||
@@ -636,8 +637,10 @@ static void findloader (lua_State *L, const char *name) {
|
|||||||
lua_pop(L, 1); /* remove extra return */
|
lua_pop(L, 1); /* remove extra return */
|
||||||
luaL_addvalue(&msg); /* concatenate error message */
|
luaL_addvalue(&msg); /* concatenate error message */
|
||||||
}
|
}
|
||||||
else
|
else { /* no error message */
|
||||||
lua_pop(L, 2); /* remove both returns */
|
lua_pop(L, 2); /* remove both returns */
|
||||||
|
luaL_buffsub(&msg, 2); /* remove prefix */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -696,8 +699,13 @@ static const luaL_Reg ll_funcs[] = {
|
|||||||
|
|
||||||
|
|
||||||
static void createsearcherstable (lua_State *L) {
|
static void createsearcherstable (lua_State *L) {
|
||||||
static const lua_CFunction searchers[] =
|
static const lua_CFunction searchers[] = {
|
||||||
{searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
|
searcher_preload,
|
||||||
|
searcher_Lua,
|
||||||
|
searcher_C,
|
||||||
|
searcher_Croot,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
int i;
|
int i;
|
||||||
/* create 'searchers' table */
|
/* create 'searchers' table */
|
||||||
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
|
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
|
||||||
@@ -716,12 +724,11 @@ static void createsearcherstable (lua_State *L) {
|
|||||||
** setting a finalizer to close all libraries when closing state.
|
** setting a finalizer to close all libraries when closing state.
|
||||||
*/
|
*/
|
||||||
static void createclibstable (lua_State *L) {
|
static void createclibstable (lua_State *L) {
|
||||||
lua_newtable(L); /* create CLIBS table */
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
||||||
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
||||||
lua_pushcfunction(L, gctm);
|
lua_pushcfunction(L, gctm);
|
||||||
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||||
lua_setmetatable(L, -2);
|
lua_setmetatable(L, -2);
|
||||||
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,32 +29,6 @@
|
|||||||
#include "lvm.h"
|
#include "lvm.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** converts an integer to a "floating point byte", represented as
|
|
||||||
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
|
||||||
** eeeee != 0 and (xxx) otherwise.
|
|
||||||
*/
|
|
||||||
int luaO_int2fb (unsigned int x) {
|
|
||||||
int e = 0; /* exponent */
|
|
||||||
if (x < 8) return x;
|
|
||||||
while (x >= (8 << 4)) { /* coarse steps */
|
|
||||||
x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
|
|
||||||
e += 4;
|
|
||||||
}
|
|
||||||
while (x >= (8 << 1)) { /* fine steps */
|
|
||||||
x = (x + 1) >> 1; /* x = ceil(x / 2) */
|
|
||||||
e++;
|
|
||||||
}
|
|
||||||
return ((e+1) << 3) | (cast_int(x) - 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* converts back */
|
|
||||||
int luaO_fb2int (int x) {
|
|
||||||
return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Computes ceil(log2(x))
|
** Computes ceil(log2(x))
|
||||||
*/
|
*/
|
||||||
@@ -88,7 +62,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
|
|||||||
case LUA_OPBOR: return intop(|, v1, v2);
|
case LUA_OPBOR: return intop(|, v1, v2);
|
||||||
case LUA_OPBXOR: return intop(^, v1, v2);
|
case LUA_OPBXOR: return intop(^, v1, v2);
|
||||||
case LUA_OPSHL: return luaV_shiftl(v1, v2);
|
case LUA_OPSHL: return luaV_shiftl(v1, v2);
|
||||||
case LUA_OPSHR: return luaV_shiftl(v1, -v2);
|
case LUA_OPSHR: return luaV_shiftr(v1, v2);
|
||||||
case LUA_OPUNM: return intop(-, 0, v1);
|
case LUA_OPUNM: return intop(-, 0, v1);
|
||||||
case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
|
case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
|
||||||
default: lua_assert(0); return 0;
|
default: lua_assert(0); return 0;
|
||||||
@@ -190,7 +164,7 @@ static int isneg (const char **s) {
|
|||||||
*/
|
*/
|
||||||
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||||
int dot = lua_getlocaledecpoint();
|
int dot = lua_getlocaledecpoint();
|
||||||
lua_Number r = 0.0; /* result (accumulator) */
|
lua_Number r = l_mathop(0.0); /* result (accumulator) */
|
||||||
int sigdig = 0; /* number of significant digits */
|
int sigdig = 0; /* number of significant digits */
|
||||||
int nosigdig = 0; /* number of non-significant digits */
|
int nosigdig = 0; /* number of non-significant digits */
|
||||||
int e = 0; /* exponent correction */
|
int e = 0; /* exponent correction */
|
||||||
@@ -200,7 +174,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
||||||
neg = isneg(&s); /* check sign */
|
neg = isneg(&s); /* check sign */
|
||||||
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
||||||
return 0.0; /* invalid format (no '0x') */
|
return l_mathop(0.0); /* invalid format (no '0x') */
|
||||||
for (s += 2; ; s++) { /* skip '0x' and read numeral */
|
for (s += 2; ; s++) { /* skip '0x' and read numeral */
|
||||||
if (*s == dot) {
|
if (*s == dot) {
|
||||||
if (hasdot) break; /* second dot? stop loop */
|
if (hasdot) break; /* second dot? stop loop */
|
||||||
@@ -210,14 +184,14 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
|
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
|
||||||
nosigdig++;
|
nosigdig++;
|
||||||
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
||||||
r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
|
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 */
|
if (hasdot) e--; /* decimal digit? correct exponent */
|
||||||
}
|
}
|
||||||
else break; /* neither a dot nor a digit */
|
else break; /* neither a dot nor a digit */
|
||||||
}
|
}
|
||||||
if (nosigdig + sigdig == 0) /* no digits? */
|
if (nosigdig + sigdig == 0) /* no digits? */
|
||||||
return 0.0; /* invalid format */
|
return l_mathop(0.0); /* invalid format */
|
||||||
*endptr = cast_charp(s); /* valid up to here */
|
*endptr = cast_charp(s); /* valid up to here */
|
||||||
e *= 4; /* each digit multiplies/divides value by 2^4 */
|
e *= 4; /* each digit multiplies/divides value by 2^4 */
|
||||||
if (*s == 'p' || *s == 'P') { /* exponent part? */
|
if (*s == 'p' || *s == 'P') { /* exponent part? */
|
||||||
@@ -226,7 +200,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
s++; /* skip 'p' */
|
s++; /* skip 'p' */
|
||||||
neg1 = isneg(&s); /* sign */
|
neg1 = isneg(&s); /* sign */
|
||||||
if (!lisdigit(cast_uchar(*s)))
|
if (!lisdigit(cast_uchar(*s)))
|
||||||
return 0.0; /* invalid; must have at least one digit */
|
return l_mathop(0.0); /* invalid; must have at least one digit */
|
||||||
while (lisdigit(cast_uchar(*s))) /* read exponent */
|
while (lisdigit(cast_uchar(*s))) /* read exponent */
|
||||||
exp1 = exp1 * 10 + *(s++) - '0';
|
exp1 = exp1 * 10 + *(s++) - '0';
|
||||||
if (neg1) exp1 = -exp1;
|
if (neg1) exp1 = -exp1;
|
||||||
@@ -241,37 +215,42 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
/* maximum length of a numeral */
|
/* maximum length of a numeral to be converted to a number */
|
||||||
#if !defined (L_MAXLENNUM)
|
#if !defined (L_MAXLENNUM)
|
||||||
#define L_MAXLENNUM 200
|
#define L_MAXLENNUM 200
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Convert string 's' to a Lua number (put in 'result'). Return NULL on
|
||||||
|
** fail or the address of the ending '\0' on success. ('mode' == 'x')
|
||||||
|
** means a hexadecimal numeral.
|
||||||
|
*/
|
||||||
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
|
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
|
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
|
||||||
: lua_str2number(s, &endptr);
|
: lua_str2number(s, &endptr);
|
||||||
if (endptr == s) return NULL; /* nothing recognized? */
|
if (endptr == s) return NULL; /* nothing recognized? */
|
||||||
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
|
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
|
||||||
return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */
|
return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Convert string 's' to a Lua number (put in 'result'). Return NULL
|
** Convert string 's' to a Lua number (put in 'result') handling the
|
||||||
** on fail or the address of the ending '\0' on success.
|
** current locale.
|
||||||
** 'pmode' points to (and 'mode' contains) special things in the string:
|
|
||||||
** - 'x'/'X' means a hexadecimal numeral
|
|
||||||
** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
|
|
||||||
** - '.' just optimizes the search for the common case (nothing special)
|
|
||||||
** This function accepts both the current locale or a dot as the radix
|
** This function accepts both the current locale or a dot as the radix
|
||||||
** mark. If the conversion fails, it may mean number has a dot but
|
** mark. If the conversion fails, it may mean number has a dot but
|
||||||
** locale accepts something else. In that case, the code copies 's'
|
** locale accepts something else. In that case, the code copies 's'
|
||||||
** to a buffer (because 's' is read-only), changes the dot to the
|
** to a buffer (because 's' is read-only), changes the dot to the
|
||||||
** current locale radix mark, and tries to convert again.
|
** current locale radix mark, and tries to convert again.
|
||||||
|
** The variable 'mode' checks for special characters in the string:
|
||||||
|
** - 'n' means 'inf' or 'nan' (which should be rejected)
|
||||||
|
** - 'x' means a hexadecimal numeral
|
||||||
|
** - '.' just optimizes the search for the common case (no special chars)
|
||||||
*/
|
*/
|
||||||
static const char *l_str2d (const char *s, lua_Number *result) {
|
static const char *l_str2d (const char *s, lua_Number *result) {
|
||||||
const char *endptr;
|
const char *endptr;
|
||||||
const char *pmode = strpbrk(s, ".xXnN");
|
const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
|
||||||
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
|
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
|
||||||
if (mode == 'n') /* reject 'inf' and 'nan' */
|
if (mode == 'n') /* reject 'inf' and 'nan' */
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -279,7 +258,7 @@ static const char *l_str2d (const char *s, lua_Number *result) {
|
|||||||
if (endptr == NULL) { /* failed? may be a different locale */
|
if (endptr == NULL) { /* failed? may be a different locale */
|
||||||
char buff[L_MAXLENNUM + 1];
|
char buff[L_MAXLENNUM + 1];
|
||||||
const char *pdot = strchr(s, '.');
|
const char *pdot = strchr(s, '.');
|
||||||
if (strlen(s) > L_MAXLENNUM || pdot == NULL)
|
if (pdot == NULL || strlen(s) > L_MAXLENNUM)
|
||||||
return NULL; /* string too long or no dot; fail */
|
return NULL; /* string too long or no dot; fail */
|
||||||
strcpy(buff, s); /* copy string to buffer */
|
strcpy(buff, s); /* copy string to buffer */
|
||||||
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
|
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
|
||||||
@@ -359,8 +338,15 @@ int luaO_utf8esc (char *buff, unsigned long x) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* maximum length of the conversion of a number to a string */
|
/*
|
||||||
#define MAXNUMBER2STR 50
|
** 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.)
|
||||||
|
*/
|
||||||
|
#define MAXNUMBER2STR 44
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -400,31 +386,39 @@ void luaO_tostring (lua_State *L, TValue *obj) {
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* size for buffer space used by 'luaO_pushvfstring' */
|
/*
|
||||||
#define BUFVFS 400
|
** 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.
|
||||||
|
*/
|
||||||
|
#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95)
|
||||||
|
|
||||||
/* buffer used by 'luaO_pushvfstring' */
|
/* buffer used by 'luaO_pushvfstring' */
|
||||||
typedef struct BuffFS {
|
typedef struct BuffFS {
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
int pushed; /* number of string pieces already on the stack */
|
int pushed; /* true if there is a part of the result on the stack */
|
||||||
int blen; /* length of partial string in 'space' */
|
int blen; /* length of partial string in 'space' */
|
||||||
char space[BUFVFS]; /* holds last part of the result */
|
char space[BUFVFS]; /* holds last part of the result */
|
||||||
} BuffFS;
|
} BuffFS;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Push given string to the stack, as part of the buffer. If the stack
|
** Push given string to the stack, as part of the result, and
|
||||||
** is almost full, join all partial strings in the stack into one.
|
** 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.
|
||||||
*/
|
*/
|
||||||
static void pushstr (BuffFS *buff, const char *str, size_t l) {
|
static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
|
||||||
lua_State *L = buff->L;
|
lua_State *L = buff->L;
|
||||||
setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
|
setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr));
|
||||||
L->top++; /* may use one extra slot */
|
L->top.p++; /* may use one slot from EXTRA_STACK */
|
||||||
buff->pushed++;
|
if (!buff->pushed) /* no previous string on the stack? */
|
||||||
if (buff->pushed > 1 && L->top + 1 >= L->stack_last) {
|
buff->pushed = 1; /* now there is one */
|
||||||
luaV_concat(L, buff->pushed); /* join all partial results into one */
|
else /* join previous string with new one */
|
||||||
buff->pushed = 1;
|
luaV_concat(L, 2);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -470,7 +464,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Add a number to the buffer.
|
** Add a numeral to the buffer.
|
||||||
*/
|
*/
|
||||||
static void addnum2buff (BuffFS *buff, TValue *num) {
|
static void addnum2buff (BuffFS *buff, TValue *num) {
|
||||||
char *numbuff = getbuff(buff, MAXNUMBER2STR);
|
char *numbuff = getbuff(buff, MAXNUMBER2STR);
|
||||||
@@ -547,9 +541,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
|||||||
}
|
}
|
||||||
addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
|
addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
|
||||||
clearbuff(&buff); /* empty buffer into the stack */
|
clearbuff(&buff); /* empty buffer into the stack */
|
||||||
if (buff.pushed > 1)
|
lua_assert(buff.pushed == 1);
|
||||||
luaV_concat(L, buff.pushed); /* join all partial results */
|
return getstr(tsvalue(s2v(L->top.p - 1)));
|
||||||
return svalue(s2v(L->top - 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,24 +17,30 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Extra tags for non-values
|
** Extra types for collectable non-values
|
||||||
*/
|
*/
|
||||||
#define LUA_TUPVAL LUA_NUMTAGS /* upvalues */
|
#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */
|
||||||
#define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */
|
#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */
|
||||||
|
#define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** number of all possible tags (including LUA_TNONE)
|
** number of all possible types (including LUA_TNONE but excluding DEADKEY)
|
||||||
*/
|
*/
|
||||||
#define LUA_TOTALTAGS (LUA_TPROTO + 2)
|
#define LUA_TOTALTYPES (LUA_TPROTO + 2)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** tags for Tagged Values have the following use of bits:
|
** tags for Tagged Values have the following use of bits:
|
||||||
** bits 0-3: actual tag (a LUA_T* value)
|
** bits 0-3: actual tag (a LUA_T* constant)
|
||||||
** bits 4-5: variant bits
|
** bits 4-5: variant bits
|
||||||
** bit 6: whether value is collectable
|
** bit 6: whether value is collectable
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* add variant bits to a type */
|
||||||
|
#define makevariant(t,v) ((t) | ((v) << 4))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -43,10 +49,11 @@
|
|||||||
typedef union Value {
|
typedef union Value {
|
||||||
struct GCObject *gc; /* collectable objects */
|
struct GCObject *gc; /* collectable objects */
|
||||||
void *p; /* light userdata */
|
void *p; /* light userdata */
|
||||||
int b; /* booleans */
|
|
||||||
lua_CFunction f; /* light C functions */
|
lua_CFunction f; /* light C functions */
|
||||||
lua_Integer i; /* integer numbers */
|
lua_Integer i; /* integer numbers */
|
||||||
lua_Number n; /* float numbers */
|
lua_Number n; /* float numbers */
|
||||||
|
/* not used, but may avoid warnings for uninitialized value */
|
||||||
|
lu_byte ub;
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +70,7 @@ typedef struct TValue {
|
|||||||
|
|
||||||
|
|
||||||
#define val_(o) ((o)->value_)
|
#define val_(o) ((o)->value_)
|
||||||
#define valraw(o) (&val_(o))
|
#define valraw(o) (val_(o))
|
||||||
|
|
||||||
|
|
||||||
/* raw type tag of a TValue */
|
/* raw type tag of a TValue */
|
||||||
@@ -86,24 +93,36 @@ typedef struct TValue {
|
|||||||
|
|
||||||
|
|
||||||
/* Macros for internal tests */
|
/* Macros for internal tests */
|
||||||
|
|
||||||
|
/* collectable object has the same tag as the original value */
|
||||||
#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)
|
#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Any value being manipulated by the program either is non
|
||||||
|
** collectable, or the collectable object has the right tag
|
||||||
|
** and it is not dead. The option 'L == NULL' allows other
|
||||||
|
** macros using this one to be used where L is not available.
|
||||||
|
*/
|
||||||
#define checkliveness(L,obj) \
|
#define checkliveness(L,obj) \
|
||||||
lua_longassert(!iscollectable(obj) || \
|
((void)L, lua_longassert(!iscollectable(obj) || \
|
||||||
(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
|
(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
|
||||||
|
|
||||||
|
|
||||||
/* Macros to set values */
|
/* Macros to set values */
|
||||||
|
|
||||||
|
/* set a value's tag */
|
||||||
#define settt_(o,t) ((o)->tt_=(t))
|
#define settt_(o,t) ((o)->tt_=(t))
|
||||||
|
|
||||||
|
|
||||||
|
/* main macro to copy values (from 'obj2' to 'obj1') */
|
||||||
#define setobj(L,obj1,obj2) \
|
#define setobj(L,obj1,obj2) \
|
||||||
{ TValue *io1=(obj1); const TValue *io2=(obj2); \
|
{ TValue *io1=(obj1); const TValue *io2=(obj2); \
|
||||||
io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
|
io1->value_ = io2->value_; settt_(io1, io2->tt_); \
|
||||||
(void)L; checkliveness(L,io1); lua_assert(!isreallyempty(io1)); }
|
checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** different types of assignments, according to destination
|
** Different types of assignments, according to source and destination.
|
||||||
|
** (They are mostly equal now, but may be different in the future.)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* from stack to stack */
|
/* from stack to stack */
|
||||||
@@ -118,13 +137,36 @@ typedef struct TValue {
|
|||||||
#define setobj2t setobj
|
#define setobj2t setobj
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Entries in a Lua stack. Field 'tbclist' forms a list of all
|
||||||
|
** to-be-closed variables active in this stack. Dummy entries are
|
||||||
|
** used when the distance between two tbc variables does not fit
|
||||||
|
** in an unsigned short. They are represented by delta==0, and
|
||||||
|
** their real delta is always the maximum value that fits in
|
||||||
|
** that field.
|
||||||
|
*/
|
||||||
typedef union StackValue {
|
typedef union StackValue {
|
||||||
TValue val;
|
TValue val;
|
||||||
|
struct {
|
||||||
|
TValuefields;
|
||||||
|
unsigned short delta;
|
||||||
|
} tbclist;
|
||||||
} StackValue;
|
} StackValue;
|
||||||
|
|
||||||
|
|
||||||
typedef StackValue *StkId; /* index to stack elements */
|
/* index to stack elements */
|
||||||
|
typedef StackValue *StkId;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** When reallocating the stack, change all pointers to the stack into
|
||||||
|
** proper offsets.
|
||||||
|
*/
|
||||||
|
typedef union {
|
||||||
|
StkId p; /* actual pointer */
|
||||||
|
ptrdiff_t offset; /* used while the stack is being reallocated */
|
||||||
|
} StkIdRel;
|
||||||
|
|
||||||
|
|
||||||
/* convert a 'StackValue' to a 'TValue' */
|
/* convert a 'StackValue' to a 'TValue' */
|
||||||
#define s2v(o) (&(o)->val)
|
#define s2v(o) (&(o)->val)
|
||||||
@@ -137,36 +179,34 @@ typedef StackValue *StkId; /* index to stack elements */
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* Standard nil */
|
||||||
|
#define LUA_VNIL makevariant(LUA_TNIL, 0)
|
||||||
|
|
||||||
|
/* Empty slot (which might be different from a slot containing nil) */
|
||||||
|
#define LUA_VEMPTY makevariant(LUA_TNIL, 1)
|
||||||
|
|
||||||
|
/* Value returned for a key not found in a table (absent key) */
|
||||||
|
#define LUA_VABSTKEY makevariant(LUA_TNIL, 2)
|
||||||
|
|
||||||
|
|
||||||
/* macro to test for (any kind of) nil */
|
/* macro to test for (any kind of) nil */
|
||||||
#define ttisnil(v) checktype((v), LUA_TNIL)
|
#define ttisnil(v) checktype((v), LUA_TNIL)
|
||||||
|
|
||||||
/* macro to test for a "pure" nil */
|
|
||||||
#define ttisstrictnil(o) checktag((o), LUA_TNIL)
|
/* macro to test for a standard nil */
|
||||||
|
#define ttisstrictnil(o) checktag((o), LUA_VNIL)
|
||||||
|
|
||||||
|
|
||||||
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
|
#define setnilvalue(obj) settt_(obj, LUA_VNIL)
|
||||||
|
|
||||||
|
|
||||||
/*
|
#define isabstkey(v) checktag((v), LUA_VABSTKEY)
|
||||||
** Variant tag, used only in tables to signal an empty slot
|
|
||||||
** (which might be different from a slot containing nil)
|
|
||||||
*/
|
|
||||||
#define LUA_TEMPTY (LUA_TNIL | (1 << 4))
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Variant used only in the value returned for a key not found in a
|
|
||||||
** table (absent key).
|
|
||||||
*/
|
|
||||||
#define LUA_TABSTKEY (LUA_TNIL | (2 << 4))
|
|
||||||
|
|
||||||
|
|
||||||
#define isabstkey(v) checktag((v), LUA_TABSTKEY)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** macro to detect non-standard nils (used only in assertions)
|
** macro to detect non-standard nils (used only in assertions)
|
||||||
*/
|
*/
|
||||||
#define isreallyempty(v) (ttisnil(v) && !ttisstrictnil(v))
|
#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -178,11 +218,11 @@ typedef StackValue *StkId; /* index to stack elements */
|
|||||||
|
|
||||||
|
|
||||||
/* macro defining a value corresponding to an absent key */
|
/* macro defining a value corresponding to an absent key */
|
||||||
#define ABSTKEYCONSTANT {NULL}, LUA_TABSTKEY
|
#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY
|
||||||
|
|
||||||
|
|
||||||
/* mark an entry as empty */
|
/* mark an entry as empty */
|
||||||
#define setempty(v) settt_(v, LUA_TEMPTY)
|
#define setempty(v) settt_(v, LUA_VEMPTY)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -195,16 +235,20 @@ typedef StackValue *StkId; /* index to stack elements */
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
|
|
||||||
|
|
||||||
#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
|
#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)
|
||||||
|
#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)
|
||||||
|
|
||||||
#define bvalueraw(v) ((v).b)
|
#define ttisboolean(o) checktype((o), LUA_TBOOLEAN)
|
||||||
|
#define ttisfalse(o) checktag((o), LUA_VFALSE)
|
||||||
|
#define ttistrue(o) checktag((o), LUA_VTRUE)
|
||||||
|
|
||||||
#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
|
|
||||||
|
|
||||||
#define setbvalue(obj,x) \
|
#define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
|
||||||
{ TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
|
|
||||||
|
|
||||||
|
#define setbfvalue(obj) settt_(obj, LUA_VFALSE)
|
||||||
|
#define setbtvalue(obj) settt_(obj, LUA_VTRUE)
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
@@ -215,13 +259,15 @@ typedef StackValue *StkId; /* index to stack elements */
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
|
#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0)
|
||||||
|
|
||||||
|
#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))
|
||||||
|
|
||||||
#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
|
#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
|
||||||
|
|
||||||
#define setthvalue(L,obj,x) \
|
#define setthvalue(L,obj,x) \
|
||||||
{ TValue *io = (obj); lua_State *x_ = (x); \
|
{ TValue *io = (obj); lua_State *x_ = (x); \
|
||||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
|
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
|
||||||
checkliveness(L,io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
|
#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
|
||||||
@@ -274,12 +320,12 @@ typedef struct GCObject {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* Variant tags for numbers */
|
/* Variant tags for numbers */
|
||||||
#define LUA_TNUMFLT (LUA_TNUMBER | (1 << 4)) /* float numbers */
|
#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */
|
||||||
#define LUA_TNUMINT (LUA_TNUMBER | (2 << 4)) /* integer numbers */
|
#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */
|
||||||
|
|
||||||
#define ttisnumber(o) checktype((o), LUA_TNUMBER)
|
#define ttisnumber(o) checktype((o), LUA_TNUMBER)
|
||||||
#define ttisfloat(o) checktag((o), LUA_TNUMFLT)
|
#define ttisfloat(o) checktag((o), LUA_VNUMFLT)
|
||||||
#define ttisinteger(o) checktag((o), LUA_TNUMINT)
|
#define ttisinteger(o) checktag((o), LUA_VNUMINT)
|
||||||
|
|
||||||
#define nvalue(o) check_exp(ttisnumber(o), \
|
#define nvalue(o) check_exp(ttisnumber(o), \
|
||||||
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
|
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
|
||||||
@@ -290,13 +336,13 @@ typedef struct GCObject {
|
|||||||
#define ivalueraw(v) ((v).i)
|
#define ivalueraw(v) ((v).i)
|
||||||
|
|
||||||
#define setfltvalue(obj,x) \
|
#define setfltvalue(obj,x) \
|
||||||
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
|
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
|
||||||
|
|
||||||
#define chgfltvalue(obj,x) \
|
#define chgfltvalue(obj,x) \
|
||||||
{ TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
|
{ TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
|
||||||
|
|
||||||
#define setivalue(obj,x) \
|
#define setivalue(obj,x) \
|
||||||
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
|
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
|
||||||
|
|
||||||
#define chgivalue(obj,x) \
|
#define chgivalue(obj,x) \
|
||||||
{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
|
{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
|
||||||
@@ -311,12 +357,12 @@ typedef struct GCObject {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* Variant tags for strings */
|
/* Variant tags for strings */
|
||||||
#define LUA_TSHRSTR (LUA_TSTRING | (1 << 4)) /* short strings */
|
#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */
|
||||||
#define LUA_TLNGSTR (LUA_TSTRING | (2 << 4)) /* long strings */
|
#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */
|
||||||
|
|
||||||
#define ttisstring(o) checktype((o), LUA_TSTRING)
|
#define ttisstring(o) checktype((o), LUA_TSTRING)
|
||||||
#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
|
#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR))
|
||||||
#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
|
#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR))
|
||||||
|
|
||||||
#define tsvalueraw(v) (gco2ts((v).gc))
|
#define tsvalueraw(v) (gco2ts((v).gc))
|
||||||
|
|
||||||
@@ -335,38 +381,34 @@ typedef struct GCObject {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Header for string value; string bytes follow the end of this structure
|
** Header for a string value.
|
||||||
** (aligned according to 'UTString'; see next).
|
|
||||||
*/
|
*/
|
||||||
typedef struct TString {
|
typedef struct TString {
|
||||||
CommonHeader;
|
CommonHeader;
|
||||||
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
|
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
|
||||||
lu_byte shrlen; /* length for short strings */
|
lu_byte shrlen; /* length for short strings, 0xFF for long strings */
|
||||||
unsigned int hash;
|
unsigned int hash;
|
||||||
union {
|
union {
|
||||||
size_t lnglen; /* length for long strings */
|
size_t lnglen; /* length for long strings */
|
||||||
struct TString *hnext; /* linked list for hash table */
|
struct TString *hnext; /* linked list for hash table */
|
||||||
} u;
|
} u;
|
||||||
|
char contents[1];
|
||||||
} TString;
|
} TString;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Get the actual string (array of bytes) from a 'TString'.
|
** Get the actual string (array of bytes) from a 'TString'. (Generic
|
||||||
** (Access to 'extra' ensures that value is really a 'TString'.)
|
** version and specialized versions for long and short strings.)
|
||||||
*/
|
*/
|
||||||
#define getstr(ts) \
|
#define getstr(ts) ((ts)->contents)
|
||||||
check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString))
|
#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents)
|
||||||
|
#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents)
|
||||||
|
|
||||||
|
|
||||||
/* get the actual string (array of bytes) from a Lua value */
|
|
||||||
#define svalue(o) getstr(tsvalue(o))
|
|
||||||
|
|
||||||
/* get string length from 'TString *s' */
|
/* get string length from 'TString *s' */
|
||||||
#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
|
#define tsslen(s) \
|
||||||
|
((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen)
|
||||||
/* get string length from 'TValue *o' */
|
|
||||||
#define vslen(o) tsslen(tsvalue(o))
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
@@ -377,8 +419,17 @@ typedef struct TString {
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
|
|
||||||
#define ttisfulluserdata(o) checktype((o), LUA_TUSERDATA)
|
/*
|
||||||
|
** Light userdata should be a variant of userdata, but for compatibility
|
||||||
|
** reasons they are also different types.
|
||||||
|
*/
|
||||||
|
#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0)
|
||||||
|
|
||||||
|
#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0)
|
||||||
|
|
||||||
|
#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)
|
||||||
|
#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
|
||||||
|
|
||||||
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
||||||
#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
|
#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
|
||||||
@@ -386,11 +437,11 @@ typedef struct TString {
|
|||||||
#define pvalueraw(v) ((v).p)
|
#define pvalueraw(v) ((v).p)
|
||||||
|
|
||||||
#define setpvalue(obj,x) \
|
#define setpvalue(obj,x) \
|
||||||
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
|
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
|
||||||
|
|
||||||
#define setuvalue(L,obj,x) \
|
#define setuvalue(L,obj,x) \
|
||||||
{ TValue *io = (obj); Udata *x_ = (x); \
|
{ TValue *io = (obj); Udata *x_ = (x); \
|
||||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
|
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
|
||||||
checkliveness(L,io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
|
|
||||||
@@ -453,6 +504,9 @@ typedef struct Udata0 {
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define LUA_VPROTO makevariant(LUA_TPROTO, 0)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Description of an upvalue for function prototypes
|
** Description of an upvalue for function prototypes
|
||||||
*/
|
*/
|
||||||
@@ -460,6 +514,7 @@ typedef struct Upvaldesc {
|
|||||||
TString *name; /* upvalue name (for debug information) */
|
TString *name; /* upvalue name (for debug information) */
|
||||||
lu_byte instack; /* whether it is in stack (register) */
|
lu_byte instack; /* whether it is in stack (register) */
|
||||||
lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
|
lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
|
||||||
|
lu_byte kind; /* kind of corresponding variable */
|
||||||
} Upvaldesc;
|
} Upvaldesc;
|
||||||
|
|
||||||
|
|
||||||
@@ -522,20 +577,24 @@ typedef struct Proto {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Closures
|
** Functions
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0)
|
||||||
|
|
||||||
|
|
||||||
/* Variant tags for functions */
|
/* Variant tags for functions */
|
||||||
#define LUA_TLCL (LUA_TFUNCTION | (1 << 4)) /* Lua closure */
|
#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */
|
||||||
#define LUA_TLCF (LUA_TFUNCTION | (2 << 4)) /* light C function */
|
#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */
|
||||||
#define LUA_TCCL (LUA_TFUNCTION | (3 << 4)) /* C closure */
|
#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */
|
||||||
|
|
||||||
#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
|
#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
|
||||||
#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL)
|
#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))
|
||||||
#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
|
#define ttislcf(o) checktag((o), LUA_VLCF)
|
||||||
#define ttislcf(o) checktag((o), LUA_TLCF)
|
#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))
|
||||||
#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
|
#define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o))
|
||||||
|
|
||||||
|
|
||||||
#define isLfunction(o) ttisLclosure(o)
|
#define isLfunction(o) ttisLclosure(o)
|
||||||
|
|
||||||
@@ -548,17 +607,17 @@ typedef struct Proto {
|
|||||||
|
|
||||||
#define setclLvalue(L,obj,x) \
|
#define setclLvalue(L,obj,x) \
|
||||||
{ TValue *io = (obj); LClosure *x_ = (x); \
|
{ TValue *io = (obj); LClosure *x_ = (x); \
|
||||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
|
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
|
||||||
checkliveness(L,io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
|
#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
|
||||||
|
|
||||||
#define setfvalue(obj,x) \
|
#define setfvalue(obj,x) \
|
||||||
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
|
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
|
||||||
|
|
||||||
#define setclCvalue(L,obj,x) \
|
#define setclCvalue(L,obj,x) \
|
||||||
{ TValue *io = (obj); CClosure *x_ = (x); \
|
{ TValue *io = (obj); CClosure *x_ = (x); \
|
||||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
|
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
|
||||||
checkliveness(L,io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
|
|
||||||
@@ -567,7 +626,10 @@ typedef struct Proto {
|
|||||||
*/
|
*/
|
||||||
typedef struct UpVal {
|
typedef struct UpVal {
|
||||||
CommonHeader;
|
CommonHeader;
|
||||||
TValue *v; /* points to stack or to its own value */
|
union {
|
||||||
|
TValue *p; /* points to stack or to its own value */
|
||||||
|
ptrdiff_t offset; /* used while the stack is being reallocated */
|
||||||
|
} v;
|
||||||
union {
|
union {
|
||||||
struct { /* (when open) */
|
struct { /* (when open) */
|
||||||
struct UpVal *next; /* linked list */
|
struct UpVal *next; /* linked list */
|
||||||
@@ -578,9 +640,6 @@ typedef struct UpVal {
|
|||||||
} UpVal;
|
} UpVal;
|
||||||
|
|
||||||
|
|
||||||
/* variant for "To Be Closed" upvalues */
|
|
||||||
#define LUA_TUPVALTBC (LUA_TUPVAL | (1 << 4))
|
|
||||||
|
|
||||||
|
|
||||||
#define ClosureHeader \
|
#define ClosureHeader \
|
||||||
CommonHeader; lu_byte nupvalues; GCObject *gclist
|
CommonHeader; lu_byte nupvalues; GCObject *gclist
|
||||||
@@ -616,13 +675,15 @@ typedef union Closure {
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define ttistable(o) checktag((o), ctb(LUA_TTABLE))
|
#define LUA_VTABLE makevariant(LUA_TTABLE, 0)
|
||||||
|
|
||||||
|
#define ttistable(o) checktag((o), ctb(LUA_VTABLE))
|
||||||
|
|
||||||
#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
|
#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
|
||||||
|
|
||||||
#define sethvalue(L,obj,x) \
|
#define sethvalue(L,obj,x) \
|
||||||
{ TValue *io = (obj); Table *x_ = (x); \
|
{ TValue *io = (obj); Table *x_ = (x); \
|
||||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
|
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
|
||||||
checkliveness(L,io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
|
#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
|
||||||
@@ -650,14 +711,14 @@ typedef union Node {
|
|||||||
#define setnodekey(L,node,obj) \
|
#define setnodekey(L,node,obj) \
|
||||||
{ Node *n_=(node); const TValue *io_=(obj); \
|
{ Node *n_=(node); const TValue *io_=(obj); \
|
||||||
n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
|
n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
|
||||||
(void)L; checkliveness(L,io_); }
|
checkliveness(L,io_); }
|
||||||
|
|
||||||
|
|
||||||
/* copy a value from a key */
|
/* copy a value from a key */
|
||||||
#define getnodekey(L,obj,node) \
|
#define getnodekey(L,obj,node) \
|
||||||
{ TValue *io_=(obj); const Node *n_=(node); \
|
{ TValue *io_=(obj); const Node *n_=(node); \
|
||||||
io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
|
io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
|
||||||
(void)L; checkliveness(L,io_); }
|
checkliveness(L,io_); }
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -668,9 +729,9 @@ typedef union Node {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#define BITRAS (1 << 7)
|
#define BITRAS (1 << 7)
|
||||||
#define isrealasize(t) (!((t)->marked & BITRAS))
|
#define isrealasize(t) (!((t)->flags & BITRAS))
|
||||||
#define setrealasize(t) ((t)->marked &= cast_byte(~BITRAS))
|
#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS))
|
||||||
#define setnorealasize(t) ((t)->marked |= BITRAS)
|
#define setnorealasize(t) ((t)->flags |= BITRAS)
|
||||||
|
|
||||||
|
|
||||||
typedef struct Table {
|
typedef struct Table {
|
||||||
@@ -693,9 +754,9 @@ typedef struct Table {
|
|||||||
#define keyval(node) ((node)->u.key_val)
|
#define keyval(node) ((node)->u.key_val)
|
||||||
|
|
||||||
#define keyisnil(node) (keytt(node) == LUA_TNIL)
|
#define keyisnil(node) (keytt(node) == LUA_TNIL)
|
||||||
#define keyisinteger(node) (keytt(node) == LUA_TNUMINT)
|
#define keyisinteger(node) (keytt(node) == LUA_VNUMINT)
|
||||||
#define keyival(node) (keyval(node).i)
|
#define keyival(node) (keyval(node).i)
|
||||||
#define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR))
|
#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))
|
||||||
#define keystrval(node) (gco2ts(keyval(node).gc))
|
#define keystrval(node) (gco2ts(keyval(node).gc))
|
||||||
|
|
||||||
#define setnilkey(node) (keytt(node) = LUA_TNIL)
|
#define setnilkey(node) (keytt(node) = LUA_TNIL)
|
||||||
@@ -707,13 +768,13 @@ typedef struct Table {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Use a "nil table" to mark dead keys in a table. Those keys serve
|
** Dead keys in tables have the tag DEADKEY but keep their original
|
||||||
** to keep space for removed entries, which may still be part of
|
** gcvalue. This distinguishes them from regular keys but allows them to
|
||||||
** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE
|
** be found when searched in a special way. ('next' needs that to find
|
||||||
** set, so these values are considered not collectable and are different
|
** keys removed from a table during a traversal.)
|
||||||
** from any valid value.
|
|
||||||
*/
|
*/
|
||||||
#define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL)
|
#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY)
|
||||||
|
#define keyisdead(node) (keytt(node) == LUA_TDEADKEY)
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
@@ -733,8 +794,6 @@ typedef struct Table {
|
|||||||
/* size of buffer for 'luaO_utf8esc' function */
|
/* size of buffer for 'luaO_utf8esc' function */
|
||||||
#define UTF8BUFFSZ 8
|
#define UTF8BUFFSZ 8
|
||||||
|
|
||||||
LUAI_FUNC int luaO_int2fb (unsigned int x);
|
|
||||||
LUAI_FUNC int luaO_fb2int (int x);
|
|
||||||
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
|
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
|
||||||
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
|
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
|
||||||
LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
|
LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
|
||||||
|
|||||||
+84
-87
@@ -10,98 +10,95 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#include "lopcodes.h"
|
#include "lopcodes.h"
|
||||||
|
|
||||||
|
|
||||||
/* ORDER OP */
|
/* ORDER OP */
|
||||||
|
|
||||||
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||||
/* OT IT T A mode opcode */
|
/* MM OT IT T A mode opcode */
|
||||||
opmode(0, 0, 0, 1, iABC) /* OP_MOVE */
|
opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */
|
||||||
,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADI */
|
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */
|
||||||
,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADF */
|
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */
|
||||||
,opmode(0, 0, 0, 1, iABx) /* OP_LOADK */
|
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */
|
||||||
,opmode(0, 0, 0, 1, iABx) /* OP_LOADKX */
|
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_LOADBOOL */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_LOADNIL */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETUPVAL */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETUPVAL */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETTABUP */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETTABLE */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETFIELD */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETTABUP */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETTABLE */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETI */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETFIELD */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_NEWTABLE */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SELF */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_ADDI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SUBI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_MULI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_MODI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_POWI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_ADDK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SUBK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_MULK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_MODK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_POWK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_DIVK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_IDIVK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_BORK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHRI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHLI */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_ADD */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SUB */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_MUL */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_MOD */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_POW */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_DIV */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_IDIV */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_BAND */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_BOR */
|
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */
|
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHL */
|
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHR */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_UNM */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_NOT */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_LEN */
|
,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_TBC */
|
,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */
|
||||||
,opmode(0, 0, 0, 0, isJ) /* OP_JMP */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_EQ */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_LT */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_LE */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_EQK */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_EQI */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_LTI */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_LEI */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_GTI */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_GEI */
|
,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */
|
||||||
,opmode(0, 0, 1, 0, iABC) /* OP_TEST */
|
,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */
|
||||||
,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */
|
,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */
|
||||||
,opmode(1, 1, 0, 1, iABC) /* OP_CALL */
|
,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */
|
||||||
,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */
|
,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */
|
||||||
,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */
|
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */
|
||||||
,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */
|
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */
|
||||||
,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */
|
,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */
|
||||||
,opmode(0, 0, 0, 0, iABx) /* OP_TFORPREP */
|
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */
|
||||||
,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */
|
,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */
|
||||||
,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */
|
,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */
|
||||||
,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */
|
,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
|
||||||
,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */
|
,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
|
||||||
,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */
|
,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
|
||||||
,opmode(0, 0, 0, 1, iABC) /* OP_VARARGPREP */
|
,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
|
||||||
,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+133
-106
@@ -17,11 +17,11 @@
|
|||||||
|
|
||||||
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
|
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
|
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) |
|
iABC C(8) | B(8) |k| A(8) | Op(7) |
|
||||||
iABx Bx(17) | A(8) | Op(7) |
|
iABx Bx(17) | A(8) | Op(7) |
|
||||||
iAsB sBx (signed)(17) | A(8) | Op(7) |
|
iAsBx sBx (signed)(17) | A(8) | Op(7) |
|
||||||
iAx Ax(25) | Op(7) |
|
iAx Ax(25) | Op(7) |
|
||||||
isJ sJ(25) | Op(7) |
|
isJ sJ (signed)(25) | Op(7) |
|
||||||
|
|
||||||
A signed argument is represented in excess K: the represented value is
|
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
|
the written unsigned value minus K, where K is half the maximum for the
|
||||||
@@ -97,6 +97,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
|||||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||||
#define OFFSET_sC (MAXARG_C >> 1)
|
#define OFFSET_sC (MAXARG_C >> 1)
|
||||||
|
|
||||||
|
#define int2sC(i) ((i) + OFFSET_sC)
|
||||||
|
#define sC2int(i) ((i) - OFFSET_sC)
|
||||||
|
|
||||||
|
|
||||||
/* creates a mask with 'n' 1 bits at position 'p' */
|
/* creates a mask with 'n' 1 bits at position 'p' */
|
||||||
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
|
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
|
||||||
@@ -123,14 +126,14 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
|||||||
#define SETARG_A(i,v) setarg(i, v, 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_sB(i) (GETARG_B(i) - OFFSET_sC)
|
#define GETARG_sB(i) sC2int(GETARG_B(i))
|
||||||
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
|
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
|
||||||
|
|
||||||
#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_sC(i) (GETARG_C(i) - OFFSET_sC)
|
#define GETARG_sC(i) sC2int(GETARG_C(i))
|
||||||
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
|
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
|
||||||
|
|
||||||
#define TESTARG_k(i) (cast_int(((i) & (1u << POS_k))))
|
#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 GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
|
||||||
#define SETARG_k(i,v) setarg(i, v, POS_k, 1)
|
#define SETARG_k(i,v) setarg(i, v, POS_k, 1)
|
||||||
|
|
||||||
@@ -180,127 +183,128 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** R(x) - register
|
** R[x] - register
|
||||||
** K(x) - constant (in constant table)
|
** K[x] - constant (in constant table)
|
||||||
** RK(x) == if k(i) then K(x) else R(x)
|
** RK(x) == if k(i) then K[x] else R[x]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** grep "ORDER OP" if you change these enums
|
** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
|
||||||
|
** has extra descriptions in the notes after the enumeration.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
/*----------------------------------------------------------------------
|
/*----------------------------------------------------------------------
|
||||||
name args description
|
name args description
|
||||||
------------------------------------------------------------------------*/
|
------------------------------------------------------------------------*/
|
||||||
OP_MOVE,/* A B R(A) := R(B) */
|
OP_MOVE,/* A B R[A] := R[B] */
|
||||||
OP_LOADI,/* A sBx R(A) := sBx */
|
OP_LOADI,/* A sBx R[A] := sBx */
|
||||||
OP_LOADF,/* A sBx R(A) := (lua_Number)sBx */
|
OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
|
||||||
OP_LOADK,/* A Bx R(A) := K(Bx) */
|
OP_LOADK,/* A Bx R[A] := K[Bx] */
|
||||||
OP_LOADKX,/* A R(A) := K(extra arg) */
|
OP_LOADKX,/* A R[A] := K[extra arg] */
|
||||||
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
|
OP_LOADFALSE,/* A R[A] := false */
|
||||||
OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
|
OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */
|
||||||
OP_GETUPVAL,/* A B R(A) := UpValue[B] */
|
OP_LOADTRUE,/* A R[A] := true */
|
||||||
OP_SETUPVAL,/* A B UpValue[B] := R(A) */
|
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_GETTABLE,/* A B C R[A] := R[B][R[C]] */
|
||||||
OP_GETI,/* A B C R(A) := R(B)[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_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
|
||||||
OP_SETI,/* A B C R(A)[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 R(A) := {} (size = B,C) */
|
OP_NEWTABLE,/* A B C 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][RK(C):string] */
|
||||||
|
|
||||||
OP_ADDI,/* A B sC R(A) := R(B) + C */
|
OP_ADDI,/* A B sC R[A] := R[B] + sC */
|
||||||
OP_SUBI,/* A B sC R(A) := R(B) - C */
|
|
||||||
OP_MULI,/* A B sC R(A) := R(B) * C */
|
|
||||||
OP_MODI,/* A B sC R(A) := R(B) % C */
|
|
||||||
OP_POWI,/* A B sC R(A) := R(B) ^ C */
|
|
||||||
OP_DIVI,/* A B sC R(A) := R(B) / C */
|
|
||||||
OP_IDIVI,/* A B sC R(A) := R(B) // C */
|
|
||||||
|
|
||||||
OP_ADDK,/* A B C R(A) := R(B) + K(C) */
|
OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */
|
||||||
OP_SUBK,/* A B C R(A) := R(B) - K(C) */
|
OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */
|
||||||
OP_MULK,/* A B C R(A) := R(B) * K(C) */
|
OP_MULK,/* A B C R[A] := R[B] * K[C]:number */
|
||||||
OP_MODK,/* A B C R(A) := R(B) % K(C) */
|
OP_MODK,/* A B C R[A] := R[B] % K[C]:number */
|
||||||
OP_POWK,/* A B C R(A) := R(B) ^ K(C) */
|
OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */
|
||||||
OP_DIVK,/* A B C R(A) := R(B) / K(C) */
|
OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */
|
||||||
OP_IDIVK,/* A B C R(A) := R(B) // K(C) */
|
OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */
|
||||||
|
|
||||||
OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */
|
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_BORK,/* A B C R[A] := R[B] | K[C]:integer */
|
||||||
OP_BXORK,/* 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) >> C */
|
OP_SHRI,/* A B sC R[A] := R[B] >> sC */
|
||||||
OP_SHLI,/* A B sC R(A) := C << R(B) */
|
OP_SHLI,/* A B sC R[A] := sC << R[B] */
|
||||||
|
|
||||||
OP_ADD,/* A B C R(A) := R(B) + R(C) */
|
OP_ADD,/* A B C R[A] := R[B] + R[C] */
|
||||||
OP_SUB,/* A B C R(A) := R(B) - R(C) */
|
OP_SUB,/* A B C R[A] := R[B] - R[C] */
|
||||||
OP_MUL,/* A B C R(A) := R(B) * R(C) */
|
OP_MUL,/* A B C R[A] := R[B] * R[C] */
|
||||||
OP_MOD,/* A B C R(A) := R(B) % R(C) */
|
OP_MOD,/* A B C R[A] := R[B] % R[C] */
|
||||||
OP_POW,/* A B C R(A) := R(B) ^ R(C) */
|
OP_POW,/* A B C R[A] := R[B] ^ R[C] */
|
||||||
OP_DIV,/* A B C R(A) := R(B) / R(C) */
|
OP_DIV,/* A B C R[A] := R[B] / R[C] */
|
||||||
OP_IDIV,/* A B C R(A) := R(B) // R(C) */
|
OP_IDIV,/* A B C R[A] := R[B] // R[C] */
|
||||||
|
|
||||||
OP_BAND,/* A B C R(A) := R(B) & R(C) */
|
OP_BAND,/* A B C R[A] := R[B] & R[C] */
|
||||||
OP_BOR,/* A B C R(A) := R(B) | R(C) */
|
OP_BOR,/* A B C R[A] := R[B] | R[C] */
|
||||||
OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */
|
OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
|
||||||
OP_SHL,/* 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_SHR,/* A B C R[A] := R[B] >> R[C] */
|
||||||
|
|
||||||
OP_UNM,/* A B R(A) := -R(B) */
|
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */
|
||||||
OP_BNOT,/* A B R(A) := ~R(B) */
|
OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
|
||||||
OP_NOT,/* A B R(A) := not R(B) */
|
OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
|
||||||
OP_LEN,/* A B R(A) := length of R(B) */
|
|
||||||
|
|
||||||
OP_CONCAT,/* A B R(A) := R(A).. ... ..R(A + B - 1) */
|
OP_UNM,/* A B R[A] := -R[B] */
|
||||||
|
OP_BNOT,/* A B R[A] := ~R[B] */
|
||||||
|
OP_NOT,/* A B R[A] := not R[B] */
|
||||||
|
OP_LEN,/* A B R[A] := #R[B] (length operator) */
|
||||||
|
|
||||||
OP_CLOSE,/* A close all upvalues >= R(A) */
|
OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
|
||||||
|
|
||||||
|
OP_CLOSE,/* A close all upvalues >= R[A] */
|
||||||
OP_TBC,/* A mark variable A "to be closed" */
|
OP_TBC,/* A mark variable A "to be closed" */
|
||||||
OP_JMP,/* k sJ pc += sJ (k is used in code generation) */
|
OP_JMP,/* sJ pc += sJ */
|
||||||
OP_EQ,/* A B if ((R(A) == R(B)) ~= k) then pc++ */
|
OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */
|
||||||
OP_LT,/* A B if ((R(A) < R(B)) ~= k) then pc++ */
|
OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */
|
||||||
OP_LE,/* A B if ((R(A) <= R(B)) ~= k) then pc++ */
|
OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
|
||||||
|
|
||||||
OP_EQK,/* A B if ((R(A) == K(B)) ~= k) then pc++ */
|
OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */
|
||||||
OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */
|
OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */
|
||||||
OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */
|
OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */
|
||||||
OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */
|
OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */
|
||||||
OP_GTI,/* A sB if ((R(A) > sB) ~= k) then pc++ */
|
OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
|
||||||
OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */
|
OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
|
||||||
|
|
||||||
OP_TEST,/* A if (not R(A) == k) then pc++ */
|
OP_TEST,/* A k if (not R[A] == k) then pc++ */
|
||||||
OP_TESTSET,/* A B 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_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
|
||||||
OP_TAILCALL,/* A B C return 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 return R(A), ... ,R(A+B-2) (see note) */
|
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */
|
||||||
OP_RETURN0,/* return */
|
OP_RETURN0,/* return */
|
||||||
OP_RETURN1,/* A return R(A) */
|
OP_RETURN1,/* A return R[A] */
|
||||||
|
|
||||||
OP_FORLOOP,/* A Bx R(A)+=R(A+2);
|
OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */
|
||||||
if R(A) <?= R(A+1) then { pc-=Bx; R(A+3)=R(A) } */
|
OP_FORPREP,/* A Bx <check values and prepare counters>;
|
||||||
OP_FORPREP,/* A Bx R(A)-=R(A+2); pc+=Bx */
|
if not to run then pc+=Bx+1; */
|
||||||
|
|
||||||
OP_TFORPREP,/* A Bx create upvalue for R(A + 3); pc+=Bx */
|
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_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_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
|
||||||
|
|
||||||
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
|
OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */
|
||||||
|
|
||||||
OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
|
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 C R[A], R[A+1], ..., R[A+C-2] = vararg */
|
||||||
|
|
||||||
OP_VARARGPREP,/*A (adjust vararg parameters) */
|
OP_VARARGPREP,/*A (adjust vararg parameters) */
|
||||||
|
|
||||||
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||||
} OpCode;
|
} OpCode;
|
||||||
@@ -312,6 +316,18 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
|||||||
|
|
||||||
/*===========================================================================
|
/*===========================================================================
|
||||||
Notes:
|
Notes:
|
||||||
|
|
||||||
|
(*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
|
||||||
|
value, in a code equivalent to (not cond ? false : true). (It
|
||||||
|
produces false and skips the next instruction producing true.)
|
||||||
|
|
||||||
|
(*) Opcodes OP_MMBIN and variants follow each arithmetic and
|
||||||
|
bitwise opcode. If the operation succeeds, it skips this next
|
||||||
|
opcode. Otherwise, this opcode calls the corresponding metamethod.
|
||||||
|
|
||||||
|
(*) Opcode OP_TESTSET is used in short-circuit expressions that need
|
||||||
|
both to jump and to produce a value, such as (a = b or c).
|
||||||
|
|
||||||
(*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
|
(*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
|
||||||
'top' is set to last_result+1, so next open instruction (OP_CALL,
|
'top' is set to last_result+1, so next open instruction (OP_CALL,
|
||||||
OP_RETURN*, OP_SETLIST) may use 'top'.
|
OP_RETURN*, OP_SETLIST) may use 'top'.
|
||||||
@@ -321,24 +337,33 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
|||||||
|
|
||||||
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
||||||
|
|
||||||
(*) In OP_SETLIST, if (B == 0) then real B = 'top'; if (C == 0) then
|
(*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
|
||||||
next 'instruction' is EXTRAARG(real C).
|
OP_EXTRAARG.
|
||||||
|
|
||||||
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
(*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
|
||||||
|
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
|
||||||
|
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.
|
||||||
|
|
||||||
(*) For comparisons, k specifies what condition the test should accept
|
(*) For comparisons, k specifies what condition the test should accept
|
||||||
(true or false).
|
(true or false).
|
||||||
|
|
||||||
|
(*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
|
||||||
|
(the constant is the first operand).
|
||||||
|
|
||||||
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
||||||
|
|
||||||
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
|
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
|
||||||
function either builds upvalues, which may need to be closed, or is
|
function builds upvalues, which may need to be closed. C > 0 means
|
||||||
vararg, which must be corrected before returning. When 'k' is true,
|
the function is vararg, so that its 'func' must be corrected before
|
||||||
C > 0 means the function is vararg and (C - 1) is its number of
|
returning; in this case, (C - 1) is its number of fixed parameters.
|
||||||
fixed parameters.
|
|
||||||
|
|
||||||
(*) In comparisons with an immediate operand, C signals whether the
|
(*) In comparisons with an immediate operand, C signals whether the
|
||||||
original operand was a float.
|
original operand was a float. (It must be corrected in case of
|
||||||
|
metamethods.)
|
||||||
|
|
||||||
===========================================================================*/
|
===========================================================================*/
|
||||||
|
|
||||||
@@ -350,6 +375,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
|||||||
** bit 4: operator is a test (next instruction must be a jump)
|
** bit 4: operator is a test (next instruction must be a jump)
|
||||||
** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
|
** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
|
||||||
** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
|
** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
|
||||||
|
** bit 7: instruction is an MM instruction (call a metamethod)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
||||||
@@ -359,6 +385,7 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
|||||||
#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
|
#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
|
||||||
#define testITMode(m) (luaP_opmodes[m] & (1 << 5))
|
#define testITMode(m) (luaP_opmodes[m] & (1 << 5))
|
||||||
#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
|
#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
|
||||||
|
#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
|
||||||
|
|
||||||
/* "out top" (set top for next instruction) */
|
/* "out top" (set top for next instruction) */
|
||||||
#define isOT(i) \
|
#define isOT(i) \
|
||||||
@@ -368,11 +395,11 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
|||||||
/* "in top" (uses top from previous instruction) */
|
/* "in top" (uses top from previous instruction) */
|
||||||
#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
|
#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
|
||||||
|
|
||||||
#define opmode(ot,it,t,a,m) (((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m))
|
#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 */
|
/* number of list items to accumulate before a SETLIST instruction */
|
||||||
#define LFIELDS_PER_FLUSH 50
|
#define LFIELDS_PER_FLUSH 50
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+9
-7
@@ -7,6 +7,9 @@
|
|||||||
#if !defined(lopnames_h)
|
#if !defined(lopnames_h)
|
||||||
#define lopnames_h
|
#define lopnames_h
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|
||||||
/* ORDER OP */
|
/* ORDER OP */
|
||||||
|
|
||||||
static const char *const opnames[] = {
|
static const char *const opnames[] = {
|
||||||
@@ -15,7 +18,9 @@ static const char *const opnames[] = {
|
|||||||
"LOADF",
|
"LOADF",
|
||||||
"LOADK",
|
"LOADK",
|
||||||
"LOADKX",
|
"LOADKX",
|
||||||
"LOADBOOL",
|
"LOADFALSE",
|
||||||
|
"LFALSESKIP",
|
||||||
|
"LOADTRUE",
|
||||||
"LOADNIL",
|
"LOADNIL",
|
||||||
"GETUPVAL",
|
"GETUPVAL",
|
||||||
"SETUPVAL",
|
"SETUPVAL",
|
||||||
@@ -30,12 +35,6 @@ static const char *const opnames[] = {
|
|||||||
"NEWTABLE",
|
"NEWTABLE",
|
||||||
"SELF",
|
"SELF",
|
||||||
"ADDI",
|
"ADDI",
|
||||||
"SUBI",
|
|
||||||
"MULI",
|
|
||||||
"MODI",
|
|
||||||
"POWI",
|
|
||||||
"DIVI",
|
|
||||||
"IDIVI",
|
|
||||||
"ADDK",
|
"ADDK",
|
||||||
"SUBK",
|
"SUBK",
|
||||||
"MULK",
|
"MULK",
|
||||||
@@ -60,6 +59,9 @@ static const char *const opnames[] = {
|
|||||||
"BXOR",
|
"BXOR",
|
||||||
"SHL",
|
"SHL",
|
||||||
"SHR",
|
"SHR",
|
||||||
|
"MMBIN",
|
||||||
|
"MMBINI",
|
||||||
|
"MMBINK",
|
||||||
"UNM",
|
"UNM",
|
||||||
"BNOT",
|
"BNOT",
|
||||||
"NOT",
|
"NOT",
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -29,23 +30,14 @@
|
|||||||
*/
|
*/
|
||||||
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
||||||
|
|
||||||
/* options for ANSI C 89 (only 1-char options) */
|
|
||||||
#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%"
|
|
||||||
|
|
||||||
/* options for ISO C 99 and POSIX */
|
|
||||||
#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
|
|
||||||
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
|
|
||||||
|
|
||||||
/* options for Windows */
|
|
||||||
#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \
|
|
||||||
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
|
|
||||||
|
|
||||||
#if defined(LUA_USE_WINDOWS)
|
#if defined(LUA_USE_WINDOWS)
|
||||||
#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN
|
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \
|
||||||
#elif defined(LUA_USE_C89)
|
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
|
||||||
#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89
|
#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */
|
||||||
|
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%"
|
||||||
#else /* C99 specification */
|
#else /* C99 specification */
|
||||||
#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99
|
#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
|
||||||
|
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
@@ -58,18 +50,20 @@
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !defined(l_time_t) /* { */
|
|
||||||
/*
|
/*
|
||||||
** type to represent time_t in Lua
|
** type to represent time_t in Lua
|
||||||
*/
|
*/
|
||||||
|
#if !defined(LUA_NUMTIME) /* { */
|
||||||
|
|
||||||
#define l_timet lua_Integer
|
#define l_timet lua_Integer
|
||||||
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
|
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
|
||||||
|
#define l_gettime(L,arg) luaL_checkinteger(L, arg)
|
||||||
|
|
||||||
static time_t l_checktime (lua_State *L, int arg) {
|
#else /* }{ */
|
||||||
lua_Integer t = luaL_checkinteger(L, arg);
|
|
||||||
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
#define l_timet lua_Number
|
||||||
return (time_t)t;
|
#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t))
|
||||||
}
|
#define l_gettime(L,arg) luaL_checknumber(L, arg)
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
@@ -89,7 +83,7 @@ static time_t l_checktime (lua_State *L, int arg) {
|
|||||||
|
|
||||||
/* ISO C definitions */
|
/* ISO C definitions */
|
||||||
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
|
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
|
||||||
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
@@ -135,11 +129,21 @@ static time_t l_checktime (lua_State *L, int arg) {
|
|||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(l_system)
|
||||||
|
#if defined(LUA_USE_IOS)
|
||||||
|
/* Despite claiming to be ISO C, iOS does not implement 'system'. */
|
||||||
|
#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
|
||||||
|
#else
|
||||||
|
#define l_system(cmd) system(cmd) /* default definition */
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int os_execute (lua_State *L) {
|
static int os_execute (lua_State *L) {
|
||||||
const char *cmd = luaL_optstring(L, 1, NULL);
|
const char *cmd = luaL_optstring(L, 1, NULL);
|
||||||
int stat = system(cmd);
|
int stat;
|
||||||
|
errno = 0;
|
||||||
|
stat = l_system(cmd);
|
||||||
if (cmd != NULL)
|
if (cmd != NULL)
|
||||||
return luaL_execresult(L, stat);
|
return luaL_execresult(L, stat);
|
||||||
else {
|
else {
|
||||||
@@ -151,6 +155,7 @@ static int os_execute (lua_State *L) {
|
|||||||
|
|
||||||
static int os_remove (lua_State *L) {
|
static int os_remove (lua_State *L) {
|
||||||
const char *filename = luaL_checkstring(L, 1);
|
const char *filename = luaL_checkstring(L, 1);
|
||||||
|
errno = 0;
|
||||||
return luaL_fileresult(L, remove(filename) == 0, filename);
|
return luaL_fileresult(L, remove(filename) == 0, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,6 +163,7 @@ static int os_remove (lua_State *L) {
|
|||||||
static int os_rename (lua_State *L) {
|
static int os_rename (lua_State *L) {
|
||||||
const char *fromname = luaL_checkstring(L, 1);
|
const char *fromname = luaL_checkstring(L, 1);
|
||||||
const char *toname = luaL_checkstring(L, 2);
|
const char *toname = luaL_checkstring(L, 2);
|
||||||
|
errno = 0;
|
||||||
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,7 +172,7 @@ static int os_tmpname (lua_State *L) {
|
|||||||
char buff[LUA_TMPNAMBUFSIZE];
|
char buff[LUA_TMPNAMBUFSIZE];
|
||||||
int err;
|
int err;
|
||||||
lua_tmpnam(buff, err);
|
lua_tmpnam(buff, err);
|
||||||
if (err)
|
if (l_unlikely(err))
|
||||||
return luaL_error(L, "unable to generate a unique filename");
|
return luaL_error(L, "unable to generate a unique filename");
|
||||||
lua_pushstring(L, buff);
|
lua_pushstring(L, buff);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -193,11 +199,25 @@ static int os_clock (lua_State *L) {
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void setfield (lua_State *L, const char *key, int value) {
|
/*
|
||||||
lua_pushinteger(L, value);
|
** About the overflow check: an overflow cannot occur when time
|
||||||
|
** is represented by a lua_Integer, because either lua_Integer is
|
||||||
|
** large enough to represent all int fields or it is not large enough
|
||||||
|
** to represent a time that cause a field to overflow. However, if
|
||||||
|
** times are represented as doubles and lua_Integer is int, then the
|
||||||
|
** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900
|
||||||
|
** to compute the year.
|
||||||
|
*/
|
||||||
|
static void setfield (lua_State *L, const char *key, int value, int delta) {
|
||||||
|
#if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
|
||||||
|
if (l_unlikely(value > LUA_MAXINTEGER - delta))
|
||||||
|
luaL_error(L, "field '%s' is out-of-bound", key);
|
||||||
|
#endif
|
||||||
|
lua_pushinteger(L, (lua_Integer)value + delta);
|
||||||
lua_setfield(L, -2, key);
|
lua_setfield(L, -2, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void setboolfield (lua_State *L, const char *key, int value) {
|
static void setboolfield (lua_State *L, const char *key, int value) {
|
||||||
if (value < 0) /* undefined? */
|
if (value < 0) /* undefined? */
|
||||||
return; /* does not set field */
|
return; /* does not set field */
|
||||||
@@ -210,14 +230,14 @@ static void setboolfield (lua_State *L, const char *key, int value) {
|
|||||||
** Set all fields from structure 'tm' in the table on top of the stack
|
** Set all fields from structure 'tm' in the table on top of the stack
|
||||||
*/
|
*/
|
||||||
static void setallfields (lua_State *L, struct tm *stm) {
|
static void setallfields (lua_State *L, struct tm *stm) {
|
||||||
setfield(L, "sec", stm->tm_sec);
|
setfield(L, "year", stm->tm_year, 1900);
|
||||||
setfield(L, "min", stm->tm_min);
|
setfield(L, "month", stm->tm_mon, 1);
|
||||||
setfield(L, "hour", stm->tm_hour);
|
setfield(L, "day", stm->tm_mday, 0);
|
||||||
setfield(L, "day", stm->tm_mday);
|
setfield(L, "hour", stm->tm_hour, 0);
|
||||||
setfield(L, "month", stm->tm_mon + 1);
|
setfield(L, "min", stm->tm_min, 0);
|
||||||
setfield(L, "year", stm->tm_year + 1900);
|
setfield(L, "sec", stm->tm_sec, 0);
|
||||||
setfield(L, "wday", stm->tm_wday + 1);
|
setfield(L, "yday", stm->tm_yday, 1);
|
||||||
setfield(L, "yday", stm->tm_yday + 1);
|
setfield(L, "wday", stm->tm_wday, 1);
|
||||||
setboolfield(L, "isdst", stm->tm_isdst);
|
setboolfield(L, "isdst", stm->tm_isdst);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,24 +250,19 @@ static int getboolfield (lua_State *L, const char *key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* maximum value for date fields (to avoid arithmetic overflows with 'int') */
|
|
||||||
#if !defined(L_MAXDATEFIELD)
|
|
||||||
#define L_MAXDATEFIELD (INT_MAX / 2)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int getfield (lua_State *L, const char *key, int d, int delta) {
|
static int getfield (lua_State *L, const char *key, int d, int delta) {
|
||||||
int isnum;
|
int isnum;
|
||||||
int t = lua_getfield(L, -1, key); /* get field and its type */
|
int t = lua_getfield(L, -1, key); /* get field and its type */
|
||||||
lua_Integer res = lua_tointegerx(L, -1, &isnum);
|
lua_Integer res = lua_tointegerx(L, -1, &isnum);
|
||||||
if (!isnum) { /* field is not an integer? */
|
if (!isnum) { /* field is not an integer? */
|
||||||
if (t != LUA_TNIL) /* some other value? */
|
if (l_unlikely(t != LUA_TNIL)) /* some other value? */
|
||||||
return luaL_error(L, "field '%s' is not an integer", key);
|
return luaL_error(L, "field '%s' is not an integer", key);
|
||||||
else if (d < 0) /* absent field; no default? */
|
else if (l_unlikely(d < 0)) /* absent field; no default? */
|
||||||
return luaL_error(L, "field '%s' missing in date table", key);
|
return luaL_error(L, "field '%s' missing in date table", key);
|
||||||
res = d;
|
res = d;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
|
if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
|
||||||
return luaL_error(L, "field '%s' is out-of-bound", key);
|
return luaL_error(L, "field '%s' is out-of-bound", key);
|
||||||
res -= delta;
|
res -= delta;
|
||||||
}
|
}
|
||||||
@@ -275,6 +290,13 @@ static const char *checkoption (lua_State *L, const char *conv,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static time_t l_checktime (lua_State *L, int arg) {
|
||||||
|
l_timet t = l_gettime(L, arg);
|
||||||
|
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
||||||
|
return (time_t)t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* maximum size for an individual 'strftime' item */
|
/* maximum size for an individual 'strftime' item */
|
||||||
#define SIZETIMEFMT 250
|
#define SIZETIMEFMT 250
|
||||||
|
|
||||||
@@ -293,7 +315,7 @@ static int os_date (lua_State *L) {
|
|||||||
stm = l_localtime(&t, &tmr);
|
stm = l_localtime(&t, &tmr);
|
||||||
if (stm == NULL) /* invalid date? */
|
if (stm == NULL) /* invalid date? */
|
||||||
return luaL_error(L,
|
return luaL_error(L,
|
||||||
"time result cannot be represented in this installation");
|
"date result cannot be represented in this installation");
|
||||||
if (strcmp(s, "*t") == 0) {
|
if (strcmp(s, "*t") == 0) {
|
||||||
lua_createtable(L, 0, 9); /* 9 = number of fields */
|
lua_createtable(L, 0, 9); /* 9 = number of fields */
|
||||||
setallfields(L, stm);
|
setallfields(L, stm);
|
||||||
@@ -329,12 +351,12 @@ static int os_time (lua_State *L) {
|
|||||||
struct tm ts;
|
struct tm ts;
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
lua_settop(L, 1); /* make sure table is at the top */
|
lua_settop(L, 1); /* make sure table is at the top */
|
||||||
ts.tm_sec = getfield(L, "sec", 0, 0);
|
|
||||||
ts.tm_min = getfield(L, "min", 0, 0);
|
|
||||||
ts.tm_hour = getfield(L, "hour", 12, 0);
|
|
||||||
ts.tm_mday = getfield(L, "day", -1, 0);
|
|
||||||
ts.tm_mon = getfield(L, "month", -1, 1);
|
|
||||||
ts.tm_year = getfield(L, "year", -1, 1900);
|
ts.tm_year = getfield(L, "year", -1, 1900);
|
||||||
|
ts.tm_mon = getfield(L, "month", -1, 1);
|
||||||
|
ts.tm_mday = getfield(L, "day", -1, 0);
|
||||||
|
ts.tm_hour = getfield(L, "hour", 12, 0);
|
||||||
|
ts.tm_min = getfield(L, "min", 0, 0);
|
||||||
|
ts.tm_sec = getfield(L, "sec", 0, 0);
|
||||||
ts.tm_isdst = getboolfield(L, "isdst");
|
ts.tm_isdst = getboolfield(L, "isdst");
|
||||||
t = mktime(&ts);
|
t = mktime(&ts);
|
||||||
setallfields(L, &ts); /* update fields with normalized values */
|
setallfields(L, &ts); /* update fields with normalized values */
|
||||||
|
|||||||
@@ -23,18 +23,23 @@
|
|||||||
|
|
||||||
/* kinds of variables/expressions */
|
/* kinds of variables/expressions */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VVOID, /* when 'expdesc' describes the last expression a list,
|
VVOID, /* when 'expdesc' describes the last expression of a list,
|
||||||
this kind means an empty list (so, no expression) */
|
this kind means an empty list (so, no expression) */
|
||||||
VNIL, /* constant nil */
|
VNIL, /* constant nil */
|
||||||
VTRUE, /* constant true */
|
VTRUE, /* constant true */
|
||||||
VFALSE, /* constant false */
|
VFALSE, /* constant false */
|
||||||
VK, /* constant in 'k'; info = index of constant in 'k' */
|
VK, /* constant in 'k'; info = index of constant in 'k' */
|
||||||
VKFLT, /* floating constant; nval = numerical float value */
|
VKFLT, /* floating constant; nval = numerical float value */
|
||||||
VKINT, /* integer constant; nval = numerical integer value */
|
VKINT, /* integer constant; ival = numerical integer value */
|
||||||
|
VKSTR, /* string constant; strval = TString address;
|
||||||
|
(string is fixed by the lexer) */
|
||||||
VNONRELOC, /* expression has its value in a fixed register;
|
VNONRELOC, /* expression has its value in a fixed register;
|
||||||
info = result register */
|
info = result register */
|
||||||
VLOCAL, /* local variable; var.idx = local register */
|
VLOCAL, /* local variable; var.ridx = register index;
|
||||||
VUPVAL, /* upvalue variable; var.idx = index of upvalue in 'upvalues' */
|
var.vidx = relative index in 'actvar.arr' */
|
||||||
|
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
|
||||||
|
VCONST, /* compile-time <const> variable;
|
||||||
|
info = absolute index in 'actvar.arr' */
|
||||||
VINDEXED, /* indexed variable;
|
VINDEXED, /* indexed variable;
|
||||||
ind.t = table register;
|
ind.t = table register;
|
||||||
ind.idx = key's R index */
|
ind.idx = key's R index */
|
||||||
@@ -65,13 +70,15 @@ typedef struct expdesc {
|
|||||||
union {
|
union {
|
||||||
lua_Integer ival; /* for VKINT */
|
lua_Integer ival; /* for VKINT */
|
||||||
lua_Number nval; /* for VKFLT */
|
lua_Number nval; /* for VKFLT */
|
||||||
|
TString *strval; /* for VKSTR */
|
||||||
int info; /* for generic use */
|
int info; /* for generic use */
|
||||||
struct { /* for indexed variables */
|
struct { /* for indexed variables */
|
||||||
short idx; /* index (R or "long" K) */
|
short idx; /* index (R or "long" K) */
|
||||||
lu_byte t; /* table (register or upvalue) */
|
lu_byte t; /* table (register or upvalue) */
|
||||||
} ind;
|
} ind;
|
||||||
struct { /* for local variables and upvalues */
|
struct { /* for local variables */
|
||||||
lu_byte idx; /* index of the variable */
|
lu_byte ridx; /* register holding the variable */
|
||||||
|
unsigned short vidx; /* compiler index (in 'actvar.arr') */
|
||||||
} var;
|
} var;
|
||||||
} u;
|
} u;
|
||||||
int t; /* patch list of 'exit when true' */
|
int t; /* patch list of 'exit when true' */
|
||||||
@@ -79,20 +86,32 @@ typedef struct expdesc {
|
|||||||
} expdesc;
|
} expdesc;
|
||||||
|
|
||||||
|
|
||||||
|
/* kinds of variables */
|
||||||
|
#define VDKREG 0 /* regular */
|
||||||
|
#define RDKCONST 1 /* constant */
|
||||||
|
#define RDKTOCLOSE 2 /* to-be-closed */
|
||||||
|
#define RDKCTC 3 /* compile-time constant */
|
||||||
|
|
||||||
/* description of an active local variable */
|
/* description of an active local variable */
|
||||||
typedef struct Vardesc {
|
typedef union Vardesc {
|
||||||
TString *name;
|
struct {
|
||||||
short idx; /* index of the variable in the Proto's 'locvars' array */
|
TValuefields; /* constant value (if it is a compile-time constant) */
|
||||||
lu_byte ro; /* true if variable is 'const' */
|
lu_byte kind;
|
||||||
|
lu_byte ridx; /* register holding the variable */
|
||||||
|
short pidx; /* index of the variable in the Proto's 'locvars' array */
|
||||||
|
TString *name; /* variable name */
|
||||||
|
} vd;
|
||||||
|
TValue k; /* constant value (if any) */
|
||||||
} Vardesc;
|
} Vardesc;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* description of pending goto statements and label statements */
|
/* description of pending goto statements and label statements */
|
||||||
typedef struct Labeldesc {
|
typedef struct Labeldesc {
|
||||||
TString *name; /* label identifier */
|
TString *name; /* label identifier */
|
||||||
int pc; /* position in code */
|
int pc; /* position in code */
|
||||||
int line; /* line where it appeared */
|
int line; /* line where it appeared */
|
||||||
lu_byte nactvar; /* local level where it appears in current block */
|
lu_byte nactvar; /* number of active variables in that position */
|
||||||
lu_byte close; /* goto that escapes upvalues */
|
lu_byte close; /* goto that escapes upvalues */
|
||||||
} Labeldesc;
|
} Labeldesc;
|
||||||
|
|
||||||
@@ -107,7 +126,7 @@ typedef struct Labellist {
|
|||||||
|
|
||||||
/* dynamic structures used by the parser */
|
/* dynamic structures used by the parser */
|
||||||
typedef struct Dyndata {
|
typedef struct Dyndata {
|
||||||
struct { /* list of active local variables */
|
struct { /* list of all active local variables */
|
||||||
Vardesc *arr;
|
Vardesc *arr;
|
||||||
int n;
|
int n;
|
||||||
int size;
|
int size;
|
||||||
@@ -135,7 +154,7 @@ typedef struct FuncState {
|
|||||||
int nabslineinfo; /* number of elements in 'abslineinfo' */
|
int nabslineinfo; /* number of elements in 'abslineinfo' */
|
||||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||||
int firstlabel; /* index of first label (in 'dyd->label->arr') */
|
int firstlabel; /* index of first label (in 'dyd->label->arr') */
|
||||||
short nlocvars; /* number of elements in 'f->locvars' */
|
short ndebugvars; /* number of elements in 'f->locvars' */
|
||||||
lu_byte nactvar; /* number of active local variables */
|
lu_byte nactvar; /* number of active local variables */
|
||||||
lu_byte nups; /* number of upvalues */
|
lu_byte nups; /* number of upvalues */
|
||||||
lu_byte freereg; /* first free register */
|
lu_byte freereg; /* first free register */
|
||||||
@@ -144,6 +163,7 @@ typedef struct FuncState {
|
|||||||
} FuncState;
|
} FuncState;
|
||||||
|
|
||||||
|
|
||||||
|
LUAI_FUNC int luaY_nvarstack (FuncState *fs);
|
||||||
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||||
Dyndata *dyd, const char *name, int firstchar);
|
Dyndata *dyd, const char *name, int firstchar);
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
/*
|
/*
|
||||||
** Windows stuff
|
** Windows stuff
|
||||||
*/
|
*/
|
||||||
#if defined(_WIN32) /* { */
|
#if defined(_WIN32) /* { */
|
||||||
|
|
||||||
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||||
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
|
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
|
||||||
|
|||||||
@@ -96,45 +96,15 @@ void luaE_setdebt (global_State *g, l_mem debt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
|
||||||
** Increment count of "C calls" and check for overflows. In case of
|
UNUSED(L); UNUSED(limit);
|
||||||
** a stack overflow, check appropriate error ("regular" overflow or
|
return LUAI_MAXCCALLS; /* warning?? */
|
||||||
** overflow while handling stack overflow).
|
|
||||||
** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than
|
|
||||||
** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means
|
|
||||||
** it has just entered the "overflow zone", so the function raises an
|
|
||||||
** overflow error.
|
|
||||||
** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2
|
|
||||||
** (which means it is already handling an overflow) but smaller than
|
|
||||||
** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message
|
|
||||||
** handling to work).
|
|
||||||
** Otherwise, report a stack overflow while handling a stack overflow
|
|
||||||
** (probably caused by a repeating error in the message handling
|
|
||||||
** function).
|
|
||||||
*/
|
|
||||||
void luaE_enterCcall (lua_State *L) {
|
|
||||||
int ncalls = getCcalls(L);
|
|
||||||
L->nCcalls++;
|
|
||||||
if (ncalls >= LUAI_MAXCSTACK) { /* possible overflow? */
|
|
||||||
luaE_freeCI(L); /* release unused CIs */
|
|
||||||
ncalls = getCcalls(L); /* update call count */
|
|
||||||
if (ncalls >= LUAI_MAXCSTACK) { /* still overflow? */
|
|
||||||
if (ncalls <= LUAI_MAXCSTACK + CSTACKCF + 2) {
|
|
||||||
/* no error before increments; raise the error now */
|
|
||||||
L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */
|
|
||||||
luaG_runerror(L, "C stack overflow");
|
|
||||||
}
|
|
||||||
else if (ncalls >= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK >> 3)))
|
|
||||||
luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CallInfo *luaE_extendCI (lua_State *L) {
|
CallInfo *luaE_extendCI (lua_State *L) {
|
||||||
CallInfo *ci;
|
CallInfo *ci;
|
||||||
lua_assert(L->ci->next == NULL);
|
lua_assert(L->ci->next == NULL);
|
||||||
luaE_enterCcall(L);
|
|
||||||
ci = luaM_new(L, CallInfo);
|
ci = luaM_new(L, CallInfo);
|
||||||
lua_assert(L->ci->next == NULL);
|
lua_assert(L->ci->next == NULL);
|
||||||
L->ci->next = ci;
|
L->ci->next = ci;
|
||||||
@@ -149,69 +119,94 @@ CallInfo *luaE_extendCI (lua_State *L) {
|
|||||||
/*
|
/*
|
||||||
** free all CallInfo structures not in use by a thread
|
** 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 *ci = L->ci;
|
||||||
CallInfo *next = ci->next;
|
CallInfo *next = ci->next;
|
||||||
ci->next = NULL;
|
ci->next = NULL;
|
||||||
L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */
|
|
||||||
while ((ci = next) != NULL) {
|
while ((ci = next) != NULL) {
|
||||||
next = ci->next;
|
next = ci->next;
|
||||||
luaM_free(L, ci);
|
luaM_free(L, ci);
|
||||||
L->nci--;
|
L->nci--;
|
||||||
}
|
}
|
||||||
L->nCcalls += L->nci; /* adjust result */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** free half of the CallInfo structures not in use by a thread
|
** free half of the CallInfo structures not in use by a thread,
|
||||||
|
** keeping the first one.
|
||||||
*/
|
*/
|
||||||
void luaE_shrinkCI (lua_State *L) {
|
void luaE_shrinkCI (lua_State *L) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci->next; /* first free CallInfo */
|
||||||
CallInfo *next2; /* next's next */
|
CallInfo *next;
|
||||||
L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */
|
if (ci == NULL)
|
||||||
/* while there are two nexts */
|
return; /* no extra elements */
|
||||||
while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
|
while ((next = ci->next) != NULL) { /* two extra elements? */
|
||||||
luaM_free(L, ci->next); /* free next */
|
CallInfo *next2 = next->next; /* next's next */
|
||||||
|
ci->next = next2; /* remove next from the list */
|
||||||
L->nci--;
|
L->nci--;
|
||||||
ci->next = next2; /* remove 'next' from the list */
|
luaM_free(L, next); /* free next */
|
||||||
next2->previous = ci;
|
if (next2 == NULL)
|
||||||
ci = next2; /* keep next's next */
|
break; /* no more elements */
|
||||||
|
else {
|
||||||
|
next2->previous = ci;
|
||||||
|
ci = next2; /* continue */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
L->nCcalls += L->nci; /* adjust result */
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
|
||||||
|
** If equal, raises an overflow error. If value is larger than
|
||||||
|
** LUAI_MAXCCALLS (which means it is handling an overflow) but
|
||||||
|
** not much larger, does not report an error (to allow overflow
|
||||||
|
** handling to work).
|
||||||
|
*/
|
||||||
|
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_errerr(L); /* error while handling stack error */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
LUAI_FUNC void luaE_incCstack (lua_State *L) {
|
||||||
|
L->nCcalls++;
|
||||||
|
if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
|
||||||
|
luaE_checkcstack(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void stack_init (lua_State *L1, lua_State *L) {
|
static void stack_init (lua_State *L1, lua_State *L) {
|
||||||
int i; CallInfo *ci;
|
int i; CallInfo *ci;
|
||||||
/* initialize stack array */
|
/* initialize stack array */
|
||||||
L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);
|
L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
|
||||||
L1->stacksize = BASIC_STACK_SIZE;
|
L1->tbclist.p = L1->stack.p;
|
||||||
for (i = 0; i < BASIC_STACK_SIZE; i++)
|
for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
|
||||||
setnilvalue(s2v(L1->stack + i)); /* erase new stack */
|
setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */
|
||||||
L1->top = L1->stack;
|
L1->top.p = L1->stack.p;
|
||||||
L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
|
L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
|
||||||
/* initialize first ci */
|
/* initialize first ci */
|
||||||
ci = &L1->base_ci;
|
ci = &L1->base_ci;
|
||||||
ci->next = ci->previous = NULL;
|
ci->next = ci->previous = NULL;
|
||||||
ci->callstatus = CIST_C;
|
ci->callstatus = CIST_C;
|
||||||
ci->func = L1->top;
|
ci->func.p = L1->top.p;
|
||||||
ci->u.c.k = NULL;
|
ci->u.c.k = NULL;
|
||||||
ci->nresults = 0;
|
ci->nresults = 0;
|
||||||
setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
|
setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */
|
||||||
L1->top++;
|
L1->top.p++;
|
||||||
ci->top = L1->top + LUA_MINSTACK;
|
ci->top.p = L1->top.p + LUA_MINSTACK;
|
||||||
L1->ci = ci;
|
L1->ci = ci;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void freestack (lua_State *L) {
|
static void freestack (lua_State *L) {
|
||||||
if (L->stack == NULL)
|
if (L->stack.p == NULL)
|
||||||
return; /* stack not completely built yet */
|
return; /* stack not completely built yet */
|
||||||
L->ci = &L->base_ci; /* free the entire 'ci' list */
|
L->ci = &L->base_ci; /* free the entire 'ci' list */
|
||||||
luaE_freeCI(L);
|
freeCI(L);
|
||||||
lua_assert(L->nci == 0);
|
lua_assert(L->nci == 0);
|
||||||
luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
|
luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -219,24 +214,19 @@ static void freestack (lua_State *L) {
|
|||||||
** Create registry table and its predefined values
|
** Create registry table and its predefined values
|
||||||
*/
|
*/
|
||||||
static void init_registry (lua_State *L, global_State *g) {
|
static void init_registry (lua_State *L, global_State *g) {
|
||||||
TValue temp;
|
|
||||||
/* create registry */
|
/* create registry */
|
||||||
Table *registry = luaH_new(L);
|
Table *registry = luaH_new(L);
|
||||||
sethvalue(L, &g->l_registry, registry);
|
sethvalue(L, &g->l_registry, registry);
|
||||||
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
||||||
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
||||||
setthvalue(L, &temp, L); /* temp = L */
|
setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L);
|
||||||
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
|
/* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
|
||||||
/* registry[LUA_RIDX_GLOBALS] = table of globals */
|
sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
|
||||||
sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
|
|
||||||
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** open parts of the state that may cause memory-allocation errors.
|
** open parts of the state that may cause memory-allocation errors.
|
||||||
** ('g->nilvalue' being a nil value flags that the state was completely
|
|
||||||
** build.)
|
|
||||||
*/
|
*/
|
||||||
static void f_luaopen (lua_State *L, void *ud) {
|
static void f_luaopen (lua_State *L, void *ud) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
@@ -246,8 +236,8 @@ static void f_luaopen (lua_State *L, void *ud) {
|
|||||||
luaS_init(L);
|
luaS_init(L);
|
||||||
luaT_init(L);
|
luaT_init(L);
|
||||||
luaX_init(L);
|
luaX_init(L);
|
||||||
g->gcrunning = 1; /* allow gc */
|
g->gcstp = 0; /* allow gc */
|
||||||
setnilvalue(&g->nilvalue);
|
setnilvalue(&g->nilvalue); /* now state is complete */
|
||||||
luai_userstateopen(L);
|
luai_userstateopen(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,13 +248,12 @@ static void f_luaopen (lua_State *L, void *ud) {
|
|||||||
*/
|
*/
|
||||||
static void preinit_thread (lua_State *L, global_State *g) {
|
static void preinit_thread (lua_State *L, global_State *g) {
|
||||||
G(L) = g;
|
G(L) = g;
|
||||||
L->stack = NULL;
|
L->stack.p = NULL;
|
||||||
L->ci = NULL;
|
L->ci = NULL;
|
||||||
L->nci = 0;
|
L->nci = 0;
|
||||||
L->stacksize = 0;
|
|
||||||
L->twups = L; /* thread has no upvalues */
|
L->twups = L; /* thread has no upvalues */
|
||||||
L->errorJmp = NULL;
|
|
||||||
L->nCcalls = 0;
|
L->nCcalls = 0;
|
||||||
|
L->errorJmp = NULL;
|
||||||
L->hook = NULL;
|
L->hook = NULL;
|
||||||
L->hookmask = 0;
|
L->hookmask = 0;
|
||||||
L->basehookcount = 0;
|
L->basehookcount = 0;
|
||||||
@@ -273,15 +262,22 @@ static void preinit_thread (lua_State *L, global_State *g) {
|
|||||||
L->openupval = NULL;
|
L->openupval = NULL;
|
||||||
L->status = LUA_OK;
|
L->status = LUA_OK;
|
||||||
L->errfunc = 0;
|
L->errfunc = 0;
|
||||||
|
L->oldpc = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void close_state (lua_State *L) {
|
static void close_state (lua_State *L) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */
|
if (!completestate(g)) /* closing a partially built state? */
|
||||||
luaC_freeallobjects(L); /* collect all objects */
|
luaC_freeallobjects(L); /* just collect its objects */
|
||||||
if (ttisnil(&g->nilvalue)) /* closing a fully built state? */
|
else { /* closing a fully built state */
|
||||||
|
L->ci = &L->base_ci; /* unwind CallInfo list */
|
||||||
|
L->errfunc = 0; /* stack unwind can "throw away" the error function */
|
||||||
|
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);
|
luai_userstateclose(L);
|
||||||
|
}
|
||||||
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
|
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
|
||||||
freestack(L);
|
freestack(L);
|
||||||
lua_assert(gettotalbytes(g) == sizeof(LG));
|
lua_assert(gettotalbytes(g) == sizeof(LG));
|
||||||
@@ -291,18 +287,15 @@ static void close_state (lua_State *L) {
|
|||||||
|
|
||||||
LUA_API lua_State *lua_newthread (lua_State *L) {
|
LUA_API lua_State *lua_newthread (lua_State *L) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
|
GCObject *o;
|
||||||
lua_State *L1;
|
lua_State *L1;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
/* create new thread */
|
/* create new thread */
|
||||||
L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
|
o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
|
||||||
L1->marked = luaC_white(g);
|
L1 = gco2th(o);
|
||||||
L1->tt = LUA_TTHREAD;
|
|
||||||
/* link it on list 'allgc' */
|
|
||||||
L1->next = g->allgc;
|
|
||||||
g->allgc = obj2gco(L1);
|
|
||||||
/* anchor it on L stack */
|
/* anchor it on L stack */
|
||||||
setthvalue2s(L, L->top, L1);
|
setthvalue2s(L, L->top.p, L1);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
preinit_thread(L1, g);
|
preinit_thread(L1, g);
|
||||||
L1->hookmask = L->hookmask;
|
L1->hookmask = L->hookmask;
|
||||||
@@ -321,7 +314,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
|
|||||||
|
|
||||||
void luaE_freethread (lua_State *L, lua_State *L1) {
|
void luaE_freethread (lua_State *L, lua_State *L1) {
|
||||||
LX *l = fromstate(L1);
|
LX *l = fromstate(L1);
|
||||||
luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */
|
luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
|
||||||
lua_assert(L1->openupval == NULL);
|
lua_assert(L1->openupval == NULL);
|
||||||
luai_userstatefree(L, L1);
|
luai_userstatefree(L, L1);
|
||||||
freestack(L1);
|
freestack(L1);
|
||||||
@@ -329,29 +322,44 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int lua_resetthread (lua_State *L) {
|
int luaE_resetthread (lua_State *L, int status) {
|
||||||
CallInfo *ci;
|
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;
|
||||||
|
if (status == LUA_YIELD)
|
||||||
|
status = LUA_OK;
|
||||||
|
L->status = LUA_OK; /* so it can run __close metamethods */
|
||||||
|
L->errfunc = 0; /* stack unwind can "throw away" the error function */
|
||||||
|
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);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
LUA_API int lua_closethread (lua_State *L, lua_State *from) {
|
||||||
int status;
|
int status;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
ci = &L->base_ci;
|
L->nCcalls = (from) ? getCcalls(from) : 0;
|
||||||
status = luaF_close(L, L->stack, CLOSEPROTECT);
|
status = luaE_resetthread(L, L->status);
|
||||||
setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */
|
|
||||||
if (status != CLOSEPROTECT) /* real errors? */
|
|
||||||
luaD_seterrorobj(L, status, L->stack + 1);
|
|
||||||
else {
|
|
||||||
status = LUA_OK;
|
|
||||||
L->top = L->stack + 1;
|
|
||||||
}
|
|
||||||
ci->callstatus = CIST_C;
|
|
||||||
ci->func = L->stack;
|
|
||||||
ci->top = L->top + LUA_MINSTACK;
|
|
||||||
L->ci = ci;
|
|
||||||
L->status = status;
|
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Deprecated! Use 'lua_closethread' instead.
|
||||||
|
*/
|
||||||
|
LUA_API int lua_resetthread (lua_State *L) {
|
||||||
|
return lua_closethread(L, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||||
int i;
|
int i;
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
@@ -360,29 +368,31 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
|||||||
if (l == NULL) return NULL;
|
if (l == NULL) return NULL;
|
||||||
L = &l->l.l;
|
L = &l->l.l;
|
||||||
g = &l->g;
|
g = &l->g;
|
||||||
L->tt = LUA_TTHREAD;
|
L->tt = LUA_VTHREAD;
|
||||||
g->currentwhite = bitmask(WHITE0BIT);
|
g->currentwhite = bitmask(WHITE0BIT);
|
||||||
L->marked = luaC_white(g);
|
L->marked = luaC_white(g);
|
||||||
preinit_thread(L, g);
|
preinit_thread(L, g);
|
||||||
g->allgc = obj2gco(L); /* by now, only object is the main thread */
|
g->allgc = obj2gco(L); /* by now, only object is the main thread */
|
||||||
L->next = NULL;
|
L->next = NULL;
|
||||||
|
incnny(L); /* main thread is always non yieldable */
|
||||||
g->frealloc = f;
|
g->frealloc = f;
|
||||||
g->ud = ud;
|
g->ud = ud;
|
||||||
g->warnf = NULL;
|
g->warnf = NULL;
|
||||||
g->ud_warn = NULL;
|
g->ud_warn = NULL;
|
||||||
g->mainthread = L;
|
g->mainthread = L;
|
||||||
g->seed = luai_makeseed(L);
|
g->seed = luai_makeseed(L);
|
||||||
g->gcrunning = 0; /* no GC while building state */
|
g->gcstp = GCSTPGC; /* no GC while building state */
|
||||||
g->strt.size = g->strt.nuse = 0;
|
g->strt.size = g->strt.nuse = 0;
|
||||||
g->strt.hash = NULL;
|
g->strt.hash = NULL;
|
||||||
setnilvalue(&g->l_registry);
|
setnilvalue(&g->l_registry);
|
||||||
g->panic = NULL;
|
g->panic = NULL;
|
||||||
g->gcstate = GCSpause;
|
g->gcstate = GCSpause;
|
||||||
g->gckind = KGC_INC;
|
g->gckind = KGC_INC;
|
||||||
|
g->gcstopem = 0;
|
||||||
g->gcemergency = 0;
|
g->gcemergency = 0;
|
||||||
g->finobj = g->tobefnz = g->fixedgc = NULL;
|
g->finobj = g->tobefnz = g->fixedgc = NULL;
|
||||||
g->survival = g->old = g->reallyold = NULL;
|
g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
|
||||||
g->finobjsur = g->finobjold = g->finobjrold = NULL;
|
g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
|
||||||
g->sweepgc = NULL;
|
g->sweepgc = NULL;
|
||||||
g->gray = g->grayagain = NULL;
|
g->gray = g->grayagain = NULL;
|
||||||
g->weak = g->ephemeron = g->allweak = NULL;
|
g->weak = g->ephemeron = g->allweak = NULL;
|
||||||
@@ -407,8 +417,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API void lua_close (lua_State *L) {
|
LUA_API void lua_close (lua_State *L) {
|
||||||
L = G(L)->mainthread; /* only the main thread can be closed */
|
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
|
L = G(L)->mainthread; /* only the main thread can be closed */
|
||||||
close_state(L);
|
close_state(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,3 +430,19 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Generate a warning from an error message
|
||||||
|
*/
|
||||||
|
void luaE_warnerror (lua_State *L, const char *where) {
|
||||||
|
TValue *errobj = s2v(L->top.p - 1); /* error object */
|
||||||
|
const char *msg = (ttisstring(errobj))
|
||||||
|
? getstr(tsvalue(errobj))
|
||||||
|
: "error object is not a string";
|
||||||
|
/* produce warning "error in %s (%s)" (where, msg) */
|
||||||
|
luaE_warning(L, "error in ", 1);
|
||||||
|
luaE_warning(L, where, 1);
|
||||||
|
luaE_warning(L, " (", 1);
|
||||||
|
luaE_warning(L, msg, 1);
|
||||||
|
luaE_warning(L, ")", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,11 @@
|
|||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* Some header files included here need this definition */
|
||||||
|
typedef struct CallInfo CallInfo;
|
||||||
|
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "ltm.h"
|
#include "ltm.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
@@ -32,13 +37,29 @@
|
|||||||
**
|
**
|
||||||
** 'allgc' -> 'survival': new objects;
|
** 'allgc' -> 'survival': new objects;
|
||||||
** 'survival' -> 'old': objects that survived one collection;
|
** 'survival' -> 'old': objects that survived one collection;
|
||||||
** 'old' -> 'reallyold': objects that became old in last collection;
|
** 'old1' -> 'reallyold': objects that became old in last collection;
|
||||||
** 'reallyold' -> NULL: objects old for more than one cycle.
|
** 'reallyold' -> NULL: objects old for more than one cycle.
|
||||||
**
|
**
|
||||||
** 'finobj' -> 'finobjsur': new objects marked for finalization;
|
** 'finobj' -> 'finobjsur': new objects marked for finalization;
|
||||||
** 'finobjsur' -> 'finobjold': survived """";
|
** 'finobjsur' -> 'finobjold1': survived """";
|
||||||
** 'finobjold' -> 'finobjrold': just old """";
|
** 'finobjold1' -> 'finobjrold': just old """";
|
||||||
** 'finobjrold' -> NULL: really old """".
|
** 'finobjrold' -> NULL: really old """".
|
||||||
|
**
|
||||||
|
** All lists can contain elements older than their main ages, due
|
||||||
|
** to 'luaC_checkfinalizer' and 'udata2finalize', which move
|
||||||
|
** objects between the normal lists and the "marked for finalization"
|
||||||
|
** lists. Moreover, barriers can age young objects in young lists as
|
||||||
|
** OLD0, which then become OLD1. However, a list never contains
|
||||||
|
** elements younger than their main ages.
|
||||||
|
**
|
||||||
|
** The generational collector also uses a pointer 'firstold1', which
|
||||||
|
** points to the first OLD1 object in the list. It is used to optimize
|
||||||
|
** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc'
|
||||||
|
** and 'reallyold', but often the list has no OLD1 objects or they are
|
||||||
|
** after 'old1'.) Note the difference between it and 'old1':
|
||||||
|
** 'firstold1': no OLD1 objects before this point; there can be all
|
||||||
|
** ages after it.
|
||||||
|
** 'old1': no objects younger than OLD1 after this point.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -47,7 +68,7 @@
|
|||||||
** can become gray have such a field. The field is not the same
|
** can become gray have such a field. The field is not the same
|
||||||
** in all objects, but it always has this name.) Any gray object
|
** in all objects, but it always has this name.) Any gray object
|
||||||
** must belong to one of these lists, and all objects in these lists
|
** must belong to one of these lists, and all objects in these lists
|
||||||
** must be gray:
|
** must be gray (with two exceptions explained below):
|
||||||
**
|
**
|
||||||
** 'gray': regular gray objects, still waiting to be visited.
|
** 'gray': regular gray objects, still waiting to be visited.
|
||||||
** 'grayagain': objects that must be revisited at the atomic phase.
|
** 'grayagain': objects that must be revisited at the atomic phase.
|
||||||
@@ -58,33 +79,26 @@
|
|||||||
** 'weak': tables with weak values to be cleared;
|
** 'weak': tables with weak values to be cleared;
|
||||||
** 'ephemeron': ephemeron tables with white->white entries;
|
** 'ephemeron': ephemeron tables with white->white entries;
|
||||||
** 'allweak': tables with weak keys and/or weak values to be cleared.
|
** 'allweak': tables with weak keys and/or weak values to be cleared.
|
||||||
|
**
|
||||||
|
** The exceptions to that "gray rule" are:
|
||||||
|
** - TOUCHED2 objects in generational mode stay in a gray list (because
|
||||||
|
** 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
|
||||||
|
** of gray lists. (They don't even have a 'gclist' field.)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of
|
** About 'nCcalls': This count has two parts: the lower 16 bits counts
|
||||||
** how many "C calls" it can do in the C stack, to avoid C-stack overflow.
|
** the number of recursive invocations in the C stack; the higher
|
||||||
** This count is very rough approximation; it considers only recursive
|
** 16 bits counts the number of non-yieldable calls in the stack.
|
||||||
** functions inside the interpreter, as non-recursive calls can be
|
** (They are together so that we can change and save both with one
|
||||||
** considered using a fixed (although unknown) amount of stack space.
|
** instruction.)
|
||||||
**
|
|
||||||
** The count itself has two parts: the lower part is the count itself;
|
|
||||||
** the higher part counts the number of non-yieldable calls in the stack.
|
|
||||||
**
|
|
||||||
** Because calls to external C functions can use of unkown amount
|
|
||||||
** of space (e.g., functions using an auxiliary buffer), calls
|
|
||||||
** to these functions add more than one to the count.
|
|
||||||
**
|
|
||||||
** The proper count also includes the number of CallInfo structures
|
|
||||||
** allocated by Lua, as a kind of "potential" calls. So, when Lua
|
|
||||||
** calls a function (and "consumes" one CallInfo), it needs neither to
|
|
||||||
** increment nor to check 'nCcalls', as its use of C stack is already
|
|
||||||
** accounted for.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* number of "C stack slots" used by an external C function */
|
|
||||||
#define CSTACKCF 10
|
|
||||||
|
|
||||||
/* true if this thread does not have non-yieldable calls in the stack */
|
/* true if this thread does not have non-yieldable calls in the stack */
|
||||||
#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0)
|
#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0)
|
||||||
@@ -99,13 +113,8 @@
|
|||||||
/* Decrement the number of non-yieldable calls */
|
/* Decrement the number of non-yieldable calls */
|
||||||
#define decnny(L) ((L)->nCcalls -= 0x10000)
|
#define decnny(L) ((L)->nCcalls -= 0x10000)
|
||||||
|
|
||||||
/* Increment the number of non-yieldable calls and nCcalls */
|
/* Non-yieldable call increment */
|
||||||
#define incXCcalls(L) ((L)->nCcalls += 0x10000 + CSTACKCF)
|
#define nyci (0x10000 | 1)
|
||||||
|
|
||||||
/* Decrement the number of non-yieldable calls and nCcalls */
|
|
||||||
#define decXCcalls(L) ((L)->nCcalls -= 0x10000 + CSTACKCF)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -123,12 +132,20 @@ struct lua_longjmp; /* defined in ldo.c */
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* extra stack space to handle TM calls and some other extras */
|
/*
|
||||||
|
** Extra stack space to handle TM calls and some other extras. This
|
||||||
|
** space is not included in 'stack_last'. It is used only to avoid stack
|
||||||
|
** checks, either because the element will be promptly popped or because
|
||||||
|
** there will be a stack check soon after the push. Function frames
|
||||||
|
** never use this extra space, so it does not need to be kept clean.
|
||||||
|
*/
|
||||||
#define EXTRA_STACK 5
|
#define EXTRA_STACK 5
|
||||||
|
|
||||||
|
|
||||||
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
||||||
|
|
||||||
|
#define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p)
|
||||||
|
|
||||||
|
|
||||||
/* kinds of Garbage Collection */
|
/* kinds of Garbage Collection */
|
||||||
#define KGC_INC 0 /* incremental gc */
|
#define KGC_INC 0 /* incremental gc */
|
||||||
@@ -144,15 +161,27 @@ typedef struct stringtable {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Information about a call.
|
** Information about a call.
|
||||||
|
** About union 'u':
|
||||||
|
** - field 'l' is used only for Lua functions;
|
||||||
|
** - field 'c' is used only for C functions.
|
||||||
|
** About union 'u2':
|
||||||
|
** - field 'funcidx' is used only by C functions while doing a
|
||||||
|
** protected call;
|
||||||
|
** - field 'nyield' is used only while a function is "doing" an
|
||||||
|
** 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.
|
||||||
*/
|
*/
|
||||||
typedef struct CallInfo {
|
struct CallInfo {
|
||||||
StkId func; /* function index in the stack */
|
StkIdRel func; /* function index in the stack */
|
||||||
StkId top; /* top for this function */
|
StkIdRel top; /* top for this function */
|
||||||
struct CallInfo *previous, *next; /* dynamic call link */
|
struct CallInfo *previous, *next; /* dynamic call link */
|
||||||
union {
|
union {
|
||||||
struct { /* only for Lua functions */
|
struct { /* only for Lua functions */
|
||||||
const Instruction *savedpc;
|
const Instruction *savedpc;
|
||||||
l_signalT trap;
|
volatile l_signalT trap; /* function is tracing lines/counts */
|
||||||
int nextraargs; /* # of extra arguments in vararg functions */
|
int nextraargs; /* # of extra arguments in vararg functions */
|
||||||
} l;
|
} l;
|
||||||
struct { /* only for C functions */
|
struct { /* only for C functions */
|
||||||
@@ -164,14 +193,15 @@ typedef struct CallInfo {
|
|||||||
union {
|
union {
|
||||||
int funcidx; /* called-function index */
|
int funcidx; /* called-function index */
|
||||||
int nyield; /* number of values yielded */
|
int nyield; /* number of values yielded */
|
||||||
struct { /* info about transfered values (for call/return hooks) */
|
int nres; /* number of values returned */
|
||||||
unsigned short ftransfer; /* offset of first value transfered */
|
struct { /* info about transferred values (for call/return hooks) */
|
||||||
unsigned short ntransfer; /* number of values transfered */
|
unsigned short ftransfer; /* offset of first value transferred */
|
||||||
|
unsigned short ntransfer; /* number of values transferred */
|
||||||
} transferinfo;
|
} transferinfo;
|
||||||
} u2;
|
} u2;
|
||||||
short nresults; /* expected number of results from this function */
|
short nresults; /* expected number of results from this function */
|
||||||
unsigned short callstatus;
|
unsigned short callstatus;
|
||||||
} CallInfo;
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -179,16 +209,34 @@ typedef struct CallInfo {
|
|||||||
*/
|
*/
|
||||||
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
||||||
#define CIST_C (1<<1) /* call is running a C function */
|
#define CIST_C (1<<1) /* call is running a C function */
|
||||||
#define CIST_HOOKED (1<<2) /* call is running a debug hook */
|
#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */
|
||||||
#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */
|
#define CIST_HOOKED (1<<3) /* call is running a debug hook */
|
||||||
#define CIST_TAIL (1<<4) /* call was tail called */
|
#define CIST_YPCALL (1<<4) /* doing a yieldable protected call */
|
||||||
#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */
|
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||||
#define CIST_FIN (1<<6) /* call is running a finalizer */
|
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||||
#define CIST_TRAN (1<<7) /* 'ci' has transfer information */
|
#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)
|
#if defined(LUA_COMPAT_LT_LE)
|
||||||
#define CIST_LEQ (1<<8) /* using __lt for __le */
|
#define CIST_LEQ (1<<13) /* using __lt for __le */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Field CIST_RECST stores the "recover status", used to keep the error
|
||||||
|
** status while closing to-be-closed variables in coroutines, so that
|
||||||
|
** Lua can correctly resume after an yield from a __close method called
|
||||||
|
** because of an error. (Three bits are enough for error status.)
|
||||||
|
*/
|
||||||
|
#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)))
|
||||||
|
|
||||||
|
|
||||||
/* active function is a Lua function */
|
/* active function is a Lua function */
|
||||||
#define isLua(ci) (!((ci)->callstatus & CIST_C))
|
#define isLua(ci) (!((ci)->callstatus & CIST_C))
|
||||||
|
|
||||||
@@ -217,9 +265,10 @@ typedef struct global_State {
|
|||||||
lu_byte currentwhite;
|
lu_byte currentwhite;
|
||||||
lu_byte gcstate; /* state of garbage collector */
|
lu_byte gcstate; /* state of garbage collector */
|
||||||
lu_byte gckind; /* kind of GC running */
|
lu_byte gckind; /* kind of GC running */
|
||||||
|
lu_byte gcstopem; /* stops emergency collections */
|
||||||
lu_byte genminormul; /* control for minor generational collections */
|
lu_byte genminormul; /* control for minor generational collections */
|
||||||
lu_byte genmajormul; /* control for major generational collections */
|
lu_byte genmajormul; /* control for major generational collections */
|
||||||
lu_byte gcrunning; /* true if GC is running */
|
lu_byte gcstp; /* control whether GC is running */
|
||||||
lu_byte gcemergency; /* true if this is an emergency collection */
|
lu_byte gcemergency; /* true if this is an emergency collection */
|
||||||
lu_byte gcpause; /* size of pause between successive GCs */
|
lu_byte gcpause; /* size of pause between successive GCs */
|
||||||
lu_byte gcstepmul; /* GC "speed" */
|
lu_byte gcstepmul; /* GC "speed" */
|
||||||
@@ -236,17 +285,18 @@ typedef struct global_State {
|
|||||||
GCObject *fixedgc; /* list of objects not to be collected */
|
GCObject *fixedgc; /* list of objects not to be collected */
|
||||||
/* fields for generational collector */
|
/* fields for generational collector */
|
||||||
GCObject *survival; /* start of objects that survived one GC cycle */
|
GCObject *survival; /* start of objects that survived one GC cycle */
|
||||||
GCObject *old; /* start of old objects */
|
GCObject *old1; /* start of old1 objects */
|
||||||
GCObject *reallyold; /* old objects with more than one cycle */
|
GCObject *reallyold; /* objects more than one cycle old ("really old") */
|
||||||
|
GCObject *firstold1; /* first OLD1 object in the list (if any) */
|
||||||
GCObject *finobjsur; /* list of survival objects with finalizers */
|
GCObject *finobjsur; /* list of survival objects with finalizers */
|
||||||
GCObject *finobjold; /* list of old objects with finalizers */
|
GCObject *finobjold1; /* list of old1 objects with finalizers */
|
||||||
GCObject *finobjrold; /* list of really old objects with finalizers */
|
GCObject *finobjrold; /* list of really old objects with finalizers */
|
||||||
struct lua_State *twups; /* list of threads with open upvalues */
|
struct lua_State *twups; /* list of threads with open upvalues */
|
||||||
lua_CFunction panic; /* to be called in unprotected errors */
|
lua_CFunction panic; /* to be called in unprotected errors */
|
||||||
struct lua_State *mainthread;
|
struct lua_State *mainthread;
|
||||||
TString *memerrmsg; /* message for memory-allocation errors */
|
TString *memerrmsg; /* message for memory-allocation errors */
|
||||||
TString *tmname[TM_N]; /* array with tag-method names */
|
TString *tmname[TM_N]; /* array with tag-method names */
|
||||||
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
|
struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */
|
||||||
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
||||||
lua_WarnFunction warnf; /* warning function */
|
lua_WarnFunction warnf; /* warning function */
|
||||||
void *ud_warn; /* auxiliary data to 'warnf' */
|
void *ud_warn; /* auxiliary data to 'warnf' */
|
||||||
@@ -261,32 +311,44 @@ struct lua_State {
|
|||||||
lu_byte status;
|
lu_byte status;
|
||||||
lu_byte allowhook;
|
lu_byte allowhook;
|
||||||
unsigned short nci; /* number of items in 'ci' list */
|
unsigned short nci; /* number of items in 'ci' list */
|
||||||
StkId top; /* first free slot in the stack */
|
StkIdRel top; /* first free slot in the stack */
|
||||||
global_State *l_G;
|
global_State *l_G;
|
||||||
CallInfo *ci; /* call info for current function */
|
CallInfo *ci; /* call info for current function */
|
||||||
const Instruction *oldpc; /* last pc traced */
|
StkIdRel stack_last; /* end of stack (last element + 1) */
|
||||||
StkId stack_last; /* last free slot in the stack */
|
StkIdRel stack; /* stack base */
|
||||||
StkId stack; /* stack base */
|
|
||||||
UpVal *openupval; /* list of open upvalues in this stack */
|
UpVal *openupval; /* list of open upvalues in this stack */
|
||||||
|
StkIdRel tbclist; /* list of to-be-closed variables */
|
||||||
GCObject *gclist;
|
GCObject *gclist;
|
||||||
struct lua_State *twups; /* list of threads with open upvalues */
|
struct lua_State *twups; /* list of threads with open upvalues */
|
||||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||||
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
|
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
|
||||||
volatile lua_Hook hook;
|
volatile lua_Hook hook;
|
||||||
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
||||||
l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */
|
l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */
|
||||||
int stacksize;
|
int oldpc; /* last pc traced */
|
||||||
int basehookcount;
|
int basehookcount;
|
||||||
int hookcount;
|
int hookcount;
|
||||||
l_signalT hookmask;
|
volatile l_signalT hookmask;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#define G(L) (L->l_G)
|
#define G(L) (L->l_G)
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 'g->nilvalue' being a nil value flags that the state was completely
|
||||||
|
** build.
|
||||||
|
*/
|
||||||
|
#define completestate(g) ttisnil(&g->nilvalue)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Union of all collectable objects (only for conversions)
|
** Union of all collectable objects (only for conversions)
|
||||||
|
** ISO C99, 6.5.2.3 p.5:
|
||||||
|
** "if a union contains several structures that share a common initial
|
||||||
|
** sequence [...], and if the union object currently contains one
|
||||||
|
** of these structures, it is permitted to inspect the common initial
|
||||||
|
** part of any of them anywhere that a declaration of the complete type
|
||||||
|
** of the union is visible."
|
||||||
*/
|
*/
|
||||||
union GCUnion {
|
union GCUnion {
|
||||||
GCObject gc; /* common header */
|
GCObject gc; /* common header */
|
||||||
@@ -300,21 +362,25 @@ union GCUnion {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** ISO C99, 6.7.2.1 p.14:
|
||||||
|
** "A pointer to a union object, suitably converted, points to each of
|
||||||
|
** its members [...], and vice versa."
|
||||||
|
*/
|
||||||
#define cast_u(o) cast(union GCUnion *, (o))
|
#define cast_u(o) cast(union GCUnion *, (o))
|
||||||
|
|
||||||
/* macros to convert a GCObject into a specific value */
|
/* macros to convert a GCObject into a specific value */
|
||||||
#define gco2ts(o) \
|
#define gco2ts(o) \
|
||||||
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
|
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
|
||||||
#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
|
#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u))
|
||||||
#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
|
#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l))
|
||||||
#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
|
#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c))
|
||||||
#define gco2cl(o) \
|
#define gco2cl(o) \
|
||||||
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
|
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
|
||||||
#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
|
#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h))
|
||||||
#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
|
#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p))
|
||||||
#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
|
#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th))
|
||||||
#define gco2upv(o) \
|
#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv))
|
||||||
check_exp(novariant((o)->tt) == LUA_TUPVAL, &((cast_u(o))->upv))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -330,13 +396,13 @@ union GCUnion {
|
|||||||
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
|
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
|
||||||
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
||||||
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
|
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
|
||||||
LUAI_FUNC void luaE_freeCI (lua_State *L);
|
|
||||||
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
|
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
|
||||||
LUAI_FUNC void luaE_enterCcall (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_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);
|
||||||
|
|
||||||
|
|
||||||
#define luaE_exitCcall(L) ((L)->nCcalls--)
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -22,16 +22,6 @@
|
|||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
|
||||||
** compute its hash
|
|
||||||
*/
|
|
||||||
#if !defined(LUAI_HASHLIMIT)
|
|
||||||
#define LUAI_HASHLIMIT 5
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Maximum size for string table.
|
** Maximum size for string table.
|
||||||
*/
|
*/
|
||||||
@@ -43,26 +33,26 @@
|
|||||||
*/
|
*/
|
||||||
int luaS_eqlngstr (TString *a, TString *b) {
|
int luaS_eqlngstr (TString *a, TString *b) {
|
||||||
size_t len = a->u.lnglen;
|
size_t len = a->u.lnglen;
|
||||||
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
|
lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
|
||||||
return (a == b) || /* same instance or... */
|
return (a == b) || /* same instance or... */
|
||||||
((len == b->u.lnglen) && /* equal length and ... */
|
((len == b->u.lnglen) && /* equal length and ... */
|
||||||
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
(memcmp(getlngstr(a), getlngstr(b), len) == 0)); /* equal contents */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
||||||
unsigned int h = seed ^ cast_uint(l);
|
unsigned int h = seed ^ cast_uint(l);
|
||||||
size_t step = (l >> LUAI_HASHLIMIT) + 1;
|
for (; l > 0; l--)
|
||||||
for (; l >= step; l -= step)
|
|
||||||
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
|
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int luaS_hashlongstr (TString *ts) {
|
unsigned int luaS_hashlongstr (TString *ts) {
|
||||||
lua_assert(ts->tt == LUA_TLNGSTR);
|
lua_assert(ts->tt == LUA_VLNGSTR);
|
||||||
if (ts->extra == 0) { /* no hash? */
|
if (ts->extra == 0) { /* no hash? */
|
||||||
ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
|
size_t len = ts->u.lnglen;
|
||||||
|
ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
|
||||||
ts->extra = 1; /* now it has its hash */
|
ts->extra = 1; /* now it has its hash */
|
||||||
}
|
}
|
||||||
return ts->hash;
|
return ts->hash;
|
||||||
@@ -99,7 +89,7 @@ void luaS_resize (lua_State *L, int nsize) {
|
|||||||
if (nsize < osize) /* shrinking table? */
|
if (nsize < osize) /* shrinking table? */
|
||||||
tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
|
tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
|
||||||
newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
|
newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
|
||||||
if (unlikely(newvect == NULL)) { /* reallocation failed? */
|
if (l_unlikely(newvect == NULL)) { /* reallocation failed? */
|
||||||
if (nsize < osize) /* was it shrinking table? */
|
if (nsize < osize) /* was it shrinking table? */
|
||||||
tablerehash(tb->hash, nsize, osize); /* restore to original size */
|
tablerehash(tb->hash, nsize, osize); /* restore to original size */
|
||||||
/* leave table as it was */
|
/* leave table as it was */
|
||||||
@@ -165,8 +155,9 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
|
|||||||
|
|
||||||
|
|
||||||
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
|
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
|
||||||
TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
|
TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
|
||||||
ts->u.lnglen = l;
|
ts->u.lnglen = l;
|
||||||
|
ts->shrlen = 0xFF; /* signals that it is a long string */
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +173,7 @@ void luaS_remove (lua_State *L, TString *ts) {
|
|||||||
|
|
||||||
|
|
||||||
static void growstrtab (lua_State *L, stringtable *tb) {
|
static void growstrtab (lua_State *L, stringtable *tb) {
|
||||||
if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
|
if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
|
||||||
luaC_fullgc(L, 1); /* try to free some... */
|
luaC_fullgc(L, 1); /* try to free some... */
|
||||||
if (tb->nuse == MAX_INT) /* still too many? */
|
if (tb->nuse == MAX_INT) /* still too many? */
|
||||||
luaM_error(L); /* cannot even create a message... */
|
luaM_error(L); /* cannot even create a message... */
|
||||||
@@ -203,7 +194,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
|||||||
TString **list = &tb->hash[lmod(h, tb->size)];
|
TString **list = &tb->hash[lmod(h, tb->size)];
|
||||||
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
|
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
|
||||||
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
||||||
if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
if (l == ts->shrlen && (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
|
||||||
/* found! */
|
/* found! */
|
||||||
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
||||||
changewhite(ts); /* resurrect it */
|
changewhite(ts); /* resurrect it */
|
||||||
@@ -215,9 +206,9 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
|||||||
growstrtab(L, tb);
|
growstrtab(L, tb);
|
||||||
list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
|
list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
|
||||||
}
|
}
|
||||||
ts = createstrobj(L, l, LUA_TSHRSTR, h);
|
ts = createstrobj(L, l, LUA_VSHRSTR, h);
|
||||||
memcpy(getstr(ts), str, l * sizeof(char));
|
|
||||||
ts->shrlen = cast_byte(l);
|
ts->shrlen = cast_byte(l);
|
||||||
|
memcpy(getshrstr(ts), str, l * sizeof(char));
|
||||||
ts->u.hnext = *list;
|
ts->u.hnext = *list;
|
||||||
*list = ts;
|
*list = ts;
|
||||||
tb->nuse++;
|
tb->nuse++;
|
||||||
@@ -233,10 +224,10 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
|||||||
return internshrstr(L, str, l);
|
return internshrstr(L, str, l);
|
||||||
else {
|
else {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
|
if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
|
||||||
luaM_toobig(L);
|
luaM_toobig(L);
|
||||||
ts = luaS_createlngstrobj(L, l);
|
ts = luaS_createlngstrobj(L, l);
|
||||||
memcpy(getstr(ts), str, l * sizeof(char));
|
memcpy(getlngstr(ts), str, l * sizeof(char));
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -269,9 +260,9 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
|
|||||||
Udata *u;
|
Udata *u;
|
||||||
int i;
|
int i;
|
||||||
GCObject *o;
|
GCObject *o;
|
||||||
if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
|
if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
|
||||||
luaM_toobig(L);
|
luaM_toobig(L);
|
||||||
o = luaC_newobj(L, LUA_TUSERDATA, sizeudata(nuvalue, s));
|
o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
|
||||||
u = gco2u(o);
|
u = gco2u(o);
|
||||||
u->len = s;
|
u->len = s;
|
||||||
u->nuvalue = nuvalue;
|
u->nuvalue = nuvalue;
|
||||||
|
|||||||
@@ -19,7 +19,11 @@
|
|||||||
#define MEMERRMSG "not enough memory"
|
#define MEMERRMSG "not enough memory"
|
||||||
|
|
||||||
|
|
||||||
#define sizelstring(l) (sizeof(TString) + ((l) + 1) * sizeof(char))
|
/*
|
||||||
|
** Size of a 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 luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
|
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
|
||||||
(sizeof(s)/sizeof(char))-1))
|
(sizeof(s)/sizeof(char))-1))
|
||||||
@@ -28,13 +32,13 @@
|
|||||||
/*
|
/*
|
||||||
** test whether a string is a reserved word
|
** test whether a string is a reserved word
|
||||||
*/
|
*/
|
||||||
#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0)
|
#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** equality for short strings, which are always internalized
|
** equality for short strings, which are always internalized
|
||||||
*/
|
*/
|
||||||
#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b))
|
#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_hash (const char *str, size_t l, unsigned int seed);
|
||||||
|
|||||||
@@ -152,8 +152,9 @@ static int str_rep (lua_State *L) {
|
|||||||
const char *s = luaL_checklstring(L, 1, &l);
|
const char *s = luaL_checklstring(L, 1, &l);
|
||||||
lua_Integer n = luaL_checkinteger(L, 2);
|
lua_Integer n = luaL_checkinteger(L, 2);
|
||||||
const char *sep = luaL_optlstring(L, 3, "", &lsep);
|
const char *sep = luaL_optlstring(L, 3, "", &lsep);
|
||||||
if (n <= 0) lua_pushliteral(L, "");
|
if (n <= 0)
|
||||||
else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
|
lua_pushliteral(L, "");
|
||||||
|
else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n))
|
||||||
return luaL_error(L, "resulting string too large");
|
return luaL_error(L, "resulting string too large");
|
||||||
else {
|
else {
|
||||||
size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
|
size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
|
||||||
@@ -181,7 +182,7 @@ static int str_byte (lua_State *L) {
|
|||||||
size_t pose = getendpos(L, 3, pi, l);
|
size_t pose = getendpos(L, 3, pi, l);
|
||||||
int n, i;
|
int n, i;
|
||||||
if (posi > pose) return 0; /* empty interval; return no values */
|
if (posi > pose) return 0; /* empty interval; return no values */
|
||||||
if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */
|
if (l_unlikely(pose - posi >= (size_t)INT_MAX)) /* arithmetic overflow? */
|
||||||
return luaL_error(L, "string slice too long");
|
return luaL_error(L, "string slice too long");
|
||||||
n = (int)(pose - posi) + 1;
|
n = (int)(pose - posi) + 1;
|
||||||
luaL_checkstack(L, n, "string slice too long");
|
luaL_checkstack(L, n, "string slice too long");
|
||||||
@@ -206,22 +207,38 @@ static int str_char (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int writer (lua_State *L, const void *b, size_t size, void *B) {
|
/*
|
||||||
(void)L;
|
** Buffer to store the result of 'string.dump'. It must be initialized
|
||||||
luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
|
** after the call to 'lua_dump', to ensure that the function is on the
|
||||||
|
** top of the stack when 'lua_dump' is called. ('luaL_buffinit' might
|
||||||
|
** push stuff.)
|
||||||
|
*/
|
||||||
|
struct str_Writer {
|
||||||
|
int init; /* true iff buffer has been initialized */
|
||||||
|
luaL_Buffer B;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static int writer (lua_State *L, const void *b, size_t size, void *ud) {
|
||||||
|
struct str_Writer *state = (struct str_Writer *)ud;
|
||||||
|
if (!state->init) {
|
||||||
|
state->init = 1;
|
||||||
|
luaL_buffinit(L, &state->B);
|
||||||
|
}
|
||||||
|
luaL_addlstring(&state->B, (const char *)b, size);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int str_dump (lua_State *L) {
|
static int str_dump (lua_State *L) {
|
||||||
luaL_Buffer b;
|
struct str_Writer state;
|
||||||
int strip = lua_toboolean(L, 2);
|
int strip = lua_toboolean(L, 2);
|
||||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||||
lua_settop(L, 1);
|
lua_settop(L, 1); /* ensure function is on the top of the stack */
|
||||||
luaL_buffinit(L,&b);
|
state.init = 0;
|
||||||
if (lua_dump(L, writer, &b, strip) != 0)
|
if (l_unlikely(lua_dump(L, writer, &state, strip) != 0))
|
||||||
return luaL_error(L, "unable to dump given function");
|
return luaL_error(L, "unable to dump given function");
|
||||||
luaL_pushresult(&b);
|
luaL_pushresult(&state.B);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,6 +250,17 @@ static int str_dump (lua_State *L) {
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#if defined(LUA_NOCVTS2N) /* { */
|
||||||
|
|
||||||
|
/* no coercion from strings to numbers */
|
||||||
|
|
||||||
|
static const luaL_Reg stringmetamethods[] = {
|
||||||
|
{"__index", NULL}, /* placeholder */
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
static int tonum (lua_State *L, int arg) {
|
static int tonum (lua_State *L, int arg) {
|
||||||
if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */
|
if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */
|
||||||
lua_pushvalue(L, arg);
|
lua_pushvalue(L, arg);
|
||||||
@@ -248,7 +276,8 @@ static int tonum (lua_State *L, int arg) {
|
|||||||
|
|
||||||
static void trymt (lua_State *L, const char *mtname) {
|
static void trymt (lua_State *L, const char *mtname) {
|
||||||
lua_settop(L, 2); /* back to the original arguments */
|
lua_settop(L, 2); /* back to the original arguments */
|
||||||
if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname))
|
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_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2,
|
||||||
luaL_typename(L, -2), luaL_typename(L, -1));
|
luaL_typename(L, -2), luaL_typename(L, -1));
|
||||||
lua_insert(L, -3); /* put metamethod before arguments */
|
lua_insert(L, -3); /* put metamethod before arguments */
|
||||||
@@ -311,6 +340,8 @@ static const luaL_Reg stringmetamethods[] = {
|
|||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -354,7 +385,8 @@ static const char *match (MatchState *ms, const char *s, const char *p);
|
|||||||
|
|
||||||
static int check_capture (MatchState *ms, int l) {
|
static int check_capture (MatchState *ms, int l) {
|
||||||
l -= '1';
|
l -= '1';
|
||||||
if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
|
if (l_unlikely(l < 0 || l >= ms->level ||
|
||||||
|
ms->capture[l].len == CAP_UNFINISHED))
|
||||||
return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
|
return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
@@ -371,14 +403,14 @@ static int capture_to_close (MatchState *ms) {
|
|||||||
static const char *classend (MatchState *ms, const char *p) {
|
static const char *classend (MatchState *ms, const char *p) {
|
||||||
switch (*p++) {
|
switch (*p++) {
|
||||||
case L_ESC: {
|
case L_ESC: {
|
||||||
if (p == ms->p_end)
|
if (l_unlikely(p == ms->p_end))
|
||||||
luaL_error(ms->L, "malformed pattern (ends with '%%')");
|
luaL_error(ms->L, "malformed pattern (ends with '%%')");
|
||||||
return p+1;
|
return p+1;
|
||||||
}
|
}
|
||||||
case '[': {
|
case '[': {
|
||||||
if (*p == '^') p++;
|
if (*p == '^') p++;
|
||||||
do { /* look for a ']' */
|
do { /* look for a ']' */
|
||||||
if (p == ms->p_end)
|
if (l_unlikely(p == ms->p_end))
|
||||||
luaL_error(ms->L, "malformed pattern (missing ']')");
|
luaL_error(ms->L, "malformed pattern (missing ']')");
|
||||||
if (*(p++) == L_ESC && p < ms->p_end)
|
if (*(p++) == L_ESC && p < ms->p_end)
|
||||||
p++; /* skip escapes (e.g. '%]') */
|
p++; /* skip escapes (e.g. '%]') */
|
||||||
@@ -453,7 +485,7 @@ static int singlematch (MatchState *ms, const char *s, const char *p,
|
|||||||
|
|
||||||
static const char *matchbalance (MatchState *ms, const char *s,
|
static const char *matchbalance (MatchState *ms, const char *s,
|
||||||
const char *p) {
|
const char *p) {
|
||||||
if (p >= ms->p_end - 1)
|
if (l_unlikely(p >= ms->p_end - 1))
|
||||||
luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
|
luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
|
||||||
if (*s != *p) return NULL;
|
if (*s != *p) return NULL;
|
||||||
else {
|
else {
|
||||||
@@ -536,9 +568,9 @@ static const char *match_capture (MatchState *ms, const char *s, int l) {
|
|||||||
|
|
||||||
|
|
||||||
static const char *match (MatchState *ms, const char *s, const char *p) {
|
static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||||
if (ms->matchdepth-- == 0)
|
if (l_unlikely(ms->matchdepth-- == 0))
|
||||||
luaL_error(ms->L, "pattern too complex");
|
luaL_error(ms->L, "pattern too complex");
|
||||||
init: /* using goto's to optimize tail recursion */
|
init: /* using goto to optimize tail recursion */
|
||||||
if (p != ms->p_end) { /* end of pattern? */
|
if (p != ms->p_end) { /* end of pattern? */
|
||||||
switch (*p) {
|
switch (*p) {
|
||||||
case '(': { /* start capture */
|
case '(': { /* start capture */
|
||||||
@@ -570,7 +602,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
|||||||
case 'f': { /* frontier? */
|
case 'f': { /* frontier? */
|
||||||
const char *ep; char previous;
|
const char *ep; char previous;
|
||||||
p += 2;
|
p += 2;
|
||||||
if (*p != '[')
|
if (l_unlikely(*p != '['))
|
||||||
luaL_error(ms->L, "missing '[' after '%%f' in pattern");
|
luaL_error(ms->L, "missing '[' after '%%f' in pattern");
|
||||||
ep = classend(ms, p); /* points to what is next */
|
ep = classend(ms, p); /* points to what is next */
|
||||||
previous = (s == ms->src_init) ? '\0' : *(s - 1);
|
previous = (s == ms->src_init) ? '\0' : *(s - 1);
|
||||||
@@ -670,7 +702,7 @@ static const char *lmemfind (const char *s1, size_t l1,
|
|||||||
static size_t get_onecapture (MatchState *ms, int i, const char *s,
|
static size_t get_onecapture (MatchState *ms, int i, const char *s,
|
||||||
const char *e, const char **cap) {
|
const char *e, const char **cap) {
|
||||||
if (i >= ms->level) {
|
if (i >= ms->level) {
|
||||||
if (i != 0)
|
if (l_unlikely(i != 0))
|
||||||
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
|
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
|
||||||
*cap = s;
|
*cap = s;
|
||||||
return e - s;
|
return e - s;
|
||||||
@@ -678,7 +710,7 @@ static size_t get_onecapture (MatchState *ms, int i, const char *s,
|
|||||||
else {
|
else {
|
||||||
ptrdiff_t capl = ms->capture[i].len;
|
ptrdiff_t capl = ms->capture[i].len;
|
||||||
*cap = ms->capture[i].init;
|
*cap = ms->capture[i].init;
|
||||||
if (capl == CAP_UNFINISHED)
|
if (l_unlikely(capl == CAP_UNFINISHED))
|
||||||
luaL_error(ms->L, "unfinished capture");
|
luaL_error(ms->L, "unfinished capture");
|
||||||
else if (capl == CAP_POSITION)
|
else if (capl == CAP_POSITION)
|
||||||
lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
|
lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
|
||||||
@@ -744,7 +776,7 @@ static int str_find_aux (lua_State *L, int find) {
|
|||||||
const char *p = luaL_checklstring(L, 2, &lp);
|
const char *p = luaL_checklstring(L, 2, &lp);
|
||||||
size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
|
size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
|
||||||
if (init > ls) { /* start after string's end? */
|
if (init > ls) { /* start after string's end? */
|
||||||
lua_pushnil(L); /* cannot find anything */
|
luaL_pushfail(L); /* cannot find anything */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* explicit request or no special characters? */
|
/* explicit request or no special characters? */
|
||||||
@@ -779,7 +811,7 @@ static int str_find_aux (lua_State *L, int find) {
|
|||||||
}
|
}
|
||||||
} while (s1++ < ms.src_end && !anchor);
|
} while (s1++ < ms.src_end && !anchor);
|
||||||
}
|
}
|
||||||
lua_pushnil(L); /* not found */
|
luaL_pushfail(L); /* not found */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -897,7 +929,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
|
|||||||
luaL_addlstring(b, s, e - s); /* keep original text */
|
luaL_addlstring(b, s, e - s); /* keep original text */
|
||||||
return 0; /* no changes */
|
return 0; /* no changes */
|
||||||
}
|
}
|
||||||
else if (!lua_isstring(L, -1))
|
else if (l_unlikely(!lua_isstring(L, -1)))
|
||||||
return luaL_error(L, "invalid replacement value (a %s)",
|
return luaL_error(L, "invalid replacement value (a %s)",
|
||||||
luaL_typename(L, -1));
|
luaL_typename(L, -1));
|
||||||
else {
|
else {
|
||||||
@@ -975,7 +1007,7 @@ static int str_gsub (lua_State *L) {
|
|||||||
** to nibble boundaries by making what is left after that first digit a
|
** to nibble boundaries by making what is left after that first digit a
|
||||||
** multiple of 4.
|
** multiple of 4.
|
||||||
*/
|
*/
|
||||||
#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1)
|
#define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1029,7 +1061,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
|
|||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
buff[i] = toupper(uchar(buff[i]));
|
buff[i] = toupper(uchar(buff[i]));
|
||||||
}
|
}
|
||||||
else if (fmt[SIZELENMOD] != 'a')
|
else if (l_unlikely(fmt[SIZELENMOD] != 'a'))
|
||||||
return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
|
return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
@@ -1043,7 +1075,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
|
|||||||
** and '\0') + number of decimal digits to represent maxfloat (which
|
** and '\0') + number of decimal digits to represent maxfloat (which
|
||||||
** is maximum exponent + 1). (99+3+1, adding some extra, 110)
|
** is maximum exponent + 1). (99+3+1, adding some extra, 110)
|
||||||
*/
|
*/
|
||||||
#define MAX_ITEMF (110 + l_mathlim(MAX_10_EXP))
|
#define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1058,10 +1090,31 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
|
|||||||
|
|
||||||
|
|
||||||
/* valid flags in a format specification */
|
/* valid flags in a format specification */
|
||||||
#define FLAGS "-+ #0"
|
#if !defined(L_FMTFLAGSF)
|
||||||
|
|
||||||
|
/* valid flags for a, A, e, E, f, F, g, and G conversions */
|
||||||
|
#define L_FMTFLAGSF "-+#0 "
|
||||||
|
|
||||||
|
/* valid flags for o, x, and X conversions */
|
||||||
|
#define L_FMTFLAGSX "-#0"
|
||||||
|
|
||||||
|
/* valid flags for d and i conversions */
|
||||||
|
#define L_FMTFLAGSI "-+0 "
|
||||||
|
|
||||||
|
/* valid flags for u conversions */
|
||||||
|
#define L_FMTFLAGSU "-0"
|
||||||
|
|
||||||
|
/* valid flags for c, p, and s conversions */
|
||||||
|
#define L_FMTFLAGSC "-"
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** maximum size of each format specification (such as "%-099.99d")
|
** Maximum size of each format specification (such as "%-099.99d"):
|
||||||
|
** Initial '%', flags (up to 5), width (2), period, precision (2),
|
||||||
|
** length modifier (8), conversion specifier, and final '\0', plus some
|
||||||
|
** extra.
|
||||||
*/
|
*/
|
||||||
#define MAX_FORMAT 32
|
#define MAX_FORMAT 32
|
||||||
|
|
||||||
@@ -1154,25 +1207,53 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
static const char *get2digits (const char *s) {
|
||||||
const char *p = strfrmt;
|
if (isdigit(uchar(*s))) {
|
||||||
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
|
s++;
|
||||||
if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
|
if (isdigit(uchar(*s))) s++; /* (2 digits at most) */
|
||||||
luaL_error(L, "invalid format (repeated flags)");
|
|
||||||
if (isdigit(uchar(*p))) p++; /* skip width */
|
|
||||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
|
||||||
if (*p == '.') {
|
|
||||||
p++;
|
|
||||||
if (isdigit(uchar(*p))) p++; /* skip precision */
|
|
||||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
|
||||||
}
|
}
|
||||||
if (isdigit(uchar(*p)))
|
return s;
|
||||||
luaL_error(L, "invalid format (width or precision too long)");
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Check whether a conversion specification is valid. When called,
|
||||||
|
** first character in 'form' must be '%' and last character must
|
||||||
|
** be a valid conversion specifier. 'flags' are the accepted flags;
|
||||||
|
** 'precision' signals whether to accept a precision.
|
||||||
|
*/
|
||||||
|
static void checkformat (lua_State *L, const char *form, const char *flags,
|
||||||
|
int precision) {
|
||||||
|
const char *spec = form + 1; /* skip '%' */
|
||||||
|
spec += strspn(spec, flags); /* skip flags */
|
||||||
|
if (*spec != '0') { /* a width cannot start with '0' */
|
||||||
|
spec = get2digits(spec); /* skip width */
|
||||||
|
if (*spec == '.' && precision) {
|
||||||
|
spec++;
|
||||||
|
spec = get2digits(spec); /* skip precision */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isalpha(uchar(*spec))) /* did not go to the end? */
|
||||||
|
luaL_error(L, "invalid conversion specification: '%s'", form);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Get a conversion specification and copy it to 'form'.
|
||||||
|
** Return the address of its last character.
|
||||||
|
*/
|
||||||
|
static const char *getformat (lua_State *L, const char *strfrmt,
|
||||||
|
char *form) {
|
||||||
|
/* spans flags, width, and precision ('0' is included as a flag) */
|
||||||
|
size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789.");
|
||||||
|
len++; /* adds following character (should be the specifier) */
|
||||||
|
/* still needs space for '%', '\0', plus a length modifier */
|
||||||
|
if (len >= MAX_FORMAT - 10)
|
||||||
|
luaL_error(L, "invalid format (too long)");
|
||||||
*(form++) = '%';
|
*(form++) = '%';
|
||||||
memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char));
|
memcpy(form, strfrmt, len * sizeof(char));
|
||||||
form += (p - strfrmt) + 1;
|
*(form + len) = '\0';
|
||||||
*form = '\0';
|
return strfrmt + len - 1;
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1195,6 +1276,7 @@ static int str_format (lua_State *L) {
|
|||||||
size_t sfl;
|
size_t sfl;
|
||||||
const char *strfrmt = luaL_checklstring(L, arg, &sfl);
|
const char *strfrmt = luaL_checklstring(L, arg, &sfl);
|
||||||
const char *strfrmt_end = strfrmt+sfl;
|
const char *strfrmt_end = strfrmt+sfl;
|
||||||
|
const char *flags;
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
while (strfrmt < strfrmt_end) {
|
while (strfrmt < strfrmt_end) {
|
||||||
@@ -1204,43 +1286,57 @@ static int str_format (lua_State *L) {
|
|||||||
luaL_addchar(&b, *strfrmt++); /* %% */
|
luaL_addchar(&b, *strfrmt++); /* %% */
|
||||||
else { /* format item */
|
else { /* format item */
|
||||||
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||||
int maxitem = MAX_ITEM;
|
int maxitem = MAX_ITEM; /* maximum length for the result */
|
||||||
char *buff = luaL_prepbuffsize(&b, maxitem); /* to put formatted item */
|
char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */
|
||||||
int nb = 0; /* number of bytes in added item */
|
int nb = 0; /* number of bytes in result */
|
||||||
if (++arg > top)
|
if (++arg > top)
|
||||||
return luaL_argerror(L, arg, "no value");
|
return luaL_argerror(L, arg, "no value");
|
||||||
strfrmt = scanformat(L, strfrmt, form);
|
strfrmt = getformat(L, strfrmt, form);
|
||||||
switch (*strfrmt++) {
|
switch (*strfrmt++) {
|
||||||
case 'c': {
|
case 'c': {
|
||||||
|
checkformat(L, form, L_FMTFLAGSC, 0);
|
||||||
nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg));
|
nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': case 'i':
|
case 'd': case 'i':
|
||||||
case 'o': case 'u': case 'x': case 'X': {
|
flags = L_FMTFLAGSI;
|
||||||
|
goto intcase;
|
||||||
|
case 'u':
|
||||||
|
flags = L_FMTFLAGSU;
|
||||||
|
goto intcase;
|
||||||
|
case 'o': case 'x': case 'X':
|
||||||
|
flags = L_FMTFLAGSX;
|
||||||
|
intcase: {
|
||||||
lua_Integer n = luaL_checkinteger(L, arg);
|
lua_Integer n = luaL_checkinteger(L, arg);
|
||||||
|
checkformat(L, form, flags, 1);
|
||||||
addlenmod(form, LUA_INTEGER_FRMLEN);
|
addlenmod(form, LUA_INTEGER_FRMLEN);
|
||||||
nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n);
|
nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'a': case 'A':
|
case 'a': case 'A':
|
||||||
|
checkformat(L, form, L_FMTFLAGSF, 1);
|
||||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
nb = lua_number2strx(L, buff, maxitem, form,
|
nb = lua_number2strx(L, buff, maxitem, form,
|
||||||
luaL_checknumber(L, arg));
|
luaL_checknumber(L, arg));
|
||||||
break;
|
break;
|
||||||
case 'e': case 'E': case 'f':
|
case 'f':
|
||||||
case 'g': case 'G': {
|
maxitem = MAX_ITEMF; /* extra space for '%f' */
|
||||||
|
buff = luaL_prepbuffsize(&b, maxitem);
|
||||||
|
/* FALLTHROUGH */
|
||||||
|
case 'e': case 'E': case 'g': case 'G': {
|
||||||
lua_Number n = luaL_checknumber(L, arg);
|
lua_Number n = luaL_checknumber(L, arg);
|
||||||
if (*(strfrmt - 1) == 'f' && l_mathop(fabs)(n) >= 1e100) {
|
checkformat(L, form, L_FMTFLAGSF, 1);
|
||||||
/* 'n' needs more than 99 digits */
|
|
||||||
maxitem = MAX_ITEMF; /* extra space for '%f' */
|
|
||||||
buff = luaL_prepbuffsize(&b, maxitem);
|
|
||||||
}
|
|
||||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
|
nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'p': {
|
case 'p': {
|
||||||
const void *p = lua_topointer(L, arg);
|
const void *p = lua_topointer(L, arg);
|
||||||
|
checkformat(L, form, L_FMTFLAGSC, 0);
|
||||||
|
if (p == NULL) { /* avoid calling 'printf' with argument NULL */
|
||||||
|
p = "(null)"; /* result */
|
||||||
|
form[strlen(form) - 1] = 's'; /* format it as a string */
|
||||||
|
}
|
||||||
nb = l_sprintf(buff, maxitem, form, p);
|
nb = l_sprintf(buff, maxitem, form, p);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1257,7 +1353,8 @@ static int str_format (lua_State *L) {
|
|||||||
luaL_addvalue(&b); /* keep entire string */
|
luaL_addvalue(&b); /* keep entire string */
|
||||||
else {
|
else {
|
||||||
luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
|
luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
|
||||||
if (!strchr(form, '.') && l >= 100) {
|
checkformat(L, form, L_FMTFLAGSC, 1);
|
||||||
|
if (strchr(form, '.') == NULL && l >= 100) {
|
||||||
/* no precision and string is too long to be formatted */
|
/* no precision and string is too long to be formatted */
|
||||||
luaL_addvalue(&b); /* keep entire string */
|
luaL_addvalue(&b); /* keep entire string */
|
||||||
}
|
}
|
||||||
@@ -1315,26 +1412,6 @@ static const union {
|
|||||||
} nativeendian = {1};
|
} nativeendian = {1};
|
||||||
|
|
||||||
|
|
||||||
/* dummy structure to get native alignment requirements */
|
|
||||||
struct cD {
|
|
||||||
char c;
|
|
||||||
union { double d; void *p; lua_Integer i; lua_Number n; } u;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define MAXALIGN (offsetof(struct cD, u))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Union for serializing floats
|
|
||||||
*/
|
|
||||||
typedef union Ftypes {
|
|
||||||
float f;
|
|
||||||
double d;
|
|
||||||
lua_Number n;
|
|
||||||
char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
|
|
||||||
} Ftypes;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** information to pack/unpack stuff
|
** information to pack/unpack stuff
|
||||||
*/
|
*/
|
||||||
@@ -1351,7 +1428,9 @@ typedef struct Header {
|
|||||||
typedef enum KOption {
|
typedef enum KOption {
|
||||||
Kint, /* signed integers */
|
Kint, /* signed integers */
|
||||||
Kuint, /* unsigned integers */
|
Kuint, /* unsigned integers */
|
||||||
Kfloat, /* floating-point numbers */
|
Kfloat, /* single-precision floating-point numbers */
|
||||||
|
Knumber, /* Lua "native" floating-point numbers */
|
||||||
|
Kdouble, /* double-precision floating-point numbers */
|
||||||
Kchar, /* fixed-length strings */
|
Kchar, /* fixed-length strings */
|
||||||
Kstring, /* strings with prefixed length */
|
Kstring, /* strings with prefixed length */
|
||||||
Kzstr, /* zero-terminated strings */
|
Kzstr, /* zero-terminated strings */
|
||||||
@@ -1386,7 +1465,7 @@ static int getnum (const char **fmt, int df) {
|
|||||||
*/
|
*/
|
||||||
static int getnumlimit (Header *h, const char **fmt, int df) {
|
static int getnumlimit (Header *h, const char **fmt, int df) {
|
||||||
int sz = getnum(fmt, df);
|
int sz = getnum(fmt, df);
|
||||||
if (sz > MAXINTSIZE || sz <= 0)
|
if (l_unlikely(sz > MAXINTSIZE || sz <= 0))
|
||||||
return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
|
return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
|
||||||
sz, MAXINTSIZE);
|
sz, MAXINTSIZE);
|
||||||
return sz;
|
return sz;
|
||||||
@@ -1407,6 +1486,8 @@ static void initheader (lua_State *L, Header *h) {
|
|||||||
** Read and classify next option. 'size' is filled with option's size.
|
** 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, int *size) {
|
||||||
|
/* dummy structure to get native alignment requirements */
|
||||||
|
struct cD { char c; union { LUAI_MAXALIGN; } u; };
|
||||||
int opt = *((*fmt)++);
|
int opt = *((*fmt)++);
|
||||||
*size = 0; /* default */
|
*size = 0; /* default */
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
@@ -1420,14 +1501,14 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
|||||||
case 'J': *size = sizeof(lua_Integer); return Kuint;
|
case 'J': *size = sizeof(lua_Integer); return Kuint;
|
||||||
case 'T': *size = sizeof(size_t); return Kuint;
|
case 'T': *size = sizeof(size_t); return Kuint;
|
||||||
case 'f': *size = sizeof(float); return Kfloat;
|
case 'f': *size = sizeof(float); return Kfloat;
|
||||||
case 'd': *size = sizeof(double); return Kfloat;
|
case 'n': *size = sizeof(lua_Number); return Knumber;
|
||||||
case 'n': *size = sizeof(lua_Number); return Kfloat;
|
case 'd': *size = sizeof(double); return Kdouble;
|
||||||
case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
|
case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
|
||||||
case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
|
case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
|
||||||
case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
|
case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
|
||||||
case 'c':
|
case 'c':
|
||||||
*size = getnum(fmt, -1);
|
*size = getnum(fmt, -1);
|
||||||
if (*size == -1)
|
if (l_unlikely(*size == -1))
|
||||||
luaL_error(h->L, "missing size for format option 'c'");
|
luaL_error(h->L, "missing size for format option 'c'");
|
||||||
return Kchar;
|
return Kchar;
|
||||||
case 'z': return Kzstr;
|
case 'z': return Kzstr;
|
||||||
@@ -1437,7 +1518,11 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
|||||||
case '<': h->islittle = 1; break;
|
case '<': h->islittle = 1; break;
|
||||||
case '>': h->islittle = 0; break;
|
case '>': h->islittle = 0; break;
|
||||||
case '=': h->islittle = nativeendian.little; break;
|
case '=': h->islittle = nativeendian.little; break;
|
||||||
case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break;
|
case '!': {
|
||||||
|
const int maxalign = offsetof(struct cD, u);
|
||||||
|
h->maxalign = getnumlimit(h, fmt, maxalign);
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: luaL_error(h->L, "invalid format option '%c'", opt);
|
default: luaL_error(h->L, "invalid format option '%c'", opt);
|
||||||
}
|
}
|
||||||
return Knop;
|
return Knop;
|
||||||
@@ -1466,7 +1551,7 @@ static KOption getdetails (Header *h, size_t totalsize,
|
|||||||
else {
|
else {
|
||||||
if (align > h->maxalign) /* enforce maximum alignment */
|
if (align > h->maxalign) /* enforce maximum alignment */
|
||||||
align = h->maxalign;
|
align = h->maxalign;
|
||||||
if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */
|
if (l_unlikely((align & (align - 1)) != 0)) /* not a power of 2? */
|
||||||
luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
|
luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
|
||||||
*ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
|
*ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
|
||||||
}
|
}
|
||||||
@@ -1501,12 +1586,10 @@ static void packint (luaL_Buffer *b, lua_Unsigned n,
|
|||||||
** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
|
** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
|
||||||
** given 'islittle' is different from native endianness.
|
** given 'islittle' is different from native endianness.
|
||||||
*/
|
*/
|
||||||
static void copywithendian (volatile char *dest, volatile const char *src,
|
static void copywithendian (char *dest, const char *src,
|
||||||
int size, int islittle) {
|
int size, int islittle) {
|
||||||
if (islittle == nativeendian.little) {
|
if (islittle == nativeendian.little)
|
||||||
while (size-- != 0)
|
memcpy(dest, src, size);
|
||||||
*(dest++) = *(src++);
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
dest += size - 1;
|
dest += size - 1;
|
||||||
while (size-- != 0)
|
while (size-- != 0)
|
||||||
@@ -1549,15 +1632,27 @@ static int str_pack (lua_State *L) {
|
|||||||
packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
|
packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kfloat: { /* floating-point options */
|
case Kfloat: { /* C float */
|
||||||
volatile Ftypes u;
|
float f = (float)luaL_checknumber(L, arg); /* get argument */
|
||||||
char *buff = luaL_prepbuffsize(&b, size);
|
char *buff = luaL_prepbuffsize(&b, sizeof(f));
|
||||||
lua_Number n = luaL_checknumber(L, arg); /* get argument */
|
/* move 'f' to final result, correcting endianness if needed */
|
||||||
if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
|
copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
|
||||||
else if (size == sizeof(u.d)) u.d = (double)n;
|
luaL_addsize(&b, size);
|
||||||
else u.n = n;
|
break;
|
||||||
/* move 'u' to final result, correcting endianness if needed */
|
}
|
||||||
copywithendian(buff, u.buff, size, h.islittle);
|
case Knumber: { /* Lua float */
|
||||||
|
lua_Number f = luaL_checknumber(L, arg); /* get argument */
|
||||||
|
char *buff = luaL_prepbuffsize(&b, sizeof(f));
|
||||||
|
/* move 'f' to final result, correcting endianness if needed */
|
||||||
|
copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
|
||||||
|
luaL_addsize(&b, size);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kdouble: { /* C double */
|
||||||
|
double f = (double)luaL_checknumber(L, arg); /* get argument */
|
||||||
|
char *buff = luaL_prepbuffsize(&b, sizeof(f));
|
||||||
|
/* move 'f' to final result, correcting endianness if needed */
|
||||||
|
copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
|
||||||
luaL_addsize(&b, size);
|
luaL_addsize(&b, size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1648,7 +1743,7 @@ static lua_Integer unpackint (lua_State *L, const char *str,
|
|||||||
else if (size > SZINT) { /* must check unread bytes */
|
else if (size > SZINT) { /* must check unread bytes */
|
||||||
int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
|
int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
|
||||||
for (i = limit; i < size; i++) {
|
for (i = limit; i < size; i++) {
|
||||||
if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
|
if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask))
|
||||||
luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
|
luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1683,13 +1778,21 @@ static int str_unpack (lua_State *L) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kfloat: {
|
case Kfloat: {
|
||||||
volatile Ftypes u;
|
float f;
|
||||||
lua_Number num;
|
copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
|
||||||
copywithendian(u.buff, data + pos, size, h.islittle);
|
lua_pushnumber(L, (lua_Number)f);
|
||||||
if (size == sizeof(u.f)) num = (lua_Number)u.f;
|
break;
|
||||||
else if (size == sizeof(u.d)) num = (lua_Number)u.d;
|
}
|
||||||
else num = u.n;
|
case Knumber: {
|
||||||
lua_pushnumber(L, num);
|
lua_Number f;
|
||||||
|
copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
|
||||||
|
lua_pushnumber(L, f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Kdouble: {
|
||||||
|
double f;
|
||||||
|
copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
|
||||||
|
lua_pushnumber(L, (lua_Number)f);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kchar: {
|
case Kchar: {
|
||||||
@@ -1704,7 +1807,7 @@ static int str_unpack (lua_State *L) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kzstr: {
|
case Kzstr: {
|
||||||
size_t len = (int)strlen(data + pos);
|
size_t len = strlen(data + pos);
|
||||||
luaL_argcheck(L, pos + len < ld, 2,
|
luaL_argcheck(L, pos + len < ld, 2,
|
||||||
"unfinished string for format 'z'");
|
"unfinished string for format 'z'");
|
||||||
lua_pushlstring(L, data + pos, len);
|
lua_pushlstring(L, data + pos, len);
|
||||||
|
|||||||
@@ -68,18 +68,21 @@
|
|||||||
#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
|
#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** When the original hash value is good, hashing by a power of 2
|
||||||
|
** avoids the cost of '%'.
|
||||||
|
*/
|
||||||
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
|
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** for other types, it is better to avoid modulo by power of 2, as
|
||||||
|
** they can have many 2 factors.
|
||||||
|
*/
|
||||||
|
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
|
||||||
|
|
||||||
|
|
||||||
#define hashstr(t,str) hashpow2(t, (str)->hash)
|
#define hashstr(t,str) hashpow2(t, (str)->hash)
|
||||||
#define hashboolean(t,p) hashpow2(t, p)
|
#define hashboolean(t,p) hashpow2(t, p)
|
||||||
#define hashint(t,i) hashpow2(t, i)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** for some types, it is better to avoid modulus by power of 2, as
|
|
||||||
** they tend to have many 2 factors.
|
|
||||||
*/
|
|
||||||
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
|
|
||||||
|
|
||||||
|
|
||||||
#define hashpointer(t,p) hashmod(t, point2uint(p))
|
#define hashpointer(t,p) hashmod(t, point2uint(p))
|
||||||
@@ -88,14 +91,28 @@
|
|||||||
#define dummynode (&dummynode_)
|
#define dummynode (&dummynode_)
|
||||||
|
|
||||||
static const Node dummynode_ = {
|
static const Node dummynode_ = {
|
||||||
{{NULL}, LUA_TEMPTY, /* value's value and type */
|
{{NULL}, LUA_VEMPTY, /* value's value and type */
|
||||||
LUA_TNIL, 0, {NULL}} /* key type, next, and key value */
|
LUA_VNIL, 0, {NULL}} /* key type, next, and key value */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static const TValue absentkey = {ABSTKEYCONSTANT};
|
static const TValue absentkey = {ABSTKEYCONSTANT};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Hash for integers. To allow a good hash, use the remainder operator
|
||||||
|
** ('%'). If integer fits as a non-negative int, compute an int
|
||||||
|
** remainder, which is faster. Otherwise, use an unsigned-integer
|
||||||
|
** remainder, which uses all bits and ensures a non-negative result.
|
||||||
|
*/
|
||||||
|
static Node *hashint (const Table *t, lua_Integer i) {
|
||||||
|
lua_Unsigned ui = l_castS2U(i);
|
||||||
|
if (ui <= cast_uint(INT_MAX))
|
||||||
|
return hashmod(t, cast_int(ui));
|
||||||
|
else
|
||||||
|
return hashmod(t, ui);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Hash for floating-point numbers.
|
** Hash for floating-point numbers.
|
||||||
@@ -129,62 +146,89 @@ static int l_hashfloat (lua_Number n) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** returns the 'main' position of an element in a table (that is,
|
** returns the 'main' position of an element in a table (that is,
|
||||||
** the index of its hash value). The key comes broken (tag in 'ktt'
|
** the index of its hash value).
|
||||||
** and value in 'vkl') so that we can call it on keys inserted into
|
|
||||||
** nodes.
|
|
||||||
*/
|
*/
|
||||||
static Node *mainposition (const Table *t, int ktt, const Value *kvl) {
|
static Node *mainpositionTV (const Table *t, const TValue *key) {
|
||||||
switch (withvariant(ktt)) {
|
switch (ttypetag(key)) {
|
||||||
case LUA_TNUMINT:
|
case LUA_VNUMINT: {
|
||||||
return hashint(t, ivalueraw(*kvl));
|
lua_Integer i = ivalue(key);
|
||||||
case LUA_TNUMFLT:
|
return hashint(t, i);
|
||||||
return hashmod(t, l_hashfloat(fltvalueraw(*kvl)));
|
}
|
||||||
case LUA_TSHRSTR:
|
case LUA_VNUMFLT: {
|
||||||
return hashstr(t, tsvalueraw(*kvl));
|
lua_Number n = fltvalue(key);
|
||||||
case LUA_TLNGSTR:
|
return hashmod(t, l_hashfloat(n));
|
||||||
return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl)));
|
}
|
||||||
case LUA_TBOOLEAN:
|
case LUA_VSHRSTR: {
|
||||||
return hashboolean(t, bvalueraw(*kvl));
|
TString *ts = tsvalue(key);
|
||||||
case LUA_TLIGHTUSERDATA:
|
return hashstr(t, ts);
|
||||||
return hashpointer(t, pvalueraw(*kvl));
|
}
|
||||||
case LUA_TLCF:
|
case LUA_VLNGSTR: {
|
||||||
return hashpointer(t, fvalueraw(*kvl));
|
TString *ts = tsvalue(key);
|
||||||
default:
|
return hashpow2(t, luaS_hashlongstr(ts));
|
||||||
return hashpointer(t, gcvalueraw(*kvl));
|
}
|
||||||
|
case LUA_VFALSE:
|
||||||
|
return hashboolean(t, 0);
|
||||||
|
case LUA_VTRUE:
|
||||||
|
return hashboolean(t, 1);
|
||||||
|
case LUA_VLIGHTUSERDATA: {
|
||||||
|
void *p = pvalue(key);
|
||||||
|
return hashpointer(t, p);
|
||||||
|
}
|
||||||
|
case LUA_VLCF: {
|
||||||
|
lua_CFunction f = fvalue(key);
|
||||||
|
return hashpointer(t, f);
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
GCObject *o = gcvalue(key);
|
||||||
|
return hashpointer(t, o);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static Node *mainpositionTV (const Table *t, const TValue *key) {
|
l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) {
|
||||||
return mainposition(t, rawtt(key), valraw(key));
|
TValue key;
|
||||||
|
getnodekey(cast(lua_State *, NULL), &key, nd);
|
||||||
|
return mainpositionTV(t, &key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check whether key 'k1' is equal to the key in node 'n2'.
|
** Check whether key 'k1' is equal to the key in node 'n2'. This
|
||||||
** This equality is raw, so there are no metamethods. Floats
|
** equality is raw, so there are no metamethods. Floats with integer
|
||||||
** with integer values have been normalized, so integers cannot
|
** values have been normalized, so integers cannot be equal to
|
||||||
** be equal to floats. It is assumed that 'eqshrstr' is simply
|
** floats. It is assumed that 'eqshrstr' is simply pointer equality, so
|
||||||
** pointer equality, so that short strings are handled in the
|
** that short strings are handled in the default case.
|
||||||
** default case.
|
** A true 'deadok' means to accept dead keys as equal to their original
|
||||||
|
** values. All dead keys are compared in the default case, by pointer
|
||||||
|
** identity. (Only collectable objects can produce dead keys.) Note that
|
||||||
|
** dead long strings are also compared by identity.
|
||||||
|
** Once a key is dead, its corresponding value may be collected, and
|
||||||
|
** then another value can be created with the same address. If this
|
||||||
|
** other value is given to 'next', 'equalkey' will signal a false
|
||||||
|
** positive. In a regular traversal, this situation should never happen,
|
||||||
|
** as all keys given to 'next' came from the table itself, and therefore
|
||||||
|
** could not have been collected. Outside a regular traversal, we
|
||||||
|
** have garbage in, garbage out. What is relevant is that this false
|
||||||
|
** positive does not break anything. (In particular, 'next' will return
|
||||||
|
** some other valid item on the table or nil.)
|
||||||
*/
|
*/
|
||||||
static int equalkey (const TValue *k1, const Node *n2) {
|
static int equalkey (const TValue *k1, const Node *n2, int deadok) {
|
||||||
if (rawtt(k1) != keytt(n2)) /* not the same variants? */
|
if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */
|
||||||
|
!(deadok && keyisdead(n2) && iscollectable(k1)))
|
||||||
return 0; /* cannot be same key */
|
return 0; /* cannot be same key */
|
||||||
switch (ttypetag(k1)) {
|
switch (keytt(n2)) {
|
||||||
case LUA_TNIL:
|
case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
|
||||||
return 1;
|
return 1;
|
||||||
case LUA_TNUMINT:
|
case LUA_VNUMINT:
|
||||||
return (ivalue(k1) == keyival(n2));
|
return (ivalue(k1) == keyival(n2));
|
||||||
case LUA_TNUMFLT:
|
case LUA_VNUMFLT:
|
||||||
return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
|
return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
|
||||||
case LUA_TBOOLEAN:
|
case LUA_VLIGHTUSERDATA:
|
||||||
return bvalue(k1) == bvalueraw(keyval(n2));
|
|
||||||
case LUA_TLIGHTUSERDATA:
|
|
||||||
return pvalue(k1) == pvalueraw(keyval(n2));
|
return pvalue(k1) == pvalueraw(keyval(n2));
|
||||||
case LUA_TLCF:
|
case LUA_VLCF:
|
||||||
return fvalue(k1) == fvalueraw(keyval(n2));
|
return fvalue(k1) == fvalueraw(keyval(n2));
|
||||||
case LUA_TLNGSTR:
|
case ctb(LUA_VLNGSTR):
|
||||||
return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
|
return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
|
||||||
default:
|
default:
|
||||||
return gcvalue(k1) == gcvalueraw(keyval(n2));
|
return gcvalue(k1) == gcvalueraw(keyval(n2));
|
||||||
@@ -208,14 +252,16 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t) {
|
|||||||
return t->alimit; /* this is the size */
|
return t->alimit; /* this is the size */
|
||||||
else {
|
else {
|
||||||
unsigned int size = t->alimit;
|
unsigned int size = t->alimit;
|
||||||
/* compute the smallest power of 2 not smaller than 'n' */
|
/* compute the smallest power of 2 not smaller than 'size' */
|
||||||
size |= (size >> 1);
|
size |= (size >> 1);
|
||||||
size |= (size >> 2);
|
size |= (size >> 2);
|
||||||
size |= (size >> 4);
|
size |= (size >> 4);
|
||||||
size |= (size >> 8);
|
size |= (size >> 8);
|
||||||
|
#if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */
|
||||||
size |= (size >> 16);
|
size |= (size >> 16);
|
||||||
#if (UINT_MAX >> 30) > 3
|
#if (UINT_MAX >> 30) > 3
|
||||||
size |= (size >> 32); /* unsigned int has more than 32 bits */
|
size |= (size >> 32); /* unsigned int has more than 32 bits */
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
size++;
|
size++;
|
||||||
lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
|
lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
|
||||||
@@ -248,11 +294,12 @@ static unsigned int setlimittosize (Table *t) {
|
|||||||
/*
|
/*
|
||||||
** "Generic" get version. (Not that generic: not valid for integers,
|
** "Generic" get version. (Not that generic: not valid for integers,
|
||||||
** which may be in array part, nor for floats with integral values.)
|
** which may be in array part, nor for floats with integral values.)
|
||||||
|
** See explanation about 'deadok' in function 'equalkey'.
|
||||||
*/
|
*/
|
||||||
static const TValue *getgeneric (Table *t, const TValue *key) {
|
static const TValue *getgeneric (Table *t, const TValue *key, int deadok) {
|
||||||
Node *n = mainpositionTV(t, key);
|
Node *n = mainpositionTV(t, key);
|
||||||
for (;;) { /* check whether 'key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
if (equalkey(key, n))
|
if (equalkey(key, n, deadok))
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else {
|
else {
|
||||||
int nx = gnext(n);
|
int nx = gnext(n);
|
||||||
@@ -289,8 +336,8 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key,
|
|||||||
if (i - 1u < asize) /* is 'key' inside array part? */
|
if (i - 1u < asize) /* is 'key' inside array part? */
|
||||||
return i; /* yes; that's the index */
|
return i; /* yes; that's the index */
|
||||||
else {
|
else {
|
||||||
const TValue *n = getgeneric(t, key);
|
const TValue *n = getgeneric(t, key, 1);
|
||||||
if (unlikely(isabstkey(n)))
|
if (l_unlikely(isabstkey(n)))
|
||||||
luaG_runerror(L, "invalid key to 'next'"); /* key not found */
|
luaG_runerror(L, "invalid key to 'next'"); /* key not found */
|
||||||
i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
|
i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
|
||||||
/* hash elements are numbered after array ones */
|
/* hash elements are numbered after array ones */
|
||||||
@@ -443,7 +490,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) {
|
|||||||
luaG_runerror(L, "table overflow");
|
luaG_runerror(L, "table overflow");
|
||||||
size = twoto(lsize);
|
size = twoto(lsize);
|
||||||
t->node = luaM_newvector(L, size, Node);
|
t->node = luaM_newvector(L, size, Node);
|
||||||
for (i = 0; i < (int)size; i++) {
|
for (i = 0; i < cast_int(size); i++) {
|
||||||
Node *n = gnode(t, i);
|
Node *n = gnode(t, i);
|
||||||
gnext(n) = 0;
|
gnext(n) = 0;
|
||||||
setnilkey(n);
|
setnilkey(n);
|
||||||
@@ -468,7 +515,7 @@ static void reinsert (lua_State *L, Table *ot, Table *t) {
|
|||||||
already present in the table */
|
already present in the table */
|
||||||
TValue k;
|
TValue k;
|
||||||
getnodekey(L, &k, old);
|
getnodekey(L, &k, old);
|
||||||
setobjt2t(L, luaH_set(L, t, &k), gval(old));
|
luaH_set(L, t, &k, gval(old));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -524,7 +571,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
|
|||||||
}
|
}
|
||||||
/* allocate new array */
|
/* allocate new array */
|
||||||
newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue);
|
newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue);
|
||||||
if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
|
if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
|
||||||
freehash(L, &newt); /* release new hash part */
|
freehash(L, &newt); /* release new hash part */
|
||||||
luaM_error(L); /* raise error (with array unchanged) */
|
luaM_error(L); /* raise error (with array unchanged) */
|
||||||
}
|
}
|
||||||
@@ -577,10 +624,10 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
|||||||
|
|
||||||
|
|
||||||
Table *luaH_new (lua_State *L) {
|
Table *luaH_new (lua_State *L) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table));
|
GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
|
||||||
Table *t = gco2t(o);
|
Table *t = gco2t(o);
|
||||||
t->metatable = NULL;
|
t->metatable = NULL;
|
||||||
t->flags = cast_byte(~0);
|
t->flags = cast_byte(maskflags); /* table has no metamethod fields */
|
||||||
t->array = NULL;
|
t->array = NULL;
|
||||||
t->alimit = 0;
|
t->alimit = 0;
|
||||||
setnodevector(L, t, 0);
|
setnodevector(L, t, 0);
|
||||||
@@ -615,21 +662,24 @@ static Node *getfreepos (Table *t) {
|
|||||||
** put new key in its main position; otherwise (colliding node is in its main
|
** put new key in its main position; otherwise (colliding node is in its main
|
||||||
** position), new key goes to an empty position.
|
** position), new key goes to an empty position.
|
||||||
*/
|
*/
|
||||||
TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
|
||||||
|
TValue *value) {
|
||||||
Node *mp;
|
Node *mp;
|
||||||
TValue aux;
|
TValue aux;
|
||||||
if (unlikely(ttisnil(key)))
|
if (l_unlikely(ttisnil(key)))
|
||||||
luaG_runerror(L, "table index is nil");
|
luaG_runerror(L, "table index is nil");
|
||||||
else if (ttisfloat(key)) {
|
else if (ttisfloat(key)) {
|
||||||
lua_Number f = fltvalue(key);
|
lua_Number f = fltvalue(key);
|
||||||
lua_Integer k;
|
lua_Integer k;
|
||||||
if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */
|
if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
|
||||||
setivalue(&aux, k);
|
setivalue(&aux, k);
|
||||||
key = &aux; /* insert it as an integer */
|
key = &aux; /* insert it as an integer */
|
||||||
}
|
}
|
||||||
else if (unlikely(luai_numisnan(f)))
|
else if (l_unlikely(luai_numisnan(f)))
|
||||||
luaG_runerror(L, "table index is NaN");
|
luaG_runerror(L, "table index is NaN");
|
||||||
}
|
}
|
||||||
|
if (ttisnil(value))
|
||||||
|
return; /* do not insert nil values */
|
||||||
mp = mainpositionTV(t, key);
|
mp = mainpositionTV(t, key);
|
||||||
if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */
|
if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */
|
||||||
Node *othern;
|
Node *othern;
|
||||||
@@ -637,10 +687,11 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
if (f == NULL) { /* cannot find a free place? */
|
if (f == NULL) { /* cannot find a free place? */
|
||||||
rehash(L, t, key); /* grow table */
|
rehash(L, t, key); /* grow table */
|
||||||
/* whatever called 'newkey' takes care of TM cache */
|
/* whatever called 'newkey' takes care of TM cache */
|
||||||
return luaH_set(L, t, key); /* insert key into grown table */
|
luaH_set(L, t, key, value); /* insert key into grown table */
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
lua_assert(!isdummy(t));
|
lua_assert(!isdummy(t));
|
||||||
othern = mainposition(t, keytt(mp), &keyval(mp));
|
othern = mainpositionfromnode(t, mp);
|
||||||
if (othern != mp) { /* is colliding node out of its main position? */
|
if (othern != mp) { /* is colliding node out of its main position? */
|
||||||
/* yes; move colliding node into free position */
|
/* yes; move colliding node into free position */
|
||||||
while (othern + gnext(othern) != mp) /* find previous */
|
while (othern + gnext(othern) != mp) /* find previous */
|
||||||
@@ -665,28 +716,42 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
setnodekey(L, mp, key);
|
setnodekey(L, mp, key);
|
||||||
luaC_barrierback(L, obj2gco(t), key);
|
luaC_barrierback(L, obj2gco(t), key);
|
||||||
lua_assert(isempty(gval(mp)));
|
lua_assert(isempty(gval(mp)));
|
||||||
return gval(mp);
|
setobj2t(L, gval(mp), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Search function for integers. If integer is inside 'alimit', get it
|
** Search function for integers. If integer is inside 'alimit', get it
|
||||||
** directly from the array part. Otherwise, if 'alimit' is not equal to
|
** directly from the array part. Otherwise, if 'alimit' is not
|
||||||
** the real size of the array, key still can be in the array part. In
|
** the real size of the array, the key still can be in the array part.
|
||||||
** this case, try to avoid a call to 'luaH_realasize' when key is just
|
** In this case, do the "Xmilia trick" to check whether 'key-1' is
|
||||||
** one more than the limit (so that it can be incremented without
|
** smaller than the real size.
|
||||||
** changing the real size of the array).
|
** The trick works as follow: let 'p' be an integer such that
|
||||||
|
** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'.
|
||||||
|
** That is, 2^(p+1) is the real size of the array, and 'p' is the highest
|
||||||
|
** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'.
|
||||||
|
** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will
|
||||||
|
** have the 'p' bit cleared. If the key is outside the array, that is,
|
||||||
|
** 'key-1 >= 2^(p+1)', then 'res' will have some bit on higher than 'p',
|
||||||
|
** therefore it will be larger or equal to 'alimit', and the check
|
||||||
|
** will fail. If 'key-1 < 2^(p+1)', then 'res' has no bit on higher than
|
||||||
|
** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller
|
||||||
|
** than 2^p, therefore smaller than 'alimit', and the check succeeds.
|
||||||
|
** As special cases, when 'alimit' is 0 the condition is trivially false,
|
||||||
|
** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'.
|
||||||
|
** If key is 0 or negative, 'res' will have its higher bit on, so that
|
||||||
|
** if cannot be smaller than alimit.
|
||||||
*/
|
*/
|
||||||
const TValue *luaH_getint (Table *t, lua_Integer key) {
|
const TValue *luaH_getint (Table *t, lua_Integer key) {
|
||||||
if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */
|
lua_Unsigned alimit = t->alimit;
|
||||||
|
if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */
|
||||||
return &t->array[key - 1];
|
return &t->array[key - 1];
|
||||||
else if (!limitequalsasize(t) && /* key still may be in the array part? */
|
else if (!isrealasize(t) && /* key still may be in the array part? */
|
||||||
(l_castS2U(key) == t->alimit + 1 ||
|
(((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) {
|
||||||
l_castS2U(key) - 1u < luaH_realasize(t))) {
|
|
||||||
t->alimit = cast_uint(key); /* probably '#t' is here now */
|
t->alimit = cast_uint(key); /* probably '#t' is here now */
|
||||||
return &t->array[key - 1];
|
return &t->array[key - 1];
|
||||||
}
|
}
|
||||||
else {
|
else { /* key is not in the array part; check the hash */
|
||||||
Node *n = hashint(t, key);
|
Node *n = hashint(t, key);
|
||||||
for (;;) { /* check whether 'key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
if (keyisinteger(n) && keyival(n) == key)
|
if (keyisinteger(n) && keyival(n) == key)
|
||||||
@@ -707,7 +772,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
|
|||||||
*/
|
*/
|
||||||
const TValue *luaH_getshortstr (Table *t, TString *key) {
|
const TValue *luaH_getshortstr (Table *t, TString *key) {
|
||||||
Node *n = hashstr(t, key);
|
Node *n = hashstr(t, key);
|
||||||
lua_assert(key->tt == LUA_TSHRSTR);
|
lua_assert(key->tt == LUA_VSHRSTR);
|
||||||
for (;;) { /* check whether 'key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
|
if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
@@ -722,12 +787,12 @@ const TValue *luaH_getshortstr (Table *t, TString *key) {
|
|||||||
|
|
||||||
|
|
||||||
const TValue *luaH_getstr (Table *t, TString *key) {
|
const TValue *luaH_getstr (Table *t, TString *key) {
|
||||||
if (key->tt == LUA_TSHRSTR)
|
if (key->tt == LUA_VSHRSTR)
|
||||||
return luaH_getshortstr(t, key);
|
return luaH_getshortstr(t, key);
|
||||||
else { /* for long strings, use generic case */
|
else { /* for long strings, use generic case */
|
||||||
TValue ko;
|
TValue ko;
|
||||||
setsvalue(cast(lua_State *, NULL), &ko, key);
|
setsvalue(cast(lua_State *, NULL), &ko, key);
|
||||||
return getgeneric(t, &ko);
|
return getgeneric(t, &ko, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -737,44 +802,55 @@ const TValue *luaH_getstr (Table *t, TString *key) {
|
|||||||
*/
|
*/
|
||||||
const TValue *luaH_get (Table *t, const TValue *key) {
|
const TValue *luaH_get (Table *t, const TValue *key) {
|
||||||
switch (ttypetag(key)) {
|
switch (ttypetag(key)) {
|
||||||
case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
|
case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key));
|
||||||
case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
|
case LUA_VNUMINT: return luaH_getint(t, ivalue(key));
|
||||||
case LUA_TNIL: return &absentkey;
|
case LUA_VNIL: return &absentkey;
|
||||||
case LUA_TNUMFLT: {
|
case LUA_VNUMFLT: {
|
||||||
lua_Integer k;
|
lua_Integer k;
|
||||||
if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */
|
if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
|
||||||
return luaH_getint(t, k); /* use specialized version */
|
return luaH_getint(t, k); /* use specialized version */
|
||||||
/* else... */
|
/* else... */
|
||||||
} /* FALLTHROUGH */
|
} /* FALLTHROUGH */
|
||||||
default:
|
default:
|
||||||
return getgeneric(t, key);
|
return getgeneric(t, key, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Finish a raw "set table" operation, where 'slot' is where the value
|
||||||
|
** should have been (the result of a previous "get table").
|
||||||
|
** Beware: when using this function you probably need to check a GC
|
||||||
|
** barrier and invalidate the TM cache.
|
||||||
|
*/
|
||||||
|
void luaH_finishset (lua_State *L, Table *t, const TValue *key,
|
||||||
|
const TValue *slot, TValue *value) {
|
||||||
|
if (isabstkey(slot))
|
||||||
|
luaH_newkey(L, t, key, value);
|
||||||
|
else
|
||||||
|
setobj2t(L, cast(TValue *, slot), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** beware: when using this function you probably need to check a GC
|
** beware: when using this function you probably need to check a GC
|
||||||
** barrier and invalidate the TM cache.
|
** barrier and invalidate the TM cache.
|
||||||
*/
|
*/
|
||||||
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
|
void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) {
|
||||||
const TValue *p = luaH_get(t, key);
|
const TValue *slot = luaH_get(t, key);
|
||||||
if (!isabstkey(p))
|
luaH_finishset(L, t, key, slot, value);
|
||||||
return cast(TValue *, p);
|
|
||||||
else return luaH_newkey(L, t, key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
|
void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
|
||||||
const TValue *p = luaH_getint(t, key);
|
const TValue *p = luaH_getint(t, key);
|
||||||
TValue *cell;
|
if (isabstkey(p)) {
|
||||||
if (!isabstkey(p))
|
|
||||||
cell = cast(TValue *, p);
|
|
||||||
else {
|
|
||||||
TValue k;
|
TValue k;
|
||||||
setivalue(&k, key);
|
setivalue(&k, key);
|
||||||
cell = luaH_newkey(L, t, &k);
|
luaH_newkey(L, t, &k, value);
|
||||||
}
|
}
|
||||||
setobj2t(L, cell, value);
|
else
|
||||||
|
setobj2t(L, cast(TValue *, p), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -833,39 +909,41 @@ static unsigned int binsearch (const TValue *array, unsigned int i,
|
|||||||
** and 'maxinteger' if t[maxinteger] is present.)
|
** and 'maxinteger' if t[maxinteger] is present.)
|
||||||
** (In the next explanation, we use Lua indices, that is, with base 1.
|
** (In the next explanation, we use Lua indices, that is, with base 1.
|
||||||
** The code itself uses base 0 when indexing the array part of the table.)
|
** The code itself uses base 0 when indexing the array part of the table.)
|
||||||
** The code starts with 'limit', a position in the array part that may
|
** The code starts with 'limit = t->alimit', a position in the array
|
||||||
** be a boundary.
|
** part that may be a boundary.
|
||||||
|
**
|
||||||
** (1) If 't[limit]' is empty, there must be a boundary before it.
|
** (1) If 't[limit]' is empty, there must be a boundary before it.
|
||||||
** As a common case (e.g., after 't[#t]=nil'), check whether 'hint-1'
|
** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1'
|
||||||
** is present. If so, it is a boundary. Otherwise, do a binary search
|
** is present. If so, it is a boundary. Otherwise, do a binary search
|
||||||
** between 0 and limit to find a boundary. In both cases, try to
|
** between 0 and limit to find a boundary. In both cases, try to
|
||||||
** use this boundary as the new 'limit', as a hint for the next call.
|
** use this boundary as the new 'alimit', as a hint for the next call.
|
||||||
|
**
|
||||||
** (2) If 't[limit]' is not empty and the array has more elements
|
** (2) If 't[limit]' is not empty and the array has more elements
|
||||||
** after 'limit', try to find a boundary there. Again, try first
|
** after 'limit', try to find a boundary there. Again, try first
|
||||||
** the special case (which should be quite frequent) where 'limit+1'
|
** the special case (which should be quite frequent) where 'limit+1'
|
||||||
** is empty, so that 'limit' is a boundary. Otherwise, check the
|
** is empty, so that 'limit' is a boundary. Otherwise, check the
|
||||||
** last element of the array part (set it as a new limit). If it is empty,
|
** last element of the array part. If it is empty, there must be a
|
||||||
** there must be a boundary between the old limit (present) and the new
|
** boundary between the old limit (present) and the last element
|
||||||
** limit (absent), which is found with a binary search. (This boundary
|
** (absent), which is found with a binary search. (This boundary always
|
||||||
** always can be a new limit.)
|
** can be a new limit.)
|
||||||
|
**
|
||||||
** (3) The last case is when there are no elements in the array part
|
** (3) The last case is when there are no elements in the array part
|
||||||
** (limit == 0) or its last element (the new limit) is present.
|
** (limit == 0) or its last element (the new limit) is present.
|
||||||
** In this case, must check the hash part. If there is no hash part,
|
** In this case, must check the hash part. If there is no hash part
|
||||||
** the boundary is 0. Otherwise, if 'limit+1' is absent, 'limit' is
|
** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call
|
||||||
** a boundary. Finally, if 'limit+1' is present, call 'hash_search'
|
** 'hash_search' to find a boundary in the hash part of the table.
|
||||||
** to find a boundary in the hash part of the table. (In those
|
** (In those cases, the boundary is not inside the array part, and
|
||||||
** cases, the boundary is not inside the array part, and therefore
|
** therefore cannot be used as a new limit.)
|
||||||
** cannot be used as a new limit.)
|
|
||||||
*/
|
*/
|
||||||
lua_Unsigned luaH_getn (Table *t) {
|
lua_Unsigned luaH_getn (Table *t) {
|
||||||
unsigned int limit = t->alimit;
|
unsigned int limit = t->alimit;
|
||||||
if (limit > 0 && isempty(&t->array[limit - 1])) {
|
if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
|
||||||
/* (1) there must be a boundary before 'limit' */
|
/* there must be a boundary before 'limit' */
|
||||||
if (limit >= 2 && !isempty(&t->array[limit - 2])) {
|
if (limit >= 2 && !isempty(&t->array[limit - 2])) {
|
||||||
/* 'limit - 1' is a boundary; can it be a new limit? */
|
/* 'limit - 1' is a boundary; can it be a new limit? */
|
||||||
if (ispow2realasize(t) && !ispow2(limit - 1)) {
|
if (ispow2realasize(t) && !ispow2(limit - 1)) {
|
||||||
t->alimit = limit - 1;
|
t->alimit = limit - 1;
|
||||||
setnorealasize(t);
|
setnorealasize(t); /* now 'alimit' is not the real size */
|
||||||
}
|
}
|
||||||
return limit - 1;
|
return limit - 1;
|
||||||
}
|
}
|
||||||
@@ -880,8 +958,8 @@ lua_Unsigned luaH_getn (Table *t) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* 'limit' is zero or present in table */
|
/* 'limit' is zero or present in table */
|
||||||
if (!limitequalsasize(t)) {
|
if (!limitequalsasize(t)) { /* (2)? */
|
||||||
/* (2) 'limit' > 0 and array has more elements after 'limit' */
|
/* 'limit' > 0 and array has more elements after 'limit' */
|
||||||
if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
|
if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
|
||||||
return limit; /* this is the boundary */
|
return limit; /* this is the boundary */
|
||||||
/* else, try last element in the array */
|
/* else, try last element in the array */
|
||||||
@@ -899,7 +977,7 @@ lua_Unsigned luaH_getn (Table *t) {
|
|||||||
lua_assert(limit == luaH_realasize(t) &&
|
lua_assert(limit == luaH_realasize(t) &&
|
||||||
(limit == 0 || !isempty(&t->array[limit - 1])));
|
(limit == 0 || !isempty(&t->array[limit - 1])));
|
||||||
if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
|
if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
|
||||||
return limit; /* 'limit + 1' is absent... */
|
return limit; /* 'limit + 1' is absent */
|
||||||
else /* 'limit + 1' is also present */
|
else /* 'limit + 1' is also present */
|
||||||
return hash_search(t, limit);
|
return hash_search(t, limit);
|
||||||
}
|
}
|
||||||
@@ -908,10 +986,10 @@ lua_Unsigned luaH_getn (Table *t) {
|
|||||||
|
|
||||||
#if defined(LUA_DEBUG)
|
#if defined(LUA_DEBUG)
|
||||||
|
|
||||||
|
/* export these functions for the test library */
|
||||||
|
|
||||||
Node *luaH_mainposition (const Table *t, const TValue *key) {
|
Node *luaH_mainposition (const Table *t, const TValue *key) {
|
||||||
return mainpositionTV(t, key);
|
return mainpositionTV(t, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
int luaH_isdummy (const Table *t) { return isdummy(t); }
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -15,7 +15,12 @@
|
|||||||
#define gnext(n) ((n)->u.next)
|
#define gnext(n) ((n)->u.next)
|
||||||
|
|
||||||
|
|
||||||
#define invalidateTMcache(t) ((t)->flags = 0)
|
/*
|
||||||
|
** Clear all bits of fast-access metamethods, which means that the table
|
||||||
|
** may have any of these metamethods. (First access that fails after the
|
||||||
|
** clearing will set the bit again.)
|
||||||
|
*/
|
||||||
|
#define invalidateTMcache(t) ((t)->flags &= ~maskflags)
|
||||||
|
|
||||||
|
|
||||||
/* true when 't' is using 'dummynode' as its hash part */
|
/* true when 't' is using 'dummynode' as its hash part */
|
||||||
@@ -27,7 +32,7 @@
|
|||||||
|
|
||||||
|
|
||||||
/* returns the Node, given the value of a table entry */
|
/* returns the Node, given the value of a table entry */
|
||||||
#define nodefromval(v) cast(Node *, (v))
|
#define nodefromval(v) cast(Node *, (v))
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
|
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
|
||||||
@@ -36,8 +41,10 @@ LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
|
|||||||
LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
|
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_getstr (Table *t, TString *key);
|
||||||
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
|
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
|
||||||
LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key);
|
LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key,
|
||||||
LUAI_FUNC TValue *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);
|
||||||
LUAI_FUNC Table *luaH_new (lua_State *L);
|
LUAI_FUNC Table *luaH_new (lua_State *L);
|
||||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
||||||
unsigned int nhsize);
|
unsigned int nhsize);
|
||||||
@@ -50,7 +57,6 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t);
|
|||||||
|
|
||||||
#if defined(LUA_DEBUG)
|
#if defined(LUA_DEBUG)
|
||||||
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
|
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
|
||||||
LUAI_FUNC int luaH_isdummy (const Table *t);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -59,8 +59,9 @@ static void checktab (lua_State *L, int arg, int what) {
|
|||||||
|
|
||||||
|
|
||||||
static int tinsert (lua_State *L) {
|
static int tinsert (lua_State *L) {
|
||||||
lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
|
|
||||||
lua_Integer pos; /* where to insert new element */
|
lua_Integer pos; /* where to insert new element */
|
||||||
|
lua_Integer e = aux_getn(L, 1, TAB_RW);
|
||||||
|
e = luaL_intop(+, e, 1); /* first empty element */
|
||||||
switch (lua_gettop(L)) {
|
switch (lua_gettop(L)) {
|
||||||
case 2: { /* called with only 2 arguments */
|
case 2: { /* called with only 2 arguments */
|
||||||
pos = e; /* insert new element at the end */
|
pos = e; /* insert new element at the end */
|
||||||
@@ -92,7 +93,7 @@ static int tremove (lua_State *L) {
|
|||||||
lua_Integer pos = luaL_optinteger(L, 2, size);
|
lua_Integer pos = luaL_optinteger(L, 2, size);
|
||||||
if (pos != size) /* validate 'pos' if given */
|
if (pos != size) /* validate 'pos' if given */
|
||||||
/* check whether 'pos' is in [1, size + 1] */
|
/* check whether 'pos' is in [1, size + 1] */
|
||||||
luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1,
|
luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2,
|
||||||
"position out of bounds");
|
"position out of bounds");
|
||||||
lua_geti(L, 1, pos); /* result = t[pos] */
|
lua_geti(L, 1, pos); /* result = t[pos] */
|
||||||
for ( ; pos < size; pos++) {
|
for ( ; pos < size; pos++) {
|
||||||
@@ -145,9 +146,9 @@ static int tmove (lua_State *L) {
|
|||||||
|
|
||||||
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
|
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
|
||||||
lua_geti(L, 1, i);
|
lua_geti(L, 1, i);
|
||||||
if (!lua_isstring(L, -1))
|
if (l_unlikely(!lua_isstring(L, -1)))
|
||||||
luaL_error(L, "invalid value (%s) at index %d in table for 'concat'",
|
luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
|
||||||
luaL_typename(L, -1), i);
|
luaL_typename(L, -1), (LUAI_UACINT)i);
|
||||||
luaL_addvalue(b);
|
luaL_addvalue(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,7 +197,8 @@ static int tunpack (lua_State *L) {
|
|||||||
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
|
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
|
||||||
if (i > e) return 0; /* empty range */
|
if (i > e) return 0; /* empty range */
|
||||||
n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
|
n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
|
||||||
if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
|
if (l_unlikely(n >= (unsigned int)INT_MAX ||
|
||||||
|
!lua_checkstack(L, (int)(++n))))
|
||||||
return luaL_error(L, "too many results to unpack");
|
return luaL_error(L, "too many results to unpack");
|
||||||
for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
|
for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
|
||||||
lua_geti(L, 1, i);
|
lua_geti(L, 1, i);
|
||||||
@@ -300,14 +302,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
/* next loop: repeat ++i while a[i] < P */
|
/* next loop: repeat ++i while a[i] < P */
|
||||||
while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||||
if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
|
if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */
|
||||||
luaL_error(L, "invalid order function for sorting");
|
luaL_error(L, "invalid order function for sorting");
|
||||||
lua_pop(L, 1); /* remove a[i] */
|
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 */
|
||||||
/* next loop: repeat --j while P < a[j] */
|
/* next loop: repeat --j while P < a[j] */
|
||||||
while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||||
if (j < i) /* j < i but a[j] > P ?? */
|
if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */
|
||||||
luaL_error(L, "invalid order function for sorting");
|
luaL_error(L, "invalid order function for sorting");
|
||||||
lua_pop(L, 1); /* remove a[j] */
|
lua_pop(L, 1); /* remove a[j] */
|
||||||
}
|
}
|
||||||
@@ -338,7 +340,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** QuickSort algorithm (recursive function)
|
** Quicksort algorithm (recursive function)
|
||||||
*/
|
*/
|
||||||
static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||||
unsigned int rnd) {
|
unsigned int rnd) {
|
||||||
|
|||||||
@@ -44,7 +44,7 @@
|
|||||||
void *l_Trick = 0;
|
void *l_Trick = 0;
|
||||||
|
|
||||||
|
|
||||||
#define obj_at(L,k) s2v(L->ci->func + (k))
|
#define obj_at(L,k) s2v(L->ci->func.p + (k))
|
||||||
|
|
||||||
|
|
||||||
static int runC (lua_State *L, lua_State *L1, const char *pc);
|
static int runC (lua_State *L, lua_State *L1, const char *pc);
|
||||||
@@ -57,13 +57,15 @@ static void setnameval (lua_State *L, const char *name, int val) {
|
|||||||
|
|
||||||
|
|
||||||
static void pushobject (lua_State *L, const TValue *o) {
|
static void pushobject (lua_State *L, const TValue *o) {
|
||||||
setobj2s(L, L->top, o);
|
setobj2s(L, L->top.p, o);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void badexit (const char *fmt, const char *s) {
|
static void badexit (const char *fmt, const char *s1, const char *s2) {
|
||||||
fprintf(stderr, fmt, s);
|
fprintf(stderr, fmt, s1);
|
||||||
|
if (s2)
|
||||||
|
fprintf(stderr, "extra info: %s\n", s2);
|
||||||
/* avoid assertion failures when exiting */
|
/* avoid assertion failures when exiting */
|
||||||
l_memcontrol.numblocks = l_memcontrol.total = 0;
|
l_memcontrol.numblocks = l_memcontrol.total = 0;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -71,40 +73,84 @@ static void badexit (const char *fmt, const char *s) {
|
|||||||
|
|
||||||
|
|
||||||
static int tpanic (lua_State *L) {
|
static int tpanic (lua_State *L) {
|
||||||
|
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",
|
return (badexit("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||||
lua_tostring(L, -1)),
|
msg, NULL),
|
||||||
0); /* do not return to Lua */
|
0); /* do not return to Lua */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Warning function for tests. Fist, it concatenates all parts of
|
** Warning function for tests. First, it concatenates all parts of
|
||||||
** a warning in buffer 'buff'. Then:
|
** a warning in buffer 'buff'. Then, it has three modes:
|
||||||
** - messages starting with '#' are shown on standard output (used to
|
** - 0.normal: messages starting with '#' are shown on standard output;
|
||||||
** test explicit warnings);
|
|
||||||
** - messages containing '@' are stored in global '_WARN' (used to test
|
|
||||||
** errors that generate warnings);
|
|
||||||
** - other messages abort the tests (they represent real warning
|
** - other messages abort the tests (they represent real warning
|
||||||
** conditions; the standard tests should not generate these conditions
|
** conditions; the standard tests should not generate these conditions
|
||||||
** unexpectedly).
|
** unexpectedly);
|
||||||
|
** - 1.allow: all messages are shown;
|
||||||
|
** - 2.store: all warnings go to the global '_WARN';
|
||||||
*/
|
*/
|
||||||
static void warnf (void *ud, const char *msg, int tocont) {
|
static void warnf (void *ud, const char *msg, int tocont) {
|
||||||
|
lua_State *L = cast(lua_State *, ud);
|
||||||
static char buff[200] = ""; /* should be enough for tests... */
|
static char buff[200] = ""; /* should be enough for tests... */
|
||||||
|
static int onoff = 0;
|
||||||
|
static int mode = 0; /* start in normal mode */
|
||||||
|
static int lasttocont = 0;
|
||||||
|
if (!lasttocont && !tocont && *msg == '@') { /* control message? */
|
||||||
|
if (buff[0] != '\0')
|
||||||
|
badexit("Control warning during warning: %s\naborting...\n", msg, buff);
|
||||||
|
if (strcmp(msg, "@off") == 0)
|
||||||
|
onoff = 0;
|
||||||
|
else if (strcmp(msg, "@on") == 0)
|
||||||
|
onoff = 1;
|
||||||
|
else if (strcmp(msg, "@normal") == 0)
|
||||||
|
mode = 0;
|
||||||
|
else if (strcmp(msg, "@allow") == 0)
|
||||||
|
mode = 1;
|
||||||
|
else if (strcmp(msg, "@store") == 0)
|
||||||
|
mode = 2;
|
||||||
|
else
|
||||||
|
badexit("Invalid control warning in test mode: %s\naborting...\n",
|
||||||
|
msg, NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lasttocont = tocont;
|
||||||
if (strlen(msg) >= sizeof(buff) - strlen(buff))
|
if (strlen(msg) >= sizeof(buff) - strlen(buff))
|
||||||
badexit("%s", "warnf-buffer overflow");
|
badexit("warnf-buffer overflow (%s)\n", msg, buff);
|
||||||
strcat(buff, msg); /* add new message to current warning */
|
strcat(buff, msg); /* add new message to current warning */
|
||||||
if (!tocont) { /* message finished? */
|
if (!tocont) { /* message finished? */
|
||||||
if (buff[0] == '#') /* expected warning? */
|
lua_unlock(L);
|
||||||
printf("Expected Lua warning: %s\n", buff); /* print it */
|
luaL_checkstack(L, 1, "warn stack space");
|
||||||
else if (strchr(buff, '@') != NULL) { /* warning for test purposes? */
|
lua_getglobal(L, "_WARN");
|
||||||
lua_State *L = cast(lua_State *, ud);
|
if (!lua_toboolean(L, -1))
|
||||||
lua_unlock(L);
|
lua_pop(L, 1); /* ok, no previous unexpected warning */
|
||||||
lua_pushstring(L, buff);
|
else {
|
||||||
lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */
|
badexit("Unhandled warning in store mode: %s\naborting...\n",
|
||||||
lua_lock(L);
|
lua_tostring(L, -1), buff);
|
||||||
|
}
|
||||||
|
lua_lock(L);
|
||||||
|
switch (mode) {
|
||||||
|
case 0: { /* normal */
|
||||||
|
if (buff[0] != '#' && onoff) /* unexpected warning? */
|
||||||
|
badexit("Unexpected warning in test mode: %s\naborting...\n",
|
||||||
|
buff, NULL);
|
||||||
|
} /* FALLTHROUGH */
|
||||||
|
case 1: { /* allow */
|
||||||
|
if (onoff)
|
||||||
|
fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: { /* store */
|
||||||
|
lua_unlock(L);
|
||||||
|
luaL_checkstack(L, 1, "warn stack space");
|
||||||
|
lua_pushstring(L, buff);
|
||||||
|
lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */
|
||||||
|
lua_lock(L);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else /* a real warning; should not happen during tests */
|
|
||||||
badexit("Unexpected warning in test mode: %s\naborting...\n", buff);
|
|
||||||
buff[0] = '\0'; /* prepare buffer for next warning */
|
buff[0] = '\0'; /* prepare buffer for next warning */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,7 +189,8 @@ typedef union Header {
|
|||||||
|
|
||||||
|
|
||||||
Memcontrol l_memcontrol =
|
Memcontrol l_memcontrol =
|
||||||
{0UL, 0UL, 0UL, 0UL, (~0UL), {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
|
{0, 0UL, 0UL, 0UL, 0UL, (~0UL),
|
||||||
|
{0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
|
||||||
|
|
||||||
|
|
||||||
static void freeblock (Memcontrol *mc, Header *block) {
|
static void freeblock (Memcontrol *mc, Header *block) {
|
||||||
@@ -182,6 +229,10 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
|||||||
freeblock(mc, block);
|
freeblock(mc, block);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
if (mc->failnext) {
|
||||||
|
mc->failnext = 0;
|
||||||
|
return NULL; /* fake a single memory allocation error */
|
||||||
|
}
|
||||||
if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */
|
if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */
|
||||||
if (mc->countlimit == 0)
|
if (mc->countlimit == 0)
|
||||||
return NULL; /* fake a memory allocation error */
|
return NULL; /* fake a memory allocation error */
|
||||||
@@ -224,11 +275,15 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {=====================================================================
|
||||||
** Functions to check memory consistency
|
** Functions to check memory consistency.
|
||||||
** =======================================================
|
** Most of these checks are done through asserts, so this code does
|
||||||
|
** not make sense with asserts off. For this reason, it uses 'assert'
|
||||||
|
** directly, instead of 'lua_assert'.
|
||||||
|
** ======================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check GC invariants. For incremental mode, a black object cannot
|
** Check GC invariants. For incremental mode, a black object cannot
|
||||||
@@ -261,11 +316,15 @@ static void printobj (global_State *g, GCObject *o) {
|
|||||||
ttypename(novariant(o->tt)), (void *)o,
|
ttypename(novariant(o->tt)), (void *)o,
|
||||||
isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g',
|
isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g',
|
||||||
"ns01oTt"[getage(o)], o->marked);
|
"ns01oTt"[getage(o)], o->marked);
|
||||||
if (o->tt == LUA_TSHRSTR || o->tt == LUA_TLNGSTR)
|
if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR)
|
||||||
printf(" '%s'", getstr(gco2ts(o)));
|
printf(" '%s'", getstr(gco2ts(o)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_printobj (lua_State *L, struct GCObject *o) {
|
||||||
|
printobj(G(L), o);
|
||||||
|
}
|
||||||
|
|
||||||
static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
||||||
int r1 = testobjref1(g, f, t);
|
int r1 = testobjref1(g, f, t);
|
||||||
if (!r1) {
|
if (!r1) {
|
||||||
@@ -278,13 +337,23 @@ static int testobjref (global_State *g, GCObject *f, GCObject *t) {
|
|||||||
return r1;
|
return r1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define checkobjref(g,f,t) \
|
|
||||||
{ if (t) lua_longassert(testobjref(g,f,obj2gco(t))); }
|
static void checkobjref (global_State *g, GCObject *f, GCObject *t) {
|
||||||
|
assert(testobjref(g, f, t));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Version where 't' can be NULL. In that case, it should not apply the
|
||||||
|
** macro 'obj2gco' over the object. ('t' may have several types, so this
|
||||||
|
** definition must be a macro.) Most checks need this version, because
|
||||||
|
** the check may run while an object is still being created.
|
||||||
|
*/
|
||||||
|
#define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); }
|
||||||
|
|
||||||
|
|
||||||
static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
|
static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
|
||||||
lua_assert(!iscollectable(t) ||
|
assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t))));
|
||||||
(righttt(t) && testobjref(g, f, gcvalue(t))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -293,14 +362,14 @@ static void checktable (global_State *g, Table *h) {
|
|||||||
unsigned int asize = luaH_realasize(h);
|
unsigned int asize = luaH_realasize(h);
|
||||||
Node *n, *limit = gnode(h, sizenode(h));
|
Node *n, *limit = gnode(h, sizenode(h));
|
||||||
GCObject *hgc = obj2gco(h);
|
GCObject *hgc = obj2gco(h);
|
||||||
checkobjref(g, hgc, h->metatable);
|
checkobjrefN(g, hgc, h->metatable);
|
||||||
for (i = 0; i < asize; i++)
|
for (i = 0; i < asize; i++)
|
||||||
checkvalref(g, hgc, &h->array[i]);
|
checkvalref(g, hgc, &h->array[i]);
|
||||||
for (n = gnode(h, 0); n < limit; n++) {
|
for (n = gnode(h, 0); n < limit; n++) {
|
||||||
if (!isempty(gval(n))) {
|
if (!isempty(gval(n))) {
|
||||||
TValue k;
|
TValue k;
|
||||||
getnodekey(g->mainthread, &k, n);
|
getnodekey(g->mainthread, &k, n);
|
||||||
lua_assert(!keyisnil(n));
|
assert(!keyisnil(n));
|
||||||
checkvalref(g, hgc, &k);
|
checkvalref(g, hgc, &k);
|
||||||
checkvalref(g, hgc, gval(n));
|
checkvalref(g, hgc, gval(n));
|
||||||
}
|
}
|
||||||
@@ -311,30 +380,26 @@ static void checktable (global_State *g, Table *h) {
|
|||||||
static void checkudata (global_State *g, Udata *u) {
|
static void checkudata (global_State *g, Udata *u) {
|
||||||
int i;
|
int i;
|
||||||
GCObject *hgc = obj2gco(u);
|
GCObject *hgc = obj2gco(u);
|
||||||
checkobjref(g, hgc, u->metatable);
|
checkobjrefN(g, hgc, u->metatable);
|
||||||
for (i = 0; i < u->nuvalue; i++)
|
for (i = 0; i < u->nuvalue; i++)
|
||||||
checkvalref(g, hgc, &u->uv[i].uv);
|
checkvalref(g, hgc, &u->uv[i].uv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** All marks are conditional because a GC may happen while the
|
|
||||||
** prototype is still being created
|
|
||||||
*/
|
|
||||||
static void checkproto (global_State *g, Proto *f) {
|
static void checkproto (global_State *g, Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
GCObject *fgc = obj2gco(f);
|
GCObject *fgc = obj2gco(f);
|
||||||
checkobjref(g, fgc, f->source);
|
checkobjrefN(g, fgc, f->source);
|
||||||
for (i=0; i<f->sizek; i++) {
|
for (i=0; i<f->sizek; i++) {
|
||||||
if (ttisstring(f->k + i))
|
if (iscollectable(f->k + i))
|
||||||
checkobjref(g, fgc, tsvalue(f->k + i));
|
checkobjref(g, fgc, gcvalue(f->k + i));
|
||||||
}
|
}
|
||||||
for (i=0; i<f->sizeupvalues; i++)
|
for (i=0; i<f->sizeupvalues; i++)
|
||||||
checkobjref(g, fgc, f->upvalues[i].name);
|
checkobjrefN(g, fgc, f->upvalues[i].name);
|
||||||
for (i=0; i<f->sizep; i++)
|
for (i=0; i<f->sizep; i++)
|
||||||
checkobjref(g, fgc, f->p[i]);
|
checkobjrefN(g, fgc, f->p[i]);
|
||||||
for (i=0; i<f->sizelocvars; i++)
|
for (i=0; i<f->sizelocvars; i++)
|
||||||
checkobjref(g, fgc, f->locvars[i].varname);
|
checkobjrefN(g, fgc, f->locvars[i].varname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -349,13 +414,13 @@ static void checkCclosure (global_State *g, CClosure *cl) {
|
|||||||
static void checkLclosure (global_State *g, LClosure *cl) {
|
static void checkLclosure (global_State *g, LClosure *cl) {
|
||||||
GCObject *clgc = obj2gco(cl);
|
GCObject *clgc = obj2gco(cl);
|
||||||
int i;
|
int i;
|
||||||
checkobjref(g, clgc, cl->p);
|
checkobjrefN(g, clgc, cl->p);
|
||||||
for (i=0; i<cl->nupvalues; i++) {
|
for (i=0; i<cl->nupvalues; i++) {
|
||||||
UpVal *uv = cl->upvals[i];
|
UpVal *uv = cl->upvals[i];
|
||||||
if (uv) {
|
if (uv) {
|
||||||
checkobjref(g, clgc, uv);
|
checkobjrefN(g, clgc, uv);
|
||||||
if (!upisopen(uv))
|
if (!upisopen(uv))
|
||||||
checkvalref(g, obj2gco(uv), uv->v);
|
checkvalref(g, obj2gco(uv), uv->v.p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -364,7 +429,7 @@ static void checkLclosure (global_State *g, LClosure *cl) {
|
|||||||
static int lua_checkpc (CallInfo *ci) {
|
static int lua_checkpc (CallInfo *ci) {
|
||||||
if (!isLua(ci)) return 1;
|
if (!isLua(ci)) return 1;
|
||||||
else {
|
else {
|
||||||
StkId f = ci->func;
|
StkId f = ci->func.p;
|
||||||
Proto *p = clLvalue(s2v(f))->p;
|
Proto *p = clLvalue(s2v(f))->p;
|
||||||
return p->code <= ci->u.l.savedpc &&
|
return p->code <= ci->u.l.savedpc &&
|
||||||
ci->u.l.savedpc <= p->code + p->sizecode;
|
ci->u.l.savedpc <= p->code + p->sizecode;
|
||||||
@@ -376,58 +441,60 @@ static void checkstack (global_State *g, lua_State *L1) {
|
|||||||
StkId o;
|
StkId o;
|
||||||
CallInfo *ci;
|
CallInfo *ci;
|
||||||
UpVal *uv;
|
UpVal *uv;
|
||||||
lua_assert(!isdead(g, L1));
|
assert(!isdead(g, L1));
|
||||||
|
if (L1->stack.p == NULL) { /* incomplete thread? */
|
||||||
|
assert(L1->openupval == NULL && L1->ci == NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next)
|
for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next)
|
||||||
lua_assert(upisopen(uv)); /* must be open */
|
assert(upisopen(uv)); /* must be open */
|
||||||
|
assert(L1->top.p <= L1->stack_last.p);
|
||||||
|
assert(L1->tbclist.p <= L1->top.p);
|
||||||
for (ci = L1->ci; ci != NULL; ci = ci->previous) {
|
for (ci = L1->ci; ci != NULL; ci = ci->previous) {
|
||||||
lua_assert(ci->top <= L1->stack_last);
|
assert(ci->top.p <= L1->stack_last.p);
|
||||||
lua_assert(lua_checkpc(ci));
|
assert(lua_checkpc(ci));
|
||||||
}
|
}
|
||||||
if (L1->stack) { /* complete thread? */
|
for (o = L1->stack.p; o < L1->stack_last.p; o++)
|
||||||
for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++)
|
checkliveness(L1, s2v(o)); /* entire stack must have valid values */
|
||||||
checkliveness(L1, s2v(o)); /* entire stack must have valid values */
|
|
||||||
}
|
|
||||||
else lua_assert(L1->stacksize == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void checkrefs (global_State *g, GCObject *o) {
|
static void checkrefs (global_State *g, GCObject *o) {
|
||||||
switch (o->tt) {
|
switch (o->tt) {
|
||||||
case LUA_TUSERDATA: {
|
case LUA_VUSERDATA: {
|
||||||
checkudata(g, gco2u(o));
|
checkudata(g, gco2u(o));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TUPVAL:
|
case LUA_VUPVAL: {
|
||||||
case LUA_TUPVALTBC: {
|
checkvalref(g, o, gco2upv(o)->v.p);
|
||||||
checkvalref(g, o, gco2upv(o)->v);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TTABLE: {
|
case LUA_VTABLE: {
|
||||||
checktable(g, gco2t(o));
|
checktable(g, gco2t(o));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TTHREAD: {
|
case LUA_VTHREAD: {
|
||||||
checkstack(g, gco2th(o));
|
checkstack(g, gco2th(o));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TLCL: {
|
case LUA_VLCL: {
|
||||||
checkLclosure(g, gco2lcl(o));
|
checkLclosure(g, gco2lcl(o));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TCCL: {
|
case LUA_VCCL: {
|
||||||
checkCclosure(g, gco2ccl(o));
|
checkCclosure(g, gco2ccl(o));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TPROTO: {
|
case LUA_VPROTO: {
|
||||||
checkproto(g, gco2p(o));
|
checkproto(g, gco2p(o));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TSHRSTR:
|
case LUA_VSHRSTR:
|
||||||
case LUA_TLNGSTR: {
|
case LUA_VLNGSTR: {
|
||||||
lua_assert(!isgray(o)); /* strings are never gray */
|
assert(!isgray(o)); /* strings are never gray */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: lua_assert(0);
|
default: assert(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,7 +502,7 @@ static void checkrefs (global_State *g, GCObject *o) {
|
|||||||
/*
|
/*
|
||||||
** Check consistency of an object:
|
** Check consistency of an object:
|
||||||
** - Dead objects can only happen in the 'allgc' list during a sweep
|
** - Dead objects can only happen in the 'allgc' list during a sweep
|
||||||
** phase (controled by the caller through 'maybedead').
|
** phase (controlled by the caller through 'maybedead').
|
||||||
** - During pause, all objects must be white.
|
** - During pause, all objects must be white.
|
||||||
** - In generational mode:
|
** - In generational mode:
|
||||||
** * objects must be old enough for their lists ('listage').
|
** * objects must be old enough for their lists ('listage').
|
||||||
@@ -446,18 +513,18 @@ static void checkrefs (global_State *g, GCObject *o) {
|
|||||||
static void checkobject (global_State *g, GCObject *o, int maybedead,
|
static void checkobject (global_State *g, GCObject *o, int maybedead,
|
||||||
int listage) {
|
int listage) {
|
||||||
if (isdead(g, o))
|
if (isdead(g, o))
|
||||||
lua_assert(maybedead);
|
assert(maybedead);
|
||||||
else {
|
else {
|
||||||
lua_assert(g->gcstate != GCSpause || iswhite(o));
|
assert(g->gcstate != GCSpause || iswhite(o));
|
||||||
if (g->gckind == KGC_GEN) { /* generational mode? */
|
if (g->gckind == KGC_GEN) { /* generational mode? */
|
||||||
lua_assert(getage(o) >= listage);
|
assert(getage(o) >= listage);
|
||||||
lua_assert(!iswhite(o) || !isold(o));
|
assert(!iswhite(o) || !isold(o));
|
||||||
if (isold(o)) {
|
if (isold(o)) {
|
||||||
lua_assert(isblack(o) ||
|
assert(isblack(o) ||
|
||||||
getage(o) == G_TOUCHED1 ||
|
getage(o) == G_TOUCHED1 ||
|
||||||
getage(o) == G_OLD0 ||
|
getage(o) == G_OLD0 ||
|
||||||
o->tt == LUA_TTHREAD ||
|
o->tt == LUA_VTHREAD ||
|
||||||
(o->tt == LUA_TUPVAL && upisopen(gco2upv(o))));
|
(o->tt == LUA_VUPVAL && upisopen(gco2upv(o))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checkrefs(g, o);
|
checkrefs(g, o);
|
||||||
@@ -465,53 +532,94 @@ static void checkobject (global_State *g, GCObject *o, int maybedead,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void checkgraylist (global_State *g, GCObject *o) {
|
static lu_mem checkgraylist (global_State *g, GCObject *o) {
|
||||||
((void)g); /* better to keep it available if we need to print an object */
|
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) {
|
while (o) {
|
||||||
lua_assert(isgray(o) || getage(o) == G_TOUCHED2);
|
assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2));
|
||||||
|
assert(!testbit(o->marked, TESTBIT));
|
||||||
|
if (keepinvariant(g))
|
||||||
|
l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */
|
||||||
|
total++;
|
||||||
switch (o->tt) {
|
switch (o->tt) {
|
||||||
case LUA_TTABLE: o = gco2t(o)->gclist; break;
|
case LUA_VTABLE: o = gco2t(o)->gclist; break;
|
||||||
case LUA_TLCL: o = gco2lcl(o)->gclist; break;
|
case LUA_VLCL: o = gco2lcl(o)->gclist; break;
|
||||||
case LUA_TCCL: o = gco2ccl(o)->gclist; break;
|
case LUA_VCCL: o = gco2ccl(o)->gclist; break;
|
||||||
case LUA_TTHREAD: o = gco2th(o)->gclist; break;
|
case LUA_VTHREAD: o = gco2th(o)->gclist; break;
|
||||||
case LUA_TPROTO: o = gco2p(o)->gclist; break;
|
case LUA_VPROTO: o = gco2p(o)->gclist; break;
|
||||||
default: lua_assert(0); /* other objects cannot be in a gray list */
|
case LUA_VUSERDATA:
|
||||||
|
assert(gco2u(o)->nuvalue > 0);
|
||||||
|
o = gco2u(o)->gclist;
|
||||||
|
break;
|
||||||
|
default: assert(0); /* other objects cannot be in a gray list */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check objects in gray lists.
|
** Check objects in gray lists.
|
||||||
*/
|
*/
|
||||||
static void checkgrays (global_State *g) {
|
static lu_mem checkgrays (global_State *g) {
|
||||||
if (!keepinvariant(g)) return;
|
int total = 0; /* count number of elements in all lists */
|
||||||
checkgraylist(g, g->gray);
|
if (!keepinvariant(g)) return total;
|
||||||
checkgraylist(g, g->grayagain);
|
total += checkgraylist(g, g->gray);
|
||||||
checkgraylist(g, g->weak);
|
total += checkgraylist(g, g->grayagain);
|
||||||
checkgraylist(g, g->ephemeron);
|
total += checkgraylist(g, g->weak);
|
||||||
|
total += checkgraylist(g, g->allweak);
|
||||||
|
total += checkgraylist(g, g->ephemeron);
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void checklist (global_State *g, int maybedead, int tof,
|
/*
|
||||||
|
** Check whether 'o' should be in a gray list. If so, increment
|
||||||
|
** 'count' and check its TESTBIT. (It must have been previously set by
|
||||||
|
** 'checkgraylist'.)
|
||||||
|
*/
|
||||||
|
static void incifingray (global_State *g, GCObject *o, lu_mem *count) {
|
||||||
|
if (!keepinvariant(g))
|
||||||
|
return; /* gray lists not being kept in these phases */
|
||||||
|
if (o->tt == LUA_VUPVAL) {
|
||||||
|
/* only open upvalues can be gray */
|
||||||
|
assert(!isgray(o) || upisopen(gco2upv(o)));
|
||||||
|
return; /* upvalues are never in gray lists */
|
||||||
|
}
|
||||||
|
/* these are the ones that must be in gray lists */
|
||||||
|
if (isgray(o) || getage(o) == G_TOUCHED2) {
|
||||||
|
(*count)++;
|
||||||
|
assert(testbit(o->marked, TESTBIT));
|
||||||
|
resetbit(o->marked, TESTBIT); /* prepare for next cycle */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static lu_mem checklist (global_State *g, int maybedead, int tof,
|
||||||
GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) {
|
GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) {
|
||||||
GCObject *o;
|
GCObject *o;
|
||||||
|
lu_mem total = 0; /* number of object that should be in gray lists */
|
||||||
for (o = newl; o != survival; o = o->next) {
|
for (o = newl; o != survival; o = o->next) {
|
||||||
checkobject(g, o, maybedead, G_NEW);
|
checkobject(g, o, maybedead, G_NEW);
|
||||||
lua_assert(!tof == !tofinalize(o));
|
incifingray(g, o, &total);
|
||||||
|
assert(!tof == !tofinalize(o));
|
||||||
}
|
}
|
||||||
for (o = survival; o != old; o = o->next) {
|
for (o = survival; o != old; o = o->next) {
|
||||||
checkobject(g, o, 0, G_SURVIVAL);
|
checkobject(g, o, 0, G_SURVIVAL);
|
||||||
lua_assert(!tof == !tofinalize(o));
|
incifingray(g, o, &total);
|
||||||
|
assert(!tof == !tofinalize(o));
|
||||||
}
|
}
|
||||||
for (o = old; o != reallyold; o = o->next) {
|
for (o = old; o != reallyold; o = o->next) {
|
||||||
checkobject(g, o, 0, G_OLD1);
|
checkobject(g, o, 0, G_OLD1);
|
||||||
lua_assert(!tof == !tofinalize(o));
|
incifingray(g, o, &total);
|
||||||
|
assert(!tof == !tofinalize(o));
|
||||||
}
|
}
|
||||||
for (o = reallyold; o != NULL; o = o->next) {
|
for (o = reallyold; o != NULL; o = o->next) {
|
||||||
checkobject(g, o, 0, G_OLD);
|
checkobject(g, o, 0, G_OLD);
|
||||||
lua_assert(!tof == !tofinalize(o));
|
incifingray(g, o, &total);
|
||||||
|
assert(!tof == !tofinalize(o));
|
||||||
}
|
}
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -519,32 +627,39 @@ int lua_checkmemory (lua_State *L) {
|
|||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
GCObject *o;
|
GCObject *o;
|
||||||
int maybedead;
|
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 */
|
||||||
if (keepinvariant(g)) {
|
if (keepinvariant(g)) {
|
||||||
lua_assert(!iswhite(g->mainthread));
|
assert(!iswhite(g->mainthread));
|
||||||
lua_assert(!iswhite(gcvalue(&g->l_registry)));
|
assert(!iswhite(gcvalue(&g->l_registry)));
|
||||||
}
|
}
|
||||||
lua_assert(!isdead(g, gcvalue(&g->l_registry)));
|
assert(!isdead(g, gcvalue(&g->l_registry)));
|
||||||
lua_assert(g->sweepgc == NULL || issweepphase(g));
|
assert(g->sweepgc == NULL || issweepphase(g));
|
||||||
checkgrays(g);
|
totalin = checkgrays(g);
|
||||||
|
|
||||||
/* check 'fixedgc' list */
|
/* check 'fixedgc' list */
|
||||||
for (o = g->fixedgc; o != NULL; o = o->next) {
|
for (o = g->fixedgc; o != NULL; o = o->next) {
|
||||||
lua_assert(o->tt == LUA_TSHRSTR && isgray(o) && getage(o) == G_OLD);
|
assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check 'allgc' list */
|
/* check 'allgc' list */
|
||||||
maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc);
|
maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc);
|
||||||
checklist(g, maybedead, 0, g->allgc, g->survival, g->old, g->reallyold);
|
totalshould = checklist(g, maybedead, 0, g->allgc,
|
||||||
|
g->survival, g->old1, g->reallyold);
|
||||||
|
|
||||||
/* check 'finobj' list */
|
/* check 'finobj' list */
|
||||||
checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold, g->finobjrold);
|
totalshould += checklist(g, 0, 1, g->finobj,
|
||||||
|
g->finobjsur, g->finobjold1, g->finobjrold);
|
||||||
|
|
||||||
/* check 'tobefnz' list */
|
/* check 'tobefnz' list */
|
||||||
for (o = g->tobefnz; o != NULL; o = o->next) {
|
for (o = g->tobefnz; o != NULL; o = o->next) {
|
||||||
checkobject(g, o, 0, G_NEW);
|
checkobject(g, o, 0, G_NEW);
|
||||||
lua_assert(tofinalize(o));
|
incifingray(g, o, &totalshould);
|
||||||
lua_assert(o->tt == LUA_TUSERDATA || o->tt == LUA_TTABLE);
|
assert(tofinalize(o));
|
||||||
|
assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE);
|
||||||
}
|
}
|
||||||
|
if (keepinvariant(g))
|
||||||
|
assert(totalin == totalshould);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -700,11 +815,12 @@ static int listlocals (lua_State *L) {
|
|||||||
static void printstack (lua_State *L) {
|
static void printstack (lua_State *L) {
|
||||||
int i;
|
int i;
|
||||||
int n = lua_gettop(L);
|
int n = lua_gettop(L);
|
||||||
|
printf("stack: >>\n");
|
||||||
for (i = 1; i <= n; i++) {
|
for (i = 1; i <= n; i++) {
|
||||||
printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
|
printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("<<\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -742,7 +858,7 @@ static int mem_query (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return luaL_error(L, "unkown type '%s'", t);
|
return luaL_error(L, "unknown type '%s'", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -754,7 +870,14 @@ static int alloc_count (lua_State *L) {
|
|||||||
l_memcontrol.countlimit = luaL_checkinteger(L, 1);
|
l_memcontrol.countlimit = luaL_checkinteger(L, 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int alloc_failnext (lua_State *L) {
|
||||||
|
UNUSED(L);
|
||||||
|
l_memcontrol.failnext = 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int settrick (lua_State *L) {
|
static int settrick (lua_State *L) {
|
||||||
if (ttisnil(obj_at(L, 1)))
|
if (ttisnil(obj_at(L, 1)))
|
||||||
@@ -858,8 +981,8 @@ static int hash_query (lua_State *L) {
|
|||||||
|
|
||||||
static int stacklevel (lua_State *L) {
|
static int stacklevel (lua_State *L) {
|
||||||
unsigned long a = 0;
|
unsigned long a = 0;
|
||||||
lua_pushinteger(L, (L->top - L->stack));
|
lua_pushinteger(L, (L->top.p - L->stack.p));
|
||||||
lua_pushinteger(L, (L->stack_last - L->stack));
|
lua_pushinteger(L, stacksize(L));
|
||||||
lua_pushinteger(L, L->nCcalls);
|
lua_pushinteger(L, L->nCcalls);
|
||||||
lua_pushinteger(L, L->nci);
|
lua_pushinteger(L, L->nci);
|
||||||
lua_pushinteger(L, (unsigned long)&a);
|
lua_pushinteger(L, (unsigned long)&a);
|
||||||
@@ -918,7 +1041,7 @@ static int string_query (lua_State *L) {
|
|||||||
TString *ts;
|
TString *ts;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
|
for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top.p, ts);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
@@ -933,6 +1056,7 @@ static int tref (lua_State *L) {
|
|||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
lua_pushvalue(L, 1);
|
lua_pushvalue(L, 1);
|
||||||
lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
|
lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
|
||||||
|
cast_void(level); /* to avoid warnings */
|
||||||
lua_assert(lua_gettop(L) == level+1); /* +1 for result */
|
lua_assert(lua_gettop(L) == level+1); /* +1 for result */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -940,6 +1064,7 @@ static int tref (lua_State *L) {
|
|||||||
static int getref (lua_State *L) {
|
static int getref (lua_State *L) {
|
||||||
int level = lua_gettop(L);
|
int level = lua_gettop(L);
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
|
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
|
||||||
|
cast_void(level); /* to avoid warnings */
|
||||||
lua_assert(lua_gettop(L) == level+1);
|
lua_assert(lua_gettop(L) == level+1);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -947,6 +1072,7 @@ static int getref (lua_State *L) {
|
|||||||
static int unref (lua_State *L) {
|
static int unref (lua_State *L) {
|
||||||
int level = lua_gettop(L);
|
int level = lua_gettop(L);
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
|
luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
|
||||||
|
cast_void(level); /* to avoid warnings */
|
||||||
lua_assert(lua_gettop(L) == level);
|
lua_assert(lua_gettop(L) == level);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1103,14 +1229,6 @@ static int doremote (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int int2fb_aux (lua_State *L) {
|
|
||||||
int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1));
|
|
||||||
lua_pushinteger(L, b);
|
|
||||||
lua_pushinteger(L, (unsigned int)luaO_fb2int(b));
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int log2_aux (lua_State *L) {
|
static int log2_aux (lua_State *L) {
|
||||||
unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
|
unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
|
||||||
lua_pushinteger(L, luaO_ceillog2(x));
|
lua_pushinteger(L, luaO_ceillog2(x));
|
||||||
@@ -1242,10 +1360,19 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void pushcode (lua_State *L, int code) {
|
static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN",
|
||||||
static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
|
"ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"};
|
||||||
"ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"};
|
|
||||||
lua_pushstring(L, codes[code]);
|
/*
|
||||||
|
** Avoid these stat codes from being collected, to avoid possible
|
||||||
|
** memory error when pushing them.
|
||||||
|
*/
|
||||||
|
static void regcodes (lua_State *L) {
|
||||||
|
unsigned int i;
|
||||||
|
for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) {
|
||||||
|
lua_pushboolean(L, 1);
|
||||||
|
lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1407,7 +1534,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
lua_newthread(L1);
|
lua_newthread(L1);
|
||||||
}
|
}
|
||||||
else if EQ("resetthread") {
|
else if EQ("resetthread") {
|
||||||
lua_pushinteger(L1, lua_resetthread(L1));
|
lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */
|
||||||
}
|
}
|
||||||
else if EQ("newuserdata") {
|
else if EQ("newuserdata") {
|
||||||
lua_newuserdata(L1, getnum);
|
lua_newuserdata(L1, getnum);
|
||||||
@@ -1468,7 +1595,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
lua_pushnumber(L1, (lua_Number)getnum);
|
lua_pushnumber(L1, (lua_Number)getnum);
|
||||||
}
|
}
|
||||||
else if EQ("pushstatus") {
|
else if EQ("pushstatus") {
|
||||||
pushcode(L1, status);
|
lua_pushstring(L1, statcodes[status]);
|
||||||
}
|
}
|
||||||
else if EQ("pushstring") {
|
else if EQ("pushstring") {
|
||||||
lua_pushstring(L1, getstring);
|
lua_pushstring(L1, getstring);
|
||||||
@@ -1488,6 +1615,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
else if EQ("pushfstringP") {
|
else if EQ("pushfstringP") {
|
||||||
lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1));
|
lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1));
|
||||||
}
|
}
|
||||||
|
else if EQ("rawget") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_rawget(L1, t);
|
||||||
|
}
|
||||||
else if EQ("rawgeti") {
|
else if EQ("rawgeti") {
|
||||||
int t = getindex;
|
int t = getindex;
|
||||||
lua_rawgeti(L1, t, getnum);
|
lua_rawgeti(L1, t, getnum);
|
||||||
@@ -1496,6 +1627,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
int t = getindex;
|
int t = getindex;
|
||||||
lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
||||||
}
|
}
|
||||||
|
else if EQ("rawset") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_rawset(L1, t);
|
||||||
|
}
|
||||||
|
else if EQ("rawseti") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_rawseti(L1, t, getnum);
|
||||||
|
}
|
||||||
else if EQ("rawsetp") {
|
else if EQ("rawsetp") {
|
||||||
int t = getindex;
|
int t = getindex;
|
||||||
lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
||||||
@@ -1511,6 +1650,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
int nres;
|
int nres;
|
||||||
status = lua_resume(lua_tothread(L1, i), L, getnum, &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("return") {
|
else if EQ("return") {
|
||||||
int n = getnum;
|
int n = getnum;
|
||||||
if (L1 != L) {
|
if (L1 != L) {
|
||||||
@@ -1538,6 +1685,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
const char *s = getstring;
|
const char *s = getstring;
|
||||||
lua_setfield(L1, t, s);
|
lua_setfield(L1, t, s);
|
||||||
}
|
}
|
||||||
|
else if EQ("seti") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_seti(L1, t, getnum);
|
||||||
|
}
|
||||||
else if EQ("setglobal") {
|
else if EQ("setglobal") {
|
||||||
const char *s = getstring;
|
const char *s = getstring;
|
||||||
lua_setglobal(L1, s);
|
lua_setglobal(L1, s);
|
||||||
@@ -1565,6 +1716,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
else if EQ("error") {
|
else if EQ("error") {
|
||||||
lua_error(L1);
|
lua_error(L1);
|
||||||
}
|
}
|
||||||
|
else if EQ("abort") {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
else if EQ("throw") {
|
else if EQ("throw") {
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
static struct X { int x; } x;
|
static struct X { int x; } x;
|
||||||
@@ -1595,8 +1749,12 @@ static struct X { int x; } x;
|
|||||||
else if EQ("tostring") {
|
else if EQ("tostring") {
|
||||||
const char *s = lua_tostring(L1, getindex);
|
const char *s = lua_tostring(L1, getindex);
|
||||||
const char *s1 = lua_pushstring(L1, s);
|
const char *s1 = lua_pushstring(L1, s);
|
||||||
|
cast_void(s1); /* to avoid warnings */
|
||||||
lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
|
lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
|
||||||
}
|
}
|
||||||
|
else if EQ("Ltolstring") {
|
||||||
|
luaL_tolstring(L1, getindex, NULL);
|
||||||
|
}
|
||||||
else if EQ("type") {
|
else if EQ("type") {
|
||||||
lua_pushstring(L1, luaL_typename(L1, getnum));
|
lua_pushstring(L1, luaL_typename(L1, getnum));
|
||||||
}
|
}
|
||||||
@@ -1609,6 +1767,9 @@ static struct X { int x; } x;
|
|||||||
if (n == 0) n = lua_gettop(fs);
|
if (n == 0) n = lua_gettop(fs);
|
||||||
lua_xmove(fs, ts, n);
|
lua_xmove(fs, ts, n);
|
||||||
}
|
}
|
||||||
|
else if EQ("isyieldable") {
|
||||||
|
lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex)));
|
||||||
|
}
|
||||||
else if EQ("yield") {
|
else if EQ("yield") {
|
||||||
return lua_yield(L1, getnum);
|
return lua_yield(L1, getnum);
|
||||||
}
|
}
|
||||||
@@ -1620,6 +1781,9 @@ static struct X { int x; } x;
|
|||||||
else if EQ("toclose") {
|
else if EQ("toclose") {
|
||||||
lua_toclose(L1, getnum);
|
lua_toclose(L1, getnum);
|
||||||
}
|
}
|
||||||
|
else if EQ("closeslot") {
|
||||||
|
lua_closeslot(L1, getnum);
|
||||||
|
}
|
||||||
else luaL_error(L, "unknown instruction %s", buff);
|
else luaL_error(L, "unknown instruction %s", buff);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1651,7 +1815,7 @@ static int Cfunc (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
|
static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
|
||||||
pushcode(L, status);
|
lua_pushstring(L, statcodes[status]);
|
||||||
lua_setglobal(L, "status");
|
lua_setglobal(L, "status");
|
||||||
lua_pushinteger(L, ctx);
|
lua_pushinteger(L, ctx);
|
||||||
lua_setglobal(L, "ctx");
|
lua_setglobal(L, "ctx");
|
||||||
@@ -1764,7 +1928,6 @@ static const struct luaL_Reg tests_funcs[] = {
|
|||||||
{"pobj", gc_printobj},
|
{"pobj", gc_printobj},
|
||||||
{"getref", getref},
|
{"getref", getref},
|
||||||
{"hash", hash_query},
|
{"hash", hash_query},
|
||||||
{"int2fb", int2fb_aux},
|
|
||||||
{"log2", log2_aux},
|
{"log2", log2_aux},
|
||||||
{"limits", get_limits},
|
{"limits", get_limits},
|
||||||
{"listcode", listcode},
|
{"listcode", listcode},
|
||||||
@@ -1789,6 +1952,7 @@ static const struct luaL_Reg tests_funcs[] = {
|
|||||||
{"makeCfunc", makeCfunc},
|
{"makeCfunc", makeCfunc},
|
||||||
{"totalmem", mem_query},
|
{"totalmem", mem_query},
|
||||||
{"alloccount", alloc_count},
|
{"alloccount", alloc_count},
|
||||||
|
{"allocfailnext", alloc_failnext},
|
||||||
{"trick", settrick},
|
{"trick", settrick},
|
||||||
{"udataval", udataval},
|
{"udataval", udataval},
|
||||||
{"unref", unref},
|
{"unref", unref},
|
||||||
@@ -1805,12 +1969,15 @@ static void checkfinalmem (void) {
|
|||||||
|
|
||||||
int luaB_opentests (lua_State *L) {
|
int luaB_opentests (lua_State *L) {
|
||||||
void *ud;
|
void *ud;
|
||||||
|
lua_Alloc f = lua_getallocf(L, &ud);
|
||||||
lua_atpanic(L, &tpanic);
|
lua_atpanic(L, &tpanic);
|
||||||
lua_setwarnf(L, &warnf, L);
|
lua_setwarnf(L, &warnf, L);
|
||||||
|
lua_pushboolean(L, 0);
|
||||||
|
lua_setglobal(L, "_WARN"); /* _WARN = false */
|
||||||
|
regcodes(L);
|
||||||
atexit(checkfinalmem);
|
atexit(checkfinalmem);
|
||||||
lua_assert(lua_getallocf(L, &ud) == debug_realloc);
|
lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol));
|
||||||
lua_assert(ud == cast_voidp(&l_memcontrol));
|
lua_setallocf(L, f, ud); /* exercise this function */
|
||||||
lua_setallocf(L, lua_getallocf(L, NULL), ud);
|
|
||||||
luaL_newlib(L, tests_funcs);
|
luaL_newlib(L, tests_funcs);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,19 +20,9 @@
|
|||||||
|
|
||||||
|
|
||||||
/* turn on assertions */
|
/* turn on assertions */
|
||||||
#undef NDEBUG
|
#define LUAI_ASSERT
|
||||||
#include <assert.h>
|
|
||||||
#define lua_assert(c) assert(c)
|
|
||||||
|
|
||||||
|
|
||||||
/* include opcode names */
|
|
||||||
#define LUAI_DEFOPNAMES
|
|
||||||
|
|
||||||
|
|
||||||
/* compiled with -O0, Lua uses a lot of C stack space... */
|
|
||||||
#undef LUAI_MAXCSTACK
|
|
||||||
#define LUAI_MAXCSTACK 400
|
|
||||||
|
|
||||||
/* to avoid warnings, and to make sure value is really unused */
|
/* to avoid warnings, and to make sure value is really unused */
|
||||||
#define UNUSED(x) (x=0, (void)(x))
|
#define UNUSED(x) (x=0, (void)(x))
|
||||||
|
|
||||||
@@ -56,12 +46,13 @@
|
|||||||
|
|
||||||
/* memory-allocator control variables */
|
/* memory-allocator control variables */
|
||||||
typedef struct Memcontrol {
|
typedef struct Memcontrol {
|
||||||
|
int failnext;
|
||||||
unsigned long numblocks;
|
unsigned long numblocks;
|
||||||
unsigned long total;
|
unsigned long total;
|
||||||
unsigned long maxmem;
|
unsigned long maxmem;
|
||||||
unsigned long memlimit;
|
unsigned long memlimit;
|
||||||
unsigned long countlimit;
|
unsigned long countlimit;
|
||||||
unsigned long objcount[LUA_NUMTAGS];
|
unsigned long objcount[LUA_NUMTYPES];
|
||||||
} Memcontrol;
|
} Memcontrol;
|
||||||
|
|
||||||
LUA_API Memcontrol l_memcontrol;
|
LUA_API Memcontrol l_memcontrol;
|
||||||
@@ -77,7 +68,13 @@ extern void *l_Trick;
|
|||||||
/*
|
/*
|
||||||
** Function to traverse and check all memory used by Lua
|
** Function to traverse and check all memory used by Lua
|
||||||
*/
|
*/
|
||||||
int lua_checkmemory (lua_State *L);
|
LUAI_FUNC 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);
|
||||||
|
|
||||||
|
|
||||||
/* test for lock/unlock */
|
/* test for lock/unlock */
|
||||||
@@ -121,18 +118,34 @@ LUA_API void *debug_realloc (void *ud, void *block,
|
|||||||
#define MINSTRTABSIZE 2
|
#define MINSTRTABSIZE 2
|
||||||
#define MAXIWTHABS 3
|
#define MAXIWTHABS 3
|
||||||
|
|
||||||
|
#define STRCACHE_N 23
|
||||||
|
#define STRCACHE_M 5
|
||||||
|
|
||||||
|
#undef LUAI_USER_ALIGNMENT_T
|
||||||
|
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** This one is not compatible with tests for opcode optimizations,
|
||||||
|
** as it blocks some optimizations
|
||||||
|
#define MAXINDEXRK 0
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
/* make stack-overflow tests run faster */
|
/* make stack-overflow tests run faster */
|
||||||
#undef LUAI_MAXSTACK
|
#undef LUAI_MAXSTACK
|
||||||
#define LUAI_MAXSTACK 50000
|
#define LUAI_MAXSTACK 50000
|
||||||
|
|
||||||
|
|
||||||
#undef LUAI_USER_ALIGNMENT_T
|
/* test mode uses more stack space */
|
||||||
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
#undef LUAI_MAXCCALLS
|
||||||
|
#define LUAI_MAXCCALLS 180
|
||||||
|
|
||||||
|
|
||||||
#define STRCACHE_N 23
|
/* force Lua to use its own implementations */
|
||||||
#define STRCACHE_M 5
|
#undef lua_strx2number
|
||||||
|
#undef lua_number2strx
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
static const char udatatypename[] = "userdata";
|
static const char udatatypename[] = "userdata";
|
||||||
|
|
||||||
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
|
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
|
||||||
"no value",
|
"no value",
|
||||||
"nil", "boolean", udatatypename, "number",
|
"nil", "boolean", udatatypename, "number",
|
||||||
"string", "table", "function", udatatypename, "thread",
|
"string", "table", "function", udatatypename, "thread",
|
||||||
@@ -102,12 +102,12 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
|
|||||||
|
|
||||||
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
||||||
const TValue *p2, const TValue *p3) {
|
const TValue *p2, const TValue *p3) {
|
||||||
StkId func = L->top;
|
StkId func = L->top.p;
|
||||||
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
||||||
setobj2s(L, func + 1, p1); /* 1st argument */
|
setobj2s(L, func + 1, p1); /* 1st argument */
|
||||||
setobj2s(L, func + 2, p2); /* 2nd argument */
|
setobj2s(L, func + 2, p2); /* 2nd argument */
|
||||||
setobj2s(L, func + 3, p3); /* 3rd argument */
|
setobj2s(L, func + 3, p3); /* 3rd argument */
|
||||||
L->top = func + 4;
|
L->top.p = func + 4;
|
||||||
/* metamethod may yield only when called from Lua code */
|
/* metamethod may yield only when called from Lua code */
|
||||||
if (isLuacode(L->ci))
|
if (isLuacode(L->ci))
|
||||||
luaD_call(L, func, 0);
|
luaD_call(L, func, 0);
|
||||||
@@ -119,18 +119,18 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
|||||||
void luaT_callTMres (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) {
|
const TValue *p2, StkId res) {
|
||||||
ptrdiff_t result = savestack(L, res);
|
ptrdiff_t result = savestack(L, res);
|
||||||
StkId func = L->top;
|
StkId func = L->top.p;
|
||||||
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
||||||
setobj2s(L, func + 1, p1); /* 1st argument */
|
setobj2s(L, func + 1, p1); /* 1st argument */
|
||||||
setobj2s(L, func + 2, p2); /* 2nd argument */
|
setobj2s(L, func + 2, p2); /* 2nd argument */
|
||||||
L->top += 3;
|
L->top.p += 3;
|
||||||
/* metamethod may yield only when called from Lua code */
|
/* metamethod may yield only when called from Lua code */
|
||||||
if (isLuacode(L->ci))
|
if (isLuacode(L->ci))
|
||||||
luaD_call(L, func, 1);
|
luaD_call(L, func, 1);
|
||||||
else
|
else
|
||||||
luaD_callnoyield(L, func, 1);
|
luaD_callnoyield(L, func, 1);
|
||||||
res = restorestack(L, result);
|
res = restorestack(L, result);
|
||||||
setobjs2s(L, res, --L->top); /* move result to its place */
|
setobjs2s(L, res, --L->top.p); /* move result to its place */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -147,11 +147,8 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|||||||
|
|
||||||
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
StkId res, TMS event) {
|
StkId res, TMS event) {
|
||||||
if (!callbinTM(L, p1, p2, res, event)) {
|
if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case TM_CONCAT:
|
|
||||||
luaG_concaterror(L, p1, p2);
|
|
||||||
/* call never returns, but to avoid warnings: *//* FALLTHROUGH */
|
|
||||||
case TM_BAND: case TM_BOR: case TM_BXOR:
|
case TM_BAND: case TM_BOR: case TM_BXOR:
|
||||||
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
||||||
if (ttisnumber(p1) && ttisnumber(p2))
|
if (ttisnumber(p1) && ttisnumber(p2))
|
||||||
@@ -167,9 +164,17 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
|
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
StkId res, int inv, TMS event) {
|
int flip, StkId res, TMS event) {
|
||||||
if (inv)
|
if (flip)
|
||||||
luaT_trybinTM(L, p2, p1, res, event);
|
luaT_trybinTM(L, p2, p1, res, event);
|
||||||
else
|
else
|
||||||
luaT_trybinTM(L, p1, p2, res, event);
|
luaT_trybinTM(L, p1, p2, res, event);
|
||||||
@@ -177,24 +182,33 @@ void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|||||||
|
|
||||||
|
|
||||||
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
||||||
int inv, StkId res, TMS event) {
|
int flip, StkId res, TMS event) {
|
||||||
TValue aux;
|
TValue aux;
|
||||||
setivalue(&aux, i2);
|
setivalue(&aux, i2);
|
||||||
luaT_trybinassocTM(L, p1, &aux, res, inv, event);
|
luaT_trybinassocTM(L, p1, &aux, flip, res, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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,
|
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
TMS event) {
|
TMS event) {
|
||||||
if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
|
if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */
|
||||||
return !l_isfalse(s2v(L->top));
|
return !l_isfalse(s2v(L->top.p));
|
||||||
#if defined(LUA_COMPAT_LT_LE)
|
#if defined(LUA_COMPAT_LT_LE)
|
||||||
else if (event == TM_LE) {
|
else if (event == TM_LE) {
|
||||||
/* try '!(p2 < p1)' for '(p1 <= p2)' */
|
/* try '!(p2 < p1)' for '(p1 <= p2)' */
|
||||||
L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
|
L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
|
||||||
if (callbinTM(L, p2, p1, L->top, TM_LT)) {
|
if (callbinTM(L, p2, p1, L->top.p, TM_LT)) {
|
||||||
L->ci->callstatus ^= CIST_LEQ; /* clear mark */
|
L->ci->callstatus ^= CIST_LEQ; /* clear mark */
|
||||||
return l_isfalse(s2v(L->top));
|
return l_isfalse(s2v(L->top.p));
|
||||||
}
|
}
|
||||||
/* else error will remove this 'ci'; no need to clear mark */
|
/* else error will remove this 'ci'; no need to clear mark */
|
||||||
}
|
}
|
||||||
@@ -205,14 +219,14 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|||||||
|
|
||||||
|
|
||||||
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
||||||
int inv, int isfloat, TMS event) {
|
int flip, int isfloat, TMS event) {
|
||||||
TValue aux; const TValue *p2;
|
TValue aux; const TValue *p2;
|
||||||
if (isfloat) {
|
if (isfloat) {
|
||||||
setfltvalue(&aux, cast_num(v2));
|
setfltvalue(&aux, cast_num(v2));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
setivalue(&aux, v2);
|
setivalue(&aux, v2);
|
||||||
if (inv) { /* arguments were exchanged? */
|
if (flip) { /* arguments were exchanged? */
|
||||||
p2 = p1; p1 = &aux; /* correct them */
|
p2 = p1; p1 = &aux; /* correct them */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -224,20 +238,20 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
|||||||
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
|
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
|
||||||
const Proto *p) {
|
const Proto *p) {
|
||||||
int i;
|
int i;
|
||||||
int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */
|
int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
|
||||||
int nextra = actual - nfixparams; /* number of extra arguments */
|
int nextra = actual - nfixparams; /* number of extra arguments */
|
||||||
ci->u.l.nextraargs = nextra;
|
ci->u.l.nextraargs = nextra;
|
||||||
checkstackGC(L, p->maxstacksize + 1);
|
luaD_checkstack(L, p->maxstacksize + 1);
|
||||||
/* copy function to the top of the stack */
|
/* copy function to the top of the stack */
|
||||||
setobjs2s(L, L->top++, ci->func);
|
setobjs2s(L, L->top.p++, ci->func.p);
|
||||||
/* move fixed parameters to the top of the stack */
|
/* move fixed parameters to the top of the stack */
|
||||||
for (i = 1; i <= nfixparams; i++) {
|
for (i = 1; i <= nfixparams; i++) {
|
||||||
setobjs2s(L, L->top++, ci->func + i);
|
setobjs2s(L, L->top.p++, ci->func.p + i);
|
||||||
setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */
|
setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
|
||||||
}
|
}
|
||||||
ci->func += actual + 1;
|
ci->func.p += actual + 1;
|
||||||
ci->top += actual + 1;
|
ci->top.p += actual + 1;
|
||||||
lua_assert(L->top <= ci->top && ci->top <= L->stack_last);
|
lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -246,11 +260,11 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
|
|||||||
int nextra = ci->u.l.nextraargs;
|
int nextra = ci->u.l.nextraargs;
|
||||||
if (wanted < 0) {
|
if (wanted < 0) {
|
||||||
wanted = nextra; /* get all extra arguments available */
|
wanted = nextra; /* get all extra arguments available */
|
||||||
checkstackp(L, nextra, where); /* ensure stack space */
|
checkstackGCp(L, nextra, where); /* ensure stack space */
|
||||||
L->top = where + nextra; /* next instruction will need top */
|
L->top.p = where + nextra; /* next instruction will need top */
|
||||||
}
|
}
|
||||||
for (i = 0; i < wanted && i < nextra; i++)
|
for (i = 0; i < wanted && i < nextra; i++)
|
||||||
setobjs2s(L, where + i, ci->func - nextra + i);
|
setobjs2s(L, where + i, ci->func.p - nextra + i);
|
||||||
for (; i < wanted; i++) /* complete required results with nil */
|
for (; i < wanted; i++) /* complete required results with nil */
|
||||||
setnilvalue(s2v(where + i));
|
setnilvalue(s2v(where + i));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,15 @@ typedef enum {
|
|||||||
} TMS;
|
} TMS;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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'.)
|
||||||
|
*/
|
||||||
|
#define maskflags (~(~0u << (TM_EQ + 1)))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Test whether there is no tagmethod.
|
** Test whether there is no tagmethod.
|
||||||
** (Because tagmethods use raw accesses, the result may be an "empty" nil.)
|
** (Because tagmethods use raw accesses, the result may be an "empty" nil.)
|
||||||
@@ -59,7 +68,7 @@ typedef enum {
|
|||||||
|
|
||||||
#define ttypename(x) luaT_typenames_[(x) + 1]
|
#define ttypename(x) luaT_typenames_[(x) + 1]
|
||||||
|
|
||||||
LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTAGS];)
|
LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];)
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
|
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
|
||||||
@@ -75,8 +84,9 @@ LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f,
|
|||||||
const TValue *p1, const TValue *p2, StkId p3);
|
const TValue *p1, const TValue *p2, StkId p3);
|
||||||
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
StkId res, TMS event);
|
StkId res, TMS event);
|
||||||
|
LUAI_FUNC void luaT_tryconcatTM (lua_State *L);
|
||||||
LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1,
|
LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2, StkId res, int inv, TMS event);
|
const TValue *p2, int inv, StkId res, TMS event);
|
||||||
LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
||||||
int inv, StkId res, TMS event);
|
int inv, StkId res, TMS event);
|
||||||
LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
|
LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
|
||||||
|
|||||||
@@ -37,6 +37,26 @@ static lua_State *globalL = NULL;
|
|||||||
static const char *progname = LUA_PROGNAME;
|
static const char *progname = LUA_PROGNAME;
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(LUA_USE_POSIX) /* { */
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Use 'sigaction' when available.
|
||||||
|
*/
|
||||||
|
static void setsignal (int sig, void (*handler)(int)) {
|
||||||
|
struct sigaction sa;
|
||||||
|
sa.sa_handler = handler;
|
||||||
|
sa.sa_flags = 0;
|
||||||
|
sigemptyset(&sa.sa_mask); /* do not mask any signal */
|
||||||
|
sigaction(sig, &sa, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
|
#define setsignal signal
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Hook set by signal function to stop the interpreter.
|
** Hook set by signal function to stop the interpreter.
|
||||||
*/
|
*/
|
||||||
@@ -54,8 +74,9 @@ static void lstop (lua_State *L, lua_Debug *ar) {
|
|||||||
** interpreter.
|
** interpreter.
|
||||||
*/
|
*/
|
||||||
static void laction (int i) {
|
static void laction (int i) {
|
||||||
signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
|
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
|
||||||
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
|
||||||
|
lua_sethook(globalL, lstop, flag, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,13 +89,15 @@ static void print_usage (const char *badoption) {
|
|||||||
lua_writestringerror(
|
lua_writestringerror(
|
||||||
"usage: %s [options] [script [args]]\n"
|
"usage: %s [options] [script [args]]\n"
|
||||||
"Available options are:\n"
|
"Available options are:\n"
|
||||||
" -e stat execute string 'stat'\n"
|
" -e stat execute string 'stat'\n"
|
||||||
" -i enter interactive mode after executing 'script'\n"
|
" -i enter interactive mode after executing 'script'\n"
|
||||||
" -l name require library 'name' into global 'name'\n"
|
" -l mod require library 'mod' into global 'mod'\n"
|
||||||
" -v show version information\n"
|
" -l g=mod require library 'mod' into global 'g'\n"
|
||||||
" -E ignore environment variables\n"
|
" -v show version information\n"
|
||||||
" -- stop handling options\n"
|
" -E ignore environment variables\n"
|
||||||
" - stop handling options and execute stdin\n"
|
" -W turn warnings on\n"
|
||||||
|
" -- stop handling options\n"
|
||||||
|
" - stop handling options and execute stdin\n"
|
||||||
,
|
,
|
||||||
progname);
|
progname);
|
||||||
}
|
}
|
||||||
@@ -92,12 +115,13 @@ static void l_message (const char *pname, const char *msg) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Check whether 'status' is not OK and, if so, prints the error
|
** 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
|
** message on the top of the stack.
|
||||||
** is a string, as it was either generated by Lua or by 'msghandler'.
|
|
||||||
*/
|
*/
|
||||||
static int report (lua_State *L, int status) {
|
static int report (lua_State *L, int status) {
|
||||||
if (status != LUA_OK) {
|
if (status != LUA_OK) {
|
||||||
const char *msg = lua_tostring(L, -1);
|
const char *msg = lua_tostring(L, -1);
|
||||||
|
if (msg == NULL)
|
||||||
|
msg = "(error message not a string)";
|
||||||
l_message(progname, msg);
|
l_message(progname, msg);
|
||||||
lua_pop(L, 1); /* remove message */
|
lua_pop(L, 1); /* remove message */
|
||||||
}
|
}
|
||||||
@@ -133,9 +157,9 @@ static int docall (lua_State *L, int narg, int nres) {
|
|||||||
lua_pushcfunction(L, msghandler); /* push message handler */
|
lua_pushcfunction(L, msghandler); /* push message handler */
|
||||||
lua_insert(L, base); /* put it under function and args */
|
lua_insert(L, base); /* put it under function and args */
|
||||||
globalL = L; /* to be available to 'laction' */
|
globalL = L; /* to be available to 'laction' */
|
||||||
signal(SIGINT, laction); /* set C-signal handler */
|
setsignal(SIGINT, laction); /* set C-signal handler */
|
||||||
status = lua_pcall(L, narg, nres, base);
|
status = lua_pcall(L, narg, nres, base);
|
||||||
signal(SIGINT, SIG_DFL); /* reset C-signal handler */
|
setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */
|
||||||
lua_remove(L, base); /* remove message handler from the stack */
|
lua_remove(L, base); /* remove message handler from the stack */
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -154,10 +178,11 @@ static void print_version (void) {
|
|||||||
** to the script (everything after 'script') go to positive indices;
|
** to the script (everything after 'script') go to positive indices;
|
||||||
** other arguments (before the script name) go to negative indices.
|
** other arguments (before the script name) go to negative indices.
|
||||||
** If there is no script name, assume interpreter's name as base.
|
** If there is no script name, assume interpreter's name as base.
|
||||||
|
** (If there is no interpreter's name either, 'script' is -1, so
|
||||||
|
** table sizes are zero.)
|
||||||
*/
|
*/
|
||||||
static void createargtable (lua_State *L, char **argv, int argc, int script) {
|
static void createargtable (lua_State *L, char **argv, int argc, int script) {
|
||||||
int i, narg;
|
int i, narg;
|
||||||
if (script == argc) script = 0; /* no script name? */
|
|
||||||
narg = argc - (script + 1); /* number of positive indices */
|
narg = argc - (script + 1); /* number of positive indices */
|
||||||
lua_createtable(L, narg, script + 1);
|
lua_createtable(L, narg, script + 1);
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
@@ -185,16 +210,30 @@ static int dostring (lua_State *L, const char *s, const char *name) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Calls 'require(name)' and stores the result in a global variable
|
** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
|
||||||
** with the given name.
|
** 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, const char *name) {
|
static int dolibrary (lua_State *L, char *globname) {
|
||||||
int status;
|
int status;
|
||||||
|
char *suffix = NULL;
|
||||||
|
char *modname = strchr(globname, '=');
|
||||||
|
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 '=' */
|
||||||
|
}
|
||||||
lua_getglobal(L, "require");
|
lua_getglobal(L, "require");
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, modname);
|
||||||
status = docall(L, 1, 1); /* call 'require(name)' */
|
status = docall(L, 1, 1); /* call 'require(modname)' */
|
||||||
if (status == LUA_OK)
|
if (status == LUA_OK) {
|
||||||
lua_setglobal(L, name); /* global[name] = require return */
|
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);
|
return report(L, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,14 +278,23 @@ static int handle_script (lua_State *L, char **argv) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Traverses all arguments from 'argv', returning a mask with those
|
** Traverses all arguments from 'argv', returning a mask with those
|
||||||
** needed before running any Lua code (or an error code if it finds
|
** needed before running any Lua code or an error code if it finds any
|
||||||
** any invalid argument). 'first' returns the first not-handled argument
|
** invalid argument. In case of error, 'first' is the index of the bad
|
||||||
** (either the script name or a bad argument in case of error).
|
** argument. Otherwise, 'first' is -1 if there is no program name,
|
||||||
|
** 0 if there is no script name, or the index of the script name.
|
||||||
*/
|
*/
|
||||||
static int collectargs (char **argv, int *first) {
|
static int collectargs (char **argv, int *first) {
|
||||||
int args = 0;
|
int args = 0;
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; argv[i] != NULL; i++) {
|
if (argv[0] != NULL) { /* is there a program name? */
|
||||||
|
if (argv[0][0]) /* not empty? */
|
||||||
|
progname = argv[0]; /* save it */
|
||||||
|
}
|
||||||
|
else { /* no program name */
|
||||||
|
*first = -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for (i = 1; argv[i] != NULL; i++) { /* handle arguments */
|
||||||
*first = i;
|
*first = i;
|
||||||
if (argv[i][0] != '-') /* not an option? */
|
if (argv[i][0] != '-') /* not an option? */
|
||||||
return args; /* stop handling options */
|
return args; /* stop handling options */
|
||||||
@@ -254,19 +302,24 @@ static int collectargs (char **argv, int *first) {
|
|||||||
case '-': /* '--' */
|
case '-': /* '--' */
|
||||||
if (argv[i][2] != '\0') /* extra characters after '--'? */
|
if (argv[i][2] != '\0') /* extra characters after '--'? */
|
||||||
return has_error; /* invalid option */
|
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;
|
return args;
|
||||||
case '\0': /* '-' */
|
case '\0': /* '-' */
|
||||||
return args; /* script "name" is '-' */
|
return args; /* script "name" is '-' */
|
||||||
case 'E':
|
case 'E':
|
||||||
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
if (argv[i][2] != '\0') /* extra characters? */
|
||||||
return has_error; /* invalid option */
|
return has_error; /* invalid option */
|
||||||
args |= has_E;
|
args |= has_E;
|
||||||
break;
|
break;
|
||||||
|
case 'W':
|
||||||
|
if (argv[i][2] != '\0') /* extra characters? */
|
||||||
|
return has_error; /* invalid option */
|
||||||
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
|
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
|
||||||
case 'v':
|
case 'v':
|
||||||
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
if (argv[i][2] != '\0') /* extra characters? */
|
||||||
return has_error; /* invalid option */
|
return has_error; /* invalid option */
|
||||||
args |= has_v;
|
args |= has_v;
|
||||||
break;
|
break;
|
||||||
@@ -283,13 +336,14 @@ static int collectargs (char **argv, int *first) {
|
|||||||
return has_error;
|
return has_error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*first = i; /* no script name */
|
*first = 0; /* no script name */
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Processes options 'e' and 'l', which involve running Lua code.
|
** Processes options 'e' and 'l', which involve running Lua code, and
|
||||||
|
** 'W', which also affects the state.
|
||||||
** Returns 0 if some code raises an error.
|
** Returns 0 if some code raises an error.
|
||||||
*/
|
*/
|
||||||
static int runargs (lua_State *L, char **argv, int n) {
|
static int runargs (lua_State *L, char **argv, int n) {
|
||||||
@@ -297,15 +351,21 @@ static int runargs (lua_State *L, char **argv, int n) {
|
|||||||
for (i = 1; i < n; i++) {
|
for (i = 1; i < n; i++) {
|
||||||
int option = argv[i][1];
|
int option = argv[i][1];
|
||||||
lua_assert(argv[i][0] == '-'); /* already checked */
|
lua_assert(argv[i][0] == '-'); /* already checked */
|
||||||
if (option == 'e' || option == 'l') {
|
switch (option) {
|
||||||
int status;
|
case 'e': case 'l': {
|
||||||
const char *extra = argv[i] + 2; /* both options need an argument */
|
int status;
|
||||||
if (*extra == '\0') extra = argv[++i];
|
char *extra = argv[i] + 2; /* both options need an argument */
|
||||||
lua_assert(extra != NULL);
|
if (*extra == '\0') extra = argv[++i];
|
||||||
status = (option == 'e')
|
lua_assert(extra != NULL);
|
||||||
? dostring(L, extra, "=(command line)")
|
status = (option == 'e')
|
||||||
: dolibrary(L, extra);
|
? dostring(L, extra, "=(command line)")
|
||||||
if (status != LUA_OK) return 0;
|
: dolibrary(L, extra);
|
||||||
|
if (status != LUA_OK) return 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'W':
|
||||||
|
lua_warning(L, "@on", 0); /* warnings on */
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -403,14 +463,18 @@ static int handle_luainit (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Returns the string to be used as a prompt by the interpreter.
|
** Return the string to be used as a prompt by the interpreter. Leave
|
||||||
|
** the string (or nil, if using the default value) on the stack, to keep
|
||||||
|
** it anchored.
|
||||||
*/
|
*/
|
||||||
static const char *get_prompt (lua_State *L, int firstline) {
|
static const char *get_prompt (lua_State *L, int firstline) {
|
||||||
const char *p;
|
if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
|
||||||
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
|
return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
|
||||||
p = lua_tostring(L, -1);
|
else { /* apply 'tostring' over the value */
|
||||||
if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
|
const char *p = luaL_tolstring(L, -1, NULL);
|
||||||
return p;
|
lua_remove(L, -2); /* remove original value */
|
||||||
|
return p;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mark in error messages for incomplete statements */
|
/* mark in error messages for incomplete statements */
|
||||||
@@ -427,10 +491,8 @@ static int incomplete (lua_State *L, int status) {
|
|||||||
if (status == LUA_ERRSYNTAX) {
|
if (status == LUA_ERRSYNTAX) {
|
||||||
size_t lmsg;
|
size_t lmsg;
|
||||||
const char *msg = lua_tolstring(L, -1, &lmsg);
|
const char *msg = lua_tolstring(L, -1, &lmsg);
|
||||||
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
|
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0)
|
||||||
lua_pop(L, 1);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0; /* else... */
|
return 0; /* else... */
|
||||||
}
|
}
|
||||||
@@ -445,9 +507,9 @@ static int pushline (lua_State *L, int firstline) {
|
|||||||
size_t l;
|
size_t l;
|
||||||
const char *prmt = get_prompt(L, firstline);
|
const char *prmt = get_prompt(L, firstline);
|
||||||
int readstatus = lua_readline(L, b, prmt);
|
int readstatus = lua_readline(L, b, prmt);
|
||||||
if (readstatus == 0)
|
|
||||||
return 0; /* no input (prompt will be popped by caller) */
|
|
||||||
lua_pop(L, 1); /* remove prompt */
|
lua_pop(L, 1); /* remove prompt */
|
||||||
|
if (readstatus == 0)
|
||||||
|
return 0; /* no input */
|
||||||
l = strlen(b);
|
l = strlen(b);
|
||||||
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
||||||
b[--l] = '\0'; /* remove it */
|
b[--l] = '\0'; /* remove it */
|
||||||
@@ -489,8 +551,9 @@ static int multiline (lua_State *L) {
|
|||||||
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
||||||
if (!incomplete(L, status) || !pushline(L, 0)) {
|
if (!incomplete(L, status) || !pushline(L, 0)) {
|
||||||
lua_saveline(L, line); /* keep history */
|
lua_saveline(L, line); /* keep history */
|
||||||
return status; /* cannot or should not try to add continuation line */
|
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_pushliteral(L, "\n"); /* add newline... */
|
||||||
lua_insert(L, -2); /* ...between the two lines */
|
lua_insert(L, -2); /* ...between the two lines */
|
||||||
lua_concat(L, 3); /* join them */
|
lua_concat(L, 3); /* join them */
|
||||||
@@ -565,8 +628,8 @@ static int pmain (lua_State *L) {
|
|||||||
char **argv = (char **)lua_touserdata(L, 2);
|
char **argv = (char **)lua_touserdata(L, 2);
|
||||||
int script;
|
int script;
|
||||||
int args = collectargs(argv, &script);
|
int args = collectargs(argv, &script);
|
||||||
|
int optlim = (script > 0) ? script : argc; /* first argv not an option */
|
||||||
luaL_checkversion(L); /* check that interpreter has correct version */
|
luaL_checkversion(L); /* check that interpreter has correct version */
|
||||||
if (argv[0] && argv[0][0]) progname = argv[0];
|
|
||||||
if (args == has_error) { /* bad arg? */
|
if (args == has_error) { /* bad arg? */
|
||||||
print_usage(argv[script]); /* 'script' has index of bad arg. */
|
print_usage(argv[script]); /* 'script' has index of bad arg. */
|
||||||
return 0;
|
return 0;
|
||||||
@@ -579,19 +642,21 @@ static int pmain (lua_State *L) {
|
|||||||
}
|
}
|
||||||
luaL_openlibs(L); /* open standard libraries */
|
luaL_openlibs(L); /* open standard libraries */
|
||||||
createargtable(L, argv, argc, script); /* create table 'arg' */
|
createargtable(L, argv, argc, script); /* create table 'arg' */
|
||||||
lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */
|
lua_gc(L, LUA_GCRESTART); /* start GC... */
|
||||||
|
lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */
|
||||||
if (!(args & has_E)) { /* no option '-E'? */
|
if (!(args & has_E)) { /* no option '-E'? */
|
||||||
if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
|
if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
|
||||||
return 0; /* error running LUA_INIT */
|
return 0; /* error running LUA_INIT */
|
||||||
}
|
}
|
||||||
if (!runargs(L, argv, script)) /* execute arguments -e and -l */
|
if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */
|
||||||
return 0; /* something failed */
|
return 0; /* something failed */
|
||||||
if (script < argc && /* execute main script (if there is one) */
|
if (script > 0) { /* execute main script (if there is one) */
|
||||||
handle_script(L, argv + script) != LUA_OK)
|
if (handle_script(L, argv + script) != LUA_OK)
|
||||||
return 0;
|
return 0; /* interrupt in case of error */
|
||||||
|
}
|
||||||
if (args & has_i) /* -i option? */
|
if (args & has_i) /* -i option? */
|
||||||
doREPL(L); /* do read-eval-print loop */
|
doREPL(L); /* do read-eval-print loop */
|
||||||
else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
|
else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */
|
||||||
if (lua_stdin_is_tty()) { /* running in interactive mode? */
|
if (lua_stdin_is_tty()) { /* running in interactive mode? */
|
||||||
print_version();
|
print_version();
|
||||||
doREPL(L); /* do read-eval-print loop */
|
doREPL(L); /* do read-eval-print loop */
|
||||||
@@ -610,6 +675,7 @@ int main (int argc, char **argv) {
|
|||||||
l_message(argv[0], "cannot create state: not enough memory");
|
l_message(argv[0], "cannot create state: not enough memory");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
lua_gc(L, LUA_GCSTOP); /* stop GC while building state */
|
||||||
lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
|
lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
|
||||||
lua_pushinteger(L, argc); /* 1st argument */
|
lua_pushinteger(L, argc); /* 1st argument */
|
||||||
lua_pushlightuserdata(L, argv); /* 2nd argument */
|
lua_pushlightuserdata(L, argv); /* 2nd argument */
|
||||||
|
|||||||
@@ -18,14 +18,14 @@
|
|||||||
|
|
||||||
#define LUA_VERSION_MAJOR "5"
|
#define LUA_VERSION_MAJOR "5"
|
||||||
#define LUA_VERSION_MINOR "4"
|
#define LUA_VERSION_MINOR "4"
|
||||||
#define LUA_VERSION_RELEASE "0"
|
#define LUA_VERSION_RELEASE "8"
|
||||||
|
|
||||||
#define LUA_VERSION_NUM 504
|
#define LUA_VERSION_NUM 504
|
||||||
#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0)
|
#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 8)
|
||||||
|
|
||||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2019 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_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
||||||
|
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ typedef struct lua_State lua_State;
|
|||||||
#define LUA_TUSERDATA 7
|
#define LUA_TUSERDATA 7
|
||||||
#define LUA_TTHREAD 8
|
#define LUA_TTHREAD 8
|
||||||
|
|
||||||
#define LUA_NUMTAGS 9
|
#define LUA_NUMTYPES 9
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -131,6 +131,16 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
|
|||||||
typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
|
typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Type used by the debug API to collect debug information
|
||||||
|
*/
|
||||||
|
typedef struct lua_Debug lua_Debug;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Functions to be called by the debugger in specific events
|
||||||
|
*/
|
||||||
|
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -153,7 +163,8 @@ extern const char lua_ident[];
|
|||||||
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
|
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
|
||||||
LUA_API void (lua_close) (lua_State *L);
|
LUA_API void (lua_close) (lua_State *L);
|
||||||
LUA_API lua_State *(lua_newthread) (lua_State *L);
|
LUA_API lua_State *(lua_newthread) (lua_State *L);
|
||||||
LUA_API int (lua_resetthread) (lua_State *L);
|
LUA_API int (lua_closethread) (lua_State *L, lua_State *from);
|
||||||
|
LUA_API int (lua_resetthread) (lua_State *L); /* Deprecated! */
|
||||||
|
|
||||||
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
|
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
|
||||||
|
|
||||||
@@ -347,7 +358,8 @@ 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 lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
||||||
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
||||||
|
|
||||||
LUA_API void (lua_toclose) (lua_State *L, int idx);
|
LUA_API void (lua_toclose) (lua_State *L, int idx);
|
||||||
|
LUA_API void (lua_closeslot) (lua_State *L, int idx);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -412,6 +424,8 @@ LUA_API void (lua_toclose) (lua_State *L, int idx);
|
|||||||
#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
|
#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
|
||||||
#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
|
#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
|
||||||
|
|
||||||
|
#define LUA_NUMTAGS LUA_NUMTYPES
|
||||||
|
|
||||||
/* }============================================================== */
|
/* }============================================================== */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -439,12 +453,6 @@ LUA_API void (lua_toclose) (lua_State *L, int idx);
|
|||||||
#define LUA_MASKLINE (1 << LUA_HOOKLINE)
|
#define LUA_MASKLINE (1 << LUA_HOOKLINE)
|
||||||
#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
|
#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
|
||||||
|
|
||||||
typedef struct lua_Debug lua_Debug; /* activation record */
|
|
||||||
|
|
||||||
|
|
||||||
/* Functions to be called by the debugger in specific events */
|
|
||||||
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
|
||||||
|
|
||||||
|
|
||||||
LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
|
LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
|
||||||
LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
|
LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
|
||||||
@@ -462,6 +470,7 @@ LUA_API lua_Hook (lua_gethook) (lua_State *L);
|
|||||||
LUA_API int (lua_gethookmask) (lua_State *L);
|
LUA_API int (lua_gethookmask) (lua_State *L);
|
||||||
LUA_API int (lua_gethookcount) (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 {
|
struct lua_Debug {
|
||||||
int event;
|
int event;
|
||||||
@@ -488,7 +497,7 @@ struct lua_Debug {
|
|||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Copyright (C) 1994-2019 Lua.org, PUC-Rio.
|
* Copyright (C) 1994-2025 Lua.org, PUC-Rio.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
|
|||||||
@@ -16,13 +16,13 @@
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
** General Configuration File for Lua
|
** General Configuration File for Lua
|
||||||
**
|
**
|
||||||
** Some definitions here can be changed externally, through the
|
** Some definitions here can be changed externally, through the compiler
|
||||||
** compiler (e.g., with '-D' options). Those are protected by
|
** (e.g., with '-D' options): They are commented out or protected
|
||||||
** '#if !defined' guards. However, several other definitions should
|
** by '#if !defined' guards. However, several other definitions
|
||||||
** be changed directly here, either because they affect the Lua
|
** should be changed directly here, either because they affect the
|
||||||
** ABI (by making the changes here, you ensure that all software
|
** Lua ABI (by making the changes here, you ensure that all software
|
||||||
** connected to Lua, such as C libraries, will be compiled with the
|
** connected to Lua, such as C libraries, will be compiled with the same
|
||||||
** same configuration); or because they are seldom changed.
|
** configuration); or because they are seldom changed.
|
||||||
**
|
**
|
||||||
** Search for "@@" to find all configurable definitions.
|
** Search for "@@" to find all configurable definitions.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
@@ -36,21 +36,6 @@
|
|||||||
** =====================================================================
|
** =====================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAI_MAXCSTACK defines the maximum depth for nested calls and
|
|
||||||
** also limits the maximum depth of other recursive algorithms in
|
|
||||||
** the implementation, such as syntactic analysis. A value too
|
|
||||||
** large may allow the interpreter to crash (C-stack overflow).
|
|
||||||
** The default value seems ok for regular machines, but may be
|
|
||||||
** too high for restricted hardware.
|
|
||||||
** The test file 'cstack.lua' may help finding a good limit.
|
|
||||||
** (It will crash with a limit too high.)
|
|
||||||
*/
|
|
||||||
#if !defined(LUAI_MAXCSTACK)
|
|
||||||
#define LUAI_MAXCSTACK 2200
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_USE_C89 controls the use of non-ISO-C89 features.
|
@@ LUA_USE_C89 controls the use of non-ISO-C89 features.
|
||||||
** Define it if you want Lua to avoid the use of a few C99 features
|
** Define it if you want Lua to avoid the use of a few C99 features
|
||||||
@@ -85,6 +70,12 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(LUA_USE_IOS)
|
||||||
|
#define LUA_USE_POSIX
|
||||||
|
#define LUA_USE_DLOPEN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits.
|
@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits.
|
||||||
*/
|
*/
|
||||||
@@ -96,26 +87,12 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Configuration for Number types.
|
** Configuration for Number types. These options should not be
|
||||||
|
** set externally, because any other code connected to Lua must
|
||||||
|
** use the same configuration.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats.
|
|
||||||
*/
|
|
||||||
/* #define LUA_32BITS */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for
|
|
||||||
** C89 ('long' and 'double'); Windows always has '__int64', so it does
|
|
||||||
** not need to use this case.
|
|
||||||
*/
|
|
||||||
#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS)
|
|
||||||
#define LUA_C89_NUMBERS
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_INT_TYPE defines the type for Lua integers.
|
@@ LUA_INT_TYPE defines the type for Lua integers.
|
||||||
@@ LUA_FLOAT_TYPE defines the type for Lua floats.
|
@@ LUA_FLOAT_TYPE defines the type for Lua floats.
|
||||||
@@ -136,7 +113,31 @@
|
|||||||
#define LUA_FLOAT_DOUBLE 2
|
#define LUA_FLOAT_DOUBLE 2
|
||||||
#define LUA_FLOAT_LONGDOUBLE 3
|
#define LUA_FLOAT_LONGDOUBLE 3
|
||||||
|
|
||||||
#if defined(LUA_32BITS) /* { */
|
|
||||||
|
/* Default configuration ('long long' and 'double', for 64-bit Lua) */
|
||||||
|
#define LUA_INT_DEFAULT LUA_INT_LONGLONG
|
||||||
|
#define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats.
|
||||||
|
*/
|
||||||
|
#define LUA_32BITS 0
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for
|
||||||
|
** C89 ('long' and 'double'); Windows always has '__int64', so it does
|
||||||
|
** not need to use this case.
|
||||||
|
*/
|
||||||
|
#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS)
|
||||||
|
#define LUA_C89_NUMBERS 1
|
||||||
|
#else
|
||||||
|
#define LUA_C89_NUMBERS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if LUA_32BITS /* { */
|
||||||
/*
|
/*
|
||||||
** 32-bit integers and 'float'
|
** 32-bit integers and 'float'
|
||||||
*/
|
*/
|
||||||
@@ -147,27 +148,22 @@
|
|||||||
#endif
|
#endif
|
||||||
#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT
|
#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT
|
||||||
|
|
||||||
#elif defined(LUA_C89_NUMBERS) /* }{ */
|
#elif LUA_C89_NUMBERS /* }{ */
|
||||||
/*
|
/*
|
||||||
** largest types available for C89 ('long' and 'double')
|
** largest types available for C89 ('long' and 'double')
|
||||||
*/
|
*/
|
||||||
#define LUA_INT_TYPE LUA_INT_LONG
|
#define LUA_INT_TYPE LUA_INT_LONG
|
||||||
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
/* use defaults */
|
||||||
|
|
||||||
|
#define LUA_INT_TYPE LUA_INT_DEFAULT
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** default configuration for 64-bit Lua ('long long' and 'double')
|
|
||||||
*/
|
|
||||||
#if !defined(LUA_INT_TYPE)
|
|
||||||
#define LUA_INT_TYPE LUA_INT_LONGLONG
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(LUA_FLOAT_TYPE)
|
|
||||||
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -261,6 +257,15 @@
|
|||||||
|
|
||||||
#endif
|
#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 "-"
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -388,17 +393,16 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Configuration for Numbers.
|
** Configuration for Numbers (low-level part).
|
||||||
** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_*
|
** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_*
|
||||||
** satisfy your needs.
|
** satisfy your needs.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_NUMBER is the floating-point type used by Lua.
|
|
||||||
@@ LUAI_UACNUMBER is the result of a 'default argument promotion'
|
@@ LUAI_UACNUMBER is the result of a 'default argument promotion'
|
||||||
@@ over a floating number.
|
@@ over a floating number.
|
||||||
@@ l_mathlim(x) corrects limit name 'x' to the proper float type
|
@@ l_floatatt(x) corrects float attribute 'x' to the proper float type
|
||||||
** by prefixing it with one of FLT/DBL/LDBL.
|
** by prefixing it with one of FLT/DBL/LDBL.
|
||||||
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
||||||
@@ LUA_NUMBER_FMT is the format for writing floats.
|
@@ LUA_NUMBER_FMT is the format for writing floats.
|
||||||
@@ -437,7 +441,7 @@
|
|||||||
|
|
||||||
#define LUA_NUMBER float
|
#define LUA_NUMBER float
|
||||||
|
|
||||||
#define l_mathlim(n) (FLT_##n)
|
#define l_floatatt(n) (FLT_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
@@ -453,7 +457,7 @@
|
|||||||
|
|
||||||
#define LUA_NUMBER long double
|
#define LUA_NUMBER long double
|
||||||
|
|
||||||
#define l_mathlim(n) (LDBL_##n)
|
#define l_floatatt(n) (LDBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER long double
|
#define LUAI_UACNUMBER long double
|
||||||
|
|
||||||
@@ -468,7 +472,7 @@
|
|||||||
|
|
||||||
#define LUA_NUMBER double
|
#define LUA_NUMBER double
|
||||||
|
|
||||||
#define l_mathlim(n) (DBL_##n)
|
#define l_floatatt(n) (DBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
@@ -488,18 +492,14 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_INTEGER is the integer type used by Lua.
|
|
||||||
**
|
|
||||||
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
|
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
|
||||||
**
|
|
||||||
@@ LUAI_UACINT is the result of a 'default argument promotion'
|
@@ LUAI_UACINT is the result of a 'default argument promotion'
|
||||||
@@ over a lUA_INTEGER.
|
@@ over a LUA_INTEGER.
|
||||||
@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
|
@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
|
||||||
@@ LUA_INTEGER_FMT is the format for writing integers.
|
@@ LUA_INTEGER_FMT is the format for writing integers.
|
||||||
@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
|
@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
|
||||||
@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER.
|
@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER.
|
||||||
@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED.
|
@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED.
|
||||||
@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED.
|
|
||||||
@@ lua_integer2str converts an integer to a string.
|
@@ lua_integer2str converts an integer to a string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -520,9 +520,6 @@
|
|||||||
#define LUA_UNSIGNED unsigned LUAI_UACINT
|
#define LUA_UNSIGNED unsigned LUAI_UACINT
|
||||||
|
|
||||||
|
|
||||||
#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT)
|
|
||||||
|
|
||||||
|
|
||||||
/* now the variable definitions */
|
/* now the variable definitions */
|
||||||
|
|
||||||
#if LUA_INT_TYPE == LUA_INT_INT /* { int */
|
#if LUA_INT_TYPE == LUA_INT_INT /* { int */
|
||||||
@@ -674,6 +671,34 @@
|
|||||||
#define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
|
#define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macros to improve jump prediction, used mostly for error handling
|
||||||
|
** and debug facilities. (Some macros in the Lua API use these macros.
|
||||||
|
** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your
|
||||||
|
** code.)
|
||||||
|
*/
|
||||||
|
#if !defined(luai_likely)
|
||||||
|
|
||||||
|
#if defined(__GNUC__) && !defined(LUA_NOBUILTIN)
|
||||||
|
#define luai_likely(x) (__builtin_expect(((x) != 0), 1))
|
||||||
|
#define luai_unlikely(x) (__builtin_expect(((x) != 0), 0))
|
||||||
|
#else
|
||||||
|
#define luai_likely(x) (x)
|
||||||
|
#define luai_unlikely(x) (x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -718,7 +743,7 @@
|
|||||||
** CHANGE it if you need a different limit. This limit is arbitrary;
|
** CHANGE it if you need a different limit. This limit is arbitrary;
|
||||||
** its only purpose is to stop Lua from consuming unlimited stack
|
** its only purpose is to stop Lua from consuming unlimited stack
|
||||||
** space (and to reserve some numbers for pseudo-indices).
|
** space (and to reserve some numbers for pseudo-indices).
|
||||||
** (It must fit into max(size_t)/32.)
|
** (It must fit into max(size_t)/32 and max(int)/2.)
|
||||||
*/
|
*/
|
||||||
#if LUAI_IS32INT
|
#if LUAI_IS32INT
|
||||||
#define LUAI_MAXSTACK 1000000
|
#define LUAI_MAXSTACK 1000000
|
||||||
@@ -737,14 +762,15 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
||||||
@@ of a function in debug information.
|
** of a function in debug information.
|
||||||
** CHANGE it if you want a different size.
|
** CHANGE it if you want a different size.
|
||||||
*/
|
*/
|
||||||
#define LUA_IDSIZE 60
|
#define LUA_IDSIZE 60
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
|
@@ LUAL_BUFFERSIZE is the initial buffer size used by the lauxlib
|
||||||
|
** buffer system.
|
||||||
*/
|
*/
|
||||||
#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number)))
|
#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number)))
|
||||||
|
|
||||||
|
|||||||
@@ -49,10 +49,4 @@ LUAMOD_API int (luaopen_package) (lua_State *L);
|
|||||||
LUALIB_API void (luaL_openlibs) (lua_State *L);
|
LUALIB_API void (luaL_openlibs) (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if !defined(lua_assert)
|
|
||||||
#define lua_assert(x) ((void)0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#if !defined(luai_verifycode)
|
#if !defined(luai_verifycode)
|
||||||
#define luai_verifycode(L,b,f) /* empty */
|
#define luai_verifycode(L,f) /* empty */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -44,21 +44,21 @@ static l_noret error (LoadState *S, const char *why) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** All high-level loads go through LoadVector; you can change it to
|
** All high-level loads go through loadVector; you can change it to
|
||||||
** adapt to the endianness of the input
|
** 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,(n)*sizeof((b)[0]))
|
||||||
|
|
||||||
static void LoadBlock (LoadState *S, void *b, size_t size) {
|
static void loadBlock (LoadState *S, void *b, size_t size) {
|
||||||
if (luaZ_read(S->Z, b, size) != 0)
|
if (luaZ_read(S->Z, b, size) != 0)
|
||||||
error(S, "truncated chunk");
|
error(S, "truncated chunk");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define LoadVar(S,x) LoadVector(S,&x,1)
|
#define loadVar(S,x) loadVector(S,&x,1)
|
||||||
|
|
||||||
|
|
||||||
static lu_byte LoadByte (LoadState *S) {
|
static lu_byte loadByte (LoadState *S) {
|
||||||
int b = zgetc(S->Z);
|
int b = zgetc(S->Z);
|
||||||
if (b == EOZ)
|
if (b == EOZ)
|
||||||
error(S, "truncated chunk");
|
error(S, "truncated chunk");
|
||||||
@@ -66,12 +66,12 @@ static lu_byte LoadByte (LoadState *S) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static size_t LoadUnsigned (LoadState *S, size_t limit) {
|
static size_t loadUnsigned (LoadState *S, size_t limit) {
|
||||||
size_t x = 0;
|
size_t x = 0;
|
||||||
int b;
|
int b;
|
||||||
limit >>= 7;
|
limit >>= 7;
|
||||||
do {
|
do {
|
||||||
b = LoadByte(S);
|
b = loadByte(S);
|
||||||
if (x >= limit)
|
if (x >= limit)
|
||||||
error(S, "integer overflow");
|
error(S, "integer overflow");
|
||||||
x = (x << 7) | (b & 0x7f);
|
x = (x << 7) | (b & 0x7f);
|
||||||
@@ -80,98 +80,107 @@ static size_t LoadUnsigned (LoadState *S, size_t limit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static size_t LoadSize (LoadState *S) {
|
static size_t loadSize (LoadState *S) {
|
||||||
return LoadUnsigned(S, ~(size_t)0);
|
return loadUnsigned(S, MAX_SIZET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int LoadInt (LoadState *S) {
|
static int loadInt (LoadState *S) {
|
||||||
return cast_int(LoadUnsigned(S, INT_MAX));
|
return cast_int(loadUnsigned(S, INT_MAX));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static lua_Number LoadNumber (LoadState *S) {
|
static lua_Number loadNumber (LoadState *S) {
|
||||||
lua_Number x;
|
lua_Number x;
|
||||||
LoadVar(S, x);
|
loadVar(S, x);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static lua_Integer LoadInteger (LoadState *S) {
|
static lua_Integer loadInteger (LoadState *S) {
|
||||||
lua_Integer x;
|
lua_Integer x;
|
||||||
LoadVar(S, x);
|
loadVar(S, x);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Load a nullable string
|
** Load a nullable string into prototype 'p'.
|
||||||
*/
|
*/
|
||||||
static TString *LoadStringN (LoadState *S) {
|
static TString *loadStringN (LoadState *S, Proto *p) {
|
||||||
size_t size = LoadSize(S);
|
lua_State *L = S->L;
|
||||||
if (size == 0)
|
TString *ts;
|
||||||
|
size_t size = loadSize(S);
|
||||||
|
if (size == 0) /* no string? */
|
||||||
return NULL;
|
return NULL;
|
||||||
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
|
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
|
||||||
char buff[LUAI_MAXSHORTLEN];
|
char buff[LUAI_MAXSHORTLEN];
|
||||||
LoadVector(S, buff, size);
|
loadVector(S, buff, size); /* load string into buffer */
|
||||||
return luaS_newlstr(S->L, buff, size);
|
ts = luaS_newlstr(L, buff, size); /* create string */
|
||||||
}
|
}
|
||||||
else { /* long string */
|
else { /* long string */
|
||||||
TString *ts = luaS_createlngstrobj(S->L, size);
|
ts = luaS_createlngstrobj(L, size); /* create string */
|
||||||
LoadVector(S, getstr(ts), size); /* load directly in final place */
|
setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */
|
||||||
return ts;
|
luaD_inctop(L);
|
||||||
|
loadVector(S, getlngstr(ts), size); /* load directly in final place */
|
||||||
|
L->top.p--; /* pop string */
|
||||||
}
|
}
|
||||||
|
luaC_objbarrier(L, p, ts);
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Load a non-nullable string.
|
** Load a non-nullable string into prototype 'p'.
|
||||||
*/
|
*/
|
||||||
static TString *LoadString (LoadState *S) {
|
static TString *loadString (LoadState *S, Proto *p) {
|
||||||
TString *st = LoadStringN(S);
|
TString *st = loadStringN(S, p);
|
||||||
if (st == NULL)
|
if (st == NULL)
|
||||||
error(S, "bad format for constant string");
|
error(S, "bad format for constant string");
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void LoadCode (LoadState *S, Proto *f) {
|
static void loadCode (LoadState *S, Proto *f) {
|
||||||
int n = LoadInt(S);
|
int n = loadInt(S);
|
||||||
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
||||||
f->sizecode = n;
|
f->sizecode = n;
|
||||||
LoadVector(S, f->code, n);
|
loadVector(S, f->code, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void LoadFunction(LoadState *S, Proto *f, TString *psource);
|
static void loadFunction(LoadState *S, Proto *f, TString *psource);
|
||||||
|
|
||||||
|
|
||||||
static void LoadConstants (LoadState *S, Proto *f) {
|
static void loadConstants (LoadState *S, Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
int n = LoadInt(S);
|
int n = loadInt(S);
|
||||||
f->k = luaM_newvectorchecked(S->L, n, TValue);
|
f->k = luaM_newvectorchecked(S->L, n, TValue);
|
||||||
f->sizek = n;
|
f->sizek = n;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
setnilvalue(&f->k[i]);
|
setnilvalue(&f->k[i]);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
TValue *o = &f->k[i];
|
TValue *o = &f->k[i];
|
||||||
int t = LoadByte(S);
|
int t = loadByte(S);
|
||||||
switch (t) {
|
switch (t) {
|
||||||
case LUA_TNIL:
|
case LUA_VNIL:
|
||||||
setnilvalue(o);
|
setnilvalue(o);
|
||||||
break;
|
break;
|
||||||
case LUA_TBOOLEAN:
|
case LUA_VFALSE:
|
||||||
setbvalue(o, LoadByte(S));
|
setbfvalue(o);
|
||||||
break;
|
break;
|
||||||
case LUA_TNUMFLT:
|
case LUA_VTRUE:
|
||||||
setfltvalue(o, LoadNumber(S));
|
setbtvalue(o);
|
||||||
break;
|
break;
|
||||||
case LUA_TNUMINT:
|
case LUA_VNUMFLT:
|
||||||
setivalue(o, LoadInteger(S));
|
setfltvalue(o, loadNumber(S));
|
||||||
break;
|
break;
|
||||||
case LUA_TSHRSTR:
|
case LUA_VNUMINT:
|
||||||
case LUA_TLNGSTR:
|
setivalue(o, loadInteger(S));
|
||||||
setsvalue2n(S->L, o, LoadString(S));
|
break;
|
||||||
|
case LUA_VSHRSTR:
|
||||||
|
case LUA_VLNGSTR:
|
||||||
|
setsvalue2n(S->L, o, loadString(S, f));
|
||||||
break;
|
break;
|
||||||
default: lua_assert(0);
|
default: lua_assert(0);
|
||||||
}
|
}
|
||||||
@@ -179,91 +188,101 @@ static void LoadConstants (LoadState *S, Proto *f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void LoadProtos (LoadState *S, Proto *f) {
|
static void loadProtos (LoadState *S, Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
int n = LoadInt(S);
|
int n = loadInt(S);
|
||||||
f->p = luaM_newvectorchecked(S->L, n, Proto *);
|
f->p = luaM_newvectorchecked(S->L, n, Proto *);
|
||||||
f->sizep = n;
|
f->sizep = n;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
f->p[i] = NULL;
|
f->p[i] = NULL;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
f->p[i] = luaF_newproto(S->L);
|
f->p[i] = luaF_newproto(S->L);
|
||||||
LoadFunction(S, f->p[i], f->source);
|
luaC_objbarrier(S->L, f, f->p[i]);
|
||||||
|
loadFunction(S, f->p[i], f->source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void LoadUpvalues (LoadState *S, Proto *f) {
|
/*
|
||||||
|
** Load the upvalues for a function. The names must be filled first,
|
||||||
|
** because the filling of the other fields can raise read errors and
|
||||||
|
** the creation of the error message can call an emergency collection;
|
||||||
|
** in that case all prototypes must be consistent for the GC.
|
||||||
|
*/
|
||||||
|
static void loadUpvalues (LoadState *S, Proto *f) {
|
||||||
int i, n;
|
int i, n;
|
||||||
n = LoadInt(S);
|
n = loadInt(S);
|
||||||
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
|
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
|
||||||
f->sizeupvalues = n;
|
f->sizeupvalues = n;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++) /* make array valid for GC */
|
||||||
f->upvalues[i].name = NULL;
|
f->upvalues[i].name = NULL;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) { /* following calls can raise errors */
|
||||||
f->upvalues[i].instack = LoadByte(S);
|
f->upvalues[i].instack = loadByte(S);
|
||||||
f->upvalues[i].idx = LoadByte(S);
|
f->upvalues[i].idx = loadByte(S);
|
||||||
|
f->upvalues[i].kind = loadByte(S);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void LoadDebug (LoadState *S, Proto *f) {
|
static void loadDebug (LoadState *S, Proto *f) {
|
||||||
int i, n;
|
int i, n;
|
||||||
n = LoadInt(S);
|
n = loadInt(S);
|
||||||
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
||||||
f->sizelineinfo = n;
|
f->sizelineinfo = n;
|
||||||
LoadVector(S, f->lineinfo, n);
|
loadVector(S, f->lineinfo, n);
|
||||||
n = LoadInt(S);
|
n = loadInt(S);
|
||||||
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
||||||
f->sizeabslineinfo = n;
|
f->sizeabslineinfo = n;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
f->abslineinfo[i].pc = LoadInt(S);
|
f->abslineinfo[i].pc = loadInt(S);
|
||||||
f->abslineinfo[i].line = LoadInt(S);
|
f->abslineinfo[i].line = loadInt(S);
|
||||||
}
|
}
|
||||||
n = LoadInt(S);
|
n = loadInt(S);
|
||||||
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
|
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
|
||||||
f->sizelocvars = n;
|
f->sizelocvars = n;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
f->locvars[i].varname = NULL;
|
f->locvars[i].varname = NULL;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
f->locvars[i].varname = LoadStringN(S);
|
f->locvars[i].varname = loadStringN(S, f);
|
||||||
f->locvars[i].startpc = LoadInt(S);
|
f->locvars[i].startpc = loadInt(S);
|
||||||
f->locvars[i].endpc = LoadInt(S);
|
f->locvars[i].endpc = loadInt(S);
|
||||||
}
|
}
|
||||||
n = LoadInt(S);
|
n = loadInt(S);
|
||||||
|
if (n != 0) /* does it have debug information? */
|
||||||
|
n = f->sizeupvalues; /* must be this many */
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
f->upvalues[i].name = LoadStringN(S);
|
f->upvalues[i].name = loadStringN(S, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
|
static void loadFunction (LoadState *S, Proto *f, TString *psource) {
|
||||||
f->source = LoadStringN(S);
|
f->source = loadStringN(S, f);
|
||||||
if (f->source == NULL) /* no source in dump? */
|
if (f->source == NULL) /* no source in dump? */
|
||||||
f->source = psource; /* reuse parent's source */
|
f->source = psource; /* reuse parent's source */
|
||||||
f->linedefined = LoadInt(S);
|
f->linedefined = loadInt(S);
|
||||||
f->lastlinedefined = LoadInt(S);
|
f->lastlinedefined = loadInt(S);
|
||||||
f->numparams = LoadByte(S);
|
f->numparams = loadByte(S);
|
||||||
f->is_vararg = LoadByte(S);
|
f->is_vararg = loadByte(S);
|
||||||
f->maxstacksize = LoadByte(S);
|
f->maxstacksize = loadByte(S);
|
||||||
LoadCode(S, f);
|
loadCode(S, f);
|
||||||
LoadConstants(S, f);
|
loadConstants(S, f);
|
||||||
LoadUpvalues(S, f);
|
loadUpvalues(S, f);
|
||||||
LoadProtos(S, f);
|
loadProtos(S, f);
|
||||||
LoadDebug(S, f);
|
loadDebug(S, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void checkliteral (LoadState *S, const char *s, const char *msg) {
|
static void checkliteral (LoadState *S, const char *s, const char *msg) {
|
||||||
char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
|
char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
|
||||||
size_t len = strlen(s);
|
size_t len = strlen(s);
|
||||||
LoadVector(S, buff, len);
|
loadVector(S, buff, len);
|
||||||
if (memcmp(s, buff, len) != 0)
|
if (memcmp(s, buff, len) != 0)
|
||||||
error(S, msg);
|
error(S, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
||||||
if (LoadByte(S) != size)
|
if (loadByte(S) != size)
|
||||||
error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
|
error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,23 +292,23 @@ static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
|||||||
static void checkHeader (LoadState *S) {
|
static void checkHeader (LoadState *S) {
|
||||||
/* skip 1st char (already read and checked) */
|
/* skip 1st char (already read and checked) */
|
||||||
checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
|
checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
|
||||||
if (LoadInt(S) != LUAC_VERSION)
|
if (loadByte(S) != LUAC_VERSION)
|
||||||
error(S, "version mismatch");
|
error(S, "version mismatch");
|
||||||
if (LoadByte(S) != LUAC_FORMAT)
|
if (loadByte(S) != LUAC_FORMAT)
|
||||||
error(S, "format mismatch");
|
error(S, "format mismatch");
|
||||||
checkliteral(S, LUAC_DATA, "corrupted chunk");
|
checkliteral(S, LUAC_DATA, "corrupted chunk");
|
||||||
checksize(S, Instruction);
|
checksize(S, Instruction);
|
||||||
checksize(S, lua_Integer);
|
checksize(S, lua_Integer);
|
||||||
checksize(S, lua_Number);
|
checksize(S, lua_Number);
|
||||||
if (LoadInteger(S) != LUAC_INT)
|
if (loadInteger(S) != LUAC_INT)
|
||||||
error(S, "integer format mismatch");
|
error(S, "integer format mismatch");
|
||||||
if (LoadNumber(S) != LUAC_NUM)
|
if (loadNumber(S) != LUAC_NUM)
|
||||||
error(S, "float format mismatch");
|
error(S, "float format mismatch");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** load precompiled chunk
|
** 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) {
|
||||||
LoadState S;
|
LoadState S;
|
||||||
@@ -303,13 +322,14 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
|||||||
S.L = L;
|
S.L = L;
|
||||||
S.Z = Z;
|
S.Z = Z;
|
||||||
checkHeader(&S);
|
checkHeader(&S);
|
||||||
cl = luaF_newLclosure(L, LoadByte(&S));
|
cl = luaF_newLclosure(L, loadByte(&S));
|
||||||
setclLvalue2s(L, L->top, cl);
|
setclLvalue2s(L, L->top.p, cl);
|
||||||
luaD_inctop(L);
|
luaD_inctop(L);
|
||||||
cl->p = luaF_newproto(L);
|
cl->p = luaF_newproto(L);
|
||||||
LoadFunction(&S, cl->p, NULL);
|
luaC_objbarrier(L, cl, cl->p);
|
||||||
|
loadFunction(&S, cl->p, NULL);
|
||||||
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
||||||
luai_verifycode(L, buff, cl->p);
|
luai_verifycode(L, cl->p);
|
||||||
return cl;
|
return cl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,11 @@
|
|||||||
#define LUAC_INT 0x5678
|
#define LUAC_INT 0x5678
|
||||||
#define LUAC_NUM cast_num(370.5)
|
#define LUAC_NUM cast_num(370.5)
|
||||||
|
|
||||||
#define LUAC_VERSION LUA_VERSION_NUM
|
/*
|
||||||
|
** Encode major-minor version in one byte, one nibble for each
|
||||||
|
*/
|
||||||
|
#define LUAC_VERSION (((LUA_VERSION_NUM / 100) * 16) + LUA_VERSION_NUM % 100)
|
||||||
|
|
||||||
#define LUAC_FORMAT 0 /* this is the official format */
|
#define LUAC_FORMAT 0 /* this is the official format */
|
||||||
|
|
||||||
/* load one chunk; from lundump.c */
|
/* load one chunk; from lundump.c */
|
||||||
|
|||||||
+27
-25
@@ -25,17 +25,21 @@
|
|||||||
|
|
||||||
#define MAXUTF 0x7FFFFFFFu
|
#define MAXUTF 0x7FFFFFFFu
|
||||||
|
|
||||||
|
|
||||||
|
#define MSGInvalid "invalid UTF-8 code"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
|
** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
|
||||||
*/
|
*/
|
||||||
#if (UINT_MAX >> 30) >= 1
|
#if (UINT_MAX >> 30) >= 1
|
||||||
typedef unsigned int utfint;
|
typedef unsigned int utfint;
|
||||||
#else
|
#else
|
||||||
typedef unsigned long utfint;
|
typedef unsigned long utfint;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define iscont(p) ((*(p) & 0xC0) == 0x80)
|
#define iscont(c) (((c) & 0xC0) == 0x80)
|
||||||
|
#define iscontp(p) iscont(*(p))
|
||||||
|
|
||||||
|
|
||||||
/* from strlib */
|
/* from strlib */
|
||||||
@@ -65,7 +69,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) {
|
|||||||
int count = 0; /* to count number of continuation bytes */
|
int count = 0; /* to count number of continuation bytes */
|
||||||
for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
|
for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
|
||||||
unsigned int cc = (unsigned char)s[++count]; /* read next byte */
|
unsigned int cc = (unsigned char)s[++count]; /* read next byte */
|
||||||
if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
|
if (!iscont(cc)) /* not a continuation byte? */
|
||||||
return NULL; /* invalid byte sequence */
|
return NULL; /* invalid byte sequence */
|
||||||
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
|
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
|
||||||
}
|
}
|
||||||
@@ -97,13 +101,13 @@ static int utflen (lua_State *L) {
|
|||||||
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
|
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
|
||||||
int lax = lua_toboolean(L, 4);
|
int lax = lua_toboolean(L, 4);
|
||||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
|
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
|
||||||
"initial position out of string");
|
"initial position out of bounds");
|
||||||
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
|
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
|
||||||
"final position out of string");
|
"final position out of bounds");
|
||||||
while (posi <= posj) {
|
while (posi <= posj) {
|
||||||
const char *s1 = utf8_decode(s + posi, NULL, !lax);
|
const char *s1 = utf8_decode(s + posi, NULL, !lax);
|
||||||
if (s1 == NULL) { /* conversion error? */
|
if (s1 == NULL) { /* conversion error? */
|
||||||
lua_pushnil(L); /* return nil ... */
|
luaL_pushfail(L); /* return fail ... */
|
||||||
lua_pushinteger(L, posi + 1); /* ... and current position */
|
lua_pushinteger(L, posi + 1); /* ... and current position */
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
@@ -127,8 +131,8 @@ static int codepoint (lua_State *L) {
|
|||||||
int lax = lua_toboolean(L, 4);
|
int lax = lua_toboolean(L, 4);
|
||||||
int n;
|
int n;
|
||||||
const char *se;
|
const char *se;
|
||||||
luaL_argcheck(L, posi >= 1, 2, "out of range");
|
luaL_argcheck(L, posi >= 1, 2, "out of bounds");
|
||||||
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
|
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
|
||||||
if (posi > pose) return 0; /* empty interval; return no values */
|
if (posi > pose) return 0; /* empty interval; return no values */
|
||||||
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
||||||
return luaL_error(L, "string slice too long");
|
return luaL_error(L, "string slice too long");
|
||||||
@@ -140,7 +144,7 @@ static int codepoint (lua_State *L) {
|
|||||||
utfint code;
|
utfint code;
|
||||||
s = utf8_decode(s, &code, !lax);
|
s = utf8_decode(s, &code, !lax);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return luaL_error(L, "invalid UTF-8 code");
|
return luaL_error(L, MSGInvalid);
|
||||||
lua_pushinteger(L, code);
|
lua_pushinteger(L, code);
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
@@ -187,19 +191,19 @@ static int byteoffset (lua_State *L) {
|
|||||||
lua_Integer posi = (n >= 0) ? 1 : len + 1;
|
lua_Integer posi = (n >= 0) ? 1 : len + 1;
|
||||||
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
||||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
||||||
"position out of range");
|
"position out of bounds");
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
/* find beginning of current byte sequence */
|
/* find beginning of current byte sequence */
|
||||||
while (posi > 0 && iscont(s + posi)) posi--;
|
while (posi > 0 && iscontp(s + posi)) posi--;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (iscont(s + posi))
|
if (iscontp(s + posi))
|
||||||
return luaL_error(L, "initial position is a continuation byte");
|
return luaL_error(L, "initial position is a continuation byte");
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
while (n < 0 && posi > 0) { /* move back */
|
while (n < 0 && posi > 0) { /* move back */
|
||||||
do { /* find beginning of previous character */
|
do { /* find beginning of previous character */
|
||||||
posi--;
|
posi--;
|
||||||
} while (posi > 0 && iscont(s + posi));
|
} while (posi > 0 && iscontp(s + posi));
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -208,7 +212,7 @@ static int byteoffset (lua_State *L) {
|
|||||||
while (n > 0 && posi < (lua_Integer)len) {
|
while (n > 0 && posi < (lua_Integer)len) {
|
||||||
do { /* find beginning of next character */
|
do { /* find beginning of next character */
|
||||||
posi++;
|
posi++;
|
||||||
} while (iscont(s + posi)); /* (cannot pass final '\0') */
|
} while (iscontp(s + posi)); /* (cannot pass final '\0') */
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -216,7 +220,7 @@ static int byteoffset (lua_State *L) {
|
|||||||
if (n == 0) /* did it find given character? */
|
if (n == 0) /* did it find given character? */
|
||||||
lua_pushinteger(L, posi + 1);
|
lua_pushinteger(L, posi + 1);
|
||||||
else /* no such character */
|
else /* no such character */
|
||||||
lua_pushnil(L);
|
luaL_pushfail(L);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,20 +228,17 @@ static int byteoffset (lua_State *L) {
|
|||||||
static int iter_aux (lua_State *L, int strict) {
|
static int iter_aux (lua_State *L, int strict) {
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = luaL_checklstring(L, 1, &len);
|
const char *s = luaL_checklstring(L, 1, &len);
|
||||||
lua_Integer n = lua_tointeger(L, 2) - 1;
|
lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
|
||||||
if (n < 0) /* first iteration? */
|
if (n < len) {
|
||||||
n = 0; /* start from here */
|
while (iscontp(s + n)) n++; /* go to next character */
|
||||||
else if (n < (lua_Integer)len) {
|
|
||||||
n++; /* skip current byte */
|
|
||||||
while (iscont(s + n)) n++; /* and its continuations */
|
|
||||||
}
|
}
|
||||||
if (n >= (lua_Integer)len)
|
if (n >= len) /* (also handles original 'n' being negative) */
|
||||||
return 0; /* no more codepoints */
|
return 0; /* no more codepoints */
|
||||||
else {
|
else {
|
||||||
utfint code;
|
utfint code;
|
||||||
const char *next = utf8_decode(s + n, &code, strict);
|
const char *next = utf8_decode(s + n, &code, strict);
|
||||||
if (next == NULL)
|
if (next == NULL || iscontp(next))
|
||||||
return luaL_error(L, "invalid UTF-8 code");
|
return luaL_error(L, MSGInvalid);
|
||||||
lua_pushinteger(L, n + 1);
|
lua_pushinteger(L, n + 1);
|
||||||
lua_pushinteger(L, code);
|
lua_pushinteger(L, code);
|
||||||
return 2;
|
return 2;
|
||||||
@@ -256,7 +257,8 @@ static int iter_auxlax (lua_State *L) {
|
|||||||
|
|
||||||
static int iter_codes (lua_State *L) {
|
static int iter_codes (lua_State *L) {
|
||||||
int lax = lua_toboolean(L, 2);
|
int lax = lua_toboolean(L, 2);
|
||||||
luaL_checkstring(L, 1);
|
const char *s = luaL_checkstring(L, 1);
|
||||||
|
luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
|
||||||
lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
|
lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
|
||||||
lua_pushvalue(L, 1);
|
lua_pushvalue(L, 1);
|
||||||
lua_pushinteger(L, 0);
|
lua_pushinteger(L, 0);
|
||||||
|
|||||||
@@ -33,10 +33,20 @@
|
|||||||
** integral values)
|
** integral values)
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_FLOORN2I)
|
#if !defined(LUA_FLOORN2I)
|
||||||
#define LUA_FLOORN2I 0
|
#define LUA_FLOORN2I F2Ieq
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Rounding modes for float->integer coercion
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
F2Ieq, /* no rounding; accepts only integral values */
|
||||||
|
F2Ifloor, /* takes the floor of the number */
|
||||||
|
F2Iceil /* takes the ceil of the number */
|
||||||
|
} F2Imod;
|
||||||
|
|
||||||
|
|
||||||
/* convert an object to a float (including string coercion) */
|
/* convert an object to a float (including string coercion) */
|
||||||
#define tonumber(o,n) \
|
#define tonumber(o,n) \
|
||||||
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
||||||
@@ -50,12 +60,14 @@
|
|||||||
|
|
||||||
/* convert an object to an integer (including string coercion) */
|
/* convert an object to an integer (including string coercion) */
|
||||||
#define tointeger(o,i) \
|
#define tointeger(o,i) \
|
||||||
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))
|
(l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
|
||||||
|
: luaV_tointeger(o,i,LUA_FLOORN2I))
|
||||||
|
|
||||||
|
|
||||||
/* convert an object to an integer (without string coercion) */
|
/* convert an object to an integer (without string coercion) */
|
||||||
#define tointegerns(o,i) \
|
#define tointegerns(o,i) \
|
||||||
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I))
|
(l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
|
||||||
|
: luaV_tointegerns(o,i,LUA_FLOORN2I))
|
||||||
|
|
||||||
|
|
||||||
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
|
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
|
||||||
@@ -98,15 +110,21 @@
|
|||||||
luaC_barrierback(L, gcvalue(t), v); }
|
luaC_barrierback(L, gcvalue(t), v); }
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Shift right is the same as shift left with a negative 'y'
|
||||||
|
*/
|
||||||
|
#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
|
LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
|
||||||
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
||||||
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
||||||
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
|
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
|
||||||
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
|
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
|
||||||
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode);
|
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
|
||||||
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode);
|
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,
|
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
|
||||||
StkId val, const TValue *slot);
|
StkId val, const TValue *slot);
|
||||||
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
|
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
|
||||||
|
|||||||
@@ -1,32 +1,36 @@
|
|||||||
# makefile for building Lua
|
# Developer's makefile for building Lua
|
||||||
# see INSTALL for installation instructions
|
# see luaconf.h for further customization
|
||||||
# see ../Makefile and luaconf.h for further customization
|
|
||||||
|
|
||||||
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
||||||
|
|
||||||
# Warnings valid for both C and C++
|
# Warnings valid for both C and C++
|
||||||
CWARNSCPP= \
|
CWARNSCPP= \
|
||||||
|
-Wfatal-errors \
|
||||||
-Wextra \
|
-Wextra \
|
||||||
-Wshadow \
|
-Wshadow \
|
||||||
-Wsign-compare \
|
|
||||||
-Wundef \
|
-Wundef \
|
||||||
-Wwrite-strings \
|
-Wwrite-strings \
|
||||||
-Wredundant-decls \
|
-Wredundant-decls \
|
||||||
-Wdisabled-optimization \
|
-Wdisabled-optimization \
|
||||||
-Wdouble-promotion \
|
-Wdouble-promotion \
|
||||||
#-Wno-aggressive-loop-optimizations \
|
-Wmissing-declarations \
|
||||||
#-Wlogical-op \
|
# the next warnings might be useful sometimes,
|
||||||
#-Wfatal-errors \
|
# but usually they generate too much noise
|
||||||
#-Wstrict-aliasing=3 \
|
|
||||||
# -Werror \
|
# -Werror \
|
||||||
# -pedantic # warns if we use jump tables \
|
# -pedantic # warns if we use jump tables \
|
||||||
# the next warnings generate too much noise, so they are disabled
|
# -Wconversion \
|
||||||
# -Wconversion -Wno-sign-conversion \
|
|
||||||
# -Wsign-conversion \
|
# -Wsign-conversion \
|
||||||
# -Wstrict-overflow=2 \
|
# -Wstrict-overflow=2 \
|
||||||
# -Wformat=2 \
|
# -Wformat=2 \
|
||||||
# -Wcast-qual \
|
# -Wcast-qual \
|
||||||
|
|
||||||
|
|
||||||
|
# Warnings for gcc, not valid for clang
|
||||||
|
CWARNGCC= \
|
||||||
|
-Wlogical-op \
|
||||||
|
-Wno-aggressive-loop-optimizations \
|
||||||
|
|
||||||
|
|
||||||
# The next warnings are neither valid nor needed for C++
|
# The next warnings are neither valid nor needed for C++
|
||||||
CWARNSC= -Wdeclaration-after-statement \
|
CWARNSC= -Wdeclaration-after-statement \
|
||||||
-Wmissing-prototypes \
|
-Wmissing-prototypes \
|
||||||
@@ -36,20 +40,33 @@ CWARNSC= -Wdeclaration-after-statement \
|
|||||||
-Wold-style-definition \
|
-Wold-style-definition \
|
||||||
|
|
||||||
|
|
||||||
CWARNS= $(CWARNSCPP) $(CWARNSC)
|
CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC)
|
||||||
|
|
||||||
|
# Some useful compiler options for internal tests:
|
||||||
|
# -DLUAI_ASSERT turns on all assertions inside Lua.
|
||||||
|
# -DHARDSTACKTESTS forces a reallocation of the stack at every point where
|
||||||
|
# the stack can be reallocated.
|
||||||
|
# -DHARDMEMTESTS forces a full collection at all points where the collector
|
||||||
|
# can run.
|
||||||
|
# -DEMERGENCYGCTESTS forces an emergency collection at every single allocation.
|
||||||
|
# -DEXTERNMEMCHECK removes internal consistency checking of blocks being
|
||||||
|
# deallocated (useful when an external tool like valgrind does the check).
|
||||||
|
# -DMAXINDEXRK=k limits range of constants in RK instruction operands.
|
||||||
|
# -DLUA_COMPAT_5_3
|
||||||
|
|
||||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
|
||||||
# -DMAXINDEXRK=1 -DLUA_COMPAT_5_3
|
|
||||||
# -g -DLUA_USER_H='"ltests.h"'
|
|
||||||
# -pg -malign-double
|
# -pg -malign-double
|
||||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||||
# ('-ftrapv' for runtime checks of integer overflows)
|
|
||||||
# -fsanitize=undefined -ftrapv -fno-inline
|
# The following options help detect "undefined behavior"s that seldom
|
||||||
TESTS= -DLUA_USER_H='"ltests.h"' -O0
|
# create problems; some are only available in newer gcc versions. To
|
||||||
|
# use some of them, we also have to define an environment variable
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
# LOCAL = $(TESTS) $(CWARNS) -g
|
LOCAL = $(TESTS) $(CWARNS)
|
||||||
|
|
||||||
|
|
||||||
# enable Linux goodies
|
# enable Linux goodies
|
||||||
@@ -82,11 +99,9 @@ LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \
|
|||||||
LUA_T= lua
|
LUA_T= lua
|
||||||
LUA_O= lua.o
|
LUA_O= lua.o
|
||||||
|
|
||||||
# LUAC_T= luac
|
|
||||||
# LUAC_O= luac.o print.o
|
|
||||||
|
|
||||||
ALL_T= $(CORE_T) $(LUA_T) $(LUAC_T)
|
ALL_T= $(CORE_T) $(LUA_T)
|
||||||
ALL_O= $(CORE_O) $(LUA_O) $(LUAC_O) $(AUX_O) $(LIB_O)
|
ALL_O= $(CORE_O) $(LUA_O) $(AUX_O) $(LIB_O)
|
||||||
ALL_A= $(CORE_T)
|
ALL_A= $(CORE_T)
|
||||||
|
|
||||||
all: $(ALL_T)
|
all: $(ALL_T)
|
||||||
@@ -103,11 +118,8 @@ $(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O)
|
|||||||
$(LUA_T): $(LUA_O) $(CORE_T)
|
$(LUA_T): $(LUA_O) $(CORE_T)
|
||||||
$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL)
|
$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL)
|
||||||
|
|
||||||
$(LUAC_T): $(LUAC_O) $(CORE_T)
|
|
||||||
$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS)
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rcsclean -u
|
|
||||||
$(RM) $(ALL_T) $(ALL_O)
|
$(RM) $(ALL_T) $(ALL_O)
|
||||||
|
|
||||||
depend:
|
depend:
|
||||||
|
|||||||
+2
-1
@@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
|
|||||||
<p>
|
<p>
|
||||||
<small>
|
<small>
|
||||||
<a href="http://www.lua.org/copyright.html">Copyright</a>
|
<a href="http://www.lua.org/copyright.html">Copyright</a>
|
||||||
© 2019 Lua.org, PUC-Rio. All rights reserved.
|
© 2025 Lua.org, PUC-Rio. All rights reserved.
|
||||||
</small>
|
</small>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
@@ -324,6 +324,7 @@ N = function (s) return (string.gsub(s, " ", " ")) end,
|
|||||||
NE = id, -- tag"foreignphrase",
|
NE = id, -- tag"foreignphrase",
|
||||||
num = id,
|
num = id,
|
||||||
["nil"] = fixed(Tag.b"nil"),
|
["nil"] = fixed(Tag.b"nil"),
|
||||||
|
fail = fixed(Tag.b"fail"),
|
||||||
Open = fixed"{",
|
Open = fixed"{",
|
||||||
part = section("h1", true),
|
part = section("h1", true),
|
||||||
Pat = compose(verbfixed, prepos("'", "'")),
|
Pat = compose(verbfixed, prepos("'", "'")),
|
||||||
|
|||||||
+920
-533
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
* one.c -- Lua core, libraries, and interpreter in a single file
|
** Lua core, libraries, and interpreter in a single file.
|
||||||
|
** Compiling just this file generates a complete Lua stand-alone
|
||||||
|
** program:
|
||||||
|
**
|
||||||
|
** $ gcc -O2 -std=c99 -o lua onelua.c -lm
|
||||||
|
**
|
||||||
|
** or
|
||||||
|
**
|
||||||
|
** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm
|
||||||
|
**
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* default is to build the full interpreter */
|
/* default is to build the full interpreter */
|
||||||
@@ -11,8 +20,12 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* choose suitable platform-specific features */
|
|
||||||
/* some of these may need extra libraries such as -ldl -lreadline -lncurses */
|
/*
|
||||||
|
** Choose suitable platform-specific features. Default is no
|
||||||
|
** platform-specific features. Some of these options may need extra
|
||||||
|
** libraries such as -ldl -lreadline -lncurses
|
||||||
|
*/
|
||||||
#if 0
|
#if 0
|
||||||
#define LUA_USE_LINUX
|
#define LUA_USE_LINUX
|
||||||
#define LUA_USE_MACOSX
|
#define LUA_USE_MACOSX
|
||||||
@@ -20,6 +33,7 @@
|
|||||||
#define LUA_ANSI
|
#define LUA_ANSI
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* no need to change anything below this line ----------------------------- */
|
/* no need to change anything below this line ----------------------------- */
|
||||||
|
|
||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|||||||
+33
-18
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
local version = "Lua 5.4"
|
local version = "Lua 5.4"
|
||||||
if _VERSION ~= version then
|
if _VERSION ~= version then
|
||||||
warn("This test suite is for ", version,
|
io.stderr:write("This test suite is for ", version,
|
||||||
", not for ", _VERSION, "\nExiting tests")
|
", not for ", _VERSION, "\nExiting tests")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -37,8 +37,6 @@ end
|
|||||||
-- tests should require debug when needed
|
-- tests should require debug when needed
|
||||||
debug = nil
|
debug = nil
|
||||||
|
|
||||||
require"bwcoercion"
|
|
||||||
|
|
||||||
|
|
||||||
if usertests then
|
if usertests then
|
||||||
T = nil -- no "internal" tests for user tests
|
T = nil -- no "internal" tests for user tests
|
||||||
@@ -46,7 +44,6 @@ else
|
|||||||
T = rawget(_G, "T") -- avoid problems with 'strict' module
|
T = rawget(_G, "T") -- avoid problems with 'strict' module
|
||||||
end
|
end
|
||||||
|
|
||||||
math.randomseed(0)
|
|
||||||
|
|
||||||
--[=[
|
--[=[
|
||||||
example of a long [comment],
|
example of a long [comment],
|
||||||
@@ -54,6 +51,14 @@ math.randomseed(0)
|
|||||||
|
|
||||||
]=]
|
]=]
|
||||||
|
|
||||||
|
print("\n\tStarting Tests")
|
||||||
|
|
||||||
|
do
|
||||||
|
-- set random seed
|
||||||
|
local random_x, random_y = math.randomseed()
|
||||||
|
print(string.format("random seeds: %d, %d", random_x, random_y))
|
||||||
|
end
|
||||||
|
|
||||||
print("current path:\n****" .. package.path .. "****\n")
|
print("current path:\n****" .. package.path .. "****\n")
|
||||||
|
|
||||||
|
|
||||||
@@ -95,6 +100,8 @@ local function F (m)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local Cstacklevel
|
||||||
|
|
||||||
local showmem
|
local showmem
|
||||||
if not T then
|
if not T then
|
||||||
local max = 0
|
local max = 0
|
||||||
@@ -104,6 +111,7 @@ if not T then
|
|||||||
print(format(" ---- total memory: %s, max memory: %s ----\n",
|
print(format(" ---- total memory: %s, max memory: %s ----\n",
|
||||||
F(m), F(max)))
|
F(m), F(max)))
|
||||||
end
|
end
|
||||||
|
Cstacklevel = function () return 0 end -- no info about stack level
|
||||||
else
|
else
|
||||||
showmem = function ()
|
showmem = function ()
|
||||||
T.checkmemory()
|
T.checkmemory()
|
||||||
@@ -117,9 +125,16 @@ else
|
|||||||
T.totalmem"string", T.totalmem"table", T.totalmem"function",
|
T.totalmem"string", T.totalmem"table", T.totalmem"function",
|
||||||
T.totalmem"userdata", T.totalmem"thread"))
|
T.totalmem"userdata", T.totalmem"thread"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Cstacklevel = function ()
|
||||||
|
local _, _, ncalls = T.stacklevel()
|
||||||
|
return ncalls -- number of C calls
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local Cstack = Cstacklevel()
|
||||||
|
|
||||||
--
|
--
|
||||||
-- redefine dofile to run files through dump/undump
|
-- redefine dofile to run files through dump/undump
|
||||||
--
|
--
|
||||||
@@ -139,18 +154,8 @@ end
|
|||||||
|
|
||||||
dofile('main.lua')
|
dofile('main.lua')
|
||||||
|
|
||||||
do
|
-- trace GC cycles
|
||||||
local next, setmetatable, stderr = next, setmetatable, io.stderr
|
require"tracegc".start()
|
||||||
-- track collections
|
|
||||||
local mt = {}
|
|
||||||
-- each time a table is collected, remark it for finalization
|
|
||||||
-- on next cycle
|
|
||||||
mt.__gc = function (o)
|
|
||||||
stderr:write'.' -- mark progress
|
|
||||||
local n = setmetatable(o, mt) -- remark it
|
|
||||||
end
|
|
||||||
local n = setmetatable({}, mt) -- create object
|
|
||||||
end
|
|
||||||
|
|
||||||
report"gc.lua"
|
report"gc.lua"
|
||||||
local f = assert(loadfile('gc.lua'))
|
local f = assert(loadfile('gc.lua'))
|
||||||
@@ -158,6 +163,7 @@ f()
|
|||||||
|
|
||||||
dofile('db.lua')
|
dofile('db.lua')
|
||||||
assert(dofile('calls.lua') == deep and deep)
|
assert(dofile('calls.lua') == deep and deep)
|
||||||
|
_G.deep = nil
|
||||||
olddofile('strings.lua')
|
olddofile('strings.lua')
|
||||||
olddofile('literals.lua')
|
olddofile('literals.lua')
|
||||||
dofile('tpack.lua')
|
dofile('tpack.lua')
|
||||||
@@ -195,7 +201,12 @@ if #msgs > 0 then
|
|||||||
end
|
end
|
||||||
|
|
||||||
print("(there should be two warnings now)")
|
print("(there should be two warnings now)")
|
||||||
|
warn("@on")
|
||||||
warn("#This is ", "an expected", " warning")
|
warn("#This is ", "an expected", " warning")
|
||||||
|
warn("@off")
|
||||||
|
warn("******** THIS WARNING SHOULD NOT APPEAR **********")
|
||||||
|
warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********")
|
||||||
|
warn("@on")
|
||||||
warn("#This is", " another one")
|
warn("#This is", " another one")
|
||||||
|
|
||||||
-- no test module should define 'debug'
|
-- no test module should define 'debug'
|
||||||
@@ -211,6 +222,10 @@ debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
|
|||||||
-- to survive outside block
|
-- to survive outside block
|
||||||
_G.showmem = showmem
|
_G.showmem = showmem
|
||||||
|
|
||||||
|
|
||||||
|
assert(Cstack == Cstacklevel(),
|
||||||
|
"should be at the same C-stack level it was when started the tests")
|
||||||
|
|
||||||
end --)
|
end --)
|
||||||
|
|
||||||
local _G, showmem, print, format, clock, time, difftime,
|
local _G, showmem, print, format, clock, time, difftime,
|
||||||
@@ -272,7 +287,7 @@ print("final OK !!!")
|
|||||||
|
|
||||||
--[[
|
--[[
|
||||||
*****************************************************************************
|
*****************************************************************************
|
||||||
* Copyright (C) 1994-2016 Lua.org, PUC-Rio.
|
* Copyright (C) 1994-2025 Lua.org, PUC-Rio.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
|
|||||||
+251
-87
@@ -11,7 +11,10 @@ local debug = require "debug"
|
|||||||
local pack = table.pack
|
local pack = table.pack
|
||||||
|
|
||||||
|
|
||||||
function tcheck (t1, t2)
|
-- standard error message for memory errors
|
||||||
|
local MEMERRMSG = "not enough memory"
|
||||||
|
|
||||||
|
local function tcheck (t1, t2)
|
||||||
assert(t1.n == (t2.n or #t2) + 1)
|
assert(t1.n == (t2.n or #t2) + 1)
|
||||||
for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
|
for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
|
||||||
end
|
end
|
||||||
@@ -25,7 +28,7 @@ end
|
|||||||
|
|
||||||
print('testing C API')
|
print('testing C API')
|
||||||
|
|
||||||
a = T.testC("pushvalue R; return 1")
|
local a = T.testC("pushvalue R; return 1")
|
||||||
assert(a == debug.getregistry())
|
assert(a == debug.getregistry())
|
||||||
|
|
||||||
|
|
||||||
@@ -40,10 +43,10 @@ a = T.d2s(12458954321123.0)
|
|||||||
assert(a == string.pack("d", 12458954321123.0))
|
assert(a == string.pack("d", 12458954321123.0))
|
||||||
assert(T.s2d(a) == 12458954321123.0)
|
assert(T.s2d(a) == 12458954321123.0)
|
||||||
|
|
||||||
a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
|
local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
|
||||||
assert(a == 2 and b == 3 and not c)
|
assert(a == 2 and b == 3 and not c)
|
||||||
|
|
||||||
f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
|
local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
|
||||||
a,b,c = f()
|
a,b,c = f()
|
||||||
assert(a == 2 and b == 3 and not c)
|
assert(a == 2 and b == 3 and not c)
|
||||||
|
|
||||||
@@ -58,7 +61,7 @@ assert(a==false and b==true and c==false)
|
|||||||
a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
|
a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
|
||||||
assert(a == 40 and b == 5 and not c)
|
assert(a == 40 and b == 5 and not c)
|
||||||
|
|
||||||
t = pack(T.testC("settop 5; return *", 2, 3))
|
local t = pack(T.testC("settop 5; return *", 2, 3))
|
||||||
tcheck(t, {n=4,2,3})
|
tcheck(t, {n=4,2,3})
|
||||||
|
|
||||||
t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
|
t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
|
||||||
@@ -163,16 +166,17 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- testing globals
|
-- testing globals
|
||||||
_G.a = 14; _G.b = "a31"
|
_G.AA = 14; _G.BB = "a31"
|
||||||
local a = {T.testC[[
|
local a = {T.testC[[
|
||||||
getglobal a;
|
getglobal AA;
|
||||||
getglobal b;
|
getglobal BB;
|
||||||
getglobal b;
|
getglobal BB;
|
||||||
setglobal a;
|
setglobal AA;
|
||||||
return *
|
return *
|
||||||
]]}
|
]]}
|
||||||
assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31")
|
assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31")
|
||||||
|
|
||||||
|
_G.AA, _G.BB = nil
|
||||||
|
|
||||||
-- testing arith
|
-- testing arith
|
||||||
assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
|
assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
|
||||||
@@ -196,13 +200,14 @@ a,b,c = T.testC([[pushnum 1;
|
|||||||
pushstring 10; arith _;
|
pushstring 10; arith _;
|
||||||
pushstring 5; return 3]])
|
pushstring 5; return 3]])
|
||||||
assert(a == 1 and b == -10 and c == "5")
|
assert(a == 1 and b == -10 and c == "5")
|
||||||
mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
|
local mt = {
|
||||||
|
__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
|
||||||
__mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
|
__mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
|
||||||
__unm = function (a) return setmetatable({a[1]* 2}, mt) end}
|
__unm = function (a) return setmetatable({a[1]* 2}, mt) end}
|
||||||
a,b,c = setmetatable({4}, mt),
|
a,b,c = setmetatable({4}, mt),
|
||||||
setmetatable({8}, mt),
|
setmetatable({8}, mt),
|
||||||
setmetatable({-3}, mt)
|
setmetatable({-3}, mt)
|
||||||
x,y,z = T.testC("arith +; return 2", 10, a, b)
|
local x,y,z = T.testC("arith +; return 2", 10, a, b)
|
||||||
assert(x == 10 and y[1] == 12 and z == nil)
|
assert(x == 10 and y[1] == 12 and z == nil)
|
||||||
assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
|
assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
|
||||||
assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
|
assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
|
||||||
@@ -241,6 +246,23 @@ assert(a == 20 and b == false)
|
|||||||
a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
|
a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
|
||||||
assert(a == 20 and b == true)
|
assert(a == 20 and b == true)
|
||||||
|
|
||||||
|
|
||||||
|
do -- testing lessthan and lessequal with metamethods
|
||||||
|
local mt = {__lt = function (a,b) return a[1] < b[1] end,
|
||||||
|
__le = function (a,b) return a[1] <= b[1] end,
|
||||||
|
__eq = function (a,b) return a[1] == b[1] end}
|
||||||
|
local function O (x)
|
||||||
|
return setmetatable({x}, mt)
|
||||||
|
end
|
||||||
|
|
||||||
|
local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2))
|
||||||
|
assert(a == true and b == 10)
|
||||||
|
local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2))
|
||||||
|
assert(a == false and b == 10)
|
||||||
|
local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3))
|
||||||
|
assert(a == true and b == 10)
|
||||||
|
end
|
||||||
|
|
||||||
-- testing length
|
-- testing length
|
||||||
local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
|
local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
|
||||||
a,b,c = T.testC([[
|
a,b,c = T.testC([[
|
||||||
@@ -292,9 +314,9 @@ assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
|
|||||||
|
|
||||||
-- testing lua_is
|
-- testing lua_is
|
||||||
|
|
||||||
function B(x) return x and 1 or 0 end
|
local function B (x) return x and 1 or 0 end
|
||||||
|
|
||||||
function count (x, n)
|
local function count (x, n)
|
||||||
n = n or 2
|
n = n or 2
|
||||||
local prog = [[
|
local prog = [[
|
||||||
isnumber %d;
|
isnumber %d;
|
||||||
@@ -325,7 +347,7 @@ assert(count(nil, 15) == 100)
|
|||||||
|
|
||||||
-- testing lua_to...
|
-- testing lua_to...
|
||||||
|
|
||||||
function to (s, x, n)
|
local function to (s, x, n)
|
||||||
n = n or 2
|
n = n or 2
|
||||||
return T.testC(string.format("%s %d; return 1", s, n), x)
|
return T.testC(string.format("%s %d; return 1", s, n), x)
|
||||||
end
|
end
|
||||||
@@ -354,8 +376,11 @@ assert(to("topointer", nil) == null)
|
|||||||
assert(to("topointer", "abc") ~= null)
|
assert(to("topointer", "abc") ~= null)
|
||||||
assert(to("topointer", string.rep("x", 10)) ==
|
assert(to("topointer", string.rep("x", 10)) ==
|
||||||
to("topointer", string.rep("x", 10))) -- short strings
|
to("topointer", string.rep("x", 10))) -- short strings
|
||||||
assert(to("topointer", string.rep("x", 300)) ~=
|
do -- long strings
|
||||||
to("topointer", string.rep("x", 300))) -- long strings
|
local s1 = string.rep("x", 300)
|
||||||
|
local s2 = string.rep("x", 300)
|
||||||
|
assert(to("topointer", s1) ~= to("topointer", s2))
|
||||||
|
end
|
||||||
assert(to("topointer", T.pushuserdata(20)) ~= null)
|
assert(to("topointer", T.pushuserdata(20)) ~= null)
|
||||||
assert(to("topointer", io.read) ~= null) -- light C function
|
assert(to("topointer", io.read) ~= null) -- light C function
|
||||||
assert(to("topointer", hfunc) ~= null) -- "heavy" C function
|
assert(to("topointer", hfunc) ~= null) -- "heavy" C function
|
||||||
@@ -374,6 +399,10 @@ do
|
|||||||
-- trivial error
|
-- trivial error
|
||||||
assert(T.checkpanic("pushstring hi; error") == "hi")
|
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
|
-- using the stack inside panic
|
||||||
assert(T.checkpanic("pushstring hi; error;",
|
assert(T.checkpanic("pushstring hi; error;",
|
||||||
[[checkstack 5 XX
|
[[checkstack 5 XX
|
||||||
@@ -388,7 +417,7 @@ do
|
|||||||
|
|
||||||
-- memory error
|
-- memory error
|
||||||
T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
|
T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
|
||||||
assert(T.checkpanic("newuserdata 20000") == "not enough memory")
|
assert(T.checkpanic("newuserdata 20000") == MEMERRMSG)
|
||||||
T.totalmem(0) -- restore high limit
|
T.totalmem(0) -- restore high limit
|
||||||
|
|
||||||
-- stack error
|
-- stack error
|
||||||
@@ -463,11 +492,12 @@ a = T.testC([[
|
|||||||
pushvalue 3; insert -2; pcall 1 1 0;
|
pushvalue 3; insert -2; pcall 1 1 0;
|
||||||
pcall 0 0 0;
|
pcall 0 0 0;
|
||||||
return 1
|
return 1
|
||||||
]], "x=150", function (a) assert(a==nil); return 3 end)
|
]], "XX=150", function (a) assert(a==nil); return 3 end)
|
||||||
|
|
||||||
assert(type(a) == 'string' and x == 150)
|
assert(type(a) == 'string' and XX == 150)
|
||||||
|
_G.XX = nil
|
||||||
|
|
||||||
function check3(p, ...)
|
local function check3(p, ...)
|
||||||
local arg = {...}
|
local arg = {...}
|
||||||
assert(#arg == 3)
|
assert(#arg == 3)
|
||||||
assert(string.find(arg[3], p))
|
assert(string.find(arg[3], p))
|
||||||
@@ -477,17 +507,20 @@ check3("%.", T.testC("loadfile 2; return *", "."))
|
|||||||
check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
|
check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
|
||||||
|
|
||||||
-- test errors in non protected threads
|
-- test errors in non protected threads
|
||||||
function checkerrnopro (code, msg)
|
local function checkerrnopro (code, msg)
|
||||||
local th = coroutine.create(function () end) -- create new thread
|
local th = coroutine.create(function () end) -- create new thread
|
||||||
local stt, err = pcall(T.testC, th, code) -- run code there
|
local stt, err = pcall(T.testC, th, code) -- run code there
|
||||||
assert(not stt and string.find(err, msg))
|
assert(not stt and string.find(err, msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
if not _soft then
|
if not _soft then
|
||||||
|
collectgarbage("stop") -- avoid __gc with full stack
|
||||||
checkerrnopro("pushnum 3; call 0 0", "attempt to call")
|
checkerrnopro("pushnum 3; call 0 0", "attempt to call")
|
||||||
print"testing stack overflow in unprotected thread"
|
print"testing stack overflow in unprotected thread"
|
||||||
function f () f() end
|
function F () F() end
|
||||||
checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow")
|
checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow")
|
||||||
|
F = nil
|
||||||
|
collectgarbage("restart")
|
||||||
end
|
end
|
||||||
print"+"
|
print"+"
|
||||||
|
|
||||||
@@ -496,9 +529,57 @@ print"+"
|
|||||||
|
|
||||||
do -- getp/setp
|
do -- getp/setp
|
||||||
local a = {}
|
local a = {}
|
||||||
T.testC("rawsetp 2 1", a, 20)
|
local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
|
||||||
|
assert(a == a1)
|
||||||
assert(a[T.pushuserdata(1)] == 20)
|
assert(a[T.pushuserdata(1)] == 20)
|
||||||
assert(T.testC("rawgetp 2 1; return 1", a) == 20)
|
local a1, res = T.testC("rawgetp -1 1; return 2", a)
|
||||||
|
assert(a == a1 and res == 20)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- using the table itself as index
|
||||||
|
local a = {}
|
||||||
|
a[a] = 10
|
||||||
|
local prog = "gettable -1; return *"
|
||||||
|
local res = {T.testC(prog, a)}
|
||||||
|
assert(#res == 2 and res[1] == prog and res[2] == 10)
|
||||||
|
|
||||||
|
local prog = "settable -2; return *"
|
||||||
|
local res = {T.testC(prog, a, 20)}
|
||||||
|
assert(a[a] == 20)
|
||||||
|
assert(#res == 1 and res[1] == prog)
|
||||||
|
|
||||||
|
-- raw
|
||||||
|
a[a] = 10
|
||||||
|
local prog = "rawget -1; return *"
|
||||||
|
local res = {T.testC(prog, a)}
|
||||||
|
assert(#res == 2 and res[1] == prog and res[2] == 10)
|
||||||
|
|
||||||
|
local prog = "rawset -2; return *"
|
||||||
|
local res = {T.testC(prog, a, 20)}
|
||||||
|
assert(a[a] == 20)
|
||||||
|
assert(#res == 1 and res[1] == prog)
|
||||||
|
|
||||||
|
-- using the table as the value to set
|
||||||
|
local prog = "rawset -1; return *"
|
||||||
|
local res = {T.testC(prog, 30, a)}
|
||||||
|
assert(a[30] == a)
|
||||||
|
assert(#res == 1 and res[1] == prog)
|
||||||
|
|
||||||
|
local prog = "settable -1; return *"
|
||||||
|
local res = {T.testC(prog, 40, a)}
|
||||||
|
assert(a[40] == a)
|
||||||
|
assert(#res == 1 and res[1] == prog)
|
||||||
|
|
||||||
|
local prog = "rawseti -1 100; return *"
|
||||||
|
local res = {T.testC(prog, a)}
|
||||||
|
assert(a[100] == a)
|
||||||
|
assert(#res == 1 and res[1] == prog)
|
||||||
|
|
||||||
|
local prog = "seti -1 200; return *"
|
||||||
|
local res = {T.testC(prog, a)}
|
||||||
|
assert(a[200] == a)
|
||||||
|
assert(#res == 1 and res[1] == prog)
|
||||||
end
|
end
|
||||||
|
|
||||||
a = {x=0, y=12}
|
a = {x=0, y=12}
|
||||||
@@ -515,7 +596,7 @@ assert(a[a] == "x")
|
|||||||
|
|
||||||
b = setmetatable({p = a}, {})
|
b = setmetatable({p = a}, {})
|
||||||
getmetatable(b).__index = function (t, i) return t.p[i] end
|
getmetatable(b).__index = function (t, i) return t.p[i] end
|
||||||
k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
|
local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
|
||||||
assert(x == 15 and k == 35)
|
assert(x == 15 and k == 35)
|
||||||
k = T.testC("getfield 2 y, return 1", b)
|
k = T.testC("getfield 2 y, return 1", b)
|
||||||
assert(k == 12)
|
assert(k == 12)
|
||||||
@@ -632,7 +713,7 @@ for k, v in ipairs(t) do
|
|||||||
assert(v1 == v and p)
|
assert(v1 == v and p)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(debug.getuservalue(4) == nil)
|
assert(not debug.getuservalue(4))
|
||||||
|
|
||||||
debug.setuservalue(b, function () return 10 end, 10)
|
debug.setuservalue(b, function () return 10 end, 10)
|
||||||
collectgarbage() -- function should not be collected
|
collectgarbage() -- function should not be collected
|
||||||
@@ -675,8 +756,8 @@ local i = T.ref{}
|
|||||||
T.unref(i)
|
T.unref(i)
|
||||||
assert(T.ref{} == i)
|
assert(T.ref{} == i)
|
||||||
|
|
||||||
Arr = {}
|
local Arr = {}
|
||||||
Lim = 100
|
local Lim = 100
|
||||||
for i=1,Lim do -- lock many objects
|
for i=1,Lim do -- lock many objects
|
||||||
Arr[i] = T.ref({})
|
Arr[i] = T.ref({})
|
||||||
end
|
end
|
||||||
@@ -688,7 +769,7 @@ for i=1,Lim do -- unlock all them
|
|||||||
T.unref(Arr[i])
|
T.unref(Arr[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
function printlocks ()
|
local function printlocks ()
|
||||||
local f = T.makeCfunc("gettable R; return 1")
|
local f = T.makeCfunc("gettable R; return 1")
|
||||||
local n = f("n")
|
local n = f("n")
|
||||||
print("n", n)
|
print("n", n)
|
||||||
@@ -720,8 +801,8 @@ assert(type(T.getref(a)) == 'table')
|
|||||||
|
|
||||||
|
|
||||||
-- colect in cl the `val' of all collected userdata
|
-- colect in cl the `val' of all collected userdata
|
||||||
tt = {}
|
local tt = {}
|
||||||
cl = {n=0}
|
local cl = {n=0}
|
||||||
A = nil; B = nil
|
A = nil; B = nil
|
||||||
local F
|
local F
|
||||||
F = function (x)
|
F = function (x)
|
||||||
@@ -731,20 +812,20 @@ F = function (x)
|
|||||||
d = nil
|
d = nil
|
||||||
assert(debug.getmetatable(x).__gc == F)
|
assert(debug.getmetatable(x).__gc == F)
|
||||||
assert(load("table.insert({}, {})"))() -- create more garbage
|
assert(load("table.insert({}, {})"))() -- create more garbage
|
||||||
collectgarbage() -- force a GC during GC
|
assert(not collectgarbage()) -- GC during GC (no op)
|
||||||
assert(debug.getmetatable(x).__gc == F) -- previous GC did not mess this?
|
|
||||||
local dummy = {} -- create more garbage during GC
|
local dummy = {} -- create more garbage during GC
|
||||||
if A ~= nil then
|
if A ~= nil then
|
||||||
assert(type(A) == "userdata")
|
assert(type(A) == "userdata")
|
||||||
assert(T.udataval(A) == B)
|
assert(T.udataval(A) == B)
|
||||||
debug.getmetatable(A) -- just acess it
|
debug.getmetatable(A) -- just access it
|
||||||
end
|
end
|
||||||
A = x -- ressucita userdata
|
A = x -- ressurect userdata
|
||||||
B = udval
|
B = udval
|
||||||
return 1,2,3
|
return 1,2,3
|
||||||
end
|
end
|
||||||
tt.__gc = F
|
tt.__gc = F
|
||||||
|
|
||||||
|
|
||||||
-- test whether udate collection frees memory in the right time
|
-- test whether udate collection frees memory in the right time
|
||||||
do
|
do
|
||||||
collectgarbage();
|
collectgarbage();
|
||||||
@@ -781,9 +862,9 @@ end
|
|||||||
collectgarbage("stop")
|
collectgarbage("stop")
|
||||||
|
|
||||||
-- create 3 userdatas with tag `tt'
|
-- create 3 userdatas with tag `tt'
|
||||||
a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
|
a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a)
|
||||||
b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
|
b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b)
|
||||||
c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)
|
c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c)
|
||||||
|
|
||||||
-- create userdata without meta table
|
-- create userdata without meta table
|
||||||
x = T.newuserdata(4)
|
x = T.newuserdata(4)
|
||||||
@@ -794,9 +875,9 @@ checkerr("FILE%* expected, got userdata", io.input, x)
|
|||||||
|
|
||||||
assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
|
assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
|
||||||
|
|
||||||
d=T.ref(a);
|
local d = T.ref(a);
|
||||||
e=T.ref(b);
|
local e = T.ref(b);
|
||||||
f=T.ref(c);
|
local f = T.ref(c);
|
||||||
t = {T.getref(d), T.getref(e), T.getref(f)}
|
t = {T.getref(d), T.getref(e), T.getref(f)}
|
||||||
assert(t[1] == a and t[2] == b and t[3] == c)
|
assert(t[1] == a and t[2] == b and t[3] == c)
|
||||||
|
|
||||||
@@ -816,7 +897,7 @@ tt=nil -- frees tt for GC
|
|||||||
A = nil
|
A = nil
|
||||||
b = nil
|
b = nil
|
||||||
T.unref(d);
|
T.unref(d);
|
||||||
n5 = T.newuserdata(0)
|
local n5 = T.newuserdata(0)
|
||||||
debug.setmetatable(n5, {__gc=F})
|
debug.setmetatable(n5, {__gc=F})
|
||||||
n5 = T.udataval(n5)
|
n5 = T.udataval(n5)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
@@ -887,11 +968,11 @@ print'+'
|
|||||||
|
|
||||||
|
|
||||||
-- testing changing hooks during hooks
|
-- testing changing hooks during hooks
|
||||||
_G.t = {}
|
_G.TT = {}
|
||||||
T.sethook([[
|
T.sethook([[
|
||||||
# set a line hook after 3 count hooks
|
# set a line hook after 3 count hooks
|
||||||
sethook 4 0 '
|
sethook 4 0 '
|
||||||
getglobal t;
|
getglobal TT;
|
||||||
pushvalue -3; append -2
|
pushvalue -3; append -2
|
||||||
pushvalue -2; append -2
|
pushvalue -2; append -2
|
||||||
']], "c", 3)
|
']], "c", 3)
|
||||||
@@ -901,16 +982,18 @@ a = 1 -- count hook (set line hook)
|
|||||||
a = 1 -- line hook
|
a = 1 -- line hook
|
||||||
a = 1 -- line hook
|
a = 1 -- line hook
|
||||||
debug.sethook()
|
debug.sethook()
|
||||||
t = _G.t
|
local t = _G.TT
|
||||||
assert(t[1] == "line")
|
assert(t[1] == "line")
|
||||||
line = t[2]
|
local line = t[2]
|
||||||
assert(t[3] == "line" and t[4] == line + 1)
|
assert(t[3] == "line" and t[4] == line + 1)
|
||||||
assert(t[5] == "line" and t[6] == line + 2)
|
assert(t[5] == "line" and t[6] == line + 2)
|
||||||
assert(t[7] == nil)
|
assert(t[7] == nil)
|
||||||
|
_G.TT = nil
|
||||||
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
-------------------------------------------------------------------------
|
||||||
do -- testing errors during GC
|
do -- testing errors during GC
|
||||||
|
warn("@off")
|
||||||
collectgarbage("stop")
|
collectgarbage("stop")
|
||||||
local a = {}
|
local a = {}
|
||||||
for i=1,20 do
|
for i=1,20 do
|
||||||
@@ -928,7 +1011,9 @@ do -- testing errors during GC
|
|||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(A == 10) -- number of normal collections
|
assert(A == 10) -- number of normal collections
|
||||||
collectgarbage("restart")
|
collectgarbage("restart")
|
||||||
|
warn("@on")
|
||||||
end
|
end
|
||||||
|
_G.A = nil
|
||||||
-------------------------------------------------------------------------
|
-------------------------------------------------------------------------
|
||||||
-- test for userdata vals
|
-- test for userdata vals
|
||||||
do
|
do
|
||||||
@@ -958,8 +1043,8 @@ assert(a == 'alo' and b == '3')
|
|||||||
|
|
||||||
T.doremote(L1, "_ERRORMESSAGE = nil")
|
T.doremote(L1, "_ERRORMESSAGE = nil")
|
||||||
-- error: `sin' is not defined
|
-- error: `sin' is not defined
|
||||||
a, _, b = T.doremote(L1, "return sin(1)")
|
a, b, c = T.doremote(L1, "return sin(1)")
|
||||||
assert(a == nil and b == 2) -- 2 == run-time error
|
assert(a == nil and c == 2) -- 2 == run-time error
|
||||||
|
|
||||||
-- error: syntax error
|
-- error: syntax error
|
||||||
a, b, c = T.doremote(L1, "return a+")
|
a, b, c = T.doremote(L1, "return a+")
|
||||||
@@ -1030,7 +1115,7 @@ do
|
|||||||
assert(type(a[1]) == "string" and a[2][1] == 11)
|
assert(type(a[1]) == "string" and a[2][1] == 11)
|
||||||
assert(#openresource == 0) -- was closed
|
assert(#openresource == 0) -- was closed
|
||||||
|
|
||||||
-- error
|
-- closing by error
|
||||||
local a, b = pcall(T.makeCfunc[[
|
local a, b = pcall(T.makeCfunc[[
|
||||||
call 0 1 # create resource
|
call 0 1 # create resource
|
||||||
toclose -1 # mark it to be closed
|
toclose -1 # mark it to be closed
|
||||||
@@ -1039,26 +1124,62 @@ do
|
|||||||
assert(a == false and b[1] == 11)
|
assert(a == false and b[1] == 11)
|
||||||
assert(#openresource == 0) -- was closed
|
assert(#openresource == 0) -- was closed
|
||||||
|
|
||||||
|
-- non-closable value
|
||||||
|
local a, b = pcall(T.makeCfunc[[
|
||||||
|
newtable # create non-closable object
|
||||||
|
toclose -1 # mark it to be closed (should raise an error)
|
||||||
|
abort # will not be executed
|
||||||
|
]])
|
||||||
|
assert(a == false and
|
||||||
|
string.find(b, "non%-closable value"))
|
||||||
|
|
||||||
local function check (n)
|
local function check (n)
|
||||||
assert(#openresource == n)
|
assert(#openresource == n)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- closing resources with 'settop'
|
-- closing resources with 'closeslot'
|
||||||
|
_ENV.xxx = true
|
||||||
local a = T.testC([[
|
local a = T.testC([[
|
||||||
pushvalue 2
|
pushvalue 2 # stack: S, NR, CH, NR
|
||||||
call 0 1 # create resource
|
call 0 1 # create resource; stack: S, NR, CH, R
|
||||||
toclose -1 # mark it to be closed
|
toclose -1 # mark it to be closed
|
||||||
pushvalue 2
|
pushvalue 2 # stack: S, NR, CH, R, NR
|
||||||
call 0 1 # create another resource
|
call 0 1 # create another resource; stack: S, NR, CH, R, R
|
||||||
toclose -1 # mark it to be closed
|
toclose -1 # mark it to be closed
|
||||||
pushvalue 3
|
pushvalue 3 # stack: S, NR, CH, R, R, CH
|
||||||
pushint 2 # there should be two open resources
|
pushint 2 # there should be two open resources
|
||||||
call 1 0
|
call 1 0 # stack: S, NR, CH, R, R
|
||||||
pop 1 # pop second resource from the stack
|
closeslot -1 # close second resource
|
||||||
pushvalue 3
|
pushvalue 3 # stack: S, NR, CH, R, R, CH
|
||||||
pushint 1 # there should be one open resource
|
pushint 1 # there should be one open resource
|
||||||
call 1 0
|
call 1 0 # stack: S, NR, CH, R, R
|
||||||
pop 1 # pop second resource from the stack
|
closeslot 4
|
||||||
|
setglobal "xxx" # previous op. erased the slot
|
||||||
|
pop 1 # pop other resource from the stack
|
||||||
|
pushint *
|
||||||
|
return 1 # return stack size
|
||||||
|
]], newresource, check)
|
||||||
|
assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack
|
||||||
|
|
||||||
|
-- closing resources with 'pop'
|
||||||
|
local a = T.testC([[
|
||||||
|
pushvalue 2 # stack: S, NR, CH, NR
|
||||||
|
call 0 1 # create resource; stack: S, NR, CH, R
|
||||||
|
toclose -1 # mark it to be closed
|
||||||
|
pushvalue 2 # stack: S, NR, CH, R, NR
|
||||||
|
call 0 1 # create another resource; stack: S, NR, CH, R, R
|
||||||
|
toclose -1 # mark it to be closed
|
||||||
|
pushvalue 3 # stack: S, NR, CH, R, R, CH
|
||||||
|
pushint 2 # there should be two open resources
|
||||||
|
call 1 0 # stack: S, NR, CH, R, R
|
||||||
|
pop 1 # pop second resource
|
||||||
|
pushvalue 3 # stack: S, NR, CH, R, CH
|
||||||
|
pushint 1 # there should be one open resource
|
||||||
|
call 1 0 # stack: S, NR, CH, R
|
||||||
|
pop 1 # pop other resource from the stack
|
||||||
|
pushvalue 3 # stack: S, NR, CH, CH
|
||||||
|
pushint 0 # there should be no open resources
|
||||||
|
call 1 0 # stack: S, NR, CH
|
||||||
pushint *
|
pushint *
|
||||||
return 1 # return stack size
|
return 1 # return stack size
|
||||||
]], newresource, check)
|
]], newresource, check)
|
||||||
@@ -1074,40 +1195,74 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
--[[
|
||||||
-- testing memory limits
|
** {==================================================================
|
||||||
-------------------------------------------------------------------------
|
** Testing memory limits
|
||||||
|
** ===================================================================
|
||||||
|
--]]
|
||||||
|
|
||||||
print("memory-allocation errors")
|
print("memory-allocation errors")
|
||||||
|
|
||||||
checkerr("block too big", T.newuserdata, math.maxinteger)
|
checkerr("block too big", T.newuserdata, math.maxinteger)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
local f = load"local a={}; for i=1,100000 do a[i]=i end"
|
local f = load"local a={}; for i=1,100000 do a[i]=i end"
|
||||||
T.alloccount(10)
|
T.alloccount(10)
|
||||||
checkerr("not enough memory", f)
|
checkerr(MEMERRMSG, f)
|
||||||
T.alloccount() -- remove limit
|
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
|
-- test memory errors; increase limit for number of allocations one
|
||||||
-- by one, so that we get memory errors in all allocations of a given
|
-- 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
|
-- task, until there is enough allocations to complete the task without
|
||||||
-- errors.
|
-- errors.
|
||||||
|
|
||||||
function testamem (s, f)
|
local function testalloc (s, f)
|
||||||
collectgarbage(); collectgarbage()
|
collectgarbage()
|
||||||
local M = 0
|
local M = 0
|
||||||
local a,b = nil
|
local a,b = nil
|
||||||
while true do
|
while true do
|
||||||
|
collectgarbage(); collectgarbage()
|
||||||
T.alloccount(M)
|
T.alloccount(M)
|
||||||
a, b = pcall(f)
|
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
|
||||||
T.alloccount() -- remove limit
|
T.alloccount() -- remove limit
|
||||||
if a and b then break end -- stop when no more errors
|
if a and b == "OK" then break end -- stop when no more errors
|
||||||
if not a and not -- `real' error?
|
if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
|
||||||
(string.find(b, "memory") or string.find(b, "overflow")) then
|
error(a, 0) -- propagate it
|
||||||
error(b, 0) -- propagate it
|
|
||||||
end
|
end
|
||||||
M = M + 1 -- increase allocation limit
|
M = M + 1 -- increase allocation limit
|
||||||
end
|
end
|
||||||
print(string.format("limit for %s: %d allocations", s, M))
|
print(string.format("minimum allocations for %s: %d allocations", s, M))
|
||||||
return b
|
return a
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function testamem (s, f)
|
||||||
|
testalloc(s, f)
|
||||||
|
return testbytes(s, f)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -1117,8 +1272,11 @@ assert(b == 10)
|
|||||||
|
|
||||||
-- testing memory errors when creating a new state
|
-- testing memory errors when creating a new state
|
||||||
|
|
||||||
b = testamem("state creation", T.newstate)
|
testamem("state creation", function ()
|
||||||
T.closestate(b); -- close new state
|
local st = T.newstate()
|
||||||
|
if st then T.closestate(st) end -- close new state
|
||||||
|
return st
|
||||||
|
end)
|
||||||
|
|
||||||
testamem("empty-table creation", function ()
|
testamem("empty-table creation", function ()
|
||||||
return {}
|
return {}
|
||||||
@@ -1137,7 +1295,7 @@ end)
|
|||||||
testamem("to-be-closed variables", function()
|
testamem("to-be-closed variables", function()
|
||||||
local flag
|
local flag
|
||||||
do
|
do
|
||||||
local <toclose> x =
|
local x <close> =
|
||||||
setmetatable({}, {__close = function () flag = true end})
|
setmetatable({}, {__close = function () flag = true end})
|
||||||
flag = false
|
flag = false
|
||||||
local x = {}
|
local x = {}
|
||||||
@@ -1149,12 +1307,12 @@ end)
|
|||||||
-- testing threads
|
-- testing threads
|
||||||
|
|
||||||
-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
|
-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
|
||||||
mt = T.testC("rawgeti R 1; return 1")
|
local mt = T.testC("rawgeti R 1; return 1")
|
||||||
assert(type(mt) == "thread" and coroutine.running() == mt)
|
assert(type(mt) == "thread" and coroutine.running() == mt)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function expand (n,s)
|
local function expand (n,s)
|
||||||
if n==0 then return "" end
|
if n==0 then return "" end
|
||||||
local e = string.rep("=", n)
|
local e = string.rep("=", n)
|
||||||
return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
|
return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
|
||||||
@@ -1164,9 +1322,10 @@ end
|
|||||||
G=0; collectgarbage(); a =collectgarbage("count")
|
G=0; collectgarbage(); a =collectgarbage("count")
|
||||||
load(expand(20,"G=G+1"))()
|
load(expand(20,"G=G+1"))()
|
||||||
assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
|
assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
|
||||||
|
G = nil
|
||||||
|
|
||||||
testamem("running code on new thread", function ()
|
testamem("running code on new thread", function ()
|
||||||
return T.doonnewstack("x=1") == 0 -- try to create thread
|
return T.doonnewstack("local x=1") == 0 -- try to create thread
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
@@ -1180,13 +1339,13 @@ end)
|
|||||||
local testprog = [[
|
local testprog = [[
|
||||||
local function foo () return end
|
local function foo () return end
|
||||||
local t = {"x"}
|
local t = {"x"}
|
||||||
a = "aaa"
|
AA = "aaa"
|
||||||
for i = 1, #t do a=a..t[i] end
|
for i = 1, #t do AA = AA .. t[i] end
|
||||||
return true
|
return true
|
||||||
]]
|
]]
|
||||||
|
|
||||||
-- testing memory x dofile
|
-- testing memory x dofile
|
||||||
_G.a = nil
|
_G.AA = nil
|
||||||
local t =os.tmpname()
|
local t =os.tmpname()
|
||||||
local f = assert(io.open(t, "w"))
|
local f = assert(io.open(t, "w"))
|
||||||
f:write(testprog)
|
f:write(testprog)
|
||||||
@@ -1196,7 +1355,7 @@ testamem("dofile", function ()
|
|||||||
return a and a()
|
return a and a()
|
||||||
end)
|
end)
|
||||||
assert(os.remove(t))
|
assert(os.remove(t))
|
||||||
assert(_G.a == "aaax")
|
assert(_G.AA == "aaax")
|
||||||
|
|
||||||
|
|
||||||
-- other generic tests
|
-- other generic tests
|
||||||
@@ -1213,6 +1372,8 @@ testamem("dump/undump", function ()
|
|||||||
return a and a()
|
return a and a()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
_G.AA = nil
|
||||||
|
|
||||||
local t = os.tmpname()
|
local t = os.tmpname()
|
||||||
testamem("file creation", function ()
|
testamem("file creation", function ()
|
||||||
local f = assert(io.open(t, 'w'))
|
local f = assert(io.open(t, 'w'))
|
||||||
@@ -1234,7 +1395,7 @@ testamem("constructors", function ()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
local a = 1
|
local a = 1
|
||||||
close = nil
|
local close = nil
|
||||||
testamem("closure creation", function ()
|
testamem("closure creation", function ()
|
||||||
function close (b)
|
function close (b)
|
||||||
return function (x) return b + x end
|
return function (x) return b + x end
|
||||||
@@ -1266,6 +1427,9 @@ testamem("growing stack", function ()
|
|||||||
return foo(100)
|
return foo(100)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- }==================================================================
|
||||||
|
|
||||||
|
|
||||||
do -- testing failing in 'lua_checkstack'
|
do -- testing failing in 'lua_checkstack'
|
||||||
local res = T.testC([[rawcheckstack 500000; return 1]])
|
local res = T.testC([[rawcheckstack 500000; return 1]])
|
||||||
assert(res == false)
|
assert(res == false)
|
||||||
|
|||||||
+47
-12
@@ -28,7 +28,7 @@ do
|
|||||||
local path = table.concat(t, ";")
|
local path = table.concat(t, ";")
|
||||||
-- use that path in a search
|
-- use that path in a search
|
||||||
local s, err = package.searchpath("xuxu", path)
|
local s, err = package.searchpath("xuxu", path)
|
||||||
-- search fails; check that message has an occurence of
|
-- search fails; check that message has an occurrence of
|
||||||
-- '??????????' with ? replaced by xuxu and at least 'max' lines
|
-- '??????????' with ? replaced by xuxu and at least 'max' lines
|
||||||
assert(not s and
|
assert(not s and
|
||||||
string.find(err, string.rep("xuxu", 10)) and
|
string.find(err, string.rep("xuxu", 10)) and
|
||||||
@@ -47,6 +47,29 @@ do
|
|||||||
package.path = oldpath
|
package.path = oldpath
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do print"testing 'require' message"
|
||||||
|
local oldpath = package.path
|
||||||
|
local oldcpath = package.cpath
|
||||||
|
|
||||||
|
package.path = "?.lua;?/?"
|
||||||
|
package.cpath = "?.so;?/init"
|
||||||
|
|
||||||
|
local st, msg = pcall(require, 'XXX')
|
||||||
|
|
||||||
|
local expected = [[module 'XXX' not found:
|
||||||
|
no field package.preload['XXX']
|
||||||
|
no file 'XXX.lua'
|
||||||
|
no file 'XXX/XXX'
|
||||||
|
no file 'XXX.so'
|
||||||
|
no file 'XXX/init']]
|
||||||
|
|
||||||
|
assert(msg == expected)
|
||||||
|
|
||||||
|
package.path = oldpath
|
||||||
|
package.cpath = oldcpath
|
||||||
|
end
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
@@ -62,7 +85,7 @@ local DIR = "libs" .. dirsep
|
|||||||
|
|
||||||
-- prepend DIR to a name and correct directory separators
|
-- prepend DIR to a name and correct directory separators
|
||||||
local function D (x)
|
local function D (x)
|
||||||
x = string.gsub(x, "/", dirsep)
|
local x = string.gsub(x, "/", dirsep)
|
||||||
return DIR .. x
|
return DIR .. x
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -83,7 +106,7 @@ local function createfiles (files, preextras, posextras)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function removefiles (files)
|
local function removefiles (files)
|
||||||
for n in pairs(files) do
|
for n in pairs(files) do
|
||||||
os.remove(D(n))
|
os.remove(D(n))
|
||||||
end
|
end
|
||||||
@@ -131,10 +154,9 @@ local try = function (p, n, r, ext)
|
|||||||
assert(ext == x)
|
assert(ext == x)
|
||||||
end
|
end
|
||||||
|
|
||||||
a = require"names"
|
local a = require"names"
|
||||||
assert(a[1] == "names" and a[2] == D"names.lua")
|
assert(a[1] == "names" and a[2] == D"names.lua")
|
||||||
|
|
||||||
_G.a = nil
|
|
||||||
local st, msg = pcall(require, "err")
|
local st, msg = pcall(require, "err")
|
||||||
assert(not st and string.find(msg, "arithmetic") and B == 15)
|
assert(not st and string.find(msg, "arithmetic") and B == 15)
|
||||||
st, msg = pcall(require, "synerr")
|
st, msg = pcall(require, "synerr")
|
||||||
@@ -168,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX")
|
|||||||
|
|
||||||
|
|
||||||
removefiles(files)
|
removefiles(files)
|
||||||
|
NAME, REQUIRED, AA, B = nil
|
||||||
|
|
||||||
|
|
||||||
-- testing require of sub-packages
|
-- testing require of sub-packages
|
||||||
@@ -200,7 +223,7 @@ assert(require"P1" == m and m.AA == 10)
|
|||||||
|
|
||||||
|
|
||||||
removefiles(files)
|
removefiles(files)
|
||||||
|
AA = nil
|
||||||
|
|
||||||
package.path = ""
|
package.path = ""
|
||||||
assert(not pcall(require, "file_does_not_exist"))
|
assert(not pcall(require, "file_does_not_exist"))
|
||||||
@@ -282,6 +305,7 @@ else
|
|||||||
assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1")
|
assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1")
|
||||||
assert(string.find(ext, "libs/lib1", 1, true))
|
assert(string.find(ext, "libs/lib1", 1, true))
|
||||||
assert(fs.id(45) == 45)
|
assert(fs.id(45) == 45)
|
||||||
|
_ENV.x, _ENV.y = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
_ENV = _G
|
_ENV = _G
|
||||||
@@ -315,10 +339,10 @@ print("testing assignments, logical operators, and constructors")
|
|||||||
|
|
||||||
local res, res2 = 27
|
local res, res2 = 27
|
||||||
|
|
||||||
a, b = 1, 2+3
|
local a, b = 1, 2+3
|
||||||
assert(a==1 and b==5)
|
assert(a==1 and b==5)
|
||||||
a={}
|
a={}
|
||||||
function f() return 10, 11, 12 end
|
local function f() return 10, 11, 12 end
|
||||||
a.x, b, a[1] = 1, 2, f()
|
a.x, b, a[1] = 1, 2, f()
|
||||||
assert(a.x==1 and b==2 and a[1]==10)
|
assert(a.x==1 and b==2 and a[1]==10)
|
||||||
a[f()], b, a[f()+3] = f(), a, 'x'
|
a[f()], b, a[f()+3] = f(), a, 'x'
|
||||||
@@ -330,15 +354,15 @@ do
|
|||||||
local a,b,c
|
local a,b,c
|
||||||
a,b = 0, f(1)
|
a,b = 0, f(1)
|
||||||
assert(a == 0 and b == 1)
|
assert(a == 0 and b == 1)
|
||||||
A,b = 0, f(1)
|
a,b = 0, f(1)
|
||||||
assert(A == 0 and b == 1)
|
assert(a == 0 and b == 1)
|
||||||
a,b,c = 0,5,f(4)
|
a,b,c = 0,5,f(4)
|
||||||
assert(a==0 and b==5 and c==1)
|
assert(a==0 and b==5 and c==1)
|
||||||
a,b,c = 0,5,f(0)
|
a,b,c = 0,5,f(0)
|
||||||
assert(a==0 and b==5 and c==nil)
|
assert(a==0 and b==5 and c==nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
|
local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
|
||||||
assert(not a and b and c and d==6)
|
assert(not a and b and c and d==6)
|
||||||
|
|
||||||
d = 20
|
d = 20
|
||||||
@@ -396,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end))
|
|||||||
assert(a[nil] == undef)
|
assert(a[nil] == undef)
|
||||||
a = nil
|
a = nil
|
||||||
|
|
||||||
|
local a, b, c
|
||||||
a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
|
a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
|
||||||
a, a.x, a.y = a, a[-3]
|
a, a.x, a.y = a, a[-3]
|
||||||
assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
|
assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
|
||||||
@@ -411,6 +436,16 @@ a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 ==
|
|||||||
10)
|
10)
|
||||||
|
|
||||||
|
|
||||||
|
do
|
||||||
|
-- _ENV constant
|
||||||
|
local function foo ()
|
||||||
|
local _ENV <const> = 11
|
||||||
|
X = "hi"
|
||||||
|
end
|
||||||
|
local st, msg = pcall(foo)
|
||||||
|
assert(not st and string.find(msg, "number"))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- test of large float/integer indices
|
-- test of large float/integer indices
|
||||||
|
|
||||||
@@ -422,7 +457,7 @@ while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do
|
|||||||
maxint = maxint // 2
|
maxint = maxint // 2
|
||||||
end
|
end
|
||||||
|
|
||||||
maxintF = maxint + 0.0 -- float version
|
local maxintF = maxint + 0.0 -- float version
|
||||||
|
|
||||||
assert(maxintF == maxint and math.type(maxintF) == "float" and
|
assert(maxintF == maxint and math.type(maxintF) == "float" and
|
||||||
maxintF >= 2.0^14)
|
maxintF >= 2.0^14)
|
||||||
|
|||||||
+2
-2
@@ -32,7 +32,7 @@ setmetatable(env, {
|
|||||||
})
|
})
|
||||||
|
|
||||||
X = nil
|
X = nil
|
||||||
co = coroutine.wrap(f)
|
local co = coroutine.wrap(f)
|
||||||
assert(co() == 's')
|
assert(co() == 's')
|
||||||
assert(co() == 'g')
|
assert(co() == 'g')
|
||||||
assert(co() == 'g')
|
assert(co() == 'g')
|
||||||
@@ -66,7 +66,7 @@ assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size
|
|||||||
|
|
||||||
local longs = string.rep("\0", ssize) -- create one long string
|
local longs = string.rep("\0", ssize) -- create one long string
|
||||||
|
|
||||||
-- create function to concatentate 'repstrings' copies of its argument
|
-- create function to concatenate 'repstrings' copies of its argument
|
||||||
local rep = assert(load(
|
local rep = assert(load(
|
||||||
"local a = ...; return " .. string.rep("a", repstrings, "..")))
|
"local a = ...; return " .. string.rep("a", repstrings, "..")))
|
||||||
|
|
||||||
|
|||||||
+20
-3
@@ -38,6 +38,18 @@ d = d << 32
|
|||||||
assert(a | b ~ c & d == 0xF4000000 << 32)
|
assert(a | b ~ c & d == 0xF4000000 << 32)
|
||||||
assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1)
|
assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1)
|
||||||
|
|
||||||
|
|
||||||
|
do -- constant folding
|
||||||
|
local code = string.format("return -1 >> %d", math.maxinteger)
|
||||||
|
assert(load(code)() == 0)
|
||||||
|
local code = string.format("return -1 >> %d", math.mininteger)
|
||||||
|
assert(load(code)() == 0)
|
||||||
|
local code = string.format("return -1 << %d", math.maxinteger)
|
||||||
|
assert(load(code)() == 0)
|
||||||
|
local code = string.format("return -1 << %d", math.mininteger)
|
||||||
|
assert(load(code)() == 0)
|
||||||
|
end
|
||||||
|
|
||||||
assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000)
|
assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000)
|
||||||
assert(-1 >> (numbits - 1) == 1)
|
assert(-1 >> (numbits - 1) == 1)
|
||||||
assert(-1 >> numbits == 0 and
|
assert(-1 >> numbits == 0 and
|
||||||
@@ -45,6 +57,11 @@ assert(-1 >> numbits == 0 and
|
|||||||
-1 << numbits == 0 and
|
-1 << numbits == 0 and
|
||||||
-1 << -numbits == 0)
|
-1 << -numbits == 0)
|
||||||
|
|
||||||
|
assert(1 >> math.mininteger == 0)
|
||||||
|
assert(1 >> math.maxinteger == 0)
|
||||||
|
assert(1 << math.mininteger == 0)
|
||||||
|
assert(1 << math.maxinteger == 0)
|
||||||
|
|
||||||
assert((2^30 - 1) << 2^30 == 0)
|
assert((2^30 - 1) << 2^30 == 0)
|
||||||
assert((2^30 - 1) >> 2^30 == 0)
|
assert((2^30 - 1) >> 2^30 == 0)
|
||||||
|
|
||||||
@@ -60,9 +77,9 @@ assert("1234.0" << "5.0" == 1234 * 32)
|
|||||||
assert("0xffff.0" ~ "0xAAAA" == 0x5555)
|
assert("0xffff.0" ~ "0xAAAA" == 0x5555)
|
||||||
assert(~"0x0.000p4" == -1)
|
assert(~"0x0.000p4" == -1)
|
||||||
|
|
||||||
assert("7" .. 3 << 1 == 146)
|
assert(("7" .. 3) << 1 == 146)
|
||||||
assert(10 >> 1 .. "9" == 0)
|
assert(0xffffffff >> (1 .. "9") == 0x1fff)
|
||||||
assert(10 | 1 .. "9" == 27)
|
assert(10 | (1 .. "9") == 27)
|
||||||
|
|
||||||
do
|
do
|
||||||
local st, msg = pcall(function () return 4 & "a" end)
|
local st, msg = pcall(function () return 4 & "a" end)
|
||||||
|
|||||||
+107
-28
@@ -16,7 +16,7 @@ assert(type(nil) == 'nil'
|
|||||||
and type(type) == 'function')
|
and type(type) == 'function')
|
||||||
|
|
||||||
assert(type(assert) == type(print))
|
assert(type(assert) == type(print))
|
||||||
function f (x) return a:x (x) end
|
local function f (x) return a:x (x) end
|
||||||
assert(type(f) == 'function')
|
assert(type(f) == 'function')
|
||||||
assert(not pcall(type))
|
assert(not pcall(type))
|
||||||
|
|
||||||
@@ -33,10 +33,11 @@ do
|
|||||||
assert(fact(5) == 120)
|
assert(fact(5) == 120)
|
||||||
end
|
end
|
||||||
assert(fact == false)
|
assert(fact == false)
|
||||||
|
fact = nil
|
||||||
|
|
||||||
-- testing declarations
|
-- testing declarations
|
||||||
a = {i = 10}
|
local a = {i = 10}
|
||||||
self = 20
|
local self = 20
|
||||||
function a:x (x) return x+self.i end
|
function a:x (x) return x+self.i end
|
||||||
function a.y (x) return x+self end
|
function a.y (x) return x+self end
|
||||||
|
|
||||||
@@ -72,6 +73,8 @@ f(1,2, -- this one too
|
|||||||
3,4)
|
3,4)
|
||||||
assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
|
assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
|
||||||
|
|
||||||
|
t = nil -- delete 't'
|
||||||
|
|
||||||
function fat(x)
|
function fat(x)
|
||||||
if x <= 1 then return 1
|
if x <= 1 then return 1
|
||||||
else return x*load("return fat(" .. x-1 .. ")", "")()
|
else return x*load("return fat(" .. x-1 .. ")", "")()
|
||||||
@@ -80,26 +83,29 @@ end
|
|||||||
|
|
||||||
assert(load "load 'assert(fat(6)==720)' () ")()
|
assert(load "load 'assert(fat(6)==720)' () ")()
|
||||||
a = load('return fat(5), 3')
|
a = load('return fat(5), 3')
|
||||||
a,b = a()
|
local a,b = a()
|
||||||
assert(a == 120 and b == 3)
|
assert(a == 120 and b == 3)
|
||||||
|
fat = nil
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
function err_on_n (n)
|
local function err_on_n (n)
|
||||||
if n==0 then error(); exit(1);
|
if n==0 then error(); exit(1);
|
||||||
else err_on_n (n-1); exit(1);
|
else err_on_n (n-1); exit(1);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
function dummy (n)
|
local function dummy (n)
|
||||||
if n > 0 then
|
if n > 0 then
|
||||||
assert(not pcall(err_on_n, n))
|
assert(not pcall(err_on_n, n))
|
||||||
dummy(n-1)
|
dummy(n-1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
dummy(10)
|
||||||
end
|
end
|
||||||
|
|
||||||
dummy(10)
|
_G.deep = nil -- "declaration" (used by 'all.lua')
|
||||||
|
|
||||||
function deep (n)
|
function deep (n)
|
||||||
if n>0 then deep(n-1) end
|
if n>0 then deep(n-1) end
|
||||||
@@ -107,7 +113,9 @@ end
|
|||||||
deep(10)
|
deep(10)
|
||||||
deep(180)
|
deep(180)
|
||||||
|
|
||||||
-- testing tail calls
|
|
||||||
|
print"testing tail calls"
|
||||||
|
|
||||||
function deep (n) if n>0 then return deep(n-1) else return 101 end end
|
function deep (n) if n>0 then return deep(n-1) else return 101 end end
|
||||||
assert(deep(30000) == 101)
|
assert(deep(30000) == 101)
|
||||||
a = {}
|
a = {}
|
||||||
@@ -148,9 +156,57 @@ do -- tail calls x varargs
|
|||||||
assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30)
|
assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- C-stack overflow while handling C-stack overflow
|
||||||
|
local function loop ()
|
||||||
|
assert(pcall(loop))
|
||||||
|
end
|
||||||
|
|
||||||
|
local err, msg = xpcall(loop, loop)
|
||||||
|
assert(not err and string.find(msg, "error"))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
do -- tail calls x chain of __call
|
||||||
|
local n = 10000 -- depth
|
||||||
|
|
||||||
|
local function foo ()
|
||||||
|
if n == 0 then return 1023
|
||||||
|
else n = n - 1; return foo()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- build a chain of __call metamethods ending in function 'foo'
|
||||||
|
for i = 1, 100 do
|
||||||
|
foo = setmetatable({}, {__call = foo})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- call the first one as a tail call in a new coroutine
|
||||||
|
-- (to ensure stack is not preallocated)
|
||||||
|
assert(coroutine.wrap(function() return foo() end)() == 1023)
|
||||||
|
end
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
|
do -- testing chains of '__call'
|
||||||
|
local N = 20
|
||||||
|
local u = table.pack
|
||||||
|
for i = 1, N do
|
||||||
|
u = setmetatable({i}, {__call = u})
|
||||||
|
end
|
||||||
|
|
||||||
|
local Res = u("a", "b", "c")
|
||||||
|
|
||||||
|
assert(Res.n == N + 3)
|
||||||
|
for i = 1, N do
|
||||||
|
assert(Res[i][1] == i)
|
||||||
|
end
|
||||||
|
assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
a = nil
|
a = nil
|
||||||
(function (x) a=x end)(23)
|
(function (x) a=x end)(23)
|
||||||
assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
||||||
@@ -159,7 +215,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
|||||||
-- testing closures
|
-- testing closures
|
||||||
|
|
||||||
-- fixed-point operator
|
-- fixed-point operator
|
||||||
Z = function (le)
|
local Z = function (le)
|
||||||
local function a (f)
|
local function a (f)
|
||||||
return le(function (x) return f(f)(x) end)
|
return le(function (x) return f(f)(x) end)
|
||||||
end
|
end
|
||||||
@@ -169,14 +225,14 @@ Z = function (le)
|
|||||||
|
|
||||||
-- non-recursive factorial
|
-- non-recursive factorial
|
||||||
|
|
||||||
F = function (f)
|
local F = function (f)
|
||||||
return function (n)
|
return function (n)
|
||||||
if n == 0 then return 1
|
if n == 0 then return 1
|
||||||
else return n*f(n-1) end
|
else return n*f(n-1) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
fat = Z(F)
|
local fat = Z(F)
|
||||||
|
|
||||||
assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
|
assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
|
||||||
|
|
||||||
@@ -187,22 +243,21 @@ local function g (z)
|
|||||||
return f(z,z+1,z+2,z+3)
|
return f(z,z+1,z+2,z+3)
|
||||||
end
|
end
|
||||||
|
|
||||||
f = g(10)
|
local f = g(10)
|
||||||
assert(f(9, 16) == 10+11+12+13+10+9+16+10)
|
assert(f(9, 16) == 10+11+12+13+10+9+16+10)
|
||||||
|
|
||||||
Z, F, f = nil
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
-- testing multiple returns
|
-- testing multiple returns
|
||||||
|
|
||||||
function unlpack (t, i)
|
local function unlpack (t, i)
|
||||||
i = i or 1
|
i = i or 1
|
||||||
if (i <= #t) then
|
if (i <= #t) then
|
||||||
return t[i], unlpack(t, i+1)
|
return t[i], unlpack(t, i+1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function equaltab (t1, t2)
|
local function equaltab (t1, t2)
|
||||||
assert(#t1 == #t2)
|
assert(#t1 == #t2)
|
||||||
for i = 1, #t1 do
|
for i = 1, #t1 do
|
||||||
assert(t1[i] == t2[i])
|
assert(t1[i] == t2[i])
|
||||||
@@ -211,8 +266,8 @@ end
|
|||||||
|
|
||||||
local pack = function (...) return (table.pack(...)) end
|
local pack = function (...) return (table.pack(...)) end
|
||||||
|
|
||||||
function f() return 1,2,30,4 end
|
local function f() return 1,2,30,4 end
|
||||||
function ret2 (a,b) return a,b end
|
local function ret2 (a,b) return a,b end
|
||||||
|
|
||||||
local a,b,c,d = unlpack{1,2,3}
|
local a,b,c,d = unlpack{1,2,3}
|
||||||
assert(a==1 and b==2 and c==3 and d==nil)
|
assert(a==1 and b==2 and c==3 and d==nil)
|
||||||
@@ -241,7 +296,7 @@ table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
|
|||||||
local x = "-- a comment\0\0\0\n x = 10 + \n23; \
|
local x = "-- a comment\0\0\0\n x = 10 + \n23; \
|
||||||
local a = function () x = 'hi' end; \
|
local a = function () x = 'hi' end; \
|
||||||
return '\0'"
|
return '\0'"
|
||||||
function read1 (x)
|
local function read1 (x)
|
||||||
local i = 0
|
local i = 0
|
||||||
return function ()
|
return function ()
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
@@ -250,7 +305,7 @@ function read1 (x)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function cannotload (msg, a,b)
|
local function cannotload (msg, a,b)
|
||||||
assert(not a and string.find(b, msg))
|
assert(not a and string.find(b, msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -277,11 +332,22 @@ f = load(string.dump(function () return 1 end), nil, "b", {})
|
|||||||
assert(type(f) == "function" and f() == 1)
|
assert(type(f) == "function" and f() == 1)
|
||||||
|
|
||||||
|
|
||||||
|
do -- another bug (in 5.4.0)
|
||||||
|
-- loading a binary long string interrupted by GC cycles
|
||||||
|
local f = string.dump(function ()
|
||||||
|
return '01234567890123456789012345678901234567890123456789'
|
||||||
|
end)
|
||||||
|
f = load(read1(f))
|
||||||
|
assert(f() == '01234567890123456789012345678901234567890123456789')
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
x = string.dump(load("x = 1; return x"))
|
x = string.dump(load("x = 1; return x"))
|
||||||
a = assert(load(read1(x), nil, "b"))
|
a = assert(load(read1(x), nil, "b"))
|
||||||
assert(a() == 1 and _G.x == 1)
|
assert(a() == 1 and _G.x == 1)
|
||||||
cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
|
cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
|
||||||
cannotload("attempt to load a binary chunk", load(x, nil, "t"))
|
cannotload("attempt to load a binary chunk", load(x, nil, "t"))
|
||||||
|
_G.x = nil
|
||||||
|
|
||||||
assert(not pcall(string.dump, print)) -- no dump of C functions
|
assert(not pcall(string.dump, print)) -- no dump of C functions
|
||||||
|
|
||||||
@@ -306,7 +372,7 @@ debug.setupvalue(x, 2, _G)
|
|||||||
assert(x() == 123)
|
assert(x() == 123)
|
||||||
|
|
||||||
assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
|
assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
|
||||||
|
XX = nil
|
||||||
|
|
||||||
-- test generic load with nested functions
|
-- test generic load with nested functions
|
||||||
x = [[
|
x = [[
|
||||||
@@ -318,8 +384,12 @@ x = [[
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
]]
|
]]
|
||||||
|
a = assert(load(read1(x), "read", "t"))
|
||||||
|
assert(a()(2)(3)(10) == 15)
|
||||||
|
|
||||||
a = assert(load(read1(x)))
|
-- repeat the test loading a binary chunk
|
||||||
|
x = string.dump(a)
|
||||||
|
a = assert(load(read1(x), "read", "b"))
|
||||||
assert(a()(2)(3)(10) == 15)
|
assert(a()(2)(3)(10) == 15)
|
||||||
|
|
||||||
|
|
||||||
@@ -382,20 +452,30 @@ assert((function (a) return a end)() == nil)
|
|||||||
|
|
||||||
print("testing binary chunks")
|
print("testing binary chunks")
|
||||||
do
|
do
|
||||||
local header = string.pack("c4BBBc6BBBj",
|
local header = string.pack("c4BBc6BBB",
|
||||||
"\27Lua", -- signature
|
"\27Lua", -- signature
|
||||||
(504 >> 7) & 0x7f, (504 & 0x7f) | 0x80, -- version 5.4 (504)
|
0x54, -- version 5.4 (0x54)
|
||||||
0, -- format
|
0, -- format
|
||||||
"\x19\x93\r\n\x1a\n", -- data
|
"\x19\x93\r\n\x1a\n", -- data
|
||||||
4, -- size of instruction
|
4, -- size of instruction
|
||||||
string.packsize("j"), -- sizeof(lua integer)
|
string.packsize("j"), -- sizeof(lua integer)
|
||||||
string.packsize("n"), -- sizeof(lua number)
|
string.packsize("n") -- sizeof(lua number)
|
||||||
0x5678 -- LUAC_INT
|
|
||||||
-- LUAC_NUM may not have a unique binary representation (padding...)
|
|
||||||
)
|
)
|
||||||
local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end)
|
local c = string.dump(function ()
|
||||||
|
local a = 1; local b = 3;
|
||||||
|
local f = function () return a + b + _ENV.c; end -- upvalues
|
||||||
|
local s1 = "a constant"
|
||||||
|
local s2 = "another constant"
|
||||||
|
return a + b * 3
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert(assert(load(c))() == 10)
|
||||||
|
|
||||||
|
-- check header
|
||||||
assert(string.sub(c, 1, #header) == 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
|
-- corrupted header
|
||||||
for i = 1, #header do
|
for i = 1, #header do
|
||||||
@@ -411,7 +491,6 @@ do
|
|||||||
local st, msg = load(string.sub(c, 1, i))
|
local st, msg = load(string.sub(c, 1, i))
|
||||||
assert(not st and string.find(msg, "truncated"))
|
assert(not st and string.find(msg, "truncated"))
|
||||||
end
|
end
|
||||||
assert(assert(load(c))() == 10)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
print('OK')
|
print('OK')
|
||||||
|
|||||||
+13
-3
@@ -3,8 +3,16 @@
|
|||||||
|
|
||||||
print "testing closures"
|
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 A,B = 0,{g=10}
|
||||||
function f(x)
|
local function f(x)
|
||||||
local a = {}
|
local a = {}
|
||||||
for i=1,1000 do
|
for i=1,1000 do
|
||||||
local y = 0
|
local y = 0
|
||||||
@@ -89,6 +97,7 @@ assert(r == "a" and s == "b")
|
|||||||
|
|
||||||
|
|
||||||
-- testing closures with 'for' control variable x break
|
-- testing closures with 'for' control variable x break
|
||||||
|
local f
|
||||||
for i=1,3 do
|
for i=1,3 do
|
||||||
f = function () return i end
|
f = function () return i end
|
||||||
break
|
break
|
||||||
@@ -139,7 +148,7 @@ assert(b('get') == 'xuxu')
|
|||||||
b('set', 10); assert(b('get') == 14)
|
b('set', 10); assert(b('get') == 14)
|
||||||
|
|
||||||
|
|
||||||
local w
|
local y, w
|
||||||
-- testing multi-level closure
|
-- testing multi-level closure
|
||||||
function f(x)
|
function f(x)
|
||||||
return function (y)
|
return function (y)
|
||||||
@@ -230,6 +239,7 @@ t()
|
|||||||
-- test for debug manipulation of upvalues
|
-- test for debug manipulation of upvalues
|
||||||
local debug = require'debug'
|
local debug = require'debug'
|
||||||
|
|
||||||
|
local foo1, foo2, foo3
|
||||||
do
|
do
|
||||||
local a , b, c = 3, 5, 7
|
local a , b, c = 3, 5, 7
|
||||||
foo1 = function () return a+b end;
|
foo1 = function () return a+b end;
|
||||||
@@ -242,7 +252,7 @@ end
|
|||||||
|
|
||||||
assert(debug.upvalueid(foo1, 1))
|
assert(debug.upvalueid(foo1, 1))
|
||||||
assert(debug.upvalueid(foo1, 2))
|
assert(debug.upvalueid(foo1, 2))
|
||||||
assert(not pcall(debug.upvalueid, foo1, 3))
|
assert(not debug.upvalueid(foo1, 3))
|
||||||
assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
|
assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
|
||||||
assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
|
assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
|
||||||
assert(debug.upvalueid(foo3, 1))
|
assert(debug.upvalueid(foo3, 1))
|
||||||
|
|||||||
+152
-87
@@ -7,6 +7,23 @@ if T==nil then
|
|||||||
end
|
end
|
||||||
print "testing code generation and optimizations"
|
print "testing code generation and optimizations"
|
||||||
|
|
||||||
|
-- to test constant propagation
|
||||||
|
local k0aux <const> = 0
|
||||||
|
local k0 <const> = k0aux
|
||||||
|
local k1 <const> = 1
|
||||||
|
local k3 <const> = 3
|
||||||
|
local k6 <const> = k3 + (k3 << k0)
|
||||||
|
local kFF0 <const> = 0xFF0
|
||||||
|
local k3_78 <const> = 3.78
|
||||||
|
local x, k3_78_4 <const> = 10, k3_78 / 4
|
||||||
|
assert(x == 10)
|
||||||
|
|
||||||
|
local kx <const> = "x"
|
||||||
|
|
||||||
|
local kTrue <const> = true
|
||||||
|
local kFalse <const> = false
|
||||||
|
|
||||||
|
local kNil <const> = nil
|
||||||
|
|
||||||
-- this code gave an error for the code checker
|
-- this code gave an error for the code checker
|
||||||
do
|
do
|
||||||
@@ -27,21 +44,49 @@ end
|
|||||||
|
|
||||||
local function foo ()
|
local function foo ()
|
||||||
local a
|
local a
|
||||||
a = 3;
|
a = k3;
|
||||||
a = 0; a = 0.0; a = -7 + 7
|
a = 0; a = 0.0; a = -7 + 7
|
||||||
a = 3.78/4; a = 3.78/4
|
a = k3_78/4; a = k3_78_4
|
||||||
a = -3.78/4; a = 3.78/4; a = -3.78/4
|
a = -k3_78/4; a = k3_78/4; a = -3.78/4
|
||||||
a = -3.79/4; a = 0.0; a = -0;
|
a = -3.79/4; a = 0.0; a = -0;
|
||||||
a = 3; a = 3.0; a = 3; a = 3.0
|
a = k3; a = 3.0; a = 3; a = 3.0
|
||||||
end
|
end
|
||||||
|
|
||||||
checkKlist(foo, {3.78/4, -3.78/4, -3.79/4})
|
checkKlist(foo, {3.78/4, -3.78/4, -3.79/4})
|
||||||
|
|
||||||
|
|
||||||
|
foo = function (f, a)
|
||||||
|
f(100 * 1000)
|
||||||
|
f(100.0 * 1000)
|
||||||
|
f(-100 * 1000)
|
||||||
|
f(-100 * 1000.0)
|
||||||
|
f(100000)
|
||||||
|
f(100000.0)
|
||||||
|
f(-100000)
|
||||||
|
f(-100000.0)
|
||||||
|
end
|
||||||
|
|
||||||
|
checkKlist(foo, {100000, 100000.0, -100000, -100000.0})
|
||||||
|
|
||||||
|
|
||||||
|
-- floats x integers
|
||||||
|
foo = function (t, a)
|
||||||
|
t[a] = 1; t[a] = 1.0
|
||||||
|
t[a] = 1; t[a] = 1.0
|
||||||
|
t[a] = 2; t[a] = 2.0
|
||||||
|
t[a] = 0; t[a] = 0.0
|
||||||
|
t[a] = 1; t[a] = 1.0
|
||||||
|
t[a] = 2; t[a] = 2.0
|
||||||
|
t[a] = 0; t[a] = 0.0
|
||||||
|
end
|
||||||
|
|
||||||
|
checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0})
|
||||||
|
|
||||||
|
|
||||||
-- testing opcodes
|
-- testing opcodes
|
||||||
|
|
||||||
-- check that 'f' opcodes match '...'
|
-- check that 'f' opcodes match '...'
|
||||||
function check (f, ...)
|
local function check (f, ...)
|
||||||
local arg = {...}
|
local arg = {...}
|
||||||
local c = T.listcode(f)
|
local c = T.listcode(f)
|
||||||
for i=1, #arg do
|
for i=1, #arg do
|
||||||
@@ -54,7 +99,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- check that 'f' opcodes match '...' and that 'f(p) == r'.
|
-- check that 'f' opcodes match '...' and that 'f(p) == r'.
|
||||||
function checkR (f, p, r, ...)
|
local function checkR (f, p, r, ...)
|
||||||
local r1 = f(p)
|
local r1 = f(p)
|
||||||
assert(r == r1 and math.type(r) == math.type(r1))
|
assert(r == r1 and math.type(r) == math.type(r1))
|
||||||
check(f, ...)
|
check(f, ...)
|
||||||
@@ -62,7 +107,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- check that 'a' and 'b' has the same opcodes
|
-- check that 'a' and 'b' has the same opcodes
|
||||||
function checkequal (a, b)
|
local function checkequal (a, b)
|
||||||
a = T.listcode(a)
|
a = T.listcode(a)
|
||||||
b = T.listcode(b)
|
b = T.listcode(b)
|
||||||
assert(#a == #b)
|
assert(#a == #b)
|
||||||
@@ -77,19 +122,22 @@ end
|
|||||||
-- some basic instructions
|
-- some basic instructions
|
||||||
check(function () -- function does not create upvalues
|
check(function () -- function does not create upvalues
|
||||||
(function () end){f()}
|
(function () end){f()}
|
||||||
end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN0')
|
end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
|
||||||
|
'SETLIST', 'CALL', 'RETURN0')
|
||||||
|
|
||||||
check(function (x) -- function creates upvalues
|
check(function (x) -- function creates upvalues
|
||||||
(function () return x end){f()}
|
(function () return x end){f()}
|
||||||
end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN')
|
end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
|
||||||
|
'SETLIST', 'CALL', 'RETURN')
|
||||||
|
|
||||||
|
|
||||||
-- sequence of LOADNILs
|
-- sequence of LOADNILs
|
||||||
check(function ()
|
check(function ()
|
||||||
|
local kNil <const> = nil
|
||||||
local a,b,c
|
local a,b,c
|
||||||
local d; local e;
|
local d; local e;
|
||||||
local f,g,h;
|
local f,g,h;
|
||||||
d = nil; d=nil; b=nil; a=nil; c=nil;
|
d = nil; d=nil; b=nil; a=kNil; c=nil;
|
||||||
end, 'LOADNIL', 'RETURN0')
|
end, 'LOADNIL', 'RETURN0')
|
||||||
|
|
||||||
check(function ()
|
check(function ()
|
||||||
@@ -109,7 +157,7 @@ check (function (a,b,c) return a end, 'RETURN1')
|
|||||||
|
|
||||||
|
|
||||||
-- infinite loops
|
-- infinite loops
|
||||||
check(function () while true do local a = -1 end end,
|
check(function () while kTrue do local a = -1 end end,
|
||||||
'LOADI', 'JMP', 'RETURN0')
|
'LOADI', 'JMP', 'RETURN0')
|
||||||
|
|
||||||
check(function () while 1 do local a = -1 end end,
|
check(function () while 1 do local a = -1 end end,
|
||||||
@@ -124,10 +172,10 @@ check(function (a,b,c,d) return a..b..c..d end,
|
|||||||
'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1')
|
'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1')
|
||||||
|
|
||||||
-- not
|
-- not
|
||||||
check(function () return not not nil end, 'LOADBOOL', 'RETURN1')
|
check(function () return not not nil end, 'LOADFALSE', 'RETURN1')
|
||||||
check(function () return not not false end, 'LOADBOOL', 'RETURN1')
|
check(function () return not not kFalse end, 'LOADFALSE', 'RETURN1')
|
||||||
check(function () return not not true end, 'LOADBOOL', 'RETURN1')
|
check(function () return not not true end, 'LOADTRUE', 'RETURN1')
|
||||||
check(function () return not not 1 end, 'LOADBOOL', 'RETURN1')
|
check(function () return not not k3 end, 'LOADTRUE', 'RETURN1')
|
||||||
|
|
||||||
-- direct access to locals
|
-- direct access to locals
|
||||||
check(function ()
|
check(function ()
|
||||||
@@ -136,15 +184,16 @@ check(function ()
|
|||||||
c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b
|
c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b
|
||||||
end,
|
end,
|
||||||
'LOADNIL',
|
'LOADNIL',
|
||||||
'MUL',
|
'MUL', 'MMBIN',
|
||||||
'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETFIELD', 'POW',
|
'DIV', 'MMBIN', 'ADD', 'MMBIN', 'GETTABLE', 'SUB', 'MMBIN',
|
||||||
'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0')
|
'GETFIELD', 'POW', 'MMBIN', 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0')
|
||||||
|
|
||||||
|
|
||||||
-- direct access to constants
|
-- direct access to constants
|
||||||
check(function ()
|
check(function ()
|
||||||
local a,b
|
local a,b
|
||||||
a.x = 3.2
|
local c = kNil
|
||||||
|
a[kx] = 3.2
|
||||||
a.x = b
|
a.x = b
|
||||||
a[b] = 'x'
|
a[b] = 'x'
|
||||||
end,
|
end,
|
||||||
@@ -152,8 +201,9 @@ end,
|
|||||||
|
|
||||||
-- "get/set table" with numeric indices
|
-- "get/set table" with numeric indices
|
||||||
check(function (a)
|
check(function (a)
|
||||||
|
local k255 <const> = 255
|
||||||
a[1] = a[100]
|
a[1] = a[100]
|
||||||
a[255] = a[256]
|
a[k255] = a[256]
|
||||||
a[256] = 5
|
a[256] = 5
|
||||||
end,
|
end,
|
||||||
'GETI', 'SETI',
|
'GETI', 'SETI',
|
||||||
@@ -166,13 +216,13 @@ check(function ()
|
|||||||
b = a/a
|
b = a/a
|
||||||
b = 5-4
|
b = 5-4
|
||||||
end,
|
end,
|
||||||
'LOADNIL', 'SUB', 'DIV', 'LOADI', 'RETURN0')
|
'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0')
|
||||||
|
|
||||||
check(function ()
|
check(function ()
|
||||||
local a,b
|
local a,b
|
||||||
a[true] = false
|
a[kTrue] = false
|
||||||
end,
|
end,
|
||||||
'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0')
|
'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0')
|
||||||
|
|
||||||
|
|
||||||
-- equalities
|
-- equalities
|
||||||
@@ -238,68 +288,81 @@ local function checkF (func, val)
|
|||||||
end
|
end
|
||||||
|
|
||||||
checkF(function () return 0.0 end, 0.0)
|
checkF(function () return 0.0 end, 0.0)
|
||||||
checkI(function () return 0 end, 0)
|
checkI(function () return k0 end, 0)
|
||||||
checkI(function () return -0//1 end, 0)
|
checkI(function () return -k0//1 end, 0)
|
||||||
checkK(function () return 3^-1 end, 1/3)
|
checkK(function () return 3^-1 end, 1/3)
|
||||||
checkK(function () return (1 + 1)^(50 + 50) end, 2^100)
|
checkK(function () return (1 + 1)^(50 + 50) end, 2^100)
|
||||||
checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0)
|
checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0)
|
||||||
checkF(function () return (-3^0 + 5) // 3.0 end, 1.0)
|
checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0)
|
||||||
checkI(function () return -3 % 5 end, 2)
|
checkI(function () return -k3 % 5 end, 2)
|
||||||
checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0)
|
checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0)
|
||||||
checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0)
|
checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0)
|
||||||
checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4)
|
checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4)
|
||||||
checkI(function () return ~(~0xFF0 | 0xFF0) end, 0)
|
checkI(function () return ~(~kFF0 | kFF0) end, 0)
|
||||||
checkI(function () return ~~-1024.0 end, -1024)
|
checkI(function () return ~~-1024.0 end, -1024)
|
||||||
checkI(function () return ((100 << 6) << -4) >> 2 end, 100)
|
checkI(function () return ((100 << k6) << -4) >> 2 end, 100)
|
||||||
|
|
||||||
-- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535)
|
-- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535)
|
||||||
local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding
|
local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding
|
||||||
checkI(function () return 65535 end, sbx)
|
local border <const> = 65535
|
||||||
checkI(function () return -65535 end, -sbx)
|
checkI(function () return border end, sbx)
|
||||||
checkI(function () return 65536 end, sbx + 1)
|
checkI(function () return -border end, -sbx)
|
||||||
checkK(function () return 65537 end, sbx + 2)
|
checkI(function () return border + 1 end, sbx + 1)
|
||||||
checkK(function () return -65536 end, -(sbx + 1))
|
checkK(function () return border + 2 end, sbx + 2)
|
||||||
|
checkK(function () return -(border + 1) end, -(sbx + 1))
|
||||||
|
|
||||||
checkF(function () return 65535.0 end, sbx + 0.0)
|
local border <const> = 65535.0
|
||||||
checkF(function () return -65535.0 end, -sbx + 0.0)
|
checkF(function () return border end, sbx + 0.0)
|
||||||
checkF(function () return 65536.0 end, (sbx + 1.0))
|
checkF(function () return -border end, -sbx + 0.0)
|
||||||
checkK(function () return 65537.0 end, (sbx + 2.0))
|
checkF(function () return border + 1 end, (sbx + 1.0))
|
||||||
checkK(function () return -65536.0 end, -(sbx + 1.0))
|
checkK(function () return border + 2 end, (sbx + 2.0))
|
||||||
|
checkK(function () return -(border + 1) end, -(sbx + 1.0))
|
||||||
|
|
||||||
|
|
||||||
-- immediate operands
|
-- immediate operands
|
||||||
checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1')
|
checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1')
|
||||||
checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1')
|
checkR(function (x) return x - 127 end, 10, -117, 'ADDI', 'MMBINI', 'RETURN1')
|
||||||
checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1')
|
checkR(function (x) return 128 + x end, 0.0, 128.0,
|
||||||
checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1')
|
'ADDI', 'MMBINI', 'RETURN1')
|
||||||
checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1')
|
checkR(function (x) return x * -127 end, -1.0, 127.0,
|
||||||
checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1')
|
'MULK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1')
|
checkR(function (x) return 20 * x end, 2, 40, 'MULK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1')
|
checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1')
|
checkR(function (x) return x / 40 end, 40, 1.0, 'DIVK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1')
|
checkR(function (x) return x // 1 end, 10.0, 10.0,
|
||||||
checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1')
|
'IDIVK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1')
|
checkR(function (x) return x % (100 - 10) end, 91, 1,
|
||||||
checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1')
|
'MODK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1')
|
checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1')
|
||||||
|
checkR(function (x) return x << 127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||||
|
checkR(function (x) return x << -127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||||
|
checkR(function (x) return x >> 128 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||||
|
checkR(function (x) return x >> -127 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||||
|
checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1')
|
||||||
|
checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1')
|
||||||
|
checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1')
|
||||||
|
|
||||||
-- K operands in arithmetic operations
|
-- K operands in arithmetic operations
|
||||||
checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1')
|
checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1')
|
||||||
-- check(function (x) return 128 + x end, 'ADDK', 'RETURN1')
|
-- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1')
|
checkR(function (x) return x * -10000 end, 2, -20000,
|
||||||
-- check(function (x) return 20 * x end, 'MULK', 'RETURN1')
|
'MULK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1')
|
-- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1')
|
checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1')
|
checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1')
|
||||||
checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1')
|
checkR(function (x) return x // 10000 end, 10000, 1,
|
||||||
|
'IDIVK', 'MMBINK', 'RETURN1')
|
||||||
|
checkR(function (x) return x % (100.0 - 10) end, 91, 1.0,
|
||||||
|
'MODK', 'MMBINK', 'RETURN1')
|
||||||
|
|
||||||
-- no foldings (and immediate operands)
|
-- no foldings (and immediate operands)
|
||||||
check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1')
|
check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1')
|
||||||
check(function () return 3/0 end, 'LOADI', 'DIVI', 'RETURN1')
|
check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1')
|
||||||
check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1')
|
check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1')
|
||||||
check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1')
|
check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1')
|
||||||
check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1')
|
check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1')
|
||||||
check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'RETURN1')
|
check(function (x) return x << 128 end, 'LOADI', 'SHL', 'MMBIN', 'RETURN1')
|
||||||
|
check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1')
|
||||||
|
|
||||||
-- basic 'for' loops
|
-- basic 'for' loops
|
||||||
check(function () for i = -10, 10.5 do end end,
|
check(function () for i = -10, 10.5 do end end,
|
||||||
@@ -335,7 +398,7 @@ end,
|
|||||||
|
|
||||||
do -- tests for table access in upvalues
|
do -- tests for table access in upvalues
|
||||||
local t
|
local t
|
||||||
check(function () t.x = t.y end, 'GETTABUP', 'SETTABUP')
|
check(function () t[kx] = t.y end, 'GETTABUP', 'SETTABUP')
|
||||||
check(function (a) t[a()] = t[a()] end,
|
check(function (a) t[a()] = t[a()] end,
|
||||||
'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL',
|
'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL',
|
||||||
'GETUPVAL', 'GETTABLE', 'SETTABLE')
|
'GETUPVAL', 'GETTABLE', 'SETTABLE')
|
||||||
@@ -355,30 +418,32 @@ check(function (a, b)
|
|||||||
if b then break else a = a + 1 end
|
if b then break else a = a + 1 end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'JMP', 'RETURN0')
|
'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0')
|
||||||
|
|
||||||
checkequal(
|
checkequal(function () return 6 or true or nil end,
|
||||||
function (a) while a < 10 do a = a + 1 end end,
|
function () return k6 or kTrue or kNil end)
|
||||||
function (a)
|
|
||||||
::loop::
|
|
||||||
if not (a < 10) then goto exit end
|
|
||||||
a = a + 1
|
|
||||||
goto loop
|
|
||||||
::exit::
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
checkequal(
|
checkequal(function () return 6 and true or nil end,
|
||||||
function (a) repeat local x = a + 1; a = x until a > 0 end,
|
function () return k6 and kTrue or kNil end)
|
||||||
function (a)
|
|
||||||
::loop:: do
|
|
||||||
local x = a + 1
|
do -- string constants
|
||||||
a = x
|
local k0 <const> = "00000000000000000000000000000000000000000000000000"
|
||||||
|
local function f1 ()
|
||||||
|
local k <const> = k0
|
||||||
|
return function ()
|
||||||
|
return function () return k end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if not (a > 0) then goto loop end
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
|
local f2 = f1()
|
||||||
|
local f3 = f2()
|
||||||
|
assert(f3() == k0)
|
||||||
|
checkK(f3, k0)
|
||||||
|
-- string is not needed by other functions
|
||||||
|
assert(T.listk(f1)[1] == nil)
|
||||||
|
assert(T.listk(f2)[1] == nil)
|
||||||
|
end
|
||||||
|
|
||||||
print 'OK'
|
print 'OK'
|
||||||
|
|
||||||
|
|||||||
+72
-25
@@ -11,6 +11,7 @@ local function checkload (s, msg)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- testing semicollons
|
-- testing semicollons
|
||||||
|
local a
|
||||||
do ;;; end
|
do ;;; end
|
||||||
; do ; a = 3; assert(a == 3) end;
|
; do ; a = 3; assert(a == 3) end;
|
||||||
;
|
;
|
||||||
@@ -49,10 +50,10 @@ assert((((nil and true) or false) and true) == false)
|
|||||||
|
|
||||||
local a,b = 1,nil;
|
local a,b = 1,nil;
|
||||||
assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
|
assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
|
||||||
x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
|
local x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
|
||||||
x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
|
x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
|
||||||
|
|
||||||
x,y=1,2;
|
local x, y = 1, 2;
|
||||||
assert((x>y) and x or y == 2);
|
assert((x>y) and x or y == 2);
|
||||||
x,y=2,1;
|
x,y=2,1;
|
||||||
assert((x>y) and x or y == 2);
|
assert((x>y) and x or y == 2);
|
||||||
@@ -77,13 +78,13 @@ do -- testing operators with diffent kinds of constants
|
|||||||
local gab = f(o1, o2)
|
local gab = f(o1, o2)
|
||||||
|
|
||||||
_ENV.XX = o1
|
_ENV.XX = o1
|
||||||
code = string.format("return XX %s %s", op, o2)
|
local code = string.format("return XX %s %s", op, o2)
|
||||||
res = assert(load(code))()
|
local res = assert(load(code))()
|
||||||
assert(res == gab)
|
assert(res == gab)
|
||||||
|
|
||||||
_ENV.XX = o2
|
_ENV.XX = o2
|
||||||
local code = string.format("return (%s) %s XX", o1, op)
|
code = string.format("return (%s) %s XX", o1, op)
|
||||||
local res = assert(load(code))()
|
res = assert(load(code))()
|
||||||
assert(res == gab)
|
assert(res == gab)
|
||||||
|
|
||||||
code = string.format("return (%s) %s %s", o1, op, o2)
|
code = string.format("return (%s) %s %s", o1, op, o2)
|
||||||
@@ -92,6 +93,7 @@ do -- testing operators with diffent kinds of constants
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
_ENV.XX = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -100,10 +102,35 @@ repeat until 1; repeat until true;
|
|||||||
while false do end; while nil do end;
|
while false do end; while nil do end;
|
||||||
|
|
||||||
do -- test old bug (first name could not be an `upvalue')
|
do -- test old bug (first name could not be an `upvalue')
|
||||||
local a; function f(x) x={a=1}; x={x=1}; x={G=1} end
|
local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end
|
||||||
end
|
end
|
||||||
|
|
||||||
function f (i)
|
|
||||||
|
do -- bug since 5.4.0
|
||||||
|
-- create code with a table using more than 256 constants
|
||||||
|
local code = {"local x = {"}
|
||||||
|
for i = 1, 257 do
|
||||||
|
code[#code + 1] = i .. ".1,"
|
||||||
|
end
|
||||||
|
code[#code + 1] = "};"
|
||||||
|
code = table.concat(code)
|
||||||
|
|
||||||
|
-- add "ret" to the end of that code and checks that
|
||||||
|
-- it produces the expected value "val"
|
||||||
|
local function check (ret, val)
|
||||||
|
local code = code .. ret
|
||||||
|
code = load(code)
|
||||||
|
assert(code() == val)
|
||||||
|
end
|
||||||
|
|
||||||
|
check("return (1 ~ (2 or 3))", 1 ~ 2)
|
||||||
|
check("return (1 | (2 or 3))", 1 | 2)
|
||||||
|
check("return (1 + (2 or 3))", 1 + 2)
|
||||||
|
check("return (1 << (2 or 3))", 1 << 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function f (i)
|
||||||
if type(i) ~= 'number' then return i,'jojo'; end;
|
if type(i) ~= 'number' then return i,'jojo'; end;
|
||||||
if i > 0 then return i, f(i-1); end;
|
if i > 0 then return i, f(i-1); end;
|
||||||
end
|
end
|
||||||
@@ -129,10 +156,10 @@ end
|
|||||||
assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
|
assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
|
||||||
|
|
||||||
for i=1,1000 do break; end;
|
for i=1,1000 do break; end;
|
||||||
n=100;
|
local n=100;
|
||||||
i=3;
|
local i=3;
|
||||||
t = {};
|
local t = {};
|
||||||
a=nil
|
local a=nil
|
||||||
while not a do
|
while not a do
|
||||||
a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
|
a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
|
||||||
end
|
end
|
||||||
@@ -175,14 +202,14 @@ a={y=1}
|
|||||||
x = {a.y}
|
x = {a.y}
|
||||||
assert(x[1] == 1)
|
assert(x[1] == 1)
|
||||||
|
|
||||||
function f(i)
|
local function f (i)
|
||||||
while 1 do
|
while 1 do
|
||||||
if i>0 then i=i-1;
|
if i>0 then i=i-1;
|
||||||
else return; end;
|
else return; end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function g(i)
|
local function g(i)
|
||||||
while 1 do
|
while 1 do
|
||||||
if i>0 then i=i-1
|
if i>0 then i=i-1
|
||||||
else return end
|
else return end
|
||||||
@@ -211,15 +238,15 @@ assert(a==1 and b==nil)
|
|||||||
print'+';
|
print'+';
|
||||||
|
|
||||||
do -- testing constants
|
do -- testing constants
|
||||||
local <const> prog = [[local <XXX> x = 10]]
|
local prog <const> = [[local x <XXX> = 10]]
|
||||||
checkload(prog, "unknown attribute 'XXX'")
|
checkload(prog, "unknown attribute 'XXX'")
|
||||||
|
|
||||||
checkload([[local <const> xxx = 20; xxx = 10]],
|
checkload([[local xxx <const> = 20; xxx = 10]],
|
||||||
":1: attempt to assign to const variable 'xxx'")
|
":1: attempt to assign to const variable 'xxx'")
|
||||||
|
|
||||||
checkload([[
|
checkload([[
|
||||||
local xx;
|
local xx;
|
||||||
local <const> xxx = 20;
|
local xxx <const> = 20;
|
||||||
local yyy;
|
local yyy;
|
||||||
local function foo ()
|
local function foo ()
|
||||||
local abc = xx + yyy + xxx;
|
local abc = xx + yyy + xxx;
|
||||||
@@ -228,7 +255,7 @@ do -- testing constants
|
|||||||
]], ":6: attempt to assign to const variable 'xxx'")
|
]], ":6: attempt to assign to const variable 'xxx'")
|
||||||
|
|
||||||
checkload([[
|
checkload([[
|
||||||
local <toclose> x = nil
|
local x <close> = nil
|
||||||
x = io.open()
|
x = io.open()
|
||||||
]], ":2: attempt to assign to const variable 'x'")
|
]], ":2: attempt to assign to const variable 'x'")
|
||||||
end
|
end
|
||||||
@@ -247,7 +274,7 @@ function g (a,b,c,d,e)
|
|||||||
if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
|
if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
|
||||||
end
|
end
|
||||||
|
|
||||||
function h (a,b,c,d,e)
|
local function h (a,b,c,d,e)
|
||||||
while (a>=b or c or (d and e) or nil) do return 1; end;
|
while (a>=b or c or (d and e) or nil) do return 1; end;
|
||||||
return 0;
|
return 0;
|
||||||
end;
|
end;
|
||||||
@@ -275,7 +302,7 @@ do
|
|||||||
assert(a==2)
|
assert(a==2)
|
||||||
end
|
end
|
||||||
|
|
||||||
function F(a)
|
local function F (a)
|
||||||
assert(debug.getinfo(1, "n").name == 'F')
|
assert(debug.getinfo(1, "n").name == 'F')
|
||||||
return a,2,3
|
return a,2,3
|
||||||
end
|
end
|
||||||
@@ -287,7 +314,7 @@ a,b = F(nil)==nil; assert(a == true and b == nil)
|
|||||||
------------------------------------------------------------------
|
------------------------------------------------------------------
|
||||||
|
|
||||||
-- sometimes will be 0, sometimes will not...
|
-- sometimes will be 0, sometimes will not...
|
||||||
_ENV.GLOB1 = math.floor(os.time()) % 2
|
_ENV.GLOB1 = math.random(0, 1)
|
||||||
|
|
||||||
-- basic expressions with their respective values
|
-- basic expressions with their respective values
|
||||||
local basiccases = {
|
local basiccases = {
|
||||||
@@ -298,16 +325,36 @@ local basiccases = {
|
|||||||
{"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
|
{"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local prog
|
||||||
|
|
||||||
|
if _ENV.GLOB1 == 0 then
|
||||||
|
basiccases[2][1] = "F" -- constant false
|
||||||
|
|
||||||
|
prog = [[
|
||||||
|
local F <const> = false
|
||||||
|
if %s then IX = true end
|
||||||
|
return %s
|
||||||
|
]]
|
||||||
|
else
|
||||||
|
basiccases[4][1] = "k10" -- constant 10
|
||||||
|
|
||||||
|
prog = [[
|
||||||
|
local k10 <const> = 10
|
||||||
|
if %s then IX = true end
|
||||||
|
return %s
|
||||||
|
]]
|
||||||
|
end
|
||||||
|
|
||||||
print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
|
print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
|
||||||
|
|
||||||
|
|
||||||
-- operators with their respective values
|
-- operators with their respective values
|
||||||
local <const> binops = {
|
local binops <const> = {
|
||||||
{" and ", function (a,b) if not a then return a else return b end end},
|
{" and ", function (a,b) if not a then return a else return b end end},
|
||||||
{" or ", function (a,b) if a then return a else return b end end},
|
{" or ", function (a,b) if a then return a else return b end end},
|
||||||
}
|
}
|
||||||
|
|
||||||
local <const> cases = {}
|
local cases <const> = {}
|
||||||
|
|
||||||
-- creates all combinations of '(cases[i] op cases[n-i])' plus
|
-- creates all combinations of '(cases[i] op cases[n-i])' plus
|
||||||
-- 'not(cases[i] op cases[n-i])' (syntax + value)
|
-- 'not(cases[i] op cases[n-i])' (syntax + value)
|
||||||
@@ -337,8 +384,6 @@ cases[1] = basiccases
|
|||||||
for i = 2, level do cases[i] = createcases(i) end
|
for i = 2, level do cases[i] = createcases(i) end
|
||||||
print("+")
|
print("+")
|
||||||
|
|
||||||
local prog = [[if %s then IX = true end; return %s]]
|
|
||||||
|
|
||||||
local i = 0
|
local i = 0
|
||||||
for n = 1, level do
|
for n = 1, level do
|
||||||
for _, v in pairs(cases[n]) do
|
for _, v in pairs(cases[n]) do
|
||||||
@@ -350,6 +395,8 @@ for n = 1, level do
|
|||||||
if i % 60000 == 0 then print('+') end
|
if i % 60000 == 0 then print('+') end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
IX = nil
|
||||||
|
_G.GLOB1 = nil
|
||||||
------------------------------------------------------------------
|
------------------------------------------------------------------
|
||||||
|
|
||||||
-- testing some syntax errors (chosen through 'gcov')
|
-- testing some syntax errors (chosen through 'gcov')
|
||||||
|
|||||||
+236
-46
@@ -30,7 +30,8 @@ local function eqtab (t1, t2)
|
|||||||
end
|
end
|
||||||
|
|
||||||
_G.x = nil -- declare x
|
_G.x = nil -- declare x
|
||||||
function foo (a, ...)
|
_G.f = nil -- declare f
|
||||||
|
local function foo (a, ...)
|
||||||
local x, y = coroutine.running()
|
local x, y = coroutine.running()
|
||||||
assert(x == f and y == false)
|
assert(x == f and y == false)
|
||||||
-- next call should not corrupt coroutine (but must fail,
|
-- next call should not corrupt coroutine (but must fail,
|
||||||
@@ -67,10 +68,11 @@ assert(coroutine.status(f) == "dead")
|
|||||||
s, a = coroutine.resume(f, "xuxu")
|
s, a = coroutine.resume(f, "xuxu")
|
||||||
assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead")
|
assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead")
|
||||||
|
|
||||||
|
_G.f = nil
|
||||||
|
|
||||||
-- yields in tail calls
|
-- yields in tail calls
|
||||||
local function foo (i) return coroutine.yield(i) end
|
local function foo (i) return coroutine.yield(i) end
|
||||||
f = coroutine.wrap(function ()
|
local f = coroutine.wrap(function ()
|
||||||
for i=1,10 do
|
for i=1,10 do
|
||||||
assert(foo(i) == _G.x)
|
assert(foo(i) == _G.x)
|
||||||
end
|
end
|
||||||
@@ -79,8 +81,10 @@ end)
|
|||||||
for i=1,10 do _G.x = i; assert(f(i) == i) end
|
for i=1,10 do _G.x = i; assert(f(i) == i) end
|
||||||
_G.x = 'xuxu'; assert(f('xuxu') == 'a')
|
_G.x = 'xuxu'; assert(f('xuxu') == 'a')
|
||||||
|
|
||||||
|
_G.x = nil
|
||||||
|
|
||||||
-- recursive
|
-- recursive
|
||||||
function pf (n, i)
|
local function pf (n, i)
|
||||||
coroutine.yield(n)
|
coroutine.yield(n)
|
||||||
pf(n*i, i+1)
|
pf(n*i, i+1)
|
||||||
end
|
end
|
||||||
@@ -93,14 +97,14 @@ for i=1,10 do
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- sieve
|
-- sieve
|
||||||
function gen (n)
|
local function gen (n)
|
||||||
return coroutine.wrap(function ()
|
return coroutine.wrap(function ()
|
||||||
for i=2,n do coroutine.yield(i) end
|
for i=2,n do coroutine.yield(i) end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function filter (p, g)
|
local function filter (p, g)
|
||||||
return coroutine.wrap(function ()
|
return coroutine.wrap(function ()
|
||||||
while 1 do
|
while 1 do
|
||||||
local n = g()
|
local n = g()
|
||||||
@@ -123,13 +127,23 @@ assert(#a == 22 and a[#a] == 79)
|
|||||||
x, a = nil
|
x, a = nil
|
||||||
|
|
||||||
|
|
||||||
-- coroutine closing
|
print("to-be-closed variables in coroutines")
|
||||||
|
|
||||||
|
local function func2close (f)
|
||||||
|
return setmetatable({}, {__close = f})
|
||||||
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
-- ok to close a dead coroutine
|
-- ok to close a dead coroutine
|
||||||
local co = coroutine.create(print)
|
local co = coroutine.create(print)
|
||||||
assert(coroutine.resume(co, "testing 'coroutine.close'"))
|
assert(coroutine.resume(co, "testing 'coroutine.close'"))
|
||||||
assert(coroutine.status(co) == "dead")
|
assert(coroutine.status(co) == "dead")
|
||||||
assert(coroutine.close(co))
|
local st, msg = coroutine.close(co)
|
||||||
|
assert(st and msg == nil)
|
||||||
|
-- also ok to close it again
|
||||||
|
st, msg = coroutine.close(co)
|
||||||
|
assert(st and msg == nil)
|
||||||
|
|
||||||
|
|
||||||
-- cannot close the running coroutine
|
-- cannot close the running coroutine
|
||||||
local st, msg = pcall(coroutine.close, coroutine.running())
|
local st, msg = pcall(coroutine.close, coroutine.running())
|
||||||
@@ -143,15 +157,37 @@ do
|
|||||||
assert(not st and string.find(msg, "normal"))
|
assert(not st and string.find(msg, "normal"))
|
||||||
end))()
|
end))()
|
||||||
|
|
||||||
|
-- cannot close a coroutine while closing it
|
||||||
|
do
|
||||||
|
local co
|
||||||
|
co = coroutine.create(
|
||||||
|
function()
|
||||||
|
local x <close> = func2close(function()
|
||||||
|
coroutine.close(co) -- try to 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"))
|
||||||
|
end
|
||||||
|
|
||||||
-- to-be-closed variables in coroutines
|
-- to-be-closed variables in coroutines
|
||||||
local X
|
local X
|
||||||
|
|
||||||
local function func2close (f)
|
-- closing a coroutine after an error
|
||||||
return setmetatable({}, {__close = f})
|
local co = coroutine.create(error)
|
||||||
end
|
local st, msg = coroutine.resume(co, 100)
|
||||||
|
assert(not st and msg == 100)
|
||||||
|
st, msg = coroutine.close(co)
|
||||||
|
assert(not st and msg == 100)
|
||||||
|
-- after closing, no more errors
|
||||||
|
st, msg = coroutine.close(co)
|
||||||
|
assert(st and msg == nil)
|
||||||
|
|
||||||
co = coroutine.create(function ()
|
co = coroutine.create(function ()
|
||||||
local <toclose> x = func2close(function (self, err)
|
local x <close> = func2close(function (self, err)
|
||||||
assert(err == nil); X = false
|
assert(err == nil); X = false
|
||||||
end)
|
end)
|
||||||
X = true
|
X = true
|
||||||
@@ -165,12 +201,12 @@ do
|
|||||||
-- error closing a coroutine
|
-- error closing a coroutine
|
||||||
local x = 0
|
local x = 0
|
||||||
co = coroutine.create(function()
|
co = coroutine.create(function()
|
||||||
local <toclose> y = func2close(function (self,err)
|
local y <close> = func2close(function (self,err)
|
||||||
if (err ~= 111) then os.exit(false) end -- should not happen
|
assert(err == 111)
|
||||||
x = 200
|
x = 200
|
||||||
error(200)
|
error(200)
|
||||||
end)
|
end)
|
||||||
local <toclose> x = func2close(function (self, err)
|
local x <close> = func2close(function (self, err)
|
||||||
assert(err == nil); error(111)
|
assert(err == nil); error(111)
|
||||||
end)
|
end)
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
@@ -178,15 +214,72 @@ do
|
|||||||
coroutine.resume(co)
|
coroutine.resume(co)
|
||||||
assert(x == 0)
|
assert(x == 0)
|
||||||
local st, msg = coroutine.close(co)
|
local st, msg = coroutine.close(co)
|
||||||
assert(st == false and coroutine.status(co) == "dead" and msg == 111)
|
assert(st == false and coroutine.status(co) == "dead" and msg == 200)
|
||||||
assert(x == 200)
|
assert(x == 200)
|
||||||
|
-- after closing, no more errors
|
||||||
|
st, msg = coroutine.close(co)
|
||||||
|
assert(st and msg == nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
-- <close> versus pcall in coroutines
|
||||||
|
local X = false
|
||||||
|
local Y = false
|
||||||
|
local function foo ()
|
||||||
|
local x <close> = func2close(function (self, err)
|
||||||
|
Y = debug.getinfo(2)
|
||||||
|
X = err
|
||||||
|
end)
|
||||||
|
error(43)
|
||||||
|
end
|
||||||
|
local co = coroutine.create(function () return pcall(foo) end)
|
||||||
|
local st1, st2, err = coroutine.resume(co)
|
||||||
|
assert(st1 and not st2 and err == 43)
|
||||||
|
assert(X == 43 and Y.what == "C")
|
||||||
|
|
||||||
|
-- recovering from errors in __close metamethods
|
||||||
|
local track = {}
|
||||||
|
|
||||||
|
local function h (o)
|
||||||
|
local hv <close> = o
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local function foo ()
|
||||||
|
local x <close> = func2close(function(_,msg)
|
||||||
|
track[#track + 1] = msg or false
|
||||||
|
error(20)
|
||||||
|
end)
|
||||||
|
local y <close> = func2close(function(_,msg)
|
||||||
|
track[#track + 1] = msg or false
|
||||||
|
return 1000
|
||||||
|
end)
|
||||||
|
local z <close> = func2close(function(_,msg)
|
||||||
|
track[#track + 1] = msg or false
|
||||||
|
error(10)
|
||||||
|
end)
|
||||||
|
coroutine.yield(1)
|
||||||
|
h(func2close(function(_,msg)
|
||||||
|
track[#track + 1] = msg or false
|
||||||
|
error(2)
|
||||||
|
end))
|
||||||
|
end
|
||||||
|
|
||||||
|
local co = coroutine.create(pcall)
|
||||||
|
|
||||||
|
local st, res = coroutine.resume(co, foo) -- call 'foo' protected
|
||||||
|
assert(st and res == 1) -- yield 1
|
||||||
|
local st, res1, res2 = coroutine.resume(co) -- continue
|
||||||
|
assert(coroutine.status(co) == "dead")
|
||||||
|
assert(st and not res1 and res2 == 20) -- last error (20)
|
||||||
|
assert(track[1] == false and track[2] == 2 and track[3] == 10 and
|
||||||
|
track[4] == 10)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- yielding across C boundaries
|
-- yielding across C boundaries
|
||||||
|
|
||||||
co = coroutine.wrap(function()
|
local co = coroutine.wrap(function()
|
||||||
assert(not pcall(table.sort,{1,2,3}, coroutine.yield))
|
assert(not pcall(table.sort,{1,2,3}, coroutine.yield))
|
||||||
assert(coroutine.isyieldable())
|
assert(coroutine.isyieldable())
|
||||||
coroutine.yield(20)
|
coroutine.yield(20)
|
||||||
@@ -214,15 +307,15 @@ local r1, r2, v = f1(nil)
|
|||||||
assert(r1 and not r2 and v[1] == (10 + 1)*10/2)
|
assert(r1 and not r2 and v[1] == (10 + 1)*10/2)
|
||||||
|
|
||||||
|
|
||||||
function f (a, b) a = coroutine.yield(a); error{a + b} end
|
local function f (a, b) a = coroutine.yield(a); error{a + b} end
|
||||||
function g(x) return x[1]*2 end
|
local function g(x) return x[1]*2 end
|
||||||
|
|
||||||
co = coroutine.wrap(function ()
|
co = coroutine.wrap(function ()
|
||||||
coroutine.yield(xpcall(f, g, 10, 20))
|
coroutine.yield(xpcall(f, g, 10, 20))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(co() == 10)
|
assert(co() == 10)
|
||||||
r, msg = co(100)
|
local r, msg = co(100)
|
||||||
assert(not r and msg == 240)
|
assert(not r and msg == 240)
|
||||||
|
|
||||||
|
|
||||||
@@ -284,9 +377,10 @@ assert(not a and b == foo and coroutine.status(x) == "dead")
|
|||||||
a,b = coroutine.resume(x)
|
a,b = coroutine.resume(x)
|
||||||
assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead")
|
assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead")
|
||||||
|
|
||||||
|
goo = nil
|
||||||
|
|
||||||
-- co-routines x for loop
|
-- co-routines x for loop
|
||||||
function all (a, n, k)
|
local function all (a, n, k)
|
||||||
if k == 0 then coroutine.yield(a)
|
if k == 0 then coroutine.yield(a)
|
||||||
else
|
else
|
||||||
for i=1,n do
|
for i=1,n do
|
||||||
@@ -326,7 +420,7 @@ assert(f() == 43 and f() == 53)
|
|||||||
|
|
||||||
-- old bug: attempt to resume itself
|
-- old bug: attempt to resume itself
|
||||||
|
|
||||||
function co_func (current_co)
|
local function co_func (current_co)
|
||||||
assert(coroutine.running() == current_co)
|
assert(coroutine.running() == current_co)
|
||||||
assert(coroutine.resume(current_co) == false)
|
assert(coroutine.resume(current_co) == false)
|
||||||
coroutine.yield(10, 20)
|
coroutine.yield(10, 20)
|
||||||
@@ -356,7 +450,7 @@ do
|
|||||||
|
|
||||||
local X = false
|
local X = false
|
||||||
A = coroutine.wrap(function()
|
A = coroutine.wrap(function()
|
||||||
local <toclose> _ = setmetatable({}, {__close = function () X = true end})
|
local _ <close> = func2close(function () X = true end)
|
||||||
return pcall(A, 1)
|
return pcall(A, 1)
|
||||||
end)
|
end)
|
||||||
st, res = A()
|
st, res = A()
|
||||||
@@ -364,6 +458,22 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- bug in 5.4.1
|
||||||
|
do
|
||||||
|
-- coroutine ran close metamethods with invalid status during a
|
||||||
|
-- reset.
|
||||||
|
local co
|
||||||
|
co = coroutine.wrap(function()
|
||||||
|
local x <close> = func2close(function() return pcall(co) end)
|
||||||
|
error(111)
|
||||||
|
end)
|
||||||
|
local st, errobj = pcall(co)
|
||||||
|
assert(not st and errobj == 111)
|
||||||
|
st, errobj = pcall(co)
|
||||||
|
assert(not st and string.find(errobj, "dead coroutine"))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- attempt to resume 'normal' coroutine
|
-- attempt to resume 'normal' coroutine
|
||||||
local co1, co2
|
local co1, co2
|
||||||
co1 = coroutine.create(function () return co2() end)
|
co1 = coroutine.create(function () return co2() end)
|
||||||
@@ -383,28 +493,49 @@ assert(not pcall(a, a))
|
|||||||
a = nil
|
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
|
-- access to locals of erroneous coroutines
|
||||||
local x = coroutine.create (function ()
|
local x = coroutine.create (function ()
|
||||||
local a = 10
|
local a = 10
|
||||||
_G.f = function () a=a+1; return a end
|
_G.F = function () a=a+1; return a end
|
||||||
error('x')
|
error('x')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(not coroutine.resume(x))
|
assert(not coroutine.resume(x))
|
||||||
-- overwrite previous position of local `a'
|
-- overwrite previous position of local `a'
|
||||||
assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1))
|
assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1))
|
||||||
assert(_G.f() == 11)
|
assert(_G.F() == 11)
|
||||||
assert(_G.f() == 12)
|
assert(_G.F() == 12)
|
||||||
|
_G.F = nil
|
||||||
|
|
||||||
|
|
||||||
if not T then
|
if not T then
|
||||||
(Message or print)('\n >>> testC not active: skipping yield/hook tests <<<\n')
|
(Message or print)
|
||||||
|
('\n >>> testC not active: skipping coroutine API tests <<<\n')
|
||||||
else
|
else
|
||||||
print "testing yields inside hooks"
|
print "testing yields inside hooks"
|
||||||
|
|
||||||
local turn
|
local turn
|
||||||
|
|
||||||
function fact (t, x)
|
local function fact (t, x)
|
||||||
assert(turn == t)
|
assert(turn == t)
|
||||||
if x == 0 then return 1
|
if x == 0 then return 1
|
||||||
else return x*fact(t, x-1)
|
else return x*fact(t, x-1)
|
||||||
@@ -426,10 +557,36 @@ else
|
|||||||
while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y')
|
while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y')
|
||||||
if A==0 then turn = "A"; assert(T.resume(x)) end
|
if A==0 then turn = "A"; assert(T.resume(x)) end
|
||||||
if B==0 then turn = "B"; assert(T.resume(y)) end
|
if B==0 then turn = "B"; assert(T.resume(y)) end
|
||||||
|
|
||||||
|
-- check that traceback works correctly after yields inside hooks
|
||||||
|
debug.traceback(x)
|
||||||
|
debug.traceback(y)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(B // A == 7) -- fact(7) // fact(6)
|
assert(B // A == 7) -- fact(7) // fact(6)
|
||||||
|
|
||||||
|
do -- hooks vs. multiple values
|
||||||
|
local done
|
||||||
|
local function test (n)
|
||||||
|
done = false
|
||||||
|
return coroutine.wrap(function ()
|
||||||
|
local a = {}
|
||||||
|
for i = 1, n do a[i] = i end
|
||||||
|
-- 'pushint' just to perturb the stack
|
||||||
|
T.sethook("pushint 10; yield 0", "", 1) -- yield at each op.
|
||||||
|
local a1 = {table.unpack(a)} -- must keep top between ops.
|
||||||
|
assert(#a1 == n)
|
||||||
|
for i = 1, n do assert(a[i] == i) end
|
||||||
|
done = true
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
-- arguments to the coroutine are just to perturb its stack
|
||||||
|
local co = test(0); while not done do co(30) end
|
||||||
|
co = test(1); while not done do co(20, 10) end
|
||||||
|
co = test(3); while not done do co() end
|
||||||
|
co = test(100); while not done do co() end
|
||||||
|
end
|
||||||
|
|
||||||
local line = debug.getinfo(1, "l").currentline + 2 -- get line number
|
local line = debug.getinfo(1, "l").currentline + 2 -- get line number
|
||||||
local function foo ()
|
local function foo ()
|
||||||
local x = 10 --<< this line is 'line'
|
local x = 10 --<< this line is 'line'
|
||||||
@@ -447,6 +604,7 @@ else
|
|||||||
_G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil)
|
_G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil)
|
||||||
_G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20)
|
_G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20)
|
||||||
assert(co() == 10)
|
assert(co() == 10)
|
||||||
|
_G.X = nil
|
||||||
|
|
||||||
-- testing yields in count hook
|
-- testing yields in count hook
|
||||||
co = coroutine.wrap(function ()
|
co = coroutine.wrap(function ()
|
||||||
@@ -471,18 +629,20 @@ else
|
|||||||
-- (bug in 5.2/5.3)
|
-- (bug in 5.2/5.3)
|
||||||
c = coroutine.create(function (a, ...)
|
c = coroutine.create(function (a, ...)
|
||||||
T.sethook("yield 0", "l") -- will yield on next two lines
|
T.sethook("yield 0", "l") -- will yield on next two lines
|
||||||
assert(a == 10)
|
local b = a
|
||||||
return ...
|
return ...
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine
|
assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine
|
||||||
local n,v = debug.getlocal(c, 0, 1) -- check its local
|
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'
|
assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal'
|
||||||
local t = debug.getinfo(c, 0) -- test 'getinfo'
|
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(not debug.getinfo(c, 1)) -- no other level
|
||||||
assert(coroutine.resume(c)) -- run next line
|
assert(coroutine.resume(c)) -- run next line
|
||||||
|
local n,v = debug.getlocal(c, 0, 2) -- check next local
|
||||||
|
assert(n == "b" and v == 10)
|
||||||
v = {coroutine.resume(c)} -- finish coroutine
|
v = {coroutine.resume(c)} -- finish coroutine
|
||||||
assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef)
|
assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef)
|
||||||
assert(not coroutine.resume(c))
|
assert(not coroutine.resume(c))
|
||||||
@@ -524,6 +684,8 @@ else
|
|||||||
|
|
||||||
assert(X == 'a a a' and Y == 'OK')
|
assert(X == 'a a a' and Y == 'OK')
|
||||||
|
|
||||||
|
X, Y = nil
|
||||||
|
|
||||||
|
|
||||||
-- resuming running coroutine
|
-- resuming running coroutine
|
||||||
C = coroutine.create(function ()
|
C = coroutine.create(function ()
|
||||||
@@ -551,8 +713,17 @@ else
|
|||||||
c == "ERRRUN" and d == 4)
|
c == "ERRRUN" and d == 4)
|
||||||
|
|
||||||
|
|
||||||
-- using a main thread as a coroutine
|
-- using a main thread as a coroutine (dubious use!)
|
||||||
local state = T.newstate()
|
local state = T.newstate()
|
||||||
|
|
||||||
|
-- check that yielddable is working correctly
|
||||||
|
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"))
|
||||||
|
|
||||||
|
T.testC(state, "settop 0")
|
||||||
|
|
||||||
T.loadlib(state)
|
T.loadlib(state)
|
||||||
|
|
||||||
assert(T.doremote(state, [[
|
assert(T.doremote(state, [[
|
||||||
@@ -560,7 +731,7 @@ else
|
|||||||
X = function (x) coroutine.yield(x, 'BB'); return 'CC' end;
|
X = function (x) coroutine.yield(x, 'BB'); return 'CC' end;
|
||||||
return 'ok']]))
|
return 'ok']]))
|
||||||
|
|
||||||
t = table.pack(T.testC(state, [[
|
local t = table.pack(T.testC(state, [[
|
||||||
rawgeti R 1 # get main thread
|
rawgeti R 1 # get main thread
|
||||||
pushstring 'XX'
|
pushstring 'XX'
|
||||||
getglobal X # get function for body
|
getglobal X # get function for body
|
||||||
@@ -589,31 +760,28 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- leaving a pending coroutine open
|
-- leaving a pending coroutine open
|
||||||
_X = coroutine.wrap(function ()
|
_G.TO_SURVIVE = coroutine.wrap(function ()
|
||||||
local a = 10
|
local a = 10
|
||||||
local x = function () a = a+1 end
|
local x = function () a = a+1 end
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
_X()
|
_G.TO_SURVIVE()
|
||||||
|
|
||||||
|
|
||||||
if not _soft then
|
if not _soft then
|
||||||
-- bug (stack overflow)
|
-- bug (stack overflow)
|
||||||
local j = 2^9
|
local lim = 1000000 -- stack limit; assume 32-bit machine
|
||||||
local lim = 1000000 -- (C stack limit; assume 32-bit machine)
|
local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1, lim + 5}
|
||||||
local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1}
|
|
||||||
for i = 1, #t do
|
for i = 1, #t do
|
||||||
local j = t[i]
|
local j = t[i]
|
||||||
co = coroutine.create(function()
|
local co = coroutine.create(function()
|
||||||
local t = {}
|
return table.unpack({}, 1, j)
|
||||||
for i = 1, j do t[i] = i end
|
|
||||||
return table.unpack(t)
|
|
||||||
end)
|
end)
|
||||||
local r, msg = coroutine.resume(co)
|
local r, msg = coroutine.resume(co)
|
||||||
assert(not r)
|
-- must fail for unpacking larger than stack limit
|
||||||
|
assert(j < lim or not r)
|
||||||
end
|
end
|
||||||
co = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -711,6 +879,17 @@ assert(run(function () return a / b end, {"div"}) == 10/12)
|
|||||||
assert(run(function () return a % b end, {"mod"}) == 10)
|
assert(run(function () return a % b end, {"mod"}) == 10)
|
||||||
assert(run(function () return a // b end, {"idiv"}) == 0)
|
assert(run(function () return a // b end, {"idiv"}) == 0)
|
||||||
|
|
||||||
|
-- repeat tests with larger constants (to use 'K' opcodes)
|
||||||
|
local a1000 = new(1000)
|
||||||
|
|
||||||
|
assert(run(function () return a1000 + 1000 end, {"add"}) == 2000)
|
||||||
|
assert(run(function () return a1000 - 25000 end, {"sub"}) == -24000)
|
||||||
|
assert(run(function () return 2000 * a end, {"mul"}) == 20000)
|
||||||
|
assert(run(function () return a1000 / 1000 end, {"div"}) == 1)
|
||||||
|
assert(run(function () return a1000 % 600 end, {"mod"}) == 400)
|
||||||
|
assert(run(function () return a1000 // 500 end, {"idiv"}) == 2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
assert(run(function () return a % b end, {"mod"}) == 10)
|
assert(run(function () return a % b end, {"mod"}) == 10)
|
||||||
|
|
||||||
@@ -723,6 +902,12 @@ assert(run(function () return a >> b end, {"shr"}) == 10 >> 12)
|
|||||||
assert(run(function () return 10 & b end, {"band"}) == 10 & 12)
|
assert(run(function () return 10 & b end, {"band"}) == 10 & 12)
|
||||||
assert(run(function () return a | 2 end, {"bor"}) == 10 | 2)
|
assert(run(function () return a | 2 end, {"bor"}) == 10 | 2)
|
||||||
assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2)
|
assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2)
|
||||||
|
assert(run(function () return a >> 2 end, {"shr"}) == 10 >> 2)
|
||||||
|
assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10)
|
||||||
|
assert(run(function () return a << 2 end, {"shl"}) == 10 << 2)
|
||||||
|
assert(run(function () return 1 << a end, {"shl"}) == 1 << 10)
|
||||||
|
assert(run(function () return 2 ~ a end, {"bxor"}) == 2 ~ 10)
|
||||||
|
|
||||||
|
|
||||||
assert(run(function () return a..b end, {"concat"}) == "1012")
|
assert(run(function () return a..b end, {"concat"}) == "1012")
|
||||||
|
|
||||||
@@ -733,7 +918,7 @@ assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end,
|
|||||||
{"concat", "concat", "concat"}) == "ab10chello12x")
|
{"concat", "concat", "concat"}) == "ab10chello12x")
|
||||||
|
|
||||||
|
|
||||||
do -- a few more tests for comparsion operators
|
do -- a few more tests for comparison operators
|
||||||
local mt1 = {
|
local mt1 = {
|
||||||
__le = function (a,b)
|
__le = function (a,b)
|
||||||
coroutine.yield(10)
|
coroutine.yield(10)
|
||||||
@@ -780,7 +965,7 @@ assert(run(function ()
|
|||||||
do local _ENV = _ENV
|
do local _ENV = _ENV
|
||||||
f = function () AAA = BBB + 1; return AAA end
|
f = function () AAA = BBB + 1; return AAA end
|
||||||
end
|
end
|
||||||
g = new(10); g.k.BBB = 10;
|
local g = new(10); g.k.BBB = 10;
|
||||||
debug.setupvalue(f, 1, g)
|
debug.setupvalue(f, 1, g)
|
||||||
assert(run(f, {"idx", "nidx", "idx"}) == 11)
|
assert(run(f, {"idx", "nidx", "idx"}) == 11)
|
||||||
assert(g.k.AAA == 11)
|
assert(g.k.AAA == 11)
|
||||||
@@ -805,7 +990,7 @@ assert(run(function ()
|
|||||||
-- tests for coroutine API
|
-- tests for coroutine API
|
||||||
if T==nil then
|
if T==nil then
|
||||||
(Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n')
|
(Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n')
|
||||||
return
|
print "OK"; return
|
||||||
end
|
end
|
||||||
|
|
||||||
print('testing coroutine API')
|
print('testing coroutine API')
|
||||||
@@ -920,6 +1105,8 @@ assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34)
|
|||||||
|
|
||||||
-- testing yields with continuations
|
-- testing yields with continuations
|
||||||
|
|
||||||
|
local y
|
||||||
|
|
||||||
co = coroutine.wrap(function (...) return
|
co = coroutine.wrap(function (...) return
|
||||||
T.testC([[ # initial function
|
T.testC([[ # initial function
|
||||||
yieldk 1 2
|
yieldk 1 2
|
||||||
@@ -972,6 +1159,9 @@ assert(x == "YIELD" and y == 4)
|
|||||||
|
|
||||||
assert(not pcall(co)) -- coroutine should be dead
|
assert(not pcall(co)) -- coroutine should be dead
|
||||||
|
|
||||||
|
_G.ctx = nil
|
||||||
|
_G.status = nil
|
||||||
|
|
||||||
|
|
||||||
-- bug in nCcalls
|
-- bug in nCcalls
|
||||||
local co = coroutine.wrap(function ()
|
local co = coroutine.wrap(function ()
|
||||||
|
|||||||
+149
-34
@@ -1,47 +1,33 @@
|
|||||||
-- $Id: testes/cstack.lua $
|
-- $Id: testes/cstack.lua $
|
||||||
-- See Copyright Notice in file all.lua
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print"testing C-stack overflow detection"
|
|
||||||
|
local tracegc = require"tracegc"
|
||||||
|
|
||||||
|
print"testing stack overflow detection"
|
||||||
|
|
||||||
-- Segmentation faults in these tests probably result from a C-stack
|
-- Segmentation faults in these tests probably result from a C-stack
|
||||||
-- overflow. To avoid these errors, recompile Lua with a smaller
|
-- overflow. To avoid these errors, you should set a smaller limit for
|
||||||
-- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger
|
-- the use of C stack by Lua, by changing the constant 'LUAI_MAXCCALLS'.
|
||||||
-- stack for the program.
|
-- Alternatively, you can ensure a larger stack for the program.
|
||||||
|
|
||||||
|
|
||||||
local function checkerror (msg, f, ...)
|
local function checkerror (msg, f, ...)
|
||||||
local s, err = pcall(f, ...)
|
local s, err = pcall(f, ...)
|
||||||
assert(not s and string.find(err, msg))
|
assert(not s and string.find(err, msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
local count
|
|
||||||
local back = string.rep("\b", 8)
|
|
||||||
local function progress ()
|
|
||||||
count = count + 1
|
|
||||||
local n = string.format("%-8d", count)
|
|
||||||
io.stderr:write(back, n)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do print("testing simple recursion:")
|
|
||||||
count = 0
|
|
||||||
local function foo ()
|
|
||||||
progress()
|
|
||||||
foo()
|
|
||||||
end
|
|
||||||
checkerror("stack overflow", foo)
|
|
||||||
print("\tfinal count: ", count)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do print("testing stack overflow in message handling")
|
do print("testing stack overflow in message handling")
|
||||||
count = 0
|
local count = 0
|
||||||
local function loop (x, y, z)
|
local function loop (x, y, z)
|
||||||
progress()
|
count = count + 1
|
||||||
return 1 + loop(x, y, z)
|
return 1 + loop(x, y, z)
|
||||||
end
|
end
|
||||||
|
tracegc.stop() -- __gc should not be called with a full stack
|
||||||
local res, msg = xpcall(loop, loop)
|
local res, msg = xpcall(loop, loop)
|
||||||
|
tracegc.start()
|
||||||
assert(msg == "error in error handling")
|
assert(msg == "error in error handling")
|
||||||
print("\tfinal count: ", count)
|
print("final count: ", count)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -54,29 +40,158 @@ do print("testing recursion inside pattern matching")
|
|||||||
end
|
end
|
||||||
local m = f(80)
|
local m = f(80)
|
||||||
assert(#m == 80)
|
assert(#m == 80)
|
||||||
checkerror("too complex", f, 200000)
|
checkerror("too complex", f, 2000)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
do print("testing stack-overflow in recursive 'gsub'")
|
do print("testing stack-overflow in recursive 'gsub'")
|
||||||
count = 0
|
local count = 0
|
||||||
local function foo ()
|
local function foo ()
|
||||||
progress()
|
count = count + 1
|
||||||
string.gsub("a", ".", foo)
|
string.gsub("a", ".", foo)
|
||||||
end
|
end
|
||||||
checkerror("stack overflow", foo)
|
checkerror("stack overflow", foo)
|
||||||
print("\tfinal count: ", count)
|
print("final count: ", count)
|
||||||
|
|
||||||
print("testing stack-overflow in recursive 'gsub' with metatables")
|
print("testing stack-overflow in recursive 'gsub' with metatables")
|
||||||
count = 0
|
local count = 0
|
||||||
local t = setmetatable({}, {__index = foo})
|
local t = setmetatable({}, {__index = foo})
|
||||||
foo = function ()
|
foo = function ()
|
||||||
count = count + 1
|
count = count + 1
|
||||||
progress(count)
|
|
||||||
string.gsub("a", ".", t)
|
string.gsub("a", ".", t)
|
||||||
end
|
end
|
||||||
checkerror("stack overflow", foo)
|
checkerror("stack overflow", foo)
|
||||||
print("\tfinal count: ", count)
|
print("final count: ", count)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- bug in 5.4.0
|
||||||
|
print("testing limits in coroutines inside deep calls")
|
||||||
|
local count = 0
|
||||||
|
local lim = 1000
|
||||||
|
local function stack (n)
|
||||||
|
if n > 0 then return stack(n - 1) + 1
|
||||||
|
else coroutine.wrap(function ()
|
||||||
|
count = count + 1
|
||||||
|
stack(lim)
|
||||||
|
end)()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local st, msg = xpcall(stack, function () return "ok" end, lim)
|
||||||
|
assert(not st and msg == "ok")
|
||||||
|
print("final count: ", count)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- bug since 5.4.0
|
||||||
|
local count = 0
|
||||||
|
print("chain of 'coroutine.close'")
|
||||||
|
-- create N coroutines forming a list so that each one, when closed,
|
||||||
|
-- closes the previous one. (With a large enough N, previous Lua
|
||||||
|
-- versions crash in this test.)
|
||||||
|
local coro = false
|
||||||
|
for i = 1, 1000 do
|
||||||
|
local previous = coro
|
||||||
|
coro = coroutine.create(function()
|
||||||
|
local cc <close> = setmetatable({}, {__close=function()
|
||||||
|
count = count + 1
|
||||||
|
if previous then
|
||||||
|
assert(coroutine.close(previous))
|
||||||
|
end
|
||||||
|
end})
|
||||||
|
coroutine.yield() -- leaves 'cc' pending to be closed
|
||||||
|
end)
|
||||||
|
assert(coroutine.resume(coro)) -- start it and run until it yields
|
||||||
|
end
|
||||||
|
local st, msg = coroutine.close(coro)
|
||||||
|
assert(not st and string.find(msg, "C stack overflow"))
|
||||||
|
print("final count: ", count)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do
|
||||||
|
print("nesting of resuming yielded coroutines")
|
||||||
|
local count = 0
|
||||||
|
|
||||||
|
local function body ()
|
||||||
|
coroutine.yield()
|
||||||
|
local f = coroutine.wrap(body)
|
||||||
|
f(); -- start new coroutine (will stop in previous yield)
|
||||||
|
count = count + 1
|
||||||
|
f() -- call it recursively
|
||||||
|
end
|
||||||
|
|
||||||
|
local f = coroutine.wrap(body)
|
||||||
|
f()
|
||||||
|
assert(not pcall(f))
|
||||||
|
print("final count: ", count)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- bug in 5.4.2
|
||||||
|
print("nesting coroutines running after recoverable errors")
|
||||||
|
local count = 0
|
||||||
|
local function foo()
|
||||||
|
count = count + 1
|
||||||
|
pcall(1) -- create an error
|
||||||
|
-- running now inside 'precover' ("protected recover")
|
||||||
|
coroutine.wrap(foo)() -- call another coroutine
|
||||||
|
end
|
||||||
|
checkerror("C stack overflow", foo)
|
||||||
|
print("final count: ", count)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
if T then
|
||||||
|
print("testing stack recovery")
|
||||||
|
local N = 0 -- trace number of calls
|
||||||
|
local LIM = -1 -- will store N just before stack overflow
|
||||||
|
|
||||||
|
-- trace stack size; after stack overflow, it should be
|
||||||
|
-- the maximum allowed stack size.
|
||||||
|
local stack1
|
||||||
|
local dummy
|
||||||
|
|
||||||
|
local function err(msg)
|
||||||
|
assert(string.find(msg, "stack overflow"))
|
||||||
|
local _, stacknow = T.stacklevel()
|
||||||
|
assert(stacknow == stack1 + 200)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- When LIM==-1, the 'if' is not executed, so this function only
|
||||||
|
-- counts and stores the stack limits up to overflow. Then, LIM
|
||||||
|
-- becomes N, and then the 'if' code is run when the stack is
|
||||||
|
-- full. Then, there is a stack overflow inside 'xpcall', after which
|
||||||
|
-- the stack must have been restored back to its maximum normal size.
|
||||||
|
local function f()
|
||||||
|
dummy, stack1 = T.stacklevel()
|
||||||
|
if N == LIM then
|
||||||
|
xpcall(f, err)
|
||||||
|
local _, stacknow = T.stacklevel()
|
||||||
|
assert(stacknow == stack1)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
N = N + 1
|
||||||
|
f()
|
||||||
|
end
|
||||||
|
|
||||||
|
local topB, sizeB -- top and size Before overflow
|
||||||
|
local topA, sizeA -- top and size After overflow
|
||||||
|
topB, sizeB = T.stacklevel()
|
||||||
|
tracegc.stop() -- __gc should not be called with a full stack
|
||||||
|
xpcall(f, err)
|
||||||
|
tracegc.start()
|
||||||
|
topA, sizeA = T.stacklevel()
|
||||||
|
-- sizes should be comparable
|
||||||
|
assert(topA == topB and sizeA < sizeB * 2)
|
||||||
|
print(string.format("maximum stack size: %d", stack1))
|
||||||
|
LIM = N -- will stop recursion at maximum level
|
||||||
|
N = 0 -- to count again
|
||||||
|
tracegc.stop() -- __gc should not be called with a full stack
|
||||||
|
f()
|
||||||
|
tracegc.start()
|
||||||
|
print"+"
|
||||||
end
|
end
|
||||||
|
|
||||||
print'OK'
|
print'OK'
|
||||||
|
|||||||
+120
-21
@@ -16,7 +16,7 @@ end
|
|||||||
assert(not debug.gethook())
|
assert(not debug.gethook())
|
||||||
|
|
||||||
local testline = 19 -- line where 'test' is defined
|
local testline = 19 -- line where 'test' is defined
|
||||||
function test (s, l, p) -- this must be line 19
|
local function test (s, l, p) -- this must be line 19
|
||||||
collectgarbage() -- avoid gc during trace
|
collectgarbage() -- avoid gc during trace
|
||||||
local function f (event, line)
|
local function f (event, line)
|
||||||
assert(event == 'line')
|
assert(event == 'line')
|
||||||
@@ -31,6 +31,7 @@ end
|
|||||||
|
|
||||||
do
|
do
|
||||||
assert(not pcall(debug.getinfo, print, "X")) -- invalid option
|
assert(not pcall(debug.getinfo, print, "X")) -- invalid option
|
||||||
|
assert(not pcall(debug.getinfo, 0, ">")) -- invalid option
|
||||||
assert(not debug.getinfo(1000)) -- out of range level
|
assert(not debug.getinfo(1000)) -- out of range level
|
||||||
assert(not debug.getinfo(-1)) -- out of range level
|
assert(not debug.getinfo(-1)) -- out of range level
|
||||||
local a = debug.getinfo(print)
|
local a = debug.getinfo(print)
|
||||||
@@ -48,8 +49,17 @@ do
|
|||||||
end
|
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
|
-- test file and string names truncation
|
||||||
a = "function f () end"
|
local a = "function f () end"
|
||||||
local function dostring (s, x) return load(s, x)() end
|
local function dostring (s, x) return load(s, x)() end
|
||||||
dostring(a)
|
dostring(a)
|
||||||
assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
|
assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
|
||||||
@@ -71,7 +81,8 @@ dostring(a, string.format("=%s", string.rep('x', 500)))
|
|||||||
assert(string.find(debug.getinfo(f).short_src, "^x*$"))
|
assert(string.find(debug.getinfo(f).short_src, "^x*$"))
|
||||||
dostring(a, "=")
|
dostring(a, "=")
|
||||||
assert(debug.getinfo(f).short_src == "")
|
assert(debug.getinfo(f).short_src == "")
|
||||||
a = nil; f = nil;
|
_G.a = nil; _G.f = nil;
|
||||||
|
_G[string.rep("p", 400)] = nil
|
||||||
|
|
||||||
|
|
||||||
repeat
|
repeat
|
||||||
@@ -119,6 +130,18 @@ else
|
|||||||
end
|
end
|
||||||
]], {2,3,4,7})
|
]], {2,3,4,7})
|
||||||
|
|
||||||
|
|
||||||
|
test([[
|
||||||
|
local function foo()
|
||||||
|
end
|
||||||
|
foo()
|
||||||
|
A = 1
|
||||||
|
A = 2
|
||||||
|
A = 3
|
||||||
|
]], {2, 3, 2, 4, 5, 6})
|
||||||
|
_G.A = nil
|
||||||
|
|
||||||
|
|
||||||
test([[--
|
test([[--
|
||||||
if nil then
|
if nil then
|
||||||
a=1
|
a=1
|
||||||
@@ -164,6 +187,8 @@ end
|
|||||||
|
|
||||||
test([[for i=1,4 do a=1 end]], {1,1,1,1})
|
test([[for i=1,4 do a=1 end]], {1,1,1,1})
|
||||||
|
|
||||||
|
_G.a = nil
|
||||||
|
|
||||||
|
|
||||||
do -- testing line info/trace with large gaps in source
|
do -- testing line info/trace with large gaps in source
|
||||||
|
|
||||||
@@ -183,6 +208,50 @@ do -- testing line info/trace with large gaps in source
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
_G.a = nil
|
||||||
|
|
||||||
|
|
||||||
|
do -- testing active lines
|
||||||
|
local function checkactivelines (f, lines)
|
||||||
|
local t = debug.getinfo(f, "SL")
|
||||||
|
for _, l in pairs(lines) do
|
||||||
|
l = l + t.linedefined
|
||||||
|
assert(t.activelines[l])
|
||||||
|
t.activelines[l] = undef
|
||||||
|
end
|
||||||
|
assert(next(t.activelines) == nil) -- no extra lines
|
||||||
|
end
|
||||||
|
|
||||||
|
checkactivelines(function (...) -- vararg function
|
||||||
|
-- 1st line is empty
|
||||||
|
-- 2nd line is empty
|
||||||
|
-- 3th line is empty
|
||||||
|
local a = 20
|
||||||
|
-- 5th line is empty
|
||||||
|
local b = 30
|
||||||
|
-- 7th line is empty
|
||||||
|
end, {4, 6, 8})
|
||||||
|
|
||||||
|
checkactivelines(function (a)
|
||||||
|
-- 1st line is empty
|
||||||
|
-- 2nd line is empty
|
||||||
|
local a = 20
|
||||||
|
local b = 30
|
||||||
|
-- 5th line is empty
|
||||||
|
end, {3, 4, 6})
|
||||||
|
|
||||||
|
checkactivelines(function (a, b, ...) end, {0})
|
||||||
|
|
||||||
|
checkactivelines(function (a, b)
|
||||||
|
end, {1})
|
||||||
|
|
||||||
|
for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do
|
||||||
|
checkactivelines(
|
||||||
|
load(string.format("%s return 1", string.rep("\n", n))),
|
||||||
|
{n + 1})
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
print'+'
|
print'+'
|
||||||
|
|
||||||
@@ -233,7 +302,6 @@ foo(200, 3, 4)
|
|||||||
local a = {}
|
local a = {}
|
||||||
for i = 1, (_soft and 100 or 1000) do a[i] = i end
|
for i = 1, (_soft and 100 or 1000) do a[i] = i end
|
||||||
foo(table.unpack(a))
|
foo(table.unpack(a))
|
||||||
a = nil
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -253,9 +321,14 @@ do -- test hook presence in debug info
|
|||||||
debug.sethook()
|
debug.sethook()
|
||||||
assert(count == 4)
|
assert(count == 4)
|
||||||
end
|
end
|
||||||
|
_ENV.a = nil
|
||||||
|
|
||||||
|
|
||||||
a = {}; L = nil
|
-- hook table has weak keys
|
||||||
|
assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
|
||||||
|
|
||||||
|
|
||||||
|
a = {}; local L = nil
|
||||||
local glob = 1
|
local glob = 1
|
||||||
local oldglob = glob
|
local oldglob = glob
|
||||||
debug.sethook(function (e,l)
|
debug.sethook(function (e,l)
|
||||||
@@ -281,7 +354,7 @@ function f(a,b)
|
|||||||
local _, y = debug.getlocal(1, 2)
|
local _, y = debug.getlocal(1, 2)
|
||||||
assert(x == a and y == b)
|
assert(x == a and y == b)
|
||||||
assert(debug.setlocal(2, 3, "pera") == "AA".."AA")
|
assert(debug.setlocal(2, 3, "pera") == "AA".."AA")
|
||||||
assert(debug.setlocal(2, 4, "maçã") == "B")
|
assert(debug.setlocal(2, 4, "manga") == "B")
|
||||||
x = debug.getinfo(2)
|
x = debug.getinfo(2)
|
||||||
assert(x.func == g and x.what == "Lua" and x.name == 'g' and
|
assert(x.func == g and x.what == "Lua" and x.name == 'g' and
|
||||||
x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
|
x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
|
||||||
@@ -296,7 +369,7 @@ function foo()
|
|||||||
end; foo() -- set L
|
end; foo() -- set L
|
||||||
-- check line counting inside strings and empty lines
|
-- check line counting inside strings and empty lines
|
||||||
|
|
||||||
_ = 'alo\
|
local _ = 'alo\
|
||||||
alo' .. [[
|
alo' .. [[
|
||||||
|
|
||||||
]]
|
]]
|
||||||
@@ -309,9 +382,9 @@ function g (...)
|
|||||||
local arg = {...}
|
local arg = {...}
|
||||||
do local a,b,c; a=math.sin(40); end
|
do local a,b,c; a=math.sin(40); end
|
||||||
local feijao
|
local feijao
|
||||||
local AAAA,B = "xuxu", "mamão"
|
local AAAA,B = "xuxu", "abacate"
|
||||||
f(AAAA,B)
|
f(AAAA,B)
|
||||||
assert(AAAA == "pera" and B == "maçã")
|
assert(AAAA == "pera" and B == "manga")
|
||||||
do
|
do
|
||||||
local B = 13
|
local B = 13
|
||||||
local x,y = debug.getlocal(1,5)
|
local x,y = debug.getlocal(1,5)
|
||||||
@@ -345,14 +418,15 @@ function g(a,b) return (a+1) + f() end
|
|||||||
|
|
||||||
assert(g(0,0) == 30)
|
assert(g(0,0) == 30)
|
||||||
|
|
||||||
|
_G.f, _G.g = nil
|
||||||
|
|
||||||
debug.sethook(nil);
|
debug.sethook(nil);
|
||||||
assert(debug.gethook() == nil)
|
assert(not debug.gethook())
|
||||||
|
|
||||||
|
|
||||||
-- minimal tests for setuservalue/getuservalue
|
-- minimal tests for setuservalue/getuservalue
|
||||||
do
|
do
|
||||||
assert(debug.setuservalue(io.stdin, 10) == nil)
|
assert(not debug.setuservalue(io.stdin, 10))
|
||||||
local a, b = debug.getuservalue(io.stdin, 10)
|
local a, b = debug.getuservalue(io.stdin, 10)
|
||||||
assert(a == nil and not b)
|
assert(a == nil and not b)
|
||||||
end
|
end
|
||||||
@@ -388,7 +462,7 @@ local function collectlocals (level)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
X = nil
|
local X = nil
|
||||||
a = {}
|
a = {}
|
||||||
function a:f (a, b, ...) local arg = {...}; local c = 13 end
|
function a:f (a, b, ...) local arg = {...}; local c = 13 end
|
||||||
debug.sethook(function (e)
|
debug.sethook(function (e)
|
||||||
@@ -410,7 +484,8 @@ end, "c")
|
|||||||
a:f(1,2,3,4,5)
|
a:f(1,2,3,4,5)
|
||||||
assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
|
assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
|
||||||
assert(XX == 12)
|
assert(XX == 12)
|
||||||
assert(debug.gethook() == nil)
|
assert(not debug.gethook())
|
||||||
|
_G.XX = nil
|
||||||
|
|
||||||
|
|
||||||
-- testing access to local variables in return hook (bug in 5.2)
|
-- testing access to local variables in return hook (bug in 5.2)
|
||||||
@@ -535,6 +610,7 @@ end
|
|||||||
|
|
||||||
debug.sethook()
|
debug.sethook()
|
||||||
|
|
||||||
|
local g, g1
|
||||||
|
|
||||||
-- tests for tail calls
|
-- tests for tail calls
|
||||||
local function f (x)
|
local function f (x)
|
||||||
@@ -580,7 +656,7 @@ h(false)
|
|||||||
debug.sethook()
|
debug.sethook()
|
||||||
assert(b == 2) -- two tail calls
|
assert(b == 2) -- two tail calls
|
||||||
|
|
||||||
lim = _soft and 3000 or 30000
|
local lim = _soft and 3000 or 30000
|
||||||
local function foo (x)
|
local function foo (x)
|
||||||
if x==0 then
|
if x==0 then
|
||||||
assert(debug.getinfo(2).what == "main")
|
assert(debug.getinfo(2).what == "main")
|
||||||
@@ -645,6 +721,11 @@ t = debug.getinfo(1) -- main
|
|||||||
assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
|
assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
|
||||||
debug.getupvalue(t.func, 1) == "_ENV")
|
debug.getupvalue(t.func, 1) == "_ENV")
|
||||||
|
|
||||||
|
t = debug.getinfo(math.sin) -- C function
|
||||||
|
assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
|
||||||
|
|
||||||
|
t = debug.getinfo(string.gmatch("abc", "a")) -- C closure
|
||||||
|
assert(t.isvararg == true and t.nparams == 0 and t.nups > 0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -802,11 +883,12 @@ assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and
|
|||||||
-a == "unm" and #a == "len" and a&3 == "band")
|
-a == "unm" and #a == "len" and a&3 == "band")
|
||||||
assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
|
assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
|
||||||
-a == "unm" and #a == "len" and a & 3 == "band")
|
-a == "unm" and #a == "len" and a & 3 == "band")
|
||||||
assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and
|
assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr")
|
||||||
a>>1 == "shift")
|
|
||||||
assert (a==b and a.op == "eq")
|
assert (a==b and a.op == "eq")
|
||||||
assert (a>=b and a.op == "order")
|
assert (a>=b and a.op == "le")
|
||||||
assert (a>b and a.op == "order")
|
assert ("x">=a and a.op == "le")
|
||||||
|
assert (a>b and a.op == "lt")
|
||||||
|
assert (a>10 and a.op == "lt")
|
||||||
assert(~a == "bnot")
|
assert(~a == "bnot")
|
||||||
|
|
||||||
do -- testing for-iterator name
|
do -- testing for-iterator name
|
||||||
@@ -823,7 +905,7 @@ do -- testing debug info for finalizers
|
|||||||
|
|
||||||
-- create a piece of garbage with a finalizer
|
-- create a piece of garbage with a finalizer
|
||||||
setmetatable({}, {__gc = function ()
|
setmetatable({}, {__gc = function ()
|
||||||
local t = debug.getinfo(2) -- get callee information
|
local t = debug.getinfo(1) -- get function information
|
||||||
assert(t.namewhat == "metamethod")
|
assert(t.namewhat == "metamethod")
|
||||||
name = t.name
|
name = t.name
|
||||||
end})
|
end})
|
||||||
@@ -855,7 +937,7 @@ do
|
|||||||
local cl = countlines(rest)
|
local cl = countlines(rest)
|
||||||
-- at most 10 lines in first part, 11 in second, plus '...'
|
-- at most 10 lines in first part, 11 in second, plus '...'
|
||||||
assert(cl <= 10 + 11 + 1)
|
assert(cl <= 10 + 11 + 1)
|
||||||
local brk = string.find(rest, "%.%.%.")
|
local brk = string.find(rest, "%.%.%.\t%(skip")
|
||||||
if brk then -- does message have '...'?
|
if brk then -- does message have '...'?
|
||||||
local rest1 = string.sub(rest, 1, brk)
|
local rest1 = string.sub(rest, 1, brk)
|
||||||
local rest2 = string.sub(rest, brk, #rest)
|
local rest2 = string.sub(rest, brk, #rest)
|
||||||
@@ -876,7 +958,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
print("testing debug functions on chunk without debug info")
|
print("testing debug functions on chunk without debug info")
|
||||||
prog = [[-- program to be loaded without debug information
|
local prog = [[-- program to be loaded without debug information (strip)
|
||||||
local debug = require'debug'
|
local debug = require'debug'
|
||||||
local a = 12 -- a local variable
|
local a = 12 -- a local variable
|
||||||
|
|
||||||
@@ -919,6 +1001,23 @@ local f = assert(load(string.dump(load(prog), true)))
|
|||||||
|
|
||||||
assert(f() == 13)
|
assert(f() == 13)
|
||||||
|
|
||||||
|
do -- bug in 5.4.0: line hooks in stripped code
|
||||||
|
local function foo ()
|
||||||
|
local a = 1
|
||||||
|
local b = 2
|
||||||
|
return b
|
||||||
|
end
|
||||||
|
|
||||||
|
local s = load(string.dump(foo, true))
|
||||||
|
local line = true
|
||||||
|
debug.sethook(function (e, l)
|
||||||
|
assert(e == "line")
|
||||||
|
line = l
|
||||||
|
end, "l")
|
||||||
|
assert(s() == 2); debug.sethook(nil)
|
||||||
|
assert(line == nil) -- hook called withoug debug info for 1st instruction
|
||||||
|
end
|
||||||
|
|
||||||
do -- tests for 'source' in binary dumps
|
do -- tests for 'source' in binary dumps
|
||||||
local prog = [[
|
local prog = [[
|
||||||
return function (x)
|
return function (x)
|
||||||
|
|||||||
+176
-61
@@ -18,14 +18,15 @@ end
|
|||||||
|
|
||||||
local function doit (s)
|
local function doit (s)
|
||||||
local f, msg = load(s)
|
local f, msg = load(s)
|
||||||
if f == nil then return msg end
|
if not f then return msg end
|
||||||
local cond, msg = pcall(f)
|
local cond, msg = pcall(f)
|
||||||
return (not cond) and msg
|
return (not cond) and msg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function checkmessage (prog, msg)
|
local function checkmessage (prog, msg, debug)
|
||||||
local m = doit(prog)
|
local m = doit(prog)
|
||||||
|
if debug then print(m, msg) end
|
||||||
assert(string.find(m, msg, 1, true))
|
assert(string.find(m, msg, 1, true))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -67,9 +68,30 @@ checksyntax([[
|
|||||||
]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
|
]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
|
||||||
|
|
||||||
|
|
||||||
|
do -- testing errors in goto/break
|
||||||
|
local function checksyntax (prog, msg, line)
|
||||||
|
local st, err = load(prog)
|
||||||
|
assert(string.find(err, "line " .. line))
|
||||||
|
assert(string.find(err, msg, 1, true))
|
||||||
|
end
|
||||||
|
|
||||||
|
checksyntax([[
|
||||||
|
::A:: a = 1
|
||||||
|
::A::
|
||||||
|
]], "label 'A' already defined", 1)
|
||||||
|
|
||||||
|
checksyntax([[
|
||||||
|
a = 1
|
||||||
|
goto A
|
||||||
|
do ::A:: end
|
||||||
|
]], "no visible label 'A'", 2)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
if not T then
|
if not T then
|
||||||
(Message or print)
|
(Message or print)
|
||||||
('\n >>> testC not active: skipping memory message test <<<\n')
|
('\n >>> testC not active: skipping tests for messages in C <<<\n')
|
||||||
else
|
else
|
||||||
print "testing memory error message"
|
print "testing memory error message"
|
||||||
local a = {}
|
local a = {}
|
||||||
@@ -82,6 +104,19 @@ else
|
|||||||
end)
|
end)
|
||||||
T.totalmem(0)
|
T.totalmem(0)
|
||||||
assert(not st and msg == "not enough" .. " memory")
|
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"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -92,21 +127,42 @@ checkmessage("a = {} | 1", "bitwise operation")
|
|||||||
checkmessage("a = {} < 1", "attempt to compare")
|
checkmessage("a = {} < 1", "attempt to compare")
|
||||||
checkmessage("a = {} <= 1", "attempt to compare")
|
checkmessage("a = {} <= 1", "attempt to compare")
|
||||||
|
|
||||||
checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'")
|
checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'")
|
||||||
checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'")
|
checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'")
|
||||||
checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
|
checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
|
||||||
assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'"))
|
assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
|
||||||
checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number")
|
checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
|
||||||
checkmessage("a=(1)..{}", "a table value")
|
checkmessage("aaa=(1)..{}", "a table value")
|
||||||
|
|
||||||
|
-- bug in 5.4.6
|
||||||
|
checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'")
|
||||||
|
|
||||||
|
-- a similar bug in 5.4.7, since 5.4.0
|
||||||
|
checkmessage("print(('_ENV').x + 1)", "field 'x'")
|
||||||
|
|
||||||
|
|
||||||
|
_G.aaa, _G.bbbb = nil
|
||||||
|
|
||||||
|
-- calls
|
||||||
|
checkmessage("local a; a(13)", "local 'a'")
|
||||||
|
checkmessage([[
|
||||||
|
local a = setmetatable({}, {__add = 34})
|
||||||
|
a = a + 1
|
||||||
|
]], "metamethod 'add'")
|
||||||
|
checkmessage([[
|
||||||
|
local a = setmetatable({}, {__lt = {}})
|
||||||
|
a = a > a
|
||||||
|
]], "metamethod 'lt'")
|
||||||
|
|
||||||
-- tail calls
|
-- tail calls
|
||||||
checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
|
checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
|
||||||
checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'")
|
checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'")
|
||||||
|
|
||||||
checkmessage("a = #print", "length of a function value")
|
checkmessage("aaa = #print", "length of a function value")
|
||||||
checkmessage("a = #3", "length of a number value")
|
checkmessage("aaa = #3", "length of a number value")
|
||||||
|
|
||||||
|
_G.aaa = nil
|
||||||
|
|
||||||
aaa = nil
|
|
||||||
checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
|
checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
|
||||||
checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
|
checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
|
||||||
checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
|
checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
|
||||||
@@ -119,15 +175,16 @@ checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
|
|||||||
|
|
||||||
checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
|
checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
|
||||||
|
|
||||||
checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'")
|
checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'")
|
||||||
checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
|
checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
|
||||||
checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'")
|
checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'")
|
||||||
checkmessage("aaa={}; x=-aaa", "global 'aaa'")
|
checkmessage("aaa={}; x=-aaa", "global 'aaa'")
|
||||||
|
|
||||||
-- short circuit
|
-- short circuit
|
||||||
checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)",
|
checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)",
|
||||||
"local 'bbbb'")
|
"local 'bbbb'")
|
||||||
checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'")
|
checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)",
|
||||||
|
"local 'bbbb'")
|
||||||
checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
|
checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
|
||||||
checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
|
checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
|
||||||
assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
|
assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
|
||||||
@@ -154,8 +211,15 @@ checkmessage("return ~-3e40", "has no integer representation")
|
|||||||
checkmessage("return ~-3.009", "has no integer representation")
|
checkmessage("return ~-3.009", "has no integer representation")
|
||||||
checkmessage("return 3.009 & 1", "has no integer representation")
|
checkmessage("return 3.009 & 1", "has no integer representation")
|
||||||
checkmessage("return 34 >> {}", "table value")
|
checkmessage("return 34 >> {}", "table value")
|
||||||
checkmessage("a = 24 // 0", "divide by zero")
|
checkmessage("aaa = 24 // 0", "divide by zero")
|
||||||
checkmessage("a = 1 % 0", "'n%0'")
|
checkmessage("aaa = 1 % 0", "'n%0'")
|
||||||
|
|
||||||
|
|
||||||
|
-- type error for an object which is neither in an upvalue nor a register.
|
||||||
|
-- The following code will try to index the value 10 that is stored in
|
||||||
|
-- the metatable, without moving it to a register.
|
||||||
|
checkmessage("local a = setmetatable({}, {__index = 10}).x",
|
||||||
|
"attempt to index a number value")
|
||||||
|
|
||||||
|
|
||||||
-- numeric for loops
|
-- numeric for loops
|
||||||
@@ -188,6 +252,22 @@ do -- named objects (field '__name')
|
|||||||
checkmessage("return {} < XX", "table with My Type")
|
checkmessage("return {} < XX", "table with My Type")
|
||||||
checkmessage("return XX < io.stdin", "My Type with FILE*")
|
checkmessage("return XX < io.stdin", "My Type with FILE*")
|
||||||
_G.XX = nil
|
_G.XX = nil
|
||||||
|
|
||||||
|
if T then -- extra tests for 'luaL_tolstring'
|
||||||
|
-- bug in 5.4.3; 'luaL_tolstring' with negative indices
|
||||||
|
local x = setmetatable({}, {__name="TABLE"})
|
||||||
|
assert(T.testC("Ltolstring -1; return 1", x) == tostring(x))
|
||||||
|
|
||||||
|
local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x)
|
||||||
|
assert(a == 10 and b == tostring(x))
|
||||||
|
|
||||||
|
setmetatable(x, {__tostring=function (o)
|
||||||
|
assert(o == x)
|
||||||
|
return "ABC"
|
||||||
|
end})
|
||||||
|
local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x)
|
||||||
|
assert(a == x and b == 10 and c == "ABC")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- global functions
|
-- global functions
|
||||||
@@ -213,13 +293,13 @@ end
|
|||||||
-- tests for field accesses after RK limit
|
-- tests for field accesses after RK limit
|
||||||
local t = {}
|
local t = {}
|
||||||
for i = 1, 1000 do
|
for i = 1, 1000 do
|
||||||
t[i] = "a = x" .. i
|
t[i] = "aaa = x" .. i
|
||||||
end
|
end
|
||||||
local s = table.concat(t, "; ")
|
local s = table.concat(t, "; ")
|
||||||
t = nil
|
t = nil
|
||||||
checkmessage(s.."; a = bbb + 1", "global 'bbb'")
|
checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
|
||||||
checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'")
|
checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
|
||||||
checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'")
|
checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
|
||||||
checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
|
checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
|
||||||
|
|
||||||
checkmessage([[aaa=9
|
checkmessage([[aaa=9
|
||||||
@@ -249,7 +329,7 @@ end]], "global 'insert'")
|
|||||||
|
|
||||||
checkmessage([[ -- tail call
|
checkmessage([[ -- tail call
|
||||||
return math.sin("a")
|
return math.sin("a")
|
||||||
]], "'sin'")
|
]], "sin")
|
||||||
|
|
||||||
checkmessage([[collectgarbage("nooption")]], "invalid option")
|
checkmessage([[collectgarbage("nooption")]], "invalid option")
|
||||||
|
|
||||||
@@ -268,14 +348,17 @@ main()
|
|||||||
]], "global 'NoSuchName'")
|
]], "global 'NoSuchName'")
|
||||||
print'+'
|
print'+'
|
||||||
|
|
||||||
a = {}; setmetatable(a, {__index = string})
|
aaa = {}; setmetatable(aaa, {__index = string})
|
||||||
checkmessage("a:sub()", "bad self")
|
checkmessage("aaa:sub()", "bad self")
|
||||||
checkmessage("string.sub('a', {})", "#2")
|
checkmessage("string.sub('a', {})", "#2")
|
||||||
checkmessage("('a'):sub{}", "#1")
|
checkmessage("('a'):sub{}", "#1")
|
||||||
|
|
||||||
checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
|
checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
|
||||||
checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
|
checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
|
||||||
|
|
||||||
|
_G.aaa = nil
|
||||||
|
|
||||||
|
|
||||||
-- tests for errors in coroutines
|
-- tests for errors in coroutines
|
||||||
|
|
||||||
local function f (n)
|
local function f (n)
|
||||||
@@ -293,7 +376,7 @@ checkerr("yield across", f)
|
|||||||
|
|
||||||
-- testing size of 'source' info; size of buffer for that info is
|
-- testing size of 'source' info; size of buffer for that info is
|
||||||
-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
|
-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
|
||||||
idsize = 60 - 1
|
local idsize = 60 - 1
|
||||||
local function checksize (source)
|
local function checksize (source)
|
||||||
-- syntax error
|
-- syntax error
|
||||||
local _, msg = load("x", source)
|
local _, msg = load("x", source)
|
||||||
@@ -312,8 +395,8 @@ end
|
|||||||
|
|
||||||
local function lineerror (s, l)
|
local function lineerror (s, l)
|
||||||
local err,msg = pcall(load(s))
|
local err,msg = pcall(load(s))
|
||||||
local line = string.match(msg, ":(%d+):")
|
local line = tonumber(string.match(msg, ":(%d+):"))
|
||||||
assert(tonumber(line) == l)
|
assert(line == l or (not line and not l))
|
||||||
end
|
end
|
||||||
|
|
||||||
lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
|
lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
|
||||||
@@ -329,19 +412,19 @@ lineerror("a\n=\n-\n\nprint\n;", 3)
|
|||||||
|
|
||||||
lineerror([[
|
lineerror([[
|
||||||
a
|
a
|
||||||
(
|
( -- <<
|
||||||
23)
|
23)
|
||||||
]], 1)
|
]], 2)
|
||||||
|
|
||||||
lineerror([[
|
lineerror([[
|
||||||
local a = {x = 13}
|
local a = {x = 13}
|
||||||
a
|
a
|
||||||
.
|
.
|
||||||
x
|
x
|
||||||
(
|
( -- <<
|
||||||
23
|
23
|
||||||
)
|
)
|
||||||
]], 2)
|
]], 5)
|
||||||
|
|
||||||
lineerror([[
|
lineerror([[
|
||||||
local a = {x = 13}
|
local a = {x = 13}
|
||||||
@@ -355,13 +438,14 @@ x
|
|||||||
|
|
||||||
local p = [[
|
local p = [[
|
||||||
function g() f() end
|
function g() f() end
|
||||||
function f(x) error('a', X) end
|
function f(x) error('a', XX) end
|
||||||
g()
|
g()
|
||||||
]]
|
]]
|
||||||
X=3;lineerror((p), 3)
|
XX=3;lineerror((p), 3)
|
||||||
X=0;lineerror((p), nil)
|
XX=0;lineerror((p), false)
|
||||||
X=1;lineerror((p), 2)
|
XX=1;lineerror((p), 2)
|
||||||
X=2;lineerror((p), 1)
|
XX=2;lineerror((p), 1)
|
||||||
|
_G.XX, _G.g, _G.f = nil
|
||||||
|
|
||||||
|
|
||||||
lineerror([[
|
lineerror([[
|
||||||
@@ -381,30 +465,56 @@ if not b then
|
|||||||
end]], 5)
|
end]], 5)
|
||||||
|
|
||||||
|
|
||||||
|
-- bug in 5.4.0
|
||||||
|
lineerror([[
|
||||||
|
local a = 0
|
||||||
|
local b = 1
|
||||||
|
local c = b % a
|
||||||
|
]], 3)
|
||||||
|
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
if not _soft then
|
if not _soft then
|
||||||
-- several tests that exaust the Lua stack
|
-- several tests that exaust the Lua stack
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
print"testing stack overflow"
|
print"testing stack overflow"
|
||||||
C = 0
|
local C = 0
|
||||||
local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end
|
-- get line where stack overflow will happen
|
||||||
|
local l = debug.getinfo(1, "l").currentline + 1
|
||||||
|
local function auxy () C=C+1; auxy() end -- produce a stack overflow
|
||||||
|
function YY ()
|
||||||
|
collectgarbage("stop") -- avoid running finalizers without stack space
|
||||||
|
auxy()
|
||||||
|
collectgarbage("restart")
|
||||||
|
end
|
||||||
|
|
||||||
local function checkstackmessage (m)
|
local function checkstackmessage (m)
|
||||||
|
print("(expected stack overflow after " .. C .. " calls)")
|
||||||
|
C = 0 -- prepare next count
|
||||||
return (string.find(m, "stack overflow"))
|
return (string.find(m, "stack overflow"))
|
||||||
end
|
end
|
||||||
-- repeated stack overflows (to check stack recovery)
|
-- repeated stack overflows (to check stack recovery)
|
||||||
assert(checkstackmessage(doit('y()')))
|
assert(checkstackmessage(doit('YY()')))
|
||||||
print('+')
|
assert(checkstackmessage(doit('YY()')))
|
||||||
assert(checkstackmessage(doit('y()')))
|
assert(checkstackmessage(doit('YY()')))
|
||||||
print('+')
|
|
||||||
assert(checkstackmessage(doit('y()')))
|
_G.YY = nil
|
||||||
print('+')
|
|
||||||
|
|
||||||
|
|
||||||
-- error lines in stack overflow
|
-- error lines in stack overflow
|
||||||
C = 0
|
|
||||||
local l1
|
local l1
|
||||||
local function g(x)
|
local function g(x)
|
||||||
l1 = debug.getinfo(x, "l").currentline; y()
|
l1 = debug.getinfo(x, "l").currentline + 2
|
||||||
|
collectgarbage("stop") -- avoid running finalizers without stack space
|
||||||
|
auxy()
|
||||||
|
collectgarbage("restart")
|
||||||
end
|
end
|
||||||
local _, stackmsg = xpcall(g, debug.traceback, 1)
|
local _, stackmsg = xpcall(g, debug.traceback, 1)
|
||||||
print('+')
|
print('+')
|
||||||
@@ -489,7 +599,7 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- xpcall with arguments
|
-- xpcall with arguments
|
||||||
a, b, c = xpcall(string.find, error, "alo", "al")
|
local a, b, c = xpcall(string.find, error, "alo", "al")
|
||||||
assert(a and b == 1 and c == 2)
|
assert(a and b == 1 and c == 2)
|
||||||
a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
|
a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
|
||||||
assert(not a and type(b) == "table" and c == nil)
|
assert(not a and type(b) == "table" and c == nil)
|
||||||
@@ -509,11 +619,12 @@ checksyntax("a\1a = 1", "", "<\\1>", 1)
|
|||||||
-- test 255 as first char in a chunk
|
-- test 255 as first char in a chunk
|
||||||
checksyntax("\255a = 1", "", "<\\255>", 1)
|
checksyntax("\255a = 1", "", "<\\255>", 1)
|
||||||
|
|
||||||
doit('I = load("a=9+"); a=3')
|
doit('I = load("a=9+"); aaa=3')
|
||||||
assert(a==3 and I == nil)
|
assert(_G.aaa==3 and not _G.I)
|
||||||
|
_G.I,_G.aaa = nil
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
lim = 1000
|
local lim = 1000
|
||||||
if _soft then lim = 100 end
|
if _soft then lim = 100 end
|
||||||
for i=1,lim do
|
for i=1,lim do
|
||||||
doit('a = ')
|
doit('a = ')
|
||||||
@@ -523,25 +634,29 @@ end
|
|||||||
|
|
||||||
-- testing syntax limits
|
-- testing syntax limits
|
||||||
|
|
||||||
local function testrep (init, rep, close, repc)
|
local function testrep (init, rep, close, repc, finalresult)
|
||||||
local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100)
|
local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100)
|
||||||
assert(load(s)) -- 100 levels is OK
|
local res, msg = load(s)
|
||||||
s = init .. string.rep(rep, 10000)
|
assert(res) -- 100 levels is OK
|
||||||
local res, msg = load(s) -- 10000 levels not ok
|
if (finalresult) then
|
||||||
assert(not res and (string.find(msg, "too many registers") or
|
assert(res() == finalresult)
|
||||||
string.find(msg, "stack overflow")))
|
end
|
||||||
|
s = init .. string.rep(rep, 500)
|
||||||
|
local res, msg = load(s) -- 500 levels not ok
|
||||||
|
assert(not res and (string.find(msg, "too many") or
|
||||||
|
string.find(msg, "overflow")))
|
||||||
end
|
end
|
||||||
|
|
||||||
testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
|
testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
|
||||||
testrep("local a; a=", "{", "0", "}")
|
testrep("local a; a=", "{", "0", "}")
|
||||||
testrep("local a; a=", "(", "2", ")")
|
testrep("return ", "(", "2", ")", 2)
|
||||||
testrep("local a; ", "a(", "2", ")")
|
testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2)
|
||||||
testrep("", "do ", "", " end")
|
testrep("", "do ", "", " end")
|
||||||
testrep("", "while a do ", "", " end")
|
testrep("", "while a do ", "", " end")
|
||||||
testrep("local a; ", "if a then else ", "", " end")
|
testrep("local a; ", "if a then else ", "", " end")
|
||||||
testrep("", "function foo () ", "", " end")
|
testrep("", "function foo () ", "", " end")
|
||||||
testrep("local a; a=", "a..", "a", "")
|
testrep("local a = ''; return ", "a..", "'a'", "", "a")
|
||||||
testrep("local a; a=", "a^", "a", "")
|
testrep("local a = 1; return ", "a^", "a", "", 1)
|
||||||
|
|
||||||
checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
|
checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
|
||||||
|
|
||||||
|
|||||||
+42
-3
@@ -182,7 +182,7 @@ assert(~a == a); checkcap{"bnot", a, a}
|
|||||||
assert(a << 3 == a); checkcap{"shl", a, 3}
|
assert(a << 3 == a); checkcap{"shl", a, 3}
|
||||||
assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a}
|
assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a}
|
||||||
|
|
||||||
-- for comparsion operators, all results are true
|
-- for comparison operators, all results are true
|
||||||
assert(5.0 > a); checkcap{"lt", a, 5.0}
|
assert(5.0 > a); checkcap{"lt", a, 5.0}
|
||||||
assert(a >= 10); checkcap{"le", 10, a}
|
assert(a >= 10); checkcap{"le", 10, a}
|
||||||
assert(a <= -10.0); checkcap{"le", a, -10.0}
|
assert(a <= -10.0); checkcap{"le", a, -10.0}
|
||||||
@@ -217,9 +217,16 @@ t.__le = function (a,b,c)
|
|||||||
return a<=b, "dummy"
|
return a<=b, "dummy"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
t.__eq = function (a,b,c)
|
||||||
|
assert(c == nil)
|
||||||
|
if type(a) == 'table' then a = a.x end
|
||||||
|
if type(b) == 'table' then b = b.x end
|
||||||
|
return a == b, "dummy"
|
||||||
|
end
|
||||||
|
|
||||||
function Op(x) return setmetatable({x=x}, t) end
|
function Op(x) return setmetatable({x=x}, t) end
|
||||||
|
|
||||||
local function test ()
|
local function test (a, b, c)
|
||||||
assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
|
assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
|
||||||
assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
|
assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
|
||||||
assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
|
assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
|
||||||
@@ -232,9 +239,13 @@ local function test ()
|
|||||||
assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
|
assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
|
||||||
assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
|
assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
|
||||||
assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
|
assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
|
||||||
|
assert(Op(1) == Op(1) and Op(1) ~= Op(2))
|
||||||
|
assert(Op('a') == Op('a') and Op('a') ~= Op('b'))
|
||||||
|
assert(a == a and a ~= b)
|
||||||
|
assert(Op(3) == c)
|
||||||
end
|
end
|
||||||
|
|
||||||
test()
|
test(Op(1), Op(2), Op(3))
|
||||||
|
|
||||||
|
|
||||||
-- test `partial order'
|
-- test `partial order'
|
||||||
@@ -294,6 +305,17 @@ t[Set{1,3,5}] = 1
|
|||||||
assert(t[Set{1,3,5}] == undef)
|
assert(t[Set{1,3,5}] == undef)
|
||||||
|
|
||||||
|
|
||||||
|
do -- test invalidating flags
|
||||||
|
local mt = {__eq = true}
|
||||||
|
local a = setmetatable({10}, mt)
|
||||||
|
local b = setmetatable({10}, mt)
|
||||||
|
mt.__eq = nil
|
||||||
|
assert(a ~= b) -- no metamethod
|
||||||
|
mt.__eq = function (x,y) return x[1] == y[1] end
|
||||||
|
assert(a == b) -- must use metamethod now
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
if not T then
|
if not T then
|
||||||
(Message or print)('\n >>> testC not active: skipping tests for \z
|
(Message or print)('\n >>> testC not active: skipping tests for \z
|
||||||
userdata <<<\n')
|
userdata <<<\n')
|
||||||
@@ -314,6 +336,7 @@ else
|
|||||||
assert(u1 == u3 and u3 == u1 and u1 ~= u2)
|
assert(u1 == u3 and u3 == u1 and u1 ~= u2)
|
||||||
assert(u2 == u1 and u2 == u3 and u3 == u2)
|
assert(u2 == u1 and u2 == u3 and u3 == u2)
|
||||||
assert(u2 ~= {}) -- different types cannot be equal
|
assert(u2 ~= {}) -- different types cannot be equal
|
||||||
|
assert(rawequal(u1, u1) and not rawequal(u1, u3))
|
||||||
|
|
||||||
local mirror = {}
|
local mirror = {}
|
||||||
debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
|
debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
|
||||||
@@ -347,6 +370,19 @@ x = 0 .."a".."b"..c..d.."e".."f".."g"
|
|||||||
assert(x.val == "0abcdefg")
|
assert(x.val == "0abcdefg")
|
||||||
|
|
||||||
|
|
||||||
|
do
|
||||||
|
-- bug since 5.4.1
|
||||||
|
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)
|
-- concat metamethod x numbers (bug in 5.1.1)
|
||||||
c = {}
|
c = {}
|
||||||
local x
|
local x
|
||||||
@@ -397,6 +433,9 @@ assert(i == 3 and x[1] == 3 and x[3] == 5)
|
|||||||
|
|
||||||
assert(_G.X == 20)
|
assert(_G.X == 20)
|
||||||
|
|
||||||
|
_G.X, _G.B = nil
|
||||||
|
|
||||||
|
|
||||||
print'+'
|
print'+'
|
||||||
|
|
||||||
local _g = _G
|
local _g = _G
|
||||||
|
|||||||
+111
-44
@@ -92,8 +92,8 @@ assert(io.output():seek("end") == string.len("alo joao"))
|
|||||||
|
|
||||||
assert(io.output():seek("set") == 0)
|
assert(io.output():seek("set") == 0)
|
||||||
|
|
||||||
assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
|
assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n"))
|
||||||
assert(io.write('çfourth_line'))
|
assert(io.write('Xfourth_line'))
|
||||||
io.output(io.stdout)
|
io.output(io.stdout)
|
||||||
collectgarbage() -- file should be closed by GC
|
collectgarbage() -- file should be closed by GC
|
||||||
assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
|
assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
|
||||||
@@ -125,7 +125,7 @@ do
|
|||||||
-- closing file by scope
|
-- closing file by scope
|
||||||
local F = nil
|
local F = nil
|
||||||
do
|
do
|
||||||
local <toclose> f = assert(io.open(file, "w"))
|
local f <close> = assert(io.open(file, "w"))
|
||||||
F = f
|
F = f
|
||||||
end
|
end
|
||||||
assert(tostring(F) == "file (closed)")
|
assert(tostring(F) == "file (closed)")
|
||||||
@@ -135,7 +135,7 @@ assert(os.remove(file))
|
|||||||
|
|
||||||
do
|
do
|
||||||
-- test writing/reading numbers
|
-- test writing/reading numbers
|
||||||
local <toclose> f = assert(io.open(file, "w"))
|
local f <close> = assert(io.open(file, "w"))
|
||||||
f:write(maxint, '\n')
|
f:write(maxint, '\n')
|
||||||
f:write(string.format("0X%x\n", maxint))
|
f:write(string.format("0X%x\n", maxint))
|
||||||
f:write("0xABCp-3", '\n')
|
f:write("0xABCp-3", '\n')
|
||||||
@@ -144,7 +144,7 @@ do
|
|||||||
f:write(string.format("0x%X\n", -maxint))
|
f:write(string.format("0x%X\n", -maxint))
|
||||||
f:write("-0xABCp-3", '\n')
|
f:write("-0xABCp-3", '\n')
|
||||||
assert(f:close())
|
assert(f:close())
|
||||||
local <toclose> f = assert(io.open(file, "r"))
|
local f <close> = assert(io.open(file, "r"))
|
||||||
assert(f:read("n") == maxint)
|
assert(f:read("n") == maxint)
|
||||||
assert(f:read("n") == maxint)
|
assert(f:read("n") == maxint)
|
||||||
assert(f:read("n") == 0xABCp-3)
|
assert(f:read("n") == 0xABCp-3)
|
||||||
@@ -158,7 +158,7 @@ assert(os.remove(file))
|
|||||||
|
|
||||||
-- testing multiple arguments to io.read
|
-- testing multiple arguments to io.read
|
||||||
do
|
do
|
||||||
local <toclose> f = assert(io.open(file, "w"))
|
local f <close> = assert(io.open(file, "w"))
|
||||||
f:write[[
|
f:write[[
|
||||||
a line
|
a line
|
||||||
another line
|
another line
|
||||||
@@ -170,21 +170,21 @@ three
|
|||||||
]]
|
]]
|
||||||
local l1, l2, l3, l4, n1, n2, c, dummy
|
local l1, l2, l3, l4, n1, n2, c, dummy
|
||||||
assert(f:close())
|
assert(f:close())
|
||||||
local <toclose> f = assert(io.open(file, "r"))
|
local f <close> = assert(io.open(file, "r"))
|
||||||
l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n")
|
l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n")
|
||||||
assert(l1 == "a line" and l2 == "another line\n" and
|
assert(l1 == "a line" and l2 == "another line\n" and
|
||||||
n1 == 1234 and n2 == 3.45 and dummy == nil)
|
n1 == 1234 and n2 == 3.45 and dummy == nil)
|
||||||
assert(f:close())
|
assert(f:close())
|
||||||
local <toclose> f = assert(io.open(file, "r"))
|
local f <close> = assert(io.open(file, "r"))
|
||||||
l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l")
|
l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l")
|
||||||
assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and
|
assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and
|
||||||
n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two"
|
n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two"
|
||||||
and dummy == nil)
|
and dummy == nil)
|
||||||
assert(f:close())
|
assert(f:close())
|
||||||
local <toclose> f = assert(io.open(file, "r"))
|
local f <close> = assert(io.open(file, "r"))
|
||||||
-- second item failing
|
-- second item failing
|
||||||
l1, n1, n2, dummy = f:read("l", "n", "n", "l")
|
l1, n1, n2, dummy = f:read("l", "n", "n", "l")
|
||||||
assert(l1 == "a line" and n1 == nil)
|
assert(l1 == "a line" and not n1)
|
||||||
end
|
end
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
|
|
||||||
@@ -228,7 +228,7 @@ assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n")
|
|||||||
assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
|
assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
|
||||||
|
|
||||||
do -- attempt to read too long number
|
do -- attempt to read too long number
|
||||||
assert(f:read("n") == nil) -- fails
|
assert(not f:read("n")) -- fails
|
||||||
local s = f:read("L") -- read rest of line
|
local s = f:read("L") -- read rest of line
|
||||||
assert(string.find(s, "^00*\n$")) -- lots of 0's left
|
assert(string.find(s, "^00*\n$")) -- lots of 0's left
|
||||||
end
|
end
|
||||||
@@ -300,27 +300,27 @@ do -- test error returns
|
|||||||
end
|
end
|
||||||
checkerr("invalid format", io.read, "x")
|
checkerr("invalid format", io.read, "x")
|
||||||
assert(io.read(0) == "") -- not eof
|
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(0) == "")
|
||||||
assert(io.read() == "second line")
|
assert(io.read() == "second line")
|
||||||
local x = io.input():seek()
|
local x = io.input():seek()
|
||||||
assert(io.read() == "third line ")
|
assert(io.read() == "third line ")
|
||||||
assert(io.input():seek("set", x))
|
assert(io.input():seek("set", x))
|
||||||
assert(io.read('L') == "third line \n")
|
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.read(string.len"fourth_line") == "fourth_line")
|
||||||
assert(io.input():seek("cur", -string.len"fourth_line"))
|
assert(io.input():seek("cur", -string.len"fourth_line"))
|
||||||
assert(io.read() == "fourth_line")
|
assert(io.read() == "fourth_line")
|
||||||
assert(io.read() == "") -- empty line
|
assert(io.read() == "") -- empty line
|
||||||
assert(io.read('n') == 3450)
|
assert(io.read('n') == 3450)
|
||||||
assert(io.read(1) == '\n')
|
assert(io.read(1) == '\n')
|
||||||
assert(io.read(0) == nil) -- end of file
|
assert(not io.read(0)) -- end of file
|
||||||
assert(io.read(1) == nil) -- end of file
|
assert(not io.read(1)) -- end of file
|
||||||
assert(io.read(30000) == nil) -- end of file
|
assert(not io.read(30000)) -- end of file
|
||||||
assert(({io.read(1)})[2] == undef)
|
assert(({io.read(1)})[2] == undef)
|
||||||
assert(io.read() == nil) -- end of file
|
assert(not io.read()) -- end of file
|
||||||
assert(({io.read()})[2] == undef)
|
assert(({io.read()})[2] == undef)
|
||||||
assert(io.read('n') == nil) -- end of file
|
assert(not io.read('n')) -- end of file
|
||||||
assert(({io.read('n')})[2] == undef)
|
assert(({io.read('n')})[2] == undef)
|
||||||
assert(io.read('a') == '') -- end of file (OK for 'a')
|
assert(io.read('a') == '') -- end of file (OK for 'a')
|
||||||
assert(io.read('a') == '') -- end of file (OK for 'a')
|
assert(io.read('a') == '') -- end of file (OK for 'a')
|
||||||
@@ -356,7 +356,7 @@ assert(io.read(string.len(t)) == t)
|
|||||||
assert(io.read(1) == ' ')
|
assert(io.read(1) == ' ')
|
||||||
assert(io.read(0))
|
assert(io.read(0))
|
||||||
assert(io.read('a') == ';end of file\n')
|
assert(io.read('a') == ';end of file\n')
|
||||||
assert(io.read(0) == nil)
|
assert(not io.read(0))
|
||||||
assert(io.close(io.input()))
|
assert(io.close(io.input()))
|
||||||
|
|
||||||
|
|
||||||
@@ -364,7 +364,7 @@ assert(io.close(io.input()))
|
|||||||
do
|
do
|
||||||
local function ismsg (m)
|
local function ismsg (m)
|
||||||
-- error message is not a code number
|
-- error message is not a code number
|
||||||
return (type(m) == "string" and tonumber(m) == nil)
|
return (type(m) == "string" and not tonumber(m))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- read
|
-- read
|
||||||
@@ -393,7 +393,7 @@ assert(io.read"L" == "\n")
|
|||||||
assert(io.read"L" == "\n")
|
assert(io.read"L" == "\n")
|
||||||
assert(io.read"L" == "line\n")
|
assert(io.read"L" == "line\n")
|
||||||
assert(io.read"L" == "other")
|
assert(io.read"L" == "other")
|
||||||
assert(io.read"L" == nil)
|
assert(not io.read"L")
|
||||||
io.input():close()
|
io.input():close()
|
||||||
|
|
||||||
local f = assert(io.open(file))
|
local f = assert(io.open(file))
|
||||||
@@ -427,10 +427,12 @@ do -- testing closing file in line iteration
|
|||||||
-- get the to-be-closed variable from a loop
|
-- get the to-be-closed variable from a loop
|
||||||
local function gettoclose (lv)
|
local function gettoclose (lv)
|
||||||
lv = lv + 1
|
lv = lv + 1
|
||||||
for i = 1, math.maxinteger do
|
local stvar = 0 -- to-be-closed is 4th state variable in the loop
|
||||||
|
for i = 1, 1000 do
|
||||||
local n, v = debug.getlocal(lv, i)
|
local n, v = debug.getlocal(lv, i)
|
||||||
if n == "(for toclose)" then
|
if n == "(for state)" then
|
||||||
return v
|
stvar = stvar + 1
|
||||||
|
if stvar == 4 then return v end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -460,7 +462,7 @@ end
|
|||||||
-- test for multipe arguments in 'lines'
|
-- test for multipe arguments in 'lines'
|
||||||
io.output(file); io.write"0123456789\n":close()
|
io.output(file); io.write"0123456789\n":close()
|
||||||
for a,b in io.lines(file, 1, 1) do
|
for a,b in io.lines(file, 1, 1) do
|
||||||
if a == "\n" then assert(b == nil)
|
if a == "\n" then assert(not b)
|
||||||
else assert(tonumber(a) == tonumber(b) - 1)
|
else assert(tonumber(a) == tonumber(b) - 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -471,13 +473,13 @@ end
|
|||||||
|
|
||||||
for a,b,c in io.lines(file, "a", 0, 1) do
|
for a,b,c in io.lines(file, "a", 0, 1) do
|
||||||
if a == "" then break end
|
if a == "" then break end
|
||||||
assert(a == "0123456789\n" and b == nil and c == nil)
|
assert(a == "0123456789\n" and not b and not c)
|
||||||
end
|
end
|
||||||
collectgarbage() -- to close file in previous iteration
|
collectgarbage() -- to close file in previous iteration
|
||||||
|
|
||||||
io.output(file); io.write"00\n10\n20\n30\n40\n":close()
|
io.output(file); io.write"00\n10\n20\n30\n40\n":close()
|
||||||
for a, b in io.lines(file, "n", "n") do
|
for a, b in io.lines(file, "n", "n") do
|
||||||
if a == 40 then assert(b == nil)
|
if a == 40 then assert(not b)
|
||||||
else assert(a == b - 10)
|
else assert(a == b - 10)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -505,15 +507,17 @@ load((io.lines(file, 1)))()
|
|||||||
assert(_G.X == 4)
|
assert(_G.X == 4)
|
||||||
load((io.lines(file, 3)))()
|
load((io.lines(file, 3)))()
|
||||||
assert(_G.X == 8)
|
assert(_G.X == 8)
|
||||||
|
_G.X = nil
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
|
local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
|
||||||
io.output(file)
|
io.output(file)
|
||||||
assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1)))
|
assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1)))
|
||||||
io.close()
|
io.close()
|
||||||
assert(loadfile(file))()
|
assert(loadfile(file))()
|
||||||
assert(x1 == x2)
|
assert(x1 == _G.X2)
|
||||||
|
_G.X2 = nil
|
||||||
print('+')
|
print('+')
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
assert(not os.remove(file))
|
assert(not os.remove(file))
|
||||||
@@ -652,7 +656,7 @@ and the rest of the file
|
|||||||
io.input(file)
|
io.input(file)
|
||||||
local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
|
local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
|
||||||
assert(io.close(io.input()))
|
assert(io.close(io.input()))
|
||||||
assert(_ == ' ' and __ == nil)
|
assert(_ == ' ' and not __)
|
||||||
assert(type(a) == 'number' and a==123.4 and b==-56e-2)
|
assert(type(a) == 'number' and a==123.4 and b==-56e-2)
|
||||||
assert(d=='second line' and e=='third line')
|
assert(d=='second line' and e=='third line')
|
||||||
assert(h==[[
|
assert(h==[[
|
||||||
@@ -704,7 +708,7 @@ if not _soft then
|
|||||||
io.input():seek('set', 0)
|
io.input():seek('set', 0)
|
||||||
y = io.read() -- huge line
|
y = io.read() -- huge line
|
||||||
assert(x == y..'\n'..io.read())
|
assert(x == y..'\n'..io.read())
|
||||||
assert(io.read() == nil)
|
assert(not io.read())
|
||||||
io.close(io.input())
|
io.close(io.input())
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
x = nil; y = nil
|
x = nil; y = nil
|
||||||
@@ -719,6 +723,21 @@ if not _port then
|
|||||||
progname = '"' .. arg[i + 1] .. '"'
|
progname = '"' .. arg[i + 1] .. '"'
|
||||||
end
|
end
|
||||||
print("testing popen/pclose and execute")
|
print("testing popen/pclose and execute")
|
||||||
|
-- invalid mode for popen
|
||||||
|
checkerr("invalid mode", io.popen, "cat", "")
|
||||||
|
checkerr("invalid mode", io.popen, "cat", "r+")
|
||||||
|
checkerr("invalid mode", io.popen, "cat", "rw")
|
||||||
|
do -- basic tests for popen
|
||||||
|
local file = os.tmpname()
|
||||||
|
local f = assert(io.popen("cat - > " .. file, "w"))
|
||||||
|
f:write("a line")
|
||||||
|
assert(f:close())
|
||||||
|
local f = assert(io.popen("cat - < " .. file, "r"))
|
||||||
|
assert(f:read("a") == "a line")
|
||||||
|
assert(f:close())
|
||||||
|
assert(os.remove(file))
|
||||||
|
end
|
||||||
|
|
||||||
local tests = {
|
local tests = {
|
||||||
-- command, what, code
|
-- command, what, code
|
||||||
{"ls > /dev/null", "ok"},
|
{"ls > /dev/null", "ok"},
|
||||||
@@ -773,11 +792,24 @@ assert(os.date(string.rep("%d", 1000), t) ==
|
|||||||
string.rep(os.date("%d", t), 1000))
|
string.rep(os.date("%d", t), 1000))
|
||||||
assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
|
assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
|
||||||
|
|
||||||
local t = os.time()
|
local function checkDateTable (t)
|
||||||
D = os.date("*t", t)
|
_G.D = os.date("*t", t)
|
||||||
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
|
assert(os.time(D) == t)
|
||||||
D.hour==%H and D.min==%M and D.sec==%S and
|
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
|
||||||
D.wday==%w+1 and D.yday==%j)]], t))()
|
D.hour==%H and D.min==%M and D.sec==%S and
|
||||||
|
D.wday==%w+1 and D.yday==%j)]], t))()
|
||||||
|
_G.D = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
checkDateTable(os.time())
|
||||||
|
if not _port then
|
||||||
|
-- assume that time_t can represent these values
|
||||||
|
checkDateTable(0)
|
||||||
|
checkDateTable(1)
|
||||||
|
checkDateTable(1000)
|
||||||
|
checkDateTable(0x7fffffff)
|
||||||
|
checkDateTable(0x80000000)
|
||||||
|
end
|
||||||
|
|
||||||
checkerr("invalid conversion specifier", os.date, "%")
|
checkerr("invalid conversion specifier", os.date, "%")
|
||||||
checkerr("invalid conversion specifier", os.date, "%9")
|
checkerr("invalid conversion specifier", os.date, "%9")
|
||||||
@@ -791,11 +823,33 @@ checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5})
|
|||||||
|
|
||||||
checkerr("missing", os.time, {hour = 12}) -- missing date
|
checkerr("missing", os.time, {hour = 12}) -- missing date
|
||||||
|
|
||||||
|
|
||||||
|
if string.packsize("i") == 4 then -- 4-byte ints
|
||||||
|
checkerr("field 'year' is out-of-bound", os.time,
|
||||||
|
{year = -(1 << 31) + 1899, month = 1, day = 1})
|
||||||
|
|
||||||
|
checkerr("field 'year' is out-of-bound", os.time,
|
||||||
|
{year = -(1 << 31), month = 1, day = 1})
|
||||||
|
|
||||||
|
if math.maxinteger > 2^31 then -- larger lua_integer?
|
||||||
|
checkerr("field 'year' is out-of-bound", os.time,
|
||||||
|
{year = (1 << 31) + 1900, month = 1, day = 1})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
if not _port then
|
if not _port then
|
||||||
-- test Posix-specific modifiers
|
-- test Posix-specific modifiers
|
||||||
assert(type(os.date("%Ex")) == 'string')
|
assert(type(os.date("%Ex")) == 'string')
|
||||||
assert(type(os.date("%Oy")) == 'string')
|
assert(type(os.date("%Oy")) == 'string')
|
||||||
|
|
||||||
|
-- test large dates (assume at least 4-byte ints and time_t)
|
||||||
|
local t0 = os.time{year = 1970, month = 1, day = 0}
|
||||||
|
local t1 = os.time{year = 1970, month = 1, day = 0, sec = (1 << 31) - 1}
|
||||||
|
assert(t1 - t0 == (1 << 31) - 1)
|
||||||
|
t0 = os.time{year = 1970, month = 1, day = 1}
|
||||||
|
t1 = os.time{year = 1970, month = 1, day = 1, sec = -(1 << 31)}
|
||||||
|
assert(t1 - t0 == -(1 << 31))
|
||||||
|
|
||||||
-- test out-of-range dates (at least for Unix)
|
-- test out-of-range dates (at least for Unix)
|
||||||
if maxint >= 2^62 then -- cannot do these tests in Small Lua
|
if maxint >= 2^62 then -- cannot do these tests in Small Lua
|
||||||
@@ -810,25 +864,37 @@ if not _port then
|
|||||||
-- time_t has 8 bytes; an int year cannot represent a huge time
|
-- time_t has 8 bytes; an int year cannot represent a huge time
|
||||||
print(" 8-byte time_t")
|
print(" 8-byte time_t")
|
||||||
checkerr("cannot be represented", os.date, "%Y", 2^60)
|
checkerr("cannot be represented", os.date, "%Y", 2^60)
|
||||||
-- it should have no problems with year 4000
|
|
||||||
assert(tonumber(os.time{year=4000, month=1, day=1}))
|
-- this is the maximum year
|
||||||
|
assert(tonumber(os.time
|
||||||
|
{year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=59}))
|
||||||
|
|
||||||
|
-- this is too much
|
||||||
|
checkerr("represented", os.time,
|
||||||
|
{year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=60})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- internal 'int' fields cannot hold these values
|
||||||
|
checkerr("field 'day' is out-of-bound", os.time,
|
||||||
|
{year = 0, month = 1, day = 2^32})
|
||||||
|
|
||||||
|
checkerr("field 'month' is out-of-bound", os.time,
|
||||||
|
{year = 0, month = -((1 << 31) + 1), day = 1})
|
||||||
|
|
||||||
|
checkerr("field 'year' is out-of-bound", os.time,
|
||||||
|
{year = (1 << 31) + 1900, month = 1, day = 1})
|
||||||
|
|
||||||
else -- 8-byte ints
|
else -- 8-byte ints
|
||||||
-- assume time_t has 8 bytes too
|
-- assume time_t has 8 bytes too
|
||||||
print(" 8-byte time_t")
|
print(" 8-byte time_t")
|
||||||
assert(tonumber(os.date("%Y", 2^60)))
|
assert(tonumber(os.date("%Y", 2^60)))
|
||||||
|
|
||||||
-- but still cannot represent a huge year
|
-- but still cannot represent a huge year
|
||||||
checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
|
checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
D = os.date("!*t", 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
|
|
||||||
D.wday==%w+1 and D.yday==%j)]], t))()
|
|
||||||
|
|
||||||
do
|
do
|
||||||
local D = os.date("*t")
|
local D = os.date("*t")
|
||||||
local t = os.time(D)
|
local t = os.time(D)
|
||||||
@@ -842,6 +908,7 @@ do
|
|||||||
assert(t == t1) -- if isdst is absent uses correct default
|
assert(t == t1) -- if isdst is absent uses correct default
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local D = os.date("*t")
|
||||||
t = os.time(D)
|
t = os.time(D)
|
||||||
D.year = D.year-1;
|
D.year = D.year-1;
|
||||||
local t1 = os.time(D)
|
local t1 = os.time(D)
|
||||||
|
|||||||
+59
-37
@@ -99,40 +99,33 @@ local function GC() GC1(); GC2() end
|
|||||||
do
|
do
|
||||||
print("creating many objects")
|
print("creating many objects")
|
||||||
|
|
||||||
local contCreate = 0
|
|
||||||
|
|
||||||
local limit = 5000
|
local limit = 5000
|
||||||
|
|
||||||
while contCreate <= limit do
|
for i = 1, limit do
|
||||||
local a = {}; a = nil
|
local a = {}; a = nil
|
||||||
contCreate = contCreate+1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local a = "a"
|
local a = "a"
|
||||||
|
|
||||||
contCreate = 0
|
for i = 1, limit do
|
||||||
while contCreate <= limit do
|
a = i .. "b";
|
||||||
a = contCreate .. "b";
|
|
||||||
a = string.gsub(a, '(%d%d*)', "%1 %1")
|
a = string.gsub(a, '(%d%d*)', "%1 %1")
|
||||||
a = "a"
|
a = "a"
|
||||||
contCreate = contCreate+1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
contCreate = 0
|
|
||||||
|
|
||||||
a = {}
|
a = {}
|
||||||
|
|
||||||
function a:test ()
|
function a:test ()
|
||||||
while contCreate <= limit do
|
for i = 1, limit do
|
||||||
load(string.format("function temp(a) return 'a%d' end", contCreate), "")()
|
load(string.format("function temp(a) return 'a%d' end", i), "")()
|
||||||
assert(temp() == string.format('a%d', contCreate))
|
assert(temp() == string.format('a%d', i))
|
||||||
contCreate = contCreate+1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
a:test()
|
a:test()
|
||||||
|
_G.temp = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -141,7 +134,7 @@ do local f = function () end end
|
|||||||
|
|
||||||
|
|
||||||
print("functions with errors")
|
print("functions with errors")
|
||||||
prog = [[
|
local prog = [[
|
||||||
do
|
do
|
||||||
a = 10;
|
a = 10;
|
||||||
function foo(x,y)
|
function foo(x,y)
|
||||||
@@ -160,23 +153,25 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
rawset(_G, "a", nil)
|
||||||
|
_G.x = nil
|
||||||
|
|
||||||
foo = nil
|
do
|
||||||
print('long strings')
|
foo = nil
|
||||||
x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
print('long strings')
|
||||||
assert(string.len(x)==80)
|
local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
||||||
s = ''
|
assert(string.len(x)==80)
|
||||||
n = 0
|
local s = ''
|
||||||
k = math.min(300, (math.maxinteger // 80) // 2)
|
local k = math.min(300, (math.maxinteger // 80) // 2)
|
||||||
while n < k do s = s..x; n=n+1; j=tostring(n) end
|
for n = 1, k do s = s..x; local j=tostring(n) end
|
||||||
assert(string.len(s) == k*80)
|
assert(string.len(s) == k*80)
|
||||||
s = string.sub(s, 1, 10000)
|
s = string.sub(s, 1, 10000)
|
||||||
s, i = string.gsub(s, '(%d%d%d%d)', '')
|
local s, i = string.gsub(s, '(%d%d%d%d)', '')
|
||||||
assert(i==10000 // 4)
|
assert(i==10000 // 4)
|
||||||
s = nil
|
|
||||||
x = nil
|
|
||||||
|
|
||||||
assert(_G["while"] == 234)
|
assert(_G["while"] == 234)
|
||||||
|
_G["while"] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
@@ -235,8 +230,8 @@ end
|
|||||||
|
|
||||||
|
|
||||||
print("clearing tables")
|
print("clearing tables")
|
||||||
lim = 15
|
local lim = 15
|
||||||
a = {}
|
local a = {}
|
||||||
-- fill a with `collectable' indices
|
-- fill a with `collectable' indices
|
||||||
for i=1,lim do a[{}] = i end
|
for i=1,lim do a[{}] = i end
|
||||||
b = {}
|
b = {}
|
||||||
@@ -306,6 +301,16 @@ collectgarbage()
|
|||||||
assert(next(a) == string.rep('$', 11))
|
assert(next(a) == string.rep('$', 11))
|
||||||
|
|
||||||
|
|
||||||
|
if T then -- bug since 5.3: all-weak tables are not being revisited
|
||||||
|
T.gcstate("propagate")
|
||||||
|
local t = setmetatable({}, {__mode = "kv"})
|
||||||
|
T.gcstate("atomic") -- '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
|
-- 'bug' in 5.1
|
||||||
a = {}
|
a = {}
|
||||||
local t = {x = 10}
|
local t = {x = 10}
|
||||||
@@ -377,9 +382,10 @@ if T then
|
|||||||
s[n] = i
|
s[n] = i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
warn("@on"); warn("@store")
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(string.find(_WARN, "error in __gc metamethod"))
|
assert(string.find(_WARN, "error in __gc"))
|
||||||
assert(string.match(_WARN, "@(.-)@") == "expected")
|
assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false
|
||||||
for i = 8, 10 do assert(s[i]) end
|
for i = 8, 10 do assert(s[i]) end
|
||||||
|
|
||||||
for i = 1, 5 do
|
for i = 1, 5 do
|
||||||
@@ -391,6 +397,7 @@ if T then
|
|||||||
for i = 1, 10 do assert(s[i]) end
|
for i = 1, 10 do assert(s[i]) end
|
||||||
|
|
||||||
getmetatable(u).__gc = nil
|
getmetatable(u).__gc = nil
|
||||||
|
warn("@normal")
|
||||||
|
|
||||||
end
|
end
|
||||||
print '+'
|
print '+'
|
||||||
@@ -483,9 +490,12 @@ end
|
|||||||
|
|
||||||
-- errors during collection
|
-- errors during collection
|
||||||
if T then
|
if T then
|
||||||
|
warn("@store")
|
||||||
u = setmetatable({}, {__gc = function () error "@expected error" end})
|
u = setmetatable({}, {__gc = function () error "@expected error" end})
|
||||||
u = nil
|
u = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
assert(string.find(_WARN, "@expected error")); _WARN = false
|
||||||
|
warn("@normal")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -555,6 +565,7 @@ do
|
|||||||
for i=1,1000 do _ENV.a = {} end -- no collection during the loop
|
for i=1,1000 do _ENV.a = {} end -- no collection during the loop
|
||||||
until gcinfo() > 2 * x
|
until gcinfo() > 2 * x
|
||||||
collectgarbage"restart"
|
collectgarbage"restart"
|
||||||
|
_ENV.a = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -643,7 +654,7 @@ do
|
|||||||
assert(getmetatable(o) == tt)
|
assert(getmetatable(o) == tt)
|
||||||
-- create new objects during GC
|
-- create new objects during GC
|
||||||
local a = 'xuxu'..(10+3)..'joao', {}
|
local a = 'xuxu'..(10+3)..'joao', {}
|
||||||
___Glob = o -- ressurect object!
|
___Glob = o -- ressurrect object!
|
||||||
setmetatable({}, tt) -- creates a new one with same metatable
|
setmetatable({}, tt) -- creates a new one with same metatable
|
||||||
print(">>> closing state " .. "<<<\n")
|
print(">>> closing state " .. "<<<\n")
|
||||||
end
|
end
|
||||||
@@ -653,20 +664,21 @@ end
|
|||||||
|
|
||||||
-- create several objects to raise errors when collected while closing state
|
-- create several objects to raise errors when collected while closing state
|
||||||
if T then
|
if T then
|
||||||
local error, assert, find = error, assert, string.find
|
local error, assert, find, warn = error, assert, string.find, warn
|
||||||
local n = 0
|
local n = 0
|
||||||
local lastmsg
|
local lastmsg
|
||||||
local mt = {__gc = function (o)
|
local mt = {__gc = function (o)
|
||||||
n = n + 1
|
n = n + 1
|
||||||
assert(n == o[1])
|
assert(n == o[1])
|
||||||
if n == 1 then
|
if n == 1 then
|
||||||
_WARN = nil
|
_WARN = false
|
||||||
elseif n == 2 then
|
elseif n == 2 then
|
||||||
assert(find(_WARN, "@expected warning"))
|
assert(find(_WARN, "@expected warning"))
|
||||||
lastmsg = _WARN -- get message from previous error (first 'o')
|
lastmsg = _WARN -- get message from previous error (first 'o')
|
||||||
else
|
else
|
||||||
assert(lastmsg == _WARN) -- subsequent error messages are equal
|
assert(lastmsg == _WARN) -- subsequent error messages are equal
|
||||||
end
|
end
|
||||||
|
warn("@store"); _WARN = false
|
||||||
error"@expected warning"
|
error"@expected warning"
|
||||||
end}
|
end}
|
||||||
for i = 10, 1, -1 do
|
for i = 10, 1, -1 do
|
||||||
@@ -678,6 +690,16 @@ end
|
|||||||
-- just to make sure
|
-- just to make sure
|
||||||
assert(collectgarbage'isrunning')
|
assert(collectgarbage'isrunning')
|
||||||
|
|
||||||
|
do -- check that the collector is not reentrant in incremental mode
|
||||||
|
local res = true
|
||||||
|
setmetatable({}, {__gc = function ()
|
||||||
|
res = collectgarbage()
|
||||||
|
end})
|
||||||
|
collectgarbage()
|
||||||
|
assert(not res)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
collectgarbage(oldmode)
|
collectgarbage(oldmode)
|
||||||
|
|
||||||
print('OK')
|
print('OK')
|
||||||
|
|||||||
@@ -37,6 +37,92 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do
|
||||||
|
-- ensure that 'firstold1' is corrected when object is removed from
|
||||||
|
-- the 'allgc' list
|
||||||
|
local function foo () end
|
||||||
|
local old = {10}
|
||||||
|
collectgarbage() -- make 'old' old
|
||||||
|
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
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- bug in 5.4.0
|
||||||
|
-- When an object aged OLD1 is finalized, it is moved from the list
|
||||||
|
-- 'finobj' to the *beginning* of the list 'allgc', but that part of the
|
||||||
|
-- list was not being visited by 'markold'.
|
||||||
|
local A = {}
|
||||||
|
A[1] = false -- old anchor for object
|
||||||
|
|
||||||
|
-- obj finalizer
|
||||||
|
local function gcf (obj)
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- another bug in 5.4.0
|
||||||
|
local old = {10}
|
||||||
|
collectgarbage() -- make 'old' old
|
||||||
|
local co = coroutine.create(
|
||||||
|
function ()
|
||||||
|
local x = nil
|
||||||
|
local f = function ()
|
||||||
|
return x[1]
|
||||||
|
end
|
||||||
|
x = coroutine.yield(f)
|
||||||
|
coroutine.yield()
|
||||||
|
end
|
||||||
|
)
|
||||||
|
local _, f = coroutine.resume(co) -- create closure over 'x' in coroutine
|
||||||
|
collectgarbage("step", 0) -- 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
|
||||||
|
assert(f() == 123 and old[1][1] == "hello")
|
||||||
|
collectgarbage("step", 0) -- run the collector once more
|
||||||
|
-- make sure old[1] was not collected
|
||||||
|
assert(f() == 123 and old[1][1] == "hello")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- bug introduced in commit 9cf3299fa
|
||||||
|
local t = setmetatable({}, {__mode = "kv"}) -- all-weak table
|
||||||
|
collectgarbage() -- full collection
|
||||||
|
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
|
||||||
|
assert(not T or (T.gcage(t) == "touched2" and T.gccolor(t) == "black"))
|
||||||
|
collectgarbage("step", 0) -- 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
|
||||||
|
-- t, being old, is ignored by the collection, so it is not cleared
|
||||||
|
assert(t[1] == nil) -- fails with the bug
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
if T == nil then
|
if T == nil then
|
||||||
(Message or print)('\n >>> testC not active: \z
|
(Message or print)('\n >>> testC not active: \z
|
||||||
skipping some generational tests <<<\n')
|
skipping some generational tests <<<\n')
|
||||||
@@ -72,6 +158,9 @@ do
|
|||||||
assert(debug.getuservalue(U).x[1] == 234)
|
assert(debug.getuservalue(U).x[1] == 234)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- just to make sure
|
||||||
|
assert(collectgarbage'isrunning')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- just to make sure
|
-- just to make sure
|
||||||
|
|||||||
+1
-1
@@ -258,7 +258,7 @@ do
|
|||||||
::L2:: goto L3
|
::L2:: goto L3
|
||||||
|
|
||||||
::L1:: do
|
::L1:: do
|
||||||
local <toclose> a = setmetatable({}, {__close = function () X = true end})
|
local a <close> = setmetatable({}, {__close = function () X = true end})
|
||||||
assert(X == nil)
|
assert(X == nil)
|
||||||
if a then goto L2 end -- jumping back out of scope of 'a'
|
if a then goto L2 end -- jumping back out of scope of 'a'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,17 +11,17 @@ CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared
|
|||||||
all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so
|
all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so
|
||||||
touch all
|
touch all
|
||||||
|
|
||||||
lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
||||||
$(CC) $(CFLAGS) -o lib1.so lib1.c
|
$(CC) $(CFLAGS) -o lib1.so lib1.c
|
||||||
|
|
||||||
lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
||||||
$(CC) $(CFLAGS) -o lib11.so lib11.c
|
$(CC) $(CFLAGS) -o lib11.so lib11.c
|
||||||
|
|
||||||
lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
||||||
$(CC) $(CFLAGS) -o lib2.so lib2.c
|
$(CC) $(CFLAGS) -o lib2.so lib2.c
|
||||||
|
|
||||||
lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
||||||
$(CC) $(CFLAGS) -o lib21.so lib21.c
|
$(CC) $(CFLAGS) -o lib21.so lib21.c
|
||||||
|
|
||||||
lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
||||||
$(CC) $(CFLAGS) -o lib2-v2.so lib22.c
|
$(CC) $(CFLAGS) -o lib2-v2.so lib22.c
|
||||||
|
|||||||
+42
-17
@@ -10,6 +10,7 @@ local function dostring (x) return assert(load(x), "")() end
|
|||||||
|
|
||||||
dostring("x \v\f = \t\r 'a\0a' \v\f\f")
|
dostring("x \v\f = \t\r 'a\0a' \v\f\f")
|
||||||
assert(x == 'a\0a' and string.len(x) == 3)
|
assert(x == 'a\0a' and string.len(x) == 3)
|
||||||
|
_G.x = nil
|
||||||
|
|
||||||
-- escape sequences
|
-- escape sequences
|
||||||
assert('\n\"\'\\' == [[
|
assert('\n\"\'\\' == [[
|
||||||
@@ -129,16 +130,16 @@ end
|
|||||||
|
|
||||||
-- long variable names
|
-- long variable names
|
||||||
|
|
||||||
var1 = string.rep('a', 15000) .. '1'
|
local var1 = string.rep('a', 15000) .. '1'
|
||||||
var2 = string.rep('a', 15000) .. '2'
|
local var2 = string.rep('a', 15000) .. '2'
|
||||||
prog = string.format([[
|
local prog = string.format([[
|
||||||
%s = 5
|
%s = 5
|
||||||
%s = %s + 1
|
%s = %s + 1
|
||||||
return function () return %s - %s end
|
return function () return %s - %s end
|
||||||
]], var1, var2, var1, var1, var2)
|
]], var1, var2, var1, var1, var2)
|
||||||
local f = dostring(prog)
|
local f = dostring(prog)
|
||||||
assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
|
assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
|
||||||
var1, var2, f = nil
|
_G[var1], _G[var2] = nil
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
-- escapes --
|
-- escapes --
|
||||||
@@ -150,13 +151,13 @@ assert([[
|
|||||||
$debug]] == "\n $debug")
|
$debug]] == "\n $debug")
|
||||||
assert([[ [ ]] ~= [[ ] ]])
|
assert([[ [ ]] ~= [[ ] ]])
|
||||||
-- long strings --
|
-- long strings --
|
||||||
b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
||||||
assert(string.len(b) == 960)
|
assert(string.len(b) == 960)
|
||||||
prog = [=[
|
prog = [=[
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
a1 = [["this is a 'string' with several 'quotes'"]]
|
local a1 = [["this is a 'string' with several 'quotes'"]]
|
||||||
a2 = "'quotes'"
|
local a2 = "'quotes'"
|
||||||
|
|
||||||
assert(string.find(a1, a2) == 34)
|
assert(string.find(a1, a2) == 34)
|
||||||
print('+')
|
print('+')
|
||||||
@@ -164,12 +165,13 @@ print('+')
|
|||||||
a1 = [==[temp = [[an arbitrary value]]; ]==]
|
a1 = [==[temp = [[an arbitrary value]]; ]==]
|
||||||
assert(load(a1))()
|
assert(load(a1))()
|
||||||
assert(temp == 'an arbitrary value')
|
assert(temp == 'an arbitrary value')
|
||||||
|
_G.temp = nil
|
||||||
-- long strings --
|
-- long strings --
|
||||||
b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
||||||
assert(string.len(b) == 960)
|
assert(string.len(b) == 960)
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
@@ -199,19 +201,41 @@ x = 1
|
|||||||
]=]
|
]=]
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
x = nil
|
_G.x = nil
|
||||||
dostring(prog)
|
dostring(prog)
|
||||||
assert(x)
|
assert(x)
|
||||||
|
_G.x = nil
|
||||||
|
|
||||||
prog = nil
|
|
||||||
a = nil
|
|
||||||
b = nil
|
do -- reuse of long strings
|
||||||
|
|
||||||
|
-- get the address of a string
|
||||||
|
local function getadd (s) return string.format("%p", s) end
|
||||||
|
|
||||||
|
local s1 <const> = "01234567890123456789012345678901234567890123456789"
|
||||||
|
local s2 <const> = "01234567890123456789012345678901234567890123456789"
|
||||||
|
local s3 = "01234567890123456789012345678901234567890123456789"
|
||||||
|
local function foo() return s1 end
|
||||||
|
local function foo1() return s3 end
|
||||||
|
local function foo2()
|
||||||
|
return "01234567890123456789012345678901234567890123456789"
|
||||||
|
end
|
||||||
|
local a1 = getadd(s1)
|
||||||
|
assert(a1 == getadd(s2))
|
||||||
|
assert(a1 == getadd(foo()))
|
||||||
|
assert(a1 == getadd(foo1()))
|
||||||
|
assert(a1 == getadd(foo2()))
|
||||||
|
|
||||||
|
local sd = "0123456789" .. "0123456789012345678901234567890123456789"
|
||||||
|
assert(sd == s1 and getadd(sd) ~= a1)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- testing line ends
|
-- testing line ends
|
||||||
prog = [[
|
prog = [[
|
||||||
a = 1 -- a comment
|
local a = 1 -- a comment
|
||||||
b = 2
|
local b = 2
|
||||||
|
|
||||||
|
|
||||||
x = [=[
|
x = [=[
|
||||||
@@ -228,10 +252,11 @@ for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
|
|||||||
assert(dostring(prog) == nn)
|
assert(dostring(prog) == nn)
|
||||||
assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
|
assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
|
||||||
end
|
end
|
||||||
|
_G.x, _G.y = nil
|
||||||
|
|
||||||
|
|
||||||
-- testing comments and strings with long brackets
|
-- testing comments and strings with long brackets
|
||||||
a = [==[]=]==]
|
local a = [==[]=]==]
|
||||||
assert(a == "]=")
|
assert(a == "]=")
|
||||||
|
|
||||||
a = [==[[===[[=[]]=][====[]]===]===]==]
|
a = [==[[===[[=[]]=][====[]]===]===]==]
|
||||||
@@ -281,7 +306,7 @@ if os.setlocale("pt_BR") or os.setlocale("ptb") then
|
|||||||
|
|
||||||
assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
|
assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
|
||||||
|
|
||||||
assert(tonumber"inf" == nil and tonumber"NAN" == nil)
|
assert(not tonumber"inf" and not tonumber"NAN")
|
||||||
|
|
||||||
assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
|
assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
|
||||||
|
|
||||||
|
|||||||
+740
-107
File diff suppressed because it is too large
Load Diff
+177
-29
@@ -27,22 +27,26 @@ do
|
|||||||
end
|
end
|
||||||
print("progname: "..progname)
|
print("progname: "..progname)
|
||||||
|
|
||||||
local prepfile = function (s, p)
|
|
||||||
p = p or prog
|
local prepfile = function (s, mod, p)
|
||||||
io.output(p)
|
mod = mod and "wb" or "w" -- mod true means binary files
|
||||||
io.write(s)
|
p = p or prog -- file to write the program
|
||||||
assert(io.close())
|
local f = io.open(p, mod)
|
||||||
|
f:write(s)
|
||||||
|
assert(f:close())
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getoutput ()
|
local function getoutput ()
|
||||||
io.input(out)
|
local f = io.open(out)
|
||||||
local t = io.read("a")
|
local t = f:read("a")
|
||||||
io.input():close()
|
f:close()
|
||||||
assert(os.remove(out))
|
assert(os.remove(out))
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local function checkprogout (s)
|
local function checkprogout (s)
|
||||||
|
-- expected result must end with new line
|
||||||
|
assert(string.sub(s, -1) == "\n")
|
||||||
local t = getoutput()
|
local t = getoutput()
|
||||||
for line in string.gmatch(s, ".-\n") do
|
for line in string.gmatch(s, ".-\n") do
|
||||||
assert(string.find(t, line, 1, true))
|
assert(string.find(t, line, 1, true))
|
||||||
@@ -63,10 +67,11 @@ local function RUN (p, ...)
|
|||||||
assert(os.execute(s))
|
assert(os.execute(s))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function NoRun (msg, p, ...)
|
local function NoRun (msg, p, ...)
|
||||||
p = string.gsub(p, "lua", '"'..progname..'"', 1)
|
p = string.gsub(p, "lua", '"'..progname..'"', 1)
|
||||||
local s = string.format(p, ...)
|
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(not os.execute(s))
|
||||||
assert(string.find(getoutput(), msg, 1, true)) -- check error message
|
assert(string.find(getoutput(), msg, 1, true)) -- check error message
|
||||||
end
|
end
|
||||||
@@ -85,13 +90,40 @@ prepfile[[
|
|||||||
1, a
|
1, a
|
||||||
)
|
)
|
||||||
]]
|
]]
|
||||||
RUN('lua - < %s > %s', prog, out)
|
RUN('lua - -- < %s > %s', prog, out)
|
||||||
checkout("1\tnil\n")
|
checkout("1\tnil\n")
|
||||||
|
|
||||||
RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
|
RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
|
||||||
checkout("10\n2\n")
|
checkout("10\n2\n")
|
||||||
|
|
||||||
|
|
||||||
|
-- testing BOM
|
||||||
|
prepfile("\xEF\xBB\xBF")
|
||||||
|
RUN('lua %s > %s', prog, out)
|
||||||
|
checkout("")
|
||||||
|
|
||||||
|
prepfile("\xEF\xBB\xBFprint(3)")
|
||||||
|
RUN('lua %s > %s', prog, out)
|
||||||
|
checkout("3\n")
|
||||||
|
|
||||||
|
prepfile("\xEF\xBB\xBF# comment!!\nprint(3)")
|
||||||
|
RUN('lua %s > %s', prog, out)
|
||||||
|
checkout("3\n")
|
||||||
|
|
||||||
|
-- bad BOMs
|
||||||
|
prepfile("\xEF", true)
|
||||||
|
NoRun("unexpected symbol", 'lua %s', prog)
|
||||||
|
|
||||||
|
prepfile("\xEF\xBB", true)
|
||||||
|
NoRun("unexpected symbol", 'lua %s', prog)
|
||||||
|
|
||||||
|
prepfile("\xEFprint(3)", true)
|
||||||
|
NoRun("unexpected symbol", 'lua %s', prog)
|
||||||
|
|
||||||
|
prepfile("\xEF\xBBprint(3)", true)
|
||||||
|
NoRun("unexpected symbol", 'lua %s', prog)
|
||||||
|
|
||||||
|
|
||||||
-- test option '-'
|
-- test option '-'
|
||||||
RUN('echo "print(arg[1])" | lua - -h > %s', out)
|
RUN('echo "print(arg[1])" | lua - -h > %s', out)
|
||||||
checkout("-h\n")
|
checkout("-h\n")
|
||||||
@@ -101,7 +133,7 @@ checkout("-h\n")
|
|||||||
prepfile("print(package.path)")
|
prepfile("print(package.path)")
|
||||||
|
|
||||||
-- test LUA_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")
|
checkout("x\n")
|
||||||
|
|
||||||
-- test LUA_PATH_version
|
-- test LUA_PATH_version
|
||||||
@@ -184,10 +216,22 @@ convert("a;b;;c")
|
|||||||
|
|
||||||
-- test -l over multiple libraries
|
-- test -l over multiple libraries
|
||||||
prepfile("print(1); a=2; return {x=15}")
|
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)
|
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")
|
checkout("1\n2\n15\n2\n15\n")
|
||||||
|
|
||||||
|
-- test explicit global names in -l
|
||||||
|
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 sufix ("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
|
-- test 'arg' table
|
||||||
local a = [[
|
local a = [[
|
||||||
assert(#arg == 3 and arg[1] == 'a' and
|
assert(#arg == 3 and arg[1] == 'a' and
|
||||||
@@ -203,7 +247,7 @@ RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command
|
|||||||
|
|
||||||
-- test 'arg' availability in libraries
|
-- test 'arg' availability in libraries
|
||||||
prepfile"assert(arg)"
|
prepfile"assert(arg)"
|
||||||
prepfile("assert(arg)", otherprog)
|
prepfile("assert(arg)", false, otherprog)
|
||||||
RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
|
RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
|
||||||
|
|
||||||
-- test messing up the 'arg' table
|
-- test messing up the 'arg' table
|
||||||
@@ -219,6 +263,70 @@ assert(string.find(getoutput(), "error calling 'print'"))
|
|||||||
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
|
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
|
||||||
checkout("lua_debug> 1000lua_debug> ")
|
checkout("lua_debug> 1000lua_debug> ")
|
||||||
|
|
||||||
|
|
||||||
|
print("testing warnings")
|
||||||
|
|
||||||
|
-- no warnings by default
|
||||||
|
RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua 2> %s', out)
|
||||||
|
checkout("1")
|
||||||
|
|
||||||
|
prepfile[[
|
||||||
|
warn("@allow") -- unknown control, ignored
|
||||||
|
warn("@off", "XXX", "@off") -- these are not control messages
|
||||||
|
warn("@off") -- this one is
|
||||||
|
warn("@on", "YYY", "@on") -- not control, but warn is off
|
||||||
|
warn("@off") -- keep it off
|
||||||
|
warn("@on") -- restart warnings
|
||||||
|
warn("", "@on") -- again, no control, real warning
|
||||||
|
warn("@on") -- keep it "started"
|
||||||
|
warn("Z", "Z", "Z") -- common warning
|
||||||
|
]]
|
||||||
|
RUN('lua -W %s 2> %s', prog, out)
|
||||||
|
checkout[[
|
||||||
|
Lua warning: @offXXX@off
|
||||||
|
Lua warning: @on
|
||||||
|
Lua warning: ZZZ
|
||||||
|
]]
|
||||||
|
|
||||||
|
prepfile[[
|
||||||
|
warn("@allow")
|
||||||
|
-- create two objects to be finalized when closing state
|
||||||
|
-- the errors in the finalizers must generate warnings
|
||||||
|
u1 = setmetatable({}, {__gc = function () error("XYZ") end})
|
||||||
|
u2 = setmetatable({}, {__gc = function () error("ZYX") end})
|
||||||
|
]]
|
||||||
|
RUN('lua -W %s 2> %s', prog, out)
|
||||||
|
checkprogout("ZYX)\nXYZ)\n")
|
||||||
|
|
||||||
|
-- bug since 5.2: finalizer called when closing a state could
|
||||||
|
-- subvert finalization order
|
||||||
|
prepfile[[
|
||||||
|
-- should be called last
|
||||||
|
print("creating 1")
|
||||||
|
setmetatable({}, {__gc = function () print(1) end})
|
||||||
|
|
||||||
|
print("creating 2")
|
||||||
|
setmetatable({}, {__gc = function ()
|
||||||
|
print("2")
|
||||||
|
print("creating 3")
|
||||||
|
-- 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
|
||||||
|
os.exit(0, true)
|
||||||
|
end})
|
||||||
|
]]
|
||||||
|
RUN('lua -W %s > %s', prog, out)
|
||||||
|
checkout[[
|
||||||
|
creating 1
|
||||||
|
creating 2
|
||||||
|
2
|
||||||
|
creating 3
|
||||||
|
nil
|
||||||
|
1
|
||||||
|
]]
|
||||||
|
|
||||||
|
|
||||||
-- test many arguments
|
-- test many arguments
|
||||||
prepfile[[print(({...})[30])]]
|
prepfile[[print(({...})[30])]]
|
||||||
RUN('lua %s %s > %s', prog, string.rep(" a", 30), out)
|
RUN('lua %s %s > %s', prog, string.rep(" a", 30), out)
|
||||||
@@ -238,10 +346,15 @@ RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
|||||||
checkprogout("6\n10\n10\n\n")
|
checkprogout("6\n10\n10\n\n")
|
||||||
|
|
||||||
prepfile("a = [[b\nc\nd\ne]]\n=a")
|
prepfile("a = [[b\nc\nd\ne]]\n=a")
|
||||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i -- < %s > %s]], prog, out)
|
||||||
checkprogout("b\nc\nd\ne\n\n")
|
checkprogout("b\nc\nd\ne\n\n")
|
||||||
|
|
||||||
prompt = "alo"
|
-- 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[[ --
|
prepfile[[ --
|
||||||
a = 2
|
a = 2
|
||||||
]]
|
]]
|
||||||
@@ -249,6 +362,33 @@ RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out)
|
|||||||
local t = getoutput()
|
local t = getoutput()
|
||||||
assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
|
assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
|
||||||
|
|
||||||
|
-- using the prompt default
|
||||||
|
prepfile[[ --
|
||||||
|
a = 2
|
||||||
|
]]
|
||||||
|
RUN([[lua -i < %s > %s]], prog, out)
|
||||||
|
local t = getoutput()
|
||||||
|
prompt = "> " -- the default
|
||||||
|
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})"
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
-- test for error objects
|
-- test for error objects
|
||||||
prepfile[[
|
prepfile[[
|
||||||
debug = require "debug"
|
debug = require "debug"
|
||||||
@@ -265,7 +405,7 @@ NoRun("error object is a table value", [[lua %s]], prog)
|
|||||||
|
|
||||||
|
|
||||||
-- chunk broken in many lines
|
-- chunk broken in many lines
|
||||||
s = [=[ --
|
local s = [=[ --
|
||||||
function f ( x )
|
function f ( x )
|
||||||
local a = [[
|
local a = [[
|
||||||
xuxu
|
xuxu
|
||||||
@@ -287,12 +427,10 @@ checkprogout("101\n13\t22\n\n")
|
|||||||
prepfile[[#comment in 1st line without \n at the end]]
|
prepfile[[#comment in 1st line without \n at the end]]
|
||||||
RUN('lua %s', prog)
|
RUN('lua %s', prog)
|
||||||
|
|
||||||
prepfile[[#test line number when file starts with comment line
|
-- first-line comment with binary file
|
||||||
debug = require"debug"
|
prepfile("#comment\n" .. string.dump(load("print(3)")), true)
|
||||||
print(debug.getinfo(1).currentline)
|
|
||||||
]]
|
|
||||||
RUN('lua %s > %s', prog, out)
|
RUN('lua %s > %s', prog, out)
|
||||||
checkprogout('3')
|
checkout('3\n')
|
||||||
|
|
||||||
-- close Lua with an open file
|
-- close Lua with an open file
|
||||||
prepfile(string.format([[io.output(%q); io.write('alo')]], out))
|
prepfile(string.format([[io.output(%q); io.write('alo')]], out))
|
||||||
@@ -320,15 +458,16 @@ NoRun("", "lua %s", prog) -- no message
|
|||||||
|
|
||||||
-- to-be-closed variables in main chunk
|
-- to-be-closed variables in main chunk
|
||||||
prepfile[[
|
prepfile[[
|
||||||
local <toclose> x = function (err)
|
local x <close> = setmetatable({},
|
||||||
assert(err == 120)
|
{__close = function (self, err)
|
||||||
print("Ok")
|
assert(err == nil)
|
||||||
end
|
print("Ok")
|
||||||
local <toclose> e1 = function () error(120) end
|
end})
|
||||||
|
local e1 <close> = setmetatable({}, {__close = function () print(120) end})
|
||||||
os.exit(true, true)
|
os.exit(true, true)
|
||||||
]]
|
]]
|
||||||
RUN('lua %s > %s', prog, out)
|
RUN('lua %s > %s', prog, out)
|
||||||
checkprogout("Ok")
|
checkprogout("120\nOk\n")
|
||||||
|
|
||||||
|
|
||||||
-- remove temporary files
|
-- remove temporary files
|
||||||
@@ -339,12 +478,14 @@ assert(not os.remove(out))
|
|||||||
-- invalid options
|
-- invalid options
|
||||||
NoRun("unrecognized option '-h'", "lua -h")
|
NoRun("unrecognized option '-h'", "lua -h")
|
||||||
NoRun("unrecognized option '---'", "lua ---")
|
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 '-vv'", "lua -vv")
|
||||||
NoRun("unrecognized option '-iv'", "lua -iv")
|
NoRun("unrecognized option '-iv'", "lua -iv")
|
||||||
NoRun("'-e' needs argument", "lua -e")
|
NoRun("'-e' needs argument", "lua -e")
|
||||||
NoRun("syntax error", "lua -e a")
|
NoRun("syntax error", "lua -e a")
|
||||||
NoRun("'-l' needs argument", "lua -l")
|
NoRun("'-l' needs argument", "lua -l")
|
||||||
|
NoRun("-i", "lua -- -i") -- handles -i as a script name
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if T then -- test library?
|
if T then -- test library?
|
||||||
@@ -352,8 +493,15 @@ if T then -- test library?
|
|||||||
NoRun("not enough memory", "env MEMLIMIT=100 lua")
|
NoRun("not enough memory", "env MEMLIMIT=100 lua")
|
||||||
|
|
||||||
-- testing 'warn'
|
-- testing 'warn'
|
||||||
|
warn("@store")
|
||||||
warn("@123", "456", "789")
|
warn("@123", "456", "789")
|
||||||
assert(_WARN == "@123456789")
|
assert(_WARN == "@123456789"); _WARN = false
|
||||||
|
|
||||||
|
warn("zip", "", " ", "zap")
|
||||||
|
assert(_WARN == "zip zap"); _WARN = false
|
||||||
|
warn("ZIP", "", " ", "ZAP")
|
||||||
|
assert(_WARN == "ZIP ZAP"); _WARN = false
|
||||||
|
warn("@normal")
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
|
|||||||
+82
-66
@@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
print("testing numbers and math lib")
|
print("testing numbers and math lib")
|
||||||
|
|
||||||
local <const> minint = math.mininteger
|
local minint <const> = math.mininteger
|
||||||
local <const> maxint = math.maxinteger
|
local maxint <const> = math.maxinteger
|
||||||
|
|
||||||
local <const> intbits = math.floor(math.log(maxint, 2) + 0.5) + 1
|
local intbits <const> = math.floor(math.log(maxint, 2) + 0.5) + 1
|
||||||
assert((1 << intbits) == 0)
|
assert((1 << intbits) == 0)
|
||||||
|
|
||||||
assert(minint == 1 << (intbits - 1))
|
assert(minint == 1 << (intbits - 1))
|
||||||
@@ -39,7 +39,7 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
assert(math.type(0) == "integer" and math.type(0.0) == "float"
|
assert(math.type(0) == "integer" and math.type(0.0) == "float"
|
||||||
and math.type("10") == nil)
|
and not math.type("10"))
|
||||||
|
|
||||||
|
|
||||||
local function checkerror (msg, f, ...)
|
local function checkerror (msg, f, ...)
|
||||||
@@ -50,7 +50,7 @@ end
|
|||||||
local msgf2i = "number.* has no integer representation"
|
local msgf2i = "number.* has no integer representation"
|
||||||
|
|
||||||
-- float equality
|
-- float equality
|
||||||
function eq (a,b,limit)
|
local function eq (a,b,limit)
|
||||||
if not limit then
|
if not limit then
|
||||||
if floatbits >= 50 then limit = 1E-11
|
if floatbits >= 50 then limit = 1E-11
|
||||||
else limit = 1E-5
|
else limit = 1E-5
|
||||||
@@ -62,7 +62,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- equality with types
|
-- equality with types
|
||||||
function eqT (a,b)
|
local function eqT (a,b)
|
||||||
return a == b and math.type(a) == math.type(b)
|
return a == b and math.type(a) == math.type(b)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ end
|
|||||||
do
|
do
|
||||||
local x = -1
|
local x = -1
|
||||||
local mz = 0/x -- minus zero
|
local mz = 0/x -- minus zero
|
||||||
t = {[0] = 10, 20, 30, 40, 50}
|
local t = {[0] = 10, 20, 30, 40, 50}
|
||||||
assert(t[mz] == t[0] and t[-0] == t[0])
|
assert(t[mz] == t[0] and t[-0] == t[0])
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -270,7 +270,7 @@ else
|
|||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
local NaN = 0/0
|
local NaN <const> = 0/0
|
||||||
assert(not (NaN < 0))
|
assert(not (NaN < 0))
|
||||||
assert(not (NaN > minint))
|
assert(not (NaN > minint))
|
||||||
assert(not (NaN <= -9))
|
assert(not (NaN <= -9))
|
||||||
@@ -381,17 +381,17 @@ assert(tonumber(1/0) == 1/0)
|
|||||||
|
|
||||||
-- 'tonumber' with strings
|
-- 'tonumber' with strings
|
||||||
assert(tonumber("0") == 0)
|
assert(tonumber("0") == 0)
|
||||||
assert(tonumber("") == nil)
|
assert(not tonumber(""))
|
||||||
assert(tonumber(" ") == nil)
|
assert(not tonumber(" "))
|
||||||
assert(tonumber("-") == nil)
|
assert(not tonumber("-"))
|
||||||
assert(tonumber(" -0x ") == nil)
|
assert(not tonumber(" -0x "))
|
||||||
assert(tonumber{} == nil)
|
assert(not tonumber{})
|
||||||
assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
|
assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
|
||||||
tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
|
tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
|
||||||
tonumber'+1.' == 1)
|
tonumber'+1.' == 1)
|
||||||
assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and
|
assert(not tonumber'+ 0.01' and not tonumber'+.e1' and
|
||||||
tonumber'1e' == nil and tonumber'1.0e+' == nil and
|
not tonumber'1e' and not tonumber'1.0e+' and
|
||||||
tonumber'.' == nil)
|
not tonumber'.')
|
||||||
assert(tonumber('-012') == -010-2)
|
assert(tonumber('-012') == -010-2)
|
||||||
assert(tonumber('-1.2e2') == - - -120)
|
assert(tonumber('-1.2e2') == - - -120)
|
||||||
|
|
||||||
@@ -445,45 +445,45 @@ local function f (...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(f(tonumber('fFfa', 15)) == nil)
|
assert(not f(tonumber('fFfa', 15)))
|
||||||
assert(f(tonumber('099', 8)) == nil)
|
assert(not f(tonumber('099', 8)))
|
||||||
assert(f(tonumber('1\0', 2)) == nil)
|
assert(not f(tonumber('1\0', 2)))
|
||||||
assert(f(tonumber('', 8)) == nil)
|
assert(not f(tonumber('', 8)))
|
||||||
assert(f(tonumber(' ', 9)) == nil)
|
assert(not f(tonumber(' ', 9)))
|
||||||
assert(f(tonumber(' ', 9)) == nil)
|
assert(not f(tonumber(' ', 9)))
|
||||||
assert(f(tonumber('0xf', 10)) == nil)
|
assert(not f(tonumber('0xf', 10)))
|
||||||
|
|
||||||
assert(f(tonumber('inf')) == nil)
|
assert(not f(tonumber('inf')))
|
||||||
assert(f(tonumber(' INF ')) == nil)
|
assert(not f(tonumber(' INF ')))
|
||||||
assert(f(tonumber('Nan')) == nil)
|
assert(not f(tonumber('Nan')))
|
||||||
assert(f(tonumber('nan')) == nil)
|
assert(not f(tonumber('nan')))
|
||||||
|
|
||||||
assert(f(tonumber(' ')) == nil)
|
assert(not f(tonumber(' ')))
|
||||||
assert(f(tonumber('')) == nil)
|
assert(not f(tonumber('')))
|
||||||
assert(f(tonumber('1 a')) == nil)
|
assert(not f(tonumber('1 a')))
|
||||||
assert(f(tonumber('1 a', 2)) == nil)
|
assert(not f(tonumber('1 a', 2)))
|
||||||
assert(f(tonumber('1\0')) == nil)
|
assert(not f(tonumber('1\0')))
|
||||||
assert(f(tonumber('1 \0')) == nil)
|
assert(not f(tonumber('1 \0')))
|
||||||
assert(f(tonumber('1\0 ')) == nil)
|
assert(not f(tonumber('1\0 ')))
|
||||||
assert(f(tonumber('e1')) == nil)
|
assert(not f(tonumber('e1')))
|
||||||
assert(f(tonumber('e 1')) == nil)
|
assert(not f(tonumber('e 1')))
|
||||||
assert(f(tonumber(' 3.4.5 ')) == nil)
|
assert(not f(tonumber(' 3.4.5 ')))
|
||||||
|
|
||||||
|
|
||||||
-- testing 'tonumber' for invalid hexadecimal formats
|
-- testing 'tonumber' for invalid hexadecimal formats
|
||||||
|
|
||||||
assert(tonumber('0x') == nil)
|
assert(not tonumber('0x'))
|
||||||
assert(tonumber('x') == nil)
|
assert(not tonumber('x'))
|
||||||
assert(tonumber('x3') == nil)
|
assert(not tonumber('x3'))
|
||||||
assert(tonumber('0x3.3.3') == nil) -- two decimal points
|
assert(not tonumber('0x3.3.3')) -- two decimal points
|
||||||
assert(tonumber('00x2') == nil)
|
assert(not tonumber('00x2'))
|
||||||
assert(tonumber('0x 2') == nil)
|
assert(not tonumber('0x 2'))
|
||||||
assert(tonumber('0 x2') == nil)
|
assert(not tonumber('0 x2'))
|
||||||
assert(tonumber('23x') == nil)
|
assert(not tonumber('23x'))
|
||||||
assert(tonumber('- 0xaa') == nil)
|
assert(not tonumber('- 0xaa'))
|
||||||
assert(tonumber('-0xaaP ') == nil) -- no exponent
|
assert(not tonumber('-0xaaP ')) -- no exponent
|
||||||
assert(tonumber('0x0.51p') == nil)
|
assert(not tonumber('0x0.51p'))
|
||||||
assert(tonumber('0x5p+-2') == nil)
|
assert(not tonumber('0x5p+-2'))
|
||||||
|
|
||||||
|
|
||||||
-- testing hexadecimal numerals
|
-- testing hexadecimal numerals
|
||||||
@@ -705,19 +705,19 @@ do -- testing floor & ceil
|
|||||||
assert(eqT(math.tointeger(maxint), maxint))
|
assert(eqT(math.tointeger(maxint), maxint))
|
||||||
assert(eqT(math.tointeger(maxint .. ""), maxint))
|
assert(eqT(math.tointeger(maxint .. ""), maxint))
|
||||||
assert(eqT(math.tointeger(minint + 0.0), minint))
|
assert(eqT(math.tointeger(minint + 0.0), minint))
|
||||||
assert(math.tointeger(0.0 - minint) == nil)
|
assert(not math.tointeger(0.0 - minint))
|
||||||
assert(math.tointeger(math.pi) == nil)
|
assert(not math.tointeger(math.pi))
|
||||||
assert(math.tointeger(-math.pi) == nil)
|
assert(not math.tointeger(-math.pi))
|
||||||
assert(math.floor(math.huge) == math.huge)
|
assert(math.floor(math.huge) == math.huge)
|
||||||
assert(math.ceil(math.huge) == math.huge)
|
assert(math.ceil(math.huge) == math.huge)
|
||||||
assert(math.tointeger(math.huge) == nil)
|
assert(not math.tointeger(math.huge))
|
||||||
assert(math.floor(-math.huge) == -math.huge)
|
assert(math.floor(-math.huge) == -math.huge)
|
||||||
assert(math.ceil(-math.huge) == -math.huge)
|
assert(math.ceil(-math.huge) == -math.huge)
|
||||||
assert(math.tointeger(-math.huge) == nil)
|
assert(not math.tointeger(-math.huge))
|
||||||
assert(math.tointeger("34.0") == 34)
|
assert(math.tointeger("34.0") == 34)
|
||||||
assert(math.tointeger("34.3") == nil)
|
assert(not math.tointeger("34.3"))
|
||||||
assert(math.tointeger({}) == nil)
|
assert(not math.tointeger({}))
|
||||||
assert(math.tointeger(0/0) == nil) -- NaN
|
assert(not math.tointeger(0/0)) -- NaN
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -758,7 +758,7 @@ do -- testing max/min
|
|||||||
assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
|
assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
|
||||||
assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
|
assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
|
||||||
end
|
end
|
||||||
-- testing implicit convertions
|
-- testing implicit conversions
|
||||||
|
|
||||||
local a,b = '10', '20'
|
local a,b = '10', '20'
|
||||||
assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
|
assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
|
||||||
@@ -767,7 +767,8 @@ assert(a == '10' and b == '20')
|
|||||||
|
|
||||||
do
|
do
|
||||||
print("testing -0 and NaN")
|
print("testing -0 and NaN")
|
||||||
local mz, z = -0.0, 0.0
|
local mz <const> = -0.0
|
||||||
|
local z <const> = 0.0
|
||||||
assert(mz == z)
|
assert(mz == z)
|
||||||
assert(1/mz < 0 and 0 < 1/z)
|
assert(1/mz < 0 and 0 < 1/z)
|
||||||
local a = {[mz] = 1}
|
local a = {[mz] = 1}
|
||||||
@@ -775,17 +776,18 @@ do
|
|||||||
a[z] = 2
|
a[z] = 2
|
||||||
assert(a[z] == 2 and a[mz] == 2)
|
assert(a[z] == 2 and a[mz] == 2)
|
||||||
local inf = math.huge * 2 + 1
|
local inf = math.huge * 2 + 1
|
||||||
mz, z = -1/inf, 1/inf
|
local mz <const> = -1/inf
|
||||||
|
local z <const> = 1/inf
|
||||||
assert(mz == z)
|
assert(mz == z)
|
||||||
assert(1/mz < 0 and 0 < 1/z)
|
assert(1/mz < 0 and 0 < 1/z)
|
||||||
local NaN = inf - inf
|
local NaN <const> = inf - inf
|
||||||
assert(NaN ~= NaN)
|
assert(NaN ~= NaN)
|
||||||
assert(not (NaN < NaN))
|
assert(not (NaN < NaN))
|
||||||
assert(not (NaN <= NaN))
|
assert(not (NaN <= NaN))
|
||||||
assert(not (NaN > NaN))
|
assert(not (NaN > NaN))
|
||||||
assert(not (NaN >= NaN))
|
assert(not (NaN >= NaN))
|
||||||
assert(not (0 < NaN) and not (NaN < 0))
|
assert(not (0 < NaN) and not (NaN < 0))
|
||||||
local NaN1 = 0/0
|
local NaN1 <const> = 0/0
|
||||||
assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
|
assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
|
||||||
local a = {}
|
local a = {}
|
||||||
assert(not pcall(rawset, a, NaN, 1))
|
assert(not pcall(rawset, a, NaN, 1))
|
||||||
@@ -813,9 +815,9 @@ end
|
|||||||
-- low-level!! For the current implementation of random in Lua,
|
-- low-level!! For the current implementation of random in Lua,
|
||||||
-- the first call after seed 1007 should return 0x7a7040a5a323c9d6
|
-- the first call after seed 1007 should return 0x7a7040a5a323c9d6
|
||||||
do
|
do
|
||||||
-- all computations assume at most 32-bit integers
|
-- all computations should work with 32-bit integers
|
||||||
local h = 0x7a7040a5 -- higher half
|
local h <const> = 0x7a7040a5 -- higher half
|
||||||
local l = 0xa323c9d6 -- lower half
|
local l <const> = 0xa323c9d6 -- lower half
|
||||||
|
|
||||||
math.randomseed(1007)
|
math.randomseed(1007)
|
||||||
-- get the low 'intbits' of the 64-bit expected result
|
-- get the low 'intbits' of the 64-bit expected result
|
||||||
@@ -838,7 +840,17 @@ do
|
|||||||
assert(rand * 2^floatbits == res)
|
assert(rand * 2^floatbits == res)
|
||||||
end
|
end
|
||||||
|
|
||||||
math.randomseed()
|
do
|
||||||
|
-- testing return of 'randomseed'
|
||||||
|
local x, y = math.randomseed()
|
||||||
|
local res = math.random(0)
|
||||||
|
x, y = math.randomseed(x, y) -- should repeat the state
|
||||||
|
assert(math.random(0) == res)
|
||||||
|
math.randomseed(x, y) -- again should repeat the state
|
||||||
|
assert(math.random(0) == res)
|
||||||
|
-- keep the random seed for following tests
|
||||||
|
print(string.format("random seeds: %d, %d", x, y))
|
||||||
|
end
|
||||||
|
|
||||||
do -- test random for floats
|
do -- test random for floats
|
||||||
local randbits = math.min(floatbits, 64) -- at most 64 random bits
|
local randbits = math.min(floatbits, 64) -- at most 64 random bits
|
||||||
@@ -949,7 +961,10 @@ do
|
|||||||
aux(-10,0)
|
aux(-10,0)
|
||||||
aux(1, 6)
|
aux(1, 6)
|
||||||
aux(1, 2)
|
aux(1, 2)
|
||||||
|
aux(1, 13)
|
||||||
|
aux(1, 31)
|
||||||
aux(1, 32)
|
aux(1, 32)
|
||||||
|
aux(1, 33)
|
||||||
aux(-10, 10)
|
aux(-10, 10)
|
||||||
aux(-10,-10) -- unit set
|
aux(-10,-10) -- unit set
|
||||||
aux(minint, minint) -- unit set
|
aux(minint, minint) -- unit set
|
||||||
@@ -987,6 +1002,7 @@ do
|
|||||||
end
|
end
|
||||||
aux(0, maxint)
|
aux(0, maxint)
|
||||||
aux(1, maxint)
|
aux(1, maxint)
|
||||||
|
aux(3, maxint // 3)
|
||||||
aux(minint, -1)
|
aux(minint, -1)
|
||||||
aux(minint // 2, maxint // 2)
|
aux(minint // 2, maxint // 2)
|
||||||
aux(minint, maxint)
|
aux(minint, maxint)
|
||||||
|
|||||||
+137
-50
@@ -9,6 +9,16 @@ local function checkerror (msg, f, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function check (t, na, nh)
|
||||||
|
if not T then return end
|
||||||
|
local a, h = T.querytab(t)
|
||||||
|
if a ~= na or h ~= nh then
|
||||||
|
print(na, nh, a, h)
|
||||||
|
assert(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local a = {}
|
local a = {}
|
||||||
|
|
||||||
-- make sure table has lots of space in hash part
|
-- make sure table has lots of space in hash part
|
||||||
@@ -20,6 +30,25 @@ for i=1,100 do
|
|||||||
assert(#a == i)
|
assert(#a == i)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- rehash moving elements from array to hash
|
||||||
|
local a = {}
|
||||||
|
for i = 1, 100 do a[i] = i end
|
||||||
|
check(a, 128, 0)
|
||||||
|
|
||||||
|
for i = 5, 95 do a[i] = nil end
|
||||||
|
check(a, 128, 0)
|
||||||
|
|
||||||
|
a.x = 1 -- force a re-hash
|
||||||
|
check(a, 4, 8)
|
||||||
|
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- testing ipairs
|
-- testing ipairs
|
||||||
local x = 0
|
local x = 0
|
||||||
for k,v in ipairs{10,20,30;x=12} do
|
for k,v in ipairs{10,20,30;x=12} do
|
||||||
@@ -43,48 +72,27 @@ assert(i == 4)
|
|||||||
assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
|
assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
|
||||||
|
|
||||||
|
|
||||||
|
do -- overflow (must wrap-around)
|
||||||
|
local f = ipairs{}
|
||||||
|
local k, v = f({[math.mininteger] = 10}, math.maxinteger)
|
||||||
|
assert(k == math.mininteger and v == 10)
|
||||||
|
k, v = f({[math.mininteger] = 10}, k)
|
||||||
|
assert(k == nil)
|
||||||
|
end
|
||||||
|
|
||||||
if not T then
|
if not T then
|
||||||
(Message or print)
|
(Message or print)
|
||||||
('\n >>> testC not active: skipping tests for table sizes <<<\n')
|
('\n >>> testC not active: skipping tests for table sizes <<<\n')
|
||||||
else --[
|
else --[
|
||||||
-- testing table sizes
|
-- testing table sizes
|
||||||
|
|
||||||
local function log2 (x) return math.log(x, 2) end
|
|
||||||
|
|
||||||
local function mp2 (n) -- minimum power of 2 >= n
|
local function mp2 (n) -- minimum power of 2 >= n
|
||||||
local mp = 2^math.ceil(log2(n))
|
local mp = 2^math.ceil(math.log(n, 2))
|
||||||
assert(n == 0 or (mp/2 < n and n <= mp))
|
assert(n == 0 or (mp/2 < n and n <= mp))
|
||||||
return mp
|
return mp
|
||||||
end
|
end
|
||||||
|
|
||||||
local function fb (n)
|
|
||||||
local r, nn = T.int2fb(n)
|
|
||||||
assert(r < 256)
|
|
||||||
return nn
|
|
||||||
end
|
|
||||||
|
|
||||||
-- test fb function
|
|
||||||
for a = 1, 10000 do -- all numbers up to 10^4
|
|
||||||
local n = fb(a)
|
|
||||||
assert(a <= n and n <= a*1.125)
|
|
||||||
end
|
|
||||||
local a = 1024 -- plus a few up to 2 ^30
|
|
||||||
local lim = 2^30
|
|
||||||
while a < lim do
|
|
||||||
local n = fb(a)
|
|
||||||
assert(a <= n and n <= a*1.125)
|
|
||||||
a = math.ceil(a*1.3)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function check (t, na, nh)
|
|
||||||
local a, h = T.querytab(t)
|
|
||||||
if a ~= na or h ~= nh then
|
|
||||||
print(na, nh, a, h)
|
|
||||||
assert(nil)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- testing C library sizes
|
-- testing C library sizes
|
||||||
do
|
do
|
||||||
@@ -95,30 +103,46 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- testing constructor sizes
|
-- testing constructor sizes
|
||||||
local lim = 40
|
local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17,
|
||||||
local s = 'return {'
|
30, 31, 32, 33, 34, 254, 255, 256, 500, 1000}
|
||||||
for i=1,lim do
|
|
||||||
s = s..i..','
|
for _, sa in ipairs(sizes) do -- 'sa' is size of the array part
|
||||||
local s = s
|
local arr = {"return {"}
|
||||||
for k=0,lim do
|
for i = 1, sa do arr[1 + i] = "1," end -- build array part
|
||||||
local t = load(s..'}', '')()
|
for _, sh in ipairs(sizes) do -- 'sh' is size of the hash part
|
||||||
assert(#t == i)
|
for j = 1, sh do -- build hash part
|
||||||
check(t, fb(i), mp2(k))
|
arr[1 + sa + j] = string.format('k%x=%d,', j, j)
|
||||||
s = string.format('%sa%d=%d,', s, k, k)
|
end
|
||||||
|
arr[1 + sa + sh + 1] = "}"
|
||||||
|
local prog = table.concat(arr)
|
||||||
|
local f = assert(load(prog))
|
||||||
|
collectgarbage("stop")
|
||||||
|
f() -- call once to ensure stack space
|
||||||
|
-- make sure table is not resized after being created
|
||||||
|
if sa == 0 or sh == 0 then
|
||||||
|
T.alloccount(2); -- header + array or hash part
|
||||||
|
else
|
||||||
|
T.alloccount(3); -- header + array part + hash part
|
||||||
|
end
|
||||||
|
local t = f()
|
||||||
|
T.alloccount();
|
||||||
|
collectgarbage("restart")
|
||||||
|
assert(#t == sa)
|
||||||
|
check(t, sa, mp2(sh))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- tests with unknown number of elements
|
-- tests with unknown number of elements
|
||||||
local a = {}
|
local a = {}
|
||||||
for i=1,lim do a[i] = i end -- build auxiliary table
|
for i=1,sizes[#sizes] do a[i] = i end -- build auxiliary table
|
||||||
for k=0,lim do
|
for k in ipairs(sizes) do
|
||||||
local a = {table.unpack(a,1,k)}
|
local t = {table.unpack(a,1,k)}
|
||||||
assert(#a == k)
|
assert(#t == k)
|
||||||
check(a, k, 0)
|
check(t, k, 0)
|
||||||
a = {1,2,3,table.unpack(a,1,k)}
|
t = {1,2,3,table.unpack(a,1,k)}
|
||||||
check(a, k+3, 0)
|
check(t, k+3, 0)
|
||||||
assert(#a == k + 3)
|
assert(#t == k + 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -165,7 +189,7 @@ end
|
|||||||
|
|
||||||
-- size tests for vararg
|
-- size tests for vararg
|
||||||
lim = 35
|
lim = 35
|
||||||
function foo (n, ...)
|
local function foo (n, ...)
|
||||||
local arg = {...}
|
local arg = {...}
|
||||||
check(arg, n, 0)
|
check(arg, n, 0)
|
||||||
assert(select('#', ...) == n)
|
assert(select('#', ...) == n)
|
||||||
@@ -363,6 +387,38 @@ end
|
|||||||
assert(n == 5)
|
assert(n == 5)
|
||||||
|
|
||||||
|
|
||||||
|
do
|
||||||
|
print("testing next x GC of deleted keys")
|
||||||
|
-- bug in 5.4.1
|
||||||
|
local co = coroutine.wrap(function (t)
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
local k1 = next(t) -- all previous keys were deleted
|
||||||
|
assert(k == k1) -- current key is the first in the table
|
||||||
|
t[k] = nil
|
||||||
|
local expected = (type(k) == "table" and k[1] or
|
||||||
|
type(k) == "function" and k() or
|
||||||
|
string.sub(k, 1, 1))
|
||||||
|
assert(expected == v)
|
||||||
|
coroutine.yield(v)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
local t = {}
|
||||||
|
t[{1}] = 1 -- add several unanchored, collectable keys
|
||||||
|
t[{2}] = 2
|
||||||
|
t[string.rep("a", 50)] = "a" -- long string
|
||||||
|
t[string.rep("b", 50)] = "b"
|
||||||
|
t[{3}] = 3
|
||||||
|
t[string.rep("c", 10)] = "c" -- short string
|
||||||
|
t[function () return 10 end] = 10
|
||||||
|
local count = 7
|
||||||
|
while co(t) do
|
||||||
|
collectgarbage("collect") -- collect dead keys
|
||||||
|
count = count - 1
|
||||||
|
end
|
||||||
|
assert(count == 0 and next(t) == nil) -- traversed the whole table
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local function test (a)
|
local function test (a)
|
||||||
assert(not pcall(table.insert, a, 2, 20));
|
assert(not pcall(table.insert, a, 2, 20));
|
||||||
table.insert(a, 10); table.insert(a, 2, 20);
|
table.insert(a, 10); table.insert(a, 2, 20);
|
||||||
@@ -471,6 +527,15 @@ do -- testing table library with metamethods
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
do -- testing overflow in table.insert (must wrap-around)
|
||||||
|
|
||||||
|
local t = setmetatable({},
|
||||||
|
{__len = function () return math.maxinteger end})
|
||||||
|
table.insert(t, 20)
|
||||||
|
local k, v = next(t)
|
||||||
|
assert(k == math.mininteger and v == 20)
|
||||||
|
end
|
||||||
|
|
||||||
if not T then
|
if not T then
|
||||||
(Message or print)
|
(Message or print)
|
||||||
('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
|
('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
|
||||||
@@ -677,7 +742,8 @@ collectgarbage()
|
|||||||
|
|
||||||
local function f (n, p)
|
local function f (n, p)
|
||||||
local t = {}; for i=1,p do t[i] = i*10 end
|
local t = {}; for i=1,p do t[i] = i*10 end
|
||||||
return function (_,n)
|
return function (_, n, ...)
|
||||||
|
assert(select("#", ...) == 0) -- no extra arguments
|
||||||
if n > 0 then
|
if n > 0 then
|
||||||
n = n-1
|
n = n-1
|
||||||
return n, table.unpack(t)
|
return n, table.unpack(t)
|
||||||
@@ -735,4 +801,25 @@ for k,v in ipairs(a) do
|
|||||||
end
|
end
|
||||||
assert(i == a.n)
|
assert(i == a.n)
|
||||||
|
|
||||||
|
|
||||||
|
-- testing yield inside __pairs
|
||||||
|
do
|
||||||
|
local t = setmetatable({10, 20, 30}, {__pairs = function (t)
|
||||||
|
local inc = coroutine.yield()
|
||||||
|
return function (t, i)
|
||||||
|
if i > 1 then return i - inc, t[i - inc] else return nil end
|
||||||
|
end, t, #t + 1
|
||||||
|
end})
|
||||||
|
|
||||||
|
local res = {}
|
||||||
|
local co = coroutine.wrap(function ()
|
||||||
|
for i,p in pairs(t) do res[#res + 1] = p end
|
||||||
|
end)
|
||||||
|
|
||||||
|
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"
|
print"OK"
|
||||||
|
|||||||
Executable
+55
@@ -0,0 +1,55 @@
|
|||||||
|
NAME=$1"-tests"
|
||||||
|
|
||||||
|
ln -s . $NAME
|
||||||
|
ln -s .. ltests
|
||||||
|
|
||||||
|
tar --create --gzip --no-recursion --file=$NAME.tar.gz \
|
||||||
|
$NAME/all.lua \
|
||||||
|
$NAME/api.lua \
|
||||||
|
$NAME/attrib.lua \
|
||||||
|
$NAME/big.lua \
|
||||||
|
$NAME/bitwise.lua \
|
||||||
|
$NAME/bwcoercion.lua \
|
||||||
|
$NAME/calls.lua \
|
||||||
|
$NAME/closure.lua \
|
||||||
|
$NAME/code.lua \
|
||||||
|
$NAME/constructs.lua \
|
||||||
|
$NAME/coroutine.lua \
|
||||||
|
$NAME/cstack.lua \
|
||||||
|
$NAME/db.lua \
|
||||||
|
$NAME/errors.lua \
|
||||||
|
$NAME/events.lua \
|
||||||
|
$NAME/files.lua \
|
||||||
|
$NAME/gc.lua \
|
||||||
|
$NAME/gengc.lua \
|
||||||
|
$NAME/goto.lua \
|
||||||
|
$NAME/heavy.lua \
|
||||||
|
$NAME/literals.lua \
|
||||||
|
$NAME/locals.lua \
|
||||||
|
$NAME/main.lua \
|
||||||
|
$NAME/math.lua \
|
||||||
|
$NAME/nextvar.lua \
|
||||||
|
$NAME/pm.lua \
|
||||||
|
$NAME/sort.lua \
|
||||||
|
$NAME/strings.lua \
|
||||||
|
$NAME/tpack.lua \
|
||||||
|
$NAME/tracegc.lua \
|
||||||
|
$NAME/utf8.lua \
|
||||||
|
$NAME/vararg.lua \
|
||||||
|
$NAME/verybig.lua \
|
||||||
|
$NAME/libs/makefile \
|
||||||
|
$NAME/libs/P1 \
|
||||||
|
$NAME/libs/lib1.c \
|
||||||
|
$NAME/libs/lib11.c \
|
||||||
|
$NAME/libs/lib2.c \
|
||||||
|
$NAME/libs/lib21.c \
|
||||||
|
$NAME/libs/lib22.c \
|
||||||
|
$NAME/ltests/ltests.h \
|
||||||
|
$NAME/ltests/ltests.c
|
||||||
|
|
||||||
|
\rm -I $NAME
|
||||||
|
\rm -I ltests
|
||||||
|
|
||||||
|
echo ${NAME}.tar.gz" created"
|
||||||
|
|
||||||
|
|
||||||
+63
-44
@@ -1,6 +1,9 @@
|
|||||||
-- $Id: testes/pm.lua $
|
-- $Id: testes/pm.lua $
|
||||||
-- See Copyright Notice in file all.lua
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
|
-- UTF-8 file
|
||||||
|
|
||||||
|
|
||||||
print('testing pattern matching')
|
print('testing pattern matching')
|
||||||
|
|
||||||
local function checkerror (msg, f, ...)
|
local function checkerror (msg, f, ...)
|
||||||
@@ -9,12 +12,12 @@ local function checkerror (msg, f, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function f(s, p)
|
local function f (s, p)
|
||||||
local i,e = string.find(s, p)
|
local i,e = string.find(s, p)
|
||||||
if i then return string.sub(s, i, e) end
|
if i then return string.sub(s, i, e) end
|
||||||
end
|
end
|
||||||
|
|
||||||
a,b = string.find('', '') -- empty patterns are tricky
|
local a,b = string.find('', '') -- empty patterns are tricky
|
||||||
assert(a == 1 and b == 0);
|
assert(a == 1 and b == 0);
|
||||||
a,b = string.find('alo', '')
|
a,b = string.find('alo', '')
|
||||||
assert(a == 1 and b == 0)
|
assert(a == 1 and b == 0)
|
||||||
@@ -28,10 +31,10 @@ a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end
|
|||||||
assert(a == 9 and b == 11);
|
assert(a == 9 and b == 11);
|
||||||
a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position
|
a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position
|
||||||
assert(a == 11 and b == 11)
|
assert(a == 11 and b == 11)
|
||||||
assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending
|
assert(not string.find('a\0a\0a\0a\0\0ab', 'b\0')) -- check ending
|
||||||
assert(string.find('', '\0') == nil)
|
assert(not string.find('', '\0'))
|
||||||
assert(string.find('alo123alo', '12') == 4)
|
assert(string.find('alo123alo', '12') == 4)
|
||||||
assert(string.find('alo123alo', '^12') == nil)
|
assert(not string.find('alo123alo', '^12'))
|
||||||
|
|
||||||
assert(string.match("aaab", ".*b") == "aaab")
|
assert(string.match("aaab", ".*b") == "aaab")
|
||||||
assert(string.match("aaa", ".*a") == "aaa")
|
assert(string.match("aaa", ".*a") == "aaa")
|
||||||
@@ -50,6 +53,20 @@ assert(f('aLo_ALO', '%a*') == 'aLo')
|
|||||||
|
|
||||||
assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu")
|
assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu")
|
||||||
|
|
||||||
|
|
||||||
|
-- Adapt a pattern to UTF-8
|
||||||
|
local function PU (p)
|
||||||
|
-- reapply '?' 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('aaab', 'a*') == 'aaa');
|
||||||
assert(f('aaa', '^.*$') == 'aaa');
|
assert(f('aaa', '^.*$') == 'aaa');
|
||||||
assert(f('aaa', 'b*') == '');
|
assert(f('aaa', 'b*') == '');
|
||||||
@@ -57,38 +74,38 @@ assert(f('aaa', 'ab*a') == 'aa')
|
|||||||
assert(f('aba', 'ab*a') == 'aba')
|
assert(f('aba', 'ab*a') == 'aba')
|
||||||
assert(f('aaab', 'a+') == 'aaa')
|
assert(f('aaab', 'a+') == 'aaa')
|
||||||
assert(f('aaa', '^.+$') == 'aaa')
|
assert(f('aaa', '^.+$') == 'aaa')
|
||||||
assert(f('aaa', 'b+') == nil)
|
assert(not f('aaa', 'b+'))
|
||||||
assert(f('aaa', 'ab+a') == nil)
|
assert(not f('aaa', 'ab+a'))
|
||||||
assert(f('aba', 'ab+a') == 'aba')
|
assert(f('aba', 'ab+a') == 'aba')
|
||||||
assert(f('a$a', '.$') == 'a')
|
assert(f('a$a', '.$') == 'a')
|
||||||
assert(f('a$a', '.%$') == 'a$')
|
assert(f('a$a', '.%$') == 'a$')
|
||||||
assert(f('a$a', '.$.') == 'a$a')
|
assert(f('a$a', '.$.') == 'a$a')
|
||||||
assert(f('a$a', '$$') == nil)
|
assert(not f('a$a', '$$'))
|
||||||
assert(f('a$b', 'a$') == nil)
|
assert(not f('a$b', 'a$'))
|
||||||
assert(f('a$a', '$') == '')
|
assert(f('a$a', '$') == '')
|
||||||
assert(f('', 'b*') == '')
|
assert(f('', 'b*') == '')
|
||||||
assert(f('aaa', 'bb*') == nil)
|
assert(not f('aaa', 'bb*'))
|
||||||
assert(f('aaab', 'a-') == '')
|
assert(f('aaab', 'a-') == '')
|
||||||
assert(f('aaa', '^.-$') == 'aaa')
|
assert(f('aaa', '^.-$') == 'aaa')
|
||||||
assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
|
assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
|
||||||
assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab')
|
assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab')
|
||||||
assert(f('alo xo', '.o$') == 'xo')
|
assert(f('alo xo', '.o$') == 'xo')
|
||||||
assert(f(' \n isto é assim', '%S%S*') == 'isto')
|
assert(f(' \n isto é assim', '%S%S*') == 'isto')
|
||||||
assert(f(' \n isto é assim', '%S*$') == 'assim')
|
assert(f(' \n isto é assim', '%S*$') == 'assim')
|
||||||
assert(f(' \n isto é assim', '[a-z]*$') == 'assim')
|
assert(f(' \n isto é assim', '[a-z]*$') == 'assim')
|
||||||
assert(f('um caracter ? extra', '[^%sa-z]') == '?')
|
assert(f('um caracter ? extra', '[^%sa-z]') == '?')
|
||||||
assert(f('', 'a?') == '')
|
assert(f('', 'a?') == '')
|
||||||
assert(f('á', 'á?') == 'á')
|
assert(f('á', PU'á?') == 'á')
|
||||||
assert(f('ábl', 'á?b?l?') == 'ábl')
|
assert(f('ábl', PU'á?b?l?') == 'ábl')
|
||||||
assert(f(' ábl', 'á?b?l?') == '')
|
assert(f(' ábl', PU'á?b?l?') == '')
|
||||||
assert(f('aa', '^aa?a?a') == 'aa')
|
assert(f('aa', '^aa?a?a') == 'aa')
|
||||||
assert(f(']]]áb', '[^]]') == 'á')
|
assert(f(']]]áb', '[^]]+') == 'áb')
|
||||||
assert(f("0alo alo", "%x*") == "0a")
|
assert(f("0alo alo", "%x*") == "0a")
|
||||||
assert(f("alo alo", "%C+") == "alo alo")
|
assert(f("alo alo", "%C+") == "alo alo")
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
function f1(s, p)
|
local function f1 (s, p)
|
||||||
p = string.gsub(p, "%%([0-9])", function (s)
|
p = string.gsub(p, "%%([0-9])", function (s)
|
||||||
return "%" .. (tonumber(s)+1)
|
return "%" .. (tonumber(s)+1)
|
||||||
end)
|
end)
|
||||||
@@ -101,7 +118,7 @@ end
|
|||||||
assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o")
|
assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o")
|
||||||
assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3')
|
assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3')
|
||||||
assert(f1('=======', '^(=*)=%1$') == '=======')
|
assert(f1('=======', '^(=*)=%1$') == '=======')
|
||||||
assert(string.match('==========', '^([=]*)=%1$') == nil)
|
assert(not string.match('==========', '^([=]*)=%1$'))
|
||||||
|
|
||||||
local function range (i, j)
|
local function range (i, j)
|
||||||
if i <= j then
|
if i <= j then
|
||||||
@@ -113,7 +130,7 @@ local abc = string.char(range(0, 127)) .. string.char(range(128, 255));
|
|||||||
|
|
||||||
assert(string.len(abc) == 256)
|
assert(string.len(abc) == 256)
|
||||||
|
|
||||||
function strset (p)
|
local function strset (p)
|
||||||
local res = {s=''}
|
local res = {s=''}
|
||||||
string.gsub(abc, p, function (c) res.s = res.s .. c end)
|
string.gsub(abc, p, function (c) res.s = res.s .. c end)
|
||||||
return res.s
|
return res.s
|
||||||
@@ -135,29 +152,29 @@ print('+');
|
|||||||
assert(string.match("alo xyzK", "(%w+)K") == "xyz")
|
assert(string.match("alo xyzK", "(%w+)K") == "xyz")
|
||||||
assert(string.match("254 K", "(%d*)K") == "")
|
assert(string.match("254 K", "(%d*)K") == "")
|
||||||
assert(string.match("alo ", "(%w*)$") == "")
|
assert(string.match("alo ", "(%w*)$") == "")
|
||||||
assert(string.match("alo ", "(%w+)$") == nil)
|
assert(not string.match("alo ", "(%w+)$"))
|
||||||
assert(string.find("(álo)", "%(á") == 1)
|
assert(string.find("(álo)", "%(á") == 1)
|
||||||
local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$")
|
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)
|
assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil)
|
||||||
a, b, c, d = string.match('0123456789', '(.+(.?)())')
|
a, b, c, d = string.match('0123456789', '(.+(.?)())')
|
||||||
assert(a == '0123456789' and b == '' and c == 11 and d == nil)
|
assert(a == '0123456789' and b == '' and c == 11 and d == nil)
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
|
assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo')
|
||||||
assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
|
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 ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim
|
||||||
assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
|
assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
|
||||||
t = "abç d"
|
local t = "abç d"
|
||||||
a, b = string.gsub(t, '(.)', '%1@')
|
a, b = string.gsub(t, PU'(.)', '%1@')
|
||||||
assert('@'..a == string.gsub(t, '', '@') and b == 5)
|
assert(a == "a@b@ç@ @d@" and b == 5)
|
||||||
a, b = string.gsub('abçd', '(.)', '%0@', 2)
|
a, b = string.gsub('abçd', PU'(.)', '%0@', 2)
|
||||||
assert(a == 'a@b@çd' and b == 2)
|
assert(a == 'a@b@çd' and b == 2)
|
||||||
assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o')
|
assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o')
|
||||||
assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") ==
|
assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") ==
|
||||||
"xyz=abc-abc=xyz")
|
"xyz=abc-abc=xyz")
|
||||||
assert(string.gsub("abc", "%w", "%1%0") == "aabbcc")
|
assert(string.gsub("abc", "%w", "%1%0") == "aabbcc")
|
||||||
assert(string.gsub("abc", "%w+", "%0%1") == "abcabc")
|
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')
|
||||||
assert(string.gsub('', '$', 'r') == 'r')
|
assert(string.gsub('', '$', 'r') == 'r')
|
||||||
print('+')
|
print('+')
|
||||||
@@ -184,32 +201,34 @@ do
|
|||||||
local function setglobal (n,v) rawset(_G, n, v) end
|
local function setglobal (n,v) rawset(_G, n, v) end
|
||||||
string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal)
|
string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal)
|
||||||
assert(_G.a=="roberto" and _G.roberto=="a")
|
assert(_G.a=="roberto" and _G.roberto=="a")
|
||||||
|
_G.a = nil; _G.roberto = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function f(a,b) return string.gsub(a,'.',b) end
|
function f(a,b) return string.gsub(a,'.',b) end
|
||||||
assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
|
assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) ==
|
||||||
"trocar tudo em bbbbb é alalalalalal")
|
"trocar tudo em bbbbb é alalalalalal")
|
||||||
|
|
||||||
local function dostring (s) return load(s, "")() or "" end
|
local function dostring (s) return load(s, "")() or "" end
|
||||||
assert(string.gsub("alo $a='x'$ novamente $return a$",
|
assert(string.gsub("alo $a='x'$ novamente $return a$",
|
||||||
"$([^$]*)%$",
|
"$([^$]*)%$",
|
||||||
dostring) == "alo novamente x")
|
dostring) == "alo novamente x")
|
||||||
|
|
||||||
x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$",
|
local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$",
|
||||||
"$([^$]*)%$", dostring)
|
"$([^$]*)%$", dostring)
|
||||||
assert(x == ' assim vai para ALO')
|
assert(x == ' assim vai para ALO')
|
||||||
|
_G.a, _G.x = nil
|
||||||
|
|
||||||
t = {}
|
local t = {}
|
||||||
s = 'a alo jose joao'
|
local s = 'a alo jose joao'
|
||||||
r = string.gsub(s, '()(%w+)()', function (a,w,b)
|
local r = string.gsub(s, '()(%w+)()', function (a,w,b)
|
||||||
assert(string.len(w) == b-a);
|
assert(string.len(w) == b-a);
|
||||||
t[a] = b-a;
|
t[a] = b-a;
|
||||||
end)
|
end)
|
||||||
assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
|
assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
|
||||||
|
|
||||||
|
|
||||||
function isbalanced (s)
|
local function isbalanced (s)
|
||||||
return string.find(string.gsub(s, "%b()", ""), "[()]") == nil
|
return not string.find(string.gsub(s, "%b()", ""), "[()]")
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a"))
|
assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a"))
|
||||||
@@ -251,7 +270,7 @@ if not _soft then
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- recursive nest of gsubs
|
-- recursive nest of gsubs
|
||||||
function rev (s)
|
local function rev (s)
|
||||||
return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end)
|
return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+12
-11
@@ -20,7 +20,7 @@ end
|
|||||||
checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
|
checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
|
||||||
|
|
||||||
local x,y,z,a,n
|
local x,y,z,a,n
|
||||||
a = {}; lim = _soft and 200 or 2000
|
a = {}; local lim = _soft and 200 or 2000
|
||||||
for i=1, lim do a[i]=i end
|
for i=1, lim do a[i]=i end
|
||||||
assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
|
assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
|
||||||
x = unpack(a)
|
x = unpack(a)
|
||||||
@@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
|
|||||||
table.sort(a)
|
table.sort(a)
|
||||||
check(a)
|
check(a)
|
||||||
|
|
||||||
function perm (s, n)
|
local function perm (s, n)
|
||||||
n = n or #s
|
n = n or #s
|
||||||
if n == 1 then
|
if n == 1 then
|
||||||
local t = {unpack(s)}
|
local t = {unpack(s)}
|
||||||
@@ -248,7 +248,7 @@ perm{1,2,3,3,5}
|
|||||||
perm{1,2,3,4,5,6}
|
perm{1,2,3,4,5,6}
|
||||||
perm{2,2,3,3,5,6}
|
perm{2,2,3,3,5,6}
|
||||||
|
|
||||||
function timesort (a, n, func, msg, pre)
|
local function timesort (a, n, func, msg, pre)
|
||||||
local x = os.clock()
|
local x = os.clock()
|
||||||
table.sort(a, func)
|
table.sort(a, func)
|
||||||
x = (os.clock() - x) * 1000
|
x = (os.clock() - x) * 1000
|
||||||
@@ -257,7 +257,7 @@ function timesort (a, n, func, msg, pre)
|
|||||||
check(a, func)
|
check(a, func)
|
||||||
end
|
end
|
||||||
|
|
||||||
limit = 50000
|
local limit = 50000
|
||||||
if _soft then limit = 5000 end
|
if _soft then limit = 5000 end
|
||||||
|
|
||||||
a = {}
|
a = {}
|
||||||
@@ -274,7 +274,7 @@ for i=1,limit do
|
|||||||
a[i] = math.random()
|
a[i] = math.random()
|
||||||
end
|
end
|
||||||
|
|
||||||
x = os.clock(); i=0
|
local x = os.clock(); local i = 0
|
||||||
table.sort(a, function(x,y) i=i+1; return y<x end)
|
table.sort(a, function(x,y) i=i+1; return y<x end)
|
||||||
x = (os.clock() - x) * 1000
|
x = (os.clock() - x) * 1000
|
||||||
print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons",
|
print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons",
|
||||||
@@ -289,18 +289,19 @@ timesort(a, limit, function(x,y) return nil end, "equal")
|
|||||||
|
|
||||||
for i,v in pairs(a) do assert(v == false) end
|
for i,v in pairs(a) do assert(v == false) end
|
||||||
|
|
||||||
A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
|
AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"}
|
||||||
table.sort(A)
|
table.sort(AA)
|
||||||
check(A)
|
check(AA)
|
||||||
|
|
||||||
table.sort(A, function (x, y)
|
table.sort(AA, function (x, y)
|
||||||
load(string.format("A[%q] = ''", x), "")()
|
load(string.format("AA[%q] = ''", x), "")()
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
return x<y
|
return x<y
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
_G.AA = nil
|
||||||
|
|
||||||
tt = {__lt = function (a,b) return a.val < b.val end}
|
local tt = {__lt = function (a,b) return a.val < b.val end}
|
||||||
a = {}
|
a = {}
|
||||||
for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
|
for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
|
||||||
table.sort(a)
|
table.sort(a)
|
||||||
|
|||||||
+84
-26
@@ -1,10 +1,13 @@
|
|||||||
-- $Id: testes/strings.lua $
|
-- $Id: testes/strings.lua $
|
||||||
-- See Copyright Notice in file all.lua
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
|
-- ISO Latin encoding
|
||||||
|
|
||||||
|
|
||||||
print('testing strings and string library')
|
print('testing strings and string library')
|
||||||
|
|
||||||
local <const> maxi = math.maxinteger
|
local maxi <const> = math.maxinteger
|
||||||
local <const> mini = math.mininteger
|
local mini <const> = math.mininteger
|
||||||
|
|
||||||
|
|
||||||
local function checkerror (msg, f, ...)
|
local function checkerror (msg, f, ...)
|
||||||
@@ -52,17 +55,17 @@ assert(("\000123456789"):sub(8) == "789")
|
|||||||
|
|
||||||
-- testing string.find
|
-- testing string.find
|
||||||
assert(string.find("123456789", "345") == 3)
|
assert(string.find("123456789", "345") == 3)
|
||||||
a,b = string.find("123456789", "345")
|
local a,b = string.find("123456789", "345")
|
||||||
assert(string.sub("123456789", a, b) == "345")
|
assert(string.sub("123456789", a, b) == "345")
|
||||||
assert(string.find("1234567890123456789", "345", 3) == 3)
|
assert(string.find("1234567890123456789", "345", 3) == 3)
|
||||||
assert(string.find("1234567890123456789", "345", 4) == 13)
|
assert(string.find("1234567890123456789", "345", 4) == 13)
|
||||||
assert(string.find("1234567890123456789", "346", 4) == nil)
|
assert(not string.find("1234567890123456789", "346", 4))
|
||||||
assert(string.find("1234567890123456789", ".45", -9) == 13)
|
assert(string.find("1234567890123456789", ".45", -9) == 13)
|
||||||
assert(string.find("abcdefg", "\0", 5, 1) == nil)
|
assert(not string.find("abcdefg", "\0", 5, 1))
|
||||||
assert(string.find("", "") == 1)
|
assert(string.find("", "") == 1)
|
||||||
assert(string.find("", "", 1) == 1)
|
assert(string.find("", "", 1) == 1)
|
||||||
assert(not string.find("", "", 2))
|
assert(not string.find("", "", 2))
|
||||||
assert(string.find('', 'aaa', 1) == nil)
|
assert(not string.find('', 'aaa', 1))
|
||||||
assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
|
assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
|
||||||
|
|
||||||
assert(string.len("") == 0)
|
assert(string.len("") == 0)
|
||||||
@@ -158,20 +161,41 @@ do -- tests for '%p' format
|
|||||||
-- not much to test, as C does not specify what '%p' does.
|
-- not much to test, as C does not specify what '%p' does.
|
||||||
-- ("The value of the pointer is converted to a sequence of printing
|
-- ("The value of the pointer is converted to a sequence of printing
|
||||||
-- characters, in an implementation-defined manner.")
|
-- characters, in an implementation-defined manner.")
|
||||||
local null = string.format("%p", nil)
|
local null = "(null)" -- nulls are formatted by Lua
|
||||||
assert(string.format("%p", {}) ~= null)
|
|
||||||
assert(string.format("%p", 4) == null)
|
assert(string.format("%p", 4) == null)
|
||||||
|
assert(string.format("%p", true) == null)
|
||||||
|
assert(string.format("%p", nil) == null)
|
||||||
|
assert(string.format("%p", {}) ~= null)
|
||||||
assert(string.format("%p", print) ~= null)
|
assert(string.format("%p", print) ~= null)
|
||||||
assert(string.format("%p", coroutine.running()) ~= null)
|
assert(string.format("%p", coroutine.running()) ~= null)
|
||||||
assert(string.format("%p", {}) ~= string.format("%p", {}))
|
assert(string.format("%p", io.stdin) ~= null)
|
||||||
assert(string.format("%p", string.rep("a", 10)) ==
|
assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
|
||||||
string.format("%p", string.rep("a", 10))) -- short strings
|
assert(string.format("%p", print) == string.format("%p", print))
|
||||||
assert(string.format("%p", string.rep("a", 300)) ~=
|
assert(string.format("%p", print) ~= string.format("%p", assert))
|
||||||
string.format("%p", string.rep("a", 300))) -- long strings
|
|
||||||
assert(#string.format("%90p", {}) == 90)
|
assert(#string.format("%90p", {}) == 90)
|
||||||
|
assert(#string.format("%-60p", {}) == 60)
|
||||||
|
assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null)
|
||||||
|
assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null))
|
||||||
|
|
||||||
|
do
|
||||||
|
local t1 = {}; local t2 = {}
|
||||||
|
assert(string.format("%p", t1) ~= string.format("%p", 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))
|
||||||
|
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))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
x = '"ílo"\n\\'
|
local x = '"ílo"\n\\'
|
||||||
assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\')
|
assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\')
|
||||||
assert(string.format('%q', "\0") == [["\0"]])
|
assert(string.format('%q', "\0") == [["\0"]])
|
||||||
assert(load(string.format('return %q', x))() == x)
|
assert(load(string.format('return %q', x))() == x)
|
||||||
@@ -181,13 +205,11 @@ assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140)
|
|||||||
"\0\xe4\0b8c\0")
|
"\0\xe4\0b8c\0")
|
||||||
assert(string.format('') == "")
|
assert(string.format('') == "")
|
||||||
assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) ==
|
assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) ==
|
||||||
string.format("%c%c%c%c", 34, 48, 90, 100))
|
string.format("%1c%-c%-1c%c", 34, 48, 90, 100))
|
||||||
assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be')
|
assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be')
|
||||||
assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023")
|
assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023")
|
||||||
assert(tonumber(string.format("%f", 10.3)) == 10.3)
|
assert(tonumber(string.format("%f", 10.3)) == 10.3)
|
||||||
x = string.format('"%-50s"', 'a')
|
assert(string.format('"%-50s"', 'a') == '"a' .. string.rep(' ', 49) .. '"')
|
||||||
assert(#x == 52)
|
|
||||||
assert(string.sub(x, 1, 4) == '"a ')
|
|
||||||
|
|
||||||
assert(string.format("-%.20s.20s", string.rep("%", 2000)) ==
|
assert(string.format("-%.20s.20s", string.rep("%", 2000)) ==
|
||||||
"-"..string.rep("%", 20)..".20s")
|
"-"..string.rep("%", 20)..".20s")
|
||||||
@@ -216,7 +238,6 @@ end
|
|||||||
|
|
||||||
assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0")
|
assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0")
|
||||||
checkerror("contains zeros", string.format, "%10s", "\0")
|
checkerror("contains zeros", string.format, "%10s", "\0")
|
||||||
checkerror("cannot have modifiers", string.format, "%10q", "1")
|
|
||||||
|
|
||||||
-- format x tostring
|
-- format x tostring
|
||||||
assert(string.format("%s %s", nil, true) == "nil true")
|
assert(string.format("%s %s", nil, true) == "nil true")
|
||||||
@@ -250,6 +271,12 @@ do -- longest number that can be formatted
|
|||||||
local s = string.format('%.99f', -(10^i))
|
local s = string.format('%.99f', -(10^i))
|
||||||
assert(string.len(s) >= i + 101)
|
assert(string.len(s) >= i + 101)
|
||||||
assert(tonumber(s) == -(10^i))
|
assert(tonumber(s) == -(10^i))
|
||||||
|
|
||||||
|
-- limit for floats
|
||||||
|
assert(10^38 < math.huge)
|
||||||
|
local s = string.format('%.99f', -(10^38))
|
||||||
|
assert(string.len(s) >= 38 + 101)
|
||||||
|
assert(tonumber(s) == -(10^38))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -295,8 +322,8 @@ do print("testing 'format %a %A'")
|
|||||||
matchhexa(n)
|
matchhexa(n)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$"))
|
assert(string.find(string.format("%A", 0.0), "^0X0%.?0*P%+?0$"))
|
||||||
assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$"))
|
assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0*p%+?0$"))
|
||||||
|
|
||||||
if not _port then -- test inf, -inf, NaN, and -0.0
|
if not _port then -- test inf, -inf, NaN, and -0.0
|
||||||
assert(string.find(string.format("%a", 1/0), "^inf"))
|
assert(string.find(string.format("%a", 1/0), "^inf"))
|
||||||
@@ -314,6 +341,26 @@ do print("testing 'format %a %A'")
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- testing some flags (all these results are required by ISO C)
|
||||||
|
assert(string.format("%#12o", 10) == " 012")
|
||||||
|
assert(string.format("%#10x", 100) == " 0x64")
|
||||||
|
assert(string.format("%#-17X", 100) == "0X64 ")
|
||||||
|
assert(string.format("%013i", -100) == "-000000000100")
|
||||||
|
assert(string.format("%2.5d", -100) == "-00100")
|
||||||
|
assert(string.format("%.u", 0) == "")
|
||||||
|
assert(string.format("%+#014.0f", 100) == "+000000000100.")
|
||||||
|
assert(string.format("%-16c", 97) == "a ")
|
||||||
|
assert(string.format("%+.3G", 1.5) == "+1.5")
|
||||||
|
assert(string.format("%.0s", "alo") == "")
|
||||||
|
assert(string.format("%.s", "alo") == "")
|
||||||
|
|
||||||
|
-- ISO C89 says that "The exponent always contains at least two digits",
|
||||||
|
-- but unlike ISO C99 it does not ensure that it contains "only as many
|
||||||
|
-- more digits as necessary".
|
||||||
|
assert(string.match(string.format("% 1.0E", 100), "^ 1E%+0+2$"))
|
||||||
|
assert(string.match(string.format("% .1g", 2^10), "^ 1e%+0+3$"))
|
||||||
|
|
||||||
|
|
||||||
-- errors in format
|
-- errors in format
|
||||||
|
|
||||||
local function check (fmt, msg)
|
local function check (fmt, msg)
|
||||||
@@ -321,19 +368,30 @@ local function check (fmt, msg)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local aux = string.rep('0', 600)
|
local aux = string.rep('0', 600)
|
||||||
check("%100.3d", "too long")
|
check("%100.3d", "invalid conversion")
|
||||||
check("%1"..aux..".3d", "too long")
|
check("%1"..aux..".3d", "too long")
|
||||||
check("%1.100d", "too long")
|
check("%1.100d", "invalid conversion")
|
||||||
check("%10.1"..aux.."004d", "too long")
|
check("%10.1"..aux.."004d", "too long")
|
||||||
check("%t", "invalid conversion")
|
check("%t", "invalid conversion")
|
||||||
check("%"..aux.."d", "repeated flags")
|
check("%"..aux.."d", "too long")
|
||||||
check("%d %d", "no value")
|
check("%d %d", "no value")
|
||||||
|
check("%010c", "invalid conversion")
|
||||||
|
check("%.10c", "invalid conversion")
|
||||||
|
check("%0.34s", "invalid conversion")
|
||||||
|
check("%#i", "invalid conversion")
|
||||||
|
check("%3.1p", "invalid conversion")
|
||||||
|
check("%0.s", "invalid conversion")
|
||||||
|
check("%10q", "cannot have modifiers")
|
||||||
|
check("%F", "invalid conversion") -- useless and not in C89
|
||||||
|
|
||||||
|
|
||||||
assert(load("return 1\n--comment without ending EOL")() == 1)
|
assert(load("return 1\n--comment without ending EOL")() == 1)
|
||||||
|
|
||||||
|
|
||||||
checkerror("table expected", table.concat, 3)
|
checkerror("table expected", table.concat, 3)
|
||||||
|
checkerror("at index " .. maxi, table.concat, {}, " ", maxi, maxi)
|
||||||
|
-- '%' escapes following minus signal
|
||||||
|
checkerror("at index %" .. mini, table.concat, {}, " ", mini, mini)
|
||||||
assert(table.concat{} == "")
|
assert(table.concat{} == "")
|
||||||
assert(table.concat({}, 'x') == "")
|
assert(table.concat({}, 'x') == "")
|
||||||
assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2")
|
assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2")
|
||||||
@@ -397,7 +455,7 @@ end
|
|||||||
do
|
do
|
||||||
local f = string.gmatch("1 2 3 4 5", "%d+")
|
local f = string.gmatch("1 2 3 4 5", "%d+")
|
||||||
assert(f() == "1")
|
assert(f() == "1")
|
||||||
co = coroutine.wrap(f)
|
local co = coroutine.wrap(f)
|
||||||
assert(co() == "2")
|
assert(co() == "2")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -411,7 +469,7 @@ else
|
|||||||
|
|
||||||
-- formats %U, %f, %I already tested elsewhere
|
-- formats %U, %f, %I already tested elsewhere
|
||||||
|
|
||||||
local blen = 400 -- internal buffer length in 'luaO_pushfstring'
|
local blen = 200 -- internal buffer length in 'luaO_pushfstring'
|
||||||
|
|
||||||
local function callpfs (op, fmt, n)
|
local function callpfs (op, fmt, n)
|
||||||
local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)}
|
local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)}
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ print("\talignment: " .. align)
|
|||||||
|
|
||||||
|
|
||||||
-- check errors in arguments
|
-- check errors in arguments
|
||||||
function checkerror (msg, f, ...)
|
local function checkerror (msg, f, ...)
|
||||||
local status, err = pcall(f, ...)
|
local status, err = pcall(f, ...)
|
||||||
-- print(status, err, msg)
|
-- print(status, err, msg)
|
||||||
assert(not status and string.find(err, msg))
|
assert(not status and string.find(err, msg))
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
-- track collections
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
-- import list
|
||||||
|
local setmetatable, stderr, collectgarbage =
|
||||||
|
setmetatable, io.stderr, collectgarbage
|
||||||
|
|
||||||
|
_ENV = nil
|
||||||
|
|
||||||
|
local active = false
|
||||||
|
|
||||||
|
|
||||||
|
-- each time a table is collected, remark it for finalization on next
|
||||||
|
-- cycle
|
||||||
|
local mt = {}
|
||||||
|
function mt.__gc (o)
|
||||||
|
stderr:write'.' -- mark progress
|
||||||
|
if active then
|
||||||
|
setmetatable(o, mt) -- remark object for finalization
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.start ()
|
||||||
|
if not active then
|
||||||
|
active = true
|
||||||
|
setmetatable({}, mt) -- create initial object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.stop ()
|
||||||
|
if active then
|
||||||
|
active = false
|
||||||
|
collectgarbage() -- call finalizer for the last time
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
+31
-13
@@ -1,6 +1,8 @@
|
|||||||
-- $Id: testes/utf8.lua $
|
-- $Id: testes/utf8.lua $
|
||||||
-- See Copyright Notice in file all.lua
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
|
-- UTF-8 file
|
||||||
|
|
||||||
print "testing UTF-8 library"
|
print "testing UTF-8 library"
|
||||||
|
|
||||||
local utf8 = require'utf8'
|
local utf8 = require'utf8'
|
||||||
@@ -30,8 +32,8 @@ local function checksyntax (s, t)
|
|||||||
assert(assert(load(ts))() == s)
|
assert(assert(load(ts))() == s)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(utf8.offset("alo", 5) == nil)
|
assert(not utf8.offset("alo", 5))
|
||||||
assert(utf8.offset("alo", -4) == nil)
|
assert(not utf8.offset("alo", -4))
|
||||||
|
|
||||||
-- 'check' makes several tests over the validity of string 's'.
|
-- 'check' makes several tests over the validity of string 's'.
|
||||||
-- 't' is the list of codepoints of 's'.
|
-- 't' is the list of codepoints of 's'.
|
||||||
@@ -97,9 +99,15 @@ do -- error indication in utf8.len
|
|||||||
assert(not a and b == p)
|
assert(not a and b == p)
|
||||||
end
|
end
|
||||||
check("abc\xE3def", 4)
|
check("abc\xE3def", 4)
|
||||||
check("汉字\x80", #("汉字") + 1)
|
|
||||||
check("\xF4\x9F\xBF", 1)
|
check("\xF4\x9F\xBF", 1)
|
||||||
check("\xF4\x9F\xBF\xBF", 1)
|
check("\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)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- errors in utf8.codes
|
-- errors in utf8.codes
|
||||||
@@ -112,20 +120,30 @@ do
|
|||||||
end
|
end
|
||||||
errorcodes("ab\xff")
|
errorcodes("ab\xff")
|
||||||
errorcodes("\u{110000}")
|
errorcodes("\u{110000}")
|
||||||
|
errorcodes("in\x80valid")
|
||||||
|
errorcodes("\xbfinvalid")
|
||||||
|
errorcodes("αλφ\xBFα")
|
||||||
|
|
||||||
|
-- calling interation function with invalid arguments
|
||||||
|
local f = utf8.codes("")
|
||||||
|
assert(f("", 2) == nil)
|
||||||
|
assert(f("", -1) == nil)
|
||||||
|
assert(f("", math.mininteger) == nil)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- error in initial position for offset
|
-- error in initial position for offset
|
||||||
checkerror("position out of range", utf8.offset, "abc", 1, 5)
|
checkerror("position out of bounds", utf8.offset, "abc", 1, 5)
|
||||||
checkerror("position out of range", utf8.offset, "abc", 1, -4)
|
checkerror("position out of bounds", utf8.offset, "abc", 1, -4)
|
||||||
checkerror("position out of range", utf8.offset, "", 1, 2)
|
checkerror("position out of bounds", utf8.offset, "", 1, 2)
|
||||||
checkerror("position out of range", utf8.offset, "", 1, -1)
|
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, "𦧺", 1, 2)
|
checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
|
||||||
checkerror("continuation byte", utf8.offset, "\x80", 1)
|
checkerror("continuation byte", utf8.offset, "\x80", 1)
|
||||||
|
|
||||||
-- error in indices for len
|
-- error in indices for len
|
||||||
checkerror("out of string", utf8.len, "abc", 0, 2)
|
checkerror("out of bounds", utf8.len, "abc", 0, 2)
|
||||||
checkerror("out of string", utf8.len, "abc", 1, 4)
|
checkerror("out of bounds", utf8.len, "abc", 1, 4)
|
||||||
|
|
||||||
|
|
||||||
local s = "hello World"
|
local s = "hello World"
|
||||||
@@ -140,11 +158,11 @@ do
|
|||||||
local t = {utf8.codepoint(s,1,#s - 1)}
|
local t = {utf8.codepoint(s,1,#s - 1)}
|
||||||
assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237)
|
assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237)
|
||||||
checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s)
|
checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s)
|
||||||
checkerror("out of range", utf8.codepoint, s, #s + 1)
|
checkerror("out of bounds", utf8.codepoint, s, #s + 1)
|
||||||
t = {utf8.codepoint(s, 4, 3)}
|
t = {utf8.codepoint(s, 4, 3)}
|
||||||
assert(#t == 0)
|
assert(#t == 0)
|
||||||
checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1)
|
checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1)
|
||||||
checkerror("out of range", utf8.codepoint, s, 1, #s + 1)
|
checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1)
|
||||||
-- surrogates
|
-- surrogates
|
||||||
assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1)
|
assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1)
|
||||||
assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1)
|
assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1)
|
||||||
@@ -214,7 +232,7 @@ do
|
|||||||
check(s, {0x10000, 0x1FFFFF}, true)
|
check(s, {0x10000, 0x1FFFFF}, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
x = "日本語a-4\0éó"
|
local x = "日本語a-4\0éó"
|
||||||
check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})
|
check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+9
-9
@@ -3,13 +3,13 @@
|
|||||||
|
|
||||||
print('testing vararg')
|
print('testing vararg')
|
||||||
|
|
||||||
function f(a, ...)
|
local function f (a, ...)
|
||||||
local x = {n = select('#', ...), ...}
|
local x = {n = select('#', ...), ...}
|
||||||
for i = 1, x.n do assert(a[i] == x[i]) end
|
for i = 1, x.n do assert(a[i] == x[i]) end
|
||||||
return x.n
|
return x.n
|
||||||
end
|
end
|
||||||
|
|
||||||
function c12 (...)
|
local function c12 (...)
|
||||||
assert(arg == _G.arg) -- no local 'arg'
|
assert(arg == _G.arg) -- no local 'arg'
|
||||||
local x = {...}; x.n = #x
|
local x = {...}; x.n = #x
|
||||||
local res = (x.n==2 and x[1] == 1 and x[2] == 2)
|
local res = (x.n==2 and x[1] == 1 and x[2] == 2)
|
||||||
@@ -17,7 +17,7 @@ function c12 (...)
|
|||||||
return res, 2
|
return res, 2
|
||||||
end
|
end
|
||||||
|
|
||||||
function vararg (...) return {n = select('#', ...), ...} end
|
local function vararg (...) return {n = select('#', ...), ...} end
|
||||||
|
|
||||||
local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
|
local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ assert(vararg().n == 0)
|
|||||||
assert(vararg(nil, nil).n == 2)
|
assert(vararg(nil, nil).n == 2)
|
||||||
|
|
||||||
assert(c12(1,2)==55)
|
assert(c12(1,2)==55)
|
||||||
a,b = assert(call(c12, {1,2}))
|
local a,b = assert(call(c12, {1,2}))
|
||||||
assert(a == 55 and b == 2)
|
assert(a == 55 and b == 2)
|
||||||
a = call(c12, {1,2;n=2})
|
a = call(c12, {1,2;n=2})
|
||||||
assert(a == 55 and b == 2)
|
assert(a == 55 and b == 2)
|
||||||
@@ -49,7 +49,7 @@ function t:f (...) local arg = {...}; return self[...]+#arg end
|
|||||||
assert(t:f(1,4) == 3 and t:f(2) == 11)
|
assert(t:f(1,4) == 3 and t:f(2) == 11)
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
lim = 20
|
local lim = 20
|
||||||
local i, a = 1, {}
|
local i, a = 1, {}
|
||||||
while i <= lim do a[i] = i+0.3; i=i+1 end
|
while i <= lim do a[i] = i+0.3; i=i+1 end
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ function f(a, b, c, d, ...)
|
|||||||
more[lim-4] == lim+0.3 and not more[lim-3])
|
more[lim-4] == lim+0.3 and not more[lim-3])
|
||||||
end
|
end
|
||||||
|
|
||||||
function g(a,b,c)
|
local function g (a,b,c)
|
||||||
assert(a == 1.3 and b == 2.3 and c == 3.3)
|
assert(a == 1.3 and b == 2.3 and c == 3.3)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ print("+")
|
|||||||
|
|
||||||
-- new-style varargs
|
-- new-style varargs
|
||||||
|
|
||||||
function oneless (a, ...) return ... end
|
local function oneless (a, ...) return ... end
|
||||||
|
|
||||||
function f (n, a, ...)
|
function f (n, a, ...)
|
||||||
local b
|
local b
|
||||||
@@ -99,8 +99,8 @@ assert(a==nil and b==nil and c==nil and d==nil and e==nil)
|
|||||||
|
|
||||||
|
|
||||||
-- varargs for main chunks
|
-- varargs for main chunks
|
||||||
f = load[[ return {...} ]]
|
local f = load[[ return {...} ]]
|
||||||
x = f(2,3)
|
local x = f(2,3)
|
||||||
assert(x[1] == 2 and x[2] == 3 and x[3] == undef)
|
assert(x[1] == 2 and x[2] == 3 and x[3] == undef)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -52,7 +52,7 @@ if _soft then return 10 end
|
|||||||
print "testing large programs (>64k)"
|
print "testing large programs (>64k)"
|
||||||
|
|
||||||
-- template to create a very big test file
|
-- template to create a very big test file
|
||||||
prog = [[$
|
local prog = [[$
|
||||||
|
|
||||||
local a,b
|
local a,b
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ function b:xxx (a,b) return a+b end
|
|||||||
assert(b:xxx(10, 12) == 22) -- pushself with non-constant index
|
assert(b:xxx(10, 12) == 22) -- pushself with non-constant index
|
||||||
b["xxx"] = undef
|
b["xxx"] = undef
|
||||||
|
|
||||||
s = 0; n=0
|
local s = 0; local n=0
|
||||||
for a,b in pairs(b) do s=s+b; n=n+1 end
|
for a,b in pairs(b) do s=s+b; n=n+1 end
|
||||||
-- with 32-bit floats, exact value of 's' depends on summation order
|
-- with 32-bit floats, exact value of 's' depends on summation order
|
||||||
assert(81800000.0 < s and s < 81860000 and n == 70001)
|
assert(81800000.0 < s and s < 81860000 and n == 70001)
|
||||||
@@ -93,7 +93,7 @@ assert(81800000.0 < s and s < 81860000 and n == 70001)
|
|||||||
a = nil; b = nil
|
a = nil; b = nil
|
||||||
print'+'
|
print'+'
|
||||||
|
|
||||||
function f(x) b=x end
|
local function f(x) b=x end
|
||||||
|
|
||||||
a = f{$3$} or 10
|
a = f{$3$} or 10
|
||||||
|
|
||||||
@@ -118,7 +118,7 @@ local function sig (x)
|
|||||||
return (x % 2 == 0) and '' or '-'
|
return (x % 2 == 0) and '' or '-'
|
||||||
end
|
end
|
||||||
|
|
||||||
F = {
|
local F = {
|
||||||
function () -- $1$
|
function () -- $1$
|
||||||
for i=10,50009 do
|
for i=10,50009 do
|
||||||
io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n')
|
io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n')
|
||||||
@@ -138,14 +138,14 @@ function () -- $3$
|
|||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
file = os.tmpname()
|
local file = os.tmpname()
|
||||||
io.output(file)
|
io.output(file)
|
||||||
for s in string.gmatch(prog, "$([^$]+)") do
|
for s in string.gmatch(prog, "$([^$]+)") do
|
||||||
local n = tonumber(s)
|
local n = tonumber(s)
|
||||||
if not n then io.write(s) else F[n]() end
|
if not n then io.write(s) else F[n]() end
|
||||||
end
|
end
|
||||||
io.close()
|
io.close()
|
||||||
result = dofile(file)
|
local result = dofile(file)
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
print'OK'
|
print'OK'
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user