mirror of
https://github.com/lua/lua.git
synced 2026-08-26 04:58:14 +00:00
Compare commits
377 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa66ca76ce | ||
|
|
707b0ba6e2 | ||
|
|
10b0b09555 | ||
|
|
4d5ab9baa6 | ||
|
|
b65252b39b | ||
|
|
fbd8614bdb | ||
|
|
f3b52a6061 | ||
|
|
0232fbffbe | ||
|
|
ed110f66c5 | ||
|
|
22093f9c6e | ||
|
|
792ffaccf2 | ||
|
|
57cdb60429 | ||
|
|
89c09c8e40 | ||
|
|
48baa5e89c | ||
|
|
fdd7209688 | ||
|
|
82a8e06524 | ||
|
|
c258870c59 | ||
|
|
e8e39a277f | ||
|
|
fff7d42a69 | ||
|
|
783aa8a9da | ||
|
|
8d4feb504f | ||
|
|
d77a7a8c26 | ||
|
|
e747491b96 | ||
|
|
acff3ad88d | ||
|
|
e7b2e01d43 | ||
|
|
03ca6385dc | ||
|
|
7777b412de | ||
|
|
c3e9b14d24 | ||
|
|
7d6b78ee79 | ||
|
|
5f3ad5731e | ||
|
|
494e9ba0f4 | ||
|
|
f79b4568ae | ||
|
|
fd51651684 | ||
|
|
994374c4df | ||
|
|
62f6652d53 | ||
|
|
9945253d57 | ||
|
|
1f259be52a | ||
|
|
1a44e82200 | ||
|
|
a272fa66f0 | ||
|
|
b12b635a90 | ||
|
|
7cd7c2e0a1 | ||
|
|
07cf8415e3 | ||
|
|
c0836dda74 | ||
|
|
384d1b47b0 | ||
|
|
19770b03a9 | ||
|
|
a01eba657e | ||
|
|
b7446ea88d | ||
|
|
a051b3323e | ||
|
|
c4e01c568a | ||
|
|
bda090b961 | ||
|
|
e383941652 | ||
|
|
656b3cea1b | ||
|
|
2d1d57bc18 | ||
|
|
03412af06e | ||
|
|
542dbd4c65 | ||
|
|
c25d59a6bd | ||
|
|
5936eb16d8 | ||
|
|
7dc3ca7b8e | ||
|
|
71344b5cac | ||
|
|
f230898ad6 | ||
|
|
3feb702df8 | ||
|
|
c5112f7b15 | ||
|
|
c231a5e2b6 | ||
|
|
dc9ac5b8c2 | ||
|
|
146508b28e | ||
|
|
d103312661 | ||
|
|
2e8f8a18e4 | ||
|
|
f5b0459aba | ||
|
|
d682b3c9ec | ||
|
|
fc085aa627 | ||
|
|
9a5d6aedb7 | ||
|
|
89e3a84344 | ||
|
|
04587b6256 | ||
|
|
bde03eeb48 | ||
|
|
330d426ffd | ||
|
|
02340375be | ||
|
|
5100bc8aa1 | ||
|
|
ebb2afa54b | ||
|
|
7485512384 | ||
|
|
46de77b219 | ||
|
|
d356183402 | ||
|
|
e61ee8a036 | ||
|
|
ff1289a361 | ||
|
|
cd73f3ccc5 | ||
|
|
8c1fb91802 | ||
|
|
33b366ec32 | ||
|
|
c5363a1b58 | ||
|
|
332a06bbd1 | ||
|
|
ffd0d1232d | ||
|
|
07a2dcacbf | ||
|
|
c874abac98 | ||
|
|
789e423b32 | ||
|
|
96ec8671b1 | ||
|
|
cc3a42b190 | ||
|
|
6707ce6349 | ||
|
|
257961c601 | ||
|
|
ae515a346c | ||
|
|
3cdf1d676b | ||
|
|
82129b9266 | ||
|
|
8004ab756f | ||
|
|
ee5ef0f295 | ||
|
|
5bdee4f810 | ||
|
|
48098c42ff | ||
|
|
3ad55386c4 | ||
|
|
caf8278710 | ||
|
|
f7670781cf | ||
|
|
df8b996bcc | ||
|
|
75d5a8924c | ||
|
|
880f82d089 | ||
|
|
1a741157cb | ||
|
|
0c78de0d6d | ||
|
|
df45f7118b | ||
|
|
880df57e52 | ||
|
|
8949904783 | ||
|
|
9294466234 | ||
|
|
e290bd6760 | ||
|
|
dc4232379d | ||
|
|
10fffcd80a | ||
|
|
8264dbc2bb | ||
|
|
9fae7b6d3f | ||
|
|
0f1f51be4b | ||
|
|
ee5edb6b68 | ||
|
|
bda83e22c0 | ||
|
|
364cdbdbdb | ||
|
|
b91bc93fd3 | ||
|
|
53be1451a8 | ||
|
|
eb0be12ceb | ||
|
|
1198d82530 | ||
|
|
6556fcfe5a | ||
|
|
41964648ee | ||
|
|
502214f8a5 | ||
|
|
ed19fe766c | ||
|
|
8f25d08637 | ||
|
|
3b795541c4 | ||
|
|
20b9e59441 | ||
|
|
935d9f7f90 | ||
|
|
e247c3ada3 | ||
|
|
b5dc2f9b0c | ||
|
|
5273477cb9 | ||
|
|
b9db9bd0ef | ||
|
|
050e8536bb | ||
|
|
2b61360d82 | ||
|
|
5b6ac971f9 | ||
|
|
403e181d81 | ||
|
|
4af03c5ae1 | ||
|
|
bde17a419d | ||
|
|
8950e0c049 | ||
|
|
8217e0d4fe | ||
|
|
319ccfefbc | ||
|
|
6a8400ba4f | ||
|
|
2de3361c6c | ||
|
|
e89763e964 | ||
|
|
be63d995c1 | ||
|
|
2b1fc1b38c | ||
|
|
f01c12c891 | ||
|
|
5aabb7a891 | ||
|
|
cb1e451999 | ||
|
|
43cff79bf7 | ||
|
|
95c3144393 | ||
|
|
19eb6ae580 | ||
|
|
cbe05b48bb | ||
|
|
e7f34ad395 | ||
|
|
a5cbb7c3a7 | ||
|
|
dcad08b76d | ||
|
|
afbf5c5dab | ||
|
|
a1415c0d72 | ||
|
|
2ecaf18138 | ||
|
|
cbef15f3ea | ||
|
|
16e8bb360b | ||
|
|
6628012e9e | ||
|
|
ed1fe28bb8 | ||
|
|
6645bb2df4 | ||
|
|
02aed045de | ||
|
|
72bd2ef6c5 | ||
|
|
d39bb51faa | ||
|
|
6142e663e4 | ||
|
|
6dc20ff293 | ||
|
|
99391e24ea | ||
|
|
0ec12c1bd1 | ||
|
|
a71c0ab861 | ||
|
|
04ba6ea83b | ||
|
|
a1935b9cba | ||
|
|
513c639bf9 | ||
|
|
69b5f7a410 | ||
|
|
ae76c39712 | ||
|
|
0d4a1f71db | ||
|
|
67bf789462 | ||
|
|
48d0674c2e | ||
|
|
6ffe006f5c | ||
|
|
4998e852ec | ||
|
|
65ac789565 | ||
|
|
cfc84c856d | ||
|
|
63720a4290 | ||
|
|
484bf14a6b | ||
|
|
abb2f5511d | ||
|
|
fcc6e1220e | ||
|
|
3a91274547 | ||
|
|
f2a813ae10 | ||
|
|
e723c75c02 | ||
|
|
b436ed58a3 | ||
|
|
2b37f2150e | ||
|
|
331632e8d8 | ||
|
|
7d930ec694 | ||
|
|
0cf3b6495a | ||
|
|
b66fedadae | ||
|
|
fd6e680e21 | ||
|
|
57c0db219b | ||
|
|
4ba0cb4580 | ||
|
|
a30c66f0fc | ||
|
|
bb4baa73ea | ||
|
|
d7d44b038d | ||
|
|
297512b34c | ||
|
|
2ae2e6408e | ||
|
|
a80cada914 | ||
|
|
a00013c8d0 | ||
|
|
b9a1f27250 | ||
|
|
29aed28802 | ||
|
|
951b562cf8 | ||
|
|
6408bc0b7f | ||
|
|
91efb4b895 | ||
|
|
27c5b1b237 | ||
|
|
d51bdc166d | ||
|
|
81245b1ad5 | ||
|
|
397ce11996 | ||
|
|
97f2aa5a44 | ||
|
|
419e2cb01d | ||
|
|
374f6c4612 | ||
|
|
4bdf9962bf | ||
|
|
e0306e386f | ||
|
|
2e6e53c7cc | ||
|
|
2a57d2346e | ||
|
|
7e425be975 | ||
|
|
0705e7b39f | ||
|
|
4ccc4578b3 | ||
|
|
5e8c162b6c | ||
|
|
c8d6cb0136 | ||
|
|
0edcdf49c0 | ||
|
|
b7dfd18612 | ||
|
|
8efcd411fe | ||
|
|
90d016980f | ||
|
|
b3996c55f6 | ||
|
|
438a9fcc78 | ||
|
|
7e2015a46d | ||
|
|
5b01cb39b5 | ||
|
|
a0503305bd | ||
|
|
965f74525b | ||
|
|
3f2e21f0ea | ||
|
|
3509914916 | ||
|
|
ae27be40c9 | ||
|
|
cdd26700e8 | ||
|
|
a9a4bf68b0 | ||
|
|
f31cc8e342 | ||
|
|
3c55790ebe | ||
|
|
0adfa773b9 | ||
|
|
3317f5c6d9 | ||
|
|
dbf911c516 | ||
|
|
3a29087cb7 | ||
|
|
234fb7f695 | ||
|
|
c077d47465 | ||
|
|
8e5290d81e | ||
|
|
a1c37f834a | ||
|
|
1a7868c1d5 | ||
|
|
de3933480e | ||
|
|
50e2eb8954 | ||
|
|
94eb37dc73 | ||
|
|
900bdd5761 | ||
|
|
a51069202a | ||
|
|
fb6796ba06 | ||
|
|
88ef06f4f3 | ||
|
|
eb45f3a9b6 | ||
|
|
6321041058 | ||
|
|
2b83711fba | ||
|
|
3ae21a352c | ||
|
|
570143bf88 | ||
|
|
1465edf341 | ||
|
|
77e786d436 | ||
|
|
6d79752e65 | ||
|
|
75812a517b | ||
|
|
151dc5cc5f | ||
|
|
0b6cfea005 | ||
|
|
c8da3fbc35 | ||
|
|
c96cd1c647 | ||
|
|
57e91b4159 | ||
|
|
ef7d29c666 | ||
|
|
81ecaf6178 | ||
|
|
27ab59bc16 | ||
|
|
bd8ce7e331 | ||
|
|
5c519a69d3 | ||
|
|
6f54b07663 | ||
|
|
741ad97e92 | ||
|
|
b4633c586b | ||
|
|
5fae2a5b05 | ||
|
|
36efa6a6b9 | ||
|
|
c6e74e41c9 | ||
|
|
6909b5a2b4 | ||
|
|
594d7266af | ||
|
|
ab4a890d04 | ||
|
|
64e25a6186 | ||
|
|
460968353f | ||
|
|
32a1f54b9a | ||
|
|
6df197ec15 | ||
|
|
177807f21e | ||
|
|
92f02fff11 | ||
|
|
125296c83d | ||
|
|
9c41d9d1df | ||
|
|
ad79eafe86 | ||
|
|
fd179ab4b8 | ||
|
|
bf163ea7f0 | ||
|
|
075661ffde | ||
|
|
db5ac2fa0d | ||
|
|
1735c05ac7 | ||
|
|
5fbd40dbe5 | ||
|
|
049cf14cf9 | ||
|
|
5d628519d3 | ||
|
|
244646bdf7 | ||
|
|
5bdfefd3a5 | ||
|
|
5873786e27 | ||
|
|
cb59019f58 | ||
|
|
52c0f9575b | ||
|
|
4fde357130 | ||
|
|
e5fd1e5fe2 | ||
|
|
87b179e71d | ||
|
|
b401d37188 | ||
|
|
6624ccff26 | ||
|
|
9a38c08011 | ||
|
|
be87789a6c | ||
|
|
d1d1ddec1e | ||
|
|
bfa0898312 | ||
|
|
79b0d05480 | ||
|
|
779381fe9e | ||
|
|
b6911c177d | ||
|
|
9212175ffb | ||
|
|
ad20689feb | ||
|
|
e320790154 | ||
|
|
fee3aa518d | ||
|
|
b58602d93d | ||
|
|
74e10b9c3b | ||
|
|
b044b8c182 | ||
|
|
a113e1498d | ||
|
|
17ca3b1763 | ||
|
|
50b18f60cb | ||
|
|
ad73e5156e | ||
|
|
720c68dabb | ||
|
|
81c39a5244 | ||
|
|
cfabcbfb17 | ||
|
|
bde14c3adc | ||
|
|
4b839d7c72 | ||
|
|
c49edc4566 | ||
|
|
c5d52a5468 | ||
|
|
e75c0148c3 | ||
|
|
28fdbcf393 | ||
|
|
c25072a246 | ||
|
|
1a69a3ecea | ||
|
|
dcfc3c9405 | ||
|
|
4542fb9f78 | ||
|
|
80e817719d | ||
|
|
96f3a31b24 | ||
|
|
ff6c034330 | ||
|
|
05afee0f50 | ||
|
|
351a446ec5 | ||
|
|
e9885efc7c | ||
|
|
59fbbf0a65 | ||
|
|
61c3c80177 | ||
|
|
9de384e27a | ||
|
|
4b526045fa | ||
|
|
4870194380 | ||
|
|
41d0e66184 | ||
|
|
aa1bd8876c | ||
|
|
7314acaa80 | ||
|
|
4b2521cd9e | ||
|
|
491103708e | ||
|
|
fa4d5c8689 | ||
|
|
aef2d711b1 | ||
|
|
bdf566a8a3 | ||
|
|
c3c78030f7 | ||
|
|
ff9ca88aa6 | ||
|
|
463edee2fd |
369
bugs
369
bugs
@@ -1878,13 +1878,6 @@ for i=1,25 do print(i); crash(i) end
|
|||||||
patch = [[
|
patch = [[
|
||||||
--- lundump.c 2008/04/04 16:00:45 2.7.1.3
|
--- lundump.c 2008/04/04 16:00:45 2.7.1.3
|
||||||
+++ lundump.c 2008/04/04 19:51:41 2.7.1.4
|
+++ lundump.c 2008/04/04 19:51:41 2.7.1.4
|
||||||
@@ -1,5 +1,5 @@
|
|
||||||
/*
|
|
||||||
-** $Id: bugs,v 1.132 2014/05/07 16:57:27 roberto Exp roberto $
|
|
||||||
+** $Id: bugs,v 1.132 2014/05/07 16:57:27 roberto Exp roberto $
|
|
||||||
** load precompiled Lua chunks
|
|
||||||
** See Copyright Notice in lua.h
|
|
||||||
*/
|
|
||||||
@@ -161,7 +161,9 @@
|
@@ -161,7 +161,9 @@
|
||||||
|
|
||||||
static Proto* LoadFunction(LoadState* S, TString* p)
|
static Proto* LoadFunction(LoadState* S, TString* p)
|
||||||
@@ -2971,8 +2964,10 @@ patch = [[
|
|||||||
+ return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
|
+ return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
|
||||||
+ sizeof(CallInfo) * n;
|
+ sizeof(CallInfo) * n;
|
||||||
}
|
}
|
||||||
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- [[]]
|
||||||
Bug{
|
Bug{
|
||||||
what = [[Wrong assert when reporting concatenation errors
|
what = [[Wrong assert when reporting concatenation errors
|
||||||
(manifests only when Lua is compiled in debug mode)]],
|
(manifests only when Lua is compiled in debug mode)]],
|
||||||
@@ -3220,6 +3215,26 @@ example = [[
|
|||||||
(segfaults on some platforms with some compiler options)
|
(segfaults on some platforms with some compiler options)
|
||||||
]],
|
]],
|
||||||
patch = [[
|
patch = [[
|
||||||
|
--- ltablib.c 2013/04/12 18:48:47 1.65.1.1
|
||||||
|
+++ ltablib.c 2014/05/07 16:32:55 1.65.1.2
|
||||||
|
@@ -134,13 +135,14 @@
|
||||||
|
|
||||||
|
|
||||||
|
static int unpack (lua_State *L) {
|
||||||
|
- int i, e, n;
|
||||||
|
+ int i, e;
|
||||||
|
+ unsigned int n;
|
||||||
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
i = luaL_optint(L, 2, 1);
|
||||||
|
e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
|
||||||
|
if (i > e) return 0; /* empty range */
|
||||||
|
- n = e - i + 1; /* number of elements */
|
||||||
|
- if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
|
||||||
|
+ n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */
|
||||||
|
+ if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n))
|
||||||
|
return luaL_error(L, "too many results to unpack");
|
||||||
|
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
|
||||||
|
while (i++ < e) /* push arg[i + 1...e] */
|
||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3248,6 +3263,344 @@ patch = [[
|
|||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[Chunk with too many lines can seg. fault]],
|
||||||
|
report = [[Roberto, 2014/11/14]],
|
||||||
|
since = [[5.1 (at least)]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
-- the cause of the bug is the use of an unitialized variable, so
|
||||||
|
-- it cannot be reproduced reliably
|
||||||
|
local s = string.rep("\n", 2^24)
|
||||||
|
print(load(function () return s end))
|
||||||
|
]],
|
||||||
|
patch = [[
|
||||||
|
--- llex.c 2013/08/30 15:49:41 2.63.1.2
|
||||||
|
+++ llex.c 2015/02/09 17:05:31
|
||||||
|
@@ -153,5 +153,5 @@
|
||||||
|
next(ls); /* skip `\n\r' or `\r\n' */
|
||||||
|
if (++ls->linenumber >= MAX_INT)
|
||||||
|
- luaX_syntaxerror(ls, "chunk has too many lines");
|
||||||
|
+ lexerror(ls, "chunk has too many lines", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------
|
||||||
|
-- Lua 5.3.0
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [['string.format("%f")' can cause a buffer overflow
|
||||||
|
(only when 'lua_Number' is long double!)]],
|
||||||
|
report = [[Roberto, 2015/01/13]],
|
||||||
|
since = [[5.3]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[string.format("%.99f", 1e4000) -- when floats are long double]],
|
||||||
|
patch = [[
|
||||||
|
--- lstrlib.c 2014/12/11 14:03:07 1.221
|
||||||
|
+++ lstrlib.c 2015/02/23 19:01:42
|
||||||
|
@@ -800,3 +800,4 @@
|
||||||
|
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
||||||
|
-#define MAX_ITEM 512
|
||||||
|
+#define MAX_ITEM \
|
||||||
|
+ (sizeof(lua_Number) <= 4 ? 150 : sizeof(lua_Number) <= 8 ? 450 : 5050)
|
||||||
|
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [['debug.getlocal' on a coroutine suspended in a hook
|
||||||
|
can crash the interpreter]],
|
||||||
|
report = [[云风, 2015/02/11]],
|
||||||
|
since = [[5.2]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[see http://lua-users.org/lists/lua-l/2015-02/msg00146.html]],
|
||||||
|
patch = [[
|
||||||
|
--- ldebug.c 2015/01/02 12:52:22 2.110
|
||||||
|
+++ ldebug.c 2015/02/13 16:03:23
|
||||||
|
@@ -49,4 +49,14 @@
|
||||||
|
|
||||||
|
|
||||||
|
+static void swapextra (lua_State *L) {
|
||||||
|
+ if (L->status == LUA_YIELD) {
|
||||||
|
+ CallInfo *ci = L->ci; /* get function that yielded */
|
||||||
|
+ StkId temp = ci->func; /* exchange its 'func' and 'extra' values */
|
||||||
|
+ ci->func = restorestack(L, ci->extra);
|
||||||
|
+ ci->extra = savestack(L, temp);
|
||||||
|
+ }
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+
|
||||||
|
/*
|
||||||
|
** this function can be called asynchronous (e.g. during a signal)
|
||||||
|
@@ -145,4 +155,5 @@
|
||||||
|
const char *name;
|
||||||
|
lua_lock(L);
|
||||||
|
+ swapextra(L);
|
||||||
|
if (ar == NULL) { /* information about non-active function? */
|
||||||
|
if (!isLfunction(L->top - 1)) /* not a Lua function? */
|
||||||
|
@@ -159,4 +170,5 @@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
+ swapextra(L);
|
||||||
|
lua_unlock(L);
|
||||||
|
return name;
|
||||||
|
@@ -166,10 +178,13 @@
|
||||||
|
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||||
|
StkId pos = 0; /* to avoid warnings */
|
||||||
|
- const char *name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
|
+ const char *name;
|
||||||
|
lua_lock(L);
|
||||||
|
+ swapextra(L);
|
||||||
|
+ name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
|
if (name) {
|
||||||
|
setobjs2s(L, pos, L->top - 1);
|
||||||
|
L->top--; /* pop value */
|
||||||
|
}
|
||||||
|
+ swapextra(L);
|
||||||
|
lua_unlock(L);
|
||||||
|
return name;
|
||||||
|
@@ -271,4 +286,5 @@
|
||||||
|
StkId func;
|
||||||
|
lua_lock(L);
|
||||||
|
+ swapextra(L);
|
||||||
|
if (*what == '>') {
|
||||||
|
ci = NULL;
|
||||||
|
@@ -289,4 +305,5 @@
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
+ swapextra(L);
|
||||||
|
if (strchr(what, 'L'))
|
||||||
|
collectvalidlines(L, cl);
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[suspended '__le' metamethod can give wrong result]],
|
||||||
|
report = [[Eric Zhong, 2015/04/07]],
|
||||||
|
since = [[5.2]],
|
||||||
|
fix = nil,
|
||||||
|
|
||||||
|
example = [[
|
||||||
|
mt = {__le = function (a,b) coroutine.yield("yield"); return a.x <= b.x end}
|
||||||
|
t1 = setmetatable({x=1}, mt)
|
||||||
|
t2 = {x=2}
|
||||||
|
co = coroutine.wrap(function (a,b) return t2 <= t1 end)
|
||||||
|
co()
|
||||||
|
print(co()) --> true (should be false)
|
||||||
|
]],
|
||||||
|
|
||||||
|
patch = [[
|
||||||
|
--- lstate.h 2014/10/30 18:53:28 2.119
|
||||||
|
+++ lstate.h 2015/04/13 15:58:40
|
||||||
|
@@ -94,6 +94,7 @@
|
||||||
|
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
||||||
|
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||||
|
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||||
|
+#define CIST_LEQ (1<<7) /* using __lt for __le */
|
||||||
|
|
||||||
|
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
||||||
|
|
||||||
|
--- lvm.c 2014/12/27 20:30:38 2.232
|
||||||
|
+++ lvm.c 2015/04/13 15:51:30
|
||||||
|
@@ -292,9 +292,14 @@
|
||||||
|
return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
|
||||||
|
else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try 'le' */
|
||||||
|
return res;
|
||||||
|
- else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try 'lt' */
|
||||||
|
- luaG_ordererror(L, l, r);
|
||||||
|
- return !res;
|
||||||
|
+ else { /* try 'lt': */
|
||||||
|
+ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
|
||||||
|
+ res = luaT_callorderTM(L, r, l, TM_LT);
|
||||||
|
+ L->ci->callstatus ^= CIST_LEQ; /* clear mark */
|
||||||
|
+ if (res < 0)
|
||||||
|
+ luaG_ordererror(L, l, r);
|
||||||
|
+ return !res; /* result is negated */
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -553,11 +558,11 @@
|
||||||
|
case OP_LE: case OP_LT: case OP_EQ: {
|
||||||
|
int res = !l_isfalse(L->top - 1);
|
||||||
|
L->top--;
|
||||||
|
- /* metamethod should not be called when operand is K */
|
||||||
|
- lua_assert(!ISK(GETARG_B(inst)));
|
||||||
|
- if (op == OP_LE && /* "<=" using "<" instead? */
|
||||||
|
- ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
|
||||||
|
- res = !res; /* invert result */
|
||||||
|
+ if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
|
||||||
|
+ lua_assert(op == OP_LE);
|
||||||
|
+ ci->callstatus ^= CIST_LEQ; /* clear mark */
|
||||||
|
+ res = !res; /* negate result */
|
||||||
|
+ }
|
||||||
|
lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
|
||||||
|
if (res != GETARG_A(inst)) /* condition failed? */
|
||||||
|
ci->u.l.savedpc++; /* skip jump instruction */
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[return hook may not see correct values for
|
||||||
|
active local variables when function returns]],
|
||||||
|
report = [[Philipp Janda/Peng Yi, 2015/05/19]],
|
||||||
|
since = [[5.0]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
see messasge http://lua-users.org/lists/lua-l/2015-05/msg00376.html]],
|
||||||
|
patch = [[
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------
|
||||||
|
-- Lua 5.3.1
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [['io.lines' does not check maximum number of options]],
|
||||||
|
report = [[Patrick Donnell, 2015/07/10]],
|
||||||
|
since = [[5.3.0]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
-- can segfault in some machines
|
||||||
|
t ={}; for i = 1, 253 do t[i] = 1 end
|
||||||
|
io.lines("someexistingfile", table.unpack(t))()
|
||||||
|
]],
|
||||||
|
patch = [[
|
||||||
|
--- liolib.c 2015/07/07 17:03:34 2.146
|
||||||
|
+++ liolib.c 2015/07/15 14:40:28 2.147
|
||||||
|
@@ -318,8 +318,15 @@
|
||||||
|
static int io_readline (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
+/*
|
||||||
|
+** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit
|
||||||
|
+** in the limit for upvalues of a closure)
|
||||||
|
+*/
|
||||||
|
+#define MAXARGLINE 250
|
||||||
|
+
|
||||||
|
static void aux_lines (lua_State *L, int toclose) {
|
||||||
|
int n = lua_gettop(L) - 1; /* number of arguments to read */
|
||||||
|
+ luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
|
||||||
|
lua_pushinteger(L, n); /* number of arguments to read */
|
||||||
|
lua_pushboolean(L, toclose); /* close/not close file when finished */
|
||||||
|
lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------
|
||||||
|
-- Lua 5.3.2
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[Metatable may access its own dealocated field when
|
||||||
|
it has a self reference in __newindex]],
|
||||||
|
report = [[actboy168@gmail.com, 2016/01/01]],
|
||||||
|
since = [[5.3.2]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
local mt = {}
|
||||||
|
mt.__newindex = mt
|
||||||
|
local t = setmetatable({}, mt)
|
||||||
|
t[1] = 1 -- will segfault on some machines
|
||||||
|
]],
|
||||||
|
patch = [[
|
||||||
|
--- lvm.c 2015/11/23 11:30:45 2.265
|
||||||
|
+++ lvm.c 2016/01/01 14:34:12
|
||||||
|
@@ -190,18 +190,19 @@
|
||||||
|
for (loop = 0; loop < MAXTAGLOOP; loop++) {
|
||||||
|
const TValue *tm;
|
||||||
|
if (oldval != NULL) {
|
||||||
|
- lua_assert(ttistable(t) && ttisnil(oldval));
|
||||||
|
+ Table *h = hvalue(t); /* save 't' table */
|
||||||
|
+ lua_assert(ttisnil(oldval));
|
||||||
|
/* must check the metamethod */
|
||||||
|
- if ((tm = fasttm(L, hvalue(t)->metatable, TM_NEWINDEX)) == NULL &&
|
||||||
|
+ if ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
|
||||||
|
/* no metamethod; is there a previous entry in the table? */
|
||||||
|
(oldval != luaO_nilobject ||
|
||||||
|
/* no previous entry; must create one. (The next test is
|
||||||
|
always true; we only need the assignment.) */
|
||||||
|
- (oldval = luaH_newkey(L, hvalue(t), key), 1))) {
|
||||||
|
+ (oldval = luaH_newkey(L, h, key), 1))) {
|
||||||
|
/* no metamethod and (now) there is an entry with given key */
|
||||||
|
setobj2t(L, cast(TValue *, oldval), val);
|
||||||
|
- invalidateTMcache(hvalue(t));
|
||||||
|
- luaC_barrierback(L, hvalue(t), val);
|
||||||
|
+ invalidateTMcache(h);
|
||||||
|
+ luaC_barrierback(L, h, val);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/* else will try the metamethod */
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[label between local definitions can mix-up their initializations]],
|
||||||
|
report = [[Karel Tuma, 2016/03/01]],
|
||||||
|
since = [[5.2]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
do
|
||||||
|
local k = 0
|
||||||
|
local x
|
||||||
|
::foo::
|
||||||
|
local y -- should be reset to nil after goto, but it is not
|
||||||
|
assert(not y)
|
||||||
|
y = true
|
||||||
|
k = k + 1
|
||||||
|
if k < 2 then goto foo end
|
||||||
|
end
|
||||||
|
]],
|
||||||
|
patch = [[
|
||||||
|
--- lparser.c 2015/11/02 16:09:30 2.149
|
||||||
|
+++ lparser.c 2016/03/03 12:03:37
|
||||||
|
@@ -1226,7 +1226,7 @@
|
||||||
|
checkrepeated(fs, ll, label); /* check for repeated labels */
|
||||||
|
checknext(ls, TK_DBCOLON); /* skip double colon */
|
||||||
|
/* create new entry for this label */
|
||||||
|
- l = newlabelentry(ls, ll, label, line, fs->pc);
|
||||||
|
+ l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs));
|
||||||
|
skipnoopstat(ls); /* skip other no-op statements */
|
||||||
|
if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
|
||||||
|
/* assume that locals are already out of scope */
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [['gmatch' iterator fails when called from a coroutine different
|
||||||
|
from the one that created it]],
|
||||||
|
report = [[Nagaev Boris, 2016/03/18]],
|
||||||
|
since = [[5.3.2]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
local f = string.gmatch("1 2 3 4 5", "%d+")
|
||||||
|
print(f()) --> 1
|
||||||
|
co = coroutine.wrap(f)
|
||||||
|
print(co()) --> ??? (should be 2)
|
||||||
|
]],
|
||||||
|
patch = [[
|
||||||
|
--- lstrlib.c 2015/11/25 16:28:17 1.239
|
||||||
|
+++ lstrlib.c 2016/04/11 15:29:41
|
||||||
|
@@ -688,6 +688,7 @@
|
||||||
|
static int gmatch_aux (lua_State *L) {
|
||||||
|
GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3));
|
||||||
|
const char *src;
|
||||||
|
+ gm->ms.L = L;
|
||||||
|
for (src = gm->src; src <= gm->ms.src_end; src++) {
|
||||||
|
const char *e;
|
||||||
|
reprepstate(&gm->ms);
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
--[=[
|
--[=[
|
||||||
Bug{
|
Bug{
|
||||||
@@ -3260,3 +3613,5 @@ patch = [[
|
|||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
]=]
|
]=]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
307
lapi.c
307
lapi.c
@@ -1,17 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lapi.c,v 2.237 2014/10/15 14:27:40 roberto Exp roberto $
|
** $Id: lapi.c,v 2.258 2016/01/05 16:07:21 roberto Exp roberto $
|
||||||
** Lua API
|
** Lua API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lapi_c
|
#define lapi_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -50,29 +51,29 @@ const char lua_ident[] =
|
|||||||
/* test for valid but not pseudo index */
|
/* test for valid but not pseudo index */
|
||||||
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
|
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
|
||||||
|
|
||||||
#define api_checkvalidindex(o) api_check(isvalid(o), "invalid index")
|
#define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index")
|
||||||
|
|
||||||
#define api_checkstackindex(i, o) \
|
#define api_checkstackindex(l, i, o) \
|
||||||
api_check(isstackindex(i, o), "index not in the stack")
|
api_check(l, isstackindex(i, o), "index not in the stack")
|
||||||
|
|
||||||
|
|
||||||
static TValue *index2addr (lua_State *L, int idx) {
|
static TValue *index2addr (lua_State *L, int idx) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
if (idx > 0) {
|
if (idx > 0) {
|
||||||
TValue *o = ci->func + idx;
|
TValue *o = ci->func + idx;
|
||||||
api_check(idx <= ci->top - (ci->func + 1), "unacceptable index");
|
api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
|
||||||
if (o >= L->top) return NONVALIDVALUE;
|
if (o >= L->top) return NONVALIDVALUE;
|
||||||
else return o;
|
else return o;
|
||||||
}
|
}
|
||||||
else if (!ispseudo(idx)) { /* negative index */
|
else if (!ispseudo(idx)) { /* negative index */
|
||||||
api_check(idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
||||||
return L->top + idx;
|
return L->top + 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(idx <= MAXUPVAL + 1, "upvalue index too large");
|
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
|
||||||
if (ttislcf(ci->func)) /* light C function? */
|
if (ttislcf(ci->func)) /* light C function? */
|
||||||
return NONVALIDVALUE; /* it has no upvalues */
|
return NONVALIDVALUE; /* it has no upvalues */
|
||||||
else {
|
else {
|
||||||
@@ -93,22 +94,22 @@ static void growstack (lua_State *L, void *ud) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_checkstack (lua_State *L, int size) {
|
LUA_API int lua_checkstack (lua_State *L, int n) {
|
||||||
int res;
|
int res;
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_check(size >= 0, "negative 'size'");
|
api_check(L, n >= 0, "negative 'n'");
|
||||||
if (L->stack_last - L->top > size) /* stack large enough? */
|
if (L->stack_last - L->top > n) /* stack large enough? */
|
||||||
res = 1; /* yes; check is OK */
|
res = 1; /* yes; check is OK */
|
||||||
else { /* no; need to grow stack */
|
else { /* no; need to grow stack */
|
||||||
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
|
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
|
||||||
if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
|
if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
|
||||||
res = 0; /* no */
|
res = 0; /* no */
|
||||||
else /* try to grow stack */
|
else /* try to grow stack */
|
||||||
res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
|
res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK);
|
||||||
}
|
}
|
||||||
if (res && ci->top < L->top + size)
|
if (res && ci->top < L->top + n)
|
||||||
ci->top = L->top + size; /* adjust frame top */
|
ci->top = L->top + n; /* adjust frame top */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -119,11 +120,12 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
|
|||||||
if (from == to) return;
|
if (from == to) return;
|
||||||
lua_lock(to);
|
lua_lock(to);
|
||||||
api_checknelems(from, n);
|
api_checknelems(from, n);
|
||||||
api_check(G(from) == G(to), "moving among independent states");
|
api_check(from, G(from) == G(to), "moving among independent states");
|
||||||
api_check(to->ci->top - to->top >= n, "not enough elements to move");
|
api_check(from, to->ci->top - to->top >= n, "stack overflow");
|
||||||
from->top -= n;
|
from->top -= n;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
setobj2s(to, to->top++, from->top + i);
|
setobj2s(to, to->top, from->top + i);
|
||||||
|
to->top++; /* stack already checked by previous 'api_check' */
|
||||||
}
|
}
|
||||||
lua_unlock(to);
|
lua_unlock(to);
|
||||||
}
|
}
|
||||||
@@ -158,7 +160,7 @@ LUA_API const 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 - L->ci->func) + idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -171,14 +173,14 @@ LUA_API void lua_settop (lua_State *L, int idx) {
|
|||||||
StkId func = L->ci->func;
|
StkId func = L->ci->func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
api_check(idx <= L->stack_last - (func + 1), "new top too large");
|
api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
|
||||||
while (L->top < (func + 1) + idx)
|
while (L->top < (func + 1) + idx)
|
||||||
setnilvalue(L->top++);
|
setnilvalue(L->top++);
|
||||||
L->top = (func + 1) + idx;
|
L->top = (func + 1) + idx;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
||||||
L->top += idx+1; /* `subtract' index (index is negative) */
|
L->top += idx+1; /* 'subtract' index (index is negative) */
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -207,8 +209,8 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = L->top - 1; /* end of stack segment being rotated */
|
t = L->top - 1; /* end of stack segment being rotated */
|
||||||
p = index2addr(L, idx); /* start of segment */
|
p = index2addr(L, idx); /* start of segment */
|
||||||
api_checkstackindex(idx, p);
|
api_checkstackindex(L, idx, p);
|
||||||
api_check((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 */
|
||||||
reverse(L, p, m); /* reverse the prefix with length 'n' */
|
reverse(L, p, m); /* reverse the prefix with length 'n' */
|
||||||
reverse(L, m + 1, t); /* reverse the suffix */
|
reverse(L, m + 1, t); /* reverse the suffix */
|
||||||
@@ -222,7 +224,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
fr = index2addr(L, fromidx);
|
fr = index2addr(L, fromidx);
|
||||||
to = index2addr(L, toidx);
|
to = index2addr(L, toidx);
|
||||||
api_checkvalidindex(to);
|
api_checkvalidindex(L, to);
|
||||||
setobj(L, to, fr);
|
setobj(L, to, fr);
|
||||||
if (isupvalue(toidx)) /* function upvalue? */
|
if (isupvalue(toidx)) /* function upvalue? */
|
||||||
luaC_barrier(L, clCvalue(L->ci->func), fr);
|
luaC_barrier(L, clCvalue(L->ci->func), fr);
|
||||||
@@ -254,7 +256,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(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
|
api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
|
||||||
return ttypename(t);
|
return ttypename(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +306,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
|
|||||||
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, L->top - 1);
|
||||||
L->top++;
|
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, L->top - 2, L->top - 1, L->top - 2);
|
luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
|
||||||
@@ -324,7 +326,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
|||||||
case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
|
case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
|
||||||
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
|
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
|
||||||
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
|
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
|
||||||
default: api_check(0, "invalid option");
|
default: api_check(L, 0, "invalid option");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -375,21 +377,23 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
|||||||
if (len != NULL) *len = 0;
|
if (len != NULL) *len = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
lua_lock(L); /* `luaO_tostring' may create a new string */
|
lua_lock(L); /* 'luaO_tostring' may create a new string */
|
||||||
|
luaO_tostring(L, o);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
o = index2addr(L, idx); /* previous call may reallocate the stack */
|
o = index2addr(L, idx); /* previous call may reallocate the stack */
|
||||||
luaO_tostring(L, o);
|
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
if (len != NULL) *len = tsvalue(o)->len;
|
if (len != NULL)
|
||||||
|
*len = vslen(o);
|
||||||
return svalue(o);
|
return svalue(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API size_t lua_rawlen (lua_State *L, int idx) {
|
LUA_API size_t lua_rawlen (lua_State *L, int idx) {
|
||||||
StkId o = index2addr(L, idx);
|
StkId o = index2addr(L, idx);
|
||||||
switch (ttnov(o)) {
|
switch (ttype(o)) {
|
||||||
case LUA_TSTRING: return tsvalue(o)->len;
|
case LUA_TSHRSTR: return tsvalue(o)->shrlen;
|
||||||
|
case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
|
||||||
case LUA_TUSERDATA: return uvalue(o)->len;
|
case LUA_TUSERDATA: return uvalue(o)->len;
|
||||||
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
||||||
default: return 0;
|
default: return 0;
|
||||||
@@ -430,9 +434,8 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
|||||||
case LUA_TCCL: return clCvalue(o);
|
case LUA_TCCL: return clCvalue(o);
|
||||||
case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
|
case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
|
||||||
case LUA_TTHREAD: return thvalue(o);
|
case LUA_TTHREAD: return thvalue(o);
|
||||||
case LUA_TUSERDATA:
|
case LUA_TUSERDATA: return getudatamem(uvalue(o));
|
||||||
case LUA_TLIGHTUSERDATA:
|
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
||||||
return lua_touserdata(L, idx);
|
|
||||||
default: return NULL;
|
default: return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -468,33 +471,37 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Pushes on the stack a string with given length. Avoid using 's' when
|
||||||
|
** 'len' == 0 (as 's' can be NULL in that case), due to later use of
|
||||||
|
** 'memcmp' and 'memcpy'.
|
||||||
|
*/
|
||||||
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
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);
|
||||||
luaC_checkGC(L);
|
ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
|
||||||
ts = luaS_newlstr(L, s, len);
|
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top, ts);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return getstr(ts);
|
return getstr(ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
||||||
if (s == NULL) {
|
lua_lock(L);
|
||||||
lua_pushnil(L);
|
if (s == NULL)
|
||||||
return NULL;
|
setnilvalue(L->top);
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
lua_lock(L);
|
|
||||||
luaC_checkGC(L);
|
|
||||||
ts = luaS_new(L, s);
|
ts = luaS_new(L, s);
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top, ts);
|
||||||
api_incr_top(L);
|
s = getstr(ts); /* internal copy's address */
|
||||||
lua_unlock(L);
|
|
||||||
return getstr(ts);
|
|
||||||
}
|
}
|
||||||
|
api_incr_top(L);
|
||||||
|
luaC_checkGC(L);
|
||||||
|
lua_unlock(L);
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -502,8 +509,8 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
|
|||||||
va_list argp) {
|
va_list argp) {
|
||||||
const char *ret;
|
const char *ret;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
luaC_checkGC(L);
|
|
||||||
ret = luaO_pushvfstring(L, fmt, argp);
|
ret = luaO_pushvfstring(L, fmt, argp);
|
||||||
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -513,10 +520,10 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
|
|||||||
const char *ret;
|
const char *ret;
|
||||||
va_list argp;
|
va_list argp;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
luaC_checkGC(L);
|
|
||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
ret = luaO_pushvfstring(L, fmt, argp);
|
ret = luaO_pushvfstring(L, fmt, argp);
|
||||||
va_end(argp);
|
va_end(argp);
|
||||||
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -530,8 +537,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
|||||||
else {
|
else {
|
||||||
CClosure *cl;
|
CClosure *cl;
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
api_check(n <= MAXUPVAL, "upvalue index too large");
|
api_check(L, n <= MAXUPVAL, "upvalue index too large");
|
||||||
luaC_checkGC(L);
|
|
||||||
cl = luaF_newCclosure(L, n);
|
cl = luaF_newCclosure(L, n);
|
||||||
cl->f = fn;
|
cl->f = fn;
|
||||||
L->top -= n;
|
L->top -= n;
|
||||||
@@ -542,6 +548,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
|||||||
setclCvalue(L, L->top, cl);
|
setclCvalue(L, L->top, cl);
|
||||||
}
|
}
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -577,18 +584,30 @@ LUA_API int lua_pushthread (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_getglobal (lua_State *L, const char *var) {
|
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
||||||
Table *reg = hvalue(&G(L)->l_registry);
|
const TValue *slot;
|
||||||
const TValue *gt; /* global table */
|
TString *str = luaS_new(L, k);
|
||||||
lua_lock(L);
|
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
||||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
setobj2s(L, L->top, slot);
|
||||||
setsvalue2s(L, L->top++, luaS_new(L, var));
|
api_incr_top(L);
|
||||||
luaV_gettable(L, gt, L->top - 1, L->top - 1);
|
}
|
||||||
|
else {
|
||||||
|
setsvalue2s(L, L->top, str);
|
||||||
|
api_incr_top(L);
|
||||||
|
luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
|
||||||
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttnov(L->top - 1);
|
return ttnov(L->top - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
||||||
|
Table *reg = hvalue(&G(L)->l_registry);
|
||||||
|
lua_lock(L);
|
||||||
|
return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_gettable (lua_State *L, int idx) {
|
LUA_API int lua_gettable (lua_State *L, int idx) {
|
||||||
StkId t;
|
StkId t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
@@ -600,24 +619,25 @@ LUA_API int lua_gettable (lua_State *L, int idx) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
|
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
|
||||||
StkId t;
|
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
return auxgetstr(L, index2addr(L, idx), k);
|
||||||
setsvalue2s(L, L->top, luaS_new(L, k));
|
|
||||||
api_incr_top(L);
|
|
||||||
luaV_gettable(L, t, L->top - 1, L->top - 1);
|
|
||||||
lua_unlock(L);
|
|
||||||
return ttnov(L->top - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
|
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
|
||||||
StkId t;
|
StkId t;
|
||||||
|
const TValue *slot;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
setivalue(L->top, n);
|
if (luaV_fastget(L, t, n, slot, luaH_getint)) {
|
||||||
api_incr_top(L);
|
setobj2s(L, L->top, slot);
|
||||||
luaV_gettable(L, t, L->top - 1, L->top - 1);
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
setivalue(L->top, n);
|
||||||
|
api_incr_top(L);
|
||||||
|
luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
|
||||||
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttnov(L->top - 1);
|
return ttnov(L->top - 1);
|
||||||
}
|
}
|
||||||
@@ -627,7 +647,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
|
|||||||
StkId t;
|
StkId t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
|
setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttnov(L->top - 1);
|
return ttnov(L->top - 1);
|
||||||
@@ -638,7 +658,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
StkId t;
|
StkId t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setobj2s(L, L->top, luaH_getint(hvalue(t), n));
|
setobj2s(L, L->top, luaH_getint(hvalue(t), n));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -651,7 +671,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
|
|||||||
TValue k;
|
TValue k;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setpvalue(&k, cast(void *, p));
|
setpvalue(&k, cast(void *, p));
|
||||||
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
|
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
@@ -663,19 +683,19 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
|
|||||||
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
||||||
Table *t;
|
Table *t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
luaC_checkGC(L);
|
|
||||||
t = luaH_new(L);
|
t = luaH_new(L);
|
||||||
sethvalue(L, L->top, t);
|
sethvalue(L, L->top, 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);
|
||||||
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
||||||
const TValue *obj;
|
const TValue *obj;
|
||||||
Table *mt = NULL;
|
Table *mt;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
obj = index2addr(L, objindex);
|
obj = index2addr(L, objindex);
|
||||||
@@ -704,7 +724,7 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) {
|
|||||||
StkId o;
|
StkId o;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttisfulluserdata(o), "full userdata expected");
|
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
||||||
getuservalue(L, uvalue(o), L->top);
|
getuservalue(L, uvalue(o), L->top);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -716,17 +736,29 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) {
|
|||||||
** set functions (stack -> Lua)
|
** set functions (stack -> Lua)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
LUA_API void lua_setglobal (lua_State *L, const char *var) {
|
** t[k] = value at the top of the stack (where 'k' is a string)
|
||||||
Table *reg = hvalue(&G(L)->l_registry);
|
*/
|
||||||
const TValue *gt; /* global table */
|
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
|
||||||
lua_lock(L);
|
const TValue *slot;
|
||||||
|
TString *str = luaS_new(L, k);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1))
|
||||||
setsvalue2s(L, L->top++, luaS_new(L, var));
|
L->top--; /* pop value */
|
||||||
luaV_settable(L, gt, L->top - 1, L->top - 2);
|
else {
|
||||||
L->top -= 2; /* pop value and key */
|
setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
|
||||||
lua_unlock(L);
|
api_incr_top(L);
|
||||||
|
luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
|
||||||
|
L->top -= 2; /* pop value and key */
|
||||||
|
}
|
||||||
|
lua_unlock(L); /* lock done by caller */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
||||||
|
Table *reg = hvalue(&G(L)->l_registry);
|
||||||
|
lua_lock(L); /* unlock done in 'auxsetstr' */
|
||||||
|
auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -742,40 +774,40 @@ LUA_API void lua_settable (lua_State *L, int idx) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
|
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
|
||||||
StkId t;
|
lua_lock(L); /* unlock done in 'auxsetstr' */
|
||||||
lua_lock(L);
|
auxsetstr(L, index2addr(L, idx), k);
|
||||||
api_checknelems(L, 1);
|
|
||||||
t = index2addr(L, idx);
|
|
||||||
setsvalue2s(L, L->top++, luaS_new(L, k));
|
|
||||||
luaV_settable(L, t, L->top - 1, L->top - 2);
|
|
||||||
L->top -= 2; /* pop value and key */
|
|
||||||
lua_unlock(L);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
||||||
StkId t;
|
StkId t;
|
||||||
|
const TValue *slot;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
setivalue(L->top++, n);
|
if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1))
|
||||||
luaV_settable(L, t, L->top - 1, L->top - 2);
|
L->top--; /* pop value */
|
||||||
L->top -= 2; /* pop value and key */
|
else {
|
||||||
|
setivalue(L->top, n);
|
||||||
|
api_incr_top(L);
|
||||||
|
luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
|
||||||
|
L->top -= 2; /* pop value and key */
|
||||||
|
}
|
||||||
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) {
|
||||||
StkId o;
|
StkId o;
|
||||||
Table *t;
|
TValue *slot;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 2);
|
api_checknelems(L, 2);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttistable(o), "table expected");
|
api_check(L, ttistable(o), "table expected");
|
||||||
t = hvalue(o);
|
slot = luaH_set(L, hvalue(o), L->top - 2);
|
||||||
setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
|
setobj2t(L, slot, L->top - 1);
|
||||||
invalidateTMcache(t);
|
invalidateTMcache(hvalue(o));
|
||||||
luaC_barrierback(L, t, L->top-1);
|
luaC_barrierback(L, hvalue(o), L->top-1);
|
||||||
L->top -= 2;
|
L->top -= 2;
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -783,14 +815,12 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
|
|||||||
|
|
||||||
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
||||||
StkId o;
|
StkId o;
|
||||||
Table *t;
|
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttistable(o), "table expected");
|
api_check(L, ttistable(o), "table expected");
|
||||||
t = hvalue(o);
|
luaH_setint(L, hvalue(o), n, L->top - 1);
|
||||||
luaH_setint(L, t, n, L->top - 1);
|
luaC_barrierback(L, hvalue(o), L->top-1);
|
||||||
luaC_barrierback(L, t, L->top-1);
|
|
||||||
L->top--;
|
L->top--;
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -798,16 +828,15 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
|
|
||||||
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
||||||
StkId o;
|
StkId o;
|
||||||
Table *t;
|
TValue k, *slot;
|
||||||
TValue k;
|
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttistable(o), "table expected");
|
api_check(L, ttistable(o), "table expected");
|
||||||
t = hvalue(o);
|
|
||||||
setpvalue(&k, cast(void *, p));
|
setpvalue(&k, cast(void *, p));
|
||||||
setobj2t(L, luaH_set(L, t, &k), L->top - 1);
|
slot = luaH_set(L, hvalue(o), &k);
|
||||||
luaC_barrierback(L, t, L->top - 1);
|
setobj2t(L, slot, L->top - 1);
|
||||||
|
luaC_barrierback(L, hvalue(o), L->top - 1);
|
||||||
L->top--;
|
L->top--;
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -822,7 +851,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
|||||||
if (ttisnil(L->top - 1))
|
if (ttisnil(L->top - 1))
|
||||||
mt = NULL;
|
mt = NULL;
|
||||||
else {
|
else {
|
||||||
api_check(ttistable(L->top - 1), "table expected");
|
api_check(L, ttistable(L->top - 1), "table expected");
|
||||||
mt = hvalue(L->top - 1);
|
mt = hvalue(L->top - 1);
|
||||||
}
|
}
|
||||||
switch (ttnov(obj)) {
|
switch (ttnov(obj)) {
|
||||||
@@ -858,7 +887,7 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttisfulluserdata(o), "full userdata expected");
|
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
||||||
setuservalue(L, uvalue(o), L->top - 1);
|
setuservalue(L, uvalue(o), L->top - 1);
|
||||||
luaC_barrier(L, gcvalue(o), L->top - 1);
|
luaC_barrier(L, gcvalue(o), L->top - 1);
|
||||||
L->top--;
|
L->top--;
|
||||||
@@ -867,12 +896,12 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** `load' and `call' functions (run Lua code)
|
** 'load' and 'call' functions (run Lua code)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define checkresults(L,na,nr) \
|
#define checkresults(L,na,nr) \
|
||||||
api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
|
api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
|
||||||
"results from function overflow current stack size")
|
"results from function overflow current stack size")
|
||||||
|
|
||||||
|
|
||||||
@@ -880,19 +909,19 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
|||||||
lua_KContext ctx, lua_KFunction k) {
|
lua_KContext ctx, lua_KFunction k) {
|
||||||
StkId func;
|
StkId func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_check(k == NULL || !isLua(L->ci),
|
api_check(L, k == NULL || !isLua(L->ci),
|
||||||
"cannot use continuations inside hooks");
|
"cannot use continuations inside hooks");
|
||||||
api_checknelems(L, nargs+1);
|
api_checknelems(L, nargs+1);
|
||||||
api_check(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 - (nargs+1);
|
||||||
if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
|
if (k != NULL && L->nny == 0) { /* 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 */
|
||||||
luaD_call(L, func, nresults, 1); /* do the call */
|
luaD_call(L, func, nresults); /* do the call */
|
||||||
}
|
}
|
||||||
else /* no continuation or no yieldable */
|
else /* no continuation or no yieldable */
|
||||||
luaD_call(L, func, nresults, 0); /* just do the call */
|
luaD_callnoyield(L, func, nresults); /* just do the call */
|
||||||
adjustresults(L, nresults);
|
adjustresults(L, nresults);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -902,7 +931,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
|||||||
/*
|
/*
|
||||||
** Execute a protected call.
|
** Execute a protected call.
|
||||||
*/
|
*/
|
||||||
struct CallS { /* data to `f_call' */
|
struct CallS { /* data to 'f_call' */
|
||||||
StkId func;
|
StkId func;
|
||||||
int nresults;
|
int nresults;
|
||||||
};
|
};
|
||||||
@@ -910,7 +939,7 @@ struct CallS { /* data to `f_call' */
|
|||||||
|
|
||||||
static void f_call (lua_State *L, void *ud) {
|
static void f_call (lua_State *L, void *ud) {
|
||||||
struct CallS *c = cast(struct CallS *, ud);
|
struct CallS *c = cast(struct CallS *, ud);
|
||||||
luaD_call(L, c->func, c->nresults, 0);
|
luaD_callnoyield(L, c->func, c->nresults);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -921,16 +950,16 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
|||||||
int status;
|
int status;
|
||||||
ptrdiff_t func;
|
ptrdiff_t func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_check(k == NULL || !isLua(L->ci),
|
api_check(L, k == NULL || !isLua(L->ci),
|
||||||
"cannot use continuations inside hooks");
|
"cannot use continuations inside hooks");
|
||||||
api_checknelems(L, nargs+1);
|
api_checknelems(L, nargs+1);
|
||||||
api_check(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);
|
||||||
if (errfunc == 0)
|
if (errfunc == 0)
|
||||||
func = 0;
|
func = 0;
|
||||||
else {
|
else {
|
||||||
StkId o = index2addr(L, errfunc);
|
StkId o = index2addr(L, errfunc);
|
||||||
api_checkstackindex(errfunc, o);
|
api_checkstackindex(L, errfunc, o);
|
||||||
func = savestack(L, o);
|
func = savestack(L, o);
|
||||||
}
|
}
|
||||||
c.func = L->top - (nargs+1); /* function to be called */
|
c.func = L->top - (nargs+1); /* function to be called */
|
||||||
@@ -948,7 +977,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
|||||||
L->errfunc = func;
|
L->errfunc = func;
|
||||||
setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
|
setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
|
||||||
ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
|
ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
|
||||||
luaD_call(L, c.func, nresults, 1); /* do the call */
|
luaD_call(L, c.func, nresults); /* do the call */
|
||||||
ci->callstatus &= ~CIST_YPCALL;
|
ci->callstatus &= ~CIST_YPCALL;
|
||||||
L->errfunc = ci->u.c.old_errfunc;
|
L->errfunc = ci->u.c.old_errfunc;
|
||||||
status = LUA_OK; /* if it is here, there were no errors */
|
status = LUA_OK; /* if it is here, there were no errors */
|
||||||
@@ -969,7 +998,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *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(L->top - 1); /* get newly created function */
|
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
|
||||||
if (f->nupvalues == 1) { /* does it have one 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);
|
Table *reg = hvalue(&G(L)->l_registry);
|
||||||
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||||
@@ -1037,7 +1066,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
|
|||||||
}
|
}
|
||||||
case LUA_GCSTEP: {
|
case LUA_GCSTEP: {
|
||||||
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 */
|
||||||
int oldrunning = g->gcrunning;
|
lu_byte oldrunning = g->gcrunning;
|
||||||
g->gcrunning = 1; /* allow GC to run */
|
g->gcrunning = 1; /* allow GC to run */
|
||||||
if (data == 0) {
|
if (data == 0) {
|
||||||
luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
|
luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
|
||||||
@@ -1095,7 +1124,7 @@ LUA_API int lua_next (lua_State *L, int idx) {
|
|||||||
int more;
|
int more;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
more = luaH_next(L, hvalue(t), L->top - 1);
|
more = luaH_next(L, hvalue(t), L->top - 1);
|
||||||
if (more) {
|
if (more) {
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
@@ -1111,7 +1140,6 @@ 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 >= 2) {
|
||||||
luaC_checkGC(L);
|
|
||||||
luaV_concat(L, n);
|
luaV_concat(L, n);
|
||||||
}
|
}
|
||||||
else if (n == 0) { /* push empty string */
|
else if (n == 0) { /* push empty string */
|
||||||
@@ -1119,6 +1147,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
|
|||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
/* else n == 1; nothing to do */
|
/* else n == 1; nothing to do */
|
||||||
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1154,10 +1183,10 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
|
|||||||
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
|
||||||
Udata *u;
|
Udata *u;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
luaC_checkGC(L);
|
|
||||||
u = luaS_newudata(L, size);
|
u = luaS_newudata(L, size);
|
||||||
setuvalue(L, L->top, u);
|
setuvalue(L, L->top, u);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
|
luaC_checkGC(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return getudatamem(u);
|
return getudatamem(u);
|
||||||
}
|
}
|
||||||
@@ -1227,9 +1256,9 @@ 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) {
|
||||||
LClosure *f;
|
LClosure *f;
|
||||||
StkId fi = index2addr(L, fidx);
|
StkId fi = index2addr(L, fidx);
|
||||||
api_check(ttisLclosure(fi), "Lua function expected");
|
api_check(L, ttisLclosure(fi), "Lua function expected");
|
||||||
f = clLvalue(fi);
|
f = clLvalue(fi);
|
||||||
api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
|
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 */
|
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
||||||
}
|
}
|
||||||
@@ -1243,11 +1272,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
|||||||
}
|
}
|
||||||
case LUA_TCCL: { /* C closure */
|
case LUA_TCCL: { /* C closure */
|
||||||
CClosure *f = clCvalue(fi);
|
CClosure *f = clCvalue(fi);
|
||||||
api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index");
|
api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
|
||||||
return &f->upvalue[n - 1];
|
return &f->upvalue[n - 1];
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
api_check(0, "closure expected");
|
api_check(L, 0, "closure expected");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
lapi.h
6
lapi.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 roberto Exp roberto $
|
** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 roberto Exp roberto $
|
||||||
** Auxiliary functions from Lua API
|
** Auxiliary functions from Lua API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -11,13 +11,13 @@
|
|||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
#define api_incr_top(L) {L->top++; api_check(L->top <= L->ci->top, \
|
#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
|
||||||
"stack overflow");}
|
"stack overflow");}
|
||||||
|
|
||||||
#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 < L->top) L->ci->top = L->top; }
|
||||||
|
|
||||||
#define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \
|
#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
|
||||||
"not enough elements in the stack")
|
"not enough elements in the stack")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
171
lauxlib.c
171
lauxlib.c
@@ -1,9 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.c,v 1.269 2014/10/17 16:28:21 roberto Exp roberto $
|
** $Id: lauxlib.c,v 1.285 2015/12/14 11:59:27 roberto Exp roberto $
|
||||||
** Auxiliary functions for building Lua libraries
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lauxlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
@@ -12,13 +17,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
/* This file uses only the official API of Lua.
|
/*
|
||||||
|
** This file uses only the official API of Lua.
|
||||||
** Any function declared here could be written as an application function.
|
** Any function declared here could be written as an application function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define lauxlib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -31,8 +34,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define LEVELS1 12 /* size of the first part of the stack */
|
#define LEVELS1 10 /* size of the first part of the stack */
|
||||||
#define LEVELS2 10 /* size of the second part of the stack */
|
#define LEVELS2 11 /* size of the second part of the stack */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -64,11 +67,20 @@ static int findfield (lua_State *L, int objidx, int level) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Search for a name for a function in all loaded modules
|
||||||
|
** (registry._LOADED).
|
||||||
|
*/
|
||||||
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
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_pushglobaltable(L);
|
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||||
if (findfield(L, top + 1, 2)) {
|
if (findfield(L, top + 1, 2)) {
|
||||||
|
const char *name = lua_tostring(L, -1);
|
||||||
|
if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
|
||||||
|
lua_pushstring(L, name + 3); /* push name without prefix */
|
||||||
|
lua_remove(L, -2); /* remove original name */
|
||||||
|
}
|
||||||
lua_copy(L, -1, top + 1); /* move name to proper place */
|
lua_copy(L, -1, top + 1); /* move name to proper place */
|
||||||
lua_pop(L, 2); /* remove pushed values */
|
lua_pop(L, 2); /* remove pushed values */
|
||||||
return 1;
|
return 1;
|
||||||
@@ -81,24 +93,22 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
|||||||
|
|
||||||
|
|
||||||
static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
||||||
if (*ar->namewhat != '\0') /* is there a name? */
|
if (pushglobalfuncname(L, ar)) { /* try first a global name */
|
||||||
lua_pushfstring(L, "function '%s'", ar->name);
|
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
||||||
|
lua_remove(L, -2); /* remove name */
|
||||||
|
}
|
||||||
|
else if (*ar->namewhat != '\0') /* is there a name from code? */
|
||||||
|
lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */
|
||||||
else if (*ar->what == 'm') /* main? */
|
else if (*ar->what == 'm') /* main? */
|
||||||
lua_pushliteral(L, "main chunk");
|
lua_pushliteral(L, "main chunk");
|
||||||
else if (*ar->what == 'C') {
|
else if (*ar->what != 'C') /* for Lua functions, use <file:line> */
|
||||||
if (pushglobalfuncname(L, ar)) {
|
|
||||||
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
|
||||||
lua_remove(L, -2); /* remove name */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
lua_pushliteral(L, "?");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
|
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
|
||||||
|
else /* nothing left... */
|
||||||
|
lua_pushliteral(L, "?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int countlevels (lua_State *L) {
|
static int lastlevel (lua_State *L) {
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int li = 1, le = 1;
|
int li = 1, le = 1;
|
||||||
/* find an upper bound */
|
/* find an upper bound */
|
||||||
@@ -117,14 +127,16 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
|
|||||||
const char *msg, int level) {
|
const char *msg, int level) {
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int top = lua_gettop(L);
|
int top = lua_gettop(L);
|
||||||
int numlevels = countlevels(L1);
|
int last = lastlevel(L1);
|
||||||
int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;
|
int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
|
||||||
if (msg) lua_pushfstring(L, "%s\n", msg);
|
if (msg)
|
||||||
|
lua_pushfstring(L, "%s\n", msg);
|
||||||
|
luaL_checkstack(L, 10, NULL);
|
||||||
lua_pushliteral(L, "stack traceback:");
|
lua_pushliteral(L, "stack traceback:");
|
||||||
while (lua_getstack(L1, level++, &ar)) {
|
while (lua_getstack(L1, level++, &ar)) {
|
||||||
if (level == mark) { /* too many levels? */
|
if (n1-- == 0) { /* too many levels? */
|
||||||
lua_pushliteral(L, "\n\t..."); /* add a '...' */
|
lua_pushliteral(L, "\n\t..."); /* add a '...' */
|
||||||
level = numlevels - LEVELS2; /* and skip to last ones */
|
level = last - LEVELS2 + 1; /* and skip to last ones */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lua_getinfo(L1, "Slnt", &ar);
|
lua_getinfo(L1, "Slnt", &ar);
|
||||||
@@ -156,7 +168,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
|
|||||||
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
|
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
|
||||||
lua_getinfo(L, "n", &ar);
|
lua_getinfo(L, "n", &ar);
|
||||||
if (strcmp(ar.namewhat, "method") == 0) {
|
if (strcmp(ar.namewhat, "method") == 0) {
|
||||||
arg--; /* do not count `self' */
|
arg--; /* do not count 'self' */
|
||||||
if (arg == 0) /* error is in the self argument itself? */
|
if (arg == 0) /* error is in the self argument itself? */
|
||||||
return luaL_error(L, "calling '%s' on bad self (%s)",
|
return luaL_error(L, "calling '%s' on bad self (%s)",
|
||||||
ar.name, extramsg);
|
ar.name, extramsg);
|
||||||
@@ -187,6 +199,10 @@ static void tag_error (lua_State *L, int arg, int tag) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** The use of 'lua_pushfstring' ensures this function does not
|
||||||
|
** need reserved stack space when called.
|
||||||
|
*/
|
||||||
LUALIB_API void luaL_where (lua_State *L, int level) {
|
LUALIB_API void luaL_where (lua_State *L, int level) {
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
if (lua_getstack(L, level, &ar)) { /* check function at level */
|
if (lua_getstack(L, level, &ar)) { /* check function at level */
|
||||||
@@ -196,10 +212,15 @@ LUALIB_API void luaL_where (lua_State *L, int level) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_pushliteral(L, ""); /* else, no information available... */
|
lua_pushfstring(L, ""); /* else, no information available... */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Again, the use of 'lua_pushvfstring' ensures this function does
|
||||||
|
** not need reserved stack space when called. (At worst, it generates
|
||||||
|
** an error with "stack overflow" instead of the given message.)
|
||||||
|
*/
|
||||||
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
|
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
|
||||||
va_list argp;
|
va_list argp;
|
||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
@@ -277,11 +298,10 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||||
luaL_getmetatable(L, tname); /* try to get metatable */
|
if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */
|
||||||
if (!lua_isnil(L, -1)) /* name already in use? */
|
|
||||||
return 0; /* leave previous value on top, but return 0 */
|
return 0; /* leave previous value on top, but return 0 */
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
lua_newtable(L); /* create metatable */
|
lua_createtable(L, 0, 2); /* create metatable */
|
||||||
lua_pushstring(L, tname);
|
lua_pushstring(L, tname);
|
||||||
lua_setfield(L, -2, "__name"); /* metatable.__name = tname */
|
lua_setfield(L, -2, "__name"); /* metatable.__name = tname */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
@@ -339,10 +359,15 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Ensures the stack has at least 'space' extra slots, raising an error
|
||||||
|
** if it cannot fulfill the request. (The error handling needs a few
|
||||||
|
** extra slots to format the error message. In case of an error without
|
||||||
|
** this extra space, Lua will generate the same 'stack overflow' error,
|
||||||
|
** 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) {
|
||||||
/* keep some extra space to run error routines, if needed */
|
if (!lua_checkstack(L, space)) {
|
||||||
const int extra = LUA_MINSTACK;
|
|
||||||
if (!lua_checkstack(L, space + extra)) {
|
|
||||||
if (msg)
|
if (msg)
|
||||||
luaL_error(L, "stack overflow (%s)", msg);
|
luaL_error(L, "stack overflow (%s)", msg);
|
||||||
else
|
else
|
||||||
@@ -427,6 +452,47 @@ LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* userdata to box arbitrary data */
|
||||||
|
typedef struct UBox {
|
||||||
|
void *box;
|
||||||
|
size_t bsize;
|
||||||
|
} UBox;
|
||||||
|
|
||||||
|
|
||||||
|
static void *resizebox (lua_State *L, int idx, size_t newsize) {
|
||||||
|
void *ud;
|
||||||
|
lua_Alloc allocf = lua_getallocf(L, &ud);
|
||||||
|
UBox *box = (UBox *)lua_touserdata(L, idx);
|
||||||
|
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
||||||
|
if (temp == NULL && newsize > 0) { /* allocation error? */
|
||||||
|
resizebox(L, idx, 0); /* free buffer */
|
||||||
|
luaL_error(L, "not enough memory for buffer allocation");
|
||||||
|
}
|
||||||
|
box->box = temp;
|
||||||
|
box->bsize = newsize;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int boxgc (lua_State *L) {
|
||||||
|
resizebox(L, 1, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void *newbox (lua_State *L, size_t newsize) {
|
||||||
|
UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox));
|
||||||
|
box->box = NULL;
|
||||||
|
box->bsize = 0;
|
||||||
|
if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */
|
||||||
|
lua_pushcfunction(L, boxgc);
|
||||||
|
lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */
|
||||||
|
}
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
return resizebox(L, -1, newsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** check whether buffer is using a userdata on the stack as a temporary
|
** check whether buffer is using a userdata on the stack as a temporary
|
||||||
** buffer
|
** buffer
|
||||||
@@ -447,11 +513,12 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
|
|||||||
if (newsize < B->n || newsize - B->n < sz)
|
if (newsize < B->n || newsize - B->n < sz)
|
||||||
luaL_error(L, "buffer too large");
|
luaL_error(L, "buffer too large");
|
||||||
/* create larger buffer */
|
/* create larger buffer */
|
||||||
newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
|
|
||||||
/* move content to new buffer */
|
|
||||||
memcpy(newbuff, B->b, B->n * sizeof(char));
|
|
||||||
if (buffonstack(B))
|
if (buffonstack(B))
|
||||||
lua_remove(L, -2); /* remove old buffer */
|
newbuff = (char *)resizebox(L, -1, newsize);
|
||||||
|
else { /* no buffer yet */
|
||||||
|
newbuff = (char *)newbox(L, newsize);
|
||||||
|
memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */
|
||||||
|
}
|
||||||
B->b = newbuff;
|
B->b = newbuff;
|
||||||
B->size = newsize;
|
B->size = newsize;
|
||||||
}
|
}
|
||||||
@@ -460,9 +527,11 @@ 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) {
|
||||||
char *b = luaL_prepbuffsize(B, l);
|
if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */
|
||||||
memcpy(b, s, l * sizeof(char));
|
char *b = luaL_prepbuffsize(B, l);
|
||||||
luaL_addsize(B, l);
|
memcpy(b, s, l * sizeof(char));
|
||||||
|
luaL_addsize(B, l);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -474,8 +543,10 @@ 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;
|
||||||
lua_pushlstring(L, B->b, B->n);
|
lua_pushlstring(L, B->b, B->n);
|
||||||
if (buffonstack(B))
|
if (buffonstack(B)) {
|
||||||
lua_remove(L, -2); /* remove old buffer */
|
resizebox(L, -2, 0); /* delete old buffer */
|
||||||
|
lua_remove(L, -2); /* remove its header from the stack */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -526,7 +597,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
|||||||
int ref;
|
int ref;
|
||||||
if (lua_isnil(L, -1)) {
|
if (lua_isnil(L, -1)) {
|
||||||
lua_pop(L, 1); /* remove from stack */
|
lua_pop(L, 1); /* remove from stack */
|
||||||
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 */
|
lua_rawgeti(L, t, freelist); /* get first free element */
|
||||||
@@ -565,7 +636,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
|||||||
typedef struct LoadF {
|
typedef struct LoadF {
|
||||||
int n; /* number of pre-read characters */
|
int n; /* number of pre-read characters */
|
||||||
FILE *f; /* file being read */
|
FILE *f; /* file being read */
|
||||||
char buff[LUAL_BUFFERSIZE]; /* area for reading file */
|
char buff[BUFSIZ]; /* area for reading file */
|
||||||
} LoadF;
|
} LoadF;
|
||||||
|
|
||||||
|
|
||||||
@@ -597,7 +668,7 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
|
|||||||
|
|
||||||
|
|
||||||
static int skipBOM (LoadF *lf) {
|
static int skipBOM (LoadF *lf) {
|
||||||
const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
|
const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
|
||||||
int c;
|
int c;
|
||||||
lf->n = 0;
|
lf->n = 0;
|
||||||
do {
|
do {
|
||||||
@@ -622,7 +693,7 @@ static int skipcomment (LoadF *lf, int *cp) {
|
|||||||
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(lf->f);
|
||||||
} while (c != EOF && c != '\n') ;
|
} while (c != EOF && c != '\n');
|
||||||
*cp = getc(lf->f); /* skip end-of-line, if present */
|
*cp = getc(lf->f); /* skip end-of-line, if present */
|
||||||
return 1; /* there was a comment */
|
return 1; /* there was a comment */
|
||||||
}
|
}
|
||||||
@@ -658,7 +729,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
|||||||
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) */
|
||||||
if (readstatus) {
|
if (readstatus) {
|
||||||
lua_settop(L, fnameindex); /* ignore results from `lua_load' */
|
lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
|
||||||
return errfile(L, "read", fnameindex);
|
return errfile(L, "read", fnameindex);
|
||||||
}
|
}
|
||||||
lua_remove(L, fnameindex);
|
lua_remove(L, fnameindex);
|
||||||
@@ -918,7 +989,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
|
|||||||
while ((wild = strstr(s, p)) != NULL) {
|
while ((wild = strstr(s, p)) != NULL) {
|
||||||
luaL_addlstring(&b, s, wild - s); /* push prefix */
|
luaL_addlstring(&b, s, wild - s); /* push prefix */
|
||||||
luaL_addstring(&b, r); /* push replacement in place of pattern */
|
luaL_addstring(&b, r); /* push replacement in place of pattern */
|
||||||
s = wild + l; /* continue after `p' */
|
s = wild + l; /* continue after 'p' */
|
||||||
}
|
}
|
||||||
luaL_addstring(&b, s); /* push last suffix */
|
luaL_addstring(&b, s); /* push last suffix */
|
||||||
luaL_pushresult(&b);
|
luaL_pushresult(&b);
|
||||||
@@ -938,8 +1009,8 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
|||||||
|
|
||||||
|
|
||||||
static int panic (lua_State *L) {
|
static int panic (lua_State *L) {
|
||||||
luai_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));
|
lua_tostring(L, -1));
|
||||||
return 0; /* return to Lua to abort */
|
return 0; /* return to Lua to abort */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
lauxlib.h
31
lauxlib.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.h,v 1.125 2014/06/26 17:25:11 roberto Exp roberto $
|
** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp roberto $
|
||||||
** Auxiliary functions for building Lua libraries
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* extra error code for `luaL_load' */
|
/* extra error code for 'luaL_load' */
|
||||||
#define LUA_ERRFILE (LUA_ERRERR+1)
|
#define LUA_ERRFILE (LUA_ERRERR+1)
|
||||||
|
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
|
|||||||
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
|
LUALIB_API int (luaL_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);
|
||||||
|
|
||||||
/* pre-defined references */
|
/* predefined references */
|
||||||
#define LUA_NOREF (-2)
|
#define LUA_NOREF (-2)
|
||||||
#define LUA_REFNIL (-1)
|
#define LUA_REFNIL (-1)
|
||||||
|
|
||||||
@@ -204,6 +204,31 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** "Abstraction Layer" for basic report of messages and errors
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* print a string */
|
||||||
|
#if !defined(lua_writestring)
|
||||||
|
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* print a newline and flush the output */
|
||||||
|
#if !defined(lua_writeline)
|
||||||
|
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* print an error message */
|
||||||
|
#if !defined(lua_writestringerror)
|
||||||
|
#define lua_writestringerror(s,p) \
|
||||||
|
(fprintf(stderr, (s), (p)), fflush(stderr))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {============================================================
|
** {============================================================
|
||||||
** Compatibility with deprecated conversions
|
** Compatibility with deprecated conversions
|
||||||
|
|||||||
77
lbaselib.c
77
lbaselib.c
@@ -1,9 +1,13 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lbaselib.c,v 1.302 2014/10/17 16:28:21 roberto Exp roberto $
|
** $Id: lbaselib.c,v 1.312 2015/10/29 15:21:04 roberto Exp roberto $
|
||||||
** Basic library
|
** Basic library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lbaselib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@@ -11,9 +15,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lbaselib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -33,11 +34,11 @@ static int luaB_print (lua_State *L) {
|
|||||||
s = lua_tolstring(L, -1, &l); /* get result */
|
s = lua_tolstring(L, -1, &l); /* get result */
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return luaL_error(L, "'tostring' must return a string to 'print'");
|
return luaL_error(L, "'tostring' must return a string to 'print'");
|
||||||
if (i>1) luai_writestring("\t", 1);
|
if (i>1) lua_writestring("\t", 1);
|
||||||
luai_writestring(s, l);
|
lua_writestring(s, l);
|
||||||
lua_pop(L, 1); /* pop result */
|
lua_pop(L, 1); /* pop result */
|
||||||
}
|
}
|
||||||
luai_writeline();
|
lua_writeline();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,14 +55,12 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
do {
|
do {
|
||||||
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
||||||
: toupper((unsigned char)*s) - 'A' + 10;
|
: (toupper((unsigned char)*s) - 'A') + 10;
|
||||||
if (digit >= base) return NULL; /* invalid numeral */
|
if (digit >= base) return NULL; /* invalid numeral */
|
||||||
n = n * base + digit;
|
n = n * base + digit;
|
||||||
s++;
|
s++;
|
||||||
} while (isalnum((unsigned char)*s));
|
} while (isalnum((unsigned char)*s));
|
||||||
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
||||||
if (*s != '\0') /* invalid trailing characters? */
|
|
||||||
return NULL;
|
|
||||||
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
@@ -87,8 +86,8 @@ static int luaB_tonumber (lua_State *L) {
|
|||||||
const char *s;
|
const char *s;
|
||||||
lua_Integer n = 0; /* to avoid warnings */
|
lua_Integer n = 0; /* to avoid warnings */
|
||||||
lua_Integer base = luaL_checkinteger(L, 2);
|
lua_Integer base = luaL_checkinteger(L, 2);
|
||||||
luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
|
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
|
||||||
s = luaL_checklstring(L, 1, &l);
|
s = lua_tolstring(L, 1, &l);
|
||||||
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||||
if (b_str2int(s, (int)base, &n) == s + l) {
|
if (b_str2int(s, (int)base, &n) == s + l) {
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
@@ -103,8 +102,8 @@ static int luaB_tonumber (lua_State *L) {
|
|||||||
static int luaB_error (lua_State *L) {
|
static int luaB_error (lua_State *L) {
|
||||||
int level = (int)luaL_optinteger(L, 2, 1);
|
int level = (int)luaL_optinteger(L, 2, 1);
|
||||||
lua_settop(L, 1);
|
lua_settop(L, 1);
|
||||||
if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
|
if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
|
||||||
luaL_where(L, level);
|
luaL_where(L, level); /* add extra information */
|
||||||
lua_pushvalue(L, 1);
|
lua_pushvalue(L, 1);
|
||||||
lua_concat(L, 2);
|
lua_concat(L, 2);
|
||||||
}
|
}
|
||||||
@@ -199,12 +198,10 @@ static int luaB_collectgarbage (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** This function has all type names as upvalues, to maximize performance.
|
|
||||||
*/
|
|
||||||
static int luaB_type (lua_State *L) {
|
static int luaB_type (lua_State *L) {
|
||||||
luaL_checkany(L, 1);
|
int t = lua_type(L, 1);
|
||||||
lua_pushvalue(L, lua_upvalueindex(lua_type(L, 1) + 1));
|
luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
|
||||||
|
lua_pushstring(L, lua_typename(L, t));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,18 +241,7 @@ static int luaB_pairs (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Traversal function for 'ipairs' for raw tables
|
** Traversal function for 'ipairs'
|
||||||
*/
|
|
||||||
static int ipairsaux_raw (lua_State *L) {
|
|
||||||
lua_Integer i = luaL_checkinteger(L, 2) + 1;
|
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
|
||||||
lua_pushinteger(L, i);
|
|
||||||
return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Traversal function for 'ipairs' for tables with metamethods
|
|
||||||
*/
|
*/
|
||||||
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) + 1;
|
||||||
@@ -265,17 +251,15 @@ static int ipairsaux (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** This function will use either 'ipairsaux' or 'ipairsaux_raw' to
|
** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
|
||||||
** traverse a table, depending on whether the table has metamethods
|
** (The given "table" may not be a table.)
|
||||||
** that can affect the traversal.
|
|
||||||
*/
|
*/
|
||||||
static int luaB_ipairs (lua_State *L) {
|
static int luaB_ipairs (lua_State *L) {
|
||||||
lua_CFunction iter = (luaL_getmetafield(L, 1, "__index") != LUA_TNIL)
|
|
||||||
? ipairsaux : ipairsaux_raw;
|
|
||||||
#if defined(LUA_COMPAT_IPAIRS)
|
#if defined(LUA_COMPAT_IPAIRS)
|
||||||
return pairsmeta(L, "__ipairs", 1, iter);
|
return pairsmeta(L, "__ipairs", 1, ipairsaux);
|
||||||
#else
|
#else
|
||||||
lua_pushcfunction(L, iter); /* iteration function */
|
luaL_checkany(L, 1);
|
||||||
|
lua_pushcfunction(L, ipairsaux); /* iteration function */
|
||||||
lua_pushvalue(L, 1); /* state */
|
lua_pushvalue(L, 1); /* state */
|
||||||
lua_pushinteger(L, 0); /* initial value */
|
lua_pushinteger(L, 0); /* initial value */
|
||||||
return 3;
|
return 3;
|
||||||
@@ -325,7 +309,7 @@ static int luaB_loadfile (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Reader for generic `load' function: `lua_load' uses the
|
** Reader for generic 'load' function: 'lua_load' uses the
|
||||||
** stack for internal stuff, so the reader cannot change the
|
** stack for internal stuff, so the reader cannot change the
|
||||||
** stack top. Instead, it keeps its resulting string in a
|
** stack top. Instead, it keeps its resulting string in a
|
||||||
** reserved slot inside the stack.
|
** reserved slot inside the stack.
|
||||||
@@ -389,9 +373,10 @@ static int luaB_assert (lua_State *L) {
|
|||||||
if (lua_toboolean(L, 1)) /* condition is true? */
|
if (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 */
|
||||||
if (lua_isnone(L, 2)) /* no error message? */
|
luaL_checkany(L, 1); /* there must be a condition */
|
||||||
lua_pushliteral(L, "assertion failed!"); /* use standard message */
|
lua_remove(L, 1); /* remove it */
|
||||||
lua_remove(L, 1); /* remove the condition (if there is one...) */
|
lua_pushliteral(L, "assertion failed!"); /* default message */
|
||||||
|
lua_settop(L, 1); /* leave only message (default if no other one) */
|
||||||
return luaB_error(L); /* call 'error' */
|
return luaB_error(L); /* call 'error' */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -489,9 +474,9 @@ static const luaL_Reg base_funcs[] = {
|
|||||||
{"setmetatable", luaB_setmetatable},
|
{"setmetatable", luaB_setmetatable},
|
||||||
{"tonumber", luaB_tonumber},
|
{"tonumber", luaB_tonumber},
|
||||||
{"tostring", luaB_tostring},
|
{"tostring", luaB_tostring},
|
||||||
|
{"type", luaB_type},
|
||||||
{"xpcall", luaB_xpcall},
|
{"xpcall", luaB_xpcall},
|
||||||
/* placeholders */
|
/* placeholders */
|
||||||
{"type", NULL},
|
|
||||||
{"_G", NULL},
|
{"_G", NULL},
|
||||||
{"_VERSION", NULL},
|
{"_VERSION", NULL},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
@@ -499,7 +484,6 @@ static const luaL_Reg base_funcs[] = {
|
|||||||
|
|
||||||
|
|
||||||
LUAMOD_API int luaopen_base (lua_State *L) {
|
LUAMOD_API int luaopen_base (lua_State *L) {
|
||||||
int i;
|
|
||||||
/* open lib into global table */
|
/* open lib into global table */
|
||||||
lua_pushglobaltable(L);
|
lua_pushglobaltable(L);
|
||||||
luaL_setfuncs(L, base_funcs, 0);
|
luaL_setfuncs(L, base_funcs, 0);
|
||||||
@@ -509,11 +493,6 @@ LUAMOD_API int luaopen_base (lua_State *L) {
|
|||||||
/* set global _VERSION */
|
/* set global _VERSION */
|
||||||
lua_pushliteral(L, LUA_VERSION);
|
lua_pushliteral(L, LUA_VERSION);
|
||||||
lua_setfield(L, -2, "_VERSION");
|
lua_setfield(L, -2, "_VERSION");
|
||||||
/* set function 'type' with proper upvalues */
|
|
||||||
for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */
|
|
||||||
lua_pushstring(L, lua_typename(L, i));
|
|
||||||
lua_pushcclosure(L, luaB_type, LUA_NUMTAGS);
|
|
||||||
lua_setfield(L, -2, "type");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
54
lbitlib.c
54
lbitlib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lbitlib.c,v 1.26 2014/05/15 19:28:34 roberto Exp roberto $
|
** $Id: lbitlib.c,v 1.29 2015/10/08 15:55:35 roberto Exp roberto $
|
||||||
** Standard library for bitwise operations
|
** Standard library for bitwise operations
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,6 +7,9 @@
|
|||||||
#define lbitlib_c
|
#define lbitlib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -16,6 +19,10 @@
|
|||||||
#if defined(LUA_COMPAT_BITLIB) /* { */
|
#if defined(LUA_COMPAT_BITLIB) /* { */
|
||||||
|
|
||||||
|
|
||||||
|
#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
||||||
|
#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i))
|
||||||
|
|
||||||
|
|
||||||
/* number of bits to consider in a number */
|
/* number of bits to consider in a number */
|
||||||
#if !defined(LUA_NBITS)
|
#if !defined(LUA_NBITS)
|
||||||
#define LUA_NBITS 32
|
#define LUA_NBITS 32
|
||||||
@@ -43,14 +50,14 @@ static lua_Unsigned andaux (lua_State *L) {
|
|||||||
int i, n = lua_gettop(L);
|
int i, n = lua_gettop(L);
|
||||||
lua_Unsigned r = ~(lua_Unsigned)0;
|
lua_Unsigned r = ~(lua_Unsigned)0;
|
||||||
for (i = 1; i <= n; i++)
|
for (i = 1; i <= n; i++)
|
||||||
r &= luaL_checkunsigned(L, i);
|
r &= checkunsigned(L, i);
|
||||||
return trim(r);
|
return trim(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int b_and (lua_State *L) {
|
static int b_and (lua_State *L) {
|
||||||
lua_Unsigned r = andaux(L);
|
lua_Unsigned r = andaux(L);
|
||||||
lua_pushunsigned(L, r);
|
pushunsigned(L, r);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,8 +73,8 @@ static int b_or (lua_State *L) {
|
|||||||
int i, n = lua_gettop(L);
|
int i, n = lua_gettop(L);
|
||||||
lua_Unsigned r = 0;
|
lua_Unsigned r = 0;
|
||||||
for (i = 1; i <= n; i++)
|
for (i = 1; i <= n; i++)
|
||||||
r |= luaL_checkunsigned(L, i);
|
r |= checkunsigned(L, i);
|
||||||
lua_pushunsigned(L, trim(r));
|
pushunsigned(L, trim(r));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,15 +83,15 @@ static int b_xor (lua_State *L) {
|
|||||||
int i, n = lua_gettop(L);
|
int i, n = lua_gettop(L);
|
||||||
lua_Unsigned r = 0;
|
lua_Unsigned r = 0;
|
||||||
for (i = 1; i <= n; i++)
|
for (i = 1; i <= n; i++)
|
||||||
r ^= luaL_checkunsigned(L, i);
|
r ^= checkunsigned(L, i);
|
||||||
lua_pushunsigned(L, trim(r));
|
pushunsigned(L, trim(r));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int b_not (lua_State *L) {
|
static int b_not (lua_State *L) {
|
||||||
lua_Unsigned r = ~luaL_checkunsigned(L, 1);
|
lua_Unsigned r = ~checkunsigned(L, 1);
|
||||||
lua_pushunsigned(L, trim(r));
|
pushunsigned(L, trim(r));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,23 +108,23 @@ static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {
|
|||||||
else r <<= i;
|
else r <<= i;
|
||||||
r = trim(r);
|
r = trim(r);
|
||||||
}
|
}
|
||||||
lua_pushunsigned(L, r);
|
pushunsigned(L, r);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int b_lshift (lua_State *L) {
|
static int b_lshift (lua_State *L) {
|
||||||
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2));
|
return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int b_rshift (lua_State *L) {
|
static int b_rshift (lua_State *L) {
|
||||||
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2));
|
return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int b_arshift (lua_State *L) {
|
static int b_arshift (lua_State *L) {
|
||||||
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
lua_Unsigned r = checkunsigned(L, 1);
|
||||||
lua_Integer i = luaL_checkinteger(L, 2);
|
lua_Integer i = luaL_checkinteger(L, 2);
|
||||||
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
|
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
|
||||||
return b_shift(L, r, -i);
|
return b_shift(L, r, -i);
|
||||||
@@ -125,19 +132,19 @@ static int b_arshift (lua_State *L) {
|
|||||||
if (i >= LUA_NBITS) r = ALLONES;
|
if (i >= LUA_NBITS) r = ALLONES;
|
||||||
else
|
else
|
||||||
r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
|
r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
|
||||||
lua_pushunsigned(L, r);
|
pushunsigned(L, r);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int b_rot (lua_State *L, lua_Integer d) {
|
static int b_rot (lua_State *L, lua_Integer d) {
|
||||||
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
lua_Unsigned r = checkunsigned(L, 1);
|
||||||
int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
|
int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
|
||||||
r = trim(r);
|
r = trim(r);
|
||||||
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
|
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
|
||||||
r = (r << i) | (r >> (LUA_NBITS - i));
|
r = (r << i) | (r >> (LUA_NBITS - i));
|
||||||
lua_pushunsigned(L, trim(r));
|
pushunsigned(L, trim(r));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,23 +179,22 @@ static int fieldargs (lua_State *L, int farg, int *width) {
|
|||||||
|
|
||||||
static int b_extract (lua_State *L) {
|
static int b_extract (lua_State *L) {
|
||||||
int w;
|
int w;
|
||||||
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
|
lua_Unsigned r = trim(checkunsigned(L, 1));
|
||||||
int f = fieldargs(L, 2, &w);
|
int f = fieldargs(L, 2, &w);
|
||||||
r = (r >> f) & mask(w);
|
r = (r >> f) & mask(w);
|
||||||
lua_pushunsigned(L, r);
|
pushunsigned(L, r);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int b_replace (lua_State *L) {
|
static int b_replace (lua_State *L) {
|
||||||
int w;
|
int w;
|
||||||
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
|
lua_Unsigned r = trim(checkunsigned(L, 1));
|
||||||
lua_Unsigned v = luaL_checkunsigned(L, 2);
|
lua_Unsigned v = trim(checkunsigned(L, 2));
|
||||||
int f = fieldargs(L, 3, &w);
|
int f = fieldargs(L, 3, &w);
|
||||||
int m = mask(w);
|
lua_Unsigned m = mask(w);
|
||||||
v &= m; /* erase bits outside given width */
|
r = (r & ~(m << f)) | ((v & m) << f);
|
||||||
r = (r & ~(m << f)) | (v << f);
|
pushunsigned(L, r);
|
||||||
lua_pushunsigned(L, r);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
lcode.h
5
lcode.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lcode.h,v 1.62 2013/12/18 14:12:03 roberto Exp roberto $
|
** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 roberto Exp roberto $
|
||||||
** Code generator for Lua
|
** Code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -40,7 +40,8 @@ typedef enum BinOpr {
|
|||||||
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
||||||
|
|
||||||
|
|
||||||
#define getcode(fs,e) ((fs)->f->code[(e)->u.info])
|
/* get (pointer to) instruction of given 'expdesc' */
|
||||||
|
#define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
|
||||||
|
|
||||||
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
|
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
|
||||||
|
|
||||||
|
|||||||
15
lcorolib.c
15
lcorolib.c
@@ -1,16 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lcorolib.c,v 1.6 2014/05/08 13:52:20 roberto Exp roberto $
|
** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp roberto $
|
||||||
** Coroutine Library
|
** Coroutine Library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define lcorolib_c
|
#define lcorolib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -65,7 +66,7 @@ static int luaB_coresume (lua_State *L) {
|
|||||||
else {
|
else {
|
||||||
lua_pushboolean(L, 1);
|
lua_pushboolean(L, 1);
|
||||||
lua_insert(L, -(r + 1));
|
lua_insert(L, -(r + 1));
|
||||||
return r + 1; /* return true + `resume' returns */
|
return r + 1; /* return true + 'resume' returns */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +75,7 @@ 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 (r < 0) {
|
||||||
if (lua_isstring(L, -1)) { /* error object is a string? */
|
if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */
|
||||||
luaL_where(L, 1); /* add extra info */
|
luaL_where(L, 1); /* add extra info */
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
lua_concat(L, 2);
|
lua_concat(L, 2);
|
||||||
|
|||||||
5
lctype.c
5
lctype.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lctype.c,v 1.10 2011/06/24 12:25:33 roberto Exp roberto $
|
** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp roberto $
|
||||||
** 'ctype' functions for Lua
|
** 'ctype' functions for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,6 +7,9 @@
|
|||||||
#define lctype_c
|
#define lctype_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include "lctype.h"
|
#include "lctype.h"
|
||||||
|
|
||||||
#if !LUA_USE_CTYPE /* { */
|
#if !LUA_USE_CTYPE /* { */
|
||||||
|
|||||||
74
ldblib.c
74
ldblib.c
@@ -1,24 +1,41 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldblib.c,v 1.142 2014/10/01 11:54:56 roberto Exp roberto $
|
** $Id: ldblib.c,v 1.150 2015/11/19 19:16:22 roberto Exp roberto $
|
||||||
** Interface from Lua to its debug API
|
** Interface from Lua to its debug API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ldblib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define ldblib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
#define HOOKKEY "_HKEY"
|
/*
|
||||||
|
** The hook table at registry[&HOOKKEY] maps threads to their current
|
||||||
|
** hook function. (We only need the unique address of 'HOOKKEY'.)
|
||||||
|
*/
|
||||||
|
static const int HOOKKEY = 0;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If L1 != L, L1 can be in any state, and therefore there are no
|
||||||
|
** guarantees about its stack space; any push in L1 must be
|
||||||
|
** checked.
|
||||||
|
*/
|
||||||
|
static void checkstack (lua_State *L, lua_State *L1, int n) {
|
||||||
|
if (L != L1 && !lua_checkstack(L1, n))
|
||||||
|
luaL_error(L, "stack overflow");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int db_getregistry (lua_State *L) {
|
static int db_getregistry (lua_State *L) {
|
||||||
@@ -121,12 +138,16 @@ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Calls 'lua_getinfo' and collects all results in a new table.
|
** Calls 'lua_getinfo' and collects all results in a new table.
|
||||||
|
** L1 needs stack space for an optional input (function) plus
|
||||||
|
** two optional outputs (function and line table) from function
|
||||||
|
** 'lua_getinfo'.
|
||||||
*/
|
*/
|
||||||
static int db_getinfo (lua_State *L) {
|
static int db_getinfo (lua_State *L) {
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int arg;
|
int arg;
|
||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
const char *options = luaL_optstring(L, arg+2, "flnStu");
|
const char *options = luaL_optstring(L, arg+2, "flnStu");
|
||||||
|
checkstack(L, L1, 3);
|
||||||
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 */
|
||||||
@@ -184,6 +205,7 @@ static int db_getlocal (lua_State *L) {
|
|||||||
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 (!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);
|
||||||
name = lua_getlocal(L1, &ar, nvar);
|
name = lua_getlocal(L1, &ar, nvar);
|
||||||
if (name) {
|
if (name) {
|
||||||
lua_xmove(L1, L, 1); /* move local value */
|
lua_xmove(L1, L, 1); /* move local value */
|
||||||
@@ -201,15 +223,21 @@ static int db_getlocal (lua_State *L) {
|
|||||||
|
|
||||||
static int db_setlocal (lua_State *L) {
|
static int db_setlocal (lua_State *L) {
|
||||||
int arg;
|
int arg;
|
||||||
|
const char *name;
|
||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
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);
|
||||||
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
if (!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);
|
||||||
|
checkstack(L, L1, 1);
|
||||||
lua_xmove(L, L1, 1);
|
lua_xmove(L, L1, 1);
|
||||||
lua_pushstring(L, lua_setlocal(L1, &ar, (int)luaL_checkinteger(L, arg+2)));
|
name = lua_setlocal(L1, &ar, nvar);
|
||||||
|
if (name == NULL)
|
||||||
|
lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
|
||||||
|
lua_pushstring(L, name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,13 +298,6 @@ static int db_upvaluejoin (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** The hook table (at registry[HOOKKEY]) maps threads to their current
|
|
||||||
** hook function
|
|
||||||
*/
|
|
||||||
#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Call hook function registered at hook table for the current
|
** Call hook function registered at hook table for the current
|
||||||
** thread (if there is one)
|
** thread (if there is one)
|
||||||
@@ -284,7 +305,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"};
|
||||||
gethooktable(L);
|
lua_rawgetp(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 */
|
||||||
@@ -337,16 +358,20 @@ 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 (gethooktable(L) == 0) { /* creating hook table? */
|
if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
|
||||||
|
lua_createtable(L, 0, 2); /* create a hook table */
|
||||||
|
lua_pushvalue(L, -1);
|
||||||
|
lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
|
||||||
lua_pushstring(L, "k");
|
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); /* setmetatable(hooktable) = hooktable */
|
||||||
}
|
}
|
||||||
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key */
|
checkstack(L, L1, 1);
|
||||||
lua_pushvalue(L, arg+1); /* value */
|
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
|
||||||
|
lua_pushvalue(L, arg + 1); /* value (hook function) */
|
||||||
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
|
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
|
||||||
lua_sethook(L1, func, mask, count); /* set hooks */
|
lua_sethook(L1, func, mask, count);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,10 +382,13 @@ 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 && hook != hookf) /* external hook? */
|
if (hook == NULL) /* no hook? */
|
||||||
|
lua_pushnil(L);
|
||||||
|
else if (hook != hookf) /* external hook? */
|
||||||
lua_pushliteral(L, "external hook");
|
lua_pushliteral(L, "external hook");
|
||||||
else {
|
else { /* hook table must exist */
|
||||||
gethooktable(L);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||||
|
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] */
|
||||||
lua_remove(L, -2); /* remove hook table */
|
lua_remove(L, -2); /* remove hook table */
|
||||||
@@ -374,13 +402,13 @@ static int db_gethook (lua_State *L) {
|
|||||||
static int db_debug (lua_State *L) {
|
static int db_debug (lua_State *L) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char buffer[250];
|
char buffer[250];
|
||||||
luai_writestringerror("%s", "lua_debug> ");
|
lua_writestringerror("%s", "lua_debug> ");
|
||||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||||
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))
|
||||||
luai_writestringerror("%s\n", lua_tostring(L, -1));
|
lua_writestringerror("%s\n", lua_tostring(L, -1));
|
||||||
lua_settop(L, 0); /* remove eventual returns */
|
lua_settop(L, 0); /* remove eventual returns */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
169
ldebug.c
169
ldebug.c
@@ -1,18 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldebug.c,v 2.100 2014/07/30 14:00:14 roberto Exp roberto $
|
** $Id: ldebug.c,v 2.119 2016/02/26 19:20:15 roberto Exp roberto $
|
||||||
** Debug Interface
|
** Debug Interface
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ldebug_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
#define ldebug_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -33,6 +34,10 @@
|
|||||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
||||||
|
|
||||||
|
|
||||||
|
/* Active Lua function (given call info) */
|
||||||
|
#define ci_func(ci) (clLvalue((ci)->func))
|
||||||
|
|
||||||
|
|
||||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
|
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
|
||||||
|
|
||||||
|
|
||||||
@@ -48,7 +53,29 @@ static int currentline (CallInfo *ci) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** this function can be called asynchronous (e.g. during a signal)
|
** If function yielded, its 'func' can be in the 'extra' field. The
|
||||||
|
** next function restores 'func' to its correct value for debugging
|
||||||
|
** purposes. (It exchanges 'func' and 'extra'; so, when called again,
|
||||||
|
** after debugging, it also "re-restores" ** 'func' to its altered value.
|
||||||
|
*/
|
||||||
|
static void swapextra (lua_State *L) {
|
||||||
|
if (L->status == LUA_YIELD) {
|
||||||
|
CallInfo *ci = L->ci; /* get function that yielded */
|
||||||
|
StkId temp = ci->func; /* exchange its 'func' and 'extra' values */
|
||||||
|
ci->func = restorestack(L, ci->extra);
|
||||||
|
ci->extra = savestack(L, temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** This function can be called asynchronously (e.g. during a signal).
|
||||||
|
** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
|
||||||
|
** 'resethookcount') are for debug only, and it is no problem if they
|
||||||
|
** get arbitrary values (causes at most one wrong hook call). 'hookmask'
|
||||||
|
** is an atomic value. We assume that pointers are atomic too (e.g., gcc
|
||||||
|
** ensures that for all platforms where it runs). Moreover, 'hook' is
|
||||||
|
** always checked 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? */
|
||||||
@@ -105,7 +132,7 @@ static const char *upvalname (Proto *p, int uv) {
|
|||||||
|
|
||||||
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
||||||
int nparams = clLvalue(ci->func)->p->numparams;
|
int nparams = clLvalue(ci->func)->p->numparams;
|
||||||
if (n >= ci->u.l.base - ci->func - nparams)
|
if (n >= cast_int(ci->u.l.base - ci->func) - nparams)
|
||||||
return NULL; /* no such vararg */
|
return NULL; /* no such vararg */
|
||||||
else {
|
else {
|
||||||
*pos = ci->func + nparams + n;
|
*pos = ci->func + nparams + n;
|
||||||
@@ -143,6 +170,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
|
|||||||
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
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);
|
||||||
|
swapextra(L);
|
||||||
if (ar == NULL) { /* information about non-active function? */
|
if (ar == NULL) { /* information about non-active function? */
|
||||||
if (!isLfunction(L->top - 1)) /* not a Lua function? */
|
if (!isLfunction(L->top - 1)) /* not a Lua function? */
|
||||||
name = NULL;
|
name = NULL;
|
||||||
@@ -150,25 +178,30 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
|||||||
name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
|
name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
|
||||||
}
|
}
|
||||||
else { /* active function; get information through 'ar' */
|
else { /* active function; get information through 'ar' */
|
||||||
StkId pos = 0; /* to avoid warnings */
|
StkId pos = NULL; /* to avoid warnings */
|
||||||
name = findlocal(L, ar->i_ci, n, &pos);
|
name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
if (name) {
|
if (name) {
|
||||||
setobj2s(L, L->top, pos);
|
setobj2s(L, L->top, pos);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
swapextra(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||||
StkId pos = 0; /* to avoid warnings */
|
StkId pos = NULL; /* to avoid warnings */
|
||||||
const char *name = findlocal(L, ar->i_ci, n, &pos);
|
const char *name;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (name)
|
swapextra(L);
|
||||||
|
name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
|
if (name) {
|
||||||
setobjs2s(L, pos, L->top - 1);
|
setobjs2s(L, pos, L->top - 1);
|
||||||
L->top--; /* pop value */
|
L->top--; /* pop value */
|
||||||
|
}
|
||||||
|
swapextra(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@@ -268,10 +301,11 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
|||||||
CallInfo *ci;
|
CallInfo *ci;
|
||||||
StkId func;
|
StkId func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
|
swapextra(L);
|
||||||
if (*what == '>') {
|
if (*what == '>') {
|
||||||
ci = NULL;
|
ci = NULL;
|
||||||
func = L->top - 1;
|
func = L->top - 1;
|
||||||
api_check(ttisfunction(func), "function expected");
|
api_check(L, ttisfunction(func), "function expected");
|
||||||
what++; /* skip the '>' */
|
what++; /* skip the '>' */
|
||||||
L->top--; /* pop function */
|
L->top--; /* pop function */
|
||||||
}
|
}
|
||||||
@@ -286,6 +320,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
|||||||
setobjs2s(L, L->top, func);
|
setobjs2s(L, L->top, func);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
|
swapextra(L); /* correct before option 'L', which can raise a mem. error */
|
||||||
if (strchr(what, 'L'))
|
if (strchr(what, 'L'))
|
||||||
collectvalidlines(L, cl);
|
collectvalidlines(L, cl);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -365,7 +400,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) {
|
|||||||
case OP_JMP: {
|
case OP_JMP: {
|
||||||
int b = GETARG_sBx(i);
|
int b = GETARG_sBx(i);
|
||||||
int dest = pc + 1 + b;
|
int dest = pc + 1 + b;
|
||||||
/* jump is forward and do not skip `lastpc'? */
|
/* jump is forward and do not skip 'lastpc'? */
|
||||||
if (pc < dest && dest <= lastpc) {
|
if (pc < dest && dest <= lastpc) {
|
||||||
if (dest > jmptarget)
|
if (dest > jmptarget)
|
||||||
jmptarget = dest; /* update 'jmptarget' */
|
jmptarget = dest; /* update 'jmptarget' */
|
||||||
@@ -437,10 +472,14 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
|
|||||||
|
|
||||||
|
|
||||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
||||||
TMS tm;
|
TMS tm = (TMS)0; /* to avoid warnings */
|
||||||
Proto *p = ci_func(ci)->p; /* calling function */
|
Proto *p = ci_func(ci)->p; /* calling function */
|
||||||
int pc = currentpc(ci); /* calling instruction index */
|
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: /* get function name */
|
case OP_TAILCALL: /* get function name */
|
||||||
@@ -450,26 +489,27 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
|||||||
return "for iterator";
|
return "for iterator";
|
||||||
}
|
}
|
||||||
/* all other instructions can call only through metamethods */
|
/* all other instructions can call only through metamethods */
|
||||||
case OP_SELF:
|
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
|
||||||
case OP_GETTABUP:
|
tm = TM_INDEX;
|
||||||
case OP_GETTABLE: tm = TM_INDEX; break;
|
break;
|
||||||
case OP_SETTABUP:
|
case OP_SETTABUP: case OP_SETTABLE:
|
||||||
case OP_SETTABLE: tm = TM_NEWINDEX; break;
|
tm = TM_NEWINDEX;
|
||||||
case OP_EQ: tm = TM_EQ; break;
|
break;
|
||||||
case OP_ADD: tm = TM_ADD; break;
|
case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
|
||||||
case OP_SUB: tm = TM_SUB; break;
|
case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
|
||||||
case OP_MUL: tm = TM_MUL; break;
|
case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
|
||||||
case OP_DIV: tm = TM_DIV; break;
|
int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */
|
||||||
case OP_IDIV: tm = TM_IDIV; break;
|
tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */
|
||||||
case OP_MOD: tm = TM_MOD; break;
|
break;
|
||||||
case OP_POW: tm = TM_POW; break;
|
}
|
||||||
case OP_UNM: tm = TM_UNM; break;
|
case OP_UNM: tm = TM_UNM; break;
|
||||||
|
case OP_BNOT: tm = TM_BNOT; break;
|
||||||
case OP_LEN: tm = TM_LEN; break;
|
case OP_LEN: tm = TM_LEN; break;
|
||||||
|
case OP_CONCAT: tm = TM_CONCAT; break;
|
||||||
|
case OP_EQ: tm = TM_EQ; break;
|
||||||
case OP_LT: tm = TM_LT; break;
|
case OP_LT: tm = TM_LT; break;
|
||||||
case OP_LE: tm = TM_LE; break;
|
case OP_LE: tm = TM_LE; break;
|
||||||
case OP_CONCAT: tm = TM_CONCAT; break;
|
default: lua_assert(0); /* other instructions cannot call a function */
|
||||||
default:
|
|
||||||
return NULL; /* else no useful name can be found */
|
|
||||||
}
|
}
|
||||||
*name = getstr(G(L)->tmname[tm]);
|
*name = getstr(G(L)->tmname[tm]);
|
||||||
return "metamethod";
|
return "metamethod";
|
||||||
@@ -480,17 +520,21 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** only ANSI way to check whether a pointer points to an array
|
** The subtraction of two potentially unrelated pointers is
|
||||||
** (used only for error messages, so efficiency is not a big concern)
|
** not ISO C, but it should not crash a program; the subsequent
|
||||||
|
** checks are ISO C and ensure a correct result.
|
||||||
*/
|
*/
|
||||||
static int isinstack (CallInfo *ci, const TValue *o) {
|
static int isinstack (CallInfo *ci, const TValue *o) {
|
||||||
StkId p;
|
ptrdiff_t i = o - ci->u.l.base;
|
||||||
for (p = ci->u.l.base; p < ci->top; p++)
|
return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o);
|
||||||
if (o == p) return 1;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Checks whether value 'o' came from an upvalue. (That can only happen
|
||||||
|
** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
|
||||||
|
** upvalues.)
|
||||||
|
*/
|
||||||
static const char *getupvalname (CallInfo *ci, const TValue *o,
|
static const char *getupvalname (CallInfo *ci, const TValue *o,
|
||||||
const char **name) {
|
const char **name) {
|
||||||
LClosure *c = ci_func(ci);
|
LClosure *c = ci_func(ci);
|
||||||
@@ -506,7 +550,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
|
|||||||
|
|
||||||
|
|
||||||
static const char *varinfo (lua_State *L, const TValue *o) {
|
static const char *varinfo (lua_State *L, const TValue *o) {
|
||||||
const char *name;
|
const char *name = NULL; /* to avoid warnings */
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
const char *kind = NULL;
|
const char *kind = NULL;
|
||||||
if (isLua(ci)) {
|
if (isLua(ci)) {
|
||||||
@@ -520,7 +564,7 @@ static const char *varinfo (lua_State *L, const TValue *o) {
|
|||||||
|
|
||||||
|
|
||||||
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
|
||||||
const char *t = objtypename(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, varinfo(L, o));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,11 +575,12 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
|
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
|
||||||
|
const TValue *p2, const char *msg) {
|
||||||
lua_Number temp;
|
lua_Number temp;
|
||||||
if (!tonumber(p1, &temp)) /* first operand is wrong? */
|
if (!tonumber(p1, &temp)) /* first operand is wrong? */
|
||||||
p2 = p1; /* now second is wrong */
|
p2 = p1; /* now second is wrong */
|
||||||
luaG_typeerror(L, p2, "perform arithmetic on");
|
luaG_typeerror(L, p2, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -551,49 +596,49 @@ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
|
|||||||
|
|
||||||
|
|
||||||
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
|
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||||
const char *t1 = objtypename(p1);
|
const char *t1 = luaT_objtypename(L, p1);
|
||||||
const char *t2 = objtypename(p2);
|
const char *t2 = luaT_objtypename(L, p2);
|
||||||
if (t1 == t2)
|
if (strcmp(t1, t2) == 0)
|
||||||
luaG_runerror(L, "attempt to compare two %s values", t1);
|
luaG_runerror(L, "attempt to compare two %s values", t1);
|
||||||
else
|
else
|
||||||
luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
|
luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void addinfo (lua_State *L, const char *msg) {
|
/* add src:line information to 'msg' */
|
||||||
CallInfo *ci = L->ci;
|
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
|
||||||
if (isLua(ci)) { /* is Lua code? */
|
int line) {
|
||||||
char buff[LUA_IDSIZE]; /* add file:line information */
|
char buff[LUA_IDSIZE];
|
||||||
int line = currentline(ci);
|
if (src)
|
||||||
TString *src = ci_func(ci)->p->source;
|
luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
|
||||||
if (src)
|
else { /* no source available; use "?" instead */
|
||||||
luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
|
buff[0] = '?'; buff[1] = '\0';
|
||||||
else { /* no source available; use "?" instead */
|
|
||||||
buff[0] = '?'; buff[1] = '\0';
|
|
||||||
}
|
|
||||||
luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
|
||||||
}
|
}
|
||||||
|
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
l_noret luaG_errormsg (lua_State *L) {
|
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);
|
||||||
if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
|
|
||||||
setobjs2s(L, L->top, L->top - 1); /* move argument */
|
setobjs2s(L, L->top, L->top - 1); /* move argument */
|
||||||
setobjs2s(L, L->top - 1, errfunc); /* push function */
|
setobjs2s(L, L->top - 1, errfunc); /* push function */
|
||||||
L->top++;
|
L->top++; /* assume EXTRA_STACK */
|
||||||
luaD_call(L, L->top - 2, 1, 0); /* call it */
|
luaD_callnoyield(L, L->top - 2, 1); /* call it */
|
||||||
}
|
}
|
||||||
luaD_throw(L, LUA_ERRRUN);
|
luaD_throw(L, LUA_ERRRUN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||||
|
CallInfo *ci = L->ci;
|
||||||
|
const char *msg;
|
||||||
va_list argp;
|
va_list argp;
|
||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
addinfo(L, luaO_pushvfstring(L, fmt, argp));
|
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
|
||||||
va_end(argp);
|
va_end(argp);
|
||||||
|
if (isLua(ci)) /* if Lua function, add source:line information */
|
||||||
|
luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
|
||||||
luaG_errormsg(L);
|
luaG_errormsg(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -601,9 +646,11 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
|||||||
void luaG_traceexec (lua_State *L) {
|
void luaG_traceexec (lua_State *L) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
lu_byte mask = L->hookmask;
|
lu_byte mask = L->hookmask;
|
||||||
int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
|
int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
|
||||||
if (counthook)
|
if (counthook)
|
||||||
resethookcount(L); /* reset count */
|
resethookcount(L); /* reset count */
|
||||||
|
else if (!(mask & LUA_MASKLINE))
|
||||||
|
return; /* no line hook and count != 0; nothing to be done */
|
||||||
if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
|
if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
|
||||||
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
|
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
|
||||||
return; /* do not call hook again (VM yielded, so it did not move) */
|
return; /* do not call hook again (VM yielded, so it did not move) */
|
||||||
|
|||||||
12
ldebug.h
12
ldebug.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldebug.h,v 2.10 2013/05/06 17:19:11 roberto Exp roberto $
|
** $Id: ldebug.h,v 2.13 2015/03/11 16:10:41 roberto Exp roberto $
|
||||||
** Auxiliary functions from Debug Interface module
|
** Auxiliary functions from Debug Interface module
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -17,21 +17,21 @@
|
|||||||
|
|
||||||
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
||||||
|
|
||||||
/* Active Lua function (given call info) */
|
|
||||||
#define ci_func(ci) (clLvalue((ci)->func))
|
|
||||||
|
|
||||||
|
|
||||||
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_concaterror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2,
|
||||||
|
const char *msg);
|
||||||
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
||||||
|
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
|
||||||
|
TString *src, int line);
|
||||||
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
||||||
LUAI_FUNC void luaG_traceexec (lua_State *L);
|
LUAI_FUNC void luaG_traceexec (lua_State *L);
|
||||||
|
|
||||||
|
|||||||
247
ldo.c
247
ldo.c
@@ -1,17 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldo.c,v 2.129 2014/10/08 12:20:26 roberto Exp roberto $
|
** $Id: ldo.c,v 2.150 2015/11/19 19:16:22 roberto Exp roberto $
|
||||||
** Stack and Call structure of Lua
|
** Stack and Call structure of Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ldo_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define ldo_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -67,7 +69,7 @@
|
|||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI handling with long jumps */
|
/* ISO C handling with long jumps */
|
||||||
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
|
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
|
||||||
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
|
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
|
||||||
#define luai_jmpbuf jmp_buf
|
#define luai_jmpbuf jmp_buf
|
||||||
@@ -119,8 +121,11 @@ l_noret luaD_throw (lua_State *L, int errcode) {
|
|||||||
}
|
}
|
||||||
else { /* no handler at all; abort */
|
else { /* no handler at all; abort */
|
||||||
if (g->panic) { /* panic function? */
|
if (g->panic) { /* panic function? */
|
||||||
|
seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */
|
||||||
|
if (L->ci->top < L->top)
|
||||||
|
L->ci->top = L->top; /* pushing msg. can break this invariant */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
g->panic(L); /* call it (last chance to jump out) */
|
g->panic(L); /* call panic function (last chance to jump out) */
|
||||||
}
|
}
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
@@ -145,6 +150,11 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
|||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Stack reallocation
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
static void correctstack (lua_State *L, TValue *oldstack) {
|
static void correctstack (lua_State *L, TValue *oldstack) {
|
||||||
CallInfo *ci;
|
CallInfo *ci;
|
||||||
UpVal *up;
|
UpVal *up;
|
||||||
@@ -216,17 +226,30 @@ void luaD_shrinkstack (lua_State *L) {
|
|||||||
luaE_freeCI(L); /* free all CIs (list grew because of an error) */
|
luaE_freeCI(L); /* free all CIs (list grew because of an error) */
|
||||||
else
|
else
|
||||||
luaE_shrinkCI(L); /* shrink list */
|
luaE_shrinkCI(L); /* shrink list */
|
||||||
if (inuse > LUAI_MAXSTACK || /* still handling stack overflow? */
|
if (inuse <= LUAI_MAXSTACK && /* not handling stack overflow? */
|
||||||
goodsize >= L->stacksize) /* would grow instead of shrink? */
|
goodsize < L->stacksize) /* trying to shrink? */
|
||||||
condmovestack(L); /* don't change stack (change only for debugging) */
|
|
||||||
else
|
|
||||||
luaD_reallocstack(L, goodsize); /* shrink it */
|
luaD_reallocstack(L, goodsize); /* shrink it */
|
||||||
|
else
|
||||||
|
condmovestack(L,,); /* don't change stack (change only for debugging) */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaD_inctop (lua_State *L) {
|
||||||
|
luaD_checkstack(L, 1);
|
||||||
|
L->top++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Call a hook for the given event. Make sure there is a hook to be
|
||||||
|
** called. (Both 'L->hook' and 'L->hookmask', which triggers this
|
||||||
|
** function, can be changed asynchronously by signals.)
|
||||||
|
*/
|
||||||
void luaD_hook (lua_State *L, int event, int line) {
|
void luaD_hook (lua_State *L, int event, int line) {
|
||||||
lua_Hook hook = L->hook;
|
lua_Hook hook = L->hook;
|
||||||
if (hook && L->allowhook) {
|
if (hook && L->allowhook) { /* make sure there is a hook */
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
ptrdiff_t top = savestack(L, L->top);
|
ptrdiff_t top = savestack(L, L->top);
|
||||||
ptrdiff_t ci_top = savestack(L, ci->top);
|
ptrdiff_t ci_top = savestack(L, ci->top);
|
||||||
@@ -268,31 +291,34 @@ static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
|
|||||||
int i;
|
int i;
|
||||||
int nfixargs = p->numparams;
|
int nfixargs = p->numparams;
|
||||||
StkId base, fixed;
|
StkId base, fixed;
|
||||||
lua_assert(actual >= nfixargs);
|
|
||||||
/* move fixed parameters to final position */
|
/* move fixed parameters to final position */
|
||||||
luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */
|
|
||||||
fixed = L->top - actual; /* first fixed argument */
|
fixed = L->top - actual; /* first fixed argument */
|
||||||
base = L->top; /* final position of first argument */
|
base = L->top; /* final position of first argument */
|
||||||
for (i=0; i<nfixargs; i++) {
|
for (i = 0; i < nfixargs && i < actual; i++) {
|
||||||
setobjs2s(L, L->top++, fixed + i);
|
setobjs2s(L, L->top++, fixed + i);
|
||||||
setnilvalue(fixed + i);
|
setnilvalue(fixed + i); /* erase original copy (for GC) */
|
||||||
}
|
}
|
||||||
|
for (; i < nfixargs; i++)
|
||||||
|
setnilvalue(L->top++); /* complete missing arguments */
|
||||||
return base;
|
return base;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static StkId tryfuncTM (lua_State *L, StkId func) {
|
/*
|
||||||
|
** Check whether __call metafield of 'func' is a function. If so, put
|
||||||
|
** it in stack below original 'func' so that 'luaD_precall' can call
|
||||||
|
** it. Raise an error if __call metafield is not a function.
|
||||||
|
*/
|
||||||
|
static void tryfuncTM (lua_State *L, StkId func) {
|
||||||
const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
|
const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
|
||||||
StkId p;
|
StkId p;
|
||||||
ptrdiff_t funcr = savestack(L, func);
|
|
||||||
if (!ttisfunction(tm))
|
if (!ttisfunction(tm))
|
||||||
luaG_typeerror(L, func, "call");
|
luaG_typeerror(L, func, "call");
|
||||||
/* Open a hole inside the stack at `func' */
|
/* Open a hole inside the stack at 'func' */
|
||||||
for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
|
for (p = L->top; p > func; p--)
|
||||||
incr_top(L);
|
setobjs2s(L, p, p-1);
|
||||||
func = restorestack(L, funcr); /* previous call may change stack */
|
L->top++; /* slot ensured by caller */
|
||||||
setobj2s(L, func, tm); /* tag method is the new function to be called */
|
setobj2s(L, func, tm); /* tag method is the new function to be called */
|
||||||
return func;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -300,79 +326,133 @@ static StkId tryfuncTM (lua_State *L, StkId func) {
|
|||||||
#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
|
#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
|
||||||
|
|
||||||
|
|
||||||
|
/* macro to check stack size, preserving 'p' */
|
||||||
|
#define checkstackp(L,n,p) \
|
||||||
|
luaD_checkstackaux(L, n, \
|
||||||
|
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
|
||||||
|
luaC_checkGC(L), /* stack grow uses memory */ \
|
||||||
|
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns true if function has been executed (C function)
|
** Prepares a function call: checks the stack, creates a new CallInfo
|
||||||
|
** entry, fills in the relevant information, calls hook if needed.
|
||||||
|
** If function is a C function, does the call, too. (Otherwise, leave
|
||||||
|
** the execution ('luaV_execute') to the caller, to allow stackless
|
||||||
|
** calls.) Returns true iff function has been executed (C function).
|
||||||
*/
|
*/
|
||||||
int luaD_precall (lua_State *L, StkId func, int nresults) {
|
int luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||||
lua_CFunction f;
|
lua_CFunction f;
|
||||||
CallInfo *ci;
|
CallInfo *ci;
|
||||||
int n; /* number of arguments (Lua) or returns (C) */
|
|
||||||
ptrdiff_t funcr = savestack(L, func);
|
|
||||||
switch (ttype(func)) {
|
switch (ttype(func)) {
|
||||||
|
case LUA_TCCL: /* C closure */
|
||||||
|
f = clCvalue(func)->f;
|
||||||
|
goto Cfunc;
|
||||||
case LUA_TLCF: /* light C function */
|
case LUA_TLCF: /* light C function */
|
||||||
f = fvalue(func);
|
f = fvalue(func);
|
||||||
goto Cfunc;
|
Cfunc: {
|
||||||
case LUA_TCCL: { /* C closure */
|
int n; /* number of returns */
|
||||||
f = clCvalue(func)->f;
|
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
|
||||||
Cfunc:
|
|
||||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
|
||||||
ci = next_ci(L); /* now 'enter' new function */
|
ci = next_ci(L); /* now 'enter' new function */
|
||||||
ci->nresults = nresults;
|
ci->nresults = nresults;
|
||||||
ci->func = restorestack(L, funcr);
|
ci->func = func;
|
||||||
ci->top = L->top + LUA_MINSTACK;
|
ci->top = L->top + LUA_MINSTACK;
|
||||||
lua_assert(ci->top <= L->stack_last);
|
lua_assert(ci->top <= L->stack_last);
|
||||||
ci->callstatus = 0;
|
ci->callstatus = 0;
|
||||||
luaC_checkGC(L); /* stack grow uses memory */
|
|
||||||
if (L->hookmask & LUA_MASKCALL)
|
if (L->hookmask & LUA_MASKCALL)
|
||||||
luaD_hook(L, LUA_HOOKCALL, -1);
|
luaD_hook(L, LUA_HOOKCALL, -1);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
n = (*f)(L); /* do the actual call */
|
n = (*f)(L); /* do the actual call */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
luaD_poscall(L, L->top - n);
|
luaD_poscall(L, ci, L->top - n, n);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case LUA_TLCL: { /* Lua function: prepare its call */
|
case LUA_TLCL: { /* Lua function: prepare its call */
|
||||||
StkId base;
|
StkId base;
|
||||||
Proto *p = clLvalue(func)->p;
|
Proto *p = clLvalue(func)->p;
|
||||||
n = cast_int(L->top - func) - 1; /* number of real arguments */
|
int n = cast_int(L->top - func) - 1; /* number of real arguments */
|
||||||
luaD_checkstack(L, p->maxstacksize);
|
int fsize = p->maxstacksize; /* frame size */
|
||||||
for (; n < p->numparams; n++)
|
checkstackp(L, fsize, func);
|
||||||
setnilvalue(L->top++); /* complete missing arguments */
|
if (p->is_vararg != 1) { /* do not use vararg? */
|
||||||
if (!p->is_vararg) {
|
for (; n < p->numparams; n++)
|
||||||
func = restorestack(L, funcr);
|
setnilvalue(L->top++); /* complete missing arguments */
|
||||||
base = func + 1;
|
base = func + 1;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
base = adjust_varargs(L, p, n);
|
base = adjust_varargs(L, p, n);
|
||||||
func = restorestack(L, funcr); /* previous call can change stack */
|
|
||||||
}
|
|
||||||
ci = next_ci(L); /* now 'enter' new function */
|
ci = next_ci(L); /* now 'enter' new function */
|
||||||
ci->nresults = nresults;
|
ci->nresults = nresults;
|
||||||
ci->func = func;
|
ci->func = func;
|
||||||
ci->u.l.base = base;
|
ci->u.l.base = base;
|
||||||
ci->top = base + p->maxstacksize;
|
L->top = ci->top = base + fsize;
|
||||||
lua_assert(ci->top <= L->stack_last);
|
lua_assert(ci->top <= L->stack_last);
|
||||||
ci->u.l.savedpc = p->code; /* starting point */
|
ci->u.l.savedpc = p->code; /* starting point */
|
||||||
ci->callstatus = CIST_LUA;
|
ci->callstatus = CIST_LUA;
|
||||||
L->top = ci->top;
|
|
||||||
luaC_checkGC(L); /* stack grow uses memory */
|
|
||||||
if (L->hookmask & LUA_MASKCALL)
|
if (L->hookmask & LUA_MASKCALL)
|
||||||
callhook(L, ci);
|
callhook(L, ci);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
default: { /* not a function */
|
default: { /* not a function */
|
||||||
func = tryfuncTM(L, func); /* retry with 'function' tag method */
|
checkstackp(L, 1, func); /* ensure space for metamethod */
|
||||||
|
tryfuncTM(L, func); /* try to get '__call' metamethod */
|
||||||
return luaD_precall(L, func, nresults); /* now it must be a function */
|
return luaD_precall(L, func, nresults); /* now it must be a function */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaD_poscall (lua_State *L, StkId firstResult) {
|
/*
|
||||||
|
** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'.
|
||||||
|
** Handle most typical cases (zero results for commands, one result for
|
||||||
|
** expressions, multiple results for tail calls/single parameters)
|
||||||
|
** separated.
|
||||||
|
*/
|
||||||
|
static int moveresults (lua_State *L, const TValue *firstResult, StkId res,
|
||||||
|
int nres, int wanted) {
|
||||||
|
switch (wanted) { /* handle typical cases separately */
|
||||||
|
case 0: break; /* nothing to move */
|
||||||
|
case 1: { /* one result needed */
|
||||||
|
if (nres == 0) /* no results? */
|
||||||
|
firstResult = luaO_nilobject; /* adjust with nil */
|
||||||
|
setobjs2s(L, res, firstResult); /* move it to proper place */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case LUA_MULTRET: {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < nres; i++) /* move all results to correct place */
|
||||||
|
setobjs2s(L, res + i, firstResult + i);
|
||||||
|
L->top = res + nres;
|
||||||
|
return 0; /* wanted == LUA_MULTRET */
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
int i;
|
||||||
|
if (wanted <= nres) { /* enough results? */
|
||||||
|
for (i = 0; i < wanted; i++) /* move wanted results to correct place */
|
||||||
|
setobjs2s(L, res + i, firstResult + i);
|
||||||
|
}
|
||||||
|
else { /* not enough results; use all of them plus nils */
|
||||||
|
for (i = 0; i < nres; i++) /* move all results to correct place */
|
||||||
|
setobjs2s(L, res + i, firstResult + i);
|
||||||
|
for (; i < wanted; i++) /* complete wanted number of results */
|
||||||
|
setnilvalue(res + i);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
L->top = res + wanted; /* top points after the last result */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Finishes a function call: calls hook if necessary, removes CallInfo,
|
||||||
|
** moves current number of results to proper place; returns 0 iff call
|
||||||
|
** wanted multiple (variable number of) results.
|
||||||
|
*/
|
||||||
|
int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
|
||||||
StkId res;
|
StkId res;
|
||||||
int wanted, i;
|
int wanted = ci->nresults;
|
||||||
CallInfo *ci = L->ci;
|
|
||||||
if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
|
if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
|
||||||
if (L->hookmask & LUA_MASKRET) {
|
if (L->hookmask & LUA_MASKRET) {
|
||||||
ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
|
ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
|
||||||
@@ -382,15 +462,24 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
|
|||||||
L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
|
L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
|
||||||
}
|
}
|
||||||
res = ci->func; /* res == final position of 1st result */
|
res = ci->func; /* res == final position of 1st result */
|
||||||
wanted = ci->nresults;
|
L->ci = ci->previous; /* back to caller */
|
||||||
L->ci = ci = ci->previous; /* back to caller */
|
/* move results to proper place */
|
||||||
/* move results to correct place */
|
return moveresults(L, firstResult, res, nres, wanted);
|
||||||
for (i = wanted; i != 0 && firstResult < L->top; i--)
|
}
|
||||||
setobjs2s(L, res++, firstResult++);
|
|
||||||
while (i-- > 0)
|
|
||||||
setnilvalue(res++);
|
/*
|
||||||
L->top = res;
|
** Check appropriate error for stack overflow ("regular" overflow or
|
||||||
return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
|
** overflow while handling stack overflow). If 'nCalls' is larger than
|
||||||
|
** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but
|
||||||
|
** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to
|
||||||
|
** allow overflow handling to work)
|
||||||
|
*/
|
||||||
|
static void stackerror (lua_State *L) {
|
||||||
|
if (L->nCcalls == LUAI_MAXCCALLS)
|
||||||
|
luaG_runerror(L, "C stack overflow");
|
||||||
|
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
|
||||||
|
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -400,21 +489,25 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
|
|||||||
** When returns, all the results are on the stack, starting at the original
|
** When returns, all the results are on the stack, starting at the original
|
||||||
** function position.
|
** function position.
|
||||||
*/
|
*/
|
||||||
void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
|
void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||||
if (++L->nCcalls >= LUAI_MAXCCALLS) {
|
if (++L->nCcalls >= LUAI_MAXCCALLS)
|
||||||
if (L->nCcalls == LUAI_MAXCCALLS)
|
stackerror(L);
|
||||||
luaG_runerror(L, "C stack overflow");
|
|
||||||
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
|
|
||||||
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
|
|
||||||
}
|
|
||||||
if (!allowyield) L->nny++;
|
|
||||||
if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
|
if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
|
||||||
luaV_execute(L); /* call it */
|
luaV_execute(L); /* call it */
|
||||||
if (!allowyield) L->nny--;
|
|
||||||
L->nCcalls--;
|
L->nCcalls--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Similar to 'luaD_call', but does not allow yields during the call
|
||||||
|
*/
|
||||||
|
void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
|
||||||
|
L->nny++;
|
||||||
|
luaD_call(L, func, nResults);
|
||||||
|
L->nny--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Completes the execution of an interrupted C function, calling its
|
** Completes the execution of an interrupted C function, calling its
|
||||||
** continuation function.
|
** continuation function.
|
||||||
@@ -439,7 +532,7 @@ static void finishCcall (lua_State *L, int status) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
/* finish 'luaD_precall' */
|
/* finish 'luaD_precall' */
|
||||||
luaD_poscall(L, L->top - n);
|
luaD_poscall(L, ci, L->top - n, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -523,7 +616,8 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
|
|||||||
*/
|
*/
|
||||||
static void resume (lua_State *L, void *ud) {
|
static void resume (lua_State *L, void *ud) {
|
||||||
int nCcalls = L->nCcalls;
|
int nCcalls = L->nCcalls;
|
||||||
StkId firstArg = cast(StkId, ud);
|
int n = *(cast(int*, ud)); /* number of arguments */
|
||||||
|
StkId firstArg = L->top - n; /* first argument */
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
if (nCcalls >= LUAI_MAXCCALLS)
|
if (nCcalls >= LUAI_MAXCCALLS)
|
||||||
resume_error(L, "C stack overflow", firstArg);
|
resume_error(L, "C stack overflow", firstArg);
|
||||||
@@ -543,14 +637,13 @@ static void resume (lua_State *L, void *ud) {
|
|||||||
luaV_execute(L); /* just continue running Lua code */
|
luaV_execute(L); /* just continue running Lua code */
|
||||||
else { /* 'common' yield */
|
else { /* 'common' yield */
|
||||||
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
|
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
|
||||||
int n;
|
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
|
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
firstArg = L->top - n; /* yield results come from continuation */
|
firstArg = L->top - n; /* yield results come from continuation */
|
||||||
}
|
}
|
||||||
luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
|
luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */
|
||||||
}
|
}
|
||||||
unroll(L, NULL); /* run continuation */
|
unroll(L, NULL); /* run continuation */
|
||||||
}
|
}
|
||||||
@@ -560,13 +653,13 @@ static void resume (lua_State *L, void *ud) {
|
|||||||
|
|
||||||
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
||||||
int status;
|
int status;
|
||||||
int oldnny = L->nny; /* save "number of non-yieldable" calls */
|
unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
luai_userstateresume(L, nargs);
|
luai_userstateresume(L, nargs);
|
||||||
L->nCcalls = (from) ? from->nCcalls + 1 : 1;
|
L->nCcalls = (from) ? from->nCcalls + 1 : 1;
|
||||||
L->nny = 0; /* allow yields */
|
L->nny = 0; /* allow yields */
|
||||||
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
||||||
status = luaD_rawrunprotected(L, resume, L->top - nargs);
|
status = luaD_rawrunprotected(L, resume, &nargs);
|
||||||
if (status == -1) /* error calling 'lua_resume'? */
|
if (status == -1) /* error calling 'lua_resume'? */
|
||||||
status = LUA_ERRRUN;
|
status = LUA_ERRRUN;
|
||||||
else { /* continue running after recoverable errors */
|
else { /* continue running after recoverable errors */
|
||||||
@@ -575,7 +668,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
|||||||
status = luaD_rawrunprotected(L, unroll, &status);
|
status = luaD_rawrunprotected(L, unroll, &status);
|
||||||
}
|
}
|
||||||
if (errorstatus(status)) { /* unrecoverable error? */
|
if (errorstatus(status)) { /* unrecoverable error? */
|
||||||
L->status = cast_byte(status); /* mark thread as `dead' */
|
L->status = cast_byte(status); /* mark thread as 'dead' */
|
||||||
seterrorobj(L, status, L->top); /* push error message */
|
seterrorobj(L, status, L->top); /* push error message */
|
||||||
L->ci->top = L->top;
|
L->ci->top = L->top;
|
||||||
}
|
}
|
||||||
@@ -609,7 +702,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
|||||||
L->status = LUA_YIELD;
|
L->status = LUA_YIELD;
|
||||||
ci->extra = savestack(L, ci->func); /* save current 'func' */
|
ci->extra = savestack(L, ci->func); /* save current 'func' */
|
||||||
if (isLua(ci)) { /* inside a hook? */
|
if (isLua(ci)) { /* inside a hook? */
|
||||||
api_check(k == NULL, "hooks cannot continue after yielding");
|
api_check(L, k == NULL, "hooks cannot continue after yielding");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
|
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
|
||||||
@@ -650,7 +743,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
|||||||
/*
|
/*
|
||||||
** Execute a protected parser.
|
** Execute a protected parser.
|
||||||
*/
|
*/
|
||||||
struct SParser { /* data to `f_parser' */
|
struct SParser { /* data to 'f_parser' */
|
||||||
ZIO *z;
|
ZIO *z;
|
||||||
Mbuffer buff; /* dynamic structure used by the scanner */
|
Mbuffer buff; /* dynamic structure used by the scanner */
|
||||||
Dyndata dyd; /* dynamic structures used by the parser */
|
Dyndata dyd; /* dynamic structures used by the parser */
|
||||||
@@ -674,7 +767,7 @@ static void f_parser (lua_State *L, void *ud) {
|
|||||||
int c = zgetc(p->z); /* read first character */
|
int c = zgetc(p->z); /* read first character */
|
||||||
if (c == LUA_SIGNATURE[0]) {
|
if (c == LUA_SIGNATURE[0]) {
|
||||||
checkmode(L, p->mode, "binary");
|
checkmode(L, p->mode, "binary");
|
||||||
cl = luaU_undump(L, p->z, &p->buff, p->name);
|
cl = luaU_undump(L, p->z, p->name);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
checkmode(L, p->mode, "text");
|
checkmode(L, p->mode, "text");
|
||||||
|
|||||||
28
ldo.h
28
ldo.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldo.h,v 2.19 2011/10/07 20:45:19 roberto Exp roberto $
|
** $Id: ldo.h,v 2.28 2015/11/23 11:29:43 roberto Exp roberto $
|
||||||
** Stack and Call structure of Lua
|
** Stack and Call structure of Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,31 +13,43 @@
|
|||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
#define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \
|
/*
|
||||||
luaD_growstack(L, n); else condmovestack(L);
|
** Macro to check stack size and grow stack if needed. Parameters
|
||||||
|
** 'pre'/'pos' allow the macro to preserve a pointer into the
|
||||||
|
** stack across reallocations, doing the work only when needed.
|
||||||
|
** 'condmovestack' is used in heavy tests to force a stack reallocation
|
||||||
|
** at every check.
|
||||||
|
*/
|
||||||
|
#define luaD_checkstackaux(L,n,pre,pos) \
|
||||||
|
if (L->stack_last - L->top <= (n)) \
|
||||||
|
{ pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); }
|
||||||
|
|
||||||
|
/* In general, 'pre'/'pos' are empty (nothing to save) */
|
||||||
|
#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
|
||||||
|
|
||||||
|
|
||||||
#define incr_top(L) {L->top++; luaD_checkstack(L,0);}
|
|
||||||
|
|
||||||
#define savestack(L,p) ((char *)(p) - (char *)L->stack)
|
#define savestack(L,p) ((char *)(p) - (char *)L->stack)
|
||||||
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
|
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
|
||||||
|
|
||||||
|
|
||||||
/* 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 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);
|
||||||
LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
|
LUAI_FUNC int 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);
|
||||||
int allowyield);
|
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
||||||
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 int luaD_poscall (lua_State *L, StkId firstResult);
|
LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,
|
||||||
|
int nres);
|
||||||
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
||||||
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
||||||
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
||||||
|
LUAI_FUNC void luaD_inctop (lua_State *L);
|
||||||
|
|
||||||
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
|
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
|
||||||
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||||
|
|||||||
16
ldump.c
16
ldump.c
@@ -1,14 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldump.c,v 2.32 2014/06/18 18:35:43 roberto Exp roberto $
|
** $Id: ldump.c,v 2.36 2015/03/30 15:43:51 roberto Exp roberto $
|
||||||
** save precompiled Lua chunks
|
** save precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#define ldump_c
|
#define ldump_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
@@ -35,7 +38,7 @@ typedef struct {
|
|||||||
|
|
||||||
|
|
||||||
static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
||||||
if (D->status == 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);
|
||||||
lua_lock(D->L);
|
lua_lock(D->L);
|
||||||
@@ -71,14 +74,15 @@ static void DumpString (const TString *s, DumpState *D) {
|
|||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
DumpByte(0, D);
|
DumpByte(0, D);
|
||||||
else {
|
else {
|
||||||
size_t size = s->len + 1; /* include trailing '\0' */
|
size_t size = tsslen(s) + 1; /* include trailing '\0' */
|
||||||
|
const char *str = getstr(s);
|
||||||
if (size < 0xFF)
|
if (size < 0xFF)
|
||||||
DumpByte(cast_int(size), D);
|
DumpByte(cast_int(size), D);
|
||||||
else {
|
else {
|
||||||
DumpByte(0xFF, D);
|
DumpByte(0xFF, D);
|
||||||
DumpVar(size, D);
|
DumpVar(size, D);
|
||||||
}
|
}
|
||||||
DumpVector(getstr(s), size - 1, D); /* no need to save '\0' */
|
DumpVector(str, size - 1, D); /* no need to save '\0' */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
lfunc.c
14
lfunc.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lfunc.c,v 2.42 2014/06/18 22:59:29 roberto Exp roberto $
|
** $Id: lfunc.c,v 2.44 2014/10/25 11:50:46 roberto Exp roberto $
|
||||||
** Auxiliary functions to manipulate prototypes and closures
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#define lfunc_c
|
#define lfunc_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lfunc.h"
|
#include "lfunc.h"
|
||||||
@@ -82,7 +84,7 @@ void luaF_close (lua_State *L, StkId level) {
|
|||||||
UpVal *uv;
|
UpVal *uv;
|
||||||
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
|
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
|
||||||
lua_assert(upisopen(uv));
|
lua_assert(upisopen(uv));
|
||||||
L->openupval = uv->u.open.next; /* remove from `open' list */
|
L->openupval = uv->u.open.next; /* remove from 'open' list */
|
||||||
if (uv->refcount == 0) /* no references? */
|
if (uv->refcount == 0) /* no references? */
|
||||||
luaM_free(L, uv); /* free upvalue */
|
luaM_free(L, uv); /* free upvalue */
|
||||||
else {
|
else {
|
||||||
@@ -132,7 +134,7 @@ void luaF_freeproto (lua_State *L, Proto *f) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Look for n-th local variable at line `line' in function `func'.
|
** Look for n-th local variable at line 'line' in function 'func'.
|
||||||
** Returns NULL if not found.
|
** Returns NULL if not found.
|
||||||
*/
|
*/
|
||||||
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||||
|
|||||||
9
lfunc.h
9
lfunc.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lfunc.h,v 2.13 2014/02/18 13:39:37 roberto Exp roberto $
|
** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 roberto Exp roberto $
|
||||||
** Auxiliary functions to manipulate prototypes and closures
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -22,6 +22,13 @@
|
|||||||
#define isintwups(L) (L->twups != L)
|
#define isintwups(L) (L->twups != L)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** maximum number of upvalues in a closure (both C and Lua). (Value
|
||||||
|
** must fit in a VM register.)
|
||||||
|
*/
|
||||||
|
#define MAXUPVAL 255
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Upvalues for Lua closures
|
** Upvalues for Lua closures
|
||||||
*/
|
*/
|
||||||
|
|||||||
141
lgc.c
141
lgc.c
@@ -1,14 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lgc.c,v 2.195 2014/09/04 18:15:29 roberto Exp roberto $
|
** $Id: lgc.c,v 2.211 2015/12/10 18:12:30 roberto Exp roberto $
|
||||||
** Garbage Collector
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lgc_c
|
#define lgc_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -80,8 +83,13 @@
|
|||||||
#define markvalue(g,o) { checkconsistency(o); \
|
#define markvalue(g,o) { checkconsistency(o); \
|
||||||
if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
|
if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
|
||||||
|
|
||||||
#define markobject(g,t) \
|
#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); }
|
||||||
{ if ((t) && iswhite(t)) reallymarkobject(g, obj2gco(t)); }
|
|
||||||
|
/*
|
||||||
|
** mark an object that can be NULL (either because it is really optional,
|
||||||
|
** or it was stripped as debug info, or inside an uncompleted structure)
|
||||||
|
*/
|
||||||
|
#define markobjectN(g,t) { if (t) markobject(g,t); }
|
||||||
|
|
||||||
static void reallymarkobject (global_State *g, GCObject *o);
|
static void reallymarkobject (global_State *g, GCObject *o);
|
||||||
|
|
||||||
@@ -106,8 +114,13 @@ static void reallymarkobject (global_State *g, GCObject *o);
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** if key is not marked, mark its entry as dead (therefore removing it
|
** If key is not marked, mark its entry as dead. This allows key to be
|
||||||
** from the table)
|
** collected, but keeps its entry in the table. A dead node is needed
|
||||||
|
** when Lua looks up for a key (it may be part of a chain) and when
|
||||||
|
** traversing a weak table (key might be removed from the table during
|
||||||
|
** traversal). Other places never manipulate dead keys, because its
|
||||||
|
** associated nil value is enough to signal that the entry is logically
|
||||||
|
** empty.
|
||||||
*/
|
*/
|
||||||
static void removeentry (Node *n) {
|
static void removeentry (Node *n) {
|
||||||
lua_assert(ttisnil(gval(n)));
|
lua_assert(ttisnil(gval(n)));
|
||||||
@@ -119,14 +132,14 @@ static void removeentry (Node *n) {
|
|||||||
/*
|
/*
|
||||||
** tells whether a key or value can be cleared from a weak
|
** tells whether a key or value can be cleared from a weak
|
||||||
** table. Non-collectable objects are never removed from weak
|
** table. Non-collectable objects are never removed from weak
|
||||||
** tables. Strings behave as `values', so are never removed too. for
|
** tables. Strings behave as 'values', so are never removed too. for
|
||||||
** other objects: if really collected, cannot keep them; for objects
|
** other objects: if really collected, cannot keep them; for objects
|
||||||
** being finalized, keep them in keys, but not in values
|
** being finalized, keep them in keys, but not in values
|
||||||
*/
|
*/
|
||||||
static int iscleared (global_State *g, const TValue *o) {
|
static int iscleared (global_State *g, const TValue *o) {
|
||||||
if (!iscollectable(o)) return 0;
|
if (!iscollectable(o)) return 0;
|
||||||
else if (ttisstring(o)) {
|
else if (ttisstring(o)) {
|
||||||
markobject(g, tsvalue(o)); /* strings are `values', so are never weak */
|
markobject(g, tsvalue(o)); /* strings are 'values', so are never weak */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else return iswhite(gcvalue(o));
|
else return iswhite(gcvalue(o));
|
||||||
@@ -223,15 +236,19 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
|||||||
reentry:
|
reentry:
|
||||||
white2gray(o);
|
white2gray(o);
|
||||||
switch (o->tt) {
|
switch (o->tt) {
|
||||||
case LUA_TSHRSTR:
|
case LUA_TSHRSTR: {
|
||||||
|
gray2black(o);
|
||||||
|
g->GCmemtrav += sizelstring(gco2ts(o)->shrlen);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case LUA_TLNGSTR: {
|
case LUA_TLNGSTR: {
|
||||||
gray2black(o);
|
gray2black(o);
|
||||||
g->GCmemtrav += sizestring(gco2ts(o));
|
g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TUSERDATA: {
|
case LUA_TUSERDATA: {
|
||||||
TValue uvalue;
|
TValue uvalue;
|
||||||
markobject(g, gco2u(o)->metatable); /* mark its metatable */
|
markobjectN(g, gco2u(o)->metatable); /* mark its metatable */
|
||||||
gray2black(o);
|
gray2black(o);
|
||||||
g->GCmemtrav += sizeudata(gco2u(o));
|
g->GCmemtrav += sizeudata(gco2u(o));
|
||||||
getuservalue(g->mainthread, gco2u(o), &uvalue);
|
getuservalue(g->mainthread, gco2u(o), &uvalue);
|
||||||
@@ -272,7 +289,7 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
|||||||
static void markmt (global_State *g) {
|
static void markmt (global_State *g) {
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i < LUA_NUMTAGS; i++)
|
for (i=0; i < LUA_NUMTAGS; i++)
|
||||||
markobject(g, g->mt[i]);
|
markobjectN(g, g->mt[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -434,7 +451,7 @@ static void traversestrongtable (global_State *g, Table *h) {
|
|||||||
static lu_mem traversetable (global_State *g, Table *h) {
|
static lu_mem traversetable (global_State *g, Table *h) {
|
||||||
const char *weakkey, *weakvalue;
|
const char *weakkey, *weakvalue;
|
||||||
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
||||||
markobject(g, h->metatable);
|
markobjectN(g, h->metatable);
|
||||||
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
||||||
((weakkey = strchr(svalue(mode), 'k')),
|
((weakkey = strchr(svalue(mode), 'k')),
|
||||||
(weakvalue = strchr(svalue(mode), 'v')),
|
(weakvalue = strchr(svalue(mode), 'v')),
|
||||||
@@ -454,19 +471,24 @@ static lu_mem traversetable (global_State *g, Table *h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Traverse a prototype. (While a prototype is being build, its
|
||||||
|
** arrays can be larger than needed; the extra slots are filled with
|
||||||
|
** NULL, so the use of 'markobjectN')
|
||||||
|
*/
|
||||||
static int traverseproto (global_State *g, Proto *f) {
|
static int traverseproto (global_State *g, Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
if (f->cache && iswhite(f->cache))
|
if (f->cache && iswhite(f->cache))
|
||||||
f->cache = NULL; /* allow cache to be collected */
|
f->cache = NULL; /* allow cache to be collected */
|
||||||
markobject(g, f->source);
|
markobjectN(g, f->source);
|
||||||
for (i = 0; i < f->sizek; i++) /* mark literals */
|
for (i = 0; i < f->sizek; i++) /* mark literals */
|
||||||
markvalue(g, &f->k[i]);
|
markvalue(g, &f->k[i]);
|
||||||
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
|
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
|
||||||
markobject(g, f->upvalues[i].name);
|
markobjectN(g, f->upvalues[i].name);
|
||||||
for (i = 0; i < f->sizep; i++) /* mark nested protos */
|
for (i = 0; i < f->sizep; i++) /* mark nested protos */
|
||||||
markobject(g, f->p[i]);
|
markobjectN(g, f->p[i]);
|
||||||
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
||||||
markobject(g, f->locvars[i].varname);
|
markobjectN(g, f->locvars[i].varname);
|
||||||
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
|
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
|
||||||
sizeof(Proto *) * f->sizep +
|
sizeof(Proto *) * f->sizep +
|
||||||
sizeof(TValue) * f->sizek +
|
sizeof(TValue) * f->sizek +
|
||||||
@@ -491,7 +513,7 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
|||||||
*/
|
*/
|
||||||
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
||||||
int i;
|
int i;
|
||||||
markobject(g, cl->p); /* mark its prototype */
|
markobjectN(g, cl->p); /* mark its prototype */
|
||||||
for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */
|
for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */
|
||||||
UpVal *uv = cl->upvals[i];
|
UpVal *uv = cl->upvals[i];
|
||||||
if (uv != NULL) {
|
if (uv != NULL) {
|
||||||
@@ -525,7 +547,8 @@ static lu_mem traversethread (global_State *g, lua_State *th) {
|
|||||||
}
|
}
|
||||||
else if (g->gckind != KGC_EMERGENCY)
|
else if (g->gckind != KGC_EMERGENCY)
|
||||||
luaD_shrinkstack(th); /* do not change stack in emergency cycle */
|
luaD_shrinkstack(th); /* do not change stack in emergency cycle */
|
||||||
return (sizeof(lua_State) + sizeof(TValue) * th->stacksize);
|
return (sizeof(lua_State) + sizeof(TValue) * th->stacksize +
|
||||||
|
sizeof(CallInfo) * th->nci);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -686,9 +709,10 @@ static void freeobj (lua_State *L, GCObject *o) {
|
|||||||
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
||||||
case LUA_TSHRSTR:
|
case LUA_TSHRSTR:
|
||||||
luaS_remove(L, gco2ts(o)); /* remove it from hash table */
|
luaS_remove(L, gco2ts(o)); /* remove it from hash table */
|
||||||
/* go through */
|
luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen));
|
||||||
|
break;
|
||||||
case LUA_TLNGSTR: {
|
case LUA_TLNGSTR: {
|
||||||
luaM_freemem(L, o, sizestring(gco2ts(o)));
|
luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: lua_assert(0);
|
default: lua_assert(0);
|
||||||
@@ -702,10 +726,10 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count);
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** sweep at most 'count' elements from a list of GCObjects erasing dead
|
** sweep at most 'count' elements from a list of GCObjects erasing dead
|
||||||
** objects, where a dead (not alive) object is one marked with the "old"
|
** objects, where a dead object is one marked with the old (non current)
|
||||||
** (non current) white and not fixed; change all non-dead objects back
|
** white; change all non-dead objects back to white, preparing for next
|
||||||
** to white, preparing for next collection cycle.
|
** collection cycle. Return where to continue the traversal or NULL if
|
||||||
** When object is a thread, sweep its list of open upvalues too.
|
** list is finished.
|
||||||
*/
|
*/
|
||||||
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
@@ -718,7 +742,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|||||||
*p = curr->next; /* remove 'curr' from list */
|
*p = curr->next; /* remove 'curr' from list */
|
||||||
freeobj(L, curr); /* erase 'curr' */
|
freeobj(L, curr); /* erase 'curr' */
|
||||||
}
|
}
|
||||||
else { /* update marks */
|
else { /* change mark to 'white' */
|
||||||
curr->marked = cast_byte((marked & maskcolors) | white);
|
curr->marked = cast_byte((marked & maskcolors) | white);
|
||||||
p = &curr->next; /* go to next element */
|
p = &curr->next; /* go to next element */
|
||||||
}
|
}
|
||||||
@@ -730,14 +754,11 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|||||||
/*
|
/*
|
||||||
** sweep a list until a live object (or end of list)
|
** sweep a list until a live object (or end of list)
|
||||||
*/
|
*/
|
||||||
static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) {
|
static GCObject **sweeptolive (lua_State *L, GCObject **p) {
|
||||||
GCObject **old = p;
|
GCObject **old = p;
|
||||||
int i = 0;
|
|
||||||
do {
|
do {
|
||||||
i++;
|
|
||||||
p = sweeplist(L, p, 1);
|
p = sweeplist(L, p, 1);
|
||||||
} while (p == old);
|
} while (p == old);
|
||||||
if (n) *n += i;
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -751,12 +772,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If possible, free concatenation buffer and shrink string table
|
** If possible, shrink string table
|
||||||
*/
|
*/
|
||||||
static void checkSizes (lua_State *L, global_State *g) {
|
static void checkSizes (lua_State *L, global_State *g) {
|
||||||
if (g->gckind != KGC_EMERGENCY) {
|
if (g->gckind != KGC_EMERGENCY) {
|
||||||
l_mem olddebt = g->GCdebt;
|
l_mem olddebt = g->GCdebt;
|
||||||
luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */
|
|
||||||
if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
|
if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
|
||||||
luaS_resize(L, g->strt.size / 2); /* shrink it a little */
|
luaS_resize(L, g->strt.size / 2); /* shrink it a little */
|
||||||
g->GCestimate += g->GCdebt - olddebt; /* update estimate */
|
g->GCestimate += g->GCdebt - olddebt; /* update estimate */
|
||||||
@@ -779,7 +799,7 @@ static GCObject *udata2finalize (global_State *g) {
|
|||||||
|
|
||||||
static void dothecall (lua_State *L, void *ud) {
|
static void dothecall (lua_State *L, void *ud) {
|
||||||
UNUSED(ud);
|
UNUSED(ud);
|
||||||
luaD_call(L, L->top - 2, 0, 0);
|
luaD_callnoyield(L, L->top - 2, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -833,10 +853,10 @@ static int runafewfinalizers (lua_State *L) {
|
|||||||
/*
|
/*
|
||||||
** call all pending finalizers
|
** call all pending finalizers
|
||||||
*/
|
*/
|
||||||
static void callallpendingfinalizers (lua_State *L, int propagateerrors) {
|
static void callallpendingfinalizers (lua_State *L) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
while (g->tobefnz)
|
while (g->tobefnz)
|
||||||
GCTM(L, propagateerrors);
|
GCTM(L, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -886,7 +906,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
|||||||
if (issweepphase(g)) {
|
if (issweepphase(g)) {
|
||||||
makewhite(g, o); /* "sweep" object 'o' */
|
makewhite(g, o); /* "sweep" object 'o' */
|
||||||
if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
|
if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
|
||||||
g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */
|
g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
|
||||||
}
|
}
|
||||||
/* search for pointer pointing to 'o' */
|
/* search for pointer pointing to 'o' */
|
||||||
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
|
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
|
||||||
@@ -909,12 +929,15 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** set a reasonable "time" to wait before starting a new GC cycle;
|
** Set a reasonable "time" to wait before starting a new GC cycle; cycle
|
||||||
** cycle will start when memory use hits threshold
|
** will start when memory use hits threshold. (Division by 'estimate'
|
||||||
|
** should be OK: it cannot be zero (because Lua cannot even start with
|
||||||
|
** less than PAUSEADJ bytes).
|
||||||
*/
|
*/
|
||||||
static void setpause (global_State *g) {
|
static void setpause (global_State *g) {
|
||||||
l_mem threshold, debt;
|
l_mem threshold, debt;
|
||||||
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
|
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
|
||||||
|
lua_assert(estimate > 0);
|
||||||
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
|
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
|
||||||
? estimate * g->gcpause /* no overflow */
|
? estimate * g->gcpause /* no overflow */
|
||||||
: MAX_LMEM; /* overflow; truncate to maximum */
|
: MAX_LMEM; /* overflow; truncate to maximum */
|
||||||
@@ -925,19 +948,16 @@ static void setpause (global_State *g) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Enter first sweep phase.
|
** Enter first sweep phase.
|
||||||
** The call to 'sweeptolive' makes pointer point to an object inside
|
** The call to 'sweeplist' tries to make pointer point to an object
|
||||||
** the list (instead of to the header), so that the real sweep do not
|
** inside the list (instead of to the header), so that the real sweep do
|
||||||
** need to skip objects created between "now" and the start of the real
|
** not need to skip objects created between "now" and the start of the
|
||||||
** sweep.
|
** real sweep.
|
||||||
** Returns how many objects it swept.
|
|
||||||
*/
|
*/
|
||||||
static int entersweep (lua_State *L) {
|
static void entersweep (lua_State *L) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
int n = 0;
|
|
||||||
g->gcstate = GCSswpallgc;
|
g->gcstate = GCSswpallgc;
|
||||||
lua_assert(g->sweepgc == NULL);
|
lua_assert(g->sweepgc == NULL);
|
||||||
g->sweepgc = sweeptolive(L, &g->allgc, &n);
|
g->sweepgc = sweeplist(L, &g->allgc, 1);
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -945,7 +965,7 @@ void luaC_freeallobjects (lua_State *L) {
|
|||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
separatetobefnz(g, 1); /* separate all objects with finalizers */
|
separatetobefnz(g, 1); /* separate all objects with finalizers */
|
||||||
lua_assert(g->finobj == NULL);
|
lua_assert(g->finobj == NULL);
|
||||||
callallpendingfinalizers(L, 0);
|
callallpendingfinalizers(L);
|
||||||
lua_assert(g->tobefnz == NULL);
|
lua_assert(g->tobefnz == NULL);
|
||||||
g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */
|
g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */
|
||||||
g->gckind = KGC_NORMAL;
|
g->gckind = KGC_NORMAL;
|
||||||
@@ -972,7 +992,7 @@ static l_mem atomic (lua_State *L) {
|
|||||||
/* remark occasional upvalues of (maybe) dead threads */
|
/* remark occasional upvalues of (maybe) dead threads */
|
||||||
remarkupvals(g);
|
remarkupvals(g);
|
||||||
propagateall(g); /* propagate changes */
|
propagateall(g); /* propagate changes */
|
||||||
work = g->GCmemtrav; /* stop counting (do not recount gray-agains) */
|
work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */
|
||||||
g->gray = grayagain;
|
g->gray = grayagain;
|
||||||
propagateall(g); /* traverse 'grayagain' list */
|
propagateall(g); /* traverse 'grayagain' list */
|
||||||
g->GCmemtrav = 0; /* restart counting */
|
g->GCmemtrav = 0; /* restart counting */
|
||||||
@@ -992,10 +1012,11 @@ static l_mem atomic (lua_State *L) {
|
|||||||
/* at this point, all resurrected objects are marked. */
|
/* at this point, all resurrected objects are marked. */
|
||||||
/* remove dead objects from weak tables */
|
/* remove dead objects from weak tables */
|
||||||
clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */
|
clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */
|
||||||
clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */
|
clearkeys(g, g->allweak, NULL); /* clear keys from all 'allweak' tables */
|
||||||
/* clear values from resurrected weak tables */
|
/* clear values from resurrected weak tables */
|
||||||
clearvalues(g, g->weak, origweak);
|
clearvalues(g, g->weak, origweak);
|
||||||
clearvalues(g, g->allweak, origall);
|
clearvalues(g, g->allweak, origall);
|
||||||
|
luaS_clearcache(g);
|
||||||
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
||||||
work += g->GCmemtrav; /* complete counting */
|
work += g->GCmemtrav; /* complete counting */
|
||||||
return work; /* estimate of memory marked by 'atomic' */
|
return work; /* estimate of memory marked by 'atomic' */
|
||||||
@@ -1031,18 +1052,17 @@ static lu_mem singlestep (lua_State *L) {
|
|||||||
g->GCmemtrav = 0;
|
g->GCmemtrav = 0;
|
||||||
lua_assert(g->gray);
|
lua_assert(g->gray);
|
||||||
propagatemark(g);
|
propagatemark(g);
|
||||||
if (g->gray == NULL) /* no more `gray' objects? */
|
if (g->gray == NULL) /* no more gray objects? */
|
||||||
g->gcstate = GCSatomic; /* finish propagate phase */
|
g->gcstate = GCSatomic; /* finish propagate phase */
|
||||||
return g->GCmemtrav; /* memory traversed in this step */
|
return g->GCmemtrav; /* memory traversed in this step */
|
||||||
}
|
}
|
||||||
case GCSatomic: {
|
case GCSatomic: {
|
||||||
lu_mem work;
|
lu_mem work;
|
||||||
int sw;
|
|
||||||
propagateall(g); /* make sure gray list is empty */
|
propagateall(g); /* make sure gray list is empty */
|
||||||
work = atomic(L); /* work is what was traversed by 'atomic' */
|
work = atomic(L); /* work is what was traversed by 'atomic' */
|
||||||
sw = entersweep(L);
|
entersweep(L);
|
||||||
g->GCestimate = gettotalbytes(g); /* first estimate */;
|
g->GCestimate = gettotalbytes(g); /* first estimate */;
|
||||||
return work + sw * GCSWEEPCOST;
|
return work;
|
||||||
}
|
}
|
||||||
case GCSswpallgc: { /* sweep "regular" objects */
|
case GCSswpallgc: { /* sweep "regular" objects */
|
||||||
return sweepstep(L, g, GCSswpfinobj, &g->finobj);
|
return sweepstep(L, g, GCSswpfinobj, &g->finobj);
|
||||||
@@ -1092,9 +1112,12 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
|
|||||||
static l_mem getdebt (global_State *g) {
|
static l_mem getdebt (global_State *g) {
|
||||||
l_mem debt = g->GCdebt;
|
l_mem debt = g->GCdebt;
|
||||||
int stepmul = g->gcstepmul;
|
int stepmul = g->gcstepmul;
|
||||||
debt = (debt / STEPMULADJ) + 1;
|
if (debt <= 0) return 0; /* minimal debt */
|
||||||
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
|
else {
|
||||||
return debt;
|
debt = (debt / STEPMULADJ) + 1;
|
||||||
|
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
|
||||||
|
return debt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1122,7 +1145,7 @@ void luaC_step (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Performs a full GC cycle; if "isemergency", set a flag to avoid
|
** Performs a full GC cycle; if 'isemergency', set a flag to avoid
|
||||||
** some operations which could change the interpreter state in some
|
** some operations which could change the interpreter state in some
|
||||||
** unexpected ways (running finalizers and shrinking some structures).
|
** unexpected ways (running finalizers and shrinking some structures).
|
||||||
** Before running the collection, check 'keepinvariant'; if it is true,
|
** Before running the collection, check 'keepinvariant'; if it is true,
|
||||||
|
|||||||
43
lgc.h
43
lgc.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lgc.h,v 2.84 2014/07/19 15:09:37 roberto Exp roberto $
|
** $Id: lgc.h,v 2.90 2015/10/21 18:15:15 roberto Exp roberto $
|
||||||
** Garbage Collector
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -74,7 +74,7 @@
|
|||||||
#define testbit(x,b) testbits(x, bitmask(b))
|
#define testbit(x,b) testbits(x, bitmask(b))
|
||||||
|
|
||||||
|
|
||||||
/* Layout for bit use in `marked' field: */
|
/* Layout for bit use in 'marked' field: */
|
||||||
#define WHITE0BIT 0 /* object is white (type 0) */
|
#define WHITE0BIT 0 /* object is white (type 0) */
|
||||||
#define WHITE1BIT 1 /* object is white (type 1) */
|
#define WHITE1BIT 1 /* object is white (type 1) */
|
||||||
#define BLACKBIT 2 /* object is black */
|
#define BLACKBIT 2 /* object is black */
|
||||||
@@ -101,26 +101,35 @@
|
|||||||
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
||||||
|
|
||||||
|
|
||||||
#define luaC_condGC(L,c) \
|
/*
|
||||||
{if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
|
** Does one step of collection when debt becomes positive. 'pre'/'pos'
|
||||||
#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
|
** allows some adjustments to be done only when needed. macro
|
||||||
|
** 'condchangemem' is used only for heavy tests (forcing a full
|
||||||
|
** GC cycle on every opportunity)
|
||||||
|
*/
|
||||||
|
#define luaC_condGC(L,pre,pos) \
|
||||||
|
{ if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
|
||||||
|
condchangemem(L,pre,pos); }
|
||||||
|
|
||||||
|
/* more often than not, 'pre'/'pos' are empty */
|
||||||
|
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
|
||||||
|
|
||||||
|
|
||||||
#define luaC_barrier(L,p,v) { \
|
#define luaC_barrier(L,p,v) ( \
|
||||||
if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
|
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
||||||
luaC_barrier_(L,obj2gco(p),gcvalue(v)); }
|
luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
|
||||||
|
|
||||||
#define luaC_barrierback(L,p,v) { \
|
#define luaC_barrierback(L,p,v) ( \
|
||||||
if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \
|
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
||||||
luaC_barrierback_(L,p); }
|
luaC_barrierback_(L,p) : cast_void(0))
|
||||||
|
|
||||||
#define luaC_objbarrier(L,p,o) { \
|
#define luaC_objbarrier(L,p,o) ( \
|
||||||
if (isblack(p) && iswhite(o)) \
|
(isblack(p) && iswhite(o)) ? \
|
||||||
luaC_barrier_(L,obj2gco(p),obj2gco(o)); }
|
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
|
||||||
|
|
||||||
#define luaC_upvalbarrier(L,uv) \
|
#define luaC_upvalbarrier(L,uv) ( \
|
||||||
{ if (iscollectable((uv)->v) && !upisopen(uv)) \
|
(iscollectable((uv)->v) && !upisopen(uv)) ? \
|
||||||
luaC_upvalbarrier_(L,uv); }
|
luaC_upvalbarrier_(L,uv) : 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);
|
||||||
|
|||||||
38
linit.c
38
linit.c
@@ -1,20 +1,33 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: linit.c,v 1.33 2014/02/06 17:32:33 roberto Exp roberto $
|
** $Id: linit.c,v 1.37 2014/12/09 15:00:17 roberto Exp roberto $
|
||||||
** Initialization of libraries for lua.c and other clients
|
** Initialization of libraries for lua.c and other clients
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define linit_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If you embed Lua in your program and need to open the standard
|
** If you embed Lua in your program and need to open the standard
|
||||||
** libraries, call luaL_openlibs in your program. If you need a
|
** libraries, call luaL_openlibs in your program. If you need a
|
||||||
** different set of libraries, copy this file to your project and edit
|
** different set of libraries, copy this file to your project and edit
|
||||||
** it to suit your needs.
|
** it to suit your needs.
|
||||||
|
**
|
||||||
|
** You can also *preload* libraries, so that a later 'require' can
|
||||||
|
** open the library, which is already linked to the application.
|
||||||
|
** For that, do the following code:
|
||||||
|
**
|
||||||
|
** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||||
|
** lua_pushcfunction(L, luaopen_modname);
|
||||||
|
** lua_setfield(L, -2, modname);
|
||||||
|
** lua_pop(L, 1); // remove _PRELOAD table
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
#define linit_c
|
|
||||||
#define LUA_LIB
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
@@ -35,8 +48,8 @@ static const luaL_Reg loadedlibs[] = {
|
|||||||
{LUA_OSLIBNAME, luaopen_os},
|
{LUA_OSLIBNAME, luaopen_os},
|
||||||
{LUA_STRLIBNAME, luaopen_string},
|
{LUA_STRLIBNAME, luaopen_string},
|
||||||
{LUA_MATHLIBNAME, luaopen_math},
|
{LUA_MATHLIBNAME, luaopen_math},
|
||||||
{LUA_DBLIBNAME, luaopen_debug},
|
|
||||||
{LUA_UTF8LIBNAME, luaopen_utf8},
|
{LUA_UTF8LIBNAME, luaopen_utf8},
|
||||||
|
{LUA_DBLIBNAME, luaopen_debug},
|
||||||
#if defined(LUA_COMPAT_BITLIB)
|
#if defined(LUA_COMPAT_BITLIB)
|
||||||
{LUA_BITLIBNAME, luaopen_bit32},
|
{LUA_BITLIBNAME, luaopen_bit32},
|
||||||
#endif
|
#endif
|
||||||
@@ -44,27 +57,12 @@ static const luaL_Reg loadedlibs[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** these libs are preloaded and must be required before used
|
|
||||||
*/
|
|
||||||
static const luaL_Reg preloadedlibs[] = {
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
LUALIB_API void luaL_openlibs (lua_State *L) {
|
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||||
const luaL_Reg *lib;
|
const luaL_Reg *lib;
|
||||||
/* call open functions from 'loadedlibs' and set results to global table */
|
/* "require" functions from 'loadedlibs' and set results to global table */
|
||||||
for (lib = loadedlibs; lib->func; lib++) {
|
for (lib = loadedlibs; lib->func; lib++) {
|
||||||
luaL_requiref(L, lib->name, lib->func, 1);
|
luaL_requiref(L, lib->name, lib->func, 1);
|
||||||
lua_pop(L, 1); /* remove lib */
|
lua_pop(L, 1); /* remove lib */
|
||||||
}
|
}
|
||||||
/* add open functions from 'preloadedlibs' into 'package.preload' table */
|
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
|
||||||
for (lib = preloadedlibs; lib->func; lib++) {
|
|
||||||
lua_pushcfunction(L, lib->func);
|
|
||||||
lua_setfield(L, -2, lib->name);
|
|
||||||
}
|
|
||||||
lua_pop(L, 1); /* remove _PRELOAD table */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
102
liolib.c
102
liolib.c
@@ -1,18 +1,13 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: liolib.c,v 2.135 2014/10/22 11:44:20 roberto Exp roberto $
|
** $Id: liolib.c,v 2.148 2015/11/23 11:36:11 roberto Exp roberto $
|
||||||
** Standard I/O (and system) library
|
** Standard I/O (and system) library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define liolib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
/*
|
#include "lprefix.h"
|
||||||
** This definition must come before the inclusion of 'stdio.h'; it
|
|
||||||
** should not affect non-POSIX systems
|
|
||||||
*/
|
|
||||||
#if !defined(_FILE_OFFSET_BITS)
|
|
||||||
#define _LARGEFILE_SOURCE 1
|
|
||||||
#define _FILE_OFFSET_BITS 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@@ -22,27 +17,30 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define liolib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
#if !defined(l_checkmode)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check whether 'mode' matches '[rwa]%+?b?'.
|
|
||||||
** Change this macro to accept other modes for 'fopen' besides
|
** Change this macro to accept other modes for 'fopen' besides
|
||||||
** the standard ones.
|
** the standard ones.
|
||||||
*/
|
*/
|
||||||
|
#if !defined(l_checkmode)
|
||||||
|
|
||||||
|
/* accepted extensions to 'mode' in 'fopen' */
|
||||||
|
#if !defined(L_MODEEXT)
|
||||||
|
#define L_MODEEXT "b"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
|
||||||
#define l_checkmode(mode) \
|
#define l_checkmode(mode) \
|
||||||
(*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
|
(*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
|
||||||
(*mode != '+' || ++mode) && /* skip if char is '+' */ \
|
(*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ \
|
||||||
(*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
|
(strspn(mode, L_MODEEXT) == strlen(mode)))
|
||||||
(*mode == '\0'))
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -60,14 +58,14 @@
|
|||||||
#define l_popen(L,c,m) (fflush(NULL), popen(c,m))
|
#define l_popen(L,c,m) (fflush(NULL), popen(c,m))
|
||||||
#define l_pclose(L,file) (pclose(file))
|
#define l_pclose(L,file) (pclose(file))
|
||||||
|
|
||||||
#elif defined(LUA_WIN) /* }{ */
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
|
|
||||||
#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))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definitions */
|
/* ISO C definitions */
|
||||||
#define l_popen(L,c,m) \
|
#define l_popen(L,c,m) \
|
||||||
((void)((void)c, m), \
|
((void)((void)c, m), \
|
||||||
luaL_error(L, "'popen' not supported"), \
|
luaL_error(L, "'popen' not supported"), \
|
||||||
@@ -112,7 +110,7 @@
|
|||||||
#define l_ftell(f) ftello(f)
|
#define l_ftell(f) ftello(f)
|
||||||
#define l_seeknum off_t
|
#define l_seeknum off_t
|
||||||
|
|
||||||
#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \
|
#elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \
|
||||||
&& defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
|
&& defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
|
||||||
|
|
||||||
/* Windows (but not DDK) and Visual C++ 2005 or higher */
|
/* Windows (but not DDK) and Visual C++ 2005 or higher */
|
||||||
@@ -122,7 +120,7 @@
|
|||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definitions */
|
/* ISO C definitions */
|
||||||
#define l_fseek(f,o,w) fseek(f,o,w)
|
#define l_fseek(f,o,w) fseek(f,o,w)
|
||||||
#define l_ftell(f) ftell(f)
|
#define l_ftell(f) ftell(f)
|
||||||
#define l_seeknum long
|
#define l_seeknum long
|
||||||
@@ -182,9 +180,9 @@ static FILE *tofile (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** When creating file handles, always creates a `closed' file handle
|
** When creating file handles, always creates a 'closed' file handle
|
||||||
** before opening the actual file; so, if there is a memory error, the
|
** before opening the actual file; so, if there is a memory error, the
|
||||||
** file is not left opened.
|
** handle is in a consistent state.
|
||||||
*/
|
*/
|
||||||
static LStream *newprefile (lua_State *L) {
|
static LStream *newprefile (lua_State *L) {
|
||||||
LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
|
LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
|
||||||
@@ -326,8 +324,15 @@ static int io_output (lua_State *L) {
|
|||||||
static int io_readline (lua_State *L);
|
static int io_readline (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit
|
||||||
|
** in the limit for upvalues of a closure)
|
||||||
|
*/
|
||||||
|
#define MAXARGLINE 250
|
||||||
|
|
||||||
static void aux_lines (lua_State *L, int toclose) {
|
static void aux_lines (lua_State *L, int toclose) {
|
||||||
int n = lua_gettop(L) - 1; /* number of arguments to read */
|
int n = lua_gettop(L) - 1; /* number of arguments to read */
|
||||||
|
luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
|
||||||
lua_pushinteger(L, n); /* number of arguments to read */
|
lua_pushinteger(L, n); /* number of arguments to read */
|
||||||
lua_pushboolean(L, toclose); /* close/not close file when finished */
|
lua_pushboolean(L, toclose); /* close/not close file when finished */
|
||||||
lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
|
lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
|
||||||
@@ -370,14 +375,17 @@ static int io_lines (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/* maximum length of a numeral */
|
/* maximum length of a numeral */
|
||||||
#define MAXRN 200
|
#if !defined (L_MAXLENNUM)
|
||||||
|
#define L_MAXLENNUM 200
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* auxiliary structure used by 'read_number' */
|
/* auxiliary structure used by 'read_number' */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
FILE *f; /* file being read */
|
FILE *f; /* file being read */
|
||||||
int c; /* current character (look ahead) */
|
int c; /* current character (look ahead) */
|
||||||
int n; /* number of elements in buffer 'buff' */
|
int n; /* number of elements in buffer 'buff' */
|
||||||
char buff[MAXRN + 1]; /* +1 for ending '\0' */
|
char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */
|
||||||
} RN;
|
} RN;
|
||||||
|
|
||||||
|
|
||||||
@@ -385,7 +393,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 >= MAXRN) { /* buffer overflow? */
|
if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */
|
||||||
rn->buff[0] = '\0'; /* invalidate result */
|
rn->buff[0] = '\0'; /* invalidate result */
|
||||||
return 0; /* fail */
|
return 0; /* fail */
|
||||||
}
|
}
|
||||||
@@ -398,10 +406,10 @@ static int nextc (RN *rn) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Accept current char if it is in 'set' (of size 1 or 2)
|
** Accept current char if it is in 'set' (of size 2)
|
||||||
*/
|
*/
|
||||||
static int test2 (RN *rn, const char *set) {
|
static int test2 (RN *rn, const char *set) {
|
||||||
if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0'))
|
if (rn->c == set[0] || rn->c == set[1])
|
||||||
return nextc(rn);
|
return nextc(rn);
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
@@ -418,12 +426,6 @@ static int readdigits (RN *rn, int hex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* access to locale "radix character" (decimal point) */
|
|
||||||
#if !defined(l_getlocaledecpoint)
|
|
||||||
#define l_getlocaledecpoint() (localeconv()->decimal_point[0])
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
||||||
** Then it calls 'lua_stringtonumber' to check whether the format is
|
** Then it calls 'lua_stringtonumber' to check whether the format is
|
||||||
@@ -433,13 +435,14 @@ static int read_number (lua_State *L, FILE *f) {
|
|||||||
RN rn;
|
RN rn;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int hex = 0;
|
int hex = 0;
|
||||||
char decp[2] = ".";
|
char decp[2];
|
||||||
rn.f = f; rn.n = 0;
|
rn.f = f; rn.n = 0;
|
||||||
decp[0] = l_getlocaledecpoint(); /* get decimal point from locale */
|
decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
|
||||||
|
decp[1] = '.'; /* always accept a dot */
|
||||||
l_lockfile(rn.f);
|
l_lockfile(rn.f);
|
||||||
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
||||||
test2(&rn, "-+"); /* optional signal */
|
test2(&rn, "-+"); /* optional signal */
|
||||||
if (test2(&rn, "0")) {
|
if (test2(&rn, "00")) {
|
||||||
if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
|
if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
|
||||||
else count = 1; /* count initial '0' as a valid digit */
|
else count = 1; /* count initial '0' as a valid digit */
|
||||||
}
|
}
|
||||||
@@ -465,21 +468,28 @@ static int read_number (lua_State *L, FILE *f) {
|
|||||||
static int test_eof (lua_State *L, FILE *f) {
|
static int test_eof (lua_State *L, FILE *f) {
|
||||||
int c = getc(f);
|
int c = getc(f);
|
||||||
ungetc(c, f); /* no-op when c == EOF */
|
ungetc(c, f); /* no-op when c == EOF */
|
||||||
lua_pushlstring(L, NULL, 0);
|
lua_pushliteral(L, "");
|
||||||
return (c != EOF);
|
return (c != EOF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int read_line (lua_State *L, FILE *f, int chop) {
|
static int read_line (lua_State *L, FILE *f, int chop) {
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
int c;
|
int c = '\0';
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
l_lockfile(f);
|
while (c != EOF && c != '\n') { /* repeat until end of line */
|
||||||
while ((c = l_getc(f)) != EOF && c != '\n')
|
char *buff = luaL_prepbuffer(&b); /* preallocate buffer */
|
||||||
luaL_addchar(&b, c);
|
int i = 0;
|
||||||
l_unlockfile(f);
|
l_lockfile(f); /* no memory errors can happen inside the lock */
|
||||||
if (!chop && c == '\n') luaL_addchar(&b, c);
|
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
|
||||||
|
buff[i++] = c;
|
||||||
|
l_unlockfile(f);
|
||||||
|
luaL_addsize(&b, i);
|
||||||
|
}
|
||||||
|
if (!chop && c == '\n') /* want a newline and have one? */
|
||||||
|
luaL_addchar(&b, c); /* add ending newline to result */
|
||||||
luaL_pushresult(&b); /* close buffer */
|
luaL_pushresult(&b); /* close buffer */
|
||||||
|
/* return ok if read something (either a newline or something else) */
|
||||||
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +499,7 @@ static void read_all (lua_State *L, FILE *f) {
|
|||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
|
do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
|
||||||
char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE);
|
char *p = luaL_prepbuffer(&b);
|
||||||
nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
|
nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
|
||||||
luaL_addsize(&b, nr);
|
luaL_addsize(&b, nr);
|
||||||
} while (nr == LUAL_BUFFERSIZE);
|
} while (nr == LUAL_BUFFERSIZE);
|
||||||
@@ -524,7 +534,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
|||||||
success = 1;
|
success = 1;
|
||||||
for (n = first; nargs-- && success; n++) {
|
for (n = first; nargs-- && success; n++) {
|
||||||
if (lua_type(L, n) == LUA_TNUMBER) {
|
if (lua_type(L, n) == LUA_TNUMBER) {
|
||||||
size_t l = (size_t)lua_tointeger(L, n);
|
size_t l = (size_t)luaL_checkinteger(L, n);
|
||||||
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
90
llex.c
90
llex.c
@@ -1,19 +1,22 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: llex.c,v 2.83 2014/10/17 16:28:21 roberto Exp roberto $
|
** $Id: llex.c,v 2.95 2015/11/19 19:16:22 roberto Exp roberto $
|
||||||
** Lexical Analyzer
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define llex_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define llex_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lctype.h"
|
#include "lctype.h"
|
||||||
|
#include "ldebug.h"
|
||||||
#include "ldo.h"
|
#include "ldo.h"
|
||||||
#include "lgc.h"
|
#include "lgc.h"
|
||||||
#include "llex.h"
|
#include "llex.h"
|
||||||
@@ -41,7 +44,7 @@ static const char *const luaX_tokens [] = {
|
|||||||
"return", "then", "true", "until", "while",
|
"return", "then", "true", "until", "while",
|
||||||
"//", "..", "...", "==", ">=", "<=", "~=",
|
"//", "..", "...", "==", ">=", "<=", "~=",
|
||||||
"<<", ">>", "::", "<eof>",
|
"<<", ">>", "::", "<eof>",
|
||||||
"<number>", "<number>", "<name>", "<string>"
|
"<number>", "<integer>", "<name>", "<string>"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -66,7 +69,7 @@ static void save (LexState *ls, int c) {
|
|||||||
|
|
||||||
void luaX_init (lua_State *L) {
|
void luaX_init (lua_State *L) {
|
||||||
int i;
|
int i;
|
||||||
TString *e = luaS_new(L, LUA_ENV); /* create env name */
|
TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */
|
||||||
luaC_fix(L, obj2gco(e)); /* never collect this name */
|
luaC_fix(L, obj2gco(e)); /* never collect this name */
|
||||||
for (i=0; i<NUM_RESERVED; i++) {
|
for (i=0; i<NUM_RESERVED; i++) {
|
||||||
TString *ts = luaS_new(L, luaX_tokens[i]);
|
TString *ts = luaS_new(L, luaX_tokens[i]);
|
||||||
@@ -104,9 +107,7 @@ static const char *txtToken (LexState *ls, int token) {
|
|||||||
|
|
||||||
|
|
||||||
static l_noret lexerror (LexState *ls, const char *msg, int token) {
|
static l_noret lexerror (LexState *ls, const char *msg, int token) {
|
||||||
char buff[LUA_IDSIZE];
|
msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
|
||||||
luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
|
|
||||||
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
|
|
||||||
if (token)
|
if (token)
|
||||||
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
|
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
|
||||||
luaD_throw(ls->L, LUA_ERRSYNTAX);
|
luaD_throw(ls->L, LUA_ERRSYNTAX);
|
||||||
@@ -125,7 +126,7 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
|||||||
*/
|
*/
|
||||||
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' */
|
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 */
|
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
||||||
o = luaH_set(L, ls->h, L->top - 1);
|
o = luaH_set(L, ls->h, L->top - 1);
|
||||||
@@ -150,17 +151,17 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
|||||||
static void inclinenumber (LexState *ls) {
|
static void inclinenumber (LexState *ls) {
|
||||||
int old = ls->current;
|
int old = ls->current;
|
||||||
lua_assert(currIsNewline(ls));
|
lua_assert(currIsNewline(ls));
|
||||||
next(ls); /* skip `\n' or `\r' */
|
next(ls); /* skip '\n' or '\r' */
|
||||||
if (currIsNewline(ls) && ls->current != old)
|
if (currIsNewline(ls) && ls->current != old)
|
||||||
next(ls); /* skip `\n\r' or `\r\n' */
|
next(ls); /* skip '\n\r' or '\r\n' */
|
||||||
if (++ls->linenumber >= MAX_INT)
|
if (++ls->linenumber >= MAX_INT)
|
||||||
luaX_syntaxerror(ls, "chunk has too many lines");
|
lexerror(ls, "chunk has too many lines", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
||||||
int firstchar) {
|
int firstchar) {
|
||||||
ls->decpoint = '.';
|
ls->t.token = 0;
|
||||||
ls->L = L;
|
ls->L = L;
|
||||||
ls->current = firstchar;
|
ls->current = firstchar;
|
||||||
ls->lookahead.token = TK_EOS; /* no look-ahead token */
|
ls->lookahead.token = TK_EOS; /* no look-ahead token */
|
||||||
@@ -169,7 +170,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
|||||||
ls->linenumber = 1;
|
ls->linenumber = 1;
|
||||||
ls->lastline = 1;
|
ls->lastline = 1;
|
||||||
ls->source = source;
|
ls->source = source;
|
||||||
ls->envn = luaS_new(L, LUA_ENV); /* get env name */
|
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */
|
||||||
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,42 +206,6 @@ static int check_next2 (LexState *ls, const char *set) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** change all characters 'from' in buffer to 'to'
|
|
||||||
*/
|
|
||||||
static void buffreplace (LexState *ls, char from, char to) {
|
|
||||||
if (from != to) {
|
|
||||||
size_t n = luaZ_bufflen(ls->buff);
|
|
||||||
char *p = luaZ_buffer(ls->buff);
|
|
||||||
while (n--)
|
|
||||||
if (p[n] == from) p[n] = to;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#if !defined(l_getlocaledecpoint)
|
|
||||||
#define l_getlocaledecpoint() (localeconv()->decimal_point[0])
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
|
|
||||||
|
|
||||||
/*
|
|
||||||
** in case of format error, try to change decimal point separator to
|
|
||||||
** the one defined in the current locale and check again
|
|
||||||
*/
|
|
||||||
static void trydecpoint (LexState *ls, TValue *o) {
|
|
||||||
char old = ls->decpoint;
|
|
||||||
ls->decpoint = l_getlocaledecpoint();
|
|
||||||
buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
|
|
||||||
if (!buff2num(ls->buff, o)) {
|
|
||||||
/* format error with correct decimal point: no more options */
|
|
||||||
buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
|
|
||||||
lexerror(ls, "malformed number", TK_FLT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* 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'
|
||||||
@@ -264,9 +229,8 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
|||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
save(ls, '\0');
|
save(ls, '\0');
|
||||||
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
|
if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
|
||||||
if (!buff2num(ls->buff, &obj)) /* format error? */
|
lexerror(ls, "malformed number", TK_FLT);
|
||||||
trydecpoint(ls, &obj); /* try to update decimal point separator */
|
|
||||||
if (ttisinteger(&obj)) {
|
if (ttisinteger(&obj)) {
|
||||||
seminfo->i = ivalue(&obj);
|
seminfo->i = ivalue(&obj);
|
||||||
return TK_INT;
|
return TK_INT;
|
||||||
@@ -280,8 +244,9 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** skip a sequence '[=*[' or ']=*]' and return its number of '='s or
|
** skip a sequence '[=*[' or ']=*]'; if sequence is well formed, return
|
||||||
** -1 if sequence is malformed
|
** its number of '='s; otherwise, return a negative number (-1 iff there
|
||||||
|
** are no '='s after initial bracket)
|
||||||
*/
|
*/
|
||||||
static int skip_sep (LexState *ls) {
|
static int skip_sep (LexState *ls) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@@ -298,7 +263,7 @@ static int skip_sep (LexState *ls) {
|
|||||||
|
|
||||||
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
||||||
int line = ls->linenumber; /* initial line (for error message) */
|
int line = ls->linenumber; /* initial line (for error message) */
|
||||||
save_and_next(ls); /* skip 2nd `[' */
|
save_and_next(ls); /* skip 2nd '[' */
|
||||||
if (currIsNewline(ls)) /* string starts with a newline? */
|
if (currIsNewline(ls)) /* string starts with a newline? */
|
||||||
inclinenumber(ls); /* skip it */
|
inclinenumber(ls); /* skip it */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -312,7 +277,7 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
|||||||
}
|
}
|
||||||
case ']': {
|
case ']': {
|
||||||
if (skip_sep(ls) == sep) {
|
if (skip_sep(ls) == sep) {
|
||||||
save_and_next(ls); /* skip 2nd `]' */
|
save_and_next(ls); /* skip 2nd ']' */
|
||||||
goto endloop;
|
goto endloop;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -438,7 +403,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
|
|||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
|
esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
|
||||||
c = readdecesc(ls); /* digital escape \ddd */
|
c = readdecesc(ls); /* digital escape '\ddd' */
|
||||||
goto only_save;
|
goto only_save;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -480,7 +445,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
next(ls);
|
next(ls);
|
||||||
if (ls->current == '[') { /* long comment? */
|
if (ls->current == '[') { /* long comment? */
|
||||||
int sep = skip_sep(ls);
|
int sep = skip_sep(ls);
|
||||||
luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
|
luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
|
||||||
if (sep >= 0) {
|
if (sep >= 0) {
|
||||||
read_long_string(ls, NULL, sep); /* skip long comment */
|
read_long_string(ls, NULL, sep); /* skip long comment */
|
||||||
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
|
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
|
||||||
@@ -498,8 +463,9 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
read_long_string(ls, seminfo, sep);
|
read_long_string(ls, seminfo, sep);
|
||||||
return TK_STRING;
|
return TK_STRING;
|
||||||
}
|
}
|
||||||
else if (sep == -1) return '[';
|
else if (sep != -1) /* '[=...' missing second bracket */
|
||||||
else lexerror(ls, "invalid long string delimiter", TK_STRING);
|
lexerror(ls, "invalid long string delimiter", TK_STRING);
|
||||||
|
return '[';
|
||||||
}
|
}
|
||||||
case '=': {
|
case '=': {
|
||||||
next(ls);
|
next(ls);
|
||||||
|
|||||||
9
llex.h
9
llex.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: llex.h,v 1.75 2013/08/30 16:01:37 roberto Exp roberto $
|
** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp roberto $
|
||||||
** Lexical Analyzer
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,6 +14,10 @@
|
|||||||
#define FIRST_RESERVED 257
|
#define FIRST_RESERVED 257
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(LUA_ENV)
|
||||||
|
#define LUA_ENV "_ENV"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* WARNING: if you change the order of this enumeration,
|
* WARNING: if you change the order of this enumeration,
|
||||||
@@ -54,7 +58,7 @@ typedef struct Token {
|
|||||||
typedef struct LexState {
|
typedef struct LexState {
|
||||||
int current; /* current character (charint) */
|
int current; /* current character (charint) */
|
||||||
int linenumber; /* input line counter */
|
int linenumber; /* input line counter */
|
||||||
int lastline; /* line of last token `consumed' */
|
int lastline; /* line of last token 'consumed' */
|
||||||
Token t; /* current token */
|
Token t; /* current token */
|
||||||
Token lookahead; /* look ahead token */
|
Token lookahead; /* look ahead token */
|
||||||
struct FuncState *fs; /* current function (parser) */
|
struct FuncState *fs; /* current function (parser) */
|
||||||
@@ -65,7 +69,6 @@ typedef struct LexState {
|
|||||||
struct Dyndata *dyd; /* dynamic structures used by the parser */
|
struct Dyndata *dyd; /* dynamic structures used by the parser */
|
||||||
TString *source; /* current source name */
|
TString *source; /* current source name */
|
||||||
TString *envn; /* environment variable name */
|
TString *envn; /* environment variable name */
|
||||||
char decpoint; /* locale decimal point */
|
|
||||||
} LexState;
|
} LexState;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
177
llimits.h
177
llimits.h
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: llimits.h,v 1.119 2014/07/18 18:18:45 roberto Exp roberto $
|
** $Id: llimits.h,v 1.140 2015/10/21 18:40:47 roberto Exp roberto $
|
||||||
** Limits, basic types, and some other `installation-dependent' definitions
|
** Limits, basic types, and some other 'installation-dependent' definitions
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -14,16 +14,24 @@
|
|||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
/*
|
||||||
typedef unsigned LUA_INT32 lu_int32;
|
** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
|
||||||
|
** the total memory used by Lua (in bytes). Usually, 'size_t' and
|
||||||
|
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
|
||||||
|
*/
|
||||||
|
#if defined(LUAI_MEM) /* { external definitions? */
|
||||||
typedef LUAI_UMEM lu_mem;
|
typedef LUAI_UMEM lu_mem;
|
||||||
|
|
||||||
typedef LUAI_MEM l_mem;
|
typedef LUAI_MEM l_mem;
|
||||||
|
#elif LUAI_BITSINT >= 32 /* }{ */
|
||||||
|
typedef size_t lu_mem;
|
||||||
|
typedef ptrdiff_t l_mem;
|
||||||
|
#else /* 16-bit ints */ /* }{ */
|
||||||
|
typedef unsigned long lu_mem;
|
||||||
|
typedef long l_mem;
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/* chars used as small naturals (so that 'char' is reserved for characters) */
|
||||||
/* chars used as small naturals (so that `char' is reserved for characters) */
|
|
||||||
typedef unsigned char lu_byte;
|
typedef unsigned char lu_byte;
|
||||||
|
|
||||||
|
|
||||||
@@ -44,11 +52,11 @@ typedef unsigned char lu_byte;
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** conversion of pointer to integer:
|
** conversion of pointer to unsigned integer:
|
||||||
** this is for hashing only; there is no problem if the integer
|
** this is for hashing only; there is no problem if the integer
|
||||||
** cannot hold the whole pointer value
|
** cannot hold the whole pointer value
|
||||||
*/
|
*/
|
||||||
#define point2int(p) ((unsigned int)((lu_mem)(p) & UINT_MAX))
|
#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -56,7 +64,13 @@ typedef unsigned char lu_byte;
|
|||||||
#if defined(LUAI_USER_ALIGNMENT_T)
|
#if defined(LUAI_USER_ALIGNMENT_T)
|
||||||
typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
|
typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
|
||||||
#else
|
#else
|
||||||
typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign;
|
typedef union {
|
||||||
|
lua_Number n;
|
||||||
|
double u;
|
||||||
|
void *s;
|
||||||
|
lua_Integer i;
|
||||||
|
long l;
|
||||||
|
} L_Umaxalign;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -70,7 +84,7 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
#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 */
|
||||||
#define lua_longassert(c) { if (!(c)) lua_assert(0); }
|
#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
|
||||||
#else
|
#else
|
||||||
#define lua_assert(c) ((void)0)
|
#define lua_assert(c) ((void)0)
|
||||||
#define check_exp(c,e) (e)
|
#define check_exp(c,e) (e)
|
||||||
@@ -80,22 +94,20 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
/*
|
/*
|
||||||
** assertion for checking API calls
|
** assertion for checking API calls
|
||||||
*/
|
*/
|
||||||
#if defined(LUA_USE_APICHECK)
|
#if !defined(luai_apicheck)
|
||||||
#include <assert.h>
|
#define luai_apicheck(l,e) lua_assert(e)
|
||||||
#define luai_apicheck(e) assert(e)
|
|
||||||
#else
|
|
||||||
#define luai_apicheck(e) lua_assert(e)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||||
#define api_check(e,msg) luai_apicheck((e) && msg)
|
|
||||||
|
|
||||||
|
|
||||||
|
/* macro to avoid warnings about unused variables */
|
||||||
#if !defined(UNUSED)
|
#if !defined(UNUSED)
|
||||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
#define UNUSED(x) ((void)(x))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* type casts (a macro highlights casts in the code) */
|
||||||
#define cast(t, exp) ((t)(exp))
|
#define cast(t, exp) ((t)(exp))
|
||||||
|
|
||||||
#define cast_void(i) cast(void, (i))
|
#define cast_void(i) cast(void, (i))
|
||||||
@@ -112,7 +124,7 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** cast a lua_Unsigned to a signed lua_Integer; this cast is
|
** cast a lua_Unsigned to a signed lua_Integer; this cast is
|
||||||
** not strict ANSI C, but two-complement architectures should
|
** not strict ISO C, but two-complement architectures should
|
||||||
** work fine.
|
** work fine.
|
||||||
*/
|
*/
|
||||||
#if !defined(l_castU2S)
|
#if !defined(l_castU2S)
|
||||||
@@ -125,7 +137,7 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
*/
|
*/
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
#define l_noret void __attribute__((noreturn))
|
#define l_noret void __attribute__((noreturn))
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER) && _MSC_VER >= 1200
|
||||||
#define l_noret void __declspec(noreturn)
|
#define l_noret void __declspec(noreturn)
|
||||||
#else
|
#else
|
||||||
#define l_noret void
|
#define l_noret void
|
||||||
@@ -141,29 +153,50 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
#define LUAI_MAXCCALLS 200
|
#define LUAI_MAXCCALLS 200
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
** maximum number of upvalues in a closure (both C and Lua). (Value
|
|
||||||
** must fit in an unsigned char.)
|
|
||||||
*/
|
|
||||||
#define MAXUPVAL UCHAR_MAX
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** 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)
|
||||||
*/
|
*/
|
||||||
typedef lu_int32 Instruction;
|
#if LUAI_BITSINT >= 32
|
||||||
|
typedef unsigned int Instruction;
|
||||||
|
#else
|
||||||
|
typedef unsigned long Instruction;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* maximum stack for a Lua function */
|
/*
|
||||||
#define MAXSTACK 250
|
** Maximum length for short strings, that is, strings that are
|
||||||
|
** internalized. (Cannot be smaller than reserved words or tags for
|
||||||
|
** metamethods, as these strings must be internalized;
|
||||||
|
** #("function") = 8, #("__newindex") = 10.)
|
||||||
|
*/
|
||||||
|
#if !defined(LUAI_MAXSHORTLEN)
|
||||||
|
#define LUAI_MAXSHORTLEN 40
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
/* minimum size for the string table (must be power of 2) */
|
** Initial size for the string table (must be power of 2).
|
||||||
|
** The Lua core alone registers ~50 strings (reserved words +
|
||||||
|
** metaevent keys + a few others). Libraries would typically add
|
||||||
|
** a few dozens more.
|
||||||
|
*/
|
||||||
#if !defined(MINSTRTABSIZE)
|
#if !defined(MINSTRTABSIZE)
|
||||||
#define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */
|
#define MINSTRTABSIZE 128
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Size of cache for strings in the API. 'N' is the number of
|
||||||
|
** sets (better be a prime) and "M" is the size of each set (M == 1
|
||||||
|
** makes a direct cache.)
|
||||||
|
*/
|
||||||
|
#if !defined(STRCACHE_N)
|
||||||
|
#define STRCACHE_N 53
|
||||||
|
#define STRCACHE_M 2
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -173,13 +206,21 @@ typedef lu_int32 Instruction;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macros that are executed whenever program enters the Lua core
|
||||||
|
** ('lua_lock') and leaves the core ('lua_unlock')
|
||||||
|
*/
|
||||||
#if !defined(lua_lock)
|
#if !defined(lua_lock)
|
||||||
#define lua_lock(L) ((void) 0)
|
#define lua_lock(L) ((void) 0)
|
||||||
#define lua_unlock(L) ((void) 0)
|
#define lua_unlock(L) ((void) 0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macro executed during Lua functions at points where the
|
||||||
|
** function can yield.
|
||||||
|
*/
|
||||||
#if !defined(luai_threadyield)
|
#if !defined(luai_threadyield)
|
||||||
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -205,30 +246,78 @@ typedef lu_int32 Instruction;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(luai_userstateresume)
|
#if !defined(luai_userstateresume)
|
||||||
#define luai_userstateresume(L,n) ((void)L)
|
#define luai_userstateresume(L,n) ((void)L)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(luai_userstateyield)
|
#if !defined(luai_userstateyield)
|
||||||
#define luai_userstateyield(L,n) ((void)L)
|
#define luai_userstateyield(L,n) ((void)L)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** The luai_num* macros define the primitive operations over numbers.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* floor division (defined as 'floor(a/b)') */
|
||||||
|
#if !defined(luai_numidiv)
|
||||||
|
#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* float division */
|
||||||
|
#if !defined(luai_numdiv)
|
||||||
|
#define luai_numdiv(L,a,b) ((a)/(b))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
|
||||||
|
** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
|
||||||
|
** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
|
||||||
|
** ~= floor(a/b)'. That happens when the division has a non-integer
|
||||||
|
** negative result, which is equivalent to the test below.
|
||||||
|
*/
|
||||||
|
#if !defined(luai_nummod)
|
||||||
|
#define luai_nummod(L,a,b,m) \
|
||||||
|
{ (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* exponentiation */
|
||||||
|
#if !defined(luai_numpow)
|
||||||
|
#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* the others are quite standard operations */
|
||||||
|
#if !defined(luai_numadd)
|
||||||
|
#define luai_numadd(L,a,b) ((a)+(b))
|
||||||
|
#define luai_numsub(L,a,b) ((a)-(b))
|
||||||
|
#define luai_nummul(L,a,b) ((a)*(b))
|
||||||
|
#define luai_numunm(L,a) (-(a))
|
||||||
|
#define luai_numeq(a,b) ((a)==(b))
|
||||||
|
#define luai_numlt(a,b) ((a)<(b))
|
||||||
|
#define luai_numle(a,b) ((a)<=(b))
|
||||||
|
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** macro to control inclusion of some hard tests on stack reallocation
|
** macro to control inclusion of some hard tests on stack reallocation
|
||||||
*/
|
*/
|
||||||
#if !defined(HARDSTACKTESTS)
|
#if !defined(HARDSTACKTESTS)
|
||||||
#define condmovestack(L) ((void)0)
|
#define condmovestack(L,pre,pos) ((void)0)
|
||||||
#else
|
#else
|
||||||
/* realloc stack keeping its size */
|
/* realloc stack keeping its size */
|
||||||
#define condmovestack(L) luaD_reallocstack((L), (L)->stacksize)
|
#define condmovestack(L,pre,pos) \
|
||||||
|
{ int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(HARDMEMTESTS)
|
#if !defined(HARDMEMTESTS)
|
||||||
#define condchangemem(L) condmovestack(L)
|
#define condchangemem(L,pre,pos) ((void)0)
|
||||||
#else
|
#else
|
||||||
#define condchangemem(L) \
|
#define condchangemem(L,pre,pos) \
|
||||||
((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
|
{ if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
29
lmathlib.c
29
lmathlib.c
@@ -1,16 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmathlib.c,v 1.109 2014/10/01 11:54:56 roberto Exp $
|
** $Id: lmathlib.c,v 1.116 2015/06/26 19:30:32 roberto Exp $
|
||||||
** Standard mathematical library
|
** Standard mathematical library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lmathlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#define lmathlib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -25,9 +27,11 @@
|
|||||||
#if defined(LUA_USE_POSIX)
|
#if defined(LUA_USE_POSIX)
|
||||||
#define l_rand() random()
|
#define l_rand() random()
|
||||||
#define l_srand(x) srandom(x)
|
#define l_srand(x) srandom(x)
|
||||||
|
#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */
|
||||||
#else
|
#else
|
||||||
#define l_rand() rand()
|
#define l_rand() rand()
|
||||||
#define l_srand(x) srand(x)
|
#define l_srand(x) srand(x)
|
||||||
|
#define L_RANDMAX RAND_MAX
|
||||||
#endif
|
#endif
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
@@ -35,7 +39,7 @@
|
|||||||
static int math_abs (lua_State *L) {
|
static int math_abs (lua_State *L) {
|
||||||
if (lua_isinteger(L, 1)) {
|
if (lua_isinteger(L, 1)) {
|
||||||
lua_Integer n = lua_tointeger(L, 1);
|
lua_Integer n = lua_tointeger(L, 1);
|
||||||
if (n < 0) n = (lua_Integer)(0u - n);
|
if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -91,7 +95,7 @@ static int math_toint (lua_State *L) {
|
|||||||
|
|
||||||
static void pushnumint (lua_State *L, lua_Number d) {
|
static void pushnumint (lua_State *L, lua_Number d) {
|
||||||
lua_Integer n;
|
lua_Integer n;
|
||||||
if (lua_numtointeger(d, &n)) /* does 'd' fit in an integer? */
|
if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
|
||||||
lua_pushinteger(L, n); /* result is integer */
|
lua_pushinteger(L, n); /* result is integer */
|
||||||
else
|
else
|
||||||
lua_pushnumber(L, d); /* result is float */
|
lua_pushnumber(L, d); /* result is float */
|
||||||
@@ -179,6 +183,9 @@ static int math_log (lua_State *L) {
|
|||||||
res = l_mathop(log)(x);
|
res = l_mathop(log)(x);
|
||||||
else {
|
else {
|
||||||
lua_Number base = luaL_checknumber(L, 2);
|
lua_Number base = luaL_checknumber(L, 2);
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
if (base == 2.0) res = l_mathop(log2)(x); else
|
||||||
|
#endif
|
||||||
if (base == 10.0) res = l_mathop(log10)(x);
|
if (base == 10.0) res = l_mathop(log10)(x);
|
||||||
else res = l_mathop(log)(x)/l_mathop(log)(base);
|
else res = l_mathop(log)(x)/l_mathop(log)(base);
|
||||||
}
|
}
|
||||||
@@ -231,12 +238,12 @@ static int math_max (lua_State *L) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** This function uses 'double' (instead of 'lua_Number') to ensure that
|
** This function uses 'double' (instead of 'lua_Number') to ensure that
|
||||||
** all bits from 'l_rand' can be represented, and that 'RAND_MAX + 1.0'
|
** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0'
|
||||||
** will keep full precision (ensuring that 'r' is always less than 1.0.)
|
** will keep full precision (ensuring that 'r' is always less than 1.0.)
|
||||||
*/
|
*/
|
||||||
static int math_random (lua_State *L) {
|
static int math_random (lua_State *L) {
|
||||||
lua_Integer low, up;
|
lua_Integer low, up;
|
||||||
double r = (double)l_rand() * (1.0 / ((double)RAND_MAX + 1.0));
|
double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
|
||||||
switch (lua_gettop(L)) { /* check number of arguments */
|
switch (lua_gettop(L)) { /* check number of arguments */
|
||||||
case 0: { /* no arguments */
|
case 0: { /* no arguments */
|
||||||
lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
|
lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
|
||||||
@@ -256,8 +263,8 @@ static int math_random (lua_State *L) {
|
|||||||
}
|
}
|
||||||
/* random integer in the interval [low, up] */
|
/* random integer in the interval [low, up] */
|
||||||
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
||||||
luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER,
|
luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
|
||||||
1, "interval too large");
|
"interval too large");
|
||||||
r *= (double)(up - low) + 1.0;
|
r *= (double)(up - low) + 1.0;
|
||||||
lua_pushinteger(L, (lua_Integer)r + low);
|
lua_pushinteger(L, (lua_Integer)r + low);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -266,7 +273,7 @@ static int math_random (lua_State *L) {
|
|||||||
|
|
||||||
static int math_randomseed (lua_State *L) {
|
static int math_randomseed (lua_State *L) {
|
||||||
l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
|
l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
|
||||||
(void)rand(); /* discard first value to avoid undesirable correlations */
|
(void)l_rand(); /* discard first value to avoid undesirable correlations */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
lmem.c
27
lmem.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmem.c,v 1.85 2014/06/26 18:29:05 roberto Exp roberto $
|
** $Id: lmem.c,v 1.90 2015/03/03 18:18:29 roberto Exp roberto $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#define lmem_c
|
#define lmem_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -24,15 +26,15 @@
|
|||||||
/*
|
/*
|
||||||
** About the realloc function:
|
** About the realloc function:
|
||||||
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||||
** (`osize' is the old size, `nsize' is the new size)
|
** ('osize' is the old size, 'nsize' is the new size)
|
||||||
**
|
**
|
||||||
** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
|
** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
|
||||||
** matter 'x').
|
** matter 'x').
|
||||||
**
|
**
|
||||||
** * frealloc(ud, p, x, 0) frees the block `p'
|
** * frealloc(ud, p, x, 0) frees the block 'p'
|
||||||
** (in this specific case, frealloc must return NULL);
|
** (in this specific case, frealloc must return NULL);
|
||||||
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
||||||
** (which is equivalent to free(NULL) in ANSI C)
|
** (which is equivalent to free(NULL) in ISO C)
|
||||||
**
|
**
|
||||||
** frealloc returns NULL if it cannot create or reallocate the area
|
** frealloc returns NULL if it cannot create or reallocate the area
|
||||||
** (any reallocation to an equal or smaller size cannot fail!)
|
** (any reallocation to an equal or smaller size cannot fail!)
|
||||||
@@ -83,10 +85,11 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
|||||||
#endif
|
#endif
|
||||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
||||||
if (newblock == NULL && nsize > 0) {
|
if (newblock == NULL && nsize > 0) {
|
||||||
api_check( nsize > realosize,
|
lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
|
||||||
"realloc cannot fail when shrinking a block");
|
if (g->version) { /* is state fully built? */
|
||||||
luaC_fullgc(L, 1); /* try to free some memory... */
|
luaC_fullgc(L, 1); /* try to free some memory... */
|
||||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
||||||
|
}
|
||||||
if (newblock == NULL)
|
if (newblock == NULL)
|
||||||
luaD_throw(L, LUA_ERRMEM);
|
luaD_throw(L, LUA_ERRMEM);
|
||||||
}
|
}
|
||||||
|
|||||||
8
lmem.h
8
lmem.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmem.h,v 1.40 2013/02/20 14:08:21 roberto Exp roberto $
|
** $Id: lmem.h,v 1.42 2014/12/19 13:45:40 roberto Exp roberto $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -32,6 +32,12 @@
|
|||||||
? luaM_toobig(L) : cast_void(0)) , \
|
? luaM_toobig(L) : cast_void(0)) , \
|
||||||
luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
|
luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Arrays of chars do not need any test
|
||||||
|
*/
|
||||||
|
#define luaM_reallocvchar(L,b,on,n) \
|
||||||
|
cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
|
||||||
|
|
||||||
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
|
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
|
||||||
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
||||||
#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
|
#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
|
||||||
|
|||||||
74
loadlib.c
74
loadlib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: loadlib.c,v 1.116 2014/07/29 16:01:00 roberto Exp roberto $
|
** $Id: loadlib.c,v 1.126 2015/02/16 13:14:33 roberto Exp roberto $
|
||||||
** Dynamic library loader for Lua
|
** Dynamic library loader for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
**
|
**
|
||||||
@@ -8,22 +8,16 @@
|
|||||||
** systems.
|
** systems.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** if needed, includes windows header before everything else
|
|
||||||
*/
|
|
||||||
#if defined(_WIN32)
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define loadlib_c
|
#define loadlib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -92,8 +86,11 @@
|
|||||||
#define LUA_OFSEP "_"
|
#define LUA_OFSEP "_"
|
||||||
|
|
||||||
|
|
||||||
/* table (in the registry) that keeps handles for all loaded C libraries */
|
/*
|
||||||
#define CLIBS "_CLIBS"
|
** unique key for table in the registry that keeps handles
|
||||||
|
** for all loaded C libraries
|
||||||
|
*/
|
||||||
|
static const int CLIBS = 0;
|
||||||
|
|
||||||
#define LIB_FAIL "open"
|
#define LIB_FAIL "open"
|
||||||
|
|
||||||
@@ -127,7 +124,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_DLOPEN)
|
#if defined(LUA_USE_DLOPEN) /* { */
|
||||||
/*
|
/*
|
||||||
** {========================================================================
|
** {========================================================================
|
||||||
** This is an implementation of loadlib based on the dlfcn interface.
|
** This is an implementation of loadlib based on the dlfcn interface.
|
||||||
@@ -139,6 +136,18 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
|||||||
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
||||||
|
** is undefined according to ISO C, but POSIX assumes that it works.
|
||||||
|
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
||||||
|
*/
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#define cast_func(p) (__extension__ (lua_CFunction)(p))
|
||||||
|
#else
|
||||||
|
#define cast_func(p) ((lua_CFunction)(p))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static void lsys_unloadlib (void *lib) {
|
static void lsys_unloadlib (void *lib) {
|
||||||
dlclose(lib);
|
dlclose(lib);
|
||||||
}
|
}
|
||||||
@@ -152,7 +161,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)dlsym(lib, sym);
|
lua_CFunction f = cast_func(dlsym(lib, sym));
|
||||||
if (f == NULL) lua_pushstring(L, dlerror());
|
if (f == NULL) lua_pushstring(L, dlerror());
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@@ -161,13 +170,15 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#elif defined(LUA_DL_DLL)
|
#elif defined(LUA_DL_DLL) /* }{ */
|
||||||
/*
|
/*
|
||||||
** {======================================================================
|
** {======================================================================
|
||||||
** This is an implementation of loadlib for Windows using native functions.
|
** This is an implementation of loadlib for Windows using native functions.
|
||||||
** =======================================================================
|
** =======================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
#undef setprogdir
|
#undef setprogdir
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -225,7 +236,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
#else
|
#else /* }{ */
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {======================================================
|
||||||
** Fallback for other systems
|
** Fallback for other systems
|
||||||
@@ -258,7 +269,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
#endif
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -266,7 +277,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
*/
|
*/
|
||||||
static void *checkclib (lua_State *L, const char *path) {
|
static void *checkclib (lua_State *L, const char *path) {
|
||||||
void *plib;
|
void *plib;
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
lua_rawgetp(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' */
|
||||||
@@ -279,7 +290,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_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
lua_rawgetp(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 */
|
||||||
@@ -520,8 +531,8 @@ 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 */
|
||||||
luaL_buffinit(L, &msg);
|
luaL_buffinit(L, &msg);
|
||||||
lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */
|
/* push 'package.searchers' to index 3 in the stack */
|
||||||
if (!lua_istable(L, 3))
|
if (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");
|
||||||
/* iterate over available searchers to find a loader */
|
/* iterate over available searchers to find a loader */
|
||||||
for (i = 1; ; i++) {
|
for (i = 1; ; i++) {
|
||||||
@@ -721,7 +732,7 @@ static void createsearcherstable (lua_State *L) {
|
|||||||
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);
|
||||||
/* fill it with pre-defined searchers */
|
/* fill it with predefined searchers */
|
||||||
for (i=0; searchers[i] != NULL; i++) {
|
for (i=0; searchers[i] != NULL; i++) {
|
||||||
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
|
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
|
||||||
lua_pushcclosure(L, searchers[i], 1);
|
lua_pushcclosure(L, searchers[i], 1);
|
||||||
@@ -729,7 +740,7 @@ static void createsearcherstable (lua_State *L) {
|
|||||||
}
|
}
|
||||||
#if defined(LUA_COMPAT_LOADERS)
|
#if defined(LUA_COMPAT_LOADERS)
|
||||||
lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
|
lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
|
||||||
lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */
|
lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */
|
||||||
#endif
|
#endif
|
||||||
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
||||||
}
|
}
|
||||||
@@ -740,17 +751,18 @@ 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) {
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
lua_newtable(L); /* 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 */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||||
createclibstable(L);
|
createclibstable(L);
|
||||||
luaL_newlib(L, pk_funcs); /* create `package' table */
|
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
||||||
createsearcherstable(L);
|
createsearcherstable(L);
|
||||||
/* set field 'path' */
|
/* set field 'path' */
|
||||||
setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT);
|
setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT);
|
||||||
@@ -760,10 +772,10 @@ LUAMOD_API int luaopen_package (lua_State *L) {
|
|||||||
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
|
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
|
||||||
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
|
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
|
||||||
lua_setfield(L, -2, "config");
|
lua_setfield(L, -2, "config");
|
||||||
/* set field `loaded' */
|
/* set field 'loaded' */
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||||
lua_setfield(L, -2, "loaded");
|
lua_setfield(L, -2, "loaded");
|
||||||
/* set field `preload' */
|
/* set field 'preload' */
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||||
lua_setfield(L, -2, "preload");
|
lua_setfield(L, -2, "preload");
|
||||||
lua_pushglobaltable(L);
|
lua_pushglobaltable(L);
|
||||||
|
|||||||
219
lobject.c
219
lobject.c
@@ -1,17 +1,22 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lobject.c,v 2.92 2014/10/10 22:23:04 roberto Exp roberto $
|
** $Id: lobject.c,v 2.110 2016/05/02 14:00:32 roberto Exp roberto $
|
||||||
** Some generic functions over Lua objects
|
** Some generic functions over Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lobject_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <locale.h>
|
||||||
|
#include <math.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lobject_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lctype.h"
|
#include "lctype.h"
|
||||||
@@ -36,8 +41,12 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
|||||||
int luaO_int2fb (unsigned int x) {
|
int luaO_int2fb (unsigned int x) {
|
||||||
int e = 0; /* exponent */
|
int e = 0; /* exponent */
|
||||||
if (x < 8) return x;
|
if (x < 8) return x;
|
||||||
while (x >= 0x10) {
|
while (x >= (8 << 4)) { /* coarse steps */
|
||||||
x = (x+1) >> 1;
|
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++;
|
e++;
|
||||||
}
|
}
|
||||||
return ((e+1) << 3) | (cast_int(x) - 8);
|
return ((e+1) << 3) | (cast_int(x) - 8);
|
||||||
@@ -46,14 +55,15 @@ int luaO_int2fb (unsigned int x) {
|
|||||||
|
|
||||||
/* converts back */
|
/* converts back */
|
||||||
int luaO_fb2int (int x) {
|
int luaO_fb2int (int x) {
|
||||||
int e = (x >> 3) & 0x1f;
|
return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
|
||||||
if (e == 0) return x;
|
|
||||||
else return ((x & 7) + 8) << (e - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Computes ceil(log2(x))
|
||||||
|
*/
|
||||||
int luaO_ceillog2 (unsigned int x) {
|
int luaO_ceillog2 (unsigned int x) {
|
||||||
static const lu_byte log_2[256] = {
|
static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
|
||||||
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||||
@@ -98,6 +108,7 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1,
|
|||||||
case LUA_OPMUL: return luai_nummul(L, v1, v2);
|
case LUA_OPMUL: return luai_nummul(L, v1, v2);
|
||||||
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
|
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
|
||||||
case LUA_OPPOW: return luai_numpow(L, v1, v2);
|
case LUA_OPPOW: return luai_numpow(L, v1, v2);
|
||||||
|
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
|
||||||
case LUA_OPUNM: return luai_numunm(L, v1);
|
case LUA_OPUNM: return luai_numunm(L, v1);
|
||||||
case LUA_OPMOD: {
|
case LUA_OPMOD: {
|
||||||
lua_Number m;
|
lua_Number m;
|
||||||
@@ -112,9 +123,9 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1,
|
|||||||
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
||||||
TValue *res) {
|
TValue *res) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR:
|
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
|
||||||
case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR:
|
case LUA_OPSHL: case LUA_OPSHR:
|
||||||
case LUA_OPBNOT: { /* operates only on integers */
|
case LUA_OPBNOT: { /* operate only on integers */
|
||||||
lua_Integer i1; lua_Integer i2;
|
lua_Integer i1; lua_Integer i2;
|
||||||
if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
|
if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
|
||||||
setivalue(res, intarith(L, op, i1, i2));
|
setivalue(res, intarith(L, op, i1, i2));
|
||||||
@@ -145,13 +156,13 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
|||||||
}
|
}
|
||||||
/* could not perform raw operation; try metamethod */
|
/* could not perform raw operation; try metamethod */
|
||||||
lua_assert(L != NULL); /* should not fail when folding (compile time) */
|
lua_assert(L != NULL); /* should not fail when folding (compile time) */
|
||||||
luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD));
|
luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaO_hexavalue (int c) {
|
int luaO_hexavalue (int c) {
|
||||||
if (lisdigit(c)) return c - '0';
|
if (lisdigit(c)) return c - '0';
|
||||||
else return ltolower(c) - 'a' + 10;
|
else return (ltolower(c) - 'a') + 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -162,26 +173,14 @@ static int isneg (const char **s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {==================================================================
|
||||||
** lua_strx2number converts an hexadecimal numeric string to a number.
|
** Lua's implementation for 'lua_strx2number'
|
||||||
** In C99, 'strtod' does both conversions. C89, however, has no function
|
** ===================================================================
|
||||||
** to convert floating hexadecimal strings to numbers. For these
|
|
||||||
** systems, you can leave 'lua_strx2number' undefined and Lua will
|
|
||||||
** provide its own implementation.
|
|
||||||
** =======================================================
|
|
||||||
*/
|
*/
|
||||||
#if !defined(lua_strx2number) /* { */
|
|
||||||
|
|
||||||
#if defined(LUA_USE_C99) /* { */
|
#if !defined(lua_strx2number)
|
||||||
|
|
||||||
#define lua_strx2number(s,p) lua_str2number(s,p)
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
|
|
||||||
/* Lua's implementation for 'lua_strx2number' */
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
/* maximum number of significant digits to read (to avoid overflows
|
/* maximum number of significant digits to read (to avoid overflows
|
||||||
even with single floats) */
|
even with single floats) */
|
||||||
@@ -192,35 +191,30 @@ static int isneg (const char **s) {
|
|||||||
** C99 specification for 'strtod'
|
** C99 specification for 'strtod'
|
||||||
*/
|
*/
|
||||||
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||||
|
int dot = lua_getlocaledecpoint();
|
||||||
lua_Number r = 0.0; /* result (accumulator) */
|
lua_Number r = 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 */
|
||||||
int neg = 0; /* 1 if number is negative */
|
int neg; /* 1 if number is negative */
|
||||||
int dot = 0; /* true after seen a dot */
|
int hasdot = 0; /* true after seen a dot */
|
||||||
*endptr = cast(char *, s); /* nothing is valid yet */
|
*endptr = cast(char *, s); /* nothing is valid yet */
|
||||||
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
||||||
neg = isneg(&s); /* check signal */
|
neg = isneg(&s); /* check signal */
|
||||||
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 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 == '.') {
|
if (*s == dot) {
|
||||||
if (dot) break; /* second dot? stop loop */
|
if (hasdot) break; /* second dot? stop loop */
|
||||||
else dot = 1;
|
else hasdot = 1;
|
||||||
}
|
}
|
||||||
else if (lisxdigit(cast_uchar(*s))) {
|
else if (lisxdigit(cast_uchar(*s))) {
|
||||||
if (sigdig == 0 && *s == '0') { /* non-significant zero? */
|
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
|
||||||
nosigdig++;
|
nosigdig++;
|
||||||
if (dot) e--; /* zero after dot? correct exponent */
|
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
||||||
}
|
r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
|
||||||
else {
|
else e++; /* too many digits; ignore, but still count for exponent */
|
||||||
if (++sigdig <= MAXSIGDIG) { /* can read it without overflow? */
|
if (hasdot) e--; /* decimal digit? correct exponent */
|
||||||
r = (r * cast_num(16.0)) + luaO_hexavalue(cast_uchar(*s));
|
|
||||||
if (dot) e--; /* decimal digit */
|
|
||||||
}
|
|
||||||
else /* too many digits; ignore */
|
|
||||||
if (!dot) e++; /* still count it for exponent */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else break; /* neither a dot nor a digit */
|
else break; /* neither a dot nor a digit */
|
||||||
}
|
}
|
||||||
@@ -245,27 +239,63 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
return l_mathop(ldexp)(r, e);
|
return l_mathop(ldexp)(r, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* } */
|
#endif
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
static const char *l_str2d (const char *s, lua_Number *result) {
|
/* maximum length of a numeral */
|
||||||
|
#if !defined (L_MAXLENNUM)
|
||||||
|
#define L_MAXLENNUM 200
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
|
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
|
||||||
return NULL;
|
: lua_str2number(s, &endptr);
|
||||||
else if (strpbrk(s, "xX")) /* hex? */
|
if (endptr == s) return NULL; /* nothing recognized? */
|
||||||
*result = lua_strx2number(s, &endptr);
|
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
|
||||||
else
|
return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */
|
||||||
*result = lua_str2number(s, &endptr);
|
|
||||||
if (endptr == s) return 0; /* nothing recognized */
|
|
||||||
while (lisspace(cast_uchar(*endptr))) endptr++;
|
|
||||||
return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Convert string 's' to a Lua number (put in 'result'). Return NULL
|
||||||
|
** on fail or the address of the ending '\0' on success.
|
||||||
|
** 'pmode' points to (and 'mode' contains) special things in the string:
|
||||||
|
** - 'x'/'X' means an 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
|
||||||
|
** mark. If the convertion fails, it may mean number has a dot but
|
||||||
|
** locale accepts something else. In that case, the code copies 's'
|
||||||
|
** to a buffer (because 's' is read-only), changes the dot to the
|
||||||
|
** current locale radix mark, and tries to convert again.
|
||||||
|
*/
|
||||||
|
static const char *l_str2d (const char *s, lua_Number *result) {
|
||||||
|
const char *endptr;
|
||||||
|
const char *pmode = strpbrk(s, ".xXnN");
|
||||||
|
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
|
||||||
|
if (mode == 'n') /* reject 'inf' and 'nan' */
|
||||||
|
return NULL;
|
||||||
|
endptr = l_str2dloc(s, result, mode); /* try to convert */
|
||||||
|
if (endptr == NULL) { /* failed? may be a different locale */
|
||||||
|
char buff[L_MAXLENNUM + 1];
|
||||||
|
char *pdot = strchr(s, '.');
|
||||||
|
if (strlen(s) > L_MAXLENNUM || pdot == NULL)
|
||||||
|
return NULL; /* string too long or no dot; fail */
|
||||||
|
strcpy(buff, s); /* copy string to buffer */
|
||||||
|
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
|
||||||
|
endptr = l_str2dloc(buff, result, mode); /* try again */
|
||||||
|
if (endptr != NULL)
|
||||||
|
endptr = s + (endptr - buff); /* make relative to 's' */
|
||||||
|
}
|
||||||
|
return endptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
|
||||||
|
#define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
|
||||||
|
|
||||||
static const char *l_str2int (const char *s, lua_Integer *result) {
|
static const char *l_str2int (const char *s, lua_Integer *result) {
|
||||||
lua_Unsigned a = 0;
|
lua_Unsigned a = 0;
|
||||||
int empty = 1;
|
int empty = 1;
|
||||||
@@ -276,13 +306,16 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
|
|||||||
(s[1] == 'x' || s[1] == 'X')) { /* hex? */
|
(s[1] == 'x' || s[1] == 'X')) { /* hex? */
|
||||||
s += 2; /* skip '0x' */
|
s += 2; /* skip '0x' */
|
||||||
for (; lisxdigit(cast_uchar(*s)); s++) {
|
for (; lisxdigit(cast_uchar(*s)); s++) {
|
||||||
a = a * 16 + luaO_hexavalue(cast_uchar(*s));
|
a = a * 16 + luaO_hexavalue(*s);
|
||||||
empty = 0;
|
empty = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* decimal */
|
else { /* decimal */
|
||||||
for (; lisdigit(cast_uchar(*s)); s++) {
|
for (; lisdigit(cast_uchar(*s)); s++) {
|
||||||
a = a * 10 + luaO_hexavalue(cast_uchar(*s));
|
int d = *s - '0';
|
||||||
|
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
|
||||||
|
return NULL; /* do not accept it (as integer) */
|
||||||
|
a = a * 10 + d;
|
||||||
empty = 0;
|
empty = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -306,7 +339,7 @@ size_t luaO_str2num (const char *s, TValue *o) {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
return 0; /* conversion failed */
|
return 0; /* conversion failed */
|
||||||
return (e - s + 1); /* success; return string size */
|
return (e - s) + 1; /* success; return string size */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -317,8 +350,8 @@ int luaO_utf8esc (char *buff, unsigned long x) {
|
|||||||
buff[UTF8BUFFSZ - 1] = cast(char, x);
|
buff[UTF8BUFFSZ - 1] = cast(char, x);
|
||||||
else { /* need continuation bytes */
|
else { /* need continuation bytes */
|
||||||
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
||||||
do {
|
do { /* add continuation bytes */
|
||||||
buff[UTF8BUFFSZ - (n++)] = 0x80 | (x & 0x3f); /* add continuation byte */
|
buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
|
||||||
x >>= 6; /* remove added bits */
|
x >>= 6; /* remove added bits */
|
||||||
mfb >>= 1; /* now there is one less bit available in first byte */
|
mfb >>= 1; /* now there is one less bit available in first byte */
|
||||||
} while (x > mfb); /* still needs continuation byte? */
|
} while (x > mfb); /* still needs continuation byte? */
|
||||||
@@ -340,12 +373,12 @@ void luaO_tostring (lua_State *L, StkId obj) {
|
|||||||
size_t len;
|
size_t len;
|
||||||
lua_assert(ttisnumber(obj));
|
lua_assert(ttisnumber(obj));
|
||||||
if (ttisinteger(obj))
|
if (ttisinteger(obj))
|
||||||
len = lua_integer2str(buff, ivalue(obj));
|
len = lua_integer2str(buff, sizeof(buff), ivalue(obj));
|
||||||
else {
|
else {
|
||||||
len = lua_number2str(buff, fltvalue(obj));
|
len = lua_number2str(buff, sizeof(buff), fltvalue(obj));
|
||||||
#if !defined(LUA_COMPAT_FLOATSTRING)
|
#if !defined(LUA_COMPAT_FLOATSTRING)
|
||||||
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
||||||
buff[len++] = '.';
|
buff[len++] = lua_getlocaledecpoint();
|
||||||
buff[len++] = '0'; /* adds '.0' to result */
|
buff[len++] = '0'; /* adds '.0' to result */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -355,27 +388,29 @@ void luaO_tostring (lua_State *L, StkId obj) {
|
|||||||
|
|
||||||
|
|
||||||
static void pushstr (lua_State *L, const char *str, size_t l) {
|
static void pushstr (lua_State *L, const char *str, size_t l) {
|
||||||
setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
|
setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
|
||||||
|
luaD_inctop(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* this function handles only '%d', '%c', '%f', '%p', and '%s'
|
/*
|
||||||
conventional formats, plus Lua-specific '%I' and '%U' */
|
** this function handles only '%d', '%c', '%f', '%p', and '%s'
|
||||||
|
conventional formats, plus Lua-specific '%I' and '%U'
|
||||||
|
*/
|
||||||
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
const char *e = strchr(fmt, '%');
|
const char *e = strchr(fmt, '%');
|
||||||
if (e == NULL) break;
|
if (e == NULL) break;
|
||||||
luaD_checkstack(L, 2); /* fmt + item */
|
|
||||||
pushstr(L, fmt, e - fmt);
|
pushstr(L, fmt, e - fmt);
|
||||||
switch (*(e+1)) {
|
switch (*(e+1)) {
|
||||||
case 's': {
|
case 's': { /* zero-terminated string */
|
||||||
const char *s = va_arg(argp, char *);
|
const char *s = va_arg(argp, char *);
|
||||||
if (s == NULL) s = "(null)";
|
if (s == NULL) s = "(null)";
|
||||||
pushstr(L, s, strlen(s));
|
pushstr(L, s, strlen(s));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'c': {
|
case 'c': { /* an 'int' as a character */
|
||||||
char buff = cast(char, va_arg(argp, int));
|
char buff = cast(char, va_arg(argp, int));
|
||||||
if (lisprint(cast_uchar(buff)))
|
if (lisprint(cast_uchar(buff)))
|
||||||
pushstr(L, &buff, 1);
|
pushstr(L, &buff, 1);
|
||||||
@@ -383,28 +418,28 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
|||||||
luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
|
luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': {
|
case 'd': { /* an 'int' */
|
||||||
setivalue(L->top++, va_arg(argp, int));
|
setivalue(L->top, va_arg(argp, int));
|
||||||
|
goto top2str;
|
||||||
|
}
|
||||||
|
case 'I': { /* a 'lua_Integer' */
|
||||||
|
setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt)));
|
||||||
|
goto top2str;
|
||||||
|
}
|
||||||
|
case 'f': { /* a 'lua_Number' */
|
||||||
|
setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
|
||||||
|
top2str: /* convert the top element to a string */
|
||||||
|
luaD_inctop(L);
|
||||||
luaO_tostring(L, L->top - 1);
|
luaO_tostring(L, L->top - 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'I': {
|
case 'p': { /* a pointer */
|
||||||
setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
|
char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
|
||||||
luaO_tostring(L, L->top - 1);
|
int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *));
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'f': {
|
|
||||||
setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
|
|
||||||
luaO_tostring(L, L->top - 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'p': {
|
|
||||||
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
|
|
||||||
int l = sprintf(buff, "%p", va_arg(argp, void *));
|
|
||||||
pushstr(L, buff, l);
|
pushstr(L, buff, l);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'U': {
|
case 'U': { /* an 'int' as a UTF-8 sequence */
|
||||||
char buff[UTF8BUFFSZ];
|
char buff[UTF8BUFFSZ];
|
||||||
int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
|
int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
|
||||||
pushstr(L, buff + UTF8BUFFSZ - l, l);
|
pushstr(L, buff + UTF8BUFFSZ - l, l);
|
||||||
|
|||||||
135
lobject.h
135
lobject.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lobject.h,v 2.102 2014/09/04 18:15:29 roberto Exp roberto $
|
** $Id: lobject.h,v 2.115 2015/10/28 17:28:40 roberto Exp roberto $
|
||||||
** Type definitions for Lua objects
|
** Type definitions for Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -19,8 +19,8 @@
|
|||||||
/*
|
/*
|
||||||
** Extra tags for non-values
|
** Extra tags for non-values
|
||||||
*/
|
*/
|
||||||
#define LUA_TPROTO LUA_NUMTAGS
|
#define LUA_TPROTO LUA_NUMTAGS /* function prototypes */
|
||||||
#define LUA_TDEADKEY (LUA_NUMTAGS+1)
|
#define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
|
** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
|
||||||
@@ -35,8 +35,6 @@
|
|||||||
** bit 6: whether value is collectable
|
** bit 6: whether value is collectable
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define VARBITS (3 << 4)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** LUA_TFUNCTION variants:
|
** LUA_TFUNCTION variants:
|
||||||
@@ -90,22 +88,32 @@ struct GCObject {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Union of all Lua values
|
|
||||||
*/
|
|
||||||
typedef union Value Value;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Tagged Values. This is the basic representation of values in Lua,
|
** Tagged Values. This is the basic representation of values in Lua,
|
||||||
** an actual value plus a tag with its type.
|
** an actual value plus a tag with its type.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Union of all Lua values
|
||||||
|
*/
|
||||||
|
typedef union Value {
|
||||||
|
GCObject *gc; /* collectable objects */
|
||||||
|
void *p; /* light userdata */
|
||||||
|
int b; /* booleans */
|
||||||
|
lua_CFunction f; /* light C functions */
|
||||||
|
lua_Integer i; /* integer numbers */
|
||||||
|
lua_Number n; /* float numbers */
|
||||||
|
} Value;
|
||||||
|
|
||||||
|
|
||||||
#define TValuefields Value value_; int tt_
|
#define TValuefields Value value_; int tt_
|
||||||
|
|
||||||
typedef struct lua_TValue TValue;
|
|
||||||
|
typedef struct lua_TValue {
|
||||||
|
TValuefields;
|
||||||
|
} TValue;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* macro defining a nil value */
|
/* macro defining a nil value */
|
||||||
@@ -154,6 +162,8 @@ typedef struct lua_TValue TValue;
|
|||||||
/* Macros to access values */
|
/* Macros to access values */
|
||||||
#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
|
#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
|
||||||
#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
|
#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
|
||||||
|
#define nvalue(o) check_exp(ttisnumber(o), \
|
||||||
|
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
|
||||||
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
|
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
|
||||||
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
||||||
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
|
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
|
||||||
@@ -177,9 +187,9 @@ typedef struct lua_TValue TValue;
|
|||||||
/* Macros for internal tests */
|
/* Macros for internal tests */
|
||||||
#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
|
#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
|
||||||
|
|
||||||
#define checkliveness(g,obj) \
|
#define checkliveness(L,obj) \
|
||||||
lua_longassert(!iscollectable(obj) || \
|
lua_longassert(!iscollectable(obj) || \
|
||||||
(righttt(obj) && !isdead(g,gcvalue(obj))))
|
(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
|
||||||
|
|
||||||
|
|
||||||
/* Macros to set values */
|
/* Macros to set values */
|
||||||
@@ -188,9 +198,15 @@ typedef struct lua_TValue TValue;
|
|||||||
#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_TNUMFLT); }
|
||||||
|
|
||||||
|
#define chgfltvalue(obj,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_TNUMINT); }
|
||||||
|
|
||||||
|
#define chgivalue(obj,x) \
|
||||||
|
{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
|
||||||
|
|
||||||
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
|
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
|
||||||
|
|
||||||
#define setfvalue(obj,x) \
|
#define setfvalue(obj,x) \
|
||||||
@@ -209,32 +225,32 @@ typedef struct lua_TValue TValue;
|
|||||||
#define setsvalue(L,obj,x) \
|
#define setsvalue(L,obj,x) \
|
||||||
{ TValue *io = (obj); TString *x_ = (x); \
|
{ TValue *io = (obj); TString *x_ = (x); \
|
||||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
|
val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#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_TUSERDATA)); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#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_TTHREAD)); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#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_TLCL)); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#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_TCCL)); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#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_TTABLE)); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
|
#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
|
||||||
|
|
||||||
@@ -242,7 +258,7 @@ typedef struct lua_TValue TValue;
|
|||||||
|
|
||||||
#define setobj(L,obj1,obj2) \
|
#define setobj(L,obj1,obj2) \
|
||||||
{ TValue *io1=(obj1); *io1 = *(obj2); \
|
{ TValue *io1=(obj1); *io1 = *(obj2); \
|
||||||
(void)L; checkliveness(G(L),io1); }
|
(void)L; checkliveness(L,io1); }
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -258,12 +274,13 @@ typedef struct lua_TValue TValue;
|
|||||||
#define setptvalue2s setptvalue
|
#define setptvalue2s setptvalue
|
||||||
/* from table to same table */
|
/* from table to same table */
|
||||||
#define setobjt2t setobj
|
#define setobjt2t setobj
|
||||||
/* to table */
|
|
||||||
#define setobj2t setobj
|
|
||||||
/* to new object */
|
/* to new object */
|
||||||
#define setobj2n setobj
|
#define setobj2n setobj
|
||||||
#define setsvalue2n setsvalue
|
#define setsvalue2n setsvalue
|
||||||
|
|
||||||
|
/* to table (define it as an expression to be used in macros) */
|
||||||
|
#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -274,21 +291,6 @@ typedef struct lua_TValue TValue;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
union Value {
|
|
||||||
GCObject *gc; /* collectable objects */
|
|
||||||
void *p; /* light userdata */
|
|
||||||
int b; /* booleans */
|
|
||||||
lua_CFunction f; /* light C functions */
|
|
||||||
lua_Integer i; /* integer numbers */
|
|
||||||
lua_Number n; /* float numbers */
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct lua_TValue {
|
|
||||||
TValuefields;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
typedef TValue *StkId; /* index to stack elements */
|
typedef TValue *StkId; /* index to stack elements */
|
||||||
|
|
||||||
|
|
||||||
@@ -301,9 +303,12 @@ typedef TValue *StkId; /* index to stack elements */
|
|||||||
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 */
|
||||||
unsigned int hash;
|
unsigned int hash;
|
||||||
size_t len; /* number of characters in string */
|
union {
|
||||||
struct TString *hnext; /* linked list for hash table */
|
size_t lnglen; /* length for long strings */
|
||||||
|
struct TString *hnext; /* linked list for hash table */
|
||||||
|
} u;
|
||||||
} TString;
|
} TString;
|
||||||
|
|
||||||
|
|
||||||
@@ -320,13 +325,19 @@ typedef union UTString {
|
|||||||
** Get the actual string (array of bytes) from a 'TString'.
|
** Get the actual string (array of bytes) from a 'TString'.
|
||||||
** (Access to 'extra' ensures that value is really a 'TString'.)
|
** (Access to 'extra' ensures that value is really a 'TString'.)
|
||||||
*/
|
*/
|
||||||
#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString))
|
|
||||||
#define getstr(ts) \
|
#define getstr(ts) \
|
||||||
check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))
|
check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
|
||||||
|
|
||||||
|
|
||||||
/* get the actual string (array of bytes) from a Lua value */
|
/* get the actual string (array of bytes) from a Lua value */
|
||||||
#define svalue(o) getstr(tsvalue(o))
|
#define svalue(o) getstr(tsvalue(o))
|
||||||
|
|
||||||
|
/* get string length from 'TString *s' */
|
||||||
|
#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
|
||||||
|
|
||||||
|
/* get string length from 'TValue *o' */
|
||||||
|
#define vslen(o) tsslen(tsvalue(o))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Header for userdata; memory area follows the end of this structure
|
** Header for userdata; memory area follows the end of this structure
|
||||||
@@ -345,7 +356,7 @@ typedef struct Udata {
|
|||||||
** Ensures that address after this type is always fully aligned.
|
** Ensures that address after this type is always fully aligned.
|
||||||
*/
|
*/
|
||||||
typedef union UUdata {
|
typedef union UUdata {
|
||||||
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
|
||||||
Udata uv;
|
Udata uv;
|
||||||
} UUdata;
|
} UUdata;
|
||||||
|
|
||||||
@@ -359,14 +370,14 @@ typedef union UUdata {
|
|||||||
|
|
||||||
#define setuservalue(L,u,o) \
|
#define setuservalue(L,u,o) \
|
||||||
{ const TValue *io=(o); Udata *iu = (u); \
|
{ const TValue *io=(o); Udata *iu = (u); \
|
||||||
iu->user_ = io->value_; iu->ttuv_ = io->tt_; \
|
iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
|
|
||||||
#define getuservalue(L,u,o) \
|
#define getuservalue(L,u,o) \
|
||||||
{ TValue *io=(o); const Udata *iu = (u); \
|
{ TValue *io=(o); const Udata *iu = (u); \
|
||||||
io->value_ = iu->user_; io->tt_ = iu->ttuv_; \
|
io->value_ = iu->user_; settt_(io, iu->ttuv_); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(L,io); }
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -374,7 +385,7 @@ typedef union UUdata {
|
|||||||
*/
|
*/
|
||||||
typedef struct Upvaldesc {
|
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 */
|
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) */
|
||||||
} Upvaldesc;
|
} Upvaldesc;
|
||||||
|
|
||||||
@@ -396,23 +407,23 @@ typedef struct LocVar {
|
|||||||
typedef struct Proto {
|
typedef struct Proto {
|
||||||
CommonHeader;
|
CommonHeader;
|
||||||
lu_byte numparams; /* number of fixed parameters */
|
lu_byte numparams; /* number of fixed parameters */
|
||||||
lu_byte is_vararg;
|
lu_byte is_vararg; /* 2: declared vararg; 1: uses vararg */
|
||||||
lu_byte maxstacksize; /* maximum stack used by this function */
|
lu_byte maxstacksize; /* number of registers needed by this function */
|
||||||
int sizeupvalues; /* size of 'upvalues' */
|
int sizeupvalues; /* size of 'upvalues' */
|
||||||
int sizek; /* size of `k' */
|
int sizek; /* size of 'k' */
|
||||||
int sizecode;
|
int sizecode;
|
||||||
int sizelineinfo;
|
int sizelineinfo;
|
||||||
int sizep; /* size of `p' */
|
int sizep; /* size of 'p' */
|
||||||
int sizelocvars;
|
int sizelocvars;
|
||||||
int linedefined;
|
int linedefined; /* debug information */
|
||||||
int lastlinedefined;
|
int lastlinedefined; /* debug information */
|
||||||
TValue *k; /* constants used by the function */
|
TValue *k; /* constants used by the function */
|
||||||
Instruction *code;
|
Instruction *code; /* opcodes */
|
||||||
struct Proto **p; /* functions defined inside the function */
|
struct Proto **p; /* functions defined inside the function */
|
||||||
int *lineinfo; /* map from opcodes to source lines (debug information) */
|
int *lineinfo; /* map from opcodes to source lines (debug information) */
|
||||||
LocVar *locvars; /* information about local variables (debug information) */
|
LocVar *locvars; /* information about local variables (debug information) */
|
||||||
Upvaldesc *upvalues; /* upvalue information */
|
Upvaldesc *upvalues; /* upvalue information */
|
||||||
struct LClosure *cache; /* last created closure with this prototype */
|
struct LClosure *cache; /* last-created closure with this prototype */
|
||||||
TString *source; /* used for debug information */
|
TString *source; /* used for debug information */
|
||||||
GCObject *gclist;
|
GCObject *gclist;
|
||||||
} Proto;
|
} Proto;
|
||||||
@@ -471,10 +482,10 @@ typedef union TKey {
|
|||||||
|
|
||||||
|
|
||||||
/* copy a value into a key without messing up field 'next' */
|
/* copy a value into a key without messing up field 'next' */
|
||||||
#define setkey(L,key,obj) \
|
#define setnodekey(L,key,obj) \
|
||||||
{ TKey *k_=(key); const TValue *io_=(obj); \
|
{ TKey *k_=(key); const TValue *io_=(obj); \
|
||||||
k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
|
k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
|
||||||
(void)L; checkliveness(G(L),io_); }
|
(void)L; checkliveness(L,io_); }
|
||||||
|
|
||||||
|
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
@@ -486,8 +497,8 @@ typedef struct Node {
|
|||||||
typedef struct Table {
|
typedef struct Table {
|
||||||
CommonHeader;
|
CommonHeader;
|
||||||
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
||||||
lu_byte lsizenode; /* log2 of size of `node' array */
|
lu_byte lsizenode; /* log2 of size of 'node' array */
|
||||||
unsigned int sizearray; /* size of `array' array */
|
unsigned int sizearray; /* size of 'array' array */
|
||||||
TValue *array; /* array part */
|
TValue *array; /* array part */
|
||||||
Node *node;
|
Node *node;
|
||||||
Node *lastfree; /* any free position is before this position */
|
Node *lastfree; /* any free position is before this position */
|
||||||
@@ -498,7 +509,7 @@ typedef struct Table {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** `module' operation for hashing (size is always a power of 2)
|
** 'module' operation for hashing (size is always a power of 2)
|
||||||
*/
|
*/
|
||||||
#define lmod(s,size) \
|
#define lmod(s,size) \
|
||||||
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
|
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.c,v 1.52 2013/12/18 14:12:03 roberto Exp roberto $
|
** $Id: lopcodes.c,v 1.54 2014/11/02 19:19:04 roberto Exp roberto $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define lopcodes_c
|
#define lopcodes_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lopcodes.h"
|
#include "lopcodes.h"
|
||||||
|
|
||||||
|
|||||||
28
lopcodes.h
28
lopcodes.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.h,v 1.146 2013/12/30 20:47:58 roberto Exp roberto $
|
** $Id: lopcodes.h,v 1.147 2014/10/20 18:29:55 roberto Exp roberto $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,12 +14,12 @@
|
|||||||
We assume that instructions are unsigned numbers.
|
We assume that instructions are unsigned numbers.
|
||||||
All instructions have an opcode in the first 6 bits.
|
All instructions have an opcode in the first 6 bits.
|
||||||
Instructions can have the following fields:
|
Instructions can have the following fields:
|
||||||
`A' : 8 bits
|
'A' : 8 bits
|
||||||
`B' : 9 bits
|
'B' : 9 bits
|
||||||
`C' : 9 bits
|
'C' : 9 bits
|
||||||
'Ax' : 26 bits ('A', 'B', and 'C' together)
|
'Ax' : 26 bits ('A', 'B', and 'C' together)
|
||||||
`Bx' : 18 bits (`B' and `C' together)
|
'Bx' : 18 bits ('B' and 'C' together)
|
||||||
`sBx' : signed Bx
|
'sBx' : signed Bx
|
||||||
|
|
||||||
A signed argument is represented in excess K; that is, the number
|
A signed argument is represented in excess K; that is, the number
|
||||||
value is the unsigned value minus K. K is exactly the maximum value
|
value is the unsigned value minus K. K is exactly the maximum value
|
||||||
@@ -58,7 +58,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
|
|||||||
*/
|
*/
|
||||||
#if SIZE_Bx < LUAI_BITSINT-1
|
#if SIZE_Bx < LUAI_BITSINT-1
|
||||||
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
||||||
#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
|
#define MAXARG_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
|
||||||
#else
|
#else
|
||||||
#define MAXARG_Bx MAX_INT
|
#define MAXARG_Bx MAX_INT
|
||||||
#define MAXARG_sBx MAX_INT
|
#define MAXARG_sBx MAX_INT
|
||||||
@@ -76,10 +76,10 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
|
|||||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||||
|
|
||||||
|
|
||||||
/* 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))
|
||||||
|
|
||||||
/* creates a mask with `n' 0 bits at position `p' */
|
/* creates a mask with 'n' 0 bits at position 'p' */
|
||||||
#define MASK0(n,p) (~MASK1(n,p))
|
#define MASK0(n,p) (~MASK1(n,p))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -238,16 +238,16 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
|||||||
|
|
||||||
/*===========================================================================
|
/*===========================================================================
|
||||||
Notes:
|
Notes:
|
||||||
(*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is
|
(*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
|
||||||
set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
|
set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
|
||||||
OP_SETLIST) may use `top'.
|
OP_SETLIST) may use 'top'.
|
||||||
|
|
||||||
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
|
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
|
||||||
set top (like in OP_CALL with C == 0).
|
set top (like in OP_CALL with C == 0).
|
||||||
|
|
||||||
(*) 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 B = `top'; if (C == 0) then next
|
(*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
|
||||||
'instruction' is EXTRAARG(real C).
|
'instruction' is EXTRAARG(real C).
|
||||||
|
|
||||||
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
||||||
@@ -255,7 +255,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
|||||||
(*) For comparisons, A specifies what condition the test should accept
|
(*) For comparisons, A specifies what condition the test should accept
|
||||||
(true or false).
|
(true or false).
|
||||||
|
|
||||||
(*) All `skips' (pc++) assume that next instruction is a jump.
|
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
||||||
|
|
||||||
===========================================================================*/
|
===========================================================================*/
|
||||||
|
|
||||||
|
|||||||
239
loslib.c
239
loslib.c
@@ -1,9 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: loslib.c,v 1.48 2014/10/08 19:57:31 roberto Exp roberto $
|
** $Id: loslib.c,v 1.63 2016/02/09 12:16:11 roberto Exp roberto $
|
||||||
** Standard Operating System library
|
** Standard Operating System library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define loslib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
@@ -11,32 +16,48 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define loslib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
|
||||||
/*
|
/*
|
||||||
** list of valid conversion specifiers for the 'strftime' function
|
** {==================================================================
|
||||||
|
** List of valid conversion specifiers for the 'strftime' function;
|
||||||
|
** options are grouped by length; group of length 2 start with '||'.
|
||||||
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
||||||
|
|
||||||
#if !defined(LUA_USE_POSIX)
|
/* options for ANSI C 89 */
|
||||||
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
|
#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%"
|
||||||
#else
|
|
||||||
#define LUA_STRFTIMEOPTIONS \
|
/* options for ISO C 99 and POSIX */
|
||||||
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
|
#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
|
||||||
"E", "cCxXyY", \
|
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy"
|
||||||
"O", "deHImMSuUVwWy" }
|
|
||||||
|
/* options for Windows */
|
||||||
|
#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \
|
||||||
|
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y"
|
||||||
|
|
||||||
|
#if defined(LUA_USE_WINDOWS)
|
||||||
|
#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN
|
||||||
|
#elif defined(LUA_USE_C89)
|
||||||
|
#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89
|
||||||
|
#else /* C99 specification */
|
||||||
|
#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Configuration for time-related stuff
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
#if !defined(l_time_t) /* { */
|
#if !defined(l_time_t) /* { */
|
||||||
/*
|
/*
|
||||||
@@ -44,20 +65,51 @@
|
|||||||
*/
|
*/
|
||||||
#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_checktime(L,a) ((time_t)luaL_checkinteger(L,a))
|
|
||||||
|
static time_t l_checktime (lua_State *L, int arg) {
|
||||||
|
lua_Integer t = luaL_checkinteger(L, arg);
|
||||||
|
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
||||||
|
return (time_t)t;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(l_gmtime) /* { */
|
||||||
#if !defined(lua_tmpnam) /* { */
|
|
||||||
/*
|
/*
|
||||||
** By default, Lua uses tmpnam except when POSIX is available, where it
|
** By default, Lua uses gmtime/localtime, except when POSIX is available,
|
||||||
** uses mkstemp.
|
** where it uses gmtime_r/localtime_r
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(LUA_USE_POSIX) /* { */
|
#if defined(LUA_USE_POSIX) /* { */
|
||||||
|
|
||||||
|
#define l_gmtime(t,r) gmtime_r(t,r)
|
||||||
|
#define l_localtime(t,r) localtime_r(t,r)
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
|
/* ISO C definitions */
|
||||||
|
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
|
||||||
|
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Configuration for 'tmpnam':
|
||||||
|
** By default, Lua uses tmpnam except when POSIX is available, where
|
||||||
|
** it uses mkstemp.
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
#if !defined(lua_tmpnam) /* { */
|
||||||
|
|
||||||
|
#if defined(LUA_USE_POSIX) /* { */
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define LUA_TMPNAMBUFSIZE 32
|
#define LUA_TMPNAMBUFSIZE 32
|
||||||
@@ -74,38 +126,17 @@
|
|||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definitions */
|
/* ISO C definitions */
|
||||||
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
||||||
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if !defined(l_gmtime) /* { */
|
|
||||||
/*
|
|
||||||
** By default, Lua uses gmtime/localtime, except when POSIX is available,
|
|
||||||
** where it uses gmtime_r/localtime_r
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(LUA_USE_POSIX) /* { */
|
|
||||||
|
|
||||||
#define l_gmtime(t,r) gmtime_r(t,r)
|
|
||||||
#define l_localtime(t,r) localtime_r(t,r)
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
|
|
||||||
/* ANSI definitions */
|
|
||||||
#define l_gmtime(t,r) ((void)r, gmtime(t))
|
|
||||||
#define l_localtime(t,r) ((void)r, localtime(t))
|
|
||||||
|
|
||||||
#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);
|
||||||
@@ -175,6 +206,23 @@ static void setboolfield (lua_State *L, const char *key, int value) {
|
|||||||
lua_setfield(L, -2, key);
|
lua_setfield(L, -2, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Set all fields from structure 'tm' in the table on top of the stack
|
||||||
|
*/
|
||||||
|
static void setallfields (lua_State *L, struct tm *stm) {
|
||||||
|
setfield(L, "sec", stm->tm_sec);
|
||||||
|
setfield(L, "min", stm->tm_min);
|
||||||
|
setfield(L, "hour", stm->tm_hour);
|
||||||
|
setfield(L, "day", stm->tm_mday);
|
||||||
|
setfield(L, "month", stm->tm_mon + 1);
|
||||||
|
setfield(L, "year", stm->tm_year + 1900);
|
||||||
|
setfield(L, "wday", stm->tm_wday + 1);
|
||||||
|
setfield(L, "yday", stm->tm_yday + 1);
|
||||||
|
setboolfield(L, "isdst", stm->tm_isdst);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int getboolfield (lua_State *L, const char *key) {
|
static int getboolfield (lua_State *L, const char *key) {
|
||||||
int res;
|
int res;
|
||||||
res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
|
res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
|
||||||
@@ -183,36 +231,42 @@ static int getboolfield (lua_State *L, const char *key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int getfield (lua_State *L, const char *key, int d) {
|
/* maximum value for date fields (to avoid arithmetic overflows with 'int') */
|
||||||
int res, isnum;
|
#if !defined(L_MAXDATEFIELD)
|
||||||
lua_getfield(L, -1, key);
|
#define L_MAXDATEFIELD (INT_MAX / 2)
|
||||||
res = (int)lua_tointegerx(L, -1, &isnum);
|
#endif
|
||||||
if (!isnum) {
|
|
||||||
if (d < 0)
|
static int getfield (lua_State *L, const char *key, int d, int delta) {
|
||||||
|
int isnum;
|
||||||
|
int t = lua_getfield(L, -1, key); /* get field and its type */
|
||||||
|
lua_Integer res = lua_tointegerx(L, -1, &isnum);
|
||||||
|
if (!isnum) { /* field is not an integer? */
|
||||||
|
if (t != LUA_TNIL) /* some other value? */
|
||||||
|
return luaL_error(L, "field '%s' is not an integer", key);
|
||||||
|
else if (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 {
|
||||||
|
if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
|
||||||
|
return luaL_error(L, "field '%s' is out-of-bound", key);
|
||||||
|
res -= delta;
|
||||||
|
}
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
return res;
|
return (int)res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *checkoption (lua_State *L, const char *conv, char *buff) {
|
static const char *checkoption (lua_State *L, const char *conv, char *buff) {
|
||||||
static const char *const options[] = LUA_STRFTIMEOPTIONS;
|
const char *option;
|
||||||
unsigned int i;
|
int oplen = 1;
|
||||||
for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
|
for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) {
|
||||||
if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
|
if (*option == '|') /* next block? */
|
||||||
buff[1] = *conv;
|
oplen++; /* next length */
|
||||||
if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
|
else if (memcmp(conv, option, oplen) == 0) { /* match? */
|
||||||
buff[2] = '\0'; /* end buffer */
|
memcpy(buff, conv, oplen); /* copy valid option to buffer */
|
||||||
return conv + 1;
|
buff[oplen] = '\0';
|
||||||
}
|
return conv + oplen; /* return next item */
|
||||||
else if (*(conv + 1) != '\0' &&
|
|
||||||
strchr(options[i + 1], *(conv + 1)) != NULL) {
|
|
||||||
buff[2] = *(conv + 1); /* valid two-char conversion specifier */
|
|
||||||
buff[3] = '\0'; /* end buffer */
|
|
||||||
return conv + 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
luaL_argerror(L, 1,
|
luaL_argerror(L, 1,
|
||||||
@@ -221,44 +275,40 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum size for an individual 'strftime' item */
|
||||||
|
#define SIZETIMEFMT 250
|
||||||
|
|
||||||
|
|
||||||
static int os_date (lua_State *L) {
|
static int os_date (lua_State *L) {
|
||||||
const char *s = luaL_optstring(L, 1, "%c");
|
const char *s = luaL_optstring(L, 1, "%c");
|
||||||
time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
|
time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
|
||||||
struct tm tmr, *stm;
|
struct tm tmr, *stm;
|
||||||
if (*s == '!') { /* UTC? */
|
if (*s == '!') { /* UTC? */
|
||||||
stm = l_gmtime(&t, &tmr);
|
stm = l_gmtime(&t, &tmr);
|
||||||
s++; /* skip `!' */
|
s++; /* skip '!' */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
stm = l_localtime(&t, &tmr);
|
stm = l_localtime(&t, &tmr);
|
||||||
if (stm == NULL) /* invalid date? */
|
if (stm == NULL) /* invalid date? */
|
||||||
lua_pushnil(L);
|
luaL_error(L, "time result cannot be represented in this installation");
|
||||||
else 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 */
|
||||||
setfield(L, "sec", stm->tm_sec);
|
setallfields(L, stm);
|
||||||
setfield(L, "min", stm->tm_min);
|
|
||||||
setfield(L, "hour", stm->tm_hour);
|
|
||||||
setfield(L, "day", stm->tm_mday);
|
|
||||||
setfield(L, "month", stm->tm_mon+1);
|
|
||||||
setfield(L, "year", stm->tm_year+1900);
|
|
||||||
setfield(L, "wday", stm->tm_wday+1);
|
|
||||||
setfield(L, "yday", stm->tm_yday+1);
|
|
||||||
setboolfield(L, "isdst", stm->tm_isdst);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
char cc[4];
|
char cc[4]; /* buffer for individual conversion specifiers */
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
cc[0] = '%';
|
cc[0] = '%';
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
while (*s) {
|
while (*s) {
|
||||||
if (*s != '%') /* no conversion specifier? */
|
if (*s != '%') /* not a conversion specifier? */
|
||||||
luaL_addchar(&b, *s++);
|
luaL_addchar(&b, *s++);
|
||||||
else {
|
else {
|
||||||
size_t reslen;
|
size_t reslen;
|
||||||
char buff[200]; /* should be big enough for any conversion result */
|
char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
|
||||||
s = checkoption(L, s + 1, cc);
|
s = checkoption(L, s + 1, cc + 1); /* copy specifier to 'cc' */
|
||||||
reslen = strftime(buff, sizeof(buff), cc, stm);
|
reslen = strftime(buff, SIZETIMEFMT, cc, stm);
|
||||||
luaL_addlstring(&b, buff, reslen);
|
luaL_addsize(&b, reslen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
luaL_pushresult(&b);
|
luaL_pushresult(&b);
|
||||||
@@ -275,28 +325,27 @@ 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);
|
ts.tm_sec = getfield(L, "sec", 0, 0);
|
||||||
ts.tm_min = getfield(L, "min", 0);
|
ts.tm_min = getfield(L, "min", 0, 0);
|
||||||
ts.tm_hour = getfield(L, "hour", 12);
|
ts.tm_hour = getfield(L, "hour", 12, 0);
|
||||||
ts.tm_mday = getfield(L, "day", -1);
|
ts.tm_mday = getfield(L, "day", -1, 0);
|
||||||
ts.tm_mon = getfield(L, "month", -1) - 1;
|
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_isdst = getboolfield(L, "isdst");
|
ts.tm_isdst = getboolfield(L, "isdst");
|
||||||
t = mktime(&ts);
|
t = mktime(&ts);
|
||||||
|
setallfields(L, &ts); /* update fields with normalized values */
|
||||||
}
|
}
|
||||||
if (t != (time_t)(l_timet)t)
|
if (t != (time_t)(l_timet)t || t == (time_t)(-1))
|
||||||
luaL_error(L, "time result cannot be represented in this Lua instalation");
|
luaL_error(L, "time result cannot be represented in this installation");
|
||||||
else if (t == (time_t)(-1))
|
l_pushtime(L, t);
|
||||||
lua_pushnil(L);
|
|
||||||
else
|
|
||||||
l_pushtime(L, t);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int os_difftime (lua_State *L) {
|
static int os_difftime (lua_State *L) {
|
||||||
double res = difftime((l_checktime(L, 1)), (l_checktime(L, 2)));
|
time_t t1 = l_checktime(L, 1);
|
||||||
lua_pushnumber(L, (lua_Number)res);
|
time_t t2 = l_checktime(L, 2);
|
||||||
|
lua_pushnumber(L, (lua_Number)difftime(t1, t2));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
125
lparser.c
125
lparser.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.c,v 2.142 2014/07/21 16:02:10 roberto Exp roberto $
|
** $Id: lparser.c,v 2.152 2016/03/07 19:25:39 roberto Exp roberto $
|
||||||
** Lua Parser
|
** Lua Parser
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lparser_c
|
#define lparser_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lcode.h"
|
#include "lcode.h"
|
||||||
@@ -45,11 +47,11 @@
|
|||||||
*/
|
*/
|
||||||
typedef struct BlockCnt {
|
typedef struct BlockCnt {
|
||||||
struct BlockCnt *previous; /* chain */
|
struct BlockCnt *previous; /* chain */
|
||||||
short firstlabel; /* index of first label in this block */
|
int firstlabel; /* index of first label in this block */
|
||||||
short firstgoto; /* index of first pending goto in this block */
|
int firstgoto; /* index of first pending goto in this block */
|
||||||
lu_byte nactvar; /* # active locals outside the block */
|
lu_byte nactvar; /* # active locals outside the block */
|
||||||
lu_byte upval; /* true if some variable in the block is an upvalue */
|
lu_byte upval; /* true if some variable in the block is an upvalue */
|
||||||
lu_byte isloop; /* true if `block' is a loop */
|
lu_byte isloop; /* true if 'block' is a loop */
|
||||||
} BlockCnt;
|
} BlockCnt;
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +65,7 @@ static void expr (LexState *ls, expdesc *v);
|
|||||||
|
|
||||||
/* semantic error */
|
/* semantic error */
|
||||||
static l_noret semerror (LexState *ls, const char *msg) {
|
static l_noret semerror (LexState *ls, const char *msg) {
|
||||||
ls->t.token = 0; /* remove 'near to' from final message */
|
ls->t.token = 0; /* remove "near <token>" from final message */
|
||||||
luaX_syntaxerror(ls, msg);
|
luaX_syntaxerror(ls, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +164,8 @@ static int registerlocalvar (LexState *ls, TString *varname) {
|
|||||||
int oldsize = f->sizelocvars;
|
int oldsize = f->sizelocvars;
|
||||||
luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
|
luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
|
||||||
LocVar, SHRT_MAX, "local variables");
|
LocVar, SHRT_MAX, "local variables");
|
||||||
while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
|
while (oldsize < f->sizelocvars)
|
||||||
|
f->locvars[oldsize++].varname = NULL;
|
||||||
f->locvars[fs->nlocvars].varname = varname;
|
f->locvars[fs->nlocvars].varname = varname;
|
||||||
luaC_objbarrier(ls->L, f, varname);
|
luaC_objbarrier(ls->L, f, varname);
|
||||||
return fs->nlocvars++;
|
return fs->nlocvars++;
|
||||||
@@ -228,7 +231,8 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
|
|||||||
checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
|
checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
|
||||||
luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
|
luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
|
||||||
Upvaldesc, MAXUPVAL, "upvalues");
|
Upvaldesc, MAXUPVAL, "upvalues");
|
||||||
while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL;
|
while (oldsize < f->sizeupvalues)
|
||||||
|
f->upvalues[oldsize++].name = NULL;
|
||||||
f->upvalues[fs->nups].instack = (v->k == VLOCAL);
|
f->upvalues[fs->nups].instack = (v->k == VLOCAL);
|
||||||
f->upvalues[fs->nups].idx = cast_byte(v->u.info);
|
f->upvalues[fs->nups].idx = cast_byte(v->u.info);
|
||||||
f->upvalues[fs->nups].name = name;
|
f->upvalues[fs->nups].name = name;
|
||||||
@@ -253,7 +257,8 @@ static int searchvar (FuncState *fs, TString *n) {
|
|||||||
*/
|
*/
|
||||||
static void markupval (FuncState *fs, int level) {
|
static void markupval (FuncState *fs, int level) {
|
||||||
BlockCnt *bl = fs->bl;
|
BlockCnt *bl = fs->bl;
|
||||||
while (bl->nactvar > level) bl = bl->previous;
|
while (bl->nactvar > level)
|
||||||
|
bl = bl->previous;
|
||||||
bl->upval = 1;
|
bl->upval = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,27 +267,26 @@ static void markupval (FuncState *fs, int level) {
|
|||||||
Find variable with given name 'n'. If it is an upvalue, add this
|
Find variable with given name 'n'. If it is an upvalue, add this
|
||||||
upvalue into all intermediate functions.
|
upvalue into all intermediate functions.
|
||||||
*/
|
*/
|
||||||
static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
|
static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
|
||||||
if (fs == NULL) /* no more levels? */
|
if (fs == NULL) /* no more levels? */
|
||||||
return VVOID; /* default is global */
|
init_exp(var, VVOID, 0); /* default is global */
|
||||||
else {
|
else {
|
||||||
int v = searchvar(fs, n); /* look up locals at current level */
|
int v = searchvar(fs, n); /* look up locals at current level */
|
||||||
if (v >= 0) { /* found? */
|
if (v >= 0) { /* found? */
|
||||||
init_exp(var, VLOCAL, v); /* variable is local */
|
init_exp(var, VLOCAL, v); /* variable is local */
|
||||||
if (!base)
|
if (!base)
|
||||||
markupval(fs, v); /* local will be used as an upval */
|
markupval(fs, v); /* local will be used as an upval */
|
||||||
return VLOCAL;
|
|
||||||
}
|
}
|
||||||
else { /* not found as local at current level; try upvalues */
|
else { /* not found as local at current level; try upvalues */
|
||||||
int idx = searchupvalue(fs, n); /* try existing upvalues */
|
int idx = searchupvalue(fs, n); /* try existing upvalues */
|
||||||
if (idx < 0) { /* not found? */
|
if (idx < 0) { /* not found? */
|
||||||
if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */
|
singlevaraux(fs->prev, n, var, 0); /* try upper levels */
|
||||||
return VVOID; /* not found; is a global */
|
if (var->k == VVOID) /* not found? */
|
||||||
|
return; /* it is a global */
|
||||||
/* else was LOCAL or UPVAL */
|
/* else was LOCAL or UPVAL */
|
||||||
idx = newupvalue(fs, n, var); /* will be a new upvalue */
|
idx = newupvalue(fs, n, var); /* will be a new upvalue */
|
||||||
}
|
}
|
||||||
init_exp(var, VUPVAL, idx);
|
init_exp(var, VUPVAL, idx); /* new or old upvalue */
|
||||||
return VUPVAL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -291,10 +295,11 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
|
|||||||
static void singlevar (LexState *ls, expdesc *var) {
|
static void singlevar (LexState *ls, expdesc *var) {
|
||||||
TString *varname = str_checkname(ls);
|
TString *varname = str_checkname(ls);
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */
|
singlevaraux(fs, varname, var, 1);
|
||||||
|
if (var->k == VVOID) { /* global name? */
|
||||||
expdesc key;
|
expdesc key;
|
||||||
singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
|
singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
|
||||||
lua_assert(var->k == VLOCAL || var->k == VUPVAL);
|
lua_assert(var->k != VVOID); /* this one must exist */
|
||||||
codestring(ls, &key, varname); /* key is variable name */
|
codestring(ls, &key, varname); /* key is variable name */
|
||||||
luaK_indexed(fs, var, &key); /* env[varname] */
|
luaK_indexed(fs, var, &key); /* env[varname] */
|
||||||
}
|
}
|
||||||
@@ -384,7 +389,7 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name,
|
|||||||
l->arr[n].line = line;
|
l->arr[n].line = line;
|
||||||
l->arr[n].nactvar = ls->fs->nactvar;
|
l->arr[n].nactvar = ls->fs->nactvar;
|
||||||
l->arr[n].pc = pc;
|
l->arr[n].pc = pc;
|
||||||
l->n++;
|
l->n = n + 1;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,7 +411,7 @@ static void findgotos (LexState *ls, Labeldesc *lb) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** "export" pending gotos to outer level, to check them against
|
** export pending gotos to outer level, to check them against
|
||||||
** outer labels; if the block being exited has upvalues, and
|
** outer labels; if the block being exited has upvalues, and
|
||||||
** the goto exits the scope of any variable (which can be the
|
** the goto exits the scope of any variable (which can be the
|
||||||
** upvalue), close those variables being exited.
|
** upvalue), close those variables being exited.
|
||||||
@@ -442,7 +447,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** create a label named "break" to resolve break statements
|
** create a label named 'break' to resolve break statements
|
||||||
*/
|
*/
|
||||||
static void breaklabel (LexState *ls) {
|
static void breaklabel (LexState *ls) {
|
||||||
TString *n = luaS_new(ls->L, "break");
|
TString *n = luaS_new(ls->L, "break");
|
||||||
@@ -497,7 +502,8 @@ static Proto *addprototype (LexState *ls) {
|
|||||||
if (fs->np >= f->sizep) {
|
if (fs->np >= f->sizep) {
|
||||||
int oldsize = f->sizep;
|
int oldsize = f->sizep;
|
||||||
luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
|
luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
|
||||||
while (oldsize < f->sizep) f->p[oldsize++] = NULL;
|
while (oldsize < f->sizep)
|
||||||
|
f->p[oldsize++] = NULL;
|
||||||
}
|
}
|
||||||
f->p[fs->np++] = clp = luaF_newproto(L);
|
f->p[fs->np++] = clp = luaF_newproto(L);
|
||||||
luaC_objbarrier(L, f, clp);
|
luaC_objbarrier(L, f, clp);
|
||||||
@@ -574,7 +580,7 @@ static void close_func (LexState *ls) {
|
|||||||
/*
|
/*
|
||||||
** check whether current token is in the follow set of a block.
|
** check whether current token is in the follow set of a block.
|
||||||
** 'until' closes syntactical blocks, but do not close scope,
|
** 'until' closes syntactical blocks, but do not close scope,
|
||||||
** so it handled in separate.
|
** so it is handled in separate.
|
||||||
*/
|
*/
|
||||||
static int block_follow (LexState *ls, int withuntil) {
|
static int block_follow (LexState *ls, int withuntil) {
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
@@ -588,7 +594,7 @@ static int block_follow (LexState *ls, int withuntil) {
|
|||||||
|
|
||||||
|
|
||||||
static void statlist (LexState *ls) {
|
static void statlist (LexState *ls) {
|
||||||
/* statlist -> { stat [`;'] } */
|
/* statlist -> { stat [';'] } */
|
||||||
while (!block_follow(ls, 1)) {
|
while (!block_follow(ls, 1)) {
|
||||||
if (ls->t.token == TK_RETURN) {
|
if (ls->t.token == TK_RETURN) {
|
||||||
statement(ls);
|
statement(ls);
|
||||||
@@ -629,14 +635,14 @@ static void yindex (LexState *ls, expdesc *v) {
|
|||||||
struct ConsControl {
|
struct ConsControl {
|
||||||
expdesc v; /* last list item read */
|
expdesc v; /* last list item read */
|
||||||
expdesc *t; /* table descriptor */
|
expdesc *t; /* table descriptor */
|
||||||
int nh; /* total number of `record' elements */
|
int nh; /* total number of 'record' elements */
|
||||||
int na; /* total number of array elements */
|
int na; /* total number of array elements */
|
||||||
int tostore; /* number of array elements pending to be stored */
|
int tostore; /* number of array elements pending to be stored */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void recfield (LexState *ls, struct ConsControl *cc) {
|
static void recfield (LexState *ls, struct ConsControl *cc) {
|
||||||
/* recfield -> (NAME | `['exp1`]') = exp1 */
|
/* recfield -> (NAME | '['exp1']') = exp1 */
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
int reg = ls->fs->freereg;
|
int reg = ls->fs->freereg;
|
||||||
expdesc key, val;
|
expdesc key, val;
|
||||||
@@ -743,12 +749,12 @@ static void constructor (LexState *ls, expdesc *t) {
|
|||||||
|
|
||||||
|
|
||||||
static void parlist (LexState *ls) {
|
static void parlist (LexState *ls) {
|
||||||
/* parlist -> [ param { `,' param } ] */
|
/* parlist -> [ param { ',' param } ] */
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
Proto *f = fs->f;
|
Proto *f = fs->f;
|
||||||
int nparams = 0;
|
int nparams = 0;
|
||||||
f->is_vararg = 0;
|
f->is_vararg = 0;
|
||||||
if (ls->t.token != ')') { /* is `parlist' not empty? */
|
if (ls->t.token != ')') { /* is 'parlist' not empty? */
|
||||||
do {
|
do {
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
case TK_NAME: { /* param -> NAME */
|
case TK_NAME: { /* param -> NAME */
|
||||||
@@ -756,9 +762,9 @@ static void parlist (LexState *ls) {
|
|||||||
nparams++;
|
nparams++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TK_DOTS: { /* param -> `...' */
|
case TK_DOTS: { /* param -> '...' */
|
||||||
luaX_next(ls);
|
luaX_next(ls);
|
||||||
f->is_vararg = 1;
|
f->is_vararg = 2; /* declared vararg */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: luaX_syntaxerror(ls, "<name> or '...' expected");
|
default: luaX_syntaxerror(ls, "<name> or '...' expected");
|
||||||
@@ -772,7 +778,7 @@ static void parlist (LexState *ls) {
|
|||||||
|
|
||||||
|
|
||||||
static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
||||||
/* body -> `(' parlist `)' block END */
|
/* body -> '(' parlist ')' block END */
|
||||||
FuncState new_fs;
|
FuncState new_fs;
|
||||||
BlockCnt bl;
|
BlockCnt bl;
|
||||||
new_fs.f = addprototype(ls);
|
new_fs.f = addprototype(ls);
|
||||||
@@ -794,7 +800,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
|||||||
|
|
||||||
|
|
||||||
static int explist (LexState *ls, expdesc *v) {
|
static int explist (LexState *ls, expdesc *v) {
|
||||||
/* explist -> expr { `,' expr } */
|
/* explist -> expr { ',' expr } */
|
||||||
int n = 1; /* at least one expression */
|
int n = 1; /* at least one expression */
|
||||||
expr(ls, v);
|
expr(ls, v);
|
||||||
while (testnext(ls, ',')) {
|
while (testnext(ls, ',')) {
|
||||||
@@ -811,7 +817,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
|||||||
expdesc args;
|
expdesc args;
|
||||||
int base, nparams;
|
int base, nparams;
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
case '(': { /* funcargs -> `(' [ explist ] `)' */
|
case '(': { /* funcargs -> '(' [ explist ] ')' */
|
||||||
luaX_next(ls);
|
luaX_next(ls);
|
||||||
if (ls->t.token == ')') /* arg list is empty? */
|
if (ls->t.token == ')') /* arg list is empty? */
|
||||||
args.k = VVOID;
|
args.k = VVOID;
|
||||||
@@ -828,7 +834,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
|||||||
}
|
}
|
||||||
case TK_STRING: { /* funcargs -> STRING */
|
case TK_STRING: { /* funcargs -> STRING */
|
||||||
codestring(ls, &args, ls->t.seminfo.ts);
|
codestring(ls, &args, ls->t.seminfo.ts);
|
||||||
luaX_next(ls); /* must use `seminfo' before `next' */
|
luaX_next(ls); /* must use 'seminfo' before 'next' */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
@@ -894,14 +900,14 @@ static void suffixedexp (LexState *ls, expdesc *v) {
|
|||||||
fieldsel(ls, v);
|
fieldsel(ls, v);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case '[': { /* `[' exp1 `]' */
|
case '[': { /* '[' exp1 ']' */
|
||||||
expdesc key;
|
expdesc key;
|
||||||
luaK_exp2anyregup(fs, v);
|
luaK_exp2anyregup(fs, v);
|
||||||
yindex(ls, &key);
|
yindex(ls, &key);
|
||||||
luaK_indexed(fs, v, &key);
|
luaK_indexed(fs, v, &key);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ':': { /* `:' NAME funcargs */
|
case ':': { /* ':' NAME funcargs */
|
||||||
expdesc key;
|
expdesc key;
|
||||||
luaX_next(ls);
|
luaX_next(ls);
|
||||||
checkname(ls, &key);
|
checkname(ls, &key);
|
||||||
@@ -954,6 +960,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
|
|||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
check_condition(ls, fs->f->is_vararg,
|
check_condition(ls, fs->f->is_vararg,
|
||||||
"cannot use '...' outside a vararg function");
|
"cannot use '...' outside a vararg function");
|
||||||
|
fs->f->is_vararg = 1; /* function actually uses vararg */
|
||||||
init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
|
init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1035,7 +1042,7 @@ static const struct {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
|
** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
|
||||||
** where `binop' is any binary operator with a priority higher than `limit'
|
** where 'binop' is any binary operator with a priority higher than 'limit'
|
||||||
*/
|
*/
|
||||||
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
||||||
BinOpr op;
|
BinOpr op;
|
||||||
@@ -1049,7 +1056,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
|||||||
luaK_prefix(ls->fs, uop, v, line);
|
luaK_prefix(ls->fs, uop, v, line);
|
||||||
}
|
}
|
||||||
else simpleexp(ls, v);
|
else simpleexp(ls, v);
|
||||||
/* expand while operators have priorities higher than `limit' */
|
/* expand while operators have priorities higher than 'limit' */
|
||||||
op = getbinopr(ls->t.token);
|
op = getbinopr(ls->t.token);
|
||||||
while (op != OPR_NOBINOPR && priority[op].left > limit) {
|
while (op != OPR_NOBINOPR && priority[op].left > limit) {
|
||||||
expdesc v2;
|
expdesc v2;
|
||||||
@@ -1149,7 +1156,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
|
|||||||
"C levels");
|
"C levels");
|
||||||
assignment(ls, &nv, nvars+1);
|
assignment(ls, &nv, nvars+1);
|
||||||
}
|
}
|
||||||
else { /* assignment -> `=' explist */
|
else { /* assignment -> '=' explist */
|
||||||
int nexps;
|
int nexps;
|
||||||
checknext(ls, '=');
|
checknext(ls, '=');
|
||||||
nexps = explist(ls, &e);
|
nexps = explist(ls, &e);
|
||||||
@@ -1173,7 +1180,7 @@ static int cond (LexState *ls) {
|
|||||||
/* cond -> exp */
|
/* cond -> exp */
|
||||||
expdesc v;
|
expdesc v;
|
||||||
expr(ls, &v); /* read condition */
|
expr(ls, &v); /* read condition */
|
||||||
if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
|
if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */
|
||||||
luaK_goiftrue(ls->fs, &v);
|
luaK_goiftrue(ls->fs, &v);
|
||||||
return v.f;
|
return v.f;
|
||||||
}
|
}
|
||||||
@@ -1223,7 +1230,7 @@ static void labelstat (LexState *ls, TString *label, int line) {
|
|||||||
checkrepeated(fs, ll, label); /* check for repeated labels */
|
checkrepeated(fs, ll, label); /* check for repeated labels */
|
||||||
checknext(ls, TK_DBCOLON); /* skip double colon */
|
checknext(ls, TK_DBCOLON); /* skip double colon */
|
||||||
/* create new entry for this label */
|
/* create new entry for this label */
|
||||||
l = newlabelentry(ls, ll, label, line, fs->pc);
|
l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs));
|
||||||
skipnoopstat(ls); /* skip other no-op statements */
|
skipnoopstat(ls); /* skip other no-op statements */
|
||||||
if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
|
if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
|
||||||
/* assume that locals are already out of scope */
|
/* assume that locals are already out of scope */
|
||||||
@@ -1362,7 +1369,7 @@ static void forstat (LexState *ls, int line) {
|
|||||||
TString *varname;
|
TString *varname;
|
||||||
BlockCnt bl;
|
BlockCnt bl;
|
||||||
enterblock(fs, &bl, 1); /* scope for loop and control variables */
|
enterblock(fs, &bl, 1); /* scope for loop and control variables */
|
||||||
luaX_next(ls); /* skip `for' */
|
luaX_next(ls); /* skip 'for' */
|
||||||
varname = str_checkname(ls); /* first variable name */
|
varname = str_checkname(ls); /* first variable name */
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
case '=': fornum(ls, varname, line); break;
|
case '=': fornum(ls, varname, line); break;
|
||||||
@@ -1370,7 +1377,7 @@ static void forstat (LexState *ls, int line) {
|
|||||||
default: luaX_syntaxerror(ls, "'=' or 'in' expected");
|
default: luaX_syntaxerror(ls, "'=' or 'in' expected");
|
||||||
}
|
}
|
||||||
check_match(ls, TK_END, TK_FOR, line);
|
check_match(ls, TK_END, TK_FOR, line);
|
||||||
leaveblock(fs); /* loop scope (`break' jumps to this point) */
|
leaveblock(fs); /* loop scope ('break' jumps to this point) */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1400,7 +1407,7 @@ static void test_then_block (LexState *ls, int *escapelist) {
|
|||||||
enterblock(fs, &bl, 0);
|
enterblock(fs, &bl, 0);
|
||||||
jf = v.f;
|
jf = v.f;
|
||||||
}
|
}
|
||||||
statlist(ls); /* `then' part */
|
statlist(ls); /* 'then' part */
|
||||||
leaveblock(fs);
|
leaveblock(fs);
|
||||||
if (ls->t.token == TK_ELSE ||
|
if (ls->t.token == TK_ELSE ||
|
||||||
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
||||||
@@ -1417,7 +1424,7 @@ static void ifstat (LexState *ls, int line) {
|
|||||||
while (ls->t.token == TK_ELSEIF)
|
while (ls->t.token == TK_ELSEIF)
|
||||||
test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
|
test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
|
||||||
if (testnext(ls, TK_ELSE))
|
if (testnext(ls, TK_ELSE))
|
||||||
block(ls); /* `else' part */
|
block(ls); /* 'else' part */
|
||||||
check_match(ls, TK_END, TK_IF, line);
|
check_match(ls, TK_END, TK_IF, line);
|
||||||
luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
|
luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
|
||||||
}
|
}
|
||||||
@@ -1435,7 +1442,7 @@ static void localfunc (LexState *ls) {
|
|||||||
|
|
||||||
|
|
||||||
static void localstat (LexState *ls) {
|
static void localstat (LexState *ls) {
|
||||||
/* stat -> LOCAL NAME {`,' NAME} [`=' explist] */
|
/* stat -> LOCAL NAME {',' NAME} ['=' explist] */
|
||||||
int nvars = 0;
|
int nvars = 0;
|
||||||
int nexps;
|
int nexps;
|
||||||
expdesc e;
|
expdesc e;
|
||||||
@@ -1455,7 +1462,7 @@ static void localstat (LexState *ls) {
|
|||||||
|
|
||||||
|
|
||||||
static int funcname (LexState *ls, expdesc *v) {
|
static int funcname (LexState *ls, expdesc *v) {
|
||||||
/* funcname -> NAME {fieldsel} [`:' NAME] */
|
/* funcname -> NAME {fieldsel} [':' NAME] */
|
||||||
int ismethod = 0;
|
int ismethod = 0;
|
||||||
singlevar(ls, v);
|
singlevar(ls, v);
|
||||||
while (ls->t.token == '.')
|
while (ls->t.token == '.')
|
||||||
@@ -1476,7 +1483,7 @@ static void funcstat (LexState *ls, int line) {
|
|||||||
ismethod = funcname(ls, &v);
|
ismethod = funcname(ls, &v);
|
||||||
body(ls, &b, ismethod, line);
|
body(ls, &b, ismethod, line);
|
||||||
luaK_storevar(ls->fs, &v, &b);
|
luaK_storevar(ls->fs, &v, &b);
|
||||||
luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
|
luaK_fixline(ls->fs, line); /* definition "happens" in the first line */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1491,7 +1498,7 @@ static void exprstat (LexState *ls) {
|
|||||||
}
|
}
|
||||||
else { /* stat -> func */
|
else { /* stat -> func */
|
||||||
check_condition(ls, v.v.k == VCALL, "syntax error");
|
check_condition(ls, v.v.k == VCALL, "syntax error");
|
||||||
SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
|
SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1508,8 +1515,8 @@ static void retstat (LexState *ls) {
|
|||||||
if (hasmultret(e.k)) {
|
if (hasmultret(e.k)) {
|
||||||
luaK_setmultret(fs, &e);
|
luaK_setmultret(fs, &e);
|
||||||
if (e.k == VCALL && nret == 1) { /* tail call? */
|
if (e.k == VCALL && nret == 1) { /* tail call? */
|
||||||
SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
|
SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL);
|
||||||
lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
|
lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar);
|
||||||
}
|
}
|
||||||
first = fs->nactvar;
|
first = fs->nactvar;
|
||||||
nret = LUA_MULTRET; /* return all values */
|
nret = LUA_MULTRET; /* return all values */
|
||||||
@@ -1518,8 +1525,8 @@ static void retstat (LexState *ls) {
|
|||||||
if (nret == 1) /* only one single value? */
|
if (nret == 1) /* only one single value? */
|
||||||
first = luaK_exp2anyreg(fs, &e);
|
first = luaK_exp2anyreg(fs, &e);
|
||||||
else {
|
else {
|
||||||
luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
|
luaK_exp2nextreg(fs, &e); /* values must go to the stack */
|
||||||
first = fs->nactvar; /* return all `active' values */
|
first = fs->nactvar; /* return all active values */
|
||||||
lua_assert(nret == fs->freereg - first);
|
lua_assert(nret == fs->freereg - first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1608,7 +1615,7 @@ static void mainfunc (LexState *ls, FuncState *fs) {
|
|||||||
BlockCnt bl;
|
BlockCnt bl;
|
||||||
expdesc v;
|
expdesc v;
|
||||||
open_func(ls, fs, &bl);
|
open_func(ls, fs, &bl);
|
||||||
fs->f->is_vararg = 1; /* main function is always vararg */
|
fs->f->is_vararg = 2; /* main function is always declared vararg */
|
||||||
init_exp(&v, VLOCAL, 0); /* create and... */
|
init_exp(&v, VLOCAL, 0); /* create and... */
|
||||||
newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */
|
newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */
|
||||||
luaX_next(ls); /* read first token */
|
luaX_next(ls); /* read first token */
|
||||||
@@ -1624,13 +1631,13 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
|||||||
FuncState funcstate;
|
FuncState funcstate;
|
||||||
LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */
|
LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */
|
||||||
setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
|
setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
|
||||||
incr_top(L);
|
luaD_inctop(L);
|
||||||
lexstate.h = luaH_new(L); /* create table for scanner */
|
lexstate.h = luaH_new(L); /* create table for scanner */
|
||||||
sethvalue(L, L->top, lexstate.h); /* anchor it */
|
sethvalue(L, L->top, lexstate.h); /* anchor it */
|
||||||
incr_top(L);
|
luaD_inctop(L);
|
||||||
funcstate.f = cl->p = luaF_newproto(L);
|
funcstate.f = cl->p = luaF_newproto(L);
|
||||||
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
|
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
|
||||||
luaC_objbarrier(L, funcstate.f, funcstate.f->source);
|
lua_assert(iswhite(funcstate.f)); /* do not need barrier here */
|
||||||
lexstate.buff = buff;
|
lexstate.buff = buff;
|
||||||
lexstate.dyd = dyd;
|
lexstate.dyd = dyd;
|
||||||
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
|
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
|
||||||
|
|||||||
65
lparser.h
65
lparser.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.h,v 1.72 2013/08/30 16:01:37 roberto Exp roberto $
|
** $Id: lparser.h,v 1.75 2015/12/17 15:44:50 roberto Exp roberto $
|
||||||
** Lua Parser
|
** Lua Parser
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,25 +13,38 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Expression descriptor
|
** Expression and variable descriptor.
|
||||||
|
** Code generation for variables and expressions can be delayed to allow
|
||||||
|
** optimizations; An 'expdesc' structure describes a potentially-delayed
|
||||||
|
** variable/expression. It has a description of its "main" value plus a
|
||||||
|
** list of conditional jumps that can also produce its value (generated
|
||||||
|
** by short-circuit operators 'and'/'or').
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* kinds of variables/expressions */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VVOID, /* no value */
|
VVOID, /* when 'expdesc' describes the last expression a list,
|
||||||
VNIL,
|
this kind means an empty list (so, no expression) */
|
||||||
VTRUE,
|
VNIL, /* constant nil */
|
||||||
VFALSE,
|
VTRUE, /* constant true */
|
||||||
VK, /* info = index of constant in `k' */
|
VFALSE, /* constant false */
|
||||||
VKFLT, /* nval = numerical float value */
|
VK, /* constant in 'k'; info = index of constant in 'k' */
|
||||||
VKINT, /* nval = numerical integer value */
|
VKFLT, /* floating constant; nval = numerical float value */
|
||||||
VNONRELOC, /* info = result register */
|
VKINT, /* integer constant; nval = numerical integer value */
|
||||||
VLOCAL, /* info = local register */
|
VNONRELOC, /* expression has its value in a fixed register;
|
||||||
VUPVAL, /* info = index of upvalue in 'upvalues' */
|
info = result register */
|
||||||
VINDEXED, /* t = table register/upvalue; idx = index R/K */
|
VLOCAL, /* local variable; info = local register */
|
||||||
VJMP, /* info = instruction pc */
|
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
|
||||||
VRELOCABLE, /* info = instruction pc */
|
VINDEXED, /* indexed variable;
|
||||||
VCALL, /* info = instruction pc */
|
ind.vt = whether 't' is register or upvalue;
|
||||||
VVARARG /* info = instruction pc */
|
ind.t = table register or upvalue;
|
||||||
|
ind.idx = key's R/K index */
|
||||||
|
VJMP, /* expression is a test/comparison;
|
||||||
|
info = pc of corresponding jump instruction */
|
||||||
|
VRELOCABLE, /* expression can put result in any register;
|
||||||
|
info = instruction pc */
|
||||||
|
VCALL, /* expression is a function call; info = instruction pc */
|
||||||
|
VVARARG /* vararg expression; info = instruction pc */
|
||||||
} expkind;
|
} expkind;
|
||||||
|
|
||||||
|
|
||||||
@@ -41,17 +54,17 @@ typedef enum {
|
|||||||
typedef struct expdesc {
|
typedef struct expdesc {
|
||||||
expkind k;
|
expkind k;
|
||||||
union {
|
union {
|
||||||
|
lua_Integer ival; /* for VKINT */
|
||||||
|
lua_Number nval; /* for VKFLT */
|
||||||
|
int info; /* for generic use */
|
||||||
struct { /* for indexed variables (VINDEXED) */
|
struct { /* for indexed variables (VINDEXED) */
|
||||||
short idx; /* index (R/K) */
|
short idx; /* index (R/K) */
|
||||||
lu_byte t; /* table (register or upvalue) */
|
lu_byte t; /* table (register or upvalue) */
|
||||||
lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */
|
lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */
|
||||||
} ind;
|
} ind;
|
||||||
int info; /* for generic use */
|
|
||||||
lua_Number nval; /* for VKFLT */
|
|
||||||
lua_Integer ival; /* for VKINT */
|
|
||||||
} u;
|
} u;
|
||||||
int t; /* patch list of `exit when true' */
|
int t; /* patch list of 'exit when true' */
|
||||||
int f; /* patch list of `exit when false' */
|
int f; /* patch list of 'exit when false' */
|
||||||
} expdesc;
|
} expdesc;
|
||||||
|
|
||||||
|
|
||||||
@@ -100,11 +113,11 @@ typedef struct FuncState {
|
|||||||
struct FuncState *prev; /* enclosing function */
|
struct FuncState *prev; /* enclosing function */
|
||||||
struct LexState *ls; /* lexical state */
|
struct LexState *ls; /* lexical state */
|
||||||
struct BlockCnt *bl; /* chain of current blocks */
|
struct BlockCnt *bl; /* chain of current blocks */
|
||||||
int pc; /* next position to code (equivalent to `ncode') */
|
int pc; /* next position to code (equivalent to 'ncode') */
|
||||||
int lasttarget; /* 'label' of last 'jump label' */
|
int lasttarget; /* 'label' of last 'jump label' */
|
||||||
int jpc; /* list of pending jumps to `pc' */
|
int jpc; /* list of pending jumps to 'pc' */
|
||||||
int nk; /* number of elements in `k' */
|
int nk; /* number of elements in 'k' */
|
||||||
int np; /* number of elements in `p' */
|
int np; /* number of elements in 'p' */
|
||||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||||
short nlocvars; /* number of elements in 'f->locvars' */
|
short nlocvars; /* number of elements in 'f->locvars' */
|
||||||
lu_byte nactvar; /* number of active local variables */
|
lu_byte nactvar; /* number of active local variables */
|
||||||
|
|||||||
45
lprefix.h
Normal file
45
lprefix.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lprefix.h,v 1.1 2014/11/03 15:12:44 roberto Exp roberto $
|
||||||
|
** Definitions for Lua code that must come before any other header file
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lprefix_h
|
||||||
|
#define lprefix_h
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Allows POSIX/XSI stuff
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_USE_C89) /* { */
|
||||||
|
|
||||||
|
#if !defined(_XOPEN_SOURCE)
|
||||||
|
#define _XOPEN_SOURCE 600
|
||||||
|
#elif _XOPEN_SOURCE == 0
|
||||||
|
#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Allows manipulation of large files in gcc and some other compilers
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
|
||||||
|
#define _LARGEFILE_SOURCE 1
|
||||||
|
#define _FILE_OFFSET_BITS 64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Windows stuff
|
||||||
|
*/
|
||||||
|
#if defined(_WIN32) /* { */
|
||||||
|
|
||||||
|
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||||
|
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
50
lstate.c
50
lstate.c
@@ -1,16 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstate.c,v 2.124 2014/07/24 14:00:16 roberto Exp roberto $
|
** $Id: lstate.c,v 2.132 2015/11/02 16:01:41 roberto Exp roberto $
|
||||||
** Global State
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lstate_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lstate_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -35,9 +37,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define MEMERRMSG "not enough memory"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** a macro to help the creation of a unique random seed when a state is
|
** a macro to help the creation of a unique random seed when a state is
|
||||||
** created; the seed is used to randomize hashes.
|
** created; the seed is used to randomize hashes.
|
||||||
@@ -72,13 +71,12 @@ typedef struct LG {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Compute an initial seed as random as possible. In ANSI, rely on
|
** Compute an initial seed as random as possible. Rely on Address Space
|
||||||
** Address Space Layout Randomization (if present) to increase
|
** Layout Randomization (if present) to increase randomness..
|
||||||
** randomness..
|
|
||||||
*/
|
*/
|
||||||
#define addbuff(b,p,e) \
|
#define addbuff(b,p,e) \
|
||||||
{ size_t t = cast(size_t, e); \
|
{ size_t t = cast(size_t, e); \
|
||||||
memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
|
memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
|
||||||
|
|
||||||
static unsigned int makeseed (lua_State *L) {
|
static unsigned int makeseed (lua_State *L) {
|
||||||
char buff[4 * sizeof(size_t)];
|
char buff[4 * sizeof(size_t)];
|
||||||
@@ -95,10 +93,14 @@ static unsigned int makeseed (lua_State *L) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
|
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
|
||||||
** invariant
|
** invariant (and avoiding underflows in 'totalbytes')
|
||||||
*/
|
*/
|
||||||
void luaE_setdebt (global_State *g, l_mem debt) {
|
void luaE_setdebt (global_State *g, l_mem debt) {
|
||||||
g->totalbytes -= (debt - g->GCdebt);
|
l_mem tb = gettotalbytes(g);
|
||||||
|
lua_assert(tb > 0);
|
||||||
|
if (debt < tb - MAX_LMEM)
|
||||||
|
debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
|
||||||
|
g->totalbytes = tb - debt;
|
||||||
g->GCdebt = debt;
|
g->GCdebt = debt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +111,7 @@ CallInfo *luaE_extendCI (lua_State *L) {
|
|||||||
L->ci->next = ci;
|
L->ci->next = ci;
|
||||||
ci->previous = L->ci;
|
ci->previous = L->ci;
|
||||||
ci->next = NULL;
|
ci->next = NULL;
|
||||||
|
L->nci++;
|
||||||
return ci;
|
return ci;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,6 +126,7 @@ void luaE_freeCI (lua_State *L) {
|
|||||||
while ((ci = next) != NULL) {
|
while ((ci = next) != NULL) {
|
||||||
next = ci->next;
|
next = ci->next;
|
||||||
luaM_free(L, ci);
|
luaM_free(L, ci);
|
||||||
|
L->nci--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,13 +136,14 @@ void luaE_freeCI (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
void luaE_shrinkCI (lua_State *L) {
|
void luaE_shrinkCI (lua_State *L) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
while (ci->next != NULL) { /* while there is 'next' */
|
CallInfo *next2; /* next's next */
|
||||||
CallInfo *next2 = ci->next->next; /* next's next */
|
/* while there are two nexts */
|
||||||
if (next2 == NULL) break;
|
while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
|
||||||
luaM_free(L, ci->next); /* remove next */
|
luaM_free(L, ci->next); /* free next */
|
||||||
|
L->nci--;
|
||||||
ci->next = next2; /* remove 'next' from the list */
|
ci->next = next2; /* remove 'next' from the list */
|
||||||
next2->previous = ci;
|
next2->previous = ci;
|
||||||
ci = next2;
|
ci = next2; /* keep next's next */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,6 +173,7 @@ static void freestack (lua_State *L) {
|
|||||||
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);
|
luaE_freeCI(L);
|
||||||
|
lua_assert(L->nci == 0);
|
||||||
luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
|
luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,12 +205,9 @@ static void f_luaopen (lua_State *L, void *ud) {
|
|||||||
UNUSED(ud);
|
UNUSED(ud);
|
||||||
stack_init(L, L); /* init stack */
|
stack_init(L, L); /* init stack */
|
||||||
init_registry(L, g);
|
init_registry(L, g);
|
||||||
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
luaS_init(L);
|
||||||
luaT_init(L);
|
luaT_init(L);
|
||||||
luaX_init(L);
|
luaX_init(L);
|
||||||
/* pre-create memory-error message */
|
|
||||||
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
|
|
||||||
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
|
|
||||||
g->gcrunning = 1; /* allow gc */
|
g->gcrunning = 1; /* allow gc */
|
||||||
g->version = lua_version(NULL);
|
g->version = lua_version(NULL);
|
||||||
luai_userstateopen(L);
|
luai_userstateopen(L);
|
||||||
@@ -219,6 +222,7 @@ static void preinit_thread (lua_State *L, global_State *g) {
|
|||||||
G(L) = g;
|
G(L) = g;
|
||||||
L->stack = NULL;
|
L->stack = NULL;
|
||||||
L->ci = NULL;
|
L->ci = NULL;
|
||||||
|
L->nci = 0;
|
||||||
L->stacksize = 0;
|
L->stacksize = 0;
|
||||||
L->twups = L; /* thread has no upvalues */
|
L->twups = L; /* thread has no upvalues */
|
||||||
L->errorJmp = NULL;
|
L->errorJmp = NULL;
|
||||||
@@ -242,7 +246,6 @@ static void close_state (lua_State *L) {
|
|||||||
if (g->version) /* closing a fully built state? */
|
if (g->version) /* closing a fully built state? */
|
||||||
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);
|
||||||
luaZ_freebuffer(L, &g->buff);
|
|
||||||
freestack(L);
|
freestack(L);
|
||||||
lua_assert(gettotalbytes(g) == sizeof(LG));
|
lua_assert(gettotalbytes(g) == sizeof(LG));
|
||||||
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
||||||
@@ -311,7 +314,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
|||||||
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);
|
||||||
luaZ_initbuffer(L, &g->buff);
|
|
||||||
g->panic = NULL;
|
g->panic = NULL;
|
||||||
g->version = NULL;
|
g->version = NULL;
|
||||||
g->gcstate = GCSpause;
|
g->gcstate = GCSpause;
|
||||||
|
|||||||
43
lstate.h
43
lstate.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstate.h,v 2.116 2014/10/06 21:34:34 roberto Exp roberto $
|
** $Id: lstate.h,v 2.129 2015/12/14 11:54:49 roberto Exp roberto $
|
||||||
** Global State
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -21,10 +21,10 @@
|
|||||||
** belong to one (and only one) of these lists, using field 'next' of
|
** belong to one (and only one) of these lists, using field 'next' of
|
||||||
** the 'CommonHeader' for the link:
|
** the 'CommonHeader' for the link:
|
||||||
**
|
**
|
||||||
** allgc: all objects not marked for finalization;
|
** 'allgc': all objects not marked for finalization;
|
||||||
** finobj: all objects marked for finalization;
|
** 'finobj': all objects marked for finalization;
|
||||||
** tobefnz: all objects ready to be finalized;
|
** 'tobefnz': all objects ready to be finalized;
|
||||||
** fixedgc: all objects that are not to be collected (currently
|
** 'fixedgc': all objects that are not to be collected (currently
|
||||||
** only small strings, such as reserved words).
|
** only small strings, such as reserved words).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@@ -33,6 +33,15 @@
|
|||||||
struct lua_longjmp; /* defined in ldo.c */
|
struct lua_longjmp; /* defined in ldo.c */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Atomic type (relative to signals) to better ensure that 'lua_sethook'
|
||||||
|
** is thread safe
|
||||||
|
*/
|
||||||
|
#if !defined(l_signalT)
|
||||||
|
#include <signal.h>
|
||||||
|
#define l_signalT sig_atomic_t
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* extra stack space to handle TM calls and some other extras */
|
/* extra stack space to handle TM calls and some other extras */
|
||||||
#define EXTRA_STACK 5
|
#define EXTRA_STACK 5
|
||||||
@@ -89,11 +98,12 @@ typedef struct CallInfo {
|
|||||||
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
||||||
#define CIST_LUA (1<<1) /* call is running a Lua function */
|
#define CIST_LUA (1<<1) /* call is running a Lua function */
|
||||||
#define CIST_HOOKED (1<<2) /* call is running a debug hook */
|
#define CIST_HOOKED (1<<2) /* call is running a debug hook */
|
||||||
#define CIST_REENTRY (1<<3) /* call is running on same invocation of
|
#define CIST_FRESH (1<<3) /* call is running on a fresh invocation
|
||||||
luaV_execute of previous call */
|
of luaV_execute */
|
||||||
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
||||||
#define CIST_TAIL (1<<5) /* call was tail called */
|
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||||
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||||
|
#define CIST_LEQ (1<<7) /* using __lt for __le */
|
||||||
|
|
||||||
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
||||||
|
|
||||||
@@ -103,12 +113,12 @@ typedef struct CallInfo {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** `global state', shared by all threads of this state
|
** 'global state', shared by all threads of this state
|
||||||
*/
|
*/
|
||||||
typedef struct global_State {
|
typedef struct global_State {
|
||||||
lua_Alloc frealloc; /* function to reallocate memory */
|
lua_Alloc frealloc; /* function to reallocate memory */
|
||||||
void *ud; /* auxiliary data to `frealloc' */
|
void *ud; /* auxiliary data to 'frealloc' */
|
||||||
lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||||
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||||
lu_mem GCmemtrav; /* memory traversed by the GC */
|
lu_mem GCmemtrav; /* memory traversed by the GC */
|
||||||
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
||||||
@@ -130,24 +140,25 @@ typedef struct global_State {
|
|||||||
GCObject *tobefnz; /* list of userdata to be GC */
|
GCObject *tobefnz; /* list of userdata to be GC */
|
||||||
GCObject *fixedgc; /* list of objects not to be collected */
|
GCObject *fixedgc; /* list of objects not to be collected */
|
||||||
struct lua_State *twups; /* list of threads with open upvalues */
|
struct lua_State *twups; /* list of threads with open upvalues */
|
||||||
Mbuffer buff; /* temporary buffer for string concatenation */
|
|
||||||
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
|
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
|
||||||
int gcpause; /* size of pause between successive GCs */
|
int gcpause; /* size of pause between successive GCs */
|
||||||
int gcstepmul; /* GC `granularity' */
|
int gcstepmul; /* GC 'granularity' */
|
||||||
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;
|
||||||
const lua_Number *version; /* pointer to version number */
|
const lua_Number *version; /* pointer to version number */
|
||||||
TString *memerrmsg; /* memory-error message */
|
TString *memerrmsg; /* memory-error message */
|
||||||
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_NUMTAGS]; /* metatables for basic types */
|
||||||
|
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
||||||
} global_State;
|
} global_State;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** `per thread' state
|
** 'per thread' state
|
||||||
*/
|
*/
|
||||||
struct lua_State {
|
struct lua_State {
|
||||||
CommonHeader;
|
CommonHeader;
|
||||||
|
unsigned short nci; /* number of items in 'ci' list */
|
||||||
lu_byte status;
|
lu_byte status;
|
||||||
StkId top; /* first free slot in the stack */
|
StkId top; /* first free slot in the stack */
|
||||||
global_State *l_G;
|
global_State *l_G;
|
||||||
@@ -160,14 +171,14 @@ struct lua_State {
|
|||||||
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) */
|
||||||
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) */
|
||||||
int stacksize;
|
int stacksize;
|
||||||
int basehookcount;
|
int basehookcount;
|
||||||
int hookcount;
|
int hookcount;
|
||||||
unsigned short nny; /* number of non-yieldable calls in stack */
|
unsigned short nny; /* number of non-yieldable calls in stack */
|
||||||
unsigned short nCcalls; /* number of nested C calls */
|
unsigned short nCcalls; /* number of nested C calls */
|
||||||
lu_byte hookmask;
|
l_signalT hookmask;
|
||||||
lu_byte allowhook;
|
lu_byte allowhook;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -210,7 +221,7 @@ union GCUnion {
|
|||||||
|
|
||||||
|
|
||||||
/* actual number of total bytes allocated */
|
/* actual number of total bytes allocated */
|
||||||
#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)
|
#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
120
lstring.c
120
lstring.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstring.c,v 2.43 2014/07/18 14:46:47 roberto Exp roberto $
|
** $Id: lstring.c,v 2.55 2015/11/03 15:36:01 roberto Exp roberto $
|
||||||
** String table (keeps all strings handled by Lua)
|
** String table (keeps all strings handled by Lua)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lstring_c
|
#define lstring_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -20,6 +22,8 @@
|
|||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define MEMERRMSG "not enough memory"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
||||||
@@ -34,24 +38,33 @@
|
|||||||
** equality for long strings
|
** equality for long strings
|
||||||
*/
|
*/
|
||||||
int luaS_eqlngstr (TString *a, TString *b) {
|
int luaS_eqlngstr (TString *a, TString *b) {
|
||||||
size_t len = a->len;
|
size_t len = a->u.lnglen;
|
||||||
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
|
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
|
||||||
return (a == b) || /* same instance or... */
|
return (a == b) || /* same instance or... */
|
||||||
((len == b->len) && /* equal length and ... */
|
((len == b->u.lnglen) && /* equal length and ... */
|
||||||
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
(memcmp(getstr(a), getstr(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(unsigned int, l);
|
unsigned int h = seed ^ cast(unsigned int, l);
|
||||||
size_t l1;
|
|
||||||
size_t step = (l >> LUAI_HASHLIMIT) + 1;
|
size_t step = (l >> LUAI_HASHLIMIT) + 1;
|
||||||
for (l1 = l; l1 >= step; l1 -= step)
|
for (; l >= step; l -= step)
|
||||||
h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
|
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int luaS_hashlongstr (TString *ts) {
|
||||||
|
lua_assert(ts->tt == LUA_TLNGSTR);
|
||||||
|
if (ts->extra == 0) { /* no hash? */
|
||||||
|
ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
|
||||||
|
ts->extra = 1; /* now it has its hash */
|
||||||
|
}
|
||||||
|
return ts->hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** resizes the string table
|
** resizes the string table
|
||||||
*/
|
*/
|
||||||
@@ -67,9 +80,9 @@ void luaS_resize (lua_State *L, int newsize) {
|
|||||||
TString *p = tb->hash[i];
|
TString *p = tb->hash[i];
|
||||||
tb->hash[i] = NULL;
|
tb->hash[i] = NULL;
|
||||||
while (p) { /* for each node in the list */
|
while (p) { /* for each node in the list */
|
||||||
TString *hnext = p->hnext; /* save next */
|
TString *hnext = p->u.hnext; /* save next */
|
||||||
unsigned int h = lmod(p->hash, newsize); /* new position */
|
unsigned int h = lmod(p->hash, newsize); /* new position */
|
||||||
p->hnext = tb->hash[h]; /* chain it */
|
p->u.hnext = tb->hash[h]; /* chain it */
|
||||||
tb->hash[h] = p;
|
tb->hash[h] = p;
|
||||||
p = hnext;
|
p = hnext;
|
||||||
}
|
}
|
||||||
@@ -83,23 +96,57 @@ void luaS_resize (lua_State *L, int newsize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Clear API string cache. (Entries cannot be empty, so fill them with
|
||||||
|
** a non-collectable string.)
|
||||||
|
*/
|
||||||
|
void luaS_clearcache (global_State *g) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < STRCACHE_N; i++)
|
||||||
|
for (j = 0; j < STRCACHE_M; j++) {
|
||||||
|
if (iswhite(g->strcache[i][j])) /* will entry be collected? */
|
||||||
|
g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Initialize the string table and the string cache
|
||||||
|
*/
|
||||||
|
void luaS_init (lua_State *L) {
|
||||||
|
global_State *g = G(L);
|
||||||
|
int i, j;
|
||||||
|
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
||||||
|
/* pre-create memory-error message */
|
||||||
|
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
|
||||||
|
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
|
||||||
|
for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
|
||||||
|
for (j = 0; j < STRCACHE_M; j++)
|
||||||
|
g->strcache[i][j] = g->memerrmsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** creates a new string object
|
** creates a new string object
|
||||||
*/
|
*/
|
||||||
static TString *createstrobj (lua_State *L, const char *str, size_t l,
|
static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
|
||||||
int tag, unsigned int h) {
|
|
||||||
TString *ts;
|
TString *ts;
|
||||||
GCObject *o;
|
GCObject *o;
|
||||||
size_t totalsize; /* total size of TString object */
|
size_t totalsize; /* total size of TString object */
|
||||||
totalsize = sizelstring(l);
|
totalsize = sizelstring(l);
|
||||||
o = luaC_newobj(L, tag, totalsize);
|
o = luaC_newobj(L, tag, totalsize);
|
||||||
ts = gco2ts(o);
|
ts = gco2ts(o);
|
||||||
ts->len = l;
|
|
||||||
ts->hash = h;
|
ts->hash = h;
|
||||||
ts->extra = 0;
|
ts->extra = 0;
|
||||||
memcpy(getaddrstr(ts), str, l * sizeof(char));
|
getstr(ts)[l] = '\0'; /* ending 0 */
|
||||||
getaddrstr(ts)[l] = '\0'; /* ending 0 */
|
return ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
|
||||||
|
TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
|
||||||
|
ts->u.lnglen = l;
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,8 +155,8 @@ void luaS_remove (lua_State *L, TString *ts) {
|
|||||||
stringtable *tb = &G(L)->strt;
|
stringtable *tb = &G(L)->strt;
|
||||||
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
|
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
|
||||||
while (*p != ts) /* find previous element */
|
while (*p != ts) /* find previous element */
|
||||||
p = &(*p)->hnext;
|
p = &(*p)->u.hnext;
|
||||||
*p = (*p)->hnext; /* remove element from its list */
|
*p = (*p)->u.hnext; /* remove element from its list */
|
||||||
tb->nuse--;
|
tb->nuse--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,8 +169,9 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
|||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
unsigned int h = luaS_hash(str, l, g->seed);
|
unsigned int h = luaS_hash(str, l, g->seed);
|
||||||
TString **list = &g->strt.hash[lmod(h, g->strt.size)];
|
TString **list = &g->strt.hash[lmod(h, g->strt.size)];
|
||||||
for (ts = *list; ts != NULL; ts = ts->hnext) {
|
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
|
||||||
if (l == ts->len &&
|
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
||||||
|
if (l == ts->shrlen &&
|
||||||
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
(memcmp(str, getstr(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)? */
|
||||||
@@ -135,8 +183,10 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
|||||||
luaS_resize(L, g->strt.size * 2);
|
luaS_resize(L, g->strt.size * 2);
|
||||||
list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
|
list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
|
||||||
}
|
}
|
||||||
ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
|
ts = createstrobj(L, l, LUA_TSHRSTR, h);
|
||||||
ts->hnext = *list;
|
memcpy(getstr(ts), str, l * sizeof(char));
|
||||||
|
ts->shrlen = cast_byte(l);
|
||||||
|
ts->u.hnext = *list;
|
||||||
*list = ts;
|
*list = ts;
|
||||||
g->strt.nuse++;
|
g->strt.nuse++;
|
||||||
return ts;
|
return ts;
|
||||||
@@ -150,18 +200,36 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
|||||||
if (l <= LUAI_MAXSHORTLEN) /* short string? */
|
if (l <= LUAI_MAXSHORTLEN) /* short string? */
|
||||||
return internshrstr(L, str, l);
|
return internshrstr(L, str, l);
|
||||||
else {
|
else {
|
||||||
if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
|
TString *ts;
|
||||||
|
if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
|
||||||
luaM_toobig(L);
|
luaM_toobig(L);
|
||||||
return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
|
ts = luaS_createlngstrobj(L, l);
|
||||||
|
memcpy(getstr(ts), str, l * sizeof(char));
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** new zero-terminated string
|
** Create or reuse a zero-terminated string, first checking in the
|
||||||
|
** cache (using the string address as a key). The cache can contain
|
||||||
|
** only zero-terminated strings, so it is safe to use 'strcmp' to
|
||||||
|
** check hits.
|
||||||
*/
|
*/
|
||||||
TString *luaS_new (lua_State *L, const char *str) {
|
TString *luaS_new (lua_State *L, const char *str) {
|
||||||
return luaS_newlstr(L, str, strlen(str));
|
unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
|
||||||
|
int j;
|
||||||
|
TString **p = G(L)->strcache[i];
|
||||||
|
for (j = 0; j < STRCACHE_M; j++) {
|
||||||
|
if (strcmp(str, getstr(p[j])) == 0) /* hit? */
|
||||||
|
return p[j]; /* that is it */
|
||||||
|
}
|
||||||
|
/* normal route */
|
||||||
|
for (j = STRCACHE_M - 1; j > 0; j--)
|
||||||
|
p[j] = p[j - 1]; /* move out last element */
|
||||||
|
/* new element is first in the list */
|
||||||
|
p[0] = luaS_newlstr(L, str, strlen(str));
|
||||||
|
return p[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstring.h,v 1.54 2014/03/19 18:51:42 roberto Exp roberto $
|
** $Id: lstring.h,v 1.60 2015/09/08 15:41:05 roberto Exp roberto $
|
||||||
** String table (keep all strings handled by Lua)
|
** String table (keep all strings handled by Lua)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,7 +13,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
|
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
|
||||||
#define sizestring(s) sizelstring((s)->len)
|
|
||||||
|
|
||||||
#define sizeludata(l) (sizeof(union UUdata) + (l))
|
#define sizeludata(l) (sizeof(union UUdata) + (l))
|
||||||
#define sizeudata(u) sizeludata((u)->len)
|
#define sizeudata(u) sizeludata((u)->len)
|
||||||
@@ -35,12 +34,16 @@
|
|||||||
|
|
||||||
|
|
||||||
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);
|
||||||
|
LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
|
||||||
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
||||||
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
||||||
|
LUAI_FUNC void luaS_clearcache (global_State *g);
|
||||||
|
LUAI_FUNC void luaS_init (lua_State *L);
|
||||||
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
||||||
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
|
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
|
||||||
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||||
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
|
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
|
||||||
|
LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
526
lstrlib.c
526
lstrlib.c
@@ -1,20 +1,24 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstrlib.c,v 1.204 2014/10/17 16:28:21 roberto Exp roberto $
|
** $Id: lstrlib.c,v 1.250 2016/05/18 18:19:51 roberto Exp roberto $
|
||||||
** Standard library for string operations and pattern-matching
|
** Standard library for string operations and pattern-matching
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lstrlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <float.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <locale.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lstrlib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -23,17 +27,29 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** maximum number of captures that a pattern can do during
|
** maximum number of captures that a pattern can do during
|
||||||
** pattern-matching. This limit is arbitrary.
|
** pattern-matching. This limit is arbitrary, but must fit in
|
||||||
|
** an unsigned char.
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_MAXCAPTURES)
|
#if !defined(LUA_MAXCAPTURES)
|
||||||
#define LUA_MAXCAPTURES 32
|
#define LUA_MAXCAPTURES 32
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* macro to `unsign' a character */
|
/* macro to 'unsign' a character */
|
||||||
#define uchar(c) ((unsigned char)(c))
|
#define uchar(c) ((unsigned char)(c))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Some sizes are better limited to fit in 'int', but must also fit in
|
||||||
|
** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
|
||||||
|
*/
|
||||||
|
#define MAX_SIZET ((size_t)(~(size_t)0))
|
||||||
|
|
||||||
|
#define MAXSIZE \
|
||||||
|
(sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int str_len (lua_State *L) {
|
static int str_len (lua_State *L) {
|
||||||
size_t l;
|
size_t l;
|
||||||
@@ -59,7 +75,7 @@ static int str_sub (lua_State *L) {
|
|||||||
if (start < 1) start = 1;
|
if (start < 1) start = 1;
|
||||||
if (end > (lua_Integer)l) end = l;
|
if (end > (lua_Integer)l) end = l;
|
||||||
if (start <= end)
|
if (start <= end)
|
||||||
lua_pushlstring(L, s + start - 1, (size_t)(end - start + 1));
|
lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1);
|
||||||
else lua_pushliteral(L, "");
|
else lua_pushliteral(L, "");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -103,13 +119,6 @@ static int str_upper (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* reasonable limit to avoid arithmetic overflow and strings too big */
|
|
||||||
#if LUA_MAXINTEGER / 2 <= 0x10000000
|
|
||||||
#define MAXSIZE ((size_t)(LUA_MAXINTEGER / 2))
|
|
||||||
#else
|
|
||||||
#define MAXSIZE ((size_t)0x10000000)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int str_rep (lua_State *L) {
|
static int str_rep (lua_State *L) {
|
||||||
size_t l, lsep;
|
size_t l, lsep;
|
||||||
const char *s = luaL_checklstring(L, 1, &l);
|
const char *s = luaL_checklstring(L, 1, &l);
|
||||||
@@ -145,9 +154,9 @@ static int str_byte (lua_State *L) {
|
|||||||
if (posi < 1) posi = 1;
|
if (posi < 1) posi = 1;
|
||||||
if (pose > (lua_Integer)l) pose = l;
|
if (pose > (lua_Integer)l) pose = l;
|
||||||
if (posi > pose) return 0; /* empty interval; return no values */
|
if (posi > pose) return 0; /* empty interval; return no values */
|
||||||
n = (int)(pose - posi + 1);
|
if (pose - posi >= INT_MAX) /* arithmetic overflow? */
|
||||||
if (posi + n <= pose) /* arithmetic overflow? */
|
|
||||||
return luaL_error(L, "string slice too long");
|
return luaL_error(L, "string slice too long");
|
||||||
|
n = (int)(pose - posi) + 1;
|
||||||
luaL_checkstack(L, n, "string slice too long");
|
luaL_checkstack(L, n, "string slice too long");
|
||||||
for (i=0; i<n; i++)
|
for (i=0; i<n; i++)
|
||||||
lua_pushinteger(L, uchar(s[posi+i-1]));
|
lua_pushinteger(L, uchar(s[posi+i-1]));
|
||||||
@@ -203,12 +212,12 @@ static int str_dump (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
typedef struct MatchState {
|
typedef struct MatchState {
|
||||||
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
|
|
||||||
const char *src_init; /* init of source string */
|
const char *src_init; /* init of source string */
|
||||||
const char *src_end; /* end ('\0') of source string */
|
const char *src_end; /* end ('\0') of source string */
|
||||||
const char *p_end; /* end ('\0') of pattern */
|
const char *p_end; /* end ('\0') of pattern */
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
int level; /* total number of captures (finished or unfinished) */
|
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
|
||||||
|
unsigned char level; /* total number of captures (finished or unfinished) */
|
||||||
struct {
|
struct {
|
||||||
const char *init;
|
const char *init;
|
||||||
ptrdiff_t len;
|
ptrdiff_t len;
|
||||||
@@ -255,11 +264,11 @@ static const char *classend (MatchState *ms, const char *p) {
|
|||||||
}
|
}
|
||||||
case '[': {
|
case '[': {
|
||||||
if (*p == '^') p++;
|
if (*p == '^') p++;
|
||||||
do { /* look for a `]' */
|
do { /* look for a ']' */
|
||||||
if (p == ms->p_end)
|
if (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. '%]') */
|
||||||
} while (*p != ']');
|
} while (*p != ']');
|
||||||
return p+1;
|
return p+1;
|
||||||
}
|
}
|
||||||
@@ -294,7 +303,7 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
|
|||||||
int sig = 1;
|
int sig = 1;
|
||||||
if (*(p+1) == '^') {
|
if (*(p+1) == '^') {
|
||||||
sig = 0;
|
sig = 0;
|
||||||
p++; /* skip the `^' */
|
p++; /* skip the '^' */
|
||||||
}
|
}
|
||||||
while (++p < ec) {
|
while (++p < ec) {
|
||||||
if (*p == L_ESC) {
|
if (*p == L_ESC) {
|
||||||
@@ -431,7 +440,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case '$': {
|
case '$': {
|
||||||
if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */
|
if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */
|
||||||
goto dflt; /* no; go to default */
|
goto dflt; /* no; go to default */
|
||||||
s = (s == ms->src_end) ? s : NULL; /* check end of string */
|
s = (s == ms->src_end) ? s : NULL; /* check end of string */
|
||||||
break;
|
break;
|
||||||
@@ -495,7 +504,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
|||||||
}
|
}
|
||||||
case '+': /* 1 or more repetitions */
|
case '+': /* 1 or more repetitions */
|
||||||
s++; /* 1 match already done */
|
s++; /* 1 match already done */
|
||||||
/* go through */
|
/* FALLTHROUGH */
|
||||||
case '*': /* 0 or more repetitions */
|
case '*': /* 0 or more repetitions */
|
||||||
s = max_expand(ms, s, p, ep);
|
s = max_expand(ms, s, p, ep);
|
||||||
break;
|
break;
|
||||||
@@ -519,16 +528,16 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
|||||||
static const char *lmemfind (const char *s1, size_t l1,
|
static const char *lmemfind (const char *s1, size_t l1,
|
||||||
const char *s2, size_t l2) {
|
const char *s2, size_t l2) {
|
||||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||||
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
else if (l2 > l1) return NULL; /* avoids a negative 'l1' */
|
||||||
else {
|
else {
|
||||||
const char *init; /* to search for a `*s2' inside `s1' */
|
const char *init; /* to search for a '*s2' inside 's1' */
|
||||||
l2--; /* 1st char will be checked by `memchr' */
|
l2--; /* 1st char will be checked by 'memchr' */
|
||||||
l1 = l1-l2; /* `s2' cannot be found after that */
|
l1 = l1-l2; /* 's2' cannot be found after that */
|
||||||
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
||||||
init++; /* 1st char is already checked */
|
init++; /* 1st char is already checked */
|
||||||
if (memcmp(init, s2+1, l2) == 0)
|
if (memcmp(init, s2+1, l2) == 0)
|
||||||
return init-1;
|
return init-1;
|
||||||
else { /* correct `l1' and `s1' to try again */
|
else { /* correct 'l1' and 's1' to try again */
|
||||||
l1 -= init-s1;
|
l1 -= init-s1;
|
||||||
s1 = init;
|
s1 = init;
|
||||||
}
|
}
|
||||||
@@ -544,13 +553,13 @@ static void push_onecapture (MatchState *ms, int i, const char *s,
|
|||||||
if (i == 0) /* ms->level == 0, too */
|
if (i == 0) /* ms->level == 0, too */
|
||||||
lua_pushlstring(ms->L, s, e - s); /* add whole match */
|
lua_pushlstring(ms->L, s, e - s); /* add whole match */
|
||||||
else
|
else
|
||||||
luaL_error(ms->L, "invalid capture index");
|
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ptrdiff_t l = ms->capture[i].len;
|
ptrdiff_t l = ms->capture[i].len;
|
||||||
if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
|
if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
|
||||||
if (l == CAP_POSITION)
|
if (l == 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);
|
||||||
else
|
else
|
||||||
lua_pushlstring(ms->L, ms->capture[i].init, l);
|
lua_pushlstring(ms->L, ms->capture[i].init, l);
|
||||||
}
|
}
|
||||||
@@ -579,6 +588,22 @@ static int nospecials (const char *p, size_t l) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void prepstate (MatchState *ms, lua_State *L,
|
||||||
|
const char *s, size_t ls, const char *p, size_t lp) {
|
||||||
|
ms->L = L;
|
||||||
|
ms->matchdepth = MAXCCALLS;
|
||||||
|
ms->src_init = s;
|
||||||
|
ms->src_end = s + ls;
|
||||||
|
ms->p_end = p + lp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void reprepstate (MatchState *ms) {
|
||||||
|
ms->level = 0;
|
||||||
|
lua_assert(ms->matchdepth == MAXCCALLS);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int str_find_aux (lua_State *L, int find) {
|
static int str_find_aux (lua_State *L, int find) {
|
||||||
size_t ls, lp;
|
size_t ls, lp;
|
||||||
const char *s = luaL_checklstring(L, 1, &ls);
|
const char *s = luaL_checklstring(L, 1, &ls);
|
||||||
@@ -594,8 +619,8 @@ static int str_find_aux (lua_State *L, int find) {
|
|||||||
/* do a plain search */
|
/* do a plain search */
|
||||||
const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
|
const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
|
||||||
if (s2) {
|
if (s2) {
|
||||||
lua_pushinteger(L, s2 - s + 1);
|
lua_pushinteger(L, (s2 - s) + 1);
|
||||||
lua_pushinteger(L, s2 - s + lp);
|
lua_pushinteger(L, (s2 - s) + lp);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -606,18 +631,13 @@ static int str_find_aux (lua_State *L, int find) {
|
|||||||
if (anchor) {
|
if (anchor) {
|
||||||
p++; lp--; /* skip anchor character */
|
p++; lp--; /* skip anchor character */
|
||||||
}
|
}
|
||||||
ms.L = L;
|
prepstate(&ms, L, s, ls, p, lp);
|
||||||
ms.matchdepth = MAXCCALLS;
|
|
||||||
ms.src_init = s;
|
|
||||||
ms.src_end = s + ls;
|
|
||||||
ms.p_end = p + lp;
|
|
||||||
do {
|
do {
|
||||||
const char *res;
|
const char *res;
|
||||||
ms.level = 0;
|
reprepstate(&ms);
|
||||||
lua_assert(ms.matchdepth == MAXCCALLS);
|
|
||||||
if ((res=match(&ms, s1, p)) != NULL) {
|
if ((res=match(&ms, s1, p)) != NULL) {
|
||||||
if (find) {
|
if (find) {
|
||||||
lua_pushinteger(L, s1 - s + 1); /* start */
|
lua_pushinteger(L, (s1 - s) + 1); /* start */
|
||||||
lua_pushinteger(L, res - s); /* end */
|
lua_pushinteger(L, res - s); /* end */
|
||||||
return push_captures(&ms, NULL, 0) + 2;
|
return push_captures(&ms, NULL, 0) + 2;
|
||||||
}
|
}
|
||||||
@@ -641,29 +661,25 @@ static int str_match (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* state for 'gmatch' */
|
||||||
|
typedef struct GMatchState {
|
||||||
|
const char *src; /* current position */
|
||||||
|
const char *p; /* pattern */
|
||||||
|
const char *lastmatch; /* end of last match */
|
||||||
|
MatchState ms; /* match state */
|
||||||
|
} GMatchState;
|
||||||
|
|
||||||
|
|
||||||
static int gmatch_aux (lua_State *L) {
|
static int gmatch_aux (lua_State *L) {
|
||||||
MatchState ms;
|
GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3));
|
||||||
size_t ls, lp;
|
|
||||||
const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
|
|
||||||
const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
|
|
||||||
const char *src;
|
const char *src;
|
||||||
ms.L = L;
|
gm->ms.L = L;
|
||||||
ms.matchdepth = MAXCCALLS;
|
for (src = gm->src; src <= gm->ms.src_end; src++) {
|
||||||
ms.src_init = s;
|
|
||||||
ms.src_end = s+ls;
|
|
||||||
ms.p_end = p + lp;
|
|
||||||
for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
|
|
||||||
src <= ms.src_end;
|
|
||||||
src++) {
|
|
||||||
const char *e;
|
const char *e;
|
||||||
ms.level = 0;
|
reprepstate(&gm->ms);
|
||||||
lua_assert(ms.matchdepth == MAXCCALLS);
|
if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) {
|
||||||
if ((e = match(&ms, src, p)) != NULL) {
|
gm->src = gm->lastmatch = e;
|
||||||
lua_Integer newstart = e-s;
|
return push_captures(&gm->ms, src, e);
|
||||||
if (e == src) newstart++; /* empty match? go at least one position */
|
|
||||||
lua_pushinteger(L, newstart);
|
|
||||||
lua_replace(L, lua_upvalueindex(3));
|
|
||||||
return push_captures(&ms, src, e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0; /* not found */
|
return 0; /* not found */
|
||||||
@@ -671,10 +687,14 @@ static int gmatch_aux (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int gmatch (lua_State *L) {
|
static int gmatch (lua_State *L) {
|
||||||
luaL_checkstring(L, 1);
|
size_t ls, lp;
|
||||||
luaL_checkstring(L, 2);
|
const char *s = luaL_checklstring(L, 1, &ls);
|
||||||
lua_settop(L, 2);
|
const char *p = luaL_checklstring(L, 2, &lp);
|
||||||
lua_pushinteger(L, 0);
|
GMatchState *gm;
|
||||||
|
lua_settop(L, 2); /* keep them on closure to avoid being collected */
|
||||||
|
gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState));
|
||||||
|
prepstate(&gm->ms, L, s, ls, p, lp);
|
||||||
|
gm->src = s; gm->p = p; gm->lastmatch = NULL;
|
||||||
lua_pushcclosure(L, gmatch_aux, 3);
|
lua_pushcclosure(L, gmatch_aux, 3);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -741,12 +761,13 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
|
|||||||
|
|
||||||
static int str_gsub (lua_State *L) {
|
static int str_gsub (lua_State *L) {
|
||||||
size_t srcl, lp;
|
size_t srcl, lp;
|
||||||
const char *src = luaL_checklstring(L, 1, &srcl);
|
const char *src = luaL_checklstring(L, 1, &srcl); /* subject */
|
||||||
const char *p = luaL_checklstring(L, 2, &lp);
|
const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
|
||||||
int tr = lua_type(L, 3);
|
const char *lastmatch = NULL; /* end of last match */
|
||||||
lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1);
|
int tr = lua_type(L, 3); /* replacement type */
|
||||||
|
lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
|
||||||
int anchor = (*p == '^');
|
int anchor = (*p == '^');
|
||||||
lua_Integer n = 0;
|
lua_Integer n = 0; /* replacement count */
|
||||||
MatchState ms;
|
MatchState ms;
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
|
luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
|
||||||
@@ -756,25 +777,18 @@ static int str_gsub (lua_State *L) {
|
|||||||
if (anchor) {
|
if (anchor) {
|
||||||
p++; lp--; /* skip anchor character */
|
p++; lp--; /* skip anchor character */
|
||||||
}
|
}
|
||||||
ms.L = L;
|
prepstate(&ms, L, src, srcl, p, lp);
|
||||||
ms.matchdepth = MAXCCALLS;
|
|
||||||
ms.src_init = src;
|
|
||||||
ms.src_end = src+srcl;
|
|
||||||
ms.p_end = p + lp;
|
|
||||||
while (n < max_s) {
|
while (n < max_s) {
|
||||||
const char *e;
|
const char *e;
|
||||||
ms.level = 0;
|
reprepstate(&ms); /* (re)prepare state for new match */
|
||||||
lua_assert(ms.matchdepth == MAXCCALLS);
|
if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */
|
||||||
e = match(&ms, src, p);
|
|
||||||
if (e) {
|
|
||||||
n++;
|
n++;
|
||||||
add_value(&ms, &b, src, e, tr);
|
add_value(&ms, &b, src, e, tr); /* add replacement to buffer */
|
||||||
|
src = lastmatch = e;
|
||||||
}
|
}
|
||||||
if (e && e>src) /* non empty match? */
|
else if (src < ms.src_end) /* otherwise, skip one character */
|
||||||
src = e; /* skip it */
|
|
||||||
else if (src < ms.src_end)
|
|
||||||
luaL_addchar(&b, *src++);
|
luaL_addchar(&b, *src++);
|
||||||
else break;
|
else break; /* end of subject */
|
||||||
if (anchor) break;
|
if (anchor) break;
|
||||||
}
|
}
|
||||||
luaL_addlstring(&b, src, ms.src_end-src);
|
luaL_addlstring(&b, src, ms.src_end-src);
|
||||||
@@ -793,34 +807,116 @@ static int str_gsub (lua_State *L) {
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
#if !defined(lua_number2strx) /* { */
|
||||||
#define MAX_ITEM 512
|
|
||||||
|
/*
|
||||||
|
** Hexadecimal floating-point formatter
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Number of bits that goes into the first digit. It can be any value
|
||||||
|
** between 1 and 4; the following definition tries to align the number
|
||||||
|
** to nibble boundaries by making what is left after that first digit a
|
||||||
|
** multiple of 4.
|
||||||
|
*/
|
||||||
|
#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Add integer part of 'x' to buffer and return new 'x'
|
||||||
|
*/
|
||||||
|
static lua_Number adddigit (char *buff, int n, lua_Number x) {
|
||||||
|
lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
|
||||||
|
int d = (int)dd;
|
||||||
|
buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
|
||||||
|
return x - dd; /* return what is left */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int num2straux (char *buff, int sz, lua_Number x) {
|
||||||
|
if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */
|
||||||
|
return l_sprintf(buff, sz, LUA_NUMBER_FMT, x); /* equal to '%g' */
|
||||||
|
else if (x == 0) { /* can be -0... */
|
||||||
|
/* create "0" or "-0" followed by exponent */
|
||||||
|
return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", x);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int e;
|
||||||
|
lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */
|
||||||
|
int n = 0; /* character count */
|
||||||
|
if (m < 0) { /* is number negative? */
|
||||||
|
buff[n++] = '-'; /* add signal */
|
||||||
|
m = -m; /* make it positive */
|
||||||
|
}
|
||||||
|
buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */
|
||||||
|
m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */
|
||||||
|
e -= L_NBFD; /* this digit goes before the radix point */
|
||||||
|
if (m > 0) { /* more digits? */
|
||||||
|
buff[n++] = lua_getlocaledecpoint(); /* add radix point */
|
||||||
|
do { /* add as many digits as needed */
|
||||||
|
m = adddigit(buff, n++, m * 16);
|
||||||
|
} while (m > 0);
|
||||||
|
}
|
||||||
|
n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
|
||||||
|
lua_assert(n < sz);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int lua_number2strx (lua_State *L, char *buff, int sz,
|
||||||
|
const char *fmt, lua_Number x) {
|
||||||
|
int n = num2straux(buff, sz, x);
|
||||||
|
if (fmt[SIZELENMOD] == 'A') {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
buff[i] = toupper(uchar(buff[i]));
|
||||||
|
}
|
||||||
|
else if (fmt[SIZELENMOD] != 'a')
|
||||||
|
luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Maximum size of each formatted item. This maximum size is produced
|
||||||
|
** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.',
|
||||||
|
** and '\0') + number of decimal digits to represent maxfloat (which
|
||||||
|
** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra
|
||||||
|
** expenses", such as locale-dependent stuff)
|
||||||
|
*/
|
||||||
|
#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
|
||||||
|
|
||||||
|
|
||||||
/* valid flags in a format specification */
|
/* valid flags in a format specification */
|
||||||
#define FLAGS "-+ #0"
|
#define FLAGS "-+ #0"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** maximum size of each format specification (such as "%-099.99d")
|
** maximum size of each format specification (such as "%-099.99d")
|
||||||
** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error)
|
|
||||||
*/
|
*/
|
||||||
#define MAX_FORMAT (sizeof(FLAGS) + 2 + 10)
|
#define MAX_FORMAT 32
|
||||||
|
|
||||||
|
|
||||||
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
|
||||||
size_t l;
|
|
||||||
const char *s = luaL_checklstring(L, arg, &l);
|
|
||||||
luaL_addchar(b, '"');
|
luaL_addchar(b, '"');
|
||||||
while (l--) {
|
while (len--) {
|
||||||
if (*s == '"' || *s == '\\' || *s == '\n') {
|
if (*s == '"' || *s == '\\' || *s == '\n') {
|
||||||
luaL_addchar(b, '\\');
|
luaL_addchar(b, '\\');
|
||||||
luaL_addchar(b, *s);
|
luaL_addchar(b, *s);
|
||||||
}
|
}
|
||||||
else if (*s == '\0' || iscntrl(uchar(*s))) {
|
else if (iscntrl(uchar(*s))) {
|
||||||
char buff[10];
|
char buff[10];
|
||||||
if (!isdigit(uchar(*(s+1))))
|
if (!isdigit(uchar(*(s+1))))
|
||||||
sprintf(buff, "\\%d", (int)uchar(*s));
|
l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
|
||||||
else
|
else
|
||||||
sprintf(buff, "\\%03d", (int)uchar(*s));
|
l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
|
||||||
luaL_addstring(b, buff);
|
luaL_addstring(b, buff);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -830,6 +926,57 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
|||||||
luaL_addchar(b, '"');
|
luaL_addchar(b, '"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Ensures the 'buff' string uses a dot as the radix character.
|
||||||
|
*/
|
||||||
|
static void checkdp (char *buff, int nb) {
|
||||||
|
if (memchr(buff, '.', nb) == NULL) { /* no dot? */
|
||||||
|
char point = lua_getlocaledecpoint(); /* try locale point */
|
||||||
|
char *ppoint = memchr(buff, point, nb);
|
||||||
|
if (ppoint) *ppoint = '.'; /* change it to a dot */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
|
||||||
|
switch (lua_type(L, arg)) {
|
||||||
|
case LUA_TSTRING: {
|
||||||
|
size_t len;
|
||||||
|
const char *s = lua_tolstring(L, arg, &len);
|
||||||
|
addquoted(b, s, len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case LUA_TNUMBER: {
|
||||||
|
char *buff = luaL_prepbuffsize(b, MAX_ITEM);
|
||||||
|
int nb;
|
||||||
|
if (!lua_isinteger(L, arg)) { /* float? */
|
||||||
|
lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */
|
||||||
|
nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n);
|
||||||
|
checkdp(buff, nb); /* ensure it uses a dot */
|
||||||
|
}
|
||||||
|
else { /* integers */
|
||||||
|
lua_Integer n = lua_tointeger(L, arg);
|
||||||
|
const char *format = (n == LUA_MININTEGER) /* corner case? */
|
||||||
|
? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */
|
||||||
|
: LUA_INTEGER_FMT; /* else use default format */
|
||||||
|
nb = l_sprintf(buff, MAX_ITEM, format, n);
|
||||||
|
}
|
||||||
|
luaL_addsize(b, nb);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case LUA_TNIL: case LUA_TBOOLEAN: {
|
||||||
|
luaL_tolstring(L, arg, NULL);
|
||||||
|
luaL_addvalue(b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
luaL_argerror(L, arg, "value has no literal form");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
||||||
const char *p = strfrmt;
|
const char *p = strfrmt;
|
||||||
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
|
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
|
||||||
@@ -845,8 +992,8 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
|||||||
if (isdigit(uchar(*p)))
|
if (isdigit(uchar(*p)))
|
||||||
luaL_error(L, "invalid format (width or precision too long)");
|
luaL_error(L, "invalid format (width or precision too long)");
|
||||||
*(form++) = '%';
|
*(form++) = '%';
|
||||||
memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
|
memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char));
|
||||||
form += p - strfrmt + 1;
|
form += (p - strfrmt) + 1;
|
||||||
*form = '\0';
|
*form = '\0';
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
@@ -879,7 +1026,7 @@ static int str_format (lua_State *L) {
|
|||||||
else if (*++strfrmt == L_ESC)
|
else if (*++strfrmt == L_ESC)
|
||||||
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 ('%...') */
|
||||||
char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
|
char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
|
||||||
int nb = 0; /* number of bytes in added item */
|
int nb = 0; /* number of bytes in added item */
|
||||||
if (++arg > top)
|
if (++arg > top)
|
||||||
@@ -887,49 +1034,55 @@ static int str_format (lua_State *L) {
|
|||||||
strfrmt = scanformat(L, strfrmt, form);
|
strfrmt = scanformat(L, strfrmt, form);
|
||||||
switch (*strfrmt++) {
|
switch (*strfrmt++) {
|
||||||
case 'c': {
|
case 'c': {
|
||||||
nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg));
|
nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': case 'i':
|
case 'd': case 'i':
|
||||||
case 'o': case 'u': case 'x': case 'X': {
|
case 'o': case 'u': case 'x': case 'X': {
|
||||||
lua_Integer n = luaL_checkinteger(L, arg);
|
lua_Integer n = luaL_checkinteger(L, arg);
|
||||||
addlenmod(form, LUA_INTEGER_FRMLEN);
|
addlenmod(form, LUA_INTEGER_FRMLEN);
|
||||||
nb = sprintf(buff, form, n);
|
nb = l_sprintf(buff, MAX_ITEM, form, n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'e': case 'E': case 'f':
|
|
||||||
#if defined(LUA_USE_AFORMAT)
|
|
||||||
case 'a': case 'A':
|
case 'a': case 'A':
|
||||||
#endif
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
|
nb = lua_number2strx(L, buff, MAX_ITEM, form,
|
||||||
|
luaL_checknumber(L, arg));
|
||||||
|
break;
|
||||||
|
case 'e': case 'E': case 'f':
|
||||||
case 'g': case 'G': {
|
case 'g': case 'G': {
|
||||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
nb = sprintf(buff, form, luaL_checknumber(L, arg));
|
nb = l_sprintf(buff, MAX_ITEM, form, luaL_checknumber(L, arg));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'q': {
|
case 'q': {
|
||||||
addquoted(L, &b, arg);
|
addliteral(L, &b, arg);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 's': {
|
case 's': {
|
||||||
size_t l;
|
size_t l;
|
||||||
const char *s = luaL_tolstring(L, arg, &l);
|
const char *s = luaL_tolstring(L, arg, &l);
|
||||||
if (!strchr(form, '.') && l >= 100) {
|
if (form[2] == '\0') /* no modifiers? */
|
||||||
/* no precision and string is too long to be formatted;
|
luaL_addvalue(&b); /* keep entire string */
|
||||||
keep original string */
|
|
||||||
luaL_addvalue(&b);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
nb = sprintf(buff, form, s);
|
luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
|
||||||
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
|
if (!strchr(form, '.') && l >= 100) {
|
||||||
break;
|
/* no precision and string is too long to be formatted */
|
||||||
|
luaL_addvalue(&b); /* keep entire string */
|
||||||
|
}
|
||||||
|
else { /* format the string into 'buff' */
|
||||||
|
nb = l_sprintf(buff, MAX_ITEM, form, s);
|
||||||
|
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default: { /* also treat cases `pnLlh' */
|
default: { /* also treat cases 'pnLlh' */
|
||||||
return luaL_error(L, "invalid option '%%%c' to 'format'",
|
return luaL_error(L, "invalid option '%%%c' to 'format'",
|
||||||
*(strfrmt - 1));
|
*(strfrmt - 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
lua_assert(nb < MAX_ITEM);
|
||||||
luaL_addsize(&b, nb);
|
luaL_addsize(&b, nb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -947,6 +1100,11 @@ static int str_format (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/* value used for padding */
|
||||||
|
#if !defined(LUAL_PACKPADBYTE)
|
||||||
|
#define LUAL_PACKPADBYTE 0x00
|
||||||
|
#endif
|
||||||
|
|
||||||
/* maximum size for the binary representation of an integer */
|
/* maximum size for the binary representation of an integer */
|
||||||
#define MAXINTSIZE 16
|
#define MAXINTSIZE 16
|
||||||
|
|
||||||
@@ -959,9 +1117,6 @@ static int str_format (lua_State *L) {
|
|||||||
/* size of a lua_Integer */
|
/* size of a lua_Integer */
|
||||||
#define SZINT ((int)sizeof(lua_Integer))
|
#define SZINT ((int)sizeof(lua_Integer))
|
||||||
|
|
||||||
/* mask for all ones in last byte in a lua Integer */
|
|
||||||
#define HIGHERBYTE ((lua_Unsigned)MC << (NB * (SZINT - 1)))
|
|
||||||
|
|
||||||
|
|
||||||
/* dummy union to get native endianness */
|
/* dummy union to get native endianness */
|
||||||
static const union {
|
static const union {
|
||||||
@@ -1012,8 +1167,7 @@ typedef enum KOption {
|
|||||||
Kzstr, /* zero-terminated strings */
|
Kzstr, /* zero-terminated strings */
|
||||||
Kpadding, /* padding */
|
Kpadding, /* padding */
|
||||||
Kpaddalign, /* padding for alignment */
|
Kpaddalign, /* padding for alignment */
|
||||||
Knop, /* no-op (configuration or spaces) */
|
Knop /* no-op (configuration or spaces) */
|
||||||
Keof /* end of format */
|
|
||||||
} KOption;
|
} KOption;
|
||||||
|
|
||||||
|
|
||||||
@@ -1029,8 +1183,8 @@ static int getnum (const char **fmt, int df) {
|
|||||||
else {
|
else {
|
||||||
int a = 0;
|
int a = 0;
|
||||||
do {
|
do {
|
||||||
a = a*10 + *((*fmt)++) - '0';
|
a = a*10 + (*((*fmt)++) - '0');
|
||||||
} while (digit(**fmt) && a < (INT_MAX/10 - 10));
|
} while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10);
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1043,7 +1197,8 @@ 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 (sz > MAXINTSIZE || sz <= 0)
|
||||||
luaL_error(h->L, "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE);
|
luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
|
||||||
|
sz, MAXINTSIZE);
|
||||||
return sz;
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1080,20 +1235,22 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
|||||||
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': *size = getnum(fmt, 1); return Kchar;
|
case 'c':
|
||||||
|
*size = getnum(fmt, -1);
|
||||||
|
if (*size == -1)
|
||||||
|
luaL_error(h->L, "missing size for format option 'c'");
|
||||||
|
return Kchar;
|
||||||
case 'z': return Kzstr;
|
case 'z': return Kzstr;
|
||||||
case 'x': *size = 1; return Kpadding;
|
case 'x': *size = 1; return Kpadding;
|
||||||
case 'X': return Kpaddalign;
|
case 'X': return Kpaddalign;
|
||||||
case ' ': return Knop;
|
case ' ': break;
|
||||||
case '\0': return Keof;
|
case '<': h->islittle = 1; break;
|
||||||
case '<': h->islittle = 1; return Knop;
|
case '>': h->islittle = 0; break;
|
||||||
case '>': h->islittle = 0; return Knop;
|
case '=': h->islittle = nativeendian.little; break;
|
||||||
case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); return Knop;
|
case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break;
|
||||||
default: {
|
default: luaL_error(h->L, "invalid format option '%c'", opt);
|
||||||
luaL_error(h->L, "invalid format option '%c'", opt);
|
|
||||||
return Knop;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return Knop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1103,19 +1260,15 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
|||||||
** alignment requirements.
|
** alignment requirements.
|
||||||
** Local variable 'size' gets the size to be aligned. (Kpadal option
|
** Local variable 'size' gets the size to be aligned. (Kpadal option
|
||||||
** always gets its full alignment, other options are limited by
|
** always gets its full alignment, other options are limited by
|
||||||
** the maximum alignment ('maxalign). Kchar option needs no aligment
|
** the maximum alignment ('maxalign'). Kchar option needs no alignment
|
||||||
** despite its size.
|
** despite its size.
|
||||||
*/
|
*/
|
||||||
static KOption getdetails (Header *h, size_t totalsize,
|
static KOption getdetails (Header *h, size_t totalsize,
|
||||||
const char **fmt, int *psize, int *ntoalign) {
|
const char **fmt, int *psize, int *ntoalign) {
|
||||||
int align;
|
KOption opt = getoption(h, fmt, psize);
|
||||||
KOption opt;
|
int align = *psize; /* usually, alignment follows size */
|
||||||
do {
|
|
||||||
opt = getoption(h, fmt, psize);
|
|
||||||
} while (opt == Knop); /* skip no-op options */
|
|
||||||
align = *psize; /* usually, alignment follows size */
|
|
||||||
if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
|
if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
|
||||||
if (getoption(h, fmt, &align) == Kchar || align == 0)
|
if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
|
||||||
luaL_argerror(h->L, 1, "invalid next option for option 'X'");
|
luaL_argerror(h->L, 1, "invalid next option for option 'X'");
|
||||||
}
|
}
|
||||||
if (align <= 1 || opt == Kchar) /* need no alignment? */
|
if (align <= 1 || opt == Kchar) /* need no alignment? */
|
||||||
@@ -1131,15 +1284,25 @@ static KOption getdetails (Header *h, size_t totalsize,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
|
||||||
|
** The final 'if' handles the case when 'size' is larger than
|
||||||
|
** the size of a Lua integer, correcting the extra sign-extension
|
||||||
|
** bytes if necessary (by default they would be zeros).
|
||||||
|
*/
|
||||||
static void packint (luaL_Buffer *b, lua_Unsigned n,
|
static void packint (luaL_Buffer *b, lua_Unsigned n,
|
||||||
int islittle, int size, lua_Unsigned mask) {
|
int islittle, int size, int neg) {
|
||||||
char *buff = luaL_prepbuffsize(b, size);
|
char *buff = luaL_prepbuffsize(b, size);
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < size - 1; i++) {
|
buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
|
||||||
buff[islittle ? i : size - 1 - i] = (n & MC);
|
for (i = 1; i < size; i++) {
|
||||||
n = (n >> NB) | mask;
|
n >>= NB;
|
||||||
|
buff[islittle ? i : size - 1 - i] = (char)(n & MC);
|
||||||
|
}
|
||||||
|
if (neg && size > SZINT) { /* negative number need sign extension? */
|
||||||
|
for (i = SZINT; i < size; i++) /* correct extra bytes */
|
||||||
|
buff[islittle ? i : size - 1 - i] = (char)MC;
|
||||||
}
|
}
|
||||||
buff[islittle ? i : size - 1 - i] = (n & MC);
|
|
||||||
luaL_addsize(b, size); /* add result to buffer */
|
luaL_addsize(b, size); /* add result to buffer */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1171,21 +1334,21 @@ static int str_pack (lua_State *L) {
|
|||||||
initheader(L, &h);
|
initheader(L, &h);
|
||||||
lua_pushnil(L); /* mark to separate arguments from string buffer */
|
lua_pushnil(L); /* mark to separate arguments from string buffer */
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
for (;;) {
|
while (*fmt != '\0') {
|
||||||
int size, ntoalign;
|
int size, ntoalign;
|
||||||
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||||
totalsize += ntoalign + size;
|
totalsize += ntoalign + size;
|
||||||
while (ntoalign-- > 0) luaL_addchar(&b, '\0'); /* fill alignment */
|
while (ntoalign-- > 0)
|
||||||
|
luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */
|
||||||
arg++;
|
arg++;
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case Kint: { /* signed integers */
|
case Kint: { /* signed integers */
|
||||||
lua_Integer n = luaL_checkinteger(L, arg);
|
lua_Integer n = luaL_checkinteger(L, arg);
|
||||||
lua_Unsigned mask = (n < 0) ? HIGHERBYTE : 0; /* sign extension */
|
|
||||||
if (size < SZINT) { /* need overflow check? */
|
if (size < SZINT) { /* need overflow check? */
|
||||||
lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
|
lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
|
||||||
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
|
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
|
||||||
}
|
}
|
||||||
packint(&b, (lua_Unsigned)n, h.islittle, size, mask);
|
packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kuint: { /* unsigned integers */
|
case Kuint: { /* unsigned integers */
|
||||||
@@ -1211,8 +1374,11 @@ static int str_pack (lua_State *L) {
|
|||||||
case Kchar: { /* fixed-size string */
|
case Kchar: { /* fixed-size string */
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = luaL_checklstring(L, arg, &len);
|
const char *s = luaL_checklstring(L, arg, &len);
|
||||||
luaL_argcheck(L, len == (size_t)size, arg, "wrong length");
|
luaL_argcheck(L, len <= (size_t)size, arg,
|
||||||
luaL_addlstring(&b, s, size);
|
"string longer than given size");
|
||||||
|
luaL_addlstring(&b, s, len); /* add string */
|
||||||
|
while (len++ < (size_t)size) /* pad extra space */
|
||||||
|
luaL_addchar(&b, LUAL_PACKPADBYTE);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kstring: { /* strings with length count */
|
case Kstring: { /* strings with length count */
|
||||||
@@ -1235,18 +1401,50 @@ static int str_pack (lua_State *L) {
|
|||||||
totalsize += len + 1;
|
totalsize += len + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kpadding: luaL_addchar(&b, '\0'); /* go through */
|
case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */
|
||||||
case Kpaddalign: case Knop:
|
case Kpaddalign: case Knop:
|
||||||
arg--; /* undo increment */
|
arg--; /* undo increment */
|
||||||
break;
|
break;
|
||||||
case Keof: /* end of format */
|
|
||||||
luaL_pushresult(&b);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_packsize (lua_State *L) {
|
||||||
|
Header h;
|
||||||
|
const char *fmt = luaL_checkstring(L, 1); /* format string */
|
||||||
|
size_t totalsize = 0; /* accumulate total size of result */
|
||||||
|
initheader(L, &h);
|
||||||
|
while (*fmt != '\0') {
|
||||||
|
int size, ntoalign;
|
||||||
|
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||||
|
size += ntoalign; /* total space used by option */
|
||||||
|
luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
|
||||||
|
"format result too large");
|
||||||
|
totalsize += size;
|
||||||
|
switch (opt) {
|
||||||
|
case Kstring: /* strings with length count */
|
||||||
|
case Kzstr: /* zero-terminated string */
|
||||||
|
luaL_argerror(L, 1, "variable-length format");
|
||||||
|
/* call never return, but to avoid warnings: *//* FALLTHROUGH */
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushinteger(L, (lua_Integer)totalsize);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Unpack an integer with 'size' bytes and 'islittle' endianness.
|
||||||
|
** If size is smaller than the size of a Lua integer and integer
|
||||||
|
** is signed, must do sign extension (propagating the sign to the
|
||||||
|
** higher bits); if size is larger than the size of a Lua integer,
|
||||||
|
** it must check the unread bytes to see whether they do not cause an
|
||||||
|
** overflow.
|
||||||
|
*/
|
||||||
static lua_Integer unpackint (lua_State *L, const char *str,
|
static lua_Integer unpackint (lua_State *L, const char *str,
|
||||||
int islittle, int size, int issigned) {
|
int islittle, int size, int issigned) {
|
||||||
lua_Unsigned res = 0;
|
lua_Unsigned res = 0;
|
||||||
@@ -1262,11 +1460,11 @@ static lua_Integer unpackint (lua_State *L, const char *str,
|
|||||||
res = ((res ^ mask) - mask); /* do sign extension */
|
res = ((res ^ mask) - mask); /* do sign extension */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* 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 ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
|
||||||
luaL_error(L, "%d-bit integer does not fit into Lua Integer", size);
|
luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (lua_Integer)res;
|
return (lua_Integer)res;
|
||||||
@@ -1282,12 +1480,12 @@ static int str_unpack (lua_State *L) {
|
|||||||
int n = 0; /* number of results */
|
int n = 0; /* number of results */
|
||||||
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
|
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
|
||||||
initheader(L, &h);
|
initheader(L, &h);
|
||||||
for (;;) {
|
while (*fmt != '\0') {
|
||||||
int size, ntoalign;
|
int size, ntoalign;
|
||||||
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
||||||
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
|
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
|
||||||
luaL_argerror(L, 2, "data string too short");
|
luaL_argerror(L, 2, "data string too short");
|
||||||
pos += ntoalign;
|
pos += ntoalign; /* skip alignment */
|
||||||
/* stack space for item + next position */
|
/* stack space for item + next position */
|
||||||
luaL_checkstack(L, 2, "too many results");
|
luaL_checkstack(L, 2, "too many results");
|
||||||
n++;
|
n++;
|
||||||
@@ -1317,24 +1515,23 @@ static int str_unpack (lua_State *L) {
|
|||||||
size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
|
size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
|
||||||
luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
|
luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
|
||||||
lua_pushlstring(L, data + pos + size, len);
|
lua_pushlstring(L, data + pos + size, len);
|
||||||
pos += len;
|
pos += len; /* skip string */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kzstr: {
|
case Kzstr: {
|
||||||
size_t len = (int)strlen(data + pos);
|
size_t len = (int)strlen(data + pos);
|
||||||
lua_pushlstring(L, data + pos, len);
|
lua_pushlstring(L, data + pos, len);
|
||||||
pos += len + 1; /* skip final '\0' */
|
pos += len + 1; /* skip string plus final '\0' */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kpaddalign: case Kpadding: case Knop:
|
case Kpaddalign: case Kpadding: case Knop:
|
||||||
n--; /* undo increment */
|
n--; /* undo increment */
|
||||||
break;
|
break;
|
||||||
case Keof: /* end of format */
|
|
||||||
lua_pushinteger(L, pos + 1); /* next position */
|
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
pos += size;
|
pos += size;
|
||||||
}
|
}
|
||||||
|
lua_pushinteger(L, pos + 1); /* next position */
|
||||||
|
return n + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
@@ -1356,6 +1553,7 @@ static const luaL_Reg strlib[] = {
|
|||||||
{"sub", str_sub},
|
{"sub", str_sub},
|
||||||
{"upper", str_upper},
|
{"upper", str_upper},
|
||||||
{"pack", str_pack},
|
{"pack", str_pack},
|
||||||
|
{"packsize", str_packsize},
|
||||||
{"unpack", str_unpack},
|
{"unpack", str_unpack},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
233
ltable.c
233
ltable.c
@@ -1,31 +1,31 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltable.c,v 2.95 2014/09/04 18:15:29 roberto Exp roberto $
|
** $Id: ltable.c,v 2.116 2015/11/03 18:35:21 roberto Exp roberto $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ltable_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Implementation of tables (aka arrays, objects, or hash tables).
|
** Implementation of tables (aka arrays, objects, or hash tables).
|
||||||
** Tables keep its elements in two parts: an array part and a hash part.
|
** Tables keep its elements in two parts: an array part and a hash part.
|
||||||
** Non-negative integer keys are all candidates to be kept in the array
|
** Non-negative integer keys are all candidates to be kept in the array
|
||||||
** part. The actual size of the array is the largest `n' such that at
|
** part. The actual size of the array is the largest 'n' such that
|
||||||
** least half the slots between 0 and n are in use.
|
** more than half the slots between 1 and n are in use.
|
||||||
** Hash uses a mix of chained scatter table with Brent's variation.
|
** Hash uses a mix of chained scatter table with Brent's variation.
|
||||||
** A main invariant of these tables is that, if an element is not
|
** A main invariant of these tables is that, if an element is not
|
||||||
** in its main position (i.e. the `original' position that its hash gives
|
** in its main position (i.e. the 'original' position that its hash gives
|
||||||
** to it), then the colliding element is in its own main position.
|
** to it), then the colliding element is in its own main position.
|
||||||
** Hence even when the load factor reaches 100%, performance remains good.
|
** Hence even when the load factor reaches 100%, performance remains good.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <float.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
#define ltable_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -69,7 +69,7 @@
|
|||||||
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
|
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
|
||||||
|
|
||||||
|
|
||||||
#define hashpointer(t,p) hashmod(t, point2int(p))
|
#define hashpointer(t,p) hashmod(t, point2uint(p))
|
||||||
|
|
||||||
|
|
||||||
#define dummynode (&dummynode_)
|
#define dummynode (&dummynode_)
|
||||||
@@ -83,35 +83,37 @@ static const Node dummynode_ = {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Checks whether a float has a value representable as a lua_Integer
|
** Hash for floating-point numbers.
|
||||||
** (and does the conversion if so)
|
** The main computation should be just
|
||||||
|
** n = frexp(n, &i); return (n * INT_MAX) + i
|
||||||
|
** but there are some numerical subtleties.
|
||||||
|
** In a two-complement representation, INT_MAX does not has an exact
|
||||||
|
** representation as a float, but INT_MIN does; because the absolute
|
||||||
|
** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the
|
||||||
|
** absolute value of the product 'frexp * -INT_MIN' is smaller or equal
|
||||||
|
** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when
|
||||||
|
** adding 'i'; the use of '~u' (instead of '-u') avoids problems with
|
||||||
|
** INT_MIN.
|
||||||
*/
|
*/
|
||||||
static int numisinteger (lua_Number x, lua_Integer *p) {
|
#if !defined(l_hashfloat)
|
||||||
if ((x) == l_floor(x)) /* integral value? */
|
static int l_hashfloat (lua_Number n) {
|
||||||
return lua_numtointeger(x, p); /* try as an integer */
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** hash for floating-point numbers
|
|
||||||
*/
|
|
||||||
static Node *hashfloat (const Table *t, lua_Number n) {
|
|
||||||
int i;
|
int i;
|
||||||
n = l_mathop(frexp)(n, &i) * cast_num(INT_MAX - DBL_MAX_EXP);
|
lua_Integer ni;
|
||||||
i += cast_int(n);
|
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
|
||||||
if (i < 0) {
|
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
|
||||||
if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */
|
lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
|
||||||
i = 0; /* handle INT_MIN */
|
return 0;
|
||||||
i = -i; /* must be a positive value */
|
}
|
||||||
|
else { /* normal case */
|
||||||
|
unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni);
|
||||||
|
return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u);
|
||||||
}
|
}
|
||||||
return hashmod(t, i);
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns the `main' position of an element in a table (that is, the index
|
** returns the 'main' position of an element in a table (that is, the index
|
||||||
** of its hash value)
|
** of its hash value)
|
||||||
*/
|
*/
|
||||||
static Node *mainposition (const Table *t, const TValue *key) {
|
static Node *mainposition (const Table *t, const TValue *key) {
|
||||||
@@ -119,17 +121,11 @@ static Node *mainposition (const Table *t, const TValue *key) {
|
|||||||
case LUA_TNUMINT:
|
case LUA_TNUMINT:
|
||||||
return hashint(t, ivalue(key));
|
return hashint(t, ivalue(key));
|
||||||
case LUA_TNUMFLT:
|
case LUA_TNUMFLT:
|
||||||
return hashfloat(t, fltvalue(key));
|
return hashmod(t, l_hashfloat(fltvalue(key)));
|
||||||
case LUA_TSHRSTR:
|
case LUA_TSHRSTR:
|
||||||
return hashstr(t, tsvalue(key));
|
return hashstr(t, tsvalue(key));
|
||||||
case LUA_TLNGSTR: {
|
case LUA_TLNGSTR:
|
||||||
TString *s = tsvalue(key);
|
return hashpow2(t, luaS_hashlongstr(tsvalue(key)));
|
||||||
if (s->extra == 0) { /* no hash? */
|
|
||||||
s->hash = luaS_hash(getstr(s), s->len, s->hash);
|
|
||||||
s->extra = 1; /* now it has its hash */
|
|
||||||
}
|
|
||||||
return hashstr(t, tsvalue(key));
|
|
||||||
}
|
|
||||||
case LUA_TBOOLEAN:
|
case LUA_TBOOLEAN:
|
||||||
return hashboolean(t, bvalue(key));
|
return hashboolean(t, bvalue(key));
|
||||||
case LUA_TLIGHTUSERDATA:
|
case LUA_TLIGHTUSERDATA:
|
||||||
@@ -137,13 +133,14 @@ static Node *mainposition (const Table *t, const TValue *key) {
|
|||||||
case LUA_TLCF:
|
case LUA_TLCF:
|
||||||
return hashpointer(t, fvalue(key));
|
return hashpointer(t, fvalue(key));
|
||||||
default:
|
default:
|
||||||
|
lua_assert(!ttisdeadkey(key));
|
||||||
return hashpointer(t, gcvalue(key));
|
return hashpointer(t, gcvalue(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns the index for `key' if `key' is an appropriate key to live in
|
** returns the index for 'key' if 'key' is an appropriate key to live in
|
||||||
** the array part of the table, 0 otherwise.
|
** the array part of the table, 0 otherwise.
|
||||||
*/
|
*/
|
||||||
static unsigned int arrayindex (const TValue *key) {
|
static unsigned int arrayindex (const TValue *key) {
|
||||||
@@ -152,12 +149,12 @@ static unsigned int arrayindex (const TValue *key) {
|
|||||||
if (0 < k && (lua_Unsigned)k <= MAXASIZE)
|
if (0 < k && (lua_Unsigned)k <= MAXASIZE)
|
||||||
return cast(unsigned int, k); /* 'key' is an appropriate array index */
|
return cast(unsigned int, k); /* 'key' is an appropriate array index */
|
||||||
}
|
}
|
||||||
return 0; /* `key' did not match some condition */
|
return 0; /* 'key' did not match some condition */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns the index of a `key' for table traversals. First goes all
|
** returns the index of a 'key' for table traversals. First goes all
|
||||||
** elements in the array part, then elements in the hash part. The
|
** elements in the array part, then elements in the hash part. The
|
||||||
** beginning of a traversal is signaled by 0.
|
** beginning of a traversal is signaled by 0.
|
||||||
*/
|
*/
|
||||||
@@ -165,13 +162,13 @@ static unsigned int findindex (lua_State *L, Table *t, StkId key) {
|
|||||||
unsigned int i;
|
unsigned int i;
|
||||||
if (ttisnil(key)) return 0; /* first iteration */
|
if (ttisnil(key)) return 0; /* first iteration */
|
||||||
i = arrayindex(key);
|
i = arrayindex(key);
|
||||||
if (i != 0 && i <= t->sizearray) /* is `key' inside array part? */
|
if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */
|
||||||
return i; /* yes; that's the index */
|
return i; /* yes; that's the index */
|
||||||
else {
|
else {
|
||||||
int nx;
|
int nx;
|
||||||
Node *n = mainposition(t, key);
|
Node *n = mainposition(t, key);
|
||||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
/* key may be dead already, but it is ok to use it in `next' */
|
/* key may be dead already, but it is ok to use it in 'next' */
|
||||||
if (luaV_rawequalobj(gkey(n), key) ||
|
if (luaV_rawequalobj(gkey(n), key) ||
|
||||||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
||||||
deadvalue(gkey(n)) == gcvalue(key))) {
|
deadvalue(gkey(n)) == gcvalue(key))) {
|
||||||
@@ -217,34 +214,35 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
|
|||||||
/*
|
/*
|
||||||
** Compute the optimal size for the array part of table 't'. 'nums' is a
|
** Compute the optimal size for the array part of table 't'. 'nums' is a
|
||||||
** "count array" where 'nums[i]' is the number of integers in the table
|
** "count array" where 'nums[i]' is the number of integers in the table
|
||||||
** between 2^(i - 1) + 1 and 2^i. Put in '*narray' the optimal size, and
|
** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of
|
||||||
** return the number of elements that will go to that part.
|
** integer keys in the table and leaves with the number of keys that
|
||||||
|
** will go to the array part; return the optimal size.
|
||||||
*/
|
*/
|
||||||
static unsigned int computesizes (unsigned int nums[], unsigned int *narray) {
|
static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
|
||||||
int i;
|
int i;
|
||||||
unsigned int twotoi; /* 2^i */
|
unsigned int twotoi; /* 2^i (candidate for optimal size) */
|
||||||
unsigned int a = 0; /* number of elements smaller than 2^i */
|
unsigned int a = 0; /* number of elements smaller than 2^i */
|
||||||
unsigned int na = 0; /* number of elements to go to array part */
|
unsigned int na = 0; /* number of elements to go to array part */
|
||||||
unsigned int n = 0; /* optimal size for array part */
|
unsigned int optimal = 0; /* optimal size for array part */
|
||||||
for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
|
/* loop while keys can fill more than half of total size */
|
||||||
|
for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) {
|
||||||
if (nums[i] > 0) {
|
if (nums[i] > 0) {
|
||||||
a += nums[i];
|
a += nums[i];
|
||||||
if (a > twotoi/2) { /* more than half elements present? */
|
if (a > twotoi/2) { /* more than half elements present? */
|
||||||
n = twotoi; /* optimal size (till now) */
|
optimal = twotoi; /* optimal size (till now) */
|
||||||
na = a; /* all elements up to 'n' will go to array part */
|
na = a; /* all elements up to 'optimal' will go to array part */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (a == *narray) break; /* all elements already counted */
|
|
||||||
}
|
}
|
||||||
*narray = n;
|
lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
|
||||||
lua_assert(*narray/2 <= na && na <= *narray);
|
*pna = na;
|
||||||
return na;
|
return optimal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int countint (const TValue *key, unsigned int *nums) {
|
static int countint (const TValue *key, unsigned int *nums) {
|
||||||
unsigned int k = arrayindex(key);
|
unsigned int k = arrayindex(key);
|
||||||
if (k != 0) { /* is `key' an appropriate array index? */
|
if (k != 0) { /* is 'key' an appropriate array index? */
|
||||||
nums[luaO_ceillog2(k)]++; /* count as such */
|
nums[luaO_ceillog2(k)]++; /* count as such */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -253,10 +251,15 @@ static int countint (const TValue *key, unsigned int *nums) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Count keys in array part of table 't': Fill 'nums[i]' with
|
||||||
|
** number of keys that will go into corresponding slice and return
|
||||||
|
** total number of non-nil keys.
|
||||||
|
*/
|
||||||
static unsigned int numusearray (const Table *t, unsigned int *nums) {
|
static unsigned int numusearray (const Table *t, unsigned int *nums) {
|
||||||
int lg;
|
int lg;
|
||||||
unsigned int ttlg; /* 2^lg */
|
unsigned int ttlg; /* 2^lg */
|
||||||
unsigned int ause = 0; /* summation of `nums' */
|
unsigned int ause = 0; /* summation of 'nums' */
|
||||||
unsigned int i = 1; /* count to traverse all array keys */
|
unsigned int i = 1; /* count to traverse all array keys */
|
||||||
/* traverse each slice */
|
/* traverse each slice */
|
||||||
for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
|
for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
|
||||||
@@ -279,8 +282,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int numusehash (const Table *t, unsigned int *nums,
|
static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
|
||||||
unsigned int *pnasize) {
|
|
||||||
int totaluse = 0; /* total number of elements */
|
int totaluse = 0; /* total number of elements */
|
||||||
int ause = 0; /* elements added to 'nums' (can go to array part) */
|
int ause = 0; /* elements added to 'nums' (can go to array part) */
|
||||||
int i = sizenode(t);
|
int i = sizenode(t);
|
||||||
@@ -291,7 +293,7 @@ static int numusehash (const Table *t, unsigned int *nums,
|
|||||||
totaluse++;
|
totaluse++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*pnasize += ause;
|
*pna += ause;
|
||||||
return totaluse;
|
return totaluse;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,7 +310,7 @@ static void setarrayvector (lua_State *L, Table *t, unsigned int size) {
|
|||||||
static void setnodevector (lua_State *L, Table *t, unsigned int size) {
|
static void setnodevector (lua_State *L, Table *t, unsigned int size) {
|
||||||
int lsize;
|
int lsize;
|
||||||
if (size == 0) { /* no elements to hash part? */
|
if (size == 0) { /* no elements to hash part? */
|
||||||
t->node = cast(Node *, dummynode); /* use common `dummynode' */
|
t->node = cast(Node *, dummynode); /* use common 'dummynode' */
|
||||||
lsize = 0;
|
lsize = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -361,7 +363,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isdummy(nold))
|
if (!isdummy(nold))
|
||||||
luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */
|
luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old hash */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -374,21 +376,22 @@ void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
|
|||||||
** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
|
** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
|
||||||
*/
|
*/
|
||||||
static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
||||||
unsigned int nasize, na;
|
unsigned int asize; /* optimal size for array part */
|
||||||
|
unsigned int na; /* number of keys in the array part */
|
||||||
unsigned int nums[MAXABITS + 1];
|
unsigned int nums[MAXABITS + 1];
|
||||||
int i;
|
int i;
|
||||||
int totaluse;
|
int totaluse;
|
||||||
for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
|
for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
|
||||||
nasize = numusearray(t, nums); /* count keys in array part */
|
na = numusearray(t, nums); /* count keys in array part */
|
||||||
totaluse = nasize; /* all those keys are integer keys */
|
totaluse = na; /* all those keys are integer keys */
|
||||||
totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
|
totaluse += numusehash(t, nums, &na); /* count keys in hash part */
|
||||||
/* count extra key */
|
/* count extra key */
|
||||||
nasize += countint(ek, nums);
|
na += countint(ek, nums);
|
||||||
totaluse++;
|
totaluse++;
|
||||||
/* compute new size for array part */
|
/* compute new size for array part */
|
||||||
na = computesizes(nums, &nasize);
|
asize = computesizes(nums, &na);
|
||||||
/* resize the table to new computed sizes */
|
/* resize the table to new computed sizes */
|
||||||
luaH_resize(L, t, nasize, totaluse - na);
|
luaH_resize(L, t, asize, totaluse - na);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -441,14 +444,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
TValue aux;
|
TValue aux;
|
||||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||||
else if (ttisfloat(key)) {
|
else if (ttisfloat(key)) {
|
||||||
lua_Number n = fltvalue(key);
|
|
||||||
lua_Integer k;
|
lua_Integer k;
|
||||||
if (luai_numisnan(n))
|
if (luaV_tointeger(key, &k, 0)) { /* index is int? */
|
||||||
luaG_runerror(L, "table index is NaN");
|
|
||||||
if (numisinteger(n, &k)) { /* index is int? */
|
|
||||||
setivalue(&aux, k);
|
setivalue(&aux, k);
|
||||||
key = &aux; /* insert it as an integer */
|
key = &aux; /* insert it as an integer */
|
||||||
}
|
}
|
||||||
|
else if (luai_numisnan(fltvalue(key)))
|
||||||
|
luaG_runerror(L, "table index is NaN");
|
||||||
}
|
}
|
||||||
mp = mainposition(t, key);
|
mp = mainposition(t, key);
|
||||||
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
|
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
|
||||||
@@ -456,7 +458,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
Node *f = getfreepos(t); /* get a free place */
|
Node *f = getfreepos(t); /* get a free place */
|
||||||
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 and GC barrier */
|
/* whatever called 'newkey' takes care of TM cache */
|
||||||
return luaH_set(L, t, key); /* insert key into grown table */
|
return luaH_set(L, t, key); /* insert key into grown table */
|
||||||
}
|
}
|
||||||
lua_assert(!isdummy(f));
|
lua_assert(!isdummy(f));
|
||||||
@@ -482,7 +484,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
mp = f;
|
mp = f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setkey(L, &mp->i_key, key);
|
setnodekey(L, &mp->i_key, key);
|
||||||
luaC_barrierback(L, t, key);
|
luaC_barrierback(L, t, key);
|
||||||
lua_assert(ttisnil(gval(mp)));
|
lua_assert(ttisnil(gval(mp)));
|
||||||
return gval(mp);
|
return gval(mp);
|
||||||
@@ -494,11 +496,11 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
*/
|
*/
|
||||||
const TValue *luaH_getint (Table *t, lua_Integer key) {
|
const TValue *luaH_getint (Table *t, lua_Integer key) {
|
||||||
/* (1 <= key && key <= t->sizearray) */
|
/* (1 <= key && key <= t->sizearray) */
|
||||||
if (l_castS2U(key - 1) < t->sizearray)
|
if (l_castS2U(key) - 1 < t->sizearray)
|
||||||
return &t->array[key - 1];
|
return &t->array[key - 1];
|
||||||
else {
|
else {
|
||||||
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 (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key)
|
if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key)
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else {
|
else {
|
||||||
@@ -506,7 +508,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
|
|||||||
if (nx == 0) break;
|
if (nx == 0) break;
|
||||||
n += nx;
|
n += nx;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
return luaO_nilobject;
|
return luaO_nilobject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -515,20 +517,50 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
|
|||||||
/*
|
/*
|
||||||
** search function for short strings
|
** search function for short strings
|
||||||
*/
|
*/
|
||||||
const TValue *luaH_getstr (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_TSHRSTR);
|
||||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
const TValue *k = gkey(n);
|
const TValue *k = gkey(n);
|
||||||
if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
|
if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else {
|
else {
|
||||||
int nx = gnext(n);
|
int nx = gnext(n);
|
||||||
if (nx == 0) break;
|
if (nx == 0)
|
||||||
|
return luaO_nilobject; /* not found */
|
||||||
n += nx;
|
n += nx;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
return luaO_nilobject;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** "Generic" get version. (Not that generic: not valid for integers,
|
||||||
|
** which may be in array part, nor for floats with integral values.)
|
||||||
|
*/
|
||||||
|
static const TValue *getgeneric (Table *t, const TValue *key) {
|
||||||
|
Node *n = mainposition(t, key);
|
||||||
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
|
if (luaV_rawequalobj(gkey(n), key))
|
||||||
|
return gval(n); /* that's it */
|
||||||
|
else {
|
||||||
|
int nx = gnext(n);
|
||||||
|
if (nx == 0)
|
||||||
|
return luaO_nilobject; /* not found */
|
||||||
|
n += nx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const TValue *luaH_getstr (Table *t, TString *key) {
|
||||||
|
if (key->tt == LUA_TSHRSTR)
|
||||||
|
return luaH_getshortstr(t, key);
|
||||||
|
else { /* for long strings, use generic case */
|
||||||
|
TValue ko;
|
||||||
|
setsvalue(cast(lua_State *, NULL), &ko, key);
|
||||||
|
return getgeneric(t, &ko);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -537,28 +569,17 @@ 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 (ttype(key)) {
|
switch (ttype(key)) {
|
||||||
case LUA_TSHRSTR: return luaH_getstr(t, tsvalue(key));
|
case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
|
||||||
case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
|
case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
|
||||||
case LUA_TNIL: return luaO_nilobject;
|
case LUA_TNIL: return luaO_nilobject;
|
||||||
case LUA_TNUMFLT: {
|
case LUA_TNUMFLT: {
|
||||||
lua_Integer k;
|
lua_Integer k;
|
||||||
if (numisinteger(fltvalue(key), &k)) /* index is int? */
|
if (luaV_tointeger(key, &k, 0)) /* index is int? */
|
||||||
return luaH_getint(t, k); /* use specialized version */
|
return luaH_getint(t, k); /* use specialized version */
|
||||||
/* else go through */
|
/* else... */
|
||||||
}
|
} /* FALLTHROUGH */
|
||||||
default: {
|
default:
|
||||||
Node *n = mainposition(t, key);
|
return getgeneric(t, key);
|
||||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
|
||||||
if (luaV_rawequalobj(gkey(n), key))
|
|
||||||
return gval(n); /* that's it */
|
|
||||||
else {
|
|
||||||
int nx = gnext(n);
|
|
||||||
if (nx == 0) break;
|
|
||||||
n += nx;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return luaO_nilobject;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -592,7 +613,7 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
|
|||||||
static int unbound_search (Table *t, unsigned int j) {
|
static int unbound_search (Table *t, unsigned int j) {
|
||||||
unsigned int i = j; /* i is zero or a present index */
|
unsigned int i = j; /* i is zero or a present index */
|
||||||
j++;
|
j++;
|
||||||
/* find `i' and `j' such that i is present and j is not */
|
/* find 'i' and 'j' such that i is present and j is not */
|
||||||
while (!ttisnil(luaH_getint(t, j))) {
|
while (!ttisnil(luaH_getint(t, j))) {
|
||||||
i = j;
|
i = j;
|
||||||
if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */
|
if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */
|
||||||
@@ -614,7 +635,7 @@ static int unbound_search (Table *t, unsigned int j) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Try to find a boundary in table `t'. A `boundary' is an integer index
|
** Try to find a boundary in table 't'. A 'boundary' is an integer index
|
||||||
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
|
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
|
||||||
*/
|
*/
|
||||||
int luaH_getn (Table *t) {
|
int luaH_getn (Table *t) {
|
||||||
|
|||||||
7
ltable.h
7
ltable.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltable.h,v 2.19 2014/07/29 16:22:24 roberto Exp roberto $
|
** $Id: ltable.h,v 2.20 2014/09/04 18:15:29 roberto Exp roberto $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -18,6 +18,10 @@
|
|||||||
/* 'const' to avoid wrong writings that can mess up field 'next' */
|
/* 'const' to avoid wrong writings that can mess up field 'next' */
|
||||||
#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))
|
#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** writable version of 'gkey'; allows updates to individual fields,
|
||||||
|
** but not to the whole (which has incompatible type)
|
||||||
|
*/
|
||||||
#define wgkey(n) (&(n)->i_key.nk)
|
#define wgkey(n) (&(n)->i_key.nk)
|
||||||
|
|
||||||
#define invalidateTMcache(t) ((t)->flags = 0)
|
#define invalidateTMcache(t) ((t)->flags = 0)
|
||||||
@@ -31,6 +35,7 @@
|
|||||||
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
|
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
|
||||||
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
|
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
|
||||||
TValue *value);
|
TValue *value);
|
||||||
|
LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
|
||||||
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
LUAI_FUNC const TValue *luaH_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 TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key);
|
||||||
|
|||||||
395
ltablib.c
395
ltablib.c
@@ -1,15 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltablib.c,v 1.76 2014/09/22 06:42:15 roberto Exp roberto $
|
** $Id: ltablib.c,v 1.92 2016/02/08 12:55:19 roberto Exp roberto $
|
||||||
** Library for Table Manipulation
|
** Library for Table Manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ltablib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
#define ltablib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
@@ -17,40 +20,42 @@
|
|||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Structure with table-access functions
|
** Operations that an object must define to mimic a table
|
||||||
|
** (some functions only need some of them)
|
||||||
*/
|
*/
|
||||||
typedef struct {
|
#define TAB_R 1 /* read */
|
||||||
int (*geti) (lua_State *L, int idx, lua_Integer n);
|
#define TAB_W 2 /* write */
|
||||||
void (*seti) (lua_State *L, int idx, lua_Integer n);
|
#define TAB_L 4 /* length */
|
||||||
} TabA;
|
#define TAB_RW (TAB_R | TAB_W) /* read/write */
|
||||||
|
|
||||||
|
|
||||||
/*
|
#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
|
||||||
** Check that 'arg' has a table and set access functions in 'ta' to raw
|
|
||||||
** or non-raw according to the presence of corresponding metamethods.
|
|
||||||
*/
|
static int checkfield (lua_State *L, const char *key, int n) {
|
||||||
static void checktab (lua_State *L, int arg, TabA *ta) {
|
lua_pushstring(L, key);
|
||||||
ta->geti = NULL; ta->seti = NULL;
|
return (lua_rawget(L, -n) != LUA_TNIL);
|
||||||
if (lua_getmetatable(L, arg)) {
|
|
||||||
lua_pushliteral(L, "__index"); /* 'index' metamethod */
|
|
||||||
if (lua_rawget(L, -2) != LUA_TNIL)
|
|
||||||
ta->geti = lua_geti;
|
|
||||||
lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */
|
|
||||||
if (lua_rawget(L, -3) != LUA_TNIL)
|
|
||||||
ta->seti = lua_seti;
|
|
||||||
lua_pop(L, 3); /* pop metatable plus both metamethods */
|
|
||||||
}
|
|
||||||
if (ta->geti == NULL || ta->seti == NULL) {
|
|
||||||
luaL_checktype(L, arg, LUA_TTABLE); /* must be table for raw methods */
|
|
||||||
if (ta->geti == NULL) ta->geti = lua_rawgeti;
|
|
||||||
if (ta->seti == NULL) ta->seti = lua_rawseti;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define aux_getn(L,n,ta) (checktab(L, n, ta), luaL_len(L, n))
|
/*
|
||||||
|
** Check that 'arg' either is a table or can behave like one (that is,
|
||||||
|
** has a metatable with the required metamethods)
|
||||||
|
*/
|
||||||
|
static void checktab (lua_State *L, int arg, int what) {
|
||||||
|
if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
|
||||||
|
int n = 1; /* number of elements to pop */
|
||||||
|
if (lua_getmetatable(L, arg) && /* must have metatable */
|
||||||
|
(!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
|
||||||
|
(!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
|
||||||
|
(!(what & TAB_L) || checkfield(L, "__len", ++n))) {
|
||||||
|
lua_pop(L, n); /* pop metatable and tested metamethods */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
luaL_checktype(L, arg, LUA_TTABLE); /* force an error */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_COMPAT_MAXN)
|
#if defined(LUA_COMPAT_MAXN)
|
||||||
@@ -72,8 +77,7 @@ static int maxn (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int tinsert (lua_State *L) {
|
static int tinsert (lua_State *L) {
|
||||||
TabA ta;
|
lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
|
||||||
lua_Integer e = aux_getn(L, 1, &ta) + 1; /* first empty element */
|
|
||||||
lua_Integer pos; /* where to insert new element */
|
lua_Integer pos; /* where to insert new element */
|
||||||
switch (lua_gettop(L)) {
|
switch (lua_gettop(L)) {
|
||||||
case 2: { /* called with only 2 arguments */
|
case 2: { /* called with only 2 arguments */
|
||||||
@@ -85,8 +89,8 @@ static int tinsert (lua_State *L) {
|
|||||||
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
|
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
|
||||||
luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
|
luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
|
||||||
for (i = e; i > pos; i--) { /* move up elements */
|
for (i = e; i > pos; i--) { /* move up elements */
|
||||||
(*ta.geti)(L, 1, i - 1);
|
lua_geti(L, 1, i - 1);
|
||||||
(*ta.seti)(L, 1, i); /* t[i] = t[i - 1] */
|
lua_seti(L, 1, i); /* t[i] = t[i - 1] */
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -94,65 +98,67 @@ static int tinsert (lua_State *L) {
|
|||||||
return luaL_error(L, "wrong number of arguments to 'insert'");
|
return luaL_error(L, "wrong number of arguments to 'insert'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(*ta.seti)(L, 1, pos); /* t[pos] = v */
|
lua_seti(L, 1, pos); /* t[pos] = v */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int tremove (lua_State *L) {
|
static int tremove (lua_State *L) {
|
||||||
TabA ta;
|
lua_Integer size = aux_getn(L, 1, TAB_RW);
|
||||||
lua_Integer size = aux_getn(L, 1, &ta);
|
|
||||||
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 */
|
||||||
luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
|
luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
|
||||||
(*ta.geti)(L, 1, pos); /* result = t[pos] */
|
lua_geti(L, 1, pos); /* result = t[pos] */
|
||||||
for ( ; pos < size; pos++) {
|
for ( ; pos < size; pos++) {
|
||||||
(*ta.geti)(L, 1, pos + 1);
|
lua_geti(L, 1, pos + 1);
|
||||||
(*ta.seti)(L, 1, pos); /* t[pos] = t[pos + 1] */
|
lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
|
||||||
}
|
}
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
(*ta.seti)(L, 1, pos); /* t[pos] = nil */
|
lua_seti(L, 1, pos); /* t[pos] = nil */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
|
||||||
|
** possible, copy in increasing order, which is better for rehashing.
|
||||||
|
** "possible" means destination after original range, or smaller
|
||||||
|
** than origin, or copying to another table.
|
||||||
|
*/
|
||||||
static int tmove (lua_State *L) {
|
static int tmove (lua_State *L) {
|
||||||
TabA ta;
|
|
||||||
lua_Integer f = luaL_checkinteger(L, 2);
|
lua_Integer f = luaL_checkinteger(L, 2);
|
||||||
lua_Integer e = luaL_checkinteger(L, 3);
|
lua_Integer e = luaL_checkinteger(L, 3);
|
||||||
lua_Integer t = luaL_checkinteger(L, 4);
|
lua_Integer t = luaL_checkinteger(L, 4);
|
||||||
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
|
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
|
||||||
/* the following restriction avoids several problems with overflows */
|
checktab(L, 1, TAB_R);
|
||||||
luaL_argcheck(L, f > 0, 2, "initial position must be positive");
|
checktab(L, tt, TAB_W);
|
||||||
if (e >= f) { /* otherwise, nothing to move */
|
if (e >= f) { /* otherwise, nothing to move */
|
||||||
lua_Integer n, i;
|
lua_Integer n, i;
|
||||||
ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL)
|
luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
|
||||||
? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti)
|
"too many elements to move");
|
||||||
: lua_geti;
|
|
||||||
ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL)
|
|
||||||
? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
|
|
||||||
: lua_seti;
|
|
||||||
n = e - f + 1; /* number of elements to move */
|
n = e - f + 1; /* number of elements to move */
|
||||||
if (t > f) {
|
luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
|
||||||
for (i = n - 1; i >= 0; i--) {
|
"destination wrap around");
|
||||||
(*ta.geti)(L, 1, f + i);
|
if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
|
||||||
(*ta.seti)(L, tt, t + i);
|
for (i = 0; i < n; i++) {
|
||||||
|
lua_geti(L, 1, f + i);
|
||||||
|
lua_seti(L, tt, t + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (i = 0; i < n; i++) {
|
for (i = n - 1; i >= 0; i--) {
|
||||||
(*ta.geti)(L, 1, f + i);
|
lua_geti(L, 1, f + i);
|
||||||
(*ta.seti)(L, tt, t + i);
|
lua_seti(L, tt, t + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_pushvalue(L, tt); /* return "to table" */
|
lua_pushvalue(L, tt); /* return destination table */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) {
|
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
|
||||||
(*ta->geti)(L, 1, i);
|
lua_geti(L, 1, i);
|
||||||
if (!lua_isstring(L, -1))
|
if (!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 %d in table for 'concat'",
|
||||||
luaL_typename(L, -1), i);
|
luaL_typename(L, -1), i);
|
||||||
@@ -161,21 +167,19 @@ static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) {
|
|||||||
|
|
||||||
|
|
||||||
static int tconcat (lua_State *L) {
|
static int tconcat (lua_State *L) {
|
||||||
TabA ta;
|
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
|
lua_Integer last = aux_getn(L, 1, TAB_R);
|
||||||
size_t lsep;
|
size_t lsep;
|
||||||
lua_Integer i, last;
|
|
||||||
const char *sep = luaL_optlstring(L, 2, "", &lsep);
|
const char *sep = luaL_optlstring(L, 2, "", &lsep);
|
||||||
checktab(L, 1, &ta);
|
lua_Integer i = luaL_optinteger(L, 3, 1);
|
||||||
i = luaL_optinteger(L, 3, 1);
|
last = luaL_optinteger(L, 4, last);
|
||||||
last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1));
|
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
for (; i < last; i++) {
|
for (; i < last; i++) {
|
||||||
addfield(L, &b, &ta, i);
|
addfield(L, &b, i);
|
||||||
luaL_addlstring(&b, sep, lsep);
|
luaL_addlstring(&b, sep, lsep);
|
||||||
}
|
}
|
||||||
if (i == last) /* add last value (if interval was not empty) */
|
if (i == last) /* add last value (if interval was not empty) */
|
||||||
addfield(L, &b, &ta, i);
|
addfield(L, &b, i);
|
||||||
luaL_pushresult(&b);
|
luaL_pushresult(&b);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -193,7 +197,7 @@ static int pack (lua_State *L) {
|
|||||||
lua_createtable(L, n, 1); /* create result table */
|
lua_createtable(L, n, 1); /* create result table */
|
||||||
lua_insert(L, 1); /* put it at index 1 */
|
lua_insert(L, 1); /* put it at index 1 */
|
||||||
for (i = n; i >= 1; i--) /* assign elements */
|
for (i = n; i >= 1; i--) /* assign elements */
|
||||||
lua_rawseti(L, 1, i);
|
lua_seti(L, 1, i);
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
lua_setfield(L, 1, "n"); /* t.n = number of elements */
|
lua_setfield(L, 1, "n"); /* t.n = number of elements */
|
||||||
return 1; /* return table */
|
return 1; /* return table */
|
||||||
@@ -201,20 +205,17 @@ static int pack (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int unpack (lua_State *L) {
|
static int unpack (lua_State *L) {
|
||||||
TabA ta;
|
|
||||||
lua_Integer i, e;
|
|
||||||
lua_Unsigned n;
|
lua_Unsigned n;
|
||||||
checktab(L, 1, &ta);
|
lua_Integer i = luaL_optinteger(L, 2, 1);
|
||||||
i = luaL_optinteger(L, 2, 1);
|
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
|
||||||
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 (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");
|
||||||
do { /* must have at least one element */
|
for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
|
||||||
(*ta.geti)(L, 1, i); /* push arg[i..e] */
|
lua_geti(L, 1, i);
|
||||||
} while (i++ < e);
|
}
|
||||||
|
lua_geti(L, 1, e); /* push last element */
|
||||||
return (int)n;
|
return (int)n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,103 +226,197 @@ static int unpack (lua_State *L) {
|
|||||||
/*
|
/*
|
||||||
** {======================================================
|
** {======================================================
|
||||||
** Quicksort
|
** Quicksort
|
||||||
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
|
** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
|
||||||
** Addison-Wesley, 1993.)
|
** Addison-Wesley, 1993.)
|
||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
static void set2 (lua_State *L, TabA *ta, int i, int j) {
|
/* type for array indices */
|
||||||
(*ta->seti)(L, 1, i);
|
typedef unsigned int IdxT;
|
||||||
(*ta->seti)(L, 1, j);
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Produce a "random" 'unsigned int' to randomize pivot choice. This
|
||||||
|
** macro is used only when 'sort' detects a big imbalance in the result
|
||||||
|
** of a partition. (If you don't want/need this "randomness", ~0 is a
|
||||||
|
** good choice.)
|
||||||
|
*/
|
||||||
|
#if !defined(l_randomizePivot) /* { */
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
/* size of 'e' measured in number of 'unsigned int's */
|
||||||
|
#define sof(e) (sizeof(e) / sizeof(unsigned int))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Use 'time' and 'clock' as sources of "randomness". Because we don't
|
||||||
|
** know the types 'clock_t' and 'time_t', we cannot cast them to
|
||||||
|
** anything without risking overflows. A safe way to use their values
|
||||||
|
** is to copy them to an array of a known type and use the array values.
|
||||||
|
*/
|
||||||
|
static unsigned int l_randomizePivot (void) {
|
||||||
|
clock_t c = clock();
|
||||||
|
time_t t = time(NULL);
|
||||||
|
unsigned int buff[sof(c) + sof(t)];
|
||||||
|
unsigned int i, rnd = 0;
|
||||||
|
memcpy(buff, &c, sof(c) * sizeof(unsigned int));
|
||||||
|
memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
|
||||||
|
for (i = 0; i < sof(buff); i++)
|
||||||
|
rnd += buff[i];
|
||||||
|
return rnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/* arrays larger than 'RANLIMIT' may use randomized pivots */
|
||||||
|
#define RANLIMIT 100u
|
||||||
|
|
||||||
|
|
||||||
|
static void set2 (lua_State *L, IdxT i, IdxT j) {
|
||||||
|
lua_seti(L, 1, i);
|
||||||
|
lua_seti(L, 1, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Return true iff value at stack index 'a' is less than the value at
|
||||||
|
** index 'b' (according to the order of the sort).
|
||||||
|
*/
|
||||||
static int sort_comp (lua_State *L, int a, int b) {
|
static int sort_comp (lua_State *L, int a, int b) {
|
||||||
if (!lua_isnil(L, 2)) { /* function? */
|
if (lua_isnil(L, 2)) /* no function? */
|
||||||
|
return lua_compare(L, a, b, LUA_OPLT); /* a < b */
|
||||||
|
else { /* function */
|
||||||
int res;
|
int res;
|
||||||
lua_pushvalue(L, 2);
|
lua_pushvalue(L, 2); /* push function */
|
||||||
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
||||||
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
|
lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
|
||||||
lua_call(L, 2, 1);
|
lua_call(L, 2, 1); /* call function */
|
||||||
res = lua_toboolean(L, -1);
|
res = lua_toboolean(L, -1); /* get result */
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1); /* pop result */
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
else /* a < b? */
|
|
||||||
return lua_compare(L, a, b, LUA_OPLT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void auxsort (lua_State *L, TabA *ta, int l, int u) {
|
|
||||||
while (l < u) { /* for tail recursion */
|
/*
|
||||||
int i, j;
|
** Does the partition: Pivot P is at the top of the stack.
|
||||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
** precondition: a[lo] <= P == a[up-1] <= a[up],
|
||||||
(*ta->geti)(L, 1, l);
|
** so it only needs to do the partition from lo + 1 to up - 2.
|
||||||
(*ta->geti)(L, 1, u);
|
** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
|
||||||
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
|
** returns 'i'.
|
||||||
set2(L, ta, l, u); /* swap a[l] - a[u] */
|
*/
|
||||||
|
static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
||||||
|
IdxT i = lo; /* will be incremented before first use */
|
||||||
|
IdxT j = up - 1; /* will be decremented before first use */
|
||||||
|
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
|
||||||
|
for (;;) {
|
||||||
|
/* next loop: repeat ++i while a[i] < P */
|
||||||
|
while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||||
|
if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
|
||||||
|
luaL_error(L, "invalid order function for sorting");
|
||||||
|
lua_pop(L, 1); /* remove a[i] */
|
||||||
|
}
|
||||||
|
/* after the loop, a[i] >= P and a[lo .. i - 1] < P */
|
||||||
|
/* next loop: repeat --j while P < a[j] */
|
||||||
|
while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||||
|
if (j < i) /* j < i but a[j] > P ?? */
|
||||||
|
luaL_error(L, "invalid order function for sorting");
|
||||||
|
lua_pop(L, 1); /* remove a[j] */
|
||||||
|
}
|
||||||
|
/* after the loop, a[j] <= P and a[j + 1 .. up] >= P */
|
||||||
|
if (j < i) { /* no elements out of place? */
|
||||||
|
/* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */
|
||||||
|
lua_pop(L, 1); /* pop a[j] */
|
||||||
|
/* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */
|
||||||
|
set2(L, up - 1, i);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/* otherwise, swap a[i] - a[j] to restore invariant and repeat */
|
||||||
|
set2(L, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Choose an element in the middle (2nd-3th quarters) of [lo,up]
|
||||||
|
** "randomized" by 'rnd'
|
||||||
|
*/
|
||||||
|
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
||||||
|
IdxT r4 = (up - lo) / 4; /* range/4 */
|
||||||
|
IdxT p = rnd % (r4 * 2) + (lo + r4);
|
||||||
|
lua_assert(lo + r4 <= p && p <= up - r4);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** QuickSort algorithm (recursive function)
|
||||||
|
*/
|
||||||
|
static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||||
|
unsigned int rnd) {
|
||||||
|
while (lo < up) { /* loop for tail recursion */
|
||||||
|
IdxT p; /* Pivot index */
|
||||||
|
IdxT n; /* to be used later */
|
||||||
|
/* sort elements 'lo', 'p', and 'up' */
|
||||||
|
lua_geti(L, 1, lo);
|
||||||
|
lua_geti(L, 1, up);
|
||||||
|
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
|
||||||
|
set2(L, lo, up); /* swap a[lo] - a[up] */
|
||||||
else
|
else
|
||||||
lua_pop(L, 2);
|
lua_pop(L, 2); /* remove both values */
|
||||||
if (u-l == 1) break; /* only 2 elements */
|
if (up - lo == 1) /* only 2 elements? */
|
||||||
i = (l+u)/2;
|
return; /* already sorted */
|
||||||
(*ta->geti)(L, 1, i);
|
if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
|
||||||
(*ta->geti)(L, 1, l);
|
p = (lo + up)/2; /* middle element is a good pivot */
|
||||||
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
|
else /* for larger intervals, it is worth a random pivot */
|
||||||
set2(L, ta, i, l);
|
p = choosePivot(lo, up, rnd);
|
||||||
|
lua_geti(L, 1, p);
|
||||||
|
lua_geti(L, 1, lo);
|
||||||
|
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
|
||||||
|
set2(L, p, lo); /* swap a[p] - a[lo] */
|
||||||
else {
|
else {
|
||||||
lua_pop(L, 1); /* remove a[l] */
|
lua_pop(L, 1); /* remove a[lo] */
|
||||||
(*ta->geti)(L, 1, u);
|
lua_geti(L, 1, up);
|
||||||
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
|
if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
|
||||||
set2(L, ta, i, u);
|
set2(L, p, up); /* swap a[up] - a[p] */
|
||||||
else
|
else
|
||||||
lua_pop(L, 2);
|
lua_pop(L, 2);
|
||||||
}
|
}
|
||||||
if (u-l == 2) break; /* only 3 elements */
|
if (up - lo == 2) /* only 3 elements? */
|
||||||
(*ta->geti)(L, 1, i); /* Pivot */
|
return; /* already sorted */
|
||||||
lua_pushvalue(L, -1);
|
lua_geti(L, 1, p); /* get middle element (Pivot) */
|
||||||
(*ta->geti)(L, 1, u-1);
|
lua_pushvalue(L, -1); /* push Pivot */
|
||||||
set2(L, ta, i, u-1);
|
lua_geti(L, 1, up - 1); /* push a[up - 1] */
|
||||||
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
|
set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
|
||||||
i = l; j = u-1;
|
p = partition(L, lo, up);
|
||||||
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
/* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
|
||||||
/* repeat ++i until a[i] >= P */
|
if (p - lo < up - p) { /* lower interval is smaller? */
|
||||||
while ((*ta->geti)(L, 1, ++i), sort_comp(L, -1, -2)) {
|
auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
|
||||||
if (i>=u) luaL_error(L, "invalid order function for sorting");
|
n = p - lo; /* size of smaller interval */
|
||||||
lua_pop(L, 1); /* remove a[i] */
|
lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
|
||||||
}
|
|
||||||
/* repeat --j until a[j] <= P */
|
|
||||||
while ((*ta->geti)(L, 1, --j), sort_comp(L, -3, -1)) {
|
|
||||||
if (j<=l) luaL_error(L, "invalid order function for sorting");
|
|
||||||
lua_pop(L, 1); /* remove a[j] */
|
|
||||||
}
|
|
||||||
if (j<i) {
|
|
||||||
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
set2(L, ta, i, j);
|
|
||||||
}
|
|
||||||
(*ta->geti)(L, 1, u-1);
|
|
||||||
(*ta->geti)(L, 1, i);
|
|
||||||
set2(L, ta, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
|
||||||
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
|
|
||||||
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
|
|
||||||
if (i-l < u-i) {
|
|
||||||
j=l; i=i-1; l=i+2;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
j=i+1; i=u; u=j-2;
|
auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
|
||||||
|
n = up - p; /* size of smaller interval */
|
||||||
|
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
|
||||||
}
|
}
|
||||||
auxsort(L, ta, j, i); /* call recursively the smaller one */
|
if ((up - lo) / 128 > n) /* partition too imbalanced? */
|
||||||
} /* repeat the routine for the larger one */
|
rnd = l_randomizePivot(); /* try a new randomization */
|
||||||
|
} /* tail call auxsort(L, lo, up, rnd) */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int sort (lua_State *L) {
|
static int sort (lua_State *L) {
|
||||||
TabA ta;
|
lua_Integer n = aux_getn(L, 1, TAB_RW);
|
||||||
int n = (int)aux_getn(L, 1, &ta);
|
if (n > 1) { /* non-trivial interval? */
|
||||||
luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */
|
luaL_argcheck(L, n < INT_MAX, 1, "array too big");
|
||||||
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
|
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
|
||||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */
|
||||||
lua_settop(L, 2); /* make sure there are two arguments */
|
lua_settop(L, 2); /* make sure there are two arguments */
|
||||||
auxsort(L, &ta, 1, n);
|
auxsort(L, 1, (IdxT)n, 0);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
174
ltests.c
174
ltests.c
@@ -1,18 +1,21 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltests.c,v 2.187 2014/10/06 17:06:49 roberto Exp roberto $
|
** $Id: ltests.c,v 2.208 2015/09/08 16:55:43 roberto Exp roberto $
|
||||||
** Internal Module for Debugging of the Lua Implementation
|
** Internal Module for Debugging of the Lua Implementation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ltests_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <setjmp.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define ltests_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -46,6 +49,9 @@ int islocked = 0;
|
|||||||
#define obj_at(L,k) (L->ci->func + (k))
|
#define obj_at(L,k) (L->ci->func + (k))
|
||||||
|
|
||||||
|
|
||||||
|
static int runC (lua_State *L, lua_State *L1, const char *pc);
|
||||||
|
|
||||||
|
|
||||||
static void setnameval (lua_State *L, const char *name, int val) {
|
static void setnameval (lua_State *L, const char *name, int val) {
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_pushinteger(L, val);
|
lua_pushinteger(L, val);
|
||||||
@@ -152,7 +158,7 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
|||||||
memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
|
memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
|
||||||
freeblock(mc, block); /* erase (and check) old copy */
|
freeblock(mc, block); /* erase (and check) old copy */
|
||||||
}
|
}
|
||||||
/* initialize new part of the block with something `weird' */
|
/* initialize new part of the block with something weird */
|
||||||
fillmem(cast(char *, newblock + 1) + commonsize, size - commonsize);
|
fillmem(cast(char *, newblock + 1) + commonsize, size - commonsize);
|
||||||
/* initialize marks after block */
|
/* initialize marks after block */
|
||||||
for (i = 0; i < MARKSIZE; i++)
|
for (i = 0; i < MARKSIZE; i++)
|
||||||
@@ -283,11 +289,11 @@ static void checkLclosure (global_State *g, LClosure *cl) {
|
|||||||
static int lua_checkpc (lua_State *L, CallInfo *ci) {
|
static int lua_checkpc (lua_State *L, CallInfo *ci) {
|
||||||
if (!isLua(ci)) return 1;
|
if (!isLua(ci)) return 1;
|
||||||
else {
|
else {
|
||||||
Proto *p;
|
/* if function yielded (inside a hook), real 'func' is in 'extra' field */
|
||||||
if (L->status != LUA_YIELD || ci != L->ci)
|
StkId f = (L->status != LUA_YIELD || ci != L->ci)
|
||||||
p = ci_func(ci)->p;
|
? ci->func
|
||||||
else /* real 'func' was saved in 'extra' field */
|
: restorestack(L, ci->extra);
|
||||||
p = clLvalue(restorestack(L, ci->extra))->p;
|
Proto *p = clLvalue(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;
|
||||||
}
|
}
|
||||||
@@ -305,9 +311,9 @@ static void checkstack (global_State *g, lua_State *L1) {
|
|||||||
lua_assert(ci->top <= L1->stack_last);
|
lua_assert(ci->top <= L1->stack_last);
|
||||||
lua_assert(lua_checkpc(L1, ci));
|
lua_assert(lua_checkpc(L1, ci));
|
||||||
}
|
}
|
||||||
if (L1->stack) {
|
if (L1->stack) { /* complete thread? */
|
||||||
for (o = L1->stack; o < L1->top; o++)
|
for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++)
|
||||||
checkliveness(g, o);
|
checkliveness(L1, o); /* entire stack must have valid values */
|
||||||
}
|
}
|
||||||
else lua_assert(L1->stacksize == 0);
|
else lua_assert(L1->stacksize == 0);
|
||||||
}
|
}
|
||||||
@@ -534,7 +540,7 @@ static int listk (lua_State *L) {
|
|||||||
|
|
||||||
static int listlocals (lua_State *L) {
|
static int listlocals (lua_State *L) {
|
||||||
Proto *p;
|
Proto *p;
|
||||||
int pc = (int)luaL_checkinteger(L, 2) - 1;
|
int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
const char *name;
|
const char *name;
|
||||||
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||||
@@ -549,12 +555,25 @@ static int listlocals (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void printstack (lua_State *L) {
|
||||||
|
int i;
|
||||||
|
int n = lua_gettop(L);
|
||||||
|
for (i = 1; i <= n; i++) {
|
||||||
|
printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int get_limits (lua_State *L) {
|
static int get_limits (lua_State *L) {
|
||||||
lua_createtable(L, 0, 5);
|
lua_createtable(L, 0, 5);
|
||||||
setnameval(L, "BITS_INT", LUAI_BITSINT);
|
setnameval(L, "BITS_INT", LUAI_BITSINT);
|
||||||
|
setnameval(L, "MAXARG_Ax", MAXARG_Ax);
|
||||||
|
setnameval(L, "MAXARG_Bx", MAXARG_Bx);
|
||||||
|
setnameval(L, "MAXARG_sBx", MAXARG_sBx);
|
||||||
|
setnameval(L, "BITS_INT", LUAI_BITSINT);
|
||||||
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||||
setnameval(L, "MAXSTACK", MAXSTACK);
|
|
||||||
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -657,13 +676,13 @@ static int stacklevel (lua_State *L) {
|
|||||||
lua_pushinteger(L, (L->top - L->stack));
|
lua_pushinteger(L, (L->top - L->stack));
|
||||||
lua_pushinteger(L, (L->stack_last - L->stack));
|
lua_pushinteger(L, (L->stack_last - L->stack));
|
||||||
lua_pushinteger(L, (unsigned long)&a);
|
lua_pushinteger(L, (unsigned long)&a);
|
||||||
return 5;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int table_query (lua_State *L) {
|
static int table_query (lua_State *L) {
|
||||||
const Table *t;
|
const Table *t;
|
||||||
int i = (int)luaL_optinteger(L, 2, -1);
|
int i = cast_int(luaL_optinteger(L, 2, -1));
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
t = hvalue(obj_at(L, 1));
|
t = hvalue(obj_at(L, 1));
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
@@ -696,7 +715,7 @@ static int table_query (lua_State *L) {
|
|||||||
|
|
||||||
static int string_query (lua_State *L) {
|
static int string_query (lua_State *L) {
|
||||||
stringtable *tb = &G(L)->strt;
|
stringtable *tb = &G(L)->strt;
|
||||||
int s = (int)luaL_optinteger(L, 1, 0) - 1;
|
int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
|
||||||
if (s == -1) {
|
if (s == -1) {
|
||||||
lua_pushinteger(L ,tb->size);
|
lua_pushinteger(L ,tb->size);
|
||||||
lua_pushinteger(L ,tb->nuse);
|
lua_pushinteger(L ,tb->nuse);
|
||||||
@@ -705,7 +724,7 @@ static int string_query (lua_State *L) {
|
|||||||
else if (s < tb->size) {
|
else if (s < tb->size) {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (ts = tb->hash[s]; ts != NULL; ts = ts->hnext) {
|
for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top, ts);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
n++;
|
n++;
|
||||||
@@ -734,14 +753,14 @@ 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, (int)luaL_checkinteger(L, 1));
|
luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
|
||||||
lua_assert(lua_gettop(L) == level);
|
lua_assert(lua_gettop(L) == level);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int upvalue (lua_State *L) {
|
static int upvalue (lua_State *L) {
|
||||||
int n = (int)luaL_checkinteger(L, 2);
|
int n = cast_int(luaL_checkinteger(L, 2));
|
||||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||||
if (lua_isnone(L, 3)) {
|
if (lua_isnone(L, 3)) {
|
||||||
const char *name = lua_getupvalue(L, 1, n);
|
const char *name = lua_getupvalue(L, 1, n);
|
||||||
@@ -892,17 +911,69 @@ static int doremote (lua_State *L) {
|
|||||||
static int int2fb_aux (lua_State *L) {
|
static int int2fb_aux (lua_State *L) {
|
||||||
int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1));
|
int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1));
|
||||||
lua_pushinteger(L, b);
|
lua_pushinteger(L, b);
|
||||||
lua_pushinteger(L, luaO_fb2int(b));
|
lua_pushinteger(L, (unsigned int)luaO_fb2int(b));
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int log2_aux (lua_State *L) {
|
||||||
|
unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
|
||||||
|
lua_pushinteger(L, luaO_ceillog2(x));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; };
|
||||||
|
|
||||||
|
/*
|
||||||
|
** does a long-jump back to "main program".
|
||||||
|
*/
|
||||||
|
static int panicback (lua_State *L) {
|
||||||
|
struct Aux *b;
|
||||||
|
lua_checkstack(L, 1); /* open space for 'Aux' struct */
|
||||||
|
lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */
|
||||||
|
b = (struct Aux *)lua_touserdata(L, -1);
|
||||||
|
lua_pop(L, 1); /* remove 'Aux' struct */
|
||||||
|
runC(b->L, L, b->paniccode); /* run optional panic code */
|
||||||
|
longjmp(b->jb, 1);
|
||||||
|
return 1; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int checkpanic (lua_State *L) {
|
||||||
|
struct Aux b;
|
||||||
|
void *ud;
|
||||||
|
lua_State *L1;
|
||||||
|
const char *code = luaL_checkstring(L, 1);
|
||||||
|
lua_Alloc f = lua_getallocf(L, &ud);
|
||||||
|
b.paniccode = luaL_optstring(L, 2, "");
|
||||||
|
b.L = L;
|
||||||
|
L1 = lua_newstate(f, ud); /* create new state */
|
||||||
|
if (L1 == NULL) { /* error? */
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
lua_atpanic(L1, panicback); /* set its panic function */
|
||||||
|
lua_pushlightuserdata(L1, &b);
|
||||||
|
lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */
|
||||||
|
if (setjmp(b.jb) == 0) { /* set jump buffer */
|
||||||
|
runC(L, L1, code); /* run code unprotected */
|
||||||
|
lua_pushliteral(L, "no errors");
|
||||||
|
}
|
||||||
|
else { /* error handling */
|
||||||
|
/* move error message to original state */
|
||||||
|
lua_pushstring(L, lua_tostring(L1, -1));
|
||||||
|
}
|
||||||
|
lua_close(L1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {====================================================================
|
||||||
** function to test the API with C. It interprets a kind of assembler
|
** function to test the API with C. It interprets a kind of assembler
|
||||||
** language with calls to the API, so the test can be driven by Lua code
|
** language with calls to the API, so the test can be driven by Lua code
|
||||||
** =======================================================
|
** =====================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -925,11 +996,16 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
|
|||||||
int sig = 1;
|
int sig = 1;
|
||||||
skip(pc);
|
skip(pc);
|
||||||
if (**pc == '.') {
|
if (**pc == '.') {
|
||||||
res = lua_tointeger(L1, -1);
|
res = cast_int(lua_tointeger(L1, -1));
|
||||||
lua_pop(L1, 1);
|
lua_pop(L1, 1);
|
||||||
(*pc)++;
|
(*pc)++;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
else if (**pc == '*') {
|
||||||
|
res = lua_gettop(L1);
|
||||||
|
(*pc)++;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
else if (**pc == '-') {
|
else if (**pc == '-') {
|
||||||
sig = -1;
|
sig = -1;
|
||||||
(*pc)++;
|
(*pc)++;
|
||||||
@@ -996,7 +1072,7 @@ static int Cfunck (lua_State *L, int status, lua_KContext ctx);
|
|||||||
** LUA_OPUNM -> _
|
** LUA_OPUNM -> _
|
||||||
** LUA_OPBNOT -> !
|
** LUA_OPBNOT -> !
|
||||||
*/
|
*/
|
||||||
static char ops[] = "+-*%^/\\&|~<>_!";
|
static const char ops[] = "+-*%^/\\&|~<>_!";
|
||||||
|
|
||||||
static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||||
char buff[300];
|
char buff[300];
|
||||||
@@ -1032,12 +1108,18 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
}
|
}
|
||||||
else if EQ("checkstack") {
|
else if EQ("checkstack") {
|
||||||
int sz = getnum;
|
int sz = getnum;
|
||||||
luaL_checkstack(L1, sz, getstring);
|
const char *msg = getstring;
|
||||||
|
if (*msg == '\0')
|
||||||
|
msg = NULL; /* to test 'luaL_checkstack' with no message */
|
||||||
|
luaL_checkstack(L1, sz, msg);
|
||||||
}
|
}
|
||||||
else if EQ("compare") {
|
else if EQ("compare") {
|
||||||
|
const char *opt = getstring; /* EQ, LT, or LE */
|
||||||
|
int op = (opt[0] == 'E') ? LUA_OPEQ
|
||||||
|
: (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE;
|
||||||
int a = getindex;
|
int a = getindex;
|
||||||
int b = getindex;
|
int b = getindex;
|
||||||
lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
|
lua_pushboolean(L1, lua_compare(L1, a, b, op));
|
||||||
}
|
}
|
||||||
else if EQ("concat") {
|
else if EQ("concat") {
|
||||||
lua_concat(L1, getnum);
|
lua_concat(L1, getnum);
|
||||||
@@ -1113,9 +1195,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
||||||
}
|
}
|
||||||
else if EQ("loadstring") {
|
else if EQ("loadstring") {
|
||||||
size_t sl;
|
const char *s = luaL_checkstring(L1, getnum);
|
||||||
const char *s = luaL_checklstring(L1, getnum, &sl);
|
luaL_loadstring(L1, s);
|
||||||
luaL_loadbuffer(L1, s, sl, s);
|
|
||||||
}
|
}
|
||||||
else if EQ("newmetatable") {
|
else if EQ("newmetatable") {
|
||||||
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
||||||
@@ -1138,7 +1219,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
else if EQ("pcall") {
|
else if EQ("pcall") {
|
||||||
int narg = getnum;
|
int narg = getnum;
|
||||||
int nres = getnum;
|
int nres = getnum;
|
||||||
status = lua_pcall(L1, narg, nres, 0);
|
status = lua_pcall(L1, narg, nres, getnum);
|
||||||
}
|
}
|
||||||
else if EQ("pcallk") {
|
else if EQ("pcallk") {
|
||||||
int narg = getnum;
|
int narg = getnum;
|
||||||
@@ -1155,15 +1236,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
||||||
lua_pop(L1, 1);
|
lua_pop(L1, 1);
|
||||||
}
|
}
|
||||||
else {
|
else printstack(L1);
|
||||||
int i;
|
|
||||||
n = lua_gettop(L1);
|
|
||||||
for (i = 1; i <= n; i++) {
|
|
||||||
printf("%s ", luaL_tolstring(L1, i, NULL));
|
|
||||||
lua_pop(L1, 1);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if EQ("pushbool") {
|
else if EQ("pushbool") {
|
||||||
lua_pushboolean(L1, getnum);
|
lua_pushboolean(L1, getnum);
|
||||||
@@ -1196,6 +1269,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
int t = getindex;
|
int t = getindex;
|
||||||
lua_rawgeti(L1, t, getnum);
|
lua_rawgeti(L1, t, getnum);
|
||||||
}
|
}
|
||||||
|
else if EQ("rawgetp") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_rawgetp(L1, t, cast(void *, cast(size_t, getnum)));
|
||||||
|
}
|
||||||
|
else if EQ("rawsetp") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_rawsetp(L1, t, cast(void *, cast(size_t, getnum)));
|
||||||
|
}
|
||||||
else if EQ("remove") {
|
else if EQ("remove") {
|
||||||
lua_remove(L1, getnum);
|
lua_remove(L1, getnum);
|
||||||
}
|
}
|
||||||
@@ -1244,6 +1325,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
int i = getindex;
|
int i = getindex;
|
||||||
lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
|
lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
|
||||||
}
|
}
|
||||||
|
else if EQ("error") {
|
||||||
|
lua_error(L1);
|
||||||
|
}
|
||||||
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;
|
||||||
@@ -1271,7 +1355,7 @@ 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);
|
||||||
lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
|
lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
|
||||||
}
|
}
|
||||||
else if EQ("type") {
|
else if EQ("type") {
|
||||||
lua_pushstring(L1, luaL_typename(L1, getnum));
|
lua_pushstring(L1, luaL_typename(L1, getnum));
|
||||||
@@ -1366,7 +1450,7 @@ static void Chook (lua_State *L, lua_Debug *ar) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** sets registry.C_HOOK[L] = scpt and sets Chook as a hook
|
** sets 'registry.C_HOOK[L] = scpt' and sets 'Chook' as a hook
|
||||||
*/
|
*/
|
||||||
static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
|
static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
|
||||||
if (*scpt == '\0') { /* no script? */
|
if (*scpt == '\0') { /* no script? */
|
||||||
@@ -1393,7 +1477,7 @@ static int sethook (lua_State *L) {
|
|||||||
else {
|
else {
|
||||||
const char *scpt = luaL_checkstring(L, 1);
|
const char *scpt = luaL_checkstring(L, 1);
|
||||||
const char *smask = luaL_checkstring(L, 2);
|
const char *smask = luaL_checkstring(L, 2);
|
||||||
int count = (int)luaL_optinteger(L, 3, 0);
|
int count = cast_int(luaL_optinteger(L, 3, 0));
|
||||||
int mask = 0;
|
int mask = 0;
|
||||||
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
||||||
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
||||||
@@ -1436,11 +1520,13 @@ static const struct luaL_Reg tests_funcs[] = {
|
|||||||
{"getref", getref},
|
{"getref", getref},
|
||||||
{"hash", hash_query},
|
{"hash", hash_query},
|
||||||
{"int2fb", int2fb_aux},
|
{"int2fb", int2fb_aux},
|
||||||
|
{"log2", log2_aux},
|
||||||
{"limits", get_limits},
|
{"limits", get_limits},
|
||||||
{"listcode", listcode},
|
{"listcode", listcode},
|
||||||
{"listk", listk},
|
{"listk", listk},
|
||||||
{"listlocals", listlocals},
|
{"listlocals", listlocals},
|
||||||
{"loadlib", loadlib},
|
{"loadlib", loadlib},
|
||||||
|
{"checkpanic", checkpanic},
|
||||||
{"newstate", newstate},
|
{"newstate", newstate},
|
||||||
{"newuserdata", newuserdata},
|
{"newuserdata", newuserdata},
|
||||||
{"num2int", num2int},
|
{"num2int", num2int},
|
||||||
|
|||||||
26
ltests.h
26
ltests.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltests.h,v 2.39 2014/07/24 19:33:29 roberto Exp roberto $
|
** $Id: ltests.h,v 2.48 2015/06/18 14:27:44 roberto Exp roberto $
|
||||||
** Internal Header for Debugging of the Lua Implementation
|
** Internal Header for Debugging of the Lua Implementation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -37,6 +37,15 @@
|
|||||||
#define UNUSED(x) (x=0, (void)(x))
|
#define UNUSED(x) (x=0, (void)(x))
|
||||||
|
|
||||||
|
|
||||||
|
/* test for sizes in 'l_sprintf' (make sure whole buffer is available) */
|
||||||
|
#undef l_sprintf
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
#define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), snprintf(s,sz,f,i))
|
||||||
|
#else
|
||||||
|
#define l_sprintf(s,sz,f,i) (memset(s,0xAB,sz), sprintf(s,f,i))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* memory-allocator control variables */
|
/* memory-allocator control variables */
|
||||||
typedef struct Memcontrol {
|
typedef struct Memcontrol {
|
||||||
unsigned long numblocks;
|
unsigned long numblocks;
|
||||||
@@ -46,7 +55,7 @@ typedef struct Memcontrol {
|
|||||||
unsigned long objcount[LUA_NUMTAGS];
|
unsigned long objcount[LUA_NUMTAGS];
|
||||||
} Memcontrol;
|
} Memcontrol;
|
||||||
|
|
||||||
extern Memcontrol l_memcontrol;
|
LUA_API Memcontrol l_memcontrol;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -81,9 +90,10 @@ struct L_EXTRA { int lock; int *plock; };
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int luaB_opentests (lua_State *L);
|
LUA_API int luaB_opentests (lua_State *L);
|
||||||
|
|
||||||
void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize);
|
LUA_API void *debug_realloc (void *ud, void *block,
|
||||||
|
size_t osize, size_t nsize);
|
||||||
|
|
||||||
#if defined(lua_c)
|
#if defined(lua_c)
|
||||||
#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol)
|
#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol)
|
||||||
@@ -100,9 +110,17 @@ void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize);
|
|||||||
#define MINSTRTABSIZE 2
|
#define MINSTRTABSIZE 2
|
||||||
|
|
||||||
|
|
||||||
|
/* make stack-overflow tests run faster */
|
||||||
|
#undef LUAI_MAXSTACK
|
||||||
|
#define LUAI_MAXSTACK 50000
|
||||||
|
|
||||||
|
|
||||||
#undef LUAI_USER_ALIGNMENT_T
|
#undef LUAI_USER_ALIGNMENT_T
|
||||||
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||||
|
|
||||||
|
|
||||||
|
#define STRCACHE_N 23
|
||||||
|
#define STRCACHE_M 5
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
52
ltm.c
52
ltm.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltm.c,v 2.28 2014/07/18 12:17:54 roberto Exp roberto $
|
** $Id: ltm.c,v 2.36 2015/11/03 15:47:30 roberto Exp roberto $
|
||||||
** Tag methods
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define ltm_c
|
#define ltm_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -55,7 +57,7 @@ void luaT_init (lua_State *L) {
|
|||||||
** tag methods
|
** tag methods
|
||||||
*/
|
*/
|
||||||
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
||||||
const TValue *tm = luaH_getstr(events, ename);
|
const TValue *tm = luaH_getshortstr(events, ename);
|
||||||
lua_assert(event <= TM_EQ);
|
lua_assert(event <= TM_EQ);
|
||||||
if (ttisnil(tm)) { /* no tag method? */
|
if (ttisnil(tm)) { /* no tag method? */
|
||||||
events->flags |= cast_byte(1u<<event); /* cache this fact */
|
events->flags |= cast_byte(1u<<event); /* cache this fact */
|
||||||
@@ -77,20 +79,41 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
|||||||
default:
|
default:
|
||||||
mt = G(L)->mt[ttnov(o)];
|
mt = G(L)->mt[ttnov(o)];
|
||||||
}
|
}
|
||||||
return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Return the name of the type of an object. For tables and userdata
|
||||||
|
** with metatable, use their '__name' metafield, if present.
|
||||||
|
*/
|
||||||
|
const char *luaT_objtypename (lua_State *L, const TValue *o) {
|
||||||
|
Table *mt;
|
||||||
|
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
|
||||||
|
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
|
||||||
|
const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
|
||||||
|
if (ttisstring(name)) /* is '__name' a string? */
|
||||||
|
return getstr(tsvalue(name)); /* use it as type name */
|
||||||
|
}
|
||||||
|
return ttypename(ttnov(o)); /* else use standard type name */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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, TValue *p3, int hasres) {
|
const TValue *p2, TValue *p3, int hasres) {
|
||||||
ptrdiff_t result = savestack(L, p3);
|
ptrdiff_t result = savestack(L, p3);
|
||||||
setobj2s(L, L->top++, f); /* push function */
|
StkId func = L->top;
|
||||||
setobj2s(L, L->top++, p1); /* 1st argument */
|
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
||||||
setobj2s(L, L->top++, p2); /* 2nd argument */
|
setobj2s(L, func + 1, p1); /* 1st argument */
|
||||||
|
setobj2s(L, func + 2, p2); /* 2nd argument */
|
||||||
|
L->top += 3;
|
||||||
if (!hasres) /* no result? 'p3' is third argument */
|
if (!hasres) /* no result? 'p3' is third argument */
|
||||||
setobj2s(L, L->top++, p3); /* 3rd argument */
|
setobj2s(L, L->top++, p3); /* 3rd argument */
|
||||||
/* metamethod may yield only when called from Lua code */
|
/* metamethod may yield only when called from Lua code */
|
||||||
luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
|
if (isLua(L->ci))
|
||||||
|
luaD_call(L, func, hasres);
|
||||||
|
else
|
||||||
|
luaD_callnoyield(L, func, hasres);
|
||||||
if (hasres) { /* if has result, move it to its place */
|
if (hasres) { /* if has result, move it to its place */
|
||||||
p3 = restorestack(L, result);
|
p3 = restorestack(L, result);
|
||||||
setobjs2s(L, p3, --L->top);
|
setobjs2s(L, p3, --L->top);
|
||||||
@@ -115,15 +138,18 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|||||||
switch (event) {
|
switch (event) {
|
||||||
case TM_CONCAT:
|
case TM_CONCAT:
|
||||||
luaG_concaterror(L, p1, p2);
|
luaG_concaterror(L, p1, p2);
|
||||||
case TM_IDIV: case TM_BAND: case TM_BOR: case TM_BXOR:
|
/* call never returns, but to avoid warnings: *//* FALLTHROUGH */
|
||||||
|
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: {
|
||||||
lua_Number dummy;
|
lua_Number dummy;
|
||||||
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
|
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
|
||||||
luaG_tointerror(L, p1, p2);
|
luaG_tointerror(L, p1, p2);
|
||||||
/* else go through */
|
else
|
||||||
|
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
|
||||||
}
|
}
|
||||||
|
/* calls never return, but to avoid warnings: *//* FALLTHROUGH */
|
||||||
default:
|
default:
|
||||||
luaG_aritherror(L, p1, p2);
|
luaG_opinterror(L, p1, p2, "perform arithmetic on");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
ltm.h
7
ltm.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltm.h,v 2.19 2013/12/30 20:47:58 roberto Exp roberto $
|
** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 roberto Exp roberto $
|
||||||
** Tag methods
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -21,7 +21,7 @@ typedef enum {
|
|||||||
TM_GC,
|
TM_GC,
|
||||||
TM_MODE,
|
TM_MODE,
|
||||||
TM_LEN,
|
TM_LEN,
|
||||||
TM_EQ, /* last tag method with `fast' access */
|
TM_EQ, /* last tag method with fast access */
|
||||||
TM_ADD,
|
TM_ADD,
|
||||||
TM_SUB,
|
TM_SUB,
|
||||||
TM_MUL,
|
TM_MUL,
|
||||||
@@ -51,11 +51,12 @@ typedef enum {
|
|||||||
#define fasttm(l,et,e) gfasttm(G(l), et, e)
|
#define fasttm(l,et,e) gfasttm(G(l), et, e)
|
||||||
|
|
||||||
#define ttypename(x) luaT_typenames_[(x) + 1]
|
#define ttypename(x) luaT_typenames_[(x) + 1]
|
||||||
#define objtypename(x) ttypename(ttnov(x))
|
|
||||||
|
|
||||||
LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
|
LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
|
||||||
|
|
||||||
|
|
||||||
|
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
|
||||||
|
|
||||||
LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
|
LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
|
||||||
LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
|
LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
|
||||||
TMS event);
|
TMS event);
|
||||||
|
|||||||
81
lua.c
81
lua.c
@@ -1,17 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lua.c,v 1.216 2014/10/20 18:19:26 roberto Exp roberto $
|
** $Id: lua.c,v 1.225 2015/03/30 15:42:59 roberto Exp roberto $
|
||||||
** Lua stand-alone interpreter
|
** Lua stand-alone interpreter
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lua_c
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lua_c
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -50,14 +52,14 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#define lua_stdin_is_tty() isatty(0)
|
#define lua_stdin_is_tty() isatty(0)
|
||||||
|
|
||||||
#elif defined(LUA_WIN) /* }{ */
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
|
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definition */
|
/* ISO C definition */
|
||||||
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
@@ -78,9 +80,7 @@
|
|||||||
#include <readline/readline.h>
|
#include <readline/readline.h>
|
||||||
#include <readline/history.h>
|
#include <readline/history.h>
|
||||||
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
||||||
#define lua_saveline(L,idx) \
|
#define lua_saveline(L,line) ((void)L, add_history(line))
|
||||||
if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \
|
|
||||||
add_history(lua_tostring(L, idx)); /* add it to history */
|
|
||||||
#define lua_freeline(L,b) ((void)L, free(b))
|
#define lua_freeline(L,b) ((void)L, free(b))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
@@ -88,7 +88,7 @@
|
|||||||
#define lua_readline(L,b,p) \
|
#define lua_readline(L,b,p) \
|
||||||
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
||||||
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
||||||
#define lua_saveline(L,idx) { (void)L; (void)idx; }
|
#define lua_saveline(L,line) { (void)L; (void)line; }
|
||||||
#define lua_freeline(L,b) { (void)L; (void)b; }
|
#define lua_freeline(L,b) { (void)L; (void)b; }
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
@@ -126,12 +126,12 @@ static void laction (int i) {
|
|||||||
|
|
||||||
|
|
||||||
static void print_usage (const char *badoption) {
|
static void print_usage (const char *badoption) {
|
||||||
luai_writestringerror("%s: ", progname);
|
lua_writestringerror("%s: ", progname);
|
||||||
if (badoption[1] == 'e' || badoption[1] == 'l')
|
if (badoption[1] == 'e' || badoption[1] == 'l')
|
||||||
luai_writestringerror("'%s' needs argument\n", badoption);
|
lua_writestringerror("'%s' needs argument\n", badoption);
|
||||||
else
|
else
|
||||||
luai_writestringerror("unrecognized option '%s'\n", badoption);
|
lua_writestringerror("unrecognized option '%s'\n", badoption);
|
||||||
luai_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"
|
||||||
@@ -151,8 +151,8 @@ static void print_usage (const char *badoption) {
|
|||||||
** (if present)
|
** (if present)
|
||||||
*/
|
*/
|
||||||
static void l_message (const char *pname, const char *msg) {
|
static void l_message (const char *pname, const char *msg) {
|
||||||
if (pname) luai_writestringerror("%s: ", pname);
|
if (pname) lua_writestringerror("%s: ", pname);
|
||||||
luai_writestringerror("%s\n", msg);
|
lua_writestringerror("%s\n", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -208,8 +208,8 @@ static int docall (lua_State *L, int narg, int nres) {
|
|||||||
|
|
||||||
|
|
||||||
static void print_version (void) {
|
static void print_version (void) {
|
||||||
luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
||||||
luai_writeline();
|
lua_writeline();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -313,31 +313,31 @@ static int pushline (lua_State *L, int firstline) {
|
|||||||
lua_pop(L, 1); /* remove prompt */
|
lua_pop(L, 1); /* remove prompt */
|
||||||
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-1] = '\0'; /* remove it */
|
b[--l] = '\0'; /* remove it */
|
||||||
if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
|
if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
|
||||||
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
||||||
else
|
else
|
||||||
lua_pushstring(L, b);
|
lua_pushlstring(L, b, l);
|
||||||
lua_freeline(L, b);
|
lua_freeline(L, b);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Try to compile line on the stack as 'return <line>'; on return, stack
|
** Try to compile line on the stack as 'return <line>;'; on return, stack
|
||||||
** has either compiled chunk or original line (if compilation failed).
|
** has either compiled chunk or original line (if compilation failed).
|
||||||
*/
|
*/
|
||||||
static int addreturn (lua_State *L) {
|
static int addreturn (lua_State *L) {
|
||||||
int status;
|
const char *line = lua_tostring(L, -1); /* original line */
|
||||||
size_t len; const char *line;
|
const char *retline = lua_pushfstring(L, "return %s;", line);
|
||||||
lua_pushliteral(L, "return ");
|
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
|
||||||
lua_pushvalue(L, -2); /* duplicate line */
|
if (status == LUA_OK) {
|
||||||
lua_concat(L, 2); /* new line is "return ..." */
|
lua_remove(L, -2); /* remove modified line */
|
||||||
line = lua_tolstring(L, -1, &len);
|
if (line[0] != '\0') /* non empty? */
|
||||||
if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK)
|
lua_saveline(L, line); /* keep history */
|
||||||
lua_remove(L, -3); /* remove original line */
|
}
|
||||||
else
|
else
|
||||||
lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */
|
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -350,8 +350,10 @@ static int multiline (lua_State *L) {
|
|||||||
size_t len;
|
size_t len;
|
||||||
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
||||||
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 */
|
||||||
return status; /* cannot or should not try to add continuation line */
|
return status; /* cannot or should not try to add continuation 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 */
|
||||||
@@ -372,7 +374,6 @@ static int loadline (lua_State *L) {
|
|||||||
return -1; /* no input */
|
return -1; /* no input */
|
||||||
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
||||||
status = multiline(L); /* try as command, maybe with continuation lines */
|
status = multiline(L); /* try as command, maybe with continuation lines */
|
||||||
lua_saveline(L, 1); /* keep history */
|
|
||||||
lua_remove(L, 1); /* remove line from the stack */
|
lua_remove(L, 1); /* remove line from the stack */
|
||||||
lua_assert(lua_gettop(L) == 1);
|
lua_assert(lua_gettop(L) == 1);
|
||||||
return status;
|
return status;
|
||||||
@@ -410,7 +411,7 @@ static void doREPL (lua_State *L) {
|
|||||||
else report(L, status);
|
else report(L, status);
|
||||||
}
|
}
|
||||||
lua_settop(L, 0); /* clear stack */
|
lua_settop(L, 0); /* clear stack */
|
||||||
luai_writeline();
|
lua_writeline();
|
||||||
progname = oldprogname;
|
progname = oldprogname;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,8 +421,7 @@ static void doREPL (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
static int pushargs (lua_State *L) {
|
static int pushargs (lua_State *L) {
|
||||||
int i, n;
|
int i, n;
|
||||||
lua_getglobal(L, "arg");
|
if (lua_getglobal(L, "arg") != LUA_TTABLE)
|
||||||
if (!lua_istable(L, -1))
|
|
||||||
luaL_error(L, "'arg' is not a table");
|
luaL_error(L, "'arg' is not a table");
|
||||||
n = (int)luaL_len(L, -1);
|
n = (int)luaL_len(L, -1);
|
||||||
luaL_checkstack(L, n + 3, "too many arguments to script");
|
luaL_checkstack(L, n + 3, "too many arguments to script");
|
||||||
@@ -481,14 +481,14 @@ static int collectargs (char **argv, int *first) {
|
|||||||
args |= has_E;
|
args |= has_E;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
args |= has_i; /* goes through (-i implies -v) */
|
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 after 1st? */
|
||||||
return has_error; /* invalid option */
|
return has_error; /* invalid option */
|
||||||
args |= has_v;
|
args |= has_v;
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
args |= has_e; /* go through */
|
args |= has_e; /* FALLTHROUGH */
|
||||||
case 'l': /* both options need an argument */
|
case 'l': /* both options need an argument */
|
||||||
if (argv[i][2] == '\0') { /* no concatenated argument? */
|
if (argv[i][2] == '\0') { /* no concatenated argument? */
|
||||||
i++; /* try next 'argv' */
|
i++; /* try next 'argv' */
|
||||||
@@ -512,17 +512,16 @@ static int collectargs (char **argv, int *first) {
|
|||||||
static int runargs (lua_State *L, char **argv, int n) {
|
static int runargs (lua_State *L, char **argv, int n) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i < n; i++) {
|
for (i = 1; i < n; i++) {
|
||||||
int status;
|
|
||||||
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') {
|
if (option == 'e' || option == 'l') {
|
||||||
|
int status;
|
||||||
const char *extra = argv[i] + 2; /* both options need an argument */
|
const char *extra = argv[i] + 2; /* both options need an argument */
|
||||||
if (*extra == '\0') extra = argv[++i];
|
if (*extra == '\0') extra = argv[++i];
|
||||||
lua_assert(extra != NULL);
|
lua_assert(extra != NULL);
|
||||||
if (option == 'e')
|
status = (option == 'e')
|
||||||
status = dostring(L, extra, "=(command line)");
|
? dostring(L, extra, "=(command line)")
|
||||||
else
|
: dolibrary(L, extra);
|
||||||
status = dolibrary(L, extra);
|
|
||||||
if (status != LUA_OK) return 0;
|
if (status != LUA_OK) return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
lua.h
45
lua.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lua.h,v 1.318 2014/10/15 14:27:40 roberto Exp roberto $
|
** $Id: lua.h,v 1.330 2016/01/13 17:55:19 roberto Exp roberto $
|
||||||
** Lua - A Scripting Language
|
** Lua - A Scripting Language
|
||||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||||
** See Copyright Notice at the end of this file
|
** See Copyright Notice at the end of this file
|
||||||
@@ -19,25 +19,27 @@
|
|||||||
#define LUA_VERSION_MAJOR "5"
|
#define LUA_VERSION_MAJOR "5"
|
||||||
#define LUA_VERSION_MINOR "3"
|
#define LUA_VERSION_MINOR "3"
|
||||||
#define LUA_VERSION_NUM 503
|
#define LUA_VERSION_NUM 503
|
||||||
#define LUA_VERSION_RELEASE "beta"
|
#define LUA_VERSION_RELEASE "3"
|
||||||
|
|
||||||
#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-2014 Lua.org, PUC-Rio"
|
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2016 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"
|
||||||
|
|
||||||
|
|
||||||
/* mark for precompiled code ('<esc>Lua') */
|
/* mark for precompiled code ('<esc>Lua') */
|
||||||
#define LUA_SIGNATURE "\033Lua"
|
#define LUA_SIGNATURE "\x1bLua"
|
||||||
|
|
||||||
/* option for multiple returns in 'lua_pcall' and 'lua_call' */
|
/* option for multiple returns in 'lua_pcall' and 'lua_call' */
|
||||||
#define LUA_MULTRET (-1)
|
#define LUA_MULTRET (-1)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** pseudo-indices
|
** Pseudo-indices
|
||||||
|
** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty
|
||||||
|
** space after that to help overflow detection)
|
||||||
*/
|
*/
|
||||||
#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX
|
#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000)
|
||||||
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
|
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
|
||||||
|
|
||||||
|
|
||||||
@@ -159,7 +161,7 @@ LUA_API void (lua_settop) (lua_State *L, int idx);
|
|||||||
LUA_API void (lua_pushvalue) (lua_State *L, int idx);
|
LUA_API void (lua_pushvalue) (lua_State *L, int idx);
|
||||||
LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
|
LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
|
||||||
LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
|
LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
|
||||||
LUA_API int (lua_checkstack) (lua_State *L, int sz);
|
LUA_API int (lua_checkstack) (lua_State *L, int n);
|
||||||
|
|
||||||
LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
|
LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
|
||||||
|
|
||||||
@@ -222,7 +224,7 @@ LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
|
|||||||
LUA_API void (lua_pushnil) (lua_State *L);
|
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_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
||||||
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l);
|
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_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
|
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
|
||||||
va_list argp);
|
va_list argp);
|
||||||
@@ -236,7 +238,7 @@ LUA_API int (lua_pushthread) (lua_State *L);
|
|||||||
/*
|
/*
|
||||||
** get functions (Lua -> stack)
|
** get functions (Lua -> stack)
|
||||||
*/
|
*/
|
||||||
LUA_API int (lua_getglobal) (lua_State *L, const char *var);
|
LUA_API int (lua_getglobal) (lua_State *L, const char *name);
|
||||||
LUA_API int (lua_gettable) (lua_State *L, int idx);
|
LUA_API int (lua_gettable) (lua_State *L, int idx);
|
||||||
LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
|
LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
|
||||||
LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
|
LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
|
||||||
@@ -253,7 +255,7 @@ LUA_API int (lua_getuservalue) (lua_State *L, int idx);
|
|||||||
/*
|
/*
|
||||||
** set functions (stack -> Lua)
|
** set functions (stack -> Lua)
|
||||||
*/
|
*/
|
||||||
LUA_API void (lua_setglobal) (lua_State *L, const char *var);
|
LUA_API void (lua_setglobal) (lua_State *L, const char *name);
|
||||||
LUA_API void (lua_settable) (lua_State *L, int idx);
|
LUA_API void (lua_settable) (lua_State *L, int idx);
|
||||||
LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
|
LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
|
||||||
LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
|
LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
|
||||||
@@ -276,8 +278,7 @@ LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
|
|||||||
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
|
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
|
||||||
|
|
||||||
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
|
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
|
||||||
const char *chunkname,
|
const char *chunkname, const char *mode);
|
||||||
const char *mode);
|
|
||||||
|
|
||||||
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
||||||
|
|
||||||
@@ -285,12 +286,13 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
|||||||
/*
|
/*
|
||||||
** coroutine functions
|
** coroutine functions
|
||||||
*/
|
*/
|
||||||
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
|
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
|
||||||
lua_KFunction k);
|
lua_KFunction k);
|
||||||
|
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
|
||||||
|
LUA_API int (lua_status) (lua_State *L);
|
||||||
|
LUA_API int (lua_isyieldable) (lua_State *L);
|
||||||
|
|
||||||
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
|
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
|
||||||
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
|
|
||||||
LUA_API int (lua_status) (lua_State *L);
|
|
||||||
LUA_API int lua_isyieldable (lua_State *L);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -356,11 +358,10 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
|||||||
#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
|
#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
|
||||||
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
|
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
|
||||||
|
|
||||||
#define lua_pushliteral(L, s) \
|
#define lua_pushliteral(L, s) lua_pushstring(L, "" s)
|
||||||
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
|
|
||||||
|
|
||||||
#define lua_pushglobaltable(L) \
|
#define lua_pushglobaltable(L) \
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
|
((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS))
|
||||||
|
|
||||||
#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
|
#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
|
||||||
|
|
||||||
@@ -382,7 +383,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
|||||||
#if defined(LUA_COMPAT_APIINTCASTS)
|
#if defined(LUA_COMPAT_APIINTCASTS)
|
||||||
|
|
||||||
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
||||||
#define lua_tounsignedx(L,i,is) ((lua_Integer)lua_tointegerx(L,i,is))
|
#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is))
|
||||||
#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
|
#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -459,7 +460,7 @@ struct lua_Debug {
|
|||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Copyright (C) 1994-2014 Lua.org, PUC-Rio.
|
* Copyright (C) 1994-2016 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
|
||||||
|
|||||||
684
luaconf.h
684
luaconf.h
@@ -1,130 +1,174 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: luaconf.h,v 1.219 2014/10/20 16:32:05 roberto Exp roberto $
|
** $Id: luaconf.h,v 1.254 2015/10/21 18:17:40 roberto Exp roberto $
|
||||||
** Configuration file for Lua
|
** Configuration file for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef lconfig_h
|
#ifndef luaconf_h
|
||||||
#define lconfig_h
|
#define luaconf_h
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** ==================================================================
|
** ===================================================================
|
||||||
** Search for "@@" to find all configurable definitions.
|
** Search for "@@" to find all configurable definitions.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {====================================================================
|
||||||
@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines type for
|
** System Configuration: macros to adapt (if needed) Lua to some
|
||||||
@@ Lua integers; you must define one of them.
|
** particular platform, for instance compiling it with 32-bit numbers or
|
||||||
@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines
|
** restricting it to C89.
|
||||||
@@ type for Lua floats. You must define one of them.
|
|
||||||
**
|
|
||||||
** These definitions set the numeric types for Lua. Lua should work fine
|
|
||||||
** with any mix of these previous options. The usual configurations
|
|
||||||
** are 64-bit integers and floats (the default) and 32-bit integers and
|
|
||||||
** floats (Small Lua, for restricted platforms).
|
|
||||||
**
|
|
||||||
** Note that C compilers not compliant with C99 may not have
|
|
||||||
** support for 'long long'. In that case, you should not use option
|
|
||||||
** 'LUA_INT_LONGLONG'; use instead option 'LUA_32BITS' for Small Lua
|
|
||||||
** (see below), or LUA_INT_LONG plus LUA_REAL_DOUBLE for an interpreter
|
|
||||||
** with 32-bit integers and double floating-point numbers.
|
|
||||||
** =====================================================================
|
** =====================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Just uncomment the next line for Small Lua; you can also define
|
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You
|
||||||
** LUA_32BITS in the make file, but changing here you ensure that
|
** can also define LUA_32BITS in the make file, but changing here you
|
||||||
** all software connected to Lua will be compiled with the same
|
** ensure that all software connected to Lua will be compiled with the
|
||||||
** configuration.
|
** same configuration.
|
||||||
*/
|
*/
|
||||||
/* #define LUA_32BITS */
|
/* #define LUA_32BITS */
|
||||||
|
|
||||||
#if !defined(LUA_32BITS) && !defined(LUA_ANSI)
|
|
||||||
#define LUA_INT_LONGLONG
|
|
||||||
#define LUA_REAL_DOUBLE
|
|
||||||
#else /* Lua 32 bits */
|
|
||||||
#define LUA_INT_LONG
|
|
||||||
#define LUA_REAL_FLOAT
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/*
|
||||||
|
@@ 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
|
||||||
|
** or Windows-specific features on Windows.
|
||||||
|
*/
|
||||||
|
/* #define LUA_USE_C89 */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_ANSI controls the use of non-ansi features.
|
** By default, Lua on Windows use (some) specific Windows features
|
||||||
** CHANGE it (define it) if you want Lua to avoid the use of any
|
|
||||||
** non-ansi feature or library.
|
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_ANSI) && defined(__STRICT_ANSI__)
|
#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE)
|
||||||
#define LUA_ANSI
|
#define LUA_USE_WINDOWS /* enable goodies for regular Windows */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE)
|
#if defined(LUA_USE_WINDOWS)
|
||||||
#define LUA_WIN /* enable goodies for regular Windows platforms */
|
#define LUA_DL_DLL /* enable support for DLL */
|
||||||
|
#define LUA_USE_C89 /* broadly, Windows is C89 */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(LUA_WIN)
|
|
||||||
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ANSI C functions */
|
|
||||||
#define LUA_DL_DLL
|
|
||||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_LINUX)
|
#if defined(LUA_USE_LINUX)
|
||||||
#define LUA_USE_C99
|
|
||||||
#define LUA_USE_POSIX
|
#define LUA_USE_POSIX
|
||||||
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
||||||
#define LUA_USE_READLINE /* needs some extra libraries */
|
#define LUA_USE_READLINE /* needs some extra libraries */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_MACOSX)
|
#if defined(LUA_USE_MACOSX)
|
||||||
#define LUA_USE_C99
|
|
||||||
#define LUA_USE_POSIX
|
#define LUA_USE_POSIX
|
||||||
#define LUA_USE_DLOPEN /* does not need -ldl */
|
#define LUA_USE_DLOPEN /* MacOS does not need -ldl */
|
||||||
#define LUA_USE_READLINE /* needs an extra library: -lreadline */
|
#define LUA_USE_READLINE /* needs an extra library: -lreadline */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_USE_C99 includes all functionality that depends on C 99.
|
@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for
|
||||||
** CHANGE it (define it) if your system is compatible.
|
** C89 ('long' and 'double'); Windows always has '__int64', so it does
|
||||||
|
** not need to use this case.
|
||||||
*/
|
*/
|
||||||
#if defined(LUA_USE_C99)
|
#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS)
|
||||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
#define LUA_C89_NUMBERS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'.
|
||||||
|
*/
|
||||||
|
/* avoid undefined shifts */
|
||||||
|
#if ((INT_MAX >> 15) >> 15) >= 1
|
||||||
|
#define LUAI_BITSINT 32
|
||||||
|
#else
|
||||||
|
/* 'int' always must have at least 16 bits */
|
||||||
|
#define LUAI_BITSINT 16
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_USE_POSIX includes all functionality listed as X/Open System
|
@@ LUA_INT_TYPE defines the type for Lua integers.
|
||||||
@@ Interfaces Extension (XSI).
|
@@ LUA_FLOAT_TYPE defines the type for Lua floats.
|
||||||
** CHANGE it (define it) if your system is XSI compatible.
|
** Lua should work fine with any mix of these options (if supported
|
||||||
|
** by your C compiler). The usual configurations are 64-bit integers
|
||||||
|
** and 'double' (the default), 32-bit integers and 'float' (for
|
||||||
|
** restricted platforms), and 'long'/'double' (for C compilers not
|
||||||
|
** compliant with C99, which may not have support for 'long long').
|
||||||
*/
|
*/
|
||||||
#if defined(LUA_USE_POSIX)
|
|
||||||
|
/* predefined options for LUA_INT_TYPE */
|
||||||
|
#define LUA_INT_INT 1
|
||||||
|
#define LUA_INT_LONG 2
|
||||||
|
#define LUA_INT_LONGLONG 3
|
||||||
|
|
||||||
|
/* predefined options for LUA_FLOAT_TYPE */
|
||||||
|
#define LUA_FLOAT_FLOAT 1
|
||||||
|
#define LUA_FLOAT_DOUBLE 2
|
||||||
|
#define LUA_FLOAT_LONGDOUBLE 3
|
||||||
|
|
||||||
|
#if defined(LUA_32BITS) /* { */
|
||||||
|
/*
|
||||||
|
** 32-bit integers and 'float'
|
||||||
|
*/
|
||||||
|
#if LUAI_BITSINT >= 32 /* use 'int' if big enough */
|
||||||
|
#define LUA_INT_TYPE LUA_INT_INT
|
||||||
|
#else /* otherwise use 'long' */
|
||||||
|
#define LUA_INT_TYPE LUA_INT_LONG
|
||||||
|
#endif
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT
|
||||||
|
|
||||||
|
#elif defined(LUA_C89_NUMBERS) /* }{ */
|
||||||
|
/*
|
||||||
|
** largest types available for C89 ('long' and 'double')
|
||||||
|
*/
|
||||||
|
#define LUA_INT_TYPE LUA_INT_LONG
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** default configuration for 64-bit Lua ('long long' and 'double')
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_INT_TYPE)
|
||||||
|
#define LUA_INT_TYPE LUA_INT_LONGLONG
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(LUA_FLOAT_TYPE)
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Configuration for Paths.
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
|
@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
|
||||||
@@ Lua libraries.
|
** Lua libraries.
|
||||||
@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
|
@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
|
||||||
@@ C libraries.
|
** C libraries.
|
||||||
** CHANGE them if your machine has a non-conventional directory
|
** CHANGE them if your machine has a non-conventional directory
|
||||||
** hierarchy or if you want to install your libraries in
|
** hierarchy or if you want to install your libraries in
|
||||||
** non-conventional directories.
|
** non-conventional directories.
|
||||||
*/
|
*/
|
||||||
#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||||
#if defined(_WIN32) /* { */
|
#if defined(_WIN32) /* { */
|
||||||
/*
|
/*
|
||||||
** In Windows, any exclamation mark ('!') in the path is replaced by the
|
** In Windows, any exclamation mark ('!') in the path is replaced by the
|
||||||
** path of the directory of the executable file of the current process.
|
** path of the directory of the executable file of the current process.
|
||||||
@@ -135,7 +179,7 @@
|
|||||||
#define LUA_PATH_DEFAULT \
|
#define LUA_PATH_DEFAULT \
|
||||||
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
||||||
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
|
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
|
||||||
LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
|
LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
|
||||||
".\\?.lua;" ".\\?\\init.lua"
|
".\\?.lua;" ".\\?\\init.lua"
|
||||||
#define LUA_CPATH_DEFAULT \
|
#define LUA_CPATH_DEFAULT \
|
||||||
LUA_CDIR"?.dll;" \
|
LUA_CDIR"?.dll;" \
|
||||||
@@ -167,14 +211,14 @@
|
|||||||
#define LUA_DIRSEP "/"
|
#define LUA_DIRSEP "/"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_ENV is the name of the variable that holds the current
|
** {==================================================================
|
||||||
@@ environment, used to access global names.
|
** Marks for exported symbols in the C code
|
||||||
** CHANGE it if you do not like this name.
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
#define LUA_ENV "_ENV"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_API is a mark for all core API functions.
|
@@ LUA_API is a mark for all core API functions.
|
||||||
@@ -207,10 +251,10 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUAI_FUNC is a mark for all extern functions that are not to be
|
@@ LUAI_FUNC is a mark for all extern functions that are not to be
|
||||||
@@ exported to outside modules.
|
** exported to outside modules.
|
||||||
@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables
|
@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables
|
||||||
@@ that are not to be exported to outside modules (LUAI_DDEF for
|
** that are not to be exported to outside modules (LUAI_DDEF for
|
||||||
@@ definitions and LUAI_DDEC for declarations).
|
** definitions and LUAI_DDEC for declarations).
|
||||||
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
||||||
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
||||||
** when Lua is compiled as a shared library. Not all elf targets support
|
** when Lua is compiled as a shared library. Not all elf targets support
|
||||||
@@ -222,82 +266,14 @@
|
|||||||
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||||
defined(__ELF__) /* { */
|
defined(__ELF__) /* { */
|
||||||
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
||||||
|
#else /* }{ */
|
||||||
|
#define LUAI_FUNC extern
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
#define LUAI_DDEC LUAI_FUNC
|
#define LUAI_DDEC LUAI_FUNC
|
||||||
#define LUAI_DDEF /* empty */
|
#define LUAI_DDEF /* empty */
|
||||||
|
|
||||||
#else /* }{ */
|
/* }================================================================== */
|
||||||
#define LUAI_FUNC extern
|
|
||||||
#define LUAI_DDEC extern
|
|
||||||
#define LUAI_DDEF /* empty */
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
|
|
||||||
** a Lua state with very fast access.
|
|
||||||
** CHANGE it if you need a different size.
|
|
||||||
*/
|
|
||||||
#define LUA_EXTRASPACE (sizeof(void *))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_QL describes how error messages quote program elements.
|
|
||||||
** Lua does not use these macros anymore; they are here for
|
|
||||||
** compatibility only.
|
|
||||||
*/
|
|
||||||
#define LUA_QL(x) "'" x "'"
|
|
||||||
#define LUA_QS LUA_QL("%s")
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
|
||||||
@@ of a function in debug information.
|
|
||||||
** CHANGE it if you want a different size.
|
|
||||||
*/
|
|
||||||
#define LUA_IDSIZE 60
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ luai_writestring/luai_writeline define how 'print' prints its results.
|
|
||||||
** They are only used in libraries and the stand-alone program.
|
|
||||||
*/
|
|
||||||
#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
|
||||||
#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout))
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ luai_writestringerror defines how to print error messages.
|
|
||||||
** (A format string with one argument is enough for Lua...)
|
|
||||||
*/
|
|
||||||
#define luai_writestringerror(s,p) \
|
|
||||||
(fprintf(stderr, (s), (p)), fflush(stderr))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is,
|
|
||||||
** strings that are internalized. (Cannot be smaller than reserved words
|
|
||||||
** or tags for metamethods, as these strings must be internalized;
|
|
||||||
** #("function") = 8, #("__newindex") = 10.)
|
|
||||||
*/
|
|
||||||
#define LUAI_MAXSHORTLEN 40
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation
|
|
||||||
@@ functions. It must be a numerical type; Lua will use 'intptr_t' if
|
|
||||||
@@ available.
|
|
||||||
*/
|
|
||||||
#if defined (LUA_USE_C99)
|
|
||||||
#include <stdint.h>
|
|
||||||
#if defined (INTPTR_MAX) /* even in C99 this type is optional */
|
|
||||||
#define LUA_KCONTEXT intptr_t
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(LUA_KCONTEXT)
|
|
||||||
/* default definition (the nearest thing to 'intptr_t' in C89) */
|
|
||||||
#define LUA_KCONTEXT ptrdiff_t
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -337,20 +313,15 @@
|
|||||||
*/
|
*/
|
||||||
#define LUA_COMPAT_APIINTCASTS
|
#define LUA_COMPAT_APIINTCASTS
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a
|
|
||||||
@@ a float mark ('.0').
|
|
||||||
** This macro is not on by default even in compatibility mode,
|
|
||||||
** because this is not really an incompatibility.
|
|
||||||
*/
|
|
||||||
/* #define LUA_COMPAT_FLOATSTRING */
|
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_COMPAT_5_1) /* { */
|
#if defined(LUA_COMPAT_5_1) /* { */
|
||||||
|
|
||||||
|
/* Incompatibilities from 5.2 -> 5.3 */
|
||||||
|
#define LUA_COMPAT_MATHLIB
|
||||||
|
#define LUA_COMPAT_APIINTCASTS
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
|
@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
|
||||||
** You can replace it with 'table.unpack'.
|
** You can replace it with 'table.unpack'.
|
||||||
@@ -410,100 +381,70 @@
|
|||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a
|
||||||
|
@@ a float mark ('.0').
|
||||||
|
** This macro is not on by default even in compatibility mode,
|
||||||
|
** because this is not really an incompatibility.
|
||||||
|
*/
|
||||||
|
/* #define LUA_COMPAT_FLOATSTRING */
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAI_BITSINT defines the number of bits in an int.
|
|
||||||
** CHANGE here if Lua cannot automatically detect the number of bits of
|
|
||||||
** your machine. Probably you do not need to change this.
|
|
||||||
*/
|
|
||||||
/* avoid overflows in comparison */
|
|
||||||
#if INT_MAX-20 < 32760 /* { */
|
|
||||||
#define LUAI_BITSINT 16
|
|
||||||
#elif INT_MAX > 2147483640L /* }{ */
|
|
||||||
/* int has at least 32 bits */
|
|
||||||
#define LUAI_BITSINT 32
|
|
||||||
#else /* }{ */
|
|
||||||
#error "you must define LUA_BITSINT with number of bits in an integer"
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_INT32 is an signed integer with exactly 32 bits.
|
|
||||||
@@ LUAI_UMEM is an unsigned integer big enough to count the total
|
|
||||||
@@ memory used by Lua.
|
|
||||||
@@ LUAI_MEM is a signed integer big enough to count the total memory
|
|
||||||
@@ used by Lua.
|
|
||||||
** CHANGE here if for some weird reason the default definitions are not
|
|
||||||
** good enough for your machine. Probably you do not need to change
|
|
||||||
** this.
|
|
||||||
*/
|
|
||||||
#if LUAI_BITSINT >= 32 /* { */
|
|
||||||
#define LUA_INT32 int
|
|
||||||
#define LUAI_UMEM size_t
|
|
||||||
#define LUAI_MEM ptrdiff_t
|
|
||||||
#else /* }{ */
|
|
||||||
/* 16-bit ints */
|
|
||||||
#define LUA_INT32 long
|
|
||||||
#define LUAI_UMEM unsigned long
|
|
||||||
#define LUAI_MEM long
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAI_MAXSTACK limits the size of the Lua stack.
|
|
||||||
** CHANGE it if you need a different limit. This limit is arbitrary;
|
|
||||||
** its only purpose is to stop Lua from consuming unlimited stack
|
|
||||||
** space (and to reserve some numbers for pseudo-indices).
|
|
||||||
*/
|
|
||||||
#if LUAI_BITSINT >= 32
|
|
||||||
#define LUAI_MAXSTACK 1000000
|
|
||||||
#else
|
|
||||||
#define LUAI_MAXSTACK 15000
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* reserve some space for error handling */
|
|
||||||
#define LUAI_FIRSTPSEUDOIDX (-LUAI_MAXSTACK - 1000)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
|
|
||||||
** CHANGE it if it uses too much C-stack space.
|
|
||||||
*/
|
|
||||||
#define LUAL_BUFFERSIZE BUFSIZ
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Configuration for Numbers.
|
** Configuration for Numbers.
|
||||||
** Change these definitions if no predefined LUA_REAL_* / 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.
|
@@ LUA_NUMBER is the floating-point type used by Lua.
|
||||||
**
|
|
||||||
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
|
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
|
||||||
@@ over a floating number.
|
@@ over a floating number.
|
||||||
**
|
@@ l_mathlim(x) corrects limit name 'x' to the proper float type
|
||||||
|
** by prefixing it with one of FLT/DBL/LDBL.
|
||||||
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
@@ LUA_NUMBER_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.
|
||||||
@@ lua_number2str converts a float to a string.
|
@@ lua_number2str converts a float to a string.
|
||||||
**
|
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
|
||||||
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations
|
@@ l_floor takes the floor of a float.
|
||||||
**
|
|
||||||
@@ lua_str2number converts a decimal numeric string to a number.
|
@@ lua_str2number converts a decimal numeric string to a number.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(LUA_REAL_FLOAT) /* { single float */
|
|
||||||
|
/* The following definitions are good for most cases here */
|
||||||
|
|
||||||
|
#define l_floor(x) (l_mathop(floor)(x))
|
||||||
|
|
||||||
|
#define lua_number2str(s,sz,n) l_sprintf((s), sz, LUA_NUMBER_FMT, (n))
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ lua_numbertointeger converts a float number to an integer, or
|
||||||
|
** returns 0 if float is not within the range of a lua_Integer.
|
||||||
|
** (The range comparisons are tricky because of rounding. The tests
|
||||||
|
** here assume a two-complement representation, where MININTEGER always
|
||||||
|
** has an exact representation as a float; MAXINTEGER may not have one,
|
||||||
|
** and therefore its conversion to float may have an ill-defined value.)
|
||||||
|
*/
|
||||||
|
#define lua_numbertointeger(n,p) \
|
||||||
|
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
|
||||||
|
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
||||||
|
(*(p) = (LUA_INTEGER)(n), 1))
|
||||||
|
|
||||||
|
|
||||||
|
/* now the variable definitions */
|
||||||
|
|
||||||
|
#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */
|
||||||
|
|
||||||
#define LUA_NUMBER float
|
#define LUA_NUMBER float
|
||||||
|
|
||||||
|
#define l_mathlim(n) (FLT_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN ""
|
#define LUA_NUMBER_FRMLEN ""
|
||||||
@@ -514,10 +455,12 @@
|
|||||||
#define lua_str2number(s,p) strtof((s), (p))
|
#define lua_str2number(s,p) strtof((s), (p))
|
||||||
|
|
||||||
|
|
||||||
#elif defined(LUA_REAL_LONGDOUBLE) /* }{ long double */
|
#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */
|
||||||
|
|
||||||
#define LUA_NUMBER long double
|
#define LUA_NUMBER long double
|
||||||
|
|
||||||
|
#define l_mathlim(n) (LDBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER long double
|
#define LUAI_UACNUMBER long double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN "L"
|
#define LUA_NUMBER_FRMLEN "L"
|
||||||
@@ -527,10 +470,12 @@
|
|||||||
|
|
||||||
#define lua_str2number(s,p) strtold((s), (p))
|
#define lua_str2number(s,p) strtold((s), (p))
|
||||||
|
|
||||||
#elif defined(LUA_REAL_DOUBLE) /* }{ double */
|
#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */
|
||||||
|
|
||||||
#define LUA_NUMBER double
|
#define LUA_NUMBER double
|
||||||
|
|
||||||
|
#define l_mathlim(n) (DBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN ""
|
#define LUA_NUMBER_FRMLEN ""
|
||||||
@@ -540,75 +485,13 @@
|
|||||||
|
|
||||||
#define lua_str2number(s,p) strtod((s), (p))
|
#define lua_str2number(s,p) strtod((s), (p))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
#error "numeric real type not defined"
|
#error "numeric float type not defined"
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_USE_C99)
|
|
||||||
/* 'strtof' and 'opf' variants for math functions are C99 */
|
|
||||||
#undef l_mathop
|
|
||||||
#undef lua_str2number
|
|
||||||
#define l_mathop(op) (lua_Number)op
|
|
||||||
#define lua_str2number(s,p) ((lua_Number)strtod((s), (p)))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define l_floor(x) (l_mathop(floor)(x))
|
|
||||||
|
|
||||||
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ lua_numtointeger converts a float number to an integer, or
|
|
||||||
** returns 0 if float is not within the range of a lua_Integer.
|
|
||||||
** (The range comparisons are tricky because of rounding. The tests
|
|
||||||
** here assume a two-complement representation, where MININTEGER always
|
|
||||||
** has an exact representation as a float; MAXINTEGER may not have one,
|
|
||||||
** and therefore its conversion to float may have an ill-defined value.)
|
|
||||||
*/
|
|
||||||
#define lua_numtointeger(n,p) \
|
|
||||||
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
|
|
||||||
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
|
||||||
(*(p) = (LUA_INTEGER)(n), 1))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ The luai_num* macros define the primitive operations over numbers.
|
|
||||||
** They should work for any size of floating numbers.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* the following operations need the math library */
|
|
||||||
#if defined(lobject_c) || defined(lvm_c)
|
|
||||||
#include <math.h>
|
|
||||||
#define luai_nummod(L,a,b,m) \
|
|
||||||
{ (m) = l_mathop(fmod)(a,b); if ((m) != 0 && (a)*(b) < 0) (m) += (b); }
|
|
||||||
#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* these are quite standard operations */
|
|
||||||
#if defined(LUA_CORE)
|
|
||||||
#define luai_numadd(L,a,b) ((a)+(b))
|
|
||||||
#define luai_numsub(L,a,b) ((a)-(b))
|
|
||||||
#define luai_nummul(L,a,b) ((a)*(b))
|
|
||||||
#define luai_numdiv(L,a,b) ((a)/(b))
|
|
||||||
#define luai_numunm(L,a) (-(a))
|
|
||||||
#define luai_numeq(a,b) ((a)==(b))
|
|
||||||
#define luai_numlt(a,b) ((a)<(b))
|
|
||||||
#define luai_numle(a,b) ((a)<=(b))
|
|
||||||
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** The following macro checks whether an operation is not safe to be
|
|
||||||
** performed by the constant folder. It should result in zero only if
|
|
||||||
** the operation is safe.
|
|
||||||
*/
|
|
||||||
#define luai_numinvalidop(op,a,b) 0
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_INTEGER is the integer type used by Lua.
|
@@ LUA_INTEGER is the integer type used by Lua.
|
||||||
@@ -628,7 +511,7 @@
|
|||||||
/* The following definitions are good for most cases here */
|
/* The following definitions are good for most cases here */
|
||||||
|
|
||||||
#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
|
#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
|
||||||
#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
|
#define lua_integer2str(s,sz,n) l_sprintf((s), sz, LUA_INTEGER_FMT, (n))
|
||||||
|
|
||||||
#define LUAI_UACINT LUA_INTEGER
|
#define LUAI_UACINT LUA_INTEGER
|
||||||
|
|
||||||
@@ -641,7 +524,7 @@
|
|||||||
|
|
||||||
/* now the variable definitions */
|
/* now the variable definitions */
|
||||||
|
|
||||||
#if defined(LUA_INT_INT) /* { int */
|
#if LUA_INT_TYPE == LUA_INT_INT /* { int */
|
||||||
|
|
||||||
#define LUA_INTEGER int
|
#define LUA_INTEGER int
|
||||||
#define LUA_INTEGER_FRMLEN ""
|
#define LUA_INTEGER_FRMLEN ""
|
||||||
@@ -649,7 +532,7 @@
|
|||||||
#define LUA_MAXINTEGER INT_MAX
|
#define LUA_MAXINTEGER INT_MAX
|
||||||
#define LUA_MININTEGER INT_MIN
|
#define LUA_MININTEGER INT_MIN
|
||||||
|
|
||||||
#elif defined(LUA_INT_LONG) /* }{ long */
|
#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */
|
||||||
|
|
||||||
#define LUA_INTEGER long
|
#define LUA_INTEGER long
|
||||||
#define LUA_INTEGER_FRMLEN "l"
|
#define LUA_INTEGER_FRMLEN "l"
|
||||||
@@ -657,21 +540,11 @@
|
|||||||
#define LUA_MAXINTEGER LONG_MAX
|
#define LUA_MAXINTEGER LONG_MAX
|
||||||
#define LUA_MININTEGER LONG_MIN
|
#define LUA_MININTEGER LONG_MIN
|
||||||
|
|
||||||
#elif defined(LUA_INT_LONGLONG) /* }{ long long */
|
#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */
|
||||||
|
|
||||||
#if defined(_WIN32)
|
/* use presence of macro LLONG_MAX as proxy for C99 compliance */
|
||||||
|
#if defined(LLONG_MAX) /* { */
|
||||||
#define LUA_INTEGER __int64
|
/* use ISO C99 stuff */
|
||||||
#define LUA_INTEGER_FRMLEN "I64"
|
|
||||||
|
|
||||||
#define LUA_MAXINTEGER _I64_MAX
|
|
||||||
#define LUA_MININTEGER _I64_MIN
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#if !defined(LLONG_MAX)
|
|
||||||
#error "Compiler does not support 'long long'. See file 'luaconf.h' line 24"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define LUA_INTEGER long long
|
#define LUA_INTEGER long long
|
||||||
#define LUA_INTEGER_FRMLEN "ll"
|
#define LUA_INTEGER_FRMLEN "ll"
|
||||||
@@ -679,27 +552,21 @@
|
|||||||
#define LUA_MAXINTEGER LLONG_MAX
|
#define LUA_MAXINTEGER LLONG_MAX
|
||||||
#define LUA_MININTEGER LLONG_MIN
|
#define LUA_MININTEGER LLONG_MIN
|
||||||
|
|
||||||
#endif
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
|
/* in Windows, can use specific Windows types */
|
||||||
|
|
||||||
#elif defined(LUA_INT_SHORT) /* }{ short int */
|
#define LUA_INTEGER __int64
|
||||||
/*
|
#define LUA_INTEGER_FRMLEN "I64"
|
||||||
** this option is for tests only; it is not particularly useful and
|
|
||||||
** it does not pass the test suit.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LUA_INTEGER short int
|
#define LUA_MAXINTEGER _I64_MAX
|
||||||
#define LUA_INTEGER_FRMLEN ""
|
#define LUA_MININTEGER _I64_MIN
|
||||||
|
|
||||||
#define LUA_MAXINTEGER SHRT_MAX
|
#else /* }{ */
|
||||||
#define LUA_MININTEGER SHRT_MIN
|
|
||||||
|
|
||||||
#undef LUAI_UACINT
|
#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \
|
||||||
#define LUAI_UACINT int
|
or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)"
|
||||||
|
|
||||||
#undef LUAI_MAXSTACK
|
#endif /* } */
|
||||||
#define LUAI_MAXSTACK 15000
|
|
||||||
|
|
||||||
#define l_castS2U(x) ((LUA_UNSIGNED)(unsigned short)(x))
|
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
@@ -710,6 +577,179 @@
|
|||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Dependencies with C99 and other C details
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89.
|
||||||
|
** (All uses in Lua have only one format item.)
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i)
|
||||||
|
#else
|
||||||
|
#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ lua_strx2number converts an hexadecimal numeric string to a number.
|
||||||
|
** In C99, 'strtod' does that conversion. Otherwise, you can
|
||||||
|
** leave 'lua_strx2number' undefined and Lua will provide its own
|
||||||
|
** implementation.
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
#define lua_strx2number(s,p) lua_str2number(s,p)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ lua_number2strx converts a float to an hexadecimal numeric string.
|
||||||
|
** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that.
|
||||||
|
** Otherwise, you can leave 'lua_number2strx' undefined and Lua will
|
||||||
|
** provide its own implementation.
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
#define lua_number2strx(L,b,sz,f,n) ((void)L, l_sprintf(b,sz,f,n))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 'strtof' and 'opf' variants for math functions are not valid in
|
||||||
|
** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the
|
||||||
|
** availability of these variants. ('math.h' is already included in
|
||||||
|
** all files that use these macros.)
|
||||||
|
*/
|
||||||
|
#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF))
|
||||||
|
#undef l_mathop /* variants not available */
|
||||||
|
#undef lua_str2number
|
||||||
|
#define l_mathop(op) (lua_Number)op /* no variant */
|
||||||
|
#define lua_str2number(s,p) ((lua_Number)strtod((s), (p)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation
|
||||||
|
** functions. It must be a numerical type; Lua will use 'intptr_t' if
|
||||||
|
** available, otherwise it will use 'ptrdiff_t' (the nearest thing to
|
||||||
|
** 'intptr_t' in C89)
|
||||||
|
*/
|
||||||
|
#define LUA_KCONTEXT ptrdiff_t
|
||||||
|
|
||||||
|
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
|
||||||
|
__STDC_VERSION__ >= 199901L
|
||||||
|
#include <stdint.h>
|
||||||
|
#if defined(INTPTR_MAX) /* even in C99 this type is optional */
|
||||||
|
#undef LUA_KCONTEXT
|
||||||
|
#define LUA_KCONTEXT intptr_t
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point).
|
||||||
|
** Change that if you do not want to use C locales. (Code using this
|
||||||
|
** macro must include header 'locale.h'.)
|
||||||
|
*/
|
||||||
|
#if !defined(lua_getlocaledecpoint)
|
||||||
|
#define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Language Variations
|
||||||
|
** =====================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some
|
||||||
|
** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from
|
||||||
|
** numbers to strings. Define LUA_NOCVTS2N to turn off automatic
|
||||||
|
** coercion from strings to numbers.
|
||||||
|
*/
|
||||||
|
/* #define LUA_NOCVTN2S */
|
||||||
|
/* #define LUA_NOCVTS2N */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_USE_APICHECK turns on several consistency checks on the C API.
|
||||||
|
** Define it as a help when debugging C code.
|
||||||
|
*/
|
||||||
|
#if defined(LUA_USE_APICHECK)
|
||||||
|
#include <assert.h>
|
||||||
|
#define luai_apicheck(l,e) assert(e)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Macros that affect the API and must be stable (that is, must be the
|
||||||
|
** same when you compile Lua and when you compile code that links to
|
||||||
|
** Lua). You probably do not want/need to change them.
|
||||||
|
** =====================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUAI_MAXSTACK limits the size of the Lua stack.
|
||||||
|
** CHANGE it if you need a different limit. This limit is arbitrary;
|
||||||
|
** its only purpose is to stop Lua from consuming unlimited stack
|
||||||
|
** space (and to reserve some numbers for pseudo-indices).
|
||||||
|
*/
|
||||||
|
#if LUAI_BITSINT >= 32
|
||||||
|
#define LUAI_MAXSTACK 1000000
|
||||||
|
#else
|
||||||
|
#define LUAI_MAXSTACK 15000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
|
||||||
|
** a Lua state with very fast access.
|
||||||
|
** CHANGE it if you need a different size.
|
||||||
|
*/
|
||||||
|
#define LUA_EXTRASPACE (sizeof(void *))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
||||||
|
@@ of a function in debug information.
|
||||||
|
** CHANGE it if you want a different size.
|
||||||
|
*/
|
||||||
|
#define LUA_IDSIZE 60
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
|
||||||
|
** CHANGE it if it uses too much C-stack space. (For long double,
|
||||||
|
** 'string.format("%.99f", 1e4932)' needs ~5030 bytes, so a
|
||||||
|
** smaller buffer would force a memory allocation for each call to
|
||||||
|
** 'string.format'.)
|
||||||
|
*/
|
||||||
|
#if defined(LUA_FLOAT_LONGDOUBLE)
|
||||||
|
#define LUAL_BUFFERSIZE 8192
|
||||||
|
#else
|
||||||
|
#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_QL describes how error messages quote program elements.
|
||||||
|
** Lua does not use these macros anymore; they are here for
|
||||||
|
** compatibility only.
|
||||||
|
*/
|
||||||
|
#define LUA_QL(x) "'" x "'"
|
||||||
|
#define LUA_QS LUA_QL("%s")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* =================================================================== */
|
/* =================================================================== */
|
||||||
|
|||||||
29
lundump.c
29
lundump.c
@@ -1,14 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.c,v 2.39 2014/06/18 18:35:43 roberto Exp roberto $
|
** $Id: lundump.c,v 2.43 2015/09/17 15:51:05 roberto Exp roberto $
|
||||||
** load precompiled Lua chunks
|
** load precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lundump_c
|
#define lundump_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -29,7 +32,6 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
ZIO *Z;
|
ZIO *Z;
|
||||||
Mbuffer *b;
|
|
||||||
const char *name;
|
const char *name;
|
||||||
} LoadState;
|
} LoadState;
|
||||||
|
|
||||||
@@ -89,10 +91,15 @@ static TString *LoadString (LoadState *S) {
|
|||||||
LoadVar(S, size);
|
LoadVar(S, size);
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
else {
|
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
|
||||||
char *s = luaZ_openspace(S->L, S->b, --size);
|
char buff[LUAI_MAXSHORTLEN];
|
||||||
LoadVector(S, s, size);
|
LoadVector(S, buff, size);
|
||||||
return luaS_newlstr(S->L, s, size);
|
return luaS_newlstr(S->L, buff, size);
|
||||||
|
}
|
||||||
|
else { /* long string */
|
||||||
|
TString *ts = luaS_createlngstrobj(S->L, size);
|
||||||
|
LoadVector(S, getstr(ts), size); /* load directly in final place */
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,8 +255,7 @@ static void checkHeader (LoadState *S) {
|
|||||||
/*
|
/*
|
||||||
** load precompiled chunk
|
** load precompiled chunk
|
||||||
*/
|
*/
|
||||||
LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
|
LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
||||||
const char *name) {
|
|
||||||
LoadState S;
|
LoadState S;
|
||||||
LClosure *cl;
|
LClosure *cl;
|
||||||
if (*name == '@' || *name == '=')
|
if (*name == '@' || *name == '=')
|
||||||
@@ -260,11 +266,10 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
|
|||||||
S.name = name;
|
S.name = name;
|
||||||
S.L = L;
|
S.L = L;
|
||||||
S.Z = Z;
|
S.Z = Z;
|
||||||
S.b = buff;
|
|
||||||
checkHeader(&S);
|
checkHeader(&S);
|
||||||
cl = luaF_newLclosure(L, LoadByte(&S));
|
cl = luaF_newLclosure(L, LoadByte(&S));
|
||||||
setclLvalue(L, L->top, cl);
|
setclLvalue(L, L->top, cl);
|
||||||
incr_top(L);
|
luaD_inctop(L);
|
||||||
cl->p = luaF_newproto(L);
|
cl->p = luaF_newproto(L);
|
||||||
LoadFunction(&S, cl->p, NULL);
|
LoadFunction(&S, cl->p, NULL);
|
||||||
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.h,v 1.43 2014/04/15 14:28:20 roberto Exp roberto $
|
** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 roberto Exp roberto $
|
||||||
** load precompiled Lua chunks
|
** load precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -23,8 +23,7 @@
|
|||||||
#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 */
|
||||||
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff,
|
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
|
||||||
const char* name);
|
|
||||||
|
|
||||||
/* dump one chunk; from ldump.c */
|
/* dump one chunk; from ldump.c */
|
||||||
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
|
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
|
||||||
|
|||||||
25
lutf8lib.c
25
lutf8lib.c
@@ -1,17 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lutf8lib.c,v 1.11 2014/10/01 11:52:33 roberto Exp roberto $
|
** $Id: lutf8lib.c,v 1.14 2015/03/05 16:07:46 roberto Exp roberto $
|
||||||
** Standard library for UTF-8 manipulation
|
** Standard library for UTF-8 manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lutf8lib_c
|
#define lutf8lib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -35,7 +38,7 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
|
|||||||
** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
|
** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
|
||||||
*/
|
*/
|
||||||
static const char *utf8_decode (const char *o, int *val) {
|
static const char *utf8_decode (const char *o, int *val) {
|
||||||
static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
|
static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
|
||||||
const unsigned char *s = (const unsigned char *)o;
|
const unsigned char *s = (const unsigned char *)o;
|
||||||
unsigned int c = s[0];
|
unsigned int c = s[0];
|
||||||
unsigned int res = 0; /* final result */
|
unsigned int res = 0; /* final result */
|
||||||
@@ -104,9 +107,9 @@ static int codepoint (lua_State *L) {
|
|||||||
luaL_argcheck(L, posi >= 1, 2, "out of range");
|
luaL_argcheck(L, posi >= 1, 2, "out of range");
|
||||||
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
|
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
|
||||||
if (posi > pose) return 0; /* empty interval; return no values */
|
if (posi > pose) return 0; /* empty interval; return no values */
|
||||||
n = (int)(pose - posi + 1);
|
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
||||||
if (posi + n <= pose) /* (lua_Integer -> int) overflow? */
|
|
||||||
return luaL_error(L, "string slice too long");
|
return luaL_error(L, "string slice too long");
|
||||||
|
n = (int)(pose - posi) + 1;
|
||||||
luaL_checkstack(L, n, "string slice too long");
|
luaL_checkstack(L, n, "string slice too long");
|
||||||
n = 0;
|
n = 0;
|
||||||
se = s + pose;
|
se = s + pose;
|
||||||
@@ -232,7 +235,7 @@ static int iter_codes (lua_State *L) {
|
|||||||
#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
|
#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
|
||||||
|
|
||||||
|
|
||||||
static struct luaL_Reg funcs[] = {
|
static const luaL_Reg funcs[] = {
|
||||||
{"offset", byteoffset},
|
{"offset", byteoffset},
|
||||||
{"codepoint", codepoint},
|
{"codepoint", codepoint},
|
||||||
{"char", utfchar},
|
{"char", utfchar},
|
||||||
@@ -246,7 +249,7 @@ static struct luaL_Reg funcs[] = {
|
|||||||
|
|
||||||
LUAMOD_API int luaopen_utf8 (lua_State *L) {
|
LUAMOD_API int luaopen_utf8 (lua_State *L) {
|
||||||
luaL_newlib(L, funcs);
|
luaL_newlib(L, funcs);
|
||||||
lua_pushliteral(L, UTF8PATT);
|
lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
|
||||||
lua_setfield(L, -2, "charpattern");
|
lua_setfield(L, -2, "charpattern");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
69
lvm.h
69
lvm.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lvm.h,v 2.33 2014/07/30 14:42:44 roberto Exp roberto $
|
** $Id: lvm.h,v 2.39 2015/09/09 13:44:07 roberto Exp roberto $
|
||||||
** Lua virtual machine
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -27,26 +27,81 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** You can define LUA_FLOORN2I if you want to convert floats to integers
|
||||||
|
** by flooring them (instead of raising an error if they are not
|
||||||
|
** integral values)
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_FLOORN2I)
|
||||||
|
#define LUA_FLOORN2I 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#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))
|
||||||
|
|
||||||
#define tointeger(o,i) \
|
#define tointeger(o,i) \
|
||||||
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i))
|
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(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))
|
||||||
|
|
||||||
#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
|
#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** fast track for 'gettable': if 't' is a table and 't[k]' is not nil,
|
||||||
|
** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise,
|
||||||
|
** return 0 (meaning it will have to check metamethod) with 'slot'
|
||||||
|
** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise).
|
||||||
|
** 'f' is the raw get function to use.
|
||||||
|
*/
|
||||||
|
#define luaV_fastget(L,t,k,slot,f) \
|
||||||
|
(!ttistable(t) \
|
||||||
|
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
|
||||||
|
: (slot = f(hvalue(t), k), /* else, do raw access */ \
|
||||||
|
!ttisnil(slot))) /* result not nil? */
|
||||||
|
|
||||||
|
/*
|
||||||
|
** standard implementation for 'gettable'
|
||||||
|
*/
|
||||||
|
#define luaV_gettable(L,t,k,v) { const TValue *slot; \
|
||||||
|
if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
|
||||||
|
else luaV_finishget(L,t,k,v,slot); }
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Fast track for set table. If 't' is a table and 't[k]' is not nil,
|
||||||
|
** call GC barrier, do a raw 't[k]=v', and return true; otherwise,
|
||||||
|
** return false with 'slot' equal to NULL (if 't' is not a table) or
|
||||||
|
** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro
|
||||||
|
** returns true, there is no need to 'invalidateTMcache', because the
|
||||||
|
** call is not creating a new entry.
|
||||||
|
*/
|
||||||
|
#define luaV_fastset(L,t,k,slot,f,v) \
|
||||||
|
(!ttistable(t) \
|
||||||
|
? (slot = NULL, 0) \
|
||||||
|
: (slot = f(hvalue(t), k), \
|
||||||
|
ttisnil(slot) ? 0 \
|
||||||
|
: (luaC_barrierback(L, hvalue(t), v), \
|
||||||
|
setobj2t(L, cast(TValue *,slot), v), \
|
||||||
|
1)))
|
||||||
|
|
||||||
|
|
||||||
|
#define luaV_settable(L,t,k,v) { const TValue *slot; \
|
||||||
|
if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
|
||||||
|
luaV_finishset(L,t,k,v,slot); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
|
||||||
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
|
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
|
||||||
StkId val);
|
StkId val, const TValue *slot);
|
||||||
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
|
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
|
||||||
StkId val);
|
StkId val, const TValue *slot);
|
||||||
LUAI_FUNC void luaV_finishOp (lua_State *L);
|
LUAI_FUNC void luaV_finishOp (lua_State *L);
|
||||||
LUAI_FUNC void luaV_execute (lua_State *L);
|
LUAI_FUNC void luaV_execute (lua_State *L);
|
||||||
LUAI_FUNC void luaV_concat (lua_State *L, int total);
|
LUAI_FUNC void luaV_concat (lua_State *L, int total);
|
||||||
|
|||||||
20
lzio.c
20
lzio.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lzio.c,v 1.34 2011/07/15 12:35:32 roberto Exp roberto $
|
** $Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp roberto $
|
||||||
** Buffered streams
|
** Buffered streams
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lzio_c
|
#define lzio_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
@@ -64,13 +66,3 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------ */
|
|
||||||
char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
|
|
||||||
if (n > buff->buffsize) {
|
|
||||||
if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
|
|
||||||
luaZ_resizebuffer(L, buff, n);
|
|
||||||
}
|
|
||||||
return buff->buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
6
lzio.h
6
lzio.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lzio.h,v 1.27 2013/06/07 14:51:10 roberto Exp roberto $
|
** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 roberto Exp roberto $
|
||||||
** Buffered streams
|
** Buffered streams
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -37,13 +37,13 @@ typedef struct Mbuffer {
|
|||||||
|
|
||||||
|
|
||||||
#define luaZ_resizebuffer(L, buff, size) \
|
#define luaZ_resizebuffer(L, buff, size) \
|
||||||
(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
|
((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \
|
||||||
|
(buff)->buffsize, size), \
|
||||||
(buff)->buffsize = size)
|
(buff)->buffsize = size)
|
||||||
|
|
||||||
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
|
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
|
|
||||||
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
|
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
|
||||||
void *data);
|
void *data);
|
||||||
LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
|
LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
|
||||||
|
|||||||
162
makefile
162
makefile
@@ -5,19 +5,18 @@
|
|||||||
# == 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=-pedantic -Wno-long-long \
|
CWARNSCPP= \
|
||||||
-Wall -Wextra \
|
-pedantic \
|
||||||
-Waggregate-return \
|
-Wextra \
|
||||||
-Wcast-align \
|
|
||||||
-Wdisabled-optimization \
|
|
||||||
-Wshadow \
|
-Wshadow \
|
||||||
-Wsign-compare \
|
-Wsign-compare \
|
||||||
-Wundef \
|
-Wundef \
|
||||||
-Wwrite-strings \
|
-Wwrite-strings \
|
||||||
-Wredundant-decls \
|
-Wredundant-decls \
|
||||||
-Wdisabled-optimization \
|
-Wdisabled-optimization \
|
||||||
-Wstrict-aliasing \
|
# the next warnings generate to much noise, so they are disabled
|
||||||
# the next warnings generate to much noise, so they are disabled
|
# -Wdouble-promotion \
|
||||||
|
# -Wconversion -Wno-sign-conversion \
|
||||||
# -Wsign-conversion \
|
# -Wsign-conversion \
|
||||||
# -Wconversion \
|
# -Wconversion \
|
||||||
# -Wlogical-op \
|
# -Wlogical-op \
|
||||||
@@ -27,21 +26,23 @@ CWARNSCPP=-pedantic -Wno-long-long \
|
|||||||
|
|
||||||
# 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 \
|
||||||
-Wnested-externs \
|
-Wnested-externs \
|
||||||
-Wstrict-prototypes \
|
-Wstrict-prototypes \
|
||||||
-Wc++-compat \
|
-Wc++-compat \
|
||||||
-Wold-style-declaration \
|
|
||||||
-Wold-style-definition \
|
-Wold-style-definition \
|
||||||
|
|
||||||
|
|
||||||
CWARNS= $(CWARNSCPP) $(CWARNSC)
|
CWARNS= $(CWARNSCPP) $(CWARNSC)
|
||||||
|
|
||||||
|
|
||||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
||||||
# -g -DLUA_USER_H='"ltests.h"'
|
# -g -DLUA_USER_H='"ltests.h"'
|
||||||
# -fomit-frame-pointer #-pg -malign-double
|
# -pg -malign-double
|
||||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||||
TESTS= -DLUA_USER_H='"ltests.h"'
|
# (in clang, '-ftrapv' for runtime checks of integer overflows)
|
||||||
|
# -fsanitize=undefined -ftrapv
|
||||||
|
# TESTS= -DLUA_USER_H='"ltests.h"'
|
||||||
|
|
||||||
# -mtune=native -fomit-frame-pointer
|
# -mtune=native -fomit-frame-pointer
|
||||||
LOCAL = $(TESTS) $(CWARNS) -g
|
LOCAL = $(TESTS) $(CWARNS) -g
|
||||||
@@ -49,15 +50,13 @@ LOCAL = $(TESTS) $(CWARNS) -g
|
|||||||
|
|
||||||
|
|
||||||
# enable Linux goodies
|
# enable Linux goodies
|
||||||
MYCFLAGS= $(LOCAL) -DLUA_USE_LINUX -DLUA_COMPAT_5_2
|
MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_COMPAT_5_2
|
||||||
MYLDFLAGS= $(LOCAL) -Wl,-E
|
MYLDFLAGS= $(LOCAL) -Wl,-E
|
||||||
MYLIBS= -ldl -lreadline -lhistory -lncurses
|
MYLIBS= -ldl -lreadline -lhistory -lncurses
|
||||||
|
|
||||||
|
|
||||||
CC= gcc
|
CC= clang-3.6
|
||||||
CFLAGS= -Wall -O2 $(MYCFLAGS)
|
CFLAGS= -Wall -O2 $(MYCFLAGS)
|
||||||
# CC= ~lhf/sunstudio12/bin/cc
|
|
||||||
# CFLAGS= -xO5 -v -Xc -native -xstrconst
|
|
||||||
AR= ar rcu
|
AR= ar rcu
|
||||||
RANLIB= ranlib
|
RANLIB= ranlib
|
||||||
RM= rm -f
|
RM= rm -f
|
||||||
@@ -126,66 +125,71 @@ $(ALL_O): makefile
|
|||||||
# DO NOT EDIT
|
# DO NOT EDIT
|
||||||
# automatically made with 'gcc -MM l*.c'
|
# automatically made with 'gcc -MM l*.c'
|
||||||
|
|
||||||
lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \
|
lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \
|
||||||
|
ltable.h lundump.h lvm.h
|
||||||
|
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h
|
||||||
|
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lbitlib.o: lbitlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||||
|
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||||
|
ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||||
|
lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h
|
||||||
|
ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
|
lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \
|
||||||
|
ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h
|
||||||
|
ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
|
||||||
|
lparser.h lstring.h ltable.h lundump.h lvm.h
|
||||||
|
ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \
|
||||||
|
ltm.h lzio.h lmem.h lundump.h
|
||||||
|
lfunc.o: lfunc.c lprefix.h lua.h luaconf.h lfunc.h lobject.h llimits.h \
|
||||||
|
lgc.h lstate.h ltm.h lzio.h lmem.h
|
||||||
|
lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
|
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
|
||||||
|
linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h
|
||||||
|
liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \
|
||||||
|
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \
|
||||||
|
lstring.h ltable.h
|
||||||
|
lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
|
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h
|
||||||
|
loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \
|
||||||
|
ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \
|
||||||
lvm.h
|
lvm.h
|
||||||
lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h
|
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h
|
||||||
lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h
|
loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h
|
lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||||
lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
|
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||||
lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
|
ldo.h lfunc.h lstring.h lgc.h ltable.h
|
||||||
lstring.h ltable.h lvm.h
|
lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \
|
||||||
lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h
|
lstring.h ltable.h
|
||||||
ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
|
lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
||||||
ldebug.o: ldebug.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \
|
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
||||||
ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h ldebug.h ldo.h \
|
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lfunc.h lstring.h lgc.h ltable.h lvm.h
|
ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
ldo.o: ldo.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \
|
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||||
lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h \
|
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lstring.h ltable.h lundump.h lvm.h
|
ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \
|
lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \
|
||||||
lzio.h lmem.h lundump.h
|
lparser.h lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h \
|
||||||
lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \
|
lualib.h
|
||||||
lstate.h ltm.h lzio.h lmem.h
|
ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h
|
||||||
lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
|
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h
|
lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
||||||
liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h
|
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
|
||||||
llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \
|
lundump.h
|
||||||
lstate.h ltm.h lzio.h lmem.h lgc.h llex.h lparser.h lstring.h ltable.h
|
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
|
lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
|
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \
|
||||||
ltm.h lzio.h lmem.h ldo.h lgc.h
|
ltable.h lvm.h
|
||||||
loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
|
lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
|
||||||
lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \
|
lobject.h ltm.h lzio.h
|
||||||
lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h
|
|
||||||
lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h
|
|
||||||
loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
|
|
||||||
lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lfunc.h \
|
|
||||||
lstring.h lgc.h ltable.h
|
|
||||||
lstate.o: lstate.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \
|
|
||||||
ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h lstring.h \
|
|
||||||
ltable.h
|
|
||||||
lstring.o: lstring.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
|
||||||
lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
|
|
||||||
ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
|
||||||
ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
ltests.o: ltests.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \
|
|
||||||
ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h lparser.h \
|
|
||||||
lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lualib.h
|
|
||||||
ltm.o: ltm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
|
||||||
lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h
|
|
||||||
lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h
|
|
||||||
lutf8lib.o: lutf8lib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
|
||||||
lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
|
|
||||||
lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \
|
|
||||||
lzio.h
|
|
||||||
|
|
||||||
# (end of Makefile)
|
# (end of Makefile)
|
||||||
|
|||||||
Reference in New Issue
Block a user