mirror of
https://github.com/lua/lua.git
synced 2026-08-10 15:19:35 +00:00
Compare commits
212 Commits
| 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 |
223
bugs
223
bugs
@@ -1878,13 +1878,6 @@ for i=1,25 do print(i); crash(i) end
|
|||||||
patch = [[
|
patch = [[
|
||||||
--- lundump.c 2008/04/04 16:00:45 2.7.1.3
|
--- lundump.c 2008/04/04 16:00:45 2.7.1.3
|
||||||
+++ lundump.c 2008/04/04 19:51:41 2.7.1.4
|
+++ lundump.c 2008/04/04 19:51:41 2.7.1.4
|
||||||
@@ -1,5 +1,5 @@
|
|
||||||
/*
|
|
||||||
-** $Id: bugs,v 1.132 2014/05/07 16:57:27 roberto Exp roberto $
|
|
||||||
+** $Id: bugs,v 1.132 2014/05/07 16:57:27 roberto Exp roberto $
|
|
||||||
** load precompiled Lua chunks
|
|
||||||
** See Copyright Notice in lua.h
|
|
||||||
*/
|
|
||||||
@@ -161,7 +161,9 @@
|
@@ -161,7 +161,9 @@
|
||||||
|
|
||||||
static Proto* LoadFunction(LoadState* S, TString* p)
|
static Proto* LoadFunction(LoadState* S, TString* p)
|
||||||
@@ -3220,6 +3213,26 @@ example = [[
|
|||||||
(segfaults on some platforms with some compiler options)
|
(segfaults on some platforms with some compiler options)
|
||||||
]],
|
]],
|
||||||
patch = [[
|
patch = [[
|
||||||
|
--- ltablib.c 2013/04/12 18:48:47 1.65.1.1
|
||||||
|
+++ ltablib.c 2014/05/07 16:32:55 1.65.1.2
|
||||||
|
@@ -134,13 +135,14 @@
|
||||||
|
|
||||||
|
|
||||||
|
static int unpack (lua_State *L) {
|
||||||
|
- int i, e, n;
|
||||||
|
+ int i, e;
|
||||||
|
+ unsigned int n;
|
||||||
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
i = luaL_optint(L, 2, 1);
|
||||||
|
e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
|
||||||
|
if (i > e) return 0; /* empty range */
|
||||||
|
- n = e - i + 1; /* number of elements */
|
||||||
|
- if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
|
||||||
|
+ n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */
|
||||||
|
+ if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n))
|
||||||
|
return luaL_error(L, "too many results to unpack");
|
||||||
|
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
|
||||||
|
while (i++ < e) /* push arg[i + 1...e] */
|
||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3248,6 +3261,200 @@ patch = [[
|
|||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[Chunk with too many lines can seg. fault]],
|
||||||
|
report = [[Roberto, 2014/11/14]],
|
||||||
|
since = [[5.1 (at least)]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
-- the cause of the bug is the use of an unitialized variable, so
|
||||||
|
-- it cannot be reproduced reliably
|
||||||
|
local s = string.rep("\n", 2^24)
|
||||||
|
print(load(function () return s end))
|
||||||
|
]],
|
||||||
|
patch = [[
|
||||||
|
--- llex.c 2013/08/30 15:49:41 2.63.1.2
|
||||||
|
+++ llex.c 2015/02/09 17:05:31
|
||||||
|
@@ -153,5 +153,5 @@
|
||||||
|
next(ls); /* skip `\n\r' or `\r\n' */
|
||||||
|
if (++ls->linenumber >= MAX_INT)
|
||||||
|
- luaX_syntaxerror(ls, "chunk has too many lines");
|
||||||
|
+ lexerror(ls, "chunk has too many lines", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------
|
||||||
|
-- Lua 5.3.0
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [['string.format("%f")' can cause a buffer overflow
|
||||||
|
(only when 'lua_Number' is long double!)]],
|
||||||
|
report = [[Roberto, 2015/01/13]],
|
||||||
|
since = [[5.3]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[string.format("%.99f", 1e4000) -- when floats are long double]],
|
||||||
|
patch = [[
|
||||||
|
--- lstrlib.c 2014/12/11 14:03:07 1.221
|
||||||
|
+++ lstrlib.c 2015/02/23 19:01:42
|
||||||
|
@@ -800,3 +800,4 @@
|
||||||
|
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
||||||
|
-#define MAX_ITEM 512
|
||||||
|
+#define MAX_ITEM \
|
||||||
|
+ (sizeof(lua_Number) <= 4 ? 150 : sizeof(lua_Number) <= 8 ? 450 : 5050)
|
||||||
|
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [['debug.getlocal' on a coroutine suspended in a hook
|
||||||
|
can crash the interpreter]],
|
||||||
|
report = [[云风, 2015/02/11]],
|
||||||
|
since = [[5.2]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[see http://lua-users.org/lists/lua-l/2015-02/msg00146.html]],
|
||||||
|
patch = [[
|
||||||
|
--- ldebug.c 2015/01/02 12:52:22 2.110
|
||||||
|
+++ ldebug.c 2015/02/13 16:03:23
|
||||||
|
@@ -49,4 +49,14 @@
|
||||||
|
|
||||||
|
|
||||||
|
+static void swapextra (lua_State *L) {
|
||||||
|
+ if (L->status == LUA_YIELD) {
|
||||||
|
+ CallInfo *ci = L->ci; /* get function that yielded */
|
||||||
|
+ StkId temp = ci->func; /* exchange its 'func' and 'extra' values */
|
||||||
|
+ ci->func = restorestack(L, ci->extra);
|
||||||
|
+ ci->extra = savestack(L, temp);
|
||||||
|
+ }
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+
|
||||||
|
/*
|
||||||
|
** this function can be called asynchronous (e.g. during a signal)
|
||||||
|
@@ -145,4 +155,5 @@
|
||||||
|
const char *name;
|
||||||
|
lua_lock(L);
|
||||||
|
+ swapextra(L);
|
||||||
|
if (ar == NULL) { /* information about non-active function? */
|
||||||
|
if (!isLfunction(L->top - 1)) /* not a Lua function? */
|
||||||
|
@@ -159,4 +170,5 @@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
+ swapextra(L);
|
||||||
|
lua_unlock(L);
|
||||||
|
return name;
|
||||||
|
@@ -166,10 +178,13 @@
|
||||||
|
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||||
|
StkId pos = 0; /* to avoid warnings */
|
||||||
|
- const char *name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
|
+ const char *name;
|
||||||
|
lua_lock(L);
|
||||||
|
+ swapextra(L);
|
||||||
|
+ name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
|
if (name) {
|
||||||
|
setobjs2s(L, pos, L->top - 1);
|
||||||
|
L->top--; /* pop value */
|
||||||
|
}
|
||||||
|
+ swapextra(L);
|
||||||
|
lua_unlock(L);
|
||||||
|
return name;
|
||||||
|
@@ -271,4 +286,5 @@
|
||||||
|
StkId func;
|
||||||
|
lua_lock(L);
|
||||||
|
+ swapextra(L);
|
||||||
|
if (*what == '>') {
|
||||||
|
ci = NULL;
|
||||||
|
@@ -289,4 +305,5 @@
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
+ swapextra(L);
|
||||||
|
if (strchr(what, 'L'))
|
||||||
|
collectvalidlines(L, cl);
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[suspended '__le' metamethod can give wrong result]],
|
||||||
|
report = [[Eric Zhong, 2015/04/07]],
|
||||||
|
since = [[5.2]],
|
||||||
|
fix = nil,
|
||||||
|
|
||||||
|
example = [[
|
||||||
|
mt = {__le = function (a,b) coroutine.yield("yield"); return a.x <= b.x end}
|
||||||
|
t1 = setmetatable({x=1}, mt)
|
||||||
|
t2 = {x=2}
|
||||||
|
co = coroutine.wrap(function (a,b) return t2 <= t1 end)
|
||||||
|
co()
|
||||||
|
print(co()) --> true (should be false)
|
||||||
|
]],
|
||||||
|
|
||||||
|
patch = [[
|
||||||
|
--- lstate.h 2014/10/30 18:53:28 2.119
|
||||||
|
+++ lstate.h 2015/04/13 15:58:40
|
||||||
|
@@ -94,6 +94,7 @@
|
||||||
|
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
||||||
|
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||||
|
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||||
|
+#define CIST_LEQ (1<<7) /* using __lt for __le */
|
||||||
|
|
||||||
|
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
||||||
|
|
||||||
|
--- lvm.c 2014/12/27 20:30:38 2.232
|
||||||
|
+++ lvm.c 2015/04/13 15:51:30
|
||||||
|
@@ -292,9 +292,14 @@
|
||||||
|
return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
|
||||||
|
else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try 'le' */
|
||||||
|
return res;
|
||||||
|
- else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try 'lt' */
|
||||||
|
- luaG_ordererror(L, l, r);
|
||||||
|
- return !res;
|
||||||
|
+ else { /* try 'lt': */
|
||||||
|
+ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */
|
||||||
|
+ res = luaT_callorderTM(L, r, l, TM_LT);
|
||||||
|
+ L->ci->callstatus ^= CIST_LEQ; /* clear mark */
|
||||||
|
+ if (res < 0)
|
||||||
|
+ luaG_ordererror(L, l, r);
|
||||||
|
+ return !res; /* result is negated */
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -553,11 +558,11 @@
|
||||||
|
case OP_LE: case OP_LT: case OP_EQ: {
|
||||||
|
int res = !l_isfalse(L->top - 1);
|
||||||
|
L->top--;
|
||||||
|
- /* metamethod should not be called when operand is K */
|
||||||
|
- lua_assert(!ISK(GETARG_B(inst)));
|
||||||
|
- if (op == OP_LE && /* "<=" using "<" instead? */
|
||||||
|
- ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
|
||||||
|
- res = !res; /* invert result */
|
||||||
|
+ if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
|
||||||
|
+ lua_assert(op == OP_LE);
|
||||||
|
+ ci->callstatus ^= CIST_LEQ; /* clear mark */
|
||||||
|
+ res = !res; /* negate result */
|
||||||
|
+ }
|
||||||
|
lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
|
||||||
|
if (res != GETARG_A(inst)) /* condition failed? */
|
||||||
|
ci->u.l.savedpc++; /* skip jump instruction */
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Bug{
|
||||||
|
what = [[return hook may not see correct values for
|
||||||
|
active local variables when function returns]],
|
||||||
|
report = [[Philipp Janda/Peng Yi, 2015/05/19]],
|
||||||
|
since = [[5.0]],
|
||||||
|
fix = nil,
|
||||||
|
example = [[
|
||||||
|
see messasge http://lua-users.org/lists/lua-l/2015-05/msg00376.html]],
|
||||||
|
patch = [[
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
--[=[
|
--[=[
|
||||||
Bug{
|
Bug{
|
||||||
@@ -3260,3 +3467,5 @@ patch = [[
|
|||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
]=]
|
]=]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
162
lapi.c
162
lapi.c
@@ -1,17 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lapi.c,v 2.237 2014/10/15 14:27:40 roberto Exp roberto $
|
** $Id: lapi.c,v 2.248 2015/03/28 19:14:47 roberto Exp roberto $
|
||||||
** Lua API
|
** Lua API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lapi_c
|
#define lapi_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -50,29 +51,29 @@ const char lua_ident[] =
|
|||||||
/* test for valid but not pseudo index */
|
/* test for valid but not pseudo index */
|
||||||
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
|
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
|
||||||
|
|
||||||
#define api_checkvalidindex(o) api_check(isvalid(o), "invalid index")
|
#define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index")
|
||||||
|
|
||||||
#define api_checkstackindex(i, o) \
|
#define api_checkstackindex(l, i, o) \
|
||||||
api_check(isstackindex(i, o), "index not in the stack")
|
api_check(l, isstackindex(i, o), "index not in the stack")
|
||||||
|
|
||||||
|
|
||||||
static TValue *index2addr (lua_State *L, int idx) {
|
static TValue *index2addr (lua_State *L, int idx) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
if (idx > 0) {
|
if (idx > 0) {
|
||||||
TValue *o = ci->func + idx;
|
TValue *o = ci->func + idx;
|
||||||
api_check(idx <= ci->top - (ci->func + 1), "unacceptable index");
|
api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
|
||||||
if (o >= L->top) return NONVALIDVALUE;
|
if (o >= L->top) return NONVALIDVALUE;
|
||||||
else return o;
|
else return o;
|
||||||
}
|
}
|
||||||
else if (!ispseudo(idx)) { /* negative index */
|
else if (!ispseudo(idx)) { /* negative index */
|
||||||
api_check(idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
||||||
return L->top + idx;
|
return L->top + idx;
|
||||||
}
|
}
|
||||||
else if (idx == LUA_REGISTRYINDEX)
|
else if (idx == LUA_REGISTRYINDEX)
|
||||||
return &G(L)->l_registry;
|
return &G(L)->l_registry;
|
||||||
else { /* upvalues */
|
else { /* upvalues */
|
||||||
idx = LUA_REGISTRYINDEX - idx;
|
idx = LUA_REGISTRYINDEX - idx;
|
||||||
api_check(idx <= MAXUPVAL + 1, "upvalue index too large");
|
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
|
||||||
if (ttislcf(ci->func)) /* light C function? */
|
if (ttislcf(ci->func)) /* light C function? */
|
||||||
return NONVALIDVALUE; /* it has no upvalues */
|
return NONVALIDVALUE; /* it has no upvalues */
|
||||||
else {
|
else {
|
||||||
@@ -93,22 +94,22 @@ static void growstack (lua_State *L, void *ud) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_checkstack (lua_State *L, int size) {
|
LUA_API int lua_checkstack (lua_State *L, int n) {
|
||||||
int res;
|
int res;
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_check(size >= 0, "negative 'size'");
|
api_check(L, n >= 0, "negative 'n'");
|
||||||
if (L->stack_last - L->top > size) /* stack large enough? */
|
if (L->stack_last - L->top > n) /* stack large enough? */
|
||||||
res = 1; /* yes; check is OK */
|
res = 1; /* yes; check is OK */
|
||||||
else { /* no; need to grow stack */
|
else { /* no; need to grow stack */
|
||||||
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
|
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
|
||||||
if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */
|
if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
|
||||||
res = 0; /* no */
|
res = 0; /* no */
|
||||||
else /* try to grow stack */
|
else /* try to grow stack */
|
||||||
res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
|
res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK);
|
||||||
}
|
}
|
||||||
if (res && ci->top < L->top + size)
|
if (res && ci->top < L->top + n)
|
||||||
ci->top = L->top + size; /* adjust frame top */
|
ci->top = L->top + n; /* adjust frame top */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -119,11 +120,12 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
|
|||||||
if (from == to) return;
|
if (from == to) return;
|
||||||
lua_lock(to);
|
lua_lock(to);
|
||||||
api_checknelems(from, n);
|
api_checknelems(from, n);
|
||||||
api_check(G(from) == G(to), "moving among independent states");
|
api_check(from, G(from) == G(to), "moving among independent states");
|
||||||
api_check(to->ci->top - to->top >= n, "not enough elements to move");
|
api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
|
||||||
from->top -= n;
|
from->top -= n;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
setobj2s(to, to->top++, from->top + i);
|
setobj2s(to, to->top, from->top + i);
|
||||||
|
api_incr_top(to);
|
||||||
}
|
}
|
||||||
lua_unlock(to);
|
lua_unlock(to);
|
||||||
}
|
}
|
||||||
@@ -158,7 +160,7 @@ LUA_API const lua_Number *lua_version (lua_State *L) {
|
|||||||
LUA_API int lua_absindex (lua_State *L, int idx) {
|
LUA_API int lua_absindex (lua_State *L, int idx) {
|
||||||
return (idx > 0 || ispseudo(idx))
|
return (idx > 0 || ispseudo(idx))
|
||||||
? idx
|
? idx
|
||||||
: cast_int(L->top - L->ci->func + idx);
|
: cast_int(L->top - L->ci->func) + idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -171,14 +173,14 @@ LUA_API void lua_settop (lua_State *L, int idx) {
|
|||||||
StkId func = L->ci->func;
|
StkId func = L->ci->func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
api_check(idx <= L->stack_last - (func + 1), "new top too large");
|
api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
|
||||||
while (L->top < (func + 1) + idx)
|
while (L->top < (func + 1) + idx)
|
||||||
setnilvalue(L->top++);
|
setnilvalue(L->top++);
|
||||||
L->top = (func + 1) + idx;
|
L->top = (func + 1) + idx;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
||||||
L->top += idx+1; /* `subtract' index (index is negative) */
|
L->top += idx+1; /* 'subtract' index (index is negative) */
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
@@ -207,8 +209,8 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = L->top - 1; /* end of stack segment being rotated */
|
t = L->top - 1; /* end of stack segment being rotated */
|
||||||
p = index2addr(L, idx); /* start of segment */
|
p = index2addr(L, idx); /* start of segment */
|
||||||
api_checkstackindex(idx, p);
|
api_checkstackindex(L, idx, p);
|
||||||
api_check((n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
||||||
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
||||||
reverse(L, p, m); /* reverse the prefix with length 'n' */
|
reverse(L, p, m); /* reverse the prefix with length 'n' */
|
||||||
reverse(L, m + 1, t); /* reverse the suffix */
|
reverse(L, m + 1, t); /* reverse the suffix */
|
||||||
@@ -222,7 +224,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
fr = index2addr(L, fromidx);
|
fr = index2addr(L, fromidx);
|
||||||
to = index2addr(L, toidx);
|
to = index2addr(L, toidx);
|
||||||
api_checkvalidindex(to);
|
api_checkvalidindex(L, to);
|
||||||
setobj(L, to, fr);
|
setobj(L, to, fr);
|
||||||
if (isupvalue(toidx)) /* function upvalue? */
|
if (isupvalue(toidx)) /* function upvalue? */
|
||||||
luaC_barrier(L, clCvalue(L->ci->func), fr);
|
luaC_barrier(L, clCvalue(L->ci->func), fr);
|
||||||
@@ -254,7 +256,7 @@ LUA_API int lua_type (lua_State *L, int idx) {
|
|||||||
|
|
||||||
LUA_API const char *lua_typename (lua_State *L, int t) {
|
LUA_API const char *lua_typename (lua_State *L, int t) {
|
||||||
UNUSED(L);
|
UNUSED(L);
|
||||||
api_check(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
|
api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
|
||||||
return ttypename(t);
|
return ttypename(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +306,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
|
|||||||
else { /* for unary operations, add fake 2nd operand */
|
else { /* for unary operations, add fake 2nd operand */
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
setobjs2s(L, L->top, L->top - 1);
|
setobjs2s(L, L->top, L->top - 1);
|
||||||
L->top++;
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
/* first operand at top - 2, second at top - 1; result go to top - 2 */
|
/* first operand at top - 2, second at top - 1; result go to top - 2 */
|
||||||
luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
|
luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
|
||||||
@@ -324,7 +326,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
|||||||
case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
|
case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
|
||||||
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
|
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
|
||||||
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
|
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
|
||||||
default: api_check(0, "invalid option");
|
default: api_check(L, 0, "invalid option");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -375,21 +377,23 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
|||||||
if (len != NULL) *len = 0;
|
if (len != NULL) *len = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
lua_lock(L); /* `luaO_tostring' may create a new string */
|
lua_lock(L); /* 'luaO_tostring' may create a new string */
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
o = index2addr(L, idx); /* previous call may reallocate the stack */
|
o = index2addr(L, idx); /* previous call may reallocate the stack */
|
||||||
luaO_tostring(L, o);
|
luaO_tostring(L, o);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
}
|
}
|
||||||
if (len != NULL) *len = tsvalue(o)->len;
|
if (len != NULL)
|
||||||
|
*len = vslen(o);
|
||||||
return svalue(o);
|
return svalue(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API size_t lua_rawlen (lua_State *L, int idx) {
|
LUA_API size_t lua_rawlen (lua_State *L, int idx) {
|
||||||
StkId o = index2addr(L, idx);
|
StkId o = index2addr(L, idx);
|
||||||
switch (ttnov(o)) {
|
switch (ttype(o)) {
|
||||||
case LUA_TSTRING: return tsvalue(o)->len;
|
case LUA_TSHRSTR: return tsvalue(o)->shrlen;
|
||||||
|
case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
|
||||||
case LUA_TUSERDATA: return uvalue(o)->len;
|
case LUA_TUSERDATA: return uvalue(o)->len;
|
||||||
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
||||||
default: return 0;
|
default: return 0;
|
||||||
@@ -430,9 +434,8 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
|||||||
case LUA_TCCL: return clCvalue(o);
|
case LUA_TCCL: return clCvalue(o);
|
||||||
case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
|
case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
|
||||||
case LUA_TTHREAD: return thvalue(o);
|
case LUA_TTHREAD: return thvalue(o);
|
||||||
case LUA_TUSERDATA:
|
case LUA_TUSERDATA: return getudatamem(uvalue(o));
|
||||||
case LUA_TLIGHTUSERDATA:
|
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
||||||
return lua_touserdata(L, idx);
|
|
||||||
default: return NULL;
|
default: return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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) {
|
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
||||||
if (s == NULL) {
|
lua_lock(L);
|
||||||
lua_pushnil(L);
|
if (s == NULL)
|
||||||
return NULL;
|
setnilvalue(L->top);
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
lua_lock(L);
|
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
ts = luaS_new(L, s);
|
ts = luaS_new(L, s);
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top, ts);
|
||||||
api_incr_top(L);
|
s = getstr(ts); /* internal copy's address */
|
||||||
lua_unlock(L);
|
|
||||||
return getstr(ts);
|
|
||||||
}
|
}
|
||||||
|
api_incr_top(L);
|
||||||
|
lua_unlock(L);
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -530,7 +532,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
|||||||
else {
|
else {
|
||||||
CClosure *cl;
|
CClosure *cl;
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
api_check(n <= MAXUPVAL, "upvalue index too large");
|
api_check(L, n <= MAXUPVAL, "upvalue index too large");
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
cl = luaF_newCclosure(L, n);
|
cl = luaF_newCclosure(L, n);
|
||||||
cl->f = fn;
|
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);
|
Table *reg = hvalue(&G(L)->l_registry);
|
||||||
const TValue *gt; /* global table */
|
const TValue *gt; /* global table */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
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);
|
luaV_gettable(L, gt, L->top - 1, L->top - 1);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttnov(L->top - 1);
|
return ttnov(L->top - 1);
|
||||||
@@ -627,7 +630,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
|
|||||||
StkId t;
|
StkId t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
|
setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return ttnov(L->top - 1);
|
return ttnov(L->top - 1);
|
||||||
@@ -638,7 +641,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
StkId t;
|
StkId t;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setobj2s(L, L->top, luaH_getint(hvalue(t), n));
|
setobj2s(L, L->top, luaH_getint(hvalue(t), n));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -651,7 +654,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
|
|||||||
TValue k;
|
TValue k;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
setpvalue(&k, cast(void *, p));
|
setpvalue(&k, cast(void *, p));
|
||||||
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
|
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
@@ -675,7 +678,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
|||||||
|
|
||||||
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
||||||
const TValue *obj;
|
const TValue *obj;
|
||||||
Table *mt = NULL;
|
Table *mt;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
obj = index2addr(L, objindex);
|
obj = index2addr(L, objindex);
|
||||||
@@ -704,7 +707,7 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) {
|
|||||||
StkId o;
|
StkId o;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttisfulluserdata(o), "full userdata expected");
|
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
||||||
getuservalue(L, uvalue(o), L->top);
|
getuservalue(L, uvalue(o), L->top);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -717,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);
|
Table *reg = hvalue(&G(L)->l_registry);
|
||||||
const TValue *gt; /* global table */
|
const TValue *gt; /* global table */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
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);
|
luaV_settable(L, gt, L->top - 1, L->top - 2);
|
||||||
L->top -= 2; /* pop value and key */
|
L->top -= 2; /* pop value and key */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -746,7 +750,8 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
t = index2addr(L, idx);
|
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);
|
luaV_settable(L, t, L->top - 1, L->top - 2);
|
||||||
L->top -= 2; /* pop value and key */
|
L->top -= 2; /* pop value and key */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -758,7 +763,8 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
setivalue(L->top++, n);
|
setivalue(L->top, n);
|
||||||
|
api_incr_top(L);
|
||||||
luaV_settable(L, t, L->top - 1, L->top - 2);
|
luaV_settable(L, t, L->top - 1, L->top - 2);
|
||||||
L->top -= 2; /* pop value and key */
|
L->top -= 2; /* pop value and key */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -771,7 +777,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 2);
|
api_checknelems(L, 2);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttistable(o), "table expected");
|
api_check(L, ttistable(o), "table expected");
|
||||||
t = hvalue(o);
|
t = hvalue(o);
|
||||||
setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
|
setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
|
||||||
invalidateTMcache(t);
|
invalidateTMcache(t);
|
||||||
@@ -787,7 +793,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttistable(o), "table expected");
|
api_check(L, ttistable(o), "table expected");
|
||||||
t = hvalue(o);
|
t = hvalue(o);
|
||||||
luaH_setint(L, t, n, L->top - 1);
|
luaH_setint(L, t, n, L->top - 1);
|
||||||
luaC_barrierback(L, t, L->top-1);
|
luaC_barrierback(L, t, L->top-1);
|
||||||
@@ -803,7 +809,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttistable(o), "table expected");
|
api_check(L, ttistable(o), "table expected");
|
||||||
t = hvalue(o);
|
t = hvalue(o);
|
||||||
setpvalue(&k, cast(void *, p));
|
setpvalue(&k, cast(void *, p));
|
||||||
setobj2t(L, luaH_set(L, t, &k), L->top - 1);
|
setobj2t(L, luaH_set(L, t, &k), L->top - 1);
|
||||||
@@ -822,7 +828,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
|||||||
if (ttisnil(L->top - 1))
|
if (ttisnil(L->top - 1))
|
||||||
mt = NULL;
|
mt = NULL;
|
||||||
else {
|
else {
|
||||||
api_check(ttistable(L->top - 1), "table expected");
|
api_check(L, ttistable(L->top - 1), "table expected");
|
||||||
mt = hvalue(L->top - 1);
|
mt = hvalue(L->top - 1);
|
||||||
}
|
}
|
||||||
switch (ttnov(obj)) {
|
switch (ttnov(obj)) {
|
||||||
@@ -858,7 +864,7 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, 1);
|
api_checknelems(L, 1);
|
||||||
o = index2addr(L, idx);
|
o = index2addr(L, idx);
|
||||||
api_check(ttisfulluserdata(o), "full userdata expected");
|
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
||||||
setuservalue(L, uvalue(o), L->top - 1);
|
setuservalue(L, uvalue(o), L->top - 1);
|
||||||
luaC_barrier(L, gcvalue(o), L->top - 1);
|
luaC_barrier(L, gcvalue(o), L->top - 1);
|
||||||
L->top--;
|
L->top--;
|
||||||
@@ -867,12 +873,12 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** `load' and `call' functions (run Lua code)
|
** 'load' and 'call' functions (run Lua code)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define checkresults(L,na,nr) \
|
#define checkresults(L,na,nr) \
|
||||||
api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
|
api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
|
||||||
"results from function overflow current stack size")
|
"results from function overflow current stack size")
|
||||||
|
|
||||||
|
|
||||||
@@ -880,10 +886,10 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
|||||||
lua_KContext ctx, lua_KFunction k) {
|
lua_KContext ctx, lua_KFunction k) {
|
||||||
StkId func;
|
StkId func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_check(k == NULL || !isLua(L->ci),
|
api_check(L, k == NULL || !isLua(L->ci),
|
||||||
"cannot use continuations inside hooks");
|
"cannot use continuations inside hooks");
|
||||||
api_checknelems(L, nargs+1);
|
api_checknelems(L, nargs+1);
|
||||||
api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
|
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
||||||
checkresults(L, nargs, nresults);
|
checkresults(L, nargs, nresults);
|
||||||
func = L->top - (nargs+1);
|
func = L->top - (nargs+1);
|
||||||
if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
|
if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
|
||||||
@@ -902,7 +908,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
|||||||
/*
|
/*
|
||||||
** Execute a protected call.
|
** Execute a protected call.
|
||||||
*/
|
*/
|
||||||
struct CallS { /* data to `f_call' */
|
struct CallS { /* data to 'f_call' */
|
||||||
StkId func;
|
StkId func;
|
||||||
int nresults;
|
int nresults;
|
||||||
};
|
};
|
||||||
@@ -921,16 +927,16 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
|||||||
int status;
|
int status;
|
||||||
ptrdiff_t func;
|
ptrdiff_t func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_check(k == NULL || !isLua(L->ci),
|
api_check(L, k == NULL || !isLua(L->ci),
|
||||||
"cannot use continuations inside hooks");
|
"cannot use continuations inside hooks");
|
||||||
api_checknelems(L, nargs+1);
|
api_checknelems(L, nargs+1);
|
||||||
api_check(L->status == LUA_OK, "cannot do calls on non-normal thread");
|
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
||||||
checkresults(L, nargs, nresults);
|
checkresults(L, nargs, nresults);
|
||||||
if (errfunc == 0)
|
if (errfunc == 0)
|
||||||
func = 0;
|
func = 0;
|
||||||
else {
|
else {
|
||||||
StkId o = index2addr(L, errfunc);
|
StkId o = index2addr(L, errfunc);
|
||||||
api_checkstackindex(errfunc, o);
|
api_checkstackindex(L, errfunc, o);
|
||||||
func = savestack(L, o);
|
func = savestack(L, o);
|
||||||
}
|
}
|
||||||
c.func = L->top - (nargs+1); /* function to be called */
|
c.func = L->top - (nargs+1); /* function to be called */
|
||||||
@@ -969,7 +975,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
|||||||
status = luaD_protectedparser(L, &z, chunkname, mode);
|
status = luaD_protectedparser(L, &z, chunkname, mode);
|
||||||
if (status == LUA_OK) { /* no errors? */
|
if (status == LUA_OK) { /* no errors? */
|
||||||
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
|
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
|
||||||
if (f->nupvalues == 1) { /* does it have one upvalue? */
|
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
||||||
/* get global table from registry */
|
/* get global table from registry */
|
||||||
Table *reg = hvalue(&G(L)->l_registry);
|
Table *reg = hvalue(&G(L)->l_registry);
|
||||||
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
||||||
@@ -1095,7 +1101,7 @@ LUA_API int lua_next (lua_State *L, int idx) {
|
|||||||
int more;
|
int more;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
t = index2addr(L, idx);
|
t = index2addr(L, idx);
|
||||||
api_check(ttistable(t), "table expected");
|
api_check(L, ttistable(t), "table expected");
|
||||||
more = luaH_next(L, hvalue(t), L->top - 1);
|
more = luaH_next(L, hvalue(t), L->top - 1);
|
||||||
if (more) {
|
if (more) {
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
@@ -1227,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) {
|
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
|
||||||
LClosure *f;
|
LClosure *f;
|
||||||
StkId fi = index2addr(L, fidx);
|
StkId fi = index2addr(L, fidx);
|
||||||
api_check(ttisLclosure(fi), "Lua function expected");
|
api_check(L, ttisLclosure(fi), "Lua function expected");
|
||||||
f = clLvalue(fi);
|
f = clLvalue(fi);
|
||||||
api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
|
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
|
||||||
if (pf) *pf = f;
|
if (pf) *pf = f;
|
||||||
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
||||||
}
|
}
|
||||||
@@ -1243,11 +1249,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
|||||||
}
|
}
|
||||||
case LUA_TCCL: { /* C closure */
|
case LUA_TCCL: { /* C closure */
|
||||||
CClosure *f = clCvalue(fi);
|
CClosure *f = clCvalue(fi);
|
||||||
api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index");
|
api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
|
||||||
return &f->upvalue[n - 1];
|
return &f->upvalue[n - 1];
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
api_check(0, "closure expected");
|
api_check(L, 0, "closure expected");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
lapi.h
6
lapi.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 roberto Exp roberto $
|
** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 roberto Exp roberto $
|
||||||
** Auxiliary functions from Lua API
|
** Auxiliary functions from Lua API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -11,13 +11,13 @@
|
|||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
#define api_incr_top(L) {L->top++; api_check(L->top <= L->ci->top, \
|
#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
|
||||||
"stack overflow");}
|
"stack overflow");}
|
||||||
|
|
||||||
#define adjustresults(L,nres) \
|
#define adjustresults(L,nres) \
|
||||||
{ if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
|
{ if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
|
||||||
|
|
||||||
#define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \
|
#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
|
||||||
"not enough elements in the stack")
|
"not enough elements in the stack")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
58
lauxlib.c
58
lauxlib.c
@@ -1,9 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.c,v 1.269 2014/10/17 16:28:21 roberto Exp roberto $
|
** $Id: lauxlib.c,v 1.279 2014/12/14 18:32:26 roberto Exp roberto $
|
||||||
** Auxiliary functions for building Lua libraries
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lauxlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
@@ -16,9 +21,6 @@
|
|||||||
** Any function declared here could be written as an application function.
|
** Any function declared here could be written as an application function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define lauxlib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -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) {
|
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
||||||
int top = lua_gettop(L);
|
int top = lua_gettop(L);
|
||||||
lua_getinfo(L, "f", ar); /* push function */
|
lua_getinfo(L, "f", ar); /* push function */
|
||||||
lua_pushglobaltable(L);
|
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||||
if (findfield(L, top + 1, 2)) {
|
if (findfield(L, top + 1, 2)) {
|
||||||
|
const char *name = lua_tostring(L, -1);
|
||||||
|
if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
|
||||||
|
lua_pushstring(L, name + 3); /* push name without prefix */
|
||||||
|
lua_remove(L, -2); /* remove original name */
|
||||||
|
}
|
||||||
lua_copy(L, -1, top + 1); /* move name to proper place */
|
lua_copy(L, -1, top + 1); /* move name to proper place */
|
||||||
lua_pop(L, 2); /* remove pushed values */
|
lua_pop(L, 2); /* remove pushed values */
|
||||||
return 1;
|
return 1;
|
||||||
@@ -81,20 +92,18 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
|||||||
|
|
||||||
|
|
||||||
static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
||||||
if (*ar->namewhat != '\0') /* is there a name? */
|
if (pushglobalfuncname(L, ar)) { /* try first a global name */
|
||||||
lua_pushfstring(L, "function '%s'", ar->name);
|
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
||||||
|
lua_remove(L, -2); /* remove name */
|
||||||
|
}
|
||||||
|
else if (*ar->namewhat != '\0') /* is there a name from code? */
|
||||||
|
lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */
|
||||||
else if (*ar->what == 'm') /* main? */
|
else if (*ar->what == 'm') /* main? */
|
||||||
lua_pushliteral(L, "main chunk");
|
lua_pushliteral(L, "main chunk");
|
||||||
else if (*ar->what == 'C') {
|
else if (*ar->what != 'C') /* for Lua functions, use <file:line> */
|
||||||
if (pushglobalfuncname(L, ar)) {
|
|
||||||
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
|
||||||
lua_remove(L, -2); /* remove name */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
lua_pushliteral(L, "?");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
|
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
|
||||||
|
else /* nothing left... */
|
||||||
|
lua_pushliteral(L, "?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -156,7 +165,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
|
|||||||
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
|
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
|
||||||
lua_getinfo(L, "n", &ar);
|
lua_getinfo(L, "n", &ar);
|
||||||
if (strcmp(ar.namewhat, "method") == 0) {
|
if (strcmp(ar.namewhat, "method") == 0) {
|
||||||
arg--; /* do not count `self' */
|
arg--; /* do not count 'self' */
|
||||||
if (arg == 0) /* error is in the self argument itself? */
|
if (arg == 0) /* error is in the self argument itself? */
|
||||||
return luaL_error(L, "calling '%s' on bad self (%s)",
|
return luaL_error(L, "calling '%s' on bad self (%s)",
|
||||||
ar.name, extramsg);
|
ar.name, extramsg);
|
||||||
@@ -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) {
|
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
|
||||||
luaL_getmetatable(L, tname); /* try to get metatable */
|
if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */
|
||||||
if (!lua_isnil(L, -1)) /* name already in use? */
|
|
||||||
return 0; /* leave previous value on top, but return 0 */
|
return 0; /* leave previous value on top, but return 0 */
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
lua_newtable(L); /* create metatable */
|
lua_newtable(L); /* create metatable */
|
||||||
@@ -526,7 +534,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
|||||||
int ref;
|
int ref;
|
||||||
if (lua_isnil(L, -1)) {
|
if (lua_isnil(L, -1)) {
|
||||||
lua_pop(L, 1); /* remove from stack */
|
lua_pop(L, 1); /* remove from stack */
|
||||||
return LUA_REFNIL; /* `nil' has a unique fixed reference */
|
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
|
||||||
}
|
}
|
||||||
t = lua_absindex(L, t);
|
t = lua_absindex(L, t);
|
||||||
lua_rawgeti(L, t, freelist); /* get first free element */
|
lua_rawgeti(L, t, freelist); /* get first free element */
|
||||||
@@ -565,7 +573,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
|||||||
typedef struct LoadF {
|
typedef struct LoadF {
|
||||||
int n; /* number of pre-read characters */
|
int n; /* number of pre-read characters */
|
||||||
FILE *f; /* file being read */
|
FILE *f; /* file being read */
|
||||||
char buff[LUAL_BUFFERSIZE]; /* area for reading file */
|
char buff[BUFSIZ]; /* area for reading file */
|
||||||
} LoadF;
|
} LoadF;
|
||||||
|
|
||||||
|
|
||||||
@@ -658,7 +666,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
|||||||
readstatus = ferror(lf.f);
|
readstatus = ferror(lf.f);
|
||||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||||
if (readstatus) {
|
if (readstatus) {
|
||||||
lua_settop(L, fnameindex); /* ignore results from `lua_load' */
|
lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
|
||||||
return errfile(L, "read", fnameindex);
|
return errfile(L, "read", fnameindex);
|
||||||
}
|
}
|
||||||
lua_remove(L, fnameindex);
|
lua_remove(L, fnameindex);
|
||||||
@@ -918,7 +926,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
|
|||||||
while ((wild = strstr(s, p)) != NULL) {
|
while ((wild = strstr(s, p)) != NULL) {
|
||||||
luaL_addlstring(&b, s, wild - s); /* push prefix */
|
luaL_addlstring(&b, s, wild - s); /* push prefix */
|
||||||
luaL_addstring(&b, r); /* push replacement in place of pattern */
|
luaL_addstring(&b, r); /* push replacement in place of pattern */
|
||||||
s = wild + l; /* continue after `p' */
|
s = wild + l; /* continue after 'p' */
|
||||||
}
|
}
|
||||||
luaL_addstring(&b, s); /* push last suffix */
|
luaL_addstring(&b, s); /* push last suffix */
|
||||||
luaL_pushresult(&b);
|
luaL_pushresult(&b);
|
||||||
@@ -938,8 +946,8 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
|||||||
|
|
||||||
|
|
||||||
static int panic (lua_State *L) {
|
static int panic (lua_State *L) {
|
||||||
luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||||
lua_tostring(L, -1));
|
lua_tostring(L, -1));
|
||||||
return 0; /* return to Lua to abort */
|
return 0; /* return to Lua to abort */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
29
lauxlib.h
29
lauxlib.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.h,v 1.125 2014/06/26 17:25:11 roberto Exp roberto $
|
** $Id: lauxlib.h,v 1.127 2014/10/25 11:50:46 roberto Exp roberto $
|
||||||
** Auxiliary functions for building Lua libraries
|
** Auxiliary functions for building Lua libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* extra error code for `luaL_load' */
|
/* extra error code for 'luaL_load' */
|
||||||
#define LUA_ERRFILE (LUA_ERRERR+1)
|
#define LUA_ERRFILE (LUA_ERRERR+1)
|
||||||
|
|
||||||
|
|
||||||
@@ -204,6 +204,31 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** "Abstraction Layer" for basic report of messages and errors
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* print a string */
|
||||||
|
#if !defined(lua_writestring)
|
||||||
|
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* print a newline and flush the output */
|
||||||
|
#if !defined(lua_writeline)
|
||||||
|
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* print an error message */
|
||||||
|
#if !defined(lua_writestringerror)
|
||||||
|
#define lua_writestringerror(s,p) \
|
||||||
|
(fprintf(stderr, (s), (p)), fflush(stderr))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {============================================================
|
** {============================================================
|
||||||
** Compatibility with deprecated conversions
|
** Compatibility with deprecated conversions
|
||||||
|
|||||||
29
lbaselib.c
29
lbaselib.c
@@ -1,9 +1,13 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lbaselib.c,v 1.302 2014/10/17 16:28:21 roberto Exp roberto $
|
** $Id: lbaselib.c,v 1.309 2014/12/10 12:26:42 roberto Exp roberto $
|
||||||
** Basic library
|
** Basic library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lbaselib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@@ -11,9 +15,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lbaselib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -33,11 +34,11 @@ static int luaB_print (lua_State *L) {
|
|||||||
s = lua_tolstring(L, -1, &l); /* get result */
|
s = lua_tolstring(L, -1, &l); /* get result */
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return luaL_error(L, "'tostring' must return a string to 'print'");
|
return luaL_error(L, "'tostring' must return a string to 'print'");
|
||||||
if (i>1) luai_writestring("\t", 1);
|
if (i>1) lua_writestring("\t", 1);
|
||||||
luai_writestring(s, l);
|
lua_writestring(s, l);
|
||||||
lua_pop(L, 1); /* pop result */
|
lua_pop(L, 1); /* pop result */
|
||||||
}
|
}
|
||||||
luai_writeline();
|
lua_writeline();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,14 +55,12 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
do {
|
do {
|
||||||
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
||||||
: toupper((unsigned char)*s) - 'A' + 10;
|
: (toupper((unsigned char)*s) - 'A') + 10;
|
||||||
if (digit >= base) return NULL; /* invalid numeral */
|
if (digit >= base) return NULL; /* invalid numeral */
|
||||||
n = n * base + digit;
|
n = n * base + digit;
|
||||||
s++;
|
s++;
|
||||||
} while (isalnum((unsigned char)*s));
|
} while (isalnum((unsigned char)*s));
|
||||||
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
||||||
if (*s != '\0') /* invalid trailing characters? */
|
|
||||||
return NULL;
|
|
||||||
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
@@ -275,6 +274,7 @@ static int luaB_ipairs (lua_State *L) {
|
|||||||
#if defined(LUA_COMPAT_IPAIRS)
|
#if defined(LUA_COMPAT_IPAIRS)
|
||||||
return pairsmeta(L, "__ipairs", 1, iter);
|
return pairsmeta(L, "__ipairs", 1, iter);
|
||||||
#else
|
#else
|
||||||
|
luaL_checkany(L, 1);
|
||||||
lua_pushcfunction(L, iter); /* iteration function */
|
lua_pushcfunction(L, iter); /* iteration function */
|
||||||
lua_pushvalue(L, 1); /* state */
|
lua_pushvalue(L, 1); /* state */
|
||||||
lua_pushinteger(L, 0); /* initial value */
|
lua_pushinteger(L, 0); /* initial value */
|
||||||
@@ -325,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 for internal stuff, so the reader cannot change the
|
||||||
** stack top. Instead, it keeps its resulting string in a
|
** stack top. Instead, it keeps its resulting string in a
|
||||||
** reserved slot inside the stack.
|
** reserved slot inside the stack.
|
||||||
@@ -389,9 +389,10 @@ static int luaB_assert (lua_State *L) {
|
|||||||
if (lua_toboolean(L, 1)) /* condition is true? */
|
if (lua_toboolean(L, 1)) /* condition is true? */
|
||||||
return lua_gettop(L); /* return all arguments */
|
return lua_gettop(L); /* return all arguments */
|
||||||
else { /* error */
|
else { /* error */
|
||||||
if (lua_isnone(L, 2)) /* no error message? */
|
luaL_checkany(L, 1); /* there must be a condition */
|
||||||
lua_pushliteral(L, "assertion failed!"); /* use standard message */
|
lua_remove(L, 1); /* remove it */
|
||||||
lua_remove(L, 1); /* remove the condition (if there is one...) */
|
lua_pushliteral(L, "assertion failed!"); /* default message */
|
||||||
|
lua_settop(L, 1); /* leave only message (default if no other one) */
|
||||||
return luaB_error(L); /* call 'error' */
|
return luaB_error(L); /* call 'error' */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lbitlib.c,v 1.26 2014/05/15 19:28:34 roberto Exp roberto $
|
** $Id: lbitlib.c,v 1.27 2014/10/01 11:54:56 roberto Exp roberto $
|
||||||
** Standard library for bitwise operations
|
** Standard library for bitwise operations
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,6 +7,9 @@
|
|||||||
#define lbitlib_c
|
#define lbitlib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
|
|||||||
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
|
** Code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lcode_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define lcode_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lcode.h"
|
#include "lcode.h"
|
||||||
@@ -27,12 +29,8 @@
|
|||||||
#include "lvm.h"
|
#include "lvm.h"
|
||||||
|
|
||||||
|
|
||||||
/* test for x == -0 */
|
/* Maximum number of registers in a Lua function (must fit in 8 bits) */
|
||||||
#if defined(signbit)
|
#define MAXREGS 255
|
||||||
#define isminuszero(x) ((x) == 0.0 && signbit(x))
|
|
||||||
#else
|
|
||||||
#define isminuszero(x) ((x) == 0.0 && 1.0/(x) < 0.0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define hasjumps(e) ((e)->t != (e)->f)
|
#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).
|
** optimizations with consecutive instructions not in the same basic block).
|
||||||
*/
|
*/
|
||||||
int luaK_getlabel (FuncState *fs) {
|
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) {
|
static int luaK_code (FuncState *fs, Instruction i) {
|
||||||
Proto *f = fs->f;
|
Proto *f = fs->f;
|
||||||
dischargejpc(fs); /* `pc' will change */
|
dischargejpc(fs); /* 'pc' will change */
|
||||||
/* put new instruction in code array */
|
/* put new instruction in code array */
|
||||||
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||||
MAX_INT, "opcodes");
|
MAX_INT, "opcodes");
|
||||||
@@ -280,8 +278,9 @@ int luaK_codek (FuncState *fs, int reg, int k) {
|
|||||||
void luaK_checkstack (FuncState *fs, int n) {
|
void luaK_checkstack (FuncState *fs, int n) {
|
||||||
int newstack = fs->freereg + n;
|
int newstack = fs->freereg + n;
|
||||||
if (newstack > fs->f->maxstacksize) {
|
if (newstack > fs->f->maxstacksize) {
|
||||||
if (newstack >= MAXSTACK)
|
if (newstack >= MAXREGS)
|
||||||
luaX_syntaxerror(fs->ls, "function or expression too complex");
|
luaX_syntaxerror(fs->ls,
|
||||||
|
"function or expression needs too many registers");
|
||||||
fs->f->maxstacksize = cast_byte(newstack);
|
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) {
|
static int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||||
TValue o;
|
TValue o;
|
||||||
lua_assert(!luai_numisnan(r) && !isminuszero(r));
|
|
||||||
setfltvalue(&o, r);
|
setfltvalue(&o, r);
|
||||||
return addk(fs, &o, &o);
|
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) {
|
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
|
||||||
discharge2reg(fs, e, reg);
|
discharge2reg(fs, e, reg);
|
||||||
if (e->k == VJMP)
|
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)) {
|
if (hasjumps(e)) {
|
||||||
int final; /* position after whole expression */
|
int final; /* position after whole expression */
|
||||||
int p_f = NO_JUMP; /* position of an eventual LOAD false */
|
int p_f = NO_JUMP; /* position of an eventual LOAD false */
|
||||||
@@ -581,11 +574,11 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|||||||
case VKFLT: {
|
case VKFLT: {
|
||||||
e->u.info = luaK_numberK(fs, e->u.nval);
|
e->u.info = luaK_numberK(fs, e->u.nval);
|
||||||
e->k = VK;
|
e->k = VK;
|
||||||
/* go through */
|
|
||||||
}
|
}
|
||||||
|
/* FALLTHROUGH */
|
||||||
case VK: {
|
case VK: {
|
||||||
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);
|
return RKASK(e->u.info);
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
@@ -677,7 +670,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
|||||||
break;
|
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);
|
luaK_patchtohere(fs, e->t);
|
||||||
e->t = NO_JUMP;
|
e->t = NO_JUMP;
|
||||||
}
|
}
|
||||||
@@ -700,7 +693,7 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
|||||||
break;
|
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);
|
luaK_patchtohere(fs, e->f);
|
||||||
e->f = NO_JUMP;
|
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
|
** return false if folding can raise an error
|
||||||
*/
|
*/
|
||||||
static int validop (int op, TValue *v1, TValue *v2) {
|
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) {
|
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_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));
|
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 */
|
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;
|
TValue v1, v2, res;
|
||||||
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
|
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
|
||||||
return 0; /* non-numeric operands or not safe to fold */
|
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)) {
|
if (ttisinteger(&res)) {
|
||||||
e1->k = VKINT;
|
e1->k = VKINT;
|
||||||
e1->u.ival = ivalue(&res);
|
e1->u.ival = ivalue(&res);
|
||||||
}
|
}
|
||||||
else {
|
else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */
|
||||||
lua_Number n = fltvalue(&res);
|
lua_Number n = fltvalue(&res);
|
||||||
if (luai_numisnan(n) || isminuszero(n))
|
if (luai_numisnan(n) || n == 0)
|
||||||
return 0; /* folds neither NaN nor -0 */
|
return 0;
|
||||||
e1->k = VKFLT;
|
e1->k = VKFLT;
|
||||||
e1->u.nval = n;
|
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) {
|
** Code for binary and unary expressions that "produce values"
|
||||||
if (!constfolding(fs, op - OP_ADD + LUA_OPADD, e1, e2)) {
|
** (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;
|
int o1, o2;
|
||||||
if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) {
|
/* move operands to registers (if needed) */
|
||||||
o2 = 0;
|
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 */
|
o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */
|
||||||
}
|
}
|
||||||
else { /* regular case (binary operators) */
|
else { /* regular case (binary operators) */
|
||||||
o2 = luaK_exp2RK(fs, e2);
|
o2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */
|
||||||
o1 = luaK_exp2RK(fs, e1);
|
o1 = luaK_exp2RK(fs, e1);
|
||||||
}
|
}
|
||||||
if (o1 > o2) {
|
if (o1 > o2) { /* free registers in proper order */
|
||||||
freeexp(fs, e1);
|
freeexp(fs, e1);
|
||||||
freeexp(fs, e2);
|
freeexp(fs, e2);
|
||||||
}
|
}
|
||||||
@@ -817,8 +815,8 @@ static void codearith (FuncState *fs, OpCode op,
|
|||||||
freeexp(fs, e2);
|
freeexp(fs, e2);
|
||||||
freeexp(fs, e1);
|
freeexp(fs, e1);
|
||||||
}
|
}
|
||||||
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
|
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */
|
||||||
e1->k = VRELOCABLE;
|
e1->k = VRELOCABLE; /* all those operations are relocable */
|
||||||
luaK_fixline(fs, line);
|
luaK_fixline(fs, line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -831,7 +829,7 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
|
|||||||
freeexp(fs, e2);
|
freeexp(fs, e2);
|
||||||
freeexp(fs, e1);
|
freeexp(fs, e1);
|
||||||
if (cond == 0 && op != OP_EQ) {
|
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 */
|
temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
|
||||||
cond = 1;
|
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;
|
e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case OPR_MINUS: case OPR_BNOT: case OPR_LEN: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case OPR_NOT: codenot(fs, e); break;
|
case OPR_NOT: codenot(fs, e); break;
|
||||||
@@ -865,7 +863,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OPR_CONCAT: {
|
case OPR_CONCAT: {
|
||||||
luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
|
luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OPR_ADD: case OPR_SUB:
|
case OPR_ADD: case OPR_SUB:
|
||||||
@@ -911,7 +909,7 @@ void luaK_posfix (FuncState *fs, BinOpr op,
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
|
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;
|
break;
|
||||||
}
|
}
|
||||||
@@ -919,15 +917,15 @@ void luaK_posfix (FuncState *fs, BinOpr op,
|
|||||||
case OPR_IDIV: case OPR_MOD: case OPR_POW:
|
case OPR_IDIV: case OPR_MOD: case OPR_POW:
|
||||||
case OPR_BAND: case OPR_BOR: case OPR_BXOR:
|
case OPR_BAND: case OPR_BOR: case OPR_BXOR:
|
||||||
case OPR_SHL: case OPR_SHR: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case OPR_EQ: case OPR_LT: case OPR_LE: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case OPR_NE: case OPR_GT: case OPR_GE: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
default: lua_assert(0);
|
default: lua_assert(0);
|
||||||
|
|||||||
13
lcorolib.c
13
lcorolib.c
@@ -1,16 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lcorolib.c,v 1.6 2014/05/08 13:52:20 roberto Exp roberto $
|
** $Id: lcorolib.c,v 1.8 2014/10/25 11:50:46 roberto Exp roberto $
|
||||||
** Coroutine Library
|
** Coroutine Library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define lcorolib_c
|
#define lcorolib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -65,7 +66,7 @@ static int luaB_coresume (lua_State *L) {
|
|||||||
else {
|
else {
|
||||||
lua_pushboolean(L, 1);
|
lua_pushboolean(L, 1);
|
||||||
lua_insert(L, -(r + 1));
|
lua_insert(L, -(r + 1));
|
||||||
return r + 1; /* return true + `resume' returns */
|
return r + 1; /* return true + 'resume' returns */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
lctype.c
5
lctype.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lctype.c,v 1.10 2011/06/24 12:25:33 roberto Exp roberto $
|
** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp roberto $
|
||||||
** 'ctype' functions for Lua
|
** 'ctype' functions for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,6 +7,9 @@
|
|||||||
#define lctype_c
|
#define lctype_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include "lctype.h"
|
#include "lctype.h"
|
||||||
|
|
||||||
#if !LUA_USE_CTYPE /* { */
|
#if !LUA_USE_CTYPE /* { */
|
||||||
|
|||||||
74
ldblib.c
74
ldblib.c
@@ -1,24 +1,41 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldblib.c,v 1.142 2014/10/01 11:54:56 roberto Exp roberto $
|
** $Id: ldblib.c,v 1.148 2015/01/02 12:52:22 roberto Exp roberto $
|
||||||
** Interface from Lua to its debug API
|
** Interface from Lua to its debug API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ldblib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define ldblib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
#define HOOKKEY "_HKEY"
|
/*
|
||||||
|
** The hook table at registry[&HOOKKEY] maps threads to their current
|
||||||
|
** hook function. (We only need the unique address of 'HOOKKEY'.)
|
||||||
|
*/
|
||||||
|
static const int HOOKKEY = 0;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If L1 != L, L1 can be in any state, and therefore there 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int db_getregistry (lua_State *L) {
|
static int db_getregistry (lua_State *L) {
|
||||||
@@ -121,12 +138,16 @@ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Calls 'lua_getinfo' and collects all results in a new table.
|
** Calls 'lua_getinfo' and collects all results in a new table.
|
||||||
|
** L1 needs stack space for an optional input (function) plus
|
||||||
|
** two optional outputs (function and line table) from function
|
||||||
|
** 'lua_getinfo'.
|
||||||
*/
|
*/
|
||||||
static int db_getinfo (lua_State *L) {
|
static int db_getinfo (lua_State *L) {
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int arg;
|
int arg;
|
||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
const char *options = luaL_optstring(L, arg+2, "flnStu");
|
const char *options = luaL_optstring(L, arg+2, "flnStu");
|
||||||
|
checkstack(L, L1, 3);
|
||||||
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
|
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
|
||||||
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
|
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
|
||||||
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
|
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
|
||||||
@@ -184,6 +205,7 @@ static int db_getlocal (lua_State *L) {
|
|||||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||||
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
||||||
return luaL_argerror(L, arg+1, "level out of range");
|
return luaL_argerror(L, arg+1, "level out of range");
|
||||||
|
checkstack(L, L1, 1);
|
||||||
name = lua_getlocal(L1, &ar, nvar);
|
name = lua_getlocal(L1, &ar, nvar);
|
||||||
if (name) {
|
if (name) {
|
||||||
lua_xmove(L1, L, 1); /* move local value */
|
lua_xmove(L1, L, 1); /* move local value */
|
||||||
@@ -201,15 +223,21 @@ static int db_getlocal (lua_State *L) {
|
|||||||
|
|
||||||
static int db_setlocal (lua_State *L) {
|
static int db_setlocal (lua_State *L) {
|
||||||
int arg;
|
int arg;
|
||||||
|
const char *name;
|
||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||||
|
int nvar = (int)luaL_checkinteger(L, arg + 2);
|
||||||
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
||||||
return luaL_argerror(L, arg+1, "level out of range");
|
return luaL_argerror(L, arg+1, "level out of range");
|
||||||
luaL_checkany(L, arg+3);
|
luaL_checkany(L, arg+3);
|
||||||
lua_settop(L, arg+3);
|
lua_settop(L, arg+3);
|
||||||
|
checkstack(L, L1, 1);
|
||||||
lua_xmove(L, L1, 1);
|
lua_xmove(L, L1, 1);
|
||||||
lua_pushstring(L, lua_setlocal(L1, &ar, (int)luaL_checkinteger(L, arg+2)));
|
name = lua_setlocal(L1, &ar, nvar);
|
||||||
|
if (name == NULL)
|
||||||
|
lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
|
||||||
|
lua_pushstring(L, name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,13 +298,6 @@ static int db_upvaluejoin (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** The hook table (at registry[HOOKKEY]) maps threads to their current
|
|
||||||
** hook function
|
|
||||||
*/
|
|
||||||
#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Call hook function registered at hook table for the current
|
** Call hook function registered at hook table for the current
|
||||||
** thread (if there is one)
|
** thread (if there is one)
|
||||||
@@ -284,7 +305,7 @@ static int db_upvaluejoin (lua_State *L) {
|
|||||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||||
static const char *const hooknames[] =
|
static const char *const hooknames[] =
|
||||||
{"call", "return", "line", "count", "tail call"};
|
{"call", "return", "line", "count", "tail call"};
|
||||||
gethooktable(L);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||||
lua_pushthread(L);
|
lua_pushthread(L);
|
||||||
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
||||||
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
||||||
@@ -337,16 +358,20 @@ static int db_sethook (lua_State *L) {
|
|||||||
count = (int)luaL_optinteger(L, arg + 3, 0);
|
count = (int)luaL_optinteger(L, arg + 3, 0);
|
||||||
func = hookf; mask = makemask(smask, count);
|
func = hookf; mask = makemask(smask, count);
|
||||||
}
|
}
|
||||||
if (gethooktable(L) == 0) { /* creating hook table? */
|
if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
|
||||||
|
lua_createtable(L, 0, 2); /* create a hook table */
|
||||||
|
lua_pushvalue(L, -1);
|
||||||
|
lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
|
||||||
lua_pushstring(L, "k");
|
lua_pushstring(L, "k");
|
||||||
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
||||||
}
|
}
|
||||||
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key */
|
checkstack(L, L1, 1);
|
||||||
lua_pushvalue(L, arg+1); /* value */
|
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
|
||||||
|
lua_pushvalue(L, arg + 1); /* value (hook function) */
|
||||||
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
|
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
|
||||||
lua_sethook(L1, func, mask, count); /* set hooks */
|
lua_sethook(L1, func, mask, count);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,10 +382,13 @@ static int db_gethook (lua_State *L) {
|
|||||||
char buff[5];
|
char buff[5];
|
||||||
int mask = lua_gethookmask(L1);
|
int mask = lua_gethookmask(L1);
|
||||||
lua_Hook hook = lua_gethook(L1);
|
lua_Hook hook = lua_gethook(L1);
|
||||||
if (hook != NULL && hook != hookf) /* external hook? */
|
if (hook == NULL) /* no hook? */
|
||||||
|
lua_pushnil(L);
|
||||||
|
else if (hook != hookf) /* external hook? */
|
||||||
lua_pushliteral(L, "external hook");
|
lua_pushliteral(L, "external hook");
|
||||||
else {
|
else { /* hook table must exist */
|
||||||
gethooktable(L);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||||
|
checkstack(L, L1, 1);
|
||||||
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||||
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
||||||
lua_remove(L, -2); /* remove hook table */
|
lua_remove(L, -2); /* remove hook table */
|
||||||
@@ -374,13 +402,13 @@ static int db_gethook (lua_State *L) {
|
|||||||
static int db_debug (lua_State *L) {
|
static int db_debug (lua_State *L) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char buffer[250];
|
char buffer[250];
|
||||||
luai_writestringerror("%s", "lua_debug> ");
|
lua_writestringerror("%s", "lua_debug> ");
|
||||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||||
strcmp(buffer, "cont\n") == 0)
|
strcmp(buffer, "cont\n") == 0)
|
||||||
return 0;
|
return 0;
|
||||||
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
||||||
lua_pcall(L, 0, 0, 0))
|
lua_pcall(L, 0, 0, 0))
|
||||||
luai_writestringerror("%s\n", lua_tostring(L, -1));
|
lua_writestringerror("%s\n", lua_tostring(L, -1));
|
||||||
lua_settop(L, 0); /* remove eventual returns */
|
lua_settop(L, 0); /* remove eventual returns */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
147
ldebug.c
147
ldebug.c
@@ -1,18 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldebug.c,v 2.100 2014/07/30 14:00:14 roberto Exp roberto $
|
** $Id: ldebug.c,v 2.114 2015/03/28 19:14:47 roberto Exp roberto $
|
||||||
** Debug Interface
|
** Debug Interface
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ldebug_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
#define ldebug_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -33,6 +34,10 @@
|
|||||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
||||||
|
|
||||||
|
|
||||||
|
/* Active Lua function (given call info) */
|
||||||
|
#define ci_func(ci) (clLvalue((ci)->func))
|
||||||
|
|
||||||
|
|
||||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
|
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
|
||||||
|
|
||||||
|
|
||||||
@@ -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)
|
** 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) {
|
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
|
||||||
int nparams = clLvalue(ci->func)->p->numparams;
|
int nparams = clLvalue(ci->func)->p->numparams;
|
||||||
if (n >= ci->u.l.base - ci->func - nparams)
|
if (n >= cast_int(ci->u.l.base - ci->func) - nparams)
|
||||||
return NULL; /* no such vararg */
|
return NULL; /* no such vararg */
|
||||||
else {
|
else {
|
||||||
*pos = ci->func + nparams + n;
|
*pos = ci->func + nparams + n;
|
||||||
@@ -143,6 +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) {
|
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||||
const char *name;
|
const char *name;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
|
swapextra(L);
|
||||||
if (ar == NULL) { /* information about non-active function? */
|
if (ar == NULL) { /* information about non-active function? */
|
||||||
if (!isLfunction(L->top - 1)) /* not a Lua function? */
|
if (!isLfunction(L->top - 1)) /* not a Lua function? */
|
||||||
name = NULL;
|
name = NULL;
|
||||||
@@ -150,25 +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);
|
name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
|
||||||
}
|
}
|
||||||
else { /* active function; get information through 'ar' */
|
else { /* active function; get information through 'ar' */
|
||||||
StkId pos = 0; /* to avoid warnings */
|
StkId pos = NULL; /* to avoid warnings */
|
||||||
name = findlocal(L, ar->i_ci, n, &pos);
|
name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
if (name) {
|
if (name) {
|
||||||
setobj2s(L, L->top, pos);
|
setobj2s(L, L->top, pos);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
swapextra(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
|
||||||
StkId pos = 0; /* to avoid warnings */
|
StkId pos = NULL; /* to avoid warnings */
|
||||||
const char *name = findlocal(L, ar->i_ci, n, &pos);
|
const char *name;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
if (name)
|
swapextra(L);
|
||||||
|
name = findlocal(L, ar->i_ci, n, &pos);
|
||||||
|
if (name) {
|
||||||
setobjs2s(L, pos, L->top - 1);
|
setobjs2s(L, pos, L->top - 1);
|
||||||
L->top--; /* pop value */
|
L->top--; /* pop value */
|
||||||
|
}
|
||||||
|
swapextra(L);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@@ -268,10 +295,11 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
|||||||
CallInfo *ci;
|
CallInfo *ci;
|
||||||
StkId func;
|
StkId func;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
|
swapextra(L);
|
||||||
if (*what == '>') {
|
if (*what == '>') {
|
||||||
ci = NULL;
|
ci = NULL;
|
||||||
func = L->top - 1;
|
func = L->top - 1;
|
||||||
api_check(ttisfunction(func), "function expected");
|
api_check(L, ttisfunction(func), "function expected");
|
||||||
what++; /* skip the '>' */
|
what++; /* skip the '>' */
|
||||||
L->top--; /* pop function */
|
L->top--; /* pop function */
|
||||||
}
|
}
|
||||||
@@ -286,6 +314,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
|||||||
setobjs2s(L, L->top, func);
|
setobjs2s(L, L->top, func);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
}
|
}
|
||||||
|
swapextra(L); /* correct before option 'L', which can raise a mem. error */
|
||||||
if (strchr(what, 'L'))
|
if (strchr(what, 'L'))
|
||||||
collectvalidlines(L, cl);
|
collectvalidlines(L, cl);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
@@ -365,7 +394,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) {
|
|||||||
case OP_JMP: {
|
case OP_JMP: {
|
||||||
int b = GETARG_sBx(i);
|
int b = GETARG_sBx(i);
|
||||||
int dest = pc + 1 + b;
|
int dest = pc + 1 + b;
|
||||||
/* jump is forward and do not skip `lastpc'? */
|
/* jump is forward and do not skip 'lastpc'? */
|
||||||
if (pc < dest && dest <= lastpc) {
|
if (pc < dest && dest <= lastpc) {
|
||||||
if (dest > jmptarget)
|
if (dest > jmptarget)
|
||||||
jmptarget = dest; /* update 'jmptarget' */
|
jmptarget = dest; /* update 'jmptarget' */
|
||||||
@@ -437,10 +466,14 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
|
|||||||
|
|
||||||
|
|
||||||
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
||||||
TMS tm;
|
TMS tm = (TMS)0; /* to avoid warnings */
|
||||||
Proto *p = ci_func(ci)->p; /* calling function */
|
Proto *p = ci_func(ci)->p; /* calling function */
|
||||||
int pc = currentpc(ci); /* calling instruction index */
|
int pc = currentpc(ci); /* calling instruction index */
|
||||||
Instruction i = p->code[pc]; /* calling instruction */
|
Instruction i = p->code[pc]; /* calling instruction */
|
||||||
|
if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
|
||||||
|
*name = "?";
|
||||||
|
return "hook";
|
||||||
|
}
|
||||||
switch (GET_OPCODE(i)) {
|
switch (GET_OPCODE(i)) {
|
||||||
case OP_CALL:
|
case OP_CALL:
|
||||||
case OP_TAILCALL: /* get function name */
|
case OP_TAILCALL: /* get function name */
|
||||||
@@ -450,26 +483,27 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
|
|||||||
return "for iterator";
|
return "for iterator";
|
||||||
}
|
}
|
||||||
/* all other instructions can call only through metamethods */
|
/* all other instructions can call only through metamethods */
|
||||||
case OP_SELF:
|
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
|
||||||
case OP_GETTABUP:
|
tm = TM_INDEX;
|
||||||
case OP_GETTABLE: tm = TM_INDEX; break;
|
break;
|
||||||
case OP_SETTABUP:
|
case OP_SETTABUP: case OP_SETTABLE:
|
||||||
case OP_SETTABLE: tm = TM_NEWINDEX; break;
|
tm = TM_NEWINDEX;
|
||||||
case OP_EQ: tm = TM_EQ; break;
|
break;
|
||||||
case OP_ADD: tm = TM_ADD; break;
|
case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
|
||||||
case OP_SUB: tm = TM_SUB; break;
|
case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
|
||||||
case OP_MUL: tm = TM_MUL; break;
|
case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
|
||||||
case OP_DIV: tm = TM_DIV; break;
|
int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */
|
||||||
case OP_IDIV: tm = TM_IDIV; break;
|
tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */
|
||||||
case OP_MOD: tm = TM_MOD; break;
|
break;
|
||||||
case OP_POW: tm = TM_POW; break;
|
}
|
||||||
case OP_UNM: tm = TM_UNM; break;
|
case OP_UNM: tm = TM_UNM; break;
|
||||||
|
case OP_BNOT: tm = TM_BNOT; break;
|
||||||
case OP_LEN: tm = TM_LEN; break;
|
case OP_LEN: tm = TM_LEN; break;
|
||||||
|
case OP_CONCAT: tm = TM_CONCAT; break;
|
||||||
|
case OP_EQ: tm = TM_EQ; break;
|
||||||
case OP_LT: tm = TM_LT; break;
|
case OP_LT: tm = TM_LT; break;
|
||||||
case OP_LE: tm = TM_LE; break;
|
case OP_LE: tm = TM_LE; break;
|
||||||
case OP_CONCAT: tm = TM_CONCAT; break;
|
default: lua_assert(0); /* other instructions cannot call a function */
|
||||||
default:
|
|
||||||
return NULL; /* else no useful name can be found */
|
|
||||||
}
|
}
|
||||||
*name = getstr(G(L)->tmname[tm]);
|
*name = getstr(G(L)->tmname[tm]);
|
||||||
return "metamethod";
|
return "metamethod";
|
||||||
@@ -480,17 +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
|
** The subtraction of two potentially unrelated pointers is
|
||||||
** (used only for error messages, so efficiency is not a big concern)
|
** not ISO C, but it should not crash a program; the subsequent
|
||||||
|
** checks are ISO C and ensure a correct result.
|
||||||
*/
|
*/
|
||||||
static int isinstack (CallInfo *ci, const TValue *o) {
|
static int isinstack (CallInfo *ci, const TValue *o) {
|
||||||
StkId p;
|
ptrdiff_t i = o - ci->u.l.base;
|
||||||
for (p = ci->u.l.base; p < ci->top; p++)
|
return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o);
|
||||||
if (o == p) return 1;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Checks whether value 'o' came from an upvalue. (That can only happen
|
||||||
|
** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
|
||||||
|
** upvalues.)
|
||||||
|
*/
|
||||||
static const char *getupvalname (CallInfo *ci, const TValue *o,
|
static const char *getupvalname (CallInfo *ci, const TValue *o,
|
||||||
const char **name) {
|
const char **name) {
|
||||||
LClosure *c = ci_func(ci);
|
LClosure *c = ci_func(ci);
|
||||||
@@ -506,7 +544,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
|
|||||||
|
|
||||||
|
|
||||||
static const char *varinfo (lua_State *L, const TValue *o) {
|
static const char *varinfo (lua_State *L, const TValue *o) {
|
||||||
const char *name;
|
const char *name = NULL; /* to avoid warnings */
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
const char *kind = NULL;
|
const char *kind = NULL;
|
||||||
if (isLua(ci)) {
|
if (isLua(ci)) {
|
||||||
@@ -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;
|
lua_Number temp;
|
||||||
if (!tonumber(p1, &temp)) /* first operand is wrong? */
|
if (!tonumber(p1, &temp)) /* first operand is wrong? */
|
||||||
p2 = p1; /* now second is wrong */
|
p2 = p1; /* now second is wrong */
|
||||||
luaG_typeerror(L, p2, "perform arithmetic on");
|
luaG_typeerror(L, p2, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -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) {
|
/* add src:line information to 'msg' */
|
||||||
CallInfo *ci = L->ci;
|
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
|
||||||
if (isLua(ci)) { /* is Lua code? */
|
int line) {
|
||||||
char buff[LUA_IDSIZE]; /* add file:line information */
|
char buff[LUA_IDSIZE];
|
||||||
int line = currentline(ci);
|
if (src)
|
||||||
TString *src = ci_func(ci)->p->source;
|
luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
|
||||||
if (src)
|
else { /* no source available; use "?" instead */
|
||||||
luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
|
buff[0] = '?'; buff[1] = '\0';
|
||||||
else { /* no source available; use "?" instead */
|
|
||||||
buff[0] = '?'; buff[1] = '\0';
|
|
||||||
}
|
|
||||||
luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
|
||||||
}
|
}
|
||||||
|
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
l_noret luaG_errormsg (lua_State *L) {
|
l_noret luaG_errormsg (lua_State *L) {
|
||||||
if (L->errfunc != 0) { /* is there an error handling function? */
|
if (L->errfunc != 0) { /* is there an error handling function? */
|
||||||
StkId errfunc = restorestack(L, L->errfunc);
|
StkId errfunc = restorestack(L, L->errfunc);
|
||||||
if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
|
|
||||||
setobjs2s(L, L->top, L->top - 1); /* move argument */
|
setobjs2s(L, L->top, L->top - 1); /* move argument */
|
||||||
setobjs2s(L, L->top - 1, errfunc); /* push function */
|
setobjs2s(L, L->top - 1, errfunc); /* push function */
|
||||||
L->top++;
|
L->top++; /* assume EXTRA_STACK */
|
||||||
luaD_call(L, L->top - 2, 1, 0); /* call it */
|
luaD_call(L, L->top - 2, 1, 0); /* call it */
|
||||||
}
|
}
|
||||||
luaD_throw(L, LUA_ERRRUN);
|
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, ...) {
|
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||||
|
CallInfo *ci = L->ci;
|
||||||
|
const char *msg;
|
||||||
va_list argp;
|
va_list argp;
|
||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
addinfo(L, luaO_pushvfstring(L, fmt, argp));
|
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
|
||||||
va_end(argp);
|
va_end(argp);
|
||||||
|
if (isLua(ci)) /* if Lua function, add source:line information */
|
||||||
|
luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
|
||||||
luaG_errormsg(L);
|
luaG_errormsg(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
ldebug.h
12
ldebug.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldebug.h,v 2.10 2013/05/06 17:19:11 roberto Exp roberto $
|
** $Id: ldebug.h,v 2.13 2015/03/11 16:10:41 roberto Exp roberto $
|
||||||
** Auxiliary functions from Debug Interface module
|
** Auxiliary functions from Debug Interface module
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -17,21 +17,21 @@
|
|||||||
|
|
||||||
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
||||||
|
|
||||||
/* Active Lua function (given call info) */
|
|
||||||
#define ci_func(ci) (clLvalue((ci)->func))
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
|
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
|
||||||
const char *opname);
|
const char *opname);
|
||||||
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2,
|
||||||
|
const char *msg);
|
||||||
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
||||||
|
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
|
||||||
|
TString *src, int line);
|
||||||
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
||||||
LUAI_FUNC void luaG_traceexec (lua_State *L);
|
LUAI_FUNC void luaG_traceexec (lua_State *L);
|
||||||
|
|
||||||
|
|||||||
66
ldo.c
66
ldo.c
@@ -1,17 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldo.c,v 2.129 2014/10/08 12:20:26 roberto Exp roberto $
|
** $Id: ldo.c,v 2.137 2015/03/30 16:05:23 roberto Exp roberto $
|
||||||
** Stack and Call structure of Lua
|
** Stack and Call structure of Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ldo_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define ldo_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -67,7 +69,7 @@
|
|||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI handling with long jumps */
|
/* ISO C handling with long jumps */
|
||||||
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
|
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
|
||||||
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
|
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
|
||||||
#define luai_jmpbuf jmp_buf
|
#define luai_jmpbuf jmp_buf
|
||||||
@@ -119,8 +121,11 @@ l_noret luaD_throw (lua_State *L, int errcode) {
|
|||||||
}
|
}
|
||||||
else { /* no handler at all; abort */
|
else { /* no handler at all; abort */
|
||||||
if (g->panic) { /* panic function? */
|
if (g->panic) { /* panic function? */
|
||||||
|
seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */
|
||||||
|
if (L->ci->top < L->top)
|
||||||
|
L->ci->top = L->top; /* pushing msg. can break this invariant */
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
g->panic(L); /* call it (last chance to jump out) */
|
g->panic(L); /* call panic function (last chance to jump out) */
|
||||||
}
|
}
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
@@ -281,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);
|
const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
|
||||||
StkId p;
|
StkId p;
|
||||||
ptrdiff_t funcr = savestack(L, func);
|
|
||||||
if (!ttisfunction(tm))
|
if (!ttisfunction(tm))
|
||||||
luaG_typeerror(L, func, "call");
|
luaG_typeerror(L, func, "call");
|
||||||
/* Open a hole inside the stack at `func' */
|
/* Open a hole inside the stack at 'func' */
|
||||||
for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
|
for (p = L->top; p > func; p--)
|
||||||
incr_top(L);
|
setobjs2s(L, p, p-1);
|
||||||
func = restorestack(L, funcr); /* previous call may change stack */
|
L->top++; /* slot ensured by caller */
|
||||||
setobj2s(L, func, tm); /* tag method is the new function to be called */
|
setobj2s(L, func, tm); /* tag method is the new function to be called */
|
||||||
return func;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -315,6 +323,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|||||||
case LUA_TCCL: { /* C closure */
|
case LUA_TCCL: { /* C closure */
|
||||||
f = clCvalue(func)->f;
|
f = clCvalue(func)->f;
|
||||||
Cfunc:
|
Cfunc:
|
||||||
|
luaC_checkGC(L); /* stack grow uses memory */
|
||||||
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
||||||
ci = next_ci(L); /* now 'enter' new function */
|
ci = next_ci(L); /* now 'enter' new function */
|
||||||
ci->nresults = nresults;
|
ci->nresults = nresults;
|
||||||
@@ -322,20 +331,20 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|||||||
ci->top = L->top + LUA_MINSTACK;
|
ci->top = L->top + LUA_MINSTACK;
|
||||||
lua_assert(ci->top <= L->stack_last);
|
lua_assert(ci->top <= L->stack_last);
|
||||||
ci->callstatus = 0;
|
ci->callstatus = 0;
|
||||||
luaC_checkGC(L); /* stack grow uses memory */
|
|
||||||
if (L->hookmask & LUA_MASKCALL)
|
if (L->hookmask & LUA_MASKCALL)
|
||||||
luaD_hook(L, LUA_HOOKCALL, -1);
|
luaD_hook(L, LUA_HOOKCALL, -1);
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
n = (*f)(L); /* do the actual call */
|
n = (*f)(L); /* do the actual call */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
luaD_poscall(L, L->top - n);
|
luaD_poscall(L, L->top - n, n);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case LUA_TLCL: { /* Lua function: prepare its call */
|
case LUA_TLCL: { /* Lua function: prepare its call */
|
||||||
StkId base;
|
StkId base;
|
||||||
Proto *p = clLvalue(func)->p;
|
Proto *p = clLvalue(func)->p;
|
||||||
n = cast_int(L->top - func) - 1; /* number of real arguments */
|
n = cast_int(L->top - func) - 1; /* number of real arguments */
|
||||||
|
luaC_checkGC(L); /* stack grow uses memory */
|
||||||
luaD_checkstack(L, p->maxstacksize);
|
luaD_checkstack(L, p->maxstacksize);
|
||||||
for (; n < p->numparams; n++)
|
for (; n < p->numparams; n++)
|
||||||
setnilvalue(L->top++); /* complete missing arguments */
|
setnilvalue(L->top++); /* complete missing arguments */
|
||||||
@@ -356,20 +365,21 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
|
|||||||
ci->u.l.savedpc = p->code; /* starting point */
|
ci->u.l.savedpc = p->code; /* starting point */
|
||||||
ci->callstatus = CIST_LUA;
|
ci->callstatus = CIST_LUA;
|
||||||
L->top = ci->top;
|
L->top = ci->top;
|
||||||
luaC_checkGC(L); /* stack grow uses memory */
|
|
||||||
if (L->hookmask & LUA_MASKCALL)
|
if (L->hookmask & LUA_MASKCALL)
|
||||||
callhook(L, ci);
|
callhook(L, ci);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
default: { /* not a function */
|
default: { /* not a function */
|
||||||
func = tryfuncTM(L, func); /* retry with 'function' tag method */
|
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 */
|
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;
|
StkId res;
|
||||||
int wanted, i;
|
int wanted, i;
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
@@ -383,9 +393,9 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
|
|||||||
}
|
}
|
||||||
res = ci->func; /* res == final position of 1st result */
|
res = ci->func; /* res == final position of 1st result */
|
||||||
wanted = ci->nresults;
|
wanted = ci->nresults;
|
||||||
L->ci = ci = ci->previous; /* back to caller */
|
L->ci = ci->previous; /* back to caller */
|
||||||
/* move results to correct place */
|
/* 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++);
|
setobjs2s(L, res++, firstResult++);
|
||||||
while (i-- > 0)
|
while (i-- > 0)
|
||||||
setnilvalue(res++);
|
setnilvalue(res++);
|
||||||
@@ -439,7 +449,7 @@ static void finishCcall (lua_State *L, int status) {
|
|||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
/* finish 'luaD_precall' */
|
/* finish 'luaD_precall' */
|
||||||
luaD_poscall(L, L->top - n);
|
luaD_poscall(L, L->top - n, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -523,7 +533,8 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
|
|||||||
*/
|
*/
|
||||||
static void resume (lua_State *L, void *ud) {
|
static void resume (lua_State *L, void *ud) {
|
||||||
int nCcalls = L->nCcalls;
|
int nCcalls = L->nCcalls;
|
||||||
StkId firstArg = cast(StkId, ud);
|
int n = *(cast(int*, ud)); /* number of arguments */
|
||||||
|
StkId firstArg = L->top - n; /* first argument */
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
if (nCcalls >= LUAI_MAXCCALLS)
|
if (nCcalls >= LUAI_MAXCCALLS)
|
||||||
resume_error(L, "C stack overflow", firstArg);
|
resume_error(L, "C stack overflow", firstArg);
|
||||||
@@ -543,14 +554,13 @@ static void resume (lua_State *L, void *ud) {
|
|||||||
luaV_execute(L); /* just continue running Lua code */
|
luaV_execute(L); /* just continue running Lua code */
|
||||||
else { /* 'common' yield */
|
else { /* 'common' yield */
|
||||||
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
|
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
|
||||||
int n;
|
|
||||||
lua_unlock(L);
|
lua_unlock(L);
|
||||||
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
|
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
api_checknelems(L, n);
|
api_checknelems(L, n);
|
||||||
firstArg = L->top - n; /* yield results come from continuation */
|
firstArg = L->top - n; /* yield results come from continuation */
|
||||||
}
|
}
|
||||||
luaD_poscall(L, firstArg); /* finish 'luaD_precall' */
|
luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */
|
||||||
}
|
}
|
||||||
unroll(L, NULL); /* run continuation */
|
unroll(L, NULL); /* run continuation */
|
||||||
}
|
}
|
||||||
@@ -566,7 +576,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
|||||||
L->nCcalls = (from) ? from->nCcalls + 1 : 1;
|
L->nCcalls = (from) ? from->nCcalls + 1 : 1;
|
||||||
L->nny = 0; /* allow yields */
|
L->nny = 0; /* allow yields */
|
||||||
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
||||||
status = luaD_rawrunprotected(L, resume, L->top - nargs);
|
status = luaD_rawrunprotected(L, resume, &nargs);
|
||||||
if (status == -1) /* error calling 'lua_resume'? */
|
if (status == -1) /* error calling 'lua_resume'? */
|
||||||
status = LUA_ERRRUN;
|
status = LUA_ERRRUN;
|
||||||
else { /* continue running after recoverable errors */
|
else { /* continue running after recoverable errors */
|
||||||
@@ -575,7 +585,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
|
|||||||
status = luaD_rawrunprotected(L, unroll, &status);
|
status = luaD_rawrunprotected(L, unroll, &status);
|
||||||
}
|
}
|
||||||
if (errorstatus(status)) { /* unrecoverable error? */
|
if (errorstatus(status)) { /* unrecoverable error? */
|
||||||
L->status = cast_byte(status); /* mark thread as `dead' */
|
L->status = cast_byte(status); /* mark thread as 'dead' */
|
||||||
seterrorobj(L, status, L->top); /* push error message */
|
seterrorobj(L, status, L->top); /* push error message */
|
||||||
L->ci->top = L->top;
|
L->ci->top = L->top;
|
||||||
}
|
}
|
||||||
@@ -609,7 +619,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
|
|||||||
L->status = LUA_YIELD;
|
L->status = LUA_YIELD;
|
||||||
ci->extra = savestack(L, ci->func); /* save current 'func' */
|
ci->extra = savestack(L, ci->func); /* save current 'func' */
|
||||||
if (isLua(ci)) { /* inside a hook? */
|
if (isLua(ci)) { /* inside a hook? */
|
||||||
api_check(k == NULL, "hooks cannot continue after yielding");
|
api_check(L, k == NULL, "hooks cannot continue after yielding");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
|
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
|
||||||
@@ -650,7 +660,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
|||||||
/*
|
/*
|
||||||
** Execute a protected parser.
|
** Execute a protected parser.
|
||||||
*/
|
*/
|
||||||
struct SParser { /* data to `f_parser' */
|
struct SParser { /* data to 'f_parser' */
|
||||||
ZIO *z;
|
ZIO *z;
|
||||||
Mbuffer buff; /* dynamic structure used by the scanner */
|
Mbuffer buff; /* dynamic structure used by the scanner */
|
||||||
Dyndata dyd; /* dynamic structures used by the parser */
|
Dyndata dyd; /* dynamic structures used by the parser */
|
||||||
|
|||||||
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
|
** Stack and Call structure of Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
|
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
|
||||||
|
|
||||||
|
|
||||||
/* type of protected functions, to be ran by `runprotected' */
|
/* type of protected functions, to be ran by 'runprotected' */
|
||||||
typedef void (*Pfunc) (lua_State *L, void *ud);
|
typedef void (*Pfunc) (lua_State *L, void *ud);
|
||||||
|
|
||||||
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||||
@@ -34,7 +34,7 @@ LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults,
|
|||||||
int allowyield);
|
int allowyield);
|
||||||
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||||
ptrdiff_t oldtop, ptrdiff_t ef);
|
ptrdiff_t oldtop, ptrdiff_t ef);
|
||||||
LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult);
|
LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres);
|
||||||
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
||||||
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
||||||
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
||||||
|
|||||||
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
|
** save precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#define ldump_c
|
#define ldump_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
@@ -71,14 +74,15 @@ static void DumpString (const TString *s, DumpState *D) {
|
|||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
DumpByte(0, D);
|
DumpByte(0, D);
|
||||||
else {
|
else {
|
||||||
size_t size = s->len + 1; /* include trailing '\0' */
|
size_t size = tsslen(s) + 1; /* include trailing '\0' */
|
||||||
|
const char *str = getstr(s);
|
||||||
if (size < 0xFF)
|
if (size < 0xFF)
|
||||||
DumpByte(cast_int(size), D);
|
DumpByte(cast_int(size), D);
|
||||||
else {
|
else {
|
||||||
DumpByte(0xFF, D);
|
DumpByte(0xFF, D);
|
||||||
DumpVar(size, D);
|
DumpVar(size, D);
|
||||||
}
|
}
|
||||||
DumpVector(getstr(s), size - 1, D); /* no need to save '\0' */
|
DumpVector(str, size - 1, D); /* no need to save '\0' */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
lfunc.c
14
lfunc.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lfunc.c,v 2.42 2014/06/18 22:59:29 roberto Exp roberto $
|
** $Id: lfunc.c,v 2.44 2014/10/25 11:50:46 roberto Exp roberto $
|
||||||
** Auxiliary functions to manipulate prototypes and closures
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#define lfunc_c
|
#define lfunc_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lfunc.h"
|
#include "lfunc.h"
|
||||||
@@ -82,7 +84,7 @@ void luaF_close (lua_State *L, StkId level) {
|
|||||||
UpVal *uv;
|
UpVal *uv;
|
||||||
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
|
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
|
||||||
lua_assert(upisopen(uv));
|
lua_assert(upisopen(uv));
|
||||||
L->openupval = uv->u.open.next; /* remove from `open' list */
|
L->openupval = uv->u.open.next; /* remove from 'open' list */
|
||||||
if (uv->refcount == 0) /* no references? */
|
if (uv->refcount == 0) /* no references? */
|
||||||
luaM_free(L, uv); /* free upvalue */
|
luaM_free(L, uv); /* free upvalue */
|
||||||
else {
|
else {
|
||||||
@@ -132,7 +134,7 @@ void luaF_freeproto (lua_State *L, Proto *f) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Look for n-th local variable at line `line' in function `func'.
|
** Look for n-th local variable at line 'line' in function 'func'.
|
||||||
** Returns NULL if not found.
|
** Returns NULL if not found.
|
||||||
*/
|
*/
|
||||||
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||||
|
|||||||
9
lfunc.h
9
lfunc.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lfunc.h,v 2.13 2014/02/18 13:39:37 roberto Exp roberto $
|
** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 roberto Exp roberto $
|
||||||
** Auxiliary functions to manipulate prototypes and closures
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -22,6 +22,13 @@
|
|||||||
#define isintwups(L) (L->twups != L)
|
#define isintwups(L) (L->twups != L)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** maximum number of upvalues in a closure (both C and Lua). (Value
|
||||||
|
** must fit in a VM register.)
|
||||||
|
*/
|
||||||
|
#define MAXUPVAL 255
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Upvalues for Lua closures
|
** Upvalues for Lua closures
|
||||||
*/
|
*/
|
||||||
|
|||||||
82
lgc.c
82
lgc.c
@@ -1,14 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lgc.c,v 2.195 2014/09/04 18:15:29 roberto Exp roberto $
|
** $Id: lgc.c,v 2.204 2015/03/04 13:51:55 roberto Exp roberto $
|
||||||
** Garbage Collector
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lgc_c
|
#define lgc_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -80,8 +83,13 @@
|
|||||||
#define markvalue(g,o) { checkconsistency(o); \
|
#define markvalue(g,o) { checkconsistency(o); \
|
||||||
if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
|
if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
|
||||||
|
|
||||||
#define markobject(g,t) \
|
#define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); }
|
||||||
{ if ((t) && iswhite(t)) reallymarkobject(g, obj2gco(t)); }
|
|
||||||
|
/*
|
||||||
|
** mark an object that can be NULL (either because it is really optional,
|
||||||
|
** or it was stripped as debug info, or inside an uncompleted structure)
|
||||||
|
*/
|
||||||
|
#define markobjectN(g,t) { if (t) markobject(g,t); }
|
||||||
|
|
||||||
static void reallymarkobject (global_State *g, GCObject *o);
|
static void reallymarkobject (global_State *g, GCObject *o);
|
||||||
|
|
||||||
@@ -119,14 +127,14 @@ static void removeentry (Node *n) {
|
|||||||
/*
|
/*
|
||||||
** tells whether a key or value can be cleared from a weak
|
** tells whether a key or value can be cleared from a weak
|
||||||
** table. Non-collectable objects are never removed from weak
|
** table. Non-collectable objects are never removed from weak
|
||||||
** tables. Strings behave as `values', so are never removed too. for
|
** tables. Strings behave as 'values', so are never removed too. for
|
||||||
** other objects: if really collected, cannot keep them; for objects
|
** other objects: if really collected, cannot keep them; for objects
|
||||||
** being finalized, keep them in keys, but not in values
|
** being finalized, keep them in keys, but not in values
|
||||||
*/
|
*/
|
||||||
static int iscleared (global_State *g, const TValue *o) {
|
static int iscleared (global_State *g, const TValue *o) {
|
||||||
if (!iscollectable(o)) return 0;
|
if (!iscollectable(o)) return 0;
|
||||||
else if (ttisstring(o)) {
|
else if (ttisstring(o)) {
|
||||||
markobject(g, tsvalue(o)); /* strings are `values', so are never weak */
|
markobject(g, tsvalue(o)); /* strings are 'values', so are never weak */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else return iswhite(gcvalue(o));
|
else return iswhite(gcvalue(o));
|
||||||
@@ -223,15 +231,19 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
|||||||
reentry:
|
reentry:
|
||||||
white2gray(o);
|
white2gray(o);
|
||||||
switch (o->tt) {
|
switch (o->tt) {
|
||||||
case LUA_TSHRSTR:
|
case LUA_TSHRSTR: {
|
||||||
|
gray2black(o);
|
||||||
|
g->GCmemtrav += sizelstring(gco2ts(o)->shrlen);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case LUA_TLNGSTR: {
|
case LUA_TLNGSTR: {
|
||||||
gray2black(o);
|
gray2black(o);
|
||||||
g->GCmemtrav += sizestring(gco2ts(o));
|
g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TUSERDATA: {
|
case LUA_TUSERDATA: {
|
||||||
TValue uvalue;
|
TValue uvalue;
|
||||||
markobject(g, gco2u(o)->metatable); /* mark its metatable */
|
markobjectN(g, gco2u(o)->metatable); /* mark its metatable */
|
||||||
gray2black(o);
|
gray2black(o);
|
||||||
g->GCmemtrav += sizeudata(gco2u(o));
|
g->GCmemtrav += sizeudata(gco2u(o));
|
||||||
getuservalue(g->mainthread, gco2u(o), &uvalue);
|
getuservalue(g->mainthread, gco2u(o), &uvalue);
|
||||||
@@ -272,7 +284,7 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
|||||||
static void markmt (global_State *g) {
|
static void markmt (global_State *g) {
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i < LUA_NUMTAGS; i++)
|
for (i=0; i < LUA_NUMTAGS; i++)
|
||||||
markobject(g, g->mt[i]);
|
markobjectN(g, g->mt[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -434,7 +446,7 @@ static void traversestrongtable (global_State *g, Table *h) {
|
|||||||
static lu_mem traversetable (global_State *g, Table *h) {
|
static lu_mem traversetable (global_State *g, Table *h) {
|
||||||
const char *weakkey, *weakvalue;
|
const char *weakkey, *weakvalue;
|
||||||
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
|
||||||
markobject(g, h->metatable);
|
markobjectN(g, h->metatable);
|
||||||
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
||||||
((weakkey = strchr(svalue(mode), 'k')),
|
((weakkey = strchr(svalue(mode), 'k')),
|
||||||
(weakvalue = strchr(svalue(mode), 'v')),
|
(weakvalue = strchr(svalue(mode), 'v')),
|
||||||
@@ -454,19 +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) {
|
static int traverseproto (global_State *g, Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
if (f->cache && iswhite(f->cache))
|
if (f->cache && iswhite(f->cache))
|
||||||
f->cache = NULL; /* allow cache to be collected */
|
f->cache = NULL; /* allow cache to be collected */
|
||||||
markobject(g, f->source);
|
markobjectN(g, f->source);
|
||||||
for (i = 0; i < f->sizek; i++) /* mark literals */
|
for (i = 0; i < f->sizek; i++) /* mark literals */
|
||||||
markvalue(g, &f->k[i]);
|
markvalue(g, &f->k[i]);
|
||||||
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
|
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
|
||||||
markobject(g, f->upvalues[i].name);
|
markobjectN(g, f->upvalues[i].name);
|
||||||
for (i = 0; i < f->sizep; i++) /* mark nested protos */
|
for (i = 0; i < f->sizep; i++) /* mark nested protos */
|
||||||
markobject(g, f->p[i]);
|
markobjectN(g, f->p[i]);
|
||||||
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
|
||||||
markobject(g, f->locvars[i].varname);
|
markobjectN(g, f->locvars[i].varname);
|
||||||
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
|
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
|
||||||
sizeof(Proto *) * f->sizep +
|
sizeof(Proto *) * f->sizep +
|
||||||
sizeof(TValue) * f->sizek +
|
sizeof(TValue) * f->sizek +
|
||||||
@@ -491,7 +508,7 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) {
|
|||||||
*/
|
*/
|
||||||
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
|
||||||
int i;
|
int i;
|
||||||
markobject(g, cl->p); /* mark its prototype */
|
markobjectN(g, cl->p); /* mark its prototype */
|
||||||
for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */
|
for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */
|
||||||
UpVal *uv = cl->upvals[i];
|
UpVal *uv = cl->upvals[i];
|
||||||
if (uv != NULL) {
|
if (uv != NULL) {
|
||||||
@@ -686,9 +703,10 @@ static void freeobj (lua_State *L, GCObject *o) {
|
|||||||
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
|
||||||
case LUA_TSHRSTR:
|
case LUA_TSHRSTR:
|
||||||
luaS_remove(L, gco2ts(o)); /* remove it from hash table */
|
luaS_remove(L, gco2ts(o)); /* remove it from hash table */
|
||||||
/* go through */
|
luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen));
|
||||||
|
break;
|
||||||
case LUA_TLNGSTR: {
|
case LUA_TLNGSTR: {
|
||||||
luaM_freemem(L, o, sizestring(gco2ts(o)));
|
luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: lua_assert(0);
|
default: lua_assert(0);
|
||||||
@@ -702,10 +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
|
** sweep at most 'count' elements from a list of GCObjects erasing dead
|
||||||
** objects, where a dead (not alive) object is one marked with the "old"
|
** objects, where a dead object is one marked with the old (non current)
|
||||||
** (non current) white and not fixed; change all non-dead objects back
|
** white; change all non-dead objects back to white, preparing for next
|
||||||
** to white, preparing for next collection cycle.
|
** collection cycle. Return where to continue the traversal or NULL if
|
||||||
** When object is a thread, sweep its list of open upvalues too.
|
** list is finished.
|
||||||
*/
|
*/
|
||||||
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
@@ -718,7 +736,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
|
|||||||
*p = curr->next; /* remove 'curr' from list */
|
*p = curr->next; /* remove 'curr' from list */
|
||||||
freeobj(L, curr); /* erase 'curr' */
|
freeobj(L, curr); /* erase 'curr' */
|
||||||
}
|
}
|
||||||
else { /* update marks */
|
else { /* change mark to 'white' */
|
||||||
curr->marked = cast_byte((marked & maskcolors) | white);
|
curr->marked = cast_byte((marked & maskcolors) | white);
|
||||||
p = &curr->next; /* go to next element */
|
p = &curr->next; /* go to next element */
|
||||||
}
|
}
|
||||||
@@ -909,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;
|
** Set a reasonable "time" to wait before starting a new GC cycle; cycle
|
||||||
** cycle will start when memory use hits threshold
|
** will start when memory use hits threshold. (Division by 'estimate'
|
||||||
|
** should be OK: it cannot be zero (because Lua cannot even start with
|
||||||
|
** less than PAUSEADJ bytes).
|
||||||
*/
|
*/
|
||||||
static void setpause (global_State *g) {
|
static void setpause (global_State *g) {
|
||||||
l_mem threshold, debt;
|
l_mem threshold, debt;
|
||||||
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
|
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
|
||||||
|
lua_assert(estimate > 0);
|
||||||
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
|
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
|
||||||
? estimate * g->gcpause /* no overflow */
|
? estimate * g->gcpause /* no overflow */
|
||||||
: MAX_LMEM; /* overflow; truncate to maximum */
|
: MAX_LMEM; /* overflow; truncate to maximum */
|
||||||
@@ -972,7 +993,7 @@ static l_mem atomic (lua_State *L) {
|
|||||||
/* remark occasional upvalues of (maybe) dead threads */
|
/* remark occasional upvalues of (maybe) dead threads */
|
||||||
remarkupvals(g);
|
remarkupvals(g);
|
||||||
propagateall(g); /* propagate changes */
|
propagateall(g); /* propagate changes */
|
||||||
work = g->GCmemtrav; /* stop counting (do not recount gray-agains) */
|
work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */
|
||||||
g->gray = grayagain;
|
g->gray = grayagain;
|
||||||
propagateall(g); /* traverse 'grayagain' list */
|
propagateall(g); /* traverse 'grayagain' list */
|
||||||
g->GCmemtrav = 0; /* restart counting */
|
g->GCmemtrav = 0; /* restart counting */
|
||||||
@@ -992,10 +1013,11 @@ static l_mem atomic (lua_State *L) {
|
|||||||
/* at this point, all resurrected objects are marked. */
|
/* at this point, all resurrected objects are marked. */
|
||||||
/* remove dead objects from weak tables */
|
/* remove dead objects from weak tables */
|
||||||
clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */
|
clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */
|
||||||
clearkeys(g, g->allweak, NULL); /* clear keys from all allweak tables */
|
clearkeys(g, g->allweak, NULL); /* clear keys from all 'allweak' tables */
|
||||||
/* clear values from resurrected weak tables */
|
/* clear values from resurrected weak tables */
|
||||||
clearvalues(g, g->weak, origweak);
|
clearvalues(g, g->weak, origweak);
|
||||||
clearvalues(g, g->allweak, origall);
|
clearvalues(g, g->allweak, origall);
|
||||||
|
luaS_clearcache(g);
|
||||||
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */
|
||||||
work += g->GCmemtrav; /* complete counting */
|
work += g->GCmemtrav; /* complete counting */
|
||||||
return work; /* estimate of memory marked by 'atomic' */
|
return work; /* estimate of memory marked by 'atomic' */
|
||||||
@@ -1031,7 +1053,7 @@ static lu_mem singlestep (lua_State *L) {
|
|||||||
g->GCmemtrav = 0;
|
g->GCmemtrav = 0;
|
||||||
lua_assert(g->gray);
|
lua_assert(g->gray);
|
||||||
propagatemark(g);
|
propagatemark(g);
|
||||||
if (g->gray == NULL) /* no more `gray' objects? */
|
if (g->gray == NULL) /* no more gray objects? */
|
||||||
g->gcstate = GCSatomic; /* finish propagate phase */
|
g->gcstate = GCSatomic; /* finish propagate phase */
|
||||||
return g->GCmemtrav; /* memory traversed in this step */
|
return g->GCmemtrav; /* memory traversed in this step */
|
||||||
}
|
}
|
||||||
@@ -1122,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
|
** some operations which could change the interpreter state in some
|
||||||
** unexpected ways (running finalizers and shrinking some structures).
|
** unexpected ways (running finalizers and shrinking some structures).
|
||||||
** Before running the collection, check 'keepinvariant'; if it is true,
|
** Before running the collection, check 'keepinvariant'; if it is true,
|
||||||
|
|||||||
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
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -74,7 +74,7 @@
|
|||||||
#define testbit(x,b) testbits(x, bitmask(b))
|
#define testbit(x,b) testbits(x, bitmask(b))
|
||||||
|
|
||||||
|
|
||||||
/* Layout for bit use in `marked' field: */
|
/* Layout for bit use in 'marked' field: */
|
||||||
#define WHITE0BIT 0 /* object is white (type 0) */
|
#define WHITE0BIT 0 /* object is white (type 0) */
|
||||||
#define WHITE1BIT 1 /* object is white (type 1) */
|
#define WHITE1BIT 1 /* object is white (type 1) */
|
||||||
#define BLACKBIT 2 /* object is black */
|
#define BLACKBIT 2 /* object is black */
|
||||||
|
|||||||
38
linit.c
38
linit.c
@@ -1,20 +1,33 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: linit.c,v 1.33 2014/02/06 17:32:33 roberto Exp roberto $
|
** $Id: linit.c,v 1.37 2014/12/09 15:00:17 roberto Exp roberto $
|
||||||
** Initialization of libraries for lua.c and other clients
|
** Initialization of libraries for lua.c and other clients
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define linit_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If you embed Lua in your program and need to open the standard
|
** If you embed Lua in your program and need to open the standard
|
||||||
** libraries, call luaL_openlibs in your program. If you need a
|
** libraries, call luaL_openlibs in your program. If you need a
|
||||||
** different set of libraries, copy this file to your project and edit
|
** different set of libraries, copy this file to your project and edit
|
||||||
** it to suit your needs.
|
** it to suit your needs.
|
||||||
|
**
|
||||||
|
** You can also *preload* libraries, so that a later 'require' can
|
||||||
|
** open the library, which is already linked to the application.
|
||||||
|
** For that, do the following code:
|
||||||
|
**
|
||||||
|
** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||||
|
** lua_pushcfunction(L, luaopen_modname);
|
||||||
|
** lua_setfield(L, -2, modname);
|
||||||
|
** lua_pop(L, 1); // remove _PRELOAD table
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
#define linit_c
|
|
||||||
#define LUA_LIB
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
@@ -35,8 +48,8 @@ static const luaL_Reg loadedlibs[] = {
|
|||||||
{LUA_OSLIBNAME, luaopen_os},
|
{LUA_OSLIBNAME, luaopen_os},
|
||||||
{LUA_STRLIBNAME, luaopen_string},
|
{LUA_STRLIBNAME, luaopen_string},
|
||||||
{LUA_MATHLIBNAME, luaopen_math},
|
{LUA_MATHLIBNAME, luaopen_math},
|
||||||
{LUA_DBLIBNAME, luaopen_debug},
|
|
||||||
{LUA_UTF8LIBNAME, luaopen_utf8},
|
{LUA_UTF8LIBNAME, luaopen_utf8},
|
||||||
|
{LUA_DBLIBNAME, luaopen_debug},
|
||||||
#if defined(LUA_COMPAT_BITLIB)
|
#if defined(LUA_COMPAT_BITLIB)
|
||||||
{LUA_BITLIBNAME, luaopen_bit32},
|
{LUA_BITLIBNAME, luaopen_bit32},
|
||||||
#endif
|
#endif
|
||||||
@@ -44,27 +57,12 @@ static const luaL_Reg loadedlibs[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** these libs are preloaded and must be required before used
|
|
||||||
*/
|
|
||||||
static const luaL_Reg preloadedlibs[] = {
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
LUALIB_API void luaL_openlibs (lua_State *L) {
|
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||||
const luaL_Reg *lib;
|
const luaL_Reg *lib;
|
||||||
/* call open functions from 'loadedlibs' and set results to global table */
|
/* "require" functions from 'loadedlibs' and set results to global table */
|
||||||
for (lib = loadedlibs; lib->func; lib++) {
|
for (lib = loadedlibs; lib->func; lib++) {
|
||||||
luaL_requiref(L, lib->name, lib->func, 1);
|
luaL_requiref(L, lib->name, lib->func, 1);
|
||||||
lua_pop(L, 1); /* remove lib */
|
lua_pop(L, 1); /* remove lib */
|
||||||
}
|
}
|
||||||
/* add open functions from 'preloadedlibs' into 'package.preload' table */
|
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
|
||||||
for (lib = preloadedlibs; lib->func; lib++) {
|
|
||||||
lua_pushcfunction(L, lib->func);
|
|
||||||
lua_setfield(L, -2, lib->name);
|
|
||||||
}
|
|
||||||
lua_pop(L, 1); /* remove _PRELOAD table */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
60
liolib.c
60
liolib.c
@@ -1,18 +1,13 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: liolib.c,v 2.135 2014/10/22 11:44:20 roberto Exp roberto $
|
** $Id: liolib.c,v 2.143 2015/03/06 19:09:08 roberto Exp roberto $
|
||||||
** Standard I/O (and system) library
|
** Standard I/O (and system) library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define liolib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
/*
|
#include "lprefix.h"
|
||||||
** This definition must come before the inclusion of 'stdio.h'; it
|
|
||||||
** should not affect non-POSIX systems
|
|
||||||
*/
|
|
||||||
#if !defined(_FILE_OFFSET_BITS)
|
|
||||||
#define _LARGEFILE_SOURCE 1
|
|
||||||
#define _FILE_OFFSET_BITS 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@@ -22,9 +17,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define liolib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -60,14 +52,14 @@
|
|||||||
#define l_popen(L,c,m) (fflush(NULL), popen(c,m))
|
#define l_popen(L,c,m) (fflush(NULL), popen(c,m))
|
||||||
#define l_pclose(L,file) (pclose(file))
|
#define l_pclose(L,file) (pclose(file))
|
||||||
|
|
||||||
#elif defined(LUA_WIN) /* }{ */
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
|
|
||||||
#define l_popen(L,c,m) (_popen(c,m))
|
#define l_popen(L,c,m) (_popen(c,m))
|
||||||
#define l_pclose(L,file) (_pclose(file))
|
#define l_pclose(L,file) (_pclose(file))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definitions */
|
/* ISO C definitions */
|
||||||
#define l_popen(L,c,m) \
|
#define l_popen(L,c,m) \
|
||||||
((void)((void)c, m), \
|
((void)((void)c, m), \
|
||||||
luaL_error(L, "'popen' not supported"), \
|
luaL_error(L, "'popen' not supported"), \
|
||||||
@@ -112,7 +104,7 @@
|
|||||||
#define l_ftell(f) ftello(f)
|
#define l_ftell(f) ftello(f)
|
||||||
#define l_seeknum off_t
|
#define l_seeknum off_t
|
||||||
|
|
||||||
#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \
|
#elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \
|
||||||
&& defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
|
&& defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
|
||||||
|
|
||||||
/* Windows (but not DDK) and Visual C++ 2005 or higher */
|
/* Windows (but not DDK) and Visual C++ 2005 or higher */
|
||||||
@@ -122,7 +114,7 @@
|
|||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definitions */
|
/* ISO C definitions */
|
||||||
#define l_fseek(f,o,w) fseek(f,o,w)
|
#define l_fseek(f,o,w) fseek(f,o,w)
|
||||||
#define l_ftell(f) ftell(f)
|
#define l_ftell(f) ftell(f)
|
||||||
#define l_seeknum long
|
#define l_seeknum long
|
||||||
@@ -182,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
|
** before opening the actual file; so, if there is a memory error, the
|
||||||
** file is not left opened.
|
** file is not left opened.
|
||||||
*/
|
*/
|
||||||
@@ -418,12 +410,6 @@ static int readdigits (RN *rn, int hex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* access to locale "radix character" (decimal point) */
|
|
||||||
#if !defined(l_getlocaledecpoint)
|
|
||||||
#define l_getlocaledecpoint() (localeconv()->decimal_point[0])
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
||||||
** Then it calls 'lua_stringtonumber' to check whether the format is
|
** Then it calls 'lua_stringtonumber' to check whether the format is
|
||||||
@@ -433,9 +419,10 @@ static int read_number (lua_State *L, FILE *f) {
|
|||||||
RN rn;
|
RN rn;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int hex = 0;
|
int hex = 0;
|
||||||
char decp[2] = ".";
|
char decp[2];
|
||||||
rn.f = f; rn.n = 0;
|
rn.f = f; rn.n = 0;
|
||||||
decp[0] = l_getlocaledecpoint(); /* get decimal point from locale */
|
decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
|
||||||
|
decp[1] = '\0';
|
||||||
l_lockfile(rn.f);
|
l_lockfile(rn.f);
|
||||||
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
||||||
test2(&rn, "-+"); /* optional signal */
|
test2(&rn, "-+"); /* optional signal */
|
||||||
@@ -465,21 +452,28 @@ static int read_number (lua_State *L, FILE *f) {
|
|||||||
static int test_eof (lua_State *L, FILE *f) {
|
static int test_eof (lua_State *L, FILE *f) {
|
||||||
int c = getc(f);
|
int c = getc(f);
|
||||||
ungetc(c, f); /* no-op when c == EOF */
|
ungetc(c, f); /* no-op when c == EOF */
|
||||||
lua_pushlstring(L, NULL, 0);
|
lua_pushliteral(L, "");
|
||||||
return (c != EOF);
|
return (c != EOF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int read_line (lua_State *L, FILE *f, int chop) {
|
static int read_line (lua_State *L, FILE *f, int chop) {
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
int c;
|
int c = '\0';
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
l_lockfile(f);
|
while (c != EOF && c != '\n') { /* repeat until end of line */
|
||||||
while ((c = l_getc(f)) != EOF && c != '\n')
|
char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */
|
||||||
luaL_addchar(&b, c);
|
int i = 0;
|
||||||
l_unlockfile(f);
|
l_lockfile(f); /* no memory errors can happen inside the lock */
|
||||||
if (!chop && c == '\n') luaL_addchar(&b, c);
|
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
|
||||||
|
buff[i++] = c;
|
||||||
|
l_unlockfile(f);
|
||||||
|
luaL_addsize(&b, i);
|
||||||
|
}
|
||||||
|
if (!chop && c == '\n') /* want a newline and have one? */
|
||||||
|
luaL_addchar(&b, c); /* add ending newline to result */
|
||||||
luaL_pushresult(&b); /* close buffer */
|
luaL_pushresult(&b); /* close buffer */
|
||||||
|
/* return ok if read something (either a newline or something else) */
|
||||||
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -524,7 +518,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
|||||||
success = 1;
|
success = 1;
|
||||||
for (n = first; nargs-- && success; n++) {
|
for (n = first; nargs-- && success; n++) {
|
||||||
if (lua_type(L, n) == LUA_TNUMBER) {
|
if (lua_type(L, n) == LUA_TNUMBER) {
|
||||||
size_t l = (size_t)lua_tointeger(L, n);
|
size_t l = (size_t)luaL_checkinteger(L, n);
|
||||||
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
55
llex.c
55
llex.c
@@ -1,19 +1,22 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: llex.c,v 2.83 2014/10/17 16:28:21 roberto Exp roberto $
|
** $Id: llex.c,v 2.92 2015/04/03 18:41:57 roberto Exp roberto $
|
||||||
** Lexical Analyzer
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define llex_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define llex_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lctype.h"
|
#include "lctype.h"
|
||||||
|
#include "ldebug.h"
|
||||||
#include "ldo.h"
|
#include "ldo.h"
|
||||||
#include "lgc.h"
|
#include "lgc.h"
|
||||||
#include "llex.h"
|
#include "llex.h"
|
||||||
@@ -41,7 +44,7 @@ static const char *const luaX_tokens [] = {
|
|||||||
"return", "then", "true", "until", "while",
|
"return", "then", "true", "until", "while",
|
||||||
"//", "..", "...", "==", ">=", "<=", "~=",
|
"//", "..", "...", "==", ">=", "<=", "~=",
|
||||||
"<<", ">>", "::", "<eof>",
|
"<<", ">>", "::", "<eof>",
|
||||||
"<number>", "<number>", "<name>", "<string>"
|
"<number>", "<integer>", "<name>", "<string>"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -66,7 +69,7 @@ static void save (LexState *ls, int c) {
|
|||||||
|
|
||||||
void luaX_init (lua_State *L) {
|
void luaX_init (lua_State *L) {
|
||||||
int i;
|
int i;
|
||||||
TString *e = luaS_new(L, LUA_ENV); /* create env name */
|
TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */
|
||||||
luaC_fix(L, obj2gco(e)); /* never collect this name */
|
luaC_fix(L, obj2gco(e)); /* never collect this name */
|
||||||
for (i=0; i<NUM_RESERVED; i++) {
|
for (i=0; i<NUM_RESERVED; i++) {
|
||||||
TString *ts = luaS_new(L, luaX_tokens[i]);
|
TString *ts = luaS_new(L, luaX_tokens[i]);
|
||||||
@@ -104,9 +107,7 @@ static const char *txtToken (LexState *ls, int token) {
|
|||||||
|
|
||||||
|
|
||||||
static l_noret lexerror (LexState *ls, const char *msg, int token) {
|
static l_noret lexerror (LexState *ls, const char *msg, int token) {
|
||||||
char buff[LUA_IDSIZE];
|
msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
|
||||||
luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
|
|
||||||
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
|
|
||||||
if (token)
|
if (token)
|
||||||
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
|
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
|
||||||
luaD_throw(ls->L, LUA_ERRSYNTAX);
|
luaD_throw(ls->L, LUA_ERRSYNTAX);
|
||||||
@@ -125,7 +126,7 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
|||||||
*/
|
*/
|
||||||
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||||
lua_State *L = ls->L;
|
lua_State *L = ls->L;
|
||||||
TValue *o; /* entry for `str' */
|
TValue *o; /* entry for 'str' */
|
||||||
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
||||||
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
||||||
o = luaH_set(L, ls->h, L->top - 1);
|
o = luaH_set(L, ls->h, L->top - 1);
|
||||||
@@ -150,16 +151,17 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
|||||||
static void inclinenumber (LexState *ls) {
|
static void inclinenumber (LexState *ls) {
|
||||||
int old = ls->current;
|
int old = ls->current;
|
||||||
lua_assert(currIsNewline(ls));
|
lua_assert(currIsNewline(ls));
|
||||||
next(ls); /* skip `\n' or `\r' */
|
next(ls); /* skip '\n' or '\r' */
|
||||||
if (currIsNewline(ls) && ls->current != old)
|
if (currIsNewline(ls) && ls->current != old)
|
||||||
next(ls); /* skip `\n\r' or `\r\n' */
|
next(ls); /* skip '\n\r' or '\r\n' */
|
||||||
if (++ls->linenumber >= MAX_INT)
|
if (++ls->linenumber >= MAX_INT)
|
||||||
luaX_syntaxerror(ls, "chunk has too many lines");
|
lexerror(ls, "chunk has too many lines", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
||||||
int firstchar) {
|
int firstchar) {
|
||||||
|
ls->t.token = 0;
|
||||||
ls->decpoint = '.';
|
ls->decpoint = '.';
|
||||||
ls->L = L;
|
ls->L = L;
|
||||||
ls->current = firstchar;
|
ls->current = firstchar;
|
||||||
@@ -169,7 +171,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
|||||||
ls->linenumber = 1;
|
ls->linenumber = 1;
|
||||||
ls->lastline = 1;
|
ls->lastline = 1;
|
||||||
ls->source = source;
|
ls->source = source;
|
||||||
ls->envn = luaS_new(L, LUA_ENV); /* get env name */
|
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */
|
||||||
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,11 +220,6 @@ static void buffreplace (LexState *ls, char from, char to) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if !defined(l_getlocaledecpoint)
|
|
||||||
#define l_getlocaledecpoint() (localeconv()->decimal_point[0])
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
|
#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -231,7 +228,7 @@ static void buffreplace (LexState *ls, char from, char to) {
|
|||||||
*/
|
*/
|
||||||
static void trydecpoint (LexState *ls, TValue *o) {
|
static void trydecpoint (LexState *ls, TValue *o) {
|
||||||
char old = ls->decpoint;
|
char old = ls->decpoint;
|
||||||
ls->decpoint = l_getlocaledecpoint();
|
ls->decpoint = lua_getlocaledecpoint();
|
||||||
buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
|
buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
|
||||||
if (!buff2num(ls->buff, o)) {
|
if (!buff2num(ls->buff, o)) {
|
||||||
/* format error with correct decimal point: no more options */
|
/* format error with correct decimal point: no more options */
|
||||||
@@ -280,8 +277,9 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** skip a sequence '[=*[' or ']=*]' and return its number of '='s or
|
** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return
|
||||||
** -1 if sequence is malformed
|
** its number of '='s; otherwise, return a negative number (-1 iff there
|
||||||
|
** are no '='s after initial bracket)
|
||||||
*/
|
*/
|
||||||
static int skip_sep (LexState *ls) {
|
static int skip_sep (LexState *ls) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@@ -298,7 +296,7 @@ static int skip_sep (LexState *ls) {
|
|||||||
|
|
||||||
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
||||||
int line = ls->linenumber; /* initial line (for error message) */
|
int line = ls->linenumber; /* initial line (for error message) */
|
||||||
save_and_next(ls); /* skip 2nd `[' */
|
save_and_next(ls); /* skip 2nd '[' */
|
||||||
if (currIsNewline(ls)) /* string starts with a newline? */
|
if (currIsNewline(ls)) /* string starts with a newline? */
|
||||||
inclinenumber(ls); /* skip it */
|
inclinenumber(ls); /* skip it */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -312,7 +310,7 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
|||||||
}
|
}
|
||||||
case ']': {
|
case ']': {
|
||||||
if (skip_sep(ls) == sep) {
|
if (skip_sep(ls) == sep) {
|
||||||
save_and_next(ls); /* skip 2nd `]' */
|
save_and_next(ls); /* skip 2nd ']' */
|
||||||
goto endloop;
|
goto endloop;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -438,7 +436,7 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
|
|||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
|
esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
|
||||||
c = readdecesc(ls); /* digital escape \ddd */
|
c = readdecesc(ls); /* digital escape '\ddd' */
|
||||||
goto only_save;
|
goto only_save;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -480,7 +478,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
next(ls);
|
next(ls);
|
||||||
if (ls->current == '[') { /* long comment? */
|
if (ls->current == '[') { /* long comment? */
|
||||||
int sep = skip_sep(ls);
|
int sep = skip_sep(ls);
|
||||||
luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
|
luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
|
||||||
if (sep >= 0) {
|
if (sep >= 0) {
|
||||||
read_long_string(ls, NULL, sep); /* skip long comment */
|
read_long_string(ls, NULL, sep); /* skip long comment */
|
||||||
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
|
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
|
||||||
@@ -498,8 +496,9 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
read_long_string(ls, seminfo, sep);
|
read_long_string(ls, seminfo, sep);
|
||||||
return TK_STRING;
|
return TK_STRING;
|
||||||
}
|
}
|
||||||
else if (sep == -1) return '[';
|
else if (sep != -1) /* '[=...' missing second bracket */
|
||||||
else lexerror(ls, "invalid long string delimiter", TK_STRING);
|
lexerror(ls, "invalid long string delimiter", TK_STRING);
|
||||||
|
return '[';
|
||||||
}
|
}
|
||||||
case '=': {
|
case '=': {
|
||||||
next(ls);
|
next(ls);
|
||||||
|
|||||||
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
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,6 +14,10 @@
|
|||||||
#define FIRST_RESERVED 257
|
#define FIRST_RESERVED 257
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(LUA_ENV)
|
||||||
|
#define LUA_ENV "_ENV"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* WARNING: if you change the order of this enumeration,
|
* WARNING: if you change the order of this enumeration,
|
||||||
@@ -54,7 +58,7 @@ typedef struct Token {
|
|||||||
typedef struct LexState {
|
typedef struct LexState {
|
||||||
int current; /* current character (charint) */
|
int current; /* current character (charint) */
|
||||||
int linenumber; /* input line counter */
|
int linenumber; /* input line counter */
|
||||||
int lastline; /* line of last token `consumed' */
|
int lastline; /* line of last token 'consumed' */
|
||||||
Token t; /* current token */
|
Token t; /* current token */
|
||||||
Token lookahead; /* look ahead token */
|
Token lookahead; /* look ahead token */
|
||||||
struct FuncState *fs; /* current function (parser) */
|
struct FuncState *fs; /* current function (parser) */
|
||||||
|
|||||||
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 $
|
** $Id: llimits.h,v 1.134 2015/03/06 19:49:50 roberto Exp roberto $
|
||||||
** Limits, basic types, and some other `installation-dependent' definitions
|
** Limits, basic types, and some other 'installation-dependent' definitions
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -14,16 +14,24 @@
|
|||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
/*
|
||||||
typedef unsigned LUA_INT32 lu_int32;
|
** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
|
||||||
|
** the total memory used by Lua (in bytes). Usually, 'size_t' and
|
||||||
|
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
|
||||||
|
*/
|
||||||
|
#if defined(LUAI_MEM) /* { external definitions? */
|
||||||
typedef LUAI_UMEM lu_mem;
|
typedef LUAI_UMEM lu_mem;
|
||||||
|
|
||||||
typedef LUAI_MEM l_mem;
|
typedef LUAI_MEM l_mem;
|
||||||
|
#elif LUAI_BITSINT >= 32 /* }{ */
|
||||||
|
typedef size_t lu_mem;
|
||||||
|
typedef ptrdiff_t l_mem;
|
||||||
|
#else /* 16-bit ints */ /* }{ */
|
||||||
|
typedef unsigned long lu_mem;
|
||||||
|
typedef long l_mem;
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/* chars used as small naturals (so that 'char' is reserved for characters) */
|
||||||
/* chars used as small naturals (so that `char' is reserved for characters) */
|
|
||||||
typedef unsigned char lu_byte;
|
typedef unsigned char lu_byte;
|
||||||
|
|
||||||
|
|
||||||
@@ -44,11 +52,11 @@ typedef unsigned char lu_byte;
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** conversion of pointer to integer:
|
** conversion of pointer to unsigned integer:
|
||||||
** this is for hashing only; there is no problem if the integer
|
** this is for hashing only; there is no problem if the integer
|
||||||
** cannot hold the whole pointer value
|
** cannot hold the whole pointer value
|
||||||
*/
|
*/
|
||||||
#define point2int(p) ((unsigned int)((lu_mem)(p) & UINT_MAX))
|
#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -80,22 +88,20 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
/*
|
/*
|
||||||
** assertion for checking API calls
|
** assertion for checking API calls
|
||||||
*/
|
*/
|
||||||
#if defined(LUA_USE_APICHECK)
|
#if !defined(luai_apicheck)
|
||||||
#include <assert.h>
|
#define luai_apicheck(l,e) lua_assert(e)
|
||||||
#define luai_apicheck(e) assert(e)
|
|
||||||
#else
|
|
||||||
#define luai_apicheck(e) lua_assert(e)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||||
#define api_check(e,msg) luai_apicheck((e) && msg)
|
|
||||||
|
|
||||||
|
|
||||||
|
/* macro to avoid warnings about unused variables */
|
||||||
#if !defined(UNUSED)
|
#if !defined(UNUSED)
|
||||||
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
#define UNUSED(x) ((void)(x))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* type casts (a macro highlights casts in the code) */
|
||||||
#define cast(t, exp) ((t)(exp))
|
#define cast(t, exp) ((t)(exp))
|
||||||
|
|
||||||
#define cast_void(i) cast(void, (i))
|
#define cast_void(i) cast(void, (i))
|
||||||
@@ -112,7 +118,7 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** cast a lua_Unsigned to a signed lua_Integer; this cast is
|
** cast a lua_Unsigned to a signed lua_Integer; this cast is
|
||||||
** not strict ANSI C, but two-complement architectures should
|
** not strict ISO C, but two-complement architectures should
|
||||||
** work fine.
|
** work fine.
|
||||||
*/
|
*/
|
||||||
#if !defined(l_castU2S)
|
#if !defined(l_castU2S)
|
||||||
@@ -125,7 +131,7 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
*/
|
*/
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
#define l_noret void __attribute__((noreturn))
|
#define l_noret void __attribute__((noreturn))
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER) && _MSC_VER >= 1200
|
||||||
#define l_noret void __declspec(noreturn)
|
#define l_noret void __declspec(noreturn)
|
||||||
#else
|
#else
|
||||||
#define l_noret void
|
#define l_noret void
|
||||||
@@ -141,29 +147,47 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
#define LUAI_MAXCCALLS 200
|
#define LUAI_MAXCCALLS 200
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
** maximum number of upvalues in a closure (both C and Lua). (Value
|
|
||||||
** must fit in an unsigned char.)
|
|
||||||
*/
|
|
||||||
#define MAXUPVAL UCHAR_MAX
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** type for virtual-machine instructions
|
** type for virtual-machine instructions;
|
||||||
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||||
*/
|
*/
|
||||||
typedef lu_int32 Instruction;
|
#if LUAI_BITSINT >= 32
|
||||||
|
typedef unsigned int Instruction;
|
||||||
|
#else
|
||||||
|
typedef unsigned long Instruction;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* maximum stack for a Lua function */
|
/*
|
||||||
#define MAXSTACK 250
|
** Maximum length for short strings, that is, strings that are
|
||||||
|
** internalized. (Cannot be smaller than reserved words or tags for
|
||||||
|
** metamethods, as these strings must be internalized;
|
||||||
|
** #("function") = 8, #("__newindex") = 10.)
|
||||||
|
*/
|
||||||
|
#if !defined(LUAI_MAXSHORTLEN)
|
||||||
|
#define LUAI_MAXSHORTLEN 40
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
/* minimum size for the string table (must be power of 2) */
|
** Initial size for the string table (must be power of 2).
|
||||||
|
** The Lua core alone registers ~50 strings (reserved words +
|
||||||
|
** metaevent keys + a few others). Libraries would typically add
|
||||||
|
** a few dozens more.
|
||||||
|
*/
|
||||||
#if !defined(MINSTRTABSIZE)
|
#if !defined(MINSTRTABSIZE)
|
||||||
#define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */
|
#define MINSTRTABSIZE 128
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Size of cache for strings in the API (better be a prime)
|
||||||
|
*/
|
||||||
|
#if !defined(STRCACHE_SIZE)
|
||||||
|
#define STRCACHE_SIZE 127
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -173,13 +197,21 @@ typedef lu_int32 Instruction;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macros that are executed whenether program enters the Lua core
|
||||||
|
** ('lua_lock') and leaves the core ('lua_unlock')
|
||||||
|
*/
|
||||||
#if !defined(lua_lock)
|
#if !defined(lua_lock)
|
||||||
#define lua_lock(L) ((void) 0)
|
#define lua_lock(L) ((void) 0)
|
||||||
#define lua_unlock(L) ((void) 0)
|
#define lua_unlock(L) ((void) 0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macro executed during Lua functions at points where the
|
||||||
|
** function can yield.
|
||||||
|
*/
|
||||||
#if !defined(luai_threadyield)
|
#if !defined(luai_threadyield)
|
||||||
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -205,15 +237,62 @@ typedef lu_int32 Instruction;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(luai_userstateresume)
|
#if !defined(luai_userstateresume)
|
||||||
#define luai_userstateresume(L,n) ((void)L)
|
#define luai_userstateresume(L,n) ((void)L)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(luai_userstateyield)
|
#if !defined(luai_userstateyield)
|
||||||
#define luai_userstateyield(L,n) ((void)L)
|
#define luai_userstateyield(L,n) ((void)L)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** The luai_num* macros define the primitive operations over numbers.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* floor division (defined as 'floor(a/b)') */
|
||||||
|
#if !defined(luai_numidiv)
|
||||||
|
#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* float division */
|
||||||
|
#if !defined(luai_numdiv)
|
||||||
|
#define luai_numdiv(L,a,b) ((a)/(b))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
|
||||||
|
** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
|
||||||
|
** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
|
||||||
|
** ~= floor(a/b)'. That happens when the division has a non-integer
|
||||||
|
** negative result, which is equivalent to the test below.
|
||||||
|
*/
|
||||||
|
#if !defined(luai_nummod)
|
||||||
|
#define luai_nummod(L,a,b,m) \
|
||||||
|
{ (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* exponentiation */
|
||||||
|
#if !defined(luai_numpow)
|
||||||
|
#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* the others are quite standard operations */
|
||||||
|
#if !defined(luai_numadd)
|
||||||
|
#define luai_numadd(L,a,b) ((a)+(b))
|
||||||
|
#define luai_numsub(L,a,b) ((a)-(b))
|
||||||
|
#define luai_nummul(L,a,b) ((a)*(b))
|
||||||
|
#define luai_numunm(L,a) (-(a))
|
||||||
|
#define luai_numeq(a,b) ((a)==(b))
|
||||||
|
#define luai_numlt(a,b) ((a)<(b))
|
||||||
|
#define luai_numle(a,b) ((a)<=(b))
|
||||||
|
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** macro to control inclusion of some hard tests on stack reallocation
|
** macro to control inclusion of some hard tests on stack reallocation
|
||||||
*/
|
*/
|
||||||
|
|||||||
25
lmathlib.c
25
lmathlib.c
@@ -1,16 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmathlib.c,v 1.109 2014/10/01 11:54:56 roberto Exp $
|
** $Id: lmathlib.c,v 1.114 2014/12/27 20:32:26 roberto Exp roberto $
|
||||||
** Standard mathematical library
|
** Standard mathematical library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lmathlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#define lmathlib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -25,9 +27,11 @@
|
|||||||
#if defined(LUA_USE_POSIX)
|
#if defined(LUA_USE_POSIX)
|
||||||
#define l_rand() random()
|
#define l_rand() random()
|
||||||
#define l_srand(x) srandom(x)
|
#define l_srand(x) srandom(x)
|
||||||
|
#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */
|
||||||
#else
|
#else
|
||||||
#define l_rand() rand()
|
#define l_rand() rand()
|
||||||
#define l_srand(x) srand(x)
|
#define l_srand(x) srand(x)
|
||||||
|
#define L_RANDMAX RAND_MAX
|
||||||
#endif
|
#endif
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
@@ -91,7 +95,7 @@ static int math_toint (lua_State *L) {
|
|||||||
|
|
||||||
static void pushnumint (lua_State *L, lua_Number d) {
|
static void pushnumint (lua_State *L, lua_Number d) {
|
||||||
lua_Integer n;
|
lua_Integer n;
|
||||||
if (lua_numtointeger(d, &n)) /* does 'd' fit in an integer? */
|
if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
|
||||||
lua_pushinteger(L, n); /* result is integer */
|
lua_pushinteger(L, n); /* result is integer */
|
||||||
else
|
else
|
||||||
lua_pushnumber(L, d); /* result is float */
|
lua_pushnumber(L, d); /* result is float */
|
||||||
@@ -179,6 +183,9 @@ static int math_log (lua_State *L) {
|
|||||||
res = l_mathop(log)(x);
|
res = l_mathop(log)(x);
|
||||||
else {
|
else {
|
||||||
lua_Number base = luaL_checknumber(L, 2);
|
lua_Number base = luaL_checknumber(L, 2);
|
||||||
|
#if !defined(LUA_USE_C89)
|
||||||
|
if (base == 2.0) res = l_mathop(log2)(x); else
|
||||||
|
#endif
|
||||||
if (base == 10.0) res = l_mathop(log10)(x);
|
if (base == 10.0) res = l_mathop(log10)(x);
|
||||||
else res = l_mathop(log)(x)/l_mathop(log)(base);
|
else res = l_mathop(log)(x)/l_mathop(log)(base);
|
||||||
}
|
}
|
||||||
@@ -231,12 +238,12 @@ static int math_max (lua_State *L) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** This function uses 'double' (instead of 'lua_Number') to ensure that
|
** This function uses 'double' (instead of 'lua_Number') to ensure that
|
||||||
** all bits from 'l_rand' can be represented, and that 'RAND_MAX + 1.0'
|
** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0'
|
||||||
** will keep full precision (ensuring that 'r' is always less than 1.0.)
|
** will keep full precision (ensuring that 'r' is always less than 1.0.)
|
||||||
*/
|
*/
|
||||||
static int math_random (lua_State *L) {
|
static int math_random (lua_State *L) {
|
||||||
lua_Integer low, up;
|
lua_Integer low, up;
|
||||||
double r = (double)l_rand() * (1.0 / ((double)RAND_MAX + 1.0));
|
double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
|
||||||
switch (lua_gettop(L)) { /* check number of arguments */
|
switch (lua_gettop(L)) { /* check number of arguments */
|
||||||
case 0: { /* no arguments */
|
case 0: { /* no arguments */
|
||||||
lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
|
lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
|
||||||
@@ -256,8 +263,8 @@ static int math_random (lua_State *L) {
|
|||||||
}
|
}
|
||||||
/* random integer in the interval [low, up] */
|
/* random integer in the interval [low, up] */
|
||||||
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
||||||
luaL_argcheck(L, (lua_Unsigned)up - low <= (lua_Unsigned)LUA_MAXINTEGER,
|
luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
|
||||||
1, "interval too large");
|
"interval too large");
|
||||||
r *= (double)(up - low) + 1.0;
|
r *= (double)(up - low) + 1.0;
|
||||||
lua_pushinteger(L, (lua_Integer)r + low);
|
lua_pushinteger(L, (lua_Integer)r + low);
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
27
lmem.c
27
lmem.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmem.c,v 1.85 2014/06/26 18:29:05 roberto Exp roberto $
|
** $Id: lmem.c,v 1.90 2015/03/03 18:18:29 roberto Exp roberto $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#define lmem_c
|
#define lmem_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -24,15 +26,15 @@
|
|||||||
/*
|
/*
|
||||||
** About the realloc function:
|
** About the realloc function:
|
||||||
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||||
** (`osize' is the old size, `nsize' is the new size)
|
** ('osize' is the old size, 'nsize' is the new size)
|
||||||
**
|
**
|
||||||
** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
|
** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
|
||||||
** matter 'x').
|
** matter 'x').
|
||||||
**
|
**
|
||||||
** * frealloc(ud, p, x, 0) frees the block `p'
|
** * frealloc(ud, p, x, 0) frees the block 'p'
|
||||||
** (in this specific case, frealloc must return NULL);
|
** (in this specific case, frealloc must return NULL);
|
||||||
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
||||||
** (which is equivalent to free(NULL) in ANSI C)
|
** (which is equivalent to free(NULL) in ISO C)
|
||||||
**
|
**
|
||||||
** frealloc returns NULL if it cannot create or reallocate the area
|
** frealloc returns NULL if it cannot create or reallocate the area
|
||||||
** (any reallocation to an equal or smaller size cannot fail!)
|
** (any reallocation to an equal or smaller size cannot fail!)
|
||||||
@@ -83,10 +85,11 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
|||||||
#endif
|
#endif
|
||||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
||||||
if (newblock == NULL && nsize > 0) {
|
if (newblock == NULL && nsize > 0) {
|
||||||
api_check( nsize > realosize,
|
lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
|
||||||
"realloc cannot fail when shrinking a block");
|
if (g->version) { /* is state fully built? */
|
||||||
luaC_fullgc(L, 1); /* try to free some memory... */
|
luaC_fullgc(L, 1); /* try to free some memory... */
|
||||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
||||||
|
}
|
||||||
if (newblock == NULL)
|
if (newblock == NULL)
|
||||||
luaD_throw(L, LUA_ERRMEM);
|
luaD_throw(L, LUA_ERRMEM);
|
||||||
}
|
}
|
||||||
|
|||||||
8
lmem.h
8
lmem.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmem.h,v 1.40 2013/02/20 14:08:21 roberto Exp roberto $
|
** $Id: lmem.h,v 1.42 2014/12/19 13:45:40 roberto Exp roberto $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -32,6 +32,12 @@
|
|||||||
? luaM_toobig(L) : cast_void(0)) , \
|
? luaM_toobig(L) : cast_void(0)) , \
|
||||||
luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
|
luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Arrays of chars do not need any test
|
||||||
|
*/
|
||||||
|
#define luaM_reallocvchar(L,b,on,n) \
|
||||||
|
cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
|
||||||
|
|
||||||
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
|
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
|
||||||
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
||||||
#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
|
#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
|
||||||
|
|||||||
72
loadlib.c
72
loadlib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: loadlib.c,v 1.116 2014/07/29 16:01:00 roberto Exp roberto $
|
** $Id: loadlib.c,v 1.125 2015/01/13 13:18:04 roberto Exp roberto $
|
||||||
** Dynamic library loader for Lua
|
** Dynamic library loader for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
**
|
**
|
||||||
@@ -8,22 +8,16 @@
|
|||||||
** systems.
|
** systems.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** if needed, includes windows header before everything else
|
|
||||||
*/
|
|
||||||
#if defined(_WIN32)
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define loadlib_c
|
#define loadlib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -92,8 +86,11 @@
|
|||||||
#define LUA_OFSEP "_"
|
#define LUA_OFSEP "_"
|
||||||
|
|
||||||
|
|
||||||
/* table (in the registry) that keeps handles for all loaded C libraries */
|
/*
|
||||||
#define CLIBS "_CLIBS"
|
** unique key for table in the registry that keeps handles
|
||||||
|
** for all loaded C libraries
|
||||||
|
*/
|
||||||
|
static const int CLIBS = 0;
|
||||||
|
|
||||||
#define LIB_FAIL "open"
|
#define LIB_FAIL "open"
|
||||||
|
|
||||||
@@ -127,7 +124,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_DLOPEN)
|
#if defined(LUA_USE_DLOPEN) /* { */
|
||||||
/*
|
/*
|
||||||
** {========================================================================
|
** {========================================================================
|
||||||
** This is an implementation of loadlib based on the dlfcn interface.
|
** This is an implementation of loadlib based on the dlfcn interface.
|
||||||
@@ -139,6 +136,18 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
|||||||
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
||||||
|
** is undefined according to ISO C, but POSIX assumes that it works.
|
||||||
|
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
||||||
|
*/
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#define cast_func(p) (__extension__ (lua_CFunction)(p))
|
||||||
|
#else
|
||||||
|
#define cast_func(p) ((lua_CFunction)(p))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static void lsys_unloadlib (void *lib) {
|
static void lsys_unloadlib (void *lib) {
|
||||||
dlclose(lib);
|
dlclose(lib);
|
||||||
}
|
}
|
||||||
@@ -152,7 +161,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
|||||||
|
|
||||||
|
|
||||||
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||||
lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
|
lua_CFunction f = cast_func(dlsym(lib, sym));
|
||||||
if (f == NULL) lua_pushstring(L, dlerror());
|
if (f == NULL) lua_pushstring(L, dlerror());
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@@ -161,13 +170,15 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#elif defined(LUA_DL_DLL)
|
#elif defined(LUA_DL_DLL) /* }{ */
|
||||||
/*
|
/*
|
||||||
** {======================================================================
|
** {======================================================================
|
||||||
** This is an implementation of loadlib for Windows using native functions.
|
** This is an implementation of loadlib for Windows using native functions.
|
||||||
** =======================================================================
|
** =======================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
#undef setprogdir
|
#undef setprogdir
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -225,7 +236,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
#else
|
#else /* }{ */
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {======================================================
|
||||||
** Fallback for other systems
|
** Fallback for other systems
|
||||||
@@ -258,7 +269,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
#endif
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -266,7 +277,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
*/
|
*/
|
||||||
static void *checkclib (lua_State *L, const char *path) {
|
static void *checkclib (lua_State *L, const char *path) {
|
||||||
void *plib;
|
void *plib;
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||||
lua_getfield(L, -1, path);
|
lua_getfield(L, -1, path);
|
||||||
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||||
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
||||||
@@ -279,7 +290,7 @@ static void *checkclib (lua_State *L, const char *path) {
|
|||||||
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
||||||
*/
|
*/
|
||||||
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||||
lua_pushlightuserdata(L, plib);
|
lua_pushlightuserdata(L, plib);
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||||
@@ -520,8 +531,8 @@ static void findloader (lua_State *L, const char *name) {
|
|||||||
int i;
|
int i;
|
||||||
luaL_Buffer msg; /* to build error message */
|
luaL_Buffer msg; /* to build error message */
|
||||||
luaL_buffinit(L, &msg);
|
luaL_buffinit(L, &msg);
|
||||||
lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */
|
/* push 'package.searchers' to index 3 in the stack */
|
||||||
if (!lua_istable(L, 3))
|
if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
|
||||||
luaL_error(L, "'package.searchers' must be a table");
|
luaL_error(L, "'package.searchers' must be a table");
|
||||||
/* iterate over available searchers to find a loader */
|
/* iterate over available searchers to find a loader */
|
||||||
for (i = 1; ; i++) {
|
for (i = 1; ; i++) {
|
||||||
@@ -729,7 +740,7 @@ static void createsearcherstable (lua_State *L) {
|
|||||||
}
|
}
|
||||||
#if defined(LUA_COMPAT_LOADERS)
|
#if defined(LUA_COMPAT_LOADERS)
|
||||||
lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
|
lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
|
||||||
lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */
|
lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */
|
||||||
#endif
|
#endif
|
||||||
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
||||||
}
|
}
|
||||||
@@ -740,17 +751,18 @@ static void createsearcherstable (lua_State *L) {
|
|||||||
** setting a finalizer to close all libraries when closing state.
|
** setting a finalizer to close all libraries when closing state.
|
||||||
*/
|
*/
|
||||||
static void createclibstable (lua_State *L) {
|
static void createclibstable (lua_State *L) {
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
lua_newtable(L); /* create CLIBS table */
|
||||||
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
||||||
lua_pushcfunction(L, gctm);
|
lua_pushcfunction(L, gctm);
|
||||||
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||||
lua_setmetatable(L, -2);
|
lua_setmetatable(L, -2);
|
||||||
|
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||||
createclibstable(L);
|
createclibstable(L);
|
||||||
luaL_newlib(L, pk_funcs); /* create `package' table */
|
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
||||||
createsearcherstable(L);
|
createsearcherstable(L);
|
||||||
/* set field 'path' */
|
/* set field 'path' */
|
||||||
setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT);
|
setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT);
|
||||||
@@ -760,10 +772,10 @@ LUAMOD_API int luaopen_package (lua_State *L) {
|
|||||||
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
|
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
|
||||||
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
|
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
|
||||||
lua_setfield(L, -2, "config");
|
lua_setfield(L, -2, "config");
|
||||||
/* set field `loaded' */
|
/* set field 'loaded' */
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||||
lua_setfield(L, -2, "loaded");
|
lua_setfield(L, -2, "loaded");
|
||||||
/* set field `preload' */
|
/* set field 'preload' */
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||||
lua_setfield(L, -2, "preload");
|
lua_setfield(L, -2, "preload");
|
||||||
lua_pushglobaltable(L);
|
lua_pushglobaltable(L);
|
||||||
|
|||||||
107
lobject.c
107
lobject.c
@@ -1,17 +1,22 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lobject.c,v 2.92 2014/10/10 22:23:04 roberto Exp roberto $
|
** $Id: lobject.c,v 2.103 2015/03/28 19:14:47 roberto Exp roberto $
|
||||||
** Some generic functions over Lua objects
|
** Some generic functions over Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lobject_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <locale.h>
|
||||||
|
#include <math.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lobject_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lctype.h"
|
#include "lctype.h"
|
||||||
@@ -36,8 +41,12 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
|||||||
int luaO_int2fb (unsigned int x) {
|
int luaO_int2fb (unsigned int x) {
|
||||||
int e = 0; /* exponent */
|
int e = 0; /* exponent */
|
||||||
if (x < 8) return x;
|
if (x < 8) return x;
|
||||||
while (x >= 0x10) {
|
while (x >= (8 << 4)) { /* coarse steps */
|
||||||
x = (x+1) >> 1;
|
x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
|
||||||
|
e += 4;
|
||||||
|
}
|
||||||
|
while (x >= (8 << 1)) { /* fine steps */
|
||||||
|
x = (x + 1) >> 1; /* x = ceil(x / 2) */
|
||||||
e++;
|
e++;
|
||||||
}
|
}
|
||||||
return ((e+1) << 3) | (cast_int(x) - 8);
|
return ((e+1) << 3) | (cast_int(x) - 8);
|
||||||
@@ -52,8 +61,11 @@ int luaO_fb2int (int x) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Computes ceil(log2(x))
|
||||||
|
*/
|
||||||
int luaO_ceillog2 (unsigned int x) {
|
int luaO_ceillog2 (unsigned int x) {
|
||||||
static const lu_byte log_2[256] = {
|
static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
|
||||||
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||||
@@ -98,6 +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_OPMUL: return luai_nummul(L, v1, v2);
|
||||||
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
|
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
|
||||||
case LUA_OPPOW: return luai_numpow(L, v1, v2);
|
case LUA_OPPOW: return luai_numpow(L, v1, v2);
|
||||||
|
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
|
||||||
case LUA_OPUNM: return luai_numunm(L, v1);
|
case LUA_OPUNM: return luai_numunm(L, v1);
|
||||||
case LUA_OPMOD: {
|
case LUA_OPMOD: {
|
||||||
lua_Number m;
|
lua_Number m;
|
||||||
@@ -112,9 +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,
|
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
||||||
TValue *res) {
|
TValue *res) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR:
|
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
|
||||||
case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR:
|
case LUA_OPSHL: case LUA_OPSHR:
|
||||||
case LUA_OPBNOT: { /* operates only on integers */
|
case LUA_OPBNOT: { /* operate only on integers */
|
||||||
lua_Integer i1; lua_Integer i2;
|
lua_Integer i1; lua_Integer i2;
|
||||||
if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
|
if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
|
||||||
setivalue(res, intarith(L, op, i1, i2));
|
setivalue(res, intarith(L, op, i1, i2));
|
||||||
@@ -145,13 +158,13 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
|||||||
}
|
}
|
||||||
/* could not perform raw operation; try metamethod */
|
/* could not perform raw operation; try metamethod */
|
||||||
lua_assert(L != NULL); /* should not fail when folding (compile time) */
|
lua_assert(L != NULL); /* should not fail when folding (compile time) */
|
||||||
luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD));
|
luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaO_hexavalue (int c) {
|
int luaO_hexavalue (int c) {
|
||||||
if (lisdigit(c)) return c - '0';
|
if (lisdigit(c)) return c - '0';
|
||||||
else return ltolower(c) - 'a' + 10;
|
else return (ltolower(c) - 'a') + 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -162,26 +175,14 @@ static int isneg (const char **s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {==================================================================
|
||||||
** lua_strx2number converts an hexadecimal numeric string to a number.
|
** Lua's implementation for 'lua_strx2number'
|
||||||
** In C99, 'strtod' does both conversions. C89, however, has no function
|
** ===================================================================
|
||||||
** to convert floating hexadecimal strings to numbers. For these
|
|
||||||
** systems, you can leave 'lua_strx2number' undefined and Lua will
|
|
||||||
** provide its own implementation.
|
|
||||||
** =======================================================
|
|
||||||
*/
|
*/
|
||||||
#if !defined(lua_strx2number) /* { */
|
|
||||||
|
|
||||||
#if defined(LUA_USE_C99) /* { */
|
#if !defined(lua_strx2number)
|
||||||
|
|
||||||
#define lua_strx2number(s,p) lua_str2number(s,p)
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
|
|
||||||
/* Lua's implementation for 'lua_strx2number' */
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
/* maximum number of significant digits to read (to avoid overflows
|
/* maximum number of significant digits to read (to avoid overflows
|
||||||
even with single floats) */
|
even with single floats) */
|
||||||
@@ -192,35 +193,30 @@ static int isneg (const char **s) {
|
|||||||
** C99 specification for 'strtod'
|
** C99 specification for 'strtod'
|
||||||
*/
|
*/
|
||||||
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||||
|
int dot = lua_getlocaledecpoint();
|
||||||
lua_Number r = 0.0; /* result (accumulator) */
|
lua_Number r = 0.0; /* result (accumulator) */
|
||||||
int sigdig = 0; /* number of significant digits */
|
int sigdig = 0; /* number of significant digits */
|
||||||
int nosigdig = 0; /* number of non-significant digits */
|
int nosigdig = 0; /* number of non-significant digits */
|
||||||
int e = 0; /* exponent correction */
|
int e = 0; /* exponent correction */
|
||||||
int neg = 0; /* 1 if number is negative */
|
int neg; /* 1 if number is negative */
|
||||||
int dot = 0; /* true after seen a dot */
|
int hasdot = 0; /* true after seen a dot */
|
||||||
*endptr = cast(char *, s); /* nothing is valid yet */
|
*endptr = cast(char *, s); /* nothing is valid yet */
|
||||||
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
||||||
neg = isneg(&s); /* check signal */
|
neg = isneg(&s); /* check signal */
|
||||||
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
||||||
return 0.0; /* invalid format (no '0x') */
|
return 0.0; /* invalid format (no '0x') */
|
||||||
for (s += 2; ; s++) { /* skip '0x' and read numeral */
|
for (s += 2; ; s++) { /* skip '0x' and read numeral */
|
||||||
if (*s == '.') {
|
if (*s == dot) {
|
||||||
if (dot) break; /* second dot? stop loop */
|
if (hasdot) break; /* second dot? stop loop */
|
||||||
else dot = 1;
|
else hasdot = 1;
|
||||||
}
|
}
|
||||||
else if (lisxdigit(cast_uchar(*s))) {
|
else if (lisxdigit(cast_uchar(*s))) {
|
||||||
if (sigdig == 0 && *s == '0') { /* non-significant zero? */
|
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
|
||||||
nosigdig++;
|
nosigdig++;
|
||||||
if (dot) e--; /* zero after dot? correct exponent */
|
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
||||||
}
|
r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
|
||||||
else {
|
else e++; /* too many digits; ignore, but still count for exponent */
|
||||||
if (++sigdig <= MAXSIGDIG) { /* can read it without overflow? */
|
if (hasdot) e--; /* decimal digit? correct exponent */
|
||||||
r = (r * cast_num(16.0)) + luaO_hexavalue(cast_uchar(*s));
|
|
||||||
if (dot) e--; /* decimal digit */
|
|
||||||
}
|
|
||||||
else /* too many digits; ignore */
|
|
||||||
if (!dot) e++; /* still count it for exponent */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else break; /* neither a dot nor a digit */
|
else break; /* neither a dot nor a digit */
|
||||||
}
|
}
|
||||||
@@ -245,10 +241,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
return l_mathop(ldexp)(r, e);
|
return l_mathop(ldexp)(r, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* } */
|
#endif
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -260,7 +253,7 @@ static const char *l_str2d (const char *s, lua_Number *result) {
|
|||||||
*result = lua_strx2number(s, &endptr);
|
*result = lua_strx2number(s, &endptr);
|
||||||
else
|
else
|
||||||
*result = lua_str2number(s, &endptr);
|
*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++;
|
while (lisspace(cast_uchar(*endptr))) endptr++;
|
||||||
return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
|
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[1] == 'x' || s[1] == 'X')) { /* hex? */
|
||||||
s += 2; /* skip '0x' */
|
s += 2; /* skip '0x' */
|
||||||
for (; lisxdigit(cast_uchar(*s)); s++) {
|
for (; lisxdigit(cast_uchar(*s)); s++) {
|
||||||
a = a * 16 + luaO_hexavalue(cast_uchar(*s));
|
a = a * 16 + luaO_hexavalue(*s);
|
||||||
empty = 0;
|
empty = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* decimal */
|
else { /* decimal */
|
||||||
for (; lisdigit(cast_uchar(*s)); s++) {
|
for (; lisdigit(cast_uchar(*s)); s++) {
|
||||||
a = a * 10 + luaO_hexavalue(cast_uchar(*s));
|
a = a * 10 + *s - '0';
|
||||||
empty = 0;
|
empty = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -306,7 +299,7 @@ size_t luaO_str2num (const char *s, TValue *o) {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
return 0; /* conversion failed */
|
return 0; /* conversion failed */
|
||||||
return (e - s + 1); /* success; return string size */
|
return (e - s) + 1; /* success; return string size */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -317,8 +310,8 @@ int luaO_utf8esc (char *buff, unsigned long x) {
|
|||||||
buff[UTF8BUFFSZ - 1] = cast(char, x);
|
buff[UTF8BUFFSZ - 1] = cast(char, x);
|
||||||
else { /* need continuation bytes */
|
else { /* need continuation bytes */
|
||||||
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
||||||
do {
|
do { /* add continuation bytes */
|
||||||
buff[UTF8BUFFSZ - (n++)] = 0x80 | (x & 0x3f); /* add continuation byte */
|
buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
|
||||||
x >>= 6; /* remove added bits */
|
x >>= 6; /* remove added bits */
|
||||||
mfb >>= 1; /* now there is one less bit available in first byte */
|
mfb >>= 1; /* now there is one less bit available in first byte */
|
||||||
} while (x > mfb); /* still needs continuation byte? */
|
} while (x > mfb); /* still needs continuation byte? */
|
||||||
@@ -345,7 +338,7 @@ void luaO_tostring (lua_State *L, StkId obj) {
|
|||||||
len = lua_number2str(buff, fltvalue(obj));
|
len = lua_number2str(buff, fltvalue(obj));
|
||||||
#if !defined(LUA_COMPAT_FLOATSTRING)
|
#if !defined(LUA_COMPAT_FLOATSTRING)
|
||||||
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
||||||
buff[len++] = '.';
|
buff[len++] = lua_getlocaledecpoint();
|
||||||
buff[len++] = '0'; /* adds '.0' to result */
|
buff[len++] = '0'; /* adds '.0' to result */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -399,7 +392,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'p': {
|
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 *));
|
int l = sprintf(buff, "%p", va_arg(argp, void *));
|
||||||
pushstr(L, buff, l);
|
pushstr(L, buff, l);
|
||||||
break;
|
break;
|
||||||
|
|||||||
51
lobject.h
51
lobject.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lobject.h,v 2.102 2014/09/04 18:15:29 roberto Exp roberto $
|
** $Id: lobject.h,v 2.110 2015/04/02 21:10:53 roberto Exp roberto $
|
||||||
** Type definitions for Lua objects
|
** Type definitions for Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -35,8 +35,6 @@
|
|||||||
** bit 6: whether value is collectable
|
** bit 6: whether value is collectable
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define VARBITS (3 << 4)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** LUA_TFUNCTION variants:
|
** LUA_TFUNCTION variants:
|
||||||
@@ -154,6 +152,8 @@ typedef struct lua_TValue TValue;
|
|||||||
/* Macros to access values */
|
/* Macros to access values */
|
||||||
#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
|
#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
|
||||||
#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
|
#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
|
||||||
|
#define nvalue(o) check_exp(ttisnumber(o), \
|
||||||
|
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
|
||||||
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
|
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
|
||||||
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
||||||
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
|
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
|
||||||
@@ -188,9 +188,15 @@ typedef struct lua_TValue TValue;
|
|||||||
#define setfltvalue(obj,x) \
|
#define setfltvalue(obj,x) \
|
||||||
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
|
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
|
||||||
|
|
||||||
|
#define chgfltvalue(obj,x) \
|
||||||
|
{ TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
|
||||||
|
|
||||||
#define setivalue(obj,x) \
|
#define setivalue(obj,x) \
|
||||||
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
|
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
|
||||||
|
|
||||||
|
#define chgivalue(obj,x) \
|
||||||
|
{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
|
||||||
|
|
||||||
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
|
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
|
||||||
|
|
||||||
#define setfvalue(obj,x) \
|
#define setfvalue(obj,x) \
|
||||||
@@ -301,9 +307,12 @@ typedef TValue *StkId; /* index to stack elements */
|
|||||||
typedef struct TString {
|
typedef struct TString {
|
||||||
CommonHeader;
|
CommonHeader;
|
||||||
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
|
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
|
||||||
|
lu_byte shrlen; /* length for short strings */
|
||||||
unsigned int hash;
|
unsigned int hash;
|
||||||
size_t len; /* number of characters in string */
|
union {
|
||||||
struct TString *hnext; /* linked list for hash table */
|
size_t lnglen; /* length for long strings */
|
||||||
|
struct TString *hnext; /* linked list for hash table */
|
||||||
|
} u;
|
||||||
} TString;
|
} TString;
|
||||||
|
|
||||||
|
|
||||||
@@ -327,6 +336,12 @@ typedef union UTString {
|
|||||||
/* get the actual string (array of bytes) from a Lua value */
|
/* get the actual string (array of bytes) from a Lua value */
|
||||||
#define svalue(o) getstr(tsvalue(o))
|
#define svalue(o) getstr(tsvalue(o))
|
||||||
|
|
||||||
|
/* get string length from 'TString *s' */
|
||||||
|
#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
|
||||||
|
|
||||||
|
/* get string length from 'TValue *o' */
|
||||||
|
#define vslen(o) tsslen(tsvalue(o))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Header for userdata; memory area follows the end of this structure
|
** Header for userdata; memory area follows the end of this structure
|
||||||
@@ -345,7 +360,7 @@ typedef struct Udata {
|
|||||||
** Ensures that address after this type is always fully aligned.
|
** Ensures that address after this type is always fully aligned.
|
||||||
*/
|
*/
|
||||||
typedef union UUdata {
|
typedef union UUdata {
|
||||||
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
|
L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
|
||||||
Udata uv;
|
Udata uv;
|
||||||
} UUdata;
|
} UUdata;
|
||||||
|
|
||||||
@@ -359,13 +374,13 @@ typedef union UUdata {
|
|||||||
|
|
||||||
#define setuservalue(L,u,o) \
|
#define setuservalue(L,u,o) \
|
||||||
{ const TValue *io=(o); Udata *iu = (u); \
|
{ const TValue *io=(o); Udata *iu = (u); \
|
||||||
iu->user_ = io->value_; iu->ttuv_ = io->tt_; \
|
iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(G(L),io); }
|
||||||
|
|
||||||
|
|
||||||
#define getuservalue(L,u,o) \
|
#define getuservalue(L,u,o) \
|
||||||
{ TValue *io=(o); const Udata *iu = (u); \
|
{ TValue *io=(o); const Udata *iu = (u); \
|
||||||
io->value_ = iu->user_; io->tt_ = iu->ttuv_; \
|
io->value_ = iu->user_; settt_(io, iu->ttuv_); \
|
||||||
checkliveness(G(L),io); }
|
checkliveness(G(L),io); }
|
||||||
|
|
||||||
|
|
||||||
@@ -374,7 +389,7 @@ typedef union UUdata {
|
|||||||
*/
|
*/
|
||||||
typedef struct Upvaldesc {
|
typedef struct Upvaldesc {
|
||||||
TString *name; /* upvalue name (for debug information) */
|
TString *name; /* upvalue name (for debug information) */
|
||||||
lu_byte instack; /* whether it is in stack */
|
lu_byte instack; /* whether it is in stack (register) */
|
||||||
lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
|
lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
|
||||||
} Upvaldesc;
|
} Upvaldesc;
|
||||||
|
|
||||||
@@ -397,22 +412,22 @@ typedef struct Proto {
|
|||||||
CommonHeader;
|
CommonHeader;
|
||||||
lu_byte numparams; /* number of fixed parameters */
|
lu_byte numparams; /* number of fixed parameters */
|
||||||
lu_byte is_vararg;
|
lu_byte is_vararg;
|
||||||
lu_byte maxstacksize; /* maximum stack used by this function */
|
lu_byte maxstacksize; /* number of registers needed by this function */
|
||||||
int sizeupvalues; /* size of 'upvalues' */
|
int sizeupvalues; /* size of 'upvalues' */
|
||||||
int sizek; /* size of `k' */
|
int sizek; /* size of 'k' */
|
||||||
int sizecode;
|
int sizecode;
|
||||||
int sizelineinfo;
|
int sizelineinfo;
|
||||||
int sizep; /* size of `p' */
|
int sizep; /* size of 'p' */
|
||||||
int sizelocvars;
|
int sizelocvars;
|
||||||
int linedefined;
|
int linedefined;
|
||||||
int lastlinedefined;
|
int lastlinedefined;
|
||||||
TValue *k; /* constants used by the function */
|
TValue *k; /* constants used by the function */
|
||||||
Instruction *code;
|
Instruction *code; /* opcodes */
|
||||||
struct Proto **p; /* functions defined inside the function */
|
struct Proto **p; /* functions defined inside the function */
|
||||||
int *lineinfo; /* map from opcodes to source lines (debug information) */
|
int *lineinfo; /* map from opcodes to source lines (debug information) */
|
||||||
LocVar *locvars; /* information about local variables (debug information) */
|
LocVar *locvars; /* information about local variables (debug information) */
|
||||||
Upvaldesc *upvalues; /* upvalue information */
|
Upvaldesc *upvalues; /* upvalue information */
|
||||||
struct LClosure *cache; /* last created closure with this prototype */
|
struct LClosure *cache; /* last-created closure with this prototype */
|
||||||
TString *source; /* used for debug information */
|
TString *source; /* used for debug information */
|
||||||
GCObject *gclist;
|
GCObject *gclist;
|
||||||
} Proto;
|
} Proto;
|
||||||
@@ -471,7 +486,7 @@ typedef union TKey {
|
|||||||
|
|
||||||
|
|
||||||
/* copy a value into a key without messing up field 'next' */
|
/* copy a value into a key without messing up field 'next' */
|
||||||
#define setkey(L,key,obj) \
|
#define setnodekey(L,key,obj) \
|
||||||
{ TKey *k_=(key); const TValue *io_=(obj); \
|
{ TKey *k_=(key); const TValue *io_=(obj); \
|
||||||
k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
|
k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
|
||||||
(void)L; checkliveness(G(L),io_); }
|
(void)L; checkliveness(G(L),io_); }
|
||||||
@@ -486,8 +501,8 @@ typedef struct Node {
|
|||||||
typedef struct Table {
|
typedef struct Table {
|
||||||
CommonHeader;
|
CommonHeader;
|
||||||
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
|
||||||
lu_byte lsizenode; /* log2 of size of `node' array */
|
lu_byte lsizenode; /* log2 of size of 'node' array */
|
||||||
unsigned int sizearray; /* size of `array' array */
|
unsigned int sizearray; /* size of 'array' array */
|
||||||
TValue *array; /* array part */
|
TValue *array; /* array part */
|
||||||
Node *node;
|
Node *node;
|
||||||
Node *lastfree; /* any free position is before this position */
|
Node *lastfree; /* any free position is before this position */
|
||||||
@@ -498,7 +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) \
|
#define lmod(s,size) \
|
||||||
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
|
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.c,v 1.52 2013/12/18 14:12:03 roberto Exp roberto $
|
** $Id: lopcodes.c,v 1.54 2014/11/02 19:19:04 roberto Exp roberto $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define lopcodes_c
|
#define lopcodes_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lopcodes.h"
|
#include "lopcodes.h"
|
||||||
|
|
||||||
|
|||||||
28
lopcodes.h
28
lopcodes.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.h,v 1.146 2013/12/30 20:47:58 roberto Exp roberto $
|
** $Id: lopcodes.h,v 1.147 2014/10/20 18:29:55 roberto Exp roberto $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,12 +14,12 @@
|
|||||||
We assume that instructions are unsigned numbers.
|
We assume that instructions are unsigned numbers.
|
||||||
All instructions have an opcode in the first 6 bits.
|
All instructions have an opcode in the first 6 bits.
|
||||||
Instructions can have the following fields:
|
Instructions can have the following fields:
|
||||||
`A' : 8 bits
|
'A' : 8 bits
|
||||||
`B' : 9 bits
|
'B' : 9 bits
|
||||||
`C' : 9 bits
|
'C' : 9 bits
|
||||||
'Ax' : 26 bits ('A', 'B', and 'C' together)
|
'Ax' : 26 bits ('A', 'B', and 'C' together)
|
||||||
`Bx' : 18 bits (`B' and `C' together)
|
'Bx' : 18 bits ('B' and 'C' together)
|
||||||
`sBx' : signed Bx
|
'sBx' : signed Bx
|
||||||
|
|
||||||
A signed argument is represented in excess K; that is, the number
|
A signed argument is represented in excess K; that is, the number
|
||||||
value is the unsigned value minus K. K is exactly the maximum value
|
value is the unsigned value minus K. K is exactly the maximum value
|
||||||
@@ -58,7 +58,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
|
|||||||
*/
|
*/
|
||||||
#if SIZE_Bx < LUAI_BITSINT-1
|
#if SIZE_Bx < LUAI_BITSINT-1
|
||||||
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
||||||
#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
|
#define MAXARG_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
|
||||||
#else
|
#else
|
||||||
#define MAXARG_Bx MAX_INT
|
#define MAXARG_Bx MAX_INT
|
||||||
#define MAXARG_sBx MAX_INT
|
#define MAXARG_sBx MAX_INT
|
||||||
@@ -76,10 +76,10 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
|
|||||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||||
|
|
||||||
|
|
||||||
/* creates a mask with `n' 1 bits at position `p' */
|
/* creates a mask with 'n' 1 bits at position 'p' */
|
||||||
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
|
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
|
||||||
|
|
||||||
/* creates a mask with `n' 0 bits at position `p' */
|
/* creates a mask with 'n' 0 bits at position 'p' */
|
||||||
#define MASK0(n,p) (~MASK1(n,p))
|
#define MASK0(n,p) (~MASK1(n,p))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -238,16 +238,16 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
|||||||
|
|
||||||
/*===========================================================================
|
/*===========================================================================
|
||||||
Notes:
|
Notes:
|
||||||
(*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is
|
(*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
|
||||||
set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
|
set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
|
||||||
OP_SETLIST) may use `top'.
|
OP_SETLIST) may use 'top'.
|
||||||
|
|
||||||
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
|
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
|
||||||
set top (like in OP_CALL with C == 0).
|
set top (like in OP_CALL with C == 0).
|
||||||
|
|
||||||
(*) In OP_RETURN, if (B == 0) then return up to `top'.
|
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
||||||
|
|
||||||
(*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
|
(*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
|
||||||
'instruction' is EXTRAARG(real C).
|
'instruction' is EXTRAARG(real C).
|
||||||
|
|
||||||
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
||||||
@@ -255,7 +255,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
|||||||
(*) For comparisons, A specifies what condition the test should accept
|
(*) For comparisons, A specifies what condition the test should accept
|
||||||
(true or false).
|
(true or false).
|
||||||
|
|
||||||
(*) All `skips' (pc++) assume that next instruction is a jump.
|
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
||||||
|
|
||||||
===========================================================================*/
|
===========================================================================*/
|
||||||
|
|
||||||
|
|||||||
92
loslib.c
92
loslib.c
@@ -1,9 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: loslib.c,v 1.48 2014/10/08 19:57:31 roberto Exp roberto $
|
** $Id: loslib.c,v 1.56 2015/02/09 17:41:54 roberto Exp roberto $
|
||||||
** Standard Operating System library
|
** Standard Operating System library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define loslib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
@@ -11,23 +16,22 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define loslib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
|
||||||
/*
|
/*
|
||||||
|
** {==================================================================
|
||||||
** list of valid conversion specifiers for the 'strftime' function
|
** list of valid conversion specifiers for the 'strftime' function
|
||||||
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
||||||
|
|
||||||
#if !defined(LUA_USE_POSIX)
|
#if defined(LUA_USE_C89)
|
||||||
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
|
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
|
||||||
#else
|
#else /* C99 specification */
|
||||||
#define LUA_STRFTIMEOPTIONS \
|
#define LUA_STRFTIMEOPTIONS \
|
||||||
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
|
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
|
||||||
"E", "cCxXyY", \
|
"E", "cCxXyY", \
|
||||||
@@ -35,8 +39,14 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Configuration for time-related stuff
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
#if !defined(l_time_t) /* { */
|
#if !defined(l_time_t) /* { */
|
||||||
/*
|
/*
|
||||||
@@ -49,15 +59,41 @@
|
|||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(l_gmtime) /* { */
|
||||||
#if !defined(lua_tmpnam) /* { */
|
|
||||||
/*
|
/*
|
||||||
** By default, Lua uses tmpnam except when POSIX is available, where it
|
** By default, Lua uses gmtime/localtime, except when POSIX is available,
|
||||||
** uses mkstemp.
|
** where it uses gmtime_r/localtime_r
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(LUA_USE_POSIX) /* { */
|
#if defined(LUA_USE_POSIX) /* { */
|
||||||
|
|
||||||
|
#define l_gmtime(t,r) gmtime_r(t,r)
|
||||||
|
#define l_localtime(t,r) localtime_r(t,r)
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
|
/* ISO C definitions */
|
||||||
|
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
|
||||||
|
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Configuration for 'tmpnam':
|
||||||
|
** By default, Lua uses tmpnam except when POSIX is available, where
|
||||||
|
** it uses mkstemp.
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
#if !defined(lua_tmpnam) /* { */
|
||||||
|
|
||||||
|
#if defined(LUA_USE_POSIX) /* { */
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define LUA_TMPNAMBUFSIZE 32
|
#define LUA_TMPNAMBUFSIZE 32
|
||||||
@@ -74,38 +110,17 @@
|
|||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definitions */
|
/* ISO C definitions */
|
||||||
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
||||||
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if !defined(l_gmtime) /* { */
|
|
||||||
/*
|
|
||||||
** By default, Lua uses gmtime/localtime, except when POSIX is available,
|
|
||||||
** where it uses gmtime_r/localtime_r
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if defined(LUA_USE_POSIX) /* { */
|
|
||||||
|
|
||||||
#define l_gmtime(t,r) gmtime_r(t,r)
|
|
||||||
#define l_localtime(t,r) localtime_r(t,r)
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
|
|
||||||
/* ANSI definitions */
|
|
||||||
#define l_gmtime(t,r) ((void)r, gmtime(t))
|
|
||||||
#define l_localtime(t,r) ((void)r, localtime(t))
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int os_execute (lua_State *L) {
|
static int os_execute (lua_State *L) {
|
||||||
const char *cmd = luaL_optstring(L, 1, NULL);
|
const char *cmd = luaL_optstring(L, 1, NULL);
|
||||||
@@ -227,7 +242,7 @@ static int os_date (lua_State *L) {
|
|||||||
struct tm tmr, *stm;
|
struct tm tmr, *stm;
|
||||||
if (*s == '!') { /* UTC? */
|
if (*s == '!') { /* UTC? */
|
||||||
stm = l_gmtime(&t, &tmr);
|
stm = l_gmtime(&t, &tmr);
|
||||||
s++; /* skip `!' */
|
s++; /* skip '!' */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
stm = l_localtime(&t, &tmr);
|
stm = l_localtime(&t, &tmr);
|
||||||
@@ -285,7 +300,7 @@ static int os_time (lua_State *L) {
|
|||||||
t = mktime(&ts);
|
t = mktime(&ts);
|
||||||
}
|
}
|
||||||
if (t != (time_t)(l_timet)t)
|
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))
|
else if (t == (time_t)(-1))
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
else
|
else
|
||||||
@@ -295,8 +310,9 @@ static int os_time (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int os_difftime (lua_State *L) {
|
static int os_difftime (lua_State *L) {
|
||||||
double res = difftime((l_checktime(L, 1)), (l_checktime(L, 2)));
|
time_t t1 = l_checktime(L, 1);
|
||||||
lua_pushnumber(L, (lua_Number)res);
|
time_t t2 = l_checktime(L, 2);
|
||||||
|
lua_pushnumber(L, (lua_Number)difftime(t1, t2));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
78
lparser.c
78
lparser.c
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.c,v 2.142 2014/07/21 16:02:10 roberto Exp roberto $
|
** $Id: lparser.c,v 2.146 2014/11/27 18:41:43 roberto Exp roberto $
|
||||||
** Lua Parser
|
** Lua Parser
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lparser_c
|
#define lparser_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lcode.h"
|
#include "lcode.h"
|
||||||
@@ -45,11 +47,11 @@
|
|||||||
*/
|
*/
|
||||||
typedef struct BlockCnt {
|
typedef struct BlockCnt {
|
||||||
struct BlockCnt *previous; /* chain */
|
struct BlockCnt *previous; /* chain */
|
||||||
short firstlabel; /* index of first label in this block */
|
int firstlabel; /* index of first label in this block */
|
||||||
short firstgoto; /* index of first pending goto in this block */
|
int firstgoto; /* index of first pending goto in this block */
|
||||||
lu_byte nactvar; /* # active locals outside the block */
|
lu_byte nactvar; /* # active locals outside the block */
|
||||||
lu_byte upval; /* true if some variable in the block is an upvalue */
|
lu_byte upval; /* true if some variable in the block is an upvalue */
|
||||||
lu_byte isloop; /* true if `block' is a loop */
|
lu_byte isloop; /* true if 'block' is a loop */
|
||||||
} BlockCnt;
|
} BlockCnt;
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +65,7 @@ static void expr (LexState *ls, expdesc *v);
|
|||||||
|
|
||||||
/* semantic error */
|
/* semantic error */
|
||||||
static l_noret semerror (LexState *ls, const char *msg) {
|
static l_noret semerror (LexState *ls, const char *msg) {
|
||||||
ls->t.token = 0; /* remove 'near to' from final message */
|
ls->t.token = 0; /* remove "near <token>" from final message */
|
||||||
luaX_syntaxerror(ls, msg);
|
luaX_syntaxerror(ls, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -384,7 +386,7 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name,
|
|||||||
l->arr[n].line = line;
|
l->arr[n].line = line;
|
||||||
l->arr[n].nactvar = ls->fs->nactvar;
|
l->arr[n].nactvar = ls->fs->nactvar;
|
||||||
l->arr[n].pc = pc;
|
l->arr[n].pc = pc;
|
||||||
l->n++;
|
l->n = n + 1;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,7 +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
|
** outer labels; if the block being exited has upvalues, and
|
||||||
** the goto exits the scope of any variable (which can be the
|
** the goto exits the scope of any variable (which can be the
|
||||||
** upvalue), close those variables being exited.
|
** upvalue), close those variables being exited.
|
||||||
@@ -442,7 +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) {
|
static void breaklabel (LexState *ls) {
|
||||||
TString *n = luaS_new(ls->L, "break");
|
TString *n = luaS_new(ls->L, "break");
|
||||||
@@ -574,7 +576,7 @@ static void close_func (LexState *ls) {
|
|||||||
/*
|
/*
|
||||||
** check whether current token is in the follow set of a block.
|
** check whether current token is in the follow set of a block.
|
||||||
** 'until' closes syntactical blocks, but do not close scope,
|
** 'until' closes syntactical blocks, but do not close scope,
|
||||||
** so it handled in separate.
|
** so it is handled in separate.
|
||||||
*/
|
*/
|
||||||
static int block_follow (LexState *ls, int withuntil) {
|
static int block_follow (LexState *ls, int withuntil) {
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
@@ -588,7 +590,7 @@ static int block_follow (LexState *ls, int withuntil) {
|
|||||||
|
|
||||||
|
|
||||||
static void statlist (LexState *ls) {
|
static void statlist (LexState *ls) {
|
||||||
/* statlist -> { stat [`;'] } */
|
/* statlist -> { stat [';'] } */
|
||||||
while (!block_follow(ls, 1)) {
|
while (!block_follow(ls, 1)) {
|
||||||
if (ls->t.token == TK_RETURN) {
|
if (ls->t.token == TK_RETURN) {
|
||||||
statement(ls);
|
statement(ls);
|
||||||
@@ -629,14 +631,14 @@ static void yindex (LexState *ls, expdesc *v) {
|
|||||||
struct ConsControl {
|
struct ConsControl {
|
||||||
expdesc v; /* last list item read */
|
expdesc v; /* last list item read */
|
||||||
expdesc *t; /* table descriptor */
|
expdesc *t; /* table descriptor */
|
||||||
int nh; /* total number of `record' elements */
|
int nh; /* total number of 'record' elements */
|
||||||
int na; /* total number of array elements */
|
int na; /* total number of array elements */
|
||||||
int tostore; /* number of array elements pending to be stored */
|
int tostore; /* number of array elements pending to be stored */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void recfield (LexState *ls, struct ConsControl *cc) {
|
static void recfield (LexState *ls, struct ConsControl *cc) {
|
||||||
/* recfield -> (NAME | `['exp1`]') = exp1 */
|
/* recfield -> (NAME | '['exp1']') = exp1 */
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
int reg = ls->fs->freereg;
|
int reg = ls->fs->freereg;
|
||||||
expdesc key, val;
|
expdesc key, val;
|
||||||
@@ -743,12 +745,12 @@ static void constructor (LexState *ls, expdesc *t) {
|
|||||||
|
|
||||||
|
|
||||||
static void parlist (LexState *ls) {
|
static void parlist (LexState *ls) {
|
||||||
/* parlist -> [ param { `,' param } ] */
|
/* parlist -> [ param { ',' param } ] */
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
Proto *f = fs->f;
|
Proto *f = fs->f;
|
||||||
int nparams = 0;
|
int nparams = 0;
|
||||||
f->is_vararg = 0;
|
f->is_vararg = 0;
|
||||||
if (ls->t.token != ')') { /* is `parlist' not empty? */
|
if (ls->t.token != ')') { /* is 'parlist' not empty? */
|
||||||
do {
|
do {
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
case TK_NAME: { /* param -> NAME */
|
case TK_NAME: { /* param -> NAME */
|
||||||
@@ -756,7 +758,7 @@ static void parlist (LexState *ls) {
|
|||||||
nparams++;
|
nparams++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TK_DOTS: { /* param -> `...' */
|
case TK_DOTS: { /* param -> '...' */
|
||||||
luaX_next(ls);
|
luaX_next(ls);
|
||||||
f->is_vararg = 1;
|
f->is_vararg = 1;
|
||||||
break;
|
break;
|
||||||
@@ -772,7 +774,7 @@ static void parlist (LexState *ls) {
|
|||||||
|
|
||||||
|
|
||||||
static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
||||||
/* body -> `(' parlist `)' block END */
|
/* body -> '(' parlist ')' block END */
|
||||||
FuncState new_fs;
|
FuncState new_fs;
|
||||||
BlockCnt bl;
|
BlockCnt bl;
|
||||||
new_fs.f = addprototype(ls);
|
new_fs.f = addprototype(ls);
|
||||||
@@ -794,7 +796,7 @@ static void body (LexState *ls, expdesc *e, int ismethod, int line) {
|
|||||||
|
|
||||||
|
|
||||||
static int explist (LexState *ls, expdesc *v) {
|
static int explist (LexState *ls, expdesc *v) {
|
||||||
/* explist -> expr { `,' expr } */
|
/* explist -> expr { ',' expr } */
|
||||||
int n = 1; /* at least one expression */
|
int n = 1; /* at least one expression */
|
||||||
expr(ls, v);
|
expr(ls, v);
|
||||||
while (testnext(ls, ',')) {
|
while (testnext(ls, ',')) {
|
||||||
@@ -811,7 +813,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
|||||||
expdesc args;
|
expdesc args;
|
||||||
int base, nparams;
|
int base, nparams;
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
case '(': { /* funcargs -> `(' [ explist ] `)' */
|
case '(': { /* funcargs -> '(' [ explist ] ')' */
|
||||||
luaX_next(ls);
|
luaX_next(ls);
|
||||||
if (ls->t.token == ')') /* arg list is empty? */
|
if (ls->t.token == ')') /* arg list is empty? */
|
||||||
args.k = VVOID;
|
args.k = VVOID;
|
||||||
@@ -828,7 +830,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
|||||||
}
|
}
|
||||||
case TK_STRING: { /* funcargs -> STRING */
|
case TK_STRING: { /* funcargs -> STRING */
|
||||||
codestring(ls, &args, ls->t.seminfo.ts);
|
codestring(ls, &args, ls->t.seminfo.ts);
|
||||||
luaX_next(ls); /* must use `seminfo' before `next' */
|
luaX_next(ls); /* must use 'seminfo' before 'next' */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
@@ -894,14 +896,14 @@ static void suffixedexp (LexState *ls, expdesc *v) {
|
|||||||
fieldsel(ls, v);
|
fieldsel(ls, v);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case '[': { /* `[' exp1 `]' */
|
case '[': { /* '[' exp1 ']' */
|
||||||
expdesc key;
|
expdesc key;
|
||||||
luaK_exp2anyregup(fs, v);
|
luaK_exp2anyregup(fs, v);
|
||||||
yindex(ls, &key);
|
yindex(ls, &key);
|
||||||
luaK_indexed(fs, v, &key);
|
luaK_indexed(fs, v, &key);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ':': { /* `:' NAME funcargs */
|
case ':': { /* ':' NAME funcargs */
|
||||||
expdesc key;
|
expdesc key;
|
||||||
luaX_next(ls);
|
luaX_next(ls);
|
||||||
checkname(ls, &key);
|
checkname(ls, &key);
|
||||||
@@ -1035,7 +1037,7 @@ static const struct {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
|
** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
|
||||||
** where `binop' is any binary operator with a priority higher than `limit'
|
** where 'binop' is any binary operator with a priority higher than 'limit'
|
||||||
*/
|
*/
|
||||||
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
||||||
BinOpr op;
|
BinOpr op;
|
||||||
@@ -1049,7 +1051,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
|
|||||||
luaK_prefix(ls->fs, uop, v, line);
|
luaK_prefix(ls->fs, uop, v, line);
|
||||||
}
|
}
|
||||||
else simpleexp(ls, v);
|
else simpleexp(ls, v);
|
||||||
/* expand while operators have priorities higher than `limit' */
|
/* expand while operators have priorities higher than 'limit' */
|
||||||
op = getbinopr(ls->t.token);
|
op = getbinopr(ls->t.token);
|
||||||
while (op != OPR_NOBINOPR && priority[op].left > limit) {
|
while (op != OPR_NOBINOPR && priority[op].left > limit) {
|
||||||
expdesc v2;
|
expdesc v2;
|
||||||
@@ -1149,7 +1151,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
|
|||||||
"C levels");
|
"C levels");
|
||||||
assignment(ls, &nv, nvars+1);
|
assignment(ls, &nv, nvars+1);
|
||||||
}
|
}
|
||||||
else { /* assignment -> `=' explist */
|
else { /* assignment -> '=' explist */
|
||||||
int nexps;
|
int nexps;
|
||||||
checknext(ls, '=');
|
checknext(ls, '=');
|
||||||
nexps = explist(ls, &e);
|
nexps = explist(ls, &e);
|
||||||
@@ -1173,7 +1175,7 @@ static int cond (LexState *ls) {
|
|||||||
/* cond -> exp */
|
/* cond -> exp */
|
||||||
expdesc v;
|
expdesc v;
|
||||||
expr(ls, &v); /* read condition */
|
expr(ls, &v); /* read condition */
|
||||||
if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
|
if (v.k == VNIL) v.k = VFALSE; /* 'falses' are all equal here */
|
||||||
luaK_goiftrue(ls->fs, &v);
|
luaK_goiftrue(ls->fs, &v);
|
||||||
return v.f;
|
return v.f;
|
||||||
}
|
}
|
||||||
@@ -1362,7 +1364,7 @@ static void forstat (LexState *ls, int line) {
|
|||||||
TString *varname;
|
TString *varname;
|
||||||
BlockCnt bl;
|
BlockCnt bl;
|
||||||
enterblock(fs, &bl, 1); /* scope for loop and control variables */
|
enterblock(fs, &bl, 1); /* scope for loop and control variables */
|
||||||
luaX_next(ls); /* skip `for' */
|
luaX_next(ls); /* skip 'for' */
|
||||||
varname = str_checkname(ls); /* first variable name */
|
varname = str_checkname(ls); /* first variable name */
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
case '=': fornum(ls, varname, line); break;
|
case '=': fornum(ls, varname, line); break;
|
||||||
@@ -1370,7 +1372,7 @@ static void forstat (LexState *ls, int line) {
|
|||||||
default: luaX_syntaxerror(ls, "'=' or 'in' expected");
|
default: luaX_syntaxerror(ls, "'=' or 'in' expected");
|
||||||
}
|
}
|
||||||
check_match(ls, TK_END, TK_FOR, line);
|
check_match(ls, TK_END, TK_FOR, line);
|
||||||
leaveblock(fs); /* loop scope (`break' jumps to this point) */
|
leaveblock(fs); /* loop scope ('break' jumps to this point) */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1400,7 +1402,7 @@ static void test_then_block (LexState *ls, int *escapelist) {
|
|||||||
enterblock(fs, &bl, 0);
|
enterblock(fs, &bl, 0);
|
||||||
jf = v.f;
|
jf = v.f;
|
||||||
}
|
}
|
||||||
statlist(ls); /* `then' part */
|
statlist(ls); /* 'then' part */
|
||||||
leaveblock(fs);
|
leaveblock(fs);
|
||||||
if (ls->t.token == TK_ELSE ||
|
if (ls->t.token == TK_ELSE ||
|
||||||
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
||||||
@@ -1417,7 +1419,7 @@ static void ifstat (LexState *ls, int line) {
|
|||||||
while (ls->t.token == TK_ELSEIF)
|
while (ls->t.token == TK_ELSEIF)
|
||||||
test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
|
test_then_block(ls, &escapelist); /* ELSEIF cond THEN block */
|
||||||
if (testnext(ls, TK_ELSE))
|
if (testnext(ls, TK_ELSE))
|
||||||
block(ls); /* `else' part */
|
block(ls); /* 'else' part */
|
||||||
check_match(ls, TK_END, TK_IF, line);
|
check_match(ls, TK_END, TK_IF, line);
|
||||||
luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
|
luaK_patchtohere(fs, escapelist); /* patch escape list to 'if' end */
|
||||||
}
|
}
|
||||||
@@ -1435,7 +1437,7 @@ static void localfunc (LexState *ls) {
|
|||||||
|
|
||||||
|
|
||||||
static void localstat (LexState *ls) {
|
static void localstat (LexState *ls) {
|
||||||
/* stat -> LOCAL NAME {`,' NAME} [`=' explist] */
|
/* stat -> LOCAL NAME {',' NAME} ['=' explist] */
|
||||||
int nvars = 0;
|
int nvars = 0;
|
||||||
int nexps;
|
int nexps;
|
||||||
expdesc e;
|
expdesc e;
|
||||||
@@ -1455,7 +1457,7 @@ static void localstat (LexState *ls) {
|
|||||||
|
|
||||||
|
|
||||||
static int funcname (LexState *ls, expdesc *v) {
|
static int funcname (LexState *ls, expdesc *v) {
|
||||||
/* funcname -> NAME {fieldsel} [`:' NAME] */
|
/* funcname -> NAME {fieldsel} [':' NAME] */
|
||||||
int ismethod = 0;
|
int ismethod = 0;
|
||||||
singlevar(ls, v);
|
singlevar(ls, v);
|
||||||
while (ls->t.token == '.')
|
while (ls->t.token == '.')
|
||||||
@@ -1476,7 +1478,7 @@ static void funcstat (LexState *ls, int line) {
|
|||||||
ismethod = funcname(ls, &v);
|
ismethod = funcname(ls, &v);
|
||||||
body(ls, &b, ismethod, line);
|
body(ls, &b, ismethod, line);
|
||||||
luaK_storevar(ls->fs, &v, &b);
|
luaK_storevar(ls->fs, &v, &b);
|
||||||
luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
|
luaK_fixline(ls->fs, line); /* definition "happens" in the first line */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1518,8 +1520,8 @@ static void retstat (LexState *ls) {
|
|||||||
if (nret == 1) /* only one single value? */
|
if (nret == 1) /* only one single value? */
|
||||||
first = luaK_exp2anyreg(fs, &e);
|
first = luaK_exp2anyreg(fs, &e);
|
||||||
else {
|
else {
|
||||||
luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
|
luaK_exp2nextreg(fs, &e); /* values must go to the stack */
|
||||||
first = fs->nactvar; /* return all `active' values */
|
first = fs->nactvar; /* return all active values */
|
||||||
lua_assert(nret == fs->freereg - first);
|
lua_assert(nret == fs->freereg - first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1630,7 +1632,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
|||||||
incr_top(L);
|
incr_top(L);
|
||||||
funcstate.f = cl->p = luaF_newproto(L);
|
funcstate.f = cl->p = luaF_newproto(L);
|
||||||
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
|
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
|
||||||
luaC_objbarrier(L, funcstate.f, funcstate.f->source);
|
lua_assert(iswhite(funcstate.f)); /* do not need barrier here */
|
||||||
lexstate.buff = buff;
|
lexstate.buff = buff;
|
||||||
lexstate.dyd = dyd;
|
lexstate.dyd = dyd;
|
||||||
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
|
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
|
||||||
|
|||||||
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
|
** Lua Parser
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -21,7 +21,7 @@ typedef enum {
|
|||||||
VNIL,
|
VNIL,
|
||||||
VTRUE,
|
VTRUE,
|
||||||
VFALSE,
|
VFALSE,
|
||||||
VK, /* info = index of constant in `k' */
|
VK, /* info = index of constant in 'k' */
|
||||||
VKFLT, /* nval = numerical float value */
|
VKFLT, /* nval = numerical float value */
|
||||||
VKINT, /* nval = numerical integer value */
|
VKINT, /* nval = numerical integer value */
|
||||||
VNONRELOC, /* info = result register */
|
VNONRELOC, /* info = result register */
|
||||||
@@ -50,8 +50,8 @@ typedef struct expdesc {
|
|||||||
lua_Number nval; /* for VKFLT */
|
lua_Number nval; /* for VKFLT */
|
||||||
lua_Integer ival; /* for VKINT */
|
lua_Integer ival; /* for VKINT */
|
||||||
} u;
|
} u;
|
||||||
int t; /* patch list of `exit when true' */
|
int t; /* patch list of 'exit when true' */
|
||||||
int f; /* patch list of `exit when false' */
|
int f; /* patch list of 'exit when false' */
|
||||||
} expdesc;
|
} expdesc;
|
||||||
|
|
||||||
|
|
||||||
@@ -100,11 +100,11 @@ typedef struct FuncState {
|
|||||||
struct FuncState *prev; /* enclosing function */
|
struct FuncState *prev; /* enclosing function */
|
||||||
struct LexState *ls; /* lexical state */
|
struct LexState *ls; /* lexical state */
|
||||||
struct BlockCnt *bl; /* chain of current blocks */
|
struct BlockCnt *bl; /* chain of current blocks */
|
||||||
int pc; /* next position to code (equivalent to `ncode') */
|
int pc; /* next position to code (equivalent to 'ncode') */
|
||||||
int lasttarget; /* 'label' of last 'jump label' */
|
int lasttarget; /* 'label' of last 'jump label' */
|
||||||
int jpc; /* list of pending jumps to `pc' */
|
int jpc; /* list of pending jumps to 'pc' */
|
||||||
int nk; /* number of elements in `k' */
|
int nk; /* number of elements in 'k' */
|
||||||
int np; /* number of elements in `p' */
|
int np; /* number of elements in 'p' */
|
||||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||||
short nlocvars; /* number of elements in 'f->locvars' */
|
short nlocvars; /* number of elements in 'f->locvars' */
|
||||||
lu_byte nactvar; /* number of active local variables */
|
lu_byte nactvar; /* number of active local variables */
|
||||||
|
|||||||
45
lprefix.h
Normal file
45
lprefix.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lprefix.h,v 1.1 2014/11/03 15:12:44 roberto Exp roberto $
|
||||||
|
** Definitions for Lua code that must come before any other header file
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lprefix_h
|
||||||
|
#define lprefix_h
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Allows POSIX/XSI stuff
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_USE_C89) /* { */
|
||||||
|
|
||||||
|
#if !defined(_XOPEN_SOURCE)
|
||||||
|
#define _XOPEN_SOURCE 600
|
||||||
|
#elif _XOPEN_SOURCE == 0
|
||||||
|
#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Allows manipulation of large files in gcc and some other compilers
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
|
||||||
|
#define _LARGEFILE_SOURCE 1
|
||||||
|
#define _FILE_OFFSET_BITS 64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Windows stuff
|
||||||
|
*/
|
||||||
|
#if defined(_WIN32) /* { */
|
||||||
|
|
||||||
|
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||||
|
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
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
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lstate_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lstate_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -35,9 +37,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define MEMERRMSG "not enough memory"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** a macro to help the creation of a unique random seed when a state is
|
** a macro to help the creation of a unique random seed when a state is
|
||||||
** created; the seed is used to randomize hashes.
|
** created; the seed is used to randomize hashes.
|
||||||
@@ -72,9 +71,8 @@ typedef struct LG {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Compute an initial seed as random as possible. In ANSI, rely on
|
** Compute an initial seed as random as possible. Rely on Address Space
|
||||||
** Address Space Layout Randomization (if present) to increase
|
** Layout Randomization (if present) to increase randomness..
|
||||||
** randomness..
|
|
||||||
*/
|
*/
|
||||||
#define addbuff(b,p,e) \
|
#define addbuff(b,p,e) \
|
||||||
{ size_t t = cast(size_t, e); \
|
{ size_t t = cast(size_t, e); \
|
||||||
@@ -199,12 +197,9 @@ static void f_luaopen (lua_State *L, void *ud) {
|
|||||||
UNUSED(ud);
|
UNUSED(ud);
|
||||||
stack_init(L, L); /* init stack */
|
stack_init(L, L); /* init stack */
|
||||||
init_registry(L, g);
|
init_registry(L, g);
|
||||||
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
luaS_init(L);
|
||||||
luaT_init(L);
|
luaT_init(L);
|
||||||
luaX_init(L);
|
luaX_init(L);
|
||||||
/* pre-create memory-error message */
|
|
||||||
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
|
|
||||||
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
|
|
||||||
g->gcrunning = 1; /* allow gc */
|
g->gcrunning = 1; /* allow gc */
|
||||||
g->version = lua_version(NULL);
|
g->version = lua_version(NULL);
|
||||||
luai_userstateopen(L);
|
luai_userstateopen(L);
|
||||||
|
|||||||
20
lstate.h
20
lstate.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstate.h,v 2.116 2014/10/06 21:34:34 roberto Exp roberto $
|
** $Id: lstate.h,v 2.121 2015/04/10 17:56:25 roberto Exp roberto $
|
||||||
** Global State
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -21,10 +21,10 @@
|
|||||||
** belong to one (and only one) of these lists, using field 'next' of
|
** belong to one (and only one) of these lists, using field 'next' of
|
||||||
** the 'CommonHeader' for the link:
|
** the 'CommonHeader' for the link:
|
||||||
**
|
**
|
||||||
** allgc: all objects not marked for finalization;
|
** 'allgc': all objects not marked for finalization;
|
||||||
** finobj: all objects marked for finalization;
|
** 'finobj': all objects marked for finalization;
|
||||||
** tobefnz: all objects ready to be finalized;
|
** 'tobefnz': all objects ready to be finalized;
|
||||||
** fixedgc: all objects that are not to be collected (currently
|
** 'fixedgc': all objects that are not to be collected (currently
|
||||||
** only small strings, such as reserved words).
|
** only small strings, such as reserved words).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@@ -94,6 +94,7 @@ typedef struct CallInfo {
|
|||||||
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
||||||
#define CIST_TAIL (1<<5) /* call was tail called */
|
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||||
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||||
|
#define CIST_LEQ (1<<7) /* using __lt for __le */
|
||||||
|
|
||||||
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
||||||
|
|
||||||
@@ -103,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 {
|
typedef struct global_State {
|
||||||
lua_Alloc frealloc; /* function to reallocate memory */
|
lua_Alloc frealloc; /* function to reallocate memory */
|
||||||
void *ud; /* auxiliary data to `frealloc' */
|
void *ud; /* auxiliary data to 'frealloc' */
|
||||||
lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||||
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||||
lu_mem GCmemtrav; /* memory traversed by the GC */
|
lu_mem GCmemtrav; /* memory traversed by the GC */
|
||||||
@@ -133,18 +134,19 @@ typedef struct global_State {
|
|||||||
Mbuffer buff; /* temporary buffer for string concatenation */
|
Mbuffer buff; /* temporary buffer for string concatenation */
|
||||||
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
|
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
|
||||||
int gcpause; /* size of pause between successive GCs */
|
int gcpause; /* size of pause between successive GCs */
|
||||||
int gcstepmul; /* GC `granularity' */
|
int gcstepmul; /* GC 'granularity' */
|
||||||
lua_CFunction panic; /* to be called in unprotected errors */
|
lua_CFunction panic; /* to be called in unprotected errors */
|
||||||
struct lua_State *mainthread;
|
struct lua_State *mainthread;
|
||||||
const lua_Number *version; /* pointer to version number */
|
const lua_Number *version; /* pointer to version number */
|
||||||
TString *memerrmsg; /* memory-error message */
|
TString *memerrmsg; /* memory-error message */
|
||||||
TString *tmname[TM_N]; /* array with tag-method names */
|
TString *tmname[TM_N]; /* array with tag-method names */
|
||||||
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
|
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
|
||||||
|
TString *strcache[STRCACHE_SIZE][1]; /* cache for strings in API */
|
||||||
} global_State;
|
} global_State;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** `per thread' state
|
** 'per thread' state
|
||||||
*/
|
*/
|
||||||
struct lua_State {
|
struct lua_State {
|
||||||
CommonHeader;
|
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)
|
** String table (keeps all strings handled by Lua)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lstring_c
|
#define lstring_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -20,6 +22,8 @@
|
|||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define MEMERRMSG "not enough memory"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
||||||
@@ -34,10 +38,10 @@
|
|||||||
** equality for long strings
|
** equality for long strings
|
||||||
*/
|
*/
|
||||||
int luaS_eqlngstr (TString *a, TString *b) {
|
int luaS_eqlngstr (TString *a, TString *b) {
|
||||||
size_t len = a->len;
|
size_t len = a->u.lnglen;
|
||||||
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
|
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
|
||||||
return (a == b) || /* same instance or... */
|
return (a == b) || /* same instance or... */
|
||||||
((len == b->len) && /* equal length and ... */
|
((len == b->u.lnglen) && /* equal length and ... */
|
||||||
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,9 +71,9 @@ void luaS_resize (lua_State *L, int newsize) {
|
|||||||
TString *p = tb->hash[i];
|
TString *p = tb->hash[i];
|
||||||
tb->hash[i] = NULL;
|
tb->hash[i] = NULL;
|
||||||
while (p) { /* for each node in the list */
|
while (p) { /* for each node in the list */
|
||||||
TString *hnext = p->hnext; /* save next */
|
TString *hnext = p->u.hnext; /* save next */
|
||||||
unsigned int h = lmod(p->hash, newsize); /* new position */
|
unsigned int h = lmod(p->hash, newsize); /* new position */
|
||||||
p->hnext = tb->hash[h]; /* chain it */
|
p->u.hnext = tb->hash[h]; /* chain it */
|
||||||
tb->hash[h] = p;
|
tb->hash[h] = p;
|
||||||
p = hnext;
|
p = hnext;
|
||||||
}
|
}
|
||||||
@@ -83,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
|
** creates a new string object
|
||||||
@@ -95,7 +127,6 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
|
|||||||
totalsize = sizelstring(l);
|
totalsize = sizelstring(l);
|
||||||
o = luaC_newobj(L, tag, totalsize);
|
o = luaC_newobj(L, tag, totalsize);
|
||||||
ts = gco2ts(o);
|
ts = gco2ts(o);
|
||||||
ts->len = l;
|
|
||||||
ts->hash = h;
|
ts->hash = h;
|
||||||
ts->extra = 0;
|
ts->extra = 0;
|
||||||
memcpy(getaddrstr(ts), str, l * sizeof(char));
|
memcpy(getaddrstr(ts), str, l * sizeof(char));
|
||||||
@@ -108,8 +139,8 @@ void luaS_remove (lua_State *L, TString *ts) {
|
|||||||
stringtable *tb = &G(L)->strt;
|
stringtable *tb = &G(L)->strt;
|
||||||
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
|
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
|
||||||
while (*p != ts) /* find previous element */
|
while (*p != ts) /* find previous element */
|
||||||
p = &(*p)->hnext;
|
p = &(*p)->u.hnext;
|
||||||
*p = (*p)->hnext; /* remove element from its list */
|
*p = (*p)->u.hnext; /* remove element from its list */
|
||||||
tb->nuse--;
|
tb->nuse--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,8 +153,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
|||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
unsigned int h = luaS_hash(str, l, g->seed);
|
unsigned int h = luaS_hash(str, l, g->seed);
|
||||||
TString **list = &g->strt.hash[lmod(h, g->strt.size)];
|
TString **list = &g->strt.hash[lmod(h, g->strt.size)];
|
||||||
for (ts = *list; ts != NULL; ts = ts->hnext) {
|
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
||||||
if (l == ts->len &&
|
if (l == ts->shrlen &&
|
||||||
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
||||||
/* found! */
|
/* found! */
|
||||||
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
||||||
@@ -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 */
|
list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
|
||||||
}
|
}
|
||||||
ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
|
ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
|
||||||
ts->hnext = *list;
|
ts->shrlen = cast_byte(l);
|
||||||
|
ts->u.hnext = *list;
|
||||||
*list = ts;
|
*list = ts;
|
||||||
g->strt.nuse++;
|
g->strt.nuse++;
|
||||||
return ts;
|
return ts;
|
||||||
@@ -150,18 +182,32 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
|||||||
if (l <= LUAI_MAXSHORTLEN) /* short string? */
|
if (l <= LUAI_MAXSHORTLEN) /* short string? */
|
||||||
return internshrstr(L, str, l);
|
return internshrstr(L, str, l);
|
||||||
else {
|
else {
|
||||||
|
TString *ts;
|
||||||
if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
|
if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
|
||||||
luaM_toobig(L);
|
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) {
|
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)
|
** String table (keep all strings handled by Lua)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -13,7 +13,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
|
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
|
||||||
#define sizestring(s) sizelstring((s)->len)
|
|
||||||
|
|
||||||
#define sizeludata(l) (sizeof(union UUdata) + (l))
|
#define sizeludata(l) (sizeof(union UUdata) + (l))
|
||||||
#define sizeudata(u) sizeludata((u)->len)
|
#define sizeudata(u) sizeludata((u)->len)
|
||||||
@@ -37,6 +36,8 @@
|
|||||||
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
|
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
|
||||||
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
||||||
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
||||||
|
LUAI_FUNC void luaS_clearcache (global_State *g);
|
||||||
|
LUAI_FUNC void luaS_init (lua_State *L);
|
||||||
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
||||||
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
|
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
|
||||||
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||||
|
|||||||
312
lstrlib.c
312
lstrlib.c
@@ -1,20 +1,23 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstrlib.c,v 1.204 2014/10/17 16:28:21 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
|
** Standard library for string operations and pattern-matching
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lstrlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <float.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lstrlib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -30,10 +33,19 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* macro to `unsign' a character */
|
/* macro to 'unsign' a character */
|
||||||
#define uchar(c) ((unsigned char)(c))
|
#define uchar(c) ((unsigned char)(c))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Some sizes are better limited to fit in 'int', but must also fit in
|
||||||
|
** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
|
||||||
|
*/
|
||||||
|
#define MAXSIZE \
|
||||||
|
(sizeof(size_t) < sizeof(int) ? (~(size_t)0) : (size_t)(INT_MAX))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int str_len (lua_State *L) {
|
static int str_len (lua_State *L) {
|
||||||
size_t l;
|
size_t l;
|
||||||
@@ -59,7 +71,7 @@ static int str_sub (lua_State *L) {
|
|||||||
if (start < 1) start = 1;
|
if (start < 1) start = 1;
|
||||||
if (end > (lua_Integer)l) end = l;
|
if (end > (lua_Integer)l) end = l;
|
||||||
if (start <= end)
|
if (start <= end)
|
||||||
lua_pushlstring(L, s + start - 1, (size_t)(end - start + 1));
|
lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1);
|
||||||
else lua_pushliteral(L, "");
|
else lua_pushliteral(L, "");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -103,13 +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) {
|
static int str_rep (lua_State *L) {
|
||||||
size_t l, lsep;
|
size_t l, lsep;
|
||||||
const char *s = luaL_checklstring(L, 1, &l);
|
const char *s = luaL_checklstring(L, 1, &l);
|
||||||
@@ -145,9 +150,9 @@ static int str_byte (lua_State *L) {
|
|||||||
if (posi < 1) posi = 1;
|
if (posi < 1) posi = 1;
|
||||||
if (pose > (lua_Integer)l) pose = l;
|
if (pose > (lua_Integer)l) pose = l;
|
||||||
if (posi > pose) return 0; /* empty interval; return no values */
|
if (posi > pose) return 0; /* empty interval; return no values */
|
||||||
n = (int)(pose - posi + 1);
|
if (pose - posi >= INT_MAX) /* arithmetic overflow? */
|
||||||
if (posi + n <= pose) /* arithmetic overflow? */
|
|
||||||
return luaL_error(L, "string slice too long");
|
return luaL_error(L, "string slice too long");
|
||||||
|
n = (int)(pose - posi) + 1;
|
||||||
luaL_checkstack(L, n, "string slice too long");
|
luaL_checkstack(L, n, "string slice too long");
|
||||||
for (i=0; i<n; i++)
|
for (i=0; i<n; i++)
|
||||||
lua_pushinteger(L, uchar(s[posi+i-1]));
|
lua_pushinteger(L, uchar(s[posi+i-1]));
|
||||||
@@ -255,11 +260,11 @@ static const char *classend (MatchState *ms, const char *p) {
|
|||||||
}
|
}
|
||||||
case '[': {
|
case '[': {
|
||||||
if (*p == '^') p++;
|
if (*p == '^') p++;
|
||||||
do { /* look for a `]' */
|
do { /* look for a ']' */
|
||||||
if (p == ms->p_end)
|
if (p == ms->p_end)
|
||||||
luaL_error(ms->L, "malformed pattern (missing ']')");
|
luaL_error(ms->L, "malformed pattern (missing ']')");
|
||||||
if (*(p++) == L_ESC && p < ms->p_end)
|
if (*(p++) == L_ESC && p < ms->p_end)
|
||||||
p++; /* skip escapes (e.g. `%]') */
|
p++; /* skip escapes (e.g. '%]') */
|
||||||
} while (*p != ']');
|
} while (*p != ']');
|
||||||
return p+1;
|
return p+1;
|
||||||
}
|
}
|
||||||
@@ -294,7 +299,7 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
|
|||||||
int sig = 1;
|
int sig = 1;
|
||||||
if (*(p+1) == '^') {
|
if (*(p+1) == '^') {
|
||||||
sig = 0;
|
sig = 0;
|
||||||
p++; /* skip the `^' */
|
p++; /* skip the '^' */
|
||||||
}
|
}
|
||||||
while (++p < ec) {
|
while (++p < ec) {
|
||||||
if (*p == L_ESC) {
|
if (*p == L_ESC) {
|
||||||
@@ -431,7 +436,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case '$': {
|
case '$': {
|
||||||
if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */
|
if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */
|
||||||
goto dflt; /* no; go to default */
|
goto dflt; /* no; go to default */
|
||||||
s = (s == ms->src_end) ? s : NULL; /* check end of string */
|
s = (s == ms->src_end) ? s : NULL; /* check end of string */
|
||||||
break;
|
break;
|
||||||
@@ -495,7 +500,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
|||||||
}
|
}
|
||||||
case '+': /* 1 or more repetitions */
|
case '+': /* 1 or more repetitions */
|
||||||
s++; /* 1 match already done */
|
s++; /* 1 match already done */
|
||||||
/* go through */
|
/* FALLTHROUGH */
|
||||||
case '*': /* 0 or more repetitions */
|
case '*': /* 0 or more repetitions */
|
||||||
s = max_expand(ms, s, p, ep);
|
s = max_expand(ms, s, p, ep);
|
||||||
break;
|
break;
|
||||||
@@ -519,16 +524,16 @@ static const char *match (MatchState *ms, const char *s, const char *p) {
|
|||||||
static const char *lmemfind (const char *s1, size_t l1,
|
static const char *lmemfind (const char *s1, size_t l1,
|
||||||
const char *s2, size_t l2) {
|
const char *s2, size_t l2) {
|
||||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||||
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
else if (l2 > l1) return NULL; /* avoids a negative 'l1' */
|
||||||
else {
|
else {
|
||||||
const char *init; /* to search for a `*s2' inside `s1' */
|
const char *init; /* to search for a '*s2' inside 's1' */
|
||||||
l2--; /* 1st char will be checked by `memchr' */
|
l2--; /* 1st char will be checked by 'memchr' */
|
||||||
l1 = l1-l2; /* `s2' cannot be found after that */
|
l1 = l1-l2; /* 's2' cannot be found after that */
|
||||||
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
||||||
init++; /* 1st char is already checked */
|
init++; /* 1st char is already checked */
|
||||||
if (memcmp(init, s2+1, l2) == 0)
|
if (memcmp(init, s2+1, l2) == 0)
|
||||||
return init-1;
|
return init-1;
|
||||||
else { /* correct `l1' and `s1' to try again */
|
else { /* correct 'l1' and 's1' to try again */
|
||||||
l1 -= init-s1;
|
l1 -= init-s1;
|
||||||
s1 = init;
|
s1 = init;
|
||||||
}
|
}
|
||||||
@@ -544,13 +549,13 @@ static void push_onecapture (MatchState *ms, int i, const char *s,
|
|||||||
if (i == 0) /* ms->level == 0, too */
|
if (i == 0) /* ms->level == 0, too */
|
||||||
lua_pushlstring(ms->L, s, e - s); /* add whole match */
|
lua_pushlstring(ms->L, s, e - s); /* add whole match */
|
||||||
else
|
else
|
||||||
luaL_error(ms->L, "invalid capture index");
|
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ptrdiff_t l = ms->capture[i].len;
|
ptrdiff_t l = ms->capture[i].len;
|
||||||
if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
|
if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
|
||||||
if (l == CAP_POSITION)
|
if (l == CAP_POSITION)
|
||||||
lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
|
lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
|
||||||
else
|
else
|
||||||
lua_pushlstring(ms->L, ms->capture[i].init, l);
|
lua_pushlstring(ms->L, ms->capture[i].init, l);
|
||||||
}
|
}
|
||||||
@@ -594,8 +599,8 @@ static int str_find_aux (lua_State *L, int find) {
|
|||||||
/* do a plain search */
|
/* do a plain search */
|
||||||
const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
|
const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
|
||||||
if (s2) {
|
if (s2) {
|
||||||
lua_pushinteger(L, s2 - s + 1);
|
lua_pushinteger(L, (s2 - s) + 1);
|
||||||
lua_pushinteger(L, s2 - s + lp);
|
lua_pushinteger(L, (s2 - s) + lp);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -617,7 +622,7 @@ static int str_find_aux (lua_State *L, int find) {
|
|||||||
lua_assert(ms.matchdepth == MAXCCALLS);
|
lua_assert(ms.matchdepth == MAXCCALLS);
|
||||||
if ((res=match(&ms, s1, p)) != NULL) {
|
if ((res=match(&ms, s1, p)) != NULL) {
|
||||||
if (find) {
|
if (find) {
|
||||||
lua_pushinteger(L, s1 - s + 1); /* start */
|
lua_pushinteger(L, (s1 - s) + 1); /* start */
|
||||||
lua_pushinteger(L, res - s); /* end */
|
lua_pushinteger(L, res - s); /* end */
|
||||||
return push_captures(&ms, NULL, 0) + 2;
|
return push_captures(&ms, NULL, 0) + 2;
|
||||||
}
|
}
|
||||||
@@ -793,17 +798,100 @@ static int str_gsub (lua_State *L) {
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
|
#if !defined(lua_number2strx) /* { */
|
||||||
#define MAX_ITEM 512
|
|
||||||
|
/*
|
||||||
|
** Hexadecimal floating-point formatter
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <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 */
|
/* valid flags in a format specification */
|
||||||
#define FLAGS "-+ #0"
|
#define FLAGS "-+ #0"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** maximum size of each format specification (such as "%-099.99d")
|
** maximum size of each format specification (such as "%-099.99d")
|
||||||
** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error)
|
|
||||||
*/
|
*/
|
||||||
#define MAX_FORMAT (sizeof(FLAGS) + 2 + 10)
|
#define MAX_FORMAT 32
|
||||||
|
|
||||||
|
|
||||||
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
||||||
@@ -845,8 +933,8 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
|||||||
if (isdigit(uchar(*p)))
|
if (isdigit(uchar(*p)))
|
||||||
luaL_error(L, "invalid format (width or precision too long)");
|
luaL_error(L, "invalid format (width or precision too long)");
|
||||||
*(form++) = '%';
|
*(form++) = '%';
|
||||||
memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
|
memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char));
|
||||||
form += p - strfrmt + 1;
|
form += (p - strfrmt) + 1;
|
||||||
*form = '\0';
|
*form = '\0';
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
@@ -879,7 +967,7 @@ static int str_format (lua_State *L) {
|
|||||||
else if (*++strfrmt == L_ESC)
|
else if (*++strfrmt == L_ESC)
|
||||||
luaL_addchar(&b, *strfrmt++); /* %% */
|
luaL_addchar(&b, *strfrmt++); /* %% */
|
||||||
else { /* format item */
|
else { /* format item */
|
||||||
char form[MAX_FORMAT]; /* to store the format (`%...') */
|
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||||
char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
|
char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
|
||||||
int nb = 0; /* number of bytes in added item */
|
int nb = 0; /* number of bytes in added item */
|
||||||
if (++arg > top)
|
if (++arg > top)
|
||||||
@@ -897,10 +985,11 @@ static int str_format (lua_State *L) {
|
|||||||
nb = sprintf(buff, form, n);
|
nb = sprintf(buff, form, n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'e': case 'E': case 'f':
|
|
||||||
#if defined(LUA_USE_AFORMAT)
|
|
||||||
case 'a': case 'A':
|
case 'a': case 'A':
|
||||||
#endif
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
|
nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg));
|
||||||
|
break;
|
||||||
|
case 'e': case 'E': case 'f':
|
||||||
case 'g': case 'G': {
|
case 'g': case 'G': {
|
||||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||||
nb = sprintf(buff, form, luaL_checknumber(L, arg));
|
nb = sprintf(buff, form, luaL_checknumber(L, arg));
|
||||||
@@ -917,15 +1006,14 @@ static int str_format (lua_State *L) {
|
|||||||
/* no precision and string is too long to be formatted;
|
/* no precision and string is too long to be formatted;
|
||||||
keep original string */
|
keep original string */
|
||||||
luaL_addvalue(&b);
|
luaL_addvalue(&b);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
nb = sprintf(buff, form, s);
|
nb = sprintf(buff, form, s);
|
||||||
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
|
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default: { /* also treat cases `pnLlh' */
|
default: { /* also treat cases 'pnLlh' */
|
||||||
return luaL_error(L, "invalid option '%%%c' to 'format'",
|
return luaL_error(L, "invalid option '%%%c' to 'format'",
|
||||||
*(strfrmt - 1));
|
*(strfrmt - 1));
|
||||||
}
|
}
|
||||||
@@ -947,6 +1035,11 @@ 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 */
|
/* maximum size for the binary representation of an integer */
|
||||||
#define MAXINTSIZE 16
|
#define MAXINTSIZE 16
|
||||||
|
|
||||||
@@ -959,9 +1052,6 @@ static int str_format (lua_State *L) {
|
|||||||
/* size of a lua_Integer */
|
/* size of a lua_Integer */
|
||||||
#define SZINT ((int)sizeof(lua_Integer))
|
#define SZINT ((int)sizeof(lua_Integer))
|
||||||
|
|
||||||
/* mask for all ones in last byte in a lua Integer */
|
|
||||||
#define HIGHERBYTE ((lua_Unsigned)MC << (NB * (SZINT - 1)))
|
|
||||||
|
|
||||||
|
|
||||||
/* dummy union to get native endianness */
|
/* dummy union to get native endianness */
|
||||||
static const union {
|
static const union {
|
||||||
@@ -1012,8 +1102,7 @@ typedef enum KOption {
|
|||||||
Kzstr, /* zero-terminated strings */
|
Kzstr, /* zero-terminated strings */
|
||||||
Kpadding, /* padding */
|
Kpadding, /* padding */
|
||||||
Kpaddalign, /* padding for alignment */
|
Kpaddalign, /* padding for alignment */
|
||||||
Knop, /* no-op (configuration or spaces) */
|
Knop /* no-op (configuration or spaces) */
|
||||||
Keof /* end of format */
|
|
||||||
} KOption;
|
} KOption;
|
||||||
|
|
||||||
|
|
||||||
@@ -1029,8 +1118,8 @@ static int getnum (const char **fmt, int df) {
|
|||||||
else {
|
else {
|
||||||
int a = 0;
|
int a = 0;
|
||||||
do {
|
do {
|
||||||
a = a*10 + *((*fmt)++) - '0';
|
a = a*10 + (*((*fmt)++) - '0');
|
||||||
} while (digit(**fmt) && a < (INT_MAX/10 - 10));
|
} while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10);
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1043,7 +1132,8 @@ static int getnum (const char **fmt, int df) {
|
|||||||
static int getnumlimit (Header *h, const char **fmt, int df) {
|
static int getnumlimit (Header *h, const char **fmt, int df) {
|
||||||
int sz = getnum(fmt, df);
|
int sz = getnum(fmt, df);
|
||||||
if (sz > MAXINTSIZE || sz <= 0)
|
if (sz > MAXINTSIZE || sz <= 0)
|
||||||
luaL_error(h->L, "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE);
|
luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
|
||||||
|
sz, MAXINTSIZE);
|
||||||
return sz;
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1080,20 +1170,22 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
|||||||
case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
|
case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
|
||||||
case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
|
case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
|
||||||
case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
|
case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
|
||||||
case 'c': *size = getnum(fmt, 1); return Kchar;
|
case 'c':
|
||||||
|
*size = getnum(fmt, -1);
|
||||||
|
if (*size == -1)
|
||||||
|
luaL_error(h->L, "missing size for format option 'c'");
|
||||||
|
return Kchar;
|
||||||
case 'z': return Kzstr;
|
case 'z': return Kzstr;
|
||||||
case 'x': *size = 1; return Kpadding;
|
case 'x': *size = 1; return Kpadding;
|
||||||
case 'X': return Kpaddalign;
|
case 'X': return Kpaddalign;
|
||||||
case ' ': return Knop;
|
case ' ': break;
|
||||||
case '\0': return Keof;
|
case '<': h->islittle = 1; break;
|
||||||
case '<': h->islittle = 1; return Knop;
|
case '>': h->islittle = 0; break;
|
||||||
case '>': h->islittle = 0; return Knop;
|
case '=': h->islittle = nativeendian.little; break;
|
||||||
case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); return Knop;
|
case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break;
|
||||||
default: {
|
default: luaL_error(h->L, "invalid format option '%c'", opt);
|
||||||
luaL_error(h->L, "invalid format option '%c'", opt);
|
|
||||||
return Knop;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return Knop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1103,19 +1195,15 @@ static KOption getoption (Header *h, const char **fmt, int *size) {
|
|||||||
** alignment requirements.
|
** alignment requirements.
|
||||||
** Local variable 'size' gets the size to be aligned. (Kpadal option
|
** Local variable 'size' gets the size to be aligned. (Kpadal option
|
||||||
** always gets its full alignment, other options are limited by
|
** always gets its full alignment, other options are limited by
|
||||||
** the maximum alignment ('maxalign). Kchar option needs no aligment
|
** the maximum alignment ('maxalign'). Kchar option needs no alignment
|
||||||
** despite its size.
|
** despite its size.
|
||||||
*/
|
*/
|
||||||
static KOption getdetails (Header *h, size_t totalsize,
|
static KOption getdetails (Header *h, size_t totalsize,
|
||||||
const char **fmt, int *psize, int *ntoalign) {
|
const char **fmt, int *psize, int *ntoalign) {
|
||||||
int align;
|
KOption opt = getoption(h, fmt, psize);
|
||||||
KOption opt;
|
int align = *psize; /* usually, alignment follows size */
|
||||||
do {
|
|
||||||
opt = getoption(h, fmt, psize);
|
|
||||||
} while (opt == Knop); /* skip no-op options */
|
|
||||||
align = *psize; /* usually, alignment follows size */
|
|
||||||
if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
|
if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
|
||||||
if (getoption(h, fmt, &align) == Kchar || align == 0)
|
if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
|
||||||
luaL_argerror(h->L, 1, "invalid next option for option 'X'");
|
luaL_argerror(h->L, 1, "invalid next option for option 'X'");
|
||||||
}
|
}
|
||||||
if (align <= 1 || opt == Kchar) /* need no alignment? */
|
if (align <= 1 || opt == Kchar) /* need no alignment? */
|
||||||
@@ -1131,15 +1219,25 @@ static KOption getdetails (Header *h, size_t totalsize,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
|
||||||
|
** The final 'if' handles the case when 'size' is larger than
|
||||||
|
** the size of a Lua integer, correcting the extra sign-extension
|
||||||
|
** bytes if necessary (by default they would be zeros).
|
||||||
|
*/
|
||||||
static void packint (luaL_Buffer *b, lua_Unsigned n,
|
static void packint (luaL_Buffer *b, lua_Unsigned n,
|
||||||
int islittle, int size, lua_Unsigned mask) {
|
int islittle, int size, int neg) {
|
||||||
char *buff = luaL_prepbuffsize(b, size);
|
char *buff = luaL_prepbuffsize(b, size);
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < size - 1; i++) {
|
buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
|
||||||
buff[islittle ? i : size - 1 - i] = (n & MC);
|
for (i = 1; i < size; i++) {
|
||||||
n = (n >> NB) | mask;
|
n >>= NB;
|
||||||
|
buff[islittle ? i : size - 1 - i] = (char)(n & MC);
|
||||||
|
}
|
||||||
|
if (neg && size > SZINT) { /* negative number need sign extension? */
|
||||||
|
for (i = SZINT; i < size; i++) /* correct extra bytes */
|
||||||
|
buff[islittle ? i : size - 1 - i] = (char)MC;
|
||||||
}
|
}
|
||||||
buff[islittle ? i : size - 1 - i] = (n & MC);
|
|
||||||
luaL_addsize(b, size); /* add result to buffer */
|
luaL_addsize(b, size); /* add result to buffer */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1171,21 +1269,21 @@ static int str_pack (lua_State *L) {
|
|||||||
initheader(L, &h);
|
initheader(L, &h);
|
||||||
lua_pushnil(L); /* mark to separate arguments from string buffer */
|
lua_pushnil(L); /* mark to separate arguments from string buffer */
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
for (;;) {
|
while (*fmt != '\0') {
|
||||||
int size, ntoalign;
|
int size, ntoalign;
|
||||||
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||||
totalsize += ntoalign + size;
|
totalsize += ntoalign + size;
|
||||||
while (ntoalign-- > 0) luaL_addchar(&b, '\0'); /* fill alignment */
|
while (ntoalign-- > 0)
|
||||||
|
luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */
|
||||||
arg++;
|
arg++;
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case Kint: { /* signed integers */
|
case Kint: { /* signed integers */
|
||||||
lua_Integer n = luaL_checkinteger(L, arg);
|
lua_Integer n = luaL_checkinteger(L, arg);
|
||||||
lua_Unsigned mask = (n < 0) ? HIGHERBYTE : 0; /* sign extension */
|
|
||||||
if (size < SZINT) { /* need overflow check? */
|
if (size < SZINT) { /* need overflow check? */
|
||||||
lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
|
lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
|
||||||
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
|
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
|
||||||
}
|
}
|
||||||
packint(&b, (lua_Unsigned)n, h.islittle, size, mask);
|
packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kuint: { /* unsigned integers */
|
case Kuint: { /* unsigned integers */
|
||||||
@@ -1235,18 +1333,50 @@ static int str_pack (lua_State *L) {
|
|||||||
totalsize += len + 1;
|
totalsize += len + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kpadding: luaL_addchar(&b, '\0'); /* go through */
|
case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */
|
||||||
case Kpaddalign: case Knop:
|
case Kpaddalign: case Knop:
|
||||||
arg--; /* undo increment */
|
arg--; /* undo increment */
|
||||||
break;
|
break;
|
||||||
case Keof: /* end of format */
|
|
||||||
luaL_pushresult(&b);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_packsize (lua_State *L) {
|
||||||
|
Header h;
|
||||||
|
const char *fmt = luaL_checkstring(L, 1); /* format string */
|
||||||
|
size_t totalsize = 0; /* accumulate total size of result */
|
||||||
|
initheader(L, &h);
|
||||||
|
while (*fmt != '\0') {
|
||||||
|
int size, ntoalign;
|
||||||
|
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
|
||||||
|
size += ntoalign; /* total space used by option */
|
||||||
|
luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
|
||||||
|
"format result too large");
|
||||||
|
totalsize += size;
|
||||||
|
switch (opt) {
|
||||||
|
case Kstring: /* strings with length count */
|
||||||
|
case Kzstr: /* zero-terminated string */
|
||||||
|
luaL_argerror(L, 1, "variable-length format");
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushinteger(L, (lua_Integer)totalsize);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Unpack an integer with 'size' bytes and 'islittle' endianness.
|
||||||
|
** If size is smaller than the size of a Lua integer and integer
|
||||||
|
** is signed, must do sign extension (propagating the sign to the
|
||||||
|
** higher bits); if size is larger than the size of a Lua integer,
|
||||||
|
** it must check the unread bytes to see whether they do not cause an
|
||||||
|
** overflow.
|
||||||
|
*/
|
||||||
static lua_Integer unpackint (lua_State *L, const char *str,
|
static lua_Integer unpackint (lua_State *L, const char *str,
|
||||||
int islittle, int size, int issigned) {
|
int islittle, int size, int issigned) {
|
||||||
lua_Unsigned res = 0;
|
lua_Unsigned res = 0;
|
||||||
@@ -1262,11 +1392,11 @@ static lua_Integer unpackint (lua_State *L, const char *str,
|
|||||||
res = ((res ^ mask) - mask); /* do sign extension */
|
res = ((res ^ mask) - mask); /* do sign extension */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* must check unread bytes */
|
else if (size > SZINT) { /* must check unread bytes */
|
||||||
int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
|
int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
|
||||||
for (i = limit; i < size; i++) {
|
for (i = limit; i < size; i++) {
|
||||||
if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
|
if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
|
||||||
luaL_error(L, "%d-bit integer does not fit into Lua Integer", size);
|
luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (lua_Integer)res;
|
return (lua_Integer)res;
|
||||||
@@ -1282,12 +1412,12 @@ static int str_unpack (lua_State *L) {
|
|||||||
int n = 0; /* number of results */
|
int n = 0; /* number of results */
|
||||||
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
|
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
|
||||||
initheader(L, &h);
|
initheader(L, &h);
|
||||||
for (;;) {
|
while (*fmt != '\0') {
|
||||||
int size, ntoalign;
|
int size, ntoalign;
|
||||||
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
|
||||||
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
|
if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
|
||||||
luaL_argerror(L, 2, "data string too short");
|
luaL_argerror(L, 2, "data string too short");
|
||||||
pos += ntoalign;
|
pos += ntoalign; /* skip alignment */
|
||||||
/* stack space for item + next position */
|
/* stack space for item + next position */
|
||||||
luaL_checkstack(L, 2, "too many results");
|
luaL_checkstack(L, 2, "too many results");
|
||||||
n++;
|
n++;
|
||||||
@@ -1317,24 +1447,23 @@ static int str_unpack (lua_State *L) {
|
|||||||
size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
|
size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
|
||||||
luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
|
luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
|
||||||
lua_pushlstring(L, data + pos + size, len);
|
lua_pushlstring(L, data + pos + size, len);
|
||||||
pos += len;
|
pos += len; /* skip string */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kzstr: {
|
case Kzstr: {
|
||||||
size_t len = (int)strlen(data + pos);
|
size_t len = (int)strlen(data + pos);
|
||||||
lua_pushlstring(L, data + pos, len);
|
lua_pushlstring(L, data + pos, len);
|
||||||
pos += len + 1; /* skip final '\0' */
|
pos += len + 1; /* skip string plus final '\0' */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kpaddalign: case Kpadding: case Knop:
|
case Kpaddalign: case Kpadding: case Knop:
|
||||||
n--; /* undo increment */
|
n--; /* undo increment */
|
||||||
break;
|
break;
|
||||||
case Keof: /* end of format */
|
|
||||||
lua_pushinteger(L, pos + 1); /* next position */
|
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
pos += size;
|
pos += size;
|
||||||
}
|
}
|
||||||
|
lua_pushinteger(L, pos + 1); /* next position */
|
||||||
|
return n + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
@@ -1356,6 +1485,7 @@ static const luaL_Reg strlib[] = {
|
|||||||
{"sub", str_sub},
|
{"sub", str_sub},
|
||||||
{"upper", str_upper},
|
{"upper", str_upper},
|
||||||
{"pack", str_pack},
|
{"pack", str_pack},
|
||||||
|
{"packsize", str_packsize},
|
||||||
{"unpack", str_unpack},
|
{"unpack", str_unpack},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
165
ltable.c
165
ltable.c
@@ -1,31 +1,31 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltable.c,v 2.95 2014/09/04 18:15:29 roberto Exp roberto $
|
** $Id: ltable.c,v 2.110 2015/05/20 16:22:30 roberto Exp roberto $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ltable_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Implementation of tables (aka arrays, objects, or hash tables).
|
** Implementation of tables (aka arrays, objects, or hash tables).
|
||||||
** Tables keep its elements in two parts: an array part and a hash part.
|
** Tables keep its elements in two parts: an array part and a hash part.
|
||||||
** Non-negative integer keys are all candidates to be kept in the array
|
** Non-negative integer keys are all candidates to be kept in the array
|
||||||
** part. The actual size of the array is the largest `n' such that at
|
** part. The actual size of the array is the largest 'n' such that
|
||||||
** least half the slots between 0 and n are in use.
|
** more than half the slots between 1 and n are in use.
|
||||||
** Hash uses a mix of chained scatter table with Brent's variation.
|
** Hash uses a mix of chained scatter table with Brent's variation.
|
||||||
** A main invariant of these tables is that, if an element is not
|
** A main invariant of these tables is that, if an element is not
|
||||||
** in its main position (i.e. the `original' position that its hash gives
|
** in its main position (i.e. the 'original' position that its hash gives
|
||||||
** to it), then the colliding element is in its own main position.
|
** to it), then the colliding element is in its own main position.
|
||||||
** Hence even when the load factor reaches 100%, performance remains good.
|
** Hence even when the load factor reaches 100%, performance remains good.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <float.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
#define ltable_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -69,7 +69,7 @@
|
|||||||
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
|
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
|
||||||
|
|
||||||
|
|
||||||
#define hashpointer(t,p) hashmod(t, point2int(p))
|
#define hashpointer(t,p) hashmod(t, point2uint(p))
|
||||||
|
|
||||||
|
|
||||||
#define dummynode (&dummynode_)
|
#define dummynode (&dummynode_)
|
||||||
@@ -83,35 +83,37 @@ static const Node dummynode_ = {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Checks whether a float has a value representable as a lua_Integer
|
** Hash for floating-point numbers.
|
||||||
** (and does the conversion if so)
|
** The main computation should be just
|
||||||
|
** n = 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 !defined(l_hashfloat)
|
||||||
if ((x) == l_floor(x)) /* integral value? */
|
static int l_hashfloat (lua_Number n) {
|
||||||
return lua_numtointeger(x, p); /* try as an integer */
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** hash for floating-point numbers
|
|
||||||
*/
|
|
||||||
static Node *hashfloat (const Table *t, lua_Number n) {
|
|
||||||
int i;
|
int i;
|
||||||
n = l_mathop(frexp)(n, &i) * cast_num(INT_MAX - DBL_MAX_EXP);
|
lua_Integer ni;
|
||||||
i += cast_int(n);
|
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
|
||||||
if (i < 0) {
|
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
|
||||||
if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */
|
lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == HUGE_VAL);
|
||||||
i = 0; /* handle INT_MIN */
|
return 0;
|
||||||
i = -i; /* must be a positive value */
|
}
|
||||||
|
else { /* normal case */
|
||||||
|
unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni);
|
||||||
|
return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u);
|
||||||
}
|
}
|
||||||
return hashmod(t, i);
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns the `main' position of an element in a table (that is, the index
|
** returns the 'main' position of an element in a table (that is, the index
|
||||||
** of its hash value)
|
** of its hash value)
|
||||||
*/
|
*/
|
||||||
static Node *mainposition (const Table *t, const TValue *key) {
|
static Node *mainposition (const Table *t, const TValue *key) {
|
||||||
@@ -119,13 +121,13 @@ static Node *mainposition (const Table *t, const TValue *key) {
|
|||||||
case LUA_TNUMINT:
|
case LUA_TNUMINT:
|
||||||
return hashint(t, ivalue(key));
|
return hashint(t, ivalue(key));
|
||||||
case LUA_TNUMFLT:
|
case LUA_TNUMFLT:
|
||||||
return hashfloat(t, fltvalue(key));
|
return hashmod(t, l_hashfloat(fltvalue(key)));
|
||||||
case LUA_TSHRSTR:
|
case LUA_TSHRSTR:
|
||||||
return hashstr(t, tsvalue(key));
|
return hashstr(t, tsvalue(key));
|
||||||
case LUA_TLNGSTR: {
|
case LUA_TLNGSTR: {
|
||||||
TString *s = tsvalue(key);
|
TString *s = tsvalue(key);
|
||||||
if (s->extra == 0) { /* no hash? */
|
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 */
|
s->extra = 1; /* now it has its hash */
|
||||||
}
|
}
|
||||||
return hashstr(t, tsvalue(key));
|
return hashstr(t, tsvalue(key));
|
||||||
@@ -143,7 +145,7 @@ static Node *mainposition (const Table *t, const TValue *key) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns the index for `key' if `key' is an appropriate key to live in
|
** returns the index for 'key' if 'key' is an appropriate key to live in
|
||||||
** the array part of the table, 0 otherwise.
|
** the array part of the table, 0 otherwise.
|
||||||
*/
|
*/
|
||||||
static unsigned int arrayindex (const TValue *key) {
|
static unsigned int arrayindex (const TValue *key) {
|
||||||
@@ -152,12 +154,12 @@ static unsigned int arrayindex (const TValue *key) {
|
|||||||
if (0 < k && (lua_Unsigned)k <= MAXASIZE)
|
if (0 < k && (lua_Unsigned)k <= MAXASIZE)
|
||||||
return cast(unsigned int, k); /* 'key' is an appropriate array index */
|
return cast(unsigned int, k); /* 'key' is an appropriate array index */
|
||||||
}
|
}
|
||||||
return 0; /* `key' did not match some condition */
|
return 0; /* 'key' did not match some condition */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** returns the index of a `key' for table traversals. First goes all
|
** returns the index of a 'key' for table traversals. First goes all
|
||||||
** elements in the array part, then elements in the hash part. The
|
** elements in the array part, then elements in the hash part. The
|
||||||
** beginning of a traversal is signaled by 0.
|
** beginning of a traversal is signaled by 0.
|
||||||
*/
|
*/
|
||||||
@@ -165,13 +167,13 @@ static unsigned int findindex (lua_State *L, Table *t, StkId key) {
|
|||||||
unsigned int i;
|
unsigned int i;
|
||||||
if (ttisnil(key)) return 0; /* first iteration */
|
if (ttisnil(key)) return 0; /* first iteration */
|
||||||
i = arrayindex(key);
|
i = arrayindex(key);
|
||||||
if (i != 0 && i <= t->sizearray) /* is `key' inside array part? */
|
if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */
|
||||||
return i; /* yes; that's the index */
|
return i; /* yes; that's the index */
|
||||||
else {
|
else {
|
||||||
int nx;
|
int nx;
|
||||||
Node *n = mainposition(t, key);
|
Node *n = mainposition(t, key);
|
||||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
/* key may be dead already, but it is ok to use it in `next' */
|
/* key may be dead already, but it is ok to use it in 'next' */
|
||||||
if (luaV_rawequalobj(gkey(n), key) ||
|
if (luaV_rawequalobj(gkey(n), key) ||
|
||||||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
||||||
deadvalue(gkey(n)) == gcvalue(key))) {
|
deadvalue(gkey(n)) == gcvalue(key))) {
|
||||||
@@ -217,34 +219,35 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
|
|||||||
/*
|
/*
|
||||||
** Compute the optimal size for the array part of table 't'. 'nums' is a
|
** Compute the optimal size for the array part of table 't'. 'nums' is a
|
||||||
** "count array" where 'nums[i]' is the number of integers in the table
|
** "count array" where 'nums[i]' is the number of integers in the table
|
||||||
** between 2^(i - 1) + 1 and 2^i. Put in '*narray' the optimal size, and
|
** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of
|
||||||
** return the number of elements that will go to that part.
|
** integer keys in the table and leaves with the number of keys that
|
||||||
|
** will go to the array part; return the optimal size.
|
||||||
*/
|
*/
|
||||||
static unsigned int computesizes (unsigned int nums[], unsigned int *narray) {
|
static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
|
||||||
int i;
|
int i;
|
||||||
unsigned int twotoi; /* 2^i */
|
unsigned int twotoi; /* 2^i (candidate for optimal size) */
|
||||||
unsigned int a = 0; /* number of elements smaller than 2^i */
|
unsigned int a = 0; /* number of elements smaller than 2^i */
|
||||||
unsigned int na = 0; /* number of elements to go to array part */
|
unsigned int na = 0; /* number of elements to go to array part */
|
||||||
unsigned int n = 0; /* optimal size for array part */
|
unsigned int optimal = 0; /* optimal size for array part */
|
||||||
for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
|
/* loop while keys can fill more than half of total size */
|
||||||
|
for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) {
|
||||||
if (nums[i] > 0) {
|
if (nums[i] > 0) {
|
||||||
a += nums[i];
|
a += nums[i];
|
||||||
if (a > twotoi/2) { /* more than half elements present? */
|
if (a > twotoi/2) { /* more than half elements present? */
|
||||||
n = twotoi; /* optimal size (till now) */
|
optimal = twotoi; /* optimal size (till now) */
|
||||||
na = a; /* all elements up to 'n' will go to array part */
|
na = a; /* all elements up to 'optimal' will go to array part */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (a == *narray) break; /* all elements already counted */
|
|
||||||
}
|
}
|
||||||
*narray = n;
|
lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
|
||||||
lua_assert(*narray/2 <= na && na <= *narray);
|
*pna = na;
|
||||||
return na;
|
return optimal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int countint (const TValue *key, unsigned int *nums) {
|
static int countint (const TValue *key, unsigned int *nums) {
|
||||||
unsigned int k = arrayindex(key);
|
unsigned int k = arrayindex(key);
|
||||||
if (k != 0) { /* is `key' an appropriate array index? */
|
if (k != 0) { /* is 'key' an appropriate array index? */
|
||||||
nums[luaO_ceillog2(k)]++; /* count as such */
|
nums[luaO_ceillog2(k)]++; /* count as such */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -253,10 +256,15 @@ static int countint (const TValue *key, unsigned int *nums) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Count keys in array part of table 't': Fill 'nums[i]' with
|
||||||
|
** number of keys that will go into corresponding slice and return
|
||||||
|
** total number of non-nil keys.
|
||||||
|
*/
|
||||||
static unsigned int numusearray (const Table *t, unsigned int *nums) {
|
static unsigned int numusearray (const Table *t, unsigned int *nums) {
|
||||||
int lg;
|
int lg;
|
||||||
unsigned int ttlg; /* 2^lg */
|
unsigned int ttlg; /* 2^lg */
|
||||||
unsigned int ause = 0; /* summation of `nums' */
|
unsigned int ause = 0; /* summation of 'nums' */
|
||||||
unsigned int i = 1; /* count to traverse all array keys */
|
unsigned int i = 1; /* count to traverse all array keys */
|
||||||
/* traverse each slice */
|
/* traverse each slice */
|
||||||
for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
|
for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
|
||||||
@@ -279,8 +287,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int numusehash (const Table *t, unsigned int *nums,
|
static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
|
||||||
unsigned int *pnasize) {
|
|
||||||
int totaluse = 0; /* total number of elements */
|
int totaluse = 0; /* total number of elements */
|
||||||
int ause = 0; /* elements added to 'nums' (can go to array part) */
|
int ause = 0; /* elements added to 'nums' (can go to array part) */
|
||||||
int i = sizenode(t);
|
int i = sizenode(t);
|
||||||
@@ -291,7 +298,7 @@ static int numusehash (const Table *t, unsigned int *nums,
|
|||||||
totaluse++;
|
totaluse++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*pnasize += ause;
|
*pna += ause;
|
||||||
return totaluse;
|
return totaluse;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,7 +315,7 @@ static void setarrayvector (lua_State *L, Table *t, unsigned int size) {
|
|||||||
static void setnodevector (lua_State *L, Table *t, unsigned int size) {
|
static void setnodevector (lua_State *L, Table *t, unsigned int size) {
|
||||||
int lsize;
|
int lsize;
|
||||||
if (size == 0) { /* no elements to hash part? */
|
if (size == 0) { /* no elements to hash part? */
|
||||||
t->node = cast(Node *, dummynode); /* use common `dummynode' */
|
t->node = cast(Node *, dummynode); /* use common 'dummynode' */
|
||||||
lsize = 0;
|
lsize = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -361,7 +368,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isdummy(nold))
|
if (!isdummy(nold))
|
||||||
luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */
|
luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old hash */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -374,21 +381,22 @@ void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
|
|||||||
** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
|
** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
|
||||||
*/
|
*/
|
||||||
static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
||||||
unsigned int nasize, na;
|
unsigned int asize; /* optimal size for array part */
|
||||||
|
unsigned int na; /* number of keys in the array part */
|
||||||
unsigned int nums[MAXABITS + 1];
|
unsigned int nums[MAXABITS + 1];
|
||||||
int i;
|
int i;
|
||||||
int totaluse;
|
int totaluse;
|
||||||
for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
|
for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
|
||||||
nasize = numusearray(t, nums); /* count keys in array part */
|
na = numusearray(t, nums); /* count keys in array part */
|
||||||
totaluse = nasize; /* all those keys are integer keys */
|
totaluse = na; /* all those keys are integer keys */
|
||||||
totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
|
totaluse += numusehash(t, nums, &na); /* count keys in hash part */
|
||||||
/* count extra key */
|
/* count extra key */
|
||||||
nasize += countint(ek, nums);
|
na += countint(ek, nums);
|
||||||
totaluse++;
|
totaluse++;
|
||||||
/* compute new size for array part */
|
/* compute new size for array part */
|
||||||
na = computesizes(nums, &nasize);
|
asize = computesizes(nums, &na);
|
||||||
/* resize the table to new computed sizes */
|
/* resize the table to new computed sizes */
|
||||||
luaH_resize(L, t, nasize, totaluse - na);
|
luaH_resize(L, t, asize, totaluse - na);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -441,14 +449,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
TValue aux;
|
TValue aux;
|
||||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||||
else if (ttisfloat(key)) {
|
else if (ttisfloat(key)) {
|
||||||
lua_Number n = fltvalue(key);
|
|
||||||
lua_Integer k;
|
lua_Integer k;
|
||||||
if (luai_numisnan(n))
|
if (luaV_tointeger(key, &k, 0)) { /* index is int? */
|
||||||
luaG_runerror(L, "table index is NaN");
|
|
||||||
if (numisinteger(n, &k)) { /* index is int? */
|
|
||||||
setivalue(&aux, k);
|
setivalue(&aux, k);
|
||||||
key = &aux; /* insert it as an integer */
|
key = &aux; /* insert it as an integer */
|
||||||
}
|
}
|
||||||
|
else if (luai_numisnan(fltvalue(key)))
|
||||||
|
luaG_runerror(L, "table index is NaN");
|
||||||
}
|
}
|
||||||
mp = mainposition(t, key);
|
mp = mainposition(t, key);
|
||||||
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
|
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
|
||||||
@@ -482,7 +489,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
|||||||
mp = f;
|
mp = f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setkey(L, &mp->i_key, key);
|
setnodekey(L, &mp->i_key, key);
|
||||||
luaC_barrierback(L, t, key);
|
luaC_barrierback(L, t, key);
|
||||||
lua_assert(ttisnil(gval(mp)));
|
lua_assert(ttisnil(gval(mp)));
|
||||||
return gval(mp);
|
return gval(mp);
|
||||||
@@ -498,7 +505,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
|
|||||||
return &t->array[key - 1];
|
return &t->array[key - 1];
|
||||||
else {
|
else {
|
||||||
Node *n = hashint(t, key);
|
Node *n = hashint(t, key);
|
||||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key)
|
if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key)
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else {
|
else {
|
||||||
@@ -518,7 +525,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
|
|||||||
const TValue *luaH_getstr (Table *t, TString *key) {
|
const TValue *luaH_getstr (Table *t, TString *key) {
|
||||||
Node *n = hashstr(t, key);
|
Node *n = hashstr(t, key);
|
||||||
lua_assert(key->tt == LUA_TSHRSTR);
|
lua_assert(key->tt == LUA_TSHRSTR);
|
||||||
for (;;) { /* check whether `key' is somewhere in the chain */
|
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||||
const TValue *k = gkey(n);
|
const TValue *k = gkey(n);
|
||||||
if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
|
if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
@@ -542,13 +549,13 @@ const TValue *luaH_get (Table *t, const TValue *key) {
|
|||||||
case LUA_TNIL: return luaO_nilobject;
|
case LUA_TNIL: return luaO_nilobject;
|
||||||
case LUA_TNUMFLT: {
|
case LUA_TNUMFLT: {
|
||||||
lua_Integer k;
|
lua_Integer k;
|
||||||
if (numisinteger(fltvalue(key), &k)) /* index is int? */
|
if (luaV_tointeger(key, &k, 0)) /* index is int? */
|
||||||
return luaH_getint(t, k); /* use specialized version */
|
return luaH_getint(t, k); /* use specialized version */
|
||||||
/* else go through */
|
/* else... */
|
||||||
}
|
} /* FALLTHROUGH */
|
||||||
default: {
|
default: {
|
||||||
Node *n = mainposition(t, key);
|
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))
|
if (luaV_rawequalobj(gkey(n), key))
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else {
|
else {
|
||||||
@@ -592,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) {
|
static int unbound_search (Table *t, unsigned int j) {
|
||||||
unsigned int i = j; /* i is zero or a present index */
|
unsigned int i = j; /* i is zero or a present index */
|
||||||
j++;
|
j++;
|
||||||
/* find `i' and `j' such that i is present and j is not */
|
/* find 'i' and 'j' such that i is present and j is not */
|
||||||
while (!ttisnil(luaH_getint(t, j))) {
|
while (!ttisnil(luaH_getint(t, j))) {
|
||||||
i = j;
|
i = j;
|
||||||
if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */
|
if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */
|
||||||
@@ -614,7 +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).
|
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
|
||||||
*/
|
*/
|
||||||
int luaH_getn (Table *t) {
|
int luaH_getn (Table *t) {
|
||||||
|
|||||||
20
ltablib.c
20
ltablib.c
@@ -1,16 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltablib.c,v 1.76 2014/09/22 06:42:15 roberto Exp roberto $
|
** $Id: ltablib.c,v 1.79 2014/11/02 19:19:04 roberto Exp roberto $
|
||||||
** Library for Table Manipulation
|
** Library for Table Manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ltablib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define ltablib_c
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -122,8 +124,6 @@ static int tmove (lua_State *L) {
|
|||||||
lua_Integer e = luaL_checkinteger(L, 3);
|
lua_Integer e = luaL_checkinteger(L, 3);
|
||||||
lua_Integer t = luaL_checkinteger(L, 4);
|
lua_Integer t = luaL_checkinteger(L, 4);
|
||||||
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
|
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
|
||||||
/* the following restriction avoids several problems with overflows */
|
|
||||||
luaL_argcheck(L, f > 0, 2, "initial position must be positive");
|
|
||||||
if (e >= f) { /* otherwise, nothing to move */
|
if (e >= f) { /* otherwise, nothing to move */
|
||||||
lua_Integer n, i;
|
lua_Integer n, i;
|
||||||
ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL)
|
ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL)
|
||||||
@@ -132,7 +132,11 @@ static int tmove (lua_State *L) {
|
|||||||
ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL)
|
ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL)
|
||||||
? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
|
? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
|
||||||
: lua_seti;
|
: 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 */
|
n = e - f + 1; /* number of elements to move */
|
||||||
|
luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
|
||||||
|
"destination wrap around");
|
||||||
if (t > f) {
|
if (t > f) {
|
||||||
for (i = n - 1; i >= 0; i--) {
|
for (i = n - 1; i >= 0; i--) {
|
||||||
(*ta.geti)(L, 1, f + i);
|
(*ta.geti)(L, 1, f + i);
|
||||||
@@ -225,7 +229,7 @@ static int unpack (lua_State *L) {
|
|||||||
/*
|
/*
|
||||||
** {======================================================
|
** {======================================================
|
||||||
** Quicksort
|
** Quicksort
|
||||||
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
|
** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
|
||||||
** Addison-Wesley, 1993.)
|
** Addison-Wesley, 1993.)
|
||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
@@ -241,7 +245,7 @@ static int sort_comp (lua_State *L, int a, int b) {
|
|||||||
int res;
|
int res;
|
||||||
lua_pushvalue(L, 2);
|
lua_pushvalue(L, 2);
|
||||||
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
||||||
lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
|
lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
|
||||||
lua_call(L, 2, 1);
|
lua_call(L, 2, 1);
|
||||||
res = lua_toboolean(L, -1);
|
res = lua_toboolean(L, -1);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
|||||||
167
ltests.c
167
ltests.c
@@ -1,18 +1,21 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltests.c,v 2.187 2014/10/06 17:06:49 roberto Exp roberto $
|
** $Id: ltests.c,v 2.204 2015/03/13 16:24:50 roberto Exp roberto $
|
||||||
** Internal Module for Debugging of the Lua Implementation
|
** Internal Module for Debugging of the Lua Implementation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define ltests_c
|
||||||
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <setjmp.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define ltests_c
|
|
||||||
#define LUA_CORE
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
#include "lapi.h"
|
||||||
@@ -46,6 +49,9 @@ int islocked = 0;
|
|||||||
#define obj_at(L,k) (L->ci->func + (k))
|
#define obj_at(L,k) (L->ci->func + (k))
|
||||||
|
|
||||||
|
|
||||||
|
static int runC (lua_State *L, lua_State *L1, const char *pc);
|
||||||
|
|
||||||
|
|
||||||
static void setnameval (lua_State *L, const char *name, int val) {
|
static void setnameval (lua_State *L, const char *name, int val) {
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_pushinteger(L, val);
|
lua_pushinteger(L, val);
|
||||||
@@ -152,7 +158,7 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
|
|||||||
memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
|
memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
|
||||||
freeblock(mc, block); /* erase (and check) old copy */
|
freeblock(mc, block); /* erase (and check) old copy */
|
||||||
}
|
}
|
||||||
/* initialize new part of the block with something `weird' */
|
/* initialize new part of the block with something weird */
|
||||||
fillmem(cast(char *, newblock + 1) + commonsize, size - commonsize);
|
fillmem(cast(char *, newblock + 1) + commonsize, size - commonsize);
|
||||||
/* initialize marks after block */
|
/* initialize marks after block */
|
||||||
for (i = 0; i < MARKSIZE; i++)
|
for (i = 0; i < MARKSIZE; i++)
|
||||||
@@ -283,11 +289,11 @@ static void checkLclosure (global_State *g, LClosure *cl) {
|
|||||||
static int lua_checkpc (lua_State *L, CallInfo *ci) {
|
static int lua_checkpc (lua_State *L, CallInfo *ci) {
|
||||||
if (!isLua(ci)) return 1;
|
if (!isLua(ci)) return 1;
|
||||||
else {
|
else {
|
||||||
Proto *p;
|
/* if function yielded (inside a hook), real 'func' is in 'extra' field */
|
||||||
if (L->status != LUA_YIELD || ci != L->ci)
|
StkId f = (L->status != LUA_YIELD || ci != L->ci)
|
||||||
p = ci_func(ci)->p;
|
? ci->func
|
||||||
else /* real 'func' was saved in 'extra' field */
|
: restorestack(L, ci->extra);
|
||||||
p = clLvalue(restorestack(L, ci->extra))->p;
|
Proto *p = clLvalue(f)->p;
|
||||||
return p->code <= ci->u.l.savedpc &&
|
return p->code <= ci->u.l.savedpc &&
|
||||||
ci->u.l.savedpc <= p->code + p->sizecode;
|
ci->u.l.savedpc <= p->code + p->sizecode;
|
||||||
}
|
}
|
||||||
@@ -305,9 +311,9 @@ static void checkstack (global_State *g, lua_State *L1) {
|
|||||||
lua_assert(ci->top <= L1->stack_last);
|
lua_assert(ci->top <= L1->stack_last);
|
||||||
lua_assert(lua_checkpc(L1, ci));
|
lua_assert(lua_checkpc(L1, ci));
|
||||||
}
|
}
|
||||||
if (L1->stack) {
|
if (L1->stack) { /* complete thread? */
|
||||||
for (o = L1->stack; o < L1->top; o++)
|
for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++)
|
||||||
checkliveness(g, o);
|
checkliveness(g, o); /* entire stack must have valid values */
|
||||||
}
|
}
|
||||||
else lua_assert(L1->stacksize == 0);
|
else lua_assert(L1->stacksize == 0);
|
||||||
}
|
}
|
||||||
@@ -534,7 +540,7 @@ static int listk (lua_State *L) {
|
|||||||
|
|
||||||
static int listlocals (lua_State *L) {
|
static int listlocals (lua_State *L) {
|
||||||
Proto *p;
|
Proto *p;
|
||||||
int pc = (int)luaL_checkinteger(L, 2) - 1;
|
int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
const char *name;
|
const char *name;
|
||||||
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
|
||||||
@@ -549,12 +555,25 @@ static int listlocals (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void printstack (lua_State *L) {
|
||||||
|
int i;
|
||||||
|
int n = lua_gettop(L);
|
||||||
|
for (i = 1; i <= n; i++) {
|
||||||
|
printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int get_limits (lua_State *L) {
|
static int get_limits (lua_State *L) {
|
||||||
lua_createtable(L, 0, 5);
|
lua_createtable(L, 0, 5);
|
||||||
setnameval(L, "BITS_INT", LUAI_BITSINT);
|
setnameval(L, "BITS_INT", LUAI_BITSINT);
|
||||||
|
setnameval(L, "MAXARG_Ax", MAXARG_Ax);
|
||||||
|
setnameval(L, "MAXARG_Bx", MAXARG_Bx);
|
||||||
|
setnameval(L, "MAXARG_sBx", MAXARG_sBx);
|
||||||
|
setnameval(L, "BITS_INT", LUAI_BITSINT);
|
||||||
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||||
setnameval(L, "MAXSTACK", MAXSTACK);
|
|
||||||
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
setnameval(L, "NUM_OPCODES", NUM_OPCODES);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -657,13 +676,13 @@ static int stacklevel (lua_State *L) {
|
|||||||
lua_pushinteger(L, (L->top - L->stack));
|
lua_pushinteger(L, (L->top - L->stack));
|
||||||
lua_pushinteger(L, (L->stack_last - L->stack));
|
lua_pushinteger(L, (L->stack_last - L->stack));
|
||||||
lua_pushinteger(L, (unsigned long)&a);
|
lua_pushinteger(L, (unsigned long)&a);
|
||||||
return 5;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int table_query (lua_State *L) {
|
static int table_query (lua_State *L) {
|
||||||
const Table *t;
|
const Table *t;
|
||||||
int i = (int)luaL_optinteger(L, 2, -1);
|
int i = cast_int(luaL_optinteger(L, 2, -1));
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
t = hvalue(obj_at(L, 1));
|
t = hvalue(obj_at(L, 1));
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
@@ -696,7 +715,7 @@ static int table_query (lua_State *L) {
|
|||||||
|
|
||||||
static int string_query (lua_State *L) {
|
static int string_query (lua_State *L) {
|
||||||
stringtable *tb = &G(L)->strt;
|
stringtable *tb = &G(L)->strt;
|
||||||
int s = (int)luaL_optinteger(L, 1, 0) - 1;
|
int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
|
||||||
if (s == -1) {
|
if (s == -1) {
|
||||||
lua_pushinteger(L ,tb->size);
|
lua_pushinteger(L ,tb->size);
|
||||||
lua_pushinteger(L ,tb->nuse);
|
lua_pushinteger(L ,tb->nuse);
|
||||||
@@ -705,7 +724,7 @@ static int string_query (lua_State *L) {
|
|||||||
else if (s < tb->size) {
|
else if (s < tb->size) {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (ts = tb->hash[s]; ts != NULL; ts = ts->hnext) {
|
for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
|
||||||
setsvalue2s(L, L->top, ts);
|
setsvalue2s(L, L->top, ts);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
n++;
|
n++;
|
||||||
@@ -734,14 +753,14 @@ static int getref (lua_State *L) {
|
|||||||
|
|
||||||
static int unref (lua_State *L) {
|
static int unref (lua_State *L) {
|
||||||
int level = lua_gettop(L);
|
int level = lua_gettop(L);
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, (int)luaL_checkinteger(L, 1));
|
luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
|
||||||
lua_assert(lua_gettop(L) == level);
|
lua_assert(lua_gettop(L) == level);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int upvalue (lua_State *L) {
|
static int upvalue (lua_State *L) {
|
||||||
int n = (int)luaL_checkinteger(L, 2);
|
int n = cast_int(luaL_checkinteger(L, 2));
|
||||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||||
if (lua_isnone(L, 3)) {
|
if (lua_isnone(L, 3)) {
|
||||||
const char *name = lua_getupvalue(L, 1, n);
|
const char *name = lua_getupvalue(L, 1, n);
|
||||||
@@ -897,12 +916,64 @@ static int int2fb_aux (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int log2_aux (lua_State *L) {
|
||||||
|
unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
|
||||||
|
lua_pushinteger(L, luaO_ceillog2(x));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; };
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** does a long-jump back to "main program".
|
||||||
|
*/
|
||||||
|
static int panicback (lua_State *L) {
|
||||||
|
struct Aux *b;
|
||||||
|
lua_checkstack(L, 1); /* open space for 'Aux' struct */
|
||||||
|
lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */
|
||||||
|
b = (struct Aux *)lua_touserdata(L, -1);
|
||||||
|
lua_pop(L, 1); /* remove 'Aux' struct */
|
||||||
|
runC(b->L, L, b->paniccode); /* run optional panic code */
|
||||||
|
longjmp(b->jb, 1);
|
||||||
|
return 1; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int checkpanic (lua_State *L) {
|
||||||
|
struct Aux b;
|
||||||
|
void *ud;
|
||||||
|
lua_State *L1;
|
||||||
|
const char *code = luaL_checkstring(L, 1);
|
||||||
|
lua_Alloc f = lua_getallocf(L, &ud);
|
||||||
|
b.paniccode = luaL_optstring(L, 2, "");
|
||||||
|
b.L = L;
|
||||||
|
L1 = lua_newstate(f, ud); /* create new state */
|
||||||
|
if (L1 == NULL) { /* error? */
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
lua_atpanic(L1, panicback); /* set its panic function */
|
||||||
|
lua_pushlightuserdata(L1, &b);
|
||||||
|
lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */
|
||||||
|
if (setjmp(b.jb) == 0) { /* set jump buffer */
|
||||||
|
runC(L, L1, code); /* run code unprotected */
|
||||||
|
lua_pushliteral(L, "no errors");
|
||||||
|
}
|
||||||
|
else { /* error handling */
|
||||||
|
/* move error message to original state */
|
||||||
|
lua_pushstring(L, lua_tostring(L1, -1));
|
||||||
|
}
|
||||||
|
lua_close(L1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {====================================================================
|
||||||
** function to test the API with C. It interprets a kind of assembler
|
** function to test the API with C. It interprets a kind of assembler
|
||||||
** language with calls to the API, so the test can be driven by Lua code
|
** language with calls to the API, so the test can be driven by Lua code
|
||||||
** =======================================================
|
** =====================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -925,7 +996,7 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
|
|||||||
int sig = 1;
|
int sig = 1;
|
||||||
skip(pc);
|
skip(pc);
|
||||||
if (**pc == '.') {
|
if (**pc == '.') {
|
||||||
res = lua_tointeger(L1, -1);
|
res = cast_int(lua_tointeger(L1, -1));
|
||||||
lua_pop(L1, 1);
|
lua_pop(L1, 1);
|
||||||
(*pc)++;
|
(*pc)++;
|
||||||
return res;
|
return res;
|
||||||
@@ -996,7 +1067,7 @@ static int Cfunck (lua_State *L, int status, lua_KContext ctx);
|
|||||||
** LUA_OPUNM -> _
|
** LUA_OPUNM -> _
|
||||||
** LUA_OPBNOT -> !
|
** LUA_OPBNOT -> !
|
||||||
*/
|
*/
|
||||||
static char ops[] = "+-*%^/\\&|~<>_!";
|
static const char ops[] = "+-*%^/\\&|~<>_!";
|
||||||
|
|
||||||
static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||||
char buff[300];
|
char buff[300];
|
||||||
@@ -1032,12 +1103,18 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
}
|
}
|
||||||
else if EQ("checkstack") {
|
else if EQ("checkstack") {
|
||||||
int sz = getnum;
|
int sz = getnum;
|
||||||
luaL_checkstack(L1, sz, getstring);
|
const char *msg = getstring;
|
||||||
|
if (*msg == '\0')
|
||||||
|
msg = NULL; /* to test 'luaL_checkstack' with no message */
|
||||||
|
luaL_checkstack(L1, sz, msg);
|
||||||
}
|
}
|
||||||
else if EQ("compare") {
|
else if EQ("compare") {
|
||||||
|
const char *opt = getstring; /* EQ, LT, or LE */
|
||||||
|
int op = (opt[0] == 'E') ? LUA_OPEQ
|
||||||
|
: (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE;
|
||||||
int a = getindex;
|
int a = getindex;
|
||||||
int b = getindex;
|
int b = getindex;
|
||||||
lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
|
lua_pushboolean(L1, lua_compare(L1, a, b, op));
|
||||||
}
|
}
|
||||||
else if EQ("concat") {
|
else if EQ("concat") {
|
||||||
lua_concat(L1, getnum);
|
lua_concat(L1, getnum);
|
||||||
@@ -1113,9 +1190,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
luaL_loadfile(L1, luaL_checkstring(L1, getnum));
|
||||||
}
|
}
|
||||||
else if EQ("loadstring") {
|
else if EQ("loadstring") {
|
||||||
size_t sl;
|
const char *s = luaL_checkstring(L1, getnum);
|
||||||
const char *s = luaL_checklstring(L1, getnum, &sl);
|
luaL_loadstring(L1, s);
|
||||||
luaL_loadbuffer(L1, s, sl, s);
|
|
||||||
}
|
}
|
||||||
else if EQ("newmetatable") {
|
else if EQ("newmetatable") {
|
||||||
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
|
||||||
@@ -1138,7 +1214,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
else if EQ("pcall") {
|
else if EQ("pcall") {
|
||||||
int narg = getnum;
|
int narg = getnum;
|
||||||
int nres = getnum;
|
int nres = getnum;
|
||||||
status = lua_pcall(L1, narg, nres, 0);
|
status = lua_pcall(L1, narg, nres, getnum);
|
||||||
}
|
}
|
||||||
else if EQ("pcallk") {
|
else if EQ("pcallk") {
|
||||||
int narg = getnum;
|
int narg = getnum;
|
||||||
@@ -1155,15 +1231,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
printf("%s\n", luaL_tolstring(L1, n, NULL));
|
||||||
lua_pop(L1, 1);
|
lua_pop(L1, 1);
|
||||||
}
|
}
|
||||||
else {
|
else printstack(L1);
|
||||||
int i;
|
|
||||||
n = lua_gettop(L1);
|
|
||||||
for (i = 1; i <= n; i++) {
|
|
||||||
printf("%s ", luaL_tolstring(L1, i, NULL));
|
|
||||||
lua_pop(L1, 1);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if EQ("pushbool") {
|
else if EQ("pushbool") {
|
||||||
lua_pushboolean(L1, getnum);
|
lua_pushboolean(L1, getnum);
|
||||||
@@ -1196,6 +1264,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
int t = getindex;
|
int t = getindex;
|
||||||
lua_rawgeti(L1, t, getnum);
|
lua_rawgeti(L1, t, getnum);
|
||||||
}
|
}
|
||||||
|
else if EQ("rawgetp") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_rawgetp(L1, t, cast(void *, cast(size_t, getnum)));
|
||||||
|
}
|
||||||
|
else if EQ("rawsetp") {
|
||||||
|
int t = getindex;
|
||||||
|
lua_rawsetp(L1, t, cast(void *, cast(size_t, getnum)));
|
||||||
|
}
|
||||||
else if EQ("remove") {
|
else if EQ("remove") {
|
||||||
lua_remove(L1, getnum);
|
lua_remove(L1, getnum);
|
||||||
}
|
}
|
||||||
@@ -1244,6 +1320,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
|||||||
int i = getindex;
|
int i = getindex;
|
||||||
lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
|
lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
|
||||||
}
|
}
|
||||||
|
else if EQ("error") {
|
||||||
|
lua_error(L1);
|
||||||
|
}
|
||||||
else if EQ("throw") {
|
else if EQ("throw") {
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
static struct X { int x; } x;
|
static struct X { int x; } x;
|
||||||
@@ -1271,7 +1350,7 @@ static struct X { int x; } x;
|
|||||||
else if EQ("tostring") {
|
else if EQ("tostring") {
|
||||||
const char *s = lua_tostring(L1, getindex);
|
const char *s = lua_tostring(L1, getindex);
|
||||||
const char *s1 = lua_pushstring(L1, s);
|
const char *s1 = lua_pushstring(L1, s);
|
||||||
lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
|
lua_assert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
|
||||||
}
|
}
|
||||||
else if EQ("type") {
|
else if EQ("type") {
|
||||||
lua_pushstring(L1, luaL_typename(L1, getnum));
|
lua_pushstring(L1, luaL_typename(L1, getnum));
|
||||||
@@ -1366,7 +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) {
|
static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
|
||||||
if (*scpt == '\0') { /* no script? */
|
if (*scpt == '\0') { /* no script? */
|
||||||
@@ -1393,7 +1472,7 @@ static int sethook (lua_State *L) {
|
|||||||
else {
|
else {
|
||||||
const char *scpt = luaL_checkstring(L, 1);
|
const char *scpt = luaL_checkstring(L, 1);
|
||||||
const char *smask = luaL_checkstring(L, 2);
|
const char *smask = luaL_checkstring(L, 2);
|
||||||
int count = (int)luaL_optinteger(L, 3, 0);
|
int count = cast_int(luaL_optinteger(L, 3, 0));
|
||||||
int mask = 0;
|
int mask = 0;
|
||||||
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
|
||||||
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
|
||||||
@@ -1436,11 +1515,13 @@ static const struct luaL_Reg tests_funcs[] = {
|
|||||||
{"getref", getref},
|
{"getref", getref},
|
||||||
{"hash", hash_query},
|
{"hash", hash_query},
|
||||||
{"int2fb", int2fb_aux},
|
{"int2fb", int2fb_aux},
|
||||||
|
{"log2", log2_aux},
|
||||||
{"limits", get_limits},
|
{"limits", get_limits},
|
||||||
{"listcode", listcode},
|
{"listcode", listcode},
|
||||||
{"listk", listk},
|
{"listk", listk},
|
||||||
{"listlocals", listlocals},
|
{"listlocals", listlocals},
|
||||||
{"loadlib", loadlib},
|
{"loadlib", loadlib},
|
||||||
|
{"checkpanic", checkpanic},
|
||||||
{"newstate", newstate},
|
{"newstate", newstate},
|
||||||
{"newuserdata", newuserdata},
|
{"newuserdata", newuserdata},
|
||||||
{"num2int", num2int},
|
{"num2int", num2int},
|
||||||
|
|||||||
15
ltests.h
15
ltests.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltests.h,v 2.39 2014/07/24 19:33:29 roberto Exp roberto $
|
** $Id: ltests.h,v 2.46 2014/12/19 13:33:06 roberto Exp roberto $
|
||||||
** Internal Header for Debugging of the Lua Implementation
|
** Internal Header for Debugging of the Lua Implementation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -46,7 +46,7 @@ typedef struct Memcontrol {
|
|||||||
unsigned long objcount[LUA_NUMTAGS];
|
unsigned long objcount[LUA_NUMTAGS];
|
||||||
} Memcontrol;
|
} Memcontrol;
|
||||||
|
|
||||||
extern Memcontrol l_memcontrol;
|
LUA_API Memcontrol l_memcontrol;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -81,9 +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)
|
#if defined(lua_c)
|
||||||
#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol)
|
#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
|
#define MINSTRTABSIZE 2
|
||||||
|
|
||||||
|
|
||||||
|
/* make stack-overflow tests run faster */
|
||||||
|
#undef LUAI_MAXSTACK
|
||||||
|
#define LUAI_MAXSTACK 50000
|
||||||
|
|
||||||
|
|
||||||
#undef LUAI_USER_ALIGNMENT_T
|
#undef LUAI_USER_ALIGNMENT_T
|
||||||
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#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
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define ltm_c
|
#define ltm_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
@@ -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,
|
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
||||||
const TValue *p2, TValue *p3, int hasres) {
|
const TValue *p2, TValue *p3, int hasres) {
|
||||||
ptrdiff_t result = savestack(L, p3);
|
ptrdiff_t result = savestack(L, p3);
|
||||||
setobj2s(L, L->top++, f); /* push function */
|
setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */
|
||||||
setobj2s(L, L->top++, p1); /* 1st argument */
|
setobj2s(L, L->top++, p1); /* 1st argument */
|
||||||
setobj2s(L, L->top++, p2); /* 2nd argument */
|
setobj2s(L, L->top++, p2); /* 2nd argument */
|
||||||
if (!hasres) /* no result? 'p3' is third 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) {
|
switch (event) {
|
||||||
case TM_CONCAT:
|
case TM_CONCAT:
|
||||||
luaG_concaterror(L, p1, p2);
|
luaG_concaterror(L, p1, p2);
|
||||||
case TM_IDIV: case TM_BAND: case TM_BOR: case TM_BXOR:
|
/* call never returns, but to avoid warnings: *//* FALLTHROUGH */
|
||||||
|
case TM_BAND: case TM_BOR: case TM_BXOR:
|
||||||
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
||||||
lua_Number dummy;
|
lua_Number dummy;
|
||||||
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
|
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
|
||||||
luaG_tointerror(L, p1, p2);
|
luaG_tointerror(L, p1, p2);
|
||||||
/* else go through */
|
else
|
||||||
|
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
|
||||||
}
|
}
|
||||||
|
/* calls never return, but to avoid warnings: *//* FALLTHROUGH */
|
||||||
default:
|
default:
|
||||||
luaG_aritherror(L, p1, p2);
|
luaG_opinterror(L, p1, p2, "perform arithmetic on");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -21,7 +21,7 @@ typedef enum {
|
|||||||
TM_GC,
|
TM_GC,
|
||||||
TM_MODE,
|
TM_MODE,
|
||||||
TM_LEN,
|
TM_LEN,
|
||||||
TM_EQ, /* last tag method with `fast' access */
|
TM_EQ, /* last tag method with fast access */
|
||||||
TM_ADD,
|
TM_ADD,
|
||||||
TM_SUB,
|
TM_SUB,
|
||||||
TM_MUL,
|
TM_MUL,
|
||||||
|
|||||||
67
lua.c
67
lua.c
@@ -1,17 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lua.c,v 1.216 2014/10/20 18:19:26 roberto Exp roberto $
|
** $Id: lua.c,v 1.224 2015/03/10 14:15:06 roberto Exp roberto $
|
||||||
** Lua stand-alone interpreter
|
** Lua stand-alone interpreter
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define lua_c
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define lua_c
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -50,14 +52,14 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#define lua_stdin_is_tty() isatty(0)
|
#define lua_stdin_is_tty() isatty(0)
|
||||||
|
|
||||||
#elif defined(LUA_WIN) /* }{ */
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
|
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ANSI definition */
|
/* ISO C definition */
|
||||||
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
@@ -78,9 +80,7 @@
|
|||||||
#include <readline/readline.h>
|
#include <readline/readline.h>
|
||||||
#include <readline/history.h>
|
#include <readline/history.h>
|
||||||
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
||||||
#define lua_saveline(L,idx) \
|
#define lua_saveline(L,line) ((void)L, add_history(line))
|
||||||
if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \
|
|
||||||
add_history(lua_tostring(L, idx)); /* add it to history */
|
|
||||||
#define lua_freeline(L,b) ((void)L, free(b))
|
#define lua_freeline(L,b) ((void)L, free(b))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
@@ -88,7 +88,7 @@
|
|||||||
#define lua_readline(L,b,p) \
|
#define lua_readline(L,b,p) \
|
||||||
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
||||||
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
||||||
#define lua_saveline(L,idx) { (void)L; (void)idx; }
|
#define lua_saveline(L,line) { (void)L; (void)line; }
|
||||||
#define lua_freeline(L,b) { (void)L; (void)b; }
|
#define lua_freeline(L,b) { (void)L; (void)b; }
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
@@ -126,12 +126,12 @@ static void laction (int i) {
|
|||||||
|
|
||||||
|
|
||||||
static void print_usage (const char *badoption) {
|
static void print_usage (const char *badoption) {
|
||||||
luai_writestringerror("%s: ", progname);
|
lua_writestringerror("%s: ", progname);
|
||||||
if (badoption[1] == 'e' || badoption[1] == 'l')
|
if (badoption[1] == 'e' || badoption[1] == 'l')
|
||||||
luai_writestringerror("'%s' needs argument\n", badoption);
|
lua_writestringerror("'%s' needs argument\n", badoption);
|
||||||
else
|
else
|
||||||
luai_writestringerror("unrecognized option '%s'\n", badoption);
|
lua_writestringerror("unrecognized option '%s'\n", badoption);
|
||||||
luai_writestringerror(
|
lua_writestringerror(
|
||||||
"usage: %s [options] [script [args]]\n"
|
"usage: %s [options] [script [args]]\n"
|
||||||
"Available options are:\n"
|
"Available options are:\n"
|
||||||
" -e stat execute string 'stat'\n"
|
" -e stat execute string 'stat'\n"
|
||||||
@@ -151,8 +151,8 @@ static void print_usage (const char *badoption) {
|
|||||||
** (if present)
|
** (if present)
|
||||||
*/
|
*/
|
||||||
static void l_message (const char *pname, const char *msg) {
|
static void l_message (const char *pname, const char *msg) {
|
||||||
if (pname) luai_writestringerror("%s: ", pname);
|
if (pname) lua_writestringerror("%s: ", pname);
|
||||||
luai_writestringerror("%s\n", msg);
|
lua_writestringerror("%s\n", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -208,8 +208,8 @@ static int docall (lua_State *L, int narg, int nres) {
|
|||||||
|
|
||||||
|
|
||||||
static void print_version (void) {
|
static void print_version (void) {
|
||||||
luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
||||||
luai_writeline();
|
lua_writeline();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -313,11 +313,11 @@ static int pushline (lua_State *L, int firstline) {
|
|||||||
lua_pop(L, 1); /* remove prompt */
|
lua_pop(L, 1); /* remove prompt */
|
||||||
l = strlen(b);
|
l = strlen(b);
|
||||||
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
||||||
b[l-1] = '\0'; /* remove it */
|
b[--l] = '\0'; /* remove it */
|
||||||
if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
|
if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
|
||||||
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
||||||
else
|
else
|
||||||
lua_pushstring(L, b);
|
lua_pushlstring(L, b, l);
|
||||||
lua_freeline(L, b);
|
lua_freeline(L, b);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -334,8 +334,12 @@ static int addreturn (lua_State *L) {
|
|||||||
lua_pushvalue(L, -2); /* duplicate line */
|
lua_pushvalue(L, -2); /* duplicate line */
|
||||||
lua_concat(L, 2); /* new line is "return ..." */
|
lua_concat(L, 2); /* new line is "return ..." */
|
||||||
line = lua_tolstring(L, -1, &len);
|
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 */
|
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
|
else
|
||||||
lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */
|
lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */
|
||||||
return status;
|
return status;
|
||||||
@@ -350,8 +354,10 @@ static int multiline (lua_State *L) {
|
|||||||
size_t len;
|
size_t len;
|
||||||
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
||||||
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
||||||
if (!incomplete(L, status) || !pushline(L, 0))
|
if (!incomplete(L, status) || !pushline(L, 0)) {
|
||||||
|
lua_saveline(L, line); /* keep history */
|
||||||
return status; /* cannot or should not try to add continuation line */
|
return status; /* cannot or should not try to add continuation line */
|
||||||
|
}
|
||||||
lua_pushliteral(L, "\n"); /* add newline... */
|
lua_pushliteral(L, "\n"); /* add newline... */
|
||||||
lua_insert(L, -2); /* ...between the two lines */
|
lua_insert(L, -2); /* ...between the two lines */
|
||||||
lua_concat(L, 3); /* join them */
|
lua_concat(L, 3); /* join them */
|
||||||
@@ -372,7 +378,6 @@ static int loadline (lua_State *L) {
|
|||||||
return -1; /* no input */
|
return -1; /* no input */
|
||||||
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
||||||
status = multiline(L); /* try as command, maybe with continuation lines */
|
status = multiline(L); /* try as command, maybe with continuation lines */
|
||||||
lua_saveline(L, 1); /* keep history */
|
|
||||||
lua_remove(L, 1); /* remove line from the stack */
|
lua_remove(L, 1); /* remove line from the stack */
|
||||||
lua_assert(lua_gettop(L) == 1);
|
lua_assert(lua_gettop(L) == 1);
|
||||||
return status;
|
return status;
|
||||||
@@ -410,7 +415,7 @@ static void doREPL (lua_State *L) {
|
|||||||
else report(L, status);
|
else report(L, status);
|
||||||
}
|
}
|
||||||
lua_settop(L, 0); /* clear stack */
|
lua_settop(L, 0); /* clear stack */
|
||||||
luai_writeline();
|
lua_writeline();
|
||||||
progname = oldprogname;
|
progname = oldprogname;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,8 +425,7 @@ static void doREPL (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
static int pushargs (lua_State *L) {
|
static int pushargs (lua_State *L) {
|
||||||
int i, n;
|
int i, n;
|
||||||
lua_getglobal(L, "arg");
|
if (lua_getglobal(L, "arg") != LUA_TTABLE)
|
||||||
if (!lua_istable(L, -1))
|
|
||||||
luaL_error(L, "'arg' is not a table");
|
luaL_error(L, "'arg' is not a table");
|
||||||
n = (int)luaL_len(L, -1);
|
n = (int)luaL_len(L, -1);
|
||||||
luaL_checkstack(L, n + 3, "too many arguments to script");
|
luaL_checkstack(L, n + 3, "too many arguments to script");
|
||||||
@@ -481,14 +485,14 @@ static int collectargs (char **argv, int *first) {
|
|||||||
args |= has_E;
|
args |= has_E;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
args |= has_i; /* goes through (-i implies -v) */
|
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
|
||||||
case 'v':
|
case 'v':
|
||||||
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
||||||
return has_error; /* invalid option */
|
return has_error; /* invalid option */
|
||||||
args |= has_v;
|
args |= has_v;
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
args |= has_e; /* go through */
|
args |= has_e; /* FALLTHROUGH */
|
||||||
case 'l': /* both options need an argument */
|
case 'l': /* both options need an argument */
|
||||||
if (argv[i][2] == '\0') { /* no concatenated argument? */
|
if (argv[i][2] == '\0') { /* no concatenated argument? */
|
||||||
i++; /* try next 'argv' */
|
i++; /* try next 'argv' */
|
||||||
@@ -512,17 +516,16 @@ static int collectargs (char **argv, int *first) {
|
|||||||
static int runargs (lua_State *L, char **argv, int n) {
|
static int runargs (lua_State *L, char **argv, int n) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i < n; i++) {
|
for (i = 1; i < n; i++) {
|
||||||
int status;
|
|
||||||
int option = argv[i][1];
|
int option = argv[i][1];
|
||||||
lua_assert(argv[i][0] == '-'); /* already checked */
|
lua_assert(argv[i][0] == '-'); /* already checked */
|
||||||
if (option == 'e' || option == 'l') {
|
if (option == 'e' || option == 'l') {
|
||||||
|
int status;
|
||||||
const char *extra = argv[i] + 2; /* both options need an argument */
|
const char *extra = argv[i] + 2; /* both options need an argument */
|
||||||
if (*extra == '\0') extra = argv[++i];
|
if (*extra == '\0') extra = argv[++i];
|
||||||
lua_assert(extra != NULL);
|
lua_assert(extra != NULL);
|
||||||
if (option == 'e')
|
status = (option == 'e')
|
||||||
status = dostring(L, extra, "=(command line)");
|
? dostring(L, extra, "=(command line)")
|
||||||
else
|
: dolibrary(L, extra);
|
||||||
status = dolibrary(L, extra);
|
|
||||||
if (status != LUA_OK) return 0;
|
if (status != LUA_OK) return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
43
lua.h
43
lua.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lua.h,v 1.318 2014/10/15 14:27:40 roberto Exp roberto $
|
** $Id: lua.h,v 1.327 2015/03/10 14:17:51 roberto Exp roberto $
|
||||||
** Lua - A Scripting Language
|
** Lua - A Scripting Language
|
||||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||||
** See Copyright Notice at the end of this file
|
** See Copyright Notice at the end of this file
|
||||||
@@ -19,25 +19,27 @@
|
|||||||
#define LUA_VERSION_MAJOR "5"
|
#define LUA_VERSION_MAJOR "5"
|
||||||
#define LUA_VERSION_MINOR "3"
|
#define LUA_VERSION_MINOR "3"
|
||||||
#define LUA_VERSION_NUM 503
|
#define LUA_VERSION_NUM 503
|
||||||
#define LUA_VERSION_RELEASE "beta"
|
#define LUA_VERSION_RELEASE "1"
|
||||||
|
|
||||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2014 Lua.org, PUC-Rio"
|
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio"
|
||||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
||||||
|
|
||||||
|
|
||||||
/* mark for precompiled code ('<esc>Lua') */
|
/* mark for precompiled code ('<esc>Lua') */
|
||||||
#define LUA_SIGNATURE "\033Lua"
|
#define LUA_SIGNATURE "\x1bLua"
|
||||||
|
|
||||||
/* option for multiple returns in 'lua_pcall' and 'lua_call' */
|
/* option for multiple returns in 'lua_pcall' and 'lua_call' */
|
||||||
#define LUA_MULTRET (-1)
|
#define LUA_MULTRET (-1)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** pseudo-indices
|
** Pseudo-indices
|
||||||
|
** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty
|
||||||
|
** space after that to help overflow detection)
|
||||||
*/
|
*/
|
||||||
#define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX
|
#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000)
|
||||||
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
|
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
|
||||||
|
|
||||||
|
|
||||||
@@ -159,7 +161,7 @@ LUA_API void (lua_settop) (lua_State *L, int idx);
|
|||||||
LUA_API void (lua_pushvalue) (lua_State *L, int idx);
|
LUA_API void (lua_pushvalue) (lua_State *L, int idx);
|
||||||
LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
|
LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
|
||||||
LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
|
LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
|
||||||
LUA_API int (lua_checkstack) (lua_State *L, int sz);
|
LUA_API int (lua_checkstack) (lua_State *L, int n);
|
||||||
|
|
||||||
LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
|
LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
|
||||||
|
|
||||||
@@ -222,7 +224,7 @@ LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
|
|||||||
LUA_API void (lua_pushnil) (lua_State *L);
|
LUA_API void (lua_pushnil) (lua_State *L);
|
||||||
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
|
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
|
||||||
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
||||||
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l);
|
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len);
|
||||||
LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
|
LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
|
||||||
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
|
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
|
||||||
va_list argp);
|
va_list argp);
|
||||||
@@ -236,7 +238,7 @@ LUA_API int (lua_pushthread) (lua_State *L);
|
|||||||
/*
|
/*
|
||||||
** get functions (Lua -> stack)
|
** get functions (Lua -> stack)
|
||||||
*/
|
*/
|
||||||
LUA_API int (lua_getglobal) (lua_State *L, const char *var);
|
LUA_API int (lua_getglobal) (lua_State *L, const char *name);
|
||||||
LUA_API int (lua_gettable) (lua_State *L, int idx);
|
LUA_API int (lua_gettable) (lua_State *L, int idx);
|
||||||
LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
|
LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
|
||||||
LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
|
LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
|
||||||
@@ -253,7 +255,7 @@ LUA_API int (lua_getuservalue) (lua_State *L, int idx);
|
|||||||
/*
|
/*
|
||||||
** set functions (stack -> Lua)
|
** set functions (stack -> Lua)
|
||||||
*/
|
*/
|
||||||
LUA_API void (lua_setglobal) (lua_State *L, const char *var);
|
LUA_API void (lua_setglobal) (lua_State *L, const char *name);
|
||||||
LUA_API void (lua_settable) (lua_State *L, int idx);
|
LUA_API void (lua_settable) (lua_State *L, int idx);
|
||||||
LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
|
LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
|
||||||
LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
|
LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
|
||||||
@@ -276,8 +278,7 @@ LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
|
|||||||
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
|
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
|
||||||
|
|
||||||
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
|
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
|
||||||
const char *chunkname,
|
const char *chunkname, const char *mode);
|
||||||
const char *mode);
|
|
||||||
|
|
||||||
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
||||||
|
|
||||||
@@ -285,12 +286,13 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
|||||||
/*
|
/*
|
||||||
** coroutine functions
|
** coroutine functions
|
||||||
*/
|
*/
|
||||||
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
|
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
|
||||||
lua_KFunction k);
|
lua_KFunction k);
|
||||||
|
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
|
||||||
|
LUA_API int (lua_status) (lua_State *L);
|
||||||
|
LUA_API int (lua_isyieldable) (lua_State *L);
|
||||||
|
|
||||||
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
|
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
|
||||||
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
|
|
||||||
LUA_API int (lua_status) (lua_State *L);
|
|
||||||
LUA_API int lua_isyieldable (lua_State *L);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -356,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_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
|
||||||
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
|
#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
|
||||||
|
|
||||||
#define lua_pushliteral(L, s) \
|
#define lua_pushliteral(L, s) lua_pushstring(L, "" s)
|
||||||
lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
|
|
||||||
|
|
||||||
#define lua_pushglobaltable(L) \
|
#define lua_pushglobaltable(L) \
|
||||||
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
|
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
|
||||||
@@ -382,7 +383,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
|||||||
#if defined(LUA_COMPAT_APIINTCASTS)
|
#if defined(LUA_COMPAT_APIINTCASTS)
|
||||||
|
|
||||||
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
||||||
#define lua_tounsignedx(L,i,is) ((lua_Integer)lua_tointegerx(L,i,is))
|
#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is))
|
||||||
#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
|
#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -459,7 +460,7 @@ struct lua_Debug {
|
|||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Copyright (C) 1994-2014 Lua.org, PUC-Rio.
|
* Copyright (C) 1994-2015 Lua.org, PUC-Rio.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
|
|||||||
631
luaconf.h
631
luaconf.h
@@ -1,130 +1,174 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: luaconf.h,v 1.219 2014/10/20 16:32:05 roberto Exp roberto $
|
** $Id: luaconf.h,v 1.250 2015/04/03 18:41:57 roberto Exp roberto $
|
||||||
** Configuration file for Lua
|
** Configuration file for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef lconfig_h
|
#ifndef luaconf_h
|
||||||
#define lconfig_h
|
#define luaconf_h
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** ==================================================================
|
** ===================================================================
|
||||||
** Search for "@@" to find all configurable definitions.
|
** Search for "@@" to find all configurable definitions.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {====================================================================
|
||||||
@@ LUA_INT_INT / LUA_INT_LONG / LUA_INT_LONGLONG defines type for
|
** System Configuration: macros to adapt (if needed) Lua to some
|
||||||
@@ Lua integers; you must define one of them.
|
** particular platform, for instance compiling it with 32-bit numbers or
|
||||||
@@ LUA_REAL_FLOAT / LUA_REAL_DOUBLE / LUA_REAL_LONGDOUBLE defines
|
** restricting it to C89.
|
||||||
@@ type for Lua floats. You must define one of them.
|
|
||||||
**
|
|
||||||
** These definitions set the numeric types for Lua. Lua should work fine
|
|
||||||
** with any mix of these previous options. The usual configurations
|
|
||||||
** are 64-bit integers and floats (the default) and 32-bit integers and
|
|
||||||
** floats (Small Lua, for restricted platforms).
|
|
||||||
**
|
|
||||||
** Note that C compilers not compliant with C99 may not have
|
|
||||||
** support for 'long long'. In that case, you should not use option
|
|
||||||
** 'LUA_INT_LONGLONG'; use instead option 'LUA_32BITS' for Small Lua
|
|
||||||
** (see below), or LUA_INT_LONG plus LUA_REAL_DOUBLE for an interpreter
|
|
||||||
** with 32-bit integers and double floating-point numbers.
|
|
||||||
** =====================================================================
|
** =====================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Just uncomment the next line for Small Lua; you can also define
|
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You
|
||||||
** LUA_32BITS in the make file, but changing here you ensure that
|
** can also define LUA_32BITS in the make file, but changing here you
|
||||||
** all software connected to Lua will be compiled with the same
|
** ensure that all software connected to Lua will be compiled with the
|
||||||
** configuration.
|
** same configuration.
|
||||||
*/
|
*/
|
||||||
/* #define LUA_32BITS */
|
/* #define LUA_32BITS */
|
||||||
|
|
||||||
#if !defined(LUA_32BITS) && !defined(LUA_ANSI)
|
|
||||||
#define LUA_INT_LONGLONG
|
|
||||||
#define LUA_REAL_DOUBLE
|
|
||||||
#else /* Lua 32 bits */
|
|
||||||
#define LUA_INT_LONG
|
|
||||||
#define LUA_REAL_FLOAT
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/*
|
||||||
|
@@ LUA_USE_C89 controls the use of non-ISO-C89 features.
|
||||||
|
** Define it if you want Lua to avoid the use of a few C99 features
|
||||||
|
** or Windows-specific features on Windows.
|
||||||
|
*/
|
||||||
|
/* #define LUA_USE_C89 */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_ANSI controls the use of non-ansi features.
|
** By default, Lua on Windows use (some) specific Windows features
|
||||||
** CHANGE it (define it) if you want Lua to avoid the use of any
|
|
||||||
** non-ansi feature or library.
|
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_ANSI) && defined(__STRICT_ANSI__)
|
#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE)
|
||||||
#define LUA_ANSI
|
#define LUA_USE_WINDOWS /* enable goodies for regular Windows */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE)
|
#if defined(LUA_USE_WINDOWS)
|
||||||
#define LUA_WIN /* enable goodies for regular Windows platforms */
|
#define LUA_DL_DLL /* enable support for DLL */
|
||||||
|
#define LUA_USE_C89 /* broadly, Windows is C89 */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(LUA_WIN)
|
|
||||||
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ANSI C functions */
|
|
||||||
#define LUA_DL_DLL
|
|
||||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_LINUX)
|
#if defined(LUA_USE_LINUX)
|
||||||
#define LUA_USE_C99
|
|
||||||
#define LUA_USE_POSIX
|
#define LUA_USE_POSIX
|
||||||
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
|
||||||
#define LUA_USE_READLINE /* needs some extra libraries */
|
#define LUA_USE_READLINE /* needs some extra libraries */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_MACOSX)
|
#if defined(LUA_USE_MACOSX)
|
||||||
#define LUA_USE_C99
|
|
||||||
#define LUA_USE_POSIX
|
#define LUA_USE_POSIX
|
||||||
#define LUA_USE_DLOPEN /* does not need -ldl */
|
#define LUA_USE_DLOPEN /* MacOS does not need -ldl */
|
||||||
#define LUA_USE_READLINE /* needs an extra library: -lreadline */
|
#define LUA_USE_READLINE /* needs an extra library: -lreadline */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_USE_C99 includes all functionality that depends on C 99.
|
@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for
|
||||||
** CHANGE it (define it) if your system is compatible.
|
** C89 ('long' and 'double'); Windows always has '__int64', so it does
|
||||||
|
** not need to use this case.
|
||||||
*/
|
*/
|
||||||
#if defined(LUA_USE_C99)
|
#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS)
|
||||||
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
|
#define LUA_C89_NUMBERS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'.
|
||||||
|
*/
|
||||||
|
/* avoid undefined shifts */
|
||||||
|
#if ((INT_MAX >> 15) >> 15) >= 1
|
||||||
|
#define LUAI_BITSINT 32
|
||||||
|
#else
|
||||||
|
/* 'int' always must have at least 16 bits */
|
||||||
|
#define LUAI_BITSINT 16
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_USE_POSIX includes all functionality listed as X/Open System
|
@@ LUA_INT_TYPE defines the type for Lua integers.
|
||||||
@@ Interfaces Extension (XSI).
|
@@ LUA_FLOAT_TYPE defines the type for Lua floats.
|
||||||
** CHANGE it (define it) if your system is XSI compatible.
|
** Lua should work fine with any mix of these options (if supported
|
||||||
|
** by your C compiler). The usual configurations are 64-bit integers
|
||||||
|
** and 'double' (the default), 32-bit integers and 'float' (for
|
||||||
|
** restricted platforms), and 'long'/'double' (for C compilers not
|
||||||
|
** compliant with C99, which may not have support for 'long long').
|
||||||
*/
|
*/
|
||||||
#if defined(LUA_USE_POSIX)
|
|
||||||
|
/* predefined options for LUA_INT_TYPE */
|
||||||
|
#define LUA_INT_INT 1
|
||||||
|
#define LUA_INT_LONG 2
|
||||||
|
#define LUA_INT_LONGLONG 3
|
||||||
|
|
||||||
|
/* predefined options for LUA_FLOAT_TYPE */
|
||||||
|
#define LUA_FLOAT_FLOAT 1
|
||||||
|
#define LUA_FLOAT_DOUBLE 2
|
||||||
|
#define LUA_FLOAT_LONGDOUBLE 3
|
||||||
|
|
||||||
|
#if defined(LUA_32BITS) /* { */
|
||||||
|
/*
|
||||||
|
** 32-bit integers and 'float'
|
||||||
|
*/
|
||||||
|
#if LUAI_BITSINT >= 32 /* use 'int' if big enough */
|
||||||
|
#define LUA_INT_TYPE LUA_INT_INT
|
||||||
|
#else /* otherwise use 'long' */
|
||||||
|
#define LUA_INT_TYPE LUA_INT_LONG
|
||||||
|
#endif
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT
|
||||||
|
|
||||||
|
#elif defined(LUA_C89_NUMBERS) /* }{ */
|
||||||
|
/*
|
||||||
|
** largest types available for C89 ('long' and 'double')
|
||||||
|
*/
|
||||||
|
#define LUA_INT_TYPE LUA_INT_LONG
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** default configuration for 64-bit Lua ('long long' and 'double')
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_INT_TYPE)
|
||||||
|
#define LUA_INT_TYPE LUA_INT_LONGLONG
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(LUA_FLOAT_TYPE)
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** Configuration for Paths.
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
|
@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
|
||||||
@@ Lua libraries.
|
** Lua libraries.
|
||||||
@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
|
@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
|
||||||
@@ C libraries.
|
** C libraries.
|
||||||
** CHANGE them if your machine has a non-conventional directory
|
** CHANGE them if your machine has a non-conventional directory
|
||||||
** hierarchy or if you want to install your libraries in
|
** hierarchy or if you want to install your libraries in
|
||||||
** non-conventional directories.
|
** non-conventional directories.
|
||||||
*/
|
*/
|
||||||
#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||||
#if defined(_WIN32) /* { */
|
#if defined(_WIN32) /* { */
|
||||||
/*
|
/*
|
||||||
** In Windows, any exclamation mark ('!') in the path is replaced by the
|
** In Windows, any exclamation mark ('!') in the path is replaced by the
|
||||||
** path of the directory of the executable file of the current process.
|
** path of the directory of the executable file of the current process.
|
||||||
@@ -135,7 +179,7 @@
|
|||||||
#define LUA_PATH_DEFAULT \
|
#define LUA_PATH_DEFAULT \
|
||||||
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
||||||
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
|
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
|
||||||
LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
|
LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
|
||||||
".\\?.lua;" ".\\?\\init.lua"
|
".\\?.lua;" ".\\?\\init.lua"
|
||||||
#define LUA_CPATH_DEFAULT \
|
#define LUA_CPATH_DEFAULT \
|
||||||
LUA_CDIR"?.dll;" \
|
LUA_CDIR"?.dll;" \
|
||||||
@@ -167,14 +211,14 @@
|
|||||||
#define LUA_DIRSEP "/"
|
#define LUA_DIRSEP "/"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_ENV is the name of the variable that holds the current
|
** {==================================================================
|
||||||
@@ environment, used to access global names.
|
** Marks for exported symbols in the C code
|
||||||
** CHANGE it if you do not like this name.
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
#define LUA_ENV "_ENV"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_API is a mark for all core API functions.
|
@@ LUA_API is a mark for all core API functions.
|
||||||
@@ -207,10 +251,10 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUAI_FUNC is a mark for all extern functions that are not to be
|
@@ LUAI_FUNC is a mark for all extern functions that are not to be
|
||||||
@@ exported to outside modules.
|
** exported to outside modules.
|
||||||
@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables
|
@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables
|
||||||
@@ that are not to be exported to outside modules (LUAI_DDEF for
|
** that are not to be exported to outside modules (LUAI_DDEF for
|
||||||
@@ definitions and LUAI_DDEC for declarations).
|
** definitions and LUAI_DDEC for declarations).
|
||||||
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
||||||
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
||||||
** when Lua is compiled as a shared library. Not all elf targets support
|
** when Lua is compiled as a shared library. Not all elf targets support
|
||||||
@@ -222,82 +266,14 @@
|
|||||||
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||||
defined(__ELF__) /* { */
|
defined(__ELF__) /* { */
|
||||||
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
||||||
|
#else /* }{ */
|
||||||
|
#define LUAI_FUNC extern
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
#define LUAI_DDEC LUAI_FUNC
|
#define LUAI_DDEC LUAI_FUNC
|
||||||
#define LUAI_DDEF /* empty */
|
#define LUAI_DDEF /* empty */
|
||||||
|
|
||||||
#else /* }{ */
|
/* }================================================================== */
|
||||||
#define LUAI_FUNC extern
|
|
||||||
#define LUAI_DDEC extern
|
|
||||||
#define LUAI_DDEF /* empty */
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
|
|
||||||
** a Lua state with very fast access.
|
|
||||||
** CHANGE it if you need a different size.
|
|
||||||
*/
|
|
||||||
#define LUA_EXTRASPACE (sizeof(void *))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_QL describes how error messages quote program elements.
|
|
||||||
** Lua does not use these macros anymore; they are here for
|
|
||||||
** compatibility only.
|
|
||||||
*/
|
|
||||||
#define LUA_QL(x) "'" x "'"
|
|
||||||
#define LUA_QS LUA_QL("%s")
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
|
||||||
@@ of a function in debug information.
|
|
||||||
** CHANGE it if you want a different size.
|
|
||||||
*/
|
|
||||||
#define LUA_IDSIZE 60
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ luai_writestring/luai_writeline define how 'print' prints its results.
|
|
||||||
** They are only used in libraries and the stand-alone program.
|
|
||||||
*/
|
|
||||||
#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
|
||||||
#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout))
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ luai_writestringerror defines how to print error messages.
|
|
||||||
** (A format string with one argument is enough for Lua...)
|
|
||||||
*/
|
|
||||||
#define luai_writestringerror(s,p) \
|
|
||||||
(fprintf(stderr, (s), (p)), fflush(stderr))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAI_MAXSHORTLEN is the maximum length for short strings, that is,
|
|
||||||
** strings that are internalized. (Cannot be smaller than reserved words
|
|
||||||
** or tags for metamethods, as these strings must be internalized;
|
|
||||||
** #("function") = 8, #("__newindex") = 10.)
|
|
||||||
*/
|
|
||||||
#define LUAI_MAXSHORTLEN 40
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation
|
|
||||||
@@ functions. It must be a numerical type; Lua will use 'intptr_t' if
|
|
||||||
@@ available.
|
|
||||||
*/
|
|
||||||
#if defined (LUA_USE_C99)
|
|
||||||
#include <stdint.h>
|
|
||||||
#if defined (INTPTR_MAX) /* even in C99 this type is optional */
|
|
||||||
#define LUA_KCONTEXT intptr_t
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(LUA_KCONTEXT)
|
|
||||||
/* default definition (the nearest thing to 'intptr_t' in C89) */
|
|
||||||
#define LUA_KCONTEXT ptrdiff_t
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -337,20 +313,15 @@
|
|||||||
*/
|
*/
|
||||||
#define LUA_COMPAT_APIINTCASTS
|
#define LUA_COMPAT_APIINTCASTS
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a
|
|
||||||
@@ a float mark ('.0').
|
|
||||||
** This macro is not on by default even in compatibility mode,
|
|
||||||
** because this is not really an incompatibility.
|
|
||||||
*/
|
|
||||||
/* #define LUA_COMPAT_FLOATSTRING */
|
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_COMPAT_5_1) /* { */
|
#if defined(LUA_COMPAT_5_1) /* { */
|
||||||
|
|
||||||
|
/* Incompatibilities from 5.2 -> 5.3 */
|
||||||
|
#define LUA_COMPAT_MATHLIB
|
||||||
|
#define LUA_COMPAT_APIINTCASTS
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
|
@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
|
||||||
** You can replace it with 'table.unpack'.
|
** You can replace it with 'table.unpack'.
|
||||||
@@ -410,100 +381,46 @@
|
|||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a
|
||||||
|
@@ a float mark ('.0').
|
||||||
|
** This macro is not on by default even in compatibility mode,
|
||||||
|
** because this is not really an incompatibility.
|
||||||
|
*/
|
||||||
|
/* #define LUA_COMPAT_FLOATSTRING */
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAI_BITSINT defines the number of bits in an int.
|
|
||||||
** CHANGE here if Lua cannot automatically detect the number of bits of
|
|
||||||
** your machine. Probably you do not need to change this.
|
|
||||||
*/
|
|
||||||
/* avoid overflows in comparison */
|
|
||||||
#if INT_MAX-20 < 32760 /* { */
|
|
||||||
#define LUAI_BITSINT 16
|
|
||||||
#elif INT_MAX > 2147483640L /* }{ */
|
|
||||||
/* int has at least 32 bits */
|
|
||||||
#define LUAI_BITSINT 32
|
|
||||||
#else /* }{ */
|
|
||||||
#error "you must define LUA_BITSINT with number of bits in an integer"
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_INT32 is an signed integer with exactly 32 bits.
|
|
||||||
@@ LUAI_UMEM is an unsigned integer big enough to count the total
|
|
||||||
@@ memory used by Lua.
|
|
||||||
@@ LUAI_MEM is a signed integer big enough to count the total memory
|
|
||||||
@@ used by Lua.
|
|
||||||
** CHANGE here if for some weird reason the default definitions are not
|
|
||||||
** good enough for your machine. Probably you do not need to change
|
|
||||||
** this.
|
|
||||||
*/
|
|
||||||
#if LUAI_BITSINT >= 32 /* { */
|
|
||||||
#define LUA_INT32 int
|
|
||||||
#define LUAI_UMEM size_t
|
|
||||||
#define LUAI_MEM ptrdiff_t
|
|
||||||
#else /* }{ */
|
|
||||||
/* 16-bit ints */
|
|
||||||
#define LUA_INT32 long
|
|
||||||
#define LUAI_UMEM unsigned long
|
|
||||||
#define LUAI_MEM long
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAI_MAXSTACK limits the size of the Lua stack.
|
|
||||||
** CHANGE it if you need a different limit. This limit is arbitrary;
|
|
||||||
** its only purpose is to stop Lua from consuming unlimited stack
|
|
||||||
** space (and to reserve some numbers for pseudo-indices).
|
|
||||||
*/
|
|
||||||
#if LUAI_BITSINT >= 32
|
|
||||||
#define LUAI_MAXSTACK 1000000
|
|
||||||
#else
|
|
||||||
#define LUAI_MAXSTACK 15000
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* reserve some space for error handling */
|
|
||||||
#define LUAI_FIRSTPSEUDOIDX (-LUAI_MAXSTACK - 1000)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
|
|
||||||
** CHANGE it if it uses too much C-stack space.
|
|
||||||
*/
|
|
||||||
#define LUAL_BUFFERSIZE BUFSIZ
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Configuration for Numbers.
|
** Configuration for Numbers.
|
||||||
** Change these definitions if no predefined LUA_REAL_* / LUA_INT_*
|
** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_*
|
||||||
** satisfy your needs.
|
** satisfy your needs.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_NUMBER is the floating-point type used by Lua.
|
@@ LUA_NUMBER is the floating-point type used by Lua.
|
||||||
**
|
|
||||||
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
|
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
|
||||||
@@ over a floating number.
|
@@ over a floating number.
|
||||||
**
|
@@ l_mathlim(x) corrects limit name 'x' to the proper float type
|
||||||
|
** by prefixing it with one of FLT/DBL/LDBL.
|
||||||
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
||||||
@@ LUA_NUMBER_FMT is the format for writing floats.
|
@@ LUA_NUMBER_FMT is the format for writing floats.
|
||||||
@@ lua_number2str converts a float to a string.
|
@@ lua_number2str converts a float to a string.
|
||||||
**
|
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
|
||||||
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations
|
|
||||||
**
|
|
||||||
@@ lua_str2number converts a decimal numeric string to a number.
|
@@ lua_str2number converts a decimal numeric string to a number.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(LUA_REAL_FLOAT) /* { single float */
|
#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */
|
||||||
|
|
||||||
#define LUA_NUMBER float
|
#define LUA_NUMBER float
|
||||||
|
|
||||||
|
#define l_mathlim(n) (FLT_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN ""
|
#define LUA_NUMBER_FRMLEN ""
|
||||||
@@ -514,10 +431,12 @@
|
|||||||
#define lua_str2number(s,p) strtof((s), (p))
|
#define lua_str2number(s,p) strtof((s), (p))
|
||||||
|
|
||||||
|
|
||||||
#elif defined(LUA_REAL_LONGDOUBLE) /* }{ long double */
|
#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */
|
||||||
|
|
||||||
#define LUA_NUMBER long double
|
#define LUA_NUMBER long double
|
||||||
|
|
||||||
|
#define l_mathlim(n) (LDBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER long double
|
#define LUAI_UACNUMBER long double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN "L"
|
#define LUA_NUMBER_FRMLEN "L"
|
||||||
@@ -527,10 +446,12 @@
|
|||||||
|
|
||||||
#define lua_str2number(s,p) strtold((s), (p))
|
#define lua_str2number(s,p) strtold((s), (p))
|
||||||
|
|
||||||
#elif defined(LUA_REAL_DOUBLE) /* }{ double */
|
#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */
|
||||||
|
|
||||||
#define LUA_NUMBER double
|
#define LUA_NUMBER double
|
||||||
|
|
||||||
|
#define l_mathlim(n) (DBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN ""
|
#define LUA_NUMBER_FRMLEN ""
|
||||||
@@ -540,75 +461,32 @@
|
|||||||
|
|
||||||
#define lua_str2number(s,p) strtod((s), (p))
|
#define lua_str2number(s,p) strtod((s), (p))
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
#error "numeric real type not defined"
|
#error "numeric float type not defined"
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_USE_C99)
|
|
||||||
/* 'strtof' and 'opf' variants for math functions are C99 */
|
|
||||||
#undef l_mathop
|
|
||||||
#undef lua_str2number
|
|
||||||
#define l_mathop(op) (lua_Number)op
|
|
||||||
#define lua_str2number(s,p) ((lua_Number)strtod((s), (p)))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define l_floor(x) (l_mathop(floor)(x))
|
#define l_floor(x) (l_mathop(floor)(x))
|
||||||
|
|
||||||
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
|
#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.
|
** returns 0 if float is not within the range of a lua_Integer.
|
||||||
** (The range comparisons are tricky because of rounding. The tests
|
** (The range comparisons are tricky because of rounding. The tests
|
||||||
** here assume a two-complement representation, where MININTEGER always
|
** here assume a two-complement representation, where MININTEGER always
|
||||||
** has an exact representation as a float; MAXINTEGER may not have one,
|
** has an exact representation as a float; MAXINTEGER may not have one,
|
||||||
** and therefore its conversion to float may have an ill-defined value.)
|
** 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) && \
|
||||||
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
||||||
(*(p) = (LUA_INTEGER)(n), 1))
|
(*(p) = (LUA_INTEGER)(n), 1))
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ The luai_num* macros define the primitive operations over numbers.
|
|
||||||
** They should work for any size of floating numbers.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* the following operations need the math library */
|
|
||||||
#if defined(lobject_c) || defined(lvm_c)
|
|
||||||
#include <math.h>
|
|
||||||
#define luai_nummod(L,a,b,m) \
|
|
||||||
{ (m) = l_mathop(fmod)(a,b); if ((m) != 0 && (a)*(b) < 0) (m) += (b); }
|
|
||||||
#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* these are quite standard operations */
|
|
||||||
#if defined(LUA_CORE)
|
|
||||||
#define luai_numadd(L,a,b) ((a)+(b))
|
|
||||||
#define luai_numsub(L,a,b) ((a)-(b))
|
|
||||||
#define luai_nummul(L,a,b) ((a)*(b))
|
|
||||||
#define luai_numdiv(L,a,b) ((a)/(b))
|
|
||||||
#define luai_numunm(L,a) (-(a))
|
|
||||||
#define luai_numeq(a,b) ((a)==(b))
|
|
||||||
#define luai_numlt(a,b) ((a)<(b))
|
|
||||||
#define luai_numle(a,b) ((a)<=(b))
|
|
||||||
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** The following macro checks whether an operation is not safe to be
|
|
||||||
** performed by the constant folder. It should result in zero only if
|
|
||||||
** the operation is safe.
|
|
||||||
*/
|
|
||||||
#define luai_numinvalidop(op,a,b) 0
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_INTEGER is the integer type used by Lua.
|
@@ LUA_INTEGER is the integer type used by Lua.
|
||||||
@@ -641,7 +519,7 @@
|
|||||||
|
|
||||||
/* now the variable definitions */
|
/* now the variable definitions */
|
||||||
|
|
||||||
#if defined(LUA_INT_INT) /* { int */
|
#if LUA_INT_TYPE == LUA_INT_INT /* { int */
|
||||||
|
|
||||||
#define LUA_INTEGER int
|
#define LUA_INTEGER int
|
||||||
#define LUA_INTEGER_FRMLEN ""
|
#define LUA_INTEGER_FRMLEN ""
|
||||||
@@ -649,7 +527,7 @@
|
|||||||
#define LUA_MAXINTEGER INT_MAX
|
#define LUA_MAXINTEGER INT_MAX
|
||||||
#define LUA_MININTEGER INT_MIN
|
#define LUA_MININTEGER INT_MIN
|
||||||
|
|
||||||
#elif defined(LUA_INT_LONG) /* }{ long */
|
#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */
|
||||||
|
|
||||||
#define LUA_INTEGER long
|
#define LUA_INTEGER long
|
||||||
#define LUA_INTEGER_FRMLEN "l"
|
#define LUA_INTEGER_FRMLEN "l"
|
||||||
@@ -657,21 +535,10 @@
|
|||||||
#define LUA_MAXINTEGER LONG_MAX
|
#define LUA_MAXINTEGER LONG_MAX
|
||||||
#define LUA_MININTEGER LONG_MIN
|
#define LUA_MININTEGER LONG_MIN
|
||||||
|
|
||||||
#elif defined(LUA_INT_LONGLONG) /* }{ long long */
|
#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(LLONG_MAX) /* { */
|
||||||
|
/* use ISO C99 stuff */
|
||||||
#define LUA_INTEGER __int64
|
|
||||||
#define LUA_INTEGER_FRMLEN "I64"
|
|
||||||
|
|
||||||
#define LUA_MAXINTEGER _I64_MAX
|
|
||||||
#define LUA_MININTEGER _I64_MIN
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#if !defined(LLONG_MAX)
|
|
||||||
#error "Compiler does not support 'long long'. See file 'luaconf.h' line 24"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define LUA_INTEGER long long
|
#define LUA_INTEGER long long
|
||||||
#define LUA_INTEGER_FRMLEN "ll"
|
#define LUA_INTEGER_FRMLEN "ll"
|
||||||
@@ -679,27 +546,21 @@
|
|||||||
#define LUA_MAXINTEGER LLONG_MAX
|
#define LUA_MAXINTEGER LLONG_MAX
|
||||||
#define LUA_MININTEGER LLONG_MIN
|
#define LUA_MININTEGER LLONG_MIN
|
||||||
|
|
||||||
#endif
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
|
/* in Windows, can use specific Windows types */
|
||||||
|
|
||||||
#elif defined(LUA_INT_SHORT) /* }{ short int */
|
#define LUA_INTEGER __int64
|
||||||
/*
|
#define LUA_INTEGER_FRMLEN "I64"
|
||||||
** this option is for tests only; it is not particularly useful and
|
|
||||||
** it does not pass the test suit.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LUA_INTEGER short int
|
#define LUA_MAXINTEGER _I64_MAX
|
||||||
#define LUA_INTEGER_FRMLEN ""
|
#define LUA_MININTEGER _I64_MIN
|
||||||
|
|
||||||
#define LUA_MAXINTEGER SHRT_MAX
|
#else /* }{ */
|
||||||
#define LUA_MININTEGER SHRT_MIN
|
|
||||||
|
|
||||||
#undef LUAI_UACINT
|
#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \
|
||||||
#define LUAI_UACINT int
|
or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)"
|
||||||
|
|
||||||
#undef LUAI_MAXSTACK
|
#endif /* } */
|
||||||
#define LUAI_MAXSTACK 15000
|
|
||||||
|
|
||||||
#define l_castS2U(x) ((LUA_UNSIGNED)(unsigned short)(x))
|
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
@@ -710,6 +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
|
** load precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lundump_c
|
#define lundump_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
|
|||||||
25
lutf8lib.c
25
lutf8lib.c
@@ -1,17 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lutf8lib.c,v 1.11 2014/10/01 11:52:33 roberto Exp roberto $
|
** $Id: lutf8lib.c,v 1.14 2015/03/05 16:07:46 roberto Exp roberto $
|
||||||
** Standard library for UTF-8 manipulation
|
** Standard library for UTF-8 manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lutf8lib_c
|
#define lutf8lib_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
@@ -35,7 +38,7 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
|
|||||||
** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
|
** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
|
||||||
*/
|
*/
|
||||||
static const char *utf8_decode (const char *o, int *val) {
|
static const char *utf8_decode (const char *o, int *val) {
|
||||||
static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
|
static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
|
||||||
const unsigned char *s = (const unsigned char *)o;
|
const unsigned char *s = (const unsigned char *)o;
|
||||||
unsigned int c = s[0];
|
unsigned int c = s[0];
|
||||||
unsigned int res = 0; /* final result */
|
unsigned int res = 0; /* final result */
|
||||||
@@ -104,9 +107,9 @@ static int codepoint (lua_State *L) {
|
|||||||
luaL_argcheck(L, posi >= 1, 2, "out of range");
|
luaL_argcheck(L, posi >= 1, 2, "out of range");
|
||||||
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
|
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
|
||||||
if (posi > pose) return 0; /* empty interval; return no values */
|
if (posi > pose) return 0; /* empty interval; return no values */
|
||||||
n = (int)(pose - posi + 1);
|
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
||||||
if (posi + n <= pose) /* (lua_Integer -> int) overflow? */
|
|
||||||
return luaL_error(L, "string slice too long");
|
return luaL_error(L, "string slice too long");
|
||||||
|
n = (int)(pose - posi) + 1;
|
||||||
luaL_checkstack(L, n, "string slice too long");
|
luaL_checkstack(L, n, "string slice too long");
|
||||||
n = 0;
|
n = 0;
|
||||||
se = s + pose;
|
se = s + pose;
|
||||||
@@ -232,7 +235,7 @@ static int iter_codes (lua_State *L) {
|
|||||||
#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
|
#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
|
||||||
|
|
||||||
|
|
||||||
static struct luaL_Reg funcs[] = {
|
static const luaL_Reg funcs[] = {
|
||||||
{"offset", byteoffset},
|
{"offset", byteoffset},
|
||||||
{"codepoint", codepoint},
|
{"codepoint", codepoint},
|
||||||
{"char", utfchar},
|
{"char", utfchar},
|
||||||
@@ -246,7 +249,7 @@ static struct luaL_Reg funcs[] = {
|
|||||||
|
|
||||||
LUAMOD_API int luaopen_utf8 (lua_State *L) {
|
LUAMOD_API int luaopen_utf8 (lua_State *L) {
|
||||||
luaL_newlib(L, funcs);
|
luaL_newlib(L, funcs);
|
||||||
lua_pushliteral(L, UTF8PATT);
|
lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
|
||||||
lua_setfield(L, -2, "charpattern");
|
lua_setfield(L, -2, "charpattern");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
16
lvm.h
16
lvm.h
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lvm.h,v 2.33 2014/07/30 14:42:44 roberto Exp roberto $
|
** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 roberto Exp roberto $
|
||||||
** Lua virtual machine
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -27,11 +27,21 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** You can define LUA_FLOORN2I if you want to convert floats to integers
|
||||||
|
** by flooring them (instead of raising an error if they are not
|
||||||
|
** integral values)
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_FLOORN2I)
|
||||||
|
#define LUA_FLOORN2I 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define tonumber(o,n) \
|
#define tonumber(o,n) \
|
||||||
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
||||||
|
|
||||||
#define tointeger(o,i) \
|
#define tointeger(o,i) \
|
||||||
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i))
|
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))
|
||||||
|
|
||||||
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
|
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
|
||||||
|
|
||||||
@@ -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_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
||||||
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
||||||
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
|
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
|
||||||
LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p);
|
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
|
||||||
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
|
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
|
||||||
StkId val);
|
StkId val);
|
||||||
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
|
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
|
** Buffered streams
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define lzio_c
|
#define lzio_c
|
||||||
#define LUA_CORE
|
#define LUA_CORE
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
|
|||||||
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
|
** Buffered streams
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -37,7 +37,8 @@ typedef struct Mbuffer {
|
|||||||
|
|
||||||
|
|
||||||
#define luaZ_resizebuffer(L, buff, size) \
|
#define luaZ_resizebuffer(L, buff, size) \
|
||||||
(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
|
((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \
|
||||||
|
(buff)->buffsize, size), \
|
||||||
(buff)->buffsize = size)
|
(buff)->buffsize = size)
|
||||||
|
|
||||||
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
|
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
|
||||||
|
|||||||
151
makefile
151
makefile
@@ -5,19 +5,19 @@
|
|||||||
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
||||||
|
|
||||||
# Warnings valid for both C and C++
|
# Warnings valid for both C and C++
|
||||||
CWARNSCPP=-pedantic -Wno-long-long \
|
CWARNSCPP= \
|
||||||
-Wall -Wextra \
|
-pedantic \
|
||||||
|
-Wextra \
|
||||||
-Waggregate-return \
|
-Waggregate-return \
|
||||||
-Wcast-align \
|
|
||||||
-Wdisabled-optimization \
|
|
||||||
-Wshadow \
|
-Wshadow \
|
||||||
-Wsign-compare \
|
-Wsign-compare \
|
||||||
-Wundef \
|
-Wundef \
|
||||||
-Wwrite-strings \
|
-Wwrite-strings \
|
||||||
-Wredundant-decls \
|
-Wredundant-decls \
|
||||||
-Wdisabled-optimization \
|
-Wdisabled-optimization \
|
||||||
-Wstrict-aliasing \
|
-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 \
|
# -Wsign-conversion \
|
||||||
# -Wconversion \
|
# -Wconversion \
|
||||||
# -Wlogical-op \
|
# -Wlogical-op \
|
||||||
@@ -27,21 +27,21 @@ CWARNSCPP=-pedantic -Wno-long-long \
|
|||||||
|
|
||||||
# The next warnings are neither valid nor needed for C++
|
# The next warnings are neither valid nor needed for C++
|
||||||
CWARNSC= -Wdeclaration-after-statement \
|
CWARNSC= -Wdeclaration-after-statement \
|
||||||
-Wmissing-prototypes \
|
-Wmissing-prototypes \
|
||||||
-Wnested-externs \
|
-Wnested-externs \
|
||||||
-Wstrict-prototypes \
|
-Wstrict-prototypes \
|
||||||
-Wc++-compat \
|
-Wc++-compat \
|
||||||
-Wold-style-declaration \
|
|
||||||
-Wold-style-definition \
|
-Wold-style-definition \
|
||||||
|
-Wold-style-declaration \
|
||||||
|
|
||||||
CWARNS= $(CWARNSCPP) $(CWARNSC)
|
CWARNS= $(CWARNSCPP) $(CWARNSC)
|
||||||
|
|
||||||
|
|
||||||
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
||||||
# -g -DLUA_USER_H='"ltests.h"'
|
# -g -DLUA_USER_H='"ltests.h"'
|
||||||
# -fomit-frame-pointer #-pg -malign-double
|
# -fomit-frame-pointer #-pg -malign-double
|
||||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||||
TESTS= -DLUA_USER_H='"ltests.h"'
|
# TESTS= -DLUA_USER_H='"ltests.h"'
|
||||||
|
|
||||||
# -mtune=native -fomit-frame-pointer
|
# -mtune=native -fomit-frame-pointer
|
||||||
LOCAL = $(TESTS) $(CWARNS) -g
|
LOCAL = $(TESTS) $(CWARNS) -g
|
||||||
@@ -49,7 +49,7 @@ LOCAL = $(TESTS) $(CWARNS) -g
|
|||||||
|
|
||||||
|
|
||||||
# enable Linux goodies
|
# enable Linux goodies
|
||||||
MYCFLAGS= $(LOCAL) -DLUA_USE_LINUX -DLUA_COMPAT_5_2
|
MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_COMPAT_5_2
|
||||||
MYLDFLAGS= $(LOCAL) -Wl,-E
|
MYLDFLAGS= $(LOCAL) -Wl,-E
|
||||||
MYLIBS= -ldl -lreadline -lhistory -lncurses
|
MYLIBS= -ldl -lreadline -lhistory -lncurses
|
||||||
|
|
||||||
@@ -126,66 +126,71 @@ $(ALL_O): makefile
|
|||||||
# DO NOT EDIT
|
# DO NOT EDIT
|
||||||
# automatically made with 'gcc -MM l*.c'
|
# automatically made with 'gcc -MM l*.c'
|
||||||
|
|
||||||
lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \
|
lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \
|
||||||
|
ltable.h lundump.h lvm.h
|
||||||
|
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h
|
||||||
|
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lbitlib.o: lbitlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||||
|
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||||
|
ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||||
|
lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h
|
||||||
|
ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
|
ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
|
lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \
|
||||||
|
ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h
|
||||||
|
ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
|
||||||
|
lparser.h lstring.h ltable.h lundump.h lvm.h
|
||||||
|
ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \
|
||||||
|
ltm.h lzio.h lmem.h lundump.h
|
||||||
|
lfunc.o: lfunc.c lprefix.h lua.h luaconf.h lfunc.h lobject.h llimits.h \
|
||||||
|
lgc.h lstate.h ltm.h lzio.h lmem.h
|
||||||
|
lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h 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
|
lvm.h
|
||||||
lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h
|
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h
|
||||||
lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h
|
loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h
|
lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||||
lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
|
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||||
lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
|
ldo.h lfunc.h lstring.h lgc.h ltable.h
|
||||||
lstring.h ltable.h lvm.h
|
lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \
|
||||||
lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h
|
lstring.h ltable.h
|
||||||
ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
|
lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h llimits.h \
|
||||||
ldebug.o: ldebug.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \
|
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
||||||
ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h ldebug.h ldo.h \
|
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lfunc.h lstring.h lgc.h ltable.h lvm.h
|
ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h llimits.h lstate.h \
|
||||||
ldo.o: ldo.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \
|
lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||||
lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h \
|
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lstring.h ltable.h lundump.h lvm.h
|
ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \
|
lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \
|
||||||
lzio.h lmem.h lundump.h
|
lparser.h lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h \
|
||||||
lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \
|
lualib.h
|
||||||
lstate.h ltm.h lzio.h lmem.h
|
ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h llimits.h lstate.h \
|
||||||
lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h
|
||||||
lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
|
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h
|
lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h llimits.h \
|
||||||
liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h
|
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
|
||||||
llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \
|
lundump.h
|
||||||
lstate.h ltm.h lzio.h lmem.h lgc.h llex.h lparser.h lstring.h ltable.h
|
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
|
lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h llimits.h lstate.h \
|
||||||
lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
|
lobject.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \
|
||||||
ltm.h lzio.h lmem.h ldo.h lgc.h
|
ltable.h lvm.h
|
||||||
loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
|
lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
|
||||||
lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \
|
lobject.h ltm.h lzio.h
|
||||||
lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h
|
|
||||||
lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h
|
|
||||||
loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
|
|
||||||
lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lfunc.h \
|
|
||||||
lstring.h lgc.h ltable.h
|
|
||||||
lstate.o: lstate.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \
|
|
||||||
ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h lstring.h \
|
|
||||||
ltable.h
|
|
||||||
lstring.o: lstring.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
|
||||||
lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
|
|
||||||
ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
|
||||||
ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
ltests.o: ltests.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \
|
|
||||||
ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h lparser.h \
|
|
||||||
lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lualib.h
|
|
||||||
ltm.o: ltm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
|
||||||
lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h
|
|
||||||
lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h
|
|
||||||
lutf8lib.o: lutf8lib.c lua.h luaconf.h lauxlib.h lualib.h
|
|
||||||
lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
|
|
||||||
lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
|
|
||||||
lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \
|
|
||||||
lzio.h
|
|
||||||
|
|
||||||
# (end of Makefile)
|
# (end of Makefile)
|
||||||
|
|||||||
Reference in New Issue
Block a user