mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
261 Commits
v5.3-alpha
...
v5.3.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
88ff582fd8 | ||
|
|
6b12b82532 | ||
|
|
d7a0179cd6 | ||
|
|
6d613817d4 | ||
|
|
b8d412aa07 | ||
|
|
961760c9b6 | ||
|
|
57559547a3 | ||
|
|
cfa84e1232 | ||
|
|
0d31efb365 | ||
|
|
f97c64d7bf | ||
|
|
ea3155e380 | ||
|
|
c172a4f7c2 | ||
|
|
f8e0d33b25 | ||
|
|
745618d941 | ||
|
|
89602bf747 | ||
|
|
56d4537879 | ||
|
|
18014ef2fd | ||
|
|
ffe7401ceb | ||
|
|
65ec3379b1 | ||
|
|
85fc9ecd5f | ||
|
|
2a21f6c894 | ||
|
|
3670db99b6 | ||
|
|
e14a02c24f | ||
|
|
15f4150518 | ||
|
|
c5cae9362c | ||
|
|
4f4e0e49bb | ||
|
|
2f4d429071 | ||
|
|
27f7bd2e83 | ||
|
|
798660c9cd | ||
|
|
34b6664dcb | ||
|
|
d35fff16d5 | ||
|
|
56699cd603 | ||
|
|
8ede2c353c | ||
|
|
6384475ec4 | ||
|
|
3a15c7ce43 | ||
|
|
0a6b58c3aa | ||
|
|
979a663d2a | ||
|
|
22952d8973 | ||
|
|
df608f842a | ||
|
|
001bb46ae1 | ||
|
|
9b25347a67 | ||
|
|
e401513086 | ||
|
|
7f1a2ad699 | ||
|
|
a1ab5ab396 | ||
|
|
01549fb1ed | ||
|
|
4fba117433 | ||
|
|
2be88d5084 | ||
|
|
f5c690b684 | ||
|
|
61fa462535 |
248
bugs
248
bugs
@@ -1878,13 +1878,6 @@ for i=1,25 do print(i); crash(i) end
|
||||
patch = [[
|
||||
--- lundump.c 2008/04/04 16:00:45 2.7.1.3
|
||||
+++ lundump.c 2008/04/04 19:51:41 2.7.1.4
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
-** $Id: bugs,v 1.131 2014/05/07 16:35:24 roberto Exp roberto $
|
||||
+** $Id: bugs,v 1.131 2014/05/07 16:35:24 roberto Exp roberto $
|
||||
** load precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -161,7 +161,9 @@
|
||||
|
||||
static Proto* LoadFunction(LoadState* S, TString* p)
|
||||
@@ -3220,6 +3213,245 @@ example = [[
|
||||
(segfaults on some platforms with some compiler options)
|
||||
]],
|
||||
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] */
|
||||
]]
|
||||
}
|
||||
|
||||
Bug{
|
||||
what = [[Ephemeron table can wrongly collect entry with strong key]],
|
||||
report = [[Jörg Richter, 2014/08/22]],
|
||||
since = [[5.2]],
|
||||
fix = nil,
|
||||
example = [[
|
||||
(This bug is very hard to reproduce,
|
||||
because it depends on a specific interleaving of
|
||||
events between the incremental collector and the program.)
|
||||
]],
|
||||
patch = [[
|
||||
--- lgc.c 2013/04/26 18:22:05 2.140.1.2
|
||||
+++ lgc.c 2014/09/01 13:24:33
|
||||
@@ -403,7 +403,7 @@
|
||||
reallymarkobject(g, gcvalue(gval(n))); /* mark it now */
|
||||
}
|
||||
}
|
||||
- if (prop)
|
||||
+ if (g->gcstate != GCSatomic || prop)
|
||||
linktable(h, &g->ephemeron); /* have to propagate again */
|
||||
else if (hasclears) /* does table have white keys? */
|
||||
linktable(h, &g->allweak); /* may have to clean white keys */
|
||||
]]
|
||||
}
|
||||
|
||||
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 = [[
|
||||
]]
|
||||
}
|
||||
|
||||
@@ -3235,3 +3467,5 @@ patch = [[
|
||||
]]
|
||||
}
|
||||
]=]
|
||||
|
||||
|
||||
|
||||
198
lapi.c
198
lapi.c
@@ -1,17 +1,18 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 2.231 2014/07/22 18:07:47 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 2.248 2015/03/28 19:14:47 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lapi_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -50,29 +51,29 @@ const char lua_ident[] =
|
||||
/* test for valid but not pseudo index */
|
||||
#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) \
|
||||
api_check(isstackindex(i, o), "index not in the stack")
|
||||
#define api_checkstackindex(l, i, o) \
|
||||
api_check(l, isstackindex(i, o), "index not in the stack")
|
||||
|
||||
|
||||
static TValue *index2addr (lua_State *L, int idx) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (idx > 0) {
|
||||
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;
|
||||
else return o;
|
||||
}
|
||||
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;
|
||||
}
|
||||
else if (idx == LUA_REGISTRYINDEX)
|
||||
return &G(L)->l_registry;
|
||||
else { /* upvalues */
|
||||
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? */
|
||||
return NONVALIDVALUE; /* it has no upvalues */
|
||||
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;
|
||||
CallInfo *ci = L->ci;
|
||||
lua_lock(L);
|
||||
api_check(size >= 0, "negative 'size'");
|
||||
if (L->stack_last - L->top > size) /* stack large enough? */
|
||||
api_check(L, n >= 0, "negative 'n'");
|
||||
if (L->stack_last - L->top > n) /* stack large enough? */
|
||||
res = 1; /* yes; check is OK */
|
||||
else { /* no; need to grow 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 */
|
||||
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)
|
||||
ci->top = L->top + size; /* adjust frame top */
|
||||
if (res && ci->top < L->top + n)
|
||||
ci->top = L->top + n; /* adjust frame top */
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
@@ -119,11 +120,12 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
|
||||
if (from == to) return;
|
||||
lua_lock(to);
|
||||
api_checknelems(from, n);
|
||||
api_check(G(from) == G(to), "moving among independent states");
|
||||
api_check(to->ci->top - to->top >= n, "not enough elements to move");
|
||||
api_check(from, G(from) == G(to), "moving among independent states");
|
||||
api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
|
||||
from->top -= n;
|
||||
for (i = 0; i < n; i++) {
|
||||
setobj2s(to, to->top++, from->top + i);
|
||||
setobj2s(to, to->top, from->top + i);
|
||||
api_incr_top(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) {
|
||||
return (idx > 0 || ispseudo(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;
|
||||
lua_lock(L);
|
||||
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)
|
||||
setnilvalue(L->top++);
|
||||
L->top = (func + 1) + idx;
|
||||
}
|
||||
else {
|
||||
api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
||||
L->top += idx+1; /* `subtract' index (index is negative) */
|
||||
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
||||
L->top += idx+1; /* 'subtract' index (index is negative) */
|
||||
}
|
||||
lua_unlock(L);
|
||||
}
|
||||
@@ -207,8 +209,8 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
||||
lua_lock(L);
|
||||
t = L->top - 1; /* end of stack segment being rotated */
|
||||
p = index2addr(L, idx); /* start of segment */
|
||||
api_checkstackindex(idx, p);
|
||||
api_check((n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
||||
api_checkstackindex(L, idx, p);
|
||||
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
||||
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
||||
reverse(L, p, m); /* reverse the prefix with length 'n' */
|
||||
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);
|
||||
fr = index2addr(L, fromidx);
|
||||
to = index2addr(L, toidx);
|
||||
api_checkvalidindex(to);
|
||||
api_checkvalidindex(L, to);
|
||||
setobj(L, to, fr);
|
||||
if (isupvalue(toidx)) /* function upvalue? */
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -304,7 +306,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
|
||||
else { /* for unary operations, add fake 2nd operand */
|
||||
api_checknelems(L, 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 */
|
||||
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_OPLT: i = luaV_lessthan(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);
|
||||
@@ -332,7 +334,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API size_t lua_strtonum (lua_State *L, const char *s) {
|
||||
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
||||
size_t sz = luaO_str2num(s, L->top);
|
||||
if (sz != 0)
|
||||
api_incr_top(L);
|
||||
@@ -375,21 +377,23 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
if (len != NULL) *len = 0;
|
||||
return NULL;
|
||||
}
|
||||
lua_lock(L); /* `luaO_tostring' may create a new string */
|
||||
lua_lock(L); /* 'luaO_tostring' may create a new string */
|
||||
luaC_checkGC(L);
|
||||
o = index2addr(L, idx); /* previous call may reallocate the stack */
|
||||
luaO_tostring(L, o);
|
||||
lua_unlock(L);
|
||||
}
|
||||
if (len != NULL) *len = tsvalue(o)->len;
|
||||
if (len != NULL)
|
||||
*len = vslen(o);
|
||||
return svalue(o);
|
||||
}
|
||||
|
||||
|
||||
LUA_API size_t lua_rawlen (lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
switch (ttnov(o)) {
|
||||
case LUA_TSTRING: return tsvalue(o)->len;
|
||||
switch (ttype(o)) {
|
||||
case LUA_TSHRSTR: return tsvalue(o)->shrlen;
|
||||
case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
|
||||
case LUA_TUSERDATA: return uvalue(o)->len;
|
||||
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
||||
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_TLCF: return cast(void *, cast(size_t, fvalue(o)));
|
||||
case LUA_TTHREAD: return thvalue(o);
|
||||
case LUA_TUSERDATA:
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
return lua_touserdata(L, idx);
|
||||
case LUA_TUSERDATA: return getudatamem(uvalue(o));
|
||||
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
@@ -481,20 +484,19 @@ 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) {
|
||||
if (s == NULL) {
|
||||
lua_pushnil(L);
|
||||
return NULL;
|
||||
}
|
||||
lua_lock(L);
|
||||
if (s == NULL)
|
||||
setnilvalue(L->top);
|
||||
else {
|
||||
TString *ts;
|
||||
lua_lock(L);
|
||||
luaC_checkGC(L);
|
||||
ts = luaS_new(L, s);
|
||||
setsvalue2s(L, L->top, ts);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return getstr(ts);
|
||||
s = getstr(ts); /* internal copy's address */
|
||||
}
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
@@ -530,7 +532,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
else {
|
||||
CClosure *cl;
|
||||
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->f = fn;
|
||||
@@ -577,12 +579,13 @@ LUA_API int lua_pushthread (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
LUA_API int lua_getglobal (lua_State *L, const char *var) {
|
||||
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt; /* global table */
|
||||
lua_lock(L);
|
||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||
setsvalue2s(L, L->top++, luaS_new(L, var));
|
||||
setsvalue2s(L, L->top, luaS_new(L, name));
|
||||
api_incr_top(L);
|
||||
luaV_gettable(L, gt, L->top - 1, L->top - 1);
|
||||
lua_unlock(L);
|
||||
return ttnov(L->top - 1);
|
||||
@@ -611,11 +614,23 @@ 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) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
setivalue(L->top, n);
|
||||
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_rawget (lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
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));
|
||||
lua_unlock(L);
|
||||
return ttnov(L->top - 1);
|
||||
@@ -626,7 +641,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
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));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
@@ -639,7 +654,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
|
||||
TValue k;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
api_check(ttistable(t), "table expected");
|
||||
api_check(L, ttistable(t), "table expected");
|
||||
setpvalue(&k, cast(void *, p));
|
||||
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
|
||||
api_incr_top(L);
|
||||
@@ -663,8 +678,8 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
||||
|
||||
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
||||
const TValue *obj;
|
||||
Table *mt = NULL;
|
||||
int res;
|
||||
Table *mt;
|
||||
int res = 0;
|
||||
lua_lock(L);
|
||||
obj = index2addr(L, objindex);
|
||||
switch (ttnov(obj)) {
|
||||
@@ -678,9 +693,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
||||
mt = G(L)->mt[ttnov(obj)];
|
||||
break;
|
||||
}
|
||||
if (mt == NULL)
|
||||
res = 0;
|
||||
else {
|
||||
if (mt != NULL) {
|
||||
sethvalue(L, L->top, mt);
|
||||
api_incr_top(L);
|
||||
res = 1;
|
||||
@@ -694,7 +707,7 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
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);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
@@ -707,13 +720,14 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) {
|
||||
*/
|
||||
|
||||
|
||||
LUA_API void lua_setglobal (lua_State *L, const char *var) {
|
||||
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt; /* global table */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||
setsvalue2s(L, L->top++, luaS_new(L, var));
|
||||
setsvalue2s(L, L->top, luaS_new(L, name));
|
||||
api_incr_top(L);
|
||||
luaV_settable(L, gt, L->top - 1, L->top - 2);
|
||||
L->top -= 2; /* pop value and key */
|
||||
lua_unlock(L);
|
||||
@@ -736,7 +750,21 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = index2addr(L, idx);
|
||||
setsvalue2s(L, L->top++, luaS_new(L, k));
|
||||
setsvalue2s(L, L->top, luaS_new(L, k));
|
||||
api_incr_top(L);
|
||||
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) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = index2addr(L, idx);
|
||||
setivalue(L->top, n);
|
||||
api_incr_top(L);
|
||||
luaV_settable(L, t, L->top - 1, L->top - 2);
|
||||
L->top -= 2; /* pop value and key */
|
||||
lua_unlock(L);
|
||||
@@ -749,7 +777,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
o = index2addr(L, idx);
|
||||
api_check(ttistable(o), "table expected");
|
||||
api_check(L, ttistable(o), "table expected");
|
||||
t = hvalue(o);
|
||||
setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
|
||||
invalidateTMcache(t);
|
||||
@@ -765,7 +793,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
o = index2addr(L, idx);
|
||||
api_check(ttistable(o), "table expected");
|
||||
api_check(L, ttistable(o), "table expected");
|
||||
t = hvalue(o);
|
||||
luaH_setint(L, t, n, L->top - 1);
|
||||
luaC_barrierback(L, t, L->top-1);
|
||||
@@ -781,7 +809,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
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));
|
||||
setobj2t(L, luaH_set(L, t, &k), L->top - 1);
|
||||
@@ -800,7 +828,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
||||
if (ttisnil(L->top - 1))
|
||||
mt = NULL;
|
||||
else {
|
||||
api_check(ttistable(L->top - 1), "table expected");
|
||||
api_check(L, ttistable(L->top - 1), "table expected");
|
||||
mt = hvalue(L->top - 1);
|
||||
}
|
||||
switch (ttnov(obj)) {
|
||||
@@ -836,7 +864,7 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
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);
|
||||
luaC_barrier(L, gcvalue(o), L->top - 1);
|
||||
L->top--;
|
||||
@@ -845,23 +873,23 @@ 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) \
|
||||
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")
|
||||
|
||||
|
||||
LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_Ctx ctx,
|
||||
lua_KFunction k) {
|
||||
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
||||
lua_KContext ctx, lua_KFunction k) {
|
||||
StkId func;
|
||||
lua_lock(L);
|
||||
api_check(k == NULL || !isLua(L->ci),
|
||||
api_check(L, k == NULL || !isLua(L->ci),
|
||||
"cannot use continuations inside hooks");
|
||||
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);
|
||||
func = L->top - (nargs+1);
|
||||
if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
|
||||
@@ -880,7 +908,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_Ctx ctx,
|
||||
/*
|
||||
** Execute a protected call.
|
||||
*/
|
||||
struct CallS { /* data to `f_call' */
|
||||
struct CallS { /* data to 'f_call' */
|
||||
StkId func;
|
||||
int nresults;
|
||||
};
|
||||
@@ -894,21 +922,21 @@ static void f_call (lua_State *L, void *ud) {
|
||||
|
||||
|
||||
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
lua_Ctx ctx, lua_KFunction k) {
|
||||
lua_KContext ctx, lua_KFunction k) {
|
||||
struct CallS c;
|
||||
int status;
|
||||
ptrdiff_t func;
|
||||
lua_lock(L);
|
||||
api_check(k == NULL || !isLua(L->ci),
|
||||
api_check(L, k == NULL || !isLua(L->ci),
|
||||
"cannot use continuations inside hooks");
|
||||
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);
|
||||
if (errfunc == 0)
|
||||
func = 0;
|
||||
else {
|
||||
StkId o = index2addr(L, errfunc);
|
||||
api_checkstackindex(errfunc, o);
|
||||
api_checkstackindex(L, errfunc, o);
|
||||
func = savestack(L, o);
|
||||
}
|
||||
c.func = L->top - (nargs+1); /* function to be called */
|
||||
@@ -947,7 +975,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
||||
status = luaD_protectedparser(L, &z, chunkname, mode);
|
||||
if (status == LUA_OK) { /* no errors? */
|
||||
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 */
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||
@@ -1073,7 +1101,7 @@ LUA_API int lua_next (lua_State *L, int idx) {
|
||||
int more;
|
||||
lua_lock(L);
|
||||
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);
|
||||
if (more) {
|
||||
api_incr_top(L);
|
||||
@@ -1205,9 +1233,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) {
|
||||
LClosure *f;
|
||||
StkId fi = index2addr(L, fidx);
|
||||
api_check(ttisLclosure(fi), "Lua function expected");
|
||||
api_check(L, ttisLclosure(fi), "Lua function expected");
|
||||
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;
|
||||
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
||||
}
|
||||
@@ -1221,11 +1249,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
||||
}
|
||||
case LUA_TCCL: { /* C closure */
|
||||
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];
|
||||
}
|
||||
default: {
|
||||
api_check(0, "closure expected");
|
||||
api_check(L, 0, "closure expected");
|
||||
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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -11,13 +11,13 @@
|
||||
#include "llimits.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");}
|
||||
|
||||
#define adjustresults(L,nres) \
|
||||
{ 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")
|
||||
|
||||
|
||||
|
||||
105
lauxlib.c
105
lauxlib.c
@@ -1,9 +1,14 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.266 2014/07/17 12:30:53 roberto Exp roberto $
|
||||
** $Id: lauxlib.c,v 1.279 2014/12/14 18:32:26 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lauxlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
@@ -16,9 +21,6 @@
|
||||
** Any function declared here could be written as an application function.
|
||||
*/
|
||||
|
||||
#define lauxlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -64,11 +66,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) {
|
||||
int top = lua_gettop(L);
|
||||
lua_getinfo(L, "f", ar); /* push function */
|
||||
lua_pushglobaltable(L);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
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_pop(L, 2); /* remove pushed values */
|
||||
return 1;
|
||||
@@ -81,20 +92,18 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
||||
|
||||
|
||||
static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
||||
if (*ar->namewhat != '\0') /* is there a name? */
|
||||
lua_pushfstring(L, "function " LUA_QS, ar->name);
|
||||
if (pushglobalfuncname(L, ar)) { /* try first a global name */
|
||||
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
||||
lua_remove(L, -2); /* remove name */
|
||||
}
|
||||
else if (*ar->namewhat != '\0') /* is there a name from code? */
|
||||
lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */
|
||||
else if (*ar->what == 'm') /* main? */
|
||||
lua_pushliteral(L, "main chunk");
|
||||
else if (*ar->what == 'C') {
|
||||
if (pushglobalfuncname(L, ar)) {
|
||||
lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
|
||||
lua_remove(L, -2); /* remove name */
|
||||
}
|
||||
else
|
||||
lua_pushliteral(L, "?");
|
||||
}
|
||||
else
|
||||
else if (*ar->what != 'C') /* for Lua functions, use <file:line> */
|
||||
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
|
||||
else /* nothing left... */
|
||||
lua_pushliteral(L, "?");
|
||||
}
|
||||
|
||||
|
||||
@@ -156,27 +165,27 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
|
||||
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
|
||||
lua_getinfo(L, "n", &ar);
|
||||
if (strcmp(ar.namewhat, "method") == 0) {
|
||||
arg--; /* do not count `self' */
|
||||
arg--; /* do not count 'self' */
|
||||
if (arg == 0) /* error is in the self argument itself? */
|
||||
return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
|
||||
return luaL_error(L, "calling '%s' on bad self (%s)",
|
||||
ar.name, extramsg);
|
||||
}
|
||||
if (ar.name == NULL)
|
||||
ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
|
||||
return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
|
||||
return luaL_error(L, "bad argument #%d to '%s' (%s)",
|
||||
arg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
static int typeerror (lua_State *L, int arg, const char *tname) {
|
||||
const char *msg;
|
||||
const char *typearg = luaL_typename(L, arg);
|
||||
if (lua_getmetatable(L, arg)) {
|
||||
if (lua_getfield(L, -1, "__name") == LUA_TSTRING)
|
||||
typearg = lua_tostring(L, -1);
|
||||
}
|
||||
const char *typearg; /* name for the type of the actual argument */
|
||||
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
|
||||
typearg = lua_tostring(L, -1); /* use the given type name */
|
||||
else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
|
||||
typearg = "light userdata";
|
||||
typearg = "light userdata"; /* special name for messages */
|
||||
else
|
||||
typearg = luaL_typename(L, arg); /* standard name */
|
||||
msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg);
|
||||
return luaL_argerror(L, arg, msg);
|
||||
}
|
||||
@@ -229,7 +238,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
||||
}
|
||||
|
||||
|
||||
#if !defined(inspectstat) /* { */
|
||||
#if !defined(l_inspectstat) /* { */
|
||||
|
||||
#if defined(LUA_USE_POSIX)
|
||||
|
||||
@@ -238,13 +247,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
||||
/*
|
||||
** use appropriate macros to interpret 'pclose' return status
|
||||
*/
|
||||
#define inspectstat(stat,what) \
|
||||
#define l_inspectstat(stat,what) \
|
||||
if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
|
||||
else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
|
||||
|
||||
#else
|
||||
|
||||
#define inspectstat(stat,what) /* no op */
|
||||
#define l_inspectstat(stat,what) /* no op */
|
||||
|
||||
#endif
|
||||
|
||||
@@ -256,7 +265,7 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) {
|
||||
if (stat == -1) /* error? */
|
||||
return luaL_fileresult(L, 0, NULL);
|
||||
else {
|
||||
inspectstat(stat, what); /* interpret result */
|
||||
l_inspectstat(stat, what); /* interpret result */
|
||||
if (*what == 'e' && stat == 0) /* successful termination? */
|
||||
lua_pushboolean(L, 1);
|
||||
else
|
||||
@@ -277,8 +286,7 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) {
|
||||
*/
|
||||
|
||||
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||
luaL_getmetatable(L, tname); /* try to get metatable */
|
||||
if (!lua_isnil(L, -1)) /* name already in use? */
|
||||
if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */
|
||||
return 0; /* leave previous value on top, but return 0 */
|
||||
lua_pop(L, 1);
|
||||
lua_newtable(L); /* create metatable */
|
||||
@@ -335,7 +343,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
|
||||
if (strcmp(lst[i], name) == 0)
|
||||
return i;
|
||||
return luaL_argerror(L, arg,
|
||||
lua_pushfstring(L, "invalid option " LUA_QS, name));
|
||||
lua_pushfstring(L, "invalid option '%s'", name));
|
||||
}
|
||||
|
||||
|
||||
@@ -526,7 +534,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
int ref;
|
||||
if (lua_isnil(L, -1)) {
|
||||
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);
|
||||
lua_rawgeti(L, t, freelist); /* get first free element */
|
||||
@@ -565,7 +573,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
||||
typedef struct LoadF {
|
||||
int n; /* number of pre-read characters */
|
||||
FILE *f; /* file being read */
|
||||
char buff[LUAL_BUFFERSIZE]; /* area for reading file */
|
||||
char buff[BUFSIZ]; /* area for reading file */
|
||||
} LoadF;
|
||||
|
||||
|
||||
@@ -658,7 +666,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
||||
readstatus = ferror(lf.f);
|
||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||
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);
|
||||
}
|
||||
lua_remove(L, fnameindex);
|
||||
@@ -701,22 +709,23 @@ LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
|
||||
|
||||
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
|
||||
if (!lua_getmetatable(L, obj)) /* no metatable? */
|
||||
return 0;
|
||||
lua_pushstring(L, event);
|
||||
if (lua_rawget(L, -2) == LUA_TNIL) { /* is metafield nil? */
|
||||
lua_pop(L, 2); /* remove metatable and metafield */
|
||||
return 0;
|
||||
}
|
||||
return LUA_TNIL;
|
||||
else {
|
||||
lua_remove(L, -2); /* remove only metatable */
|
||||
return 1;
|
||||
int tt;
|
||||
lua_pushstring(L, event);
|
||||
tt = lua_rawget(L, -2);
|
||||
if (tt == LUA_TNIL) /* is metafield nil? */
|
||||
lua_pop(L, 2); /* remove metatable and metafield */
|
||||
else
|
||||
lua_remove(L, -2); /* remove only metatable */
|
||||
return tt; /* return metafield type */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
|
||||
obj = lua_absindex(L, obj);
|
||||
if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
|
||||
if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */
|
||||
return 0;
|
||||
lua_pushvalue(L, obj);
|
||||
lua_call(L, 1, 1);
|
||||
@@ -822,7 +831,7 @@ LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
|
||||
/* try global variable (and create one if it does not exist) */
|
||||
lua_pushglobaltable(L);
|
||||
if (luaL_findtable(L, 0, modname, sizehint) != NULL)
|
||||
luaL_error(L, "name conflict for module " LUA_QS, modname);
|
||||
luaL_error(L, "name conflict for module '%s'", modname);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
|
||||
}
|
||||
@@ -917,7 +926,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
|
||||
while ((wild = strstr(s, p)) != NULL) {
|
||||
luaL_addlstring(&b, s, wild - s); /* push prefix */
|
||||
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_pushresult(&b);
|
||||
@@ -937,8 +946,8 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
|
||||
|
||||
static int panic (lua_State *L) {
|
||||
luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
lua_tostring(L, -1));
|
||||
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
lua_tostring(L, -1));
|
||||
return 0; /* return to Lua to abort */
|
||||
}
|
||||
|
||||
|
||||
45
lauxlib.h
45
lauxlib.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.124 2014/04/15 18:25:49 roberto Exp roberto $
|
||||
** $Id: lauxlib.h,v 1.127 2014/10/25 11:50:46 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** 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)
|
||||
|
||||
|
||||
@@ -115,10 +115,6 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
||||
((void)((cond) || luaL_argerror(L, (arg), (extramsg))))
|
||||
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
|
||||
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
|
||||
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
|
||||
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
|
||||
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
|
||||
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
|
||||
|
||||
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
|
||||
|
||||
@@ -208,17 +204,48 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
|
||||
#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 unsigned conversions
|
||||
** Compatibility with deprecated conversions
|
||||
** =============================================================
|
||||
*/
|
||||
#if defined(LUA_COMPAT_APIUNSIGNED)
|
||||
#if defined(LUA_COMPAT_APIINTCASTS)
|
||||
|
||||
#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
|
||||
#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
|
||||
#define luaL_optunsigned(L,a,d) \
|
||||
((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
|
||||
|
||||
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
|
||||
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
|
||||
|
||||
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
|
||||
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
|
||||
|
||||
#endif
|
||||
/* }============================================================ */
|
||||
|
||||
|
||||
78
lbaselib.c
78
lbaselib.c
@@ -1,9 +1,13 @@
|
||||
/*
|
||||
** $Id: lbaselib.c,v 1.292 2014/07/17 13:53:37 roberto Exp roberto $
|
||||
** $Id: lbaselib.c,v 1.309 2014/12/10 12:26:42 roberto Exp roberto $
|
||||
** Basic library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lbaselib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
@@ -11,9 +15,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lbaselib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -32,13 +33,12 @@ static int luaB_print (lua_State *L) {
|
||||
lua_call(L, 1, 1);
|
||||
s = lua_tolstring(L, -1, &l); /* get result */
|
||||
if (s == NULL)
|
||||
return luaL_error(L,
|
||||
LUA_QL("tostring") " must return a string to " LUA_QL("print"));
|
||||
if (i>1) luai_writestring("\t", 1);
|
||||
luai_writestring(s, l);
|
||||
return luaL_error(L, "'tostring' must return a string to 'print'");
|
||||
if (i>1) lua_writestring("\t", 1);
|
||||
lua_writestring(s, l);
|
||||
lua_pop(L, 1); /* pop result */
|
||||
}
|
||||
luai_writeline();
|
||||
lua_writeline();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -55,14 +55,12 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
|
||||
return NULL;
|
||||
do {
|
||||
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 */
|
||||
n = n * base + digit;
|
||||
s++;
|
||||
} while (isalnum((unsigned char)*s));
|
||||
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
||||
if (*s != '\0') /* invalid trailing characters? */
|
||||
return NULL;
|
||||
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
||||
return s;
|
||||
}
|
||||
@@ -78,7 +76,7 @@ static int luaB_tonumber (lua_State *L) {
|
||||
else {
|
||||
size_t l;
|
||||
const char *s = lua_tolstring(L, 1, &l);
|
||||
if (s != NULL && lua_strtonum(L, s) == l + 1)
|
||||
if (s != NULL && lua_stringtonumber(L, s) == l + 1)
|
||||
return 1; /* successful conversion to number */
|
||||
/* else not a number */
|
||||
}
|
||||
@@ -87,11 +85,11 @@ static int luaB_tonumber (lua_State *L) {
|
||||
size_t l;
|
||||
const char *s;
|
||||
lua_Integer n = 0; /* to avoid warnings */
|
||||
int base = luaL_checkint(L, 2);
|
||||
lua_Integer base = luaL_checkinteger(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
|
||||
s = luaL_checklstring(L, 1, &l);
|
||||
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||
if (b_str2int(s, base, &n) == s + l) {
|
||||
if (b_str2int(s, (int)base, &n) == s + l) {
|
||||
lua_pushinteger(L, n);
|
||||
return 1;
|
||||
} /* else not a number */
|
||||
@@ -102,7 +100,7 @@ static int luaB_tonumber (lua_State *L) {
|
||||
|
||||
|
||||
static int luaB_error (lua_State *L) {
|
||||
int level = luaL_optint(L, 2, 1);
|
||||
int level = (int)luaL_optinteger(L, 2, 1);
|
||||
lua_settop(L, 1);
|
||||
if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
|
||||
luaL_where(L, level);
|
||||
@@ -129,7 +127,7 @@ static int luaB_setmetatable (lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||
"nil or table expected");
|
||||
if (luaL_getmetafield(L, 1, "__metatable"))
|
||||
if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
|
||||
return luaL_error(L, "cannot change a protected metatable");
|
||||
lua_settop(L, 2);
|
||||
lua_setmetatable(L, 1);
|
||||
@@ -180,7 +178,7 @@ static int luaB_collectgarbage (lua_State *L) {
|
||||
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
|
||||
LUA_GCISRUNNING};
|
||||
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
|
||||
int ex = luaL_optint(L, 2, 0);
|
||||
int ex = (int)luaL_optinteger(L, 2, 0);
|
||||
int res = lua_gc(L, o, ex);
|
||||
switch (o) {
|
||||
case LUA_GCCOUNT: {
|
||||
@@ -212,7 +210,7 @@ static int luaB_type (lua_State *L) {
|
||||
|
||||
static int pairsmeta (lua_State *L, const char *method, int iszero,
|
||||
lua_CFunction iter) {
|
||||
if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
|
||||
if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
|
||||
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
|
||||
lua_pushcfunction(L, iter); /* will return generator, */
|
||||
lua_pushvalue(L, 1); /* state, */
|
||||
@@ -248,7 +246,7 @@ static int luaB_pairs (lua_State *L) {
|
||||
** Traversal function for 'ipairs' for raw tables
|
||||
*/
|
||||
static int ipairsaux_raw (lua_State *L) {
|
||||
int i = luaL_checkint(L, 2) + 1;
|
||||
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;
|
||||
@@ -259,17 +257,9 @@ static int ipairsaux_raw (lua_State *L) {
|
||||
** Traversal function for 'ipairs' for tables with metamethods
|
||||
*/
|
||||
static int ipairsaux (lua_State *L) {
|
||||
int i = luaL_checkint(L, 2) + 1;
|
||||
if (i > luaL_len(L, 1)) { /* larger than length? */
|
||||
lua_pushnil(L); /* end traversal */
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
lua_pushinteger(L, i);
|
||||
lua_pushinteger(L, i); /* key for indexing table */
|
||||
lua_gettable(L, 1);
|
||||
return 2;
|
||||
}
|
||||
lua_Integer i = luaL_checkinteger(L, 2) + 1;
|
||||
lua_pushinteger(L, i);
|
||||
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
|
||||
}
|
||||
|
||||
|
||||
@@ -279,13 +269,12 @@ static int ipairsaux (lua_State *L) {
|
||||
** that can affect the traversal.
|
||||
*/
|
||||
static int luaB_ipairs (lua_State *L) {
|
||||
lua_CFunction iter =
|
||||
(luaL_getmetafield(L, 1, "__len") ||
|
||||
luaL_getmetafield(L, 1, "__index"))
|
||||
? ipairsaux : ipairsaux_raw;
|
||||
lua_CFunction iter = (luaL_getmetafield(L, 1, "__index") != LUA_TNIL)
|
||||
? ipairsaux : ipairsaux_raw;
|
||||
#if defined(LUA_COMPAT_IPAIRS)
|
||||
return pairsmeta(L, "__ipairs", 1, iter);
|
||||
#else
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushcfunction(L, iter); /* iteration function */
|
||||
lua_pushvalue(L, 1); /* state */
|
||||
lua_pushinteger(L, 0); /* initial value */
|
||||
@@ -336,7 +325,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 top. Instead, it keeps its resulting string in a
|
||||
** reserved slot inside the stack.
|
||||
@@ -380,7 +369,7 @@ static int luaB_load (lua_State *L) {
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static int dofilecont (lua_State *L, int d1, lua_Ctx d2) {
|
||||
static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
|
||||
(void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
|
||||
return lua_gettop(L) - 1;
|
||||
}
|
||||
@@ -400,9 +389,10 @@ static int luaB_assert (lua_State *L) {
|
||||
if (lua_toboolean(L, 1)) /* condition is true? */
|
||||
return lua_gettop(L); /* return all arguments */
|
||||
else { /* error */
|
||||
if (lua_isnone(L, 2)) /* no error message? */
|
||||
lua_pushliteral(L, "assertion failed!"); /* use standard message */
|
||||
lua_remove(L, 1); /* remove the condition (if there is one...) */
|
||||
luaL_checkany(L, 1); /* there must be a condition */
|
||||
lua_remove(L, 1); /* remove it */
|
||||
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' */
|
||||
}
|
||||
}
|
||||
@@ -415,11 +405,11 @@ static int luaB_select (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int i = luaL_checkint(L, 1);
|
||||
lua_Integer i = luaL_checkinteger(L, 1);
|
||||
if (i < 0) i = n + i;
|
||||
else if (i > n) i = n;
|
||||
luaL_argcheck(L, 1 <= i, 1, "index out of range");
|
||||
return n - i;
|
||||
return n - (int)i;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,14 +421,14 @@ static int luaB_select (lua_State *L) {
|
||||
** 'extra' values (where 'extra' is exactly the number of items to be
|
||||
** ignored).
|
||||
*/
|
||||
static int finishpcall (lua_State *L, int status, lua_Ctx extra) {
|
||||
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
|
||||
if (status != LUA_OK && status != LUA_YIELD) { /* error? */
|
||||
lua_pushboolean(L, 0); /* first result (false) */
|
||||
lua_pushvalue(L, -2); /* error message */
|
||||
return 2; /* return false, msg */
|
||||
}
|
||||
else
|
||||
return lua_gettop(L) - extra; /* return all results */
|
||||
return lua_gettop(L) - (int)extra; /* return all results */
|
||||
}
|
||||
|
||||
|
||||
|
||||
29
lbitlib.c
29
lbitlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lbitlib.c,v 1.25 2014/03/20 19:22:16 roberto Exp roberto $
|
||||
** $Id: lbitlib.c,v 1.27 2014/10/01 11:54:56 roberto Exp roberto $
|
||||
** Standard library for bitwise operations
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,9 @@
|
||||
#define lbitlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -89,7 +92,7 @@ static int b_not (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int b_shift (lua_State *L, lua_Unsigned r, int i) {
|
||||
static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {
|
||||
if (i < 0) { /* shift right? */
|
||||
i = -i;
|
||||
r = trim(r);
|
||||
@@ -107,18 +110,18 @@ static int b_shift (lua_State *L, lua_Unsigned r, int i) {
|
||||
|
||||
|
||||
static int b_lshift (lua_State *L) {
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_rshift (lua_State *L) {
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_arshift (lua_State *L) {
|
||||
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
||||
int i = luaL_checkint(L, 2);
|
||||
lua_Integer i = luaL_checkinteger(L, 2);
|
||||
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
|
||||
return b_shift(L, r, -i);
|
||||
else { /* arithmetic shift for 'negative' number */
|
||||
@@ -131,9 +134,9 @@ static int b_arshift (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int b_rot (lua_State *L, int i) {
|
||||
static int b_rot (lua_State *L, lua_Integer d) {
|
||||
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
||||
i &= (LUA_NBITS - 1); /* i = i % NBITS */
|
||||
int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
|
||||
r = trim(r);
|
||||
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
|
||||
r = (r << i) | (r >> (LUA_NBITS - i));
|
||||
@@ -143,12 +146,12 @@ static int b_rot (lua_State *L, int i) {
|
||||
|
||||
|
||||
static int b_lrot (lua_State *L) {
|
||||
return b_rot(L, luaL_checkint(L, 2));
|
||||
return b_rot(L, luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_rrot (lua_State *L) {
|
||||
return b_rot(L, -luaL_checkint(L, 2));
|
||||
return b_rot(L, -luaL_checkinteger(L, 2));
|
||||
}
|
||||
|
||||
|
||||
@@ -159,14 +162,14 @@ static int b_rrot (lua_State *L) {
|
||||
** 'width' being used uninitialized.)
|
||||
*/
|
||||
static int fieldargs (lua_State *L, int farg, int *width) {
|
||||
int f = luaL_checkint(L, farg);
|
||||
int w = luaL_optint(L, farg + 1, 1);
|
||||
lua_Integer f = luaL_checkinteger(L, farg);
|
||||
lua_Integer w = luaL_optinteger(L, farg + 1, 1);
|
||||
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
||||
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
|
||||
if (f + w > LUA_NBITS)
|
||||
luaL_error(L, "trying to access non-existent bits");
|
||||
*width = w;
|
||||
return f;
|
||||
*width = (int)w;
|
||||
return (int)f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
110
lcode.c
110
lcode.c
@@ -1,16 +1,18 @@
|
||||
/*
|
||||
** $Id: lcode.c,v 2.89 2014/04/29 18:14:16 roberto Exp roberto $
|
||||
** $Id: lcode.c,v 2.100 2015/03/28 19:14:47 roberto Exp roberto $
|
||||
** Code generator for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lcode_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define lcode_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lcode.h"
|
||||
@@ -27,12 +29,8 @@
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
/* test for x == -0 */
|
||||
#if defined(signbit)
|
||||
#define isminuszero(x) ((x) == 0.0 && signbit(x))
|
||||
#else
|
||||
#define isminuszero(x) ((x) == 0.0 && 1.0/(x) < 0.0)
|
||||
#endif
|
||||
/* Maximum number of registers in a Lua function (must fit in 8 bits) */
|
||||
#define MAXREGS 255
|
||||
|
||||
|
||||
#define hasjumps(e) ((e)->t != (e)->f)
|
||||
@@ -107,7 +105,7 @@ static void fixjump (FuncState *fs, int pc, int dest) {
|
||||
|
||||
|
||||
/*
|
||||
** returns current `pc' and marks it as a jump target (to avoid wrong
|
||||
** returns current 'pc' and marks it as a jump target (to avoid wrong
|
||||
** optimizations with consecutive instructions not in the same basic block).
|
||||
*/
|
||||
int luaK_getlabel (FuncState *fs) {
|
||||
@@ -230,7 +228,7 @@ void luaK_concat (FuncState *fs, int *l1, int l2) {
|
||||
|
||||
static int luaK_code (FuncState *fs, Instruction i) {
|
||||
Proto *f = fs->f;
|
||||
dischargejpc(fs); /* `pc' will change */
|
||||
dischargejpc(fs); /* 'pc' will change */
|
||||
/* put new instruction in code array */
|
||||
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||
MAX_INT, "opcodes");
|
||||
@@ -280,8 +278,9 @@ int luaK_codek (FuncState *fs, int reg, int k) {
|
||||
void luaK_checkstack (FuncState *fs, int n) {
|
||||
int newstack = fs->freereg + n;
|
||||
if (newstack > fs->f->maxstacksize) {
|
||||
if (newstack >= MAXSTACK)
|
||||
luaX_syntaxerror(fs->ls, "function or expression too complex");
|
||||
if (newstack >= MAXREGS)
|
||||
luaX_syntaxerror(fs->ls,
|
||||
"function or expression needs too many registers");
|
||||
fs->f->maxstacksize = cast_byte(newstack);
|
||||
}
|
||||
}
|
||||
@@ -358,14 +357,8 @@ int luaK_intK (FuncState *fs, lua_Integer n) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Both NaN and -0.0 should not go to the constant table, as they have
|
||||
** problems with the hashing. (NaN is not a valid key, -0.0 collides
|
||||
** with +0.0.)
|
||||
*/
|
||||
static int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||
TValue o;
|
||||
lua_assert(!luai_numisnan(r) && !isminuszero(r));
|
||||
setfltvalue(&o, r);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
@@ -503,7 +496,7 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
||||
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
|
||||
discharge2reg(fs, e, reg);
|
||||
if (e->k == VJMP)
|
||||
luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
|
||||
luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
|
||||
if (hasjumps(e)) {
|
||||
int final; /* position after whole expression */
|
||||
int p_f = NO_JUMP; /* position of an eventual LOAD false */
|
||||
@@ -581,11 +574,11 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
||||
case VKFLT: {
|
||||
e->u.info = luaK_numberK(fs, e->u.nval);
|
||||
e->k = VK;
|
||||
/* go through */
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
case VK: {
|
||||
vk:
|
||||
if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
|
||||
if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */
|
||||
return RKASK(e->u.info);
|
||||
else break;
|
||||
}
|
||||
@@ -677,7 +670,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
||||
luaK_concat(fs, &e->f, pc); /* insert last jump in 'f' list */
|
||||
luaK_patchtohere(fs, e->t);
|
||||
e->t = NO_JUMP;
|
||||
}
|
||||
@@ -700,7 +693,7 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
||||
luaK_concat(fs, &e->t, pc); /* insert last jump in 't' list */
|
||||
luaK_patchtohere(fs, e->f);
|
||||
e->f = NO_JUMP;
|
||||
}
|
||||
@@ -755,20 +748,14 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
||||
** return false if folding can raise an error
|
||||
*/
|
||||
static int validop (int op, TValue *v1, TValue *v2) {
|
||||
lua_Number a, b;
|
||||
lua_Integer i;
|
||||
cast_void(a); cast_void(b); /* macro may not use its arguments */
|
||||
if (luai_numinvalidop(op, (cast_void(tonumber(v1, &a)), a),
|
||||
(cast_void(tonumber(v2, &b)), b)))
|
||||
return 0;
|
||||
switch (op) {
|
||||
case LUA_OPIDIV: /* division by 0 and conversion errors */
|
||||
return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0);
|
||||
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
|
||||
case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */
|
||||
case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
|
||||
lua_Integer i;
|
||||
return (tointeger(v1, &i) && tointeger(v2, &i));
|
||||
case LUA_OPMOD: /* integer module by 0 */
|
||||
return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
|
||||
}
|
||||
case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
|
||||
return (nvalue(v2) != 0);
|
||||
default: return 1; /* everything else is valid */
|
||||
}
|
||||
}
|
||||
@@ -781,15 +768,15 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) {
|
||||
TValue v1, v2, res;
|
||||
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
|
||||
return 0; /* non-numeric operands or not safe to fold */
|
||||
luaO_arith(fs->ls->L, op, &v1, &v2, &res);
|
||||
luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
|
||||
if (ttisinteger(&res)) {
|
||||
e1->k = VKINT;
|
||||
e1->u.ival = ivalue(&res);
|
||||
}
|
||||
else {
|
||||
else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */
|
||||
lua_Number n = fltvalue(&res);
|
||||
if (luai_numisnan(n) || isminuszero(n))
|
||||
return 0; /* folds neither NaN nor -0 */
|
||||
if (luai_numisnan(n) || n == 0)
|
||||
return 0;
|
||||
e1->k = VKFLT;
|
||||
e1->u.nval = n;
|
||||
}
|
||||
@@ -797,19 +784,30 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) {
|
||||
}
|
||||
|
||||
|
||||
static void codearith (FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
if (!constfolding(fs, op - OP_ADD + LUA_OPADD, e1, e2)) {
|
||||
/*
|
||||
** Code for binary and unary expressions that "produce values"
|
||||
** (arithmetic operations, bitwise operations, concat, length). First
|
||||
** try to do constant folding (only for numeric [arithmetic and
|
||||
** bitwise] operations, which is what 'lua_arith' accepts).
|
||||
** Expression to produce final result will be encoded in 'e1'.
|
||||
*/
|
||||
static void codeexpval (FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
lua_assert(op >= OP_ADD);
|
||||
if (op <= OP_BNOT && constfolding(fs, (op - OP_ADD) + LUA_OPADD, e1, e2))
|
||||
return; /* result has been folded */
|
||||
else {
|
||||
int o1, o2;
|
||||
if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) {
|
||||
o2 = 0;
|
||||
/* move operands to registers (if needed) */
|
||||
if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) { /* unary op? */
|
||||
o2 = 0; /* no second expression */
|
||||
o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */
|
||||
}
|
||||
else { /* regular case (binary operators) */
|
||||
o2 = luaK_exp2RK(fs, e2);
|
||||
o2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */
|
||||
o1 = luaK_exp2RK(fs, e1);
|
||||
}
|
||||
if (o1 > o2) {
|
||||
if (o1 > o2) { /* free registers in proper order */
|
||||
freeexp(fs, e1);
|
||||
freeexp(fs, e2);
|
||||
}
|
||||
@@ -817,8 +815,8 @@ static void codearith (FuncState *fs, OpCode op,
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
}
|
||||
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
|
||||
e1->k = VRELOCABLE;
|
||||
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */
|
||||
e1->k = VRELOCABLE; /* all those operations are relocable */
|
||||
luaK_fixline(fs, line);
|
||||
}
|
||||
}
|
||||
@@ -831,7 +829,7 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
|
||||
freeexp(fs, e2);
|
||||
freeexp(fs, e1);
|
||||
if (cond == 0 && op != OP_EQ) {
|
||||
int temp; /* exchange args to replace by `<' or `<=' */
|
||||
int temp; /* exchange args to replace by '<' or '<=' */
|
||||
temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
|
||||
cond = 1;
|
||||
}
|
||||
@@ -845,7 +843,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
|
||||
e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
|
||||
switch (op) {
|
||||
case OPR_MINUS: case OPR_BNOT: case OPR_LEN: {
|
||||
codearith(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line);
|
||||
codeexpval(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line);
|
||||
break;
|
||||
}
|
||||
case OPR_NOT: codenot(fs, e); break;
|
||||
@@ -865,7 +863,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
||||
break;
|
||||
}
|
||||
case OPR_CONCAT: {
|
||||
luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
|
||||
luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */
|
||||
break;
|
||||
}
|
||||
case OPR_ADD: case OPR_SUB:
|
||||
@@ -911,7 +909,7 @@ void luaK_posfix (FuncState *fs, BinOpr op,
|
||||
}
|
||||
else {
|
||||
luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
|
||||
codearith(fs, OP_CONCAT, e1, e2, line);
|
||||
codeexpval(fs, OP_CONCAT, e1, e2, line);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -919,15 +917,15 @@ void luaK_posfix (FuncState *fs, BinOpr op,
|
||||
case OPR_IDIV: case OPR_MOD: case OPR_POW:
|
||||
case OPR_BAND: case OPR_BOR: case OPR_BXOR:
|
||||
case OPR_SHL: case OPR_SHR: {
|
||||
codearith(fs, cast(OpCode, (op - OPR_ADD) + OP_ADD), e1, e2, line);
|
||||
codeexpval(fs, cast(OpCode, (op - OPR_ADD) + OP_ADD), e1, e2, line);
|
||||
break;
|
||||
}
|
||||
case OPR_EQ: case OPR_LT: case OPR_LE: {
|
||||
codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
|
||||
codecomp(fs, cast(OpCode, (op - OPR_EQ) + OP_EQ), 1, e1, e2);
|
||||
break;
|
||||
}
|
||||
case OPR_NE: case OPR_GT: case OPR_GE: {
|
||||
codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
|
||||
codecomp(fs, cast(OpCode, (op - OPR_NE) + OP_EQ), 0, e1, e2);
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
|
||||
15
lcorolib.c
15
lcorolib.c
@@ -1,16 +1,17 @@
|
||||
/*
|
||||
** $Id: lcorolib.c,v 1.5 2013/02/21 13:44:53 roberto Exp roberto $
|
||||
** $Id: lcorolib.c,v 1.8 2014/10/25 11:50:46 roberto Exp roberto $
|
||||
** Coroutine Library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define lcorolib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -19,7 +20,7 @@
|
||||
|
||||
static lua_State *getco (lua_State *L) {
|
||||
lua_State *co = lua_tothread(L, 1);
|
||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||
luaL_argcheck(L, co, 1, "thread expected");
|
||||
return co;
|
||||
}
|
||||
|
||||
@@ -65,7 +66,7 @@ static int luaB_coresume (lua_State *L) {
|
||||
else {
|
||||
lua_pushboolean(L, 1);
|
||||
lua_insert(L, -(r + 1));
|
||||
return r + 1; /* return true + `resume' returns */
|
||||
return r + 1; /* return true + 'resume' returns */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -7,6 +7,9 @@
|
||||
#define lctype_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include "lctype.h"
|
||||
|
||||
#if !LUA_USE_CTYPE /* { */
|
||||
|
||||
114
ldblib.c
114
ldblib.c
@@ -1,50 +1,40 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.138 2014/04/30 18:59:15 roberto Exp roberto $
|
||||
** $Id: ldblib.c,v 1.148 2015/01/02 12:52:22 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define ldblib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ldblib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.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;
|
||||
|
||||
|
||||
static int db_Csize (lua_State *L) {
|
||||
static struct {
|
||||
char c;
|
||||
unsigned char sz;
|
||||
} sizes[] = {
|
||||
{'I', sizeof(lua_Integer)},
|
||||
{'F', sizeof(lua_Number)},
|
||||
{'b', CHAR_BIT}, /* here is number of bits (not bytes) */
|
||||
{'h', sizeof(short)},
|
||||
{'i', sizeof(int)},
|
||||
{'l', sizeof(long)},
|
||||
{'z', sizeof(size_t)},
|
||||
{'f', sizeof(float)},
|
||||
{'d', sizeof(double)}
|
||||
};
|
||||
const char *s = luaL_checkstring(L, 1);
|
||||
int i;
|
||||
for (i = 0; i < (int)(sizeof(sizes)/sizeof(sizes[0])); i++) {
|
||||
if (*s == sizes[i].c) {
|
||||
lua_pushinteger(L, sizes[i].sz);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return luaL_argerror(L, 1, lua_pushfstring(L, "invalid option '%c'", *s));
|
||||
/*
|
||||
** If L1 != L, L1 can be in any state, and therefore there is no
|
||||
** garanties 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");
|
||||
}
|
||||
|
||||
|
||||
@@ -148,19 +138,23 @@ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
|
||||
|
||||
/*
|
||||
** 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) {
|
||||
lua_Debug ar;
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
const char *options = luaL_optstring(L, arg+2, "flnStu");
|
||||
checkstack(L, L1, 3);
|
||||
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
|
||||
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
|
||||
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
|
||||
lua_xmove(L, L1, 1);
|
||||
}
|
||||
else { /* stack level */
|
||||
if (!lua_getstack(L1, luaL_checkint(L, arg + 1), &ar)) {
|
||||
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
|
||||
lua_pushnil(L); /* level out of range */
|
||||
return 1;
|
||||
}
|
||||
@@ -201,15 +195,17 @@ static int db_getlocal (lua_State *L) {
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
lua_Debug ar;
|
||||
const char *name;
|
||||
int nvar = luaL_checkint(L, arg+2); /* local-variable index */
|
||||
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
|
||||
if (lua_isfunction(L, arg + 1)) { /* function argument? */
|
||||
lua_pushvalue(L, arg + 1); /* push function */
|
||||
lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
|
||||
return 1; /* return only name (there is no value) */
|
||||
}
|
||||
else { /* stack-level argument */
|
||||
if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
|
||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
||||
return luaL_argerror(L, arg+1, "level out of range");
|
||||
checkstack(L, L1, 1);
|
||||
name = lua_getlocal(L1, &ar, nvar);
|
||||
if (name) {
|
||||
lua_xmove(L1, L, 1); /* move local value */
|
||||
@@ -227,14 +223,21 @@ static int db_getlocal (lua_State *L) {
|
||||
|
||||
static int db_setlocal (lua_State *L) {
|
||||
int arg;
|
||||
const char *name;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
|
||||
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? */
|
||||
return luaL_argerror(L, arg+1, "level out of range");
|
||||
luaL_checkany(L, arg+3);
|
||||
lua_settop(L, arg+3);
|
||||
checkstack(L, L1, 1);
|
||||
lua_xmove(L, L1, 1);
|
||||
lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(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;
|
||||
}
|
||||
|
||||
@@ -244,7 +247,7 @@ static int db_setlocal (lua_State *L) {
|
||||
*/
|
||||
static int auxupvalue (lua_State *L, int get) {
|
||||
const char *name;
|
||||
int n = luaL_checkint(L, 2); /* upvalue index */
|
||||
int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
|
||||
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
|
||||
if (name == NULL) return 0;
|
||||
@@ -270,7 +273,7 @@ static int db_setupvalue (lua_State *L) {
|
||||
** returns its index
|
||||
*/
|
||||
static int checkupval (lua_State *L, int argf, int argnup) {
|
||||
int nup = luaL_checkint(L, argnup); /* upvalue index */
|
||||
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
|
||||
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
|
||||
luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
|
||||
"invalid upvalue index");
|
||||
@@ -295,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
|
||||
** thread (if there is one)
|
||||
@@ -309,7 +305,7 @@ static int db_upvaluejoin (lua_State *L) {
|
||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||
static const char *const hooknames[] =
|
||||
{"call", "return", "line", "count", "tail call"};
|
||||
gethooktable(L);
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||
lua_pushthread(L);
|
||||
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
||||
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
||||
@@ -359,19 +355,23 @@ static int db_sethook (lua_State *L) {
|
||||
else {
|
||||
const char *smask = luaL_checkstring(L, arg+2);
|
||||
luaL_checktype(L, arg+1, LUA_TFUNCTION);
|
||||
count = luaL_optint(L, arg+3, 0);
|
||||
count = (int)luaL_optinteger(L, arg + 3, 0);
|
||||
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_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
||||
}
|
||||
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key */
|
||||
lua_pushvalue(L, arg+1); /* value */
|
||||
checkstack(L, L1, 1);
|
||||
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_sethook(L1, func, mask, count); /* set hooks */
|
||||
lua_sethook(L1, func, mask, count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -382,10 +382,13 @@ static int db_gethook (lua_State *L) {
|
||||
char buff[5];
|
||||
int mask = lua_gethookmask(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");
|
||||
else {
|
||||
gethooktable(L);
|
||||
else { /* hook table must exist */
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||
checkstack(L, L1, 1);
|
||||
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
||||
lua_remove(L, -2); /* remove hook table */
|
||||
@@ -399,13 +402,13 @@ static int db_gethook (lua_State *L) {
|
||||
static int db_debug (lua_State *L) {
|
||||
for (;;) {
|
||||
char buffer[250];
|
||||
luai_writestringerror("%s", "lua_debug> ");
|
||||
lua_writestringerror("%s", "lua_debug> ");
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||
strcmp(buffer, "cont\n") == 0)
|
||||
return 0;
|
||||
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
||||
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 */
|
||||
}
|
||||
}
|
||||
@@ -418,7 +421,7 @@ static int db_traceback (lua_State *L) {
|
||||
if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
|
||||
lua_pushvalue(L, arg + 1); /* return it untouched */
|
||||
else {
|
||||
int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0);
|
||||
int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
|
||||
luaL_traceback(L, L1, msg, level);
|
||||
}
|
||||
return 1;
|
||||
@@ -426,7 +429,6 @@ static int db_traceback (lua_State *L) {
|
||||
|
||||
|
||||
static const luaL_Reg dblib[] = {
|
||||
{"Csize", db_Csize},
|
||||
{"debug", db_debug},
|
||||
{"getuservalue", db_getuservalue},
|
||||
{"gethook", db_gethook},
|
||||
|
||||
149
ldebug.c
149
ldebug.c
@@ -1,18 +1,19 @@
|
||||
/*
|
||||
** $Id: ldebug.c,v 2.99 2014/07/17 12:30:53 roberto Exp roberto $
|
||||
** $Id: ldebug.c,v 2.114 2015/03/28 19:14:47 roberto Exp roberto $
|
||||
** Debug Interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define ldebug_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define ldebug_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -33,6 +34,10 @@
|
||||
#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);
|
||||
|
||||
|
||||
@@ -47,6 +52,22 @@ static int currentline (CallInfo *ci) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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 asynchronous (e.g. during a signal)
|
||||
*/
|
||||
@@ -105,7 +126,7 @@ static const char *upvalname (Proto *p, int uv) {
|
||||
|
||||
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
||||
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 */
|
||||
else {
|
||||
*pos = ci->func + nparams + n;
|
||||
@@ -143,6 +164,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) {
|
||||
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? */
|
||||
name = NULL;
|
||||
@@ -150,25 +172,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);
|
||||
}
|
||||
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);
|
||||
if (name) {
|
||||
setobj2s(L, L->top, pos);
|
||||
api_incr_top(L);
|
||||
}
|
||||
}
|
||||
swapextra(L);
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
StkId pos = NULL; /* to avoid warnings */
|
||||
const char *name;
|
||||
lua_lock(L);
|
||||
if (name)
|
||||
swapextra(L);
|
||||
name = findlocal(L, ar->i_ci, n, &pos);
|
||||
if (name) {
|
||||
setobjs2s(L, pos, L->top - 1);
|
||||
L->top--; /* pop value */
|
||||
L->top--; /* pop value */
|
||||
}
|
||||
swapextra(L);
|
||||
lua_unlock(L);
|
||||
return name;
|
||||
}
|
||||
@@ -268,10 +295,11 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
CallInfo *ci;
|
||||
StkId func;
|
||||
lua_lock(L);
|
||||
swapextra(L);
|
||||
if (*what == '>') {
|
||||
ci = NULL;
|
||||
func = L->top - 1;
|
||||
api_check(ttisfunction(func), "function expected");
|
||||
api_check(L, ttisfunction(func), "function expected");
|
||||
what++; /* skip the '>' */
|
||||
L->top--; /* pop function */
|
||||
}
|
||||
@@ -286,6 +314,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||
setobjs2s(L, L->top, func);
|
||||
api_incr_top(L);
|
||||
}
|
||||
swapextra(L); /* correct before option 'L', which can raise a mem. error */
|
||||
if (strchr(what, 'L'))
|
||||
collectvalidlines(L, cl);
|
||||
lua_unlock(L);
|
||||
@@ -365,7 +394,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) {
|
||||
case OP_JMP: {
|
||||
int b = GETARG_sBx(i);
|
||||
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 (dest > jmptarget)
|
||||
jmptarget = dest; /* update 'jmptarget' */
|
||||
@@ -437,10 +466,14 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
|
||||
|
||||
|
||||
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 */
|
||||
int pc = currentpc(ci); /* calling instruction index */
|
||||
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)) {
|
||||
case OP_CALL:
|
||||
case OP_TAILCALL: /* get function name */
|
||||
@@ -450,26 +483,27 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
||||
return "for iterator";
|
||||
}
|
||||
/* all other instructions can call only through metamethods */
|
||||
case OP_SELF:
|
||||
case OP_GETTABUP:
|
||||
case OP_GETTABLE: tm = TM_INDEX; break;
|
||||
case OP_SETTABUP:
|
||||
case OP_SETTABLE: tm = TM_NEWINDEX; break;
|
||||
case OP_EQ: tm = TM_EQ; break;
|
||||
case OP_ADD: tm = TM_ADD; break;
|
||||
case OP_SUB: tm = TM_SUB; break;
|
||||
case OP_MUL: tm = TM_MUL; break;
|
||||
case OP_DIV: tm = TM_DIV; break;
|
||||
case OP_IDIV: tm = TM_IDIV; break;
|
||||
case OP_MOD: tm = TM_MOD; break;
|
||||
case OP_POW: tm = TM_POW; break;
|
||||
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
|
||||
tm = TM_INDEX;
|
||||
break;
|
||||
case OP_SETTABUP: case OP_SETTABLE:
|
||||
tm = TM_NEWINDEX;
|
||||
break;
|
||||
case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
|
||||
case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
|
||||
case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
|
||||
int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */
|
||||
tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */
|
||||
break;
|
||||
}
|
||||
case OP_UNM: tm = TM_UNM; break;
|
||||
case OP_BNOT: tm = TM_BNOT; 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_LE: tm = TM_LE; break;
|
||||
case OP_CONCAT: tm = TM_CONCAT; break;
|
||||
default:
|
||||
return NULL; /* else no useful name can be found */
|
||||
default: lua_assert(0); /* other instructions cannot call a function */
|
||||
}
|
||||
*name = getstr(G(L)->tmname[tm]);
|
||||
return "metamethod";
|
||||
@@ -480,17 +514,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
|
||||
** (used only for error messages, so efficiency is not a big concern)
|
||||
** The subtraction of two potentially unrelated pointers is
|
||||
** 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) {
|
||||
StkId p;
|
||||
for (p = ci->u.l.base; p < ci->top; p++)
|
||||
if (o == p) return 1;
|
||||
return 0;
|
||||
ptrdiff_t i = o - ci->u.l.base;
|
||||
return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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,
|
||||
const char **name) {
|
||||
LClosure *c = ci_func(ci);
|
||||
@@ -506,7 +544,7 @@ static const char *getupvalname (CallInfo *ci, 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;
|
||||
const char *kind = NULL;
|
||||
if (isLua(ci)) {
|
||||
@@ -515,7 +553,7 @@ static const char *varinfo (lua_State *L, const TValue *o) {
|
||||
kind = getobjname(ci_func(ci)->p, currentpc(ci),
|
||||
cast_int(o - ci->u.l.base), &name);
|
||||
}
|
||||
return (kind) ? luaO_pushfstring(L, " (%s " LUA_QS ")", kind, name) : "";
|
||||
return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : "";
|
||||
}
|
||||
|
||||
|
||||
@@ -531,11 +569,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;
|
||||
if (!tonumber(p1, &temp)) /* first operand is wrong? */
|
||||
p2 = p1; /* now second is wrong */
|
||||
luaG_typeerror(L, p2, "perform arithmetic on");
|
||||
luaG_typeerror(L, p2, msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -560,29 +599,25 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
}
|
||||
|
||||
|
||||
static void addinfo (lua_State *L, const char *msg) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (isLua(ci)) { /* is Lua code? */
|
||||
char buff[LUA_IDSIZE]; /* add file:line information */
|
||||
int line = currentline(ci);
|
||||
TString *src = ci_func(ci)->p->source;
|
||||
if (src)
|
||||
luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
|
||||
else { /* no source available; use "?" instead */
|
||||
buff[0] = '?'; buff[1] = '\0';
|
||||
}
|
||||
luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
||||
/* add src:line information to 'msg' */
|
||||
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
|
||||
int line) {
|
||||
char buff[LUA_IDSIZE];
|
||||
if (src)
|
||||
luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
|
||||
else { /* no source available; use "?" instead */
|
||||
buff[0] = '?'; buff[1] = '\0';
|
||||
}
|
||||
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
||||
}
|
||||
|
||||
|
||||
l_noret luaG_errormsg (lua_State *L) {
|
||||
if (L->errfunc != 0) { /* is there an error handling function? */
|
||||
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 - 1, errfunc); /* push function */
|
||||
L->top++;
|
||||
L->top++; /* assume EXTRA_STACK */
|
||||
luaD_call(L, L->top - 2, 1, 0); /* call it */
|
||||
}
|
||||
luaD_throw(L, LUA_ERRRUN);
|
||||
@@ -590,10 +625,14 @@ l_noret luaG_errormsg (lua_State *L) {
|
||||
|
||||
|
||||
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||
CallInfo *ci = L->ci;
|
||||
const char *msg;
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
addinfo(L, luaO_pushvfstring(L, fmt, argp));
|
||||
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -17,21 +17,21 @@
|
||||
|
||||
#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,
|
||||
const char *opname);
|
||||
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2,
|
||||
const char *msg);
|
||||
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
||||
const TValue *p2);
|
||||
LUAI_FUNC l_noret luaG_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 void luaG_traceexec (lua_State *L);
|
||||
|
||||
|
||||
79
ldo.c
79
ldo.c
@@ -1,17 +1,19 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 2.125 2014/07/15 21:26:50 roberto Exp roberto $
|
||||
** $Id: ldo.c,v 2.137 2015/03/30 16:05:23 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define ldo_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ldo_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -67,7 +69,7 @@
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* ANSI handling with long jumps */
|
||||
/* ISO C handling with long jumps */
|
||||
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
|
||||
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
|
||||
#define luai_jmpbuf jmp_buf
|
||||
@@ -111,15 +113,19 @@ l_noret luaD_throw (lua_State *L, int errcode) {
|
||||
LUAI_THROW(L, L->errorJmp); /* jump to it */
|
||||
}
|
||||
else { /* thread has no error handler */
|
||||
global_State *g = G(L);
|
||||
L->status = cast_byte(errcode); /* mark it as dead */
|
||||
if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */
|
||||
setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */
|
||||
luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */
|
||||
if (g->mainthread->errorJmp) { /* main thread has a handler? */
|
||||
setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */
|
||||
luaD_throw(g->mainthread, errcode); /* re-throw in main thread */
|
||||
}
|
||||
else { /* no handler at all; abort */
|
||||
if (G(L)->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);
|
||||
G(L)->panic(L); /* call it (last chance to jump out) */
|
||||
g->panic(L); /* call panic function (last chance to jump out) */
|
||||
}
|
||||
abort();
|
||||
}
|
||||
@@ -280,18 +286,21 @@ static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
StkId p;
|
||||
ptrdiff_t funcr = savestack(L, func);
|
||||
if (!ttisfunction(tm))
|
||||
luaG_typeerror(L, func, "call");
|
||||
/* Open a hole inside the stack at `func' */
|
||||
for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
|
||||
incr_top(L);
|
||||
func = restorestack(L, funcr); /* previous call may change stack */
|
||||
/* Open a hole inside the stack at 'func' */
|
||||
for (p = L->top; p > func; p--)
|
||||
setobjs2s(L, p, p-1);
|
||||
L->top++; /* slot ensured by caller */
|
||||
setobj2s(L, func, tm); /* tag method is the new function to be called */
|
||||
return func;
|
||||
}
|
||||
|
||||
|
||||
@@ -314,6 +323,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
case LUA_TCCL: { /* C closure */
|
||||
f = clCvalue(func)->f;
|
||||
Cfunc:
|
||||
luaC_checkGC(L); /* stack grow uses memory */
|
||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||
ci = next_ci(L); /* now 'enter' new function */
|
||||
ci->nresults = nresults;
|
||||
@@ -321,20 +331,20 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
ci->top = L->top + LUA_MINSTACK;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
ci->callstatus = 0;
|
||||
luaC_checkGC(L); /* stack grow uses memory */
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
luaD_hook(L, LUA_HOOKCALL, -1);
|
||||
lua_unlock(L);
|
||||
n = (*f)(L); /* do the actual call */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
luaD_poscall(L, L->top - n);
|
||||
luaD_poscall(L, L->top - n, n);
|
||||
return 1;
|
||||
}
|
||||
case LUA_TLCL: { /* Lua function: prepare its call */
|
||||
StkId base;
|
||||
Proto *p = clLvalue(func)->p;
|
||||
n = cast_int(L->top - func) - 1; /* number of real arguments */
|
||||
luaC_checkGC(L); /* stack grow uses memory */
|
||||
luaD_checkstack(L, p->maxstacksize);
|
||||
for (; n < p->numparams; n++)
|
||||
setnilvalue(L->top++); /* complete missing arguments */
|
||||
@@ -355,20 +365,21 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
||||
ci->u.l.savedpc = p->code; /* starting point */
|
||||
ci->callstatus = CIST_LUA;
|
||||
L->top = ci->top;
|
||||
luaC_checkGC(L); /* stack grow uses memory */
|
||||
if (L->hookmask & LUA_MASKCALL)
|
||||
callhook(L, ci);
|
||||
return 0;
|
||||
}
|
||||
default: { /* not a function */
|
||||
func = tryfuncTM(L, func); /* retry with 'function' tag method */
|
||||
luaD_checkstack(L, 1); /* ensure space for metamethod */
|
||||
func = restorestack(L, funcr); /* previous call may change stack */
|
||||
tryfuncTM(L, func); /* try to get '__call' metamethod */
|
||||
return luaD_precall(L, func, nresults); /* now it must be a function */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaD_poscall (lua_State *L, StkId firstResult) {
|
||||
int luaD_poscall (lua_State *L, StkId firstResult, int nres) {
|
||||
StkId res;
|
||||
int wanted, i;
|
||||
CallInfo *ci = L->ci;
|
||||
@@ -382,9 +393,9 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
|
||||
}
|
||||
res = ci->func; /* res == final position of 1st result */
|
||||
wanted = ci->nresults;
|
||||
L->ci = ci = ci->previous; /* back to caller */
|
||||
L->ci = ci->previous; /* back to caller */
|
||||
/* move results to correct place */
|
||||
for (i = wanted; i != 0 && firstResult < L->top; i--)
|
||||
for (i = wanted; i != 0 && nres-- > 0; i--)
|
||||
setobjs2s(L, res++, firstResult++);
|
||||
while (i-- > 0)
|
||||
setnilvalue(res++);
|
||||
@@ -438,7 +449,7 @@ static void finishCcall (lua_State *L, int status) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
/* finish 'luaD_precall' */
|
||||
luaD_poscall(L, L->top - n);
|
||||
luaD_poscall(L, L->top - n, n);
|
||||
}
|
||||
|
||||
|
||||
@@ -522,7 +533,8 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
|
||||
*/
|
||||
static void resume (lua_State *L, void *ud) {
|
||||
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;
|
||||
if (nCcalls >= LUAI_MAXCCALLS)
|
||||
resume_error(L, "C stack overflow", firstArg);
|
||||
@@ -542,14 +554,13 @@ static void resume (lua_State *L, void *ud) {
|
||||
luaV_execute(L); /* just continue running Lua code */
|
||||
else { /* 'common' yield */
|
||||
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
|
||||
int n;
|
||||
lua_unlock(L);
|
||||
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
firstArg = L->top - n; /* yield results come from continuation */
|
||||
}
|
||||
luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
|
||||
luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */
|
||||
}
|
||||
unroll(L, NULL); /* run continuation */
|
||||
}
|
||||
@@ -565,7 +576,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
||||
L->nCcalls = (from) ? from->nCcalls + 1 : 1;
|
||||
L->nny = 0; /* allow yields */
|
||||
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'? */
|
||||
status = LUA_ERRRUN;
|
||||
else { /* continue running after recoverable errors */
|
||||
@@ -574,7 +585,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
||||
status = luaD_rawrunprotected(L, unroll, &status);
|
||||
}
|
||||
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 */
|
||||
L->ci->top = L->top;
|
||||
}
|
||||
@@ -593,7 +604,7 @@ LUA_API int lua_isyieldable (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_yieldk (lua_State *L, int nresults, lua_Ctx ctx,
|
||||
LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
||||
lua_KFunction k) {
|
||||
CallInfo *ci = L->ci;
|
||||
luai_userstateyield(L, nresults);
|
||||
@@ -608,7 +619,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_Ctx ctx,
|
||||
L->status = LUA_YIELD;
|
||||
ci->extra = savestack(L, ci->func); /* save current 'func' */
|
||||
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 {
|
||||
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
|
||||
@@ -649,7 +660,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
/*
|
||||
** Execute a protected parser.
|
||||
*/
|
||||
struct SParser { /* data to `f_parser' */
|
||||
struct SParser { /* data to 'f_parser' */
|
||||
ZIO *z;
|
||||
Mbuffer buff; /* dynamic structure used by the scanner */
|
||||
Dyndata dyd; /* dynamic structures used by the parser */
|
||||
@@ -661,7 +672,7 @@ struct SParser { /* data to `f_parser' */
|
||||
static void checkmode (lua_State *L, const char *mode, const char *x) {
|
||||
if (mode && strchr(mode, x[0]) == NULL) {
|
||||
luaO_pushfstring(L,
|
||||
"attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
|
||||
"attempt to load a %s chunk (mode is '%s')", x, mode);
|
||||
luaD_throw(L, LUA_ERRSYNTAX);
|
||||
}
|
||||
}
|
||||
|
||||
6
ldo.h
6
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.21 2014/10/25 11:50:46 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -23,7 +23,7 @@
|
||||
#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);
|
||||
|
||||
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||
@@ -34,7 +34,7 @@ LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults,
|
||||
int allowyield);
|
||||
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||
ptrdiff_t oldtop, ptrdiff_t ef);
|
||||
LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult);
|
||||
LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres);
|
||||
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
||||
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
||||
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
||||
|
||||
14
ldump.c
14
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.35 2015/01/16 16:54:37 roberto Exp roberto $
|
||||
** save precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define ldump_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lobject.h"
|
||||
@@ -71,14 +74,15 @@ static void DumpString (const TString *s, DumpState *D) {
|
||||
if (s == NULL)
|
||||
DumpByte(0, D);
|
||||
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)
|
||||
DumpByte(cast_int(size), D);
|
||||
else {
|
||||
DumpByte(0xFF, 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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define lfunc_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lfunc.h"
|
||||
@@ -82,7 +84,7 @@ void luaF_close (lua_State *L, StkId level) {
|
||||
UpVal *uv;
|
||||
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
|
||||
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? */
|
||||
luaM_free(L, uv); /* free upvalue */
|
||||
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.
|
||||
*/
|
||||
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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -22,6 +22,13 @@
|
||||
#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
|
||||
*/
|
||||
|
||||
204
lgc.c
204
lgc.c
@@ -1,14 +1,17 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 2.191 2014/07/19 15:14:46 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 2.204 2015/03/04 13:51:55 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lgc_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
@@ -80,8 +83,13 @@
|
||||
#define markvalue(g,o) { checkconsistency(o); \
|
||||
if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
|
||||
|
||||
#define markobject(g,t) \
|
||||
{ if ((t) && iswhite(t)) reallymarkobject(g, obj2gco(t)); }
|
||||
#define markobject(g,t) { if (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);
|
||||
|
||||
@@ -100,9 +108,9 @@ static void reallymarkobject (global_State *g, GCObject *o);
|
||||
|
||||
|
||||
/*
|
||||
** link table 'h' into list pointed by 'p'
|
||||
** link collectable object 'o' into list pointed by 'p'
|
||||
*/
|
||||
#define linktable(h,p) ((h)->gclist = *(p), *(p) = obj2gco(h))
|
||||
#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o))
|
||||
|
||||
|
||||
/*
|
||||
@@ -119,14 +127,14 @@ static void removeentry (Node *n) {
|
||||
/*
|
||||
** tells whether a key or value can be cleared from a 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
|
||||
** being finalized, keep them in keys, but not in values
|
||||
*/
|
||||
static int iscleared (global_State *g, const TValue *o) {
|
||||
if (!iscollectable(o)) return 0;
|
||||
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;
|
||||
}
|
||||
else return iswhite(gcvalue(o));
|
||||
@@ -159,8 +167,7 @@ void luaC_barrierback_ (lua_State *L, Table *t) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(isblack(t) && !isdead(g, t));
|
||||
black2gray(t); /* make table gray (again) */
|
||||
t->gclist = g->grayagain;
|
||||
g->grayagain = obj2gco(t);
|
||||
linkgclist(t, g->grayagain);
|
||||
}
|
||||
|
||||
|
||||
@@ -224,15 +231,19 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
||||
reentry:
|
||||
white2gray(o);
|
||||
switch (o->tt) {
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TSHRSTR: {
|
||||
gray2black(o);
|
||||
g->GCmemtrav += sizelstring(gco2ts(o)->shrlen);
|
||||
break;
|
||||
}
|
||||
case LUA_TLNGSTR: {
|
||||
gray2black(o);
|
||||
g->GCmemtrav += sizestring(gco2ts(o));
|
||||
g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen);
|
||||
break;
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
TValue uvalue;
|
||||
markobject(g, gco2u(o)->metatable); /* mark its metatable */
|
||||
markobjectN(g, gco2u(o)->metatable); /* mark its metatable */
|
||||
gray2black(o);
|
||||
g->GCmemtrav += sizeudata(gco2u(o));
|
||||
getuservalue(g->mainthread, gco2u(o), &uvalue);
|
||||
@@ -243,27 +254,23 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
||||
break;
|
||||
}
|
||||
case LUA_TLCL: {
|
||||
gco2lcl(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
linkgclist(gco2lcl(o), g->gray);
|
||||
break;
|
||||
}
|
||||
case LUA_TCCL: {
|
||||
gco2ccl(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
linkgclist(gco2ccl(o), g->gray);
|
||||
break;
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
linktable(gco2t(o), &g->gray);
|
||||
linkgclist(gco2t(o), g->gray);
|
||||
break;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
gco2th(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
linkgclist(gco2th(o), g->gray);
|
||||
break;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
gco2p(o)->gclist = g->gray;
|
||||
g->gray = o;
|
||||
linkgclist(gco2p(o), g->gray);
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0); break;
|
||||
@@ -277,7 +284,7 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
||||
static void markmt (global_State *g) {
|
||||
int i;
|
||||
for (i=0; i < LUA_NUMTAGS; i++)
|
||||
markobject(g, g->mt[i]);
|
||||
markobjectN(g, g->mt[i]);
|
||||
}
|
||||
|
||||
|
||||
@@ -340,12 +347,18 @@ static void restartcollection (global_State *g) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
** Traverse a table with weak values and link it to proper list. During
|
||||
** propagate phase, keep it in 'grayagain' list, to be revisited in the
|
||||
** atomic phase. In the atomic phase, if table has any white value,
|
||||
** put it in 'weak' list, to be cleared.
|
||||
*/
|
||||
static void traverseweakvalue (global_State *g, Table *h) {
|
||||
Node *n, *limit = gnodelast(h);
|
||||
/* if there is array part, assume it may have white values (do not
|
||||
traverse it just to check) */
|
||||
/* if there is array part, assume it may have white values (it is not
|
||||
worth traversing it now just to check) */
|
||||
int hasclears = (h->sizearray > 0);
|
||||
for (n = gnode(h, 0); n < limit; n++) {
|
||||
for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */
|
||||
checkdeadkey(n);
|
||||
if (ttisnil(gval(n))) /* entry is empty? */
|
||||
removeentry(n); /* remove it */
|
||||
@@ -356,20 +369,30 @@ static void traverseweakvalue (global_State *g, Table *h) {
|
||||
hasclears = 1; /* table will have to be cleared */
|
||||
}
|
||||
}
|
||||
if (hasclears)
|
||||
linktable(h, &g->weak); /* has to be cleared later */
|
||||
else /* no white values */
|
||||
linktable(h, &g->grayagain); /* no need to clean */
|
||||
if (g->gcstate == GCSpropagate)
|
||||
linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */
|
||||
else if (hasclears)
|
||||
linkgclist(h, g->weak); /* has to be cleared later */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traverse an ephemeron table and link it to proper list. Returns true
|
||||
** iff any object was marked during this traversal (which implies that
|
||||
** convergence has to continue). During propagation phase, keep table
|
||||
** in 'grayagain' list, to be visited again in the atomic phase. In
|
||||
** the atomic phase, if table has any white->white entry, it has to
|
||||
** be revisited during ephemeron convergence (as that key may turn
|
||||
** black). Otherwise, if it has any white key, table has to be cleared
|
||||
** (in the atomic phase).
|
||||
*/
|
||||
static int traverseephemeron (global_State *g, Table *h) {
|
||||
int marked = 0; /* true if an object is marked in this traversal */
|
||||
int hasclears = 0; /* true if table has white keys */
|
||||
int prop = 0; /* true if table has entry "white-key -> white-value" */
|
||||
int hasww = 0; /* true if table has entry "white-key -> white-value" */
|
||||
Node *n, *limit = gnodelast(h);
|
||||
int i;
|
||||
/* traverse array part (numeric keys are 'strong') */
|
||||
unsigned int i;
|
||||
/* traverse array part */
|
||||
for (i = 0; i < h->sizearray; i++) {
|
||||
if (valiswhite(&h->array[i])) {
|
||||
marked = 1;
|
||||
@@ -384,26 +407,27 @@ static int traverseephemeron (global_State *g, Table *h) {
|
||||
else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */
|
||||
hasclears = 1; /* table must be cleared */
|
||||
if (valiswhite(gval(n))) /* value not marked yet? */
|
||||
prop = 1; /* must propagate again */
|
||||
hasww = 1; /* white-white entry */
|
||||
}
|
||||
else if (valiswhite(gval(n))) { /* value not marked yet? */
|
||||
marked = 1;
|
||||
reallymarkobject(g, gcvalue(gval(n))); /* mark it now */
|
||||
}
|
||||
}
|
||||
if (prop)
|
||||
linktable(h, &g->ephemeron); /* have to propagate again */
|
||||
else if (hasclears) /* does table have white keys? */
|
||||
linktable(h, &g->allweak); /* may have to clean white keys */
|
||||
else /* no white keys */
|
||||
linktable(h, &g->grayagain); /* no need to clean */
|
||||
/* link table into proper list */
|
||||
if (g->gcstate == GCSpropagate)
|
||||
linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */
|
||||
else if (hasww) /* table has white->white entries? */
|
||||
linkgclist(h, g->ephemeron); /* have to propagate again */
|
||||
else if (hasclears) /* table has white keys? */
|
||||
linkgclist(h, g->allweak); /* may have to clean white keys */
|
||||
return marked;
|
||||
}
|
||||
|
||||
|
||||
static void traversestrongtable (global_State *g, Table *h) {
|
||||
Node *n, *limit = gnodelast(h);
|
||||
int i;
|
||||
unsigned int i;
|
||||
for (i = 0; i < h->sizearray; i++) /* traverse array part */
|
||||
markvalue(g, &h->array[i]);
|
||||
for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */
|
||||
@@ -422,7 +446,7 @@ static void traversestrongtable (global_State *g, Table *h) {
|
||||
static lu_mem traversetable (global_State *g, Table *h) {
|
||||
const char *weakkey, *weakvalue;
|
||||
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? */
|
||||
((weakkey = strchr(svalue(mode), 'k')),
|
||||
(weakvalue = strchr(svalue(mode), 'v')),
|
||||
@@ -433,7 +457,7 @@ static lu_mem traversetable (global_State *g, Table *h) {
|
||||
else if (!weakvalue) /* strong values? */
|
||||
traverseephemeron(g, h);
|
||||
else /* all weak */
|
||||
linktable(h, &g->allweak); /* nothing to traverse now */
|
||||
linkgclist(h, g->allweak); /* nothing to traverse now */
|
||||
}
|
||||
else /* not weak */
|
||||
traversestrongtable(g, h);
|
||||
@@ -442,19 +466,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) {
|
||||
int i;
|
||||
if (f->cache && iswhite(f->cache))
|
||||
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 */
|
||||
markvalue(g, &f->k[i]);
|
||||
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 */
|
||||
markobject(g, f->p[i]);
|
||||
markobjectN(g, f->p[i]);
|
||||
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 +
|
||||
sizeof(Proto *) * f->sizep +
|
||||
sizeof(TValue) * f->sizek +
|
||||
@@ -479,7 +508,7 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
||||
*/
|
||||
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
||||
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 */
|
||||
UpVal *uv = cl->upvals[i];
|
||||
if (uv != NULL) {
|
||||
@@ -548,8 +577,7 @@ static void propagatemark (global_State *g) {
|
||||
case LUA_TTHREAD: {
|
||||
lua_State *th = gco2th(o);
|
||||
g->gray = th->gclist; /* remove from 'gray' list */
|
||||
th->gclist = g->grayagain;
|
||||
g->grayagain = o; /* insert into 'grayagain' list */
|
||||
linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
|
||||
black2gray(o);
|
||||
size = traversethread(g, th);
|
||||
break;
|
||||
@@ -571,35 +599,12 @@ static void propagateall (global_State *g) {
|
||||
}
|
||||
|
||||
|
||||
static void propagatelist (global_State *g, GCObject *l) {
|
||||
lua_assert(g->gray == NULL); /* no grays left */
|
||||
g->gray = l;
|
||||
propagateall(g); /* traverse all elements from 'l' */
|
||||
}
|
||||
|
||||
/*
|
||||
** retraverse all gray lists. Because tables may be reinserted in other
|
||||
** lists when traversed, traverse the original lists to avoid traversing
|
||||
** twice the same table (which is not wrong, but inefficient)
|
||||
*/
|
||||
static void retraversegrays (global_State *g) {
|
||||
GCObject *weak = g->weak; /* save original lists */
|
||||
GCObject *grayagain = g->grayagain;
|
||||
GCObject *ephemeron = g->ephemeron;
|
||||
g->weak = g->grayagain = g->ephemeron = NULL;
|
||||
propagateall(g); /* traverse main gray list */
|
||||
propagatelist(g, grayagain);
|
||||
propagatelist(g, weak);
|
||||
propagatelist(g, ephemeron);
|
||||
}
|
||||
|
||||
|
||||
static void convergeephemerons (global_State *g) {
|
||||
int changed;
|
||||
do {
|
||||
GCObject *w;
|
||||
GCObject *next = g->ephemeron; /* get ephemeron list */
|
||||
g->ephemeron = NULL; /* tables will return to this list when traversed */
|
||||
g->ephemeron = NULL; /* tables may return to this list when traversed */
|
||||
changed = 0;
|
||||
while ((w = next) != NULL) {
|
||||
next = gco2t(w)->gclist;
|
||||
@@ -647,7 +652,7 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) {
|
||||
for (; l != f; l = gco2t(l)->gclist) {
|
||||
Table *h = gco2t(l);
|
||||
Node *n, *limit = gnodelast(h);
|
||||
int i;
|
||||
unsigned int i;
|
||||
for (i = 0; i < h->sizearray; i++) {
|
||||
TValue *o = &h->array[i];
|
||||
if (iscleared(g, o)) /* value was collected? */
|
||||
@@ -698,9 +703,10 @@ static void freeobj (lua_State *L, GCObject *o) {
|
||||
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
||||
case LUA_TSHRSTR:
|
||||
luaS_remove(L, gco2ts(o)); /* remove it from hash table */
|
||||
/* go through */
|
||||
luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen));
|
||||
break;
|
||||
case LUA_TLNGSTR: {
|
||||
luaM_freemem(L, o, sizestring(gco2ts(o)));
|
||||
luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen));
|
||||
break;
|
||||
}
|
||||
default: lua_assert(0);
|
||||
@@ -714,10 +720,10 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count);
|
||||
|
||||
/*
|
||||
** 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"
|
||||
** (non current) white and not fixed; change all non-dead objects back
|
||||
** to white, preparing for next collection cycle.
|
||||
** When object is a thread, sweep its list of open upvalues too.
|
||||
** objects, where a dead object is one marked with the old (non current)
|
||||
** white; change all non-dead objects back to white, preparing for next
|
||||
** collection cycle. Return where to continue the traversal or NULL if
|
||||
** list is finished.
|
||||
*/
|
||||
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||
global_State *g = G(L);
|
||||
@@ -730,7 +736,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||
*p = curr->next; /* remove 'curr' from list */
|
||||
freeobj(L, curr); /* erase 'curr' */
|
||||
}
|
||||
else { /* update marks */
|
||||
else { /* change mark to 'white' */
|
||||
curr->marked = cast_byte((marked & maskcolors) | white);
|
||||
p = &curr->next; /* go to next element */
|
||||
}
|
||||
@@ -864,7 +870,7 @@ static GCObject **findlast (GCObject **p) {
|
||||
|
||||
/*
|
||||
** move all unreachable objects (or 'all' objects) that need
|
||||
** finalization from list 'p' to list 'tobefnz' (to be finalized)
|
||||
** finalization from list 'finobj' to list 'tobefnz' (to be finalized)
|
||||
*/
|
||||
static void separatetobefnz (global_State *g, int all) {
|
||||
GCObject *curr;
|
||||
@@ -875,7 +881,7 @@ static void separatetobefnz (global_State *g, int all) {
|
||||
if (!(iswhite(curr) || all)) /* not being collected? */
|
||||
p = &curr->next; /* don't bother with it */
|
||||
else {
|
||||
*p = curr->next; /* remove 'curr' from "fin" list */
|
||||
*p = curr->next; /* remove 'curr' from 'finobj' list */
|
||||
curr->next = *lastnext; /* link at the end of 'tobefnz' list */
|
||||
*lastnext = curr;
|
||||
lastnext = &curr->next;
|
||||
@@ -903,7 +909,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
||||
/* search for pointer pointing to 'o' */
|
||||
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
|
||||
*p = o->next; /* remove 'o' from 'allgc' list */
|
||||
o->next = g->finobj; /* link it in "fin" list */
|
||||
o->next = g->finobj; /* link it in 'finobj' list */
|
||||
g->finobj = o;
|
||||
l_setbit(o->marked, FINALIZEDBIT); /* mark it as such */
|
||||
}
|
||||
@@ -921,12 +927,15 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
||||
|
||||
|
||||
/*
|
||||
** set a reasonable "time" to wait before starting a new GC cycle;
|
||||
** cycle will start when memory use hits threshold
|
||||
** Set a reasonable "time" to wait before starting a new GC cycle; cycle
|
||||
** 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) {
|
||||
l_mem threshold, debt;
|
||||
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
|
||||
lua_assert(estimate > 0);
|
||||
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
|
||||
? estimate * g->gcpause /* no overflow */
|
||||
: MAX_LMEM; /* overflow; truncate to maximum */
|
||||
@@ -972,19 +981,21 @@ static l_mem atomic (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
l_mem work;
|
||||
GCObject *origweak, *origall;
|
||||
g->GCmemtrav = 0; /* start counting work */
|
||||
GCObject *grayagain = g->grayagain; /* save original list */
|
||||
lua_assert(g->ephemeron == NULL && g->weak == NULL);
|
||||
lua_assert(!iswhite(g->mainthread));
|
||||
g->gcstate = GCSinsideatomic;
|
||||
g->GCmemtrav = 0; /* start counting work */
|
||||
markobject(g, L); /* mark running thread */
|
||||
/* registry and global metatables may be changed by API */
|
||||
markvalue(g, &g->l_registry);
|
||||
markmt(g); /* mark basic metatables */
|
||||
markmt(g); /* mark global metatables */
|
||||
/* remark occasional upvalues of (maybe) dead threads */
|
||||
remarkupvals(g);
|
||||
propagateall(g); /* propagate changes */
|
||||
work = g->GCmemtrav; /* stop counting (do not (re)count grays) */
|
||||
/* traverse objects caught by write barrier and by 'remarkupvals' */
|
||||
retraversegrays(g);
|
||||
work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */
|
||||
g->gray = grayagain;
|
||||
propagateall(g); /* traverse 'grayagain' list */
|
||||
g->GCmemtrav = 0; /* restart counting */
|
||||
convergeephemerons(g);
|
||||
/* at this point, all strongly accessible objects are marked. */
|
||||
@@ -1002,10 +1013,11 @@ static l_mem atomic (lua_State *L) {
|
||||
/* at this point, all resurrected objects are marked. */
|
||||
/* remove dead objects from weak 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 */
|
||||
clearvalues(g, g->weak, origweak);
|
||||
clearvalues(g, g->allweak, origall);
|
||||
luaS_clearcache(g);
|
||||
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
||||
work += g->GCmemtrav; /* complete counting */
|
||||
return work; /* estimate of memory marked by 'atomic' */
|
||||
@@ -1041,7 +1053,7 @@ static lu_mem singlestep (lua_State *L) {
|
||||
g->GCmemtrav = 0;
|
||||
lua_assert(g->gray);
|
||||
propagatemark(g);
|
||||
if (g->gray == NULL) /* no more `gray' objects? */
|
||||
if (g->gray == NULL) /* no more gray objects? */
|
||||
g->gcstate = GCSatomic; /* finish propagate phase */
|
||||
return g->GCmemtrav; /* memory traversed in this step */
|
||||
}
|
||||
@@ -1132,7 +1144,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
|
||||
** unexpected ways (running finalizers and shrinking some structures).
|
||||
** Before running the collection, check 'keepinvariant'; if it is true,
|
||||
|
||||
4
lgc.h
4
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.85 2014/07/19 15:14:46 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -74,7 +74,7 @@
|
||||
#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 WHITE1BIT 1 /* object is white (type 1) */
|
||||
#define BLACKBIT 2 /* object is black */
|
||||
|
||||
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
|
||||
** 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
|
||||
** libraries, call luaL_openlibs in your program. If you need a
|
||||
** different set of libraries, copy this file to your project and edit
|
||||
** it to suit your needs.
|
||||
**
|
||||
** You can also *preload* libraries, so that a later 'require' can
|
||||
** open the library, which is already linked to the application.
|
||||
** For that, do the following code:
|
||||
**
|
||||
** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_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"
|
||||
|
||||
@@ -35,8 +48,8 @@ static const luaL_Reg loadedlibs[] = {
|
||||
{LUA_OSLIBNAME, luaopen_os},
|
||||
{LUA_STRLIBNAME, luaopen_string},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
{LUA_UTF8LIBNAME, luaopen_utf8},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
#if defined(LUA_COMPAT_BITLIB)
|
||||
{LUA_BITLIBNAME, luaopen_bit32},
|
||||
#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) {
|
||||
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++) {
|
||||
luaL_requiref(L, lib->name, lib->func, 1);
|
||||
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 */
|
||||
}
|
||||
|
||||
|
||||
82
liolib.c
82
liolib.c
@@ -1,18 +1,13 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 2.127 2014/06/30 19:48:08 roberto Exp roberto $
|
||||
** $Id: liolib.c,v 2.143 2015/03/06 19:09:08 roberto Exp roberto $
|
||||
** Standard I/O (and system) library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define liolib_c
|
||||
#define LUA_LIB
|
||||
|
||||
/*
|
||||
** 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 "lprefix.h"
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
@@ -22,9 +17,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define liolib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -60,17 +52,17 @@
|
||||
#define l_popen(L,c,m) (fflush(NULL), popen(c,m))
|
||||
#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_pclose(L,file) (_pclose(file))
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* ANSI definitions */
|
||||
/* ISO C definitions */
|
||||
#define l_popen(L,c,m) \
|
||||
((void)((void)c, m), \
|
||||
luaL_error(L, LUA_QL("popen") " not supported"), \
|
||||
luaL_error(L, "'popen' not supported"), \
|
||||
(FILE*)0)
|
||||
#define l_pclose(L,file) ((void)L, (void)file, -1)
|
||||
|
||||
@@ -112,7 +104,7 @@
|
||||
#define l_ftell(f) ftello(f)
|
||||
#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) /* }{ */
|
||||
|
||||
/* Windows (but not DDK) and Visual C++ 2005 or higher */
|
||||
@@ -122,7 +114,7 @@
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* ANSI definitions */
|
||||
/* ISO C definitions */
|
||||
#define l_fseek(f,o,w) fseek(f,o,w)
|
||||
#define l_ftell(f) ftell(f)
|
||||
#define l_seeknum long
|
||||
@@ -135,6 +127,7 @@
|
||||
|
||||
|
||||
#define IO_PREFIX "_IO_"
|
||||
#define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1)
|
||||
#define IO_INPUT (IO_PREFIX "input")
|
||||
#define IO_OUTPUT (IO_PREFIX "output")
|
||||
|
||||
@@ -181,7 +174,7 @@ 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
|
||||
** file is not left opened.
|
||||
*/
|
||||
@@ -193,9 +186,14 @@ static LStream *newprefile (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Calls the 'close' function from a file handle. The 'volatile' avoids
|
||||
** a bug in some versions of the Clang compiler (e.g., clang 3.0 for
|
||||
** 32 bits).
|
||||
*/
|
||||
static int aux_close (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
lua_CFunction cf = p->closef;
|
||||
volatile lua_CFunction cf = p->closef;
|
||||
p->closef = NULL; /* mark stream as closed */
|
||||
return (*cf)(L); /* close it */
|
||||
}
|
||||
@@ -239,7 +237,7 @@ static void opencheck (lua_State *L, const char *fname, const char *mode) {
|
||||
LStream *p = newfile(L);
|
||||
p->f = fopen(fname, mode);
|
||||
if (p->f == NULL)
|
||||
luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno));
|
||||
luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
|
||||
}
|
||||
|
||||
|
||||
@@ -285,7 +283,7 @@ static FILE *getiofile (lua_State *L, const char *findex) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
||||
p = (LStream *)lua_touserdata(L, -1);
|
||||
if (isclosed(p))
|
||||
luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX));
|
||||
luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
|
||||
return p->f;
|
||||
}
|
||||
|
||||
@@ -412,24 +410,19 @@ static int readdigits (RN *rn, int hex) {
|
||||
}
|
||||
|
||||
|
||||
/* access to locale "radix character" (decimal point) */
|
||||
#if !defined(getlocaledecpoint)
|
||||
#define getlocaledecpoint() (localeconv()->decimal_point[0])
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
||||
** Then it calls 'lua_strtonum' to check whether the format is correct
|
||||
** and to convert it to a Lua number
|
||||
** Then it calls 'lua_stringtonumber' to check whether the format is
|
||||
** correct and to convert it to a Lua number
|
||||
*/
|
||||
static int read_number (lua_State *L, FILE *f) {
|
||||
RN rn;
|
||||
int count = 0;
|
||||
int hex = 0;
|
||||
char decp[2] = ".";
|
||||
char decp[2];
|
||||
rn.f = f; rn.n = 0;
|
||||
decp[0] = getlocaledecpoint(); /* get decimal point from locale */
|
||||
decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
|
||||
decp[1] = '\0';
|
||||
l_lockfile(rn.f);
|
||||
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
||||
test2(&rn, "-+"); /* optional signal */
|
||||
@@ -447,7 +440,7 @@ static int read_number (lua_State *L, FILE *f) {
|
||||
ungetc(rn.c, rn.f); /* unread look-ahead char */
|
||||
l_unlockfile(rn.f);
|
||||
rn.buff[rn.n] = '\0'; /* finish string */
|
||||
if (lua_strtonum(L, rn.buff)) /* is this a valid number? */
|
||||
if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */
|
||||
return 1; /* ok */
|
||||
else { /* invalid format */
|
||||
lua_pushnil(L); /* "result" to be removed */
|
||||
@@ -459,21 +452,28 @@ static int read_number (lua_State *L, FILE *f) {
|
||||
static int test_eof (lua_State *L, FILE *f) {
|
||||
int c = getc(f);
|
||||
ungetc(c, f); /* no-op when c == EOF */
|
||||
lua_pushlstring(L, NULL, 0);
|
||||
lua_pushliteral(L, "");
|
||||
return (c != EOF);
|
||||
}
|
||||
|
||||
|
||||
static int read_line (lua_State *L, FILE *f, int chop) {
|
||||
luaL_Buffer b;
|
||||
int c;
|
||||
int c = '\0';
|
||||
luaL_buffinit(L, &b);
|
||||
l_lockfile(f);
|
||||
while ((c = l_getc(f)) != EOF && c != '\n')
|
||||
luaL_addchar(&b, c);
|
||||
l_unlockfile(f);
|
||||
if (!chop && c == '\n') luaL_addchar(&b, c);
|
||||
while (c != EOF && c != '\n') { /* repeat until end of line */
|
||||
char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */
|
||||
int i = 0;
|
||||
l_lockfile(f); /* no memory errors can happen inside the lock */
|
||||
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
|
||||
buff[i++] = c;
|
||||
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 */
|
||||
/* return ok if read something (either a newline or something else) */
|
||||
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
||||
}
|
||||
|
||||
@@ -518,7 +518,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
||||
success = 1;
|
||||
for (n = first; nargs-- && success; n++) {
|
||||
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);
|
||||
}
|
||||
else {
|
||||
@@ -576,7 +576,7 @@ static int io_readline (lua_State *L) {
|
||||
lua_pushvalue(L, lua_upvalueindex(3 + i));
|
||||
n = g_read(L, p->f, 2); /* 'n' is number of results */
|
||||
lua_assert(n > 0); /* should return at least a nil */
|
||||
if (!lua_isnil(L, -n)) /* read at least one value? */
|
||||
if (lua_toboolean(L, -n)) /* read at least one value? */
|
||||
return n; /* return them */
|
||||
else { /* first result is nil: EOF or error */
|
||||
if (n > 1) { /* is there error information? */
|
||||
|
||||
68
llex.c
68
llex.c
@@ -1,19 +1,22 @@
|
||||
/*
|
||||
** $Id: llex.c,v 2.79 2014/07/18 12:17:54 roberto Exp roberto $
|
||||
** $Id: llex.c,v 2.92 2015/04/03 18:41:57 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define llex_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <locale.h>
|
||||
#include <string.h>
|
||||
|
||||
#define llex_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lctype.h"
|
||||
#include "ldebug.h"
|
||||
#include "ldo.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
@@ -41,7 +44,7 @@ static const char *const luaX_tokens [] = {
|
||||
"return", "then", "true", "until", "while",
|
||||
"//", "..", "...", "==", ">=", "<=", "~=",
|
||||
"<<", ">>", "::", "<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) {
|
||||
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 */
|
||||
for (i=0; i<NUM_RESERVED; i++) {
|
||||
TString *ts = luaS_new(L, luaX_tokens[i]);
|
||||
@@ -78,14 +81,13 @@ void luaX_init (lua_State *L) {
|
||||
|
||||
const char *luaX_token2str (LexState *ls, int token) {
|
||||
if (token < FIRST_RESERVED) { /* single-byte symbols? */
|
||||
lua_assert(token == cast(unsigned char, token));
|
||||
return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) :
|
||||
luaO_pushfstring(ls->L, "char(%d)", token);
|
||||
lua_assert(token == cast_uchar(token));
|
||||
return luaO_pushfstring(ls->L, "'%c'", token);
|
||||
}
|
||||
else {
|
||||
const char *s = luaX_tokens[token - FIRST_RESERVED];
|
||||
if (token < TK_EOS) /* fixed format (symbols and reserved words)? */
|
||||
return luaO_pushfstring(ls->L, LUA_QS, s);
|
||||
return luaO_pushfstring(ls->L, "'%s'", s);
|
||||
else /* names, strings, and numerals */
|
||||
return s;
|
||||
}
|
||||
@@ -97,7 +99,7 @@ static const char *txtToken (LexState *ls, int token) {
|
||||
case TK_NAME: case TK_STRING:
|
||||
case TK_FLT: case TK_INT:
|
||||
save(ls, '\0');
|
||||
return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff));
|
||||
return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff));
|
||||
default:
|
||||
return luaX_token2str(ls, token);
|
||||
}
|
||||
@@ -105,9 +107,7 @@ static const char *txtToken (LexState *ls, int token) {
|
||||
|
||||
|
||||
static l_noret lexerror (LexState *ls, const char *msg, int token) {
|
||||
char buff[LUA_IDSIZE];
|
||||
luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
|
||||
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
|
||||
msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
|
||||
if (token)
|
||||
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
|
||||
luaD_throw(ls->L, LUA_ERRSYNTAX);
|
||||
@@ -126,7 +126,7 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
||||
*/
|
||||
TString *luaX_newstring (LexState *ls, const char *str, size_t 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 */
|
||||
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
||||
o = luaH_set(L, ls->h, L->top - 1);
|
||||
@@ -151,16 +151,17 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||
static void inclinenumber (LexState *ls) {
|
||||
int old = ls->current;
|
||||
lua_assert(currIsNewline(ls));
|
||||
next(ls); /* skip `\n' or `\r' */
|
||||
next(ls); /* skip '\n' or '\r' */
|
||||
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)
|
||||
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,
|
||||
int firstchar) {
|
||||
ls->t.token = 0;
|
||||
ls->decpoint = '.';
|
||||
ls->L = L;
|
||||
ls->current = firstchar;
|
||||
@@ -170,7 +171,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
||||
ls->linenumber = 1;
|
||||
ls->lastline = 1;
|
||||
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 */
|
||||
}
|
||||
|
||||
@@ -219,11 +220,6 @@ static void buffreplace (LexState *ls, char from, char to) {
|
||||
}
|
||||
|
||||
|
||||
#if !defined(getlocaledecpoint)
|
||||
#define getlocaledecpoint() (localeconv()->decimal_point[0])
|
||||
#endif
|
||||
|
||||
|
||||
#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
|
||||
|
||||
/*
|
||||
@@ -232,7 +228,7 @@ static void buffreplace (LexState *ls, char from, char to) {
|
||||
*/
|
||||
static void trydecpoint (LexState *ls, TValue *o) {
|
||||
char old = ls->decpoint;
|
||||
ls->decpoint = getlocaledecpoint();
|
||||
ls->decpoint = lua_getlocaledecpoint();
|
||||
buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
|
||||
if (!buff2num(ls->buff, o)) {
|
||||
/* format error with correct decimal point: no more options */
|
||||
@@ -281,8 +277,9 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||
|
||||
|
||||
/*
|
||||
** skip a sequence '[=*[' or ']=*]' and return its number of '='s or
|
||||
** -1 if sequence is malformed
|
||||
** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return
|
||||
** its number of '='s; otherwise, return a negative number (-1 iff there
|
||||
** are no '='s after initial bracket)
|
||||
*/
|
||||
static int skip_sep (LexState *ls) {
|
||||
int count = 0;
|
||||
@@ -299,7 +296,7 @@ static int skip_sep (LexState *ls) {
|
||||
|
||||
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
||||
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? */
|
||||
inclinenumber(ls); /* skip it */
|
||||
for (;;) {
|
||||
@@ -313,7 +310,7 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
||||
}
|
||||
case ']': {
|
||||
if (skip_sep(ls) == sep) {
|
||||
save_and_next(ls); /* skip 2nd `]' */
|
||||
save_and_next(ls); /* skip 2nd ']' */
|
||||
goto endloop;
|
||||
}
|
||||
break;
|
||||
@@ -360,8 +357,8 @@ static int readhexaesc (LexState *ls) {
|
||||
}
|
||||
|
||||
|
||||
static unsigned int readutf8esc (LexState *ls) {
|
||||
unsigned int r;
|
||||
static unsigned long readutf8esc (LexState *ls) {
|
||||
unsigned long r;
|
||||
int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */
|
||||
save_and_next(ls); /* skip 'u' */
|
||||
esccheck(ls, ls->current == '{', "missing '{'");
|
||||
@@ -439,7 +436,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
|
||||
}
|
||||
default: {
|
||||
esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
|
||||
c = readdecesc(ls); /* digital escape \ddd */
|
||||
c = readdecesc(ls); /* digital escape '\ddd' */
|
||||
goto only_save;
|
||||
}
|
||||
}
|
||||
@@ -481,7 +478,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
||||
next(ls);
|
||||
if (ls->current == '[') { /* long comment? */
|
||||
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) {
|
||||
read_long_string(ls, NULL, sep); /* skip long comment */
|
||||
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
|
||||
@@ -499,8 +496,9 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
||||
read_long_string(ls, seminfo, sep);
|
||||
return TK_STRING;
|
||||
}
|
||||
else if (sep == -1) return '[';
|
||||
else lexerror(ls, "invalid long string delimiter", TK_STRING);
|
||||
else if (sep != -1) /* '[=...' missing second bracket */
|
||||
lexerror(ls, "invalid long string delimiter", TK_STRING);
|
||||
return '[';
|
||||
}
|
||||
case '=': {
|
||||
next(ls);
|
||||
|
||||
8
llex.h
8
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.77 2014/10/25 11:50:46 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -14,6 +14,10 @@
|
||||
#define FIRST_RESERVED 257
|
||||
|
||||
|
||||
#if !defined(LUA_ENV)
|
||||
#define LUA_ENV "_ENV"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
@@ -54,7 +58,7 @@ typedef struct Token {
|
||||
typedef struct LexState {
|
||||
int current; /* current character (charint) */
|
||||
int linenumber; /* input line counter */
|
||||
int lastline; /* line of last token `consumed' */
|
||||
int lastline; /* line of last token 'consumed' */
|
||||
Token t; /* current token */
|
||||
Token lookahead; /* look ahead token */
|
||||
struct FuncState *fs; /* current function (parser) */
|
||||
|
||||
153
llimits.h
153
llimits.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** $Id: llimits.h,v 1.119 2014/07/18 18:18:45 roberto Exp roberto $
|
||||
** Limits, basic types, and some other `installation-dependent' definitions
|
||||
** $Id: llimits.h,v 1.134 2015/03/06 19:49:50 roberto Exp roberto $
|
||||
** Limits, basic types, and some other 'installation-dependent' definitions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
@@ -14,16 +14,24 @@
|
||||
|
||||
#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_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;
|
||||
|
||||
|
||||
@@ -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
|
||||
** 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))
|
||||
|
||||
|
||||
|
||||
@@ -80,22 +88,20 @@ typedef LUAI_UACINT l_uacInt;
|
||||
/*
|
||||
** assertion for checking API calls
|
||||
*/
|
||||
#if defined(LUA_USE_APICHECK)
|
||||
#include <assert.h>
|
||||
#define luai_apicheck(e) assert(e)
|
||||
#else
|
||||
#define luai_apicheck(e) lua_assert(e)
|
||||
#if !defined(luai_apicheck)
|
||||
#define luai_apicheck(l,e) lua_assert(e)
|
||||
#endif
|
||||
|
||||
|
||||
#define api_check(e,msg) luai_apicheck((e) && msg)
|
||||
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||
|
||||
|
||||
/* macro to avoid warnings about unused variables */
|
||||
#if !defined(UNUSED)
|
||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
||||
#define UNUSED(x) ((void)(x))
|
||||
#endif
|
||||
|
||||
|
||||
/* type casts (a macro highlights casts in the code) */
|
||||
#define cast(t, exp) ((t)(exp))
|
||||
|
||||
#define cast_void(i) cast(void, (i))
|
||||
@@ -112,7 +118,7 @@ typedef LUAI_UACINT l_uacInt;
|
||||
|
||||
/*
|
||||
** 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.
|
||||
*/
|
||||
#if !defined(l_castU2S)
|
||||
@@ -125,7 +131,7 @@ typedef LUAI_UACINT l_uacInt;
|
||||
*/
|
||||
#if defined(__GNUC__)
|
||||
#define l_noret void __attribute__((noreturn))
|
||||
#elif defined(_MSC_VER)
|
||||
#elif defined(_MSC_VER) && _MSC_VER >= 1200
|
||||
#define l_noret void __declspec(noreturn)
|
||||
#else
|
||||
#define l_noret void
|
||||
@@ -141,29 +147,47 @@ typedef LUAI_UACINT l_uacInt;
|
||||
#define LUAI_MAXCCALLS 200
|
||||
#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)
|
||||
*/
|
||||
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)
|
||||
#define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */
|
||||
#define MINSTRTABSIZE 128
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Size of cache for strings in the API (better be a prime)
|
||||
*/
|
||||
#if !defined(STRCACHE_SIZE)
|
||||
#define STRCACHE_SIZE 127
|
||||
#endif
|
||||
|
||||
|
||||
@@ -173,13 +197,21 @@ typedef lu_int32 Instruction;
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** macros that are executed whenether program enters the Lua core
|
||||
** ('lua_lock') and leaves the core ('lua_unlock')
|
||||
*/
|
||||
#if !defined(lua_lock)
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#define lua_lock(L) ((void) 0)
|
||||
#define lua_unlock(L) ((void) 0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
** macro executed during Lua functions at points where the
|
||||
** function can yield.
|
||||
*/
|
||||
#if !defined(luai_threadyield)
|
||||
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
||||
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -205,15 +237,62 @@ typedef lu_int32 Instruction;
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstateresume)
|
||||
#define luai_userstateresume(L,n) ((void)L)
|
||||
#define luai_userstateresume(L,n) ((void)L)
|
||||
#endif
|
||||
|
||||
#if !defined(luai_userstateyield)
|
||||
#define luai_userstateyield(L,n) ((void)L)
|
||||
#define luai_userstateyield(L,n) ((void)L)
|
||||
#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
|
||||
*/
|
||||
|
||||
37
lmathlib.c
37
lmathlib.c
@@ -1,16 +1,18 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.107 2014/07/17 12:30:53 roberto Exp roberto $
|
||||
** $Id: lmathlib.c,v 1.114 2014/12/27 20:32:26 roberto Exp roberto $
|
||||
** Standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lmathlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define lmathlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -25,9 +27,11 @@
|
||||
#if defined(LUA_USE_POSIX)
|
||||
#define l_rand() random()
|
||||
#define l_srand(x) srandom(x)
|
||||
#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */
|
||||
#else
|
||||
#define l_rand() rand()
|
||||
#define l_srand(x) srand(x)
|
||||
#define L_RANDMAX RAND_MAX
|
||||
#endif
|
||||
#endif /* } */
|
||||
|
||||
@@ -91,7 +95,7 @@ static int math_toint (lua_State *L) {
|
||||
|
||||
static void pushnumint (lua_State *L, lua_Number d) {
|
||||
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 */
|
||||
else
|
||||
lua_pushnumber(L, d); /* result is float */
|
||||
@@ -153,7 +157,7 @@ static int math_modf (lua_State *L) {
|
||||
lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
|
||||
pushnumint(L, ip);
|
||||
/* fractional part (test needed for inf/-inf) */
|
||||
lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
|
||||
lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
@@ -179,6 +183,9 @@ static int math_log (lua_State *L) {
|
||||
res = l_mathop(log)(x);
|
||||
else {
|
||||
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);
|
||||
else res = l_mathop(log)(x)/l_mathop(log)(base);
|
||||
}
|
||||
@@ -192,12 +199,12 @@ static int math_exp (lua_State *L) {
|
||||
}
|
||||
|
||||
static int math_deg (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (180.0 / PI));
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_rad (lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / 180.0));
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -231,15 +238,15 @@ static int math_max (lua_State *L) {
|
||||
|
||||
/*
|
||||
** 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.)
|
||||
*/
|
||||
static int math_random (lua_State *L) {
|
||||
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 */
|
||||
case 0: { /* no arguments */
|
||||
lua_pushnumber(L, r); /* Number between 0 and 1 */
|
||||
lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
|
||||
return 1;
|
||||
}
|
||||
case 1: { /* only upper limit */
|
||||
@@ -256,8 +263,8 @@ static int math_random (lua_State *L) {
|
||||
}
|
||||
/* random integer in the interval [low, up] */
|
||||
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
||||
luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER,
|
||||
1, "interval too large");
|
||||
luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
|
||||
"interval too large");
|
||||
r *= (double)(up - low) + 1.0;
|
||||
lua_pushinteger(L, (lua_Integer)r + low);
|
||||
return 1;
|
||||
@@ -324,7 +331,7 @@ static int math_frexp (lua_State *L) {
|
||||
|
||||
static int math_ldexp (lua_State *L) {
|
||||
lua_Number x = luaL_checknumber(L, 1);
|
||||
int ep = luaL_checkint(L, 2);
|
||||
int ep = (int)luaL_checkinteger(L, 2);
|
||||
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
|
||||
return 1;
|
||||
}
|
||||
@@ -389,7 +396,7 @@ LUAMOD_API int luaopen_math (lua_State *L) {
|
||||
luaL_newlib(L, mathlib);
|
||||
lua_pushnumber(L, PI);
|
||||
lua_setfield(L, -2, "pi");
|
||||
lua_pushnumber(L, HUGE_VAL);
|
||||
lua_pushnumber(L, (lua_Number)HUGE_VAL);
|
||||
lua_setfield(L, -2, "huge");
|
||||
lua_pushinteger(L, LUA_MAXINTEGER);
|
||||
lua_setfield(L, -2, "maxinteger");
|
||||
|
||||
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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define lmem_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
@@ -24,15 +26,15 @@
|
||||
/*
|
||||
** About the realloc function:
|
||||
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||
** (`osize' is the old size, `nsize' is the new size)
|
||||
** ('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').
|
||||
**
|
||||
** * 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);
|
||||
** 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
|
||||
** (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
|
||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
||||
if (newblock == NULL && nsize > 0) {
|
||||
api_check( nsize > realosize,
|
||||
"realloc cannot fail when shrinking a block");
|
||||
luaC_fullgc(L, 1); /* try to free some memory... */
|
||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
||||
lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
|
||||
if (g->version) { /* is state fully built? */
|
||||
luaC_fullgc(L, 1); /* try to free some memory... */
|
||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
||||
}
|
||||
if (newblock == NULL)
|
||||
luaD_throw(L, LUA_ERRMEM);
|
||||
}
|
||||
|
||||
30
lmem.h
30
lmem.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.39 2012/11/14 17:21:34 roberto Exp roberto $
|
||||
** $Id: lmem.h,v 1.42 2014/12/19 13:45:40 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -15,20 +15,32 @@
|
||||
|
||||
|
||||
/*
|
||||
** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is
|
||||
** always constant.
|
||||
** The macro is somewhat complex to avoid warnings:
|
||||
** +1 avoids warnings of "comparison has constant result";
|
||||
** cast to 'void' avoids warnings of "value unused".
|
||||
** This macro reallocs a vector 'b' from 'on' to 'n' elements, where
|
||||
** each element has size 'e'. In case of arithmetic overflow of the
|
||||
** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because
|
||||
** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e).
|
||||
**
|
||||
** (The macro is somewhat complex to avoid warnings: The 'sizeof'
|
||||
** comparison avoids a runtime comparison when overflow cannot occur.
|
||||
** The compiler should be able to optimize the real test by itself, but
|
||||
** when it does it, it may give a warning about "comparison is always
|
||||
** false due to limited range of data type"; the +1 tricks the compiler,
|
||||
** avoiding this warning but also this optimization.)
|
||||
*/
|
||||
#define luaM_reallocv(L,b,on,n,e) \
|
||||
(cast(void, \
|
||||
(cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \
|
||||
(((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \
|
||||
? luaM_toobig(L) : cast_void(0)) , \
|
||||
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_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
||||
#define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0]))
|
||||
#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
|
||||
|
||||
#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s))
|
||||
#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
|
||||
|
||||
89
loadlib.c
89
loadlib.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: loadlib.c,v 1.115 2014/07/28 17:47:53 roberto Exp roberto $
|
||||
** $Id: loadlib.c,v 1.125 2015/01/13 13:18:04 roberto Exp roberto $
|
||||
** Dynamic library loader for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
**
|
||||
@@ -8,22 +8,16 @@
|
||||
** 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 LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -92,8 +86,11 @@
|
||||
#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"
|
||||
|
||||
@@ -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.
|
||||
@@ -139,6 +136,18 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
/*
|
||||
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
||||
** is undefined according to ISO C, but POSIX assumes that it works.
|
||||
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
||||
*/
|
||||
#if defined(__GNUC__)
|
||||
#define cast_func(p) (__extension__ (lua_CFunction)(p))
|
||||
#else
|
||||
#define cast_func(p) ((lua_CFunction)(p))
|
||||
#endif
|
||||
|
||||
|
||||
static void lsys_unloadlib (void *lib) {
|
||||
dlclose(lib);
|
||||
}
|
||||
@@ -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) {
|
||||
lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
|
||||
lua_CFunction f = cast_func(dlsym(lib, sym));
|
||||
if (f == NULL) lua_pushstring(L, dlerror());
|
||||
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.
|
||||
** =======================================================================
|
||||
*/
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#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
|
||||
@@ -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) {
|
||||
void *plib;
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||
lua_getfield(L, -1, path);
|
||||
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||
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
|
||||
*/
|
||||
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_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||
@@ -396,7 +407,7 @@ static const char *searchpath (lua_State *L, const char *name,
|
||||
lua_remove(L, -2); /* remove path template */
|
||||
if (readable(filename)) /* does file exist and is readable? */
|
||||
return filename; /* return that file name */
|
||||
lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
|
||||
lua_pushfstring(L, "\n\tno file '%s'", filename);
|
||||
lua_remove(L, -2); /* remove file name */
|
||||
luaL_addvalue(&msg); /* concatenate error msg. entry */
|
||||
}
|
||||
@@ -426,7 +437,7 @@ static const char *findfile (lua_State *L, const char *name,
|
||||
lua_getfield(L, lua_upvalueindex(1), pname);
|
||||
path = lua_tostring(L, -1);
|
||||
if (path == NULL)
|
||||
luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
|
||||
luaL_error(L, "'package.%s' must be a string", pname);
|
||||
return searchpath(L, name, path, ".", dirsep);
|
||||
}
|
||||
|
||||
@@ -437,8 +448,7 @@ static int checkload (lua_State *L, int stat, const char *filename) {
|
||||
return 2; /* return open function and file name */
|
||||
}
|
||||
else
|
||||
return luaL_error(L, "error loading module " LUA_QS
|
||||
" from file " LUA_QS ":\n\t%s",
|
||||
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
|
||||
lua_tostring(L, 1), filename, lua_tostring(L, -1));
|
||||
}
|
||||
|
||||
@@ -499,8 +509,7 @@ static int searcher_Croot (lua_State *L) {
|
||||
if (stat != ERRFUNC)
|
||||
return checkload(L, 0, filename); /* real error */
|
||||
else { /* open function not found */
|
||||
lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
|
||||
name, filename);
|
||||
lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -522,16 +531,15 @@ static void findloader (lua_State *L, const char *name) {
|
||||
int i;
|
||||
luaL_Buffer msg; /* to build error message */
|
||||
luaL_buffinit(L, &msg);
|
||||
lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */
|
||||
if (!lua_istable(L, 3))
|
||||
luaL_error(L, LUA_QL("package.searchers") " must be a table");
|
||||
/* push 'package.searchers' to index 3 in the stack */
|
||||
if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
|
||||
luaL_error(L, "'package.searchers' must be a table");
|
||||
/* iterate over available searchers to find a loader */
|
||||
for (i = 1; ; i++) {
|
||||
if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
|
||||
lua_pop(L, 1); /* remove nil */
|
||||
luaL_pushresult(&msg); /* create error message */
|
||||
luaL_error(L, "module " LUA_QS " not found:%s",
|
||||
name, lua_tostring(L, -1));
|
||||
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
||||
}
|
||||
lua_pushstring(L, name);
|
||||
lua_call(L, 1, 2); /* call it */
|
||||
@@ -589,7 +597,7 @@ static void set_env (lua_State *L) {
|
||||
if (lua_getstack(L, 1, &ar) == 0 ||
|
||||
lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
|
||||
lua_iscfunction(L, -1))
|
||||
luaL_error(L, LUA_QL("module") " not called from a Lua function");
|
||||
luaL_error(L, "'module' not called from a Lua function");
|
||||
lua_pushvalue(L, -2); /* copy new environment table to top */
|
||||
lua_setupvalue(L, -2, 1);
|
||||
lua_pop(L, 1); /* remove function */
|
||||
@@ -732,7 +740,7 @@ static void createsearcherstable (lua_State *L) {
|
||||
}
|
||||
#if defined(LUA_COMPAT_LOADERS)
|
||||
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
|
||||
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
||||
}
|
||||
@@ -743,17 +751,18 @@ static void createsearcherstable (lua_State *L) {
|
||||
** setting a finalizer to close all libraries when closing state.
|
||||
*/
|
||||
static void createclibstable (lua_State *L) {
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
||||
lua_newtable(L); /* create CLIBS table */
|
||||
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
||||
lua_pushcfunction(L, gctm);
|
||||
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
|
||||
}
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
createclibstable(L);
|
||||
luaL_newlib(L, pk_funcs); /* create `package' table */
|
||||
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
||||
createsearcherstable(L);
|
||||
/* set field 'path' */
|
||||
setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT);
|
||||
@@ -763,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_EXEC_DIR "\n" LUA_IGMARK "\n");
|
||||
lua_setfield(L, -2, "config");
|
||||
/* set field `loaded' */
|
||||
/* set field 'loaded' */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_setfield(L, -2, "loaded");
|
||||
/* set field `preload' */
|
||||
/* set field 'preload' */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
lua_setfield(L, -2, "preload");
|
||||
lua_pushglobaltable(L);
|
||||
|
||||
128
lobject.c
128
lobject.c
@@ -1,17 +1,22 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 2.87 2014/06/30 19:48:08 roberto Exp roberto $
|
||||
** $Id: lobject.c,v 2.103 2015/03/28 19:14:47 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lobject_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lctype.h"
|
||||
@@ -36,8 +41,12 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
||||
int luaO_int2fb (unsigned int x) {
|
||||
int e = 0; /* exponent */
|
||||
if (x < 8) return x;
|
||||
while (x >= 0x10) {
|
||||
x = (x+1) >> 1;
|
||||
while (x >= (8 << 4)) { /* coarse steps */
|
||||
x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
|
||||
e += 4;
|
||||
}
|
||||
while (x >= (8 << 1)) { /* fine steps */
|
||||
x = (x + 1) >> 1; /* x = ceil(x / 2) */
|
||||
e++;
|
||||
}
|
||||
return ((e+1) << 3) | (cast_int(x) - 8);
|
||||
@@ -52,8 +61,11 @@ int luaO_fb2int (int x) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Computes ceil(log2(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,
|
||||
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,
|
||||
@@ -98,6 +110,7 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1,
|
||||
case LUA_OPMUL: return luai_nummul(L, v1, v2);
|
||||
case LUA_OPDIV: return luai_numdiv(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_OPMOD: {
|
||||
lua_Number m;
|
||||
@@ -112,9 +125,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,
|
||||
TValue *res) {
|
||||
switch (op) {
|
||||
case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR:
|
||||
case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR:
|
||||
case LUA_OPBNOT: { /* operates only on integers */
|
||||
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
|
||||
case LUA_OPSHL: case LUA_OPSHR:
|
||||
case LUA_OPBNOT: { /* operate only on integers */
|
||||
lua_Integer i1; lua_Integer i2;
|
||||
if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
|
||||
setivalue(res, intarith(L, op, i1, i2));
|
||||
@@ -145,13 +158,13 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
||||
}
|
||||
/* could not perform raw operation; try metamethod */
|
||||
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) {
|
||||
if (lisdigit(c)) return c - '0';
|
||||
else return ltolower(c) - 'a' + 10;
|
||||
else return (ltolower(c) - 'a') + 10;
|
||||
}
|
||||
|
||||
|
||||
@@ -162,26 +175,14 @@ static int isneg (const char **s) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** lua_strx2number converts an hexadecimal numeric string to a number.
|
||||
** 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.
|
||||
** =======================================================
|
||||
** {==================================================================
|
||||
** Lua's implementation for 'lua_strx2number'
|
||||
** ===================================================================
|
||||
*/
|
||||
#if !defined(lua_strx2number) /* { */
|
||||
|
||||
#if defined(LUA_USE_C99) /* { */
|
||||
|
||||
#define lua_strx2number(s,p) lua_str2number(s,p)
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* Lua's implementation for 'lua_strx2number' */
|
||||
|
||||
#include <math.h>
|
||||
#if !defined(lua_strx2number)
|
||||
|
||||
/* maximum number of significant digits to read (to avoid overflows
|
||||
even with single floats) */
|
||||
@@ -192,35 +193,30 @@ static int isneg (const char **s) {
|
||||
** C99 specification for 'strtod'
|
||||
*/
|
||||
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||
int dot = lua_getlocaledecpoint();
|
||||
lua_Number r = 0.0; /* result (accumulator) */
|
||||
int sigdig = 0; /* number of significant digits */
|
||||
int nosigdig = 0; /* number of non-significant digits */
|
||||
int e = 0; /* exponent correction */
|
||||
int neg = 0; /* 1 if number is negative */
|
||||
int dot = 0; /* true after seen a dot */
|
||||
int neg; /* 1 if number is negative */
|
||||
int hasdot = 0; /* true after seen a dot */
|
||||
*endptr = cast(char *, s); /* nothing is valid yet */
|
||||
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
||||
neg = isneg(&s); /* check signal */
|
||||
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
||||
return 0.0; /* invalid format (no '0x') */
|
||||
for (s += 2; ; s++) { /* skip '0x' and read numeral */
|
||||
if (*s == '.') {
|
||||
if (dot) break; /* second dot? stop loop */
|
||||
else dot = 1;
|
||||
if (*s == dot) {
|
||||
if (hasdot) break; /* second dot? stop loop */
|
||||
else hasdot = 1;
|
||||
}
|
||||
else if (lisxdigit(cast_uchar(*s))) {
|
||||
if (sigdig == 0 && *s == '0') { /* non-significant zero? */
|
||||
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
|
||||
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(cast_uchar(*s));
|
||||
if (dot) e--; /* decimal digit */
|
||||
}
|
||||
else /* too many digits; ignore */
|
||||
if (!dot) e++; /* still count it for exponent */
|
||||
}
|
||||
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
||||
r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
|
||||
else e++; /* too many digits; ignore, but still count for exponent */
|
||||
if (hasdot) e--; /* decimal digit? correct exponent */
|
||||
}
|
||||
else break; /* neither a dot nor a digit */
|
||||
}
|
||||
@@ -245,10 +241,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||
return l_mathop(ldexp)(r, e);
|
||||
}
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif /* } */
|
||||
|
||||
#endif
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
@@ -260,7 +253,7 @@ static const char *l_str2d (const char *s, lua_Number *result) {
|
||||
*result = lua_strx2number(s, &endptr);
|
||||
else
|
||||
*result = lua_str2number(s, &endptr);
|
||||
if (endptr == s) return 0; /* nothing recognized */
|
||||
if (endptr == s) return NULL; /* nothing recognized */
|
||||
while (lisspace(cast_uchar(*endptr))) endptr++;
|
||||
return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
|
||||
}
|
||||
@@ -276,13 +269,13 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
|
||||
(s[1] == 'x' || s[1] == 'X')) { /* hex? */
|
||||
s += 2; /* skip '0x' */
|
||||
for (; lisxdigit(cast_uchar(*s)); s++) {
|
||||
a = a * 16 + luaO_hexavalue(cast_uchar(*s));
|
||||
a = a * 16 + luaO_hexavalue(*s);
|
||||
empty = 0;
|
||||
}
|
||||
}
|
||||
else { /* decimal */
|
||||
for (; lisdigit(cast_uchar(*s)); s++) {
|
||||
a = a * 10 + luaO_hexavalue(cast_uchar(*s));
|
||||
a = a * 10 + *s - '0';
|
||||
empty = 0;
|
||||
}
|
||||
}
|
||||
@@ -306,22 +299,23 @@ size_t luaO_str2num (const char *s, TValue *o) {
|
||||
}
|
||||
else
|
||||
return 0; /* conversion failed */
|
||||
return (e - s + 1); /* success; return string size */
|
||||
return (e - s) + 1; /* success; return string size */
|
||||
}
|
||||
|
||||
|
||||
int luaO_utf8esc (char *buff, unsigned int x) {
|
||||
int luaO_utf8esc (char *buff, unsigned long x) {
|
||||
int n = 1; /* number of bytes put in buffer (backwards) */
|
||||
lua_assert(x <= 0x10FFFF);
|
||||
if (x < 0x80) /* ascii? */
|
||||
buff[UTF8BUFFSZ - 1] = x;
|
||||
buff[UTF8BUFFSZ - 1] = cast(char, x);
|
||||
else { /* need continuation bytes */
|
||||
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
||||
do {
|
||||
buff[UTF8BUFFSZ - (n++)] = 0x80 | (x & 0x3f); /* add continuation byte */
|
||||
do { /* add continuation bytes */
|
||||
buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
|
||||
x >>= 6; /* remove added bits */
|
||||
mfb >>= 1; /* now there is one less bit available in first byte */
|
||||
} while (x > mfb); /* still needs continuation byte? */
|
||||
buff[UTF8BUFFSZ - n] = (~mfb << 1) | x; /* add first byte */
|
||||
buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */
|
||||
}
|
||||
return n;
|
||||
}
|
||||
@@ -344,7 +338,7 @@ void luaO_tostring (lua_State *L, StkId obj) {
|
||||
len = lua_number2str(buff, fltvalue(obj));
|
||||
#if !defined(LUA_COMPAT_FLOATSTRING)
|
||||
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
||||
buff[len++] = '.';
|
||||
buff[len++] = lua_getlocaledecpoint();
|
||||
buff[len++] = '0'; /* adds '.0' to result */
|
||||
}
|
||||
#endif
|
||||
@@ -359,7 +353,7 @@ static void pushstr (lua_State *L, const char *str, size_t l) {
|
||||
|
||||
|
||||
/* this function handles only '%d', '%c', '%f', '%p', and '%s'
|
||||
conventional formats, plus Lua-specific '%L' and '%U' */
|
||||
conventional formats, plus Lua-specific '%I' and '%U' */
|
||||
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
int n = 0;
|
||||
for (;;) {
|
||||
@@ -376,7 +370,10 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
}
|
||||
case 'c': {
|
||||
char buff = cast(char, va_arg(argp, int));
|
||||
pushstr(L, &buff, 1);
|
||||
if (lisprint(cast_uchar(buff)))
|
||||
pushstr(L, &buff, 1);
|
||||
else /* non-printable character; print its code */
|
||||
luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
|
||||
break;
|
||||
}
|
||||
case 'd': {
|
||||
@@ -395,14 +392,14 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
break;
|
||||
}
|
||||
case 'p': {
|
||||
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%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);
|
||||
break;
|
||||
}
|
||||
case 'U': {
|
||||
char buff[UTF8BUFFSZ];
|
||||
int l = luaO_utf8esc(buff, va_arg(argp, int));
|
||||
int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
|
||||
pushstr(L, buff + UTF8BUFFSZ - l, l);
|
||||
break;
|
||||
}
|
||||
@@ -411,9 +408,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
luaG_runerror(L,
|
||||
"invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
|
||||
*(e + 1));
|
||||
luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
|
||||
*(e + 1));
|
||||
}
|
||||
}
|
||||
n += 2;
|
||||
|
||||
53
lobject.h
53
lobject.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 2.100 2014/07/29 16:22:24 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 2.110 2015/04/02 21:10:53 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -35,8 +35,6 @@
|
||||
** bit 6: whether value is collectable
|
||||
*/
|
||||
|
||||
#define VARBITS (3 << 4)
|
||||
|
||||
|
||||
/*
|
||||
** LUA_TFUNCTION variants:
|
||||
@@ -154,6 +152,8 @@ typedef struct lua_TValue TValue;
|
||||
/* Macros to access values */
|
||||
#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
|
||||
#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 pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
||||
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
|
||||
@@ -188,9 +188,15 @@ typedef struct lua_TValue TValue;
|
||||
#define setfltvalue(obj,x) \
|
||||
{ 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) \
|
||||
{ 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 setfvalue(obj,x) \
|
||||
@@ -301,9 +307,12 @@ typedef TValue *StkId; /* index to stack elements */
|
||||
typedef struct TString {
|
||||
CommonHeader;
|
||||
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
|
||||
lu_byte shrlen; /* length for short strings */
|
||||
unsigned int hash;
|
||||
size_t len; /* number of characters in string */
|
||||
struct TString *hnext; /* linked list for hash table */
|
||||
union {
|
||||
size_t lnglen; /* length for long strings */
|
||||
struct TString *hnext; /* linked list for hash table */
|
||||
} u;
|
||||
} TString;
|
||||
|
||||
|
||||
@@ -327,6 +336,12 @@ typedef union UTString {
|
||||
/* get the actual string (array of bytes) from a Lua value */
|
||||
#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
|
||||
@@ -345,7 +360,7 @@ typedef struct Udata {
|
||||
** Ensures that address after this type is always fully aligned.
|
||||
*/
|
||||
typedef union UUdata {
|
||||
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
||||
L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
|
||||
Udata uv;
|
||||
} UUdata;
|
||||
|
||||
@@ -359,13 +374,13 @@ typedef union UUdata {
|
||||
|
||||
#define setuservalue(L,u,o) \
|
||||
{ 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); }
|
||||
|
||||
|
||||
#define getuservalue(L,u,o) \
|
||||
{ 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); }
|
||||
|
||||
|
||||
@@ -374,7 +389,7 @@ typedef union UUdata {
|
||||
*/
|
||||
typedef struct Upvaldesc {
|
||||
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) */
|
||||
} Upvaldesc;
|
||||
|
||||
@@ -397,22 +412,22 @@ typedef struct Proto {
|
||||
CommonHeader;
|
||||
lu_byte numparams; /* number of fixed parameters */
|
||||
lu_byte is_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 sizek; /* size of `k' */
|
||||
int sizek; /* size of 'k' */
|
||||
int sizecode;
|
||||
int sizelineinfo;
|
||||
int sizep; /* size of `p' */
|
||||
int sizep; /* size of 'p' */
|
||||
int sizelocvars;
|
||||
int linedefined;
|
||||
int lastlinedefined;
|
||||
TValue *k; /* constants used by the function */
|
||||
Instruction *code;
|
||||
Instruction *code; /* opcodes */
|
||||
struct Proto **p; /* functions defined inside the function */
|
||||
int *lineinfo; /* map from opcodes to source lines (debug information) */
|
||||
LocVar *locvars; /* information about local variables (debug 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 */
|
||||
GCObject *gclist;
|
||||
} Proto;
|
||||
@@ -471,7 +486,7 @@ typedef union TKey {
|
||||
|
||||
|
||||
/* 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); \
|
||||
k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
|
||||
(void)L; checkliveness(G(L),io_); }
|
||||
@@ -486,8 +501,8 @@ typedef struct Node {
|
||||
typedef struct Table {
|
||||
CommonHeader;
|
||||
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
||||
lu_byte lsizenode; /* log2 of size of `node' array */
|
||||
int sizearray; /* size of `array' array */
|
||||
lu_byte lsizenode; /* log2 of size of 'node' array */
|
||||
unsigned int sizearray; /* size of 'array' array */
|
||||
TValue *array; /* array part */
|
||||
Node *node;
|
||||
Node *lastfree; /* any free position is before this position */
|
||||
@@ -498,7 +513,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) \
|
||||
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
|
||||
@@ -521,7 +536,7 @@ LUAI_DDEC const TValue luaO_nilobject_;
|
||||
|
||||
LUAI_FUNC int luaO_int2fb (unsigned int x);
|
||||
LUAI_FUNC int luaO_fb2int (int x);
|
||||
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned int x);
|
||||
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
|
||||
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
|
||||
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
|
||||
const TValue *p2, TValue *res);
|
||||
|
||||
@@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#define lopcodes_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
30
lopcodes.h
30
lopcodes.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.145 2013/12/18 14:12:03 roberto Exp roberto $
|
||||
** $Id: lopcodes.h,v 1.147 2014/10/20 18:29:55 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -14,12 +14,12 @@
|
||||
We assume that instructions are unsigned numbers.
|
||||
All instructions have an opcode in the first 6 bits.
|
||||
Instructions can have the following fields:
|
||||
`A' : 8 bits
|
||||
`B' : 9 bits
|
||||
`C' : 9 bits
|
||||
'A' : 8 bits
|
||||
'B' : 9 bits
|
||||
'C' : 9 bits
|
||||
'Ax' : 26 bits ('A', 'B', and 'C' together)
|
||||
`Bx' : 18 bits (`B' and `C' together)
|
||||
`sBx' : signed Bx
|
||||
'Bx' : 18 bits ('B' and 'C' together)
|
||||
'sBx' : signed Bx
|
||||
|
||||
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
|
||||
@@ -58,7 +58,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
|
||||
*/
|
||||
#if SIZE_Bx < LUAI_BITSINT-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
|
||||
#define MAXARG_Bx 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)
|
||||
|
||||
|
||||
/* 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))
|
||||
|
||||
/* 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))
|
||||
|
||||
/*
|
||||
@@ -203,7 +203,7 @@ OP_LEN,/* A B R(A) := length of R(B) */
|
||||
|
||||
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||
|
||||
OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */
|
||||
OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
|
||||
OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
|
||||
OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
|
||||
OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
|
||||
@@ -238,16 +238,16 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
|
||||
/*===========================================================================
|
||||
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,
|
||||
OP_SETLIST) may use `top'.
|
||||
OP_SETLIST) may use 'top'.
|
||||
|
||||
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
|
||||
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).
|
||||
|
||||
(*) 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
|
||||
(true or false).
|
||||
|
||||
(*) All `skips' (pc++) assume that next instruction is a jump.
|
||||
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
||||
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
95
loslib.c
95
loslib.c
@@ -1,9 +1,14 @@
|
||||
/*
|
||||
** $Id: loslib.c,v 1.45 2014/03/20 19:18:54 roberto Exp roberto $
|
||||
** $Id: loslib.c,v 1.56 2015/02/09 17:41:54 roberto Exp roberto $
|
||||
** Standard Operating System library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define loslib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
@@ -11,23 +16,22 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define loslib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
||||
/*
|
||||
** {==================================================================
|
||||
** list of valid conversion specifiers for the 'strftime' function
|
||||
** ===================================================================
|
||||
*/
|
||||
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
||||
|
||||
#if !defined(LUA_USE_POSIX)
|
||||
#if defined(LUA_USE_C89)
|
||||
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
|
||||
#else
|
||||
#else /* C99 specification */
|
||||
#define LUA_STRFTIMEOPTIONS \
|
||||
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
|
||||
"E", "cCxXyY", \
|
||||
@@ -35,8 +39,14 @@
|
||||
#endif
|
||||
|
||||
#endif /* } */
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** Configuration for time-related stuff
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#if !defined(l_time_t) /* { */
|
||||
/*
|
||||
@@ -49,15 +59,41 @@
|
||||
#endif /* } */
|
||||
|
||||
|
||||
|
||||
#if !defined(lua_tmpnam) /* { */
|
||||
#if !defined(l_gmtime) /* { */
|
||||
/*
|
||||
** By default, Lua uses tmpnam except when POSIX is available, where it
|
||||
** uses mkstemp.
|
||||
** 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 /* }{ */
|
||||
|
||||
/* 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>
|
||||
|
||||
#define LUA_TMPNAMBUFSIZE 32
|
||||
@@ -74,38 +110,17 @@
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* ANSI definitions */
|
||||
/* ISO C definitions */
|
||||
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
||||
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
||||
|
||||
#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) {
|
||||
const char *cmd = luaL_optstring(L, 1, NULL);
|
||||
@@ -189,7 +204,7 @@ static int getfield (lua_State *L, const char *key, int d) {
|
||||
res = (int)lua_tointegerx(L, -1, &isnum);
|
||||
if (!isnum) {
|
||||
if (d < 0)
|
||||
return luaL_error(L, "field " LUA_QS " missing in date table", key);
|
||||
return luaL_error(L, "field '%s' missing in date table", key);
|
||||
res = d;
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
@@ -227,7 +242,7 @@ static int os_date (lua_State *L) {
|
||||
struct tm tmr, *stm;
|
||||
if (*s == '!') { /* UTC? */
|
||||
stm = l_gmtime(&t, &tmr);
|
||||
s++; /* skip `!' */
|
||||
s++; /* skip '!' */
|
||||
}
|
||||
else
|
||||
stm = l_localtime(&t, &tmr);
|
||||
@@ -285,7 +300,7 @@ static int os_time (lua_State *L) {
|
||||
t = mktime(&ts);
|
||||
}
|
||||
if (t != (time_t)(l_timet)t)
|
||||
luaL_error(L, "time result cannot be represented in this Lua instalation");
|
||||
luaL_error(L, "time result cannot be represented in this Lua installation");
|
||||
else if (t == (time_t)(-1))
|
||||
lua_pushnil(L);
|
||||
else
|
||||
@@ -295,7 +310,9 @@ static int os_time (lua_State *L) {
|
||||
|
||||
|
||||
static int os_difftime (lua_State *L) {
|
||||
lua_pushnumber(L, difftime((l_checktime(L, 1)), (l_checktime(L, 2))));
|
||||
time_t t1 = l_checktime(L, 1);
|
||||
time_t t2 = l_checktime(L, 2);
|
||||
lua_pushnumber(L, (lua_Number)difftime(t1, t2));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -319,7 +336,7 @@ static int os_exit (lua_State *L) {
|
||||
if (lua_isboolean(L, 1))
|
||||
status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
else
|
||||
status = luaL_optint(L, 1, EXIT_SUCCESS);
|
||||
status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
|
||||
if (lua_toboolean(L, 2))
|
||||
lua_close(L);
|
||||
if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
|
||||
|
||||
90
lparser.c
90
lparser.c
@@ -1,15 +1,17 @@
|
||||
/*
|
||||
** $Id: lparser.c,v 2.141 2014/07/18 13:36:14 roberto Exp roberto $
|
||||
** $Id: lparser.c,v 2.146 2014/11/27 18:41:43 roberto Exp roberto $
|
||||
** Lua Parser
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lparser_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lcode.h"
|
||||
@@ -45,11 +47,11 @@
|
||||
*/
|
||||
typedef struct BlockCnt {
|
||||
struct BlockCnt *previous; /* chain */
|
||||
short firstlabel; /* index of first label in this block */
|
||||
short firstgoto; /* index of first pending goto in this block */
|
||||
int firstlabel; /* index of first label in this block */
|
||||
int firstgoto; /* index of first pending goto in this block */
|
||||
lu_byte nactvar; /* # active locals outside the block */
|
||||
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;
|
||||
|
||||
|
||||
@@ -63,7 +65,7 @@ static void expr (LexState *ls, expdesc *v);
|
||||
|
||||
/* semantic error */
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -340,7 +342,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) {
|
||||
if (gt->nactvar < label->nactvar) {
|
||||
TString *vname = getlocvar(fs, gt->nactvar)->varname;
|
||||
const char *msg = luaO_pushfstring(ls->L,
|
||||
"<goto %s> at line %d jumps into the scope of local " LUA_QS,
|
||||
"<goto %s> at line %d jumps into the scope of local '%s'",
|
||||
getstr(gt->name), gt->line, getstr(vname));
|
||||
semerror(ls, msg);
|
||||
}
|
||||
@@ -384,7 +386,7 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name,
|
||||
l->arr[n].line = line;
|
||||
l->arr[n].nactvar = ls->fs->nactvar;
|
||||
l->arr[n].pc = pc;
|
||||
l->n++;
|
||||
l->n = n + 1;
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -406,7 +408,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
|
||||
** the goto exits the scope of any variable (which can be the
|
||||
** upvalue), close those variables being exited.
|
||||
@@ -442,7 +444,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) {
|
||||
TString *n = luaS_new(ls->L, "break");
|
||||
@@ -457,7 +459,7 @@ static void breaklabel (LexState *ls) {
|
||||
static l_noret undefgoto (LexState *ls, Labeldesc *gt) {
|
||||
const char *msg = isreserved(gt->name)
|
||||
? "<%s> at line %d not inside a loop"
|
||||
: "no visible label " LUA_QS " for <goto> at line %d";
|
||||
: "no visible label '%s' for <goto> at line %d";
|
||||
msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
|
||||
semerror(ls, msg);
|
||||
}
|
||||
@@ -574,7 +576,7 @@ static void close_func (LexState *ls) {
|
||||
/*
|
||||
** check whether current token is in the follow set of a block.
|
||||
** '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) {
|
||||
switch (ls->t.token) {
|
||||
@@ -588,7 +590,7 @@ static int block_follow (LexState *ls, int withuntil) {
|
||||
|
||||
|
||||
static void statlist (LexState *ls) {
|
||||
/* statlist -> { stat [`;'] } */
|
||||
/* statlist -> { stat [';'] } */
|
||||
while (!block_follow(ls, 1)) {
|
||||
if (ls->t.token == TK_RETURN) {
|
||||
statement(ls);
|
||||
@@ -629,14 +631,14 @@ static void yindex (LexState *ls, expdesc *v) {
|
||||
struct ConsControl {
|
||||
expdesc v; /* last list item read */
|
||||
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 tostore; /* number of array elements pending to be stored */
|
||||
};
|
||||
|
||||
|
||||
static void recfield (LexState *ls, struct ConsControl *cc) {
|
||||
/* recfield -> (NAME | `['exp1`]') = exp1 */
|
||||
/* recfield -> (NAME | '['exp1']') = exp1 */
|
||||
FuncState *fs = ls->fs;
|
||||
int reg = ls->fs->freereg;
|
||||
expdesc key, val;
|
||||
@@ -743,12 +745,12 @@ static void constructor (LexState *ls, expdesc *t) {
|
||||
|
||||
|
||||
static void parlist (LexState *ls) {
|
||||
/* parlist -> [ param { `,' param } ] */
|
||||
/* parlist -> [ param { ',' param } ] */
|
||||
FuncState *fs = ls->fs;
|
||||
Proto *f = fs->f;
|
||||
int nparams = 0;
|
||||
f->is_vararg = 0;
|
||||
if (ls->t.token != ')') { /* is `parlist' not empty? */
|
||||
if (ls->t.token != ')') { /* is 'parlist' not empty? */
|
||||
do {
|
||||
switch (ls->t.token) {
|
||||
case TK_NAME: { /* param -> NAME */
|
||||
@@ -756,12 +758,12 @@ static void parlist (LexState *ls) {
|
||||
nparams++;
|
||||
break;
|
||||
}
|
||||
case TK_DOTS: { /* param -> `...' */
|
||||
case TK_DOTS: { /* param -> '...' */
|
||||
luaX_next(ls);
|
||||
f->is_vararg = 1;
|
||||
break;
|
||||
}
|
||||
default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
|
||||
default: luaX_syntaxerror(ls, "<name> or '...' expected");
|
||||
}
|
||||
} while (!f->is_vararg && testnext(ls, ','));
|
||||
}
|
||||
@@ -772,7 +774,7 @@ static void parlist (LexState *ls) {
|
||||
|
||||
|
||||
static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
||||
/* body -> `(' parlist `)' block END */
|
||||
/* body -> '(' parlist ')' block END */
|
||||
FuncState new_fs;
|
||||
BlockCnt bl;
|
||||
new_fs.f = addprototype(ls);
|
||||
@@ -794,7 +796,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
||||
|
||||
|
||||
static int explist (LexState *ls, expdesc *v) {
|
||||
/* explist -> expr { `,' expr } */
|
||||
/* explist -> expr { ',' expr } */
|
||||
int n = 1; /* at least one expression */
|
||||
expr(ls, v);
|
||||
while (testnext(ls, ',')) {
|
||||
@@ -811,7 +813,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
||||
expdesc args;
|
||||
int base, nparams;
|
||||
switch (ls->t.token) {
|
||||
case '(': { /* funcargs -> `(' [ explist ] `)' */
|
||||
case '(': { /* funcargs -> '(' [ explist ] ')' */
|
||||
luaX_next(ls);
|
||||
if (ls->t.token == ')') /* arg list is empty? */
|
||||
args.k = VVOID;
|
||||
@@ -828,7 +830,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
||||
}
|
||||
case TK_STRING: { /* funcargs -> STRING */
|
||||
codestring(ls, &args, ls->t.seminfo.ts);
|
||||
luaX_next(ls); /* must use `seminfo' before `next' */
|
||||
luaX_next(ls); /* must use 'seminfo' before 'next' */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
@@ -894,14 +896,14 @@ static void suffixedexp (LexState *ls, expdesc *v) {
|
||||
fieldsel(ls, v);
|
||||
break;
|
||||
}
|
||||
case '[': { /* `[' exp1 `]' */
|
||||
case '[': { /* '[' exp1 ']' */
|
||||
expdesc key;
|
||||
luaK_exp2anyregup(fs, v);
|
||||
yindex(ls, &key);
|
||||
luaK_indexed(fs, v, &key);
|
||||
break;
|
||||
}
|
||||
case ':': { /* `:' NAME funcargs */
|
||||
case ':': { /* ':' NAME funcargs */
|
||||
expdesc key;
|
||||
luaX_next(ls);
|
||||
checkname(ls, &key);
|
||||
@@ -953,7 +955,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
|
||||
case TK_DOTS: { /* vararg */
|
||||
FuncState *fs = ls->fs;
|
||||
check_condition(ls, fs->f->is_vararg,
|
||||
"cannot use " LUA_QL("...") " outside a vararg function");
|
||||
"cannot use '...' outside a vararg function");
|
||||
init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
|
||||
break;
|
||||
}
|
||||
@@ -1035,7 +1037,7 @@ static const struct {
|
||||
|
||||
/*
|
||||
** 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) {
|
||||
BinOpr op;
|
||||
@@ -1049,7 +1051,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
||||
luaK_prefix(ls->fs, uop, v, line);
|
||||
}
|
||||
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);
|
||||
while (op != OPR_NOBINOPR && priority[op].left > limit) {
|
||||
expdesc v2;
|
||||
@@ -1149,7 +1151,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
|
||||
"C levels");
|
||||
assignment(ls, &nv, nvars+1);
|
||||
}
|
||||
else { /* assignment -> `=' explist */
|
||||
else { /* assignment -> '=' explist */
|
||||
int nexps;
|
||||
checknext(ls, '=');
|
||||
nexps = explist(ls, &e);
|
||||
@@ -1173,7 +1175,7 @@ static int cond (LexState *ls) {
|
||||
/* cond -> exp */
|
||||
expdesc v;
|
||||
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);
|
||||
return v.f;
|
||||
}
|
||||
@@ -1200,7 +1202,7 @@ static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) {
|
||||
for (i = fs->bl->firstlabel; i < ll->n; i++) {
|
||||
if (eqstr(label, ll->arr[i].name)) {
|
||||
const char *msg = luaO_pushfstring(fs->ls->L,
|
||||
"label " LUA_QS " already defined on line %d",
|
||||
"label '%s' already defined on line %d",
|
||||
getstr(label), ll->arr[i].line);
|
||||
semerror(fs->ls, msg);
|
||||
}
|
||||
@@ -1362,15 +1364,15 @@ static void forstat (LexState *ls, int line) {
|
||||
TString *varname;
|
||||
BlockCnt bl;
|
||||
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 */
|
||||
switch (ls->t.token) {
|
||||
case '=': fornum(ls, varname, line); break;
|
||||
case ',': case TK_IN: forlist(ls, varname); break;
|
||||
default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
|
||||
default: luaX_syntaxerror(ls, "'=' or 'in' expected");
|
||||
}
|
||||
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 +1402,7 @@ static void test_then_block (LexState *ls, int *escapelist) {
|
||||
enterblock(fs, &bl, 0);
|
||||
jf = v.f;
|
||||
}
|
||||
statlist(ls); /* `then' part */
|
||||
statlist(ls); /* 'then' part */
|
||||
leaveblock(fs);
|
||||
if (ls->t.token == TK_ELSE ||
|
||||
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
||||
@@ -1417,7 +1419,7 @@ static void ifstat (LexState *ls, int line) {
|
||||
while (ls->t.token == TK_ELSEIF)
|
||||
test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
|
||||
if (testnext(ls, TK_ELSE))
|
||||
block(ls); /* `else' part */
|
||||
block(ls); /* 'else' part */
|
||||
check_match(ls, TK_END, TK_IF, line);
|
||||
luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
|
||||
}
|
||||
@@ -1435,7 +1437,7 @@ static void localfunc (LexState *ls) {
|
||||
|
||||
|
||||
static void localstat (LexState *ls) {
|
||||
/* stat -> LOCAL NAME {`,' NAME} [`=' explist] */
|
||||
/* stat -> LOCAL NAME {',' NAME} ['=' explist] */
|
||||
int nvars = 0;
|
||||
int nexps;
|
||||
expdesc e;
|
||||
@@ -1455,7 +1457,7 @@ static void localstat (LexState *ls) {
|
||||
|
||||
|
||||
static int funcname (LexState *ls, expdesc *v) {
|
||||
/* funcname -> NAME {fieldsel} [`:' NAME] */
|
||||
/* funcname -> NAME {fieldsel} [':' NAME] */
|
||||
int ismethod = 0;
|
||||
singlevar(ls, v);
|
||||
while (ls->t.token == '.')
|
||||
@@ -1476,7 +1478,7 @@ static void funcstat (LexState *ls, int line) {
|
||||
ismethod = funcname(ls, &v);
|
||||
body(ls, &b, ismethod, line);
|
||||
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 */
|
||||
}
|
||||
|
||||
|
||||
@@ -1518,8 +1520,8 @@ static void retstat (LexState *ls) {
|
||||
if (nret == 1) /* only one single value? */
|
||||
first = luaK_exp2anyreg(fs, &e);
|
||||
else {
|
||||
luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
|
||||
first = fs->nactvar; /* return all `active' values */
|
||||
luaK_exp2nextreg(fs, &e); /* values must go to the stack */
|
||||
first = fs->nactvar; /* return all active values */
|
||||
lua_assert(nret == fs->freereg - first);
|
||||
}
|
||||
}
|
||||
@@ -1630,7 +1632,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
incr_top(L);
|
||||
funcstate.f = cl->p = luaF_newproto(L);
|
||||
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.dyd = dyd;
|
||||
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
|
||||
|
||||
16
lparser.h
16
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.73 2014/06/19 18:27:20 roberto Exp roberto $
|
||||
** Lua Parser
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -21,7 +21,7 @@ typedef enum {
|
||||
VNIL,
|
||||
VTRUE,
|
||||
VFALSE,
|
||||
VK, /* info = index of constant in `k' */
|
||||
VK, /* info = index of constant in 'k' */
|
||||
VKFLT, /* nval = numerical float value */
|
||||
VKINT, /* nval = numerical integer value */
|
||||
VNONRELOC, /* info = result register */
|
||||
@@ -50,8 +50,8 @@ typedef struct expdesc {
|
||||
lua_Number nval; /* for VKFLT */
|
||||
lua_Integer ival; /* for VKINT */
|
||||
} u;
|
||||
int t; /* patch list of `exit when true' */
|
||||
int f; /* patch list of `exit when false' */
|
||||
int t; /* patch list of 'exit when true' */
|
||||
int f; /* patch list of 'exit when false' */
|
||||
} expdesc;
|
||||
|
||||
|
||||
@@ -100,11 +100,11 @@ typedef struct FuncState {
|
||||
struct FuncState *prev; /* enclosing function */
|
||||
struct LexState *ls; /* lexical state */
|
||||
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 jpc; /* list of pending jumps to `pc' */
|
||||
int nk; /* number of elements in `k' */
|
||||
int np; /* number of elements in `p' */
|
||||
int jpc; /* list of pending jumps to 'pc' */
|
||||
int nk; /* number of elements in 'k' */
|
||||
int np; /* number of elements in 'p' */
|
||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||
short nlocvars; /* number of elements in 'f->locvars' */
|
||||
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
|
||||
|
||||
23
lstate.c
23
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.127 2014/11/02 19:33:33 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lstate_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lstate_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -35,9 +37,6 @@
|
||||
#endif
|
||||
|
||||
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
/*
|
||||
** a macro to help the creation of a unique random seed when a state is
|
||||
** created; the seed is used to randomize hashes.
|
||||
@@ -72,9 +71,8 @@ typedef struct LG {
|
||||
|
||||
|
||||
/*
|
||||
** Compute an initial seed as random as possible. In ANSI, rely on
|
||||
** Address Space Layout Randomization (if present) to increase
|
||||
** randomness..
|
||||
** Compute an initial seed as random as possible. Rely on Address Space
|
||||
** Layout Randomization (if present) to increase randomness..
|
||||
*/
|
||||
#define addbuff(b,p,e) \
|
||||
{ size_t t = cast(size_t, e); \
|
||||
@@ -199,12 +197,9 @@ static void f_luaopen (lua_State *L, void *ud) {
|
||||
UNUSED(ud);
|
||||
stack_init(L, L); /* init stack */
|
||||
init_registry(L, g);
|
||||
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
||||
luaS_init(L);
|
||||
luaT_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->version = lua_version(NULL);
|
||||
luai_userstateopen(L);
|
||||
|
||||
30
lstate.h
30
lstate.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 2.113 2014/07/18 14:46:47 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 2.121 2015/04/10 17:56:25 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -21,10 +21,10 @@
|
||||
** belong to one (and only one) of these lists, using field 'next' of
|
||||
** the 'CommonHeader' for the link:
|
||||
**
|
||||
** allgc: all objects not marked for finalization;
|
||||
** finobj: all objects marked for finalization;
|
||||
** tobefnz: all objects ready to be finalized;
|
||||
** fixedgc: all objects that are not to be collected (currently
|
||||
** 'allgc': all objects not marked for finalization;
|
||||
** 'finobj': all objects marked for finalization;
|
||||
** 'tobefnz': all objects ready to be finalized;
|
||||
** 'fixedgc': all objects that are not to be collected (currently
|
||||
** only small strings, such as reserved words).
|
||||
|
||||
*/
|
||||
@@ -54,7 +54,13 @@ typedef struct stringtable {
|
||||
|
||||
|
||||
/*
|
||||
** information about a call
|
||||
** Information about a call.
|
||||
** When a thread yields, 'func' is adjusted to pretend that the
|
||||
** top function has only the yielded values in its stack; in that
|
||||
** case, the actual 'func' value is saved in field 'extra'.
|
||||
** When a function calls another with a continuation, 'extra' keeps
|
||||
** the function index so that, in case of errors, the continuation
|
||||
** function can be called with the correct top.
|
||||
*/
|
||||
typedef struct CallInfo {
|
||||
StkId func; /* function index in the stack */
|
||||
@@ -68,7 +74,7 @@ typedef struct CallInfo {
|
||||
struct { /* only for C functions */
|
||||
lua_KFunction k; /* continuation in case of yields */
|
||||
ptrdiff_t old_errfunc;
|
||||
lua_Ctx ctx; /* context info. in case of yields */
|
||||
lua_KContext ctx; /* context info. in case of yields */
|
||||
} c;
|
||||
} u;
|
||||
ptrdiff_t extra;
|
||||
@@ -88,6 +94,7 @@ typedef struct CallInfo {
|
||||
#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)
|
||||
|
||||
@@ -97,11 +104,11 @@ 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 {
|
||||
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 GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||
lu_mem GCmemtrav; /* memory traversed by the GC */
|
||||
@@ -127,18 +134,19 @@ typedef struct global_State {
|
||||
Mbuffer buff; /* temporary buffer for string concatenation */
|
||||
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
|
||||
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 */
|
||||
struct lua_State *mainthread;
|
||||
const lua_Number *version; /* pointer to version number */
|
||||
TString *memerrmsg; /* memory-error message */
|
||||
TString *tmname[TM_N]; /* array with tag-method names */
|
||||
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
|
||||
TString *strcache[STRCACHE_SIZE][1]; /* cache for strings in API */
|
||||
} global_State;
|
||||
|
||||
|
||||
/*
|
||||
** `per thread' state
|
||||
** 'per thread' state
|
||||
*/
|
||||
struct lua_State {
|
||||
CommonHeader;
|
||||
|
||||
80
lstring.c
80
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.48 2015/03/25 13:42:19 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lstring_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
@@ -20,6 +22,8 @@
|
||||
#include "lstring.h"
|
||||
|
||||
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
/*
|
||||
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
||||
@@ -34,10 +38,10 @@
|
||||
** equality for long strings
|
||||
*/
|
||||
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);
|
||||
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 */
|
||||
}
|
||||
|
||||
@@ -67,9 +71,9 @@ void luaS_resize (lua_State *L, int newsize) {
|
||||
TString *p = tb->hash[i];
|
||||
tb->hash[i] = NULL;
|
||||
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 */
|
||||
p->hnext = tb->hash[h]; /* chain it */
|
||||
p->u.hnext = tb->hash[h]; /* chain it */
|
||||
tb->hash[h] = p;
|
||||
p = hnext;
|
||||
}
|
||||
@@ -83,6 +87,34 @@ 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;
|
||||
for (i = 0; i < STRCACHE_SIZE; i++) {
|
||||
if (iswhite(g->strcache[i][0])) /* will entry be collected? */
|
||||
g->strcache[i][0] = 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;
|
||||
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_SIZE; i++) /* fill cache with valid strings */
|
||||
g->strcache[i][0] = g->memerrmsg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** creates a new string object
|
||||
@@ -95,7 +127,6 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
|
||||
totalsize = sizelstring(l);
|
||||
o = luaC_newobj(L, tag, totalsize);
|
||||
ts = gco2ts(o);
|
||||
ts->len = l;
|
||||
ts->hash = h;
|
||||
ts->extra = 0;
|
||||
memcpy(getaddrstr(ts), str, l * sizeof(char));
|
||||
@@ -108,8 +139,8 @@ void luaS_remove (lua_State *L, TString *ts) {
|
||||
stringtable *tb = &G(L)->strt;
|
||||
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
|
||||
while (*p != ts) /* find previous element */
|
||||
p = &(*p)->hnext;
|
||||
*p = (*p)->hnext; /* remove element from its list */
|
||||
p = &(*p)->u.hnext;
|
||||
*p = (*p)->u.hnext; /* remove element from its list */
|
||||
tb->nuse--;
|
||||
}
|
||||
|
||||
@@ -122,8 +153,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
||||
global_State *g = G(L);
|
||||
unsigned int h = luaS_hash(str, l, g->seed);
|
||||
TString **list = &g->strt.hash[lmod(h, g->strt.size)];
|
||||
for (ts = *list; ts != NULL; ts = ts->hnext) {
|
||||
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)) {
|
||||
/* found! */
|
||||
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
||||
@@ -136,7 +167,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
||||
list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
|
||||
}
|
||||
ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
|
||||
ts->hnext = *list;
|
||||
ts->shrlen = cast_byte(l);
|
||||
ts->u.hnext = *list;
|
||||
*list = ts;
|
||||
g->strt.nuse++;
|
||||
return ts;
|
||||
@@ -150,18 +182,32 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||
if (l <= LUAI_MAXSHORTLEN) /* short string? */
|
||||
return internshrstr(L, str, l);
|
||||
else {
|
||||
TString *ts;
|
||||
if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
|
||||
luaM_toobig(L);
|
||||
return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
|
||||
ts = createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
|
||||
ts->u.lnglen = l;
|
||||
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) {
|
||||
return luaS_newlstr(L, str, strlen(str));
|
||||
unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */
|
||||
TString **p = G(L)->strcache[i];
|
||||
if (strcmp(str, getstr(p[0])) == 0) /* hit? */
|
||||
return p[0]; /* that it is */
|
||||
else { /* normal route */
|
||||
TString *s = luaS_newlstr(L, str, strlen(str));
|
||||
p[0] = s;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.54 2014/03/19 18:51:42 roberto Exp roberto $
|
||||
** $Id: lstring.h,v 1.58 2015/03/04 13:31:21 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
|
||||
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
|
||||
#define sizestring(s) sizelstring((s)->len)
|
||||
|
||||
#define sizeludata(l) (sizeof(union UUdata) + (l))
|
||||
#define sizeudata(u) sizeludata((u)->len)
|
||||
@@ -37,6 +36,8 @@
|
||||
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
|
||||
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
||||
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
||||
LUAI_FUNC void luaS_clearcache (global_State *g);
|
||||
LUAI_FUNC void luaS_init (lua_State *L);
|
||||
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
||||
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
|
||||
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||
|
||||
749
lstrlib.c
749
lstrlib.c
@@ -1,20 +1,23 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.199 2014/07/29 16:01:00 roberto Exp roberto $
|
||||
** $Id: lstrlib.c,v 1.228 2015/04/03 18:41:57 roberto Exp roberto $
|
||||
** Standard library for string operations and pattern-matching
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lstrlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lstrlib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -30,10 +33,19 @@
|
||||
#endif
|
||||
|
||||
|
||||
/* macro to `unsign' a character */
|
||||
/* macro to 'unsign' a character */
|
||||
#define uchar(c) ((unsigned char)(c))
|
||||
|
||||
|
||||
/*
|
||||
** Some sizes are better limited to fit in 'int', but must also fit in
|
||||
** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
|
||||
*/
|
||||
#define MAXSIZE \
|
||||
(sizeof(size_t) < sizeof(int) ? (~(size_t)0) : (size_t)(INT_MAX))
|
||||
|
||||
|
||||
|
||||
|
||||
static int str_len (lua_State *L) {
|
||||
size_t l;
|
||||
@@ -59,7 +71,7 @@ static int str_sub (lua_State *L) {
|
||||
if (start < 1) start = 1;
|
||||
if (end > (lua_Integer)l) end = l;
|
||||
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, "");
|
||||
return 1;
|
||||
}
|
||||
@@ -103,13 +115,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) {
|
||||
size_t l, lsep;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
@@ -145,9 +150,9 @@ static int str_byte (lua_State *L) {
|
||||
if (posi < 1) posi = 1;
|
||||
if (pose > (lua_Integer)l) pose = l;
|
||||
if (posi > pose) return 0; /* empty interval; return no values */
|
||||
n = (int)(pose - posi + 1);
|
||||
if (posi + n <= pose) /* arithmetic overflow? */
|
||||
if (pose - posi >= INT_MAX) /* arithmetic overflow? */
|
||||
return luaL_error(L, "string slice too long");
|
||||
n = (int)(pose - posi) + 1;
|
||||
luaL_checkstack(L, n, "string slice too long");
|
||||
for (i=0; i<n; i++)
|
||||
lua_pushinteger(L, uchar(s[posi+i-1]));
|
||||
@@ -250,16 +255,16 @@ static const char *classend (MatchState *ms, const char *p) {
|
||||
switch (*p++) {
|
||||
case L_ESC: {
|
||||
if (p == ms->p_end)
|
||||
luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
|
||||
luaL_error(ms->L, "malformed pattern (ends with '%%')");
|
||||
return p+1;
|
||||
}
|
||||
case '[': {
|
||||
if (*p == '^') p++;
|
||||
do { /* look for a `]' */
|
||||
do { /* look for a ']' */
|
||||
if (p == ms->p_end)
|
||||
luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
|
||||
luaL_error(ms->L, "malformed pattern (missing ']')");
|
||||
if (*(p++) == L_ESC && p < ms->p_end)
|
||||
p++; /* skip escapes (e.g. `%]') */
|
||||
p++; /* skip escapes (e.g. '%]') */
|
||||
} while (*p != ']');
|
||||
return p+1;
|
||||
}
|
||||
@@ -294,7 +299,7 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
|
||||
int sig = 1;
|
||||
if (*(p+1) == '^') {
|
||||
sig = 0;
|
||||
p++; /* skip the `^' */
|
||||
p++; /* skip the '^' */
|
||||
}
|
||||
while (++p < ec) {
|
||||
if (*p == L_ESC) {
|
||||
@@ -332,8 +337,7 @@ static int singlematch (MatchState *ms, const char *s, const char *p,
|
||||
static const char *matchbalance (MatchState *ms, const char *s,
|
||||
const char *p) {
|
||||
if (p >= ms->p_end - 1)
|
||||
luaL_error(ms->L, "malformed pattern "
|
||||
"(missing arguments to " LUA_QL("%%b") ")");
|
||||
luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
|
||||
if (*s != *p) return NULL;
|
||||
else {
|
||||
int b = *p;
|
||||
@@ -432,7 +436,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||
break;
|
||||
}
|
||||
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 */
|
||||
s = (s == ms->src_end) ? s : NULL; /* check end of string */
|
||||
break;
|
||||
@@ -450,8 +454,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||
const char *ep; char previous;
|
||||
p += 2;
|
||||
if (*p != '[')
|
||||
luaL_error(ms->L, "missing " LUA_QL("[") " after "
|
||||
LUA_QL("%%f") " in pattern");
|
||||
luaL_error(ms->L, "missing '[' after '%%f' in pattern");
|
||||
ep = classend(ms, p); /* points to what is next */
|
||||
previous = (s == ms->src_init) ? '\0' : *(s - 1);
|
||||
if (!matchbracketclass(uchar(previous), p, ep - 1) &&
|
||||
@@ -497,7 +500,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||
}
|
||||
case '+': /* 1 or more repetitions */
|
||||
s++; /* 1 match already done */
|
||||
/* go through */
|
||||
/* FALLTHROUGH */
|
||||
case '*': /* 0 or more repetitions */
|
||||
s = max_expand(ms, s, p, ep);
|
||||
break;
|
||||
@@ -521,16 +524,16 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
||||
static const char *lmemfind (const char *s1, size_t l1,
|
||||
const char *s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
||||
else if (l2 > l1) return NULL; /* avoids a negative 'l1' */
|
||||
else {
|
||||
const char *init; /* to search for a `*s2' inside `s1' */
|
||||
l2--; /* 1st char will be checked by `memchr' */
|
||||
l1 = l1-l2; /* `s2' cannot be found after that */
|
||||
const char *init; /* to search for a '*s2' inside 's1' */
|
||||
l2--; /* 1st char will be checked by 'memchr' */
|
||||
l1 = l1-l2; /* 's2' cannot be found after that */
|
||||
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
||||
init++; /* 1st char is already checked */
|
||||
if (memcmp(init, s2+1, l2) == 0)
|
||||
return init-1;
|
||||
else { /* correct `l1' and `s1' to try again */
|
||||
else { /* correct 'l1' and 's1' to try again */
|
||||
l1 -= init-s1;
|
||||
s1 = init;
|
||||
}
|
||||
@@ -546,13 +549,13 @@ static void push_onecapture (MatchState *ms, int i, const char *s,
|
||||
if (i == 0) /* ms->level == 0, too */
|
||||
lua_pushlstring(ms->L, s, e - s); /* add whole match */
|
||||
else
|
||||
luaL_error(ms->L, "invalid capture index");
|
||||
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
|
||||
}
|
||||
else {
|
||||
ptrdiff_t l = ms->capture[i].len;
|
||||
if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
|
||||
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
|
||||
lua_pushlstring(ms->L, ms->capture[i].init, l);
|
||||
}
|
||||
@@ -596,8 +599,8 @@ static int str_find_aux (lua_State *L, int find) {
|
||||
/* do a plain search */
|
||||
const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
|
||||
if (s2) {
|
||||
lua_pushinteger(L, s2 - s + 1);
|
||||
lua_pushinteger(L, s2 - s + lp);
|
||||
lua_pushinteger(L, (s2 - s) + 1);
|
||||
lua_pushinteger(L, (s2 - s) + lp);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@@ -619,7 +622,7 @@ static int str_find_aux (lua_State *L, int find) {
|
||||
lua_assert(ms.matchdepth == MAXCCALLS);
|
||||
if ((res=match(&ms, s1, p)) != NULL) {
|
||||
if (find) {
|
||||
lua_pushinteger(L, s1 - s + 1); /* start */
|
||||
lua_pushinteger(L, (s1 - s) + 1); /* start */
|
||||
lua_pushinteger(L, res - s); /* end */
|
||||
return push_captures(&ms, NULL, 0) + 2;
|
||||
}
|
||||
@@ -694,8 +697,7 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit(uchar(news[i]))) {
|
||||
if (news[i] != L_ESC)
|
||||
luaL_error(L, "invalid use of " LUA_QL("%c")
|
||||
" in replacement string", L_ESC);
|
||||
luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
|
||||
luaL_addchar(b, news[i]);
|
||||
}
|
||||
else if (news[i] == '0')
|
||||
@@ -796,17 +798,100 @@ static int str_gsub (lua_State *L) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
||||
#define MAX_ITEM 512
|
||||
#if !defined(lua_number2strx) /* { */
|
||||
|
||||
/*
|
||||
** Hexadecimal floating-point formatter
|
||||
*/
|
||||
|
||||
#include <locale.h>
|
||||
#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, lua_Number x) {
|
||||
if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */
|
||||
return sprintf(buff, LUA_NUMBER_FMT, x); /* equal to '%g' */
|
||||
else if (x == 0) { /* can be -0... */
|
||||
sprintf(buff, LUA_NUMBER_FMT, x);
|
||||
strcat(buff, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */
|
||||
return strlen(buff);
|
||||
}
|
||||
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 += sprintf(buff + n, "p%+d", e); /* add exponent */
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int lua_number2strx (lua_State *L, char *buff, const char *fmt,
|
||||
lua_Number x) {
|
||||
int n = num2straux(buff, 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', minfloat), and is equal to 99 + 2 ('-' and '.') +
|
||||
** number of decimal digits to represent minfloat.
|
||||
*/
|
||||
#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
|
||||
|
||||
|
||||
/* valid flags in a format specification */
|
||||
#define FLAGS "-+ #0"
|
||||
|
||||
/*
|
||||
** 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) {
|
||||
@@ -848,8 +933,8 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
||||
if (isdigit(uchar(*p)))
|
||||
luaL_error(L, "invalid format (width or precision too long)");
|
||||
*(form++) = '%';
|
||||
memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
|
||||
form += p - strfrmt + 1;
|
||||
memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char));
|
||||
form += (p - strfrmt) + 1;
|
||||
*form = '\0';
|
||||
return p;
|
||||
}
|
||||
@@ -882,7 +967,7 @@ static int str_format (lua_State *L) {
|
||||
else if (*++strfrmt == L_ESC)
|
||||
luaL_addchar(&b, *strfrmt++); /* %% */
|
||||
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 */
|
||||
int nb = 0; /* number of bytes in added item */
|
||||
if (++arg > top)
|
||||
@@ -890,7 +975,7 @@ static int str_format (lua_State *L) {
|
||||
strfrmt = scanformat(L, strfrmt, form);
|
||||
switch (*strfrmt++) {
|
||||
case 'c': {
|
||||
nb = sprintf(buff, form, luaL_checkint(L, arg));
|
||||
nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg));
|
||||
break;
|
||||
}
|
||||
case 'd': case 'i':
|
||||
@@ -900,10 +985,11 @@ static int str_format (lua_State *L) {
|
||||
nb = sprintf(buff, form, n);
|
||||
break;
|
||||
}
|
||||
case 'e': case 'E': case 'f':
|
||||
#if defined(LUA_USE_AFORMAT)
|
||||
case 'a': case 'A':
|
||||
#endif
|
||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||
nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg));
|
||||
break;
|
||||
case 'e': case 'E': case 'f':
|
||||
case 'g': case 'G': {
|
||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||
nb = sprintf(buff, form, luaL_checknumber(L, arg));
|
||||
@@ -920,17 +1006,16 @@ static int str_format (lua_State *L) {
|
||||
/* no precision and string is too long to be formatted;
|
||||
keep original string */
|
||||
luaL_addvalue(&b);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
nb = sprintf(buff, form, s);
|
||||
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: { /* also treat cases `pnLlh' */
|
||||
return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
|
||||
LUA_QL("format"), *(strfrmt - 1));
|
||||
default: { /* also treat cases 'pnLlh' */
|
||||
return luaL_error(L, "invalid option '%%%c' to 'format'",
|
||||
*(strfrmt - 1));
|
||||
}
|
||||
}
|
||||
luaL_addsize(&b, nb);
|
||||
@@ -950,212 +1035,435 @@ static int str_format (lua_State *L) {
|
||||
*/
|
||||
|
||||
|
||||
/* value used for padding */
|
||||
#if !defined(LUA_PACKPADBYTE)
|
||||
#define LUA_PACKPADBYTE 0x00
|
||||
#endif
|
||||
|
||||
/* maximum size for the binary representation of an integer */
|
||||
#define MAXINTSIZE 16
|
||||
|
||||
/* number of bits in a character */
|
||||
#define NB CHAR_BIT
|
||||
|
||||
/* mask for one character (NB 1's) */
|
||||
#define MC ((1 << NB) - 1)
|
||||
|
||||
/* mask for one character without sign bit ((NB - 1) 1's) */
|
||||
#define SM (MC >> 1)
|
||||
|
||||
/* size of a lua_Integer */
|
||||
#define SZINT ((int)sizeof(lua_Integer))
|
||||
|
||||
/* maximum size for the binary representation of an integer */
|
||||
#define MAXINTSIZE 12
|
||||
|
||||
|
||||
static union {
|
||||
/* dummy union to get native endianness */
|
||||
static const union {
|
||||
int dummy;
|
||||
char little; /* true iff machine is little endian */
|
||||
} const nativeendian = {1};
|
||||
} nativeendian = {1};
|
||||
|
||||
|
||||
static int getendian (lua_State *L, int arg) {
|
||||
const char *endian = luaL_optstring(L, arg,
|
||||
(nativeendian.little ? "l" : "b"));
|
||||
if (*endian == 'n') /* native? */
|
||||
return nativeendian.little;
|
||||
luaL_argcheck(L, *endian == 'l' || *endian == 'b', arg,
|
||||
"endianness must be 'l'/'b'/'n'");
|
||||
return (*endian == 'l');
|
||||
/* dummy structure to get native alignment requirements */
|
||||
struct cD {
|
||||
char c;
|
||||
union { double d; void *p; lua_Integer i; lua_Number n; } u;
|
||||
};
|
||||
|
||||
#define MAXALIGN (offsetof(struct cD, u))
|
||||
|
||||
|
||||
/*
|
||||
** Union for serializing floats
|
||||
*/
|
||||
typedef union Ftypes {
|
||||
float f;
|
||||
double d;
|
||||
lua_Number n;
|
||||
char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
|
||||
} Ftypes;
|
||||
|
||||
|
||||
/*
|
||||
** information to pack/unpack stuff
|
||||
*/
|
||||
typedef struct Header {
|
||||
lua_State *L;
|
||||
int islittle;
|
||||
int maxalign;
|
||||
} Header;
|
||||
|
||||
|
||||
/*
|
||||
** options for pack/unpack
|
||||
*/
|
||||
typedef enum KOption {
|
||||
Kint, /* signed integers */
|
||||
Kuint, /* unsigned integers */
|
||||
Kfloat, /* floating-point numbers */
|
||||
Kchar, /* fixed-length strings */
|
||||
Kstring, /* strings with prefixed length */
|
||||
Kzstr, /* zero-terminated strings */
|
||||
Kpadding, /* padding */
|
||||
Kpaddalign, /* padding for alignment */
|
||||
Knop /* no-op (configuration or spaces) */
|
||||
} KOption;
|
||||
|
||||
|
||||
/*
|
||||
** Read an integer numeral from string 'fmt' or return 'df' if
|
||||
** there is no numeral
|
||||
*/
|
||||
static int digit (int c) { return '0' <= c && c <= '9'; }
|
||||
|
||||
static int getnum (const char **fmt, int df) {
|
||||
if (!digit(**fmt)) /* no number? */
|
||||
return df; /* return default value */
|
||||
else {
|
||||
int a = 0;
|
||||
do {
|
||||
a = a*10 + (*((*fmt)++) - '0');
|
||||
} while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int getintsize (lua_State *L, int arg) {
|
||||
int size = luaL_optint(L, arg, 0);
|
||||
if (size == 0) size = SZINT;
|
||||
luaL_argcheck(L, 1 <= size && size <= MAXINTSIZE, arg,
|
||||
"integer size out of valid range");
|
||||
return size;
|
||||
/*
|
||||
** Read an integer numeral and raises an error if it is larger
|
||||
** than the maximum size for integers.
|
||||
*/
|
||||
static int getnumlimit (Header *h, const char **fmt, int df) {
|
||||
int sz = getnum(fmt, df);
|
||||
if (sz > MAXINTSIZE || sz <= 0)
|
||||
luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
|
||||
sz, MAXINTSIZE);
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
/* mask for all ones in last byte in a lua Integer */
|
||||
#define HIGHERBYTE ((lua_Unsigned)MC << (NB * (SZINT - 1)))
|
||||
/*
|
||||
** Initialize Header
|
||||
*/
|
||||
static void initheader (lua_State *L, Header *h) {
|
||||
h->L = L;
|
||||
h->islittle = nativeendian.little;
|
||||
h->maxalign = 1;
|
||||
}
|
||||
|
||||
|
||||
static int dumpint (char *buff, lua_Integer m, int littleendian, int size) {
|
||||
/*
|
||||
** Read and classify next option. 'size' is filled with option's size.
|
||||
*/
|
||||
static KOption getoption (Header *h, const char **fmt, int *size) {
|
||||
int opt = *((*fmt)++);
|
||||
*size = 0; /* default */
|
||||
switch (opt) {
|
||||
case 'b': *size = sizeof(char); return Kint;
|
||||
case 'B': *size = sizeof(char); return Kuint;
|
||||
case 'h': *size = sizeof(short); return Kint;
|
||||
case 'H': *size = sizeof(short); return Kuint;
|
||||
case 'l': *size = sizeof(long); return Kint;
|
||||
case 'L': *size = sizeof(long); return Kuint;
|
||||
case 'j': *size = sizeof(lua_Integer); return Kint;
|
||||
case 'J': *size = sizeof(lua_Integer); return Kuint;
|
||||
case 'T': *size = sizeof(size_t); return Kuint;
|
||||
case 'f': *size = sizeof(float); return Kfloat;
|
||||
case 'd': *size = sizeof(double); return Kfloat;
|
||||
case 'n': *size = sizeof(lua_Number); return Kfloat;
|
||||
case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
|
||||
case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
|
||||
case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
|
||||
case 'c':
|
||||
*size = getnum(fmt, -1);
|
||||
if (*size == -1)
|
||||
luaL_error(h->L, "missing size for format option 'c'");
|
||||
return Kchar;
|
||||
case 'z': return Kzstr;
|
||||
case 'x': *size = 1; return Kpadding;
|
||||
case 'X': return Kpaddalign;
|
||||
case ' ': break;
|
||||
case '<': h->islittle = 1; break;
|
||||
case '>': h->islittle = 0; break;
|
||||
case '=': h->islittle = nativeendian.little; break;
|
||||
case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break;
|
||||
default: luaL_error(h->L, "invalid format option '%c'", opt);
|
||||
}
|
||||
return Knop;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Read, classify, and fill other details about the next option.
|
||||
** 'psize' is filled with option's size, 'notoalign' with its
|
||||
** alignment requirements.
|
||||
** Local variable 'size' gets the size to be aligned. (Kpadal option
|
||||
** always gets its full alignment, other options are limited by
|
||||
** the maximum alignment ('maxalign'). Kchar option needs no alignment
|
||||
** despite its size.
|
||||
*/
|
||||
static KOption getdetails (Header *h, size_t totalsize,
|
||||
const char **fmt, int *psize, int *ntoalign) {
|
||||
KOption opt = getoption(h, fmt, psize);
|
||||
int align = *psize; /* usually, alignment follows size */
|
||||
if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
|
||||
if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
|
||||
luaL_argerror(h->L, 1, "invalid next option for option 'X'");
|
||||
}
|
||||
if (align <= 1 || opt == Kchar) /* need no alignment? */
|
||||
*ntoalign = 0;
|
||||
else {
|
||||
if (align > h->maxalign) /* enforce maximum alignment */
|
||||
align = h->maxalign;
|
||||
if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */
|
||||
luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
|
||||
*ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
|
||||
}
|
||||
return opt;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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,
|
||||
int islittle, int size, int neg) {
|
||||
char *buff = luaL_prepbuffsize(b, size);
|
||||
int i;
|
||||
lua_Unsigned n = (lua_Unsigned)m;
|
||||
lua_Unsigned mask = (m >= 0) ? 0 : HIGHERBYTE; /* sign extension */
|
||||
if (littleendian) {
|
||||
for (i = 0; i < size - 1; i++) {
|
||||
buff[i] = (n & MC);
|
||||
n = (n >> NB) | mask;
|
||||
}
|
||||
buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
|
||||
for (i = 1; i < size; i++) {
|
||||
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;
|
||||
}
|
||||
luaL_addsize(b, size); /* add result to buffer */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
|
||||
** given 'islittle' is different from native endianness.
|
||||
*/
|
||||
static void copywithendian (volatile char *dest, volatile const char *src,
|
||||
int size, int islittle) {
|
||||
if (islittle == nativeendian.little) {
|
||||
while (size-- != 0)
|
||||
*(dest++) = *(src++);
|
||||
}
|
||||
else {
|
||||
for (i = size - 1; i > 0; i--) {
|
||||
buff[i] = (n & MC);
|
||||
n = (n >> NB) | mask;
|
||||
}
|
||||
dest += size - 1;
|
||||
while (size-- != 0)
|
||||
*(dest--) = *(src++);
|
||||
}
|
||||
buff[i] = (n & MC); /* last byte */
|
||||
if (size < SZINT) { /* need test for overflow? */
|
||||
/* OK if there are only zeros left in higher bytes,
|
||||
or only ones left (excluding non-signal bits in last byte) */
|
||||
return ((n & ~(lua_Unsigned)MC) == 0 ||
|
||||
(n | SM) == ~(lua_Unsigned)0);
|
||||
}
|
||||
else return 1; /* no overflow can occur with full size */
|
||||
}
|
||||
|
||||
|
||||
static int dumpint_l (lua_State *L) {
|
||||
char buff[MAXINTSIZE];
|
||||
lua_Integer n = luaL_checkinteger(L, 1);
|
||||
int size = getintsize(L, 2);
|
||||
int endian = getendian(L, 3);
|
||||
if (dumpint(buff, n, endian, size))
|
||||
lua_pushlstring(L, buff, size);
|
||||
else
|
||||
luaL_error(L, "integer does not fit into given size (%d)", size);
|
||||
static int str_pack (lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
Header h;
|
||||
const char *fmt = luaL_checkstring(L, 1); /* format string */
|
||||
int arg = 1; /* current argument to pack */
|
||||
size_t totalsize = 0; /* accumulate total size of result */
|
||||
initheader(L, &h);
|
||||
lua_pushnil(L); /* mark to separate arguments from string buffer */
|
||||
luaL_buffinit(L, &b);
|
||||
while (*fmt != '\0') {
|
||||
int size, ntoalign;
|
||||
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||
totalsize += ntoalign + size;
|
||||
while (ntoalign-- > 0)
|
||||
luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */
|
||||
arg++;
|
||||
switch (opt) {
|
||||
case Kint: { /* signed integers */
|
||||
lua_Integer n = luaL_checkinteger(L, arg);
|
||||
if (size < SZINT) { /* need overflow check? */
|
||||
lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
|
||||
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
|
||||
}
|
||||
packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
|
||||
break;
|
||||
}
|
||||
case Kuint: { /* unsigned integers */
|
||||
lua_Integer n = luaL_checkinteger(L, arg);
|
||||
if (size < SZINT) /* need overflow check? */
|
||||
luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
|
||||
arg, "unsigned overflow");
|
||||
packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
|
||||
break;
|
||||
}
|
||||
case Kfloat: { /* floating-point options */
|
||||
volatile Ftypes u;
|
||||
char *buff = luaL_prepbuffsize(&b, size);
|
||||
lua_Number n = luaL_checknumber(L, arg); /* get argument */
|
||||
if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
|
||||
else if (size == sizeof(u.d)) u.d = (double)n;
|
||||
else u.n = n;
|
||||
/* move 'u' to final result, correcting endianness if needed */
|
||||
copywithendian(buff, u.buff, size, h.islittle);
|
||||
luaL_addsize(&b, size);
|
||||
break;
|
||||
}
|
||||
case Kchar: { /* fixed-size string */
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, arg, &len);
|
||||
luaL_argcheck(L, len == (size_t)size, arg, "wrong length");
|
||||
luaL_addlstring(&b, s, size);
|
||||
break;
|
||||
}
|
||||
case Kstring: { /* strings with length count */
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, arg, &len);
|
||||
luaL_argcheck(L, size >= (int)sizeof(size_t) ||
|
||||
len < ((size_t)1 << (size * NB)),
|
||||
arg, "string length does not fit in given size");
|
||||
packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */
|
||||
luaL_addlstring(&b, s, len);
|
||||
totalsize += len;
|
||||
break;
|
||||
}
|
||||
case Kzstr: { /* zero-terminated string */
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, arg, &len);
|
||||
luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
|
||||
luaL_addlstring(&b, s, len);
|
||||
luaL_addchar(&b, '\0'); /* add zero at the end */
|
||||
totalsize += len + 1;
|
||||
break;
|
||||
}
|
||||
case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */
|
||||
case Kpaddalign: case Knop:
|
||||
arg--; /* undo increment */
|
||||
break;
|
||||
}
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* mask to check higher-order byte + signal bit of next (lower) byte */
|
||||
#define HIGHERBYTE1 (HIGHERBYTE | (HIGHERBYTE >> 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");
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
lua_pushinteger(L, (lua_Integer)totalsize);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int undumpint (const char *buff, lua_Integer *res,
|
||||
int littleendian, int size) {
|
||||
lua_Unsigned n = 0;
|
||||
/*
|
||||
** 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,
|
||||
int islittle, int size, int issigned) {
|
||||
lua_Unsigned res = 0;
|
||||
int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
if (i >= SZINT) { /* will throw away a byte? */
|
||||
/* check for overflow: it is OK to throw away leading zeros for a
|
||||
positive number, leading ones for a negative number, and a
|
||||
leading zero byte to allow unsigned integers with a 1 in
|
||||
its "signal bit" */
|
||||
if (!((n & HIGHERBYTE1) == 0 || /* zeros for positive number */
|
||||
(n & HIGHERBYTE1) == HIGHERBYTE1 || /* ones for negative number */
|
||||
(i == size - 1 && (n & HIGHERBYTE) == 0))) /* leading zero */
|
||||
return 0; /* overflow */
|
||||
}
|
||||
n <<= NB;
|
||||
n |= (lua_Unsigned)(unsigned char)buff[littleendian ? size - 1 - i : i];
|
||||
int limit = (size <= SZINT) ? size : SZINT;
|
||||
for (i = limit - 1; i >= 0; i--) {
|
||||
res <<= NB;
|
||||
res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i];
|
||||
}
|
||||
if (size < SZINT) { /* need sign extension? */
|
||||
lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
|
||||
*res = (lua_Integer)((n ^ mask) - mask); /* do sign extension */
|
||||
}
|
||||
else
|
||||
*res = (lua_Integer)n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int undumpint_l (lua_State *L) {
|
||||
lua_Integer res;
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, 1, &len);
|
||||
lua_Integer pos = posrelat(luaL_optinteger(L, 2, 1), len);
|
||||
int size = getintsize(L, 3);
|
||||
int endian = getendian(L, 4);
|
||||
luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,
|
||||
"string too short");
|
||||
if(undumpint(s + pos - 1, &res, endian, size))
|
||||
lua_pushinteger(L, res);
|
||||
else
|
||||
luaL_error(L, "result does not fit into a Lua integer");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void correctendianness (lua_State *L, char *b, int size, int endianarg) {
|
||||
int endian = getendian(L, endianarg);
|
||||
if (endian != nativeendian.little) { /* not native endianness? */
|
||||
int i = 0;
|
||||
while (i < --size) {
|
||||
char temp = b[i];
|
||||
b[i++] = b[size];
|
||||
b[size] = temp;
|
||||
if (size < SZINT) { /* real size smaller than lua_Integer? */
|
||||
if (issigned) { /* needs sign extension? */
|
||||
lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
|
||||
res = ((res ^ mask) - mask); /* do sign extension */
|
||||
}
|
||||
}
|
||||
else if (size > SZINT) { /* must check unread bytes */
|
||||
int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
|
||||
for (i = limit; i < size; i++) {
|
||||
if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
|
||||
luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
|
||||
}
|
||||
}
|
||||
return (lua_Integer)res;
|
||||
}
|
||||
|
||||
|
||||
static int getfloatsize (lua_State *L, int arg) {
|
||||
const char *size = luaL_optstring(L, arg, "n");
|
||||
if (*size == 'n') return sizeof(lua_Number);
|
||||
luaL_argcheck(L, *size == 'd' || *size == 'f', arg,
|
||||
"size must be 'f'/'d'/'n'");
|
||||
return (*size == 'd' ? sizeof(double) : sizeof(float));
|
||||
}
|
||||
|
||||
|
||||
static int dumpfloat_l (lua_State *L) {
|
||||
float f; double d;
|
||||
char *pn; /* pointer to number */
|
||||
lua_Number n = luaL_checknumber(L, 1);
|
||||
int size = getfloatsize(L, 2);
|
||||
if (size == sizeof(lua_Number))
|
||||
pn = (char*)&n;
|
||||
else if (size == sizeof(float)) {
|
||||
f = (float)n;
|
||||
pn = (char*)&f;
|
||||
}
|
||||
else { /* native lua_Number may be neither float nor double */
|
||||
lua_assert(size == sizeof(double));
|
||||
d = (double)n;
|
||||
pn = (char*)&d;
|
||||
static int str_unpack (lua_State *L) {
|
||||
Header h;
|
||||
const char *fmt = luaL_checkstring(L, 1);
|
||||
size_t ld;
|
||||
const char *data = luaL_checklstring(L, 2, &ld);
|
||||
size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
|
||||
int n = 0; /* number of results */
|
||||
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
|
||||
initheader(L, &h);
|
||||
while (*fmt != '\0') {
|
||||
int size, ntoalign;
|
||||
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
||||
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
|
||||
luaL_argerror(L, 2, "data string too short");
|
||||
pos += ntoalign; /* skip alignment */
|
||||
/* stack space for item + next position */
|
||||
luaL_checkstack(L, 2, "too many results");
|
||||
n++;
|
||||
switch (opt) {
|
||||
case Kint:
|
||||
case Kuint: {
|
||||
lua_Integer res = unpackint(L, data + pos, h.islittle, size,
|
||||
(opt == Kint));
|
||||
lua_pushinteger(L, res);
|
||||
break;
|
||||
}
|
||||
case Kfloat: {
|
||||
volatile Ftypes u;
|
||||
lua_Number num;
|
||||
copywithendian(u.buff, data + pos, size, h.islittle);
|
||||
if (size == sizeof(u.f)) num = (lua_Number)u.f;
|
||||
else if (size == sizeof(u.d)) num = (lua_Number)u.d;
|
||||
else num = u.n;
|
||||
lua_pushnumber(L, num);
|
||||
break;
|
||||
}
|
||||
case Kchar: {
|
||||
lua_pushlstring(L, data + pos, size);
|
||||
break;
|
||||
}
|
||||
case Kstring: {
|
||||
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");
|
||||
lua_pushlstring(L, data + pos + size, len);
|
||||
pos += len; /* skip string */
|
||||
break;
|
||||
}
|
||||
case Kzstr: {
|
||||
size_t len = (int)strlen(data + pos);
|
||||
lua_pushlstring(L, data + pos, len);
|
||||
pos += len + 1; /* skip string plus final '\0' */
|
||||
break;
|
||||
}
|
||||
case Kpaddalign: case Kpadding: case Knop:
|
||||
n--; /* undo increment */
|
||||
break;
|
||||
}
|
||||
pos += size;
|
||||
}
|
||||
correctendianness(L, pn, size, 3);
|
||||
lua_pushlstring(L, pn, size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int undumpfloat_l (lua_State *L) {
|
||||
lua_Number res;
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, 1, &len);
|
||||
lua_Integer pos = posrelat(luaL_optinteger(L, 2, 1), len);
|
||||
int size = getfloatsize(L, 3);
|
||||
luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,
|
||||
"string too short");
|
||||
if (size == sizeof(lua_Number)) {
|
||||
memcpy(&res, s + pos - 1, size);
|
||||
correctendianness(L, (char*)&res, size, 4);
|
||||
}
|
||||
else if (size == sizeof(float)) {
|
||||
float f;
|
||||
memcpy(&f, s + pos - 1, size);
|
||||
correctendianness(L, (char*)&f, size, 4);
|
||||
res = (lua_Number)f;
|
||||
}
|
||||
else { /* native lua_Number may be neither float nor double */
|
||||
double d;
|
||||
lua_assert(size == sizeof(double));
|
||||
memcpy(&d, s + pos - 1, size);
|
||||
correctendianness(L, (char*)&d, size, 4);
|
||||
res = (lua_Number)d;
|
||||
}
|
||||
lua_pushnumber(L, res);
|
||||
return 1;
|
||||
lua_pushinteger(L, pos + 1); /* next position */
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
@@ -1176,10 +1484,9 @@ static const luaL_Reg strlib[] = {
|
||||
{"reverse", str_reverse},
|
||||
{"sub", str_sub},
|
||||
{"upper", str_upper},
|
||||
{"dumpfloat", dumpfloat_l},
|
||||
{"dumpint", dumpint_l},
|
||||
{"undumpfloat", undumpfloat_l},
|
||||
{"undumpint", undumpint_l},
|
||||
{"pack", str_pack},
|
||||
{"packsize", str_packsize},
|
||||
{"unpack", str_unpack},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
275
ltable.c
275
ltable.c
@@ -1,31 +1,31 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 2.92 2014/07/18 13:36:14 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 2.110 2015/05/20 16:22:30 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define ltable_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
/*
|
||||
** Implementation of tables (aka arrays, objects, or hash tables).
|
||||
** 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
|
||||
** part. The actual size of the array is the largest `n' such that at
|
||||
** least half the slots between 0 and n are in use.
|
||||
** part. The actual size of the array is the largest 'n' such that
|
||||
** more than half the slots between 1 and n are in use.
|
||||
** 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
|
||||
** 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.
|
||||
** Hence even when the load factor reaches 100%, performance remains good.
|
||||
*/
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#define ltable_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
@@ -40,14 +40,19 @@
|
||||
|
||||
|
||||
/*
|
||||
** Maximum size of array part (MAXASIZE) is 2^MAXBITS. (SIZEINT is the
|
||||
** minimum between size of int and size of LUA_INTEGER; array indices
|
||||
** are limited by both types.)
|
||||
** Maximum size of array part (MAXASIZE) is 2^MAXABITS. MAXABITS is
|
||||
** the largest integer such that MAXASIZE fits in an unsigned int.
|
||||
*/
|
||||
#define SIZEINT \
|
||||
(sizeof(int) < sizeof(LUA_INTEGER) ? sizeof(int) : sizeof(LUA_INTEGER))
|
||||
#define MAXBITS cast_int(SIZEINT * CHAR_BIT - 2)
|
||||
#define MAXASIZE (1 << MAXBITS)
|
||||
#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1)
|
||||
#define MAXASIZE (1u << MAXABITS)
|
||||
|
||||
/*
|
||||
** Maximum size of hash part is 2^MAXHBITS. MAXHBITS is the largest
|
||||
** integer such that 2^MAXHBITS fits in a signed int. (Note that the
|
||||
** maximum number of elements in a table, 2^MAXABITS + 2^MAXHBITS, still
|
||||
** fits comfortably in an unsigned int.)
|
||||
*/
|
||||
#define MAXHBITS (MAXABITS - 1)
|
||||
|
||||
|
||||
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
|
||||
@@ -64,7 +69,7 @@
|
||||
#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_)
|
||||
@@ -78,35 +83,37 @@ static const Node dummynode_ = {
|
||||
|
||||
|
||||
/*
|
||||
** Checks whether a float has a value representable as a lua_Integer
|
||||
** (and does the conversion if so)
|
||||
** Hash for floating-point numbers.
|
||||
** The main computation should be just
|
||||
** n = frepx(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 ((x) == l_floor(x)) /* integral value? */
|
||||
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) {
|
||||
#if !defined(l_hashfloat)
|
||||
static int l_hashfloat (lua_Number n) {
|
||||
int i;
|
||||
n = l_mathop(frexp)(n, &i) * cast_num(INT_MAX - DBL_MAX_EXP);
|
||||
i += cast_int(n);
|
||||
if (i < 0) {
|
||||
if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */
|
||||
i = 0; /* handle INT_MIN */
|
||||
i = -i; /* must be a positive value */
|
||||
lua_Integer ni;
|
||||
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
|
||||
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
|
||||
lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == HUGE_VAL);
|
||||
return 0;
|
||||
}
|
||||
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)
|
||||
*/
|
||||
static Node *mainposition (const Table *t, const TValue *key) {
|
||||
@@ -114,13 +121,13 @@ static Node *mainposition (const Table *t, const TValue *key) {
|
||||
case LUA_TNUMINT:
|
||||
return hashint(t, ivalue(key));
|
||||
case LUA_TNUMFLT:
|
||||
return hashfloat(t, fltvalue(key));
|
||||
return hashmod(t, l_hashfloat(fltvalue(key)));
|
||||
case LUA_TSHRSTR:
|
||||
return hashstr(t, tsvalue(key));
|
||||
case LUA_TLNGSTR: {
|
||||
TString *s = tsvalue(key);
|
||||
if (s->extra == 0) { /* no hash? */
|
||||
s->hash = luaS_hash(getstr(s), s->len, s->hash);
|
||||
s->hash = luaS_hash(getstr(s), s->u.lnglen, s->hash);
|
||||
s->extra = 1; /* now it has its hash */
|
||||
}
|
||||
return hashstr(t, tsvalue(key));
|
||||
@@ -138,45 +145,45 @@ static Node *mainposition (const Table *t, const TValue *key) {
|
||||
|
||||
|
||||
/*
|
||||
** returns the index for `key' if `key' is an appropriate key to live in
|
||||
** the array part of the table, -1 otherwise.
|
||||
** returns the index for 'key' if 'key' is an appropriate key to live in
|
||||
** the array part of the table, 0 otherwise.
|
||||
*/
|
||||
static int arrayindex (const TValue *key) {
|
||||
static unsigned int arrayindex (const TValue *key) {
|
||||
if (ttisinteger(key)) {
|
||||
lua_Integer k = ivalue(key);
|
||||
if (0 < k && k <= MAXASIZE) /* is `key' an appropriate array index? */
|
||||
return cast_int(k);
|
||||
if (0 < k && (lua_Unsigned)k <= MAXASIZE)
|
||||
return cast(unsigned int, k); /* 'key' is an appropriate array index */
|
||||
}
|
||||
return -1; /* `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
|
||||
** beginning of a traversal is signaled by -1.
|
||||
** beginning of a traversal is signaled by 0.
|
||||
*/
|
||||
static int findindex (lua_State *L, Table *t, StkId key) {
|
||||
int i;
|
||||
if (ttisnil(key)) return -1; /* first iteration */
|
||||
static unsigned int findindex (lua_State *L, Table *t, StkId key) {
|
||||
unsigned int i;
|
||||
if (ttisnil(key)) return 0; /* first iteration */
|
||||
i = arrayindex(key);
|
||||
if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
|
||||
return i-1; /* yes; that's the index (corrected to C) */
|
||||
if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */
|
||||
return i; /* yes; that's the index */
|
||||
else {
|
||||
int nx;
|
||||
Node *n = mainposition(t, key);
|
||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
||||
/* key may be dead already, but it is ok to use it in `next' */
|
||||
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||
/* key may be dead already, but it is ok to use it in 'next' */
|
||||
if (luaV_rawequalobj(gkey(n), key) ||
|
||||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
||||
deadvalue(gkey(n)) == gcvalue(key))) {
|
||||
i = cast_int(n - gnode(t, 0)); /* key index in hash table */
|
||||
/* hash elements are numbered after array ones */
|
||||
return i + t->sizearray;
|
||||
return (i + 1) + t->sizearray;
|
||||
}
|
||||
nx = gnext(n);
|
||||
if (nx == 0)
|
||||
luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
|
||||
luaG_runerror(L, "invalid key to 'next'"); /* key not found */
|
||||
else n += nx;
|
||||
}
|
||||
}
|
||||
@@ -184,15 +191,15 @@ static int findindex (lua_State *L, Table *t, StkId key) {
|
||||
|
||||
|
||||
int luaH_next (lua_State *L, Table *t, StkId key) {
|
||||
int i = findindex(L, t, key); /* find original element */
|
||||
for (i++; i < t->sizearray; i++) { /* try first array part */
|
||||
unsigned int i = findindex(L, t, key); /* find original element */
|
||||
for (; i < t->sizearray; i++) { /* try first array part */
|
||||
if (!ttisnil(&t->array[i])) { /* a non-nil value? */
|
||||
setivalue(key, i + 1);
|
||||
setobj2s(L, key+1, &t->array[i]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
|
||||
for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */
|
||||
if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
|
||||
setobj2s(L, key, gkey(gnode(t, i)));
|
||||
setobj2s(L, key+1, gval(gnode(t, i)));
|
||||
@@ -209,32 +216,38 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
|
||||
** ==============================================================
|
||||
*/
|
||||
|
||||
|
||||
static int computesizes (int nums[], int *narray) {
|
||||
/*
|
||||
** 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
|
||||
** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of
|
||||
** 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 *pna) {
|
||||
int i;
|
||||
int twotoi; /* 2^i */
|
||||
int a = 0; /* number of elements smaller than 2^i */
|
||||
int na = 0; /* number of elements to go to array part */
|
||||
int n = 0; /* optimal size for array part */
|
||||
for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
|
||||
unsigned int twotoi; /* 2^i (candidate for optimal size) */
|
||||
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 optimal = 0; /* optimal size for array part */
|
||||
/* 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) {
|
||||
a += nums[i];
|
||||
if (a > twotoi/2) { /* more than half elements present? */
|
||||
n = twotoi; /* optimal size (till now) */
|
||||
na = a; /* all elements smaller than n will go to array part */
|
||||
optimal = twotoi; /* optimal size (till now) */
|
||||
na = a; /* all elements up to 'optimal' will go to array part */
|
||||
}
|
||||
}
|
||||
if (a == *narray) break; /* all elements already counted */
|
||||
}
|
||||
*narray = n;
|
||||
lua_assert(*narray/2 <= na && na <= *narray);
|
||||
return na;
|
||||
lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
|
||||
*pna = na;
|
||||
return optimal;
|
||||
}
|
||||
|
||||
|
||||
static int countint (const TValue *key, int *nums) {
|
||||
int k = arrayindex(key);
|
||||
if (k > 0) { /* is `key' an appropriate array index? */
|
||||
static int countint (const TValue *key, unsigned int *nums) {
|
||||
unsigned int k = arrayindex(key);
|
||||
if (k != 0) { /* is 'key' an appropriate array index? */
|
||||
nums[luaO_ceillog2(k)]++; /* count as such */
|
||||
return 1;
|
||||
}
|
||||
@@ -243,20 +256,26 @@ static int countint (const TValue *key, int *nums) {
|
||||
}
|
||||
|
||||
|
||||
static int numusearray (const Table *t, 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) {
|
||||
int lg;
|
||||
int ttlg; /* 2^lg */
|
||||
int ause = 0; /* summation of `nums' */
|
||||
int i = 1; /* count to traverse all array keys */
|
||||
for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
|
||||
int lc = 0; /* counter */
|
||||
int lim = ttlg;
|
||||
unsigned int ttlg; /* 2^lg */
|
||||
unsigned int ause = 0; /* summation of 'nums' */
|
||||
unsigned int i = 1; /* count to traverse all array keys */
|
||||
/* traverse each slice */
|
||||
for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
|
||||
unsigned int lc = 0; /* counter */
|
||||
unsigned int lim = ttlg;
|
||||
if (lim > t->sizearray) {
|
||||
lim = t->sizearray; /* adjust upper limit */
|
||||
if (i > lim)
|
||||
break; /* no more elements to count */
|
||||
}
|
||||
/* count elements in range (2^(lg-1), 2^lg] */
|
||||
/* count elements in range (2^(lg - 1), 2^lg] */
|
||||
for (; i <= lim; i++) {
|
||||
if (!ttisnil(&t->array[i-1]))
|
||||
lc++;
|
||||
@@ -268,9 +287,9 @@ static int numusearray (const Table *t, int *nums) {
|
||||
}
|
||||
|
||||
|
||||
static int numusehash (const Table *t, int *nums, int *pnasize) {
|
||||
static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
|
||||
int totaluse = 0; /* total number of elements */
|
||||
int ause = 0; /* summation of `nums' */
|
||||
int ause = 0; /* elements added to 'nums' (can go to array part) */
|
||||
int i = sizenode(t);
|
||||
while (i--) {
|
||||
Node *n = &t->node[i];
|
||||
@@ -279,13 +298,13 @@ static int numusehash (const Table *t, int *nums, int *pnasize) {
|
||||
totaluse++;
|
||||
}
|
||||
}
|
||||
*pnasize += ause;
|
||||
*pna += ause;
|
||||
return totaluse;
|
||||
}
|
||||
|
||||
|
||||
static void setarrayvector (lua_State *L, Table *t, int size) {
|
||||
int i;
|
||||
static void setarrayvector (lua_State *L, Table *t, unsigned int size) {
|
||||
unsigned int i;
|
||||
luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
|
||||
for (i=t->sizearray; i<size; i++)
|
||||
setnilvalue(&t->array[i]);
|
||||
@@ -293,20 +312,20 @@ static void setarrayvector (lua_State *L, Table *t, int size) {
|
||||
}
|
||||
|
||||
|
||||
static void setnodevector (lua_State *L, Table *t, int size) {
|
||||
static void setnodevector (lua_State *L, Table *t, unsigned int size) {
|
||||
int lsize;
|
||||
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;
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
lsize = luaO_ceillog2(size);
|
||||
if (lsize > MAXBITS)
|
||||
if (lsize > MAXHBITS)
|
||||
luaG_runerror(L, "table overflow");
|
||||
size = twoto(lsize);
|
||||
t->node = luaM_newvector(L, size, Node);
|
||||
for (i=0; i<size; i++) {
|
||||
for (i = 0; i < (int)size; i++) {
|
||||
Node *n = gnode(t, i);
|
||||
gnext(n) = 0;
|
||||
setnilvalue(wgkey(n));
|
||||
@@ -318,9 +337,11 @@ static void setnodevector (lua_State *L, Table *t, int size) {
|
||||
}
|
||||
|
||||
|
||||
void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
int i;
|
||||
int oldasize = t->sizearray;
|
||||
void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
||||
unsigned int nhsize) {
|
||||
unsigned int i;
|
||||
int j;
|
||||
unsigned int oldasize = t->sizearray;
|
||||
int oldhsize = t->lsizenode;
|
||||
Node *nold = t->node; /* save old hash ... */
|
||||
if (nasize > oldasize) /* array part must grow? */
|
||||
@@ -338,8 +359,8 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
|
||||
}
|
||||
/* re-insert elements from hash part */
|
||||
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
|
||||
Node *old = nold+i;
|
||||
for (j = twoto(oldhsize) - 1; j >= 0; j--) {
|
||||
Node *old = nold + j;
|
||||
if (!ttisnil(gval(old))) {
|
||||
/* doesn't need barrier/invalidate cache, as entry was
|
||||
already present in the table */
|
||||
@@ -347,32 +368,35 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
}
|
||||
}
|
||||
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 */
|
||||
}
|
||||
|
||||
|
||||
void luaH_resizearray (lua_State *L, Table *t, int nasize) {
|
||||
void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
|
||||
int nsize = isdummy(t->node) ? 0 : sizenode(t);
|
||||
luaH_resize(L, t, nasize, nsize);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
|
||||
*/
|
||||
static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
||||
int nasize, na;
|
||||
int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */
|
||||
unsigned int asize; /* optimal size for array part */
|
||||
unsigned int na; /* number of keys in the array part */
|
||||
unsigned int nums[MAXABITS + 1];
|
||||
int i;
|
||||
int totaluse;
|
||||
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
|
||||
nasize = numusearray(t, nums); /* count keys in array part */
|
||||
totaluse = nasize; /* all those keys are integer keys */
|
||||
totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
|
||||
for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
|
||||
na = numusearray(t, nums); /* count keys in array part */
|
||||
totaluse = na; /* all those keys are integer keys */
|
||||
totaluse += numusehash(t, nums, &na); /* count keys in hash part */
|
||||
/* count extra key */
|
||||
nasize += countint(ek, nums);
|
||||
na += countint(ek, nums);
|
||||
totaluse++;
|
||||
/* compute new size for array part */
|
||||
na = computesizes(nums, &nasize);
|
||||
asize = computesizes(nums, &na);
|
||||
/* resize the table to new computed sizes */
|
||||
luaH_resize(L, t, nasize, totaluse - na);
|
||||
luaH_resize(L, t, asize, totaluse - na);
|
||||
}
|
||||
|
||||
|
||||
@@ -425,14 +449,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
TValue aux;
|
||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||
else if (ttisfloat(key)) {
|
||||
lua_Number n = fltvalue(key);
|
||||
lua_Integer k;
|
||||
if (luai_numisnan(n))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
if (numisinteger(n, &k)) { /* index is int? */
|
||||
if (luaV_tointeger(key, &k, 0)) { /* index is int? */
|
||||
setivalue(&aux, k);
|
||||
key = &aux; /* insert it as an integer */
|
||||
}
|
||||
else if (luai_numisnan(fltvalue(key)))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
}
|
||||
mp = mainposition(t, key);
|
||||
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
|
||||
@@ -440,7 +463,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
Node *f = getfreepos(t); /* get a free place */
|
||||
if (f == NULL) { /* cannot find a free place? */
|
||||
rehash(L, t, key); /* grow table */
|
||||
/* whatever called 'newkey' take care of TM cache and GC barrier */
|
||||
/* whatever called 'newkey' takes care of TM cache and GC barrier */
|
||||
return luaH_set(L, t, key); /* insert key into grown table */
|
||||
}
|
||||
lua_assert(!isdummy(f));
|
||||
@@ -466,7 +489,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
mp = f;
|
||||
}
|
||||
}
|
||||
setkey(L, &mp->i_key, key);
|
||||
setnodekey(L, &mp->i_key, key);
|
||||
luaC_barrierback(L, t, key);
|
||||
lua_assert(ttisnil(gval(mp)));
|
||||
return gval(mp);
|
||||
@@ -478,11 +501,11 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
*/
|
||||
const TValue *luaH_getint (Table *t, lua_Integer key) {
|
||||
/* (1 <= key && key <= t->sizearray) */
|
||||
if (l_castS2U(key - 1) < cast(unsigned int, t->sizearray))
|
||||
if (l_castS2U(key - 1) < t->sizearray)
|
||||
return &t->array[key - 1];
|
||||
else {
|
||||
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)
|
||||
return gval(n); /* that's it */
|
||||
else {
|
||||
@@ -502,7 +525,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
|
||||
const TValue *luaH_getstr (Table *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
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);
|
||||
if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
|
||||
return gval(n); /* that's it */
|
||||
@@ -526,13 +549,13 @@ const TValue *luaH_get (Table *t, const TValue *key) {
|
||||
case LUA_TNIL: return luaO_nilobject;
|
||||
case LUA_TNUMFLT: {
|
||||
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 */
|
||||
/* else go through */
|
||||
}
|
||||
/* else... */
|
||||
} /* FALLTHROUGH */
|
||||
default: {
|
||||
Node *n = mainposition(t, key);
|
||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
||||
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||
if (luaV_rawequalobj(gkey(n), key))
|
||||
return gval(n); /* that's it */
|
||||
else {
|
||||
@@ -576,7 +599,7 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
|
||||
static int unbound_search (Table *t, unsigned int j) {
|
||||
unsigned int i = j; /* i is zero or a present index */
|
||||
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))) {
|
||||
i = j;
|
||||
if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */
|
||||
@@ -598,7 +621,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).
|
||||
*/
|
||||
int luaH_getn (Table *t) {
|
||||
|
||||
9
ltable.h
9
ltable.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 2.18 2013/08/30 16:01:37 roberto Exp roberto $
|
||||
** $Id: ltable.h,v 2.19 2014/07/29 16:22:24 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -30,14 +30,15 @@
|
||||
|
||||
LUAI_FUNC const TValue *luaH_getint (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_getstr (Table *t, TString *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_set (lua_State *L, Table *t, const TValue *key);
|
||||
LUAI_FUNC Table *luaH_new (lua_State *L);
|
||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize);
|
||||
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
|
||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
||||
unsigned int nhsize);
|
||||
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
|
||||
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
|
||||
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
|
||||
LUAI_FUNC int luaH_getn (Table *t);
|
||||
|
||||
86
ltablib.c
86
ltablib.c
@@ -1,16 +1,18 @@
|
||||
/*
|
||||
** $Id: ltablib.c,v 1.72 2014/07/25 18:46:00 roberto Exp roberto $
|
||||
** $Id: ltablib.c,v 1.79 2014/11/02 19:19:04 roberto Exp roberto $
|
||||
** Library for Table Manipulation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define ltablib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define ltablib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -27,42 +29,26 @@ typedef struct {
|
||||
} TabA;
|
||||
|
||||
|
||||
/*
|
||||
** equivalent to 'lua_rawgeti', but not raw
|
||||
*/
|
||||
static int geti (lua_State *L, int idx, lua_Integer n) {
|
||||
lua_pushinteger(L, n);
|
||||
return lua_gettable(L, idx); /* assume 'idx' is not negative */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** equivalent to 'lua_rawseti', but not raw
|
||||
*/
|
||||
static void seti (lua_State *L, int idx, lua_Integer n) {
|
||||
lua_pushinteger(L, n);
|
||||
lua_rotate(L, -2, 1); /* exchange key and value */
|
||||
lua_settable(L, idx); /* assume 'idx' is not negative */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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 void checktab (lua_State *L, int arg, TabA *ta) {
|
||||
luaL_checktype(L, arg, LUA_TTABLE);
|
||||
if (!lua_getmetatable(L, arg)) { /* fast track */
|
||||
ta->geti = lua_rawgeti; /* with no metatable, all is raw */
|
||||
ta->seti = lua_rawseti;
|
||||
}
|
||||
else {
|
||||
ta->geti = NULL; ta->seti = NULL;
|
||||
if (lua_getmetatable(L, arg)) {
|
||||
lua_pushliteral(L, "__index"); /* 'index' metamethod */
|
||||
ta->geti = (lua_rawget(L, -2) == LUA_TNIL) ? lua_rawgeti : geti;
|
||||
if (lua_rawget(L, -2) != LUA_TNIL)
|
||||
ta->geti = lua_geti;
|
||||
lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */
|
||||
ta->seti = (lua_rawget(L, -3) == LUA_TNIL) ? lua_rawseti : seti;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +93,7 @@ static int tinsert (lua_State *L) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
|
||||
return luaL_error(L, "wrong number of arguments to 'insert'");
|
||||
}
|
||||
}
|
||||
(*ta.seti)(L, 1, pos); /* t[pos] = v */
|
||||
@@ -132,25 +118,25 @@ static int tremove (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int tcopy (lua_State *L) {
|
||||
static int tmove (lua_State *L) {
|
||||
TabA ta;
|
||||
lua_Integer f = luaL_checkinteger(L, 2);
|
||||
lua_Integer e = luaL_checkinteger(L, 3);
|
||||
lua_Integer t;
|
||||
int tt = 4; /* destination table */
|
||||
/* the following restriction avoids several problems with overflows */
|
||||
luaL_argcheck(L, f > 0, 2, "initial position must be positive");
|
||||
if (lua_istable(L, tt))
|
||||
t = luaL_checkinteger(L, 5);
|
||||
else {
|
||||
tt = 1; /* destination table is equal to source */
|
||||
t = luaL_checkinteger(L, 4);
|
||||
}
|
||||
lua_Integer t = luaL_checkinteger(L, 4);
|
||||
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
|
||||
if (e >= f) { /* otherwise, nothing to move */
|
||||
lua_Integer n, i;
|
||||
ta.geti = (!luaL_getmetafield(L, 1, "__index")) ? lua_rawgeti : geti;
|
||||
ta.seti = (!luaL_getmetafield(L, tt, "__newindex")) ? lua_rawseti : seti;
|
||||
ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL)
|
||||
? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti)
|
||||
: lua_geti;
|
||||
ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL)
|
||||
? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
|
||||
: lua_seti;
|
||||
luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
|
||||
"too many elements to move");
|
||||
n = e - f + 1; /* number of elements to move */
|
||||
luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
|
||||
"destination wrap around");
|
||||
if (t > f) {
|
||||
for (i = n - 1; i >= 0; i--) {
|
||||
(*ta.geti)(L, 1, f + i);
|
||||
@@ -172,8 +158,8 @@ static int tcopy (lua_State *L) {
|
||||
static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) {
|
||||
(*ta->geti)(L, 1, i);
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_error(L, "invalid value (%s) at index %d in table for "
|
||||
LUA_QL("concat"), luaL_typename(L, -1), i);
|
||||
luaL_error(L, "invalid value (%s) at index %d in table for 'concat'",
|
||||
luaL_typename(L, -1), i);
|
||||
luaL_addvalue(b);
|
||||
}
|
||||
|
||||
@@ -243,7 +229,7 @@ static int unpack (lua_State *L) {
|
||||
/*
|
||||
** {======================================================
|
||||
** Quicksort
|
||||
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
|
||||
** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
|
||||
** Addison-Wesley, 1993.)
|
||||
** =======================================================
|
||||
*/
|
||||
@@ -259,7 +245,7 @@ static int sort_comp (lua_State *L, int a, int b) {
|
||||
int res;
|
||||
lua_pushvalue(L, 2);
|
||||
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);
|
||||
res = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
@@ -355,7 +341,7 @@ static const luaL_Reg tab_funcs[] = {
|
||||
{"pack", pack},
|
||||
{"unpack", unpack},
|
||||
{"remove", tremove},
|
||||
{"copy", tcopy},
|
||||
{"move", tmove},
|
||||
{"sort", sort},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
188
ltests.c
188
ltests.c
@@ -1,18 +1,21 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 2.181 2014/07/19 15:14:46 roberto Exp roberto $
|
||||
** $Id: ltests.c,v 2.204 2015/03/13 16:24:50 roberto Exp roberto $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define ltests_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ltests_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lapi.h"
|
||||
@@ -46,6 +49,9 @@ int islocked = 0;
|
||||
#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) {
|
||||
lua_pushstring(L, name);
|
||||
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 */
|
||||
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);
|
||||
/* initialize marks after block */
|
||||
for (i = 0; i < MARKSIZE; i++)
|
||||
@@ -218,7 +224,7 @@ static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
|
||||
|
||||
|
||||
static void checktable (global_State *g, Table *h) {
|
||||
int i;
|
||||
unsigned int i;
|
||||
Node *n, *limit = gnode(h, sizenode(h));
|
||||
GCObject *hgc = obj2gco(h);
|
||||
checkobjref(g, hgc, h->metatable);
|
||||
@@ -280,10 +286,14 @@ static void checkLclosure (global_State *g, LClosure *cl) {
|
||||
}
|
||||
|
||||
|
||||
static int lua_checkpc (CallInfo *ci) {
|
||||
static int lua_checkpc (lua_State *L, CallInfo *ci) {
|
||||
if (!isLua(ci)) return 1;
|
||||
else {
|
||||
Proto *p = ci_func(ci)->p;
|
||||
/* if function yielded (inside a hook), real 'func' is in 'extra' field */
|
||||
StkId f = (L->status != LUA_YIELD || ci != L->ci)
|
||||
? ci->func
|
||||
: restorestack(L, ci->extra);
|
||||
Proto *p = clLvalue(f)->p;
|
||||
return p->code <= ci->u.l.savedpc &&
|
||||
ci->u.l.savedpc <= p->code + p->sizecode;
|
||||
}
|
||||
@@ -299,11 +309,11 @@ static void checkstack (global_State *g, lua_State *L1) {
|
||||
lua_assert(upisopen(uv)); /* must be open */
|
||||
for (ci = L1->ci; ci != NULL; ci = ci->previous) {
|
||||
lua_assert(ci->top <= L1->stack_last);
|
||||
lua_assert(lua_checkpc(ci));
|
||||
lua_assert(lua_checkpc(L1, ci));
|
||||
}
|
||||
if (L1->stack) {
|
||||
for (o = L1->stack; o < L1->top; o++)
|
||||
checkliveness(g, o);
|
||||
if (L1->stack) { /* complete thread? */
|
||||
for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++)
|
||||
checkliveness(g, o); /* entire stack must have valid values */
|
||||
}
|
||||
else lua_assert(L1->stacksize == 0);
|
||||
}
|
||||
@@ -530,7 +540,7 @@ static int listk (lua_State *L) {
|
||||
|
||||
static int listlocals (lua_State *L) {
|
||||
Proto *p;
|
||||
int pc = luaL_checkint(L, 2) - 1;
|
||||
int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
|
||||
int i = 0;
|
||||
const char *name;
|
||||
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||
@@ -545,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) {
|
||||
lua_createtable(L, 0, 5);
|
||||
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, "MAXSTACK", MAXSTACK);
|
||||
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
||||
return 1;
|
||||
}
|
||||
@@ -598,9 +621,12 @@ static int gc_color (lua_State *L) {
|
||||
o = obj_at(L, 1);
|
||||
if (!iscollectable(o))
|
||||
lua_pushstring(L, "no collectable");
|
||||
else
|
||||
lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
|
||||
isblack(gcvalue(o)) ? "black" : "grey");
|
||||
else {
|
||||
GCObject *obj = gcvalue(o);
|
||||
lua_pushstring(L, isdead(G(L), obj) ? "dead" :
|
||||
iswhite(obj) ? "white" :
|
||||
isblack(obj) ? "black" : "grey");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -650,13 +676,13 @@ static int stacklevel (lua_State *L) {
|
||||
lua_pushinteger(L, (L->top - L->stack));
|
||||
lua_pushinteger(L, (L->stack_last - L->stack));
|
||||
lua_pushinteger(L, (unsigned long)&a);
|
||||
return 5;
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
static int table_query (lua_State *L) {
|
||||
const Table *t;
|
||||
int i = luaL_optint(L, 2, -1);
|
||||
int i = cast_int(luaL_optinteger(L, 2, -1));
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
t = hvalue(obj_at(L, 1));
|
||||
if (i == -1) {
|
||||
@@ -664,7 +690,7 @@ static int table_query (lua_State *L) {
|
||||
lua_pushinteger(L, luaH_isdummy(t->node) ? 0 : sizenode(t));
|
||||
lua_pushinteger(L, t->lastfree - t->node);
|
||||
}
|
||||
else if (i < t->sizearray) {
|
||||
else if ((unsigned int)i < t->sizearray) {
|
||||
lua_pushinteger(L, i);
|
||||
pushobject(L, &t->array[i]);
|
||||
lua_pushnil(L);
|
||||
@@ -689,7 +715,7 @@ static int table_query (lua_State *L) {
|
||||
|
||||
static int string_query (lua_State *L) {
|
||||
stringtable *tb = &G(L)->strt;
|
||||
int s = luaL_optint(L, 1, 0) - 1;
|
||||
int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
|
||||
if (s == -1) {
|
||||
lua_pushinteger(L ,tb->size);
|
||||
lua_pushinteger(L ,tb->nuse);
|
||||
@@ -698,7 +724,7 @@ static int string_query (lua_State *L) {
|
||||
else if (s < tb->size) {
|
||||
TString *ts;
|
||||
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);
|
||||
api_incr_top(L);
|
||||
n++;
|
||||
@@ -720,21 +746,21 @@ static int tref (lua_State *L) {
|
||||
|
||||
static int getref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkint(L, 1));
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
|
||||
lua_assert(lua_gettop(L) == level+1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int unref (lua_State *L) {
|
||||
int level = lua_gettop(L);
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, luaL_checkint(L, 1));
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
|
||||
lua_assert(lua_gettop(L) == level);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int upvalue (lua_State *L) {
|
||||
int n = luaL_checkint(L, 2);
|
||||
int n = cast_int(luaL_checkinteger(L, 2));
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
if (lua_isnone(L, 3)) {
|
||||
const char *name = lua_getupvalue(L, 1, n);
|
||||
@@ -883,19 +909,71 @@ static int doremote (lua_State *L) {
|
||||
|
||||
|
||||
static int int2fb_aux (lua_State *L) {
|
||||
int b = luaO_int2fb(luaL_checkint(L, 1));
|
||||
int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1));
|
||||
lua_pushinteger(L, b);
|
||||
lua_pushinteger(L, luaO_fb2int(b));
|
||||
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
|
||||
** language with calls to the API, so the test can be driven by Lua code
|
||||
** =======================================================
|
||||
** =====================================================================
|
||||
*/
|
||||
|
||||
|
||||
@@ -918,7 +996,7 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
|
||||
int sig = 1;
|
||||
skip(pc);
|
||||
if (**pc == '.') {
|
||||
res = lua_tointeger(L1, -1);
|
||||
res = cast_int(lua_tointeger(L1, -1));
|
||||
lua_pop(L1, 1);
|
||||
(*pc)++;
|
||||
return res;
|
||||
@@ -979,7 +1057,7 @@ static void pushcode (lua_State *L, int code) {
|
||||
|
||||
|
||||
static int testC (lua_State *L);
|
||||
static int Cfunck (lua_State *L, int status, lua_Ctx ctx);
|
||||
static int Cfunck (lua_State *L, int status, lua_KContext ctx);
|
||||
|
||||
/*
|
||||
** arithmetic operation encoding for 'arith' instruction
|
||||
@@ -989,7 +1067,7 @@ static int Cfunck (lua_State *L, int status, lua_Ctx ctx);
|
||||
** LUA_OPUNM -> _
|
||||
** LUA_OPBNOT -> !
|
||||
*/
|
||||
static char ops[] = "+-*%^/\\&|~<>_!";
|
||||
static const char ops[] = "+-*%^/\\&|~<>_!";
|
||||
|
||||
static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
char buff[300];
|
||||
@@ -1025,12 +1103,18 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
}
|
||||
else if EQ("checkstack") {
|
||||
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") {
|
||||
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 b = getindex;
|
||||
lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
|
||||
lua_pushboolean(L1, lua_compare(L1, a, b, op));
|
||||
}
|
||||
else if EQ("concat") {
|
||||
lua_concat(L1, getnum);
|
||||
@@ -1106,9 +1190,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
||||
}
|
||||
else if EQ("loadstring") {
|
||||
size_t sl;
|
||||
const char *s = luaL_checklstring(L1, getnum, &sl);
|
||||
luaL_loadbuffer(L1, s, sl, s);
|
||||
const char *s = luaL_checkstring(L1, getnum);
|
||||
luaL_loadstring(L1, s);
|
||||
}
|
||||
else if EQ("newmetatable") {
|
||||
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
||||
@@ -1131,7 +1214,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
else if EQ("pcall") {
|
||||
int narg = getnum;
|
||||
int nres = getnum;
|
||||
status = lua_pcall(L1, narg, nres, 0);
|
||||
status = lua_pcall(L1, narg, nres, getnum);
|
||||
}
|
||||
else if EQ("pcallk") {
|
||||
int narg = getnum;
|
||||
@@ -1148,15 +1231,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
||||
lua_pop(L1, 1);
|
||||
}
|
||||
else {
|
||||
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 printstack(L1);
|
||||
}
|
||||
else if EQ("pushbool") {
|
||||
lua_pushboolean(L1, getnum);
|
||||
@@ -1189,6 +1264,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
int t = getindex;
|
||||
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") {
|
||||
lua_remove(L1, getnum);
|
||||
}
|
||||
@@ -1237,6 +1320,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
int i = getindex;
|
||||
lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
|
||||
}
|
||||
else if EQ("error") {
|
||||
lua_error(L1);
|
||||
}
|
||||
else if EQ("throw") {
|
||||
#if defined(__cplusplus)
|
||||
static struct X { int x; } x;
|
||||
@@ -1264,7 +1350,7 @@ static struct X { int x; } x;
|
||||
else if EQ("tostring") {
|
||||
const char *s = lua_tostring(L1, getindex);
|
||||
const char *s1 = lua_pushstring(L1, s);
|
||||
lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
|
||||
lua_assert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
|
||||
}
|
||||
else if EQ("type") {
|
||||
lua_pushstring(L1, luaL_typename(L1, getnum));
|
||||
@@ -1316,7 +1402,7 @@ static int Cfunc (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int Cfunck (lua_State *L, int status, lua_Ctx ctx) {
|
||||
static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
|
||||
pushcode(L, status);
|
||||
lua_setglobal(L, "status");
|
||||
lua_pushinteger(L, ctx);
|
||||
@@ -1359,7 +1445,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) {
|
||||
if (*scpt == '\0') { /* no script? */
|
||||
@@ -1386,7 +1472,7 @@ static int sethook (lua_State *L) {
|
||||
else {
|
||||
const char *scpt = luaL_checkstring(L, 1);
|
||||
const char *smask = luaL_checkstring(L, 2);
|
||||
int count = luaL_optint(L, 3, 0);
|
||||
int count = cast_int(luaL_optinteger(L, 3, 0));
|
||||
int mask = 0;
|
||||
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
||||
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
||||
@@ -1429,11 +1515,13 @@ static const struct luaL_Reg tests_funcs[] = {
|
||||
{"getref", getref},
|
||||
{"hash", hash_query},
|
||||
{"int2fb", int2fb_aux},
|
||||
{"log2", log2_aux},
|
||||
{"limits", get_limits},
|
||||
{"listcode", listcode},
|
||||
{"listk", listk},
|
||||
{"listlocals", listlocals},
|
||||
{"loadlib", loadlib},
|
||||
{"checkpanic", checkpanic},
|
||||
{"newstate", newstate},
|
||||
{"newuserdata", newuserdata},
|
||||
{"num2int", num2int},
|
||||
|
||||
17
ltests.h
17
ltests.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltests.h,v 2.38 2014/07/24 14:00:16 roberto Exp roberto $
|
||||
** $Id: ltests.h,v 2.46 2014/12/19 13:33:06 roberto Exp roberto $
|
||||
** Internal Header for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -14,7 +14,7 @@
|
||||
#undef LUA_COMPAT_MATHLIB
|
||||
#undef LUA_COMPAT_IPAIRS
|
||||
#undef LUA_COMPAT_BITLIB
|
||||
#undef LUA_COMPAT_APIUNSIGNED
|
||||
#undef LUA_COMPAT_APIINTCASTS
|
||||
#undef LUA_COMPAT_FLOATSTRING
|
||||
#undef LUA_COMPAT_UNPACK
|
||||
#undef LUA_COMPAT_LOADERS
|
||||
@@ -46,7 +46,7 @@ typedef struct Memcontrol {
|
||||
unsigned long objcount[LUA_NUMTAGS];
|
||||
} Memcontrol;
|
||||
|
||||
extern Memcontrol l_memcontrol;
|
||||
LUA_API Memcontrol l_memcontrol;
|
||||
|
||||
|
||||
/*
|
||||
@@ -81,9 +81,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)
|
||||
#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol)
|
||||
@@ -100,9 +101,13 @@ void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize);
|
||||
#define MINSTRTABSIZE 2
|
||||
|
||||
|
||||
/* make stack-overflow tests run faster */
|
||||
#undef LUAI_MAXSTACK
|
||||
#define LUAI_MAXSTACK 50000
|
||||
|
||||
|
||||
#undef LUAI_USER_ALIGNMENT_T
|
||||
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
21
ltm.c
21
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.33 2014/11/21 12:15:57 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define ltm_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
@@ -84,7 +86,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
||||
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
||||
const TValue *p2, TValue *p3, int hasres) {
|
||||
ptrdiff_t result = savestack(L, p3);
|
||||
setobj2s(L, L->top++, f); /* push function */
|
||||
setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */
|
||||
setobj2s(L, L->top++, p1); /* 1st argument */
|
||||
setobj2s(L, L->top++, p2); /* 2nd argument */
|
||||
if (!hasres) /* no result? 'p3' is third argument */
|
||||
@@ -115,15 +117,18 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
switch (event) {
|
||||
case TM_CONCAT:
|
||||
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: {
|
||||
lua_Number dummy;
|
||||
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
|
||||
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:
|
||||
luaG_aritherror(L, p1, p2);
|
||||
luaG_opinterror(L, p1, p2, "perform arithmetic on");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
4
ltm.h
4
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.20 2014/06/10 18:53:18 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -21,7 +21,7 @@ typedef enum {
|
||||
TM_GC,
|
||||
TM_MODE,
|
||||
TM_LEN,
|
||||
TM_EQ, /* last tag method with `fast' access */
|
||||
TM_EQ, /* last tag method with fast access */
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
|
||||
108
lua.c
108
lua.c
@@ -1,17 +1,19 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.212 2014/06/26 17:08:52 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.224 2015/03/10 14:15:06 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#define lua_c
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lua_c
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
@@ -50,14 +52,14 @@
|
||||
#include <unistd.h>
|
||||
#define lua_stdin_is_tty() isatty(0)
|
||||
|
||||
#elif defined(LUA_WIN) /* }{ */
|
||||
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||
|
||||
#include <io.h>
|
||||
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
/* ANSI definition */
|
||||
/* ISO C definition */
|
||||
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
||||
|
||||
#endif /* } */
|
||||
@@ -78,9 +80,7 @@
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
||||
#define lua_saveline(L,idx) \
|
||||
if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \
|
||||
add_history(lua_tostring(L, idx)); /* add it to history */
|
||||
#define lua_saveline(L,line) ((void)L, add_history(line))
|
||||
#define lua_freeline(L,b) ((void)L, free(b))
|
||||
|
||||
#else /* }{ */
|
||||
@@ -88,7 +88,7 @@
|
||||
#define lua_readline(L,b,p) \
|
||||
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
||||
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
||||
#define lua_saveline(L,idx) { (void)L; (void)idx; }
|
||||
#define lua_saveline(L,line) { (void)L; (void)line; }
|
||||
#define lua_freeline(L,b) { (void)L; (void)b; }
|
||||
|
||||
#endif /* } */
|
||||
@@ -126,17 +126,17 @@ static void laction (int i) {
|
||||
|
||||
|
||||
static void print_usage (const char *badoption) {
|
||||
luai_writestringerror("%s: ", progname);
|
||||
lua_writestringerror("%s: ", progname);
|
||||
if (badoption[1] == 'e' || badoption[1] == 'l')
|
||||
luai_writestringerror("'%s' needs argument\n", badoption);
|
||||
lua_writestringerror("'%s' needs argument\n", badoption);
|
||||
else
|
||||
luai_writestringerror("unrecognized option '%s'\n", badoption);
|
||||
luai_writestringerror(
|
||||
lua_writestringerror("unrecognized option '%s'\n", badoption);
|
||||
lua_writestringerror(
|
||||
"usage: %s [options] [script [args]]\n"
|
||||
"Available options are:\n"
|
||||
" -e stat execute string " LUA_QL("stat") "\n"
|
||||
" -i enter interactive mode after executing " LUA_QL("script") "\n"
|
||||
" -l name require library " LUA_QL("name") "\n"
|
||||
" -e stat execute string 'stat'\n"
|
||||
" -i enter interactive mode after executing 'script'\n"
|
||||
" -l name require library 'name'\n"
|
||||
" -v show version information\n"
|
||||
" -E ignore environment variables\n"
|
||||
" -- stop handling options\n"
|
||||
@@ -151,41 +151,41 @@ static void print_usage (const char *badoption) {
|
||||
** (if present)
|
||||
*/
|
||||
static void l_message (const char *pname, const char *msg) {
|
||||
if (pname) luai_writestringerror("%s: ", pname);
|
||||
luai_writestringerror("%s\n", msg);
|
||||
if (pname) lua_writestringerror("%s: ", pname);
|
||||
lua_writestringerror("%s\n", msg);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Check whether 'status' is not OK and, if so, prints the error
|
||||
** message on the top of the stack. Because this function can be called
|
||||
** unprotected, it only accepts actual strings as error messages. (A
|
||||
** coercion could raise a memory error.)
|
||||
** message on the top of the stack. It assumes that the error object
|
||||
** is a string, as it was either generated by Lua or by 'msghandler'.
|
||||
*/
|
||||
static int report (lua_State *L, int status) {
|
||||
if (status != LUA_OK) {
|
||||
const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1)
|
||||
: NULL;
|
||||
if (msg == NULL) msg = "(error object is not a string)";
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
l_message(progname, msg);
|
||||
lua_pop(L, 1);
|
||||
lua_pop(L, 1); /* remove message */
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Message handler to be used to run all chunks
|
||||
** Message handler used to run all chunks
|
||||
*/
|
||||
static int msghandler (lua_State *L) {
|
||||
const char *msg = lua_tostring(L, 1);
|
||||
if (msg) /* is error object a string? */
|
||||
luaL_traceback(L, L, msg, 1); /* use standard traceback */
|
||||
else if (!lua_isnoneornil(L, 1)) { /* non-string error object? */
|
||||
if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */
|
||||
lua_pushliteral(L, "(no error message)");
|
||||
} /* else no error object, does nothing */
|
||||
return 1;
|
||||
if (msg == NULL) { /* is error object not a string? */
|
||||
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
|
||||
lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
|
||||
return 1; /* that is the message */
|
||||
else
|
||||
msg = lua_pushfstring(L, "(error object is a %s value)",
|
||||
luaL_typename(L, 1));
|
||||
}
|
||||
luaL_traceback(L, L, msg, 1); /* append a standard traceback */
|
||||
return 1; /* return the traceback */
|
||||
}
|
||||
|
||||
|
||||
@@ -208,8 +208,8 @@ static int docall (lua_State *L, int narg, int nres) {
|
||||
|
||||
|
||||
static void print_version (void) {
|
||||
luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
||||
luai_writeline();
|
||||
lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
||||
lua_writeline();
|
||||
}
|
||||
|
||||
|
||||
@@ -313,11 +313,11 @@ static int pushline (lua_State *L, int firstline) {
|
||||
lua_pop(L, 1); /* remove prompt */
|
||||
l = strlen(b);
|
||||
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, ... */
|
||||
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
||||
else
|
||||
lua_pushstring(L, b);
|
||||
lua_pushlstring(L, b, l);
|
||||
lua_freeline(L, b);
|
||||
return 1;
|
||||
}
|
||||
@@ -334,8 +334,12 @@ static int addreturn (lua_State *L) {
|
||||
lua_pushvalue(L, -2); /* duplicate line */
|
||||
lua_concat(L, 2); /* new line is "return ..." */
|
||||
line = lua_tolstring(L, -1, &len);
|
||||
if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK)
|
||||
if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) {
|
||||
lua_remove(L, -3); /* remove original line */
|
||||
line += sizeof("return")/sizeof(char); /* remove 'return' for history */
|
||||
if (line[0] != '\0') /* non empty? */
|
||||
lua_saveline(L, line); /* keep history */
|
||||
}
|
||||
else
|
||||
lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */
|
||||
return status;
|
||||
@@ -350,8 +354,10 @@ static int multiline (lua_State *L) {
|
||||
size_t len;
|
||||
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
||||
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
||||
if (!incomplete(L, status) || !pushline(L, 0))
|
||||
if (!incomplete(L, status) || !pushline(L, 0)) {
|
||||
lua_saveline(L, line); /* keep history */
|
||||
return status; /* cannot or should not try to add continuation line */
|
||||
}
|
||||
lua_pushliteral(L, "\n"); /* add newline... */
|
||||
lua_insert(L, -2); /* ...between the two lines */
|
||||
lua_concat(L, 3); /* join them */
|
||||
@@ -372,7 +378,6 @@ static int loadline (lua_State *L) {
|
||||
return -1; /* no input */
|
||||
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
||||
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_assert(lua_gettop(L) == 1);
|
||||
return status;
|
||||
@@ -389,9 +394,8 @@ static void l_print (lua_State *L) {
|
||||
lua_getglobal(L, "print");
|
||||
lua_insert(L, 1);
|
||||
if (lua_pcall(L, n, 0, 0) != LUA_OK)
|
||||
l_message(progname, lua_pushfstring(L,
|
||||
"error calling " LUA_QL("print") " (%s)",
|
||||
lua_tostring(L, -1)));
|
||||
l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
|
||||
lua_tostring(L, -1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -411,7 +415,7 @@ static void doREPL (lua_State *L) {
|
||||
else report(L, status);
|
||||
}
|
||||
lua_settop(L, 0); /* clear stack */
|
||||
luai_writeline();
|
||||
lua_writeline();
|
||||
progname = oldprogname;
|
||||
}
|
||||
|
||||
@@ -421,8 +425,7 @@ static void doREPL (lua_State *L) {
|
||||
*/
|
||||
static int pushargs (lua_State *L) {
|
||||
int i, n;
|
||||
lua_getglobal(L, "arg");
|
||||
if (!lua_istable(L, -1))
|
||||
if (lua_getglobal(L, "arg") != LUA_TTABLE)
|
||||
luaL_error(L, "'arg' is not a table");
|
||||
n = (int)luaL_len(L, -1);
|
||||
luaL_checkstack(L, n + 3, "too many arguments to script");
|
||||
@@ -482,14 +485,14 @@ static int collectargs (char **argv, int *first) {
|
||||
args |= has_E;
|
||||
break;
|
||||
case 'i':
|
||||
args |= has_i; /* goes through (-i implies -v) */
|
||||
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
|
||||
case 'v':
|
||||
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
||||
return has_error; /* invalid option */
|
||||
args |= has_v;
|
||||
break;
|
||||
case 'e':
|
||||
args |= has_e; /* go through */
|
||||
args |= has_e; /* FALLTHROUGH */
|
||||
case 'l': /* both options need an argument */
|
||||
if (argv[i][2] == '\0') { /* no concatenated argument? */
|
||||
i++; /* try next 'argv' */
|
||||
@@ -513,17 +516,16 @@ static int collectargs (char **argv, int *first) {
|
||||
static int runargs (lua_State *L, char **argv, int n) {
|
||||
int i;
|
||||
for (i = 1; i < n; i++) {
|
||||
int status;
|
||||
int option = argv[i][1];
|
||||
lua_assert(argv[i][0] == '-'); /* already checked */
|
||||
if (option == 'e' || option == 'l') {
|
||||
int status;
|
||||
const char *extra = argv[i] + 2; /* both options need an argument */
|
||||
if (*extra == '\0') extra = argv[++i];
|
||||
lua_assert(extra != NULL);
|
||||
if (option == 'e')
|
||||
status = dostring(L, extra, "=(command line)");
|
||||
else
|
||||
status = dolibrary(L, extra);
|
||||
status = (option == 'e')
|
||||
? dostring(L, extra, "=(command line)")
|
||||
: dolibrary(L, extra);
|
||||
if (status != LUA_OK) return 0;
|
||||
}
|
||||
}
|
||||
|
||||
59
lua.h
59
lua.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.311 2014/07/24 14:00:16 roberto Exp roberto $
|
||||
** $Id: lua.h,v 1.327 2015/03/10 14:17:51 roberto Exp roberto $
|
||||
** Lua - A Scripting Language
|
||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||
** See Copyright Notice at the end of this file
|
||||
@@ -19,25 +19,27 @@
|
||||
#define LUA_VERSION_MAJOR "5"
|
||||
#define LUA_VERSION_MINOR "3"
|
||||
#define LUA_VERSION_NUM 503
|
||||
#define LUA_VERSION_RELEASE "alpha"
|
||||
#define LUA_VERSION_RELEASE "1"
|
||||
|
||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2014 Lua.org, PUC-Rio"
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio"
|
||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
||||
|
||||
|
||||
/* mark for precompiled code ('<esc>Lua') */
|
||||
#define LUA_SIGNATURE "\033Lua"
|
||||
#define LUA_SIGNATURE "\x1bLua"
|
||||
|
||||
/* option for multiple returns in 'lua_pcall' and 'lua_call' */
|
||||
#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))
|
||||
|
||||
|
||||
@@ -94,7 +96,7 @@ typedef LUA_INTEGER lua_Integer;
|
||||
typedef LUA_UNSIGNED lua_Unsigned;
|
||||
|
||||
/* type for continuation-function contexts */
|
||||
typedef LUA_CTXT lua_Ctx;
|
||||
typedef LUA_KCONTEXT lua_KContext;
|
||||
|
||||
|
||||
/*
|
||||
@@ -105,7 +107,7 @@ typedef int (*lua_CFunction) (lua_State *L);
|
||||
/*
|
||||
** Type for continuation functions
|
||||
*/
|
||||
typedef int (*lua_KFunction) (lua_State *L, int status, lua_Ctx ctx);
|
||||
typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
|
||||
|
||||
|
||||
/*
|
||||
@@ -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_rotate) (lua_State *L, int idx, int n);
|
||||
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);
|
||||
|
||||
@@ -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_pushnumber) (lua_State *L, lua_Number n);
|
||||
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
||||
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t 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_pushvfstring) (lua_State *L, const char *fmt,
|
||||
va_list argp);
|
||||
@@ -236,9 +238,10 @@ LUA_API int (lua_pushthread) (lua_State *L);
|
||||
/*
|
||||
** 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_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_rawget) (lua_State *L, int idx);
|
||||
LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
|
||||
LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
|
||||
@@ -252,9 +255,10 @@ LUA_API int (lua_getuservalue) (lua_State *L, int idx);
|
||||
/*
|
||||
** 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_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_rawset) (lua_State *L, int idx);
|
||||
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);
|
||||
@@ -265,17 +269,16 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx);
|
||||
/*
|
||||
** 'load' and 'call' functions (load and run Lua code)
|
||||
*/
|
||||
LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, lua_Ctx ctx,
|
||||
lua_KFunction k);
|
||||
LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults,
|
||||
lua_KContext ctx, lua_KFunction k);
|
||||
#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
|
||||
|
||||
LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
|
||||
lua_Ctx ctx, lua_KFunction k);
|
||||
lua_KContext ctx, lua_KFunction k);
|
||||
#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,
|
||||
const char *chunkname,
|
||||
const char *mode);
|
||||
const char *chunkname, const char *mode);
|
||||
|
||||
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
||||
|
||||
@@ -283,12 +286,13 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
||||
/*
|
||||
** coroutine functions
|
||||
*/
|
||||
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_Ctx ctx,
|
||||
lua_KFunction k);
|
||||
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
|
||||
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)
|
||||
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);
|
||||
|
||||
|
||||
/*
|
||||
@@ -319,7 +323,7 @@ LUA_API int (lua_next) (lua_State *L, int idx);
|
||||
LUA_API void (lua_concat) (lua_State *L, int n);
|
||||
LUA_API void (lua_len) (lua_State *L, int idx);
|
||||
|
||||
LUA_API size_t (lua_strtonum) (lua_State *L, const char *s);
|
||||
LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
|
||||
|
||||
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
||||
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
||||
@@ -354,8 +358,7 @@ 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_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
|
||||
|
||||
#define lua_pushliteral(L, s) \
|
||||
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
|
||||
#define lua_pushliteral(L, s) lua_pushstring(L, "" s)
|
||||
|
||||
#define lua_pushglobaltable(L) \
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
|
||||
@@ -377,10 +380,10 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
||||
** compatibility macros for unsigned conversions
|
||||
** ===============================================================
|
||||
*/
|
||||
#if defined(LUA_COMPAT_APIUNSIGNED)
|
||||
#if defined(LUA_COMPAT_APIINTCASTS)
|
||||
|
||||
#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)
|
||||
|
||||
#endif
|
||||
@@ -457,7 +460,7 @@ struct lua_Debug {
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 1994-2014 Lua.org, PUC-Rio.
|
||||
* Copyright (C) 1994-2015 Lua.org, PUC-Rio.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
|
||||
628
luaconf.h
628
luaconf.h
@@ -1,107 +1,174 @@
|
||||
/*
|
||||
** $Id: luaconf.h,v 1.211 2014/07/24 14:00:16 roberto Exp roberto $
|
||||
** $Id: luaconf.h,v 1.250 2015/04/03 18:41:57 roberto Exp roberto $
|
||||
** Configuration file for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lconfig_h
|
||||
#define lconfig_h
|
||||
#ifndef luaconf_h
|
||||
#define luaconf_h
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
/*
|
||||
** ==================================================================
|
||||
** ===================================================================
|
||||
** Search for "@@" to find all configurable definitions.
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** ===================================================================
|
||||
@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines type for
|
||||
@@ Lua integers;
|
||||
@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines
|
||||
@@ type for Lua floats.
|
||||
**
|
||||
** 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 hardware).
|
||||
** {====================================================================
|
||||
** System Configuration: macros to adapt (if needed) Lua to some
|
||||
** particular platform, for instance compiling it with 32-bit numbers or
|
||||
** restricting it to C89.
|
||||
** =====================================================================
|
||||
*/
|
||||
#define LUA_INT_LONGLONG
|
||||
#define LUA_REAL_DOUBLE
|
||||
|
||||
/*
|
||||
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You
|
||||
** can also define LUA_32BITS in the make file, but changing here you
|
||||
** ensure that all software connected to Lua will be compiled with the
|
||||
** same configuration.
|
||||
*/
|
||||
/* #define LUA_32BITS */
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_ANSI controls the use of non-ansi features.
|
||||
** CHANGE it (define it) if you want Lua to avoid the use of any
|
||||
** non-ansi feature or library.
|
||||
@@ 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.
|
||||
*/
|
||||
#if !defined(LUA_ANSI) && defined(__STRICT_ANSI__)
|
||||
#define LUA_ANSI
|
||||
/* #define LUA_USE_C89 */
|
||||
|
||||
|
||||
/*
|
||||
** By default, Lua on Windows use (some) specific Windows features
|
||||
*/
|
||||
#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE)
|
||||
#define LUA_USE_WINDOWS /* enable goodies for regular Windows */
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE)
|
||||
#define LUA_WIN /* enable goodies for regular Windows platforms */
|
||||
#if defined(LUA_USE_WINDOWS)
|
||||
#define LUA_DL_DLL /* enable support for DLL */
|
||||
#define LUA_USE_C89 /* broadly, Windows is C89 */
|
||||
#endif
|
||||
|
||||
#if defined(LUA_WIN)
|
||||
#define LUA_DL_DLL
|
||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if defined(LUA_USE_LINUX)
|
||||
#define LUA_USE_C99
|
||||
#define LUA_USE_POSIX
|
||||
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
||||
#define LUA_USE_READLINE /* needs some extra libraries */
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(LUA_USE_MACOSX)
|
||||
#define LUA_USE_C99
|
||||
#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 */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_USE_C99 includes all functionality that depends on C 99.
|
||||
** CHANGE it (define it) if your system is compatible.
|
||||
@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for
|
||||
** C89 ('long' and 'double'); Windows always has '__int64', so it does
|
||||
** not need to use this case.
|
||||
*/
|
||||
#if defined(LUA_USE_C99)
|
||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
||||
#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS)
|
||||
#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
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_USE_POSIX includes all functionality listed as X/Open System
|
||||
@@ Interfaces Extension (XSI).
|
||||
** CHANGE it (define it) if your system is XSI compatible.
|
||||
@@ LUA_INT_TYPE defines the type for Lua integers.
|
||||
@@ LUA_FLOAT_TYPE defines the type for Lua floats.
|
||||
** 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
|
||||
|
||||
#if !defined(LUA_FLOAT_TYPE)
|
||||
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||
#endif /* } */
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** Configuration for Paths.
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ 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
|
||||
@@ C libraries.
|
||||
** C libraries.
|
||||
** CHANGE them if your machine has a non-conventional directory
|
||||
** hierarchy or if you want to install your libraries in
|
||||
** non-conventional directories.
|
||||
*/
|
||||
#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
|
||||
** path of the directory of the executable file of the current process.
|
||||
@@ -112,7 +179,7 @@
|
||||
#define LUA_PATH_DEFAULT \
|
||||
LUA_LDIR"?.lua;" LUA_LDIR"?\\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"
|
||||
#define LUA_CPATH_DEFAULT \
|
||||
LUA_CDIR"?.dll;" \
|
||||
@@ -144,14 +211,14 @@
|
||||
#define LUA_DIRSEP "/"
|
||||
#endif
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_ENV is the name of the variable that holds the current
|
||||
@@ environment, used to access global names.
|
||||
** CHANGE it if you do not like this name.
|
||||
** {==================================================================
|
||||
** Marks for exported symbols in the C code
|
||||
** ===================================================================
|
||||
*/
|
||||
#define LUA_ENV "_ENV"
|
||||
|
||||
|
||||
/*
|
||||
@@ LUA_API is a mark for all core API functions.
|
||||
@@ -184,10 +251,10 @@
|
||||
|
||||
/*
|
||||
@@ 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
|
||||
@@ that are not to be exported to outside modules (LUAI_DDEF for
|
||||
@@ definitions and LUAI_DDEC for declarations).
|
||||
** that are not to be exported to outside modules (LUAI_DDEF for
|
||||
** definitions and LUAI_DDEC for declarations).
|
||||
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
||||
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
||||
** when Lua is compiled as a shared library. Not all elf targets support
|
||||
@@ -199,84 +266,14 @@
|
||||
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||
defined(__ELF__) /* { */
|
||||
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
||||
#else /* }{ */
|
||||
#define LUAI_FUNC extern
|
||||
#endif /* } */
|
||||
|
||||
#define LUAI_DDEC LUAI_FUNC
|
||||
#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.
|
||||
** CHANGE it if you want a different appearance.
|
||||
*/
|
||||
#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. (The #if
|
||||
** avoids including 'stdio.h' everywhere.)
|
||||
*/
|
||||
#if defined(LUA_LIB) || defined(lua_c)
|
||||
#include <stdio.h>
|
||||
#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
||||
#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout))
|
||||
#endif
|
||||
|
||||
/*
|
||||
@@ 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_CTXT 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_CTXT intptr_t
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(LUA_CTXT)
|
||||
/* default definition (the nearest thing to 'intptr_t' in C89) */
|
||||
#define LUA_CTXT ptrdiff_t
|
||||
#endif
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
@@ -305,30 +302,26 @@
|
||||
#define LUA_COMPAT_BITLIB
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_IPAIRS controls the effectivness of the __ipairs metamethod.
|
||||
@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod.
|
||||
*/
|
||||
#define LUA_COMPAT_IPAIRS
|
||||
|
||||
/*
|
||||
@@ LUA_COMPAT_APIUNSIGNED controls the presence of macros for
|
||||
** manipulating unsigned integers (lua_pushunsigned, lua_tounsigned, etc.)
|
||||
@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for
|
||||
** manipulating other integer types (lua_pushunsigned, lua_tounsigned,
|
||||
** luaL_checkint, luaL_checklong, etc.)
|
||||
*/
|
||||
#define LUA_COMPAT_APIUNSIGNED
|
||||
|
||||
|
||||
/*
|
||||
@@ 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 */
|
||||
#define LUA_COMPAT_APIINTCASTS
|
||||
|
||||
#endif /* } */
|
||||
|
||||
|
||||
#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'.
|
||||
** You can replace it with 'table.unpack'.
|
||||
@@ -388,100 +381,46 @@
|
||||
|
||||
#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.
|
||||
** Change these definitions if no predefined LUA_REAL_* / LUA_INT_*
|
||||
** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_*
|
||||
** satisfy your needs.
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ LUA_NUMBER is the floating-point type used by Lua.
|
||||
**
|
||||
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
|
||||
@@ 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_FMT is the format for writing floats.
|
||||
@@ 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.
|
||||
@@ lua_str2number converts a decimal numeric string to a number.
|
||||
*/
|
||||
|
||||
#if defined(LUA_REAL_FLOAT) /* { single float */
|
||||
#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */
|
||||
|
||||
#define LUA_NUMBER float
|
||||
|
||||
#define l_mathlim(n) (FLT_##n)
|
||||
|
||||
#define LUAI_UACNUMBER double
|
||||
|
||||
#define LUA_NUMBER_FRMLEN ""
|
||||
@@ -492,10 +431,12 @@
|
||||
#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 l_mathlim(n) (LDBL_##n)
|
||||
|
||||
#define LUAI_UACNUMBER long double
|
||||
|
||||
#define LUA_NUMBER_FRMLEN "L"
|
||||
@@ -505,10 +446,12 @@
|
||||
|
||||
#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 l_mathlim(n) (DBL_##n)
|
||||
|
||||
#define LUAI_UACNUMBER double
|
||||
|
||||
#define LUA_NUMBER_FRMLEN ""
|
||||
@@ -518,80 +461,32 @@
|
||||
|
||||
#define lua_str2number(s,p) strtod((s), (p))
|
||||
|
||||
#else /* }{ */
|
||||
#else /* }{ */
|
||||
|
||||
#error "numeric real type not defined"
|
||||
#error "numeric float type not defined"
|
||||
|
||||
#endif /* } */
|
||||
|
||||
|
||||
#if defined(LUA_ANSI)
|
||||
/* C89 does not support 'opf' variants for math functions */
|
||||
#undef l_mathop
|
||||
#define l_mathop(op) (lua_Number)op
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(LUA_ANSI) || defined(_WIN32)
|
||||
/* C89 and Windows do not support 'strtof'... */
|
||||
#undef lua_str2number
|
||||
#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
|
||||
@@ 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_numtointeger(n,p) \
|
||||
#define lua_numbertointeger(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.
|
||||
@@ -624,7 +519,7 @@
|
||||
|
||||
/* 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_FRMLEN ""
|
||||
@@ -632,7 +527,7 @@
|
||||
#define LUA_MAXINTEGER INT_MAX
|
||||
#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_FRMLEN "l"
|
||||
@@ -640,17 +535,10 @@
|
||||
#define LUA_MAXINTEGER LONG_MAX
|
||||
#define LUA_MININTEGER LONG_MIN
|
||||
|
||||
#elif defined(LUA_INT_LONGLONG) /* }{ long long */
|
||||
#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#define LUA_INTEGER __int64
|
||||
#define LUA_INTEGER_FRMLEN "I64"
|
||||
|
||||
#define LUA_MAXINTEGER _I64_MAX
|
||||
#define LUA_MININTEGER _I64_MIN
|
||||
|
||||
#else
|
||||
#if defined(LLONG_MAX) /* { */
|
||||
/* use ISO C99 stuff */
|
||||
|
||||
#define LUA_INTEGER long long
|
||||
#define LUA_INTEGER_FRMLEN "ll"
|
||||
@@ -658,27 +546,21 @@
|
||||
#define LUA_MAXINTEGER LLONG_MAX
|
||||
#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 */
|
||||
/*
|
||||
** this option is for tests only; it is not particularly useful and
|
||||
** it does not pass the test suit.
|
||||
*/
|
||||
#define LUA_INTEGER __int64
|
||||
#define LUA_INTEGER_FRMLEN "I64"
|
||||
|
||||
#define LUA_INTEGER short int
|
||||
#define LUA_INTEGER_FRMLEN ""
|
||||
#define LUA_MAXINTEGER _I64_MAX
|
||||
#define LUA_MININTEGER _I64_MIN
|
||||
|
||||
#define LUA_MAXINTEGER SHRT_MAX
|
||||
#define LUA_MININTEGER SHRT_MIN
|
||||
#else /* }{ */
|
||||
|
||||
#undef LUAI_UACINT
|
||||
#define LUAI_UACINT int
|
||||
#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \
|
||||
or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)"
|
||||
|
||||
#undef LUAI_MAXSTACK
|
||||
#define LUAI_MAXSTACK 15000
|
||||
|
||||
#define l_castS2U(x) ((LUA_UNSIGNED)(unsigned short)(x))
|
||||
#endif /* } */
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
@@ -689,6 +571,168 @@
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** Dependencies with C99 and other C details
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ 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,f,n) sprintf(b,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")
|
||||
|
||||
|
||||
|
||||
|
||||
/* =================================================================== */
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 2.39 2014/06/18 18:35:43 roberto Exp roberto $
|
||||
** $Id: lundump.c,v 2.40 2014/06/19 18:27:20 roberto Exp roberto $
|
||||
** load precompiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lundump_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "ldebug.h"
|
||||
|
||||
35
lutf8lib.c
35
lutf8lib.c
@@ -1,17 +1,20 @@
|
||||
/*
|
||||
** $Id: lutf8lib.c,v 1.9 2014/05/14 18:33:37 roberto Exp roberto $
|
||||
** $Id: lutf8lib.c,v 1.14 2015/03/05 16:07:46 roberto Exp roberto $
|
||||
** Standard library for UTF-8 manipulation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define lutf8lib_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.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.
|
||||
*/
|
||||
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;
|
||||
unsigned int c = s[0];
|
||||
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, pose <= (lua_Integer)len, 3, "out of range");
|
||||
if (posi > pose) return 0; /* empty interval; return no values */
|
||||
n = (int)(pose - posi + 1);
|
||||
if (posi + n <= pose) /* (lua_Integer -> int) overflow? */
|
||||
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
||||
return luaL_error(L, "string slice too long");
|
||||
n = (int)(pose - posi) + 1;
|
||||
luaL_checkstack(L, n, "string slice too long");
|
||||
n = 0;
|
||||
se = s + pose;
|
||||
@@ -123,9 +126,9 @@ static int codepoint (lua_State *L) {
|
||||
|
||||
|
||||
static void pushutfchar (lua_State *L, int arg) {
|
||||
int code = luaL_checkint(L, arg);
|
||||
lua_Integer code = luaL_checkinteger(L, arg);
|
||||
luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range");
|
||||
lua_pushfstring(L, "%U", code);
|
||||
lua_pushfstring(L, "%U", (long)code);
|
||||
}
|
||||
|
||||
|
||||
@@ -157,7 +160,7 @@ static int utfchar (lua_State *L) {
|
||||
static int byteoffset (lua_State *L) {
|
||||
size_t len;
|
||||
const char *s = luaL_checklstring(L, 1, &len);
|
||||
int n = luaL_checkint(L, 2);
|
||||
lua_Integer n = luaL_checkinteger(L, 2);
|
||||
lua_Integer posi = (n >= 0) ? 1 : len + 1;
|
||||
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
||||
@@ -232,22 +235,22 @@ static int iter_codes (lua_State *L) {
|
||||
#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
|
||||
|
||||
|
||||
static struct luaL_Reg funcs[] = {
|
||||
static const luaL_Reg funcs[] = {
|
||||
{"offset", byteoffset},
|
||||
{"codepoint", codepoint},
|
||||
{"char", utfchar},
|
||||
{"len", utflen},
|
||||
{"codes", iter_codes},
|
||||
/* placeholders */
|
||||
{"charpatt", NULL},
|
||||
{"charpattern", NULL},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_utf8 (lua_State *L) {
|
||||
luaL_newlib(L, funcs);
|
||||
lua_pushliteral(L, UTF8PATT);
|
||||
lua_setfield(L, -2, "charpatt");
|
||||
lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
|
||||
lua_setfield(L, -2, "charpattern");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
20
lvm.h
20
lvm.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 2.32 2014/07/30 14:00:14 roberto Exp roberto $
|
||||
** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -16,14 +16,24 @@
|
||||
#if !defined(LUA_NOCVTN2S)
|
||||
#define cvt2str(o) ttisnumber(o)
|
||||
#else
|
||||
#define cvt2str(o) 0 /* no convertion from numbers to strings */
|
||||
#define cvt2str(o) 0 /* no conversion from numbers to strings */
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(LUA_NOCVTS2N)
|
||||
#define cvt2num(o) ttisstring(o)
|
||||
#else
|
||||
#define cvt2num(o) 0 /* no convertion from strings to numbers */
|
||||
#define cvt2num(o) 0 /* no conversion from strings to numbers */
|
||||
#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
|
||||
|
||||
|
||||
@@ -31,7 +41,7 @@
|
||||
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
||||
|
||||
#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))
|
||||
|
||||
@@ -42,7 +52,7 @@ 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_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_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,
|
||||
StkId val);
|
||||
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
|
||||
|
||||
10
lzio.c
10
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.35 2012/05/14 13:34:18 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define lzio_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
#include "llimits.h"
|
||||
|
||||
5
lzio.h
5
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.29 2014/12/19 13:45:40 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -37,7 +37,8 @@ typedef struct Mbuffer {
|
||||
|
||||
|
||||
#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)
|
||||
|
||||
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
|
||||
|
||||
151
makefile
151
makefile
@@ -5,19 +5,19 @@
|
||||
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
||||
|
||||
# Warnings valid for both C and C++
|
||||
CWARNSCPP=-pedantic -Wno-long-long \
|
||||
-Wall -Wextra \
|
||||
CWARNSCPP= \
|
||||
-pedantic \
|
||||
-Wextra \
|
||||
-Waggregate-return \
|
||||
-Wcast-align \
|
||||
-Wdisabled-optimization \
|
||||
-Wshadow \
|
||||
-Wsign-compare \
|
||||
-Wsign-compare \
|
||||
-Wundef \
|
||||
-Wwrite-strings \
|
||||
-Wredundant-decls \
|
||||
-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
|
||||
# -Wconversion -Wno-sign-conversion \
|
||||
# -Wsign-conversion \
|
||||
# -Wconversion \
|
||||
# -Wlogical-op \
|
||||
@@ -27,21 +27,21 @@ CWARNSCPP=-pedantic -Wno-long-long \
|
||||
|
||||
# The next warnings are neither valid nor needed for C++
|
||||
CWARNSC= -Wdeclaration-after-statement \
|
||||
-Wmissing-prototypes \
|
||||
-Wnested-externs \
|
||||
-Wmissing-prototypes \
|
||||
-Wnested-externs \
|
||||
-Wstrict-prototypes \
|
||||
-Wc++-compat \
|
||||
-Wold-style-declaration \
|
||||
-Wold-style-definition \
|
||||
-Wold-style-declaration \
|
||||
|
||||
CWARNS= $(CWARNSCPP) $(CWARNSC)
|
||||
|
||||
|
||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
||||
# -g -DLUA_USER_H='"ltests.h"'
|
||||
# -fomit-frame-pointer #-pg -malign-double
|
||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||
TESTS= -DLUA_USER_H='"ltests.h"'
|
||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||
# TESTS= -DLUA_USER_H='"ltests.h"'
|
||||
|
||||
# -mtune=native -fomit-frame-pointer
|
||||
LOCAL = $(TESTS) $(CWARNS) -g
|
||||
@@ -49,7 +49,7 @@ LOCAL = $(TESTS) $(CWARNS) -g
|
||||
|
||||
|
||||
# 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
|
||||
MYLIBS= -ldl -lreadline -lhistory -lncurses
|
||||
|
||||
@@ -126,66 +126,71 @@ $(ALL_O): makefile
|
||||
# DO NOT EDIT
|
||||
# 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 \
|
||||
lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \
|
||||
lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \
|
||||
ltable.h lundump.h lvm.h
|
||||
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h
|
||||
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
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 llimits.h lstate.h \
|
||||
lobject.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 llimits.h lstate.h \
|
||||
lobject.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
|
||||
lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h
|
||||
lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lcode.o: lcode.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 lgc.h \
|
||||
lstring.h ltable.h lvm.h
|
||||
lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h
|
||||
ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
ldebug.o: ldebug.c 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 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 lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \
|
||||
lzio.h lmem.h lundump.h
|
||||
lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \
|
||||
lstate.h ltm.h lzio.h lmem.h
|
||||
lgc.o: lgc.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 lstring.h ltable.h
|
||||
linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h
|
||||
liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \
|
||||
lstate.h ltm.h lzio.h lmem.h lgc.h llex.h lparser.h lstring.h ltable.h
|
||||
lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
|
||||
lmem.o: lmem.c 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 lua.h luaconf.h lauxlib.h lualib.h
|
||||
lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h
|
||||
lopcodes.o: lopcodes.c 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
|
||||
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h
|
||||
loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||
ldo.h lfunc.h lstring.h lgc.h ltable.h
|
||||
lstate.o: lstate.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 llex.h \
|
||||
lstring.h ltable.h
|
||||
lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h llimits.h \
|
||||
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
||||
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \
|
||||
lparser.h lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h \
|
||||
lualib.h
|
||||
ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h llimits.h lstate.h \
|
||||
lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h
|
||||
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h llimits.h \
|
||||
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
|
||||
lundump.h
|
||||
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||
lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h llimits.h lstate.h \
|
||||
lobject.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 lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
|
||||
lobject.h ltm.h lzio.h
|
||||
|
||||
# (end of Makefile)
|
||||
|
||||
Reference in New Issue
Block a user